home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / pch.c < prev    next >
C/C++ Source or Header  |  1991-12-24  |  35KB  |  1,318 lines

  1. /* $Header: pch.c,v 2.0.1.7 88/06/03 15:13:28 lwall Locked $
  2.  *
  3.  * $Log:    pch.c,v $
  4.  * Revision 2.0.2.0  90/05/01  22:17:51  davison
  5.  * patch12u: unidiff support added
  6.  *
  7.  * Revision 2.0.1.7  88/06/03  15:13:28  lwall
  8.  * patch10: Can now find patches in shar scripts.
  9.  * patch10: Hunks that swapped and then swapped back could core dump.
  10.  *
  11.  * Revision 2.0.1.6  87/06/04  16:18:13  lwall
  12.  * pch_swap didn't swap p_bfake and p_efake.
  13.  *
  14.  * Revision 2.0.1.5  87/01/30  22:47:42  lwall
  15.  * Improved responses to mangled patches.
  16.  *
  17.  * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  18.  * New-style context diffs caused double call to free().
  19.  *
  20.  * Revision 2.0.1.3  86/11/14  10:08:33  lwall
  21.  * Fixed problem where a long pattern wouldn't grow the hunk.
  22.  * Also restored p_input_line when backtracking so error messages are right.
  23.  *
  24.  * Revision 2.0.1.2  86/11/03  17:49:52  lwall
  25.  * New-style delete triggers spurious assertion error.
  26.  *
  27.  * Revision 2.0.1.1  86/10/29  15:52:08  lwall
  28.  * Could falsely report new-style context diff.
  29.  *
  30.  * Revision 2.0  86/09/17  15:39:37  lwall
  31.  * Baseline for netwide release.
  32.  *
  33.  */
  34.  
  35. #include "EXTERN.h"
  36. #include "common.h"
  37. #include "util.h"
  38. #include "INTERN.h"
  39. #include "pch.h"
  40. #include "version.h"
  41.  
  42. #ifdef OS2
  43. #define EDPATH "ed"
  44. #else
  45. #define EDPATH "/bin/ed"
  46. #endif
  47.  
  48. /* Patch (diff listing) abstract type. */
  49.  
  50. static long p_filesize;            /* size of the patch file */
  51. static LINENUM p_first;            /* 1st line number */
  52. static LINENUM p_newfirst;        /* 1st line number of replacement */
  53. static LINENUM p_ptrn_lines;        /* # lines in pattern */
  54. static LINENUM p_repl_lines;        /* # lines in replacement text */
  55. static LINENUM p_end = -1;        /* last line in hunk */
  56. static LINENUM p_max;            /* max allowed value of p_end */
  57. static LINENUM p_context = 3;        /* # of context lines */
  58. static LINENUM p_input_line = 0;    /* current line # from patch file */
  59. static char **p_line = Null(char**);    /* the text of the hunk */
  60. static short *p_len = Null(short*);    /* length of each line */
  61. static char *p_char = Nullch;        /* +, -, and ! */
  62. static int hunkmax = INITHUNKMAX;    /* size of above arrays to begin with */
  63. static int p_indent;            /* indent to patch */
  64. static LINENUM p_base;            /* where to intuit this time */
  65. static LINENUM p_bline;            /* line # of p_base */
  66. static LINENUM p_start;            /* where intuit found a patch */
  67. static LINENUM p_sline;            /* and the line number for it */
  68. static LINENUM p_hunk_beg;        /* line number of current hunk */
  69. static LINENUM p_efake = -1;        /* end of faked up lines--don't free */
  70. static LINENUM p_bfake = -1;        /* beg of faked up lines */
  71.  
  72. /* Prepare to look for the next patch in the patch file. */
  73.  
  74. void
  75. re_patch()
  76. {
  77.     p_first = Nulline;
  78.     p_newfirst = Nulline;
  79.     p_ptrn_lines = Nulline;
  80.     p_repl_lines = Nulline;
  81.     p_end = (LINENUM)-1;
  82.     p_max = Nulline;
  83.     p_indent = 0;
  84. }
  85.  
  86. /* Open the patch file at the beginning of time. */
  87.  
  88. void
  89. open_patch_file(filename)
  90. char *filename;
  91. {
  92.     if (filename == Nullch || !*filename || strEQ(filename, "-")) {
  93.         if ( isatty(fileno(stdin)) )
  94.         {
  95.           Usage();
  96.           exit(1);
  97.         }
  98.  
  99.     pfp = fopen(TMPPATNAME, "w");
  100.     if (pfp == Nullfp)
  101.         fatal2("patch: can't create %s.\n", TMPPATNAME);
  102.     while (fgets(buf, sizeof buf, stdin) != Nullch)
  103.         fputs(buf, pfp);
  104.     Fclose(pfp);
  105.     filename = TMPPATNAME;
  106.     }
  107.     pfp = fopen(filename, "r");
  108.     if (pfp == Nullfp)
  109.     fatal2("patch file %s not found\n", filename);
  110.     Fstat(fileno(pfp), &filestat);
  111.     p_filesize = filestat.st_size;
  112.     next_intuit_at(0L,1L);            /* start at the beginning */
  113.     set_hunkmax();
  114. }
  115.  
  116. /* Make sure our dynamically realloced tables are malloced to begin with. */
  117.  
  118. void
  119. set_hunkmax()
  120. {
  121. #ifndef lint
  122.     if (p_line == Null(char**))
  123.     p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
  124.     if (p_len == Null(short*))
  125.     p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
  126. #endif
  127.     if (p_char == Nullch)
  128.     p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
  129. }
  130.  
  131. /* Enlarge the arrays containing the current hunk of patch. */
  132.  
  133. void
  134. grow_hunkmax()
  135. {
  136.     hunkmax *= 2;
  137.     /*
  138.      * Note that on most systems, only the p_line array ever gets fresh memory
  139.      * since p_len can move into p_line's old space, and p_char can move into
  140.      * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
  141.      */
  142.     assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
  143. #ifndef lint
  144.     p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
  145.     p_len  = (short*) realloc((char*)p_len,  (MEM)hunkmax * sizeof(short));
  146.     p_char = (char*)  realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
  147. #endif
  148.     if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
  149.     return;
  150.     if (!using_plan_a)
  151.     fatal1("patch: out of memory (grow_hunkmax)\n");
  152.     out_of_mem = TRUE;        /* whatever is null will be allocated again */
  153.                 /* from within plan_a(), of all places */
  154. }
  155.  
  156. /* True if the remainder of the patch file contains a diff of some sort. */
  157.  
  158. bool
  159. there_is_another_patch()
  160. {
  161.     if (p_base != 0L && p_base >= p_filesize) {
  162.     if (verbose)
  163.         say1("done\n");
  164.     return FALSE;
  165.     }
  166.     if (verbose)
  167.     say1("Hmm...");
  168.     diff_type = intuit_diff_type();
  169.     if (!diff_type) {
  170.     if (p_base != 0L) {
  171.         if (verbose)
  172.         say1("  Ignoring the trailing garbage.\ndone\n");
  173.     }
  174.     else
  175.         say1("  I can't seem to find a patch in there anywhere.\n");
  176.     return FALSE;
  177.     }
  178.     if (verbose)
  179.     say3("  %sooks like %s to me...\n",
  180.         (p_base == 0L ? "L" : "The next patch l"),
  181.         diff_type == UNI_DIFF ? "a unified diff" :
  182.         diff_type == CONTEXT_DIFF ? "a context diff" :
  183.         diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
  184.         diff_type == NORMAL_DIFF ? "a normal diff" :
  185.         "an ed script" );
  186.     if (p_indent && verbose)
  187.     say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  188.     skip_to(p_start,p_sline);
  189.     while (filearg[0] == Nullch) {
  190.     if (force) {
  191.         say1("No file to patch.  Skipping...\n");
  192.         filearg[0] = savestr(bestguess);
  193.         return TRUE;
  194.     }
  195.     ask1("File to patch: ");
  196.     if (*buf != '\n') {
  197.         if (bestguess)
  198.         free(bestguess);
  199.         bestguess = savestr(buf);
  200.         filearg[0] = fetchname(buf, 0, FALSE);
  201.     }
  202.     if (filearg[0] == Nullch) {
  203.         ask1("No file found--skip this patch? [n] ");
  204.         if (*buf != 'y') {
  205.         continue;
  206.         }
  207.         if (verbose)
  208.         say1("Skipping patch...\n");
  209.         filearg[0] = fetchname(bestguess, 0, TRUE);
  210.         skip_rest_of_patch = TRUE;
  211.         return TRUE;
  212.     }
  213.     }
  214.     return TRUE;
  215. }
  216.  
  217. /* Determine what kind of diff is in the remaining part of the patch file. */
  218.  
  219. int
  220. intuit_diff_type()
  221. {
  222.     Reg4 long this_line = 0;
  223.     Reg5 long previous_line;
  224.     Reg6 long first_command_line = -1;
  225.     long fcl_line;
  226.     Reg7 bool last_line_was_command = FALSE;
  227.     Reg8 bool this_is_a_command = FALSE;
  228.     Reg9 bool stars_last_line = FALSE;
  229.     Reg10 bool stars_this_line = FALSE;
  230.     Reg3 int indent;
  231.     Reg1 char *s;
  232.     Reg2 char *t;
  233.     char *indtmp = Nullch;
  234.     char *oldtmp = Nullch;
  235.     char *newtmp = Nullch;
  236.     char *indname = Nullch;
  237.     char *oldname = Nullch;
  238.     char *newname = Nullch;
  239.     Reg11 int retval;
  240.     bool no_filearg = (filearg[0] == Nullch);
  241.  
  242.     ok_to_create_file = FALSE;
  243.     Fseek(pfp, p_base, 0);
  244.     p_input_line = p_bline - 1;
  245.     for (;;) {
  246.     previous_line = this_line;
  247.     last_line_was_command = this_is_a_command;
  248.     stars_last_line = stars_this_line;
  249.     this_line = ftell(pfp);
  250.     indent = 0;
  251.     p_input_line++;
  252.     if (fgets(buf, sizeof buf, pfp) == Nullch) {
  253.         if (first_command_line >= 0L) {
  254.                     /* nothing but deletes!? */
  255.         p_start = first_command_line;
  256.         p_sline = fcl_line;
  257.         retval = ED_DIFF;
  258.         goto scan_exit;
  259.         }
  260.         else {
  261.         p_start = this_line;
  262.         p_sline = p_input_line;
  263.         retval = 0;
  264.         goto scan_exit;
  265.         }
  266.     }
  267.     for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
  268.         if (*s == '\t')
  269.         indent += 8 - (indent % 8);
  270.         else
  271.         indent++;
  272.     }
  273.     for (t=s; isdigit(*t) || *t == ','; t++) ;
  274.     this_is_a_command = (isdigit(*s) &&
  275.       (*t == 'd' || *t == 'c' || *t == 'a') );
  276.     if (first_command_line < 0L && this_is_a_command) {
  277.         first_command_line = this_line;
  278.         fcl_line = p_input_line;
  279.         p_indent = indent;        /* assume this for now */
  280.     }
  281.     if (!stars_last_line && strnEQ(s, "*** ", 4))
  282.         oldtmp = savestr(s+4);
  283.     else if (strnEQ(s, "--- ", 4))
  284.         newtmp = savestr(s+4);
  285.     else if (strnEQ(s, "+++ ", 4))
  286.         oldtmp = savestr(s+4);    /* pretend it is the old name */
  287.     else if (strnEQ(s, "Index:", 6))
  288.         indtmp = savestr(s+6);
  289.     else if (strnEQ(s, "Prereq:", 7)) {
  290.         for (t=s+7; isspace(*t); t++) ;
  291.         revision = savestr(t);
  292.         for (t=revision; *t && !isspace(*t); t++) ;
  293.         *t = '\0';
  294.         if (!*revision) {
  295.         free(revision);
  296.         revision = Nullch;
  297.         }
  298.     }
  299.     if ((!diff_type || diff_type == ED_DIFF) &&
  300.       first_command_line >= 0L &&
  301.       strEQ(s, ".\n") ) {
  302.         p_indent = indent;
  303.         p_start = first_command_line;
  304.         p_sline = fcl_line;
  305.         retval = ED_DIFF;
  306.         goto scan_exit;
  307.     }
  308.     if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
  309.         if (!atol(s+3))
  310.         ok_to_create_file = TRUE;
  311.         p_indent = indent;
  312.         p_start = this_line;
  313.         p_sline = p_input_line;
  314.         retval = UNI_DIFF;
  315.         goto scan_exit;
  316.     }
  317.     stars_this_line = strnEQ(s, "********", 8);
  318.     if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
  319.          strnEQ(s, "*** ", 4)) {
  320.         if (!atol(s+4))
  321.         ok_to_create_file = TRUE;
  322.         /* if this is a new context diff the character just before */
  323.         /* the newline is a '*'. */
  324.         while (*s != '\n')
  325.         s++;
  326.         p_indent = indent;
  327.         p_start = previous_line;
  328.         p_sline = p_input_line - 1;
  329.         retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
  330.         goto scan_exit;
  331.     }
  332.     if ((!diff_type || diff_type == NORMAL_DIFF) &&
  333.       last_line_was_command &&
  334.       (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
  335.         p_start = previous_line;
  336.         p_sline = p_input_line - 1;
  337.         p_indent = indent;
  338.         retval = NORMAL_DIFF;
  339.         goto scan_exit;
  340.     }
  341.     }
  342.   scan_exit:
  343.     if (no_filearg) {
  344.     if (indtmp != Nullch)
  345.         indname = fetchname(indtmp, strippath, ok_to_create_file);
  346.     if (oldtmp != Nullch)
  347.         oldname = fetchname(oldtmp, strippath, ok_to_create_file);
  348.     if (newtmp != Nullch)
  349.         newname = fetchname(newtmp, strippath, ok_to_create_file);
  350.     if (oldname && newname) {
  351.         if (strlen(oldname) < strlen(newname))
  352.         filearg[0] = savestr(oldname);
  353.         else
  354.         filearg[0] = savestr(newname);
  355.     }
  356.     else if (oldname)
  357.         filearg[0] = savestr(oldname);
  358.     else if (newname)
  359.         filearg[0] = savestr(newname);
  360.     else if (indname)
  361.         filearg[0] = savestr(indname);
  362.     }
  363.     if (bestguess) {
  364.     free(bestguess);
  365.     bestguess = Nullch;
  366.     }
  367.     if (filearg[0] != Nullch)
  368.     bestguess = savestr(filearg[0]);
  369.     else if (indtmp != Nullch)
  370.     bestguess = fetchname(indtmp, strippath, TRUE);
  371.     else {
  372.     if (oldtmp != Nullch)
  373.         oldname = fetchname(oldtmp, strippath, TRUE);
  374.     if (newtmp != Nullch)
  375.         newname = fetchname(newtmp, strippath, TRUE);
  376.     if (oldname && newname) {
  377.         if (strlen(oldname) < strlen(newname))
  378.         bestguess = savestr(oldname);
  379.         else
  380.         bestguess = savestr(newname);
  381.     }
  382.     else if (oldname)
  383.         bestguess = savestr(oldname);
  384.     else if (newname)
  385.         bestguess = savestr(newname);
  386.     }
  387.     if (indtmp != Nullch)
  388.     free(indtmp);
  389.     if (oldtmp != Nullch)
  390.     free(oldtmp);
  391.     if (newtmp != Nullch)
  392.     free(newtmp);
  393.     if (indname != Nullch)
  394.     free(indname);
  395.     if (oldname != Nullch)
  396.     free(oldname);
  397.     if (newname != Nullch)
  398.     free(newname);
  399.     return retval;
  400. }
  401.  
  402. /* Remember where this patch ends so we know where to start up again. */
  403.  
  404. void
  405. next_intuit_at(file_pos,file_line)
  406. long file_pos;
  407. long file_line;
  408. {
  409.     p_base = file_pos;
  410.     p_bline = file_line;
  411. }
  412.  
  413. /* Basically a verbose fseek() to the actual diff listing. */
  414.  
  415. void
  416. skip_to(file_pos,file_line)
  417. long file_pos;
  418. long file_line;
  419. {
  420.     char *ret;
  421.  
  422.     assert(p_base <= file_pos);
  423.     if (verbose && p_base < file_pos) {
  424.     Fseek(pfp, p_base, 0);
  425.     say1("The text leading up to this was:\n--------------------------\n");
  426.     while (ftell(pfp) < file_pos) {
  427.         ret = fgets(buf, sizeof buf, pfp);
  428.         assert(ret != Nullch);
  429.         say2("|%s", buf);
  430.     }
  431.     say1("--------------------------\n");
  432.     }
  433.     else
  434.     Fseek(pfp, file_pos, 0);
  435.     p_input_line = file_line - 1;
  436. }
  437.  
  438. /* Make this a function for better debugging.  */
  439. static void
  440. malformed ()
  441. {
  442.     fatal3("Malformed patch at line %ld: %s", p_input_line, buf);
  443.         /* about as informative as "Syntax error" in C */
  444. }
  445.  
  446. /* True if there is more of the current diff listing to process. */
  447.  
  448. bool
  449. another_hunk()
  450. {
  451.     Reg1 char *s;
  452.     Reg8 char *ret;
  453.     Reg2 int context = 0;
  454.  
  455.     while (p_end >= 0) {
  456.     if (p_end == p_efake)
  457.         p_end = p_bfake;        /* don't free twice */
  458.     else
  459.         free(p_line[p_end]);
  460.     p_end--;
  461.     }
  462.     assert(p_end == -1);
  463.     p_efake = -1;
  464.  
  465.     p_max = hunkmax;            /* gets reduced when --- found */
  466.     if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
  467.     long line_beginning = ftell(pfp);
  468.                     /* file pos of the current line */
  469.     LINENUM repl_beginning = 0;    /* index of --- line */
  470.     Reg4 LINENUM fillcnt = 0;    /* #lines of missing ptrn or repl */
  471.     Reg5 LINENUM fillsrc;        /* index of first line to copy */
  472.     Reg6 LINENUM filldst;        /* index of first missing line */
  473.     bool ptrn_spaces_eaten = FALSE;    /* ptrn was slightly misformed */
  474.     Reg9 bool repl_could_be_missing = TRUE;
  475.                     /* no + or ! lines in this hunk */
  476.     bool repl_missing = FALSE;    /* we are now backtracking */
  477.     long repl_backtrack_position = 0;
  478.                     /* file pos of first repl line */
  479.     LINENUM repl_patch_line;    /* input line number for same */
  480.     Reg7 LINENUM ptrn_copiable = 0;
  481.                     /* # of copiable lines in ptrn */
  482.  
  483.     ret = pgets(buf, sizeof buf, pfp);
  484.     p_input_line++;
  485.     if (ret == Nullch || strnNE(buf, "********", 8)) {
  486.         next_intuit_at(line_beginning,p_input_line);
  487.         return FALSE;
  488.     }
  489.     p_context = 100;
  490.     p_hunk_beg = p_input_line + 1;
  491.     while (p_end < p_max) {
  492.         line_beginning = ftell(pfp);
  493.         ret = pgets(buf, sizeof buf, pfp);
  494.         p_input_line++;
  495.         if (ret == Nullch) {
  496.         if (p_max - p_end < 4)
  497.             Strcpy(buf, "  \n");  /* assume blank lines got chopped */
  498.         else {
  499.             if (repl_beginning && repl_could_be_missing) {
  500.             repl_missing = TRUE;
  501.             goto hunk_done;
  502.             }
  503.             fatal1("Unexpected end of file in patch.\n");
  504.         }
  505.         }
  506.         p_end++;
  507.         assert(p_end < hunkmax);
  508.         p_char[p_end] = *buf;
  509. #ifdef zilog
  510.         p_line[(short)p_end] = Nullch;
  511. #else
  512.         p_line[p_end] = Nullch;
  513. #endif
  514.         switch (*buf) {
  515.         case '*':
  516.         if (strnEQ(buf, "********", 8)) {
  517.             if (repl_beginning && repl_could_be_missing) {
  518.             repl_missing = TRUE;
  519.             goto hunk_done;
  520.             }
  521.             else
  522.             fatal2("Unexpected end of hunk at line %ld.\n",
  523.                 p_input_line);
  524.         }
  525.         if (p_end != 0) {
  526.             if (repl_beginning && repl_could_be_missing) {
  527.             repl_missing = TRUE;
  528.             goto hunk_done;
  529.             }
  530.             fatal3("Unexpected *** at line %ld: %s", p_input_line, buf);
  531.         }
  532.         context = 0;
  533.         p_line[p_end] = savestr(buf);
  534.         if (out_of_mem) {
  535.             p_end--;
  536.             return FALSE;
  537.         }
  538.         for (s=buf; *s && !isdigit(*s); s++) ;
  539.         if (!*s)
  540.             malformed ();
  541.         if (strnEQ(s,"0,0",3))
  542.             strcpy(s,s+2);
  543.         p_first = (LINENUM) atol(s);
  544.         while (isdigit(*s)) s++;
  545.         if (*s == ',') {
  546.             for (; *s && !isdigit(*s); s++) ;
  547.             if (!*s)
  548.             malformed ();
  549.             p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
  550.         }
  551.         else if (p_first)
  552.             p_ptrn_lines = 1;
  553.         else {
  554.             p_ptrn_lines = 0;
  555.             p_first = 1;
  556.         }
  557.         p_max = p_ptrn_lines + 6;    /* we need this much at least */
  558.         while (p_max >= hunkmax)
  559.             grow_hunkmax();
  560.         p_max = hunkmax;
  561.         break;
  562.         case '-':
  563.         if (buf[1] == '-') {
  564.             if (repl_beginning ||
  565.             (p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
  566.             {
  567.             if (p_end == 1) {
  568.                 /* `old' lines were omitted - set up to fill */
  569.                 /* them in from 'new' context lines. */
  570.                 p_end = p_ptrn_lines + 1;
  571.                 fillsrc = p_end + 1;
  572.                 filldst = 1;
  573.                 fillcnt = p_ptrn_lines;
  574.             }
  575.             else {
  576.                 if (repl_beginning) {
  577.                 if (repl_could_be_missing){
  578.                     repl_missing = TRUE;
  579.                     goto hunk_done;
  580.                 }
  581.                 fatal3(
  582. "Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
  583.                     p_input_line, p_hunk_beg + repl_beginning);
  584.                 }
  585.                 else {
  586.                 fatal4(
  587. "%s \"---\" at line %ld--check line numbers at line %ld.\n",
  588.                     (p_end <= p_ptrn_lines
  589.                     ? "Premature"
  590.                     : "Overdue" ),
  591.                     p_input_line, p_hunk_beg);
  592.                 }
  593.             }
  594.             }
  595.             repl_beginning = p_end;
  596.             repl_backtrack_position = ftell(pfp);
  597.             repl_patch_line = p_input_line;
  598.             p_line[p_end] = savestr(buf);
  599.             if (out_of_mem) {
  600.             p_end--;
  601.             return FALSE;
  602.             }
  603.             p_char[p_end] = '=';
  604.             for (s=buf; *s && !isdigit(*s); s++) ;
  605.             if (!*s)
  606.             malformed ();
  607.             p_newfirst = (LINENUM) atol(s);
  608.             while (isdigit(*s)) s++;
  609.             if (*s == ',') {
  610.             for (; *s && !isdigit(*s); s++) ;
  611.             if (!*s)
  612.                 malformed ();
  613.             p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
  614.             }
  615.             else if (p_newfirst)
  616.             p_repl_lines = 1;
  617.             else {
  618.             p_repl_lines = 0;
  619.             p_newfirst = 1;
  620.             }
  621.             p_max = p_repl_lines + p_end;
  622.             if (p_max > MAXHUNKSIZE)
  623.             fatal4("Hunk too large (%ld lines) at line %ld: %s",
  624.                   p_max, p_input_line, buf);
  625.             while (p_max >= hunkmax)
  626.             grow_hunkmax();
  627.             if (p_repl_lines != ptrn_copiable
  628.              && (p_context != 0 || p_repl_lines != 1))
  629.             repl_could_be_missing = FALSE;
  630.             break;
  631.         }
  632.         goto change_line;
  633.         case '+':  case '!':
  634.         repl_could_be_missing = FALSE;
  635.           change_line:
  636.         if (buf[1] == '\n' && canonicalize)
  637.             strcpy(buf+1," \n");
  638.         if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
  639.           repl_beginning && repl_could_be_missing) {
  640.             repl_missing = TRUE;
  641.             goto hunk_done;
  642.         }
  643.         if (context >= 0) {
  644.             if (context < p_context)
  645.             p_context = context;
  646.             context = -1000;
  647.         }
  648.         p_line[p_end] = savestr(buf+2);
  649.         if (out_of_mem) {
  650.             p_end--;
  651.             return FALSE;
  652.         }
  653.         break;
  654.         case '\t': case '\n':    /* assume the 2 spaces got eaten */
  655.         if (repl_beginning && repl_could_be_missing &&
  656.           (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
  657.             repl_missing = TRUE;
  658.             goto hunk_done;
  659.         }
  660.         p_line[p_end] = savestr(buf);
  661.         if (out_of_mem) {
  662.             p_end--;
  663.             return FALSE;
  664.         }
  665.         if (p_end != p_ptrn_lines + 1) {
  666.             ptrn_spaces_eaten |= (repl_beginning != 0);
  667.             context++;
  668.             if (!repl_beginning)
  669.             ptrn_copiable++;
  670.             p_char[p_end] = ' ';
  671.         }
  672.         break;
  673.         case ' ':
  674.         if (!isspace(buf[1]) &&
  675.           repl_beginning && repl_could_be_missing) {
  676.             repl_missing = TRUE;
  677.             goto hunk_done;
  678.         }
  679.         context++;
  680.         if (!repl_beginning)
  681.             ptrn_copiable++;
  682.         p_line[p_end] = savestr(buf+2);
  683.         if (out_of_mem) {
  684.             p_end--;
  685.             return FALSE;
  686.         }
  687.         break;
  688.         default:
  689.         if (repl_beginning && repl_could_be_missing) {
  690.             repl_missing = TRUE;
  691.             goto hunk_done;
  692.         }
  693.         malformed ();
  694.         }
  695.         /* set up p_len for strncmp() so we don't have to */
  696.         /* assume null termination */
  697.         if (p_line[p_end])
  698.         p_len[p_end] = strlen(p_line[p_end]);
  699.         else
  700.         p_len[p_end] = 0;
  701.     }
  702.  
  703.     hunk_done:
  704.     if (p_end >=0 && !repl_beginning)
  705.         fatal2("No --- found in patch at line %ld\n", pch_hunk_beg());
  706.  
  707.     if (repl_missing) {
  708.  
  709.         /* reset state back to just after --- */
  710.         p_input_line = repl_patch_line;
  711.         for (p_end--; p_end > repl_beginning; p_end--)
  712.         free(p_line[p_end]);
  713.         Fseek(pfp, repl_backtrack_position, 0);
  714.  
  715.         /* redundant 'new' context lines were omitted - set */
  716.         /* up to fill them in from the old file context */
  717.         if (!p_context && p_repl_lines == 1) {
  718.         p_repl_lines = 0;
  719.         p_max--;
  720.         }
  721.         fillsrc = 1;
  722.         filldst = repl_beginning+1;
  723.         fillcnt = p_repl_lines;
  724.         p_end = p_max;
  725.     }
  726.     else if (!p_context && fillcnt == 1) {
  727.         /* the first hunk was a null hunk with no context */
  728.         /* and we were expecting one line -- fix it up. */
  729.         while (filldst < p_end) {
  730.         p_line[filldst] = p_line[filldst+1];
  731.         p_char[filldst] = p_char[filldst+1];
  732.         p_len[filldst] = p_len[filldst+1];
  733.         filldst++;
  734.         }
  735. /*        repl_beginning--;        /* this doesn't need to be fixed */
  736.         p_end--;
  737.         p_first++;            /* do append rather than insert */
  738.         fillcnt = 0;
  739.         p_ptrn_lines = 0;
  740.     }
  741.  
  742.     if (diff_type == CONTEXT_DIFF &&
  743.       (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
  744.         if (verbose)
  745.         say4("%s\n%s\n%s\n",
  746. "(Fascinating--this is really a new-style context diff but without",
  747. "the telltale extra asterisks on the *** line that usually indicate",
  748. "the new style...)");
  749.         diff_type = NEW_CONTEXT_DIFF;
  750.     }
  751.  
  752.     /* if there were omitted context lines, fill them in now */
  753.     if (fillcnt) {
  754.         p_bfake = filldst;        /* remember where not to free() */
  755.         p_efake = filldst + fillcnt - 1;
  756.         while (fillcnt-- > 0) {
  757.         while (fillsrc <= p_end && p_char[fillsrc] != ' ')
  758.             fillsrc++;
  759.         if (fillsrc > p_end)
  760.             fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
  761.             p_hunk_beg);
  762.         p_line[filldst] = p_line[fillsrc];
  763.         p_char[filldst] = p_char[fillsrc];
  764.         p_len[filldst] = p_len[fillsrc];
  765.         fillsrc++; filldst++;
  766.         }
  767.         while (fillsrc <= p_end && fillsrc != repl_beginning &&
  768.           p_char[fillsrc] != ' ')
  769.         fillsrc++;
  770. #ifdef DEBUGGING
  771.         if (debug & 64)
  772.         printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
  773.             fillsrc,filldst,repl_beginning,p_end+1);
  774. #endif
  775.         assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
  776.         assert(filldst==p_end+1 || filldst==repl_beginning);
  777.     }
  778.     }
  779.     else if (diff_type == UNI_DIFF) {
  780.     long line_beginning = ftell(pfp);
  781.                     /* file pos of the current line */
  782.     Reg4 LINENUM fillsrc;        /* index of old lines */
  783.     Reg5 LINENUM filldst;        /* index of new lines */
  784.     char ch;
  785.  
  786.     ret = pgets(buf, sizeof buf, pfp);
  787.     p_input_line++;
  788.     if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
  789.         next_intuit_at(line_beginning,p_input_line);
  790.         return FALSE;
  791.     }
  792.     s = buf+4;
  793.     if (!*s)
  794.         malformed ();
  795.     p_first = (LINENUM) atol(s);
  796.     while (isdigit(*s)) s++;
  797.     if (*s == ',') {
  798.         p_ptrn_lines = (LINENUM) atol(++s);
  799.         while (isdigit(*s)) s++;
  800.     } else
  801.         p_ptrn_lines = 1;
  802.     if (*s == ' ') s++;
  803.     if (*s != '+' || !*++s)
  804.         malformed ();
  805.     p_newfirst = (LINENUM) atol(s);
  806.     while (isdigit(*s)) s++;
  807.     if (*s == ',') {
  808.         p_repl_lines = (LINENUM) atol(++s);
  809.         while (isdigit(*s)) s++;
  810.     } else
  811.         p_repl_lines = 1;
  812.     if (*s == ' ') s++;
  813.     if (*s != '@')
  814.         malformed ();
  815.     if (!p_ptrn_lines)
  816.         p_first++;            /* do append rather than insert */
  817.     p_max = p_ptrn_lines + p_repl_lines + 1;
  818.     while (p_max >= hunkmax)
  819.         grow_hunkmax();
  820.     fillsrc = 1;
  821.     filldst = fillsrc + p_ptrn_lines;
  822.     p_end = filldst + p_repl_lines;
  823.     Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
  824.     p_line[0] = savestr(buf);
  825.     if (out_of_mem) {
  826.         p_end = -1;
  827.         return FALSE;
  828.     }
  829.     p_char[0] = '*';
  830.         Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
  831.     p_line[filldst] = savestr(buf);
  832.     if (out_of_mem) {
  833.         p_end = 0;
  834.         return FALSE;
  835.     }
  836.     p_char[filldst++] = '=';
  837.     p_context = 100;
  838.     context = 0;
  839.     p_hunk_beg = p_input_line + 1;
  840.     while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
  841.         line_beginning = ftell(pfp);
  842.         ret = pgets(buf, sizeof buf, pfp);
  843.         p_input_line++;
  844.         if (ret == Nullch) {
  845.         if (p_max - filldst < 3)
  846.             Strcpy(buf, " \n");  /* assume blank lines got chopped */
  847.         else {
  848.             fatal1("Unexpected end of file in patch.\n");
  849.         }
  850.         }
  851.         if (*buf == '\t' || *buf == '\n') {
  852.         ch = ' ';        /* assume the space got eaten */
  853.         s = savestr(buf);
  854.         }
  855.         else {
  856.         ch = *buf;
  857.         s = savestr(buf+1);
  858.         }
  859.         if (out_of_mem) {
  860.         while (--filldst > p_ptrn_lines)
  861.             free(p_line[filldst]);
  862.         p_end = fillsrc-1;
  863.         return FALSE;
  864.         }
  865.         switch (ch) {
  866.         case '-':
  867.         if (fillsrc > p_ptrn_lines) {
  868.             free(s);
  869.             p_end = filldst-1;
  870.             malformed ();
  871.         }
  872.         p_char[fillsrc] = ch;
  873.         p_line[fillsrc] = s;
  874.         p_len[fillsrc++] = strlen(s);
  875.         break;
  876.         case '=':
  877.         ch = ' ';
  878.         /* FALL THROUGH */
  879.         case ' ':
  880.         if (fillsrc > p_ptrn_lines) {
  881.             free(s);
  882.             while (--filldst > p_ptrn_lines)
  883.             free(p_line[filldst]);
  884.             p_end = fillsrc-1;
  885.             malformed ();
  886.         }
  887.         context++;
  888.         p_char[fillsrc] = ch;
  889.         p_line[fillsrc] = s;
  890.         p_len[fillsrc++] = strlen(s);
  891.         s = savestr(s);
  892.         if (out_of_mem) {
  893.             while (--filldst > p_ptrn_lines)
  894.             free(p_line[filldst]);
  895.             p_end = fillsrc-1;
  896.             return FALSE;
  897.         }
  898.         /* FALL THROUGH */
  899.         case '+':
  900.         if (filldst > p_end) {
  901.             free(s);
  902.             while (--filldst > p_ptrn_lines)
  903.             free(p_line[filldst]);
  904.             p_end = fillsrc-1;
  905.             malformed ();
  906.         }
  907.         p_char[filldst] = ch;
  908.         p_line[filldst] = s;
  909.         p_len[filldst++] = strlen(s);
  910.         break;
  911.         default:
  912.         p_end = filldst;
  913.         malformed ();
  914.         }
  915.         if (ch != ' ' && context > 0) {
  916.         if (context < p_context)
  917.             p_context = context;
  918.         context = -1000;
  919.         }
  920.     }/* while */
  921.     }
  922.     else {                /* normal diff--fake it up */
  923.     char hunk_type;
  924.     Reg3 int i;
  925.     LINENUM min, max;
  926.     long line_beginning = ftell(pfp);
  927.  
  928.     p_context = 0;
  929.     ret = pgets(buf, sizeof buf, pfp);
  930.     p_input_line++;
  931.     if (ret == Nullch || !isdigit(*buf)) {
  932.         next_intuit_at(line_beginning,p_input_line);
  933.         return FALSE;
  934.     }
  935.     p_first = (LINENUM)atol(buf);
  936.     for (s=buf; isdigit(*s); s++) ;
  937.     if (*s == ',') {
  938.         p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
  939.         while (isdigit(*s)) s++;
  940.     }
  941.     else
  942.         p_ptrn_lines = (*s != 'a');
  943.     hunk_type = *s;
  944.     if (hunk_type == 'a')
  945.         p_first++;            /* do append rather than insert */
  946.     min = (LINENUM)atol(++s);
  947.     for (; isdigit(*s); s++) ;
  948.     if (*s == ',')
  949.         max = (LINENUM)atol(++s);
  950.     else
  951.         max = min;
  952.     if (hunk_type == 'd')
  953.         min++;
  954.     p_end = p_ptrn_lines + 1 + max - min + 1;
  955.     if (p_end > MAXHUNKSIZE)
  956.         fatal4("Hunk too large (%ld lines) at line %ld: %s",
  957.           p_end, p_input_line, buf);
  958.     while (p_end >= hunkmax)
  959.         grow_hunkmax();
  960.     p_newfirst = min;
  961.     p_repl_lines = max - min + 1;
  962.     Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
  963.     p_line[0] = savestr(buf);
  964.     if (out_of_mem) {
  965.         p_end = -1;
  966.         return FALSE;
  967.     }
  968.     p_char[0] = '*';
  969.     for (i=1; i<=p_ptrn_lines; i++) {
  970.         ret = pgets(buf, sizeof buf, pfp);
  971.         p_input_line++;
  972.         if (ret == Nullch)
  973.         fatal2("Unexpected end of file in patch at line %ld.\n",
  974.           p_input_line);
  975.         if (*buf != '<')
  976.         fatal2("< expected at line %ld of patch.\n", p_input_line);
  977.         p_line[i] = savestr(buf+2);
  978.         if (out_of_mem) {
  979.         p_end = i-1;
  980.         return FALSE;
  981.         }
  982.         p_len[i] = strlen(p_line[i]);
  983.         p_char[i] = '-';
  984.     }
  985.     if (hunk_type == 'c') {
  986.         ret = pgets(buf, sizeof buf, pfp);
  987.         p_input_line++;
  988.         if (ret == Nullch)
  989.         fatal2("Unexpected end of file in patch at line %ld.\n",
  990.             p_input_line);
  991.         if (*buf != '-')
  992.         fatal2("--- expected at line %ld of patch.\n", p_input_line);
  993.     }
  994.     Sprintf(buf, "--- %ld,%ld\n", (long) min, (long) max);
  995.     p_line[i] = savestr(buf);
  996.     if (out_of_mem) {
  997.         p_end = i-1;
  998.         return FALSE;
  999.     }
  1000.     p_char[i] = '=';
  1001.     for (i++; i<=p_end; i++) {
  1002.         ret = pgets(buf, sizeof buf, pfp);
  1003.         p_input_line++;
  1004.         if (ret == Nullch)
  1005.         fatal2("Unexpected end of file in patch at line %ld.\n",
  1006.             p_input_line);
  1007.         if (*buf != '>')
  1008.         fatal2("> expected at line %ld of patch.\n", p_input_line);
  1009.         p_line[i] = savestr(buf+2);
  1010.         if (out_of_mem) {
  1011.         p_end = i-1;
  1012.         return FALSE;
  1013.         }
  1014.         p_len[i] = strlen(p_line[i]);
  1015.         p_char[i] = '+';
  1016.     }
  1017.     }
  1018.     if (reverse)            /* backwards patch? */
  1019.     if (!pch_swap())
  1020.         say1("Not enough memory to swap next hunk!\n");
  1021. #ifdef DEBUGGING
  1022.     if (debug & 2) {
  1023.     int i;
  1024.     char special;
  1025.  
  1026.     for (i=0; i <= p_end; i++) {
  1027.         if (i == p_ptrn_lines)
  1028.         special = '^';
  1029.         else
  1030.         special = ' ';
  1031.         fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
  1032.         Fflush(stderr);
  1033.     }
  1034.     }
  1035. #endif
  1036.     if (p_end+1 < hunkmax)    /* paranoia reigns supreme... */
  1037.     p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */
  1038.     return TRUE;
  1039. }
  1040.  
  1041. /* Input a line from the patch file, worrying about indentation. */
  1042.  
  1043. char *
  1044. pgets(bf,sz,fp)
  1045. char *bf;
  1046. int sz;
  1047. FILE *fp;
  1048. {
  1049.     char *ret = fgets(bf, sz, fp);
  1050.     Reg1 char *s;
  1051.     Reg2 int indent = 0;
  1052.  
  1053.     if (p_indent && ret != Nullch) {
  1054.     for (s=buf;
  1055.       indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
  1056.         if (*s == '\t')
  1057.         indent += 8 - (indent % 7);
  1058.         else
  1059.         indent++;
  1060.     }
  1061.     if (buf != s)
  1062.         Strcpy(buf, s);
  1063.     }
  1064.     return ret;
  1065. }
  1066.  
  1067. /* Reverse the old and new portions of the current hunk. */
  1068.  
  1069. bool
  1070. pch_swap()
  1071. {
  1072.     char **tp_line;        /* the text of the hunk */
  1073.     short *tp_len;        /* length of each line */
  1074.     char *tp_char;        /* +, -, and ! */
  1075.     Reg1 LINENUM i;
  1076.     Reg2 LINENUM n;
  1077.     bool blankline = FALSE;
  1078.     Reg3 char *s;
  1079.  
  1080.     i = p_first;
  1081.     p_first = p_newfirst;
  1082.     p_newfirst = i;
  1083.  
  1084.     /* make a scratch copy */
  1085.  
  1086.     tp_line = p_line;
  1087.     tp_len = p_len;
  1088.     tp_char = p_char;
  1089.     p_line = Null(char**);    /* force set_hunkmax to allocate again */
  1090.     p_len = Null(short*);
  1091.     p_char = Nullch;
  1092.     set_hunkmax();
  1093.     if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
  1094. #ifndef lint
  1095.     if (p_line == Null(char**))
  1096.         free((char*)p_line);
  1097.     p_line = tp_line;
  1098.     if (p_len == Null(short*))
  1099.         free((char*)p_len);
  1100.     p_len = tp_len;
  1101. #endif
  1102.     if (p_char == Nullch)
  1103.         free((char*)p_char);
  1104.     p_char = tp_char;
  1105.     return FALSE;        /* not enough memory to swap hunk! */
  1106.     }
  1107.  
  1108.     /* now turn the new into the old */
  1109.  
  1110.     i = p_ptrn_lines + 1;
  1111.     if (tp_char[i] == '\n') {        /* account for possible blank line */
  1112.     blankline = TRUE;
  1113.     i++;
  1114.     }
  1115.     if (p_efake >= 0) {            /* fix non-freeable ptr range */
  1116.     if (p_efake <= i)
  1117.         n = p_end - i + 1;
  1118.     else
  1119.         n = -i;
  1120.     p_efake += n;
  1121.     p_bfake += n;
  1122.     }
  1123.     for (n=0; i <= p_end; i++,n++) {
  1124.     p_line[n] = tp_line[i];
  1125.     p_char[n] = tp_char[i];
  1126.     if (p_char[n] == '+')
  1127.         p_char[n] = '-';
  1128.     p_len[n] = tp_len[i];
  1129.     }
  1130.     if (blankline) {
  1131.     i = p_ptrn_lines + 1;
  1132.     p_line[n] = tp_line[i];
  1133.     p_char[n] = tp_char[i];
  1134.     p_len[n] = tp_len[i];
  1135.     n++;
  1136.     }
  1137.     assert(p_char[0] == '=');
  1138.     p_char[0] = '*';
  1139.     for (s=p_line[0]; *s; s++)
  1140.     if (*s == '-')
  1141.         *s = '*';
  1142.  
  1143.     /* now turn the old into the new */
  1144.  
  1145.     assert(tp_char[0] == '*');
  1146.     tp_char[0] = '=';
  1147.     for (s=tp_line[0]; *s; s++)
  1148.     if (*s == '*')
  1149.         *s = '-';
  1150.     for (i=0; n <= p_end; i++,n++) {
  1151.     p_line[n] = tp_line[i];
  1152.     p_char[n] = tp_char[i];
  1153.     if (p_char[n] == '-')
  1154.         p_char[n] = '+';
  1155.     p_len[n] = tp_len[i];
  1156.     }
  1157.     assert(i == p_ptrn_lines + 1);
  1158.     i = p_ptrn_lines;
  1159.     p_ptrn_lines = p_repl_lines;
  1160.     p_repl_lines = i;
  1161. #ifndef lint
  1162.     if (tp_line == Null(char**))
  1163.     free((char*)tp_line);
  1164.     if (tp_len == Null(short*))
  1165.     free((char*)tp_len);
  1166. #endif
  1167.     if (tp_char == Nullch)
  1168.     free((char*)tp_char);
  1169.     return TRUE;
  1170. }
  1171.  
  1172. /* Return the specified line position in the old file of the old context. */
  1173.  
  1174. LINENUM
  1175. pch_first()
  1176. {
  1177.     return p_first;
  1178. }
  1179.  
  1180. /* Return the number of lines of old context. */
  1181.  
  1182. LINENUM
  1183. pch_ptrn_lines()
  1184. {
  1185.     return p_ptrn_lines;
  1186. }
  1187.  
  1188. /* Return the probable line position in the new file of the first line. */
  1189.  
  1190. LINENUM
  1191. pch_newfirst()
  1192. {
  1193.     return p_newfirst;
  1194. }
  1195.  
  1196. /* Return the number of lines in the replacement text including context. */
  1197.  
  1198. LINENUM
  1199. pch_repl_lines()
  1200. {
  1201.     return p_repl_lines;
  1202. }
  1203.  
  1204. /* Return the number of lines in the whole hunk. */
  1205.  
  1206. LINENUM
  1207. pch_end()
  1208. {
  1209.     return p_end;
  1210. }
  1211.  
  1212. /* Return the number of context lines before the first changed line. */
  1213.  
  1214. LINENUM
  1215. pch_context()
  1216. {
  1217.     return p_context;
  1218. }
  1219.  
  1220. /* Return the length of a particular patch line. */
  1221.  
  1222. short
  1223. pch_line_len(line)
  1224. LINENUM line;
  1225. {
  1226.     return p_len[line];
  1227. }
  1228.  
  1229. /* Return the control character (+, -, *, !, etc) for a patch line. */
  1230.  
  1231. char
  1232. pch_char(line)
  1233. LINENUM line;
  1234. {
  1235.     return p_char[line];
  1236. }
  1237.  
  1238. /* Return a pointer to a particular patch line. */
  1239.  
  1240. char *
  1241. pfetch(line)
  1242. LINENUM line;
  1243. {
  1244.     return p_line[line];
  1245. }
  1246.  
  1247. /* Return where in the patch file this hunk began, for error messages. */
  1248.  
  1249. LINENUM
  1250. pch_hunk_beg()
  1251. {
  1252.     return p_hunk_beg;
  1253. }
  1254.  
  1255. /* Apply an ed script by feeding ed itself. */
  1256.  
  1257. void
  1258. do_ed_script()
  1259. {
  1260.     Reg1 char *t;
  1261.     Reg2 long beginning_of_this_line;
  1262.     Reg3 bool this_line_is_command = FALSE;
  1263.     Reg4 FILE *pipefp;
  1264.     FILE *popen();
  1265.  
  1266.     if (!skip_rest_of_patch) {
  1267.     Unlink(TMPOUTNAME);
  1268.     copy_file(filearg[0], TMPOUTNAME);
  1269.     if (verbose)
  1270.         Sprintf(buf, "%s %s", EDPATH, TMPOUTNAME);
  1271.     else
  1272.         Sprintf(buf, "%s - %s", EDPATH, TMPOUTNAME);
  1273.     pipefp = popen(buf, "w");
  1274.     }
  1275.     for (;;) {
  1276.     beginning_of_this_line = ftell(pfp);
  1277.     if (pgets(buf, sizeof buf, pfp) == Nullch) {
  1278.         next_intuit_at(beginning_of_this_line,p_input_line);
  1279.         break;
  1280.     }
  1281.     p_input_line++;
  1282.     for (t=buf; isdigit(*t) || *t == ','; t++) ;
  1283.     this_line_is_command = (isdigit(*buf) &&
  1284.       (*t == 'd' || *t == 'c' || *t == 'a') );
  1285.     if (this_line_is_command) {
  1286.         if (!skip_rest_of_patch)
  1287.         fputs(buf, pipefp);
  1288.         if (*t != 'd') {
  1289.         while (pgets(buf, sizeof buf, pfp) != Nullch) {
  1290.             p_input_line++;
  1291.             if (!skip_rest_of_patch)
  1292.             fputs(buf, pipefp);
  1293.             if (strEQ(buf, ".\n"))
  1294.             break;
  1295.         }
  1296.         }
  1297.     }
  1298.     else {
  1299.         next_intuit_at(beginning_of_this_line,p_input_line);
  1300.         break;
  1301.     }
  1302.     }
  1303.     if (skip_rest_of_patch)
  1304.     return;
  1305.     fprintf(pipefp, "w\n");
  1306.     fprintf(pipefp, "q\n");
  1307.     Fflush(pipefp);
  1308.     Pclose(pipefp);
  1309.     ignore_signals();
  1310.     if (move_file(TMPOUTNAME, outname) < 0) {
  1311.     toutkeep = TRUE;
  1312.     chmod(TMPOUTNAME, filemode);
  1313.     }
  1314.     else
  1315.     chmod(outname, filemode);
  1316.     set_signals(1);
  1317. }
  1318.