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.23 2008/09/19 18:33:34 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>
35 #include <sys/types.h>
49 #include "pathnames.h"
51 /* Patch (diff listing) abstract type. */
53 static long p_filesize
; /* size of the patch file */
54 static LINENUM p_first
; /* 1st line number */
55 static LINENUM p_newfirst
; /* 1st line number of replacement */
56 static LINENUM p_ptrn_lines
; /* # lines in pattern */
57 static LINENUM p_repl_lines
; /* # lines in replacement text */
58 static LINENUM p_end
= -1; /* last line in hunk */
59 static LINENUM p_max
; /* max allowed value of p_end */
60 static LINENUM p_context
= 3; /* # of context lines */
61 static LINENUM p_input_line
= 0; /* current line # from patch file */
62 static char **p_line
= NULL
;/* the text of the hunk */
63 static short *p_len
= NULL
; /* length of each line */
64 static char *p_char
= NULL
; /* +, -, and ! */
65 static int hunkmax
= INITHUNKMAX
; /* size of above arrays to begin with */
66 static int p_indent
; /* indent to patch */
67 static LINENUM p_base
; /* where to intuit this time */
68 static LINENUM p_bline
; /* line # of p_base */
69 static LINENUM p_start
; /* where intuit found a patch */
70 static LINENUM p_sline
; /* and the line number for it */
71 static LINENUM p_hunk_beg
; /* line number of current hunk */
72 static LINENUM p_efake
= -1; /* end of faked up lines--don't free */
73 static LINENUM p_bfake
= -1; /* beg of faked up lines */
74 static FILE *pfp
= NULL
; /* patch file pointer */
75 static char *bestguess
= NULL
; /* guess at correct filename */
77 static void grow_hunkmax(void);
78 static int intuit_diff_type(void);
79 static void next_intuit_at(LINENUM
, LINENUM
);
80 static void skip_to(LINENUM
, LINENUM
);
81 static char *pgets(char *, int, FILE *);
82 static char *best_name(const struct file_name
*, bool);
83 static char *posix_name(const struct file_name
*, bool);
84 static size_t num_components(const char *);
87 * Prepare to look for the next patch in the patch file.
96 p_end
= (LINENUM
) - 1;
102 * Open the patch file at the beginning of time.
105 open_patch_file(const char *filename
)
107 struct stat filestat
;
109 if (filename
== NULL
|| *filename
== '\0' || strEQ(filename
, "-")) {
110 pfp
= fopen(TMPPATNAME
, "w");
112 pfatal("can't create %s", TMPPATNAME
);
113 while (fgets(buf
, buf_len
, stdin
) != NULL
)
115 if (ferror(pfp
) || fclose(pfp
))
116 pfatal("can't write %s", TMPPATNAME
);
117 filename
= TMPPATNAME
;
119 pfp
= fopen(filename
, "r");
121 pfatal("patch file %s not found", filename
);
122 fstat(fileno(pfp
), &filestat
);
123 p_filesize
= filestat
.st_size
;
124 next_intuit_at(0L, 1L); /* start at the beginning */
129 * Make sure our dynamically realloced tables are malloced to begin with.
135 p_line
= calloc((size_t) hunkmax
, sizeof(char *));
137 p_len
= calloc((size_t) hunkmax
, sizeof(short));
139 p_char
= calloc((size_t) hunkmax
, sizeof(char));
143 * Enlarge the arrays containing the current hunk of patch.
153 new_hunkmax
= hunkmax
* 2;
155 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
)
156 fatal("Internal memory allocation error\n");
158 new_p_line
= realloc(p_line
, new_hunkmax
* sizeof(char *));
159 if (new_p_line
== NULL
)
162 new_p_len
= realloc(p_len
, new_hunkmax
* sizeof(short));
163 if (new_p_len
== NULL
)
166 new_p_char
= realloc(p_char
, new_hunkmax
* sizeof(char));
167 if (new_p_char
== NULL
)
174 if (p_line
!= NULL
&& p_len
!= NULL
&& p_char
!= NULL
) {
175 hunkmax
= new_hunkmax
;
180 fatal("out of memory\n");
181 out_of_mem
= true; /* whatever is null will be allocated again */
182 /* from within plan_a(), of all places */
185 /* True if the remainder of the patch file contains a diff of some sort. */
188 there_is_another_patch(void)
192 if (p_base
!= 0L && p_base
>= p_filesize
) {
199 diff_type
= intuit_diff_type();
203 say(" Ignoring the trailing garbage.\ndone\n");
205 say(" I can't seem to find a patch in there anywhere.\n");
209 say(" %sooks like %s to me...\n",
210 (p_base
== 0L ? "L" : "The next patch l"),
211 diff_type
== UNI_DIFF
? "a unified diff" :
212 diff_type
== CONTEXT_DIFF
? "a context diff" :
213 diff_type
== NEW_CONTEXT_DIFF
? "a new-style context diff" :
214 diff_type
== NORMAL_DIFF
? "a normal diff" :
216 if (p_indent
&& verbose
)
217 say("(Patch is indented %d space%s.)\n", p_indent
,
218 p_indent
== 1 ? "" : "s");
219 skip_to(p_start
, p_sline
);
220 while (filearg
[0] == NULL
) {
221 if (force
|| batch
) {
222 say("No file to patch. Skipping...\n");
223 filearg
[0] = savestr(bestguess
);
224 skip_rest_of_patch
= true;
227 ask("File to patch: ");
230 bestguess
= savestr(buf
);
231 filearg
[0] = fetchname(buf
, &exists
, 0);
234 ask("No file found--skip this patch? [n] ");
238 say("Skipping patch...\n");
240 filearg
[0] = fetchname(bestguess
, &exists
, 0);
241 skip_rest_of_patch
= true;
248 /* Determine what kind of diff is in the remaining part of the patch file. */
251 intuit_diff_type(void)
253 long this_line
= 0, previous_line
;
254 long first_command_line
= -1;
255 LINENUM fcl_line
= -1;
256 bool last_line_was_command
= false, this_is_a_command
= false;
257 bool stars_last_line
= false, stars_this_line
= false;
260 struct file_name names
[MAX_FILE
];
262 memset(names
, 0, sizeof(names
));
263 ok_to_create_file
= false;
264 fseek(pfp
, p_base
, SEEK_SET
);
265 p_input_line
= p_bline
- 1;
267 previous_line
= this_line
;
268 last_line_was_command
= this_is_a_command
;
269 stars_last_line
= stars_this_line
;
270 this_line
= ftell(pfp
);
273 if (fgets(buf
, buf_len
, pfp
) == NULL
) {
274 if (first_command_line
>= 0L) {
275 /* nothing but deletes!? */
276 p_start
= first_command_line
;
282 p_sline
= p_input_line
;
287 for (s
= buf
; *s
== ' ' || *s
== '\t' || *s
== 'X'; s
++) {
289 indent
+= 8 - (indent
% 8);
293 for (t
= s
; isdigit((unsigned char)*t
) || *t
== ','; t
++)
295 this_is_a_command
= (isdigit((unsigned char)*s
) &&
296 (*t
== 'd' || *t
== 'c' || *t
== 'a'));
297 if (first_command_line
< 0L && this_is_a_command
) {
298 first_command_line
= this_line
;
299 fcl_line
= p_input_line
;
300 p_indent
= indent
; /* assume this for now */
302 if (!stars_last_line
&& strnEQ(s
, "*** ", 4))
303 names
[OLD_FILE
].path
= fetchname(s
+ 4,
304 &names
[OLD_FILE
].exists
, strippath
);
305 else if (strnEQ(s
, "--- ", 4))
306 names
[NEW_FILE
].path
= fetchname(s
+ 4,
307 &names
[NEW_FILE
].exists
, strippath
);
308 else if (strnEQ(s
, "+++ ", 4))
309 /* pretend it is the old name */
310 names
[OLD_FILE
].path
= fetchname(s
+ 4,
311 &names
[OLD_FILE
].exists
, strippath
);
312 else if (strnEQ(s
, "Index:", 6))
313 names
[INDEX_FILE
].path
= fetchname(s
+ 6,
314 &names
[INDEX_FILE
].exists
, strippath
);
315 else if (strnEQ(s
, "Prereq:", 7)) {
316 for (t
= s
+ 7; isspace((unsigned char)*t
); t
++)
318 revision
= savestr(t
);
319 for (t
= revision
; *t
&& !isspace((unsigned char)*t
); t
++)
322 if (*revision
== '\0') {
327 if ((!diff_type
|| diff_type
== ED_DIFF
) &&
328 first_command_line
>= 0L &&
331 p_start
= first_command_line
;
336 if ((!diff_type
|| diff_type
== UNI_DIFF
) && strnEQ(s
, "@@ -", 4)) {
337 if (strnEQ(s
+ 4, "0,0", 3))
338 ok_to_create_file
= true;
341 p_sline
= p_input_line
;
345 stars_this_line
= strnEQ(s
, "********", 8);
346 if ((!diff_type
|| diff_type
== CONTEXT_DIFF
) && stars_last_line
&&
347 strnEQ(s
, "*** ", 4)) {
348 if (atol(s
+ 4) == 0)
349 ok_to_create_file
= true;
351 * If this is a new context diff the character just
352 * before the newline is a '*'.
357 p_start
= previous_line
;
358 p_sline
= p_input_line
- 1;
359 retval
= (*(s
- 1) == '*' ? NEW_CONTEXT_DIFF
: CONTEXT_DIFF
);
362 if ((!diff_type
|| diff_type
== NORMAL_DIFF
) &&
363 last_line_was_command
&&
364 (strnEQ(s
, "< ", 2) || strnEQ(s
, "> ", 2))) {
365 p_start
= previous_line
;
366 p_sline
= p_input_line
- 1;
368 retval
= NORMAL_DIFF
;
373 if (retval
== UNI_DIFF
) {
374 /* unswap old and new */
375 struct file_name tmp
= names
[OLD_FILE
];
376 names
[OLD_FILE
] = names
[NEW_FILE
];
377 names
[NEW_FILE
] = tmp
;
379 if (filearg
[0] == NULL
) {
381 filearg
[0] = posix_name(names
, ok_to_create_file
);
383 /* Ignore the Index: name for context diffs, like GNU */
384 if (names
[OLD_FILE
].path
!= NULL
||
385 names
[NEW_FILE
].path
!= NULL
) {
386 free(names
[INDEX_FILE
].path
);
387 names
[INDEX_FILE
].path
= NULL
;
389 filearg
[0] = best_name(names
, ok_to_create_file
);
395 if (filearg
[0] != NULL
)
396 bestguess
= savestr(filearg
[0]);
397 else if (!ok_to_create_file
) {
399 * We don't want to create a new file but we need a
400 * filename to set bestguess. Avoid setting filearg[0]
401 * so the file is not created automatically.
404 bestguess
= posix_name(names
, true);
406 bestguess
= best_name(names
, true);
408 free(names
[OLD_FILE
].path
);
409 free(names
[NEW_FILE
].path
);
410 free(names
[INDEX_FILE
].path
);
415 * Remember where this patch ends so we know where to start up again.
418 next_intuit_at(LINENUM file_pos
, LINENUM file_line
)
425 * Basically a verbose fseek() to the actual diff listing.
428 skip_to(LINENUM file_pos
, LINENUM file_line
)
432 if (p_base
> file_pos
)
433 fatal("Internal error: seek %ld>%ld\n", p_base
, file_pos
);
434 if (verbose
&& p_base
< file_pos
) {
435 fseek(pfp
, p_base
, SEEK_SET
);
436 say("The text leading up to this was:\n--------------------------\n");
437 while (ftell(pfp
) < file_pos
) {
438 ret
= fgets(buf
, buf_len
, pfp
);
440 fatal("Unexpected end of file\n");
443 say("--------------------------\n");
445 fseek(pfp
, file_pos
, SEEK_SET
);
446 p_input_line
= file_line
- 1;
449 /* Make this a function for better debugging. */
453 fatal("malformed patch at line %ld: %s", p_input_line
, buf
);
454 /* about as informative as "Syntax error" in C */
458 * True if the line has been discarded (i.e. it is a line saying
459 * "\ No newline at end of file".)
462 remove_special_line(void)
470 } while (c
!= EOF
&& c
!= '\n');
475 fseek(pfp
, -1L, SEEK_CUR
);
481 * True if there is more of the current diff listing to process.
486 long line_beginning
; /* file pos of the current line */
487 LINENUM repl_beginning
; /* index of --- line */
488 LINENUM fillcnt
; /* #lines of missing ptrn or repl */
489 LINENUM fillsrc
; /* index of first line to copy */
490 LINENUM filldst
; /* index of first missing line */
491 bool ptrn_spaces_eaten
; /* ptrn was slightly misformed */
492 bool repl_could_be_missing
; /* no + or ! lines in this hunk */
493 bool repl_missing
; /* we are now backtracking */
494 long repl_backtrack_position
; /* file pos of first repl line */
495 LINENUM repl_patch_line
; /* input line number for same */
496 LINENUM ptrn_copiable
; /* # of copiable lines in ptrn */
501 if (p_end
== p_efake
)
502 p_end
= p_bfake
; /* don't free twice */
509 p_max
= hunkmax
; /* gets reduced when --- found */
510 if (diff_type
== CONTEXT_DIFF
|| diff_type
== NEW_CONTEXT_DIFF
) {
511 line_beginning
= ftell(pfp
);
516 ptrn_spaces_eaten
= false;
517 repl_could_be_missing
= true;
518 repl_missing
= false;
519 repl_backtrack_position
= 0;
523 ret
= pgets(buf
, buf_len
, pfp
);
525 if (ret
== NULL
|| strnNE(buf
, "********", 8)) {
526 next_intuit_at(line_beginning
, p_input_line
);
530 p_hunk_beg
= p_input_line
+ 1;
531 while (p_end
< p_max
) {
532 line_beginning
= ftell(pfp
);
533 ret
= pgets(buf
, buf_len
, pfp
);
536 if (p_max
- p_end
< 4) {
537 /* assume blank lines got chopped */
538 strlcpy(buf
, " \n", buf_len
);
540 if (repl_beginning
&& repl_could_be_missing
) {
544 fatal("unexpected end of file in patch\n");
548 if (p_end
>= hunkmax
)
549 fatal("Internal error: hunk larger than hunk "
551 p_char
[p_end
] = *buf
;
552 p_line
[p_end
] = NULL
;
555 if (strnEQ(buf
, "********", 8)) {
556 if (repl_beginning
&& repl_could_be_missing
) {
560 fatal("unexpected end of hunk "
565 if (repl_beginning
&& repl_could_be_missing
) {
569 fatal("unexpected *** at line %ld: %s",
573 p_line
[p_end
] = savestr(buf
);
578 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
582 if (strnEQ(s
, "0,0", 3))
583 memmove(s
, s
+ 2, strlen(s
+ 2) + 1);
584 p_first
= (LINENUM
) atol(s
);
585 while (isdigit((unsigned char)*s
))
588 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
592 p_ptrn_lines
= ((LINENUM
) atol(s
)) - p_first
+ 1;
600 /* we need this much at least */
601 p_max
= p_ptrn_lines
+ 6;
602 while (p_max
>= hunkmax
)
608 if (repl_beginning
||
609 (p_end
!= p_ptrn_lines
+ 1 +
610 (p_char
[p_end
- 1] == '\n'))) {
613 * `old' lines were omitted;
614 * set up to fill them in
615 * from 'new' context lines.
617 p_end
= p_ptrn_lines
+ 1;
620 fillcnt
= p_ptrn_lines
;
622 if (repl_beginning
) {
623 if (repl_could_be_missing
) {
627 fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
628 p_input_line
, p_hunk_beg
+ repl_beginning
);
630 fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
631 (p_end
<= p_ptrn_lines
634 p_input_line
, p_hunk_beg
);
638 repl_beginning
= p_end
;
639 repl_backtrack_position
= ftell(pfp
);
640 repl_patch_line
= p_input_line
;
641 p_line
[p_end
] = savestr(buf
);
647 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
651 p_newfirst
= (LINENUM
) atol(s
);
652 while (isdigit((unsigned char)*s
))
655 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
659 p_repl_lines
= ((LINENUM
) atol(s
)) -
661 } else if (p_newfirst
)
667 p_max
= p_repl_lines
+ p_end
;
668 if (p_max
> MAXHUNKSIZE
)
669 fatal("hunk too large (%ld lines) at line %ld: %s",
670 p_max
, p_input_line
, buf
);
671 while (p_max
>= hunkmax
)
673 if (p_repl_lines
!= ptrn_copiable
&&
674 (p_context
!= 0 || p_repl_lines
!= 1))
675 repl_could_be_missing
= false;
681 repl_could_be_missing
= false;
683 if (buf
[1] == '\n' && canonicalize
)
684 strlcpy(buf
+ 1, " \n", buf_len
- 1);
685 if (!isspace((unsigned char)buf
[1]) && buf
[1] != '>' &&
687 repl_beginning
&& repl_could_be_missing
) {
692 if (context
< p_context
)
696 p_line
[p_end
] = savestr(buf
+ 2);
701 if (p_end
== p_ptrn_lines
) {
702 if (remove_special_line()) {
705 len
= strlen(p_line
[p_end
]) - 1;
706 (p_line
[p_end
])[len
] = 0;
711 case '\n': /* assume the 2 spaces got eaten */
712 if (repl_beginning
&& repl_could_be_missing
&&
713 (!ptrn_spaces_eaten
||
714 diff_type
== NEW_CONTEXT_DIFF
)) {
718 p_line
[p_end
] = savestr(buf
);
723 if (p_end
!= p_ptrn_lines
+ 1) {
724 ptrn_spaces_eaten
|= (repl_beginning
!= 0);
732 if (!isspace((unsigned char)buf
[1]) &&
733 repl_beginning
&& repl_could_be_missing
) {
740 p_line
[p_end
] = savestr(buf
+ 2);
747 if (repl_beginning
&& repl_could_be_missing
) {
753 /* set up p_len for strncmp() so we don't have to */
754 /* assume null termination */
756 p_len
[p_end
] = strlen(p_line
[p_end
]);
762 if (p_end
>= 0 && !repl_beginning
)
763 fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
767 /* reset state back to just after --- */
768 p_input_line
= repl_patch_line
;
769 for (p_end
--; p_end
> repl_beginning
; p_end
--)
771 fseek(pfp
, repl_backtrack_position
, SEEK_SET
);
773 /* redundant 'new' context lines were omitted - set */
774 /* up to fill them in from the old file context */
775 if (!p_context
&& p_repl_lines
== 1) {
780 filldst
= repl_beginning
+ 1;
781 fillcnt
= p_repl_lines
;
783 } else if (!p_context
&& fillcnt
== 1) {
784 /* the first hunk was a null hunk with no context */
785 /* and we were expecting one line -- fix it up. */
786 while (filldst
< p_end
) {
787 p_line
[filldst
] = p_line
[filldst
+ 1];
788 p_char
[filldst
] = p_char
[filldst
+ 1];
789 p_len
[filldst
] = p_len
[filldst
+ 1];
793 repl_beginning
--; /* this doesn't need to be fixed */
796 p_first
++; /* do append rather than insert */
800 if (diff_type
== CONTEXT_DIFF
&&
801 (fillcnt
|| (p_first
> 1 && ptrn_copiable
> 2 * p_context
))) {
804 "(Fascinating--this is really a new-style context diff but without",
805 "the telltale extra asterisks on the *** line that usually indicate",
806 "the new style...)");
807 diff_type
= NEW_CONTEXT_DIFF
;
809 /* if there were omitted context lines, fill them in now */
811 p_bfake
= filldst
; /* remember where not to free() */
812 p_efake
= filldst
+ fillcnt
- 1;
813 while (fillcnt
-- > 0) {
814 while (fillsrc
<= p_end
&& p_char
[fillsrc
] != ' ')
817 fatal("replacement text or line numbers mangled in hunk at line %ld\n",
819 p_line
[filldst
] = p_line
[fillsrc
];
820 p_char
[filldst
] = p_char
[fillsrc
];
821 p_len
[filldst
] = p_len
[fillsrc
];
825 while (fillsrc
<= p_end
&& fillsrc
!= repl_beginning
&&
826 p_char
[fillsrc
] != ' ')
830 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
831 fillsrc
, filldst
, repl_beginning
, p_end
+ 1);
833 if (fillsrc
!= p_end
+ 1 && fillsrc
!= repl_beginning
)
835 if (filldst
!= p_end
+ 1 && filldst
!= repl_beginning
)
838 if (p_line
[p_end
] != NULL
) {
839 if (remove_special_line()) {
841 (p_line
[p_end
])[p_len
[p_end
]] = 0;
844 } else if (diff_type
== UNI_DIFF
) {
845 LINENUM fillold
; /* index of old lines */
846 LINENUM fillnew
; /* index of new lines */
849 line_beginning
= ftell(pfp
); /* file pos of the current line */
850 ret
= pgets(buf
, buf_len
, pfp
);
852 if (ret
== NULL
|| strnNE(buf
, "@@ -", 4)) {
853 next_intuit_at(line_beginning
, p_input_line
);
859 p_first
= (LINENUM
) atol(s
);
860 while (isdigit((unsigned char)*s
))
863 p_ptrn_lines
= (LINENUM
) atol(++s
);
864 while (isdigit((unsigned char)*s
))
870 if (*s
!= '+' || !*++s
)
872 p_newfirst
= (LINENUM
) atol(s
);
873 while (isdigit((unsigned char)*s
))
876 p_repl_lines
= (LINENUM
) atol(++s
);
877 while (isdigit((unsigned char)*s
))
886 p_first
++; /* do append rather than insert */
887 p_max
= p_ptrn_lines
+ p_repl_lines
+ 1;
888 while (p_max
>= hunkmax
)
891 fillnew
= fillold
+ p_ptrn_lines
;
892 p_end
= fillnew
+ p_repl_lines
;
893 snprintf(buf
, buf_len
, "*** %ld,%ld ****\n", p_first
,
894 p_first
+ p_ptrn_lines
- 1);
895 p_line
[0] = savestr(buf
);
901 snprintf(buf
, buf_len
, "--- %ld,%ld ----\n", p_newfirst
,
902 p_newfirst
+ p_repl_lines
- 1);
903 p_line
[fillnew
] = savestr(buf
);
908 p_char
[fillnew
++] = '=';
911 p_hunk_beg
= p_input_line
+ 1;
912 while (fillold
<= p_ptrn_lines
|| fillnew
<= p_end
) {
913 line_beginning
= ftell(pfp
);
914 ret
= pgets(buf
, buf_len
, pfp
);
917 if (p_max
- fillnew
< 3) {
918 /* assume blank lines got chopped */
919 strlcpy(buf
, " \n", buf_len
);
921 fatal("unexpected end of file in patch\n");
924 if (*buf
== '\t' || *buf
== '\n') {
925 ch
= ' '; /* assume the space got eaten */
929 s
= savestr(buf
+ 1);
932 while (--fillnew
> p_ptrn_lines
)
933 free(p_line
[fillnew
]);
939 if (fillold
> p_ptrn_lines
) {
944 p_char
[fillold
] = ch
;
946 p_len
[fillold
++] = strlen(s
);
947 if (fillold
> p_ptrn_lines
) {
948 if (remove_special_line()) {
949 p_len
[fillold
- 1] -= 1;
950 s
[p_len
[fillold
- 1]] = 0;
958 if (fillold
> p_ptrn_lines
) {
960 while (--fillnew
> p_ptrn_lines
)
961 free(p_line
[fillnew
]);
966 p_char
[fillold
] = ch
;
968 p_len
[fillold
++] = strlen(s
);
971 while (--fillnew
> p_ptrn_lines
)
972 free(p_line
[fillnew
]);
976 if (fillold
> p_ptrn_lines
) {
977 if (remove_special_line()) {
978 p_len
[fillold
- 1] -= 1;
979 s
[p_len
[fillold
- 1]] = 0;
984 if (fillnew
> p_end
) {
986 while (--fillnew
> p_ptrn_lines
)
987 free(p_line
[fillnew
]);
991 p_char
[fillnew
] = ch
;
993 p_len
[fillnew
++] = strlen(s
);
994 if (fillold
> p_ptrn_lines
) {
995 if (remove_special_line()) {
996 p_len
[fillnew
- 1] -= 1;
997 s
[p_len
[fillnew
- 1]] = 0;
1005 if (ch
!= ' ' && context
> 0) {
1006 if (context
< p_context
)
1007 p_context
= context
;
1011 } else { /* normal diff--fake it up */
1016 line_beginning
= ftell(pfp
);
1018 ret
= pgets(buf
, buf_len
, pfp
);
1020 if (ret
== NULL
|| !isdigit((unsigned char)*buf
)) {
1021 next_intuit_at(line_beginning
, p_input_line
);
1024 p_first
= (LINENUM
) atol(buf
);
1025 for (s
= buf
; isdigit((unsigned char)*s
); s
++)
1028 p_ptrn_lines
= (LINENUM
) atol(++s
) - p_first
+ 1;
1029 while (isdigit((unsigned char)*s
))
1032 p_ptrn_lines
= (*s
!= 'a');
1034 if (hunk_type
== 'a')
1035 p_first
++; /* do append rather than insert */
1036 min
= (LINENUM
) atol(++s
);
1037 for (; isdigit((unsigned char)*s
); s
++)
1040 max
= (LINENUM
) atol(++s
);
1043 if (hunk_type
== 'd')
1045 p_end
= p_ptrn_lines
+ 1 + max
- min
+ 1;
1046 if (p_end
> MAXHUNKSIZE
)
1047 fatal("hunk too large (%ld lines) at line %ld: %s",
1048 p_end
, p_input_line
, buf
);
1049 while (p_end
>= hunkmax
)
1052 p_repl_lines
= max
- min
+ 1;
1053 snprintf(buf
, buf_len
, "*** %ld,%ld\n", p_first
,
1054 p_first
+ p_ptrn_lines
- 1);
1055 p_line
[0] = savestr(buf
);
1061 for (i
= 1; i
<= p_ptrn_lines
; i
++) {
1062 ret
= pgets(buf
, buf_len
, pfp
);
1065 fatal("unexpected end of file in patch at line %ld\n",
1068 fatal("< expected at line %ld of patch\n",
1070 p_line
[i
] = savestr(buf
+ 2);
1075 p_len
[i
] = strlen(p_line
[i
]);
1079 if (remove_special_line()) {
1081 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1083 if (hunk_type
== 'c') {
1084 ret
= pgets(buf
, buf_len
, pfp
);
1087 fatal("unexpected end of file in patch at line %ld\n",
1090 fatal("--- expected at line %ld of patch\n",
1093 snprintf(buf
, buf_len
, "--- %ld,%ld\n", min
, max
);
1094 p_line
[i
] = savestr(buf
);
1100 for (i
++; i
<= p_end
; i
++) {
1101 ret
= pgets(buf
, buf_len
, pfp
);
1104 fatal("unexpected end of file in patch at line %ld\n",
1107 fatal("> expected at line %ld of patch\n",
1109 p_line
[i
] = savestr(buf
+ 2);
1114 p_len
[i
] = strlen(p_line
[i
]);
1118 if (remove_special_line()) {
1120 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1123 if (reverse
) /* backwards patch? */
1125 say("Not enough memory to swap next hunk!\n");
1131 for (i
= 0; i
<= p_end
; i
++) {
1132 if (i
== p_ptrn_lines
)
1136 fprintf(stderr
, "%3d %c %c %s", i
, p_char
[i
],
1137 special
, p_line
[i
]);
1142 if (p_end
+ 1 < hunkmax
)/* paranoia reigns supreme... */
1143 p_char
[p_end
+ 1] = '^'; /* add a stopper for apply_hunk */
1148 * Input a line from the patch file, worrying about indentation.
1151 pgets(char *bf
, int sz
, FILE *fp
)
1153 char *s
, *ret
= fgets(bf
, sz
, fp
);
1156 if (p_indent
&& ret
!= NULL
) {
1158 indent
< p_indent
&& (*s
== ' ' || *s
== '\t' || *s
== 'X');
1161 indent
+= 8 - (indent
% 7);
1165 if (buf
!= s
&& strlcpy(buf
, s
, buf_len
) >= buf_len
)
1166 fatal("buffer too small in pgets()\n");
1172 * Reverse the old and new portions of the current hunk.
1177 char **tp_line
; /* the text of the hunk */
1178 short *tp_len
; /* length of each line */
1179 char *tp_char
; /* +, -, and ! */
1182 bool blankline
= false;
1186 p_first
= p_newfirst
;
1189 /* make a scratch copy */
1194 p_line
= NULL
; /* force set_hunkmax to allocate again */
1198 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
) {
1206 return false; /* not enough memory to swap hunk! */
1208 /* now turn the new into the old */
1210 i
= p_ptrn_lines
+ 1;
1211 if (tp_char
[i
] == '\n') { /* account for possible blank line */
1215 if (p_efake
>= 0) { /* fix non-freeable ptr range */
1223 for (n
= 0; i
<= p_end
; i
++, n
++) {
1224 p_line
[n
] = tp_line
[i
];
1225 p_char
[n
] = tp_char
[i
];
1226 if (p_char
[n
] == '+')
1228 p_len
[n
] = tp_len
[i
];
1231 i
= p_ptrn_lines
+ 1;
1232 p_line
[n
] = tp_line
[i
];
1233 p_char
[n
] = tp_char
[i
];
1234 p_len
[n
] = tp_len
[i
];
1237 if (p_char
[0] != '=')
1238 fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1239 p_input_line
, p_char
[0]);
1241 for (s
= p_line
[0]; *s
; s
++)
1245 /* now turn the old into the new */
1247 if (p_char
[0] != '*')
1248 fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
1249 p_input_line
, p_char
[0]);
1251 for (s
= tp_line
[0]; *s
; s
++)
1254 for (i
= 0; n
<= p_end
; i
++, n
++) {
1255 p_line
[n
] = tp_line
[i
];
1256 p_char
[n
] = tp_char
[i
];
1257 if (p_char
[n
] == '-')
1259 p_len
[n
] = tp_len
[i
];
1262 if (i
!= p_ptrn_lines
+ 1)
1263 fatal("Malformed patch at line %ld: expected %ld lines, "
1265 p_input_line
, p_ptrn_lines
+ 1, i
);
1268 p_ptrn_lines
= p_repl_lines
;
1279 * Return the specified line position in the old file of the old context.
1288 * Return the number of lines of old context.
1291 pch_ptrn_lines(void)
1293 return p_ptrn_lines
;
1297 * Return the probable line position in the new file of the first line.
1306 * Return the number of lines in the replacement text including context.
1309 pch_repl_lines(void)
1311 return p_repl_lines
;
1315 * Return the number of lines in the whole hunk.
1324 * Return the number of context lines before the first changed line.
1333 * Return the length of a particular patch line.
1336 pch_line_len(LINENUM line
)
1342 * Return the control character (+, -, *, !, etc) for a patch line.
1345 pch_char(LINENUM line
)
1347 return p_char
[line
];
1351 * Return a pointer to a particular patch line.
1354 pfetch(LINENUM line
)
1356 return p_line
[line
];
1360 * Return where in the patch file this hunk began, for error messages.
1369 * Apply an ed script by feeding ed itself.
1375 long beginning_of_this_line
;
1376 FILE *pipefp
= NULL
;
1378 if (!skip_rest_of_patch
) {
1379 if (copy_file(filearg
[0], TMPOUTNAME
) < 0) {
1381 fatal("can't create temp file %s", TMPOUTNAME
);
1383 snprintf(buf
, buf_len
, "%s%s%s", _PATH_ED
,
1384 verbose
? " " : " -s ", TMPOUTNAME
);
1385 pipefp
= popen(buf
, "w");
1388 beginning_of_this_line
= ftell(pfp
);
1389 if (pgets(buf
, buf_len
, pfp
) == NULL
) {
1390 next_intuit_at(beginning_of_this_line
, p_input_line
);
1394 for (t
= buf
; isdigit((unsigned char)*t
) || *t
== ','; t
++)
1396 /* POSIX defines allowed commands as {a,c,d,i,s} */
1397 if (isdigit((unsigned char)*buf
) && (*t
== 'a' || *t
== 'c' ||
1398 *t
== 'd' || *t
== 'i' || *t
== 's')) {
1402 while (pgets(buf
, buf_len
, pfp
) != NULL
) {
1406 if (strEQ(buf
, ".\n"))
1411 next_intuit_at(beginning_of_this_line
, p_input_line
);
1417 fprintf(pipefp
, "w\n");
1418 fprintf(pipefp
, "q\n");
1423 if (move_file(TMPOUTNAME
, outname
) < 0) {
1425 chmod(TMPOUTNAME
, filemode
);
1427 chmod(outname
, filemode
);
1433 * Choose the name of the file to be patched based on POSIX rules.
1434 * NOTE: the POSIX rules are amazingly stupid and we only follow them
1435 * if the user specified --posix or set POSIXLY_CORRECT.
1438 posix_name(const struct file_name
*names
, bool assume_exists
)
1444 * POSIX states that the filename will be chosen from one
1445 * of the old, new and index names (in that order) if
1446 * the file exists relative to CWD after -p stripping.
1448 for (i
= 0; i
< MAX_FILE
; i
++) {
1449 if (names
[i
].path
!= NULL
&& names
[i
].exists
) {
1450 path
= names
[i
].path
;
1454 if (path
== NULL
&& !assume_exists
) {
1456 * No files found, look for something we can checkout from
1457 * RCS/SCCS dirs. Same order as above.
1459 for (i
= 0; i
< MAX_FILE
; i
++) {
1460 if (names
[i
].path
!= NULL
&&
1461 (path
= checked_in(names
[i
].path
)) != NULL
)
1465 * Still no match? Check to see if the diff could be creating
1468 if (path
== NULL
&& ok_to_create_file
&&
1469 names
[NEW_FILE
].path
!= NULL
)
1470 path
= names
[NEW_FILE
].path
;
1473 return path
? savestr(path
) : NULL
;
1477 * Choose the name of the file to be patched based the "best" one
1481 best_name(const struct file_name
*names
, bool assume_exists
)
1483 size_t min_components
, min_baselen
, min_len
, tmp
;
1488 * The "best" name is the one with the fewest number of path
1489 * components, the shortest basename length, and the shortest
1490 * overall length (in that order). We only use the Index: file
1491 * if neither of the old or new files could be intuited from
1494 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1495 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1496 if (names
[i
].path
== NULL
||
1497 (!names
[i
].exists
&& !assume_exists
))
1499 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1501 min_components
= tmp
;
1502 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1505 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1508 best
= names
[i
].path
;
1512 * No files found, look for something we can checkout from
1513 * RCS/SCCS dirs. Logic is identical to that above...
1515 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1516 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1517 if (names
[i
].path
== NULL
||
1518 checked_in(names
[i
].path
) == NULL
)
1520 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1522 min_components
= tmp
;
1523 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1526 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1529 best
= names
[i
].path
;
1532 * Still no match? Check to see if the diff could be creating
1535 if (best
== NULL
&& ok_to_create_file
&&
1536 names
[NEW_FILE
].path
!= NULL
)
1537 best
= names
[NEW_FILE
].path
;
1540 return best
? savestr(best
) : NULL
;
1544 num_components(const char *path
)
1549 for (n
= 0, cp
= path
; (cp
= strchr(cp
, '/')) != NULL
; n
++, cp
++) {
1551 cp
++; /* skip consecutive slashes */