summaryrefslogtreecommitdiff
path: root/src/util.c
blob: 868b7e868bf8d0cd59318735f61c83ca05a28394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/* Support routines for GNU DIFF.

   Copyright (C) 1988-1989, 1992-1995, 1998, 2001-2002, 2004, 2006, 2009-2012
   Free Software Foundation, Inc.

   This file is part of GNU DIFF.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "diff.h"
#include <dirname.h>
#include <error.h>
#include <system-quote.h>
#include <xalloc.h>

char const pr_program[] = PR_PROGRAM;

/* Queue up one-line messages to be printed at the end,
   when -l is specified.  Each message is recorded with a 'struct msg'.  */

struct msg
{
  struct msg *next;
  char args[1]; /* Format + 4 args, each '\0' terminated, concatenated.  */
};

/* Head of the chain of queues messages.  */

static struct msg *msg_chain;

/* Tail of the chain of queues messages.  */

static struct msg **msg_chain_end = &msg_chain;

/* Use when a system call returns non-zero status.
   NAME should normally be the file name.  */

void
perror_with_name (char const *name)
{
  error (0, errno, "%s", name);
}

/* Use when a system call returns non-zero status and that is fatal.  */

void
pfatal_with_name (char const *name)
{
  int e = errno;
  print_message_queue ();
  error (EXIT_TROUBLE, e, "%s", name);
  abort ();
}

/* Print an error message containing MSGID, then exit.  */

void
fatal (char const *msgid)
{
  print_message_queue ();
  error (EXIT_TROUBLE, 0, "%s", _(msgid));
  abort ();
}

/* Like printf, except if -l in effect then save the message and print later.
   This is used for things like "Only in ...".  */

void
message (char const *format_msgid, char const *arg1, char const *arg2)
{
  message5 (format_msgid, arg1, arg2, 0, 0);
}

void
message5 (char const *format_msgid, char const *arg1, char const *arg2,
	  char const *arg3, char const *arg4)
{
  if (paginate)
    {
      char *p;
      char const *arg[5];
      int i;
      size_t size[5];
      size_t total_size = offsetof (struct msg, args);
      struct msg *new;

      arg[0] = format_msgid;
      arg[1] = arg1;
      arg[2] = arg2;
      arg[3] = arg3 ? arg3 : "";
      arg[4] = arg4 ? arg4 : "";

      for (i = 0;  i < 5;  i++)
	total_size += size[i] = strlen (arg[i]) + 1;

      new = xmalloc (total_size);

      for (i = 0, p = new->args;  i < 5;  p += size[i++])
	memcpy (p, arg[i], size[i]);

      *msg_chain_end = new;
      new->next = 0;
      msg_chain_end = &new->next;
    }
  else
    {
      if (sdiff_merge_assist)
	putchar (' ');
      printf (_(format_msgid), arg1, arg2, arg3, arg4);
    }
}

/* Output all the messages that were saved up by calls to 'message'.  */

void
print_message_queue (void)
{
  char const *arg[5];
  int i;
  struct msg *m = msg_chain;

  while (m)
    {
      struct msg *next = m->next;
      arg[0] = m->args;
      for (i = 0;  i < 4;  i++)
	arg[i + 1] = arg[i] + strlen (arg[i]) + 1;
      printf (_(arg[0]), arg[1], arg[2], arg[3], arg[4]);
      free (m);
      m = next;
    }
}

/* Call before outputting the results of comparing files NAME0 and NAME1
   to set up OUTFILE, the stdio stream for the output to go to.

   Usually, OUTFILE is just stdout.  But when -l was specified
   we fork off a 'pr' and make OUTFILE a pipe to it.
   'pr' then outputs to our stdout.  */

static char const *current_name0;
static char const *current_name1;
static bool currently_recursive;

void
setup_output (char const *name0, char const *name1, bool recursive)
{
  current_name0 = name0;
  current_name1 = name1;
  currently_recursive = recursive;
  outfile = 0;
}

