Ignore:
Timestamp:
Sep 13, 2020, 1:17:09 PM (5 years ago)
Author:
bird
Message:

kash: Eliminate the 'temp' node field in nfile so we can share node trees later.

File:
1 edited

Legend:

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

    r3445 r3449  
    4343#include <sys/types.h>
    4444#include <limits.h>         /* PIPE_BUF */
     45#include <assert.h>
    4546#include <string.h>
    4647#include <errno.h>
     48#include <stddef.h>
    4749#include <stdlib.h>
    4850
     
    8890//int fd0_redirected = 0;
    8991
    90 STATIC void openredirect(shinstance *, union node *, char[10], int);
     92STATIC void openredirect(shinstance *, union node *, char[10], int, const char *);
    9193STATIC int openhere(shinstance *, union node *);
    9294
     
    114116        psh->fd0_redirected = inherit->fd0_redirected;
    115117    }
     118
     119    /* Copy the expanded redirection filenames (stack), but only the last entry
     120       as the subshell does not have the ability to unwind stack in the parent
     121       and therefore cannot get to the earlier redirection stuff: */
     122    if (inherit->expfnames)
     123    {
     124        redirexpfnames * const expfnamesrc = inherit->expfnames;
     125        unsigned i = expfnamesrc->count;
     126        redirexpfnames *dst = stalloc(psh, offsetof(redirexpfnames, names) + sizeof(dst->names[0]) * i);
     127        dst->count = i;
     128        dst->depth = 1;
     129        dst->prev = NULL;
     130        while (i-- > 0)
     131            dst->names[i] = stsavestr(psh, expfnamesrc->names[i]);
     132        psh->expfnames = dst;
     133    }
    116134}
    117135#endif /* !SH_FORKED_MODE */
     
    135153        int try;
    136154        char memory[10];        /* file descriptors to write to memory */
     155        unsigned idxexpfname;
    137156
    138157        for (i = 10 ; --i >= 0 ; )
     
    146165                psh->redirlist = sv;
    147166        }
    148         for (n = redir ; n ; n = n->nfile.next) {
     167        idxexpfname = 0;
     168        for (n = redir, idxexpfname = 0 ; n ; n = n->nfile.next, idxexpfname++) {
     169                assert(idxexpfname < psh->expfnames->count);
    149170                fd = n->nfile.fd;
    150171                try = 0;
     
    160181                                case EBADF:
    161182                                        if (!try) {
    162                                                 openredirect(psh, n, memory, flags);
     183                                                openredirect(psh, n, memory, flags, psh->expfnames->names[idxexpfname]);
    163184                                                try++;
    164185                                                goto again;
     
    182203                        psh->fd0_redirected++;
    183204                if (!try)
    184                         openredirect(psh, n, memory, flags);
    185         }
     205                        openredirect(psh, n, memory, flags, psh->expfnames->names[idxexpfname]);
     206        }
     207        assert(!redir || idxexpfname == psh->expfnames->count);
    186208        if (memory[1])
    187209                psh->out1 = &psh->memout;
     
    192214
    193215STATIC void
    194 openredirect(shinstance *psh, union node *redir, char memory[10], int flags)
     216openredirect(shinstance *psh, union node *redir, char memory[10], int flags, const char *fname)
    195217{
    196218        int fd = redir->nfile.fd;
    197         char *fname;
    198219        int f;
    199220        int oflags = O_WRONLY|O_CREAT|O_TRUNC;
     
    208229        switch (redir->nfile.type) {
    209230        case NFROM:
    210                 fname = redir->nfile.expfname;
    211231                if ((f = shfile_open(&psh->fdtab, fname, O_RDONLY, 0)) < 0)
    212232                        goto eopen;
    213233                break;
    214234        case NFROMTO:
    215                 fname = redir->nfile.expfname;
    216235                if ((f = shfile_open(&psh->fdtab, fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
    217236                        goto ecreate;
     
    222241                /* FALLTHROUGH */
    223242        case NCLOBBER:
    224                 fname = redir->nfile.expfname;
    225243                if ((f = shfile_open(&psh->fdtab, fname, oflags, 0666)) < 0)
    226244                        goto ecreate;
    227245                break;
    228246        case NAPPEND:
    229                 fname = redir->nfile.expfname;
    230247                if ((f = shfile_open(&psh->fdtab, fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
    231248                        goto ecreate;
Note: See TracChangeset for help on using the changeset viewer.