xref: /freebsd-src/contrib/diff/src/util.c (revision 18fd37a72c3a7549d2d4f6c6ea00bdcd2bdaca01)
1*18fd37a7SXin LI /* Support routines for GNU DIFF.
2*18fd37a7SXin LI 
3*18fd37a7SXin LI    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002,
4*18fd37a7SXin LI    2004 Free Software Foundation, Inc.
5*18fd37a7SXin LI 
6*18fd37a7SXin LI    This file is part of GNU DIFF.
7*18fd37a7SXin LI 
8*18fd37a7SXin LI    GNU DIFF is free software; you can redistribute it and/or modify
9*18fd37a7SXin LI    it under the terms of the GNU General Public License as published by
10*18fd37a7SXin LI    the Free Software Foundation; either version 2, or (at your option)
11*18fd37a7SXin LI    any later version.
12*18fd37a7SXin LI 
13*18fd37a7SXin LI    GNU DIFF is distributed in the hope that it will be useful,
14*18fd37a7SXin LI    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*18fd37a7SXin LI    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*18fd37a7SXin LI    GNU General Public License for more details.
17*18fd37a7SXin LI 
18*18fd37a7SXin LI    You should have received a copy of the GNU General Public License
19*18fd37a7SXin LI    along with this program; see the file COPYING.
20*18fd37a7SXin LI    If not, write to the Free Software Foundation,
21*18fd37a7SXin LI    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22*18fd37a7SXin LI 
23*18fd37a7SXin LI #include "diff.h"
24*18fd37a7SXin LI #include <dirname.h>
25*18fd37a7SXin LI #include <error.h>
26*18fd37a7SXin LI #include <quotesys.h>
27*18fd37a7SXin LI #include <xalloc.h>
28*18fd37a7SXin LI 
29*18fd37a7SXin LI char const pr_program[] = PR_PROGRAM;
30*18fd37a7SXin LI 
31*18fd37a7SXin LI /* Queue up one-line messages to be printed at the end,
32*18fd37a7SXin LI    when -l is specified.  Each message is recorded with a `struct msg'.  */
33*18fd37a7SXin LI 
34*18fd37a7SXin LI struct msg
35*18fd37a7SXin LI {
36*18fd37a7SXin LI   struct msg *next;
37*18fd37a7SXin LI   char args[1]; /* Format + 4 args, each '\0' terminated, concatenated.  */
38*18fd37a7SXin LI };
39*18fd37a7SXin LI 
40*18fd37a7SXin LI /* Head of the chain of queues messages.  */
41*18fd37a7SXin LI 
42*18fd37a7SXin LI static struct msg *msg_chain;
43*18fd37a7SXin LI 
44*18fd37a7SXin LI /* Tail of the chain of queues messages.  */
45*18fd37a7SXin LI 
46*18fd37a7SXin LI static struct msg **msg_chain_end = &msg_chain;
47*18fd37a7SXin LI 
48*18fd37a7SXin LI /* Use when a system call returns non-zero status.
49*18fd37a7SXin LI    NAME should normally be the file name.  */
50*18fd37a7SXin LI 
51*18fd37a7SXin LI void
perror_with_name(char const * name)52*18fd37a7SXin LI perror_with_name (char const *name)
53*18fd37a7SXin LI {
54*18fd37a7SXin LI   error (0, errno, "%s", name);
55*18fd37a7SXin LI }
56*18fd37a7SXin LI 
57*18fd37a7SXin LI /* Use when a system call returns non-zero status and that is fatal.  */
58*18fd37a7SXin LI 
59*18fd37a7SXin LI void
pfatal_with_name(char const * name)60*18fd37a7SXin LI pfatal_with_name (char const *name)
61*18fd37a7SXin LI {
62*18fd37a7SXin LI   int e = errno;
63*18fd37a7SXin LI   print_message_queue ();
64*18fd37a7SXin LI   error (EXIT_TROUBLE, e, "%s", name);
65*18fd37a7SXin LI   abort ();
66*18fd37a7SXin LI }
67*18fd37a7SXin LI 
68*18fd37a7SXin LI /* Print an error message containing MSGID, then exit.  */
69*18fd37a7SXin LI 
70*18fd37a7SXin LI void
fatal(char const * msgid)71*18fd37a7SXin LI fatal (char const *msgid)
72*18fd37a7SXin LI {
73*18fd37a7SXin LI   print_message_queue ();
74*18fd37a7SXin LI   error (EXIT_TROUBLE, 0, "%s", _(msgid));
75*18fd37a7SXin LI   abort ();
76*18fd37a7SXin LI }
77*18fd37a7SXin LI 
78*18fd37a7SXin LI /* Like printf, except if -l in effect then save the message and print later.
79*18fd37a7SXin LI    This is used for things like "Only in ...".  */
80*18fd37a7SXin LI 
81*18fd37a7SXin LI void
message(char const * format_msgid,char const * arg1,char const * arg2)82*18fd37a7SXin LI message (char const *format_msgid, char const *arg1, char const *arg2)
83*18fd37a7SXin LI {
84*18fd37a7SXin LI   message5 (format_msgid, arg1, arg2, 0, 0);
85*18fd37a7SXin LI }
86*18fd37a7SXin LI 
87*18fd37a7SXin LI void
message5(char const * format_msgid,char const * arg1,char const * arg2,char const * arg3,char const * arg4)88*18fd37a7SXin LI message5 (char const *format_msgid, char const *arg1, char const *arg2,
89*18fd37a7SXin LI 	  char const *arg3, char const *arg4)
90*18fd37a7SXin LI {
91*18fd37a7SXin LI   if (paginate)
92*18fd37a7SXin LI     {
93*18fd37a7SXin LI       char *p;
94*18fd37a7SXin LI       char const *arg[5];
95*18fd37a7SXin LI       int i;
96*18fd37a7SXin LI       size_t size[5];
97*18fd37a7SXin LI       size_t total_size = offsetof (struct msg, args);
98*18fd37a7SXin LI       struct msg *new;
99*18fd37a7SXin LI 
100*18fd37a7SXin LI       arg[0] = format_msgid;
101*18fd37a7SXin LI       arg[1] = arg1;
102*18fd37a7SXin LI       arg[2] = arg2;
103*18fd37a7SXin LI       arg[3] = arg3 ? arg3 : "";
104*18fd37a7SXin LI       arg[4] = arg4 ? arg4 : "";
105*18fd37a7SXin LI 
106*18fd37a7SXin LI       for (i = 0;  i < 5;  i++)
107*18fd37a7SXin LI 	total_size += size[i] = strlen (arg[i]) + 1;
108*18fd37a7SXin LI 
109*18fd37a7SXin LI       new = xmalloc (total_size);
110*18fd37a7SXin LI 
111*18fd37a7SXin LI       for (i = 0, p = new->args;  i < 5;  p += size[i++])
112*18fd37a7SXin LI 	memcpy (p, arg[i], size[i]);
113*18fd37a7SXin LI 
114*18fd37a7SXin LI       *msg_chain_end = new;
115*18fd37a7SXin LI       new->next = 0;
116*18fd37a7SXin LI       msg_chain_end = &new->next;
117*18fd37a7SXin LI     }
118*18fd37a7SXin LI   else
119*18fd37a7SXin LI     {
120*18fd37a7SXin LI       if (sdiff_merge_assist)
121*18fd37a7SXin LI 	putchar (' ');
122*18fd37a7SXin LI       printf (_(format_msgid), arg1, arg2, arg3, arg4);
123*18fd37a7SXin LI     }
124*18fd37a7SXin LI }
125*18fd37a7SXin LI 
126*18fd37a7SXin LI /* Output all the messages that were saved up by calls to `message'.  */
127*18fd37a7SXin LI 
128*18fd37a7SXin LI void
print_message_queue(void)129*18fd37a7SXin LI print_message_queue (void)
130*18fd37a7SXin LI {
131*18fd37a7SXin LI   char const *arg[5];
132*18fd37a7SXin LI   int i;
133*18fd37a7SXin LI   struct msg *m = msg_chain;
134*18fd37a7SXin LI 
135*18fd37a7SXin LI   while (m)
136*18fd37a7SXin LI     {
137*18fd37a7SXin LI       struct msg *next = m->next;
138*18fd37a7SXin LI       arg[0] = m->args;
139*18fd37a7SXin LI       for (i = 0;  i < 4;  i++)
140*18fd37a7SXin LI 	arg[i + 1] = arg[i] + strlen (arg[i]) + 1;
141*18fd37a7SXin LI       printf (_(arg[0]), arg[1], arg[2], arg[3], arg[4]);
142*18fd37a7SXin LI       free (m);
143*18fd37a7SXin LI       m = next;
144*18fd37a7SXin LI     }
145*18fd37a7SXin LI }
146*18fd37a7SXin LI 
147*18fd37a7SXin LI /* Call before outputting the results of comparing files NAME0 and NAME1
148*18fd37a7SXin LI    to set up OUTFILE, the stdio stream for the output to go to.
149*18fd37a7SXin LI 
150*18fd37a7SXin LI    Usually, OUTFILE is just stdout.  But when -l was specified
151*18fd37a7SXin LI    we fork off a `pr' and make OUTFILE a pipe to it.
152*18fd37a7SXin LI    `pr' then outputs to our stdout.  */
153*18fd37a7SXin LI 
154*18fd37a7SXin LI static char const *current_name0;
155*18fd37a7SXin LI static char const *current_name1;
156*18fd37a7SXin LI static bool currently_recursive;
157*18fd37a7SXin LI 
158*18fd37a7SXin LI void
setup_output(char const * name0,char const * name1,bool recursive)159*18fd37a7SXin LI setup_output (char const *name0, char const *name1, bool recursive)
160*18fd37a7SXin LI {
161*18fd37a7SXin LI   current_name0 = name0;
162*18fd37a7SXin LI   current_name1 = name1;
163*18fd37a7SXin LI   currently_recursive = recursive;
164*18fd37a7SXin LI   outfile = 0;
165*18fd37a7SXin LI }
166*18fd37a7SXin LI 
167*18fd37a7SXin LI #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
168*18fd37a7SXin LI static pid_t pr_pid;
169*18fd37a7SXin LI #endif
170*18fd37a7SXin LI 
171*18fd37a7SXin LI void
begin_output(void)172*18fd37a7SXin LI begin_output (void)
173*18fd37a7SXin LI {
174*18fd37a7SXin LI   char *name;
175*18fd37a7SXin LI 
176*18fd37a7SXin LI   if (outfile != 0)
177*18fd37a7SXin LI     return;
178*18fd37a7SXin LI 
179*18fd37a7SXin LI   /* Construct the header of this piece of diff.  */
180*18fd37a7SXin LI   name = xmalloc (strlen (current_name0) + strlen (current_name1)
181*18fd37a7SXin LI 		  + strlen (switch_string) + 7);
182*18fd37a7SXin LI 
183*18fd37a7SXin LI   /* POSIX 1003.1-2001 specifies this format.  But there are some bugs in
184*18fd37a7SXin LI      the standard: it says that we must print only the last component
185*18fd37a7SXin LI      of the pathnames, and it requires two spaces after "diff" if
186*18fd37a7SXin LI      there are no options.  These requirements are silly and do not
187*18fd37a7SXin LI      match historical practice.  */
188*18fd37a7SXin LI   sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
189*18fd37a7SXin LI 
190*18fd37a7SXin LI   if (paginate)
191*18fd37a7SXin LI     {
192*18fd37a7SXin LI       if (fflush (stdout) != 0)
193*18fd37a7SXin LI 	pfatal_with_name (_("write failed"));
194*18fd37a7SXin LI 
195*18fd37a7SXin LI       /* Make OUTFILE a pipe to a subsidiary `pr'.  */
196*18fd37a7SXin LI       {
197*18fd37a7SXin LI #if HAVE_WORKING_FORK || HAVE_WORKING_VFORK
198*18fd37a7SXin LI 	int pipes[2];
199*18fd37a7SXin LI 
200*18fd37a7SXin LI 	if (pipe (pipes) != 0)
201*18fd37a7SXin LI 	  pfatal_with_name ("pipe");
202*18fd37a7SXin LI 
203*18fd37a7SXin LI 	pr_pid = vfork ();
204*18fd37a7SXin LI 	if (pr_pid < 0)
205*18fd37a7SXin LI 	  pfatal_with_name ("fork");
206*18fd37a7SXin LI 
207*18fd37a7SXin LI 	if (pr_pid == 0)
208*18fd37a7SXin LI 	  {
209*18fd37a7SXin LI 	    close (pipes[1]);
210*18fd37a7SXin LI 	    if (pipes[0] != STDIN_FILENO)
211*18fd37a7SXin LI 	      {
212*18fd37a7SXin LI 		if (dup2 (pipes[0], STDIN_FILENO) < 0)
213*18fd37a7SXin LI 		  pfatal_with_name ("dup2");
214*18fd37a7SXin LI 		close (pipes[0]);
215*18fd37a7SXin LI 	      }
216*18fd37a7SXin LI 
217*18fd37a7SXin LI 	    execl (pr_program, pr_program, "-h", name, (char *) 0);
218*18fd37a7SXin LI 	    _exit (errno == ENOENT ? 127 : 126);
219*18fd37a7SXin LI 	  }
220*18fd37a7SXin LI 	else
221*18fd37a7SXin LI 	  {
222*18fd37a7SXin LI 	    close (pipes[0]);
223*18fd37a7SXin LI 	    outfile = fdopen (pipes[1], "w");
224*18fd37a7SXin LI 	    if (!outfile)
225*18fd37a7SXin LI 	      pfatal_with_name ("fdopen");
226*18fd37a7SXin LI 	  }
227*18fd37a7SXin LI #else
228*18fd37a7SXin LI 	char *command = xmalloc (sizeof pr_program - 1 + 7
229*18fd37a7SXin LI 				 + quote_system_arg ((char *) 0, name) + 1);
230*18fd37a7SXin LI 	char *p;
231*18fd37a7SXin LI 	sprintf (command, "%s -f -h ", pr_program);
232*18fd37a7SXin LI 	p = command + sizeof pr_program - 1 + 7;
233*18fd37a7SXin LI 	p += quote_system_arg (p, name);
234*18fd37a7SXin LI 	*p = 0;
235*18fd37a7SXin LI 	errno = 0;
236*18fd37a7SXin LI 	outfile = popen (command, "w");
237*18fd37a7SXin LI 	if (!outfile)
238*18fd37a7SXin LI 	  pfatal_with_name (command);
239*18fd37a7SXin LI 	free (command);
240*18fd37a7SXin LI #endif
241*18fd37a7SXin LI       }
242*18fd37a7SXin LI     }
243*18fd37a7SXin LI   else
244*18fd37a7SXin LI     {
245*18fd37a7SXin LI 
246*18fd37a7SXin LI       /* If -l was not specified, output the diff straight to `stdout'.  */
247*18fd37a7SXin LI 
248*18fd37a7SXin LI       outfile = stdout;
249*18fd37a7SXin LI 
250*18fd37a7SXin LI       /* If handling multiple files (because scanning a directory),
251*18fd37a7SXin LI 	 print which files the following output is about.  */
252*18fd37a7SXin LI       if (currently_recursive)
253*18fd37a7SXin LI 	printf ("%s\n", name);
254*18fd37a7SXin LI     }
255*18fd37a7SXin LI 
256*18fd37a7SXin LI   free (name);
257*18fd37a7SXin LI 
258*18fd37a7SXin LI   /* A special header is needed at the beginning of context output.  */
259*18fd37a7SXin LI   switch (output_style)
260*18fd37a7SXin LI     {
261*18fd37a7SXin LI     case OUTPUT_CONTEXT:
262*18fd37a7SXin LI       print_context_header (files, false);
263*18fd37a7SXin LI       break;
264*18fd37a7SXin LI 
265*18fd37a7SXin LI     case OUTPUT_UNIFIED:
266*18fd37a7SXin LI       print_context_header (files, true);
267*18fd37a7SXin LI       break;
268*18fd37a7SXin LI 
269*18fd37a7SXin LI     default:
270*18fd37a7SXin LI       break;
271*18fd37a7SXin LI     }
272*18fd37a7SXin LI }
273*18fd37a7SXin LI 
274*18fd37a7SXin LI /* Call after the end of output of diffs for one file.
275*18fd37a7SXin LI    Close OUTFILE and get rid of the `pr' subfork.  */
276*18fd37a7SXin LI 
277*18fd37a7SXin LI void
finish_output(void)278*18fd37a7SXin LI finish_output (void)
279*18fd37a7SXin LI {
280*18fd37a7SXin LI   if (outfile != 0 && outfile != stdout)
281*18fd37a7SXin LI     {
282*18fd37a7SXin LI       int status;
283*18fd37a7SXin LI       int wstatus;
284*18fd37a7SXin LI       int werrno = 0;
285*18fd37a7SXin LI       if (ferror (outfile))
286*18fd37a7SXin LI 	fatal ("write failed");
287*18fd37a7SXin LI #if ! (HAVE_WORKING_FORK || HAVE_WORKING_VFORK)
288*18fd37a7SXin LI       wstatus = pclose (outfile);
289*18fd37a7SXin LI       if (wstatus == -1)
290*18fd37a7SXin LI 	werrno = errno;
291*18fd37a7SXin LI #else
292*18fd37a7SXin LI       if (fclose (outfile) != 0)
293*18fd37a7SXin LI 	pfatal_with_name (_("write failed"));
294*18fd37a7SXin LI       if (waitpid (pr_pid, &wstatus, 0) < 0)
295*18fd37a7SXin LI 	pfatal_with_name ("waitpid");
296*18fd37a7SXin LI #endif
297*18fd37a7SXin LI       status = (! werrno && WIFEXITED (wstatus)
298*18fd37a7SXin LI 		? WEXITSTATUS (wstatus)
299*18fd37a7SXin LI 		: INT_MAX);
300*18fd37a7SXin LI       if (status)
301*18fd37a7SXin LI 	error (EXIT_TROUBLE, werrno,
302*18fd37a7SXin LI 	       _(status == 126
303*18fd37a7SXin LI 		 ? "subsidiary program `%s' could not be invoked"
304*18fd37a7SXin LI 		 : status == 127
305*18fd37a7SXin LI 		 ? "subsidiary program `%s' not found"
306*18fd37a7SXin LI 		 : status == INT_MAX
307*18fd37a7SXin LI 		 ? "subsidiary program `%s' failed"
308*18fd37a7SXin LI 		 : "subsidiary program `%s' failed (exit status %d)"),
309*18fd37a7SXin LI 	       pr_program, status);
310*18fd37a7SXin LI     }
311*18fd37a7SXin LI 
312*18fd37a7SXin LI   outfile = 0;
313*18fd37a7SXin LI }
314*18fd37a7SXin LI 
315*18fd37a7SXin LI /* Compare two lines (typically one from each input file)
316*18fd37a7SXin LI    according to the command line options.
317*18fd37a7SXin LI    For efficiency, this is invoked only when the lines do not match exactly
318*18fd37a7SXin LI    but an option like -i might cause us to ignore the difference.
319*18fd37a7SXin LI    Return nonzero if the lines differ.  */
320*18fd37a7SXin LI 
321*18fd37a7SXin LI bool
lines_differ(char const * s1,char const * s2)322*18fd37a7SXin LI lines_differ (char const *s1, char const *s2)
323*18fd37a7SXin LI {
324*18fd37a7SXin LI   register char const *t1 = s1;
325*18fd37a7SXin LI   register char const *t2 = s2;
326*18fd37a7SXin LI   size_t column = 0;
327*18fd37a7SXin LI 
328*18fd37a7SXin LI   while (1)
329*18fd37a7SXin LI     {
330*18fd37a7SXin LI       register unsigned char c1 = *t1++;
331*18fd37a7SXin LI       register unsigned char c2 = *t2++;
332*18fd37a7SXin LI 
333*18fd37a7SXin LI       /* Test for exact char equality first, since it's a common case.  */
334*18fd37a7SXin LI       if (c1 != c2)
335*18fd37a7SXin LI 	{
336*18fd37a7SXin LI 	  switch (ignore_white_space)
337*18fd37a7SXin LI 	    {
338*18fd37a7SXin LI 	    case IGNORE_ALL_SPACE:
339*18fd37a7SXin LI 	      /* For -w, just skip past any white space.  */
340*18fd37a7SXin LI 	      while (isspace (c1) && c1 != '\n') c1 = *t1++;
341*18fd37a7SXin LI 	      while (isspace (c2) && c2 != '\n') c2 = *t2++;
342*18fd37a7SXin LI 	      break;
343*18fd37a7SXin LI 
344*18fd37a7SXin LI 	    case IGNORE_SPACE_CHANGE:
345*18fd37a7SXin LI 	      /* For -b, advance past any sequence of white space in
346*18fd37a7SXin LI 		 line 1 and consider it just one space, or nothing at
347*18fd37a7SXin LI 		 all if it is at the end of the line.  */
348*18fd37a7SXin LI 	      if (isspace (c1))
349*18fd37a7SXin LI 		{
350*18fd37a7SXin LI 		  while (c1 != '\n')
351*18fd37a7SXin LI 		    {
352*18fd37a7SXin LI 		      c1 = *t1++;
353*18fd37a7SXin LI 		      if (! isspace (c1))
354*18fd37a7SXin LI 			{
355*18fd37a7SXin LI 			  --t1;
356*18fd37a7SXin LI 			  c1 = ' ';
357*18fd37a7SXin LI 			  break;
358*18fd37a7SXin LI 			}
359*18fd37a7SXin LI 		    }
360*18fd37a7SXin LI 		}
361*18fd37a7SXin LI 
362*18fd37a7SXin LI 	      /* Likewise for line 2.  */
363*18fd37a7SXin LI 	      if (isspace (c2))
364*18fd37a7SXin LI 		{
365*18fd37a7SXin LI 		  while (c2 != '\n')
366*18fd37a7SXin LI 		    {
367*18fd37a7SXin LI 		      c2 = *t2++;
368*18fd37a7SXin LI 		      if (! isspace (c2))
369*18fd37a7SXin LI 			{
370*18fd37a7SXin LI 			  --t2;
371*18fd37a7SXin LI 			  c2 = ' ';
372*18fd37a7SXin LI 			  break;
373*18fd37a7SXin LI 			}
374*18fd37a7SXin LI 		    }
375*18fd37a7SXin LI 		}
376*18fd37a7SXin LI 
377*18fd37a7SXin LI 	      if (c1 != c2)
378*18fd37a7SXin LI 		{
379*18fd37a7SXin LI 		  /* If we went too far when doing the simple test
380*18fd37a7SXin LI 		     for equality, go back to the first non-white-space
381*18fd37a7SXin LI 		     character in both sides and try again.  */
382*18fd37a7SXin LI 		  if (c2 == ' ' && c1 != '\n'
383*18fd37a7SXin LI 		      && s1 + 1 < t1
384*18fd37a7SXin LI 		      && isspace ((unsigned char) t1[-2]))
385*18fd37a7SXin LI 		    {
386*18fd37a7SXin LI 		      --t1;
387*18fd37a7SXin LI 		      continue;
388*18fd37a7SXin LI 		    }
389*18fd37a7SXin LI 		  if (c1 == ' ' && c2 != '\n'
390*18fd37a7SXin LI 		      && s2 + 1 < t2
391*18fd37a7SXin LI 		      && isspace ((unsigned char) t2[-2]))
392*18fd37a7SXin LI 		    {
393*18fd37a7SXin LI 		      --t2;
394*18fd37a7SXin LI 		      continue;
395*18fd37a7SXin LI 		    }
396*18fd37a7SXin LI 		}
397*18fd37a7SXin LI 
398*18fd37a7SXin LI 	      break;
399*18fd37a7SXin LI 
400*18fd37a7SXin LI 	    case IGNORE_TAB_EXPANSION:
401*18fd37a7SXin LI 	      if ((c1 == ' ' && c2 == '\t')
402*18fd37a7SXin LI 		  || (c1 == '\t' && c2 == ' '))
403*18fd37a7SXin LI 		{
404*18fd37a7SXin LI 		  size_t column2 = column;
405*18fd37a7SXin LI 		  for (;; c1 = *t1++)
406*18fd37a7SXin LI 		    {
407*18fd37a7SXin LI 		      if (c1 == ' ')
408*18fd37a7SXin LI 			column++;
409*18fd37a7SXin LI 		      else if (c1 == '\t')
410*18fd37a7SXin LI 			column += tabsize - column % tabsize;
411*18fd37a7SXin LI 		      else
412*18fd37a7SXin LI 			break;
413*18fd37a7SXin LI 		    }
414*18fd37a7SXin LI 		  for (;; c2 = *t2++)
415*18fd37a7SXin LI 		    {
416*18fd37a7SXin LI 		      if (c2 == ' ')
417*18fd37a7SXin LI 			column2++;
418*18fd37a7SXin LI 		      else if (c2 == '\t')
419*18fd37a7SXin LI 			column2 += tabsize - column2 % tabsize;
420*18fd37a7SXin LI 		      else
421*18fd37a7SXin LI 			break;
422*18fd37a7SXin LI 		    }
423*18fd37a7SXin LI 		  if (column != column2)
424*18fd37a7SXin LI 		    return true;
425*18fd37a7SXin LI 		}
426*18fd37a7SXin LI 	      break;
427*18fd37a7SXin LI 
428*18fd37a7SXin LI 	    case IGNORE_NO_WHITE_SPACE:
429*18fd37a7SXin LI 	      break;
430*18fd37a7SXin LI 	    }
431*18fd37a7SXin LI 
432*18fd37a7SXin LI 	  /* Lowercase all letters if -i is specified.  */
433*18fd37a7SXin LI 
434*18fd37a7SXin LI 	  if (ignore_case)
435*18fd37a7SXin LI 	    {
436*18fd37a7SXin LI 	      c1 = tolower (c1);
437*18fd37a7SXin LI 	      c2 = tolower (c2);
438*18fd37a7SXin LI 	    }
439*18fd37a7SXin LI 
440*18fd37a7SXin LI 	  if (c1 != c2)
441*18fd37a7SXin LI 	    break;
442*18fd37a7SXin LI 	}
443*18fd37a7SXin LI       if (c1 == '\n')
444*18fd37a7SXin LI 	return false;
445*18fd37a7SXin LI 
446*18fd37a7SXin LI       column += c1 == '\t' ? tabsize - column % tabsize : 1;
447*18fd37a7SXin LI     }
448*18fd37a7SXin LI 
449*18fd37a7SXin LI   return true;
450*18fd37a7SXin LI }
451*18fd37a7SXin LI 
452*18fd37a7SXin LI /* Find the consecutive changes at the start of the script START.
453*18fd37a7SXin LI    Return the last link before the first gap.  */
454*18fd37a7SXin LI 
455*18fd37a7SXin LI struct change *
find_change(struct change * start)456*18fd37a7SXin LI find_change (struct change *start)
457*18fd37a7SXin LI {
458*18fd37a7SXin LI   return start;
459*18fd37a7SXin LI }
460*18fd37a7SXin LI 
461*18fd37a7SXin LI struct change *
find_reverse_change(struct change * start)462*18fd37a7SXin LI find_reverse_change (struct change *start)
463*18fd37a7SXin LI {
464*18fd37a7SXin LI   return start;
465*18fd37a7SXin LI }
466*18fd37a7SXin LI 
467*18fd37a7SXin LI /* Divide SCRIPT into pieces by calling HUNKFUN and
468*18fd37a7SXin LI    print each piece with PRINTFUN.
469*18fd37a7SXin LI    Both functions take one arg, an edit script.
470*18fd37a7SXin LI 
471*18fd37a7SXin LI    HUNKFUN is called with the tail of the script
472*18fd37a7SXin LI    and returns the last link that belongs together with the start
473*18fd37a7SXin LI    of the tail.
474*18fd37a7SXin LI 
475*18fd37a7SXin LI    PRINTFUN takes a subscript which belongs together (with a null
476*18fd37a7SXin LI    link at the end) and prints it.  */
477*18fd37a7SXin LI 
478*18fd37a7SXin LI void
print_script(struct change * script,struct change * (* hunkfun)(struct change *),void (* printfun)(struct change *))479*18fd37a7SXin LI print_script (struct change *script,
480*18fd37a7SXin LI 	      struct change * (*hunkfun) (struct change *),
481*18fd37a7SXin LI 	      void (*printfun) (struct change *))
482*18fd37a7SXin LI {
483*18fd37a7SXin LI   struct change *next = script;
484*18fd37a7SXin LI 
485*18fd37a7SXin LI   while (next)
486*18fd37a7SXin LI     {
487*18fd37a7SXin LI       struct change *this, *end;
488*18fd37a7SXin LI 
489*18fd37a7SXin LI       /* Find a set of changes that belong together.  */
490*18fd37a7SXin LI       this = next;
491*18fd37a7SXin LI       end = (*hunkfun) (next);
492*18fd37a7SXin LI 
493*18fd37a7SXin LI       /* Disconnect them from the rest of the changes,
494*18fd37a7SXin LI 	 making them a hunk, and remember the rest for next iteration.  */
495*18fd37a7SXin LI       next = end->link;
496*18fd37a7SXin LI       end->link = 0;
497*18fd37a7SXin LI #ifdef DEBUG
498*18fd37a7SXin LI       debug_script (this);
499*18fd37a7SXin LI #endif
500*18fd37a7SXin LI 
501*18fd37a7SXin LI       /* Print this hunk.  */
502*18fd37a7SXin LI       (*printfun) (this);
503*18fd37a7SXin LI 
504*18fd37a7SXin LI       /* Reconnect the script so it will all be freed properly.  */
505*18fd37a7SXin LI       end->link = next;
506*18fd37a7SXin LI     }
507*18fd37a7SXin LI }
508*18fd37a7SXin LI 
509*18fd37a7SXin LI /* Print the text of a single line LINE,
510*18fd37a7SXin LI    flagging it with the characters in LINE_FLAG (which say whether
511*18fd37a7SXin LI    the line is inserted, deleted, changed, etc.).  */
512*18fd37a7SXin LI 
513*18fd37a7SXin LI void
print_1_line(char const * line_flag,char const * const * line)514*18fd37a7SXin LI print_1_line (char const *line_flag, char const *const *line)
515*18fd37a7SXin LI {
516*18fd37a7SXin LI   char const *base = line[0], *limit = line[1]; /* Help the compiler.  */
517*18fd37a7SXin LI   FILE *out = outfile; /* Help the compiler some more.  */
518*18fd37a7SXin LI   char const *flag_format = 0;
519*18fd37a7SXin LI 
520*18fd37a7SXin LI   /* If -T was specified, use a Tab between the line-flag and the text.
521*18fd37a7SXin LI      Otherwise use a Space (as Unix diff does).
522*18fd37a7SXin LI      Print neither space nor tab if line-flags are empty.  */
523*18fd37a7SXin LI 
524*18fd37a7SXin LI   if (line_flag && *line_flag)
525*18fd37a7SXin LI     {
526*18fd37a7SXin LI       flag_format = initial_tab ? "%s\t" : "%s ";
527*18fd37a7SXin LI       fprintf (out, flag_format, line_flag);
528*18fd37a7SXin LI     }
529*18fd37a7SXin LI 
530*18fd37a7SXin LI   output_1_line (base, limit, flag_format, line_flag);
531*18fd37a7SXin LI 
532*18fd37a7SXin LI   if ((!line_flag || line_flag[0]) && limit[-1] != '\n')
533*18fd37a7SXin LI     fprintf (out, "\n\\ %s\n", _("No newline at end of file"));
534*18fd37a7SXin LI }
535*18fd37a7SXin LI 
536*18fd37a7SXin LI /* Output a line from BASE up to LIMIT.
537*18fd37a7SXin LI    With -t, expand white space characters to spaces, and if FLAG_FORMAT
538*18fd37a7SXin LI    is nonzero, output it with argument LINE_FLAG after every
539*18fd37a7SXin LI    internal carriage return, so that tab stops continue to line up.  */
540*18fd37a7SXin LI 
541*18fd37a7SXin LI void
output_1_line(char const * base,char const * limit,char const * flag_format,char const * line_flag)542*18fd37a7SXin LI output_1_line (char const *base, char const *limit, char const *flag_format,
543*18fd37a7SXin LI 	       char const *line_flag)
544*18fd37a7SXin LI {
545*18fd37a7SXin LI   if (!expand_tabs)
546*18fd37a7SXin LI     fwrite (base, sizeof (char), limit - base, outfile);
547*18fd37a7SXin LI   else
548*18fd37a7SXin LI     {
549*18fd37a7SXin LI       register FILE *out = outfile;
550*18fd37a7SXin LI       register unsigned char c;
551*18fd37a7SXin LI       register char const *t = base;
552*18fd37a7SXin LI       register size_t column = 0;
553*18fd37a7SXin LI       size_t tab_size = tabsize;
554*18fd37a7SXin LI 
555*18fd37a7SXin LI       while (t < limit)
556*18fd37a7SXin LI 	switch ((c = *t++))
557*18fd37a7SXin LI 	  {
558*18fd37a7SXin LI 	  case '\t':
559*18fd37a7SXin LI 	    {
560*18fd37a7SXin LI 	      size_t spaces = tab_size - column % tab_size;
561*18fd37a7SXin LI 	      column += spaces;
562*18fd37a7SXin LI 	      do
563*18fd37a7SXin LI 		putc (' ', out);
564*18fd37a7SXin LI 	      while (--spaces);
565*18fd37a7SXin LI 	    }
566*18fd37a7SXin LI 	    break;
567*18fd37a7SXin LI 
568*18fd37a7SXin LI 	  case '\r':
569*18fd37a7SXin LI 	    putc (c, out);
570*18fd37a7SXin LI 	    if (flag_format && t < limit && *t != '\n')
571*18fd37a7SXin LI 	      fprintf (out, flag_format, line_flag);
572*18fd37a7SXin LI 	    column = 0;
573*18fd37a7SXin LI 	    break;
574*18fd37a7SXin LI 
575*18fd37a7SXin LI 	  case '\b':
576*18fd37a7SXin LI 	    if (column == 0)
577*18fd37a7SXin LI 	      continue;
578*18fd37a7SXin LI 	    column--;
579*18fd37a7SXin LI 	    putc (c, out);
580*18fd37a7SXin LI 	    break;
581*18fd37a7SXin LI 
582*18fd37a7SXin LI 	  default:
583*18fd37a7SXin LI 	    column += isprint (c) != 0;
584*18fd37a7SXin LI 	    putc (c, out);
585*18fd37a7SXin LI 	    break;
586*18fd37a7SXin LI 	  }
587*18fd37a7SXin LI     }
588*18fd37a7SXin LI }
589*18fd37a7SXin LI 
590*18fd37a7SXin LI char const change_letter[] = { 0, 'd', 'a', 'c' };
591*18fd37a7SXin LI 
592*18fd37a7SXin LI /* Translate an internal line number (an index into diff's table of lines)
593*18fd37a7SXin LI    into an actual line number in the input file.
594*18fd37a7SXin LI    The internal line number is I.  FILE points to the data on the file.
595*18fd37a7SXin LI 
596*18fd37a7SXin LI    Internal line numbers count from 0 starting after the prefix.
597*18fd37a7SXin LI    Actual line numbers count from 1 within the entire file.  */
598*18fd37a7SXin LI 
599*18fd37a7SXin LI lin
translate_line_number(struct file_data const * file,lin i)600*18fd37a7SXin LI translate_line_number (struct file_data const *file, lin i)
601*18fd37a7SXin LI {
602*18fd37a7SXin LI   return i + file->prefix_lines + 1;
603*18fd37a7SXin LI }
604*18fd37a7SXin LI 
605*18fd37a7SXin LI /* Translate a line number range.  This is always done for printing,
606*18fd37a7SXin LI    so for convenience translate to long int rather than lin, so that the
607*18fd37a7SXin LI    caller can use printf with "%ld" without casting.  */
608*18fd37a7SXin LI 
609*18fd37a7SXin LI void
translate_range(struct file_data const * file,lin a,lin b,long int * aptr,long int * bptr)610*18fd37a7SXin LI translate_range (struct file_data const *file,
611*18fd37a7SXin LI 		 lin a, lin b,
612*18fd37a7SXin LI 		 long int *aptr, long int *bptr)
613*18fd37a7SXin LI {
614*18fd37a7SXin LI   *aptr = translate_line_number (file, a - 1) + 1;
615*18fd37a7SXin LI   *bptr = translate_line_number (file, b + 1) - 1;
616*18fd37a7SXin LI }
617*18fd37a7SXin LI 
618*18fd37a7SXin LI /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
619*18fd37a7SXin LI    If the two numbers are identical, print just one number.
620*18fd37a7SXin LI 
621*18fd37a7SXin LI    Args A and B are internal line numbers.
622*18fd37a7SXin LI    We print the translated (real) line numbers.  */
623*18fd37a7SXin LI 
624*18fd37a7SXin LI void
print_number_range(char sepchar,struct file_data * file,lin a,lin b)625*18fd37a7SXin LI print_number_range (char sepchar, struct file_data *file, lin a, lin b)
626*18fd37a7SXin LI {
627*18fd37a7SXin LI   long int trans_a, trans_b;
628*18fd37a7SXin LI   translate_range (file, a, b, &trans_a, &trans_b);
629*18fd37a7SXin LI 
630*18fd37a7SXin LI   /* Note: we can have B < A in the case of a range of no lines.
631*18fd37a7SXin LI      In this case, we should print the line number before the range,
632*18fd37a7SXin LI      which is B.  */
633*18fd37a7SXin LI   if (trans_b > trans_a)
634*18fd37a7SXin LI     fprintf (outfile, "%ld%c%ld", trans_a, sepchar, trans_b);
635*18fd37a7SXin LI   else
636*18fd37a7SXin LI     fprintf (outfile, "%ld", trans_b);
637*18fd37a7SXin LI }
638*18fd37a7SXin LI 
639*18fd37a7SXin LI /* Look at a hunk of edit script and report the range of lines in each file
640*18fd37a7SXin LI    that it applies to.  HUNK is the start of the hunk, which is a chain
641*18fd37a7SXin LI    of `struct change'.  The first and last line numbers of file 0 are stored in
642*18fd37a7SXin LI    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
643*18fd37a7SXin LI    Note that these are internal line numbers that count from 0.
644*18fd37a7SXin LI 
645*18fd37a7SXin LI    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
646*18fd37a7SXin LI 
647*18fd37a7SXin LI    Return UNCHANGED if only ignorable lines are inserted or deleted,
648*18fd37a7SXin LI    OLD if lines of file 0 are deleted,
649*18fd37a7SXin LI    NEW if lines of file 1 are inserted,
650*18fd37a7SXin LI    and CHANGED if both kinds of changes are found. */
651*18fd37a7SXin LI 
652*18fd37a7SXin LI enum changes
analyze_hunk(struct change * hunk,lin * first0,lin * last0,lin * first1,lin * last1)653*18fd37a7SXin LI analyze_hunk (struct change *hunk,
654*18fd37a7SXin LI 	      lin *first0, lin *last0,
655*18fd37a7SXin LI 	      lin *first1, lin *last1)
656*18fd37a7SXin LI {
657*18fd37a7SXin LI   struct change *next;
658*18fd37a7SXin LI   lin l0, l1;
659*18fd37a7SXin LI   lin show_from, show_to;
660*18fd37a7SXin LI   lin i;
661*18fd37a7SXin LI   bool trivial = ignore_blank_lines || ignore_regexp.fastmap;
662*18fd37a7SXin LI   size_t trivial_length = ignore_blank_lines - 1;
663*18fd37a7SXin LI     /* If 0, ignore zero-length lines;
664*18fd37a7SXin LI        if SIZE_MAX, do not ignore lines just because of their length.  */
665*18fd37a7SXin LI   bool skip_leading_white_space =
666*18fd37a7SXin LI     (ignore_blank_lines && IGNORE_SPACE_CHANGE <= ignore_white_space);
667*18fd37a7SXin LI 
668*18fd37a7SXin LI   char const * const *linbuf0 = files[0].linbuf;  /* Help the compiler.  */
669*18fd37a7SXin LI   char const * const *linbuf1 = files[1].linbuf;
670*18fd37a7SXin LI 
671*18fd37a7SXin LI   show_from = show_to = 0;
672*18fd37a7SXin LI 
673*18fd37a7SXin LI   *first0 = hunk->line0;
674*18fd37a7SXin LI   *first1 = hunk->line1;
675*18fd37a7SXin LI 
676*18fd37a7SXin LI   next = hunk;
677*18fd37a7SXin LI   do
678*18fd37a7SXin LI     {
679*18fd37a7SXin LI       l0 = next->line0 + next->deleted - 1;
680*18fd37a7SXin LI       l1 = next->line1 + next->inserted - 1;
681*18fd37a7SXin LI       show_from += next->deleted;
682*18fd37a7SXin LI       show_to += next->inserted;
683*18fd37a7SXin LI 
684*18fd37a7SXin LI       for (i = next->line0; i <= l0 && trivial; i++)
685*18fd37a7SXin LI 	{
686*18fd37a7SXin LI 	  char const *line = linbuf0[i];
687*18fd37a7SXin LI 	  char const *newline = linbuf0[i + 1] - 1;
688*18fd37a7SXin LI 	  size_t len = newline - line;
689*18fd37a7SXin LI 	  char const *p = line;
690*18fd37a7SXin LI 	  if (skip_leading_white_space)
691*18fd37a7SXin LI 	    while (isspace ((unsigned char) *p) && *p != '\n')
692*18fd37a7SXin LI 	      p++;
693*18fd37a7SXin LI 	  if (newline - p != trivial_length
694*18fd37a7SXin LI 	      && (! ignore_regexp.fastmap
695*18fd37a7SXin LI 		  || re_search (&ignore_regexp, line, len, 0, len, 0) < 0))
696*18fd37a7SXin LI 	    trivial = 0;
697*18fd37a7SXin LI 	}
698*18fd37a7SXin LI 
699*18fd37a7SXin LI       for (i = next->line1; i <= l1 && trivial; i++)
700*18fd37a7SXin LI 	{
701*18fd37a7SXin LI 	  char const *line = linbuf1[i];
702*18fd37a7SXin LI 	  char const *newline = linbuf1[i + 1] - 1;
703*18fd37a7SXin LI 	  size_t len = newline - line;
704*18fd37a7SXin LI 	  char const *p = line;
705*18fd37a7SXin LI 	  if (skip_leading_white_space)
706*18fd37a7SXin LI 	    while (isspace ((unsigned char) *p) && *p != '\n')
707*18fd37a7SXin LI 	      p++;
708*18fd37a7SXin LI 	  if (newline - p != trivial_length
709*18fd37a7SXin LI 	      && (! ignore_regexp.fastmap
710*18fd37a7SXin LI 		  || re_search (&ignore_regexp, line, len, 0, len, 0) < 0))
711*18fd37a7SXin LI 	    trivial = 0;
712*18fd37a7SXin LI 	}
713*18fd37a7SXin LI     }
714*18fd37a7SXin LI   while ((next = next->link) != 0);
715*18fd37a7SXin LI 
716*18fd37a7SXin LI   *last0 = l0;
717*18fd37a7SXin LI   *last1 = l1;
718*18fd37a7SXin LI 
719*18fd37a7SXin LI   /* If all inserted or deleted lines are ignorable,
720*18fd37a7SXin LI      tell the caller to ignore this hunk.  */
721*18fd37a7SXin LI 
722*18fd37a7SXin LI   if (trivial)
723*18fd37a7SXin LI     return UNCHANGED;
724*18fd37a7SXin LI 
725*18fd37a7SXin LI   return (show_from ? OLD : UNCHANGED) | (show_to ? NEW : UNCHANGED);
726*18fd37a7SXin LI }
727*18fd37a7SXin LI 
728*18fd37a7SXin LI /* Concatenate three strings, returning a newly malloc'd string.  */
729*18fd37a7SXin LI 
730*18fd37a7SXin LI char *
concat(char const * s1,char const * s2,char const * s3)731*18fd37a7SXin LI concat (char const *s1, char const *s2, char const *s3)
732*18fd37a7SXin LI {
733*18fd37a7SXin LI   char *new = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1);
734*18fd37a7SXin LI   sprintf (new, "%s%s%s", s1, s2, s3);
735*18fd37a7SXin LI   return new;
736*18fd37a7SXin LI }
737*18fd37a7SXin LI 
738*18fd37a7SXin LI /* Yield a new block of SIZE bytes, initialized to zero.  */
739*18fd37a7SXin LI 
740*18fd37a7SXin LI void *
zalloc(size_t size)741*18fd37a7SXin LI zalloc (size_t size)
742*18fd37a7SXin LI {
743*18fd37a7SXin LI   void *p = xmalloc (size);
744*18fd37a7SXin LI   memset (p, 0, size);
745*18fd37a7SXin LI   return p;
746*18fd37a7SXin LI }
747*18fd37a7SXin LI 
748*18fd37a7SXin LI /* Yield the newly malloc'd pathname
749*18fd37a7SXin LI    of the file in DIR whose filename is FILE.  */
750*18fd37a7SXin LI 
751*18fd37a7SXin LI char *
dir_file_pathname(char const * dir,char const * file)752*18fd37a7SXin LI dir_file_pathname (char const *dir, char const *file)
753*18fd37a7SXin LI {
754*18fd37a7SXin LI   char const *base = base_name (dir);
755*18fd37a7SXin LI   bool omit_slash = !*base || base[strlen (base) - 1] == '/';
756*18fd37a7SXin LI   return concat (dir, "/" + omit_slash, file);
757*18fd37a7SXin LI }
758*18fd37a7SXin LI 
759*18fd37a7SXin LI void
debug_script(struct change * sp)760*18fd37a7SXin LI debug_script (struct change *sp)
761*18fd37a7SXin LI {
762*18fd37a7SXin LI   fflush (stdout);
763*18fd37a7SXin LI 
764*18fd37a7SXin LI   for (; sp; sp = sp->link)
765*18fd37a7SXin LI     {
766*18fd37a7SXin LI       long int line0 = sp->line0;
767*18fd37a7SXin LI       long int line1 = sp->line1;
768*18fd37a7SXin LI       long int deleted = sp->deleted;
769*18fd37a7SXin LI       long int inserted = sp->inserted;
770*18fd37a7SXin LI       fprintf (stderr, "%3ld %3ld delete %ld insert %ld\n",
771*18fd37a7SXin LI 	       line0, line1, deleted, inserted);
772*18fd37a7SXin LI     }
773*18fd37a7SXin LI 
774*18fd37a7SXin LI   fflush (stderr);
775*18fd37a7SXin LI }
776