libshell-0.0.3/000075500000000000000000000000001076625201600133135ustar00rootroot00000000000000libshell-0.0.3/COPYING000064400000000000000000000010261076625201600143450ustar00rootroot00000000000000The file shell-getopt Version 1.2 2008 by Alexey Gladkov , 2008 by Dmitry V. Levin Version 1.1 2004 by Raphael Version 1.0 1997 by Grigoriy Strokin (grg@philol.msu.ru), Public Domain The files shell-quote, shell-args are Copyright (C) 2007 Dmitry V. Levin , 2008 Alexey Gladkov The files shell-version, shell-config, shell-error are Copyright (C) 2008 Alexey Gladkov libshell-0.0.3/SYMS000064400000000000000000000010231076625201600140250ustar00rootroot00000000000000shell-args opt_check_dir shell-args opt_check_number shell-args opt_check_read shell-args parse_common_option shell-args print_version shell-args show_help shell-args show_usage shell-config shell_config_get shell-config shell_config_set shell-config shell_config_del shell-config shell_config_comment shell-error fatal shell-error message shell-error verbose shell-getopt getopt shell-getopt getopts shell-getopt getoptex shell-getopt getsubopt shell-quote quote_sed_regexp shell-quote quote_shell shell-quote string_quote_remove libshell-0.0.3/contrib/000075500000000000000000000000001076625201600147535ustar00rootroot00000000000000libshell-0.0.3/contrib/getoptx_1.0.bash000064400000000000000000000215741076625201600176730ustar00rootroot00000000000000#From: "Grigoriy Strokin" #Newsgroups: comp.unix.shell #Subject: BASH: getopt function that parses long-named options #Date: Mon, 22 Dec 1997 20:35:18 +0300 #Hi, I have written a BASH function named getoptex, that is like bash builtin #"getopts", but does parse long-named options and optional arguments. It only #uses builtin bash commands, so it is very fast. In order to use it in your #bash scripts, include a command ". getopt.sh" ( getopt.sh) to the file #containing your script, and that will define functions getopt, getoptex, and #optlistex (the file getopt.sh with its detailed description is listed #below). #*** file getopt.sh *** #! /bin/bash # # getopt.sh: # functions like getopts but do long-named options parsing # and support optional arguments # # Version 1.0 1997 by Grigoriy Strokin (grg@philol.msu.ru), Public Domain # Date created: December 21, 1997 # Date modified: December 21, 1997 # # IMPORTANT FEATURES # # 1) Parses both short and long-named options # 2) Supports optional arguments # 3) Only uses bash builtins, thus no calls to external # utilities such as expr or sed is done. Therefore, # parsing speed is high enough # # # DESCRIPTION # # FUNCTION getopt # Usage: getopt OPTLIST {"$@"|ALTERNATIVE_PARAMETERS} # # like getopts, but parse options with both required and optional arguments, # Options with optional arguments must have "." instead of ":" after them. # Furthemore, a variable name to place option name cannot be specified # and is always placed in OPTOPT variable # # This function is provided for compatibility with getopts() # OPTLIST style, and it actually calls getoptex (see bellow) # # NOTE that a list of parameters is required and must be either "$@", # if processing command line arguments, or some alternative parameters. # # FUNCTION getoptex # Usage: getoptex OPTION_LIST {"$@"|ALTERNATIVE_PARAMETERS} # # like getopts, but parse long-named options. # # Both getopt and getoptex return 0 if an option has been parsed, # and 1 if all options are already parsed or an error occured # # Both getopt and getoptex set or test the following variables: # # OPTERR -- tested for whether error messages must be given for invalid options # # OPTOPT -- set to the name of an option parsed, # or to "?" if no more options or error # OPTARG -- set to the option argument, if any; # unset if ther is no argument; # on error, set to the erroneous option name # # OPTIND -- Initialized to 1. # Then set to the number of the next parameter to be parsed # when getopt or getoptex will be called next time. # When all options are parsed, contains a number of # the first non-option argument. # # # OPTOFS -- If a parameter number $OPTIND containg an option parsed # does not contain any more options, OPTOFS is unset; # otherwise, OPTOFS is set to such a number of "?" signs # which is equal to the number of options parsed # # You might not set variables OPTIND and OPTOFS yourself # unless you want to parse a list of parameters more than once. # Otherwise, you whould unset OPTIND (or set it to 1) # and unset OPTOFS each time you want to parse a new parameters list # # Option list format is DIFFERENT from one for getopts or getopt. getopts-style # option list can be converted to getoptex-style using a function optlistex # (see bellow) # # DESCRIPTION of option list used with getoptex: # Option names are separated by whitespace. Options consiting of # more than one character are treated as long-named (--option) # # Special characters can appear at the and of option names specifying # whether an argument is required (default is ";"): # ";" (default) -- no argument # ":" -- required argument # "," -- optional argument # # For example, an option list "a b c help version f: file: separator." # defines the following options: # -a, -b, -c, --help, --version -- no argument # -f, --file -- argument required # --separator -- optional argument # # FUNCTION optlistex # Usage new_style_optlist=`optlistex OLD_STYLE_OPTLIST` # # Converts getopts-style option list in a format suitable for use with getoptex # Namely, it inserts spaces after each option name. # # # HOW TO USE # # In order o use in your bash scripts the functions described, # include a command ". getopt.sh" to the file containing the script, # which will define functions getopt, getoptex, and optlistex # # EXAMPLES # # See files 'getopt1' and 'getopt2' that contain sample scripts that use # getopt and getoptex functions respectively # # # Please send your comments to grg@philol.msu.ru function getoptex() { let $# || return 1 local optlist="${1#;}" let OPTIND || OPTIND=1 [ $OPTIND -lt $# ] || return 1 shift $OPTIND if [ "$1" != "-" ] && [ "$1" != "${1#-}" ] then OPTIND=$[OPTIND+1]; if [ "$1" != "--" ] then local o o="-${1#-$OPTOFS}" for opt in ${optlist#;} do OPTOPT="${opt%[;.:]}" unset OPTARG local opttype="${opt##*[^;:.]}" [ -z "$opttype" ] && opttype=";" if [ ${#OPTOPT} -gt 1 ] then # long-named option case $o in "--$OPTOPT") if [ "$opttype" != ":" ]; then return 0; fi OPTARG="$2" if [ -z "$OPTARG" ]; then # error: must have an agrument let OPTERR && echo "$0: error: $OPTOPT must have an argument" >&2 OPTARG="$OPTOPT"; OPTOPT="?" return 1; fi OPTIND=$[OPTIND+1] # skip option's argument return 0 ;; "--$OPTOPT="*) if [ "$opttype" = ";" ]; then # error: must not have arguments let OPTERR && echo "$0: error: $OPTOPT must not have arguments" >&2 OPTARG="$OPTOPT" OPTOPT="?" return 1 fi OPTARG=${o#"--$OPTOPT="} return 0 ;; esac else # short-named option case "$o" in "-$OPTOPT") unset OPTOFS [ "$opttype" != ":" ] && return 0 OPTARG="$2" if [ -z "$OPTARG" ] then echo "$0: error: -$OPTOPT must have an argument" >&2 OPTARG="$OPTOPT" OPTOPT="?" return 1 fi OPTIND=$[OPTIND+1] # skip option's argument return 0 ;; "-$OPTOPT"*) if [ $opttype = ";" ] then # an option with no argument is in a chain of options OPTOFS="$OPTOFS?" # move to the next option in the chain OPTIND=$[OPTIND-1] # the chain still has other options return 0 else unset OPTOFS OPTARG="${o#-$OPTOPT}" return 0 fi ;; esac fi done echo "$0: error: invalid option: $o" fi; fi OPTOPT="?" unset OPTARG return 1 } function optlistex { local l="$1" local m # mask local r # to store result while [ ${#m} -lt $[${#l}-1] ]; do m="$m?"; done # create a "???..." mask while [ -n "$l" ] do r="${r:+"$r "}${l%$m}" # append the first character of $l to $r l="${l#?}" # cut the first charecter from $l m="${m#?}" # cut one "?" sign from m if [ -n "${l%%[^:.;]*}" ] then # a special character (";", ".", or ":") was found r="$r${l%$m}" # append it to $r l="${l#?}" # cut the special character from l m="${m#?}" # cut one more "?" sign fi done echo $r } function getopt() { local optlist=`optlistex "$1"` shift getoptex "$optlist" "$@" return $? } #************************************** # cut here #************************************** #*** (end of getopt.sh) *** #*** file getopt1 *** #! /bin/bash # getopt1: # Sample script using the function getopt # # Type something like "getopt1 -ab -d 10 -e20 text1 text2" # on the command line to see how it works # # See getopt.sh for more information #. getopt.sh #echo Using getopt to parse arguments: #while getopt "abcd:e." "$@" #do # echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}" #done #shift $[OPTIND-1] #for arg in "$@" #do # echo "Non option argument <$arg>" #done # #************************************** # cut here #************************************** #*** (end of getopt1) *** # # #*** file getopt2 *** # #! /bin/bash # getopt2: # Sample script using the function getoptex # # Type something like "getopt2 -ab -d 10 -e20 --opt1 --opt4=100 text1 text2" # to see how it works # # See getopt.sh for more information . getopt.sh #echo Using getoptex to parse arguments: #while getoptex "a; b; c; d: e. opt1 opt2 opt3 opt4: opt5." "$@" #do # echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}" #done #shift $[OPTIND-1] #for arg in "$@" #do # echo "Non option argument <$arg>" #done # #************************************** # cut here #************************************** #*** (end of getopt2) *** libshell-0.0.3/contrib/getoptx_1.1.bash000064400000000000000000000171641076625201600176740ustar00rootroot00000000000000#! /bin/bash # # getopt.sh: # functions like getopts but do long-named options parsing # and support optional arguments # # Version 1.1 2004 by Raphael # Date modified: April 6, 2004 # Version 1.0 1997 by Grigoriy Strokin (grg@philol.msu.ru), Public Domain # Date created: December 21, 1997 # Date modified: December 21, 1997 # # UPDATE (by raphael) # # I found the routine wouldn't report option that need arguments right # if they where between or before other options. Rather it would take # the following option as the argument. The routine now checks if # the required argument starts with a dash '-' indicating that it is an # option. See source below. # # IMPORTANT FEATURES # # 1) Parses both short and long-named options # 2) Supports optional arguments # 3) Only uses bash builtins, thus no calls to external # utilities such as expr or sed is done. Therefore, # parsing speed is high enough # # # DESCRIPTION # # FUNCTION getopt # Usage: getopt OPTLIST {"$@"|ALTERNATIVE_PARAMETERS} # # like getopts, but parse options with both required and optional arguments, # Options with optional arguments must have "." instead of ":" after them. # Furthemore, a variable name to place option name cannot be specified # and is always placed in OPTOPT variable # # This function is provided for compatibility with getopts() # OPTLIST style, and it actually calls getoptex (see bellow) # # NOTE that a list of parameters is required and must be either "$@", # if processing command line arguments, or some alternative parameters. # # FUNCTION getoptex # Usage: getoptex OPTION_LIST {"$@"|ALTERNATIVE_PARAMETERS} # # like getopts, but parse long-named options. # # Both getopt and getoptex return 0 if an option has been parsed, # and 1 if all options are already parsed or an error occured # # Both getopt and getoptex set or test the following variables: # # OPTERR -- tested for whether error messages must be given for invalid options # # OPTOPT -- set to the name of an option parsed, # or to "?" if no more options or error # OPTARG -- set to the option argument, if any; # unset if ther is no argument; # on error, set to the erroneous option name # # OPTIND -- Initialized to 1. # Then set to the number of the next parameter to be parsed # when getopt or getoptex will be called next time. # When all options are parsed, contains a number of # the first non-option argument. # # # OPTOFS -- If a parameter number $OPTIND containg an option parsed # does not contain any more options, OPTOFS is unset; # otherwise, OPTOFS is set to such a number of "?" signs # which is equal to the number of options parsed # # You might not set variables OPTIND and OPTOFS yourself # unless you want to parse a list of parameters more than once. # Otherwise, you whould unset OPTIND (or set it to 1) # and unset OPTOFS each time you want to parse a new parameters list # # Option list format is DIFFERENT from one for getopts or getopt. getopts-style # option list can be converted to getoptex-style using a function optlistex # (see bellow) # # DESCRIPTION of option list used with getoptex: # Option names are separated by whitespace. Options consiting of # more than one character are treated as long-named (--option) # # Special characters can appear at the and of option names specifying # whether an argument is required (default is ";"): # ";" (default) -- no argument # ":" -- required argument # "," -- optional argument # # For example, an option list "a b c help version f: file: separator." # defines the following options: # -a, -b, -c, --help, --version -- no argument # -f, --file -- argument required # --separator -- optional argument # # FUNCTION optlistex # Usage new_style_optlist=`optlistex OLD_STYLE_OPTLIST` # # Converts getopts-style option list in a format suitable for use with getoptex # Namely, it inserts spaces after each option name. # # # HOW TO USE # # In order o use in your bash scripts the functions described, # include a command ". getopt.sh" to the file containing the script, # which will define functions getopt, getoptex, and optlistex # # EXAMPLES # # See files 'getopt1' and 'getopt2' that contain sample scripts that use # getopt and getoptex functions respectively # # # Please send your comments to grg@philol.msu.ru function getoptex() { let $# || return 1 local optlist="${1#;}" let OPTIND || OPTIND=1 [ $OPTIND -lt $# ] || return 1 shift $OPTIND if [ "$1" != "-" -a "$1" != "${1#-}" ] then OPTIND=$[OPTIND+1]; if [ "$1" != "--" ] then local o o="-${1#-$OPTOFS}" for opt in ${optlist#;} do OPTOPT="${opt%[;.:]}" unset OPTARG local opttype="${opt##*[^;:.]}" [ -z "$opttype" ] && opttype=";" if [ ${#OPTOPT} -gt 1 ] then # long-named option case $o in "--$OPTOPT") if [ "$opttype" != ":" ]; then return 0; fi OPTARG="$2" # Added test on following argument being an option identified by '-' this way # # the routine no longer takes options as an argument thus breaking error # # detection. 2004-04-04 by raphael at oninet dot pt # if [ -z "$OPTARG" -o "${OPTARG:0:1}" = "-" ] ; then # error: must have an agrument let OPTERR && echo "$0: error: $OPTOPT must have an argument" >&2 OPTARG="$OPTOPT"; OPTOPT="?" return 1; fi OPTIND=$[OPTIND+1] # skip option's argument return 0 ;; "--$OPTOPT="*) if [ "$opttype" = ";" ]; then # error: must not have arguments let OPTERR && echo "$0: error: $OPTOPT must not have arguments" >&2 OPTARG="$OPTOPT" OPTOPT="?" return 1 fi OPTARG=${o#"--$OPTOPT="} return 0 ;; esac else # short-named option case "$o" in "-$OPTOPT") unset OPTOFS [ "$opttype" != ":" ] && return 0 OPTARG="$2" # Added test on following argument being an option identified by '-' this way # # the routine no longer takes options as an argument thus breaking error # # detection. 2004-04-04 by raphael at oninet dot pt # if [ -z "$OPTARG" -o "${OPTARG:0:1}" = "-" ] ; then echo "$0: error: -$OPTOPT must have an argument" >&2 OPTARG="$OPTOPT" OPTOPT="?" return 1 fi OPTIND=$[OPTIND+1] # skip option's argument return 0 ;; "-$OPTOPT"*) if [ $opttype = ";" ] then # an option with no argument is in a chain of options OPTOFS="$OPTOFS?" # move to the next option in the chain OPTIND=$[OPTIND-1] # the chain still has other options return 0 else unset OPTOFS OPTARG="${o#-$OPTOPT}" return 0 fi ;; esac fi done echo "$0: error: invalid option: $o" fi; fi OPTOPT="?" unset OPTARG return 1 } function optlistex { local l="$1" local m # mask local r # to store result while [ ${#m} -lt $[${#l}-1] ]; do m="$m?"; done # create a "???..." mask while [ -n "$l" ] do r="${r:+"$r "}${l%$m}" # append the first character of $l to $r l="${l#?}" # cut the first charecter from $l m="${m#?}" # cut one "?" sign from m if [ -n "${l%%[^:.;]*}" ] then # a special character (";", ".", or ":") was found r="$r${l%$m}" # append it to $r l="${l#?}" # cut the special character from l m="${m#?}" # cut one more "?" sign fi done echo $r } function getopt() { local optlist=`optlistex "$1"` shift getoptex "$optlist" "$@" return $? } libshell-0.0.3/shell-args000064400000000000000000000032501076625201600152770ustar00rootroot00000000000000#!/bin/sh -efu if [ -z "${__included_shell_args-}" ]; then __included_shell_args=1 . shell-error # Checks that given option value is a readable file. # Arguments: $1 is option name, $2 is option value. # If $2 is a readable file, outputs canonicalized file name, # otherwise fails. opt_check_read() { local value [ -r "$2" ] && value="$(readlink -ev "$2")" || fatal "$1: $2: file not available." printf %s "$value" } # Checks that given option value is a traversable directory. # Arguments: $1 is option name, $2 is option value. # If $2 is a readable file, outputs canonicalized directory name, # otherwise fails. opt_check_dir() { local value [ -d "$2" -a -x "$2" ] && value="$(readlink -ev "$2")" || fatal "$1: $2: directory not available." printf %s "$value" } # Checks that given option value is a positive decimal number. # Arguments: $1 is option name, $2 is option value. # If $2 is a positive decimal number, outputs it, otherwise fails. opt_check_number() { [ -n "${2##0*}" -a -n "${2##*[!0-9]*}" ] && [ "$2" -gt 0 ] 2>/dev/null || fatal "$1: $2: invalid number." printf %s "$2" } show_usage() { [ -z "$*" ] || message "$*" echo "Try \`$PROG --help' for more information." >&2 exit 1 } show_help() { fatal "show_help(): Undefined function" } print_version() { fatal "print_version() Undefined function" } readonly getopt_common_opts="q,v,V,h" readonly getopt_common_longopts="quiet,verbose,version,help" parse_common_option() { case "$1" in -h|--help) show_help ;; -q|--quiet) quiet=-q ;; -v|--verbose) verbose=-v; quiet= ;; -V|--version) print_version "$PROG" ;; *) fatal "Unrecognized option: $1" ;; esac } fi #__included_shell_args libshell-0.0.3/shell-config000064400000000000000000000040521076625201600156110ustar00rootroot00000000000000#!/bin/sh -efu if [ -z "${__included_shell_config-}" ]; then __included_shell_config=1 . shell-error . shell-quote __shell_config_comment='#' shell_config_get() { [ "$#" -ge 2 -a "$#" -le 3 ] || fatal "Usage: shell_config_get file name [delim]" local file="$1" name="$2" delim="${3-=}" [ -s "$file" ] || return 0 name="$(quote_sed_regexp "$name")" sed -n -e "s/^[[:space:]]*$name$delim//p" -- "$file" } shell_config_set() { [ "$#" -ge 3 -a "$#" -le 5 ] || fatal "Usage: shell_config_set file name value [read-delim [write-delim]]" local file="$1" name="$2" value="$3" r_delim="${4-=}" w_delim="${5-=}" local n v nv= created= if [ ! -f "$file" ]; then if [ ! -w "${file%/*}" ]; then message "${file%/*}: not writable." return 1 fi touch -- "$file" || return 1 created=1 fi if [ -z "$created" ]; then n="$(quote_sed_regexp "$name")" if v="$(grep -m1 "^[[:space:]]*$n$r_delim" -- "$file" 2>/dev/null)"; then if [ "${v#*$name$r_delim}" != "$value" ]; then nv="$(quote_sed_regexp "$w_delim$value")" sed -i -e "s/^[[:space:]]*$n$r_delim.*/$n$nv/" -- "$file" fi return fi if [ -n "${__shell_config_comment-}" ] && v="$(grep -m1 "^[[:space:]]*${__shell_config_comment:-#}[[:space:]]*$n$r_delim" -- "$file" 2>/dev/null)"; then v="$(quote_sed_regexp "$v")" nv="$(quote_sed_regexp "$w_delim$value")" sed -i -e "s/^$v\$/$n$nv/" -- "$file" return fi fi printf '%s\n' "$name$w_delim$value" >> "$file" } shell_config_del() { [ "$#" -ge 2 -a "$#" -le 3 ] || fatal "Usage: shell_config_del file name [delim]" local file="$1" name="$2" delim="${3-=}" [ -s "$file" ] || return 0 name="$(quote_sed_regexp "$name")" sed -i -e "/^[[:space:]]*$name$delim/d" -- "$file" } shell_config_comment() { [ "$#" -ge 2 -a "$#" -le 3 ] || fatal "Usage: shell_config_comment file name [delim]" local file="$1" name="$2" delim="${3-=}" [ -s "$file" ] || return 0 name="$(quote_sed_regexp "$name")" sed -i -e "s/^[[:space:]]*$name$delim.*/${__shell_config_comment:-#}&/" -- "$file" } fi #__included_shell_config libshell-0.0.3/shell-error000064400000000000000000000005001076625201600154670ustar00rootroot00000000000000#!/bin/sh -efu if [ -z "${__included_shell_error-}" ]; then __included_shell_error=1 PROG="${PROG:-${0##*/}}" message() { printf %s\\n "$PROG: $*" >&2 } fatal() { message "$@" exit 1 } quiet="${quiet-}" verbose="${verbose-}" verbose() { [ -n "$verbose" ] || return 0 message "$@" } fi #__included_shell_error libshell-0.0.3/shell-getopt000064400000000000000000000200521076625201600156440ustar00rootroot00000000000000#! /bin/sh -efu if [ -z "${__included_shell_getopt-}" ]; then __included_shell_getopt=1 . shell-error . shell-quote GETOPT_ALLOW_UNKNOWN= GETOPT_ALLOW_ABBREV=1 GETOPT_ALLOW_ALTERNATIVE= OPTERR=1 OPTTYP= OPTUKN= OPTOPT= OPTARG= OPTIND=1 OPTOFS= #*** Example *** ### Usage: getopt2 -ab -d 10 -e20 --opt1 --opt4=100 text1 text2 ### # . shell-getopt # echo Using getoptex to parse arguments: # while getoptex "a; b; c; d: e. opt1 opt2 opt3 opt4: opt5." "$@"; do # echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}" # done # shift $(($OPTIND-1)) # set -- $@ ${OPTUKN-} # for arg in "$@"; do # echo "Non option argument <$arg>" # done getoptex() { [ "$#" -gt 0 ] || return 1 [ "${OPTIND-}" -gt 0 ] 2>/dev/null || OPTIND=1 [ "$OPTIND" -lt "$#" ] || return 1 getoptex_badarg() { [ -z "${OPTERR-}" ] || message "option requires an argument -- $OPTOPT" OPTARG="$OPTOPT" OPTOPT='?' } getoptex_argument() { [ "$OPTTYP" = ':' ] || return 0 OPTARG="$1" # Added test on following argument being an option identified by '-' this way # # the routine no longer takes options as an argument thus breaking error # # detection. 2004-04-04 by raphael at oninet dot pt # [ -n "$OPTARG" -a -n "${OPTARG%%-*}" ] || { getoptex_badarg; return 1; } OPTARG="${1#\#}" OPTIND=$(($OPTIND+1)) # skip option's argument } getoptex_option_long() { local arg="$1" v p for p in '--' ${GETOPT_ALLOW_ALTERNATIVE:+'-'}; do if [ -n "${GETOPT_ALLOW_ABBREV-}" ]; then arg="${1%%=*}" v="${1#*=}" o="$p$OPTOPT" if [ -z "${o##$arg*}" ]; then [ "$arg" != "$v" ] && arg="$o=$v" || arg="$o" unset o v fi fi case "$arg" in $p$OPTOPT=*) [ -n "$OPTTYP" ] || { getoptex_badarg; return 3; } OPTARG="${arg#$p$OPTOPT=}" return 1 ;; $p$OPTOPT) getoptex_argument "$2" || return 3 return 1 ;; esac done } getoptex_option_short() { local o="-${1#-${OPTOFS-}}" case "$o" in -$OPTOPT) OPTOFS= getoptex_argument "$2" || return 3 return 1 ;; -$OPTOPT*) if [ -z "$OPTTYP" ]; then # an option with no argument is in a chain of options OPTOFS="${OPTOFS-}?" # move to the next option in the chain OPTIND=$(($OPTIND-1)) # the chain still has other options else OPTOFS= OPTARG="${o#-$OPTOPT}" fi return 1 ;; esac } local optlist="${1#;}" value= shift $OPTIND [ "$#" -lt 2 ] || value="#$2" # test whether $1 is an option. if [ "$1" = '--' ]; then OPTTYP='--' OPTIND=$(($OPTIND+1)) elif [ "$1" != '-' -a "$1" != "${1#-}" ]; then OPTIND=$(($OPTIND+1)) local cmd argtype for opt in $optlist; do OPTOPT="${opt%[;.:]}" OPTARG= OPTTYP= [ "$OPTTYP" = ';' ] || OPTTYP="${opt#$OPTOPT}" cmd=long [ "${#OPTOPT}" -gt 1 ] || cmd=short getoptex_option_$cmd "$1" "$value" || return $(($?-1)) done OPTOPT='?' OPTARG= if [ -n "${GETOPT_ALLOW_UNKNOWN-}" ]; then OPTUKN="${OPTUKN:+$OPTUKN } \"$(quote_shell "$1")\"" return 0 fi message "unrecognized option \`$1'" return 2 fi OPTOPT='?' OPTARG= return 1 } #*** Example *** ### Usage: getopt1 -ab -d 10 -e20 text1 text2 ### # . shell-getopt # echo "Using getopt to parse arguments:" # while getopts "abcd:e." "$@"; do # echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}" # done # shift $(($OPTIND-1)) # set -- $@ ${OPTUKN-} # for arg in "$@"; do # echo "Non option argument <$arg>" # done getopts() { local l="$1" local m= # mask local r= # to store result while [ ${#m} -lt $((${#l}-1)) ]; do m="$m?" done # create a "???..." mask while [ -n "$l" ]; do r="${r:+"$r "}${l%$m}" # append the first character of $l to $r l="${l#?}" # cut the first charecter from $l m="${m#?}" # cut one "?" sign from m if [ "${l#[:.;]}" != "$l" ]; then # a special character (";", ".", or ":") was found r="$r${l%$m}" # append it to $r l="${l#?}" # cut the special character from l m="${m#?}" # cut one more "?" sign fi done shift getoptex "$r" "$@" || return $? } #*** Example *** # . shell-getopt # while getsubopt 'rw mode: path: dev:' 'rw,mode=755,path="/zzz xxx",dev=/dev/zzz'; do # echo "Option <$OPTOPT> ${OPTARG:+has an arg <$OPTARG>}" # done __getsubopt_arguments= getsubopt() { local l="$2" if [ -z "$__getsubopt_arguments" ]; then local ch m= while [ ${#m} -lt $((${#l}-1)) ]; do m="$m?" done __getsubopt_arguments='--' while [ -n "$l" ]; do ch="${l%$m}" l="${l#?}" m="${m#?}" case "$ch" in ,) ch=' --' ;; [\\\\\`\$\"\ ]) ch="\\$ch" ;; esac __getsubopt_arguments="$__getsubopt_arguments$ch" done fi local GETOPT_ALLOW_ABBREV= eval getoptex "\"$1\"" "$__getsubopt_arguments" || return $? } . shell-version __getopt_version() { cat <<-EOF $PROG version $libshell_version Written by Alexey Gladkov Copyright (C) 2008 Alexey Gladkov This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. EOF } __getopt_usage() { cat <<-EOF Try \`$PROG --help' for more information. EOF } __getopt_help() { cat <<-EOF Usage: $PROG optstring parameters or: $PROG [options] [--] optstring parameters or: $PROG [options] -o|--options optstring [options] [--] parameters Options ignored: -s, --shell=shell Set shell quoting conventions; -u, --unqote Do not quote the output; Options: -a, --alternative Allow long options starting with single -; -l, --longoptions=longopts Long options to be recognized; -n, --name=progname The name under which errors are reported; -o, --options=optstring Short options to be recognized; -q, --quiet Disable error reporting; -Q, --quiet-output No normal output; -T, --test Test for getopt version; -V, --version Output version information; -h, --help This small usage guide. Report bugs to http://bugzilla.altlinux.org/ EOF } getopt() { local PROG='getopt' OPTERR=1 prg= opts= lopts= quiet_output= alt= rc while getoptex 'a alternative h help l: longoptions: n: name: o: options: q quiet Q quiet-output s shell T test u unquoted V version' "$@" && rc=0 || rc=$?; do case "$rc" in 2) __getopt_usage; return 2 ;; 1) break ;; esac case "$OPTOPT" in a|alternative) alt=1 ;; n|name) prg="$OPTARG" ;; o|options) opts="$OPTARG " ;; l|longoptions) lopts="$OPTARG" ;; q|quiet) OPTERR= ;; Q|quiet-output) quiet_output=1 ;; T|test) return 4 ;; V|version) __getopt_version return 0 ;; h|help) __getopt_help return 2 ;; esac done shift $(($OPTIND-1)) set -- "$@" if [ -z "${opts##+*}" ]; then opts="${opts#+}" local GETOPT_ALLOW_UNKNOWN=1 elif [ -z "${opts##-*}" ]; then opts="${opts#-}" fi if [ -z "$opts" ]; then if [ "$#" -gt 1 -a "${1#-}" = "$1" -a "$1" != '--' ]; then opts="$1" shift else message "$PROG: missing optstring argument" __getopt_usage return 2 fi fi opts="${lopts:+$lopts,}$opts" while :; do case "$opts" in *,*) opts="${opts%%,*} ${opts#*,}" ;; *::*) opts="${opts%%::*}.${opts#*::}" ;; *) break ;; esac done local OPTIND=1 OPTOFS= GETOPT_ALLOW_ALTERNATIVE="${alt:+1}" local ret=0 out=' -- ' out_arg() { shift $(($OPTIND-1)) out="${out%% -- *} -- ${out#* -- } \"$(quote_shell "$1")\"" } PROG="$prg" while getoptex "$opts" "$@" && rc=0 || rc=$?; do case "$rc" in 1) [ $# -ge $OPTIND ] || break rc=0 out_arg "$@" OPTIND=$(($OPTIND+1)) [ "$OPTTYP" != '--' ] || break continue ;; 2) ret=1 ;; esac [ "$OPTOPT" != '?' ] || continue pfx='-' [ "${#OPTOPT}" -eq 1 ] || pfx='--' out="${out%% -- *} $pfx$OPTOPT ${OPTTYP:+'$OPTARG'} -- ${out#* -- }" done shift $(($OPTIND-1)) set -- "$@" while [ "$#" -gt 0 ]; do out="${out%% -- *} -- ${out#* -- } \"$(quote_shell "$1")\"" shift done [ -n "$quiet_output" ] || printf '%s\n' "$out${OPTUKN:+$OPTUKN}" return $ret } fi #__included_shell_getopt libshell-0.0.3/shell-quote000064400000000000000000000020711076625201600155000ustar00rootroot00000000000000#!/bin/sh -efu if [ -z "${__included_shell_quote-}" ]; then __included_shell_quote=1 # Quote given arguments for sed basic regular expression. # Usage example: sed "s/$(quote_sed_regexp "$var_pattern")/$(quote_sed_regexp "$var_replacement")/" quote_sed_regexp() { local out="$*" if [ -z "${out##*[\[\].*&^\$\\\\/]*}" ]; then out="$(printf %s "$out" |sed -e 's/[].*&^$[\/]/\\&/g')" || return 1 fi printf %s "$out" } # Quote argument for shell. # Usage example: eval "$var_name=\"$(quote_shell "$var_value")\"" quote_shell() { local out="$*" if [ -z "${out##*[\"\$\`\\\\]*}" ]; then out="$(printf %s "$out" |sed -e 's/["$`\\]/\\&/g')" || return 1 fi printf %s "$out" } # Remove quote symbol from string # Usage example: for i in "\"str1\"" "'str2'" "\"str3'"; do echo "$(string_quote_remove "$i")"; done # str1 # str2 # "str3' string_quote_remove() { local out="$1" if [ -z "${1##*'}${1%%'*}" ]; then out="${1#'}" out="${out%'}" elif [ -z "${1##*\"}${1%%\"*}" ]; then out="${1#\"}" out="${out%\"}" fi printf %s "$out" } fi #__included_shell_quote libshell-0.0.3/shell-regexp000077700000000000000000000000001076625201600200172shell-quoteustar00rootroot00000000000000libshell-0.0.3/shell-version000064400000000000000000000002141076625201600160250ustar00rootroot00000000000000#!/bin/sh -efu if [ -z "${__included_shell_version-}" ]; then __included_shell_version=1 libshell_version=2 fi #__included_shell_version libshell-0.0.3/tests/000075500000000000000000000000001076625201600144555ustar00rootroot00000000000000libshell-0.0.3/tests/fatal000064400000000000000000000017101076625201600154660ustar00rootroot00000000000000#!/bin/ash -efu PROG='TEST' appendTests fatal_test1 fatal_test1() { . ../shell-error expect="$PROG: message message;" result=`fatal "message message" 2>&1 |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests fatal_test2 fatal_test2() { . ../shell-error expect="$PROG: message;message;" result=`fatal "message message" 2>&1 |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests fatal_test3 fatal_test3() { . ../shell-error zzz=ZZZ expect="$PROG: message ZZZ message;" result=`fatal "message $zzz message" 2>&1 |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests fatal_test4 fatal_test4() { . ../shell-error expect="$PROG: message message;" result=`{ fatal "message message" 2>&1; echo "another message"; } |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } libshell-0.0.3/tests/getopt000064400000000000000000000135571076625201600157150ustar00rootroot00000000000000#!/bin/ash -efu normalize() { sed \ -e "s/\"/'/g" \ -e 's/[[:space:]]\+/ /g' \ -e 's/^[[:space:]]\+//' \ -e 's/[[:space:]]\+$//' } appendTests getopt_test1 getopt_test1() { LANG=C expect=`{ getopt -n TEST -o ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test2 getopt_test2() { LANG=C expect=`{ getopt -n TEST -o ' ' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o ' ' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test3 getopt_test3() { LANG=C expect=`{ getopt -n TEST -o '' -l ' ' -- ZZZ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o '' -l ' ' -- ZZZ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test4 getopt_test4() { LANG=C expect=`{ getopt -n TEST -o 'a,b,c' -- -abc -a -b -c ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a,b,c' -- -abc -a -b -c ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test5 getopt_test5() { LANG=C expect=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -abc --caa 'AAA' --cba='BBB' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -abc --caa 'AAA' --cba='BBB' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test6 getopt_test6() { LANG=C expect=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- --cb 'AAA' --c='BBB' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- --cb 'AAA' --c='BBB' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test7 getopt_test7() { LANG=C expect=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- --cbz 'AAA' --c='BBB' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- --cbz 'AAA' --c='BBB' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test8 getopt_test8() { LANG=C expect=`{ getopt -a -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -cb 'AAA' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -a -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -cb 'AAA' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test9 getopt_test9() { LANG=C expect=`{ getopt -a -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -c='AAA' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -a -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -c='AAA' ; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test10 getopt_test10() { LANG=C expect=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -c='AAA'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -c='AAA'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test11 getopt_test11() { LANG=C expect=`{ getopt -n TEST -o 'a,b,c' -l 'abcd:,cbce:' -- --abcx 'AAA'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a,b,c' -l 'abcd:,cbce:' -- --abcx 'AAA'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test12 getopt_test12() { LANG=C expect=`{ getopt -n TEST -o 'a:,b,c' -- -abc 'AAA'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a:,b,c' -- -abc 'AAA'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test13 getopt_test13() { LANG=C expect=`{ getopt -n TEST -o 'a::,b,c' -- -a -bc 'AAA'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a::,b,c' -- -a -bc 'AAA'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test14 getopt_test14() { LANG=C expect=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- 'BBB' 'XXX ZZZ'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- 'BBB' 'XXX ZZZ'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test15 getopt_test15() { LANG=C expect=`{ getopt -n TEST -o '+a,b,c' -l 'caa:,cba:' -- --caa='AAA' 'BBB' --zzz -x; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o '+a,b,c' -l 'caa:,cba:' -- --caa='AAA' 'BBB' --zzz -x; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests getopt_test16 getopt_test16() { LANG=C expect=`{ getopt -n TEST -o '-a,b,c' -l 'caa:,cba:' -- 'AAA' --caa='BBB' 'CCC'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` . ../shell-getopt result=`{ getopt -n TEST -o '-a,b,c' -l 'caa:,cba:' -- 'AAA' --caa='BBB' 'CCC'; echo rc=$?; } 2>&1 |normalize |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } libshell-0.0.3/tests/message000064400000000000000000000017601076625201600160300ustar00rootroot00000000000000#!/bin/ash -efu PROG='TEST' appendTests message_test1 message_test1() { . ../shell-error expect="$PROG: message message;" result=`message "message message" 2>&1 |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests message_test2 message_test2() { . ../shell-error expect="$PROG: message;message;" result=`message "message message" 2>&1 |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests message_test3 message_test3() { . ../shell-error zzz=ZZZ expect="$PROG: message ZZZ message;" result=`message "message $zzz message" 2>&1 |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests message_test4 message_test4() { . ../shell-error expect="$PROG: message message;another message;" result=`{ message "message message" 2>&1; echo "another message"; } |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } libshell-0.0.3/tests/opt_check_dir000064400000000000000000000013421076625201600171750ustar00rootroot00000000000000#!/bin/ash -efu appendTests opt_check_dir_test1 opt_check_dir_test1() { . ../shell-args [ -d "$WORKDIR/dir-644" ] || mkdir -m644 "$WORKDIR/dir-644" cd "$WORKDIR" expect="$(message "DIR: ./dir-644: directory not available." 2>&1)" result="$(opt_check_dir "DIR" "./dir-644" 2>&1)" ||: rmdir "dir-644" if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests opt_check_dir_test2 opt_check_dir_test2() { . ../shell-args [ -d "$WORKDIR/dir-755" ] || mkdir -m755 "$WORKDIR/dir-755" cd "$WORKDIR" expect="$(readlink -ev ./dir-755 2>&1)" ||: result="$(opt_check_dir "DIR" "./dir-755" 2>&1)" ||: rmdir "dir-755" if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } libshell-0.0.3/tests/opt_check_number000064400000000000000000000015041076625201600177070ustar00rootroot00000000000000#!/bin/ash -efu appendTests opt_check_number_test1 opt_check_number_test1() { . ../shell-args string="1234" expect="1234" result="$(opt_check_number "NUM" "$string" 2>&1)" ||: if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests opt_check_number_test2 opt_check_number_test2() { . ../shell-args string="0001" expect="$(message "NUM: $string: invalid number." 2>&1)" result="$(opt_check_number "NUM" "$string" 2>&1)" ||: if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests opt_check_number_test3 opt_check_number_test3() { . ../shell-args string="1 2" expect="$(message "NUM: $string: invalid number." 2>&1)" result="$(opt_check_number "NUM" "$string" 2>&1)" ||: if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } libshell-0.0.3/tests/opt_check_read000064400000000000000000000030641076625201600173350ustar00rootroot00000000000000#!/bin/ash -efu appendTests opt_check_read_test1 opt_check_read_test1() { . ../shell-args if [ ! -f "$WORKDIR/file-000" ]; then > "$WORKDIR/file-000" chmod 000 "$WORKDIR/file-000" fi cd "$WORKDIR" expect="$(message "FILE: ./file-000: file not available." 2>&1)" result="$(opt_check_read "FILE" "./file-000" 2>&1)" ||: if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests opt_check_read_test2 opt_check_read_test2() { . ../shell-args if [ ! -f "$WORKDIR/file-200" ]; then > "$WORKDIR/file-200" chmod 200 "$WORKDIR/file-200" fi cd "$WORKDIR" expect="$(message "FILE: ./file-200: file not available." 2>&1)" result="$(opt_check_read "FILE" "./file-200" 2>&1)" ||: if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests opt_check_read_test3 opt_check_read_test3() { . ../shell-args if [ ! -f "$WORKDIR/file-644" ]; then > "$WORKDIR/file-644" chmod 644 "$WORKDIR/file-644" fi cd "$WORKDIR" expect="$(readlink -ev ./file-644)" result="$(opt_check_read "FILE" "./file-644" 2>&1)" ||: if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests opt_check_read_test4 opt_check_read_test4() { . ../shell-args if [ ! -e "$WORKDIR/broken-symlink" ]; then ln -s "$WORKDIR/broken-symlink" "$WORKDIR/broken-symlink" fi cd "$WORKDIR" expect="$(message "FILE: ./broken-symlink: file not available." 2>&1)" result="$(opt_check_read "FILE" "./broken-symlink" 2>&1)" ||: if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } libshell-0.0.3/tests/quote_sed_regexp000064400000000000000000000023521076625201600177440ustar00rootroot00000000000000#!/bin/ash -efu appendTests quote_sed_regexp_test1 quote_sed_regexp_test1() { . ../shell-quote local string='.*' local regexp="$(quote_sed_regexp "$string")" local result="$(printf '%s\n' "$string passed" |sed -e "s/^$regexp/test/")" if [ "$result" != 'test passed' ]; then printf '%s' "$string" return 1 fi } appendTests quote_sed_regexp_test2 quote_sed_regexp_test2() { . ../shell-quote local string='[[:space:]]' local regexp="$(quote_sed_regexp "$string")" local result="$(printf '%s\n' "test $string" |sed -e "s/$regexp/passed/")" if [ "$result" != 'test passed' ]; then printf '%s' "$string" return 1 fi } appendTests quote_sed_regexp_test3 quote_sed_regexp_test3() { . ../shell-quote local string='t{1,3}' local regexp="$(quote_sed_regexp "$string")" local result="$(printf '%s\n' "test$string passed" |sed -e "s/$regexp//")" if [ "$result" != 'test passed' ]; then printf '%s' "$string" return 1 fi } appendTests quote_sed_regexp_test4 quote_sed_regexp_test4() { . ../shell-quote local string='&\1' local regexp="$(quote_sed_regexp "$string")" local result="$(printf '%s\n' "test not passed" |sed -e "s/\(not\)/$regexp/")" #" if [ "$result" != 'test &\1 passed' ]; then printf '%s' "$string" return 1 fi } libshell-0.0.3/tests/quote_shell000064400000000000000000000013171076625201600167260ustar00rootroot00000000000000#!/bin/ash -efu appendTests quote_shell_test1 quote_shell_test1() { . ../shell-quote local string='`true`' local result eval "result=\"$(quote_shell "$string")\"" if [ "$result" != "$string" ]; then printf '%s' "$string" return 1 fi } appendTests quote_shell_test2 quote_shell_test2() { . ../shell-quote local string='$(true)' local result eval "result=\"$(quote_shell "$string")\"" if [ "$result" != "$string" ]; then printf '%s' "$string" return 1 fi } appendTests quote_shell_test3 quote_shell_test3() { . ../shell-quote local string='\`true\`; echo zzz' local result eval "result=\"$(quote_shell "$string")\"" if [ "$result" != "$string" ]; then printf '%s' "$string" return 1 fi } libshell-0.0.3/tests/runtests000075500000000000000000000005271076625201600162760ustar00rootroot00000000000000#!/bin/ash -efu . shell-error . ./shell-unittest WORKDIR= beginTests() { WORKDIR="$(mktemp -d "$PROG.XXXXXXXXX")" } finishTests() { rm -rf -- "$WORKDIR" } for s in \ quote_sed_regexp quote_shell string_quote_remove \ opt_check_dir opt_check_number opt_check_read \ getopt \ fatal message verbose \ ; do . "./$s" done runUnitTests libshell-0.0.3/tests/shell-unittest000064400000000000000000000023051076625201600173640ustar00rootroot00000000000000#!/bin/ash -efu . shell-error beginTests() { :; } finishTests() { :; } startTest() { :; } endTest() { :; } run_or_exit() { "$@" || fatal "$1() fail rc=$?" } __shell_unit_tests= appendTests() { __shell_unit_tests="$__shell_unit_tests $*" } messageTest() { case "$3" in 0) printf '[done]' ;; 1) printf '[FAIL]' ;; 2) printf '[skip]' ;; esac printf ' (%s) %s\n' "$1" "$2" } showSummary() { if [ "$total" -eq 0 ]; then message "Nothing to do" return fi printf '\n' printf 'tests passed: %6d %3d%%\n' "$passed" "$((($passed*100)/$total))" printf 'tests failed: %6d %3d%%\n' "$failed" "$((($failed*100)/$total))" printf 'tests skipped: %6d %3d%%\n' "$skipped" "$((($skipped*100)/$total))" printf 'tests total: %6d\n\n' "$total" } runUnitTests() { run_or_exit beginTests set -- ${__shell_unit_tests-} local rc passed=0 failed=0 skipped=0 total="$#" while [ "$#" -gt 0 ]; do run_or_exit startTest rc=0 msg="$("$1")" || rc=$? case "$rc" in 0) passed=$(($passed+1)) ;; 1) failed=$(($failed+1)) ;; 2) skipped=$(($skipped+1)) ;; esac run_or_exit messageTest "$1" "$msg" "$rc" run_or_exit endTest shift done run_or_exit showSummary run_or_exit finishTests } libshell-0.0.3/tests/string_quote_remove000064400000000000000000000017641076625201600205100ustar00rootroot00000000000000#!/bin/sh appendTests string_quote_remove_test1 string_quote_remove_test1() { . ../shell-quote local string='"test passed"' local result="$(string_quote_remove "$string")" if [ "$result" != 'test passed' ]; then printf '%s' "$string" return 1 fi } appendTests string_quote_remove_test2 string_quote_remove_test2() { . ../shell-quote local string="'test passed'" local result="$(string_quote_remove "$string")" if [ "$result" != 'test passed' ]; then printf '%s' "$string" return 1 fi } appendTests string_quote_remove_test3 string_quote_remove_test3() { . ../shell-quote local string="'test passed\"" local result="$(string_quote_remove "$string")" if [ "$result" != "'test passed\"" ]; then printf '%s' "$string" return 1 fi } appendTests string_quote_remove_test4 string_quote_remove_test4() { . ../shell-quote local string="test ' passed" local result="$(string_quote_remove "$string")" if [ "$result" != "test ' passed" ]; then printf '%s' "$string" return 1 fi } libshell-0.0.3/tests/verbose000064400000000000000000000007431076625201600160510ustar00rootroot00000000000000#!/bin/ash -efu PROG='TEST' appendTests verbose_test1 verbose_test1() { . ../shell-error verbose=1 expect="$PROG: message message;" result=`verbose "message message" 2>&1 |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi } appendTests verbose_test2 verbose_test2() { . ../shell-error verbose= expect= result=`verbose "message message" 2>&1 |tr '\n' ';'` if [ "$result" != "$expect" ]; then printf '%s' "$result" return 1 fi }