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 for PROPERTY in $* ; do 79 if [ "$PROPERTY" = "$FIELD" ] ; then 80 echo "$LINE" 81 break 82 fi 83 done 84 85 # Otherwise, select all properties. 86 87 else 88 echo "$LINE" 89 fi 90 fi 91 92 # End sections on blank lines. 93 94 if [ ! "$LINE" ] ; then 95 SECTION= 96 fi 97 done 98 } 99 100 # get_field <string> 101 # 102 # Emit the field name of a property definition. 103 104 get_field() 105 { 106 echo "$1" | cut -d: -f1 107 } 108 109 # get_value <string> 110 # 111 # Emit the value of a property definition. 112 113 get_value() 114 { 115 echo "$1" | cut -d: -f2 | sed 's/^\s*//' 116 } 117 118 # search <string> <file> 119 # 120 # Search for the given string in the input, returning the line number. 121 122 search() 123 { 124 grep -h -n "$1" "$2" | cut -d: -f1 125 } 126 127 # set_value <name> <value> 128 # 129 # Set the parameter of the given name to value. 130 131 set_value() 132 { 133 eval "$1=\"\$2\"" 134 } 135 136 # test_align_cylinder <args> 137 # 138 # Test for the cylinder alignment option. 139 140 test_align_cylinder() 141 { 142 if [ "$1" = '--align' ] ; then 143 ALIGN_CYLINDER="$1" 144 return 0 145 else 146 ALIGN_CYLINDER= 147 return 1 148 fi 149 } 150 151 # Common messages. 152 153 align_cylinder_description() 154 { 155 cat <<EOF 156 If the --align option is specified, align partitions to cylinders for the 157 satisfaction of earlier sfdisk versions that are obsessed with cylinders, heads 158 and sectors. 159 EOF 160 } 161 162 show_definitions_description() 163 { 164 cat <<EOF 165 If the -a, --all or --defs options are specified, all hardware definitions are 166 emitted and no further actions are taken. 167 EOF 168 }