home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / utils / hwgrcs / hwgdiff / rcs.rcsfiles / diff3.c,v < prev    next >
Encoding:
Text File  |  1993-02-20  |  52.0 KB  |  2,044 lines

  1. head    1.2;
  2. access;
  3. symbols
  4.     HWGDIFF_Fish:1.2
  5.     HWGDIFF:1.2;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 1.2
  11. date    93.01.19.14.32.45;    author heinz;    state Exp;
  12. branches;
  13. next    1.1;
  14.  
  15. 1.1
  16. date    93.01.19.14.04.33;    author heinz;    state Exp;
  17. branches;
  18. next    ;
  19.  
  20.  
  21. desc
  22. @RCS for the first time ...
  23. @
  24.  
  25.  
  26. 1.2
  27. log
  28. @Amiga port
  29. @
  30. text
  31. @/* Three-way file comparison program (diff3) for Project GNU
  32.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  33.  
  34.    This program is free software; you can redistribute it and/or modify
  35.    it under the terms of the GNU General Public License as published by
  36.    the Free Software Foundation; either version 1, or (at your option)
  37.    any later version.
  38.  
  39.    This program is distributed in the hope that it will be useful,
  40.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42.    GNU General Public License for more details.
  43.  
  44.    You should have received a copy of the GNU General Public License
  45.    along with this program; if not, write to the Free Software
  46.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  47.  
  48.  
  49. /* Written by Randy Smith */
  50.  
  51. #ifdef __STDC__
  52. #define VOID void
  53. #else
  54. #define VOID char
  55. #endif
  56.  
  57. /*
  58.  * Include files.
  59.  */
  60. #include <stdio.h>
  61. #include <ctype.h>
  62. #include <sys/types.h>
  63. #include <sys/stat.h>
  64.  
  65. #ifdef AMIGA
  66. #include <fcntl.h>
  67. #include <string.h>
  68. #include <stdlib.h>
  69. /* This is a good place for this! */
  70. char HWGversion[] = "\0$VER: GNUDIFF 1.15 (19.1.93) Changes (C)1993 by HWG, For Joan";
  71.  
  72. #else
  73. #ifdef USG
  74. #include <fcntl.h>
  75.  
  76. /* Define needed BSD functions in terms of sysV library.  */
  77.  
  78. #define bcmp(s1,s2,n)   memcmp((s1),(s2),(n))
  79. #define bzero(s,n)      memset((s),0,(n))
  80.  
  81. #ifndef XENIX
  82. #define dup2(f,t)       (close(t),fcntl((f),F_DUPFD,(t)))
  83. #endif
  84.  
  85. #define vfork    fork
  86.  
  87. #else /* not USG */
  88. #include <sys/wait.h>
  89. #endif /* not USG */
  90. #endif
  91.  
  92. #ifndef WEXITSTATUS
  93. #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  94. #undef WIFEXITED /* Avoid 4.3BSD incompatibility with Posix.  */
  95. #endif
  96. #ifndef WIFEXITED
  97. #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  98. #endif
  99.  
  100. #ifdef sparc
  101. /* vfork clobbers registers on the Sparc, so don't use it.  */
  102. #define vfork fork
  103. #endif
  104.  
  105. /*
  106.  * Internal data structures and macros for the diff3 program; includes
  107.  * data structures for both diff3 diffs and normal diffs.
  108.  */
  109.  
  110. /*
  111.  * Different files within a diff
  112.  */
  113. #define FILE0    0
  114. #define FILE1    1
  115. #define FILE2    2
  116.  
  117. /*
  118.  * Three way diffs are build out of two two-way diffs; the file which
  119.  * the two two-way diffs share is:
  120.  */
  121. #define FILEC    FILE0
  122.  
  123. /* The ranges are indexed by */
  124. #define START    0
  125. #define END    1
  126.  
  127. enum diff_type {
  128.   ERROR,            /* Should not be used */
  129.   ADD,                /* Two way diff add */
  130.   CHANGE,            /* Two way diff change */
  131.   DELETE,            /* Two way diff delete */
  132.   DIFF_ALL,            /* All three are different */
  133.   DIFF_1ST,            /* Only the first is different */
  134.   DIFF_2ND,            /* Only the second */
  135.   DIFF_3RD            /* Only the third */
  136. };
  137.  
  138. /* Two-way diff */
  139. struct diff_block {
  140.   int ranges[2][2];        /* Ranges are inclusive */
  141.   char **lines[2];        /* The actual lines (may contain nulls) */
  142.   int *lengths[2];        /* Line lengths (including newlines, if any) */
  143.   struct diff_block *next;
  144. };
  145.  
  146. /* Three-way diff */
  147.  
  148. struct diff3_block {
  149.   enum diff_type correspond;    /* Type of diff */
  150.   int ranges[3][2];        /* Ranges are inclusive */
  151.   char **lines[3];        /* The actual lines (may contain nulls) */
  152.   int *lengths[3];        /* Line lengths (including newlines, if any) */
  153.   struct diff3_block *next;
  154. };
  155.  
  156. /*
  157.  * Access the ranges on a diff block.
  158.  */
  159. #define D_LOWLINE(diff, filenum)        \
  160.   ((diff)->ranges[filenum][START])
  161. #define D_HIGHLINE(diff, filenum)       \
  162.   ((diff)->ranges[filenum][END])
  163. #define D_NUMLINES(diff, filenum)       \
  164.   (D_HIGHLINE((diff), (filenum)) - D_LOWLINE((diff), (filenum)) + 1)
  165.  
  166. /*
  167.  * Access the line numbers in a file in a diff by relative line
  168.  * numbers (i.e. line number within the diff itself).  Note that these
  169.  * are lvalues and can be used for assignment.
  170.  */
  171. #define D_RELNUM(diff, filenum, linenum)        \
  172.   (*((diff)->lines[filenum] + linenum))
  173. #define D_RELLEN(diff, filenum, linenum)        \
  174.   (*((diff)->lengths[filenum] + linenum))
  175.  
  176. /*
  177.  * And get at them directly, when that should be necessary.
  178.  */
  179. #define D_LINEARRAY(diff, filenum)      \
  180.   ((diff)->lines[filenum])
  181. #define D_LENARRAY(diff, filenum)       \
  182.   ((diff)->lengths[filenum])
  183.  
  184. /*
  185.  * Next block.
  186.  */
  187. #define D_NEXT(diff)    ((diff)->next)
  188.  
  189. /*
  190.  * Access the type of a diff3 block.
  191.  */
  192. #define D3_TYPE(diff)   ((diff)->correspond)
  193.  
  194. /*
  195.  * Line mappings based on diffs.  The first maps off the top of the
  196.  * diff, the second off of the bottom.
  197.  */
  198. #define D_HIGH_MAPLINE(diff, fromfile, tofile, lineno)  \
  199.   ((lineno)                                             \
  200.    - D_HIGHLINE ((diff), (fromfile))                    \
  201.    + D_HIGHLINE ((diff), (tofile)))
  202.  
  203. #define D_LOW_MAPLINE(diff, fromfile, tofile, lineno)   \
  204.   ((lineno)                                             \
  205.    - D_LOWLINE ((diff), (fromfile))                     \
  206.    + D_LOWLINE ((diff), (tofile)))
  207.  
  208. /*
  209.  * General memory allocation function.
  210.  */
  211. #define ALLOCATE(number, type)  \
  212.   (type *) xmalloc ((number) * sizeof (type))
  213.  
  214. /*
  215.  * Options variables for flags set on command line.
  216.  *
  217.  * ALWAYS_TEXT: Treat all files as text files; never treat as binary.
  218.  *
  219.  * EDSCRIPT: Write out an ed script instead of the standard diff3 format.
  220.  *
  221.  * FLAGGING: Indicates that in the case of overlapping diffs (type
  222.  * DIFF_ALL), the lines which would normally be deleted from file 1
  223.  * should be preserved with a special flagging mechanism.
  224.  *
  225.  * DONT_WRITE_OVERLAP: 1 if information for overlapping diffs should
  226.  * not be output.
  227.  *
  228.  * DONT_WRITE_SIMPLE: 1 if information for non-overlapping diffs
  229.  * should not be output.
  230.  *
  231.  * FINALWRITE: 1 if a :wq should be included at the end of the script
  232.  * to write out the file being edited.
  233.  *
  234.  * MERGE: output a merged file.
  235.  */
  236. int always_text;
  237. int edscript;
  238. int flagging;
  239. int dont_write_overlap;
  240. int dont_write_simple;
  241. int finalwrite;
  242. int merge;
  243.  
  244. extern int optind;
  245.  
  246. char *argv0;
  247.  
  248. /*
  249.  * Forward function declarations.
  250.  */
  251. struct diff_block *process_diff ();
  252. struct diff3_block *make_3way_diff ();
  253. void output_diff3 ();
  254. int output_diff3_edscript ();
  255. int output_diff3_merge ();
  256. void usage ();
  257.  
  258. struct diff3_block *using_to_diff3_block ();
  259. int copy_stringlist ();
  260. struct diff3_block *create_diff3_block ();
  261. int compare_line_list ();
  262.  
  263. char *read_diff ();
  264. enum diff_type process_diff_control ();
  265. char *scan_diff_line ();
  266.  
  267. struct diff3_block *reverse_diff3_blocklist ();
  268.  
  269. VOID *xmalloc ();
  270. VOID *xrealloc ();
  271.  
  272. char diff_program[] = DIFF_PROGRAM;
  273.  
  274. /*
  275.  * Main program.  Calls diff twice on two pairs of input files,
  276.  * combines the two diffs, and outputs them.
  277.  */
  278. main (argc, argv)
  279.      int argc;
  280.      char **argv;
  281. {
  282.   int c, i;
  283.   int mapping[3];
  284.   int rev_mapping[3];
  285.   int incompat;
  286.   int overlaps_found;
  287.   struct diff_block *thread1, *thread2;
  288.   struct diff3_block *diff;
  289.   int tag_count = 0;
  290.   /* Element 0 is for file 0, element 1 is for file 2.    */
  291.   char *tag_strings[2];
  292.   extern char *optarg;
  293.   char *commonname;
  294.   struct stat statb;
  295.  
  296.   incompat = 0;
  297.   tag_strings[0] = tag_strings[1] = 0;
  298.  
  299.   argv0 = argv[0];
  300.  
  301.   while ((c = getopt (argc, argv, "aeimx3EXL:")) != EOF)
  302.     {
  303.       switch (c)
  304.     {
  305.     case 'a':
  306.       always_text = 1;
  307.       break;
  308.     case 'x':
  309.       dont_write_simple = 1;
  310.       incompat++;
  311.       break;
  312.     case '3':
  313.       dont_write_overlap = 1;
  314.       incompat++;
  315.       break;
  316.     case 'i':
  317.       finalwrite = 1;
  318.       break;
  319.     case 'm':
  320.       merge = 1;
  321.       break;
  322.     case 'X':
  323.       dont_write_simple = 1;
  324.       /* Falls through */
  325.     case 'E':
  326.       flagging = 1;
  327.       /* Falls through */
  328.     case 'e':
  329.       incompat++;
  330.       break;
  331.     case 'L':
  332.       /* Handle one or two -L arguments.  */
  333.       if (tag_count < 2)
  334.         {
  335.           tag_strings[tag_count++] = optarg;
  336.           break;
  337.         }
  338.       /* Falls through */
  339.     case '?':
  340.     default:
  341.       usage ();
  342.       /* NOTREACHED */
  343.     }
  344.     }
  345.  
  346.   edscript = incompat & ~merge;  /* -eExX3 without -m implies ed script.  */
  347.   flagging |= ~incompat & merge;  /* -m without -eExX3 implies -E.  */
  348.  
  349.   if (incompat > 1  /* Ensure at most one of -eExX3.  */
  350.       || finalwrite & (~incompat | merge)
  351.         /* -i needs one of -eExX3; -i -m would rewrite input file.    */
  352.       || tag_count && ! flagging /* -L requires one of -EX.  */
  353.       || argc - optind != 3)
  354.     usage ();
  355.  
  356.   if (tag_strings[0] == 0)
  357.     tag_strings[0] = argv[optind];
  358.   if (tag_strings[1] == 0)
  359.     tag_strings[1] = argv[optind + 2];
  360.  
  361.   if (*argv[optind] == '-' && *(argv[optind] + 1) == '\0')
  362.     {
  363.       /* Sigh.    We've got standard input as the first arg. We can't */
  364.       /* call diff twice on stdin */
  365.       if (! strcmp (argv[optind + 1], "-") || ! strcmp (argv[optind + 2], "-"))
  366.     fatal ("`-' specified for more than one input file");
  367.       mapping[0] = 1;
  368.       mapping[1] = 2;
  369.       mapping[2] = 0;
  370.       rev_mapping[1] = 0;
  371.       rev_mapping[2] = 1;
  372.       rev_mapping[0] = 2;
  373.     }
  374.   else
  375.     {
  376.       /* Normal, what you'd expect */
  377.       mapping[0] = 0;
  378.       mapping[1] = 1;
  379.       mapping[2] = 2;
  380.       rev_mapping[0] = 0;
  381.       rev_mapping[1] = 1;
  382.       rev_mapping[2] = 2;
  383.     }
  384.  
  385.   for (i = 0; i < 3; i++)
  386.     if (argv[optind + i][0] != '-' || argv[optind + i][1] != '\0')
  387.       if (stat (argv[optind + i], &statb) < 0)
  388.     perror_with_exit (argv[optind + i]);
  389.       else if ((statb.st_mode & S_IFMT) == S_IFDIR)
  390.     {
  391.       fprintf (stderr, "%s: %s: Is a directory\n", argv0,
  392.            argv[optind + i]);
  393.       exit (2);
  394.     }
  395.  
  396.  
  397.   commonname = argv[optind + rev_mapping[0]];
  398.   thread1 = process_diff (commonname, argv[optind + rev_mapping[1]]);
  399.   thread2 = process_diff (commonname, argv[optind + rev_mapping[2]]);
  400.   diff = make_3way_diff (thread1, thread2);
  401.   if (edscript)
  402.     overlaps_found
  403.       = output_diff3_edscript (stdout, diff, mapping, rev_mapping,
  404.                    tag_strings[0], argv[optind+1], tag_strings[1]);
  405.   else if (merge)
  406.     {
  407.       if (! freopen (commonname, "r", stdin))
  408.     perror_with_exit (commonname);
  409.       overlaps_found
  410.     = output_diff3_merge (stdin, stdout, diff, mapping, rev_mapping,
  411.                   tag_strings[0], argv[optind+1], tag_strings[1]);
  412.       if (ferror (stdin))
  413.     fatal ("read error");
  414.     }
  415.   else
  416.     {
  417.       output_diff3 (stdout, diff, mapping, rev_mapping);
  418.       overlaps_found = 0;
  419.     }
  420.  
  421.   if (ferror (stdout) || fflush (stdout) != 0)
  422.     fatal ("write error");
  423.   exit (overlaps_found);
  424. }
  425.  
  426. /*
  427.  * Explain, patiently and kindly, how to use this program.  Then exit.
  428.  */
  429. void
  430. usage ()
  431. {
  432.   fprintf (stderr, "Usage:\t%s [-exEX3 [-i | -m] [-L label1 -L label3]] file1 file2 file3\n",
  433.        argv0);
  434.   fprintf (stderr, "\tOnly one of [exEX3] allowed\n");
  435.   exit (2);
  436. }
  437.  
  438. /*
  439.  * Routines that combine the two diffs together into one.  The
  440.  * algorithm used follows:
  441.  *
  442.  *   File0 is shared in common between the two diffs.
  443.  *   Diff01 is the diff between 0 and 1.
  444.  *   Diff02 is the diff between 0 and 2.
  445.  *
  446.  *    1) Find the range for the first block in File0.
  447.  *        a) Take the lowest of the two ranges (in File0) in the two
  448.  *           current blocks (one from each diff) as being the low
  449.  *           water mark.  Assign the upper end of this block as
  450.  *           being the high water mark and move the current block up
  451.  *           one.  Mark the block just moved over as to be used.
  452.  *        b) Check the next block in the diff that the high water
  453.  *           mark is *not* from.
  454.  *
  455.  *           *If* the high water mark is above
  456.  *           the low end of the range in that block,
  457.  *
  458.  *           mark that block as to be used and move the current
  459.  *           block up.  Set the high water mark to the max of
  460.  *           the high end of this block and the current.    Repeat b.
  461.  *
  462.  *     2) Find the corresponding ranges in Files1 (from the blocks
  463.  *        in diff01; line per line outside of diffs) and in File2.
  464.  *        Create a diff3_block, reserving space as indicated by the ranges.
  465.  *
  466.  *     3) Copy all of the pointers for file0 in.  At least for now,
  467.  *        do bcmp's between corresponding strings in the two diffs.
  468.  *
  469.  *     4) Copy all of the pointers for file1 and 2 in.  Get what you
  470.  *        need from file0 (when there isn't a diff block, it's
  471.  *        identical to file0 within the range between diff blocks).
  472.  *
  473.  *     5) If the diff blocks you used came from only one of the two
  474.  *        strings of diffs, then that file (i.e. the one other than
  475.  *        file 0 in that diff) is the odd person out.  If you used
  476.  *        diff blocks from both sets, check to see if files 1 and 2 match:
  477.  *
  478.  *        Same number of lines?  If so, do a set of bcmp's (if a
  479.  *        bcmp matches; copy the pointer over; it'll be easier later
  480.  *        if you have to do any compares).  If they match, 1 & 2 are
  481.  *        the same.  If not, all three different.
  482.  *
  483.  *   Then you do it again, until you run out of blocks.
  484.  *
  485.  */
  486.  
  487. /*
  488.  * This routine makes a three way diff (chain of diff3_block's) from two
  489.  * two way diffs (chains of diff_block's).  It is assumed that each of
  490.  * the two diffs passed are off of the same file (i.e. that each of the
  491.  * diffs were made "from" the same file).  The three way diff pointer
  492.  * returned will have numbering 0--the common file, 1--the other file
  493.  * in diff1, and 2--the other file in diff2.
  494.  */
  495. struct diff3_block *
  496. make_3way_diff (thread1, thread2)
  497.      struct diff_block *thread1, *thread2;
  498. {
  499. /*
  500.  * This routine works on the two diffs passed to it as threads.
  501.  * Thread number 0 is diff1, thread number 1 is diff2.    The USING
  502.  * array is set to the base of the list of blocks to be used to
  503.  * construct each block of the three way diff; if no blocks from a
  504.  * particular thread are to be used, that element of the using array
  505.  * is set to 0.  The elements LAST_USING array are set to the last
  506.  * elements on each of the using lists.
  507.  *
  508.  * The HIGH_WATER_MARK is set to the highest line number in File 0
  509.  * described in any of the diffs in either of the USING lists.    The
  510.  * HIGH_WATER_THREAD names the thread.    Similarly the BASE_WATER_MARK
  511.  * and BASE_WATER_THREAD describe the lowest line number in File 0
  512.  * described in any of the diffs in either of the USING lists.    The
  513.  * HIGH_WATER_DIFF is the diff from which the HIGH_WATER_MARK was
  514.  * taken.
  515.  *
  516.  * The HIGH_WATER_DIFF should always be equal to LAST_USING
  517.  * [HIGH_WATER_THREAD].  The OTHER_DIFF is the next diff to check for
  518.  * higher water, and should always be equal to
  519.  * CURRENT[HIGH_WATER_THREAD ^ 0x1].  The OTHER_THREAD is the thread
  520.  * in which the OTHER_DIFF is, and hence should always be equal to
  521.  * HIGH_WATER_THREAD ^ 0x1.
  522.  *
  523.  * The variable LAST_DIFF is kept set to the last diff block produced
  524.  * by this routine, for line correspondence purposes between that diff
  525.  * and the one currently being worked on.  It is initialized to
  526.  * ZERO_DIFF before any blocks have been created.
  527.  */
  528.  
  529.   struct diff_block
  530.     *using[2],
  531.     *last_using[2],
  532.     *current[2];
  533.  
  534.   int
  535.     high_water_mark;
  536.  
  537.   int
  538.     high_water_thread,
  539.     base_water_thread,
  540.     other_thread;
  541.  
  542.   struct diff_block
  543.     *high_water_diff,
  544.     *other_diff;
  545.  
  546.   struct diff3_block
  547.     *result,
  548.     *tmpblock,
  549.     *result_last,
  550.     *last_diff;
  551.  
  552.   static struct diff3_block zero_diff = {
  553.       ERROR,
  554.       { {0, 0}, {0, 0}, {0, 0} },
  555.       { (char **) 0, (char **) 0, (char **) 0 },
  556.       { (int *) 0, (int *) 0, (int *) 0 },
  557.       (struct diff3_block *) 0
  558.       };
  559.  
  560.   /* Initialization */
  561.   result = result_last = (struct diff3_block *) 0;
  562.   current[0] = thread1; current[1] = thread2;
  563.   last_diff = &zero_diff;
  564.  
  565.   /* Sniff up the threads until we reach the end */
  566.  
  567.   while (current[0] || current[1])
  568.     {
  569.       using[0] = using[1] = last_using[0] = last_using[1] =
  570.     (struct diff_block *) 0;
  571.  
  572.       /* Setup low and high water threads, diffs, and marks.  */
  573.       if (!current[0])
  574.     base_water_thread = 1;
  575.       else if (!current[1])
  576.     base_water_thread = 0;
  577.       else
  578.     base_water_thread =
  579.       (D_LOWLINE (current[0], FILE0) > D_LOWLINE (current[1], FILE0));
  580.  
  581.       high_water_thread = base_water_thread;
  582.  
  583.       high_water_diff = current[high_water_thread];
  584.  
  585. #if 0
  586.       /* low and high waters start off same diff */
  587.       base_water_mark = D_LOWLINE (high_water_diff, FILE0);
  588. #endif
  589.  
  590.       high_water_mark = D_HIGHLINE (high_water_diff, FILE0);
  591.  
  592.       /* Make the diff you just got info from into the using class */
  593.       using[high_water_thread]
  594.     = last_using[high_water_thread]
  595.     = high_water_diff;
  596.       current[high_water_thread] = high_water_diff->next;
  597.       last_using[high_water_thread]->next
  598.     = (struct diff_block *) 0;
  599.  
  600.       /* And mark the other diff */
  601.       other_thread = high_water_thread ^ 0x1;
  602.       other_diff = current[other_thread];
  603.  
  604.       /* Shuffle up the ladder, checking the other diff to see if it
  605.      needs to be incorporated */
  606.       while (other_diff
  607.          && D_LOWLINE (other_diff, FILE0) <= high_water_mark + 1)
  608.     {
  609.  
  610.       /* Incorporate this diff into the using list.  Note that
  611.          this doesn't take it off the current list */
  612.       if (using[other_thread])
  613.         last_using[other_thread]->next = other_diff;
  614.       else
  615.         using[other_thread] = other_diff;
  616.       last_using[other_thread] = other_diff;
  617.  
  618.       /* Take it off the current list.  Note that this following
  619.          code assumes that other_diff enters it equal to
  620.          current[high_water_thread ^ 0x1] */
  621.       current[other_thread]
  622.         = current[other_thread]->next;
  623.       other_diff->next
  624.         = (struct diff_block *) 0;
  625.  
  626.       /* Set the high_water stuff
  627.          If this comparison is equal, then this is the last pass
  628.          through this loop; since diff blocks within a given
  629.          thread cannot overlap, the high_water_mark will be
  630.          *below* the range_start of either of the next diffs. */
  631.  
  632.       if (high_water_mark < D_HIGHLINE (other_diff, FILE0))
  633.         {
  634.           high_water_thread ^= 1;
  635.           high_water_diff = other_diff;
  636.           high_water_mark = D_HIGHLINE (other_diff, FILE0);
  637.         }
  638.  
  639.       /* Set the other diff */
  640.       other_thread = high_water_thread ^ 0x1;
  641.       other_diff = current[other_thread];
  642.     }
  643.  
  644.       /* The using lists contain a list of all of the blocks to be
  645.      included in this diff3_block.    Create it.  */
  646.  
  647.       tmpblock = using_to_diff3_block (using, last_using,
  648.                        base_water_thread, high_water_thread,
  649.                        last_diff);
  650.  
  651.       if (!tmpblock)
  652.     fatal ("internal: screwup in format of diff blocks");
  653.  
  654.       /* Put it on the list */
  655.       if (result)
  656.     result_last->next = tmpblock;
  657.       else
  658.     result = tmpblock;
  659.       result_last = tmpblock;
  660.  
  661.       /* Setup corresponding lines correctly */
  662.       last_diff = tmpblock;
  663.     }
  664.   return result;
  665. }
  666.  
  667. /*
  668.  * using_to_diff3_block:
  669.  *   This routine takes two lists of blocks (from two separate diff
  670.  * threads) and puts them together into one diff3 block.
  671.  * It then returns a pointer to this diff3 block or 0 for failure.
  672.  *
  673.  * All arguments besides using are for the convenience of the routine;
  674.  * they could be derived from the using array.
  675.  * LAST_USING is a pair of pointers to the last blocks in the using
  676.  * structure.
  677.  * LOW_THREAD and HIGH_THREAD tell which threads contain the lowest
  678.  * and highest line numbers for File0.
  679.  * last_diff contains the last diff produced in the calling routine.
  680.  * This is used for lines mappings which would still be identical to
  681.  * the state that diff ended in.
  682.  *
  683.  * A distinction should be made in this routine between the two diffs
  684.  * that are part of a normal two diff block, and the three diffs that
  685.  * are part of a diff3_block.
  686.  */
  687. struct diff3_block *
  688. using_to_diff3_block (using, last_using, low_thread, high_thread, last_diff)
  689.      struct diff_block
  690.        *using[2],
  691.        *last_using[2];
  692.      int low_thread, high_thread;
  693.      struct diff3_block *last_diff;
  694. {
  695.   int lowc, highc, low1, high1, low2, high2;
  696.   struct diff3_block *result;
  697.   struct diff_block *ptr;
  698.   int i;
  699.   int current0line;
  700.  
  701.   /* Find the range in file0 */
  702.   lowc = using[low_thread]->ranges[0][START];
  703.   highc = last_using[high_thread]->ranges[0][END];
  704.  
  705.   /* Find the ranges in the other files.
  706.      If using[x] is null, that means that the file to which that diff
  707.      refers is equivalent to file 0 over this range */
  708.  
  709.   if (using[0])
  710.     {
  711.       low1 = D_LOW_MAPLINE (using[0], FILE0, FILE1, lowc);
  712.       high1 = D_HIGH_MAPLINE (last_using[0], FILE0, FILE1, highc);
  713.     }
  714.   else
  715.     {
  716.       low1 = D_HIGH_MAPLINE (last_diff, FILEC, FILE1, lowc);
  717.       high1 = D_HIGH_MAPLINE (last_diff, FILEC, FILE1, highc);
  718.     }
  719.  
  720.   /*
  721.    * Note that in the following, we use file 1 relative to the diff,
  722.    * and file 2 relative to the corresponding lines struct.
  723.    */
  724.   if (using[1])
  725.     {
  726.       low2 = D_LOW_MAPLINE (using[1], FILE0, FILE1, lowc);
  727.       high2 = D_HIGH_MAPLINE (last_using[1], FILE0, FILE1, highc);
  728.     }
  729.   else
  730.     {
  731.       low2 = D_HIGH_MAPLINE (last_diff, FILEC, FILE2, lowc);
  732.       high2 = D_HIGH_MAPLINE (last_diff, FILEC, FILE2, highc);
  733.     }
  734.  
  735.   /* Create a block with the appropriate sizes */
  736.   result = create_diff3_block (lowc, highc, low1, high1, low2, high2);
  737.  
  738.   /* Copy over all of the information for File 0.  Return with a zero
  739.      if any of the compares failed. */
  740.   for (ptr = using[0]; ptr; ptr = D_NEXT (ptr))
  741.     {
  742.       int result_offset = D_LOWLINE (ptr, FILE0) - lowc;
  743.       int copy_size
  744.     = D_HIGHLINE (ptr, FILE0) - D_LOWLINE (ptr, FILE0) + 1;
  745.  
  746.       if (!copy_stringlist (D_LINEARRAY (ptr, FILE0),
  747.                 D_LENARRAY (ptr, FILE0),
  748.                 D_LINEARRAY (result, FILEC) + result_offset,
  749.                 D_LENARRAY (result, FILEC) + result_offset,
  750.                 copy_size))
  751.     return 0;
  752.     }
  753.  
  754.   for (ptr = using[1]; ptr; ptr = D_NEXT (ptr))
  755.     {
  756.       int result_offset = D_LOWLINE (ptr, FILEC) - lowc;
  757.       int copy_size
  758.     = D_HIGHLINE (ptr, FILEC) - D_LOWLINE (ptr, FILEC) + 1;
  759.  
  760.       if (!copy_stringlist (D_LINEARRAY (ptr, FILE0),
  761.                 D_LENARRAY (ptr, FILE0),
  762.                 D_LINEARRAY (result, FILEC) + result_offset,
  763.                 D_LENARRAY (result, FILEC) + result_offset,
  764.                 copy_size))
  765.     return 0;
  766.     }
  767.  
  768.   /* Copy stuff for file 1.  First deal with anything that might be
  769.      before the first diff. */
  770.  
  771.   for (i = 0;
  772.        i + low1 < (using[0] ? D_LOWLINE (using[0], FILE1) : high1 + 1);
  773.        i++)
  774.     {
  775.       D_RELNUM (result, FILE1, i) = D_RELNUM (result, FILEC, i);
  776.       D_RELLEN (result, FILE1, i) = D_RELLEN (result, FILEC, i);
  777.     }
  778.  
  779.   for (ptr = using[0]; ptr; ptr = D_NEXT (ptr))
  780.     {
  781.       int result_offset = D_LOWLINE (ptr, FILE1) - low1;
  782.       int copy_size
  783.     = D_HIGHLINE (ptr, FILE1) - D_LOWLINE (ptr, FILE1) + 1;
  784.  
  785.       if (!copy_stringlist (D_LINEARRAY (ptr, FILE1),
  786.                 D_LENARRAY (ptr, FILE1),
  787.                 D_LINEARRAY (result, FILE1) + result_offset,
  788.                 D_LENARRAY (result, FILE1) + result_offset,
  789.                 copy_size))
  790.     return 0;
  791.  
  792.       /* Catch the lines between here and the next diff */
  793.       current0line = D_HIGHLINE (ptr, FILE0) + 1 - lowc;
  794.       for (i = D_HIGHLINE (ptr, FILE1) + 1 - low1;
  795.        i < (D_NEXT (ptr) ?
  796.         D_LOWLINE (D_NEXT (ptr), FILE1) :
  797.         high1 + 1) - low1;
  798.        i++)
  799.     {
  800.       D_RELNUM (result, FILE1, i)
  801.         = D_RELNUM (result, FILEC, current0line);
  802.       D_RELLEN (result, FILE1, i)
  803.         = D_RELLEN (result, FILEC, current0line++);
  804.     }
  805.     }
  806.  
  807.   /* Copy stuff for file 2.  First deal with anything that might be
  808.      before the first diff. */
  809.  
  810.   for (i = 0;
  811.        i + low2 < (using[1] ? D_LOWLINE (using[1], FILE1) : high2 + 1);
  812.        i++)
  813.     {
  814.       D_RELNUM (result, FILE2, i) = D_RELNUM (result, FILEC, i);
  815.       D_RELLEN (result, FILE2, i) = D_RELLEN (result, FILEC, i);
  816.     }
  817.  
  818.   for (ptr = using[1]; ptr; ptr = D_NEXT (ptr))
  819.     {
  820.       int result_offset = D_LOWLINE (ptr, FILE1) - low2;
  821.       int copy_size
  822.     = D_HIGHLINE (ptr, FILE1) - D_LOWLINE (ptr, FILE1) + 1;
  823.  
  824.       if (!copy_stringlist (D_LINEARRAY (ptr, FILE1),
  825.                 D_LENARRAY (ptr, FILE1),
  826.                 D_LINEARRAY (result, FILE2) + result_offset,
  827.                 D_LENARRAY (result, FILE2) + result_offset,
  828.                 copy_size))
  829.     return 0;
  830.  
  831.       /* Catch the lines between here and the next diff */
  832.       current0line = D_HIGHLINE (ptr, FILE0) + 1 - lowc;
  833.       for (i = D_HIGHLINE (ptr, FILE1) + 1 - low2;
  834.        i < (D_NEXT (ptr) ?
  835.         D_LOWLINE (D_NEXT (ptr), FILE1) :
  836.         high2 + 1) - low2;
  837.        i++)
  838.     {
  839.       D_RELNUM (result, FILE2, i)
  840.         = D_RELNUM (result, FILEC, current0line);
  841.       D_RELLEN (result, FILE2, i)
  842.         = D_RELLEN (result, FILEC, current0line++);
  843.     }
  844.     }
  845.  
  846.   /* Set correspond */
  847.   if (!using[0])
  848.     D3_TYPE (result) = DIFF_3RD;
  849.   else if (!using[1])
  850.     D3_TYPE (result) = DIFF_2ND;
  851.   else
  852.     {
  853.       int nl1
  854.     = D_HIGHLINE (result, FILE1) - D_LOWLINE (result, FILE1) + 1;
  855.       int nl2
  856.     = D_HIGHLINE (result, FILE2) - D_LOWLINE (result, FILE2) + 1;
  857.  
  858.       if (nl1 != nl2
  859.       || !compare_line_list (D_LINEARRAY (result, FILE1),
  860.                  D_LENARRAY (result, FILE1),
  861.                  D_LINEARRAY (result, FILE2),
  862.                  D_LENARRAY (result, FILE2),
  863.                  nl1))
  864.     D3_TYPE (result) = DIFF_ALL;
  865.       else
  866.     D3_TYPE (result) = DIFF_1ST;
  867.     }
  868.  
  869.   return result;
  870. }
  871.  
  872. /*
  873.  * This routine copies pointers from a list of strings to a different list
  874.  * of strings.    If a spot in the second list is already filled, it
  875.  * makes sure that it is filled with the same string; if not it
  876.  * returns 0, the copy incomplete.
  877.  * Upon successful completion of the copy, it returns 1.
  878.  */
  879. int
  880. copy_stringlist (fromptrs, fromlengths, toptrs, tolengths, copynum)
  881.      char *fromptrs[], *toptrs[];
  882.      int *fromlengths, *tolengths;
  883.      int copynum;
  884. {
  885.   register char
  886.     **f = fromptrs,
  887.     **t = toptrs;
  888.   register int
  889.     *fl = fromlengths,
  890.     *tl = tolengths;
  891.  
  892.   while (copynum--)
  893.     {
  894.       if (*t)
  895.     { if (*fl != *tl || bcmp (*f, *t, *fl)) return 0; }
  896.       else
  897.     { *t = *f ; *tl = *fl; }
  898.  
  899.       t++; f++; tl++; fl++;
  900.     }
  901.   return 1;
  902. }
  903.  
  904. /*
  905.  * Create a diff3_block, with ranges as specified in the arguments.
  906.  * Allocate the arrays for the various pointers (and zero them) based
  907.  * on the arguments passed.  Return the block as a result.
  908.  */
  909. struct diff3_block *
  910. create_diff3_block (low0, high0, low1, high1, low2, high2)
  911.      register int low0, high0, low1, high1, low2, high2;
  912. {
  913.   struct diff3_block *result = ALLOCATE (1, struct diff3_block);
  914.   int numlines;
  915.  
  916.   D3_TYPE (result) = ERROR;
  917.   D_NEXT (result) = 0;
  918.  
  919.   /* Assign ranges */
  920.   D_LOWLINE (result, FILE0) = low0;
  921.   D_HIGHLINE (result, FILE0) = high0;
  922.   D_LOWLINE (result, FILE1) = low1;
  923.   D_HIGHLINE (result, FILE1) = high1;
  924.   D_LOWLINE (result, FILE2) = low2;
  925.   D_HIGHLINE (result, FILE2) = high2;
  926.  
  927.   /* Allocate and zero space */
  928.   numlines = D_NUMLINES (result, FILE0);
  929.   if (numlines)
  930.     {
  931.       D_LINEARRAY (result, FILE0) = ALLOCATE (numlines, char *);
  932.       D_LENARRAY (result, FILE0) = ALLOCATE (numlines, int);
  933.       bzero (D_LINEARRAY (result, FILE0), (numlines * sizeof (char *)));
  934.       bzero (D_LENARRAY (result, FILE0), (numlines * sizeof (int)));
  935.     }
  936.   else
  937.     {
  938.       D_LINEARRAY (result, FILE0) = (char **) 0;
  939.       D_LENARRAY (result, FILE0) = (int *) 0;
  940.     }
  941.  
  942.   numlines = D_NUMLINES (result, FILE1);
  943.   if (numlines)
  944.     {
  945.       D_LINEARRAY (result, FILE1) = ALLOCATE (numlines, char *);
  946.       D_LENARRAY (result, FILE1) = ALLOCATE (numlines, int);
  947.       bzero (D_LINEARRAY (result, FILE1), (numlines * sizeof (char *)));
  948.       bzero (D_LENARRAY (result, FILE1), (numlines * sizeof (int)));
  949.     }
  950.   else
  951.     {
  952.       D_LINEARRAY (result, FILE1) = (char **) 0;
  953.       D_LENARRAY (result, FILE1) = (int *) 0;
  954.     }
  955.  
  956.   numlines = D_NUMLINES (result, FILE2);
  957.   if (numlines)
  958.     {
  959.       D_LINEARRAY (result, FILE2) = ALLOCATE (numlines, char *);
  960.       D_LENARRAY (result, FILE2) = ALLOCATE (numlines, int);
  961.       bzero (D_LINEARRAY (result, FILE2), (numlines * sizeof (char *)));
  962.       bzero (D_LENARRAY (result, FILE2), (numlines * sizeof (int)));
  963.     }
  964.   else
  965.     {
  966.       D_LINEARRAY (result, FILE2) = (char **) 0;
  967.       D_LENARRAY (result, FILE2) = (int *) 0;
  968.     }
  969.  
  970.   /* Return */
  971.   return result;
  972. }
  973.  
  974. /*
  975.  * Compare two lists of lines of text.
  976.  * Return 1 if they are equivalent, 0 if not.
  977.  */
  978. int
  979. compare_line_list (list1, lengths1, list2, lengths2, nl)
  980.      char *list1[], *list2[];
  981.      int *lengths1, *lengths2;
  982.      int nl;
  983. {
  984.   char
  985.     **l1 = list1,
  986.     **l2 = list2;
  987.   int
  988.     *lgths1 = lengths1,
  989.     *lgths2 = lengths2;
  990.  
  991.   while (nl--)
  992.     if (!*l1 || !*l2 || *lgths1 != *lgths2++
  993.     || bcmp (*l1++, *l2++, *lgths1++))
  994.       return 0;
  995.   return 1;
  996. }
  997.  
  998. /*
  999.  * Routines to input and parse two way diffs.
  1000.  */
  1001.  
  1002. extern char **environ;
  1003.  
  1004. #define DIFF_CHUNK_SIZE 10000
  1005.  
  1006. struct diff_block *
  1007. process_diff (filea, fileb)
  1008.      char *filea, *fileb;
  1009. {
  1010.   char *diff_contents;
  1011.   char *diff_limit;
  1012.   char *scan_diff;
  1013.   enum diff_type dt;
  1014.   int i;
  1015.   struct diff_block *block_list, *block_list_end, *bptr;
  1016.  
  1017.   diff_limit = read_diff (filea, fileb, &diff_contents);
  1018.   scan_diff = diff_contents;
  1019.   bptr = block_list_end = block_list = (struct diff_block *) 0;
  1020.  
  1021.   while (scan_diff < diff_limit)
  1022.     {
  1023.       bptr = ALLOCATE (1, struct diff_block);
  1024.       bptr->next = 0;
  1025.       bptr->lines[0] = bptr->lines[1] = (char **) 0;
  1026.       bptr->lengths[0] = bptr->lengths[1] = (int *) 0;
  1027.  
  1028.       dt = process_diff_control (&scan_diff, bptr);
  1029.       if (dt == ERROR || *scan_diff != '\n')
  1030.     {
  1031.       fprintf (stderr, "%s: diff error: ", argv0);
  1032.       do
  1033.         {
  1034.           putc (*scan_diff, stderr);
  1035.         }
  1036.       while (*scan_diff++ != '\n');
  1037.       exit (2);
  1038.     }
  1039.       scan_diff++;
  1040.  
  1041.       /* Force appropriate ranges to be null, if necessary */
  1042.       switch (dt)
  1043.     {
  1044.     case ADD:
  1045.       bptr->ranges[0][0]++;
  1046.       break;
  1047.     case DELETE:
  1048.       bptr->ranges[1][0]++;
  1049.       break;
  1050.     case CHANGE:
  1051.       break;
  1052.     default:
  1053.       fatal ("internal: Bad diff type in process_diff");
  1054.       break;
  1055.     }
  1056.  
  1057.       /* Allocate space for the pointers for the lines from filea, and
  1058.      parcel them out among these pointers */
  1059.       if (dt != ADD)
  1060.     {
  1061.       bptr->lines[0] = ALLOCATE ((bptr->ranges[0][END]
  1062.                       - bptr->ranges[0][START] + 1),
  1063.                      char *);
  1064.       bptr->lengths[0] = ALLOCATE ((bptr->ranges[0][END]
  1065.                     - bptr->ranges[0][START] + 1),
  1066.                        int);
  1067.       for (i = 0; i <= (bptr->ranges[0][END]
  1068.                 - bptr->ranges[0][START]); i++)
  1069.         scan_diff = scan_diff_line (scan_diff,
  1070.                     &(bptr->lines[0][i]),
  1071.                     &(bptr->lengths[0][i]),
  1072.                     diff_limit,
  1073.                     '<');
  1074.     }
  1075.  
  1076.       /* Get past the separator for changes */
  1077.       if (dt == CHANGE)
  1078.     {
  1079.       if (strncmp (scan_diff, "---\n", 4))
  1080.         fatal ("Bad diff format: bad change separator");
  1081.       scan_diff += 4;
  1082.     }
  1083.  
  1084.       /* Allocate space for the pointers for the lines from fileb, and
  1085.      parcel them out among these pointers */
  1086.       if (dt != DELETE)
  1087.     {
  1088.       bptr->lines[1] = ALLOCATE ((bptr->ranges[1][END]
  1089.                       - bptr->ranges[1][START] + 1),
  1090.                      char *);
  1091.       bptr->lengths[1] = ALLOCATE ((bptr->ranges[1][END]
  1092.                     - bptr->ranges[1][START] + 1),
  1093.                        int);
  1094.       for (i = 0; i <= (bptr->ranges[1][END]
  1095.                 - bptr->ranges[1][START]); i++)
  1096.         scan_diff = scan_diff_line (scan_diff,
  1097.                     &(bptr->lines[1][i]),
  1098.                     &(bptr->lengths[1][i]),
  1099.                     diff_limit,
  1100.                     '>');
  1101.     }
  1102.  
  1103.       /* Place this block on the blocklist */
  1104.       if (block_list_end)
  1105.     block_list_end->next = bptr;
  1106.       else
  1107.     block_list = bptr;
  1108.  
  1109.       block_list_end = bptr;
  1110.  
  1111.     }
  1112.  
  1113.   return block_list;
  1114. }
  1115.  
  1116. /*
  1117.  * This routine will parse a normal format diff control string.  It
  1118.  * returns the type of the diff (ERROR if the format is bad).  All of
  1119.  * the other important information is filled into to the structure
  1120.  * pointed to by db, and the string pointer (whose location is passed
  1121.  * to this routine) is updated to point beyond the end of the string
  1122.  * parsed.  Note that only the ranges in the diff_block will be set by
  1123.  * this routine.
  1124.  *
  1125.  * If some specific pair of numbers has been reduced to a single
  1126.  * number, then both corresponding numbers in the diff block are set
  1127.  * to that number.  In general these numbers are interpetted as ranges
  1128.  * inclusive, unless being used by the ADD or DELETE commands.    It is
  1129.  * assumed that these will be special cased in a superior routine.
  1130.  */
  1131.  
  1132. enum diff_type
  1133. process_diff_control (string, db)
  1134.      char **string;
  1135.      struct diff_block *db;
  1136. {
  1137.   char *s = *string;
  1138.   int holdnum;
  1139.   enum diff_type type;
  1140.  
  1141. /* These macros are defined here because they can use variables
  1142.    defined in this function.  Don't try this at home kids, we're
  1143.    trained professionals!
  1144.  
  1145.    Also note that SKIPWHITE only recognizes tabs and spaces, and
  1146.    that READNUM can only read positive, integral numbers */
  1147.  
  1148. #define SKIPWHITE(s)    { while (*s == ' ' || *s == '\t') s++; }
  1149. #define READNUM(s, num) \
  1150.     { if (!isdigit (*s)) return ERROR; holdnum = 0; \
  1151.       do { holdnum = (*s++ - '0' + holdnum * 10); } \
  1152.       while (isdigit (*s)); (num) = holdnum; }
  1153.  
  1154.   /* Read first set of digits */
  1155.   SKIPWHITE (s);
  1156.   READNUM (s, db->ranges[0][START]);
  1157.  
  1158.   /* Was that the only digit? */
  1159.   SKIPWHITE(s);
  1160.   if (*s == ',')
  1161.     {
  1162.       /* Get the next digit */
  1163.       s++;
  1164.       READNUM (s, db->ranges[0][END]);
  1165.     }
  1166.   else
  1167.     db->ranges[0][END] = db->ranges[0][START];
  1168.  
  1169.   /* Get the letter */
  1170.   SKIPWHITE (s);
  1171.   switch (*s)
  1172.     {
  1173.     case 'a':
  1174.       type = ADD;
  1175.       break;
  1176.     case 'c':
  1177.       type = CHANGE;
  1178.       break;
  1179.     case 'd':
  1180.       type = DELETE;
  1181.       break;
  1182.     default:
  1183.       return ERROR;            /* Bad format */
  1184.     }
  1185.   s++;                /* Past letter */
  1186.  
  1187.   /* Read second set of digits */
  1188.   SKIPWHITE (s);
  1189.   READNUM (s, db->ranges[1][START]);
  1190.  
  1191.   /* Was that the only digit? */
  1192.   SKIPWHITE(s);
  1193.   if (*s == ',')
  1194.     {
  1195.       /* Get the next digit */
  1196.       s++;
  1197.       READNUM (s, db->ranges[1][END]);
  1198.       SKIPWHITE (s);            /* To move to end */
  1199.     }
  1200.   else
  1201.     db->ranges[1][END] = db->ranges[1][START];
  1202.  
  1203.   *string = s;
  1204.   return type;
  1205. }
  1206.  
  1207. char *
  1208. read_diff (filea, fileb, output_placement)
  1209.      char *filea, *fileb;
  1210.      char **output_placement;
  1211. {
  1212. #ifdef AMIGA
  1213.   int pipefd;
  1214.   FILE *pipefp, *popenl();
  1215.   char *diff_result;
  1216.   long current_chunk_size;
  1217.   int bytes;
  1218.   int total;
  1219.   int wstatus;
  1220.  
  1221.   pipefp = popenl(diff_program, "-a", "--", filea, fileb, NULL, "r");
  1222.   if (pipefp == NULL) {
  1223.     printf("Couldn't open pipe to diff program\n");
  1224.     exit(1);
  1225.     }
  1226.   pipefd = fileno(pipefp);
  1227.   current_chunk_size = DIFF_CHUNK_SIZE;
  1228.   diff_result = (char *) xmalloc (current_chunk_size);
  1229.   total = 0;
  1230.   do {
  1231.     bytes = myread (pipefd,
  1232.             diff_result + total,
  1233.             current_chunk_size - total);
  1234.     total += bytes;
  1235.     if (total == current_chunk_size)
  1236.       diff_result = (char *) xrealloc (diff_result, (current_chunk_size *= 2));
  1237.   } while (bytes);
  1238.  
  1239.   *output_placement = diff_result;
  1240.   wstatus = pclose(pipefp);
  1241.  
  1242.   if (! (WIFEXITED (wstatus) && WEXITSTATUS (wstatus) < 2))
  1243.     fatal ("Subsidiary diff failed");
  1244.  
  1245.   return diff_result + total;
  1246. #else
  1247.   char *argv[6];
  1248.   char **ap;
  1249.   int fds[2];
  1250.   char *diff_result;
  1251.   int current_chunk_size;
  1252.   int bytes;
  1253.   int total;
  1254.   int pid, w;
  1255.   int wstatus;
  1256.  
  1257.   ap = argv;
  1258.   *ap++ = diff_program;
  1259.   if (always_text)
  1260.     *ap++ = "-a";
  1261.   *ap++ = "--";
  1262.   *ap++ = filea;
  1263.   *ap++ = fileb;
  1264.   *ap = (char *) 0;
  1265.  
  1266.   if (pipe (fds) < 0)
  1267.     perror_with_exit ("Pipe failed");
  1268.  
  1269.   pid = vfork ();
  1270.   if (pid == 0)
  1271.     {
  1272.       /* Child */
  1273.       close (fds[0]);
  1274.       if (fds[1] != fileno (stdout))
  1275.     {
  1276.       dup2 (fds[1], fileno (stdout));
  1277.       close (fds[1]);
  1278.     }
  1279.       execve (diff_program, argv, environ);
  1280.       /* Avoid stdio, because the parent process's buffers are inherited. */
  1281.       write (fileno (stderr), diff_program, strlen (diff_program));
  1282.       write (fileno (stderr), ": not found\n", 12);
  1283.       _exit (2);
  1284.     }
  1285.  
  1286.   if (pid == -1)
  1287.     perror_with_exit ("Fork failed");
  1288.  
  1289.   close (fds[1]);               /* Prevent erroneous lack of EOF */
  1290.   current_chunk_size = DIFF_CHUNK_SIZE;
  1291.   diff_result = (char *) xmalloc (current_chunk_size);
  1292.   total = 0;
  1293.   do {
  1294.     bytes = myread (fds[0],
  1295.             diff_result + total,
  1296.             current_chunk_size - total);
  1297.     total += bytes;
  1298.     if (total == current_chunk_size)
  1299.       diff_result = (char *) xrealloc (diff_result, (current_chunk_size *= 2));
  1300.   } while (bytes);
  1301.  
  1302.   if (total != 0 && diff_result[total-1] != '\n')
  1303.     fatal ("bad diff format; incomplete last line");
  1304.  
  1305.   *output_placement = diff_result;
  1306.  
  1307.   do
  1308.     if ((w = wait (&wstatus)) == -1)
  1309.       perror_with_exit ("Wait failed");
  1310.   while (w != pid);
  1311.  
  1312.   if (! (WIFEXITED (wstatus) && WEXITSTATUS (wstatus) < 2))
  1313.     fatal ("Subsidiary diff failed");
  1314.  
  1315.   return diff_result + total;
  1316. #endif
  1317. }
  1318.  
  1319.  
  1320. /*
  1321.  * Scan a regular diff line (consisting of > or <, followed by a
  1322.  * space, followed by text (including nulls) up to a newline.
  1323.  *
  1324.  * This next routine began life as a macro and many parameters in it
  1325.  * are used as call-by-reference values.
  1326.  */
  1327. char *
  1328. scan_diff_line (scan_ptr, set_start, set_length, limit, firstchar)
  1329.      char *scan_ptr, **set_start;
  1330.      int *set_length;
  1331.      char *limit;
  1332.      char firstchar;
  1333. {
  1334.   char *line_ptr;
  1335.  
  1336.   if (!(scan_ptr[0] == (firstchar)
  1337.     && scan_ptr[1] == ' '))
  1338.     fatal ("Bad diff format; incorrect leading line chars");
  1339.  
  1340.   *set_start = line_ptr = scan_ptr + 2;
  1341.   while (*line_ptr++ != '\n')
  1342.     ;
  1343.  
  1344.   /* Include newline if the original line ended in a newline,
  1345.      or if an edit script is being generated.
  1346.      Copy any missing newline message to stderr if an edit script is being
  1347.      generated, because edit scripts cannot handle missing newlines.
  1348.      Return the beginning of the next line.  */
  1349.   *set_length = line_ptr - *set_start;
  1350.   if (line_ptr < limit && *line_ptr == '\\')
  1351.     {
  1352.       if (edscript)
  1353.     fprintf (stderr, "%s:", argv0);
  1354.       else
  1355.     --*set_length;
  1356.       line_ptr++;
  1357.       do
  1358.     {
  1359.       if (edscript)
  1360.         putc (*line_ptr, stderr);
  1361.     }
  1362.       while (*line_ptr++ != '\n');
  1363.     }
  1364.  
  1365.   return line_ptr;
  1366. }
  1367.  
  1368. /*
  1369.  * This routine outputs a three way diff passed as a list of
  1370.  * diff3_block's.
  1371.  * The argument MAPPING is indexed by external file number (in the
  1372.  * argument list) and contains the internal file number (from the
  1373.  * diff passed).  This is important because the user expects his
  1374.  * outputs in terms of the argument list number, and the diff passed
  1375.  * may have been done slightly differently (if the first argument in
  1376.  * the argument list was the standard input, for example).
  1377.  * REV_MAPPING is the inverse of MAPPING.
  1378.  */
  1379. void
  1380. output_diff3 (outputfile, diff, mapping, rev_mapping)
  1381.      FILE *outputfile;
  1382.      struct diff3_block *diff;
  1383.      int mapping[3], rev_mapping[3];
  1384. {
  1385.   int i;
  1386.   int oddoneout;
  1387.   char *cp;
  1388.   struct diff3_block *ptr;
  1389.   int line;
  1390.   int length;
  1391.   int dontprint;
  1392.   static int skew_increment[3] = { 2, 3, 1 }; /* 0==>2==>1==>3 */
  1393.  
  1394.   for (ptr = diff; ptr; ptr = D_NEXT (ptr))
  1395.     {
  1396.       char x[2];
  1397.  
  1398.       switch (ptr->correspond)
  1399.     {
  1400.     case DIFF_ALL:
  1401.       x[0] = '\0';
  1402.       dontprint = 3;    /* Print them all */
  1403.       oddoneout = 3;    /* Nobody's odder than anyone else */
  1404.       break;
  1405.     case DIFF_1ST:
  1406.     case DIFF_2ND:
  1407.     case DIFF_3RD:
  1408.       oddoneout = rev_mapping[(int) ptr->correspond - (int) DIFF_1ST];
  1409.  
  1410.       x[0] = oddoneout + '1';
  1411.       x[1] = '\0';
  1412.       dontprint = oddoneout==0;
  1413.       break;
  1414.     default:
  1415.       fatal ("internal: Bad diff type passed to output");
  1416.     }
  1417.       fprintf (outputfile, "====%s\n", x);
  1418.  
  1419.       /* Go 0, 2, 1 if the first and third outputs are equivalent. */
  1420.       for (i = 0; i < 3;
  1421.        i = (oddoneout == 1 ? skew_increment[i] : i + 1))
  1422.     {
  1423.       int realfile = mapping[i];
  1424.       int
  1425.         lowt = D_LOWLINE (ptr, realfile),
  1426.         hight = D_HIGHLINE (ptr, realfile);
  1427.  
  1428.       fprintf (outputfile, "%d:", i + 1);
  1429.       switch (lowt - hight)
  1430.         {
  1431.         case 1:
  1432.           fprintf (outputfile, "%da\n", lowt - 1);
  1433.           break;
  1434.         case 0:
  1435.           fprintf (outputfile, "%dc\n", lowt);
  1436.           break;
  1437.         default:
  1438.           fprintf (outputfile, "%d,%dc\n", lowt, hight);
  1439.           break;
  1440.         }
  1441.  
  1442.       if (i == dontprint) continue;
  1443.  
  1444.       for (line = 0; line < hight - lowt + 1; line++)
  1445.         {
  1446.           fprintf (outputfile, "  ");
  1447.           cp = D_RELNUM (ptr, realfile, line);
  1448.           length = D_RELLEN (ptr, realfile, line);
  1449.           fwrite (cp, sizeof (char), length, outputfile);
  1450.         }
  1451.       if (line != 0 && cp[length - 1] != '\n')
  1452.         fprintf (outputfile, "\n\\ No newline at end of file\n");
  1453.     }
  1454.     }
  1455. }
  1456.  
  1457. /*
  1458.  * This routine outputs a diff3 set of blocks as an ed script.    This
  1459.  * script applies the changes between file's 2 & 3 to file 1.  It
  1460.  * takes the precise format of the ed script to be output from global
  1461.  * variables set during options processing.  Note that it does
  1462.  * destructive things to the set of diff3 blocks it is passed; it
  1463.  * reverses their order (this gets around the problems involved with
  1464.  * changing line numbers in an ed script).
  1465.  *
  1466.  * Note that this routine has the same problem of mapping as the last
  1467.  * one did; the variable MAPPING maps from file number according to
  1468.  * the argument list to file number according to the diff passed.  All
  1469.  * files listed below are in terms of the argument list.
  1470.  * REV_MAPPING is the inverse of MAPPING.
  1471.  *
  1472.  * The arguments FILE0, FILE1 and FILE2 are the strings to print
  1473.  * as the names of the three files.  These may be the actual names,
  1474.  * or may be the arguments specified with -L.
  1475.  *
  1476.  * Returns 1 if overlaps were found.
  1477.  */
  1478.  
  1479. int
  1480. output_diff3_edscript (outputfile, diff, mapping, rev_mapping,
  1481.                file0, file1, file2)
  1482.      FILE *outputfile;
  1483.      struct diff3_block *diff;
  1484.      int mapping[3], rev_mapping[3];
  1485.      char *file0, *file1, *file2;
  1486. {
  1487.   int i;
  1488.   int leading_dot;
  1489.   int overlaps_found = 0;
  1490.   struct diff3_block *newblock, *thisblock;
  1491.  
  1492.   leading_dot = 0;
  1493.  
  1494.   newblock = reverse_diff3_blocklist (diff);
  1495.  
  1496.   for (thisblock = newblock; thisblock; thisblock = thisblock->next)
  1497.     {
  1498.       /* Must do mapping correctly.  */
  1499.       enum diff_type type
  1500.     = ((thisblock->correspond == DIFF_ALL) ?
  1501.        DIFF_ALL :
  1502.        ((enum diff_type)
  1503.         (((int) DIFF_1ST)
  1504.          + rev_mapping[(int) thisblock->correspond - (int) DIFF_1ST])));
  1505.  
  1506.       /* If we aren't supposed to do this output block, skip it */
  1507.       if (type == DIFF_2ND || type == DIFF_1ST
  1508.       || (type == DIFF_3RD && dont_write_simple)
  1509.       || (type == DIFF_ALL && dont_write_overlap))
  1510.     continue;
  1511.  
  1512.       if (flagging && type == DIFF_ALL)
  1513.     /* Do special flagging */
  1514.     {
  1515.  
  1516.       /* Put in lines from FILE2 with bracket */
  1517.       fprintf (outputfile, "%da\n",
  1518.            D_HIGHLINE (thisblock, mapping[FILE0]));
  1519.       fprintf (outputfile, "=======\n");
  1520.       for (i = 0;
  1521.            i < D_NUMLINES (thisblock, mapping[FILE2]);
  1522.            i++)
  1523.         {
  1524.           if (D_RELNUM (thisblock, mapping[FILE2], i)[0] == '.')
  1525.         { leading_dot = 1; fprintf(outputfile, "."); }
  1526.           fwrite (D_RELNUM (thisblock, mapping[FILE2], i), sizeof (char),
  1527.               D_RELLEN (thisblock, mapping[FILE2], i), outputfile);
  1528.         }
  1529.       fprintf (outputfile, ">>>>>>> %s\n.\n", file2);
  1530.       overlaps_found = 1;
  1531.  
  1532.       /* Add in code to take care of leading dots, if necessary. */
  1533.       if (leading_dot)
  1534.         {
  1535.           fprintf (outputfile, "%d,%ds/^\\.\\./\\./\n",
  1536.                D_HIGHLINE (thisblock, mapping[FILE0]) + 1,
  1537.                (D_HIGHLINE (thisblock, mapping[FILE0])
  1538.             + D_NUMLINES (thisblock, mapping[FILE2])));
  1539.           leading_dot = 0;
  1540.         }
  1541.  
  1542.       /* Put in code to do initial bracket of lines from FILE0  */
  1543.       fprintf (outputfile, "%da\n<<<<<<< %s\n.\n",
  1544.            D_LOWLINE (thisblock, mapping[FILE0]) - 1,
  1545.            file0);
  1546.     }
  1547.       else if (D_NUMLINES (thisblock, mapping[FILE2]) == 0)
  1548.     /* Write out a delete */
  1549.     {
  1550.       if (D_NUMLINES (thisblock, mapping[FILE0]) == 1)
  1551.         fprintf (outputfile, "%dd\n",
  1552.              D_LOWLINE (thisblock, mapping[FILE0]));
  1553.       else
  1554.         fprintf (outputfile, "%d,%dd\n",
  1555.              D_LOWLINE (thisblock, mapping[FILE0]),
  1556.              D_HIGHLINE (thisblock, mapping[FILE0]));
  1557.     }
  1558.       else
  1559.     /* Write out an add or change */
  1560.     {
  1561.       switch (D_NUMLINES (thisblock, mapping[FILE0]))
  1562.         {
  1563.         case 0:
  1564.           fprintf (outputfile, "%da\n",
  1565.                D_HIGHLINE (thisblock, mapping[FILE0]));
  1566.           break;
  1567.         case 1:
  1568.           fprintf (outputfile, "%dc\n",
  1569.                D_HIGHLINE (thisblock, mapping[FILE0]));
  1570.           break;
  1571.         default:
  1572.           fprintf (outputfile, "%d,%dc\n",
  1573.                D_LOWLINE (thisblock, mapping[FILE0]),
  1574.                D_HIGHLINE (thisblock, mapping[FILE0]));
  1575.           break;
  1576.         }
  1577.       for (i = 0;
  1578.            i < D_NUMLINES (thisblock, mapping[FILE2]);
  1579.            i++)
  1580.         {
  1581.           if (D_RELNUM (thisblock, mapping[FILE2], i)[0] == '.')
  1582.         { leading_dot = 1; fprintf (outputfile, "."); }
  1583.           fwrite (D_RELNUM (thisblock, mapping[FILE2], i), sizeof (char),
  1584.               D_RELLEN (thisblock, mapping[FILE2], i), outputfile);
  1585.         }
  1586.       fprintf (outputfile, ".\n");
  1587.  
  1588.       /* Add in code to take care of leading dots, if necessary. */
  1589.       if (leading_dot)
  1590.         {
  1591.           fprintf (outputfile, "%d,%ds/^\\.\\./\\./\n",
  1592.                D_HIGHLINE (thisblock, mapping[FILE0]) + 1,
  1593.                (D_HIGHLINE (thisblock, mapping[FILE0])
  1594.             + D_NUMLINES (thisblock, mapping[FILE2])));
  1595.           leading_dot = 0;
  1596.         }
  1597.     }
  1598.     }
  1599.   if (finalwrite) fprintf (outputfile, "w\nq\n");
  1600.   return overlaps_found;
  1601. }
  1602.  
  1603. /*
  1604.  * Read from COMMONFILE and output to OUTPUTFILE a set of diff3_ blocks DIFF
  1605.  * as a merged file.  This acts like 'ed file0 <[output_diff3_edscript]',
  1606.  * except that it works even for binary data or incomplete lines.
  1607.  *
  1608.  * As before, MAPPING maps from arg list file number to diff file number,
  1609.  * REV_MAPPING is its inverse,
  1610.  * and FILE0, FILE1, and FILE2 are the names of the files.
  1611.  *
  1612.  * Returns 1 if overlaps were found.
  1613.  */
  1614.  
  1615. int
  1616. output_diff3_merge (commonfile, outputfile, diff, mapping, rev_mapping,
  1617.             file0, file1, file2)
  1618.      FILE *commonfile, *outputfile;
  1619.      struct diff3_block *diff;
  1620.      int mapping[3], rev_mapping[3];
  1621.      char *file0, *file1, *file2;
  1622. {
  1623.   int c, i;
  1624.   int overlaps_found = 0;
  1625.   struct diff3_block *b;
  1626.   int linesread = 0;
  1627.  
  1628.   for (b = diff; b; b = b->next)
  1629.     {
  1630.       /* Must do mapping correctly */
  1631.       enum diff_type type
  1632.     = ((b->correspond == DIFF_ALL) ?
  1633.        DIFF_ALL :
  1634.        ((enum diff_type)
  1635.         (((int) DIFF_1ST)
  1636.          + rev_mapping[(int) b->correspond - (int) DIFF_1ST])));
  1637.  
  1638.       /* If we aren't supposed to do this output block, skip it.  */
  1639.       if (type == DIFF_2ND || type == DIFF_1ST
  1640.       || (type == DIFF_3RD && dont_write_simple)
  1641.       || (type == DIFF_ALL && dont_write_overlap))
  1642.     continue;
  1643.  
  1644.       /* Copy I lines from common file.  */
  1645.       i = D_LOWLINE (b, FILE0) - linesread - 1;
  1646.       linesread += i;
  1647.       while (0 <= --i)
  1648.     {
  1649.       while ((c = getc(commonfile)) != '\n')
  1650.         {
  1651.           if (c == EOF)
  1652.         fatal ("input file shrank");
  1653.           putc (c, outputfile);
  1654.         }
  1655.       putc (c, outputfile);
  1656.     }
  1657.  
  1658.       if (flagging && type == DIFF_ALL)
  1659.     /* Do special flagging.  */
  1660.     {
  1661.       /* Put in lines from FILE0 with bracket.  */
  1662.       fprintf (outputfile, "<<<<<<< %s\n", file0);
  1663.       for (i = 0;
  1664.            i < D_NUMLINES (b, mapping[FILE0]);
  1665.            i++)
  1666.         fwrite (D_RELNUM (b, mapping[FILE0], i), sizeof (char),
  1667.             D_RELLEN (b, mapping[FILE0], i), outputfile);
  1668.       fprintf (outputfile, "=======\n");
  1669.       overlaps_found = 1;
  1670.     }
  1671.  
  1672.       /* Put in lines from FILE2.  */
  1673.       for (i = 0;
  1674.        i < D_NUMLINES (b, mapping[FILE2]);
  1675.        i++)
  1676.     fwrite (D_RELNUM (b, mapping[FILE2], i), sizeof (char),
  1677.         D_RELLEN (b, mapping[FILE2], i), outputfile);
  1678.  
  1679.       if (flagging && type == DIFF_ALL)
  1680.     fprintf (outputfile, ">>>>>>> %s\n", file2);
  1681.  
  1682.       /* Skip I lines in common file.  */
  1683.       i = D_NUMLINES (b, FILE0);
  1684.       linesread += i;
  1685.       while (0 <= --i)
  1686.     while ((c = getc(commonfile)) != '\n')
  1687.       if (c == EOF)
  1688.         {
  1689.           if (i || b->next)
  1690.         fatal ("input file shrank");
  1691.           return overlaps_found;
  1692.         }
  1693.     }
  1694.   /* Copy rest of common file.    */
  1695.   while ((c = getc (commonfile)) != EOF)
  1696.     putc (c, outputfile);
  1697.   return overlaps_found;
  1698. }
  1699.  
  1700. /*
  1701.  * Reverse the order of the list of diff3 blocks.
  1702.  */
  1703. struct diff3_block *
  1704. reverse_diff3_blocklist (diff)
  1705.      struct diff3_block *diff;
  1706. {
  1707.   register struct diff3_block *tmp, *next, *prev;
  1708.  
  1709.   for (tmp = diff, prev = (struct diff3_block *) 0;
  1710.        tmp; tmp = next)
  1711.     {
  1712.       next = tmp->next;
  1713.       tmp->next = prev;
  1714.       prev = tmp;
  1715.     }
  1716.  
  1717.   return prev;
  1718. }
  1719.  
  1720. int
  1721. myread (fd, ptr, size)
  1722.      int fd, size;
  1723.      char *ptr;
  1724. {
  1725.   int result = read (fd, ptr, size);
  1726.   if (result < 0)
  1727.     perror_with_exit ("Read failed");
  1728.   return result;
  1729. }
  1730.  
  1731. VOID *
  1732. xmalloc (size)
  1733.      int size;
  1734. {
  1735.   VOID *result = (VOID *) malloc (size ? size : 1);
  1736.   if (!result)
  1737.     fatal ("Malloc failed");
  1738.   return result;
  1739. }
  1740.  
  1741. VOID *
  1742. xrealloc (ptr, size)
  1743.      VOID *ptr;
  1744.      int size;
  1745. {
  1746.   VOID *result = (VOID *) realloc (ptr, size ? size : 1);
  1747.   if (!result)
  1748.     fatal ("Malloc failed");
  1749.   return result;
  1750. }
  1751.  
  1752. fatal (string)
  1753.      char *string;
  1754. {
  1755.   fprintf (stderr, "%s: %s\n", argv0, string);
  1756.   exit (2);
  1757. }
  1758.  
  1759. perror_with_exit (string)
  1760.      char *string;
  1761. {
  1762.   perror (string);
  1763.   exit (2);
  1764. }
  1765. @
  1766.  
  1767.  
  1768. 1.1
  1769. log
  1770. @Initial revision
  1771. @
  1772. text
  1773. @d27 1
  1774. a27 1
  1775. /* 
  1776. d35 8
  1777. d48 2
  1778. a49 2
  1779. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  1780. #define bzero(s,n)    memset((s),0,(n))
  1781. d52 1
  1782. a52 1
  1783. #define dup2(f,t)    (close(t),fcntl((f),F_DUPFD,(t)))
  1784. d60 1
  1785. d83 3
  1786. a85 3
  1787. #define    FILE0    0
  1788. #define    FILE1    1
  1789. #define    FILE2    2
  1790. d91 1
  1791. a91 1
  1792. #define    FILEC    FILE0
  1793. d94 2
  1794. a95 2
  1795. #define    START    0
  1796. #define    END    1
  1797. d110 1
  1798. a110 1
  1799.   int ranges[2][2];            /* Ranges are inclusive */
  1800. d120 1
  1801. a120 1
  1802.   int ranges[3][2];            /* Ranges are inclusive */
  1803. d129 1
  1804. a129 1
  1805. #define    D_LOWLINE(diff, filenum)    \
  1806. d131 1
  1807. a131 1
  1808. #define    D_HIGHLINE(diff, filenum)    \
  1809. d133 1
  1810. a133 1
  1811. #define    D_NUMLINES(diff, filenum)    \
  1812. d141 1
  1813. a141 1
  1814. #define    D_RELNUM(diff, filenum, linenum)    \
  1815. d143 1
  1816. a143 1
  1817. #define    D_RELLEN(diff, filenum, linenum)    \
  1818. d149 1
  1819. a149 1
  1820. #define    D_LINEARRAY(diff, filenum)    \
  1821. d151 1
  1822. a151 1
  1823. #define    D_LENARRAY(diff, filenum)    \
  1824. d157 1
  1825. a157 1
  1826. #define    D_NEXT(diff)    ((diff)->next)
  1827. d162 1
  1828. a162 1
  1829. #define    D3_TYPE(diff)    ((diff)->correspond)
  1830. d168 3
  1831. a170 3
  1832. #define    D_HIGH_MAPLINE(diff, fromfile, tofile, lineno)    \
  1833.   ((lineno)                        \
  1834.    - D_HIGHLINE ((diff), (fromfile))            \
  1835. d173 3
  1836. a175 3
  1837. #define    D_LOW_MAPLINE(diff, fromfile, tofile, lineno)    \
  1838.   ((lineno)                        \
  1839.    - D_LOWLINE ((diff), (fromfile))            \
  1840. d181 1
  1841. a181 1
  1842. #define    ALLOCATE(number, type)    \
  1843. d199 1
  1844. a199 1
  1845.  * should not be output. 
  1846. d260 1
  1847. a260 1
  1848.   /* Element 0 is for file 0, element 1 is for file 2.  */
  1849. d270 1
  1850. a270 1
  1851.   
  1852. d321 1
  1853. a321 1
  1854.         /* -i needs one of -eExX3; -i -m would rewrite input file.  */
  1855. d333 1
  1856. a333 1
  1857.       /* Sigh.  We've got standard input as the first arg. We can't */
  1858. d365 1
  1859. a365 1
  1860.   
  1861. d395 1
  1862. a395 1
  1863.       
  1864. d416 6
  1865. a421 6
  1866.  *     1) Find the range for the first block in File0.
  1867.  *          a) Take the lowest of the two ranges (in File0) in the two
  1868.  *             current blocks (one from each diff) as being the low
  1869.  *             water mark.  Assign the upper end of this block as
  1870.  *             being the high water mark and move the current block up
  1871.  *             one.  Mark the block just moved over as to be used.
  1872. d423 2
  1873. a424 2
  1874.  *             mark is *not* from.  
  1875.  *           
  1876. d426 10
  1877. a435 10
  1878.  *             the low end of the range in that block, 
  1879.  * 
  1880.  *               mark that block as to be used and move the current
  1881.  *                 block up.  Set the high water mark to the max of
  1882.  *                 the high end of this block and the current.  Repeat b.
  1883.  * 
  1884.  *       2) Find the corresponding ranges in Files1 (from the blocks
  1885.  *          in diff01; line per line outside of diffs) and in File2.
  1886.  *          Create a diff3_block, reserving space as indicated by the ranges.
  1887.  *        
  1888. d437 2
  1889. a438 2
  1890.  *          do bcmp's between corresponding strings in the two diffs.
  1891.  *        
  1892. d440 3
  1893. a442 3
  1894.  *          need from file0 (when there isn't a diff block, it's
  1895.  *          identical to file0 within the range between diff blocks).
  1896.  *        
  1897. d444 11
  1898. a454 11
  1899.  *         strings of diffs, then that file (i.e. the one other than
  1900.  *         file 0 in that diff) is the odd person out.  If you used
  1901.  *         diff blocks from both sets, check to see if files 1 and 2 match:
  1902.  *        
  1903.  *            Same number of lines?  If so, do a set of bcmp's (if a
  1904.  *          bcmp matches; copy the pointer over; it'll be easier later
  1905.  *          if you have to do any compares).  If they match, 1 & 2 are
  1906.  *          the same.  If not, all three different.
  1907.  * 
  1908.  *   Then you do it again, until you run out of blocks. 
  1909.  * 
  1910. d457 1
  1911. a457 1
  1912. /* 
  1913. d471 1
  1914. a471 1
  1915.  * Thread number 0 is diff1, thread number 1 is diff2.  The USING
  1916. d479 2
  1917. a480 2
  1918.  * described in any of the diffs in either of the USING lists.  The
  1919.  * HIGH_WATER_THREAD names the thread.  Similarly the BASE_WATER_MARK
  1920. d482 1
  1921. a482 1
  1922.  * described in any of the diffs in either of the USING lists.  The
  1923. d484 1
  1924. a484 1
  1925.  * taken. 
  1926. d552 1
  1927. a552 1
  1928.       
  1929. d554 1
  1930. a554 1
  1931.     
  1932. d575 1
  1933. a575 1
  1934.          needs to be incorporated */
  1935. d615 1
  1936. a615 1
  1937.          included in this diff3_block.  Create it.  */
  1938. d670 1
  1939. a670 1
  1940.   
  1941. d678 1
  1942. a678 1
  1943.   
  1944. d682 1
  1945. a682 1
  1946.       high1 = D_HIGH_MAPLINE (last_using[0], FILE0, FILE1, highc); 
  1947. d697 1
  1948. a697 1
  1949.       high2 = D_HIGH_MAPLINE (last_using[1], FILE0, FILE1, highc); 
  1950. d715 1
  1951. a715 1
  1952.       
  1953. d729 1
  1954. a729 1
  1955.       
  1956. d748 1
  1957. a748 1
  1958.   
  1959. d787 1
  1960. a787 1
  1961.   
  1962. d838 1
  1963. a838 1
  1964.   
  1965. d844 1
  1966. a844 1
  1967.  * of strings.  If a spot in the second list is already filled, it
  1968. d861 1
  1969. a861 1
  1970.   
  1971. d960 1
  1972. a960 1
  1973.   
  1974. d968 1
  1975. a968 1
  1976. /* 
  1977. d974 1
  1978. a974 1
  1979. #define    DIFF_CHUNK_SIZE    10000
  1980. d997 1
  1981. a997 1
  1982.       
  1983. d1010 1
  1984. a1010 1
  1985.       
  1986. d1026 1
  1987. a1026 1
  1988.       
  1989. d1045 1
  1990. a1045 1
  1991.       
  1992. d1053 1
  1993. a1053 1
  1994.       
  1995. d1072 1
  1996. a1072 1
  1997.       
  1998. d1078 1
  1999. a1078 1
  2000.       
  2001. d1080 1
  2002. a1080 1
  2003.       
  2004. d1098 2
  2005. a1099 2
  2006.  * inclusive, unless being used by the ADD or DELETE commands.  It is
  2007.  * assumed that these will be special cased in a superior routine.  
  2008. d1118 4
  2009. a1121 4
  2010. #define    SKIPWHITE(s)    { while (*s == ' ' || *s == '\t') s++; }
  2011. #define    READNUM(s, num)    \
  2012.     { if (!isdigit (*s)) return ERROR; holdnum = 0;    \
  2013.       do { holdnum = (*s++ - '0' + holdnum * 10); }    \
  2014. d1156 1
  2015. a1156 1
  2016.   
  2017. d1168 1
  2018. a1168 1
  2019.       SKIPWHITE (s);        /* To move to end */
  2020. d1182 35
  2021. d1259 1
  2022. a1259 1
  2023.   close (fds[1]);        /* Prevent erroneous lack of EOF */
  2024. d1286 1
  2025. d1379 1
  2026. a1379 1
  2027.         
  2028. d1397 1
  2029. a1397 1
  2030.       
  2031. d1428 1
  2032. a1428 1
  2033.  * This routine outputs a diff3 set of blocks as an ed script.  This
  2034. d1557 1
  2035. a1557 1
  2036.       
  2037. d1664 1
  2038. a1664 1
  2039.   /* Copy rest of common file.  */
  2040. d1686 1
  2041. a1686 1
  2042.   
  2043. @
  2044.