GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/* $OpenBSD: var.c,v 1.55 2015/12/30 09:07:00 tedu Exp $ */ |
||
2 |
|||
3 |
#include <sys/stat.h> |
||
4 |
|||
5 |
#include <ctype.h> |
||
6 |
#include <errno.h> |
||
7 |
#include <limits.h> |
||
8 |
#include <stdlib.h> |
||
9 |
#include <string.h> |
||
10 |
#include <time.h> |
||
11 |
#include <unistd.h> |
||
12 |
|||
13 |
#include "sh.h" |
||
14 |
|||
15 |
/* |
||
16 |
* Variables |
||
17 |
* |
||
18 |
* WARNING: unreadable code, needs a rewrite |
||
19 |
* |
||
20 |
* if (flag&INTEGER), val.i contains integer value, and type contains base. |
||
21 |
* otherwise, (val.s + type) contains string value. |
||
22 |
* if (flag&EXPORT), val.s contains "name=value" for E-Z exporting. |
||
23 |
*/ |
||
24 |
static struct tbl vtemp; |
||
25 |
static struct table specials; |
||
26 |
static char *formatstr(struct tbl *, const char *); |
||
27 |
static void export(struct tbl *, const char *); |
||
28 |
static int special(const char *); |
||
29 |
static void unspecial(const char *); |
||
30 |
static void getspec(struct tbl *); |
||
31 |
static void setspec(struct tbl *); |
||
32 |
static void unsetspec(struct tbl *); |
||
33 |
static struct tbl *arraysearch(struct tbl *, int); |
||
34 |
|||
35 |
/* |
||
36 |
* create a new block for function calls and simple commands |
||
37 |
* assume caller has allocated and set up genv->loc |
||
38 |
*/ |
||
39 |
void |
||
40 |
newblock(void) |
||
41 |
143337 |
{ |
|
42 |
struct block *l; |
||
43 |
static char *const empty[] = {null}; |
||
44 |
|||
45 |
143337 |
l = alloc(sizeof(struct block), ATEMP); |
|
46 |
143337 |
l->flags = 0; |
|
47 |
143337 |
ainit(&l->area); /* todo: could use genv->area (l->area => l->areap) */ |
|
48 |
✓✓ | 143337 |
if (!genv->loc) { |
49 |
3525 |
l->argc = 0; |
|
50 |
3525 |
l->argv = (char **) empty; |
|
51 |
} else { |
||
52 |
139812 |
l->argc = genv->loc->argc; |
|
53 |
139812 |
l->argv = genv->loc->argv; |
|
54 |
} |
||
55 |
143337 |
l->exit = l->error = NULL; |
|
56 |
143337 |
ktinit(&l->vars, &l->area, 0); |
|
57 |
143337 |
ktinit(&l->funs, &l->area, 0); |
|
58 |
143337 |
l->next = genv->loc; |
|
59 |
143337 |
genv->loc = l; |
|
60 |
143337 |
} |
|
61 |
|||
62 |
/* |
||
63 |
* pop a block handling special variables |
||
64 |
*/ |
||
65 |
void |
||
66 |
popblock(void) |
||
67 |
136162 |
{ |
|
68 |
136162 |
struct block *l = genv->loc; |
|
69 |
136162 |
struct tbl *vp, **vpp = l->vars.tbls, *vq; |
|
70 |
int i; |
||
71 |
|||
72 |
136162 |
genv->loc = l->next; /* pop block */ |
|
73 |
✓✓ | 273972 |
for (i = l->vars.size; --i >= 0; ) |
74 |
✓✓✓✓ |
1648 |
if ((vp = *vpp++) != NULL && (vp->flag&SPECIAL)) { |
75 |
✓✓ | 8 |
if ((vq = global(vp->name))->flag & ISSET) |
76 |
4 |
setspec(vq); |
|
77 |
else |
||
78 |
4 |
unsetspec(vq); |
|
79 |
} |
||
80 |
✓✓ | 136162 |
if (l->flags & BF_DOGETOPTS) |
81 |
8 |
user_opt = l->getopts_state; |
|
82 |
136162 |
afreeall(&l->area); |
|
83 |
136162 |
afree(l, ATEMP); |
|
84 |
136162 |
} |
|
85 |
|||
86 |
/* called by main() to initialize variable data structures */ |
||
87 |
void |
||
88 |
initvar(void) |
||
89 |
3525 |
{ |
|
90 |
static const struct { |
||
91 |
const char *name; |
||
92 |
int v; |
||
93 |
} names[] = { |
||
94 |
{ "COLUMNS", V_COLUMNS }, |
||
95 |
{ "IFS", V_IFS }, |
||
96 |
{ "OPTIND", V_OPTIND }, |
||
97 |
{ "PATH", V_PATH }, |
||
98 |
{ "POSIXLY_CORRECT", V_POSIXLY_CORRECT }, |
||
99 |
{ "TMPDIR", V_TMPDIR }, |
||
100 |
#ifdef HISTORY |
||
101 |
{ "HISTFILE", V_HISTFILE }, |
||
102 |
{ "HISTSIZE", V_HISTSIZE }, |
||
103 |
#endif /* HISTORY */ |
||
104 |
#ifdef EDIT |
||
105 |
{ "EDITOR", V_EDITOR }, |
||
106 |
{ "VISUAL", V_VISUAL }, |
||
107 |
#endif /* EDIT */ |
||
108 |
{ "MAIL", V_MAIL }, |
||
109 |
{ "MAILCHECK", V_MAILCHECK }, |
||
110 |
{ "MAILPATH", V_MAILPATH }, |
||
111 |
{ "RANDOM", V_RANDOM }, |
||
112 |
{ "SECONDS", V_SECONDS }, |
||
113 |
{ "TMOUT", V_TMOUT }, |
||
114 |
{ "LINENO", V_LINENO }, |
||
115 |
{ NULL, 0 } |
||
116 |
}; |
||
117 |
int i; |
||
118 |
struct tbl *tp; |
||
119 |
|||
120 |
3525 |
ktinit(&specials, APERM, 32); /* must be 2^n (currently 17 specials) */ |
|
121 |
✓✓ | 63450 |
for (i = 0; names[i].name; i++) { |
122 |
59925 |
tp = ktenter(&specials, names[i].name, hash(names[i].name)); |
|
123 |
59925 |
tp->flag = DEFINED|ISSET; |
|
124 |
59925 |
tp->type = names[i].v; |
|
125 |
} |
||
126 |
3525 |
} |
|
127 |
|||
128 |
/* Used to calculate an array index for global()/local(). Sets *arrayp to |
||
129 |
* non-zero if this is an array, sets *valp to the array index, returns |
||
130 |
* the basename of the array. |
||
131 |
*/ |
||
132 |
static const char * |
||
133 |
array_index_calc(const char *n, bool *arrayp, int *valp) |
||
134 |
337674 |
{ |
|
135 |
const char *p; |
||
136 |
int len; |
||
137 |
|||
138 |
337674 |
*arrayp = false; |
|
139 |
337674 |
p = skip_varname(n, false); |
|
140 |
✓✓✓✓ ✓✗ |
337674 |
if (p != n && *p == '[' && (len = array_ref_len(p))) { |
141 |
char *sub, *tmp; |
||
142 |
long rval; |
||
143 |
|||
144 |
/* Calculate the value of the subscript */ |
||
145 |
20 |
*arrayp = true; |
|
146 |
20 |
tmp = str_nsave(p+1, len-2, ATEMP); |
|
147 |
20 |
sub = substitute(tmp, 0); |
|
148 |
20 |
afree(tmp, ATEMP); |
|
149 |
20 |
n = str_nsave(n, p - n, ATEMP); |
|
150 |
20 |
evaluate(sub, &rval, KSH_UNWIND_ERROR, true); |
|
151 |
✗✓ | 20 |
if (rval < 0 || rval > INT_MAX) |
152 |
errorf("%s: subscript %ld out of range", n, rval); |
||
153 |
20 |
*valp = rval; |
|
154 |
20 |
afree(sub, ATEMP); |
|
155 |
} |
||
156 |
337674 |
return n; |
|
157 |
} |
||
158 |
|||
159 |
/* |
||
160 |
* Search for variable, if not found create globally. |
||
161 |
*/ |
||
162 |
struct tbl * |
||
163 |
global(const char *n) |
||
164 |
301656 |
{ |
|
165 |
301656 |
struct block *l = genv->loc; |
|
166 |
struct tbl *vp; |
||
167 |
long num; |
||
168 |
int c; |
||
169 |
unsigned int h; |
||
170 |
bool array; |
||
171 |
int val; |
||
172 |
|||
173 |
/* Check to see if this is an array */ |
||
174 |
301656 |
n = array_index_calc(n, &array, &val); |
|
175 |
301656 |
h = hash(n); |
|
176 |
301656 |
c = (unsigned char)n[0]; |
|
177 |
✓✓ | 301656 |
if (!letter(c)) { |
178 |
✗✓ | 4325 |
if (array) |
179 |
errorf("bad substitution"); |
||
180 |
4325 |
vp = &vtemp; |
|
181 |
4325 |
vp->flag = DEFINED; |
|
182 |
4325 |
vp->type = 0; |
|
183 |
4325 |
vp->areap = ATEMP; |
|
184 |
4325 |
*vp->name = c; |
|
185 |
✓✓ | 4325 |
if (digit(c)) { |
186 |
3592 |
errno = 0; |
|
187 |
3592 |
num = strtol(n, NULL, 10); |
|
188 |
✓✗✓✓ |
3592 |
if (errno == 0 && num <= l->argc) |
189 |
/* setstr can't fail here */ |
||
190 |
3575 |
setstr(vp, l->argv[num], KSH_RETURN_ERROR); |
|
191 |
3592 |
vp->flag |= RDONLY; |
|
192 |
3592 |
return vp; |
|
193 |
} |
||
194 |
733 |
vp->flag |= RDONLY; |
|
195 |
✗✓ | 733 |
if (n[1] != '\0') |
196 |
return vp; |
||
197 |
733 |
vp->flag |= ISSET|INTEGER; |
|
198 |
✓✓✓✓ ✓✓ |
733 |
switch (c) { |
199 |
case '$': |
||
200 |
136 |
vp->val.i = kshpid; |
|
201 |
136 |
break; |
|
202 |
case '!': |
||
203 |
/* If no job, expand to nothing */ |
||
204 |
✗✓ | 95 |
if ((vp->val.i = j_async()) == 0) |
205 |
vp->flag &= ~(ISSET|INTEGER); |
||
206 |
break; |
||
207 |
case '?': |
||
208 |
387 |
vp->val.i = exstat; |
|
209 |
387 |
break; |
|
210 |
case '#': |
||
211 |
78 |
vp->val.i = l->argc; |
|
212 |
78 |
break; |
|
213 |
case '-': |
||
214 |
9 |
vp->flag &= ~INTEGER; |
|
215 |
9 |
vp->val.s = getoptions(); |
|
216 |
9 |
break; |
|
217 |
default: |
||
218 |
28 |
vp->flag &= ~(ISSET|INTEGER); |
|
219 |
} |
||
220 |
733 |
return vp; |
|
221 |
} |
||
222 |
489890 |
for (l = genv->loc; ; l = l->next) { |
|
223 |
489890 |
vp = ktsearch(&l->vars, n, h); |
|
224 |
✓✓ | 489890 |
if (vp != NULL) { |
225 |
✓✓ | 198464 |
if (array) |
226 |
14 |
return arraysearch(vp, val); |
|
227 |
else |
||
228 |
198450 |
return vp; |
|
229 |
} |
||
230 |
✓✓ | 291426 |
if (l->next == NULL) |
231 |
98867 |
break; |
|
232 |
192559 |
} |
|
233 |
98867 |
vp = ktenter(&l->vars, n, h); |
|
234 |
✓✓ | 98867 |
if (array) |
235 |
4 |
vp = arraysearch(vp, val); |
|
236 |
98867 |
vp->flag |= DEFINED; |
|
237 |
✓✓ | 98867 |
if (special(n)) |
238 |
20869 |
vp->flag |= SPECIAL; |
|
239 |
98867 |
return vp; |
|
240 |
} |
||
241 |
|||
242 |
/* |
||
243 |
* Search for local variable, if not found create locally. |
||
244 |
*/ |
||
245 |
struct tbl * |
||
246 |
local(const char *n, bool copy) |
||
247 |
36016 |
{ |
|
248 |
36016 |
struct block *l = genv->loc; |
|
249 |
struct tbl *vp; |
||
250 |
unsigned int h; |
||
251 |
bool array; |
||
252 |
int val; |
||
253 |
|||
254 |
/* Check to see if this is an array */ |
||
255 |
36016 |
n = array_index_calc(n, &array, &val); |
|
256 |
36016 |
h = hash(n); |
|
257 |
✗✓ | 36016 |
if (!letter(*n)) { |
258 |
vp = &vtemp; |
||
259 |
vp->flag = DEFINED|RDONLY; |
||
260 |
vp->type = 0; |
||
261 |
vp->areap = ATEMP; |
||
262 |
return vp; |
||
263 |
} |
||
264 |
36016 |
vp = ktenter(&l->vars, n, h); |
|
265 |
✓✓✓✗ |
36016 |
if (copy && !(vp->flag & DEFINED)) { |
266 |
35 |
struct block *ll = l; |
|
267 |
35 |
struct tbl *vq = NULL; |
|
268 |
|||
269 |
✓✓✓✓ |
56 |
while ((ll = ll->next) && !(vq = ktsearch(&ll->vars, n, h))) |
270 |
; |
||
271 |
✓✓ | 35 |
if (vq) { |
272 |
14 |
vp->flag |= vq->flag & |
|
273 |
(EXPORT | INTEGER | RDONLY | LJUST | RJUST | |
||
274 |
ZEROFIL | LCASEV | UCASEV_AL | INT_U | INT_L); |
||
275 |
✗✓ | 14 |
if (vq->flag & INTEGER) |
276 |
vp->type = vq->type; |
||
277 |
14 |
vp->u2.field = vq->u2.field; |
|
278 |
} |
||
279 |
} |
||
280 |
✓✓ | 36016 |
if (array) |
281 |
2 |
vp = arraysearch(vp, val); |
|
282 |
36016 |
vp->flag |= DEFINED; |
|
283 |
✓✓ | 36016 |
if (special(n)) |
284 |
21164 |
vp->flag |= SPECIAL; |
|
285 |
36016 |
return vp; |
|
286 |
} |
||
287 |
|||
288 |
/* get variable string value */ |
||
289 |
char * |
||
290 |
str_val(struct tbl *vp) |
||
291 |
208367 |
{ |
|
292 |
char *s; |
||
293 |
|||
294 |
✓✓ | 208367 |
if ((vp->flag&SPECIAL)) |
295 |
26125 |
getspec(vp); |
|
296 |
✓✓ | 208367 |
if (!(vp->flag&ISSET)) |
297 |
24571 |
s = null; /* special to dollar() */ |
|
298 |
✓✓ | 183796 |
else if (!(vp->flag&INTEGER)) /* string source */ |
299 |
177998 |
s = vp->val.s + vp->type; |
|
300 |
else { /* integer source */ |
||
301 |
/* worst case number length is when base=2, so use BITS(long) */ |
||
302 |
/* minus base # number null */ |
||
303 |
char strbuf[1 + 2 + 1 + BITS(long) + 1]; |
||
304 |
const char *digits = (vp->flag & UCASEV_AL) ? |
||
305 |
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" : |
||
306 |
✗✓ | 5798 |
"0123456789abcdefghijklmnopqrstuvwxyz"; |
307 |
unsigned long n; |
||
308 |
int base; |
||
309 |
|||
310 |
5798 |
s = strbuf + sizeof(strbuf); |
|
311 |
✗✓ | 5798 |
if (vp->flag & INT_U) |
312 |
n = (unsigned long) vp->val.i; |
||
313 |
else |
||
314 |
5798 |
n = (vp->val.i < 0) ? -vp->val.i : vp->val.i; |
|
315 |
✓✓ | 5798 |
base = (vp->type == 0) ? 10 : vp->type; |
316 |
✓✗✗✓ |
5798 |
if (base < 2 || base > strlen(digits)) |
317 |
base = 10; |
||
318 |
|||
319 |
5798 |
*--s = '\0'; |
|
320 |
do { |
||
321 |
24428 |
*--s = digits[n % base]; |
|
322 |
24428 |
n /= base; |
|
323 |
✓✓ | 24428 |
} while (n != 0); |
324 |
✓✓ | 5798 |
if (base != 10) { |
325 |
26 |
*--s = '#'; |
|
326 |
26 |
*--s = digits[base % 10]; |
|
327 |
✗✓ | 26 |
if (base >= 10) |
328 |
*--s = digits[base / 10]; |
||
329 |
} |
||
330 |
✓✗✓✓ |
5798 |
if (!(vp->flag & INT_U) && vp->val.i < 0) |
331 |
2 |
*--s = '-'; |
|
332 |
✗✓ | 5798 |
if (vp->flag & (RJUST|LJUST)) /* case already dealt with */ |
333 |
s = formatstr(vp, s); |
||
334 |
else |
||
335 |
5798 |
s = str_save(s, ATEMP); |
|
336 |
} |
||
337 |
208367 |
return s; |
|
338 |
} |
||
339 |
|||
340 |
/* get variable integer value, with error checking */ |
||
341 |
long |
||
342 |
intval(struct tbl *vp) |
||
343 |
21168 |
{ |
|
344 |
long num; |
||
345 |
int base; |
||
346 |
|||
347 |
21168 |
base = getint(vp, &num, false); |
|
348 |
✗✓ | 21168 |
if (base == -1) |
349 |
/* XXX check calls - is error here ok by POSIX? */ |
||
350 |
errorf("%s: bad number", str_val(vp)); |
||
351 |
21168 |
return num; |
|
352 |
} |
||
353 |
|||
354 |
/* set variable to string value */ |
||
355 |
int |
||
356 |
setstr(struct tbl *vq, const char *s, int error_ok) |
||
357 |
165716 |
{ |
|
358 |
165716 |
const char *fs = NULL; |
|
359 |
165716 |
int no_ro_check = error_ok & 0x4; |
|
360 |
165716 |
error_ok &= ~0x4; |
|
361 |
✗✓ | 165716 |
if ((vq->flag & RDONLY) && !no_ro_check) { |
362 |
warningf(true, "%s: is read only", vq->name); |
||
363 |
if (!error_ok) |
||
364 |
errorf(NULL); |
||
365 |
return 0; |
||
366 |
} |
||
367 |
✓✓ | 165716 |
if (!(vq->flag&INTEGER)) { /* string dest */ |
368 |
✓✓ | 147999 |
if ((vq->flag&ALLOC)) { |
369 |
/* debugging */ |
||
370 |
✓✓✗✓ |
56699 |
if (s >= vq->val.s && |
371 |
s <= vq->val.s + strlen(vq->val.s)) |
||
372 |
internal_errorf(true, |
||
373 |
"setstr: %s=%s: assigning to self", |
||
374 |
vq->name, s); |
||
375 |
56699 |
afree(vq->val.s, vq->areap); |
|
376 |
} |
||
377 |
147999 |
vq->flag &= ~(ISSET|ALLOC); |
|
378 |
147999 |
vq->type = 0; |
|
379 |
✓✗✗✓ |
147999 |
if (s && (vq->flag & (UCASEV_AL|LCASEV|LJUST|RJUST))) |
380 |
fs = s = formatstr(vq, s); |
||
381 |
✓✓ | 147999 |
if ((vq->flag&EXPORT)) |
382 |
63135 |
export(vq, s); |
|
383 |
else { |
||
384 |
84864 |
vq->val.s = str_save(s, vq->areap); |
|
385 |
84864 |
vq->flag |= ALLOC; |
|
386 |
} |
||
387 |
} else { /* integer dest */ |
||
388 |
✗✓ | 17717 |
if (!v_evaluate(vq, s, error_ok, true)) |
389 |
return 0; |
||
390 |
} |
||
391 |
165706 |
vq->flag |= ISSET; |
|
392 |
✓✓ | 165706 |
if ((vq->flag&SPECIAL)) |
393 |
28228 |
setspec(vq); |
|
394 |
165706 |
afree((void *)fs, ATEMP); |
|
395 |
165706 |
return 1; |
|
396 |
} |
||
397 |
|||
398 |
/* set variable to integer */ |
||
399 |
void |
||
400 |
setint(struct tbl *vq, long int n) |
||
401 |
4908 |
{ |
|
402 |
✓✓ | 4908 |
if (!(vq->flag&INTEGER)) { |
403 |
4814 |
struct tbl *vp = &vtemp; |
|
404 |
4814 |
vp->flag = (ISSET|INTEGER); |
|
405 |
4814 |
vp->type = 0; |
|
406 |
4814 |
vp->areap = ATEMP; |
|
407 |
4814 |
vp->val.i = n; |
|
408 |
/* setstr can't fail here */ |
||
409 |
4814 |
setstr(vq, str_val(vp), KSH_RETURN_ERROR); |
|
410 |
} else |
||
411 |
94 |
vq->val.i = n; |
|
412 |
4908 |
vq->flag |= ISSET; |
|
413 |
✓✓ | 4908 |
if ((vq->flag&SPECIAL)) |
414 |
84 |
setspec(vq); |
|
415 |
4908 |
} |
|
416 |
|||
417 |
int |
||
418 |
getint(struct tbl *vp, long int *nump, bool arith) |
||
419 |
57486 |
{ |
|
420 |
char *s; |
||
421 |
int c; |
||
422 |
int base, neg; |
||
423 |
57486 |
int have_base = 0; |
|
424 |
long num; |
||
425 |
|||
426 |
✓✓ | 57486 |
if (vp->flag&SPECIAL) |
427 |
168 |
getspec(vp); |
|
428 |
/* XXX is it possible for ISSET to be set and val.s to be 0? */ |
||
429 |
✓✗✓✓ ✗✓ |
57486 |
if (!(vp->flag&ISSET) || (!(vp->flag&INTEGER) && vp->val.s == NULL)) |
430 |
return -1; |
||
431 |
✓✓ | 57486 |
if (vp->flag&INTEGER) { |
432 |
39203 |
*nump = vp->val.i; |
|
433 |
39203 |
return vp->type; |
|
434 |
} |
||
435 |
18283 |
s = vp->val.s + vp->type; |
|
436 |
✗✓ | 18283 |
if (s == NULL) /* redundant given initial test */ |
437 |
s = null; |
||
438 |
18283 |
base = 10; |
|
439 |
18283 |
num = 0; |
|
440 |
18283 |
neg = 0; |
|
441 |
✓✓✓✓ ✓✓ |
18283 |
if (arith && *s == '0' && *(s+1)) { |
442 |
2 |
s++; |
|
443 |
✗✓ | 2 |
if (*s == 'x' || *s == 'X') { |
444 |
s++; |
||
445 |
base = 16; |
||
446 |
✗✓ | 2 |
} else if (vp->flag & ZEROFIL) { |
447 |
while (*s == '0') |
||
448 |
s++; |
||
449 |
} else |
||
450 |
2 |
base = 8; |
|
451 |
2 |
have_base++; |
|
452 |
} |
||
453 |
✓✓ | 57901 |
for (c = (unsigned char)*s++; c ; c = (unsigned char)*s++) { |
454 |
✗✓ | 39624 |
if (c == '-') { |
455 |
neg++; |
||
456 |
✓✓ | 39624 |
} else if (c == '#') { |
457 |
24 |
base = (int) num; |
|
458 |
✓✓✗✓ |
24 |
if (have_base || base < 2 || base > 36) |
459 |
4 |
return -1; |
|
460 |
20 |
num = 0; |
|
461 |
20 |
have_base = 1; |
|
462 |
✓✗✓✗ |
79198 |
} else if (letnum(c)) { |
463 |
✓✗ | 39600 |
if (isdigit(c)) |
464 |
39600 |
c -= '0'; |
|
465 |
else if (islower(c)) |
||
466 |
c -= 'a' - 10; /* todo: assumes ascii */ |
||
467 |
else if (isupper(c)) |
||
468 |
c -= 'A' - 10; /* todo: assumes ascii */ |
||
469 |
else |
||
470 |
c = -1; /* _: force error */ |
||
471 |
✓✓ | 39600 |
if (c < 0 || c >= base) |
472 |
2 |
return -1; |
|
473 |
39598 |
num = num * base + c; |
|
474 |
} else |
||
475 |
return -1; |
||
476 |
} |
||
477 |
✗✓ | 18277 |
if (neg) |
478 |
num = -num; |
||
479 |
18277 |
*nump = num; |
|
480 |
18277 |
return base; |
|
481 |
} |
||
482 |
|||
483 |
/* convert variable vq to integer variable, setting its value from vp |
||
484 |
* (vq and vp may be the same) |
||
485 |
*/ |
||
486 |
struct tbl * |
||
487 |
setint_v(struct tbl *vq, struct tbl *vp, bool arith) |
||
488 |
36150 |
{ |
|
489 |
int base; |
||
490 |
long num; |
||
491 |
|||
492 |
✓✓ | 36150 |
if ((base = getint(vp, &num, arith)) == -1) |
493 |
6 |
return NULL; |
|
494 |
✗✓ | 36144 |
if (!(vq->flag & INTEGER) && (vq->flag & ALLOC)) { |
495 |
vq->flag &= ~ALLOC; |
||
496 |
afree(vq->val.s, vq->areap); |
||
497 |
} |
||
498 |
36144 |
vq->val.i = num; |
|
499 |
✓✓ | 36144 |
if (vq->type == 0) /* default base */ |
500 |
36072 |
vq->type = base; |
|
501 |
36144 |
vq->flag |= ISSET|INTEGER; |
|
502 |
✓✓ | 36144 |
if (vq->flag&SPECIAL) |
503 |
14104 |
setspec(vq); |
|
504 |
36144 |
return vq; |
|
505 |
} |
||
506 |
|||
507 |
static char * |
||
508 |
formatstr(struct tbl *vp, const char *s) |
||
509 |
{ |
||
510 |
int olen, nlen; |
||
511 |
char *p, *q; |
||
512 |
|||
513 |
olen = strlen(s); |
||
514 |
|||
515 |
if (vp->flag & (RJUST|LJUST)) { |
||
516 |
if (!vp->u2.field) /* default field width */ |
||
517 |
vp->u2.field = olen; |
||
518 |
nlen = vp->u2.field; |
||
519 |
} else |
||
520 |
nlen = olen; |
||
521 |
|||
522 |
p = alloc(nlen + 1, ATEMP); |
||
523 |
if (vp->flag & (RJUST|LJUST)) { |
||
524 |
int slen; |
||
525 |
|||
526 |
if (vp->flag & RJUST) { |
||
527 |
const char *q = s + olen; |
||
528 |
/* strip trailing spaces (at&t ksh uses q[-1] == ' ') */ |
||
529 |
while (q > s && isspace((unsigned char)q[-1])) |
||
530 |
--q; |
||
531 |
slen = q - s; |
||
532 |
if (slen > vp->u2.field) { |
||
533 |
s += slen - vp->u2.field; |
||
534 |
slen = vp->u2.field; |
||
535 |
} |
||
536 |
shf_snprintf(p, nlen + 1, |
||
537 |
((vp->flag & ZEROFIL) && digit(*s)) ? |
||
538 |
"%0*s%.*s" : "%*s%.*s", |
||
539 |
vp->u2.field - slen, null, slen, s); |
||
540 |
} else { |
||
541 |
/* strip leading spaces/zeros */ |
||
542 |
while (isspace((unsigned char)*s)) |
||
543 |
s++; |
||
544 |
if (vp->flag & ZEROFIL) |
||
545 |
while (*s == '0') |
||
546 |
s++; |
||
547 |
shf_snprintf(p, nlen + 1, "%-*.*s", |
||
548 |
vp->u2.field, vp->u2.field, s); |
||
549 |
} |
||
550 |
} else |
||
551 |
memcpy(p, s, olen + 1); |
||
552 |
|||
553 |
if (vp->flag & UCASEV_AL) { |
||
554 |
for (q = p; *q; q++) |
||
555 |
if (islower((unsigned char)*q)) |
||
556 |
*q = toupper((unsigned char)*q); |
||
557 |
} else if (vp->flag & LCASEV) { |
||
558 |
for (q = p; *q; q++) |
||
559 |
if (isupper((unsigned char)*q)) |
||
560 |
*q = tolower((unsigned char)*q); |
||
561 |
} |
||
562 |
|||
563 |
return p; |
||
564 |
} |
||
565 |
|||
566 |
/* |
||
567 |
* make vp->val.s be "name=value" for quick exporting. |
||
568 |
*/ |
||
569 |
static void |
||
570 |
export(struct tbl *vp, const char *val) |
||
571 |
63407 |
{ |
|
572 |
char *xp; |
||
573 |
✓✓ | 63407 |
char *op = (vp->flag&ALLOC) ? vp->val.s : NULL; |
574 |
63407 |
int namelen = strlen(vp->name); |
|
575 |
63407 |
int vallen = strlen(val) + 1; |
|
576 |
|||
577 |
63407 |
vp->flag |= ALLOC; |
|
578 |
63407 |
xp = alloc(namelen + 1 + vallen, vp->areap); |
|
579 |
63407 |
memcpy(vp->val.s = xp, vp->name, namelen); |
|
580 |
63407 |
xp += namelen; |
|
581 |
63407 |
*xp++ = '='; |
|
582 |
63407 |
vp->type = xp - vp->val.s; /* offset to value */ |
|
583 |
63407 |
memcpy(xp, val, vallen); |
|
584 |
63407 |
afree(op, vp->areap); |
|
585 |
63407 |
} |
|
586 |
|||
587 |
/* |
||
588 |
* lookup variable (according to (set&LOCAL)), |
||
589 |
* set its attributes (INTEGER, RDONLY, EXPORT, TRACE, LJUST, RJUST, ZEROFIL, |
||
590 |
* LCASEV, UCASEV_AL), and optionally set its value if an assignment. |
||
591 |
*/ |
||
592 |
struct tbl * |
||
593 |
typeset(const char *var, int set, int clr, int field, int base) |
||
594 |
119048 |
{ |
|
595 |
struct tbl *vp; |
||
596 |
struct tbl *vpbase, *t; |
||
597 |
char *tvar; |
||
598 |
const char *val; |
||
599 |
|||
600 |
/* check for valid variable name, search for value */ |
||
601 |
119048 |
val = skip_varname(var, false); |
|
602 |
✗✓ | 119048 |
if (val == var) |
603 |
return NULL; |
||
604 |
✓✓ | 119048 |
if (*val == '[') { |
605 |
int len; |
||
606 |
|||
607 |
16 |
len = array_ref_len(val); |
|
608 |
✗✓ | 16 |
if (len == 0) |
609 |
return NULL; |
||
610 |
/* IMPORT is only used when the shell starts up and is |
||
611 |
* setting up its environment. Allow only simple array |
||
612 |
* references at this time since parameter/command substitution |
||
613 |
* is preformed on the [expression], which would be a major |
||
614 |
* security hole. |
||
615 |
*/ |
||
616 |
✗✓ | 16 |
if (set & IMPORT) { |
617 |
int i; |
||
618 |
for (i = 1; i < len - 1; i++) |
||
619 |
if (!digit(val[i])) |
||
620 |
return NULL; |
||
621 |
} |
||
622 |
16 |
val += len; |
|
623 |
} |
||
624 |
✓✓ | 119048 |
if (*val == '=') |
625 |
96897 |
tvar = str_nsave(var, val++ - var, ATEMP); |
|
626 |
else { |
||
627 |
/* Importing from original environment: must have an = */ |
||
628 |
✗✓ | 22151 |
if (set & IMPORT) |
629 |
return NULL; |
||
630 |
22151 |
tvar = (char *) var; |
|
631 |
22151 |
val = NULL; |
|
632 |
} |
||
633 |
|||
634 |
/* Prevent typeset from creating a local PATH/ENV/SHELL */ |
||
635 |
✗✓✗✗ ✗✗✗✗ |
119048 |
if (Flag(FRESTRICTED) && (strcmp(tvar, "PATH") == 0 || |
636 |
strcmp(tvar, "ENV") == 0 || strcmp(tvar, "SHELL") == 0)) |
||
637 |
errorf("%s: restricted", tvar); |
||
638 |
|||
639 |
✓✓ | 119048 |
vp = (set&LOCAL) ? local(tvar, (set & LOCAL_COPY) ? true : false) : |
640 |
global(tvar); |
||
641 |
119048 |
set &= ~(LOCAL|LOCAL_COPY); |
|
642 |
|||
643 |
✓✓ | 119048 |
vpbase = (vp->flag & ARRAY) ? global(arrayname(var)) : vp; |
644 |
|||
645 |
/* only allow export flag to be set. at&t ksh allows any attribute to |
||
646 |
* be changed, which means it can be truncated or modified |
||
647 |
* (-L/-R/-Z/-i). |
||
648 |
*/ |
||
649 |
✓✓✗✓ ✗✗ |
119048 |
if ((vpbase->flag&RDONLY) && |
650 |
(val || clr || (set & ~EXPORT))) |
||
651 |
/* XXX check calls - is error here ok by POSIX? */ |
||
652 |
2 |
errorf("%s: is read only", tvar); |
|
653 |
✓✓ | 119046 |
if (val) |
654 |
96895 |
afree(tvar, ATEMP); |
|
655 |
|||
656 |
/* most calls are with set/clr == 0 */ |
||
657 |
✓✓ | 119046 |
if (set | clr) { |
658 |
94910 |
int ok = 1; |
|
659 |
/* XXX if x[0] isn't set, there will be problems: need to have |
||
660 |
* one copy of attributes for arrays... |
||
661 |
*/ |
||
662 |
✓✓ | 189822 |
for (t = vpbase; t; t = t->u.array) { |
663 |
int fake_assign; |
||
664 |
94912 |
char *s = NULL; |
|
665 |
94912 |
char *free_me = NULL; |
|
666 |
|||
667 |
✓✓✓✓ ✓✗✓✓ ✓✗✓✓ ✓✓ |
94912 |
fake_assign = (t->flag & ISSET) && (!val || t != vp) && |
668 |
((set & (UCASEV_AL|LCASEV|LJUST|RJUST|ZEROFIL)) || |
||
669 |
((t->flag & INTEGER) && (clr & INTEGER)) || |
||
670 |
(!(t->flag & INTEGER) && (set & INTEGER))); |
||
671 |
✓✓ | 94912 |
if (fake_assign) { |
672 |
✗✓ | 3529 |
if (t->flag & INTEGER) { |
673 |
s = str_val(t); |
||
674 |
free_me = NULL; |
||
675 |
} else { |
||
676 |
3529 |
s = t->val.s + t->type; |
|
677 |
✓✗ | 3529 |
free_me = (t->flag & ALLOC) ? t->val.s : |
678 |
NULL; |
||
679 |
} |
||
680 |
3529 |
t->flag &= ~ALLOC; |
|
681 |
} |
||
682 |
✓✓✓✓ |
94912 |
if (!(t->flag & INTEGER) && (set & INTEGER)) { |
683 |
21218 |
t->type = 0; |
|
684 |
21218 |
t->flag &= ~ALLOC; |
|
685 |
} |
||
686 |
94912 |
t->flag = (t->flag | set) & ~clr; |
|
687 |
/* Don't change base if assignment is to be done, |
||
688 |
* in case assignment fails. |
||
689 |
*/ |
||
690 |
✓✓✓✓ |
94912 |
if ((set & INTEGER) && base > 0 && (!val || t != vp)) |
691 |
6 |
t->type = base; |
|
692 |
✗✓ | 94912 |
if (set & (LJUST|RJUST|ZEROFIL)) |
693 |
t->u2.field = field; |
||
694 |
✓✓ | 94912 |
if (fake_assign) { |
695 |
✗✓ | 3529 |
if (!setstr(t, s, KSH_RETURN_ERROR)) { |
696 |
/* Somewhat arbitrary action here: |
||
697 |
* zap contents of variable, but keep |
||
698 |
* the flag settings. |
||
699 |
*/ |
||
700 |
ok = 0; |
||
701 |
if (t->flag & INTEGER) |
||
702 |
t->flag &= ~ISSET; |
||
703 |
else { |
||
704 |
if (t->flag & ALLOC) |
||
705 |
afree(t->val.s, t->areap); |
||
706 |
t->flag &= ~(ISSET|ALLOC); |
||
707 |
t->type = 0; |
||
708 |
} |
||
709 |
} |
||
710 |
3529 |
afree(free_me, t->areap); |
|
711 |
} |
||
712 |
} |
||
713 |
✗✓ | 94910 |
if (!ok) |
714 |
errorf(NULL); |
||
715 |
} |
||
716 |
|||
717 |
✓✓ | 119046 |
if (val != NULL) { |
718 |
✓✓ | 96895 |
if (vp->flag&INTEGER) { |
719 |
/* do not zero base before assignment */ |
||
720 |
14182 |
setstr(vp, val, KSH_UNWIND_ERROR | 0x4); |
|
721 |
/* Done after assignment to override default */ |
||
722 |
✓✓ | 14172 |
if (base > 0) |
723 |
14 |
vp->type = base; |
|
724 |
} else |
||
725 |
/* setstr can't fail (readonly check already done) */ |
||
726 |
82713 |
setstr(vp, val, KSH_RETURN_ERROR | 0x4); |
|
727 |
} |
||
728 |
|||
729 |
/* only x[0] is ever exported, so use vpbase */ |
||
730 |
✓✓ | 119036 |
if ((vpbase->flag&EXPORT) && !(vpbase->flag&INTEGER) && |
731 |
vpbase->type == 0) |
||
732 |
✓✓ | 272 |
export(vpbase, (vpbase->flag&ISSET) ? vpbase->val.s : null); |
733 |
|||
734 |
119036 |
return vp; |
|
735 |
} |
||
736 |
|||
737 |
/* Unset a variable. array_ref is set if there was an array reference in |
||
738 |
* the name lookup (eg, x[2]). |
||
739 |
*/ |
||
740 |
void |
||
741 |
unset(struct tbl *vp, int array_ref) |
||
742 |
311 |
{ |
|
743 |
✓✓ | 311 |
if (vp->flag & ALLOC) |
744 |
205 |
afree(vp->val.s, vp->areap); |
|
745 |
✗✓ | 311 |
if ((vp->flag & ARRAY) && !array_ref) { |
746 |
struct tbl *a, *tmp; |
||
747 |
|||
748 |
/* Free up entire array */ |
||
749 |
for (a = vp->u.array; a; ) { |
||
750 |
tmp = a; |
||
751 |
a = a->u.array; |
||
752 |
if (tmp->flag & ALLOC) |
||
753 |
afree(tmp->val.s, tmp->areap); |
||
754 |
afree(tmp, tmp->areap); |
||
755 |
} |
||
756 |
vp->u.array = NULL; |
||
757 |
} |
||
758 |
/* If foo[0] is being unset, the remainder of the array is kept... */ |
||
759 |
✗✓ | 311 |
vp->flag &= SPECIAL | (array_ref ? ARRAY|DEFINED : 0); |
760 |
✓✓ | 311 |
if (vp->flag & SPECIAL) |
761 |
27 |
unsetspec(vp); /* responsible for `unspecial'ing var */ |
|
762 |
311 |
} |
|
763 |
|||
764 |
/* return a pointer to the first char past a legal variable name (returns the |
||
765 |
* argument if there is no legal name, returns * a pointer to the terminating |
||
766 |
* null if whole string is legal). |
||
767 |
*/ |
||
768 |
char * |
||
769 |
skip_varname(const char *s, int aok) |
||
770 |
456779 |
{ |
|
771 |
int alen; |
||
772 |
|||
773 |
✓✗✓✓ |
456779 |
if (s && letter(*s)) { |
774 |
✓✓✓✓ ✓✓ |
2633523 |
while (*++s && letnum(*s)) |
775 |
; |
||
776 |
✓✓✗✓ ✗✗ |
452454 |
if (aok && *s == '[' && (alen = array_ref_len(s))) |
777 |
s += alen; |
||
778 |
} |
||
779 |
456779 |
return (char *) s; |
|
780 |
} |
||
781 |
|||
782 |
/* Return a pointer to the first character past any legal variable name. */ |
||
783 |
char * |
||
784 |
skip_wdvarname(const char *s, |
||
785 |
int aok) /* skip array de-reference? */ |
||
786 |
72717 |
{ |
|
787 |
✓✓✓✓ |
72717 |
if (s[0] == CHAR && letter(s[1])) { |
788 |
do { |
||
789 |
406305 |
s += 2; |
|
790 |
✓✓✓✓ ✓✓ |
406305 |
} while (s[0] == CHAR && letnum(s[1])); |
791 |
✓✓✓✓ ✓✓ |
62375 |
if (aok && s[0] == CHAR && s[1] == '[') { |
792 |
/* skip possible array de-reference */ |
||
793 |
31 |
const char *p = s; |
|
794 |
char c; |
||
795 |
31 |
int depth = 0; |
|
796 |
|||
797 |
while (1) { |
||
798 |
✗✓ | 313 |
if (p[0] != CHAR) |
799 |
break; |
||
800 |
313 |
c = p[1]; |
|
801 |
313 |
p += 2; |
|
802 |
✓✓ | 313 |
if (c == '[') |
803 |
52 |
depth++; |
|
804 |
✓✓✓✓ |
261 |
else if (c == ']' && --depth == 0) { |
805 |
31 |
s = p; |
|
806 |
31 |
break; |
|
807 |
} |
||
808 |
} |
||
809 |
} |
||
810 |
} |
||
811 |
72717 |
return (char *) s; |
|
812 |
} |
||
813 |
|||
814 |
/* Check if coded string s is a variable name */ |
||
815 |
int |
||
816 |
is_wdvarname(const char *s, int aok) |
||
817 |
2210 |
{ |
|
818 |
2210 |
char *p = skip_wdvarname(s, aok); |
|
819 |
|||
820 |
✓✓✓✓ |
2210 |
return p != s && p[0] == EOS; |
821 |
} |
||
822 |
|||
823 |
/* Check if coded string s is a variable assignment */ |
||
824 |
int |
||
825 |
is_wdvarassign(const char *s) |
||
826 |
70507 |
{ |
|
827 |
70507 |
char *p = skip_wdvarname(s, true); |
|
828 |
|||
829 |
✓✓✓✓ ✓✓ |
70507 |
return p != s && p[0] == CHAR && p[1] == '='; |
830 |
} |
||
831 |
|||
832 |
/* |
||
833 |
* Make the exported environment from the exported names in the dictionary. |
||
834 |
*/ |
||
835 |
char ** |
||
836 |
makenv(void) |
||
837 |
10638 |
{ |
|
838 |
struct block *l; |
||
839 |
XPtrV env; |
||
840 |
struct tbl *vp, **vpp; |
||
841 |
int i; |
||
842 |
|||
843 |
10638 |
XPinit(env, 64); |
|
844 |
✓✓ | 35373 |
for (l = genv->loc; l != NULL; l = l->next) |
845 |
✓✓ | 3296494 |
for (vpp = l->vars.tbls, i = l->vars.size; --i >= 0; ) |
846 |
✓✓✓✓ |
3247024 |
if ((vp = *vpp++) != NULL && |
847 |
(vp->flag&(ISSET|EXPORT)) == (ISSET|EXPORT)) { |
||
848 |
struct block *l2; |
||
849 |
struct tbl *vp2; |
||
850 |
246632 |
unsigned int h = hash(vp->name); |
|
851 |
|||
852 |
/* unexport any redefined instances */ |
||
853 |
✓✓ | 246946 |
for (l2 = l->next; l2 != NULL; l2 = l2->next) { |
854 |
314 |
vp2 = ktsearch(&l2->vars, vp->name, h); |
|
855 |
✓✓ | 314 |
if (vp2 != NULL) |
856 |
47 |
vp2->flag &= ~EXPORT; |
|
857 |
} |
||
858 |
✗✓ | 246632 |
if ((vp->flag&INTEGER)) { |
859 |
/* integer to string */ |
||
860 |
char *val; |
||
861 |
val = str_val(vp); |
||
862 |
vp->flag &= ~(INTEGER|RDONLY); |
||
863 |
/* setstr can't fail here */ |
||
864 |
setstr(vp, val, KSH_RETURN_ERROR); |
||
865 |
} |
||
866 |
✗✓ | 246632 |
XPput(env, vp->val.s); |
867 |
} |
||
868 |
✗✓ | 10638 |
XPput(env, NULL); |
869 |
10638 |
return (char **) XPclose(env); |
|
870 |
} |
||
871 |
|||
872 |
/* |
||
873 |
* Called after a fork in parent to bump the random number generator. |
||
874 |
* Done to ensure children will not get the same random number sequence |
||
875 |
* if the parent doesn't use $RANDOM. |
||
876 |
*/ |
||
877 |
void |
||
878 |
change_random(void) |
||
879 |
14173 |
{ |
|
880 |
14173 |
rand(); |
|
881 |
14173 |
} |
|
882 |
|||
883 |
/* |
||
884 |
* handle special variables with side effects - PATH, SECONDS. |
||
885 |
*/ |
||
886 |
|||
887 |
/* Test if name is a special parameter */ |
||
888 |
static int |
||
889 |
special(const char *name) |
||
890 |
203627 |
{ |
|
891 |
struct tbl *tp; |
||
892 |
|||
893 |
203627 |
tp = ktsearch(&specials, name, hash(name)); |
|
894 |
✓✓✓✗ |
203627 |
return tp && (tp->flag & ISSET) ? tp->type : V_NONE; |
895 |
} |
||
896 |
|||
897 |
/* Make a variable non-special */ |
||
898 |
static void |
||
899 |
unspecial(const char *name) |
||
900 |
4 |
{ |
|
901 |
struct tbl *tp; |
||
902 |
|||
903 |
4 |
tp = ktsearch(&specials, name, hash(name)); |
|
904 |
✓✗ | 4 |
if (tp) |
905 |
4 |
ktdelete(tp); |
|
906 |
4 |
} |
|
907 |
|||
908 |
static time_t seconds; /* time SECONDS last set */ |
||
909 |
static int user_lineno; /* what user set $LINENO to */ |
||
910 |
|||
911 |
static void |
||
912 |
getspec(struct tbl *vp) |
||
913 |
26293 |
{ |
|
914 |
✓✗✗✓ ✓✓ |
26293 |
switch (special(vp->name)) { |
915 |
case V_SECONDS: |
||
916 |
3553 |
vp->flag &= ~SPECIAL; |
|
917 |
/* On start up the value of SECONDS is used before seconds |
||
918 |
* has been set - don't do anything in this case |
||
919 |
* (see initcoms[] in main.c). |
||
920 |
*/ |
||
921 |
✓✓ | 3553 |
if (vp->flag & ISSET) |
922 |
28 |
setint(vp, (long)(time(NULL) - seconds)); /* XXX 2038 */ |
|
923 |
3553 |
vp->flag |= SPECIAL; |
|
924 |
3553 |
break; |
|
925 |
case V_RANDOM: |
||
926 |
vp->flag &= ~SPECIAL; |
||
927 |
setint(vp, (long) (rand() & 0x7fff)); |
||
928 |
vp->flag |= SPECIAL; |
||
929 |
break; |
||
930 |
#ifdef HISTORY |
||
931 |
case V_HISTSIZE: |
||
932 |
vp->flag &= ~SPECIAL; |
||
933 |
setint(vp, (long) histsize); |
||
934 |
vp->flag |= SPECIAL; |
||
935 |
break; |
||
936 |
#endif /* HISTORY */ |
||
937 |
case V_OPTIND: |
||
938 |
66 |
vp->flag &= ~SPECIAL; |
|
939 |
66 |
setint(vp, (long) user_opt.uoptind); |
|
940 |
66 |
vp->flag |= SPECIAL; |
|
941 |
66 |
break; |
|
942 |
case V_LINENO: |
||
943 |
1107 |
vp->flag &= ~SPECIAL; |
|
944 |
1107 |
setint(vp, (long) current_lineno + user_lineno); |
|
945 |
1107 |
vp->flag |= SPECIAL; |
|
946 |
break; |
||
947 |
} |
||
948 |
26293 |
} |
|
949 |
|||
950 |
static void |
||
951 |
setspec(struct tbl *vp) |
||
952 |
42420 |
{ |
|
953 |
char *s; |
||
954 |
|||
955 |
✓✓✓✓ ✓✓✓✗ ✗✓✓✗ ✓✓✓✓ ✓✓ |
42420 |
switch (special(vp->name)) { |
956 |
case V_PATH: |
||
957 |
7080 |
afree(path, APERM); |
|
958 |
7080 |
path = str_save(str_val(vp), APERM); |
|
959 |
7080 |
flushcom(1); /* clear tracked aliases */ |
|
960 |
7080 |
break; |
|
961 |
case V_IFS: |
||
962 |
3829 |
setctypes(s = str_val(vp), C_IFS); |
|
963 |
3829 |
ifs0 = *s; |
|
964 |
3829 |
break; |
|
965 |
case V_OPTIND: |
||
966 |
7054 |
vp->flag &= ~SPECIAL; |
|
967 |
7054 |
getopts_reset((int) intval(vp)); |
|
968 |
7054 |
vp->flag |= SPECIAL; |
|
969 |
7054 |
break; |
|
970 |
case V_POSIXLY_CORRECT: |
||
971 |
2 |
change_flag(FPOSIX, OF_SPECIAL, 1); |
|
972 |
2 |
break; |
|
973 |
case V_TMPDIR: |
||
974 |
✗✓ | 24 |
if (tmpdir) { |
975 |
afree(tmpdir, APERM); |
||
976 |
tmpdir = NULL; |
||
977 |
} |
||
978 |
/* Use tmpdir iff it is an absolute path, is writable and |
||
979 |
* searchable and is a directory... |
||
980 |
*/ |
||
981 |
{ |
||
982 |
struct stat statb; |
||
983 |
|||
984 |
24 |
s = str_val(vp); |
|
985 |
✓✗✓✗ ✓✗✓✗ |
24 |
if (s[0] == '/' && access(s, W_OK|X_OK) == 0 && |
986 |
stat(s, &statb) == 0 && S_ISDIR(statb.st_mode)) |
||
987 |
24 |
tmpdir = str_save(s, APERM); |
|
988 |
} |
||
989 |
break; |
||
990 |
#ifdef HISTORY |
||
991 |
case V_HISTSIZE: |
||
992 |
4 |
vp->flag &= ~SPECIAL; |
|
993 |
4 |
sethistsize((int) intval(vp)); |
|
994 |
4 |
vp->flag |= SPECIAL; |
|
995 |
4 |
break; |
|
996 |
case V_HISTFILE: |
||
997 |
42 |
sethistfile(str_val(vp)); |
|
998 |
42 |
break; |
|
999 |
#endif /* HISTORY */ |
||
1000 |
#ifdef EDIT |
||
1001 |
case V_VISUAL: |
||
1002 |
set_editmode(str_val(vp)); |
||
1003 |
break; |
||
1004 |
case V_EDITOR: |
||
1005 |
if (!(global("VISUAL")->flag & ISSET)) |
||
1006 |
set_editmode(str_val(vp)); |
||
1007 |
break; |
||
1008 |
case V_COLUMNS: |
||
1009 |
{ |
||
1010 |
long l; |
||
1011 |
|||
1012 |
✗✓ | 168 |
if (getint(vp, &l, false) == -1) { |
1013 |
x_cols = MIN_COLS; |
||
1014 |
break; |
||
1015 |
} |
||
1016 |
✗✓ | 168 |
if (l <= MIN_COLS || l > INT_MAX) |
1017 |
x_cols = MIN_COLS; |
||
1018 |
else |
||
1019 |
168 |
x_cols = l; |
|
1020 |
} |
||
1021 |
break; |
||
1022 |
#endif /* EDIT */ |
||
1023 |
case V_MAIL: |
||
1024 |
3055 |
mbset(str_val(vp)); |
|
1025 |
3055 |
break; |
|
1026 |
case V_MAILPATH: |
||
1027 |
mpset(str_val(vp)); |
||
1028 |
break; |
||
1029 |
case V_MAILCHECK: |
||
1030 |
7050 |
vp->flag &= ~SPECIAL; |
|
1031 |
7050 |
mcset(intval(vp)); |
|
1032 |
7050 |
vp->flag |= SPECIAL; |
|
1033 |
7050 |
break; |
|
1034 |
case V_RANDOM: |
||
1035 |
6 |
vp->flag &= ~SPECIAL; |
|
1036 |
6 |
srand_deterministic((unsigned int)intval(vp)); |
|
1037 |
6 |
vp->flag |= SPECIAL; |
|
1038 |
6 |
break; |
|
1039 |
case V_SECONDS: |
||
1040 |
7050 |
vp->flag &= ~SPECIAL; |
|
1041 |
7050 |
seconds = time(NULL) - intval(vp); /* XXX 2038 */ |
|
1042 |
7050 |
vp->flag |= SPECIAL; |
|
1043 |
7050 |
break; |
|
1044 |
case V_TMOUT: |
||
1045 |
/* at&t ksh seems to do this (only listen if integer) */ |
||
1046 |
✓✗ | 7050 |
if (vp->flag & INTEGER) |
1047 |
7050 |
ksh_tmout = vp->val.i >= 0 ? vp->val.i : 0; |
|
1048 |
break; |
||
1049 |
case V_LINENO: |
||
1050 |
4 |
vp->flag &= ~SPECIAL; |
|
1051 |
/* The -1 is because line numbering starts at 1. */ |
||
1052 |
4 |
user_lineno = (unsigned int) intval(vp) - current_lineno - 1; |
|
1053 |
4 |
vp->flag |= SPECIAL; |
|
1054 |
break; |
||
1055 |
} |
||
1056 |
42420 |
} |
|
1057 |
|||
1058 |
static void |
||
1059 |
unsetspec(struct tbl *vp) |
||
1060 |
31 |
{ |
|
1061 |
✗✗✓✓ ✓✓✓ |
31 |
switch (special(vp->name)) { |
1062 |
case V_PATH: |
||
1063 |
afree(path, APERM); |
||
1064 |
path = str_save(def_path, APERM); |
||
1065 |
flushcom(1); /* clear tracked aliases */ |
||
1066 |
break; |
||
1067 |
case V_IFS: |
||
1068 |
setctypes(" \t\n", C_IFS); |
||
1069 |
ifs0 = ' '; |
||
1070 |
break; |
||
1071 |
case V_TMPDIR: |
||
1072 |
/* should not become unspecial */ |
||
1073 |
✓✗ | 4 |
if (tmpdir) { |
1074 |
4 |
afree(tmpdir, APERM); |
|
1075 |
4 |
tmpdir = NULL; |
|
1076 |
} |
||
1077 |
break; |
||
1078 |
case V_MAIL: |
||
1079 |
14 |
mbset(NULL); |
|
1080 |
14 |
break; |
|
1081 |
case V_MAILPATH: |
||
1082 |
8 |
mpset(NULL); |
|
1083 |
8 |
break; |
|
1084 |
case V_LINENO: |
||
1085 |
case V_MAILCHECK: /* at&t ksh leaves previous value in place */ |
||
1086 |
case V_RANDOM: |
||
1087 |
case V_SECONDS: |
||
1088 |
case V_TMOUT: /* at&t ksh leaves previous value in place */ |
||
1089 |
4 |
unspecial(vp->name); |
|
1090 |
break; |
||
1091 |
|||
1092 |
/* at&t ksh man page says OPTIND, OPTARG and _ lose special meaning, |
||
1093 |
* but OPTARG does not (still set by getopts) and _ is also still |
||
1094 |
* set in various places. |
||
1095 |
* Don't know what at&t does for: |
||
1096 |
* MAIL, MAILPATH, HISTSIZE, HISTFILE, |
||
1097 |
* Unsetting these in at&t ksh does not loose the `specialness': |
||
1098 |
* no effect: IFS, COLUMNS, PATH, TMPDIR, |
||
1099 |
* VISUAL, EDITOR, |
||
1100 |
* pdkshisms: no effect: |
||
1101 |
* POSIXLY_CORRECT (use set +o posix instead) |
||
1102 |
*/ |
||
1103 |
} |
||
1104 |
31 |
} |
|
1105 |
|||
1106 |
/* |
||
1107 |
* Search for (and possibly create) a table entry starting with |
||
1108 |
* vp, indexed by val. |
||
1109 |
*/ |
||
1110 |
static struct tbl * |
||
1111 |
arraysearch(struct tbl *vp, int val) |
||
1112 |
43 |
{ |
|
1113 |
struct tbl *prev, *curr, *new; |
||
1114 |
43 |
size_t namelen = strlen(vp->name) + 1; |
|
1115 |
|||
1116 |
43 |
vp->flag |= ARRAY|DEFINED; |
|
1117 |
43 |
vp->index = 0; |
|
1118 |
/* The table entry is always [0] */ |
||
1119 |
✓✓ | 43 |
if (val == 0) |
1120 |
10 |
return vp; |
|
1121 |
33 |
prev = vp; |
|
1122 |
33 |
curr = vp->u.array; |
|
1123 |
✓✓✓✓ |
100 |
while (curr && curr->index < val) { |
1124 |
34 |
prev = curr; |
|
1125 |
34 |
curr = curr->u.array; |
|
1126 |
} |
||
1127 |
✓✓✓✓ |
33 |
if (curr && curr->index == val) { |
1128 |
✓✗ | 4 |
if (curr->flag&ISSET) |
1129 |
4 |
return curr; |
|
1130 |
else |
||
1131 |
new = curr; |
||
1132 |
} else |
||
1133 |
29 |
new = alloc(sizeof(struct tbl) + namelen, |
|
1134 |
vp->areap); |
||
1135 |
29 |
strlcpy(new->name, vp->name, namelen); |
|
1136 |
29 |
new->flag = vp->flag & ~(ALLOC|DEFINED|ISSET|SPECIAL); |
|
1137 |
29 |
new->type = vp->type; |
|
1138 |
29 |
new->areap = vp->areap; |
|
1139 |
29 |
new->u2.field = vp->u2.field; |
|
1140 |
29 |
new->index = val; |
|
1141 |
✓✗ | 29 |
if (curr != new) { /* not reusing old array entry */ |
1142 |
29 |
prev->u.array = new; |
|
1143 |
29 |
new->u.array = curr; |
|
1144 |
} |
||
1145 |
29 |
return new; |
|
1146 |
} |
||
1147 |
|||
1148 |
/* Return the length of an array reference (eg, [1+2]) - cp is assumed |
||
1149 |
* to point to the open bracket. Returns 0 if there is no matching closing |
||
1150 |
* bracket. |
||
1151 |
*/ |
||
1152 |
int |
||
1153 |
array_ref_len(const char *cp) |
||
1154 |
36 |
{ |
|
1155 |
36 |
const char *s = cp; |
|
1156 |
int c; |
||
1157 |
36 |
int depth = 0; |
|
1158 |
|||
1159 |
✓✗✓✓ ✓✓ |
312 |
while ((c = *s++) && (c != ']' || --depth)) |
1160 |
✓✓ | 240 |
if (c == '[') |
1161 |
48 |
depth++; |
|
1162 |
✗✓ | 36 |
if (!c) |
1163 |
return 0; |
||
1164 |
36 |
return s - cp; |
|
1165 |
} |
||
1166 |
|||
1167 |
/* |
||
1168 |
* Make a copy of the base of an array name |
||
1169 |
*/ |
||
1170 |
char * |
||
1171 |
arrayname(const char *str) |
||
1172 |
32 |
{ |
|
1173 |
const char *p; |
||
1174 |
|||
1175 |
✗✓ | 32 |
if ((p = strchr(str, '[')) == 0) |
1176 |
/* Shouldn't happen, but why worry? */ |
||
1177 |
return (char *) str; |
||
1178 |
|||
1179 |
32 |
return str_nsave(str, p - str, ATEMP); |
|
1180 |
} |
||
1181 |
|||
1182 |
/* Set (or overwrite, if !reset) the array variable var to the values in vals. |
||
1183 |
*/ |
||
1184 |
void |
||
1185 |
set_array(const char *var, int reset, char **vals) |
||
1186 |
10 |
{ |
|
1187 |
struct tbl *vp, *vq; |
||
1188 |
int i; |
||
1189 |
|||
1190 |
/* to get local array, use "typeset foo; set -A foo" */ |
||
1191 |
10 |
vp = global(var); |
|
1192 |
|||
1193 |
/* Note: at&t ksh allows set -A but not set +A of a read-only var */ |
||
1194 |
✗✓ | 10 |
if ((vp->flag&RDONLY)) |
1195 |
errorf("%s: is read only", var); |
||
1196 |
/* This code is quite non-optimal */ |
||
1197 |
✓✗ | 10 |
if (reset > 0) |
1198 |
/* trash existing values and attributes */ |
||
1199 |
10 |
unset(vp, 0); |
|
1200 |
/* todo: would be nice for assignment to completely succeed or |
||
1201 |
* completely fail. Only really effects integer arrays: |
||
1202 |
* evaluation of some of vals[] may fail... |
||
1203 |
*/ |
||
1204 |
✓✓ | 33 |
for (i = 0; vals[i]; i++) { |
1205 |
23 |
vq = arraysearch(vp, i); |
|
1206 |
/* would be nice to deal with errors here... (see above) */ |
||
1207 |
23 |
setstr(vq, vals[i], KSH_RETURN_ERROR); |
|
1208 |
} |
||
1209 |
10 |
} |
Generated by: GCOVR (Version 3.3) |