home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / sxt-sh-jobs / part02 / xec.c.Diff < prev   
Encoding:
Text File  |  1987-06-22  |  13.3 KB  |  693 lines

  1. *** xec.c    Mon Nov 24 17:49:32 1986
  2. --- /cs/simon/c/shdiff-sources/xec.c    Fri Feb 20 14:36:02 1987
  3. ***************
  4. *** 13,18
  5.   #include    "sym.h"
  6.   #include    "hash.h"
  7.   
  8.   static int    parent;
  9.   
  10.   /* ========    command execution    ========*/
  11.  
  12. --- 13,22 -----
  13.   #include    "sym.h"
  14.   #include    "hash.h"
  15.   
  16. + #ifdef JOB
  17. + #include "job.h"
  18. + #endif JOB
  19.   static int    parent;
  20.   
  21.   
  22. ***************
  23. *** 15,20
  24.   
  25.   static int    parent;
  26.   
  27.   /* ========    command execution    ========*/
  28.   
  29.   
  30.  
  31. --- 19,25 -----
  32.   
  33.   static int    parent;
  34.   
  35.   /* ========    command execution    ========*/
  36.   
  37.   
  38. ***************
  39. *** 87,92
  40.                   struct ionod    *io = t->treio;
  41.                   short     cmdhash;
  42.                   short    comtype;
  43.   
  44.                   exitval = 0;
  45.   
  46.  
  47. --- 92,100 -----
  48.                   struct ionod    *io = t->treio;
  49.                   short     cmdhash;
  50.                   short    comtype;
  51. + #ifdef JOB
  52. +                 char *fgcom[3];
  53. + #endif
  54.   
  55.                   exitval = 0;
  56.   
  57. ***************
  58. *** 95,100
  59.                   com = scan(argn);
  60.                   a1 = com[1];
  61.                   gchain = schain;
  62.   
  63.                   if (argn != 0)
  64.                       cmdhash = pathlook(com[0], 1, comptr(t)->comset);
  65.  
  66. --- 103,118 -----
  67.                   com = scan(argn);
  68.                   a1 = com[1];
  69.                   gchain = schain;
  70. + #ifdef JOB
  71. +                 if (com[0][0]=='%'){
  72. +                     /* this is incredibly kludgy! */
  73. +                     fgcom[0] = "fg";
  74. +                     fgcom[1] = com[0];
  75. +                     fgcom[2] = 0;
  76. +                     com = fgcom;
  77. +                     argn=2;
  78. +                 }
  79. + #endif JOB
  80.   
  81.                   if (argn != 0)
  82.                       cmdhash = pathlook(com[0], 1, comptr(t)->comset);
  83. ***************
  84. *** 165,171
  85.                               break;    
  86.                   
  87.                           case SYSEXIT:    
  88. !                             flags |= forked;    /* force exit */    
  89.                               exitsh(a1 ? stoi(a1) : retval);
  90.                   
  91.                           case SYSNULL:    
  92.  
  93. --- 183,192 -----
  94.                               break;    
  95.                   
  96.                           case SYSEXIT:    
  97. ! #ifdef JOBSXT
  98. !                                 if ((flags&ttyflg)==ttyflg && stpjobs())
  99. !                                         error("There are blocked jobs");
  100. ! #endif JOBSXT
  101.                               exitsh(a1 ? stoi(a1) : retval);
  102.                   
  103.                           case SYSNULL:    
  104. ***************
  105. *** 250,255
  106.   #ifdef RES    /* Research includes login as part of the shell */    
  107.           
  108.                           case SYSLOGIN:    
  109.                               oldsigs();    
  110.                               execa(com, -1);
  111.                               done();    
  112.  
  113. --- 271,283 -----
  114.   #ifdef RES    /* Research includes login as part of the shell */    
  115.           
  116.                           case SYSLOGIN:    
  117. + #ifdef JOBSXT
  118. +                                 if ((flags&ttyflg)==ttyflg && stpjobs())
  119. +                                         error("There are blocked jobs");
  120. +                             sxtrelease(0);
  121. +                             nattrib(&TTYnod,N_VAR|N_EXPORT);
  122. + #endif JOBSXT
  123. +                             flags |= forked;
  124.                               oldsigs();    
  125.                               execa(com, -1);
  126.                               done();    
  127. ***************
  128. *** 260,265
  129.                                   failed(com[0], restricted);    
  130.                               else    
  131.                               {    
  132.                                   flags |= forked;    /* force bad exec to terminate shell */    
  133.                                   oldsigs();    
  134.                                   execa(com, -1);
  135.  
  136. --- 288,299 -----
  137.                                   failed(com[0], restricted);    
  138.                               else    
  139.                               {    
  140. + #ifdef JOBSXT
  141. +                                     if ((flags&ttyflg)==ttyflg && stpjobs())
  142. +                                             error("There are blocked jobs");
  143. +                                 sxtrelease(0);
  144. +                                 nattrib(&TTYnod,N_EXPORT);
  145. + #endif JOBSXT
  146.                                   flags |= forked;    /* force bad exec to terminate shell */    
  147.                                   oldsigs();    
  148.                                   execa(com, -1);
  149. ***************
  150. *** 335,340
  151.                               break;    
  152.                   
  153.                           case SYSWAIT:    
  154.                               await(a1 ? stoi(a1) : -1, 1);    
  155.                               break;    
  156.                   
  157.  
  158. --- 369,377 -----
  159.                               break;    
  160.                   
  161.                           case SYSWAIT:    
  162. + #ifdef JOB
  163. +                             await(a1 ? stoi(a1) : -1, WAIT_BG);    
  164. + #else
  165.                               await(a1 ? stoi(a1) : -1, 1);    
  166.   #endif
  167.                               break;    
  168. ***************
  169. *** 336,341
  170.                   
  171.                           case SYSWAIT:    
  172.                               await(a1 ? stoi(a1) : -1, 1);    
  173.                               break;    
  174.                   
  175.                           case SYSREAD:    
  176.  
  177. --- 373,379 -----
  178.                               await(a1 ? stoi(a1) : -1, WAIT_BG);    
  179.   #else
  180.                               await(a1 ? stoi(a1) : -1, 1);    
  181. + #endif
  182.                               break;    
  183.                   
  184.                           case SYSREAD:    
  185. ***************
  186. *** 547,552
  187.                                       unset_name(*com);
  188.                               }
  189.                               break;
  190.   
  191.                           default:    
  192.                               prs_buff("unknown builtin\n");
  193.  
  194. --- 585,615 -----
  195.                                       unset_name(*com);
  196.                               }
  197.                               break;
  198. + #ifdef JOB
  199. +                         case SYSKILL:
  200. +                             zipkill(argn,com);
  201. +                             break;
  202. +                         case SYSSTOP:
  203. +                             zipstop(argn,com);
  204. +                             break;
  205. +                         case SYSJOBS:
  206. +                             listjobs(com[1]);
  207. +                             break;
  208. +                         case SYSFG:
  209. + # ifdef JOBSXT
  210. +                             if (sxtchan==-1)
  211. +                                 failed(com[0],badexec);
  212. +                             else 
  213. + # endif JOBSXT
  214. +                               fg(findjob(com[1]));
  215. +                             break;
  216. +                         case SYSBG:
  217. +                             bg(findjob(com[1]));
  218. +                             break;
  219. +                         case SYSNOTIFY:
  220. +                             notify(findjob(com[1]));
  221. +                             break;
  222. + # ifdef JOBSXT
  223.   
  224.                           case SYSSXT1:
  225.                               {
  226. ***************
  227. *** 548,553
  228.                               }
  229.                               break;
  230.   
  231.                           default:    
  232.                               prs_buff("unknown builtin\n");
  233.                           }    
  234.  
  235. --- 611,632 -----
  236.                               break;
  237.   # ifdef JOBSXT
  238.   
  239. +                         case SYSSXT1:
  240. +                             {
  241. +                                 int chsxt;
  242. +                                 extern int crisps;
  243. +                                 if (com[1]){
  244. +                                     chsxt = hide_sxt();
  245. +                                     execexp(com[1],&com[2]);
  246. +                                     if (chsxt)
  247. +                                         restore_sxt();    
  248. +                                 }
  249. +                             }
  250. +                             break;
  251. + # endif JOBSXT
  252. + #endif JOB
  253.                           default:    
  254.                               prs_buff("unknown builtin\n");
  255.                           }    
  256. ***************
  257. *** 586,591
  258.               }
  259.   
  260.           case TFORK:
  261.               exitval = 0;
  262.               if (execflg && (treeflgs & (FAMP | FPOU)) == 0)
  263.                   parent = 0;
  264.  
  265. --- 665,674 -----
  266.               }
  267.   
  268.           case TFORK:
  269. + #ifdef JOB
  270. +             forkprocess(t,treeflgs,com,pf1,pf2,type,execflg,errorflg,pos,exec_link,linked);
  271. +             break;
  272. + #else
  273.               exitval = 0;
  274.               if (execflg && (treeflgs & (FAMP | FPOU)) == 0)
  275.                   parent = 0;
  276. ***************
  277. *** 719,724
  278.                   }
  279.                   done();
  280.               }
  281.   
  282.           case TPAR:
  283.               execute(parptr(t)->partre, exec_link, errorflg);
  284.  
  285. --- 802,808 -----
  286.                   }
  287.                   done();
  288.               }
  289. + #endif JOB
  290.   
  291.           case TPAR:
  292.               execute(parptr(t)->partre, exec_link, errorflg);
  293. ***************
  294. *** 857,862
  295.   int    f;
  296.   {
  297.       struct fileblk    fb;
  298.   
  299.       push(&fb);
  300.       if (s)
  301.  
  302. --- 941,949 -----
  303.   int    f;
  304.   {
  305.       struct fileblk    fb;
  306. + #ifdef JOBSXT
  307. +     BOOL chsxt=FALSE;
  308. + #endif JOBSXT
  309.   
  310.       push(&fb);
  311.       if (s)
  312. ***************
  313. *** 864,870
  314.           estabf(s);
  315.           fb.feval = (char **)(f);
  316.       }
  317. !     else if (f >= 0)
  318.           initf(f);
  319.       execute(cmd(NL, NLFLG | MTFLG), 0, (int)(flags & errflg));
  320.       pop();
  321.  
  322. --- 951,957 -----
  323.           estabf(s);
  324.           fb.feval = (char **)(f);
  325.       }
  326. !     else if (f >= 0){
  327.           initf(f);
  328.   #ifdef JOBSXT
  329.           chsxt = hide_sxt();
  330. ***************
  331. *** 866,871
  332.       }
  333.       else if (f >= 0)
  334.           initf(f);
  335.       execute(cmd(NL, NLFLG | MTFLG), 0, (int)(flags & errflg));
  336.       pop();
  337.   }
  338.  
  339. --- 953,963 -----
  340.       }
  341.       else if (f >= 0){
  342.           initf(f);
  343. + #ifdef JOBSXT
  344. +         chsxt = hide_sxt();
  345. + #endif JOBSXT
  346. +     }
  347. +     else error("execexp: no args");
  348.       execute(cmd(NL, NLFLG | MTFLG), 0, (int)(flags & errflg));
  349.       pop();
  350.   #ifdef JOBSXT
  351. ***************
  352. *** 868,873
  353.           initf(f);
  354.       execute(cmd(NL, NLFLG | MTFLG), 0, (int)(flags & errflg));
  355.       pop();
  356.   }
  357.   
  358.   execprint(com)
  359.  
  360. --- 960,969 -----
  361.       else error("execexp: no args");
  362.       execute(cmd(NL, NLFLG | MTFLG), 0, (int)(flags & errflg));
  363.       pop();
  364. + #ifdef JOBSXT
  365. +     if (chsxt)
  366. +         restore_sxt();
  367. + #endif JOBSXT
  368.   }
  369.   
  370.   
  371. ***************
  372. *** 870,875
  373.       pop();
  374.   }
  375.   
  376.   execprint(com)
  377.       char **com;
  378.   {
  379.  
  380. --- 966,974 -----
  381.   #endif JOBSXT
  382.   }
  383.   
  384.   execprint(com)
  385.       char **com;
  386.   {
  387. ***************
  388. *** 885,887
  389.   
  390.       newline();
  391.   }
  392.  
  393. --- 984,1275 -----
  394.   
  395.       newline();
  396.   }
  397. + #ifdef JOB
  398. + /*
  399. +  * service-things for job-control.
  400. +  */
  401. + /*ARGSUSED*/
  402. + zipkill(argc,argv)
  403. + char **argv;
  404. + {
  405. +     int signo, procid, ret;
  406. +     char * a1 = argv[1];
  407. +     extern char * sys_errlist[];
  408. +     if (!a1) failed(argv[0],synmsg);
  409. +     if (*a1=='-'){
  410. +         a1++;
  411. +         signo=stoi(a1);
  412. +         argv++;
  413. +     } else signo=SIGTERM;
  414. +     while (*++argv){
  415. +         if (**argv=='%'){
  416. +             procid = jobs[findjob(argv[0])]->jb_pgrp;
  417. +             ret = killpg(procid,signo);
  418. +         } else {
  419. +             procid = stoi(*argv);
  420. +             ret = kill(procid,signo);
  421. +             if (ret==-1) ret=errno;
  422. +         }
  423. +         if (ret!=0){
  424. +             prn_buff(procid);
  425. +             prs_buff(" ");
  426. +             prs_buff(procid,sys_errlist[ret]);
  427. +             newline();
  428. +         }
  429. +     }
  430. +     }
  431. + /* ------------------------------- stop ------------------------------ */
  432. + /*ARGSUSED*/
  433. + zipstop(argc,argv)
  434. + char **argv;
  435. + {
  436. +     int id;
  437. +     if (argv[1]==0){
  438. +         register int i;
  439. +         for (i=1; i<JOB_MAX; i++)
  440. +             if (jobs[i]){
  441. + # ifdef JOBSXT
  442. +                 sxtblk(i);
  443. + # endif JOBSXT
  444. +             }
  445. +     } else {
  446. +         argv++;
  447. +         while (argv[1]){
  448. +             if (jobs[id=findjob(argv[1])]){
  449. + # ifdef JOBSXT
  450. +                 sxtblk(id);
  451. + # endif JOBSXT
  452. +             }
  453. +             argv++;
  454. +         }
  455. +     }
  456. + }
  457. + #ifdef JOBSXT
  458. + #undef     input
  459. + #include    <sys/types.h>
  460. + #include    <sys/tty.h>
  461. + #include    <sys/sxt.h>
  462. + #include    <sys/ipc.h>
  463. + #include    <sys/msg.h>
  464. + #endif JOBSXT
  465. + /*
  466. +  *    fork a process, exec the child and deal with job initialization, etc...
  467. +  *    (the parameter-passing is a bit crude)
  468. +  */
  469. + forkprocess(t,treeflgs,com,pf1,pf2,type,execflg,errorflg,pos,exec_link,linked)
  470. + register struct trenod * t;
  471. + register int treeflgs;
  472. + register char * *com;
  473. + int *pf1, *pf2;
  474. + int type,errorflg,pos,exec_link,linked;
  475. + {
  476. + #ifdef JOBSXT
  477. +     struct msgbuf msgbuffer;
  478. +     int msgqid = -1;
  479. + #endif JOBSXT
  480. +     exitval=0;
  481. +     if (execflg && (treeflgs&(FAMP|FPOU))==0)
  482. +         parent=0;
  483. +     else {
  484. +         int forkcnt = 1;
  485. +         if (treeflgs & (FAMP | FPOU))
  486. +         {
  487. +             link_iodocs(iotemp);
  488. +             linked = 1;
  489. +         }
  490. +         if ((treeflgs&FPIN)==0){
  491. +             bookjob();    /* set up a job slot */
  492. + #ifdef JOBSXT
  493. +             if (sxtchan!=-1)
  494. +                 msgqid = msgget(IPC_PRIVATE,0);
  495. + #endif JOBSXT
  496. +         }
  497. +         while ((parent=fork()) == -1){
  498. +             if ((forkcnt = (forkcnt * 2)) > FORKLIM)    /* 32 */
  499. +             {
  500. +                 switch (errno)
  501. +                 {
  502. +                 case ENOMEM:
  503. +                     error(noswap);
  504. +                     break;
  505. +                 default:
  506. +                 case EAGAIN:
  507. +                     error(nofork);
  508. +                     break;
  509. +                 }
  510. +             }
  511. +             if (trapnote&SIGSET){
  512. +                 postjclr(currentjob);
  513. + #ifdef JOBSXT
  514. +                 if (sxtchan!=-1)
  515. +                     msgctl(msgqid, IPC_RMID, (struct msqid_ds *)0);
  516. + #endif JOBSXT
  517. +                 sigchk();
  518. +             }
  519. +             alarm(forkcnt);
  520. +             pause();
  521. +         }
  522. +     }
  523. +     if (parent){
  524. +         /* This is the parent branch of fork;     */
  525. +         /* it may or may not wait for the child. */
  526. +         struct argnod *Dcom;
  527. +         if (treeflgs&FPCL) closepipe(pf1);
  528. +         while (type==TLST || type==TFIL || type==TFORK){
  529. +             if (type==TFORK)
  530. +                 t=forkptr(t)->forktre;
  531. +             else    t=lstptr(t)->lstlef;
  532. +             type=(t->tretyp)&COMMSK;
  533. +         }
  534. +         if (type==TCOM)
  535. +             Dcom = comptr(t)->comarg;
  536. +         else    Dcom = 0;
  537. +         if ((treeflgs&FPIN)==0){
  538. + #ifdef JOBSXT
  539. +             if (sxtchan!=-1){
  540. +                 msgrcv(msgqid, &msgbuffer,0,0,0);
  541. +                 msgctl(msgqid, IPC_RMID, (struct msqid_ds *)0);
  542. +                 sxtchan = sxtopen(SXT(currentjob));
  543. +                 newjob(parent,Dcom,treeflgs&FAMP,sxtchan);
  544. +                 if (sxtchan==-1) sxtchan=0;    /* fix */
  545. +             } else  newjob(parent,Dcom,treeflgs&FAMP,-1);
  546. + #else
  547. +             newjob(parent,Dcom,treeflgs&FAMP,0);
  548. + #endif JOBSXT
  549. +         }
  550. +         if (treeflgs&FPRS && (flags&(ttyflg|prompt))==(ttyflg|prompt)){
  551. +             prc('[');
  552. +             prn(currentjob);
  553. +             prs("]\t");
  554. +             prn(parent);
  555. +             newline();
  556. +         }
  557. +         if ((treeflgs&(FAMP|FPOU))==0)
  558. +             await(parent,WAIT_FG);    /* a process, in the foreground */
  559. +         else if ((treeflgs&FAMP)==0)
  560. +             post(parent);
  561. +         else {
  562. +             assnum(&pcsadr, parent);
  563. +             post(parent);
  564. +             bg(currentjob);        /* set it running in bg, and reset current job to 0 */
  565. +         }
  566. +         chktrap();
  567. +         return;
  568. +     } else    {    /* this is the forked branch (child) of execute */
  569. +         flags |= forked;
  570. +         fiotemp  = 0;
  571. +         if (linked == 1)
  572. +         {
  573. +             swap_iodoc_nm(iotemp);
  574. +             exec_link |= 06;
  575. +         }
  576. +         else if (linked == 0)
  577. +             iotemp = 0;
  578. + #ifdef ACCT
  579. +         suspacct();
  580. + #endif
  581. +         setchildjob(currentjob,0);
  582. +         postclr();
  583. +         settmp();
  584. +         /* Turn off INTR and QUIT if `FINT'  */
  585. +         /* Reset ramaining signals to parent */
  586. +         /* except for those `lost' by trap   */
  587. +         oldsigs();
  588. +         fixsigs();
  589. + #ifdef JOBSXT
  590. +         if ((iflags&jobflg)==0)
  591. + #endif JOBSXT
  592. +           if (treeflgs&FINT){
  593. +             signal(SIGINT,SIG_IGN);
  594. +             signal(SIGQUIT,SIG_IGN);
  595. + #ifdef NICE
  596. +             if (treeflgs&FAMP)
  597. +                 nice(NICEVAL);
  598. + #endif
  599. +           }
  600. + #ifdef JOBSXT
  601. +         /* reset fildes 0,1,2 through sxt channel */
  602. +         if (sxtchan!=-1){
  603. +             if (currentjob<SXTMAX)
  604. +                 setpgrp();
  605. +             sxtchan=sxtopen(SXT(currentjob));
  606. +             if ((treeflgs&(FPIN|FPOU))!=FPOU)    /*pipes once*/
  607. +                 setchildjob(currentjob, ((treeflgs&FAMP)==0)?1:2);
  608. +             if (sxtchan != -1){
  609. +                 close(0); close(1); close(2);
  610. +                 dup(sxtchan);
  611. +                 dup(0); dup(0); /* stdout and stderr */
  612. +             }
  613. +             sxtchan = -1;    /* prevent subshell jobs */
  614. + #ifdef SXT_SUSPEND
  615. +             if (treeflgs&FAMP)
  616. + #endif
  617. +                 close(sxt_fd);
  618. +             msgbuffer.mtype = 1;
  619. +             msgsnd(msgqid, msgbuffer, 0, 0);
  620. +             msgctl(msgqid, IPC_RMID, (struct msqid_ds *)0);    /* delete in kid too */
  621. +         }
  622. +         if ((iflags&jobflg) && (treeflgs&(FPIN|FPOU))==FPOU){
  623. +             alarm(1);
  624. +             pause();    /* Horrendous Kludge! */
  625. +         }
  626. + #endif JOBSXT
  627. +         /* pipe in or out */
  628. +         if (treeflgs&FPIN){
  629. +             rename(pf1[INPIPE],0);
  630. +             close(pf1[OTPIPE]);
  631. +         }
  632. +         if (treeflgs&FPOU){
  633. +             rename(pf2[OTPIPE],1);
  634. +             close(pf2[INPIPE]);
  635. +         }
  636. +         /* default std input for & */
  637. + #ifdef JOBSXT
  638. +         if ((iflags&jobflg)==0)       /* only if no job-control */
  639. + #endif JOBSXT
  640. +           if ((treeflgs&FINT) && ioset==0){
  641. +             int devvy = chkopen(devnull);
  642. +             rename(devvy,0);
  643. +           }
  644. +         iflags &= ~jobflg;
  645. +         /* io redirection */
  646. +         initio(t->treio,0);
  647. +         if (type!=TCOM){
  648. +             execute(forkptr(t)->forktre,exec_link|01,errorflg);
  649. +         } else if (com[0]!=ENDARGS){
  650. +             eflag = 0;
  651. +             setlist(comptr(t)->comset,N_EXPORT);
  652. +             rmtemp(0);
  653. +             execa(com,pos);
  654. +         }
  655. +         flags &= ~ttyflg;     /* don't reset tty pgrp */
  656. +         done();
  657. +     }
  658. + }
  659. + #endif JOB
  660.