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 $
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>
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 * True if the line has been discarded (i.e. it is a line saying
460 * "\ No newline at end of file".)
463 remove_special_line(void)
471 } while (c
!= EOF
&& c
!= '\n');
476 fseek(pfp
, -1L, SEEK_CUR
);
482 * True if there is more of the current diff listing to process.
487 long line_beginning
; /* file pos of the current line */
488 LINENUM repl_beginning
; /* index of --- line */
489 LINENUM fillcnt
; /* #lines of missing ptrn or repl */
490 LINENUM fillsrc
; /* index of first line to copy */
491 LINENUM filldst
; /* index of first missing line */
492 bool ptrn_spaces_eaten
; /* ptrn was slightly misformed */
493 bool repl_could_be_missing
; /* no + or ! lines in this hunk */
494 bool repl_missing
; /* we are now backtracking */
495 long repl_backtrack_position
; /* file pos of first repl line */
496 LINENUM repl_patch_line
; /* input line number for same */
497 LINENUM ptrn_copiable
; /* # of copiable lines in ptrn */
502 if (p_end
== p_efake
)
503 p_end
= p_bfake
; /* don't free twice */
510 p_max
= hunkmax
; /* gets reduced when --- found */
511 if (diff_type
== CONTEXT_DIFF
|| diff_type
== NEW_CONTEXT_DIFF
) {
512 line_beginning
= ftell(pfp
);
517 ptrn_spaces_eaten
= false;
518 repl_could_be_missing
= true;
519 repl_missing
= false;
520 repl_backtrack_position
= 0;
524 ret
= pgets(buf
, buf_len
, pfp
);
526 if (ret
== NULL
|| strnNE(buf
, "********", 8)) {
527 next_intuit_at(line_beginning
, p_input_line
);
531 p_hunk_beg
= p_input_line
+ 1;
532 while (p_end
< p_max
) {
533 line_beginning
= ftell(pfp
);
534 ret
= pgets(buf
, buf_len
, pfp
);
537 if (p_max
- p_end
< 4) {
538 /* assume blank lines got chopped */
539 strlcpy(buf
, " \n", buf_len
);
541 if (repl_beginning
&& repl_could_be_missing
) {
545 fatal("unexpected end of file in patch\n");
549 if (p_end
>= hunkmax
)
550 fatal("Internal error: hunk larger than hunk "
552 p_char
[p_end
] = *buf
;
553 p_line
[p_end
] = NULL
;
556 if (strnEQ(buf
, "********", 8)) {
557 if (repl_beginning
&& repl_could_be_missing
) {
561 fatal("unexpected end of hunk "
566 if (repl_beginning
&& repl_could_be_missing
) {
570 fatal("unexpected *** at line %ld: %s",
574 p_line
[p_end
] = savestr(buf
);
579 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
583 if (strnEQ(s
, "0,0", 3))
584 memmove(s
, s
+ 2, strlen(s
+ 2) + 1);
585 p_first
= (LINENUM
) atol(s
);
586 while (isdigit((unsigned char)*s
))
589 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
593 p_ptrn_lines
= ((LINENUM
) atol(s
)) - p_first
+ 1;
601 /* we need this much at least */
602 p_max
= p_ptrn_lines
+ 6;
603 while (p_max
>= hunkmax
)
609 if (repl_beginning
||
610 (p_end
!= p_ptrn_lines
+ 1 +
611 (p_char
[p_end
- 1] == '\n'))) {
614 * `old' lines were omitted;
615 * set up to fill them in
616 * from 'new' context lines.
618 p_end
= p_ptrn_lines
+ 1;
621 fillcnt
= p_ptrn_lines
;
623 if (repl_beginning
) {
624 if (repl_could_be_missing
) {
628 fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
629 p_input_line
, p_hunk_beg
+ repl_beginning
);
631 fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
632 (p_end
<= p_ptrn_lines
635 p_input_line
, p_hunk_beg
);
639 repl_beginning
= p_end
;
640 repl_backtrack_position
= ftell(pfp
);
641 repl_patch_line
= p_input_line
;
642 p_line
[p_end
] = savestr(buf
);
648 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
652 p_newfirst
= (LINENUM
) atol(s
);
653 while (isdigit((unsigned char)*s
))
656 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
660 p_repl_lines
= ((LINENUM
) atol(s
)) -
662 } else if (p_newfirst
)
668 p_max
= p_repl_lines
+ p_end
;
669 if (p_max
> MAXHUNKSIZE
)
670 fatal("hunk too large (%ld lines) at line %ld: %s",
671 p_max
, p_input_line
, buf
);
672 while (p_max
>= hunkmax
)
674 if (p_repl_lines
!= ptrn_copiable
&&
675 (p_context
!= 0 || p_repl_lines
!= 1))
676 repl_could_be_missing
= false;
682 repl_could_be_missing
= false;
684 if (buf
[1] == '\n' && canonicalize
)
685 strlcpy(buf
+ 1, " \n", buf_len
- 1);
686 if (!isspace((unsigned char)buf
[1]) && buf
[1] != '>' &&
688 repl_beginning
&& repl_could_be_missing
) {
693 if (context
< p_context
)
697 p_line
[p_end
] = savestr(buf
+ 2);
702 if (p_end
== p_ptrn_lines
) {
703 if (remove_special_line()) {
706 len
= strlen(p_line
[p_end
]) - 1;
707 (p_line
[p_end
])[len
] = 0;
712 case '\n': /* assume the 2 spaces got eaten */
713 if (repl_beginning
&& repl_could_be_missing
&&
714 (!ptrn_spaces_eaten
||
715 diff_type
== NEW_CONTEXT_DIFF
)) {
719 p_line
[p_end
] = savestr(buf
);
724 if (p_end
!= p_ptrn_lines
+ 1) {
725 ptrn_spaces_eaten
|= (repl_beginning
!= 0);
733 if (!isspace((unsigned char)buf
[1]) &&
734 repl_beginning
&& repl_could_be_missing
) {
741 p_line
[p_end
] = savestr(buf
+ 2);
748 if (repl_beginning
&& repl_could_be_missing
) {
754 /* set up p_len for strncmp() so we don't have to */
755 /* assume null termination */
757 p_len
[p_end
] = strlen(p_line
[p_end
]);
763 if (p_end
>= 0 && !repl_beginning
)
764 fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
768 /* reset state back to just after --- */
769 p_input_line
= repl_patch_line
;
770 for (p_end
--; p_end
> repl_beginning
; p_end
--)
772 fseek(pfp
, repl_backtrack_position
, SEEK_SET
);
774 /* redundant 'new' context lines were omitted - set */
775 /* up to fill them in from the old file context */
776 if (!p_context
&& p_repl_lines
== 1) {
781 filldst
= repl_beginning
+ 1;
782 fillcnt
= p_repl_lines
;
784 } else if (!p_context
&& fillcnt
== 1) {
785 /* the first hunk was a null hunk with no context */
786 /* and we were expecting one line -- fix it up. */
787 while (filldst
< p_end
) {
788 p_line
[filldst
] = p_line
[filldst
+ 1];
789 p_char
[filldst
] = p_char
[filldst
+ 1];
790 p_len
[filldst
] = p_len
[filldst
+ 1];
794 repl_beginning
--; /* this doesn't need to be fixed */
797 p_first
++; /* do append rather than insert */
801 if (diff_type
== CONTEXT_DIFF
&&
802 (fillcnt
|| (p_first
> 1 && ptrn_copiable
> 2 * p_context
))) {
805 "(Fascinating--this is really a new-style context diff but without",
806 "the telltale extra asterisks on the *** line that usually indicate",
807 "the new style...)");
808 diff_type
= NEW_CONTEXT_DIFF
;
810 /* if there were omitted context lines, fill them in now */
812 p_bfake
= filldst
; /* remember where not to free() */
813 p_efake
= filldst
+ fillcnt
- 1;
814 while (fillcnt
-- > 0) {
815 while (fillsrc
<= p_end
&& p_char
[fillsrc
] != ' ')
818 fatal("replacement text or line numbers mangled in hunk at line %ld\n",
820 p_line
[filldst
] = p_line
[fillsrc
];
821 p_char
[filldst
] = p_char
[fillsrc
];
822 p_len
[filldst
] = p_len
[fillsrc
];
826 while (fillsrc
<= p_end
&& fillsrc
!= repl_beginning
&&
827 p_char
[fillsrc
] != ' ')
831 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
832 fillsrc
, filldst
, repl_beginning
, p_end
+ 1);
834 if (fillsrc
!= p_end
+ 1 && fillsrc
!= repl_beginning
)
836 if (filldst
!= p_end
+ 1 && filldst
!= repl_beginning
)
839 if (p_line
[p_end
] != NULL
) {
840 if (remove_special_line()) {
842 (p_line
[p_end
])[p_len
[p_end
]] = 0;
845 } else if (diff_type
== UNI_DIFF
) {
846 LINENUM fillold
; /* index of old lines */
847 LINENUM fillnew
; /* index of new lines */
850 line_beginning
= ftell(pfp
); /* file pos of the current line */
851 ret
= pgets(buf
, buf_len
, pfp
);
853 if (ret
== NULL
|| strnNE(buf
, "@@ -", 4)) {
854 next_intuit_at(line_beginning
, p_input_line
);
860 p_first
= (LINENUM
) atol(s
);
861 while (isdigit((unsigned char)*s
))
864 p_ptrn_lines
= (LINENUM
) atol(++s
);
865 while (isdigit((unsigned char)*s
))
871 if (*s
!= '+' || !*++s
)
873 p_newfirst
= (LINENUM
) atol(s
);
874 while (isdigit((unsigned char)*s
))
877 p_repl_lines
= (LINENUM
) atol(++s
);
878 while (isdigit((unsigned char)*s
))
887 p_first
++; /* do append rather than insert */
888 p_max
= p_ptrn_lines
+ p_repl_lines
+ 1;
889 while (p_max
>= hunkmax
)
892 fillnew
= fillold
+ p_ptrn_lines
;
893 p_end
= fillnew
+ p_repl_lines
;
894 snprintf(buf
, buf_len
, "*** %ld,%ld ****\n", p_first
,
895 p_first
+ p_ptrn_lines
- 1);
896 p_line
[0] = savestr(buf
);
902 snprintf(buf
, buf_len
, "--- %ld,%ld ----\n", p_newfirst
,
903 p_newfirst
+ p_repl_lines
- 1);
904 p_line
[fillnew
] = savestr(buf
);
909 p_char
[fillnew
++] = '=';
912 p_hunk_beg
= p_input_line
+ 1;
913 while (fillold
<= p_ptrn_lines
|| fillnew
<= p_end
) {
914 line_beginning
= ftell(pfp
);
915 ret
= pgets(buf
, buf_len
, pfp
);
918 if (p_max
- fillnew
< 3) {
919 /* assume blank lines got chopped */
920 strlcpy(buf
, " \n", buf_len
);
922 fatal("unexpected end of file in patch\n");
925 if (*buf
== '\t' || *buf
== '\n') {
926 ch
= ' '; /* assume the space got eaten */
930 s
= savestr(buf
+ 1);
933 while (--fillnew
> p_ptrn_lines
)
934 free(p_line
[fillnew
]);
940 if (fillold
> p_ptrn_lines
) {
945 p_char
[fillold
] = ch
;
947 p_len
[fillold
++] = strlen(s
);
948 if (fillold
> p_ptrn_lines
) {
949 if (remove_special_line()) {
950 p_len
[fillold
- 1] -= 1;
951 s
[p_len
[fillold
- 1]] = 0;
959 if (fillold
> p_ptrn_lines
) {
961 while (--fillnew
> p_ptrn_lines
)
962 free(p_line
[fillnew
]);
967 p_char
[fillold
] = ch
;
969 p_len
[fillold
++] = strlen(s
);
972 while (--fillnew
> p_ptrn_lines
)
973 free(p_line
[fillnew
]);
977 if (fillold
> p_ptrn_lines
) {
978 if (remove_special_line()) {
979 p_len
[fillold
- 1] -= 1;
980 s
[p_len
[fillold
- 1]] = 0;
985 if (fillnew
> p_end
) {
987 while (--fillnew
> p_ptrn_lines
)
988 free(p_line
[fillnew
]);
992 p_char
[fillnew
] = ch
;
994 p_len
[fillnew
++] = strlen(s
);
995 if (fillold
> p_ptrn_lines
) {
996 if (remove_special_line()) {
997 p_len
[fillnew
- 1] -= 1;
998 s
[p_len
[fillnew
- 1]] = 0;
1006 if (ch
!= ' ' && context
> 0) {
1007 if (context
< p_context
)
1008 p_context
= context
;
1012 } else { /* normal diff--fake it up */
1017 line_beginning
= ftell(pfp
);
1019 ret
= pgets(buf
, buf_len
, pfp
);
1021 if (ret
== NULL
|| !isdigit((unsigned char)*buf
)) {
1022 next_intuit_at(line_beginning
, p_input_line
);
1025 p_first
= (LINENUM
) atol(buf
);
1026 for (s
= buf
; isdigit((unsigned char)*s
); s
++)
1029 p_ptrn_lines
= (LINENUM
) atol(++s
) - p_first
+ 1;
1030 while (isdigit((unsigned char)*s
))
1033 p_ptrn_lines
= (*s
!= 'a');
1035 if (hunk_type
== 'a')
1036 p_first
++; /* do append rather than insert */
1037 min
= (LINENUM
) atol(++s
);
1038 for (; isdigit((unsigned char)*s
); s
++)
1041 max
= (LINENUM
) atol(++s
);
1044 if (hunk_type
== 'd')
1046 p_end
= p_ptrn_lines
+ 1 + max
- min
+ 1;
1047 if (p_end
> MAXHUNKSIZE
)
1048 fatal("hunk too large (%ld lines) at line %ld: %s",
1049 p_end
, p_input_line
, buf
);
1050 while (p_end
>= hunkmax
)
1053 p_repl_lines
= max
- min
+ 1;
1054 snprintf(buf
, buf_len
, "*** %ld,%ld\n", p_first
,
1055 p_first
+ p_ptrn_lines
- 1);
1056 p_line
[0] = savestr(buf
);
1062 for (i
= 1; i
<= p_ptrn_lines
; i
++) {
1063 ret
= pgets(buf
, buf_len
, pfp
);
1066 fatal("unexpected end of file in patch at line %ld\n",
1069 fatal("< expected at line %ld of patch\n",
1071 p_line
[i
] = savestr(buf
+ 2);
1076 p_len
[i
] = strlen(p_line
[i
]);
1080 if (remove_special_line()) {
1082 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1084 if (hunk_type
== 'c') {
1085 ret
= pgets(buf
, buf_len
, pfp
);
1088 fatal("unexpected end of file in patch at line %ld\n",
1091 fatal("--- expected at line %ld of patch\n",
1094 snprintf(buf
, buf_len
, "--- %ld,%ld\n", min
, max
);
1095 p_line
[i
] = savestr(buf
);
1101 for (i
++; i
<= p_end
; i
++) {
1102 ret
= pgets(buf
, buf_len
, pfp
);
1105 fatal("unexpected end of file in patch at line %ld\n",
1108 fatal("> expected at line %ld of patch\n",
1110 p_line
[i
] = savestr(buf
+ 2);
1115 p_len
[i
] = strlen(p_line
[i
]);
1119 if (remove_special_line()) {
1121 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1124 if (reverse
) /* backwards patch? */
1126 say("Not enough memory to swap next hunk!\n");
1132 for (i
= 0; i
<= p_end
; i
++) {
1133 if (i
== p_ptrn_lines
)
1137 fprintf(stderr
, "%3d %c %c %s", i
, p_char
[i
],
1138 special
, p_line
[i
]);
1143 if (p_end
+ 1 < hunkmax
)/* paranoia reigns supreme... */
1144 p_char
[p_end
+ 1] = '^'; /* add a stopper for apply_hunk */
1149 * Input a line from the patch file, worrying about indentation.
1152 pgets(char *bf
, int sz
, FILE *fp
)
1154 char *s
, *ret
= fgets(bf
, sz
, fp
);
1157 if (p_indent
&& ret
!= NULL
) {
1159 indent
< p_indent
&& (*s
== ' ' || *s
== '\t' || *s
== 'X');
1162 indent
+= 8 - (indent
% 7);
1166 if (buf
!= s
&& strlcpy(buf
, s
, buf_len
) >= buf_len
)
1167 fatal("buffer too small in pgets()\n");
1173 * Reverse the old and new portions of the current hunk.
1178 char **tp_line
; /* the text of the hunk */
1179 short *tp_len
; /* length of each line */
1180 char *tp_char
; /* +, -, and ! */
1183 bool blankline
= false;
1187 p_first
= p_newfirst
;
1190 /* make a scratch copy */
1195 p_line
= NULL
; /* force set_hunkmax to allocate again */
1199 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
) {
1207 return false; /* not enough memory to swap hunk! */
1209 /* now turn the new into the old */
1211 i
= p_ptrn_lines
+ 1;
1212 if (tp_char
[i
] == '\n') { /* account for possible blank line */
1216 if (p_efake
>= 0) { /* fix non-freeable ptr range */
1224 for (n
= 0; i
<= p_end
; i
++, n
++) {
1225 p_line
[n
] = tp_line
[i
];
1226 p_char
[n
] = tp_char
[i
];
1227 if (p_char
[n
] == '+')
1229 p_len
[n
] = tp_len
[i
];
1232 i
= p_ptrn_lines
+ 1;
1233 p_line
[n
] = tp_line
[i
];
1234 p_char
[n
] = tp_char
[i
];
1235 p_len
[n
] = tp_len
[i
];
1238 if (p_char
[0] != '=')
1239 fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1240 p_input_line
, p_char
[0]);
1242 for (s
= p_line
[0]; *s
; s
++)
1246 /* now turn the old into the new */
1248 if (p_char
[0] != '*')
1249 fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
1250 p_input_line
, p_char
[0]);
1252 for (s
= tp_line
[0]; *s
; s
++)
1255 for (i
= 0; n
<= p_end
; i
++, n
++) {
1256 p_line
[n
] = tp_line
[i
];
1257 p_char
[n
] = tp_char
[i
];
1258 if (p_char
[n
] == '-')
1260 p_len
[n
] = tp_len
[i
];
1263 if (i
!= p_ptrn_lines
+ 1)
1264 fatal("Malformed patch at line %ld: expected %ld lines, "
1266 p_input_line
, p_ptrn_lines
+ 1, i
);
1269 p_ptrn_lines
= p_repl_lines
;
1280 * Return the specified line position in the old file of the old context.
1289 * Return the number of lines of old context.
1292 pch_ptrn_lines(void)
1294 return p_ptrn_lines
;
1298 * Return the probable line position in the new file of the first line.
1307 * Return the number of lines in the replacement text including context.
1310 pch_repl_lines(void)
1312 return p_repl_lines
;
1316 * Return the number of lines in the whole hunk.
1325 * Return the number of context lines before the first changed line.
1334 * Return the length of a particular patch line.
1337 pch_line_len(LINENUM line
)
1343 * Return the control character (+, -, *, !, etc) for a patch line.
1346 pch_char(LINENUM line
)
1348 return p_char
[line
];
1352 * Return a pointer to a particular patch line.
1355 pfetch(LINENUM line
)
1357 return p_line
[line
];
1361 * Return where in the patch file this hunk began, for error messages.
1370 * Apply an ed script by feeding ed itself.
1376 long beginning_of_this_line
;
1377 FILE *pipefp
= NULL
;
1379 if (!skip_rest_of_patch
) {
1380 if (copy_file(filearg
[0], TMPOUTNAME
) < 0) {
1382 fatal("can't create temp file %s", TMPOUTNAME
);
1384 snprintf(buf
, buf_len
, "%s%s%s", _PATH_ED
,
1385 verbose
? " " : " -s ", TMPOUTNAME
);
1386 pipefp
= popen(buf
, "w");
1389 beginning_of_this_line
= ftell(pfp
);
1390 if (pgets(buf
, buf_len
, pfp
) == NULL
) {
1391 next_intuit_at(beginning_of_this_line
, p_input_line
);
1395 for (t
= buf
; isdigit((unsigned char)*t
) || *t
== ','; t
++)
1397 /* POSIX defines allowed commands as {a,c,d,i,s} */
1398 if (isdigit((unsigned char)*buf
) && (*t
== 'a' || *t
== 'c' ||
1399 *t
== 'd' || *t
== 'i' || *t
== 's')) {
1403 while (pgets(buf
, buf_len
, pfp
) != NULL
) {
1407 if (strEQ(buf
, ".\n"))
1412 next_intuit_at(beginning_of_this_line
, p_input_line
);
1418 fprintf(pipefp
, "w\n");
1419 fprintf(pipefp
, "q\n");
1424 if (move_file(TMPOUTNAME
, outname
) < 0) {
1426 chmod(TMPOUTNAME
, filemode
);
1428 chmod(outname
, filemode
);
1434 * Choose the name of the file to be patched based on POSIX rules.
1435 * NOTE: the POSIX rules are amazingly stupid and we only follow them
1436 * if the user specified --posix or set POSIXLY_CORRECT.
1439 posix_name(const struct file_name
*names
, bool assume_exists
)
1445 * POSIX states that the filename will be chosen from one
1446 * of the old, new and index names (in that order) if
1447 * the file exists relative to CWD after -p stripping.
1449 for (i
= 0; i
< MAX_FILE
; i
++) {
1450 if (names
[i
].path
!= NULL
&& names
[i
].exists
) {
1451 path
= names
[i
].path
;
1455 if (path
== NULL
&& !assume_exists
) {
1457 * No files found, look for something we can checkout from
1458 * RCS/SCCS dirs. Same order as above.
1460 for (i
= 0; i
< MAX_FILE
; i
++) {
1461 if (names
[i
].path
!= NULL
&&
1462 (path
= checked_in(names
[i
].path
)) != NULL
)
1466 * Still no match? Check to see if the diff could be creating
1469 if (path
== NULL
&& ok_to_create_file
&&
1470 names
[NEW_FILE
].path
!= NULL
)
1471 path
= names
[NEW_FILE
].path
;
1474 return path
? savestr(path
) : NULL
;
1478 * Choose the name of the file to be patched based the "best" one
1482 best_name(const struct file_name
*names
, bool assume_exists
)
1484 size_t min_components
, min_baselen
, min_len
, tmp
;
1489 * The "best" name is the one with the fewest number of path
1490 * components, the shortest basename length, and the shortest
1491 * overall length (in that order). We only use the Index: file
1492 * if neither of the old or new files could be intuited from
1495 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1496 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1497 if (names
[i
].path
== NULL
||
1498 (!names
[i
].exists
&& !assume_exists
))
1500 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1502 min_components
= tmp
;
1503 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1506 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1509 best
= names
[i
].path
;
1513 * No files found, look for something we can checkout from
1514 * RCS/SCCS dirs. Logic is identical to that above...
1516 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1517 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1518 if (names
[i
].path
== NULL
||
1519 checked_in(names
[i
].path
) == NULL
)
1521 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1523 min_components
= tmp
;
1524 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1527 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1530 best
= names
[i
].path
;
1533 * Still no match? Check to see if the diff could be creating
1536 if (best
== NULL
&& ok_to_create_file
&&
1537 names
[NEW_FILE
].path
!= NULL
)
1538 best
= names
[NEW_FILE
].path
;
1541 return best
? savestr(best
) : NULL
;
1545 num_components(const char *path
)
1550 for (n
= 0, cp
= path
; (cp
= strchr(cp
, '/')) != NULL
; n
++, cp
++) {
1552 cp
++; /* skip consecutive slashes */