3 # Copyright (c) 2013 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 ############################################################ INCLUDES
31 # Prevent common.subr from auto initializing debugging (this is not an inter-
32 # active utility that requires debugging; also `-d' has been repurposed).
34 DEBUG_SELF_INITIALIZE=NO
36 BSDCFG_SHARE="/usr/share/bsdconfig"
37 . $BSDCFG_SHARE/common.subr || exit 1
38 f_dprintf "%s: loading includes..." "$0"
40 BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="includes"
41 f_include_lang $BSDCFG_LIBE/include/messages.subr
42 f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr
44 f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&
47 ############################################################ GLOBALS
57 ############################################################ FUNCTIONS
59 # show_functions $file
61 # Show the functions in the given include file.
67 local pattern="${FUNC_PATTERN:-.*}"
69 -v use_color=${USE_COLOR:-0} \
71 -v show_desc=${SHOW_DESC:-0} '
72 function _asorti(src, dest)
76 # Copy src indices to dest and calculate array length
77 for (i in src) dest[++nitems] = i
79 # Sort the array of indices (dest) using insertion sort method
80 for (i = 1; i <= nitems; k = i++)
83 while ((k > 0) && (dest[k] > idx))
95 if (!match($2, re)) next
98 syntax[fn] = sprintf("+%s
\e[1;31m%s
\e[0m%s\n",
99 substr($0, 2, RSTART),
100 substr($0, 2 + RSTART, RLENGTH),
101 substr($0, 2 + RSTART + RLENGTH))
103 syntax[fn] = "+" substr($0, 2) "\n"
107 print_more = substr($0, length($0)) == "\\"
109 if (show_desc && print_more) {
112 syntax[fn] = syntax[fn] " " substr($0, 2) "\n"
116 } else while (print_more) {
118 syntax[fn] = syntax[fn] " " substr($0, 2) "\n"
119 print_more = substr($0, length($0)) == "\\"
123 n = _asorti(syntax, sorted_indices)
124 for (i = 1; i <= n; i++)
125 printf "%s", syntax[sorted_indices[i]]
127 if [ "$output" ]; then
128 if [ ! "$SHOW_FUNCS" ]; then
132 if [ "$FUNC_PATTERN" ]; then
133 printf ">>> $msg_functions_in_matching\n" \
134 "$file" "$FUNC_PATTERN"
136 printf ">>> $msg_functions_in\n" "$file"
139 echo # blank line to simplify awk(1)-based reparse
143 ############################################################ MAIN
145 # Incorporate rc-file if it exists
146 [ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"
148 # Are we in a terminal?
149 [ -t 1 ] || USE_COLOR=
152 # Process command-line arguments
154 while getopts adfF:hn flag; do
157 d) SHOW_DESC=1 SHOW_FUNCS=1 ;;
159 F) FUNC_PATTERN="$OPTARG" ;;
161 h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;
164 shift $(( $OPTIND - 1 ))
166 # cd(1) to `share' dir so relative paths work for find and positional args
167 cd $BSDCFG_SHARE || f_die # Pedantic
170 # If given an argument, operate on it specifically (implied `-f') and exit
172 [ $# -gt 0 ] && SHOW_FUNCS=1
173 for include in "$@"; do
174 # See if they've just omitted the `*.subr' suffix
175 [ -f "$include.subr" -a ! -f "$include" ] && include="$include.subr"
176 if [ ! -f "$include" ]; then
177 printf "$msg_no_such_file_or_directory\n" "$0" "$include"
179 elif [ ! -r "$include" ]; then
180 printf "$msg_permission_denied\n" "$0" "$include"
183 show_include "$include" || f_die
186 # Exit if we processed some include arguments
187 [ $# -gt 0 ] && exit $SUCCESS
190 # Operate an all known include files
191 # NB: If we get this far, we had no include arguments
193 find -s . -type f -and -iname '*.subr' | while read file; do
194 if [ "$SHOW_FUNCS" -o "$FUNC_PATTERN" ]; then
203 ################################################################################
205 ################################################################################