1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/*
* loop.c - loop execution
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1997 Paul Falstad
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Paul Falstad or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "zsh.mdh"
#include "loop.pro"
/* # of nested loops we are in */
/**/
int loops;
/* # of continue levels */
/**/
int contflag;
/* # of break levels */
/**/
int breaks;
/**/
int
execfor(Cmd cmd)
{
List list;
Forcmd node;
char *str;
int val = 0;
LinkList args;
node = cmd->u.forcmd;
args = cmd->args;
if (node->condition) {
str = node->name;
singsub(&str);
if (!errflag)
matheval(str);
if (errflag)
return lastval = errflag;
} else if (!node->inflag) {
char **x;
args = newlinklist();
for (x = pparams; *x; x++)
addlinknode(args, ztrdup(*x));
}
lastval = 0;
loops++;
pushheap();
for (;;) {
if (node->condition) {
str = dupstring(node->condition);
singsub(&str);
if (!errflag) {
while (iblank(*str))
str++;
if (*str)
val = matheval(str);
else
val = 1;
}
if (errflag) {
if (breaks)
breaks--;
lastval = 1;
break;
}
if (!val)
break;
} else {
str = (char *) ugetnode(args);
if (!str)
break;
setsparam(node->name, ztrdup(str));
}
list = (List) dupstruct(node->list);
execlist(list, 1, (cmd->flags & CFLAG_EXEC) && empty(args));
if (breaks) {
breaks--;
if (breaks || !contflag)
break;
contflag = 0;
}
if (node->condition && !errflag) {
str = dupstring(node->advance);
singsub(&str);
if (!errflag)
matheval(str);
}
if (errflag) {
if (breaks)
breaks--;
lastval = 1;
break;
}
freeheap();
}
popheap();
loops--;
return lastval;
}
/**/
int
execselect(Cmd cmd)
{
List list;
Forcmd node;
char *str, *s;
LinkList args;
LinkNode n;
int i;
FILE *inp;
node = cmd->u.forcmd;
args = cmd->args;
if (!node->inflag) {
char **x;
args = newlinklist();
for (x = pparams; *x; x++)
addlinknode(args, ztrdup(*x));
}
if (empty(args))
return 1;
loops++;
lastval = 0;
pushheap();
inp = fdopen(dup((SHTTY == -1) ? 0 : SHTTY), "r");
selectlist(args);
for (;;) {
for (;;) {
if (empty(bufstack)) {
if (interact && SHTTY != -1 && isset(USEZLE)) {
isfirstln = 1;
str = (char *)zleread(prompt3, NULL, 0);
} else {
str = promptexpand(prompt3, 0, NULL, NULL);
zputs(str, stderr);
free(str);
fflush(stderr);
str = fgets(zalloc(256), 256, inp);
}
} else
str = (char *)getlinknode(bufstack);
if (!str || errflag) {
if (breaks)
breaks--;
fprintf(stderr, "\n");
fflush(stderr);
goto done;
}
if ((s = strchr(str, '\n')))
*s = '\0';
if (*str)
break;
selectlist(args);
}
setsparam("REPLY", ztrdup(str));
i = atoi(str);
if (!i)
str = "";
else {
for (i--, n = firstnode(args); n && i; incnode(n), i--);
if (n)
str = (char *) getdata(n);
else
str = "";
}
setsparam(node->name, ztrdup(str));
list = (List) dupstruct(node->list);
execlist(list, 1, 0);
freeheap();
if (breaks) {
breaks--;
if (breaks || !contflag)
break;
contflag = 0;
}
if (errflag)
break;
}
done:
popheap();
fclose(inp);
loops--;
return lastval;
}
/* And this is used to print select lists. */
/**/
static void
selectlist(LinkList l)
{
size_t longest = 1, fct, fw = 0, colsz, t0, t1, ct;
LinkNode n;
char **arr, **ap;
trashzle();
ct = countlinknodes(l);
ap = arr = (char **)alloc((countlinknodes(l) + 1) * sizeof(char **));
for (n = (LinkNode) firstnode(l); n; incnode(n))
*ap++ = (char *)getdata(n);
*ap = NULL;
for (ap = arr; *ap; ap++)
if (strlen(*ap) > longest)
longest = strlen(*ap);
t0 = ct;
longest++;
while (t0)
t0 /= 10, longest++;
/* to compensate for added ')' */
fct = (columns - 1) / (longest + 3);
if (fct == 0)
fct = 1;
else
fw = (columns - 1) / fct;
colsz = (ct + fct - 1) / fct;
for (t1 = 0; t1 != colsz; t1++) {
ap = arr + t1;
do {
int t2 = strlen(*ap) + 2, t3;
fprintf(stderr, "%d) %s", t3 = ap - arr + 1, *ap);
while (t3)
t2++, t3 /= 10;
for (; t2 < fw; t2++)
fputc(' ', stderr);
for (t0 = colsz; t0 && *ap; t0--, ap++);
}
while (*ap);
fputc('\n', stderr);
}
/* Below is a simple attempt at doing it the Korn Way..
ap = arr;
t0 = 0;
do {
t0++;
fprintf(stderr,"%d) %s\n",t0,*ap);
ap++;
}
while (*ap);*/
fflush(stderr);
}
/**/
int
execwhile(Cmd cmd)
{
List list;
struct whilecmd *node;
int olderrexit, oldval;
olderrexit = noerrexit;
node = cmd->u.whilecmd;
oldval = 0;
pushheap();
loops++;
for (;;) {
list = (List) dupstruct(node->cont);
noerrexit = 1;
execlist(list, 1, 0);
noerrexit = olderrexit;
if (!((lastval == 0) ^ node->cond)) {
if (breaks)
breaks--;
lastval = oldval;
break;
}
list = (List) dupstruct(node->loop);
execlist(list, 1, 0);
if (breaks) {
breaks--;
if (breaks || !contflag)
break;
contflag = 0;
}
freeheap();
if (errflag) {
lastval = 1;
break;
}
oldval = lastval;
}
popheap();
loops--;
return lastval;
}
/**/
int
execrepeat(Cmd cmd)
{
List list;
int count;
lastval = 0;
if (empty(cmd->args) || nextnode(firstnode(cmd->args))) {
zerr("bad argument for repeat", NULL, 0);
return 1;
}
count = atoi(peekfirst(cmd->args));
pushheap();
loops++;
while (count--) {
list = (List) dupstruct(cmd->u.list);
execlist(list, 1, 0);
freeheap();
if (breaks) {
breaks--;
if (breaks || !contflag)
break;
contflag = 0;
}
if (errflag) {
lastval = 1;
break;
}
}
popheap();
loops--;
return lastval;
}
/**/
int
execif(Cmd cmd)
{
struct ifcmd *node;
int olderrexit;
List *i, *t;
olderrexit = noerrexit;
node = cmd->u.ifcmd;
i = node->ifls;
t = node->thenls;
if (!noerrexit)
noerrexit = 1;
while (*i) {
execlist(*i, 1, 0);
if (!lastval)
break;
i++;
t++;
}
noerrexit = olderrexit;
if (*t)
execlist(*t, 1, cmd->flags & CFLAG_EXEC);
else
lastval = 0;
return lastval;
}
/**/
int
execcase(Cmd cmd)
{
struct casecmd *node;
char *word;
List *l;
char **p;
node = cmd->u.casecmd;
l = node->lists;
p = node->pats;
word = *p++;
singsub(&word);
untokenize(word);
lastval = 0;
if (node) {
while (*p) {
char *pat = *p + 1;
singsub(&pat);
if (matchpat(word, pat)) {
do {
execlist(*l++, 1, **p == ';' && (cmd->flags & CFLAG_EXEC));
} while(**p++ == '&' && *p);
break;
}
p++;
l++;
}
}
return lastval;
}
|