#if HAVE_WORKING_FORK
static pid_t pr_pid;
#endif

static char c_escape_char (char c)
{
  switch (c) {
    case '\a': return 'a';
    case '\b': return 'b';
    case '\t': return 't';
    case '\n': return 'n';
    case '\v': return 'v';
    case '\f': return 'f';
    case '\r': return 'r';
    case '"': return '"';
    case '\\': return '\\';
    default:
      return c < 32;
  }
}

static char *
c_escape (char const *str)
{
  char const *s;
  size_t plus = 0;
  bool must_quote = false;

  for (s = str; *s; s++)
    {
      char c = *s;

      if (c == ' ')
	{
	  must_quote = true;
	  continue;
	}
      switch (c_escape_char (*s))
	{
	  case 1:
	    plus += 3;
	    /* fall through */
	  case 0:
	    break;
	  default:
	    plus++;
	    break;
	}
    }

  if (must_quote || plus)
    {
      size_t s_len = s - str;
      char *buffer = xmalloc (s_len + plus + 3);
      char *b = buffer;

      *b++ = '"';
      for (s = str; *s; s++)
	{
	  char c = *s;
	  char escape = c_escape_char (c);

	  switch (escape)
	    {
	      case 0:
		*b++ = c;
		break;
	      case 1:
		*b++ = '\\';
		*b++ = ((c >> 6) & 03) + '0';
		*b++ = ((c >> 3) & 07) + '0';
		*b++ = ((c >> 0) & 07) + '0';
		break;
	      default:
		*b++ = '\\';
		*b++ = escape;
		break;
	    }
	}
      *b++ = '"';
      *b = 0;
      return buffer;
    }

  return (char *) str;
}

void
begin_output (void)
{
  char *names[2];
  char *name;

  if (outfile != 0)
    return;

  names[0] = c_escape (current_name0);
  names[1] = c_escape (current_name1);

  /* Construct the header of this piece of diff.  */
  name = xmalloc (strlen (names[0]) + strlen (names[1]) + strlen (switch_string) + 7);

  /* POSIX 1003.1-2001 specifies this format.  But there are some bugs in
     the standard: it says that we must print only the last component
     of the pathnames, and it requires two spaces after "diff" if
     there are no options.  These requirements are silly and do not
     match historical practice.  */
  sprintf (name, "diff%s %s %s", switch_string, names[0], names[1]);

  if (paginate)
    {
      char const *argv[4];

      if (fflush (stdout) != 0)
	pfatal_with_name (_("write failed"));

      argv[0] = pr_program;
      argv[1] = "-h";
      argv[2] = name;
      argv[3] = 0;

      /* Make OUTFILE a pipe to a subsidiary 'pr'.  */
      {
#if HAVE_WORKING_FORK
	int pipes[2];

	if (pipe (pipes) != 0)
	  pfatal_with_name ("pipe");

	pr_pid = fork ();
	if (pr_pid < 0)
	  pfatal_with_name ("fork");

	if (pr_pid == 0)
	  {
	    close (pipes[1]);
	    if (pipes[0] != STDIN_FILENO)
	      {
		if (dup2 (pipes[0], STDIN_FILENO) < 0)
		  pfatal_with_name ("dup2");
		close (pipes[0]);
	      }

	    execv (pr_program, (char **) argv);
	    _exit (errno == ENOENT ? 127 : 126);
	  }
	else
	  {
	    close (pipes[0]);
	    outfile = fdopen (pipes[1], "w");
	    if (!outfile)
	      pfatal_with_name ("fdopen");
	  }
#else
	char *command = system_quote_argv (SCI_SYSTEM, (char **) argv);
	errno = 0;
	outfile = popen (command, "w");
	if (!outfile)
	  pfatal_with_name (command);
	free (command);
#endif
      }
    }
  else
    {

      /* If -l was not specified, output the diff straight to 'stdout'.  */

      outfile = stdout;

      /* If handling multiple files (because scanning a directory),
	 print which files the following output is about.  */
      if (currently_recursive)
	printf ("%s\n", name);
    }

  free (name);

  /* A special header is needed at the beginning of context output.  */
  switch (output_style)
    {
    case OUTPUT_CONTEXT:
      print_context_header (files, (char const *const *)names, false);
      break;

    case OUTPUT_UNIFIED:
      print_context_header (files, (char const *const *)names, true);
      break;

    default:
      break;
    }

  if (names[0] != current_name0)
    free (names[0]);
  if (names[1] != current_name1)
    free (names[1]);
}

