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