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