1 /* Support routines for GNU DIFF.
2 Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU DIFF.
6 GNU DIFF is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU DIFF is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU DIFF; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #define PR_PROGRAM "/bin/pr"
28 /* Queue up one-line messages to be printed at the end,
29 when -l is specified. Each message is recorded with a `struct msg'. */
41 /* Head of the chain of queues messages. */
43 static struct msg *msg_chain;
45 /* Tail of the chain of queues messages. */
47 static struct msg **msg_chain_end = &msg_chain;
49 /* Use when a system call returns non-zero status.
50 TEXT should normally be the file name. */
53 perror_with_name (text)
57 fprintf (stderr, "%s: ", program_name);
62 /* Use when a system call returns non-zero status and that is fatal. */
65 pfatal_with_name (text)
69 print_message_queue ();
70 fprintf (stderr, "%s: ", program_name);
76 /* Print an error message from the format-string FORMAT
77 with args ARG1 and ARG2. */
80 error (format, arg, arg1)
81 char const *format, *arg, *arg1;
83 fprintf (stderr, "%s: ", program_name);
84 fprintf (stderr, format, arg, arg1);
85 fprintf (stderr, "\n");
88 /* Print an error message containing the string TEXT, then exit. */
94 print_message_queue ();
99 /* Like printf, except if -l in effect then save the message and print later.
100 This is used for things like "binary files differ" and "Only in ...". */
103 message (format, arg1, arg2)
104 char const *format, *arg1, *arg2;
106 message5 (format, arg1, arg2, 0, 0);
110 message5 (format, arg1, arg2, arg3, arg4)
111 char const *format, *arg1, *arg2, *arg3, *arg4;
115 struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
116 new->format = format;
117 new->arg1 = concat (arg1, "", "");
118 new->arg2 = concat (arg2, "", "");
119 new->arg3 = arg3 ? concat (arg3, "", "") : 0;
120 new->arg4 = arg4 ? concat (arg4, "", "") : 0;
122 *msg_chain_end = new;
123 msg_chain_end = &new->next;
127 if (sdiff_help_sdiff)
129 printf (format, arg1, arg2, arg3, arg4);
133 /* Output all the messages that were saved up by calls to `message'. */
136 print_message_queue ()
140 for (m = msg_chain; m; m = m->next)
141 printf (m->format, m->arg1, m->arg2, m->arg3, m->arg4);
144 /* Call before outputting the results of comparing files NAME0 and NAME1
145 to set up OUTFILE, the stdio stream for the output to go to.
147 Usually, OUTFILE is just stdout. But when -l was specified
148 we fork off a `pr' and make OUTFILE a pipe to it.
149 `pr' then outputs to our stdout. */
151 static char const *current_name0;
152 static char const *current_name1;
153 static int current_depth;
156 setup_output (name0, name1, depth)
157 char const *name0, *name1;
160 current_name0 = name0;
161 current_name1 = name1;
162 current_depth = depth;
178 /* Construct the header of this piece of diff. */
179 name = xmalloc (strlen (current_name0) + strlen (current_name1)
180 + strlen (switch_string) + 7);
181 /* Posix.2 section 4.17.6.1.1 specifies this format. But there is a
182 bug in the first printing (IEEE Std 1003.2-1992 p 251 l 3304):
183 it says that we must print only the last component of the pathnames.
184 This requirement is silly and does not match historical practice. */
185 sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
189 /* Make OUTFILE a pipe to a subsidiary `pr'. */
194 if (pipe (pipes) != 0)
195 pfatal_with_name ("pipe");
201 pfatal_with_name ("vfork");
206 if (pipes[0] != STDIN_FILENO)
208 if (dup2 (pipes[0], STDIN_FILENO) < 0)
209 pfatal_with_name ("dup2");
213 execl (PR_PROGRAM, PR_PROGRAM, "-F", "-h", name, 0);
215 execl (PR_PROGRAM, PR_PROGRAM, "-f", "-h", name, 0);
217 pfatal_with_name (PR_PROGRAM);
222 outfile = fdopen (pipes[1], "w");
224 pfatal_with_name ("fdopen");
226 #else /* ! HAVE_FORK */
227 char *command = xmalloc (4 * strlen (name) + strlen (PR_PROGRAM) + 10);
229 char const *a = name;
230 sprintf (command, "%s -f -h ", PR_PROGRAM);
231 p = command + strlen (command);
232 SYSTEM_QUOTE_ARG (p, a);
234 outfile = popen (command, "w");
236 pfatal_with_name (command);
238 #endif /* ! HAVE_FORK */
243 /* If -l was not specified, output the diff straight to `stdout'. */
247 /* If handling multiple files (because scanning a directory),
248 print which files the following output is about. */
249 if (current_depth > 0)
250 printf ("%s\n", name);
255 /* A special header is needed at the beginning of context output. */
256 switch (output_style)
259 print_context_header (files, 0);
263 print_context_header (files, 1);
271 /* Call after the end of output of diffs for one file.
272 Close OUTFILE and get rid of the `pr' subfork. */
277 if (outfile != 0 && outfile != stdout)
280 if (ferror (outfile))
281 fatal ("write error");
283 wstatus = pclose (outfile);
284 #else /* HAVE_FORK */
285 if (fclose (outfile) != 0)
286 pfatal_with_name ("write error");
287 if (waitpid (pr_pid, &wstatus, 0) < 0)
288 pfatal_with_name ("waitpid");
289 #endif /* HAVE_FORK */
291 fatal ("subsidiary pr failed");
297 /* Compare two lines (typically one from each input file)
298 according to the command line options.
299 For efficiency, this is invoked only when the lines do not match exactly
300 but an option like -i might cause us to ignore the difference.
301 Return nonzero if the lines differ. */
307 register unsigned char const *t1 = (unsigned char const *) s1;
308 register unsigned char const *t2 = (unsigned char const *) s2;
312 register unsigned char c1 = *t1++;
313 register unsigned char c2 = *t2++;
315 /* Test for exact char equality first, since it's a common case. */
318 /* Ignore horizontal white space if -b or -w is specified. */
320 if (ignore_all_space_flag)
322 /* For -w, just skip past any white space. */
323 while (ISSPACE (c1) && c1 != '\n') c1 = *t1++;
324 while (ISSPACE (c2) && c2 != '\n') c2 = *t2++;
326 else if (ignore_space_change_flag)
328 /* For -b, advance past any sequence of white space in line 1
329 and consider it just one Space, or nothing at all
330 if it is at the end of the line. */
345 /* Likewise for line 2. */
362 /* If we went too far when doing the simple test
363 for equality, go back to the first non-white-space
364 character in both sides and try again. */
365 if (c2 == ' ' && c1 != '\n'
366 && (unsigned char const *) s1 + 1 < t1
372 if (c1 == ' ' && c2 != '\n'
373 && (unsigned char const *) s2 + 1 < t2
382 /* Lowercase all letters if -i is specified. */
384 if (ignore_case_flag)
402 /* Find the consecutive changes at the start of the script START.
403 Return the last link before the first gap. */
407 struct change *start;
413 find_reverse_change (start)
414 struct change *start;
419 /* Divide SCRIPT into pieces by calling HUNKFUN and
420 print each piece with PRINTFUN.
421 Both functions take one arg, an edit script.
423 HUNKFUN is called with the tail of the script
424 and returns the last link that belongs together with the start
427 PRINTFUN takes a subscript which belongs together (with a null
428 link at the end) and prints it. */
431 print_script (script, hunkfun, printfun)
432 struct change *script;
433 struct change * (*hunkfun) PARAMS((struct change *));
434 void (*printfun) PARAMS((struct change *));
436 struct change *next = script;
440 struct change *this, *end;
442 /* Find a set of changes that belong together. */
444 end = (*hunkfun) (next);
446 /* Disconnect them from the rest of the changes,
447 making them a hunk, and remember the rest for next iteration. */
454 /* Print this hunk. */
457 /* Reconnect the script so it will all be freed properly. */
462 /* Print the text of a single line LINE,
463 flagging it with the characters in LINE_FLAG (which say whether
464 the line is inserted, deleted, changed, etc.). */
467 print_1_line (line_flag, line)
468 char const *line_flag;
469 char const * const *line;
471 char const *text = line[0], *limit = line[1]; /* Help the compiler. */
472 FILE *out = outfile; /* Help the compiler some more. */
473 char const *flag_format = 0;
475 /* If -T was specified, use a Tab between the line-flag and the text.
476 Otherwise use a Space (as Unix diff does).
477 Print neither space nor tab if line-flags are empty. */
479 if (line_flag && *line_flag)
481 flag_format = tab_align_flag ? "%s\t" : "%s ";
482 fprintf (out, flag_format, line_flag);
485 output_1_line (text, limit, flag_format, line_flag);
487 if ((!line_flag || line_flag[0]) && limit[-1] != '\n')
491 /* Output a line from TEXT up to LIMIT. Without -t, output verbatim.
492 With -t, expand white space characters to spaces, and if FLAG_FORMAT
493 is nonzero, output it with argument LINE_FLAG after every
494 internal carriage return, so that tab stops continue to line up. */
497 output_1_line (text, limit, flag_format, line_flag)
498 char const *text, *limit, *flag_format, *line_flag;
500 if (!tab_expand_flag)
501 fwrite (text, sizeof (char), limit - text, outfile);
504 register FILE *out = outfile;
505 register unsigned char c;
506 register char const *t = text;
507 register unsigned column = 0;
514 unsigned spaces = TAB_WIDTH - column % TAB_WIDTH;
524 if (flag_format && t < limit && *t != '\n')
525 fprintf (out, flag_format, line_flag);
546 change_letter (inserts, deletes)
547 int inserts, deletes;
557 /* Translate an internal line number (an index into diff's table of lines)
558 into an actual line number in the input file.
559 The internal line number is LNUM. FILE points to the data on the file.
561 Internal line numbers count from 0 starting after the prefix.
562 Actual line numbers count from 1 within the entire file. */
565 translate_line_number (file, lnum)
566 struct file_data const *file;
569 return lnum + file->prefix_lines + 1;
573 translate_range (file, a, b, aptr, bptr)
574 struct file_data const *file;
578 *aptr = translate_line_number (file, a - 1) + 1;
579 *bptr = translate_line_number (file, b + 1) - 1;
582 /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
583 If the two numbers are identical, print just one number.
585 Args A and B are internal line numbers.
586 We print the translated (real) line numbers. */
589 print_number_range (sepchar, file, a, b)
591 struct file_data *file;
594 int trans_a, trans_b;
595 translate_range (file, a, b, &trans_a, &trans_b);
597 /* Note: we can have B < A in the case of a range of no lines.
598 In this case, we should print the line number before the range,
600 if (trans_b > trans_a)
601 fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
603 fprintf (outfile, "%d", trans_b);
606 /* Look at a hunk of edit script and report the range of lines in each file
607 that it applies to. HUNK is the start of the hunk, which is a chain
608 of `struct change'. The first and last line numbers of file 0 are stored in
609 *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
610 Note that these are internal line numbers that count from 0.
612 If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
614 Also set *DELETES nonzero if any lines of file 0 are deleted
615 and set *INSERTS nonzero if any lines of file 1 are inserted.
616 If only ignorable lines are inserted or deleted, both are
620 analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
622 int *first0, *last0, *first1, *last1;
623 int *deletes, *inserts;
625 int l0, l1, show_from, show_to;
627 int trivial = ignore_blank_lines_flag || ignore_regexp_list;
630 show_from = show_to = 0;
632 *first0 = hunk->line0;
633 *first1 = hunk->line1;
638 l0 = next->line0 + next->deleted - 1;
639 l1 = next->line1 + next->inserted - 1;
640 show_from += next->deleted;
641 show_to += next->inserted;
643 for (i = next->line0; i <= l0 && trivial; i++)
644 if (!ignore_blank_lines_flag || files[0].linbuf[i][0] != '\n')
646 struct regexp_list *r;
647 char const *line = files[0].linbuf[i];
648 int len = files[0].linbuf[i + 1] - line;
650 for (r = ignore_regexp_list; r; r = r->next)
651 if (0 <= re_search (&r->buf, line, len, 0, len, 0))
652 break; /* Found a match. Ignore this line. */
653 /* If we got all the way through the regexp list without
654 finding a match, then it's nontrivial. */
659 for (i = next->line1; i <= l1 && trivial; i++)
660 if (!ignore_blank_lines_flag || files[1].linbuf[i][0] != '\n')
662 struct regexp_list *r;
663 char const *line = files[1].linbuf[i];
664 int len = files[1].linbuf[i + 1] - line;
666 for (r = ignore_regexp_list; r; r = r->next)
667 if (0 <= re_search (&r->buf, line, len, 0, len, 0))
668 break; /* Found a match. Ignore this line. */
669 /* If we got all the way through the regexp list without
670 finding a match, then it's nontrivial. */
675 while ((next = next->link) != 0);
680 /* If all inserted or deleted lines are ignorable,
681 tell the caller to ignore this hunk. */
684 show_from = show_to = 0;
686 *deletes = show_from;
690 /* malloc a block of memory, with fatal error message if we can't do it. */
696 register VOID *value;
701 value = (VOID *) malloc (size);
704 fatal ("memory exhausted");
708 /* realloc a block of memory, with fatal error message if we can't do it. */
715 register VOID *value;
720 value = (VOID *) realloc (old, size);
723 fatal ("memory exhausted");
727 /* Concatenate three strings, returning a newly malloc'd string. */
731 char const *s1, *s2, *s3;
733 size_t len = strlen (s1) + strlen (s2) + strlen (s3);
734 char *new = xmalloc (len + 1);
735 sprintf (new, "%s%s%s", s1, s2, s3);
739 /* Yield the newly malloc'd pathname
740 of the file in DIR whose filename is FILE. */
743 dir_file_pathname (dir, file)
744 char const *dir, *file;
746 char const *p = filename_lastdirchar (dir);
747 return concat (dir, "/" + (p && !p[1]), file);
755 for (; sp; sp = sp->link)
756 fprintf (stderr, "%3d %3d delete %d insert %d\n",
757 sp->line0, sp->line1, sp->deleted, sp->inserted);