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