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

Defined functions

Ldup defined in line 169; used 2 times
chkpr defined in line 155; used 7 times
exfile defined in line 93; used 3 times
main defined in line 30; never used
settmp defined in line 163; used 2 times
Last modified: 1981-11-28
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 854
Valid CSS Valid XHTML 1.0 Strict