home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / Other / Coherent / base / disk1.4.10.dd / usr / lib / shell_lib.sh
Encoding:
Text File  |  1994-05-03  |  5.0 KB  |  243 lines

  1. # (-lgl
  2. #    Coherent 386 release 4.2-Beta
  3. #    Copyright (c) 1982, 1994 by Mark Williams Company.
  4. #    All rights reserved. May not be copied without permission.
  5. #    For copying permission and licensing info, write licensing@mwc.com
  6. # -lgl)
  7.  
  8. # Revised: Tue May  3 16:17:38 1994 CDT
  9.  
  10. # A small library of POSIX-shell functions for the configuration shell
  11. # scripts. The scripts below use the features defined in POSIX.2 which
  12. # are also supported by the Coherent shell. It is intended that standard
  13. # Coherent shell scripts import this file using the '.' command.
  14.  
  15. # Returns true if the arguments are equal
  16.  
  17. is_equal () {
  18.     case $1 in
  19.     $2)    return 0 ;;
  20.     esac
  21.     return 1
  22. }
  23.  
  24.  
  25. # Returns true if the argument is null.
  26.  
  27. is_empty () {
  28.     return ${#1}
  29. }
  30.  
  31.  
  32. # Returns true if the argument matches an affirmative answer, 1 if the
  33. # argument matches a negative answer, and 2 otherwise;
  34.  
  35. is_yes () {
  36.     case $1 in
  37.     [Yy] | [Yy][Ee][Ss])    return 0 ;;
  38.     [Nn] | [Nn][Oo])    return 1 ;;
  39.     esac
  40.     return 2
  41. }
  42.  
  43.  
  44. # Standard paths for finding configuration files.
  45.  
  46. if is_empty $CONF_PATH; then
  47.     CONF_PATH=/etc/conf:/etc/conf/install_conf
  48. else
  49.     CONF_PATH=$CONF_PATH:/etc/conf:/etc/conf/install_conf
  50. fi
  51.  
  52.  
  53. # Identical functionality to the "basename" command, except that an optional
  54. # third and fourth arguments provide a prefix and suffix for a command to
  55. # execute.
  56.  
  57. basename () {
  58.     set -- "${1%$2}" "$3" "$4"    # strip optional (shortest) suffix
  59.     is_empty "$2$3" && set -- "$1" "echo "
  60.     eval "$2${1##*/}$3"        # strip longest prefix
  61. }
  62.  
  63.  
  64. # Return TRUE if $1 has $2 as a prefix
  65.  
  66. has_prefix () {
  67.     set -- "${1##$2*}"    # if $2 is a prefix, consumes all of $1
  68.     is_empty $1        # so $1 will be empty if $2 is a prefix
  69. }
  70.  
  71.  
  72. # Return TRUE if the indicated file exists. There are two good candidates for
  73. # doing this, test and globbing; while globbing might seem preferable in the
  74. # absence of a built-in 'test', note that globbing requires read permission
  75. # where using 'test' requires search.
  76.  
  77. file_exists () {
  78.     [ -f "$1" ]
  79. }
  80.  
  81.  
  82. # By default, write a parent pathname for the pathname given as the first
  83. # argument. The optional second and third arguments are the prefix and postfix
  84. # of a command run with the output pathname.
  85.  
  86. parent_of () {
  87.     is_empty "$2$3" && set -- "$1" "echo "
  88.     case "$1" in
  89.     */.. | ..)
  90.         eval "$2$1/.."        # Go up further
  91.         ;;
  92.  
  93.     .)    eval "$2.."
  94.         ;;
  95.  
  96.     */.)    parent_of ${1%/.} "$2" "$3"
  97.         ;;            # Strip redundant /.
  98.  
  99.     */*)    eval "$2${1%/*}"    # strip last component
  100.         ;;
  101.  
  102.     *)    eval "$2."        # current directory
  103.         ;;
  104.     esac
  105. }
  106.  
  107.  
  108. # Read input, with prompting, defaults, and shell escapes
  109. #    read_input prompt variable default validate
  110.  
  111. read_input () {
  112.     while : ; do        # until we break out...
  113.         # Append question mark to prompt unless it ends in a period.
  114.         case $1 in
  115.             *[.:!?])
  116.                 echo -n "$1 "
  117.                 ;;
  118.             *)
  119.                 echo -n "$1 ? "
  120.                 ;;
  121.         esac
  122.         is_empty "$3" || echo -n "[$3] "
  123.  
  124.         read $2
  125.  
  126.         case $(eval echo \$$2) in
  127.         !)    $SHELL            # subshell escape
  128.             echo            # write a newline
  129.             ;;
  130.  
  131.         !\?)    echo "!          Escapes to a shell prompt"
  132.             echo "!?         This help"
  133.             echo "!!         Takes default; query fails"
  134.             echo "!command   Executes command"
  135.             ;;
  136.  
  137.         !!*)    eval $2="\$3"        # restore default
  138.             return 1
  139.             ;;
  140.  
  141.         !*)    eval $(eval echo \${$2#!})
  142.             ;;            # escape a single command
  143.  
  144.         '')    eval $2="\$3"        # use default
  145.             break
  146.             ;;
  147.  
  148.         *)    is_empty "$4" || eval "$4 \$$2" || continue
  149.             break            # optionally validate
  150.             ;;
  151.         esac
  152.     done
  153.     return 0
  154. }
  155.  
  156.  
  157. # Validation function for the above, for yes/no answers.
  158.  
  159. require_yes_or_no () {
  160.     is_yes $1
  161.     case $? in
  162.     2)    echo "You must enter a yes or no answer"
  163.         return 1
  164.         ;;
  165.     *)    return 0
  166.         ;;
  167.     esac
  168. }
  169.  
  170.  
  171. # Take apart an input string consisting of colon-separated elements, such as
  172. # a $PATH string. The first argument is the path string, the second and third
  173. # form the prefix and postfix of a command that is run for every component.
  174.  
  175. split_path () {
  176.     while : ; do
  177.         case "$1" in
  178.         '' | :)    break
  179.             ;;
  180.  
  181.         :*)    set -- "${1#:}" "$2" "$3"
  182.             ;;
  183.  
  184.         *:*)    eval "$2${1%:"${1#*:}"}$3"
  185.             set -- "${1#*:}" "$2" "$3"
  186.             ;;
  187.  
  188.         *)    eval "$2$1$3"
  189.             break
  190.             ;;
  191.         esac
  192.     done
  193.     return 1
  194. }
  195.  
  196.  
  197. # Called with a filename and a list of paths to search, this function will
  198. # attempt to locate the file in the given directories.
  199.  
  200. find_file () {
  201.     split_path "$2" "find_file=" "/$1
  202.             file_exists \$find_file && {
  203.              eval \"$3\"\$find_file; return 0
  204.             }" || eval "$4 $1"
  205. }
  206.  
  207.  
  208. # Call with the $0 of the script as a parameter; by default, this command
  209. # writes the name of the directory containing the script to standard output.
  210. # If a second argument is given, it is taken as a command to pass the output
  211. # pathname to.
  212.  
  213. source_path () {
  214.     is_empty "$2$3" && set -- "$1" "echo "
  215.  
  216.     case $1 in
  217.     /*)    eval "$2${1%/*}$3"        # absolute pathname
  218.         ;;
  219.  
  220.     */*)    eval "$2$(pwd)/${1%/*}$3"    # relative pathname
  221.         ;;
  222.  
  223.     *)    eval "$2$(pwd)$3"        # current directory
  224.         ;;
  225.     esac
  226. }
  227.  
  228.  
  229. # Take the value of the arguments (which consist of an expression) and
  230. # return it, negated, as an exit status. Useful for turning shell arithmetic
  231. # expansions into test results.
  232.  
  233. val () {
  234.     return $((! ($*)))
  235. }
  236.  
  237.  
  238. # Check the argument to see if it is numeric or not.
  239.  
  240. is_numeric () {
  241.     (trap "exit 1" 0; echo $(($1*0)); trap 0) >/dev/null 2>&1
  242. }
  243.