| 1 | /* $NetBSD: expand.c,v 1.71 2005/06/01 15:41:19 lukem Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*-
|
|---|
| 4 | * Copyright (c) 1991, 1993
|
|---|
| 5 | * The Regents of the University of California. All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * This code is derived from software contributed to Berkeley by
|
|---|
| 8 | * Kenneth Almquist.
|
|---|
| 9 | *
|
|---|
| 10 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 11 | * modification, are permitted provided that the following conditions
|
|---|
| 12 | * are met:
|
|---|
| 13 | * 1. Redistributions of source code must retain the above copyright
|
|---|
| 14 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 15 | * 2. Redistributions in binary form must reproduce the above copyright
|
|---|
| 16 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 17 | * documentation and/or other materials provided with the distribution.
|
|---|
| 18 | * 3. Neither the name of the University nor the names of its contributors
|
|---|
| 19 | * may be used to endorse or promote products derived from this software
|
|---|
| 20 | * without specific prior written permission.
|
|---|
| 21 | *
|
|---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|---|
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|---|
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|---|
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|---|
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|---|
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|---|
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|---|
| 32 | * SUCH DAMAGE.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #if 0
|
|---|
| 36 | #ifndef lint
|
|---|
| 37 | static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
|
|---|
| 38 | #else
|
|---|
| 39 | __RCSID("$NetBSD: expand.c,v 1.71 2005/06/01 15:41:19 lukem Exp $");
|
|---|
| 40 | #endif /* not lint */
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | #include <sys/types.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 | #include <stdio.h>
|
|---|
| 47 |
|
|---|
| 48 | /*
|
|---|
| 49 | * Routines to expand arguments to commands. We have to deal with
|
|---|
| 50 | * backquotes, shell variables, and file metacharacters.
|
|---|
| 51 | */
|
|---|
| 52 |
|
|---|
| 53 | #include "shell.h"
|
|---|
| 54 | #include "main.h"
|
|---|
| 55 | #include "nodes.h"
|
|---|
| 56 | #include "eval.h"
|
|---|
| 57 | #include "expand.h"
|
|---|
| 58 | #include "syntax.h"
|
|---|
| 59 | #include "parser.h"
|
|---|
| 60 | #include "jobs.h"
|
|---|
| 61 | #include "options.h"
|
|---|
| 62 | #include "var.h"
|
|---|
| 63 | #include "input.h"
|
|---|
| 64 | #include "output.h"
|
|---|
| 65 | #include "memalloc.h"
|
|---|
| 66 | #include "error.h"
|
|---|
| 67 | #include "mystring.h"
|
|---|
| 68 | #include "show.h"
|
|---|
| 69 | #include "shinstance.h"
|
|---|
| 70 |
|
|---|
| 71 | ///*
|
|---|
| 72 | // * Structure specifying which parts of the string should be searched
|
|---|
| 73 | // * for IFS characters.
|
|---|
| 74 | // */
|
|---|
| 75 | //
|
|---|
| 76 | //struct ifsregion {
|
|---|
| 77 | // struct ifsregion *next; /* next region in list */
|
|---|
| 78 | // int begoff; /* offset of start of region */
|
|---|
| 79 | // int endoff; /* offset of end of region */
|
|---|
| 80 | // int inquotes; /* search for nul bytes only */
|
|---|
| 81 | //};
|
|---|
| 82 | //
|
|---|
| 83 | //
|
|---|
| 84 | //char *expdest; /* output of current string */
|
|---|
| 85 | //struct nodelist *argbackq; /* list of back quote expressions */
|
|---|
| 86 | //struct ifsregion ifsfirst; /* first struct in list of ifs regions */
|
|---|
| 87 | //struct ifsregion *ifslastp; /* last struct in list */
|
|---|
| 88 | //struct arglist exparg; /* holds expanded arg list */
|
|---|
| 89 |
|
|---|
| 90 | STATIC void argstr(shinstance *, char *, int);
|
|---|
| 91 | STATIC char *exptilde(shinstance *, char *, int);
|
|---|
| 92 | STATIC void expbackq(shinstance *, union node *, int, int);
|
|---|
| 93 | STATIC int subevalvar(shinstance *, char *, char *, int, int, int, int);
|
|---|
| 94 | STATIC char *evalvar(shinstance *, char *, int);
|
|---|
| 95 | STATIC int varisset(shinstance *, char *, int);
|
|---|
| 96 | STATIC void varvalue(shinstance *, char *, int, int, int);
|
|---|
| 97 | STATIC void recordregion(shinstance *, int, int, int);
|
|---|
| 98 | STATIC void removerecordregions(shinstance *, int);
|
|---|
| 99 | STATIC void ifsbreakup(shinstance *, char *, struct arglist *);
|
|---|
| 100 | STATIC void ifsfree(shinstance *);
|
|---|
| 101 | STATIC void expandmeta(shinstance *, struct strlist *, int);
|
|---|
| 102 | STATIC void expmeta(shinstance *, char *, char *);
|
|---|
| 103 | STATIC void addfname(shinstance *, char *);
|
|---|
| 104 | STATIC struct strlist *expsort(struct strlist *);
|
|---|
| 105 | STATIC struct strlist *msort(struct strlist *, int);
|
|---|
| 106 | STATIC int pmatch(char *, char *, int);
|
|---|
| 107 | STATIC char *cvtnum(shinstance *, int, char *);
|
|---|
| 108 |
|
|---|
| 109 | /*
|
|---|
| 110 | * Expand shell variables and backquotes inside a here document.
|
|---|
| 111 | */
|
|---|
| 112 |
|
|---|
| 113 | void
|
|---|
| 114 | expandhere(shinstance *psh, union node *arg, int fd)
|
|---|
| 115 | {
|
|---|
| 116 | psh->herefd = fd;
|
|---|
| 117 | expandarg(psh, arg, (struct arglist *)NULL, 0);
|
|---|
| 118 | xwrite(psh, fd, stackblock(psh), psh->expdest - stackblock(psh));
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | /*
|
|---|
| 123 | * Perform variable substitution and command substitution on an argument,
|
|---|
| 124 | * placing the resulting list of arguments in arglist. If EXP_FULL is true,
|
|---|
| 125 | * perform splitting and file name expansion. When arglist is NULL, perform
|
|---|
| 126 | * here document expansion.
|
|---|
| 127 | */
|
|---|
| 128 |
|
|---|
| 129 | void
|
|---|
| 130 | expandarg(shinstance *psh, union node *arg, struct arglist *arglist, int flag)
|
|---|
| 131 | {
|
|---|
| 132 | struct strlist *sp;
|
|---|
| 133 | char *p;
|
|---|
| 134 |
|
|---|
| 135 | psh->argbackq = arg->narg.backquote;
|
|---|
| 136 | STARTSTACKSTR(psh, psh->expdest);
|
|---|
| 137 | psh->ifsfirst.next = NULL;
|
|---|
| 138 | psh->ifslastp = NULL;
|
|---|
| 139 | argstr(psh, arg->narg.text, flag);
|
|---|
| 140 | if (arglist == NULL) {
|
|---|
| 141 | return; /* here document expanded */
|
|---|
| 142 | }
|
|---|
| 143 | STPUTC(psh, '\0', psh->expdest);
|
|---|
| 144 | p = grabstackstr(psh, psh->expdest);
|
|---|
| 145 | psh->exparg.lastp = &psh->exparg.list;
|
|---|
| 146 | /*
|
|---|
| 147 | * TODO - EXP_REDIR
|
|---|
| 148 | */
|
|---|
| 149 | if (flag & EXP_FULL) {
|
|---|
| 150 | ifsbreakup(psh, p, &psh->exparg);
|
|---|
| 151 | *psh->exparg.lastp = NULL;
|
|---|
| 152 | psh->exparg.lastp = &psh->exparg.list;
|
|---|
| 153 | expandmeta(psh, psh->exparg.list, flag);
|
|---|
| 154 | } else {
|
|---|
| 155 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
|
|---|
| 156 | rmescapes(psh, p);
|
|---|
| 157 | sp = (struct strlist *)stalloc(psh, sizeof (struct strlist));
|
|---|
| 158 | sp->text = p;
|
|---|
| 159 | *psh->exparg.lastp = sp;
|
|---|
| 160 | psh->exparg.lastp = &sp->next;
|
|---|
| 161 | }
|
|---|
| 162 | ifsfree(psh);
|
|---|
| 163 | *psh->exparg.lastp = NULL;
|
|---|
| 164 | if (psh->exparg.list) {
|
|---|
| 165 | *arglist->lastp = psh->exparg.list;
|
|---|
| 166 | arglist->lastp = psh->exparg.lastp;
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | /*
|
|---|
| 173 | * Perform variable and command substitution.
|
|---|
| 174 | * If EXP_FULL is set, output CTLESC characters to allow for further processing.
|
|---|
| 175 | * Otherwise treat $@ like $* since no splitting will be performed.
|
|---|
| 176 | */
|
|---|
| 177 |
|
|---|
| 178 | STATIC void
|
|---|
| 179 | argstr(shinstance *psh, char *p, int flag)
|
|---|
| 180 | {
|
|---|
| 181 | char c;
|
|---|
| 182 | int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */
|
|---|
| 183 | int firsteq = 1;
|
|---|
| 184 | const char *ifs = NULL;
|
|---|
| 185 | int ifs_split = EXP_IFS_SPLIT;
|
|---|
| 186 |
|
|---|
| 187 | if (flag & EXP_IFS_SPLIT)
|
|---|
| 188 | ifs = ifsset(psh) ? ifsval(psh) : " \t\n";
|
|---|
| 189 |
|
|---|
| 190 | if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
|
|---|
| 191 | p = exptilde(psh, p, flag);
|
|---|
| 192 | for (;;) {
|
|---|
| 193 | switch (c = *p++) {
|
|---|
| 194 | case '\0':
|
|---|
| 195 | case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */
|
|---|
| 196 | return;
|
|---|
| 197 | case CTLQUOTEMARK:
|
|---|
| 198 | /* "$@" syntax adherence hack */
|
|---|
| 199 | if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
|
|---|
| 200 | break;
|
|---|
| 201 | if ((flag & EXP_FULL) != 0)
|
|---|
| 202 | STPUTC(psh, c, psh->expdest);
|
|---|
| 203 | ifs_split = 0;
|
|---|
| 204 | break;
|
|---|
| 205 | case CTLQUOTEEND:
|
|---|
| 206 | ifs_split = EXP_IFS_SPLIT;
|
|---|
| 207 | break;
|
|---|
| 208 | case CTLESC:
|
|---|
| 209 | if (quotes)
|
|---|
| 210 | STPUTC(psh, c, psh->expdest);
|
|---|
| 211 | c = *p++;
|
|---|
| 212 | STPUTC(psh, c, psh->expdest);
|
|---|
| 213 | break;
|
|---|
| 214 | case CTLVAR:
|
|---|
| 215 | p = evalvar(psh, p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));
|
|---|
| 216 | break;
|
|---|
| 217 | case CTLBACKQ:
|
|---|
| 218 | case CTLBACKQ|CTLQUOTE:
|
|---|
| 219 | expbackq(psh, psh->argbackq->n, c & CTLQUOTE, flag);
|
|---|
| 220 | psh->argbackq = psh->argbackq->next;
|
|---|
| 221 | break;
|
|---|
| 222 | case CTLENDARI:
|
|---|
| 223 | expari(psh, flag);
|
|---|
| 224 | break;
|
|---|
| 225 | case ':':
|
|---|
| 226 | case '=':
|
|---|
| 227 | /*
|
|---|
| 228 | * sort of a hack - expand tildes in variable
|
|---|
| 229 | * assignments (after the first '=' and after ':'s).
|
|---|
| 230 | */
|
|---|
| 231 | STPUTC(psh, c, psh->expdest);
|
|---|
| 232 | if (flag & EXP_VARTILDE && *p == '~') {
|
|---|
| 233 | if (c == '=') {
|
|---|
| 234 | if (firsteq)
|
|---|
| 235 | firsteq = 0;
|
|---|
| 236 | else
|
|---|
| 237 | break;
|
|---|
| 238 | }
|
|---|
| 239 | p = exptilde(psh, p, flag);
|
|---|
| 240 | }
|
|---|
| 241 | break;
|
|---|
| 242 | default:
|
|---|
| 243 | STPUTC(psh, c, psh->expdest);
|
|---|
| 244 | if (flag & EXP_IFS_SPLIT & ifs_split && strchr(ifs, c) != NULL) {
|
|---|
| 245 | /* We need to get the output split here... */
|
|---|
| 246 | recordregion(psh, (int)(psh->expdest - stackblock(psh) - 1),
|
|---|
| 247 | (int)(psh->expdest - stackblock(psh)), 0);
|
|---|
| 248 | }
|
|---|
| 249 | break;
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | STATIC char *
|
|---|
| 255 | exptilde(shinstance *psh, char *p, int flag)
|
|---|
| 256 | {
|
|---|
| 257 | char c, *startp = p;
|
|---|
| 258 | const char *home;
|
|---|
| 259 | int quotes = flag & (EXP_FULL | EXP_CASE);
|
|---|
| 260 |
|
|---|
| 261 | while ((c = *p) != '\0') {
|
|---|
| 262 | switch(c) {
|
|---|
| 263 | case CTLESC:
|
|---|
| 264 | return (startp);
|
|---|
| 265 | case CTLQUOTEMARK:
|
|---|
| 266 | return (startp);
|
|---|
| 267 | case ':':
|
|---|
| 268 | if (flag & EXP_VARTILDE)
|
|---|
| 269 | goto done;
|
|---|
| 270 | break;
|
|---|
| 271 | case '/':
|
|---|
| 272 | goto done;
|
|---|
| 273 | }
|
|---|
| 274 | p++;
|
|---|
| 275 | }
|
|---|
| 276 | done:
|
|---|
| 277 | *p = '\0';
|
|---|
| 278 | if (*(startp+1) == '\0') {
|
|---|
| 279 | if ((home = lookupvar(psh, "HOME")) == NULL)
|
|---|
| 280 | goto lose;
|
|---|
| 281 | } else {
|
|---|
| 282 | if ((home = sh_gethomedir(psh, startp+1)) == NULL)
|
|---|
| 283 | goto lose;
|
|---|
| 284 | }
|
|---|
| 285 | if (*home == '\0')
|
|---|
| 286 | goto lose;
|
|---|
| 287 | *p = c;
|
|---|
| 288 | while ((c = *home++) != '\0') {
|
|---|
| 289 | if (quotes && SQSYNTAX[(int)c] == CCTL)
|
|---|
| 290 | STPUTC(psh, CTLESC, psh->expdest);
|
|---|
| 291 | STPUTC(psh, c, psh->expdest);
|
|---|
| 292 | }
|
|---|
| 293 | return (p);
|
|---|
| 294 | lose:
|
|---|
| 295 | *p = c;
|
|---|
| 296 | return (startp);
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 | STATIC void
|
|---|
| 301 | removerecordregions(shinstance *psh, int endoff)
|
|---|
| 302 | {
|
|---|
| 303 | if (psh->ifslastp == NULL)
|
|---|
| 304 | return;
|
|---|
| 305 |
|
|---|
| 306 | if (psh->ifsfirst.endoff > endoff) {
|
|---|
| 307 | while (psh->ifsfirst.next != NULL) {
|
|---|
| 308 | struct ifsregion *ifsp;
|
|---|
| 309 | INTOFF;
|
|---|
| 310 | ifsp = psh->ifsfirst.next->next;
|
|---|
| 311 | ckfree(psh, psh->ifsfirst.next);
|
|---|
| 312 | psh->ifsfirst.next = ifsp;
|
|---|
| 313 | INTON;
|
|---|
| 314 | }
|
|---|
| 315 | if (psh->ifsfirst.begoff > endoff)
|
|---|
| 316 | psh->ifslastp = NULL;
|
|---|
| 317 | else {
|
|---|
| 318 | psh->ifslastp = &psh->ifsfirst;
|
|---|
| 319 | psh->ifsfirst.endoff = endoff;
|
|---|
| 320 | }
|
|---|
| 321 | return;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | psh->ifslastp = &psh->ifsfirst;
|
|---|
| 325 | while (psh->ifslastp->next && psh->ifslastp->next->begoff < endoff)
|
|---|
| 326 | psh->ifslastp=psh->ifslastp->next;
|
|---|
| 327 | while (psh->ifslastp->next != NULL) {
|
|---|
| 328 | struct ifsregion *ifsp;
|
|---|
| 329 | INTOFF;
|
|---|
| 330 | ifsp = psh->ifslastp->next->next;
|
|---|
| 331 | ckfree(psh, psh->ifslastp->next);
|
|---|
| 332 | psh->ifslastp->next = ifsp;
|
|---|
| 333 | INTON;
|
|---|
| 334 | }
|
|---|
| 335 | if (psh->ifslastp->endoff > endoff)
|
|---|
| 336 | psh->ifslastp->endoff = endoff;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 | /*
|
|---|
| 341 | * Expand arithmetic expression. Backup to start of expression,
|
|---|
| 342 | * evaluate, place result in (backed up) result, adjust string position.
|
|---|
| 343 | */
|
|---|
| 344 | void
|
|---|
| 345 | expari(shinstance *psh, int flag)
|
|---|
| 346 | {
|
|---|
| 347 | char *p, *start;
|
|---|
| 348 | int result;
|
|---|
| 349 | int begoff;
|
|---|
| 350 | int quotes = flag & (EXP_FULL | EXP_CASE);
|
|---|
| 351 | int quoted;
|
|---|
| 352 |
|
|---|
| 353 | /* ifsfree(); */
|
|---|
| 354 |
|
|---|
| 355 | /*
|
|---|
| 356 | * This routine is slightly over-complicated for
|
|---|
| 357 | * efficiency. First we make sure there is
|
|---|
| 358 | * enough space for the result, which may be bigger
|
|---|
| 359 | * than the expression if we add exponentation. Next we
|
|---|
| 360 | * scan backwards looking for the start of arithmetic. If the
|
|---|
| 361 | * next previous character is a CTLESC character, then we
|
|---|
| 362 | * have to rescan starting from the beginning since CTLESC
|
|---|
| 363 | * characters have to be processed left to right.
|
|---|
| 364 | */
|
|---|
| 365 | #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
|
|---|
| 366 | #error "integers with more than 10 digits are not supported"
|
|---|
| 367 | #endif
|
|---|
| 368 | CHECKSTRSPACE(psh, 12 - 2, psh->expdest);
|
|---|
| 369 | USTPUTC(psh, '\0', psh->expdest);
|
|---|
| 370 | start = stackblock(psh);
|
|---|
| 371 | p = psh->expdest - 1;
|
|---|
| 372 | while (*p != CTLARI && p >= start)
|
|---|
| 373 | --p;
|
|---|
| 374 | if (*p != CTLARI)
|
|---|
| 375 | error(psh, "missing CTLARI (shouldn't happen)");
|
|---|
| 376 | if (p > start && *(p-1) == CTLESC)
|
|---|
| 377 | for (p = start; *p != CTLARI; p++)
|
|---|
| 378 | if (*p == CTLESC)
|
|---|
| 379 | p++;
|
|---|
| 380 |
|
|---|
| 381 | if (p[1] == '"')
|
|---|
| 382 | quoted=1;
|
|---|
| 383 | else
|
|---|
| 384 | quoted=0;
|
|---|
| 385 | begoff = (int)(p - start);
|
|---|
| 386 | removerecordregions(psh, begoff);
|
|---|
| 387 | if (quotes)
|
|---|
| 388 | rmescapes(psh, p+2);
|
|---|
| 389 | result = arith(psh, p+2);
|
|---|
| 390 | fmtstr(p, 12, "%d", result);
|
|---|
| 391 |
|
|---|
| 392 | while (*p++)
|
|---|
| 393 | ;
|
|---|
| 394 |
|
|---|
| 395 | if (quoted == 0)
|
|---|
| 396 | recordregion(psh, begoff, (int)(p - 1 - start), 0);
|
|---|
| 397 | result = (int)(psh->expdest - p + 1);
|
|---|
| 398 | STADJUST(psh, -result, psh->expdest);
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 | /*
|
|---|
| 403 | * Expand stuff in backwards quotes.
|
|---|
| 404 | */
|
|---|
| 405 |
|
|---|
| 406 | STATIC void
|
|---|
| 407 | expbackq(shinstance *psh, union node *cmd, int quoted, int flag)
|
|---|
| 408 | {
|
|---|
| 409 | struct backcmd in;
|
|---|
| 410 | int i;
|
|---|
| 411 | char buf[128];
|
|---|
| 412 | char *p;
|
|---|
| 413 | char *dest = psh->expdest;
|
|---|
| 414 | struct ifsregion saveifs, *savelastp;
|
|---|
| 415 | struct nodelist *saveargbackq;
|
|---|
| 416 | char lastc;
|
|---|
| 417 | int startloc = (int)(dest - stackblock(psh));
|
|---|
| 418 | char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
|
|---|
| 419 | int saveherefd;
|
|---|
| 420 | int quotes = flag & (EXP_FULL | EXP_CASE);
|
|---|
| 421 | #ifdef SH_DEAL_WITH_CRLF
|
|---|
| 422 | int pending_cr = 0;
|
|---|
| 423 | #endif
|
|---|
| 424 |
|
|---|
| 425 | INTOFF;
|
|---|
| 426 | saveifs = psh->ifsfirst;
|
|---|
| 427 | savelastp = psh->ifslastp;
|
|---|
| 428 | saveargbackq = psh->argbackq;
|
|---|
| 429 | saveherefd = psh->herefd;
|
|---|
| 430 | psh->herefd = -1;
|
|---|
| 431 | p = grabstackstr(psh, dest);
|
|---|
| 432 | evalbackcmd(psh, cmd, &in);
|
|---|
| 433 | ungrabstackstr(psh, p, dest);
|
|---|
| 434 | psh->ifsfirst = saveifs;
|
|---|
| 435 | psh->ifslastp = savelastp;
|
|---|
| 436 | psh->argbackq = saveargbackq;
|
|---|
| 437 | psh->herefd = saveherefd;
|
|---|
| 438 |
|
|---|
| 439 | p = in.buf;
|
|---|
| 440 | lastc = '\0';
|
|---|
| 441 | for (;;) {
|
|---|
| 442 | if (--in.nleft < 0) {
|
|---|
| 443 | if (in.fd < 0)
|
|---|
| 444 | break;
|
|---|
| 445 | while ((i = shfile_read(&psh->fdtab, in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
|
|---|
| 446 | TRACE((psh, "expbackq: read returns %d\n", i));
|
|---|
| 447 | if (i <= 0)
|
|---|
| 448 | break;
|
|---|
| 449 | p = buf;
|
|---|
| 450 | in.nleft = i - 1;
|
|---|
| 451 | }
|
|---|
| 452 | lastc = *p++;
|
|---|
| 453 | #ifdef SH_DEAL_WITH_CRLF
|
|---|
| 454 | if (pending_cr) {
|
|---|
| 455 | pending_cr = 0;
|
|---|
| 456 | if (lastc != '\n') {
|
|---|
| 457 | if (quotes && syntax[(int)'\r'] == CCTL)
|
|---|
| 458 | STPUTC(psh, CTLESC, dest);
|
|---|
| 459 | STPUTC(psh, '\r', dest);
|
|---|
| 460 | }
|
|---|
| 461 | }
|
|---|
| 462 | if (lastc == '\r')
|
|---|
| 463 | pending_cr = '\r';
|
|---|
| 464 | else
|
|---|
| 465 | #endif
|
|---|
| 466 | if (lastc != '\0') {
|
|---|
| 467 | if (quotes && syntax[(int)lastc] == CCTL)
|
|---|
| 468 | STPUTC(psh, CTLESC, dest);
|
|---|
| 469 | STPUTC(psh, lastc, dest);
|
|---|
| 470 | }
|
|---|
| 471 | }
|
|---|
| 472 | #ifdef SH_DEAL_WITH_CRLF
|
|---|
| 473 | if (pending_cr) {
|
|---|
| 474 | if (quotes && syntax[(int)'\r'] == CCTL)
|
|---|
| 475 | STPUTC(psh, CTLESC, dest);
|
|---|
| 476 | STPUTC(psh, '\r', dest);
|
|---|
| 477 | }
|
|---|
| 478 | #endif
|
|---|
| 479 |
|
|---|
| 480 | /* Eat all trailing newlines */
|
|---|
| 481 | p = stackblock(psh) + startloc;
|
|---|
| 482 | while (dest > p && dest[-1] == '\n')
|
|---|
| 483 | STUNPUTC(psh, dest);
|
|---|
| 484 |
|
|---|
| 485 | if (in.fd >= 0)
|
|---|
| 486 | shfile_close(&psh->fdtab, in.fd);
|
|---|
| 487 | if (in.buf)
|
|---|
| 488 | ckfree(psh, in.buf);
|
|---|
| 489 | if (in.jp)
|
|---|
| 490 | psh->back_exitstatus = waitforjob(psh, in.jp);
|
|---|
| 491 | if (quoted == 0)
|
|---|
| 492 | recordregion(psh, startloc, (int)(dest - stackblock(psh)), 0);
|
|---|
| 493 | TRACE((psh, "evalbackq: size=%d: \"%.*s\"\n",
|
|---|
| 494 | (dest - stackblock(psh)) - startloc,
|
|---|
| 495 | (dest - stackblock(psh)) - startloc,
|
|---|
| 496 | stackblock(psh) + startloc));
|
|---|
| 497 | psh->expdest = dest;
|
|---|
| 498 | INTON;
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 | STATIC int
|
|---|
| 504 | subevalvar(shinstance *psh, char *p, char *str, int strloc, int subtype, int startloc, int varflags)
|
|---|
| 505 | {
|
|---|
| 506 | char *startp;
|
|---|
| 507 | char *loc = NULL;
|
|---|
| 508 | char *q;
|
|---|
| 509 | int c = 0;
|
|---|
| 510 | int saveherefd = psh->herefd;
|
|---|
| 511 | struct nodelist *saveargbackq = psh->argbackq;
|
|---|
| 512 | int amount;
|
|---|
| 513 |
|
|---|
| 514 | psh->herefd = -1;
|
|---|
| 515 | argstr(psh, p, 0);
|
|---|
| 516 | STACKSTRNUL(psh, psh->expdest);
|
|---|
| 517 | psh->herefd = saveherefd;
|
|---|
| 518 | psh->argbackq = saveargbackq;
|
|---|
| 519 | startp = stackblock(psh) + startloc;
|
|---|
| 520 | if (str == NULL)
|
|---|
| 521 | str = stackblock(psh) + strloc;
|
|---|
| 522 |
|
|---|
| 523 | switch (subtype) {
|
|---|
| 524 | case VSASSIGN:
|
|---|
| 525 | setvar(psh, str, startp, 0);
|
|---|
| 526 | amount = (int)(startp - psh->expdest);
|
|---|
| 527 | STADJUST(psh, amount, psh->expdest);
|
|---|
| 528 | varflags &= ~VSNUL;
|
|---|
| 529 | if (c != 0)
|
|---|
| 530 | *loc = c;
|
|---|
| 531 | return 1;
|
|---|
| 532 |
|
|---|
| 533 | case VSQUESTION:
|
|---|
| 534 | if (*p != CTLENDVAR) {
|
|---|
| 535 | outfmt(&psh->errout, "%s\n", startp);
|
|---|
| 536 | error(psh, (char *)NULL);
|
|---|
| 537 | }
|
|---|
| 538 | error(psh, "%.*s: parameter %snot set", p - str - 1,
|
|---|
| 539 | str, (varflags & VSNUL) ? "null or "
|
|---|
| 540 | : nullstr);
|
|---|
| 541 | /* NOTREACHED */
|
|---|
| 542 |
|
|---|
| 543 | case VSTRIMLEFT:
|
|---|
| 544 | for (loc = startp; loc < str; loc++) {
|
|---|
| 545 | c = *loc;
|
|---|
| 546 | *loc = '\0';
|
|---|
| 547 | if (patmatch(psh, str, startp, varflags & VSQUOTE))
|
|---|
| 548 | goto recordleft;
|
|---|
| 549 | *loc = c;
|
|---|
| 550 | if ((varflags & VSQUOTE) && *loc == CTLESC)
|
|---|
| 551 | loc++;
|
|---|
| 552 | }
|
|---|
| 553 | return 0;
|
|---|
| 554 |
|
|---|
| 555 | case VSTRIMLEFTMAX:
|
|---|
| 556 | for (loc = str - 1; loc >= startp;) {
|
|---|
| 557 | c = *loc;
|
|---|
| 558 | *loc = '\0';
|
|---|
| 559 | if (patmatch(psh, str, startp, varflags & VSQUOTE))
|
|---|
| 560 | goto recordleft;
|
|---|
| 561 | *loc = c;
|
|---|
| 562 | loc--;
|
|---|
| 563 | if ((varflags & VSQUOTE) && loc > startp &&
|
|---|
| 564 | *(loc - 1) == CTLESC) {
|
|---|
| 565 | for (q = startp; q < loc; q++)
|
|---|
| 566 | if (*q == CTLESC)
|
|---|
| 567 | q++;
|
|---|
| 568 | if (q > loc)
|
|---|
| 569 | loc--;
|
|---|
| 570 | }
|
|---|
| 571 | }
|
|---|
| 572 | return 0;
|
|---|
| 573 |
|
|---|
| 574 | case VSTRIMRIGHT:
|
|---|
| 575 | for (loc = str - 1; loc >= startp;) {
|
|---|
| 576 | if (patmatch(psh, str, loc, varflags & VSQUOTE))
|
|---|
| 577 | goto recordright;
|
|---|
| 578 | loc--;
|
|---|
| 579 | if ((varflags & VSQUOTE) && loc > startp &&
|
|---|
| 580 | *(loc - 1) == CTLESC) {
|
|---|
| 581 | for (q = startp; q < loc; q++)
|
|---|
| 582 | if (*q == CTLESC)
|
|---|
| 583 | q++;
|
|---|
| 584 | if (q > loc)
|
|---|
| 585 | loc--;
|
|---|
| 586 | }
|
|---|
| 587 | }
|
|---|
| 588 | return 0;
|
|---|
| 589 |
|
|---|
| 590 | case VSTRIMRIGHTMAX:
|
|---|
| 591 | for (loc = startp; loc < str - 1; loc++) {
|
|---|
| 592 | if (patmatch(psh, str, loc, varflags & VSQUOTE))
|
|---|
| 593 | goto recordright;
|
|---|
| 594 | if ((varflags & VSQUOTE) && *loc == CTLESC)
|
|---|
| 595 | loc++;
|
|---|
| 596 | }
|
|---|
| 597 | return 0;
|
|---|
| 598 |
|
|---|
| 599 | default:
|
|---|
| 600 | sh_abort(psh);
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | recordleft:
|
|---|
| 604 | *loc = c;
|
|---|
| 605 | amount = (int)(((str - 1) - (loc - startp)) - psh->expdest);
|
|---|
| 606 | STADJUST(psh, amount, psh->expdest);
|
|---|
| 607 | while (loc != str - 1)
|
|---|
| 608 | *startp++ = *loc++;
|
|---|
| 609 | return 1;
|
|---|
| 610 |
|
|---|
| 611 | recordright:
|
|---|
| 612 | amount = (int)(loc - psh->expdest);
|
|---|
| 613 | STADJUST(psh, amount, psh->expdest);
|
|---|
| 614 | STPUTC(psh, '\0', psh->expdest);
|
|---|
| 615 | STADJUST(psh, -1, psh->expdest);
|
|---|
| 616 | return 1;
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 | /*
|
|---|
| 621 | * Expand a variable, and return a pointer to the next character in the
|
|---|
| 622 | * input string.
|
|---|
| 623 | */
|
|---|
| 624 |
|
|---|
| 625 | STATIC char *
|
|---|
| 626 | evalvar(shinstance *psh, char *p, int flag)
|
|---|
| 627 | {
|
|---|
| 628 | int subtype;
|
|---|
| 629 | int varflags;
|
|---|
| 630 | char *var;
|
|---|
| 631 | char *val;
|
|---|
| 632 | int patloc;
|
|---|
| 633 | int c;
|
|---|
| 634 | int set;
|
|---|
| 635 | int special;
|
|---|
| 636 | int startloc;
|
|---|
| 637 | int varlen;
|
|---|
| 638 | int apply_ifs;
|
|---|
| 639 | int quotes = flag & (EXP_FULL | EXP_CASE);
|
|---|
| 640 |
|
|---|
| 641 | varflags = (unsigned char)*p++;
|
|---|
| 642 | subtype = varflags & VSTYPE;
|
|---|
| 643 | var = p;
|
|---|
| 644 | special = !is_name(*p);
|
|---|
| 645 | p = strchr(p, '=') + 1;
|
|---|
| 646 |
|
|---|
| 647 | again: /* jump here after setting a variable with ${var=text} */
|
|---|
| 648 | if (special) {
|
|---|
| 649 | set = varisset(psh, var, varflags & VSNUL);
|
|---|
| 650 | val = NULL;
|
|---|
| 651 | } else {
|
|---|
| 652 | val = lookupvar(psh, var);
|
|---|
| 653 | if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
|
|---|
| 654 | val = NULL;
|
|---|
| 655 | set = 0;
|
|---|
| 656 | } else
|
|---|
| 657 | set = 1;
|
|---|
| 658 | }
|
|---|
| 659 |
|
|---|
| 660 | varlen = 0;
|
|---|
| 661 | startloc = (int)(psh->expdest - stackblock(psh));
|
|---|
| 662 |
|
|---|
| 663 | if (!set && uflag(psh)) {
|
|---|
| 664 | switch (subtype) {
|
|---|
| 665 | case VSNORMAL:
|
|---|
| 666 | case VSTRIMLEFT:
|
|---|
| 667 | case VSTRIMLEFTMAX:
|
|---|
| 668 | case VSTRIMRIGHT:
|
|---|
| 669 | case VSTRIMRIGHTMAX:
|
|---|
| 670 | case VSLENGTH:
|
|---|
| 671 | error(psh, "%.*s: parameter not set", p - var - 1, var);
|
|---|
| 672 | /* NOTREACHED */
|
|---|
| 673 | }
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | if (set && subtype != VSPLUS) {
|
|---|
| 677 | /* insert the value of the variable */
|
|---|
| 678 | if (special) {
|
|---|
| 679 | varvalue(psh, var, varflags & VSQUOTE, subtype, flag);
|
|---|
| 680 | if (subtype == VSLENGTH) {
|
|---|
| 681 | varlen = (int)(psh->expdest - stackblock(psh) - startloc);
|
|---|
| 682 | STADJUST(psh, -varlen, psh->expdest);
|
|---|
| 683 | }
|
|---|
| 684 | } else {
|
|---|
| 685 | char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
|
|---|
| 686 | : BASESYNTAX;
|
|---|
| 687 |
|
|---|
| 688 | if (subtype == VSLENGTH) {
|
|---|
| 689 | for (;*val; val++)
|
|---|
| 690 | varlen++;
|
|---|
| 691 | } else {
|
|---|
| 692 | while (*val) {
|
|---|
| 693 | if (quotes && syntax[(int)*val] == CCTL)
|
|---|
| 694 | STPUTC(psh, CTLESC, psh->expdest);
|
|---|
| 695 | STPUTC(psh, *val++, psh->expdest);
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| 698 | }
|
|---|
| 699 | }
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 | apply_ifs = ((varflags & VSQUOTE) == 0 ||
|
|---|
| 704 | (*var == '@' && psh->shellparam.nparam != 1));
|
|---|
| 705 |
|
|---|
| 706 | switch (subtype) {
|
|---|
| 707 | case VSLENGTH:
|
|---|
| 708 | psh->expdest = cvtnum(psh, varlen, psh->expdest);
|
|---|
| 709 | break;
|
|---|
| 710 |
|
|---|
| 711 | case VSNORMAL:
|
|---|
| 712 | break;
|
|---|
| 713 |
|
|---|
| 714 | case VSPLUS:
|
|---|
| 715 | set = !set;
|
|---|
| 716 | /* FALLTHROUGH */
|
|---|
| 717 | case VSMINUS:
|
|---|
| 718 | if (!set) {
|
|---|
| 719 | argstr(psh, p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
|
|---|
| 720 | /*
|
|---|
| 721 | * ${x-a b c} doesn't get split, but removing the
|
|---|
| 722 | * 'apply_ifs = 0' apparantly breaks ${1+"$@"}..
|
|---|
| 723 | * ${x-'a b' c} should generate 2 args.
|
|---|
| 724 | */
|
|---|
| 725 | /* We should have marked stuff already */
|
|---|
| 726 | apply_ifs = 0;
|
|---|
| 727 | }
|
|---|
| 728 | break;
|
|---|
| 729 |
|
|---|
| 730 | case VSTRIMLEFT:
|
|---|
| 731 | case VSTRIMLEFTMAX:
|
|---|
| 732 | case VSTRIMRIGHT:
|
|---|
| 733 | case VSTRIMRIGHTMAX:
|
|---|
| 734 | if (!set)
|
|---|
| 735 | break;
|
|---|
| 736 | /*
|
|---|
| 737 | * Terminate the string and start recording the pattern
|
|---|
| 738 | * right after it
|
|---|
| 739 | */
|
|---|
| 740 | STPUTC(psh, '\0', psh->expdest);
|
|---|
| 741 | patloc = (int)(psh->expdest - stackblock(psh));
|
|---|
| 742 | if (subevalvar(psh, p, NULL, patloc, subtype,
|
|---|
| 743 | startloc, varflags) == 0) {
|
|---|
| 744 | int amount = (int)(psh->expdest - stackblock(psh) - patloc) + 1;
|
|---|
| 745 | STADJUST(psh, -amount, psh->expdest);
|
|---|
| 746 | }
|
|---|
| 747 | /* Remove any recorded regions beyond start of variable */
|
|---|
| 748 | removerecordregions(psh, startloc);
|
|---|
| 749 | apply_ifs = 1;
|
|---|
| 750 | break;
|
|---|
| 751 |
|
|---|
| 752 | case VSASSIGN:
|
|---|
| 753 | case VSQUESTION:
|
|---|
| 754 | if (set)
|
|---|
| 755 | break;
|
|---|
| 756 | if (subevalvar(psh, p, var, 0, subtype, startloc, varflags)) {
|
|---|
| 757 | varflags &= ~VSNUL;
|
|---|
| 758 | /*
|
|---|
| 759 | * Remove any recorded regions beyond
|
|---|
| 760 | * start of variable
|
|---|
| 761 | */
|
|---|
| 762 | removerecordregions(psh, startloc);
|
|---|
| 763 | goto again;
|
|---|
| 764 | }
|
|---|
| 765 | apply_ifs = 0;
|
|---|
| 766 | break;
|
|---|
| 767 |
|
|---|
| 768 | default:
|
|---|
| 769 | sh_abort(psh);
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | if (apply_ifs)
|
|---|
| 773 | recordregion(psh, startloc, (int)(psh->expdest - stackblock(psh)),
|
|---|
| 774 | varflags & VSQUOTE);
|
|---|
| 775 |
|
|---|
| 776 | if (subtype != VSNORMAL) { /* skip to end of alternative */
|
|---|
| 777 | int nesting = 1;
|
|---|
| 778 | for (;;) {
|
|---|
| 779 | if ((c = *p++) == CTLESC)
|
|---|
| 780 | p++;
|
|---|
| 781 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
|
|---|
| 782 | if (set)
|
|---|
| 783 | psh->argbackq = psh->argbackq->next;
|
|---|
| 784 | } else if (c == CTLVAR) {
|
|---|
| 785 | if ((*p++ & VSTYPE) != VSNORMAL)
|
|---|
| 786 | nesting++;
|
|---|
| 787 | } else if (c == CTLENDVAR) {
|
|---|
| 788 | if (--nesting == 0)
|
|---|
| 789 | break;
|
|---|
| 790 | }
|
|---|
| 791 | }
|
|---|
| 792 | }
|
|---|
| 793 | return p;
|
|---|
| 794 | }
|
|---|
| 795 |
|
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 | /*
|
|---|
| 799 | * Test whether a specialized variable is set.
|
|---|
| 800 | */
|
|---|
| 801 |
|
|---|
| 802 | STATIC int
|
|---|
| 803 | varisset(shinstance *psh, char *name, int nulok)
|
|---|
| 804 | {
|
|---|
| 805 | if (*name == '!')
|
|---|
| 806 | return psh->backgndpid != -1;
|
|---|
| 807 | else if (*name == '@' || *name == '*') {
|
|---|
| 808 | if (*psh->shellparam.p == NULL)
|
|---|
| 809 | return 0;
|
|---|
| 810 |
|
|---|
| 811 | if (nulok) {
|
|---|
| 812 | char **av;
|
|---|
| 813 |
|
|---|
| 814 | for (av = psh->shellparam.p; *av; av++)
|
|---|
| 815 | if (**av != '\0')
|
|---|
| 816 | return 1;
|
|---|
| 817 | return 0;
|
|---|
| 818 | }
|
|---|
| 819 | } else if (is_digit(*name)) {
|
|---|
| 820 | char *ap;
|
|---|
| 821 | int num = atoi(name);
|
|---|
| 822 |
|
|---|
| 823 | if (num > psh->shellparam.nparam)
|
|---|
| 824 | return 0;
|
|---|
| 825 |
|
|---|
| 826 | if (num == 0)
|
|---|
| 827 | ap = psh->arg0;
|
|---|
| 828 | else
|
|---|
| 829 | ap = psh->shellparam.p[num - 1];
|
|---|
| 830 |
|
|---|
| 831 | if (nulok && (ap == NULL || *ap == '\0'))
|
|---|
| 832 | return 0;
|
|---|
| 833 | }
|
|---|
| 834 | return 1;
|
|---|
| 835 | }
|
|---|
| 836 |
|
|---|
| 837 |
|
|---|
| 838 |
|
|---|
| 839 | /*
|
|---|
| 840 | * Add the value of a specialized variable to the stack string.
|
|---|
| 841 | */
|
|---|
| 842 |
|
|---|
| 843 | STATIC void
|
|---|
| 844 | varvalue(shinstance *psh, char *name, int quoted, int subtype, int flag)
|
|---|
| 845 | {
|
|---|
| 846 | int num;
|
|---|
| 847 | char *p;
|
|---|
| 848 | int i;
|
|---|
| 849 | char sep;
|
|---|
| 850 | char **ap;
|
|---|
| 851 | char const *syntax;
|
|---|
| 852 |
|
|---|
| 853 | #define STRTODEST(p) \
|
|---|
| 854 | do {\
|
|---|
| 855 | if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
|
|---|
| 856 | syntax = quoted? DQSYNTAX : BASESYNTAX; \
|
|---|
| 857 | while (*p) { \
|
|---|
| 858 | if (syntax[(int)*p] == CCTL) \
|
|---|
| 859 | STPUTC(psh, CTLESC, psh->expdest); \
|
|---|
| 860 | STPUTC(psh, *p++, psh->expdest); \
|
|---|
| 861 | } \
|
|---|
| 862 | } else \
|
|---|
| 863 | while (*p) \
|
|---|
| 864 | STPUTC(psh, *p++, psh->expdest); \
|
|---|
| 865 | } while (0)
|
|---|
| 866 |
|
|---|
| 867 |
|
|---|
| 868 | switch (*name) {
|
|---|
| 869 | case '$':
|
|---|
| 870 | num = psh->rootpid;
|
|---|
| 871 | goto numvar;
|
|---|
| 872 | case '?':
|
|---|
| 873 | num = psh->exitstatus;
|
|---|
| 874 | goto numvar;
|
|---|
| 875 | case '#':
|
|---|
| 876 | num = psh->shellparam.nparam;
|
|---|
| 877 | goto numvar;
|
|---|
| 878 | case '!':
|
|---|
| 879 | num = psh->backgndpid;
|
|---|
| 880 | numvar:
|
|---|
| 881 | psh->expdest = cvtnum(psh, num, psh->expdest);
|
|---|
| 882 | break;
|
|---|
| 883 | case '-':
|
|---|
| 884 | for (i = 0; psh->optlist[i].name; i++) {
|
|---|
| 885 | if (psh->optlist[i].val)
|
|---|
| 886 | STPUTC(psh, psh->optlist[i].letter, psh->expdest);
|
|---|
| 887 | }
|
|---|
| 888 | break;
|
|---|
| 889 | case '@':
|
|---|
| 890 | if (flag & EXP_FULL && quoted) {
|
|---|
| 891 | for (ap = psh->shellparam.p ; (p = *ap++) != NULL ; ) {
|
|---|
| 892 | STRTODEST(p);
|
|---|
| 893 | if (*ap)
|
|---|
| 894 | STPUTC(psh, '\0', psh->expdest);
|
|---|
| 895 | }
|
|---|
| 896 | break;
|
|---|
| 897 | }
|
|---|
| 898 | /* fall through */
|
|---|
| 899 | case '*':
|
|---|
| 900 | if (ifsset(psh) != 0)
|
|---|
| 901 | sep = ifsval(psh)[0];
|
|---|
| 902 | else
|
|---|
| 903 | sep = ' ';
|
|---|
| 904 | for (ap = psh->shellparam.p ; (p = *ap++) != NULL ; ) {
|
|---|
| 905 | STRTODEST(p);
|
|---|
| 906 | if (*ap && sep)
|
|---|
| 907 | STPUTC(psh, sep, psh->expdest);
|
|---|
| 908 | }
|
|---|
| 909 | break;
|
|---|
| 910 | case '0':
|
|---|
| 911 | p = psh->arg0;
|
|---|
| 912 | STRTODEST(p);
|
|---|
| 913 | break;
|
|---|
| 914 | default:
|
|---|
| 915 | if (is_digit(*name)) {
|
|---|
| 916 | num = atoi(name);
|
|---|
| 917 | if (num > 0 && num <= psh->shellparam.nparam) {
|
|---|
| 918 | p = psh->shellparam.p[num - 1];
|
|---|
| 919 | STRTODEST(p);
|
|---|
| 920 | }
|
|---|
| 921 | }
|
|---|
| 922 | break;
|
|---|
| 923 | }
|
|---|
| 924 | }
|
|---|
| 925 |
|
|---|
| 926 |
|
|---|
| 927 |
|
|---|
| 928 | /*
|
|---|
| 929 | * Record the fact that we have to scan this region of the
|
|---|
| 930 | * string for IFS characters.
|
|---|
| 931 | */
|
|---|
| 932 |
|
|---|
| 933 | STATIC void
|
|---|
| 934 | recordregion(shinstance *psh, int start, int end, int inquotes)
|
|---|
| 935 | {
|
|---|
| 936 | struct ifsregion *ifsp;
|
|---|
| 937 |
|
|---|
| 938 | if (psh->ifslastp == NULL) {
|
|---|
| 939 | ifsp = &psh->ifsfirst;
|
|---|
| 940 | } else {
|
|---|
| 941 | if (psh->ifslastp->endoff == start
|
|---|
| 942 | && psh->ifslastp->inquotes == inquotes) {
|
|---|
| 943 | /* extend previous area */
|
|---|
| 944 | psh->ifslastp->endoff = end;
|
|---|
| 945 | return;
|
|---|
| 946 | }
|
|---|
| 947 | ifsp = (struct ifsregion *)ckmalloc(psh, sizeof (struct ifsregion));
|
|---|
| 948 | psh->ifslastp->next = ifsp;
|
|---|
| 949 | }
|
|---|
| 950 | psh->ifslastp = ifsp;
|
|---|
| 951 | psh->ifslastp->next = NULL;
|
|---|
| 952 | psh->ifslastp->begoff = start;
|
|---|
| 953 | psh->ifslastp->endoff = end;
|
|---|
| 954 | psh->ifslastp->inquotes = inquotes;
|
|---|
| 955 | }
|
|---|
| 956 |
|
|---|
| 957 |
|
|---|
| 958 |
|
|---|
| 959 | /*
|
|---|
| 960 | * Break the argument string into pieces based upon IFS and add the
|
|---|
| 961 | * strings to the argument list. The regions of the string to be
|
|---|
| 962 | * searched for IFS characters have been stored by recordregion.
|
|---|
| 963 | */
|
|---|
| 964 | STATIC void
|
|---|
| 965 | ifsbreakup(shinstance *psh, char *string, struct arglist *arglist)
|
|---|
| 966 | {
|
|---|
| 967 | struct ifsregion *ifsp;
|
|---|
| 968 | struct strlist *sp;
|
|---|
| 969 | char *start;
|
|---|
| 970 | char *p;
|
|---|
| 971 | char *q;
|
|---|
| 972 | const char *ifs;
|
|---|
| 973 | const char *ifsspc;
|
|---|
| 974 | int inquotes;
|
|---|
| 975 |
|
|---|
| 976 | start = string;
|
|---|
| 977 | ifsspc = NULL;
|
|---|
| 978 | inquotes = 0;
|
|---|
| 979 |
|
|---|
| 980 | if (psh->ifslastp == NULL) {
|
|---|
| 981 | /* Return entire argument, IFS doesn't apply to any of it */
|
|---|
| 982 | sp = (struct strlist *)stalloc(psh, sizeof *sp);
|
|---|
| 983 | sp->text = start;
|
|---|
| 984 | *arglist->lastp = sp;
|
|---|
| 985 | arglist->lastp = &sp->next;
|
|---|
| 986 | return;
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | ifs = ifsset(psh) ? ifsval(psh) : " \t\n";
|
|---|
| 990 |
|
|---|
| 991 | for (ifsp = &psh->ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
|
|---|
| 992 | p = string + ifsp->begoff;
|
|---|
| 993 | inquotes = ifsp->inquotes;
|
|---|
| 994 | ifsspc = NULL;
|
|---|
| 995 | while (p < string + ifsp->endoff) {
|
|---|
| 996 | q = p;
|
|---|
| 997 | if (*p == CTLESC)
|
|---|
| 998 | p++;
|
|---|
| 999 | if (inquotes) {
|
|---|
| 1000 | /* Only NULs (probably from "$@") end args */
|
|---|
| 1001 | if (*p != 0) {
|
|---|
| 1002 | p++;
|
|---|
| 1003 | continue;
|
|---|
| 1004 | }
|
|---|
| 1005 | } else {
|
|---|
| 1006 | if (!strchr(ifs, *p)) {
|
|---|
| 1007 | p++;
|
|---|
| 1008 | continue;
|
|---|
| 1009 | }
|
|---|
| 1010 | ifsspc = strchr(" \t\n", *p);
|
|---|
| 1011 |
|
|---|
| 1012 | /* Ignore IFS whitespace at start */
|
|---|
| 1013 | if (q == start && ifsspc != NULL) {
|
|---|
| 1014 | p++;
|
|---|
| 1015 | start = p;
|
|---|
| 1016 | continue;
|
|---|
| 1017 | }
|
|---|
| 1018 | }
|
|---|
| 1019 |
|
|---|
| 1020 | /* Save this argument... */
|
|---|
| 1021 | *q = '\0';
|
|---|
| 1022 | sp = (struct strlist *)stalloc(psh, sizeof *sp);
|
|---|
| 1023 | sp->text = start;
|
|---|
| 1024 | *arglist->lastp = sp;
|
|---|
| 1025 | arglist->lastp = &sp->next;
|
|---|
| 1026 | p++;
|
|---|
| 1027 |
|
|---|
| 1028 | if (ifsspc != NULL) {
|
|---|
| 1029 | /* Ignore further trailing IFS whitespace */
|
|---|
| 1030 | for (; p < string + ifsp->endoff; p++) {
|
|---|
| 1031 | q = p;
|
|---|
| 1032 | if (*p == CTLESC)
|
|---|
| 1033 | p++;
|
|---|
| 1034 | if (strchr(ifs, *p) == NULL) {
|
|---|
| 1035 | p = q;
|
|---|
| 1036 | break;
|
|---|
| 1037 | }
|
|---|
| 1038 | if (strchr(" \t\n", *p) == NULL) {
|
|---|
| 1039 | p++;
|
|---|
| 1040 | break;
|
|---|
| 1041 | }
|
|---|
| 1042 | }
|
|---|
| 1043 | }
|
|---|
| 1044 | start = p;
|
|---|
| 1045 | }
|
|---|
| 1046 | }
|
|---|
| 1047 |
|
|---|
| 1048 | /*
|
|---|
| 1049 | * Save anything left as an argument.
|
|---|
| 1050 | * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
|
|---|
| 1051 | * generating 2 arguments, the second of which is empty.
|
|---|
| 1052 | * Some recent clarification of the Posix spec say that it
|
|---|
| 1053 | * should only generate one....
|
|---|
| 1054 | */
|
|---|
| 1055 | if (*start /* || (!ifsspc && start > string) */) {
|
|---|
| 1056 | sp = (struct strlist *)stalloc(psh, sizeof *sp);
|
|---|
| 1057 | sp->text = start;
|
|---|
| 1058 | *arglist->lastp = sp;
|
|---|
| 1059 | arglist->lastp = &sp->next;
|
|---|
| 1060 | }
|
|---|
| 1061 | }
|
|---|
| 1062 |
|
|---|
| 1063 | STATIC void
|
|---|
| 1064 | ifsfree(shinstance *psh)
|
|---|
| 1065 | {
|
|---|
| 1066 | while (psh->ifsfirst.next != NULL) {
|
|---|
| 1067 | struct ifsregion *ifsp;
|
|---|
| 1068 | INTOFF;
|
|---|
| 1069 | ifsp = psh->ifsfirst.next->next;
|
|---|
| 1070 | ckfree(psh, psh->ifsfirst.next);
|
|---|
| 1071 | psh->ifsfirst.next = ifsp;
|
|---|
| 1072 | INTON;
|
|---|
| 1073 | }
|
|---|
| 1074 | psh->ifslastp = NULL;
|
|---|
| 1075 | psh->ifsfirst.next = NULL;
|
|---|
| 1076 | }
|
|---|
| 1077 |
|
|---|
| 1078 |
|
|---|
| 1079 |
|
|---|
| 1080 | /*
|
|---|
| 1081 | * Expand shell metacharacters. At this point, the only control characters
|
|---|
| 1082 | * should be escapes. The results are stored in the list psh->exparg.
|
|---|
| 1083 | */
|
|---|
| 1084 |
|
|---|
| 1085 | //char *expdir;
|
|---|
| 1086 |
|
|---|
| 1087 |
|
|---|
| 1088 | STATIC void
|
|---|
| 1089 | expandmeta(shinstance *psh, struct strlist *str, int flag)
|
|---|
| 1090 | {
|
|---|
| 1091 | char *p;
|
|---|
| 1092 | struct strlist **savelastp;
|
|---|
| 1093 | struct strlist *sp;
|
|---|
| 1094 | char c;
|
|---|
| 1095 | /* TODO - EXP_REDIR */
|
|---|
| 1096 |
|
|---|
| 1097 | while (str) {
|
|---|
| 1098 | if (fflag(psh))
|
|---|
| 1099 | goto nometa;
|
|---|
| 1100 | p = str->text;
|
|---|
| 1101 | for (;;) { /* fast check for meta chars */
|
|---|
| 1102 | if ((c = *p++) == '\0')
|
|---|
| 1103 | goto nometa;
|
|---|
| 1104 | if (c == '*' || c == '?' || c == '[' || c == '!')
|
|---|
| 1105 | break;
|
|---|
| 1106 | }
|
|---|
| 1107 | savelastp = psh->exparg.lastp;
|
|---|
| 1108 | INTOFF;
|
|---|
| 1109 | if (psh->expdir == NULL) {
|
|---|
| 1110 | size_t i = strlen(str->text);
|
|---|
| 1111 | psh->expdir = ckmalloc(psh, i < 2048 ? 2048 : i); /* XXX */
|
|---|
| 1112 | }
|
|---|
| 1113 |
|
|---|
| 1114 | expmeta(psh, psh->expdir, str->text);
|
|---|
| 1115 | ckfree(psh, psh->expdir);
|
|---|
| 1116 | psh->expdir = NULL;
|
|---|
| 1117 | INTON;
|
|---|
| 1118 | if (psh->exparg.lastp == savelastp) {
|
|---|
| 1119 | /*
|
|---|
| 1120 | * no matches
|
|---|
| 1121 | */
|
|---|
| 1122 | nometa:
|
|---|
| 1123 | *psh->exparg.lastp = str;
|
|---|
| 1124 | rmescapes(psh, str->text);
|
|---|
| 1125 | psh->exparg.lastp = &str->next;
|
|---|
| 1126 | } else {
|
|---|
| 1127 | *psh->exparg.lastp = NULL;
|
|---|
| 1128 | *savelastp = sp = expsort(*savelastp);
|
|---|
| 1129 | while (sp->next != NULL)
|
|---|
| 1130 | sp = sp->next;
|
|---|
| 1131 | psh->exparg.lastp = &sp->next;
|
|---|
| 1132 | }
|
|---|
| 1133 | str = str->next;
|
|---|
| 1134 | }
|
|---|
| 1135 | }
|
|---|
| 1136 |
|
|---|
| 1137 |
|
|---|
| 1138 | /*
|
|---|
| 1139 | * Do metacharacter (i.e. *, ?, [...]) expansion.
|
|---|
| 1140 | */
|
|---|
| 1141 |
|
|---|
| 1142 | STATIC void
|
|---|
| 1143 | expmeta(shinstance *psh, char *enddir, char *name)
|
|---|
| 1144 | {
|
|---|
| 1145 | char *p;
|
|---|
| 1146 | const char *cp;
|
|---|
| 1147 | char *q;
|
|---|
| 1148 | char *start;
|
|---|
| 1149 | char *endname;
|
|---|
| 1150 | int metaflag;
|
|---|
| 1151 | struct stat statb;
|
|---|
| 1152 | shdir *dirp;
|
|---|
| 1153 | shdirent *dp;
|
|---|
| 1154 | int atend;
|
|---|
| 1155 | int matchdot;
|
|---|
| 1156 |
|
|---|
| 1157 | metaflag = 0;
|
|---|
| 1158 | start = name;
|
|---|
| 1159 | for (p = name ; ; p++) {
|
|---|
| 1160 | if (*p == '*' || *p == '?')
|
|---|
| 1161 | metaflag = 1;
|
|---|
| 1162 | else if (*p == '[') {
|
|---|
| 1163 | q = p + 1;
|
|---|
| 1164 | if (*q == '!')
|
|---|
| 1165 | q++;
|
|---|
| 1166 | for (;;) {
|
|---|
| 1167 | while (*q == CTLQUOTEMARK)
|
|---|
| 1168 | q++;
|
|---|
| 1169 | if (*q == CTLESC)
|
|---|
| 1170 | q++;
|
|---|
| 1171 | if (*q == '/' || *q == '\0')
|
|---|
| 1172 | break;
|
|---|
| 1173 | if (*++q == ']') {
|
|---|
| 1174 | metaflag = 1;
|
|---|
| 1175 | break;
|
|---|
| 1176 | }
|
|---|
| 1177 | }
|
|---|
| 1178 | } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
|
|---|
| 1179 | metaflag = 1;
|
|---|
| 1180 | } else if (*p == '\0')
|
|---|
| 1181 | break;
|
|---|
| 1182 | else if (*p == CTLQUOTEMARK)
|
|---|
| 1183 | continue;
|
|---|
| 1184 | else if (*p == CTLESC)
|
|---|
| 1185 | p++;
|
|---|
| 1186 | if (*p == '/') {
|
|---|
| 1187 | if (metaflag)
|
|---|
| 1188 | break;
|
|---|
| 1189 | start = p + 1;
|
|---|
| 1190 | }
|
|---|
| 1191 | }
|
|---|
| 1192 | if (metaflag == 0) { /* we've reached the end of the file name */
|
|---|
| 1193 | if (enddir != psh->expdir)
|
|---|
| 1194 | metaflag++;
|
|---|
| 1195 | for (p = name ; ; p++) {
|
|---|
| 1196 | if (*p == CTLQUOTEMARK)
|
|---|
| 1197 | continue;
|
|---|
| 1198 | if (*p == CTLESC)
|
|---|
| 1199 | p++;
|
|---|
| 1200 | *enddir++ = *p;
|
|---|
| 1201 | if (*p == '\0')
|
|---|
| 1202 | break;
|
|---|
| 1203 | }
|
|---|
| 1204 | if (metaflag == 0 || shfile_lstat(&psh->fdtab, psh->expdir, &statb) >= 0)
|
|---|
| 1205 | addfname(psh, psh->expdir);
|
|---|
| 1206 | return;
|
|---|
| 1207 | }
|
|---|
| 1208 | endname = p;
|
|---|
| 1209 | if (start != name) {
|
|---|
| 1210 | p = name;
|
|---|
| 1211 | while (p < start) {
|
|---|
| 1212 | while (*p == CTLQUOTEMARK)
|
|---|
| 1213 | p++;
|
|---|
| 1214 | if (*p == CTLESC)
|
|---|
| 1215 | p++;
|
|---|
| 1216 | *enddir++ = *p++;
|
|---|
| 1217 | }
|
|---|
| 1218 | }
|
|---|
| 1219 | if (enddir == psh->expdir) {
|
|---|
| 1220 | cp = ".";
|
|---|
| 1221 | } else if (enddir == psh->expdir + 1 && *psh->expdir == '/') {
|
|---|
| 1222 | cp = "/";
|
|---|
| 1223 | } else {
|
|---|
| 1224 | cp = psh->expdir;
|
|---|
| 1225 | enddir[-1] = '\0';
|
|---|
| 1226 | }
|
|---|
| 1227 | if ((dirp = shfile_opendir(&psh->fdtab, cp)) == NULL)
|
|---|
| 1228 | return;
|
|---|
| 1229 | if (enddir != psh->expdir)
|
|---|
| 1230 | enddir[-1] = '/';
|
|---|
| 1231 | if (*endname == 0) {
|
|---|
| 1232 | atend = 1;
|
|---|
| 1233 | } else {
|
|---|
| 1234 | atend = 0;
|
|---|
| 1235 | *endname++ = '\0';
|
|---|
| 1236 | }
|
|---|
| 1237 | matchdot = 0;
|
|---|
| 1238 | p = start;
|
|---|
| 1239 | while (*p == CTLQUOTEMARK)
|
|---|
| 1240 | p++;
|
|---|
| 1241 | if (*p == CTLESC)
|
|---|
| 1242 | p++;
|
|---|
| 1243 | if (*p == '.')
|
|---|
| 1244 | matchdot++;
|
|---|
| 1245 | while (! int_pending() && (dp = shfile_readdir(dirp)) != NULL) {
|
|---|
| 1246 | if (dp->name[0] == '.' && ! matchdot)
|
|---|
| 1247 | continue;
|
|---|
| 1248 | if (patmatch(psh, start, dp->name, 0)) {
|
|---|
| 1249 | if (atend) {
|
|---|
| 1250 | scopy(dp->name, enddir);
|
|---|
| 1251 | addfname(psh, psh->expdir);
|
|---|
| 1252 | } else {
|
|---|
| 1253 | for (p = enddir, cp = dp->name;
|
|---|
| 1254 | (*p++ = *cp++) != '\0';)
|
|---|
| 1255 | continue;
|
|---|
| 1256 | p[-1] = '/';
|
|---|
| 1257 | expmeta(psh, p, endname);
|
|---|
| 1258 | }
|
|---|
| 1259 | }
|
|---|
| 1260 | }
|
|---|
| 1261 | shfile_closedir(dirp);
|
|---|
| 1262 | if (! atend)
|
|---|
| 1263 | endname[-1] = '/';
|
|---|
| 1264 | }
|
|---|
| 1265 |
|
|---|
| 1266 |
|
|---|
| 1267 | /*
|
|---|
| 1268 | * Add a file name to the list.
|
|---|
| 1269 | */
|
|---|
| 1270 |
|
|---|
| 1271 | STATIC void
|
|---|
| 1272 | addfname(shinstance *psh, char *name)
|
|---|
| 1273 | {
|
|---|
| 1274 | char *p;
|
|---|
| 1275 | struct strlist *sp;
|
|---|
| 1276 |
|
|---|
| 1277 | p = stalloc(psh, strlen(name) + 1);
|
|---|
| 1278 | scopy(name, p);
|
|---|
| 1279 | sp = (struct strlist *)stalloc(psh, sizeof *sp);
|
|---|
| 1280 | sp->text = p;
|
|---|
| 1281 | *psh->exparg.lastp = sp;
|
|---|
| 1282 | psh->exparg.lastp = &sp->next;
|
|---|
| 1283 | }
|
|---|
| 1284 |
|
|---|
| 1285 |
|
|---|
| 1286 | /*
|
|---|
| 1287 | * Sort the results of file name expansion. It calculates the number of
|
|---|
| 1288 | * strings to sort and then calls msort (short for merge sort) to do the
|
|---|
| 1289 | * work.
|
|---|
| 1290 | */
|
|---|
| 1291 |
|
|---|
| 1292 | STATIC struct strlist *
|
|---|
| 1293 | expsort(struct strlist *str)
|
|---|
| 1294 | {
|
|---|
| 1295 | int len;
|
|---|
| 1296 | struct strlist *sp;
|
|---|
| 1297 |
|
|---|
| 1298 | len = 0;
|
|---|
| 1299 | for (sp = str ; sp ; sp = sp->next)
|
|---|
| 1300 | len++;
|
|---|
| 1301 | return msort(str, len);
|
|---|
| 1302 | }
|
|---|
| 1303 |
|
|---|
| 1304 |
|
|---|
| 1305 | STATIC struct strlist *
|
|---|
| 1306 | msort(struct strlist *list, int len)
|
|---|
| 1307 | {
|
|---|
| 1308 | struct strlist *p, *q = NULL;
|
|---|
| 1309 | struct strlist **lpp;
|
|---|
| 1310 | int half;
|
|---|
| 1311 | int n;
|
|---|
| 1312 |
|
|---|
| 1313 | if (len <= 1)
|
|---|
| 1314 | return list;
|
|---|
| 1315 | half = len >> 1;
|
|---|
| 1316 | p = list;
|
|---|
| 1317 | for (n = half ; --n >= 0 ; ) {
|
|---|
| 1318 | q = p;
|
|---|
| 1319 | p = p->next;
|
|---|
| 1320 | }
|
|---|
| 1321 | q->next = NULL; /* terminate first half of list */
|
|---|
| 1322 | q = msort(list, half); /* sort first half of list */
|
|---|
| 1323 | p = msort(p, len - half); /* sort second half */
|
|---|
| 1324 | lpp = &list;
|
|---|
| 1325 | for (;;) {
|
|---|
| 1326 | if (strcmp(p->text, q->text) < 0) {
|
|---|
| 1327 | *lpp = p;
|
|---|
| 1328 | lpp = &p->next;
|
|---|
| 1329 | if ((p = *lpp) == NULL) {
|
|---|
| 1330 | *lpp = q;
|
|---|
| 1331 | break;
|
|---|
| 1332 | }
|
|---|
| 1333 | } else {
|
|---|
| 1334 | *lpp = q;
|
|---|
| 1335 | lpp = &q->next;
|
|---|
| 1336 | if ((q = *lpp) == NULL) {
|
|---|
| 1337 | *lpp = p;
|
|---|
| 1338 | break;
|
|---|
| 1339 | }
|
|---|
| 1340 | }
|
|---|
| 1341 | }
|
|---|
| 1342 | return list;
|
|---|
| 1343 | }
|
|---|
| 1344 |
|
|---|
| 1345 |
|
|---|
| 1346 |
|
|---|
| 1347 | /*
|
|---|
| 1348 | * Returns true if the pattern matches the string.
|
|---|
| 1349 | */
|
|---|
| 1350 |
|
|---|
| 1351 | int
|
|---|
| 1352 | patmatch(shinstance *psh, char *pattern, char *string, int squoted)
|
|---|
| 1353 | {
|
|---|
| 1354 | #ifdef notdef
|
|---|
| 1355 | if (pattern[0] == '!' && pattern[1] == '!')
|
|---|
| 1356 | return 1 - pmatch(pattern + 2, string);
|
|---|
| 1357 | else
|
|---|
| 1358 | #endif
|
|---|
| 1359 | return pmatch(pattern, string, squoted);
|
|---|
| 1360 | }
|
|---|
| 1361 |
|
|---|
| 1362 |
|
|---|
| 1363 | STATIC int
|
|---|
| 1364 | pmatch(char *pattern, char *string, int squoted)
|
|---|
| 1365 | {
|
|---|
| 1366 | char *p, *q;
|
|---|
| 1367 | char c;
|
|---|
| 1368 |
|
|---|
| 1369 | p = pattern;
|
|---|
| 1370 | q = string;
|
|---|
| 1371 | for (;;) {
|
|---|
| 1372 | switch (c = *p++) {
|
|---|
| 1373 | case '\0':
|
|---|
| 1374 | goto breakloop;
|
|---|
| 1375 | case CTLESC:
|
|---|
| 1376 | if (squoted && *q == CTLESC)
|
|---|
| 1377 | q++;
|
|---|
| 1378 | if (*q++ != *p++)
|
|---|
| 1379 | return 0;
|
|---|
| 1380 | break;
|
|---|
| 1381 | case CTLQUOTEMARK:
|
|---|
| 1382 | continue;
|
|---|
| 1383 | case '?':
|
|---|
| 1384 | if (squoted && *q == CTLESC)
|
|---|
| 1385 | q++;
|
|---|
| 1386 | if (*q++ == '\0')
|
|---|
| 1387 | return 0;
|
|---|
| 1388 | break;
|
|---|
| 1389 | case '*':
|
|---|
| 1390 | c = *p;
|
|---|
| 1391 | while (c == CTLQUOTEMARK || c == '*')
|
|---|
| 1392 | c = *++p;
|
|---|
| 1393 | if (c != CTLESC && c != CTLQUOTEMARK &&
|
|---|
| 1394 | c != '?' && c != '*' && c != '[') {
|
|---|
| 1395 | while (*q != c) {
|
|---|
| 1396 | if (squoted && *q == CTLESC &&
|
|---|
| 1397 | q[1] == c)
|
|---|
| 1398 | break;
|
|---|
| 1399 | if (*q == '\0')
|
|---|
| 1400 | return 0;
|
|---|
| 1401 | if (squoted && *q == CTLESC)
|
|---|
| 1402 | q++;
|
|---|
| 1403 | q++;
|
|---|
| 1404 | }
|
|---|
| 1405 | }
|
|---|
| 1406 | do {
|
|---|
| 1407 | if (pmatch(p, q, squoted))
|
|---|
| 1408 | return 1;
|
|---|
| 1409 | if (squoted && *q == CTLESC)
|
|---|
| 1410 | q++;
|
|---|
| 1411 | } while (*q++ != '\0');
|
|---|
| 1412 | return 0;
|
|---|
| 1413 | case '[': {
|
|---|
| 1414 | char *endp;
|
|---|
| 1415 | int invert, found;
|
|---|
| 1416 | char chr;
|
|---|
| 1417 |
|
|---|
| 1418 | endp = p;
|
|---|
| 1419 | if (*endp == '!')
|
|---|
| 1420 | endp++;
|
|---|
| 1421 | for (;;) {
|
|---|
| 1422 | while (*endp == CTLQUOTEMARK)
|
|---|
| 1423 | endp++;
|
|---|
| 1424 | if (*endp == '\0')
|
|---|
| 1425 | goto dft; /* no matching ] */
|
|---|
| 1426 | if (*endp == CTLESC)
|
|---|
| 1427 | endp++;
|
|---|
| 1428 | if (*++endp == ']')
|
|---|
| 1429 | break;
|
|---|
| 1430 | }
|
|---|
| 1431 | invert = 0;
|
|---|
| 1432 | if (*p == '!') {
|
|---|
| 1433 | invert++;
|
|---|
| 1434 | p++;
|
|---|
| 1435 | }
|
|---|
| 1436 | found = 0;
|
|---|
| 1437 | chr = *q++;
|
|---|
| 1438 | if (squoted && chr == CTLESC)
|
|---|
| 1439 | chr = *q++;
|
|---|
| 1440 | if (chr == '\0')
|
|---|
| 1441 | return 0;
|
|---|
| 1442 | c = *p++;
|
|---|
| 1443 | do {
|
|---|
| 1444 | if (c == CTLQUOTEMARK)
|
|---|
| 1445 | continue;
|
|---|
| 1446 | if (c == CTLESC)
|
|---|
| 1447 | c = *p++;
|
|---|
| 1448 | if (*p == '-' && p[1] != ']') {
|
|---|
| 1449 | p++;
|
|---|
| 1450 | while (*p == CTLQUOTEMARK)
|
|---|
| 1451 | p++;
|
|---|
| 1452 | if (*p == CTLESC)
|
|---|
| 1453 | p++;
|
|---|
| 1454 | if (chr >= c && chr <= *p)
|
|---|
| 1455 | found = 1;
|
|---|
| 1456 | p++;
|
|---|
| 1457 | } else {
|
|---|
| 1458 | if (chr == c)
|
|---|
| 1459 | found = 1;
|
|---|
| 1460 | }
|
|---|
| 1461 | } while ((c = *p++) != ']');
|
|---|
| 1462 | if (found == invert)
|
|---|
| 1463 | return 0;
|
|---|
| 1464 | break;
|
|---|
| 1465 | }
|
|---|
| 1466 | dft: default:
|
|---|
| 1467 | if (squoted && *q == CTLESC)
|
|---|
| 1468 | q++;
|
|---|
| 1469 | if (*q++ != c)
|
|---|
| 1470 | return 0;
|
|---|
| 1471 | break;
|
|---|
| 1472 | }
|
|---|
| 1473 | }
|
|---|
| 1474 | breakloop:
|
|---|
| 1475 | if (*q != '\0')
|
|---|
| 1476 | return 0;
|
|---|
| 1477 | return 1;
|
|---|
| 1478 | }
|
|---|
| 1479 |
|
|---|
| 1480 |
|
|---|
| 1481 |
|
|---|
| 1482 | /*
|
|---|
| 1483 | * Remove any CTLESC characters from a string.
|
|---|
| 1484 | */
|
|---|
| 1485 |
|
|---|
| 1486 | void
|
|---|
| 1487 | rmescapes(shinstance *psh, char *str)
|
|---|
| 1488 | {
|
|---|
| 1489 | char *p, *q;
|
|---|
| 1490 |
|
|---|
| 1491 | p = str;
|
|---|
| 1492 | while (*p != CTLESC && *p != CTLQUOTEMARK) {
|
|---|
| 1493 | if (*p++ == '\0')
|
|---|
| 1494 | return;
|
|---|
| 1495 | }
|
|---|
| 1496 | q = p;
|
|---|
| 1497 | while (*p) {
|
|---|
| 1498 | if (*p == CTLQUOTEMARK) {
|
|---|
| 1499 | p++;
|
|---|
| 1500 | continue;
|
|---|
| 1501 | }
|
|---|
| 1502 | if (*p == CTLESC)
|
|---|
| 1503 | p++;
|
|---|
| 1504 | *q++ = *p++;
|
|---|
| 1505 | }
|
|---|
| 1506 | *q = '\0';
|
|---|
| 1507 | }
|
|---|
| 1508 |
|
|---|
| 1509 |
|
|---|
| 1510 |
|
|---|
| 1511 | /*
|
|---|
| 1512 | * See if a pattern matches in a case statement.
|
|---|
| 1513 | */
|
|---|
| 1514 |
|
|---|
| 1515 | int
|
|---|
| 1516 | casematch(shinstance *psh, union node *pattern, char *val)
|
|---|
| 1517 | {
|
|---|
| 1518 | struct stackmark smark;
|
|---|
| 1519 | int result;
|
|---|
| 1520 | char *p;
|
|---|
| 1521 |
|
|---|
| 1522 | setstackmark(psh, &smark);
|
|---|
| 1523 | psh->argbackq = pattern->narg.backquote;
|
|---|
| 1524 | STARTSTACKSTR(psh, psh->expdest);
|
|---|
| 1525 | psh->ifslastp = NULL;
|
|---|
| 1526 | argstr(psh, pattern->narg.text, EXP_TILDE | EXP_CASE);
|
|---|
| 1527 | STPUTC(psh, '\0', psh->expdest);
|
|---|
| 1528 | p = grabstackstr(psh, psh->expdest);
|
|---|
| 1529 | result = patmatch(psh, p, val, 0);
|
|---|
| 1530 | popstackmark(psh, &smark);
|
|---|
| 1531 | return result;
|
|---|
| 1532 | }
|
|---|
| 1533 |
|
|---|
| 1534 | /*
|
|---|
| 1535 | * Our own itoa().
|
|---|
| 1536 | */
|
|---|
| 1537 |
|
|---|
| 1538 | STATIC char *
|
|---|
| 1539 | cvtnum(shinstance *psh, int num, char *buf)
|
|---|
| 1540 | {
|
|---|
| 1541 | char temp[32];
|
|---|
| 1542 | int neg = num < 0;
|
|---|
| 1543 | char *p = temp + 31;
|
|---|
| 1544 |
|
|---|
| 1545 | temp[31] = '\0';
|
|---|
| 1546 |
|
|---|
| 1547 | do {
|
|---|
| 1548 | *--p = num % 10 + '0';
|
|---|
| 1549 | } while ((num /= 10) != 0);
|
|---|
| 1550 |
|
|---|
| 1551 | if (neg)
|
|---|
| 1552 | *--p = '-';
|
|---|
| 1553 |
|
|---|
| 1554 | while (*p)
|
|---|
| 1555 | STPUTC(psh, *p++, buf);
|
|---|
| 1556 | return buf;
|
|---|
| 1557 | }
|
|---|
| 1558 |
|
|---|
| 1559 | /*
|
|---|
| 1560 | * Do most of the work for wordexp(3).
|
|---|
| 1561 | */
|
|---|
| 1562 |
|
|---|
| 1563 | int
|
|---|
| 1564 | wordexpcmd(shinstance *psh, int argc, char **argv)
|
|---|
| 1565 | {
|
|---|
| 1566 | size_t len;
|
|---|
| 1567 | int i;
|
|---|
| 1568 |
|
|---|
| 1569 | out1fmt(psh, "%d", argc - 1);
|
|---|
| 1570 | out1c(psh, '\0');
|
|---|
| 1571 | for (i = 1, len = 0; i < argc; i++)
|
|---|
| 1572 | len += strlen(argv[i]);
|
|---|
| 1573 | out1fmt(psh, "%zd", len);
|
|---|
| 1574 | out1c(psh, '\0');
|
|---|
| 1575 | for (i = 1; i < argc; i++) {
|
|---|
| 1576 | out1str(psh, argv[i]);
|
|---|
| 1577 | out1c(psh, '\0');
|
|---|
| 1578 | }
|
|---|
| 1579 | return (0);
|
|---|
| 1580 | }
|
|---|