3 ## GNU shtool -- The GNU Portable Shell Tool
4 ## Copyright (c) 1994-2000 Ralf S. Engelschall <rse@engelschall.com>
6 ## See http://www.gnu.org/software/shtool/ for more information.
7 ## See ftp://ftp.gnu.org/gnu/shtool/ for latest version.
9 ## Version 1.4.9 (16-Apr-2000)
10 ## Ingredients: 3/17 available modules
14 ## This program is free software; you can redistribute it and/or modify
15 ## it under the terms of the GNU General Public License as published by
16 ## the Free Software Foundation; either version 2 of the License, or
17 ## (at your option) any later version.
19 ## This program is distributed in the hope that it will be useful,
20 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ## General Public License for more details.
24 ## You should have received a copy of the GNU General Public License
25 ## along with this program; if not, write to the Free Software
26 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 ## USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
29 ## Notice: Given that you include this file verbatim into your own
30 ## source tree, you are justified in saying that it remains separate
31 ## from your package, and that this way you are simply just using GNU
32 ## shtool. So, in this situation, there is no requirement that your
33 ## package itself is licensed under the GNU General Public License in
34 ## order to take advantage of GNU shtool.
38 ## Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]
40 ## Available commands:
41 ## echo Print string with optional construct expansion
42 ## install Install a program, script or datafile
43 ## mkdir Make one or more directories
45 ## Not available commands (because module was not built-in):
46 ## mdate Pretty-print modification time of a file or dir
47 ## table Pretty-print a field-separated list as a table
48 ## prop Display progress with a running propeller
49 ## move Move files with simultaneous substitution
50 ## mkln Make link with calculation of relative paths
51 ## mkshadow Make a shadow tree through symbolic links
52 ## fixperm Fix file permissions inside a source tree
53 ## tarball Roll distribution tarballs
54 ## guessos Simple operating system guesser
55 ## arx Extended archive command
56 ## slo Separate linker options by library class
57 ## scpp Sharing C Pre-Processor
58 ## version Generate and maintain a version information file
59 ## path Deal with program paths
63 echo "$0:Error: invalid command line" 1>&2
64 echo "$0:Hint: run \`$0 -h' for usage" 1>&2
67 if [ ".$1" = ".-h" -o ".$1" = ".--help" ]; then
68 echo "This is GNU shtool, version 1.4.9 (16-Apr-2000)"
69 echo "Copyright (c) 1994-2000 Ralf S. Engelschall <rse@engelschall.com>"
70 echo "Report bugs to <bug-shtool@gnu.org>"
72 echo "Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]"
74 echo 'Available global <options>:'
75 echo ' -v, --version display shtool version information'
76 echo ' -h, --help display shtool usage help page (this one)'
77 echo ' -d, --debug display shell trace information'
79 echo 'Available <cmd-name> [<cmd-options>] [<cmd-args>]:'
80 echo ' echo [-n] [-e] [<str> ...]'
81 echo ' install [-v] [-t] [-c] [-C] [-s] [-m<mode>] [-o<owner>] [-g<group>]'
82 echo ' [-e<ext>] <file> <path>'
83 echo ' mkdir [-t] [-f] [-p] [-m<mode>] <dir> [<dir> ...]'
85 echo 'Not available <cmd-name> (because module was not built-in):'
86 echo ' mdate [-n] [-z] [-s] [-d] [-f<str>] [-o<spec>] <path>'
87 echo ' table [-F<sep>] [-w<width>] [-c<cols>] [-s<strip>] <str><sep><str>...'
88 echo ' prop [-p<str>]'
89 echo ' move [-v] [-t] [-e] [-p] <src-file> <dst-file>'
90 echo ' mkln [-t] [-f] [-s] <src-path> [<src-path> ...] <dst-path>'
91 echo ' mkshadow [-v] [-t] [-a] <src-dir> <dst-dir>'
92 echo ' fixperm [-v] [-t] <path> [<path> ...]'
93 echo ' tarball [-t] [-v] [-o <tarball>] [-c <prog>] [-d <dir>] [-u'
94 echo ' <user>] [-g <group>] [-e <pattern>] <path> [<path> ...]'
96 echo ' arx [-t] [-C<cmd>] <op> <archive> [<file> ...]'
97 echo ' slo [-p<str>] -- -L<dir> -l<lib> [-L<dir> -l<lib> ...]'
98 echo ' scpp [-v] [-p] [-f<filter>] [-o<ofile>] [-t<tfile>] [-M<mark>]'
99 echo ' [-D<dname>] [-C<cname>] <file> [<file> ...]'
100 echo ' version [-l<lang>] [-n<name>] [-p<prefix>] [-s<version>] [-i<knob>]'
101 echo ' [-d<type>] <file>'
102 echo ' path [-s] [-r] [-d] [-b] [-m] [-p<path>] <str> [<str> ...]'
106 if [ ".$1" = ".-v" -o ".$1" = ."--version" ]; then
107 echo "GNU shtool 1.4.9 (16-Apr-2000)"
110 if [ ".$1" = ".-d" -o ".$1" = ."--debug" ]; then
114 name=`echo "$0" | sed -e 's;.*/\([^/]*\)$;\1;' -e 's;-sh$;;' -e 's;\.sh$;;'`
117 # implicit tool command selection
121 # explicit tool command selection
131 ## DISPATCH INTO SCRIPT PROLOG
137 str_usage="[-n] [-e] [<str> ...]"
145 str_usage="[-v] [-t] [-c] [-C] [-s] [-m<mode>] [-o<owner>] [-g<group>] [-e<ext>] <file> <path>"
147 opt_spec="v.t.c.C.s.m:o:g:e:"
160 str_usage="[-t] [-f] [-p] [-m<mode>] <dir> [<dir> ...]"
169 echo "$0:Error: unknown option \`$tool'" 2>&1
170 echo "$0:Hint: run \`$0 -h' for usage" 2>&1
174 echo "$0:Error: unknown command \`$tool'" 2>&1
175 echo "$0:Hint: run \`$0 -h' for usage" 2>&1
181 ## COMMON UTILITY CODE
184 # determine name of tool
185 if [ ".$tool" != . ]; then
186 # used inside shtool script
188 toolcmdhelp="shtool $tool"
189 msgprefix="shtool:$tool"
191 # used as standalone script
194 msgprefix="$str_tool"
197 # parse argument specification string
198 eval `echo $arg_spec |\
199 sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS=\1; arg_MODE=\2/'`
201 # parse option specification string
202 eval `echo h.$opt_spec |\
203 sed -e 's/\([a-zA-Z0-9]\)\([.:+]\)/opt_MODE_\1=\2;/g'`
205 # interate over argument line
207 while [ $# -gt 0 ]; do
208 # special option stops processing
209 if [ ".$1" = ".--" ]; then
214 # determine option and argument
216 if [ ".$opt_PREV" != . ]; then
217 # merge previous seen option with argument
223 # split argument into option and argument
227 sed -e 's/^x-\([a-zA-Z0-9]\)/opt_OPT="\1";/' \
228 -e 's/";\(.*\)$/"; opt_ARG="\1"/'`
231 opt_OPT=`echo "x$1" | cut -c3-`
243 # determine whether option needs an argument
244 eval "opt_MODE=\$opt_MODE_${opt_OPT}"
245 if [ ".$opt_ARG" = . -a ".$opt_ARG_OK" != .yes ]; then
246 if [ ".$opt_MODE" = ".:" -o ".$opt_MODE" = ".+" ]; then
256 eval "opt_${opt_OPT}=yes"
259 # option with argument (multiple occurances override)
260 eval "opt_${opt_OPT}=\"\$opt_ARG\""
263 # option with argument (multiple occurances append)
264 eval "opt_${opt_OPT}=\"\$opt_${opt_OPT} \$opt_ARG\""
267 echo "$msgprefix:Error: unknown option: \`-$opt_OPT'" 1>&2
268 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
273 if [ ".$opt_PREV" != . ]; then
274 echo "$msgprefix:Error: missing argument to option \`-$opt_PREV'" 1>&2
275 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
279 # process help option
280 if [ ".$opt_h" = .yes ]; then
281 echo "Usage: $toolcmdhelp $str_usage"
285 # complain about incorrect number of arguments
288 if [ $# -ne $arg_NUMS ]; then
289 echo "$msgprefix:Error: invalid number of arguments (exactly $arg_NUMS expected)" 1>&2
290 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2
295 if [ $# -lt $arg_NUMS ]; then
296 echo "$msgprefix:Error: invalid number of arguments (at least $arg_NUMS expected)" 1>&2
297 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2
303 # establish a temporary file on request
304 if [ ".$gen_tmpfile" = .yes ]; then
305 if [ ".$TMPDIR" != . ]; then
307 elif [ ".$TEMPDIR" != . ]; then
312 tmpfile="$tmpdir/.shtool.$$"
313 rm -f $tmpfile >/dev/null 2>&1
318 ## DISPATCH INTO SCRIPT BODY
325 ## echo -- Print string with optional construct expansion
326 ## Copyright (c) 1998-2000 Ralf S. Engelschall <rse@engelschall.com>
327 ## Originally written for WML as buildinfo
332 # check for broken escape sequence expansion
334 bytes=`echo '\1' | wc -c | awk '{ printf("%s", $1); }'`
335 if [ ".$bytes" != .3 ]; then
336 bytes=`echo -E '\1' | wc -c | awk '{ printf("%s", $1); }'`
337 if [ ".$bytes" = .3 ]; then
342 # check for existing -n option (to suppress newline)
344 bytes=`echo -n 123 2>/dev/null | wc -c | awk '{ printf("%s", $1); }'`
345 if [ ".$bytes" = .3 ]; then
349 # determine terminal bold sequence
352 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%[Bb]'`" != . ]; then
354 # for the most important terminal types we directly know the sequences
355 xterm|xterm*|vt220|vt220*)
356 term_bold=`awk 'BEGIN { printf("%c%c%c%c", 27, 91, 49, 109); }' </dev/null 2>/dev/null`
357 term_norm=`awk 'BEGIN { printf("%c%c%c", 27, 91, 109); }' </dev/null 2>/dev/null`
360 term_bold=`awk 'BEGIN { printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }' </dev/null 2>/dev/null`
361 term_norm=`awk 'BEGIN { printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }' </dev/null 2>/dev/null`
363 # for all others, we try to use a possibly existing `tput' or `tcout' utility
365 paths=`echo $PATH | sed -e 's/:/ /g'`
366 for tool in tput tcout; do
367 for dir in $paths; do
368 if [ -r "$dir/$tool" ]; then
369 for seq in bold md smso; do # 'smso' is last
370 bold="`$dir/$tool $seq 2>/dev/null`"
371 if [ ".$bold" != . ]; then
376 if [ ".$term_bold" != . ]; then
377 for seq in sgr0 me rmso reset; do # 'reset' is last
378 norm="`$dir/$tool $seq 2>/dev/null`"
379 if [ ".$norm" != . ]; then
388 if [ ".$term_bold" != . -a ".$term_norm" != . ]; then
394 if [ ".$term_bold" = . -o ".$term_norm" = . ]; then
395 echo "$msgprefix:Warning: unable to determine terminal sequence for bold mode" 1>&2
399 # determine user name
401 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%[uU]'`" != . ]; then
403 if [ ".$username" = . ]; then
405 if [ ".$username" = . ]; then
406 username="`(whoami) 2>/dev/null |\
407 awk '{ printf("%s", $1); }'`"
408 if [ ".$username" = . ]; then
409 username="`(who am i) 2>/dev/null |\
410 awk '{ printf("%s", $1); }'`"
411 if [ ".$username" = . ]; then
421 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%U'`" != . ]; then
422 userid="`(id -u) 2>/dev/null`"
423 if [ ".$userid" = . ]; then
424 str="`(id) 2>/dev/null`"
425 if [ ".`echo $str | grep '^uid[ ]*=[ ]*[0-9]*('`" != . ]; then
426 userid=`echo $str | sed -e 's/^uid[ ]*=[ ]*//' -e 's/(.*//'`
428 if [ ".$userid" = . ]; then
429 userid=`egrep "^${username}:" /etc/passwd 2>/dev/null | \
430 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
431 if [ ".$userid" = . ]; then
432 userid=`(ypcat passwd) 2>/dev/null |
433 egrep "^${username}:" | \
434 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
435 if [ ".$userid" = . ]; then
443 # determine host name
445 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%h'`" != . ]; then
446 hostname="`(uname -n) 2>/dev/null |\
447 awk '{ printf("%s", $1); }'`"
448 if [ ".$hostname" = . ]; then
449 hostname="`(hostname) 2>/dev/null |\
450 awk '{ printf("%s", $1); }'`"
451 if [ ".$hostname" = . ]; then
457 domainname=".`echo $hostname | cut -d. -f2-`"
458 hostname="`echo $hostname | cut -d. -f1`"
463 # determine domain name
465 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%d'`" != . ]; then
466 if [ ".$domainname" = . ]; then
467 if [ -f /etc/resolv.conf ]; then
468 domainname="`egrep '^[ ]*domain' /etc/resolv.conf | head -1 |\
469 sed -e 's/.*domain//' \
470 -e 's/^[ ]*//' -e 's/^ *//' -e 's/^ *//' \
471 -e 's/^\.//' -e 's/^/./' |\
472 awk '{ printf("%s", $1); }'`"
473 if [ ".$domainname" = . ]; then
474 domainname="`egrep '^[ ]*search' /etc/resolv.conf | head -1 |\
475 sed -e 's/.*search//' \
476 -e 's/^[ ]*//' -e 's/^ *//' -e 's/^ *//' \
477 -e 's/ .*//' -e 's/ .*//' \
478 -e 's/^\.//' -e 's/^/./' |\
479 awk '{ printf("%s", $1); }'`"
485 # determine current time
490 if [ ".$opt_e" = .yes -a ".`echo $text | egrep '%[DMYm]'`" != . ]; then
491 time_day=`date '+%d'`
492 time_month=`date '+%m'`
493 time_year=`date '+%Y' 2>/dev/null`
494 if [ ".$time_year" = . ]; then
495 time_year=`date '+%y'`
497 [5-9][0-9]) time_year="19$time_year" ;;
498 [0-4][0-9]) time_year="20$time_year" ;;
502 1|01) time_monthname='Jan' ;;
503 2|02) time_monthname='Feb' ;;
504 3|03) time_monthname='Mar' ;;
505 4|04) time_monthname='Apr' ;;
506 5|05) time_monthname='May' ;;
507 6|06) time_monthname='Jun' ;;
508 7|07) time_monthname='Jul' ;;
509 8|08) time_monthname='Aug' ;;
510 9|09) time_monthname='Sep' ;;
511 10) time_monthname='Oct' ;;
512 11) time_monthname='Nov' ;;
513 12) time_monthname='Dec' ;;
517 # expand special ``%x'' constructs
518 if [ ".$opt_e" = .yes ]; then
519 text=`echo $seo "$text" |\
520 sed -e "s/%B/${term_bold}/g" \
521 -e "s/%b/${term_norm}/g" \
522 -e "s/%u/${username}/g" \
523 -e "s/%U/${userid}/g" \
524 -e "s/%h/${hostname}/g" \
525 -e "s/%d/${domainname}/g" \
526 -e "s/%D/${time_day}/g" \
527 -e "s/%M/${time_month}/g" \
528 -e "s/%Y/${time_year}/g" \
529 -e "s/%m/${time_monthname}/g" 2>/dev/null`
533 if [ .$opt_n = .no ]; then
536 # the harder part: echo -n is best, because
537 # awk may complain about some \xx sequences.
538 if [ ".$minusn" != . ]; then
539 echo $seo $minusn "$text"
541 echo dummy | awk '{ printf("%s", TEXT); }' TEXT="$text"
548 ## install -- Install a program, script or datafile
549 ## Copyright (c) 1997-2000 Ralf S. Engelschall <rse@engelschall.com>
550 ## Originally written for shtool
556 # If destination is a directory, append the input filename
558 dst=`echo "$dst" | sed -e 's:/$::'`
559 dstfile=`echo "$src" | sed -e 's;.*/\([^/]*\)$;\1;'`
563 # Add a possible extension to src and dst
564 if [ ".$opt_e" != . ]; then
569 # Check for correct arguments
570 if [ ".$src" = ".$dst" ]; then
571 echo "$msgprefix:Error: source and destination are the same" 1>&2
575 # Make a temp file name in the destination directory
576 dstdir=`echo $dst | sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;' -e 's;^$;.;'`
577 dsttmp="$dstdir/#INST@$$#"
580 if [ ".$opt_v" = .yes ]; then
581 echo "$src -> $dst" 1>&2
584 # Copy or move the file name to the temp name
585 # (because we might be not allowed to change the source)
586 if [ ".$opt_C" = .yes ]; then
589 if [ ".$opt_c" = .yes ]; then
590 if [ ".$opt_t" = .yes ]; then
591 echo "cp $src $dsttmp" 1>&2
593 cp $src $dsttmp || exit $?
595 if [ ".$opt_t" = .yes ]; then
596 echo "mv $src $dsttmp" 1>&2
598 mv $src $dsttmp || exit $?
601 # Adjust the target file
602 # (we do chmod last to preserve setuid bits)
603 if [ ".$opt_s" = .yes ]; then
604 if [ ".$opt_t" = .yes ]; then
605 echo "strip $dsttmp" 1>&2
607 strip $dsttmp || exit $?
609 if [ ".$opt_o" != . ]; then
610 if [ ".$opt_t" = .yes ]; then
611 echo "chown $opt_o $dsttmp" 1>&2
613 chown $opt_o $dsttmp || exit $?
615 if [ ".$opt_g" != . ]; then
616 if [ ".$opt_t" = .yes ]; then
617 echo "chgrp $opt_g $dsttmp" 1>&2
619 chgrp $opt_g $dsttmp || exit $?
621 if [ ".$opt_m" != . ]; then
622 if [ ".$opt_t" = .yes ]; then
623 echo "chmod $opt_m $dsttmp" 1>&2
625 chmod $opt_m $dsttmp || exit $?
628 # Determine whether to do a quick install
629 # (has to be done _after_ the strip was already done)
631 if [ ".$opt_C" = .yes ]; then
633 if cmp -s $src $dst; then
639 # Finally install the file to the real destination
640 if [ $quick = yes ]; then
641 if [ ".$opt_t" = .yes ]; then
642 echo "rm -f $dsttmp" 1>&2
646 if [ ".$opt_t" = .yes ]; then
647 echo "rm -f $dst && mv $dsttmp $dst" 1>&2
649 rm -f $dst && mv $dsttmp $dst
655 ## mkdir -- Make one or more directories
656 ## Copyright (c) 1996-2000 Ralf S. Engelschall <rse@engelschall.com>
657 ## Originally written for public domain by Noah Friedman <friedman@prep.ai.mit.edu>
658 ## Cleaned up and enhanced for shtool
662 for p in ${1+"$@"}; do
663 # if the directory already exists...
665 if [ ".$opt_f" = .no ] && [ ".$opt_p" = .no ]; then
666 echo "$msgprefix:Error: directory already exists: $p" 1>&2
673 # if the directory has to be created...
674 if [ ".$opt_p" = .no ]; then
675 if [ ".$opt_t" = .yes ]; then
678 mkdir $p || errstatus=$?
680 # the smart situation
681 set fnord `echo ":$p" |\
688 for d in ${1+"$@"}; do
689 pathcomp="$pathcomp$d"
691 -* ) pathcomp="./$pathcomp" ;;
693 if [ ! -d "$pathcomp" ]; then
694 if [ ".$opt_t" = .yes ]; then
695 echo "mkdir $pathcomp" 1>&2
697 mkdir $pathcomp || errstatus=$?
698 if [ ".$opt_m" != . ]; then
699 if [ ".$opt_t" = .yes ]; then
700 echo "chmod $opt_m $pathcomp" 1>&2
702 chmod $opt_m $pathcomp || errstatus=$?
705 pathcomp="$pathcomp/"