less -> more
[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 *
5 * Redistribution and use in source and binary forms are permitted
5e8b0e60
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
60de5df9
EW
16 */
17
46e9ea25 18#ifndef lint
5e8b0e60 19static char sccsid[] = "@(#)var.c 3.10 (Berkeley) %G%";
46e9ea25
KB
20#endif /* not lint */
21
4ae9aa77
EW
22#include "value.h"
23#include "var.h"
24#include "string.h"
25
26char *malloc();
27
28struct var *
bb4a0c0b
EW
29var_set1(head, name, v)
30struct var **head;
4ae9aa77
EW
31char *name;
32struct value *v;
33{
b44eb60a 34 register struct var **p;
4ae9aa77 35 register struct var *r;
b44eb60a 36 struct value val;
4ae9aa77 37
b44eb60a
EW
38 /* do this first, easier to recover */
39 val = *v;
855d0f8f
EW
40 if (val.v_type == V_STR && val.v_str != 0 &&
41 (val.v_str = str_cpy(val.v_str)) == 0)
b44eb60a 42 return 0;
bb4a0c0b 43 if (*(p = var_lookup1(head, name)) == 0) {
4ae9aa77 44 r = (struct var *) malloc(sizeof (struct var));
b44eb60a
EW
45 if (r == 0) {
46 val_free(val);
4ae9aa77
EW
47 return 0;
48 }
b44eb60a
EW
49 if ((r->r_name = str_cpy(name)) == 0) {
50 val_free(val);
4ae9aa77 51 free((char *) r);
4ae9aa77
EW
52 return 0;
53 }
b44eb60a
EW
54 r->r_left = r->r_right = 0;
55 *p = r;
56 } else {
57 r = *p;
58 val_free(r->r_val);
4ae9aa77 59 }
b44eb60a 60 r->r_val = val;
4ae9aa77
EW
61 return r;
62}
63
a21d3dfe 64struct var *
bb4a0c0b
EW
65var_setstr1(head, name, str)
66struct var **head;
a21d3dfe
EW
67char *name;
68char *str;
69{
70 struct value v;
71
72 v.v_type = V_STR;
73 v.v_str = str;
bb4a0c0b 74 return var_set1(head, name, &v);
a21d3dfe
EW
75}
76
77struct var *
bb4a0c0b
EW
78var_setnum1(head, name, num)
79struct var **head;
a21d3dfe
EW
80char *name;
81int num;
82{
83 struct value v;
84
85 v.v_type = V_NUM;
86 v.v_num = num;
bb4a0c0b 87 return var_set1(head, name, &v);
a21d3dfe
EW
88}
89
bb4a0c0b
EW
90var_unset1(head, name)
91struct var **head;
4ae9aa77
EW
92char *name;
93{
b44eb60a 94 register struct var **p;
4ae9aa77 95 register struct var *r;
b44eb60a 96
bb4a0c0b 97 if (*(p = var_lookup1(head, name)) == 0)
b44eb60a
EW
98 return -1;
99 r = *p;
100 *p = r->r_left;
101 while (*p != 0)
102 p = &(*p)->r_right;
103 *p = r->r_right;
104 val_free(r->r_val);
105 str_free(r->r_name);
106 free((char *) r);
107 return 0;
108}
109
110struct var **
bb4a0c0b
EW
111var_lookup1(p, name)
112register struct var **p;
b44eb60a
EW
113register char *name;
114{
4ae9aa77
EW
115 register cmp;
116
bb4a0c0b 117 while (*p != 0) {
b44eb60a
EW
118 if ((cmp = strcmp(name, (*p)->r_name)) < 0)
119 p = &(*p)->r_left;
4ae9aa77 120 else if (cmp > 0)
b44eb60a 121 p = &(*p)->r_right;
4ae9aa77
EW
122 else
123 break;
124 }
b44eb60a 125 return p;
4ae9aa77
EW
126}
127
e1d25671 128var_walk1(r, func, a)
4ae9aa77
EW
129register struct var *r;
130int (*func)();
131{
132 if (r == 0)
e1d25671
EW
133 return 0;
134 if (var_walk1(r->r_left, func, a) < 0 || (*func)(a, r) < 0
135 || var_walk1(r->r_right, func, a) < 0)
136 return -1;
137 return 0;
4ae9aa77 138}