1 /* $Header: /src/pub/tcsh/sh.set.c,v 3.48 2004/03/21 16:48:14 christos 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.48 2004/03/21 16:48:14 christos Exp $")
41 extern bool GotTermCaps;
44 static void update_vars __P((Char *));
45 static Char *getinx __P((Char *, int *));
46 static void asx __P((Char *, int, Char *));
47 static struct varent *getvx __P((Char *, int));
48 static Char *xset __P((Char *, Char ***));
49 static Char *operate __P((int, Char *, Char *));
50 static void putn1 __P((int));
51 static struct varent *madrof __P((Char *, struct varent *));
52 static void unsetv1 __P((struct varent *));
53 static void exportpath __P((Char **));
54 static void balance __P((struct varent *, int, int));
64 if (eq(vp, STRpath)) {
65 exportpath(adrof(STRpath)->vec);
68 else if (eq(vp, STRhistchars)) {
69 register Char *pn = varval(vp);
74 else if (eq(vp, STRpromptchars)) {
75 register Char *pn = varval(vp);
80 else if (eq(vp, STRhistlit)) {
83 else if (eq(vp, STRuser)) {
84 tsetenv(STRKUSER, varval(vp));
85 tsetenv(STRLOGNAME, varval(vp));
87 else if (eq(vp, STRgroup)) {
88 tsetenv(STRKGROUP, varval(vp));
90 else if (eq(vp, STRwordchars)) {
91 word_chars = varval(vp);
93 else if (eq(vp, STRloginsh)) {
96 else if (eq(vp, STRsymlinks)) {
97 register Char *pn = varval(vp);
99 if (eq(pn, STRignore))
100 symlinks = SYM_IGNORE;
101 else if (eq(pn, STRexpand))
102 symlinks = SYM_EXPAND;
103 else if (eq(pn, STRchase))
104 symlinks = SYM_CHASE;
108 else if (eq(vp, STRterm)) {
109 Char *cp = varval(vp);
110 tsetenv(STRKTERM, cp);
111 #ifdef DOESNT_WORK_RIGHT
112 cp = getenv("TERMCAP");
113 if (cp && (*cp != '/')) /* if TERMCAP and not a path */
114 Unsetenv(STRTERMCAP);
115 #endif /* DOESNT_WORK_RIGHT */
117 if (noediting && Strcmp(cp, STRnetwork) != 0 &&
118 Strcmp(cp, STRunknown) != 0 && Strcmp(cp, STRdumb) != 0) {
121 set(STRedit, Strsave(STRNULL), VAR_READWRITE);
123 ed_Init(); /* reset the editor */
125 else if (eq(vp, STRhome)) {
128 cp = Strsave(varval(vp)); /* get the old value back */
131 * convert to cononical pathname (possibly resolving symlinks)
135 set(vp, Strsave(cp), VAR_READWRITE); /* have to save the new val */
137 /* and now mirror home with HOME */
138 tsetenv(STRKHOME, cp);
139 /* fix directory stack for new tilde home */
143 else if (eq(vp, STRedit)) {
146 /* PWP: add more stuff in here later */
148 else if (eq(vp, STRshlvl)) {
149 tsetenv(STRKSHLVL, varval(vp));
151 else if (eq(vp, STRignoreeof)) {
154 for ((cp = varval(STRignoreeof)); cp && *cp; cp++) {
159 numeof = numeof * 10 + *cp - '0';
161 if (numeof <= 0) numeof = 26; /* Sanity check */
163 else if (eq(vp, STRbackslash_quote)) {
166 else if (eq(vp, STRdirstack)) {
169 else if (eq(vp, STRrecognize_only_executables)) {
172 else if (eq(vp, STRkillring)) {
173 SetKillRing(getn(varval(vp)));
176 else if (eq(vp, STRwatch)) {
179 #endif /* HAVENOUTMP */
180 else if (eq(vp, STRimplicitcd)) {
181 implicit_cd = ((eq(varval(vp), STRverbose)) ? 2 : 1);
184 else if (eq(vp, STRcolor)) {
187 #endif /* COLOR_LS_F */
188 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
189 else if(eq(vp, CHECK_MBYTEVAR) || eq(vp, STRnokanji)) {
190 update_dspmbyte_vars();
194 else if (eq(vp, STRcatalog)) {
195 (void) catclose(catd);
198 #if defined(FILEC) && defined(TIOCSTI)
199 else if (eq(vp, STRfilec))
202 #endif /* NLS_CATALOGS */
217 int flags = VAR_READWRITE;
218 bool first_match = 0;
227 * Readonly addition From: Tim P. Starrin <noid@cyborg.larc.nasa.gov>
229 if (*v && eq(*v, STRmr)) {
230 flags = VAR_READONLY;
234 if (*v && eq(*v, STRmf) && !last_match) {
239 if (*v && eq(*v, STRml) && !first_match) {
247 plist(&shvhed, flags);
254 for (; alnum(*p); p++)
256 if (vp == p || !letter(*vp))
257 stderror(ERR_NAME | ERR_VARBEGIN);
258 if ((p - vp) > MAXVARLEN) {
259 stderror(ERR_NAME | ERR_VARTOOLONG);
264 p = getinx(p, &subscr);
266 if ((op = *p) != 0) {
268 if (*p == 0 && *v && **v == '(')
271 else if (*v && eq(*v, STRequal)) {
277 stderror(ERR_NAME | ERR_SYNTAX);
278 if (eq(p, STRLparen)) {
279 register Char **e = v;
282 stderror(ERR_NAME | ERR_SYNTAX);
285 stderror(ERR_NAME | ERR_MISSING, ')');
298 set1(vp, vecp, &shvhed, flags);
303 asx(vp, subscr, Strsave(p));
305 set(vp, Strsave(p), flags);
307 } while ((p = *v++) != NULL);
317 while (*cp && Isdigit(*cp))
318 *ip = *ip * 10 + *cp++ - '0';
320 stderror(ERR_NAME | ERR_SUBSCRIPT);
330 register struct varent *v = getvx(vp, subscr);
332 if (v->v_flags & VAR_READONLY)
333 stderror(ERR_READONLY|ERR_NAME, v->v_name);
334 xfree((ptr_t) v->vec[subscr - 1]);
335 v->vec[subscr - 1] = globone(p, G_APPEND);
338 static struct varent *
343 register struct varent *v = adrof(vp);
347 if (subscr < 1 || subscr > blklen(v->vec))
348 stderror(ERR_NAME | ERR_RANGE);
356 struct command *dummy;
374 for (; alnum(*p); p++)
376 if (vp == p || !letter(*vp))
377 stderror(ERR_NAME | ERR_VARBEGIN);
378 if ((p - vp) > MAXVARLEN)
379 stderror(ERR_NAME | ERR_VARTOOLONG);
382 p = getinx(p, &subscr);
389 stderror(ERR_NAME | ERR_ASSIGN);
392 * if there is no expression after the '=' then print a "Syntax Error"
395 if (*p == '\0' && *v == NULL)
396 stderror(ERR_NAME | ERR_ASSIGN);
407 stderror(ERR_NAME | ERR_UNKNOWNOP);
413 stderror(ERR_NAME | ERR_UNKNOWNOP);
415 stderror(ERR_NAME | ERR_SYNTAX);
418 stderror(ERR_NAME | ERR_UNKNOWNOP);
426 set(vp, p, VAR_READWRITE);
429 struct varent *gv = getvx(vp, subscr);
431 asx(vp, subscr, operate(op, gv->vec[subscr - 1], p));
434 set(vp, operate(op, varval(vp), p), VAR_READWRITE);
439 } while ((p = *v++) != NULL);
451 xfree((ptr_t) ** vp);
454 return (putn(expr(vp)));
464 register Char **v = vec;
474 if (op == '<' || op == '>')
481 stderror(ERR_NAME | ERR_EXPRESSION);
485 static Char *putp, nbuf[50];
498 num = 2; /* confuse lint */
499 if (sizeof(int) == num && ((unsigned int) n) == 0x8000) {
507 num = 4; /* confuse lint */
508 if (sizeof(int) == num && ((unsigned int) n) == 0x80000000) {
516 return (Strsave(nbuf));
525 *putp++ = n % 10 + '0';
535 if (!cp) /* PWP: extra error checking */
536 stderror(ERR_NAME | ERR_BADNUM);
539 if (cp[0] == '+' && cp[1])
545 stderror(ERR_NAME | ERR_BADNUM);
549 n = n * 10 + *cp++ - '0';
551 stderror(ERR_NAME | ERR_BADNUM);
552 return (sign ? -n : n);
560 register struct varent *vp;
562 if (!var || !head) /* PWP: extra error checking */
565 vp = adrof1(var, head);
566 return ((vp == NULL || vp->vec == NULL || vp->vec[0] == NULL) ?
567 STRNULL : vp->vec[0]);
570 static struct varent *
573 register struct varent *vp;
575 register struct varent *vp1;
577 for (vp = vp->v_left; vp; vp = vp->v_right) {
578 if (vp->v_left && (vp1 = madrof(pat, vp)) != NULL)
580 if (Gmatch(vp->v_name, pat))
589 register struct varent *v;
594 while (v && ((cmp = *name - *v->v_name) != 0 ||
595 (cmp = Strcmp(name, v->v_name)) != 0))
604 * The caller is responsible for putting value in a safe place
611 register Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
615 set1(var, vec, &shvhed, flags);
619 set1(var, vec, head, flags)
624 register Char **oldv = vec;
626 if ((flags & VAR_NOGLOB) == 0) {
633 stderror(ERR_NAME | ERR_NOMATCH);
641 * Uniqueness addition from: Michael Veksler <mveksler@vnet.ibm.com>
643 if ( flags & (VAR_FIRST | VAR_LAST) ) {
645 * Code for -f (VAR_FIRST) and -l (VAR_LAST) options.
647 * Delete all duplicate words leaving "holes" in the word array (vec).
648 * Then remove the "holes", keeping the order of the words unchanged.
650 if (vec && vec[0] && vec[1]) { /* more than one word ? */
654 for (num_items = 0; vec[num_items]; num_items++)
656 if (flags & VAR_FIRST) {
657 /* delete duplications, keeping first occurance */
658 for (i = 1; i < num_items; i++)
659 for (j = 0; j < i; j++)
660 /* If have earlier identical item, remove i'th item */
661 if (vec[i] && vec[j] && Strcmp(vec[j], vec[i]) == 0) {
666 } else if (flags & VAR_LAST) {
667 /* delete duplications, keeping last occurance */
668 for (i = 0; i < num_items - 1; i++)
669 for (j = i + 1; j < num_items; j++)
670 /* If have later identical item, remove i'th item */
671 if (vec[i] && vec[j] && Strcmp(vec[j], vec[i]) == 0) {
672 /* remove identical item (the first) */
677 /* Compress items - remove empty items */
678 for (j = i = 0; i < num_items; i++)
682 /* NULL-fy remaining items */
683 for (; j < num_items; j++)
686 /* don't let the attribute propagate */
687 flags &= ~(VAR_FIRST|VAR_LAST);
689 setq(var, vec, head, flags);
694 setq(name, vec, p, flags)
696 register struct varent *p;
699 register struct varent *c;
702 f = 0; /* tree hangs off the header's left link */
703 while ((c = p->v_link[f]) != 0) {
704 if ((f = *name - *c->v_name) == 0 &&
705 (f = Strcmp(name, c->v_name)) == 0) {
706 if (c->v_flags & VAR_READONLY)
707 stderror(ERR_READONLY|ERR_NAME, c->v_name);
716 p->v_link[f] = c = (struct varent *) xmalloc((size_t)sizeof(struct varent));
717 c->v_name = Strsave(name);
720 c->v_left = c->v_right = 0;
732 bool did_roe, did_edit;
735 did_roe = adrof(STRrecognize_only_executables) != NULL;
736 did_edit = adrof(STRedit) != NULL;
739 #if defined(FILEC) && defined(TIOCSTI)
740 if (adrof(STRfilec) == 0)
742 #endif /* FILEC && TIOCSTI */
744 if (adrof(STRhistchars) == 0) {
748 if (adrof(STRignoreeof) == 0)
750 if (adrof(STRpromptchars) == 0) {
754 if (adrof(STRhistlit) == 0)
756 if (adrof(STRloginsh) == 0)
758 if (adrof(STRwordchars) == 0)
759 word_chars = STR_WORD_CHARS;
760 if (adrof(STRedit) == 0)
762 if (adrof(STRbackslash_quote) == 0)
764 if (adrof(STRsymlinks) == 0)
766 if (adrof(STRimplicitcd) == 0)
768 if (adrof(STRkillring) == 0)
770 if (did_edit && noediting && adrof(STRedit) == 0)
772 if (did_roe && adrof(STRrecognize_only_executables) == 0)
775 if (adrof(STRcolor) == 0)
777 #endif /* COLOR_LS_F */
778 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
779 update_dspmbyte_vars();
782 (void) catclose(catd);
784 #endif /* NLS_CATALOGS */
792 register struct varent *vp;
797 while ((vp = madrof(*v, head)) != NULL)
798 if (vp->v_flags & VAR_READONLY)
799 stderror(ERR_READONLY|ERR_NAME, vp->v_name);
803 setname(short2str(*v));
811 register struct varent *vp;
813 if ((vp = adrof1(var, &shvhed)) == 0)
820 register struct varent *p;
822 register struct varent *c, *pp;
826 * Free associated memory first to avoid complications.
829 xfree((ptr_t) p->v_name);
831 * If p is missing one child, then we can move the other into where p is.
832 * Otherwise, we find the predecessor of p, which is guaranteed to have no
833 * right child, copy it into p, and move it's left child into it.
837 else if (p->v_left == 0)
840 for (c = p->v_left; c->v_right; c = c->v_right)
842 p->v_name = c->v_name;
843 p->v_flags = c->v_flags;
850 * Move c into where p is.
853 f = pp->v_right == p;
854 if ((pp->v_link[f] = c) != 0)
857 * Free the deleted node, and rebalance.
867 set(cp, Strsave(STRNULL), VAR_READWRITE);
876 register struct varent *argv;
887 if (argv == NULL || argv->vec == NULL)
889 if (argv->vec[0] == 0)
890 stderror(ERR_NAME | ERR_NOMORE);
891 lshift(argv->vec, 1);
895 static Char STRsep[2] = { PATHSEP, '\0' };
902 size_t exppath_size = BUFSIZE;
903 exppath = (Char *)xmalloc(sizeof(Char)*exppath_size);
908 while (Strlen(*val) + Strlen(exppath) + 2 > exppath_size) {
910 = (Char *)xrealloc(exppath, sizeof(Char)*(exppath_size *= 2)))
913 "Warning: ridiculously long PATH truncated\n"));
917 (void) Strcat(exppath, *val++);
918 if (*val == 0 || eq(*val, STRRparen))
920 (void) Strcat(exppath, STRsep);
922 tsetenv(STRKPATH, exppath);
928 * Lint thinks these have null effect
930 /* macros to do single rotations on node p */
931 # define rright(p) (\
933 (t)->v_parent = (p)->v_parent,\
934 (((p)->v_left = t->v_right) != NULL) ?\
935 (t->v_right->v_parent = (p)) : 0,\
936 (t->v_right = (p))->v_parent = t,\
940 ((t)->v_parent = (p)->v_parent,\
941 ((p)->v_right = t->v_left) != NULL) ? \
942 (t->v_left->v_parent = (p)) : 0,\
943 (t->v_left = (p))->v_parent = t,\
946 static struct varent *
952 static struct varent *
963 * Rebalance a tree, starting at p and up.
964 * F == 0 means we've come from p's left child.
965 * D == 1 means we've just done a delete, otherwise an insert.
969 register struct varent *p;
972 register struct varent *pp;
975 register struct varent *t; /* used by the rotate macros */
979 ff = 0; /* Sun's lint is dumb! */
983 * Ok, from here on, p is the node we're operating on; pp is it's parent; f
984 * is the branch of p from which we have come; ff is the branch of pp which
987 for (; (pp = p->v_parent) != 0; p = pp, f = ff) {
988 ff = pp->v_right == p;
989 if (f ^ d) { /* right heavy */
991 case -1: /* was left heavy */
994 case 0: /* was balanced */
997 case 1: /* was already right heavy */
998 switch (p->v_right->v_bal) {
999 case 1: /* sigle rotate */
1000 pp->v_link[ff] = rleft(p);
1001 p->v_left->v_bal = 0;
1004 case 0: /* single rotate */
1005 pp->v_link[ff] = rleft(p);
1006 p->v_left->v_bal = 1;
1009 case -1: /* double rotate */
1010 (void) rright(p->v_right);
1011 pp->v_link[ff] = rleft(p);
1013 p->v_bal < 1 ? 0 : -1;
1015 p->v_bal > -1 ? 0 : 1;
1026 else { /* left heavy */
1028 case 1: /* was right heavy */
1031 case 0: /* was balanced */
1034 case -1: /* was already left heavy */
1035 switch (p->v_left->v_bal) {
1036 case -1: /* single rotate */
1037 pp->v_link[ff] = rright(p);
1038 p->v_right->v_bal = 0;
1041 case 0: /* signle rotate */
1042 pp->v_link[ff] = rright(p);
1043 p->v_right->v_bal = -1;
1046 case 1: /* double rotate */
1047 (void) rleft(p->v_left);
1048 pp->v_link[ff] = rright(p);
1050 p->v_bal < 1 ? 0 : -1;
1052 p->v_bal > -1 ? 0 : 1;
1064 * If from insert, then we terminate when p is balanced. If from
1065 * delete, then we terminate when p is unbalanced.
1067 if ((p->v_bal == 0) ^ d)
1074 register struct varent *p;
1077 register struct varent *c;
1082 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
1083 #else /* !BSDSIGS */
1084 (void) sigrelse(SIGINT);
1085 #endif /* BSDSIGS */
1091 if (p->v_parent == 0) /* is it the header? */
1093 if ((p->v_flags & what) != 0) {
1094 len = blklen(p->vec);
1095 xprintf("%S\t", p->v_name);
1110 } while (p->v_right == c);
1115 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
1119 update_dspmbyte_vars()
1125 /* if variable "nokanji" is set, multi-byte display is disabled */
1126 if ((vp = adrof(CHECK_MBYTEVAR)) && !adrof(STRnokanji)) {
1129 if(eq (dstr1, STRKSJIS))
1131 else if (eq(dstr1, STRKEUC))
1133 else if (eq(dstr1, STRKBIG5))
1135 else if (eq(dstr1, STRKUTF8))
1137 else if ((dstr1[0] - '0') >= 0 && (dstr1[0] - '0') <= 3) {
1141 xprintf(CGETS(18, 2,
1142 "Warning: unknown multibyte display; using default(euc(JP))\n"));
1145 if (dstr1 && vp->vec[1] && eq(vp->vec[1], STRls))
1149 for (lp = 0; lp < 256 && iskcode > 0; lp++) {
1153 _cmap[lp] = _cmap_mbyte[lp];
1154 _mbmap[lp] = _mbmap_sjis[lp];
1158 _cmap[lp] = _cmap_mbyte[lp];
1159 _mbmap[lp] = _mbmap_euc[lp];
1163 _cmap[lp] = _cmap_mbyte[lp];
1164 _mbmap[lp] = _mbmap_big5[lp];
1168 _cmap[lp] = _cmap_mbyte[lp];
1169 _mbmap[lp] = _mbmap_utf8[lp];
1172 xprintf(CGETS(18, 3,
1173 "Warning: unknown multibyte code %d; multibyte disabled\n"),
1175 _cmap[lp] = _cmap_c[lp];
1176 _mbmap[lp] = 0; /* Default map all 0 */
1182 /* check original table */
1183 if (Strlen(dstr1) != 256) {
1184 xprintf(CGETS(18, 4,
1185 "Warning: Invalid multibyte table length (%d); multibyte disabled\n"),
1189 for (lp = 0; lp < 256 && _enable_mbdisp == 1; lp++) {
1190 if (!((dstr1[lp] - '0') >= 0 && (dstr1[lp] - '0') <= 3)) {
1191 xprintf(CGETS(18, 4,
1192 "Warning: bad multibyte code at offset +%d; multibyte diabled\n"),
1198 /* set original table */
1199 for (lp = 0; lp < 256; lp++) {
1200 if (_enable_mbdisp == 1) {
1201 _cmap[lp] = _cmap_mbyte[lp];
1202 _mbmap[lp] = (unsigned short) ((dstr1[lp] - '0') & 0x0f);
1205 _cmap[lp] = _cmap_c[lp];
1206 _mbmap[lp] = 0; /* Default map all 0 */
1212 for (lp = 0; lp < 256; lp++) {
1213 _cmap[lp] = _cmap_c[lp];
1214 _mbmap[lp] = 0; /* Default map all 0 */
1219 #ifdef MBYTEDEBUG /* Sorry, use for beta testing */
1222 for (lp = 0; lp < 256; lp++) {
1223 mbmapstr[lp] = _mbmap[lp] + '0';
1226 set(STRmbytemap, Strsave(mbmapstr), VAR_READWRITE);
1228 #endif /* MBYTEMAP */
1231 /* dspkanji/dspmbyte autosetting */
1232 /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
1234 autoset_dspmbyte(pcp)
1238 struct dspm_autoset_Table {
1242 { STRLANGEUCJP, STRKEUC },
1243 { STRLANGEUCKR, STRKEUC },
1244 { STRLANGEUCZH, STRKEUC },
1245 { STRLANGEUCJPB, STRKEUC },
1246 { STRLANGEUCKRB, STRKEUC },
1247 { STRLANGEUCZHB, STRKEUC },
1249 { STRLANGEUCJPC, STRKEUC },
1251 { STRLANGSJIS, STRKSJIS },
1252 { STRLANGSJISB, STRKSJIS },
1253 { STRLANGBIG5, STRKBIG5 },
1254 { STRSTARKUTF8, STRKUTF8 },
1261 for (i = 0; dspmt[i].n; i++) {
1263 if (t_pmatch(pcp, dspmt[i].n, &estr, 1) > 0) {
1264 set(CHECK_MBYTEVAR, Strsave(dspmt[i].v), VAR_READWRITE);
1265 update_dspmbyte_vars();