home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / patch2 / patch7 / text0000.txt < prev   
Encoding:
Text File  |  1987-02-01  |  23.1 KB  |  927 lines

  1. System: patch version 2.0
  2. Patch #: 7
  3. Priority: LOW (MEDIUM if you're trying to install mangled patches)
  4. Subject: Some mangled patches result in misleading diagnostics.
  5. From: lwall@sdcrdcf.UUCP
  6.  
  7. Description:
  8.     Certain kinds of mangled patches (namely ones in which the line
  9.     numbers are wrong) can give misleading error messages.  At best,
  10.     the line numbers (in the patch file) are wrong.  At worst, the
  11.     error message reads, "core dumped".  At least I hope that was
  12.     the worst.
  13.  
  14. Repeat-By:
  15.     Take a new-style context diff, one which adds some lines, and edit
  16.     the replacement line numbers so that the maximim line number is one
  17.     less than it ought to be.  Run patch.  Watch it puke.
  18.  
  19.     Make an old-style diff of the same patch.  Run patch.  Note the
  20.     uninformative error message with incorrect line numbers.
  21.  
  22. Fix:    From rn, say "| patch -d DIR", where DIR is your patch source
  23.     directory.  Outside of rn, say "cd DIR; patch <thisarticle".
  24.     If you don't have the patch program, apply the following by hand,
  25.     or get patch.
  26.  
  27.     If patch indicates that patchlevel is the wrong version, you may need
  28.     to apply one or more previous patches, or the patch may already
  29.     have been applied.  See the patchlevel.h file to find out what has or
  30.     has not been applied.  In any event, don't continue with the patch.
  31.  
  32.     If you are missing previous patches they can be obtained from me:
  33.  
  34.     Larry Wall
  35.     {allegra,burdvax,cbosgd,hplabs,ihnp4,sdcsvax}!sdcrdcf!lwall
  36.  
  37.     If you send a mail message of the following form (only left-justified)
  38.     it will greatly speed processing:
  39.  
  40.     Subject: Command
  41.     @SH mailpatch PATH patch 2.0 LIST
  42.            ^ note the c
  43.  
  44.     where PATH is a return path FROM ME TO YOU in bang notation, and LIST
  45.     is the number of one or more patches you need, separated by spaces,
  46.     commas, and/or hyphens.  Saying 35- says everything from 35 to the end.
  47.  
  48. Index: patchlevel.h
  49. Prereq: 6
  50. 1c1
  51. < #define PATCHLEVEL 6
  52. ---
  53. > #define PATCHLEVEL 7
  54.  
  55. Index: patch.c
  56. Prereq: 2.0.1.2
  57. *** patch.c.old    Fri Jan 30 22:48:49 1987
  58. *** patch.c    Fri Jan 30 22:49:04 1987
  59. ***************
  60. *** 1,5
  61.   char rcsid[] =
  62. !     "$Header: patch.c,v 2.0.1.2 86/11/21 09:39:15 lwall Exp $";
  63.   
  64.   /* patch - a program to apply diffs to original files
  65.    *
  66.  
  67. --- 1,5 -----
  68.   char rcsid[] =
  69. !     "$Header: patch.c,v 2.0.1.3 87/01/30 22:45:50 lwall Exp $";
  70.   
  71.   /* patch - a program to apply diffs to original files
  72.    *
  73. ***************
  74. *** 9,14
  75.    * money off of it, or pretend that you wrote it.
  76.    *
  77.    * $Log:    patch.c,v $
  78.    * Revision 2.0.1.2  86/11/21  09:39:15  lwall
  79.    * Fuzz factor caused offset of installed lines.
  80.    * 
  81.  
  82. --- 9,18 -----
  83.    * money off of it, or pretend that you wrote it.
  84.    *
  85.    * $Log:    patch.c,v $
  86. +  * Revision 2.0.1.3  87/01/30  22:45:50  lwall
  87. +  * Improved diagnostic on sync error.
  88. +  * Moved do_ed_script() to pch.c.
  89. +  * 
  90.    * Revision 2.0.1.2  86/11/21  09:39:15  lwall
  91.    * Fuzz factor caused offset of installed lines.
  92.    * 
  93. ***************
  94. *** 88,94
  95.   LINENUM locate_hunk();
  96.   void abort_hunk();
  97.   void apply_hunk();
  98. - void do_ed_script();
  99.   void init_output();
  100.   void init_reject();
  101.   void copy_till();
  102.  
  103. --- 92,97 -----
  104.   LINENUM locate_hunk();
  105.   void abort_hunk();
  106.   void apply_hunk();
  107.   void init_output();
  108.   void init_reject();
  109.   void copy_till();
  110. ***************
  111. *** 584,592
  112.       }
  113.       else {
  114.           if (pch_char(new) != pch_char(old)) {
  115. !         say3("Out-of-sync patch, lines %ld,%ld\n",
  116. !             pch_hunk_beg() + old - 1,
  117. !             pch_hunk_beg() + new - 1);
  118.   #ifdef DEBUGGING
  119.           say3("oldchar = '%c', newchar = '%c'\n",
  120.               pch_char(old), pch_char(new));
  121.  
  122. --- 587,595 -----
  123.       }
  124.       else {
  125.           if (pch_char(new) != pch_char(old)) {
  126. !         say3("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
  127. !             pch_hunk_beg() + old,
  128. !             pch_hunk_beg() + new);
  129.   #ifdef DEBUGGING
  130.           say3("oldchar = '%c', newchar = '%c'\n",
  131.               pch_char(old), pch_char(new));
  132. ***************
  133. *** 646,713
  134.       if (R_do_defines && def_state != OUTSIDE) {
  135.       fputs(end_defined, ofp);
  136.       }
  137. - }
  138. - /* Apply an ed script by feeding ed itself. */
  139. - void
  140. - do_ed_script()
  141. - {
  142. -     Reg1 char *t;
  143. -     Reg2 long beginning_of_this_line;
  144. -     Reg3 bool this_line_is_command = FALSE;
  145. -     Reg4 FILE *pipefp;
  146. -     FILE *popen();
  147. -     if (!skip_rest_of_patch) {
  148. -     Unlink(TMPOUTNAME);
  149. -     copy_file(filearg[0], TMPOUTNAME);
  150. -     if (verbose)
  151. -         Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
  152. -     else
  153. -         Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
  154. -     pipefp = popen(buf, "w");
  155. -     }
  156. -     for (;;) {
  157. -     beginning_of_this_line = ftell(pfp);
  158. -     if (pgets(buf, sizeof buf, pfp) == Nullch) {
  159. -         next_intuit_at(beginning_of_this_line);
  160. -         break;
  161. -     }
  162. -     for (t=buf; isdigit(*t) || *t == ','; t++) ;
  163. -     this_line_is_command = (isdigit(*buf) &&
  164. -       (*t == 'd' || *t == 'c' || *t == 'a') );
  165. -     if (this_line_is_command) {
  166. -         if (!skip_rest_of_patch)
  167. -         fputs(buf, pipefp);
  168. -         if (*t != 'd') {
  169. -         while (pgets(buf, sizeof buf, pfp) != Nullch) {
  170. -             if (!skip_rest_of_patch)
  171. -             fputs(buf, pipefp);
  172. -             if (strEQ(buf, ".\n"))
  173. -             break;
  174. -         }
  175. -         }
  176. -     }
  177. -     else {
  178. -         next_intuit_at(beginning_of_this_line);
  179. -         break;
  180. -     }
  181. -     }
  182. -     if (skip_rest_of_patch)
  183. -     return;
  184. -     fprintf(pipefp, "w\n");
  185. -     fprintf(pipefp, "q\n");
  186. -     Fflush(pipefp);
  187. -     Pclose(pipefp);
  188. -     ignore_signals();
  189. -     if (move_file(TMPOUTNAME, outname) < 0) {
  190. -     toutkeep = TRUE;
  191. -     chmod(TMPOUTNAME, filemode);
  192. -     }
  193. -     else
  194. -     chmod(outname, filemode);
  195. -     set_signals();
  196.   }
  197.   
  198.   /* Open the new file. */
  199.  
  200. --- 649,654 -----
  201.       if (R_do_defines && def_state != OUTSIDE) {
  202.       fputs(end_defined, ofp);
  203.       }
  204.   }
  205.   
  206.   /* Open the new file. */
  207.  
  208. Index: pch.c
  209. Prereq: 2.0.1.4
  210. *** pch.c.old    Fri Jan 30 22:50:06 1987
  211. *** pch.c    Fri Jan 30 22:50:30 1987
  212. ***************
  213. *** 1,4
  214. ! /* $Header: pch.c,v 2.0.1.4 87/01/05 16:59:53 lwall Exp $
  215.    *
  216.    * $Log:    pch.c,v $
  217.    * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  218.  
  219. --- 1,4 -----
  220. ! /* $Header: pch.c,v 2.0.1.5 87/01/30 22:47:42 lwall Exp $
  221.    *
  222.    * $Log:    pch.c,v $
  223.    * Revision 2.0.1.5  87/01/30  22:47:42  lwall
  224. ***************
  225. *** 1,6
  226.   /* $Header: pch.c,v 2.0.1.4 87/01/05 16:59:53 lwall Exp $
  227.    *
  228.    * $Log:    pch.c,v $
  229.    * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  230.    * New-style context diffs caused double call to free().
  231.    * 
  232.  
  233. --- 1,9 -----
  234.   /* $Header: pch.c,v 2.0.1.5 87/01/30 22:47:42 lwall Exp $
  235.    *
  236.    * $Log:    pch.c,v $
  237. +  * Revision 2.0.1.5  87/01/30  22:47:42  lwall
  238. +  * Improved responses to mangled patches.
  239. +  * 
  240.    * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  241.    * New-style context diffs caused double call to free().
  242.    * 
  243. ***************
  244. *** 42,47
  245.   static int hunkmax = INITHUNKMAX;    /* size of above arrays to begin with */
  246.   static int p_indent;            /* indent to patch */
  247.   static LINENUM p_base;            /* where to intuit this time */
  248.   static LINENUM p_start;            /* where intuit found a patch */
  249.   static LINENUM p_efake = -1;        /* end of faked up lines--don't free */
  250.   static LINENUM p_bfake = -1;        /* beg of faked up lines */
  251.  
  252. --- 45,51 -----
  253.   static int hunkmax = INITHUNKMAX;    /* size of above arrays to begin with */
  254.   static int p_indent;            /* indent to patch */
  255.   static LINENUM p_base;            /* where to intuit this time */
  256. + static LINENUM p_bline;            /* line # of p_base */
  257.   static LINENUM p_start;            /* where intuit found a patch */
  258.   static LINENUM p_sline;            /* and the line number for it */
  259.   static LINENUM p_hunk_beg;        /* line number of current hunk */
  260. ***************
  261. *** 43,48
  262.   static int p_indent;            /* indent to patch */
  263.   static LINENUM p_base;            /* where to intuit this time */
  264.   static LINENUM p_start;            /* where intuit found a patch */
  265.   static LINENUM p_efake = -1;        /* end of faked up lines--don't free */
  266.   static LINENUM p_bfake = -1;        /* beg of faked up lines */
  267.   
  268.  
  269. --- 47,54 -----
  270.   static LINENUM p_base;            /* where to intuit this time */
  271.   static LINENUM p_bline;            /* line # of p_base */
  272.   static LINENUM p_start;            /* where intuit found a patch */
  273. + static LINENUM p_sline;            /* and the line number for it */
  274. + static LINENUM p_hunk_beg;        /* line number of current hunk */
  275.   static LINENUM p_efake = -1;        /* end of faked up lines--don't free */
  276.   static LINENUM p_bfake = -1;        /* beg of faked up lines */
  277.   
  278. ***************
  279. *** 80,86
  280.       fatal2("patch file %s not found\n", filename);
  281.       Fstat(fileno(pfp), &filestat);
  282.       p_filesize = filestat.st_size;
  283. !     next_intuit_at(0L);            /* start at the beginning */
  284.       set_hunkmax();
  285.   }
  286.   
  287.  
  288. --- 86,92 -----
  289.       fatal2("patch file %s not found\n", filename);
  290.       Fstat(fileno(pfp), &filestat);
  291.       p_filesize = filestat.st_size;
  292. !     next_intuit_at(0L,1L);            /* start at the beginning */
  293.       set_hunkmax();
  294.   }
  295.   
  296. ***************
  297. *** 155,161
  298.           "an ed script" );
  299.       if (p_indent && verbose)
  300.       say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  301. !     skip_to(p_start);
  302.       while (filearg[0] == Nullch) {
  303.       if (force) {
  304.           say1("No file to patch.  Skipping...\n");
  305.  
  306. --- 161,167 -----
  307.           "an ed script" );
  308.       if (p_indent && verbose)
  309.       say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  310. !     skip_to(p_start,p_sline);
  311.       while (filearg[0] == Nullch) {
  312.       if (force) {
  313.           say1("No file to patch.  Skipping...\n");
  314. ***************
  315. *** 192,197
  316.       Reg4 long this_line = 0;
  317.       Reg5 long previous_line;
  318.       Reg6 long first_command_line = -1;
  319.       Reg7 bool last_line_was_command = FALSE;
  320.       Reg8 bool this_is_a_command = FALSE;
  321.       Reg9 bool stars_last_line = FALSE;
  322.  
  323. --- 198,204 -----
  324.       Reg4 long this_line = 0;
  325.       Reg5 long previous_line;
  326.       Reg6 long first_command_line = -1;
  327. +     long fcl_line;
  328.       Reg7 bool last_line_was_command = FALSE;
  329.       Reg8 bool this_is_a_command = FALSE;
  330.       Reg9 bool stars_last_line = FALSE;
  331. ***************
  332. *** 210,215
  333.   
  334.       ok_to_create_file = FALSE;
  335.       Fseek(pfp, p_base, 0);
  336.       for (;;) {
  337.       previous_line = this_line;
  338.       last_line_was_command = this_is_a_command;
  339.  
  340. --- 217,223 -----
  341.   
  342.       ok_to_create_file = FALSE;
  343.       Fseek(pfp, p_base, 0);
  344. +     p_input_line = p_bline - 1;
  345.       for (;;) {
  346.       previous_line = this_line;
  347.       last_line_was_command = this_is_a_command;
  348. ***************
  349. *** 216,221
  350.       stars_last_line = stars_this_line;
  351.       this_line = ftell(pfp);
  352.       indent = 0;
  353.       if (fgets(buf, sizeof buf, pfp) == Nullch) {
  354.           if (first_command_line >= 0L) {
  355.                       /* nothing but deletes!? */
  356.  
  357. --- 224,230 -----
  358.       stars_last_line = stars_this_line;
  359.       this_line = ftell(pfp);
  360.       indent = 0;
  361. +     p_input_line++;
  362.       if (fgets(buf, sizeof buf, pfp) == Nullch) {
  363.           if (first_command_line >= 0L) {
  364.                       /* nothing but deletes!? */
  365. ***************
  366. *** 220,225
  367.           if (first_command_line >= 0L) {
  368.                       /* nothing but deletes!? */
  369.           p_start = first_command_line;
  370.           retval = ED_DIFF;
  371.           goto scan_exit;
  372.           }
  373.  
  374. --- 229,235 -----
  375.           if (first_command_line >= 0L) {
  376.                       /* nothing but deletes!? */
  377.           p_start = first_command_line;
  378. +         p_sline = fcl_line;
  379.           retval = ED_DIFF;
  380.           goto scan_exit;
  381.           }
  382. ***************
  383. *** 225,230
  384.           }
  385.           else {
  386.           p_start = this_line;
  387.           retval = 0;
  388.           goto scan_exit;
  389.           }
  390.  
  391. --- 235,241 -----
  392.           }
  393.           else {
  394.           p_start = this_line;
  395. +         p_sline = p_input_line;
  396.           retval = 0;
  397.           goto scan_exit;
  398.           }
  399. ***************
  400. *** 240,245
  401.         (*t == 'd' || *t == 'c' || *t == 'a') );
  402.       if (first_command_line < 0L && this_is_a_command) { 
  403.           first_command_line = this_line;
  404.           p_indent = indent;        /* assume this for now */
  405.       }
  406.       if (!stars_last_line && strnEQ(s, "*** ", 4))
  407.  
  408. --- 251,257 -----
  409.         (*t == 'd' || *t == 'c' || *t == 'a') );
  410.       if (first_command_line < 0L && this_is_a_command) { 
  411.           first_command_line = this_line;
  412. +         fcl_line = p_input_line;
  413.           p_indent = indent;        /* assume this for now */
  414.       }
  415.       if (!stars_last_line && strnEQ(s, "*** ", 4))
  416. ***************
  417. *** 263,268
  418.         strEQ(s, ".\n") ) {
  419.           p_indent = indent;
  420.           p_start = first_command_line;
  421.           retval = ED_DIFF;
  422.           goto scan_exit;
  423.       }
  424.  
  425. --- 275,281 -----
  426.         strEQ(s, ".\n") ) {
  427.           p_indent = indent;
  428.           p_start = first_command_line;
  429. +         p_sline = fcl_line;
  430.           retval = ED_DIFF;
  431.           goto scan_exit;
  432.       }
  433. ***************
  434. *** 277,282
  435.           s++;
  436.           p_indent = indent;
  437.           p_start = previous_line;
  438.           retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
  439.           goto scan_exit;
  440.       }
  441.  
  442. --- 290,296 -----
  443.           s++;
  444.           p_indent = indent;
  445.           p_start = previous_line;
  446. +         p_sline = p_input_line - 1;
  447.           retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
  448.           goto scan_exit;
  449.       }
  450. ***************
  451. *** 284,289
  452.         last_line_was_command &&
  453.         (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
  454.           p_start = previous_line;
  455.           p_indent = indent;
  456.           retval = NORMAL_DIFF;
  457.           goto scan_exit;
  458.  
  459. --- 298,304 -----
  460.         last_line_was_command &&
  461.         (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
  462.           p_start = previous_line;
  463. +         p_sline = p_input_line - 1;
  464.           p_indent = indent;
  465.           retval = NORMAL_DIFF;
  466.           goto scan_exit;
  467. ***************
  468. *** 352,358
  469.   /* Remember where this patch ends so we know where to start up again. */
  470.   
  471.   void
  472. ! next_intuit_at(file_pos)
  473.   long file_pos;
  474.   {
  475.       p_base = file_pos;
  476.  
  477. --- 367,373 -----
  478.   /* Remember where this patch ends so we know where to start up again. */
  479.   
  480.   void
  481. ! next_intuit_at(file_pos,file_line)
  482.   long file_pos;
  483.   long file_line;
  484.   {
  485. ***************
  486. *** 354,359
  487.   void
  488.   next_intuit_at(file_pos)
  489.   long file_pos;
  490.   {
  491.       p_base = file_pos;
  492.   }
  493.  
  494. --- 369,375 -----
  495.   void
  496.   next_intuit_at(file_pos,file_line)
  497.   long file_pos;
  498. + long file_line;
  499.   {
  500.       p_base = file_pos;
  501.       p_bline = file_line;
  502. ***************
  503. *** 356,361
  504.   long file_pos;
  505.   {
  506.       p_base = file_pos;
  507.   }
  508.   
  509.   /* Basically a verbose fseek() to the actual diff listing. */
  510.  
  511. --- 372,378 -----
  512.   long file_line;
  513.   {
  514.       p_base = file_pos;
  515. +     p_bline = file_line;
  516.   }
  517.   
  518.   /* Basically a verbose fseek() to the actual diff listing. */
  519. ***************
  520. *** 361,367
  521.   /* Basically a verbose fseek() to the actual diff listing. */
  522.   
  523.   void
  524. ! skip_to(file_pos)
  525.   long file_pos;
  526.   {
  527.       char *ret;
  528.  
  529. --- 378,384 -----
  530.   /* Basically a verbose fseek() to the actual diff listing. */
  531.   
  532.   void
  533. ! skip_to(file_pos,file_line)
  534.   long file_pos;
  535.   long file_line;
  536.   {
  537. ***************
  538. *** 363,368
  539.   void
  540.   skip_to(file_pos)
  541.   long file_pos;
  542.   {
  543.       char *ret;
  544.   
  545.  
  546. --- 380,386 -----
  547.   void
  548.   skip_to(file_pos,file_line)
  549.   long file_pos;
  550. + long file_line;
  551.   {
  552.       char *ret;
  553.   
  554. ***************
  555. *** 379,384
  556.       }
  557.       else
  558.       Fseek(pfp, file_pos, 0);
  559.   }
  560.   
  561.   /* True if there is more of the current diff listing to process. */
  562.  
  563. --- 397,403 -----
  564.       }
  565.       else
  566.       Fseek(pfp, file_pos, 0);
  567. +     p_input_line = file_line - 1;
  568.   }
  569.   
  570.   /* True if there is more of the current diff listing to process. */
  571. ***************
  572. *** 419,424
  573.                       /* # of copiable lines in ptrn */
  574.   
  575.       ret = pgets(buf, sizeof buf, pfp);
  576.       if (ret == Nullch || strnNE(buf, "********", 8)) {
  577.           next_intuit_at(line_beginning);
  578.           return FALSE;
  579.  
  580. --- 438,444 -----
  581.                       /* # of copiable lines in ptrn */
  582.   
  583.       ret = pgets(buf, sizeof buf, pfp);
  584. +     p_input_line++;
  585.       if (ret == Nullch || strnNE(buf, "********", 8)) {
  586.           next_intuit_at(line_beginning,p_input_line);
  587.           return FALSE;
  588. ***************
  589. *** 420,426
  590.   
  591.       ret = pgets(buf, sizeof buf, pfp);
  592.       if (ret == Nullch || strnNE(buf, "********", 8)) {
  593. !         next_intuit_at(line_beginning);
  594.           return FALSE;
  595.       }
  596.       p_context = 100;
  597.  
  598. --- 440,446 -----
  599.       ret = pgets(buf, sizeof buf, pfp);
  600.       p_input_line++;
  601.       if (ret == Nullch || strnNE(buf, "********", 8)) {
  602. !         next_intuit_at(line_beginning,p_input_line);
  603.           return FALSE;
  604.       }
  605.       p_context = 100;
  606. ***************
  607. *** 424,429
  608.           return FALSE;
  609.       }
  610.       p_context = 100;
  611.       while (p_end < p_max) {
  612.           line_beginning = ftell(pfp);
  613.           ret = pgets(buf, sizeof buf, pfp);
  614.  
  615. --- 444,450 -----
  616.           return FALSE;
  617.       }
  618.       p_context = 100;
  619. +     p_hunk_beg = p_input_line + 1;
  620.       while (p_end < p_max) {
  621.           line_beginning = ftell(pfp);
  622.           ret = pgets(buf, sizeof buf, pfp);
  623. ***************
  624. *** 427,432
  625.       while (p_end < p_max) {
  626.           line_beginning = ftell(pfp);
  627.           ret = pgets(buf, sizeof buf, pfp);
  628.           if (ret == Nullch) {
  629.           if (p_max - p_end < 4)
  630.               Strcpy(buf, "  \n");  /* assume blank lines got chopped */
  631.  
  632. --- 448,454 -----
  633.       while (p_end < p_max) {
  634.           line_beginning = ftell(pfp);
  635.           ret = pgets(buf, sizeof buf, pfp);
  636. +         p_input_line++;
  637.           if (ret == Nullch) {
  638.           if (p_max - p_end < 4)
  639.               Strcpy(buf, "  \n");  /* assume blank lines got chopped */
  640. ***************
  641. *** 438,444
  642.               fatal1("Unexpected end of file in patch.\n");
  643.           }
  644.           }
  645. -         p_input_line++;
  646.           p_end++;
  647.           assert(p_end < hunkmax);
  648.           p_char[p_end] = *buf;
  649.  
  650. --- 460,465 -----
  651.               fatal1("Unexpected end of file in patch.\n");
  652.           }
  653.           }
  654.           p_end++;
  655.           assert(p_end < hunkmax);
  656.           p_char[p_end] = *buf;
  657. ***************
  658. *** 491,498
  659.           break;
  660.           case '-':
  661.           if (buf[1] == '-') {
  662. !             if (p_end != p_ptrn_lines + 1 &&
  663. !             p_end != p_ptrn_lines + 2) {
  664.               if (p_end == 1) {
  665.                   /* `old' lines were omitted - set up to fill */
  666.                   /* them in from 'new' context lines. */
  667.  
  668. --- 512,520 -----
  669.           break;
  670.           case '-':
  671.           if (buf[1] == '-') {
  672. !             if (repl_beginning ||
  673. !             (p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
  674. !             {
  675.               if (p_end == 1) {
  676.                   /* `old' lines were omitted - set up to fill */
  677.                   /* them in from 'new' context lines. */
  678. ***************
  679. *** 502,510
  680.                   fillcnt = p_ptrn_lines;
  681.               }
  682.               else {
  683. !                 if (repl_beginning && repl_could_be_missing){
  684. !                 repl_missing = TRUE;
  685. !                 goto hunk_done;
  686.                   }
  687.                   fatal3("Unexpected --- at line %ld: %s",
  688.                   p_input_line, buf);
  689.  
  690. --- 524,537 -----
  691.                   fillcnt = p_ptrn_lines;
  692.               }
  693.               else {
  694. !                 if (repl_beginning) {
  695. !                 if (repl_could_be_missing){
  696. !                     repl_missing = TRUE;
  697. !                     goto hunk_done;
  698. !                 }
  699. !                 fatal3(
  700. ! "Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
  701. !                     p_input_line, p_hunk_beg + repl_beginning);
  702.                   }
  703.                   else {
  704.                   fatal4(
  705. ***************
  706. *** 506,513
  707.                   repl_missing = TRUE;
  708.                   goto hunk_done;
  709.                   }
  710. !                 fatal3("Unexpected --- at line %ld: %s",
  711. !                 p_input_line, buf);
  712.               }
  713.               }
  714.               repl_beginning = p_end;
  715.  
  716. --- 533,546 -----
  717.   "Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
  718.                       p_input_line, p_hunk_beg + repl_beginning);
  719.                   }
  720. !                 else {
  721. !                 fatal4(
  722. ! "%s \"---\" at line %ld--check line numbers at line %ld.\n",
  723. !                     (p_end <= p_ptrn_lines
  724. !                     ? "Premature"
  725. !                     : "Overdue" ),
  726. !                     p_input_line, p_hunk_beg);
  727. !                 }
  728.               }
  729.               }
  730.               repl_beginning = p_end;
  731. ***************
  732. *** 649,655
  733.           p_bfake = filldst;        /* remember where not to free() */
  734.           p_efake = filldst + fillcnt - 1;
  735.           while (fillcnt-- > 0) {
  736. !         while (p_char[fillsrc] != ' ')
  737.               fillsrc++;
  738.           p_line[filldst] = p_line[fillsrc];
  739.           p_char[filldst] = p_char[fillsrc];
  740.  
  741. --- 682,688 -----
  742.           p_bfake = filldst;        /* remember where not to free() */
  743.           p_efake = filldst + fillcnt - 1;
  744.           while (fillcnt-- > 0) {
  745. !         while (fillsrc <= p_end && p_char[fillsrc] != ' ')
  746.               fillsrc++;
  747.           if (fillsrc > p_end)
  748.               fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
  749. ***************
  750. *** 651,656
  751.           while (fillcnt-- > 0) {
  752.           while (p_char[fillsrc] != ' ')
  753.               fillsrc++;
  754.           p_line[filldst] = p_line[fillsrc];
  755.           p_char[filldst] = p_char[fillsrc];
  756.           p_len[filldst] = p_len[fillsrc];
  757.  
  758. --- 684,692 -----
  759.           while (fillcnt-- > 0) {
  760.           while (fillsrc <= p_end && p_char[fillsrc] != ' ')
  761.               fillsrc++;
  762. +         if (fillsrc > p_end)
  763. +             fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
  764. +             p_hunk_beg);
  765.           p_line[filldst] = p_line[fillsrc];
  766.           p_char[filldst] = p_char[fillsrc];
  767.           p_len[filldst] = p_len[fillsrc];
  768. ***************
  769. *** 678,684
  770.       ret = pgets(buf, sizeof buf, pfp);
  771.       p_input_line++;
  772.       if (ret == Nullch || !isdigit(*buf)) {
  773. !         next_intuit_at(line_beginning);
  774.           return FALSE;
  775.       }
  776.       p_first = (LINENUM)atol(buf);
  777.  
  778. --- 714,720 -----
  779.       ret = pgets(buf, sizeof buf, pfp);
  780.       p_input_line++;
  781.       if (ret == Nullch || !isdigit(*buf)) {
  782. !         next_intuit_at(line_beginning,p_input_line);
  783.           return FALSE;
  784.       }
  785.       p_first = (LINENUM)atol(buf);
  786. ***************
  787. *** 994,999
  788.   LINENUM
  789.   pch_hunk_beg()
  790.   {
  791. !     return p_input_line - p_end - 1;
  792.   }
  793.   
  794.  
  795. --- 1030,1036 -----
  796.   LINENUM
  797.   pch_hunk_beg()
  798.   {
  799. !     return p_hunk_beg;
  800.   }
  801.   
  802.   /* Apply an ed script by feeding ed itself. */
  803. ***************
  804. *** 997,999
  805.       return p_input_line - p_end - 1;
  806.   }
  807.   
  808.  
  809. --- 1033,1098 -----
  810.       return p_hunk_beg;
  811.   }
  812.   
  813. + /* Apply an ed script by feeding ed itself. */
  814. + void
  815. + do_ed_script()
  816. + {
  817. +     Reg1 char *t;
  818. +     Reg2 long beginning_of_this_line;
  819. +     Reg3 bool this_line_is_command = FALSE;
  820. +     Reg4 FILE *pipefp;
  821. +     FILE *popen();
  822. +     if (!skip_rest_of_patch) {
  823. +     Unlink(TMPOUTNAME);
  824. +     copy_file(filearg[0], TMPOUTNAME);
  825. +     if (verbose)
  826. +         Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
  827. +     else
  828. +         Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
  829. +     pipefp = popen(buf, "w");
  830. +     }
  831. +     for (;;) {
  832. +     beginning_of_this_line = ftell(pfp);
  833. +     if (pgets(buf, sizeof buf, pfp) == Nullch) {
  834. +         next_intuit_at(beginning_of_this_line,p_input_line);
  835. +         break;
  836. +     }
  837. +     p_input_line++;
  838. +     for (t=buf; isdigit(*t) || *t == ','; t++) ;
  839. +     this_line_is_command = (isdigit(*buf) &&
  840. +       (*t == 'd' || *t == 'c' || *t == 'a') );
  841. +     if (this_line_is_command) {
  842. +         if (!skip_rest_of_patch)
  843. +         fputs(buf, pipefp);
  844. +         if (*t != 'd') {
  845. +         while (pgets(buf, sizeof buf, pfp) != Nullch) {
  846. +             p_input_line++;
  847. +             if (!skip_rest_of_patch)
  848. +             fputs(buf, pipefp);
  849. +             if (strEQ(buf, ".\n"))
  850. +             break;
  851. +         }
  852. +         }
  853. +     }
  854. +     else {
  855. +         next_intuit_at(beginning_of_this_line,p_input_line);
  856. +         break;
  857. +     }
  858. +     }
  859. +     if (skip_rest_of_patch)
  860. +     return;
  861. +     fprintf(pipefp, "w\n");
  862. +     fprintf(pipefp, "q\n");
  863. +     Fflush(pipefp);
  864. +     Pclose(pipefp);
  865. +     ignore_signals();
  866. +     if (move_file(TMPOUTNAME, outname) < 0) {
  867. +     toutkeep = TRUE;
  868. +     chmod(TMPOUTNAME, filemode);
  869. +     }
  870. +     else
  871. +     chmod(outname, filemode);
  872. +     set_signals();
  873. + }
  874.  
  875. Index: pch.h
  876. Prereq: 2.0
  877. *** pch.h.old    Fri Jan 30 22:49:38 1987
  878. *** pch.h    Fri Jan 30 22:49:40 1987
  879. ***************
  880. *** 1,4
  881. ! /* $Header: pch.h,v 2.0 86/09/17 15:39:57 lwall Exp $
  882.    *
  883.    * $Log:    pch.h,v $
  884.    * Revision 2.0  86/09/17  15:39:57  lwall
  885.  
  886. --- 1,4 -----
  887. ! /* $Header: pch.h,v 2.0.1.1 87/01/30 22:47:16 lwall Exp $
  888.    *
  889.    * $Log:    pch.h,v $
  890.    * Revision 2.0.1.1  87/01/30  22:47:16  lwall
  891. ***************
  892. *** 1,6
  893.   /* $Header: pch.h,v 2.0 86/09/17 15:39:57 lwall Exp $
  894.    *
  895.    * $Log:    pch.h,v $
  896.    * Revision 2.0  86/09/17  15:39:57  lwall
  897.    * Baseline for netwide release.
  898.    * 
  899.  
  900. --- 1,9 -----
  901.   /* $Header: pch.h,v 2.0.1.1 87/01/30 22:47:16 lwall Exp $
  902.    *
  903.    * $Log:    pch.h,v $
  904. +  * Revision 2.0.1.1  87/01/30  22:47:16  lwall
  905. +  * Added do_ed_script().
  906. +  * 
  907.    * Revision 2.0  86/09/17  15:39:57  lwall
  908.    * Baseline for netwide release.
  909.    * 
  910. ***************
  911. *** 30,32
  912.   char pch_char();
  913.   char *pfetch();
  914.   char *pgets();
  915.  
  916. --- 33,36 -----
  917.   char pch_char();
  918.   char *pfetch();
  919.   char *pgets();
  920. + void do_ed_script();
  921.  
  922.