1 /* $Header: /src/pub/tcsh/sh.set.c,v 3.37 2000/07/15 19:58:51 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 RCSID("$Id: sh.set.c,v 3.37 2000/07/15 19:58:51 christos Exp $")
45 extern bool 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 register Char *pn = varval(vp);
77 else if (eq(vp, STRpromptchars)) {
78 register 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 register 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, STRbackslash_quote)) {
157 else if (eq(vp, STRdirstack)) {
160 else if (eq(vp, STRrecognize_only_executables)) {
164 else if (eq(vp, STRwatch)) {
167 #endif /* HAVENOUTMP */
168 else if (eq(vp, STRimplicitcd)) {
169 implicit_cd = ((eq(varval(vp), STRverbose)) ? 2 : 1);
172 else if (eq(vp, STRcolor)) {
175 #endif /* COLOR_LS_F */
176 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
177 else if(eq(vp, CHECK_MBYTEVAR) || eq(vp, STRnokanji)) {
178 update_dspmbyte_vars();
182 else if (eq(vp, STRcatalog)) {
183 (void) catclose(catd);
186 #endif /* NLS_CATALOGS */
201 int flags = VAR_READWRITE;
202 bool first_match = 0;
211 * Readonly addition From: Tim P. Starrin <noid@cyborg.larc.nasa.gov>
213 if (*v && eq(*v, STRmr)) {
214 flags = VAR_READONLY;
218 if (*v && eq(*v, STRmf) && !last_match) {
223 if (*v && eq(*v, STRml) && !first_match) {
231 plist(&shvhed, flags);
238 for (; alnum(*p); p++)
240 if (vp == p || !letter(*vp))
241 stderror(ERR_NAME | ERR_VARBEGIN);
242 if ((p - vp) > MAXVARLEN) {
243 stderror(ERR_NAME | ERR_VARTOOLONG);
248 p = getinx(p, &subscr);
250 if ((op = *p) != 0) {
252 if (*p == 0 && *v && **v == '(')
255 else if (*v && eq(*v, STRequal)) {
261 stderror(ERR_NAME | ERR_SYNTAX);
262 if (eq(p, STRLparen)) {
263 register Char **e = v;
266 stderror(ERR_NAME | ERR_SYNTAX);
269 stderror(ERR_NAME | ERR_MISSING, ')');
282 set1(vp, vecp, &shvhed, flags);
287 asx(vp, subscr, Strsave(p));
289 set(vp, Strsave(p), flags);
291 } while ((p = *v++) != NULL);
301 while (*cp && Isdigit(*cp))
302 *ip = *ip * 10 + *cp++ - '0';
304 stderror(ERR_NAME | ERR_SUBSCRIPT);
314 register struct varent *v = getvx(vp, subscr);
316 if (v->v_flags & VAR_READONLY)
317 stderror(ERR_READONLY|ERR_NAME, v->v_name);
318 xfree((ptr_t) v->vec[subscr - 1]);
319 v->vec[subscr - 1] = globone(p, G_APPEND);
322 static struct varent *
327 register struct varent *v = adrof(vp);
331 if (subscr < 1 || subscr > blklen(v->vec))
332 stderror(ERR_NAME | ERR_RANGE);
340 struct command *dummy;
358 for (; alnum(*p); p++)
360 if (vp == p || !letter(*vp))
361 stderror(ERR_NAME | ERR_VARBEGIN);
362 if ((p - vp) > MAXVARLEN)
363 stderror(ERR_NAME | ERR_VARTOOLONG);
366 p = getinx(p, &subscr);
373 stderror(ERR_NAME | ERR_ASSIGN);
376 * if there is no expression after the '=' then print a "Syntax Error"
379 if (*p == '\0' && *v == NULL)
380 stderror(ERR_NAME | ERR_ASSIGN);
391 stderror(ERR_NAME | ERR_UNKNOWNOP);
397 stderror(ERR_NAME | ERR_UNKNOWNOP);
399 stderror(ERR_NAME | ERR_SYNTAX);
402 stderror(ERR_NAME | ERR_UNKNOWNOP);
410 set(vp, p, VAR_READWRITE);
413 struct varent *gv = getvx(vp, subscr);
415 asx(vp, subscr, operate(op, gv->vec[subscr - 1], p));
418 set(vp, operate(op, varval(vp), p), VAR_READWRITE);
423 } while ((p = *v++) != NULL);
435 xfree((ptr_t) ** vp);
438 return (putn(expr(vp)));
448 register Char **v = vec;
458 if (op == '<' || op == '>')
465 stderror(ERR_NAME | ERR_EXPRESSION);
469 static Char *putp, nbuf[50];
482 num = 2; /* confuse lint */
483 if (sizeof(int) == num && ((unsigned int) n) == 0x8000) {
491 num = 4; /* confuse lint */
492 if (sizeof(int) == num && ((unsigned int) n) == 0x80000000) {
500 return (Strsave(nbuf));
509 *putp++ = n % 10 + '0';
519 if (!cp) /* PWP: extra error checking */
520 stderror(ERR_NAME | ERR_BADNUM);
523 if (cp[0] == '+' && cp[1])
529 stderror(ERR_NAME | ERR_BADNUM);
533 n = n * 10 + *cp++ - '0';
535 stderror(ERR_NAME | ERR_BADNUM);
536 return (sign ? -n : n);
544 register struct varent *vp;
546 if (!var || !head) /* PWP: extra error checking */
549 vp = adrof1(var, head);
550 return (vp == 0 || vp->vec[0] == 0 ? STRNULL : vp->vec[0]);
553 static struct varent *
556 register struct varent *vp;
558 register struct varent *vp1;
560 for (vp = vp->v_left; vp; vp = vp->v_right) {
561 if (vp->v_left && (vp1 = madrof(pat, vp)) != NULL)
563 if (Gmatch(vp->v_name, pat))
572 register struct varent *v;
577 while (v && ((cmp = *name - *v->v_name) != 0 ||
578 (cmp = Strcmp(name, v->v_name)) != 0))
587 * The caller is responsible for putting value in a safe place
594 register Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
598 set1(var, vec, &shvhed, flags);
602 set1(var, vec, head, flags)
607 register Char **oldv = vec;
609 if ((flags & VAR_NOGLOB) == 0) {
616 stderror(ERR_NAME | ERR_NOMATCH);
624 * Uniqueness addition from: Michael Veksler <mveksler@vnet.ibm.com>
626 if ( flags & (VAR_FIRST | VAR_LAST) ) {
628 * Code for -f (VAR_FIRST) and -l (VAR_LAST) options.
630 * Delete all duplicate words leaving "holes" in the word array (vec).
631 * Then remove the "holes", keeping the order of the words unchanged.
633 if (vec && vec[0] && vec[1]) { /* more than one word ? */
637 for (num_items = 0; vec[num_items]; num_items++)
639 if (flags & VAR_FIRST) {
640 /* delete duplications, keeping first occurance */
641 for (i = 1; i < num_items; i++)
642 for (j = 0; j < i; j++)
643 /* If have earlier identical item, remove i'th item */
644 if (vec[i] && vec[j] && Strcmp(vec[j], vec[i]) == 0) {
649 } else if (flags & VAR_LAST) {
650 /* delete duplications, keeping last occurance */
651 for (i = 0; i < num_items - 1; i++)
652 for (j = i + 1; j < num_items; j++)
653 /* If have later identical item, remove i'th item */
654 if (vec[i] && vec[j] && Strcmp(vec[j], vec[i]) == 0) {
655 /* remove identical item (the first) */
660 /* Compress items - remove empty items */
661 for (j = i = 0; i < num_items; i++)
665 /* NULL-fy remaining items */
666 for (; j < num_items; j++)
669 /* don't let the attribute propagate */
670 flags &= ~(VAR_FIRST|VAR_LAST);
672 setq(var, vec, head, flags);
677 setq(name, vec, p, flags)
679 register struct varent *p;
682 register struct varent *c;
685 f = 0; /* tree hangs off the header's left link */
686 while ((c = p->v_link[f]) != 0) {
687 if ((f = *name - *c->v_name) == 0 &&
688 (f = Strcmp(name, c->v_name)) == 0) {
689 if (c->v_flags & VAR_READONLY)
690 stderror(ERR_READONLY|ERR_NAME, c->v_name);
699 p->v_link[f] = c = (struct varent *) xmalloc((size_t)sizeof(struct varent));
700 c->v_name = Strsave(name);
703 c->v_left = c->v_right = 0;
715 bool did_roe, did_edit;
718 did_roe = adrof(STRrecognize_only_executables) != NULL;
719 did_edit = adrof(STRedit) != NULL;
721 if (adrof(STRhistchars) == 0) {
725 if (adrof(STRpromptchars) == 0) {
729 if (adrof(STRhistlit) == 0)
731 if (adrof(STRloginsh) == 0)
733 if (adrof(STRwordchars) == 0)
734 word_chars = STR_WORD_CHARS;
735 if (adrof(STRedit) == 0)
737 if (adrof(STRbackslash_quote) == 0)
739 if (adrof(STRsymlinks) == 0)
741 if (adrof(STRimplicitcd) == 0)
743 if (did_edit && noediting && adrof(STRedit) == 0)
745 if (did_roe && adrof(STRrecognize_only_executables) == 0)
748 if (adrof(STRcolor) == 0)
750 #endif /* COLOR_LS_F */
751 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
752 update_dspmbyte_vars();
755 (void) catclose(catd);
757 #endif /* NLS_CATALOGS */
765 register struct varent *vp;
770 while ((vp = madrof(*v, head)) != NULL)
771 if (vp->v_flags & VAR_READONLY)
772 stderror(ERR_READONLY|ERR_NAME, vp->v_name);
776 setname(short2str(*v));
784 register struct varent *vp;
786 if ((vp = adrof1(var, &shvhed)) == 0)
793 register struct varent *p;
795 register struct varent *c, *pp;
799 * Free associated memory first to avoid complications.
802 xfree((ptr_t) p->v_name);
804 * If p is missing one child, then we can move the other into where p is.
805 * Otherwise, we find the predecessor of p, which is guaranteed to have no
806 * right child, copy it into p, and move it's left child into it.
810 else if (p->v_left == 0)
813 for (c = p->v_left; c->v_right; c = c->v_right)
815 p->v_name = c->v_name;
816 p->v_flags = c->v_flags;
823 * Move c into where p is.
826 f = pp->v_right == p;
827 if ((pp->v_link[f] = c) != 0)
830 * Free the deleted node, and rebalance.
840 set(cp, Strsave(STRNULL), VAR_READWRITE);
849 register struct varent *argv;
862 if (argv->vec[0] == 0)
863 stderror(ERR_NAME | ERR_NOMORE);
864 lshift(argv->vec, 1);
868 static Char STRsep[2] = { PATHSEP, '\0' };
875 size_t exppath_size = BUFSIZE;
876 exppath = (Char *)xmalloc(sizeof(Char)*exppath_size);
881 while (Strlen(*val) + Strlen(exppath) + 2 > exppath_size) {
883 = (Char *)xrealloc(exppath, sizeof(Char)*(exppath_size *= 2)))
886 "Warning: ridiculously long PATH truncated\n"));
890 (void) Strcat(exppath, *val++);
891 if (*val == 0 || eq(*val, STRRparen))
893 (void) Strcat(exppath, STRsep);
895 tsetenv(STRKPATH, exppath);
901 * Lint thinks these have null effect
903 /* macros to do single rotations on node p */
904 # define rright(p) (\
906 (t)->v_parent = (p)->v_parent,\
907 (((p)->v_left = t->v_right) != NULL) ?\
908 (t->v_right->v_parent = (p)) : 0,\
909 (t->v_right = (p))->v_parent = t,\
913 ((t)->v_parent = (p)->v_parent,\
914 ((p)->v_right = t->v_left) != NULL) ? \
915 (t->v_left->v_parent = (p)) : 0,\
916 (t->v_left = (p))->v_parent = t,\
919 static struct varent *
925 static struct varent *
936 * Rebalance a tree, starting at p and up.
937 * F == 0 means we've come from p's left child.
938 * D == 1 means we've just done a delete, otherwise an insert.
942 register struct varent *p;
945 register struct varent *pp;
948 register struct varent *t; /* used by the rotate macros */
952 ff = 0; /* Sun's lint is dumb! */
956 * Ok, from here on, p is the node we're operating on; pp is it's parent; f
957 * is the branch of p from which we have come; ff is the branch of pp which
960 for (; (pp = p->v_parent) != 0; p = pp, f = ff) {
961 ff = pp->v_right == p;
962 if (f ^ d) { /* right heavy */
964 case -1: /* was left heavy */
967 case 0: /* was balanced */
970 case 1: /* was already right heavy */
971 switch (p->v_right->v_bal) {
972 case 1: /* sigle rotate */
973 pp->v_link[ff] = rleft(p);
974 p->v_left->v_bal = 0;
977 case 0: /* single rotate */
978 pp->v_link[ff] = rleft(p);
979 p->v_left->v_bal = 1;
982 case -1: /* double rotate */
983 (void) rright(p->v_right);
984 pp->v_link[ff] = rleft(p);
986 p->v_bal < 1 ? 0 : -1;
988 p->v_bal > -1 ? 0 : 1;
999 else { /* left heavy */
1001 case 1: /* was right heavy */
1004 case 0: /* was balanced */
1007 case -1: /* was already left heavy */
1008 switch (p->v_left->v_bal) {
1009 case -1: /* single rotate */
1010 pp->v_link[ff] = rright(p);
1011 p->v_right->v_bal = 0;
1014 case 0: /* signle rotate */
1015 pp->v_link[ff] = rright(p);
1016 p->v_right->v_bal = -1;
1019 case 1: /* double rotate */
1020 (void) rleft(p->v_left);
1021 pp->v_link[ff] = rright(p);
1023 p->v_bal < 1 ? 0 : -1;
1025 p->v_bal > -1 ? 0 : 1;
1037 * If from insert, then we terminate when p is balanced. If from
1038 * delete, then we terminate when p is unbalanced.
1040 if ((p->v_bal == 0) ^ d)
1047 register struct varent *p;
1050 register struct varent *c;
1055 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
1056 #else /* !BSDSIGS */
1057 (void) sigrelse(SIGINT);
1058 #endif /* BSDSIGS */
1064 if (p->v_parent == 0) /* is it the header? */
1066 if ((p->v_flags & what) != 0) {
1067 len = blklen(p->vec);
1068 xprintf("%S\t", p->v_name);
1083 } while (p->v_right == c);
1088 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
1092 update_dspmbyte_vars()
1098 /* if variable "nokanji" is set, multi-byte display is disabled */
1099 if ((vp = adrof(CHECK_MBYTEVAR)) && !adrof(STRnokanji)) {
1102 if(eq (dstr1, STRKSJIS))
1104 else if (eq(dstr1, STRKEUC))
1106 else if ((dstr1[0] - '0') >= 0 && (dstr1[0] - '0') <= 3) {
1110 xprintf(CGETS(18, 2,
1111 "Warning: unknown multibyte display; using default(euc(JP))\n"));
1114 if (dstr1 && vp->vec[1] && eq(vp->vec[1], STRls))
1118 for (lp = 0; lp < 256 && iskcode > 0; lp++) {
1122 _cmap[lp] = _cmap_mbyte[lp];
1123 _mbmap[lp] = _mbmap_sjis[lp];
1127 _cmap[lp] = _cmap_mbyte[lp];
1128 _mbmap[lp] = _mbmap_euc[lp];
1131 xprintf(CGETS(18, 3,
1132 "Warning: unknown multibyte code %d; multibyte disabled\n"),
1134 _cmap[lp] = _cmap_c[lp];
1135 _mbmap[lp] = 0; /* Default map all 0 */
1141 /* check original table */
1142 if (Strlen(dstr1) != 256) {
1143 xprintf(CGETS(18, 4,
1144 "Warning: Invalid multibyte table length (%d); multibyte disabled\n"),
1148 for (lp = 0; lp < 256 && _enable_mbdisp == 1; lp++) {
1149 if (!((dstr1[lp] - '0') >= 0 && (dstr1[lp] - '0') <= 3)) {
1150 xprintf(CGETS(18, 4,
1151 "Warning: bad multibyte code at offset +%d; multibyte diabled\n"),
1157 /* set original table */
1158 for (lp = 0; lp < 256; lp++) {
1159 if (_enable_mbdisp == 1) {
1160 _cmap[lp] = _cmap_mbyte[lp];
1161 _mbmap[lp] = (unsigned short) ((dstr1[lp] - '0') & 0x0f);
1164 _cmap[lp] = _cmap_c[lp];
1165 _mbmap[lp] = 0; /* Default map all 0 */
1171 for (lp = 0; lp < 256; lp++) {
1172 _cmap[lp] = _cmap_c[lp];
1173 _mbmap[lp] = 0; /* Default map all 0 */
1178 #ifdef MBYTEDEBUG /* Sorry, use for beta testing */
1181 for (lp = 0; lp < 256; lp++) {
1182 mbmapstr[lp] = _mbmap[lp] + '0';
1185 set(STRmbytemap, Strsave(mbmapstr), VAR_READWRITE);
1187 #endif /* MBYTEMAP */
1190 /* dspkanji/dspmbyte autosetting */
1191 /* PATCH IDEA FROM Issei.Suzuki VERY THANKS */
1193 autoset_dspmbyte(pcp)
1197 struct dspm_autoset_Table {
1201 { STRLANGEUCJP, STRKEUC },
1202 { STRLANGEUCKR, STRKEUC },
1203 { STRLANGEUCJPB, STRKEUC },
1204 { STRLANGEUCKRB, STRKEUC },
1205 { STRLANGSJIS, STRKSJIS },
1206 { STRLANGSJISB, STRKSJIS },
1213 for (i = 0; dspmt[i].n; i++) {
1214 if (eq(pcp, dspmt[i].n)) {
1215 set(CHECK_MBYTEVAR, Strsave(dspmt[i].v), VAR_READWRITE);
1216 update_dspmbyte_vars();