eval.c revision 1.13 1 1.13 christos /* $NetBSD: eval.c,v 1.13 2011/08/14 10:40:25 christos Exp $ */
2 1.2 tls
3 1.1 jtc /*
4 1.1 jtc * Expansion - quoting, separation, substitution, globbing
5 1.1 jtc */
6 1.4 agc #include <sys/cdefs.h>
7 1.4 agc
8 1.4 agc #ifndef lint
9 1.13 christos __RCSID("$NetBSD: eval.c,v 1.13 2011/08/14 10:40:25 christos Exp $");
10 1.4 agc #endif
11 1.4 agc
12 1.1 jtc
13 1.1 jtc #include "sh.h"
14 1.1 jtc #include <pwd.h>
15 1.1 jtc #include "ksh_dir.h"
16 1.1 jtc #include "ksh_stat.h"
17 1.1 jtc
18 1.1 jtc /*
19 1.1 jtc * string expansion
20 1.1 jtc *
21 1.1 jtc * first pass: quoting, IFS separation, ~, ${}, $() and $(()) substitution.
22 1.1 jtc * second pass: alternation ({,}), filename expansion (*?[]).
23 1.1 jtc */
24 1.1 jtc
25 1.1 jtc /* expansion generator state */
26 1.1 jtc typedef struct Expand {
27 1.1 jtc /* int type; */ /* see expand() */
28 1.1 jtc const char *str; /* string */
29 1.1 jtc union {
30 1.1 jtc const char **strv;/* string[] */
31 1.1 jtc struct shf *shf;/* file */
32 1.1 jtc } u; /* source */
33 1.1 jtc struct tbl *var; /* variable in ${var..} */
34 1.1 jtc short split; /* split "$@" / call waitlast $() */
35 1.1 jtc } Expand;
36 1.1 jtc
37 1.1 jtc #define XBASE 0 /* scanning original */
38 1.1 jtc #define XSUB 1 /* expanding ${} string */
39 1.1 jtc #define XARGSEP 2 /* ifs0 between "$*" */
40 1.1 jtc #define XARG 3 /* expanding $*, $@ */
41 1.1 jtc #define XCOM 4 /* expanding $() */
42 1.1 jtc #define XNULLSUB 5 /* "$@" when $# is 0 (don't generate word) */
43 1.1 jtc
44 1.1 jtc /* States used for field splitting */
45 1.1 jtc #define IFS_WORD 0 /* word has chars (or quotes) */
46 1.1 jtc #define IFS_WS 1 /* have seen IFS white-space */
47 1.1 jtc #define IFS_NWS 2 /* have seen IFS non-white-space */
48 1.1 jtc
49 1.3 hubertf static int varsub ARGS((Expand *xp, char *sp, char *word, int *stypep, int *slenp));
50 1.1 jtc static int comsub ARGS((Expand *xp, char *cp));
51 1.1 jtc static char *trimsub ARGS((char *str, char *pat, int how));
52 1.1 jtc static void glob ARGS((char *cp, XPtrV *wp, int markdirs));
53 1.1 jtc static void globit ARGS((XString *xs, char **xpp, char *sp, XPtrV *wp,
54 1.1 jtc int check));
55 1.1 jtc static char *maybe_expand_tilde ARGS((char *p, XString *dsp, char **dpp,
56 1.1 jtc int isassign));
57 1.1 jtc static char *tilde ARGS((char *acp));
58 1.1 jtc static char *homedir ARGS((char *name));
59 1.1 jtc #ifdef BRACE_EXPAND
60 1.1 jtc static void alt_expand ARGS((XPtrV *wp, char *start, char *exp_start,
61 1.1 jtc char *end, int fdo));
62 1.1 jtc #endif
63 1.1 jtc
64 1.1 jtc /* compile and expand word */
65 1.1 jtc char *
66 1.1 jtc substitute(cp, f)
67 1.1 jtc const char *cp;
68 1.1 jtc int f;
69 1.1 jtc {
70 1.1 jtc struct source *s, *sold;
71 1.1 jtc
72 1.1 jtc sold = source;
73 1.1 jtc s = pushs(SWSTR, ATEMP);
74 1.1 jtc s->start = s->str = cp;
75 1.1 jtc source = s;
76 1.1 jtc if (yylex(ONEWORD) != LWORD)
77 1.1 jtc internal_errorf(1, "substitute");
78 1.1 jtc source = sold;
79 1.1 jtc afree(s, ATEMP);
80 1.1 jtc return evalstr(yylval.cp, f);
81 1.1 jtc }
82 1.1 jtc
83 1.1 jtc /*
84 1.1 jtc * expand arg-list
85 1.1 jtc */
86 1.1 jtc char **
87 1.1 jtc eval(ap, f)
88 1.1 jtc register char **ap;
89 1.1 jtc int f;
90 1.1 jtc {
91 1.1 jtc XPtrV w;
92 1.1 jtc
93 1.1 jtc if (*ap == NULL)
94 1.1 jtc return ap;
95 1.1 jtc XPinit(w, 32);
96 1.1 jtc XPput(w, NULL); /* space for shell name */
97 1.1 jtc #ifdef SHARPBANG
98 1.1 jtc XPput(w, NULL); /* and space for one arg */
99 1.1 jtc #endif
100 1.1 jtc while (*ap != NULL)
101 1.1 jtc expand(*ap++, &w, f);
102 1.1 jtc XPput(w, NULL);
103 1.1 jtc #ifdef SHARPBANG
104 1.1 jtc return (char **) XPclose(w) + 2;
105 1.1 jtc #else
106 1.1 jtc return (char **) XPclose(w) + 1;
107 1.1 jtc #endif
108 1.1 jtc }
109 1.1 jtc
110 1.1 jtc /*
111 1.1 jtc * expand string
112 1.1 jtc */
113 1.1 jtc char *
114 1.1 jtc evalstr(cp, f)
115 1.1 jtc char *cp;
116 1.1 jtc int f;
117 1.1 jtc {
118 1.1 jtc XPtrV w;
119 1.1 jtc
120 1.1 jtc XPinit(w, 1);
121 1.1 jtc expand(cp, &w, f);
122 1.1 jtc cp = (XPsize(w) == 0) ? null : (char*) *XPptrv(w);
123 1.1 jtc XPfree(w);
124 1.1 jtc return cp;
125 1.1 jtc }
126 1.1 jtc
127 1.1 jtc /*
128 1.1 jtc * expand string - return only one component
129 1.1 jtc * used from iosetup to expand redirection files
130 1.1 jtc */
131 1.1 jtc char *
132 1.1 jtc evalonestr(cp, f)
133 1.1 jtc register char *cp;
134 1.1 jtc int f;
135 1.1 jtc {
136 1.1 jtc XPtrV w;
137 1.1 jtc
138 1.1 jtc XPinit(w, 1);
139 1.1 jtc expand(cp, &w, f);
140 1.1 jtc switch (XPsize(w)) {
141 1.1 jtc case 0:
142 1.1 jtc cp = null;
143 1.1 jtc break;
144 1.1 jtc case 1:
145 1.1 jtc cp = (char*) *XPptrv(w);
146 1.1 jtc break;
147 1.1 jtc default:
148 1.1 jtc cp = evalstr(cp, f&~DOGLOB);
149 1.1 jtc break;
150 1.1 jtc }
151 1.1 jtc XPfree(w);
152 1.1 jtc return cp;
153 1.1 jtc }
154 1.1 jtc
155 1.1 jtc /* for nested substitution: ${var:=$var2} */
156 1.1 jtc typedef struct SubType {
157 1.1 jtc short stype; /* [=+-?%#] action after expanded word */
158 1.1 jtc short base; /* begin position of expanded word */
159 1.1 jtc short f; /* saved value of f (DOPAT, etc) */
160 1.1 jtc struct tbl *var; /* variable for ${var..} */
161 1.1 jtc short quote; /* saved value of quote (for ${..[%#]..}) */
162 1.1 jtc struct SubType *prev; /* old type */
163 1.1 jtc struct SubType *next; /* poped type (to avoid re-allocating) */
164 1.1 jtc } SubType;
165 1.1 jtc
166 1.1 jtc void
167 1.1 jtc expand(cp, wp, f)
168 1.1 jtc char *cp; /* input word */
169 1.1 jtc register XPtrV *wp; /* output words */
170 1.1 jtc int f; /* DO* flags */
171 1.1 jtc {
172 1.1 jtc register int UNINITIALIZED(c);
173 1.1 jtc register int type; /* expansion type */
174 1.1 jtc register int quote = 0; /* quoted */
175 1.1 jtc XString ds; /* destination string */
176 1.1 jtc register char *dp, *sp; /* dest., source */
177 1.1 jtc int fdo, word; /* second pass flags; have word */
178 1.5 mycroft int doblank; /* field splitting of parameter/command subst */
179 1.1 jtc Expand x; /* expansion variables */
180 1.1 jtc SubType st_head, *st;
181 1.1 jtc int UNINITIALIZED(newlines); /* For trailing newlines in COMSUB */
182 1.1 jtc int saw_eq, tilde_ok;
183 1.1 jtc int make_magic;
184 1.5 mycroft size_t len;
185 1.1 jtc
186 1.6 mrg x.split = 0; /* XXX gcc */
187 1.6 mrg x.str = NULL; /* XXX gcc */
188 1.13 christos x.u.strv = NULL;/* XXX gcc */
189 1.1 jtc if (cp == NULL)
190 1.1 jtc internal_errorf(1, "expand(NULL)");
191 1.1 jtc /* for alias, readonly, set, typeset commands */
192 1.1 jtc if ((f & DOVACHECK) && is_wdvarassign(cp)) {
193 1.1 jtc f &= ~(DOVACHECK|DOBLANK|DOGLOB|DOTILDE);
194 1.1 jtc f |= DOASNTILDE;
195 1.1 jtc }
196 1.1 jtc if (Flag(FNOGLOB))
197 1.1 jtc f &= ~DOGLOB;
198 1.1 jtc if (Flag(FMARKDIRS))
199 1.1 jtc f |= DOMARKDIRS;
200 1.1 jtc #ifdef BRACE_EXPAND
201 1.1 jtc if (Flag(FBRACEEXPAND) && (f & DOGLOB))
202 1.1 jtc f |= DOBRACE_;
203 1.1 jtc #endif /* BRACE_EXPAND */
204 1.1 jtc
205 1.1 jtc Xinit(ds, dp, 128, ATEMP); /* init dest. string */
206 1.1 jtc type = XBASE;
207 1.1 jtc sp = cp;
208 1.1 jtc fdo = 0;
209 1.1 jtc saw_eq = 0;
210 1.1 jtc tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0; /* must be 1/0 */
211 1.1 jtc doblank = 0;
212 1.1 jtc make_magic = 0;
213 1.1 jtc word = (f&DOBLANK) ? IFS_WS : IFS_WORD;
214 1.1 jtc st_head.next = (SubType *) 0;
215 1.1 jtc st = &st_head;
216 1.1 jtc
217 1.1 jtc while (1) {
218 1.1 jtc Xcheck(ds, dp);
219 1.1 jtc
220 1.1 jtc switch (type) {
221 1.1 jtc case XBASE: /* original prefixed string */
222 1.1 jtc c = *sp++;
223 1.1 jtc switch (c) {
224 1.1 jtc case EOS:
225 1.1 jtc c = 0;
226 1.1 jtc break;
227 1.1 jtc case CHAR:
228 1.1 jtc c = *sp++;
229 1.1 jtc break;
230 1.1 jtc case QCHAR:
231 1.1 jtc quote |= 2; /* temporary quote */
232 1.1 jtc c = *sp++;
233 1.1 jtc break;
234 1.1 jtc case OQUOTE:
235 1.1 jtc word = IFS_WORD;
236 1.1 jtc tilde_ok = 0;
237 1.1 jtc quote = 1;
238 1.1 jtc continue;
239 1.1 jtc case CQUOTE:
240 1.1 jtc quote = 0;
241 1.1 jtc continue;
242 1.1 jtc case COMSUB:
243 1.1 jtc tilde_ok = 0;
244 1.1 jtc if (f & DONTRUNCOMMAND) {
245 1.1 jtc word = IFS_WORD;
246 1.1 jtc *dp++ = '$'; *dp++ = '(';
247 1.1 jtc while (*sp != '\0') {
248 1.1 jtc Xcheck(ds, dp);
249 1.1 jtc *dp++ = *sp++;
250 1.1 jtc }
251 1.1 jtc *dp++ = ')';
252 1.1 jtc } else {
253 1.1 jtc type = comsub(&x, sp);
254 1.1 jtc if (type == XCOM && (f&DOBLANK))
255 1.1 jtc doblank++;
256 1.1 jtc sp = strchr(sp, 0) + 1;
257 1.1 jtc newlines = 0;
258 1.1 jtc }
259 1.1 jtc continue;
260 1.1 jtc case EXPRSUB:
261 1.1 jtc word = IFS_WORD;
262 1.1 jtc tilde_ok = 0;
263 1.1 jtc if (f & DONTRUNCOMMAND) {
264 1.1 jtc *dp++ = '$'; *dp++ = '('; *dp++ = '(';
265 1.1 jtc while (*sp != '\0') {
266 1.1 jtc Xcheck(ds, dp);
267 1.1 jtc *dp++ = *sp++;
268 1.1 jtc }
269 1.1 jtc *dp++ = ')'; *dp++ = ')';
270 1.1 jtc } else {
271 1.1 jtc struct tbl v;
272 1.1 jtc char *p;
273 1.1 jtc
274 1.1 jtc v.flag = DEFINED|ISSET|INTEGER;
275 1.1 jtc v.type = 10; /* not default */
276 1.1 jtc v.name[0] = '\0';
277 1.1 jtc v_evaluate(&v, substitute(sp, 0),
278 1.3 hubertf KSH_UNWIND_ERROR);
279 1.1 jtc sp = strchr(sp, 0) + 1;
280 1.1 jtc for (p = str_val(&v); *p; ) {
281 1.1 jtc Xcheck(ds, dp);
282 1.1 jtc *dp++ = *p++;
283 1.1 jtc }
284 1.1 jtc }
285 1.1 jtc continue;
286 1.1 jtc case OSUBST: /* ${{#}var{:}[=+-?#%]word} */
287 1.1 jtc /* format is:
288 1.3 hubertf * OSUBST [{x] plain-variable-part \0
289 1.3 hubertf * compiled-word-part CSUBST [}x]
290 1.1 jtc * This is were all syntax checking gets done...
291 1.1 jtc */
292 1.1 jtc {
293 1.3 hubertf char *varname = ++sp; /* skip the { or x (}) */
294 1.1 jtc int stype;
295 1.3 hubertf int slen;
296 1.1 jtc
297 1.6 mrg slen = -1; /* XXX gcc */
298 1.1 jtc sp = strchr(sp, '\0') + 1; /* skip variable */
299 1.3 hubertf type = varsub(&x, varname, sp, &stype, &slen);
300 1.1 jtc if (type < 0) {
301 1.1 jtc char endc;
302 1.1 jtc char *str, *end;
303 1.1 jtc
304 1.1 jtc end = (char *) wdscan(sp, CSUBST);
305 1.3 hubertf /* ({) the } or x is already skipped */
306 1.1 jtc endc = *end;
307 1.1 jtc *end = EOS;
308 1.1 jtc str = snptreef((char *) 0, 64, "%S",
309 1.1 jtc varname - 1);
310 1.1 jtc *end = endc;
311 1.1 jtc errorf("%s: bad substitution", str);
312 1.1 jtc }
313 1.1 jtc if (f&DOBLANK)
314 1.1 jtc doblank++;
315 1.1 jtc tilde_ok = 0;
316 1.1 jtc if (type == XBASE) { /* expand? */
317 1.1 jtc if (!st->next) {
318 1.1 jtc SubType *newst;
319 1.1 jtc
320 1.1 jtc newst = (SubType *) alloc(
321 1.1 jtc sizeof(SubType), ATEMP);
322 1.1 jtc newst->next = (SubType *) 0;
323 1.1 jtc newst->prev = st;
324 1.1 jtc st->next = newst;
325 1.1 jtc }
326 1.1 jtc st = st->next;
327 1.1 jtc st->stype = stype;
328 1.1 jtc st->base = Xsavepos(ds, dp);
329 1.1 jtc st->f = f;
330 1.1 jtc st->var = x.var;
331 1.1 jtc st->quote = quote;
332 1.1 jtc /* skip qualifier(s) */
333 1.3 hubertf if (stype)
334 1.3 hubertf sp += slen;
335 1.1 jtc switch (stype & 0x7f) {
336 1.1 jtc case '#':
337 1.1 jtc case '%':
338 1.1 jtc /* ! DOBLANK,DOBRACE_,DOTILDE */
339 1.1 jtc f = DOPAT | (f&DONTRUNCOMMAND)
340 1.1 jtc | DOTEMP_;
341 1.1 jtc quote = 0;
342 1.3 hubertf /* Prepend open pattern (so |
343 1.3 hubertf * in a trim will work as
344 1.3 hubertf * expected)
345 1.3 hubertf */
346 1.3 hubertf *dp++ = MAGIC;
347 1.3 hubertf *dp++ = '@' + 0x80;
348 1.1 jtc break;
349 1.1 jtc case '=':
350 1.1 jtc /* Enabling tilde expansion
351 1.1 jtc * after :'s here is
352 1.1 jtc * non-standard ksh, but is
353 1.1 jtc * consistent with rules for
354 1.1 jtc * other assignments. Not
355 1.1 jtc * sure what POSIX thinks of
356 1.1 jtc * this.
357 1.1 jtc * Not doing tilde expansion
358 1.1 jtc * for integer variables is a
359 1.1 jtc * non-POSIX thing - makes
360 1.1 jtc * sense though, since ~ is
361 1.1 jtc * a arithmetic operator.
362 1.1 jtc */
363 1.1 jtc if (!(x.var->flag & INTEGER))
364 1.1 jtc f |= DOASNTILDE|DOTILDE;
365 1.1 jtc f |= DOTEMP_;
366 1.1 jtc /* These will be done after the
367 1.1 jtc * value has been assigned.
368 1.1 jtc */
369 1.1 jtc f &= ~(DOBLANK|DOGLOB|DOBRACE_);
370 1.1 jtc tilde_ok = 1;
371 1.1 jtc break;
372 1.1 jtc case '?':
373 1.1 jtc f &= ~DOBLANK;
374 1.1 jtc f |= DOTEMP_;
375 1.1 jtc /* fall through */
376 1.1 jtc default:
377 1.1 jtc /* Enable tilde expansion */
378 1.1 jtc tilde_ok = 1;
379 1.1 jtc f |= DOTILDE;
380 1.1 jtc }
381 1.1 jtc } else
382 1.1 jtc /* skip word */
383 1.1 jtc sp = (char *) wdscan(sp, CSUBST);
384 1.1 jtc continue;
385 1.1 jtc }
386 1.1 jtc case CSUBST: /* only get here if expanding word */
387 1.3 hubertf sp++; /* ({) skip the } or x */
388 1.1 jtc tilde_ok = 0; /* in case of ${unset:-} */
389 1.1 jtc *dp = '\0';
390 1.1 jtc quote = st->quote;
391 1.1 jtc f = st->f;
392 1.1 jtc if (f&DOBLANK)
393 1.1 jtc doblank--;
394 1.1 jtc switch (st->stype&0x7f) {
395 1.1 jtc case '#':
396 1.1 jtc case '%':
397 1.3 hubertf /* Append end-pattern */
398 1.3 hubertf *dp++ = MAGIC; *dp++ = ')'; *dp = '\0';
399 1.1 jtc dp = Xrestpos(ds, dp, st->base);
400 1.1 jtc /* Must use st->var since calling
401 1.1 jtc * global would break things
402 1.1 jtc * like x[i+=1].
403 1.1 jtc */
404 1.1 jtc x.str = trimsub(str_val(st->var),
405 1.1 jtc dp, st->stype);
406 1.1 jtc type = XSUB;
407 1.1 jtc if (f&DOBLANK)
408 1.1 jtc doblank++;
409 1.1 jtc st = st->prev;
410 1.1 jtc continue;
411 1.1 jtc case '=':
412 1.1 jtc /* Restore our position and substitute
413 1.1 jtc * the value of st->var (may not be
414 1.1 jtc * the assigned value in the presence
415 1.1 jtc * of integer/right-adj/etc attributes).
416 1.1 jtc */
417 1.1 jtc dp = Xrestpos(ds, dp, st->base);
418 1.1 jtc /* Must use st->var since calling
419 1.1 jtc * global would cause with things
420 1.1 jtc * like x[i+=1] to be evaluated twice.
421 1.1 jtc */
422 1.3 hubertf /* Note: not exported by FEXPORT
423 1.3 hubertf * in at&t ksh.
424 1.3 hubertf */
425 1.3 hubertf /* XXX POSIX says readonly is only
426 1.3 hubertf * fatal for special builtins (setstr
427 1.3 hubertf * does readonly check).
428 1.3 hubertf */
429 1.5 mycroft len = strlen(dp) + 1;
430 1.5 mycroft setstr(st->var,
431 1.5 mycroft debunk((char *) alloc(len, ATEMP),
432 1.5 mycroft dp, len),
433 1.5 mycroft KSH_UNWIND_ERROR);
434 1.1 jtc x.str = str_val(st->var);
435 1.1 jtc type = XSUB;
436 1.1 jtc if (f&DOBLANK)
437 1.1 jtc doblank++;
438 1.1 jtc st = st->prev;
439 1.1 jtc continue;
440 1.1 jtc case '?':
441 1.1 jtc {
442 1.1 jtc char *s = Xrestpos(ds, dp, st->base);
443 1.1 jtc
444 1.1 jtc errorf("%s: %s", st->var->name,
445 1.5 mycroft dp == s ?
446 1.1 jtc "parameter null or not set"
447 1.5 mycroft : (debunk(s, s, strlen(s) + 1), s));
448 1.1 jtc }
449 1.1 jtc }
450 1.1 jtc st = st->prev;
451 1.1 jtc type = XBASE;
452 1.1 jtc continue;
453 1.1 jtc
454 1.1 jtc case OPAT: /* open pattern: *(foo|bar) */
455 1.1 jtc /* Next char is the type of pattern */
456 1.1 jtc make_magic = 1;
457 1.1 jtc c = *sp++ + 0x80;
458 1.1 jtc break;
459 1.1 jtc
460 1.5 mycroft case SPAT: /* pattern separator (|) */
461 1.1 jtc make_magic = 1;
462 1.1 jtc c = '|';
463 1.1 jtc break;
464 1.1 jtc
465 1.1 jtc case CPAT: /* close pattern */
466 1.1 jtc make_magic = 1;
467 1.1 jtc c = /*(*/ ')';
468 1.1 jtc break;
469 1.1 jtc }
470 1.1 jtc break;
471 1.1 jtc
472 1.1 jtc case XNULLSUB:
473 1.1 jtc /* Special case for "$@" (and "${foo[@]}") - no
474 1.1 jtc * word is generated if $# is 0 (unless there is
475 1.1 jtc * other stuff inside the quotes).
476 1.1 jtc */
477 1.1 jtc type = XBASE;
478 1.1 jtc if (f&DOBLANK) {
479 1.1 jtc doblank--;
480 1.1 jtc /* not really correct: x=; "$x$@" should
481 1.1 jtc * generate a null argument and
482 1.1 jtc * set A; "${@:+}" shouldn't.
483 1.1 jtc */
484 1.1 jtc if (dp == Xstring(ds, dp))
485 1.1 jtc word = IFS_WS;
486 1.1 jtc }
487 1.1 jtc continue;
488 1.1 jtc
489 1.1 jtc case XSUB:
490 1.1 jtc if ((c = *x.str++) == 0) {
491 1.1 jtc type = XBASE;
492 1.1 jtc if (f&DOBLANK)
493 1.1 jtc doblank--;
494 1.1 jtc continue;
495 1.1 jtc }
496 1.1 jtc break;
497 1.1 jtc
498 1.1 jtc case XARGSEP:
499 1.1 jtc type = XARG;
500 1.1 jtc quote = 1;
501 1.1 jtc case XARG:
502 1.1 jtc if ((c = *x.str++) == '\0') {
503 1.1 jtc /* force null words to be created so
504 1.1 jtc * set -- '' 2 ''; foo "$@" will do
505 1.1 jtc * the right thing
506 1.1 jtc */
507 1.1 jtc if (quote && x.split)
508 1.1 jtc word = IFS_WORD;
509 1.1 jtc if ((x.str = *x.u.strv++) == NULL) {
510 1.1 jtc type = XBASE;
511 1.1 jtc if (f&DOBLANK)
512 1.1 jtc doblank--;
513 1.1 jtc continue;
514 1.1 jtc }
515 1.1 jtc c = ifs0;
516 1.1 jtc if (c == 0) {
517 1.1 jtc if (quote && !x.split)
518 1.1 jtc continue;
519 1.1 jtc c = ' ';
520 1.1 jtc }
521 1.1 jtc if (quote && x.split) {
522 1.1 jtc /* terminate word for "$@" */
523 1.1 jtc type = XARGSEP;
524 1.1 jtc quote = 0;
525 1.1 jtc }
526 1.1 jtc }
527 1.1 jtc break;
528 1.1 jtc
529 1.1 jtc case XCOM:
530 1.1 jtc if (newlines) { /* Spit out saved nl's */
531 1.1 jtc c = '\n';
532 1.1 jtc --newlines;
533 1.1 jtc } else {
534 1.1 jtc while ((c = shf_getc(x.u.shf)) == 0 || c == '\n')
535 1.1 jtc if (c == '\n')
536 1.1 jtc newlines++; /* Save newlines */
537 1.1 jtc if (newlines && c != EOF) {
538 1.1 jtc shf_ungetc(c, x.u.shf);
539 1.1 jtc c = '\n';
540 1.1 jtc --newlines;
541 1.1 jtc }
542 1.1 jtc }
543 1.1 jtc if (c == EOF) {
544 1.1 jtc newlines = 0;
545 1.1 jtc shf_close(x.u.shf);
546 1.1 jtc if (x.split)
547 1.1 jtc subst_exstat = waitlast();
548 1.1 jtc type = XBASE;
549 1.1 jtc if (f&DOBLANK)
550 1.1 jtc doblank--;
551 1.1 jtc continue;
552 1.1 jtc }
553 1.1 jtc break;
554 1.1 jtc }
555 1.1 jtc
556 1.1 jtc /* check for end of word or IFS separation */
557 1.1 jtc if (c == 0 || (!quote && (f & DOBLANK) && doblank && !make_magic
558 1.1 jtc && ctype(c, C_IFS)))
559 1.1 jtc {
560 1.1 jtc /* How words are broken up:
561 1.1 jtc * | value of c
562 1.1 jtc * word | ws nws 0
563 1.1 jtc * -----------------------------------
564 1.1 jtc * IFS_WORD w/WS w/NWS w
565 1.1 jtc * IFS_WS -/WS w/NWS -
566 1.1 jtc * IFS_NWS -/NWS w/NWS w
567 1.1 jtc * (w means generate a word)
568 1.1 jtc * Note that IFS_NWS/0 generates a word (at&t ksh
569 1.1 jtc * doesn't do this, but POSIX does).
570 1.1 jtc */
571 1.1 jtc if (word == IFS_WORD
572 1.1 jtc || (!ctype(c, C_IFSWS) && (c || word == IFS_NWS)))
573 1.1 jtc {
574 1.1 jtc char *p;
575 1.1 jtc
576 1.1 jtc *dp++ = '\0';
577 1.1 jtc p = Xclose(ds, dp);
578 1.1 jtc #ifdef BRACE_EXPAND
579 1.1 jtc if (fdo & DOBRACE_)
580 1.1 jtc /* also does globbing */
581 1.1 jtc alt_expand(wp, p, p,
582 1.1 jtc p + Xlength(ds, (dp - 1)),
583 1.1 jtc fdo | (f & DOMARKDIRS));
584 1.1 jtc else
585 1.1 jtc #endif /* BRACE_EXPAND */
586 1.1 jtc if (fdo & DOGLOB)
587 1.1 jtc glob(p, wp, f & DOMARKDIRS);
588 1.1 jtc else if ((f & DOPAT) || !(fdo & DOMAGIC_))
589 1.1 jtc XPput(*wp, p);
590 1.1 jtc else
591 1.5 mycroft XPput(*wp, debunk(p, p, strlen(p) + 1));
592 1.1 jtc fdo = 0;
593 1.1 jtc saw_eq = 0;
594 1.1 jtc tilde_ok = (f & (DOTILDE|DOASNTILDE)) ? 1 : 0;
595 1.1 jtc if (c != 0)
596 1.1 jtc Xinit(ds, dp, 128, ATEMP);
597 1.1 jtc }
598 1.1 jtc if (c == 0)
599 1.1 jtc return;
600 1.1 jtc if (word != IFS_NWS)
601 1.1 jtc word = ctype(c, C_IFSWS) ? IFS_WS : IFS_NWS;
602 1.1 jtc } else {
603 1.1 jtc /* age tilde_ok info - ~ code tests second bit */
604 1.1 jtc tilde_ok <<= 1;
605 1.1 jtc /* mark any special second pass chars */
606 1.1 jtc if (!quote)
607 1.1 jtc switch (c) {
608 1.1 jtc case '[':
609 1.1 jtc case NOT:
610 1.1 jtc case '-':
611 1.1 jtc case ']':
612 1.1 jtc /* For character classes - doesn't hurt
613 1.1 jtc * to have magic !,-,]'s outside of
614 1.1 jtc * [...] expressions.
615 1.1 jtc */
616 1.1 jtc if (f & (DOPAT | DOGLOB)) {
617 1.1 jtc fdo |= DOMAGIC_;
618 1.1 jtc if (c == '[')
619 1.1 jtc fdo |= f & DOGLOB;
620 1.1 jtc *dp++ = MAGIC;
621 1.1 jtc }
622 1.1 jtc break;
623 1.1 jtc case '*':
624 1.1 jtc case '?':
625 1.1 jtc if (f & (DOPAT | DOGLOB)) {
626 1.1 jtc fdo |= DOMAGIC_ | (f & DOGLOB);
627 1.1 jtc *dp++ = MAGIC;
628 1.1 jtc }
629 1.1 jtc break;
630 1.1 jtc #ifdef BRACE_EXPAND
631 1.1 jtc case OBRACE:
632 1.1 jtc case ',':
633 1.1 jtc case CBRACE:
634 1.1 jtc if ((f & DOBRACE_) && (c == OBRACE
635 1.1 jtc || (fdo & DOBRACE_)))
636 1.1 jtc {
637 1.1 jtc fdo |= DOBRACE_|DOMAGIC_;
638 1.1 jtc *dp++ = MAGIC;
639 1.1 jtc }
640 1.1 jtc break;
641 1.1 jtc #endif /* BRACE_EXPAND */
642 1.1 jtc case '=':
643 1.1 jtc /* Note first unquoted = for ~ */
644 1.1 jtc if (!(f & DOTEMP_) && !saw_eq) {
645 1.1 jtc saw_eq = 1;
646 1.1 jtc tilde_ok = 1;
647 1.1 jtc }
648 1.1 jtc break;
649 1.1 jtc case PATHSEP: /* : */
650 1.1 jtc /* Note unquoted : for ~ */
651 1.1 jtc if (!(f & DOTEMP_) && (f & DOASNTILDE))
652 1.1 jtc tilde_ok = 1;
653 1.1 jtc break;
654 1.1 jtc case '~':
655 1.1 jtc /* tilde_ok is reset whenever
656 1.1 jtc * any of ' " $( $(( ${ } are seen.
657 1.1 jtc * Note that tilde_ok must be preserved
658 1.1 jtc * through the sequence ${A=a=}~
659 1.1 jtc */
660 1.1 jtc if (type == XBASE
661 1.1 jtc && (f & (DOTILDE|DOASNTILDE))
662 1.1 jtc && (tilde_ok & 2))
663 1.1 jtc {
664 1.1 jtc char *p, *dp_x;
665 1.1 jtc
666 1.1 jtc dp_x = dp;
667 1.1 jtc p = maybe_expand_tilde(sp,
668 1.1 jtc &ds, &dp_x,
669 1.1 jtc f & DOASNTILDE);
670 1.1 jtc if (p) {
671 1.1 jtc if (dp != dp_x)
672 1.1 jtc word = IFS_WORD;
673 1.1 jtc dp = dp_x;
674 1.1 jtc sp = p;
675 1.1 jtc continue;
676 1.1 jtc }
677 1.1 jtc }
678 1.1 jtc break;
679 1.1 jtc }
680 1.1 jtc else
681 1.1 jtc quote &= ~2; /* undo temporary */
682 1.1 jtc
683 1.1 jtc if (make_magic) {
684 1.1 jtc make_magic = 0;
685 1.1 jtc fdo |= DOMAGIC_ | (f & DOGLOB);
686 1.1 jtc *dp++ = MAGIC;
687 1.1 jtc } else if (ISMAGIC(c)) {
688 1.1 jtc fdo |= DOMAGIC_;
689 1.1 jtc *dp++ = MAGIC;
690 1.1 jtc }
691 1.1 jtc *dp++ = c; /* save output char */
692 1.1 jtc word = IFS_WORD;
693 1.1 jtc }
694 1.1 jtc }
695 1.1 jtc }
696 1.1 jtc
697 1.1 jtc /*
698 1.1 jtc * Prepare to generate the string returned by ${} substitution.
699 1.1 jtc */
700 1.1 jtc static int
701 1.3 hubertf varsub(xp, sp, word, stypep, slenp)
702 1.1 jtc Expand *xp;
703 1.1 jtc char *sp;
704 1.1 jtc char *word;
705 1.3 hubertf int *stypep; /* becomes qualifier type */
706 1.3 hubertf int *slenp; /* " " len (=, :=, etc.) valid iff *stypep != 0 */
707 1.1 jtc {
708 1.1 jtc int c;
709 1.1 jtc int state; /* next state: XBASE, XARG, XSUB, XNULLSUB */
710 1.1 jtc int stype; /* substitution type */
711 1.3 hubertf int slen;
712 1.1 jtc char *p;
713 1.1 jtc struct tbl *vp;
714 1.1 jtc
715 1.1 jtc if (sp[0] == '\0') /* Bad variable name */
716 1.1 jtc return -1;
717 1.1 jtc
718 1.13 christos xp->var = NULL;
719 1.1 jtc
720 1.1 jtc /* ${#var}, string length or array size */
721 1.1 jtc if (sp[0] == '#' && (c = sp[1]) != '\0') {
722 1.1 jtc int zero_ok = 0;
723 1.1 jtc
724 1.1 jtc /* Can't have any modifiers for ${#...} */
725 1.1 jtc if (*word != CSUBST)
726 1.1 jtc return -1;
727 1.1 jtc sp++;
728 1.1 jtc /* Check for size of array */
729 1.1 jtc if ((p=strchr(sp,'[')) && (p[1]=='*'||p[1]=='@') && p[2]==']') {
730 1.3 hubertf int n = 0;
731 1.3 hubertf int max = 0;
732 1.1 jtc vp = global(arrayname(sp));
733 1.1 jtc if (vp->flag & (ISSET|ARRAY))
734 1.1 jtc zero_ok = 1;
735 1.1 jtc for (; vp; vp = vp->u.array)
736 1.3 hubertf if (vp->flag & ISSET) {
737 1.3 hubertf max = vp->index + 1;
738 1.3 hubertf n++;
739 1.3 hubertf }
740 1.3 hubertf c = n; /* ksh88/ksh93 go for number, not max index */
741 1.1 jtc } else if (c == '*' || c == '@')
742 1.1 jtc c = e->loc->argc;
743 1.1 jtc else {
744 1.1 jtc p = str_val(global(sp));
745 1.1 jtc zero_ok = p != null;
746 1.1 jtc c = strlen(p);
747 1.1 jtc }
748 1.1 jtc if (Flag(FNOUNSET) && c == 0 && !zero_ok)
749 1.1 jtc errorf("%s: parameter not set", sp);
750 1.1 jtc *stypep = 0; /* unqualified variable/string substitution */
751 1.1 jtc xp->str = str_save(ulton((unsigned long)c, 10), ATEMP);
752 1.1 jtc return XSUB;
753 1.1 jtc }
754 1.1 jtc
755 1.1 jtc /* Check for qualifiers in word part */
756 1.1 jtc stype = 0;
757 1.3 hubertf c = word[slen = 0] == CHAR ? word[1] : 0;
758 1.1 jtc if (c == ':') {
759 1.3 hubertf slen += 2;
760 1.1 jtc stype = 0x80;
761 1.3 hubertf c = word[slen + 0] == CHAR ? word[slen + 1] : 0;
762 1.1 jtc }
763 1.3 hubertf if (ctype(c, C_SUBOP1)) {
764 1.3 hubertf slen += 2;
765 1.1 jtc stype |= c;
766 1.3 hubertf } else if (ctype(c, C_SUBOP2)) { /* Note: ksh88 allows :%, :%%, etc */
767 1.3 hubertf slen += 2;
768 1.1 jtc stype = c;
769 1.3 hubertf if (word[slen + 0] == CHAR && c == word[slen + 1]) {
770 1.1 jtc stype |= 0x80;
771 1.3 hubertf slen += 2;
772 1.3 hubertf }
773 1.3 hubertf } else if (stype) /* : is not ok */
774 1.3 hubertf return -1;
775 1.1 jtc if (!stype && *word != CSUBST)
776 1.1 jtc return -1;
777 1.1 jtc *stypep = stype;
778 1.3 hubertf *slenp = slen;
779 1.1 jtc
780 1.1 jtc c = sp[0];
781 1.1 jtc if (c == '*' || c == '@') {
782 1.1 jtc switch (stype & 0x7f) {
783 1.1 jtc case '=': /* can't assign to a vector */
784 1.3 hubertf case '%': /* can't trim a vector (yet) */
785 1.1 jtc case '#':
786 1.1 jtc return -1;
787 1.1 jtc }
788 1.1 jtc if (e->loc->argc == 0) {
789 1.13 christos xp->u.strv = NULL;
790 1.1 jtc xp->str = null;
791 1.1 jtc state = c == '@' ? XNULLSUB : XSUB;
792 1.1 jtc } else {
793 1.13 christos char **t = &e->loc->argv[1];
794 1.13 christos xp->u.strv = (void *)(uintptr_t)t;
795 1.1 jtc xp->str = *xp->u.strv++;
796 1.1 jtc xp->split = c == '@'; /* $@ */
797 1.1 jtc state = XARG;
798 1.1 jtc }
799 1.1 jtc } else {
800 1.1 jtc if ((p=strchr(sp,'[')) && (p[1]=='*'||p[1]=='@') && p[2]==']') {
801 1.1 jtc XPtrV wv;
802 1.1 jtc
803 1.1 jtc switch (stype & 0x7f) {
804 1.1 jtc case '=': /* can't assign to a vector */
805 1.3 hubertf case '%': /* can't trim a vector (yet) */
806 1.1 jtc case '#':
807 1.1 jtc return -1;
808 1.1 jtc }
809 1.1 jtc XPinit(wv, 32);
810 1.1 jtc vp = global(arrayname(sp));
811 1.1 jtc for (; vp; vp = vp->u.array) {
812 1.1 jtc if (!(vp->flag&ISSET))
813 1.1 jtc continue;
814 1.1 jtc XPput(wv, str_val(vp));
815 1.1 jtc }
816 1.1 jtc if (XPsize(wv) == 0) {
817 1.1 jtc xp->str = null;
818 1.1 jtc state = p[1] == '@' ? XNULLSUB : XSUB;
819 1.1 jtc XPfree(wv);
820 1.1 jtc } else {
821 1.1 jtc XPput(wv, 0);
822 1.1 jtc xp->u.strv = (const char **) XPptrv(wv);
823 1.1 jtc xp->str = *xp->u.strv++;
824 1.1 jtc xp->split = p[1] == '@'; /* ${foo[@]} */
825 1.1 jtc state = XARG;
826 1.1 jtc }
827 1.1 jtc } else {
828 1.1 jtc /* Can't assign things like $! or $1 */
829 1.1 jtc if ((stype & 0x7f) == '='
830 1.1 jtc && (ctype(*sp, C_VAR1) || digit(*sp)))
831 1.1 jtc return -1;
832 1.1 jtc xp->var = global(sp);
833 1.1 jtc xp->str = str_val(xp->var);
834 1.1 jtc state = XSUB;
835 1.1 jtc }
836 1.1 jtc }
837 1.1 jtc
838 1.1 jtc c = stype&0x7f;
839 1.1 jtc /* test the compiler's code generator */
840 1.1 jtc if (ctype(c, C_SUBOP2) ||
841 1.1 jtc (((stype&0x80) ? *xp->str=='\0' : xp->str==null) ? /* undef? */
842 1.1 jtc c == '=' || c == '-' || c == '?' : c == '+'))
843 1.1 jtc state = XBASE; /* expand word instead of variable value */
844 1.1 jtc if (Flag(FNOUNSET) && xp->str == null
845 1.1 jtc && (ctype(c, C_SUBOP2) || (state != XBASE && c != '+')))
846 1.1 jtc errorf("%s: parameter not set", sp);
847 1.1 jtc return state;
848 1.1 jtc }
849 1.1 jtc
850 1.1 jtc /*
851 1.1 jtc * Run the command in $(...) and read its output.
852 1.1 jtc */
853 1.1 jtc static int
854 1.1 jtc comsub(xp, cp)
855 1.1 jtc register Expand *xp;
856 1.1 jtc char *cp;
857 1.1 jtc {
858 1.1 jtc Source *s, *sold;
859 1.1 jtc register struct op *t;
860 1.1 jtc struct shf *shf;
861 1.1 jtc
862 1.1 jtc s = pushs(SSTRING, ATEMP);
863 1.1 jtc s->start = s->str = cp;
864 1.1 jtc sold = source;
865 1.1 jtc t = compile(s);
866 1.7 christos afree(s, ATEMP);
867 1.1 jtc source = sold;
868 1.1 jtc
869 1.1 jtc if (t == NULL)
870 1.1 jtc return XBASE;
871 1.1 jtc
872 1.1 jtc if (t != NULL && t->type == TCOM && /* $(<file) */
873 1.1 jtc *t->args == NULL && *t->vars == NULL && t->ioact != NULL) {
874 1.1 jtc register struct ioword *io = *t->ioact;
875 1.1 jtc char *name;
876 1.1 jtc
877 1.1 jtc if ((io->flag&IOTYPE) != IOREAD)
878 1.1 jtc errorf("funny $() command: %s",
879 1.1 jtc snptreef((char *) 0, 32, "%R", io));
880 1.1 jtc shf = shf_open(name = evalstr(io->name, DOTILDE), O_RDONLY, 0,
881 1.1 jtc SHF_MAPHI|SHF_CLEXEC);
882 1.1 jtc if (shf == NULL)
883 1.1 jtc errorf("%s: cannot open $() input", name);
884 1.1 jtc xp->split = 0; /* no waitlast() */
885 1.1 jtc } else {
886 1.1 jtc int ofd1, pv[2];
887 1.1 jtc openpipe(pv);
888 1.1 jtc shf = shf_fdopen(pv[0], SHF_RD, (struct shf *) 0);
889 1.1 jtc ofd1 = savefd(1, 0); /* fd 1 may be closed... */
890 1.5 mycroft if (pv[1] != 1) {
891 1.5 mycroft ksh_dup2(pv[1], 1, FALSE);
892 1.5 mycroft close(pv[1]);
893 1.5 mycroft }
894 1.1 jtc execute(t, XFORK|XXCOM|XPIPEO);
895 1.1 jtc restfd(1, ofd1);
896 1.1 jtc startlast();
897 1.1 jtc xp->split = 1; /* waitlast() */
898 1.1 jtc }
899 1.1 jtc
900 1.1 jtc xp->u.shf = shf;
901 1.1 jtc return XCOM;
902 1.1 jtc }
903 1.1 jtc
904 1.1 jtc /*
905 1.1 jtc * perform #pattern and %pattern substitution in ${}
906 1.1 jtc */
907 1.1 jtc
908 1.1 jtc static char *
909 1.1 jtc trimsub(str, pat, how)
910 1.1 jtc register char *str;
911 1.1 jtc char *pat;
912 1.1 jtc int how;
913 1.1 jtc {
914 1.1 jtc register char *end = strchr(str, 0);
915 1.1 jtc register char *p, c;
916 1.1 jtc
917 1.1 jtc switch (how&0xff) { /* UCHAR_MAX maybe? */
918 1.5 mycroft case '#': /* shortest at beginning */
919 1.1 jtc for (p = str; p <= end; p++) {
920 1.1 jtc c = *p; *p = '\0';
921 1.1 jtc if (gmatch(str, pat, FALSE)) {
922 1.1 jtc *p = c;
923 1.1 jtc return p;
924 1.1 jtc }
925 1.1 jtc *p = c;
926 1.1 jtc }
927 1.1 jtc break;
928 1.5 mycroft case '#'|0x80: /* longest match at beginning */
929 1.1 jtc for (p = end; p >= str; p--) {
930 1.1 jtc c = *p; *p = '\0';
931 1.1 jtc if (gmatch(str, pat, FALSE)) {
932 1.1 jtc *p = c;
933 1.1 jtc return p;
934 1.1 jtc }
935 1.1 jtc *p = c;
936 1.1 jtc }
937 1.1 jtc break;
938 1.1 jtc case '%': /* shortest match at end */
939 1.1 jtc for (p = end; p >= str; p--) {
940 1.1 jtc if (gmatch(p, pat, FALSE))
941 1.1 jtc return str_nsave(str, p - str, ATEMP);
942 1.1 jtc }
943 1.1 jtc break;
944 1.1 jtc case '%'|0x80: /* longest match at end */
945 1.1 jtc for (p = str; p <= end; p++) {
946 1.1 jtc if (gmatch(p, pat, FALSE))
947 1.1 jtc return str_nsave(str, p - str, ATEMP);
948 1.1 jtc }
949 1.1 jtc break;
950 1.1 jtc }
951 1.1 jtc
952 1.1 jtc return str; /* no match, return string */
953 1.1 jtc }
954 1.1 jtc
955 1.1 jtc /*
956 1.1 jtc * glob
957 1.1 jtc * Name derived from V6's /etc/glob, the program that expanded filenames.
958 1.1 jtc */
959 1.1 jtc
960 1.1 jtc /* XXX cp not const 'cause slashes are temporarily replaced with nulls... */
961 1.1 jtc static void
962 1.1 jtc glob(cp, wp, markdirs)
963 1.1 jtc char *cp;
964 1.1 jtc register XPtrV *wp;
965 1.1 jtc int markdirs;
966 1.1 jtc {
967 1.1 jtc int oldsize = XPsize(*wp);
968 1.1 jtc
969 1.1 jtc if (glob_str(cp, wp, markdirs) == 0)
970 1.5 mycroft XPput(*wp, debunk(cp, cp, strlen(cp) + 1));
971 1.1 jtc else
972 1.1 jtc qsortp(XPptrv(*wp) + oldsize, (size_t)(XPsize(*wp) - oldsize),
973 1.1 jtc xstrcmp);
974 1.1 jtc }
975 1.1 jtc
976 1.1 jtc #define GF_NONE 0
977 1.5 mycroft #define GF_EXCHECK BIT(0) /* do existence check on file */
978 1.1 jtc #define GF_GLOBBED BIT(1) /* some globbing has been done */
979 1.1 jtc #define GF_MARKDIR BIT(2) /* add trailing / to directories */
980 1.1 jtc
981 1.1 jtc /* Apply file globbing to cp and store the matching files in wp. Returns
982 1.1 jtc * the number of matches found.
983 1.1 jtc */
984 1.1 jtc int
985 1.1 jtc glob_str(cp, wp, markdirs)
986 1.1 jtc char *cp;
987 1.1 jtc XPtrV *wp;
988 1.1 jtc int markdirs;
989 1.1 jtc {
990 1.1 jtc int oldsize = XPsize(*wp);
991 1.1 jtc XString xs;
992 1.1 jtc char *xp;
993 1.1 jtc
994 1.1 jtc Xinit(xs, xp, 256, ATEMP);
995 1.1 jtc globit(&xs, &xp, cp, wp, markdirs ? GF_MARKDIR : GF_NONE);
996 1.1 jtc Xfree(xs, xp);
997 1.1 jtc
998 1.1 jtc return XPsize(*wp) - oldsize;
999 1.1 jtc }
1000 1.1 jtc
1001 1.1 jtc static void
1002 1.1 jtc globit(xs, xpp, sp, wp, check)
1003 1.1 jtc XString *xs; /* dest string */
1004 1.1 jtc char **xpp; /* ptr to dest end */
1005 1.1 jtc char *sp; /* source path */
1006 1.1 jtc register XPtrV *wp; /* output list */
1007 1.1 jtc int check; /* GF_* flags */
1008 1.1 jtc {
1009 1.1 jtc register char *np; /* next source component */
1010 1.1 jtc char *xp = *xpp;
1011 1.1 jtc char *se;
1012 1.1 jtc char odirsep;
1013 1.1 jtc
1014 1.1 jtc /* This to allow long expansions to be interrupted */
1015 1.1 jtc intrcheck();
1016 1.1 jtc
1017 1.1 jtc if (sp == NULL) { /* end of source path */
1018 1.1 jtc /* We only need to check if the file exists if a pattern
1019 1.1 jtc * is followed by a non-pattern (eg, foo*x/bar; no check
1020 1.1 jtc * is needed for foo* since the match must exist) or if
1021 1.1 jtc * any patterns were expanded and the markdirs option is set.
1022 1.1 jtc * Symlinks make things a bit tricky...
1023 1.1 jtc */
1024 1.1 jtc if ((check & GF_EXCHECK)
1025 1.1 jtc || ((check & GF_MARKDIR) && (check & GF_GLOBBED)))
1026 1.1 jtc {
1027 1.1 jtc #define stat_check() (stat_done ? stat_done : \
1028 1.1 jtc (stat_done = stat(Xstring(*xs, xp), &statb) < 0 \
1029 1.1 jtc ? -1 : 1))
1030 1.1 jtc struct stat lstatb, statb;
1031 1.1 jtc int stat_done = 0; /* -1: failed, 1 ok */
1032 1.1 jtc
1033 1.1 jtc if (lstat(Xstring(*xs, xp), &lstatb) < 0)
1034 1.1 jtc return;
1035 1.1 jtc /* special case for systems which strip trailing
1036 1.1 jtc * slashes from regular files (eg, /etc/passwd/).
1037 1.1 jtc * SunOS 4.1.3 does this...
1038 1.1 jtc */
1039 1.1 jtc if ((check & GF_EXCHECK) && xp > Xstring(*xs, xp)
1040 1.1 jtc && ISDIRSEP(xp[-1]) && !S_ISDIR(lstatb.st_mode)
1041 1.1 jtc #ifdef S_ISLNK
1042 1.1 jtc && (!S_ISLNK(lstatb.st_mode)
1043 1.1 jtc || stat_check() < 0
1044 1.1 jtc || !S_ISDIR(statb.st_mode))
1045 1.1 jtc #endif /* S_ISLNK */
1046 1.1 jtc )
1047 1.1 jtc return;
1048 1.1 jtc /* Possibly tack on a trailing / if there isn't already
1049 1.1 jtc * one and if the file is a directory or a symlink to a
1050 1.1 jtc * directory
1051 1.1 jtc */
1052 1.1 jtc if (((check & GF_MARKDIR) && (check & GF_GLOBBED))
1053 1.1 jtc && xp > Xstring(*xs, xp) && !ISDIRSEP(xp[-1])
1054 1.1 jtc && (S_ISDIR(lstatb.st_mode)
1055 1.1 jtc #ifdef S_ISLNK
1056 1.1 jtc || (S_ISLNK(lstatb.st_mode)
1057 1.1 jtc && stat_check() > 0
1058 1.1 jtc && S_ISDIR(statb.st_mode))
1059 1.1 jtc #endif /* S_ISLNK */
1060 1.1 jtc ))
1061 1.1 jtc {
1062 1.1 jtc *xp++ = DIRSEP;
1063 1.1 jtc *xp = '\0';
1064 1.1 jtc }
1065 1.1 jtc }
1066 1.1 jtc #ifdef OS2 /* Done this way to avoid bug in gcc 2.7.2... */
1067 1.1 jtc /* Ugly kludge required for command
1068 1.1 jtc * completion - see how search_access()
1069 1.1 jtc * is implemented for OS/2...
1070 1.1 jtc */
1071 1.1 jtc # define KLUDGE_VAL 4
1072 1.1 jtc #else /* OS2 */
1073 1.1 jtc # define KLUDGE_VAL 0
1074 1.1 jtc #endif /* OS2 */
1075 1.1 jtc XPput(*wp, str_nsave(Xstring(*xs, xp), Xlength(*xs, xp)
1076 1.1 jtc + KLUDGE_VAL, ATEMP));
1077 1.1 jtc return;
1078 1.1 jtc }
1079 1.1 jtc
1080 1.1 jtc if (xp > Xstring(*xs, xp))
1081 1.1 jtc *xp++ = DIRSEP;
1082 1.1 jtc while (ISDIRSEP(*sp)) {
1083 1.1 jtc Xcheck(*xs, xp);
1084 1.1 jtc *xp++ = *sp++;
1085 1.1 jtc }
1086 1.1 jtc np = ksh_strchr_dirsep(sp);
1087 1.1 jtc if (np != NULL) {
1088 1.1 jtc se = np;
1089 1.1 jtc odirsep = *np; /* don't assume DIRSEP, can be multiple kinds */
1090 1.1 jtc *np++ = '\0';
1091 1.1 jtc } else {
1092 1.1 jtc odirsep = '\0'; /* keep gcc quiet */
1093 1.1 jtc se = sp + strlen(sp);
1094 1.1 jtc }
1095 1.1 jtc
1096 1.1 jtc
1097 1.1 jtc /* Check if sp needs globbing - done to avoid pattern checks for strings
1098 1.1 jtc * containing MAGIC characters, open ['s without the matching close ],
1099 1.1 jtc * etc. (otherwise opendir() will be called which may fail because the
1100 1.1 jtc * directory isn't readable - if no globbing is needed, only execute
1101 1.1 jtc * permission should be required (as per POSIX)).
1102 1.1 jtc */
1103 1.1 jtc if (!has_globbing(sp, se)) {
1104 1.1 jtc XcheckN(*xs, xp, se - sp + 1);
1105 1.5 mycroft debunk(xp, sp, Xnleft(*xs, xp));
1106 1.1 jtc xp += strlen(xp);
1107 1.1 jtc *xpp = xp;
1108 1.1 jtc globit(xs, xpp, np, wp, check);
1109 1.1 jtc } else {
1110 1.1 jtc DIR *dirp;
1111 1.1 jtc struct dirent *d;
1112 1.1 jtc char *name;
1113 1.1 jtc int len;
1114 1.1 jtc int prefix_len;
1115 1.1 jtc
1116 1.1 jtc /* xp = *xpp; copy_non_glob() may have re-alloc'd xs */
1117 1.1 jtc *xp = '\0';
1118 1.1 jtc prefix_len = Xlength(*xs, xp);
1119 1.1 jtc dirp = ksh_opendir(prefix_len ? Xstring(*xs, xp) : ".");
1120 1.1 jtc if (dirp == NULL)
1121 1.1 jtc goto Nodir;
1122 1.1 jtc while ((d = readdir(dirp)) != NULL) {
1123 1.1 jtc name = d->d_name;
1124 1.1 jtc if ((*name == '.' && *sp != '.')
1125 1.1 jtc || !gmatch(name, sp, TRUE))
1126 1.1 jtc continue;
1127 1.1 jtc
1128 1.1 jtc len = NLENGTH(d) + 1;
1129 1.1 jtc XcheckN(*xs, xp, len);
1130 1.1 jtc memcpy(xp, name, len);
1131 1.1 jtc *xpp = xp + len - 1;
1132 1.1 jtc globit(xs, xpp, np, wp,
1133 1.1 jtc (check & GF_MARKDIR) | GF_GLOBBED
1134 1.1 jtc | (np ? GF_EXCHECK : GF_NONE));
1135 1.1 jtc xp = Xstring(*xs, xp) + prefix_len;
1136 1.1 jtc }
1137 1.1 jtc closedir(dirp);
1138 1.1 jtc Nodir:;
1139 1.1 jtc }
1140 1.1 jtc
1141 1.1 jtc if (np != NULL)
1142 1.1 jtc *--np = odirsep;
1143 1.1 jtc }
1144 1.1 jtc
1145 1.1 jtc #if 0
1146 1.1 jtc /* Check if p contains something that needs globbing; if it does, 0 is
1147 1.1 jtc * returned; if not, p is copied into xs/xp after stripping any MAGICs
1148 1.1 jtc */
1149 1.1 jtc static int copy_non_glob ARGS((XString *xs, char **xpp, char *p));
1150 1.1 jtc static int
1151 1.1 jtc copy_non_glob(xs, xpp, p)
1152 1.1 jtc XString *xs;
1153 1.1 jtc char **xpp;
1154 1.1 jtc char *p;
1155 1.1 jtc {
1156 1.1 jtc char *xp;
1157 1.1 jtc int len = strlen(p);
1158 1.1 jtc
1159 1.1 jtc XcheckN(*xs, *xpp, len);
1160 1.1 jtc xp = *xpp;
1161 1.1 jtc for (; *p; p++) {
1162 1.1 jtc if (ISMAGIC(*p)) {
1163 1.1 jtc int c = *++p;
1164 1.1 jtc
1165 1.1 jtc if (c == '*' || c == '?')
1166 1.1 jtc return 0;
1167 1.1 jtc if (*p == '[') {
1168 1.1 jtc char *q = p + 1;
1169 1.1 jtc
1170 1.1 jtc if (ISMAGIC(*q) && q[1] == NOT)
1171 1.1 jtc q += 2;
1172 1.1 jtc if (ISMAGIC(*q) && q[1] == ']')
1173 1.1 jtc q += 2;
1174 1.1 jtc for (; *q; q++)
1175 1.1 jtc if (ISMAGIC(*q) && *++q == ']')
1176 1.1 jtc return 0;
1177 1.1 jtc /* pass a literal [ through */
1178 1.1 jtc }
1179 1.1 jtc /* must be a MAGIC-MAGIC, or MAGIC-!, MAGIC--, etc. */
1180 1.1 jtc }
1181 1.1 jtc *xp++ = *p;
1182 1.1 jtc }
1183 1.1 jtc *xp = '\0';
1184 1.1 jtc *xpp = xp;
1185 1.1 jtc return 1;
1186 1.1 jtc }
1187 1.1 jtc #endif /* 0 */
1188 1.1 jtc
1189 1.1 jtc /* remove MAGIC from string */
1190 1.1 jtc char *
1191 1.5 mycroft debunk(dp, sp, dlen)
1192 1.1 jtc char *dp;
1193 1.1 jtc const char *sp;
1194 1.5 mycroft size_t dlen;
1195 1.1 jtc {
1196 1.1 jtc char *d, *s;
1197 1.1 jtc
1198 1.1 jtc if ((s = strchr(sp, MAGIC))) {
1199 1.11 lukem if (s - sp >= (ptrdiff_t)dlen)
1200 1.5 mycroft return dp;
1201 1.1 jtc memcpy(dp, sp, s - sp);
1202 1.11 lukem for (d = dp + (s - sp); *s && (d - dp < (ptrdiff_t)dlen); s++)
1203 1.1 jtc if (!ISMAGIC(*s) || !(*++s & 0x80)
1204 1.3 hubertf || !strchr("*+?@! ", *s & 0x7f))
1205 1.1 jtc *d++ = *s;
1206 1.1 jtc else {
1207 1.1 jtc /* extended pattern operators: *+?@! */
1208 1.3 hubertf if ((*s & 0x7f) != ' ')
1209 1.3 hubertf *d++ = *s & 0x7f;
1210 1.11 lukem if (d - dp < (ptrdiff_t)dlen)
1211 1.5 mycroft *d++ = '(';
1212 1.1 jtc }
1213 1.1 jtc *d = '\0';
1214 1.1 jtc } else if (dp != sp)
1215 1.5 mycroft strlcpy(dp, sp, dlen);
1216 1.1 jtc return dp;
1217 1.1 jtc }
1218 1.1 jtc
1219 1.1 jtc /* Check if p is an unquoted name, possibly followed by a / or :. If so
1220 1.1 jtc * puts the expanded version in *dcp,dp and returns a pointer in p just
1221 1.1 jtc * past the name, otherwise returns 0.
1222 1.1 jtc */
1223 1.1 jtc static char *
1224 1.1 jtc maybe_expand_tilde(p, dsp, dpp, isassign)
1225 1.1 jtc char *p;
1226 1.1 jtc XString *dsp;
1227 1.1 jtc char **dpp;
1228 1.1 jtc int isassign;
1229 1.1 jtc {
1230 1.1 jtc XString ts;
1231 1.1 jtc char *dp = *dpp;
1232 1.1 jtc char *tp, *r;
1233 1.1 jtc
1234 1.1 jtc Xinit(ts, tp, 16, ATEMP);
1235 1.1 jtc /* : only for DOASNTILDE form */
1236 1.1 jtc while (p[0] == CHAR && !ISDIRSEP(p[1])
1237 1.1 jtc && (!isassign || p[1] != PATHSEP))
1238 1.1 jtc {
1239 1.1 jtc Xcheck(ts, tp);
1240 1.1 jtc *tp++ = p[1];
1241 1.1 jtc p += 2;
1242 1.1 jtc }
1243 1.1 jtc *tp = '\0';
1244 1.1 jtc r = (p[0] == EOS || p[0] == CHAR || p[0] == CSUBST) ? tilde(Xstring(ts, tp)) : (char *) 0;
1245 1.1 jtc Xfree(ts, tp);
1246 1.1 jtc if (r) {
1247 1.1 jtc while (*r) {
1248 1.1 jtc Xcheck(*dsp, dp);
1249 1.1 jtc if (ISMAGIC(*r))
1250 1.1 jtc *dp++ = MAGIC;
1251 1.1 jtc *dp++ = *r++;
1252 1.1 jtc }
1253 1.1 jtc *dpp = dp;
1254 1.1 jtc r = p;
1255 1.1 jtc }
1256 1.1 jtc return r;
1257 1.1 jtc }
1258 1.1 jtc
1259 1.1 jtc /*
1260 1.1 jtc * tilde expansion
1261 1.1 jtc *
1262 1.1 jtc * based on a version by Arnold Robbins
1263 1.1 jtc */
1264 1.1 jtc
1265 1.1 jtc static char *
1266 1.1 jtc tilde(cp)
1267 1.1 jtc char *cp;
1268 1.1 jtc {
1269 1.1 jtc char *dp;
1270 1.1 jtc
1271 1.1 jtc if (cp[0] == '\0')
1272 1.1 jtc dp = str_val(global("HOME"));
1273 1.1 jtc else if (cp[0] == '+' && cp[1] == '\0')
1274 1.1 jtc dp = str_val(global("PWD"));
1275 1.1 jtc else if (cp[0] == '-' && cp[1] == '\0')
1276 1.1 jtc dp = str_val(global("OLDPWD"));
1277 1.1 jtc else
1278 1.1 jtc dp = homedir(cp);
1279 1.1 jtc /* If HOME, PWD or OLDPWD are not set, don't expand ~ */
1280 1.1 jtc if (dp == null)
1281 1.1 jtc dp = (char *) 0;
1282 1.1 jtc return dp;
1283 1.1 jtc }
1284 1.1 jtc
1285 1.1 jtc /*
1286 1.1 jtc * map userid to user's home directory.
1287 1.1 jtc * note that 4.3's getpw adds more than 6K to the shell,
1288 1.1 jtc * and the YP version probably adds much more.
1289 1.1 jtc * we might consider our own version of getpwnam() to keep the size down.
1290 1.1 jtc */
1291 1.1 jtc
1292 1.1 jtc static char *
1293 1.1 jtc homedir(name)
1294 1.1 jtc char *name;
1295 1.1 jtc {
1296 1.1 jtc register struct tbl *ap;
1297 1.1 jtc
1298 1.1 jtc ap = tenter(&homedirs, name, hash(name));
1299 1.1 jtc if (!(ap->flag & ISSET)) {
1300 1.1 jtc #ifdef OS2
1301 1.1 jtc /* No usernames in OS2 - punt */
1302 1.1 jtc return NULL;
1303 1.1 jtc #else /* OS2 */
1304 1.1 jtc struct passwd *pw;
1305 1.8 cbiere size_t n;
1306 1.1 jtc
1307 1.1 jtc pw = getpwnam(name);
1308 1.1 jtc if (pw == NULL)
1309 1.1 jtc return NULL;
1310 1.8 cbiere n = strlen(pw->pw_dir);
1311 1.8 cbiere if (n > 0 && '/' != pw->pw_dir[n - 1]) {
1312 1.8 cbiere ap->val.s = str_nsave(pw->pw_dir, n + 1, APERM);
1313 1.8 cbiere ap->val.s[n] = '/';
1314 1.8 cbiere ap->val.s[n + 1] = '\0';
1315 1.8 cbiere } else {
1316 1.8 cbiere ap->val.s = str_save(pw->pw_dir, APERM);
1317 1.8 cbiere }
1318 1.1 jtc ap->flag |= DEFINED|ISSET|ALLOC;
1319 1.1 jtc #endif /* OS2 */
1320 1.1 jtc }
1321 1.1 jtc return ap->val.s;
1322 1.1 jtc }
1323 1.1 jtc
1324 1.1 jtc #ifdef BRACE_EXPAND
1325 1.1 jtc static void
1326 1.1 jtc alt_expand(wp, start, exp_start, end, fdo)
1327 1.1 jtc XPtrV *wp;
1328 1.1 jtc char *start, *exp_start;
1329 1.1 jtc char *end;
1330 1.1 jtc int fdo;
1331 1.1 jtc {
1332 1.1 jtc int UNINITIALIZED(count);
1333 1.1 jtc char *brace_start, *brace_end, *UNINITIALIZED(comma);
1334 1.1 jtc char *field_start;
1335 1.1 jtc char *p;
1336 1.1 jtc
1337 1.1 jtc /* search for open brace */
1338 1.1 jtc for (p = exp_start; (p = strchr(p, MAGIC)) && p[1] != OBRACE; p += 2)
1339 1.1 jtc ;
1340 1.1 jtc brace_start = p;
1341 1.1 jtc
1342 1.1 jtc /* find matching close brace, if any */
1343 1.1 jtc if (p) {
1344 1.1 jtc comma = (char *) 0;
1345 1.1 jtc count = 1;
1346 1.1 jtc for (p += 2; *p && count; p++) {
1347 1.1 jtc if (ISMAGIC(*p)) {
1348 1.1 jtc if (*++p == OBRACE)
1349 1.1 jtc count++;
1350 1.1 jtc else if (*p == CBRACE)
1351 1.1 jtc --count;
1352 1.1 jtc else if (*p == ',' && count == 1)
1353 1.1 jtc comma = p;
1354 1.1 jtc }
1355 1.1 jtc }
1356 1.1 jtc }
1357 1.1 jtc /* no valid expansions... */
1358 1.1 jtc if (!p || count != 0) {
1359 1.1 jtc /* Note that given a{{b,c} we do not expand anything (this is
1360 1.1 jtc * what at&t ksh does. This may be changed to do the {b,c}
1361 1.1 jtc * expansion. }
1362 1.1 jtc */
1363 1.1 jtc if (fdo & DOGLOB)
1364 1.1 jtc glob(start, wp, fdo & DOMARKDIRS);
1365 1.1 jtc else
1366 1.5 mycroft XPput(*wp, debunk(start, start, end - start));
1367 1.1 jtc return;
1368 1.1 jtc }
1369 1.1 jtc brace_end = p;
1370 1.1 jtc if (!comma) {
1371 1.1 jtc alt_expand(wp, start, brace_end, end, fdo);
1372 1.1 jtc return;
1373 1.1 jtc }
1374 1.1 jtc
1375 1.1 jtc /* expand expression */
1376 1.1 jtc field_start = brace_start + 2;
1377 1.1 jtc count = 1;
1378 1.1 jtc for (p = brace_start + 2; p != brace_end; p++) {
1379 1.1 jtc if (ISMAGIC(*p)) {
1380 1.1 jtc if (*++p == OBRACE)
1381 1.1 jtc count++;
1382 1.1 jtc else if ((*p == CBRACE && --count == 0)
1383 1.1 jtc || (*p == ',' && count == 1))
1384 1.1 jtc {
1385 1.1 jtc char *new;
1386 1.1 jtc int l1, l2, l3;
1387 1.1 jtc
1388 1.1 jtc l1 = brace_start - start;
1389 1.1 jtc l2 = (p - 1) - field_start;
1390 1.1 jtc l3 = end - brace_end;
1391 1.1 jtc new = (char *) alloc(l1 + l2 + l3 + 1, ATEMP);
1392 1.1 jtc memcpy(new, start, l1);
1393 1.1 jtc memcpy(new + l1, field_start, l2);
1394 1.1 jtc memcpy(new + l1 + l2, brace_end, l3);
1395 1.1 jtc new[l1 + l2 + l3] = '\0';
1396 1.1 jtc alt_expand(wp, new, new + l1,
1397 1.1 jtc new + l1 + l2 + l3, fdo);
1398 1.1 jtc field_start = p + 1;
1399 1.1 jtc }
1400 1.1 jtc }
1401 1.1 jtc }
1402 1.1 jtc return;
1403 1.1 jtc }
1404 1.1 jtc #endif /* BRACE_EXPAND */
1405