Debian Codegraph @

Bootdisks/bp2cf

raw
1 #!/bin/sh
2 ###############################################################################
3 # BRLTTY - A background process providing access to the console screen (when in
4 # text mode) for a blind person using a refreshable braille display.
5 #
6 # Copyright (C) 1995-2026 by The BRLTTY Developers.
7 #
8 # BRLTTY comes with ABSOLUTELY NO WARRANTY.
9 #
10 # This is free software, placed under the terms of the
11 # GNU Lesser General Public License, as published by the Free Software
12 # Foundation; either version 2.1 of the License, or (at your option) any
13 # later version. Please see the file LICENSE-LGPL for details.
14 #
15 # Web Page: http://brltty.app/
16 #
17 # This software is maintained by Dave Mielke <[email protected]>.
18 ###############################################################################
19
20 # Convert the boot parameter to configuration file directives.
21 # If /proc is mounted then use the brltty= boot parameter in /proc/cmdline.
22 # If /proc is not mounted then use the brltty environment variable.
23 # Invoke with -h for usage information.
24
25 programName="${0##*/}"
26 programMessage()
27 {
28 echo 2>&1 "${programName}: ${1}"
29 }
30 syntaxError()
31 {
32 programMessage "${1}"
33 exit 2
34 }
35 internalError()
36 {
37 programMessage "${1}"
38 exit 3
39 }
40
41 configurationFile=""
42 requestedAction=create
43 deviceTranslation=none
44 requestedParameter=""
45 OPTIND=1
46 while getopts ":f:cundop:h" option
47 do
48 case "${option}"
49 in
50 f) configurationFile="${OPTARG}";;
51 c) requestedAction=create;;
52 u) requestedAction=update;;
53 n) deviceTranslation=none;;
54 d) deviceTranslation=devfs;;
55 o) deviceTranslation=old;;
56 p) requestedParameter="${OPTARG}";;
57 h)
58 cat <<EOF
59 Usage: ${programName} [option ...]
60 -f file The configuration file to create/update.
61 -c Create the configuration file (write to stdout if no -f).
62 -u Update the configuration file (copying from stdin to stdout if no -f).
63 -n Do not translate device paths.
64 -d Do old-style to devfs device path translation.
65 -o Do devfs to old-style device path translation.
66 -p [driver][,[device][,[table]]]
67 Explicitly specify the boot parameter.
68 -h Display this usage summary.
69 EOF
70 exit 0
71 ;;
72 \?) syntaxError "unknown option: -${OPTARG}";;
73 :) syntaxError "missing value: -${OPTARG}";;
74 *) internalError "unimplemented option: -${option}";;
75 esac
76 done
77 shift "`expr $OPTIND - 1`"
78 [ "${#}" -eq 0 ] || syntaxError "too many parameters."
79
80 case "${requestedAction}"
81 in
82 create)
83 putConfigurationLine()
84 {
85 echo "${1}" || exit 4
86 }
87 startConfigurationFile()
88 {
89 [ -n "${configurationFile}" ] && exec >"${configurationFile}"
90 putConfigurationLine "`makeHeaderLine Created`"
91 putConfigurationLine "`makeParameterLine`"
92 putConfigurationLine ""
93 }
94 putConfigurationDirective()
95 {
96 putConfigurationLine "${1} ${2}"
97 }
98 finalizeConfigurationFile()
99 {
100 :
101 }
102 ;;
103 update)
104 putSedCommand()
105 {
106 sedScript="${sedScript}
107 ${1}"
108 }
109 startConfigurationFile()
110 {
111 if [ -n "${configurationFile}" ]
112 then
113 [ -e "${configurationFile}" ] || syntaxError "file not found: ${configurationFile}"
114 [ -f "${configurationFile}" ] || syntaxError "not a file: ${configurationFile}"
115 [ -r "${configurationFile}" ] || syntaxError "file not readable: ${configurationFile}"
116 [ -w "${configurationFile}" ] || syntaxError "file not writable: ${configurationFile}"
117 outputFile="${configurationFile}.new"
118 exec <"${configurationFile}" >"${outputFile}"
119 fi
120 sedScript=""
121 putSedCommand "\$a\\
122 \\
123 `makeHeaderLine Updated`\\
124 `makeParameterLine`\\
125 "
126 }
127 putConfigurationDirective()
128 {
129 value="`echo "${2}" | sed -e 's%\\([/\\]\\)%\\\\\\1%g'`"
130 putSedCommand "/^[[:space:]]*${1}[[:space:]]/s/^/#/"
131 putSedCommand "\$a${1} ${value}"
132 }
133 finalizeConfigurationFile()
134 {
135 sed -e "${sedScript}"
136 [ -n "${outputFile}" ] && mv -f "${outputFile}" "${configurationFile}"
137 }
138 ;;
139 *) internalError "unimplemented action: ${requestedAction}";;
140 esac
141
142 translateDevice_none()
143 {
144 :
145 }
146 translateDevice_devfs()
147 {
148 minor="${device#ttyS}"
149 if [ "${minor}" != "${device}" ]
150 then
151 device="tts/${minor}"
152 return 0
153 fi
154 minor="${device#lp}"
155 if [ "${minor}" != "${device}" ]
156 then
157 device="printers/${minor}"
158 return 0
159 fi
160 programMessage "unsupported old-style device: ${device}"
161 }
162 translateDevice_old()
163 {
164 major="${device%%/*}"
165 if [ "${major}" != "${device}" ]
166 then
167 minor="${device#*/}"
168 case "${major}"
169 in
170 tts) devfs="ttyS${minor}";;
171 printers) devfs="lp${minor}";;
172 esac
173 fi
174 if [ -n "${devfs}" ]
175 then
176 device="${devfs}"
177 else
178 programMessage "unsupported devfs device: ${device}"
179 fi
180 }
181
182 makeHeaderLine()
183 {
184 echo "# ${1} by brltty-bp2cf`date +' on %Y-%m-%d at %H:%M:%S %Z (UTC%z)'`."
185 }
186 makeParameterLine()
187 {
188 echo "# Boot Parameter:${bootParameter}"
189 }
190 putConfigurationFile()
191 {
192 startConfigurationFile
193 [ -n "${brailleDriver}" ] && putConfigurationDirective "braille-driver" "${brailleDriver}"
194 [ -n "${brailleDevice}" ] && {
195 device="`echo "${brailleDevice}" | sed -e 's%//*%/%g' -e 's%^/dev/%%'`"
196 if [ "${device#/}" = "${device}" ]
197 then
198 translateDevice_${deviceTranslation}
199 fi
200 putConfigurationDirective "braille-device" "${device}"
201 }
202 [ -n "${textTable}" ] && putConfigurationDirective "text-table" "${textTable}"
203 finalizeConfigurationFile
204 }
205 parseBootParameter()
206 {
207 bootParameter="${bootParameter} ${1}"
208 number=1
209 while [ "${number}" -le 3 ]
210 do
211 cut="cut -d, -f${number}"
212 [ "${number}" -gt 1 ] && cut="${cut} -s"
213 operand="`echo ${1} | ${cut}`"
214 if [ -n "${operand}" ]
215 then
216 case "${number}"
217 in
218 1) brailleDriver="${operand}";;
219 2) brailleDevice="${operand}";;
220 3) textTable="${operand}";;
221 esac
222 fi
223 number="`expr ${number} + 1`"
224 done
225 }
226 putBootParameter()
227 {
228 parseBootParameter "${1}"
229 putConfigurationFile
230 }
231 parseBootCommand()
232 {
233 found=false
234 while [ "${#}" -gt 0 ]
235 do
236 case "${1}"
237 in
238 "brltty="*)
239 found=true
240 parseBootParameter "${1#*=}"
241 ;;
242 esac
243 shift
244 done
245 "${found}" && putConfigurationFile
246 }
247
248 brailleDriver=""
249 brailleDevice=""
250 textTable=""
251 bootCommandFile="/proc/cmdline"
252 if [ -n "${requestedParameter}" ]
253 then
254 putBootParameter "${requestedParameter}"
255 elif [ -f "${bootCommandFile}" ]
256 then
257 parseBootCommand `cat "${bootCommandFile}"`
258 elif [ -n "${brltty}" ]
259 then
260 putBootParameter "${brltty}"
261 fi
262 exit 0