VirtualBox

Changeset 1201 in kBuild


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

input.c

Location:
trunk/src/kash
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/Makefile.kmk

    r1199 r1201  
    4444        syntax.c \
    4545        output.c \
     46        input.c \
    4647        \
    4748        exec.c \
    4849        expand.c \
    49         input.c \
    5050        jobs.c \
    5151        miscbltin.c \
  • trunk/src/kash/bltin/bltin.h

    r1199 r1201  
    5151#undef putchar
    5252#undef fileno
    53 #define stdout out1
    54 #define stderr out2
     53#define stdout psh->out1
     54#define stderr psh->out2
    5555#define printf out1fmt
    5656#define putc(c, file)   outc(c, file)
  • trunk/src/kash/exec.c

    r1200 r1201  
    218218                        exraise(psh, EXSHELLPROC);
    219219                }
    220                 initshellproc();
    221                 setinputfile(cmd, 0);
     220                initshellproc(psh);
     221                setinputfile(psh, cmd, 0);
    222222                commandname = arg0 = savestr(argv[0]);
    223223#ifdef EXEC_HASH_BANG_SCRIPT
    224                 pgetc(); pungetc();             /* fill up input buffer */
     224                pgetc(psh); pungetc(psh);               /* fill up input buffer */
    225225                p = parsenextc;
    226226                if (parsenleft > 2 && p[0] == '#' && p[1] == '!') {
     
    703703                delete_cmd_entry();
    704704        if (act & DO_ERR)
    705                 outfmt(out2, "%s: %s\n", name, errmsg(psh, e, E_EXEC));
     705                outfmt(psh->out2, "%s: %s\n", name, errmsg(psh, e, E_EXEC));
    706706        entry->cmdtype = CMDUNKNOWN;
    707707        return;
  • trunk/src/kash/expand.c

    r1200 r1201  
    122122
    123123void
    124 expandhere(union node *arg, int fd)
     124expandhere(shinstance *psh, union node *arg, int fd)
    125125{
    126126        psh->herefd = fd;
    127         expandarg(arg, (struct arglist *)NULL, 0);
    128         xwrite(psh, fd, stackblock(psh), expdest - stackblock(psh));
     127        expandarg(psh, arg, (struct arglist *)NULL, 0);
     128        xwrite(psh, fd, stackblock(psh), psh->expdest - stackblock(psh));
    129129}
    130130
     
    164164        } else {
    165165                if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
    166                         rmescapes(p);
     166                        rmescapes(psh, p);
    167167                sp = (struct strlist *)stalloc(psh, sizeof (struct strlist));
    168168                sp->text = p;
     
    231231                        break;
    232232                case CTLENDARI:
    233                         expari(flag);
     233                        expari(psh, flag);
    234234                        break;
    235235                case ':':
     
    398398        removerecordregions(begoff);
    399399        if (quotes)
    400                 rmescapes(p+2);
     400                rmescapes(psh, p+2);
    401401        result = arith(p+2);
    402402        fmtstr(p, 12, "%d", result);
     
    522522        case VSQUESTION:
    523523                if (*p != CTLENDVAR) {
    524                         outfmt(&errout, "%s\n", startp);
     524                        outfmt(&psh->errout, "%s\n", startp);
    525525                        error(psh, (char *)NULL);
    526526                }
     
    534534                        c = *loc;
    535535                        *loc = '\0';
    536                         if (patmatch(str, startp, varflags & VSQUOTE))
     536                        if (patmatch(psh, str, startp, varflags & VSQUOTE))
    537537                                goto recordleft;
    538538                        *loc = c;
     
    546546                        c = *loc;
    547547                        *loc = '\0';
    548                         if (patmatch(str, startp, varflags & VSQUOTE))
     548                        if (patmatch(psh, str, startp, varflags & VSQUOTE))
    549549                                goto recordleft;
    550550                        *loc = c;
     
    563563        case VSTRIMRIGHT:
    564564                for (loc = str - 1; loc >= startp;) {
    565                         if (patmatch(str, loc, varflags & VSQUOTE))
     565                        if (patmatch(psh, str, loc, varflags & VSQUOTE))
    566566                                goto recordright;
    567567                        loc--;
     
    579579        case VSTRIMRIGHTMAX:
    580580                for (loc = startp; loc < str - 1; loc++) {
    581                         if (patmatch(str, loc, varflags & VSQUOTE))
     581                        if (patmatch(psh, str, loc, varflags & VSQUOTE))
    582582                                goto recordright;
    583583                        if ((varflags & VSQUOTE) && *loc == CTLESC)
     
    11111111nometa:
    11121112                        *exparg.lastp = str;
    1113                         rmescapes(str->text);
     1113                        rmescapes(psh, str->text);
    11141114                        exparg.lastp = &str->next;
    11151115                } else {
     
    12351235                if (dp->d_name[0] == '.' && ! matchdot)
    12361236                        continue;
    1237                 if (patmatch(start, dp->d_name, 0)) {
     1237                if (patmatch(psh, start, dp->d_name, 0)) {
    12381238                        if (atend) {
    12391239                                scopy(dp->d_name, enddir);
     
    15161516        STPUTC(psh, '\0', expdest);
    15171517        p = grabstackstr(psh, expdest);
    1518         result = patmatch(p, val, 0);
     1518        result = patmatch(psh, p, val, 0);
    15191519        popstackmark(psh, &smark);
    15201520        return result;
     
    15511551
    15521552int
    1553 wordexpcmd(int argc, char **argv)
     1553wordexpcmd(shinstance *psh, int argc, char **argv)
    15541554{
    15551555        size_t len;
  • trunk/src/kash/generated/init.c

    r1200 r1201  
    173173
    174174void
    175 init() {
     175init(shinstance *psh) {
    176176
    177177      /* from exec.c: */
     
    206206
    207207void
    208 reset() {
     208reset(shinstance *psh) {
    209209
    210210      /* from eval.c: */
     
    219219              if (exception != EXSHELLPROC)
    220220                      parselleft = parsenleft = 0;      /* clear input buffer */
    221               popallfiles();
     221              popallfiles(psh);
    222222      }
    223223
    224224      /* from output.c: */
    225225      {
    226               out1 = &output;
    227               out2 = &errout;
     226              psh->out1 = &psh->output;
     227              psh->out2 = &psh->errout;
    228228              if (memout.buf != NULL) {
    229229                      ckfree(memout.buf);
     
    252252
    253253void
    254 initshellproc() {
     254initshellproc(shinstance *psh) {
    255255
    256256      /* from alias.c: */
     
    271271      /* from input.c: */
    272272      {
    273               popallfiles();
     273              popallfiles(psh);
    274274      }
    275275
  • trunk/src/kash/histedit.c

    r1200 r1201  
    196196        if (el != NULL && term != NULL)
    197197                if (el_set(el, EL_TERMINAL, term) != 0) {
    198                         outfmt(out2, "sh: Can't set terminal type %s\n", term);
    199                         outfmt(out2, "sh: Using dumb terminal settings.\n");
     198                        outfmt(psh->out2, "sh: Can't set terminal type %s\n", term);
     199                        outfmt(psh->out2, "sh: Using dumb terminal settings.\n");
    200200                }
    201201}
  • trunk/src/kash/input.c

    r1199 r1201  
    6969#include "parser.h"
    7070#include "myhistedit.h"
     71#include "shinstance.h"
    7172
    7273#define EOF_NLEFT -99           /* value of parsenleft when EOF pushed back */
    7374
    74 MKINIT
    75 struct strpush {
    76         struct strpush *prev;   /* preceding string on stack */
    77         char *prevstring;
    78         int prevnleft;
    79         int prevlleft;
    80         struct alias *ap;       /* if push was associated with an alias */
    81 };
    82 
    83 /*
    84  * The parsefile structure pointed to by the global variable parsefile
    85  * contains information about the current file being read.
    86  */
    87 
    88 MKINIT
    89 struct parsefile {
    90         struct parsefile *prev; /* preceding file on stack */
    91         int linno;              /* current line */
    92         int fd;                 /* file descriptor (or -1 if string) */
    93         int nleft;              /* number of chars left in this line */
    94         int lleft;              /* number of chars left in this buffer */
    95         char *nextc;            /* next char in buffer */
    96         char *buf;              /* input buffer */
    97         struct strpush *strpush; /* for pushing strings at this level */
    98         struct strpush basestrpush; /* so pushing one is fast */
    99 };
    100 
    101 
    102 int plinno = 1;                 /* input line number */
    103 int parsenleft;                 /* copy of parsefile->nleft */
    104 MKINIT int parselleft;          /* copy of parsefile->lleft */
    105 char *parsenextc;               /* copy of parsefile->nextc */
    106 MKINIT struct parsefile basepf; /* top level input file */
    107 MKINIT char basebuf[BUFSIZ];    /* buffer for top level input file */
    108 struct parsefile *parsefile = &basepf;  /* current input file */
    109 int init_editline = 0;          /* editline library initialized? */
    110 int whichprompt;                /* 1 == PS1, 2 == PS2 */
    111 
    112 #ifndef SMALL
    113 EditLine *el;                   /* cookie for editline package */
    114 #endif
    115 
    116 STATIC void pushfile(void);
    117 static int preadfd(void);
     75//MKINIT
     76//struct strpush {
     77//      struct strpush *prev;   /* preceding string on stack */
     78//      char *prevstring;
     79//      int prevnleft;
     80//      int prevlleft;
     81//      struct alias *ap;       /* if push was associated with an alias */
     82//};
     83//
     84///*
     85// * The parsefile structure pointed to by the global variable parsefile
     86// * contains information about the current file being read.
     87// */
     88//
     89//MKINIT
     90//struct parsefile {
     91//      struct parsefile *prev; /* preceding file on stack */
     92//      int linno;              /* current line */
     93//      int fd;                 /* file descriptor (or -1 if string) */
     94//      int nleft;              /* number of chars left in this line */
     95//      int lleft;              /* number of chars left in this buffer */
     96//      char *nextc;            /* next char in buffer */
     97//      char *buf;              /* input buffer */
     98//      struct strpush *strpush; /* for pushing strings at this level */
     99//      struct strpush basestrpush; /* so pushing one is fast */
     100//};
     101//
     102//
     103//int plinno = 1;                       /* input line number */
     104//int parsenleft;                       /* copy of parsefile->nleft */
     105//MKINIT int parselleft;                /* copy of parsefile->lleft */
     106//char *parsenextc;             /* copy of parsefile->nextc */
     107//MKINIT struct parsefile basepf;       /* top level input file */
     108//MKINIT char basebuf[BUFSIZ];  /* buffer for top level input file */
     109//struct parsefile *parsefile = &basepf;        /* current input file */
     110//int init_editline = 0;                /* editline library initialized? */
     111//int whichprompt;              /* 1 == PS1, 2 == PS2 */
     112//
     113//#ifndef SMALL
     114//EditLine *el;                 /* cookie for editline package */
     115//#endif
     116
     117STATIC void pushfile(shinstance *psh);
     118static int preadfd(shinstance *psh);
    118119
    119120#ifdef mkinit
     
    123124
    124125INIT {
    125         basepf.nextc = basepf.buf = basebuf;
     126        psh->basepf.nextc = psh->basepf.buf = psh->basebuf;
    126127}
    127128
    128129RESET {
    129         if (exception != EXSHELLPROC)
    130                 parselleft = parsenleft = 0;    /* clear input buffer */
    131         popallfiles();
     130        if (psh->exception != EXSHELLPROC)
     131                psh->parselleft = psh->parsenleft = 0;  /* clear input buffer */
     132        popallfiles(psh);
    132133}
    133134
    134135SHELLPROC {
    135         popallfiles();
     136        popallfiles(psh);
    136137}
    137138#endif
     
    143144
    144145char *
    145 pfgets(char *line, int len)
     146pfgets(shinstance *psh, char *line, int len)
    146147{
    147148        char *p = line;
     
    150151
    151152        while (--nleft > 0) {
    152                 c = pgetc_macro();
     153                c = pgetc_macro(psh);
    153154                if (c == PEOF) {
    154155                        if (p == line)
     
    172173
    173174int
    174 pgetc(void)
    175 {
    176         return pgetc_macro();
     175pgetc(shinstance *psh)
     176{
     177        return pgetc_macro(psh);
    177178}
    178179
    179180
    180181static int
    181 preadfd(void)
     182preadfd(shinstance *psh)
    182183{
    183184        int nr;
    184         char *buf =  parsefile->buf;
    185         parsenextc = buf;
     185        char *buf = psh->parsefile->buf;
     186        psh->parsenextc = buf;
    186187
    187188retry:
    188189#ifndef SMALL
    189         if (parsefile->fd == 0 && el) {
     190        if (psh->parsefile->fd == 0 && psh->el) {
    190191                static const char *rl_cp;
    191192                static int el_len;
    192193
    193194                if (rl_cp == NULL)
    194                         rl_cp = el_gets(el, &el_len);
     195                        rl_cp = el_gets(psh->el, &el_len);
    195196                if (rl_cp == NULL)
    196197                        nr = 0;
     
    209210        } else
    210211#endif
    211                 nr = read(parsefile->fd, buf, BUFSIZ - 8);
     212                nr = shfile_read(&psh->fdtab, psh->parsefile->fd, buf, BUFSIZ - 8);
    212213
    213214
     
    216217                        if (errno == EINTR)
    217218                                goto retry;
    218                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
    219                                 int flags = fcntl(0, F_GETFL, 0);
     219                        if (psh->parsefile->fd == 0 && errno == EWOULDBLOCK) {
     220                                int flags = shfile_fcntl(&psh->fdtab, 0, F_GETFL, 0);
    220221                                if (flags >= 0 && flags & O_NONBLOCK) {
    221222                                        flags &=~ O_NONBLOCK;
    222                                         if (fcntl(0, F_SETFL, flags) >= 0) {
     223                                        if (shfile_fcntl(&psh->fdtab, 0, F_SETFL, flags) >= 0) {
    223224                                                out2str(psh, "sh: turning off NDELAY mode\n");
    224225                                                goto retry;
     
    243244
    244245int
    245 preadbuffer(void)
     246preadbuffer(shinstance *psh)
    246247{
    247248        char *p, *q;
     
    250251        char savec;
    251252
    252         if (parsefile->strpush) {
    253                 popstring();
    254                 if (--parsenleft >= 0)
    255                         return (*parsenextc++);
    256         }
    257         if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
     253        if (psh->parsefile->strpush) {
     254                popstring(psh);
     255                if (--psh->parsenleft >= 0)
     256                        return (*psh->parsenextc++);
     257        }
     258        if (psh->parsenleft == EOF_NLEFT || psh->parsefile->buf == NULL)
    258259                return PEOF;
    259         flushout(&output);
    260         flushout(&errout);
     260        flushout(&psh->output);
     261        flushout(&psh->errout);
    261262
    262263again:
    263         if (parselleft <= 0) {
    264                 if ((parselleft = preadfd()) == -1) {
    265                         parselleft = parsenleft = EOF_NLEFT;
     264        if (psh->parselleft <= 0) {
     265                if ((psh->parselleft = preadfd(psh)) == -1) {
     266                        psh->parselleft = psh->parsenleft = EOF_NLEFT;
    266267                        return PEOF;
    267268                }
    268269        }
    269270
    270         q = p = parsenextc;
     271        q = p = psh->parsenextc;
    271272
    272273        /* delete nul characters */
     
    283284
    284285                case '\n':
    285                         parsenleft = q - parsenextc;
     286                        psh->parsenleft = (int)(q - psh->parsenextc);
    286287                        more = 0; /* Stop processing here */
    287288                        break;
     
    294295                *q++ = *p++;
    295296check:
    296                 if (--parselleft <= 0) {
    297                         parsenleft = q - parsenextc - 1;
    298                         if (parsenleft < 0)
     297                if (--psh->parselleft <= 0) {
     298                        psh->parsenleft = (int)(q - psh->parsenextc - 1);
     299                        if (psh->parsenleft < 0)
    299300                                goto again;
    300301                        *q = '\0';
     
    307308
    308309#ifndef SMALL
    309         if (parsefile->fd == 0 && hist && something) {
     310        if (psh->parsefile->fd == 0 && hist && something) {
    310311                HistEvent he;
    311312                INTOFF;
    312                 history(hist, &he, whichprompt == 1? H_ENTER : H_APPEND,
    313                     parsenextc);
     313                history(hist, &he, psh->whichprompt == 1? H_ENTER : H_APPEND,
     314                    psh->parsenextc);
    314315                INTON;
    315316        }
     
    317318
    318319        if (vflag(psh)) {
    319                 out2str(psh, parsenextc);
    320                 flushout(out2);
     320                out2str(psh, psh->parsenextc);
     321                flushout(psh->out2);
    321322        }
    322323
    323324        *q = savec;
    324325
    325         return *parsenextc++;
     326        return *psh->parsenextc++;
    326327}
    327328
     
    332333
    333334void
    334 pungetc(void)
    335 {
    336         parsenleft++;
    337         parsenextc--;
     335pungetc(shinstance *psh)
     336{
     337        psh->parsenleft++;
     338        psh->parsenextc--;
    338339}
    339340
     
    343344 */
    344345void
    345 pushstring(char *s, int len, void *ap)
     346pushstring(shinstance *psh, char *s, int len, void *ap)
    346347{
    347348        struct strpush *sp;
     
    349350        INTOFF;
    350351/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
    351         if (parsefile->strpush) {
     352        if (psh->parsefile->strpush) {
    352353                sp = ckmalloc(sizeof (struct strpush));
    353                 sp->prev = parsefile->strpush;
    354                 parsefile->strpush = sp;
     354                sp->prev = psh->parsefile->strpush;
     355                psh->parsefile->strpush = sp;
    355356        } else
    356                 sp = parsefile->strpush = &(parsefile->basestrpush);
    357         sp->prevstring = parsenextc;
    358         sp->prevnleft = parsenleft;
    359         sp->prevlleft = parselleft;
     357                sp = psh->parsefile->strpush = &(psh->parsefile->basestrpush);
     358        sp->prevstring = psh->parsenextc;
     359        sp->prevnleft = psh->parsenleft;
     360        sp->prevlleft = psh->parselleft;
    360361        sp->ap = (struct alias *)ap;
    361362        if (ap)
    362363                ((struct alias *)ap)->flag |= ALIASINUSE;
    363         parsenextc = s;
    364         parsenleft = len;
     364        psh->parsenextc = s;
     365        psh->parsenleft = len;
    365366        INTON;
    366367}
    367368
    368369void
    369 popstring(void)
    370 {
    371         struct strpush *sp = parsefile->strpush;
     370popstring(shinstance *psh)
     371{
     372        struct strpush *sp = psh->parsefile->strpush;
    372373
    373374        INTOFF;
    374         parsenextc = sp->prevstring;
    375         parsenleft = sp->prevnleft;
    376         parselleft = sp->prevlleft;
    377 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
     375        psh->parsenextc = sp->prevstring;
     376        psh->parsenleft = sp->prevnleft;
     377        psh->parselleft = sp->prevlleft;
     378/*dprintf("*** calling popstring: restoring to '%s'\n", psh->parsenextc);*/
    378379        if (sp->ap)
    379380                sp->ap->flag &= ~ALIASINUSE;
    380         parsefile->strpush = sp->prev;
    381         if (sp != &(parsefile->basestrpush))
     381        psh->parsefile->strpush = sp->prev;
     382        if (sp != &(psh->parsefile->basestrpush))
    382383                ckfree(sp);
    383384        INTON;
     
    390391
    391392void
    392 setinputfile(const char *fname, int push)
     393setinputfile(shinstance *psh, const char *fname, int push)
    393394{
    394395        int fd;
     
    396397
    397398        INTOFF;
    398         if ((fd = open(fname, O_RDONLY)) < 0)
     399/** @todo shfile fixme */
     400        if ((fd = shfile_open(&psh->fdtab, fname, O_RDONLY)) < 0)
    399401                error(psh, "Can't open %s", fname);
    400402        if (fd < 10) {
    401                 fd2 = copyfd(fd, 10);
    402                 close(fd);
     403                fd2 = copyfd(psh, fd, 10);
     404                shfile_close(&psh->fdtab, fd);
    403405                if (fd2 < 0)
    404406                        error(psh, "Out of file descriptors");
    405407                fd = fd2;
    406408        }
    407         setinputfd(fd, push);
     409        setinputfd(psh, fd, push);
    408410        INTON;
    409411}
     
    416418
    417419void
    418 setinputfd(int fd, int push)
    419 {
    420         (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
     420setinputfd(shinstance *psh, int fd, int push)
     421{
     422        (void) shfile_fcntl(&psh->fdtab, fd, F_SETFD, FD_CLOEXEC);
    421423        if (push) {
    422                 pushfile();
    423                 parsefile->buf = ckmalloc(BUFSIZ);
    424         }
    425         if (parsefile->fd > 0)
    426                 close(parsefile->fd);
    427         parsefile->fd = fd;
    428         if (parsefile->buf == NULL)
    429                 parsefile->buf = ckmalloc(BUFSIZ);
    430         parselleft = parsenleft = 0;
    431         plinno = 1;
     424                pushfile(psh);
     425                psh->parsefile->buf = ckmalloc(BUFSIZ);
     426        }
     427        if (psh->parsefile->fd > 0)
     428                shfile_close(&psh->fdtab, psh->parsefile->fd);
     429        psh->parsefile->fd = fd;
     430        if (psh->parsefile->buf == NULL)
     431                psh->parsefile->buf = ckmalloc(BUFSIZ);
     432        psh->parselleft = psh->parsenleft = 0;
     433        psh->plinno = 1;
    432434}
    433435
     
    438440
    439441void
    440 setinputstring(char *string, int push)
     442setinputstring(shinstance *psh, char *string, int push)
    441443{
    442444        INTOFF;
    443445        if (push)
    444                 pushfile();
    445         parsenextc = string;
    446         parselleft = parsenleft = strlen(string);
    447         parsefile->buf = NULL;
    448         plinno = 1;
     446                pushfile(psh);
     447        psh->parsenextc = string;
     448        psh->parselleft = psh->parsenleft = (int)strlen(string);
     449        psh->parsefile->buf = NULL;
     450        psh->plinno = 1;
    449451        INTON;
    450452}
     
    458460
    459461STATIC void
    460 pushfile(void)
     462pushfile(shinstance *psh)
    461463{
    462464        struct parsefile *pf;
    463465
    464         parsefile->nleft = parsenleft;
    465         parsefile->lleft = parselleft;
    466         parsefile->nextc = parsenextc;
    467         parsefile->linno = plinno;
     466        psh->parsefile->nleft = psh->parsenleft;
     467        psh->parsefile->lleft = psh->parselleft;
     468        psh->parsefile->nextc = psh->parsenextc;
     469        psh->parsefile->linno = psh->plinno;
    468470        pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
    469         pf->prev = parsefile;
     471        pf->prev = psh->parsefile;
    470472        pf->fd = -1;
    471473        pf->strpush = NULL;
    472474        pf->basestrpush.prev = NULL;
    473         parsefile = pf;
    474 }
    475 
    476 
    477 void
    478 popfile(void)
    479 {
    480         struct parsefile *pf = parsefile;
     475        psh->parsefile = pf;
     476}
     477
     478
     479void
     480popfile(shinstance *psh)
     481{
     482        struct parsefile *pf = psh->parsefile;
    481483
    482484        INTOFF;
    483485        if (pf->fd >= 0)
    484                 close(pf->fd);
     486                shfile_close(&psh->fdtab, pf->fd);
    485487        if (pf->buf)
    486488                ckfree(pf->buf);
    487489        while (pf->strpush)
    488                 popstring();
    489         parsefile = pf->prev;
     490                popstring(psh);
     491        psh->parsefile = pf->prev;
    490492        ckfree(pf);
    491         parsenleft = parsefile->nleft;
    492         parselleft = parsefile->lleft;
    493         parsenextc = parsefile->nextc;
    494         plinno = parsefile->linno;
     493        psh->parsenleft = psh->parsefile->nleft;
     494        psh->parselleft = psh->parsefile->lleft;
     495        psh->parsenextc = psh->parsefile->nextc;
     496        psh->plinno = psh->parsefile->linno;
    495497        INTON;
    496498}
     
    502504
    503505void
    504 popallfiles(void)
    505 {
    506         while (parsefile != &basepf)
    507                 popfile();
     506popallfiles(shinstance *psh)
     507{
     508        while (psh->parsefile != &psh->basepf)
     509                popfile(psh);
    508510}
    509511
     
    525527
    526528void
    527 closescript(int vforked)
     529closescript(shinstance *psh, int vforked)
    528530{
    529531        if (vforked)
    530532                return;
    531         popallfiles();
    532         if (parsefile->fd > 0) {
    533                 close(parsefile->fd);
    534                 parsefile->fd = 0;
    535         }
    536 }
     533        popallfiles(psh);
     534        if (psh->parsefile->fd > 0) {
     535                shfile_close(&psh->fdtab, psh->parsefile->fd);
     536                psh->parsefile->fd = 0;
     537        }
     538}
  • trunk/src/kash/input.h

    r879 r1201  
    4242 * package must set its value.
    4343 */
    44 extern int plinno;
    45 extern int parsenleft;          /* number of characters left in input buffer */
    46 extern char *parsenextc;        /* next character in input buffer */
    47 extern int init_editline;       /* 0 == not setup, 1 == OK, -1 == failed */
     44//extern int plinno;
     45//extern int parsenleft;                /* number of characters left in input buffer */
     46//extern char *parsenextc;      /* next character in input buffer */
     47//extern int init_editline;     /* 0 == not setup, 1 == OK, -1 == failed */
    4848
    4949char *pfgets(struct shinstance *, char *, int);
     
    6060void closescript(struct shinstance *, int);
    6161
    62 #define pgetc_macro()   (--psh->parsenleft >= 0? *psh->parsenextc++ : preadbuffer(psh))
     62#define pgetc_macro(psh)        (--(psh)->parsenleft >= 0? *(psh)->parsenextc++ : preadbuffer(psh))
  • trunk/src/kash/jobs.c

    r1199 r1201  
    905905                rootshell = 0;
    906906
    907         closescript(vforked);
    908         clear_traps(vforked);
     907        closescript(psh, vforked);
     908        clear_traps(psh, vforked);
    909909#if JOBS
    910910        if (!vforked)
     
    10951095                        mode = SHOW_SIGNALLED | SHOW_NO_FREE;
    10961096                if (mode)
    1097                         showjob(out2, thisjob, mode);
     1097                        showjob(psh->out2, thisjob, mode);
    10981098                else {
    10991099                        TRACE((psh, "Not printing status, rootshell=%d, job=%p\n",
  • trunk/src/kash/parser.c

    r1199 r1201  
    209209                                parseheredoc();
    210210                        else
    211                                 pungetc();              /* push back EOF on input */
     211                                pungetc(psh);           /* push back EOF on input */
    212212                        return n1;
    213213                default:
     
    661661                if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
    662662                        synerror("Illegal eof marker for << redirection");
    663                 rmescapes(wordtext);
     663                rmescapes(psh, wordtext);
    664664                here->eofmark = wordtext;
    665665                here->next = NULL;
     
    695695                        needprompt = 0;
    696696                }
    697                 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
     697                readtoken1(pgetc(psh), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
    698698                                here->eofmark, here->striptabs);
    699699                n = (union node *)stalloc(psh, sizeof (struct narg));
     
    759759                        if(!noalias &&
    760760                            (ap = lookupalias(psh, wordtext, 1)) != NULL) {
    761                                 pushstring(ap->val, strlen(ap->val), ap);
     761                                pushstring(psh, ap->val, strlen(ap->val), ap);
    762762                                checkkwd = savecheckkwd;
    763763                                goto top;
     
    812812        startlinno = plinno;
    813813        for (;;) {      /* until token or start of word found */
    814                 c = pgetc_macro();
     814                c = pgetc_macro(psh);
    815815                if (c == ' ' || c == '\t')
    816816                        continue;               /* quick check for white space first */
     
    819819                        continue;
    820820                case '#':
    821                         while ((c = pgetc()) != '\n' && c != PEOF);
    822                         pungetc();
     821                        while ((c = pgetc(psh)) != '\n' && c != PEOF);
     822                        pungetc(psh);
    823823                        continue;
    824824                case '\\':
    825                         if (pgetc() == '\n') {
     825                        if (pgetc(psh) == '\n') {
    826826                                startlinno = ++plinno;
    827827                                if (doprompt)
     
    831831                                continue;
    832832                        }
    833                         pungetc();
     833                        pungetc(psh);
    834834                        goto breakloop;
    835835                case '\n':
     
    840840                        RETURN(TEOF);
    841841                case '&':
    842                         if (pgetc() == '&')
     842                        if (pgetc(psh) == '&')
    843843                                RETURN(TAND);
    844                         pungetc();
     844                        pungetc(psh);
    845845                        RETURN(TBACKGND);
    846846                case '|':
    847                         if (pgetc() == '|')
     847                        if (pgetc(psh) == '|')
    848848                                RETURN(TOR);
    849                         pungetc();
     849                        pungetc(psh);
    850850                        RETURN(TPIPE);
    851851                case ';':
    852                         if (pgetc() == ';')
     852                        if (pgetc(psh) == ';')
    853853                                RETURN(TENDCASE);
    854                         pungetc();
     854                        pungetc(psh);
    855855                        RETURN(TSEMI);
    856856                case '(':
     
    961961                        if (syntax == BASESYNTAX)
    962962                                return readtoken();
    963                         c = pgetc();
     963                        c = pgetc(psh);
    964964                        goto loop;
    965965                }
     
    978978                                else
    979979                                        setprompt(0);
    980                                 c = pgetc();
     980                                c = pgetc(psh);
    981981                                goto loop;              /* continue outer loop */
    982982                        case CWORD:
     
    989989                                break;
    990990                        case CBACK:     /* backslash */
    991                                 c = pgetc();
     991                                c = pgetc(psh);
    992992                                if (c == PEOF) {
    993993                                        USTPUTC(psh, '\\', out);
    994                                         pungetc();
     994                                        pungetc(psh);
    995995                                        break;
    996996                                }
     
    10931093                                        --parenlevel;
    10941094                                } else {
    1095                                         if (pgetc() == ')') {
     1095                                        if (pgetc(psh) == ')') {
    10961096                                                if (--arinest == 0) {
    10971097                                                        USTPUTC(psh, CTLENDARI, out);
     
    11081108                                                 *  (don't 2nd guess - no error)
    11091109                                                 */
    1110                                                 pungetc();
     1110                                                pungetc(psh);
    11111111                                                USTPUTC(psh, ')', out);
    11121112                                        }
     
    11231123                                USTPUTC(psh, c, out);
    11241124                        }
    1125                         c = pgetc_macro();
     1125                        c = pgetc_macro(psh);
    11261126                }
    11271127        }
     
    11471147                        return lasttoken = TREDIR;
    11481148                } else {
    1149                         pungetc();
     1149                        pungetc(psh);
    11501150                }
    11511151        }
     
    11711171                if (striptabs) {
    11721172                        while (c == '\t')
    1173                                 c = pgetc();
     1173                                c = pgetc(psh);
    11741174                }
    11751175                if (c == *eofmark) {
    1176                         if (pfgets(line, sizeof line) != NULL) {
     1176                        if (pfgets(psh, line, sizeof line) != NULL) {
    11771177                                char *p, *q;
    11781178
     
    11841184                                        needprompt = doprompt;
    11851185                                } else {
    1186                                         pushstring(line, strlen(line), NULL);
     1186                                        pushstring(psh, line, strlen(line), NULL);
    11871187                                }
    11881188                        }
     
    12061206        if (c == '>') {
    12071207                np->nfile.fd = 1;
    1208                 c = pgetc();
     1208                c = pgetc(psh);
    12091209                if (c == '>')
    12101210                        np->type = NAPPEND;
     
    12151215                else {
    12161216                        np->type = NTO;
    1217                         pungetc();
     1217                        pungetc(psh);
    12181218                }
    12191219        } else {        /* c == '<' */
    12201220                np->nfile.fd = 0;
    1221                 switch (c = pgetc()) {
     1221                switch (c = pgetc(psh)) {
    12221222                case '<':
    12231223                        if (sizeof (struct nfile) != sizeof (struct nhere)) {
     
    12281228                        heredoc = (struct heredoc *)stalloc(psh, sizeof (struct heredoc));
    12291229                        heredoc->here = np;
    1230                         if ((c = pgetc()) == '-') {
     1230                        if ((c = pgetc(psh)) == '-') {
    12311231                                heredoc->striptabs = 1;
    12321232                        } else {
    12331233                                heredoc->striptabs = 0;
    1234                                 pungetc();
     1234                                pungetc(psh);
    12351235                        }
    12361236                        break;
     
    12461246                default:
    12471247                        np->type = NFROM;
    1248                         pungetc();
     1248                        pungetc(psh);
    12491249                        break;
    12501250                }
     
    12691269        static const char types[] = "}-+?=";
    12701270
    1271         c = pgetc();
     1271        c = pgetc(psh);
    12721272        if (c != '(' && c != OPENBRACE && !is_name(c) && !is_special(c)) {
    12731273                USTPUTC(psh, '$', out);
    1274                 pungetc();
     1274                pungetc(psh);
    12751275        } else if (c == '(') {  /* $(command) or $((arith)) */
    1276                 if (pgetc() == '(') {
     1276                if (pgetc(psh) == '(') {
    12771277                        PARSEARITH();
    12781278                } else {
    1279                         pungetc();
     1279                        pungetc(psh);
    12801280                        PARSEBACKQNEW();
    12811281                }
     
    12861286                subtype = VSNORMAL;
    12871287                if (c == OPENBRACE) {
    1288                         c = pgetc();
     1288                        c = pgetc(psh);
    12891289                        if (c == '#') {
    1290                                 if ((c = pgetc()) == CLOSEBRACE)
     1290                                if ((c = pgetc(psh)) == CLOSEBRACE)
    12911291                                        c = '#';
    12921292                                else
     
    12991299                        do {
    13001300                                STPUTC(psh, c, out);
    1301                                 c = pgetc();
     1301                                c = pgetc(psh);
    13021302                        } while (is_in_name(c));
    13031303                } else if (is_digit(c)) {
    13041304                        do {
    13051305                                USTPUTC(psh, c, out);
    1306                                 c = pgetc();
     1306                                c = pgetc(psh);
    13071307                        } while (is_digit(c));
    13081308                }
    13091309                else if (is_special(c)) {
    13101310                        USTPUTC(psh, c, out);
    1311                         c = pgetc();
     1311                        c = pgetc(psh);
    13121312                }
    13131313                else
     
    13201320                        case ':':
    13211321                                flags = VSNUL;
    1322                                 c = pgetc();
     1322                                c = pgetc(psh);
    13231323                                /*FALLTHROUGH*/
    13241324                        default:
     
    13341334                                        subtype = c == '#' ? VSTRIMLEFT :
    13351335                                                             VSTRIMRIGHT;
    1336                                         c = pgetc();
     1336                                        c = pgetc(psh);
    13371337                                        if (c == cc)
    13381338                                                subtype++;
    13391339                                        else
    1340                                                 pungetc();
     1340                                                pungetc(psh);
    13411341                                        break;
    13421342                                }
    13431343                        }
    13441344                } else {
    1345                         pungetc();
     1345                        pungetc(psh);
    13461346                }
    13471347                if (ISDBLQUOTE() || arinest)
     
    14151415                                needprompt = 0;
    14161416                        }
    1417                         switch (pc = pgetc()) {
     1417                        switch (pc = pgetc(psh)) {
    14181418                        case '`':
    14191419                                goto done;
    14201420
    14211421                        case '\\':
    1422                                 if ((pc = pgetc()) == '\n') {
     1422                                if ((pc = pgetc(psh)) == '\n') {
    14231423                                        plinno++;
    14241424                                        if (doprompt)
     
    14591459                if (psavelen > 0) {
    14601460                        pstr = grabstackstr(psh, pout);
    1461                         setinputstring(pstr, 1);
     1461                        setinputstring(psh, pstr, 1);
    14621462                }
    14631463        }
     
    14891489                 * tokens left from the backquote parsing
    14901490                 */
    1491                 popfile();
     1491                popfile(psh);
    14921492                tokpushback = 0;
    14931493        }
     
    16291629setprompt(int which)
    16301630{
    1631         whichprompt = which;
     1631        psh->whichprompt = which;
    16321632
    16331633#ifndef SMALL
     
    16441644getprompt(void *unused)
    16451645        {
    1646         switch (whichprompt) {
     1646        switch (psh->whichprompt) {
    16471647        case 0:
    16481648                return "";
  • trunk/src/kash/redir.c

    r1199 r1201  
    167167        }
    168168        if (memory[1])
    169                 out1 = &memout;
     169                psh->out1 = &psh->memout;
    170170        if (memory[2])
    171                 out2 = &memout;
     171                psh->out2 = &psh->memout;
    172172}
    173173
     
    283283                        xwrite(psh, pip[1], redir->nhere.doc->narg.text, len);
    284284                else
    285                         expandhere(redir->nhere.doc, pip[1]);
     285                        expandhere(psh, redir->nhere.doc, pip[1]);
    286286                _exit(0);
    287287        }
  • trunk/src/kash/shinstance.c

    r1199 r1201  
    5252        psh->stacknxt = psh->stackbase.space;
    5353
     54        /* input.c */
     55        psh->plinno = 1;
     56        psh->init_editline = 0;
     57        psh->parsefile = &psh->basepf;
     58
    5459        /* output.c */
    5560        psh->output.bufsize = OUTBUFSIZ;
     
    6166        psh->out2 = &psh->errout;
    6267
    63         /* .c */
    6468    }
    6569    return psh;
  • trunk/src/kash/shinstance.h

    r1199 r1201  
    2727#ifndef ___shinstance_h___
    2828#define ___shinstance_h___
     29
     30#include <stdio.h> /* BUFSIZ */
    2931
    3032#include "shtypes.h"
     
    3638#include "var.h"
    3739
     40/* memalloc.c */
    3841#define MINSIZE 504             /* minimum size of a block */
    3942struct stack_block {
     
    4144        char space[MINSIZE];
    4245};
     46
     47/* input.c */
     48struct strpush {
     49        struct strpush *prev;   /* preceding string on stack */
     50        char *prevstring;
     51        int prevnleft;
     52        int prevlleft;
     53        struct alias *ap;       /* if push was associated with an alias */
     54};
     55
     56/*
     57 * The parsefile structure pointed to by the global variable parsefile
     58 * contains information about the current file being read.
     59 */
     60struct parsefile {
     61        struct parsefile *prev; /* preceding file on stack */
     62        int linno;              /* current line */
     63        int fd;                 /* file descriptor (or -1 if string) */
     64        int nleft;              /* number of chars left in this line */
     65        int lleft;              /* number of chars left in this buffer */
     66        char *nextc;            /* next char in buffer */
     67        char *buf;              /* input buffer */
     68        struct strpush *strpush; /* for pushing strings at this level */
     69        struct strpush basestrpush; /* so pushing one is fast */
     70};
     71
    4372
    4473
     
    136165    struct stackmark   *markp;
    137166
    138 
    139167    /* jobs.h */
    140168    pid_t               backgndpid;     /**< pid of last background process */
     
    142170
    143171    /* input.h */
    144     int                 plinno;
     172    int                 plinno/* = 1 */;/**< input line number */
    145173    int                 parsenleft;     /**< number of characters left in input buffer */
    146174    char               *parsenextc;     /**< next character in input buffer */
    147     int                 init_editline;  /**< 0 == not setup, 1 == OK, -1 == failed */
     175    int                 init_editline/* = 0 */;     /**< 0 == not setup, 1 == OK, -1 == failed */
     176
     177    /* input.c */
     178    int                 parselleft;     /**< copy of parsefile->lleft */
     179    struct parsefile    basepf;         /**< top level input file */
     180    char                basebuf[BUFSIZ];/**< buffer for top level input file */
     181    struct parsefile   *parsefile/* = &basepf*/;    /**< current input file */
     182#ifndef SMALL
     183    EditLine           *el;             /**< cookie for editline package */
     184#endif
     185
    148186
    149187    /* exec.h */
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette