Path: blob/main/usr.sbin/bsdconfig/includes/includes.sh
103352 views
#!/bin/sh1#-2# Copyright (c) 2013 Devin Teske3# All rights reserved.4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions7# are met:8# 1. Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# 2. Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24# SUCH DAMAGE.25#26#27############################################################ INCLUDES2829# Prevent common.subr from auto initializing debugging (this is not an inter-30# active utility that requires debugging; also `-d' has been repurposed).31#32DEBUG_SELF_INITIALIZE=NO3334BSDCFG_SHARE="/usr/share/bsdconfig"35. $BSDCFG_SHARE/common.subr || exit 136f_dprintf "%s: loading includes..." "$0"3738BSDCFG_LIBE="/usr/libexec/bsdconfig" APP_DIR="includes"39f_include_lang $BSDCFG_LIBE/include/messages.subr40f_include_lang $BSDCFG_LIBE/$APP_DIR/include/messages.subr4142f_index_menusel_keyword $BSDCFG_LIBE/$APP_DIR/INDEX "$pgm" ipgm &&43pgm="${ipgm:-$pgm}"4445############################################################ GLOBALS4647#48# Options49#50USE_COLOR=151SHOW_DESC=52SHOW_FUNCS=53FUNC_PATTERN=5455############################################################ FUNCTIONS5657# show_functions $file58#59# Show the functions in the given include file.60#61show_include()62{63local file="${1#./}"6465local pattern="${FUNC_PATTERN:-.*}"66output=$( awk \67-v use_color=${USE_COLOR:-0} \68-v re="$pattern" \69-v show_desc=${SHOW_DESC:-0} '70function _asorti(src, dest)71{72k = nitems = 0;7374# Copy src indices to dest and calculate array length75for (i in src) dest[++nitems] = i7677# Sort the array of indices (dest) using insertion sort method78for (i = 1; i <= nitems; k = i++)79{80idx = dest[i]81while ((k > 0) && (dest[k] > idx))82{83dest[k+1] = dest[k]84k--85}86dest[k+1] = idx87}8889return nitems90}91/^$/,/^#/ {92if ($0 ~ /^# f_/) {93if (!match($2, re)) next94fn = $295if (use_color)96syntax[fn] = sprintf("+%s[1;31m%s[0m%s\n",97substr($0, 2, RSTART),98substr($0, 2 + RSTART, RLENGTH),99substr($0, 2 + RSTART + RLENGTH))100else101syntax[fn] = "+" substr($0, 2) "\n"102if (show_desc)103print_more = 1104else105print_more = substr($0, length($0)) == "\\"106}107if (show_desc && print_more) {108getline109while ($0 ~ /^#/) {110syntax[fn] = syntax[fn] " " substr($0, 2) "\n"111getline112}113print_more = 0114} else while (print_more) {115getline116syntax[fn] = syntax[fn] " " substr($0, 2) "\n"117print_more = substr($0, length($0)) == "\\"118}119}120END {121n = _asorti(syntax, sorted_indices)122for (i = 1; i <= n; i++)123printf "%s", syntax[sorted_indices[i]]124}' "$file" )125if [ "$output" ]; then126if [ ! "$SHOW_FUNCS" ]; then127echo "$file"128return $SUCCESS129fi130if [ "$FUNC_PATTERN" ]; then131printf ">>> $msg_functions_in_matching\n" \132"$file" "$FUNC_PATTERN"133else134printf ">>> $msg_functions_in\n" "$file"135fi136echo "$output"137echo # blank line to simplify awk(1)-based reparse138fi139}140141############################################################ MAIN142143# Incorporate rc-file if it exists144[ -f "$HOME/.bsdconfigrc" ] && f_include "$HOME/.bsdconfigrc"145146# Are we in a terminal?147[ -t 1 ] || USE_COLOR=148149#150# Process command-line arguments151#152while getopts adfF:hn flag; do153case "$flag" in154a) USE_COLOR=1 ;;155d) SHOW_DESC=1 SHOW_FUNCS=1 ;;156f) SHOW_FUNCS=1 ;;157F) FUNC_PATTERN="$OPTARG" ;;158n) USE_COLOR= ;;159h|\?) f_usage $BSDCFG_LIBE/$APP_DIR/USAGE "PROGRAM_NAME" "$pgm" ;;160esac161done162shift $(( $OPTIND - 1 ))163164# cd(1) to `share' dir so relative paths work for find and positional args165cd $BSDCFG_SHARE || f_die # Pedantic166167#168# If given an argument, operate on it specifically (implied `-f') and exit169#170[ $# -gt 0 ] && SHOW_FUNCS=1171for include in "$@"; do172# See if they've just omitted the `*.subr' suffix173[ -f "$include.subr" -a ! -f "$include" ] && include="$include.subr"174if [ ! -f "$include" ]; then175printf "$msg_no_such_file_or_directory\n" "$0" "$include"176exit $FAILURE177elif [ ! -r "$include" ]; then178printf "$msg_permission_denied\n" "$0" "$include"179exit $FAILURE180fi181show_include "$include" || f_die182done183184# Exit if we processed some include arguments185[ $# -gt 0 ] && exit $SUCCESS186187#188# Operate an all known include files189# NB: If we get this far, we had no include arguments190#191find -s . -type f -and -iname '*.subr' | while read file; do192if [ "$SHOW_FUNCS" -o "$FUNC_PATTERN" ]; then193show_include "$file"194else195echo "${file#./}"196fi197done198199exit $SUCCESS200201################################################################################202# END203################################################################################204205206