/* Call after the end of output of diffs for one file.
   Close OUTFILE and get rid of the 'pr' subfork.  */

void
finish_output (void)
{
  if (outfile != 0 && outfile != stdout)
    {
      int status;
      int wstatus;
      int werrno = 0;
      if (ferror (outfile))
	fatal ("write failed");
#if ! HAVE_WORKING_FORK
      wstatus = pclose (outfile);
      if (wstatus == -1)
	werrno = errno;
#else
      if (fclose (outfile) != 0)
	pfatal_with_name (_("write failed"));
      if (waitpid (pr_pid, &wstatus, 0) < 0)
	pfatal_with_name ("waitpid");
#endif
      status = (! werrno && WIFEXITED (wstatus)
		? WEXITSTATUS (wstatus)
		: INT_MAX);
      if (status)
	error (EXIT_TROUBLE, werrno,
	       _(status == 126
		 ? "subsidiary program '%s' could not be invoked"
		 : status == 127
		 ? "subsidiary program '%s' not found"
		 : status == INT_MAX
		 ? "subsidiary program '%s' failed"
		 : "subsidiary program '%s' failed (exit status %d)"),
	       pr_program, status);
    }

  outfile = 0;
}

/* Compare two lines (typically one from each input file)
   according to the command line options.
   For efficiency, this is invoked only when the lines do not match exactly
   but an option like -i might cause us to ignore the difference.
   Return nonzero if the lines differ.  */

