checkpoint of hacking for mail.cs.berkeley.edu
[unix-history] / usr / src / usr.bin / window / var.c
CommitLineData
60de5df9 1/*
46e9ea25
KB
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
3dd3a9e5
KB
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
7 *
87f529ec 8 * %sccs.include.redist.c%
60de5df9
EW
9 */
10
46e9ea25 11#ifndef lint
3dd3a9e5 12static char sccsid[] = "@(#)var.c 3.12 (Berkeley) %G%";
46e9ea25
KB
13#endif /* not lint */
14
4ae9aa77
EW
15#include "value.h"
16#include "var.h"
17#include "string.h"
18
19char *malloc();
20
21struct var *
bb4a0c0b
EW
22var_set1(head, name, v)
23struct var **head;
4ae9aa77
EW
24char *name;
25struct value *v;
26{
b44eb60a 27 register struct var **p;
4ae9aa77 28 register struct var *r;
b44eb60a 29 struct value val;
4ae9aa77 30
b44eb60a
EW
31 /* do this first, easier to recover */
32 val = *v;
855d0f8f
EW
33 if (val.v_type == V_STR && val.v_str != 0 &&
34 (val.v_str = str_cpy(val.v_str)) == 0)
b44eb60a 35 return 0;
bb4a0c0b 36 if (*(p = var_lookup1(head, name)) == 0) {
4ae9aa77 37 r = (struct var *) malloc(sizeof (struct var));
b44eb60a
EW
38 if (r == 0) {
39 val_free(val);
4ae9aa77
EW
40 return 0;
41 }
b44eb60a
EW
42 if ((r->r_name = str_cpy(name)) == 0) {
43 val_free(val);
4ae9aa77 44 free((char *) r);
4ae9aa77
EW
45 return 0;
46 }
b44eb60a
EW
47 r->r_left = r->r_right = 0;
48 *p = r;
49 } else {
50 r = *p;
51 val_free(r->r_val);
4ae9aa77 52 }
b44eb60a 53 r->r_val = val;
4ae9aa77
EW
54 return r;
55}
56
a21d3dfe 57struct var *
bb4a0c0b
EW
58var_setstr1(head, name, str)
59struct var **head;
a21d3dfe
EW
60char *name;
61char *str;
62{
63 struct value v;
64
65 v.v_type = V_STR;
66 v.v_str = str;
bb4a0c0b 67 return var_set1(head, name, &v);
a21d3dfe
EW
68}
69
70struct var *
bb4a0c0b
EW
71var_setnum1(head, name, num)
72struct var **head;
a21d3dfe
EW
73char *name;
74int num;
75{
76 struct value v;
77
78 v.v_type = V_NUM;
79 v.v_num = num;
bb4a0c0b 80 return var_set1(head, name, &v);
a21d3dfe
EW
81}
82
bb4a0c0b
EW
83var_unset1(head, name)
84struct var **head;
4ae9aa77
EW
85char *name;
86{
b44eb60a 87 register struct var **p;
4ae9aa77 88 register struct var *r;
b44eb60a 89
bb4a0c0b 90 if (*(p = var_lookup1(head, name)) == 0)
b44eb60a
EW
91 return -1;
92 r = *p;
93 *p = r->r_left;
94 while (*p != 0)
95 p = &(*p)->r_right;
96 *p = r->r_right;
97 val_free(r->r_val);
98 str_free(r->r_name);
99 free((char *) r);
100 return 0;
101}
102
103struct var **
bb4a0c0b
EW
104var_lookup1(p, name)
105register struct var **p;
b44eb60a
EW
106register char *name;
107{
4ae9aa77
EW
108 register cmp;
109
bb4a0c0b 110 while (*p != 0) {
b44eb60a
EW
111 if ((cmp = strcmp(name, (*p)->r_name)) < 0)
112 p = &(*p)->r_left;
4ae9aa77 113 else if (cmp > 0)
b44eb60a 114 p = &(*p)->r_right;
4ae9aa77
EW
115 else
116 break;
117 }
b44eb60a 118 return p;
4ae9aa77
EW
119}
120
e1d25671 121var_walk1(r, func, a)
4ae9aa77
EW
122register struct var *r;
123int (*func)();
124{
125 if (r == 0)
e1d25671
EW
126 return 0;
127 if (var_walk1(r->r_left, func, a) < 0 || (*func)(a, r) < 0
128 || var_walk1(r->r_right, func, a) < 0)
129 return -1;
130 return 0;
4ae9aa77 131}