1995-05-15 06:47:38 +04:00
|
|
|
/* $NetBSD: expand.c,v 1.19 1995/05/15 02:47:41 christos 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 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
|
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
|
1995-05-15 06:47:38 +04:00
|
|
|
static char rcsid[] = "$NetBSD: expand.c,v 1.19 1995/05/15 02:47:41 christos 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>
|
|
|
|
|
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 "arith.h"
|
|
|
|
#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 */
|
|
|
|
int nulonly; /* search for nul bytes only */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
1995-05-12 01:28:33 +04:00
|
|
|
STATIC void argstr __P((char *, int));
|
|
|
|
STATIC char *exptilde __P((char *, int));
|
|
|
|
STATIC void expbackq __P((union node *, int, int));
|
|
|
|
STATIC int subevalvar __P((char *, char *, int, int, int));
|
|
|
|
STATIC char *evalvar __P((char *, int));
|
|
|
|
STATIC int varisset __P((int));
|
|
|
|
STATIC void varvalue __P((int, int, int));
|
|
|
|
STATIC void recordregion __P((int, int, int));
|
|
|
|
STATIC void ifsbreakup __P((char *, struct arglist *));
|
|
|
|
STATIC void expandmeta __P((struct strlist *, int));
|
|
|
|
STATIC void expmeta __P((char *, char *));
|
|
|
|
STATIC void addfname __P((char *));
|
|
|
|
STATIC struct strlist *expsort __P((struct strlist *));
|
|
|
|
STATIC struct strlist *msort __P((struct strlist *, int));
|
|
|
|
STATIC int pmatch __P((char *, char *));
|
|
|
|
STATIC char *cvtnum __P((int, char *));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand shell variables and backquotes inside a here document.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
expandhere(arg, fd)
|
|
|
|
union node *arg; /* the document */
|
|
|
|
int fd; /* where to write the expanded version */
|
|
|
|
{
|
|
|
|
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
|
1994-05-11 21:09:42 +04:00
|
|
|
expandarg(arg, arglist, flag)
|
1993-03-21 12:45:37 +03:00
|
|
|
union node *arg;
|
|
|
|
struct arglist *arglist;
|
1994-12-04 10:11:37 +03:00
|
|
|
int flag;
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct strlist *sp;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
argbackq = arg->narg.backquote;
|
|
|
|
STARTSTACKSTR(expdest);
|
|
|
|
ifsfirst.next = NULL;
|
|
|
|
ifslastp = NULL;
|
1994-05-11 21:09:42 +04:00
|
|
|
argstr(arg->narg.text, flag);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-05-11 21:09:42 +04:00
|
|
|
* Perform variable and command substitution. If EXP_FULL is set, output CTLESC
|
|
|
|
* characters to allow for further processing. Otherwise treat
|
1993-03-21 12:45:37 +03:00
|
|
|
* $@ like $* since no splitting will be performed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
1994-05-11 21:09:42 +04:00
|
|
|
argstr(p, flag)
|
1993-03-21 12:45:37 +03:00
|
|
|
register char *p;
|
1994-12-04 10:11:37 +03:00
|
|
|
int flag;
|
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
register char c;
|
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */
|
|
|
|
int firsteq = 1;
|
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':
|
1994-05-11 21:09:42 +04:00
|
|
|
case CTLENDVAR: /* ??? */
|
1993-03-21 12:45:37 +03:00
|
|
|
goto breakloop;
|
|
|
|
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:
|
1994-05-11 21:09:42 +04:00
|
|
|
p = evalvar(p, flag);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
breakloop:;
|
|
|
|
}
|
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
STATIC char *
|
|
|
|
exptilde(p, flag)
|
|
|
|
char *p;
|
1994-12-04 10:11:37 +03:00
|
|
|
int flag;
|
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
char c, *startp = p;
|
|
|
|
struct passwd *pw;
|
|
|
|
char *home;
|
|
|
|
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);
|
|
|
|
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') {
|
1994-05-11 21:09:42 +04:00
|
|
|
if (quotes && SQSYNTAX[c] == CCTL)
|
|
|
|
STPUTC(CTLESC, expdest);
|
|
|
|
STPUTC(c, expdest);
|
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
lose:
|
|
|
|
*p = c;
|
|
|
|
return (startp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand arithmetic expression. Backup to start of expression,
|
|
|
|
* evaluate, place result in (backed up) result, adjust string position.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
expari(flag)
|
1994-12-04 10:11:37 +03:00
|
|
|
int flag;
|
|
|
|
{
|
1994-05-11 21:09:42 +04:00
|
|
|
char *p, *start;
|
|
|
|
int result;
|
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is slightly over-compilcated for
|
|
|
|
* 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
|
|
|
|
* characters have to be processed left to right.
|
|
|
|
*/
|
|
|
|
CHECKSTRSPACE(8, expdest);
|
|
|
|
USTPUTC('\0', expdest);
|
|
|
|
start = stackblock();
|
|
|
|
p = expdest;
|
|
|
|
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++;
|
|
|
|
if (quotes)
|
|
|
|
rmescapes(p+1);
|
|
|
|
result = arith(p+1);
|
|
|
|
fmtstr(p, 10, "%d", result);
|
|
|
|
while (*p++)
|
|
|
|
;
|
|
|
|
result = expdest - p + 1;
|
|
|
|
STADJUST(-result, expdest);
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand stuff in backwards quotes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
1994-05-11 21:09:42 +04:00
|
|
|
expbackq(cmd, quoted, flag)
|
1993-03-21 12:45:37 +03:00
|
|
|
union node *cmd;
|
1994-12-04 10:11:37 +03:00
|
|
|
int quoted;
|
|
|
|
int flag;
|
|
|
|
{
|
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;
|
|
|
|
saveherefd = herefd;
|
|
|
|
herefd = -1;
|
|
|
|
p = grabstackstr(dest);
|
|
|
|
evalbackcmd(cmd, &in);
|
|
|
|
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') {
|
1994-05-11 21:09:42 +04:00
|
|
|
if (quotes && syntax[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 */
|
|
|
|
for (p--; lastc == '\n'; lastc = *--p)
|
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)
|
1993-05-02 05:28:40 +04:00
|
|
|
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
|
|
|
|
subevalvar(p, str, subtype, startloc, varflags)
|
|
|
|
char *p;
|
|
|
|
char *str;
|
|
|
|
int subtype;
|
|
|
|
int startloc;
|
|
|
|
int varflags;
|
|
|
|
{
|
|
|
|
|
|
|
|
char *startp;
|
|
|
|
char *loc;
|
|
|
|
int c = 0;
|
|
|
|
int saveherefd = herefd;
|
|
|
|
struct nodelist *saveargbackq = argbackq;
|
|
|
|
herefd = -1;
|
|
|
|
argstr(p, 0);
|
|
|
|
STACKSTRNUL(expdest);
|
|
|
|
herefd = saveherefd;
|
|
|
|
argbackq = saveargbackq;
|
|
|
|
startp = stackblock() + startloc;
|
|
|
|
|
|
|
|
switch (subtype) {
|
|
|
|
case VSASSIGN:
|
|
|
|
setvar(str, startp, 0);
|
|
|
|
STADJUST(startp - expdest, expdest);
|
|
|
|
varflags &= ~VSNUL;
|
|
|
|
if (c != 0)
|
|
|
|
*loc = c;
|
|
|
|
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);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMLEFT:
|
|
|
|
for (loc = startp; loc < str - 1; loc++) {
|
|
|
|
c = *loc;
|
|
|
|
*loc = '\0';
|
|
|
|
if (patmatch(str, startp)) {
|
|
|
|
*loc = c;
|
|
|
|
goto recordleft;
|
|
|
|
}
|
|
|
|
*loc = c;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMLEFTMAX:
|
|
|
|
for (loc = str - 1; loc >= startp; loc--) {
|
|
|
|
c = *loc;
|
|
|
|
*loc = '\0';
|
|
|
|
if (patmatch(str, startp)) {
|
|
|
|
*loc = c;
|
|
|
|
goto recordleft;
|
|
|
|
}
|
|
|
|
*loc = c;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMRIGHT:
|
|
|
|
for (loc = str - 1; loc >= startp; loc--) {
|
|
|
|
if (patmatch(str, loc)) {
|
|
|
|
expdest = loc;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VSTRIMRIGHTMAX:
|
|
|
|
for (loc = startp; loc < str - 1; loc++) {
|
|
|
|
if (patmatch(str, loc)) {
|
|
|
|
expdest = loc;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
recordleft:
|
|
|
|
expdest = (str - 1) - (loc - startp);
|
|
|
|
while (loc != str - 1)
|
|
|
|
*startp++ = *loc++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 *
|
1994-05-11 21:09:42 +04:00
|
|
|
evalvar(p, flag)
|
1993-03-21 12:45:37 +03:00
|
|
|
char *p;
|
1994-12-04 10:11:37 +03:00
|
|
|
int flag;
|
|
|
|
{
|
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;
|
1995-01-23 09:33:03 +03:00
|
|
|
char *pat;
|
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;
|
|
|
|
int easy;
|
1994-05-11 21:09:42 +04:00
|
|
|
int quotes = flag & (EXP_FULL | EXP_CASE);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
varflags = *p++;
|
|
|
|
subtype = varflags & VSTYPE;
|
1993-03-21 12:45:37 +03:00
|
|
|
var = p;
|
|
|
|
special = 0;
|
|
|
|
if (! is_name(*p))
|
|
|
|
special = 1;
|
|
|
|
p = strchr(p, '=') + 1;
|
|
|
|
again: /* jump here after setting a variable with ${var=text} */
|
|
|
|
if (special) {
|
|
|
|
set = varisset(*var);
|
|
|
|
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;
|
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
varlen = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
startloc = expdest - stackblock();
|
|
|
|
if (set && subtype != VSPLUS) {
|
|
|
|
/* insert the value of the variable */
|
|
|
|
if (special) {
|
1995-01-23 09:33:03 +03:00
|
|
|
char *exp, *oexpdest = expdest;
|
1994-05-11 21:09:42 +04:00
|
|
|
varvalue(*var, varflags & VSQUOTE, flag & EXP_FULL);
|
1995-01-23 09:33:03 +03:00
|
|
|
if (subtype == VSLENGTH) {
|
|
|
|
for (exp = oexpdest;exp != expdest; exp++)
|
|
|
|
varlen++;
|
|
|
|
expdest = oexpdest;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
1995-01-23 09:33:03 +03:00
|
|
|
char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
|
|
|
|
: BASESYNTAX;
|
|
|
|
|
|
|
|
if (subtype == VSLENGTH) {
|
|
|
|
for (;*val; val++)
|
|
|
|
varlen++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (*val) {
|
|
|
|
if (quotes && syntax[*val] == CCTL)
|
|
|
|
STPUTC(CTLESC, expdest);
|
|
|
|
STPUTC(*val++, expdest);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (subtype == VSPLUS)
|
|
|
|
set = ! set;
|
1995-01-23 09:33:03 +03:00
|
|
|
|
|
|
|
easy = ((varflags & VSQUOTE) == 0 ||
|
|
|
|
(*var == '@' && shellparam.nparam != 1));
|
|
|
|
|
|
|
|
|
|
|
|
switch (subtype) {
|
|
|
|
case VSLENGTH:
|
|
|
|
expdest = cvtnum(varlen, expdest);
|
|
|
|
goto record;
|
|
|
|
|
|
|
|
case VSNORMAL:
|
|
|
|
if (!easy)
|
|
|
|
break;
|
|
|
|
record:
|
|
|
|
recordregion(startloc, expdest - stackblock(),
|
|
|
|
varflags & VSQUOTE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VSPLUS:
|
|
|
|
case VSMINUS:
|
|
|
|
if (!set) {
|
1994-05-11 21:09:42 +04:00
|
|
|
argstr(p, flag);
|
1995-01-23 09:33:03 +03: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);
|
|
|
|
pat = expdest;
|
|
|
|
if (subevalvar(p, pat, subtype, startloc, varflags))
|
|
|
|
goto record;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VSASSIGN:
|
|
|
|
case VSQUESTION:
|
|
|
|
if (!set) {
|
|
|
|
if (subevalvar(p, var, subtype, startloc, varflags))
|
1993-03-21 12:45:37 +03:00
|
|
|
goto again;
|
1995-01-23 09:33:03 +03:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
if (easy)
|
|
|
|
goto record;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
|
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
|
|
|
|
varisset(name)
|
|
|
|
char name;
|
|
|
|
{
|
|
|
|
char **ap;
|
|
|
|
|
|
|
|
if (name == '!') {
|
|
|
|
if (backgndpid == -1)
|
|
|
|
return 0;
|
|
|
|
} else if (name == '@' || name == '*') {
|
|
|
|
if (*shellparam.p == NULL)
|
|
|
|
return 0;
|
|
|
|
} else if ((unsigned)(name -= '1') <= '9' - '1') {
|
|
|
|
ap = shellparam.p;
|
|
|
|
do {
|
|
|
|
if (*ap++ == NULL)
|
|
|
|
return 0;
|
|
|
|
} while (--name >= 0);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the value of a specialized variable to the stack string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
|
|
|
varvalue(name, quoted, allow_split)
|
|
|
|
char name;
|
1994-12-04 10:11:37 +03:00
|
|
|
int quoted;
|
|
|
|
int allow_split;
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int num;
|
|
|
|
char *p;
|
|
|
|
int i;
|
1995-05-15 06:47:38 +04:00
|
|
|
extern int oexitstatus;
|
1993-03-21 12:45:37 +03:00
|
|
|
char sep;
|
|
|
|
char **ap;
|
|
|
|
char const *syntax;
|
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
#define STRTODEST(p) \
|
|
|
|
do {\
|
|
|
|
if (allow_split) { \
|
|
|
|
syntax = quoted? DQSYNTAX : BASESYNTAX; \
|
|
|
|
while (*p) { \
|
|
|
|
if (syntax[*p] == CCTL) \
|
|
|
|
STPUTC(CTLESC, expdest); \
|
|
|
|
STPUTC(*p++, expdest); \
|
|
|
|
} \
|
|
|
|
} else \
|
|
|
|
while (*p) \
|
|
|
|
STPUTC(*p++, expdest); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (name) {
|
|
|
|
case '$':
|
|
|
|
num = rootpid;
|
|
|
|
goto numvar;
|
|
|
|
case '?':
|
1995-05-15 06:47:38 +04:00
|
|
|
num = oexitstatus;
|
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 '-':
|
1994-05-11 21:09:42 +04:00
|
|
|
for (i = 0 ; i < NOPTS ; i++) {
|
|
|
|
if (optlist[i].val)
|
|
|
|
STPUTC(optlist[i].letter, expdest);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '@':
|
|
|
|
if (allow_split) {
|
|
|
|
sep = '\0';
|
|
|
|
goto allargs;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
case '*':
|
|
|
|
sep = ' ';
|
|
|
|
allargs:
|
|
|
|
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
|
1994-05-11 21:09:42 +04:00
|
|
|
STRTODEST(p);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*ap)
|
|
|
|
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:
|
|
|
|
if ((unsigned)(name -= '1') <= '9' - '1') {
|
|
|
|
p = shellparam.p[name];
|
1994-05-11 21:09:42 +04:00
|
|
|
STRTODEST(p);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record the the fact that we have to scan this region of the
|
|
|
|
* string for IFS characters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC void
|
1994-12-04 10:11:37 +03:00
|
|
|
recordregion(start, end, nulonly)
|
|
|
|
int start;
|
|
|
|
int end;
|
|
|
|
int nulonly;
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
register struct ifsregion *ifsp;
|
|
|
|
|
|
|
|
if (ifslastp == NULL) {
|
|
|
|
ifsp = &ifsfirst;
|
|
|
|
} else {
|
|
|
|
ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
|
|
|
|
ifslastp->next = ifsp;
|
|
|
|
}
|
|
|
|
ifslastp = ifsp;
|
|
|
|
ifslastp->next = NULL;
|
|
|
|
ifslastp->begoff = start;
|
|
|
|
ifslastp->endoff = end;
|
|
|
|
ifslastp->nulonly = nulonly;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
ifsbreakup(string, arglist)
|
|
|
|
char *string;
|
|
|
|
struct arglist *arglist;
|
|
|
|
{
|
|
|
|
struct ifsregion *ifsp;
|
|
|
|
struct strlist *sp;
|
|
|
|
char *start;
|
|
|
|
register char *p;
|
|
|
|
char *q;
|
|
|
|
char *ifs;
|
1995-03-01 01:46:12 +03:00
|
|
|
int ifsspc;
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
start = string;
|
|
|
|
if (ifslastp != NULL) {
|
|
|
|
ifsp = &ifsfirst;
|
|
|
|
do {
|
|
|
|
p = string + ifsp->begoff;
|
|
|
|
ifs = ifsp->nulonly? nullstr : ifsval();
|
1995-03-01 01:46:12 +03:00
|
|
|
ifsspc = strchr(ifs, ' ') != NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
while (p < string + ifsp->endoff) {
|
|
|
|
q = p;
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
if (strchr(ifs, *p++)) {
|
1995-03-01 02:27:24 +03:00
|
|
|
if (q > start || !ifsspc) {
|
1993-03-21 12:45:37 +03:00
|
|
|
*q = '\0';
|
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = start;
|
|
|
|
*arglist->lastp = sp;
|
|
|
|
arglist->lastp = &sp->next;
|
|
|
|
}
|
1995-03-01 01:46:12 +03:00
|
|
|
if (ifsspc) {
|
1993-03-21 12:45:37 +03:00
|
|
|
for (;;) {
|
|
|
|
if (p >= string + ifsp->endoff)
|
|
|
|
break;
|
|
|
|
q = p;
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
if (strchr(ifs, *p++) == NULL) {
|
|
|
|
p = q;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ((ifsp = ifsp->next) != NULL);
|
1995-03-01 01:46:12 +03:00
|
|
|
if (*start || (!ifsspc && start > string)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = start;
|
|
|
|
*arglist->lastp = sp;
|
|
|
|
arglist->lastp = &sp->next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sp = (struct strlist *)stalloc(sizeof *sp);
|
|
|
|
sp->text = start;
|
|
|
|
*arglist->lastp = sp;
|
|
|
|
arglist->lastp = &sp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
1994-05-11 21:09:42 +04:00
|
|
|
expandmeta(str, flag)
|
1993-03-21 12:45:37 +03:00
|
|
|
struct strlist *str;
|
1994-12-04 10:11:37 +03:00
|
|
|
int flag;
|
|
|
|
{
|
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) {
|
1994-05-11 21:09:42 +04:00
|
|
|
/*
|
|
|
|
* no matches
|
|
|
|
*/
|
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
|
|
|
|
expmeta(enddir, name)
|
|
|
|
char *enddir;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
register char *p;
|
|
|
|
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 (;;) {
|
|
|
|
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;
|
|
|
|
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++) {
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*enddir++ = *p;
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (metaflag == 0 || stat(expdir, &statb) >= 0)
|
|
|
|
addfname(expdir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
endname = p;
|
|
|
|
if (start != name) {
|
|
|
|
p = name;
|
|
|
|
while (p < start) {
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*enddir++ = *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
*endname++ = '\0';
|
|
|
|
}
|
|
|
|
matchdot = 0;
|
1995-05-12 01:28:33 +04:00
|
|
|
if (start[0] == '.' || (start[0] == CTLESC && start[1] == '.'))
|
1993-03-21 12:45:37 +03:00
|
|
|
matchdot++;
|
|
|
|
while (! int_pending() && (dp = readdir(dirp)) != NULL) {
|
|
|
|
if (dp->d_name[0] == '.' && ! matchdot)
|
|
|
|
continue;
|
|
|
|
if (patmatch(start, dp->d_name)) {
|
|
|
|
if (atend) {
|
|
|
|
scopy(dp->d_name, enddir);
|
|
|
|
addfname(expdir);
|
|
|
|
} else {
|
|
|
|
char *q;
|
1995-05-12 01:28:33 +04:00
|
|
|
for (p = enddir, q = dp->d_name;
|
|
|
|
(*p++ = *q++) != '\0';)
|
|
|
|
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
|
|
|
|
addfname(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
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 *
|
|
|
|
expsort(str)
|
|
|
|
struct strlist *str;
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
struct strlist *sp;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
for (sp = str ; sp ; sp = sp->next)
|
|
|
|
len++;
|
|
|
|
return msort(str, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STATIC struct strlist *
|
|
|
|
msort(list, len)
|
|
|
|
struct strlist *list;
|
1994-12-04 10:11:37 +03:00
|
|
|
int len;
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
struct strlist *p, *q;
|
|
|
|
struct strlist **lpp;
|
|
|
|
int half;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (len <= 1)
|
|
|
|
return list;
|
|
|
|
half = len >> 1;
|
|
|
|
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
|
|
|
|
patmatch(pattern, string)
|
|
|
|
char *pattern;
|
|
|
|
char *string;
|
|
|
|
{
|
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
|
1993-03-21 12:45:37 +03:00
|
|
|
return pmatch(pattern, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STATIC int
|
|
|
|
pmatch(pattern, string)
|
|
|
|
char *pattern;
|
|
|
|
char *string;
|
|
|
|
{
|
|
|
|
register char *p, *q;
|
|
|
|
register char c;
|
|
|
|
|
|
|
|
p = pattern;
|
|
|
|
q = string;
|
|
|
|
for (;;) {
|
|
|
|
switch (c = *p++) {
|
|
|
|
case '\0':
|
|
|
|
goto breakloop;
|
|
|
|
case CTLESC:
|
|
|
|
if (*q++ != *p++)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
if (*q++ == '\0')
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
c = *p;
|
|
|
|
if (c != CTLESC && c != '?' && c != '*' && c != '[') {
|
|
|
|
while (*q != c) {
|
|
|
|
if (*q == '\0')
|
|
|
|
return 0;
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (pmatch(p, q))
|
|
|
|
return 1;
|
|
|
|
} while (*q++ != '\0');
|
|
|
|
return 0;
|
|
|
|
case '[': {
|
|
|
|
char *endp;
|
|
|
|
int invert, found;
|
|
|
|
char chr;
|
|
|
|
|
|
|
|
endp = p;
|
|
|
|
if (*endp == '!')
|
|
|
|
endp++;
|
|
|
|
for (;;) {
|
|
|
|
if (*endp == '\0')
|
|
|
|
goto dft; /* no matching ] */
|
|
|
|
if (*endp == CTLESC)
|
|
|
|
endp++;
|
|
|
|
if (*++endp == ']')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
invert = 0;
|
|
|
|
if (*p == '!') {
|
|
|
|
invert++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
found = 0;
|
|
|
|
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 {
|
|
|
|
if (c == CTLESC)
|
|
|
|
c = *p++;
|
|
|
|
if (*p == '-' && p[1] != ']') {
|
|
|
|
p++;
|
|
|
|
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:
|
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
|
|
|
|
rmescapes(str)
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
register char *p, *q;
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
while (*p != CTLESC) {
|
|
|
|
if (*p++ == '\0')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
q = p;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == CTLESC)
|
|
|
|
p++;
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if a pattern matches in a case statement.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
casematch(pattern, val)
|
|
|
|
union node *pattern;
|
|
|
|
char *val;
|
|
|
|
{
|
|
|
|
struct stackmark smark;
|
|
|
|
int result;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
setstackmark(&smark);
|
|
|
|
argbackq = pattern->narg.backquote;
|
|
|
|
STARTSTACKSTR(expdest);
|
|
|
|
ifslastp = NULL;
|
1994-05-11 21:09:42 +04:00
|
|
|
argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
|
1993-03-21 12:45:37 +03:00
|
|
|
STPUTC('\0', expdest);
|
|
|
|
p = grabstackstr(expdest);
|
|
|
|
result = patmatch(p, val);
|
|
|
|
popstackmark(&smark);
|
|
|
|
return result;
|
|
|
|
}
|
1995-01-23 09:33:03 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Our own itoa().
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC char *
|
|
|
|
cvtnum(num, buf)
|
|
|
|
int num;
|
|
|
|
char *buf;
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|