home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / utility / patch / patch.c < prev    next >
Encoding:
Text File  |  1988-07-26  |  19.6 KB  |  689 lines

  1. char rcsid[] =
  2.     "$Header: patch.c,v 2.0.1.6 88/06/22 20:46:39 lwall Locked $";
  3.  
  4. /* patch - a program to apply diffs to original files
  5.  *
  6.  * Copyright 1986, Larry Wall
  7.  *
  8.  * This program may be copied as long as you don't try to make any
  9.  * money off of it, or pretend that you wrote it.
  10.  *
  11.  * $Log:    patch.c,v $
  12.  * Revision 2.0.1.6  88/06/22  20:46:39  lwall
  13.  * patch12: rindex() wasn't declared
  14.  * 
  15.  * Revision 2.0.1.5  88/06/03  15:09:37  lwall
  16.  * patch10: exit code improved.
  17.  * patch10: better support for non-flexfilenames.
  18.  * 
  19.  * Revision 2.0.1.4  87/02/16  14:00:04  lwall
  20.  * Short replacement caused spurious "Out of sync" message.
  21.  * 
  22.  * Revision 2.0.1.3  87/01/30  22:45:50  lwall
  23.  * Improved diagnostic on sync error.
  24.  * Moved do_ed_script() to pch.c.
  25.  * 
  26.  * Revision 2.0.1.2  86/11/21  09:39:15  lwall
  27.  * Fuzz factor caused offset of installed lines.
  28.  * 
  29.  * Revision 2.0.1.1  86/10/29  13:10:22  lwall
  30.  * Backwards search could terminate prematurely.
  31.  * 
  32.  * Revision 2.0  86/09/17  15:37:32  lwall
  33.  * Baseline for netwide release.
  34.  * 
  35.  * Revision 1.5  86/08/01  20:53:24  lwall
  36.  * Changed some %d's to %ld's.
  37.  * Linted.
  38.  * 
  39.  * Revision 1.4  86/08/01  19:17:29  lwall
  40.  * Fixes for machines that can't vararg.
  41.  * Added fuzz factor.
  42.  * Generalized -p.
  43.  * General cleanup.
  44.  * 
  45.  * 85/08/15 van%ucbmonet@berkeley
  46.  * Changes for 4.3bsd diff -c.
  47.  *
  48.  * Revision 1.3  85/03/26  15:07:43  lwall
  49.  * Frozen.
  50.  * 
  51.  * Revision 1.2.1.9  85/03/12  17:03:35  lwall
  52.  * Changed pfp->_file to fileno(pfp).
  53.  * 
  54.  * Revision 1.2.1.8  85/03/12  16:30:43  lwall
  55.  * Check i_ptr and i_womp to make sure they aren't null before freeing.
  56.  * Also allow ed output to be suppressed.
  57.  * 
  58.  * Revision 1.2.1.7  85/03/12  15:56:13  lwall
  59.  * Added -p option from jromine@uci-750a.
  60.  * 
  61.  * Revision 1.2.1.6  85/03/12  12:12:51  lwall
  62.  * Now checks for normalness of file to patch.
  63.  * 
  64.  * Revision 1.2.1.5  85/03/12  11:52:12  lwall
  65.  * Added -D (#ifdef) option from joe@fluke.
  66.  * 
  67.  * Revision 1.2.1.4  84/12/06  11:14:15  lwall
  68.  * Made smarter about SCCS subdirectories.
  69.  * 
  70.  * Revision 1.2.1.3  84/12/05  11:18:43  lwall
  71.  * Added -l switch to do loose string comparison.
  72.  * 
  73.  * Revision 1.2.1.2  84/12/04  09:47:13  lwall
  74.  * Failed hunk count not reset on multiple patch file.
  75.  * 
  76.  * Revision 1.2.1.1  84/12/04  09:42:37  lwall
  77.  * Branch for sdcrdcf changes.
  78.  * 
  79.  * Revision 1.2  84/11/29  13:29:51  lwall
  80.  * Linted.  Identifiers uniqified.  Fixed i_ptr malloc() bug.  Fixed
  81.  * multiple calls to mktemp().  Will now work on machines that can only
  82.  * read 32767 chars.  Added -R option for diffs with new and old swapped.
  83.  * Various cosmetic changes.
  84.  * 
  85.  * Revision 1.1  84/11/09  17:03:58  lwall
  86.  * Initial revision
  87.  * 
  88.  */
  89.  
  90. #include "INTERN.h"
  91. #include "common.h"
  92. #include "EXTERN.h"
  93. #include "version.h"
  94. #include "util.h"
  95. #include "pch.h"
  96. #include "inp.h"
  97.  
  98. /* procedures */
  99.  
  100. void reinitialize_almost_everything();
  101. void get_some_switches();
  102. LINENUM locate_hunk();
  103. void abort_hunk();
  104. void apply_hunk();
  105. void init_output();
  106. void init_reject();
  107. void copy_till();
  108. void spew_output();
  109. void dump_line();
  110. bool patch_match();
  111. bool similar();
  112. void re_input();
  113. void my_exit();
  114.  
  115. /* Apply a set of diffs as appropriate. */
  116.  
  117. main(argc,argv)
  118. int argc;
  119. char **argv;
  120. {
  121.     LINENUM where;
  122.     LINENUM newwhere;
  123.     LINENUM fuzz;
  124.     LINENUM mymaxfuzz;
  125.     int hunk = 0;
  126.     int failed = 0;
  127.     int failtotal = 0;
  128.     int i;
  129.  
  130.     setbuf(stderr, serrbuf);
  131.     for (i = 0; i<MAXFILEC; i++)
  132.     filearg[i] = Nullch;
  133.     Mktemp(TMPOUTNAME);
  134.     Mktemp(TMPINNAME);
  135.     Mktemp(TMPREJNAME);
  136.     Mktemp(TMPPATNAME);
  137.  
  138.     /* parse switches */
  139.     Argc = argc;
  140.     Argv = argv;
  141.     get_some_switches();
  142.     
  143.     /* make sure we clean up /tmp in case of disaster */
  144.     set_signals(0);
  145.  
  146.     for (
  147.     open_patch_file(filearg[1]);
  148.     there_is_another_patch();
  149.     reinitialize_almost_everything()
  150.     ) {                    /* for each patch in patch file */
  151.  
  152.     if (outname == Nullch)
  153.         outname = savestr(filearg[0]);
  154.     
  155.     /* initialize the patched file */
  156.     if (!skip_rest_of_patch)
  157.         init_output(TMPOUTNAME);
  158.     
  159.     /* for ed script just up and do it and exit */
  160.     if (diff_type == ED_DIFF) {
  161.         do_ed_script();
  162.         continue;
  163.     }
  164.     
  165.     /* initialize reject file */
  166.     init_reject(TMPREJNAME);
  167.     
  168.     /* find out where all the lines are */
  169.     if (!skip_rest_of_patch)
  170.         scan_input(filearg[0]);
  171.     
  172.     /* from here on, open no standard i/o files, because malloc */
  173.     /* might misfire and we can't catch it easily */
  174.     
  175.     /* apply each hunk of patch */
  176.     hunk = 0;
  177.     failed = 0;
  178.     out_of_mem = FALSE;
  179.     while (another_hunk()) {
  180.         hunk++;
  181.         fuzz = Nulline;
  182.         mymaxfuzz = pch_context();
  183.         if (maxfuzz < mymaxfuzz)
  184.         mymaxfuzz = maxfuzz;
  185.         if (!skip_rest_of_patch) {
  186.         do {
  187.             where = locate_hunk(fuzz);
  188.             if (hunk == 1 && where == Nulline && !force) {
  189.                         /* dwim for reversed patch? */
  190.             if (!pch_swap()) {
  191.                 if (fuzz == Nulline)
  192.                 say1(
  193. "Not enough memory to try swapped hunk!  Assuming unswapped.\n");
  194.                 continue;
  195.             }
  196.             reverse = !reverse;
  197.             where = locate_hunk(fuzz);  /* try again */
  198.             if (where == Nulline) {        /* didn't find it swapped */
  199.                 if (!pch_swap())         /* put it back to nor⇧נijijpp≥rrr✓Ä⇧Ä⇧הCכæÇ⇦⇧נâÇÇOÇנÄ⇧üÇ Oê⇦äÇy⓪Çijfa 88y99'τττאDÖ\£ךHH✓\Ö]Ö\£ךNבBéBB_CBéBBY[ךHəYê
  200. ¢ץÖ]Ö\£ךJHבBéBBH✓✓✓əYê
  201. ✓\⑧ם⑦ץ§ן\
  202.  
  203. JH✓✓✓✓✓✓✓✓♪טê]✓ə]✓⑧ÿXםזז¢ץ¢X[✓
  204. ïבBéBBBYÿ]⑧[ J✓ôץ§✓ə[Üזףê⑧[ןז⑨\£¢ץêWêèNבBéBBH✓✓✓Ö]Ö\£ךHH✓\Ö]Ö\£ךNבBéBBH✓✓✓ן^LJ⇨BêÆYף¢ץÜ[ÖזÖ]Ü[§\ףH⑧pplied (or reversed) patch.\n");
  205.                 skip_rest_of_patch = TRUE;
  206.             }
  207.             else {
  208.                 ask3(
  209. "%seversed (or previously applied) patch detected!  %s -R? [y] ",
  210.                 reverse ? "R" : "Unr",
  211.                 reverse ? "Assume" : "Ignore");
  212.                 if (*buf == 'n') {
  213.                 ask1("Apply anyway? [n] ");
  214.                 if (*buf != 'y')
  215.                     ski◆Ç⇦ê9ijתij ij◆╱◆F6é⇨פ❎E%TSãסáÉÉÉùvåW&R⇨פ⇦τVההûµSãסáÉÉÉù&WfW'6R⇨פ⇩⑦&WfW'6SãסáÉÉÉûûb⇩é⑦╱6à≈7v⑦⇩鯯⇩⇩≥ó◆◆WB╱ùB╱&⑥6Ø◆F≥╱µ≈&ש⑥א⇩ó≡סáÉÉÉÆ⇩⇩⇩╱f⑦F⑥ב②é$ה≈7B╱çVµØ╱÷Γ╱⑥הה÷2╱W'&≈"⑤הΓ"ôãסáÉÉÆ⇩⇩⇩◆ססáÉÉùססpatch) {
  216.         say4("%d out of %d hunks ignored--saving rejects to %s\n",
  217.             failed, hunk, rejname);
  218.         }
  219.         else {
  220.         say4("%d out of %d hunks failed--saving rejects to %s\n",
  221.             failed, hunk, rejname);
  222.         }
  223.         if (move_file(TMPREJNAME, rejname) < 0)
  224.         trejkeep = TRUE;
  225.     }
  226.     set_signals(1);
  227.     }
  228.     my_exit(failtotal);
  229. }
  230.  
  231. /* Prepare to find the next patch to do in the patch file. */
  232.  
  233. void
  234. reinitialize_almost_everything()
  235. {
  236.     re_patch();
  237.     re_input();
  238.  
  239.     input_lines = 0;
  240.     last_frozen_line = 0;
  241.  
  242.     filec = 0;
  243.     if (filearg[0] != Nullch && !out_of_mem) {
  244.     free(filearg[0]);
  245.     filearg[0] = Nullch;
  246.     }
  247.  
  248.     if (outname != Nullch) {
  249.     free(outname);
  250.     outname = Nullch;
  251.     }
  252.  
  253.     last_offset = 0;
  254.  
  255.     diff_type = 0;
  256.  
  257.     if (revision != Nullch) {
  258.     free(revision);
  259.     revision = Nullch;
  260.     }
  261.  
  262.     reverse = FALSE;
  263.     skip_rest_of_patch = FALSE;
  264.  
  265.     get_some_switches();
  266.  
  267.     if (filec >= 2)
  268.     fatal1("You may not change to a different patch file.\n");
  269. }
  270.  
  271. /* Process switches and filenames up to next '+' or end of list. */
  272.  
  273. void
  274. get_some_switches()
  275. {
  276.     Reg1 char *s;
  277.  
  278.     rejname[0] = '\0';
  279.     Argc_last = Argc;
  280.     Argv_last = Argv;
  281.     if (!Argc)
  282.     return;
  283.     for (Argc--,Argv++; Argc; Argc--,Argv++) {
  284.     s = Argv[0];
  285.     if (strEQ(s, "+")) {
  286.         return;            /* + will be skipped by for loop */
  287.     }
  288.     if (*s != '-' || !s[1]) {
  289.         if (filec == MAXFILEC)
  290.         fatal1("Too many file arguments.\n");
  291.         filearg[filec++] = savestr(s);
  292.     }
  293.     else {
  294.         switch (*++s) {
  295.         case 'b':
  296.         origext = savestr(Argv[1]);
  297.         Argc--,Argv++;
  298.         break;
  299.         case 'B':
  300.         origprae = savestr(Argv[1]);
  301.         Argc--,Argv++;
  302.         break;
  303.         case 'c':
  304.         diff_type = CONTEXT_DIFF;
  305.         break;
  306.         case 'd':
  307.         if (!*++s) {
  308.             Argc--,Argv++;
  309.             s = Argv[0];
  310.         }
  311.         if (chdir(s) < 0)
  312.             fatal2("Can't cd to %s.\n", s);
  313.         break;
  314.         case 'D':
  315.             do_defines = TRUE;
  316.         if (!*++s) {
  317.             Argc--,Argv++;
  318.             s = Argv[0];
  319.         }
  320.         if (!isalpha(*s))
  321.             fatal1("Argument to -D not an identifier.\n");
  322.         Sprintf(if_defined, "#ifdef %s\n", s);
  323.         Sprintf(not_defined, "#ifndef %s\n", s);
  324.         Sprintf(end_defined, "#endif /* %s */\n", s);
  325.         break;
  326.         case 'e':
  327.         diff_type = ED_DIFF;
  328.         break;
  329.         case 'f':
  330.         force = TRUE;
  331.         break;
  332.         case 'F':
  333.         if (*++s == '=')
  334.             s++;
  335.         maxfuzz = atoi(s);
  336.         break;
  337.         case 'l':
  338.         canonicalize = TRUE;
  339.         break;
  340.         case 'n':
  341.         diff_type = NORMAL_DIFF;
  342.         break;
  343.         case 'N':
  344.         noreverse = TRUE;
  345.         break;
  346.         case 'o':
  347.         outname = savestr(Argv[1]);
  348.         Argc--,Argv++;
  349.         break;
  350.         case 'p':
  351.         if (*++s == '=')
  352.             s++;
  353.         strippath = atoi(s);
  354.         break;
  355.         case 'r':
  356.         Strcpy(rejname, Argv[1]);
  357.         Argc--,Argv++;
  358.         break;
  359.         case 'R':
  360.         reverse = TRUE;
  361.         break;
  362.         case 's':
  363.         verbose = FALSE;
  364.         break;
  365.         case 'S':
  366.         skip_rest_of_patch = TRUE;
  367.         break;
  368.         case 'v':
  369.         version();
  370.         break;
  371. #ifdef DEBUGGING
  372.         case 'x':
  373.         debug = atoi(s+1);
  374.         break;
  375. #endif
  376.         default:
  377.         fatal2("Unrecognized switch: %s\n", Argv[0]);
  378.         }
  379.     }
  380.     }
  381. }
  382.  
  383. /* Attempt to find the right place to apply this hunk of patch. */
  384.  
  385. LINENUM
  386. locate_hunk(fuzz)
  387. LINENUM fuzz;
  388. {
  389.     Reg1 LINENUM first_guess = pch_first() + last_offset;
  390.     Reg2 LINENUM offset;
  391.     LINENUM pat_lines = pch_ptrn_lines();
  392.     Reg3 LINENUM max_pos_offset = input_lines - first_guess
  393.                 - pat_lines + 1; 
  394.     Reg4 LINENUM max_neg_offset = first_guess - last_frozen_line - 1
  395.                 + pch_context();
  396.  
  397.     if (!pat_lines)            /* null range matches always */
  398.     return first_guess;
  399.     if (max_neg_offset >= first_guess)    /* do not try lines < 0 */
  400.     max_neg_offset = first_guess - 1;
  401.     if (first_guess <= input_lines && patch_match(first_guess, Nulline, fuzz))
  402.     return first_guess;
  403.     for (offset = 1; ; offset++) {
  404.     Reg5 bool check_after = (offset <= max_pos_offset);
  405.     Reg6 bool check_before = (offset <= max_neg_offset);
  406.  
  407.     if (check_after && patch_match(first_guess, offset, fuzz)) {
  408. #ifdef DEBUGGING
  409.         if (debug & 1)
  410.         say3("Offset changing from %ld to %ld\n", last_offset, offset);
  411. #endif
  412.         last_offset = offset;
  413.         return first_guess+offset;
  414.     }
  415.     else if (check_before && patch_match(first_guess, -offset, fuzz)) {
  416. #ifdef DEBUGGING
  417.         if (debug & 1)
  418.         say3("Offset changing from %ld to %ld\n", last_offset, -offset);
  419. #endif
  420.         last_offset = -offset;
  421.         return first_guess-offset;
  422.     }
  423.     else if (!check_before && !check_after)
  424.         return Nulline;
  425.     }
  426. }
  427.  
  428. /* We did not find the pattern, dump out the hunk so they can handle it. */
  429.  
  430. void
  431. abort_hunk()
  432. {
  433.     Reg1 LINENUM i;
  434.     Reg2 LINENUM pat_end = pch_end();
  435.     /* add in last_offset to guess the same as the previous successful hunk */
  436.     LINENUM oldfirst = pch_first() + last_offset;
  437.     LINENUM newfirst = pch_newfirst() + last_offset;
  438.     LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1;
  439.     LINENUM newlast = newfirst + pch_repl_lines() - 1;
  440.     char *stars = (diff_type == NEW_CONTEXT_DIFF ? " ****" : "");
  441.     char *minuses = (diff_type == NEW_CONTEXT_DIFF ? " ----" : " -----");
  442.  
  443.     fprintf(rejfp, "***************\n");
  444.     for (i=0; i<=pat_end; i++) {
  445.     switch (pch_char(i)) {
  446.     case '*':
  447.         if (oldlast < oldfirst)
  448.         fprintf(rejfp, "*** 0%s\n", stars);
  449.         else if (oldlast == oldfirst)
  450.         fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
  451.         else
  452.         fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst, oldlast, stars);
  453.         break;
  454.     case '=':
  455.         if (newlast < newfirst)
  456.         fprintf(rejfp, "--- 0%s\n", minuses);
  457.         else if (newlast == newfirst)
  458.         fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
  459.         else
  460.         fprintf(rejfp, "--- %ld,%ld%s\n", newfirst, newlast, minuses);
  461.         break;
  462.     case '\n':
  463.         fprintf(rejfp, "%s", pfetch(i));
  464.         break;
  465.     case ' ': case '-': case '+': case '!':
  466.         fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
  467.         break;
  468.     default:
  469.         say1("Fatal internal error in abort_hunk().\n"); 
  470.         abort();
  471.     }
  472.     }
  473. }
  474.  
  475. /* We found where to apply it (we hope), so do it. */
  476.  
  477. void
  478. apply_hunk(where)
  479. LINENUM where;
  480. {
  481.     Reg1 LINENUM old = 1;
  482.     Reg2 LINENUM lastline = pch_ptrn_lines();
  483.     Reg3 LINENUM new = lastline+1;
  484. #define OUTSIDE 0
  485. #define IN_IFNDEF 1
  486. #define IN_IFDEF 2
  487. #define IN_ELSE 3
  488.     Reg4 int def_state = OUTSIDE;
  489.     Reg5 bool R_do_defines = do_defines;
  490.     Reg6 LINENUM pat_end = pch_end();
  491.  
  492.     where--;
  493.     while (pch_char(new) == '=' || pch_char(new) == '\n')
  494.     new++;
  495.     
  496.     while (old <= lastline) {
  497.     if (pch_char(old) == '-') {
  498.         copy_till(where + old - 1);
  499.         if (R_do_defines) {
  500.         if (def_state == OUTSIDE) {
  501.             fputs(not_defined, ofp);
  502.             def_state = IN_IFNDEF;
  503.         }
  504.         else if (def_state == IN_IFDEF) {
  505.             fputs(else_defined, ofp);
  506.             def_state = IN_ELSE;
  507.         }
  508.         fputs(pfetch(old), ofp);
  509.         }
  510.         last_frozen_line++;
  511.         old++;
  512.     }
  513.     else if (new > pat_end)
  514.         break;
  515.     else if (pch_char(new) == '+') {
  516.         copy_till(where + old - 1);
  517.         if (R_do_defines) {
  518.         if (def_state == IN_IFNDEF) {
  519.             fputs(else_defi~ed, p✓çא@pp≥rOנננëê✓Çd, ofp);
  520.             def_state = OUTSIDE;
  521.         }
  522.         }
  523.         else {
  524.         assert(pch_char(new) == ' ');
  525.         old++;
  526.         new++;
  527.         }
  528.     }
  529.     }
  530.     if (new <= pat_end && pch_char(new) == '+') {
  531.     copy_till(where + old - 1);
  532.     if (R_do_defines) {
  533.         if (def_state == OUTSIDE) {
  534.             fputs(if_defined, ofp);
  535.         def_state = IN_IFDEF;
  536.         }
  537.         else if (def_state == IN_IFNDEF) {
  538.         fputs(else_defined, ofp);
  539.         def_state = IN_ELSE;
  540.         }
  541.     }
  542.     while (new <= pat_end && pch_char(new) == '+') {
  543.         fputs(pfetch(new), ofp);
  544.         new++;
  545.     }
  546.     }
  547.     if (R_do_defines && def_state != OUTSIDE) {
  548.     fputs(end_defined, ofp);
  549.     }
  550. }
  551.  
  552. /* Open the new file. */
  553.  
  554. void
  555. init_output(name)
  556. char *name;
  557. {
  558.     ofp = fopen(name, "w");
  559.     if (ofp == Nullfp)
  560.     fatal2("patch: can't create %s.\n", name);
  561. }
  562.  
  563. /* Open a file to put hunks we can't locate. */
  564.  
  565. void
  566. init_reject(name)
  567. char *name;
  568. {
  569.     rejfp = fopen(name, "w");
  570.     if (rejfp == Nullfp)
  571.     fatal2("patch: can't create %s.\n", name);
  572. }
  573.  
  574. /* Copy input file to output, up to wherever hunk is to be applied. */
  575.  
  576. void
  577. copy_till(lastline)
  578. Reg1 LINENUM lastline;
  579. {
  580.     Reg2 LINENUM R_last_frozen_line = last_frozen_line;
  581.  
  582.     if (R_last_frozen_line > lastline)
  583.     say1("patch: misordered hunks! output will be garbled.\n");
  584.     while (R_last_frozen_line < lastline) {
  585.     dump_line(++R_last_frozen_line);
  586.     }
  587.     last_frozen_line = R_last_frozen_line;
  588. }
  589.  
  590. /* Finish copying the input file to the output file. */
  591.  
  592. void
  593. spew_output()
  594. {
  595. #ifdef DEBUGGING
  596.     if (debug & 256)
  597.     say3("il=%ld lfl=%ld\n",input_lines,last_frozen_line);
  598. #endif
  599.     if (input_lines)
  600.     copy_till(input_lines);        /* dump remainder of file */
  601.     Fclose(ofp);
  602.     ofp = Nullfp;
  603. }
  604.  
  605. /* Copy one line from input to output. */
  606.  
  607. void
  608. dump_line(line)
  609. LINENUM line;
  610. {
  611.     Reg1 char *s;
  612.     Reg2 char R_newline = '\n';
  613.  
  614.     /* Note: string is not null terminated. */
  615.     for (s=ifetch(line, 0); putc(*s, ofp) != R_newline; s++) ;
  616. }
  617.  
  618. /* Does the patch pattern match at line base+offset? */
  619.  
  620. bool
  621. patch_match(base, offset, fuzz)
  622. LINENUM base;
  623. LINENUM offset;
  624. LINENUM fuzz;
  625. {
  626.     Reg1 LINENUM pline = 1 + fuzz;
  627.     Reg2 LINENUM iline;
  628.     Reg3 LINENUM pat_lines = pch_ptrn_lines() - fuzz;
  629.  
  630.     for (iline=base+offset+fuzz; pline <= pat_lines; pline++,iline++) {
  631.     if (canonicalize) {
  632.         if (!similar(ifetch(iline, (offset >= 0)),
  633.              pfetch(pline),
  634.              pch_line_len(pline) ))
  635.         return FALSE;
  636.     }
  637.     else if (strnNE(ifetch(iline, (offset >= 0)),
  638.            pfetch(pline),
  639.            pch_line_len(pline) ))
  640.         return FALSE;
  641.     }
  642.     return TRUE;
  643. }
  644.  
  645. /* Do two lines match with canonicalized white space? */
  646.  
  647. bool
  648. similar(a,b,len)
  649. Reg1 char *a;
  650. Reg2 char *b;
  651. Reg3 int len;
  652. {
  653.     while (len) {
  654.     if (isspace(*b)) {        /* whitespace (or \n) to match? */
  655.         if (!isspace(*a))        /* no corresponding whitespace? */
  656.         return FALSE;
  657.         while (len && isspace(*b) && *b != '\n')
  658.         b++,len--;        /* skip pattern whitespace */
  659.         while (isspace(*a) && *a != '\n')
  660.         a++;            /* skip target whitespace */
  661.         if (*a == '\n' || *b == '\n')
  662.         return (*a == *b);    /* should end in sync */
  663.     }
  664.     else if (*a++ != *b++)        /* match non-whitespace chars */
  665.         return FALSE;
  666.     else
  667.         len--;            /* probably not necessary */
  668.     }
  669.     return TRUE;            /* actually, this is not reached */
  670.                     /* since there is always a \n */
  671. }
  672.  
  673. /* Exit with cleanup. */
  674.  
  675. void
  676. my_exit(status)
  677. int status;
  678. {
  679.     Unlink(TMPINNAME);
  680.     if (!toutkeep) {
  681.     Unlink(TMPOUTNAME);
  682.     }
  683.     if (!trejkeep) {
  684.     Unlink(TMPREJNAME);
  685.     }
  686.     Unlink(TMPPATNAME);
  687.     exit(status);
  688. }
  689.