1258400Sdteske#!/bin/sh 2258400Sdteske#- 3258400Sdteske# Copyright (c) 2013 Devin Teske 4258400Sdteske# All rights reserved. 5258400Sdteske# 6258400Sdteske# Redistribution and use in source and binary forms, with or without 7258400Sdteske# modification, are permitted provided that the following conditions 8258400Sdteske# are met: 9258400Sdteske# 1. Redistributions of source code must retain the above copyright 10258400Sdteske# notice, this list of conditions and the following disclaimer. 11258400Sdteske# 2. Redistributions in binary form must reproduce the above copyright 12258400Sdteske# notice, this list of conditions and the following disclaimer in the 13258400Sdteske# documentation and/or other materials provided with the distribution. 14258400Sdteske# 15258400Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16258400Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17258400Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18258400Sdteske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19258400Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20258400Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21258400Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22258400Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23258400Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24258400Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25258400Sdteske# SUCH DAMAGE. 26258400Sdteske# 27258400Sdteske# $FreeBSD$ 28258400Sdteske# 29258400Sdteske############################################################ INCLUDES 30258400Sdteske 31258400Sdteske# Prevent common.subr from auto initializing debugging (this is not an inter- 32263791Sdteske# active utility that requires debugging; also `-d' has been repurposed). 33258400Sdteske# 34258400SdteskeDEBUG_SELF_INITIALIZE=NO 35258400Sdteske 36258400SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 37258400Sdteske. $BSDCFG_SHARE/common.subr || exit 1 38258400Sdteskef_dprintf "%s: loading includes..." "$0" 39258400Sdteske 40258400SdteskeBSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="includes" 41258400Sdteskef_include_lang $BSDCFG_LIBE/include/messages.subr 42258400Sdteskef_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr 43258400Sdteske 44263791Sdteskef_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm && 45263791Sdteske pgm="${ipgm:-$pgm}" 46258400Sdteske 47258400Sdteske############################################################ GLOBALS 48258400Sdteske 49258400Sdteske# 50258400Sdteske# Options 51258400Sdteske# 52258400SdteskeUSE_COLOR=1 53263791SdteskeSHOW_DESC= 54258400SdteskeSHOW_FUNCS= 55258400SdteskeFUNC_PATTERN= 56258400Sdteske 57258400Sdteske############################################################ FUNCTIONS 58258400Sdteske 59258400Sdteske# show_functions $file 60258400Sdteske# 61258400Sdteske# Show the functions in the given include file. 62258400Sdteske# 63258400Sdteskeshow_include() 64258400Sdteske{ 65258400Sdteske local file="${1#./}" 66258400Sdteske 67258400Sdteske local pattern="${FUNC_PATTERN:-.*}" 68263791Sdteske output=$( awk \ 69263791Sdteske -v use_color=${USE_COLOR:-0} \ 70263791Sdteske -v re="$pattern" \ 71263791Sdteske -v show_desc=${SHOW_DESC:-0} ' 72263791Sdteske function asorti(src, dest) 73263791Sdteske { 74263791Sdteske # Copy src indices to dest and calculate array length 75263791Sdteske nitems = 0; for (i in src) dest[++nitems] = i 76263791Sdteske 77263791Sdteske # Sort the array of indices (dest) using insertion sort method 78263791Sdteske for (i = 1; i <= nitems; k = i++) 79263791Sdteske { 80263791Sdteske idx = dest[i] 81263791Sdteske while ((k > 0) && (dest[k] > idx)) 82263791Sdteske { 83263791Sdteske dest[k+1] = dest[k] 84263791Sdteske k-- 85263791Sdteske } 86263791Sdteske dest[k+1] = idx 87263791Sdteske } 88263791Sdteske 89263791Sdteske return nitems 90263791Sdteske } 91258400Sdteske /^$/,/^#/ { 92258400Sdteske if ($0 ~ /^# f_/) { 93258400Sdteske if (!match($2, re)) next 94263791Sdteske fn = $2 95258400Sdteske if (use_color) 96263791Sdteske syntax[fn] = sprintf("+%s[1;31m%s[0m%s\n", 97258400Sdteske substr($0, 2, RSTART), 98258400Sdteske substr($0, 2 + RSTART, RLENGTH), 99263791Sdteske substr($0, 2 + RSTART + RLENGTH)) 100258400Sdteske else 101263791Sdteske syntax[fn] = "+" substr($0, 2) "\n" 102263791Sdteske if (show_desc) 103263791Sdteske print_more = 1 104263791Sdteske else 105263791Sdteske print_more = substr($0, length($0)) == "\\" 106258400Sdteske } 107263791Sdteske if (show_desc && print_more) { 108258400Sdteske getline 109263791Sdteske while ($0 ~ /^#/) { 110263791Sdteske syntax[fn] = syntax[fn] " " substr($0, 2) "\n" 111263791Sdteske getline 112263791Sdteske } 113263791Sdteske print_more = 0 114263791Sdteske } else while (print_more) { 115263791Sdteske getline 116263791Sdteske syntax[fn] = syntax[fn] " " substr($0, 2) "\n" 117258400Sdteske print_more = substr($0, length($0)) == "\\" 118258400Sdteske } 119263791Sdteske } 120263791Sdteske END { 121263791Sdteske n = asorti(syntax, sorted_indices) 122263791Sdteske for (i = 1; i <= n; i++) 123263791Sdteske printf "%s", syntax[sorted_indices[i]] 124258400Sdteske }' "$file" ) 125258400Sdteske if [ "$output" ]; then 126258400Sdteske if [ ! "$SHOW_FUNCS" ]; then 127258400Sdteske echo "$file" 128258400Sdteske return $SUCCESS 129258400Sdteske fi 130258400Sdteske if [ "$FUNC_PATTERN" ]; then 131263791Sdteske printf ">>> $msg_functions_in_matching\n" \ 132258400Sdteske "$file" "$FUNC_PATTERN" 133258400Sdteske else 134263791Sdteske printf ">>> $msg_functions_in\n" "$file" 135258400Sdteske fi 136258400Sdteske echo "$output" 137258400Sdteske echo # blank line to simplify awk(1)-based reparse 138258400Sdteske fi 139258400Sdteske} 140258400Sdteske 141258400Sdteske############################################################ MAIN 142258400Sdteske 143258400Sdteske# Incorporate rc-file if it exists 144258400Sdteske[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc" 145258400Sdteske 146258400Sdteske# Are we in a terminal? 147258400Sdteske[ -t 1 ] || USE_COLOR= 148258400Sdteske 149258400Sdteske# 150258400Sdteske# Process command-line arguments 151258400Sdteske# 152263791Sdteskewhile getopts adfF:hn flag; do 153258400Sdteske case "$flag" in 154258400Sdteske a) USE_COLOR=1 ;; 155263791Sdteske d) SHOW_DESC=1 SHOW_FUNCS=1 ;; 156258400Sdteske f) SHOW_FUNCS=1 ;; 157258400Sdteske F) FUNC_PATTERN="$OPTARG" ;; 158258400Sdteske n) USE_COLOR= ;; 159258400Sdteske h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;; 160258400Sdteske esac 161258400Sdteskedone 162258400Sdteskeshift $(( $OPTIND - 1 )) 163258400Sdteske 164258400Sdteske# cd(1) to `share' dir so relative paths work for find and positional args 165263791Sdteskecd $BSDCFG_SHARE || f_die # Pedantic 166258400Sdteske 167258400Sdteske# 168258400Sdteske# If given an argument, operate on it specifically (implied `-f') and exit 169258400Sdteske# 170258400Sdteske[ $# -gt 0 ] && SHOW_FUNCS=1 171258400Sdteskefor include in "$@"; do 172258400Sdteske # See if they've just omitted the `*.subr' suffix 173258400Sdteske [ -f "$include.subr" -a ! -f "$include" ] && include="$include.subr" 174258400Sdteske if [ ! -f "$include" ]; then 175263791Sdteske printf "$msg_no_such_file_or_directory\n" "$0" "$include" 176263791Sdteske exit $FAILURE 177258400Sdteske elif [ ! -r "$include" ]; then 178263791Sdteske printf "$msg_permission_denied\n" "$0" "$include" 179263791Sdteske exit $FAILURE 180258400Sdteske fi 181258400Sdteske show_include "$include" || f_die 182258400Sdteskedone 183258400Sdteske 184258400Sdteske# Exit if we processed some include arguments 185258400Sdteske[ $# -gt 0 ] && exit $SUCCESS 186258400Sdteske 187258400Sdteske# 188258400Sdteske# Operate an all known include files 189258400Sdteske# NB: If we get this far, we had no include arguments 190258400Sdteske# 191258400Sdteskefind -s . -type f -and -iname '*.subr' | while read file; do 192258400Sdteske if [ "$SHOW_FUNCS" -o "$FUNC_PATTERN" ]; then 193258400Sdteske show_include "$file" 194258400Sdteske else 195258400Sdteske echo "${file#./}" 196258400Sdteske fi 197258400Sdteskedone 198258400Sdteske 199258400Sdteskeexit $SUCCESS 200258400Sdteske 201258400Sdteske################################################################################ 202258400Sdteske# END 203258400Sdteske################################################################################ 204