[BACK]Return to var.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / window

Annotation of src/usr.bin/window/var.c, Revision 1.1.1.1

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