]>
git.cameronkatri.com Git - apple_cmds.git/blob - patch_cmds/patch/pch.c
1 /* $OpenBSD: pch.c,v 1.38 2009/10/27 23:59:41 deraadt Exp $ */
4 * patch - a program to apply diffs to original files
6 * Copyright 1986, Larry Wall
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following condition is met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this condition and the following disclaimer.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * -C option added in 1998, original code by Marc Espie, based on FreeBSD
29 #include <sys/types.h>
43 #include "pathnames.h"
45 /* Patch (diff listing) abstract type. */
47 static long p_filesize
; /* size of the patch file */
48 static LINENUM p_first
; /* 1st line number */
49 static LINENUM p_newfirst
; /* 1st line number of replacement */
50 static LINENUM p_ptrn_lines
; /* # lines in pattern */
51 static LINENUM p_repl_lines
; /* # lines in replacement text */
52 static LINENUM p_end
= -1; /* last line in hunk */
53 static LINENUM p_max
; /* max allowed value of p_end */
54 static LINENUM p_context
= 3; /* # of context lines */
55 static LINENUM p_input_line
= 0; /* current line # from patch file */
56 static char **p_line
= NULL
;/* the text of the hunk */
57 static short *p_len
= NULL
; /* length of each line */
58 static char *p_char
= NULL
; /* +, -, and ! */
59 static int hunkmax
= INITHUNKMAX
; /* size of above arrays to begin with */
60 static int p_indent
; /* indent to patch */
61 static LINENUM p_base
; /* where to intuit this time */
62 static LINENUM p_bline
; /* line # of p_base */
63 static LINENUM p_start
; /* where intuit found a patch */
64 static LINENUM p_sline
; /* and the line number for it */
65 static LINENUM p_hunk_beg
; /* line number of current hunk */
66 static LINENUM p_efake
= -1; /* end of faked up lines--don't free */
67 static LINENUM p_bfake
= -1; /* beg of faked up lines */
68 static FILE *pfp
= NULL
; /* patch file pointer */
69 static char *bestguess
= NULL
; /* guess at correct filename */
71 static void grow_hunkmax(void);
72 static int intuit_diff_type(void);
73 static void next_intuit_at(LINENUM
, LINENUM
);
74 static void skip_to(LINENUM
, LINENUM
);
75 static char *pgets(char *, int, FILE *);
76 static char *best_name(const struct file_name
*, bool);
77 static char *posix_name(const struct file_name
*, bool);
78 static size_t num_components(const char *);
81 * Prepare to look for the next patch in the patch file.
90 p_end
= (LINENUM
) - 1;
96 * Open the patch file at the beginning of time.
99 open_patch_file(const char *filename
)
101 struct stat filestat
;
103 if (filename
== NULL
|| *filename
== '\0' || strEQ(filename
, "-")) {
104 pfp
= fopen(TMPPATNAME
, "w");
106 pfatal("can't create %s", TMPPATNAME
);
107 while (fgets(buf
, sizeof buf
, stdin
) != NULL
)
110 filename
= TMPPATNAME
;
112 pfp
= fopen(filename
, "r");
114 pfatal("patch file %s not found", filename
);
115 fstat(fileno(pfp
), &filestat
);
116 p_filesize
= filestat
.st_size
;
117 next_intuit_at(0L, 1L); /* start at the beginning */
122 * Make sure our dynamically realloced tables are malloced to begin with.
128 p_line
= calloc((size_t) hunkmax
, sizeof(char *));
130 p_len
= calloc((size_t) hunkmax
, sizeof(short));
132 p_char
= calloc((size_t) hunkmax
, sizeof(char));
136 * Enlarge the arrays containing the current hunk of patch.
146 new_hunkmax
= hunkmax
* 2;
148 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
)
149 fatal("Internal memory allocation error\n");
151 new_p_line
= realloc(p_line
, new_hunkmax
* sizeof(char *));
152 if (new_p_line
== NULL
)
155 new_p_len
= realloc(p_len
, new_hunkmax
* sizeof(short));
156 if (new_p_len
== NULL
)
159 new_p_char
= realloc(p_char
, new_hunkmax
* sizeof(char));
160 if (new_p_char
== NULL
)
167 if (p_line
!= NULL
&& p_len
!= NULL
&& p_char
!= NULL
) {
168 hunkmax
= new_hunkmax
;
173 fatal("out of memory\n");
174 out_of_mem
= true; /* whatever is null will be allocated again */
175 /* from within plan_a(), of all places */
178 /* True if the remainder of the patch file contains a diff of some sort. */
181 there_is_another_patch(void)
185 if (p_base
!= 0L && p_base
>= p_filesize
) {
192 diff_type
= intuit_diff_type();
196 say(" Ignoring the trailing garbage.\ndone\n");
198 say(" I can't seem to find a patch in there anywhere.\n");
202 say(" %sooks like %s to me...\n",
203 (p_base
== 0L ? "L" : "The next patch l"),
204 diff_type
== UNI_DIFF
? "a unified diff" :
205 diff_type
== CONTEXT_DIFF
? "a context diff" :
206 diff_type
== NEW_CONTEXT_DIFF
? "a new-style context diff" :
207 diff_type
== NORMAL_DIFF
? "a normal diff" :
209 if (p_indent
&& verbose
)
210 say("(Patch is indented %d space%s.)\n", p_indent
,
211 p_indent
== 1 ? "" : "s");
212 skip_to(p_start
, p_sline
);
213 while (filearg
[0] == NULL
) {
214 if (force
|| batch
) {
215 say("No file to patch. Skipping...\n");
216 filearg
[0] = savestr(bestguess
);
217 skip_rest_of_patch
= true;
220 ask("File to patch: ");
223 bestguess
= savestr(buf
);
224 filearg
[0] = fetchname(buf
, &exists
, 0);
227 ask("No file found--skip this patch? [n] ");
231 say("Skipping patch...\n");
233 filearg
[0] = fetchname(bestguess
, &exists
, 0);
234 skip_rest_of_patch
= true;
241 /* Determine what kind of diff is in the remaining part of the patch file. */
244 intuit_diff_type(void)
246 long this_line
= 0, previous_line
;
247 long first_command_line
= -1;
248 LINENUM fcl_line
= -1;
249 bool last_line_was_command
= false, this_is_a_command
= false;
250 bool stars_last_line
= false, stars_this_line
= false;
253 struct file_name names
[MAX_FILE
];
255 memset(names
, 0, sizeof(names
));
256 ok_to_create_file
= false;
257 fseek(pfp
, p_base
, SEEK_SET
);
258 p_input_line
= p_bline
- 1;
260 previous_line
= this_line
;
261 last_line_was_command
= this_is_a_command
;
262 stars_last_line
= stars_this_line
;
263 this_line
= ftell(pfp
);
266 if (fgets(buf
, sizeof buf
, pfp
) == NULL
) {
267 if (first_command_line
>= 0L) {
268 /* nothing but deletes!? */
269 p_start
= first_command_line
;
275 p_sline
= p_input_line
;
280 for (s
= buf
; *s
== ' ' || *s
== '\t' || *s
== 'X'; s
++) {
282 indent
+= 8 - (indent
% 8);
286 for (t
= s
; isdigit(*t
) || *t
== ','; t
++)
288 this_is_a_command
= (isdigit(*s
) &&
289 (*t
== 'd' || *t
== 'c' || *t
== 'a'));
290 if (first_command_line
< 0L && this_is_a_command
) {
291 first_command_line
= this_line
;
292 fcl_line
= p_input_line
;
293 p_indent
= indent
; /* assume this for now */
295 if (!stars_last_line
&& strnEQ(s
, "*** ", 4))
296 names
[OLD_FILE
].path
= fetchname(s
+ 4,
297 &names
[OLD_FILE
].exists
, strippath
);
298 else if (strnEQ(s
, "--- ", 4))
299 names
[NEW_FILE
].path
= fetchname(s
+ 4,
300 &names
[NEW_FILE
].exists
, strippath
);
301 else if (strnEQ(s
, "+++ ", 4))
302 /* pretend it is the old name */
303 names
[OLD_FILE
].path
= fetchname(s
+ 4,
304 &names
[OLD_FILE
].exists
, strippath
);
305 else if (strnEQ(s
, "Index:", 6))
306 names
[INDEX_FILE
].path
= fetchname(s
+ 6,
307 &names
[INDEX_FILE
].exists
, strippath
);
308 else if (strnEQ(s
, "Prereq:", 7)) {
309 for (t
= s
+ 7; isspace(*t
); t
++)
311 revision
= savestr(t
);
312 for (t
= revision
; *t
&& !isspace(*t
); t
++)
315 if (*revision
== '\0') {
320 if ((!diff_type
|| diff_type
== ED_DIFF
) &&
321 first_command_line
>= 0L &&
324 p_start
= first_command_line
;
329 if ((!diff_type
|| diff_type
== UNI_DIFF
) && strnEQ(s
, "@@ -", 4)) {
330 if (strnEQ(s
+ 4, "0,0", 3))
331 ok_to_create_file
= true;
334 p_sline
= p_input_line
;
338 stars_this_line
= strnEQ(s
, "********", 8);
339 if ((!diff_type
|| diff_type
== CONTEXT_DIFF
) && stars_last_line
&&
340 strnEQ(s
, "*** ", 4)) {
341 if (atol(s
+ 4) == 0)
342 ok_to_create_file
= true;
344 * If this is a new context diff the character just
345 * before the newline is a '*'.
350 p_start
= previous_line
;
351 p_sline
= p_input_line
- 1;
352 retval
= (*(s
- 1) == '*' ? NEW_CONTEXT_DIFF
: CONTEXT_DIFF
);
355 if ((!diff_type
|| diff_type
== NORMAL_DIFF
) &&
356 last_line_was_command
&&
357 (strnEQ(s
, "< ", 2) || strnEQ(s
, "> ", 2))) {
358 p_start
= previous_line
;
359 p_sline
= p_input_line
- 1;
361 retval
= NORMAL_DIFF
;
366 if (retval
== UNI_DIFF
) {
367 /* unswap old and new */
368 struct file_name tmp
= names
[OLD_FILE
];
369 names
[OLD_FILE
] = names
[NEW_FILE
];
370 names
[NEW_FILE
] = tmp
;
372 if (filearg
[0] == NULL
) {
374 filearg
[0] = posix_name(names
, ok_to_create_file
);
376 /* Ignore the Index: name for context diffs, like GNU */
377 if (names
[OLD_FILE
].path
!= NULL
||
378 names
[NEW_FILE
].path
!= NULL
) {
379 free(names
[INDEX_FILE
].path
);
380 names
[INDEX_FILE
].path
= NULL
;
382 filearg
[0] = best_name(names
, ok_to_create_file
);
388 if (filearg
[0] != NULL
)
389 bestguess
= savestr(filearg
[0]);
390 else if (!ok_to_create_file
) {
392 * We don't want to create a new file but we need a
393 * filename to set bestguess. Avoid setting filearg[0]
394 * so the file is not created automatically.
397 bestguess
= posix_name(names
, true);
399 bestguess
= best_name(names
, true);
401 free(names
[OLD_FILE
].path
);
402 free(names
[NEW_FILE
].path
);
403 free(names
[INDEX_FILE
].path
);
408 * Remember where this patch ends so we know where to start up again.
411 next_intuit_at(LINENUM file_pos
, LINENUM file_line
)
418 * Basically a verbose fseek() to the actual diff listing.
421 skip_to(LINENUM file_pos
, LINENUM file_line
)
425 if (p_base
> file_pos
)
426 fatal("Internal error: seek %ld>%ld\n", p_base
, file_pos
);
427 if (verbose
&& p_base
< file_pos
) {
428 fseek(pfp
, p_base
, SEEK_SET
);
429 say("The text leading up to this was:\n--------------------------\n");
430 while (ftell(pfp
) < file_pos
) {
431 ret
= fgets(buf
, sizeof buf
, pfp
);
433 fatal("Unexpected end of file\n");
436 say("--------------------------\n");
438 fseek(pfp
, file_pos
, SEEK_SET
);
439 p_input_line
= file_line
- 1;
442 /* Make this a function for better debugging. */
446 fatal("malformed patch at line %ld: %s", p_input_line
, buf
);
447 /* about as informative as "Syntax error" in C */
451 * True if the line has been discarded (i.e. it is a line saying
452 * "\ No newline at end of file".)
455 remove_special_line(void)
463 } while (c
!= EOF
&& c
!= '\n');
468 fseek(pfp
, -1L, SEEK_CUR
);
474 * True if there is more of the current diff listing to process.
479 long line_beginning
; /* file pos of the current line */
480 LINENUM repl_beginning
; /* index of --- line */
481 LINENUM fillcnt
; /* #lines of missing ptrn or repl */
482 LINENUM fillsrc
; /* index of first line to copy */
483 LINENUM filldst
; /* index of first missing line */
484 bool ptrn_spaces_eaten
; /* ptrn was slightly misformed */
485 bool repl_could_be_missing
; /* no + or ! lines in this hunk */
486 bool repl_missing
; /* we are now backtracking */
487 long repl_backtrack_position
; /* file pos of first repl line */
488 LINENUM repl_patch_line
; /* input line number for same */
489 LINENUM ptrn_copiable
; /* # of copiable lines in ptrn */
494 if (p_end
== p_efake
)
495 p_end
= p_bfake
; /* don't free twice */
502 p_max
= hunkmax
; /* gets reduced when --- found */
503 if (diff_type
== CONTEXT_DIFF
|| diff_type
== NEW_CONTEXT_DIFF
) {
504 line_beginning
= ftell(pfp
);
507 ptrn_spaces_eaten
= false;
508 repl_could_be_missing
= true;
509 repl_missing
= false;
510 repl_backtrack_position
= 0;
513 ret
= pgets(buf
, sizeof buf
, pfp
);
515 if (ret
== NULL
|| strnNE(buf
, "********", 8)) {
516 next_intuit_at(line_beginning
, p_input_line
);
520 p_hunk_beg
= p_input_line
+ 1;
521 while (p_end
< p_max
) {
522 line_beginning
= ftell(pfp
);
523 ret
= pgets(buf
, sizeof buf
, pfp
);
526 if (p_max
- p_end
< 4) {
527 /* assume blank lines got chopped */
528 strlcpy(buf
, " \n", sizeof buf
);
530 if (repl_beginning
&& repl_could_be_missing
) {
534 fatal("unexpected end of file in patch\n");
538 if (p_end
>= hunkmax
)
539 fatal("Internal error: hunk larger than hunk "
541 p_char
[p_end
] = *buf
;
542 p_line
[p_end
] = NULL
;
545 if (strnEQ(buf
, "********", 8)) {
546 if (repl_beginning
&& repl_could_be_missing
) {
550 fatal("unexpected end of hunk "
555 if (repl_beginning
&& repl_could_be_missing
) {
559 fatal("unexpected *** at line %ld: %s",
563 p_line
[p_end
] = savestr(buf
);
568 for (s
= buf
; *s
&& !isdigit(*s
); s
++)
572 if (strnEQ(s
, "0,0", 3))
573 memmove(s
, s
+ 2, strlen(s
+ 2) + 1);
574 p_first
= (LINENUM
) atol(s
);
578 for (; *s
&& !isdigit(*s
); s
++)
582 p_ptrn_lines
= ((LINENUM
) atol(s
)) - p_first
+ 1;
590 /* we need this much at least */
591 p_max
= p_ptrn_lines
+ 6;
592 while (p_max
>= hunkmax
)
598 if (repl_beginning
||
599 (p_end
!= p_ptrn_lines
+ 1 +
600 (p_char
[p_end
- 1] == '\n'))) {
603 * `old' lines were omitted;
604 * set up to fill them in
605 * from 'new' context lines.
607 p_end
= p_ptrn_lines
+ 1;
610 fillcnt
= p_ptrn_lines
;
612 if (repl_beginning
) {
613 if (repl_could_be_missing
) {
617 fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
618 p_input_line
, p_hunk_beg
+ repl_beginning
);
620 fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
621 (p_end
<= p_ptrn_lines
624 p_input_line
, p_hunk_beg
);
628 repl_beginning
= p_end
;
629 repl_backtrack_position
= ftell(pfp
);
630 repl_patch_line
= p_input_line
;
631 p_line
[p_end
] = savestr(buf
);
637 for (s
= buf
; *s
&& !isdigit(*s
); s
++)
641 p_newfirst
= (LINENUM
) atol(s
);
645 for (; *s
&& !isdigit(*s
); s
++)
649 p_repl_lines
= ((LINENUM
) atol(s
)) -
651 } else if (p_newfirst
)
657 p_max
= p_repl_lines
+ p_end
;
658 if (p_max
> MAXHUNKSIZE
)
659 fatal("hunk too large (%ld lines) at line %ld: %s",
660 p_max
, p_input_line
, buf
);
661 while (p_max
>= hunkmax
)
663 if (p_repl_lines
!= ptrn_copiable
&&
664 (p_context
!= 0 || p_repl_lines
!= 1))
665 repl_could_be_missing
= false;
671 repl_could_be_missing
= false;
673 if (buf
[1] == '\n' && canonicalize
)
674 strlcpy(buf
+ 1, " \n", sizeof buf
- 1);
675 if (!isspace(buf
[1]) && buf
[1] != '>' &&
677 repl_beginning
&& repl_could_be_missing
) {
682 if (context
< p_context
)
686 p_line
[p_end
] = savestr(buf
+ 2);
691 if (p_end
== p_ptrn_lines
) {
692 if (remove_special_line()) {
695 len
= strlen(p_line
[p_end
]) - 1;
696 (p_line
[p_end
])[len
] = 0;
701 case '\n': /* assume the 2 spaces got eaten */
702 if (repl_beginning
&& repl_could_be_missing
&&
703 (!ptrn_spaces_eaten
||
704 diff_type
== NEW_CONTEXT_DIFF
)) {
708 p_line
[p_end
] = savestr(buf
);
713 if (p_end
!= p_ptrn_lines
+ 1) {
714 ptrn_spaces_eaten
|= (repl_beginning
!= 0);
722 if (!isspace(buf
[1]) &&
723 repl_beginning
&& repl_could_be_missing
) {
730 p_line
[p_end
] = savestr(buf
+ 2);
737 if (repl_beginning
&& repl_could_be_missing
) {
743 /* set up p_len for strncmp() so we don't have to */
744 /* assume null termination */
746 p_len
[p_end
] = strlen(p_line
[p_end
]);
752 if (p_end
>= 0 && !repl_beginning
)
753 fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
757 /* reset state back to just after --- */
758 p_input_line
= repl_patch_line
;
759 for (p_end
--; p_end
> repl_beginning
; p_end
--)
761 fseek(pfp
, repl_backtrack_position
, SEEK_SET
);
763 /* redundant 'new' context lines were omitted - set */
764 /* up to fill them in from the old file context */
765 if (!p_context
&& p_repl_lines
== 1) {
770 filldst
= repl_beginning
+ 1;
771 fillcnt
= p_repl_lines
;
773 } else if (!p_context
&& fillcnt
== 1) {
774 /* the first hunk was a null hunk with no context */
775 /* and we were expecting one line -- fix it up. */
776 while (filldst
< p_end
) {
777 p_line
[filldst
] = p_line
[filldst
+ 1];
778 p_char
[filldst
] = p_char
[filldst
+ 1];
779 p_len
[filldst
] = p_len
[filldst
+ 1];
783 repl_beginning
--; /* this doesn't need to be fixed */
786 p_first
++; /* do append rather than insert */
790 if (diff_type
== CONTEXT_DIFF
&&
791 (fillcnt
|| (p_first
> 1 && ptrn_copiable
> 2 * p_context
))) {
794 "(Fascinating--this is really a new-style context diff but without",
795 "the telltale extra asterisks on the *** line that usually indicate",
796 "the new style...)");
797 diff_type
= NEW_CONTEXT_DIFF
;
799 /* if there were omitted context lines, fill them in now */
801 p_bfake
= filldst
; /* remember where not to free() */
802 p_efake
= filldst
+ fillcnt
- 1;
803 while (fillcnt
-- > 0) {
804 while (fillsrc
<= p_end
&& p_char
[fillsrc
] != ' ')
807 fatal("replacement text or line numbers mangled in hunk at line %ld\n",
809 p_line
[filldst
] = p_line
[fillsrc
];
810 p_char
[filldst
] = p_char
[fillsrc
];
811 p_len
[filldst
] = p_len
[fillsrc
];
815 while (fillsrc
<= p_end
&& fillsrc
!= repl_beginning
&&
816 p_char
[fillsrc
] != ' ')
820 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
821 fillsrc
, filldst
, repl_beginning
, p_end
+ 1);
823 if (fillsrc
!= p_end
+ 1 && fillsrc
!= repl_beginning
)
825 if (filldst
!= p_end
+ 1 && filldst
!= repl_beginning
)
828 if (p_line
[p_end
] != NULL
) {
829 if (remove_special_line()) {
831 (p_line
[p_end
])[p_len
[p_end
]] = 0;
834 } else if (diff_type
== UNI_DIFF
) {
835 long line_beginning
= ftell(pfp
); /* file pos of the current line */
836 LINENUM fillsrc
; /* index of old lines */
837 LINENUM filldst
; /* index of new lines */
840 ret
= pgets(buf
, sizeof buf
, pfp
);
842 if (ret
== NULL
|| strnNE(buf
, "@@ -", 4)) {
843 next_intuit_at(line_beginning
, p_input_line
);
849 p_first
= (LINENUM
) atol(s
);
853 p_ptrn_lines
= (LINENUM
) atol(++s
);
860 if (*s
!= '+' || !*++s
)
862 p_newfirst
= (LINENUM
) atol(s
);
866 p_repl_lines
= (LINENUM
) atol(++s
);
876 p_first
++; /* do append rather than insert */
877 p_max
= p_ptrn_lines
+ p_repl_lines
+ 1;
878 while (p_max
>= hunkmax
)
881 filldst
= fillsrc
+ p_ptrn_lines
;
882 p_end
= filldst
+ p_repl_lines
;
883 snprintf(buf
, sizeof buf
, "*** %ld,%ld ****\n", p_first
,
884 p_first
+ p_ptrn_lines
- 1);
885 p_line
[0] = savestr(buf
);
891 snprintf(buf
, sizeof buf
, "--- %ld,%ld ----\n", p_newfirst
,
892 p_newfirst
+ p_repl_lines
- 1);
893 p_line
[filldst
] = savestr(buf
);
898 p_char
[filldst
++] = '=';
901 p_hunk_beg
= p_input_line
+ 1;
902 while (fillsrc
<= p_ptrn_lines
|| filldst
<= p_end
) {
903 line_beginning
= ftell(pfp
);
904 ret
= pgets(buf
, sizeof buf
, pfp
);
907 if (p_max
- filldst
< 3) {
908 /* assume blank lines got chopped */
909 strlcpy(buf
, " \n", sizeof buf
);
911 fatal("unexpected end of file in patch\n");
914 if (*buf
== '\t' || *buf
== '\n') {
915 ch
= ' '; /* assume the space got eaten */
919 s
= savestr(buf
+ 1);
922 while (--filldst
> p_ptrn_lines
)
923 free(p_line
[filldst
]);
929 if (fillsrc
> p_ptrn_lines
) {
934 p_char
[fillsrc
] = ch
;
936 p_len
[fillsrc
++] = strlen(s
);
937 if (fillsrc
> p_ptrn_lines
) {
938 if (remove_special_line()) {
939 p_len
[fillsrc
- 1] -= 1;
940 s
[p_len
[fillsrc
- 1]] = 0;
948 if (fillsrc
> p_ptrn_lines
) {
950 while (--filldst
> p_ptrn_lines
)
951 free(p_line
[filldst
]);
956 p_char
[fillsrc
] = ch
;
958 p_len
[fillsrc
++] = strlen(s
);
961 while (--filldst
> p_ptrn_lines
)
962 free(p_line
[filldst
]);
966 if (fillsrc
> p_ptrn_lines
) {
967 if (remove_special_line()) {
968 p_len
[fillsrc
- 1] -= 1;
969 s
[p_len
[fillsrc
- 1]] = 0;
974 if (filldst
> p_end
) {
976 while (--filldst
> p_ptrn_lines
)
977 free(p_line
[filldst
]);
981 p_char
[filldst
] = ch
;
983 p_len
[filldst
++] = strlen(s
);
984 if (fillsrc
> p_ptrn_lines
) {
985 if (remove_special_line()) {
986 p_len
[filldst
- 1] -= 1;
987 s
[p_len
[filldst
- 1]] = 0;
995 if (ch
!= ' ' && context
> 0) {
996 if (context
< p_context
)
1001 } else { /* normal diff--fake it up */
1005 long line_beginning
= ftell(pfp
);
1008 ret
= pgets(buf
, sizeof buf
, pfp
);
1010 if (ret
== NULL
|| !isdigit(*buf
)) {
1011 next_intuit_at(line_beginning
, p_input_line
);
1014 p_first
= (LINENUM
) atol(buf
);
1015 for (s
= buf
; isdigit(*s
); s
++)
1018 p_ptrn_lines
= (LINENUM
) atol(++s
) - p_first
+ 1;
1022 p_ptrn_lines
= (*s
!= 'a');
1024 if (hunk_type
== 'a')
1025 p_first
++; /* do append rather than insert */
1026 min
= (LINENUM
) atol(++s
);
1027 for (; isdigit(*s
); s
++)
1030 max
= (LINENUM
) atol(++s
);
1033 if (hunk_type
== 'd')
1035 p_end
= p_ptrn_lines
+ 1 + max
- min
+ 1;
1036 if (p_end
> MAXHUNKSIZE
)
1037 fatal("hunk too large (%ld lines) at line %ld: %s",
1038 p_end
, p_input_line
, buf
);
1039 while (p_end
>= hunkmax
)
1042 p_repl_lines
= max
- min
+ 1;
1043 snprintf(buf
, sizeof buf
, "*** %ld,%ld\n", p_first
,
1044 p_first
+ p_ptrn_lines
- 1);
1045 p_line
[0] = savestr(buf
);
1051 for (i
= 1; i
<= p_ptrn_lines
; i
++) {
1052 ret
= pgets(buf
, sizeof buf
, pfp
);
1055 fatal("unexpected end of file in patch at line %ld\n",
1058 fatal("< expected at line %ld of patch\n",
1060 p_line
[i
] = savestr(buf
+ 2);
1065 p_len
[i
] = strlen(p_line
[i
]);
1069 if (remove_special_line()) {
1071 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1073 if (hunk_type
== 'c') {
1074 ret
= pgets(buf
, sizeof buf
, pfp
);
1077 fatal("unexpected end of file in patch at line %ld\n",
1080 fatal("--- expected at line %ld of patch\n",
1083 snprintf(buf
, sizeof(buf
), "--- %ld,%ld\n", min
, max
);
1084 p_line
[i
] = savestr(buf
);
1090 for (i
++; i
<= p_end
; i
++) {
1091 ret
= pgets(buf
, sizeof buf
, pfp
);
1094 fatal("unexpected end of file in patch at line %ld\n",
1097 fatal("> expected at line %ld of patch\n",
1099 p_line
[i
] = savestr(buf
+ 2);
1104 p_len
[i
] = strlen(p_line
[i
]);
1108 if (remove_special_line()) {
1110 (p_line
[i
- 1])[p_len
[i
- 1]] = 0;
1113 if (reverse
) /* backwards patch? */
1115 say("Not enough memory to swap next hunk!\n");
1121 for (i
= 0; i
<= p_end
; i
++) {
1122 if (i
== p_ptrn_lines
)
1126 fprintf(stderr
, "%3d %c %c %s", i
, p_char
[i
],
1127 special
, p_line
[i
]);
1132 if (p_end
+ 1 < hunkmax
)/* paranoia reigns supreme... */
1133 p_char
[p_end
+ 1] = '^'; /* add a stopper for apply_hunk */
1138 * Input a line from the patch file, worrying about indentation.
1141 pgets(char *bf
, int sz
, FILE *fp
)
1143 char *s
, *ret
= fgets(bf
, sz
, fp
);
1146 if (p_indent
&& ret
!= NULL
) {
1148 indent
< p_indent
&& (*s
== ' ' || *s
== '\t' || *s
== 'X');
1151 indent
+= 8 - (indent
% 7);
1155 if (buf
!= s
&& strlcpy(buf
, s
, sizeof(buf
)) >= sizeof(buf
))
1156 fatal("buffer too small in pgets()\n");
1162 * Reverse the old and new portions of the current hunk.
1167 char **tp_line
; /* the text of the hunk */
1168 short *tp_len
; /* length of each line */
1169 char *tp_char
; /* +, -, and ! */
1172 bool blankline
= false;
1176 p_first
= p_newfirst
;
1179 /* make a scratch copy */
1184 p_line
= NULL
; /* force set_hunkmax to allocate again */
1188 if (p_line
== NULL
|| p_len
== NULL
|| p_char
== NULL
) {
1196 return false; /* not enough memory to swap hunk! */
1198 /* now turn the new into the old */
1200 i
= p_ptrn_lines
+ 1;
1201 if (tp_char
[i
] == '\n') { /* account for possible blank line */
1205 if (p_efake
>= 0) { /* fix non-freeable ptr range */
1213 for (n
= 0; i
<= p_end
; i
++, n
++) {
1214 p_line
[n
] = tp_line
[i
];
1215 p_char
[n
] = tp_char
[i
];
1216 if (p_char
[n
] == '+')
1218 p_len
[n
] = tp_len
[i
];
1221 i
= p_ptrn_lines
+ 1;
1222 p_line
[n
] = tp_line
[i
];
1223 p_char
[n
] = tp_char
[i
];
1224 p_len
[n
] = tp_len
[i
];
1227 if (p_char
[0] != '=')
1228 fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1229 p_input_line
, p_char
[0]);
1231 for (s
= p_line
[0]; *s
; s
++)
1235 /* now turn the old into the new */
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
= tp_line
[0]; *s
; s
++)
1244 for (i
= 0; n
<= p_end
; i
++, n
++) {
1245 p_line
[n
] = tp_line
[i
];
1246 p_char
[n
] = tp_char
[i
];
1247 if (p_char
[n
] == '-')
1249 p_len
[n
] = tp_len
[i
];
1252 if (i
!= p_ptrn_lines
+ 1)
1253 fatal("Malformed patch at line %ld: expected %ld lines, "
1255 p_input_line
, p_ptrn_lines
+ 1, i
);
1258 p_ptrn_lines
= p_repl_lines
;
1269 * Return the specified line position in the old file of the old context.
1278 * Return the number of lines of old context.
1281 pch_ptrn_lines(void)
1283 return p_ptrn_lines
;
1287 * Return the probable line position in the new file of the first line.
1296 * Return the number of lines in the replacement text including context.
1299 pch_repl_lines(void)
1301 return p_repl_lines
;
1305 * Return the number of lines in the whole hunk.
1314 * Return the number of context lines before the first changed line.
1323 * Return the length of a particular patch line.
1326 pch_line_len(LINENUM line
)
1332 * Return the control character (+, -, *, !, etc) for a patch line.
1335 pch_char(LINENUM line
)
1337 return p_char
[line
];
1341 * Return a pointer to a particular patch line.
1344 pfetch(LINENUM line
)
1346 return p_line
[line
];
1350 * Return where in the patch file this hunk began, for error messages.
1359 * Apply an ed script by feeding ed itself.
1365 long beginning_of_this_line
;
1366 FILE *pipefp
= NULL
;
1368 if (!skip_rest_of_patch
) {
1369 if (copy_file(filearg
[0], TMPOUTNAME
) < 0) {
1371 fatal("can't create temp file %s", TMPOUTNAME
);
1373 snprintf(buf
, sizeof buf
, "%s%s%s", _PATH_ED
,
1374 verbose
? " " : " -s ", TMPOUTNAME
);
1375 pipefp
= popen(buf
, "w");
1378 beginning_of_this_line
= ftell(pfp
);
1379 if (pgets(buf
, sizeof buf
, pfp
) == NULL
) {
1380 next_intuit_at(beginning_of_this_line
, p_input_line
);
1384 for (t
= buf
; isdigit(*t
) || *t
== ','; t
++)
1386 /* POSIX defines allowed commands as {a,c,d,i,s} */
1387 if (isdigit(*buf
) && (*t
== 'a' || *t
== 'c' || *t
== 'd' ||
1388 *t
== 'i' || *t
== 's')) {
1392 while (pgets(buf
, sizeof buf
, pfp
) != NULL
) {
1396 if (strEQ(buf
, ".\n"))
1401 next_intuit_at(beginning_of_this_line
, p_input_line
);
1407 fprintf(pipefp
, "w\n");
1408 fprintf(pipefp
, "q\n");
1413 if (move_file(TMPOUTNAME
, outname
) < 0) {
1415 chmod(TMPOUTNAME
, filemode
);
1417 chmod(outname
, filemode
);
1423 * Choose the name of the file to be patched based on POSIX rules.
1424 * NOTE: the POSIX rules are amazingly stupid and we only follow them
1425 * if the user specified --posix or set POSIXLY_CORRECT.
1428 posix_name(const struct file_name
*names
, bool assume_exists
)
1434 * POSIX states that the filename will be chosen from one
1435 * of the old, new and index names (in that order) if
1436 * the file exists relative to CWD after -p stripping.
1438 for (i
= 0; i
< MAX_FILE
; i
++) {
1439 if (names
[i
].path
!= NULL
&& names
[i
].exists
) {
1440 path
= names
[i
].path
;
1444 if (path
== NULL
&& !assume_exists
) {
1446 * No files found, look for something we can checkout from
1447 * RCS/SCCS dirs. Same order as above.
1449 for (i
= 0; i
< MAX_FILE
; i
++) {
1450 if (names
[i
].path
!= NULL
&&
1451 (path
= checked_in(names
[i
].path
)) != NULL
)
1455 * Still no match? Check to see if the diff could be creating
1458 if (path
== NULL
&& ok_to_create_file
&&
1459 names
[NEW_FILE
].path
!= NULL
)
1460 path
= names
[NEW_FILE
].path
;
1463 return path
? savestr(path
) : NULL
;
1467 * Choose the name of the file to be patched based the "best" one
1471 best_name(const struct file_name
*names
, bool assume_exists
)
1473 size_t min_components
, min_baselen
, min_len
, tmp
;
1478 * The "best" name is the one with the fewest number of path
1479 * components, the shortest basename length, and the shortest
1480 * overall length (in that order). We only use the Index: file
1481 * if neither of the old or new files could be intuited from
1484 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1485 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1486 if (names
[i
].path
== NULL
||
1487 (!names
[i
].exists
&& !assume_exists
))
1489 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1491 min_components
= tmp
;
1492 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1495 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1498 best
= names
[i
].path
;
1502 * No files found, look for something we can checkout from
1503 * RCS/SCCS dirs. Logic is identical to that above...
1505 min_components
= min_baselen
= min_len
= SIZE_MAX
;
1506 for (i
= INDEX_FILE
; i
>= OLD_FILE
; i
--) {
1507 if (names
[i
].path
== NULL
||
1508 checked_in(names
[i
].path
) == NULL
)
1510 if ((tmp
= num_components(names
[i
].path
)) > min_components
)
1512 min_components
= tmp
;
1513 if ((tmp
= strlen(basename(names
[i
].path
))) > min_baselen
)
1516 if ((tmp
= strlen(names
[i
].path
)) > min_len
)
1519 best
= names
[i
].path
;
1522 * Still no match? Check to see if the diff could be creating
1525 if (best
== NULL
&& ok_to_create_file
&&
1526 names
[NEW_FILE
].path
!= NULL
)
1527 best
= names
[NEW_FILE
].path
;
1530 return best
? savestr(best
) : NULL
;
1534 num_components(const char *path
)
1539 for (n
= 0, cp
= path
; (cp
= strchr(cp
, '/')) != NULL
; n
++, cp
++) {
1541 cp
++; /* skip consecutive slashes */