Ignore:
Timestamp:
Oct 7, 2007, 3:39:01 AM (18 years ago)
Author:
bird
Message:

converted a few more files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/expand.c

    r1202 r1203  
    7878#include "mystring.h"
    7979#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
     101STATIC void argstr(shinstance *, char *, int);
     102STATIC char *exptilde(shinstance *, char *, int);
     103STATIC void expbackq(shinstance *, union node *, int, int);
     104STATIC int subevalvar(shinstance *, char *, char *, int, int, int, int);
     105STATIC char *evalvar(shinstance *, char *, int);
     106STATIC int varisset(shinstance *, char *, int);
     107STATIC void varvalue(shinstance *, char *, int, int, int);
     108STATIC void recordregion(shinstance *, int, int, int);
     109STATIC void removerecordregions(shinstance *, int);
     110STATIC void ifsbreakup(shinstance *, char *, struct arglist *);
     111STATIC void ifsfree(shinstance *);
     112STATIC void expandmeta(shinstance *, struct strlist *, int);
     113STATIC void expmeta(shinstance *, char *, char *);
     114STATIC void addfname(shinstance *, char *);
    114115STATIC struct strlist *expsort(struct strlist *);
    115116STATIC struct strlist *msort(struct strlist *, int);
    116117STATIC int pmatch(char *, char *, int);
    117 STATIC char *cvtnum(int, char *);
     118STATIC char *cvtnum(shinstance *, int, char *);
    118119
    119120/*
     
    138139
    139140void
    140 expandarg(union node *arg, struct arglist *arglist, int flag)
     141expandarg(shinstance *psh, union node *arg, struct arglist *arglist, int flag)
    141142{
    142143        struct strlist *sp;
    143144        char *p;
    144145
    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);
    150151        if (arglist == NULL) {
    151152                return;                 /* here document expanded */
    152153        }
    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;
    156157        /*
    157158         * TODO - EXP_REDIR
    158159         */
    159160        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);
    164165        } else {
    165166                if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
     
    167168                sp = (struct strlist *)stalloc(psh, sizeof (struct strlist));
    168169                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;
    177178        }
    178179}
     
    187188
    188189STATIC void
    189 argstr(char *p, int flag)
     190argstr(shinstance *psh, char *p, int flag)
    190191{
    191192        char c;
     
    196197
    197198        if (flag & EXP_IFS_SPLIT)
    198                 ifs = ifsset() ? ifsval() : " \t\n";
     199                ifs = ifsset(psh) ? ifsval(psh) : " \t\n";
    199200
    200201        if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
    201                 p = exptilde(p, flag);
     202                p = exptilde(psh, p, flag);
    202203        for (;;) {
    203204                switch (c = *p++) {
     
    210211                                break;
    211212                        if ((flag & EXP_FULL) != 0)
    212                                 STPUTC(psh, c, expdest);
     213                                STPUTC(psh, c, psh->expdest);
    213214                        ifs_split = 0;
    214215                        break;
     
    218219                case CTLESC:
    219220                        if (quotes)
    220                                 STPUTC(psh, c, expdest);
     221                                STPUTC(psh, c, psh->expdest);
    221222                        c = *p++;
    222                         STPUTC(psh, c, expdest);
     223                        STPUTC(psh, c, psh->expdest);
    223224                        break;
    224225                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));
    226227                        break;
    227228                case CTLBACKQ:
    228229                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;
    231232                        break;
    232233                case CTLENDARI:
     
    239240                         * assignments (after the first '=' and after ':'s).
    240241                         */
    241                         STPUTC(psh, c, expdest);
     242                        STPUTC(psh, c, psh->expdest);
    242243                        if (flag & EXP_VARTILDE && *p == '~') {
    243244                                if (c == '=') {
     
    247248                                                break;
    248249                                }
    249                                 p = exptilde(p, flag);
     250                                p = exptilde(psh, p, flag);
    250251                        }
    251252                        break;
    252253                default:
    253                         STPUTC(psh, c, expdest);
     254                        STPUTC(psh, c, psh->expdest);
    254255                        if (flag & EXP_IFS_SPLIT & ifs_split && strchr(ifs, c) != NULL) {
    255256                                /* 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);
    258259                        }
    259260                        break;
     
    263264
    264265STATIC char *
    265 exptilde(char *p, int flag)
     266exptilde(shinstance *psh, char *p, int flag)
    266267{
    267268        char c, *startp = p;
     
    300301        while ((c = *home++) != '\0') {
    301302                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);
    304305        }
    305306        return (p);
     
    311312
    312313STATIC void
    313 removerecordregions(int endoff)
    314 {
    315         if (ifslastp == NULL)
     314removerecordregions(shinstance *psh, int endoff)
     315{
     316        if (psh->ifslastp == NULL)
    316317                return;
    317318
    318         if (ifsfirst.endoff > endoff) {
    319                 while (ifsfirst.next != NULL) {
     319        if (psh->ifsfirst.endoff > endoff) {
     320                while (psh->ifsfirst.next != NULL) {
    320321                        struct ifsregion *ifsp;
    321322                        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;
    325326                        INTON;
    326327                }
    327                 if (ifsfirst.begoff > endoff)
    328                         ifslastp = NULL;
     328                if (psh->ifsfirst.begoff > endoff)
     329                        psh->ifslastp = NULL;
    329330                else {
    330                         ifslastp = &ifsfirst;
    331                         ifsfirst.endoff = endoff;
     331                        psh->ifslastp = &psh->ifsfirst;
     332                        psh->ifsfirst.endoff = endoff;
    332333                }
    333334                return;
    334335        }
    335336
    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) {
    340341                struct ifsregion *ifsp;
    341342                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;
    345346                INTON;
    346347        }
    347         if (ifslastp->endoff > endoff)
    348                 ifslastp->endoff = endoff;
     348        if (psh->ifslastp->endoff > endoff)
     349                psh->ifslastp->endoff = endoff;
    349350}
    350351
     
    355356 */
    356357void
    357 expari(int flag)
     358expari(shinstance *psh, int flag)
    358359{
    359360        char *p, *start;
     
    378379#error "integers with more than 10 digits are not supported"
    379380#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);
    382383        start = stackblock(psh);
    383         p = expdest - 1;
     384        p = psh->expdest - 1;
    384385        while (*p != CTLARI && p >= start)
    385386                --p;
     
    395396        else
    396397                quoted=0;
    397         begoff = p - start;
    398         removerecordregions(begoff);
     398        begoff = (int)(p - start);
     399        removerecordregions(psh, begoff);
    399400        if (quotes)
    400401                rmescapes(psh, p+2);
     
    406407
    407408        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);
    411412}
    412413
     
    417418
    418419STATIC void
    419 expbackq(union node *cmd, int quoted, int flag)
     420expbackq(shinstance *psh, union node *cmd, int quoted, int flag)
    420421{
    421422        struct backcmd in;
     
    423424        char buf[128];
    424425        char *p;
    425         char *dest = expdest;
     426        char *dest = psh->expdest;
    426427        struct ifsregion saveifs, *savelastp;
    427428        struct nodelist *saveargbackq;
    428429        char lastc;
    429         int startloc = dest - stackblock(psh);
     430        int startloc = (int)(dest - stackblock(psh));
    430431        char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
    431432        int saveherefd;
     
    433434
    434435        INTOFF;
    435         saveifs = ifsfirst;
    436         savelastp = ifslastp;
    437         saveargbackq = argbackq;
     436        saveifs = psh->ifsfirst;
     437        savelastp = psh->ifslastp;
     438        saveargbackq = psh->argbackq;
    438439        saveherefd = psh->herefd;
    439440        psh->herefd = -1;
     
    441442        evalbackcmd(psh, cmd, &in);
    442443        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;
    446447        psh->herefd = saveherefd;
    447448
     
    477478                ckfree(in.buf);
    478479        if (in.jp)
    479                 back_exitstatus = waitforjob(psh, in.jp);
     480                psh->back_exitstatus = waitforjob(psh, in.jp);
    480481        if (quoted == 0)
    481                 recordregion(startloc, dest - stackblock(psh), 0);
     482                recordregion(psh, startloc, (int)(dest - stackblock(psh)), 0);
    482483        TRACE((psh, "evalbackq: size=%d: \"%.*s\"\n",
    483484                (dest - stackblock(psh)) - startloc,
    484485                (dest - stackblock(psh)) - startloc,
    485486                stackblock(psh) + startloc));
    486         expdest = dest;
     487        psh->expdest = dest;
    487488        INTON;
    488489}
     
    491492
    492493STATIC int
    493 subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags)
     494subevalvar(shinstance *psh, char *p, char *str, int strloc, int subtype, int startloc, int varflags)
    494495{
    495496        char *startp;
     
    498499        int c = 0;
    499500        int saveherefd = psh->herefd;
    500         struct nodelist *saveargbackq = argbackq;
     501        struct nodelist *saveargbackq = psh->argbackq;
    501502        int amount;
    502503
    503504        psh->herefd = -1;
    504         argstr(p, 0);
    505         STACKSTRNUL(psh, expdest);
     505        argstr(psh, p, 0);
     506        STACKSTRNUL(psh, psh->expdest);
    506507        psh->herefd = saveherefd;
    507         argbackq = saveargbackq;
     508        psh->argbackq = saveargbackq;
    508509        startp = stackblock(psh) + startloc;
    509510        if (str == NULL)
     
    513514        case VSASSIGN:
    514515                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);
    517518                varflags &= ~VSNUL;
    518519                if (c != 0)
     
    592593recordleft:
    593594        *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);
    596597        while (loc != str - 1)
    597598                *startp++ = *loc++;
     
    599600
    600601recordright:
    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);
    605606        return 1;
    606607}
     
    613614
    614615STATIC char *
    615 evalvar(char *p, int flag)
     616evalvar(shinstance *psh, char *p, int flag)
    616617{
    617618        int subtype;
     
    636637again: /* jump here after setting a variable with ${var=text} */
    637638        if (special) {
    638                 set = varisset(var, varflags & VSNUL);
     639                set = varisset(psh, var, varflags & VSNUL);
    639640                val = NULL;
    640641        } else {
     
    648649
    649650        varlen = 0;
    650         startloc = expdest - stackblock(psh);
     651        startloc = (int)(psh->expdest - stackblock(psh));
    651652
    652653        if (!set && uflag(psh)) {
     
    666667                /* insert the value of the variable */
    667668                if (special) {
    668                         varvalue(var, varflags & VSQUOTE, subtype, flag);
     669                        varvalue(psh, var, varflags & VSQUOTE, subtype, flag);
    669670                        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);
    672673                        }
    673674                } else {
     
    681682                                while (*val) {
    682683                                        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);
    685686                                }
    686687
     
    691692
    692693        apply_ifs = ((varflags & VSQUOTE) == 0 ||
    693                 (*var == '@' && shellparam.nparam != 1));
     694                (*var == '@' && psh->shellparam.nparam != 1));
    694695
    695696        switch (subtype) {
    696697        case VSLENGTH:
    697                 expdest = cvtnum(varlen, expdest);
     698                psh->expdest = cvtnum(psh, varlen, psh->expdest);
    698699                break;
    699700
     
    706707        case VSMINUS:
    707708                if (!set) {
    708                         argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
     709                        argstr(psh, p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0));
    709710                        /*
    710711                         * ${x-a b c} doesn't get split, but removing the
     
    727728                 * right after it
    728729                 */
    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,
    732733                               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);
    735736                }
    736737                /* Remove any recorded regions beyond start of variable */
    737                 removerecordregions(startloc);
     738                removerecordregions(psh, startloc);
    738739                apply_ifs = 1;
    739740                break;
     
    743744                if (set)
    744745                        break;
    745                 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
     746                if (subevalvar(psh, p, var, 0, subtype, startloc, varflags)) {
    746747                        varflags &= ~VSNUL;
    747748                        /*
     
    749750                         * start of variable
    750751                         */
    751                         removerecordregions(startloc);
     752                        removerecordregions(psh, startloc);
    752753                        goto again;
    753754                }
     
    760761
    761762        if (apply_ifs)
    762                 recordregion(startloc, expdest - stackblock(psh),
     763                recordregion(psh, startloc, (int)(psh->expdest - stackblock(psh)),
    763764                             varflags & VSQUOTE);
    764765
     
    770771                        else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
    771772                                if (set)
    772                                         argbackq = argbackq->next;
     773                                        psh->argbackq = psh->argbackq->next;
    773774                        } else if (c == CTLVAR) {
    774775                                if ((*p++ & VSTYPE) != VSNORMAL)
     
    790791
    791792STATIC int
    792 varisset(char *name, int nulok)
     793varisset(shinstance *psh, char *name, int nulok)
    793794{
    794795        if (*name == '!')
    795                 return backgndpid != -1;
     796                return psh->backgndpid != -1;
    796797        else if (*name == '@' || *name == '*') {
    797                 if (*shellparam.p == NULL)
     798                if (*psh->shellparam.p == NULL)
    798799                        return 0;
    799800
     
    801802                        char **av;
    802803
    803                         for (av = shellparam.p; *av; av++)
     804                        for (av = psh->shellparam.p; *av; av++)
    804805                                if (**av != '\0')
    805806                                        return 1;
     
    810811                int num = atoi(name);
    811812
    812                 if (num > shellparam.nparam)
     813                if (num > psh->shellparam.nparam)
    813814                        return 0;
    814815
    815816                if (num == 0)
    816                         ap = arg0;
     817                        ap = psh->arg0;
    817818                else
    818                         ap = shellparam.p[num - 1];
     819                        ap = psh->shellparam.p[num - 1];
    819820
    820821                if (nulok && (ap == NULL || *ap == '\0'))
     
    831832
    832833STATIC void
    833 varvalue(char *name, int quoted, int subtype, int flag)
     834varvalue(shinstance *psh, char *name, int quoted, int subtype, int flag)
    834835{
    835836        int num;
     
    846847                while (*p) { \
    847848                        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); \
    850851                } \
    851852        } else \
    852853                while (*p) \
    853                         STPUTC(psh, *p++, expdest); \
     854                        STPUTC(psh, *p++, psh->expdest); \
    854855        } while (0)
    855856
     
    857858        switch (*name) {
    858859        case '$':
    859                 num = rootpid;
     860                num = psh->rootpid;
    860861                goto numvar;
    861862        case '?':
    862                 num = exitstatus;
     863                num = psh->exitstatus;
    863864                goto numvar;
    864865        case '#':
    865                 num = shellparam.nparam;
     866                num = psh->shellparam.nparam;
    866867                goto numvar;
    867868        case '!':
    868                 num = backgndpid;
     869                num = psh->backgndpid;
    869870numvar:
    870                 expdest = cvtnum(num, expdest);
     871                psh->expdest = cvtnum(psh, num, psh->expdest);
    871872                break;
    872873        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);
    876877                }
    877878                break;
    878879        case '@':
    879880                if (flag & EXP_FULL && quoted) {
    880                         for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
     881                        for (ap = psh->shellparam.p ; (p = *ap++) != NULL ; ) {
    881882                                STRTODEST(p);
    882883                                if (*ap)
    883                                         STPUTC(psh, '\0', expdest);
     884                                        STPUTC(psh, '\0', psh->expdest);
    884885                        }
    885886                        break;
     
    887888                /* fall through */
    888889        case '*':
    889                 if (ifsset() != 0)
    890                         sep = ifsval()[0];
     890                if (ifsset(psh) != 0)
     891                        sep = ifsval(psh)[0];
    891892                else
    892893                        sep = ' ';
    893                 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
     894                for (ap = psh->shellparam.p ; (p = *ap++) != NULL ; ) {
    894895                        STRTODEST(p);
    895896                        if (*ap && sep)
    896                                 STPUTC(psh, sep, expdest);
     897                                STPUTC(psh, sep, psh->expdest);
    897898                }
    898899                break;
    899900        case '0':
    900                 p = arg0;
     901                p = psh->arg0;
    901902                STRTODEST(p);
    902903                break;
     
    904905                if (is_digit(*name)) {
    905906                        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];
    908909                                STRTODEST(p);
    909910                        }
     
    921922
    922923STATIC void
    923 recordregion(int start, int end, int inquotes)
     924recordregion(shinstance *psh, int start, int end, int inquotes)
    924925{
    925926        struct ifsregion *ifsp;
    926927
    927         if (ifslastp == NULL) {
    928                 ifsp = &ifsfirst;
     928        if (psh->ifslastp == NULL) {
     929                ifsp = &psh->ifsfirst;
    929930        } else {
    930                 if (ifslastp->endoff == start
    931                     && ifslastp->inquotes == inquotes) {
     931                if (psh->ifslastp->endoff == start
     932                    && psh->ifslastp->inquotes == inquotes) {
    932933                        /* extend previous area */
    933                         ifslastp->endoff = end;
     934                        psh->ifslastp->endoff = end;
    934935                        return;
    935936                }
    936937                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;
    944945}
    945946
     
    952953 */
    953954STATIC void
    954 ifsbreakup(char *string, struct arglist *arglist)
     955ifsbreakup(shinstance *psh, char *string, struct arglist *arglist)
    955956{
    956957        struct ifsregion *ifsp;
     
    967968        inquotes = 0;
    968969
    969         if (ifslastp == NULL) {
     970        if (psh->ifslastp == NULL) {
    970971                /* Return entire argument, IFS doesn't apply to any of it */
    971972                sp = (struct strlist *)stalloc(psh, sizeof *sp);
     
    976977        }
    977978
    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) {
    981982                p = string + ifsp->begoff;
    982983                inquotes = ifsp->inquotes;
     
    10511052
    10521053STATIC void
    1053 ifsfree(void)
    1054 {
    1055         while (ifsfirst.next != NULL) {
     1054ifsfree(shinstance *psh)
     1055{
     1056        while (psh->ifsfirst.next != NULL) {
    10561057                struct ifsregion *ifsp;
    10571058                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;
    10611062                INTON;
    10621063        }
    1063         ifslastp = NULL;
    1064         ifsfirst.next = NULL;
     1064        psh->ifslastp = NULL;
     1065        psh->ifsfirst.next = NULL;
    10651066}
    10661067
     
    10691070/*
    10701071 * 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;
    10751076
    10761077
    10771078STATIC void
    1078 expandmeta(struct strlist *str, int flag)
     1079expandmeta(shinstance *psh, struct strlist *str, int flag)
    10791080{
    10801081        char *p;
     
    10941095                                break;
    10951096                }
    1096                 savelastp = exparg.lastp;
     1097                savelastp = psh->exparg.lastp;
    10971098                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;
    11061107                INTON;
    1107                 if (exparg.lastp == savelastp) {
     1108                if (psh->exparg.lastp == savelastp) {
    11081109                        /*
    11091110                         * no matches
    11101111                         */
    11111112nometa:
    1112                         *exparg.lastp = str;
     1113                        *psh->exparg.lastp = str;
    11131114                        rmescapes(psh, str->text);
    1114                         exparg.lastp = &str->next;
     1115                        psh->exparg.lastp = &str->next;
    11151116                } else {
    1116                         *exparg.lastp = NULL;
     1117                        *psh->exparg.lastp = NULL;
    11171118                        *savelastp = sp = expsort(*savelastp);
    11181119                        while (sp->next != NULL)
    11191120                                sp = sp->next;
    1120                         exparg.lastp = &sp->next;
     1121                        psh->exparg.lastp = &sp->next;
    11211122                }
    11221123                str = str->next;
     
    11301131
    11311132STATIC void
    1132 expmeta(char *enddir, char *name)
     1133expmeta(shinstance *psh, char *enddir, char *name)
    11331134{
    11341135        char *p;
     
    11801181        }
    11811182        if (metaflag == 0) {    /* we've reached the end of the file name */
    1182                 if (enddir != expdir)
     1183                if (enddir != psh->expdir)
    11831184                        metaflag++;
    11841185                for (p = name ; ; p++) {
     
    11911192                                break;
    11921193                }
    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);
    11951196                return;
    11961197        }
     
    12061207                }
    12071208        }
    1208         if (enddir == expdir) {
     1209        if (enddir == psh->expdir) {
    12091210                cp = ".";
    1210         } else if (enddir == expdir + 1 && *expdir == '/') {
     1211        } else if (enddir == psh->expdir + 1 && *psh->expdir == '/') {
    12111212                cp = "/";
    12121213        } else {
    1213                 cp = expdir;
     1214                cp = psh->expdir;
    12141215                enddir[-1] = '\0';
    12151216        }
    12161217        if ((dirp = opendir(cp)) == NULL)
    12171218                return;
    1218         if (enddir != expdir)
     1219        if (enddir != psh->expdir)
    12191220                enddir[-1] = '/';
    12201221        if (*endname == 0) {
     
    12381239                        if (atend) {
    12391240                                scopy(dp->d_name, enddir);
    1240                                 addfname(expdir);
     1241                                addfname(psh, psh->expdir);
    12411242                        } else {
    12421243                                for (p = enddir, cp = dp->d_name;
     
    12441245                                        continue;
    12451246                                p[-1] = '/';
    1246                                 expmeta(p, endname);
     1247                                expmeta(psh, p, endname);
    12471248                        }
    12481249                }
     
    12591260
    12601261STATIC void
    1261 addfname(char *name)
     1262addfname(shinstance *psh, char *name)
    12621263{
    12631264        char *p;
     
    12681269        sp = (struct strlist *)stalloc(psh, sizeof *sp);
    12691270        sp->text = p;
    1270         *exparg.lastp = sp;
    1271         exparg.lastp = &sp->next;
     1271        *psh->exparg.lastp = sp;
     1272        psh->exparg.lastp = &sp->next;
    12721273}
    12731274
     
    13391340
    13401341int
    1341 patmatch(char *pattern, char *string, int squoted)
     1342patmatch(shinstance *psh, char *pattern, char *string, int squoted)
    13421343{
    13431344#ifdef notdef
     
    14741475
    14751476void
    1476 rmescapes(char *str)
     1477rmescapes(shinstance *psh, char *str)
    14771478{
    14781479        char *p, *q;
     
    15031504
    15041505int
    1505 casematch(union node *pattern, char *val)
     1506casematch(shinstance *psh, union node *pattern, char *val)
    15061507{
    15071508        struct stackmark smark;
     
    15101511
    15111512        setstackmark(psh, &smark);
    1512         argbackq = pattern->narg.backquote;
    1513         STARTSTACKSTR(psh, expdest);
    1514         ifslastp = NULL;
    1515         argstr(pattern->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);
    15181519        result = patmatch(psh, p, val, 0);
    15191520        popstackmark(psh, &smark);
     
    15261527
    15271528STATIC char *
    1528 cvtnum(int num, char *buf)
     1529cvtnum(shinstance *psh, int num, char *buf)
    15291530{
    15301531        char temp[32];
Note: See TracChangeset for help on using the changeset viewer.