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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
|
/* reading patches */
/* Copyright (C) 1986, 1987, 1988 Larry Wall
Copyright (C) 1990, 1991, 1992, 1993, 1997, 1998, 1999, 2000, 2001,
2002, 2003, 2006, 2009, 2010 Free Software Foundation, Inc.
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/>. */
#define XTERN extern
#include <common.h>
#include <backupfile.h>
#include <dirname.h>
#include <inp.h>
#include <quotearg.h>
#include <util.h>
#undef XTERN
#define XTERN
#include <pch.h>
#define INITHUNKMAX 125 /* initial dynamic allocation size */
/* Patch (diff listing) abstract type. */
static FILE *pfp; /* patch file pointer */
static int p_says_nonexistent[2]; /* [0] for old file, [1] for new:
0 for existent and nonempty,
1 for existent and probably (but not necessarily) empty,
2 for nonexistent */
static int p_rfc934_nesting; /* RFC 934 nesting level */
static char *p_name[3]; /* filenames in patch headers */
static char *p_timestr[2]; /* timestamps as strings */
static off_t p_filesize; /* size of the patch file */
static lin p_first; /* 1st line number */
static lin p_newfirst; /* 1st line number of replacement */
static lin p_ptrn_lines; /* # lines in pattern */
static lin p_repl_lines; /* # lines in replacement text */
static lin p_end = -1; /* last line in hunk */
static lin p_max; /* max allowed value of p_end */
static lin p_prefix_context; /* # of prefix context lines */
static lin p_suffix_context; /* # of suffix context lines */
static lin p_input_line; /* current line # from patch file */
static char **p_line; /* the text of the hunk */
static size_t *p_len; /* line length including \n if any */
static char *p_Char; /* +, -, and ! */
static lin hunkmax = INITHUNKMAX; /* size of above arrays */
static size_t p_indent; /* indent to patch */
static bool p_strip_trailing_cr; /* true if stripping trailing \r */
static bool p_pass_comments_through; /* true if not ignoring # lines */
static file_offset p_base; /* where to intuit this time */
static lin p_bline; /* line # of p_base */
static file_offset p_start; /* where intuit found a patch */
static lin p_sline; /* and the line number for it */
static lin p_hunk_beg; /* line number of current hunk */
static lin p_efake = -1; /* end of faked up lines--don't free */
static lin p_bfake = -1; /* beg of faked up lines */
static char *p_c_function; /* the C function a hunk is in */
static char *scan_linenum (char *, lin *);
static enum diff intuit_diff_type (bool);
static enum nametype best_name (char * const *, int const *);
static int prefix_components (char *, bool);
static size_t pget_line (size_t, int, bool, bool);
static size_t get_line (void);
static bool incomplete_line (void);
static bool grow_hunkmax (void);
static void malformed (void) __attribute__ ((noreturn));
static void next_intuit_at (file_offset, lin);
static void skip_to (file_offset, lin);
static char get_ed_command_letter (char const *);
/* Prepare to look for the next patch in the patch file. */
void
re_patch (void)
{
p_first = 0;
p_newfirst = 0;
p_ptrn_lines = 0;
p_repl_lines = 0;
p_end = -1;
p_max = 0;
p_indent = 0;
p_strip_trailing_cr = false;
}
/* Open the patch file at the beginning of time. */
void
open_patch_file (char const *filename)
{
file_offset file_pos = 0;
struct stat st;
if (!filename || !*filename || strEQ (filename, "-"))
{
file_offset stdin_pos;
#if HAVE_SETMODE_DOS
if (binary_transput)
{
if (isatty (STDIN_FILENO))
fatal ("cannot read binary data from tty on this platform");
setmode (STDIN_FILENO, O_BINARY);
}
#endif
if (fstat (STDIN_FILENO, &st) != 0)
pfatal ("fstat");
if (S_ISREG (st.st_mode) && (stdin_pos = file_tell (stdin)) != -1)
{
pfp = stdin;
file_pos = stdin_pos;
}
else
{
size_t charsread;
int exclusive = TMPPATNAME_needs_removal ? 0 : O_EXCL;
TMPPATNAME_needs_removal = 1;
pfp = fdopen (create_file (TMPPATNAME,
O_RDWR | O_BINARY | exclusive,
(mode_t) 0, true),
"w+b");
if (!pfp)
pfatal ("Can't open stream for file %s", quotearg (TMPPATNAME));
for (st.st_size = 0;
(charsread = fread (buf, 1, bufsize, stdin)) != 0;
st.st_size += charsread)
if (fwrite (buf, 1, charsread, pfp) != charsread)
write_fatal ();
if (ferror (stdin) || fclose (stdin) != 0)
read_fatal ();
if (fflush (pfp) != 0
|| file_seek (pfp, (file_offset) 0, SEEK_SET) != 0)
write_fatal ();
}
}
else
{
pfp = fopen (filename, binary_transput ? "rb" : "r");
if (!pfp)
pfatal ("Can't open patch file %s", quotearg (filename));
if (fstat (fileno (pfp), &st) != 0)
pfatal ("fstat");
}
p_filesize = st.st_size;
if (p_filesize != (file_offset) p_filesize)
fatal ("patch file is too long");
next_intuit_at (file_pos, 1);
set_hunkmax();
}
/* Make sure our dynamically realloced tables are malloced to begin with. */
void
set_hunkmax (void)
{
if (!p_line)
p_line = (char **) malloc (hunkmax * sizeof *p_line);
if (!p_len)
p_len = (size_t *) malloc (hunkmax * sizeof *p_len);
if (!p_Char)
p_Char = malloc (hunkmax * sizeof *p_Char);
}
/* Enlarge the arrays containing the current hunk of patch. */
static bool
grow_hunkmax (void)
{
hunkmax *= 2;
assert (p_line && p_len && p_Char);
if ((p_line = (char **) realloc (p_line, hunkmax * sizeof (*p_line)))
&& (p_len = (size_t *) realloc (p_len, hunkmax * sizeof (*p_len)))
&& (p_Char = realloc (p_Char, hunkmax * sizeof (*p_Char))))
return true;
if (!using_plan_a)
xalloc_die ();
/* Don't free previous values of p_line etc.,
since some broken implementations free them for us.
Whatever is null will be allocated again from within plan_a (),
of all places. */
return false;
}
static bool
maybe_reverse (char const *name, bool nonexistent, bool is_empty)
{
bool looks_reversed = (! is_empty) < p_says_nonexistent[reverse ^ is_empty];
if (looks_reversed)
reverse ^=
ok_to_reverse ("The next patch%s would %s the file %s,\nwhich %s!",
reverse ? ", when reversed," : "",
(nonexistent ? "delete"
: is_empty ? "empty out"
: "create"),
quotearg (name),
(nonexistent ? "does not exist"
: is_empty ? "is already empty"
: "already exists"));
return looks_reversed;
}
/* True if the remainder of the patch file contains a diff of some sort. */
bool
there_is_another_patch (bool need_header)
{
if (p_base != 0 && p_base >= p_filesize) {
if (verbosity == VERBOSE)
say ("done\n");
return false;
}
if (verbosity == VERBOSE)
say ("Hmm...");
diff_type = intuit_diff_type (need_header);
if (diff_type == NO_DIFF) {
if (verbosity == VERBOSE)
say (p_base
? " Ignoring the trailing garbage.\ndone\n"
: " I can't seem to find a patch in there anywhere.\n");
if (! p_base && p_filesize)
fatal ("Only garbage was found in the patch input.");
return false;
}
if (skip_rest_of_patch)
{
Fseek (pfp, p_start, SEEK_SET);
p_input_line = p_sline - 1;
return true;
}
if (verbosity == VERBOSE)
say (" %sooks like %s to me...\n",
(p_base == 0 ? "L" : "The next patch l"),
diff_type == UNI_DIFF ? "a unified diff" :
diff_type == CONTEXT_DIFF ? "a context diff" :
diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
diff_type == NORMAL_DIFF ? "a normal diff" :
diff_type == GIT_BINARY_DIFF ? "a git binary diff" :
"an ed script" );
if (verbosity != SILENT)
{
if (p_indent)
say ("(Patch is indented %lu space%s.)\n",
(unsigned long int) p_indent, p_indent==1?"":"s");
if (no_strip_trailing_cr)
p_strip_trailing_cr = false;
else if (p_strip_trailing_cr)
say ("(Stripping trailing CRs from patch.)\n");
if (! inname)
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
say ("can't find file to patch at input line %s\n",
format_linenum (numbuf, p_sline));
if (diff_type != ED_DIFF && diff_type != NORMAL_DIFF)
say (strippath == -1
? "Perhaps you should have used the -p or --strip option?\n"
: "Perhaps you used the wrong -p or --strip option?\n");
}
}
skip_to(p_start,p_sline);
while (!inname) {
char *t;
if (force | batch) {
say ("No file to patch. Skipping patch.\n");
skip_rest_of_patch = true;
return true;
}
ask ("File to patch: ");
t = buf + strlen (buf);
if (t > buf + 1 && *(t - 1) == '\n')
{
inname = savebuf (buf, t - buf);
inname[t - buf - 1] = 0;
if (stat (inname, &instat) == 0)
{
inerrno = 0;
invc = -1;
}
else
{
perror (inname);
fflush (stderr);
free (inname);
inname = 0;
}
}
if (!inname) {
ask ("Skip this patch? [y] ");
if (*buf != 'n') {
if (verbosity != SILENT)
say ("Skipping patch.\n");
skip_rest_of_patch = true;
return true;
}
}
}
return true;
}
/* Determine what kind of diff is in the remaining part of the patch file. */
static enum diff
intuit_diff_type (bool need_header)
{
register file_offset this_line = 0;
register file_offset first_command_line = -1;
char first_ed_command_letter = 0;
lin fcl_line = 0; /* Pacify `gcc -W'. */
register bool this_is_a_command = false;
register bool stars_this_line = false;
bool git_diff = false;
enum nametype i;
struct stat st[3];
int stat_errno[3];
int version_controlled[3];
register enum diff retval;
for (i = OLD; i <= INDEX; i++)
if (p_name[i]) {
free (p_name[i]);
p_name[i] = 0;
}
for (i = OLD; i <= NEW; i++)
if (p_timestr[i])
{
free(p_timestr[i]);
p_timestr[i] = 0;
}
/* Ed and normal format patches don't have filename headers. */
if (diff_type == ED_DIFF || diff_type == NORMAL_DIFF)
need_header = false;
version_controlled[OLD] = -1;
version_controlled[NEW] = -1;
version_controlled[INDEX] = -1;
p_rfc934_nesting = 0;
p_timestamp[OLD].tv_sec = p_timestamp[NEW].tv_sec = -1;
p_says_nonexistent[OLD] = p_says_nonexistent[NEW] = 0;
Fseek (pfp, p_base, SEEK_SET);
p_input_line = p_bline - 1;
for (;;) {
register char *s;
register char *t;
register file_offset previous_line = this_line;
register bool last_line_was_command = this_is_a_command;
register bool stars_last_line = stars_this_line;
register size_t indent = 0;
char ed_command_letter;
bool strip_trailing_cr;
size_t chars_read;
this_line = file_tell (pfp);
chars_read = pget_line (0, 0, false, false);
if (chars_read == (size_t) -1)
xalloc_die ();
if (! chars_read) {
if (first_ed_command_letter) {
/* nothing but deletes!? */
p_start = first_command_line;
p_sline = fcl_line;
retval = ED_DIFF;
goto scan_exit;
}
else {
p_start = this_line;
p_sline = p_input_line;
return NO_DIFF;
}
}
strip_trailing_cr = 2 <= chars_read && buf[chars_read - 2] == '\r';
for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
if (*s == '\t')
indent = (indent + 8) & ~7;
else
indent++;
}
if (ISDIGIT (*s))
{
for (t = s + 1; ISDIGIT (*t) || *t == ','; t++)
/* do nothing */ ;
if (*t == 'd' || *t == 'c' || *t == 'a')
{
for (t++; ISDIGIT (*t) || *t == ','; t++)
/* do nothing */ ;
for (; *t == ' ' || *t == '\t'; t++)
/* do nothing */ ;
if (*t == '\r')
t++;
this_is_a_command = (*t == '\n');
}
}
if (! need_header
&& first_command_line < 0
&& ((ed_command_letter = get_ed_command_letter (s))
|| this_is_a_command)) {
first_command_line = this_line;
first_ed_command_letter = ed_command_letter;
fcl_line = p_input_line;
p_indent = indent; /* assume this for now */
p_strip_trailing_cr = strip_trailing_cr;
}
if (!stars_last_line && strnEQ(s, "*** ", 4))
{
free (p_name[OLD]);
p_name[OLD] = fetchname (s+4, strippath, &p_timestr[OLD],
&p_timestamp[OLD]);
need_header = false;
}
else if (strnEQ(s, "+++ ", 4))
{
/* Swap with NEW below. */
free (p_name[OLD]);
p_name[OLD] = fetchname (s+4, strippath, &p_timestr[OLD],
&p_timestamp[OLD]);
need_header = false;
p_strip_trailing_cr = strip_trailing_cr;
}
else if (strnEQ(s, "Index:", 6))
{
free (p_name[INDEX]);
p_name[INDEX] = fetchname (s+6, strippath, (char **) 0, NULL);
need_header = false;
p_strip_trailing_cr = strip_trailing_cr;
}
else if (strnEQ(s, "Prereq:", 7))
{
for (t = s + 7; ISSPACE ((unsigned char) *t); t++)
/* do nothing */ ;
revision = t;
for (t = revision; *t; t++)
if (ISSPACE ((unsigned char) *t))
{
char const *u;
for (u = t + 1; ISSPACE ((unsigned char) *u); u++)
/* do nothing */ ;
if (*u)
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
say ("Prereq: with multiple words at line %s of patch\n",
format_linenum (numbuf, this_line));
}
break;
}
if (t == revision)
revision = 0;
else {
char oldc = *t;
*t = '\0';
revision = savestr (revision);
*t = oldc;
}
}
else if (strnEQ (s, "diff --git ", 11))
{
char const *t;
if (! ((free (p_name[OLD]),
(p_name[OLD] = parse_name (s + 11, strippath, &t)))
&& ISSPACE (*t)
&& (free (p_name[NEW]),
(p_name[NEW] = parse_name (t, strippath, &t)))
&& (t = skip_spaces (t), ! *t)))
for (i = OLD; i <= NEW; i++)
{
free (p_name[i]);
p_name[i] = 0;
}
git_diff = true;
}
else if (git_diff && strnEQ (s, "deleted file mode ", 18))
p_says_nonexistent[NEW] = 2;
else if (git_diff && strnEQ (s, "new file mode ", 14))
p_says_nonexistent[OLD] = 2;
else if (git_diff && strnEQ (s, "GIT binary patch", 16))
{
p_start = this_line;
p_sline = p_input_line;
retval = GIT_BINARY_DIFF;
goto scan_exit;
}
else
{
for (t = s; t[0] == '-' && t[1] == ' '; t += 2)
/* do nothing */ ;
if (strnEQ(t, "--- ", 4))
{
struct timespec timestamp;
timestamp.tv_sec = -1;
free (p_name[NEW]);
p_name[NEW] = fetchname (t+4, strippath, &p_timestr[NEW],
×tamp);
need_header = false;
if (timestamp.tv_sec != -1)
{
p_timestamp[NEW] = timestamp;
p_rfc934_nesting = (t - s) >> 1;
}
p_strip_trailing_cr = strip_trailing_cr;
}
}
if (need_header)
continue;
if ((diff_type == NO_DIFF || diff_type == ED_DIFF) &&
first_command_line >= 0 &&
strEQ(s, ".\n") ) {
p_start = first_command_line;
p_sline = fcl_line;
retval = ED_DIFF;
goto scan_exit;
}
if ((diff_type == NO_DIFF || diff_type == UNI_DIFF)
&& strnEQ(s, "@@ -", 4)) {
/* `p_name', `p_timestr', and `p_timestamp' are backwards;
swap them. */
struct timespec ti = p_timestamp[OLD];
p_timestamp[OLD] = p_timestamp[NEW];
p_timestamp[NEW] = ti;
t = p_name[OLD];
p_name[OLD] = p_name[NEW];
p_name[NEW] = t;
t = p_timestr[OLD];
p_timestr[OLD] = p_timestr[NEW];
p_timestr[NEW] = t;
s += 4;
if (s[0] == '0' && !ISDIGIT (s[1]))
p_says_nonexistent[OLD] = 1 + ! p_timestamp[OLD].tv_sec;
while (*s != ' ' && *s != '\n')
s++;
while (*s == ' ')
s++;
if (s[0] == '+' && s[1] == '0' && !ISDIGIT (s[2]))
p_says_nonexistent[NEW] = 1 + ! p_timestamp[NEW].tv_sec;
p_indent = indent;
p_start = this_line;
p_sline = p_input_line;
retval = UNI_DIFF;
if (! ((p_name[OLD] || ! p_timestamp[OLD].tv_sec)
&& (p_name[NEW] || ! p_timestamp[NEW].tv_sec))
&& ! p_name[INDEX] && need_header)
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
say ("missing header for unified diff at line %s of patch\n",
format_linenum (numbuf, p_sline));
}
goto scan_exit;
}
stars_this_line = strnEQ(s, "********", 8);
if ((diff_type == NO_DIFF
|| diff_type == CONTEXT_DIFF
|| diff_type == NEW_CONTEXT_DIFF)
&& stars_last_line && strnEQ (s, "*** ", 4)) {
s += 4;
if (s[0] == '0' && !ISDIGIT (s[1]))
p_says_nonexistent[OLD] = 1 + ! p_timestamp[OLD].tv_sec;
/* if this is a new context diff the character just before */
/* the newline is a '*'. */
while (*s != '\n')
s++;
p_indent = indent;
p_strip_trailing_cr = strip_trailing_cr;
p_start = previous_line;
p_sline = p_input_line - 1;
retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
{
/* Scan the first hunk to see whether the file contents
appear to have been deleted. */
file_offset saved_p_base = p_base;
lin saved_p_bline = p_bline;
Fseek (pfp, previous_line, SEEK_SET);
p_input_line -= 2;
if (another_hunk (retval, false)
&& ! p_repl_lines && p_newfirst == 1)
p_says_nonexistent[NEW] = 1 + ! p_timestamp[NEW].tv_sec;
next_intuit_at (saved_p_base, saved_p_bline);
}
if (! ((p_name[OLD] || ! p_timestamp[OLD].tv_sec)
&& (p_name[NEW] || ! p_timestamp[NEW].tv_sec))
&& ! p_name[INDEX] && need_header)
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
say ("missing header for context diff at line %s of patch\n",
format_linenum (numbuf, p_sline));
}
goto scan_exit;
}
if ((diff_type == NO_DIFF || diff_type == NORMAL_DIFF) &&
last_line_was_command &&
(strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
p_start = previous_line;
p_sline = p_input_line - 1;
p_indent = indent;
retval = NORMAL_DIFF;
goto scan_exit;
}
}
scan_exit:
/* To intuit `inname', the name of the file to patch,
use the algorithm specified by POSIX 1003.1-2001 XCU lines 25680-26599
(with some modifications if posixly_correct is zero):
- Take the old and new names from the context header if present,
and take the index name from the `Index:' line if present and
if either the old and new names are both absent
or posixly_correct is nonzero.
Consider the file names to be in the order (old, new, index).
- If some named files exist, use the first one if posixly_correct
is nonzero, the best one otherwise.
- If patch_get is nonzero, and no named files exist,
but an RCS or SCCS master file exists,
use the first named file with an RCS or SCCS master.
- If no named files exist, no RCS or SCCS master was found,
some names are given, posixly_correct is zero,
and the patch appears to create a file, then use the best name
requiring the creation of the fewest directories.
- Otherwise, report failure by setting `inname' to 0;
this causes our invoker to ask the user for a file name. */
i = NONE;
if (!inname)
{
enum nametype i0 = NONE;
if (! posixly_correct && (p_name[OLD] || p_name[NEW]) && p_name[INDEX])
{
free (p_name[INDEX]);
p_name[INDEX] = 0;
}
for (i = OLD; i <= INDEX; i++)
if (p_name[i])
{
if (i0 != NONE && strcmp (p_name[i0], p_name[i]) == 0)
{
/* It's the same name as before; reuse stat results. */
stat_errno[i] = stat_errno[i0];
if (! stat_errno[i])
st[i] = st[i0];
}
else if (stat (p_name[i], &st[i]) != 0)
stat_errno[i] = errno;
else
{
stat_errno[i] = 0;
if (posixly_correct)
break;
}
i0 = i;
}
if (! posixly_correct)
{
/* The best of all existing files. */
i = best_name (p_name, stat_errno);
if (i == NONE && patch_get)
{
enum nametype nope = NONE;
for (i = OLD; i <= INDEX; i++)
if (p_name[i])
{
char const *cs;
char *getbuf;
char *diffbuf;
bool readonly = (outfile
&& strcmp (outfile, p_name[i]) != 0);
if (nope == NONE || strcmp (p_name[nope], p_name[i]) != 0)
{
cs = (version_controller
(p_name[i], readonly, (struct stat *) 0,
&getbuf, &diffbuf));
version_controlled[i] = !! cs;
if (cs)
{
if (version_get (p_name[i], cs, false, readonly,
getbuf, &st[i]))
stat_errno[i] = 0;
else
version_controlled[i] = 0;
free (getbuf);
if (diffbuf)
free (diffbuf);
if (! stat_errno[i])
break;
}
}
nope = i;
}
}
if (i0 != NONE
&& maybe_reverse (p_name[i0], i == NONE,
i == NONE || st[i].st_size == 0))
i = i0;
if (i == NONE && p_says_nonexistent[reverse])
{
int newdirs[3];
int newdirs_min = INT_MAX;
int distance_from_minimum[3];
for (i = OLD; i <= INDEX; i++)
if (p_name[i])
{
newdirs[i] = (prefix_components (p_name[i], false)
- prefix_components (p_name[i], true));
if (newdirs[i] < newdirs_min)
newdirs_min = newdirs[i];
}
for (i = OLD; i <= INDEX; i++)
if (p_name[i])
distance_from_minimum[i] = newdirs[i] - newdirs_min;
/* The best of the filenames which create the fewest directories. */
i = best_name (p_name, distance_from_minimum);
}
}
}
if (i == NONE)
{
if (inname)
{
inerrno = stat (inname, &instat) == 0 ? 0 : errno;
maybe_reverse (inname, inerrno, inerrno || instat.st_size == 0);
}
else
inerrno = -1;
}
else
{
inname = savestr(p_name[i]);
inerrno = stat_errno[i];
invc = version_controlled[i];
instat = st[i];
}
return retval;
}
/* Count the path name components in FILENAME's prefix.
If CHECKDIRS is true, count only existing directories. */
static int
prefix_components (char *filename, bool checkdirs)
{
int count = 0;
struct stat stat_buf;
int stat_result;
char *f = filename + FILE_SYSTEM_PREFIX_LEN (filename);
if (*f)
while (*++f)
if (ISSLASH (f[0]) && ! ISSLASH (f[-1]))
{
if (checkdirs)
{
*f = '\0';
stat_result = stat (filename, &stat_buf);
*f = '/';
if (! (stat_result == 0 && S_ISDIR (stat_buf.st_mode)))
break;
}
count++;
}
return count;
}
/* Return the index of the best of NAME[OLD], NAME[NEW], and NAME[INDEX].
Ignore null names, and ignore NAME[i] if IGNORE[i] is nonzero.
Return NONE if all names are ignored. */
static enum nametype
best_name (char *const *name, int const *ignore)
{
enum nametype i;
int components[3];
int components_min = INT_MAX;
size_t basename_len[3];
size_t basename_len_min = SIZE_MAX;
size_t len[3];
size_t len_min = SIZE_MAX;
for (i = OLD; i <= INDEX; i++)
if (name[i] && !ignore[i])
{
/* Take the names with the fewest prefix components. */
components[i] = prefix_components (name[i], false);
if (components_min < components[i])
continue;
components_min = components[i];
/* Of those, take the names with the shortest basename. */
basename_len[i] = base_len (name[i]);
if (basename_len_min < basename_len[i])
continue;
basename_len_min = basename_len[i];
/* Of those, take the shortest names. */
len[i] = strlen (name[i]);
if (len_min < len[i])
continue;
len_min = len[i];
}
/* Of those, take the first name. */
for (i = OLD; i <= INDEX; i++)
if (name[i] && !ignore[i]
&& components[i] == components_min
&& basename_len[i] == basename_len_min
&& len[i] == len_min)
break;
return i;
}
/* Remember where this patch ends so we know where to start up again. */
static void
next_intuit_at (file_offset file_pos, lin file_line)
{
p_base = file_pos;
p_bline = file_line;
}
/* Basically a verbose fseek() to the actual diff listing. */
static void
skip_to (file_offset file_pos, lin file_line)
{
register FILE *i = pfp;
register FILE *o = stdout;
register int c;
assert(p_base <= file_pos);
if ((verbosity == VERBOSE || !inname) && p_base < file_pos) {
Fseek (i, p_base, SEEK_SET);
say ("The text leading up to this was:\n--------------------------\n");
while (file_tell (i) < file_pos)
{
putc ('|', o);
do
{
if ((c = getc (i)) == EOF)
read_fatal ();
putc (c, o);
}
while (c != '\n');
}
say ("--------------------------\n");
}
else
Fseek (i, file_pos, SEEK_SET);
p_input_line = file_line - 1;
}
/* Make this a function for better debugging. */
static void
malformed (void)
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
fatal ("malformed patch at line %s: %s",
format_linenum (numbuf, p_input_line), buf);
/* about as informative as "Syntax error" in C */
}
/* Parse a line number from a string.
Return the address of the first char after the number. */
static char *
scan_linenum (char *s0, lin *linenum)
{
char *s;
lin n = 0;
bool overflow = false;
char numbuf[LINENUM_LENGTH_BOUND + 1];
for (s = s0; ISDIGIT (*s); s++)
{
lin new_n = 10 * n + (*s - '0');
overflow |= new_n / 10 != n;
n = new_n;
}
if (s == s0)
fatal ("missing line number at line %s: %s",
format_linenum (numbuf, p_input_line), buf);
if (overflow)
fatal ("line number %.*s is too large at line %s: %s",
(int) (s - s0), s0, format_linenum (numbuf, p_input_line), buf);
*linenum = n;
return s;
}
/* 1 if there is more of the current diff listing to process;
0 if not; -1 if ran out of memory. */
int
another_hunk (enum diff difftype, bool rev)
{
register char *s;
register lin context = 0;
register size_t chars_read;
char numbuf0[LINENUM_LENGTH_BOUND + 1];
char numbuf1[LINENUM_LENGTH_BOUND + 1];
char numbuf2[LINENUM_LENGTH_BOUND + 1];
char numbuf3[LINENUM_LENGTH_BOUND + 1];
while (p_end >= 0) {
if (p_end == p_efake)
p_end = p_bfake; /* don't free twice */
else
free(p_line[p_end]);
p_end--;
}
assert(p_end == -1);
p_efake = -1;
if (p_c_function)
{
free (p_c_function);
p_c_function = NULL;
}
p_max = hunkmax; /* gets reduced when --- found */
if (difftype == CONTEXT_DIFF || difftype == NEW_CONTEXT_DIFF) {
file_offset line_beginning = file_tell (pfp);
/* file pos of the current line */
lin repl_beginning = 0; /* index of --- line */
register lin fillcnt = 0; /* #lines of missing ptrn or repl */
register lin fillsrc; /* index of first line to copy */
register lin filldst; /* index of first missing line */
bool ptrn_spaces_eaten = false; /* ptrn was slightly misformed */
bool some_context = false; /* (perhaps internal) context seen */
register bool repl_could_be_missing = true;
bool ptrn_missing = false; /* The pattern was missing. */
bool repl_missing = false; /* Likewise for replacement. */
file_offset repl_backtrack_position = 0;
/* file pos of first repl line */
lin repl_patch_line; /* input line number for same */
lin repl_context; /* context for same */
lin ptrn_prefix_context = -1; /* lines in pattern prefix context */
lin ptrn_suffix_context = -1; /* lines in pattern suffix context */
lin repl_prefix_context = -1; /* lines in replac. prefix context */
lin ptrn_copiable = 0; /* # of copiable lines in ptrn */
lin repl_copiable = 0; /* Likewise for replacement. */
/* Pacify `gcc -Wall'. */
fillsrc = filldst = repl_patch_line = repl_context = 0;
chars_read = get_line ();
if (chars_read == (size_t) -1
|| chars_read <= 8
|| strncmp (buf, "********", 8) != 0) {
next_intuit_at(line_beginning,p_input_line);
return chars_read == (size_t) -1 ? -1 : 0;
}
s = buf;
while (*s == '*')
s++;
if (*s == ' ')
{
p_c_function = s;
while (*s != '\n')
s++;
*s = '\0';
p_c_function = savestr (p_c_function);
}
p_hunk_beg = p_input_line + 1;
while (p_end < p_max) {
chars_read = get_line ();
if (chars_read == (size_t) -1)
return -1;
if (!chars_read) {
if (repl_beginning && repl_could_be_missing) {
repl_missing = true;
goto hunk_done;
}
if (p_max - p_end < 4) {
strcpy (buf, " \n"); /* assume blank lines got chopped */
chars_read = 3;
} else {
fatal ("unexpected end of file in patch");
}
}
p_end++;
if (p_end == hunkmax)
fatal ("unterminated hunk starting at line %s; giving up at line %s: %s",
format_linenum (numbuf0, pch_hunk_beg ()),
format_linenum (numbuf1, p_input_line), buf);
assert(p_end < hunkmax);
p_Char[p_end] = *buf;
p_len[p_end] = 0;
p_line[p_end] = 0;
switch (*buf) {
case '*':
if (strnEQ(buf, "********", 8)) {
if (repl_beginning && repl_could_be_missing) {
repl_missing = true;
goto hunk_done;
}
else
fatal ("unexpected end of hunk at line %s",
format_linenum (numbuf0, p_input_line));
}
if (p_end != 0) {
if (repl_beginning && repl_could_be_missing) {
repl_missing = true;
goto hunk_done;
}
fatal ("unexpected `***' at line %s: %s",
format_linenum (numbuf0, p_input_line), buf);
}
context = 0;
p_len[p_end] = strlen (buf);
if (! (p_line[p_end] = savestr (buf))) {
p_end--;
return -1;
}
for (s = buf; *s && !ISDIGIT (*s); s++)
/* do nothing */ ;
if (strnEQ(s,"0,0",3))
remove_prefix (s, 2);
s = scan_linenum (s, &p_first);
if (*s == ',') {
while (*s && !ISDIGIT (*s))
s++;
scan_linenum (s, &p_ptrn_lines);
p_ptrn_lines += 1 - p_first;
}
else if (p_first)
p_ptrn_lines = 1;
else {
p_ptrn_lines = 0;
p_first = 1;
}
p_max = p_ptrn_lines + 6; /* we need this much at least */
while (p_max + 1 >= hunkmax)
if (! grow_hunkmax ())
return -1;
p_max = hunkmax;
break;
case '-':
if (buf[1] != '-')
goto change_line;
if (ptrn_prefix_context == -1)
ptrn_prefix_context = context;
ptrn_suffix_context = context;
if (repl_beginning
|| (p_end
!= p_ptrn_lines + 1 + (p_Char[p_end - 1] == '\n')))
{
if (p_end == 1)
{
/* `Old' lines were omitted. Set up to fill
them in from `new' context lines. */
ptrn_missing = true;
p_end = p_ptrn_lines + 1;
ptrn_prefix_context = ptrn_suffix_context = -1;
fillsrc = p_end + 1;
filldst = 1;
fillcnt = p_ptrn_lines;
}
else if (! repl_beginning)
fatal ("%s `---' at line %s; check line numbers at line %s",
(p_end <= p_ptrn_lines
? "Premature"
: "Overdue"),
format_linenum (numbuf0, p_input_line),
format_linenum (numbuf1, p_hunk_beg));
else if (! repl_could_be_missing)
fatal ("duplicate `---' at line %s; check line numbers at line %s",
format_linenum (numbuf0, p_input_line),
format_linenum (numbuf1,
p_hunk_beg + repl_beginning));
else
{
repl_missing = true;
goto hunk_done;
}
}
repl_beginning = p_end;
repl_backtrack_position = file_tell (pfp);
repl_patch_line = p_input_line;
repl_context = context;
p_len[p_end] = strlen (buf);
if (! (p_line[p_end] = savestr (buf)))
{
p_end--;
return -1;
}
p_Char[p_end] = '=';
for (s = buf; *s && ! ISDIGIT (*s); s++)
/* do nothing */ ;
s = scan_linenum (s, &p_newfirst);
if (*s == ',')
{
do
{
if (!*++s)
malformed ();
}
while (! ISDIGIT (*s));
scan_linenum (s, &p_repl_lines);
p_repl_lines += 1 - p_newfirst;
}
else if (p_newfirst)
p_repl_lines = 1;
else
{
p_repl_lines = 0;
p_newfirst = 1;
}
p_max = p_repl_lines + p_end;
while (p_max + 1 >= hunkmax)
if (! grow_hunkmax ())
return -1;
if (p_repl_lines != ptrn_copiable
&& (p_prefix_context != 0
|| context != 0
|| p_repl_lines != 1))
repl_could_be_missing = false;
context = 0;
break;
case '+': case '!':
repl_could_be_missing = false;
change_line:
s = buf + 1;
chars_read--;
if (*s == '\n' && canonicalize) {
strcpy (s, " \n");
chars_read = 2;
}
if (*s == ' ' || *s == '\t') {
s++;
chars_read--;
} else if (repl_beginning && repl_could_be_missing) {
repl_missing = true;
goto hunk_done;
}
if (! repl_beginning)
{
if (ptrn_prefix_context == -1)
ptrn_prefix_context = context;
}
else
{
if (repl_prefix_context == -1)
repl_prefix_context = context;
}
chars_read -=
(1 < chars_read
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
p_line[p_end] = savebuf (s, chars_read);
if (chars_read && ! p_line[p_end]) {
p_end--;
return -1;
}
context = 0;
break;
case '\t': case '\n': /* assume spaces got eaten */
s = buf;
if (*buf == '\t') {
s++;
chars_read--;
}
if (repl_beginning && repl_could_be_missing &&
(!ptrn_spaces_eaten || difftype == NEW_CONTEXT_DIFF) ) {
repl_missing = true;
goto hunk_done;
}
chars_read -=
(1 < chars_read
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
p_line[p_end] = savebuf (buf, chars_read);
if (chars_read && ! p_line[p_end]) {
p_end--;
return -1;
}
if (p_end != p_ptrn_lines + 1) {
ptrn_spaces_eaten |= (repl_beginning != 0);
some_context = true;
context++;
if (repl_beginning)
repl_copiable++;
else
ptrn_copiable++;
p_Char[p_end] = ' ';
}
break;
case ' ':
s = buf + 1;
chars_read--;
if (*s == '\n' && canonicalize) {
strcpy (s, "\n");
chars_read = 2;
}
if (*s == ' ' || *s == '\t') {
s++;
chars_read--;
} else if (repl_beginning && repl_could_be_missing) {
repl_missing = true;
goto hunk_done;
}
some_context = true;
context++;
if (repl_beginning)
repl_copiable++;
else
ptrn_copiable++;
chars_read -=
(1 < chars_read
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
p_line[p_end] = savebuf (s, chars_read);
if (chars_read && ! p_line[p_end]) {
p_end--;
return -1;
}
break;
default:
if (repl_beginning && repl_could_be_missing) {
repl_missing = true;
goto hunk_done;
}
malformed ();
}
}
hunk_done:
if (p_end >=0 && !repl_beginning)
fatal ("no `---' found in patch at line %s",
format_linenum (numbuf0, pch_hunk_beg ()));
if (repl_missing) {
/* reset state back to just after --- */
p_input_line = repl_patch_line;
context = repl_context;
for (p_end--; p_end > repl_beginning; p_end--)
free(p_line[p_end]);
Fseek (pfp, repl_backtrack_position, SEEK_SET);
/* redundant 'new' context lines were omitted - set */
/* up to fill them in from the old file context */
fillsrc = 1;
filldst = repl_beginning+1;
fillcnt = p_repl_lines;
p_end = p_max;
}
else if (! ptrn_missing && ptrn_copiable != repl_copiable)
fatal ("context mangled in hunk at line %s",
format_linenum (numbuf0, p_hunk_beg));
else if (!some_context && fillcnt == 1) {
/* the first hunk was a null hunk with no context */
/* and we were expecting one line -- fix it up. */
while (filldst < p_end) {
p_line[filldst] = p_line[filldst+1];
p_Char[filldst] = p_Char[filldst+1];
p_len[filldst] = p_len[filldst+1];
filldst++;
}
#if 0
repl_beginning--; /* this doesn't need to be fixed */
#endif
p_end--;
p_first++; /* do append rather than insert */
fillcnt = 0;
p_ptrn_lines = 0;
}
p_prefix_context = ((repl_prefix_context == -1
|| (ptrn_prefix_context != -1
&& ptrn_prefix_context < repl_prefix_context))
? ptrn_prefix_context : repl_prefix_context);
p_suffix_context = ((ptrn_suffix_context != -1
&& ptrn_suffix_context < context)
? ptrn_suffix_context : context);
assert (p_prefix_context != -1 && p_suffix_context != -1);
if (difftype == CONTEXT_DIFF
&& (fillcnt
|| (p_first > 1
&& p_prefix_context + p_suffix_context < ptrn_copiable))) {
if (verbosity == VERBOSE)
say ("%s\n%s\n%s\n",
"(Fascinating -- this is really a new-style context diff but without",
"the telltale extra asterisks on the *** line that usually indicate",
"the new style...)");
diff_type = difftype = NEW_CONTEXT_DIFF;
}
/* if there were omitted context lines, fill them in now */
if (fillcnt) {
p_bfake = filldst; /* remember where not to free() */
p_efake = filldst + fillcnt - 1;
while (fillcnt-- > 0) {
while (fillsrc <= p_end && fillsrc != repl_beginning
&& p_Char[fillsrc] != ' ')
fillsrc++;
if (p_end < fillsrc || fillsrc == repl_beginning)
{
fatal ("replacement text or line numbers mangled in hunk at line %s",
format_linenum (numbuf0, p_hunk_beg));
}
p_line[filldst] = p_line[fillsrc];
p_Char[filldst] = p_Char[fillsrc];
p_len[filldst] = p_len[fillsrc];
fillsrc++; filldst++;
}
while (fillsrc <= p_end && fillsrc != repl_beginning)
{
if (p_Char[fillsrc] == ' ')
fatal ("replacement text or line numbers mangled in hunk at line %s",
format_linenum (numbuf0, p_hunk_beg));
fillsrc++;
}
if (debug & 64)
printf ("fillsrc %s, filldst %s, rb %s, e+1 %s\n",
format_linenum (numbuf0, fillsrc),
format_linenum (numbuf1, filldst),
format_linenum (numbuf2, repl_beginning),
format_linenum (numbuf3, p_end + 1));
assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
assert(filldst==p_end+1 || filldst==repl_beginning);
}
}
else if (difftype == UNI_DIFF) {
file_offset line_beginning = file_tell (pfp); /* file pos of the current line */
register lin fillsrc; /* index of old lines */
register lin filldst; /* index of new lines */
char ch = '\0';
chars_read = get_line ();
if (chars_read == (size_t) -1
|| chars_read <= 4
|| strncmp (buf, "@@ -", 4) != 0) {
next_intuit_at(line_beginning,p_input_line);
return chars_read == (size_t) -1 ? -1 : 0;
}
s = scan_linenum (buf + 4, &p_first);
if (*s == ',')
s = scan_linenum (s + 1, &p_ptrn_lines);
else
p_ptrn_lines = 1;
if (*s == ' ') s++;
if (*s != '+')
malformed ();
s = scan_linenum (s + 1, &p_newfirst);
if (*s == ',')
s = scan_linenum (s + 1, &p_repl_lines);
else
p_repl_lines = 1;
if (*s == ' ') s++;
if (*s++ != '@')
malformed ();
if (*s++ == '@' && *s == ' ' && *s != '\0')
{
p_c_function = s;
while (*s != '\n')
s++;
*s = '\0';
p_c_function = savestr (p_c_function);
}
if (!p_ptrn_lines)
p_first++; /* do append rather than insert */
if (!p_repl_lines)
p_newfirst++;
p_max = p_ptrn_lines + p_repl_lines + 1;
while (p_max + 1 >= hunkmax)
if (! grow_hunkmax ())
return -1;
fillsrc = 1;
filldst = fillsrc + p_ptrn_lines;
p_end = filldst + p_repl_lines;
sprintf (buf, "*** %s,%s ****\n",
format_linenum (numbuf0, p_first),
format_linenum (numbuf1, p_first + p_ptrn_lines - 1));
p_len[0] = strlen (buf);
if (! (p_line[0] = savestr (buf))) {
p_end = -1;
return -1;
}
p_Char[0] = '*';
sprintf (buf, "--- %s,%s ----\n",
format_linenum (numbuf0, p_newfirst),
format_linenum (numbuf1, p_newfirst + p_repl_lines - 1));
p_len[filldst] = strlen (buf);
if (! (p_line[filldst] = savestr (buf))) {
p_end = 0;
return -1;
}
p_Char[filldst++] = '=';
p_prefix_context = -1;
p_hunk_beg = p_input_line + 1;
while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
chars_read = get_line ();
if (!chars_read) {
if (p_max - filldst < 3) {
strcpy (buf, " \n"); /* assume blank lines got chopped */
chars_read = 2;
} else {
fatal ("unexpected end of file in patch");
}
}
if (chars_read == (size_t) -1)
s = 0;
else if (*buf == '\t' || *buf == '\n') {
ch = ' '; /* assume the space got eaten */
s = savebuf (buf, chars_read);
}
else {
ch = *buf;
s = savebuf (buf+1, --chars_read);
}
if (chars_read && ! s)
{
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
return -1;
}
switch (ch) {
case '-':
if (fillsrc > p_ptrn_lines) {
free(s);
p_end = filldst-1;
malformed ();
}
chars_read -= fillsrc == p_ptrn_lines && incomplete_line ();
p_Char[fillsrc] = ch;
p_line[fillsrc] = s;
p_len[fillsrc++] = chars_read;
break;
case '=':
ch = ' ';
/* FALL THROUGH */
case ' ':
if (fillsrc > p_ptrn_lines) {
free(s);
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
malformed ();
}
context++;
chars_read -= fillsrc == p_ptrn_lines && incomplete_line ();
p_Char[fillsrc] = ch;
p_line[fillsrc] = s;
p_len[fillsrc++] = chars_read;
s = savebuf (s, chars_read);
if (chars_read && ! s) {
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
return -1;
}
/* FALL THROUGH */
case '+':
if (filldst > p_end) {
free(s);
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
malformed ();
}
chars_read -= filldst == p_end && incomplete_line ();
p_Char[filldst] = ch;
p_line[filldst] = s;
p_len[filldst++] = chars_read;
break;
default:
p_end = filldst;
malformed ();
}
if (ch != ' ') {
if (p_prefix_context == -1)
p_prefix_context = context;
context = 0;
}
}/* while */
if (p_prefix_context == -1)
malformed ();
p_suffix_context = context;
}
else { /* normal diff--fake it up */
char hunk_type;
register int i;
lin min, max;
file_offset line_beginning = file_tell (pfp);
p_prefix_context = p_suffix_context = 0;
chars_read = get_line ();
if (chars_read == (size_t) -1 || !chars_read || !ISDIGIT (*buf)) {
next_intuit_at(line_beginning,p_input_line);
return chars_read == (size_t) -1 ? -1 : 0;
}
s = scan_linenum (buf, &p_first);
if (*s == ',') {
s = scan_linenum (s + 1, &p_ptrn_lines);
p_ptrn_lines += 1 - p_first;
}
else
p_ptrn_lines = (*s != 'a');
hunk_type = *s;
if (hunk_type == 'a')
p_first++; /* do append rather than insert */
s = scan_linenum (s + 1, &min);
if (*s == ',')
scan_linenum (s + 1, &max);
else
max = min;
if (hunk_type == 'd')
min++;
p_end = p_ptrn_lines + 1 + max - min + 1;
while (p_end + 1 >= hunkmax)
if (! grow_hunkmax ())
{
p_end = -1;
return -1;
}
p_newfirst = min;
p_repl_lines = max - min + 1;
sprintf (buf, "*** %s,%s\n",
format_linenum (numbuf0, p_first),
format_linenum (numbuf1, p_first + p_ptrn_lines - 1));
p_len[0] = strlen (buf);
if (! (p_line[0] = savestr (buf))) {
p_end = -1;
return -1;
}
p_Char[0] = '*';
for (i=1; i<=p_ptrn_lines; i++) {
chars_read = get_line ();
if (chars_read == (size_t) -1)
{
p_end = i - 1;
return -1;
}
if (!chars_read)
fatal ("unexpected end of file in patch at line %s",
format_linenum (numbuf0, p_input_line));
if (buf[0] != '<' || (buf[1] != ' ' && buf[1] != '\t'))
fatal ("`<' expected at line %s of patch",
format_linenum (numbuf0, p_input_line));
chars_read -= 2 + (i == p_ptrn_lines && incomplete_line ());
p_len[i] = chars_read;
p_line[i] = savebuf (buf + 2, chars_read);
if (chars_read && ! p_line[i]) {
p_end = i-1;
return -1;
}
p_Char[i] = '-';
}
if (hunk_type == 'c') {
chars_read = get_line ();
if (chars_read == (size_t) -1)
{
p_end = i - 1;
return -1;
}
if (! chars_read)
fatal ("unexpected end of file in patch at line %s",
format_linenum (numbuf0, p_input_line));
if (*buf != '-')
fatal ("`---' expected at line %s of patch",
format_linenum (numbuf0, p_input_line));
}
sprintf (buf, "--- %s,%s\n",
format_linenum (numbuf0, min),
format_linenum (numbuf1, max));
p_len[i] = strlen (buf);
if (! (p_line[i] = savestr (buf))) {
p_end = i-1;
return -1;
}
p_Char[i] = '=';
for (i++; i<=p_end; i++) {
chars_read = get_line ();
if (chars_read == (size_t) -1)
{
p_end = i - 1;
return -1;
}
if (!chars_read)
fatal ("unexpected end of file in patch at line %s",
format_linenum (numbuf0, p_input_line));
if (buf[0] != '>' || (buf[1] != ' ' && buf[1] != '\t'))
fatal ("`>' expected at line %s of patch",
format_linenum (numbuf0, p_input_line));
chars_read -= 2 + (i == p_end && incomplete_line ());
p_len[i] = chars_read;
p_line[i] = savebuf (buf + 2, chars_read);
if (chars_read && ! p_line[i]) {
p_end = i-1;
return -1;
}
p_Char[i] = '+';
}
}
if (rev) /* backwards patch? */
if (!pch_swap())
say ("Not enough memory to swap next hunk!\n");
assert (p_end + 1 < hunkmax);
p_Char[p_end + 1] = '^'; /* add a stopper for apply_hunk */
if (debug & 2) {
lin i;
for (i = 0; i <= p_end + 1; i++) {
fprintf (stderr, "%s %c",
format_linenum (numbuf0, i),
p_Char[i]);
if (p_Char[i] == '*')
fprintf (stderr, " %s,%s\n",
format_linenum (numbuf0, p_first),
format_linenum (numbuf1, p_ptrn_lines));
else if (p_Char[i] == '=')
fprintf (stderr, " %s,%s\n",
format_linenum (numbuf0, p_newfirst),
format_linenum (numbuf1, p_repl_lines));
else if (p_Char[i] != '^')
{
fputs(" |", stderr);
pch_write_line (i, stderr);
}
else
fputc('\n', stderr);
}
fflush (stderr);
}
return 1;
}
static size_t
get_line (void)
{
return pget_line (p_indent, p_rfc934_nesting, p_strip_trailing_cr,
p_pass_comments_through);
}
/* Input a line from the patch file, worrying about indentation.
Strip up to INDENT characters' worth of leading indentation.
Then remove up to RFC934_NESTING instances of leading "- ".
If STRIP_TRAILING_CR is true, remove any trailing carriage-return.
Unless PASS_COMMENTS_THROUGH is true, ignore any resulting lines
that begin with '#'; they're comments.
Ignore any partial lines at end of input, but warn about them.
Succeed if a line was read; it is terminated by "\n\0" for convenience.
Return the number of characters read, including '\n' but not '\0'.
Return -1 if we ran out of memory. */
static size_t
pget_line (size_t indent, int rfc934_nesting, bool strip_trailing_cr,
bool pass_comments_through)
{
register FILE *fp = pfp;
register int c;
register size_t i;
register char *b;
register size_t s;
do
{
i = 0;
for (;;)
{
c = getc (fp);
if (c == EOF)
{
if (ferror (fp))
read_fatal ();
return 0;
}
if (indent <= i)
break;
if (c == ' ' || c == 'X')
i++;
else if (c == '\t')
i = (i + 8) & ~7;
else
break;
}
i = 0;
b = buf;
while (c == '-' && 0 <= --rfc934_nesting)
{
c = getc (fp);
if (c == EOF)
goto patch_ends_in_middle_of_line;
if (c != ' ')
{
i = 1;
b[0] = '-';
break;
}
c = getc (fp);
if (c == EOF)
goto patch_ends_in_middle_of_line;
}
s = bufsize;
for (;;)
{
if (i == s - 1)
{
s *= 2;
b = realloc (b, s);
if (!b)
{
if (!using_plan_a)
xalloc_die ();
return (size_t) -1;
}
buf = b;
bufsize = s;
}
b[i++] = c;
if (c == '\n')
break;
c = getc (fp);
if (c == EOF)
goto patch_ends_in_middle_of_line;
}
p_input_line++;
}
while (*b == '#' && !pass_comments_through);
if (strip_trailing_cr && 2 <= i && b[i - 2] == '\r')
b[i-- - 2] = '\n';
b[i] = '\0';
return i;
patch_ends_in_middle_of_line:
if (ferror (fp))
read_fatal ();
say ("patch unexpectedly ends in middle of line\n");
return 0;
}
static bool
incomplete_line (void)
{
register FILE *fp = pfp;
register int c;
register file_offset line_beginning = file_tell (fp);
if (getc (fp) == '\\')
{
while ((c = getc (fp)) != '\n' && c != EOF)
/* do nothing */ ;
return true;
}
else
{
/* We don't trust ungetc. */
Fseek (pfp, line_beginning, SEEK_SET);
return false;
}
}
/* Reverse the old and new portions of the current hunk. */
bool
pch_swap (void)
{
char **tp_line; /* the text of the hunk */
size_t *tp_len; /* length of each line */
char *tp_char; /* +, -, and ! */
register lin i;
register lin n;
bool blankline = false;
register char *s;
i = p_first;
p_first = p_newfirst;
p_newfirst = i;
/* make a scratch copy */
tp_line = p_line;
tp_len = p_len;
tp_char = p_Char;
p_line = 0; /* force set_hunkmax to allocate again */
p_len = 0;
p_Char = 0;
set_hunkmax();
if (!p_line || !p_len || !p_Char) {
if (p_line)
free (p_line);
p_line = tp_line;
if (p_len)
free (p_len);
p_len = tp_len;
if (p_Char)
free (p_Char);
p_Char = tp_char;
return false; /* not enough memory to swap hunk! */
}
/* now turn the new into the old */
i = p_ptrn_lines + 1;
if (tp_char[i] == '\n') { /* account for possible blank line */
blankline = true;
i++;
}
if (p_efake >= 0) { /* fix non-freeable ptr range */
if (p_efake <= i)
n = p_end - i + 1;
else
n = -i;
p_efake += n;
p_bfake += n;
}
for (n=0; i <= p_end; i++,n++) {
p_line[n] = tp_line[i];
p_Char[n] = tp_char[i];
if (p_Char[n] == '+')
p_Char[n] = '-';
p_len[n] = tp_len[i];
}
if (blankline) {
i = p_ptrn_lines + 1;
p_line[n] = tp_line[i];
p_Char[n] = tp_char[i];
p_len[n] = tp_len[i];
n++;
}
assert(p_Char[0] == '=');
p_Char[0] = '*';
for (s=p_line[0]; *s; s++)
if (*s == '-')
*s = '*';
/* now turn the old into the new */
assert(tp_char[0] == '*');
tp_char[0] = '=';
for (s=tp_line[0]; *s; s++)
if (*s == '*')
*s = '-';
for (i=0; n <= p_end; i++,n++) {
p_line[n] = tp_line[i];
p_Char[n] = tp_char[i];
if (p_Char[n] == '-')
p_Char[n] = '+';
p_len[n] = tp_len[i];
}
assert(i == p_ptrn_lines + 1);
i = p_ptrn_lines;
p_ptrn_lines = p_repl_lines;
p_repl_lines = i;
p_Char[p_end + 1] = '^';
if (tp_line)
free (tp_line);
if (tp_len)
free (tp_len);
if (tp_char)
free (tp_char);
return true;
}
/* Return whether file WHICH (false = old, true = new) appears to nonexistent.
Return 1 for empty, 2 for nonexistent. */
int
pch_says_nonexistent (bool which)
{
return p_says_nonexistent[which];
}
const char *
pch_name (enum nametype type)
{
return type == NONE ? NULL : p_name[type];
}
/* Return the specified line position in the old file of the old context. */
lin
pch_first (void)
{
return p_first;
}
/* Return the number of lines of old context. */
lin
pch_ptrn_lines (void)
{
return p_ptrn_lines;
}
/* Return the probable line position in the new file of the first line. */
lin
pch_newfirst (void)
{
return p_newfirst;
}
/* Return the number of lines in the replacement text including context. */
lin
pch_repl_lines (void)
{
return p_repl_lines;
}
/* Return the number of lines in the whole hunk. */
lin
pch_end (void)
{
return p_end;
}
/* Return the number of context lines before the first changed line. */
lin
pch_prefix_context (void)
{
return p_prefix_context;
}
/* Return the number of context lines after the last changed line. */
lin
pch_suffix_context (void)
{
return p_suffix_context;
}
/* Return the length of a particular patch line. */
size_t
pch_line_len (lin line)
{
return p_len[line];
}
/* Return the control character (+, -, *, !, etc) for a patch line. A '\n'
indicates an empty line in a hunk. (The empty line is not part of the
old or new context. For some reson, the context format allows that.) */
char
pch_char (lin line)
{
return p_Char[line];
}
/* Return a pointer to a particular patch line. */
char *
pfetch (lin line)
{
return p_line[line];
}
/* Output a patch line. */
bool
pch_write_line (lin line, FILE *file)
{
bool after_newline = p_line[line][p_len[line] - 1] == '\n';
if (! fwrite (p_line[line], sizeof (*p_line[line]), p_len[line], file))
write_fatal ();
return after_newline;
}
/* Return where in the patch file this hunk began, for error messages. */
lin
pch_hunk_beg (void)
{
return p_hunk_beg;
}
char const *
pch_c_function (void)
{
return p_c_function;
}
char const *
pch_timestr (bool which)
{
return p_timestr[which];
}
/* Is the newline-terminated line a valid `ed' command for patch
input? If so, return the command character; if not, return 0.
This accepts just a subset of the valid commands, but it's
good enough in practice. */
static char
get_ed_command_letter (char const *line)
{
char const *p = line;
char letter;
bool pair = false;
if (ISDIGIT (*p))
{
while (ISDIGIT (*++p))
/* do nothing */ ;
if (*p == ',')
{
if (! ISDIGIT (*++p))
return 0;
while (ISDIGIT (*++p))
/* do nothing */ ;
pair = true;
}
}
letter = *p++;
switch (letter)
{
case 'a':
case 'i':
if (pair)
return 0;
break;
case 'c':
case 'd':
break;
case 's':
if (strncmp (p, "/.//", 4) != 0)
return 0;
p += 4;
break;
default:
return 0;
}
while (*p == ' ' || *p == '\t')
p++;
if (*p == '\n')
return letter;
return 0;
}
/* Apply an ed script by feeding ed itself. */
void
do_ed_script (FILE *ofp)
{
static char const editor_program[] = EDITOR_PROGRAM;
register file_offset beginning_of_this_line;
register FILE *pipefp = 0;
register size_t chars_read;
if (! dry_run && ! skip_rest_of_patch) {
int exclusive = TMPOUTNAME_needs_removal ? 0 : O_EXCL;
assert (! inerrno);
TMPOUTNAME_needs_removal = 1;
copy_file (inname, TMPOUTNAME, 0, exclusive, instat.st_mode, true);
sprintf (buf, "%s %s%s", editor_program, verbosity == VERBOSE ? "" : "- ",
TMPOUTNAME);
fflush (stdout);
pipefp = popen(buf, binary_transput ? "wb" : "w");
if (!pipefp)
pfatal ("Can't open pipe to %s", quotearg (buf));
}
for (;;) {
char ed_command_letter;
beginning_of_this_line = file_tell (pfp);
chars_read = get_line ();
if (! chars_read) {
next_intuit_at(beginning_of_this_line,p_input_line);
break;
}
ed_command_letter = get_ed_command_letter (buf);
if (ed_command_letter) {
if (pipefp)
if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
write_fatal ();
if (ed_command_letter != 'd' && ed_command_letter != 's') {
p_pass_comments_through = true;
while ((chars_read = get_line ()) != 0) {
if (pipefp)
if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
write_fatal ();
if (chars_read == 2 && strEQ (buf, ".\n"))
break;
}
p_pass_comments_through = false;
}
}
else {
next_intuit_at(beginning_of_this_line,p_input_line);
break;
}
}
if (!pipefp)
return;
if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
|| fflush (pipefp) != 0)
write_fatal ();
if (pclose (pipefp) != 0)
fatal ("%s FAILED", editor_program);
if (ofp)
{
FILE *ifp = fopen (TMPOUTNAME, binary_transput ? "rb" : "r");
int c;
if (!ifp)
pfatal ("can't open `%s'", TMPOUTNAME);
while ((c = getc (ifp)) != EOF)
if (putc (c, ofp) == EOF)
write_fatal ();
if (ferror (ifp) || fclose (ifp) != 0)
read_fatal ();
}
}
void
pch_normalize (enum diff format)
{
lin old = 1;
lin new = p_ptrn_lines + 1;
while (p_Char[new] == '=' || p_Char[new] == '\n')
new++;
if (format == UNI_DIFF)
{
/* Convert '!' markers into '-' and '+' as defined by the Unified
Format. */
for (; old <= p_ptrn_lines; old++)
if (p_Char[old] == '!')
p_Char[old] = '-';
for (; new <= p_end; new++)
if (p_Char[new] == '!')
p_Char[new] = '+';
}
else
{
/* Convert '-' and '+' markers which are part of a group into '!' as
defined by the Context Format. */
while (old <= p_ptrn_lines)
{
if (p_Char[old] == '-')
{
if (new <= p_end && p_Char[new] == '+')
{
do
{
p_Char[old] = '!';
old++;
}
while (old <= p_ptrn_lines && p_Char[old] == '-');
do
{
p_Char[new] = '!';
new++;
}
while (new <= p_end && p_Char[new] == '+');
}
else
{
do
old++;
while (old <= p_ptrn_lines && p_Char[old] == '-');
}
}
else if (new <= p_end && p_Char[new] == '+')
{
do
new++;
while (new <= p_end && p_Char[new] == '+');
}
else
{
old++;
new++;
}
}
}
}
|