[comp.unix.shell] Multiline aliases in csh ?

aps@tut.fi (Suntioinen Ari) (10/23/90)

	Is there any way to define multi line alias in csh ?
	What I mean is (for example) if-else or foreach loop 
	as a part of alias. I tried it some time and just 
	couldn't make it work.

	PS.  Right answer is not "use scripts".
	PPS. Would be nice to have functions in csh.

--
  Ari Suntioinen           "Scitsophrenia is the cure for cancer..
  Opiskelijankatu 4C162     Cancer is probably the cure for scitsophrenia..
  33720 Tampere, Finland    People just don't know how tough it is out there."

jik@athena.mit.edu (Jonathan I. Kamens) (10/23/90)

In article <APS.90Oct23053623@kaarne.tut.fi>, aps@tut.fi (Suntioinen Ari) writes:
|> 	Is there any way to define multi line alias in csh ?
|> 	What I mean is (for example) if-else or foreach loop 
|> 	as a part of alias. I tried it some time and just 
|> 	couldn't make it work.

  In a word, no.

  There are ways to get around it for if-else, for example checking the
condition for every statement in the if.  If you have this in a shell script:

    if (foo) then
      bar
      baz
    else
      frelt
    endif

Then you can have an alias that does this:

    if (foo) bar; if (foo) baz; if (! foo) frelt

  I don't think there's any way to get around it for foreach.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (10/24/90)

In article <APS.90Oct23053623@kaarne.tut.fi> aps@tut.fi (Suntioinen Ari) writes:
: 	Is there any way to define multi line alias in csh ?
: 	What I mean is (for example) if-else or foreach loop 
: 	as a part of alias. I tried it some time and just 
: 	couldn't make it work.

No, but you can often get the same effect using a program that forces
multiple commands back into your input:

alias patchit forceme "'"'foreach file (*.pat)'"'"\; \
forceme "'"'patch <$file'"'"\; \
forceme 'end'

Won't work in a script, of course, but if you're just interested in being
a lazy typist...

: 	PS.  Right answer is not "use scripts".

Why not?  Assuming a reasonable script language, of course...   :-)

: 	PPS. Would be nice to have functions in csh.

Some would call this "throwing good money after bad".

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (10/24/90)

In article <10084@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
> No, but you can often get the same effect using a program that forces
> multiple commands back into your input:
> alias patchit forceme "'"'foreach file (*.pat)'"'"\; \
> forceme "'"'patch <$file'"'"\; \
> forceme 'end'

I assume that ``forceme'' uses TIOCSTI to simulate terminal input. For
readers without it, enclosed is tiocsti, which does the same job.

> Won't work in a script, of course, but if you're just interested in being
> a lazy typist...

Right. Btw, why don't you use \' rather than "'"? Might not work in
awk, of course, but if you're just interested in being a lazy typist...
Actually, it'd be a lot clearer to just double-quote your entire alias.

---Dan

/* Public domain. */
#include <sys/ioctl.h>

main(argc,argv)
int argc;
char *argv[];
{
 int j;
 char *s;

 if (ioctl(3,TIOCGPGRP,(char *) &j) == -1)
   (void) dup2(0,3);

 for (j = 1;j < argc;j++)
  {
   for (s = argv[j];*s;s++)
     (void) ioctl(3,TIOCSTI,s);
   if (j < argc - 1)
     (void) ioctl(3,TIOCSTI," ");
  }
}

TH tiocsti 1
SH NAME
tiocsti \- simulate terminal input
SH SYNOPSIS
B tiocsti
[
I arg
] ...
SH DESCRIPTION
I tiocsti
``types'' each of its arguments on the
current terminal, separated by spaces,
as if you had typed them.
SH "SEE ALSO"
tty(4)

jbw@bucsf.bu.edu (Joe Wells) (10/25/90)

aps@tut.fi (Suntioinen Ari) writes:

	   Is there any way to define multi line alias in csh ?
	   What I mean is (for example) if-else or foreach loop 
	   as a part of alias.

Sure, here's an appropriately twisted example:

    alias which '(set argv=($path) target=\!*; whichi)'
    alias whichi 'if (! $#argv) set status=1 && whichii'
    alias whichii \
    'if (-x "$1/$target") set status=1 && shift && eval "whichi" \\
     || if ($#argv > 0) echo "$1/$target"'

Conditionals are done with:

    if (condition) set status=1 && else-statement || then-statement

Looping is done with recursion, which requires you to use "eval",
otherwise the csh will detect the recursion and refuse to do it.

Note, the last line should actually read:

     || echo "$1/$target"'

but yet another csh bug prevents it from working that way.

Enjoy,

-- 
Joe Wells <jbw@bu.edu>

PS.  I think this example clearly justifies why the author(s) of the csh
will be roasting in hell for a long long long time.