bool
lines_differ (char const *s1, char const *s2)
{
  register char const *t1 = s1;
  register char const *t2 = s2;
  size_t column = 0;

  while (1)
    {
      register unsigned char c1 = *t1++;
      register unsigned char c2 = *t2++;

      /* Test for exact char equality first, since it's a common case.  */
      if (c1 != c2)
	{
	  switch (ignore_white_space)
	    {
	    case IGNORE_ALL_SPACE:
	      /* For -w, just skip past any white space.  */
	      while (isspace (c1) && c1 != '\n') c1 = *t1++;
	      while (isspace (c2) && c2 != '\n') c2 = *t2++;
	      break;

	    case IGNORE_SPACE_CHANGE:
	      /* For -b, advance past any sequence of white space in
		 line 1 and consider it just one space, or nothing at
		 all if it is at the end of the line.  */
	      if (isspace (c1))
		{
		  while (c1 != '\n')
		    {
		      c1 = *t1++;
		      if (! isspace (c1))
			{
			  --t1;
			  c1 = ' ';
			  break;
			}
		    }
		}

	      /* Likewise for line 2.  */
	      if (isspace (c2))
		{
		  while (c2 != '\n')
		    {
		      c2 = *t2++;
		      if (! isspace (c2))
			{
			  --t2;
			  c2 = ' ';
			  break;
			}
		    }
		}

	      if (c1 != c2)
		{
		  /* If we went too far when doing the simple test
		     for equality, go back to the first non-white-space
		     character in both sides and try again.  */
		  if (c2 == ' ' && c1 != '\n'
		      && s1 + 1 < t1
		      && isspace ((unsigned char) t1[-2]))
		    {
		      --t1;
		      continue;
		    }
		  if (c1 == ' ' && c2 != '\n'
		      && s2 + 1 < t2
		      && isspace ((unsigned char) t2[-2]))
		    {
		      --t2;
		      continue;
		    }
		}

	      break;

	    case IGNORE_TRAILING_SPACE:
	    case IGNORE_TAB_EXPANSION_AND_TRAILING_SPACE:
	      if (isspace (c1) && isspace (c2))
		{
		  unsigned char c;
		  if (c1 != '\n')
		    {
		      char const *p = t1;
		      while ((c = *p) != '\n' && isspace (c))
			++p;
		      if (c != '\n')
			break;
		    }
		  if (c2 != '\n')
		    {
		      char const *p = t2;
		      while ((c = *p) != '\n' && isspace (c))
			++p;
		      if (c != '\n')
			break;
		    }
		  /* Both lines have nothing but whitespace left.  */
		  return false;
		}
	      if (ignore_white_space == IGNORE_TRAILING_SPACE)
		break;
	      /* Fall through.  */
	    case IGNORE_TAB_EXPANSION:
	      if ((c1 == ' ' && c2 == '\t')
		  || (c1 == '\t' && c2 == ' '))
		{
		  size_t column2 = column;
		  for (;; c1 = *t1++)
		    {
		      if (c1 == ' ')
			column++;
		      else if (c1 == '\t')
			column += tabsize - column % tabsize;
		      else
			break;
		    }
		  for (;; c2 = *t2++)
		    {
		      if (c2 == ' ')
			column2++;
		      else if (c2 == '\t')
			column2 += tabsize - column2 % tabsize;
		      else
			break;
		    }
		  if (column != column2)
		    return true;
		}
	      break;

	    case IGNORE_NO_WHITE_SPACE:
	      break;
	    }

	  /* Lowercase all letters if -i is specified.  */

	  if (ignore_case)
	    {
	      c1 = tolower (c1);
	      c2 = tolower (c2);
	    }

	  if (c1 != c2)
	    break;
	}
      if (c1 == '\n')
	return false;

      column += c1 == '\t' ? tabsize - column % tabsize : 1;
    }

  return true;
}

/* Find the consecutive changes at the start of the script START.
   Return the last link before the first gap.  */

struct change * _GL_ATTRIBUTE_CONST
find_change (struct change *start)
{
  return start;
}

struct change * _GL_ATTRIBUTE_CONST
find_reverse_change (struct change *start)
{
  return start;
}

/* Divide SCRIPT into pieces by calling HUNKFUN and
   print each piece with PRINTFUN.
   Both functions take one arg, an edit script.

   HUNKFUN is called with the tail of the script
   and returns the last link that belongs together with the start
   of the tail.

   PRINTFUN takes a subscript which belongs together (with a null
   link at the end) and prints it.  */

void
print_script (struct change *script,
	      struct change * (*hunkfun) (struct change *),
	      void (*printfun) (struct change *))
{
  struct change *next = script;

  while (next)
    {
      struct change *this, *end;

      /* Find a set of changes that belong together.  */
      this = next;
      end = (*hunkfun) (next);

      /* Disconnect them from the rest of the changes,
	 making them a hunk, and remember the rest for next iteration.  */
      next = end->link;
      end->link = 0;
#ifdef DEBUG
      debug_script (this);
#endif

      /* Print this hunk.  */
      (*printfun) (this);

      /* Reconnect the script so it will all be freed properly.  */
      end->link = next;
    }
}

/* Print the text of a single line LINE,
   flagging it with the characters in LINE_FLAG (which say whether
   the line is inserted, deleted, changed, etc.).  LINE_FLAG must not
   end in a blank, unless it is a single blank.  */

