]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/bsdconfig/share/common.subr
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / bsdconfig / share / common.subr
1 if [ ! "$_COMMON_SUBR" ]; then _COMMON_SUBR=1
2 #
3 # Copyright (c) 2012 Ron McDowell
4 # Copyright (c) 2012-2013 Devin Teske
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29 #
30 ############################################################ CONFIGURATION
31
32 #
33 # Default file descriptors to link to stdout/stderr for passthru allowing
34 # redirection within a sub-shell to bypass directly to the terminal.
35 #
36 : ${TERMINAL_STDOUT_PASSTHRU:=3}}
37 : ${TERMINAL_STDERR_PASSTHRU:=4}}
38
39 ############################################################ GLOBALS
40
41 #
42 # Program name
43 #
44 pgm="${0##*/}"
45
46 #
47 # Program arguments
48 #
49 ARGC="$#"
50 ARGV="$@"
51
52 #
53 # Global exit status variables
54 #
55 SUCCESS=0
56 FAILURE=1
57
58 #
59 # Operating environment details
60 #
61 export UNAME_S="$(uname -s)" # Operating System (i.e. FreeBSD)
62 export UNAME_P="$(uname -p)" # Processor Architecture (i.e. i386)
63 export UNAME_R="$(uname -r)" # Release Level (i.e. X.Y-RELEASE)
64
65 #
66 # Default behavior is to call f_debug_init() automatically when loaded.
67 #
68 : ${DEBUG_SELF_INITIALIZE=1}
69
70 #
71 # Define standard optstring arguments that should be supported by all programs
72 # using this include (unless DEBUG_SELF_INITIALIZE is set to NULL to prevent
73 # f_debug_init() from autamatically processing "$@" for the below arguments):
74 #
75 #       d       Sets $debug to 1
76 #       D:      Sets $debugFile to $OPTARG
77 #
78 GETOPTS_STDARGS="dD:"
79
80 #
81 # The getopts builtin will return 1 either when the end of "$@" or the first
82 # invalid flag is reached. This makes it impossible to determine if you've
83 # processed all the arguments or simply have hit an invalid flag. In the cases
84 # where we want to tolerate invalid flags (f_debug_init() for example), the
85 # following variable can be appended to your optstring argument to getopts,
86 # preventing it from prematurely returning 1 before the end of the arguments.
87 #
88 # NOTE: This assumes that all unknown flags are argument-less.
89 #
90 GETOPTS_ALLFLAGS="abcdefghijklmnopqrstuvwxyz"
91 GETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}ABCDEFGHIJKLMNOPQRSTUVWXYZ"
92 GETOPTS_ALLFLAGS="${GETOPTS_ALLFLAGS}0123456789"
93
94 #
95 # When we get included, f_debug_init() will fire (unless $DEBUG_SELF_INITIALIZE
96 # is set to disable automatic initialization) and process "$@" for a few global
97 # options such as `-d' and/or `-D file'. However, if your program takes custom
98 # flags that take arguments, this automatic processing may fail unexpectedly.
99 #
100 # The solution to this problem is to pre-define (before including this file)
101 # the following variable (which defaults to NULL) to indicate that there are
102 # extra flags that should be considered when performing automatic processing of
103 # globally persistent flags.
104 #
105 : ${GETOPTS_EXTRA:=}
106
107 ############################################################ FUNCTIONS
108
109 # f_dprintf $fmt [ $opts ... ]
110 #
111 # Sensible debug function. Override in ~/.bsdconfigrc if desired.
112 # See /usr/share/examples/bsdconfig/bsdconfigrc for example.
113 #
114 # If $debug is set and non-NULL, prints DEBUG info using printf(1) syntax:
115 #       + To $debugFile, if set and non-NULL
116 #       + To standard output if $debugFile is either NULL or unset
117 #       + To both if $debugFile begins with a single plus-sign (`+')
118 #
119 f_dprintf()
120 {
121         [ "$debug" ] || return $SUCCESS
122         local fmt="$1"; shift
123         case "$debugFile" in ""|+*)
124         printf "DEBUG: $fmt${fmt:+\n}" "$@" >&${TERMINAL_STDOUT_PASSTHRU:-1}
125         esac
126         [ "${debugFile#+}" ] &&
127                 printf "DEBUG: $fmt${fmt:+\n}" "$@" >> "${debugFile#+}"
128         return $SUCCESS
129 }
130
131 # f_debug_init
132 #
133 # Initialize debugging. Truncates $debugFile to zero bytes if set.
134 #
135 f_debug_init()
136 {
137         #
138         # Process stored command-line arguments
139         #
140         set -- $ARGV
141         local OPTIND
142         f_dprintf "f_debug_init: ARGV=[%s] GETOPTS_STDARGS=[%s]" \
143                   "$ARGV" "$GETOPTS_STDARGS"
144         while getopts "$GETOPTS_STDARGS$GETOPTS_EXTRA$GETOPTS_ALLFLAGS" flag \
145         > /dev/null; do
146                 case "$flag" in
147                 d) debug=1 ;;
148                 D) debugFile="$OPTARG" ;;
149                 esac
150         done
151         shift $(( $OPTIND - 1 ))
152         f_dprintf "f_debug_init: debug=[%s] debugFile=[%s]" \
153                   "$debug" "$debugFile"
154
155         #
156         # Automagically enable debugging if debugFile is set (and non-NULL)
157         #
158         [ "$debugFile" ] && { [ "${debug+set}" ] || debug=1; }
159
160         #
161         # Make debugging persistant if set
162         #
163         [ "$debug" ] && export debug
164         [ "$debugFile" ] && export debugFile
165
166         #
167         # Truncate the debug file upon. Note that we will trim a leading plus
168         # (`+') from the value of debugFile to support persistant meaning that
169         # f_dprintf() should print both to standard output and $debugFile
170         # (minus the leading plus, of course).
171         #
172         local _debug_file="${debugFile#+}"
173         if [ "$_debug_file" ]; then
174                 if ( umask 022 && :> "$_debug_file" ); then
175                         f_dprintf "Successfully initialized debugFile \`%s'" \
176                                   "$_debug_file"
177                         [ "${debug+set}" ] ||
178                                 debug=1 # turn debugging on if not set
179                 else
180                         unset debugFile
181                         f_dprintf "Unable to initialize debugFile \`%s'" \
182                                   "$_debug_file"
183                 fi
184         fi
185 }
186
187 # f_err $fmt [ $opts ... ]
188 #
189 # Print a message to stderr (fd=2).
190 #
191 f_err()
192 {
193         printf "$@" >&${TERMINAL_STDERR_PASSTHRU:-2}
194 }
195
196 # f_quietly $command [ $arguments ... ]
197 #
198 # Run a command quietly (quell any output to stdout or stderr)
199 #
200 f_quietly()
201 {
202         "$@" > /dev/null 2>&1
203 }
204
205 # f_have $anything ...
206 #
207 # A wrapper to the `type' built-in. Returns true if argument is a valid shell
208 # built-in, keyword, or externally-tracked binary, otherwise false.
209 #
210 f_have()
211 {
212         f_quietly type "$@"
213 }
214
215 # f_getvar $var_to_get [$var_to_set]
216 #
217 # Utility function designed to go along with the already-builtin setvar.
218 # Allows clean variable name indirection without forking or sub-shells.
219 #
220 # Returns error status if the requested variable ($var_to_get) is not set.
221 #
222 # If $var_to_set is missing or NULL, the value of $var_to_get is printed to
223 # standard output for capturing in a sub-shell (which is less-recommended
224 # because of performance degredation; for example, when called in a loop).
225 #
226 f_getvar()
227 {
228         local __var_to_get="$1" __var_to_set="$2"
229         [ "$__var_to_set" ] || local value
230         eval ${__var_to_set:-value}=\"\${$__var_to_get}\"
231         eval [ \"\${$__var_to_get+set}\" ]
232         local __retval=$?
233         eval f_dprintf '"f_getvar: var=[%s] value=[%s] r=%u"' \
234                 \"\$__var_to_get\" \"\$${__var_to_set:-value}\" \$__retval
235         [ "$__var_to_set" ] || { [ "$value" ] && echo "$value"; }
236         return $__retval
237 }
238
239 # f_isset $var
240 #
241 # Check if variable $var is set. Returns success if variable is set, otherwise
242 # returns failure.
243 #
244 f_isset()
245 {
246         eval [ \"\${${1%%[$IFS]*}+set}\" ]
247 }
248
249 # f_die [ $status [ $fmt [ $opts ... ]]]
250 #
251 # Abruptly terminate due to an error optionally displaying a message in a
252 # dialog box using printf(1) syntax.
253 #
254 f_die()
255 {
256         local status=$FAILURE
257
258         # If there is at least one argument, take it as the status
259         if [ $# -gt 0 ]; then
260                 status=$1
261                 shift 1 # status
262         fi
263
264         # If there are still arguments left, pass them to f_show_msg
265         [ $# -gt 0 ] && f_show_msg "$@"
266
267         # Optionally call f_clean_up() function if it exists
268         f_have f_clean_up && f_clean_up
269
270         exit $status
271 }
272
273 # f_interrupt
274 #
275 # Interrupt handler.
276 #
277 f_interrupt()
278 {
279         exec 2>&1 # fix sh(1) bug where stderr gets lost within async-trap
280         f_die
281 }
282
283 # f_show_info $fmt [ $opts ... ]
284 #
285 # Display a message in a dialog infobox using printf(1) syntax.
286 #
287 f_show_info()
288 {
289         local msg
290         msg=$( printf "$@" )
291
292         #
293         # Use f_dialog_infobox from dialog.subr if possible, otherwise fall
294         # back to dialog(1) (without options, making it obvious when using
295         # un-aided system dialog).
296         #
297         if f_have f_dialog_info; then
298                 f_dialog_info "$msg"
299         else
300                 dialog --infobox "$msg" 0 0
301         fi
302 }
303
304 # f_show_msg $fmt [ $opts ... ]
305 #
306 # Display a message in a dialog box using printf(1) syntax.
307 #
308 f_show_msg()
309 {
310         local msg
311         msg=$( printf "$@" )
312
313         #
314         # Use f_dialog_msgbox from dialog.subr if possible, otherwise fall
315         # back to dialog(1) (without options, making it obvious when using
316         # un-aided system dialog).
317         #
318         if f_have f_dialog_msgbox; then
319                 f_dialog_msgbox "$msg"
320         else
321                 dialog --msgbox "$msg" 0 0
322         fi
323 }
324
325
326 # f_yesno $fmt [ $opts ... ]
327 #
328 # Display a message in a dialog yes/no box using printf(1) syntax.
329 #
330 f_yesno()
331 {
332         local msg
333         msg=$( printf "$@" )
334
335         #
336         # Use f_dialog_yesno from dialog.subr if possible, otherwise fall
337         # back to dialog(1) (without options, making it obvious when using
338         # un-aided system dialog).
339         #
340         if f_have f_dialog_yesno; then
341                 f_dialog_yesno "$msg"
342         else
343                 dialog --yesno "$msg" 0 0
344         fi
345 }
346
347 # f_noyes $fmt [ $opts ... ]
348 #
349 # Display a message in a dialog yes/no box using printf(1) syntax.
350 # NOTE: THis is just like the f_yesno function except "No" is default.
351 #
352 f_noyes()
353 {
354         local msg
355         msg=$( printf "$@" )
356
357         #
358         # Use f_dialog_noyes from dialog.subr if possible, otherwise fall
359         # back to dialog(1) (without options, making it obvious when using
360         # un-aided system dialog).
361         #
362         if f_have f_dialog_noyes; then
363                 f_dialog_noyes "$msg"
364         else
365                 dialog --defaultno --yesno "$msg" 0 0
366         fi
367 }
368
369 # f_show_help $file
370 #
371 # Display a language help-file. Automatically takes $LANG and $LC_ALL into
372 # consideration when displaying $file (suffix ".$LC_ALL" or ".$LANG" will
373 # automatically be added prior to loading the language help-file).
374 #
375 # If a language has been requested by setting either $LANG or $LC_ALL in the
376 # environment and the language-specific help-file does not exist we will fall
377 # back to $file without-suffix.
378 #
379 # If the language help-file does not exist, an error is displayed instead.
380 #
381 f_show_help()
382 {
383         local file="$1"
384         local lang="${LANG:-$LC_ALL}"
385
386         [ -f "$file.$lang" ] && file="$file.$lang"
387
388         #
389         # Use f_dialog_textbox from dialog.subr if possible, otherwise fall
390         # back to dialog(1) (without options, making it obvious when using
391         # un-aided system dialog).
392         #
393         if f_have f_dialog_textbox; then
394                 f_dialog_textbox "$file"
395         else
396                 dialog --msgbox "$( cat "$file" 2>&1 )" 0 0
397         fi
398 }
399
400 # f_include $file
401 #
402 # Include a shell subroutine file.
403 #
404 # If the subroutine file exists but returns error status during loading, exit
405 # is called and execution is prematurely terminated with the same error status.
406 #
407 f_include()
408 {
409         local file="$1"
410         f_dprintf "f_include: file=[%s]" "$file"
411         . "$file" || exit $?
412 }
413
414 # f_include_lang $file
415 #
416 # Include a language file. Automatically takes $LANG and $LC_ALL into
417 # consideration when including $file (suffix ".$LC_ALL" or ".$LANG" will
418 # automatically by added prior to loading the language file).
419 #
420 # No error is produced if (a) a language has been requested (by setting either
421 # $LANG or $LC_ALL in the environment) and (b) the language file does not
422 # exist -- in which case we will fall back to loading $file without-suffix.
423 #
424 # If the language file exists but returns error status during loading, exit
425 # is called and execution is prematurely terminated with the same error status.
426 #
427 f_include_lang()
428 {
429         local file="$1"
430         local lang="${LANG:-$LC_ALL}"
431
432         f_dprintf "f_include_lang: file=[%s] lang=[%s]" "$file" "$lang"
433         if [ -f "$file.$lang" ]; then
434                 . "$file.$lang" || exit $?
435         else
436                 . "$file" || exit $?
437         fi
438 }
439
440 # f_usage $file [ $key1 $value1 ... ]
441 #
442 # Display USAGE file with optional pre-processor macro definitions. The first
443 # argument is the template file containing the usage text to be displayed. If
444 # $LANG or $LC_ALL (in order of preference, respectively) is set, ".encoding"
445 # will automatically be appended as a suffix to the provided $file pathname.
446 #
447 # When processing $file, output begins at the first line containing that is
448 # (a) not a comment, (b) not empty, and (c) is not pure-whitespace. All lines
449 # appearing after this first-line are output, including (a) comments (b) empty
450 # lines, and (c) lines that are purely whitespace-only.
451 #
452 # If additional arguments appear after $file, substitutions are made while
453 # printing the contents of the USAGE file. The pre-processor macro syntax is in
454 # the style of autoconf(1), for example:
455 #
456 #       f_usage $file "FOO" "BAR"
457 #
458 # Will cause instances of "@FOO@" appearing in $file to be replaced with the
459 # text "BAR" before bering printed to the screen.
460 #
461 # This function is a two-parter. Below is the awk(1) portion of the function,
462 # afterward is the sh(1) function which utilizes the below awk script.
463 #
464 f_usage_awk='
465 BEGIN { found = 0 }
466 {
467         if ( !found && $0 ~ /^[[:space:]]*($|#)/ ) next
468         found = 1
469         print
470 }
471 '
472 f_usage()
473 {
474         local file="$1"
475         local lang="${LANG:-$LC_ALL}"
476
477         f_dprintf "f_usage: file=[%s] lang=[%s]" "$file" "$lang"
478
479         shift 1 # file
480
481         local usage
482         if [ -f "$file.$lang" ]; then
483                 usage=$( awk "$f_usage_awk" "$file.$lang" ) || exit $FAILURE
484         else
485                 usage=$( awk "$f_usage_awk" "$file" ) || exit $FAILURE
486         fi
487
488         while [ $# -gt 0 ]; do
489                 local key="$1"
490                 export value="$2"
491                 usage=$( echo "$usage" | awk \
492                         "{ gsub(/@$key@/, ENVIRON[\"value\"]); print }" )
493                 shift 2
494         done
495
496         f_err "%s\n" "$usage"
497
498         exit $FAILURE
499 }
500
501 # f_index_file $keyword
502 #
503 # Process all INDEX files known to bsdconfig and return the path to first file
504 # containing a menu_selection line with a keyword portion matching $keyword.
505 #
506 # If $LANG or $LC_ALL (in order of preference, respectively) is set,
507 # "INDEX.encoding" files will be searched first.
508 #
509 # If no file is found, error status is returned along with the NULL string.
510 #
511 # This function is a two-parter. Below is the awk(1) portion of the function,
512 # afterward is the sh(1) function which utilizes the below awk script.
513 #
514 f_index_file_awk='
515 # Variables that should be defined on the invocation line:
516 #       -v keyword="keyword"
517 BEGIN { found = 0 }
518 ( $0 ~ "^menu_selection=\"" keyword "\\|" ) {
519         print FILENAME
520         found++
521         exit
522 }
523 END { exit ! found }
524 '
525 f_index_file()
526 {
527         local keyword="$1"
528         local lang="${LANG:-$LC_ALL}"
529
530         f_dprintf "f_index_file: keyword=[%s] lang=[%s]" "$keyword" "$lang"
531
532         if [ "$lang" ]; then
533                 awk -v keyword="$keyword" "$f_index_file_awk" \
534                         $BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX.$lang && return
535                 # No match, fall-thru to non-i18n sources
536         fi
537         awk -v keyword="$keyword" "$f_index_file_awk" \
538                 $BSDCFG_LIBE${BSDCFG_LIBE:+/}*/INDEX && return
539
540         # No match? Fall-thru to `local' libexec sources (add-on modules)
541
542         [ "$BSDCFG_LOCAL_LIBE" ] || return $FAILURE
543         if [ "$lang" ]; then
544                 awk -v keyword="$keyword" "$f_index_file_awk" \
545                         $BSDCFG_LOCAL_LIBE/*/INDEX.$lang && return
546                 # No match, fall-thru to non-i18n sources
547         fi
548         awk -v keyword="$keyword" "$f_index_file_awk" \
549                 $BSDCFG_LOCAL_LIBE/*/INDEX
550 }
551
552 # f_index_menusel_keyword $indexfile $pgm
553 #
554 # Process $indexfile and return only the keyword portion of the menu_selection
555 # line with a command portion matching $pgm.
556 #
557 # This function is for internationalization (i18n) mapping of the on-disk
558 # scriptname ($pgm) into the localized language (given language-specific
559 # $indexfile). If $LANG or $LC_ALL (in orderder of preference, respectively) is
560 # set, ".encoding" will automatically be appended as a suffix to the provided
561 # $indexfile pathname.
562 #
563 # If, within $indexfile, multiple $menu_selection values map to $pgm, only the
564 # first one will be returned. If no mapping can be made, the NULL string is
565 # returned.
566 #
567 # If $indexfile does not exist, error status is returned with NULL.
568 #
569 # This function is a two-parter. Below is the awk(1) portion of the function,
570 # afterward is the sh(1) function which utilizes the below awk script.
571 #
572 f_index_menusel_keyword_awk='
573 # Variables that should be defined on the invocation line:
574 #       -v pgm="program_name"
575 #
576 BEGIN {
577         prefix = "menu_selection=\""
578         plen = length(prefix)
579         found = 0
580 }
581 {
582         if (!match($0, "^" prefix ".*\\|.*\"")) next
583
584         keyword = command = substr($0, plen + 1, RLENGTH - plen - 1)
585         sub(/^.*\|/, "", command)
586         sub(/\|.*$/, "", keyword)
587
588         if ( command == pgm )
589         {
590                 print keyword
591                 found++
592                 exit
593         }
594 }
595 END { exit ! found }
596 '
597 f_index_menusel_keyword()
598 {
599         local indexfile="$1" pgm="$2"
600         local lang="${LANG:-$LC_ALL}"
601
602         f_dprintf "f_index_menusel_keyword: index=[%s] pgm=[%s] lang=[%s]" \
603                   "$indexfile" "$pgm" "$lang"
604
605         if [ -f "$indexfile.$lang" ]; then
606                 awk -v pgm="$pgm" \
607                         "$f_index_menusel_keyword_awk" \
608                         "$indexfile.$lang"
609         elif [ -f "$indexfile" ]; then
610                 awk -v pgm="$pgm" \
611                         "$f_index_menusel_keyword_awk" \
612                         "$indexfile"
613         fi
614 }
615
616 # f_index_menusel_command $indexfile $keyword
617 #
618 # Process $indexfile and return only the command portion of the menu_selection
619 # line with a keyword portion matching $keyword.
620 #
621 # This function is for mapping [possibly international] keywords into the
622 # command to be executed. If $LANG or $LC_ALL (order of preference) is set,
623 # ".encoding" will automatically be appended as a suffix to the provided
624 # $indexfile pathname.
625 #
626 # If, within $indexfile, multiple $menu_selection values map to $keyword, only
627 # the first one will be returned. If no mapping can be made, the NULL string is
628 # returned.
629 #
630 # If $indexfile doesn't exist, error status is returned with NULL.
631 #
632 # This function is a two-parter. Below is the awk(1) portion of the function,
633 # afterward is the sh(1) function which utilizes the below awk script.
634 #
635 f_index_menusel_command_awk='
636 # Variables that should be defined on the invocation line:
637 #       -v key="keyword"
638 #
639 BEGIN {
640         prefix = "menu_selection=\""
641         plen = length(prefix)
642         found = 0
643 }
644 {
645         if (!match($0, "^" prefix ".*\\|.*\"")) next
646
647         keyword = command = substr($0, plen + 1, RLENGTH - plen - 1)
648         sub(/^.*\|/, "", command)
649         sub(/\|.*$/, "", keyword)
650
651         if ( keyword == key )
652         {
653                 print command
654                 found++
655                 exit
656         }
657 }
658 END { exit ! found }
659 '
660 f_index_menusel_command()
661 {
662         local indexfile="$1" keyword="$2" command
663         local lang="${LANG:-$LC_ALL}"
664
665         f_dprintf "f_index_menusel_command: index=[%s] key=[%s] lang=[%s]" \
666                   "$indexfile" "$keyword" "$lang"
667
668         if [ -f "$indexfile.$lang" ]; then
669                 command=$( awk -v key="$keyword" \
670                                 "$f_index_menusel_command_awk" \
671                                 "$indexfile.$lang" ) || return $FAILURE
672         elif [ -f "$indexfile" ]; then
673                 command=$( awk -v key="$keyword" \
674                                 "$f_index_menusel_command_awk" \
675                                 "$indexfile" ) || return $FAILURE
676         else
677                 return $FAILURE
678         fi
679
680         #
681         # If the command pathname is not fully qualified fix-up/force to be
682         # relative to the $indexfile directory.
683         #
684         case "$command" in
685         /*) : already fully qualified ;;
686         *)
687                 local indexdir="${indexfile%/*}"
688                 [ "$indexdir" != "$indexfile" ] || indexdir="."
689                 command="$indexdir/$command"
690         esac
691
692         echo "$command"
693 }
694
695 # f_running_as_init
696 #
697 # Returns true if running as init(1).
698 #
699 f_running_as_init()
700 {
701         #
702         # When a custom init(8) performs an exec(3) to invoke a shell script,
703         # PID 1 becomes sh(1) and $PPID is set to 1 in the executed script.
704         #
705         [ ${PPID:-0} -eq 1 ] # Return status
706 }
707
708 # f_mounted $local_directory
709 #
710 # Return success if a filesystem is mounted on a particular directory.
711 #
712 f_mounted()
713 {
714         local dir="$1"
715         [ -d "$dir" ] || return $FAILURE
716         mount | grep -Eq " on $dir \([^)]+\)$"
717 }
718
719 ############################################################ MAIN
720
721 #
722 # Trap signals so we can recover gracefully
723 #
724 trap 'f_interrupt' SIGINT
725 trap 'f_die' SIGTERM SIGPIPE SIGXCPU SIGXFSZ \
726              SIGFPE SIGTRAP SIGABRT SIGSEGV
727 trap '' SIGALRM SIGPROF SIGUSR1 SIGUSR2 SIGHUP SIGVTALRM
728
729 #
730 # Clone terminal stdout/stderr so we can redirect to it from within sub-shells
731 #
732 eval exec $TERMINAL_STDOUT_PASSTHRU\>\&1
733 eval exec $TERMINAL_STDERR_PASSTHRU\>\&2
734
735 #
736 # Self-initialize unless requested otherwise
737 #
738 f_dprintf "%s: DEBUG_SELF_INITIALIZE=[%s]" \
739           dialog.subr "$DEBUG_SELF_INITIALIZE"
740 case "$DEBUG_SELF_INITIALIZE" in
741 ""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
742 *) f_debug_init
743 esac
744
745 #
746 # Log our operating environment for debugging purposes
747 #
748 f_dprintf "UNAME_S=[%s] UNAME_P=[%s] UNAME_R=[%s]" \
749           "$UNAME_S" "$UNAME_P" "$UNAME_R"
750
751 f_dprintf "%s: Successfully loaded." common.subr
752
753 fi # ! $_COMMON_SUBR