home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / jove-4.16-src.tgz / tar.out / bsd / jove / proc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  24KB  |  970 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986-1996 by Jonathan Payne.  JOVE is  *
  3.  * provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is *
  5.  * included in all the files.                                           *
  6.  ************************************************************************/
  7.  
  8. #include "jove.h"
  9.  
  10. #include "jctype.h"
  11. #include "fp.h"
  12. #include "re.h"
  13. #include "disp.h"
  14. #include "sysprocs.h"
  15. #include "ask.h"
  16. #include "delete.h"
  17. #include "extend.h"
  18. #include "fmt.h"
  19. #include "insert.h"
  20. #ifdef IPROCS
  21. # include "iproc.h"
  22. #endif
  23. #include "marks.h"
  24. #include "misc.h"
  25. #include "move.h"
  26. #include "proc.h"
  27. #include "wind.h"
  28.  
  29. #include <signal.h>
  30. #include <errno.h>
  31.  
  32. #ifdef MSDOS_PROCS
  33. # include <io.h>
  34. # ifndef MSC51
  35. #  include <sys/stat.h>    /* for S_IWRITE and S_IREAD */
  36. # endif
  37. # include <process.h>
  38. #endif /* WIN32 */
  39.  
  40. #ifdef POSIX_SIGS
  41. # define SIGINTMASK_DECL    sigset_t sigintmask;
  42. # define SIGINTMASK_INIT()    { sigemptyset(&sigintmask); sigaddset(&sigintmask, SIGINT); }
  43. # define SIGINT_BLOCK()    sigprocmask(SIG_BLOCK, &sigintmask, (sigset_t *)NULL)
  44. # define SIGINT_UNBLOCK()    sigprocmask(SIG_UNBLOCK, &sigintmask, (sigset_t *)NULL)
  45. #else /* !POSIX_SIGS */
  46. # ifdef USE_SIGSET
  47. #  define SIGINT_BLOCK()    sighold(SIGINT)
  48. #  define SIGINT_UNBLOCK()    sigrelse(SIGINT)
  49. # else /* !USE_SIGSET */
  50. #  ifdef BSD_SIGS
  51. #   define SIGINT_BLOCK()    sigsetmask(sigmask(SIGINT))
  52. #   define SIGINT_UNBLOCK()    sigsetmask(0)
  53. #  endif /* BSD_SIGS */
  54. # endif /* !USE_SIGSET */
  55. #endif /* !POSIX_SIGS */
  56.  
  57. /* This disgusting RE search string parses output from the GREP
  58.    family, from the pdp11 compiler, pcc, and lint.  Jay (HACK)
  59.    Fenlasen changed this to work for the lint errors. */
  60. char    ErrFmtStr[256] = "^\\{\"\\|\\}\\([^:\"( \t]*\\)\\{\"\\, line \\|:\\|(\\} *\\([0-9][0-9]*\\)[:)]\
  61. \\|::  *\\([^(]*\\)(\\([0-9]*\\))$\
  62. \\|( \\([^(]*\\)(\\([0-9]*\\)) ),";    /* VAR: format string for parse errors */
  63.  
  64. struct error {
  65.     Buffer        *er_buf;    /* Buffer error is in */
  66.     LinePtr        er_mess,    /* Actual error message */
  67.             er_text;    /* Actual error */
  68.     int        er_char;    /* char pos of error */
  69.     struct error    *er_prev,    /* List of errors */
  70.             *er_next;
  71. };
  72.  
  73. private struct error    *cur_error = NULL,
  74.         *errorlist = NULL;
  75.  
  76. /* Eliminate any error records that contain dangling references to Lines.
  77.  * We only eliminate error structs when either referent is recycled.
  78.  * If it deleted, we keep it (dormant) in case it will be pasted back
  79.  * into the same buffer.
  80.  */
  81.  
  82. void
  83. ChkErrorLines()
  84. {
  85.     register struct error    *e;
  86.     struct error    *prev = NULL;
  87.  
  88.     for (e = errorlist; e != NULL; ) {
  89.         struct error    *next = e->er_next;
  90.  
  91.         if (e->er_mess->l_dline == NULL_DADDR
  92.         || e->er_text->l_dline == NULL_DADDR)
  93.         {
  94.             /* dangling reference: delete */
  95.             if (prev == NULL)
  96.                 errorlist = next;
  97.             else
  98.                 prev->er_next = next;
  99.             if (next != NULL)
  100.                 next->er_prev = prev;
  101.             if (cur_error == e)
  102.                 cur_error = next;
  103.             free((UnivPtr)e);
  104.         } else {
  105.             prev = e;
  106.         }
  107.         e = next;
  108.     }
  109. }
  110.  
  111. /* Add an error to the end of the list of errors.  This is used for
  112.    parse-{C,LINT}-errors and for the spell-buffer command */
  113.  
  114. private struct error *
  115. AddError(laste, errline, buf, line, charpos)
  116. struct error    *laste;
  117. LinePtr    errline,
  118.     line;
  119. Buffer    *buf;
  120. int    charpos;
  121. {
  122.     struct error    *new = (struct error *) emalloc(sizeof *new);
  123.  
  124.     new->er_prev = laste;
  125.     if (laste == NULL) {
  126.         /* first time: free up old errors */
  127.         if (errorlist != NULL)
  128.             ErrFree();
  129.         cur_error = errorlist = new;
  130.     } else {
  131.         laste->er_next = new;
  132.     }
  133.     new->er_next = NULL;
  134.     new->er_buf = buf;
  135.     new->er_text = line;
  136.     new->er_char = charpos;
  137.     new->er_mess = errline;
  138.  
  139.     return new;
  140. }
  141.  
  142. void
  143. get_FL_info(fname, lineno)
  144. char    *fname,
  145.     *lineno;
  146. {
  147.     putmatch(1, fname, (size_t)FILESIZE);
  148.     putmatch(2, lineno, (size_t)FILESIZE);
  149.  
  150.     /* error had lineno followed fname, so switch the two */
  151.     if (!jisdigit(lineno[0])) {
  152.         char    tmp[FILESIZE];
  153.  
  154.         strcpy(tmp, lineno);
  155.         strcpy(lineno, fname);
  156.         strcpy(fname, tmp);
  157.     }
  158. }
  159.  
  160. /* Free up all the errors */
  161.  
  162. void
  163. ErrFree()
  164. {
  165.     register struct error    *ep;
  166.  
  167.     for (ep = errorlist; ep != NULL; ep = ep->er_next)
  168.         free((UnivPtr) ep);
  169.     errorlist = cur_error = NULL;
  170. }
  171.  
  172. /* Parse errors of the form specified in ErrFmtStr in the current
  173.    buffer.  Do a show error of the first error.  This is neat because this
  174.    will work for any kind of output that prints a file name and a line
  175.    number on the same line. */
  176.  
  177. void
  178. ErrParse()
  179. {
  180.     struct RE_block    re_blk;
  181.     Bufpos    *bp;
  182.     char    fname[FILESIZE],
  183.         lineno[FILESIZE];
  184.     int    lnum,
  185.         last_lnum = -1;
  186.     struct error    *ep = NULL;
  187.     Buffer    *buf,
  188.         *lastb = NULL;
  189.     LinePtr    err_line = NULL;    /* avoid uninitialized complaint from gcc -W */
  190.  
  191.     ErrFree();        /* This is important! */
  192.     ToFirst();
  193.     perr_buf = curbuf;
  194.     REcompile(ErrFmtStr, YES, &re_blk);
  195.     /* Find a line with a number on it. */
  196.     while ((bp = docompiled(FORWARD, &re_blk)) != NULL) {
  197.         SetDot(bp);
  198.         get_FL_info(fname, lineno);
  199.         buf = do_find((Window *)NULL, fname, YES, YES);
  200.         (void) chr_to_int(lineno, 10, NO, &lnum);
  201.         if (buf != lastb) {
  202.             lastb = buf;
  203.             last_lnum = 1;        /* new file */
  204.             err_line = buf->b_first;
  205.         } else if (lnum == last_lnum)    /* one error per line is nicer */
  206.             continue;
  207.         err_line = next_line(err_line, lnum - last_lnum);
  208.         ep = AddError(ep, curline, buf, err_line, 0);
  209.         last_lnum = lnum;
  210.     }
  211.     if (cur_error != NULL)
  212.         ShowErr();
  213. }
  214.  
  215. private void
  216. NeedErrors()
  217. {
  218.     if (cur_error == NULL)
  219.         complain("No errors!");
  220. }
  221.  
  222. private bool
  223. ErrorHasReferents()
  224. {
  225.     return inlist(cur_error->er_buf->b_first, cur_error->er_text)
  226.         && inlist(perr_buf->b_first, cur_error->er_mess);
  227. }
  228.  
  229. /* Go the the next error, if there is one.  Put the error buffer in
  230.    one window and the buffer with the error in another window.
  231.    It checks to make sure that the error actually exists. */
  232.  
  233. private void
  234. ToError(forward)
  235. bool    forward;
  236. {
  237.     register struct error    *e = cur_error;
  238.     int    num = arg_value();
  239.  
  240.     NeedErrors();
  241.     if ((forward? e->er_next : e->er_prev) == NULL) {
  242.         s_mess("You're at the %s error.", forward ? "last" : "first");
  243.     } else {
  244.         while (--num >= 0 || !ErrorHasReferents()) {
  245.             e = forward ? e->er_next : e->er_prev;
  246.             if (e == NULL)
  247.                 break;
  248.             cur_error = e;
  249.         }
  250.         ShowErr();
  251.     }
  252. }
  253.  
  254. void
  255. NextError()
  256. {
  257.     ToError(YES);
  258. }
  259.  
  260. void
  261. PrevError()
  262. {
  263.     ToError(NO);
  264. }
  265.  
  266. int    EWSize = 20;    /* VAR: percentage of screen to make the error window */
  267.  
  268. private void
  269. set_wsize(wsize)
  270. int    wsize;
  271. {
  272.     wsize = (LI * wsize) / 100;
  273.     if (wsize >= 1 && !one_windp())
  274.         WindSize(curwind, wsize - (curwind->w_height - 1));
  275. }
  276.  
  277. /* Show the current error, i.e. put the line containing the error message
  278.    in one window, and the buffer containing the actual error in another
  279.    window. */
  280.  
  281. void
  282. ShowErr()
  283. {
  284.     Window    *err_wind,
  285.         *buf_wind;
  286.  
  287.     NeedErrors();
  288.     if (!ErrorHasReferents()) {
  289.         rbell();
  290.         return;
  291.     }
  292.     err_wind = windbp(perr_buf);
  293.     buf_wind = windbp(cur_error->er_buf);
  294.  
  295.     if (err_wind == NULL) {
  296.         if (buf_wind != NULL) {
  297.             SetWind(buf_wind);
  298.             pop_wind(perr_buf->b_name, NO, -1);
  299.             err_wind = curwind;
  300.         } else {
  301.             pop_wind(perr_buf->b_name, NO, -1);
  302.             err_wind = curwind;
  303.             pop_wind(cur_error->er_buf->b_name, NO, -1);
  304.             buf_wind = curwind;
  305.         }
  306.     } else if (buf_wind == NULL) {
  307.         SetWind(err_wind);
  308.         pop_wind(cur_error->er_buf->b_name, NO, -1);
  309.         buf_wind = curwind;
  310.     }
  311.  
  312.     /* Put the current error message at the top of its Window */
  313.     SetWind(err_wind);
  314.     SetLine(cur_error->er_mess);
  315.     SetTop(curwind, (curwind->w_line = cur_error->er_mess));
  316.     set_wsize(EWSize);
  317.  
  318.     /* now go to the the line with the error in the other window */
  319.     SetWind(buf_wind);
  320.     DotTo(cur_error->er_text, cur_error->er_char);
  321. }
  322.  
  323. char    ShcomBuf[LBSIZE];
  324.  
  325. /* Make a buffer name given the command `command', i.e. "fgrep -n foo *.c"
  326.    will return the buffer name "fgrep".  */
  327.  
  328. char *
  329. MakeName(command)
  330. char    *command;
  331. {
  332.     static char    bnm[50];
  333.     register char    *cp = bnm,
  334.             c;
  335.  
  336.     do {
  337.         c = *command++;
  338.     } while (jiswhite(c));
  339.     while (cp < &bnm[sizeof(bnm) - 1] && c != '\0' && !jiswhite(c)) {
  340.         *cp++ = c;
  341.         c = *command++;
  342.     }
  343.     *cp = '\0';
  344.     strcpy(bnm, basename(bnm));
  345.  
  346.     return bnm;
  347. }
  348.  
  349. #ifdef SUBSHELL    /* the body is the rest of this file */
  350.  
  351. /* Run make, first writing all the modified buffers (if the WtOnMk flag is
  352.    on), parse the errors, and go the first error. */
  353.  
  354. bool    WtOnMk = YES;        /* VAR: write files on compile-it command */
  355. bool    WrapProcessLines = NO;    /* VAR: wrap process lines at CO-1 chars */
  356.  
  357. private void
  358.     DoShell proto((char *, char *)),
  359.     com_finish proto((wait_status_t, char *));
  360.  
  361. private char    make_cmd[LBSIZE] = "make";
  362.  
  363. void
  364. MakeErrors()
  365. {
  366.     Window    *old = curwind;
  367.  
  368.     if (WtOnMk)
  369.         put_bufs(NO);
  370.  
  371.     /* When we're not doing make or cc (i.e., the last command
  372.        was probably a grep or something) and the user just types
  373.        ^X ^E, he probably (possibly, hopefully, usually (in my
  374.        case)) doesn't want to do the grep again but rather wants
  375.        to do a make again; so we ring the bell and insert the
  376.        default command and let the person decide. */
  377.  
  378.     if (is_an_arg()
  379.     || !(sindex("make", make_cmd) || sindex("cc", make_cmd)))
  380.     {
  381.         if (!is_an_arg())
  382.             rbell();
  383.         /* insert the default for the user (Kludge: only if Inputp is free) */
  384.         if (Inputp == NULL)
  385.             Inputp = make_cmd;
  386.         null_ncpy(make_cmd, ask(make_cmd, "Compilation command: "),
  387.                 sizeof (make_cmd) - 1);
  388.     }
  389.     com_finish(UnixToBuf(UTB_DISP|UTB_CLOBBER|UTB_ERRWIN|UTB_SH,
  390.         MakeName(make_cmd), (char *)NULL, make_cmd), make_cmd);
  391.  
  392.     ErrParse();
  393.  
  394.     if (!cur_error)
  395.         SetWind(old);
  396. }
  397.  
  398. # ifdef SPELL
  399.  
  400. private void
  401. SpelParse(bname)
  402. char    *bname;
  403. {
  404.     Buffer    *buftospel,
  405.         *wordsb;
  406.     char    wordspel[100];
  407.     Bufpos    *bp;
  408.     struct error    *ep = NULL;
  409.  
  410.     ErrFree();        /* This is important! */
  411.  
  412.     buftospel = curbuf;
  413.     wordsb = buf_exists(bname);
  414.     if (wordsb == NULL)
  415.         complain("Buffer %s is gone!", bname);
  416.     perr_buf = wordsb;    /* This is important (buffer containing
  417.                    error messages) */
  418.     SetBuf(wordsb);
  419.     ToFirst();
  420.     f_mess("Finding misspelled words ... ");
  421.     while (!lastp(curline)) {
  422.         swritef(wordspel, sizeof(wordspel), "\\<%s\\>", linebuf);
  423.         SetBuf(buftospel);
  424.         ToFirst();
  425.         while ((bp = dosearch(wordspel, FORWARD, NO)) != NULL) {
  426.             SetDot(bp);
  427.             ep = AddError(ep, wordsb->b_dot, buftospel,
  428.                       curline, curchar);
  429.         }
  430.         SetBuf(wordsb);
  431.         line_move(FORWARD, 1, NO);
  432.     }
  433.     add_mess("Done.");
  434.  
  435.     /* undo buffer switches that ought not to be reflected in current window */
  436.     SetBuf(curwind->w_bufp);
  437.  
  438.     ShowErr();
  439. }
  440.  
  441. void
  442. SpelBuffer()
  443. {
  444.     char    *Spell = "Spell",
  445.         com[100];
  446.     Buffer    *savebp = curbuf;
  447.  
  448.     if (curbuf->b_fname == NULL)
  449.         complain("no file name");
  450.     if (IsModified(curbuf))
  451.         SaveFile();
  452.     swritef(com, sizeof(com), "spell %s", curbuf->b_fname);
  453.     (void) UnixToBuf(UTB_DISP|UTB_CLOBBER|UTB_ERRWIN|UTB_SH,
  454.         Spell, (char *)NULL, com);
  455.     message("[Delete the irrelevant words and then type ^X ^C]");
  456.     ToFirst();
  457.     Recur();
  458.     if (!valid_bp(savebp))
  459.         complain("Buffer gone!");
  460.     SetBuf(savebp);
  461.     SpelParse(Spell);
  462. }
  463.  
  464. void
  465. SpelWords()
  466. {
  467.     Buffer    *wordsb = curbuf;
  468.     char    *buftospel = ask_buf((Buffer *)NULL, ALLOW_OLD | ALLOW_INDEX);
  469.  
  470.     SetBuf(do_select(curwind, buftospel));
  471.     SpelParse(wordsb->b_name);
  472. }
  473.  
  474. # endif /* SPELL */
  475.  
  476. void
  477. ShToBuf()
  478. {
  479.     char    bnm[128],
  480.         cmd[LBSIZE];
  481.  
  482.     strcpy(bnm, ask((char *)NULL, "Buffer: "));
  483.     strcpy(cmd, ask(ShcomBuf, "Command: "));
  484.     DoShell(bnm, cmd);
  485. }
  486.  
  487. void
  488. ShellCom()
  489. {
  490.     null_ncpy(ShcomBuf, ask(ShcomBuf, ProcFmt), (sizeof ShcomBuf) - 1);
  491.     DoShell(MakeName(ShcomBuf), ShcomBuf);
  492. }
  493.  
  494. void
  495. ShNoBuf()
  496. {
  497.     null_ncpy(ShcomBuf, ask(ShcomBuf, ProcFmt), (sizeof ShcomBuf) - 1);
  498.     com_finish(UnixToBuf(UTB_SH|UTB_FILEARG, (char *)NULL, (char *)NULL,
  499.         ShcomBuf), ShcomBuf);
  500. }
  501.  
  502. void
  503. Shtypeout()
  504. {
  505.     wait_status_t    status;
  506.  
  507.     null_ncpy(ShcomBuf, ask(ShcomBuf, ProcFmt), (sizeof ShcomBuf) - 1);
  508.     status = UnixToBuf(UTB_DISP|UTB_SH|UTB_FILEARG, (char *)NULL, (char *)NULL,
  509.         ShcomBuf);
  510. #ifdef MSDOS_PROCS
  511.     if (status < 0)
  512.         Typeout("[%s: not executed %d]", ShcomBuf, status);
  513.     else if (status > 0)
  514.         Typeout("[%s: exited with %d]", ShcomBuf, status);
  515.     else if (!is_an_arg())
  516.         Typeout("[%s: completed successfully]", ShcomBuf);
  517. #else /* !MSDOS_PROCS */
  518.     if (WIFSIGNALED(status))
  519.         Typeout("[%s: terminated by signal %d]", ShcomBuf, WTERMSIG(status));
  520.     else if (WIFEXITED(status) && WEXITSTATUS(status)!=0)
  521.         Typeout("[%s: exited with %d]", ShcomBuf, WEXITSTATUS(status));
  522.     else if (!is_an_arg())
  523.         Typeout("[%s: completed successfully]", ShcomBuf);
  524. #endif /* !MSDOS_PROCS */
  525.     TOstop();
  526. }
  527.  
  528. /* Run the shell command into `bnm'.  Empty the buffer except when we
  529.    give a numeric argument, in which case it inserts the output at the
  530.    current position in the buffer.  */
  531.  
  532. private void
  533. DoShell(bnm, command)
  534. char    *bnm,
  535.     *command;
  536. {
  537.     Window    *savewp = curwind;
  538.  
  539.     com_finish(UnixToBuf(
  540.         (is_an_arg()
  541.             ? UTB_DISP|UTB_SH|UTB_FILEARG
  542.             : UTB_DISP|UTB_CLOBBER|UTB_SH|UTB_FILEARG),
  543.         bnm, (char *)NULL, command), command);
  544.     SetWind(savewp);
  545. }
  546.  
  547. private void
  548. com_finish(status, cmd)
  549. wait_status_t    status;
  550. char    *cmd;
  551. {
  552. #ifdef MSDOS_PROCS
  553.     if (status < 0)
  554.         s_mess("[%s: not executed %d]", cmd, status);
  555.     else if (status > 0)
  556.         s_mess("[%s: exited with %d]", cmd, status);
  557.     else
  558.         s_mess("[%s: completed successfully]", cmd);
  559. #else /* !MSDOS_PROCS */
  560.     if (WIFSIGNALED(status))
  561.         s_mess("[%s: terminated by signal %d]", cmd, WTERMSIG(status));
  562.     else if (WIFEXITED(status) && WEXITSTATUS(status)!=0)
  563.         s_mess("[%s: exited with %d]", cmd, WEXITSTATUS(status));
  564.     else
  565.         s_mess("[%s: completed successfully]", cmd);
  566. #endif /* !MSDOS_PROCS */
  567. }
  568.  
  569. #ifndef MSDOS_PROCS
  570.  
  571. /* pid of any outstanding non-iproc process.
  572.  * Note: since there is only room for one pid, there can be no more than
  573.  * one running non-iproc process.
  574.  */
  575. pid_t    ChildPid;
  576.  
  577. void
  578. dowait(status)
  579. wait_status_t    *status;    /* may be NULL */
  580. {
  581. # ifdef IPROCS
  582.     while (DeadPid != ChildPid) {
  583.         wait_status_t    w;
  584.         pid_t    rpid = wait(&w);
  585.  
  586.         if (rpid == -1) {
  587.             if (errno == ECHILD) {
  588.                 /* fudge what we hope is a bland value */
  589.                 byte_zero((UnivPtr)&DeadStatus, sizeof(wait_status_t));
  590.                 break;
  591.             }
  592.         } else {
  593.             kill_off(rpid, w);
  594.         }
  595.     }
  596.     DeadPid = 0;
  597.     if (status != NULL)
  598.         *status = DeadStatus;
  599. # else
  600.     wait_status_t    w;
  601.  
  602.     for (;;) {
  603.         pid_t    rpid = wait(&w);
  604.  
  605.         if (rpid == -1) {
  606.             if (errno == ECHILD) {
  607.                 /* fudge what we hope is a bland value */
  608.                 byte_zero((UnivPtr)&w, sizeof(wait_status_t));
  609.                 break;
  610.             }
  611.         } else if (rpid == ChildPid) {
  612.             break;
  613.         }
  614.     }
  615.     if (status != NULL)
  616.         *status = w;
  617. # endif
  618.     ChildPid = 0;
  619. }
  620.  
  621. #endif /* !MSDOS_PROCS */
  622.  
  623. /* Run the command cmd.  Output to the buffer named bnm (if not
  624.    NULL), first erasing bnm (if UTB_DISP and UTB_CLOBBER), and
  625.    redisplay (if UTB_DISP).  Leaves bnm as the current buffer and
  626.    leaves any windows it creates lying around.  It's up to the
  627.    caller to fix everything up after we're done.  (Usually there's
  628.    nothing to fix up.)
  629.  
  630.    If bnm is non-NULL, the process output goes to that buffer.
  631.    Furthermore, if UTB_DISP, the buffer is displayed in a window.
  632.    If not UTB_DISP, the buffer is not given a window (of course it
  633.    might already have one).  If UTB_DISP and UTB_CLOBBER, the buffer
  634.    is emptied initially.  If UTB_DISP and UTB_ERRWIN, that window's
  635.    size is as specified by the variable error-window-size.
  636.  
  637.    If bnm is NULL, the process output does not go to a buffer.  In this
  638.    case, if UTB_DISP, it is displayed using Typeout; if not UTB_DISP,
  639.    the output is discarded.
  640.  
  641.    Only if UTB_DISP and bnm is non-NULL are UTB_ERRWIN and
  642.    UTB_CLOBBER meaningful. */
  643.  
  644. wait_status_t
  645. UnixToBuf(flags, bnm, InFName, cmd)
  646.     int    flags;    /* bunch of booleans: see UTB_* in proc.h */
  647.     char    *bnm;    /* buffer name (NULL means none) */
  648.     char    *InFName;    /* name of file for process stdin (NULL means none) */
  649.     char    *cmd;    /* command to run */
  650. {
  651. #ifndef MSDOS_PROCS
  652.     int    p[2];
  653.     wait_status_t    status;
  654.     SIGHANDLERTYPE    old_int;
  655. #else /* MSDOS_PROCS */
  656.     char    cmdbuf[129];
  657.     int    status;
  658.     char    pnbuf[FILESIZE];
  659.     char    *pipename;
  660. #endif /* MSDOS_PROCS */
  661.     bool    eof;
  662.     char    *argv[7];    /* worst case: /bin/sh sh -cf "echo $1" $1 $1 NULL */
  663.     char    **ap = argv;
  664.     File    *fp;
  665. #ifdef SIGINTMASK_DECL
  666.     SIGINTMASK_DECL
  667.  
  668.     SIGINTMASK_INIT();
  669. #endif
  670.  
  671.     SlowCmd += 1;;
  672.     if (flags & UTB_SH) {
  673.             *ap++ = Shell;
  674.             *ap++ = basename(Shell);
  675.             *ap++ = ShFlags;
  676.             *ap++ = cmd;
  677. #ifdef MSDOS_PROCS
  678.             /* Kludge alert!
  679.              * UNIX-like DOS shells and command.com-like DOS shells
  680.              * seem to differ seem to differ on two points:
  681.              * - UNIX-like shells use "-" to start flags whereas
  682.              *   command.com-like shells use "/".
  683.              * - UNIX-like shells seem to require that the argument to
  684.              *   $SHELL -c be quoted to cause it to be taken as a single argument.
  685.              *   command.com-like shells seem to automatically use the rest
  686.              *   of the arguments.  This is not an issue under real UNIX
  687.              *   since arguments are passed already broken down.
  688.              *
  689.              * E.g., your shell comand: echo foo
  690.              *         jove runs: command /c echo foo     OK
  691.              *         jove runs: sh -c echo foo          Oops! sh just runs "echo"
  692.              *         jove runs: sh -c "echo foo"        Ah, now I get it.
  693.              *
  694.              * We use the first character of ShFlags to distinguish
  695.              * which kind of shell we are dealing with!
  696.              */
  697.             if (ShFlags[0] == '-') {
  698.                 swritef(cmdbuf, sizeof(cmdbuf), "\"%s\"", cmd);
  699.                 ap[-1] = cmdbuf;
  700.                 /* ??? can we usefully jam in a copy or two of current filename? */
  701.             }
  702. #else /* !MSDOS_PROCS */
  703.             /* Two copies of the file name are passed to the shell:
  704.              * The Cshell uses the first as a definition of $1.
  705.              * Most versions of the Bourne shell use the second as a
  706.              * definition of $1.  (Unfortunately, these same versions
  707.              * of the Bourne shell take the first as their own name
  708.              * for error reporting.)
  709.              */
  710.             if (flags & UTB_FILEARG) {
  711.                 char    *fn = pr_name(curbuf->b_fname, NO);
  712.  
  713.                 *ap++ = fn;    /* NOTE: NULL simply terminates argv */
  714.                 *ap++ = fn;
  715.             }
  716. #endif /* !MSDOS_PROCS */
  717.     } else {
  718.         *ap++ = cmd;
  719.         *ap++ = basename(cmd);
  720.     }
  721.     *ap++ = NULL;
  722.  
  723.     if (access(argv[0], X_OK) != 0) {
  724.         complain("[Couldn't access %s: %s]", argv[0], strerror(errno));
  725.         /* NOTREACHED */
  726.     }
  727.     if (flags & UTB_DISP) {
  728.         if (bnm != NULL) {
  729.             if (flags & UTB_CLOBBER) {
  730.                 isprocbuf(bnm);
  731.                 pop_wind(bnm, YES, B_PROCESS);
  732.             } else {
  733.                 pop_wind(bnm, NO, B_FILE);
  734.             }
  735.             set_wsize(flags & UTB_ERRWIN? EWSize : 0);
  736.             message("Starting up...");
  737.             redisplay();
  738.         } else {
  739.             TOstart(argv[0]);
  740.             Typeout("Starting up...");
  741.             TOstart(argv[0]);    /* overwrite "Starting up..." */
  742.         }
  743.     }
  744.     /* Now I will attempt to describe how I deal with signals during
  745.        the execution of the shell command.  My desire was to be able
  746.        to interrupt the shell command AS SOON AS the window pops up.
  747.        So, if we have SIGINT_BLOCK (i.e., a modern signal mechanism)
  748.        I hold SIGINT, meaning if we interrupt now, we will eventually
  749.        see the interrupt, but not before we are ready for it.  We
  750.        fork, the child releases the interrupt, it then sees the
  751.        interrupt, and so exits.  Meanwhile the parent ignores the
  752.        signal, so if there was a pending one, it's now lost.
  753.  
  754.        Without SIGINT_BLOCK, the best behavior you can expect is that
  755.        when you type ^] too soon after the window pops up, it may
  756.        be ignored.  The behavior BEFORE was that it would interrupt
  757.        JOVE and then you would have to continue JOVE and wait a
  758.        little while longer before trying again.  Now that is fixed,
  759.        in that you just have to type it twice. */
  760.  
  761. #ifndef MSDOS_PROCS
  762.     dopipe(p);
  763.  
  764. # ifdef SIGINT_BLOCK
  765.     SIGINT_BLOCK();
  766. # else
  767.     old_int = setsighandler(SIGINT, SIG_IGN),
  768. # endif
  769.  
  770. # ifdef USE_VFORK
  771.     ChildPid = vfork();
  772. # else
  773.     ChildPid = fork();
  774. # endif
  775.  
  776.     if (ChildPid == -1) {
  777.         int    fork_errno = errno;
  778.  
  779.         pipeclose(p);
  780. # ifdef SIGINT_UNBLOCK
  781.         SIGINT_UNBLOCK();
  782. # else
  783.         (void) setsighandler(SIGINT, old_int),
  784. # endif
  785.         complain("[Fork failed: %s]", strerror(fork_errno));
  786.     }
  787.     if (ChildPid == 0) {
  788. # ifdef USE_VFORK
  789.         /* There are several other forks in Jove, but this is
  790.          * the only one we execute often enough to make it worth
  791.          * using a vfork.  This assumes a system with vfork also
  792.          * has BSD signals!
  793.          */
  794.         (void) setsighandler(SIGINT, SIG_DFL);
  795. #  ifdef SIGINT_UNBLOCK
  796.         SIGINT_UNBLOCK();
  797. #  endif
  798. # else /* !USE_VFORK */
  799.         (void) setsighandler(SIGINT, SIG_DFL);
  800. #  ifdef SIGINT_UNBLOCK
  801.         SIGINT_UNBLOCK();
  802. #  endif
  803. # endif /* !USE_VFORK */
  804.         (void) close(0);
  805.         (void) open(InFName==NULL? "/dev/null" : InFName, 0);
  806.         (void) close(1);
  807.         (void) dup(p[1]);
  808.         (void) close(2);
  809.         (void) dup(p[1]);
  810.         pipeclose(p);
  811.         jcloseall();
  812.         execv(argv[0], &argv[1]);
  813.         raw_complain("Execl failed: %s", strerror(errno));
  814.         _exit(1);
  815.     }
  816. # ifdef SIGINT_BLOCK
  817.     old_int = setsighandler(SIGINT, SIG_IGN);    /* got to do this eventually */
  818. # endif
  819.     (void) close(p[1]);
  820.     fp = fd_open(argv[1], F_READ, p[0], iobuff, LBSIZE);
  821. #else /* MSDOS_PROCS*/
  822.     {
  823.         int    oldi = dup(0),
  824.             oldo = dup(1),
  825.             olde = dup(2);
  826.         bool    InFailure;
  827.         int    ph;
  828.  
  829.         swritef(pnbuf, sizeof(pnbuf), "%s/%s", TmpDir, "jpXXXXXX");
  830.         pipename = mktemp(pnbuf);
  831.         if ((ph = creat(pipename, S_IWRITE|S_IREAD)) < 0)
  832.             complain("cannot make pipe for filter: %s", strerror(errno));
  833.         close(1);
  834.         close(2);
  835.         dup(ph);
  836.         dup(ph);
  837.         close(ph);
  838.  
  839.         close(0);
  840.         InFailure = InFName != NULL && open(InFName, 0) < 0;
  841.         if (!InFailure)
  842.             status = spawnv(0, argv[0], &argv[1]);
  843.  
  844.         close(0);
  845.         close(1);
  846.         close(2);
  847.         dup(oldi);
  848.         dup(oldo);
  849.         dup(olde);
  850.         close(oldi);
  851.         close(oldo);
  852.         close(olde);
  853.  
  854.         if (InFailure)
  855.             complain("[filter input failed]");
  856.         if (status < 0)
  857.             s_mess("[Spawn failed %d]", errno);
  858.         ph = open(pipename, 0);
  859.         if (ph < 0)
  860.             complain("[cannot reopen pipe]", strerror(errno));
  861.         fp = fd_open(argv[1], F_READ, ph, iobuff, LBSIZE);
  862.     }
  863.  
  864. #endif /* MSDOS_PROCS */
  865.  
  866.     do {
  867.         int    wrap_col = WrapProcessLines ? CO-1 : LBSIZE;
  868. #ifdef UNIX
  869.         InSlowRead = YES;
  870. #endif
  871.         eof = f_gets(fp, genbuf, (size_t)LBSIZE);
  872. #ifdef UNIX
  873.         InSlowRead = NO;
  874. #endif
  875.         if (bnm != NULL) {
  876.             ins_str_wrap(genbuf, YES, wrap_col);
  877.             if (!eof)
  878.                 LineInsert(1);
  879.             if ((flags & UTB_DISP) && fp->f_cnt <= 0) {
  880.                 message("Chugging along...");
  881.                 redisplay();
  882.             }
  883.         } else if (flags & UTB_DISP)
  884.             Typeout("%s", genbuf);
  885.     } while (!eof);
  886.     if (flags & UTB_DISP)
  887.         DrawMesg(NO);
  888.     close_file(fp);
  889. #ifndef MSDOS_PROCS
  890.     dowait(&status);
  891. # ifdef SIGINT_UNBLOCK
  892.     SIGINT_UNBLOCK();
  893. # endif
  894.     (void) setsighandler(SIGINT, old_int);
  895. #else /* MSDOS_PROCS */
  896.     unlink(pipename);
  897.     getCWD();
  898. # ifdef WINRESIZE
  899.     ResizePending = YES;   /* In case subproc did a MODE command or something */
  900. # endif
  901. #endif /* MSDOS_PROCS */
  902.     SlowCmd -= 1;;
  903.     return status;
  904. }
  905.  
  906. /* Send the current region to CMD and insert the output from the
  907.    command into OUT_BUF. */
  908.  
  909. private void
  910. RegToUnix(outbuf, cmd, wrap)
  911. Buffer    *outbuf;
  912. char    *cmd;
  913. bool    wrap;
  914. {
  915.     Mark    *m = CurMark();
  916.     static char    tnambuf[FILESIZE];
  917.     char    *tname;
  918.     Window    *save_wind = curwind;
  919.     volatile wait_status_t    status;
  920.     volatile bool    err = NO;
  921.     bool    old_wrap = WrapProcessLines;
  922.     File    *volatile fp;
  923.     jmp_buf    sav_jmp;
  924.  
  925.     swritef(tnambuf, sizeof(tnambuf), "%s/%s", TmpDir, "jfXXXXXX");
  926.     tname = mktemp(tnambuf);
  927.     fp = open_file(tname, iobuff, F_WRITE, YES);
  928.     push_env(sav_jmp);
  929.     if (setjmp(mainjmp) == 0) {
  930.         WrapProcessLines = wrap;
  931.         putreg(fp, m->m_line, m->m_char, curline, curchar, YES);
  932.         DelReg();
  933.         f_close(fp);
  934.         status = UnixToBuf(UTB_SH|UTB_FILEARG, outbuf->b_name, tname, cmd);
  935.     } else {
  936.         f_close(fp);
  937.         err = YES;
  938.     }
  939.     pop_env(sav_jmp);
  940.     WrapProcessLines = old_wrap;
  941.  
  942.     (void) unlink(tname);
  943.     SetWind(save_wind);
  944.     if (!err)
  945.         com_finish(status, cmd);
  946. }
  947.  
  948. void
  949. FilterRegion()
  950. {
  951.     static char FltComBuf[LBSIZE];
  952.  
  953.     null_ncpy(FltComBuf, ask(FltComBuf, ": %f (through command) "),
  954.           (sizeof FltComBuf) - 1);
  955.     RegToUnix(curbuf, FltComBuf, NO);
  956.     this_cmd = UNDOABLECMD;
  957. }
  958.  
  959. void
  960. isprocbuf(bnm)
  961. char    *bnm;
  962. {
  963.     Buffer    *bp;
  964.  
  965.     if ((bp = buf_exists(bnm)) != NULL && bp->b_type != B_PROCESS)
  966.         confirm("Over-write buffer %s? ", bnm);
  967. }
  968.  
  969. #endif /* SUBSHELL */
  970.