rc

[fork] interactive rc shell
git clone https://hhvn.uk/rc
git clone git://hhvn.uk/rc
Log | Files | Refs | README | LICENSE

EXAMPLES (16075B)


      1 There is no repository for useful rc code snippets as yet, so
      2 I'm including a (short) file in the distribution with some
      3 helpful/intriguing pieces of rc code.
      4 
      5 A sample .rcrc
      6 --------------
      7 Here is the .rcrc I use on archone:
      8 
      9 umask 022 
     10 path=(/bin /usr/bin /usr/ucb)
     11 ht=`/usr/arch/bin/hosttype
     12 h=$home
     13 history=$h/.history
     14 bin=$h/bin/$ht
     15 lib=$h/lib/$ht
     16 sh=$h/bin/sh
     17 include=$h/lib/include
     18 
     19 switch ($ht) {
     20 case sun*
     21 	OBERON='. '$h/other/oberon
     22 	p=/usr/ucb
     23 	compiler='gcc -Wall -O -g'
     24 	MANPATH=$h/man:/usr/arch/man:/usr/man
     25 	if (! ~ $TERM ()) {
     26 		stty dec
     27 		/usr/arch/bin/msgs -q
     28 	}
     29 case next
     30 	p=(/usr/ucb /usr/bin /NextApps)
     31 	compiler='cc -Wall -O -g -DNODIRENT'
     32 	MANPATH=$h/man:/usr/arch/man:/usr/man
     33 	if (! ~ $TERM ())
     34 		stty dec
     35 case sgi
     36 	p=(/usr/ucb /usr/sbin /usr/bin)
     37 	compiler='gcc -Wall -O -g -DNOSIGCLD'
     38 	MANPATH=$h/man:/usr/arch/man:/usr/catman
     39 	if (!{~ $TERM () || ~ $TERM *iris*})
     40 		stty line 1 intr '' erase '' kill ''
     41 case *
     42 	echo .rcrc not configured for this machine
     43 }
     44 
     45 path=(. $sh $bin /usr/arch/bin $p /bin /usr/bin/X11 /etc /usr/etc)
     46 cdpath=(. .. $h $h/src $h/misc $h/other $h/adm)
     47 RNINIT=-d$h' -t -M -2400-h -2400+hfrom'; DOTDIR=$h/misc/news
     48 PRINTER=lw
     49 
     50 fn s {
     51 	echo $status
     52 }
     53 fn cd {
     54 	builtin cd $1 && \
     55 	switch ($1) {
     56 	case ()
     57 		dir=$home
     58 	case *
     59 		dir=()
     60 	}
     61 }
     62 fn pwd {
     63 	if (~ $dir ())
     64 		dir=`/bin/pwd
     65 	echo $dir
     66 }
     67 fn x {
     68 	if (~ `tty /dev/console)
     69 		clear_colormap
     70 	clear
     71 	exit
     72 }
     73 fn p {
     74 	if (~ $history ()) {
     75 		echo '$history not set' >[1=2]
     76 		return 1
     77 	}
     78 
     79 	if (! ~ $#* 0 1 2) {
     80 		echo usage: $0 '[egrep pattern] [sed command]' >[1=2]
     81 		return 1
     82 	}
     83 
     84 	command=`{
     85 		egrep -v '^[ 	]*p([	 ]+|$)' $history | switch ($#*) {
     86 		case 0
     87 			cat
     88 		case 1
     89 			egrep $1
     90 		case 2
     91 			egrep $1 | sed $2
     92 		} | tail -1
     93 	}
     94 
     95 	echo $command
     96 	eval $command
     97 }
     98 
     99 if (~ $TERM dialup network) {
    100 	TERM=vt100
    101 	biff y
    102 }
    103 
    104 A front-end to NeXT's "openfile"
    105 --------------------------------
    106 
    107 Named after the sam "B" command for opening a file, this script was written
    108 by Paul Haahr. (Assumes the "pick" command from Kernighan and Pike is also
    109 in your path.)
    110 
    111 #!/bin/rc
    112 if (~ $#* 0)
    113 	exec openfile
    114 create = ()
    115 files = ()
    116 for (i in $*)
    117 	if (test -f $i) {
    118 		files = ($files $i)
    119 	} else {
    120 		create = ($create $i)
    121 	}
    122 create = `{ pick $create }
    123 files = ($files $create)
    124 for (i in $create)
    125 	> $i
    126 if (! ~ $#files 0)
    127 	openfile $files
    128 
    129 A read function
    130 ---------------
    131 
    132 Unlike sh, rc doesn't have a read. This clever alternative returns an
    133 exit status as well as fetch a variable. Use as
    134 
    135 	read foo
    136 
    137 to set $foo to a single line from the terminal.
    138 
    139 (due to John Mackin <john@syd.dit.csiro.au>)
    140 
    141 fn read {
    142 	x=() {
    143 		x = `` ($nl) { awk '{print; print 0; exit}' ^ $nl ^ \
    144 				   'END {print 1; print 1}' }
    145 		$1 = $x(1)
    146 		return $x(2)
    147 	}
    148 }
    149 
    150 From cs.wisc.edu!dws Fri Aug  2 18:16:14 1991
    151 
    152 #-------
    153 # ls front end
    154 #-------
    155 fn ls	\
    156 {
    157 	test -t 1 && * = (-FCb $*)
    158 	builtin ls $*
    159 }
    160 #-------
    161 # nl - holds a newline, useful in certain command substitutions
    162 #-------
    163 nl='
    164 '
    165 #-------
    166 # show - tell me about a name
    167 #
    168 # Runs possibly dangerous things through cat -v in order to protect
    169 # me from the effects of control characters I might have in the
    170 # environment.
    171 #-------
    172 fn show	\
    173 {
    174 	* = `` $nl {whatis -- $*}
    175 	for(itis)
    176 	{
    177 		switch($^itis)
    178 		{
    179 		case 'fn '*	; echo $itis | cat -v -t
    180 		case builtin*	; echo $itis
    181 		case /*		; file $itis; ls -ld $itis
    182 		case *'='*	; echo $itis | cat -v -t
    183 		case *		; echo $itis: UNKNOWN: update show
    184 		}
    185 	}
    186 	itis = ()
    187 }
    188 #-------
    189 # Tell me automatically when a command has a nonzero status.
    190 #-------
    191 fn prompt	\
    192 {
    193 	Status = $status
    194 	~ $Status 0 || echo '[status '$Status']'
    195 }
    196 
    197 #-------
    198 # chop - echo the given list, less its final member
    199 #
    200 # e.g. chop (a b c) -> (a b)
    201 #-------
    202 fn chop {
    203 	~ $#* 0 1 && return 0
    204 	ans = '' {	# local variable
    205 		ans = ()
    206 		while(! ~ $#* 1)
    207 		{
    208 			ans = ($ans $1)
    209 			shift
    210 		}
    211 		echo $ans
    212 	}
    213 }
    214 
    215 From arnold@audiofax.com Thu May 30 08:49:51 1991
    216 
    217 # cd.rc --- souped up version of cd
    218 
    219 # this is designed to emulate the fancy version of cd in ksh,
    220 # so if you're a purist, feel free to gag
    221 
    222 _cwd=$home
    223 _oldcwd=$home
    224 
    225 fn cd {
    226 	if (~ $#* 0) {
    227 		if (~ $_cwd $home) {	# do nothing
    228 		} else {
    229 			builtin cd && { _oldcwd=$_cwd ; _cwd=$home }
    230 		}
    231 	} else if (~ $#* 1) {
    232 		if (~ $1 -) {
    233 			_t=$_cwd
    234 			builtin cd $_oldcwd && {
    235 				_cwd=$_oldcwd
    236 				_oldcwd=$_t
    237 				echo $_cwd
    238 			}
    239 			_t=()
    240 		} else {
    241 			# if a cd happens through the cdpath, rc echos
    242 			# the directory on its own.  all we have to do
    243 			# is track where we end up
    244 			_dopwd = 1
    245 			{ ~ $1 /* } && _dopwd = 0	# absolute path
    246 			builtin cd $1 && {
    247 				_oldcwd=$_cwd
    248 				_cwd=$1
    249 				{ ~ $_dopwd 1 } && _cwd=`/bin/pwd
    250 			}
    251 			_dopwd=()
    252 		}
    253 	} else if (~ $#* 2) {
    254 		_t=`{ echo $_cwd | sed 's<'$1'<'$2'<' }
    255 		builtin cd $_t && {
    256 			_oldcwd=$_cwd
    257 			_cwd=$_t
    258 			echo $_cwd
    259 		}
    260 		_t=()
    261 	} else {
    262 		echo cd: takes 0, 1, or 2 arguments >[1=2]
    263 		builtin cd $1 && { _oldcwd=$_cwd ; _cwd=`/bin/pwd ; echo $_cwd }
    264 	}
    265 }
    266 
    267 fn pwd { echo $_cwd }
    268 
    269 From vlsi.cs.caltech.edu!drazen Tue Jan 21 16:03:14 1992
    270 
    271 # A kill builtin.
    272 
    273 #ifdef B_KILL
    274 #include <ctype.h>
    275 static void b_kill(char **av)
    276 {
    277     int signal = SIGTERM;
    278     int n = 1;
    279     pid_t pid;
    280     boolean res;
    281 
    282     if (!av[1]) {
    283 	set(TRUE);
    284 	return;
    285     }
    286 #undef  STRCMP
    287 #define STRCMP strcmp
    288     if ( '-' == av[1][0]) {
    289 	char *p = 1+av[1];
    290 	if (0 == strcmp(av[1], "-l")){
    291 	    int r; const int nsig = NUMOFSIGNALS;
    292 	    const int C = 4, R = 1 + (int)((nsig-2)/C);
    293 	    for (r=1; r<=R; r++){
    294 	      int j;
    295 	      for (j=r; j<nsig; j+=R){
    296 		fprint(1, "%s%d. %s\t", j<10?" ":"", j, signals[j][0]);
    297 	      }
    298 	      fprint(1,"\n");
    299 	    }
    300 	    set(TRUE);
    301 	    return;
    302 	}
    303 	n++;
    304 	if ( (signal=a2u(p)) < 0){
    305 	    int i;
    306 	    for (i = 1; i < NUMOFSIGNALS; i++){
    307 		char UPPER[31], *u=UPPER, *q;
    308 		for (q=signals[i][0]; *q; q++, u++) *u = toupper(*q);
    309 		*u = '\0';
    310 
    311 		if (*signals[i][0] &&
    312 		  (!STRCMP(signals[i][0], p) || !STRCMP(3+signals[i][0],p)
    313 		   || !STRCMP(UPPER, p) || !STRCMP(3+UPPER, p) )  )
    314 		{
    315 		    signal = i;
    316 		    break;
    317 		}
    318 	    }
    319 	    if (signal < 0){
    320 		fprint(2,"kill: bad signal %s\n", av[1]);
    321 		set(FALSE);
    322 		return;
    323 	    }
    324 	}
    325     }
    326 #undef STRCMP
    327 
    328     for (res=TRUE; av[n]; n++){
    329 	if( (pid = (pid_t) a2u(av[n])) < 0){
    330 	    fprint(2, "kill: bad process number %s\n", av[n]);
    331 	    res = FALSE;
    332 	    continue;
    333 	}
    334 	if (kill(pid,signal)){
    335 	    uerror("kill");
    336 	    res = FALSE;
    337 	    continue;
    338 	}
    339     }
    340     set(res);
    341 }
    342 #endif
    343 From acc.stolaf.edu!quanstro Thu Apr  2 02:51:10 1992
    344 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
    345 Received: by thor.acc.stolaf.edu; Thu, 2 Apr 92 02:49:31 -0600
    346 Date: Thu, 2 Apr 1992 02:49:31 -0600
    347 From: quanstro@acc.stolaf.edu
    348 Message-Id: <9204020849.AA26566@thor.acc.stolaf.edu>
    349 To: byron@archone.tamu.edu
    350 Subject: EXAMPLES in 1.4beta
    351 Status: RO
    352 
    353 
    354 I have a little bit of code which might be a little more general than
    355 the souped-up version that is already there. Here it is, if you are
    356 interested. 
    357 
    358 # directory functions ###################################################
    359 fn pwd { echo $PWD; }
    360 
    361 fn pushd {
    362   dirs = ($PWD $dirs);		
    363   builtin cd $*;
    364   PWD = `{builtin pwd};
    365 }
    366 
    367 fn popd {
    368   switch ($#*)
    369     {
    370     case 0
    371       ;
    372     case 1
    373       echo 'popd: argument '^$1^' ignored.' >[1=2];
    374     case *
    375       echo 'popd: usage: popd [n].';
    376     }
    377 
    378   if (! ~ $dirs ()) 
    379     {
    380       builtin cd $dirs(1);
    381       PWD = $dirs(1);
    382       echo $PWD;
    383       * = $dirs;
    384       shift
    385       dirs = $*;
    386     }
    387 }
    388 
    389 fn cd {
    390   ~ $* () && * = $home;
    391   !~ $#* 1 && echo 'cd: too many arguments' >[1=2] && return 1;
    392 
    393   if (test -r $* ) {
    394     pushd $*;
    395   } else {
    396     echo cd: $* does not exist. >[1=2]
    397     return 1;
    398   } 
    399 }
    400 
    401 fn back { popd $*; }
    402 
    403 fn Back { 
    404   cd $home;
    405   PWD = $home;
    406   dirs = ();
    407 }
    408 
    409 fn dirs {
    410   echo $dirs;
    411 }
    412 
    413 PWD = `{builtin pwd} ; dirs = $PWD		# kickstart
    414 
    415 
    416 
    417 
    418 
    419 From acc.stolaf.edu!quanstro Thu Apr  2 02:53:40 1992
    420 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
    421 Received: by thor.acc.stolaf.edu; Thu, 2 Apr 92 02:51:46 -0600
    422 Date: Thu, 2 Apr 1992 02:51:46 -0600
    423 From: quanstro@acc.stolaf.edu
    424 Message-Id: <9204020851.AA26573@thor.acc.stolaf.edu>
    425 To: byron@archone.tamu.edu
    426 Subject: EXAMPLES
    427 Status: RO
    428 
    429 
    430 Little yp hack which act's like ~ w/o syntatic sugar (for those who do
    431 not have the luxury of seting up symbolic links to all user's homes
    432 
    433 # user function #########################################################
    434 fn u user {  
    435   info = ()
    436   info = `` (':') {ypmatch $1 passwd >[2] /dev/null }
    437 
    438   if (~ $info ()) {
    439     echo user $1 unknown >[1=2];
    440     return 1;
    441   } else {
    442     echo $info(6)
    443     if (~ $0 user) 
    444       cd $info(6)
    445   }
    446 }
    447 
    448 
    449 From stolaf.edu!quanstro Sun Apr  5 04:53:34 1992
    450 Date: Sun, 5 Apr 1992 04:53:08 -0500
    451 From: quanstro@stolaf.edu (Erik Quanstrom)
    452 To: byron@archone.tamu.edu
    453 Subject: man written in rc 
    454 Status: RO
    455 
    456 I whipped this up because the NeXTs here insist on using MANPAGER
    457 instead of PAGER and otherwise being obnoxious . . . 
    458 
    459 Anyway ... I hope you approve
    460 
    461 #!/bin/rc
    462 #########################################################################
    463 # file:		man							#
    464 #									#
    465 # object:	display man pages					#
    466 #									#
    467 # bugs:		* slow							#
    468 #		* does not know about fmt files				#
    469 #									#
    470 # Erik Quanstrom							#
    471 # 11. Februar 1992							#
    472 #########################################################################
    473 PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:$PATH ; 
    474 TROFF = (nroff -hq -Tcrt);
    475 macros=an;
    476 sections=(cat1 cat2 cat3 cat4 cat5 cat6 cat7 cat8 catl man1 man2 man3 man4 \
    477 	  man5 man6 man7 man8 manl)
    478 $nl='
    479 '
    480 fn sigint sighup sigquit sigalrm sigterm { rm -f $Bat; exit 2;}
    481   
    482 fn usage {
    483   echo usage: $0 [-] [-t] [-M path] [-T macros] [[section] title] ...>[1=2];
    484   exit 1;
    485 }
    486 
    487 n=();
    488 fn shiftn {
    489   n=($n 1)
    490 }
    491 
    492 ~ $PAGER () && test -t 1 && PAGER=more; #default pager
    493      
    494 while (~ $1 -*)
    495   { 
    496     switch ($1)
    497       {
    498       case -
    499 	if (~ $PAGER troff)
    500 	  echo bad combination of flags >[1=2] && usage;
    501 	PAGER=cat;
    502       case -t
    503 	~ TROFF () && TROFF = (troff -t);
    504 	~ TCAT  () && PAGER=(lpr -t);
    505       case -M
    506 	shift;
    507 	~ $1 () && usage;
    508 	
    509 	MANPATH=$1;
    510       case -T
    511 	shift;
    512 	~ $1 () && usage;
    513 	macros=$1;
    514       case -k
    515 	fflag=(); kflag=1;
    516 	shift;
    517 	break;
    518       case -f
    519 	# locate related too filenames
    520 	kflag=(); fflag=1;
    521 	shift;
    522 	break;
    523       case -*
    524 	echo bad flag '`'^$1^'''' >[1=2];
    525 	usage;
    526       }
    527     shift;
    528   }
    529 
    530 if (!~ $#* 1) {
    531   ~ $1 [l1-8] && { sname=$1 ; sections=(cat man)^$1 ; shift }
    532   #hack for sun-style man pages
    533   ~ $1 [l1-8]? && { sname=$1 ; sections=(cat man)^`{echo $1| cut -c1} ; shift }
    534 }
    535 
    536 if (~ 1 $fflag $kflag) {
    537   dirlist=();
    538   for (d in ``(:$nl) {echo $MANPATH})
    539     test -s $d^/whatis && dirlist=($dirlist $d^/whatis);
    540   
    541   ~ $1 () && usage;
    542   
    543   if (~ $fflag 1) { 
    544     while (!~ $1 ()) {
    545       cmd=`{echo $1 | sed 's/^.*\///g'}
    546       egrep -h '^'^$cmd' ' $dirlist;
    547       shift;
    548     }
    549   } else {
    550     while (!~ $1 ()) {
    551       grep -h $1 $dirlist;
    552       shift;
    553     }
    554   }
    555  exit 0;
    556 }
    557 
    558 s=0;	 
    559 while (!~ $1 ()) {
    560   for (dir in ``(:$nl) {echo $MANPATH}) {
    561     filelist=($filelist `{echo $dir/^$sections^/$1^.* |\
    562 			    tr ' ' '\12' | grep -v '*'})
    563 
    564     # coment this out if you don't care about speed, but 
    565     # would rather find all the pages.
    566     ~ $filelist () || break;
    567   }
    568 
    569   if (~ $filelist ()) {
    570     if (~ $#sections 2) {
    571       echo no entry for $1 in section '`'$sname'''' of the manual >[1=2];
    572     } else {
    573       echo no entry for '`'$1'''' found. >[1=2];
    574     }
    575     s=1;
    576   } else {
    577 
    578     echo $filelist '(' $#filelist ')' >[1=2];
    579 
    580     for (file in $filelist) {
    581       if (~ $file */cat[1-8l]/*) {
    582 	Cat = ($Cat $file);
    583       } else {
    584 	# search for dups
    585 	dont=()
    586 	for (x in $Cat) {
    587 	  if (~ `{echo $x |  sed 's/\/[mc]a[nt][1-8l]//'} \
    588 		`{echo $file |  sed 's/\/[mc]a[nt][1-8l]//'}) {
    589 	    dont=1;
    590 	    break;
    591 	  }
    592 	}
    593 
    594 	if (~ $dont ()) {
    595 	  cd `{echo $file | sed 's/man[1-8].*//'}
    596 	  echo -n Formatting ...
    597 	  $TROFF -m^$macros $file > /tmp/man^$pid^$#n && \
    598 		Bat = ($Bat /tmp/man^$pid^$#n)
    599 
    600 	  shiftn;
    601 	  echo ' 'done.
    602 	}
    603       }
    604     }
    605   } 
    606   shift;
    607 }
    608 
    609 { !~ () $Cat || !~ () $Bat } && $PAGER $Cat $Bat;
    610 
    611 rm -f $Bat;
    612 ~ $s () || exit $s;
    613      
    614 exit 0;
    615 
    616 
    617 
    618 From osf.org!rsalz Thu Apr 23 16:22:32 1992
    619 Date: Thu, 23 Apr 1992 16:22:07 -0500
    620 From: rsalz@osf.org
    621 To: byron@archone.tamu.edu
    622 Subject: One for your EXAMPLES file
    623 Status: RO
    624 
    625 Use
    626 	trimhist [-#lines]
    627 trims your history file back; useful for folks with disk quota's :-)
    628 fn trimhist { p1=-100 {
    629     cp $history $history^'~'
    630     ~ $#* 1 && p1=$1
    631     tail $p1 <$history^'~' >$history
    632     rm $history^'~'
    633 } }
    634 
    635 From Pa.dec.com!uucp Mon Apr 27 12:25:02 1992
    636 Date: Mon, 27 Apr 1992 12:15:18 -0500
    637 From: haahr@adobe.com
    638 To: Byron Rakitzis <byron@archone.tamu.edu>
    639 Subject: a neat little rc script
    640 
    641 what you have to know to understand this:
    642 	$md for me is usually obj.$machine
    643 	my mkfiles build *.o, *.a, and the a.outs in $md
    644 	this is my acc script, which i use for compiling adobe routines
    645 ---
    646 #! /user/haahr/bin/next/rc
    647 
    648 cc = cc
    649 proto = '-DPROTOTYPES=1'
    650 
    651 switch ($md) {
    652 case noproto.$machine;	proto = '-DPROTOTYPES=0'
    653 case gprof.$machine;	cc = ($cc -pg)
    654 case prof.$machine;	cc = ($cc -p)
    655 case lcomp.$machine;	cc = lcomp
    656 }
    657 exec $cc $proto '-DPACKAGE_SPECS="package.h"' '-DISP=isp_mc68020' '-DOS=os_mach' $*
    658 
    659 From rc-owner Tue May 12 14:54:10 1992
    660 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
    661 Received: from earth.osf.org by postman.osf.org (5.64+/OSF 1.0)
    662 	id AA14480; Tue, 12 May 92 13:25:03 -0400
    663 Received: by earth.osf.org (5.64/4.7) id AA03499; Tue, 12 May 92 13:25:02 -0400
    664 Date: Tue, 12 May 1992 12:25:02 -0500
    665 From: rsalz@osf.org
    666 Message-Id: <9205121725.AA03499@earth.osf.org>
    667 To: rc@archone.tamu.edu
    668 Subject: Useful function
    669 Status: R
    670 
    671 It looks like line noise, but it turns things like
    672 	/home/rsalz/foo/bar
    673 into
    674 	~/foo/bar
    675 
    676 Useful for when you put your current directory up in your icon title.
    677 By duplicating the $home section you can make things like
    678 	/project/dce/build/dce1.0.1/src/rpc
    679 become
    680 	$MYBT/src/rpc
    681 
    682 ##  If a pathname starts with $home, turn $home into ~.  Uses all built-ins.
    683 fn tildepath { p1=() i=() {
    684     p1=$1
    685     switch ($p1) {
    686     case $home $home/*
    687 	# Split arg into components
    688 	*=`` (/) { echo -n $p1 }
    689 	# Shift down by number of components in $home
    690 	for (i in `` (/) { echo -n $home } ) shift
    691 	# Glue back together
    692 	p1='~'
    693 	for (i) p1=$p1 ^ '/' ^ $i
    694 	echo $p1
    695     case *
    696 	echo $p1
    697     }
    698     return 0
    699 } }
    700 
    701 From osf.org!rsalz Tue May 12 15:47:12 1992
    702 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
    703 Received: from earth.osf.org by postman.osf.org (5.64+/OSF 1.0)
    704 	id AA21070; Tue, 12 May 92 16:46:58 -0400
    705 Received: by earth.osf.org (5.64/4.7) id AA09396; Tue, 12 May 92 16:46:56 -0400
    706 Date: Tue, 12 May 1992 15:46:56 -0500
    707 From: rsalz@osf.org
    708 Message-Id: <9205122046.AA09396@earth.osf.org>
    709 To: byron@archone.tamu.edu
    710 Subject: Re: Useful function
    711 Status: R
    712 
    713 >wow. thanks, i'll add it to EXAMPLES.
    714 Glad you like.  Getting something added to EXAMPLES has been a goal of mine...
    715 
    716 I've been thinking, a bit, about a more general way of doing it.  I want
    717 a "prefix-sub" function, like this
    718 	prefix $some_path var1 var2 var3 var4 var5
    719 That would take some_path and replace any leading $var1 (etc) values
    720 with the variable name.  Return on the first match.
    721 
    722 Hmm -- come to think of it, that's very easy to do:
    723 
    724 # Use pathprefix filename var1 var2 var3
    725 # returns filename, with leading prefixes (in $var1...) turned into the
    726 # string $var1...
    727 fn pathprefix { p1=() i=() j=() {
    728     p1=$1 ; shift
    729     for (i) {
    730 	~ $p1 $$i $$i^/* && {
    731 	    *=`` (/) { echo -n $p1 }
    732 	    for (j in `` (/) { echo -n $$i } ) shift
    733 	    p1='$'^$i
    734 	    for (j) p1=$p1 ^ '/' ^ $j
    735 	    echo $p1
    736 	    return 0
    737 	}
    738     }
    739     echo $p1
    740     return 0
    741 } }
    742 
    743 home=/user/users/rsalz
    744 z=/usr/users
    745 pathprefix /usr/users/rsalz home usr    # --> $home
    746 pathprefix /usr/users/rsalz z           # --> $z/rsalz
    747 pathprefix /usr/users/rsalz/foo z home  # --> $z/rsalz/foo
    748 pathprefix /usr/users/rsalz/foo home    # --> $home/foo
    749