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