-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmldiff.c
1456 lines (1202 loc) · 37.3 KB
/
htmldiff.c
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* wdiff -- front end to diff for comparing on a word per word basis.
Copyright © 1992, 1997, 1998, 1999 Free Software Foundation, Inc.
François Pinard <pinard@iro.umontreal.ca>, 1992.
Changed 18 Dec 1997, 16 May 2001, by Bert Bos <bert@w3.org>
to become an htmldiff program.
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 2, 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include "system.h"
/* Exit codes values. */
#define EXIT_DIFFERENCE 1 /* some differences found */
#undef EXIT_FAILURE
#define EXIT_FAILURE 2 /* any other reason for exit */
/* It is mandatory that some `diff' program is selected for use. The
following definition may also include the complete path. */
#ifndef DIFF_PROGRAM
# define DIFF_PROGRAM "diff"
#endif
/* One may also, optionnaly, define a default PAGER_PROGRAM. This might
be done from the Makefile. If PAGER_PROGRAM is undefined and the
PAGER environment variable is not set, none will be used. */
/* Define the separator lines when output is inhibited. */
#define SEPARATOR_LINE \
"======================================================================"
/* Library declarations. */
#include <ctype.h>
#if STDC_HEADERS
# include <string.h>
#else
# if HAVE_STRING_H
# include <string.h>
# else
# include <strings.h>
# define strrchr rindex
# endif
#endif
#ifndef strstr
char *strstr ();
#endif
#if HAVE_TPUTS
# if HAVE_TERMCAP_H
# include <termcap.h>
# else
# if HAVE_TERMLIB_H
# include <termlib.h>
# else
# if HAVE_CURSES_H
# include <curses.h>
# else
# if HAVE_NCURSES_H
# include <ncurses.h>
# endif
# endif
# endif
const char *tgetstr ();
# endif
#endif
#include <setjmp.h>
#include <signal.h>
#ifndef RETSIGTYPE
# define RETSIGTYPE void
#endif
#include <sys/stat.h>
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <stdarg.h>
#include "getopt.h"
char *getenv ();
FILE *readpipe PARAMS ((const char *, ...));
FILE *writepipe PARAMS ((const char *, ...));
/* Declarations. */
/* Option variables. */
struct option const longopts[] =
{
{"auto-pager" , 0, NULL, 'a'},
{"avoid-wraps" , 0, NULL, 'n'},
{"copyright" , 0, NULL, 'C'},
{"end-delete" , 1, NULL, 'x'},
{"end-insert" , 1, NULL, 'z'},
{"help" , 0, NULL, 'h'},
{"ignore-case" , 0, NULL, 'i'},
{"less-mode" , 0, NULL, 'l'},
{"no-common" , 0, NULL, '3'},
{"no-deleted" , 0, NULL, '1'},
{"no-init-term", 0, NULL, 'K'},
{"no-inserted" , 0, NULL, '2'},
{"printer" , 0, NULL, 'p'},
{"start-delete", 1, NULL, 'w'},
{"start-insert", 1, NULL, 'y'},
{"statistics" , 0, NULL, 's'},
{"terminal" , 0, NULL, 't'},
{"version" , 0, NULL, 'V'},
{"tmp-path" , 1, NULL, 'P'},
{NULL , 0, NULL, 0}
};
const char *program_name; /* name of executing program */
int inhibit_left; /* inhibit display of left side words */
int inhibit_right; /* inhibit display of left side words */
int inhibit_common; /* inhibit display of common words */
int ignore_case; /* ignore case in comparisons */
int show_statistics; /* if printing summary statistics */
int no_wrapping; /* end/restart strings at end of lines */
int autopager; /* if calling the pager automatically */
int overstrike; /* if using printer overstrikes */
int overstrike_for_less; /* if output aimed to the "less" program */
const char *user_delete_start; /* user specified string for start of delete */
const char *user_delete_end; /* user specified string for end of delete */
const char *user_insert_start; /* user specified string for start of insert */
const char *user_insert_end; /* user specified string for end of insert */
const char* tmp_path = "/tmp";
int find_termcap; /* initialize the termcap strings */
int no_init_term; /* do not send init/term termcap strings */
const char *term_delete_start; /* termcap string for start of delete */
const char *term_delete_end; /* termcap string for end of delete */
const char *term_insert_start; /* termcap string for start of insert */
const char *term_insert_end; /* termcap string for end of insert */
/* Other variables. */
enum copy_mode
{
COPY_NORMAL, /* copy text unemphasized */
COPY_DELETED, /* copy text underlined */
COPY_INSERTED /* copy text bolded */
}
copy_mode;
jmp_buf signal_label; /* where to jump when signal received */
int interrupted; /* set when some signal has been received */
#define TMP_PATH_MAX_LEN 64
typedef struct side SIDE; /* all variables for one side */
struct side
{
const char *filename; /* original input file name */
FILE *file; /* original input file */
int position; /* number of words read so far */
int character; /* one character look ahead */
char temp_name[TMP_PATH_MAX_LEN]; /* temporary file name */
FILE *temp_file; /* temporary file */
};
SIDE side_array[2]; /* area for holding side descriptions */
SIDE *left_side = &side_array[0];
SIDE *right_side = &side_array[1];
FILE *input_file; /* stream being produced by diff */
int character; /* for reading input_file */
char directive; /* diff directive character */
int argument[4]; /* four diff directive arguments */
FILE *output_file; /* file to which we write output */
const char *termcap_init_string; /* how to initialize the termcap mode */
const char *termcap_end_string; /* how to complete the termcap mode */
int count_total_left; /* count of total words in left file */
int count_total_right; /* count of total words in right file */
int count_isolated_left; /* count of deleted words in left file */
int count_isolated_right; /* count of added words in right file */
int count_changed_left; /* count of changed words in left file */
int count_changed_right; /* count of changed words in right file */
/*----------------------------------.
| Print error and optionally exit. |
`----------------------------------*/
static void errexit(int status, int errnum, const char *format, ...)
{
if (format) {
va_list ap;
vfprintf(stderr, format, ap);
}
if (errnum) fprintf(stderr, ": %s\n", strerror(errnum));
if (status) exit(status);
}
/* Signal processing. */
/*-----------------.
| Signal handler. |
`-----------------*/
static RETSIGTYPE
signal_handler (int number)
{
interrupted = 1;
signal (number, signal_handler);
}
/*----------------------------.
| Prepare to handle signals. |
`----------------------------*/
static void
setup_signals (void)
{
interrupted = 0;
/* Intercept willingful requests for stopping. */
signal (SIGINT, signal_handler);
signal (SIGPIPE, signal_handler);
signal (SIGTERM, signal_handler);
}
/* Terminal initialization. */
static void
initialize_strings (void)
{
#if HAVE_TPUTS
if (find_termcap)
{
const char *name; /* terminal capability name */
char term_buffer[2048]; /* terminal description */
static char *buffer; /* buffer for capabilities */
char *filler; /* cursor into allocated strings */
int success; /* tgetent results */
name = getenv ("TERM");
if (name == NULL)
errexit (1, 0,
_("Select a terminal through the TERM environment variable."));
success = tgetent (term_buffer, name);
if (success < 0)
errexit (1, 0, _("Could not access the termcap data base."));
if (success == 0)
errexit (1, 0, _("Terminal type `%s' is not defined."), name);
buffer = (char *) malloc (strlen (term_buffer));
filler = buffer;
if (no_init_term) {
termcap_init_string = NULL;
termcap_end_string = NULL;
} else {
termcap_init_string = tgetstr ("ti", &filler);
termcap_end_string = tgetstr ("te", &filler);
}
term_delete_start = tgetstr ("us", &filler);
term_delete_end = tgetstr ("ue", &filler);
term_insert_start = tgetstr ("so", &filler);
term_insert_end = tgetstr ("se", &filler);
}
#endif /* HAVE_TPUTS */
/* Ensure some default strings. */
if (!overstrike) {
if (!term_delete_start && !user_delete_start) {
user_delete_start = "<s><del>";
}
if (!term_delete_end && !user_delete_end) {
user_delete_end = "</del></s>";
}
if (!term_insert_start && !user_insert_start) {
user_insert_start = "<u><ins>";
}
if (!term_insert_end && !user_insert_end) {
user_insert_end = "</ins></u>";
}
}
}
/* Character input and output. */
#if HAVE_TPUTS
/*-----------------------------------------.
| Write one character for tputs function. |
`-----------------------------------------*/
static int
putc_for_tputs (int chr)
{
return putc (chr, output_file);
}
#endif /* HAVE_TPUTS */
/*---------------------------.
| Indicate start of delete. |
`---------------------------*/
static void
start_of_delete (void)
{
/* Avoid any emphasis if it would be useless. */
if (inhibit_common && (inhibit_right || inhibit_left)) {
return;
}
copy_mode = COPY_DELETED;
#if HAVE_TPUTS
if (term_delete_start) {
tputs (term_delete_start, 0, putc_for_tputs);
}
#endif
if (user_delete_start) {
fprintf (output_file, "%s", user_delete_start);
}
}
/*-------------------------.
| Indicate end of delete. |
`-------------------------*/
static void
end_of_delete (void)
{
/* Avoid any emphasis if it would be useless. */
if (inhibit_common && (inhibit_right || inhibit_left)) {
return;
}
if (user_delete_end) {
fprintf (output_file, "%s", user_delete_end);
}
#if HAVE_TPUTS
if (term_delete_end) {
tputs (term_delete_end, 0, putc_for_tputs);
}
#endif
copy_mode = COPY_NORMAL;
}
/*---------------------------.
| Indicate start of insert. |
`---------------------------*/
static void
start_of_insert (void)
{
/* Avoid any emphasis if it would be useless. */
if (inhibit_common && (inhibit_right || inhibit_left)) {
return;
}
copy_mode = COPY_INSERTED;
#if HAVE_TPUTS
if (term_insert_start) {
tputs (term_insert_start, 0, putc_for_tputs);
}
#endif
if (user_insert_start) {
fprintf (output_file, "%s", user_insert_start);
}
}
/*-------------------------.
| Indicate end of insert. |
`-------------------------*/
static void
end_of_insert (void)
{
/* Avoid any emphasis if it would be useless. */
if (inhibit_common && (inhibit_right || inhibit_left)) {
return;
}
if (user_insert_end) {
fprintf (output_file, "%s", user_insert_end);
}
#if HAVE_TPUTS
if (term_insert_end) {
tputs (term_insert_end, 0, putc_for_tputs);
}
#endif
copy_mode = COPY_NORMAL;
}
/*-----------------------------------------.
| Skip over white space and tags on SIDE. |
`-----------------------------------------*/
static void
skip_whitespace (SIDE *side)
{
if (interrupted) {
longjmp (signal_label, 1);
}
while (isspace(side->character) || side->character == '<') {
if (side->character == '<') {
while (side->character != '>' && side->character != EOF)
side->character = getc(side->file);
}
side->character = getc(side->file);
}
}
/*------------------------------------.
| Skip over non white space on SIDE. |
`------------------------------------*/
static void skip_word (SIDE *side)
{
if (interrupted) {
longjmp (signal_label, 1);
}
while (side->character != EOF && !isspace (side->character) && side->character != '<') {
side->character = getc (side->file);
}
side->position++;
}
/*----------------------------------------------.
| Copy white space and tags from SIDE to FILE. |
`----------------------------------------------*/
static void copy_whitespace (SIDE *side, FILE *file)
{
if (interrupted)
longjmp (signal_label, 1);
while (isspace (side->character) || side->character == '<') {
/* While changing lines, ensure we stop any special display prior
to, and restore the special display after. When copy_mode is
anything else than COPY_NORMAL, file is always output_file. We
care underlining whitespace or overstriking it with itself,
because "less" understands these things as emphasis requests. */
switch (copy_mode) {
case COPY_NORMAL:
if (side->character == '<') {
while (side->character != '>' && side->character != EOF) {
putc (side->character, file);
side->character = getc(side->file);
}
}
if (side->character != EOF) putc (side->character, file);
break;
case COPY_DELETED:
if (side->character == '\n') {
if (no_wrapping && user_delete_end)
fprintf (output_file, "%s", user_delete_end);
#if HAVE_TPUTS
if (term_delete_end)
tputs (term_delete_end, 0, putc_for_tputs);
#endif
putc ('\n', output_file);
#if HAVE_TPUTS
if (term_delete_start)
tputs (term_delete_start, 0, putc_for_tputs);
#endif
if (no_wrapping && user_delete_start)
fprintf (output_file, "%s", user_delete_start);
} else if (overstrike_for_less) {
/* TODO: while ('>') */
putc ('_', output_file);
putc ('\b', output_file);
putc (side->character, output_file);
} else {
if (side->character == '<') {
while (side->character != '>' && side->character != EOF) {
putc (side->character, output_file);
side->character = getc(side->file);
}
}
if (side->character != EOF)
putc (side->character, output_file);
}
break;
case COPY_INSERTED:
if (side->character == '\n') {
if (no_wrapping && user_insert_end)
fprintf (output_file, "%s", user_insert_end);
#if HAVE_TPUTS
if (term_insert_end)
tputs (term_insert_end, 0, putc_for_tputs);
#endif
putc ('\n', output_file);
#if HAVE_TPUTS
if (term_insert_start)
tputs (term_insert_start, 0, putc_for_tputs);
#endif
if (no_wrapping && user_insert_start)
fprintf (output_file, "%s", user_insert_start);
} else if (overstrike_for_less) {
/* TODO: while ('>') */
putc (side->character, output_file);
putc ('\b', output_file);
putc (side->character, output_file);
} else
if (side->character == '<') {
while (side->character != '>' && side->character != EOF) {
putc (side->character, output_file);
side->character = getc(side->file);
}
}
if (side->character != EOF)
putc (side->character, output_file);
break;
}
/* Advance to next character. */
side->character = getc (side->file);
}
}
/*-----------------------------------------.
| Copy non white space from SIDE to FILE. |
`-----------------------------------------*/
static void copy_word (SIDE *side, FILE *file)
{
if (interrupted)
longjmp (signal_label, 1);
while (side->character != EOF && !isspace (side->character) && side->character != '<') {
/* In printer mode, act according to copy_mode. If copy_mode is not
COPY_NORMAL, we know that file is necessarily output_file. */
if (overstrike) {
switch (copy_mode) {
case COPY_NORMAL:
if (side->character != EOF) putc (side->character, file);
break;
case COPY_DELETED:
/* TODO: while ('>') */
putc ('_', output_file);
/* Avoid underlining an underscore. */
if (side->character != '_') {
putc ('\b', output_file);
putc (side->character, output_file);
}
break;
case COPY_INSERTED:
/* TODO: while ('>') */
putc (side->character, output_file);
putc ('\b', output_file);
putc (side->character, output_file);
break;
}
} else {
if (side->character != EOF)
putc (side->character, file);
}
side->character = getc (side->file);
}
side->position++;
}
/*-------------------------------------------------------------------------.
| For a given SIDE, turn original input file in another one, in which each |
| word is on one line. |
`-------------------------------------------------------------------------*/
static void split_file_into_words (SIDE *side)
{
struct stat stat_buffer; /* for checking if file is directory */
if (stat (tmp_path, &stat_buffer) != 0) {
fprintf(stderr, "Could not open path %s", tmp_path);
errexit (EXIT_FAILURE, errno, NULL);
}
if ((stat_buffer.st_mode & S_IFMT) != S_IFDIR) {
errexit (EXIT_FAILURE, 0, "Temporary path must be a directory.\n");
}
/* Open files. */
size_t tmplen;
if (side->filename == NULL) {
/* Select a file name, use it for opening a temporary file and
unlink it right away. Then, copy the whole standard input on
this temporary local file. Once done, prepare it for reading.
We do not need the file name itself anymore. */
/* check if we have room for <tmp_path + template + null byte> */
tmplen = strlen(tmp_path) + strlen("/htmldiff1XXXXXX") + 1;
if (tmplen > TMP_PATH_MAX_LEN) {
fprintf(stderr, "Maximum length of temporary path is %d; tried to allocate %zu bytes\n", TMP_PATH_MAX_LEN, tmplen);
errexit(EXIT_FAILURE, 0, NULL);
}
sprintf(side->temp_name, "%s/htmldiff1XXXXXX", tmp_path);
side->file = fdopen(mkstemp(side->temp_name), "w+");
if (side->file == NULL)
errexit (EXIT_FAILURE, errno, "%s", side->temp_name);
if (unlink (side->temp_name) != 0)
errexit (EXIT_FAILURE, errno, "%s", side->temp_name);
while (side->character = getchar (), side->character != EOF)
putc (side->character, side->file);
rewind (side->file);
} else {
/* Check and diagnose if the file name is a directory. Or else,
prepare the file for reading. */
if (access(side->filename, F_OK | R_OK) == -1) {
fprintf(stderr, "Input file %s does not exist or we dont have read permissions", side->filename);
errexit(EXIT_FAILURE, errno, NULL);
}
if (stat (side->filename, &stat_buffer) != 0) {
errexit (EXIT_FAILURE, errno, "%s", side->filename);
}
if ((stat_buffer.st_mode & S_IFMT) == S_IFDIR) {
errexit (EXIT_FAILURE, 0, _("Directories not supported"));
}
side->file = fopen (side->filename, "r");
if (side->file == NULL) {
errexit (EXIT_FAILURE, errno, "%s", side->filename);
}
}
side->character = getc (side->file);
side->position = 0;
/* check if we have room for <tmp_path + template + null byte> */
tmplen = strlen(tmp_path) + strlen("/htmldiff2XXXXXX") + 1;
if (tmplen > TMP_PATH_MAX_LEN) {
fprintf(stderr, "Maximum length of temporary path is %d; tried to allocate %zu bytes\n", TMP_PATH_MAX_LEN, tmplen);
errexit(EXIT_FAILURE, 0, NULL);
}
sprintf(side->temp_name, "%s/htmldiff2XXXXXX", tmp_path);
int tmpfilefd = mkstemp(side->temp_name);
if (tmpfilefd < 0) {
errexit(EXIT_FAILURE, 0, _("Error while creating temporary file"));
}
side->temp_file = fdopen(tmpfilefd, "w");
if (side->temp_file == NULL) {
errexit (EXIT_FAILURE, errno, "%s", side->temp_name);
}
/* Complete splitting input file into words on output. */
while (side->character != EOF) {
if (interrupted) {
longjmp (signal_label, 1);
}
skip_whitespace (side);
if (side->character == EOF) {
break;
}
copy_word (side, side->temp_file);
putc ('\n', side->temp_file);
}
fclose (side->temp_file);
}
/*-------------------------------------------------------------------.
| Decode one directive line from INPUT_FILE. The format should be: |
| |
| ARG0 [ , ARG1 ] LETTER ARG2 [ , ARG3 ] \n |
| |
| By default, ARG1 is assumed to have the value of ARG0, and ARG3 is |
| assumed to have the value of ARG2. Return 0 if any error found. |
`-------------------------------------------------------------------*/
static int decode_directive_line (void)
{
int value; /* last scanned value */
int state; /* ordinal of number being read */
int error; /* if error seen */
error = 0;
state = 0;
value = 0;
while (!error && state < 4) {
/* Read the next number. ARG0 and ARG2 are mandatory. */
if (isdigit (character)) {
value = 0;
while (isdigit (character)) {
value = 10 * value + character - '0';
character = getc (input_file);
}
} else if (state != 1 && state != 3) {
error = 1;
}
/* Assign the proper value. */
argument[state] = value;
/* Skip the following character. */
switch (state) {
case 0:
case 2:
if (character == ',') {
character = getc (input_file);
}
break;
case 1:
if (character == 'a' || character == 'd' || character == 'c') {
directive = character;
character = getc (input_file);
} else {
error = 1;
}
break;
case 3:
if (character != '\n') {
error = 1;
}
break;
}
state++;
}
/* Complete reading of the line and return success value. */
while (character != EOF && character != '\n') {
character = getc (input_file);
}
if (character == '\n') {
character = getc (input_file);
}
return !error;
}
/*----------------------------------------------.
| Skip SIDE until some word ORDINAL, included. |
`----------------------------------------------*/
static void skip_until_ordinal (SIDE *side, int ordinal)
{
while (side->position < ordinal) {
skip_whitespace (side);
skip_word (side);
}
}
/*----------------------------------------------.
| Copy SIDE until some word ORDINAL, included. |
`----------------------------------------------*/
static void copy_until_ordinal (SIDE *side, int ordinal, int mark_as)
{
while (side->position < ordinal) {
if (mark_as != 'd') {
copy_whitespace (side, output_file);
} else {
skip_whitespace(side);
putc(' ', output_file);
}
switch (mark_as) {
case 'i':
start_of_insert ();
break;
case 'd':
start_of_delete ();
break;
}
copy_word (side, output_file);
switch (mark_as) {
case 'i':
end_of_insert ();
break;
case 'd':
end_of_delete ();
break;
}
}
}
/*-----------------------------------------------------.
| Study diff output and use it to drive reformatting. |
`-----------------------------------------------------*/
static void reformat_diff_output (void)
{
int resync_left; /* word position for left resynchronisation */
int resync_right; /* word position for rigth resynchronisation */
/* Rewind input files. */
rewind (left_side->file);
left_side->character = getc (left_side->file);
left_side->position = 0;
rewind(right_side->file);
right_side->character = getc (right_side->file);
right_side->position = 0;
/* Process diff output. */
while (1) {
if (interrupted) {
longjmp (signal_label, 1);
}
/* Skip any line irrelevant to this program. */
while (character != EOF && !isdigit (character)) {
while (character != EOF && character != '\n') {
character = getc (input_file);
}
if (character == '\n') {
character = getc (input_file);
}
}
/* Get out the loop if end of file. */
if (character == EOF) {
break;
}
/* Read, decode and process one directive line. */
if (decode_directive_line ()) {
/* Accumulate statistics about isolated or changed word counts.
Decide the required position on both files to resynchronize
them, just before obeying the directive. Then, reposition
both files first, showing any needed common code along the
road. Be careful to copy common code from the left side if
only deleted code is to be shown. */
switch (directive) {
case 'a':
count_isolated_right += argument[3] - argument[2] + 1;
resync_left = argument[0];
resync_right = argument[2] - 1;
break;
case 'd':
count_isolated_left += argument[1] - argument[0] + 1;
resync_left = argument[0] - 1;
resync_right = argument[2];
break;
case 'c':
count_changed_left += argument[1] - argument[0] + 1;
count_changed_right += argument[3] - argument[2] + 1;
resync_left = argument[0] - 1;
resync_right = argument[2] - 1;
break;
default:
abort ();
}
if (!inhibit_left) {
if (!inhibit_common && inhibit_right) {
copy_until_ordinal (left_side, resync_left, ' ');
} else {
skip_until_ordinal (left_side, resync_left);
}
}
if (!inhibit_right) {
if (inhibit_common) {
skip_until_ordinal (right_side, resync_right);
} else {
copy_until_ordinal (right_side, resync_right, ' ');
}
}
if (!inhibit_common && inhibit_left && inhibit_right) {
copy_until_ordinal (right_side, resync_right, ' ');
}
/* Use separator lines to disambiguate the output. */
if (inhibit_left && inhibit_right) {
if (!inhibit_common) {
fprintf (output_file, "\n%s\n", SEPARATOR_LINE);
}
} else if (inhibit_common) {
fprintf (output_file, "\n%s\n", SEPARATOR_LINE);
}
if ((!inhibit_left || !inhibit_right) && !inhibit_common) {
copy_whitespace (right_side, output_file);
skip_whitespace (left_side);
}
/* Show any deleted code. */
if ((directive == 'd' || directive == 'c') && !inhibit_left) {
/* copy_whitespace (left_side, output_file);
start_of_delete ();
copy_word (left_side, output_file);
end_of_delete (); */
copy_until_ordinal (left_side, argument[1], 'd');
}
/* Show any inserted code, or ensure skipping over it in case the
right file is used merely to show common words. */
if (directive == 'a' || directive == 'c') {
if (inhibit_right) {
if (!inhibit_common && inhibit_left) {
skip_until_ordinal (right_side, argument[3]);
}
} else {
/* copy_whitespace (right_side, output_file);
start_of_insert ();
copy_word (right_side, output_file);
end_of_insert (); */
copy_until_ordinal (right_side, argument[3], 'i');
}
}
}
}
/* Copy remainder of input. Copy from left side if the user wanted to see
only the common code and deleted words. */
if (inhibit_common) {
if (!inhibit_left || !inhibit_right) {
fprintf (output_file, "\n%s\n", SEPARATOR_LINE);
}
} else if (!inhibit_left && inhibit_right) {
copy_until_ordinal (left_side, count_total_left, ' ');
copy_whitespace (left_side, output_file);
} else {
copy_until_ordinal (right_side, count_total_right, ' ');
copy_whitespace (right_side, output_file);
}
/* Close input files. */
fclose (left_side->file);
fclose (right_side->file);
}
/* Launch and complete various programs. */
/*-------------------------.
| Lauch the diff program. |
`-------------------------*/
static void launch_input_program (void)
{
/* Launch the diff program. */
if (ignore_case) {
input_file = readpipe (DIFF_PROGRAM, "-i", left_side->temp_name, right_side->temp_name, NULL);
} else {
input_file = readpipe (DIFF_PROGRAM, left_side->temp_name, right_side->temp_name, NULL);
}
if (!input_file) {