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