1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
|
/* $OpenBSD: glob.c,v 1.13 2009/10/27 23:59:21 deraadt Exp $ */
/* $NetBSD: glob.c,v 1.10 1995/03/21 09:03:01 cgd Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* 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. 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/param.h>
#include <glob.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <bsd/string.h>
#include <unistd.h>
#include <stdarg.h>
#include "csh.h"
#include "extern.h"
static int noglob;
static int pargsiz, gargsiz;
/*
* Values for gflag
*/
#define G_NONE 0 /* No globbing needed */
#define G_GLOB 1 /* string contains *?[] characters */
#define G_CSH 2 /* string contains ~`{ characters */
#define GLOBSPACE 100 /* Alloc increment */
#define LBRC '{'
#define RBRC '}'
#define LBRK '['
#define RBRK ']'
#define EOS '\0'
Char **gargv = NULL;
long gargc = 0;
Char **pargv = NULL;
long pargc = 0;
/*
* 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 **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);
static Char *
globtilde(Char **nv, Char *s)
{
Char gbuf[MAXPATHLEN], *gstart, *b, *u, *e;
gstart = gbuf;
*gstart++ = *s++;
u = s;
for (b = gstart, e = &gbuf[MAXPATHLEN - 1];
*s && *s != '/' && *s != ':' && b < e;
*b++ = *s++)
continue;
*b = EOS;
if (gethdir(gstart, &gbuf[sizeof(gbuf)/sizeof(Char)] - gstart)) {
blkfree(nv);
if (*gstart)
stderror(ERR_UNKUSER, vis_str(gstart));
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)
{
int i, len;
Char *pm, *pe, *lm, *pl;
Char **nv, **vl;
Char gbuf[MAXPATHLEN];
int size = GLOBSPACE;
nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
*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);
return (-RBRK);
}
}
else if (*pe == LBRC)
i++;
else if (*pe == RBRC) {
if (i == 0)
break;
i--;
}
if (i != 0 || *pe == '\0') {
blkfree(nv);
return (-RBRC);
}
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) Strlcpy(lm, pl, &gbuf[sizeof(gbuf)/sizeof(Char)] - lm);
(void) Strlcat(gbuf, pe + 1, MAXPATHLEN);
*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 *));
vl = &nv[size - GLOBSPACE];
}
}
break;
default:
break;
}
*vl = NULL;
*bl = nv;
return (len);
}
static void
expbrace(Char ***nvp, Char ***elp, int size)
{
Char **vl, **el, **nv, *s;
vl = nv = *nvp;
if (elp != NULL)
el = *elp;
else
for (el = vl; *el; el++)
continue;
for (s = *vl; s; s = *++vl) {
Char *b;
Char **vp, **bp;
/* 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;
if ((len = globbrace(s, b, &bl)) < 0) {
xfree((ptr_t) nv);
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 l, e;
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 *));
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);
}
}
if (elp != NULL)
*elp = el;
*nvp = nv;
}
static Char **
globexpand(Char **v)
{
Char *s;
Char **nv, **vl, **el;
int size = GLOBSPACE;
nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
*vl = NULL;
/*
* Step 1: expand backquotes.
*/
while ((s = *v++) != NULL) {
if (Strchr(s, '`')) {
int i;
(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 *));
vl = &nv[size - GLOBSPACE];
}
}
xfree((ptr_t) pargv);
pargv = NULL;
}
else {
*vl++ = Strsave(s);
if (vl == &nv[size]) {
size += GLOBSPACE;
nv = (Char **) xrealloc((ptr_t) nv, (size_t)
size * sizeof(Char *));
vl = &nv[size - GLOBSPACE];
}
}
}
*vl = NULL;
if (noglob)
return (nv);
/*
* Step 2: expand braces
*/
el = vl;
expbrace(&nv, &el, size);
/*
* 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)
{
Char *cp, **vlp = vl;
switch (action) {
case G_ERROR:
setname(vis_str(str));
blkfree(vl);
stderror(ERR_NAME | ERR_AMBIG);
break;
case G_APPEND:
trim(vlp);
str = Strsave(*vlp++);
do {
cp = Strspl(str, STRspace);
xfree((ptr_t) str);
str = Strspl(cp, *vlp);
xfree((ptr_t) cp);
}
while (*++vlp)
;
blkfree(vl);
break;
case G_IGNORE:
str = Strsave(strip(*vlp));
blkfree(vl);
break;
default:
break;
}
return (str);
}
static Char **
libglob(Char **vl)
{
Char **nv, **nvp;
size_t size = GLOBSPACE;
glob_t globv;
int nonomatch = adrof(STRnonomatch) != 0, magic = 0, match = 0;
if (!vl || !vl[0])
return (vl);
nvp = nv = (Char **) xmalloc(sizeof(Char *) * size);
do {
int append; /* Matches to append in this round. */
Char *magicp;
/*
* The original code depends on GLOB_NOMAGIC and GLOB_MAGCHAR to
* distinguish patterns with magic characters. Both are unreliable
* with GNU glob() as it considers a pattern magical as soon as it
* finds backslashes (unlike BSD). tglob() already implements a
* similar check for the whole list, so do it on our own: make
* magicp point to the first magic character, or NUL if none.
*/
for (magicp = *vl; *magicp; magicp++)
if (isglob(*magicp))
break;
if (*magicp) {
/*
* Pattern is magic and we need to glob(). The original code
* calls glob() repeatedly on a single output buffer, relying on
* GLOB_NOMAGIC to append the original pattern if it didn't
* contain any magic characters, or GLOB_NOCHECK to do the same
* if nonomatch is in effect. Again, both are problematic with
* GNU glob() as it leaves backslashes unstripped in the output:
* <http://sourceware.org/bugzilla/show_bug.cgi?id=1342>. This
* may get fixed at some point, but for now we can't reliably
* append an unchanged pattern to an existing glob buffer, and
* must call glob() separately each time.
*/
char *ptr = short2qstr(*vl);
globv.gl_offs = 0;
globv.gl_pathv = 0;
globv.gl_pathc = 0;
switch (glob(ptr, 0, 0, &globv)) {
case GLOB_ABORTED:
setname(vis_str(*vl));
stderror(ERR_NAME | ERR_GLOB);
/* NOTREACHED */
case GLOB_NOSPACE:
stderror(ERR_NOMEM);
/* NOTREACHED */
default:
break;
}
/*
* If gl_pathc shows matches, append that many. Otherwise
* append the original pattern if nonomatch is in effect.
*/
append = globv.gl_pathc ? globv.gl_pathc : nonomatch;
match |= (append != 0); /* Did any magic patterns match? */
magic = 1; /* At least one should match. */
} else {
/*
* No magic characters, append the pattern by itself.
*/
append = 1;
}
if (append) {
if (nvp + append >= nv + size) {
size = (nvp - nv) + append + GLOBSPACE;
nv = xrealloc((ptr_t) nv, sizeof(Char *) * size);
nvp = nv + size - append - GLOBSPACE;
}
if (*magicp && globv.gl_pathc) {
char **gvp;
for (gvp = globv.gl_pathv; *gvp; gvp++)
*nvp++ = SAVE(*gvp);
} else {
*nvp++ = strip(Strsave(*vl));
}
}
if (*magicp)
globfree(&globv); /* Must free regardless of matches. */
}
while (*++vl)
;
if (nvp == nv || (magic && !match)) {
/*
* Output is empty, or none of the magic patterns matched.
* It's up to the caller to generate a "No match" error.
*/
xfree(nv);
return (NULL);
}
*nvp = NULL;
return (nv);
}
Char *
globone(Char *str, int action)
{
Char *v[2], **vl, **vo;
int gflg;
noglob = adrof(STRnoglob) != 0;
gflag = 0;
v[0] = str;
v[1] = 0;
tglob(v);
gflg = gflag;
if (gflg == G_NONE)
return (strip(Strsave(str)));
if (gflg & G_CSH) {
/*
* Expand back-quote, tilde and brace
*/
vo = globexpand(v);
if (noglob || (gflg & G_GLOB) == 0) {
if (vo[0] == NULL) {
xfree((ptr_t) vo);
return (Strsave(STRNULL));
}
if (vo[1] != NULL)
return (handleone(str, vo, action));
else {
str = strip(vo[0]);
xfree((ptr_t) vo);
return (str);
}
}
}
else if (noglob || (gflg & G_GLOB) == 0)
return (strip(Strsave(str)));
else
vo = v;
vl = libglob(vo);
if ((gflg & G_CSH) && vl != vo)
blkfree(vo);
if (vl == NULL) {
setname(vis_str(str));
stderror(ERR_NAME | ERR_NOMATCH);
}
if (vl[0] == NULL) {
xfree((ptr_t) vl);
return (Strsave(STRNULL));
}
if (vl[1] != NULL)
return (handleone(str, vl, action));
else {
str = strip(*vl);
xfree((ptr_t) vl);
return (str);
}
}
Char **
globall(Char **v)
{
Char **vl, **vo;
int gflg = gflag;
if (!v || !v[0]) {
gargv = saveblk(v);
gargc = blklen(gargv);
return (gargv);
}
noglob = adrof(STRnoglob) != 0;
if (gflg & G_CSH)
/*
* Expand back-quote, tilde and brace
*/
vl = vo = globexpand(v);
else
vl = vo = saveblk(v);
if (!noglob && (gflg & G_GLOB)) {
vl = libglob(vo);
if ((gflg & G_CSH) && vl != vo)
blkfree(vo);
}
else
trim(vl);
gargc = vl ? blklen(vl) : 0;
return (gargv = vl);
}
void
ginit(void)
{
gargsiz = GLOBSPACE;
gargv = (Char **) xmalloc((size_t) sizeof(Char *) * gargsiz);
gargv[0] = 0;
gargc = 0;
}
void
rscan(Char **t, void (*f)(int))
{
Char *p;
while ((p = *t++) != NULL)
while (*p)
(*f) (*p++);
}
void
trim(Char **t)
{
Char *p;
while ((p = *t++) != NULL)
while (*p)
*p++ &= TRIM;
}
void
tglob(Char **t)
{
Char *p, c;
while ((p = *t++) != NULL) {
if (*p == '~' || *p == '=')
gflag |= G_CSH;
else if (*p == '{' &&
(p[1] == '\0' || (p[1] == '}' && p[2] == '\0')))
continue;
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;
}
}
}
/*
* 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)
{
Char *lp, *rp;
Char *ep, word[MAXPATHLEN];
if (pargv) {
#ifdef notdef
abort();
#endif
blkfree(pargv);
}
pargsiz = GLOBSPACE;
pargv = (Char **) xmalloc((size_t) sizeof(Char *) * pargsiz);
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;
}
if (!*rp)
oops: stderror(ERR_UNMATCHED, '`');
ep = Strsave(lp);
ep[rp - lp] = 0;
backeval(ep, literal);
cp = rp + 1;
}
}
static void
backeval(Char *cp, bool literal)
{
int icnt, c;
Char *ip;
struct command faket;
bool hadnl;
int pvec[2], quoted;
Char *fakecom[2], ibuf[BUFSIZ];
char tibuf[BUFSIZ];
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) {
struct wordent paraml;
struct command *t;
(void) close(pvec[0]);
(void) dmove(pvec[1], 1);
(void) dmove(SHERR, 2);
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;
/*
* In the child ``forget'' everything about current aliases or
* eval vectors.
*/
alvec = NULL;
evalvec = NULL;
alvecp = NULL;
evalp = NULL;
(void) lex(¶ml);
if (seterr)
stderror(ERR_OLD);
alias(¶ml);
t = syntax(paraml.next, ¶ml, 0);
if (seterr)
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);
execute(t, -1, NULL, NULL);
exitstat();
}
xfree((ptr_t) cp);
(void) close(pvec[1]);
c = 0;
ip = NULL;
do {
int cnt = 0;
for (;;) {
if (icnt == 0) {
int i;
ip = ibuf;
do
icnt = read(pvec[0], tibuf, BUFSIZ);
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]);
pwait();
prestjob();
}
static void
psave(int c)
{
if (--pnleft <= 0)
stderror(ERR_WTOOLONG);
*pargcp++ = c;
}
static void
pword(void)
{
psave(0);
if (pargc == pargsiz - 1) {
pargsiz += GLOBSPACE;
pargv = (Char **) xrealloc((ptr_t) pargv,
(size_t) pargsiz * sizeof(Char *));
}
pargv[pargc++] = Strsave(pargs);
pargv[pargc] = NULL;
pargcp = pargs;
pnleft = MAXPATHLEN - 4;
}
int
Gmatch(Char *string, Char *pattern)
{
Char **blk, **p;
int gpol = 1, gres = 0;
if (*pattern == '^') {
gpol = 0;
pattern++;
}
blk = (Char **) xmalloc(GLOBSPACE * sizeof(Char *));
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)
{
Char stringc, patternc;
int match, negate_range;
Char rangec;
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;
if ((negate_range = (*pattern == '^')) != 0)
pattern++;
while ((rangec = *pattern++) != '\0') {
if (rangec == ']')
break;
if (match)
continue;
if (rangec == '-' && *(pattern-2) != '[' && *pattern != ']') {
match = (stringc <= (*pattern & TRIM) &&
(*(pattern-2) & TRIM) <= stringc);
pattern++;
}
else
match = (stringc == (rangec & TRIM));
}
if (rangec == 0)
stderror(ERR_NAME | ERR_MISSING, ']');
if (match == negate_range)
return (0);
break;
default:
if ((patternc & TRIM) != stringc)
return (0);
break;
}
}
}
void
Gcat(Char *s1, Char *s2)
{
Char *p, *q;
int n;
for (p = s1; *p++;)
continue;
for (q = s2; *q++;)
continue;
n = (p - s1) + (q - s2) - 1;
if (++gargc >= gargsiz) {
gargsiz += GLOBSPACE;
gargv = (Char **) xrealloc((ptr_t) gargv,
(size_t) gargsiz * sizeof(Char *));
}
gargv[gargc] = 0;
p = gargv[gargc - 1] = (Char *) xmalloc((size_t) n * sizeof(Char));
for (q = s1; (*p++ = *q++) != '\0';)
continue;
for (p--, q = s2; (*p++ = *q++) != '\0';)
continue;
}
#ifdef FILEC
int
sortscmp(ptr_t a, ptr_t b)
{
#if defined(NLS) && !defined(NOSTRCOLL)
char buf[2048];
#endif
if (!a) /* check for NULL */
return (b ? 1 : 0);
if (!b)
return (-1);
if (!*(Char **)a) /* check for NULL */
return (*(Char **)b ? 1 : 0);
if (!*(Char **)b)
return (-1);
#if defined(NLS) && !defined(NOSTRCOLL)
(void) strlcpy(buf, short2str(*(Char **)a), sizeof buf);
return ((int) strcoll(buf, short2str(*(Char **)b)));
#else
return ((int) Strcmp(*(Char **)a, *(Char **)b));
#endif
}
#endif /* FILEC */
|