2007-03-25 10:29:26 +04:00
|
|
|
/* $NetBSD: expand.c,v 1.78 2007/03/25 06:29:26 apb Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-11 21:09:42 +04:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03: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.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:01:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1995-05-15 06:47:38 +04:00
|
|
|
static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
2007-03-25 10:29:26 +04:00
|
|
|
__RCSID("$NetBSD: expand.c,v 1.78 2007/03/25 06:29:26 apb Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdlib.h>
|
1999-01-25 17:20:56 +03:00
|
|
|
#include <stdio.h>
|
1995-05-12 01:28:33 +04:00
|
|
|
|
1993-03-21 12:45:37 +03: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"
|
1995-05-12 01:28:33 +04:00
|
|
|
#include "show.h"
|
1993-03-21 12:45:37 +03: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 */
|
2004-06-26 18:09:58 +04:00
|
|
|
int inquotes; /* search for nul bytes only */
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
char *expdest; /* output of current string */
|
|
|
|
struct nodelist *argbackq; /* list of back quote expressions */
|
|
|
|
struct ifsregion ifsfirst; /* first struct in list of ifs regions */
|
|
|
|
struct ifsregion *ifslastp; /* last struct in list */
|
|
|
|
struct arglist exparg; /* holds expanded arg list */
|
|
|
|
|
2002-11-25 01:35:38 +03:00
|
|
|
STATIC void argstr(char *, int);
|
|
|
|
STATIC char *exptilde(char *, int);
|
|
|
|
STATIC void expbackq(union node *, int, int);
|
|
|
|
STATIC int subevalvar(char *, char *, int, int, int, int);
|
|
|
|
STATIC char *evalvar(char *, int);
|
|
|
|
STATIC int varisset(char *, int);
|
2003-09-22 16:17:24 +04:00
|
|
|
STATIC void varvalue(char *, int, int, int);
|
2002-11-25 01:35:38 +03:00
|
|
|
STATIC void recordregion(int, int, int);
|
|
|
|
STATIC void removerecordregions(int);
|
|
|
|
STATIC void ifsbreakup(char *, struct arglist *);
|
|
|
|
STATIC void ifsfree(void);
|
|
|
|
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 int pmatch(char *, char *, int);
|
|
|
|
STATIC char *cvtnum(int, char *);
|
2001-02-04 22:52:06 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Expand shell variables and backquotes inside a here document.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
expandhere(union node *arg, int fd)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
herefd = fd;
|
|
|
|
expandarg(arg, (struct arglist *)NULL, 0);
|
|
|
|
xwrite(fd, stackblock(), expdest - stackblock());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform variable substitution and command substitution on an argument,
|
1994-05-11 21:09:42 +04:00
|
|
|
* placing the resulting list of arguments in arglist. If EXP_FULL is true,
|
1993-03-21 12:45:37 +03:00
|
|
|
* perform splitting and file name expansion. When arglist is NULL, perform
|
|
|
|
* here document expansion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
expandarg(union node *arg, struct arglist *arglist, int flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct strlist *sp;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
argbackq = arg->narg.backquote;
|
|
|
|
STARTSTACKSTR(expdest);
|
|
|
|
ifsfirst.next = NULL;
|
|
|
|
ifslastp = NULL;
|
1999-01-25 17:20:56 +03:00
|
|
|
argstr(arg->narg.text, flag);
|
1994-05-11 21:09:42 +04:00
|
|
|
if (arglist == NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
return; /* here document expanded */
|
1994-05-11 21:09:42 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
STPUTC('\0', expdest);
|
|
|
|
p = grabstackstr(expdest);
|
|
|
|
exparg.lastp = &exparg.list;
|
1994-05-11 21:09:42 +04:00
|
|
|
/*
|
|
|
|
* TODO - EXP_REDIR
|
|
|
|
*/
|
|
|
|
if (flag & EXP_FULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
ifsbreakup(p, &exparg);
|
|
|
|
*exparg.lastp = NULL;
|
|
|
|
exparg.lastp = &exparg.list;
|
1994-05-11 21:09:42 +04:00
|
|
|
expandmeta(exparg.list, flag);
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
1994-05-11 21:09:42 +04:00
|
|
|
if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
|
|
|
|
rmescapes(p);
|
1993-03-21 12:45:37 +03:00
|
|
|
sp = (struct strlist *)stalloc(sizeof (struct strlist));
|
|
|
|
sp->text = p;
|
|
|
|
*exparg.lastp = sp;
|
|
|
|
exparg.lastp = &sp->next;
|
|
|
|
}
|
1998-02-05 11:32:00 +03:00
|
|
|
ifsfree();
|
1993-03-21 12:45:37 +03:00
|
|
|
*exparg.lastp = NULL;
|
|
|
|
if (exparg.list) {
|
|
|
|
*arglist->lastp = exparg.list;
|
|
|
|
arglist->lastp = exparg.lastp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-06-27 02:09:49 +04:00
|
|
|
* Perform variable and command substitution.
|
|
|
|
* If EXP_FULL is set, output CTLESC characters to allow for further processing.
|
|
|
|
* Otherwise treat $@ like $* since no splitting will be performed.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
1999-01-25 17:20:56 +03:00
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
argstr(char *p, int flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
char c;
|
1994-05-11 21:09:42 +04:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */
|
|
|
|
int firsteq = 1;
|
2005-06-01 19:41:19 +04:00
|
|
|
const char *ifs = NULL;
|
2004-06-27 02:09:49 +04:00
|
|
|
int ifs_split = EXP_IFS_SPLIT;
|
|
|
|
|
|
|
|
if (flag & EXP_IFS_SPLIT)
|
|
|
|
ifs = ifsset() ? ifsval() : " \t\n";
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
|
|
|
|
p = exptilde(p, flag);
|
1993-03-21 12:45:37 +03:00
|
|
|
for (;;) {
|
|
|
|
switch (c = *p++) {
|
|
|
|
case '\0':
|
2004-06-26 18:09:58 +04:00
|
|
|
case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */
|
|
|
|
return;
|
1999-01-25 17:20:56 +03:00
|
|
|
case CTLQUOTEMARK:
|
|
|
|
/* "$@" syntax adherence hack */
|
|
|
|
if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
|
|
|
|
break;
|
|
|
|
if ((flag & EXP_FULL) != 0)
|
|
|
|
STPUTC(c, expdest);
|
2004-06-27 02:09:49 +04:00
|
|
|
ifs_split = 0;
|
|
|
|
break;
|
|
|
|
case CTLQUOTEEND:
|
|
|
|
ifs_split = EXP_IFS_SPLIT;
|
1999-01-25 17:20:56 +03:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case CTLESC:
|
1994-05-11 21:09:42 +04:00
|
|
|
if (quotes)
|
1993-03-21 12:45:37 +03:00
|
|
|
STPUTC(c, expdest);
|
|
|
|
c = *p++;
|
|
|
|
STPUTC(c, expdest);
|
|
|
|
break;
|
|
|
|
case CTLVAR:
|
2004-06-27 02:09:49 +04:00
|
|
|
p = evalvar(p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case CTLBACKQ:
|
|
|
|
case CTLBACKQ|CTLQUOTE:
|
1994-05-11 21:09:42 +04:00
|
|
|
expbackq(argbackq->n, c & CTLQUOTE, flag);
|
1993-03-21 12:45:37 +03:00
|
|
|
argbackq = argbackq->next;
|
|
|
|
break;
|
1994-05-11 21:09:42 +04:00
|
|
|
case CTLENDARI:
|
|
|
|
expari(flag);
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
case '=':
|
|
|
|
/*
|
|
|
|
* sort of a hack - expand tildes in variable
|
|
|
|
* assignments (after the first '=' and after ':'s).
|
|
|
|
*/
|
|
|
|
STPUTC(c, expdest);
|
|
|
|
if (flag & EXP_VARTILDE && *p == '~') {
|
|
|
|
if (c == '=') {
|
|
|
|
if (firsteq)
|
|
|
|
firsteq = 0;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = exptilde(p, flag);
|
|
|
|
}
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
|
|
|
STPUTC(c, expdest);
|
2004-06-27 02:09:49 +04:00
|
|
|
if (flag & EXP_IFS_SPLIT & ifs_split && strchr(ifs, c) != NULL) {
|
|
|
|
/* We need to get the output split here... */
|
|
|
|
recordregion(expdest - stackblock() - 1,
|
|
|
|
expdest - stackblock(), 0);
|
|
|
|
}
|
2004-06-26 18:09:58 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
STATIC char *
|
2002-11-25 01:35:38 +03:00
|
|
|
exptilde(char *p, int flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
char c, *startp = p;
|
|
|
|
struct passwd *pw;
|
1998-07-26 23:44:12 +04:00
|
|
|
const char *home;
|
1994-05-11 21:09:42 +04:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE);
|
|
|
|
|
1995-05-12 01:28:33 +04:00
|
|
|
while ((c = *p) != '\0') {
|
1994-05-11 21:09:42 +04:00
|
|
|
switch(c) {
|
|
|
|
case CTLESC:
|
|
|
|
return (startp);
|
1999-01-25 17:20:56 +03:00
|
|
|
case CTLQUOTEMARK:
|
|
|
|
return (startp);
|
1994-05-11 21:09:42 +04:00
|
|
|
case ':':
|
|
|
|
if (flag & EXP_VARTILDE)
|
|
|
|
goto done;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
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;
|
1995-05-12 01:28:33 +04:00
|
|
|
while ((c = *home++) != '\0') {
|
1998-03-29 08:41:43 +04:00
|
|
|
if (quotes && SQSYNTAX[(int)c] == CCTL)
|
1994-05-11 21:09:42 +04:00
|
|
|
STPUTC(CTLESC, expdest);
|
|
|
|
STPUTC(c, expdest);
|
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
lose:
|
|
|
|
*p = c;
|
|
|
|
return (startp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-25 17:20:56 +03:00
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
removerecordregions(int endoff)
|
1999-01-25 17:20:56 +03: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11 21:09:42 +04:00
|
|
|
/*
|
|
|
|
* Expand arithmetic expression. Backup to start of expression,
|
|
|
|
* evaluate, place result in (backed up) result, adjust string position.
|
|
|
|
*/
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
expari(int flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
char *p, *start;
|
2007-03-25 10:29:26 +04:00
|
|
|
intmax_t result;
|
|
|
|
int adjustment;
|
1999-01-25 17:20:56 +03:00
|
|
|
int begoff;
|
1994-05-11 21:09:42 +04:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE);
|
1999-01-25 17:20:56 +03:00
|
|
|
int quoted;
|
1994-05-11 21:09:42 +04:00
|
|
|
|
1999-01-25 17:20:56 +03:00
|
|
|
/* ifsfree(); */
|
1997-01-24 20:26:28 +03:00
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
/*
|
1997-11-30 23:57:33 +03:00
|
|
|
* This routine is slightly over-complicated for
|
1994-05-11 21:09:42 +04:00
|
|
|
* efficiency. First we make sure there is
|
|
|
|
* enough space for the result, which may be bigger
|
|
|
|
* than the expression if we add exponentation. Next we
|
|
|
|
* 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
|
1996-10-16 18:38:58 +04:00
|
|
|
* characters have to be processed left to right.
|
1994-05-11 21:09:42 +04:00
|
|
|
*/
|
2007-03-25 10:29:26 +04:00
|
|
|
/* SPACE_NEEDED is enough for all digits, plus possible "-", plus 2 (why?) */
|
|
|
|
#define SPACE_NEEDED ((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 1 + 2)
|
|
|
|
CHECKSTRSPACE(SPACE_NEEDED - 2, expdest);
|
1996-10-16 18:38:58 +04:00
|
|
|
USTPUTC('\0', expdest);
|
1994-05-11 21:09:42 +04:00
|
|
|
start = stackblock();
|
1999-02-06 20:23:09 +03:00
|
|
|
p = expdest - 1;
|
1994-05-11 21:09:42 +04:00
|
|
|
while (*p != CTLARI && p >= start)
|
|
|
|
--p;
|
|
|
|
if (*p != CTLARI)
|
|
|
|
error("missing CTLARI (shouldn't happen)");
|
|
|
|
if (p > start && *(p-1) == CTLESC)
|
|
|
|
for (p = start; *p != CTLARI; p++)
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
1999-01-25 17:20:56 +03:00
|
|
|
|
|
|
|
if (p[1] == '"')
|
|
|
|
quoted=1;
|
|
|
|
else
|
|
|
|
quoted=0;
|
|
|
|
begoff = p - start;
|
|
|
|
removerecordregions(begoff);
|
1994-05-11 21:09:42 +04:00
|
|
|
if (quotes)
|
1999-01-25 17:20:56 +03:00
|
|
|
rmescapes(p+2);
|
|
|
|
result = arith(p+2);
|
2007-03-25 10:29:26 +04:00
|
|
|
fmtstr(p, SPACE_NEEDED, "%"PRIdMAX, result);
|
1997-01-24 20:26:28 +03:00
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
while (*p++)
|
|
|
|
;
|
1997-01-24 20:26:28 +03:00
|
|
|
|
1999-01-25 17:20:56 +03:00
|
|
|
if (quoted == 0)
|
|
|
|
recordregion(begoff, p - 1 - start, 0);
|
2007-03-25 10:29:26 +04:00
|
|
|
adjustment = expdest - p + 1;
|
|
|
|
STADJUST(-adjustment, expdest);
|
1994-05-11 21:09:42 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand stuff in backwards quotes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
expbackq(union node *cmd, int quoted, int flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03: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;
|
|
|
|
int saveherefd;
|
1994-05-11 21:09:42 +04:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
INTOFF;
|
|
|
|
saveifs = ifsfirst;
|
|
|
|
savelastp = ifslastp;
|
|
|
|
saveargbackq = argbackq;
|
1996-10-16 18:38:58 +04:00
|
|
|
saveherefd = herefd;
|
1993-03-21 12:45:37 +03:00
|
|
|
herefd = -1;
|
|
|
|
p = grabstackstr(dest);
|
2002-09-28 05:25:01 +04:00
|
|
|
evalbackcmd(cmd, &in);
|
1993-03-21 12:45:37 +03:00
|
|
|
ungrabstackstr(p, dest);
|
|
|
|
ifsfirst = saveifs;
|
|
|
|
ifslastp = savelastp;
|
|
|
|
argbackq = saveargbackq;
|
|
|
|
herefd = saveherefd;
|
|
|
|
|
|
|
|
p = in.buf;
|
|
|
|
lastc = '\0';
|
|
|
|
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') {
|
1998-03-29 08:41:43 +04:00
|
|
|
if (quotes && syntax[(int)lastc] == CCTL)
|
1993-03-21 12:45:37 +03:00
|
|
|
STPUTC(CTLESC, dest);
|
|
|
|
STPUTC(lastc, dest);
|
|
|
|
}
|
|
|
|
}
|
1995-03-01 02:09:43 +03:00
|
|
|
|
|
|
|
/* Eat all trailing newlines */
|
2005-02-14 23:46:26 +03:00
|
|
|
p = stackblock() + startloc;
|
|
|
|
while (dest > p && dest[-1] == '\n')
|
1993-03-21 12:45:37 +03:00
|
|
|
STUNPUTC(dest);
|
1995-03-01 02:09:43 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (in.fd >= 0)
|
|
|
|
close(in.fd);
|
|
|
|
if (in.buf)
|
|
|
|
ckfree(in.buf);
|
|
|
|
if (in.jp)
|
2002-11-25 01:35:38 +03:00
|
|
|
back_exitstatus = waitforjob(in.jp);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (quoted == 0)
|
|
|
|
recordregion(startloc, dest - stackblock(), 0);
|
|
|
|
TRACE(("evalbackq: size=%d: \"%.*s\"\n",
|
|
|
|
(dest - stackblock()) - startloc,
|
|
|
|
(dest - stackblock()) - startloc,
|
|
|
|
stackblock() + startloc));
|
|
|
|
expdest = dest;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1995-01-23 09:33:03 +03:00
|
|
|
STATIC int
|
2002-11-25 01:35:38 +03:00
|
|
|
subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags)
|
1995-01-23 09:33:03 +03:00
|
|
|
{
|
|
|
|
char *startp;
|
1996-10-16 18:38:58 +04:00
|
|
|
char *loc = NULL;
|
1999-03-26 18:49:34 +03:00
|
|
|
char *q;
|
1995-01-23 09:33:03 +03:00
|
|
|
int c = 0;
|
|
|
|
int saveherefd = herefd;
|
|
|
|
struct nodelist *saveargbackq = argbackq;
|
1996-02-12 18:11:41 +03:00
|
|
|
int amount;
|
|
|
|
|
1995-01-23 09:33:03 +03:00
|
|
|
herefd = -1;
|
1999-01-25 17:20:56 +03:00
|
|
|
argstr(p, 0);
|
1995-01-23 09:33:03 +03:00
|
|
|
STACKSTRNUL(expdest);
|
|
|
|
herefd = saveherefd;
|
|
|
|
argbackq = saveargbackq;
|
|
|
|
startp = stackblock() + startloc;
|
1996-02-12 18:11:41 +03:00
|
|
|
if (str == NULL)
|
|
|
|
str = stackblock() + strloc;
|
1995-01-23 09:33:03 +03:00
|
|
|
|
|
|
|
switch (subtype) {
|
|
|
|
case VSASSIGN:
|
|
|
|
setvar(str, startp, 0);
|
1996-02-12 18:11:41 +03:00
|
|
|
amount = startp - expdest;
|
|
|
|
STADJUST(amount, expdest);
|
1995-01-23 09:33:03 +03:00
|
|
|
varflags &= ~VSNUL;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case VSQUESTION:
|
|
|
|
if (*p != CTLENDVAR) {
|
|
|
|
outfmt(&errout, "%s\n", startp);
|
|
|
|
error((char *)NULL);
|
|
|
|
}
|
|
|
|
error("%.*s: parameter %snot set", p - str - 1,
|
|
|
|
str, (varflags & VSNUL) ? "null or "
|
|
|
|
: nullstr);
|
1998-07-28 15:41:40 +04:00
|
|
|
/* NOTREACHED */
|
1995-01-23 09:33:03 +03:00
|
|
|
|
|
|
|
case VSTRIMLEFT:
|
1997-01-24 20:26:28 +03:00
|
|
|
for (loc = startp; loc < str; loc++) {
|
1995-01-23 09:33:03 +03:00
|
|
|
c = *loc;
|
|
|
|
*loc = '\0';
|
1999-03-26 18:49:34 +03:00
|
|
|
if (patmatch(str, startp, varflags & VSQUOTE))
|
1995-01-23 09:33:03 +03:00
|
|
|
goto recordleft;
|
|
|
|
*loc = c;
|
1999-04-07 01:05:27 +04:00
|
|
|
if ((varflags & VSQUOTE) && *loc == CTLESC)
|
1999-03-26 18:49:34 +03:00
|
|
|
loc++;
|
1995-01-23 09:33:03 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMLEFTMAX:
|
1999-03-26 18:49:34 +03:00
|
|
|
for (loc = str - 1; loc >= startp;) {
|
1995-01-23 09:33:03 +03:00
|
|
|
c = *loc;
|
|
|
|
*loc = '\0';
|
1999-03-26 18:49:34 +03:00
|
|
|
if (patmatch(str, startp, varflags & VSQUOTE))
|
1995-01-23 09:33:03 +03:00
|
|
|
goto recordleft;
|
|
|
|
*loc = c;
|
1999-03-26 18:49:34 +03:00
|
|
|
loc--;
|
|
|
|
if ((varflags & VSQUOTE) && loc > startp &&
|
|
|
|
*(loc - 1) == CTLESC) {
|
|
|
|
for (q = startp; q < loc; q++)
|
|
|
|
if (*q == CTLESC)
|
|
|
|
q++;
|
|
|
|
if (q > loc)
|
|
|
|
loc--;
|
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMRIGHT:
|
1999-03-26 18:49:34 +03:00
|
|
|
for (loc = str - 1; loc >= startp;) {
|
|
|
|
if (patmatch(str, loc, varflags & VSQUOTE))
|
1998-03-23 21:21:02 +03:00
|
|
|
goto recordright;
|
1999-03-26 18:49:34 +03:00
|
|
|
loc--;
|
|
|
|
if ((varflags & VSQUOTE) && loc > startp &&
|
|
|
|
*(loc - 1) == CTLESC) {
|
|
|
|
for (q = startp; q < loc; q++)
|
|
|
|
if (*q == CTLESC)
|
|
|
|
q++;
|
|
|
|
if (q > loc)
|
|
|
|
loc--;
|
|
|
|
}
|
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMRIGHTMAX:
|
1999-03-26 18:49:34 +03:00
|
|
|
for (loc = startp; loc < str - 1; loc++) {
|
|
|
|
if (patmatch(str, loc, varflags & VSQUOTE))
|
1998-03-23 21:21:02 +03:00
|
|
|
goto recordright;
|
1999-03-26 18:49:34 +03:00
|
|
|
if ((varflags & VSQUOTE) && *loc == CTLESC)
|
|
|
|
loc++;
|
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
recordleft:
|
1998-03-23 21:21:02 +03:00
|
|
|
*loc = c;
|
1996-02-12 18:11:41 +03:00
|
|
|
amount = ((str - 1) - (loc - startp)) - expdest;
|
|
|
|
STADJUST(amount, expdest);
|
1995-01-23 09:33:03 +03:00
|
|
|
while (loc != str - 1)
|
|
|
|
*startp++ = *loc++;
|
|
|
|
return 1;
|
1998-03-23 21:21:02 +03:00
|
|
|
|
|
|
|
recordright:
|
|
|
|
amount = loc - expdest;
|
|
|
|
STADJUST(amount, expdest);
|
|
|
|
STPUTC('\0', expdest);
|
|
|
|
STADJUST(-1, expdest);
|
|
|
|
return 1;
|
1995-01-23 09:33:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Expand a variable, and return a pointer to the next character in the
|
|
|
|
* input string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC char *
|
2002-11-25 01:35:38 +03:00
|
|
|
evalvar(char *p, int flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int subtype;
|
1994-05-11 21:09:42 +04:00
|
|
|
int varflags;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *var;
|
|
|
|
char *val;
|
1999-04-30 21:54:17 +04:00
|
|
|
int patloc;
|
1993-03-21 12:45:37 +03:00
|
|
|
int c;
|
|
|
|
int set;
|
|
|
|
int special;
|
|
|
|
int startloc;
|
1995-01-23 09:33:03 +03:00
|
|
|
int varlen;
|
2004-06-26 18:09:58 +04:00
|
|
|
int apply_ifs;
|
1994-05-11 21:09:42 +04:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-09-17 20:01:19 +04:00
|
|
|
varflags = (unsigned char)*p++;
|
1994-05-11 21:09:42 +04:00
|
|
|
subtype = varflags & VSTYPE;
|
1993-03-21 12:45:37 +03:00
|
|
|
var = p;
|
2003-12-21 11:32:39 +03:00
|
|
|
special = !is_name(*p);
|
1993-03-21 12:45:37 +03:00
|
|
|
p = strchr(p, '=') + 1;
|
2004-06-26 18:09:58 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
again: /* jump here after setting a variable with ${var=text} */
|
|
|
|
if (special) {
|
1997-01-24 20:26:28 +03:00
|
|
|
set = varisset(var, varflags & VSNUL);
|
1993-03-21 12:45:37 +03:00
|
|
|
val = NULL;
|
|
|
|
} else {
|
|
|
|
val = lookupvar(var);
|
1995-05-12 01:28:33 +04:00
|
|
|
if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
|
1993-03-21 12:45:37 +03:00
|
|
|
val = NULL;
|
|
|
|
set = 0;
|
|
|
|
} else
|
|
|
|
set = 1;
|
|
|
|
}
|
2004-06-26 18:09:58 +04:00
|
|
|
|
1995-01-23 09:33:03 +03:00
|
|
|
varlen = 0;
|
1999-01-25 17:20:56 +03:00
|
|
|
startloc = expdest - stackblock();
|
2004-06-26 18:09:58 +04:00
|
|
|
|
|
|
|
if (!set && uflag) {
|
2002-05-15 18:59:21 +04:00
|
|
|
switch (subtype) {
|
|
|
|
case VSNORMAL:
|
|
|
|
case VSTRIMLEFT:
|
|
|
|
case VSTRIMLEFTMAX:
|
|
|
|
case VSTRIMRIGHT:
|
|
|
|
case VSTRIMRIGHTMAX:
|
|
|
|
case VSLENGTH:
|
|
|
|
error("%.*s: parameter not set", p - var - 1, var);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2004-06-26 18:09:58 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (set && subtype != VSPLUS) {
|
|
|
|
/* insert the value of the variable */
|
|
|
|
if (special) {
|
2003-09-22 16:17:24 +04:00
|
|
|
varvalue(var, varflags & VSQUOTE, subtype, flag);
|
1995-01-23 09:33:03 +03:00
|
|
|
if (subtype == VSLENGTH) {
|
1997-03-18 21:54:40 +03:00
|
|
|
varlen = expdest - stackblock() - startloc;
|
|
|
|
STADJUST(-varlen, expdest);
|
1995-01-23 09:33:03 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
1996-10-16 18:38:58 +04:00
|
|
|
char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
|
1995-01-23 09:33:03 +03:00
|
|
|
: BASESYNTAX;
|
|
|
|
|
|
|
|
if (subtype == VSLENGTH) {
|
|
|
|
for (;*val; val++)
|
|
|
|
varlen++;
|
2004-06-26 18:09:58 +04:00
|
|
|
} else {
|
1995-01-23 09:33:03 +03:00
|
|
|
while (*val) {
|
1998-03-29 08:41:43 +04:00
|
|
|
if (quotes && syntax[(int)*val] == CCTL)
|
1995-01-23 09:33:03 +03:00
|
|
|
STPUTC(CTLESC, expdest);
|
|
|
|
STPUTC(*val++, expdest);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-10-16 18:38:58 +04:00
|
|
|
|
1995-01-23 09:33:03 +03:00
|
|
|
|
2004-06-26 18:09:58 +04:00
|
|
|
apply_ifs = ((varflags & VSQUOTE) == 0 ||
|
1995-01-23 09:33:03 +03:00
|
|
|
(*var == '@' && shellparam.nparam != 1));
|
|
|
|
|
|
|
|
switch (subtype) {
|
|
|
|
case VSLENGTH:
|
|
|
|
expdest = cvtnum(varlen, expdest);
|
2004-06-26 18:09:58 +04:00
|
|
|
break;
|
1995-01-23 09:33:03 +03:00
|
|
|
|
|
|
|
case VSNORMAL:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VSPLUS:
|
2004-06-26 18:09:58 +04:00
|
|
|
set = !set;
|
|
|
|
/* FALLTHROUGH */
|
1995-01-23 09:33:03 +03:00
|
|
|
case VSMINUS:
|
2004-06-09 16:17:36 +04:00
|
|
|
if (!set) {
|
2004-06-27 02:09:49 +04:00
|
|
|
argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
|
2004-06-26 18:09:58 +04:00
|
|
|
/*
|
|
|
|
* ${x-a b c} doesn't get split, but removing the
|
2006-11-25 01:54:47 +03:00
|
|
|
* 'apply_ifs = 0' apparently breaks ${1+"$@"}..
|
2004-06-26 18:09:58 +04:00
|
|
|
* ${x-'a b' c} should generate 2 args.
|
|
|
|
*/
|
|
|
|
/* We should have marked stuff already */
|
|
|
|
apply_ifs = 0;
|
2004-06-09 16:17:36 +04:00
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
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-30 21:54:17 +04:00
|
|
|
patloc = expdest - stackblock();
|
|
|
|
if (subevalvar(p, NULL, patloc, subtype,
|
1999-01-25 17:20:56 +03:00
|
|
|
startloc, varflags) == 0) {
|
1999-04-30 21:54:17 +04:00
|
|
|
int amount = (expdest - stackblock() - patloc) + 1;
|
1997-01-24 20:26:28 +03:00
|
|
|
STADJUST(-amount, expdest);
|
|
|
|
}
|
1999-01-25 17:20:56 +03:00
|
|
|
/* Remove any recorded regions beyond start of variable */
|
|
|
|
removerecordregions(startloc);
|
2004-06-26 18:09:58 +04:00
|
|
|
apply_ifs = 1;
|
|
|
|
break;
|
1995-01-23 09:33:03 +03:00
|
|
|
|
|
|
|
case VSASSIGN:
|
|
|
|
case VSQUESTION:
|
2004-06-26 18:09:58 +04:00
|
|
|
if (set)
|
1995-01-23 09:33:03 +03:00
|
|
|
break;
|
2004-06-26 18:09:58 +04:00
|
|
|
if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
|
|
|
|
varflags &= ~VSNUL;
|
|
|
|
/*
|
|
|
|
* Remove any recorded regions beyond
|
|
|
|
* start of variable
|
|
|
|
*/
|
|
|
|
removerecordregions(startloc);
|
|
|
|
goto again;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2004-06-26 18:09:58 +04:00
|
|
|
apply_ifs = 0;
|
1995-01-23 09:33:03 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
|
2004-06-26 18:09:58 +04:00
|
|
|
if (apply_ifs)
|
|
|
|
recordregion(startloc, expdest - stackblock(),
|
|
|
|
varflags & VSQUOTE);
|
|
|
|
|
1993-03-21 12:45:37 +03: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)) {
|
1993-10-22 16:32:22 +03:00
|
|
|
if (set)
|
1993-03-21 12:45:37 +03:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC int
|
2002-11-25 01:35:38 +03:00
|
|
|
varisset(char *name, int nulok)
|
1997-01-24 20:26:28 +03:00
|
|
|
{
|
|
|
|
if (*name == '!')
|
|
|
|
return backgndpid != -1;
|
|
|
|
else if (*name == '@' || *name == '*') {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*shellparam.p == NULL)
|
|
|
|
return 0;
|
1997-01-24 20:26:28 +03:00
|
|
|
|
|
|
|
if (nulok) {
|
1997-03-03 22:26:18 +03:00
|
|
|
char **av;
|
|
|
|
|
|
|
|
for (av = shellparam.p; *av; av++)
|
|
|
|
if (**av != '\0')
|
1997-01-24 20:26:28 +03:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1996-10-16 18:38:58 +04:00
|
|
|
} else if (is_digit(*name)) {
|
1997-03-03 22:26:18 +03:00
|
|
|
char *ap;
|
1996-10-16 18:38:58 +04:00
|
|
|
int num = atoi(name);
|
1997-01-24 20:26:28 +03:00
|
|
|
|
1997-03-03 22:26:18 +03:00
|
|
|
if (num > shellparam.nparam)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (num == 0)
|
|
|
|
ap = arg0;
|
|
|
|
else
|
|
|
|
ap = shellparam.p[num - 1];
|
1997-01-24 20:26:28 +03:00
|
|
|
|
1997-03-03 22:26:18 +03:00
|
|
|
if (nulok && (ap == NULL || *ap == '\0'))
|
1997-01-24 20:26:28 +03:00
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the value of a specialized variable to the stack string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2003-09-22 16:17:24 +04:00
|
|
|
varvalue(char *name, int quoted, int subtype, int flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int num;
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
char sep;
|
|
|
|
char **ap;
|
|
|
|
char const *syntax;
|
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
#define STRTODEST(p) \
|
|
|
|
do {\
|
2003-09-22 16:17:24 +04:00
|
|
|
if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
|
1994-05-11 21:09:42 +04:00
|
|
|
syntax = quoted? DQSYNTAX : BASESYNTAX; \
|
|
|
|
while (*p) { \
|
1998-03-29 08:41:43 +04:00
|
|
|
if (syntax[(int)*p] == CCTL) \
|
1994-05-11 21:09:42 +04:00
|
|
|
STPUTC(CTLESC, expdest); \
|
|
|
|
STPUTC(*p++, expdest); \
|
|
|
|
} \
|
|
|
|
} else \
|
|
|
|
while (*p) \
|
|
|
|
STPUTC(*p++, expdest); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
1996-10-16 18:38:58 +04:00
|
|
|
switch (*name) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case '$':
|
|
|
|
num = rootpid;
|
|
|
|
goto numvar;
|
|
|
|
case '?':
|
2002-11-25 01:35:38 +03:00
|
|
|
num = exitstatus;
|
1993-03-21 12:45:37 +03:00
|
|
|
goto numvar;
|
|
|
|
case '#':
|
|
|
|
num = shellparam.nparam;
|
|
|
|
goto numvar;
|
|
|
|
case '!':
|
|
|
|
num = backgndpid;
|
|
|
|
numvar:
|
1995-01-23 09:33:03 +03:00
|
|
|
expdest = cvtnum(num, expdest);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case '-':
|
2002-11-25 01:35:38 +03:00
|
|
|
for (i = 0; optlist[i].name; i++) {
|
2005-12-13 20:44:18 +03:00
|
|
|
if (optlist[i].val && optlist[i].letter)
|
1994-05-11 21:09:42 +04:00
|
|
|
STPUTC(optlist[i].letter, expdest);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '@':
|
2003-09-22 16:17:24 +04:00
|
|
|
if (flag & EXP_FULL && quoted) {
|
1999-01-25 17:20:56 +03:00
|
|
|
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
|
|
|
|
STRTODEST(p);
|
2006-08-22 01:30:14 +04:00
|
|
|
if (*ap)
|
|
|
|
/* A NUL separates args inside "" */
|
|
|
|
STPUTC('\0', expdest);
|
1999-01-25 17:20:56 +03:00
|
|
|
}
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-10-16 18:38:58 +04:00
|
|
|
/* fall through */
|
1993-03-21 12:45:37 +03:00
|
|
|
case '*':
|
1999-01-25 17:20:56 +03:00
|
|
|
if (ifsset() != 0)
|
|
|
|
sep = ifsval()[0];
|
|
|
|
else
|
|
|
|
sep = ' ';
|
1993-03-21 12:45:37 +03:00
|
|
|
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
|
1994-05-11 21:09:42 +04:00
|
|
|
STRTODEST(p);
|
1999-01-25 17:20:56 +03:00
|
|
|
if (*ap && sep)
|
1993-03-21 12:45:37 +03:00
|
|
|
STPUTC(sep, expdest);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
p = arg0;
|
1994-05-11 21:09:42 +04:00
|
|
|
STRTODEST(p);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
1996-10-16 18:38:58 +04:00
|
|
|
if (is_digit(*name)) {
|
|
|
|
num = atoi(name);
|
|
|
|
if (num > 0 && num <= shellparam.nparam) {
|
|
|
|
p = shellparam.p[num - 1];
|
|
|
|
STRTODEST(p);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-03-14 01:46:59 +03:00
|
|
|
* Record the fact that we have to scan this region of the
|
1993-03-21 12:45:37 +03:00
|
|
|
* string for IFS characters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2004-06-26 18:09:58 +04:00
|
|
|
recordregion(int start, int end, int inquotes)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
struct ifsregion *ifsp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (ifslastp == NULL) {
|
|
|
|
ifsp = &ifsfirst;
|
|
|
|
} else {
|
2005-03-19 18:02:58 +03:00
|
|
|
if (ifslastp->endoff == start
|
|
|
|
&& ifslastp->inquotes == inquotes) {
|
2004-06-27 02:09:49 +04:00
|
|
|
/* extend previous area */
|
|
|
|
ifslastp->endoff = end;
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
|
|
|
|
ifslastp->next = ifsp;
|
|
|
|
}
|
|
|
|
ifslastp = ifsp;
|
|
|
|
ifslastp->next = NULL;
|
|
|
|
ifslastp->begoff = start;
|
|
|
|
ifslastp->endoff = end;
|
2004-06-26 18:09:58 +04:00
|
|
|
ifslastp->inquotes = inquotes;
|
1993-03-21 12:45:37 +03: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.
|
|
|
|
*/
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
ifsbreakup(char *string, struct arglist *arglist)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct ifsregion *ifsp;
|
|
|
|
struct strlist *sp;
|
|
|
|
char *start;
|
1997-01-11 05:04:27 +03:00
|
|
|
char *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *q;
|
1999-07-09 07:05:49 +04:00
|
|
|
const char *ifs;
|
2004-06-26 18:09:58 +04:00
|
|
|
const char *ifsspc;
|
2006-08-22 01:30:14 +04:00
|
|
|
int had_param_ch = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
start = string;
|
2004-06-26 18:09:58 +04: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) {
|
2006-08-22 01:30:14 +04:00
|
|
|
had_param_ch = 1;
|
2004-06-26 18:09:58 +04:00
|
|
|
q = p;
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
2006-08-22 01:30:14 +04:00
|
|
|
if (ifsp->inquotes) {
|
|
|
|
/* Only NULs (should be from "$@") end args */
|
2004-06-26 18:09:58 +04:00
|
|
|
if (*p != 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
p++;
|
2004-06-26 18:09:58 +04:00
|
|
|
continue;
|
|
|
|
}
|
2006-08-22 01:30:14 +04:00
|
|
|
ifsspc = NULL;
|
2004-06-26 18:09:58 +04:00
|
|
|
} else {
|
|
|
|
if (!strchr(ifs, *p)) {
|
1999-01-25 17:20:56 +03:00
|
|
|
p++;
|
2004-06-26 18:09:58 +04:00
|
|
|
continue;
|
|
|
|
}
|
2006-08-22 22:11:42 +04:00
|
|
|
had_param_ch = 0;
|
2004-06-26 18:09:58 +04:00
|
|
|
ifsspc = strchr(" \t\n", *p);
|
|
|
|
|
|
|
|
/* Ignore IFS whitespace at start */
|
|
|
|
if (q == start && ifsspc != NULL) {
|
1999-01-25 17:20:56 +03:00
|
|
|
p++;
|
2004-06-26 18:09:58 +04:00
|
|
|
start = p;
|
|
|
|
continue;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2004-06-26 18:09:58 +04:00
|
|
|
|
|
|
|
/* Save this argument... */
|
|
|
|
*q = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = start;
|
|
|
|
*arglist->lastp = sp;
|
|
|
|
arglist->lastp = &sp->next;
|
2004-06-26 18:09:58 +04:00
|
|
|
p++;
|
|
|
|
|
2005-03-19 19:38:27 +03:00
|
|
|
if (ifsspc != NULL) {
|
|
|
|
/* Ignore further trailing IFS whitespace */
|
2004-06-26 18:09:58 +04:00
|
|
|
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) {
|
2005-03-19 19:38:27 +03:00
|
|
|
p++;
|
|
|
|
break;
|
2004-06-26 18:09:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start = p;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2004-06-26 18:09:58 +04:00
|
|
|
}
|
|
|
|
|
2005-03-19 19:38:27 +03: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....
|
|
|
|
*/
|
2006-08-22 01:30:14 +04:00
|
|
|
if (had_param_ch || *start != 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = start;
|
|
|
|
*arglist->lastp = sp;
|
|
|
|
arglist->lastp = &sp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-05 11:32:00 +03:00
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
ifsfree(void)
|
1998-02-05 11:32:00 +03:00
|
|
|
{
|
|
|
|
while (ifsfirst.next != NULL) {
|
|
|
|
struct ifsregion *ifsp;
|
|
|
|
INTOFF;
|
|
|
|
ifsp = ifsfirst.next->next;
|
|
|
|
ckfree(ifsfirst.next);
|
|
|
|
ifsfirst.next = ifsp;
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
ifslastp = NULL;
|
|
|
|
ifsfirst.next = NULL;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand shell metacharacters. At this point, the only control characters
|
|
|
|
* should be escapes. The results are stored in the list exparg.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *expdir;
|
|
|
|
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
expandmeta(struct strlist *str, int flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
char *p;
|
|
|
|
struct strlist **savelastp;
|
|
|
|
struct strlist *sp;
|
|
|
|
char c;
|
1994-05-11 21:09:42 +04:00
|
|
|
/* TODO - EXP_REDIR */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
while (str) {
|
|
|
|
if (fflag)
|
|
|
|
goto nometa;
|
|
|
|
p = str->text;
|
|
|
|
for (;;) { /* fast check for meta chars */
|
|
|
|
if ((c = *p++) == '\0')
|
|
|
|
goto nometa;
|
|
|
|
if (c == '*' || c == '?' || c == '[' || c == '!')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
savelastp = exparg.lastp;
|
|
|
|
INTOFF;
|
1994-05-11 21:09:42 +04:00
|
|
|
if (expdir == NULL) {
|
|
|
|
int i = strlen(str->text);
|
|
|
|
expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
expmeta(expdir, str->text);
|
|
|
|
ckfree(expdir);
|
|
|
|
expdir = NULL;
|
|
|
|
INTON;
|
|
|
|
if (exparg.lastp == savelastp) {
|
1996-10-16 18:38:58 +04:00
|
|
|
/*
|
|
|
|
* no matches
|
1994-05-11 21:09:42 +04:00
|
|
|
*/
|
1993-03-21 12:45:37 +03:00
|
|
|
nometa:
|
1994-05-11 21:09:42 +04:00
|
|
|
*exparg.lastp = str;
|
|
|
|
rmescapes(str->text);
|
|
|
|
exparg.lastp = &str->next;
|
1993-03-21 12:45:37 +03:00
|
|
|
} 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
expmeta(char *enddir, char *name)
|
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
char *p;
|
1999-07-09 07:05:49 +04:00
|
|
|
const char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *q;
|
|
|
|
char *start;
|
|
|
|
char *endname;
|
|
|
|
int metaflag;
|
|
|
|
struct stat statb;
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
|
|
|
int atend;
|
|
|
|
int matchdot;
|
|
|
|
|
|
|
|
metaflag = 0;
|
|
|
|
start = name;
|
|
|
|
for (p = name ; ; p++) {
|
|
|
|
if (*p == '*' || *p == '?')
|
|
|
|
metaflag = 1;
|
|
|
|
else if (*p == '[') {
|
|
|
|
q = p + 1;
|
|
|
|
if (*q == '!')
|
|
|
|
q++;
|
|
|
|
for (;;) {
|
1999-01-25 17:20:56 +03:00
|
|
|
while (*q == CTLQUOTEMARK)
|
|
|
|
q++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*q == CTLESC)
|
|
|
|
q++;
|
|
|
|
if (*q == '/' || *q == '\0')
|
|
|
|
break;
|
|
|
|
if (*++q == ']') {
|
|
|
|
metaflag = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
|
|
|
|
metaflag = 1;
|
|
|
|
} else if (*p == '\0')
|
|
|
|
break;
|
1999-01-25 17:20:56 +03:00
|
|
|
else if (*p == CTLQUOTEMARK)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
else if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
if (*p == '/') {
|
|
|
|
if (metaflag)
|
|
|
|
break;
|
|
|
|
start = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (metaflag == 0) { /* we've reached the end of the file name */
|
|
|
|
if (enddir != expdir)
|
|
|
|
metaflag++;
|
|
|
|
for (p = name ; ; p++) {
|
1999-01-25 17:20:56 +03:00
|
|
|
if (*p == CTLQUOTEMARK)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*enddir++ = *p;
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
}
|
2001-03-30 21:45:00 +04:00
|
|
|
if (metaflag == 0 || lstat(expdir, &statb) >= 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
addfname(expdir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
endname = p;
|
|
|
|
if (start != name) {
|
|
|
|
p = name;
|
|
|
|
while (p < start) {
|
1999-01-25 17:20:56 +03:00
|
|
|
while (*p == CTLQUOTEMARK)
|
|
|
|
p++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*enddir++ = *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (enddir == expdir) {
|
1999-07-09 07:05:49 +04:00
|
|
|
cp = ".";
|
1993-03-21 12:45:37 +03:00
|
|
|
} else if (enddir == expdir + 1 && *expdir == '/') {
|
1999-07-09 07:05:49 +04:00
|
|
|
cp = "/";
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
1999-07-09 07:05:49 +04:00
|
|
|
cp = expdir;
|
1993-03-21 12:45:37 +03:00
|
|
|
enddir[-1] = '\0';
|
|
|
|
}
|
1999-07-09 07:05:49 +04:00
|
|
|
if ((dirp = opendir(cp)) == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
if (enddir != expdir)
|
|
|
|
enddir[-1] = '/';
|
|
|
|
if (*endname == 0) {
|
|
|
|
atend = 1;
|
|
|
|
} else {
|
|
|
|
atend = 0;
|
|
|
|
*endname++ = '\0';
|
|
|
|
}
|
|
|
|
matchdot = 0;
|
1999-01-25 17:20:56 +03:00
|
|
|
p = start;
|
|
|
|
while (*p == CTLQUOTEMARK)
|
|
|
|
p++;
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
if (*p == '.')
|
1993-03-21 12:45:37 +03:00
|
|
|
matchdot++;
|
|
|
|
while (! int_pending() && (dp = readdir(dirp)) != NULL) {
|
|
|
|
if (dp->d_name[0] == '.' && ! matchdot)
|
|
|
|
continue;
|
1999-03-26 18:49:34 +03:00
|
|
|
if (patmatch(start, dp->d_name, 0)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (atend) {
|
|
|
|
scopy(dp->d_name, enddir);
|
|
|
|
addfname(expdir);
|
|
|
|
} else {
|
1999-07-09 07:05:49 +04:00
|
|
|
for (p = enddir, cp = dp->d_name;
|
|
|
|
(*p++ = *cp++) != '\0';)
|
1995-05-12 01:28:33 +04:00
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
p[-1] = '/';
|
|
|
|
expmeta(p, endname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
if (! atend)
|
|
|
|
endname[-1] = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a file name to the list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
2002-11-25 01:35:38 +03:00
|
|
|
addfname(char *name)
|
|
|
|
{
|
1993-03-21 12:45:37 +03: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC struct strlist *
|
2002-11-25 01:35:38 +03:00
|
|
|
expsort(struct strlist *str)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int len;
|
|
|
|
struct strlist *sp;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
for (sp = str ; sp ; sp = sp->next)
|
|
|
|
len++;
|
|
|
|
return msort(str, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STATIC struct strlist *
|
2002-11-25 01:35:38 +03:00
|
|
|
msort(struct strlist *list, int len)
|
1994-12-04 10:11:37 +03:00
|
|
|
{
|
1996-10-16 18:38:58 +04:00
|
|
|
struct strlist *p, *q = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct strlist **lpp;
|
|
|
|
int half;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (len <= 1)
|
|
|
|
return list;
|
1996-10-16 18:38:58 +04:00
|
|
|
half = len >> 1;
|
1993-03-21 12:45:37 +03: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
|
2002-11-25 01:35:38 +03:00
|
|
|
patmatch(char *pattern, char *string, int squoted)
|
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
#ifdef notdef
|
1993-03-21 12:45:37 +03:00
|
|
|
if (pattern[0] == '!' && pattern[1] == '!')
|
|
|
|
return 1 - pmatch(pattern + 2, string);
|
|
|
|
else
|
1994-05-11 21:09:42 +04:00
|
|
|
#endif
|
1999-03-26 18:49:34 +03:00
|
|
|
return pmatch(pattern, string, squoted);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STATIC int
|
2002-11-25 01:35:38 +03:00
|
|
|
pmatch(char *pattern, char *string, int squoted)
|
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
char *p, *q;
|
|
|
|
char c;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
p = pattern;
|
|
|
|
q = string;
|
|
|
|
for (;;) {
|
|
|
|
switch (c = *p++) {
|
|
|
|
case '\0':
|
|
|
|
goto breakloop;
|
|
|
|
case CTLESC:
|
1999-03-26 18:49:34 +03:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*q++ != *p++)
|
|
|
|
return 0;
|
|
|
|
break;
|
1999-01-25 17:20:56 +03:00
|
|
|
case CTLQUOTEMARK:
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
1999-03-26 18:49:34 +03:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*q++ == '\0')
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
c = *p;
|
1999-01-25 17:20:56 +03:00
|
|
|
while (c == CTLQUOTEMARK || c == '*')
|
|
|
|
c = *++p;
|
|
|
|
if (c != CTLESC && c != CTLQUOTEMARK &&
|
|
|
|
c != '?' && c != '*' && c != '[') {
|
1993-03-21 12:45:37 +03:00
|
|
|
while (*q != c) {
|
1999-03-26 18:49:34 +03:00
|
|
|
if (squoted && *q == CTLESC &&
|
|
|
|
q[1] == c)
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*q == '\0')
|
|
|
|
return 0;
|
1999-03-26 18:49:34 +03:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1993-03-21 12:45:37 +03:00
|
|
|
q++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do {
|
1999-03-26 18:49:34 +03:00
|
|
|
if (pmatch(p, q, squoted))
|
1993-03-21 12:45:37 +03:00
|
|
|
return 1;
|
1999-03-26 18:49:34 +03:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (*q++ != '\0');
|
|
|
|
return 0;
|
|
|
|
case '[': {
|
|
|
|
char *endp;
|
|
|
|
int invert, found;
|
|
|
|
char chr;
|
|
|
|
|
|
|
|
endp = p;
|
|
|
|
if (*endp == '!')
|
|
|
|
endp++;
|
|
|
|
for (;;) {
|
1999-01-25 17:20:56 +03:00
|
|
|
while (*endp == CTLQUOTEMARK)
|
|
|
|
endp++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*endp == '\0')
|
|
|
|
goto dft; /* no matching ] */
|
|
|
|
if (*endp == CTLESC)
|
|
|
|
endp++;
|
|
|
|
if (*++endp == ']')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
invert = 0;
|
|
|
|
if (*p == '!') {
|
|
|
|
invert++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
found = 0;
|
|
|
|
chr = *q++;
|
1999-03-26 18:49:34 +03:00
|
|
|
if (squoted && chr == CTLESC)
|
|
|
|
chr = *q++;
|
1994-08-29 05:54:39 +04:00
|
|
|
if (chr == '\0')
|
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
c = *p++;
|
|
|
|
do {
|
1999-01-25 17:20:56 +03:00
|
|
|
if (c == CTLQUOTEMARK)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (c == CTLESC)
|
|
|
|
c = *p++;
|
|
|
|
if (*p == '-' && p[1] != ']') {
|
|
|
|
p++;
|
1999-01-25 17:20:56 +03:00
|
|
|
while (*p == CTLQUOTEMARK)
|
|
|
|
p++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
if (chr >= c && chr <= *p)
|
|
|
|
found = 1;
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
if (chr == c)
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
} while ((c = *p++) != ']');
|
|
|
|
if (found == invert)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
dft: default:
|
1999-03-26 18:49:34 +03:00
|
|
|
if (squoted && *q == CTLESC)
|
|
|
|
q++;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*q++ != c)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
breakloop:
|
|
|
|
if (*q != '\0')
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove any CTLESC characters from a string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
rmescapes(char *str)
|
1999-01-25 17:20:56 +03:00
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
char *p, *q;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
p = str;
|
1999-01-25 17:20:56 +03:00
|
|
|
while (*p != CTLESC && *p != CTLQUOTEMARK) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*p++ == '\0')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
q = p;
|
|
|
|
while (*p) {
|
1999-01-25 17:20:56 +03:00
|
|
|
if (*p == CTLQUOTEMARK) {
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if a pattern matches in a case statement.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
casematch(union node *pattern, char *val)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stackmark smark;
|
|
|
|
int result;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
setstackmark(&smark);
|
|
|
|
argbackq = pattern->narg.backquote;
|
|
|
|
STARTSTACKSTR(expdest);
|
|
|
|
ifslastp = NULL;
|
1999-01-25 17:20:56 +03:00
|
|
|
argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
|
1993-03-21 12:45:37 +03:00
|
|
|
STPUTC('\0', expdest);
|
|
|
|
p = grabstackstr(expdest);
|
1999-03-26 18:49:34 +03:00
|
|
|
result = patmatch(p, val, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
popstackmark(&smark);
|
|
|
|
return result;
|
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Our own itoa().
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC char *
|
2002-11-25 01:35:38 +03:00
|
|
|
cvtnum(int num, char *buf)
|
|
|
|
{
|
1995-01-23 09:33:03 +03: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 = '-';
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
STPUTC(*p++, buf);
|
|
|
|
return buf;
|
|
|
|
}
|
2004-07-13 19:05:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do most of the work for wordexp(3).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
wordexpcmd(int argc, char **argv)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
out1fmt("%d", argc - 1);
|
|
|
|
out1c('\0');
|
|
|
|
for (i = 1, len = 0; i < argc; i++)
|
|
|
|
len += strlen(argv[i]);
|
|
|
|
out1fmt("%zd", len);
|
|
|
|
out1c('\0');
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
out1str(argv[i]);
|
|
|
|
out1c('\0');
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|