1 /* $Header: /src/pub/tcsh/sh.func.c,v 3.111 2004/05/13 15:23:39 christos Exp $ */
3 * sh.func.c: csh builtin functions
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
35 RCSID("$Id: sh.func.c,v 3.111 2004/05/13 15:23:39 christos Exp $")
42 #endif /* WINNT_NATIVE */
47 extern int just_signaled;
48 extern char **environ;
50 extern bool MapsAreInited;
51 extern bool NLSMapsAreInited;
52 extern bool NoNLSRebind;
53 extern bool GotTermCaps;
55 static int zlast = -1;
57 static void islogin __P((void));
58 static void preread __P((void));
59 static void doagain __P((void));
60 static char *isrchx __P((int));
61 static void search __P((int, int, Char *));
62 static int getword __P((Char *));
63 static void toend __P((void));
64 static void xecho __P((int, Char **));
65 static bool islocale_var __P((Char *));
66 static void wpfree __P((struct whyle *));
72 register Char *cp = t->t_dcom[0];
73 register struct biltins *bp, *bp1, *bp2;
74 static struct biltins label = {"", dozip, 0, 0};
75 static struct biltins foregnd = {"%job", dofg1, 0, 0};
76 static struct biltins backgnd = {"%job &", dobg1, 0, 0};
79 * We never match a builtin that has quoted the first
80 * character; this has been the traditional way to escape
86 if (*cp != ':' && lastchr(cp) == ':') {
87 label.bname = short2str(cp);
91 if (t->t_dflg & F_AMPERSAND) {
92 t->t_dflg &= ~F_AMPERSAND;
93 backgnd.bname = short2str(cp);
96 foregnd.bname = short2str(cp);
101 * This is a perhaps kludgy way to determine if the warp builtin is to be
102 * acknowledged or not. If checkwarp() fails, then we are to assume that
103 * the warp command is invalid, and carry on as we would handle any other
104 * non-builtin command. -- JDK 2/4/88
106 if (eq(STRwarp, cp) && !checkwarp()) {
107 return (0); /* this builtin disabled */
111 * Binary search Bp1 is the beginning of the current search range. Bp2 is
114 for (bp1 = bfunc, bp2 = bfunc + nbfunc; bp1 < bp2;) {
117 bp = bp1 + ((bp2 - bp1) >> 1);
118 if ((i = ((char) *cp) - *bp->bname) == 0 &&
119 (i = StrQcmp(cp, str2short(bp->bname))) == 0)
127 return nt_check_additional_builtins(cp);
128 #endif /*WINNT_NATIVE*/
134 register struct command *t;
135 register struct biltins *bp;
141 i = blklen(t->t_dcom) - 1;
143 stderror(ERR_NAME | ERR_TOOFEW);
145 stderror(ERR_NAME | ERR_TOOMANY);
146 (*bp->bfunct) (t->t_dcom, t);
156 register Char *vv = v[1];
159 if (parintr == SIG_IGN)
161 if (setintr && intty)
162 stderror(ERR_NAME | ERR_TERMINAL);
169 (void) sigblock(sigmask(SIGINT));
170 (void) signal(SIGINT, pintr);
173 (void) signal(SIGINT, SIG_DFL);
176 (void) sighold(SIGINT);
177 (void) sigset(SIGINT, pintr);
180 (void) sigset(SIGINT, SIG_DFL);
184 else if (eq((vv = strip(vv)), STRminus)) {
186 (void) signal(SIGINT, SIG_IGN);
188 (void) sigset(SIGINT, SIG_IGN);
190 gointr = Strsave(STRminus);
193 gointr = Strsave(vv);
195 (void) signal(SIGINT, pintr);
197 (void) sigset(SIGINT, pintr);
211 stderror(ERR_NAME | ERR_TERMINAL);
213 (void) signal(SIGHUP, SIG_IGN);
229 stderror(ERR_NAME | ERR_TERMINAL);
231 (void) signal(SIGHUP, SIG_DFL);
251 Char **fileptr, *ftest, *res;
253 if (*(ftest = *++v) != '-')
254 stderror(ERR_NAME | ERR_FILEINQ);
262 stderror(ERR_NAME | ERR_NOMATCH);
265 v = gargv = saveblk(v);
268 while (*(fileptr = v++) != '\0') {
269 xprintf("%S", res = filetest(ftest, &fileptr, 0));
285 plist(&shvhed, VAR_ALL);
294 register struct varent *vp;
301 plist(&aliases, VAR_ALL);
303 vp = adrof1(strip(p), &aliases);
305 blkpr(vp->vec), xputchar('\n');
308 if (eq(p, STRalias) || eq(p, STRunalias)) {
309 setname(short2str(p));
310 stderror(ERR_NAME | ERR_DANGER);
312 set1(strip(p), saveblk(v), &aliases, VAR_READWRITE);
349 #else /* !WINNT_NATIVE */
350 char **p = short2blk(v);
353 rechist(NULL, adrof(STRsavehist) != NULL);
354 (void) signal(SIGTERM, parterm);
355 (void) execv(_PATH_BIN_LOGIN, p);
356 (void) execv(_PATH_USRBIN_LOGIN, p);
357 blkfree((Char **) p);
360 #endif /* !WINNT_NATIVE */
372 if (chkstop == 0 && setintr)
374 (void) signal(SIGTERM, parterm);
377 * From Beto Appleton (beto@aixwiz.austin.ibm.com)
378 * Newgrp can take 2 arguments...
380 (void) execv(_PATH_BIN_NEWGRP, p);
381 (void) execv(_PATH_USRBIN_NEWGRP, p);
382 blkfree((Char **) p);
391 if (chkstop == 0 && setintr)
395 stderror(ERR_NOTLOGIN);
410 stderror(ERR_NAME | ERR_EMPTYIF);
411 if (eq(*vv, STRthen)) {
413 stderror(ERR_NAME | ERR_IMPRTHEN);
414 setname(short2str(STRthen));
416 * If expression was zero, then scan to else , otherwise just fall into
420 search(TC_IF, 0, NULL);
424 * Simple command attached to this if. Left shift the node in this tree,
425 * munging it so we can reexecute it.
428 lshift(kp->t_dcom, vv - kp->t_dcom);
435 * Reexecute a command, being careful not
436 * to redo i/o redirection, which is already set up.
440 register struct command *kp;
442 kp->t_dflg &= F_SAVE;
443 kp->t_dflg |= F_REPEAT;
445 * If tty is still ours to arbitrate, arbitrate it; otherwise dont even set
446 * pgrp's as the jobs would then have no way to get the tty (we can't give
447 * it to them, and our parent wouldn't know their pgrp, etc.
449 execute(kp, (tpgrp > 0 ? tpgrp : -1), NULL, NULL, TRUE);
460 search(TC_ELSE, 0, NULL);
472 gotolab(lp = globone(v[1], G_ERROR));
480 register struct whyle *wp;
482 * While we still can, locate any unknown ends of existing loops. This
483 * obscure code is the WORST result of the fact that we don't really parse.
486 for (wp = whyles; wp; wp = wp->w_next)
487 if (wp->w_end.type == TCSH_F_SEEK && wp->w_end.f_seek == 0) {
488 search(TC_BREAK, 0, NULL);
494 search(TC_GOTO, 0, lab);
496 * Eliminate loops which were exited.
507 register Char *cp, *lp;
511 if (!*v || *(*v++) != '(')
512 stderror(ERR_SYNTAX);
513 cp = **v == ')' ? STRNULL : *v++;
517 stderror(ERR_SYNTAX);
518 search(TC_SWITCH, 0, lp = globone(cp, G_ERROR));
533 stderror(ERR_NAME | ERR_NOTWHILE);
544 if (chkstop == 0 && (intty || intact) && evalvec == 0)
547 * Don't DEMAND parentheses here either.
551 set(STRstatus, putn(expr(&v)), VAR_READWRITE);
553 stderror(ERR_NAME | ERR_EXPRESSION);
559 /* Always close, why only on ttys? */
569 register Char *cp, *sp;
570 register struct whyle *nwp;
576 stderror(ERR_NAME | ERR_VARBEGIN);
577 while (*cp && alnum(*cp))
580 stderror(ERR_NAME | ERR_VARALNUM);
581 if ((cp - sp) > MAXVARLEN)
582 stderror(ERR_NAME | ERR_VARTOOLONG);
584 if (v[0][0] != '(' || v[blklen(v) - 1][0] != ')')
585 stderror(ERR_NAME | ERR_NOPAREN);
591 stderror(ERR_NAME | ERR_NOMATCH);
594 v = gargv = saveblk(v);
597 nwp = (struct whyle *) xcalloc(1, sizeof *nwp);
598 nwp->w_fe = nwp->w_fe0 = v;
600 btell(&nwp->w_start);
601 nwp->w_fename = Strsave(cp);
602 nwp->w_next = whyles;
603 nwp->w_end.type = TCSH_F_SEEK;
606 * Pre-read the loop so as to be more comprehensible to a terminal user.
621 register bool again = whyles != 0 &&
622 SEEKEQ(&whyles->w_start, &lineloc) &&
623 whyles->w_fename == 0;
628 * Implement prereading here also, taking care not to evaluate the
629 * expression before the loop has been read up from a terminal.
632 status = !exp0(&v, 1);
636 stderror(ERR_NAME | ERR_EXPRESSION);
638 register struct whyle *nwp =
639 (struct whyle *) xcalloc(1, sizeof(*nwp));
641 nwp->w_start = lineloc;
642 nwp->w_end.type = TCSH_F_SEEK;
643 nwp->w_end.f_seek = 0;
644 nwp->w_next = whyles;
657 /* We ain't gonna loop no more, no more! */
664 whyles->w_end.type = TCSH_I_SEEK;
667 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
669 (void) sigrelse (SIGINT);
671 search(TC_BREAK, 0, NULL); /* read the expression in */
674 (void) sigblock(sigmask(SIGINT));
676 (void) sighold(SIGINT);
678 btell(&whyles->w_end);
690 stderror(ERR_NAME | ERR_NOTWHILE);
691 btell(&whyles->w_end);
704 stderror(ERR_NAME | ERR_NOTWHILE);
711 /* Repeating a while is simple */
712 if (whyles->w_fename == 0) {
713 bseek(&whyles->w_start);
717 * The foreach variable list actually has a spurious word ")" at the end of
718 * the w_fe list. Thus we are at the of the list if one word beyond this
721 if (!whyles->w_fe[1]) {
725 set(whyles->w_fename, quote(Strsave(*whyles->w_fe++)), VAR_READWRITE);
726 bseek(&whyles->w_start);
737 register sigmask_t omask = 0;
743 } while (v[0] != NULL && Strcmp(v[0], STRrepeat) == 0);
747 omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
749 (void) sighold(SIGINT);
754 (void) sigsetmask(omask);
756 (void) sigrelse (SIGINT);
764 (void) sigsetmask(omask);
766 (void) sigrelse (SIGINT);
778 search(TC_BRKSW, 0, NULL);
785 struct srch *sp, *sp1, *sp2;
789 * Ignore keywords inside heredocs
795 * Binary search Sp1 is the beginning of the current search range. Sp2 is
798 for (sp1 = srchn, sp2 = srchn + nsrchn; sp1 < sp2;) {
799 sp = sp1 + ((sp2 - sp1) >> 1);
800 if ((i = *cp - *sp->s_name) == 0 &&
801 (i = Strcmp(cp, str2short(sp->s_name))) == 0)
815 register struct srch *sp, *sp2;
817 for (sp = srchn, sp2 = srchn + nsrchn; sp < sp2; sp++)
818 if (sp->s_value == n)
828 search(type, level, goal)
833 Char wordbuf[BUFSIZE];
834 register Char *aword = wordbuf;
841 if (type == TC_GOTO) {
843 a.type = TCSH_F_SEEK;
848 if (intty && fseekp == feobp && aret == TCSH_F_SEEK)
849 printprompt(1, isrchx(type == TC_BREAK ? zlast : type));
850 /* xprintf("? "), flush(); */
852 (void) getword(aword);
853 switch (srchx(aword)) {
856 if (level == 0 && type == TC_IF)
861 while (getword(aword))
863 if ((type == TC_IF || type == TC_ELSE) &&
869 if (type == TC_IF || type == TC_ELSE)
876 if (type == TC_BREAK)
881 if (type == TC_BRKSW) {
890 if (type == TC_BREAK)
896 if (type == TC_SWITCH || type == TC_BRKSW)
901 if (type == TC_SWITCH || type == TC_BRKSW)
906 if (type == TC_GOTO && getword(aword) && eq(aword, goal))
911 if (type != TC_GOTO && (type != TC_SWITCH || level != 0))
913 if (lastchr(aword) != ':')
915 aword[Strlen(aword) - 1] = 0;
916 if ((type == TC_GOTO && eq(aword, goal)) ||
917 (type == TC_SWITCH && eq(aword, STRdefault)))
922 if (type != TC_SWITCH || level != 0)
924 (void) getword(aword);
925 if (lastchr(aword) == ':')
926 aword[Strlen(aword) - 1] = 0;
927 cp = strip(Dfix1(aword));
928 if (Gmatch(goal, cp))
934 if (type == TC_SWITCH && level == 0)
938 (void) getword(NULL);
939 } while (level >= 0);
946 int found = 0, first;
952 while (c == ' ' || c == '\t')
957 while (c >= 0 && c != '\n');
970 if (c == '\\' && (c = readc(1)) == '\n')
972 if (c == '\'' || c == '"') {
984 if (!first && !d && c == '(') {
994 } while ((d || (c != ' ' && c != '\t')) && c != '\n');
1007 stderror(ERR_NAME | ERR_NOTFOUND, "then/endif");
1011 stderror(ERR_NAME | ERR_NOTFOUND, "endif");
1016 stderror(ERR_NAME | ERR_NOTFOUND, "endsw");
1020 stderror(ERR_NAME | ERR_NOTFOUND, "end");
1024 setname(short2str(Sgoal));
1025 stderror(ERR_NAME | ERR_NOTFOUND, "label");
1038 if (whyles->w_end.type == TCSH_F_SEEK && whyles->w_end.f_seek == 0) {
1039 search(TC_BREAK, 0, NULL);
1040 btell(&whyles->w_end);
1041 whyles->w_end.f_seek--;
1044 bseek(&whyles->w_end);
1056 xfree((ptr_t) wp->w_fename);
1066 nwp = NULL; /* sun lint is dumb! */
1070 static char foo[] = "IAFE";
1076 xprintf("o->type %c o->a_seek %d o->f_seek %d\n",
1077 foo[o.type + 1], o.a_seek, o.f_seek);
1080 for (; whyles; whyles = nwp) {
1081 register struct whyle *wp = whyles;
1085 xprintf("start->type %c start->a_seek %d start->f_seek %d\n",
1086 foo[wp->w_start.type+1],
1087 wp->w_start.a_seek, wp->w_start.f_seek);
1088 xprintf("end->type %c end->a_seek %d end->f_seek %d\n",
1089 foo[wp->w_end.type + 1], wp->w_end.a_seek, wp->w_end.f_seek);
1093 * XXX: We free loops that have different seek types.
1095 if (wp->w_end.type != TCSH_I_SEEK && wp->w_start.type == wp->w_end.type &&
1096 wp->w_start.type == o.type) {
1097 if (wp->w_end.type == TCSH_F_SEEK) {
1098 if (o.f_seek >= wp->w_start.f_seek &&
1099 (wp->w_end.f_seek == 0 || o.f_seek < wp->w_end.f_seek))
1103 if (o.a_seek >= wp->w_start.a_seek &&
1104 (wp->w_end.a_seek == 0 || o.a_seek < wp->w_end.a_seek))
1142 int echo_style = ECHO_STYLE;
1143 #else /* !ECHO_STYLE */
1145 int echo_style = SYSV_ECHO;
1146 # else /* SYSVREL == 0 */
1147 int echo_style = BSD_ECHO;
1148 # endif /* SYSVREL */
1149 #endif /* ECHO_STYLE */
1152 if ((vp = adrof(STRecho_style)) != NULL && vp->vec != NULL &&
1153 vp->vec[0] != NULL) {
1154 if (Strcmp(vp->vec[0], STRbsd) == 0)
1155 echo_style = BSD_ECHO;
1156 else if (Strcmp(vp->vec[0], STRsysv) == 0)
1157 echo_style = SYSV_ECHO;
1158 else if (Strcmp(vp->vec[0], STRboth) == 0)
1159 echo_style = BOTH_ECHO;
1160 else if (Strcmp(vp->vec[0], STRnone) == 0)
1161 echo_style = NONE_ECHO;
1166 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
1167 #else /* !BSDSIGS */
1168 (void) sigrelse (SIGINT);
1169 #endif /* BSDSIGS */
1173 gflag = 0, tglob(v);
1177 stderror(ERR_NAME | ERR_NOMATCH);
1180 v = gargv = saveblk(v);
1184 if ((echo_style & BSD_ECHO) != 0 && sep == ' ' && *v && eq(*v, STRmn))
1187 while ((cp = *v++) != 0) {
1190 while ((c = *cp++) != 0) {
1191 if ((echo_style & SYSV_ECHO) != 0 && c == '\\') {
1192 switch (c = *cp++) {
1203 #if 0 /* Windows does not understand \e */
1229 if (*cp >= '0' && *cp < '8')
1230 c = c * 8 + *cp++ - '0';
1231 if (*cp >= '0' && *cp < '8')
1232 c = c * 8 + *cp++ - '0';
1233 if (*cp >= '0' && *cp < '8')
1234 c = c * 8 + *cp++ - '0';
1241 xputchar('\\' | QUOTE);
1245 xputchar(c | QUOTE);
1249 xputchar(sep | QUOTE);
1252 if (sep && nonl == 0)
1258 (void) sigblock(sigmask(SIGINT));
1259 #else /* !BSDSIGS */
1260 (void) sighold(SIGINT);
1261 #endif /* BSDSIGS */
1263 blkfree(gargv), gargv = 0;
1266 /* check whether an environment variable should invoke 'set_locale()' */
1271 static Char *locale_vars[] = {
1272 STRLANG, STRLC_ALL, STRLC_CTYPE, STRLC_NUMERIC,
1273 STRLC_TIME, STRLC_COLLATE, STRLC_MESSAGES, STRLC_MONETARY, 0
1277 for (v = locale_vars; *v; ++v)
1290 extern bool output_raw;
1291 extern bool xlate_cr;
1296 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
1297 #else /* !BSDSIGS */
1298 (void) sigrelse (SIGINT);
1299 #endif /* BSDSIGS */
1306 for (ep = STR_environ; *ep; ep++)
1307 xprintf("%S\n", *ep);
1310 else if ((e = tgetenv(*v)) != NULL) {
1316 set(STRstatus, Strsave(STR1), VAR_READWRITE);
1319 /* from "Karl Berry." <karl%mote.umb.edu@relay.cs.net> -- for NeXT things
1320 (and anything else with a modern compiler) */
1340 for (; *lp != '\0' ; lp++) {
1342 stderror(ERR_NAME | ERR_SYNTAX);
1344 if ((lp = *v++) == 0)
1347 tsetenv(vp, lp = globone(lp, G_APPEND));
1348 if (eq(vp, STRKPATH)) {
1356 if (eq(vp, STRSYSTYPE)) {
1363 /* dspkanji/dspmbyte autosetting */
1364 /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
1365 #if defined(DSPMBYTE)
1366 if(eq(vp, STRLANG) && !adrof(CHECK_MBYTEVAR)) {
1367 autoset_dspmbyte(lp);
1371 if (islocale_var(vp)) {
1375 # ifdef SETLOCALEBUG
1377 # endif /* SETLOCALEBUG */
1378 (void) setlocale(LC_ALL, "");
1380 (void) setlocale(LC_COLLATE, "");
1382 # ifdef NLS_CATALOGS
1384 (void) setlocale(LC_MESSAGES, "");
1385 # endif /* LC_MESSAGES */
1386 (void) catclose(catd);
1388 # endif /* NLS_CATALOGS */
1390 (void) setlocale(LC_CTYPE, ""); /* for iscntrl */
1391 # endif /* LC_CTYPE */
1392 # ifdef SETLOCALEBUG
1394 # endif /* SETLOCALEBUG */
1397 # endif /* STRCOLLBUG */
1398 tw_cmd_free(); /* since the collation sequence has changed */
1399 for (k = 0200; k <= 0377 && !Isprint(k); k++)
1401 AsciiOnly = k > 0377;
1405 NLSMapsAreInited = 0;
1407 if (MapsAreInited && !NLSMapsAreInited)
1414 if (eq(vp, STRNLSPATH)) {
1415 (void) catclose(catd);
1420 if (eq(vp, STRNOREBIND)) {
1423 NLSMapsAreInited = 0;
1429 if (eq(vp, STRtcshlang)) {
1434 #endif /* WINNT_NATIVE */
1435 if (eq(vp, STRKTERM)) {
1437 set(STRterm, quote(lp), VAR_READWRITE); /* lp memory used here */
1439 if (noediting && strcmp(t, "unknown") != 0 && strcmp(t,"dumb") != 0) {
1442 set(STRedit, Strsave(STRNULL), VAR_READWRITE);
1449 if (eq(vp, STRKHOME)) {
1451 * convert to canonical pathname (possibly resolving symlinks)
1453 lp = dcanon(lp, lp);
1454 set(STRhome, quote(lp), VAR_READWRITE); /* cp memory used here */
1456 /* fix directory stack for new tilde home */
1461 if (eq(vp, STRKSHLVL)) {
1462 /* lp memory used here */
1463 set(STRshlvl, quote(lp), VAR_READWRITE);
1467 if (eq(vp, STRKUSER)) {
1468 set(STRuser, quote(lp), VAR_READWRITE); /* lp memory used here */
1472 if (eq(vp, STRKGROUP)) {
1473 set(STRgroup, quote(lp), VAR_READWRITE); /* lp memory used here */
1478 if (eq(vp, STRLS_COLORS)) {
1482 #endif /* COLOR_LS_F */
1486 * Load/Update $LINES $COLUMNS
1488 if ((eq(lp, STRNULL) && (eq(vp, STRLINES) || eq(vp, STRCOLUMNS))) ||
1489 eq(vp, STRTERMCAP)) {
1491 check_window_size(1);
1496 * Change the size to the one directed by $LINES and $COLUMNS
1498 if (eq(vp, STRLINES) || eq(vp, STRCOLUMNS)) {
1506 #endif /* SIG_WINDOW */
1518 static Char *name = NULL;
1522 xfree((ptr_t) name);
1524 * Find the longest environment variable
1526 for (maxi = 0, ep = STR_environ; *ep; ep++) {
1527 for (i = 0, p = *ep; *p && *p != '='; p++, i++)
1533 name = (Char *) xmalloc((size_t) ((maxi + 1) * sizeof(Char)));
1536 for (maxi = 1; maxi;)
1537 for (maxi = 0, ep = STR_environ; *ep; ep++) {
1538 for (n = name, p = *ep; *p && *p != '='; *n++ = *p++)
1541 if (!Gmatch(name, *v))
1545 /* Unset the name. This wasn't being done until
1546 * later but most of the stuff following won't
1547 * work (particularly the setlocale() and getenv()
1548 * stuff) as intended until the name is actually
1553 if (eq(name, STRNOREBIND)) {
1556 NLSMapsAreInited = 0;
1560 else if (eq(name, STRSYSTYPE))
1563 else if (islocale_var(name)) {
1567 # ifdef SETLOCALEBUG
1569 # endif /* SETLOCALEBUG */
1570 (void) setlocale(LC_ALL, "");
1572 (void) setlocale(LC_COLLATE, "");
1574 # ifdef NLS_CATALOGS
1576 (void) setlocale(LC_MESSAGES, "");
1577 # endif /* LC_MESSAGES */
1578 (void) catclose(catd);
1580 # endif /* NLS_CATALOGS */
1582 (void) setlocale(LC_CTYPE, ""); /* for iscntrl */
1583 # endif /* LC_CTYPE */
1584 # ifdef SETLOCALEBUG
1586 # endif /* SETLOCALEBUG */
1589 # endif /* STRCOLLBUG */
1590 tw_cmd_free();/* since the collation sequence has changed */
1591 for (k = 0200; k <= 0377 && !Isprint(k); k++)
1593 AsciiOnly = k > 0377;
1595 AsciiOnly = getenv("LANG") == NULL &&
1596 getenv("LC_CTYPE") == NULL;
1598 NLSMapsAreInited = 0;
1600 if (MapsAreInited && !NLSMapsAreInited)
1605 else if (eq(name,(STRtcshlang))) {
1609 #endif /* WINNT_NATIVE */
1611 else if (eq(name, STRLS_COLORS))
1613 #endif /* COLOR_LS_F */
1615 else if (eq(name, STRNLSPATH)) {
1616 (void) catclose(catd);
1621 * start again cause the environment changes
1625 xfree((ptr_t) name); name = NULL;
1632 #ifdef SETENV_IN_LIB
1634 * XXX: This does not work right, since tcsh cannot track changes to
1635 * the environment this way. (the builtin setenv without arguments does
1636 * not print the right stuff neither does unsetenv). This was for Mach,
1637 * it is not needed anymore.
1640 char nameBuf[BUFSIZE];
1641 char *cname = short2str(name);
1645 (void) strcpy(nameBuf, cname);
1646 setenv(nameBuf, short2str(val), 1);
1647 #else /* !SETENV_IN_LIB */
1648 register Char **ep = STR_environ;
1649 register Char *cp, *dp;
1654 nt_set_env(name,val);
1655 #endif /* WINNT_NATIVE */
1658 for (cp = name, dp = *ep; *cp && Tolower(*cp & TRIM) == Tolower(*dp);
1661 for (cp = name, dp = *ep; *cp && (*cp & TRIM) == *dp; cp++, dp++)
1662 #endif /* WINNT_NATIVE */
1664 if (*cp != 0 || *dp != '=')
1666 cp = Strspl(STRequal, val);
1667 xfree((ptr_t) * ep);
1668 *ep = strip(Strspl(name, cp));
1670 blkfree((Char **) environ);
1671 environ = short2blk(STR_environ);
1674 cp = Strspl(name, STRequal);
1675 blk[0] = strip(Strspl(cp, val));
1678 STR_environ = blkspl(STR_environ, blk);
1679 blkfree((Char **) environ);
1680 environ = short2blk(STR_environ);
1682 #endif /* SETENV_IN_LIB */
1689 register Char **ep = STR_environ;
1690 register Char *cp, *dp;
1694 nt_set_env(name,NULL);
1695 #endif /*WINNT_NATIVE */
1697 for (cp = name, dp = *ep; *cp && *cp == *dp; cp++, dp++)
1699 if (*cp != 0 || *dp != '=')
1703 STR_environ = blkspl(STR_environ, ep + 1);
1704 blkfree((Char **) environ);
1705 environ = short2blk(STR_environ);
1719 register Char *cp = v[1];
1730 while (Isdigit(*cp) && *cp != '8' && *cp != '9')
1731 i = i * 8 + *cp++ - '0';
1732 if (*cp || i < 0 || i > 0777)
1733 stderror(ERR_NAME | ERR_MASK);
1739 typedef long RLIM_TYPE;
1740 # ifndef RLIM_INFINITY
1741 # if !defined(_MINIX) && !defined(__clipper__) && !defined(_CRAY)
1742 extern RLIM_TYPE ulimit();
1743 # endif /* ! _MINIX && !__clipper__ */
1744 # define RLIM_INFINITY 0x003fffff
1745 # define RLIMIT_FSIZE 1
1746 # endif /* RLIM_INFINITY */
1748 # define toset(a) (((a) == 3) ? 1004 : (a) + 1)
1749 # define RLIMIT_DATA 3
1750 # define RLIMIT_STACK 1005
1752 # define toset(a) ((a) + 1)
1754 # else /* BSDLIMIT */
1755 # if (defined(BSD4_4) || defined(__linux__) || (HPUXVERSION >= 1100)) && !defined(__386BSD__)
1756 typedef rlim_t RLIM_TYPE;
1758 # if defined(SOLARIS2) || (defined(sgi) && SYSVREL > 3)
1759 typedef rlim_t RLIM_TYPE;
1762 typedef long long RLIM_TYPE;
1764 typedef unsigned long RLIM_TYPE;
1766 # endif /* SOLARIS2 || (sgi && SYSVREL > 3) */
1767 # endif /* BSD4_4 && !__386BSD__ */
1768 # endif /* BSDLIMIT */
1770 # if (HPUXVERSION > 700) && (HPUXVERSION < 1100) && defined(BSDLIMIT)
1771 /* Yes hpux8.0 has limits but <sys/resource.h> does not make them public */
1772 /* Yes, we could have defined _KERNEL, and -I/etc/conf/h, but is that better? */
1774 # define RLIMIT_CPU 0
1775 # define RLIMIT_FSIZE 1
1776 # define RLIMIT_DATA 2
1777 # define RLIMIT_STACK 3
1778 # define RLIMIT_CORE 4
1779 # define RLIMIT_RSS 5
1780 # define RLIMIT_NOFILE 6
1781 # endif /* RLIMIT_CPU */
1782 # ifndef RLIM_INFINITY
1783 # define RLIM_INFINITY 0x7fffffff
1784 # endif /* RLIM_INFINITY */
1786 * old versions of HP/UX counted limits in 512 bytes
1789 # define FILESIZE512
1790 # endif /* SIGRTMIN */
1791 # endif /* (HPUXVERSION > 700) && (HPUXVERSION < 1100) && BSDLIMIT */
1793 # if SYSVREL > 3 && defined(BSDLIMIT) && !defined(_SX)
1794 /* In order to use rusage, we included "/usr/ucbinclude/sys/resource.h" in */
1795 /* sh.h. However, some SVR4 limits are defined in <sys/resource.h>. Rather */
1796 /* than include both and get warnings, we define the extra SVR4 limits here. */
1797 /* XXX: I don't understand if RLIMIT_AS is defined, why don't we define */
1798 /* RLIMIT_VMEM based on it? */
1799 # ifndef RLIMIT_VMEM
1800 # define RLIMIT_VMEM 6
1803 # define RLIMIT_AS RLIMIT_VMEM
1805 # endif /* SYSVREL > 3 && BSDLIMIT */
1807 # if defined(__linux__) && defined(RLIMIT_AS) && !defined(RLIMIT_VMEM)
1808 # define RLIMIT_VMEM RLIMIT_AS
1811 struct limits limits[] =
1814 { RLIMIT_CPU, "cputime", 1, "seconds" },
1815 # endif /* RLIMIT_CPU */
1817 # ifdef RLIMIT_FSIZE
1819 { RLIMIT_FSIZE, "filesize", 1024, "kbytes" },
1821 { RLIMIT_FSIZE, "filesize", 512, "blocks" },
1823 # endif /* RLIMIT_FSIZE */
1826 { RLIMIT_DATA, "datasize", 1024, "kbytes" },
1827 # endif /* RLIMIT_DATA */
1829 # ifdef RLIMIT_STACK
1831 { RLIMIT_STACK, "stacksize", 1024, "kbytes" },
1833 { RLIMIT_STACK, "stacksize", 1024 * 1024, "kbytes"},
1835 # endif /* RLIMIT_STACK */
1838 { RLIMIT_CORE, "coredumpsize", 1024, "kbytes" },
1839 # endif /* RLIMIT_CORE */
1842 { RLIMIT_RSS, "memoryuse", 1024, "kbytes" },
1843 # endif /* RLIMIT_RSS */
1846 { RLIMIT_UMEM, "memoryuse", 1024, "kbytes" },
1847 # endif /* RLIMIT_UMEM */
1850 { RLIMIT_VMEM, "vmemoryuse", 1024, "kbytes" },
1851 # endif /* RLIMIT_VMEM */
1853 # ifdef RLIMIT_NOFILE
1854 { RLIMIT_NOFILE, "descriptors", 1, "" },
1855 # endif /* RLIMIT_NOFILE */
1857 # ifdef RLIMIT_CONCUR
1858 { RLIMIT_CONCUR, "concurrency", 1, "thread(s)" },
1859 # endif /* RLIMIT_CONCUR */
1861 # ifdef RLIMIT_MEMLOCK
1862 { RLIMIT_MEMLOCK, "memorylocked", 1024, "kbytes" },
1863 # endif /* RLIMIT_MEMLOCK */
1865 # ifdef RLIMIT_NPROC
1866 { RLIMIT_NPROC, "maxproc", 1, "" },
1867 # endif /* RLIMIT_NPROC */
1869 # if defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE)
1870 { RLIMIT_OFILE, "openfiles", 1, "" },
1871 # endif /* RLIMIT_OFILE && !defined(RLIMIT_NOFILE) */
1873 # ifdef RLIMIT_SBSIZE
1874 { RLIMIT_SBSIZE, "sbsize", 1, "" },
1875 # endif /* RLIMIT_SBSIZE */
1877 { -1, NULL, 0, NULL }
1880 static struct limits *findlim __P((Char *));
1881 static RLIM_TYPE getval __P((struct limits *, Char **));
1882 static void limtail __P((Char *, char*));
1883 static void plim __P((struct limits *, int));
1884 static int setlim __P((struct limits *, int, RLIM_TYPE));
1888 restrict_limit(value)
1892 * is f too large to cope with? return the maximum or minimum int
1894 if (value > (double) INT_MAX)
1895 return (RLIM_TYPE) INT_MAX;
1896 else if (value < (double) INT_MIN)
1897 return (RLIM_TYPE) INT_MIN;
1899 return (RLIM_TYPE) value;
1902 # define restrict_limit(x) ((RLIM_TYPE) (x))
1906 static struct limits *
1910 register struct limits *lp, *res;
1912 res = (struct limits *) NULL;
1913 for (lp = limits; lp->limconst >= 0; lp++)
1914 if (prefix(cp, str2short(lp->limname))) {
1916 stderror(ERR_NAME | ERR_AMBIG);
1921 stderror(ERR_NAME | ERR_LIMIT);
1932 register struct limits *lp;
1933 register RLIM_TYPE limit;
1938 if (*v && eq(*v, STRmh)) {
1943 for (lp = limits; lp->limconst >= 0; lp++)
1952 limit = getval(lp, v + 1);
1953 if (setlim(lp, hard, limit) < 0)
1954 stderror(ERR_SILENT);
1959 register struct limits *lp;
1963 #ifndef atof /* This can be a macro on linux */
1964 extern double atof __P((const char *));
1968 f = atof(short2str(cp));
1972 * is f too large to cope with. limit f to minint, maxint - X-6768 by
1975 if ((f < (double) INT_MIN) || (f > (double) INT_MAX)) {
1976 stderror(ERR_NAME | ERR_TOOLARGE);
1978 # endif /* convex */
1980 while (Isdigit(*cp) || *cp == '.' || *cp == 'e' || *cp == 'E')
1984 return restrict_limit((f * lp->limdiv) + 0.5);
1990 if (lp->limconst != RLIMIT_CPU)
1992 return f == 0.0 ? (RLIM_TYPE) 0 : restrict_limit((f * 60.0 + atof(short2str(cp + 1))));
1994 if (lp->limconst != RLIMIT_CPU)
1996 limtail(cp, "hours");
2000 if (lp->limconst == RLIMIT_CPU) {
2001 limtail(cp, "minutes");
2006 limtail(cp, "megabytes");
2007 f *= 1024.0 * 1024.0;
2010 if (lp->limconst != RLIMIT_CPU)
2012 limtail(cp, "seconds");
2014 # endif /* RLIMIT_CPU */
2017 if (lp->limconst == RLIMIT_CPU)
2019 # endif /* RLIMIT_CPU */
2021 limtail(cp, "megabytes");
2022 f *= 1024.0 * 1024.0;
2026 if (lp->limconst == RLIMIT_CPU)
2028 # endif /* RLIMIT_CPU */
2029 limtail(cp, "kbytes");
2034 if (lp->limconst == RLIMIT_CPU)
2036 # endif /* RLIMIT_CPU */
2037 limtail(cp, "blocks");
2041 limtail(cp, "unlimited");
2042 return ((RLIM_TYPE) RLIM_INFINITY);
2046 # endif /* RLIMIT_CPU */
2047 stderror(ERR_NAME | ERR_SCALEF);
2050 return f == 0.0 ? (RLIM_TYPE) 0 : restrict_limit((f + 0.5));
2053 if (f > (float) RLIM_INFINITY)
2054 return ((RLIM_TYPE) RLIM_INFINITY);
2056 return ((RLIM_TYPE) f);
2057 # endif /* convex */
2068 while (*cp && *cp == *str)
2071 stderror(ERR_BADSCALE, sp);
2078 register struct limits *lp;
2083 # endif /* BSDLIMIT */
2085 int div = lp->limdiv;
2087 xprintf("%-13.13s", lp->limname);
2090 limit = ulimit(lp->limconst, 0);
2092 if (lp->limconst == RLIMIT_DATA)
2093 limit -= 0x20000000;
2095 # else /* BSDLIMIT */
2096 (void) getrlimit(lp->limconst, &rlim);
2097 limit = hard ? rlim.rlim_max : rlim.rlim_cur;
2098 # endif /* BSDLIMIT */
2100 # if !defined(BSDLIMIT) || defined(FILESIZE512)
2102 * Christos: filesize comes in 512 blocks. we divide by 2 to get 1024
2103 * blocks. Note we cannot pre-multiply cause we might overflow (A/UX)
2105 if (lp->limconst == RLIMIT_FSIZE) {
2106 if (limit >= (RLIM_INFINITY / 512))
2107 limit = RLIM_INFINITY;
2109 div = (div == 1024 ? 2 : 1);
2111 # endif /* !BSDLIMIT || FILESIZE512 */
2113 if (limit == RLIM_INFINITY)
2114 xprintf("unlimited");
2117 if (lp->limconst == RLIMIT_CPU)
2118 psecs((long) limit);
2120 # endif /* RLIMIT_CPU */
2121 xprintf("%ld %s", (long) (limit / div), lp->limscale);
2131 register struct limits *lp;
2137 while (*++v && **v == '-') {
2148 stderror(ERR_ULIMUS);
2154 for (lp = limits; lp->limconst >= 0; lp++)
2155 if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0)
2158 stderror(ERR_SILENT);
2163 if (setlim(lp, hard, (RLIM_TYPE) RLIM_INFINITY) < 0 && !force)
2164 stderror(ERR_SILENT);
2169 setlim(lp, hard, limit)
2170 register struct limits *lp;
2177 (void) getrlimit(lp->limconst, &rlim);
2180 /* Even though hpux has setrlimit(), it expects fsize in 512 byte blocks */
2181 if (limit != RLIM_INFINITY && lp->limconst == RLIMIT_FSIZE)
2183 # endif /* FILESIZE512 */
2185 rlim.rlim_max = limit;
2186 else if (limit == RLIM_INFINITY && euid != 0)
2187 rlim.rlim_cur = rlim.rlim_max;
2189 rlim.rlim_cur = limit;
2191 if (rlim.rlim_cur > rlim.rlim_max)
2192 rlim.rlim_max = rlim.rlim_cur;
2194 if (setrlimit(lp->limconst, &rlim) < 0) {
2195 # else /* BSDLIMIT */
2196 if (limit != RLIM_INFINITY && lp->limconst == RLIMIT_FSIZE)
2199 if (lp->limconst == RLIMIT_DATA)
2200 limit += 0x20000000;
2202 if (ulimit(toset(lp->limconst), limit) < 0) {
2203 # endif /* BSDLIMIT */
2204 xprintf(CGETS(15, 1, "%s: %s: Can't %s%s limit (%s)\n"), bname,
2205 lp->limname, limit == RLIM_INFINITY ? CGETS(15, 2, "remove") :
2206 CGETS(15, 3, "set"), hard ? CGETS(14, 4, " hard") : "",
2213 #endif /* !HAVENOLIMIT */
2225 #endif /* BSDJOBS */
2231 stderror(ERR_SUSPLOG);
2235 old = signal(SIGTSTP, SIG_DFL);
2236 (void) kill(0, SIGTSTP);
2237 /* the shell stops here */
2238 (void) signal(SIGTSTP, old);
2239 #else /* !BSDJOBS */
2240 stderror(ERR_JOBCONTROL);
2241 #endif /* BSDJOBS */
2246 ctpgrp = tcgetpgrp(FSHTTY);
2248 stderror(ERR_SYSTEM, "tcgetpgrp", strerror(errno));
2249 if (ctpgrp != opgrp) {
2250 old = signal(SIGTTIN, SIG_DFL);
2251 (void) kill(0, SIGTTIN);
2252 (void) signal(SIGTTIN, old);
2255 (void) setpgid(0, shpgrp);
2256 (void) tcsetpgrp(FSHTTY, shpgrp);
2258 #endif /* BSDJOBS */
2259 (void) setdisc(FSHTTY);
2262 /* This is the dreaded EVAL built-in.
2263 * If you don't fiddle with file descriptors, and reset didfds,
2264 * this command will either ignore redirection inside or outside
2265 * its arguments, e.g. eval "date >x" vs. eval "date" >x
2266 * The stuff here seems to work, but I did it by trial and error rather
2267 * than really knowing what was going on. If tpgrp is zero, we are
2268 * probably a background eval, e.g. "eval date &", and we want to
2269 * make sure that any processes we start stay in our pgrp.
2270 * This is also the case for "time eval date" -- stay in same pgrp.
2271 * Otherwise, under stty tostop, processes will stop in the wrong
2272 * pgrp, with no way for the shell to get them going again. -IAN!
2275 static Char **gv = NULL, **gav = NULL;
2286 #ifndef CLOSE_ON_EXEC
2288 #endif /* CLOSE_ON_EXEC */
2292 int saveIN, saveOUT, saveDIAG;
2293 int oSHIN, oSHOUT, oSHDIAG;
2299 #ifndef CLOSE_ON_EXEC
2301 #endif /* CLOSE_ON_EXEC */
2312 gflag = 0, tglob(gav);
2314 gv = gav = globall(gav);
2317 stderror(ERR_NOMATCH);
2326 saveIN = dcopy(SHIN, -1);
2327 saveOUT = dcopy(SHOUT, -1);
2328 saveDIAG = dcopy(SHDIAG, -1);
2332 /* PWP: setjmp/longjmp bugfix for optimizing compilers */
2334 my_reenter = 1; /* assume non-zero return val */
2335 if (setexit() == 0) {
2336 my_reenter = 0; /* Oh well, we were wrong */
2338 if ((my_reenter = setexit()) == 0) {
2342 SHIN = dcopy(0, -1);
2343 SHOUT = dcopy(1, -1);
2344 SHDIAG = dcopy(2, -1);
2345 #ifndef CLOSE_ON_EXEC
2347 #endif /* CLOSE_ON_EXEC */
2355 #ifndef CLOSE_ON_EXEC
2357 #endif /* CLOSE_ON_EXEC */
2360 (void) close(SHOUT);
2361 (void) close(SHDIAG);
2362 SHIN = dmove(saveIN, oSHIN);
2363 SHOUT = dmove(saveOUT, oSHOUT);
2364 SHDIAG = dmove(saveDIAG, oSHDIAG);
2372 stderror(ERR_SILENT);
2375 /*************************************************************************/
2376 /* print list of builtin commands */
2384 /* would use print_by_column() in tw.parse.c but that assumes
2385 * we have an array of Char * to pass.. (sg)
2387 extern int Tty_raw_mode;
2388 extern int TermH; /* from the editor routines */
2389 extern int lbuffed; /* from sh.print.c */
2391 register struct biltins *b;
2392 register int row, col, columns, rows;
2393 unsigned int w, maxwidth;
2397 lbuffed = 0; /* turn off line buffering */
2399 /* find widest string */
2400 for (maxwidth = 0, b = bfunc; b < &bfunc[nbfunc]; ++b)
2401 maxwidth = max(maxwidth, strlen(b->bname));
2402 ++maxwidth; /* for space */
2404 columns = (TermH + 1) / maxwidth; /* PWP: terminal size change */
2407 rows = (nbfunc + (columns - 1)) / columns;
2409 for (b = bfunc, row = 0; row < rows; row++) {
2410 for (col = 0; col < columns; col++) {
2411 if (b < &bfunc[nbfunc]) {
2412 w = strlen(b->bname);
2413 xprintf("%s", b->bname);
2414 if (col < (columns - 1)) /* Not last column? */
2415 for (; w < maxwidth; w++)
2420 if (row < (rows - 1)) {
2427 nt_print_builtins(maxwidth);
2432 #endif /* WINNT_NATIVE */
2434 lbuffed = 1; /* turn back on line buffering */
2442 char catalog[ 256 ] = { 't', 'c', 's', 'h', '\0' };
2444 if (adrof(STRcatalog) != NULL)
2445 xsnprintf((char *)catalog, sizeof(catalog), "tcsh.%s",
2446 short2str(varval(STRcatalog)));
2447 catd = catopen(catalog, MCLoadBySet);
2448 #endif /* NLS_CATALOGS */
2451 #endif /* WINNT_NATIVE */
2452 errinit(); /* init the errorlist in correct locale */
2453 mesginit(); /* init the messages for signals */
2454 dateinit(); /* init the messages for dates */
2455 editinit(); /* init the editor messages */
2456 terminit(); /* init the termcap messages */