1 #!/bin/sh 2 3 # Emit a partition summary for a device employing parameters such as the start, 4 # size, and type. 5 # 6 # Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> 7 # 8 # This program is free software; you can redistribute it and/or modify it under 9 # the terms of the GNU General Public License as published by the Free Software 10 # Foundation; either version 3 of the License, or (at your option) any later 11 # version. 12 # 13 # This program is distributed in the hope that it will be useful, but WITHOUT 14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 # details. 17 # 18 # You should have received a copy of the GNU General Public License along with 19 # this program. If not, see <http://www.gnu.org/licenses/>. 20 21 PROGNAME=`basename "$0"` 22 THISDIR=`dirname "$0"` 23 24 25 26 # Emit any partition details as a complete record. 27 28 emit_partition() 29 { 30 echo "${START:--}\t${SIZE:--}\t${TYPE:--}" 31 } 32 33 # Reset the current partition details. 34 35 reset_partition() 36 { 37 START= 38 SIZE= 39 TYPE= 40 } 41 42 # Emit the current partition details and proceed to the next partition. 43 44 next_partition() 45 { 46 if [ "$START" ] || [ "$SIZE" ] || [ "$TYPE" ] ; then 47 emit_partition 48 reset_partition 49 fi 50 } 51 52 53 54 # Emit the help message if requested. 55 56 if [ "$1" = '--help' ] ; then 57 cat 1>&2 <<EOF 58 Usage: $PROGNAME ( -f <type> | -p <start> | -s <size> )... 59 60 Produce partition descriptions, indicating partition type, start position and 61 size for each partition. Each new occurrence of an active option starts a new 62 partition description. 63 64 Each line of the produced description is tab-separated with '-' indicating an 65 empty field. 66 EOF 67 exit 0 68 fi 69 70 # Process the arguments, building a partition description. 71 72 reset_partition 73 74 while [ "$1" ] ; do 75 case "$1" in 76 -f ) 77 if [ "$TYPE" ] ; then next_partition ; fi 78 TYPE="$2" 79 shift 2 80 ;; 81 -p ) 82 if [ "$START" ] ; then next_partition ; fi 83 START="$2" 84 shift 2 85 ;; 86 -s ) 87 if [ "$SIZE" ] ; then next_partition ; fi 88 SIZE="$2" 89 shift 2 90 ;; 91 * ) 92 shift 1 93 ;; 94 esac 95 done 96 97 # Terminate any unfinished partition. 98 99 next_partition