/
pch.c
1910 lines (1763 loc) · 49.3 KB
/
pch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* reading patches */
/* $Id: pch.c,v 1.29 1999/08/30 06:20:08 eggert Exp $ */
/* Copyright 1986, 1987, 1988 Larry Wall
Copyright 1990, 1991-1993, 1997-1998, 1999 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 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#define XTERN extern
#include <common.h>
#include <backupfile.h>
#include <basename.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 time_t p_timestamp[2]; /* timestamps in patch headers */
static off_t p_filesize; /* size of the patch file */
static LINENUM p_first; /* 1st line number */
static LINENUM p_newfirst; /* 1st line number of replacement */
static LINENUM p_ptrn_lines; /* # lines in pattern */
static LINENUM p_repl_lines; /* # lines in replacement text */
static LINENUM p_end = -1; /* last line in hunk */
static LINENUM p_max; /* max allowed value of p_end */
static LINENUM p_prefix_context; /* # of prefix context lines */
static LINENUM p_suffix_context; /* # of suffix context lines */
static LINENUM 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 LINENUM hunkmax = INITHUNKMAX; /* size of above arrays */
static int p_indent; /* indent to patch */
static int p_strip_trailing_cr; /* nonzero if stripping trailing \r */
static file_offset p_base; /* where to intuit this time */
static LINENUM p_bline; /* line # of p_base */
static file_offset p_start; /* where intuit found a patch */
static LINENUM p_sline; /* and the line number for it */
static LINENUM p_hunk_beg; /* line number of current hunk */
static LINENUM p_efake = -1; /* end of faked up lines--don't free */
static LINENUM p_bfake = -1; /* beg of faked up lines */
enum nametype { OLD, NEW, INDEX, NONE };
static char *scan_linenum PARAMS ((char *, LINENUM *));
static enum diff intuit_diff_type PARAMS ((void));
static enum nametype best_name PARAMS ((char * const *, int const *));
static int prefix_components PARAMS ((char *, int));
static size_t pget_line PARAMS ((int, int, int));
static size_t get_line PARAMS ((void));
static bool incomplete_line PARAMS ((void));
static bool grow_hunkmax PARAMS ((void));
static void malformed PARAMS ((void)) __attribute__ ((noreturn));
static void next_intuit_at PARAMS ((file_offset, LINENUM));
static void skip_to PARAMS ((file_offset, LINENUM));
/* 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 = 0;
}
/* 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
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),
"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, (LINENUM) 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)
memory_fatal ();
/* 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;
}
/* True if the remainder of the patch file contains a diff of some sort. */
bool
there_is_another_patch (void)
{
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();
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" :
"an ed script" );
if (verbosity != SILENT)
{
if (p_indent)
say ("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
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));
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) {
if (force || batch) {
say ("No file to patch. Skipping patch.\n");
skip_rest_of_patch = TRUE;
return TRUE;
}
ask ("File to patch: ");
inname = fetchname (buf, 0, (time_t *) 0);
if (inname)
{
if (stat (inname, &instat) == 0)
{
inerrno = 0;
invc = -1;
}
else
{
perror (inname);
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 (void)
{
register file_offset this_line = 0;
register file_offset first_command_line = -1;
LINENUM fcl_line = 0; /* Pacify `gcc -W'. */
register bool this_is_a_command = FALSE;
register bool stars_this_line = FALSE;
enum nametype i;
char *name[3];
struct stat st[3];
int stat_errno[3];
int version_controlled[3];
register enum diff retval;
name[OLD] = name[NEW] = name[INDEX] = 0;
version_controlled[OLD] = -1;
version_controlled[NEW] = -1;
version_controlled[INDEX] = -1;
p_rfc934_nesting = 0;
p_timestamp[OLD] = p_timestamp[NEW] = (time_t) -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 int indent = 0;
int strip_trailing_cr;
size_t chars_read;
this_line = file_tell (pfp);
chars_read = pget_line (0, 0, 0);
if (chars_read == (size_t) -1)
memory_fatal ();
if (! chars_read) {
if (first_command_line >= 0) {
/* 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++;
}
for (t = s; ISDIGIT (*t) || *t == ','; t++)
continue;
this_is_a_command = (ISDIGIT (*s) &&
(*t == 'd' || *t == 'c' || *t == 'a') );
if (first_command_line < 0 && this_is_a_command) {
first_command_line = this_line;
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))
name[OLD] = fetchname (s+4, strippath, &p_timestamp[OLD]);
else if (strnEQ(s, "+++ ", 4))
/* Swap with NEW below. */
name[OLD] = fetchname (s+4, strippath, &p_timestamp[OLD]);
else if (strnEQ(s, "Index:", 6))
name[INDEX] = fetchname (s+6, strippath, (time_t *) 0);
else if (strnEQ(s, "Prereq:", 7)) {
for (t = s + 7; ISSPACE ((unsigned char) *t); t++)
continue;
revision = t;
for (t = revision; *t && !ISSPACE ((unsigned char) *t); t++)
continue;
if (t == revision)
revision = 0;
else {
char oldc = *t;
*t = '\0';
revision = savestr (revision);
*t = oldc;
}
} else
{
for (t = s; t[0] == '-' && t[1] == ' '; t += 2)
continue;
if (strnEQ(t, "--- ", 4))
{
time_t timestamp = (time_t) -1;
name[NEW] = fetchname (t+4, strippath, ×tamp);
if (timestamp != (time_t) -1)
{
p_timestamp[NEW] = timestamp;
p_rfc934_nesting = (t - s) >> 1;
}
}
}
if ((diff_type == NO_DIFF || diff_type == ED_DIFF) &&
first_command_line >= 0 &&
strEQ(s, ".\n") ) {
p_indent = indent;
p_strip_trailing_cr = strip_trailing_cr;
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)) {
/* `name' and `p_timestamp' are backwards; swap them. */
time_t ti = p_timestamp[OLD];
p_timestamp[OLD] = p_timestamp[NEW];
p_timestamp[NEW] = ti;
t = name[OLD];
name[OLD] = name[NEW];
name[NEW] = t;
s += 4;
if (s[0] == '0' && !ISDIGIT (s[1]))
p_says_nonexistent[OLD] = 1 + ! p_timestamp[OLD];
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];
p_indent = indent;
p_strip_trailing_cr = strip_trailing_cr;
p_start = this_line;
p_sline = p_input_line;
retval = UNI_DIFF;
if (! ((name[OLD] || ! p_timestamp[OLD])
&& (name[NEW] || ! p_timestamp[NEW])))
{
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];
/* 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;
LINENUM saved_p_bline = p_bline;
Fseek (pfp, previous_line, SEEK_SET);
p_input_line -= 2;
if (another_hunk (retval, 0)
&& ! p_repl_lines && p_newfirst == 1)
p_says_nonexistent[NEW] = 1 + ! p_timestamp[NEW];
next_intuit_at (saved_p_base, saved_p_bline);
}
if (! ((name[OLD] || ! p_timestamp[OLD])
&& (name[NEW] || ! p_timestamp[NEW])))
{
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;
p_strip_trailing_cr = strip_trailing_cr;
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.2b/D11 section 5.22.7.2
(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 && (name[OLD] || name[NEW]) && name[INDEX])
{
free (name[INDEX]);
name[INDEX] = 0;
}
for (i = OLD; i <= INDEX; i++)
if (name[i])
{
if (i0 != NONE && strcmp (name[i0], 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 (name[i], &st[i]) != 0)
stat_errno[i] = errno;
else
{
stat_errno[i] = 0;
if (posixly_correct)
break;
}
i0 = i;
}
if (! posixly_correct)
{
int is_empty;
i = best_name (name, stat_errno);
if (i == NONE && patch_get)
{
enum nametype nope = NONE;
for (i = OLD; i <= INDEX; i++)
if (name[i])
{
char const *cs;
char *getbuf;
char *diffbuf;
int readonly = outfile && strcmp (outfile, name[i]) != 0;
if (nope == NONE || strcmp (name[nope], name[i]) != 0)
{
cs = (version_controller
(name[i], readonly, (struct stat *) 0,
&getbuf, &diffbuf));
version_controlled[i] = !! cs;
if (cs)
{
if (version_get (name[i], cs, 0, 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;
}
}
is_empty = i == NONE || st[i].st_size == 0;
if ((! is_empty) < p_says_nonexistent[reverse ^ is_empty])
{
assert (i0 != NONE);
if (ok_to_reverse
("The next patch%s would %s the file %s,\nwhich %s!",
reverse ? ", when reversed," : "",
(i == NONE ? "delete"
: st[i].st_size == 0 ? "empty out"
: "create"),
quotearg (name[i == NONE || st[i].st_size == 0 ? i0 : i]),
(i == NONE ? "does not exist"
: st[i].st_size == 0 ? "is already empty"
: "already exists")))
reverse ^= 1;
}
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 (name[i])
{
newdirs[i] = (prefix_components (name[i], 0)
- prefix_components (name[i], 1));
if (newdirs[i] < newdirs_min)
newdirs_min = newdirs[i];
}
for (i = OLD; i <= INDEX; i++)
if (name[i])
distance_from_minimum[i] = newdirs[i] - newdirs_min;
i = best_name (name, distance_from_minimum);
}
}
}
if (i == NONE)
inerrno = -1;
else
{
inname = name[i];
name[i] = 0;
inerrno = stat_errno[i];
invc = version_controlled[i];
instat = st[i];
}
for (i = OLD; i <= INDEX; i++)
if (name[i])
free (name[i]);
return retval;
}
/* Count the path name components in FILENAME's prefix.
If CHECKDIRS is nonzero, count only existing directories. */
static int
prefix_components (char *filename, int checkdirs)
{
int count = 0;
struct stat stat_buf;
int stat_result;
char *f = filename + FILESYSTEM_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], 0);
if (components_min < components[i])
continue;
components_min = components[i];
/* Of those, take the names with the shortest basename. */
basename_len[i] = strlen (base_name (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, LINENUM 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, LINENUM 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, LINENUM *linenum)
{
char *s;
LINENUM n = 0;
int overflow = 0;
char numbuf[LINENUM_LENGTH_BOUND + 1];
for (s = s0; ISDIGIT (*s); s++)
{
LINENUM 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, int rev)
{
register char *s;
register LINENUM 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;
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 */
LINENUM repl_beginning = 0; /* index of --- line */
register LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */
register LINENUM fillsrc; /* index of first line to copy */
register LINENUM 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 repl_missing = FALSE; /* we are now backtracking */
file_offset repl_backtrack_position = 0;
/* file pos of first repl line */
LINENUM repl_patch_line; /* input line number for same */
LINENUM repl_context; /* context for same */
LINENUM ptrn_prefix_context = -1; /* lines in pattern prefix context */
LINENUM ptrn_suffix_context = -1; /* lines in pattern suffix context */
LINENUM repl_prefix_context = -1; /* lines in replac. prefix context */
register LINENUM ptrn_copiable = 0;
/* # of copiable lines in ptrn */
/* 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;
}
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++)
continue;
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 >= 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. */
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++)
continue;
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;
}