Ignore:
Timestamp:
Oct 6, 2007, 11:19:19 PM (18 years ago)
Author:
bird
Message:

moving globals into shinstance...

File:
1 edited

Legend:

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

    r809 r1198  
    6666#endif
    6767#include "show.h"
    68 
    69 char *arg0;                     /* value of $0 */
    70 struct shparam shellparam;      /* current positional parameters */
    71 char **argptr;                  /* argument list for builtin commands */
    72 char *optionarg;                /* set by nextopt (like getopt) */
    73 char *optptr;                   /* used by nextopt */
    74 
    75 char *minusc;                   /* argument to -c option */
    76 
    77 
    78 STATIC void options(int);
    79 STATIC void minus_o(char *, int);
    80 STATIC void setoption(int, int);
    81 STATIC int getopts(char *, char *, char **, char ***, char **);
     68#include "shinstance.h"
     69
     70//char *arg0;                   /* value of $0 */
     71//struct shparam shellparam;    /* current positional parameters */
     72//char **argptr;                        /* argument list for builtin commands */
     73//char *optionarg;              /* set by nextopt (like getopt) */
     74//char *optptr;                 /* used by nextopt */
     75
     76//char *minusc;                 /* argument to -c option */
     77
     78
     79STATIC void options(shinstance *, int);
     80STATIC void minus_o(shinstance *, char *, int);
     81STATIC void setoption(shinstance *, int, int);
     82STATIC int getopts(shinstance *, char *, char *, char **, char ***, char **);
    8283
    8384
     
    8788
    8889void
    89 procargs(int argc, char **argv)
     90procargs(shinstance *psh, int argc, char **argv)
    9091{
    9192        int i;
    9293
    93         argptr = argv;
     94        psh->argptr = argv;
    9495        if (argc > 0)
    95                 argptr++;
     96                psh->argptr++;
    9697        for (i = 0; i < NOPTS; i++)
    97                 optlist[i].val = 2;
    98         options(1);
    99         if (*argptr == NULL && minusc == NULL)
    100                 sflag = 1;
    101         if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
    102                 iflag = 1;
    103         if (mflag == 2)
    104                 mflag = iflag;
     98                psh->optlist[i].val = 2;
     99        options(psh, 1);
     100        if (*psh->argptr == NULL && psh->minusc == NULL)
     101                sflag(psh) = 1;
     102        if (iflag(psh) == 2 && sflag(psh) == 1 && shfile_isatty(&psh->fdtab, 0) && shfile_isatty(&psh->fdtab, 1))
     103                iflag(psh) = 1;
     104        if (mflag(psh) == 2)
     105                mflag(psh) = iflag(psh);
    105106        for (i = 0; i < NOPTS; i++)
    106                 if (optlist[i].val == 2)
    107                         optlist[i].val = 0;
     107                if (psh->optlist[i].val == 2)
     108                        psh->optlist[i].val = 0;
    108109#if DEBUG == 2
    109         debug = 1;
    110 #endif
    111         arg0 = argv[0];
    112         if (sflag == 0 && minusc == NULL) {
    113                 commandname = argv[0];
    114                 arg0 = *argptr++;
    115                 setinputfile(arg0, 0);
    116                 commandname = arg0;
     110        debug(psh) = 1;
     111#endif
     112        psh->arg0 = argv[0];
     113        if (sflag(psh) == 0 && psh->minusc == NULL) {
     114                psh->commandname = argv[0];
     115                psh->arg0 = *psh->argptr++;
     116                setinputfile(psh, psh->arg0, 0);
     117                psh->commandname = psh->arg0;
    117118        }
    118119        /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
    119         if (minusc != NULL) {
    120                 if (argptr == NULL || *argptr == NULL)
    121                         error("Bad -c option");
    122                 minusc = *argptr++;
    123                 if (*argptr != 0)
    124                         arg0 = *argptr++;
    125         }
    126 
    127         shellparam.p = argptr;
    128         shellparam.reset = 1;
     120        if (psh->minusc != NULL) {
     121                if (psh->argptr == NULL || *psh->argptr == NULL)
     122                        error(psh, "Bad -c option");
     123                psh->minusc = *psh->argptr++;
     124                if (*psh->argptr != 0)
     125                        psh->arg0 = *psh->argptr++;
     126        }
     127
     128        psh->shellparam.p = psh->argptr;
     129        psh->shellparam.reset = 1;
    129130        /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
    130         while (*argptr) {
    131                 shellparam.nparam++;
    132                 argptr++;
    133         }
    134         optschanged();
     131        while (*psh->argptr) {
     132                psh->shellparam.nparam++;
     133                psh->argptr++;
     134        }
     135        optschanged(psh);
    135136}
    136137
    137138
    138139void
    139 optschanged(void)
    140 {
    141         setinteractive(iflag);
     140optschanged(shinstance *psh)
     141{
     142        setinteractive(psh, iflag(psh));
    142143#ifndef SMALL
    143         histedit();
    144 #endif
    145         setjobctl(mflag);
     144        histedit(psh);
     145#endif
     146        setjobctl(psh, mflag(psh));
    146147}
    147148
     
    152153
    153154STATIC void
    154 options(int cmdline)
     155options(shinstance *psh, int cmdline)
    155156{
    156157        static char empty[] = "";
     
    160161
    161162        if (cmdline)
    162                 minusc = NULL;
    163         while ((p = *argptr) != NULL) {
    164                 argptr++;
     163                psh->minusc = NULL;
     164        while ((p = *psh->argptr) != NULL) {
     165                psh->argptr++;
    165166                if ((c = *p++) == '-') {
    166167                        val = 1;
     
    169170                                        /* "-" means turn off -x and -v */
    170171                                        if (p[0] == '\0')
    171                                                 xflag = vflag = 0;
     172                                                xflag(psh) = vflag(psh) = 0;
    172173                                        /* "--" means reset params */
    173                                         else if (*argptr == NULL)
    174                                                 setparam(argptr);
     174                                        else if (*psh->argptr == NULL)
     175                                                setparam(psh, psh->argptr);
    175176                                }
    176177                                break;    /* "-" or  "--" terminates options */
     
    179180                        val = 0;
    180181                } else {
    181                         argptr--;
     182                        psh->argptr--;
    182183                        break;
    183184                }
     
    185186                        if (c == 'c' && cmdline) {
    186187                                /* command is after shell args*/
    187                                 minusc = empty;
     188                                psh->minusc = empty;
    188189                        } else if (c == 'o') {
    189                                 minus_o(*argptr, val);
    190                                 if (*argptr)
    191                                         argptr++;
     190                                minus_o(psh, *psh->argptr, val);
     191                                if (*psh->argptr)
     192                                        psh->argptr++;
    192193                        } else {
    193                                 setoption(c, val);
     194                                setoption(psh, c, val);
    194195                        }
    195196                }
     
    198199
    199200static void
    200 set_opt_val(int i, int val)
     201set_opt_val(shinstance *psh, int i, int val)
    201202{
    202203        int j;
    203204        int flag;
    204205
    205         if (val && (flag = optlist[i].opt_set)) {
     206        if (val && (flag = psh->optlist[i].opt_set)) {
    206207                /* some options (eg vi/emacs) are mutually exclusive */
    207208                for (j = 0; j < NOPTS; j++)
    208                     if (optlist[j].opt_set == flag)
    209                         optlist[j].val = 0;
    210         }
    211         optlist[i].val = val;
     209                    if (psh->optlist[j].opt_set == flag)
     210                        psh->optlist[j].val = 0;
     211        }
     212        psh->optlist[i].val = val;
    212213#ifdef DEBUG
    213         if (&optlist[i].val == &debug)
     214        if (&psh->optlist[i].val == &debug(psh))
    214215                opentrace();
    215216#endif
     
    217218
    218219STATIC void
    219 minus_o(char *name, int val)
     220minus_o(shinstance *psh, char *name, int val)
    220221{
    221222        int i;
    222223
    223224        if (name == NULL) {
    224                 out1str("Current option settings\n");
     225                out1str(psh, "Current option settings\n");
    225226                for (i = 0; i < NOPTS; i++)
    226                         out1fmt("%-16s%s\n", optlist[i].name,
    227                                 optlist[i].val ? "on" : "off");
     227                        out1fmt(psh, "%-16s%s\n", psh->optlist[i].name,
     228                                psh->optlist[i].val ? "on" : "off");
    228229        } else {
    229230                for (i = 0; i < NOPTS; i++)
    230                         if (equal(name, optlist[i].name)) {
    231                                 set_opt_val(i, val);
     231                        if (equal(name, psh->optlist[i].name)) {
     232                                set_opt_val(psh, i, val);
    232233                                return;
    233234                        }
    234                 error("Illegal option -o %s", name);
     235                error(psh, "Illegal option -o %s", name);
    235236        }
    236237}
     
    238239
    239240STATIC void
    240 setoption(int flag, int val)
     241setoption(shinstance *psh, int flag, int val)
    241242{
    242243        int i;
    243244
    244245        for (i = 0; i < NOPTS; i++)
    245                 if (optlist[i].letter == flag) {
    246                         set_opt_val( i, val );
     246                if (psh->optlist[i].letter == flag) {
     247                        set_opt_val(psh, i, val );
    247248                        return;
    248249                }
    249         error("Illegal option -%c", flag);
     250        error(psh, "Illegal option -%c", flag);
    250251        /* NOTREACHED */
    251252}
     
    259260        int i;
    260261
    261         for (i = 0; optlist[i].name; i++)
    262                 optlist[i].val = 0;
    263         optschanged();
     262        for (i = 0; psh->optlist[i].name; i++)
     263                psh->optlist[i].val = 0;
     264        optschanged(psh);
    264265
    265266}
     
    272273
    273274void
    274 setparam(char **argv)
     275setparam(shinstance *psh, char **argv)
    275276{
    276277        char **newparam;
     
    285286        }
    286287        *ap = NULL;
    287         freeparam(&shellparam);
    288         shellparam.malloc = 1;
    289         shellparam.nparam = nparam;
    290         shellparam.p = newparam;
    291         shellparam.optnext = NULL;
     288        freeparam(&psh->shellparam);
     289        psh->shellparam.malloc = 1;
     290        psh->shellparam.nparam = nparam;
     291        psh->shellparam.p = newparam;
     292        psh->shellparam.optnext = NULL;
    292293}
    293294
     
    316317
    317318int
    318 shiftcmd(int argc, char **argv)
     319shiftcmd(shinstance *psh, int argc, char **argv)
    319320{
    320321        int n;
     
    323324        n = 1;
    324325        if (argc > 1)
    325                 n = number(argv[1]);
    326         if (n > shellparam.nparam)
    327                 error("can't shift that many");
     326                n = number(psh, argv[1]);
     327        if (n > psh->shellparam.nparam)
     328                error(psh, "can't shift that many");
    328329        INTOFF;
    329         shellparam.nparam -= n;
    330         for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
    331                 if (shellparam.malloc)
     330        psh->shellparam.nparam -= n;
     331        for (ap1 = psh->shellparam.p ; --n >= 0 ; ap1++) {
     332                if (psh->shellparam.malloc)
    332333                        ckfree(*ap1);
    333334        }
    334         ap2 = shellparam.p;
     335        ap2 = psh->shellparam.p;
    335336        while ((*ap2++ = *ap1++) != NULL);
    336         shellparam.optnext = NULL;
     337        psh->shellparam.optnext = NULL;
    337338        INTON;
    338339        return 0;
     
    346347
    347348int
    348 setcmd(int argc, char **argv)
     349setcmd(shinstance *psh, int argc, char **argv)
    349350{
    350351        if (argc == 1)
    351                 return showvars(0, 0, 1);
     352                return showvars(psh, 0, 0, 1);
    352353        INTOFF;
    353         options(0);
    354         optschanged();
    355         if (*argptr != NULL) {
    356                 setparam(argptr);
     354        options(psh, 0);
     355        optschanged(psh);
     356        if (*psh->argptr != NULL) {
     357                setparam(psh, psh->argptr);
    357358        }
    358359        INTON;
     
    362363
    363364void
    364 getoptsreset(value)
    365         const char *value;
    366 {
    367         if (number(value) == 1) {
    368                 shellparam.optnext = NULL;
    369                 shellparam.reset = 1;
     365getoptsreset(shinstance *psh, const char *value)
     366{
     367        if (number(psh, value) == 1) {
     368                psh->shellparam.optnext = NULL;
     369                psh->shellparam.reset = 1;
    370370        }
    371371}
     
    379379
    380380int
    381 getoptscmd(int argc, char **argv)
     381getoptscmd(shinstance *psh, int argc, char **argv)
    382382{
    383383        char **optbase;
    384384
    385385        if (argc < 3)
    386                 error("usage: getopts optstring var [arg]");
     386                error(psh, "usage: getopts optstring var [arg]");
    387387        else if (argc == 3)
    388                 optbase = shellparam.p;
     388                optbase = psh->shellparam.p;
    389389        else
    390390                optbase = &argv[3];
    391391
    392         if (shellparam.reset == 1) {
    393                 shellparam.optnext = optbase;
    394                 shellparam.optptr = NULL;
    395                 shellparam.reset = 0;
    396         }
    397 
    398         return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
    399                        &shellparam.optptr);
     392        if (psh->shellparam.reset == 1) {
     393                psh->shellparam.optnext = optbase;
     394                psh->shellparam.optptr = NULL;
     395                psh->shellparam.reset = 0;
     396        }
     397
     398        return getopts(psh, argv[1], argv[2], optbase, &psh->shellparam.optnext,
     399                       &psh->shellparam.optptr);
    400400}
    401401
    402402STATIC int
    403 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr)
     403getopts(shinstance *psh, char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr)
    404404{
    405405        char *p, *q;
     
    417417                if (p == NULL || *p != '-' || *++p == '\0') {
    418418atend:
    419                         ind = *optnext - optfirst + 1;
     419                        ind = (int)(*optnext - optfirst + 1);
    420420                        *optnext = NULL;
    421421                        p = NULL;
     
    434434                                s[0] = c;
    435435                                s[1] = '\0';
    436                                 err |= setvarsafe("OPTARG", s, 0);
     436                                err |= setvarsafe(psh, "OPTARG", s, 0);
    437437                        } else {
    438                                 outfmt(&errout, "Illegal option -%c\n", c);
    439                                 (void) unsetvar("OPTARG", 0);
     438                                outfmt(&psh->errout, "Illegal option -%c\n", c);
     439                                (void) unsetvar(psh, "OPTARG", 0);
    440440                        }
    441441                        c = '?';
     
    451451                                s[0] = c;
    452452                                s[1] = '\0';
    453                                 err |= setvarsafe("OPTARG", s, 0);
     453                                err |= setvarsafe(psh, "OPTARG", s, 0);
    454454                                c = ':';
    455455                        } else {
    456                                 outfmt(&errout, "No arg for -%c option\n", c);
    457                                 (void) unsetvar("OPTARG", 0);
     456                                outfmt(&psh->errout, "No arg for -%c option\n", c);
     457                                (void) unsetvar(psh, "OPTARG", 0);
    458458                                c = '?';
    459459                        }
     
    463463                if (p == **optnext)
    464464                        (*optnext)++;
    465                 err |= setvarsafe("OPTARG", p, 0);
     465                err |= setvarsafe(psh, "OPTARG", p, 0);
    466466                p = NULL;
    467467        } else
    468                 err |= setvarsafe("OPTARG", "", 0);
    469         ind = *optnext - optfirst + 1;
     468                err |= setvarsafe(psh, "OPTARG", "", 0);
     469        ind = (int)(*optnext - optfirst + 1);
    470470        goto out;
    471471
     
    477477        *optpptr = p;
    478478        fmtstr(s, sizeof(s), "%d", ind);
    479         err |= setvarsafe("OPTIND", s, VNOFUNC);
     479        err |= setvarsafe(psh, "OPTIND", s, VNOFUNC);
    480480        s[0] = c;
    481481        s[1] = '\0';
    482         err |= setvarsafe(optvar, s, 0);
     482        err |= setvarsafe(psh, optvar, s, 0);
    483483        if (err) {
    484484                *optnext = NULL;
    485485                *optpptr = NULL;
    486                 output_flushall();
    487                 exraise(EXERROR);
     486                output_flushall(psh);
     487                exraise(psh, EXERROR);
    488488        }
    489489        return done;
     
    502502
    503503int
    504 nextopt(const char *optstring)
     504nextopt(shinstance *psh, const char *optstring)
    505505{
    506506        char *p;
     
    508508        char c;
    509509
    510         if ((p = optptr) == NULL || *p == '\0') {
    511                 p = *argptr;
     510        if ((p = psh->optptr) == NULL || *p == '\0') {
     511                p = *psh->argptr;
    512512                if (p == NULL || *p != '-' || *++p == '\0')
    513513                        return '\0';
    514                 argptr++;
     514                psh->argptr++;
    515515                if (p[0] == '-' && p[1] == '\0')        /* check for "--" */
    516516                        return '\0';
     
    519519        for (q = optstring ; *q != c ; ) {
    520520                if (*q == '\0')
    521                         error("Illegal option -%c", c);
     521                        error(psh, "Illegal option -%c", c);
    522522                if (*++q == ':')
    523523                        q++;
    524524        }
    525525        if (*++q == ':') {
    526                 if (*p == '\0' && (p = *argptr++) == NULL)
    527                         error("No arg for -%c option", c);
    528                 optionarg = p;
     526                if (*p == '\0' && (p = *psh->argptr++) == NULL)
     527                        error(psh, "No arg for -%c option", c);
     528                psh->optionarg = p;
    529529                p = NULL;
    530530        }
    531         optptr = p;
     531        psh->optptr = p;
    532532        return c;
    533533}
Note: See TracChangeset for help on using the changeset viewer.