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