Annotation of 41BSD/cmd/sh/main.c, revision 1.1.1.1

1.1       root        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: /*
                    119:                {
                    120:        #include <signal.h>
                    121:                signal(SIGTTIN, SIG_IGN);
                    122:                signal(SIGTTOU, SIG_IGN);
                    123:                signal(SIGTSTP, SIG_IGN);
                    124:                }
                    125: */
                    126:        ELSE    flags |= prof; flags &= ~prompt;
                    127:        FI
                    128: 
                    129:        IF setjmp(errshell) ANDF prof
                    130:        THEN    close(input); return;
                    131:        FI
                    132: 
                    133:        /* error return here */
                    134:        loopcnt=breakcnt=peekc=0; iopend=0;
                    135:        IF input>=0 THEN initf(input) FI
                    136: 
                    137:        /* command loop */
                    138:        LOOP    tdystak(0);
                    139:                stakchk(); /* may reduce sbrk */
                    140:                exitset();
                    141:                IF (flags&prompt) ANDF standin->fstak==0 ANDF !eof
                    142:                THEN    IF mailnod.namval
                    143:                            ANDF stat(mailnod.namval,&statb)>=0 ANDF statb.st_size
                    144:                            ANDF (statb.st_mtime != mailtime)
                    145:                            ANDF mailtime
                    146:                        THEN    prs(mailmsg)
                    147:                        FI
                    148:                        mailtime=statb.st_mtime;
                    149:                        prs(ps1nod.namval);
                    150:                FI
                    151: 
                    152:                trapnote=0; peekc=readc();
                    153:                IF eof
                    154:                THEN    return;
                    155:                FI
                    156:                execute(cmd(NL,MTFLG),0);
                    157:                eof |= (flags&oneflg);
                    158:        POOL
                    159: }
                    160: 
                    161: chkpr(eor)
                    162: char eor;
                    163: {
                    164:        IF (flags&prompt) ANDF standin->fstak==0 ANDF eor==NL
                    165:        THEN    prs(ps2nod.namval);
                    166:        FI
                    167: }
                    168: 
                    169: settmp()
                    170: {
                    171:        itos(getpid()); serial=0;
                    172:        tmpnam=movstr(numbuf,&tmpout[TMPNAM]);
                    173: }
                    174: 
                    175: Ldup(fa, fb)
                    176:        REG INT         fa, fb;
                    177: {
                    178:        dup(fa|DUPFLG, fb);
                    179:        close(fa);
                    180:        ioctl(fb, FIOCLEX, 0);
                    181: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.