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