Annotation of researchv10dc/cmd/osh/main.c, revision 1.1.1.1

1.1       root        1: /*     @(#)main.c      1.7     */
                      2: /*
                      3:  * UNIX shell
                      4:  *
                      5:  * Bell Telephone Laboratories
                      6:  *
                      7:  */
                      8: 
                      9: #include       "defs.h"
                     10: #include       "sym.h"
                     11: #include       "timeout.h"
                     12: #include       <sys/types.h>
                     13: #include       <sys/stat.h>
                     14: #include       <sgtty.h>
                     15: 
                     16: static BOOL    beenhere = FALSE;
                     17: char           tmpout[20] = "/tmp/sh-";
                     18: struct fileblk stdfile;
                     19: struct fileblk *standin = &stdfile;
                     20: 
                     21: static int     mailchk = 180;
                     22: static char    *mailp;
                     23: static long    mod_time = 0;
                     24: 
                     25: #ifdef pdp11
                     26: #include       <execargs.h>
                     27: #endif
                     28: 
                     29: static int     exfile();
                     30: extern char    *simple();
                     31: 
                     32: 
                     33: 
                     34: main(c, v, e)
                     35: int    c;
                     36: char   *v[];
                     37: char   *e[];
                     38: {
                     39:        stdsigs();
                     40: 
                     41:        /*
                     42:         * initialise storage allocation
                     43:         */
                     44: 
                     45:        stakbot = 0;
                     46:        addblok((unsigned)0);
                     47: 
                     48:        /*
                     49:         * set names from userenv
                     50:         */
                     51: 
                     52:        setup_env();
                     53: 
                     54:        /*
                     55:         * look for options
                     56:         * dolc is $#
                     57:         */
                     58:        dolc = options(c, v);
                     59: 
                     60:        if (dolc < 2)
                     61:        {
                     62:                flags |= stdflg;
                     63:                {
                     64:                        register char *flagc = flagadr;
                     65: 
                     66:                        while (*flagc)
                     67:                                flagc++;
                     68:                        *flagc++ = STDFLG;
                     69:                        *flagc = 0;
                     70:                }
                     71:        }
                     72:        if ((flags & stdflg) == 0)
                     73:                dolc--;
                     74:        dolv = v + c - dolc;
                     75:        dolc--;
                     76: 
                     77:        /*
                     78:         * return here for shell file execution
                     79:         * but not for parenthesis subshells
                     80:         */
                     81:        setjmp(subshell);
                     82: 
                     83:        /*
                     84:         * number of positional parameters
                     85:         */
                     86:        replace(&cmdadr, dolv[0]);      /* cmdadr is $0 */
                     87: 
                     88:        /*
                     89:         * set pidname '$$'
                     90:         */
                     91:        assnum(&pidadr, getpid());
                     92: 
                     93:        /*
                     94:         * set up temp file names
                     95:         */
                     96:        settmp();
                     97: 
                     98:        /*
                     99:         * default internal field separators - $IFS
                    100:         */
                    101:        dfault(&ifsnod, sptbnl);
                    102: 
                    103:        if ((beenhere++) == FALSE)      /* ? profile */
                    104:        {
                    105:                if ((flags&protflg)==0 && *(simple(cmdadr)) == '-')
                    106:                {
                    107:                        if ((input = pathopen(nullstr, profile)) >= 0)
                    108:                        {
                    109:                                exfile(ttyflg);
                    110:                                flags &= ~ttyflg;
                    111:                        }
                    112:                }
                    113:                /*
                    114:                 * open input file if specified
                    115:                 */
                    116:                if (comdiv)
                    117:                {
                    118:                        estabf(comdiv);
                    119:                        input = -1;
                    120:                }
                    121:                else
                    122:                {
                    123:                        input = ((flags & stdflg) ? 0 : chkopen(cmdadr));
                    124: 
                    125: #ifdef ACCT
                    126:                        if (input != 0)
                    127:                                preacct(cmdadr);
                    128: #endif
                    129:                        comdiv--;
                    130:                }
                    131:        }
                    132: #ifdef pdp11
                    133:        else
                    134:                *execargs = (char *)dolv;       /* for `ps' cmd */
                    135: #endif
                    136:                
                    137:        exfile(0);
                    138:        done();
                    139: }
                    140: 
                    141: static int
                    142: exfile(prof)
                    143: BOOL   prof;
                    144: {
                    145:        long    mailtime = 0;   /* Must not be a register variable */
                    146:        long    curtime = 0;
                    147:        register int    userid;
                    148: 
                    149:        /*
                    150:         * move input
                    151:         */
                    152:        if (input > 0)
                    153:        {
                    154:                Ldup(input, INIO);
                    155:                input = INIO;
                    156:        }
                    157: 
                    158:        userid = geteuid();
                    159: 
                    160:        /*
                    161:         * decide whether interactive
                    162:         */
                    163:        if ((flags & intflg) ||
                    164:            ((flags&oneflg) == 0 &&
                    165:            isatty(output) &&
                    166:            isatty(input)) )
                    167:            
                    168:        {
                    169:                dfault(&ps1nod, (userid ? stdprompt : supprompt));
                    170:                dfault(&ps2nod, readmsg);
                    171:                flags |= ttyflg | prompt;
                    172:                ignsig(SIGTERM);
                    173:                setmail(mailnod.namval.val);
                    174:        }
                    175:        else
                    176:        {
                    177:                flags |= prof;
                    178:                flags &= ~prompt;
                    179:        }
                    180: 
                    181:        if (setjmp(errshell) && prof)
                    182:        {
                    183:                close(input);
                    184:                return;
                    185:        }
                    186:        /*
                    187:         * error return here
                    188:         */
                    189: 
                    190:        loopcnt = peekc = peekn = 0;
                    191:        fndef = 0;
                    192:        iopend = 0;
                    193: 
                    194:        if (input >= 0)
                    195:                initf(input);
                    196:        /*
                    197:         * command loop
                    198:         */
                    199:        for (;;)
                    200:        {
                    201:                tdystak((char *)0);
                    202:                stakchk();      /* may reduce sbrk */
                    203:                exitset();
                    204: 
                    205:                if ((flags & prompt) && standin->fstak == 0 && !eof)
                    206:                {
                    207: 
                    208:                        if (mailp)
                    209:                        {
                    210:                                time(&curtime);
                    211: 
                    212:                                if ((curtime - mailtime) >= mailchk)
                    213:                                {
                    214:                                        chkmail();
                    215:                                        mailtime = curtime;
                    216:                                }
                    217:                        }
                    218: 
                    219:                        prs(ps1nod.namval.val);
                    220: 
                    221: #ifdef TIME_OUT
                    222:                        alarm(TIMEOUT);
                    223: #endif
                    224: 
                    225:                        flags |= waiting;
                    226:                }
                    227: 
                    228:                trapnote = 0;
                    229:                peekc = readc();
                    230:                if (eof)
                    231:                        return;
                    232: 
                    233: #ifdef TIME_OUT
                    234:                alarm(0);
                    235: #endif
                    236: 
                    237:                flags &= ~waiting;
                    238: 
                    239:                execute(cmd(NL, MTFLG), 0, eflag);
                    240:                eof |= (flags & oneflg);
                    241:        }
                    242: }
                    243: 
                    244: chkpr()
                    245: {
                    246:        if ((flags & prompt) && standin->fstak == 0)
                    247:                prs(ps2nod.namval.val);
                    248: }
                    249: 
                    250: settmp()
                    251: {
                    252:        itos(getpid());
                    253:        serial = 0;
                    254:        tmpname = movstr(numbuf, &tmpout[TMPNAM]);
                    255: }
                    256: 
                    257: Ldup(fa, fb)
                    258: register int   fa, fb;
                    259: {
                    260: 
                    261: #ifndef        SYSV
                    262:        dup2(fa, fb);
                    263:        close(fa);
                    264:        ioctl(fb, FIOCLEX, 0);
                    265: #else
                    266:         if (fa >= 0)
                    267:                 { close(fb);
                    268:                   fcntl(fa,0,fb);               /* normal dup */
                    269:                   close(fa);
                    270:                   fcntl(fb, 2, 1);              /* autoclose for fb */
                    271:                 }
                    272: #endif
                    273: }
                    274: 
                    275: 
                    276: chkmail()
                    277: {
                    278:        struct stat     statb;
                    279: 
                    280:        if (mailp && stat(mailp, &statb) >= 0)
                    281:        {
                    282:                if(statb.st_size && mod_time
                    283:                        && statb.st_mtime != mod_time)
                    284:                {
                    285:                        prs(mailmsg);
                    286:                }
                    287:                mod_time = statb.st_mtime;
                    288:        }
                    289:        else if (mod_time == 0)
                    290:                mod_time = 1;
                    291: }
                    292: 
                    293: 
                    294: setmail(mailpath)
                    295:        char *mailpath;
                    296: {
                    297: 
                    298:        if (mailp = mailpath)
                    299:                mod_time=0;
                    300: }

unix.superglobalmegacorp.com

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