2 # gsu -- the global subcommand utility
3 # (C) 2006-2009 Andre Noll
9 E_GSU_BAD_COMMAND invalid command
10 E_GSU_NOT_A_NUMBER not a number
11 E_GSU_SOURCE error in config file
12 E_GSU_CONFIG bad/missing config file option
13 E_GSU_BAD_CONFIG_VAR invalid config variable
14 E_GSU_NEED_VALUE value required but not given
15 E_GSU_BAD_BOOL bad value for boolian option
16 E_GSU_BAD_OPTION_TYPE invalid option type
17 E_GSU_BAD_ARG_COUNT invalid number of arguments
18 E_GSU_EDITOR failed to execute editor
19 E_NO_DEFAULT missing default value
35 export -f _gsu_init_errors
37 # check if $1 is a number
41 if test "$1" -eq "$1" &> /dev/null; then
44 ret=-$E_GSU_NOT_A_NUMBER
47 export -f gsu_is_a_number
49 # Check number of arguments.
51 # Usage: gsu_check_arg_count <num_given> <num1> [<num2>]
53 # Check that <num_given> is between <num1> and <num2> inclusively.
54 # If only <num1> ist given, num2 is assumed to be infinity.
57 # 0 0 no argument allowed
58 # 1 1 exactly one argument required
59 # 0 2 at most two arguments admissible
60 # 2 at least two arguments reqired
64 ret=-$E_GSU_BAD_ARG_COUNT
65 if [[ $# -eq 2 ]]; then # only num1 is given
66 result="at least $2 args required, $1 given"
67 [[ $1 -lt $2 ]] && return
72 result="need at least $2 args, $1 given"
73 [[ $1 -lt $2 ]] && return
74 result="need at most $3 args, $1 given"
75 [[ $1 -gt $3 ]] && return
78 export -f gsu_check_arg_count
84 export -f gsu_short_msg
88 gsu_short_msg "$_gsu_self: $1"
94 gsu_short_msg "$_gsu_self $(date): $1"
96 export -f gsu_date_msg
102 local txt="### $_gsu_self --"
103 if test -z "$gsu_banner_txt"; then
104 txt="$txt set \$gsu_banner_txt to customize this message"
106 txt="$txt $gsu_banner_txt"
108 gsu_short_msg "$txt ###"
110 export -f _gsu_banner_msg
114 local txt="$result" err
116 gsu_is_a_number "$ret"
117 if test $ret -lt 0; then
118 gsu_msg "unknown error ($ret:$txt)"
121 if test $result -ge 0; then
122 gsu_msg "unknown error ($result:$txt)"
126 if test -n "$txt"; then
127 txt="$txt: ${gsu_error_txt[$err]}"
129 txt="${gsu_error_txt[$err]}"
133 export -f gsu_err_msg
137 gsu_short_msg "# Usage: $_gsu_self command [options]"
141 _gsu_available_commands()
143 result="$( (printf "help\nman\nprefs\n"; grep "^com_[a-z_]\+()" $0) \
144 | sed -e 's/^com_//' -e 's/()//' \
149 export -f _gsu_available_commands
151 _gsu_print_available_commands()
154 gsu_short_msg "Available commands:"
155 for i in $gsu_cmds; do
157 count=$(($count + 1))
158 if test $(($count % 4)) -eq 0; then
162 if test ${#i} -lt 8; then
170 export -f _gsu_print_available_commands
172 export gsu_prefs_txt="
173 Print the current preferences.
177 If -e is given, the config file is opened with the default editor. Without
178 options, the command prints out a list of all cmt config variables, together
179 with their current value and the default value."
182 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}"
184 if [[ "$1" = "-e" ]]; then
186 result="${EDITOR:-vi}"
192 for ((i=0; i < ${#gsu_options[@]}; i++)); do
193 local name= option_type= default_value= required=
194 local description= help_text=
195 eval "${gsu_options[$i]}"
196 eval val='"$'${gsu_config_var_prefix}_$name'"'
205 printf " $option_type: $description"
206 if [[ "$required" != "yes" && "$required" != "true" ]]; then
207 printf " [$default_value]"
210 [[ -n "$help_text" ]] && sed -e '/^[ ]*$/d; s/^[ ]*/# /g' <<< "$help_text"
212 [[ "$val" == "$default_value" ]] && printf " # default"
225 local equal_signs="=================================================="
226 local minus_signs="--------------------------------------------------"
229 echo "$_gsu_self (_${gsu_banner_txt}_) manual"
230 echo "${equal_signs:0:${#_gsu_self} + ${#gsu_banner_txt} + 16}"
233 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
236 echo "$_gsu_self usage"
237 echo "${minus_signs:0:${#_gsu_self} + 6}"
240 echo "Each command has its own set of options as described below."
244 echo "Available commands:"
246 _gsu_available_commands
247 for com in $result; do
249 if test $num -lt 4; then
252 echo "${minus_signs:0:$num}"
254 echo "${minus_signs:0:$num}"
262 export gsu_help_txt="
265 Usage: help [command]
267 Without arguments, print the list of available commands. Otherwise,
268 print the help text for the given command."
273 if test -z "$1"; then
277 printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
278 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
279 printf "com_prefs()\n$gsu_prefs_txt" | head -n 4; echo "--"
280 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0
281 } | grep -v -- '--' \
282 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
283 -e 'H;$!d;x;s/\n//g;b' \
285 -e 'x;s/\n//g;${p;x;}' \
286 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
290 if test ${#a} -lt 8; then
296 echo "# Try $_gsu_self help <command> for info on <command>."
300 if test "$1" = "help"; then
305 if test "$1" = "man"; then
310 if test "$1" = "prefs"; then
311 echo "$gsu_prefs_txt"
316 if grep -q "^com_$1()" $0; then
317 sed -e "1,/^com_$1()$/d" -e '/^{/,$d' -e 's/^## *//' $0
320 _gsu_print_available_commands
322 ret=-$E_GSU_BAD_COMMAND
326 # internal gsu function that syntactically checks the gsu_options array
327 # for errors and parses the config file.
330 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}" val
332 for ((i=0; i < ${#gsu_options[@]}; i++)); do
333 eval "${gsu_options[$i]}"
334 eval val='"'\$$name'"'
335 eval orig_${gsu_config_var_prefix}_$name='"'${val}'"'
338 [[ -r "$conf" ]] && source "$conf"
340 for ((i=0; i < ${#gsu_options[@]}; i++)); do
341 local name= option_type= default_value= required=
342 local description= help_text=
345 eval "${gsu_options[$i]}"
348 # Check name. It must be non-empty and consist of [a-zA-Z_0-9]
349 # only. Moreover it must not start with [a-zA-Z].
351 ret=-$E_GSU_BAD_CONFIG_VAR
352 result="name: '$name'"
353 # bash's =~ works only for 3.2 and newer, so use grep
354 echo "$name" | grep '^[a-zA-Z][a-zA-Z_0123456789]*$' &> /dev/null;
355 [[ $? -ne 0 ]] && return
357 eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"'
358 if [[ -z "$orig_val" ]]; then
359 eval val='"'\$$name'"'
365 ret=-$E_GSU_NEED_VALUE
367 [[ -z "$val" ]] && return
373 result="required: $required, name: $name, val: $val"
377 eval ${gsu_config_var_prefix}_$name='"'${val:=$default_value}'"'
378 # Check option type. ATM, only num and string are supported
379 # Other types may be added without breaking compatibility
380 case "$option_type" in
384 gsu_is_a_number "$val"
385 [[ $ret -lt 0 ]] && return
388 ret=-$E_GSU_BAD_OPTION_TYPE
389 result="$name/$option_type"
395 export -f _gsu_check_options
401 gsu_is_a_number "${BASH_VERSINFO[0]}"
402 if [[ $ret -lt 0 ]]; then
403 gsu_msg "fatal: failed to determine bash version"
407 if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
408 gsu_msg "fatal: This script requires at least bash 4.0"
411 _gsu_self="$(basename $0)"
412 gsu_name="${gsu_name:=$_gsu_self}"
413 gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
416 if [[ "$ret" -lt 0 ]]; then
417 if [[ "$1" != "help" && "$1" != "man" ]]; then
422 _gsu_available_commands
424 if test $# -eq 0; then
426 _gsu_print_available_commands
431 # check internal commands
432 if [[ "$arg" = "help" || "$arg" = "man" || "$arg" = "prefs" ]]; then
434 if [[ "$ret" -lt 0 ]]; then
442 for i in $gsu_cmds; do
443 if test "$arg" = "$i"; then
445 if [[ "$ret" -lt 0 ]]; then
453 ret=-$E_GSU_BAD_COMMAND
456 _gsu_print_available_commands
461 # TODO: gsu_strerror: get error string