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