1 #!/bin/sh 2 3 # Common routines. 4 # 5 # Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> 6 # 7 # This program is free software; you can redistribute it and/or modify it under 8 # the terms of the GNU General Public License as published by the Free Software 9 # Foundation; either version 3 of the License, or (at your option) any later 10 # version. 11 # 12 # This program is distributed in the hope that it will be useful, but WITHOUT 13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 15 # details. 16 # 17 # You should have received a copy of the GNU General Public License along with 18 # this program. If not, see <http://www.gnu.org/licenses/>. 19 20 # Test for a selected device. 21 22 check_device() 23 { 24 if [ ! "$DEV" ] ; then 25 cat 1>&2 <<EOF 26 No device specified. Use the DEV environment variable to indicate a device. 27 EOF 28 exit 1 29 fi 30 31 if [ ! -e "$DEV" ] ; then 32 cat 1>&2 <<EOF 33 Device not found: $DEV 34 EOF 35 exit 1 36 fi 37 } 38 39 # filter <type> [ <property> ... ] 40 # 41 # Filter input, preserving only sections with the given type, preserving only 42 # the given properties if indicated. 43 44 filter() 45 { 46 local FIELD LINE PROPERTY SECTION TYPE 47 48 TYPE=$1 49 SECTION= 50 51 shift 1 52 53 while read LINE ; do 54 55 # Start a new section if appropriate. 56 57 if [ "$LINE" ] && [ ! "$SECTION" ] ; then 58 SECTION=`echo "$LINE" | cut -d' ' -f1` 59 60 # Produce suitable section headers. 61 62 if [ "$SECTION" = "$TYPE" ] ; then 63 echo "$LINE" 64 fi 65 66 continue 67 fi 68 69 # Produce lines from any suitable section including any blank line. 70 71 if [ "$SECTION" = "$TYPE" ] ; then 72 73 # Select appropriate properties if indicated. 74 75 if [ "$LINE" ] && [ "$1" ] ; then 76 FIELD=`echo "$LINE" | cut -d: -f1` 77 78 if is_in "$FIELD" $* ; then 79 echo "$LINE" 80 fi 81 82 # Otherwise, select all properties. 83 84 else 85 echo "$LINE" 86 fi 87 fi 88 89 # End sections on blank lines. 90 91 if [ ! "$LINE" ] ; then 92 SECTION= 93 fi 94 done 95 } 96 97 # get_field <string> 98 # 99 # Emit the field name of a property definition. 100 101 get_field() 102 { 103 echo "$1" | cut -d: -f1 104 } 105 106 # get_value <string> 107 # 108 # Emit the value of a property definition. 109 110 get_value() 111 { 112 echo "$1" | cut -d: -f2 | sed 's/^\s*//' 113 } 114 115 # is_in <string> [ <member> ... ] 116 # 117 # Test whether the given string is present amongst the given members. 118 119 is_in() 120 { 121 local STRING 122 123 STRING=$1 124 125 shift 1 126 127 for MEMBER in $* ; do 128 if [ "$STRING" = "$MEMBER" ] ; then 129 return 0 130 fi 131 done 132 133 return 1 134 } 135 136 # search <string> <file> 137 # 138 # Search for the given string in the input, returning the line number. 139 140 search() 141 { 142 grep -h -n "$1" "$2" | cut -d: -f1 143 } 144 145 # set_value <name> <value> 146 # 147 # Set the parameter of the given name to value. 148 149 set_value() 150 { 151 eval "$1=\"\$2\"" 152 } 153 154 # test_align_cylinder <args> 155 # 156 # Test for the cylinder alignment option. 157 158 test_align_cylinder() 159 { 160 if [ "$1" = '--align' ] ; then 161 ALIGN_CYLINDER="$1" 162 return 0 163 else 164 ALIGN_CYLINDER= 165 return 1 166 fi 167 } 168 169 # Common messages. 170 171 align_cylinder_description() 172 { 173 cat <<EOF 174 If the --align option is specified, align partitions to cylinders for the 175 satisfaction of earlier sfdisk versions that are obsessed with cylinders, heads 176 and sectors. 177 EOF 178 } 179 180 show_definitions_description() 181 { 182 cat <<EOF 183 If the -a, --all or --defs options are specified, all hardware definitions are 184 emitted and no further actions are taken. 185 EOF 186 }