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