-
Notifications
You must be signed in to change notification settings - Fork 1
/
.shrc
2987 lines (2765 loc) · 71.6 KB
/
.shrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# .shrc - per-shell startup stuff for all shells
#
# This file is only used directly by bosh(1) IFF $ENV is not set, but never used
# otherwise used as $ENVFILE (i.e. $ENV). However it is shared and sourced by
# all that are (including ~/.ashrc normally set as $ENV for bosh(1) et al).
#
# Note: Most of this should be compatible with some pre-POSIX shells. Notably:
#
# - don't use $() for command expansion (ever!) (unless in quotes?)
# - don't use '!' in test expressions for flow-control statements
# - mostly no arithmetic expressions
# - mostly no suffix or prefix patterns
# - don't use ~ for pathname expansion (use $HOME)
# - don't use "export VAR=value"
#
# It may also assume .profile has already set some things, though it should not.
# For the most part variables that are used herein should be set herein.
#
# Maybe even more environment variable settings should move here, especially any
# that are only used by functions here, e.g. as was done with MAILLOG
# (e.g. $PAGER).
#
# Files referenced:
#
# $HOME/.ashtype - sourced if no "type" command, if it is readable
# $HOME/.shpwd - sourced, if it is readable
#
#ident "@(#)HOME:.shrc 37.48 24/11/07 09:43:48 (woods)"
#echo $0: in $HOME/.shrc!
if [ -z "${SHELL}" ]; then
#
# The current shell is likely plain old Bourne (Heirloom) shell
#
# XXX too bad we can't get the true full pathname
#
SHELL=`expr "$0" : '-*\(.*\)'`
fi
# Provide 'type' for shells (i.e. early ash) that don't have it.
#
# Note: early ash reads $SHINIT at start, except if a login shell or called
# with "sh file" (but this feature is not used -- early ash is too rare, and
# besides we want to source .ashtype even if not interactive, e.g. if called by
# onx11server).
#
# The I/O re-direction doesn't actually get rid of the "type: not found" message
# from the old Ash implementation... Perhaps it parses the whole line first?
#
# XXX but maybe it would if we redirected stderr within the subshell too, as we
# must do for GNU Bash? Can't test without an old ash(1) any more though!
#
if ( type type > /dev/null 2>&1 ) > /dev/null 2>&1 ; then
:
elif [ -r ${HOME}/.ashtype ]; then
. ${HOME}/.ashtype
fi
#
# N.B.: the rest of the file is for "interactive" and login shells only!
#
sh_is_interactive=false
sh_is_login=false
case "$-" in
*i*)
sh_is_interactive=true
;;
esac
case "$0" in
-`basename $SHELL`)
# Also setting $sh_is_interactive is really just for (Schily and
# Heirloom) Bourne Shell which does not put "i" in $- until after
# ~/.profile has been sourced, not that they read $ENV by themselves!
#
# N.B.: This presumes of course that login shells are, by definition,
# interactive.
#
# XXX this can be fooled with: SHELL=/bin/sh sh -c 'echo $0' -sh foo!
#
sh_is_interactive=true
sh_is_login=true
;;
esac
case "$-" in
*l*)
# Some shells can be invoked as login shells with '-l' (pdksh, bash).
#
sh_is_interactive=true
sh_is_login=true
;;
esac
# Note: $sh_is_interactive is still not a reliable definition for older shells.
#
# POSIX(Issue 7 and Issue 8) says:
#
# If the -i option is present, or if there are no operands and the shell's
# standard input and standard error are attached to a terminal, the shell
# is considered to be interactive.
#
# Many implementations also add the caveat: "and the -c option is not present".
# This is more or less covered by the "and there are no operands" part, but....
#
# POSIX(Issue 8) does now add to the definition of "$-":
#
# It is unspecified whether the -c and -s options are included in the
# expansion of "$-". The -i option shall be included in "$-" if the shell
# is interactive, regardless of whether it was specified on invocation.
#
# So, for shells that don't add 'i' to $- the tests above are inadequate.
#
# If we could reliably test if the shell was reading commands from stdin
# (e.g. acting as if '-s' was given and there was on '-c'), then if stdin and
# stderr are terminals we would know the shell is in interactive mode. However
# NetBSD sh(1), and presumably most other ash(1) derivatives, along with the
# original Bourne Shell and its derivatives, have tested if stdin and _stdout_
# are TTYs to determine their interactactive-ness. Ksh introduced the change to
# test _stderr_ some time before ksh88e as it allows "sh | tee x" to behave the
# same as "sh". This was fixed in NetBSD sh(1) as of 2017-07-24 and netbsd-8
# (unfortunately after the introduction of $NETBSD_SHELL though).
#
# However since most shells also don't add either "c" or "s" to $- this is not
# possible. Worse yet the original Bourne Shell and its derivatives, including
# even the latest ksh93, add "s" to $- even when they shouldn't (but only when
# there are no further "positional" arguments):
#
# sh -c 'echo $0: \$-=\"$-\"'
#
# Note that '-c' is a plain flag ("c" in getopts, i.e. no parameter) which
# simply declares how remaining arguments are to be treated after any remaining
# option arguments are processed. The first remaining argument is treated as a
# string to be parsed as a script, and the next of any more remaining arguments
# is set as $0, with the rest set as positional parameters in $@ (i.e. as if
# 'set -- args ..." were executed) for that script's use.
#
# N.B. This all really doesn't matter in the real world though -- we can (for
# now) assume all shells that support $ENV will stick 'i' in $- if they are
# interactive (even when -i is not given as an argument), and furthermore shells
# which expand and source $ENV even when not interactive are the only ones we
# really care about anyway. That said, pdksh and its derivatives, including
# NetBSD ksh(1), (which all do source $ENV all the time) can use the old AT&T
# Ksh array trick as they support arrays, so even they don't need any internal
# tests. This is all really for NetBSD sh(1) (when it is not in POSIX mode) and
# other older Ash variants (not dash).
# note: $FROM_DOT_PROFILE is for non-interactive, non-login, uses such as
# directly sourcing ~/.profile e.g. in ~/.xinitrc and from a remotely invoked
# command-line, as is done in onx11server, and getting the definition of
# onx11server, as well as some other environment variable setup, is of course
# why we need this escape hatch in the first place.
#
# xxx the "|| $sh_is_login" is currently redundant -- all login shells are also
# considered to be interactive shells in the tests above.
#
if ${sh_is_interactive} || ${sh_is_login} || ${FROM_DOT_PROFILE:-false}; then
: OK
else
# Everything in this file is for interactive (or explicitly chosen) use
# only, but $ENV is set to just the plain filename of this file because
# ash(1) and its derivatives do not have arrays and so cannot use the
# ksh(1) ${ENVFILE[]} trick, so we must exit now.
#
return
fi
#echo $0: still in $HOME/.shrc!
# do this early so everything else can use it if needed
#
HAVEX=false
if type xinit >/dev/null 2>&1; then
HAVEX=true
fi
# we start with a default of true, but if tty fails, we set it to false
#
# $ISATTY is basically a "memoization" to optimize for executing tty(1)
#
# xxx Hmmmm.... "test -t" also works all the way back to v7.
#
ISATTY="test -t"
TTY=`tty` || ISATTY="false" # "not a tty" if not...
# This file sets D_PS1 at the end to allow setaliases to avoid clobbering
# .*shrc's new value.
#
# XXX In a sense this is an include guard and maybe we could use it as such?
#
if [ -z "${D_PS1}" ]; then
# N.B.: this will be possibly reset, then appended to....
PS1=""
fi
# PS2="> "
# PS3="#? "
# PS4="+ "
is_bash ()
{
[ ${RANDOM:-0} -ne ${RANDOM:-0} -a -n "${BASH}" ]
}
is_ksh ()
{
if [ ${RANDOM:-0} -ne ${RANDOM:-0} -a -z "${BASH}" ]; then
case "${KSH_VERSION}" in
"Version"*)
return 0 # likely att ksh
;;
*"MIRBSD"*)
return 0 # mksh
;;
*"PD"*"KSH"*)
return 0 # likely pdksh or some other derivative
;;
*)
return 1
;;
esac
else
return 1
fi
}
is_attksh ()
{
is_bash && return 1
# ksh93 and [p]bosh
_sh_version=`eval 'echo "${.sh.version}"' 2>/dev/null`
# [p]bosh only
_sh_shell=`eval 'echo "${.sh.shell}"' 2>/dev/null`
test -n "${_sh_version}" -a -z "${_sh_shell}"
_rc=$?
unset _sh_shell _sh_version
return ${_rc}
}
is_ash ()
{
is_ksh && return 1
is_bash && return 1
# older Ash didn't have $RANDOM?
[ "`echo ~`" = "${HOME}" ]
}
is_bourne_sh ()
{
is_ash && return 1
[ "`echo ~`" != "${HOME}" -a -z "${RANDOM}" ]
}
is_posixish_sh ()
{
[ "`echo ~`" = "${HOME}" -a ${RANDOM:-0} -eq ${RANDOM:-0} ]
}
is_schily_sh ()
{
_sh_shell=`eval 'echo "${.sh.shell}"' 2>/dev/null`
test -n "${_sh_shell}"
_rc=$?
unset _sh_shell
return ${_rc}
}
ISSUN=false
if [ -x /usr/bin/sun ]; then
if sun; then
ISSUN=true
fi
fi
if [ -r /var/log/smail/logfile ] ; then
MAILLOG="/var/log/smail/logfile"
elif [ -r ${LOCAL}/var/log/smail/logfile ] ; then
MAILLOG=${LOCAL}/var/log/smail/logfile
else
MAILLOG="/var/log/smail/logfile"
fi
export MAILLOG
# always use ``$echo'' if any of the other variables are used...
# $nl - print a newline (always required at end of line if desired)
# $n - option to turn off final newline
# $c - escape sequence to turn off final newline
# usage for a prompt is:
# $echo $n "prompt: $c"
# and for a normal line
# $echo "message$nl"
#
# Don't pretend to have print or printf if they are not builtin
#
HAVEPRINT=false
if expr "`type print 2>/dev/null`" : '^print is a shell ' > /dev/null 2>&1 ; then
HAVEPRINT=true
fi
HAVEPRINTF=false
if expr "`type printf 2>/dev/null`" : '^printf is a shell ' > /dev/null 2>&1 ; then
HAVEPRINTF=true
fi
if ${HAVEPRINT} ; then
#
# XXX Ksh "print" is a horrible mess of unusability, but, if it is
# builtin....
#
# XXX in theory "print -R", if available, is a better emulation of BSD
# echo with '-n', but for $echo it doesn't really matter....
#
echo=print
nl='\n'
n='-n'
# XXX in theory '\c' is equivalent of '-n' in all Ksh-compatible shells
c=''
elif ${HAVEPRINTF} ; then
echo=printf
nl='\n'
n=''
c=''
# for fun:
portable_echo ()
{
[ "$1" = -n ] && { shift; FMT="%s"; } || FMT="%s\n"
printf "$FMT" ${1+"$@"}
}
else
# NOTE: Assume if "echo" is builtin that it is OK and do not prefer an
# external "echo" for $echo, even if that is more capable (though that
# is not likely ever true).
#
echo=echo
c=`echo "xyzzy\c" | sed 's/[^c]//g'`
if test -n "${c}" ; then
# BSD echo
nl=''
n='-n'
c=''
else
# (SysVr2 or newer) Bourne Shell echo
# (some may have -n, but ignore that as we know we have '\c')
nl='\n'
n=''
c='\c'
fi
# n.b.: ancient echo (V7) is not supportable for this use!
fi
#
# NOTE: we don't export $echo et al -- they're just in the current shell
in_pathvar_p() {
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: in_pathvar_p pathvar directory [...]" >&2
return 2
fi
eval "case :\$${1}: in *:${2}:*) return 0;; *) return 1;; esac"
}
# another way to do this without "test -d":
#
# PATH=` IFS=: ; unset tpath
# for pe in $candidates
# do cd $pe &> /dev/null && tpath=${tpath+$tpath:}$pe ; done
# echo "$tpath" `
#
#
dirappend ()
{
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: dirappend pathvar directory [...]" >&2
return 2
fi
varname=${1}
shift
while [ $# -gt 0 ] ; do
if [ -d "${1}" ]; then
eval varvalue='${'${varname}'}'
if in_pathvar_p ${varname} "${1}" ; then
:
else
if [ -z "${varvalue}" ]; then
eval ${varname}='"${1}"'
else
eval ${varname}='$'"${varname}"'":${1}"'
fi
fi
fi
shift
done
unset varname varvalue
}
dirprepend ()
{
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: dirprepend pathvar directory [...]" >&2
return 2
fi
varname=${1}
shift
while [ $# -gt 0 ] ; do
if [ -d "${1}" ]; then
eval varvalue='${'${varname}'}'
if in_pathvar_p $varname "${1}" ; then
:
else
if [ -z "${varvalue}" ]; then
eval ${varname}='"${1}"'
else
eval ${varname}='"${1}:"$'"${varname}"
fi
fi
fi
shift
done
unset varname varvalue
}
# XXX there's a slight flaw in this implementation -- it can't remove a
# double-empty trailing element (e.g. p=/path::; dirremove p ''). However two
# consecutive invocations will work to do this, or first do "removedups p".
#
# ToDo: this should probably treat '' as '[.]*' and vice-versa so as to remove
# both implied and implicit current-directory entries at the same time, but
# that's tricky while still preserving the non-forking test using
# in_pathvar_p....
#
dirremove ()
{
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: dirremove pathvar directory [...]" >&2
return 2
fi
varname=${1}
shift
while [ $# -gt 0 ] ; do
if in_pathvar_p ${varname} "${1}" ; then
eval ${varname}=`eval echo :'$'${varname}: | sed -e "s|:${1}:|:|g" -e "s|^:||" -e "s|:\$||"`
fi
shift
done
unset varname
}
# Another way...
#
pathvarrm ()
{
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: pathvarrm pathvar directory [...]" >&2
return 2
fi
IFS=':'
pathvar=${1}
rmpath=${2}
#
# N.B.: Instead of "set", ast-ksh/ksh93 and bash can use:
#
# IFS=:
# for dir in ${!pathvar}; do
#
set -- `IFS=":"; eval echo '$'${pathvar}`
for dir in ${1+"$@"}; do
if [ "${dir}" != "${opath}" ] ; then
newpath=${newpath:+${newpath}:}${dir}
fi
done
eval ${pathvar}="${newpath}"
}
removedups ()
{
if [ $# -lt 1 -o -z "${1}" ] ; then
echo "Usage: removedups pathvar" >&2
return 2
fi
varname=${1}
eval varvalue='${'${varname}'}'
nvalue=`echo "${varvalue}" | awk -v RS=: -v ORS=: '
BEGIN {
A[""] = 0;
}
{
# removing the newline is necessary in case the last
# element is a duplicate
#
sub(/\n$/, "");
if (! A[$0]) {
A[$0]=1;
printf("%s%s", ((NR == 1) ? "" : ORS ), $0);
}
}'
`
# XXX 'nbase' awk version 20121220 from Joyent packages core-dumps on macos
rc=$?
if [ ${rc} -ne 0 ]; then
echo "removedups: awk failed: $rc" >&2
unset varname varvalue nvalue rc
return 1
fi
if [ -n "${nvalue}" ]; then
eval ${varname}="${nvalue}"
else
echo "removedups: empty result!" >&2
unset varname varvalue nvalue rc
return 1
fi
unset varname varvalue nvalue rc
}
# ToDo: consider rewriting the above so they accept '-F sepchar' and then the
# following can be eliminated? (but what about the check for .../fonts.dir?
# maybe another option '-f file' to say the directory must contain a file named
# "file"?)
in_fppathvar_p() {
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: in_fppathvar_p fppathvar directory [...]" >&2
return 2
fi
eval "case ,\$${1}, in *,${2},*) return 0;; *) return 1;; esac"
}
fpappend ()
{
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: fpappend fppathvar directory [...]" >&2
return 2
fi
varname=${1}
shift
while [ $# -gt 0 ] ; do
if [ -d "${1}" -a -r "${1}"/fonts.dir -a -r "${1}"/fonts.scale ]; then
eval varvalue='${'${varname}'}'
if in_fppathvar_p ${varname} "${1}" ; then
:
else
if [ -z "${varvalue}" ]; then
eval ${varname}='"${1}"'
else
eval ${varname}='$'"${varname}"'",${1}"'
fi
fi
fi
shift
done
unset varname varvalue
}
fpprepend ()
{
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: fpprepend fppathvar directory [...]" >&2
return 2
fi
varname=${1}
shift
while [ $# -gt 0 ] ; do
if [ -d "${1}" -a -r "${1}"/fonts.dir -a -r "${1}"/fonts.scale ]; then
eval varvalue='${'${varname}'}'
if in_fppathvar_p ${varname} "${1}" ; then
:
else
if [ -z "${varvalue}" ]; then
eval ${varname}='"${1}"'
else
eval ${varname}='"${1},"$'"${varname}"
fi
fi
fi
shift
done
unset varname varvalue
}
fpremove ()
{
if [ $# -le 1 -o -z "${1}" ] ; then
echo "Usage: fpremove fppathvar directory [...]" >&2
return 2
fi
varname=${1}
shift
while [ $# -gt 0 ] ; do
if in_fppathvar_p ${varname} "${1}" ; then
eval ${varname}=`eval echo ,'$'${varname}, | sed -e "s|,${1},|,|g" -e "s|^,||" -e "s|,\$||"`
fi
shift
done
unset varname
}
pkgfirst ()
{
dirremove PATH "$PKG/bin" "$PKG/sbin"
dirprepend PATH "$PKG/bin"
}
pkglast ()
{
dirremove PATH "$PKG/bin" "$PKG/sbin"
dirappend PATH "$PKG/bin"
}
localfirst ()
{
dirremove PATH "$LOCAL/bin" "$LOCAL/sbin"
dirprepend PATH "$LOCAL/bin"
}
locallast ()
{
dirremove PATH "$LOCAL/bin" "$LOCAL/sbin"
dirappend PATH "$LOCAL/bin"
}
contribfirst ()
{
dirremove PATH "$CONTRIB/bin" "$CONTRIB/sbin"
dirprepend PATH "$CONTRIB/bin"
}
contriblast ()
{
dirremove PATH "$CONTRIB/bin" "$CONTRIB/sbin"
dirappend PATH "$CONTRIB/bin"
}
slashoptfirst ()
{
dirremove PATH "$SLASHOPT/bin" "$SLASHOPT/sbin"
dirprepend PATH "$SLASHOPT/bin"
}
slashoptlast ()
{
dirremove PATH "$SLASHOPT/bin" "$SLASHOPT/sbin"
dirappend PATH "$SLASHOPT/bin"
}
gnufirst ()
{
dirremove PATH "$GNU/bin" "$GNU/sbin"
dirprepend PATH "$GNU/bin" "$GNU/sbin"
}
gnulast ()
{
dirremove PATH "$GNU/bin" "$GNU/sbin"
dirappend PATH "$GNU/bin" "$GNU/sbin"
}
xpgfirst ()
{
dirremove PATH /usr/xpg6/bin /usr/xpg4/bin ${PKG}/heirloom-xpg4/bin
dirprepend PATH /usr/xpg6/bin /usr/xpg4/bin ${PKG}/heirloom-xpg4/bin
}
xpglast ()
{
dirremove PATH /usr/xpg6/bin /usr/xpg4/bin ${PKG}/heirloom-xpg4/bin
dirappend PATH /usr/xpg6/bin /usr/xpg4/bin ${PKG}/heirloom-xpg4/bin
}
# every truly POSIX-compliant shell implements the built-in command 'local', but
# then there's ksh (even #93!)
#
# AT&T Ksh is (still, as of ksh93u+) the only shell I know of which has
# functions and which has a 'typset' command but which does not have a 'local'
# command to declare function-scope variables, nor a "built-in" alias for it
# either. Sigh.
#
# xxx strictly speaking this should probably always be a function that would
# treat any use of options as a usage error
defun_local="local () { :; }"
if is_attksh; then # if type local >/dev/null 2>&1; then
# Ksh93:
#
# typeset vname[=value]
#
# When invoked inside a function defined with the
# function name syntax, a new instance of the variable
# 'vname' is created, and the variable's value and type
# are restored when the function completes.
#
# XXX Sadly this does not actually work for functions defined using the
# "func() {body}", and that's the only portable way to define a
# function!
#
# XXX so don't shadow globals in "fname(){}" functions!!!
#
# Note that in AT&T Ksh (and others?) aliases are resolved when a
# function definition is first read in, so set this early!
#
# I used to like AT&T Ksh -- now I'm glad it's only an inspiration...
#
alias local='typeset'
else
# for Bourne (heirloom) sh.
if type local >/dev/null 2>&1; then
:
else
# for Bourne (heirloom) sh, pbosh, etc.(?)
#
# This definition of "local" won't actually do anything useful,
# but it also won't cause a syntax error either.
#
# xxx even though this is in an 'if' that souldn't be executed
# by it, some versions of 'dash', aka "Debian Almquist shell"
# (which started life as NetBSD sh) will barf and abort just
# seeing this function definition for 'local' here!!!
# (dash-0.5.5.1 is OK, dash-0.5.9.1 through 0.5.10.2 are STUPID)
#
# even worse it is almost impossible to detect dash
# programatically. However luckily we can hide the definition
# in a variable value....
#
eval ${defun_local}
fi
fi
unset defun_local
#
# Setup for ls(1) shortcuts.
#
LS_O=
LS_COLOUR=
HAVECOLORLS=false
# XXX perhaps we should just do the following in .profile:
# export CLICOLOR="defined"
if type colorls >/dev/null 2>&1; then
LS_O=
LS=`type colorls`
LS=`expr "$LS" : '^[^/]*\(/[^ )]*\)'`
HAVECOLORLS=true
LS_COLOUR="-G"
fi
if type chflags >/dev/null 2>&1; then
case `uname -s` in
Darwin*)
LS_O="O"
HAVECOLORLS=true # by coincidence....
LS_COLOUR="-G"
LS="/bin/ls"
;;
FreeBSD)
LS_O="o"
HAVECOLORLS=true # by coincidence....
LS_COLOUR="-G"
LS="/bin/ls"
;;
*)
LS_O="o" # assume for the BSD best!
;;
esac
fi
if [ -z "${LS}" ]; then
LS=`type ls`
LS=`expr "$LS" : '^[^/]*\(/[^ )]*\)'`
if ${LS} --version >/dev/null 2>&1; then
# likely GNU ls....
LS_O=""
HAVECOLORLS=true
LS_COLOUR="--color=auto"
elif [ ${LS} = "/usr/bin/ls" ]; then
# likely SysV-ish or Solaris?
LS_O=""
LS_COLOUR=""
else
# hmmm.... what could it be?
LS_O=""
LS_COLOUR=""
fi
fi
if [ -n "$TERM" -a "X$TERM" != "Xdumb" ] && $HAVETPUT && $HAVECOLORLS; then
# xxx colorls(1) is quite "dumb" -- it apparently just uses ANSI color
# escape sequences directly without consulting $TERM
if [ "`tput colors`" -gt 0 ]; then
LS="${LS} ${LS_COLOUR}"
fi
fi
if type mktable >/dev/null 2>&1; then
MKTABLE="mktable"
else
# a little ditty to throw away comment lines....
# ToDo: allow for changing of the comment character!
# ToDo: should this remove trailing comments too? (-e '/#.*$//')
mktable ()
{
sed \
-e '/^[ ]*#/d' \
-e '/^[ ]*$/d' \
${1+"$@"}
}
fi
# all machines without 'head' had a shell with functions... well, all that matter
#
if type head >/dev/null 2>&1; then
: # have the real thing....
else
head ()
{
N=10
if [ $# -ge 1 ] ; then
case "$1" in
-[0-9]*)
N=`expr x"$1" : '^x-\([0-9]*\)$'`
shift
;;
-n)
shift
N=`expr x"$1" : '^x\([0-9]*\)$'`
shift
;;
-*)
echo "Usage: head [-N] [-n lines] [[file] ...]" 1>&2
return 2
esac
fi
sed ${N}q ${1+"$@"}
unset N
}
fi
: ${CENTRAL_USER:="woods"}
: ${CENTRAL_SERVER:="central.avoncote.ca"} # FQDN for outbound connections
: ${HOME_SERVER:="more"}
#
# dotfiles2git - meant as an internal helper
#
dotfiles2git ()
{
( cd $HOME
git-sccsimport --move-date=2010/11/5T00:00:00 --move-offset=3 --expand-kw --maildomain=robohack.ca --git-dir="$HOME/work/home" --dirs SCCS && cd $HOME/work/home && git push --mirror
if [ $? -ne 0 ]; then
echo "dotfiles2git: check for errors above!" 1>&2
#
# Quite possibly some aspect of some change that had
# already been imported was accidentally changed causing
# a fork in the change history. One likely possibility
# is some aspect of an SCCS file, e.g. a metadata value
# which appears in the expanded "@(#)" header, was
# modified, e.g. the module type.
#
# In this case there are two choices -- (1) undo the
# change; or (2) rewrite the git history.
#
# For this particular repository the second option is
# probably OK. Do the following:
#
# git reset --hard $(git log HEAD^..HEAD | awk '$1 == "commit" {print $2}')
# git push --force --mirror
#
return 1
fi
)
}
archivedotfiles ()
{
( cd $HOME
# update the live files on the central server
#
rsync -v -lptHS --progress --stats --files-from=$HOME/dotfiles.list $HOME ${CENTRAL_USER}@${CENTRAL_SERVER}:.
# remove g-files on $CENTRAL_SERVER that have been removed in SCCS
#
removed=`vremoved`
if [ -n "${removed}" ]; then
ssh ${CENTRAL_USER}@${CENTRAL_SERVER} rm -f ${removed}
fi
unset removed
# update dotfiles.tar.gz
#
pax -wzf ${HOME}/dotfiles.tar.gz < ${HOME}/dotfiles.list
ssh ${CENTRAL_USER}@${CENTRAL_SERVER} rm -f dotfiles.list # is likely unwriteable
scp ${HOME}/dotfiles.list ${HOME}/dotfiles.tar.gz ${CENTRAL_USER}@${CENTRAL_SERVER}:.
# publish via HTTP too -- xxx this is sometimes a bit wasteful, but
# simplest...
#
cd $HOME/public_html
./publish
)
}
#
# publishdotfiles - re-import to git, push, and publish to $CENTRAL_SERVER
#
# meant for incremental updates without creating a new release and can be used
# without SCCS tools (but of course requires git-sccsimport)
#
publishdotfiles ()
{
case ${HOSTNAME} in
${CENTRAL_SERVER%%.*}*) # assume there is only one by this name!
echo "publishdotfiles: not useful when run on server host!" 1>&2
return 2
;;
${HOME_SERVER}*)
if [ -z "${HOME}" ]; then
echo "publishdotfiles: HOME is unset or empty!" 1>&2
return 2
fi
( cd ${HOME}; ${SCCS:-"sccs"} info )
dotfiles2git || {
return 1
}
# update the archive files on the central server, etc.
#
archivedotfiles
;;
*)
echo "publishdotfiles: not useful when run on client host!" 1>&2
return 2
;;
esac
}
#
# releasedotfiles - publish dotfiles as a release and start a new one
#
releasedotfiles ()
{
case ${HOSTNAME} in
${CENTRAL_SERVER})
echo "releasedotfiles: not useful when run on server host!" >&2
return 1
;;
more*)
( cd ${HOME}
vcheck || {
echo "releasedotfiles: files still being edited!" >&2
return 1
}
vverify || {
return 1
}
# create archives from the "next", i.e. current level, release
#
# function is from ~/.shrc
#
publishdotfiles
# update the FTP files for the current release
#
# (note I had never put these in /work/release.d, so no need to
# do so now)
#
REL=`vcurrentrel`
scp dotfiles.list ${CENTRAL_USER}@${CENTRAL_SERVER}:\~ftp/pub/local/dotfiles-v$REL.list
scp dotfiles.tar.gz ${CENTRAL_USER}@${CENTRAL_SERVER}:\~ftp/pub/local/dotfiles-v$REL.tar.gz
ssh ${CENTRAL_USER}@${CENTRAL_SERVER} ln -fhs dotfiles-v${REL}.list \~ftp/pub/local/dotfiles.list
ssh ${CENTRAL_USER}@${CENTRAL_SERVER} ln -fhs dotfiles-v${REL}.tar.gz \~ftp/pub/local/dotfiles.tar.gz
unset REL
# finally close off the release and get ready for the next one!
#
vnewrel
if [ $? -ne 0 ]; then
echo "releasedotfiles: check for errors above!" >&2
return 1
fi
)
;;
*)
echo "releasedotfiles: this host needs dotfiles updates!" >&2
return 1
;;
esac
}
pushdotfiles ()
{
# XXX maybe _e_opt should default to "-e rsh" since that's what _rsh does (because RSH)? or just default to SSH???
_e_opt=""
_user=${LOGNAME}