1 /* $FreeBSD: src/gnu/usr.bin/patch/pch.c,v 1.16.2.3 2002/04/30 20:40:02 gad Exp $
2 /* $DragonFly: src/gnu/usr.bin/patch/Attic/pch.c,v 1.2 2003/06/17 04:25:47 dillon Exp $
5 * Revision 2.0.2.0 90/05/01 22:17:51 davison
6 * patch12u: unidiff support added
8 * Revision 2.0.1.7 88/06/03 15:13:28 lwall
9 * patch10: Can now find patches in shar scripts.
10 * patch10: Hunks that swapped and then swapped back could core dump.
12 * Revision 2.0.1.6 87/06/04 16:18:13 lwall
13 * pch_swap didn't swap p_bfake and p_efake.
15 * Revision 2.0.1.5 87/01/30 22:47:42 lwall
16 * Improved responses to mangled patches.
18 * Revision 2.0.1.4 87/01/05 16:59:53 lwall
19 * New-style context diffs caused double call to free().
21 * Revision 2.0.1.3 86/11/14 10:08:33 lwall
22 * Fixed problem where a long pattern wouldn't grow the hunk.
23 * Also restored p_input_line when backtracking so error messages are right.
25 * Revision 2.0.1.2 86/11/03 17:49:52 lwall
26 * New-style delete triggers spurious assertion error.
28 * Revision 2.0.1.1 86/10/29 15:52:08 lwall
29 * Could falsely report new-style context diff.
31 * Revision 2.0 86/09/17 15:39:37 lwall
32 * Baseline for netwide release.
42 /* Patch (diff listing) abstract type. */
44 static long p_filesize; /* size of the patch file */
45 static LINENUM p_first; /* 1st line number */
46 static LINENUM p_newfirst; /* 1st line number of replacement */
47 static LINENUM p_ptrn_lines; /* # lines in pattern */
48 static LINENUM p_repl_lines; /* # lines in replacement text */
49 static LINENUM p_end = -1; /* last line in hunk */
50 static LINENUM p_max; /* max allowed value of p_end */
51 static LINENUM p_context = 3; /* # of context lines */
52 static LINENUM p_input_line = 0; /* current line # from patch file */
53 static char **p_line = Null(char**); /* the text of the hunk */
54 static short *p_len = Null(short*); /* length of each line */
55 static char *p_Char = Nullch; /* +, -, and ! */
56 static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */
57 static int p_indent; /* indent to patch */
58 static LINENUM p_base; /* where to intuit this time */
59 static LINENUM p_bline; /* line # of p_base */
60 static LINENUM p_start; /* where intuit found a patch */
61 static LINENUM p_sline; /* and the line number for it */
62 static LINENUM p_hunk_beg; /* line number of current hunk */
63 static LINENUM p_efake = -1; /* end of faked up lines--don't free */
64 static LINENUM p_bfake = -1; /* beg of faked up lines */
67 * Prepare to look for the next patch in the patch file.
74 p_ptrn_lines = Nulline;
75 p_repl_lines = Nulline;
81 /* Open the patch file at the beginning of time. */
84 open_patch_file(char *filename)
86 if (filename == Nullch || !*filename || strEQ(filename, "-")) {
87 pfp = fopen(TMPPATNAME, "w");
89 pfatal2("can't create %s", TMPPATNAME);
90 while (fgets(buf, sizeof buf, stdin) != Nullch)
93 filename = TMPPATNAME;
95 pfp = fopen(filename, "r");
97 pfatal2("patch file %s not found", filename);
98 Fstat(fileno(pfp), &filestat);
99 p_filesize = filestat.st_size;
100 next_intuit_at(0L,1L); /* start at the beginning */
104 /* Make sure our dynamically realloced tables are malloced to begin with. */
110 if (p_line == Null(char**))
111 p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
112 if (p_len == Null(short*))
113 p_len = (short*) malloc((MEM)hunkmax * sizeof(short));
115 if (p_Char == Nullch)
116 p_Char = (char*) malloc((MEM)hunkmax * sizeof(char));
119 /* Enlarge the arrays containing the current hunk of patch. */
126 * Note that on most systems, only the p_line array ever gets fresh memory
127 * since p_len can move into p_line's old space, and p_Char can move into
128 * p_len's old space. Not on PDP-11's however. But it doesn't matter.
130 assert(p_line != Null(char**) && p_len != Null(short*) && p_Char != Nullch);
132 p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
133 p_len = (short*) realloc((char*)p_len, (MEM)hunkmax * sizeof(short));
134 p_Char = (char*) realloc((char*)p_Char, (MEM)hunkmax * sizeof(char));
136 if (p_line != Null(char**) && p_len != Null(short*) && p_Char != Nullch)
139 fatal1("out of memory\n");
140 out_of_mem = TRUE; /* whatever is null will be allocated again */
141 /* from within plan_a(), of all places */
145 * True if the remainder of the patch file contains a diff of some sort.
148 there_is_another_patch(void)
150 if (p_base != 0L && p_base >= p_filesize) {
157 diff_type = intuit_diff_type();
161 say1(" Ignoring the trailing garbage.\ndone\n");
164 say1(" I can't seem to find a patch in there anywhere.\n");
168 say3(" %sooks like %s to me...\n",
169 (p_base == 0L ? "L" : "The next patch l"),
170 diff_type == UNI_DIFF ? "a unified diff" :
171 diff_type == CONTEXT_DIFF ? "a context diff" :
172 diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
173 diff_type == NORMAL_DIFF ? "a normal diff" :
175 if (p_indent && verbose)
176 say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
177 skip_to(p_start,p_sline);
178 while (filearg[0] == Nullch) {
179 if (force || batch || skip_rest_of_patch) {
180 say1("No file to patch. Skipping...\n");
181 filearg[0] = savestr(bestguess);
182 skip_rest_of_patch = TRUE;
185 (void) ask1("File to patch: ");
189 bestguess = savestr(buf);
190 filearg[0] = fetchname(buf, 0, FALSE);
192 if (filearg[0] == Nullch) {
193 if (ask1("No file found--skip this patch? [n] ")) {
199 say1("Skipping patch...\n");
200 filearg[0] = fetchname(bestguess, 0, TRUE);
201 skip_rest_of_patch = TRUE;
209 * Determine what kind of diff is in the remaining part of the patch file.
212 intuit_diff_type(void)
214 Reg4 long this_line = 0;
215 Reg5 long previous_line;
216 Reg6 long first_command_line = -1;
218 Reg7 bool last_line_was_command = FALSE;
219 Reg8 bool this_is_a_command = FALSE;
220 Reg9 bool stars_last_line = FALSE;
221 Reg10 bool stars_this_line = FALSE;
225 char *indtmp = Nullch;
226 char *oldtmp = Nullch;
227 char *newtmp = Nullch;
228 char *indname = Nullch;
229 char *oldname = Nullch;
230 char *newname = Nullch;
232 bool no_filearg = (filearg[0] == Nullch);
233 extern int index_first;
235 ok_to_create_file = FALSE;
236 Fseek(pfp, p_base, 0);
237 p_input_line = p_bline - 1;
239 previous_line = this_line;
240 last_line_was_command = this_is_a_command;
241 stars_last_line = stars_this_line;
242 this_line = ftell(pfp);
245 if (fgets(buf, sizeof buf, pfp) == Nullch) {
246 if (first_command_line >= 0L) {
247 /* nothing but deletes!? */
248 p_start = first_command_line;
255 p_sline = p_input_line;
260 for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
262 indent += 8 - (indent % 8);
266 for (t=s; isdigit((unsigned char)*t) || *t == ','; t++) ;
267 this_is_a_command = (isdigit((unsigned char)*s) &&
268 (*t == 'd' || *t == 'c' || *t == 'a') );
269 if (first_command_line < 0L && this_is_a_command) {
270 first_command_line = this_line;
271 fcl_line = p_input_line;
272 p_indent = indent; /* assume this for now */
274 if (!stars_last_line && strnEQ(s, "*** ", 4))
275 oldtmp = savestr(s+4);
276 else if (strnEQ(s, "--- ", 4))
277 newtmp = savestr(s+4);
278 else if (strnEQ(s, "+++ ", 4))
279 oldtmp = savestr(s+4); /* pretend it is the old name */
280 else if (strnEQ(s, "Index:", 6))
281 indtmp = savestr(s+6);
282 else if (strnEQ(s, "Prereq:", 7)) {
283 for (t=s+7; isspace((unsigned char)*t); t++) ;
284 revision = savestr(t);
285 for (t=revision; *t && !isspace((unsigned char)*t); t++) ;
292 if ((!diff_type || diff_type == ED_DIFF) &&
293 first_command_line >= 0L &&
296 p_start = first_command_line;
301 if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
303 ok_to_create_file = TRUE;
306 p_sline = p_input_line;
310 stars_this_line = strnEQ(s, "********", 8);
311 if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
312 strnEQ(s, "*** ", 4)) {
314 ok_to_create_file = TRUE;
315 /* if this is a new context diff the character just before */
316 /* the newline is a '*'. */
320 p_start = previous_line;
321 p_sline = p_input_line - 1;
322 retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
325 if ((!diff_type || diff_type == NORMAL_DIFF) &&
326 last_line_was_command &&
327 (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
328 p_start = previous_line;
329 p_sline = p_input_line - 1;
331 retval = NORMAL_DIFF;
337 if (indtmp != Nullch)
338 indname = fetchname(indtmp, strippath, ok_to_create_file);
339 if (oldtmp != Nullch)
340 oldname = fetchname(oldtmp, strippath, ok_to_create_file);
341 if (newtmp != Nullch)
342 newname = fetchname(newtmp, strippath, ok_to_create_file);
343 if (index_first && indname)
344 filearg[0] = savestr(indname);
345 else if (oldname && newname) {
346 if (strlen(oldname) < strlen(newname))
347 filearg[0] = savestr(oldname);
349 filearg[0] = savestr(newname);
351 filearg[0] = savestr(indname);
353 filearg[0] = savestr(oldname);
355 filearg[0] = savestr(newname);
361 if (filearg[0] != Nullch)
362 bestguess = savestr(filearg[0]);
363 else if (indtmp != Nullch)
364 bestguess = fetchname(indtmp, strippath, TRUE);
366 if (oldtmp != Nullch)
367 oldname = fetchname(oldtmp, strippath, TRUE);
368 if (newtmp != Nullch)
369 newname = fetchname(newtmp, strippath, TRUE);
370 if (oldname && newname) {
371 if (strlen(oldname) < strlen(newname))
372 bestguess = savestr(oldname);
374 bestguess = savestr(newname);
377 bestguess = savestr(oldname);
379 bestguess = savestr(newname);
381 if (indtmp != Nullch)
383 if (oldtmp != Nullch)
385 if (newtmp != Nullch)
387 if (indname != Nullch)
389 if (oldname != Nullch)
391 if (newname != Nullch)
397 * Remember where this patch ends so we know where to start up again.
400 next_intuit_at(long file_pos, long file_line)
407 * Basically a verbose fseek() to the actual diff listing.
410 skip_to(long file_pos, long file_line)
414 assert(p_base <= file_pos);
415 if (verbose && p_base < file_pos) {
416 Fseek(pfp, p_base, 0);
417 say1("The text leading up to this was:\n--------------------------\n");
418 while (ftell(pfp) < file_pos) {
419 ret = fgets(buf, sizeof buf, pfp);
420 assert(ret != Nullch);
423 say1("--------------------------\n");
426 Fseek(pfp, file_pos, 0);
427 p_input_line = file_line - 1;
430 /* Make this a function for better debugging. */
434 fatal3("malformed patch at line %ld: %s", p_input_line, buf);
435 /* about as informative as "Syntax error" in C */
439 * True if the line has been discarded (i.e. it is a line saying
440 * "\ No newline at end of file".)
443 remove_special_line(void)
451 } while (c != EOF && c != '\n');
457 fseek(pfp, -1, SEEK_CUR);
462 /* True if there is more of the current diff listing to process. */
469 Reg2 int context = 0;
472 if (p_end == p_efake)
473 p_end = p_bfake; /* don't free twice */
481 p_max = hunkmax; /* gets reduced when --- found */
482 if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
483 long line_beginning = ftell(pfp);
484 /* file pos of the current line */
485 LINENUM repl_beginning = 0; /* index of --- line */
486 Reg4 LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */
487 Reg5 LINENUM fillsrc; /* index of first line to copy */
488 Reg6 LINENUM filldst; /* index of first missing line */
489 bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
490 Reg9 bool repl_could_be_missing = TRUE;
491 /* no + or ! lines in this hunk */
492 bool repl_missing = FALSE; /* we are now backtracking */
493 long repl_backtrack_position = 0;
494 /* file pos of first repl line */
495 LINENUM repl_patch_line; /* input line number for same */
496 Reg7 LINENUM ptrn_copiable = 0;
497 /* # of copiable lines in ptrn */
499 ret = pgets(buf, sizeof buf, pfp);
501 if (ret == Nullch || strnNE(buf, "********", 8)) {
502 next_intuit_at(line_beginning,p_input_line);
506 p_hunk_beg = p_input_line + 1;
507 while (p_end < p_max) {
508 line_beginning = ftell(pfp);
509 ret = pgets(buf, sizeof buf, pfp);
512 if (p_max - p_end < 4)
513 Strcpy(buf, " \n"); /* assume blank lines got chopped */
515 if (repl_beginning && repl_could_be_missing) {
519 fatal1("unexpected end of file in patch\n");
523 assert(p_end < hunkmax);
524 p_Char[p_end] = *buf;
526 p_line[(short)p_end] = Nullch;
528 p_line[p_end] = Nullch;
532 if (strnEQ(buf, "********", 8)) {
533 if (repl_beginning && repl_could_be_missing) {
538 fatal2("unexpected end of hunk at line %ld\n",
542 if (repl_beginning && repl_could_be_missing) {
546 fatal3("unexpected *** at line %ld: %s", p_input_line, buf);
549 p_line[p_end] = savestr(buf);
554 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
557 if (strnEQ(s,"0,0",3))
559 p_first = (LINENUM) atol(s);
560 while (isdigit((unsigned char)*s)) s++;
562 for (; *s && !isdigit((unsigned char)*s); s++) ;
565 p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
573 p_max = p_ptrn_lines + 6; /* we need this much at least */
574 while (p_max >= hunkmax)
580 if (repl_beginning ||
581 (p_end != p_ptrn_lines + 1 + (p_Char[p_end-1] == '\n')))
584 /* `old' lines were omitted - set up to fill */
585 /* them in from 'new' context lines. */
586 p_end = p_ptrn_lines + 1;
589 fillcnt = p_ptrn_lines;
592 if (repl_beginning) {
593 if (repl_could_be_missing){
598 "duplicate \"---\" at line %ld--check line numbers at line %ld\n",
599 p_input_line, p_hunk_beg + repl_beginning);
603 "%s \"---\" at line %ld--check line numbers at line %ld\n",
604 (p_end <= p_ptrn_lines
607 p_input_line, p_hunk_beg);
611 repl_beginning = p_end;
612 repl_backtrack_position = ftell(pfp);
613 repl_patch_line = p_input_line;
614 p_line[p_end] = savestr(buf);
620 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
623 p_newfirst = (LINENUM) atol(s);
624 while (isdigit((unsigned char)*s)) s++;
626 for (; *s && !isdigit((unsigned char)*s); s++) ;
629 p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
637 p_max = p_repl_lines + p_end;
638 if (p_max > MAXHUNKSIZE)
639 fatal4("hunk too large (%ld lines) at line %ld: %s",
640 p_max, p_input_line, buf);
641 while (p_max >= hunkmax)
643 if (p_repl_lines != ptrn_copiable
644 && (p_context != 0 || p_repl_lines != 1))
645 repl_could_be_missing = FALSE;
650 repl_could_be_missing = FALSE;
652 if (buf[1] == '\n' && canonicalize)
654 if (!isspace((unsigned char)buf[1]) && buf[1] != '>' && buf[1] != '<' &&
655 repl_beginning && repl_could_be_missing) {
660 if (context < p_context)
664 p_line[p_end] = savestr(buf+2);
669 if (p_end == p_ptrn_lines)
671 if (remove_special_line()) {
674 len = strlen(p_line[p_end]) - 1;
675 (p_line[p_end])[len] = 0;
679 case '\t': case '\n': /* assume the 2 spaces got eaten */
680 if (repl_beginning && repl_could_be_missing &&
681 (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
685 p_line[p_end] = savestr(buf);
690 if (p_end != p_ptrn_lines + 1) {
691 ptrn_spaces_eaten |= (repl_beginning != 0);
699 if (!isspace((unsigned char)buf[1]) &&
700 repl_beginning && repl_could_be_missing) {
707 p_line[p_end] = savestr(buf+2);
714 if (repl_beginning && repl_could_be_missing) {
720 /* set up p_len for strncmp() so we don't have to */
721 /* assume null termination */
723 p_len[p_end] = strlen(p_line[p_end]);
729 if (p_end >=0 && !repl_beginning)
730 fatal2("no --- found in patch at line %ld\n", pch_hunk_beg());
734 /* reset state back to just after --- */
735 p_input_line = repl_patch_line;
736 for (p_end--; p_end > repl_beginning; p_end--)
738 Fseek(pfp, repl_backtrack_position, 0);
740 /* redundant 'new' context lines were omitted - set */
741 /* up to fill them in from the old file context */
742 if (!p_context && p_repl_lines == 1) {
747 filldst = repl_beginning+1;
748 fillcnt = p_repl_lines;
751 else if (!p_context && fillcnt == 1) {
752 /* the first hunk was a null hunk with no context */
753 /* and we were expecting one line -- fix it up. */
754 while (filldst < p_end) {
755 p_line[filldst] = p_line[filldst+1];
756 p_Char[filldst] = p_Char[filldst+1];
757 p_len[filldst] = p_len[filldst+1];
761 repl_beginning--; /* this doesn't need to be fixed */
764 p_first++; /* do append rather than insert */
769 if (diff_type == CONTEXT_DIFF &&
770 (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
773 "(Fascinating--this is really a new-style context diff but without",
774 "the telltale extra asterisks on the *** line that usually indicate",
775 "the new style...)");
776 diff_type = NEW_CONTEXT_DIFF;
779 /* if there were omitted context lines, fill them in now */
781 p_bfake = filldst; /* remember where not to free() */
782 p_efake = filldst + fillcnt - 1;
783 while (fillcnt-- > 0) {
784 while (fillsrc <= p_end && p_Char[fillsrc] != ' ')
787 fatal2("replacement text or line numbers mangled in hunk at line %ld\n",
789 p_line[filldst] = p_line[fillsrc];
790 p_Char[filldst] = p_Char[fillsrc];
791 p_len[filldst] = p_len[fillsrc];
792 fillsrc++; filldst++;
794 while (fillsrc <= p_end && fillsrc != repl_beginning &&
795 p_Char[fillsrc] != ' ')
799 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
800 fillsrc,filldst,repl_beginning,p_end+1);
802 assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
803 assert(filldst==p_end+1 || filldst==repl_beginning);
806 if (p_line[p_end] != NULL)
808 if (remove_special_line()) {
810 (p_line[p_end])[p_len[p_end]] = 0;
814 else if (diff_type == UNI_DIFF) {
815 long line_beginning = ftell(pfp);
816 /* file pos of the current line */
817 Reg4 LINENUM fillsrc; /* index of old lines */
818 Reg5 LINENUM filldst; /* index of new lines */
821 ret = pgets(buf, sizeof buf, pfp);
823 if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
824 next_intuit_at(line_beginning,p_input_line);
830 p_first = (LINENUM) atol(s);
831 while (isdigit((unsigned char)*s)) s++;
833 p_ptrn_lines = (LINENUM) atol(++s);
834 while (isdigit((unsigned char)*s)) s++;
838 if (*s != '+' || !*++s)
840 p_newfirst = (LINENUM) atol(s);
841 while (isdigit((unsigned char)*s)) s++;
843 p_repl_lines = (LINENUM) atol(++s);
844 while (isdigit((unsigned char)*s)) s++;
851 p_first++; /* do append rather than insert */
852 p_max = p_ptrn_lines + p_repl_lines + 1;
853 while (p_max >= hunkmax)
856 filldst = fillsrc + p_ptrn_lines;
857 p_end = filldst + p_repl_lines;
858 Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
859 p_line[0] = savestr(buf);
865 Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
866 p_line[filldst] = savestr(buf);
871 p_Char[filldst++] = '=';
874 p_hunk_beg = p_input_line + 1;
875 while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
876 line_beginning = ftell(pfp);
877 ret = pgets(buf, sizeof buf, pfp);
880 if (p_max - filldst < 3)
881 Strcpy(buf, " \n"); /* assume blank lines got chopped */
883 fatal1("unexpected end of file in patch\n");
886 if (*buf == '\t' || *buf == '\n') {
887 ch = ' '; /* assume the space got eaten */
895 while (--filldst > p_ptrn_lines)
896 free(p_line[filldst]);
902 if (fillsrc > p_ptrn_lines) {
907 p_Char[fillsrc] = ch;
909 p_len[fillsrc++] = strlen(s);
910 if (fillsrc > p_ptrn_lines) {
911 if (remove_special_line()) {
912 p_len[fillsrc - 1] -= 1;
913 s[p_len[fillsrc - 1]] = 0;
921 if (fillsrc > p_ptrn_lines) {
923 while (--filldst > p_ptrn_lines)
924 free(p_line[filldst]);
929 p_Char[fillsrc] = ch;
931 p_len[fillsrc++] = strlen(s);
934 while (--filldst > p_ptrn_lines)
935 free(p_line[filldst]);
941 if (filldst > p_end) {
943 while (--filldst > p_ptrn_lines)
944 free(p_line[filldst]);
948 p_Char[filldst] = ch;
950 p_len[filldst++] = strlen(s);
951 if (fillsrc > p_ptrn_lines) {
952 if (remove_special_line()) {
953 p_len[filldst - 1] -= 1;
954 s[p_len[filldst - 1]] = 0;
962 if (ch != ' ' && context > 0) {
963 if (context < p_context)
969 else { /* normal diff--fake it up */
973 long line_beginning = ftell(pfp);
976 ret = pgets(buf, sizeof buf, pfp);
978 if (ret == Nullch || !isdigit((unsigned char)*buf)) {
979 next_intuit_at(line_beginning,p_input_line);
982 p_first = (LINENUM)atol(buf);
983 for (s=buf; isdigit((unsigned char)*s); s++) ;
985 p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
986 while (isdigit((unsigned char)*s)) s++;
989 p_ptrn_lines = (*s != 'a');
991 if (hunk_type == 'a')
992 p_first++; /* do append rather than insert */
993 min = (LINENUM)atol(++s);
994 for (; isdigit((unsigned char)*s); s++) ;
996 max = (LINENUM)atol(++s);
999 if (hunk_type == 'd')
1001 p_end = p_ptrn_lines + 1 + max - min + 1;
1002 if (p_end > MAXHUNKSIZE)
1003 fatal4("hunk too large (%ld lines) at line %ld: %s",
1004 p_end, p_input_line, buf);
1005 while (p_end >= hunkmax)
1008 p_repl_lines = max - min + 1;
1009 Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
1010 p_line[0] = savestr(buf);
1016 for (i=1; i<=p_ptrn_lines; i++) {
1017 ret = pgets(buf, sizeof buf, pfp);
1020 fatal2("unexpected end of file in patch at line %ld\n",
1023 fatal2("< expected at line %ld of patch\n", p_input_line);
1024 p_line[i] = savestr(buf+2);
1029 p_len[i] = strlen(p_line[i]);
1033 if (remove_special_line()) {
1035 (p_line[i-1])[p_len[i-1]] = 0;
1038 if (hunk_type == 'c') {
1039 ret = pgets(buf, sizeof buf, pfp);
1042 fatal2("unexpected end of file in patch at line %ld\n",
1045 fatal2("--- expected at line %ld of patch\n", p_input_line);
1047 Sprintf(buf, "--- %ld,%ld\n", min, max);
1048 p_line[i] = savestr(buf);
1054 for (i++; i<=p_end; i++) {
1055 ret = pgets(buf, sizeof buf, pfp);
1058 fatal2("unexpected end of file in patch at line %ld\n",
1061 fatal2("> expected at line %ld of patch\n", p_input_line);
1062 p_line[i] = savestr(buf+2);
1067 p_len[i] = strlen(p_line[i]);
1071 if (remove_special_line()) {
1073 (p_line[i-1])[p_len[i-1]] = 0;
1076 if (reverse) /* backwards patch? */
1078 say1("Not enough memory to swap next hunk!\n");
1084 for (i=0; i <= p_end; i++) {
1085 if (i == p_ptrn_lines)
1089 fprintf(stderr, "%3d %c %c %s", i, p_Char[i], special, p_line[i]);
1094 if (p_end+1 < hunkmax) /* paranoia reigns supreme... */
1095 p_Char[p_end+1] = '^'; /* add a stopper for apply_hunk */
1100 * Input a line from the patch file, worrying about indentation.
1103 pgets(char *bf, int sz, FILE *fp)
1105 char *ret = fgets(bf, sz, fp);
1107 Reg2 int indent = 0;
1109 if (p_indent && ret != Nullch) {
1111 indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
1113 indent += 8 - (indent % 7);
1125 * Reverse the old and new portions of the current hunk.
1130 char **tp_line; /* the text of the hunk */
1131 short *tp_len; /* length of each line */
1132 char *tp_char; /* +, -, and ! */
1135 bool blankline = FALSE;
1139 p_first = p_newfirst;
1142 /* make a scratch copy */
1147 p_line = Null(char**); /* force set_hunkmax to allocate again */
1148 p_len = Null(short*);
1151 if (p_line == Null(char**) || p_len == Null(short*) || p_Char == Nullch) {
1153 if (p_line == Null(char**))
1154 free((char*)p_line);
1156 if (p_len == Null(short*))
1160 if (p_Char == Nullch)
1161 free((char*)p_Char);
1163 return FALSE; /* not enough memory to swap hunk! */
1166 /* now turn the new into the old */
1168 i = p_ptrn_lines + 1;
1169 if (tp_char[i] == '\n') { /* account for possible blank line */
1173 if (p_efake >= 0) { /* fix non-freeable ptr range */
1181 for (n=0; i <= p_end; i++,n++) {
1182 p_line[n] = tp_line[i];
1183 p_Char[n] = tp_char[i];
1184 if (p_Char[n] == '+')
1186 p_len[n] = tp_len[i];
1189 i = p_ptrn_lines + 1;
1190 p_line[n] = tp_line[i];
1191 p_Char[n] = tp_char[i];
1192 p_len[n] = tp_len[i];
1195 assert(p_Char[0] == '=');
1197 for (s=p_line[0]; *s; s++)
1201 /* now turn the old into the new */
1203 assert(tp_char[0] == '*');
1205 for (s=tp_line[0]; *s; s++)
1208 for (i=0; n <= p_end; i++,n++) {
1209 p_line[n] = tp_line[i];
1210 p_Char[n] = tp_char[i];
1211 if (p_Char[n] == '-')
1213 p_len[n] = tp_len[i];
1215 assert(i == p_ptrn_lines + 1);
1217 p_ptrn_lines = p_repl_lines;
1220 if (tp_line == Null(char**))
1221 free((char*)tp_line);
1222 if (tp_len == Null(short*))
1223 free((char*)tp_len);
1225 if (tp_char == Nullch)
1226 free((char*)tp_char);
1231 * Return the specified line position in the old file of the old context.
1240 * Return the number of lines of old context.
1243 pch_ptrn_lines(void)
1245 return p_ptrn_lines;
1249 * Return the probable line position in the new file of the first line.
1258 * Return the number of lines in the replacement text including context.
1261 pch_repl_lines(void)
1263 return p_repl_lines;
1267 * Return the number of lines in the whole hunk.
1276 * Return the number of context lines before the first changed line.
1285 * Return the length of a particular patch line.
1288 pch_line_len(LINENUM line)
1294 * Return the control character (+, -, *, !, etc) for a patch line.
1297 pch_char(LINENUM line)
1299 return p_Char[line];
1303 * Return a pointer to a particular patch line.
1306 pfetch(LINENUM line)
1308 return p_line[line];
1312 * Return where in the patch file this hunk began, for error messages.
1321 * Apply an ed script by feeding ed itself.
1327 Reg2 long beginning_of_this_line;
1328 Reg3 bool this_line_is_command = FALSE;
1331 if (!skip_rest_of_patch) {
1333 copy_file(filearg[0], TMPOUTNAME);
1335 Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
1337 Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
1338 pipefp = popen(buf, "w");
1341 beginning_of_this_line = ftell(pfp);
1342 if (pgets(buf, sizeof buf, pfp) == Nullch) {
1343 next_intuit_at(beginning_of_this_line,p_input_line);
1347 for (t=buf; isdigit((unsigned char)*t) || *t == ','; t++) ;
1348 this_line_is_command = (isdigit((unsigned char)*buf) &&
1349 (*t == 'd' || *t == 'c' || *t == 'a') );
1350 if (this_line_is_command) {
1351 if (!skip_rest_of_patch)
1354 while (pgets(buf, sizeof buf, pfp) != Nullch) {
1356 if (!skip_rest_of_patch)
1358 if (strEQ(buf, ".\n"))
1364 next_intuit_at(beginning_of_this_line,p_input_line);
1368 if (skip_rest_of_patch)
1370 fprintf(pipefp, "w\n");
1371 fprintf(pipefp, "q\n");
1375 if (move_file(TMPOUTNAME, outname) < 0) {
1377 chmod(TMPOUTNAME, filemode);
1380 chmod(outname, filemode);