3 ## GNU shtool -- The GNU Portable Shell Tool
4 ## Copyright (c) 1994-2006 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: 2.0.5 (07-Feb-2006)
10 ## Contents: 6/19 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 ## move Move files with simultaneous substitution
43 ## install Install a program, script or datafile
44 ## mkdir Make one or more directories
45 ## mkln Make link with calculation of relative paths
46 ## subst Apply sed(1) substitution operations
48 ## Not available commands (because module was not built-in):
49 ## mdate Pretty-print modification time of a file or dir
50 ## table Pretty-print a field-separated list as a table
51 ## prop Display progress with a running propeller
52 ## mkshadow Make a shadow tree through symbolic links
53 ## fixperm Fix file permissions inside a source tree
54 ## rotate Logfile rotation
55 ## tarball Roll distribution tarballs
56 ## platform Platform Identification Utility
57 ## arx Extended archive command
58 ## slo Separate linker options by library class
59 ## scpp Sharing C Pre-Processor
60 ## version Maintain a version information file
61 ## path Deal with program paths
64 # maximum Bourne-Shell compatibility
65 if [ ".$ZSH_VERSION" != . ] && (emulate sh) >/dev/null 2>&1; then
69 alias -g '${1+"$@"}'='"$@"'
70 elif [ ".$BASH_VERSION" != . ] && (set -o posix) >/dev/null 2>&1; then
75 # maximum independence of NLS nuisances
77 LANG LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_CTYPE LC_IDENTIFICATION \
78 LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER \
81 if (set +x; test -z "`(eval $var=C; export $var) 2>&1`"); then
82 eval $var=C; export $var
88 # initial command line handling
90 echo "$0:Error: invalid command line" 1>&2
91 echo "$0:Hint: run \`$0 -h' for usage" 1>&2
94 if [ ".$1" = ".-h" ] || [ ".$1" = ".--help" ]; then
95 echo "This is GNU shtool, version 2.0.5 (07-Feb-2006)"
96 echo 'Copyright (c) 1994-2006 Ralf S. Engelschall <rse@engelschall.com>'
97 echo 'Report bugs to <bug-shtool@gnu.org>'
99 echo 'Usage: shtool [<options>] [<cmd-name> [<cmd-options>] [<cmd-args>]]'
101 echo 'Available global <options>:'
102 echo ' -v, --version display shtool version information'
103 echo ' -h, --help display shtool usage help page (this one)'
104 echo ' -d, --debug display shell trace information'
105 echo ' -r, --recreate recreate this shtool script via shtoolize'
107 echo 'Available <cmd-name> [<cmd-options>] [<cmd-args>]:'
108 echo ' echo [-n|--newline] [-e|--expand] [<string> ...]'
109 echo ' move [-v|--verbose] [-t|--trace] [-e|--expand] [-p|--preserve]'
110 echo ' <src-file> <dst-file>'
111 echo ' install [-v|--verbose] [-t|--trace] [-d|--mkdir] [-c|--copy]'
112 echo ' [-C|--compare-copy] [-s|--strip] [-m|--mode <mode>]'
113 echo ' [-o|--owner <owner>] [-g|--group <group>] [-e|--exec'
114 echo ' <sed-cmd>] <file> [<file> ...] <path>'
115 echo ' mkdir [-t|--trace] [-f|--force] [-p|--parents] [-m|--mode'
116 echo ' <mode>] [-o|--owner <owner>] [-g|--group <group>] <dir>'
118 echo ' mkln [-t|--trace] [-f|--force] [-s|--symbolic] <src-path>'
119 echo ' [<src-path> ...] <dst-path>'
120 echo ' subst [-v|--verbose] [-t|--trace] [-n|--nop] [-w|--warning]'
121 echo ' [-q|--quiet] [-s|--stealth] [-i|--interactive] [-b|--backup'
122 echo ' <ext>] [-e|--exec <cmd>] [-f|--file <cmd-file>] [<file>]'
125 echo 'Not available <cmd-name> (because module was not built-in):'
126 echo ' mdate [-n|--newline] [-z|--zero] [-s|--shorten] [-d|--digits]'
127 echo ' [-f|--field-sep <str>] [-o|--order <spec>] <path>'
128 echo ' table [-F|--field-sep <sep>] [-w|--width <width>] [-c|--columns'
129 echo ' <cols>] [-s|--strip <strip>] <str><sep><str>...'
130 echo ' prop [-p|--prefix <str>]'
131 echo ' mkshadow [-v|--verbose] [-t|--trace] [-a|--all] <src-dir> <dst-dir>'
132 echo ' fixperm [-v|--verbose] [-t|--trace] <path> [<path> ...]'
133 echo ' rotate [-v|--verbose] [-t|--trace] [-f|--force] [-n|--num-files'
134 echo ' <count>] [-s|--size <size>] [-c|--copy] [-r|--remove]'
135 echo ' [-a|--archive-dir <dir>] [-z|--compress [<tool>:]<level>]'
136 echo ' [-b|--background] [-d|--delay] [-p|--pad <len>] [-m|--mode'
137 echo ' <mode>] [-o|--owner <owner>] [-g|--group <group>] [-M|--migrate'
138 echo ' <cmd>] [-P|--prolog <cmd>] [-E|--epilog <cmd>] <file> [...]'
139 echo ' tarball [-t|--trace] [-v|--verbose] [-o|--output <tarball>]'
140 echo ' [-c|--compress <prog>] [-d|--directory <dir>] [-u|--user'
141 echo ' <user>] [-g|--group <group>] [-e|--exclude <pattern>]'
142 echo ' <path> [<path> ...]'
143 echo ' platform [-F|--format <format>] [-S|--sep <string>] [-C|--conc'
144 echo ' <string>] [-L|--lower] [-U|--upper] [-v|--verbose]'
145 echo ' [-c|--concise] [-n|--no-newline] [-t|--type <type>]'
146 echo ' [-V|--version] [-h|--help]'
147 echo ' arx [-t|--trace] [-C|--command <cmd>] <op> <archive> [<file>'
149 echo ' slo [-p|--prefix <str>] -- -L<dir> -l<lib> [-L<dir> -l<lib>'
151 echo ' scpp [-v|--verbose] [-p|--preserve] [-f|--filter <filter>]'
152 echo ' [-o|--output <ofile>] [-t|--template <tfile>] [-M|--mark'
153 echo ' <mark>] [-D|--define <dname>] [-C|--class <cname>]'
154 echo ' <file> [<file> ...]'
155 echo ' version [-l|--language <lang>] [-n|--name <name>] [-p|--prefix'
156 echo ' <prefix>] [-s|--set <version>] [-e|--edit] [-i|--increase'
157 echo ' <knob>] [-d|--display <type>] <file>'
158 echo ' path [-s|--suppress] [-r|--reverse] [-d|--dirname] [-b|--basename]'
159 echo ' [-m|--magic] [-p|--path <path>] <str> [<str> ...]'
163 if [ ".$1" = ".-v" ] || [ ".$1" = ".--version" ]; then
164 echo "GNU shtool 2.0.5 (07-Feb-2006)"
167 if [ ".$1" = ".-r" ] || [ ".$1" = ".--recreate" ]; then
168 shtoolize -oshtool echo move install mkdir mkln subst
171 if [ ".$1" = ".-d" ] || [ ".$1" = ".--debug" ]; then
175 name=`echo "$0" | sed -e 's;.*/\([^/]*\)$;\1;' -e 's;-sh$;;' -e 's;\.sh$;;'`
177 echo|move|install|mkdir|mkln|subst )
178 # implicit tool command selection
182 # explicit tool command selection
192 ## DISPATCH INTO SCRIPT PROLOG
198 str_usage="[-n|--newline] [-e|--expand] [<string> ...]"
201 opt_alias="n:newline,e:expand"
207 str_usage="[-v|--verbose] [-t|--trace] [-e|--expand] [-p|--preserve] <src-file> <dst-file>"
210 opt_alias="v:verbose,t:trace,e:expand,p:preserve"
218 str_usage="[-v|--verbose] [-t|--trace] [-d|--mkdir] [-c|--copy] [-C|--compare-copy] [-s|--strip] [-m|--mode <mode>] [-o|--owner <owner>] [-g|--group <group>] [-e|--exec <sed-cmd>] <file> [<file> ...] <path>"
220 opt_spec="v.t.d.c.C.s.m:o:g:e+"
221 opt_alias="v:verbose,t:trace,d:mkdir,c:copy,C:compare-copy,s:strip,m:mode,o:owner,g:group,e:exec"
235 str_usage="[-t|--trace] [-f|--force] [-p|--parents] [-m|--mode <mode>] [-o|--owner <owner>] [-g|--group <group>] <dir> [<dir> ...]"
237 opt_spec="t.f.p.m:o:g:"
238 opt_alias="t:trace,f:force,p:parents,m:mode,o:owner,g:group"
248 str_usage="[-t|--trace] [-f|--force] [-s|--symbolic] <src-path> [<src-path> ...] <dst-path>"
251 opt_alias="t:trace,f:force,s:symbolic"
258 str_usage="[-v|--verbose] [-t|--trace] [-n|--nop] [-w|--warning] [-q|--quiet] [-s|--stealth] [-i|--interactive] [-b|--backup <ext>] [-e|--exec <cmd>] [-f|--file <cmd-file>] [<file>] [...]"
261 opt_spec="v.t.n.w.q.s.i.b:e+f:"
262 opt_alias="v:verbose,t:trace,n:nop,w:warning,q:quiet,s:stealth,i:interactive,b:backup,e:exec,f:file"
275 echo "$0:Error: unknown option \`$tool'" 2>&1
276 echo "$0:Hint: run \`$0 -h' for usage" 2>&1
280 echo "$0:Error: unknown command \`$tool'" 2>&1
281 echo "$0:Hint: run \`$0 -h' for usage" 2>&1
287 ## COMMON UTILITY CODE
290 # commonly used ASCII values
295 # determine name of tool
296 if [ ".$tool" != . ]; then
297 # used inside shtool script
299 toolcmdhelp="shtool $tool"
300 msgprefix="shtool:$tool"
302 # used as standalone script
305 msgprefix="$str_tool"
308 # parse argument specification string
309 eval `echo $arg_spec |\
310 sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS=\1; arg_MODE=\2/'`
312 # parse option specification string
313 eval `echo h.$opt_spec |\
314 sed -e 's/\([a-zA-Z0-9]\)\([.:+]\)/opt_MODE_\1=\2;/g'`
316 # parse option alias string
317 eval `echo h:help,$opt_alias |\
318 sed -e 's/-/_/g' -e 's/\([a-zA-Z0-9]\):\([^,]*\),*/opt_ALIAS_\2=\1;/g'`
320 # interate over argument line
322 while [ $# -gt 0 ]; do
323 # special option stops processing
324 if [ ".$1" = ".--" ]; then
329 # determine option and argument
331 if [ ".$opt_PREV" != . ]; then
332 # merge previous seen option with argument
338 # split argument into option and argument
342 sed -e 's/^x--\([a-zA-Z0-9-]*\)=\(.*\)$/opt_OPT="\1";opt_ARG="\2"/'`
343 opt_STR=`echo $opt_OPT | sed -e 's/-/_/g'`
344 eval "opt_OPT=\${opt_ALIAS_${opt_STR}-${opt_OPT}}"
347 opt_OPT=`echo "x$1" | cut -c4-`
348 opt_STR=`echo $opt_OPT | sed -e 's/-/_/g'`
349 eval "opt_OPT=\${opt_ALIAS_${opt_STR}-${opt_OPT}}"
354 sed -e 's/^x-\([a-zA-Z0-9]\)/opt_OPT="\1";/' \
355 -e 's/";\(.*\)$/"; opt_ARG="\1"/'`
358 opt_OPT=`echo "x$1" | cut -c3-`
370 # determine whether option needs an argument
371 eval "opt_MODE=\$opt_MODE_${opt_OPT}"
372 if [ ".$opt_ARG" = . ] && [ ".$opt_ARG_OK" != .yes ]; then
373 if [ ".$opt_MODE" = ".:" ] || [ ".$opt_MODE" = ".+" ]; then
383 eval "opt_${opt_OPT}=yes"
386 # option with argument (multiple occurances override)
387 eval "opt_${opt_OPT}=\"\$opt_ARG\""
390 # option with argument (multiple occurances append)
391 eval "opt_${opt_OPT}=\"\$opt_${opt_OPT}\${ASC_NL}\$opt_ARG\""
394 echo "$msgprefix:Error: unknown option: \`$opt_OPT'" 1>&2
395 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
400 if [ ".$opt_PREV" != . ]; then
401 echo "$msgprefix:Error: missing argument to option \`$opt_PREV'" 1>&2
402 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2
406 # process help option
407 if [ ".$opt_h" = .yes ]; then
408 echo "Usage: $toolcmdhelp $str_usage"
412 # complain about incorrect number of arguments
415 if [ $# -ne $arg_NUMS ]; then
416 echo "$msgprefix:Error: invalid number of arguments (exactly $arg_NUMS expected)" 1>&2
417 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2
422 if [ $# -lt $arg_NUMS ]; then
423 echo "$msgprefix:Error: invalid number of arguments (at least $arg_NUMS expected)" 1>&2
424 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2
430 # establish a temporary file on request
431 if [ ".$gen_tmpfile" = .yes ]; then
432 # create (explicitly) secure temporary directory
433 if [ ".$TMPDIR" != . ]; then
435 elif [ ".$TEMPDIR" != . ]; then
440 tmpdir="$tmpdir/.shtool.$$"
442 rm -rf "$tmpdir" >/dev/null 2>&1 || true
443 mkdir "$tmpdir" >/dev/null 2>&1
444 if [ $? -ne 0 ]; then
445 echo "$msgprefix:Error: failed to create temporary directory \`$tmpdir'" 1>&2
450 # create (implicitly) secure temporary file
451 tmpfile="$tmpdir/shtool.tmp"
455 # utility function: map string to lower case
457 echo "$1" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
460 # utility function: map string to upper case
462 echo "$1" | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
468 if [ ".$gen_tmpfile" = .yes ]; then
469 rm -rf "$tmpdir" >/dev/null 2>&1 || true
475 ## DISPATCH INTO SCRIPT BODY
482 ## echo -- Print string with optional construct expansion
483 ## Copyright (c) 1998-2006 Ralf S. Engelschall <rse@engelschall.com>
488 # check for broken escape sequence expansion
490 bytes=`echo '\1' | wc -c | awk '{ printf("%s", $1); }'`
491 if [ ".$bytes" != .3 ]; then
492 bytes=`echo -E '\1' | wc -c | awk '{ printf("%s", $1); }'`
493 if [ ".$bytes" = .3 ]; then
498 # check for existing -n option (to suppress newline)
500 bytes=`echo -n 123 2>/dev/null | wc -c | awk '{ printf("%s", $1); }'`
501 if [ ".$bytes" = .3 ]; then
505 # determine terminal bold sequence
508 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[Bb]'`" != . ]; then
510 # for the most important terminal types we directly know the sequences
511 xterm|xterm*|vt220|vt220*)
512 term_bold=`awk 'BEGIN { printf("%c%c%c%c", 27, 91, 49, 109); }' </dev/null 2>/dev/null`
513 term_norm=`awk 'BEGIN { printf("%c%c%c", 27, 91, 109); }' </dev/null 2>/dev/null`
516 term_bold=`awk 'BEGIN { printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }' </dev/null 2>/dev/null`
517 term_norm=`awk 'BEGIN { printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }' </dev/null 2>/dev/null`
519 # for all others, we try to use a possibly existing `tput' or `tcout' utility
521 paths=`echo $PATH | sed -e 's/:/ /g'`
522 for tool in tput tcout; do
523 for dir in $paths; do
524 if [ -r "$dir/$tool" ]; then
525 for seq in bold md smso; do # 'smso' is last
526 bold="`$dir/$tool $seq 2>/dev/null`"
527 if [ ".$bold" != . ]; then
532 if [ ".$term_bold" != . ]; then
533 for seq in sgr0 me rmso init reset; do # 'reset' is last
534 norm="`$dir/$tool $seq 2>/dev/null`"
535 if [ ".$norm" != . ]; then
544 if [ ".$term_bold" != . ] && [ ".$term_norm" != . ]; then
550 if [ ".$term_bold" = . ] || [ ".$term_norm" = . ]; then
551 echo "$msgprefix:Warning: unable to determine terminal sequence for bold mode" 1>&2
557 # determine user name
559 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[uUgG]'`" != . ]; then
560 username="`(id -un) 2>/dev/null`"
561 if [ ".$username" = . ]; then
562 str="`(id) 2>/dev/null`"
563 if [ ".`echo $str | grep '^uid[ ]*=[ ]*[0-9]*('`" != . ]; then
564 username=`echo $str | sed -e 's/^uid[ ]*=[ ]*[0-9]*(//' -e 's/).*$//'`
566 if [ ".$username" = . ]; then
568 if [ ".$username" = . ]; then
570 if [ ".$username" = . ]; then
571 username="`(whoami) 2>/dev/null |\
572 awk '{ printf("%s", $1); }'`"
573 if [ ".$username" = . ]; then
574 username="`(who am i) 2>/dev/null |\
575 awk '{ printf("%s", $1); }'`"
576 if [ ".$username" = . ]; then
588 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%U'`" != . ]; then
589 userid="`(id -u) 2>/dev/null`"
590 if [ ".$userid" = . ]; then
591 userid="`(id -u ${username}) 2>/dev/null`"
592 if [ ".$userid" = . ]; then
593 str="`(id) 2>/dev/null`"
594 if [ ".`echo $str | grep '^uid[ ]*=[ ]*[0-9]*('`" != . ]; then
595 userid=`echo $str | sed -e 's/^uid[ ]*=[ ]*//' -e 's/(.*$//'`
597 if [ ".$userid" = . ]; then
598 userid=`(getent passwd ${username}) 2>/dev/null | \
599 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
600 if [ ".$userid" = . ]; then
601 userid=`grep "^${username}:" /etc/passwd 2>/dev/null | \
602 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
603 if [ ".$userid" = . ]; then
604 userid=`(ypcat passwd) 2>/dev/null |
605 grep "^${username}:" | \
606 sed -e 's/[^:]*:[^:]*://' -e 's/:.*$//'`
607 if [ ".$userid" = . ]; then
617 # determine (primary) group id
619 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[gG]'`" != . ]; then
620 groupid="`(id -g ${username}) 2>/dev/null`"
621 if [ ".$groupid" = . ]; then
622 str="`(id) 2>/dev/null`"
623 if [ ".`echo $str | grep 'gid[ ]*=[ ]*[0-9]*('`" != . ]; then
624 groupid=`echo $str | sed -e 's/^.*gid[ ]*=[ ]*//' -e 's/(.*$//'`
626 if [ ".$groupid" = . ]; then
627 groupid=`(getent passwd ${username}) 2>/dev/null | \
628 sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
629 if [ ".$groupid" = . ]; then
630 groupid=`grep "^${username}:" /etc/passwd 2>/dev/null | \
631 sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
632 if [ ".$groupid" = . ]; then
633 groupid=`(ypcat passwd) 2>/dev/null | grep "^${username}:" | \
634 sed -e 's/[^:]*:[^:]*:[^:]*://' -e 's/:.*$//'`
635 if [ ".$groupid" = . ]; then
644 # determine (primary) group name
646 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%g'`" != . ]; then
647 groupname="`(id -gn ${username}) 2>/dev/null`"
648 if [ ".$groupname" = . ]; then
649 str="`(id) 2>/dev/null`"
650 if [ ".`echo $str | grep 'gid[ ]*=[ ]*[0-9]*('`" != . ]; then
651 groupname=`echo $str | sed -e 's/^.*gid[ ]*=[ ]*[0-9]*(//' -e 's/).*$//'`
653 if [ ".$groupname" = . ]; then
654 groupname=`(getent group) 2>/dev/null | \
655 grep "^[^:]*:[^:]*:${groupid}:" | \
657 if [ ".$groupname" = . ]; then
658 groupname=`grep "^[^:]*:[^:]*:${groupid}:" /etc/group 2>/dev/null | \
660 if [ ".$groupname" = . ]; then
661 groupname=`(ypcat group) 2>/dev/null | \
662 grep "^[^:]*:[^:]*:${groupid}:" | \
664 if [ ".$groupname" = . ]; then
673 # determine host and domain name
676 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%h'`" != . ]; then
677 hostname="`(uname -n) 2>/dev/null |\
678 awk '{ printf("%s", $1); }'`"
679 if [ ".$hostname" = . ]; then
680 hostname="`(hostname) 2>/dev/null |\
681 awk '{ printf("%s", $1); }'`"
682 if [ ".$hostname" = . ]; then
688 domainname=".`echo $hostname | cut -d. -f2-`"
689 hostname="`echo $hostname | cut -d. -f1`"
693 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%d'`" != . ]; then
694 if [ ".$domainname" = . ]; then
695 if [ -f /etc/resolv.conf ]; then
696 domainname="`grep '^[ ]*domain' /etc/resolv.conf | sed -e 'q' |\
697 sed -e 's/.*domain//' \
698 -e 's/^[ ]*//' -e 's/^ *//' -e 's/^ *//' \
699 -e 's/^\.//' -e 's/^/./' |\
700 awk '{ printf("%s", $1); }'`"
701 if [ ".$domainname" = . ]; then
702 domainname="`grep '^[ ]*search' /etc/resolv.conf | sed -e 'q' |\
703 sed -e 's/.*search//' \
704 -e 's/^[ ]*//' -e 's/^ *//' -e 's/^ *//' \
705 -e 's/ .*//' -e 's/ .*//' \
706 -e 's/^\.//' -e 's/^/./' |\
707 awk '{ printf("%s", $1); }'`"
713 # determine current time
718 if [ ".$opt_e" = .yes ] && [ ".`echo $text | grep '%[DMYm]'`" != . ]; then
719 time_day=`date '+%d'`
720 time_month=`date '+%m'`
721 time_year=`date '+%Y' 2>/dev/null`
722 if [ ".$time_year" = . ]; then
723 time_year=`date '+%y'`
725 [5-9][0-9]) time_year="19$time_year" ;;
726 [0-4][0-9]) time_year="20$time_year" ;;
730 1|01) time_monthname='Jan' ;;
731 2|02) time_monthname='Feb' ;;
732 3|03) time_monthname='Mar' ;;
733 4|04) time_monthname='Apr' ;;
734 5|05) time_monthname='May' ;;
735 6|06) time_monthname='Jun' ;;
736 7|07) time_monthname='Jul' ;;
737 8|08) time_monthname='Aug' ;;
738 9|09) time_monthname='Sep' ;;
739 10) time_monthname='Oct' ;;
740 11) time_monthname='Nov' ;;
741 12) time_monthname='Dec' ;;
745 # expand special ``%x'' constructs
746 if [ ".$opt_e" = .yes ]; then
747 text=`echo $seo "$text" |\
748 sed -e "s/%B/${term_bold}/g" \
749 -e "s/%b/${term_norm}/g" \
750 -e "s/%u/${username}/g" \
751 -e "s/%U/${userid}/g" \
752 -e "s/%g/${groupname}/g" \
753 -e "s/%G/${groupid}/g" \
754 -e "s/%h/${hostname}/g" \
755 -e "s/%d/${domainname}/g" \
756 -e "s/%D/${time_day}/g" \
757 -e "s/%M/${time_month}/g" \
758 -e "s/%Y/${time_year}/g" \
759 -e "s/%m/${time_monthname}/g" 2>/dev/null`
763 if [ .$opt_n = .no ]; then
766 # the harder part: echo -n is best, because
767 # awk may complain about some \xx sequences.
768 if [ ".$minusn" != . ]; then
769 echo $seo $minusn "$text"
771 echo dummy | awk '{ printf("%s", TEXT); }' TEXT="$text"
780 ## move -- Move files with simultaneous substitution
781 ## Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
788 if [ ".$src" = . ] || [ ".$dst" = . ]; then
789 echo "$msgprefix:Error: Invalid arguments" 1>&2
792 if [ ".$src" = ".$dst" ]; then
793 echo "$msgprefix:Error: Source and destination files are the same" 1>&2
797 if [ ".$opt_e" = .yes ]; then
798 expsrc="`echo $expsrc`"
800 if [ ".$opt_e" = .yes ]; then
801 if [ ".`echo "$src" | sed -e 's;^.*\\*.*$;;'`" = ".$src" ]; then
802 echo "$msgprefix:Error: Source doesn't contain wildcard ('*'): $dst" 1>&2
805 if [ ".`echo "$dst" | sed -e 's;^.*%[1-9].*$;;'`" = ".$dst" ]; then
806 echo "$msgprefix:Error: Destination doesn't contain substitution ('%N'): $dst" 1>&2
809 if [ ".$expsrc" = ".$src" ]; then
810 echo "$msgprefix:Error: Sources not found or no asterisk : $src" 1>&2
814 if [ ! -r "$src" ]; then
815 echo "$msgprefix:Error: Source not found: $src" 1>&2
820 # determine substitution patterns
821 if [ ".$opt_e" = .yes ]; then
822 srcpat=`echo "$src" | sed -e 's/\\./\\\\./g' -e 's/;/\\;/g' -e 's;\\*;\\\\(.*\\\\);g'`
823 dstpat=`echo "$dst" | sed -e 's;%\([1-9]\);\\\\\1;g'`
826 # iterate over source(s)
827 for onesrc in $expsrc; do
828 if [ .$opt_e = .yes ]; then
829 onedst=`echo $onesrc | sed -e "s;$srcpat;$dstpat;"`
834 if [ ".$opt_v" = .yes ]; then
835 echo "$onesrc -> $onedst"
837 if [ ".$opt_p" = .yes ]; then
838 if [ -r $onedst ]; then
839 if cmp -s $onesrc $onedst; then
840 if [ ".$opt_t" = .yes ]; then
841 echo "rm -f $onesrc" 1>&2
843 rm -f $onesrc || errorstatus=$?
845 if [ ".$opt_t" = .yes ]; then
846 echo "mv -f $onesrc $onedst" 1>&2
848 mv -f $onesrc $onedst || errorstatus=$?
851 if [ ".$opt_t" = .yes ]; then
852 echo "mv -f $onesrc $onedst" 1>&2
854 mv -f $onesrc $onedst || errorstatus=$?
857 if [ ".$opt_t" = .yes ]; then
858 echo "mv -f $onesrc $onedst" 1>&2
860 mv -f $onesrc $onedst || errorstatus=$?
862 if [ $errorstatus -ne 0 ]; then
867 shtool_exit $errorstatus
872 ## install -- Install a program, script or datafile
873 ## Copyright (c) 1997-2006 Ralf S. Engelschall <rse@engelschall.com>
876 # special case: "shtool install -d <dir> [...]" internally
877 # maps to "shtool mkdir -f -p -m 755 <dir> [...]"
878 if [ "$opt_d" = yes ]; then
879 cmd="$0 mkdir -f -p -m 755"
880 if [ ".$opt_o" != . ]; then
881 cmd="$cmd -o '$opt_o'"
883 if [ ".$opt_g" != . ]; then
884 cmd="$cmd -g '$opt_g'"
886 if [ ".$opt_v" = .yes ]; then
889 if [ ".$opt_t" = .yes ]; then
893 eval "$cmd $dir" || shtool_exit $?
898 # determine source(s) and destination
901 while [ $# -gt 1 ]; do
907 # type check for destination
909 if [ -d $dstpath ]; then
910 dstpath=`echo "$dstpath" | sed -e 's:/$::'`
914 # consistency check for destination
915 if [ $argc -gt 2 ] && [ $dstisdir = 0 ]; then
916 echo "$msgprefix:Error: multiple sources require destination to be directory" 1>&2
920 # iterate over all source(s)
924 # if destination is a directory, append the input filename
925 if [ $dstisdir = 1 ]; then
926 dstfile=`echo "$src" | sed -e 's;.*/\([^/]*\)$;\1;'`
930 # check for correct arguments
931 if [ ".$src" = ".$dst" ]; then
932 echo "$msgprefix:Warning: source and destination are the same - skipped" 1>&2
935 if [ -d "$src" ]; then
936 echo "$msgprefix:Warning: source \`$src' is a directory - skipped" 1>&2
940 # make a temp file name in the destination directory
942 sed -e 's;[^/]*$;;' -e 's;\(.\)/$;\1;' -e 's;^$;.;' \
943 -e "s;\$;/#INST@$$#;"`
946 if [ ".$opt_v" = .yes ]; then
947 echo "$src -> $dst" 1>&2
950 # copy or move the file name to the temp name
951 # (because we might be not allowed to change the source)
952 if [ ".$opt_C" = .yes ]; then
955 if [ ".$opt_c" = .yes ]; then
956 if [ ".$opt_t" = .yes ]; then
957 echo "cp $src $dsttmp" 1>&2
959 cp $src $dsttmp || shtool_exit $?
961 if [ ".$opt_t" = .yes ]; then
962 echo "mv $src $dsttmp" 1>&2
964 mv $src $dsttmp || shtool_exit $?
967 # adjust the target file
968 if [ ".$opt_e" != . ]; then
970 OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
975 cp $dsttmp $dsttmp.old
977 eval "$sed <$dsttmp.old >$dsttmp" || shtool_exit $?
980 if [ ".$opt_s" = .yes ]; then
981 if [ ".$opt_t" = .yes ]; then
982 echo "strip $dsttmp" 1>&2
984 strip $dsttmp || shtool_exit $?
986 if [ ".$opt_o" != . ]; then
987 if [ ".$opt_t" = .yes ]; then
988 echo "chown $opt_o $dsttmp" 1>&2
990 chown $opt_o $dsttmp || shtool_exit $?
992 if [ ".$opt_g" != . ]; then
993 if [ ".$opt_t" = .yes ]; then
994 echo "chgrp $opt_g $dsttmp" 1>&2
996 chgrp $opt_g $dsttmp || shtool_exit $?
998 if [ ".$opt_m" != ".-" ]; then
999 if [ ".$opt_t" = .yes ]; then
1000 echo "chmod $opt_m $dsttmp" 1>&2
1002 chmod $opt_m $dsttmp || shtool_exit $?
1005 # determine whether to do a quick install
1006 # (has to be done _after_ the strip was already done)
1008 if [ ".$opt_C" = .yes ]; then
1009 if [ -r $dst ]; then
1010 if cmp -s $src $dst; then
1016 # finally, install the file to the real destination
1017 if [ $quick = yes ]; then
1018 if [ ".$opt_t" = .yes ]; then
1019 echo "rm -f $dsttmp" 1>&2
1023 if [ ".$opt_t" = .yes ]; then
1024 echo "rm -f $dst && mv $dsttmp $dst" 1>&2
1026 rm -f $dst && mv $dsttmp $dst
1035 ## mkdir -- Make one or more directories
1036 ## Copyright (c) 1996-2006 Ralf S. Engelschall <rse@engelschall.com>
1040 for p in ${1+"$@"}; do
1041 # if the directory already exists...
1042 if [ -d "$p" ]; then
1043 if [ ".$opt_f" = .no ] && [ ".$opt_p" = .no ]; then
1044 echo "$msgprefix:Error: directory already exists: $p" 1>&2
1051 # if the directory has to be created...
1052 if [ ".$opt_p" = .no ]; then
1053 if [ ".$opt_t" = .yes ]; then
1054 echo "mkdir $p" 1>&2
1056 mkdir $p || errstatus=$?
1057 if [ ".$opt_o" != . ]; then
1058 if [ ".$opt_t" = .yes ]; then
1059 echo "chown $opt_o $p" 1>&2
1061 chown $opt_o $p || errstatus=$?
1063 if [ ".$opt_g" != . ]; then
1064 if [ ".$opt_t" = .yes ]; then
1065 echo "chgrp $opt_g $p" 1>&2
1067 chgrp $opt_g $p || errstatus=$?
1069 if [ ".$opt_m" != . ]; then
1070 if [ ".$opt_t" = .yes ]; then
1071 echo "chmod $opt_m $p" 1>&2
1073 chmod $opt_m $p || errstatus=$?
1076 # the smart situation
1077 set fnord `echo ":$p" |\
1078 sed -e 's/^:\//%/' \
1084 for d in ${1+"$@"}; do
1085 pathcomp="$pathcomp$d"
1087 -* ) pathcomp="./$pathcomp" ;;
1089 if [ ! -d "$pathcomp" ]; then
1090 if [ ".$opt_t" = .yes ]; then
1091 echo "mkdir $pathcomp" 1>&2
1093 mkdir $pathcomp || errstatus=$?
1094 if [ ".$opt_o" != . ]; then
1095 if [ ".$opt_t" = .yes ]; then
1096 echo "chown $opt_o $pathcomp" 1>&2
1098 chown $opt_o $pathcomp || errstatus=$?
1100 if [ ".$opt_g" != . ]; then
1101 if [ ".$opt_t" = .yes ]; then
1102 echo "chgrp $opt_g $pathcomp" 1>&2
1104 chgrp $opt_g $pathcomp || errstatus=$?
1106 if [ ".$opt_m" != . ]; then
1107 if [ ".$opt_t" = .yes ]; then
1108 echo "chmod $opt_m $pathcomp" 1>&2
1110 chmod $opt_m $pathcomp || errstatus=$?
1113 pathcomp="$pathcomp/"
1118 shtool_exit $errstatus
1123 ## mkln -- Make link with calculation of relative paths
1124 ## Copyright (c) 1998-2006 Ralf S. Engelschall <rse@engelschall.com>
1127 # determine source(s) and destination
1130 while [ $# -gt 1 ]; do
1135 if [ ! -d $dst ]; then
1136 if [ $args -gt 2 ]; then
1137 echo "$msgprefix:Error: multiple sources not allowed when target isn't a directory" 1>&2
1142 # determine link options
1144 if [ ".$opt_f" = .yes ]; then
1147 if [ ".$opt_s" = .yes ]; then
1151 # iterate over sources
1152 for src in $srcs; do
1153 # determine if one of the paths is an absolute path,
1154 # because then we _have_ to use an absolute symlink
1159 /* ) oneisabs=1; srcisabs=1 ;;
1162 /* ) oneisabs=1; dstisabs=1 ;;
1165 # split source and destination into dir and base name
1166 if [ -d $src ]; then
1167 srcdir=`echo $src | sed -e 's;/*$;;'`
1170 srcdir=`echo $src | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1171 srcbase=`echo $src | sed -e 's;.*/\([^/]*\)$;\1;'`
1173 if [ -d $dst ]; then
1174 dstdir=`echo $dst | sed -e 's;/*$;;'`
1177 dstdir=`echo $dst | sed -e 's;^[^/]*$;;' -e 's;^\(.*/\)[^/]*$;\1;' -e 's;\(.\)/$;\1;'`
1178 dstbase=`echo $dst | sed -e 's;.*/\([^/]*\)$;\1;'`
1182 if [ ".$dstdir" != . ]; then
1183 if [ ! -d $dstdir ]; then
1184 echo "$msgprefix:Error: destination directory not found: $dstdir" 1>&2
1189 # make sure the source is reachable from the destination
1190 if [ $dstisabs = 1 ]; then
1191 if [ $srcisabs = 0 ]; then
1192 if [ ".$srcdir" = . ]; then
1193 srcdir="`pwd | sed -e 's;/*$;;'`"
1196 elif [ -d $srcdir ]; then
1197 srcdir="`cd $srcdir; pwd | sed -e 's;/*$;;'`"
1204 # split away a common prefix
1206 if [ ".$srcdir" = ".$dstdir" ] && [ ".$srcdir" != . ]; then
1211 while [ ".$srcdir" != . ] && [ ".$dstdir" != . ]; do
1212 presrc=`echo $srcdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1213 predst=`echo $dstdir | sed -e 's;^\([^/]*\)/.*;\1;'`
1214 if [ ".$presrc" != ".$predst" ]; then
1217 prefix="$prefix$presrc/"
1218 srcdir=`echo $srcdir | sed -e 's;^[^/]*/*;;'`
1219 dstdir=`echo $dstdir | sed -e 's;^[^/]*/*;;'`
1223 # destination prefix is just the common prefix
1226 # determine source prefix which is the reverse directory
1227 # step-up corresponding to the destination directory
1231 if [ ".$prefix" = . ] || [ ".$prefix" = ./ ]; then
1234 if [ $oneisabs = 0 ] || [ $isroot = 0 ]; then
1236 OIFS="$IFS"; IFS='/'
1238 [ ".$pe" = . ] && continue
1239 [ ".$pe" = .. ] && continue
1244 if [ $srcisabs = 1 ]; then
1249 # determine destination symlink name
1250 if [ ".$dstbase" = . ]; then
1251 if [ ".$srcbase" != . ]; then
1254 dstbase=`echo "$prefix$srcdir" | sed -e 's;/*$;;' -e 's;.*/\([^/]*\)$;\1;'`
1258 # now finalize source and destination directory paths
1259 srcdir=`echo $srcdir | sed -e 's;\([^/]\)$;\1/;'`
1260 dstdir=`echo $dstdir | sed -e 's;\([^/]\)$;\1/;'`
1262 # run the final link command
1263 if [ ".$opt_t" = .yes ]; then
1264 echo "ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase"
1266 eval ln$lnopt $srcpre$srcdir$srcbase $dstpre$dstdir$dstbase
1274 ## subst -- Apply sed(1) substitution operations
1275 ## Copyright (c) 2001-2006 Ralf S. Engelschall <rse@engelschall.com>
1278 # remember optional list of file(s)
1282 # parameter consistency check
1283 if [ $# -eq 0 ] && [ ".$opt_b" != . ]; then
1284 echo "$msgprefix:Error: option -b cannot be applied to stdin" 1>&2
1287 if [ $# -eq 0 ] && [ ".$opt_s" = .yes ]; then
1288 echo "$msgprefix:Error: option -s cannot be applied to stdin" 1>&2
1292 # build underlying sed(1) command
1294 if [ ".$opt_e" != . ]; then
1295 OIFS="$IFS"; IFS="$ASC_NL"; set -- $opt_e; IFS="$OIFS"
1298 sedcmd="$sedcmd -e '$e'"
1300 elif [ ".$opt_f" != . ]; then
1301 if [ ! -f $opt_f ]; then
1302 echo "$msgprefix:Error: command file \`$opt_f' not found or not a regular file" 1>&2
1305 sedcmd="$sedcmd -f '$opt_f'"
1307 echo "$msgprefix:Error: either -e option(s) or -f option required" 1>&2
1311 # determine extension for original file
1313 if [ ".$opt_b" != . ]; then
1317 # apply sed(1) operation(s)
1318 if [ ".$files" != . ]; then
1319 # apply operation(s) to files
1321 for file in $files; do
1322 test ".$file" = . && continue
1323 if [ ! -f $file ]; then
1324 echo "$msgprefix:Warning: file \`$file' not found or not a regular file" 1>&2
1328 # handle interactive mode
1329 if [ ".$opt_i" = .yes ]; then
1330 eval "$sedcmd <$file >$file.new"
1332 if cmp $file $file.new >/dev/null 2>&1; then
1336 (diff -U1 $file $file.new >$tmpfile) 2>/dev/null
1337 if [ ".`cat $tmpfile`" = . ]; then
1338 (diff -C1 $file $file.new >$tmpfile) 2>/dev/null
1339 if [ ".`cat $tmpfile`" = . ]; then
1340 echo "$msgprefix:Warning: unable to show difference for file \`$file'" 1>&2
1341 cp /dev/null $tmpfile
1346 echo dummy | awk '{ printf("%s", TEXT); }' TEXT=">>> Apply [Y/n]: "
1348 if [ ".$input" != .Y ] &&\
1349 [ ".$input" != .y ] &&\
1350 [ ".$input" != . ]; then
1354 if [ ".$skip" = .yes ]; then
1355 if [ ".$opt_v" = .yes ]; then
1356 echo "file \`$file' -- skipped" 1>&2
1362 # apply sed(1) operation(s)
1363 if [ ".$opt_v" = .yes ]; then
1364 echo "patching \`$file'" 1>&2
1366 if [ ".$opt_t" = .yes ]; then
1367 echo "\$ cp -p $file $file$orig"
1368 echo "\$ chmod u+w $file"
1369 echo "\$ $sedcmd <$file$orig >$file"
1371 if [ ".$opt_n" = .no ]; then
1372 cp -p $file $file$orig
1373 chmod u+w $file >/dev/null 2>&1 || true
1374 eval "$sedcmd <$file$orig >$file"
1377 # optionally fix timestamp
1378 if [ ".$opt_s" = .yes ]; then
1379 if [ ".$opt_t" = .yes ]; then
1380 echo "\$ touch -r $file$orig $file"
1382 if [ ".$opt_n" = .no ]; then
1383 touch -r $file$orig $file
1387 # optionally check whether any content change actually occurred
1388 if [ ".$opt_q" = .no ]; then
1389 if cmp $file$orig $file >/dev/null 2>&1; then
1390 if [ ".$opt_w" = .yes ]; then
1391 echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2
1398 # optionally remove preserved original file
1399 if [ ".$opt_b" = . ]; then
1400 if [ ".$opt_t" = .yes ]; then
1401 echo "\$ rm -f $file$orig"
1403 if [ ".$opt_n" = .no ]; then
1408 if [ ".$opt_q" = .no ] && [ ".$opt_w" = .no ]; then
1409 if [ ".$substdone" = .no ]; then
1410 if [ ".$files_num" = .1 ]; then
1411 echo "$msgprefix:Warning: substitution resulted in no content change on file \"$file\"" 1>&2
1413 echo "$msgprefix:Warning: substitution resulted in no content change on any file" 1>&2
1418 # apply operation(s) to stdin/stdout
1419 if [ ".$opt_v" = .yes ]; then
1420 echo "patching <stdin>" 1>&2
1422 if [ ".$opt_t" = .yes ]; then
1425 if [ ".$opt_n" = .no ]; then