]> git.saurik.com Git - apple/shell_cmds.git/blob - window/var.c
8fddabc7c1fe2ae746fd117ddf00d364aa9380da
[apple/shell_cmds.git] / window / var.c
1 /* $NetBSD: var.c,v 1.5 1997/11/21 08:36:45 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)var.c 8.1 (Berkeley) 6/6/93";
43 #else
44 __RCSID("$NetBSD: var.c,v 1.5 1997/11/21 08:36:45 lukem Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <stdlib.h>
49 #include "value.h"
50 #define EXTERN
51 #include "var.h"
52 #undef EXTERN
53 #include "window_string.h"
54
55 struct var *
56 var_set1(head, name, v)
57 struct var **head;
58 char *name;
59 struct value *v;
60 {
61 struct var **p;
62 struct var *r;
63 struct value val;
64
65 /* do this first, easier to recover */
66 val = *v;
67 if (val.v_type == V_STR && val.v_str != 0 &&
68 (val.v_str = str_cpy(val.v_str)) == 0)
69 return 0;
70 if (*(p = var_lookup1(head, name)) == 0) {
71 r = (struct var *) malloc(sizeof (struct var));
72 if (r == 0) {
73 val_free(val);
74 return 0;
75 }
76 if ((r->r_name = str_cpy(name)) == 0) {
77 val_free(val);
78 free((char *) r);
79 return 0;
80 }
81 r->r_left = r->r_right = 0;
82 *p = r;
83 } else {
84 r = *p;
85 val_free(r->r_val);
86 }
87 r->r_val = val;
88 return r;
89 }
90
91 struct var *
92 var_setstr1(head, name, str)
93 struct var **head;
94 char *name;
95 char *str;
96 {
97 struct value v;
98
99 v.v_type = V_STR;
100 v.v_str = str;
101 return var_set1(head, name, &v);
102 }
103
104 struct var *
105 var_setnum1(head, name, num)
106 struct var **head;
107 char *name;
108 int num;
109 {
110 struct value v;
111
112 v.v_type = V_NUM;
113 v.v_num = num;
114 return var_set1(head, name, &v);
115 }
116
117 int
118 var_unset1(head, name)
119 struct var **head;
120 char *name;
121 {
122 struct var **p;
123 struct var *r;
124
125 if (*(p = var_lookup1(head, name)) == 0)
126 return -1;
127 r = *p;
128 *p = r->r_left;
129 while (*p != 0)
130 p = &(*p)->r_right;
131 *p = r->r_right;
132 val_free(r->r_val);
133 str_free(r->r_name);
134 free((char *) r);
135 return 0;
136 }
137
138 struct var **
139 var_lookup1(p, name)
140 struct var **p;
141 char *name;
142 {
143 int cmp;
144
145 while (*p != 0) {
146 if ((cmp = strcmp(name, (*p)->r_name)) < 0)
147 p = &(*p)->r_left;
148 else if (cmp > 0)
149 p = &(*p)->r_right;
150 else
151 break;
152 }
153 return p;
154 }
155
156 int
157 var_walk1(r, func, a)
158 struct var *r;
159 int (*func) __P((void *, struct var *));
160 void *a;
161 {
162 if (r == 0)
163 return 0;
164 if (var_walk1(r->r_left, func, a) < 0 || (*func)(a, r) < 0
165 || var_walk1(r->r_right, func, a) < 0)
166 return -1;
167 return 0;
168 }