LWN.net Logo

Evolution of shells in Linux (developerWorks)

Evolution of shells in Linux (developerWorks)

Posted Dec 8, 2011 0:37 UTC (Thu) by lkundrak (subscriber, #43452)
Parent article: Evolution of shells in Linux (developerWorks)

One of interesting things about early UNIX shells (Thomson's, up to sixth edition) was that exit and goto were not built-ins, but simple programs that seeked a file descriptor open for the running script appropriately, either to the beginning proceeding to given label or to the end. Also, Bourne's seventh edition shell is almost universally considered to be one of the most horrible C code ever written. From its mac.h source file:

#define LOCAL   static
#define PROC    extern
#define TYPE    typedef
#define STRUCT  TYPE struct
#define UNION   TYPE union
#define REG     register
#define IF      if(
#define THEN    ){
#define ELSE    } else {
#define ELIF    } else if (
#define FI      ;}
#define BEGIN   {
#define END     }
#define SWITCH  switch(
#define IN      ){
#define ENDSW   }
#define FOR     for(
#define WHILE   while(
#define DO      ){
#define OD      ;}
#define REP     do{
#define PER     }while(
#define DONE    );
#define LOOP    for(;;){
#define POOL    }
#define SKIP    ;
#define DIV     /
#define REM     %
#define NEQ     ^
#define ANDF    &&
#define ORF     ||
#define TRUE    (-1)
#define FALSE   0
Thankfully, many of the macros, such as DIV are not actually used anywhere. This is what the code actually looks like:
VOID    fault(sig)
        REG INT         sig;
{
        REG INT         flag;

        signal(sig,fault);
        IF sig==MEMF
        THEN    IF setbrk(brkincr) == -1
                THEN    error(nospace);
                FI
Also, the code quoted is another rather creative technique used by the shell: it's the signal handler for memory faults, that simply enlarges heap with setbrk() when an out-of-bounds memory is signalled by kernel. Needless to say, there are not many calls to malloc() in the source...


(Log in to post comments)

Evolution of shells in Linux (developerWorks)

Posted Dec 8, 2011 5:04 UTC (Thu) by tshow (subscriber, #6411) [Link]

Ok, the pair of LOOP and POOL have to win some sort of award for being spectacularly awful. Was this done on a bet?

Evolution of shells in Linux (developerWorks)

Posted Dec 8, 2011 14:35 UTC (Thu) by nix (subscriber, #2304) [Link]

He liked Algol. A lot. Hence BOURNEGOL.

I have never seen another program written in BOURNEGOL, which is a real pity: the more programming becomes a matter of manipulating something deeply disgusting like that, the fewer people will want to be programmers and the higher our paycheques go :P

(similarly, I propose rotating knives and Great Cthulhu in job interviews.)

*removes tongue from cheek*

Evolution of shells in Linux (developerWorks)

Posted Dec 8, 2011 6:28 UTC (Thu) by BrucePerens (guest, #2510) [Link]

Oh yes, Steve Bourne's MACRO ALGOL.

I remember being recruited for a company where they were pushing that we would get to work with Steve Bourne! Now, I have never met him and don't know anything about him except for how much I hated that MACRO ALGOL when I was trying to fix something in v7 shell. I don't think I explained to the recruiter why the prospect of working with him had me nonplussed. But I'm sure he heard about it eventually.

Evolution of shells in Linux (developerWorks)

Posted Dec 8, 2011 14:33 UTC (Thu) by nix (subscriber, #2304) [Link]

Needless to say, there are not many calls to malloc() in the source
This would be because it predates malloc(). (Remember your K&R first edition, with no mention of malloc() but discussion of an alloc() memory allocator that you might wish to write. And that was 1978...)

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds