1: #ifndef lint
   2: static char sccsid[] = "@(#)var.c	3.8 4/24/85";
   3: #endif
   4: 
   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: 
  11: #include "value.h"
  12: #include "var.h"
  13: #include "string.h"
  14: 
  15: char *malloc();
  16: 
  17: struct var *
  18: var_set1(head, name, v)
  19: struct var **head;
  20: char *name;
  21: struct value *v;
  22: {
  23:     register struct var **p;
  24:     register struct var *r;
  25:     struct value val;
  26: 
  27:     /* do this first, easier to recover */
  28:     val = *v;
  29:     if (val.v_type == V_STR && val.v_str != 0 &&
  30:         (val.v_str = str_cpy(val.v_str)) == 0)
  31:         return 0;
  32:     if (*(p = var_lookup1(head, name)) == 0) {
  33:         r = (struct var *) malloc(sizeof (struct var));
  34:         if (r == 0) {
  35:             val_free(val);
  36:             return 0;
  37:         }
  38:         if ((r->r_name = str_cpy(name)) == 0) {
  39:             val_free(val);
  40:             free((char *) r);
  41:             return 0;
  42:         }
  43:         r->r_left = r->r_right = 0;
  44:         *p = r;
  45:     } else {
  46:         r = *p;
  47:         val_free(r->r_val);
  48:     }
  49:     r->r_val = val;
  50:     return r;
  51: }
  52: 
  53: struct var *
  54: var_setstr1(head, name, str)
  55: struct var **head;
  56: char *name;
  57: char *str;
  58: {
  59:     struct value v;
  60: 
  61:     v.v_type = V_STR;
  62:     v.v_str = str;
  63:     return var_set1(head, name, &v);
  64: }
  65: 
  66: struct var *
  67: var_setnum1(head, name, num)
  68: struct var **head;
  69: char *name;
  70: int num;
  71: {
  72:     struct value v;
  73: 
  74:     v.v_type = V_NUM;
  75:     v.v_num = num;
  76:     return var_set1(head, name, &v);
  77: }
  78: 
  79: var_unset1(head, name)
  80: struct var **head;
  81: char *name;
  82: {
  83:     register struct var **p;
  84:     register struct var *r;
  85: 
  86:     if (*(p = var_lookup1(head, name)) == 0)
  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: 
  99: struct var **
 100: var_lookup1(p, name)
 101: register struct var **p;
 102: register char *name;
 103: {
 104:     register cmp;
 105: 
 106:     while (*p != 0) {
 107:         if ((cmp = strcmp(name, (*p)->r_name)) < 0)
 108:             p = &(*p)->r_left;
 109:         else if (cmp > 0)
 110:             p = &(*p)->r_right;
 111:         else
 112:             break;
 113:     }
 114:     return p;
 115: }
 116: 
 117: var_walk1(r, func, a)
 118: register struct var *r;
 119: int (*func)();
 120: {
 121:     if (r == 0)
 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;
 127: }

Defined functions

var_lookup1 defined in line 99; used 5 times
var_set1 defined in line 17; used 4 times
var_setnum1 defined in line 66; used 2 times
var_setstr1 defined in line 53; used 3 times
var_unset1 defined in line 79; used 2 times
var_walk1 defined in line 117; used 4 times

Defined variables

sccsid defined in line 2; never used
Last modified: 1985-04-25
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1152
Valid CSS Valid XHTML 1.0 Strict