1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
2010-05-11 23:19:28 +00:00
|
|
|
* Copyright (c) 1997-2005
|
|
|
|
* Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
|
1994-05-26 06:18:55 +00:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
2010-10-13 13:22:11 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
1996-10-31 07:15:57 +00:00
|
|
|
#include <limits.h>
|
2010-10-13 13:22:11 +00:00
|
|
|
#include <pwd.h>
|
1998-09-06 21:13:09 +00:00
|
|
|
#include <stdio.h>
|
2010-10-13 13:22:11 +00:00
|
|
|
#include <stdlib.h>
|
2002-12-26 14:28:54 +00:00
|
|
|
#include <string.h>
|
2010-10-13 13:22:11 +00:00
|
|
|
#include <unistd.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Routines to expand arguments to commands. We have to deal with
|
|
|
|
* backquotes, shell variables, and file metacharacters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "nodes.h"
|
|
|
|
#include "eval.h"
|
|
|
|
#include "expand.h"
|
|
|
|
#include "syntax.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "var.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "mystring.h"
|
1996-09-01 10:22:36 +00:00
|
|
|
#include "arith.h"
|
|
|
|
#include "show.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Structure specifying which parts of the string should be searched
|
|
|
|
* for IFS characters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct ifsregion {
|
|
|
|
struct ifsregion *next; /* next region in list */
|
|
|
|
int begoff; /* offset of start of region */
|
|
|
|
int endoff; /* offset of end of region */
|
2009-06-25 17:10:51 +00:00
|
|
|
int inquotes; /* search for nul bytes only */
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static char *expdest; /* output of current string */
|
|
|
|
static struct nodelist *argbackq; /* list of back quote expressions */
|
|
|
|
static struct ifsregion ifsfirst; /* first struct in list of ifs regions */
|
|
|
|
static struct ifsregion *ifslastp; /* last struct in list */
|
|
|
|
static struct arglist exparg; /* holds expanded arg list */
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void argstr(char *, int);
|
|
|
|
static char *exptilde(char *, int);
|
|
|
|
static void expbackq(union node *, int, int);
|
2010-10-29 19:34:57 +00:00
|
|
|
static int subevalvar(char *, char *, int, int, int, int, int);
|
2010-10-13 22:18:03 +00:00
|
|
|
static char *evalvar(char *, int);
|
|
|
|
static int varisset(char *, int);
|
|
|
|
static void varvalue(char *, int, int, int);
|
|
|
|
static void recordregion(int, int, int);
|
|
|
|
static void removerecordregions(int);
|
|
|
|
static void ifsbreakup(char *, struct arglist *);
|
|
|
|
static void expandmeta(struct strlist *, int);
|
|
|
|
static void expmeta(char *, char *);
|
|
|
|
static void addfname(char *);
|
|
|
|
static struct strlist *expsort(struct strlist *);
|
|
|
|
static struct strlist *msort(struct strlist *, int);
|
|
|
|
static char *cvtnum(int, char *);
|
|
|
|
static int collate_range_cmp(int, int);
|
|
|
|
|
|
|
|
static int
|
2003-08-03 04:28:10 +00:00
|
|
|
collate_range_cmp(int c1, int c2)
|
1996-10-31 07:15:57 +00:00
|
|
|
{
|
|
|
|
static char s1[2], s2[2];
|
|
|
|
|
|
|
|
s1[0] = c1;
|
|
|
|
s2[0] = c2;
|
2003-08-03 04:28:10 +00:00
|
|
|
return (strcoll(s1, s2));
|
1996-10-31 07:15:57 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand shell variables and backquotes inside a here document.
|
2002-02-02 06:50:57 +00:00
|
|
|
* union node *arg the document
|
|
|
|
* int fd; where to write the expanded version
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
expandhere(union node *arg, int fd)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
expandarg(arg, (struct arglist *)NULL, 0);
|
1998-09-13 19:24:57 +00:00
|
|
|
xwrite(fd, stackblock(), expdest - stackblock());
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-11 22:13:29 +00:00
|
|
|
static char *
|
|
|
|
stputs_quotes(const char *data, const char *syntax, char *p)
|
|
|
|
{
|
|
|
|
while (*data) {
|
|
|
|
CHECKSTRSPACE(2, p);
|
|
|
|
if (syntax[(int)*data] == CCTL)
|
|
|
|
USTPUTC(CTLESC, p);
|
|
|
|
USTPUTC(*data++, p);
|
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
#define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
2010-09-05 21:12:48 +00:00
|
|
|
* Perform expansions on an argument, placing the resulting list of arguments
|
|
|
|
* in arglist. Parameter expansion, command substitution and arithmetic
|
|
|
|
* expansion are always performed; additional expansions can be requested
|
|
|
|
* via flag (EXP_*).
|
|
|
|
* The result is left in the stack string.
|
|
|
|
* When arglist is NULL, perform here document expansion. A partial result
|
|
|
|
* may be written to herefd, which is then not included in the stack string.
|
|
|
|
*
|
|
|
|
* Caution: this function uses global state and is not reentrant.
|
|
|
|
* However, a new invocation after an interrupted invocation is safe
|
|
|
|
* and will reset the global state for the new call.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
expandarg(union node *arg, struct arglist *arglist, int flag)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct strlist *sp;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
argbackq = arg->narg.backquote;
|
|
|
|
STARTSTACKSTR(expdest);
|
|
|
|
ifsfirst.next = NULL;
|
|
|
|
ifslastp = NULL;
|
|
|
|
argstr(arg->narg.text, flag);
|
|
|
|
if (arglist == NULL) {
|
|
|
|
return; /* here document expanded */
|
|
|
|
}
|
|
|
|
STPUTC('\0', expdest);
|
|
|
|
p = grabstackstr(expdest);
|
|
|
|
exparg.lastp = &exparg.list;
|
|
|
|
/*
|
|
|
|
* TODO - EXP_REDIR
|
|
|
|
*/
|
|
|
|
if (flag & EXP_FULL) {
|
|
|
|
ifsbreakup(p, &exparg);
|
|
|
|
*exparg.lastp = NULL;
|
|
|
|
exparg.lastp = &exparg.list;
|
|
|
|
expandmeta(exparg.list, flag);
|
|
|
|
} else {
|
|
|
|
if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
|
|
|
|
rmescapes(p);
|
|
|
|
sp = (struct strlist *)stalloc(sizeof (struct strlist));
|
|
|
|
sp->text = p;
|
|
|
|
*exparg.lastp = sp;
|
|
|
|
exparg.lastp = &sp->next;
|
|
|
|
}
|
|
|
|
while (ifsfirst.next != NULL) {
|
|
|
|
struct ifsregion *ifsp;
|
|
|
|
INTOFF;
|
|
|
|
ifsp = ifsfirst.next->next;
|
|
|
|
ckfree(ifsfirst.next);
|
|
|
|
ifsfirst.next = ifsp;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
*exparg.lastp = NULL;
|
|
|
|
if (exparg.list) {
|
|
|
|
*arglist->lastp = exparg.list;
|
|
|
|
arglist->lastp = exparg.lastp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2010-09-05 21:12:48 +00:00
|
|
|
* Perform parameter expansion, command substitution and arithmetic
|
|
|
|
* expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
|
|
|
|
* Processing ends at a CTLENDVAR character as well as '\0'.
|
|
|
|
* This is used to expand word in ${var+word} etc.
|
|
|
|
* If EXP_FULL, EXP_CASE or EXP_REDIR are set, keep and/or generate CTLESC
|
|
|
|
* characters to allow for further processing.
|
|
|
|
* If EXP_FULL is set, also preserve CTLQUOTEMARK characters.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
argstr(char *p, int flag)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1997-04-28 03:28:43 +00:00
|
|
|
char c;
|
2002-10-08 11:22:49 +00:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
|
1994-05-26 06:18:55 +00:00
|
|
|
int firsteq = 1;
|
2010-10-29 13:42:18 +00:00
|
|
|
int split_lit;
|
|
|
|
int lit_quoted;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-29 13:42:18 +00:00
|
|
|
split_lit = flag & EXP_SPLIT_LIT;
|
|
|
|
lit_quoted = flag & EXP_LIT_QUOTED;
|
|
|
|
flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
|
|
|
|
p = exptilde(p, flag);
|
|
|
|
for (;;) {
|
2010-11-23 22:17:39 +00:00
|
|
|
CHECKSTRSPACE(2, expdest);
|
1994-05-26 06:18:55 +00:00
|
|
|
switch (c = *p++) {
|
|
|
|
case '\0':
|
2010-09-05 21:12:48 +00:00
|
|
|
case CTLENDVAR:
|
1994-05-26 06:18:55 +00:00
|
|
|
goto breakloop;
|
1998-09-06 21:13:09 +00:00
|
|
|
case CTLQUOTEMARK:
|
2010-10-29 13:42:18 +00:00
|
|
|
lit_quoted = 1;
|
1998-09-06 21:13:09 +00:00
|
|
|
/* "$@" syntax adherence hack */
|
|
|
|
if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
|
|
|
|
break;
|
1998-09-13 19:24:57 +00:00
|
|
|
if ((flag & EXP_FULL) != 0)
|
2010-11-23 22:17:39 +00:00
|
|
|
USTPUTC(c, expdest);
|
1998-09-06 21:13:09 +00:00
|
|
|
break;
|
2010-10-29 13:42:18 +00:00
|
|
|
case CTLQUOTEEND:
|
|
|
|
lit_quoted = 0;
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
case CTLESC:
|
|
|
|
if (quotes)
|
2010-11-23 22:17:39 +00:00
|
|
|
USTPUTC(c, expdest);
|
1994-05-26 06:18:55 +00:00
|
|
|
c = *p++;
|
2010-11-23 22:17:39 +00:00
|
|
|
USTPUTC(c, expdest);
|
2010-10-29 13:42:18 +00:00
|
|
|
if (split_lit && !lit_quoted)
|
|
|
|
recordregion(expdest - stackblock() -
|
|
|
|
(quotes ? 2 : 1),
|
|
|
|
expdest - stackblock(), 0);
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
case CTLVAR:
|
|
|
|
p = evalvar(p, flag);
|
|
|
|
break;
|
|
|
|
case CTLBACKQ:
|
|
|
|
case CTLBACKQ|CTLQUOTE:
|
|
|
|
expbackq(argbackq->n, c & CTLQUOTE, flag);
|
|
|
|
argbackq = argbackq->next;
|
|
|
|
break;
|
|
|
|
case CTLENDARI:
|
|
|
|
expari(flag);
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
case '=':
|
|
|
|
/*
|
|
|
|
* sort of a hack - expand tildes in variable
|
|
|
|
* assignments (after the first '=' and after ':'s).
|
|
|
|
*/
|
2010-11-23 22:17:39 +00:00
|
|
|
USTPUTC(c, expdest);
|
2010-10-29 13:42:18 +00:00
|
|
|
if (split_lit && !lit_quoted)
|
|
|
|
recordregion(expdest - stackblock() - 1,
|
|
|
|
expdest - stackblock(), 0);
|
|
|
|
if (flag & EXP_VARTILDE && *p == '~' &&
|
|
|
|
(c != '=' || firsteq)) {
|
|
|
|
if (c == '=')
|
|
|
|
firsteq = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
p = exptilde(p, flag);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2010-11-23 22:17:39 +00:00
|
|
|
USTPUTC(c, expdest);
|
2010-10-29 13:42:18 +00:00
|
|
|
if (split_lit && !lit_quoted)
|
|
|
|
recordregion(expdest - stackblock() - 1,
|
|
|
|
expdest - stackblock(), 0);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
breakloop:;
|
|
|
|
}
|
|
|
|
|
2010-09-05 21:12:48 +00:00
|
|
|
/*
|
|
|
|
* Perform tilde expansion, placing the result in the stack string and
|
|
|
|
* returning the next position in the input string to process.
|
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static char *
|
2002-02-02 06:50:57 +00:00
|
|
|
exptilde(char *p, int flag)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char c, *startp = p;
|
|
|
|
struct passwd *pw;
|
|
|
|
char *home;
|
2003-01-08 10:50:08 +00:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
while ((c = *p) != '\0') {
|
1994-05-26 06:18:55 +00:00
|
|
|
switch(c) {
|
2009-12-25 15:29:18 +00:00
|
|
|
case CTLESC: /* This means CTL* are always considered quoted. */
|
|
|
|
case CTLVAR:
|
|
|
|
case CTLBACKQ:
|
|
|
|
case CTLBACKQ | CTLQUOTE:
|
|
|
|
case CTLARI:
|
|
|
|
case CTLENDARI:
|
1998-09-13 19:24:57 +00:00
|
|
|
case CTLQUOTEMARK:
|
|
|
|
return (startp);
|
1994-05-26 06:18:55 +00:00
|
|
|
case ':':
|
|
|
|
if (flag & EXP_VARTILDE)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
case '/':
|
2010-04-03 22:04:44 +00:00
|
|
|
case CTLENDVAR:
|
1994-05-26 06:18:55 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
*p = '\0';
|
|
|
|
if (*(startp+1) == '\0') {
|
|
|
|
if ((home = lookupvar("HOME")) == NULL)
|
|
|
|
goto lose;
|
|
|
|
} else {
|
|
|
|
if ((pw = getpwnam(startp+1)) == NULL)
|
|
|
|
goto lose;
|
|
|
|
home = pw->pw_dir;
|
|
|
|
}
|
|
|
|
if (*home == '\0')
|
|
|
|
goto lose;
|
|
|
|
*p = c;
|
2010-12-11 22:13:29 +00:00
|
|
|
if (quotes)
|
|
|
|
STPUTS_QUOTES(home, SQSYNTAX, expdest);
|
|
|
|
else
|
|
|
|
STPUTS(home, expdest);
|
1994-05-26 06:18:55 +00:00
|
|
|
return (p);
|
|
|
|
lose:
|
|
|
|
*p = c;
|
|
|
|
return (startp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
removerecordregions(int endoff)
|
1998-09-06 21:13:09 +00:00
|
|
|
{
|
|
|
|
if (ifslastp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ifsfirst.endoff > endoff) {
|
|
|
|
while (ifsfirst.next != NULL) {
|
|
|
|
struct ifsregion *ifsp;
|
|
|
|
INTOFF;
|
|
|
|
ifsp = ifsfirst.next->next;
|
|
|
|
ckfree(ifsfirst.next);
|
|
|
|
ifsfirst.next = ifsp;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
if (ifsfirst.begoff > endoff)
|
|
|
|
ifslastp = NULL;
|
|
|
|
else {
|
|
|
|
ifslastp = &ifsfirst;
|
|
|
|
ifsfirst.endoff = endoff;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2006-02-04 14:37:50 +00:00
|
|
|
|
1998-09-06 21:13:09 +00:00
|
|
|
ifslastp = &ifsfirst;
|
|
|
|
while (ifslastp->next && ifslastp->next->begoff < endoff)
|
|
|
|
ifslastp=ifslastp->next;
|
|
|
|
while (ifslastp->next != NULL) {
|
|
|
|
struct ifsregion *ifsp;
|
|
|
|
INTOFF;
|
|
|
|
ifsp = ifslastp->next->next;
|
|
|
|
ckfree(ifslastp->next);
|
|
|
|
ifslastp->next = ifsp;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
if (ifslastp->endoff > endoff)
|
|
|
|
ifslastp->endoff = endoff;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Expand arithmetic expression. Backup to start of expression,
|
|
|
|
* evaluate, place result in (backed up) result, adjust string position.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
expari(int flag)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2010-04-25 20:43:19 +00:00
|
|
|
char *p, *q, *start;
|
2008-04-28 07:26:34 +00:00
|
|
|
arith_t result;
|
1998-09-06 21:13:09 +00:00
|
|
|
int begoff;
|
2003-01-08 10:50:08 +00:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
|
1998-09-06 21:13:09 +00:00
|
|
|
int quoted;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
1999-05-08 10:22:15 +00:00
|
|
|
* This routine is slightly over-complicated for
|
1994-05-26 06:18:55 +00:00
|
|
|
* efficiency. First we make sure there is
|
|
|
|
* enough space for the result, which may be bigger
|
2010-09-05 21:12:48 +00:00
|
|
|
* than the expression. Next we
|
1994-05-26 06:18:55 +00:00
|
|
|
* scan backwards looking for the start of arithmetic. If the
|
|
|
|
* next previous character is a CTLESC character, then we
|
|
|
|
* have to rescan starting from the beginning since CTLESC
|
1995-05-30 00:07:29 +00:00
|
|
|
* characters have to be processed left to right.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
2008-04-28 07:26:34 +00:00
|
|
|
CHECKSTRSPACE(DIGITS(result) - 2, expdest);
|
1995-05-30 00:07:29 +00:00
|
|
|
USTPUTC('\0', expdest);
|
1994-05-26 06:18:55 +00:00
|
|
|
start = stackblock();
|
2001-09-19 20:16:38 +00:00
|
|
|
p = expdest - 2;
|
|
|
|
while (p >= start && *p != CTLARI)
|
1994-05-26 06:18:55 +00:00
|
|
|
--p;
|
2001-09-19 20:16:38 +00:00
|
|
|
if (p < start || *p != CTLARI)
|
1994-05-26 06:18:55 +00:00
|
|
|
error("missing CTLARI (shouldn't happen)");
|
2001-09-19 20:16:38 +00:00
|
|
|
if (p > start && *(p - 1) == CTLESC)
|
1994-05-26 06:18:55 +00:00
|
|
|
for (p = start; *p != CTLARI; p++)
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
1998-09-06 21:13:09 +00:00
|
|
|
|
|
|
|
if (p[1] == '"')
|
|
|
|
quoted=1;
|
|
|
|
else
|
|
|
|
quoted=0;
|
|
|
|
begoff = p - start;
|
|
|
|
removerecordregions(begoff);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (quotes)
|
1998-09-06 21:13:09 +00:00
|
|
|
rmescapes(p+2);
|
2010-04-25 20:43:19 +00:00
|
|
|
q = grabstackstr(expdest);
|
1998-09-06 21:13:09 +00:00
|
|
|
result = arith(p+2);
|
2010-04-25 20:43:19 +00:00
|
|
|
ungrabstackstr(q, expdest);
|
2008-04-28 07:26:34 +00:00
|
|
|
fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
|
1994-05-26 06:18:55 +00:00
|
|
|
while (*p++)
|
|
|
|
;
|
1998-09-06 21:13:09 +00:00
|
|
|
if (quoted == 0)
|
|
|
|
recordregion(begoff, p - 1 - start, 0);
|
1994-05-26 06:18:55 +00:00
|
|
|
result = expdest - p + 1;
|
|
|
|
STADJUST(-result, expdest);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2010-09-05 21:12:48 +00:00
|
|
|
* Perform command substitution.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
expbackq(union node *cmd, int quoted, int flag)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct backcmd in;
|
|
|
|
int i;
|
|
|
|
char buf[128];
|
|
|
|
char *p;
|
|
|
|
char *dest = expdest;
|
|
|
|
struct ifsregion saveifs, *savelastp;
|
|
|
|
struct nodelist *saveargbackq;
|
|
|
|
char lastc;
|
|
|
|
int startloc = dest - stackblock();
|
|
|
|
char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
|
2003-01-08 10:50:08 +00:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
|
2003-05-31 06:27:57 +00:00
|
|
|
int nnl;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
saveifs = ifsfirst;
|
|
|
|
savelastp = ifslastp;
|
|
|
|
saveargbackq = argbackq;
|
|
|
|
p = grabstackstr(dest);
|
|
|
|
evalbackcmd(cmd, &in);
|
|
|
|
ungrabstackstr(p, dest);
|
|
|
|
ifsfirst = saveifs;
|
|
|
|
ifslastp = savelastp;
|
|
|
|
argbackq = saveargbackq;
|
|
|
|
|
|
|
|
p = in.buf;
|
|
|
|
lastc = '\0';
|
2003-05-31 06:27:57 +00:00
|
|
|
nnl = 0;
|
|
|
|
/* Don't copy trailing newlines */
|
1994-05-26 06:18:55 +00:00
|
|
|
for (;;) {
|
|
|
|
if (--in.nleft < 0) {
|
|
|
|
if (in.fd < 0)
|
|
|
|
break;
|
|
|
|
while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
|
|
|
|
TRACE(("expbackq: read returns %d\n", i));
|
|
|
|
if (i <= 0)
|
|
|
|
break;
|
|
|
|
p = buf;
|
|
|
|
in.nleft = i - 1;
|
|
|
|
}
|
|
|
|
lastc = *p++;
|
|
|
|
if (lastc != '\0') {
|
2003-05-31 06:27:57 +00:00
|
|
|
if (lastc == '\n') {
|
|
|
|
nnl++;
|
|
|
|
} else {
|
2010-12-26 13:25:47 +00:00
|
|
|
CHECKSTRSPACE(nnl + 2, dest);
|
2003-05-31 06:27:57 +00:00
|
|
|
while (nnl > 0) {
|
|
|
|
nnl--;
|
2010-12-26 13:25:47 +00:00
|
|
|
USTPUTC('\n', dest);
|
2003-05-31 06:27:57 +00:00
|
|
|
}
|
2010-12-16 23:28:20 +00:00
|
|
|
if (quotes && syntax[(int)lastc] == CCTL)
|
2010-12-26 13:25:47 +00:00
|
|
|
USTPUTC(CTLESC, dest);
|
|
|
|
USTPUTC(lastc, dest);
|
2003-05-31 06:27:57 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if (in.fd >= 0)
|
|
|
|
close(in.fd);
|
|
|
|
if (in.buf)
|
|
|
|
ckfree(in.buf);
|
|
|
|
if (in.jp)
|
1999-04-21 11:52:39 +00:00
|
|
|
exitstatus = waitforjob(in.jp, (int *)NULL);
|
1994-05-26 06:18:55 +00:00
|
|
|
if (quoted == 0)
|
|
|
|
recordregion(startloc, dest - stackblock(), 0);
|
2010-10-13 13:22:11 +00:00
|
|
|
TRACE(("expbackq: size=%td: \"%.*s\"\n",
|
|
|
|
((dest - stackblock()) - startloc),
|
|
|
|
(int)((dest - stackblock()) - startloc),
|
1994-05-26 06:18:55 +00:00
|
|
|
stackblock() + startloc));
|
|
|
|
expdest = dest;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2002-02-02 06:50:57 +00:00
|
|
|
subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
|
2010-10-29 19:34:57 +00:00
|
|
|
int varflags, int quotes)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
|
|
|
char *startp;
|
|
|
|
char *loc = NULL;
|
1999-04-09 15:23:48 +00:00
|
|
|
char *q;
|
1996-09-01 10:22:36 +00:00
|
|
|
int c = 0;
|
|
|
|
struct nodelist *saveargbackq = argbackq;
|
1996-12-14 06:20:03 +00:00
|
|
|
int amount;
|
|
|
|
|
2010-04-03 22:04:44 +00:00
|
|
|
argstr(p, (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
|
2010-04-03 21:07:50 +00:00
|
|
|
subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ?
|
2010-04-03 22:04:44 +00:00
|
|
|
EXP_CASE : 0) | EXP_TILDE);
|
1996-09-01 10:22:36 +00:00
|
|
|
STACKSTRNUL(expdest);
|
|
|
|
argbackq = saveargbackq;
|
|
|
|
startp = stackblock() + startloc;
|
1996-12-14 06:20:03 +00:00
|
|
|
if (str == NULL)
|
|
|
|
str = stackblock() + strloc;
|
1996-09-01 10:22:36 +00:00
|
|
|
|
|
|
|
switch (subtype) {
|
|
|
|
case VSASSIGN:
|
|
|
|
setvar(str, startp, 0);
|
1996-12-14 06:20:03 +00:00
|
|
|
amount = startp - expdest;
|
|
|
|
STADJUST(amount, expdest);
|
1996-09-01 10:22:36 +00:00
|
|
|
varflags &= ~VSNUL;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case VSQUESTION:
|
|
|
|
if (*p != CTLENDVAR) {
|
2010-01-01 18:17:46 +00:00
|
|
|
outfmt(out2, "%s\n", startp);
|
1996-09-01 10:22:36 +00:00
|
|
|
error((char *)NULL);
|
|
|
|
}
|
2003-03-15 07:56:59 +00:00
|
|
|
error("%.*s: parameter %snot set", (int)(p - str - 1),
|
1996-09-01 10:22:36 +00:00
|
|
|
str, (varflags & VSNUL) ? "null or "
|
|
|
|
: nullstr);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMLEFT:
|
1997-04-28 03:28:43 +00:00
|
|
|
for (loc = startp; loc < str; loc++) {
|
1996-09-01 10:22:36 +00:00
|
|
|
c = *loc;
|
|
|
|
*loc = '\0';
|
2010-10-29 19:34:57 +00:00
|
|
|
if (patmatch(str, startp, quotes)) {
|
1996-09-01 10:22:36 +00:00
|
|
|
*loc = c;
|
|
|
|
goto recordleft;
|
|
|
|
}
|
|
|
|
*loc = c;
|
2010-10-29 19:34:57 +00:00
|
|
|
if (quotes && *loc == CTLESC)
|
1999-04-09 15:23:48 +00:00
|
|
|
loc++;
|
1996-09-01 10:22:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMLEFTMAX:
|
1999-04-09 15:23:48 +00:00
|
|
|
for (loc = str - 1; loc >= startp;) {
|
1996-09-01 10:22:36 +00:00
|
|
|
c = *loc;
|
|
|
|
*loc = '\0';
|
2010-10-29 19:34:57 +00:00
|
|
|
if (patmatch(str, startp, quotes)) {
|
1996-09-01 10:22:36 +00:00
|
|
|
*loc = c;
|
|
|
|
goto recordleft;
|
|
|
|
}
|
|
|
|
*loc = c;
|
1999-04-09 15:23:48 +00:00
|
|
|
loc--;
|
2010-10-29 19:34:57 +00:00
|
|
|
if (quotes && loc > startp && *(loc - 1) == CTLESC) {
|
1999-04-09 15:23:48 +00:00
|
|
|
for (q = startp; q < loc; q++)
|
|
|
|
if (*q == CTLESC)
|
|
|
|
q++;
|
|
|
|
if (q > loc)
|
|
|
|
loc--;
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMRIGHT:
|
1999-04-09 15:23:48 +00:00
|
|
|
for (loc = str - 1; loc >= startp;) {
|
2010-10-29 19:34:57 +00:00
|
|
|
if (patmatch(str, loc, quotes)) {
|
1996-12-14 06:20:03 +00:00
|
|
|
amount = loc - expdest;
|
|
|
|
STADJUST(amount, expdest);
|
1996-09-01 10:22:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
1999-04-09 15:23:48 +00:00
|
|
|
loc--;
|
2010-10-29 19:34:57 +00:00
|
|
|
if (quotes && loc > startp && *(loc - 1) == CTLESC) {
|
1999-04-09 15:23:48 +00:00
|
|
|
for (q = startp; q < loc; q++)
|
|
|
|
if (*q == CTLESC)
|
|
|
|
q++;
|
|
|
|
if (q > loc)
|
|
|
|
loc--;
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMRIGHTMAX:
|
|
|
|
for (loc = startp; loc < str - 1; loc++) {
|
2010-10-29 19:34:57 +00:00
|
|
|
if (patmatch(str, loc, quotes)) {
|
1996-12-14 06:20:03 +00:00
|
|
|
amount = loc - expdest;
|
|
|
|
STADJUST(amount, expdest);
|
1996-09-01 10:22:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-29 19:34:57 +00:00
|
|
|
if (quotes && *loc == CTLESC)
|
1999-04-09 15:23:48 +00:00
|
|
|
loc++;
|
1996-09-01 10:22:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
recordleft:
|
1996-12-14 06:20:03 +00:00
|
|
|
amount = ((str - 1) - (loc - startp)) - expdest;
|
|
|
|
STADJUST(amount, expdest);
|
1996-09-01 10:22:36 +00:00
|
|
|
while (loc != str - 1)
|
|
|
|
*startp++ = *loc++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Expand a variable, and return a pointer to the next character in the
|
|
|
|
* input string.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static char *
|
2002-02-02 06:50:57 +00:00
|
|
|
evalvar(char *p, int flag)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int subtype;
|
|
|
|
int varflags;
|
|
|
|
char *var;
|
|
|
|
char *val;
|
1999-04-13 04:13:09 +00:00
|
|
|
int patloc;
|
1994-05-26 06:18:55 +00:00
|
|
|
int c;
|
|
|
|
int set;
|
|
|
|
int special;
|
|
|
|
int startloc;
|
1996-09-01 10:22:36 +00:00
|
|
|
int varlen;
|
|
|
|
int easy;
|
2003-01-08 10:50:08 +00:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2006-11-07 22:46:13 +00:00
|
|
|
varflags = (unsigned char)*p++;
|
1994-05-26 06:18:55 +00:00
|
|
|
subtype = varflags & VSTYPE;
|
|
|
|
var = p;
|
|
|
|
special = 0;
|
|
|
|
if (! is_name(*p))
|
|
|
|
special = 1;
|
|
|
|
p = strchr(p, '=') + 1;
|
|
|
|
again: /* jump here after setting a variable with ${var=text} */
|
2008-05-15 19:55:27 +00:00
|
|
|
if (varflags & VSLINENO) {
|
|
|
|
set = 1;
|
|
|
|
special = 0;
|
|
|
|
val = var;
|
|
|
|
p[-1] = '\0'; /* temporarily overwrite '=' to have \0
|
|
|
|
terminated string */
|
|
|
|
} else if (special) {
|
1997-04-28 03:28:43 +00:00
|
|
|
set = varisset(var, varflags & VSNUL);
|
1994-05-26 06:18:55 +00:00
|
|
|
val = NULL;
|
|
|
|
} else {
|
2000-05-15 12:33:17 +00:00
|
|
|
val = bltinlookup(var, 1);
|
1996-09-01 10:22:36 +00:00
|
|
|
if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
|
1994-05-26 06:18:55 +00:00
|
|
|
val = NULL;
|
|
|
|
set = 0;
|
|
|
|
} else
|
|
|
|
set = 1;
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
varlen = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
startloc = expdest - stackblock();
|
2009-10-24 21:20:04 +00:00
|
|
|
if (!set && uflag && *var != '@' && *var != '*') {
|
2002-05-19 08:30:16 +00:00
|
|
|
switch (subtype) {
|
|
|
|
case VSNORMAL:
|
|
|
|
case VSTRIMLEFT:
|
|
|
|
case VSTRIMLEFTMAX:
|
|
|
|
case VSTRIMRIGHT:
|
|
|
|
case VSTRIMRIGHTMAX:
|
|
|
|
case VSLENGTH:
|
2003-03-15 07:56:59 +00:00
|
|
|
error("%.*s: parameter not set", (int)(p - var - 1),
|
|
|
|
var);
|
2002-05-19 08:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
if (set && subtype != VSPLUS) {
|
|
|
|
/* insert the value of the variable */
|
|
|
|
if (special) {
|
2006-11-07 22:46:13 +00:00
|
|
|
varvalue(var, varflags & VSQUOTE, subtype, flag);
|
1996-09-01 10:22:36 +00:00
|
|
|
if (subtype == VSLENGTH) {
|
1997-04-28 03:28:43 +00:00
|
|
|
varlen = expdest - stackblock() - startloc;
|
|
|
|
STADJUST(-varlen, expdest);
|
1996-09-01 10:22:36 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
} else {
|
1996-12-14 06:20:03 +00:00
|
|
|
char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
|
1996-09-01 10:22:36 +00:00
|
|
|
: BASESYNTAX;
|
|
|
|
|
|
|
|
if (subtype == VSLENGTH) {
|
|
|
|
for (;*val; val++)
|
|
|
|
varlen++;
|
|
|
|
}
|
|
|
|
else {
|
2010-12-11 22:13:29 +00:00
|
|
|
if (quotes)
|
|
|
|
STPUTS_QUOTES(val, syntax, expdest);
|
|
|
|
else
|
|
|
|
STPUTS(val, expdest);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-12-14 06:20:03 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if (subtype == VSPLUS)
|
|
|
|
set = ! set;
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1996-12-14 06:20:03 +00:00
|
|
|
easy = ((varflags & VSQUOTE) == 0 ||
|
1996-09-01 10:22:36 +00:00
|
|
|
(*var == '@' && shellparam.nparam != 1));
|
|
|
|
|
|
|
|
|
|
|
|
switch (subtype) {
|
|
|
|
case VSLENGTH:
|
|
|
|
expdest = cvtnum(varlen, expdest);
|
|
|
|
goto record;
|
|
|
|
|
|
|
|
case VSNORMAL:
|
|
|
|
if (!easy)
|
|
|
|
break;
|
|
|
|
record:
|
1996-12-14 06:20:03 +00:00
|
|
|
recordregion(startloc, expdest - stackblock(),
|
1996-09-01 10:22:36 +00:00
|
|
|
varflags & VSQUOTE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VSPLUS:
|
|
|
|
case VSMINUS:
|
|
|
|
if (!set) {
|
2010-10-29 13:42:18 +00:00
|
|
|
argstr(p, flag | (flag & EXP_FULL ? EXP_SPLIT_LIT : 0) |
|
|
|
|
(varflags & VSQUOTE ? EXP_LIT_QUOTED : 0));
|
1996-09-01 10:22:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (easy)
|
|
|
|
goto record;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VSTRIMLEFT:
|
|
|
|
case VSTRIMLEFTMAX:
|
|
|
|
case VSTRIMRIGHT:
|
|
|
|
case VSTRIMRIGHTMAX:
|
|
|
|
if (!set)
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Terminate the string and start recording the pattern
|
|
|
|
* right after it
|
|
|
|
*/
|
|
|
|
STPUTC('\0', expdest);
|
1999-04-13 04:13:09 +00:00
|
|
|
patloc = expdest - stackblock();
|
|
|
|
if (subevalvar(p, NULL, patloc, subtype,
|
2010-10-29 19:34:57 +00:00
|
|
|
startloc, varflags, quotes) == 0) {
|
1999-04-13 04:13:09 +00:00
|
|
|
int amount = (expdest - stackblock() - patloc) + 1;
|
1997-04-28 03:28:43 +00:00
|
|
|
STADJUST(-amount, expdest);
|
|
|
|
}
|
1998-09-06 21:13:09 +00:00
|
|
|
/* Remove any recorded regions beyond start of variable */
|
|
|
|
removerecordregions(startloc);
|
|
|
|
goto record;
|
1996-09-01 10:22:36 +00:00
|
|
|
|
|
|
|
case VSASSIGN:
|
|
|
|
case VSQUESTION:
|
|
|
|
if (!set) {
|
2010-10-29 19:34:57 +00:00
|
|
|
if (subevalvar(p, var, 0, subtype, startloc, varflags,
|
|
|
|
quotes)) {
|
1996-12-14 06:20:03 +00:00
|
|
|
varflags &= ~VSNUL;
|
2006-02-04 14:37:50 +00:00
|
|
|
/*
|
|
|
|
* Remove any recorded regions beyond
|
|
|
|
* start of variable
|
1998-09-06 21:13:09 +00:00
|
|
|
*/
|
|
|
|
removerecordregions(startloc);
|
1994-05-26 06:18:55 +00:00
|
|
|
goto again;
|
1996-12-14 06:20:03 +00:00
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
if (easy)
|
|
|
|
goto record;
|
|
|
|
break;
|
|
|
|
|
2006-11-05 18:36:05 +00:00
|
|
|
case VSERROR:
|
|
|
|
c = p - var - 1;
|
|
|
|
error("${%.*s%s}: Bad substitution", c, var,
|
|
|
|
(c > 0 && *p != CTLENDVAR) ? "..." : "");
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
default:
|
|
|
|
abort();
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2008-05-15 19:55:27 +00:00
|
|
|
p[-1] = '='; /* recover overwritten '=' */
|
1996-09-01 10:22:36 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if (subtype != VSNORMAL) { /* skip to end of alternative */
|
|
|
|
int nesting = 1;
|
|
|
|
for (;;) {
|
|
|
|
if ((c = *p++) == CTLESC)
|
|
|
|
p++;
|
|
|
|
else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
|
|
|
|
if (set)
|
|
|
|
argbackq = argbackq->next;
|
|
|
|
} else if (c == CTLVAR) {
|
|
|
|
if ((*p++ & VSTYPE) != VSNORMAL)
|
|
|
|
nesting++;
|
|
|
|
} else if (c == CTLENDVAR) {
|
|
|
|
if (--nesting == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test whether a specialized variable is set.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2002-02-02 06:50:57 +00:00
|
|
|
varisset(char *name, int nulok)
|
1997-04-28 03:28:43 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1997-04-28 03:28:43 +00:00
|
|
|
if (*name == '!')
|
2010-06-29 22:37:45 +00:00
|
|
|
return backgndpidset();
|
1997-04-28 03:28:43 +00:00
|
|
|
else if (*name == '@' || *name == '*') {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*shellparam.p == NULL)
|
|
|
|
return 0;
|
1997-04-28 03:28:43 +00:00
|
|
|
|
|
|
|
if (nulok) {
|
|
|
|
char **av;
|
|
|
|
|
|
|
|
for (av = shellparam.p; *av; av++)
|
|
|
|
if (**av != '\0')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1996-09-10 02:42:33 +00:00
|
|
|
} else if (is_digit(*name)) {
|
1997-04-28 03:28:43 +00:00
|
|
|
char *ap;
|
1996-12-14 06:20:03 +00:00
|
|
|
int num = atoi(name);
|
1997-04-28 03:28:43 +00:00
|
|
|
|
|
|
|
if (num > shellparam.nparam)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (num == 0)
|
|
|
|
ap = arg0;
|
|
|
|
else
|
|
|
|
ap = shellparam.p[num - 1];
|
|
|
|
|
|
|
|
if (nulok && (ap == NULL || *ap == '\0'))
|
|
|
|
return 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-12-11 22:13:29 +00:00
|
|
|
static void
|
|
|
|
strtodest(const char *p, int flag, int subtype, int quoted)
|
|
|
|
{
|
|
|
|
if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH)
|
|
|
|
STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
|
|
|
|
else
|
|
|
|
STPUTS(p, expdest);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the value of a specialized variable to the stack string.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2006-11-07 22:46:13 +00:00
|
|
|
varvalue(char *name, int quoted, int subtype, int flag)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int num;
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
char sep;
|
|
|
|
char **ap;
|
|
|
|
|
1996-09-10 02:42:33 +00:00
|
|
|
switch (*name) {
|
1994-05-26 06:18:55 +00:00
|
|
|
case '$':
|
|
|
|
num = rootpid;
|
|
|
|
goto numvar;
|
|
|
|
case '?':
|
1996-09-01 10:22:36 +00:00
|
|
|
num = oexitstatus;
|
1994-05-26 06:18:55 +00:00
|
|
|
goto numvar;
|
|
|
|
case '#':
|
|
|
|
num = shellparam.nparam;
|
|
|
|
goto numvar;
|
|
|
|
case '!':
|
2010-06-29 22:37:45 +00:00
|
|
|
num = backgndpidval();
|
1994-05-26 06:18:55 +00:00
|
|
|
numvar:
|
1996-09-01 10:22:36 +00:00
|
|
|
expdest = cvtnum(num, expdest);
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
for (i = 0 ; i < NOPTS ; i++) {
|
|
|
|
if (optlist[i].val)
|
|
|
|
STPUTC(optlist[i].letter, expdest);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '@':
|
2006-11-07 22:46:13 +00:00
|
|
|
if (flag & EXP_FULL && quoted) {
|
1998-09-06 21:13:09 +00:00
|
|
|
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
|
2010-12-11 22:13:29 +00:00
|
|
|
strtodest(p, flag, subtype, quoted);
|
1998-09-06 21:13:09 +00:00
|
|
|
if (*ap)
|
|
|
|
STPUTC('\0', expdest);
|
|
|
|
}
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2002-08-25 13:01:47 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-26 06:18:55 +00:00
|
|
|
case '*':
|
Various small code cleanups resulting from a code reviewing
and linting procedure:
1. Remove useless sub-expression:
- if (*start || (!ifsspc && start > string && (nulonly || 1))) {
+ if (*start || (!ifsspc && start > string)) {
The sub-expression "(nulonly || 1)" always evaluates to true and
according to CVS logs seems to be just a left-over from some
debugging and introduced by accident. Removing the sub-expression
doesn't change semantics and a code inspection showed that the
variable "nulonly" is also not necessary here in any way (and the
expression would require fixing instead of removing).
2. Remove dead code:
- if (backslash && c == '\\') {
- if (read(STDIN_FILENO, &c, 1) != 1) {
- status = 1;
- break;
- }
- STPUTC(c, p);
- } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
+ if (ap[1] != NULL && strchr(ifs, c) != NULL) {
Inspection of the control and data flow showed that variable
"backslash" is always false (0) when the "if"-expression is
evaluated, hence the whole block is effectively dead code.
Additionally, the skipping of characters after a backslash is already
performed correctly a few lines above, so this code is also not
needed at all. According to the CVS logs and the ASH 0.2 sources,
this code existed in this way already since its early days.
3. Cleanup Style:
- ! trap[signo][0] == '\0' &&
+ ! (trap[signo][0] == '\0') &&
The expression wants to ensure the trap is not assigned the empty
string. But the "!" operator has higher precedence than "==", so the
comparison should be put into parenthesis to form the intended way of
expression. Nevertheless the code was effectively not really broken
as both particular NUL comparisons are semantically equal, of course.
But the parenthesized version is a lot more intuitive.
4. Remove shadowing variable declaration:
- char *q;
The declaration of symbol "q" hides another identical declaration of
"q" in the same context. As the other "q" is already reused multiple
times and also can be reused again without negative side-effects,
just remove the shadowing declaration.
5. Just small cosmetics:
- if (ifsset() != 0)
+ if (ifsset())
The ifsset() macro is already coded by returning the boolean result
of a comparison operator, so no need to compare this boolean result
again against a numerical value. This also aligns the macros usage to
the remaining existing code.
Reviewed by: stefanf@
2005-09-06 19:30:00 +00:00
|
|
|
if (ifsset())
|
1998-09-06 21:13:09 +00:00
|
|
|
sep = ifsval()[0];
|
|
|
|
else
|
|
|
|
sep = ' ';
|
1994-05-26 06:18:55 +00:00
|
|
|
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
|
2010-12-11 22:13:29 +00:00
|
|
|
strtodest(p, flag, subtype, quoted);
|
1998-09-06 21:13:09 +00:00
|
|
|
if (*ap && sep)
|
1994-05-26 06:18:55 +00:00
|
|
|
STPUTC(sep, expdest);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
p = arg0;
|
2010-12-11 22:13:29 +00:00
|
|
|
strtodest(p, flag, subtype, quoted);
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
1996-09-10 02:42:33 +00:00
|
|
|
if (is_digit(*name)) {
|
|
|
|
num = atoi(name);
|
|
|
|
if (num > 0 && num <= shellparam.nparam) {
|
|
|
|
p = shellparam.p[num - 1];
|
2010-12-11 22:13:29 +00:00
|
|
|
strtodest(p, flag, subtype, quoted);
|
1996-09-10 02:42:33 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record the the fact that we have to scan this region of the
|
|
|
|
* string for IFS characters.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2009-06-25 17:10:51 +00:00
|
|
|
recordregion(int start, int end, int inquotes)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1997-04-28 03:28:43 +00:00
|
|
|
struct ifsregion *ifsp;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (ifslastp == NULL) {
|
|
|
|
ifsp = &ifsfirst;
|
|
|
|
} else {
|
2009-06-25 17:10:51 +00:00
|
|
|
if (ifslastp->endoff == start
|
|
|
|
&& ifslastp->inquotes == inquotes) {
|
|
|
|
/* extend previous area */
|
|
|
|
ifslastp->endoff = end;
|
|
|
|
return;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
|
|
|
|
ifslastp->next = ifsp;
|
|
|
|
}
|
|
|
|
ifslastp = ifsp;
|
|
|
|
ifslastp->next = NULL;
|
|
|
|
ifslastp->begoff = start;
|
|
|
|
ifslastp->endoff = end;
|
2009-06-25 17:10:51 +00:00
|
|
|
ifslastp->inquotes = inquotes;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Break the argument string into pieces based upon IFS and add the
|
|
|
|
* strings to the argument list. The regions of the string to be
|
|
|
|
* searched for IFS characters have been stored by recordregion.
|
2010-09-05 21:12:48 +00:00
|
|
|
* CTLESC characters are preserved but have little effect in this pass
|
|
|
|
* other than escaping CTL* characters. In particular, they do not escape
|
|
|
|
* IFS characters: that should be done with the ifsregion mechanism.
|
|
|
|
* CTLQUOTEMARK characters are used to preserve empty quoted strings.
|
|
|
|
* This pass treats them as a regular character, making the string non-empty.
|
|
|
|
* Later, they are removed along with the other CTL* characters.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
ifsbreakup(char *string, struct arglist *arglist)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct ifsregion *ifsp;
|
|
|
|
struct strlist *sp;
|
|
|
|
char *start;
|
1997-04-28 03:28:43 +00:00
|
|
|
char *p;
|
1994-05-26 06:18:55 +00:00
|
|
|
char *q;
|
2009-12-27 18:04:05 +00:00
|
|
|
const char *ifs;
|
2009-06-25 17:10:51 +00:00
|
|
|
const char *ifsspc;
|
|
|
|
int had_param_ch = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
start = string;
|
2009-06-25 17:10:51 +00:00
|
|
|
|
|
|
|
if (ifslastp == NULL) {
|
|
|
|
/* Return entire argument, IFS doesn't apply to any of it */
|
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = start;
|
|
|
|
*arglist->lastp = sp;
|
|
|
|
arglist->lastp = &sp->next;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifs = ifsset() ? ifsval() : " \t\n";
|
|
|
|
|
|
|
|
for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
|
|
|
|
p = string + ifsp->begoff;
|
|
|
|
while (p < string + ifsp->endoff) {
|
|
|
|
q = p;
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
if (ifsp->inquotes) {
|
|
|
|
/* Only NULs (should be from "$@") end args */
|
2009-06-25 17:14:06 +00:00
|
|
|
had_param_ch = 1;
|
2009-06-25 17:10:51 +00:00
|
|
|
if (*p != 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
p++;
|
2009-06-25 17:10:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ifsspc = NULL;
|
|
|
|
} else {
|
|
|
|
if (!strchr(ifs, *p)) {
|
2009-06-25 17:14:06 +00:00
|
|
|
had_param_ch = 1;
|
1998-09-06 21:13:09 +00:00
|
|
|
p++;
|
2009-06-25 17:10:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ifsspc = strchr(" \t\n", *p);
|
|
|
|
|
|
|
|
/* Ignore IFS whitespace at start */
|
|
|
|
if (q == start && ifsspc != NULL) {
|
1998-09-06 21:13:09 +00:00
|
|
|
p++;
|
2009-06-25 17:10:51 +00:00
|
|
|
start = p;
|
|
|
|
continue;
|
|
|
|
}
|
2009-06-25 17:14:06 +00:00
|
|
|
had_param_ch = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2009-06-25 17:10:51 +00:00
|
|
|
|
|
|
|
/* Save this argument... */
|
|
|
|
*q = '\0';
|
1994-05-26 06:18:55 +00:00
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = start;
|
|
|
|
*arglist->lastp = sp;
|
|
|
|
arglist->lastp = &sp->next;
|
2009-06-25 17:10:51 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
if (ifsspc != NULL) {
|
|
|
|
/* Ignore further trailing IFS whitespace */
|
|
|
|
for (; p < string + ifsp->endoff; p++) {
|
|
|
|
q = p;
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
if (strchr(ifs, *p) == NULL) {
|
|
|
|
p = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strchr(" \t\n", *p) == NULL) {
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start = p;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2009-06-25 17:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save anything left as an argument.
|
|
|
|
* Traditionally we have treated 'IFS=':'; set -- x$IFS' as
|
|
|
|
* generating 2 arguments, the second of which is empty.
|
|
|
|
* Some recent clarification of the Posix spec say that it
|
|
|
|
* should only generate one....
|
|
|
|
*/
|
|
|
|
if (had_param_ch || *start != 0) {
|
1994-05-26 06:18:55 +00:00
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = start;
|
|
|
|
*arglist->lastp = sp;
|
|
|
|
arglist->lastp = &sp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-13 04:01:01 +00:00
|
|
|
static char expdir[PATH_MAX];
|
2010-08-10 22:45:59 +00:00
|
|
|
#define expdir_end (expdir + sizeof(expdir))
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-09-05 21:12:48 +00:00
|
|
|
/*
|
|
|
|
* Perform pathname generation and remove control characters.
|
|
|
|
* At this point, the only control characters should be CTLESC and CTLQUOTEMARK.
|
|
|
|
* The results are stored in the list exparg.
|
|
|
|
*/
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
expandmeta(struct strlist *str, int flag __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char *p;
|
|
|
|
struct strlist **savelastp;
|
|
|
|
struct strlist *sp;
|
|
|
|
char c;
|
|
|
|
/* TODO - EXP_REDIR */
|
|
|
|
|
|
|
|
while (str) {
|
|
|
|
if (fflag)
|
|
|
|
goto nometa;
|
|
|
|
p = str->text;
|
|
|
|
for (;;) { /* fast check for meta chars */
|
|
|
|
if ((c = *p++) == '\0')
|
|
|
|
goto nometa;
|
2010-08-22 21:18:21 +00:00
|
|
|
if (c == '*' || c == '?' || c == '[')
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
savelastp = exparg.lastp;
|
|
|
|
INTOFF;
|
|
|
|
expmeta(expdir, str->text);
|
|
|
|
INTON;
|
|
|
|
if (exparg.lastp == savelastp) {
|
1995-05-30 00:07:29 +00:00
|
|
|
/*
|
|
|
|
* no matches
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
nometa:
|
|
|
|
*exparg.lastp = str;
|
|
|
|
rmescapes(str->text);
|
|
|
|
exparg.lastp = &str->next;
|
|
|
|
} else {
|
|
|
|
*exparg.lastp = NULL;
|
|
|
|
*savelastp = sp = expsort(*savelastp);
|
|
|
|
while (sp->next != NULL)
|
|
|
|
sp = sp->next;
|
|
|
|
exparg.lastp = &sp->next;
|
|
|
|
}
|
|
|
|
str = str->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do metacharacter (i.e. *, ?, [...]) expansion.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
expmeta(char *enddir, char *name)
|
|
|
|
{
|
1997-04-28 03:28:43 +00:00
|
|
|
char *p;
|
1994-05-26 06:18:55 +00:00
|
|
|
char *q;
|
|
|
|
char *start;
|
|
|
|
char *endname;
|
|
|
|
int metaflag;
|
|
|
|
struct stat statb;
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
|
|
|
int atend;
|
|
|
|
int matchdot;
|
2010-05-11 23:19:28 +00:00
|
|
|
int esc;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
metaflag = 0;
|
|
|
|
start = name;
|
2010-05-11 23:19:28 +00:00
|
|
|
for (p = name; esc = 0, *p; p += esc + 1) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*p == '*' || *p == '?')
|
|
|
|
metaflag = 1;
|
|
|
|
else if (*p == '[') {
|
|
|
|
q = p + 1;
|
1997-06-06 23:04:33 +00:00
|
|
|
if (*q == '!' || *q == '^')
|
1994-05-26 06:18:55 +00:00
|
|
|
q++;
|
|
|
|
for (;;) {
|
1998-09-06 21:13:09 +00:00
|
|
|
while (*q == CTLQUOTEMARK)
|
|
|
|
q++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*q == CTLESC)
|
|
|
|
q++;
|
|
|
|
if (*q == '/' || *q == '\0')
|
|
|
|
break;
|
|
|
|
if (*++q == ']') {
|
|
|
|
metaflag = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (*p == '\0')
|
|
|
|
break;
|
1998-09-06 21:13:09 +00:00
|
|
|
else if (*p == CTLQUOTEMARK)
|
|
|
|
continue;
|
2010-05-11 23:19:28 +00:00
|
|
|
else {
|
|
|
|
if (*p == CTLESC)
|
|
|
|
esc++;
|
|
|
|
if (p[esc] == '/') {
|
|
|
|
if (metaflag)
|
|
|
|
break;
|
|
|
|
start = p + esc + 1;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (metaflag == 0) { /* we've reached the end of the file name */
|
|
|
|
if (enddir != expdir)
|
|
|
|
metaflag++;
|
|
|
|
for (p = name ; ; p++) {
|
1998-09-06 21:13:09 +00:00
|
|
|
if (*p == CTLQUOTEMARK)
|
|
|
|
continue;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*enddir++ = *p;
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
2010-08-10 22:45:59 +00:00
|
|
|
if (enddir == expdir_end)
|
|
|
|
return;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2005-07-07 18:10:33 +00:00
|
|
|
if (metaflag == 0 || lstat(expdir, &statb) >= 0)
|
1994-05-26 06:18:55 +00:00
|
|
|
addfname(expdir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
endname = p;
|
|
|
|
if (start != name) {
|
|
|
|
p = name;
|
|
|
|
while (p < start) {
|
1998-09-06 21:13:09 +00:00
|
|
|
while (*p == CTLQUOTEMARK)
|
|
|
|
p++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*enddir++ = *p++;
|
2010-08-10 22:45:59 +00:00
|
|
|
if (enddir == expdir_end)
|
|
|
|
return;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (enddir == expdir) {
|
|
|
|
p = ".";
|
|
|
|
} else if (enddir == expdir + 1 && *expdir == '/') {
|
|
|
|
p = "/";
|
|
|
|
} else {
|
|
|
|
p = expdir;
|
|
|
|
enddir[-1] = '\0';
|
|
|
|
}
|
|
|
|
if ((dirp = opendir(p)) == NULL)
|
|
|
|
return;
|
|
|
|
if (enddir != expdir)
|
|
|
|
enddir[-1] = '/';
|
|
|
|
if (*endname == 0) {
|
|
|
|
atend = 1;
|
|
|
|
} else {
|
|
|
|
atend = 0;
|
2010-05-11 23:19:28 +00:00
|
|
|
*endname = '\0';
|
|
|
|
endname += esc + 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
matchdot = 0;
|
1998-09-06 21:13:09 +00:00
|
|
|
p = start;
|
|
|
|
while (*p == CTLQUOTEMARK)
|
|
|
|
p++;
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
if (*p == '.')
|
1994-05-26 06:18:55 +00:00
|
|
|
matchdot++;
|
|
|
|
while (! int_pending() && (dp = readdir(dirp)) != NULL) {
|
|
|
|
if (dp->d_name[0] == '.' && ! matchdot)
|
|
|
|
continue;
|
1999-04-09 15:23:48 +00:00
|
|
|
if (patmatch(start, dp->d_name, 0)) {
|
2010-08-10 22:45:59 +00:00
|
|
|
if (enddir + dp->d_namlen + 1 > expdir_end)
|
|
|
|
continue;
|
|
|
|
memcpy(enddir, dp->d_name, dp->d_namlen + 1);
|
|
|
|
if (atend)
|
1994-05-26 06:18:55 +00:00
|
|
|
addfname(expdir);
|
2010-08-10 22:45:59 +00:00
|
|
|
else {
|
|
|
|
if (enddir + dp->d_namlen + 2 > expdir_end)
|
1996-09-01 10:22:36 +00:00
|
|
|
continue;
|
2010-08-10 22:45:59 +00:00
|
|
|
enddir[dp->d_namlen] = '/';
|
|
|
|
enddir[dp->d_namlen + 1] = '\0';
|
|
|
|
expmeta(enddir + dp->d_namlen + 1, endname);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
if (! atend)
|
2010-05-11 23:19:28 +00:00
|
|
|
endname[-esc - 1] = esc ? CTLESC : '/';
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a file name to the list.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
addfname(char *name)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
char *p;
|
|
|
|
struct strlist *sp;
|
|
|
|
|
|
|
|
p = stalloc(strlen(name) + 1);
|
|
|
|
scopy(name, p);
|
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = p;
|
|
|
|
*exparg.lastp = sp;
|
|
|
|
exparg.lastp = &sp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sort the results of file name expansion. It calculates the number of
|
|
|
|
* strings to sort and then calls msort (short for merge sort) to do the
|
|
|
|
* work.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static struct strlist *
|
2002-02-02 06:50:57 +00:00
|
|
|
expsort(struct strlist *str)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int len;
|
|
|
|
struct strlist *sp;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
for (sp = str ; sp ; sp = sp->next)
|
|
|
|
len++;
|
|
|
|
return msort(str, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static struct strlist *
|
2002-02-02 06:50:57 +00:00
|
|
|
msort(struct strlist *list, int len)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
|
|
|
struct strlist *p, *q = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
struct strlist **lpp;
|
|
|
|
int half;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (len <= 1)
|
|
|
|
return list;
|
1995-05-30 00:07:29 +00:00
|
|
|
half = len >> 1;
|
1994-05-26 06:18:55 +00:00
|
|
|
p = list;
|
|
|
|
for (n = half ; --n >= 0 ; ) {
|
|
|
|
q = p;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
q->next = NULL; /* terminate first half of list */
|
|
|
|
q = msort(list, half); /* sort first half of list */
|
|
|
|
p = msort(p, len - half); /* sort second half */
|
|
|
|
lpp = &list;
|
|
|
|
for (;;) {
|
|
|
|
if (strcmp(p->text, q->text) < 0) {
|
|
|
|
*lpp = p;
|
|
|
|
lpp = &p->next;
|
|
|
|
if ((p = *lpp) == NULL) {
|
|
|
|
*lpp = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*lpp = q;
|
|
|
|
lpp = &q->next;
|
|
|
|
if ((q = *lpp) == NULL) {
|
|
|
|
*lpp = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if the pattern matches the string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-12-24 18:41:14 +00:00
|
|
|
patmatch(const char *pattern, const char *string, int squoted)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
2009-12-24 18:41:14 +00:00
|
|
|
const char *p, *q;
|
1997-04-28 03:28:43 +00:00
|
|
|
char c;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
p = pattern;
|
|
|
|
q = string;
|
|
|
|
for (;;) {
|
|
|
|
switch (c = *p++) {
|
|
|
|
case '\0':
|
|
|
|
goto breakloop;
|
|
|
|
case CTLESC:
|
1999-04-09 15:23:48 +00:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*q++ != *p++)
|
|
|
|
return 0;
|
|
|
|
break;
|
1998-09-06 21:13:09 +00:00
|
|
|
case CTLQUOTEMARK:
|
|
|
|
continue;
|
1994-05-26 06:18:55 +00:00
|
|
|
case '?':
|
1999-04-09 15:23:48 +00:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*q++ == '\0')
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
c = *p;
|
1998-09-06 21:13:09 +00:00
|
|
|
while (c == CTLQUOTEMARK || c == '*')
|
|
|
|
c = *++p;
|
|
|
|
if (c != CTLESC && c != CTLQUOTEMARK &&
|
|
|
|
c != '?' && c != '*' && c != '[') {
|
1994-05-26 06:18:55 +00:00
|
|
|
while (*q != c) {
|
1999-04-09 15:23:48 +00:00
|
|
|
if (squoted && *q == CTLESC &&
|
|
|
|
q[1] == c)
|
|
|
|
break;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*q == '\0')
|
|
|
|
return 0;
|
1999-04-09 15:23:48 +00:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1994-05-26 06:18:55 +00:00
|
|
|
q++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do {
|
2010-08-22 21:18:21 +00:00
|
|
|
if (patmatch(p, q, squoted))
|
1994-05-26 06:18:55 +00:00
|
|
|
return 1;
|
1999-04-09 15:23:48 +00:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1994-05-26 06:18:55 +00:00
|
|
|
} while (*q++ != '\0');
|
|
|
|
return 0;
|
|
|
|
case '[': {
|
2009-12-24 18:41:14 +00:00
|
|
|
const char *endp;
|
1994-05-26 06:18:55 +00:00
|
|
|
int invert, found;
|
|
|
|
char chr;
|
|
|
|
|
|
|
|
endp = p;
|
1997-06-06 23:04:33 +00:00
|
|
|
if (*endp == '!' || *endp == '^')
|
1994-05-26 06:18:55 +00:00
|
|
|
endp++;
|
|
|
|
for (;;) {
|
1998-09-06 21:13:09 +00:00
|
|
|
while (*endp == CTLQUOTEMARK)
|
|
|
|
endp++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*endp == '\0')
|
|
|
|
goto dft; /* no matching ] */
|
|
|
|
if (*endp == CTLESC)
|
|
|
|
endp++;
|
|
|
|
if (*++endp == ']')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
invert = 0;
|
1997-06-06 23:04:33 +00:00
|
|
|
if (*p == '!' || *p == '^') {
|
1994-05-26 06:18:55 +00:00
|
|
|
invert++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
found = 0;
|
|
|
|
chr = *q++;
|
1999-04-09 15:23:48 +00:00
|
|
|
if (squoted && chr == CTLESC)
|
|
|
|
chr = *q++;
|
1996-09-01 10:22:36 +00:00
|
|
|
if (chr == '\0')
|
|
|
|
return 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
c = *p++;
|
|
|
|
do {
|
1998-09-06 21:13:09 +00:00
|
|
|
if (c == CTLQUOTEMARK)
|
|
|
|
continue;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (c == CTLESC)
|
|
|
|
c = *p++;
|
|
|
|
if (*p == '-' && p[1] != ']') {
|
|
|
|
p++;
|
1998-09-06 21:13:09 +00:00
|
|
|
while (*p == CTLQUOTEMARK)
|
|
|
|
p++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
1996-08-12 19:31:11 +00:00
|
|
|
if ( collate_range_cmp(chr, c) >= 0
|
|
|
|
&& collate_range_cmp(chr, *p) <= 0
|
1996-08-11 22:51:00 +00:00
|
|
|
)
|
1994-05-26 06:18:55 +00:00
|
|
|
found = 1;
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
if (chr == c)
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
} while ((c = *p++) != ']');
|
|
|
|
if (found == invert)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dft: default:
|
1999-04-09 15:23:48 +00:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*q++ != c)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
breakloop:
|
|
|
|
if (*q != '\0')
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2010-09-05 21:12:48 +00:00
|
|
|
* Remove any CTLESC and CTLQUOTEMARK characters from a string.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
rmescapes(char *str)
|
1998-09-06 21:13:09 +00:00
|
|
|
{
|
1997-04-28 03:28:43 +00:00
|
|
|
char *p, *q;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
p = str;
|
2010-10-29 13:42:18 +00:00
|
|
|
while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*p++ == '\0')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
q = p;
|
|
|
|
while (*p) {
|
2010-10-29 13:42:18 +00:00
|
|
|
if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
|
1998-09-06 21:13:09 +00:00
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if a pattern matches in a case statement.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2009-12-24 18:41:14 +00:00
|
|
|
casematch(union node *pattern, const char *val)
|
2002-02-02 06:50:57 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
struct stackmark smark;
|
|
|
|
int result;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
setstackmark(&smark);
|
|
|
|
argbackq = pattern->narg.backquote;
|
|
|
|
STARTSTACKSTR(expdest);
|
|
|
|
ifslastp = NULL;
|
|
|
|
argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
|
|
|
|
STPUTC('\0', expdest);
|
|
|
|
p = grabstackstr(expdest);
|
1999-04-09 15:23:48 +00:00
|
|
|
result = patmatch(p, val, 0);
|
1994-05-26 06:18:55 +00:00
|
|
|
popstackmark(&smark);
|
|
|
|
return result;
|
|
|
|
}
|
1996-09-01 10:22:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Our own itoa().
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static char *
|
2002-02-02 06:50:57 +00:00
|
|
|
cvtnum(int num, char *buf)
|
|
|
|
{
|
1996-09-01 10:22:36 +00:00
|
|
|
char temp[32];
|
|
|
|
int neg = num < 0;
|
|
|
|
char *p = temp + 31;
|
|
|
|
|
|
|
|
temp[31] = '\0';
|
|
|
|
|
|
|
|
do {
|
|
|
|
*--p = num % 10 + '0';
|
|
|
|
} while ((num /= 10) != 0);
|
|
|
|
|
|
|
|
if (neg)
|
|
|
|
*--p = '-';
|
|
|
|
|
2010-11-23 22:17:39 +00:00
|
|
|
STPUTS(p, buf);
|
1996-09-01 10:22:36 +00:00
|
|
|
return buf;
|
|
|
|
}
|
2002-12-26 14:28:54 +00:00
|
|
|
|
2010-12-28 21:27:08 +00:00
|
|
|
/*
|
|
|
|
* Check statically if expanding a string may have side effects.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
expandhassideeffects(const char *p)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int arinest;
|
|
|
|
|
|
|
|
arinest = 0;
|
|
|
|
while ((c = *p++) != '\0') {
|
|
|
|
switch (c) {
|
|
|
|
case CTLESC:
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
case CTLVAR:
|
|
|
|
c = *p++;
|
|
|
|
/* Expanding $! sets the job to remembered. */
|
|
|
|
if (*p == '!')
|
|
|
|
return 1;
|
|
|
|
if ((c & VSTYPE) == VSASSIGN)
|
|
|
|
return 1;
|
|
|
|
/*
|
|
|
|
* If we are in arithmetic, the parameter may contain
|
|
|
|
* '=' which may cause side effects. Exceptions are
|
|
|
|
* the length of a parameter and $$, $# and $? which
|
|
|
|
* are always numeric.
|
|
|
|
*/
|
|
|
|
if ((c & VSTYPE) == VSLENGTH) {
|
|
|
|
while (*p != '=')
|
|
|
|
p++;
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((*p == '$' || *p == '#' || *p == '?') &&
|
|
|
|
p[1] == '=') {
|
|
|
|
p += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (arinest > 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case CTLBACKQ:
|
|
|
|
case CTLBACKQ | CTLQUOTE:
|
|
|
|
if (arinest > 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case CTLARI:
|
|
|
|
arinest++;
|
|
|
|
break;
|
|
|
|
case CTLENDARI:
|
|
|
|
arinest--;
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
if (*p == '=') {
|
|
|
|
/* Allow '==' operator. */
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arinest > 0)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case '!': case '<': case '>':
|
|
|
|
/* Allow '!=', '<=', '>=' operators. */
|
|
|
|
if (*p == '=')
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-26 14:28:54 +00:00
|
|
|
/*
|
|
|
|
* Do most of the work for wordexp(3).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
wordexpcmd(int argc, char **argv)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
out1fmt("%08x", argc - 1);
|
|
|
|
for (i = 1, len = 0; i < argc; i++)
|
|
|
|
len += strlen(argv[i]);
|
|
|
|
out1fmt("%08x", (int)len);
|
2010-11-20 14:14:52 +00:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
outbin(argv[i], strlen(argv[i]) + 1, out1);
|
2002-12-26 14:28:54 +00:00
|
|
|
return (0);
|
|
|
|
}
|