[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.2

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