Lines Matching refs:shp

400 int  sh_iovalidfd(Shell_t *shp, int fd)
402 Sfio_t **sftable = shp->sftable;
403 int max,n, **fdptrs = shp->fdptrs;
404 unsigned char *fdstatus = shp->fdstatus;
407 if(fd < shp->gd->lim.open_max)
418 max = shp->gd->lim.open_max;
419 shp->sftable = (Sfio_t**)calloc(n*(sizeof(int*)+sizeof(Sfio_t*)+1),1);
421 memcpy(shp->sftable,sftable,max*sizeof(Sfio_t*));
422 shp->fdptrs = (int**)(&shp->sftable[n]);
424 memcpy(shp->fdptrs,fdptrs,max*sizeof(int*));
425 shp->fdstatus = (unsigned char*)(&shp->fdptrs[n]);
427 memcpy(shp->fdstatus,fdstatus,max);
430 shp->gd->lim.open_max = n;
434 int sh_inuse(Shell_t *shp, int fd)
436 return(fd < shp->gd->lim.open_max && shp->fdptrs[fd]);
439 void sh_ioinit(Shell_t *shp)
443 sh_iovalidfd(shp,16);
444 shp->sftable[0] = sfstdin;
445 shp->sftable[1] = sfstdout;
446 shp->sftable[2] = sfstderr;
448 sh_iostream(shp,0);
449 sh_iostream(shp,1);
451 shp->outpool = sfopen(NIL(Sfio_t*),NIL(char*),"sw"); /* pool identifier */
452 shp->outbuff = (char*)malloc(IOBSIZE+4);
453 shp->errbuff = (char*)malloc(IOBSIZE/4);
454 sfsetbuf(sfstderr,shp->errbuff,IOBSIZE/4);
455 sfsetbuf(sfstdout,shp->outbuff,IOBSIZE);
456 sfpool(sfstdout,shp->outpool,SF_WRITE);
457 sfpool(sfstderr,shp->outpool,SF_WRITE);
468 Shell_t *shp = ((struct Iodisc*)handle)->sh;
487 int mode = ((struct checkpt*)shp->jmplist)->mode;
490 ((struct checkpt*)shp->jmplist)->mode = 0;
496 ((struct checkpt*)shp->jmplist)->mode = mode;
511 Sfio_t *sh_iostream(Shell_t *shp, register int fd)
514 register int status = sh_iocheckfd(shp,fd);
540 bp = shp->outbuff;
543 if((iop = shp->sftable[fd]) && sffileno(iop)>=0)
552 dp->sh = shp;
577 sfpool(iop,shp->outpool,SF_WRITE);
580 shp->sftable[fd] = iop;
587 static void io_preserve(Shell_t* shp, register Sfio_t *sp, register int f2)
594 if(f2==shp->infd)
595 shp->infd = fd;
598 shp->toomany = 1;
599 ((struct checkpt*)shp->jmplist)->mode = SH_JMPERREXIT;
603 VALIDATE_FD(shp, fd);
604 VALIDATE_FD(shp, f2);
606 if(shp->fdptrs[fd]=shp->fdptrs[f2])
610 *shp->fdptrs[fd] = fd;
611 shp->fdptrs[f2] = 0;
613 shp->sftable[fd] = sp;
614 shp->fdstatus[fd] = shp->fdstatus[f2];
618 shp->fdstatus[fd] |= IOCLEX;
620 shp->sftable[f2] = 0;
629 int sh_iorenumber(Shell_t *shp, register int f1,register int f2)
633 VALIDATE_FD(shp, f1);
634 VALIDATE_FD(shp, f2);
636 sp = shp->sftable[f2];
640 if(sh_inuse(shp,f2) || (f2>2 && sp))
642 if(!(shp->inuse_bits&(1<<f2)))
643 io_preserve(shp,sp,f2);
647 shp->st.ioset = 1;
651 register Sfio_t *spnew = sh_iostream(shp,f1);
652 shp->fdstatus[f2] = (shp->fdstatus[f1]&~IOCLEX);
659 shp->fdstatus[f2] = (shp->fdstatus[f1]&~IOCLEX);
663 sh_iostream(shp,f2);
666 shp->sftable[f1] = 0;
667 if(shp->fdstatus[f1]!=IOCLOSE)
676 VALIDATE_FD(shp, f2);
685 Shell_t *shp = sh_getinterp();
691 VALIDATE_FD(shp, fd);
693 if(!(sp=shp->sftable[fd]) || sfclose(sp) < 0)
700 shp->sftable[fd] = 0;
701 shp->fdstatus[fd] = IOCLOSE;
702 if(shp->fdptrs[fd])
703 *shp->fdptrs[fd] = -1;
704 shp->fdptrs[fd] = 0;
706 shp->inuse_bits &= ~(1<<fd);
735 Shell_t *shp = sh_getinterp();
789 if ((fd = inetopen(path+5, flags, onintr, shp)) < 0 && errno != ENOTDIR)
816 if((mode=sh_iocheckfd(shp,fd))==IOCLOSE)
837 if(errno!=EINTR || shp->trapnote)
849 VALIDATE_FD(shp, fd);
851 shp->fdstatus[fd] = mode;
872 Shell_t *shp = sh_getinterp();
875 VALIDATE_FD(shp, fdold);
881 VALIDATE_FD(shp, fdnew);
883 shp->fdstatus[fdnew] = (shp->fdstatus[fdold]&~IOCLEX);
885 shp->fdstatus[fdold] = IOCLOSE;
894 Shell_t *shp = sh_getinterp();
900 VALIDATE_FD(shp, pv[0]);
901 VALIDATE_FD(shp, pv[1]);
902 shp->fdstatus[pv[0]] = IONOSEEK|IOREAD;
903 shp->fdstatus[pv[1]] = IONOSEEK|IOWRITE;
910 int sh_coaccept(Shell_t *shp,int *pv,int out)
922 VALIDATE_FD(shp, pv[out]);
925 shp->fdstatus[pv[out]] |= IOCLEX;
926 shp->fdstatus[pv[out]] = (out?IOWRITE:IOREAD);
927 shp->fdstatus[pv[out]] |= IONOSEEK;
935 int sh_copipe(Shell_t *shp, int *pv, int out)
957 VALIDATE_FD(shp, pv[out]);
959 shp->fdstatus[pv[out]] |= IOCLEX;
985 static int io_patseek(Shell_t *shp, regex_t *rp, Sfio_t* sp, int flags)
993 VALIDATE_FD(shp, fd);
994 close_exec = shp->fdstatus[fd]&IOCLEX;
995 shp->fdstatus[sffileno(sp)] |= IOCLEX;
1020 shp->fdstatus[sffileno(sp)] &= ~IOCLEX;
1026 static Sfoff_t file_offset(Shell_t *shp, int fn, char *fname)
1032 Namval_t *mp = nv_open("EOF",shp->var_tree,0);
1033 Namval_t *pp = nv_open("CUR",shp->var_tree,0);
1035 VALIDATE_FD(shp, fn);
1037 sp = shp->sftable[fn];
1125 int sh_redirect(Shell_t *shp,struct ionod *iop, int flag)
1134 int r, indx = shp->topfd, perm= -1;
1135 char *tname=0, *after="", *trace = shp->st.trap[SH_DEBUGTRAP];
1137 int isstring = shp->subshell?(sfset(sfstdout,0,0)&SF_STRING):0;
1142 traceon = sh_trace(shp,NIL(char**),0);
1147 if(fn==1 && shp->subshell && !shp->subshare && (flag==2 || isstring))
1172 fname=sh_macpat(shp,ap,(iof&IOARITH)?ARG_ARITH:ARG_EXP);
1180 else if(shp->subshell)
1181 sh_subtmpfile(shp);
1183 ap = sh_argprocsub(shp,ap);
1187 fname=sh_mactrim(shp,fname,(!sh_isoption(SH_NOGLOB)&&sh_isoption(SH_INTERACTIVE))?2:0);
1192 if(shp->inpool)
1195 sh_coaddfile(shp,fname);
1201 np = nv_open(iop->iovname,shp->var_tree,NV_NOASSIGN|NV_VARNAME);
1208 if (!VALIDATE_FD(shp, fn))
1228 fd = io_heredoc(shp,iop,fname,traceon);
1251 VALIDATE_FD(shp, dupfd);
1252 if(shp->subshell && dupfd==1)
1255 sh_subtmpfile(shp);
1256 if(shp->comsub==1)
1257 shp->subdup |= 1<<fn;
1260 else if(shp->sftable[dupfd])
1261 sfsync(shp->sftable[dupfd]);
1263 shp->subdup &= ~(1<<fn);
1273 dupfd = shp->coutpipe;
1275 dupfd = shp->cpipe[0];
1288 VALIDATE_FD(shp, fd);
1289 sh_iocheckfd(shp,dupfd);
1290 shp->fdstatus[fd] = (shp->fdstatus[dupfd]&~IOCLEX);
1291 if(toclose<0 && shp->fdstatus[fd]&IOREAD)
1292 shp->fdstatus[fd] |= IODUP;
1293 else if(dupfd==shp->cpipe[0])
1294 sh_pclose(shp->cpipe);
1298 sh_iosave(shp,toclose,indx,(char*)0); /* save file descriptor */
1345 (!shp->gd->lim.fs3d || iview(&sb)==0))
1397 sh_debug(shp,trace,(char*)0,(char*)0,av,ARG_NOGLOB);
1403 VALIDATE_FD(shp, fn);
1405 sp = shp->sftable[fn];
1406 r = shp->fdstatus[fn];
1408 r = sh_iocheckfd(shp,fn);
1425 if((off = file_offset(shp,fn,fname))<0)
1452 sp = sh_iostream(shp,fn);
1453 r=io_patseek(shp,rp,sp,iof);
1469 if(flag==0 || tname || (flag==1 && fn==1 && (shp->fdstatus[fn]&IONOSEEK) && shp->outpipepid && shp->outpipepid==getpid()))
1479 sh_iosave(shp,fn,indx,tname?fname:(trunc?Empty:0));
1482 sh_iosave(shp,fn,indx|IOSUBSHELL,tname?fname:0);
1486 VALIDATE_FD(shp, fn);
1487 if(sh_inuse(shp,fn) || (fn && fn==shp->infd))
1489 if(fn>9 || !(shp->inuse_bits&(1<<fn)))
1490 io_preserve(shp,shp->sftable[fn],fn);
1506 if (!VALIDATE_FD(shp, fn))
1508 shp->fdstatus[fn] = shp->fdstatus[fd];
1516 sh_iocheckfd(shp,fd);
1520 fd = sh_iorenumber(shp,sh_iomovefd(fd),fn);
1522 shp->inuse_bits |= (1<<fn);
1528 shp->fdstatus[fd] |= IOCLEX;
1543 static int io_heredoc(Shell_t *shp,register struct ionod *iop, const char *name, int traceon)
1548 if(!(iop->iofile&IOSTRG) && (!shp->heredocs || iop->iosize==0))
1561 off = sftell(shp->heredocs);
1562 infile = subopen(shp,shp->heredocs,iop->iooffset,iop->iosize);
1578 char *lastpath = shp->lastpath;
1579 sh_machere(shp,infile,outfile,iop->ioname);
1580 shp->lastpath = lastpath;
1584 sfseek(shp->heredocs,off,SEEK_SET);
1593 shp->fdstatus[fd] = IOREAD;
1614 void sh_iosave(Shell_t *shp, register int origfd, int oldtop, char *name)
1620 for(savefd=shp->topfd; --savefd>=oldtop; )
1626 if(shp->topfd >= filemapsize)
1636 for(savefd=shp->gd->lim.open_max; --savefd>=0; )
1638 cp = (char*)shp->fdptrs[savefd];
1640 shp->fdptrs[savefd] = (int*)(cp+moved);
1655 shp->toomany=1;
1656 ((struct checkpt*)shp->jmplist)->mode = SH_JMPERREXIT;
1660 filemap[shp->topfd].tname = name;
1661 filemap[shp->topfd].subshell = flag;
1662 filemap[shp->topfd].orig_fd = origfd;
1663 filemap[shp->topfd++].save_fd = savefd;
1668 VALIDATE_FD(shp, origfd);
1669 VALIDATE_FD(shp, savefd);
1671 sp = shp->sftable[origfd];
1676 shp->fdstatus[savefd] = shp->fdstatus[origfd];
1677 shp->fdptrs[savefd] = &filemap[shp->topfd-1].save_fd;
1678 if(!(shp->sftable[savefd]=sp))
1685 shp->sftable[savefd] = sp;
1688 shp->sftable[origfd] = 0;
1695 void sh_iounsave(Shell_t* shp)
1698 for(newfd=fd=0; fd < shp->topfd; fd++)
1704 VALIDATE_FD(shp, savefd);
1705 shp->sftable[savefd] = 0;
1709 shp->topfd = newfd;
1715 void sh_iorestore(Shell_t *shp, int last, int jmpval)
1720 for (fd = shp->topfd - 1; fd >= last; fd--)
1728 VALIDATE_FD(shp, savefd);
1729 shp->sftable[savefd] = 0;
1735 VALIDATE_FD(shp, origfd);
1740 VALIDATE_FD(shp, savefd);
1741 shp->sftable[savefd] = 0;
1745 if(filemap[fd].tname == Empty && shp->exitval==0)
1748 io_usename(filemap[fd].tname,(int*)0,shp->exitval?2:1);
1752 VALIDATE_FD(shp, savefd);
1756 shp->fdstatus[origfd] = shp->fdstatus[savefd];
1758 if(shp->fdstatus[origfd]&IOCLEX)
1762 sfswap(shp->sftable[savefd],shp->sftable[origfd]);
1764 shp->st.ioset = 0;
1767 shp->sftable[origfd] = shp->sftable[savefd];
1768 shp->sftable[savefd] = 0;
1772 shp->fdstatus[origfd] = IOCLOSE;
1777 for (fd = last ; fd < shp->topfd; fd++)
1783 if(last < shp->topfd)
1784 shp->topfd = last;
1794 Shell_t *shp = sh_getinterp();
1798 if((flags=sh_iocheckfd(shp,fd))!=IOCLOSE)
1815 Shell_t *shp = ((struct Iodisc*)handle)->sh;
1827 if((shp->trapnote&(SH_SIGSET|SH_SIGTRAP)) && errno!=EIO && errno!=ENXIO)
1858 if(shp->bltinfun && shp->bltindata.sigset)
1861 if(shp->trapnote&SH_SIGSET)
1867 if(shp->trapnote&SH_SIGTRAP)
1868 sh_chktrap(shp);
1877 Shell_t *shp = (Shell_t*)handle;
1884 ((struct checkpt*)shp->jmplist)->mode = SH_JMPEXIT;
1886 shp->trapnote |= SH_SIGSET;
1892 shp->trapnote |= SH_SIGTRAP;
1897 Shell_t *shp = ((struct Iodisc*)handle)->sh;
1904 if(shp->trapnote)
1909 if(sh_isstate(SH_INTERACTIVE) && sffileno(iop)==0 && io_prompt(shp,iop,shp->nextprompt)<0 && errno==EIO)
1912 if(!(shp->fdstatus[sffileno(iop)]&IOCLEX) && (sfset(iop,0,0)&SF_SHARE))
1925 Shell_t *shp = ((struct Iodisc*)handle)->sh;
1946 if(shp->trapnote)
1953 if(io_prompt(shp,iop,shp->nextprompt)<0 && errno==EIO)
1955 if(shp->timeout)
1956 timeout = (void*)sh_timeradd(sh_isstate(SH_GRACE)?1000L*TGRACE:1000L*shp->timeout,0,time_grace,shp);
1962 if(rsize && *(char*)buff != '\n' && shp->nextprompt==1 && sh_isoption(SH_HISTEXPAND))
2005 int sh_iocheckfd(Shell_t *shp, register int fd)
2009 VALIDATE_FD(shp, fd);
2011 if((n=shp->fdstatus[fd])&IOCLOSE)
2017 return(shp->fdstatus[fd]=IOCLOSE);
2025 return(shp->fdstatus[fd]=IOCLOSE);
2077 shp->fdstatus[fd] = n;
2085 static int io_prompt(Shell_t *shp,Sfio_t *iop,register int flag)
2099 if(!(shp->prompt=(char*)sfreserve(sfstderr,0,0)))
2100 shp->prompt = "";
2117 cp = sh_mactry(shp,nv_getval(sh_scoped(shp,PS1NOD)));
2127 sfprintf(sfstderr,"%d", shp->gd->hist_ptr?(int)shp->gd->hist_ptr->histind:++cmdno);
2137 cp = nv_getval(sh_scoped(shp,PS2NOD));
2140 cp = nv_getval(sh_scoped(shp,PS3NOD));
2148 if(*shp->prompt && (endprompt=(char*)sfreserve(sfstderr,0,0)))
2175 Shell_t *shp = sh_getinterp();
2202 if (fd < 0 || !VALIDATE_FD(shp, fd))
2207 if(sp==shp->heredocs && fd < 10 && flag==SF_NEW)
2216 if(!shp->sftable[fd] && shp->fdstatus[fd]==IOCLOSE)
2218 shp->sftable[fd] = sp;
2222 shp->fdstatus[fd] = flag;
2223 sh_iostream(shp,fd);
2225 if((pp=(struct checkpt*)shp->jmplist) && pp->mode==SH_JMPCMD)
2243 shp->sftable[fd] = 0;
2244 shp->fdstatus[fd]=IOCLOSE;
2245 if(pp=(struct checkpt*)shp->jmplist)
2339 static Sfio_t *subopen(Shell_t *shp,Sfio_t* sp, off_t offset, long size)
2410 Shell_t *shp = sh_getinterp();
2415 char *cp = nv_getval(sh_scoped(shp,LINES));
2446 if(shp->trapnote&SH_SIGSET)
2468 Shell_t *shp = sh_getinterp();
2471 VALIDATE_FD(shp, fd);
2472 if(sp=shp->sftable[fd])
2484 Shell_t *shp = sh_getinterp();
2487 VALIDATE_FD(shp, fd);
2488 if(sp=shp->sftable[fd])
2500 Shell_t *shp = sh_getinterp();
2503 VALIDATE_FD(shp, fd);
2504 if((sp=shp->sftable[fd]) && (sfset(sp,0,0)&(SF_READ|SF_WRITE)))
2513 Shell_t *shp = sh_getinterp();
2516 VALIDATE_FD(shp, old);
2517 VALIDATE_FD(shp, fd);
2520 if(shp->fdstatus[old] == IOCLOSE)
2521 shp->fdstatus[old] = 0;
2522 shp->fdstatus[fd] = (shp->fdstatus[old]&~IOCLEX);
2532 Shell_t *shp = sh_getinterp();
2540 VALIDATE_FD(shp, fd);
2541 VALIDATE_FD(shp, newfd);
2546 if(shp->fdstatus[fd] == IOCLOSE)
2547 shp->fdstatus[fd] = 0;
2548 shp->fdstatus[newfd] = (shp->fdstatus[fd]&~IOCLEX);
2553 if(shp->fdstatus[fd] == IOCLOSE)
2554 shp->fdstatus[fd] = 0;
2556 shp->fdstatus[fd] |= IOCLEX;
2558 shp->fdstatus[fd] &= ~IOCLEX;
2566 Shell_t *shp = sh_getinterp();
2567 shp->mask = m;
2580 Shell_t *shp = sh_getinterp();
2591 if(!sh_histinit((void*)shp))
2593 fd = sffileno(shp->gd->hist_ptr->histfp);
2597 fd = shp->coutpipe;
2599 fd = shp->cpipe[0];
2602 if(fd<0 || !sh_iovalidfd(shp,fd))
2610 if(!(n=shp->fdstatus[fd]))
2611 n = sh_iocheckfd(shp,fd);
2616 VALIDATE_FD(shp, fd);
2617 if(!(iop = shp->sftable[fd]))
2618 iop=sh_iostream(shp,fd);
2634 Shell_t *shp = sh_getinterp();
2638 VALIDATE_FD(shp, fd);
2639 sp = shp->sftable[fd];
2640 if(!sp && (status = sh_iocheckfd(shp,fd))!=IOCLOSE)
2648 shp->sftable[fd] = sp;
2655 Shell_t *shp = sh_getinterp();
2658 if((n=path_open(shp,cp,path_get(shp,cp))) < 0)
2659 n = path_open(shp,cp,(Pathcomp_t*)0);
2661 if((n=path_open(shp,cp,path_get(cp))) < 0)
2662 n = path_open(shp,cp,"");
2666 return(sh_iostream(shp,n));