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