var.c (c5d5393c) var.c (0e7d3a01)
1/* $OpenBSD: var.c,v 1.21 2004/12/20 11:34:26 otto Exp $ */
1/* $OpenBSD: var.c,v 1.22 2004/12/22 17:14:34 millert Exp $ */
2
3#include "sh.h"
4#include <time.h>
5#include "ksh_limval.h"
6#include <sys/stat.h>
7#include <ctype.h>
8
9/*

--- 108 unchanged lines hidden (view full) ---

118 tp->type = names[i].v;
119 }
120}
121
122/* Used to calculate an array index for global()/local(). Sets *arrayp to
123 * non-zero if this is an array, sets *valp to the array index, returns
124 * the basename of the array.
125 */
2
3#include "sh.h"
4#include <time.h>
5#include "ksh_limval.h"
6#include <sys/stat.h>
7#include <ctype.h>
8
9/*

--- 108 unchanged lines hidden (view full) ---

118 tp->type = names[i].v;
119 }
120}
121
122/* Used to calculate an array index for global()/local(). Sets *arrayp to
123 * non-zero if this is an array, sets *valp to the array index, returns
124 * the basename of the array.
125 */
126const char *array_index_calc(const char *n, bool_t *arrayp, int *valp);
126const char *array_index_calc(const char *n, bool *arrayp, int *valp);
127
128const char *
127
128const char *
129array_index_calc(const char *n, bool_t *arrayp, int *valp)
129array_index_calc(const char *n, bool *arrayp, int *valp)
130{
131 const char *p;
132 int len;
133
130{
131 const char *p;
132 int len;
133
134 *arrayp = FALSE;
135 p = skip_varname(n, FALSE);
134 *arrayp = false;
135 p = skip_varname(n, false);
136 if (p != n && *p == '[' && (len = array_ref_len(p))) {
137 char *sub, *tmp;
138 long rval;
139
140 /* Calculate the value of the subscript */
136 if (p != n && *p == '[' && (len = array_ref_len(p))) {
137 char *sub, *tmp;
138 long rval;
139
140 /* Calculate the value of the subscript */
141 *arrayp = TRUE;
141 *arrayp = true;
142 tmp = str_nsave(p+1, len-2, ATEMP);
143 sub = substitute(tmp, 0);
144 afree(tmp, ATEMP);
145 n = str_nsave(n, p - n, ATEMP);
146 evaluate(sub, &rval, KSH_UNWIND_ERROR);
147 if (rval < 0 || rval > ARRAYMAX)
148 errorf("%s: subscript out of range", n);
149 *valp = rval;

--- 7 unchanged lines hidden (view full) ---

157 */
158struct tbl *
159global(const char *n)
160{
161 struct block *l = e->loc;
162 struct tbl *vp;
163 int c;
164 unsigned h;
142 tmp = str_nsave(p+1, len-2, ATEMP);
143 sub = substitute(tmp, 0);
144 afree(tmp, ATEMP);
145 n = str_nsave(n, p - n, ATEMP);
146 evaluate(sub, &rval, KSH_UNWIND_ERROR);
147 if (rval < 0 || rval > ARRAYMAX)
148 errorf("%s: subscript out of range", n);
149 *valp = rval;

--- 7 unchanged lines hidden (view full) ---

157 */
158struct tbl *
159global(const char *n)
160{
161 struct block *l = e->loc;
162 struct tbl *vp;
163 int c;
164 unsigned h;
165 bool_t array;
165 bool array;
166 int val;
167
168 /* Check to see if this is an array */
169 n = array_index_calc(n, &array, &val);
170 h = hash(n);
171 c = n[0];
172 if (!letter(c)) {
173 if (array)

--- 59 unchanged lines hidden (view full) ---

233 vp->flag |= SPECIAL;
234 return vp;
235}
236
237/*
238 * Search for local variable, if not found create locally.
239 */
240struct tbl *
166 int val;
167
168 /* Check to see if this is an array */
169 n = array_index_calc(n, &array, &val);
170 h = hash(n);
171 c = n[0];
172 if (!letter(c)) {
173 if (array)

--- 59 unchanged lines hidden (view full) ---

233 vp->flag |= SPECIAL;
234 return vp;
235}
236
237/*
238 * Search for local variable, if not found create locally.
239 */
240struct tbl *
241local(const char *n, bool_t copy)
241local(const char *n, bool copy)
242{
243 struct block *l = e->loc;
244 struct tbl *vp;
245 unsigned h;
242{
243 struct block *l = e->loc;
244 struct tbl *vp;
245 unsigned h;
246 bool_t array;
246 bool array;
247 int val;
248
249 /* Check to see if this is an array */
250 n = array_index_calc(n, &array, &val);
251 h = hash(n);
252 if (!letter(*n)) {
253 vp = &vtemp;
254 vp->flag = DEFINED|RDONLY;

--- 89 unchanged lines hidden (view full) ---

344
345/* set variable to string value */
346int
347setstr(struct tbl *vq, const char *s, int error_ok)
348{
349 int no_ro_check = error_ok & 0x4;
350 error_ok &= ~0x4;
351 if ((vq->flag & RDONLY) && !no_ro_check) {
247 int val;
248
249 /* Check to see if this is an array */
250 n = array_index_calc(n, &array, &val);
251 h = hash(n);
252 if (!letter(*n)) {
253 vp = &vtemp;
254 vp->flag = DEFINED|RDONLY;

--- 89 unchanged lines hidden (view full) ---

344
345/* set variable to string value */
346int
347setstr(struct tbl *vq, const char *s, int error_ok)
348{
349 int no_ro_check = error_ok & 0x4;
350 error_ok &= ~0x4;
351 if ((vq->flag & RDONLY) && !no_ro_check) {
352 warningf(TRUE, "%s: is read only", vq->name);
352 warningf(true, "%s: is read only", vq->name);
353 if (!error_ok)
354 errorf(null);
355 return 0;
356 }
357 if (!(vq->flag&INTEGER)) { /* string dest */
358 if ((vq->flag&ALLOC)) {
359 /* debugging */
360 if (s >= vq->val.s
361 && s <= vq->val.s + strlen(vq->val.s))
353 if (!error_ok)
354 errorf(null);
355 return 0;
356 }
357 if (!(vq->flag&INTEGER)) { /* string dest */
358 if ((vq->flag&ALLOC)) {
359 /* debugging */
360 if (s >= vq->val.s
361 && s <= vq->val.s + strlen(vq->val.s))
362 internal_errorf(TRUE,
362 internal_errorf(true,
363 "setstr: %s=%s: assigning to self",
364 vq->name, s);
365 afree((void*)vq->val.s, vq->areap);
366 }
367 vq->flag &= ~(ISSET|ALLOC);
368 vq->type = 0;
369 if (s && (vq->flag & (UCASEV_AL|LCASEV|LJUST|RJUST)))
370 s = formatstr(vq, s);

--- 199 unchanged lines hidden (view full) ---

570typeset(const char *var, Tflag set, Tflag clr, int field, int base)
571{
572 struct tbl *vp;
573 struct tbl *vpbase, *t;
574 char *tvar;
575 const char *val;
576
577 /* check for valid variable name, search for value */
363 "setstr: %s=%s: assigning to self",
364 vq->name, s);
365 afree((void*)vq->val.s, vq->areap);
366 }
367 vq->flag &= ~(ISSET|ALLOC);
368 vq->type = 0;
369 if (s && (vq->flag & (UCASEV_AL|LCASEV|LJUST|RJUST)))
370 s = formatstr(vq, s);

--- 199 unchanged lines hidden (view full) ---

570typeset(const char *var, Tflag set, Tflag clr, int field, int base)
571{
572 struct tbl *vp;
573 struct tbl *vpbase, *t;
574 char *tvar;
575 const char *val;
576
577 /* check for valid variable name, search for value */
578 val = skip_varname(var, FALSE);
578 val = skip_varname(var, false);
579 if (val == var)
580 return NULL;
581 if (*val == '[') {
582 int len;
583
584 len = array_ref_len(val);
585 if (len == 0)
586 return NULL;

--- 22 unchanged lines hidden (view full) ---

609 }
610
611 /* Prevent typeset from creating a local PATH/ENV/SHELL */
612 if (Flag(FRESTRICTED) && (strcmp(tvar, "PATH") == 0
613 || strcmp(tvar, "ENV") == 0
614 || strcmp(tvar, "SHELL") == 0))
615 errorf("%s: restricted", tvar);
616
579 if (val == var)
580 return NULL;
581 if (*val == '[') {
582 int len;
583
584 len = array_ref_len(val);
585 if (len == 0)
586 return NULL;

--- 22 unchanged lines hidden (view full) ---

609 }
610
611 /* Prevent typeset from creating a local PATH/ENV/SHELL */
612 if (Flag(FRESTRICTED) && (strcmp(tvar, "PATH") == 0
613 || strcmp(tvar, "ENV") == 0
614 || strcmp(tvar, "SHELL") == 0))
615 errorf("%s: restricted", tvar);
616
617 vp = (set&LOCAL) ? local(tvar, (set & LOCAL_COPY) ? TRUE : FALSE)
617 vp = (set&LOCAL) ? local(tvar, (set & LOCAL_COPY) ? true : false)
618 : global(tvar);
619 set &= ~(LOCAL|LOCAL_COPY);
620
621 vpbase = (vp->flag & ARRAY) ? global(arrayname(var)) : vp;
622
623 /* only allow export flag to be set. at&t ksh allows any attribute to
624 * be changed, which means it can be truncated or modified
625 * (-L/-R/-Z/-i).

--- 173 unchanged lines hidden (view full) ---

799
800 return p != s && p[0] == EOS;
801}
802
803/* Check if coded string s is a variable assignment */
804int
805is_wdvarassign(const char *s)
806{
618 : global(tvar);
619 set &= ~(LOCAL|LOCAL_COPY);
620
621 vpbase = (vp->flag & ARRAY) ? global(arrayname(var)) : vp;
622
623 /* only allow export flag to be set. at&t ksh allows any attribute to
624 * be changed, which means it can be truncated or modified
625 * (-L/-R/-Z/-i).

--- 173 unchanged lines hidden (view full) ---

799
800 return p != s && p[0] == EOS;
801}
802
803/* Check if coded string s is a variable assignment */
804int
805is_wdvarassign(const char *s)
806{
807 char *p = skip_wdvarname(s, TRUE);
807 char *p = skip_wdvarname(s, true);
808
809 return p != s && p[0] == CHAR && p[1] == '=';
810}
811
812/*
813 * Make the exported environment from the exported names in the dictionary.
814 */
815char **

--- 378 unchanged lines hidden ---
808
809 return p != s && p[0] == CHAR && p[1] == '=';
810}
811
812/*
813 * Make the exported environment from the exported names in the dictionary.
814 */
815char **

--- 378 unchanged lines hidden ---