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