void
print_1_line (char const *line_flag, char const *const *line)
{
  char const *base = line[0], *limit = line[1]; /* Help the compiler.  */
  FILE *out = outfile; /* Help the compiler some more.  */
  char const *flag_format = 0;

  /* If -T was specified, use a Tab between the line-flag and the text.
     Otherwise use a Space (as Unix diff does).
     Print neither space nor tab if line-flags are empty.
     But omit trailing blanks if requested.  */

  if (line_flag && *line_flag)
    {
      char const *flag_format_1 = flag_format = initial_tab ? "%s\t" : "%s ";
      char const *line_flag_1 = line_flag;

      if (suppress_blank_empty && **line == '\n')
	{
	  flag_format_1 = "%s";

	  /* This hack to omit trailing blanks takes advantage of the
	     fact that the only way that LINE_FLAG can end in a blank
	     is when LINE_FLAG consists of a single blank.  */
	  line_flag_1 += *line_flag_1 == ' ';
	}

      fprintf (out, flag_format_1, line_flag_1);
    }

  output_1_line (base, limit, flag_format, line_flag);

  if ((!line_flag || line_flag[0]) && limit[-1] != '\n')
    fprintf (out, "\n\\ %s\n", _("No newline at end of file"));
}

/* Output a line from BASE up to LIMIT.
   With -t, expand white space characters to spaces, and if FLAG_FORMAT
   is nonzero, output it with argument LINE_FLAG after every
   internal carriage return, so that tab stops continue to line up.  */

void
output_1_line (char const *base, char const *limit, char const *flag_format,
	       char const *line_flag)
{
  if (!expand_tabs)
    fwrite (base, sizeof (char), limit - base, outfile);
  else
    {
      register FILE *out = outfile;
      register unsigned char c;
      register char const *t = base;
      register size_t column = 0;
      size_t tab_size = tabsize;

      while (t < limit)
	switch ((c = *t++))
	  {
	  case '\t':
	    {
	      size_t spaces = tab_size - column % tab_size;
	      column += spaces;
	      do
		putc (' ', out);
	      while (--spaces);
	    }
	    break;

	  case '\r':
	    putc (c, out);
	    if (flag_format && t < limit && *t != '\n')
	      fprintf (out, flag_format, line_flag);
	    column = 0;
	    break;

	  case '\b':
	    if (column == 0)
	      continue;
	    column--;
	    putc (c, out);
	    break;

	  default:
	    column += isprint (c) != 0;
	    putc (c, out);
	    break;
	  }
    }
}

char const change_letter[] = { 0, 'd', 'a', 'c' };

/* Translate an internal line number (an index into diff's table of lines)
   into an actual line number in the input file.
   The internal line number is I.  FILE points to the data on the file.

   Internal line numbers count from 0 starting after the prefix.
   Actual line numbers count from 1 within the entire file.  */

lin _GL_ATTRIBUTE_PURE
translate_line_number (struct file_data const *file, lin i)
{
  return i + file->prefix_lines + 1;
}

/* Translate a line number range.  This is always done for printing,
   so for convenience translate to long int rather than lin, so that the
   caller can use printf with "%ld" without casting.  */

void
translate_range (struct file_data const *file,
		 lin a, lin b,
		 long int *aptr, long int *bptr)
{
  *aptr = translate_line_number (file, a - 1) + 1;
  *bptr = translate_line_number (file, b + 1) - 1;
}

/* Print a pair of line numbers with SEPCHAR, translated for file FILE.
   If the two numbers are identical, print just one number.

   Args A and B are internal line numbers.
   We print the translated (real) line numbers.  */

void
print_number_range (char sepchar, struct file_data *file, lin a, lin b)
{
  long int trans_a, trans_b;
  translate_range (file, a, b, &trans_a, &trans_b);

  /* Note: we can have B < A in the case of a range of no lines.
     In this case, we should print the line number before the range,
     which is B.  */
  if (trans_b > trans_a)
    fprintf (outfile, "%ld%c%ld", trans_a, sepchar, trans_b);
  else
    fprintf (outfile, "%ld", trans_b);
}

