Bell 32V development
[unix-history] / usr / src / cmd / sh / main.c
CommitLineData
b943447d
TL
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
18UFD output = 2;
19LOCAL BOOL beenhere = FALSE;
20CHAR tmpout[20] = "/tmp/sh-";
21FILEBLK stdfile;
22FILE standin = &stdfile;
23#include <execargs.h>
24
25PROC VOID exfile();
26
27
28
29
30main(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
92LOCAL VOID exfile(prof)
93BOOL 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);
142 FI
143
144 trapnote=0; peekc=readc();
145 IF eof
146 THEN return;
147 FI
148 execute(cmd(NL,MTFLG),0);
149 eof |= (flags&oneflg);
150 POOL
151}
152
153chkpr(eor)
154char eor;
155{
156 IF (flags&prompt) ANDF standin->fstak==0 ANDF eor==NL
157 THEN prs(ps2nod.namval);
158 FI
159}
160
161settmp()
162{
163 itos(getpid()); serial=0;
164 tmpnam=movstr(numbuf,&tmpout[TMPNAM]);
165}
166
167Ldup(fa, fb)
168 REG INT fa, fb;
169{
170 dup(fa|DUPFLG, fb);
171 close(fa);
172 ioctl(fb, FIOCLEX, 0);
173}