Changeset 1203 for trunk/src/kash/expand.c
- Timestamp:
- Oct 7, 2007, 3:39:01 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/expand.c
r1202 r1203 78 78 #include "mystring.h" 79 79 #include "show.h" 80 81 /* 82 * Structure specifying which parts of the string should be searched 83 * for IFS characters. 84 */ 85 86 struct ifsregion { 87 struct ifsregion *next; /* next region in list */ 88 int begoff; /* offset of start of region */ 89 int endoff; /* offset of end of region */ 90 int inquotes; /* search for nul bytes only */ 91 }; 92 93 94 char *expdest; /* output of current string */ 95 struct nodelist *argbackq; /* list of back quote expressions */ 96 struct ifsregion ifsfirst; /* first struct in list of ifs regions */ 97 struct ifsregion *ifslastp; /* last struct in list */ 98 struct arglist exparg; /* holds expanded arg list */ 99 100 STATIC void argstr(char *, int); 101 STATIC char *exptilde(char *, int); 102 STATIC void expbackq(union node *, int, int); 103 STATIC int subevalvar(char *, char *, int, int, int, int); 104 STATIC char *evalvar(char *, int); 105 STATIC int varisset(char *, int); 106 STATIC void varvalue(char *, int, int, int); 107 STATIC void recordregion(int, int, int); 108 STATIC void removerecordregions(int); 109 STATIC void ifsbreakup(char *, struct arglist *); 110 STATIC void ifsfree(void); 111 STATIC void expandmeta(struct strlist *, int); 112 STATIC void expmeta(char *, char *); 113 STATIC void addfname(char *); 80 #include "shinstance.h" 81 82 ///* 83 // * Structure specifying which parts of the string should be searched 84 // * for IFS characters. 85 // */ 86 // 87 //struct ifsregion { 88 // struct ifsregion *next; /* next region in list */ 89 // int begoff; /* offset of start of region */ 90 // int endoff; /* offset of end of region */ 91 // int inquotes; /* search for nul bytes only */ 92 //}; 93 // 94 // 95 //char *expdest; /* output of current string */ 96 //struct nodelist *argbackq; /* list of back quote expressions */ 97 //struct ifsregion ifsfirst; /* first struct in list of ifs regions */ 98 //struct ifsregion *ifslastp; /* last struct in list */ 99 //struct arglist exparg; /* holds expanded arg list */ 100 101 STATIC void argstr(shinstance *, char *, int); 102 STATIC char *exptilde(shinstance *, char *, int); 103 STATIC void expbackq(shinstance *, union node *, int, int); 104 STATIC int subevalvar(shinstance *, char *, char *, int, int, int, int); 105 STATIC char *evalvar(shinstance *, char *, int); 106 STATIC int varisset(shinstance *, char *, int); 107 STATIC void varvalue(shinstance *, char *, int, int, int); 108 STATIC void recordregion(shinstance *, int, int, int); 109 STATIC void removerecordregions(shinstance *, int); 110 STATIC void ifsbreakup(shinstance *, char *, struct arglist *); 111 STATIC void ifsfree(shinstance *); 112 STATIC void expandmeta(shinstance *, struct strlist *, int); 113 STATIC void expmeta(shinstance *, char *, char *); 114 STATIC void addfname(shinstance *, char *); 114 115 STATIC struct strlist *expsort(struct strlist *); 115 116 STATIC struct strlist *msort(struct strlist *, int); 116 117 STATIC int pmatch(char *, char *, int); 117 STATIC char *cvtnum( int, char *);118 STATIC char *cvtnum(shinstance *, int, char *); 118 119 119 120 /* … … 138 139 139 140 void 140 expandarg( union node *arg, struct arglist *arglist, int flag)141 expandarg(shinstance *psh, union node *arg, struct arglist *arglist, int flag) 141 142 { 142 143 struct strlist *sp; 143 144 char *p; 144 145 145 argbackq = arg->narg.backquote;146 STARTSTACKSTR(psh, expdest);147 ifsfirst.next = NULL;148 ifslastp = NULL;149 argstr( arg->narg.text, flag);146 psh->argbackq = arg->narg.backquote; 147 STARTSTACKSTR(psh, psh->expdest); 148 psh->ifsfirst.next = NULL; 149 psh->ifslastp = NULL; 150 argstr(psh, arg->narg.text, flag); 150 151 if (arglist == NULL) { 151 152 return; /* here document expanded */ 152 153 } 153 STPUTC(psh, '\0', expdest);154 p = grabstackstr(psh, expdest);155 exparg.lastp = &exparg.list;154 STPUTC(psh, '\0', psh->expdest); 155 p = grabstackstr(psh, psh->expdest); 156 psh->exparg.lastp = &psh->exparg.list; 156 157 /* 157 158 * TODO - EXP_REDIR 158 159 */ 159 160 if (flag & EXP_FULL) { 160 ifsbreakup(p , &exparg);161 * exparg.lastp = NULL;162 exparg.lastp = &exparg.list;163 expandmeta( exparg.list, flag);161 ifsbreakup(psh, p, &psh->exparg); 162 *psh->exparg.lastp = NULL; 163 psh->exparg.lastp = &psh->exparg.list; 164 expandmeta(psh, psh->exparg.list, flag); 164 165 } else { 165 166 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ … … 167 168 sp = (struct strlist *)stalloc(psh, sizeof (struct strlist)); 168 169 sp->text = p; 169 * exparg.lastp = sp;170 exparg.lastp = &sp->next;171 } 172 ifsfree( );173 * exparg.lastp = NULL;174 if ( exparg.list) {175 *arglist->lastp = exparg.list;176 arglist->lastp = exparg.lastp;170 *psh->exparg.lastp = sp; 171 psh->exparg.lastp = &sp->next; 172 } 173 ifsfree(psh); 174 *psh->exparg.lastp = NULL; 175 if (psh->exparg.list) { 176 *arglist->lastp = psh->exparg.list; 177 arglist->lastp = psh->exparg.lastp; 177 178 } 178 179 } … … 187 188 188 189 STATIC void 189 argstr( char *p, int flag)190 argstr(shinstance *psh, char *p, int flag) 190 191 { 191 192 char c; … … 196 197 197 198 if (flag & EXP_IFS_SPLIT) 198 ifs = ifsset( ) ? ifsval() : " \t\n";199 ifs = ifsset(psh) ? ifsval(psh) : " \t\n"; 199 200 200 201 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) 201 p = exptilde(p , flag);202 p = exptilde(psh, p, flag); 202 203 for (;;) { 203 204 switch (c = *p++) { … … 210 211 break; 211 212 if ((flag & EXP_FULL) != 0) 212 STPUTC(psh, c, expdest);213 STPUTC(psh, c, psh->expdest); 213 214 ifs_split = 0; 214 215 break; … … 218 219 case CTLESC: 219 220 if (quotes) 220 STPUTC(psh, c, expdest);221 STPUTC(psh, c, psh->expdest); 221 222 c = *p++; 222 STPUTC(psh, c, expdest);223 STPUTC(psh, c, psh->expdest); 223 224 break; 224 225 case CTLVAR: 225 p = evalvar(p , (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split));226 p = evalvar(psh, p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split)); 226 227 break; 227 228 case CTLBACKQ: 228 229 case CTLBACKQ|CTLQUOTE: 229 expbackq( argbackq->n, c & CTLQUOTE, flag);230 argbackq =argbackq->next;230 expbackq(psh, psh->argbackq->n, c & CTLQUOTE, flag); 231 psh->argbackq = psh->argbackq->next; 231 232 break; 232 233 case CTLENDARI: … … 239 240 * assignments (after the first '=' and after ':'s). 240 241 */ 241 STPUTC(psh, c, expdest);242 STPUTC(psh, c, psh->expdest); 242 243 if (flag & EXP_VARTILDE && *p == '~') { 243 244 if (c == '=') { … … 247 248 break; 248 249 } 249 p = exptilde(p , flag);250 p = exptilde(psh, p, flag); 250 251 } 251 252 break; 252 253 default: 253 STPUTC(psh, c, expdest);254 STPUTC(psh, c, psh->expdest); 254 255 if (flag & EXP_IFS_SPLIT & ifs_split && strchr(ifs, c) != NULL) { 255 256 /* We need to get the output split here... */ 256 recordregion( expdest - stackblock(psh) - 1,257 expdest - stackblock(psh), 0);257 recordregion(psh, (int)(psh->expdest - stackblock(psh) - 1), 258 (int)(psh->expdest - stackblock(psh)), 0); 258 259 } 259 260 break; … … 263 264 264 265 STATIC char * 265 exptilde( char *p, int flag)266 exptilde(shinstance *psh, char *p, int flag) 266 267 { 267 268 char c, *startp = p; … … 300 301 while ((c = *home++) != '\0') { 301 302 if (quotes && SQSYNTAX[(int)c] == CCTL) 302 STPUTC(psh, CTLESC, expdest);303 STPUTC(psh, c, expdest);303 STPUTC(psh, CTLESC, psh->expdest); 304 STPUTC(psh, c, psh->expdest); 304 305 } 305 306 return (p); … … 311 312 312 313 STATIC void 313 removerecordregions( int endoff)314 { 315 if ( ifslastp == NULL)314 removerecordregions(shinstance *psh, int endoff) 315 { 316 if (psh->ifslastp == NULL) 316 317 return; 317 318 318 if ( ifsfirst.endoff > endoff) {319 while ( ifsfirst.next != NULL) {319 if (psh->ifsfirst.endoff > endoff) { 320 while (psh->ifsfirst.next != NULL) { 320 321 struct ifsregion *ifsp; 321 322 INTOFF; 322 ifsp = ifsfirst.next->next;323 ckfree( ifsfirst.next);324 ifsfirst.next = ifsp;323 ifsp = psh->ifsfirst.next->next; 324 ckfree(psh->ifsfirst.next); 325 psh->ifsfirst.next = ifsp; 325 326 INTON; 326 327 } 327 if ( ifsfirst.begoff > endoff)328 ifslastp = NULL;328 if (psh->ifsfirst.begoff > endoff) 329 psh->ifslastp = NULL; 329 330 else { 330 ifslastp = &ifsfirst;331 ifsfirst.endoff = endoff;331 psh->ifslastp = &psh->ifsfirst; 332 psh->ifsfirst.endoff = endoff; 332 333 } 333 334 return; 334 335 } 335 336 336 ifslastp = &ifsfirst;337 while ( ifslastp->next &&ifslastp->next->begoff < endoff)338 ifslastp=ifslastp->next;339 while ( ifslastp->next != NULL) {337 psh->ifslastp = &psh->ifsfirst; 338 while (psh->ifslastp->next && psh->ifslastp->next->begoff < endoff) 339 psh->ifslastp=psh->ifslastp->next; 340 while (psh->ifslastp->next != NULL) { 340 341 struct ifsregion *ifsp; 341 342 INTOFF; 342 ifsp = ifslastp->next->next;343 ckfree( ifslastp->next);344 ifslastp->next = ifsp;343 ifsp = psh->ifslastp->next->next; 344 ckfree(psh->ifslastp->next); 345 psh->ifslastp->next = ifsp; 345 346 INTON; 346 347 } 347 if ( ifslastp->endoff > endoff)348 ifslastp->endoff = endoff;348 if (psh->ifslastp->endoff > endoff) 349 psh->ifslastp->endoff = endoff; 349 350 } 350 351 … … 355 356 */ 356 357 void 357 expari( int flag)358 expari(shinstance *psh, int flag) 358 359 { 359 360 char *p, *start; … … 378 379 #error "integers with more than 10 digits are not supported" 379 380 #endif 380 CHECKSTRSPACE(psh, 12 - 2, expdest);381 USTPUTC(psh, '\0', expdest);381 CHECKSTRSPACE(psh, 12 - 2, psh->expdest); 382 USTPUTC(psh, '\0', psh->expdest); 382 383 start = stackblock(psh); 383 p = expdest - 1;384 p = psh->expdest - 1; 384 385 while (*p != CTLARI && p >= start) 385 386 --p; … … 395 396 else 396 397 quoted=0; 397 begoff = p - start;398 removerecordregions( begoff);398 begoff = (int)(p - start); 399 removerecordregions(psh, begoff); 399 400 if (quotes) 400 401 rmescapes(psh, p+2); … … 406 407 407 408 if (quoted == 0) 408 recordregion( begoff, p - 1 - start, 0);409 result = expdest - p + 1;410 STADJUST(psh, -result, expdest);409 recordregion(psh, begoff, (int)(p - 1 - start), 0); 410 result = (int)(psh->expdest - p + 1); 411 STADJUST(psh, -result, psh->expdest); 411 412 } 412 413 … … 417 418 418 419 STATIC void 419 expbackq( union node *cmd, int quoted, int flag)420 expbackq(shinstance *psh, union node *cmd, int quoted, int flag) 420 421 { 421 422 struct backcmd in; … … 423 424 char buf[128]; 424 425 char *p; 425 char *dest = expdest;426 char *dest = psh->expdest; 426 427 struct ifsregion saveifs, *savelastp; 427 428 struct nodelist *saveargbackq; 428 429 char lastc; 429 int startloc = dest - stackblock(psh);430 int startloc = (int)(dest - stackblock(psh)); 430 431 char const *syntax = quoted? DQSYNTAX : BASESYNTAX; 431 432 int saveherefd; … … 433 434 434 435 INTOFF; 435 saveifs = ifsfirst;436 savelastp = ifslastp;437 saveargbackq = argbackq;436 saveifs = psh->ifsfirst; 437 savelastp = psh->ifslastp; 438 saveargbackq = psh->argbackq; 438 439 saveherefd = psh->herefd; 439 440 psh->herefd = -1; … … 441 442 evalbackcmd(psh, cmd, &in); 442 443 ungrabstackstr(psh, p, dest); 443 ifsfirst = saveifs;444 ifslastp = savelastp;445 argbackq = saveargbackq;444 psh->ifsfirst = saveifs; 445 psh->ifslastp = savelastp; 446 psh->argbackq = saveargbackq; 446 447 psh->herefd = saveherefd; 447 448 … … 477 478 ckfree(in.buf); 478 479 if (in.jp) 479 back_exitstatus = waitforjob(psh, in.jp);480 psh->back_exitstatus = waitforjob(psh, in.jp); 480 481 if (quoted == 0) 481 recordregion( startloc, dest - stackblock(psh), 0);482 recordregion(psh, startloc, (int)(dest - stackblock(psh)), 0); 482 483 TRACE((psh, "evalbackq: size=%d: \"%.*s\"\n", 483 484 (dest - stackblock(psh)) - startloc, 484 485 (dest - stackblock(psh)) - startloc, 485 486 stackblock(psh) + startloc)); 486 expdest = dest;487 psh->expdest = dest; 487 488 INTON; 488 489 } … … 491 492 492 493 STATIC int 493 subevalvar( char *p, char *str, int strloc, int subtype, int startloc, int varflags)494 subevalvar(shinstance *psh, char *p, char *str, int strloc, int subtype, int startloc, int varflags) 494 495 { 495 496 char *startp; … … 498 499 int c = 0; 499 500 int saveherefd = psh->herefd; 500 struct nodelist *saveargbackq = argbackq;501 struct nodelist *saveargbackq = psh->argbackq; 501 502 int amount; 502 503 503 504 psh->herefd = -1; 504 argstr(p , 0);505 STACKSTRNUL(psh, expdest);505 argstr(psh, p, 0); 506 STACKSTRNUL(psh, psh->expdest); 506 507 psh->herefd = saveherefd; 507 argbackq = saveargbackq;508 psh->argbackq = saveargbackq; 508 509 startp = stackblock(psh) + startloc; 509 510 if (str == NULL) … … 513 514 case VSASSIGN: 514 515 setvar(psh, str, startp, 0); 515 amount = startp - expdest;516 STADJUST(psh, amount, expdest);516 amount = (int)(startp - psh->expdest); 517 STADJUST(psh, amount, psh->expdest); 517 518 varflags &= ~VSNUL; 518 519 if (c != 0) … … 592 593 recordleft: 593 594 *loc = c; 594 amount = ( (str - 1) - (loc - startp)) - expdest;595 STADJUST(psh, amount, expdest);595 amount = (int)(((str - 1) - (loc - startp)) - psh->expdest); 596 STADJUST(psh, amount, psh->expdest); 596 597 while (loc != str - 1) 597 598 *startp++ = *loc++; … … 599 600 600 601 recordright: 601 amount = loc - expdest;602 STADJUST(psh, amount, expdest);603 STPUTC(psh, '\0', expdest);604 STADJUST(psh, -1, expdest);602 amount = (int)(loc - psh->expdest); 603 STADJUST(psh, amount, psh->expdest); 604 STPUTC(psh, '\0', psh->expdest); 605 STADJUST(psh, -1, psh->expdest); 605 606 return 1; 606 607 } … … 613 614 614 615 STATIC char * 615 evalvar( char *p, int flag)616 evalvar(shinstance *psh, char *p, int flag) 616 617 { 617 618 int subtype; … … 636 637 again: /* jump here after setting a variable with ${var=text} */ 637 638 if (special) { 638 set = varisset( var, varflags & VSNUL);639 set = varisset(psh, var, varflags & VSNUL); 639 640 val = NULL; 640 641 } else { … … 648 649 649 650 varlen = 0; 650 startloc = expdest - stackblock(psh);651 startloc = (int)(psh->expdest - stackblock(psh)); 651 652 652 653 if (!set && uflag(psh)) { … … 666 667 /* insert the value of the variable */ 667 668 if (special) { 668 varvalue( var, varflags & VSQUOTE, subtype, flag);669 varvalue(psh, var, varflags & VSQUOTE, subtype, flag); 669 670 if (subtype == VSLENGTH) { 670 varlen = expdest - stackblock(psh) - startloc;671 STADJUST(psh, -varlen, expdest);671 varlen = (int)(psh->expdest - stackblock(psh) - startloc); 672 STADJUST(psh, -varlen, psh->expdest); 672 673 } 673 674 } else { … … 681 682 while (*val) { 682 683 if (quotes && syntax[(int)*val] == CCTL) 683 STPUTC(psh, CTLESC, expdest);684 STPUTC(psh, *val++, expdest);684 STPUTC(psh, CTLESC, psh->expdest); 685 STPUTC(psh, *val++, psh->expdest); 685 686 } 686 687 … … 691 692 692 693 apply_ifs = ((varflags & VSQUOTE) == 0 || 693 (*var == '@' && shellparam.nparam != 1));694 (*var == '@' && psh->shellparam.nparam != 1)); 694 695 695 696 switch (subtype) { 696 697 case VSLENGTH: 697 expdest = cvtnum(varlen,expdest);698 psh->expdest = cvtnum(psh, varlen, psh->expdest); 698 699 break; 699 700 … … 706 707 case VSMINUS: 707 708 if (!set) { 708 argstr(p , flag | (apply_ifs ? EXP_IFS_SPLIT : 0));709 argstr(psh, p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0)); 709 710 /* 710 711 * ${x-a b c} doesn't get split, but removing the … … 727 728 * right after it 728 729 */ 729 STPUTC(psh, '\0', expdest);730 patloc = expdest - stackblock(psh);731 if (subevalvar(p , NULL, patloc, subtype,730 STPUTC(psh, '\0', psh->expdest); 731 patloc = (int)(psh->expdest - stackblock(psh)); 732 if (subevalvar(psh, p, NULL, patloc, subtype, 732 733 startloc, varflags) == 0) { 733 int amount = ( expdest - stackblock(psh) - patloc) + 1;734 STADJUST(psh, -amount, expdest);734 int amount = (int)(psh->expdest - stackblock(psh) - patloc) + 1; 735 STADJUST(psh, -amount, psh->expdest); 735 736 } 736 737 /* Remove any recorded regions beyond start of variable */ 737 removerecordregions( startloc);738 removerecordregions(psh, startloc); 738 739 apply_ifs = 1; 739 740 break; … … 743 744 if (set) 744 745 break; 745 if (subevalvar(p , var, 0, subtype, startloc, varflags)) {746 if (subevalvar(psh, p, var, 0, subtype, startloc, varflags)) { 746 747 varflags &= ~VSNUL; 747 748 /* … … 749 750 * start of variable 750 751 */ 751 removerecordregions( startloc);752 removerecordregions(psh, startloc); 752 753 goto again; 753 754 } … … 760 761 761 762 if (apply_ifs) 762 recordregion( startloc, expdest - stackblock(psh),763 recordregion(psh, startloc, (int)(psh->expdest - stackblock(psh)), 763 764 varflags & VSQUOTE); 764 765 … … 770 771 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { 771 772 if (set) 772 argbackq =argbackq->next;773 psh->argbackq = psh->argbackq->next; 773 774 } else if (c == CTLVAR) { 774 775 if ((*p++ & VSTYPE) != VSNORMAL) … … 790 791 791 792 STATIC int 792 varisset( char *name, int nulok)793 varisset(shinstance *psh, char *name, int nulok) 793 794 { 794 795 if (*name == '!') 795 return backgndpid != -1;796 return psh->backgndpid != -1; 796 797 else if (*name == '@' || *name == '*') { 797 if (* shellparam.p == NULL)798 if (*psh->shellparam.p == NULL) 798 799 return 0; 799 800 … … 801 802 char **av; 802 803 803 for (av = shellparam.p; *av; av++)804 for (av = psh->shellparam.p; *av; av++) 804 805 if (**av != '\0') 805 806 return 1; … … 810 811 int num = atoi(name); 811 812 812 if (num > shellparam.nparam)813 if (num > psh->shellparam.nparam) 813 814 return 0; 814 815 815 816 if (num == 0) 816 ap = arg0;817 ap = psh->arg0; 817 818 else 818 ap = shellparam.p[num - 1];819 ap = psh->shellparam.p[num - 1]; 819 820 820 821 if (nulok && (ap == NULL || *ap == '\0')) … … 831 832 832 833 STATIC void 833 varvalue( char *name, int quoted, int subtype, int flag)834 varvalue(shinstance *psh, char *name, int quoted, int subtype, int flag) 834 835 { 835 836 int num; … … 846 847 while (*p) { \ 847 848 if (syntax[(int)*p] == CCTL) \ 848 STPUTC(psh, CTLESC, expdest); \849 STPUTC(psh, *p++, expdest); \849 STPUTC(psh, CTLESC, psh->expdest); \ 850 STPUTC(psh, *p++, psh->expdest); \ 850 851 } \ 851 852 } else \ 852 853 while (*p) \ 853 STPUTC(psh, *p++, expdest); \854 STPUTC(psh, *p++, psh->expdest); \ 854 855 } while (0) 855 856 … … 857 858 switch (*name) { 858 859 case '$': 859 num = rootpid;860 num = psh->rootpid; 860 861 goto numvar; 861 862 case '?': 862 num = exitstatus;863 num = psh->exitstatus; 863 864 goto numvar; 864 865 case '#': 865 num = shellparam.nparam;866 num = psh->shellparam.nparam; 866 867 goto numvar; 867 868 case '!': 868 num = backgndpid;869 num = psh->backgndpid; 869 870 numvar: 870 expdest = cvtnum(num,expdest);871 psh->expdest = cvtnum(psh, num, psh->expdest); 871 872 break; 872 873 case '-': 873 for (i = 0; optlist[i].name; i++) {874 if ( optlist[i].val)875 STPUTC(psh, optlist[i].letter,expdest);874 for (i = 0; psh->optlist[i].name; i++) { 875 if (psh->optlist[i].val) 876 STPUTC(psh, psh->optlist[i].letter, psh->expdest); 876 877 } 877 878 break; 878 879 case '@': 879 880 if (flag & EXP_FULL && quoted) { 880 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {881 for (ap = psh->shellparam.p ; (p = *ap++) != NULL ; ) { 881 882 STRTODEST(p); 882 883 if (*ap) 883 STPUTC(psh, '\0', expdest);884 STPUTC(psh, '\0', psh->expdest); 884 885 } 885 886 break; … … 887 888 /* fall through */ 888 889 case '*': 889 if (ifsset( ) != 0)890 sep = ifsval( )[0];890 if (ifsset(psh) != 0) 891 sep = ifsval(psh)[0]; 891 892 else 892 893 sep = ' '; 893 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {894 for (ap = psh->shellparam.p ; (p = *ap++) != NULL ; ) { 894 895 STRTODEST(p); 895 896 if (*ap && sep) 896 STPUTC(psh, sep, expdest);897 STPUTC(psh, sep, psh->expdest); 897 898 } 898 899 break; 899 900 case '0': 900 p = arg0;901 p = psh->arg0; 901 902 STRTODEST(p); 902 903 break; … … 904 905 if (is_digit(*name)) { 905 906 num = atoi(name); 906 if (num > 0 && num <= shellparam.nparam) {907 p = shellparam.p[num - 1];907 if (num > 0 && num <= psh->shellparam.nparam) { 908 p = psh->shellparam.p[num - 1]; 908 909 STRTODEST(p); 909 910 } … … 921 922 922 923 STATIC void 923 recordregion( int start, int end, int inquotes)924 recordregion(shinstance *psh, int start, int end, int inquotes) 924 925 { 925 926 struct ifsregion *ifsp; 926 927 927 if ( ifslastp == NULL) {928 ifsp = & ifsfirst;928 if (psh->ifslastp == NULL) { 929 ifsp = &psh->ifsfirst; 929 930 } else { 930 if ( ifslastp->endoff == start931 && ifslastp->inquotes == inquotes) {931 if (psh->ifslastp->endoff == start 932 && psh->ifslastp->inquotes == inquotes) { 932 933 /* extend previous area */ 933 ifslastp->endoff = end;934 psh->ifslastp->endoff = end; 934 935 return; 935 936 } 936 937 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); 937 ifslastp->next = ifsp;938 } 939 ifslastp = ifsp;940 ifslastp->next = NULL;941 ifslastp->begoff = start;942 ifslastp->endoff = end;943 ifslastp->inquotes = inquotes;938 psh->ifslastp->next = ifsp; 939 } 940 psh->ifslastp = ifsp; 941 psh->ifslastp->next = NULL; 942 psh->ifslastp->begoff = start; 943 psh->ifslastp->endoff = end; 944 psh->ifslastp->inquotes = inquotes; 944 945 } 945 946 … … 952 953 */ 953 954 STATIC void 954 ifsbreakup( char *string, struct arglist *arglist)955 ifsbreakup(shinstance *psh, char *string, struct arglist *arglist) 955 956 { 956 957 struct ifsregion *ifsp; … … 967 968 inquotes = 0; 968 969 969 if ( ifslastp == NULL) {970 if (psh->ifslastp == NULL) { 970 971 /* Return entire argument, IFS doesn't apply to any of it */ 971 972 sp = (struct strlist *)stalloc(psh, sizeof *sp); … … 976 977 } 977 978 978 ifs = ifsset( ) ? ifsval() : " \t\n";979 980 for (ifsp = & ifsfirst; ifsp != NULL; ifsp = ifsp->next) {979 ifs = ifsset(psh) ? ifsval(psh) : " \t\n"; 980 981 for (ifsp = &psh->ifsfirst; ifsp != NULL; ifsp = ifsp->next) { 981 982 p = string + ifsp->begoff; 982 983 inquotes = ifsp->inquotes; … … 1051 1052 1052 1053 STATIC void 1053 ifsfree( void)1054 { 1055 while ( ifsfirst.next != NULL) {1054 ifsfree(shinstance *psh) 1055 { 1056 while (psh->ifsfirst.next != NULL) { 1056 1057 struct ifsregion *ifsp; 1057 1058 INTOFF; 1058 ifsp = ifsfirst.next->next;1059 ckfree( ifsfirst.next);1060 ifsfirst.next = ifsp;1059 ifsp = psh->ifsfirst.next->next; 1060 ckfree(psh->ifsfirst.next); 1061 psh->ifsfirst.next = ifsp; 1061 1062 INTON; 1062 1063 } 1063 ifslastp = NULL;1064 ifsfirst.next = NULL;1064 psh->ifslastp = NULL; 1065 psh->ifsfirst.next = NULL; 1065 1066 } 1066 1067 … … 1069 1070 /* 1070 1071 * Expand shell metacharacters. At this point, the only control characters 1071 * should be escapes. The results are stored in the list exparg.1072 */ 1073 1074 char *expdir;1072 * should be escapes. The results are stored in the list psh->exparg. 1073 */ 1074 1075 //char *expdir; 1075 1076 1076 1077 1077 1078 STATIC void 1078 expandmeta(s truct strlist *str, int flag)1079 expandmeta(shinstance *psh, struct strlist *str, int flag) 1079 1080 { 1080 1081 char *p; … … 1094 1095 break; 1095 1096 } 1096 savelastp = exparg.lastp;1097 savelastp = psh->exparg.lastp; 1097 1098 INTOFF; 1098 if ( expdir == NULL) {1099 int i = strlen(str->text);1100 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */1101 } 1102 1103 expmeta( expdir, str->text);1104 ckfree( expdir);1105 expdir = NULL;1099 if (psh->expdir == NULL) { 1100 size_t i = strlen(str->text); 1101 psh->expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ 1102 } 1103 1104 expmeta(psh, psh->expdir, str->text); 1105 ckfree(psh->expdir); 1106 psh->expdir = NULL; 1106 1107 INTON; 1107 if ( exparg.lastp == savelastp) {1108 if (psh->exparg.lastp == savelastp) { 1108 1109 /* 1109 1110 * no matches 1110 1111 */ 1111 1112 nometa: 1112 * exparg.lastp = str;1113 *psh->exparg.lastp = str; 1113 1114 rmescapes(psh, str->text); 1114 exparg.lastp = &str->next;1115 psh->exparg.lastp = &str->next; 1115 1116 } else { 1116 * exparg.lastp = NULL;1117 *psh->exparg.lastp = NULL; 1117 1118 *savelastp = sp = expsort(*savelastp); 1118 1119 while (sp->next != NULL) 1119 1120 sp = sp->next; 1120 exparg.lastp = &sp->next;1121 psh->exparg.lastp = &sp->next; 1121 1122 } 1122 1123 str = str->next; … … 1130 1131 1131 1132 STATIC void 1132 expmeta( char *enddir, char *name)1133 expmeta(shinstance *psh, char *enddir, char *name) 1133 1134 { 1134 1135 char *p; … … 1180 1181 } 1181 1182 if (metaflag == 0) { /* we've reached the end of the file name */ 1182 if (enddir != expdir)1183 if (enddir != psh->expdir) 1183 1184 metaflag++; 1184 1185 for (p = name ; ; p++) { … … 1191 1192 break; 1192 1193 } 1193 if (metaflag == 0 || lstat( expdir, &statb) >= 0)1194 addfname( expdir);1194 if (metaflag == 0 || lstat(psh->expdir, &statb) >= 0) 1195 addfname(psh, psh->expdir); 1195 1196 return; 1196 1197 } … … 1206 1207 } 1207 1208 } 1208 if (enddir == expdir) {1209 if (enddir == psh->expdir) { 1209 1210 cp = "."; 1210 } else if (enddir == expdir + 1 && *expdir == '/') {1211 } else if (enddir == psh->expdir + 1 && *psh->expdir == '/') { 1211 1212 cp = "/"; 1212 1213 } else { 1213 cp = expdir;1214 cp = psh->expdir; 1214 1215 enddir[-1] = '\0'; 1215 1216 } 1216 1217 if ((dirp = opendir(cp)) == NULL) 1217 1218 return; 1218 if (enddir != expdir)1219 if (enddir != psh->expdir) 1219 1220 enddir[-1] = '/'; 1220 1221 if (*endname == 0) { … … 1238 1239 if (atend) { 1239 1240 scopy(dp->d_name, enddir); 1240 addfname( expdir);1241 addfname(psh, psh->expdir); 1241 1242 } else { 1242 1243 for (p = enddir, cp = dp->d_name; … … 1244 1245 continue; 1245 1246 p[-1] = '/'; 1246 expmeta(p , endname);1247 expmeta(psh, p, endname); 1247 1248 } 1248 1249 } … … 1259 1260 1260 1261 STATIC void 1261 addfname( char *name)1262 addfname(shinstance *psh, char *name) 1262 1263 { 1263 1264 char *p; … … 1268 1269 sp = (struct strlist *)stalloc(psh, sizeof *sp); 1269 1270 sp->text = p; 1270 * exparg.lastp = sp;1271 exparg.lastp = &sp->next;1271 *psh->exparg.lastp = sp; 1272 psh->exparg.lastp = &sp->next; 1272 1273 } 1273 1274 … … 1339 1340 1340 1341 int 1341 patmatch( char *pattern, char *string, int squoted)1342 patmatch(shinstance *psh, char *pattern, char *string, int squoted) 1342 1343 { 1343 1344 #ifdef notdef … … 1474 1475 1475 1476 void 1476 rmescapes( char *str)1477 rmescapes(shinstance *psh, char *str) 1477 1478 { 1478 1479 char *p, *q; … … 1503 1504 1504 1505 int 1505 casematch( union node *pattern, char *val)1506 casematch(shinstance *psh, union node *pattern, char *val) 1506 1507 { 1507 1508 struct stackmark smark; … … 1510 1511 1511 1512 setstackmark(psh, &smark); 1512 argbackq = pattern->narg.backquote;1513 STARTSTACKSTR(psh, expdest);1514 ifslastp = NULL;1515 argstr(p attern->narg.text, EXP_TILDE | EXP_CASE);1516 STPUTC(psh, '\0', expdest);1517 p = grabstackstr(psh, expdest);1513 psh->argbackq = pattern->narg.backquote; 1514 STARTSTACKSTR(psh, psh->expdest); 1515 psh->ifslastp = NULL; 1516 argstr(psh, pattern->narg.text, EXP_TILDE | EXP_CASE); 1517 STPUTC(psh, '\0', psh->expdest); 1518 p = grabstackstr(psh, psh->expdest); 1518 1519 result = patmatch(psh, p, val, 0); 1519 1520 popstackmark(psh, &smark); … … 1526 1527 1527 1528 STATIC char * 1528 cvtnum( int num, char *buf)1529 cvtnum(shinstance *psh, int num, char *buf) 1529 1530 { 1530 1531 char temp[32];
Note:
See TracChangeset
for help on using the changeset viewer.