[comp.unix.questions] which/type & built-ins : A question ?

satam@ecs.umass.edu (Kirtikumar Satam) (01/03/90)

In article <1990Jan2.160927.11935@usenet.ins.cwru.edu>, 
chet@cwns1.CWRU.EDU (Chet Ramey) writes:
> Because `while' is a sh language construct (a statement), not a shell
   			  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^	
> builtin.  It's a small but important distinction, and one of the chief
  ^^^^^^^
> reasons that programming in sh is so much better than programming in csh
> (in csh, these constructs *are* implemented as builtins, using very ad-hoc
> parsing with thousands of special cases that serves to reduce their
> usefulness to almost zero). 
> 

Can you elaborate more on this??

-satam
------------------------------------------------------------------------
Kirtikumar "Mumbaichaa" Satam
INTERNET : satam@ecs.umass.edu
BITNET : satam@umaecs.bitnet
217 Northwood Apts, Sunderland, MA 01375   Tel# 413-665-3222
------------------------------------------------------------------------

chet@cwns1.CWRU.EDU (Chet Ramey) (01/08/90)

I wrote:

>> Because `while' is a sh language construct (a statement), not a shell
>> builtin.

and Kirtikumar Satam asks:

>Can you elaborate more on this??

This is long and rambling (and not particularly clear), but I hope it
catches the basic distinction between shell statements and builtins.

The shell is, in effect, a complete interpreted programming language.  In
that language, there are statements provided as in any programming
language, mostly looping and alternation constructs.  Sh actually parses
each complete statement in its input into a parse tree (sh uses a recursive
descent parser, bash uses a yacc-generated parser), and executes it. 
While, if, for, until, and case statements are all sh language constructs. 
Shell builtins are parsed as any other command word would be; the fact that
they are shell builtins is not discovered until the parse tree is executed. 
The C shell has everything as builtins, and uses ad-hoc parsing mechanisms
to implement its `language constructs', with lots of special cases (which is
why, for instance, it is so particular about the appearance of white space
in its `if' statement).

if [ -t 0 ] ; then
	echo terminal
else
	echo no terminal
fi

is one sh statement that is parsed into a tree and executed.  `if' is
executed as a builtin by csh, and the if builtin is responsible for
consuming as much input as it needs.  This is one reason redirection in the
C shell is so limited, like redirections into and out of loops.  In sh, a
statement is always completely parsed before it is executed, so
redirections into and out of loops are trivial to implement. 

Chet Ramey

-- 
Chet Ramey
Network Services Group				"Help! Help! I'm being
Case Western Reserve University			 repressed!"
chet@ins.CWRU.Edu