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 $
4 * $NetBSD: pch.c,v 1.28 2015/07/30 21:47:51 christos Exp $
8 * patch - a program to apply diffs to original files
10 * Copyright 1986, Larry Wall
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following condition is met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this condition and the following disclaimer.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: pch.c,v 1.28 2015/07/30 21:47:51 christos Exp $");
36 #include <sys/types.h>
50 #include "pathnames.h"
52 /* Patch (diff listing) abstract type. */
54 static long p_filesize
; /* size of the patch file */
55 static LINENUM p_first
; /* 1st line number */
56 static LINENUM p_newfirst
; /* 1st line number of replacement */
57 static LINENUM p_ptrn_lines
; /* # lines in pattern */
58 static LINENUM p_repl_lines
; /* # lines in replacement text */
59 static LINENUM p_end
= -1; /* last line in hunk */
60 static LINENUM p_max
; /* max allowed value of p_end */
61 static LINENUM p_context
= 3; /* # of context lines */
62 static LINENUM p_input_line
= 0; /* current line # from patch file */
63 static char **p_line
= NULL
;/* the text of the hunk */
64 static short *p_len
= NULL
; /* length of each line */
65 static char *p_char
= NULL
; /* +, -, and ! */
66 static int hunkmax
= INITHUNKMAX
; /* size of above arrays to begin with */
67 static int p_indent
; /* indent to patch */
68 static LINENUM p_base
; /* where to intuit this time */
69 static LINENUM p_bline
; /* line # of p_base */
70 static LINENUM p_start
; /* where intuit found a patch */
71 static LINENUM p_sline
; /* and the line number for it */
72 static LINENUM p_hunk_beg
; /* line number of current hunk */
73 static LINENUM p_efake
= -1; /* end of faked up lines--don't free */
74 static LINENUM p_bfake
= -1; /* beg of faked up lines */
75 static FILE *pfp
= NULL
; /* patch file pointer */
76 static char *bestguess
= NULL
; /* guess at correct filename */
78 static void grow_hunkmax(void);
79 static int intuit_diff_type(void);
80 static void next_intuit_at(LINENUM
, LINENUM
);
81 static void skip_to(LINENUM
, LINENUM
);
82 static char *pgets(char *, int, FILE *);
83 static char *best_name(const struct file_name
*, bool);
84 static char *posix_name(const struct file_name
*, bool);
85 static size_t num_components(const char *);
88 * Prepare to look for the next patch in the patch file.
97 p_end
= (LINENUM
) - 1;
103 * Open the patch file at the beginning of time.
106 open_patch_file(const char *filename
)
108 struct stat filestat
;
110 if (filename
== NULL
|| *filename
== '\0' || strEQ(filename
, "-")) {
111 pfp
= fopen(TMPPATNAME
, "w");
113 pfatal("can't create %s", TMPPATNAME
);
114 while (fgets(buf
, buf_len
, stdin
) != NULL
)
116 if (ferror(pfp
) || fclose(pfp
))
117 pfatal("can't write %s", TMPPATNAME
);
118 filename
= TMPPATNAME
;
120 pfp
= fopen(filename
, "r");
122 pfatal("patch file %s not found", filename
);
123 fstat(fileno(pfp
), &filestat
);
124 p_filesize
= filestat
.st_size
;
125 next_intuit_at(0L, 1L); /* start at the beginning */
130 * Make sure our dynamically realloced tables are malloced to begin with.
136 p_line
= calloc((size_t) hunkmax
, sizeof(char *));
138 p_len
= calloc((size_t) hunkmax
, sizeof(short));
140 p_char
= calloc((size_t) hunkmax
, sizeof(char));
144 * Enlarge the arrays containing the current hunk of patch.
154 new_hunkmax
= hunkmax
* 2;
156 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
)
157 fatal("Internal memory allocation error\n");
159 new_p_line
= realloc(p_line
, new_hunkmax
* sizeof(char *));
160 if (new_p_line
== NULL
)
163 new_p_len
= realloc(p_len
, new_hunkmax
* sizeof(short));
164 if (new_p_len
== NULL
)
167 new_p_char
= realloc(p_char
, new_hunkmax
* sizeof(char));
168 if (new_p_char
== NULL
)
175 if (p_line
!= NULL
&& p_len
!= NULL
&& p_char
!= NULL
) {
176 hunkmax
= new_hunkmax
;
181 fatal("out of memory\n");
182 out_of_mem
= true; /* whatever is null will be allocated again */
183 /* from within plan_a(), of all places */
186 /* True if the remainder of the patch file contains a diff of some sort. */
189 there_is_another_patch(void)
193 if (p_base
!= 0L && p_base
>= p_filesize
) {
200 diff_type
= intuit_diff_type();
204 say(" Ignoring the trailing garbage.\ndone\n");
206 say(" I can't seem to find a patch in there anywhere.\n");
210 say(" %sooks like %s to me...\n",
211 (p_base
== 0L ? "L" : "The next patch l"),
212 diff_type
== UNI_DIFF
? "a unified diff" :
213 diff_type
== CONTEXT_DIFF
? "a context diff" :
214 diff_type
== NEW_CONTEXT_DIFF
? "a new-style context diff" :
215 diff_type
== NORMAL_DIFF
? "a normal diff" :
217 if (p_indent
&& verbose
)
218 say("(Patch is indented %d space%s.)\n", p_indent
,
219 p_indent
== 1 ? "" : "s");
220 skip_to(p_start
, p_sline
);
221 while (filearg
[0] == NULL
) {
222 if (force
|| batch
) {
223 say("No file to patch. Skipping...\n");
224 filearg
[0] = savestr(bestguess
);
225 skip_rest_of_patch
= true;
228 ask("File to patch: ");
231 bestguess
= savestr(buf
);
232 filearg
[0] = fetchname(buf
, &exists
, 0);
235 ask("No file found--skip this patch? [n] ");
239 say("Skipping patch...\n");
241 filearg
[0] = fetchname(bestguess
, &exists
, 0);
242 skip_rest_of_patch
= true;
249 /* Determine what kind of diff is in the remaining part of the patch file. */
252 intuit_diff_type(void)
254 long this_line
= 0, previous_line
;
255 long first_command_line
= -1;
256 LINENUM fcl_line
= -1;
257 bool last_line_was_command
= false, this_is_a_command
= false;
258 bool stars_last_line
= false, stars_this_line
= false;
261 struct file_name names
[MAX_FILE
];
263 memset(names
, 0, sizeof(names
));
264 ok_to_create_file
= false;
265 fseek(pfp
, p_base
, SEEK_SET
);
266 p_input_line
= p_bline
- 1;
268 previous_line
= this_line
;
269 last_line_was_command
= this_is_a_command
;
270 stars_last_line
= stars_this_line
;
271 this_line
= ftell(pfp
);
274 if (fgets(buf
, buf_len
, pfp
) == NULL
) {
275 if (first_command_line
>= 0L) {
276 /* nothing but deletes!? */
277 p_start
= first_command_line
;
283 p_sline
= p_input_line
;
288 for (s
= buf
; *s
== ' ' || *s
== '\t' || *s
== 'X'; s
++) {
290 indent
+= 8 - (indent
% 8);
294 for (t
= s
; isdigit((unsigned char)*t
) || *t
== ','; t
++)
296 this_is_a_command
= (isdigit((unsigned char)*s
) &&
297 (*t
== 'd' || *t
== 'c' || *t
== 'a'));
298 if (first_command_line
< 0L && this_is_a_command
) {
299 first_command_line
= this_line
;
300 fcl_line
= p_input_line
;
301 p_indent
= indent
; /* assume this for now */
303 if (!stars_last_line
&& strnEQ(s
, "*** ", 4))
304 names
[OLD_FILE
].path
= fetchname(s
+ 4,
305 &names
[OLD_FILE
].exists
, strippath
);
306 else if (strnEQ(s
, "--- ", 4))
307 names
[NEW_FILE
].path
= fetchname(s
+ 4,
308 &names
[NEW_FILE
].exists
, strippath
);
309 else if (strnEQ(s
, "+++ ", 4))
310 /* pretend it is the old name */
311 names
[OLD_FILE
].path
= fetchname(s
+ 4,
312 &names
[OLD_FILE
].exists
, strippath
);
313 else if (strnEQ(s
, "Index:", 6))
314 names
[INDEX_FILE
].path
= fetchname(s
+ 6,
315 &names
[INDEX_FILE
].exists
, strippath
);
316 else if (strnEQ(s
, "Prereq:", 7)) {
317 for (t
= s
+ 7; isspace((unsigned char)*t
); t
++)
319 revision
= savestr(t
);
320 for (t
= revision
; *t
&& !isspace((unsigned char)*t
); t
++)
323 if (*revision
== '\0') {
328 if ((!diff_type
|| diff_type
== ED_DIFF
) &&
329 first_command_line
>= 0L &&
332 p_start
= first_command_line
;
337 if ((!diff_type
|| diff_type
== UNI_DIFF
) && strnEQ(s
, "@@ -", 4)) {
338 if (strnEQ(s
+ 4, "0,0", 3))
339 ok_to_create_file
= true;
342 p_sline
= p_input_line
;
346 stars_this_line
= strnEQ(s
, "********", 8);
347 if ((!diff_type
|| diff_type
== CONTEXT_DIFF
) && stars_last_line
&&
348 strnEQ(s
, "*** ", 4)) {
349 if (atol(s
+ 4) == 0)
350 ok_to_create_file
= true;
352 * If this is a new context diff the character just
353 * before the newline is a '*'.
358 p_start
= previous_line
;
359 p_sline
= p_input_line
- 1;
360 retval
= (*(s
- 1) == '*' ? NEW_CONTEXT_DIFF
: CONTEXT_DIFF
);
363 if ((!diff_type
|| diff_type
== NORMAL_DIFF
) &&
364 last_line_was_command
&&
365 (strnEQ(s
, "< ", 2) || strnEQ(s
, "> ", 2))) {
366 p_start
= previous_line
;
367 p_sline
= p_input_line
- 1;
369 retval
= NORMAL_DIFF
;
374 if (retval
== UNI_DIFF
) {
375 /* unswap old and new */
376 struct file_name tmp
= names
[OLD_FILE
];
377 names
[OLD_FILE
] = names
[NEW_FILE
];
378 names
[NEW_FILE
] = tmp
;
380 if (filearg
[0] == NULL
) {
382 filearg
[0] = posix_name(names
, ok_to_create_file
);
384 /* Ignore the Index: name for context diffs, like GNU */
385 if (names
[OLD_FILE
].path
!= NULL
||
386 names
[NEW_FILE
].path
!= NULL
) {
387 free(names
[INDEX_FILE
].path
);
388 names
[INDEX_FILE
].path
= NULL
;
390 filearg
[0] = best_name(names
, ok_to_create_file
);
396 if (filearg
[0] != NULL
)
397 bestguess
= savestr(filearg
[0]);
398 else if (!ok_to_create_file
) {
400 * We don't want to create a new file but we need a
401 * filename to set bestguess. Avoid setting filearg[0]
402 * so the file is not created automatically.
405 bestguess
= posix_name(names
, true);
407 bestguess
= best_name(names
, true);
409 free(names
[OLD_FILE
].path
);
410 free(names
[NEW_FILE
].path
);
411 free(names
[INDEX_FILE
].path
);
416 * Remember where this patch ends so we know where to start up again.
419 next_intuit_at(LINENUM file_pos
, LINENUM file_line
)
426 * Basically a verbose fseek() to the actual diff listing.
429 skip_to(LINENUM file_pos
, LINENUM file_line
)
433 if (p_base
> file_pos
)
434 fatal("Internal error: seek %ld>%ld\n", p_base
, file_pos
);
435 if (verbose
&& p_base
< file_pos
) {
436 fseek(pfp
, p_base
, SEEK_SET
);
437 say("The text leading up to this was:\n--------------------------\n");
438 while (ftell(pfp
) < file_pos
) {
439 ret
= fgets(buf
, buf_len
, pfp
);
441 fatal("Unexpected end of file\n");
444 say("--------------------------\n");
446 fseek(pfp
, file_pos
, SEEK_SET
);
447 p_input_line
= file_line
- 1;
450 /* Make this a function for better debugging. */
454 fatal("malformed patch at line %ld: %s", p_input_line
, buf
);
455 /* about as informative as "Syntax error" in C */
459 getlinenum(const char *s
)
461 LINENUM l
= (LINENUM
)atol(s
);
470 getskiplinenum(char **p
)
473 LINENUM l
= getlinenum(s
);
474 while (isdigit((unsigned char)*s
))
481 * True if the line has been discarded (i.e., it is a line saying
482 * "\ No newline at end of file".)
485 remove_special_line(void)
493 } while (c
!= EOF
&& c
!= '\n');
498 fseek(pfp
, -1L, SEEK_CUR
);
504 * True if there is more of the current diff listing to process.
509 long line_beginning
; /* file pos of the current line */
510 LINENUM repl_beginning
; /* index of --- line */
511 LINENUM fillcnt
; /* #lines of missing ptrn or repl */
512 LINENUM fillsrc
; /* index of first line to copy */
513 LINENUM filldst
; /* index of first missing line */
514 bool ptrn_spaces_eaten
; /* ptrn was slightly misformed */
515 bool repl_could_be_missing
; /* no + or ! lines in this hunk */
516 bool repl_missing
; /* we are now backtracking */
517 long repl_backtrack_position
; /* file pos of first repl line */
518 LINENUM repl_patch_line
; /* input line number for same */
519 LINENUM ptrn_copiable
; /* # of copiable lines in ptrn */
524 if (p_end
== p_efake
)
525 p_end
= p_bfake
; /* don't free twice */
532 p_max
= hunkmax
; /* gets reduced when --- found */
533 if (diff_type
== CONTEXT_DIFF
|| diff_type
== NEW_CONTEXT_DIFF
) {
534 line_beginning
= ftell(pfp
);
539 ptrn_spaces_eaten
= false;
540 repl_could_be_missing
= true;
541 repl_missing
= false;
542 repl_backtrack_position
= 0;
546 ret
= pgets(buf
, buf_len
, pfp
);
548 if (ret
== NULL
|| strnNE(buf
, "********", 8)) {
549 next_intuit_at(line_beginning
, p_input_line
);
553 p_hunk_beg
= p_input_line
+ 1;
554 while (p_end
< p_max
) {
555 line_beginning
= ftell(pfp
);
556 ret
= pgets(buf
, buf_len
, pfp
);
559 if (p_max
- p_end
< 4) {
560 /* assume blank lines got chopped */
561 strlcpy(buf
, " \n", buf_len
);
563 if (repl_beginning
&& repl_could_be_missing
) {
567 fatal("unexpected end of file in patch\n");
571 if (p_end
>= hunkmax
)
572 fatal("Internal error: hunk larger than hunk "
574 p_char
[p_end
] = *buf
;
575 p_line
[p_end
] = NULL
;
578 if (strnEQ(buf
, "********", 8)) {
579 if (repl_beginning
&& repl_could_be_missing
) {
583 fatal("unexpected end of hunk "
588 if (repl_beginning
&& repl_could_be_missing
) {
592 fatal("unexpected *** at line %ld: %s",
596 p_line
[p_end
] = savestr(buf
);
601 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
605 if (strnEQ(s
, "0,0", 3))
606 memmove(s
, s
+ 2, strlen(s
+ 2) + 1);
607 p_first
= getskiplinenum(&s
);
609 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
613 p_ptrn_lines
= (getlinenum(s
)) - p_first
+ 1;
614 if (p_ptrn_lines
< 0)
622 if (p_first
>= LINENUM_MAX
- p_ptrn_lines
||
623 p_ptrn_lines
>= LINENUM_MAX
- 6)
626 /* we need this much at least */
627 p_max
= p_ptrn_lines
+ 6;
628 while (p_max
>= hunkmax
)
634 if (repl_beginning
||
635 (p_end
!= p_ptrn_lines
+ 1 +
636 (p_char
[p_end
- 1] == '\n'))) {
639 * `old' lines were omitted;
640 * set up to fill them in
641 * from 'new' context lines.
643 p_end
= p_ptrn_lines
+ 1;
646 fillcnt
= p_ptrn_lines
;
648 if (repl_beginning
) {
649 if (repl_could_be_missing
) {
653 fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
654 p_input_line
, p_hunk_beg
+ repl_beginning
);
656 fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
657 (p_end
<= p_ptrn_lines
660 p_input_line
, p_hunk_beg
);
664 repl_beginning
= p_end
;
665 repl_backtrack_position
= ftell(pfp
);
666 repl_patch_line
= p_input_line
;
667 p_line
[p_end
] = savestr(buf
);
673 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
677 p_newfirst
= getskiplinenum(&s
);
679 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
683 p_repl_lines
= (getlinenum(s
)) -
685 if (p_repl_lines
< 0)
687 } else if (p_newfirst
)
693 if (p_newfirst
>= LINENUM_MAX
- p_repl_lines
||
694 p_repl_lines
>= LINENUM_MAX
- p_end
)
696 p_max
= p_repl_lines
+ p_end
;
697 if (p_max
> MAXHUNKSIZE
)
698 fatal("hunk too large (%ld lines) at line %ld: %s",
699 p_max
, p_input_line
, buf
);
700 while (p_max
>= hunkmax
)
702 if (p_repl_lines
!= ptrn_copiable
&&
703 (p_context
!= 0 || p_repl_lines
!= 1))
704 repl_could_be_missing
= false;
710 repl_could_be_missing
= false;
712 if (buf
[1] == '\n' && canonicalize
)
713 strlcpy(buf
+ 1, " \n", buf_len
- 1);
714 if (!isspace((unsigned char)buf
[1]) && buf
[1] != '>' &&
716 repl_beginning
&& repl_could_be_missing
) {
721 if (context
< p_context
)
725 p_line
[p_end
] = savestr(buf
+ 2);
730 if (p_end
== p_ptrn_lines
) {
731 if (remove_special_line()) {
734 len
= strlen(p_line
[p_end
]) - 1;
735 (p_line
[p_end
])[len
] = 0;
740 case '\n': /* assume the 2 spaces got eaten */
741 if (repl_beginning
&& repl_could_be_missing
&&
742 (!ptrn_spaces_eaten
||
743 diff_type
== NEW_CONTEXT_DIFF
)) {
747 p_line
[p_end
] = savestr(buf
);
752 if (p_end
!= p_ptrn_lines
+ 1) {
753 ptrn_spaces_eaten
|= (repl_beginning
!= 0);
761 if (!isspace((unsigned char)buf
[1]) &&
762 repl_beginning
&& repl_could_be_missing
) {
769 p_line
[p_end
] = savestr(buf
+ 2);
776 if (repl_beginning
&& repl_could_be_missing
) {
782 /* set up p_len for strncmp() so we don't have to */
783 /* assume null termination */
785 p_len
[p_end
] = strlen(p_line
[p_end
]);
791 if (p_end
>= 0 && !repl_beginning
)
792 fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
796 /* reset state back to just after --- */
797 p_input_line
= repl_patch_line
;
798 for (p_end
--; p_end
> repl_beginning
; p_end
--)
800 fseek(pfp
, repl_backtrack_position
, SEEK_SET
);
802 /* redundant 'new' context lines were omitted - set */
803 /* up to fill them in from the old file context */
804 if (!p_context
&& p_repl_lines
== 1) {
809 filldst
= repl_beginning
+ 1;
810 fillcnt
= p_repl_lines
;
812 } else if (!p_context
&& fillcnt
== 1) {
813 /* the first hunk was a null hunk with no context */
814 /* and we were expecting one line -- fix it up. */
815 while (filldst
< p_end
) {
816 p_line
[filldst
] = p_line
[filldst
+ 1];
817 p_char
[filldst
] = p_char
[filldst
+ 1];
818 p_len
[filldst
] = p_len
[filldst
+ 1];
822 repl_beginning
--; /* this doesn't need to be fixed */
825 p_first
++; /* do append rather than insert */
829 if (diff_type
== CONTEXT_DIFF
&&
830 (fillcnt
|| (p_first
> 1 && ptrn_copiable
> 2 * p_context
))) {
833 "(Fascinating--this is really a new-style context diff but without",
834 "the telltale extra asterisks on the *** line that usually indicate",
835 "the new style...)");
836 diff_type
= NEW_CONTEXT_DIFF
;
838 /* if there were omitted context lines, fill them in now */
840 p_bfake
= filldst
; /* remember where not to free() */
841 p_efake
= filldst
+ fillcnt
- 1;
842 while (fillcnt
-- > 0) {
843 while (fillsrc
<= p_end
&& p_char
[fillsrc
] != ' ')
846 fatal("replacement text or line numbers mangled in hunk at line %ld\n",
848 p_line
[filldst
] = p_line
[fillsrc
];
849 p_char
[filldst
] = p_char
[fillsrc
];
850 p_len
[filldst
] = p_len
[fillsrc
];
854 while (fillsrc
<= p_end
&& fillsrc
!= repl_beginning
&&
855 p_char
[fillsrc
] != ' ')
859 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
860 fillsrc
, filldst
, repl_beginning
, p_end
+ 1);
862 if (fillsrc
!= p_end
+ 1 && fillsrc
!= repl_beginning
)
864 if (filldst
!= p_end
+ 1 && filldst
!= repl_beginning
)
867 if (p_line
[p_end
] != NULL
) {
868 if (remove_special_line()) {
870 (p_line
[p_end
])[p_len
[p_end
]] = 0;
873 } else if (diff_type
== UNI_DIFF
) {
874 LINENUM fillold
; /* index of old lines */
875 LINENUM fillnew
; /* index of new lines */
878 line_beginning
= ftell(pfp
); /* file pos of the current line */
879 ret
= pgets(buf
, buf_len
, pfp
);
881 if (ret
== NULL
|| strnNE(buf
, "@@ -", 4)) {
882 next_intuit_at(line_beginning
, p_input_line
);
888 p_first
= getskiplinenum(&s
);
891 p_ptrn_lines
= getskiplinenum(&s
);
894 if (p_first
>= LINENUM_MAX
- p_ptrn_lines
)
898 if (*s
!= '+' || !*++s
)
900 p_newfirst
= getskiplinenum(&s
);
903 p_repl_lines
= getskiplinenum(&s
);
910 if (p_first
>= LINENUM_MAX
- p_ptrn_lines
||
911 p_newfirst
> LINENUM_MAX
- p_repl_lines
||
912 p_ptrn_lines
>= LINENUM_MAX
- p_repl_lines
- 1)
915 p_first
++; /* do append rather than insert */
916 p_max
= p_ptrn_lines
+ p_repl_lines
+ 1;
917 while (p_max
>= hunkmax
)
920 fillnew
= fillold
+ p_ptrn_lines
;
921 p_end
= fillnew
+ p_repl_lines
;
922 snprintf(buf
, buf_len
, "*** %ld,%ld ****\n", p_first
,
923 p_first
+ p_ptrn_lines
- 1);
924 p_line
[0] = savestr(buf
);
930 snprintf(buf
, buf_len
, "--- %ld,%ld ----\n", p_newfirst
,
931 p_newfirst
+ p_repl_lines
- 1);
932 p_line
[fillnew
] = savestr(buf
);
937 p_char
[fillnew
++] = '=';
940 p_hunk_beg
= p_input_line
+ 1;
941 while (fillold
<= p_ptrn_lines
|| fillnew
<= p_end
) {
942 line_beginning
= ftell(pfp
);
943 ret
= pgets(buf
, buf_len
, pfp
);
946 if (p_max
- fillnew
< 3) {
947 /* assume blank lines got chopped */
948 strlcpy(buf
, " \n", buf_len
);
950 fatal("unexpected end of file in patch\n");
953 if (*buf
== '\t' || *buf
== '\n') {
954 ch
= ' '; /* assume the space got eaten */
958 s
= savestr(buf
+ 1);
961 while (--fillnew
> p_ptrn_lines
)
962 free(p_line
[fillnew
]);
968 if (fillold
> p_ptrn_lines
) {
973 p_char
[fillold
] = ch
;
975 p_len
[fillold
++] = strlen(s
);
976 if (fillold
> p_ptrn_lines
) {
977 if (remove_special_line()) {
978 p_len
[fillold
- 1] -= 1;
979 s
[p_len
[fillold
- 1]] = 0;
987 if (fillold
> p_ptrn_lines
) {
989 while (--fillnew
> p_ptrn_lines
)
990 free(p_line
[fillnew
]);
995 p_char
[fillold
] = ch
;
997 p_len
[fillold
++] = strlen(s
);
1000 while (--fillnew
> p_ptrn_lines
)
1001 free(p_line
[fillnew
]);
1002 p_end
= fillold
- 1;
1005 if (fillold
> p_ptrn_lines
) {
1006 if (remove_special_line()) {
1007 p_len
[fillold
- 1] -= 1;
1008 s
[p_len
[fillold
- 1]] = 0;
1013 if (fillnew
> p_end
) {
1015 while (--fillnew
> p_ptrn_lines
)
1016 free(p_line
[fillnew
]);
1017 p_end
= fillold
- 1;
1020 p_char
[fillnew
] = ch
;
1021 p_line
[fillnew
] = s
;
1022 p_len
[fillnew
++] = strlen(s
);
1023 if (fillold
> p_ptrn_lines
) {
1024 if (remove_special_line()) {
1025 p_len
[fillnew
- 1] -= 1;
1026 s
[p_len
[fillnew
- 1]] = 0;
1034 if (ch
!= ' ' && context
> 0) {
1035 if (context
< p_context
)
1036 p_context
= context
;
1040 } else { /* normal diff--fake it up */
1045 line_beginning
= ftell(pfp
);
1047 ret
= pgets(buf
, buf_len
, pfp
);
1049 if (ret
== NULL
|| !isdigit((unsigned char)*buf
)) {
1050 next_intuit_at(line_beginning
, p_input_line
);
1054 p_first
= getskiplinenum(&s
);
1057 p_ptrn_lines
= getskiplinenum(&s
) - p_first
+ 1;
1059 p_ptrn_lines
= (*s
!= 'a');
1060 if (p_first
>= LINENUM_MAX
- p_ptrn_lines
)
1063 if (hunk_type
== 'a')
1064 p_first
++; /* do append rather than insert */
1065 min
= getskiplinenum(&s
);
1067 max
= getlinenum(++s
);
1070 if (min
< 0 || min
> max
|| max
- min
== LINENUM_MAX
)
1072 if (hunk_type
== 'd')
1074 p_end
= p_ptrn_lines
+ 1 + max
- min
+ 1;
1076 p_repl_lines
= max
- min
+ 1;
1077 if (p_newfirst
> LINENUM_MAX
- p_repl_lines
||
1078 p_ptrn_lines
>= LINENUM_MAX
- p_repl_lines
- 1)
1080 p_end
= p_ptrn_lines
+ p_repl_lines
+ 1;
1081 if (p_end
> MAXHUNKSIZE
)
1082 fatal("hunk too large (%ld lines) at line %ld: %s",
1083 p_end
, p_input_line
, buf
);
1084 while (p_end
>= hunkmax
)
1086 snprintf(buf
, buf_len
, "*** %ld,%ld\n", p_first
,
1087 p_first
+ p_ptrn_lines
- 1);
1088 p_line
[0] = savestr(buf
);
1094 for (i
= 1; i
<= p_ptrn_lines
; i
++) {
1095 ret
= pgets(buf
, buf_len
, pfp
);
1098 fatal("unexpected end of file in patch at line %ld\n",
1101 fatal("< expected at line %ld of patch\n",
1103 p_line
[i
] = savestr(buf
+ 2);
1108 p_len
[i
] = strlen(p_line
[i
]);
1112 if (remove_special_line()) {
1114 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1116 if (hunk_type
== 'c') {
1117 ret
= pgets(buf
, buf_len
, pfp
);
1120 fatal("unexpected end of file in patch at line %ld\n",
1123 fatal("--- expected at line %ld of patch\n",
1126 snprintf(buf
, buf_len
, "--- %ld,%ld\n", min
, max
);
1127 p_line
[i
] = savestr(buf
);
1133 for (i
++; i
<= p_end
; i
++) {
1134 ret
= pgets(buf
, buf_len
, pfp
);
1137 fatal("unexpected end of file in patch at line %ld\n",
1140 fatal("> expected at line %ld of patch\n",
1142 p_line
[i
] = savestr(buf
+ 2);
1147 p_len
[i
] = strlen(p_line
[i
]);
1151 if (remove_special_line()) {
1153 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1156 if (reverse
) /* backwards patch? */
1158 say("Not enough memory to swap next hunk!\n");
1164 for (i
= 0; i
<= p_end
; i
++) {
1165 if (i
== p_ptrn_lines
)
1169 fprintf(stderr
, "%3d %c %c %s", i
, p_char
[i
],
1170 special
, p_line
[i
]);
1175 if (p_end
+ 1 < hunkmax
)/* paranoia reigns supreme... */
1176 p_char
[p_end
+ 1] = '^'; /* add a stopper for apply_hunk */
1181 * Input a line from the patch file, worrying about indentation.
1184 pgets(char *bf
, int sz
, FILE *fp
)
1186 char *s
, *ret
= fgets(bf
, sz
, fp
);
1189 if (p_indent
&& ret
!= NULL
) {
1191 indent
< p_indent
&& (*s
== ' ' || *s
== '\t' || *s
== 'X');
1194 indent
+= 8 - (indent
% 7);
1198 if (buf
!= s
&& strlcpy(buf
, s
, buf_len
) >= buf_len
)
1199 fatal("buffer too small in pgets()\n");
1205 * Reverse the old and new portions of the current hunk.
1210 char **tp_line
; /* the text of the hunk */
1211 short *tp_len
; /* length of each line */
1212 char *tp_char
; /* +, -, and ! */
1215 bool blankline
= false;
1219 p_first
= p_newfirst
;
1222 /* make a scratch copy */
1227 p_line
= NULL
; /* force set_hunkmax to allocate again */
1231 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
) {
1239 return false; /* not enough memory to swap hunk! */
1241 /* now turn the new into the old */
1243 i
= p_ptrn_lines
+ 1;
1244 if (tp_char
[i
] == '\n') { /* account for possible blank line */
1248 if (p_efake
>= 0) { /* fix non-freeable ptr range */
1256 for (n
= 0; i
<= p_end
; i
++, n
++) {
1257 p_line
[n
] = tp_line
[i
];
1258 p_char
[n
] = tp_char
[i
];
1259 if (p_char
[n
] == '+')
1261 p_len
[n
] = tp_len
[i
];
1264 i
= p_ptrn_lines
+ 1;
1265 p_line
[n
] = tp_line
[i
];
1266 p_char
[n
] = tp_char
[i
];
1267 p_len
[n
] = tp_len
[i
];
1270 if (p_char
[0] != '=')
1271 fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1272 p_input_line
, p_char
[0]);
1274 for (s
= p_line
[0]; *s
; s
++)
1278 /* now turn the old into the new */
1280 if (p_char
[0] != '*')
1281 fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
1282 p_input_line
, p_char
[0]);
1284 for (s
= tp_line
[0]; *s
; s
++)
1287 for (i
= 0; n
<= p_end
; i
++, n
++) {
1288 p_line
[n
] = tp_line
[i
];
1289 p_char
[n
] = tp_char
[i
];
1290 if (p_char
[n
] == '-')
1292 p_len
[n
] = tp_len
[i
];
1295 if (i
!= p_ptrn_lines
+ 1)
1296 fatal("Malformed patch at line %ld: expected %ld lines, "
1298 p_input_line
, p_ptrn_lines
+ 1, i
);
1301 p_ptrn_lines
= p_repl_lines
;
1312 * Return the specified line position in the old file of the old context.
1321 * Return the number of lines of old context.
1324 pch_ptrn_lines(void)
1326 return p_ptrn_lines
;
1330 * Return the probable line position in the new file of the first line.
1339 * Return the number of lines in the replacement text including context.
1342 pch_repl_lines(void)
1344 return p_repl_lines
;
1348 * Return the number of lines in the whole hunk.
1357 * Return the number of context lines before the first changed line.
1366 * Return the length of a particular patch line.
1369 pch_line_len(LINENUM line
)
1375 * Return the control character (+, -, *, !, etc) for a patch line.
1378 pch_char(LINENUM line
)
1380 return p_char
[line
];
1384 * Return a pointer to a particular patch line.
1387 pfetch(LINENUM line
)
1389 return p_line
[line
];
1393 * Return where in the patch file this hunk began, for error messages.
1402 * Apply an ed script by feeding ed itself.
1408 long beginning_of_this_line
;
1409 FILE *pipefp
= NULL
;
1412 if (!skip_rest_of_patch
) {
1413 if (copy_file(filearg
[0], TMPOUTNAME
) < 0) {
1415 fatal("can't create temp file %s", TMPOUTNAME
);
1417 snprintf(buf
, buf_len
, "%s%s%s", _PATH_ED
,
1418 verbose
? " " : " -s ", TMPOUTNAME
);
1419 pipefp
= popen(buf
, "w");
1422 beginning_of_this_line
= ftell(pfp
);
1423 if (pgets(buf
, buf_len
, pfp
) == NULL
) {
1424 next_intuit_at(beginning_of_this_line
, p_input_line
);
1428 for (t
= buf
; isdigit((unsigned char)*t
) || *t
== ','; t
++)
1430 /* POSIX defines allowed commands as {a,c,d,i,s} */
1431 if (isdigit((unsigned char)*buf
) && (*t
== 'a' || *t
== 'c' ||
1432 *t
== 'd' || *t
== 'i' || *t
== 's')) {
1438 t
= strchr(buf
, '\0') - 1;
1439 while (--t
>= buf
&& *t
== '\\')
1440 continuation
= !continuation
;
1441 if (!continuation
||
1442 pgets(buf
, sizeof buf
, pfp
) == NULL
)
1447 } else if (*t
!= 'd') {
1448 while (pgets(buf
, buf_len
, pfp
) != NULL
) {
1452 if (strEQ(buf
, ".\n"))
1457 next_intuit_at(beginning_of_this_line
, p_input_line
);
1463 fprintf(pipefp
, "w\n");
1464 fprintf(pipefp
, "q\n");
1469 if (move_file(TMPOUTNAME
, outname
) < 0) {
1471 chmod(TMPOUTNAME
, filemode
);
1473 chmod(outname
, filemode
);
1479 * Choose the name of the file to be patched based on POSIX rules.
1480 * NOTE: the POSIX rules are amazingly stupid and we only follow them
1481 * if the user specified --posix or set POSIXLY_CORRECT.
1484 posix_name(const struct file_name
*names
, bool assume_exists
)
1490 * POSIX states that the filename will be chosen from one
1491 * of the old, new and index names (in that order) if
1492 * the file exists relative to CWD after -p stripping.
1494 for (i
= 0; i
< MAX_FILE
; i
++) {
1495 if (names
[i
].path
!= NULL
&& names
[i
].exists
) {
1496 path
= names
[i
].path
;
1500 if (path
== NULL
&& !assume_exists
) {
1502 * No files found, look for something we can checkout from
1503 * RCS/SCCS dirs. Same order as above.
1505 for (i
= 0; i
< MAX_FILE
; i
++) {
1506 if (names
[i
].path
!= NULL
&&
1507 (path
= checked_in(names
[i
].path
)) != NULL
)
1511 * Still no match? Check to see if the diff could be creating
1514 if (path
== NULL
&& ok_to_create_file
&&
1515 names
[NEW_FILE
].path
!= NULL
)
1516 path
= names
[NEW_FILE
].path
;
1519 return path
? savestr(path
) : NULL
;
1523 * Choose the name of the file to be patched based the "best" one
1527 best_name(const struct file_name
*names
, bool assume_exists
)
1529 size_t min_components
, min_baselen
, min_len
, tmp
;
1534 * The "best" name is the one with the fewest number of path
1535 * components, the shortest basename length, and the shortest
1536 * overall length (in that order). We only use the Index: file
1537 * if neither of the old or new files could be intuited from
1540 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1541 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1542 if (names
[i
].path
== NULL
||
1543 (!names
[i
].exists
&& !assume_exists
))
1545 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1547 min_components
= tmp
;
1548 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1551 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1554 best
= names
[i
].path
;
1558 * No files found, look for something we can checkout from
1559 * RCS/SCCS dirs. Logic is identical to that above...
1561 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1562 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1563 if (names
[i
].path
== NULL
||
1564 checked_in(names
[i
].path
) == NULL
)
1566 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1568 min_components
= tmp
;
1569 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1572 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1575 best
= names
[i
].path
;
1578 * Still no match? Check to see if the diff could be creating
1581 if (best
== NULL
&& ok_to_create_file
&&
1582 names
[NEW_FILE
].path
!= NULL
)
1583 best
= names
[NEW_FILE
].path
;
1586 return best
? savestr(best
) : NULL
;
1590 num_components(const char *path
)
1595 for (n
= 0, cp
= path
; (cp
= strchr(cp
, '/')) != NULL
; n
++, cp
++) {
1597 cp
++; /* skip consecutive slashes */