NetBSD/bin/csh/glob.c

928 lines
19 KiB
C
Raw Normal View History

2001-11-03 16:35:39 +03:00
/* $NetBSD: glob.c,v 1.19 2001/11/03 13:35:39 lukem Exp $ */
1995-03-21 12:01:59 +03:00
1993-03-21 12:45:37 +03:00
/*-
1994-09-21 04:10:23 +04:00
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1995-03-21 12:01:59 +03:00
#if 0
static char sccsid[] = "@(#)glob.c 8.1 (Berkeley) 5/31/93";
#else
2001-11-03 16:35:39 +03:00
__RCSID("$NetBSD: glob.c,v 1.19 2001/11/03 13:35:39 lukem Exp $");
1995-03-21 12:01:59 +03:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
1993-03-21 12:45:37 +03:00
#include <errno.h>
#include <glob.h>
1993-03-21 12:45:37 +03:00
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#if __STDC__
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "csh.h"
#include "extern.h"
1994-09-21 04:10:23 +04:00
static int noglob;
static int gargsiz, pargsiz;
1993-03-21 12:45:37 +03:00
/*
* Values for gflag
*/
#define G_NONE 0 /* No globbing needed */
#define G_GLOB 1 /* string contains *?[] characters */
#define G_CSH 2 /* string contains ~`{ characters */
1993-03-21 12:45:37 +03:00
#define GLOBSPACE 100 /* Alloc increment */
1993-03-21 12:45:37 +03:00
#define LBRC '{'
#define RBRC '}'
#define LBRK '['
#define RBRK ']'
#define EOS '\0'
Char **gargv = NULL;
Char **pargv = NULL;
long gargc = 0;
long pargc = 0;
1993-03-21 12:45:37 +03:00
/*
* globbing is now done in two stages. In the first pass we expand
* csh globbing idioms ~`{ and then we proceed doing the normal
* globbing if needed ?*[
*
* Csh type globbing is handled in globexpand() and the rest is
* handled in glob() which is part of the 4.4BSD libc.
*
*/
static Char *globtilde(Char **, Char *);
static Char *handleone(Char *, Char **, int);
static Char **libglob(Char **);
static Char **globexpand(Char **);
static int globbrace(Char *, Char *, Char ***);
static void expbrace(Char ***, Char ***, int);
static int pmatch(Char *, Char *);
static void pword(void);
static void psave(int);
static void backeval(Char *, bool);
1993-03-21 12:45:37 +03:00
static Char *
globtilde(Char **nv, Char *s)
1993-03-21 12:45:37 +03:00
{
Char gbuf[MAXPATHLEN], *b, *e, *gstart, *u;
1993-03-21 12:45:37 +03:00
gstart = gbuf;
*gstart++ = *s++;
u = s;
1994-09-21 04:10:23 +04:00
for (b = gstart, e = &gbuf[MAXPATHLEN - 1];
*s && *s != '/' && *s != ':' && b < e;
*b++ = *s++)
continue;
1993-03-21 12:45:37 +03:00
*b = EOS;
if (gethdir(gstart)) {
blkfree(nv);
if (*gstart)
1994-09-21 04:10:23 +04:00
stderror(ERR_UNKUSER, vis_str(gstart));
1993-03-21 12:45:37 +03:00
else
stderror(ERR_NOHOME);
}
b = &gstart[Strlen(gstart)];
while (*s)
*b++ = *s++;
*b = EOS;
--u;
xfree((ptr_t) u);
return (Strsave(gstart));
}
static int
globbrace(Char *s, Char *p, Char ***bl)
1993-03-21 12:45:37 +03:00
{
Char gbuf[MAXPATHLEN];
Char *lm, *pe, *pl, *pm, **nv, **vl;
int i, len, size;
1993-03-21 12:45:37 +03:00
size = GLOBSPACE;
nv = vl = (Char **)xmalloc((size_t) sizeof(Char *) * size);
1993-03-21 12:45:37 +03:00
*vl = NULL;
len = 0;
/* copy part up to the brace */
for (lm = gbuf, p = s; *p != LBRC; *lm++ = *p++)
continue;
/* check for balanced braces */
for (i = 0, pe = ++p; *pe; pe++)
if (*pe == LBRK) {
/* Ignore everything between [] */
for (++pe; *pe != RBRK && *pe != EOS; pe++)
continue;
if (*pe == EOS) {
blkfree(nv);
1994-09-21 04:10:23 +04:00
return (-RBRK);
1993-03-21 12:45:37 +03:00
}
}
else if (*pe == LBRC)
i++;
else if (*pe == RBRC) {
if (i == 0)
break;
i--;
}
if (i != 0 || *pe == '\0') {
1993-03-21 12:45:37 +03:00
blkfree(nv);
return (-RBRC);
1993-03-21 12:45:37 +03:00
}
for (i = 0, pl = pm = p; pm <= pe; pm++)
switch (*pm) {
case LBRK:
for (++pm; *pm != RBRK && *pm != EOS; pm++)
continue;
if (*pm == EOS) {
*vl = NULL;
blkfree(nv);
return (-RBRK);
}
break;
case LBRC:
i++;
break;
case RBRC:
if (i) {
i--;
break;
}
/* FALLTHROUGH */
case ',':
if (i && *pm == ',')
break;
else {
Char savec = *pm;
*pm = EOS;
(void)Strcpy(lm, pl);
(void)Strcat(gbuf, pe + 1);
1993-03-21 12:45:37 +03:00
*pm = savec;
*vl++ = Strsave(gbuf);
len++;
pl = pm + 1;
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **)xrealloc((ptr_t) nv,
(size_t)size * sizeof(Char *));
1993-03-21 12:45:37 +03:00
vl = &nv[size - GLOBSPACE];
}
}
break;
1994-09-21 04:10:23 +04:00
default:
break;
1993-03-21 12:45:37 +03:00
}
*vl = NULL;
*bl = nv;
return (len);
}
1994-09-21 04:10:23 +04:00
static void
expbrace(Char ***nvp, Char ***elp, int size)
1994-09-21 04:10:23 +04:00
{
Char **el, **nv, *s, **vl;
1994-09-21 04:10:23 +04:00
vl = nv = *nvp;
if (elp != NULL)
el = *elp;
else
for (el = vl; *el; el++)
continue;
for (s = *vl; s; s = *++vl) {
Char *b, **bp, **vp;
1994-09-21 04:10:23 +04:00
/* leave {} untouched for find */
if (s[0] == '{' && (s[1] == '\0' || (s[1] == '}' && s[2] == '\0')))
continue;
if ((b = Strchr(s, '{')) != NULL) {
Char **bl;
int len;
1994-09-21 04:10:23 +04:00
if ((len = globbrace(s, b, &bl)) < 0) {
xfree((ptr_t)nv);
1994-09-21 04:10:23 +04:00
stderror(ERR_MISSING, -len);
}
xfree((ptr_t) s);
if (len == 1) {
*vl-- = *bl;
xfree((ptr_t) bl);
continue;
}
len = blklen(bl);
if (&el[len] >= &nv[size]) {
int e, l;
1994-09-21 04:10:23 +04:00
l = &el[len] - &nv[size];
size += GLOBSPACE > l ? GLOBSPACE : l;
l = vl - nv;
e = el - nv;
nv = (Char **)xrealloc((ptr_t)nv,
(size_t)size * sizeof(Char *));
1994-09-21 04:10:23 +04:00
vl = nv + l;
el = nv + e;
}
vp = vl--;
*vp = *bl;
len--;
for (bp = el; bp != vp; bp--)
bp[len] = *bp;
el += len;
vp++;
for (bp = bl + 1; *bp; *vp++ = *bp++)
continue;
xfree((ptr_t)bl);
1994-09-21 04:10:23 +04:00
}
}
if (elp != NULL)
*elp = el;
*nvp = nv;
}
1993-03-21 12:45:37 +03:00
static Char **
globexpand(Char **v)
1993-03-21 12:45:37 +03:00
{
Char **el, **nv, *s, **vl;
int size;
1993-03-21 12:45:37 +03:00
size = GLOBSPACE;
nv = vl = (Char **)xmalloc((size_t)sizeof(Char *) * size);
1993-03-21 12:45:37 +03:00
*vl = NULL;
/*
* Step 1: expand backquotes.
*/
1994-09-21 04:10:23 +04:00
while ((s = *v++) != NULL) {
1993-03-21 12:45:37 +03:00
if (Strchr(s, '`')) {
int i;
1993-03-21 12:45:37 +03:00
(void) dobackp(s, 0);
for (i = 0; i < pargc; i++) {
*vl++ = pargv[i];
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **)xrealloc((ptr_t) nv,
(size_t)size * sizeof(Char *));
1993-03-21 12:45:37 +03:00
vl = &nv[size - GLOBSPACE];
}
}
xfree((ptr_t)pargv);
1993-03-21 12:45:37 +03:00
pargv = NULL;
}
else {
*vl++ = Strsave(s);
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **)xrealloc((ptr_t)nv,
(size_t)size * sizeof(Char *));
1993-03-21 12:45:37 +03:00
vl = &nv[size - GLOBSPACE];
}
}
}
*vl = NULL;
if (noglob)
return (nv);
/*
* Step 2: expand braces
*/
el = vl;
1994-09-21 04:10:23 +04:00
expbrace(&nv, &el, size);
1993-03-21 12:45:37 +03:00
/*
* Step 3: expand ~
*/
vl = nv;
for (s = *vl; s; s = *++vl)
if (*s == '~')
*vl = globtilde(nv, s);
vl = nv;
return (vl);
}
static Char *
handleone(Char *str, Char **vl, int action)
1993-03-21 12:45:37 +03:00
{
Char *cp, **vlp;
1993-03-21 12:45:37 +03:00
vlp = vl;
1993-03-21 12:45:37 +03:00
switch (action) {
case G_ERROR:
1994-09-21 04:10:23 +04:00
setname(vis_str(str));
1993-03-21 12:45:37 +03:00
blkfree(vl);
stderror(ERR_NAME | ERR_AMBIG);
1998-07-28 06:23:37 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
case G_APPEND:
trim(vlp);
str = Strsave(*vlp++);
do {
cp = Strspl(str, STRspace);
xfree((ptr_t)str);
1993-03-21 12:45:37 +03:00
str = Strspl(cp, *vlp);
xfree((ptr_t)cp);
1993-03-21 12:45:37 +03:00
}
while (*++vlp);
blkfree(vl);
break;
case G_IGNORE:
str = Strsave(strip(*vlp));
blkfree(vl);
break;
1994-09-21 04:10:23 +04:00
default:
break;
1993-03-21 12:45:37 +03:00
}
return (str);
}
static Char **
libglob(Char **vl)
1993-03-21 12:45:37 +03:00
{
glob_t globv;
char *ptr;
int gflgs, magic, match, nonomatch;
gflgs = GLOB_NOMAGIC;
magic = 0;
match = 0;
nonomatch = adrof(STRnonomatch) != 0;
1994-09-21 04:10:23 +04:00
if (!vl || !vl[0])
return (vl);
1993-03-21 12:45:37 +03:00
globv.gl_offs = 0;
globv.gl_pathv = 0;
globv.gl_pathc = 0;
1994-09-21 04:10:23 +04:00
if (nonomatch)
gflgs |= GLOB_NOCHECK;
1993-03-21 12:45:37 +03:00
do {
ptr = short2qstr(*vl);
switch (glob(ptr, gflgs, 0, &globv)) {
1998-04-01 00:45:25 +04:00
case GLOB_ABORTED:
1994-09-21 04:10:23 +04:00
setname(vis_str(*vl));
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_GLOB);
/* NOTREACHED */
case GLOB_NOSPACE:
stderror(ERR_NOMEM);
/* NOTREACHED */
default:
break;
}
1994-09-21 04:10:23 +04:00
if (globv.gl_flags & GLOB_MAGCHAR) {
match |= (globv.gl_matchc != 0);
magic = 1;
}
1993-03-21 12:45:37 +03:00
gflgs |= GLOB_APPEND;
}
while (*++vl);
1994-09-21 04:10:23 +04:00
vl = (globv.gl_pathc == 0 || (magic && !match && !nonomatch)) ?
NULL : blk2short(globv.gl_pathv);
1993-03-21 12:45:37 +03:00
globfree(&globv);
return (vl);
}
Char *
globone(Char *str, int action)
1993-03-21 12:45:37 +03:00
{
Char *v[2], **vl, **vo;
int gflg;
1993-03-21 12:45:37 +03:00
noglob = adrof(STRnoglob) != 0;
gflag = 0;
v[0] = str;
v[1] = 0;
tglob(v);
1994-09-21 04:10:23 +04:00
gflg = gflag;
if (gflg == G_NONE)
1993-03-21 12:45:37 +03:00
return (strip(Strsave(str)));
1994-09-21 04:10:23 +04:00
if (gflg & G_CSH) {
1993-03-21 12:45:37 +03:00
/*
* Expand back-quote, tilde and brace
*/
vo = globexpand(v);
1994-09-21 04:10:23 +04:00
if (noglob || (gflg & G_GLOB) == 0) {
1993-03-21 12:45:37 +03:00
if (vo[0] == NULL) {
xfree((ptr_t)vo);
1993-03-21 12:45:37 +03:00
return (Strsave(STRNULL));
}
if (vo[1] != NULL)
return (handleone(str, vo, action));
else {
str = strip(vo[0]);
xfree((ptr_t) vo);
return (str);
}
}
}
1994-09-21 04:10:23 +04:00
else if (noglob || (gflg & G_GLOB) == 0)
1993-03-21 12:45:37 +03:00
return (strip(Strsave(str)));
else
vo = v;
vl = libglob(vo);
1994-09-21 04:10:23 +04:00
if ((gflg & G_CSH) && vl != vo)
1993-03-21 12:45:37 +03:00
blkfree(vo);
if (vl == NULL) {
1994-09-21 04:10:23 +04:00
setname(vis_str(str));
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_NOMATCH);
}
if (vl[0] == NULL) {
xfree((ptr_t)vl);
1993-03-21 12:45:37 +03:00
return (Strsave(STRNULL));
}
if (vl[1] != NULL)
return (handleone(str, vl, action));
else {
str = strip(*vl);
xfree((ptr_t)vl);
1993-03-21 12:45:37 +03:00
return (str);
}
}
Char **
globall(Char **v)
1993-03-21 12:45:37 +03:00
{
Char **vl, **vo;
int gflg;
1993-03-21 12:45:37 +03:00
gflg = gflag;
1993-03-21 12:45:37 +03:00
if (!v || !v[0]) {
gargv = saveblk(v);
gargc = blklen(gargv);
return (gargv);
}
noglob = adrof(STRnoglob) != 0;
1994-09-21 04:10:23 +04:00
if (gflg & G_CSH)
1993-03-21 12:45:37 +03:00
/*
* Expand back-quote, tilde and brace
*/
vl = vo = globexpand(v);
else
vl = vo = saveblk(v);
if (!noglob && (gflg & G_GLOB)) {
1993-03-21 12:45:37 +03:00
vl = libglob(vo);
1994-09-21 04:10:23 +04:00
if ((gflg & G_CSH) && vl != vo)
1993-03-21 12:45:37 +03:00
blkfree(vo);
}
1994-09-21 04:10:23 +04:00
else
trim(vl);
1993-03-21 12:45:37 +03:00
gargc = vl ? blklen(vl) : 0;
return (gargv = vl);
}
void
ginit(void)
1993-03-21 12:45:37 +03:00
{
gargsiz = GLOBSPACE;
gargv = (Char **)xmalloc((size_t)sizeof(Char *) * gargsiz);
1993-03-21 12:45:37 +03:00
gargv[0] = 0;
gargc = 0;
}
void
rscan(Char **t, void (*f)(int))
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *p;
1993-03-21 12:45:37 +03:00
1994-09-21 04:10:23 +04:00
while ((p = *t++) != NULL)
1993-03-21 12:45:37 +03:00
while (*p)
(*f) (*p++);
}
void
trim(Char **t)
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *p;
1993-03-21 12:45:37 +03:00
1994-09-21 04:10:23 +04:00
while ((p = *t++) != NULL)
1993-03-21 12:45:37 +03:00
while (*p)
*p++ &= TRIM;
}
void
tglob(Char **t)
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *p, c;
1993-03-21 12:45:37 +03:00
1994-09-21 04:10:23 +04:00
while ((p = *t++) != NULL) {
1993-03-21 12:45:37 +03:00
if (*p == '~' || *p == '=')
gflag |= G_CSH;
else if (*p == '{' &&
1994-09-21 04:10:23 +04:00
(p[1] == '\0' || (p[1] == '}' && p[2] == '\0')))
1993-03-21 12:45:37 +03:00
continue;
1994-09-21 04:10:23 +04:00
while ((c = *p++) != '\0') {
/*
* eat everything inside the matching backquotes
*/
if (c == '`') {
gflag |= G_CSH;
while (*p && *p != '`')
if (*p++ == '\\') {
if (*p) /* Quoted chars */
p++;
else
break;
}
if (*p) /* The matching ` */
p++;
else
break;
}
else if (c == '{')
gflag |= G_CSH;
else if (isglob(c))
gflag |= G_GLOB;
}
1993-03-21 12:45:37 +03:00
}
}
/*
* Command substitute cp. If literal, then this is a substitution from a
* << redirection, and so we should not crunch blanks and tabs, separating
* words only at newlines.
*/
Char **
dobackp(Char *cp, bool literal)
1993-03-21 12:45:37 +03:00
{
Char word[MAXPATHLEN], *ep, *lp, *rp;
1993-03-21 12:45:37 +03:00
if (pargv) {
1994-09-21 04:10:23 +04:00
#ifdef notdef
1993-03-21 12:45:37 +03:00
abort();
1994-09-21 04:10:23 +04:00
#endif
1993-03-21 12:45:37 +03:00
blkfree(pargv);
}
pargsiz = GLOBSPACE;
pargv = (Char **)xmalloc((size_t)sizeof(Char *) * pargsiz);
1993-03-21 12:45:37 +03:00
pargv[0] = NULL;
pargcp = pargs = word;
pargc = 0;
pnleft = MAXPATHLEN - 4;
for (;;) {
for (lp = cp; *lp != '`'; lp++) {
if (*lp == 0) {
if (pargcp != pargs)
pword();
return (pargv);
}
psave(*lp);
}
lp++;
for (rp = lp; *rp && *rp != '`'; rp++)
if (*rp == '\\') {
rp++;
if (!*rp)
goto oops;
}
1998-07-28 06:23:37 +04:00
if (!*rp) {
oops:
stderror(ERR_UNMATCHED, '`');
}
1993-03-21 12:45:37 +03:00
ep = Strsave(lp);
ep[rp - lp] = 0;
backeval(ep, literal);
cp = rp + 1;
}
}
static void
backeval(Char *cp, bool literal)
1993-03-21 12:45:37 +03:00
{
struct command faket;
char tibuf[BUFSIZE];
Char ibuf[BUFSIZE], *fakecom[2], *ip;
int pvec[2], c, icnt, quoted;
bool hadnl;
1993-03-21 12:45:37 +03:00
hadnl = 0;
icnt = 0;
quoted = (literal || (cp[0] & QUOTE)) ? QUOTE : 0;
faket.t_dtyp = NODE_COMMAND;
faket.t_dflg = 0;
faket.t_dlef = 0;
faket.t_drit = 0;
faket.t_dspr = 0;
faket.t_dcom = fakecom;
fakecom[0] = STRfakecom1;
fakecom[1] = 0;
/*
* We do the psave job to temporarily change the current job so that the
* following fork is considered a separate job. This is so that when
* backquotes are used in a builtin function that calls glob the "current
* job" is not corrupted. We only need one level of pushed jobs as long as
* we are sure to fork here.
*/
psavejob();
/*
* It would be nicer if we could integrate this redirection more with the
* routines in sh.sem.c by doing a fake execute on a builtin function that
* was piped out.
*/
mypipe(pvec);
if (pfork(&faket, -1) == 0) {
2001-11-03 16:35:39 +03:00
struct wordent fparaml;
1993-03-21 12:45:37 +03:00
struct command *t;
(void)close(pvec[0]);
(void)dmove(pvec[1], 1);
(void)dmove(SHERR, 2);
1993-03-21 12:45:37 +03:00
initdesc();
/*
* Bugfix for nested backquotes by Michael Greim <greim@sbsvax.UUCP>,
* posted to comp.bugs.4bsd 12 Sep. 1989.
*/
if (pargv) /* mg, 21.dec.88 */
blkfree(pargv), pargv = 0, pargsiz = 0;
/* mg, 21.dec.88 */
arginp = cp;
while (*cp)
*cp++ &= TRIM;
1994-09-21 04:10:23 +04:00
/*
* In the child ``forget'' everything about current aliases or
* eval vectors.
*/
alvec = NULL;
evalvec = NULL;
alvecp = NULL;
evalp = NULL;
2001-11-03 16:35:39 +03:00
(void) lex(&fparaml);
if (seterr)
1993-03-21 12:45:37 +03:00
stderror(ERR_OLD);
2001-11-03 16:35:39 +03:00
alias(&fparaml);
t = syntax(fparaml.next, &fparaml, 0);
if (seterr)
1993-03-21 12:45:37 +03:00
stderror(ERR_OLD);
if (t)
t->t_dflg |= F_NOFORK;
(void)signal(SIGTSTP, SIG_IGN);
(void)signal(SIGTTIN, SIG_IGN);
(void)signal(SIGTTOU, SIG_IGN);
1993-03-21 12:45:37 +03:00
execute(t, -1, NULL, NULL);
exitstat();
}
xfree((ptr_t)cp);
(void)close(pvec[1]);
1993-03-21 12:45:37 +03:00
c = 0;
ip = NULL;
do {
int cnt;
cnt = 0;
1993-03-21 12:45:37 +03:00
for (;;) {
if (icnt == 0) {
int i;
1993-03-21 12:45:37 +03:00
ip = ibuf;
do
icnt = read(pvec[0], tibuf, BUFSIZE);
1993-03-21 12:45:37 +03:00
while (icnt == -1 && errno == EINTR);
if (icnt <= 0) {
c = -1;
break;
}
for (i = 0; i < icnt; i++)
ip[i] = (unsigned char) tibuf[i];
}
if (hadnl)
break;
--icnt;
c = (*ip++ & TRIM);
if (c == 0)
break;
if (c == '\n') {
/*
* Continue around the loop one more time, so that we can eat
* the last newline without terminating this word.
*/
hadnl = 1;
continue;
}
if (!quoted && (c == ' ' || c == '\t'))
break;
cnt++;
psave(c | quoted);
}
/*
* Unless at end-of-file, we will form a new word here if there were
* characters in the word, or in any case when we take text literally.
* If we didn't make empty words here when literal was set then we
* would lose blank lines.
*/
if (c != -1 && (cnt || literal))
pword();
hadnl = 0;
} while (c >= 0);
(void)close(pvec[0]);
1993-03-21 12:45:37 +03:00
pwait();
prestjob();
}
static void
psave(int c)
1993-03-21 12:45:37 +03:00
{
if (--pnleft <= 0)
1993-03-21 12:45:37 +03:00
stderror(ERR_WTOOLONG);
*pargcp++ = c;
}
static void
pword(void)
1993-03-21 12:45:37 +03:00
{
psave(0);
if (pargc == pargsiz - 1) {
pargsiz += GLOBSPACE;
pargv = (Char **)xrealloc((ptr_t)pargv,
(size_t)pargsiz * sizeof(Char *));
1993-03-21 12:45:37 +03:00
}
pargv[pargc++] = Strsave(pargs);
pargv[pargc] = NULL;
pargcp = pargs;
pnleft = MAXPATHLEN - 4;
}
1994-09-21 04:10:23 +04:00
int
Gmatch(Char *string, Char *pattern)
1994-09-21 04:10:23 +04:00
{
Char **blk, **p;
int gpol, gres;
gpol = 1;
gres = 0;
1994-09-21 04:10:23 +04:00
if (*pattern == '^') {
gpol = 0;
pattern++;
}
blk = (Char **)xmalloc(GLOBSPACE * sizeof(Char *));
1994-09-21 04:10:23 +04:00
blk[0] = Strsave(pattern);
blk[1] = NULL;
expbrace(&blk, NULL, GLOBSPACE);
for (p = blk; *p; p++)
gres |= pmatch(string, *p);
blkfree(blk);
return(gres == gpol);
}
static int
pmatch(Char *string, Char *pattern)
1993-03-21 12:45:37 +03:00
{
int match, negate_range;
Char patternc, rangec, stringc;
1993-03-21 12:45:37 +03:00
for (;; ++string) {
stringc = *string & TRIM;
patternc = *pattern++;
switch (patternc) {
case 0:
return (stringc == 0);
case '?':
if (stringc == 0)
return (0);
break;
case '*':
if (!*pattern)
return (1);
while (*string)
if (Gmatch(string++, pattern))
return (1);
return (0);
case '[':
match = 0;
1994-09-21 04:10:23 +04:00
if ((negate_range = (*pattern == '^')) != 0)
pattern++;
while ((rangec = *pattern++) != '\0') {
1993-03-21 12:45:37 +03:00
if (rangec == ']')
1994-09-21 04:10:23 +04:00
break;
1993-03-21 12:45:37 +03:00
if (match)
continue;
1994-09-21 04:10:23 +04:00
if (rangec == '-' && *(pattern-2) != '[' && *pattern != ']') {
1993-03-21 12:45:37 +03:00
match = (stringc <= (*pattern & TRIM) &&
1994-09-21 04:10:23 +04:00
(*(pattern-2) & TRIM) <= stringc);
1993-03-21 12:45:37 +03:00
pattern++;
}
1994-09-21 04:10:23 +04:00
else
match = (stringc == (rangec & TRIM));
1993-03-21 12:45:37 +03:00
}
if (rangec == 0)
1993-03-21 12:45:37 +03:00
stderror(ERR_NAME | ERR_MISSING, ']');
1994-09-21 04:10:23 +04:00
if (match == negate_range)
return (0);
1993-03-21 12:45:37 +03:00
break;
default:
if ((patternc & TRIM) != stringc)
return (0);
break;
}
}
}
void
Gcat(Char *s1, Char *s2)
1993-03-21 12:45:37 +03:00
{
1997-01-13 20:53:15 +03:00
Char *p, *q;
int n;
1993-03-21 12:45:37 +03:00
1994-09-21 04:10:23 +04:00
for (p = s1; *p++;)
continue;
for (q = s2; *q++;)
continue;
1993-03-21 12:45:37 +03:00
n = (p - s1) + (q - s2) - 1;
if (++gargc >= gargsiz) {
gargsiz += GLOBSPACE;
gargv = (Char **)xrealloc((ptr_t)gargv,
(size_t)gargsiz * sizeof(Char *));
1993-03-21 12:45:37 +03:00
}
gargv[gargc] = 0;
p = gargv[gargc - 1] = (Char *)xmalloc((size_t)n * sizeof(Char));
1994-09-21 04:10:23 +04:00
for (q = s1; (*p++ = *q++) != '\0';)
continue;
for (p--, q = s2; (*p++ = *q++) != '\0';)
continue;
1993-03-21 12:45:37 +03:00
}
#ifdef FILEC
int
sortscmp(const ptr_t a, const ptr_t b)
1993-03-21 12:45:37 +03:00
{
#if defined(NLS) && !defined(NOSTRCOLL)
char buf[2048];
1993-03-21 12:45:37 +03:00
#endif
if (!a) /* check for NULL */
return (b ? 1 : 0);
if (!b)
return (-1);
1994-09-21 04:10:23 +04:00
if (!*(Char **)a) /* check for NULL */
return (*(Char **)b ? 1 : 0);
if (!*(Char **)b)
1993-03-21 12:45:37 +03:00
return (-1);
#if defined(NLS) && !defined(NOSTRCOLL)
(void)strcpy(buf, short2str(*(Char **)a));
return ((int)strcoll(buf, short2str(*(Char **)b)));
1993-03-21 12:45:37 +03:00
#else
return ((int)Strcmp(*(Char **)a, *(Char **)b));
1993-03-21 12:45:37 +03:00
#endif
}
#endif /* FILEC */