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