-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEXAMPLES
749 lines (652 loc) · 16.3 KB
/
EXAMPLES
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
There is no repository for useful rc code snippets as yet, so
I'm including a (short) file in the distribution with some
helpful/intriguing pieces of rc code.
A sample .rcrc
--------------
Here is the .rcrc I use on archone:
umask 022
path=(/bin /usr/bin /usr/ucb)
ht=`/usr/arch/bin/hosttype
h=$home
history=$h/.history
bin=$h/bin/$ht
lib=$h/lib/$ht
sh=$h/bin/sh
include=$h/lib/include
switch ($ht) {
case sun*
OBERON='. '$h/other/oberon
p=/usr/ucb
compiler='gcc -Wall -O -g'
MANPATH=$h/man:/usr/arch/man:/usr/man
if (! ~ $TERM ()) {
stty dec
/usr/arch/bin/msgs -q
}
case next
p=(/usr/ucb /usr/bin /NextApps)
compiler='cc -Wall -O -g -DNODIRENT'
MANPATH=$h/man:/usr/arch/man:/usr/man
if (! ~ $TERM ())
stty dec
case sgi
p=(/usr/ucb /usr/sbin /usr/bin)
compiler='gcc -Wall -O -g -DNOSIGCLD'
MANPATH=$h/man:/usr/arch/man:/usr/catman
if (!{~ $TERM () || ~ $TERM *iris*})
stty line 1 intr '' erase '' kill ''
case *
echo .rcrc not configured for this machine
}
path=(. $sh $bin /usr/arch/bin $p /bin /usr/bin/X11 /etc /usr/etc)
cdpath=(. .. $h $h/src $h/misc $h/other $h/adm)
RNINIT=-d$h' -t -M -2400-h -2400+hfrom'; DOTDIR=$h/misc/news
PRINTER=lw
fn s {
echo $status
}
fn cd {
builtin cd $1 && \
switch ($1) {
case ()
dir=$home
case *
dir=()
}
}
fn pwd {
if (~ $dir ())
dir=`/bin/pwd
echo $dir
}
fn x {
if (~ `tty /dev/console)
clear_colormap
clear
exit
}
fn p {
if (~ $history ()) {
echo '$history not set' >[1=2]
return 1
}
if (! ~ $#* 0 1 2) {
echo usage: $0 '[egrep pattern] [sed command]' >[1=2]
return 1
}
command=`{
egrep -v '^[ ]*p([ ]+|$)' $history | switch ($#*) {
case 0
cat
case 1
egrep $1
case 2
egrep $1 | sed $2
} | tail -1
}
echo $command
eval $command
}
if (~ $TERM dialup network) {
TERM=vt100
biff y
}
A front-end to NeXT's "openfile"
--------------------------------
Named after the sam "B" command for opening a file, this script was written
by Paul Haahr. (Assumes the "pick" command from Kernighan and Pike is also
in your path.)
#!/bin/rc
if (~ $#* 0)
exec openfile
create = ()
files = ()
for (i in $*)
if (test -f $i) {
files = ($files $i)
} else {
create = ($create $i)
}
create = `{ pick $create }
files = ($files $create)
for (i in $create)
> $i
if (! ~ $#files 0)
openfile $files
A read function
---------------
Unlike sh, rc doesn't have a read. This clever alternative returns an
exit status as well as fetch a variable. Use as
read foo
to set $foo to a single line from the terminal.
(due to John Mackin <[email protected]>)
fn read {
x=() {
x = `` ($nl) { awk '{print; print 0; exit}' ^ $nl ^ \
'END {print 1; print 1}' }
$1 = $x(1)
return $x(2)
}
}
From cs.wisc.edu!dws Fri Aug 2 18:16:14 1991
#-------
# ls front end
#-------
fn ls \
{
test -t 1 && * = (-FCb $*)
builtin ls $*
}
#-------
# nl - holds a newline, useful in certain command substitutions
#-------
nl='
'
#-------
# show - tell me about a name
#
# Runs possibly dangerous things through cat -v in order to protect
# me from the effects of control characters I might have in the
# environment.
#-------
fn show \
{
* = `` $nl {whatis -- $*}
for(itis)
{
switch($^itis)
{
case 'fn '* ; echo $itis | cat -v -t
case builtin* ; echo $itis
case /* ; file $itis; ls -ld $itis
case *'='* ; echo $itis | cat -v -t
case * ; echo $itis: UNKNOWN: update show
}
}
itis = ()
}
#-------
# Tell me automatically when a command has a nonzero status.
#-------
fn prompt \
{
Status = $status
~ $Status 0 || echo '[status '$Status']'
}
#-------
# chop - echo the given list, less its final member
#
# e.g. chop (a b c) -> (a b)
#-------
fn chop {
~ $#* 0 1 && return 0
ans = '' { # local variable
ans = ()
while(! ~ $#* 1)
{
ans = ($ans $1)
shift
}
echo $ans
}
}
From [email protected] Thu May 30 08:49:51 1991
# cd.rc --- souped up version of cd
# this is designed to emulate the fancy version of cd in ksh,
# so if you're a purist, feel free to gag
_cwd=$home
_oldcwd=$home
fn cd {
if (~ $#* 0) {
if (~ $_cwd $home) { # do nothing
} else {
builtin cd && { _oldcwd=$_cwd ; _cwd=$home }
}
} else if (~ $#* 1) {
if (~ $1 -) {
_t=$_cwd
builtin cd $_oldcwd && {
_cwd=$_oldcwd
_oldcwd=$_t
echo $_cwd
}
_t=()
} else {
# if a cd happens through the cdpath, rc echos
# the directory on its own. all we have to do
# is track where we end up
_dopwd = 1
{ ~ $1 /* } && _dopwd = 0 # absolute path
builtin cd $1 && {
_oldcwd=$_cwd
_cwd=$1
{ ~ $_dopwd 1 } && _cwd=`/bin/pwd
}
_dopwd=()
}
} else if (~ $#* 2) {
_t=`{ echo $_cwd | sed 's<'$1'<'$2'<' }
builtin cd $_t && {
_oldcwd=$_cwd
_cwd=$_t
echo $_cwd
}
_t=()
} else {
echo cd: takes 0, 1, or 2 arguments >[1=2]
builtin cd $1 && { _oldcwd=$_cwd ; _cwd=`/bin/pwd ; echo $_cwd }
}
}
fn pwd { echo $_cwd }
From vlsi.cs.caltech.edu!drazen Tue Jan 21 16:03:14 1992
# A kill builtin.
#ifdef B_KILL
#include <ctype.h>
static void b_kill(char **av)
{
int signal = SIGTERM;
int n = 1;
pid_t pid;
boolean res;
if (!av[1]) {
set(TRUE);
return;
}
#undef STRCMP
#define STRCMP strcmp
if ( '-' == av[1][0]) {
char *p = 1+av[1];
if (0 == strcmp(av[1], "-l")){
int r; const int nsig = NUMOFSIGNALS;
const int C = 4, R = 1 + (int)((nsig-2)/C);
for (r=1; r<=R; r++){
int j;
for (j=r; j<nsig; j+=R){
fprint(1, "%s%d. %s\t", j<10?" ":"", j, signals[j][0]);
}
fprint(1,"\n");
}
set(TRUE);
return;
}
n++;
if ( (signal=a2u(p)) < 0){
int i;
for (i = 1; i < NUMOFSIGNALS; i++){
char UPPER[31], *u=UPPER, *q;
for (q=signals[i][0]; *q; q++, u++) *u = toupper(*q);
*u = '\0';
if (*signals[i][0] &&
(!STRCMP(signals[i][0], p) || !STRCMP(3+signals[i][0],p)
|| !STRCMP(UPPER, p) || !STRCMP(3+UPPER, p) ) )
{
signal = i;
break;
}
}
if (signal < 0){
fprint(2,"kill: bad signal %s\n", av[1]);
set(FALSE);
return;
}
}
}
#undef STRCMP
for (res=TRUE; av[n]; n++){
if( (pid = (pid_t) a2u(av[n])) < 0){
fprint(2, "kill: bad process number %s\n", av[n]);
res = FALSE;
continue;
}
if (kill(pid,signal)){
uerror("kill");
res = FALSE;
continue;
}
}
set(res);
}
#endif
From acc.stolaf.edu!quanstro Thu Apr 2 02:51:10 1992
Received: from thor.acc.stolaf.edu ([130.71.192.1]) by archone.tamu.edu with SMTP id <45339>; Thu, 2 Apr 1992 02:50:56 -0600
Received: by thor.acc.stolaf.edu; Thu, 2 Apr 92 02:49:31 -0600
Date: Thu, 2 Apr 1992 02:49:31 -0600
From: [email protected]
Message-Id: <[email protected]>
Subject: EXAMPLES in 1.4beta
Status: RO
I have a little bit of code which might be a little more general than
the souped-up version that is already there. Here it is, if you are
interested.
# directory functions ###################################################
fn pwd { echo $PWD; }
fn pushd {
dirs = ($PWD $dirs);
builtin cd $*;
PWD = `{builtin pwd};
}
fn popd {
switch ($#*)
{
case 0
;
case 1
echo 'popd: argument '^$1^' ignored.' >[1=2];
case *
echo 'popd: usage: popd [n].';
}
if (! ~ $dirs ())
{
builtin cd $dirs(1);
PWD = $dirs(1);
echo $PWD;
* = $dirs;
shift
dirs = $*;
}
}
fn cd {
~ $* () && * = $home;
!~ $#* 1 && echo 'cd: too many arguments' >[1=2] && return 1;
if (test -r $* ) {
pushd $*;
} else {
echo cd: $* does not exist. >[1=2]
return 1;
}
}
fn back { popd $*; }
fn Back {
cd $home;
PWD = $home;
dirs = ();
}
fn dirs {
echo $dirs;
}
PWD = `{builtin pwd} ; dirs = $PWD # kickstart
From acc.stolaf.edu!quanstro Thu Apr 2 02:53:40 1992
Received: from thor.acc.stolaf.edu ([130.71.192.1]) by archone.tamu.edu with SMTP id <45339>; Thu, 2 Apr 1992 02:53:38 -0600
Received: by thor.acc.stolaf.edu; Thu, 2 Apr 92 02:51:46 -0600
Date: Thu, 2 Apr 1992 02:51:46 -0600
From: [email protected]
Message-Id: <[email protected]>
Subject: EXAMPLES
Status: RO
Little yp hack which act's like ~ w/o syntatic sugar (for those who do
not have the luxury of seting up symbolic links to all user's homes
# user function #########################################################
fn u user {
info = ()
info = `` (':') {ypmatch $1 passwd >[2] /dev/null }
if (~ $info ()) {
echo user $1 unknown >[1=2];
return 1;
} else {
echo $info(6)
if (~ $0 user)
cd $info(6)
}
}
From stolaf.edu!quanstro Sun Apr 5 04:53:34 1992
Date: Sun, 5 Apr 1992 04:53:08 -0500
From: [email protected] (Erik Quanstrom)
Subject: man written in rc
Status: RO
I whipped this up because the NeXTs here insist on using MANPAGER
instead of PAGER and otherwise being obnoxious . . .
Anyway ... I hope you approve
#!/bin/rc
#########################################################################
# file: man #
# #
# object: display man pages #
# #
# bugs: * slow #
# * does not know about fmt files #
# #
# Erik Quanstrom #
# 11. Februar 1992 #
#########################################################################
PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:$PATH ;
TROFF = (nroff -hq -Tcrt);
macros=an;
sections=(cat1 cat2 cat3 cat4 cat5 cat6 cat7 cat8 catl man1 man2 man3 man4 \
man5 man6 man7 man8 manl)
$nl='
'
fn sigint sighup sigquit sigalrm sigterm { rm -f $Bat; exit 2;}
fn usage {
echo usage: $0 [-] [-t] [-M path] [-T macros] [[section] title] ...>[1=2];
exit 1;
}
n=();
fn shiftn {
n=($n 1)
}
~ $PAGER () && test -t 1 && PAGER=more; #default pager
while (~ $1 -*)
{
switch ($1)
{
case -
if (~ $PAGER troff)
echo bad combination of flags >[1=2] && usage;
PAGER=cat;
case -t
~ TROFF () && TROFF = (troff -t);
~ TCAT () && PAGER=(lpr -t);
case -M
shift;
~ $1 () && usage;
MANPATH=$1;
case -T
shift;
~ $1 () && usage;
macros=$1;
case -k
fflag=(); kflag=1;
shift;
break;
case -f
# locate related too filenames
kflag=(); fflag=1;
shift;
break;
case -*
echo bad flag '`'^$1^'''' >[1=2];
usage;
}
shift;
}
if (!~ $#* 1) {
~ $1 [l1-8] && { sname=$1 ; sections=(cat man)^$1 ; shift }
#hack for sun-style man pages
~ $1 [l1-8]? && { sname=$1 ; sections=(cat man)^`{echo $1| cut -c1} ; shift }
}
if (~ 1 $fflag $kflag) {
dirlist=();
for (d in ``(:$nl) {echo $MANPATH})
test -s $d^/whatis && dirlist=($dirlist $d^/whatis);
~ $1 () && usage;
if (~ $fflag 1) {
while (!~ $1 ()) {
cmd=`{echo $1 | sed 's/^.*\///g'}
egrep -h '^'^$cmd' ' $dirlist;
shift;
}
} else {
while (!~ $1 ()) {
grep -h $1 $dirlist;
shift;
}
}
exit 0;
}
s=0;
while (!~ $1 ()) {
for (dir in ``(:$nl) {echo $MANPATH}) {
filelist=($filelist `{echo $dir/^$sections^/$1^.* |\
tr ' ' '\12' | grep -v '*'})
# coment this out if you don't care about speed, but
# would rather find all the pages.
~ $filelist () || break;
}
if (~ $filelist ()) {
if (~ $#sections 2) {
echo no entry for $1 in section '`'$sname'''' of the manual >[1=2];
} else {
echo no entry for '`'$1'''' found. >[1=2];
}
s=1;
} else {
echo $filelist '(' $#filelist ')' >[1=2];
for (file in $filelist) {
if (~ $file */cat[1-8l]/*) {
Cat = ($Cat $file);
} else {
# search for dups
dont=()
for (x in $Cat) {
if (~ `{echo $x | sed 's/\/[mc]a[nt][1-8l]//'} \
`{echo $file | sed 's/\/[mc]a[nt][1-8l]//'}) {
dont=1;
break;
}
}
if (~ $dont ()) {
cd `{echo $file | sed 's/man[1-8].*//'}
echo -n Formatting ...
$TROFF -m^$macros $file > /tmp/man^$pid^$#n && \
Bat = ($Bat /tmp/man^$pid^$#n)
shiftn;
echo ' 'done.
}
}
}
}
shift;
}
{ !~ () $Cat || !~ () $Bat } && $PAGER $Cat $Bat;
rm -f $Bat;
~ $s () || exit $s;
exit 0;
From osf.org!rsalz Thu Apr 23 16:22:32 1992
Date: Thu, 23 Apr 1992 16:22:07 -0500
From: [email protected]
Subject: One for your EXAMPLES file
Status: RO
Use
trimhist [-#lines]
trims your history file back; useful for folks with disk quota's :-)
fn trimhist { p1=-100 {
cp $history $history^'~'
~ $#* 1 && p1=$1
tail $p1 <$history^'~' >$history
rm $history^'~'
} }
From Pa.dec.com!uucp Mon Apr 27 12:25:02 1992
Date: Mon, 27 Apr 1992 12:15:18 -0500
From: [email protected]
To: Byron Rakitzis <[email protected]>
Subject: a neat little rc script
what you have to know to understand this:
$md for me is usually obj.$machine
my mkfiles build *.o, *.a, and the a.outs in $md
this is my acc script, which i use for compiling adobe routines
---
#! /user/haahr/bin/next/rc
cc = cc
proto = '-DPROTOTYPES=1'
switch ($md) {
case noproto.$machine; proto = '-DPROTOTYPES=0'
case gprof.$machine; cc = ($cc -pg)
case prof.$machine; cc = ($cc -p)
case lcomp.$machine; cc = lcomp
}
exec $cc $proto '-DPACKAGE_SPECS="package.h"' '-DISP=isp_mc68020' '-DOS=os_mach' $*
From rc-owner Tue May 12 14:54:10 1992
Received: from postman.osf.org ([130.105.1.152]) by archone.tamu.edu with SMTP id <45337>; Tue, 12 May 1992 14:38:16 -0500
Received: from earth.osf.org by postman.osf.org (5.64+/OSF 1.0)
id AA14480; Tue, 12 May 92 13:25:03 -0400
Received: by earth.osf.org (5.64/4.7) id AA03499; Tue, 12 May 92 13:25:02 -0400
Date: Tue, 12 May 1992 12:25:02 -0500
From: [email protected]
Message-Id: <[email protected]>
Subject: Useful function
Status: R
It looks like line noise, but it turns things like
/home/rsalz/foo/bar
into
~/foo/bar
Useful for when you put your current directory up in your icon title.
By duplicating the $home section you can make things like
/project/dce/build/dce1.0.1/src/rpc
become
$MYBT/src/rpc
## If a pathname starts with $home, turn $home into ~. Uses all built-ins.
fn tildepath { p1=() i=() {
p1=$1
switch ($p1) {
case $home $home/*
# Split arg into components
*=`` (/) { echo -n $p1 }
# Shift down by number of components in $home
for (i in `` (/) { echo -n $home } ) shift
# Glue back together
p1='~'
for (i) p1=$p1 ^ '/' ^ $i
echo $p1
case *
echo $p1
}
return 0
} }
From osf.org!rsalz Tue May 12 15:47:12 1992
Received: from postman.osf.org ([130.105.1.152]) by archone.tamu.edu with SMTP id <45316>; Tue, 12 May 1992 15:47:06 -0500
Received: from earth.osf.org by postman.osf.org (5.64+/OSF 1.0)
id AA21070; Tue, 12 May 92 16:46:58 -0400
Received: by earth.osf.org (5.64/4.7) id AA09396; Tue, 12 May 92 16:46:56 -0400
Date: Tue, 12 May 1992 15:46:56 -0500
From: [email protected]
Message-Id: <[email protected]>
Subject: Re: Useful function
Status: R
>wow. thanks, i'll add it to EXAMPLES.
Glad you like. Getting something added to EXAMPLES has been a goal of mine...
I've been thinking, a bit, about a more general way of doing it. I want
a "prefix-sub" function, like this
prefix $some_path var1 var2 var3 var4 var5
That would take some_path and replace any leading $var1 (etc) values
with the variable name. Return on the first match.
Hmm -- come to think of it, that's very easy to do:
# Use pathprefix filename var1 var2 var3
# returns filename, with leading prefixes (in $var1...) turned into the
# string $var1...
fn pathprefix { p1=() i=() j=() {
p1=$1 ; shift
for (i) {
~ $p1 $$i $$i^/* && {
*=`` (/) { echo -n $p1 }
for (j in `` (/) { echo -n $$i } ) shift
p1='$'^$i
for (j) p1=$p1 ^ '/' ^ $j
echo $p1
return 0
}
}
echo $p1
return 0
} }
home=/user/users/rsalz
z=/usr/users
pathprefix /usr/users/rsalz home usr # --> $home
pathprefix /usr/users/rsalz z # --> $z/rsalz
pathprefix /usr/users/rsalz/foo z home # --> $z/rsalz/foo
pathprefix /usr/users/rsalz/foo home # --> $home/foo