|
|
1.1 root 1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved.
4: *
5: * This code is derived from software contributed to Berkeley by
6: * Edward Wang at The University of California, Berkeley.
7: *
8: * Redistribution and use in source and binary forms are permitted provided
9: * that: (1) source distributions retain this entire copyright notice and
10: * comment, and (2) distributions including binaries display the following
11: * acknowledgement: ``This product includes software developed by the
12: * University of California, Berkeley and its contributors'' in the
13: * documentation or other materials provided with the distribution and in
14: * all advertising materials mentioning features or use of this software.
15: * Neither the name of the University nor the names of its contributors may
16: * be used to endorse or promote products derived from this software without
17: * specific prior written permission.
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21: */
22:
23: #ifndef lint
24: static char sccsid[] = "@(#)var.c 3.12 (Berkeley) 6/6/90";
25: #endif /* not lint */
26:
27: #include "value.h"
28: #include "var.h"
29: #include "string.h"
30:
31: char *malloc();
32:
33: struct var *
34: var_set1(head, name, v)
35: struct var **head;
36: char *name;
37: struct value *v;
38: {
39: register struct var **p;
40: register struct var *r;
41: struct value val;
42:
43: /* do this first, easier to recover */
44: val = *v;
45: if (val.v_type == V_STR && val.v_str != 0 &&
46: (val.v_str = str_cpy(val.v_str)) == 0)
47: return 0;
48: if (*(p = var_lookup1(head, name)) == 0) {
49: r = (struct var *) malloc(sizeof (struct var));
50: if (r == 0) {
51: val_free(val);
52: return 0;
53: }
54: if ((r->r_name = str_cpy(name)) == 0) {
55: val_free(val);
56: free((char *) r);
57: return 0;
58: }
59: r->r_left = r->r_right = 0;
60: *p = r;
61: } else {
62: r = *p;
63: val_free(r->r_val);
64: }
65: r->r_val = val;
66: return r;
67: }
68:
69: struct var *
70: var_setstr1(head, name, str)
71: struct var **head;
72: char *name;
73: char *str;
74: {
75: struct value v;
76:
77: v.v_type = V_STR;
78: v.v_str = str;
79: return var_set1(head, name, &v);
80: }
81:
82: struct var *
83: var_setnum1(head, name, num)
84: struct var **head;
85: char *name;
86: int num;
87: {
88: struct value v;
89:
90: v.v_type = V_NUM;
91: v.v_num = num;
92: return var_set1(head, name, &v);
93: }
94:
95: var_unset1(head, name)
96: struct var **head;
97: char *name;
98: {
99: register struct var **p;
100: register struct var *r;
101:
102: if (*(p = var_lookup1(head, name)) == 0)
103: return -1;
104: r = *p;
105: *p = r->r_left;
106: while (*p != 0)
107: p = &(*p)->r_right;
108: *p = r->r_right;
109: val_free(r->r_val);
110: str_free(r->r_name);
111: free((char *) r);
112: return 0;
113: }
114:
115: struct var **
116: var_lookup1(p, name)
117: register struct var **p;
118: register char *name;
119: {
120: register cmp;
121:
122: while (*p != 0) {
123: if ((cmp = strcmp(name, (*p)->r_name)) < 0)
124: p = &(*p)->r_left;
125: else if (cmp > 0)
126: p = &(*p)->r_right;
127: else
128: break;
129: }
130: return p;
131: }
132:
133: var_walk1(r, func, a)
134: register struct var *r;
135: int (*func)();
136: {
137: if (r == 0)
138: return 0;
139: if (var_walk1(r->r_left, func, a) < 0 || (*func)(a, r) < 0
140: || var_walk1(r->r_right, func, a) < 0)
141: return -1;
142: return 0;
143: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.