/* Look at a hunk of edit script and report the range of lines in each file
   that it applies to.  HUNK is the start of the hunk, which is a chain
   of 'struct change'.  The first and last line numbers of file 0 are stored in
   *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
   Note that these are internal line numbers that count from 0.

   If no lines from file 0 are deleted, then FIRST0 is LAST0+1.

   Return UNCHANGED if only ignorable lines are inserted or deleted,
   OLD if lines of file 0 are deleted,
   NEW if lines of file 1 are inserted,
   and CHANGED if both kinds of changes are found. */

enum changes
analyze_hunk (struct change *hunk,
	      lin *first0, lin *last0,
	      lin *first1, lin *last1)
{
  struct change *next;
  lin l0, l1;
  lin show_from, show_to;
  lin i;
  bool trivial = ignore_blank_lines || ignore_regexp.fastmap;
  size_t trivial_length = ignore_blank_lines - 1;
    /* If 0, ignore zero-length lines;
       if SIZE_MAX, do not ignore lines just because of their length.  */

  bool skip_white_space =
    ignore_blank_lines && IGNORE_TRAILING_SPACE <= ignore_white_space;
  bool skip_leading_white_space =
    skip_white_space && IGNORE_SPACE_CHANGE <= ignore_white_space;

  char const * const *linbuf0 = files[0].linbuf;  /* Help the compiler.  */
  char const * const *linbuf1 = files[1].linbuf;

  show_from = show_to = 0;

  *first0 = hunk->line0;
  *first1 = hunk->line1;

  next = hunk;
  do
    {
      l0 = next->line0 + next->deleted - 1;
      l1 = next->line1 + next->inserted - 1;
      show_from += next->deleted;
      show_to += next->inserted;

      for (i = next->line0; i <= l0 && trivial; i++)
	{
	  char const *line = linbuf0[i];
	  char const *newline = linbuf0[i + 1] - 1;
	  size_t len = newline - line;
	  char const *p = line;
	  if (skip_white_space)
	    for (; *p != '\n'; p++)
	      if (! isspace ((unsigned char) *p))
		{
		  if (! skip_leading_white_space)
		    p = line;
		  break;
		}
	  if (newline - p != trivial_length
	      && (! ignore_regexp.fastmap
		  || re_search (&ignore_regexp, line, len, 0, len, 0) < 0))
	    trivial = 0;
	}

      for (i = next->line1; i <= l1 && trivial; i++)
	{
	  char const *line = linbuf1[i];
	  char const *newline = linbuf1[i + 1] - 1;
	  size_t len = newline - line;
	  char const *p = line;
	  if (skip_white_space)
	    for (; *p != '\n'; p++)
	      if (! isspace ((unsigned char) *p))
		{
		  if (! skip_leading_white_space)
		    p = line;
		  break;
		}
	  if (newline - p != trivial_length
	      && (! ignore_regexp.fastmap
		  || re_search (&ignore_regexp, line, len, 0, len, 0) < 0))
	    trivial = 0;
	}
    }
  while ((next = next->link) != 0);

  *last0 = l0;
  *last1 = l1;

  /* If all inserted or deleted lines are ignorable,
     tell the caller to ignore this hunk.  */

  if (trivial)
    return UNCHANGED;

  return (show_from ? OLD : UNCHANGED) | (show_to ? NEW : UNCHANGED);
}

/* Concatenate three strings, returning a newly malloc'd string.  */

char *
concat (char const *s1, char const *s2, char const *s3)
{
  char *new = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1);
  sprintf (new, "%s%s%s", s1, s2, s3);
  return new;
}

/* Yield a new block of SIZE bytes, initialized to zero.  */

void *
zalloc (size_t size)
{
  void *p = xmalloc (size);
  memset (p, 0, size);
  return p;
}

void
debug_script (struct change *sp)
{
  fflush (stdout);

  for (; sp; sp = sp->link)
    {
      long int line0 = sp->line0;
      long int line1 = sp->line1;
      long int deleted = sp->deleted;
      long int inserted = sp->inserted;
      fprintf (stderr, "%3ld %3ld delete %ld insert %ld\n",
	       line0, line1, deleted, inserted);
    }

  fflush (stderr);
}