CSH script help needed
Michael Condict
condict at csd1.UUCP
Sun Jan 8 02:35:24 AEST 1984
All you need is a way to execute "source" commands with argument(s) after
the file name, right? Well, to prove that this is completely straightforward
in the elegant and powerful mechanism loosely known as the C-shell (4.2bsd),
I hereby enclose a copy of my .cshrc file, which contains the desired alias
(called "so", below). Use it with:
so file [args]
With no args it is equivalent to "source file" (but a tad slower).
Note that, at no extra charge, I provide you with a one-line while loop and
a "cd" command that sources a .cdrc file upon entering a directory (the latter
is an optional accessory, but "while1" is needed by "so").
I posted this before as a puzzle, with no comments -- didn't think I'd ever
find that somebody actually wanted it!
Michael Condict ...!cmcl2!csd1!condict
P.S. Neither I nor my heirs and/or creditors are responsible for the correct
functioning of any part of this "code", especially if any character of it
is changed.
P.P.S. This code memorializes the closest I've ever come to hacker's paradise.
I can still feel the adrenaline coursing through my veins as I eradi-
cated the last bug with a carefully placed "\\\'\''" ...
------------------------------------------------------------------------------
#
alias l 'ls -l'
alias m 'more'
alias rm '/bin/rm -i'
alias h history
#
# A one-line while loop (can be used in aliases):
alias while1 'eval "if (" \!:1 ")'\
'eval \!:2:q \; while1 '\\\''\!:1:q'\\\' \\\''\!:2:q'\\\'\"
#
# Executes a source command with arguments (placed in argv, which is
# saved and restored) and with local variables (see local, below):
set stack = ("ENDFRAME")
alias so 'set stack = ("ENDFRAME" $stack:q);'\
'local argv;'\
'set argv=(\!*);'\
'if ($#argv == 0) source;'\
'if ($#argv > 0) eval'\
\''set stack = (" " $stack:q);'\
'@ stack[1] = 1;'\
'if ("$argv[1]" =~ /*) @ stack[1] += $#path;'\
'while1 '\'\\\'\''$stack[1] <= $#path'\'\\\'\
\\\'\''if (-x "$path[$stack[1]]"/$argv[1])'\
'@ stack[1] += $#path;'\
'@ stack[1] += 1'\'\\\'\'' ;'\
'@ stack[1] -= (1 + $#path);'\
'if ($stack[1] > 0)'\
'set argv[1] = "$path[$stack[1]]"/$argv[1];'\
'shift stack;'\
'eval "shift; source $argv[1]"'\'';'\
'unlocal; shift stack'
#
# Use at top of an "so" file (see above) to declare local vars of the file
# (ones whose lifetime should coincide with the execution of the "so" file)
alias local 'if ("\!:2*" != "") eval local \!:2*;'\
'if (! $?\!{:1})'\
'set stack = ("unset \!:1" $stack:q);'\
'if ($?\!{:1}) eval'\
\''set stack = ("shift stack;'\
'set \!:1 = ("\$"stack[1-$#\!{:1}]:q);'\
'set stack = ("\$"stack[$#\!{:1}-]:q)"'\
'$\!{:1}:q $stack:q);'\
'if ($#\!{:1} == 0) set stack[1] = "set \!:1 = ()"'\'
#
# Use to restore vars saved with local, above. There must be an occurrence
# of "ENDFRAME" as a word in $stack:
alias unlocal 'eval "$stack[1]"; shift stack;'\
'if ("$stack[1]" != "ENDFRAME") eval unlocal'
#
# The following will act like cd but will execute file .cdrc
# (using 'so', above) if it is found in the
# new directory, with args consisting of the new dir followed by any
# additional args to cd:
#alias cd 'set cd=(\!*);'\
# 'if ($#cd == 0) set cd = ~;'\
# 'eval chdir $cd[1]:q;'\
# 'if (-r .cdrc) eval so .cdrc '\''$cd:q'\''; unset cd'
----------------------------------------------------------------------------
More information about the Comp.unix.wizards
mailing list