2 * $OpenBSD: pch.c,v 1.35 2004/08/05 21:47:24 deraadt Exp $
3 * $DragonFly: src/usr.bin/patch/pch.c,v 1.4 2006/04/19 00:11:35 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
)
113 filename
= TMPPATNAME
;
115 pfp
= fopen(filename
, "r");
117 pfatal("patch file %s not found", filename
);
118 fstat(fileno(pfp
), &filestat
);
119 p_filesize
= filestat
.st_size
;
120 next_intuit_at(0L, 1L); /* start at the beginning */
125 * Make sure our dynamically realloced tables are malloced to begin with.
131 p_line
= malloc((size_t) hunkmax
* sizeof(char *));
133 p_len
= malloc((size_t) hunkmax
* sizeof(short));
135 p_char
= malloc((size_t) hunkmax
* sizeof(char));
139 * Enlarge the arrays containing the current hunk of patch.
149 new_hunkmax
= hunkmax
* 2;
151 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
)
152 fatal("Internal memory allocation error\n");
154 new_p_line
= realloc(p_line
, new_hunkmax
* sizeof(char *));
155 if (new_p_line
== NULL
)
158 new_p_len
= realloc(p_len
, new_hunkmax
* sizeof(short));
159 if (new_p_len
== NULL
)
162 new_p_char
= realloc(p_char
, new_hunkmax
* sizeof(char));
163 if (new_p_char
== NULL
)
170 if (p_line
!= NULL
&& p_len
!= NULL
&& p_char
!= NULL
) {
171 hunkmax
= new_hunkmax
;
176 fatal("out of memory\n");
177 out_of_mem
= true; /* whatever is null will be allocated again */
178 /* from within plan_a(), of all places */
181 /* True if the remainder of the patch file contains a diff of some sort. */
184 there_is_another_patch(void)
188 if (p_base
!= 0L && p_base
>= p_filesize
) {
195 diff_type
= intuit_diff_type();
199 say(" Ignoring the trailing garbage.\ndone\n");
201 say(" I can't seem to find a patch in there anywhere.\n");
205 say(" %sooks like %s to me...\n",
206 (p_base
== 0L ? "L" : "The next patch l"),
207 diff_type
== UNI_DIFF
? "a unified diff" :
208 diff_type
== CONTEXT_DIFF
? "a context diff" :
209 diff_type
== NEW_CONTEXT_DIFF
? "a new-style context diff" :
210 diff_type
== NORMAL_DIFF
? "a normal diff" :
212 if (p_indent
&& verbose
)
213 say("(Patch is indented %d space%s.)\n", p_indent
,
214 p_indent
== 1 ? "" : "s");
215 skip_to(p_start
, p_sline
);
216 while (filearg
[0] == NULL
) {
217 if (force
|| batch
) {
218 say("No file to patch. Skipping...\n");
219 filearg
[0] = savestr(bestguess
);
220 skip_rest_of_patch
= true;
223 ask("File to patch: ");
226 bestguess
= savestr(buf
);
227 filearg
[0] = fetchname(buf
, &exists
, 0);
230 ask("No file found--skip this patch? [n] ");
234 say("Skipping patch...\n");
236 filearg
[0] = fetchname(bestguess
, &exists
, 0);
237 skip_rest_of_patch
= true;
244 /* Determine what kind of diff is in the remaining part of the patch file. */
247 intuit_diff_type(void)
249 long this_line
= 0, previous_line
;
250 long first_command_line
= -1;
251 LINENUM fcl_line
= -1;
252 bool last_line_was_command
= false, this_is_a_command
= false;
253 bool stars_last_line
= false, stars_this_line
= false;
256 struct file_name names
[MAX_FILE
];
258 memset(names
, 0, sizeof(names
));
259 ok_to_create_file
= false;
260 fseek(pfp
, p_base
, SEEK_SET
);
261 p_input_line
= p_bline
- 1;
263 previous_line
= this_line
;
264 last_line_was_command
= this_is_a_command
;
265 stars_last_line
= stars_this_line
;
266 this_line
= ftell(pfp
);
269 if (fgets(buf
, buf_len
, pfp
) == NULL
) {
270 if (first_command_line
>= 0L) {
271 /* nothing but deletes!? */
272 p_start
= first_command_line
;
278 p_sline
= p_input_line
;
283 for (s
= buf
; *s
== ' ' || *s
== '\t' || *s
== 'X'; s
++) {
285 indent
+= 8 - (indent
% 8);
289 for (t
= s
; isdigit((unsigned char)*t
) || *t
== ','; t
++)
291 this_is_a_command
= (isdigit((unsigned char)*s
) &&
292 (*t
== 'd' || *t
== 'c' || *t
== 'a'));
293 if (first_command_line
< 0L && this_is_a_command
) {
294 first_command_line
= this_line
;
295 fcl_line
= p_input_line
;
296 p_indent
= indent
; /* assume this for now */
298 if (!stars_last_line
&& strnEQ(s
, "*** ", 4))
299 names
[OLD_FILE
].path
= fetchname(s
+ 4,
300 &names
[OLD_FILE
].exists
, strippath
);
301 else if (strnEQ(s
, "--- ", 4))
302 names
[NEW_FILE
].path
= fetchname(s
+ 4,
303 &names
[NEW_FILE
].exists
, strippath
);
304 else if (strnEQ(s
, "+++ ", 4))
305 /* pretend it is the old name */
306 names
[OLD_FILE
].path
= fetchname(s
+ 4,
307 &names
[OLD_FILE
].exists
, strippath
);
308 else if (strnEQ(s
, "Index:", 6))
309 names
[INDEX_FILE
].path
= fetchname(s
+ 6,
310 &names
[INDEX_FILE
].exists
, strippath
);
311 else if (strnEQ(s
, "Prereq:", 7)) {
312 for (t
= s
+ 7; isspace((unsigned char)*t
); t
++)
314 revision
= savestr(t
);
315 for (t
= revision
; *t
&& !isspace((unsigned char)*t
); t
++)
318 if (*revision
== '\0') {
323 if ((!diff_type
|| diff_type
== ED_DIFF
) &&
324 first_command_line
>= 0L &&
327 p_start
= first_command_line
;
332 if ((!diff_type
|| diff_type
== UNI_DIFF
) && strnEQ(s
, "@@ -", 4)) {
333 if (strnEQ(s
+ 4, "0,0", 3))
334 ok_to_create_file
= true;
337 p_sline
= p_input_line
;
341 stars_this_line
= strnEQ(s
, "********", 8);
342 if ((!diff_type
|| diff_type
== CONTEXT_DIFF
) && stars_last_line
&&
343 strnEQ(s
, "*** ", 4)) {
344 if (atol(s
+ 4) == 0)
345 ok_to_create_file
= true;
347 * If this is a new context diff the character just
348 * before the newline is a '*'.
353 p_start
= previous_line
;
354 p_sline
= p_input_line
- 1;
355 retval
= (*(s
- 1) == '*' ? NEW_CONTEXT_DIFF
: CONTEXT_DIFF
);
358 if ((!diff_type
|| diff_type
== NORMAL_DIFF
) &&
359 last_line_was_command
&&
360 (strnEQ(s
, "< ", 2) || strnEQ(s
, "> ", 2))) {
361 p_start
= previous_line
;
362 p_sline
= p_input_line
- 1;
364 retval
= NORMAL_DIFF
;
369 if (retval
== UNI_DIFF
) {
370 /* unswap old and new */
371 struct file_name tmp
= names
[OLD_FILE
];
372 names
[OLD_FILE
] = names
[NEW_FILE
];
373 names
[NEW_FILE
] = tmp
;
375 if (filearg
[0] == NULL
) {
377 filearg
[0] = posix_name(names
, ok_to_create_file
);
379 /* Ignore the Index: name for context diffs, like GNU */
380 if (names
[OLD_FILE
].path
!= NULL
||
381 names
[NEW_FILE
].path
!= NULL
) {
382 free(names
[INDEX_FILE
].path
);
383 names
[INDEX_FILE
].path
= NULL
;
385 filearg
[0] = best_name(names
, ok_to_create_file
);
391 if (filearg
[0] != NULL
)
392 bestguess
= savestr(filearg
[0]);
393 else if (!ok_to_create_file
) {
395 * We don't want to create a new file but we need a
396 * filename to set bestguess. Avoid setting filearg[0]
397 * so the file is not created automatically.
400 bestguess
= posix_name(names
, true);
402 bestguess
= best_name(names
, true);
404 free(names
[OLD_FILE
].path
);
405 free(names
[NEW_FILE
].path
);
406 free(names
[INDEX_FILE
].path
);
411 * Remember where this patch ends so we know where to start up again.
414 next_intuit_at(LINENUM file_pos
, LINENUM file_line
)
421 * Basically a verbose fseek() to the actual diff listing.
424 skip_to(LINENUM file_pos
, LINENUM file_line
)
428 if (p_base
> file_pos
)
429 fatal("Internal error: seek %ld>%ld\n", p_base
, file_pos
);
430 if (verbose
&& p_base
< file_pos
) {
431 fseek(pfp
, p_base
, SEEK_SET
);
432 say("The text leading up to this was:\n--------------------------\n");
433 while (ftell(pfp
) < file_pos
) {
434 ret
= fgets(buf
, buf_len
, pfp
);
436 fatal("Unexpected end of file\n");
439 say("--------------------------\n");
441 fseek(pfp
, file_pos
, SEEK_SET
);
442 p_input_line
= file_line
- 1;
445 /* Make this a function for better debugging. */
449 fatal("malformed patch at line %ld: %s", p_input_line
, buf
);
450 /* about as informative as "Syntax error" in C */
454 * True if the line has been discarded (i.e. it is a line saying
455 * "\ No newline at end of file".)
458 remove_special_line(void)
466 } while (c
!= EOF
&& c
!= '\n');
471 fseek(pfp
, -1L, SEEK_CUR
);
477 * True if there is more of the current diff listing to process.
482 long line_beginning
; /* file pos of the current line */
483 LINENUM repl_beginning
; /* index of --- line */
484 LINENUM fillcnt
; /* #lines of missing ptrn or repl */
485 LINENUM fillsrc
; /* index of first line to copy */
486 LINENUM filldst
; /* index of first missing line */
487 bool ptrn_spaces_eaten
; /* ptrn was slightly misformed */
488 bool repl_could_be_missing
; /* no + or ! lines in this hunk */
489 bool repl_missing
; /* we are now backtracking */
490 long repl_backtrack_position
; /* file pos of first repl line */
491 LINENUM repl_patch_line
; /* input line number for same */
492 LINENUM ptrn_copiable
; /* # of copiable lines in ptrn */
497 if (p_end
== p_efake
)
498 p_end
= p_bfake
; /* don't free twice */
505 p_max
= hunkmax
; /* gets reduced when --- found */
506 if (diff_type
== CONTEXT_DIFF
|| diff_type
== NEW_CONTEXT_DIFF
) {
507 line_beginning
= ftell(pfp
);
512 ptrn_spaces_eaten
= false;
513 repl_could_be_missing
= true;
514 repl_missing
= false;
515 repl_backtrack_position
= 0;
519 ret
= pgets(buf
, buf_len
, pfp
);
521 if (ret
== NULL
|| strnNE(buf
, "********", 8)) {
522 next_intuit_at(line_beginning
, p_input_line
);
526 p_hunk_beg
= p_input_line
+ 1;
527 while (p_end
< p_max
) {
528 line_beginning
= ftell(pfp
);
529 ret
= pgets(buf
, buf_len
, pfp
);
532 if (p_max
- p_end
< 4) {
533 /* assume blank lines got chopped */
534 strlcpy(buf
, " \n", buf_len
);
536 if (repl_beginning
&& repl_could_be_missing
) {
540 fatal("unexpected end of file in patch\n");
544 if (p_end
>= hunkmax
)
545 fatal("Internal error: hunk larger than hunk "
547 p_char
[p_end
] = *buf
;
548 p_line
[p_end
] = NULL
;
551 if (strnEQ(buf
, "********", 8)) {
552 if (repl_beginning
&& repl_could_be_missing
) {
556 fatal("unexpected end of hunk "
561 if (repl_beginning
&& repl_could_be_missing
) {
565 fatal("unexpected *** at line %ld: %s",
569 p_line
[p_end
] = savestr(buf
);
574 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
578 if (strnEQ(s
, "0,0", 3))
579 memmove(s
, s
+ 2, strlen(s
+ 2) + 1);
580 p_first
= (LINENUM
) atol(s
);
581 while (isdigit((unsigned char)*s
))
584 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
588 p_ptrn_lines
= ((LINENUM
) atol(s
)) - p_first
+ 1;
596 /* we need this much at least */
597 p_max
= p_ptrn_lines
+ 6;
598 while (p_max
>= hunkmax
)
604 if (repl_beginning
||
605 (p_end
!= p_ptrn_lines
+ 1 +
606 (p_char
[p_end
- 1] == '\n'))) {
609 * `old' lines were omitted;
610 * set up to fill them in
611 * from 'new' context lines.
613 p_end
= p_ptrn_lines
+ 1;
616 fillcnt
= p_ptrn_lines
;
618 if (repl_beginning
) {
619 if (repl_could_be_missing
) {
623 fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
624 p_input_line
, p_hunk_beg
+ repl_beginning
);
626 fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
627 (p_end
<= p_ptrn_lines
630 p_input_line
, p_hunk_beg
);
634 repl_beginning
= p_end
;
635 repl_backtrack_position
= ftell(pfp
);
636 repl_patch_line
= p_input_line
;
637 p_line
[p_end
] = savestr(buf
);
643 for (s
= buf
; *s
&& !isdigit((unsigned char)*s
); s
++)
647 p_newfirst
= (LINENUM
) atol(s
);
648 while (isdigit((unsigned char)*s
))
651 for (; *s
&& !isdigit((unsigned char)*s
); s
++)
655 p_repl_lines
= ((LINENUM
) atol(s
)) -
657 } else if (p_newfirst
)
663 p_max
= p_repl_lines
+ p_end
;
664 if (p_max
> MAXHUNKSIZE
)
665 fatal("hunk too large (%ld lines) at line %ld: %s",
666 p_max
, p_input_line
, buf
);
667 while (p_max
>= hunkmax
)
669 if (p_repl_lines
!= ptrn_copiable
&&
670 (p_context
!= 0 || p_repl_lines
!= 1))
671 repl_could_be_missing
= false;
677 repl_could_be_missing
= false;
679 if (buf
[1] == '\n' && canonicalize
)
680 strlcpy(buf
+ 1, " \n", buf_len
- 1);
681 if (!isspace((unsigned char)buf
[1]) && buf
[1] != '>' &&
683 repl_beginning
&& repl_could_be_missing
) {
688 if (context
< p_context
)
692 p_line
[p_end
] = savestr(buf
+ 2);
697 if (p_end
== p_ptrn_lines
) {
698 if (remove_special_line()) {
701 len
= strlen(p_line
[p_end
]) - 1;
702 (p_line
[p_end
])[len
] = 0;
707 case '\n': /* assume the 2 spaces got eaten */
708 if (repl_beginning
&& repl_could_be_missing
&&
709 (!ptrn_spaces_eaten
||
710 diff_type
== NEW_CONTEXT_DIFF
)) {
714 p_line
[p_end
] = savestr(buf
);
719 if (p_end
!= p_ptrn_lines
+ 1) {
720 ptrn_spaces_eaten
|= (repl_beginning
!= 0);
728 if (!isspace((unsigned char)buf
[1]) &&
729 repl_beginning
&& repl_could_be_missing
) {
736 p_line
[p_end
] = savestr(buf
+ 2);
743 if (repl_beginning
&& repl_could_be_missing
) {
749 /* set up p_len for strncmp() so we don't have to */
750 /* assume null termination */
752 p_len
[p_end
] = strlen(p_line
[p_end
]);
758 if (p_end
>= 0 && !repl_beginning
)
759 fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
763 /* reset state back to just after --- */
764 p_input_line
= repl_patch_line
;
765 for (p_end
--; p_end
> repl_beginning
; p_end
--)
767 fseek(pfp
, repl_backtrack_position
, SEEK_SET
);
769 /* redundant 'new' context lines were omitted - set */
770 /* up to fill them in from the old file context */
771 if (!p_context
&& p_repl_lines
== 1) {
776 filldst
= repl_beginning
+ 1;
777 fillcnt
= p_repl_lines
;
779 } else if (!p_context
&& fillcnt
== 1) {
780 /* the first hunk was a null hunk with no context */
781 /* and we were expecting one line -- fix it up. */
782 while (filldst
< p_end
) {
783 p_line
[filldst
] = p_line
[filldst
+ 1];
784 p_char
[filldst
] = p_char
[filldst
+ 1];
785 p_len
[filldst
] = p_len
[filldst
+ 1];
789 repl_beginning
--; /* this doesn't need to be fixed */
792 p_first
++; /* do append rather than insert */
796 if (diff_type
== CONTEXT_DIFF
&&
797 (fillcnt
|| (p_first
> 1 && ptrn_copiable
> 2 * p_context
))) {
800 "(Fascinating--this is really a new-style context diff but without",
801 "the telltale extra asterisks on the *** line that usually indicate",
802 "the new style...)");
803 diff_type
= NEW_CONTEXT_DIFF
;
805 /* if there were omitted context lines, fill them in now */
807 p_bfake
= filldst
; /* remember where not to free() */
808 p_efake
= filldst
+ fillcnt
- 1;
809 while (fillcnt
-- > 0) {
810 while (fillsrc
<= p_end
&& p_char
[fillsrc
] != ' ')
813 fatal("replacement text or line numbers mangled in hunk at line %ld\n",
815 p_line
[filldst
] = p_line
[fillsrc
];
816 p_char
[filldst
] = p_char
[fillsrc
];
817 p_len
[filldst
] = p_len
[fillsrc
];
821 while (fillsrc
<= p_end
&& fillsrc
!= repl_beginning
&&
822 p_char
[fillsrc
] != ' ')
826 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
827 fillsrc
, filldst
, repl_beginning
, p_end
+ 1);
829 if (fillsrc
!= p_end
+ 1 && fillsrc
!= repl_beginning
)
831 if (filldst
!= p_end
+ 1 && filldst
!= repl_beginning
)
834 if (p_line
[p_end
] != NULL
) {
835 if (remove_special_line()) {
837 (p_line
[p_end
])[p_len
[p_end
]] = 0;
840 } else if (diff_type
== UNI_DIFF
) {
841 LINENUM fillold
; /* index of old lines */
842 LINENUM fillnew
; /* index of new lines */
845 line_beginning
= ftell(pfp
); /* file pos of the current line */
846 ret
= pgets(buf
, buf_len
, pfp
);
848 if (ret
== NULL
|| strnNE(buf
, "@@ -", 4)) {
849 next_intuit_at(line_beginning
, p_input_line
);
855 p_first
= (LINENUM
) atol(s
);
856 while (isdigit((unsigned char)*s
))
859 p_ptrn_lines
= (LINENUM
) atol(++s
);
860 while (isdigit((unsigned char)*s
))
866 if (*s
!= '+' || !*++s
)
868 p_newfirst
= (LINENUM
) atol(s
);
869 while (isdigit((unsigned char)*s
))
872 p_repl_lines
= (LINENUM
) atol(++s
);
873 while (isdigit((unsigned char)*s
))
882 p_first
++; /* do append rather than insert */
883 p_max
= p_ptrn_lines
+ p_repl_lines
+ 1;
884 while (p_max
>= hunkmax
)
887 fillnew
= fillold
+ p_ptrn_lines
;
888 p_end
= fillnew
+ p_repl_lines
;
889 snprintf(buf
, buf_len
, "*** %ld,%ld ****\n", p_first
,
890 p_first
+ p_ptrn_lines
- 1);
891 p_line
[0] = savestr(buf
);
897 snprintf(buf
, buf_len
, "--- %ld,%ld ----\n", p_newfirst
,
898 p_newfirst
+ p_repl_lines
- 1);
899 p_line
[fillnew
] = savestr(buf
);
904 p_char
[fillnew
++] = '=';
907 p_hunk_beg
= p_input_line
+ 1;
908 while (fillold
<= p_ptrn_lines
|| fillnew
<= p_end
) {
909 line_beginning
= ftell(pfp
);
910 ret
= pgets(buf
, buf_len
, pfp
);
913 if (p_max
- fillnew
< 3) {
914 /* assume blank lines got chopped */
915 strlcpy(buf
, " \n", buf_len
);
917 fatal("unexpected end of file in patch\n");
920 if (*buf
== '\t' || *buf
== '\n') {
921 ch
= ' '; /* assume the space got eaten */
925 s
= savestr(buf
+ 1);
928 while (--fillnew
> p_ptrn_lines
)
929 free(p_line
[fillnew
]);
935 if (fillold
> p_ptrn_lines
) {
940 p_char
[fillold
] = ch
;
942 p_len
[fillold
++] = strlen(s
);
943 if (fillold
> p_ptrn_lines
) {
944 if (remove_special_line()) {
945 p_len
[fillold
- 1] -= 1;
946 s
[p_len
[fillold
- 1]] = 0;
954 if (fillold
> p_ptrn_lines
) {
956 while (--fillnew
> p_ptrn_lines
)
957 free(p_line
[fillnew
]);
962 p_char
[fillold
] = ch
;
964 p_len
[fillold
++] = strlen(s
);
967 while (--fillnew
> p_ptrn_lines
)
968 free(p_line
[fillnew
]);
972 if (fillold
> p_ptrn_lines
) {
973 if (remove_special_line()) {
974 p_len
[fillold
- 1] -= 1;
975 s
[p_len
[fillold
- 1]] = 0;
980 if (fillnew
> p_end
) {
982 while (--fillnew
> p_ptrn_lines
)
983 free(p_line
[fillnew
]);
987 p_char
[fillnew
] = ch
;
989 p_len
[fillnew
++] = strlen(s
);
990 if (fillold
> p_ptrn_lines
) {
991 if (remove_special_line()) {
992 p_len
[fillnew
- 1] -= 1;
993 s
[p_len
[fillnew
- 1]] = 0;
1001 if (ch
!= ' ' && context
> 0) {
1002 if (context
< p_context
)
1003 p_context
= context
;
1007 } else { /* normal diff--fake it up */
1012 line_beginning
= ftell(pfp
);
1014 ret
= pgets(buf
, buf_len
, pfp
);
1016 if (ret
== NULL
|| !isdigit((unsigned char)*buf
)) {
1017 next_intuit_at(line_beginning
, p_input_line
);
1020 p_first
= (LINENUM
) atol(buf
);
1021 for (s
= buf
; isdigit((unsigned char)*s
); s
++)
1024 p_ptrn_lines
= (LINENUM
) atol(++s
) - p_first
+ 1;
1025 while (isdigit((unsigned char)*s
))
1028 p_ptrn_lines
= (*s
!= 'a');
1030 if (hunk_type
== 'a')
1031 p_first
++; /* do append rather than insert */
1032 min
= (LINENUM
) atol(++s
);
1033 for (; isdigit((unsigned char)*s
); s
++)
1036 max
= (LINENUM
) atol(++s
);
1039 if (hunk_type
== 'd')
1041 p_end
= p_ptrn_lines
+ 1 + max
- min
+ 1;
1042 if (p_end
> MAXHUNKSIZE
)
1043 fatal("hunk too large (%ld lines) at line %ld: %s",
1044 p_end
, p_input_line
, buf
);
1045 while (p_end
>= hunkmax
)
1048 p_repl_lines
= max
- min
+ 1;
1049 snprintf(buf
, buf_len
, "*** %ld,%ld\n", p_first
,
1050 p_first
+ p_ptrn_lines
- 1);
1051 p_line
[0] = savestr(buf
);
1057 for (i
= 1; i
<= p_ptrn_lines
; i
++) {
1058 ret
= pgets(buf
, buf_len
, pfp
);
1061 fatal("unexpected end of file in patch at line %ld\n",
1064 fatal("< expected at line %ld of patch\n",
1066 p_line
[i
] = savestr(buf
+ 2);
1071 p_len
[i
] = strlen(p_line
[i
]);
1075 if (remove_special_line()) {
1077 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1079 if (hunk_type
== 'c') {
1080 ret
= pgets(buf
, buf_len
, pfp
);
1083 fatal("unexpected end of file in patch at line %ld\n",
1086 fatal("--- expected at line %ld of patch\n",
1089 snprintf(buf
, buf_len
, "--- %ld,%ld\n", min
, max
);
1090 p_line
[i
] = savestr(buf
);
1096 for (i
++; i
<= p_end
; i
++) {
1097 ret
= pgets(buf
, buf_len
, pfp
);
1100 fatal("unexpected end of file in patch at line %ld\n",
1103 fatal("> expected at line %ld of patch\n",
1105 p_line
[i
] = savestr(buf
+ 2);
1110 p_len
[i
] = strlen(p_line
[i
]);
1114 if (remove_special_line()) {
1116 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1119 if (reverse
) /* backwards patch? */
1121 say("Not enough memory to swap next hunk!\n");
1127 for (i
= 0; i
<= p_end
; i
++) {
1128 if (i
== p_ptrn_lines
)
1132 fprintf(stderr
, "%3d %c %c %s", i
, p_char
[i
],
1133 special
, p_line
[i
]);
1138 if (p_end
+ 1 < hunkmax
)/* paranoia reigns supreme... */
1139 p_char
[p_end
+ 1] = '^'; /* add a stopper for apply_hunk */
1144 * Input a line from the patch file, worrying about indentation.
1147 pgets(char *bf
, int sz
, FILE *fp
)
1149 char *s
, *ret
= fgets(bf
, sz
, fp
);
1152 if (p_indent
&& ret
!= NULL
) {
1154 indent
< p_indent
&& (*s
== ' ' || *s
== '\t' || *s
== 'X');
1157 indent
+= 8 - (indent
% 7);
1161 if (buf
!= s
&& strlcpy(buf
, s
, buf_len
) >= buf_len
)
1162 fatal("buffer too small in pgets()\n");
1168 * Reverse the old and new portions of the current hunk.
1173 char **tp_line
; /* the text of the hunk */
1174 short *tp_len
; /* length of each line */
1175 char *tp_char
; /* +, -, and ! */
1178 bool blankline
= false;
1182 p_first
= p_newfirst
;
1185 /* make a scratch copy */
1190 p_line
= NULL
; /* force set_hunkmax to allocate again */
1194 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
) {
1202 return false; /* not enough memory to swap hunk! */
1204 /* now turn the new into the old */
1206 i
= p_ptrn_lines
+ 1;
1207 if (tp_char
[i
] == '\n') { /* account for possible blank line */
1211 if (p_efake
>= 0) { /* fix non-freeable ptr range */
1219 for (n
= 0; i
<= p_end
; i
++, n
++) {
1220 p_line
[n
] = tp_line
[i
];
1221 p_char
[n
] = tp_char
[i
];
1222 if (p_char
[n
] == '+')
1224 p_len
[n
] = tp_len
[i
];
1227 i
= p_ptrn_lines
+ 1;
1228 p_line
[n
] = tp_line
[i
];
1229 p_char
[n
] = tp_char
[i
];
1230 p_len
[n
] = tp_len
[i
];
1233 if (p_char
[0] != '=')
1234 fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1235 p_input_line
, p_char
[0]);
1237 for (s
= p_line
[0]; *s
; s
++)
1241 /* now turn the old into the new */
1243 if (p_char
[0] != '*')
1244 fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
1245 p_input_line
, p_char
[0]);
1247 for (s
= tp_line
[0]; *s
; s
++)
1250 for (i
= 0; n
<= p_end
; i
++, n
++) {
1251 p_line
[n
] = tp_line
[i
];
1252 p_char
[n
] = tp_char
[i
];
1253 if (p_char
[n
] == '-')
1255 p_len
[n
] = tp_len
[i
];
1258 if (i
!= p_ptrn_lines
+ 1)
1259 fatal("Malformed patch at line %ld: expected %ld lines, "
1261 p_input_line
, p_ptrn_lines
+ 1, i
);
1264 p_ptrn_lines
= p_repl_lines
;
1275 * Return the specified line position in the old file of the old context.
1284 * Return the number of lines of old context.
1287 pch_ptrn_lines(void)
1289 return p_ptrn_lines
;
1293 * Return the probable line position in the new file of the first line.
1302 * Return the number of lines in the replacement text including context.
1305 pch_repl_lines(void)
1307 return p_repl_lines
;
1311 * Return the number of lines in the whole hunk.
1320 * Return the number of context lines before the first changed line.
1329 * Return the length of a particular patch line.
1332 pch_line_len(LINENUM line
)
1338 * Return the control character (+, -, *, !, etc) for a patch line.
1341 pch_char(LINENUM line
)
1343 return p_char
[line
];
1347 * Return a pointer to a particular patch line.
1350 pfetch(LINENUM line
)
1352 return p_line
[line
];
1356 * Return where in the patch file this hunk began, for error messages.
1365 * Apply an ed script by feeding ed itself.
1371 long beginning_of_this_line
;
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 */