2 * $OpenBSD: pch.c,v 1.37 2007/09/02 15:19:33 deraadt Exp $
3 * $DragonFly: src/usr.bin/patch/pch.c,v 1.6 2008/08/10 23:35:40 joerg Exp $
7 * patch - a program to apply diffs to original files
9 * Copyright 1986, Larry Wall
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following condition is met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this condition and the following disclaimer.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
32 #include <sys/types.h>
46 #include "pathnames.h"
48 /* Patch (diff listing) abstract type. */
50 static long p_filesize
; /* size of the patch file */
51 static LINENUM p_first
; /* 1st line number */
52 static LINENUM p_newfirst
; /* 1st line number of replacement */
53 static LINENUM p_ptrn_lines
; /* # lines in pattern */
54 static LINENUM p_repl_lines
; /* # lines in replacement text */
55 static LINENUM p_end
= -1; /* last line in hunk */
56 static LINENUM p_max
; /* max allowed value of p_end */
57 static LINENUM p_context
= 3; /* # of context lines */
58 static LINENUM p_input_line
= 0; /* current line # from patch file */
59 static char **p_line
= NULL
;/* the text of the hunk */
60 static short *p_len
= NULL
; /* length of each line */
61 static char *p_char
= NULL
; /* +, -, and ! */
62 static int hunkmax
= INITHUNKMAX
; /* size of above arrays to begin with */
63 static int p_indent
; /* indent to patch */
64 static LINENUM p_base
; /* where to intuit this time */
65 static LINENUM p_bline
; /* line # of p_base */
66 static LINENUM p_start
; /* where intuit found a patch */
67 static LINENUM p_sline
; /* and the line number for it */
68 static LINENUM p_hunk_beg
; /* line number of current hunk */
69 static LINENUM p_efake
= -1; /* end of faked up lines--don't free */
70 static LINENUM p_bfake
= -1; /* beg of faked up lines */
71 static FILE *pfp
= NULL
; /* patch file pointer */
72 static char *bestguess
= NULL
; /* guess at correct filename */
74 static void grow_hunkmax(void);
75 static int intuit_diff_type(void);
76 static void next_intuit_at(LINENUM
, LINENUM
);
77 static void skip_to(LINENUM
, LINENUM
);
78 static char *pgets(char *, int, FILE *);
79 static char *best_name(const struct file_name
*, bool);
80 static char *posix_name(const struct file_name
*, bool);
81 static size_t num_components(const char *);
84 * Prepare to look for the next patch in the patch file.
93 p_end
= (LINENUM
) - 1;
99 * Open the patch file at the beginning of time.
102 open_patch_file(const char *filename
)
104 struct stat filestat
;
106 if (filename
== NULL
|| *filename
== '\0' || strEQ(filename
, "-")) {
107 pfp
= fopen(TMPPATNAME
, "w");
109 pfatal("can't create %s", TMPPATNAME
);
110 while (fgets(buf
, buf_len
, stdin
) != NULL
)
112 if (ferror(pfp
) || fclose(pfp
))
113 pfatal("can't write %s", TMPPATNAME
);
114 filename
= TMPPATNAME
;
116 pfp
= fopen(filename
, "r");
118 pfatal("patch file %s not found", filename
);
119 fstat(fileno(pfp
), &filestat
);
120 p_filesize
= filestat
.st_size
;
121 next_intuit_at(0L, 1L); /* start at the beginning */
126 * Make sure our dynamically realloced tables are malloced to begin with.
132 p_line
= calloc((size_t) hunkmax
, sizeof(char *));
134 p_len
= calloc((size_t) hunkmax
, sizeof(short));
136 p_char
= calloc((size_t) hunkmax
, sizeof(char));
140 * Enlarge the arrays containing the current hunk of patch.
150 new_hunkmax
= hunkmax
* 2;
152 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
)
153 fatal("Internal memory allocation error\n");
155 new_p_line
= realloc(p_line
, new_hunkmax
* sizeof(char *));
156 if (new_p_line
== NULL
)
159 new_p_len
= realloc(p_len
, new_hunkmax
* sizeof(short));
160 if (new_p_len
== NULL
)
163 new_p_char
= realloc(p_char
, new_hunkmax
* sizeof(char));
164 if (new_p_char
== NULL
)
171 if (p_line
!= NULL
&& p_len
!= NULL
&& p_char
!= NULL
) {
172 hunkmax
= new_hunkmax
;
177 fatal("out of memory\n");
178 out_of_mem
= true; /* whatever is null will be allocated again */
179 /* from within plan_a(), of all places */
182 /* True if the remainder of the patch file contains a diff of some sort. */
185 there_is_another_patch(void)
189 if (p_base
!= 0L && p_base
>= p_filesize
) {
196 diff_type
= intuit_diff_type();
200 say(" Ignoring the trailing garbage.\ndone\n");
202 say(" I can't seem to find a patch in there anywhere.\n");
206 say(" %sooks like %s to me...\n",
207 (p_base
== 0L ? "L" : "The next patch l"),
208 diff_type
== UNI_DIFF
? "a unified diff" :
209 diff_type
== CONTEXT_DIFF
? "a context diff" :
210 diff_type
== NEW_CONTEXT_DIFF
? "a new-style context diff" :
211 diff_type
== NORMAL_DIFF
? "a normal diff" :
213 if (p_indent
&& verbose
)
214 say("(Patch is indented %d space%s.)\n", p_indent
,
215 p_indent
== 1 ? "" : "s");
216 skip_to(p_start
, p_sline
);
217 while (filearg
[0] == NULL
) {
218 if (force
|| batch
) {
219 say("No file to patch. Skipping...\n");
220 filearg
[0] = savestr(bestguess
);
221 skip_rest_of_patch
= true;
224 ask("File to patch: ");
227 bestguess
= savestr(buf
);
228 filearg
[0] = fetchname(buf
, &exists
, 0);
231 ask("No file found--skip this patch? [n] ");
235 say("Skipping patch...\n");
237 filearg
[0] = fetchname(bestguess
, &exists
, 0);
238 skip_rest_of_patch
= true;
245 /* Determine what kind of diff is in the remaining part of the patch file. */
248 intuit_diff_type(void)
250 long this_line
= 0, previous_line
;
251 long first_command_line
= -1;
252 LINENUM fcl_line
= -1;
253 bool last_line_was_command
= false, this_is_a_command
= false;
254 bool stars_last_line
= false, stars_this_line
= false;
257 struct file_name names
[MAX_FILE
];
259 memset(names
, 0, sizeof(names
));
260 ok_to_create_file
= false;
261 fseek(pfp
, p_base
, SEEK_SET
);
262 p_input_line
= p_bline
- 1;
264 previous_line
= this_line
;
265 last_line_was_command
= this_is_a_command
;
266 stars_last_line
= stars_this_line
;
267 this_line
= ftell(pfp
);
270 if (fgets(buf
, buf_len
, pfp
) == NULL
) {
271 if (first_command_line
>= 0L) {
272 /* nothing but deletes!? */
273 p_start
= first_command_line
;
279 p_sline
= p_input_line
;
284 for (s
= buf
; *s
== ' ' || *s
== '\t' || *s
== 'X'; s
++) {
286 indent
+= 8 - (indent
% 8);
290 for (t
= s
; isdigit((unsigned char)*t
) || *t
== ','; t
++)
292 this_is_a_command
= (isdigit((unsigned char)*s
) &&
293 (*t
== 'd' || *t
== 'c' || *t
== 'a'));
294 if (first_command_line
< 0L && this_is_a_command
) {
295 first_command_line
= this_line
;
296 fcl_line
= p_input_line
;
297 p_indent
= indent
; /* assume this for now */
299 if (!stars_last_line
&& strnEQ(s
, "*** ", 4))
300 names
[OLD_FILE
].path
= fetchname(s
+ 4,
301 &names
[OLD_FILE
].exists
, strippath
);
302 else if (strnEQ(s
, "--- ", 4))
303 names
[NEW_FILE
].path
= fetchname(s
+ 4,
304 &names
[NEW_FILE
].exists
, strippath
);
305 else if (strnEQ(s
, "+++ ", 4))
306 /* pretend it is the old name */
307 names
[OLD_FILE
].path
= fetchname(s
+ 4,
308 &names
[OLD_FILE
].exists
, strippath
);
309 else if (strnEQ(s
, "Index:", 6))
310 names
[INDEX_FILE
].path
= fetchname(s
+ 6,
311 &names
[INDEX_FILE
].exists
, strippath
);
312 else if (strnEQ(s
, "Prereq:", 7)) {
313 for (t
= s
+ 7; isspace((unsigned char)*t
); t
++)
315 revision
= savestr(t
);
316 for (t
= revision
; *t
&& !isspace((unsigned char)*t
); t
++)
319 if (*revision
== '\0') {
324 if ((!diff_type
|| diff_type
== ED_DIFF
) &&
325 first_command_line
>= 0L &&
328 p_start
= first_command_line
;
333 if ((!diff_type
|| diff_type
== UNI_DIFF
) && strnEQ(s
, "@@ -", 4)) {
334 if (strnEQ(s
+ 4, "0,0", 3))
335 ok_to_create_file
= true;
338 p_sline
= p_input_line
;
342 stars_this_line
= strnEQ(s
, "********", 8);
343 if ((!diff_type
|| diff_type
== CONTEXT_DIFF
) && stars_last_line
&&
344 strnEQ(s
, "*** ", 4)) {
345 if (atol(s
+ 4) == 0)
346 ok_to_create_file
= true;
348 * If this is a new context diff the character just
349 * before the newline is a '*'.
354 p_start
= previous_line
;
355 p_sline
= p_input_line
- 1;
356 retval
= (*(s
- 1) == '*' ? NEW_CONTEXT_DIFF
: CONTEXT_DIFF
);
359 if ((!diff_type
|| diff_type
== NORMAL_DIFF
) &&
360 last_line_was_command
&&
361 (strnEQ(s
, "< ", 2) || strnEQ(s
, "> ", 2))) {
362 p_start
= previous_line
;
363 p_sline
= p_input_line
- 1;
365 retval
= NORMAL_DIFF
;
370 if (retval
== UNI_DIFF
) {
371 /* unswap old and new */
372 struct file_name tmp
= names
[OLD_FILE
];
373 names
[OLD_FILE
] = names
[NEW_FILE
];
374 names
[NEW_FILE
] = tmp
;
376 if (filearg
[0] == NULL
) {
378 filearg
[0] = posix_name(names
, ok_to_create_file
);
380 /* Ignore the Index: name for context diffs, like GNU */
381 if (names
[OLD_FILE
].path
!= NULL
||
382 names
[NEW_FILE
].path
!= NULL
) {
383 free(names
[INDEX_FILE
].path
);
384 names
[INDEX_FILE
].path
= NULL
;
386 filearg
[0] = best_name(names
, ok_to_create_file
);
392 if (filearg
[0] != NULL
)
393 bestguess
= savestr(filearg
[0]);
394 else if (!ok_to_create_file
) {
396 * We don't want to create a new file but we need a
397 * filename to set bestguess. Avoid setting filearg[0]
398 * so the file is not created automatically.
401 bestguess
= posix_name(names
, true);
403 bestguess
= best_name(names
, true);
405 free(names
[OLD_FILE
].path
);
406 free(names
[NEW_FILE
].path
);
407 free(names
[INDEX_FILE
].path
);
412 * Remember where this patch ends so we know where to start up again.
415 next_intuit_at(LINENUM file_pos
, LINENUM file_line
)
422 * Basically a verbose fseek() to the actual diff listing.
425 skip_to(LINENUM file_pos
, LINENUM file_line
)
429 if (p_base
> file_pos
)
430 fatal("Internal error: seek %ld>%ld\n", p_base
, file_pos
);
431 if (verbose
&& p_base
< file_pos
) {
432 fseek(pfp
, p_base
, SEEK_SET
);
433 say("The text leading up to this was:\n--------------------------\n");
434 while (ftell(pfp
) < file_pos
) {
435 ret
= fgets(buf
, buf_len
, pfp
);
437 fatal("Unexpected end of file\n");
440 say("--------------------------\n");
442 fseek(pfp
, file_pos
, SEEK_SET
);
443 p_input_line
= file_line
- 1;
446 /* Make this a function for better debugging. */
450 fatal("malformed patch at line %ld: %s", p_input_line
, buf
);
451 /* about as informative as "Syntax error" in C */
455 * True if the line has been discarded (i.e. it is a line saying
456 * "\ No newline at end of file".)
459 remove_special_line(void)
467 } while (c
!= EOF
&& c
!= '\n');
472 fseek(pfp
, -1L, SEEK_CUR
);
478 * True if there is more of the current diff listing to process.
483 long line_beginning
; /* file pos of the current line */
484 LINENUM repl_beginning
; /* index of --- line */
485 LINENUM fillcnt
; /* #lines of missing ptrn or repl */
486 LINENUM fillsrc
; /* index of first line to copy */
487 LINENUM filldst
; /* index of first missing line */
488 bool ptrn_spaces_eaten
; /* ptrn was slightly misformed */
489 bool repl_could_be_missing
; /* no + or ! lines in this hunk */
490 bool repl_missing
; /* we are now backtracking */
491 long repl_backtrack_position
; /* file pos of first repl line */
492 LINENUM repl_patch_line
; /* input line number for same */
493 LINENUM ptrn_copiable
; /* # of copiable lines in ptrn */
498 if (p_end
== p_efake
)
499 p_end
= p_bfake
; /* don't free twice */
506 p_max
= hunkmax
; /* gets reduced when --- found */
507 if (diff_type
== CONTEXT_DIFF
|| diff_type
== NEW_CONTEXT_DIFF
) {
508 line_beginning
= ftell(pfp
);
513 ptrn_spaces_eaten
= false;
514 repl_could_be_missing
= true;
515 repl_missing
= false;
516 repl_backtrack_position
= 0;
520 ret
= pgets(buf
, buf_len
, pfp
);
522 if (ret
== NULL
|| strnNE(buf
, "********", 8)) {
523 next_intuit_at(line_beginning
, p_input_line
);
527 p_hunk_beg
= p_input_line
+ 1;
528 while (p_end
< p_max
) {
529 line_beginning
= ftell(pfp
);
530 ret
= pgets(buf
, buf_len
, pfp
);
533 if (p_max
- p_end
< 4) {
534 /* assume blank lines got chopped */
535 strlcpy(buf
, " \n", buf_len
);
537 if (repl_beginning
&& repl_could_be_missing
) {
541 fatal("unexpected end of file in patch\n");
545 if (p_end
>= hunkmax
)
546 fatal("Internal error: hunk larger than hunk "
548 p_char
[p_end
] = *buf
;
549 p_line
[p_end
] = NULL
;
552 if (strnEQ(buf
, "********", 8)) {
553 if (repl_beginning
&& repl_could_be_missing
) {
557 fatal("unexpected end of hunk "
562 if (repl_beginning
&& repl_could_be_missing
) {
566 fatal("unexpected *** at line %ld: %s",
570 p_line
[p_end
] = savestr(buf
);
575 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
579 if (strnEQ(s
, "0,0", 3))
580 memmove(s
, s
+ 2, strlen(s
+ 2) + 1);
581 p_first
= (LINENUM
) atol(s
);
582 while (isdigit((unsigned char)*s
))
585 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
589 p_ptrn_lines
= ((LINENUM
) atol(s
)) - p_first
+ 1;
597 /* we need this much at least */
598 p_max
= p_ptrn_lines
+ 6;
599 while (p_max
>= hunkmax
)
605 if (repl_beginning
||
606 (p_end
!= p_ptrn_lines
+ 1 +
607 (p_char
[p_end
- 1] == '\n'))) {
610 * `old' lines were omitted;
611 * set up to fill them in
612 * from 'new' context lines.
614 p_end
= p_ptrn_lines
+ 1;
617 fillcnt
= p_ptrn_lines
;
619 if (repl_beginning
) {
620 if (repl_could_be_missing
) {
624 fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
625 p_input_line
, p_hunk_beg
+ repl_beginning
);
627 fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
628 (p_end
<= p_ptrn_lines
631 p_input_line
, p_hunk_beg
);
635 repl_beginning
= p_end
;
636 repl_backtrack_position
= ftell(pfp
);
637 repl_patch_line
= p_input_line
;
638 p_line
[p_end
] = savestr(buf
);
644 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
648 p_newfirst
= (LINENUM
) atol(s
);
649 while (isdigit((unsigned char)*s
))
652 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
656 p_repl_lines
= ((LINENUM
) atol(s
)) -
658 } else if (p_newfirst
)
664 p_max
= p_repl_lines
+ p_end
;
665 if (p_max
> MAXHUNKSIZE
)
666 fatal("hunk too large (%ld lines) at line %ld: %s",
667 p_max
, p_input_line
, buf
);
668 while (p_max
>= hunkmax
)
670 if (p_repl_lines
!= ptrn_copiable
&&
671 (p_context
!= 0 || p_repl_lines
!= 1))
672 repl_could_be_missing
= false;
678 repl_could_be_missing
= false;
680 if (buf
[1] == '\n' && canonicalize
)
681 strlcpy(buf
+ 1, " \n", buf_len
- 1);
682 if (!isspace((unsigned char)buf
[1]) && buf
[1] != '>' &&
684 repl_beginning
&& repl_could_be_missing
) {
689 if (context
< p_context
)
693 p_line
[p_end
] = savestr(buf
+ 2);
698 if (p_end
== p_ptrn_lines
) {
699 if (remove_special_line()) {
702 len
= strlen(p_line
[p_end
]) - 1;
703 (p_line
[p_end
])[len
] = 0;
708 case '\n': /* assume the 2 spaces got eaten */
709 if (repl_beginning
&& repl_could_be_missing
&&
710 (!ptrn_spaces_eaten
||
711 diff_type
== NEW_CONTEXT_DIFF
)) {
715 p_line
[p_end
] = savestr(buf
);
720 if (p_end
!= p_ptrn_lines
+ 1) {
721 ptrn_spaces_eaten
|= (repl_beginning
!= 0);
729 if (!isspace((unsigned char)buf
[1]) &&
730 repl_beginning
&& repl_could_be_missing
) {
737 p_line
[p_end
] = savestr(buf
+ 2);
744 if (repl_beginning
&& repl_could_be_missing
) {
750 /* set up p_len for strncmp() so we don't have to */
751 /* assume null termination */
753 p_len
[p_end
] = strlen(p_line
[p_end
]);
759 if (p_end
>= 0 && !repl_beginning
)
760 fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
764 /* reset state back to just after --- */
765 p_input_line
= repl_patch_line
;
766 for (p_end
--; p_end
> repl_beginning
; p_end
--)
768 fseek(pfp
, repl_backtrack_position
, SEEK_SET
);
770 /* redundant 'new' context lines were omitted - set */
771 /* up to fill them in from the old file context */
772 if (!p_context
&& p_repl_lines
== 1) {
777 filldst
= repl_beginning
+ 1;
778 fillcnt
= p_repl_lines
;
780 } else if (!p_context
&& fillcnt
== 1) {
781 /* the first hunk was a null hunk with no context */
782 /* and we were expecting one line -- fix it up. */
783 while (filldst
< p_end
) {
784 p_line
[filldst
] = p_line
[filldst
+ 1];
785 p_char
[filldst
] = p_char
[filldst
+ 1];
786 p_len
[filldst
] = p_len
[filldst
+ 1];
790 repl_beginning
--; /* this doesn't need to be fixed */
793 p_first
++; /* do append rather than insert */
797 if (diff_type
== CONTEXT_DIFF
&&
798 (fillcnt
|| (p_first
> 1 && ptrn_copiable
> 2 * p_context
))) {
801 "(Fascinating--this is really a new-style context diff but without",
802 "the telltale extra asterisks on the *** line that usually indicate",
803 "the new style...)");
804 diff_type
= NEW_CONTEXT_DIFF
;
806 /* if there were omitted context lines, fill them in now */
808 p_bfake
= filldst
; /* remember where not to free() */
809 p_efake
= filldst
+ fillcnt
- 1;
810 while (fillcnt
-- > 0) {
811 while (fillsrc
<= p_end
&& p_char
[fillsrc
] != ' ')
814 fatal("replacement text or line numbers mangled in hunk at line %ld\n",
816 p_line
[filldst
] = p_line
[fillsrc
];
817 p_char
[filldst
] = p_char
[fillsrc
];
818 p_len
[filldst
] = p_len
[fillsrc
];
822 while (fillsrc
<= p_end
&& fillsrc
!= repl_beginning
&&
823 p_char
[fillsrc
] != ' ')
827 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
828 fillsrc
, filldst
, repl_beginning
, p_end
+ 1);
830 if (fillsrc
!= p_end
+ 1 && fillsrc
!= repl_beginning
)
832 if (filldst
!= p_end
+ 1 && filldst
!= repl_beginning
)
835 if (p_line
[p_end
] != NULL
) {
836 if (remove_special_line()) {
838 (p_line
[p_end
])[p_len
[p_end
]] = 0;
841 } else if (diff_type
== UNI_DIFF
) {
842 LINENUM fillold
; /* index of old lines */
843 LINENUM fillnew
; /* index of new lines */
846 line_beginning
= ftell(pfp
); /* file pos of the current line */
847 ret
= pgets(buf
, buf_len
, pfp
);
849 if (ret
== NULL
|| strnNE(buf
, "@@ -", 4)) {
850 next_intuit_at(line_beginning
, p_input_line
);
856 p_first
= (LINENUM
) atol(s
);
857 while (isdigit((unsigned char)*s
))
860 p_ptrn_lines
= (LINENUM
) atol(++s
);
861 while (isdigit((unsigned char)*s
))
867 if (*s
!= '+' || !*++s
)
869 p_newfirst
= (LINENUM
) atol(s
);
870 while (isdigit((unsigned char)*s
))
873 p_repl_lines
= (LINENUM
) atol(++s
);
874 while (isdigit((unsigned char)*s
))
883 p_first
++; /* do append rather than insert */
884 p_max
= p_ptrn_lines
+ p_repl_lines
+ 1;
885 while (p_max
>= hunkmax
)
888 fillnew
= fillold
+ p_ptrn_lines
;
889 p_end
= fillnew
+ p_repl_lines
;
890 snprintf(buf
, buf_len
, "*** %ld,%ld ****\n", p_first
,
891 p_first
+ p_ptrn_lines
- 1);
892 p_line
[0] = savestr(buf
);
898 snprintf(buf
, buf_len
, "--- %ld,%ld ----\n", p_newfirst
,
899 p_newfirst
+ p_repl_lines
- 1);
900 p_line
[fillnew
] = savestr(buf
);
905 p_char
[fillnew
++] = '=';
908 p_hunk_beg
= p_input_line
+ 1;
909 while (fillold
<= p_ptrn_lines
|| fillnew
<= p_end
) {
910 line_beginning
= ftell(pfp
);
911 ret
= pgets(buf
, buf_len
, pfp
);
914 if (p_max
- fillnew
< 3) {
915 /* assume blank lines got chopped */
916 strlcpy(buf
, " \n", buf_len
);
918 fatal("unexpected end of file in patch\n");
921 if (*buf
== '\t' || *buf
== '\n') {
922 ch
= ' '; /* assume the space got eaten */
926 s
= savestr(buf
+ 1);
929 while (--fillnew
> p_ptrn_lines
)
930 free(p_line
[fillnew
]);
936 if (fillold
> p_ptrn_lines
) {
941 p_char
[fillold
] = ch
;
943 p_len
[fillold
++] = strlen(s
);
944 if (fillold
> p_ptrn_lines
) {
945 if (remove_special_line()) {
946 p_len
[fillold
- 1] -= 1;
947 s
[p_len
[fillold
- 1]] = 0;
955 if (fillold
> p_ptrn_lines
) {
957 while (--fillnew
> p_ptrn_lines
)
958 free(p_line
[fillnew
]);
963 p_char
[fillold
] = ch
;
965 p_len
[fillold
++] = strlen(s
);
968 while (--fillnew
> p_ptrn_lines
)
969 free(p_line
[fillnew
]);
973 if (fillold
> p_ptrn_lines
) {
974 if (remove_special_line()) {
975 p_len
[fillold
- 1] -= 1;
976 s
[p_len
[fillold
- 1]] = 0;
981 if (fillnew
> p_end
) {
983 while (--fillnew
> p_ptrn_lines
)
984 free(p_line
[fillnew
]);
988 p_char
[fillnew
] = ch
;
990 p_len
[fillnew
++] = strlen(s
);
991 if (fillold
> p_ptrn_lines
) {
992 if (remove_special_line()) {
993 p_len
[fillnew
- 1] -= 1;
994 s
[p_len
[fillnew
- 1]] = 0;
1002 if (ch
!= ' ' && context
> 0) {
1003 if (context
< p_context
)
1004 p_context
= context
;
1008 } else { /* normal diff--fake it up */
1013 line_beginning
= ftell(pfp
);
1015 ret
= pgets(buf
, buf_len
, pfp
);
1017 if (ret
== NULL
|| !isdigit((unsigned char)*buf
)) {
1018 next_intuit_at(line_beginning
, p_input_line
);
1021 p_first
= (LINENUM
) atol(buf
);
1022 for (s
= buf
; isdigit((unsigned char)*s
); s
++)
1025 p_ptrn_lines
= (LINENUM
) atol(++s
) - p_first
+ 1;
1026 while (isdigit((unsigned char)*s
))
1029 p_ptrn_lines
= (*s
!= 'a');
1031 if (hunk_type
== 'a')
1032 p_first
++; /* do append rather than insert */
1033 min
= (LINENUM
) atol(++s
);
1034 for (; isdigit((unsigned char)*s
); s
++)
1037 max
= (LINENUM
) atol(++s
);
1040 if (hunk_type
== 'd')
1042 p_end
= p_ptrn_lines
+ 1 + max
- min
+ 1;
1043 if (p_end
> MAXHUNKSIZE
)
1044 fatal("hunk too large (%ld lines) at line %ld: %s",
1045 p_end
, p_input_line
, buf
);
1046 while (p_end
>= hunkmax
)
1049 p_repl_lines
= max
- min
+ 1;
1050 snprintf(buf
, buf_len
, "*** %ld,%ld\n", p_first
,
1051 p_first
+ p_ptrn_lines
- 1);
1052 p_line
[0] = savestr(buf
);
1058 for (i
= 1; i
<= p_ptrn_lines
; i
++) {
1059 ret
= pgets(buf
, buf_len
, pfp
);
1062 fatal("unexpected end of file in patch at line %ld\n",
1065 fatal("< expected at line %ld of patch\n",
1067 p_line
[i
] = savestr(buf
+ 2);
1072 p_len
[i
] = strlen(p_line
[i
]);
1076 if (remove_special_line()) {
1078 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1080 if (hunk_type
== 'c') {
1081 ret
= pgets(buf
, buf_len
, pfp
);
1084 fatal("unexpected end of file in patch at line %ld\n",
1087 fatal("--- expected at line %ld of patch\n",
1090 snprintf(buf
, buf_len
, "--- %ld,%ld\n", min
, max
);
1091 p_line
[i
] = savestr(buf
);
1097 for (i
++; i
<= p_end
; i
++) {
1098 ret
= pgets(buf
, buf_len
, pfp
);
1101 fatal("unexpected end of file in patch at line %ld\n",
1104 fatal("> expected at line %ld of patch\n",
1106 p_line
[i
] = savestr(buf
+ 2);
1111 p_len
[i
] = strlen(p_line
[i
]);
1115 if (remove_special_line()) {
1117 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1120 if (reverse
) /* backwards patch? */
1122 say("Not enough memory to swap next hunk!\n");
1128 for (i
= 0; i
<= p_end
; i
++) {
1129 if (i
== p_ptrn_lines
)
1133 fprintf(stderr
, "%3d %c %c %s", i
, p_char
[i
],
1134 special
, p_line
[i
]);
1139 if (p_end
+ 1 < hunkmax
)/* paranoia reigns supreme... */
1140 p_char
[p_end
+ 1] = '^'; /* add a stopper for apply_hunk */
1145 * Input a line from the patch file, worrying about indentation.
1148 pgets(char *bf
, int sz
, FILE *fp
)
1150 char *s
, *ret
= fgets(bf
, sz
, fp
);
1153 if (p_indent
&& ret
!= NULL
) {
1155 indent
< p_indent
&& (*s
== ' ' || *s
== '\t' || *s
== 'X');
1158 indent
+= 8 - (indent
% 7);
1162 if (buf
!= s
&& strlcpy(buf
, s
, buf_len
) >= buf_len
)
1163 fatal("buffer too small in pgets()\n");
1169 * Reverse the old and new portions of the current hunk.
1174 char **tp_line
; /* the text of the hunk */
1175 short *tp_len
; /* length of each line */
1176 char *tp_char
; /* +, -, and ! */
1179 bool blankline
= false;
1183 p_first
= p_newfirst
;
1186 /* make a scratch copy */
1191 p_line
= NULL
; /* force set_hunkmax to allocate again */
1195 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
) {
1203 return false; /* not enough memory to swap hunk! */
1205 /* now turn the new into the old */
1207 i
= p_ptrn_lines
+ 1;
1208 if (tp_char
[i
] == '\n') { /* account for possible blank line */
1212 if (p_efake
>= 0) { /* fix non-freeable ptr range */
1220 for (n
= 0; i
<= p_end
; i
++, n
++) {
1221 p_line
[n
] = tp_line
[i
];
1222 p_char
[n
] = tp_char
[i
];
1223 if (p_char
[n
] == '+')
1225 p_len
[n
] = tp_len
[i
];
1228 i
= p_ptrn_lines
+ 1;
1229 p_line
[n
] = tp_line
[i
];
1230 p_char
[n
] = tp_char
[i
];
1231 p_len
[n
] = tp_len
[i
];
1234 if (p_char
[0] != '=')
1235 fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1236 p_input_line
, p_char
[0]);
1238 for (s
= p_line
[0]; *s
; s
++)
1242 /* now turn the old into the new */
1244 if (p_char
[0] != '*')
1245 fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
1246 p_input_line
, p_char
[0]);
1248 for (s
= tp_line
[0]; *s
; s
++)
1251 for (i
= 0; n
<= p_end
; i
++, n
++) {
1252 p_line
[n
] = tp_line
[i
];
1253 p_char
[n
] = tp_char
[i
];
1254 if (p_char
[n
] == '-')
1256 p_len
[n
] = tp_len
[i
];
1259 if (i
!= p_ptrn_lines
+ 1)
1260 fatal("Malformed patch at line %ld: expected %ld lines, "
1262 p_input_line
, p_ptrn_lines
+ 1, i
);
1265 p_ptrn_lines
= p_repl_lines
;
1276 * Return the specified line position in the old file of the old context.
1285 * Return the number of lines of old context.
1288 pch_ptrn_lines(void)
1290 return p_ptrn_lines
;
1294 * Return the probable line position in the new file of the first line.
1303 * Return the number of lines in the replacement text including context.
1306 pch_repl_lines(void)
1308 return p_repl_lines
;
1312 * Return the number of lines in the whole hunk.
1321 * Return the number of context lines before the first changed line.
1330 * Return the length of a particular patch line.
1333 pch_line_len(LINENUM line
)
1339 * Return the control character (+, -, *, !, etc) for a patch line.
1342 pch_char(LINENUM line
)
1344 return p_char
[line
];
1348 * Return a pointer to a particular patch line.
1351 pfetch(LINENUM line
)
1353 return p_line
[line
];
1357 * Return where in the patch file this hunk began, for error messages.
1366 * Apply an ed script by feeding ed itself.
1372 long beginning_of_this_line
;
1373 FILE *pipefp
= NULL
;
1375 if (!skip_rest_of_patch
) {
1376 if (copy_file(filearg
[0], TMPOUTNAME
) < 0) {
1378 fatal("can't create temp file %s", TMPOUTNAME
);
1380 snprintf(buf
, buf_len
, "%s%s%s", _PATH_ED
,
1381 verbose
? " " : " -s ", TMPOUTNAME
);
1382 pipefp
= popen(buf
, "w");
1385 beginning_of_this_line
= ftell(pfp
);
1386 if (pgets(buf
, buf_len
, pfp
) == NULL
) {
1387 next_intuit_at(beginning_of_this_line
, p_input_line
);
1391 for (t
= buf
; isdigit((unsigned char)*t
) || *t
== ','; t
++)
1393 /* POSIX defines allowed commands as {a,c,d,i,s} */
1394 if (isdigit((unsigned char)*buf
) && (*t
== 'a' || *t
== 'c' ||
1395 *t
== 'd' || *t
== 'i' || *t
== 's')) {
1399 while (pgets(buf
, buf_len
, pfp
) != NULL
) {
1403 if (strEQ(buf
, ".\n"))
1408 next_intuit_at(beginning_of_this_line
, p_input_line
);
1414 fprintf(pipefp
, "w\n");
1415 fprintf(pipefp
, "q\n");
1420 if (move_file(TMPOUTNAME
, outname
) < 0) {
1422 chmod(TMPOUTNAME
, filemode
);
1424 chmod(outname
, filemode
);
1430 * Choose the name of the file to be patched based on POSIX rules.
1431 * NOTE: the POSIX rules are amazingly stupid and we only follow them
1432 * if the user specified --posix or set POSIXLY_CORRECT.
1435 posix_name(const struct file_name
*names
, bool assume_exists
)
1441 * POSIX states that the filename will be chosen from one
1442 * of the old, new and index names (in that order) if
1443 * the file exists relative to CWD after -p stripping.
1445 for (i
= 0; i
< MAX_FILE
; i
++) {
1446 if (names
[i
].path
!= NULL
&& names
[i
].exists
) {
1447 path
= names
[i
].path
;
1451 if (path
== NULL
&& !assume_exists
) {
1453 * No files found, look for something we can checkout from
1454 * RCS/SCCS dirs. Same order as above.
1456 for (i
= 0; i
< MAX_FILE
; i
++) {
1457 if (names
[i
].path
!= NULL
&&
1458 (path
= checked_in(names
[i
].path
)) != NULL
)
1462 * Still no match? Check to see if the diff could be creating
1465 if (path
== NULL
&& ok_to_create_file
&&
1466 names
[NEW_FILE
].path
!= NULL
)
1467 path
= names
[NEW_FILE
].path
;
1470 return path
? savestr(path
) : NULL
;
1474 * Choose the name of the file to be patched based the "best" one
1478 best_name(const struct file_name
*names
, bool assume_exists
)
1480 size_t min_components
, min_baselen
, min_len
, tmp
;
1485 * The "best" name is the one with the fewest number of path
1486 * components, the shortest basename length, and the shortest
1487 * overall length (in that order). We only use the Index: file
1488 * if neither of the old or new files could be intuited from
1491 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1492 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1493 if (names
[i
].path
== NULL
||
1494 (!names
[i
].exists
&& !assume_exists
))
1496 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1498 min_components
= tmp
;
1499 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1502 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1505 best
= names
[i
].path
;
1509 * No files found, look for something we can checkout from
1510 * RCS/SCCS dirs. Logic is identical to that above...
1512 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1513 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1514 if (names
[i
].path
== NULL
||
1515 checked_in(names
[i
].path
) == NULL
)
1517 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1519 min_components
= tmp
;
1520 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1523 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1526 best
= names
[i
].path
;
1529 * Still no match? Check to see if the diff could be creating
1532 if (best
== NULL
&& ok_to_create_file
&&
1533 names
[NEW_FILE
].path
!= NULL
)
1534 best
= names
[NEW_FILE
].path
;
1537 return best
? savestr(best
) : NULL
;
1541 num_components(const char *path
)
1546 for (n
= 0, cp
= path
; (cp
= strchr(cp
, '/')) != NULL
; n
++, cp
++) {
1548 cp
++; /* skip consecutive slashes */