1 | /* $Header: /afs/dev.mit.edu/source/repository/third/tcsh/sh.c,v 1.4 2005-06-03 15:17:09 ghudson Exp $ */ |
---|
2 | /* |
---|
3 | * sh.c: Main shell routines |
---|
4 | */ |
---|
5 | /*- |
---|
6 | * Copyright (c) 1980, 1991 The Regents of the University of California. |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions |
---|
11 | * are met: |
---|
12 | * 1. Redistributions of source code must retain the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer. |
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
15 | * notice, this list of conditions and the following disclaimer in the |
---|
16 | * documentation and/or other materials provided with the distribution. |
---|
17 | * 3. Neither the name of the University nor the names of its contributors |
---|
18 | * may be used to endorse or promote products derived from this software |
---|
19 | * without specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
31 | * SUCH DAMAGE. |
---|
32 | */ |
---|
33 | #define EXTERN /* Intern */ |
---|
34 | #include "sh.h" |
---|
35 | |
---|
36 | #ifndef lint |
---|
37 | char copyright[] = |
---|
38 | "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ |
---|
39 | All rights reserved.\n"; |
---|
40 | #endif /* not lint */ |
---|
41 | |
---|
42 | RCSID("$Id: sh.c,v 1.4 2005-06-03 15:17:09 ghudson Exp $") |
---|
43 | |
---|
44 | #include "tc.h" |
---|
45 | #include "ed.h" |
---|
46 | #include "tw.h" |
---|
47 | |
---|
48 | extern int MapsAreInited; |
---|
49 | extern int NLSMapsAreInited; |
---|
50 | |
---|
51 | /* |
---|
52 | * C Shell |
---|
53 | * |
---|
54 | * Bill Joy, UC Berkeley, California, USA |
---|
55 | * October 1978, May 1980 |
---|
56 | * |
---|
57 | * Jim Kulp, IIASA, Laxenburg, Austria |
---|
58 | * April 1980 |
---|
59 | * |
---|
60 | * Filename recognition added: |
---|
61 | * Ken Greer, Ind. Consultant, Palo Alto CA |
---|
62 | * October 1983. |
---|
63 | * |
---|
64 | * Karl Kleinpaste, Computer Consoles, Inc. |
---|
65 | * Added precmd, periodic/tperiod, prompt changes, |
---|
66 | * directory stack hack, and login watch. |
---|
67 | * Sometime March 1983 - Feb 1984. |
---|
68 | * |
---|
69 | * Added scheduled commands, including the "sched" command, |
---|
70 | * plus the call to sched_run near the precmd et al |
---|
71 | * routines. |
---|
72 | * Upgraded scheduled events for running events while |
---|
73 | * sitting idle at command input. |
---|
74 | * |
---|
75 | * Paul Placeway, Ohio State |
---|
76 | * added stuff for running with twenex/inputl 9 Oct 1984. |
---|
77 | * |
---|
78 | * ported to Apple Unix (TM) (OREO) 26 -- 29 Jun 1987 |
---|
79 | */ |
---|
80 | |
---|
81 | jmp_buf_t reslab INIT_ZERO_STRUCT; |
---|
82 | |
---|
83 | static const char tcshstr[] = "tcsh"; |
---|
84 | #ifdef WINNT_NATIVE |
---|
85 | static const char tcshstr_nt[] = "tcsh.exe"; |
---|
86 | #endif /* WINNT_NATIVE */ |
---|
87 | |
---|
88 | signalfun_t parintr = 0; /* Parents interrupt catch */ |
---|
89 | signalfun_t parterm = 0; /* Parents terminate catch */ |
---|
90 | |
---|
91 | #ifdef TESLA |
---|
92 | int do_logout = 0; |
---|
93 | #endif /* TESLA */ |
---|
94 | |
---|
95 | |
---|
96 | int use_fork = 0; /* use fork() instead of vfork()? */ |
---|
97 | |
---|
98 | /* |
---|
99 | * Magic pointer values. Used to specify other invalid conditions aside |
---|
100 | * from null. |
---|
101 | */ |
---|
102 | static Char INVCHAR; |
---|
103 | Char *INVPTR = &INVCHAR; |
---|
104 | Char **INVPPTR = &INVPTR; |
---|
105 | |
---|
106 | static int nofile = 0; |
---|
107 | static int reenter = 0; |
---|
108 | static int nverbose = 0; |
---|
109 | static int nexececho = 0; |
---|
110 | static int quitit = 0; |
---|
111 | static int rdirs = 0; |
---|
112 | int fast = 0; |
---|
113 | static int batch = 0; |
---|
114 | static int mflag = 0; |
---|
115 | static int prompt = 1; |
---|
116 | int enterhist = 0; |
---|
117 | int tellwhat = 0; |
---|
118 | time_t t_period; |
---|
119 | Char *ffile = NULL; |
---|
120 | int dolzero = 0; |
---|
121 | int insource = 0; |
---|
122 | int exitset = 0; |
---|
123 | static time_t chktim; /* Time mail last checked */ |
---|
124 | char *progname; |
---|
125 | int tcsh; |
---|
126 | extern char **environ; |
---|
127 | |
---|
128 | /* |
---|
129 | * This preserves the input state of the shell. It is used by |
---|
130 | * st_save and st_restore to manupulate shell state. |
---|
131 | */ |
---|
132 | struct saved_state { |
---|
133 | int insource; |
---|
134 | int OLDSTD; |
---|
135 | int SHIN; |
---|
136 | int SHOUT; |
---|
137 | int SHDIAG; |
---|
138 | int intty; |
---|
139 | struct whyle *whyles; |
---|
140 | Char *gointr; |
---|
141 | Char *arginp; |
---|
142 | Char *evalp; |
---|
143 | Char **evalvec; |
---|
144 | Char *alvecp; |
---|
145 | Char **alvec; |
---|
146 | int onelflg; |
---|
147 | int enterhist; |
---|
148 | Char **argv; |
---|
149 | Char HIST; |
---|
150 | int cantell; |
---|
151 | struct Bin B; |
---|
152 | /* These keep signal state and setjump state */ |
---|
153 | #ifdef BSDSIGS |
---|
154 | sigmask_t mask; |
---|
155 | #endif |
---|
156 | jmp_buf_t oldexit; |
---|
157 | int reenter; |
---|
158 | }; |
---|
159 | |
---|
160 | static int srccat __P((Char *, Char *)); |
---|
161 | #ifndef WINNT_NATIVE |
---|
162 | static int srcfile __P((const char *, int, int, Char **)); |
---|
163 | #else |
---|
164 | int srcfile __P((const char *, int, int, Char **)); |
---|
165 | #endif /*WINNT_NATIVE*/ |
---|
166 | static RETSIGTYPE phup __P((int)); |
---|
167 | static void srcunit __P((int, int, int, Char **)); |
---|
168 | static void mailchk __P((void)); |
---|
169 | #ifndef _PATH_DEFPATH |
---|
170 | static Char **defaultpath __P((void)); |
---|
171 | #endif |
---|
172 | static void record __P((void)); |
---|
173 | static void st_save __P((struct saved_state *, int, int, |
---|
174 | Char **, Char **)); |
---|
175 | static void st_restore __P((struct saved_state *, Char **)); |
---|
176 | |
---|
177 | int main __P((int, char **)); |
---|
178 | |
---|
179 | int |
---|
180 | main(argc, argv) |
---|
181 | int argc; |
---|
182 | char **argv; |
---|
183 | { |
---|
184 | Char *cp; |
---|
185 | #ifdef AUTOLOGOUT |
---|
186 | Char *cp2; |
---|
187 | #endif |
---|
188 | char *tcp, *ttyn; |
---|
189 | int f; |
---|
190 | char **tempv; |
---|
191 | int osetintr; |
---|
192 | signalfun_t oparintr; |
---|
193 | |
---|
194 | #ifdef BSDSIGS |
---|
195 | sigvec_t osv; |
---|
196 | #endif /* BSDSIGS */ |
---|
197 | |
---|
198 | #ifdef WINNT_NATIVE |
---|
199 | nt_init(); |
---|
200 | #endif /* WINNT_NATIVE */ |
---|
201 | #if defined(NLS_CATALOGS) && defined(LC_MESSAGES) |
---|
202 | (void) setlocale(LC_MESSAGES, ""); |
---|
203 | #endif /* NLS_CATALOGS && LC_MESSAGES */ |
---|
204 | |
---|
205 | #ifdef NLS |
---|
206 | # ifdef LC_CTYPE |
---|
207 | (void) setlocale(LC_CTYPE, ""); /* for iscntrl */ |
---|
208 | # endif /* LC_CTYPE */ |
---|
209 | #endif /* NLS */ |
---|
210 | |
---|
211 | nlsinit(); |
---|
212 | |
---|
213 | #ifdef MALLOC_TRACE |
---|
214 | mal_setstatsfile(fdopen(dmove(open("/tmp/tcsh.trace", |
---|
215 | O_WRONLY|O_CREAT|O_LARGEFILE, 0666), 25), "w")); |
---|
216 | mal_trace(1); |
---|
217 | #endif /* MALLOC_TRACE */ |
---|
218 | |
---|
219 | #if !(defined(BSDTIMES) || defined(_SEQUENT_)) && defined(POSIX) |
---|
220 | # ifdef _SC_CLK_TCK |
---|
221 | clk_tck = (clock_t) sysconf(_SC_CLK_TCK); |
---|
222 | # else /* ! _SC_CLK_TCK */ |
---|
223 | # ifdef CLK_TCK |
---|
224 | clk_tck = CLK_TCK; |
---|
225 | # else /* !CLK_TCK */ |
---|
226 | clk_tck = HZ; |
---|
227 | # endif /* CLK_TCK */ |
---|
228 | # endif /* _SC_CLK_TCK */ |
---|
229 | #endif /* !BSDTIMES && POSIX */ |
---|
230 | |
---|
231 | settimes(); /* Immed. estab. timing base */ |
---|
232 | #ifdef TESLA |
---|
233 | do_logout = 0; |
---|
234 | #endif /* TESLA */ |
---|
235 | |
---|
236 | /* |
---|
237 | * Make sure we have 0, 1, 2 open |
---|
238 | * Otherwise `` jobs will not work... (From knaff@poly.polytechnique.fr) |
---|
239 | */ |
---|
240 | { |
---|
241 | do |
---|
242 | if ((f = open(_PATH_DEVNULL, O_RDONLY|O_LARGEFILE)) == -1 && |
---|
243 | (f = open("/", O_RDONLY|O_LARGEFILE)) == -1) |
---|
244 | exit(1); |
---|
245 | while (f < 3); |
---|
246 | (void) close(f); |
---|
247 | } |
---|
248 | |
---|
249 | #ifdef O_TEXT |
---|
250 | setmode(0, O_TEXT); |
---|
251 | #endif |
---|
252 | |
---|
253 | osinit(); /* Os dependent initialization */ |
---|
254 | |
---|
255 | |
---|
256 | { |
---|
257 | char *t; |
---|
258 | |
---|
259 | t = strrchr(argv[0], '/'); |
---|
260 | #ifdef WINNT_NATIVE |
---|
261 | { |
---|
262 | char *s = strrchr(argv[0], '\\'); |
---|
263 | if (s) |
---|
264 | t = s; |
---|
265 | } |
---|
266 | #endif /* WINNT_NATIVE */ |
---|
267 | t = t ? t + 1 : argv[0]; |
---|
268 | if (*t == '-') t++; |
---|
269 | progname = strsave((t && *t) ? t : tcshstr); /* never want a null */ |
---|
270 | tcsh = strcmp(progname, tcshstr) == 0; |
---|
271 | } |
---|
272 | |
---|
273 | /* |
---|
274 | * Initialize non constant strings |
---|
275 | */ |
---|
276 | #ifdef _PATH_BSHELL |
---|
277 | STR_BSHELL = SAVE(_PATH_BSHELL); |
---|
278 | #endif |
---|
279 | #ifdef _PATH_TCSHELL |
---|
280 | STR_SHELLPATH = SAVE(_PATH_TCSHELL); |
---|
281 | #else |
---|
282 | # ifdef _PATH_CSHELL |
---|
283 | STR_SHELLPATH = SAVE(_PATH_CSHELL); |
---|
284 | # endif |
---|
285 | #endif |
---|
286 | STR_environ = blk2short(environ); |
---|
287 | environ = short2blk(STR_environ); /* So that we can free it */ |
---|
288 | STR_WORD_CHARS = SAVE(WORD_CHARS); |
---|
289 | |
---|
290 | HIST = '!'; |
---|
291 | HISTSUB = '^'; |
---|
292 | PRCH = '>'; |
---|
293 | PRCHROOT = '#'; |
---|
294 | word_chars = STR_WORD_CHARS; |
---|
295 | bslash_quote = 0; /* PWP: do tcsh-style backslash quoting? */ |
---|
296 | |
---|
297 | /* Default history size to 100 */ |
---|
298 | set(STRhistory, SAVE("100"), VAR_READWRITE); |
---|
299 | |
---|
300 | tempv = argv; |
---|
301 | ffile = SAVE(tempv[0]); |
---|
302 | dolzero = 0; |
---|
303 | if (eq(ffile, STRaout)) /* A.out's are quittable */ |
---|
304 | quitit = 1; |
---|
305 | uid = getuid(); |
---|
306 | gid = getgid(); |
---|
307 | euid = geteuid(); |
---|
308 | egid = getegid(); |
---|
309 | /* |
---|
310 | * We are a login shell if: 1. we were invoked as -<something> with |
---|
311 | * optional arguments 2. or we were invoked only with the -l flag |
---|
312 | */ |
---|
313 | loginsh = (**tempv == '-') || (argc == 2 && |
---|
314 | tempv[1][0] == '-' && tempv[1][1] == 'l' && |
---|
315 | tempv[1][2] == '\0'); |
---|
316 | #ifdef _VMS_POSIX |
---|
317 | /* No better way to find if we are a login shell */ |
---|
318 | if (!loginsh) { |
---|
319 | loginsh = (argc == 1 && getppid() == 1); |
---|
320 | **tempv = '-'; /* Avoid giving VMS an acidic stomach */ |
---|
321 | } |
---|
322 | #endif /* _VMS_POSIX */ |
---|
323 | |
---|
324 | if (loginsh && **tempv != '-') { |
---|
325 | /* |
---|
326 | * Mangle the argv space |
---|
327 | */ |
---|
328 | tempv[1][0] = '\0'; |
---|
329 | tempv[1][1] = '\0'; |
---|
330 | tempv[1] = NULL; |
---|
331 | for (tcp = *tempv; *tcp++;) |
---|
332 | continue; |
---|
333 | for (tcp--; tcp >= *tempv; tcp--) |
---|
334 | tcp[1] = tcp[0]; |
---|
335 | *++tcp = '-'; |
---|
336 | argc--; |
---|
337 | } |
---|
338 | if (loginsh) { |
---|
339 | (void) time(&chktim); |
---|
340 | set(STRloginsh, Strsave(STRNULL), VAR_READWRITE); |
---|
341 | } |
---|
342 | |
---|
343 | AsciiOnly = 1; |
---|
344 | NoNLSRebind = getenv("NOREBIND") != NULL; |
---|
345 | #ifdef NLS |
---|
346 | # ifdef SETLOCALEBUG |
---|
347 | dont_free = 1; |
---|
348 | # endif /* SETLOCALEBUG */ |
---|
349 | (void) setlocale(LC_ALL, ""); |
---|
350 | # ifdef LC_COLLATE |
---|
351 | (void) setlocale(LC_COLLATE, ""); |
---|
352 | # endif |
---|
353 | # ifdef SETLOCALEBUG |
---|
354 | dont_free = 0; |
---|
355 | # endif /* SETLOCALEBUG */ |
---|
356 | # ifdef STRCOLLBUG |
---|
357 | fix_strcoll_bug(); |
---|
358 | # endif /* STRCOLLBUG */ |
---|
359 | |
---|
360 | { |
---|
361 | int k; |
---|
362 | |
---|
363 | for (k = 0200; k <= 0377 && !Isprint(k); k++) |
---|
364 | continue; |
---|
365 | AsciiOnly = MB_CUR_MAX == 1 && k > 0377; |
---|
366 | } |
---|
367 | #else |
---|
368 | AsciiOnly = getenv("LANG") == NULL && getenv("LC_CTYPE") == NULL; |
---|
369 | #endif /* NLS */ |
---|
370 | if (MapsAreInited && !NLSMapsAreInited) |
---|
371 | ed_InitNLSMaps(); |
---|
372 | ResetArrowKeys(); |
---|
373 | |
---|
374 | /* |
---|
375 | * Initialize for periodic command intervals. Also, initialize the dummy |
---|
376 | * tty list for login-watch. |
---|
377 | */ |
---|
378 | (void) time(&t_period); |
---|
379 | #ifndef HAVENOUTMP |
---|
380 | initwatch(); |
---|
381 | #endif /* !HAVENOUTMP */ |
---|
382 | |
---|
383 | #if defined(alliant) |
---|
384 | /* |
---|
385 | * From: Jim Pace <jdp@research.att.com> |
---|
386 | * tcsh does not work properly on the alliants through an rlogin session. |
---|
387 | * The shell generally hangs. Also, reference to the controlling terminal |
---|
388 | * does not work ( ie: echo foo > /dev/tty ). |
---|
389 | * |
---|
390 | * A security feature was added to rlogind affecting FX/80's Concentrix |
---|
391 | * from revision 5.5.xx upwards (through 5.7 where this fix was implemented) |
---|
392 | * This security change also affects the FX/2800 series. |
---|
393 | * The security change to rlogind requires the process group of an rlogin |
---|
394 | * session become disassociated with the tty in rlogind. |
---|
395 | * |
---|
396 | * The changes needed are: |
---|
397 | * 1. set the process group |
---|
398 | * 2. reenable the control terminal |
---|
399 | */ |
---|
400 | if (loginsh && isatty(SHIN)) { |
---|
401 | ttyn = (char *) ttyname(SHIN); |
---|
402 | (void) close(SHIN); |
---|
403 | SHIN = open(ttyn, O_RDWR|O_LARGEFILE); |
---|
404 | shpgrp = getpid(); |
---|
405 | (void) ioctl (SHIN, TIOCSPGRP, (ioctl_t) &shpgrp); |
---|
406 | (void) setpgid(0, shpgrp); |
---|
407 | } |
---|
408 | #endif /* alliant */ |
---|
409 | |
---|
410 | /* |
---|
411 | * Move the descriptors to safe places. The variable didfds is 0 while we |
---|
412 | * have only FSH* to work with. When didfds is true, we have 0,1,2 and |
---|
413 | * prefer to use these. |
---|
414 | */ |
---|
415 | initdesc(); |
---|
416 | |
---|
417 | /* |
---|
418 | * Get and set the tty now |
---|
419 | */ |
---|
420 | if ((ttyn = ttyname(SHIN)) != NULL) { |
---|
421 | /* |
---|
422 | * Could use rindex to get rid of other possible path components, but |
---|
423 | * hpux preserves the subdirectory /pty/ when storing the tty name in |
---|
424 | * utmp, so we keep it too. |
---|
425 | */ |
---|
426 | if (strncmp(ttyn, "/dev/", 5) == 0) |
---|
427 | set(STRtty, cp = SAVE(ttyn + 5), VAR_READWRITE); |
---|
428 | else |
---|
429 | set(STRtty, cp = SAVE(ttyn), VAR_READWRITE); |
---|
430 | } |
---|
431 | else |
---|
432 | set(STRtty, cp = SAVE(""), VAR_READWRITE); |
---|
433 | /* |
---|
434 | * Initialize the shell variables. ARGV and PROMPT are initialized later. |
---|
435 | * STATUS is also munged in several places. CHILD is munged when |
---|
436 | * forking/waiting |
---|
437 | */ |
---|
438 | |
---|
439 | /* |
---|
440 | * 7-10-87 Paul Placeway autologout should be set ONLY on login shells and |
---|
441 | * on shells running as root. Out of these, autologout should NOT be set |
---|
442 | * for any psudo-terminals (this catches most window systems) and not for |
---|
443 | * any terminal running X windows. |
---|
444 | * |
---|
445 | * At Ohio State, we have had problems with a user having his X session |
---|
446 | * drop out from under him (on a Sun) because the shell in his master |
---|
447 | * xterm timed out and exited. |
---|
448 | * |
---|
449 | * Really, this should be done with a program external to the shell, that |
---|
450 | * watches for no activity (and NO running programs, such as dump) on a |
---|
451 | * terminal for a long peroid of time, and then SIGHUPS the shell on that |
---|
452 | * terminal. |
---|
453 | * |
---|
454 | * bugfix by Rich Salz <rsalz@PINEAPPLE.BBN.COM>: For root rsh things |
---|
455 | * allways first check to see if loginsh or really root, then do things |
---|
456 | * with ttyname() |
---|
457 | * |
---|
458 | * Also by Jean-Francois Lamy <lamy%ai.toronto.edu@RELAY.CS.NET>: check the |
---|
459 | * value of cp before using it! ("root can rsh too") |
---|
460 | * |
---|
461 | * PWP: keep the nested ifs; the order of the tests matters and a good |
---|
462 | * (smart) C compiler might re-arange things wrong. |
---|
463 | */ |
---|
464 | #ifdef AUTOLOGOUT |
---|
465 | # ifdef convex |
---|
466 | if (uid == 0) { |
---|
467 | /* root always has a 15 minute autologout */ |
---|
468 | set(STRautologout, Strsave(STRrootdefautologout), VAR_READWRITE); |
---|
469 | } |
---|
470 | else |
---|
471 | if (loginsh) |
---|
472 | /* users get autologout set to 0 */ |
---|
473 | set(STRautologout, Strsave(STR0), VAR_READWRITE); |
---|
474 | # else /* convex */ |
---|
475 | if (loginsh || (uid == 0)) { |
---|
476 | if (*cp) { |
---|
477 | /* only for login shells or root and we must have a tty */ |
---|
478 | if ((cp2 = Strrchr(cp, (Char) '/')) != NULL) { |
---|
479 | cp = cp2 + 1; |
---|
480 | } |
---|
481 | else |
---|
482 | cp2 = cp; |
---|
483 | if (!(((Strncmp(cp2, STRtty, 3) == 0) && Isalpha(cp2[3])) || |
---|
484 | ((Strncmp(cp, STRpts, 3) == 0) && cp[3] == '/'))) { |
---|
485 | if (getenv("DISPLAY") == NULL) { |
---|
486 | /* NOT on X window shells */ |
---|
487 | set(STRautologout, Strsave(STRdefautologout), |
---|
488 | VAR_READWRITE); |
---|
489 | } |
---|
490 | } |
---|
491 | } |
---|
492 | } |
---|
493 | # endif /* convex */ |
---|
494 | #endif /* AUTOLOGOUT */ |
---|
495 | |
---|
496 | (void) sigset(SIGALRM, alrmcatch); |
---|
497 | |
---|
498 | set(STRstatus, Strsave(STR0), VAR_READWRITE); |
---|
499 | |
---|
500 | /* |
---|
501 | * get and set machine specific environment variables |
---|
502 | */ |
---|
503 | getmachine(); |
---|
504 | |
---|
505 | |
---|
506 | /* |
---|
507 | * Publish the selected echo style |
---|
508 | */ |
---|
509 | #if ECHO_STYLE != BSD_ECHO |
---|
510 | if (tcsh) { |
---|
511 | # if ECHO_STYLE == NONE_ECHO |
---|
512 | set(STRecho_style, Strsave(STRnone), VAR_READWRITE); |
---|
513 | # endif /* ECHO_STYLE == NONE_ECHO */ |
---|
514 | # if ECHO_STYLE == SYSV_ECHO |
---|
515 | set(STRecho_style, Strsave(STRsysv), VAR_READWRITE); |
---|
516 | # endif /* ECHO_STYLE == SYSV_ECHO */ |
---|
517 | # if ECHO_STYLE == BOTH_ECHO |
---|
518 | set(STRecho_style, Strsave(STRboth), VAR_READWRITE); |
---|
519 | # endif /* ECHO_STYLE == BOTH_ECHO */ |
---|
520 | } else |
---|
521 | #endif /* ECHO_STYLE != BSD_ECHO */ |
---|
522 | set(STRecho_style, Strsave(STRbsd), VAR_READWRITE); |
---|
523 | |
---|
524 | /* |
---|
525 | * increment the shell level. |
---|
526 | */ |
---|
527 | shlvl(1); |
---|
528 | |
---|
529 | if ((tcp = getenv("HOME")) != NULL) { |
---|
530 | if (strlen(tcp) >= MAXPATHLEN) { |
---|
531 | struct passwd *pw; |
---|
532 | if ((pw = getpwuid(getuid())) != NULL) |
---|
533 | cp = quote(SAVE(pw->pw_dir)); |
---|
534 | else { |
---|
535 | tcp[MAXPATHLEN-1] = '\0'; |
---|
536 | cp = quote(SAVE(tcp)); |
---|
537 | } |
---|
538 | } else { |
---|
539 | cp = quote(SAVE(tcp)); |
---|
540 | } |
---|
541 | } else |
---|
542 | cp = NULL; |
---|
543 | |
---|
544 | if (cp == NULL) |
---|
545 | fast = 1; /* No home -> can't read scripts */ |
---|
546 | else |
---|
547 | set(STRhome, cp, VAR_READWRITE); |
---|
548 | |
---|
549 | dinit(cp); /* dinit thinks that HOME == cwd in a login |
---|
550 | * shell */ |
---|
551 | /* |
---|
552 | * Grab other useful things from the environment. Should we grab |
---|
553 | * everything?? |
---|
554 | */ |
---|
555 | { |
---|
556 | char *cln, *cus, *cgr; |
---|
557 | Char buff[BUFSIZE]; |
---|
558 | struct passwd *pw; |
---|
559 | struct group *gr; |
---|
560 | |
---|
561 | |
---|
562 | #ifdef apollo |
---|
563 | int oid = getoid(); |
---|
564 | |
---|
565 | (void) Itoa(oid, buff, 0, 0); |
---|
566 | set(STRoid, Strsave(buff), VAR_READWRITE); |
---|
567 | #endif /* apollo */ |
---|
568 | |
---|
569 | (void) Itoa(uid, buff, 0, 0); |
---|
570 | set(STRuid, Strsave(buff), VAR_READWRITE); |
---|
571 | |
---|
572 | (void) Itoa(gid, buff, 0, 0); |
---|
573 | set(STRgid, Strsave(buff), VAR_READWRITE); |
---|
574 | |
---|
575 | cln = getenv("LOGNAME"); |
---|
576 | cus = getenv("USER"); |
---|
577 | if (cus != NULL) |
---|
578 | set(STRuser, quote(SAVE(cus)), VAR_READWRITE); |
---|
579 | else if (cln != NULL) |
---|
580 | set(STRuser, quote(SAVE(cln)), VAR_READWRITE); |
---|
581 | else if ((pw = getpwuid(uid)) == NULL) |
---|
582 | set(STRuser, SAVE("unknown"), VAR_READWRITE); |
---|
583 | else |
---|
584 | set(STRuser, SAVE(pw->pw_name), VAR_READWRITE); |
---|
585 | if (cln == NULL) |
---|
586 | tsetenv(STRLOGNAME, varval(STRuser)); |
---|
587 | if (cus == NULL) |
---|
588 | tsetenv(STRKUSER, varval(STRuser)); |
---|
589 | |
---|
590 | cgr = getenv("GROUP"); |
---|
591 | if (cgr != NULL) |
---|
592 | set(STRgroup, quote(SAVE(cgr)), VAR_READWRITE); |
---|
593 | else if ((gr = getgrgid(gid)) == NULL) |
---|
594 | set(STRgroup, SAVE("unknown"), VAR_READWRITE); |
---|
595 | else |
---|
596 | set(STRgroup, SAVE(gr->gr_name), VAR_READWRITE); |
---|
597 | if (cgr == NULL) |
---|
598 | tsetenv(STRKGROUP, varval(STRgroup)); |
---|
599 | } |
---|
600 | |
---|
601 | /* |
---|
602 | * HOST may be wrong, since rexd transports the entire environment on sun |
---|
603 | * 3.x Just set it again |
---|
604 | */ |
---|
605 | { |
---|
606 | char cbuff[MAXHOSTNAMELEN]; |
---|
607 | |
---|
608 | if (gethostname(cbuff, sizeof(cbuff)) >= 0) { |
---|
609 | cbuff[sizeof(cbuff) - 1] = '\0'; /* just in case */ |
---|
610 | tsetenv(STRHOST, str2short(cbuff)); |
---|
611 | } |
---|
612 | else |
---|
613 | tsetenv(STRHOST, str2short("unknown")); |
---|
614 | } |
---|
615 | |
---|
616 | |
---|
617 | #ifdef REMOTEHOST |
---|
618 | /* |
---|
619 | * Try to determine the remote host we were logged in from. |
---|
620 | */ |
---|
621 | remotehost(); |
---|
622 | #endif /* REMOTEHOST */ |
---|
623 | |
---|
624 | #ifdef apollo |
---|
625 | if ((tcp = getenv("SYSTYPE")) == NULL) |
---|
626 | tcp = "bsd4.3"; |
---|
627 | tsetenv(STRSYSTYPE, quote(str2short(tcp))); |
---|
628 | #endif /* apollo */ |
---|
629 | |
---|
630 | /* |
---|
631 | * set editing on by default, unless running under Emacs as an inferior |
---|
632 | * shell. |
---|
633 | * We try to do this intelligently. If $TERM is available, then it |
---|
634 | * should determine if we should edit or not. $TERM is preserved |
---|
635 | * across rlogin sessions, so we will not get confused if we rlogin |
---|
636 | * under an emacs shell. Another advantage is that if we run an |
---|
637 | * xterm under an emacs shell, then the $TERM will be set to |
---|
638 | * xterm, so we are going to want to edit. Unfortunately emacs |
---|
639 | * does not restore all the tty modes, so xterm is not very well |
---|
640 | * set up. But this is not the shell's fault. |
---|
641 | * Also don't edit if $TERM == wm, for when we're running under an ATK app. |
---|
642 | * Finally, emacs compiled under terminfo, sets the terminal to dumb, |
---|
643 | * so disable editing for that too. |
---|
644 | * |
---|
645 | * Unfortunately, in some cases the initial $TERM setting is "unknown", |
---|
646 | * "dumb", or "network" which is then changed in the user's startup files. |
---|
647 | * We fix this by setting noediting here if $TERM is unknown/dumb and |
---|
648 | * if noediting is set, we switch on editing if $TERM is changed. |
---|
649 | */ |
---|
650 | if ((tcp = getenv("TERM")) != NULL) { |
---|
651 | set(STRterm, quote(SAVE(tcp)), VAR_READWRITE); |
---|
652 | noediting = strcmp(tcp, "unknown") == 0 || strcmp(tcp, "dumb") == 0 || |
---|
653 | strcmp(tcp, "network") == 0; |
---|
654 | editing = strcmp(tcp, "emacs") != 0 && strcmp(tcp, "wm") != 0 && |
---|
655 | !noediting; |
---|
656 | } |
---|
657 | else { |
---|
658 | noediting = 0; |
---|
659 | editing = ((tcp = getenv("EMACS")) == NULL || strcmp(tcp, "t") != 0); |
---|
660 | } |
---|
661 | |
---|
662 | /* |
---|
663 | * The 'edit' variable is either set or unset. It doesn't |
---|
664 | * need a value. Making it 'emacs' might be confusing. |
---|
665 | */ |
---|
666 | if (editing) |
---|
667 | set(STRedit, Strsave(STRNULL), VAR_READWRITE); |
---|
668 | |
---|
669 | |
---|
670 | /* |
---|
671 | * still more mutability: make the complete routine automatically add the |
---|
672 | * suffix of file names... |
---|
673 | */ |
---|
674 | set(STRaddsuffix, Strsave(STRNULL), VAR_READWRITE); |
---|
675 | |
---|
676 | /* |
---|
677 | * Compatibility with tcsh >= 6.12 by default |
---|
678 | */ |
---|
679 | set(STRcsubstnonl, Strsave(STRNULL), VAR_READWRITE); |
---|
680 | |
---|
681 | /* |
---|
682 | * Random default kill ring size |
---|
683 | */ |
---|
684 | set(STRkillring, SAVE("30"), VAR_READWRITE); |
---|
685 | |
---|
686 | /* |
---|
687 | * Re-initialize path if set in environment |
---|
688 | */ |
---|
689 | if ((tcp = getenv("PATH")) == NULL) |
---|
690 | #ifdef _PATH_DEFPATH |
---|
691 | importpath(str2short(_PATH_DEFPATH)); |
---|
692 | #else /* !_PATH_DEFPATH */ |
---|
693 | setq(STRpath, defaultpath(), &shvhed, VAR_READWRITE); |
---|
694 | #endif /* _PATH_DEFPATH */ |
---|
695 | else |
---|
696 | /* Importpath() allocates memory for the path, and the |
---|
697 | * returned pointer from SAVE() was discarded, so |
---|
698 | * this was a memory leak.. (sg) |
---|
699 | * |
---|
700 | * importpath(SAVE(tcp)); |
---|
701 | */ |
---|
702 | importpath(str2short(tcp)); |
---|
703 | |
---|
704 | |
---|
705 | { |
---|
706 | /* If the SHELL environment variable ends with "tcsh", set |
---|
707 | * STRshell to the same path. This is to facilitate using |
---|
708 | * the executable in environments where the compiled-in |
---|
709 | * default isn't appropriate (sg). |
---|
710 | */ |
---|
711 | |
---|
712 | int sh_len = 0; |
---|
713 | |
---|
714 | if ((tcp = getenv("SHELL")) != NULL) { |
---|
715 | sh_len = strlen(tcp); |
---|
716 | if ((sh_len >= 5 && strcmp(tcp + (sh_len - 5), "/tcsh") == 0) || |
---|
717 | (!tcsh && sh_len >= 4 && strcmp(tcp + (sh_len - 4), "/csh") == 0)) |
---|
718 | set(STRshell, quote(SAVE(tcp)), VAR_READWRITE); |
---|
719 | else |
---|
720 | sh_len = 0; |
---|
721 | } |
---|
722 | if (sh_len == 0) |
---|
723 | set(STRshell, Strsave(STR_SHELLPATH), VAR_READWRITE); |
---|
724 | } |
---|
725 | |
---|
726 | #ifdef COLOR_LS_F |
---|
727 | if ((tcp = getenv("LS_COLORS")) != NULL) |
---|
728 | parseLS_COLORS(str2short(tcp)); |
---|
729 | #endif /* COLOR_LS_F */ |
---|
730 | |
---|
731 | doldol = putn((int) getpid()); /* For $$ */ |
---|
732 | #ifdef WINNT_NATIVE |
---|
733 | { |
---|
734 | char *strtmp1, strtmp2[MAXPATHLEN]; |
---|
735 | if ((strtmp1 = getenv("TMP")) != NULL) |
---|
736 | wsprintf(strtmp2, "%s/%s", strtmp1, "sh"); |
---|
737 | shtemp = Strspl(SAVE(strtmp2), doldol); /* For << */ |
---|
738 | } |
---|
739 | #else /* !WINNT_NATIVE */ |
---|
740 | shtemp = Strspl(STRtmpsh, doldol); /* For << */ |
---|
741 | #endif /* WINNT_NATIVE */ |
---|
742 | |
---|
743 | /* |
---|
744 | * Record the interrupt states from the parent process. If the parent is |
---|
745 | * non-interruptible our hand must be forced or we (and our children) won't |
---|
746 | * be either. Our children inherit termination from our parent. We catch it |
---|
747 | * only if we are the login shell. |
---|
748 | */ |
---|
749 | #ifdef BSDSIGS |
---|
750 | /* |
---|
751 | * PURIFY-2 claims that osv does not get |
---|
752 | * initialized after the sigvec call |
---|
753 | */ |
---|
754 | setzero((char*) &osv, sizeof(osv)); |
---|
755 | /* parents interruptibility */ |
---|
756 | (void) mysigvec(SIGINT, NULL, &osv); |
---|
757 | parintr = (signalfun_t) osv.sv_handler; |
---|
758 | (void) mysigvec(SIGTERM, NULL, &osv); |
---|
759 | parterm = (signalfun_t) osv.sv_handler; |
---|
760 | #else /* BSDSIGS */ |
---|
761 | parintr = signal(SIGINT, SIG_IGN); /* parents interruptibility */ |
---|
762 | (void) sigset(SIGINT, parintr); /* ... restore */ |
---|
763 | |
---|
764 | # ifdef COHERENT |
---|
765 | if (loginsh) /* it seems that SIGTERM is always set to SIG_IGN by */ |
---|
766 | /* init/getty so it should be set to SIG_DFL - there may be */ |
---|
767 | /* a better fix for this. */ |
---|
768 | parterm = SIG_DFL; |
---|
769 | else |
---|
770 | # else /* !COHERENT */ |
---|
771 | parterm = signal(SIGTERM, SIG_IGN); /* parents terminability */ |
---|
772 | # endif /* COHERENT */ |
---|
773 | (void) sigset(SIGTERM, parterm); /* ... restore */ |
---|
774 | |
---|
775 | #endif /* BSDSIGS */ |
---|
776 | |
---|
777 | |
---|
778 | #ifdef TCF |
---|
779 | /* Enable process migration on ourselves and our progeny */ |
---|
780 | (void) signal(SIGMIGRATE, SIG_DFL); |
---|
781 | #endif /* TCF */ |
---|
782 | |
---|
783 | /* |
---|
784 | * dspkanji/dspmbyte autosetting |
---|
785 | */ |
---|
786 | /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */ |
---|
787 | #if defined(DSPMBYTE) |
---|
788 | #if defined(NLS) && defined(LC_CTYPE) |
---|
789 | if (((tcp = setlocale(LC_CTYPE, NULL)) != NULL || (tcp = getenv("LANG")) != NULL) && !adrof(CHECK_MBYTEVAR)) { |
---|
790 | #else |
---|
791 | if ((tcp = getenv("LANG")) != NULL && !adrof(CHECK_MBYTEVAR)) { |
---|
792 | #endif |
---|
793 | autoset_dspmbyte(str2short(tcp)); |
---|
794 | } |
---|
795 | #if defined(WINNT_NATIVE) |
---|
796 | else if (!adrof(CHECK_MBYTEVAR)) |
---|
797 | nt_autoset_dspmbyte(); |
---|
798 | #endif /* WINNT_NATIVE */ |
---|
799 | #endif |
---|
800 | |
---|
801 | fix_version(); /* publish the shell version */ |
---|
802 | |
---|
803 | if (argc > 1 && strcmp(argv[1], "--version") == 0) { |
---|
804 | xprintf("%S\n", varval(STRversion)); |
---|
805 | xexit(0); |
---|
806 | } |
---|
807 | if (argc > 1 && strcmp(argv[1], "--help") == 0) { |
---|
808 | xprintf("%S\n\n", varval(STRversion)); |
---|
809 | xprintf(CGETS(11, 8, HELP_STRING)); |
---|
810 | xexit(0); |
---|
811 | } |
---|
812 | /* |
---|
813 | * Process the arguments. |
---|
814 | * |
---|
815 | * Note that processing of -v/-x is actually delayed till after script |
---|
816 | * processing. |
---|
817 | * |
---|
818 | * We set the first character of our name to be '-' if we are a shell |
---|
819 | * running interruptible commands. Many programs which examine ps'es |
---|
820 | * use this to filter such shells out. |
---|
821 | */ |
---|
822 | argc--, tempv++; |
---|
823 | while (argc > 0 && (tcp = tempv[0])[0] == '-' && |
---|
824 | *++tcp != '\0' && !batch) { |
---|
825 | do |
---|
826 | switch (*tcp++) { |
---|
827 | |
---|
828 | case 0: /* - Interruptible, no prompt */ |
---|
829 | prompt = 0; |
---|
830 | setintr = 1; |
---|
831 | nofile = 1; |
---|
832 | break; |
---|
833 | |
---|
834 | case 'b': /* -b Next arg is input file */ |
---|
835 | batch = 1; |
---|
836 | break; |
---|
837 | |
---|
838 | case 'c': /* -c Command input from arg */ |
---|
839 | if (argc == 1) |
---|
840 | xexit(0); |
---|
841 | argc--, tempv++; |
---|
842 | #ifdef M_XENIX |
---|
843 | /* Xenix Vi bug: |
---|
844 | it relies on a 7 bit environment (/bin/sh), so it |
---|
845 | pass ascii arguments with the 8th bit set */ |
---|
846 | if (!strcmp(argv[0], "sh")) |
---|
847 | { |
---|
848 | char *p; |
---|
849 | |
---|
850 | for (p = tempv[0]; *p; ++p) |
---|
851 | *p &= ASCII; |
---|
852 | } |
---|
853 | #endif |
---|
854 | arginp = SAVE(tempv[0]); |
---|
855 | |
---|
856 | /* |
---|
857 | * we put the command into a variable |
---|
858 | */ |
---|
859 | if (arginp != NULL) |
---|
860 | set(STRcommand, quote(Strsave(arginp)), VAR_READWRITE); |
---|
861 | |
---|
862 | /* |
---|
863 | * * Give an error on -c arguments that end in * backslash to |
---|
864 | * ensure that you don't make * nonportable csh scripts. |
---|
865 | */ |
---|
866 | { |
---|
867 | int count; |
---|
868 | |
---|
869 | cp = arginp + Strlen(arginp); |
---|
870 | count = 0; |
---|
871 | while (cp > arginp && *--cp == '\\') |
---|
872 | ++count; |
---|
873 | if ((count & 1) != 0) { |
---|
874 | exiterr = 1; |
---|
875 | stderror(ERR_ARGC); |
---|
876 | } |
---|
877 | } |
---|
878 | prompt = 0; |
---|
879 | nofile = 1; |
---|
880 | break; |
---|
881 | case 'd': /* -d Load directory stack from file */ |
---|
882 | rdirs = 1; |
---|
883 | break; |
---|
884 | |
---|
885 | #ifdef apollo |
---|
886 | case 'D': /* -D Define environment variable */ |
---|
887 | { |
---|
888 | Char *dp; |
---|
889 | |
---|
890 | cp = str2short(tcp); |
---|
891 | if (dp = Strchr(cp, '=')) { |
---|
892 | *dp++ = '\0'; |
---|
893 | tsetenv(cp, dp); |
---|
894 | } |
---|
895 | else |
---|
896 | tsetenv(cp, STRNULL); |
---|
897 | } |
---|
898 | *tcp = '\0'; /* done with this argument */ |
---|
899 | break; |
---|
900 | #endif /* apollo */ |
---|
901 | |
---|
902 | case 'e': /* -e Exit on any error */ |
---|
903 | exiterr = 1; |
---|
904 | break; |
---|
905 | |
---|
906 | case 'f': /* -f Fast start */ |
---|
907 | fast = 1; |
---|
908 | break; |
---|
909 | |
---|
910 | case 'i': /* -i Interactive, even if !intty */ |
---|
911 | intact = 1; |
---|
912 | nofile = 1; |
---|
913 | break; |
---|
914 | |
---|
915 | case 'm': /* -m read .cshrc (from su) */ |
---|
916 | mflag = 1; |
---|
917 | break; |
---|
918 | |
---|
919 | case 'n': /* -n Don't execute */ |
---|
920 | noexec = 1; |
---|
921 | break; |
---|
922 | |
---|
923 | case 'q': /* -q (Undoc'd) ... die on quit */ |
---|
924 | quitit = 1; |
---|
925 | break; |
---|
926 | |
---|
927 | case 's': /* -s Read from std input */ |
---|
928 | nofile = 1; |
---|
929 | break; |
---|
930 | |
---|
931 | case 't': /* -t Read one line from input */ |
---|
932 | onelflg = 2; |
---|
933 | prompt = 0; |
---|
934 | nofile = 1; |
---|
935 | break; |
---|
936 | |
---|
937 | case 'v': /* -v Echo hist expanded input */ |
---|
938 | nverbose = 1; /* ... later */ |
---|
939 | break; |
---|
940 | |
---|
941 | case 'x': /* -x Echo just before execution */ |
---|
942 | nexececho = 1; /* ... later */ |
---|
943 | break; |
---|
944 | |
---|
945 | case 'V': /* -V Echo hist expanded input */ |
---|
946 | setNS(STRverbose); /* NOW! */ |
---|
947 | break; |
---|
948 | |
---|
949 | case 'X': /* -X Echo just before execution */ |
---|
950 | setNS(STRecho); /* NOW! */ |
---|
951 | break; |
---|
952 | |
---|
953 | case 'F': /* Undocumented flag */ |
---|
954 | /* |
---|
955 | * This will cause children to be created using fork instead of |
---|
956 | * vfork. |
---|
957 | */ |
---|
958 | use_fork = 1; |
---|
959 | break; |
---|
960 | |
---|
961 | case ' ': |
---|
962 | case '\t': |
---|
963 | /* |
---|
964 | * for O/S's that don't do the argument parsing right in |
---|
965 | * "#!/foo -f " scripts |
---|
966 | */ |
---|
967 | break; |
---|
968 | |
---|
969 | default: /* Unknown command option */ |
---|
970 | exiterr = 1; |
---|
971 | stderror(ERR_TCSHUSAGE, tcp-1, progname); |
---|
972 | break; |
---|
973 | |
---|
974 | } while (*tcp); |
---|
975 | tempv++, argc--; |
---|
976 | } |
---|
977 | |
---|
978 | if (quitit) /* With all due haste, for debugging */ |
---|
979 | (void) signal(SIGQUIT, SIG_DFL); |
---|
980 | |
---|
981 | /* |
---|
982 | * Unless prevented by -, -c, -i, -s, or -t, if there are remaining |
---|
983 | * arguments the first of them is the name of a shell file from which to |
---|
984 | * read commands. |
---|
985 | */ |
---|
986 | if (nofile == 0 && argc > 0) { |
---|
987 | nofile = open(tempv[0], O_RDONLY|O_LARGEFILE); |
---|
988 | if (nofile < 0) { |
---|
989 | child = 1; /* So this ... */ |
---|
990 | /* ... doesn't return */ |
---|
991 | stderror(ERR_SYSTEM, tempv[0], strerror(errno)); |
---|
992 | } |
---|
993 | #ifdef O_TEXT |
---|
994 | setmode(nofile, O_TEXT); |
---|
995 | #endif |
---|
996 | if (ffile != NULL) |
---|
997 | xfree((ptr_t) ffile); |
---|
998 | dolzero = 1; |
---|
999 | ffile = SAVE(tempv[0]); |
---|
1000 | /* |
---|
1001 | * Replace FSHIN. Handle /dev/std{in,out,err} specially |
---|
1002 | * since once they are closed we cannot open them again. |
---|
1003 | * In that case we use our own saved descriptors |
---|
1004 | */ |
---|
1005 | if ((SHIN = dmove(nofile, FSHIN)) < 0) |
---|
1006 | switch(nofile) { |
---|
1007 | case 0: |
---|
1008 | SHIN = FSHIN; |
---|
1009 | break; |
---|
1010 | case 1: |
---|
1011 | SHIN = FSHOUT; |
---|
1012 | break; |
---|
1013 | case 2: |
---|
1014 | SHIN = FSHDIAG; |
---|
1015 | break; |
---|
1016 | default: |
---|
1017 | stderror(ERR_SYSTEM, tempv[0], strerror(errno)); |
---|
1018 | break; |
---|
1019 | } |
---|
1020 | (void) close_on_exec(SHIN, 1); |
---|
1021 | prompt = 0; |
---|
1022 | /* argc not used any more */ tempv++; |
---|
1023 | } |
---|
1024 | |
---|
1025 | /* |
---|
1026 | * Call to closem() used to be part of initdesc(). Now called below where |
---|
1027 | * the script name argument has become stdin. Kernel may have used a file |
---|
1028 | * descriptor to hold the name of the script (setuid case) and this name |
---|
1029 | * mustn't be lost by closing the fd too soon. |
---|
1030 | */ |
---|
1031 | closem(); |
---|
1032 | |
---|
1033 | /* |
---|
1034 | * Consider input a tty if it really is or we are interactive. but not for |
---|
1035 | * editing (christos) |
---|
1036 | */ |
---|
1037 | if (!(intty = isatty(SHIN))) { |
---|
1038 | if (adrof(STRedit)) |
---|
1039 | unsetv(STRedit); |
---|
1040 | editing = 0; |
---|
1041 | } |
---|
1042 | intty |= intact; |
---|
1043 | #ifndef convex |
---|
1044 | if (intty || (intact && isatty(SHOUT))) { |
---|
1045 | if (!batch && (uid != euid || gid != egid)) { |
---|
1046 | errno = EACCES; |
---|
1047 | child = 1; /* So this ... */ |
---|
1048 | /* ... doesn't return */ |
---|
1049 | stderror(ERR_SYSTEM, progname, strerror(errno)); |
---|
1050 | } |
---|
1051 | } |
---|
1052 | #endif /* convex */ |
---|
1053 | isoutatty = isatty(SHOUT); |
---|
1054 | isdiagatty = isatty(SHDIAG); |
---|
1055 | /* |
---|
1056 | * Decide whether we should play with signals or not. If we are explicitly |
---|
1057 | * told (via -i, or -) or we are a login shell (arg0 starts with -) or the |
---|
1058 | * input and output are both the ttys("csh", or "csh</dev/ttyx>/dev/ttyx") |
---|
1059 | * Note that in only the login shell is it likely that parent may have set |
---|
1060 | * signals to be ignored |
---|
1061 | */ |
---|
1062 | if (loginsh || intact || (intty && isatty(SHOUT))) |
---|
1063 | setintr = 1; |
---|
1064 | settell(); |
---|
1065 | /* |
---|
1066 | * Save the remaining arguments in argv. |
---|
1067 | */ |
---|
1068 | setq(STRargv, blk2short(tempv), &shvhed, VAR_READWRITE); |
---|
1069 | |
---|
1070 | /* |
---|
1071 | * Set up the prompt. |
---|
1072 | */ |
---|
1073 | if (prompt) { |
---|
1074 | if (tcsh) |
---|
1075 | set(STRprompt, Strsave(STRdeftcshprompt), VAR_READWRITE); |
---|
1076 | else |
---|
1077 | set(STRprompt, Strsave(STRdefcshprompt), VAR_READWRITE); |
---|
1078 | /* that's a meta-questionmark */ |
---|
1079 | set(STRprompt2, Strsave(STRmquestion), VAR_READWRITE); |
---|
1080 | set(STRprompt3, Strsave(STRKCORRECT), VAR_READWRITE); |
---|
1081 | } |
---|
1082 | |
---|
1083 | /* |
---|
1084 | * If we are an interactive shell, then start fiddling with the signals; |
---|
1085 | * this is a tricky game. |
---|
1086 | */ |
---|
1087 | shpgrp = mygetpgrp(); |
---|
1088 | opgrp = tpgrp = -1; |
---|
1089 | if (setintr) { |
---|
1090 | signalfun_t osig; |
---|
1091 | **argv = '-'; |
---|
1092 | if (!quitit) /* Wary! */ |
---|
1093 | (void) signal(SIGQUIT, SIG_IGN); |
---|
1094 | (void) sigset(SIGINT, pintr); |
---|
1095 | (void) sighold(SIGINT); |
---|
1096 | (void) signal(SIGTERM, SIG_IGN); |
---|
1097 | |
---|
1098 | /* |
---|
1099 | * No reason I can see not to save history on all these events.. |
---|
1100 | * Most usual occurrence is in a window system, where we're not a login |
---|
1101 | * shell, but might as well be... (sg) |
---|
1102 | * But there might be races when lots of shells exit together... |
---|
1103 | * [this is also incompatible]. |
---|
1104 | * We have to be mre careful here. If the parent wants to |
---|
1105 | * ignore the signals then we leave them untouched... |
---|
1106 | * We also only setup the handlers for shells that are trully |
---|
1107 | * interactive. |
---|
1108 | */ |
---|
1109 | osig = signal(SIGHUP, phup); /* exit processing on HUP */ |
---|
1110 | if (!loginsh && osig == SIG_IGN) |
---|
1111 | (void) signal(SIGHUP, osig); |
---|
1112 | #ifdef SIGXCPU |
---|
1113 | osig = signal(SIGXCPU, phup); /* exit processing on XCPU */ |
---|
1114 | if (!loginsh && osig == SIG_IGN) |
---|
1115 | (void) signal(SIGXCPU, osig); |
---|
1116 | #endif |
---|
1117 | #ifdef SIGXFSZ |
---|
1118 | osig = signal(SIGXFSZ, phup); /* exit processing on XFSZ */ |
---|
1119 | if (!loginsh && osig == SIG_IGN) |
---|
1120 | (void) signal(SIGXFSZ, osig); |
---|
1121 | #endif |
---|
1122 | |
---|
1123 | if (quitit == 0 && arginp == 0) { |
---|
1124 | #ifdef SIGTSTP |
---|
1125 | (void) signal(SIGTSTP, SIG_IGN); |
---|
1126 | #endif |
---|
1127 | #ifdef SIGTTIN |
---|
1128 | (void) signal(SIGTTIN, SIG_IGN); |
---|
1129 | #endif |
---|
1130 | #ifdef SIGTTOU |
---|
1131 | (void) signal(SIGTTOU, SIG_IGN); |
---|
1132 | #endif |
---|
1133 | /* |
---|
1134 | * Wait till in foreground, in case someone stupidly runs csh & |
---|
1135 | * dont want to try to grab away the tty. |
---|
1136 | */ |
---|
1137 | if (isatty(FSHDIAG)) |
---|
1138 | f = FSHDIAG; |
---|
1139 | else if (isatty(FSHOUT)) |
---|
1140 | f = FSHOUT; |
---|
1141 | else if (isatty(OLDSTD)) |
---|
1142 | f = OLDSTD; |
---|
1143 | else |
---|
1144 | f = -1; |
---|
1145 | |
---|
1146 | #ifdef NeXT |
---|
1147 | /* NeXT 2.0 /usr/etc/rlogind, does not set our process group! */ |
---|
1148 | if (shpgrp == 0) { |
---|
1149 | shpgrp = getpid(); |
---|
1150 | (void) setpgid(0, shpgrp); |
---|
1151 | (void) tcsetpgrp(f, shpgrp); |
---|
1152 | } |
---|
1153 | #endif /* NeXT */ |
---|
1154 | #ifdef BSDJOBS /* if we have tty job control */ |
---|
1155 | retry: |
---|
1156 | if ((tpgrp = tcgetpgrp(f)) != -1) { |
---|
1157 | if (tpgrp != shpgrp) { |
---|
1158 | signalfun_t old = signal(SIGTTIN, SIG_DFL); |
---|
1159 | (void) kill(0, SIGTTIN); |
---|
1160 | (void) signal(SIGTTIN, old); |
---|
1161 | goto retry; |
---|
1162 | } |
---|
1163 | /* |
---|
1164 | * Thanks to Matt Day for the POSIX references, and to |
---|
1165 | * Paul Close for the SGI clarification. |
---|
1166 | */ |
---|
1167 | if (setdisc(f) != -1) { |
---|
1168 | opgrp = shpgrp; |
---|
1169 | shpgrp = getpid(); |
---|
1170 | tpgrp = shpgrp; |
---|
1171 | if (tcsetpgrp(f, shpgrp) == -1) { |
---|
1172 | /* |
---|
1173 | * On hpux 7.03 this fails with EPERM. This happens on |
---|
1174 | * the 800 when opgrp != shpgrp at this point. (we were |
---|
1175 | * forked from a non job control shell) |
---|
1176 | * POSIX 7.2.4, says we failed because the process |
---|
1177 | * group specified did not belong to a process |
---|
1178 | * in the same session with the tty. So we set our |
---|
1179 | * process group and try again. |
---|
1180 | */ |
---|
1181 | if (setpgid(0, shpgrp) == -1) { |
---|
1182 | xprintf("setpgid:"); |
---|
1183 | goto notty; |
---|
1184 | } |
---|
1185 | if (tcsetpgrp(f, shpgrp) == -1) { |
---|
1186 | xprintf("tcsetpgrp:"); |
---|
1187 | goto notty; |
---|
1188 | } |
---|
1189 | } |
---|
1190 | /* |
---|
1191 | * We check the process group now. If it is the same, then |
---|
1192 | * we don't need to set it again. On hpux 7.0 on the 300's |
---|
1193 | * if we set it again it fails with EPERM. This is the |
---|
1194 | * correct behavior according to POSIX 4.3.3 if the process |
---|
1195 | * was a session leader . |
---|
1196 | */ |
---|
1197 | else if (shpgrp != mygetpgrp()) { |
---|
1198 | if(setpgid(0, shpgrp) == -1) { |
---|
1199 | xprintf("setpgid:"); |
---|
1200 | goto notty; |
---|
1201 | } |
---|
1202 | } |
---|
1203 | #ifdef IRIS4D |
---|
1204 | /* |
---|
1205 | * But on irix 3.3 we need to set it again, even if it is |
---|
1206 | * the same. We do that to tell the system that we |
---|
1207 | * need BSD process group compatibility. |
---|
1208 | */ |
---|
1209 | else |
---|
1210 | (void) setpgid(0, shpgrp); |
---|
1211 | #endif |
---|
1212 | (void) close_on_exec(dcopy(f, FSHTTY), 1); |
---|
1213 | } |
---|
1214 | else |
---|
1215 | tpgrp = -1; |
---|
1216 | } |
---|
1217 | if (tpgrp == -1) { |
---|
1218 | notty: |
---|
1219 | xprintf(CGETS(11, 1, "Warning: no access to tty (%s).\n"), |
---|
1220 | strerror(errno)); |
---|
1221 | xprintf(CGETS(11, 2, "Thus no job control in this shell.\n")); |
---|
1222 | /* |
---|
1223 | * Fix from:Sakari Jalovaara <sja@sirius.hut.fi> if we don't |
---|
1224 | * have access to tty, disable editing too |
---|
1225 | */ |
---|
1226 | if (adrof(STRedit)) |
---|
1227 | unsetv(STRedit); |
---|
1228 | editing = 0; |
---|
1229 | } |
---|
1230 | #else /* BSDJOBS */ /* don't have job control, so frotz it */ |
---|
1231 | tpgrp = -1; |
---|
1232 | #endif /* BSDJOBS */ |
---|
1233 | } |
---|
1234 | } |
---|
1235 | if ((setintr == 0) && (parintr == SIG_DFL)) |
---|
1236 | setintr = 1; |
---|
1237 | |
---|
1238 | /* |
---|
1239 | * SVR4 doesn't send a SIGCHLD when a child is stopped or continued if the |
---|
1240 | * handler is installed with signal(2) or sigset(2). sigaction(2) must |
---|
1241 | * be used instead. |
---|
1242 | * |
---|
1243 | * David Dawes (dawes@physics.su.oz.au) Sept 1991 |
---|
1244 | */ |
---|
1245 | |
---|
1246 | #if SYSVREL > 3 |
---|
1247 | { |
---|
1248 | struct sigaction act; |
---|
1249 | act.sa_handler=pchild; |
---|
1250 | (void) sigemptyset(&(act.sa_mask)); /* Don't block any extra sigs |
---|
1251 | * when the handler is called |
---|
1252 | */ |
---|
1253 | act.sa_flags=0; /* want behaviour of sigset() without |
---|
1254 | * SA_NOCLDSTOP |
---|
1255 | */ |
---|
1256 | |
---|
1257 | if ((sigaction(SIGCHLD,&act,(struct sigaction *)NULL)) == -1) |
---|
1258 | stderror(ERR_SYSTEM, "sigaction", strerror(errno)); |
---|
1259 | } |
---|
1260 | #else /* SYSVREL <= 3 */ |
---|
1261 | (void) sigset(SIGCHLD, pchild); /* while signals not ready */ |
---|
1262 | #endif /* SYSVREL <= 3 */ |
---|
1263 | |
---|
1264 | |
---|
1265 | if (intty && !arginp) |
---|
1266 | (void) ed_Setup(editing);/* Get the tty state, and set defaults */ |
---|
1267 | /* Only alter the tty state if editing */ |
---|
1268 | |
---|
1269 | /* |
---|
1270 | * Set an exit here in case of an interrupt or error reading the shell |
---|
1271 | * start-up scripts. |
---|
1272 | */ |
---|
1273 | osetintr = setintr; |
---|
1274 | oparintr = parintr; |
---|
1275 | reenter = setexit(); /* PWP */ |
---|
1276 | exitset++; |
---|
1277 | haderr = 0; /* In case second time through */ |
---|
1278 | if (!fast && reenter == 0) { |
---|
1279 | /* Will have varval(STRhome) here because set fast if don't */ |
---|
1280 | { |
---|
1281 | #ifdef BSDSIGS |
---|
1282 | sigmask_t omask = sigblock(sigmask(SIGINT)); |
---|
1283 | #else |
---|
1284 | (void) sighold(SIGINT); |
---|
1285 | #endif |
---|
1286 | setintr = 0; |
---|
1287 | parintr = SIG_IGN; /* onintr in /etc/ files has no effect */ |
---|
1288 | #ifdef LOGINFIRST |
---|
1289 | #ifdef _PATH_DOTLOGIN |
---|
1290 | if (loginsh) |
---|
1291 | (void) srcfile(_PATH_DOTLOGIN, 0, 0, NULL); |
---|
1292 | #endif |
---|
1293 | #endif |
---|
1294 | |
---|
1295 | #ifdef _PATH_DOTCSHRC |
---|
1296 | (void) srcfile(_PATH_DOTCSHRC, 0, 0, NULL); |
---|
1297 | #endif |
---|
1298 | if (!arginp && !onelflg && !havhash) |
---|
1299 | dohash(NULL,NULL); |
---|
1300 | #ifndef LOGINFIRST |
---|
1301 | #ifdef _PATH_DOTLOGIN |
---|
1302 | if (loginsh) |
---|
1303 | (void) srcfile(_PATH_DOTLOGIN, 0, 0, NULL); |
---|
1304 | #endif |
---|
1305 | #endif |
---|
1306 | #ifdef BSDSIGS |
---|
1307 | (void) sigsetmask(omask); |
---|
1308 | #else |
---|
1309 | (void) sigrelse(SIGINT); |
---|
1310 | #endif |
---|
1311 | setintr = osetintr; |
---|
1312 | parintr = oparintr; |
---|
1313 | } |
---|
1314 | #ifdef LOGINFIRST |
---|
1315 | if (loginsh) |
---|
1316 | (void) srccat(varval(STRhome), STRsldotlogin); |
---|
1317 | #endif |
---|
1318 | (void) srccat(varval(STRhome), STRsldottcshrc); |
---|
1319 | (void) srccat(varval(STRhome), STRsldotcshrc); |
---|
1320 | |
---|
1321 | if (!fast && !arginp && !onelflg && !havhash) |
---|
1322 | dohash(NULL,NULL); |
---|
1323 | |
---|
1324 | /* |
---|
1325 | * Source history before .login so that it is available in .login |
---|
1326 | */ |
---|
1327 | loadhist(NULL, 0); |
---|
1328 | #ifndef LOGINFIRST |
---|
1329 | if (loginsh) |
---|
1330 | (void) srccat(varval(STRhome), STRsldotlogin); |
---|
1331 | #endif |
---|
1332 | if (!fast && (loginsh || rdirs)) |
---|
1333 | loaddirs(NULL); |
---|
1334 | } |
---|
1335 | /* Reset interrupt flag */ |
---|
1336 | setintr = osetintr; |
---|
1337 | parintr = oparintr; |
---|
1338 | |
---|
1339 | /* Initing AFTER .cshrc is the Right Way */ |
---|
1340 | if (intty && !arginp) { /* PWP setup stuff */ |
---|
1341 | ed_Init(); /* init the new line editor */ |
---|
1342 | #ifdef SIG_WINDOW |
---|
1343 | check_window_size(1); /* mung environment */ |
---|
1344 | #endif /* SIG_WINDOW */ |
---|
1345 | } |
---|
1346 | |
---|
1347 | /* |
---|
1348 | * Now are ready for the -v and -x flags |
---|
1349 | */ |
---|
1350 | if (nverbose) |
---|
1351 | setNS(STRverbose); |
---|
1352 | if (nexececho) |
---|
1353 | setNS(STRecho); |
---|
1354 | |
---|
1355 | /* |
---|
1356 | * All the rest of the world is inside this call. The argument to process |
---|
1357 | * indicates whether it should catch "error unwinds". Thus if we are a |
---|
1358 | * interactive shell our call here will never return by being blown past on |
---|
1359 | * an error. |
---|
1360 | */ |
---|
1361 | process(setintr); |
---|
1362 | |
---|
1363 | /* |
---|
1364 | * Mop-up. |
---|
1365 | */ |
---|
1366 | if (intty) { |
---|
1367 | if (loginsh) { |
---|
1368 | xprintf("logout\n"); |
---|
1369 | (void) close(SHIN); |
---|
1370 | child = 1; |
---|
1371 | #ifdef TESLA |
---|
1372 | do_logout = 1; |
---|
1373 | #endif /* TESLA */ |
---|
1374 | goodbye(NULL, NULL); |
---|
1375 | } |
---|
1376 | else { |
---|
1377 | xprintf("exit\n"); |
---|
1378 | } |
---|
1379 | } |
---|
1380 | record(); |
---|
1381 | exitstat(); |
---|
1382 | return (0); |
---|
1383 | } |
---|
1384 | |
---|
1385 | void |
---|
1386 | untty() |
---|
1387 | { |
---|
1388 | #ifdef BSDJOBS |
---|
1389 | if (tpgrp > 0 && opgrp != shpgrp) { |
---|
1390 | (void) setpgid(0, opgrp); |
---|
1391 | (void) tcsetpgrp(FSHTTY, opgrp); |
---|
1392 | (void) resetdisc(FSHTTY); |
---|
1393 | } |
---|
1394 | #endif /* BSDJOBS */ |
---|
1395 | } |
---|
1396 | |
---|
1397 | void |
---|
1398 | importpath(cp) |
---|
1399 | Char *cp; |
---|
1400 | { |
---|
1401 | int i = 0; |
---|
1402 | Char *dp; |
---|
1403 | Char **pv; |
---|
1404 | int c; |
---|
1405 | |
---|
1406 | for (dp = cp; *dp; dp++) |
---|
1407 | if (*dp == PATHSEP) |
---|
1408 | i++; |
---|
1409 | /* |
---|
1410 | * i+2 where i is the number of colons in the path. There are i+1 |
---|
1411 | * directories in the path plus we need room for a zero terminator. |
---|
1412 | */ |
---|
1413 | pv = (Char **) xcalloc((size_t) (i + 2), sizeof(Char *)); |
---|
1414 | dp = cp; |
---|
1415 | i = 0; |
---|
1416 | if (*dp) |
---|
1417 | for (;;) { |
---|
1418 | if ((c = *dp) == PATHSEP || c == 0) { |
---|
1419 | *dp = 0; |
---|
1420 | pv[i++] = Strsave(*cp ? cp : STRdot); |
---|
1421 | if (c) { |
---|
1422 | cp = dp + 1; |
---|
1423 | *dp = PATHSEP; |
---|
1424 | } |
---|
1425 | else |
---|
1426 | break; |
---|
1427 | } |
---|
1428 | #ifdef WINNT_NATIVE |
---|
1429 | else if (*dp == '\\') |
---|
1430 | *dp = '/'; |
---|
1431 | #endif /* WINNT_NATIVE */ |
---|
1432 | dp++; |
---|
1433 | } |
---|
1434 | pv[i] = 0; |
---|
1435 | setq(STRpath, pv, &shvhed, VAR_READWRITE); |
---|
1436 | } |
---|
1437 | |
---|
1438 | /* |
---|
1439 | * Source to the file which is the catenation of the argument names. |
---|
1440 | */ |
---|
1441 | static int |
---|
1442 | srccat(cp, dp) |
---|
1443 | Char *cp, *dp; |
---|
1444 | { |
---|
1445 | if (cp[0] == '/' && cp[1] == '\0') |
---|
1446 | return srcfile(short2str(dp), (mflag ? 0 : 1), 0, NULL); |
---|
1447 | else { |
---|
1448 | Char *ep; |
---|
1449 | char *ptr; |
---|
1450 | int rv; |
---|
1451 | |
---|
1452 | #ifdef WINNT_NATIVE |
---|
1453 | ep = cp; |
---|
1454 | while(*ep) |
---|
1455 | ep++; |
---|
1456 | if (ep[-1] == '/' && dp[0] == '/') /* silly win95 */ |
---|
1457 | dp++; |
---|
1458 | #endif /* WINNT_NATIVE */ |
---|
1459 | |
---|
1460 | ep = Strspl(cp, dp); |
---|
1461 | ptr = short2str(ep); |
---|
1462 | |
---|
1463 | rv = srcfile(ptr, (mflag ? 0 : 1), 0, NULL); |
---|
1464 | xfree((ptr_t) ep); |
---|
1465 | return rv; |
---|
1466 | } |
---|
1467 | } |
---|
1468 | |
---|
1469 | /* |
---|
1470 | * Source to a file putting the file descriptor in a safe place (> 2). |
---|
1471 | */ |
---|
1472 | #ifndef WINNT_NATIVE |
---|
1473 | static int |
---|
1474 | #else |
---|
1475 | int |
---|
1476 | #endif /*WINNT_NATIVE*/ |
---|
1477 | srcfile(f, onlyown, flag, av) |
---|
1478 | const char *f; |
---|
1479 | int onlyown; |
---|
1480 | int flag; |
---|
1481 | Char **av; |
---|
1482 | { |
---|
1483 | int unit; |
---|
1484 | |
---|
1485 | if ((unit = open(f, O_RDONLY|O_LARGEFILE)) == -1) |
---|
1486 | return 0; |
---|
1487 | #ifdef O_TEXT |
---|
1488 | setmode(unit, O_TEXT); |
---|
1489 | #endif |
---|
1490 | unit = dmove(unit, -1); |
---|
1491 | |
---|
1492 | (void) close_on_exec(unit, 1); |
---|
1493 | srcunit(unit, onlyown, flag, av); |
---|
1494 | return 1; |
---|
1495 | } |
---|
1496 | |
---|
1497 | |
---|
1498 | /* |
---|
1499 | * Save the shell state, and establish new argument vector, and new input |
---|
1500 | * fd. |
---|
1501 | */ |
---|
1502 | static void |
---|
1503 | st_save(st, unit, hflg, al, av) |
---|
1504 | struct saved_state *st; |
---|
1505 | int unit, hflg; |
---|
1506 | Char **al, **av; |
---|
1507 | { |
---|
1508 | st->insource = insource; |
---|
1509 | st->SHIN = SHIN; |
---|
1510 | /* Want to preserve the meaning of "source file >output". |
---|
1511 | * Save old descriptors, move new 0,1,2 to safe places and assign |
---|
1512 | * them to SH* and let process() redo 0,1,2 from them. |
---|
1513 | * |
---|
1514 | * The macro returns true if d1 and d2 are good and they point to |
---|
1515 | * different things. If you don't avoid saving duplicate |
---|
1516 | * descriptors, you really limit the depth of "source" recursion |
---|
1517 | * you can do because of all the open file descriptors. -IAN! |
---|
1518 | */ |
---|
1519 | #define NEED_SAVE_FD(d1,d2) \ |
---|
1520 | (fstat(d1, &s1) != -1 && fstat(d2, &s2) != -1 \ |
---|
1521 | && (s1.st_ino != s2.st_ino || s1.st_dev != s2.st_dev) ) |
---|
1522 | |
---|
1523 | st->OLDSTD = st->SHOUT = st->SHDIAG = -1;/* test later to restore these */ |
---|
1524 | if (didfds) { |
---|
1525 | struct stat s1, s2; |
---|
1526 | if (NEED_SAVE_FD(0,OLDSTD)) { |
---|
1527 | st->OLDSTD = OLDSTD; |
---|
1528 | OLDSTD = dmove(0, -1); |
---|
1529 | (void)close_on_exec(OLDSTD, 1); |
---|
1530 | } |
---|
1531 | if (NEED_SAVE_FD(1,SHOUT)) { |
---|
1532 | st->SHOUT = SHOUT; |
---|
1533 | SHOUT = dmove(1, -1); |
---|
1534 | (void)close_on_exec(SHOUT, 1); |
---|
1535 | } |
---|
1536 | if (NEED_SAVE_FD(2,SHDIAG)) { |
---|
1537 | st->SHDIAG = SHDIAG; |
---|
1538 | SHDIAG = dmove(2, -1); |
---|
1539 | (void)close_on_exec(SHDIAG, 1); |
---|
1540 | } |
---|
1541 | donefds(); |
---|
1542 | } |
---|
1543 | |
---|
1544 | st->intty = intty; |
---|
1545 | st->whyles = whyles; |
---|
1546 | st->gointr = gointr; |
---|
1547 | st->arginp = arginp; |
---|
1548 | st->evalp = evalp; |
---|
1549 | st->evalvec = evalvec; |
---|
1550 | st->alvecp = alvecp; |
---|
1551 | st->alvec = alvec; |
---|
1552 | st->onelflg = onelflg; |
---|
1553 | st->enterhist = enterhist; |
---|
1554 | if (hflg) |
---|
1555 | st->HIST = HIST; |
---|
1556 | else |
---|
1557 | st->HIST = '\0'; |
---|
1558 | st->cantell = cantell; |
---|
1559 | cpybin(st->B, B); |
---|
1560 | |
---|
1561 | /* |
---|
1562 | * we can now pass arguments to source. |
---|
1563 | * For compatibility we do that only if arguments were really |
---|
1564 | * passed, otherwise we keep the old, global $argv like before. |
---|
1565 | */ |
---|
1566 | if (av != NULL && *av != NULL) { |
---|
1567 | struct varent *vp; |
---|
1568 | if ((vp = adrof(STRargv)) != NULL && vp->vec != NULL) |
---|
1569 | st->argv = saveblk(vp->vec); |
---|
1570 | else |
---|
1571 | st->argv = NULL; |
---|
1572 | setq(STRargv, saveblk(av), &shvhed, VAR_READWRITE); |
---|
1573 | } |
---|
1574 | else |
---|
1575 | st->argv = NULL; |
---|
1576 | |
---|
1577 | SHIN = unit; /* Do this first */ |
---|
1578 | |
---|
1579 | /* Establish new input arena */ |
---|
1580 | { |
---|
1581 | fbuf = NULL; |
---|
1582 | fseekp = feobp = fblocks = 0; |
---|
1583 | settell(); |
---|
1584 | } |
---|
1585 | |
---|
1586 | arginp = 0; |
---|
1587 | onelflg = 0; |
---|
1588 | intty = isatty(SHIN); |
---|
1589 | whyles = 0; |
---|
1590 | gointr = 0; |
---|
1591 | evalvec = 0; |
---|
1592 | evalp = 0; |
---|
1593 | alvec = al; |
---|
1594 | alvecp = 0; |
---|
1595 | enterhist = hflg; |
---|
1596 | if (enterhist) |
---|
1597 | HIST = '\0'; |
---|
1598 | insource = 1; |
---|
1599 | } |
---|
1600 | |
---|
1601 | |
---|
1602 | /* |
---|
1603 | * Restore the shell to a saved state |
---|
1604 | */ |
---|
1605 | static void |
---|
1606 | st_restore(st, av) |
---|
1607 | struct saved_state *st; |
---|
1608 | Char **av; |
---|
1609 | { |
---|
1610 | if (st->SHIN == -1) |
---|
1611 | return; |
---|
1612 | |
---|
1613 | /* Reset input arena */ |
---|
1614 | { |
---|
1615 | int i; |
---|
1616 | Char** nfbuf = fbuf; |
---|
1617 | int nfblocks = fblocks; |
---|
1618 | |
---|
1619 | fblocks = 0; |
---|
1620 | fbuf = NULL; |
---|
1621 | for (i = 0; i < nfblocks; i++) |
---|
1622 | xfree((ptr_t) nfbuf[i]); |
---|
1623 | xfree((ptr_t) nfbuf); |
---|
1624 | } |
---|
1625 | cpybin(B, st->B); |
---|
1626 | |
---|
1627 | (void) close(SHIN); |
---|
1628 | |
---|
1629 | insource = st->insource; |
---|
1630 | SHIN = st->SHIN; |
---|
1631 | if (st->OLDSTD != -1) |
---|
1632 | (void)close(OLDSTD), OLDSTD = st->OLDSTD; |
---|
1633 | if (st->SHOUT != -1) |
---|
1634 | (void)close(SHOUT), SHOUT = st->SHOUT; |
---|
1635 | if (st->SHDIAG != -1) |
---|
1636 | (void)close(SHDIAG), SHDIAG = st->SHDIAG; |
---|
1637 | arginp = st->arginp; |
---|
1638 | onelflg = st->onelflg; |
---|
1639 | evalp = st->evalp; |
---|
1640 | evalvec = st->evalvec; |
---|
1641 | alvecp = st->alvecp; |
---|
1642 | alvec = st->alvec; |
---|
1643 | intty = st->intty; |
---|
1644 | whyles = st->whyles; |
---|
1645 | gointr = st->gointr; |
---|
1646 | if (st->HIST != '\0') |
---|
1647 | HIST = st->HIST; |
---|
1648 | enterhist = st->enterhist; |
---|
1649 | cantell = st->cantell; |
---|
1650 | |
---|
1651 | if (st->argv != NULL) |
---|
1652 | setq(STRargv, st->argv, &shvhed, VAR_READWRITE); |
---|
1653 | else if (av != NULL && *av != NULL && adrof(STRargv) != NULL) |
---|
1654 | unsetv(STRargv); |
---|
1655 | } |
---|
1656 | |
---|
1657 | /* |
---|
1658 | * Source to a unit. If onlyown it must be our file or our group or |
---|
1659 | * we don't chance it. This occurs on ".cshrc"s and the like. |
---|
1660 | */ |
---|
1661 | static void |
---|
1662 | srcunit(unit, onlyown, hflg, av) |
---|
1663 | int unit; |
---|
1664 | int onlyown; |
---|
1665 | int hflg; |
---|
1666 | Char **av; |
---|
1667 | { |
---|
1668 | struct saved_state st; |
---|
1669 | st.SHIN = -1; /* st_restore checks this */ |
---|
1670 | |
---|
1671 | if (unit < 0) |
---|
1672 | return; |
---|
1673 | |
---|
1674 | if (onlyown) { |
---|
1675 | struct stat stb; |
---|
1676 | |
---|
1677 | if (fstat(unit, &stb) < 0) { |
---|
1678 | (void) close(unit); |
---|
1679 | return; |
---|
1680 | } |
---|
1681 | } |
---|
1682 | |
---|
1683 | getexit(st.oldexit); |
---|
1684 | |
---|
1685 | if (setintr) |
---|
1686 | #ifdef BSDSIGS |
---|
1687 | st.mask = sigblock(sigmask(SIGINT)); |
---|
1688 | #else |
---|
1689 | (void) sighold(SIGINT); |
---|
1690 | #endif |
---|
1691 | |
---|
1692 | /* Save the current state and move us to a new state */ |
---|
1693 | st_save(&st, unit, hflg, NULL, av); |
---|
1694 | |
---|
1695 | /* |
---|
1696 | * Now if we are allowing commands to be interrupted, we let ourselves be |
---|
1697 | * interrupted. |
---|
1698 | */ |
---|
1699 | if (setintr) |
---|
1700 | #ifdef BSDSIGS |
---|
1701 | (void) sigsetmask(st.mask); |
---|
1702 | #else |
---|
1703 | (void) sigrelse(SIGINT); |
---|
1704 | #endif |
---|
1705 | |
---|
1706 | /* |
---|
1707 | * Bugfix for running out of memory by: Jak Kirman |
---|
1708 | * <jak%cs.brown.edu@RELAY.CS.NET>. Solution: pay attention to what |
---|
1709 | * setexit() is returning because reenter _may_ be in a register, and |
---|
1710 | * thus restored to 0 on a longjump(). (PWP: insert flames about |
---|
1711 | * compiler-dependant code here) PWP: THANKS LOTS !!! |
---|
1712 | * |
---|
1713 | * PWP: think of this as like a LISP (unwind-protect ...) |
---|
1714 | * thanks to Diana Smetters for pointing out how this _should_ be written |
---|
1715 | */ |
---|
1716 | #ifdef cray |
---|
1717 | st.reenter = 1; /* assume non-zero return val */ |
---|
1718 | if (setexit() == 0) { |
---|
1719 | st.reenter = 0; /* Oh well, we were wrong */ |
---|
1720 | #else |
---|
1721 | if ((st.reenter = setexit()) == 0) { |
---|
1722 | #endif |
---|
1723 | process(0); /* 0 -> blow away on errors */ |
---|
1724 | } |
---|
1725 | |
---|
1726 | if (setintr) |
---|
1727 | #ifdef BSDSIGS |
---|
1728 | (void) sigsetmask(st.mask); |
---|
1729 | #else |
---|
1730 | (void) sigrelse(SIGINT); |
---|
1731 | #endif |
---|
1732 | |
---|
1733 | /* Restore the old state */ |
---|
1734 | st_restore(&st, av); |
---|
1735 | resexit(st.oldexit); |
---|
1736 | /* |
---|
1737 | * If process reset() (effectively an unwind) then we must also unwind. |
---|
1738 | */ |
---|
1739 | if (st.reenter) |
---|
1740 | stderror(ERR_SILENT); |
---|
1741 | } |
---|
1742 | |
---|
1743 | |
---|
1744 | /*ARGSUSED*/ |
---|
1745 | void |
---|
1746 | goodbye(v, c) |
---|
1747 | Char **v; |
---|
1748 | struct command *c; |
---|
1749 | { |
---|
1750 | USE(v); |
---|
1751 | USE(c); |
---|
1752 | record(); |
---|
1753 | |
---|
1754 | if (loginsh) { |
---|
1755 | (void) sigset(SIGQUIT, SIG_IGN); |
---|
1756 | (void) sigset(SIGINT, SIG_IGN); |
---|
1757 | (void) sigset(SIGTERM, SIG_IGN); |
---|
1758 | (void) sigset(SIGHUP, SIG_IGN); |
---|
1759 | setintr = 0; /* No interrupts after "logout" */ |
---|
1760 | /* Trap errors inside .logout */ |
---|
1761 | reenter = setexit(); |
---|
1762 | if (reenter != 0) |
---|
1763 | exitstat(); |
---|
1764 | if (!(adrof(STRlogout))) |
---|
1765 | set(STRlogout, Strsave(STRnormal), VAR_READWRITE); |
---|
1766 | #ifdef _PATH_DOTLOGOUT |
---|
1767 | (void) srcfile(_PATH_DOTLOGOUT, 0, 0, NULL); |
---|
1768 | #endif |
---|
1769 | if (adrof(STRhome)) |
---|
1770 | (void) srccat(varval(STRhome), STRsldtlogout); |
---|
1771 | #ifdef TESLA |
---|
1772 | do_logout = 1; |
---|
1773 | #endif /* TESLA */ |
---|
1774 | } |
---|
1775 | exitstat(); |
---|
1776 | } |
---|
1777 | |
---|
1778 | void |
---|
1779 | exitstat() |
---|
1780 | { |
---|
1781 | #ifdef PROF |
---|
1782 | monitor(0); |
---|
1783 | #endif |
---|
1784 | /* |
---|
1785 | * Note that if STATUS is corrupted (i.e. getn bombs) then error will exit |
---|
1786 | * directly because we poke child here. Otherwise we might continue |
---|
1787 | * unwarrantedly (sic). |
---|
1788 | */ |
---|
1789 | child = 1; |
---|
1790 | |
---|
1791 | xexit(getn(varval(STRstatus))); |
---|
1792 | } |
---|
1793 | |
---|
1794 | /* |
---|
1795 | * in the event of a HUP we want to save the history |
---|
1796 | */ |
---|
1797 | static RETSIGTYPE |
---|
1798 | phup(snum) |
---|
1799 | int snum; |
---|
1800 | { |
---|
1801 | /* |
---|
1802 | * There is no return from here, |
---|
1803 | * so we are not going to release SIGHUP |
---|
1804 | * anymore |
---|
1805 | */ |
---|
1806 | #ifdef UNRELSIGS |
---|
1807 | if (snum) |
---|
1808 | (void) sigset(snum, SIG_IGN); |
---|
1809 | #else |
---|
1810 | # ifdef BSDSIGS |
---|
1811 | (void) sigblock(sigmask(SIGHUP)); |
---|
1812 | # else |
---|
1813 | (void) sighold(SIGHUP); |
---|
1814 | # endif /* BSDSIGS */ |
---|
1815 | #endif /* UNRELSIGS */ |
---|
1816 | |
---|
1817 | if (loginsh) { |
---|
1818 | set(STRlogout, Strsave(STRhangup), VAR_READWRITE); |
---|
1819 | #ifdef _PATH_DOTLOGOUT |
---|
1820 | (void) srcfile(_PATH_DOTLOGOUT, 0, 0, NULL); |
---|
1821 | #endif |
---|
1822 | if (adrof(STRhome)) |
---|
1823 | (void) srccat(varval(STRhome), STRsldtlogout); |
---|
1824 | } |
---|
1825 | |
---|
1826 | record(); |
---|
1827 | |
---|
1828 | #ifdef POSIXJOBS |
---|
1829 | /* |
---|
1830 | * We kill the last foreground process group. It then becomes |
---|
1831 | * responsible to propagate the SIGHUP to its progeny. |
---|
1832 | */ |
---|
1833 | { |
---|
1834 | struct process *pp, *np; |
---|
1835 | |
---|
1836 | for (pp = proclist.p_next; pp; pp = pp->p_next) { |
---|
1837 | np = pp; |
---|
1838 | /* |
---|
1839 | * Find if this job is in the foreground. It could be that |
---|
1840 | * the process leader has exited and the foreground flag |
---|
1841 | * is cleared for it. |
---|
1842 | */ |
---|
1843 | do |
---|
1844 | /* |
---|
1845 | * If a process is in the foreground we try to kill |
---|
1846 | * it's process group. If we succeed, then the |
---|
1847 | * whole job is gone. Otherwise we keep going... |
---|
1848 | * But avoid sending HUP to the shell again. |
---|
1849 | */ |
---|
1850 | if (((np->p_flags & PFOREGND) != 0) && np->p_jobid != shpgrp) { |
---|
1851 | np->p_flags &= ~PHUP; |
---|
1852 | if (killpg(np->p_jobid, SIGHUP) != -1) { |
---|
1853 | /* In case the job was suspended... */ |
---|
1854 | #ifdef SIGCONT |
---|
1855 | (void) killpg(np->p_jobid, SIGCONT); |
---|
1856 | #endif |
---|
1857 | break; |
---|
1858 | } |
---|
1859 | } |
---|
1860 | while ((np = np->p_friends) != pp); |
---|
1861 | } |
---|
1862 | } |
---|
1863 | #endif /* POSIXJOBS */ |
---|
1864 | |
---|
1865 | xexit(snum); |
---|
1866 | } |
---|
1867 | |
---|
1868 | static Char *jobargv[2] = {STRjobs, 0}; |
---|
1869 | |
---|
1870 | /* |
---|
1871 | * Catch an interrupt, e.g. during lexical input. |
---|
1872 | * If we are an interactive shell, we reset the interrupt catch |
---|
1873 | * immediately. In any case we drain the shell output, |
---|
1874 | * and finally go through the normal error mechanism, which |
---|
1875 | * gets a chance to make the shell go away. |
---|
1876 | */ |
---|
1877 | int just_signaled; /* bugfix by Michael Bloom (mg@ttidca.TTI.COM) */ |
---|
1878 | |
---|
1879 | RETSIGTYPE |
---|
1880 | pintr(snum) |
---|
1881 | int snum; |
---|
1882 | { |
---|
1883 | USE(snum); |
---|
1884 | #ifdef UNRELSIGS |
---|
1885 | if (snum) |
---|
1886 | (void) sigset(snum, pintr); |
---|
1887 | #endif /* UNRELSIGS */ |
---|
1888 | just_signaled = 1; |
---|
1889 | pintr1(1); |
---|
1890 | } |
---|
1891 | |
---|
1892 | void |
---|
1893 | pintr1(wantnl) |
---|
1894 | int wantnl; |
---|
1895 | { |
---|
1896 | Char **v; |
---|
1897 | #ifdef BSDSIGS |
---|
1898 | sigmask_t omask; |
---|
1899 | #endif |
---|
1900 | |
---|
1901 | #ifdef BSDSIGS |
---|
1902 | omask = sigblock((sigmask_t) 0); |
---|
1903 | #endif |
---|
1904 | if (setintr) { |
---|
1905 | #ifdef BSDSIGS |
---|
1906 | (void) sigsetmask(omask & ~sigmask(SIGINT)); |
---|
1907 | #else |
---|
1908 | (void) sigrelse(SIGINT); |
---|
1909 | #endif |
---|
1910 | if (pjobs) { |
---|
1911 | pjobs = 0; |
---|
1912 | xputchar('\n'); |
---|
1913 | dojobs(jobargv, NULL); |
---|
1914 | stderror(ERR_NAME | ERR_INTR); |
---|
1915 | } |
---|
1916 | } |
---|
1917 | /* MH - handle interrupted completions specially */ |
---|
1918 | { |
---|
1919 | if (InsideCompletion) |
---|
1920 | stderror(ERR_SILENT); |
---|
1921 | } |
---|
1922 | /* JV - Make sure we shut off inputl */ |
---|
1923 | { |
---|
1924 | (void) Cookedmode(); |
---|
1925 | GettingInput = 0; |
---|
1926 | } |
---|
1927 | #ifdef BSDSIGS |
---|
1928 | (void) sigsetmask(omask & ~sigmask(SIGCHLD)); |
---|
1929 | #else |
---|
1930 | if (setintr) |
---|
1931 | (void) sighold(SIGINT); |
---|
1932 | (void) sigrelse(SIGCHLD); |
---|
1933 | #endif |
---|
1934 | drainoline(); |
---|
1935 | #ifdef HAVE_GETPWENT |
---|
1936 | (void) endpwent(); |
---|
1937 | #endif |
---|
1938 | |
---|
1939 | /* |
---|
1940 | * If we have an active "onintr" then we search for the label. Note that if |
---|
1941 | * one does "onintr -" then we shan't be interruptible so we needn't worry |
---|
1942 | * about that here. |
---|
1943 | */ |
---|
1944 | if (gointr) { |
---|
1945 | gotolab(gointr); |
---|
1946 | timflg = 0; |
---|
1947 | if ((v = pargv) != 0) |
---|
1948 | pargv = 0, blkfree(v); |
---|
1949 | if ((v = gargv) != 0) |
---|
1950 | gargv = 0, blkfree(v); |
---|
1951 | reset(); |
---|
1952 | } |
---|
1953 | else if (intty && wantnl) { |
---|
1954 | if (editing) { |
---|
1955 | /* |
---|
1956 | * If we are editing a multi-line input command, and move to |
---|
1957 | * the beginning of the line, we don't want to trash it when |
---|
1958 | * we hit ^C |
---|
1959 | */ |
---|
1960 | PastBottom(); |
---|
1961 | ClearLines(); |
---|
1962 | ClearDisp(); |
---|
1963 | } |
---|
1964 | else { |
---|
1965 | /* xputchar('\n'); *//* Some like this, others don't */ |
---|
1966 | (void) putraw('\r'); |
---|
1967 | (void) putraw('\n'); |
---|
1968 | } |
---|
1969 | } |
---|
1970 | stderror(ERR_SILENT); |
---|
1971 | } |
---|
1972 | |
---|
1973 | /* |
---|
1974 | * Process is the main driving routine for the shell. |
---|
1975 | * It runs all command processing, except for those within { ... } |
---|
1976 | * in expressions (which is run by a routine evalav in sh.exp.c which |
---|
1977 | * is a stripped down process), and `...` evaluation which is run |
---|
1978 | * also by a subset of this code in sh.glob.c in the routine backeval. |
---|
1979 | * |
---|
1980 | * The code here is a little strange because part of it is interruptible |
---|
1981 | * and hence freeing of structures appears to occur when none is necessary |
---|
1982 | * if this is ignored. |
---|
1983 | * |
---|
1984 | * Note that if catch is not set then we will unwind on any error. |
---|
1985 | * If an end-of-file occurs, we return. |
---|
1986 | */ |
---|
1987 | static struct command *savet = NULL; |
---|
1988 | void |
---|
1989 | process(catch) |
---|
1990 | int catch; |
---|
1991 | { |
---|
1992 | jmp_buf_t osetexit; |
---|
1993 | /* PWP: This might get nuked my longjmp so don't make it a register var */ |
---|
1994 | struct command *t = savet; |
---|
1995 | |
---|
1996 | savet = NULL; |
---|
1997 | getexit(osetexit); |
---|
1998 | for (;;) { |
---|
1999 | |
---|
2000 | pendjob(); |
---|
2001 | |
---|
2002 | /* This was leaking memory badly, particularly when sourcing |
---|
2003 | * files, etc.. For whatever reason we were arriving here with |
---|
2004 | * allocated pointers still active, and the code was simply |
---|
2005 | * overwriting them. I can't say I fully understand the |
---|
2006 | * control flow here, but according to Purify this particular |
---|
2007 | * leak has been plugged, and I haven't noticed any ill |
---|
2008 | * effects.. (sg) |
---|
2009 | */ |
---|
2010 | if (paraml.next && paraml.next != ¶ml) |
---|
2011 | freelex(¶ml); |
---|
2012 | |
---|
2013 | paraml.next = paraml.prev = ¶ml; |
---|
2014 | paraml.word = STRNULL; |
---|
2015 | (void) setexit(); |
---|
2016 | justpr = enterhist; /* execute if not entering history */ |
---|
2017 | |
---|
2018 | /* |
---|
2019 | * Interruptible during interactive reads |
---|
2020 | */ |
---|
2021 | if (setintr) |
---|
2022 | #ifdef BSDSIGS |
---|
2023 | (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT)); |
---|
2024 | #else |
---|
2025 | (void) sigrelse(SIGINT); |
---|
2026 | #endif |
---|
2027 | |
---|
2028 | |
---|
2029 | /* |
---|
2030 | * For the sake of reset() |
---|
2031 | */ |
---|
2032 | freelex(¶ml); |
---|
2033 | if (savet) |
---|
2034 | freesyn(savet), savet = NULL; |
---|
2035 | |
---|
2036 | if (haderr) { |
---|
2037 | if (!catch) { |
---|
2038 | /* unwind */ |
---|
2039 | doneinp = 0; |
---|
2040 | savet = t; |
---|
2041 | resexit(osetexit); |
---|
2042 | reset(); |
---|
2043 | } |
---|
2044 | haderr = 0; |
---|
2045 | /* |
---|
2046 | * Every error is eventually caught here or the shell dies. It is |
---|
2047 | * at this point that we clean up any left-over open files, by |
---|
2048 | * closing all but a fixed number of pre-defined files. Thus |
---|
2049 | * routines don't have to worry about leaving files open due to |
---|
2050 | * deeper errors... they will get closed here. |
---|
2051 | */ |
---|
2052 | closem(); |
---|
2053 | continue; |
---|
2054 | } |
---|
2055 | if (doneinp) { |
---|
2056 | doneinp = 0; |
---|
2057 | break; |
---|
2058 | } |
---|
2059 | if (chkstop) |
---|
2060 | chkstop--; |
---|
2061 | if (neednote) |
---|
2062 | pnote(); |
---|
2063 | if (intty && prompt && evalvec == 0) { |
---|
2064 | just_signaled = 0; |
---|
2065 | mailchk(); |
---|
2066 | /* |
---|
2067 | * Watch for logins/logouts. Next is scheduled commands stored |
---|
2068 | * previously using "sched." Then execute periodic commands. |
---|
2069 | * Following that, the prompt precmd is run. |
---|
2070 | */ |
---|
2071 | #ifndef HAVENOUTMP |
---|
2072 | watch_login(0); |
---|
2073 | #endif /* !HAVENOUTMP */ |
---|
2074 | sched_run(0); |
---|
2075 | period_cmd(); |
---|
2076 | precmd(); |
---|
2077 | /* |
---|
2078 | * If we are at the end of the input buffer then we are going to |
---|
2079 | * read fresh stuff. Otherwise, we are rereading input and don't |
---|
2080 | * need or want to prompt. |
---|
2081 | */ |
---|
2082 | if (fseekp == feobp && aret == TCSH_F_SEEK) |
---|
2083 | printprompt(0, NULL); |
---|
2084 | flush(); |
---|
2085 | setalarm(1); |
---|
2086 | } |
---|
2087 | if (seterr) { |
---|
2088 | xfree((ptr_t) seterr); |
---|
2089 | seterr = NULL; |
---|
2090 | } |
---|
2091 | |
---|
2092 | /* |
---|
2093 | * Echo not only on VERBOSE, but also with history expansion. If there |
---|
2094 | * is a lexical error then we forego history echo. |
---|
2095 | */ |
---|
2096 | if ((lex(¶ml) && !seterr && intty && !tellwhat && !Expand && |
---|
2097 | !whyles) || adrof(STRverbose)) { |
---|
2098 | int odidfds = didfds; |
---|
2099 | haderr = 1; |
---|
2100 | didfds = 0; |
---|
2101 | prlex(¶ml); |
---|
2102 | flush(); |
---|
2103 | haderr = 0; |
---|
2104 | didfds = odidfds; |
---|
2105 | } |
---|
2106 | (void) alarm(0); /* Autologout OFF */ |
---|
2107 | |
---|
2108 | /* |
---|
2109 | * The parser may lose space if interrupted. |
---|
2110 | */ |
---|
2111 | if (setintr) |
---|
2112 | #ifdef BSDSIGS |
---|
2113 | (void) sigblock(sigmask(SIGINT)); |
---|
2114 | #else |
---|
2115 | (void) sighold(SIGINT); |
---|
2116 | #endif |
---|
2117 | |
---|
2118 | /* |
---|
2119 | * Save input text on the history list if reading in old history, or it |
---|
2120 | * is from the terminal at the top level and not in a loop. |
---|
2121 | * |
---|
2122 | * PWP: entry of items in the history list while in a while loop is done |
---|
2123 | * elsewhere... |
---|
2124 | */ |
---|
2125 | if (enterhist || (catch && intty && !whyles && !tellwhat && !arun)) |
---|
2126 | savehist(¶ml, enterhist > 1); |
---|
2127 | |
---|
2128 | if (Expand && seterr) |
---|
2129 | Expand = 0; |
---|
2130 | |
---|
2131 | /* |
---|
2132 | * Print lexical error messages, except when sourcing history lists. |
---|
2133 | */ |
---|
2134 | if (!enterhist && seterr) |
---|
2135 | stderror(ERR_OLD); |
---|
2136 | |
---|
2137 | /* |
---|
2138 | * If had a history command :p modifier then this is as far as we |
---|
2139 | * should go |
---|
2140 | */ |
---|
2141 | if (justpr) |
---|
2142 | reset(); |
---|
2143 | |
---|
2144 | /* |
---|
2145 | * If had a tellwhat from twenex() then do |
---|
2146 | */ |
---|
2147 | if (tellwhat) { |
---|
2148 | (void) tellmewhat(¶ml, NULL); |
---|
2149 | reset(); |
---|
2150 | } |
---|
2151 | |
---|
2152 | alias(¶ml); |
---|
2153 | |
---|
2154 | #ifdef BSDJOBS |
---|
2155 | /* |
---|
2156 | * If we are interactive, try to continue jobs that we have stopped |
---|
2157 | */ |
---|
2158 | if (prompt) |
---|
2159 | continue_jobs(¶ml); |
---|
2160 | #endif /* BSDJOBS */ |
---|
2161 | |
---|
2162 | /* |
---|
2163 | * Check to see if the user typed "rm * .o" or something |
---|
2164 | */ |
---|
2165 | if (prompt) |
---|
2166 | rmstar(¶ml); |
---|
2167 | /* |
---|
2168 | * Parse the words of the input into a parse tree. |
---|
2169 | */ |
---|
2170 | savet = syntax(paraml.next, ¶ml, 0); |
---|
2171 | if (seterr) |
---|
2172 | stderror(ERR_OLD); |
---|
2173 | |
---|
2174 | postcmd(); |
---|
2175 | /* |
---|
2176 | * Execute the parse tree From: Michael Schroeder |
---|
2177 | * <mlschroe@immd4.informatik.uni-erlangen.de> was execute(t, tpgrp); |
---|
2178 | */ |
---|
2179 | execute(savet, (tpgrp > 0 ? tpgrp : -1), NULL, NULL, TRUE); |
---|
2180 | |
---|
2181 | /* |
---|
2182 | * Made it! |
---|
2183 | */ |
---|
2184 | freelex(¶ml); |
---|
2185 | freesyn(savet), savet = NULL; |
---|
2186 | #ifdef SIG_WINDOW |
---|
2187 | if (windowchg || (catch && intty && !whyles && !tellwhat)) { |
---|
2188 | (void) check_window_size(0); /* for window systems */ |
---|
2189 | } |
---|
2190 | #endif /* SIG_WINDOW */ |
---|
2191 | set(STR_, Strsave(InputBuf), VAR_READWRITE | VAR_NOGLOB); |
---|
2192 | } |
---|
2193 | savet = t; |
---|
2194 | resexit(osetexit); |
---|
2195 | } |
---|
2196 | |
---|
2197 | /*ARGSUSED*/ |
---|
2198 | void |
---|
2199 | dosource(t, c) |
---|
2200 | Char **t; |
---|
2201 | struct command *c; |
---|
2202 | { |
---|
2203 | Char *f; |
---|
2204 | int hflg = 0; |
---|
2205 | char buf[BUFSIZE]; |
---|
2206 | |
---|
2207 | USE(c); |
---|
2208 | t++; |
---|
2209 | if (*t && eq(*t, STRmh)) { |
---|
2210 | if (*++t == NULL) |
---|
2211 | stderror(ERR_NAME | ERR_HFLAG); |
---|
2212 | hflg++; |
---|
2213 | } |
---|
2214 | else if (*t && eq(*t, STRmm)) { |
---|
2215 | if (*++t == NULL) |
---|
2216 | stderror(ERR_NAME | ERR_MFLAG); |
---|
2217 | hflg = 2; |
---|
2218 | } |
---|
2219 | |
---|
2220 | f = globone(*t++, G_ERROR); |
---|
2221 | (void) strcpy(buf, short2str(f)); |
---|
2222 | xfree((ptr_t) f); |
---|
2223 | gflag = 0, tglob(t); |
---|
2224 | if (gflag) { |
---|
2225 | t = globall(t); |
---|
2226 | if (t == 0) |
---|
2227 | stderror(ERR_NAME | ERR_NOMATCH); |
---|
2228 | } else { |
---|
2229 | t = saveblk(t); |
---|
2230 | trim(t); |
---|
2231 | } |
---|
2232 | if ((!srcfile(buf, 0, hflg, t)) && (!hflg) && (!bequiet)) |
---|
2233 | stderror(ERR_SYSTEM, buf, strerror(errno)); |
---|
2234 | } |
---|
2235 | |
---|
2236 | /* |
---|
2237 | * Check for mail. |
---|
2238 | * If we are a login shell, then we don't want to tell |
---|
2239 | * about any mail file unless its been modified |
---|
2240 | * after the time we started. |
---|
2241 | * This prevents us from telling the user things he already |
---|
2242 | * knows, since the login program insists on saying |
---|
2243 | * "You have mail." |
---|
2244 | */ |
---|
2245 | |
---|
2246 | /* |
---|
2247 | * The AMS version. |
---|
2248 | * This version checks if the file is a directory, and if so, |
---|
2249 | * tells you the number of files in it, otherwise do the old thang. |
---|
2250 | * The magic "+1" in the time calculation is to compensate for |
---|
2251 | * an AFS bug where directory mtimes are set to 1 second in |
---|
2252 | * the future. |
---|
2253 | */ |
---|
2254 | |
---|
2255 | static void |
---|
2256 | mailchk() |
---|
2257 | { |
---|
2258 | struct varent *v; |
---|
2259 | Char **vp; |
---|
2260 | time_t t; |
---|
2261 | int intvl, cnt; |
---|
2262 | struct stat stb; |
---|
2263 | int new; |
---|
2264 | |
---|
2265 | v = adrof(STRmail); |
---|
2266 | if (v == NULL || v->vec == NULL) |
---|
2267 | return; |
---|
2268 | (void) time(&t); |
---|
2269 | vp = v->vec; |
---|
2270 | cnt = blklen(vp); |
---|
2271 | intvl = (cnt && number(*vp)) ? (--cnt, getn(*vp++)) : MAILINTVL; |
---|
2272 | if (intvl < 1) |
---|
2273 | intvl = 1; |
---|
2274 | if (chktim + intvl > t) |
---|
2275 | return; |
---|
2276 | for (; *vp; vp++) { |
---|
2277 | char *filename = short2str(*vp); |
---|
2278 | char *mboxdir = filename; |
---|
2279 | |
---|
2280 | if (stat(filename, &stb) < 0) |
---|
2281 | continue; |
---|
2282 | #if defined(BSDTIMES) || defined(_SEQUENT_) |
---|
2283 | new = stb.st_mtime > time0.tv_sec; |
---|
2284 | #else |
---|
2285 | new = stb.st_mtime > seconds0; |
---|
2286 | #endif |
---|
2287 | if (S_ISDIR(stb.st_mode)) { |
---|
2288 | DIR *mailbox; |
---|
2289 | int mailcount = 0; |
---|
2290 | char tempfilename[MAXPATHLEN]; |
---|
2291 | struct stat stc; |
---|
2292 | |
---|
2293 | xsnprintf(tempfilename, MAXPATHLEN, "%s/new", filename); |
---|
2294 | |
---|
2295 | if (stat(tempfilename, &stc) != -1 && S_ISDIR(stc.st_mode)) { |
---|
2296 | /* |
---|
2297 | * "filename/new" exists and is a directory; you are |
---|
2298 | * using Qmail. |
---|
2299 | */ |
---|
2300 | stb = stc; |
---|
2301 | #if defined(BSDTIMES) || defined(_SEQUENT_) |
---|
2302 | new = stb.st_mtime > time0.tv_sec; |
---|
2303 | #else |
---|
2304 | new = stb.st_mtime > seconds0; |
---|
2305 | #endif |
---|
2306 | mboxdir = tempfilename; |
---|
2307 | } |
---|
2308 | |
---|
2309 | if (stb.st_mtime <= chktim + 1 || (loginsh && !new)) |
---|
2310 | continue; |
---|
2311 | |
---|
2312 | if ((mailbox = opendir(mboxdir)) == NULL) |
---|
2313 | continue; |
---|
2314 | |
---|
2315 | /* skip . and .. */ |
---|
2316 | if (!readdir(mailbox) || !readdir(mailbox)) |
---|
2317 | continue; |
---|
2318 | |
---|
2319 | while (readdir(mailbox)) |
---|
2320 | mailcount++; |
---|
2321 | |
---|
2322 | if (mailcount == 0) |
---|
2323 | continue; |
---|
2324 | |
---|
2325 | if (cnt == 1) |
---|
2326 | xprintf(CGETS(11, 3, "You have %d mail messages.\n"), |
---|
2327 | mailcount); |
---|
2328 | else |
---|
2329 | xprintf(CGETS(11, 4, "You have %d mail messages in %s.\n"), |
---|
2330 | mailcount, filename); |
---|
2331 | } |
---|
2332 | else { |
---|
2333 | char *type; |
---|
2334 | |
---|
2335 | if (stb.st_size == 0 || stb.st_atime > stb.st_mtime || |
---|
2336 | (stb.st_atime <= chktim && stb.st_mtime <= chktim) || |
---|
2337 | (loginsh && !new)) |
---|
2338 | continue; |
---|
2339 | type = strsave(new ? CGETS(11, 6, "new ") : ""); |
---|
2340 | if (cnt == 1) |
---|
2341 | xprintf(CGETS(11, 5, "You have %smail.\n"), type); |
---|
2342 | else |
---|
2343 | xprintf(CGETS(11, 7, "You have %smail in %s.\n"), type, filename); |
---|
2344 | xfree(type); |
---|
2345 | } |
---|
2346 | } |
---|
2347 | chktim = t; |
---|
2348 | } |
---|
2349 | |
---|
2350 | /* |
---|
2351 | * Extract a home directory from the password file |
---|
2352 | * The argument points to a buffer where the name of the |
---|
2353 | * user whose home directory is sought is currently. |
---|
2354 | * We write the home directory of the user back there. |
---|
2355 | */ |
---|
2356 | int |
---|
2357 | gethdir(home) |
---|
2358 | Char *home; |
---|
2359 | { |
---|
2360 | Char *h; |
---|
2361 | |
---|
2362 | /* |
---|
2363 | * Is it us? |
---|
2364 | */ |
---|
2365 | if (*home == '\0') { |
---|
2366 | if ((h = varval(STRhome)) != STRNULL) { |
---|
2367 | (void) Strcpy(home, h); |
---|
2368 | return 0; |
---|
2369 | } |
---|
2370 | else |
---|
2371 | return 1; |
---|
2372 | } |
---|
2373 | |
---|
2374 | /* |
---|
2375 | * Look in the cache |
---|
2376 | */ |
---|
2377 | if ((h = gettilde(home)) == NULL) |
---|
2378 | return 1; |
---|
2379 | else { |
---|
2380 | (void) Strcpy(home, h); |
---|
2381 | return 0; |
---|
2382 | } |
---|
2383 | } |
---|
2384 | |
---|
2385 | /* |
---|
2386 | * Move the initial descriptors to their eventual |
---|
2387 | * resting places, closing all other units. |
---|
2388 | */ |
---|
2389 | void |
---|
2390 | initdesc() |
---|
2391 | { |
---|
2392 | #ifdef NLS_BUGS |
---|
2393 | #ifdef NLS_CATALOGS |
---|
2394 | nlsclose(); |
---|
2395 | #endif /* NLS_CATALOGS */ |
---|
2396 | #endif /* NLS_BUGS */ |
---|
2397 | |
---|
2398 | |
---|
2399 | didfds = 0; /* 0, 1, 2 aren't set up */ |
---|
2400 | (void) close_on_exec(SHIN = dcopy(0, FSHIN), 1); |
---|
2401 | (void) close_on_exec(SHOUT = dcopy(1, FSHOUT), 1); |
---|
2402 | (void) close_on_exec(SHDIAG = dcopy(2, FSHDIAG), 1); |
---|
2403 | (void) close_on_exec(OLDSTD = dcopy(SHIN, FOLDSTD), 1); |
---|
2404 | #ifndef CLOSE_ON_EXEC |
---|
2405 | didcch = 0; /* Havent closed for child */ |
---|
2406 | #endif /* CLOSE_ON_EXEC */ |
---|
2407 | isdiagatty = isatty(SHDIAG); |
---|
2408 | isoutatty = isatty(SHOUT); |
---|
2409 | #ifdef NLS_BUGS |
---|
2410 | #ifdef NLS_CATALOGS |
---|
2411 | nlsinit(); |
---|
2412 | #endif /* NLS_CATALOGS */ |
---|
2413 | #endif /* NLS_BUGS */ |
---|
2414 | } |
---|
2415 | |
---|
2416 | |
---|
2417 | void |
---|
2418 | #ifdef PROF |
---|
2419 | done(i) |
---|
2420 | #else |
---|
2421 | xexit(i) |
---|
2422 | #endif |
---|
2423 | int i; |
---|
2424 | { |
---|
2425 | #ifdef TESLA |
---|
2426 | if (loginsh && do_logout) { |
---|
2427 | /* this is to send hangup signal to the develcon */ |
---|
2428 | /* we toggle DTR. clear dtr - sleep 1 - set dtr */ |
---|
2429 | /* ioctl will return ENOTTY for pty's but we ignore it */ |
---|
2430 | /* exitstat will run after disconnect */ |
---|
2431 | /* we sleep for 2 seconds to let things happen in */ |
---|
2432 | /* .logout and rechist() */ |
---|
2433 | #ifdef TIOCCDTR |
---|
2434 | (void) sleep(2); |
---|
2435 | (void) ioctl(FSHTTY, TIOCCDTR, NULL); |
---|
2436 | (void) sleep(1); |
---|
2437 | (void) ioctl(FSHTTY, TIOCSDTR, NULL); |
---|
2438 | #endif /* TIOCCDTR */ |
---|
2439 | } |
---|
2440 | #endif /* TESLA */ |
---|
2441 | |
---|
2442 | { |
---|
2443 | struct process *pp, *np; |
---|
2444 | |
---|
2445 | /* Kill all processes marked for hup'ing */ |
---|
2446 | for (pp = proclist.p_next; pp; pp = pp->p_next) { |
---|
2447 | np = pp; |
---|
2448 | do |
---|
2449 | if ((np->p_flags & PHUP) && np->p_jobid != shpgrp) { |
---|
2450 | if (killpg(np->p_jobid, SIGHUP) != -1) { |
---|
2451 | /* In case the job was suspended... */ |
---|
2452 | #ifdef SIGCONT |
---|
2453 | (void) killpg(np->p_jobid, SIGCONT); |
---|
2454 | #endif |
---|
2455 | break; |
---|
2456 | } |
---|
2457 | } |
---|
2458 | while ((np = np->p_friends) != pp); |
---|
2459 | } |
---|
2460 | } |
---|
2461 | untty(); |
---|
2462 | #ifdef NLS_CATALOGS |
---|
2463 | /* |
---|
2464 | * We need to call catclose, because SVR4 leaves symlinks behind otherwise |
---|
2465 | * in the catalog directories. We cannot close on a vforked() child, |
---|
2466 | * because messages will stop working on the parent too. |
---|
2467 | */ |
---|
2468 | if (child == 0) |
---|
2469 | nlsclose(); |
---|
2470 | #endif /* NLS_CATALOGS */ |
---|
2471 | #ifdef WINNT_NATIVE |
---|
2472 | nt_cleanup(); |
---|
2473 | #endif /* WINNT_NATIVE */ |
---|
2474 | _exit(i); |
---|
2475 | } |
---|
2476 | |
---|
2477 | #ifndef _PATH_DEFPATH |
---|
2478 | static Char ** |
---|
2479 | defaultpath() |
---|
2480 | { |
---|
2481 | char *ptr; |
---|
2482 | Char **blk, **blkp; |
---|
2483 | struct stat stb; |
---|
2484 | |
---|
2485 | blkp = blk = (Char **) xmalloc((size_t) sizeof(Char *) * 10); |
---|
2486 | |
---|
2487 | #ifndef NODOT |
---|
2488 | # ifndef DOTLAST |
---|
2489 | *blkp++ = Strsave(STRdot); |
---|
2490 | # endif |
---|
2491 | #endif |
---|
2492 | |
---|
2493 | #define DIRAPPEND(a) \ |
---|
2494 | if (stat(ptr = a, &stb) == 0 && S_ISDIR(stb.st_mode)) \ |
---|
2495 | *blkp++ = SAVE(ptr) |
---|
2496 | |
---|
2497 | #ifdef _PATH_LOCAL |
---|
2498 | DIRAPPEND(_PATH_LOCAL); |
---|
2499 | #endif |
---|
2500 | |
---|
2501 | #ifdef _PATH_USRUCB |
---|
2502 | DIRAPPEND(_PATH_USRUCB); |
---|
2503 | #endif |
---|
2504 | |
---|
2505 | #ifdef _PATH_USRBSD |
---|
2506 | DIRAPPEND(_PATH_USRBSD); |
---|
2507 | #endif |
---|
2508 | |
---|
2509 | #ifdef _PATH_BIN |
---|
2510 | DIRAPPEND(_PATH_BIN); |
---|
2511 | #endif |
---|
2512 | |
---|
2513 | #ifdef _PATH_USRBIN |
---|
2514 | DIRAPPEND(_PATH_USRBIN); |
---|
2515 | #endif |
---|
2516 | |
---|
2517 | #undef DIRAPPEND |
---|
2518 | |
---|
2519 | #ifndef NODOT |
---|
2520 | # ifdef DOTLAST |
---|
2521 | *blkp++ = Strsave(STRdot); |
---|
2522 | # endif |
---|
2523 | #endif |
---|
2524 | *blkp = NULL; |
---|
2525 | return (blk); |
---|
2526 | } |
---|
2527 | #endif |
---|
2528 | |
---|
2529 | static void |
---|
2530 | record() |
---|
2531 | { |
---|
2532 | if (!fast) { |
---|
2533 | recdirs(NULL, adrof(STRsavedirs) != NULL); |
---|
2534 | rechist(NULL, adrof(STRsavehist) != NULL); |
---|
2535 | } |
---|
2536 | } |
---|