Import tcsh-6.17.00
[dragonfly.git] / contrib / tcsh-6 / sh.func.c
CommitLineData
57e3f2b5 1/* $Header: /p/tcsh/cvsroot/tcsh/sh.func.c,v 3.153 2009/06/25 21:15:37 christos Exp $ */
7d8fb588
MS
2/*
3 * sh.func.c: csh builtin functions
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#include "sh.h"
34
57e3f2b5 35RCSID("$tcsh: sh.func.c,v 3.153 2009/06/25 21:15:37 christos Exp $")
7d8fb588
MS
36
37#include "ed.h"
38#include "tw.h"
39#include "tc.h"
40#ifdef WINNT_NATIVE
41#include "nt.const.h"
42#endif /* WINNT_NATIVE */
43
44#if defined (NLS_CATALOGS) && defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
45#include <langinfo.h>
46static iconv_t catgets_iconv; /* Or (iconv_t)-1 */
47#endif
48
49/*
50 * C shell
51 */
52
53extern int MapsAreInited;
54extern int NLSMapsAreInited;
55extern int GotTermCaps;
56
57static int zlast = -1;
58
59static void islogin (void);
60static void preread (void);
61static void doagain (void);
62static const char *isrchx (int);
63static void search (int, int, Char *);
64static int getword (struct Strbuf *);
57e3f2b5 65static struct wordent *histgetword (struct wordent *);
7d8fb588
MS
66static void toend (void);
67static void xecho (int, Char **);
68static int islocale_var (Char *);
69static void wpfree (struct whyle *);
70
71const struct biltins *
72isbfunc(struct command *t)
73{
74 Char *cp = t->t_dcom[0];
75 const struct biltins *bp, *bp1, *bp2;
76 static struct biltins label = {"", dozip, 0, 0};
77 static struct biltins foregnd = {"%job", dofg1, 0, 0};
78 static struct biltins backgnd = {"%job &", dobg1, 0, 0};
79
80 /*
81 * We never match a builtin that has quoted the first
82 * character; this has been the traditional way to escape
83 * builtin commands.
84 */
85 if (*cp & QUOTE)
86 return NULL;
87
88 if (*cp != ':' && lastchr(cp) == ':') {
89 label.bname = short2str(cp);
90 return (&label);
91 }
92 if (*cp == '%') {
93 if (t->t_dflg & F_AMPERSAND) {
94 t->t_dflg &= ~F_AMPERSAND;
95 backgnd.bname = short2str(cp);
96 return (&backgnd);
97 }
98 foregnd.bname = short2str(cp);
99 return (&foregnd);
100 }
101#ifdef WARP
102 /*
103 * This is a perhaps kludgy way to determine if the warp builtin is to be
104 * acknowledged or not. If checkwarp() fails, then we are to assume that
105 * the warp command is invalid, and carry on as we would handle any other
106 * non-builtin command. -- JDK 2/4/88
107 */
108 if (eq(STRwarp, cp) && !checkwarp()) {
109 return (0); /* this builtin disabled */
110 }
111#endif /* WARP */
112 /*
113 * Binary search Bp1 is the beginning of the current search range. Bp2 is
114 * one past the end.
115 */
116 for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2;) {
117 int i;
118
119 bp = bp1 + ((bp2 - bp1) >> 1);
120 if ((i = ((char) *cp) - *bp->bname) == 0 &&
121 (i = StrQcmp(cp, str2short(bp->bname))) == 0)
122 return bp;
123 if (i < 0)
124 bp2 = bp;
125 else
126 bp1 = bp + 1;
127 }
128#ifdef WINNT_NATIVE
129 return nt_check_additional_builtins(cp);
130#endif /*WINNT_NATIVE*/
131 return (0);
132}
133
134void
135func(struct command *t, const struct biltins *bp)
136{
137 int i;
138
139 xechoit(t->t_dcom);
140 setname(bp->bname);
141 i = blklen(t->t_dcom) - 1;
142 if (i < bp->minargs)
143 stderror(ERR_NAME | ERR_TOOFEW);
144 if (i > bp->maxargs)
145 stderror(ERR_NAME | ERR_TOOMANY);
146 (*bp->bfunct) (t->t_dcom, t);
147}
148
149/*ARGSUSED*/
150void
151doonintr(Char **v, struct command *c)
152{
153 Char *cp;
154 Char *vv = v[1];
155
156 USE(c);
157 if (parintr.sa_handler == SIG_IGN)
158 return;
159 if (setintr && intty)
160 stderror(ERR_NAME | ERR_TERMINAL);
161 cp = gointr;
162 gointr = 0;
163 xfree(cp);
164 if (vv == 0) {
165 if (setintr)
166 sigset_interrupting(SIGINT, queue_pintr);
167 else
168 (void) signal(SIGINT, SIG_DFL);
169 gointr = 0;
170 }
171 else if (eq((vv = strip(vv)), STRminus)) {
172 (void) signal(SIGINT, SIG_IGN);
173 gointr = Strsave(STRminus);
174 }
175 else {
176 gointr = Strsave(vv);
177 sigset_interrupting(SIGINT, queue_pintr);
178 }
179}
180
181/*ARGSUSED*/
182void
183donohup(Char **v, struct command *c)
184{
185 USE(c);
186 USE(v);
187 if (intty)
188 stderror(ERR_NAME | ERR_TERMINAL);
189 if (setintr == 0) {
190 (void) signal(SIGHUP, SIG_IGN);
191 phup_disabled = 1;
192#ifdef CC
193 submit(getpid());
194#endif /* CC */
195 }
196}
197
198/*ARGSUSED*/
199void
200dohup(Char **v, struct command *c)
201{
202 USE(c);
203 USE(v);
204 if (intty)
205 stderror(ERR_NAME | ERR_TERMINAL);
206 if (setintr == 0)
207 (void) signal(SIGHUP, SIG_DFL);
208}
209
210
211/*ARGSUSED*/
212void
213dozip(Char **v, struct command *c)
214{
215 USE(c);
216 USE(v);
217}
218
219/*ARGSUSED*/
220void
221dofiletest(Char **v, struct command *c)
222{
223 Char **globbed, **fileptr, *ftest, *res;
224
225 USE(c);
226 if (*(ftest = *++v) != '-')
227 stderror(ERR_NAME | ERR_FILEINQ);
228 ++v;
229
230 v = glob_all_or_error(v);
231 globbed = v;
232 cleanup_push(globbed, blk_cleanup);
233
234 while (*(fileptr = v++) != '\0') {
235 res = filetest(ftest, &fileptr, 0);
236 cleanup_push(res, xfree);
237 xprintf("%S", res);
238 cleanup_until(res);
239 if (*v)
240 xprintf(" ");
241 }
242 xprintf("\n");
243
244 cleanup_until(globbed);
245}
246
247void
248prvars(void)
249{
250 plist(&shvhed, VAR_ALL);
251}
252
253/*ARGSUSED*/
254void
255doalias(Char **v, struct command *c)
256{
257 struct varent *vp;
258 Char *p;
259
260 USE(c);
261 v++;
262 p = *v++;
263 if (p == 0)
264 plist(&aliases, VAR_ALL);
265 else if (*v == 0) {
266 vp = adrof1(strip(p), &aliases);
267 if (vp && vp->vec)
268 blkpr(vp->vec), xputchar('\n');
269 }
270 else {
271 if (eq(p, STRalias) || eq(p, STRunalias)) {
272 setname(short2str(p));
273 stderror(ERR_NAME | ERR_DANGER);
274 }
275 set1(strip(p), saveblk(v), &aliases, VAR_READWRITE);
276 tw_cmd_free();
277 }
278}
279
280/*ARGSUSED*/
281void
282unalias(Char **v, struct command *c)
283{
284 USE(c);
285 unset1(v, &aliases);
286 tw_cmd_free();
287}
288
289/*ARGSUSED*/
290void
291dologout(Char **v, struct command *c)
292{
293 USE(c);
294 USE(v);
295 islogin();
296 goodbye(NULL, NULL);
297}
298
299/*ARGSUSED*/
300void
301dologin(Char **v, struct command *c)
302{
303#ifdef WINNT_NATIVE
304 USE(c);
305 USE(v);
306#else /* !WINNT_NATIVE */
307 char **p = short2blk(v);
308
309 USE(c);
310 cleanup_push((Char **)p, blk_cleanup);
311 islogin();
312 rechist(NULL, adrof(STRsavehist) != NULL);
313 sigaction(SIGTERM, &parterm, NULL);
314 (void) execv(_PATH_BIN_LOGIN, p);
315 (void) execv(_PATH_USRBIN_LOGIN, p);
316 cleanup_until((Char **)p);
317 untty();
318 xexit(1);
319#endif /* !WINNT_NATIVE */
320}
321
322
323#ifdef NEWGRP
324/*ARGSUSED*/
325void
326donewgrp(Char **v, struct command *c)
327{
328 char **p;
329 if (chkstop == 0 && setintr)
330 panystop(0);
331 sigaction(SIGTERM, &parterm, NULL);
332 p = short2blk(v);
333 /*
334 * From Beto Appleton (beto@aixwiz.austin.ibm.com)
335 * Newgrp can take 2 arguments...
336 */
337 (void) execv(_PATH_BIN_NEWGRP, p);
338 (void) execv(_PATH_USRBIN_NEWGRP, p);
339 blkfree((Char **) p);
340 untty();
341 xexit(1);
342}
343#endif /* NEWGRP */
344
345static void
346islogin(void)
347{
348 if (chkstop == 0 && setintr)
349 panystop(0);
350 if (loginsh)
351 return;
352 stderror(ERR_NOTLOGIN);
353}
354
355void
356doif(Char **v, struct command *kp)
357{
358 int i;
359 Char **vv;
360
361 v++;
362 i = noexec ? 1 : expr(&v);
363 vv = v;
364 if (*vv == NULL)
365 stderror(ERR_NAME | ERR_EMPTYIF);
366 if (eq(*vv, STRthen)) {
367 if (*++vv)
368 stderror(ERR_NAME | ERR_IMPRTHEN);
369 setname(short2str(STRthen));
370 /*
371 * If expression was zero, then scan to else , otherwise just fall into
372 * following code.
373 */
374 if (!i)
375 search(TC_IF, 0, NULL);
376 return;
377 }
378 /*
379 * Simple command attached to this if. Left shift the node in this tree,
380 * munging it so we can reexecute it.
381 */
382 if (i) {
383 lshift(kp->t_dcom, vv - kp->t_dcom);
384 reexecute(kp);
385 donefds();
386 }
387}
388
389/*
390 * Reexecute a command, being careful not
391 * to redo i/o redirection, which is already set up.
392 */
393void
394reexecute(struct command *kp)
395{
396 kp->t_dflg &= F_SAVE;
397 kp->t_dflg |= F_REPEAT;
398 /*
399 * If tty is still ours to arbitrate, arbitrate it; otherwise dont even set
400 * pgrp's as the jobs would then have no way to get the tty (we can't give
401 * it to them, and our parent wouldn't know their pgrp, etc.
402 */
403 execute(kp, (tpgrp > 0 ? tpgrp : -1), NULL, NULL, TRUE);
404}
405
406/*ARGSUSED*/
407void
408doelse (Char **v, struct command *c)
409{
410 USE(c);
411 USE(v);
412 if (!noexec)
413 search(TC_ELSE, 0, NULL);
414}
415
416/*ARGSUSED*/
417void
418dogoto(Char **v, struct command *c)
419{
420 Char *lp;
421
422 USE(c);
423 lp = globone(v[1], G_ERROR);
424 cleanup_push(lp, xfree);
425 if (!noexec)
426 gotolab(lp);
427 cleanup_until(lp);
428}
429
430void
431gotolab(Char *lab)
432{
433 struct whyle *wp;
434 /*
435 * While we still can, locate any unknown ends of existing loops. This
436 * obscure code is the WORST result of the fact that we don't really parse.
437 */
438 zlast = TC_GOTO;
439 for (wp = whyles; wp; wp = wp->w_next)
440 if (wp->w_end.type == TCSH_F_SEEK && wp->w_end.f_seek == 0) {
441 search(TC_BREAK, 0, NULL);
442 btell(&wp->w_end);
443 }
444 else {
445 bseek(&wp->w_end);
446 }
447 search(TC_GOTO, 0, lab);
448 /*
449 * Eliminate loops which were exited.
450 */
451 wfree();
452}
453
454/*ARGSUSED*/
455void
456doswitch(Char **v, struct command *c)
457{
458 Char *cp, *lp;
459
460 USE(c);
461 v++;
462 if (!*v || *(*v++) != '(')
463 stderror(ERR_SYNTAX);
464 cp = **v == ')' ? STRNULL : *v++;
465 if (*(*v++) != ')')
466 v--;
467 if (*v)
468 stderror(ERR_SYNTAX);
469 lp = globone(cp, G_ERROR);
470 cleanup_push(lp, xfree);
471 if (!noexec)
472 search(TC_SWITCH, 0, lp);
473 cleanup_until(lp);
474}
475
476/*ARGSUSED*/
477void
478dobreak(Char **v, struct command *c)
479{
480 USE(v);
481 USE(c);
482 if (whyles == NULL)
483 stderror(ERR_NAME | ERR_NOTWHILE);
484 if (!noexec)
485 toend();
486}
487
488/*ARGSUSED*/
489void
490doexit(Char **v, struct command *c)
491{
492 USE(c);
493
494 if (chkstop == 0 && (intty || intact) && evalvec == 0)
495 panystop(0);
496 /*
497 * Don't DEMAND parentheses here either.
498 */
499 v++;
500 if (*v) {
501 setv(STRstatus, putn(expr(&v)), VAR_READWRITE);
502 if (*v)
503 stderror(ERR_NAME | ERR_EXPRESSION);
504 }
505 btoeof();
506#if 0
507 if (intty)
508#endif
509 /* Always close, why only on ttys? */
510 xclose(SHIN);
511}
512
513/*ARGSUSED*/
514void
515doforeach(Char **v, struct command *c)
516{
517 Char *cp, *sp;
518 struct whyle *nwp;
519 int gflag;
520
521 USE(c);
522 v++;
523 sp = cp = strip(*v);
524 if (!letter(*sp))
525 stderror(ERR_NAME | ERR_VARBEGIN);
526 while (*cp && alnum(*cp))
527 cp++;
528 if (*cp)
529 stderror(ERR_NAME | ERR_VARALNUM);
530 cp = *v++;
531 if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')')
532 stderror(ERR_NAME | ERR_NOPAREN);
533 v++;
534 gflag = tglob(v);
535 if (gflag) {
536 v = globall(v, gflag);
537 if (v == 0 && !noexec)
538 stderror(ERR_NAME | ERR_NOMATCH);
539 }
540 else {
541 v = saveblk(v);
542 trim(v);
543 }
544 nwp = xcalloc(1, sizeof *nwp);
545 nwp->w_fe = nwp->w_fe0 = v;
546 btell(&nwp->w_start);
547 nwp->w_fename = Strsave(cp);
548 nwp->w_next = whyles;
549 nwp->w_end.type = TCSH_F_SEEK;
550 whyles = nwp;
551 /*
552 * Pre-read the loop so as to be more comprehensible to a terminal user.
553 */
554 zlast = TC_FOREACH;
555 if (intty)
556 preread();
557 if (!noexec)
558 doagain();
559}
560
561/*ARGSUSED*/
562void
563dowhile(Char **v, struct command *c)
564{
565 int status;
566 int again = whyles != 0 &&
567 SEEKEQ(&whyles->w_start, &lineloc) &&
568 whyles->w_fename == 0;
569
570 USE(c);
571 v++;
572 /*
573 * Implement prereading here also, taking care not to evaluate the
574 * expression before the loop has been read up from a terminal.
575 */
576 if (noexec)
577 status = 0;
578 else if (intty && !again)
579 status = !exp0(&v, 1);
580 else
581 status = !expr(&v);
582 if (*v && !noexec)
583 stderror(ERR_NAME | ERR_EXPRESSION);
584 if (!again) {
585 struct whyle *nwp = xcalloc(1, sizeof(*nwp));
586
587 nwp->w_start = lineloc;
588 nwp->w_end.type = TCSH_F_SEEK;
589 nwp->w_end.f_seek = 0;
590 nwp->w_next = whyles;
591 whyles = nwp;
592 zlast = TC_WHILE;
593 if (intty) {
594 /*
595 * The tty preread
596 */
597 preread();
598 doagain();
599 return;
600 }
601 }
602 if (status)
603 /* We ain't gonna loop no more, no more! */
604 toend();
605}
606
607static void
608preread(void)
609{
610 int old_pintr_disabled;
611
612 whyles->w_end.type = TCSH_I_SEEK;
613 if (setintr)
614 pintr_push_enable(&old_pintr_disabled);
615 search(TC_BREAK, 0, NULL); /* read the expression in */
616 if (setintr)
617 cleanup_until(&old_pintr_disabled);
618 btell(&whyles->w_end);
619}
620
621/*ARGSUSED*/
622void
623doend(Char **v, struct command *c)
624{
625 USE(v);
626 USE(c);
627 if (!whyles)
628 stderror(ERR_NAME | ERR_NOTWHILE);
629 btell(&whyles->w_end);
630 if (!noexec)
631 doagain();
632}
633
634/*ARGSUSED*/
635void
636docontin(Char **v, struct command *c)
637{
638 USE(v);
639 USE(c);
640 if (!whyles)
641 stderror(ERR_NAME | ERR_NOTWHILE);
642 if (!noexec)
643 doagain();
644}
645
646static void
647doagain(void)
648{
649 /* Repeating a while is simple */
650 if (whyles->w_fename == 0) {
651 bseek(&whyles->w_start);
652 return;
653 }
654 /*
655 * The foreach variable list actually has a spurious word ")" at the end of
656 * the w_fe list. Thus we are at the of the list if one word beyond this
657 * is 0.
658 */
659 if (!whyles->w_fe[1]) {
660 dobreak(NULL, NULL);
661 return;
662 }
663 setv(whyles->w_fename, quote(Strsave(*whyles->w_fe++)), VAR_READWRITE);
664 bseek(&whyles->w_start);
665}
666
667void
668dorepeat(Char **v, struct command *kp)
669{
670 int i = 1;
671
672 do {
673 i *= getn(v[1]);
674 lshift(v, 2);
675 } while (v[0] != NULL && Strcmp(v[0], STRrepeat) == 0);
676 if (noexec)
677 i = 1;
678
679 if (setintr) {
680 pintr_disabled++;
681 cleanup_push(&pintr_disabled, disabled_cleanup);
682 }
683 while (i > 0) {
684 if (setintr && pintr_disabled == 1) {
685 cleanup_until(&pintr_disabled);
686 pintr_disabled++;
687 cleanup_push(&pintr_disabled, disabled_cleanup);
688 }
689 reexecute(kp);
690 --i;
691 }
57e3f2b5
SS
692 if (setintr && pintr_disabled == 1)
693 cleanup_until(&pintr_disabled);
7d8fb588
MS
694 donefds();
695}
696
697/*ARGSUSED*/
698void
699doswbrk(Char **v, struct command *c)
700{
701 USE(v);
702 USE(c);
703 if (!noexec)
704 search(TC_BRKSW, 0, NULL);
705}
706
707int
708srchx(Char *cp)
709{
710 struct srch *sp, *sp1, *sp2;
711 int i;
712
713 /*
714 * Ignore keywords inside heredocs
715 */
716 if (inheredoc)
717 return -1;
718
719 /*
720 * Binary search Sp1 is the beginning of the current search range. Sp2 is
721 * one past the end.
722 */
723 for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2;) {
724 sp = sp1 + ((sp2 - sp1) >> 1);
725 if ((i = *cp - *sp->s_name) == 0 &&
726 (i = Strcmp(cp, str2short(sp->s_name))) == 0)
727 return sp->s_value;
728 if (i < 0)
729 sp2 = sp;
730 else
731 sp1 = sp + 1;
732 }
733 return (-1);
734}
735
736static const char *
737isrchx(int n)
738{
739 struct srch *sp, *sp2;
740
741 for (sp = srchn, sp2 = srchn + nsrchn; sp < sp2; sp++)
742 if (sp->s_value == n)
743 return (sp->s_name);
744 return ("");
745}
746
747
748static int Stype;
749static Char *Sgoal;
750
751static void
752search(int type, int level, Char *goal)
753{
754 struct Strbuf word = Strbuf_INIT;
755 Char *cp;
756 struct whyle *wp;
757 int wlevel = 0;
57e3f2b5 758 struct wordent *histent = NULL, *ohistent = NULL;
7d8fb588
MS
759
760 Stype = type;
761 Sgoal = goal;
762 if (type == TC_GOTO) {
763 struct Ain a;
764 a.type = TCSH_F_SEEK;
765 a.f_seek = 0;
766 bseek(&a);
767 }
768 cleanup_push(&word, Strbuf_cleanup);
769 do {
57e3f2b5
SS
770
771 if (intty) {
772 histent = xmalloc(sizeof(*histent));
773 ohistent = xmalloc(sizeof(*histent));
774 ohistent->word = STRNULL;
775 ohistent->next = histent;
776 histent->prev = ohistent;
777 }
778
7d8fb588
MS
779 if (intty && fseekp == feobp && aret == TCSH_F_SEEK)
780 printprompt(1, isrchx(type == TC_BREAK ? zlast : type));
781 /* xprintf("? "), flush(); */
782 (void) getword(&word);
783 Strbuf_terminate(&word);
57e3f2b5
SS
784
785 if (intty && Strlen(word.s) > 0) {
786 histent->word = Strsave(word.s);
787 histent->next = xmalloc(sizeof(*histent));
788 histent->next->prev = histent;
789 histent = histent->next;
790 }
791
7d8fb588
MS
792 switch (srchx(word.s)) {
793
794 case TC_ELSE:
795 if (level == 0 && type == TC_IF)
796 goto end;
797 break;
798
799 case TC_IF:
800 while (getword(&word))
801 continue;
802 if ((type == TC_IF || type == TC_ELSE) &&
803 eq(word.s, STRthen))
804 level++;
805 break;
806
807 case TC_ENDIF:
808 if (type == TC_IF || type == TC_ELSE)
809 level--;
810 break;
811
812 case TC_FOREACH:
813 case TC_WHILE:
814 wlevel++;
815 if (type == TC_BREAK)
816 level++;
817 break;
818
819 case TC_END:
820 if (type == TC_BRKSW) {
821 if (wlevel == 0) {
822 wp = whyles;
823 if (wp) {
824 whyles = wp->w_next;
825 wpfree(wp);
826 }
827 }
828 }
829 if (type == TC_BREAK)
830 level--;
831 wlevel--;
832 break;
833
834 case TC_SWITCH:
835 if (type == TC_SWITCH || type == TC_BRKSW)
836 level++;
837 break;
838
839 case TC_ENDSW:
840 if (type == TC_SWITCH || type == TC_BRKSW)
841 level--;
842 break;
843
844 case TC_LABEL:
845 if (type == TC_GOTO && getword(&word) && eq(word.s, goal))
846 level = -1;
847 break;
848
849 default:
850 if (type != TC_GOTO && (type != TC_SWITCH || level != 0))
851 break;
852 if (word.len == 0 || word.s[word.len - 1] != ':')
853 break;
854 word.s[--word.len] = 0;
855 if ((type == TC_GOTO && eq(word.s, goal)) ||
856 (type == TC_SWITCH && eq(word.s, STRdefault)))
857 level = -1;
858 break;
859
860 case TC_CASE:
861 if (type != TC_SWITCH || level != 0)
862 break;
863 (void) getword(&word);
864 if (word.len != 0 && word.s[word.len - 1] == ':')
865 word.s[--word.len] = 0;
866 cp = strip(Dfix1(word.s));
867 cleanup_push(cp, xfree);
868 if (Gmatch(goal, cp))
869 level = -1;
870 cleanup_until(cp);
871 break;
872
873 case TC_DEFAULT:
874 if (type == TC_SWITCH && level == 0)
875 level = -1;
876 break;
877 }
57e3f2b5
SS
878 if (intty) {
879 ohistent->prev = histgetword(histent);
880 ohistent->prev->next = ohistent;
881 savehist(ohistent, 0);
882 freelex(ohistent);
883 xfree(ohistent);
884 } else
885 (void) getword(NULL);
7d8fb588
MS
886 } while (level >= 0);
887 end:
888 cleanup_until(&word);
889}
890
57e3f2b5
SS
891static struct wordent *
892histgetword(struct wordent *histent)
893{
894 int found = 0, first;
895 eChar c, d;
896 int e;
897 struct Strbuf *tmp;
898 tmp = xmalloc(sizeof(*tmp));
899 tmp->size = 0;
900 tmp->s = NULL;
901 c = readc(1);
902 d = 0;
903 e = 0;
904 for (;;) {
905 tmp->len = 0;
906 Strbuf_terminate (tmp);
907 while (c == ' ' || c == '\t')
908 c = readc(1);
909 if (c == '#')
910 do
911 c = readc(1);
912 while (c != CHAR_ERR && c != '\n');
913 if (c == CHAR_ERR)
914 goto past;
915 if (c == '\n')
916 goto nl;
917 unreadc(c);
918 found = 1;
919 first = 1;
920 do {
921 e = (c == '\\');
922 c = readc(1);
923 if (c == '\\' && !e) {
924 if ((c = readc(1)) == '\n') {
925 e = 1;
926 c = ' ';
927 } else {
928 unreadc(c);
929 c = '\\';
930 }
931 }
932 if ((c == '\'' || c == '"') && !e) {
933 if (d == 0)
934 d = c;
935 else if (d == c)
936 d = 0;
937 }
938 if (c == CHAR_ERR)
939 goto past;
940
941 Strbuf_append1(tmp, (Char) c);
942
943 if (!first && !d && c == '(' && !e) {
944 break;
945 }
946 first = 0;
947 } while (d || e || (c != ' ' && c != '\t' && c != '\n'));
948 tmp->len--;
949 if (tmp->len) {
950 Strbuf_terminate(tmp);
951 histent->word = Strsave(tmp->s);
952 histent->next = xmalloc(sizeof (*histent));
953 histent->next->prev = histent;
954 histent = histent->next;
955 }
956 if (c == '\n') {
957 nl:
958 tmp->len = 0;
959 Strbuf_append1(tmp, (Char) c);
960 Strbuf_terminate(tmp);
961 histent->word = Strsave(tmp->s);
962 return histent;
963 }
964 }
965
966 unreadc(c);
967 return histent;
968
969past:
970 switch (Stype) {
971
972 case TC_IF:
973 stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
974 break;
975
976 case TC_ELSE:
977 stderror(ERR_NAME | ERR_NOTFOUND, "endif");
978 break;
979
980 case TC_BRKSW:
981 case TC_SWITCH:
982 stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
983 break;
984
985 case TC_BREAK:
986 stderror(ERR_NAME | ERR_NOTFOUND, "end");
987 break;
988
989 case TC_GOTO:
990 setname(short2str(Sgoal));
991 stderror(ERR_NAME | ERR_NOTFOUND, "label");
992 break;
993
994 default:
995 break;
996 }
997 /* NOTREACHED */
998 return NULL;
999}
1000
7d8fb588
MS
1001static int
1002getword(struct Strbuf *wp)
1003{
1004 int found = 0, first;
1005 eChar c, d;
1006
1007 if (wp)
1008 wp->len = 0;
1009 c = readc(1);
1010 d = 0;
1011 do {
1012 while (c == ' ' || c == '\t')
1013 c = readc(1);
1014 if (c == '#')
1015 do
1016 c = readc(1);
1017 while (c != CHAR_ERR && c != '\n');
1018 if (c == CHAR_ERR)
1019 goto past;
1020 if (c == '\n') {
1021 if (wp)
1022 break;
1023 return (0);
1024 }
1025 unreadc(c);
1026 found = 1;
1027 first = 1;
1028 do {
1029 c = readc(1);
1030 if (c == '\\' && (c = readc(1)) == '\n')
1031 c = ' ';
1032 if (c == '\'' || c == '"') {
1033 if (d == 0)
1034 d = c;
1035 else if (d == c)
1036 d = 0;
1037 }
1038 if (c == CHAR_ERR)
1039 goto past;
1040 if (wp)
1041 Strbuf_append1(wp, (Char) c);
1042 if (!first && !d && c == '(') {
1043 if (wp)
1044 goto past_word_end;
1045 else
1046 break;
1047 }
1048 first = 0;
1049 } while ((d || (c != ' ' && c != '\t')) && c != '\n');
1050 } while (wp == 0);
1051
1052 past_word_end:
1053 unreadc(c);
1054 if (found) {
1055 wp->len--;
1056 Strbuf_terminate(wp);
1057 }
1058
1059 return (found);
1060
1061past:
1062 switch (Stype) {
1063
1064 case TC_IF:
1065 stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
1066 break;
1067
1068 case TC_ELSE:
1069 stderror(ERR_NAME | ERR_NOTFOUND, "endif");
1070 break;
1071
1072 case TC_BRKSW:
1073 case TC_SWITCH:
1074 stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
1075 break;
1076
1077 case TC_BREAK:
1078 stderror(ERR_NAME | ERR_NOTFOUND, "end");
1079 break;
1080
1081 case TC_GOTO:
1082 setname(short2str(Sgoal));
1083 stderror(ERR_NAME | ERR_NOTFOUND, "label");
1084 break;
1085
1086 default:
1087 break;
1088 }
1089 /* NOTREACHED */
1090 return (0);
1091}
1092
1093static void
1094toend(void)
1095{
1096 if (whyles->w_end.type == TCSH_F_SEEK && whyles->w_end.f_seek == 0) {
1097 search(TC_BREAK, 0, NULL);
1098 btell(&whyles->w_end);
1099 whyles->w_end.f_seek--;
1100 }
1101 else {
1102 bseek(&whyles->w_end);
1103 }
1104 wfree();
1105}
1106
1107static void
1108wpfree(struct whyle *wp)
1109{
1110 if (wp->w_fe0)
1111 blkfree(wp->w_fe0);
1112 xfree(wp->w_fename);
1113 xfree(wp);
1114}
1115
1116void
1117wfree(void)
1118{
1119 struct Ain o;
1120 struct whyle *nwp;
1121#ifdef lint
1122 nwp = NULL; /* sun lint is dumb! */
1123#endif
1124
1125#ifdef FDEBUG
1126 static const char foo[] = "IAFE";
1127#endif /* FDEBUG */
1128
1129 btell(&o);
1130
1131#ifdef FDEBUG
1132 xprintf("o->type %c o->a_seek %d o->f_seek %d\n",
1133 foo[o.type + 1], o.a_seek, o.f_seek);
1134#endif /* FDEBUG */
1135
1136 for (; whyles; whyles = nwp) {
1137 struct whyle *wp = whyles;
1138 nwp = wp->w_next;
1139
1140#ifdef FDEBUG
1141 xprintf("start->type %c start->a_seek %d start->f_seek %d\n",
1142 foo[wp->w_start.type+1],
1143 wp->w_start.a_seek, wp->w_start.f_seek);
1144 xprintf("end->type %c end->a_seek %d end->f_seek %d\n",
1145 foo[wp->w_end.type + 1], wp->w_end.a_seek, wp->w_end.f_seek);
1146#endif /* FDEBUG */
1147
1148 /*
1149 * XXX: We free loops that have different seek types.
1150 */
1151 if (wp->w_end.type != TCSH_I_SEEK && wp->w_start.type == wp->w_end.type &&
1152 wp->w_start.type == o.type) {
1153 if (wp->w_end.type == TCSH_F_SEEK) {
1154 if (o.f_seek >= wp->w_start.f_seek &&
1155 (wp->w_end.f_seek == 0 || o.f_seek < wp->w_end.f_seek))
1156 break;
1157 }
1158 else {
1159 if (o.a_seek >= wp->w_start.a_seek &&
1160 (wp->w_end.a_seek == 0 || o.a_seek < wp->w_end.a_seek))
1161 break;
1162 }
1163 }
1164
1165 wpfree(wp);
1166 }
1167}
1168
1169/*ARGSUSED*/
1170void
1171doecho(Char **v, struct command *c)
1172{
1173 USE(c);
1174 xecho(' ', v);
1175}
1176
1177/*ARGSUSED*/
1178void
1179doglob(Char **v, struct command *c)
1180{
1181 USE(c);
1182 xecho(0, v);
1183 flush();
1184}
1185
1186static void
1187xecho(int sep, Char **v)
1188{
1189 Char *cp, **globbed = NULL;
1190 int nonl = 0;
1191 int echo_style = ECHO_STYLE;
1192 struct varent *vp;
1193
1194 if ((vp = adrof(STRecho_style)) != NULL && vp->vec != NULL &&
1195 vp->vec[0] != NULL) {
1196 if (Strcmp(vp->vec[0], STRbsd) == 0)
1197 echo_style = BSD_ECHO;
1198 else if (Strcmp(vp->vec[0], STRsysv) == 0)
1199 echo_style = SYSV_ECHO;
1200 else if (Strcmp(vp->vec[0], STRboth) == 0)
1201 echo_style = BOTH_ECHO;
1202 else if (Strcmp(vp->vec[0], STRnone) == 0)
1203 echo_style = NONE_ECHO;
1204 }
1205
1206 v++;
1207 if (*v == 0)
1208 goto done;
1209 if (setintr) {
1210 int old_pintr_disabled;
1211 pintr_push_enable(&old_pintr_disabled);
1212 v = glob_all_or_error(v);
1213 cleanup_until(&old_pintr_disabled);
1214 } else {
1215 v = glob_all_or_error(v);
1216 }
1217 globbed = v;
1218 if (globbed != NULL)
1219 cleanup_push(globbed, blk_cleanup);
1220
1221 if ((echo_style & BSD_ECHO) != 0 && sep == ' ' && *v && eq(*v, STRmn))
1222 nonl++, v++;
1223
1224 while ((cp = *v++) != 0) {
1225 Char c;
1226
1227 if (setintr) {
1228 int old_pintr_disabled;
1229
1230 pintr_push_enable(&old_pintr_disabled);
1231 cleanup_until(&old_pintr_disabled);
1232 }
1233 while ((c = *cp++) != 0) {
1234 if ((echo_style & SYSV_ECHO) != 0 && c == '\\') {
1235 switch (c = *cp++) {
1236 case 'a':
1237 c = '\a';
1238 break;
1239 case 'b':
1240 c = '\b';
1241 break;
1242 case 'c':
1243 nonl = 1;
1244 goto done;
1245 case 'e':
1246#if 0 /* Windows does not understand \e */
1247 c = '\e';
1248#else
1249 c = CTL_ESC('\033');
1250#endif
1251 break;
1252 case 'f':
1253 c = '\f';
1254 break;
1255 case 'n':
1256 c = '\n';
1257 break;
1258 case 'r':
1259 c = '\r';
1260 break;
1261 case 't':
1262 c = '\t';
1263 break;
1264 case 'v':
1265 c = '\v';
1266 break;
1267 case '\\':
1268 c = '\\';
1269 break;
1270 case '0':
1271 c = 0;
1272 if (*cp >= '0' && *cp < '8')
1273 c = c * 8 + *cp++ - '0';
1274 if (*cp >= '0' && *cp < '8')
1275 c = c * 8 + *cp++ - '0';
1276 if (*cp >= '0' && *cp < '8')
1277 c = c * 8 + *cp++ - '0';
1278 break;
1279 case '\0':
1280 c = '\\';
1281 cp--;
1282 break;
1283 default:
1284 xputchar('\\' | QUOTE);
1285 break;
1286 }
1287 }
1288 xputwchar(c | QUOTE);
1289
1290 }
1291 if (*v)
1292 xputchar(sep | QUOTE);
1293 }
1294done:
1295 if (sep && nonl == 0)
1296 xputchar('\n');
1297 else
1298 flush();
1299 if (globbed != NULL)
1300 cleanup_until(globbed);
1301}
1302
1303/* check whether an environment variable should invoke 'set_locale()' */
1304static int
1305islocale_var(Char *var)
1306{
1307 static Char *locale_vars[] = {
1308 STRLANG, STRLC_ALL, STRLC_CTYPE, STRLC_NUMERIC,
1309 STRLC_TIME, STRLC_COLLATE, STRLC_MESSAGES, STRLC_MONETARY, 0
1310 };
1311 Char **v;
1312
1313 for (v = locale_vars; *v; ++v)
1314 if (eq(var, *v))
1315 return 1;
1316 return 0;
1317}
1318
1319static void
1320xlate_cr_cleanup(void *dummy)
1321{
1322 USE(dummy);
1323 xlate_cr = 0;
1324}
1325
1326/*ARGSUSED*/
1327void
1328doprintenv(Char **v, struct command *c)
1329{
1330 Char *e;
1331
1332 USE(c);
1333 v++;
1334 if (*v == 0) {
1335 Char **ep;
1336
1337 xlate_cr = 1;
1338 cleanup_push(&xlate_cr, xlate_cr_cleanup);
1339 for (ep = STR_environ; *ep; ep++) {
1340 if (setintr) {
1341 int old_pintr_disabled;
1342
1343 pintr_push_enable(&old_pintr_disabled);
1344 cleanup_until(&old_pintr_disabled);
1345 }
1346 xprintf("%S\n", *ep);
1347 }
1348 cleanup_until(&xlate_cr);
1349 }
1350 else if ((e = tgetenv(*v)) != NULL) {
1351 int old_output_raw;
1352
1353 old_output_raw = output_raw;
1354 output_raw = 1;
1355 cleanup_push(&old_output_raw, output_raw_restore);
1356 xprintf("%S\n", e);
1357 cleanup_until(&old_output_raw);
1358 }
1359 else
1360 setcopy(STRstatus, STR1, VAR_READWRITE);
1361}
1362
1363/* from "Karl Berry." <karl%mote.umb.edu@relay.cs.net> -- for NeXT things
1364 (and anything else with a modern compiler) */
1365
1366/*ARGSUSED*/
1367void
1368dosetenv(Char **v, struct command *c)
1369{
1370 Char *vp, *lp;
1371
1372 USE(c);
1373 if (*++v == 0) {
1374 doprintenv(--v, 0);
1375 return;
1376 }
1377
1378 vp = *v++;
1379
1380 lp = vp;
1381
1382 for (; *lp != '\0' ; lp++) {
1383 if (*lp == '=')
1384 stderror(ERR_NAME | ERR_SYNTAX);
1385 }
1386 if ((lp = *v++) == 0)
1387 lp = STRNULL;
1388
1389 lp = globone(lp, G_APPEND);
1390 cleanup_push(lp, xfree);
1391 tsetenv(vp, lp);
1392 if (eq(vp, STRKPATH)) {
1393 importpath(lp);
1394 dohash(NULL, NULL);
1395 cleanup_until(lp);
1396 return;
1397 }
1398
1399#ifdef apollo
1400 if (eq(vp, STRSYSTYPE)) {
1401 dohash(NULL, NULL);
1402 cleanup_until(lp);
1403 return;
1404 }
1405#endif /* apollo */
1406
1407 /* dspkanji/dspmbyte autosetting */
1408 /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
1409#if defined(DSPMBYTE)
1410 if(eq(vp, STRLANG) && !adrof(CHECK_MBYTEVAR)) {
1411 autoset_dspmbyte(lp);
1412 }
1413#endif
1414
1415 if (islocale_var(vp)) {
1416#ifdef NLS
1417 int k;
1418
1419# ifdef SETLOCALEBUG
1420 dont_free = 1;
1421# endif /* SETLOCALEBUG */
1422 (void) setlocale(LC_ALL, "");
1423# ifdef LC_COLLATE
1424 (void) setlocale(LC_COLLATE, "");
1425# endif
1426# ifdef LC_CTYPE
1427 (void) setlocale(LC_CTYPE, ""); /* for iscntrl */
1428# endif /* LC_CTYPE */
1429# ifdef NLS_CATALOGS
1430# ifdef LC_MESSAGES
1431 (void) setlocale(LC_MESSAGES, "");
1432# endif /* LC_MESSAGES */
1433 nlsclose();
1434 nlsinit();
1435# endif /* NLS_CATALOGS */
1436# ifdef SETLOCALEBUG
1437 dont_free = 0;
1438# endif /* SETLOCALEBUG */
1439# ifdef STRCOLLBUG
1440 fix_strcoll_bug();
1441# endif /* STRCOLLBUG */
1442 tw_cmd_free(); /* since the collation sequence has changed */
1443 for (k = 0200; k <= 0377 && !Isprint(CTL_ESC(k)); k++)
1444 continue;
1445 AsciiOnly = MB_CUR_MAX == 1 && k > 0377;
1446#else /* !NLS */
1447 AsciiOnly = 0;
1448#endif /* NLS */
1449 NLSMapsAreInited = 0;
1450 ed_Init();
1451 if (MapsAreInited && !NLSMapsAreInited)
1452 ed_InitNLSMaps();
1453 cleanup_until(lp);
1454 return;
1455 }
1456
1457#ifdef NLS_CATALOGS
1458 if (eq(vp, STRNLSPATH)) {
1459 nlsclose();
1460 nlsinit();
1461 }
1462#endif
1463
1464 if (eq(vp, STRNOREBIND)) {
1465 NoNLSRebind = 1;
1466 MapsAreInited = 0;
1467 NLSMapsAreInited = 0;
1468 ed_InitMaps();
1469 cleanup_until(lp);
1470 return;
1471 }
1472#ifdef WINNT_NATIVE
1473 if (eq(vp, STRtcshlang)) {
1474 nlsinit();
1475 cleanup_until(lp);
1476 return;
1477 }
1478#endif /* WINNT_NATIVE */
1479 if (eq(vp, STRKTERM)) {
1480 char *t;
1481
1482 setv(STRterm, quote(lp), VAR_READWRITE); /* lp memory used here */
1483 cleanup_ignore(lp);
1484 cleanup_until(lp);
1485 t = short2str(lp);
1486 if (noediting && strcmp(t, "unknown") != 0 && strcmp(t,"dumb") != 0) {
1487 editing = 1;
1488 noediting = 0;
1489 setNS(STRedit);
1490 }
1491 GotTermCaps = 0;
1492 ed_Init();
1493 return;
1494 }
1495
1496 if (eq(vp, STRKHOME)) {
1497 Char *canon;
1498 /*
1499 * convert to canonical pathname (possibly resolving symlinks)
1500 */
1501 canon = dcanon(lp, lp);
1502 cleanup_ignore(lp);
1503 cleanup_until(lp);
1504 cleanup_push(canon, xfree);
1505 setv(STRhome, quote(canon), VAR_READWRITE); /* lp memory used here */
1506 cleanup_ignore(canon);
1507 cleanup_until(canon);
1508
1509 /* fix directory stack for new tilde home */
1510 dtilde();
1511 return;
1512 }
1513
1514 if (eq(vp, STRKSHLVL)) {
1515 setv(STRshlvl, quote(lp), VAR_READWRITE); /* lp memory used here */
1516 cleanup_ignore(lp);
1517 cleanup_until(lp);
1518 return;
1519 }
1520
1521 if (eq(vp, STRKUSER)) {
1522 setv(STRuser, quote(lp), VAR_READWRITE); /* lp memory used here */
1523 cleanup_ignore(lp);
1524 cleanup_until(lp);
1525 return;
1526 }
1527
1528 if (eq(vp, STRKGROUP)) {
1529 setv(STRgroup, quote(lp), VAR_READWRITE); /* lp memory used here */
1530 cleanup_ignore(lp);
1531 cleanup_until(lp);
1532 return;
1533 }
1534
1535#ifdef COLOR_LS_F
1536 if (eq(vp, STRLS_COLORS)) {
1537 parseLS_COLORS(lp);
1538 cleanup_until(lp);
1539 return;
1540 }
1541#endif /* COLOR_LS_F */
1542
1543#ifdef SIG_WINDOW
1544 /*
1545 * Load/Update $LINES $COLUMNS
1546 */
1547 if ((eq(lp, STRNULL) && (eq(vp, STRLINES) || eq(vp, STRCOLUMNS))) ||
1548 eq(vp, STRTERMCAP)) {
1549 cleanup_until(lp);
1550 check_window_size(1);
1551 return;
1552 }
1553
1554 /*
1555 * Change the size to the one directed by $LINES and $COLUMNS
1556 */
1557 if (eq(vp, STRLINES) || eq(vp, STRCOLUMNS)) {
1558#if 0
1559 GotTermCaps = 0;
1560#endif
1561 cleanup_until(lp);
1562 ed_Init();
1563 return;
1564 }
1565#endif /* SIG_WINDOW */
1566 cleanup_until(lp);
1567}
1568
1569/*ARGSUSED*/
1570void
1571dounsetenv(Char **v, struct command *c)
1572{
1573 Char **ep, *p, *n, *name;
1574 int i, maxi;
1575
1576 USE(c);
1577 /*
1578 * Find the longest environment variable
1579 */
1580 for (maxi = 0, ep = STR_environ; *ep; ep++) {
1581 for (i = 0, p = *ep; *p && *p != '='; p++, i++)
1582 continue;
1583 if (i > maxi)
1584 maxi = i;
1585 }
1586
1587 name = xmalloc((maxi + 1) * sizeof(Char));
1588 cleanup_push(name, xfree);
1589
1590 while (++v && *v)
1591 for (maxi = 1; maxi;)
1592 for (maxi = 0, ep = STR_environ; *ep; ep++) {
1593 for (n = name, p = *ep; *p && *p != '='; *n++ = *p++)
1594 continue;
1595 *n = '\0';
1596 if (!Gmatch(name, *v))
1597 continue;
1598 maxi = 1;
1599
1600 /* Unset the name. This wasn't being done until
1601 * later but most of the stuff following won't
1602 * work (particularly the setlocale() and getenv()
1603 * stuff) as intended until the name is actually
1604 * removed. (sg)
1605 */
1606 Unsetenv(name);
1607
1608 if (eq(name, STRNOREBIND)) {
1609 NoNLSRebind = 0;
1610 MapsAreInited = 0;
1611 NLSMapsAreInited = 0;
1612 ed_InitMaps();
1613 }
1614#ifdef apollo
1615 else if (eq(name, STRSYSTYPE))
1616 dohash(NULL, NULL);
1617#endif /* apollo */
1618 else if (islocale_var(name)) {
1619#ifdef NLS
1620 int k;
1621
1622# ifdef SETLOCALEBUG
1623 dont_free = 1;
1624# endif /* SETLOCALEBUG */
1625 (void) setlocale(LC_ALL, "");
1626# ifdef LC_COLLATE
1627 (void) setlocale(LC_COLLATE, "");
1628# endif
1629# ifdef LC_CTYPE
1630 (void) setlocale(LC_CTYPE, ""); /* for iscntrl */
1631# endif /* LC_CTYPE */
1632# ifdef NLS_CATALOGS
1633# ifdef LC_MESSAGES
1634 (void) setlocale(LC_MESSAGES, "");
1635# endif /* LC_MESSAGES */
1636 nlsclose();
1637 nlsinit();
1638# endif /* NLS_CATALOGS */
1639# ifdef SETLOCALEBUG
1640 dont_free = 0;
1641# endif /* SETLOCALEBUG */
1642# ifdef STRCOLLBUG
1643 fix_strcoll_bug();
1644# endif /* STRCOLLBUG */
1645 tw_cmd_free();/* since the collation sequence has changed */
1646 for (k = 0200; k <= 0377 && !Isprint(CTL_ESC(k)); k++)
1647 continue;
1648 AsciiOnly = MB_CUR_MAX == 1 && k > 0377;
1649#else /* !NLS */
1650 AsciiOnly = getenv("LANG") == NULL &&
1651 getenv("LC_CTYPE") == NULL;
1652#endif /* NLS */
1653 NLSMapsAreInited = 0;
1654 ed_Init();
1655 if (MapsAreInited && !NLSMapsAreInited)
1656 ed_InitNLSMaps();
1657
1658 }
1659#ifdef WINNT_NATIVE
1660 else if (eq(name,(STRtcshlang))) {
1661 nls_dll_unload();
1662 nlsinit();
1663 }
1664#endif /* WINNT_NATIVE */
1665#ifdef COLOR_LS_F
1666 else if (eq(name, STRLS_COLORS))
1667 parseLS_COLORS(n);
1668#endif /* COLOR_LS_F */
1669#ifdef NLS_CATALOGS
1670 else if (eq(name, STRNLSPATH)) {
1671 nlsclose();
1672 nlsinit();
1673 }
1674#endif
1675 /*
1676 * start again cause the environment changes
1677 */
1678 break;
1679 }
1680 cleanup_until(name);
1681}
1682
1683void
1684tsetenv(const Char *name, const Char *val)
1685{
1686#ifdef SETENV_IN_LIB
1687/*
1688 * XXX: This does not work right, since tcsh cannot track changes to
1689 * the environment this way. (the builtin setenv without arguments does
1690 * not print the right stuff neither does unsetenv). This was for Mach,
1691 * it is not needed anymore.
1692 */
1693#undef setenv
1694 char *cname;
1695
1696 if (name == NULL)
1697 return;
1698 cname = strsave(short2str(name));
1699 setenv(cname, short2str(val), 1);
1700 xfree(cname);
1701#else /* !SETENV_IN_LIB */
1702 Char **ep = STR_environ;
1703 const Char *ccp;
1704 Char *cp, *dp;
1705 Char *blk[2];
1706 Char **oep = ep;
1707
1708#ifdef WINNT_NATIVE
1709 nt_set_env(name,val);
1710#endif /* WINNT_NATIVE */
1711 for (; *ep; ep++) {
1712#ifdef WINNT_NATIVE
1713 for (ccp = name, dp = *ep; *ccp && Tolower(*ccp & TRIM) == Tolower(*dp);
1714 ccp++, dp++)
1715#else
1716 for (ccp = name, dp = *ep; *ccp && (*ccp & TRIM) == *dp; ccp++, dp++)
1717#endif /* WINNT_NATIVE */
1718 continue;
1719 if (*ccp != 0 || *dp != '=')
1720 continue;
1721 cp = Strspl(STRequal, val);
1722 xfree(*ep);
1723 *ep = strip(Strspl(name, cp));
1724 xfree(cp);
1725 blkfree((Char **) environ);
1726 environ = short2blk(STR_environ);
1727 return;
1728 }
1729 cp = Strspl(name, STRequal);
1730 blk[0] = strip(Strspl(cp, val));
1731 xfree(cp);
1732 blk[1] = 0;
1733 STR_environ = blkspl(STR_environ, blk);
1734 blkfree((Char **) environ);
1735 environ = short2blk(STR_environ);
1736 xfree(oep);
1737#endif /* SETENV_IN_LIB */
1738}
1739
1740void
1741Unsetenv(Char *name)
1742{
1743 Char **ep = STR_environ;
1744 Char *cp, *dp;
1745 Char **oep = ep;
1746
1747#ifdef WINNT_NATIVE
1748 nt_set_env(name,NULL);
1749#endif /*WINNT_NATIVE */
1750 for (; *ep; ep++) {
1751 for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
1752 continue;
1753 if (*cp != 0 || *dp != '=')
1754 continue;
1755 cp = *ep;
1756 *ep = 0;
1757 STR_environ = blkspl(STR_environ, ep + 1);
1758 blkfree((Char **) environ);
1759 environ = short2blk(STR_environ);
1760 *ep = cp;
1761 xfree(cp);
1762 xfree(oep);
1763 return;
1764 }
1765}
1766
1767/*ARGSUSED*/
1768void
1769doumask(Char **v, struct command *c)
1770{
1771 Char *cp = v[1];
1772 int i;
1773
1774 USE(c);
1775 if (cp == 0) {
1776 i = (int)umask(0);
1777 (void) umask(i);
1778 xprintf("%o\n", i);
1779 return;
1780 }
1781 i = 0;
1782 while (Isdigit(*cp) && *cp != '8' && *cp != '9')
1783 i = i * 8 + *cp++ - '0';
1784 if (*cp || i < 0 || i > 0777)
1785 stderror(ERR_NAME | ERR_MASK);
1786 (void) umask(i);
1787}
1788
1789#ifndef HAVENOLIMIT
1790# ifndef BSDLIMIT
1791 typedef long RLIM_TYPE;
1792# ifdef _OSD_POSIX /* BS2000 */
1793# include <ulimit.h>
1794# endif
1795# ifndef RLIM_INFINITY
1796# if !defined(_MINIX) && !defined(__clipper__) && !defined(_CRAY)
1797 extern RLIM_TYPE ulimit();
1798# endif /* ! _MINIX && !__clipper__ */
1799# define RLIM_INFINITY 0x003fffff
1800# define RLIMIT_FSIZE 1
1801# endif /* RLIM_INFINITY */
1802# ifdef aiws
1803# define toset(a) (((a) == 3) ? 1004 : (a) + 1)
1804# define RLIMIT_DATA 3
1805# define RLIMIT_STACK 1005
1806# else /* aiws */
1807# define toset(a) ((a) + 1)
1808# endif /* aiws */
1809# else /* BSDLIMIT */
1810# if (defined(BSD4_4) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || (HPUXVERSION >= 1100)) && !defined(__386BSD__)
1811 typedef rlim_t RLIM_TYPE;
1812# else
1813# if defined(SOLARIS2) || (defined(sgi) && SYSVREL > 3)
1814 typedef rlim_t RLIM_TYPE;
1815# else
1816# if defined(_SX)
1817 typedef long long RLIM_TYPE;
1818# else /* !_SX */
1819 typedef unsigned long RLIM_TYPE;
1820# endif /* _SX */
1821# endif /* SOLARIS2 || (sgi && SYSVREL > 3) */
1822# endif /* BSD4_4 && !__386BSD__ */
1823# endif /* BSDLIMIT */
1824
1825# if (HPUXVERSION > 700) && (HPUXVERSION < 1100) && defined(BSDLIMIT)
1826/* Yes hpux8.0 has limits but <sys/resource.h> does not make them public */
1827/* Yes, we could have defined _KERNEL, and -I/etc/conf/h, but is that better? */
1828# ifndef RLIMIT_CPU
1829# define RLIMIT_CPU 0
1830# define RLIMIT_FSIZE 1
1831# define RLIMIT_DATA 2
1832# define RLIMIT_STACK 3
1833# define RLIMIT_CORE 4
1834# define RLIMIT_RSS 5
1835# define RLIMIT_NOFILE 6
1836# endif /* RLIMIT_CPU */
1837# ifndef RLIM_INFINITY
1838# define RLIM_INFINITY 0x7fffffff
1839# endif /* RLIM_INFINITY */
1840 /*
1841 * old versions of HP/UX counted limits in 512 bytes
1842 */
1843# ifndef SIGRTMIN
1844# define FILESIZE512
1845# endif /* SIGRTMIN */
1846# endif /* (HPUXVERSION > 700) && (HPUXVERSION < 1100) && BSDLIMIT */
1847
1848# if SYSVREL > 3 && defined(BSDLIMIT) && !defined(_SX)
1849/* In order to use rusage, we included "/usr/ucbinclude/sys/resource.h" in */
1850/* sh.h. However, some SVR4 limits are defined in <sys/resource.h>. Rather */
1851/* than include both and get warnings, we define the extra SVR4 limits here. */
1852/* XXX: I don't understand if RLIMIT_AS is defined, why don't we define */
1853/* RLIMIT_VMEM based on it? */
1854# ifndef RLIMIT_VMEM
1855# define RLIMIT_VMEM 6
1856# endif
1857# ifndef RLIMIT_AS
1858# define RLIMIT_AS RLIMIT_VMEM
1859# endif
1860# endif /* SYSVREL > 3 && BSDLIMIT */
1861
1862# if (defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)) && defined(RLIMIT_AS) && !defined(RLIMIT_VMEM)
1863# define RLIMIT_VMEM RLIMIT_AS
1864# endif
1865
1866struct limits limits[] =
1867{
1868# ifdef RLIMIT_CPU
1869 { RLIMIT_CPU, "cputime", 1, "seconds" },
1870# endif /* RLIMIT_CPU */
1871
1872# ifdef RLIMIT_FSIZE
1873# ifndef aiws
1874 { RLIMIT_FSIZE, "filesize", 1024, "kbytes" },
1875# else
1876 { RLIMIT_FSIZE, "filesize", 512, "blocks" },
1877# endif /* aiws */
1878# endif /* RLIMIT_FSIZE */
1879
1880# ifdef RLIMIT_DATA
1881 { RLIMIT_DATA, "datasize", 1024, "kbytes" },
1882# endif /* RLIMIT_DATA */
1883
1884# ifdef RLIMIT_STACK
1885# ifndef aiws
1886 { RLIMIT_STACK, "stacksize", 1024, "kbytes" },
1887# else
1888 { RLIMIT_STACK, "stacksize", 1024 * 1024, "kbytes"},
1889# endif /* aiws */
1890# endif /* RLIMIT_STACK */
1891
1892# ifdef RLIMIT_CORE
1893 { RLIMIT_CORE, "coredumpsize", 1024, "kbytes" },
1894# endif /* RLIMIT_CORE */
1895
1896# ifdef RLIMIT_RSS
1897 { RLIMIT_RSS, "memoryuse", 1024, "kbytes" },
1898# endif /* RLIMIT_RSS */
1899
1900# ifdef RLIMIT_UMEM
1901 { RLIMIT_UMEM, "memoryuse", 1024, "kbytes" },
1902# endif /* RLIMIT_UMEM */
1903
1904# ifdef RLIMIT_VMEM
1905 { RLIMIT_VMEM, "vmemoryuse", 1024, "kbytes" },
1906# endif /* RLIMIT_VMEM */
1907
1908# if defined(RLIMIT_HEAP) /* found on BS2000/OSD systems */
1909 { RLIMIT_HEAP, "heapsize", 1024, "kbytes" },
1910# endif /* RLIMIT_HEAP */
1911
1912# ifdef RLIMIT_NOFILE
1913 { RLIMIT_NOFILE, "descriptors", 1, "" },
1914# endif /* RLIMIT_NOFILE */
1915
1916# ifdef RLIMIT_CONCUR
1917 { RLIMIT_CONCUR, "concurrency", 1, "thread(s)" },
1918# endif /* RLIMIT_CONCUR */
1919
1920# ifdef RLIMIT_MEMLOCK
1921 { RLIMIT_MEMLOCK, "memorylocked", 1024, "kbytes" },
1922# endif /* RLIMIT_MEMLOCK */
1923
1924# ifdef RLIMIT_NPROC
1925 { RLIMIT_NPROC, "maxproc", 1, "" },
1926# endif /* RLIMIT_NPROC */
1927
1928# if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE)
1929 { RLIMIT_OFILE, "openfiles", 1, "" },
1930# endif /* RLIMIT_OFILE && !defined(RLIMIT_NOFILE) */
1931
1932# ifdef RLIMIT_SBSIZE
1933 { RLIMIT_SBSIZE, "sbsize", 1, "" },
1934# endif /* RLIMIT_SBSIZE */
1935
57e3f2b5
SS
1936# ifdef RLIMIT_SWAP
1937 { RLIMIT_SWAP, "swapsize", 1024, "kbytes" },
1938# endif /* RLIMIT_SWAP */
1939
7d8fb588
MS
1940 { -1, NULL, 0, NULL }
1941};
1942
1943static struct limits *findlim (Char *);
1944static RLIM_TYPE getval (struct limits *, Char **);
1945static void limtail (Char *, const char *);
1946static void plim (struct limits *, int);
1947static int setlim (struct limits *, int, RLIM_TYPE);
1948
1949#ifdef convex
1950static RLIM_TYPE
1951restrict_limit(double value)
1952{
1953 /*
1954 * is f too large to cope with? return the maximum or minimum int
1955 */
1956 if (value > (double) INT_MAX)
1957 return (RLIM_TYPE) INT_MAX;
1958 else if (value < (double) INT_MIN)
1959 return (RLIM_TYPE) INT_MIN;
1960 else
1961 return (RLIM_TYPE) value;
1962}
1963#else /* !convex */
1964# define restrict_limit(x) ((RLIM_TYPE) (x))
1965#endif /* convex */
1966
1967
1968static struct limits *
1969findlim(Char *cp)
1970{
1971 struct limits *lp, *res;
1972
1973 res = NULL;
1974 for (lp = limits; lp->limconst >= 0; lp++)
1975 if (prefix(cp, str2short(lp->limname))) {
1976 if (res)
1977 stderror(ERR_NAME | ERR_AMBIG);
1978 res = lp;
1979 }
1980 if (res)
1981 return (res);
1982 stderror(ERR_NAME | ERR_LIMIT);
1983 /* NOTREACHED */
1984 return (0);
1985}
1986
1987/*ARGSUSED*/
1988void
1989dolimit(Char **v, struct command *c)
1990{
1991 struct limits *lp;
1992 RLIM_TYPE limit;
1993 int hard = 0;
1994
1995 USE(c);
1996 v++;
1997 if (*v && eq(*v, STRmh)) {
1998 hard = 1;
1999 v++;
2000 }
2001 if (*v == 0) {
2002 for (lp = limits; lp->limconst >= 0; lp++)
2003 plim(lp, hard);
2004 return;
2005 }
2006 lp = findlim(v[0]);
2007 if (v[1] == 0) {
2008 plim(lp, hard);
2009 return;
2010 }
2011 limit = getval(lp, v + 1);
2012 if (setlim(lp, hard, limit) < 0)
2013 stderror(ERR_SILENT);
2014}
2015
2016static RLIM_TYPE
2017getval(struct limits *lp, Char **v)
2018{
2019 float f;
2020 Char *cp = *v++;
2021
2022 f = atof(short2str(cp));
2023
2024# ifdef convex
2025 /*
2026 * is f too large to cope with. limit f to minint, maxint - X-6768 by
2027 * strike
2028 */
2029 if ((f < (double) INT_MIN) || (f > (double) INT_MAX)) {
2030 stderror(ERR_NAME | ERR_TOOLARGE);
2031 }
2032# endif /* convex */
2033
2034 while (Isdigit(*cp) || *cp == '.' || *cp == 'e' || *cp == 'E')
2035 cp++;
2036 if (*cp == 0) {
2037 if (*v == 0)
2038 return restrict_limit((f * lp->limdiv) + 0.5);
2039 cp = *v;
2040 }
2041 switch (*cp) {
2042# ifdef RLIMIT_CPU
2043 case ':':
2044 if (lp->limconst != RLIMIT_CPU)
2045 goto badscal;
2046 return f == 0.0 ? (RLIM_TYPE) 0 : restrict_limit((f * 60.0 + atof(short2str(cp + 1))));
2047 case 'h':
2048 if (lp->limconst != RLIMIT_CPU)
2049 goto badscal;
2050 limtail(cp, "hours");
2051 f *= 3600.0;
2052 break;
2053 case 'm':
2054 if (lp->limconst == RLIMIT_CPU) {
2055 limtail(cp, "minutes");
2056 f *= 60.0;
2057 break;
2058 }
2059 *cp = 'm';
2060 limtail(cp, "megabytes");
2061 f *= 1024.0 * 1024.0;
2062 break;
2063 case 's':
2064 if (lp->limconst != RLIMIT_CPU)
2065 goto badscal;
2066 limtail(cp, "seconds");
2067 break;
2068# endif /* RLIMIT_CPU */
2069 case 'M':
2070# ifdef RLIMIT_CPU
2071 if (lp->limconst == RLIMIT_CPU)
2072 goto badscal;
2073# endif /* RLIMIT_CPU */
2074 *cp = 'm';
2075 limtail(cp, "megabytes");
2076 f *= 1024.0 * 1024.0;
2077 break;
2078 case 'k':
2079# ifdef RLIMIT_CPU
2080 if (lp->limconst == RLIMIT_CPU)
2081 goto badscal;
2082# endif /* RLIMIT_CPU */
2083 limtail(cp, "kbytes");
2084 f *= 1024.0;
2085 break;
2086 case 'b':
2087# ifdef RLIMIT_CPU
2088 if (lp->limconst == RLIMIT_CPU)
2089 goto badscal;
2090# endif /* RLIMIT_CPU */
2091 limtail(cp, "blocks");
2092 f *= 512.0;
2093 break;
2094 case 'u':
2095 limtail(cp, "unlimited");
2096 return ((RLIM_TYPE) RLIM_INFINITY);
2097 default:
2098# ifdef RLIMIT_CPU
2099badscal:
2100# endif /* RLIMIT_CPU */
2101 stderror(ERR_NAME | ERR_SCALEF);
2102 }
2103# ifdef convex
2104 return f == 0.0 ? (RLIM_TYPE) 0 : restrict_limit((f + 0.5));
2105# else
2106 f += 0.5;
2107 if (f > (float) RLIM_INFINITY)
2108 return ((RLIM_TYPE) RLIM_INFINITY);
2109 else
2110 return ((RLIM_TYPE) f);
2111# endif /* convex */
2112}
2113
2114static void
2115limtail(Char *cp, const char *str)
2116{
2117 const char *sp;
2118
2119 sp = str;
2120 while (*cp && *cp == (Char)*str)
2121 cp++, str++;
2122 if (*cp)
2123 stderror(ERR_BADSCALE, sp);
2124}
2125
2126
2127/*ARGSUSED*/
2128static void
2129plim(struct limits *lp, int hard)
2130{
2131# ifdef BSDLIMIT
2132 struct rlimit rlim;
2133# endif /* BSDLIMIT */
2134 RLIM_TYPE limit;
2135 int xdiv = lp->limdiv;
2136
2137 xprintf("%-13.13s", lp->limname);
2138
2139# ifndef BSDLIMIT
2140 limit = ulimit(lp->limconst, 0);
2141# ifdef aiws
2142 if (lp->limconst == RLIMIT_DATA)
2143 limit -= 0x20000000;
2144# endif /* aiws */
2145# else /* BSDLIMIT */
2146 (void) getrlimit(lp->limconst, &rlim);
2147 limit = hard ? rlim.rlim_max : rlim.rlim_cur;
2148# endif /* BSDLIMIT */
2149
2150# if !defined(BSDLIMIT) || defined(FILESIZE512)
2151 /*
2152 * Christos: filesize comes in 512 blocks. we divide by 2 to get 1024
2153 * blocks. Note we cannot pre-multiply cause we might overflow (A/UX)
2154 */
2155 if (lp->limconst == RLIMIT_FSIZE) {
2156 if (limit >= (RLIM_INFINITY / 512))
2157 limit = RLIM_INFINITY;
2158 else
2159 xdiv = (xdiv == 1024 ? 2 : 1);
2160 }
2161# endif /* !BSDLIMIT || FILESIZE512 */
2162
2163 if (limit == RLIM_INFINITY)
2164 xprintf("unlimited");
2165 else
2166# if defined(RLIMIT_CPU) && defined(_OSD_POSIX)
2167 if (lp->limconst == RLIMIT_CPU &&
2168 (unsigned long)limit >= 0x7ffffffdUL)
2169 xprintf("unlimited");
2170 else
2171# endif
2172# ifdef RLIMIT_CPU
2173 if (lp->limconst == RLIMIT_CPU)
2174 psecs(limit);
2175 else
2176# endif /* RLIMIT_CPU */
2177 xprintf("%ld %s", (long) (limit / xdiv), lp->limscale);
2178 xputchar('\n');
2179}
2180
2181/*ARGSUSED*/
2182void
2183dounlimit(Char **v, struct command *c)
2184{
2185 struct limits *lp;
2186 int lerr = 0;
2187 int hard = 0;
2188 int force = 0;
2189
2190 USE(c);
2191 while (*++v && **v == '-') {
2192 Char *vp = *v;
2193 while (*++vp)
2194 switch (*vp) {
2195 case 'f':
2196 force = 1;
2197 break;
2198 case 'h':
2199 hard = 1;
2200 break;
2201 default:
2202 stderror(ERR_ULIMUS);
2203 break;
2204 }
2205 }
2206
2207 if (*v == 0) {
2208 for (lp = limits; lp->limconst >= 0; lp++)
2209 if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0)
2210 lerr++;
2211 if (!force && lerr)
2212 stderror(ERR_SILENT);
2213 return;
2214 }
2215 while (*v) {
2216 lp = findlim(*v++);
2217 if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0 && !force)
2218 stderror(ERR_SILENT);
2219 }
2220}
2221
2222static int
2223setlim(struct limits *lp, int hard, RLIM_TYPE limit)
2224{
2225# ifdef BSDLIMIT
2226 struct rlimit rlim;
2227
2228 (void) getrlimit(lp->limconst, &rlim);
2229
2230# ifdef FILESIZE512
2231 /* Even though hpux has setrlimit(), it expects fsize in 512 byte blocks */
2232 if (limit != RLIM_INFINITY && lp->limconst == RLIMIT_FSIZE)
2233 limit /= 512;
2234# endif /* FILESIZE512 */
2235 if (hard)
2236 rlim.rlim_max = limit;
2237 else if (limit == RLIM_INFINITY && euid != 0)
2238 rlim.rlim_cur = rlim.rlim_max;
2239 else
2240 rlim.rlim_cur = limit;
2241
2242 if (rlim.rlim_cur > rlim.rlim_max)
2243 rlim.rlim_max = rlim.rlim_cur;
2244
2245 if (setrlimit(lp->limconst, &rlim) < 0) {
2246# else /* BSDLIMIT */
2247 if (limit != RLIM_INFINITY && lp->limconst == RLIMIT_FSIZE)
2248 limit /= 512;
2249# ifdef aiws
2250 if (lp->limconst == RLIMIT_DATA)
2251 limit += 0x20000000;
2252# endif /* aiws */
2253 if (ulimit(toset(lp->limconst), limit) < 0) {
2254# endif /* BSDLIMIT */
2255 int err;
2256 char *op, *type;
2257
2258 err = errno;
2259 op = strsave(limit == RLIM_INFINITY ? CGETS(15, 2, "remove") :
2260 CGETS(15, 3, "set"));
2261 cleanup_push(op, xfree);
2262 type = strsave(hard ? CGETS(15, 4, " hard") : "");
2263 cleanup_push(type, xfree);
2264 xprintf(CGETS(15, 1, "%s: %s: Can't %s%s limit (%s)\n"), bname,
2265 lp->limname, op, type, strerror(err));
2266 cleanup_until(op);
2267 return (-1);
2268 }
2269 return (0);
2270}
2271
2272#endif /* !HAVENOLIMIT */
2273
2274/*ARGSUSED*/
2275void
2276dosuspend(Char **v, struct command *c)
2277{
2278#ifdef BSDJOBS
7d8fb588
MS
2279 struct sigaction old;
2280#endif /* BSDJOBS */
57e3f2b5 2281
7d8fb588
MS
2282 USE(c);
2283 USE(v);
2284
2285 if (loginsh)
2286 stderror(ERR_SUSPLOG);
2287 untty();
2288
2289#ifdef BSDJOBS
2290 sigaction(SIGTSTP, NULL, &old);
2291 signal(SIGTSTP, SIG_DFL);
2292 (void) kill(0, SIGTSTP);
2293 /* the shell stops here */
2294 sigaction(SIGTSTP, &old, NULL);
2295#else /* !BSDJOBS */
2296 stderror(ERR_JOBCONTROL);
2297#endif /* BSDJOBS */
2298
2299#ifdef BSDJOBS
2300 if (tpgrp != -1) {
57e3f2b5 2301 if (grabpgrp(FSHTTY, opgrp) == -1)
7d8fb588 2302 stderror(ERR_SYSTEM, "tcgetpgrp", strerror(errno));
7d8fb588
MS
2303 (void) setpgid(0, shpgrp);
2304 (void) tcsetpgrp(FSHTTY, shpgrp);
2305 }
2306#endif /* BSDJOBS */
2307 (void) setdisc(FSHTTY);
2308}
2309
2310/* This is the dreaded EVAL built-in.
2311 * If you don't fiddle with file descriptors, and reset didfds,
2312 * this command will either ignore redirection inside or outside
2313 * its arguments, e.g. eval "date >x" vs. eval "date" >x
2314 * The stuff here seems to work, but I did it by trial and error rather
2315 * than really knowing what was going on. If tpgrp is zero, we are
2316 * probably a background eval, e.g. "eval date &", and we want to
2317 * make sure that any processes we start stay in our pgrp.
2318 * This is also the case for "time eval date" -- stay in same pgrp.
2319 * Otherwise, under stty tostop, processes will stop in the wrong
2320 * pgrp, with no way for the shell to get them going again. -IAN!
2321 */
2322
2323struct doeval_state
2324{
2325 Char **evalvec, *evalp;
2326 int didfds;
2327#ifndef CLOSE_ON_EXEC
2328 int didcch;
2329#endif
2330 int saveIN, saveOUT, saveDIAG;
2331 int SHIN, SHOUT, SHDIAG;
2332};
2333
2334static void
2335doeval_cleanup(void *xstate)
2336{
2337 struct doeval_state *state;
2338
2339 state = xstate;
2340 evalvec = state->evalvec;
2341 evalp = state->evalp;
2342 doneinp = 0;
2343#ifndef CLOSE_ON_EXEC
2344 didcch = state->didcch;
2345#endif /* CLOSE_ON_EXEC */
2346 didfds = state->didfds;
2347 xclose(SHIN);
2348 xclose(SHOUT);
2349 xclose(SHDIAG);
2350 close_on_exec(SHIN = dmove(state->saveIN, state->SHIN), 1);
2351 close_on_exec(SHOUT = dmove(state->saveOUT, state->SHOUT), 1);
2352 close_on_exec(SHDIAG = dmove(state->saveDIAG, state->SHDIAG), 1);
2353}
2354
57e3f2b5 2355static Char **Ggv;
7d8fb588
MS
2356/*ARGSUSED*/
2357void
2358doeval(Char **v, struct command *c)
2359{
2360 struct doeval_state state;
57e3f2b5 2361 int gflag, my_reenter;
7d8fb588 2362 Char **gv;
57e3f2b5 2363 jmp_buf_t osetexit;
7d8fb588
MS
2364
2365 USE(c);
2366 v++;
2367 if (*v == 0)
2368 return;
2369 gflag = tglob(v);
2370 if (gflag) {
2371 gv = v = globall(v, gflag);
2372 if (v == 0)
2373 stderror(ERR_NOMATCH);
2374 cleanup_push(gv, blk_cleanup);
2375 v = copyblk(v);
2376 }
2377 else {
2378 gv = NULL;
2379 v = copyblk(v);
2380 trim(v);
2381 }
2382
57e3f2b5 2383 Ggv = gv;
7d8fb588
MS
2384 state.evalvec = evalvec;
2385 state.evalp = evalp;
2386 state.didfds = didfds;
2387#ifndef CLOSE_ON_EXEC
2388 state.didcch = didcch;
2389#endif /* CLOSE_ON_EXEC */
2390 state.SHIN = SHIN;
2391 state.SHOUT = SHOUT;
2392 state.SHDIAG = SHDIAG;
2393
2394 (void)close_on_exec(state.saveIN = dcopy(SHIN, -1), 1);
2395 (void)close_on_exec(state.saveOUT = dcopy(SHOUT, -1), 1);
2396 (void)close_on_exec(state.saveDIAG = dcopy(SHDIAG, -1), 1);
2397
2398 cleanup_push(&state, doeval_cleanup);
2399
57e3f2b5
SS
2400 getexit(osetexit);
2401
2402 /* PWP: setjmp/longjmp bugfix for optimizing compilers */
2403#ifdef cray
2404 my_reenter = 1; /* assume non-zero return val */
2405 if (setexit() == 0) {
2406 my_reenter = 0; /* Oh well, we were wrong */
2407#else /* !cray */
2408 if ((my_reenter = setexit()) == 0) {
2409#endif /* cray */
2410 evalvec = v;
2411 evalp = 0;
2412 (void)close_on_exec(SHIN = dcopy(0, -1), 1);
2413 (void)close_on_exec(SHOUT = dcopy(1, -1), 1);
2414 (void)close_on_exec(SHDIAG = dcopy(2, -1), 1);
7d8fb588 2415#ifndef CLOSE_ON_EXEC
57e3f2b5 2416 didcch = 0;
7d8fb588 2417#endif /* CLOSE_ON_EXEC */
57e3f2b5
SS
2418 didfds = 0;
2419 gv = Ggv;
2420 process(0);
2421 Ggv = gv;
2422 }
7d8fb588 2423
57e3f2b5
SS
2424 if (my_reenter == 0) {
2425 cleanup_until(&state);
2426 if (Ggv)
2427 cleanup_until(Ggv);
2428 }
7d8fb588 2429
57e3f2b5
SS
2430 resexit(osetexit);
2431 if (my_reenter)
2432 stderror(ERR_SILENT);
7d8fb588
MS
2433}
2434
2435/*************************************************************************/
2436/* print list of builtin commands */
2437
2438static void
2439lbuffed_cleanup (void *dummy)
2440{
2441 USE(dummy);
2442 lbuffed = 1;
2443}
2444
2445/*ARGSUSED*/
2446void
2447dobuiltins(Char **v, struct command *c)
2448{
2449 /* would use print_by_column() in tw.parse.c but that assumes
2450 * we have an array of Char * to pass.. (sg)
2451 */
2452 const struct biltins *b;
2453 int row, col, columns, rows;
2454 unsigned int w, maxwidth;
2455
2456 USE(c);
2457 USE(v);
2458 lbuffed = 0; /* turn off line buffering */
2459 cleanup_push(&lbuffed, lbuffed_cleanup);
2460
2461 /* find widest string */
2462 for (maxwidth = 0, b = bfunc; b < &bfunc[nbfunc]; ++b)
2463 maxwidth = max(maxwidth, strlen(b->bname));
2464 ++maxwidth; /* for space */
2465
2466 columns = (TermH + 1) / maxwidth; /* PWP: terminal size change */
2467 if (!columns)
2468 columns = 1;
2469 rows = (nbfunc + (columns - 1)) / columns;
2470
2471 for (b = bfunc, row = 0; row < rows; row++) {
2472 for (col = 0; col < columns; col++) {
2473 if (b < &bfunc[nbfunc]) {
2474 w = strlen(b->bname);
2475 xprintf("%s", b->bname);
2476 if (col < (columns - 1)) /* Not last column? */
2477 for (; w < maxwidth; w++)
2478 xputchar(' ');
2479 ++b;
2480 }
2481 }
2482 if (row < (rows - 1)) {
2483 if (Tty_raw_mode)
2484 xputchar('\r');
2485 xputchar('\n');
2486 }
2487 }
2488#ifdef WINNT_NATIVE
2489 nt_print_builtins(maxwidth);
2490#else
2491 if (Tty_raw_mode)
2492 xputchar('\r');
2493 xputchar('\n');
2494#endif /* WINNT_NATIVE */
2495
2496 cleanup_until(&lbuffed); /* turn back on line buffering */
2497 flush();
2498}
2499
2500#ifdef NLS_CATALOGS
2501char *
2502xcatgets(nl_catd ctd, int set_id, int msg_id, const char *s)
2503{
2504 char *res;
2505
2506 errno = 0;
2507 while ((res = catgets(ctd, set_id, msg_id, s)) == s && errno == EINTR) {
2508 handle_pending_signals();
2509 errno = 0;
2510 }
2511 return res;
2512}
2513
2514
2515# if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
2516char *
2517iconv_catgets(nl_catd ctd, int set_id, int msg_id, const char *s)
2518{
2519 static char *buf = NULL;
2520 static size_t buf_size = 0;
2521
2522 char *orig, *dest, *p;
2523 ICONV_CONST char *src;
2524 size_t src_size, dest_size;
2525
2526 orig = xcatgets(ctd, set_id, msg_id, s);
2527 if (catgets_iconv == (iconv_t)-1 || orig == s)
2528 return orig;
2529 src = orig;
2530 src_size = strlen(src) + 1;
2531 if (buf == NULL && (buf = xmalloc(buf_size = src_size + 32)) == NULL)
2532 return orig;
2533 dest = buf;
2534 while (src_size != 0) {
2535 dest_size = buf + buf_size - dest;
2536 if (iconv(catgets_iconv, &src, &src_size, &dest, &dest_size)
2537 == (size_t)-1) {
2538 switch (errno) {
2539 case E2BIG:
2540 if ((p = xrealloc(buf, buf_size * 2)) == NULL)
2541 return orig;
2542 buf_size *= 2;
2543 dest = p + (dest - buf);
2544 buf = p;
2545 break;
2546
2547 case EILSEQ: case EINVAL: default:
2548 return orig;
2549 }
2550 }
2551 }
2552 return buf;
2553}
2554# endif /* HAVE_ICONV && HAVE_NL_LANGINFO */
2555#endif /* NLS_CATALOGS */
2556
2557void
2558nlsinit(void)
2559{
2560#ifdef NLS_CATALOGS
2561 static const char default_catalog[] = "tcsh";
2562
2563 char *catalog = (char *)(intptr_t)default_catalog;
2564
2565 if (adrof(STRcatalog) != NULL)
2566 catalog = xasprintf("tcsh.%s", short2str(varval(STRcatalog)));
2567 catd = catopen(catalog, MCLoadBySet);
2568 if (catalog != default_catalog)
2569 xfree(catalog);
2570#if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
2571 /* xcatgets (), not CGETS, the charset name should be in ASCII anyway. */
2572 catgets_iconv = iconv_open (nl_langinfo (CODESET),
2573 xcatgets(catd, 255, 1, "ASCII"));
2574#endif /* HAVE_ICONV && HAVE_NL_LANGINFO */
2575#endif /* NLS_CATALOGS */
2576#ifdef WINNT_NATIVE
2577 nls_dll_init();
2578#endif /* WINNT_NATIVE */
2579 errinit(); /* init the errorlist in correct locale */
2580 mesginit(); /* init the messages for signals */
2581 dateinit(); /* init the messages for dates */
2582 editinit(); /* init the editor messages */
2583 terminit(); /* init the termcap messages */
2584}
2585
2586void
2587nlsclose(void)
2588{
2589#ifdef NLS_CATALOGS
2590#if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
2591 if (catgets_iconv != (iconv_t)-1) {
2592 iconv_close(catgets_iconv);
2593 catgets_iconv = (iconv_t)-1;
2594 }
2595#endif /* HAVE_ICONV && HAVE_NL_LANGINFO */
2596 if (catd != (nl_catd)-1) {
2597 /*
2598 * catclose can call other functions which can call longjmp
2599 * making us re-enter this code. Prevent infinite recursion
2600 * by resetting catd. Problem reported and solved by:
2601 * Gerhard Niklasch
2602 */
2603 nl_catd oldcatd = catd;
2604 catd = (nl_catd)-1;
2605 while (catclose(oldcatd) == -1 && errno == EINTR)
2606 handle_pending_signals();
2607 }
2608#endif /* NLS_CATALOGS */
2609}