1 /* Support routines for GNU DIFF.
3 Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002,
4 2004 Free Software Foundation, Inc.
6 This file is part of GNU DIFF.
8 GNU DIFF is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU DIFF is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING.
20 If not, write to the Free Software Foundation,
21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 char const pr_program
[] = PR_PROGRAM
;
31 /* Queue up one-line messages to be printed at the end,
32 when -l is specified. Each message is recorded with a `struct msg'. */
37 char args
[1]; /* Format + 4 args, each '\0' terminated, concatenated. */
40 /* Head of the chain of queues messages. */
42 static struct msg
*msg_chain
;
44 /* Tail of the chain of queues messages. */
46 static struct msg
**msg_chain_end
= &msg_chain
;
48 /* Use when a system call returns non-zero status.
49 NAME should normally be the file name. */
52 perror_with_name (char const *name
)
54 error (0, errno
, "%s", name
);
57 /* Use when a system call returns non-zero status and that is fatal. */
60 pfatal_with_name (char const *name
)
63 print_message_queue ();
64 error (EXIT_TROUBLE
, e
, "%s", name
);
68 /* Print an error message containing MSGID, then exit. */
71 fatal (char const *msgid
)
73 print_message_queue ();
74 error (EXIT_TROUBLE
, 0, "%s", _(msgid
));
78 /* Like printf, except if -l in effect then save the message and print later.
79 This is used for things like "Only in ...". */
82 message (char const *format_msgid
, char const *arg1
, char const *arg2
)
84 message5 (format_msgid
, arg1
, arg2
, 0, 0);
88 message5 (char const *format_msgid
, char const *arg1
, char const *arg2
,
89 char const *arg3
, char const *arg4
)
97 size_t total_size
= offsetof (struct msg
, args
);
100 arg
[0] = format_msgid
;
103 arg
[3] = arg3
? arg3
: "";
104 arg
[4] = arg4
? arg4
: "";
106 for (i
= 0; i
< 5; i
++)
107 total_size
+= size
[i
] = strlen (arg
[i
]) + 1;
109 new = xmalloc (total_size
);
111 for (i
= 0, p
= new->args
; i
< 5; p
+= size
[i
++])
112 memcpy (p
, arg
[i
], size
[i
]);
114 *msg_chain_end
= new;
116 msg_chain_end
= &new->next
;
120 if (sdiff_merge_assist
)
122 printf (_(format_msgid
), arg1
, arg2
, arg3
, arg4
);
126 /* Output all the messages that were saved up by calls to `message'. */
129 print_message_queue (void)
133 struct msg
*m
= msg_chain
;
137 struct msg
*next
= m
->next
;
139 for (i
= 0; i
< 4; i
++)
140 arg
[i
+ 1] = arg
[i
] + strlen (arg
[i
]) + 1;
141 printf (_(arg
[0]), arg
[1], arg
[2], arg
[3], arg
[4]);
147 /* Call before outputting the results of comparing files NAME0 and NAME1
148 to set up OUTFILE, the stdio stream for the output to go to.
150 Usually, OUTFILE is just stdout. But when -l was specified
151 we fork off a `pr' and make OUTFILE a pipe to it.
152 `pr' then outputs to our stdout. */
154 static char const *current_name0
;
155 static char const *current_name1
;
156 static bool currently_recursive
;
159 setup_output (char const *name0
, char const *name1
, bool recursive
)
161 current_name0
= name0
;
162 current_name1
= name1
;
163 currently_recursive
= recursive
;
167 #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
179 /* Construct the header of this piece of diff. */
180 name
= xmalloc (strlen (current_name0
) + strlen (current_name1
)
181 + strlen (switch_string
) + 7);
183 /* POSIX 1003.1-2001 specifies this format. But there are some bugs in
184 the standard: it says that we must print only the last component
185 of the pathnames, and it requires two spaces after "diff" if
186 there are no options. These requirements are silly and do not
187 match historical practice. */
188 sprintf (name
, "diff%s %s %s", switch_string
, current_name0
, current_name1
);
192 if (fflush (stdout
) != 0)
193 pfatal_with_name (_("write failed"));
195 /* Make OUTFILE a pipe to a subsidiary `pr'. */
197 #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
200 if (pipe (pipes
) != 0)
201 pfatal_with_name ("pipe");
205 pfatal_with_name ("fork");
210 if (pipes
[0] != STDIN_FILENO
)
212 if (dup2 (pipes
[0], STDIN_FILENO
) < 0)
213 pfatal_with_name ("dup2");
217 execl (pr_program
, pr_program
, "-h", name
, (char *) 0);
218 _exit (errno
== ENOENT
? 127 : 126);
223 outfile
= fdopen (pipes
[1], "w");
225 pfatal_with_name ("fdopen");
228 char *command
= xmalloc (sizeof pr_program
- 1 + 7
229 + quote_system_arg ((char *) 0, name
) + 1);
231 sprintf (command
, "%s -f -h ", pr_program
);
232 p
= command
+ sizeof pr_program
- 1 + 7;
233 p
+= quote_system_arg (p
, name
);
236 outfile
= popen (command
, "w");
238 pfatal_with_name (command
);
246 /* If -l was not specified, output the diff straight to `stdout'. */
250 /* If handling multiple files (because scanning a directory),
251 print which files the following output is about. */
252 if (currently_recursive
)
253 printf ("%s\n", name
);
258 /* A special header is needed at the beginning of context output. */
259 switch (output_style
)
262 print_context_header (files
, false);
266 print_context_header (files
, true);
274 /* Call after the end of output of diffs for one file.
275 Close OUTFILE and get rid of the `pr' subfork. */
280 if (outfile
!= 0 && outfile
!= stdout
)
285 if (ferror (outfile
))
286 fatal ("write failed");
287 #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK)
288 wstatus
= pclose (outfile
);
292 if (fclose (outfile
) != 0)
293 pfatal_with_name (_("write failed"));
294 if (waitpid (pr_pid
, &wstatus
, 0) < 0)
295 pfatal_with_name ("waitpid");
297 status
= (! werrno
&& WIFEXITED (wstatus
)
298 ? WEXITSTATUS (wstatus
)
301 error (EXIT_TROUBLE
, werrno
,
303 ? "subsidiary program `%s' could not be invoked"
305 ? "subsidiary program `%s' not found"
307 ? "subsidiary program `%s' failed"
308 : "subsidiary program `%s' failed (exit status %d)"),
315 /* Compare two lines (typically one from each input file)
316 according to the command line options.
317 For efficiency, this is invoked only when the lines do not match exactly
318 but an option like -i might cause us to ignore the difference.
319 Return nonzero if the lines differ. */
322 lines_differ (char const *s1
, char const *s2
)
324 register char const *t1
= s1
;
325 register char const *t2
= s2
;
330 register unsigned char c1
= *t1
++;
331 register unsigned char c2
= *t2
++;
333 /* Test for exact char equality first, since it's a common case. */
336 switch (ignore_white_space
)
338 case IGNORE_ALL_SPACE
:
339 /* For -w, just skip past any white space. */
340 while (isspace (c1
) && c1
!= '\n') c1
= *t1
++;
341 while (isspace (c2
) && c2
!= '\n') c2
= *t2
++;
344 case IGNORE_SPACE_CHANGE
:
345 /* For -b, advance past any sequence of white space in
346 line 1 and consider it just one space, or nothing at
347 all if it is at the end of the line. */
362 /* Likewise for line 2. */
379 /* If we went too far when doing the simple test
380 for equality, go back to the first non-white-space
381 character in both sides and try again. */
382 if (c2
== ' ' && c1
!= '\n'
384 && isspace ((unsigned char) t1
[-2]))
389 if (c1
== ' ' && c2
!= '\n'
391 && isspace ((unsigned char) t2
[-2]))
400 case IGNORE_TAB_EXPANSION
:
401 if ((c1
== ' ' && c2
== '\t')
402 || (c1
== '\t' && c2
== ' '))
404 size_t column2
= column
;
410 column
+= tabsize
- column
% tabsize
;
419 column2
+= tabsize
- column2
% tabsize
;
423 if (column
!= column2
)
428 case IGNORE_NO_WHITE_SPACE
:
432 /* Lowercase all letters if -i is specified. */
446 column
+= c1
== '\t' ? tabsize
- column
% tabsize
: 1;
452 /* Find the consecutive changes at the start of the script START.
453 Return the last link before the first gap. */
456 find_change (struct change
*start
)
462 find_reverse_change (struct change
*start
)
467 /* Divide SCRIPT into pieces by calling HUNKFUN and
468 print each piece with PRINTFUN.
469 Both functions take one arg, an edit script.
471 HUNKFUN is called with the tail of the script
472 and returns the last link that belongs together with the start
475 PRINTFUN takes a subscript which belongs together (with a null
476 link at the end) and prints it. */
479 print_script (struct change
*script
,
480 struct change
* (*hunkfun
) (struct change
*),
481 void (*printfun
) (struct change
*))
483 struct change
*next
= script
;
487 struct change
*this, *end
;
489 /* Find a set of changes that belong together. */
491 end
= (*hunkfun
) (next
);
493 /* Disconnect them from the rest of the changes,
494 making them a hunk, and remember the rest for next iteration. */
501 /* Print this hunk. */
504 /* Reconnect the script so it will all be freed properly. */
509 /* Print the text of a single line LINE,
510 flagging it with the characters in LINE_FLAG (which say whether
511 the line is inserted, deleted, changed, etc.). */
514 print_1_line (char const *line_flag
, char const *const *line
)
516 char const *base
= line
[0], *limit
= line
[1]; /* Help the compiler. */
517 FILE *out
= outfile
; /* Help the compiler some more. */
518 char const *flag_format
= 0;
520 /* If -T was specified, use a Tab between the line-flag and the text.
521 Otherwise use a Space (as Unix diff does).
522 Print neither space nor tab if line-flags are empty. */
524 if (line_flag
&& *line_flag
)
526 flag_format
= initial_tab
? "%s\t" : "%s ";
527 fprintf (out
, flag_format
, line_flag
);
530 output_1_line (base
, limit
, flag_format
, line_flag
);
532 if ((!line_flag
|| line_flag
[0]) && limit
[-1] != '\n')
533 fprintf (out
, "\n\\ %s\n", _("No newline at end of file"));
536 /* Output a line from BASE up to LIMIT.
537 With -t, expand white space characters to spaces, and if FLAG_FORMAT
538 is nonzero, output it with argument LINE_FLAG after every
539 internal carriage return, so that tab stops continue to line up. */
542 output_1_line (char const *base
, char const *limit
, char const *flag_format
,
543 char const *line_flag
)
546 fwrite (base
, sizeof (char), limit
- base
, outfile
);
549 register FILE *out
= outfile
;
550 register unsigned char c
;
551 register char const *t
= base
;
552 register size_t column
= 0;
553 size_t tab_size
= tabsize
;
560 size_t spaces
= tab_size
- column
% tab_size
;
570 if (flag_format
&& t
< limit
&& *t
!= '\n')
571 fprintf (out
, flag_format
, line_flag
);
583 column
+= isprint (c
) != 0;
590 char const change_letter
[] = { 0, 'd', 'a', 'c' };
592 /* Translate an internal line number (an index into diff's table of lines)
593 into an actual line number in the input file.
594 The internal line number is I. FILE points to the data on the file.
596 Internal line numbers count from 0 starting after the prefix.
597 Actual line numbers count from 1 within the entire file. */
600 translate_line_number (struct file_data
const *file
, lin i
)
602 return i
+ file
->prefix_lines
+ 1;
605 /* Translate a line number range. This is always done for printing,
606 so for convenience translate to long int rather than lin, so that the
607 caller can use printf with "%ld" without casting. */
610 translate_range (struct file_data
const *file
,
612 long int *aptr
, long int *bptr
)
614 *aptr
= translate_line_number (file
, a
- 1) + 1;
615 *bptr
= translate_line_number (file
, b
+ 1) - 1;
618 /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
619 If the two numbers are identical, print just one number.
621 Args A and B are internal line numbers.
622 We print the translated (real) line numbers. */
625 print_number_range (char sepchar
, struct file_data
*file
, lin a
, lin b
)
627 long int trans_a
, trans_b
;
628 translate_range (file
, a
, b
, &trans_a
, &trans_b
);
630 /* Note: we can have B < A in the case of a range of no lines.
631 In this case, we should print the line number before the range,
633 if (trans_b
> trans_a
)
634 fprintf (outfile
, "%ld%c%ld", trans_a
, sepchar
, trans_b
);
636 fprintf (outfile
, "%ld", trans_b
);
639 /* Look at a hunk of edit script and report the range of lines in each file
640 that it applies to. HUNK is the start of the hunk, which is a chain
641 of `struct change'. The first and last line numbers of file 0 are stored in
642 *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
643 Note that these are internal line numbers that count from 0.
645 If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
647 Return UNCHANGED if only ignorable lines are inserted or deleted,
648 OLD if lines of file 0 are deleted,
649 NEW if lines of file 1 are inserted,
650 and CHANGED if both kinds of changes are found. */
653 analyze_hunk (struct change
*hunk
,
654 lin
*first0
, lin
*last0
,
655 lin
*first1
, lin
*last1
)
659 lin show_from
, show_to
;
661 bool trivial
= ignore_blank_lines
|| ignore_regexp
.fastmap
;
662 size_t trivial_length
= ignore_blank_lines
- 1;
663 /* If 0, ignore zero-length lines;
664 if SIZE_MAX, do not ignore lines just because of their length. */
665 bool skip_leading_white_space
=
666 (ignore_blank_lines
&& IGNORE_SPACE_CHANGE
<= ignore_white_space
);
668 char const * const *linbuf0
= files
[0].linbuf
; /* Help the compiler. */
669 char const * const *linbuf1
= files
[1].linbuf
;
671 show_from
= show_to
= 0;
673 *first0
= hunk
->line0
;
674 *first1
= hunk
->line1
;
679 l0
= next
->line0
+ next
->deleted
- 1;
680 l1
= next
->line1
+ next
->inserted
- 1;
681 show_from
+= next
->deleted
;
682 show_to
+= next
->inserted
;
684 for (i
= next
->line0
; i
<= l0
&& trivial
; i
++)
686 char const *line
= linbuf0
[i
];
687 char const *newline
= linbuf0
[i
+ 1] - 1;
688 size_t len
= newline
- line
;
689 char const *p
= line
;
690 if (skip_leading_white_space
)
691 while (isspace ((unsigned char) *p
) && *p
!= '\n')
693 if (newline
- p
!= trivial_length
694 && (! ignore_regexp
.fastmap
695 || re_search (&ignore_regexp
, line
, len
, 0, len
, 0) < 0))
699 for (i
= next
->line1
; i
<= l1
&& trivial
; i
++)
701 char const *line
= linbuf1
[i
];
702 char const *newline
= linbuf1
[i
+ 1] - 1;
703 size_t len
= newline
- line
;
704 char const *p
= line
;
705 if (skip_leading_white_space
)
706 while (isspace ((unsigned char) *p
) && *p
!= '\n')
708 if (newline
- p
!= trivial_length
709 && (! ignore_regexp
.fastmap
710 || re_search (&ignore_regexp
, line
, len
, 0, len
, 0) < 0))
714 while ((next
= next
->link
) != 0);
719 /* If all inserted or deleted lines are ignorable,
720 tell the caller to ignore this hunk. */
725 return (show_from
? OLD
: UNCHANGED
) | (show_to
? NEW
: UNCHANGED
);
728 /* Concatenate three strings, returning a newly malloc'd string. */
731 concat (char const *s1
, char const *s2
, char const *s3
)
733 char *new = xmalloc (strlen (s1
) + strlen (s2
) + strlen (s3
) + 1);
734 sprintf (new, "%s%s%s", s1
, s2
, s3
);
738 /* Yield a new block of SIZE bytes, initialized to zero. */
743 void *p
= xmalloc (size
);
748 /* Yield the newly malloc'd pathname
749 of the file in DIR whose filename is FILE. */
752 dir_file_pathname (char const *dir
, char const *file
)
754 char const *base
= base_name (dir
);
755 bool omit_slash
= !*base
|| base
[strlen (base
) - 1] == '/';
756 return concat (dir
, "/" + omit_slash
, file
);
760 debug_script (struct change
*sp
)
764 for (; sp
; sp
= sp
->link
)
766 long int line0
= sp
->line0
;
767 long int line1
= sp
->line1
;
768 long int deleted
= sp
->deleted
;
769 long int inserted
= sp
->inserted
;
770 fprintf (stderr
, "%3ld %3ld delete %ld insert %ld\n",
771 line0
, line1
, deleted
, inserted
);