Lines Matching refs:shp

385 void sh_ioinit(Shell_t *shp)
391 n = shp->lim.open_max+2;
393 n = shp->lim.open_max;
395 shp->fdstatus = (unsigned char*)malloc((unsigned)n);
396 memset((char*)shp->fdstatus,0,n);
397 shp->fdptrs = (int**)malloc(n*sizeof(int*));
398 memset((char*)shp->fdptrs,0,n*sizeof(int*));
399 shp->sftable = (Sfio_t**)malloc(n*sizeof(Sfio_t*));
400 memset((char*)shp->sftable,0,n*sizeof(Sfio_t*));
401 shp->sftable[0] = sfstdin;
402 shp->sftable[1] = sfstdout;
403 shp->sftable[2] = sfstderr;
405 sh_iostream(shp,0);
407 shp->outpool = sfopen(NIL(Sfio_t*),NIL(char*),"sw"); /* pool identifier */
408 shp->outbuff = (char*)malloc(IOBSIZE+4);
409 shp->errbuff = (char*)malloc(IOBSIZE/4);
410 sfsetbuf(sfstderr,shp->errbuff,IOBSIZE/4);
411 sfsetbuf(sfstdout,shp->outbuff,IOBSIZE);
412 sfpool(sfstdout,shp->outpool,SF_WRITE);
413 sfpool(sfstderr,shp->outpool,SF_WRITE);
468 Sfio_t *sh_iostream(Shell_t *shp, register int fd)
471 register int status = sh_iocheckfd(shp,fd);
476 if(fd>=shp->lim.open_max)
477 return(shp->sftable[fd]);
501 bp = shp->outbuff;
504 if((iop = shp->sftable[fd]) && sffileno(iop)>=0)
530 sfpool(iop,shp->outpool,SF_WRITE);
533 shp->sftable[fd] = iop;
540 static void io_preserve(Shell_t* shp, register Sfio_t *sp, register int f2)
547 if(f2==shp->infd)
548 shp->infd = fd;
551 shp->toomany = 1;
552 ((struct checkpt*)shp->jmplist)->mode = SH_JMPERREXIT;
555 if(shp->fdptrs[fd]=shp->fdptrs[f2])
559 *shp->fdptrs[fd] = fd;
560 shp->fdptrs[f2] = 0;
562 shp->sftable[fd] = sp;
563 shp->fdstatus[fd] = shp->fdstatus[f2];
567 shp->fdstatus[fd] |= IOCLEX;
569 shp->sftable[f2] = 0;
578 int sh_iorenumber(Shell_t *shp, register int f1,register int f2)
580 register Sfio_t *sp = shp->sftable[f2];
586 if(!(shp->inuse_bits&(1<<f2)))
587 io_preserve(shp,sp,f2);
591 shp->st.ioset = 1;
595 register Sfio_t *spnew = sh_iostream(shp,f1);
596 shp->fdstatus[f2] = (shp->fdstatus[f1]&~IOCLEX);
603 shp->fdstatus[f2] = (shp->fdstatus[f1]&~IOCLEX);
607 sh_iostream(shp,f2);
610 shp->sftable[f1] = 0;
667 Shell_t *shp = &sh;
737 if((mode=sh_iocheckfd(shp,fd))==IOCLOSE)
836 static int io_patseek(Shell_t *shp, regex_t *rp, Sfio_t* sp, int flags)
839 int r, fd=sffileno(sp), close_exec = shp->fdstatus[fd]&IOCLEX;
842 shp->fdstatus[sffileno(sp)] |= IOCLEX;
867 shp->fdstatus[sffileno(sp)] &= ~IOCLEX;
873 static Sfoff_t file_offset(Shell_t *shp, int fn, char *fname)
875 Sfio_t *sp = shp->sftable[fn];
879 Namval_t *mp = nv_open("EOF",shp->var_tree,0);
880 Namval_t *pp = nv_open("CUR",shp->var_tree,0);
960 int sh_redirect(Shell_t *shp,struct ionod *iop, int flag)
969 int r, indx = shp->topfd, perm= -1;
970 char *tname=0, *after="", *trace = shp->st.trap[SH_DEBUGTRAP];
972 int isstring = shp->subshell?(sfset(sfstdout,0,0)&SF_STRING):0;
981 if(fn==1 && shp->subshell && !shp->subshare && (flag==2 || isstring))
1006 fname=sh_macpat(shp,ap,(iof&IOARITH)?ARG_ARITH:ARG_EXP);
1015 ap = sh_argprocsub(shp,ap);
1019 fname=sh_mactrim(shp,fname,(!sh_isoption(SH_NOGLOB)&&sh_isoption(SH_INTERACTIVE))?2:0);
1025 np = nv_open(iop->iovname,shp->var_tree,NV_NOASSIGN|NV_VARNAME);
1050 fd = io_heredoc(shp,iop,fname,traceon);
1073 if(shp->subshell && dupfd==1 && (sfset(sfstdout,0,0)&SF_STRING))
1078 else if(shp->sftable[dupfd])
1079 sfsync(shp->sftable[dupfd]);
1089 dupfd = shp->coutpipe;
1091 dupfd = shp->cpipe[0];
1104 sh_iocheckfd(shp,dupfd);
1105 shp->fdstatus[fd] = (shp->fdstatus[dupfd]&~IOCLEX);
1106 if(toclose<0 && shp->fdstatus[fd]&IOREAD)
1107 shp->fdstatus[fd] |= IODUP;
1108 else if(dupfd==shp->cpipe[0])
1109 sh_pclose(shp->cpipe);
1113 sh_iosave(shp,toclose,indx,(char*)0); /* save file descriptor */
1160 (!shp->lim.fs3d || iview(&sb)==0))
1212 sh_debug(shp,trace,(char*)0,(char*)0,av,ARG_NOGLOB);
1216 Sfio_t *sp = shp->sftable[fn];
1217 r = shp->fdstatus[fn];
1219 r = sh_iocheckfd(shp,fn);
1236 if((off = file_offset(shp,fn,fname))<0)
1263 sp = sh_iostream(shp,fn);
1264 r=io_patseek(shp,rp,sp,iof);
1290 sh_iosave(shp,fn,indx,tname?fname:(trunc?Empty:0));
1293 sh_iosave(shp,fn,indx|IOSUBSHELL,tname?fname:0);
1297 if(sh_inuse(fn) || (fn && fn==shp->infd))
1299 if(fn>9 || !(shp->inuse_bits&(1<<fn)))
1300 io_preserve(shp,shp->sftable[fn],fn);
1316 shp->fdstatus[fn] = shp->fdstatus[fd];
1324 sh_iocheckfd(shp,fd);
1328 fd = sh_iorenumber(shp,sh_iomovefd(fd),fn);
1330 shp->inuse_bits |= (1<<fn);
1336 shp->fdstatus[fd] |= IOCLEX;
1351 static int io_heredoc(Shell_t *shp,register struct ionod *iop, const char *name, int traceon)
1355 if(!(iop->iofile&IOSTRG) && (!shp->heredocs || iop->iosize==0))
1368 infile = subopen(shp,shp->heredocs,iop->iooffset,iop->iosize);
1384 char *lastpath = shp->lastpath;
1385 sh_machere(shp,infile,outfile,iop->ioname);
1386 shp->lastpath = lastpath;
1398 shp->fdstatus[fd] = IOREAD;
1419 void sh_iosave(Shell_t *shp, register int origfd, int oldtop, char *name)
1422 assume oldtop>=0 && oldtop<shp->lim.open_max;
1429 for(savefd=shp->topfd; --savefd>=oldtop; )
1435 if(shp->topfd >= filemapsize)
1446 for(savefd=shp->lim.open_max+2; --savefd>=0; )
1448 for(savefd=shp->lim.open_max; --savefd>=0; )
1451 cp = (char*)shp->fdptrs[savefd];
1453 shp->fdptrs[savefd] = (int*)(oldptr+moved);
1468 shp->toomany=1;
1469 ((struct checkpt*)shp->jmplist)->mode = SH_JMPERREXIT;
1473 filemap[shp->topfd].tname = name;
1474 filemap[shp->topfd].subshell = flag;
1475 filemap[shp->topfd].orig_fd = origfd;
1476 filemap[shp->topfd++].save_fd = savefd;
1479 register Sfio_t* sp = shp->sftable[origfd];
1484 shp->fdstatus[savefd] = shp->fdstatus[origfd];
1485 shp->fdptrs[savefd] = &filemap[shp->topfd-1].save_fd;
1486 if(!(shp->sftable[savefd]=sp))
1493 shp->sftable[savefd] = sp;
1496 shp->sftable[origfd] = 0;
1503 void sh_iounsave(Shell_t* shp)
1506 for(newfd=fd=0; fd < shp->topfd; fd++)
1512 shp->sftable[savefd] = 0;
1516 shp->topfd = newfd;
1522 void sh_iorestore(Shell_t *shp, int last, int jmpval)
1527 for (fd = shp->topfd - 1; fd >= last; fd--)
1535 shp->sftable[savefd] = 0;
1541 if(filemap[fd].tname == Empty && shp->exitval==0)
1544 io_usename(filemap[fd].tname,(int*)0,shp->exitval?2:1);
1551 shp->fdstatus[origfd] = shp->fdstatus[savefd];
1553 if(shp->fdstatus[origfd]&IOCLEX)
1557 sfswap(shp->sftable[savefd],shp->sftable[origfd]);
1559 shp->st.ioset = 0;
1562 shp->sftable[origfd] = shp->sftable[savefd];
1563 shp->sftable[savefd] = 0;
1567 shp->fdstatus[origfd] = IOCLOSE;
1572 for (fd = last ; fd < shp->topfd; fd++)
1578 if(last < shp->topfd)
1579 shp->topfd = last;
1589 Shell_t *shp = &sh;
1593 if((flags=sh_iocheckfd(shp,fd))!=IOCLOSE)
1794 int sh_iocheckfd(Shell_t *shp, register int fd)
1861 Shell_t *shp = &sh;
1892 cp = sh_mactry(shp,nv_getval(sh_scoped(shp,PS1NOD)));
1912 cp = nv_getval(sh_scoped(shp,PS2NOD));
1915 cp = nv_getval(sh_scoped(shp,PS3NOD));
1948 Shell_t *shp = &sh;
1975 if((unsigned)fd >= shp->lim.open_max)
1980 if(sp==shp->heredocs && fd < 10 && flag==SF_NEW)
1989 if(!shp->sftable[fd] && shp->fdstatus[fd]==IOCLOSE)
1991 shp->sftable[fd] = sp;
1995 shp->fdstatus[fd] = flag;
1996 sh_iostream(shp,fd);
1998 if((pp=(struct checkpt*)shp->jmplist) && pp->mode==SH_JMPCMD)
2016 shp->sftable[fd] = 0;
2017 shp->fdstatus[fd]=IOCLOSE;
2018 if(pp=(struct checkpt*)shp->jmplist)
2112 static Sfio_t *subopen(Shell_t *shp,Sfio_t* sp, off_t offset, long size)
2123 sp = sfnew(NIL(Sfio_t*),(char*)(disp+1),IOBSIZE,shp->lim.open_max,SF_READ);
2178 Shell_t *shp = &sh;
2183 char *cp = nv_getval(sh_scoped(shp,LINES));
2329 Shell_t *shp = &sh;
2340 if(!sh_histinit((void*)shp))
2342 fd = sffileno(shp->hist_ptr->histfp);
2346 fd = shp->coutpipe;
2348 fd = shp->cpipe[0];
2351 if(fd<0 || fd >= shp->lim.open_max)
2359 if(!(n=shp->fdstatus[fd]))
2360 n = sh_iocheckfd(shp,fd);
2365 if(!(iop = shp->sftable[fd]))
2366 iop=sh_iostream(shp,fd);
2382 Shell_t *shp = &sh;
2385 if(!sp && (status = sh_iocheckfd(shp,fd))!=IOCLOSE)
2400 Shell_t *shp = &sh;
2411 return(sh_iostream(shp,n));