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