2 # Copyright (C) 2006 Andre Noll
3 # Licensed under the LGPL, version 3. See COPYING and COPYING.LESSER.
5 if [[ "$(type -t _gsu_setup)" != "function" ]]; then
6 gsu_dir=${gsu_dir:-${BASH_SOURCE[0]%/*}}
7 . $gsu_dir/common || exit 1
13 gsu_short_msg "# Usage: $gsu_name command [options]"
16 # Return an extended regular expression to match against $0.
18 # When called without argument, the expression matches all lines which define a
21 # If an argument is given, the returned expression matches only the subcommand
22 # passed as $1. This is useful to tell if a string is a valid subcommand.
24 # Regardless of whether an argument is given, the returned expression contains
25 # exactly one parenthesized subexpression for matching the command name.
26 _gsu_get_command_regex()
28 local cmd="${1:-[-a-zA-Z_0-9]{1,\}}"
29 result="^com_($cmd)\(\)"
32 _gsu_available_commands()
36 _gsu_get_command_regex
39 printf "help\nman\nprefs\ncomplete\n"
41 # if line matches, isolate command name
44 # if there is a match, (print it and) start next cycle
50 } | sort | tr '\n' ' ')"
53 # Check number of arguments.
55 # Usage: gsu_check_arg_count <num_given> <num1> [<num2>]
57 # Check that <num_given> is between <num1> and <num2> inclusively.
58 # If only <num1> ist given, num2 is assumed to be infinity.
61 # 0 0 no argument allowed
62 # 1 1 exactly one argument required
63 # 0 2 at most two arguments admissible
64 # 2 at least two arguments required
67 ret=-$E_GSU_BAD_ARG_COUNT
68 if (($# == 2)); then # only num1 is given
69 result="at least $2 args required, $1 given"
75 result="need at least $2 args, $1 given"
77 result="need at most $3 args, $1 given"
82 # Wrapper for the bash getopts builtin.
84 # Aborts on programming errors such as missing or invalid option string. On
85 # success $result contains shell code that can be eval'ed. For each defined
86 # option x, the local variable o_x will be created when calling eval "$result".
87 # o_x contains true/false for options without argument and either the empty
88 # string or the given argument for options that take an argument.
93 # (($ret < 0)) && return
95 # [[ "$o_a" = 'true' ]] && echo 'The -a flag was given'
96 # [[ -n "$o_c" ]] && echo "The -c option was given with arg $o_c"
99 local i c tab=' ' cr='
102 gsu_check_arg_count $# 1 1
103 if (($ret < 0)); then
109 result="invalid optstring $1"
110 if [[ -z "$1" ]] || grep -q '::' <<< "$1" ; then
115 for ((i=0; i < ${#1}; i++)); do
121 result="invalid character $c in optstring"
126 result="local _gsu_getopts_opt"
127 for ((i=0; i < ${#1}; i++)); do
129 c2=${1:$(($i + 1)):1}
131 if [[ "$c2" = ":" ]]; then
139 while getopts $1 _gsu_getopts_opt \"\$@\"; do
140 case \"\$_gsu_getopts_opt\" in
142 for ((i=0; i < ${#1}; i++)); do
144 c2=${1:$(($i + 1)):1}
145 result+="$tab$tab$c1) o_$c1="
146 if [[ "$c2" = ":" ]]; then
147 result+="\"\$OPTARG\""
157 result=\"invalid option given\"
162 shift \$((\$OPTIND - 1))
167 _gsu_print_available_commands()
172 _gsu_available_commands
174 printf 'Available commands:\n'
178 if (($count % 4)); then
180 ((${#cmd} < 8)) && printf '\t'
188 # Print all options of the given optstring to stdout if the word in the current
189 # command line begins with a hyphen character.
190 gsu_complete_options()
192 local opts="$1" cword="$2" cur opt
197 cur="${words[$cword]}"
199 [[ ! "$cur" == -* ]] && return
202 for ((i=0; i < ${#opts}; i++)); do
204 [[ "$opt" == ":" ]] && continue
210 com_prefs_options='e'
213 Print the current preferences.
217 If -e is given, the config file is opened with the default editor.
218 Without options, the command prints out a list of all config variables,
219 together with their current value and the default value.
224 local i conf="${gsu_config_file:=${HOME:-}/.$gsu_name.rc}"
226 gsu_getopts "$com_prefs_options"
228 (($ret < 0)) && return
229 gsu_check_arg_count $# 0 0
230 (($ret < 0)) && return
232 if [[ "$o_e" == "true" ]]; then
236 (($? != 0)) && return
238 result="${EDITOR:-vi}"
240 (($? != 0)) && return
245 for ((i=0; i < ${#gsu_options[@]}; i++)); do
246 local name= option_type= default_value= required=
247 local description= help_text=
248 eval "${gsu_options[$i]}"
249 eval val='"${'${gsu_config_var_prefix}_$name:-'}"'
258 printf " $option_type: $description"
259 if [[ "$required" != "yes" && "$required" != "true" ]]; then
260 printf " [$default_value]"
263 [[ -n "$help_text" ]] && sed -e '/^[ ]*$/d; s/^[ ]*/# /g' <<< "$help_text"
265 [[ "$val" == "$default_value" ]] && printf " # default"
272 gsu_complete_options "$com_prefs_options" "$@"
282 local equal_signs="=================================================="
283 local minus_signs="--------------------------------------------------"
286 echo "$gsu_name (_${gsu_banner_txt}_) manual"
287 echo "${equal_signs:0:${#gsu_name} + ${#gsu_banner_txt} + 16}"
290 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
293 echo "$gsu_name usage"
294 echo "${minus_signs:0:${#gsu_name} + 6}"
297 echo "Each command has its own set of options as described below."
301 echo "Available commands:"
303 _gsu_available_commands
304 for com in $result; do
306 (($num < 4)) && num=4
307 echo "${minus_signs:0:$num}"
309 echo "${minus_signs:0:$num}"
319 Usage: help [command]
321 Without arguments, print the list of available commands. Otherwise,
322 print the help text for the given command."
325 Command line completion.
327 Usage: complete [<cword> <word>...]
329 When executed without argument the command writes bash code to
330 stdout. This code is suitable to be evaled from .bashrc to enable
333 If at least one argument is given, all possible completions are
334 written to stdout. This can be used from the completion function of
340 local a b ere tab=' '
342 _gsu_get_command_regex
346 gsu_short_msg "### $gsu_name -- $gsu_banner_txt ###"
349 printf "com_help()\n$_gsu_help_txt" | head -n 4; echo "--"
350 printf "com_man()\n$_gsu_man_txt" | head -n 4; echo "--"
351 printf "com_prefs()\n$_gsu_prefs_txt" | head -n 4; echo "--"
352 printf "com_complete()\n$_gsu_complete_txt" | head -n 4; echo "--"
354 } | grep -v -- '--' \
355 | sed -En "/$ere/"'!d
356 # remove everything but the command name
357 s/^com_(.*)\(\).*/\1/
359 # append tab after short commands (less than 8 chars)
360 s/^(.{1,7})$/\1'"$tab"'/g
362 # remove next line (should contain only ## anyway)
366 # append next line, removing leading ##
370 # replace newline by tab
373 # and print the sucker
376 echo "# Try $gsu_name help <command> for info on <command>."
380 if test "$1" = "help"; then
381 echo "$_gsu_help_txt"
385 if test "$1" = "man"; then
390 if test "$1" = "prefs"; then
391 echo "$_gsu_prefs_txt"
395 if test "$1" = "complete"; then
396 echo "$_gsu_complete_txt"
401 _gsu_get_command_regex "$1"
403 if ! grep -Eq "$ere" $0; then
404 _gsu_print_available_commands
406 ret=-$E_GSU_BAD_COMMAND
410 # only consider lines in the comment of the function
416 # if it did start with ##, jump to label p and print it
419 # otherwise, move on to next line
431 _gsu_available_commands
442 local cur="\${COMP_WORDS[\$COMP_CWORD]}";
445 candidates=(\$($0 complete "\$COMP_CWORD" "\${COMP_WORDS[@]}"));
446 COMPREPLY=(\$(compgen -W "\${candidates[*]}" -- "\$cur"));
453 gsu_is_a_number "$cword"
454 (($ret < 0)) && return
455 if (($cword <= 1)); then
456 _gsu_available_commands
464 ret=$GSU_SUCCESS # It's not an error if no completer was defined
465 [[ "$(type -t complete_$cmd)" != "function" ]] && return
466 complete_$cmd "$cword" "${words[@]}"
467 # ignore errors, they would only clutter the completion output
471 # Find out if the current word is a parameter for an option.
473 # $1: usual getopts option string.
474 # $2: The current word number.
475 # $3..: All words of the current command line.
477 # return: If yes, $result contains the letter of the option for which the
478 # current word is a parameter. Otherwise, $result is empty.
480 gsu_cword_is_option_parameter()
482 local opts="$1" cword="$2" prev i n
486 (($cword == 0)) && return
487 ((${#opts} < 2)) && return
491 prev="${words[$(($cword - 1))]}"
492 [[ ! "$prev" == -* ]] && return
495 for ((i=0; i <= $n; i++)); do
497 [[ "${opts:$(($i + 1)):1}" != ":" ]] && continue
499 [[ "$prev" != "-$opt" ]] && continue
506 # Get the word number on which the cursor is, not counting options.
508 # This is useful for completing commands whose possible completions depend
509 # on the word number, for example mount.
511 # $1: Getopt option string.
512 # $2: The current word number.
513 # $3..: All words of the current command line.
515 # return: If the current word is an option, or a parameter to an option,
516 # this function sets $result to -1. Otherwise, the number of the non-option
517 # is returned in $result.
519 gsu_get_unnamed_arg_num()
521 local opts="$1" cword="$2" prev cur
527 cur="${words[$cword]}"
528 prev="${words[$(($cword - 1))]}"
530 [[ "$cur" == -* ]] && return
531 [[ "$prev" == -* ]] && [[ "$opts" == *${prev#-}:* ]] && return
533 for ((i=1; i <= $cword; i++)); do
534 prev="${words[$(($i - 1))]}"
536 [[ "$cur" == -* ]] && continue
537 if [[ "$prev" == -* ]]; then
539 [[ "$opts" != *$opt:* ]] && let n++
547 # Entry point for all gsu-based scripts.
549 # The startup part of the application script should source this file to load
550 # the functions defined here, and then call gsu(). Functions starting with com_
551 # are automatically recognized as subcommands.
559 # gsu_dir=${gsu_dir:-/system/location/where/gsu/is/installed}
560 # . $gsu_dir/subcommand || exit 1
568 _gsu_print_available_commands
573 if [[ "$(type -t com_$arg)" == 'function' ]]; then
575 if (("$ret" < 0)); then
581 ret=-$E_GSU_BAD_COMMAND
584 _gsu_print_available_commands