2 # gsu -- the global subcommand utility
3 # (C) 2006-2010 Andre Noll
9 E_GSU_BAD_COMMAND invalid command
10 E_GSU_NOT_A_NUMBER not a number
11 E_GSU_BAD_CONFIG_VAR invalid config variable
12 E_GSU_NEED_VALUE value required but not given
13 E_GSU_BAD_BOOL bad value for boolian option
14 E_GSU_BAD_OPTION_TYPE invalid option type
15 E_GSU_BAD_ARG_COUNT invalid number of arguments
16 E_GSU_EDITOR failed to execute editor
32 export -f _gsu_init_errors
34 # check if $1 is a number
38 if test "$1" -eq "$1" &> /dev/null; then
41 ret=-$E_GSU_NOT_A_NUMBER
44 export -f gsu_is_a_number
46 # Check number of arguments.
48 # Usage: gsu_check_arg_count <num_given> <num1> [<num2>]
50 # Check that <num_given> is between <num1> and <num2> inclusively.
51 # If only <num1> ist given, num2 is assumed to be infinity.
54 # 0 0 no argument allowed
55 # 1 1 exactly one argument required
56 # 0 2 at most two arguments admissible
57 # 2 at least two arguments reqired
61 ret=-$E_GSU_BAD_ARG_COUNT
62 if [[ $# -eq 2 ]]; then # only num1 is given
63 result="at least $2 args required, $1 given"
64 [[ $1 -lt $2 ]] && return
69 result="need at least $2 args, $1 given"
70 [[ $1 -lt $2 ]] && return
71 result="need at most $3 args, $1 given"
72 [[ $1 -gt $3 ]] && return
75 export -f gsu_check_arg_count
81 export -f gsu_short_msg
85 gsu_short_msg "$_gsu_self: $1"
91 gsu_short_msg "$_gsu_self $(date): $1"
93 export -f gsu_date_msg
99 local txt="### $_gsu_self --"
100 if test -z "$gsu_banner_txt"; then
101 txt="$txt set \$gsu_banner_txt to customize this message"
103 txt="$txt $gsu_banner_txt"
105 gsu_short_msg "$txt ###"
107 export -f _gsu_banner_msg
111 local txt="$result" err
113 gsu_is_a_number "$ret"
114 if test $ret -lt 0; then
115 gsu_msg "unknown error ($ret:$txt)"
118 if test $result -ge 0; then
119 gsu_msg "unknown error ($result:$txt)"
123 if test -n "$txt"; then
124 txt="$txt: ${gsu_error_txt[$err]}"
126 txt="${gsu_error_txt[$err]}"
130 export -f gsu_err_msg
134 gsu_short_msg "# Usage: $_gsu_self command [options]"
138 _gsu_available_commands()
140 result="$( (printf "help\nman\nprefs\n"; grep "^com_[a-z_]\+()" $0) \
141 | sed -e 's/^com_//' -e 's/()//' \
146 export -f _gsu_available_commands
148 _gsu_print_available_commands()
151 gsu_short_msg "Available commands:"
152 for i in $gsu_cmds; do
154 count=$(($count + 1))
155 if test $(($count % 4)) -eq 0; then
159 if test ${#i} -lt 8; then
167 export -f _gsu_print_available_commands
169 export gsu_prefs_txt="
170 Print the current preferences.
174 If -e is given, the config file is opened with the default editor. Without
175 options, the command prints out a list of all cmt config variables, together
176 with their current value and the default value."
179 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}"
181 if [[ "$1" = "-e" ]]; then
183 result="${EDITOR:-vi}"
189 for ((i=0; i < ${#gsu_options[@]}; i++)); do
190 local name= option_type= default_value= required=
191 local description= help_text=
192 eval "${gsu_options[$i]}"
193 eval val='"$'${gsu_config_var_prefix}_$name'"'
202 printf " $option_type: $description"
203 if [[ "$required" != "yes" && "$required" != "true" ]]; then
204 printf " [$default_value]"
207 [[ -n "$help_text" ]] && sed -e '/^[ ]*$/d; s/^[ ]*/# /g' <<< "$help_text"
209 [[ "$val" == "$default_value" ]] && printf " # default"
222 local equal_signs="=================================================="
223 local minus_signs="--------------------------------------------------"
226 echo "$_gsu_self (_${gsu_banner_txt}_) manual"
227 echo "${equal_signs:0:${#_gsu_self} + ${#gsu_banner_txt} + 16}"
230 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
233 echo "$_gsu_self usage"
234 echo "${minus_signs:0:${#_gsu_self} + 6}"
237 echo "Each command has its own set of options as described below."
241 echo "Available commands:"
243 _gsu_available_commands
244 for com in $result; do
246 if test $num -lt 4; then
249 echo "${minus_signs:0:$num}"
251 echo "${minus_signs:0:$num}"
259 export gsu_help_txt="
262 Usage: help [command]
264 Without arguments, print the list of available commands. Otherwise,
265 print the help text for the given command."
270 if test -z "$1"; then
274 printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
275 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
276 printf "com_prefs()\n$gsu_prefs_txt" | head -n 4; echo "--"
277 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0
278 } | grep -v -- '--' \
279 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
280 -e 'H;$!d;x;s/\n//g;b' \
282 -e 'x;s/\n//g;${p;x;}' \
283 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
287 if test ${#a} -lt 8; then
293 echo "# Try $_gsu_self help <command> for info on <command>."
297 if test "$1" = "help"; then
302 if test "$1" = "man"; then
307 if test "$1" = "prefs"; then
308 echo "$gsu_prefs_txt"
313 if grep -q "^com_$1()" $0; then
314 sed -e "1,/^com_$1()$/d" -e '/^{/,$d' -e 's/^## *//' $0
317 _gsu_print_available_commands
319 ret=-$E_GSU_BAD_COMMAND
323 # internal gsu function that syntactically checks the gsu_options array
324 # for errors and parses the config file.
327 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}" val
329 for ((i=0; i < ${#gsu_options[@]}; i++)); do
330 eval "${gsu_options[$i]}"
331 eval val='"'\$$name'"'
332 eval orig_${gsu_config_var_prefix}_$name='"'${val}'"'
335 [[ -r "$conf" ]] && source "$conf"
337 for ((i=0; i < ${#gsu_options[@]}; i++)); do
338 local name= option_type= default_value= required=
339 local description= help_text=
342 eval "${gsu_options[$i]}"
345 # Check name. It must be non-empty and consist of [a-zA-Z_0-9]
346 # only. Moreover it must not start with [a-zA-Z].
348 ret=-$E_GSU_BAD_CONFIG_VAR
349 result="name: '$name'"
350 # bash's =~ works only for 3.2 and newer, so use grep
351 echo "$name" | grep '^[a-zA-Z][a-zA-Z_0123456789]*$' &> /dev/null;
352 [[ $? -ne 0 ]] && return
354 eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"'
355 if [[ -z "$orig_val" ]]; then
356 eval val='"'\$$name'"'
362 ret=-$E_GSU_NEED_VALUE
364 [[ -z "$val" ]] && return
370 result="required: $required, name: $name, val: $val"
374 eval ${gsu_config_var_prefix}_$name='"'${val:=$default_value}'"'
375 # Check option type. ATM, only num and string are supported
376 # Other types may be added without breaking compatibility
377 case "$option_type" in
381 gsu_is_a_number "$val"
382 [[ $ret -lt 0 ]] && return
385 ret=-$E_GSU_BAD_OPTION_TYPE
386 result="$name/$option_type"
392 export -f _gsu_check_options
398 gsu_is_a_number "${BASH_VERSINFO[0]}"
399 if [[ $ret -lt 0 ]]; then
400 gsu_msg "fatal: failed to determine bash version"
404 if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
405 gsu_msg "fatal: This script requires at least bash 4.0"
408 _gsu_self="$(basename $0)"
409 gsu_name="${gsu_name:=$_gsu_self}"
410 gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
413 if [[ "$ret" -lt 0 ]]; then
414 if [[ "$1" != "help" && "$1" != "man" ]]; then
419 _gsu_available_commands
421 if test $# -eq 0; then
423 _gsu_print_available_commands
428 # check internal commands
429 if [[ "$arg" = "help" || "$arg" = "man" || "$arg" = "prefs" ]]; then
431 if [[ "$ret" -lt 0 ]]; then
439 for i in $gsu_cmds; do
440 if test "$arg" = "$i"; then
442 if [[ "$ret" -lt 0 ]]; then
450 ret=-$E_GSU_BAD_COMMAND
453 _gsu_print_available_commands
458 # TODO: gsu_strerror: get error string