home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / sh / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-12  |  3.0 KB  |  175 lines

  1. #
  2. /*
  3.  * UNIX shell
  4.  *
  5.  * S. R. Bourne
  6.  * Bell Telephone Laboratories
  7.  *
  8.  */
  9.  
  10. #include    "defs.h"
  11. #include    "dup.h"
  12. #include    "sym.h"
  13. #include    "timeout.h"
  14. #include    <sys/types.h>
  15. #include    <sys/stat.h>
  16. #include    <sgtty.h>
  17.  
  18. UFD        output = 2;
  19. LOCAL BOOL    beenhere = FALSE;
  20. CHAR        tmpout[20] = "/tmp/sh-";
  21. FILEBLK        stdfile;
  22. FILE        standin = &stdfile;
  23. #include    <execargs.h>
  24.  
  25. PROC VOID    exfile();
  26.  
  27.  
  28.  
  29.  
  30. main(c, v)
  31.     INT        c;
  32.     STRING        v[];
  33. {
  34.     REG INT        rflag=ttyflg;
  35.  
  36.     /* initialise storage allocation */
  37.     stdsigs();
  38.     setbrk(BRKINCR);
  39.     addblok((POS)0);
  40.  
  41.     /* set names from userenv */
  42.     getenv();
  43.  
  44.     /* look for restricted */
  45. /*    IF c>0 ANDF any('r', *v) THEN rflag=0 FI */
  46.  
  47.     /* look for options */
  48.     dolc=options(c,v);
  49.     IF dolc<2 THEN flags |= stdflg FI
  50.     IF (flags&stdflg)==0
  51.     THEN    dolc--;
  52.     FI
  53.     dolv=v+c-dolc; dolc--;
  54.  
  55.     /* return here for shell file execution */
  56.     setjmp(subshell);
  57.  
  58.     /* number of positional parameters */
  59.     assnum(&dolladr,dolc);
  60.     cmdadr=dolv[0];
  61.  
  62.     /* set pidname */
  63.     assnum(&pidadr, getpid());
  64.  
  65.     /* set up temp file names */
  66.     settmp();
  67.  
  68.     /* default ifs */
  69.     dfault(&ifsnod, sptbnl);
  70.  
  71.     IF (beenhere++)==FALSE
  72.     THEN    /* ? profile */
  73.         IF *cmdadr=='-'
  74.             ANDF (input=pathopen(nullstr, profile))>=0
  75.         THEN    exfile(rflag); flags &= ~ttyflg;
  76.         FI
  77.         IF rflag==0 THEN flags |= rshflg FI
  78.  
  79.         /* open input file if specified */
  80.         IF comdiv
  81.         THEN    estabf(comdiv); input = -1;
  82.         ELSE    input=((flags&stdflg) ? 0 : chkopen(cmdadr));
  83.             comdiv--;
  84.         FI
  85.     ELSE    *execargs=dolv;    /* for `ps' cmd */
  86.     FI
  87.  
  88.     exfile(0);
  89.     done();
  90. }
  91.  
  92. LOCAL VOID    exfile(prof)
  93. BOOL        prof;
  94. {
  95.     REG L_INT    mailtime = 0;
  96.     REG INT        userid;
  97.     struct stat    statb;
  98.  
  99.     /* move input */
  100.     IF input>0
  101.     THEN    Ldup(input,INIO);
  102.         input=INIO;
  103.     FI
  104.  
  105.     /* move output to safe place */
  106.     IF output==2
  107.     THEN    Ldup(dup(2),OTIO);
  108.         output=OTIO;
  109.     FI
  110.  
  111.     userid=getuid();
  112.  
  113.     /* decide whether interactive */
  114.     IF (flags&intflg) ORF ((flags&oneflg)==0 ANDF gtty(output,&statb)==0 ANDF gtty(input,&statb)==0)
  115.     THEN    dfault(&ps1nod, (userid?stdprompt:supprompt));
  116.         dfault(&ps2nod, readmsg);
  117.         flags |= ttyflg|prompt; ignsig(KILL);
  118.     ELSE    flags |= prof; flags &= ~prompt;
  119.     FI
  120.  
  121.     IF setjmp(errshell) ANDF prof
  122.     THEN    close(input); return;
  123.     FI
  124.  
  125.     /* error return here */
  126.     loopcnt=breakcnt=peekc=0; iopend=0;
  127.     IF input>=0 THEN initf(input) FI
  128.  
  129.     /* command loop */
  130.     LOOP    tdystak(0);
  131.         stakchk(); /* may reduce sbrk */
  132.         exitset();
  133.         IF (flags&prompt) ANDF standin->fstak==0 ANDF !eof
  134.         THEN    IF mailnod.namval
  135.                 ANDF stat(mailnod.namval,&statb)>=0 ANDF statb.st_size
  136.                 ANDF (statb.st_mtime != mailtime)
  137.                 ANDF mailtime
  138.             THEN    prs(mailmsg)
  139.             FI
  140.             mailtime=statb.st_mtime;
  141.             prs(ps1nod.namval); alarm(TIMEOUT); flags |= waiting;
  142.         FI
  143.  
  144.         trapnote=0; peekc=readc();
  145.         IF eof
  146.         THEN    return;
  147.         FI
  148.         alarm(0); flags &= ~waiting;
  149.         execute(cmd(NL,MTFLG),0);
  150.         eof |= (flags&oneflg);
  151.     POOL
  152. }
  153.  
  154. chkpr(eor)
  155. char eor;
  156. {
  157.     IF (flags&prompt) ANDF standin->fstak==0 ANDF eor==NL
  158.     THEN    prs(ps2nod.namval);
  159.     FI
  160. }
  161.  
  162. settmp()
  163. {
  164.     itos(getpid()); serial=0;
  165.     tmpnam=movstr(numbuf,&tmpout[TMPNAM]);
  166. }
  167.  
  168. Ldup(fa, fb)
  169.     REG INT        fa, fb;
  170. {
  171.     dup(fa|DUPFLG, fb);
  172.     close(fa);
  173.     ioctl(fb, FIOCLEX, 0);
  174. }
  175.