includes.sh revision 259054
1#!/bin/sh 2#- 3# Copyright (c) 2013 Devin Teske 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 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. 14# 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 25# SUCH DAMAGE. 26# 27# $FreeBSD: head/usr.sbin/bsdconfig/includes/includes 259054 2013-12-07 00:31:01Z dteske $ 28# 29############################################################ INCLUDES 30 31# Prevent common.subr from auto initializing debugging (this is not an inter- 32# active utility that requires debugging; also `-d' has been repurposed). 33# 34DEBUG_SELF_INITIALIZE=NO 35 36BSDCFG_SHARE="/usr/share/bsdconfig" 37. $BSDCFG_SHARE/common.subr || exit 1 38f_dprintf "%s: loading includes..." "$0" 39 40BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="includes" 41f_include_lang $BSDCFG_LIBE/include/messages.subr 42f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr 43 44f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm && 45 pgm="${ipgm:-$pgm}" 46 47############################################################ GLOBALS 48 49# 50# Options 51# 52USE_COLOR=1 53SHOW_DESC= 54SHOW_FUNCS= 55FUNC_PATTERN= 56 57############################################################ FUNCTIONS 58 59# show_functions $file 60# 61# Show the functions in the given include file. 62# 63show_include() 64{ 65 local file="${1#./}" 66 67 local pattern="${FUNC_PATTERN:-.*}" 68 output=$( awk \ 69 -v use_color=${USE_COLOR:-0} \ 70 -v re="$pattern" \ 71 -v show_desc=${SHOW_DESC:-0} ' 72 function asorti(src, dest) 73 { 74 # Copy src indices to dest and calculate array length 75 nitems = 0; for (i in src) dest[++nitems] = i 76 77 # Sort the array of indices (dest) using insertion sort method 78 for (i = 1; i <= nitems; k = i++) 79 { 80 idx = dest[i] 81 while ((k > 0) && (dest[k] > idx)) 82 { 83 dest[k+1] = dest[k] 84 k-- 85 } 86 dest[k+1] = idx 87 } 88 89 return nitems 90 } 91 /^$/,/^#/ { 92 if ($0 ~ /^# f_/) { 93 if (!match($2, re)) next 94 fn = $2 95 if (use_color) 96 syntax[fn] = sprintf("+%s[1;31m%s[0m%s\n", 97 substr($0, 2, RSTART), 98 substr($0, 2 + RSTART, RLENGTH), 99 substr($0, 2 + RSTART + RLENGTH)) 100 else 101 syntax[fn] = "+" substr($0, 2) "\n" 102 if (show_desc) 103 print_more = 1 104 else 105 print_more = substr($0, length($0)) == "\\" 106 } 107 if (show_desc && print_more) { 108 getline 109 while ($0 ~ /^#/) { 110 syntax[fn] = syntax[fn] " " substr($0, 2) "\n" 111 getline 112 } 113 print_more = 0 114 } else while (print_more) { 115 getline 116 syntax[fn] = syntax[fn] " " substr($0, 2) "\n" 117 print_more = substr($0, length($0)) == "\\" 118 } 119 } 120 END { 121 n = asorti(syntax, sorted_indices) 122 for (i = 1; i <= n; i++) 123 printf "%s", syntax[sorted_indices[i]] 124 }' "$file" ) 125 if [ "$output" ]; then 126 if [ ! "$SHOW_FUNCS" ]; then 127 echo "$file" 128 return $SUCCESS 129 fi 130 if [ "$FUNC_PATTERN" ]; then 131 printf ">>> $msg_functions_in_matching\n" \ 132 "$file" "$FUNC_PATTERN" 133 else 134 printf ">>> $msg_functions_in\n" "$file" 135 fi 136 echo "$output" 137 echo # blank line to simplify awk(1)-based reparse 138 fi 139} 140 141############################################################ MAIN 142 143# Incorporate rc-file if it exists 144[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc" 145 146# Are we in a terminal? 147[ -t 1 ] || USE_COLOR= 148 149# 150# Process command-line arguments 151# 152while getopts adfF:hn flag; do 153 case "$flag" in 154 a) USE_COLOR=1 ;; 155 d) SHOW_DESC=1 SHOW_FUNCS=1 ;; 156 f) SHOW_FUNCS=1 ;; 157 F) FUNC_PATTERN="$OPTARG" ;; 158 n) USE_COLOR= ;; 159 h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;; 160 esac 161done 162shift $(( $OPTIND - 1 )) 163 164# cd(1) to `share' dir so relative paths work for find and positional args 165cd $BSDCFG_SHARE || f_die # Pedantic 166 167# 168# If given an argument, operate on it specifically (implied `-f') and exit 169# 170[ $# -gt 0 ] && SHOW_FUNCS=1 171for include in "$@"; do 172 # See if they've just omitted the `*.subr' suffix 173 [ -f "$include.subr" -a ! -f "$include" ] && include="$include.subr" 174 if [ ! -f "$include" ]; then 175 printf "$msg_no_such_file_or_directory\n" "$0" "$include" 176 exit $FAILURE 177 elif [ ! -r "$include" ]; then 178 printf "$msg_permission_denied\n" "$0" "$include" 179 exit $FAILURE 180 fi 181 show_include "$include" || f_die 182done 183 184# Exit if we processed some include arguments 185[ $# -gt 0 ] && exit $SUCCESS 186 187# 188# Operate an all known include files 189# NB: If we get this far, we had no include arguments 190# 191find -s . -type f -and -iname '*.subr' | while read file; do 192 if [ "$SHOW_FUNCS" -o "$FUNC_PATTERN" ]; then 193 show_include "$file" 194 else 195 echo "${file#./}" 196 fi 197done 198 199exit $SUCCESS 200 201################################################################################ 202# END 203################################################################################ 204