1 /* $Header: /src/pub/tcsh/sh.set.c,v 3.61 2005/03/03 16:57:02 kim Exp $ */
3 * sh.set.c: Setting and Clearing of variables
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.set.c,v 3.61 2005/03/03 16:57:02 kim Exp $")
40 #ifdef HAVE_NL_LANGINFO
44 extern int GotTermCaps;
47 static void update_vars __P((Char *));
48 static Char *getinx __P((Char *, int *));
49 static void asx __P((Char *, int, Char *));
50 static struct varent *getvx __P((Char *, int));
51 static Char *xset __P((Char *, Char ***));
52 static Char *operate __P((int, Char *, Char *));
53 static void putn1 __P((int));
54 static struct varent *madrof __P((Char *, struct varent *));
55 static void unsetv1 __P((struct varent *));
56 static void exportpath __P((Char **));
57 static void balance __P((struct varent *, int, int));
67 if (eq(vp, STRpath)) {
68 exportpath(adrof(STRpath)->vec);
71 else if (eq(vp, STRhistchars)) {
72 Char *pn = varval(vp);
77 else if (eq(vp, STRpromptchars)) {
78 Char *pn = varval(vp);
83 else if (eq(vp, STRhistlit)) {
86 else if (eq(vp, STRuser)) {
87 tsetenv(STRKUSER, varval(vp));
88 tsetenv(STRLOGNAME, varval(vp));
90 else if (eq(vp, STRgroup)) {
91 tsetenv(STRKGROUP, varval(vp));
93 else if (eq(vp, STRwordchars)) {
94 word_chars = varval(vp);
96 else if (eq(vp, STRloginsh)) {
99 else if (eq(vp, STRsymlinks)) {
100 Char *pn = varval(vp);
102 if (eq(pn, STRignore))
103 symlinks = SYM_IGNORE;
104 else if (eq(pn, STRexpand))
105 symlinks = SYM_EXPAND;
106 else if (eq(pn, STRchase))
107 symlinks = SYM_CHASE;
111 else if (eq(vp, STRterm)) {
112 Char *cp = varval(vp);
113 tsetenv(STRKTERM, cp);
114 #ifdef DOESNT_WORK_RIGHT
115 cp = getenv("TERMCAP");
116 if (cp && (*cp != '/')) /* if TERMCAP and not a path */
117 Unsetenv(STRTERMCAP);
118 #endif /* DOESNT_WORK_RIGHT */
120 if (noediting && Strcmp(cp, STRnetwork) != 0 &&
121 Strcmp(cp, STRunknown) != 0 && Strcmp(cp, STRdumb) != 0) {
124 set(STRedit, Strsave(STRNULL), VAR_READWRITE);
126 ed_Init(); /* reset the editor */
128 else if (eq(vp, STRhome)) {
131 cp = Strsave(varval(vp)); /* get the old value back */
134 * convert to cononical pathname (possibly resolving symlinks)
138 set(vp, Strsave(cp), VAR_READWRITE); /* have to save the new val */
140 /* and now mirror home with HOME */
141 tsetenv(STRKHOME, cp);
142 /* fix directory stack for new tilde home */
146 else if (eq(vp, STRedit)) {
149 /* PWP: add more stuff in here later */
151 else if (eq(vp, STRshlvl)) {
152 tsetenv(STRKSHLVL, varval(vp));
154 else if (eq(vp, STRignoreeof)) {
157 for ((cp = varval(STRignoreeof)); cp && *cp; cp++) {
162 numeof = numeof * 10 + *cp - '0';
164 if (numeof <= 0) numeof = 26; /* Sanity check */
166 else if (eq(vp, STRbackslash_quote)) {
169 else if (eq(vp, STRdirstack)) {
172 else if (eq(vp, STRrecognize_only_executables)) {
175 else if (eq(vp, STRkillring)) {
176 SetKillRing(getn(varval(vp)));
179 else if (eq(vp, STRwatch)) {
182 #endif /* HAVENOUTMP */
183 else if (eq(vp, STRimplicitcd)) {
184 implicit_cd = ((eq(varval(vp), STRverbose)) ? 2 : 1);
187 else if (eq(vp, STRcolor)) {
190 #endif /* COLOR_LS_F */
191 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
192 else if(eq(vp, CHECK_MBYTEVAR) || eq(vp, STRnokanji)) {
193 update_dspmbyte_vars();
197 else if (eq(vp, STRcatalog)) {
201 #if defined(FILEC) && defined(TIOCSTI)
202 else if (eq(vp, STRfilec))
205 #endif /* NLS_CATALOGS */
220 int flags = VAR_READWRITE;
230 * Readonly addition From: Tim P. Starrin <noid@cyborg.larc.nasa.gov>
232 if (*v && eq(*v, STRmr)) {
233 flags = VAR_READONLY;
237 if (*v && eq(*v, STRmf) && !last_match) {
242 if (*v && eq(*v, STRml) && !first_match) {
250 plist(&shvhed, flags);
257 for (; alnum(*p); p++)
259 if (vp == p || !letter(*vp))
260 stderror(ERR_NAME | ERR_VARBEGIN);
261 if ((p - vp) > MAXVARLEN) {
262 stderror(ERR_NAME | ERR_VARTOOLONG);
267 p = getinx(p, &subscr);
269 if ((op = *p) != 0) {
271 if (*p == 0 && *v && **v == '(')
274 else if (*v && eq(*v, STRequal)) {
280 stderror(ERR_NAME | ERR_SYNTAX);
281 if (eq(p, STRLparen)) {
285 stderror(ERR_NAME | ERR_SYNTAX);
288 stderror(ERR_NAME | ERR_MISSING, ')');
301 set1(vp, vecp, &shvhed, flags);
306 asx(vp, subscr, Strsave(p));
308 set(vp, Strsave(p), flags);
310 } while ((p = *v++) != NULL);
320 while (*cp && Isdigit(*cp))
321 *ip = *ip * 10 + *cp++ - '0';
323 stderror(ERR_NAME | ERR_SUBSCRIPT);
333 struct varent *v = getvx(vp, subscr);
335 if (v->v_flags & VAR_READONLY)
336 stderror(ERR_READONLY|ERR_NAME, v->v_name);
337 xfree((ptr_t) v->vec[subscr - 1]);
338 v->vec[subscr - 1] = globone(p, G_APPEND);
341 static struct varent *
346 struct varent *v = adrof(vp);
350 if (subscr < 1 || subscr > blklen(v->vec))
351 stderror(ERR_NAME | ERR_RANGE);
359 struct command *dummy;
377 for (; alnum(*p); p++)
379 if (vp == p || !letter(*vp))
380 stderror(ERR_NAME | ERR_VARBEGIN);
381 if ((p - vp) > MAXVARLEN)
382 stderror(ERR_NAME | ERR_VARTOOLONG);
385 p = getinx(p, &subscr);
392 stderror(ERR_NAME | ERR_ASSIGN);
395 * if there is no expression after the '=' then print a "Syntax Error"
398 if (*p == '\0' && *v == NULL)
399 stderror(ERR_NAME | ERR_ASSIGN);
410 stderror(ERR_NAME | ERR_UNKNOWNOP);
416 stderror(ERR_NAME | ERR_UNKNOWNOP);
418 stderror(ERR_NAME | ERR_SYNTAX);
421 stderror(ERR_NAME | ERR_UNKNOWNOP);
429 set(vp, p, VAR_READWRITE);
432 struct varent *gv = getvx(vp, subscr);
434 asx(vp, subscr, operate(op, gv->vec[subscr - 1], p));
437 set(vp, operate(op, varval(vp), p), VAR_READWRITE);
442 } while ((p = *v++) != NULL);
454 xfree((ptr_t) ** vp);
457 return (putn(expr(vp)));
477 if (op == '<' || op == '>')
484 stderror(ERR_NAME | ERR_EXPRESSION);
488 static Char *putp, nbuf[50];
501 num = 2; /* confuse lint */
502 if (sizeof(int) == num && ((unsigned int) n) == 0x8000) {
510 num = 4; /* confuse lint */
511 if (sizeof(int) == num && ((unsigned int) n) == 0x80000000) {
519 return (Strsave(nbuf));
528 *putp++ = n % 10 + '0';
538 if (!cp) /* PWP: extra error checking */
539 stderror(ERR_NAME | ERR_BADNUM);
542 if (cp[0] == '+' && cp[1])
548 stderror(ERR_NAME | ERR_BADNUM);
552 n = n * 10 + *cp++ - '0';
554 stderror(ERR_NAME | ERR_BADNUM);
555 return (sign ? -n : n);
565 if (!var || !head) /* PWP: extra error checking */
568 vp = adrof1(var, head);
569 return ((vp == NULL || vp->vec == NULL || vp->vec[0] == NULL) ?
570 STRNULL : vp->vec[0]);
573 static struct varent *
580 for (vp = vp->v_left; vp; vp = vp->v_right) {
581 if (vp->v_left && (vp1 = madrof(pat, vp)) != NULL)
583 if (Gmatch(vp->v_name, pat))
597 while (v && ((cmp = *name - *v->v_name) != 0 ||
598 (cmp = Strcmp(name, v->v_name)) != 0))
607 * The caller is responsible for putting value in a safe place
614 Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
618 set1(var, vec, &shvhed, flags);
622 set1(var, vec, head, flags)
629 if ((flags & VAR_NOGLOB) == 0) {
636 stderror(ERR_NAME | ERR_NOMATCH);
644 * Uniqueness addition from: Michael Veksler <mveksler@vnet.ibm.com>
646 if ( flags & (VAR_FIRST | VAR_LAST) ) {
648 * Code for -f (VAR_FIRST) and -l (VAR_LAST) options.
650 * Delete all duplicate words leaving "holes" in the word array (vec).
651 * Then remove the "holes", keeping the order of the words unchanged.
653 if (vec && vec[0] && vec[1]) { /* more than one word ? */
657 for (num_items = 0; vec[num_items]; num_items++)
659 if (flags & VAR_FIRST) {
660 /* delete duplications, keeping first occurance */
661 for (i = 1; i < num_items; i++)
662 for (j = 0; j < i; j++)
663 /* If have earlier identical item, remove i'th item */
664 if (vec[i] && vec[j] && Strcmp(vec[j], vec[i]) == 0) {
669 } else if (flags & VAR_LAST) {
670 /* delete duplications, keeping last occurance */
671 for (i = 0; i < num_items - 1; i++)
672 for (j = i + 1; j < num_items; j++)
673 /* If have later identical item, remove i'th item */
674 if (vec[i] && vec[j] && Strcmp(vec[j], vec[i]) == 0) {
675 /* remove identical item (the first) */
680 /* Compress items - remove empty items */
681 for (j = i = 0; i < num_items; i++)
685 /* NULL-fy remaining items */
686 for (; j < num_items; j++)
689 /* don't let the attribute propagate */
690 flags &= ~(VAR_FIRST|VAR_LAST);
692 setq(var, vec, head, flags);
697 setq(name, vec, p, flags)
705 f = 0; /* tree hangs off the header's left link */
706 while ((c = p->v_link[f]) != 0) {
707 if ((f = *name - *c->v_name) == 0 &&
708 (f = Strcmp(name, c->v_name)) == 0) {
709 if (c->v_flags & VAR_READONLY)
710 stderror(ERR_READONLY|ERR_NAME, c->v_name);
719 p->v_link[f] = c = (struct varent *) xmalloc((size_t)sizeof(struct varent));
720 c->v_name = Strsave(name);
723 c->v_left = c->v_right = 0;
735 int did_roe, did_edit;
738 did_roe = adrof(STRrecognize_only_executables) != NULL;
739 did_edit = adrof(STRedit) != NULL;
742 #if defined(FILEC) && defined(TIOCSTI)
743 if (adrof(STRfilec) == 0)
745 #endif /* FILEC && TIOCSTI */
747 if (adrof(STRhistchars) == 0) {
751 if (adrof(STRignoreeof) == 0)
753 if (adrof(STRpromptchars) == 0) {
757 if (adrof(STRhistlit) == 0)
759 if (adrof(STRloginsh) == 0)
761 if (adrof(STRwordchars) == 0)
762 word_chars = STR_WORD_CHARS;
763 if (adrof(STRedit) == 0)
765 if (adrof(STRbackslash_quote) == 0)
767 if (adrof(STRsymlinks) == 0)
769 if (adrof(STRimplicitcd) == 0)
771 if (adrof(STRkillring) == 0)
773 if (did_edit && noediting && adrof(STRedit) == 0)
775 if (did_roe && adrof(STRrecognize_only_executables) == 0)
778 if (adrof(STRcolor) == 0)
780 #endif /* COLOR_LS_F */
781 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
782 update_dspmbyte_vars();
787 #endif /* NLS_CATALOGS */
800 while ((vp = madrof(*v, head)) != NULL)
801 if (vp->v_flags & VAR_READONLY)
802 stderror(ERR_READONLY|ERR_NAME, vp->v_name);
806 setname(short2str(*v));
816 if ((vp = adrof1(var, &shvhed)) == 0)
825 struct varent *c, *pp;
829 * Free associated memory first to avoid complications.
832 xfree((ptr_t) p->v_name);
834 * If p is missing one child, then we can move the other into where p is.
835 * Otherwise, we find the predecessor of p, which is guaranteed to have no
836 * right child, copy it into p, and move it's left child into it.
840 else if (p->v_left == 0)
843 for (c = p->v_left; c->v_right; c = c->v_right)
845 p->v_name = c->v_name;
846 p->v_flags = c->v_flags;
853 * Move c into where p is.
856 f = pp->v_right == p;
857 if ((pp->v_link[f] = c) != 0)
860 * Free the deleted node, and rebalance.
870 set(cp, Strsave(STRNULL), VAR_READWRITE);
890 if (argv == NULL || argv->vec == NULL)
892 if (argv->vec[0] == 0)
893 stderror(ERR_NAME | ERR_NOMORE);
894 lshift(argv->vec, 1);
898 static Char STRsep[2] = { PATHSEP, '\0' };
905 size_t exppath_size = BUFSIZE;
906 exppath = (Char *)xmalloc(sizeof(Char)*exppath_size);
911 while (Strlen(*val) + Strlen(exppath) + 2 > exppath_size) {
913 = (Char *)xrealloc(exppath, sizeof(Char)*(exppath_size *= 2)))
916 "Warning: ridiculously long PATH truncated\n"));
920 (void) Strcat(exppath, *val++);
921 if (*val == 0 || eq(*val, STRRparen))
923 (void) Strcat(exppath, STRsep);
925 tsetenv(STRKPATH, exppath);
931 * Lint thinks these have null effect
933 /* macros to do single rotations on node p */
934 # define rright(p) (\
936 (t)->v_parent = (p)->v_parent,\
937 (((p)->v_left = t->v_right) != NULL) ?\
938 (t->v_right->v_parent = (p)) : 0,\
939 (t->v_right = (p))->v_parent = t,\
943 ((t)->v_parent = (p)->v_parent,\
944 ((p)->v_right = t->v_left) != NULL) ? \
945 (t->v_left->v_parent = (p)) : 0,\
946 (t->v_left = (p))->v_parent = t,\
949 static struct varent *
955 static struct varent *
966 * Rebalance a tree, starting at p and up.
967 * F == 0 means we've come from p's left child.
968 * D == 1 means we've just done a delete, otherwise an insert.
978 struct varent *t; /* used by the rotate macros */
982 ff = 0; /* Sun's lint is dumb! */
986 * Ok, from here on, p is the node we're operating on; pp is it's parent; f
987 * is the branch of p from which we have come; ff is the branch of pp which
990 for (; (pp = p->v_parent) != 0; p = pp, f = ff) {
991 ff = pp->v_right == p;
992 if (f ^ d) { /* right heavy */
994 case -1: /* was left heavy */
997 case 0: /* was balanced */
1000 case 1: /* was already right heavy */
1001 switch (p->v_right->v_bal) {
1002 case 1: /* sigle rotate */
1003 pp->v_link[ff] = rleft(p);
1004 p->v_left->v_bal = 0;
1007 case 0: /* single rotate */
1008 pp->v_link[ff] = rleft(p);
1009 p->v_left->v_bal = 1;
1012 case -1: /* double rotate */
1013 (void) rright(p->v_right);
1014 pp->v_link[ff] = rleft(p);
1016 p->v_bal < 1 ? 0 : -1;
1018 p->v_bal > -1 ? 0 : 1;
1029 else { /* left heavy */
1031 case 1: /* was right heavy */
1034 case 0: /* was balanced */
1037 case -1: /* was already left heavy */
1038 switch (p->v_left->v_bal) {
1039 case -1: /* single rotate */
1040 pp->v_link[ff] = rright(p);
1041 p->v_right->v_bal = 0;
1044 case 0: /* signle rotate */
1045 pp->v_link[ff] = rright(p);
1046 p->v_right->v_bal = -1;
1049 case 1: /* double rotate */
1050 (void) rleft(p->v_left);
1051 pp->v_link[ff] = rright(p);
1053 p->v_bal < 1 ? 0 : -1;
1055 p->v_bal > -1 ? 0 : 1;
1067 * If from insert, then we terminate when p is balanced. If from
1068 * delete, then we terminate when p is unbalanced.
1070 if ((p->v_bal == 0) ^ d)
1085 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
1086 #else /* !BSDSIGS */
1087 (void) sigrelse(SIGINT);
1088 #endif /* BSDSIGS */
1094 if (p->v_parent == 0) /* is it the header? */
1096 if ((p->v_flags & what) != 0) {
1097 len = blklen(p->vec);
1098 xprintf("%S\t", p->v_name);
1113 } while (p->v_right == c);
1118 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
1119 extern int dspmbyte_ls;
1122 update_dspmbyte_vars()
1128 /* if variable "nokanji" is set, multi-byte display is disabled */
1129 if ((vp = adrof(CHECK_MBYTEVAR)) && !adrof(STRnokanji)) {
1132 if(eq (dstr1, STRsjis))
1134 else if (eq(dstr1, STReuc))
1136 else if (eq(dstr1, STRbig5))
1138 else if (eq(dstr1, STRutf8))
1140 else if ((dstr1[0] - '0') >= 0 && (dstr1[0] - '0') <= 3) {
1144 xprintf(CGETS(18, 2,
1145 "Warning: unknown multibyte display; using default(euc(JP))\n"));
1148 if (dstr1 && vp->vec[1] && eq(vp->vec[1], STRls))
1152 for (lp = 0; lp < 256 && iskcode > 0; lp++) {
1156 _cmap[lp] = _cmap_mbyte[lp];
1157 _mbmap[lp] = _mbmap_sjis[lp];
1161 _cmap[lp] = _cmap_mbyte[lp];
1162 _mbmap[lp] = _mbmap_euc[lp];
1166 _cmap[lp] = _cmap_mbyte[lp];
1167 _mbmap[lp] = _mbmap_big5[lp];
1171 _cmap[lp] = _cmap_mbyte[lp];
1172 _mbmap[lp] = _mbmap_utf8[lp];
1175 xprintf(CGETS(18, 3,
1176 "Warning: unknown multibyte code %d; multibyte disabled\n"),
1178 _cmap[lp] = _cmap_c[lp];
1179 _mbmap[lp] = 0; /* Default map all 0 */
1185 /* check original table */
1186 if (Strlen(dstr1) != 256) {
1187 xprintf(CGETS(18, 4,
1188 "Warning: Invalid multibyte table length (%d); multibyte disabled\n"),
1192 for (lp = 0; lp < 256 && _enable_mbdisp == 1; lp++) {
1193 if (!((dstr1[lp] - '0') >= 0 && (dstr1[lp] - '0') <= 3)) {
1194 xprintf(CGETS(18, 4,
1195 "Warning: bad multibyte code at offset +%d; multibyte diabled\n"),
1201 /* set original table */
1202 for (lp = 0; lp < 256; lp++) {
1203 if (_enable_mbdisp == 1) {
1204 _cmap[lp] = _cmap_mbyte[lp];
1205 _mbmap[lp] = (unsigned short) ((dstr1[lp] - '0') & 0x0f);
1208 _cmap[lp] = _cmap_c[lp];
1209 _mbmap[lp] = 0; /* Default map all 0 */
1215 for (lp = 0; lp < 256; lp++) {
1216 _cmap[lp] = _cmap_c[lp];
1217 _mbmap[lp] = 0; /* Default map all 0 */
1222 #ifdef MBYTEDEBUG /* Sorry, use for beta testing */
1225 for (lp = 0; lp < 256; lp++) {
1226 mbmapstr[lp] = _mbmap[lp] + '0';
1229 set(STRmbytemap, Strsave(mbmapstr), VAR_READWRITE);
1231 #endif /* MBYTEMAP */
1234 /* dspkanji/dspmbyte autosetting */
1235 /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
1237 autoset_dspmbyte(pcp)
1241 struct dspm_autoset_Table {
1245 { STRLANGEUCJP, STReuc },
1246 { STRLANGEUCKR, STReuc },
1247 { STRLANGEUCZH, STReuc },
1248 { STRLANGEUCJPB, STReuc },
1249 { STRLANGEUCKRB, STReuc },
1250 { STRLANGEUCZHB, STReuc },
1252 { STRLANGEUCJPC, STReuc },
1254 { STRLANGSJIS, STRsjis },
1255 { STRLANGSJISB, STRsjis },
1256 { STRLANGBIG5, STRbig5 },
1257 { STRstarutfstar8, STRutf8 },
1260 #ifdef HAVE_NL_LANGINFO
1261 struct dspm_autoset_Table dspmc[] = {
1262 { STRstarutfstar8, STRutf8 },
1264 { STRGB2312, STReuc },
1265 { STRLANGBIG5, STRbig5 },
1270 codeset = str2short(nl_langinfo(CODESET));
1271 if (*codeset != '\0') {
1272 for (i = 0; dspmc[i].n; i++) {
1274 if (dspmc[i].n[0] && t_pmatch(pcp, dspmc[i].n, &estr, 0) > 0) {
1275 set(CHECK_MBYTEVAR, Strsave(dspmc[i].v), VAR_READWRITE);
1276 update_dspmbyte_vars();
1286 for (i = 0; dspmt[i].n; i++) {
1288 if (dspmt[i].n[0] && t_pmatch(pcp, dspmt[i].n, &estr, 0) > 0) {
1289 set(CHECK_MBYTEVAR, Strsave(dspmt[i].v), VAR_READWRITE);
1290 update_dspmbyte_vars();