forked from ekr/rfcdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rfcdiff-tmpl
executable file
·1060 lines (952 loc) · 33.1 KB
/
rfcdiff-tmpl
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
#!/bin/bash
#
# Synopsis:
# Show changes between 2 internet-drafts using changebars or html
# side-by-side diff.
#
# Usage:
# rfcdiff [options] file1 file2
#
# rfcdiff takes two RFCs or Internet-Drafts in text form as input, and
# produces output which indicates the differences found in one of various
# forms, controlled by the options listed below. In all cases, page
# headers and page footers are stripped before looking for changes.
#
# --html Produce side-by-side .html diff (default)
#
# --chbars Produce changebar marked .txt output
#
# --diff Produce a regular diff output
#
# --wdiff Produce paged wdiff output
#
# --hwdiff Produce html-wrapped coloured wdiff output
#
# --oldcolour COLOURNAME Colour for new file in hwdiff (default is "green")
# --oldcolor COLORNAME Color for old file in hwdiff (default is "red")
#
# --newcolour COLOURNAME Colour for new file in hwdiff (default is "green")
# --newcolor COLORNAME Color for new file in hwdiff (default is "green")
#
# --larger Make difference text in hwdiff slightly larger
#
# --browse Show html output in browser
#
# --keep Don't delete temporary workfiles
#
# --version Show version
#
# --help Show this help
#
# --info "Synopsis|Usage|Copyright|Description|Log"
# Show various info
#
# --width N Set a maximum width of N characters for the
# display of each half of the old/new html diff
#
# --linenum Show linenumbers for each line, not only at the
# start of each change section
#
# --body Strip document preamble (title, boilerplate and
# table of contents) and postamble (Intellectual
# Property Statement, Disclaimer etc)
#
# --nostrip Don't strip headers and footers (or body)
#
# --ab-diff Before/After diff, suitable for rfc-editor
# --abdiff
#
# --stdout Send output to stdout instead to a file
#
#
# Copyright:
# -----------------------------------------------------------------
#
# Copyright 2002 Henrik Levkowetz
#
# 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 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------
#
# Description:
#
# The purpose of this program is to compare two versions of an
# internet-draft, and as output produce a diff in one of several
# formats:
#
# - side-by-side html diff
# - paged wdiff output in a text terminal
# - a text file with changebars in the left margin
# - a simple unified diff output
#
# In all cases, internet-draft headers and footers are stripped before
# generating the diff, to produce a cleaner diff.
#
# It is called as
#
# rfcdiff first-file second-file
#
# The latest version is available from
# http://tools.ietf.org/tools/rfcdiff/
#
export version="1.42"
export progdate=""
export prelines="10"
export basename=$(basename $0)
export workdir="/tmp/$basename-$$"
export pagecache1="$workdir/pagecache1"
export pagecache2="$workdir/pagecache2"
# ----------------------------------------------------------------------
# Utility to find an executable
# ----------------------------------------------------------------------
lookfor() {
for b in "$@"; do
found=$(which "$b" 2>/dev/null)
if [ -n "$found" ]; then
if [ -x "$found" ]; then
echo "$found"
return
fi
fi
done
}
AWK=$(lookfor gawk nawk awk)
# ----------------------------------------------------------------------
# Strip headers footers and formfeeds from infile to stdout
# ----------------------------------------------------------------------
strip() {
$AWK '
{ gsub(/\r/, ""); }
{ gsub(/[ \t]+$/, ""); }
{ pagelength++; }
/\[?[Pp]age [0-9ivx]+\]?[ \t\f]*$/ {
match($0, /[Pp]age [0-9ivx]+/);
num = substr($0, RSTART+5, RLENGTH-5);
print num, outline > ENVIRON["pagecache" ENVIRON["which"]]
pagelength = 0;
}
/\f/ { newpage=1;
pagelength=1;
}
/\f$/ {
# a form feed followed by a \n does not contribute to the
# line count. (But a \f followed by something else does.)
pagelength--;
}
/\f/ { next; }
/\[?[Pp]age [0-9ivx]+\]?[ \t\f]*$/ { preindent = indent; next; }
/^ *Internet.Draft.+[12][0-9][0-9][0-9] *$/ && (FNR > 15) { newpage=1; next; }
/^ *INTERNET.DRAFT.+[12][0-9][0-9][0-9] *$/ && (FNR > 15) { newpage=1; next; }
/^ *Draft.+( +)[12][0-9][0-9][0-9] *$/ && (FNR > 15) { newpage=1; next; }
/^RFC[ -]?[0-9]+.*( +).* [12][0-9][0-9][0-9]$/ && (FNR > 15) { newpage=1; next; }
/^draft-[-a-z0-9_.]+.*[0-9][0-9][0-9][0-9]$/ && (FNR > 15) { newpage=1; next; }
/(Jan|Feb|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|Sep|Oct|Nov|Dec) (19[89][0-9]|20[0-9][0-9]) *$/ && pagelength < 3 { newpage=1; next; }
newpage && $0 ~ /^ *draft-[-a-z0-9_.]+ *$/ { newpage=1; next; }
/^[ \t]+\[/ { sentence=1; }
/[^ \t]/ {
indent = match($0, /[^ ]/);
if (indent < preindent) {
sentence = 1;
}
if (newpage) {
if (sentence) {
outline++; print "";
}
} else {
if (haveblank) {
outline++; print "";
}
}
haveblank=0;
sentence=0;
newpage=0;
line = $0;
sub(/^ *\t/, " ", line);
thiscolumn = match(line, /[^ ]/);
}
/[.:][ \t]*$/ { sentence=1; }
/\(http:\/\/trustee\.ietf\.org\/license-info\)\./ { sentence=0; }
/^[ \t]*$/ { haveblank=1; next; }
{ outline++; print; }
' "$1"
}
# ----------------------------------------------------------------------
# Strip preamble (title, boilerplate and table of contents) and
# postamble (Intellectual Property Statement, Disclaimer etc)
# ----------------------------------------------------------------------
bodystrip() {
$AWK '
/^[ \t]*Acknowledgment/ { inbody = 0; }
/^(Full )*Copyright Statement$/ { inbody = 0; }
/^[ \t]*Disclaimer of Validid/ { inbody = 0; }
/^[ \t]*Intellectual Property/ { inbody = 0; }
/^Abstract$/ { inbody = 0; }
/^Table of Contents$/ { inbody = 0; }
/^1.[ \t]*Introduction$/ { inbody = 1; }
inbody { print; }
' "$1"
}
# ----------------------------------------------------------------------
# From two words, find common prefix and differing part, join descriptively
# ----------------------------------------------------------------------
worddiff() {
$AWK '
BEGIN {
w1 = ARGV[1]
w2 = ARGV[2]
format = ARGV[3]
do {
if (substr(w1,1,1) == substr(w2,1,1)) {
w1 = substr(w1,2)
w2 = substr(w2,2)
} else {
break;
}
prefixlen++;
} while (length(w1) && length(w2))
prefix = substr(ARGV[1],1,prefixlen);
do {
l1 = length(w1);
l2 = length(w2);
if (substr(w1,l1,1) == substr(w2,l2,1)) {
w1 = substr(w1,1,l1-1)
w2 = substr(w2,1,l2-1)
} else {
break;
}
} while (l1 && l2)
suffix = substr(ARGV[1], prefixlen+length(w1))
printf format, prefix, w1, w2, suffix;
}
' "$1" "$2" "$3"
}
# ----------------------------------------------------------------------
# Generate a html page with side-by-side diff from a unified diff
# ----------------------------------------------------------------------
htmldiff() {
$AWK '
BEGIN {
FS = "[ \t,]";
# Read pagecache1
maxpage[1] = 1
pageend[1,0] = 2;
while ( getline < ENVIRON["pagecache1"] > 0) {
pageend[1,$1] = $2;
if ($1+0 > maxpage[1]) maxpage[1] = $1+0;
}
# Read pagecache2
maxpage[2] = 1
pageend[2,0] = 2;
while ( getline < ENVIRON["pagecache2"] > 0) {
pageend[2,$1] = $2;
if ($1+0 > maxpage[2]) maxpage[2] = $1+0;
}
wdiff = ENVIRON["wdiffbin"]
base1 = ENVIRON["base1"]
base2 = ENVIRON["base2"]
optwidth = ENVIRON["optwidth"]
optnums = ENVIRON["optnums"]
optlinks = ENVIRON["optlinks"]
cmdline = ENVIRON["cmdline"]
gsub("--", "- -", cmdline)
ENVIRON["cmdline"] = cmdline
header(base1, base2)
difflines1 = 0
difflines2 = 0
}
function header(file1, file2) {
url1 = file1;
url2 = file2;
if (optlinks) {
if (file1 ~ /^draft-/) { url1 = sprintf("<a href=\"https://tools.ietf.org/html/%s\" style=\"color:#008\">%s</a>", file1, file1); }
if (file1 ~ /^draft-/) { prev = sprintf("<a href=\"/rfcdiff?url2=%s\" style=\"color:#008; text-decoration:none;\"><</a>", file1); }
if (file2 ~ /^draft-/) { url2 = sprintf("<a href=\"https://tools.ietf.org/html/%s\" style=\"color:#008\">%s</a>", file2, file2); }
if (file2 ~ /^draft-/) { nxt = sprintf("<a href=\"/rfcdiff?url1=%s\" style=\"color:#008; text-decoration:none;\">></a>", file2) }
}
printf "" \
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> \n" \
"<!-- Generated by rfcdiff %s: rfcdiff %s --> \n" \
"<!-- <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional\" > -->\n" \
"<!-- System: %s --> \n" \
"<!-- Using awk: %s: %s --> \n" \
"<!-- Using diff: %s: %s --> \n" \
"<!-- Using wdiff: %s: %s --> \n" \
"<html> \n" \
"<head> \n" \
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" /> \n" \
" <meta http-equiv=\"Content-Style-Type\" content=\"text/css\" /> \n" \
" <title>Diff: %s - %s</title> \n" \
" <style type=\"text/css\"> \n" \
" body { margin: 0.4ex; margin-right: auto; } \n" \
" tr { } \n" \
" td { white-space: pre; font-family: monospace; vertical-align: top; font-size: 0.86em;} \n" \
" th { font-size: 0.86em; } \n" \
" .small { font-size: 0.6em; font-style: italic; font-family: Verdana, Helvetica, sans-serif; } \n" \
" .left { background-color: #EEE; } \n" \
" .right { background-color: #FFF; } \n" \
" .diff { background-color: #CCF; } \n" \
" .lblock { background-color: #BFB; } \n" \
" .rblock { background-color: #FF8; } \n" \
" .insert { background-color: #8FF; } \n" \
" .delete { background-color: #ACF; } \n" \
" .void { background-color: #FFB; } \n" \
" .cont { background-color: #EEE; } \n" \
" .linebr { background-color: #AAA; } \n" \
" .lineno { color: red; background-color: #FFF; font-size: 0.7em; text-align: right; padding: 0 2px; } \n" \
" .elipsis{ background-color: #AAA; } \n" \
" .left .cont { background-color: #DDD; } \n" \
" .right .cont { background-color: #EEE; } \n" \
" .lblock .cont { background-color: #9D9; } \n" \
" .rblock .cont { background-color: #DD6; } \n" \
" .insert .cont { background-color: #0DD; } \n" \
" .delete .cont { background-color: #8AD; } \n" \
" .stats, .stats td, .stats th { background-color: #EEE; padding: 2px 0; } \n" \
" </style> \n" \
" %s \n" \
"</head> \n" \
"<body > \n" \
" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> \n" \
" <tr bgcolor=\"orange\"><th></th><th>%s %s </th><th> </th><th> %s %s</th><th></th></tr> \n" \
"", ENVIRON["version"], ENVIRON["cmdline"], ENVIRON["uname"], ENVIRON["awkbin"], ENVIRON["awkver"], ENVIRON["diffbin"], ENVIRON["diffver"], ENVIRON["wdiffbin"], ENVIRON["wdiffver"], file1, file2, ENVIRON["jsinc"], prev, url1, url2, nxt;
}
function worddiff(w1, w2) {
prefixlen = 0;
word1 = w1;
do {
if (substr(w1,1,1) == substr(w2,1,1)) {
w1 = substr(w1,2);
w2 = substr(w2,2);
} else {
break;
}
prefixlen++;
} while (length(w1) && length(w2));
prefix = substr(word1,1,prefixlen);
do {
l1 = length(w1);
l2 = length(w2);
if (substr(w1,l1,1) == substr(w2,l2,1)) {
w1 = substr(w1,1,l1-1);
w2 = substr(w2,1,l2-1);
} else {
break;
}
} while (l1 && l2);
suffix = substr(word1, prefixlen+length(w1)+1);
wordpart[0] = prefix;
wordpart[1] = w1;
wordpart[2] = w2;
wordpart[3] = suffix;
}
function numdisplay(which, line) {
if (optnums && (line != prevline[which])) {
prevline[which] = line;
return line-1;
}
return "";
}
function fixesc(line) {
# Making this a no-op for now -- the change in line-breaking
# "<br><span...>" => "\n" should make this less necessary.
# line = gensub(/&(<[^>]*>)/, "\\1\\&", "g", line);
# We still have to handle cases where we have a broken up "<" / ">"
gsub(/&l<\/span>t;/, "\\<</span>", line);
gsub(/&g<\/span>t;/, "\\></span>", line);
gsub(/&<span class="delete">amp;/, "<span class=\"delete\">\\&", line)
gsub(/&<span class="insert">amp;/, "<span class=\"insert\">\\&", line)
gsub(/&<span class="delete">lt;/, "<span class=\"delete\">\\<", line);
gsub(/&<span class="delete">gt;/, "<span class=\"delete\">\\>", line);
gsub(/&<span class="insert">lt;/, "<span class=\"insert\">\\<", line);
gsub(/&<span class="insert">gt;/, "<span class=\"insert\">\\>", line);
gsub(/&<span class="delete">l<\/span>t;/, "<span class=\"delete\">\\<</span>", line);
gsub(/&<span class="delete">g<\/span>t;/, "<span class=\"delete\">\\></span>", line);
gsub(/&<span class="insert">l<\/span>t;/, "<span class=\"insert\">\\<</span>", line);
gsub(/&<span class="insert">g<\/span>t;/, "<span class=\"insert\">\\></span>", line);
return line;
}
function chunkdiff(chunk) {
if (difflines1 == 0 && difflines2 == 0) return;
chunkfile1= sprintf("1/chunk%04d", chunk);
chunkfile2= sprintf("2/chunk%04d", chunk);
printf "" > chunkfile1;
printf "" > chunkfile2;
for (l = 0; l < difflines1; l++) { print stack1[l] >> chunkfile1; }
for (l = 0; l < difflines2; l++) { print stack2[l] >> chunkfile2; }
close(chunkfile1);
close(chunkfile2);
cmd1 = sprintf("%s -n -2 -w \"<span class=\\\"delete\\\">\" -x \"</span>\" %s %s", wdiff, chunkfile1, chunkfile2);
cmd2 = sprintf("%s -n -1 -y \"<span class=\\\"insert\\\">\" -z \"</span>\" %s %s", wdiff, chunkfile1, chunkfile2);
l=0; while (cmd1 | getline > 0) { stack1[l] = fixesc($0); l++; }
difflines1 = l;
l=0; while (cmd2 | getline > 0) { stack2[l] = fixesc($0); l++; }
difflines2 = l;
close(cmd1);
close(cmd2);
}
function flush() {
if (difflines1 || difflines2) {
difftag++;
multiline = (difflines1 > 1) || (difflines2 > 1);
if (multiline && (wdiff != "")) chunkdiff(difftag);
printf " <tr><td><a name=\"diff%04d\" /></td></tr>\n", difftag;
for (l = 0; l < difflines1 || l < difflines2; l++) {
if (l in stack1) {
line1 = stack1[l];
delete stack1[l];
linenum1++
if (line1 == "")
if (optwidth > 0) {
line1 = substr(" ",0,optwidth);
} else {
line1 = " ";
}
} else {
line1 = "";
}
if (l in stack2) {
line2 = stack2[l];
delete stack2[l];
linenum2++;
if (line2 == "")
if (optwidth > 0) {
line2 = substr(" ",0,optwidth);
} else {
line2 = " ";
}
} else {
line2 = "";
}
if (!multiline || (wdiff == "")) {
worddiff(line1, line2);
line1 = fixesc(sprintf("%s<span class=\"delete\">%s</span>%s", wordpart[0], wordpart[1], wordpart[3]));
line2 = fixesc(sprintf("%s<span class=\"insert\">%s</span>%s", wordpart[0], wordpart[2], wordpart[3]));
# Clean up; remove empty spans
sub(/<span class="delete"><\/span>/,"", line1);
sub(/<span class="insert"><\/span>/,"", line2);
}
left = sprintf("<td class=\"lineno\" valign=\"top\">%s</td><td class=\"lblock\">%s</td>", numdisplay(1, linenum1), line1);
right = sprintf("<td class=\"rblock\">%s</td><td class=\"lineno\" valign=\"top\">%s</td>", line2, numdisplay(2, linenum2));
printf " <tr>%s<td> </td>%s</tr>\n", left, right;
}
}
}
function getpage(which, line) {
line = line + ENVIRON["prelines"];
page = "?";
for (p=1; p <= maxpage[which]; p++) {
if (pageend[which,p] == 0) continue;
if (line <= pageend[which,p]) {
page = p;
break;
}
}
return page;
}
function getpageline(which, line, page) {
if (page == "?") {
return line + ENVIRON["prelines"];
} else {
if (pageend[which,page-1]+0 != 0) {
return line + ENVIRON["prelines"] - pageend[which,page-1] + 3; # numbers of header lines stripped
} else {
return "?";
}
}
}
function htmlesc(line) {
gsub("&", "\\&", line);
gsub("<", "\\<", line);
gsub(">", "\\>", line);
return line;
}
function expandtabs(line) {
spaces = " ";
while (pos = index(line, "\t")) {
sub("\t", substr(spaces, 0, (8-pos%8)), line);
}
return line;
}
function maybebreakline(line, width) {
width = optwidth;
new = "";
if (width > 0) {
line = expandtabs(line);
while (length(line) > width) {
new = new htmlesc(substr(line, 1, width)) "\n";
line = substr(line, width+1);
}
}
line = new htmlesc(line) ;
return line;
}
/^@@/ {
linenum1 = 0 - $2;
linenum2 = 0 + $4;
diffnum ++;
if (linenum1 > 1) {
printf " <tr><td class=\"lineno\"></td><td class=\"left\"></td><td> </td><td class=\"right\"></td><td class=\"lineno\"></td></tr>\n";
page1 = getpage(1,linenum1);
page2 = getpage(2,linenum2);
if (page1 == "?") {
posinfo1 = sprintf("<a name=\"part-l%s\" /><small>skipping to change at</small><em> line %s</em>", diffnum, getpageline(1, linenum1, page1));
} else {
posinfo1 = sprintf("<a name=\"part-l%s\" /><small>skipping to change at</small><em> page %s, line %s</em>", diffnum, page1, getpageline(1, linenum1, page1));
}
if (page2 == "?") {
posinfo2 = sprintf("<a name=\"part-r%s\" /><small>skipping to change at</small><em> line %s</em>", diffnum, getpageline(2, linenum2, page2));
} else {
posinfo2 = sprintf("<a name=\"part-r%s\" /><small>skipping to change at</small><em> page %s, line %s</em>", diffnum, page2, getpageline(2, linenum2, page2));
}
printf " <tr bgcolor=\"gray\" ><td></td><th>%s</th><th> </th><th>%s</th><td></td></tr>\n", posinfo1, posinfo2;
}
}
/^---/ { next; }
/^[+][+][+]/ { next; }
/^[ ]/ {
line = substr($0, 2);
line = maybebreakline(line);
flush();
linenum1++;
linenum2++;
printf " <tr><td class=\"lineno\" valign=\"top\">%s</td><td class=\"left\">%s</td><td> </td>", numdisplay(1, linenum1), line;
printf "<td class=\"right\">%s</td><td class=\"lineno\" valign=\"top\">%s</td></tr>\n", line, numdisplay(2, linenum2);
diffcount1 += difflines1
difflines1 = 0
diffcount2 += difflines2
difflines2 = 0
}
/^-/ {
line = substr($0, 2);
line = maybebreakline(line);
stack1[difflines1] = line;
difflines1++;
}
/^[+]/ {
line = substr($0, 2);
line = maybebreakline(line);
stack2[difflines2] = line;
difflines2++;
}
END {
flush();
printf("\n" \
" <tr><td></td><td class=\"left\"></td><td> </td><td class=\"right\"></td><td></td></tr>\n" \
" <tr bgcolor=\"gray\"><th colspan=\"5\" align=\"center\"><a name=\"end\"> %s. %s change blocks. </a></th></tr>\n" \
" <tr class=\"stats\"><td></td><th><i>%s lines changed or deleted</i></th><th><i> </i></th><th><i>%s lines changed or added</i></th><td></td></tr>\n" \
" <tr><td colspan=\"5\" align=\"center\" class=\"small\"><br/>This html diff was produced by rfcdiff %s. The latest version is available from <a href=\"http://www.tools.ietf.org/tools/rfcdiff/\" >http://tools.ietf.org/tools/rfcdiff/</a> </td></tr>\n" \
" </table>\n" \
" </body>\n" \
" </html>\n", diffnum?"End of changes":"No changes", difftag, diffcount1, diffcount2, ENVIRON["version"]);
}
' "$1"
}
# ----------------------------------------------------------------------
# Generate before/after text output from a context diff
# ----------------------------------------------------------------------
abdiff() {
$AWK '
BEGIN {
# Read pagecache1
maxpage[1] = 1
pageend[1,0] = 2;
while ( getline < ENVIRON["pagecache1"] > 0) {
pageend[1,$1] = $2;
if ($1+0 > maxpage[1]) maxpage[1] = $1+0;
}
# Read pagecache2
maxpage[2] = 1
pageend[2,0] = 2;
while ( getline < ENVIRON["pagecache2"] > 0) {
pageend[2,$1] = $2;
if ($1+0 > maxpage[2]) maxpage[2] = $1+0;
}
base1 = ENVIRON["base1"]
base2 = ENVIRON["base2"]
section = "INTRODUCTION";
para = 0;
}
/^\+\+/ {
next;
}
/^\-\-/ {
next;
}
/^ Appendix ./ {
section = $1 " " $2;
para = 0;
}
/^ ? ? ?[0-9]+(\.[0-9]+)*\.? / {
section = "Section " $1;
para = 0;
}
/^ ?$/ {
if (inpara) {
printf "\n%s, paragraph %s:\n", section, para;
print "OLD:\n"
print oldpara
print "NEW:\n"
print newpara
}
oldpara = "";
newpara = "";
para ++;
inpara = 0
}
/^ ./ {
oldpara = oldpara $0 "\n"
newpara = newpara $0 "\n"
}
/^\-/ {
sub(/^./, " ");
oldpara = oldpara $0 "\n"
inpara++;
}
/^\+/ {
sub(/^./, " ");
newpara = newpara $0 "\n"
inpara++;
}
END {
if (inpara) {
printf "\n%s, paragraph %s:\n", section, para;
print "OLD:\n"
print oldpara
print "NEW:\n"
print newpara
}
}
'
}
# ----------------------------------------------------------------------
# Utility to extract keyword info
# ----------------------------------------------------------------------
extract() {
$AWK -v keyword="$1" '
BEGIN {
# print "Keyword", keyword;
}
/^# [A-Z]/ {
# print "New key", $2;
if ($2 == keyword ":" ) { output=1; } else { output=0; }
# print "Output", output;
}
/^#\t/ {
# print "Content", output, $0;
if ( output ) {
sub(/^#/,"");
print;
}
}
{
next;
}
' "$2"
}
# ----------------------------------------------------------------------
# Utility to start a browser
# ----------------------------------------------------------------------
browse() {
if [ "$(uname)" = "Darwin" ]; then
open "$@"
else
browser=$(lookfor firefox phoenix MozillaFirebird mozilla opera Netscape netscape dillo)
if [ -z "$browser" ]; then
echo "Couldn't find any browser, can't display $*."
exit 1
fi
# make sure file name is absolute
if [ "${1#/}" = "$1" ]; then
# not absolute path, add pwd
arg="file://$PWD/$1"
else
arg="file://$1"
fi
# see if a browser is running, act accordingly
$browser -remote "ping()" >/dev/null 2>&1
if [ $? -eq 0 ]; then
# use running instance
$browser -raise -remote "openurl($arg, new-tab)"
else
# error exit: no running instance
echo "Starting web browser."
$browser "$arg" >/dev/null 2>&1 &
fi
fi
}
# ----------------------------------------------------------------------
# Utility for error exit
# ----------------------------------------------------------------------
die() {
echo $*;
exit 1;
}
# ----------------------------------------------------------------------
# Process options
# ----------------------------------------------------------------------
# Default values
opthtml=1; optdiff=0; optchbars=0; optwdiff=0; optshow=0; optnowdiff=0;
optkeep=0; optinfo=0; optwidth=0; optnums=0; optbody=0; optabdiff=0;
optstrip=1; opthwdiff=0; optlinks=0;
optoldcolour="red"; optnewcolour="green"; optlarger=""
optstdout=0;
while [ $# -gt 0 ]; do
case "$1" in
--html) opthtml=1; optdiff=0; optchbars=0; optwdiff=0; opthwdiff=0; optabdiff=0;;
--diff) opthtml=0; optdiff=1; optchbars=0; optwdiff=0; opthwdiff=0; optabdiff=0;;
--chbars) opthtml=0; optdiff=0; optchbars=1; optwdiff=0; opthwdiff=0; optabdiff=0;;
--wdiff) opthtml=0; optdiff=0; optchbars=0; optwdiff=1; opthwdiff=0; optabdiff=0;;
--hwdiff) opthtml=0; optdiff=0; optchbars=0; optwdiff=0; opthwdiff=1; optabdiff=0;;
--changes)opthtml=0; optdiff=0; optchbars=0; optwdiff=0; opthwdiff=0; optabdiff=1;;
--abdiff) opthtml=0; optdiff=0; optchbars=0; optwdiff=0; opthwdiff=0; optabdiff=1;;
--ab-diff)opthtml=0; optdiff=0; optchbars=0; optwdiff=0; opthwdiff=0; optabdiff=1;;
--rfc-editor-diff)opthtml=0; optdiff=0; optchbars=0; optwdiff=0; opthwdiff=0; optabdiff=1;;
--version)echo -e "$basename\t$version\t$progdate"; exit 0;;
--browse) optshow=1;;
--nowdiff)optnowdiff=1;;
--keep) optkeep=1;;
--info) optinfo=1; keyword=$2; shift;;
--help) optinfo=1; keyword="Usage";;
--width) optwidth=$2; shift;;
--oldcolor) optoldcolour=$2; shift;;
--oldcolour) optoldcolour=$2; shift;;
--newcolor) optnewcolour=$2; shift;;
--newcolour) optnewcolour=$2; shift;;
--larger) optlarger='size="+1"';;
--linenum)optnums=1;;
--body) optbody=1;;
--nostrip)optstrip=0; optbody=0;;
--stdout) optstdout=1;;
--links) optlinks=1;;
--no-space-changes) optnospacechange=1;;
--ignore-whitespace) optignorewhite=1;;
--wdiff-args) optwdiffargs=$2; shift;;
--) shift; break;;
-v) echo "$basename $version"; exit 0;;
-*) echo "Unrecognized option: $1";
exit 1;;
*) break;;
esac
shift
done
export optwidth
export optnums
export optlinks
# ----------------------------------------------------------------------
# Determine output file name. Maybe output usage and exit.
# ----------------------------------------------------------------------
#set -x
if [ $optinfo -gt 0 ]; then
extract $keyword $0
exit
fi
if [ $# -ge 2 ]; then
if [ "$1" = "$2" ]; then
echo "The files are the same file"
exit
fi
export base1=$(basename "$1")
export base2=$(basename "$2")
outbase=$(worddiff "$base2" "$base1" "%s%s-from-%s")
else
extract Usage $0
exit 1
fi
# ----------------------------------------------------------------------
# create working directory.
# ----------------------------------------------------------------------
mkdir $workdir || die "$0: Error: Failed to create temporary directory '$workdir'."
mkdir $workdir/1 || die "$0: Error: Failed to create temporary directory '$workdir/1'."
mkdir $workdir/2 || die "$0: Error: Failed to create temporary directory '$workdir/2'."
# ----------------------------------------------------------------------
# If any of the files is an http or ftp URL we download it, else copy it
# ----------------------------------------------------------------------
wgetbin=$(lookfor wget)
dowgetarg1=0
dowgetarg2=0
if [ -n "$wgetbin" ]; then
if [ "${1#http://}" != "$1" ]; then dowgetarg1=1; fi
if [ "${1#ftp://}" != "$1" ]; then dowgetarg1=1; fi
if [ "${2#http://}" != "$2" ]; then dowgetarg2=1; fi
if [ "${2#ftp://}" != "$2" ]; then dowgetarg2=1; fi
fi
if [ $dowgetarg1 -gt 0 ]; then
$wgetbin -nv "$1" -O $workdir/1/"$base1"
else
cp "$1" $workdir/1/"$base1"
fi
if [ $dowgetarg2 -gt 0 ]; then
$wgetbin -nv "$2" -O $workdir/2/"$base2"
else
cp "$2" $workdir/2/"$base2"
fi
# ----------------------------------------------------------------------
# Maybe strip headers/footers from both files
# ----------------------------------------------------------------------
if [ $optstrip -gt 0 ]; then
export which=1
strip $workdir/1/"$base1" > $workdir/1/"$base1".stripped
mv -f $workdir/1/"$base1".stripped $workdir/1/"$base1"
export which=2
strip $workdir/2/"$base2" > $workdir/2/"$base2".stripped
mv -f $workdir/2/"$base2".stripped $workdir/2/"$base2"
fi
# ----------------------------------------------------------------------
# Maybe do html quoting
# ----------------------------------------------------------------------
if [ $opthwdiff -gt 0 ]; then
sed -e 's/&/&/g' -e 's/</\</g' -e 's/>/\>/g' $workdir/1/"$base1" > $workdir/1/"$base1".quoted
mv -f $workdir/1/"$base1".quoted $workdir/1/"$base1"
sed -e 's/&/&/g' -e 's/</\</g' -e 's/>/\>/g' $workdir/2/"$base2" > $workdir/2/"$base2".quoted
mv -f $workdir/2/"$base2".quoted $workdir/2/"$base2"
fi
# ----------------------------------------------------------------------
# Maybe strip preamble (title, boilerplate and table of contents) and
# postamble (Intellectual Property Statement, Disclaimer etc)
# ----------------------------------------------------------------------
if [ $optbody -gt 0 ]; then
bodystrip $workdir/1/"$base1" > $workdir/1/"$base1".stripped
mv $workdir/1/"$base1".stripped $workdir/1/"$base1"
bodystrip $workdir/2/"$base2" > $workdir/2/"$base2".stripped
mv $workdir/2/"$base2".stripped $workdir/2/"$base2"
fi
# ----------------------------------------------------------------------
# Get output file name
# ----------------------------------------------------------------------
if [ "$3" ]; then
outfile="$3"
else
if [ $opthtml -gt 0 ]; then
outfile=./"$outbase".diff.html
fi
if [ $optchbars -gt 0 ]; then
outfile=./"$outbase".chbar
fi
if [ $optdiff -gt 0 ]; then
outfile=./"$outbase".diff
fi
if [ $optabdiff -gt 0 ]; then
outfile=./"$outbase".changes
fi
if [ $opthwdiff -gt 0 ]; then
outfile=./"$outbase".wdiff.html
fi
fi
if [ "$outfile" ]; then
tempout=./$(basename "$outfile")
fi
# ----------------------------------------------------------------------
# Check if we can use wdiff for block diffs
# ----------------------------------------------------------------------
if [ $optnowdiff -eq 0 ]; then
wdiffbin=$(lookfor wdiff)
if [ -n "$wdiffbin" ]; then
wdiffver=$($wdiffbin --version 2>/dev/null | egrep "(wdiff|GNU).+[0-9]\.[0-9]")
if [ -z "$wdiffver" ]; then
wdiffbin="";
echo -en "\n Found wdiff, but it reported no recognisable version."
fi
else
echo -en "\n Couldn't find wdiff."
fi
if [ -z "$wdiffbin" ]; then echo " Falling back to builtin diff colouring..."; fi
export wdiffbin
export wdiffver
#echo "Found wdiff at $wdiffbin"
fi
# ----------------------------------------------------------------------
# Get some misc. info
# ----------------------------------------------------------------------
uname=$(uname -a)
export uname
awkbin=$AWK
export awkbin
awkver=$( { $AWK --version 2>/dev/null || $AWK -V 2>/dev/null; } | head -n 1)
export awkver
diffbin=$(lookfor diff)
export diffbin
diffver=$(diff --version | head -n 1)
export diffver
# ----------------------------------------------------------------------
# Set up the JS code to page through chunks.
# ----------------------------------------------------------------------
jsinc=$(cat <<'ENDOFSCRIPT'
<script src="rfcdiff.js"></script>
ENDOFSCRIPT
)
export jsinc
# ----------------------------------------------------------------------
# Check that we don't have a broken awk
# ----------------------------------------------------------------------
if [ $opthtml -gt 0 -a "${uname%% *}" == "Darwin" -a "$awkver" == "awk version 20070501" ]; then
echo -e "\n Oops. Awk version 20070501 on OS X doesn't work with rfcdiff's html mode.\n To make rfcdiff work, you could install Gnu Awk (gawk), for instance using\n MacPorts, http://www.macports.org/."
exit 1
fi
# ----------------------------------------------------------------------
# Do diff
# ----------------------------------------------------------------------