4 * Revision 2.0.2.0 90/05/01 22:17:51 davison
5 * patch12u: unidiff support added
7 * Revision 2.0.1.7 88/06/03 15:13:28 lwall
8 * patch10: Can now find patches in shar scripts.
9 * patch10: Hunks that swapped and then swapped back could core dump.
11 * Revision 2.0.1.6 87/06/04 16:18:13 lwall
12 * pch_swap didn't swap p_bfake and p_efake.
14 * Revision 2.0.1.5 87/01/30 22:47:42 lwall
15 * Improved responses to mangled patches.
17 * Revision 2.0.1.4 87/01/05 16:59:53 lwall
18 * New-style context diffs caused double call to free().
20 * Revision 2.0.1.3 86/11/14 10:08:33 lwall
21 * Fixed problem where a long pattern wouldn't grow the hunk.
22 * Also restored p_input_line when backtracking so error messages are right.
24 * Revision 2.0.1.2 86/11/03 17:49:52 lwall
25 * New-style delete triggers spurious assertion error.
27 * Revision 2.0.1.1 86/10/29 15:52:08 lwall
28 * Could falsely report new-style context diff.
30 * Revision 2.0 86/09/17 15:39:37 lwall
31 * Baseline for netwide release.
41 /* Patch (diff listing) abstract type. */
43 static long p_filesize; /* size of the patch file */
44 static LINENUM p_first; /* 1st line number */
45 static LINENUM p_newfirst; /* 1st line number of replacement */
46 static LINENUM p_ptrn_lines; /* # lines in pattern */
47 static LINENUM p_repl_lines; /* # lines in replacement text */
48 static LINENUM p_end = -1; /* last line in hunk */
49 static LINENUM p_max; /* max allowed value of p_end */
50 static LINENUM p_context = 3; /* # of context lines */
51 static LINENUM p_input_line = 0; /* current line # from patch file */
52 static char **p_line = Null(char**); /* the text of the hunk */
53 static short *p_len = Null(short*); /* length of each line */
54 static char *p_Char = Nullch; /* +, -, and ! */
55 static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */
56 static int p_indent; /* indent to patch */
57 static LINENUM p_base; /* where to intuit this time */
58 static LINENUM p_bline; /* line # of p_base */
59 static LINENUM p_start; /* where intuit found a patch */
60 static LINENUM p_sline; /* and the line number for it */
61 static LINENUM p_hunk_beg; /* line number of current hunk */
62 static LINENUM p_efake = -1; /* end of faked up lines--don't free */
63 static LINENUM p_bfake = -1; /* beg of faked up lines */
66 * Prepare to look for the next patch in the patch file.
73 p_ptrn_lines = Nulline;
74 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, buf_size, 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 */
105 * Make sure our dynamically realloced tables are malloced to begin with.
111 if (p_line == Null(char**))
112 p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
113 if (p_len == Null(short*))
114 p_len = (short*) malloc((MEM)hunkmax * sizeof(short));
116 if (p_Char == Nullch)
117 p_Char = (char*) malloc((MEM)hunkmax * sizeof(char));
121 * Enlarge the arrays containing the current hunk of patch.
128 * Note that on most systems, only the p_line array ever gets
129 * fresh memory since p_len can move into p_line's old space,
130 * and p_Char can move into p_len's old space. Not on PDP-11's
131 * however. But it doesn't matter.
133 assert(p_line != Null(char**) && p_len != Null(short*) &&
136 p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
137 p_len = (short*) realloc((char*)p_len, (MEM)hunkmax * sizeof(short));
138 p_Char = (char*) realloc((char*)p_Char, (MEM)hunkmax * sizeof(char));
140 if (p_line != Null(char**) && p_len != Null(short*) && p_Char != Nullch)
143 fatal1("out of memory\n");
144 out_of_mem = TRUE; /* whatever is null will be allocated again */
145 /* from within plan_a(), of all places */
149 * True if the remainder of the patch file contains a diff of some sort.
152 there_is_another_patch(void)
154 if (p_base != 0L && p_base >= p_filesize) {
161 diff_type = intuit_diff_type();
165 say1(" Ignoring the trailing garbage.\ndone\n");
168 say1(" I can't seem to find a patch in there anywhere.\n");
172 say3(" %sooks like %s to me...\n",
173 (p_base == 0L ? "L" : "The next patch l"),
174 diff_type == UNI_DIFF ? "a unified diff" :
175 diff_type == CONTEXT_DIFF ? "a context diff" :
176 diff_type == NEW_CONTEXT_DIFF ?
177 "a new-style context diff" :
178 diff_type == NORMAL_DIFF ? "a normal diff" :
180 if (p_indent && verbose)
181 say3("(Patch is indented %d space%s.)\n",
182 p_indent, p_indent==1?"":"s");
183 skip_to(p_start,p_sline);
184 while (filearg[0] == Nullch) {
185 if (force || batch || skip_rest_of_patch) {
186 say1("No file to patch. Skipping...\n");
187 filearg[0] = savestr(bestguess);
188 skip_rest_of_patch = TRUE;
191 (void) ask1("File to patch: ");
195 bestguess = savestr(buf);
196 filearg[0] = fetchname(buf, 0, FALSE);
198 if (filearg[0] == Nullch) {
199 if (ask1("No file found--skip this patch? [n] ")) {
205 say1("Skipping patch...\n");
206 filearg[0] = fetchname(bestguess, 0, TRUE);
207 skip_rest_of_patch = TRUE;
215 p4_savestr(char *str)
219 /* Leading whitespace. */
220 while (isspace((unsigned char)*str))
223 /* Remove the file revision number. */
224 for (t = str, h = NULL; *t != '\0' && !isspace((unsigned char)*t); t++)
234 * Determine what kind of diff is in the remaining part of the patch file.
237 intuit_diff_type(void)
239 Reg4 long this_line = 0;
240 Reg5 long previous_line;
241 Reg6 long first_command_line = -1;
243 Reg7 bool last_line_was_command = FALSE;
244 Reg8 bool this_is_a_command = FALSE;
245 Reg9 bool stars_last_line = FALSE;
246 Reg10 bool stars_this_line = FALSE;
250 char *indtmp = Nullch;
251 char *oldtmp = Nullch;
252 char *newtmp = Nullch;
253 char *indname = Nullch;
254 char *oldname = Nullch;
255 char *newname = Nullch;
257 bool no_filearg = (filearg[0] == Nullch);
258 extern int index_first;
260 ok_to_create_file = FALSE;
261 Fseek(pfp, p_base, 0);
262 p_input_line = p_bline - 1;
264 previous_line = this_line;
265 last_line_was_command = this_is_a_command;
266 stars_last_line = stars_this_line;
267 this_line = ftell(pfp);
270 if (pgets(FALSE) == 0) {
271 if (first_command_line >= 0L) {
272 /* nothing but deletes!? */
273 p_start = first_command_line;
280 p_sline = p_input_line;
285 for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
287 indent += 8 - (indent % 8);
291 for (t=s; isdigit((unsigned char)*t) || *t == ','; t++)
293 this_is_a_command = (isdigit((unsigned char)*s) &&
294 (*t == 'd' || *t == 'c' || *t == 'a') );
295 if (first_command_line < 0L && this_is_a_command) {
296 first_command_line = this_line;
297 fcl_line = p_input_line;
298 p_indent = indent; /* assume this for now */
300 if (!stars_last_line && strnEQ(s, "*** ", 4))
301 oldtmp = savestr(s+4);
302 else if (strnEQ(s, "--- ", 4))
303 newtmp = savestr(s+4);
304 else if (strnEQ(s, "+++ ", 4))
305 oldtmp = savestr(s+4); /* pretend it is the old name */
306 else if (strnEQ(s, "Index:", 6))
307 indtmp = savestr(s+6);
308 else if (strnEQ(s, "Prereq:", 7)) {
309 for (t = s + 7; isspace((unsigned char)*t); t++)
311 revision = savestr(t);
312 for (t = revision; *t && !isspace((unsigned char)*t);
320 } else if (strnEQ(s, "==== ", 5)) {
321 /* Perforce-style diffs. */
322 if ((t = strstr(s + 5, " - ")) != NULL)
323 newtmp = p4_savestr(t + 3);
324 oldtmp = p4_savestr(s + 5);
326 if ((!diff_type || diff_type == ED_DIFF) &&
327 first_command_line >= 0L &&
330 p_start = first_command_line;
335 if ((!diff_type || diff_type == UNI_DIFF) &&
336 strnEQ(s, "@@ -", 4)) {
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) &&
348 strnEQ(s, "*** ", 4)) {
350 ok_to_create_file = TRUE;
352 * If this is a new context diff the character just
353 * before the newline is a '*'.
358 p_start = previous_line;
359 p_sline = p_input_line - 1;
360 retval = (*(s-1) == '*' ?
361 NEW_CONTEXT_DIFF : CONTEXT_DIFF);
364 if ((!diff_type || diff_type == NORMAL_DIFF) &&
365 last_line_was_command &&
366 (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
367 p_start = previous_line;
368 p_sline = p_input_line - 1;
370 retval = NORMAL_DIFF;
376 if (indtmp != Nullch)
377 indname = fetchname(indtmp, strippath,
379 if (oldtmp != Nullch)
380 oldname = fetchname(oldtmp, strippath,
382 if (newtmp != Nullch)
383 newname = fetchname(newtmp, strippath,
385 if (index_first && indname)
386 filearg[0] = savestr(indname);
387 else if (oldname && newname) {
388 if (strlen(oldname) < strlen(newname))
389 filearg[0] = savestr(oldname);
391 filearg[0] = savestr(newname);
393 filearg[0] = savestr(indname);
395 filearg[0] = savestr(oldname);
397 filearg[0] = savestr(newname);
403 if (filearg[0] != Nullch)
404 bestguess = savestr(filearg[0]);
405 else if (indtmp != Nullch)
406 bestguess = fetchname(indtmp, strippath, TRUE);
408 if (oldtmp != Nullch)
409 oldname = fetchname(oldtmp, strippath, TRUE);
410 if (newtmp != Nullch)
411 newname = fetchname(newtmp, strippath, TRUE);
412 if (oldname && newname) {
413 if (strlen(oldname) < strlen(newname))
414 bestguess = savestr(oldname);
416 bestguess = savestr(newname);
419 bestguess = savestr(oldname);
421 bestguess = savestr(newname);
423 if (indtmp != Nullch)
425 if (oldtmp != Nullch)
427 if (newtmp != Nullch)
429 if (indname != Nullch)
431 if (oldname != Nullch)
433 if (newname != Nullch)
439 * Remember where this patch ends so we know where to start up again.
442 next_intuit_at(long file_pos, long file_line)
449 * Basically a verbose fseek() to the actual diff listing.
452 skip_to(long file_pos, long file_line)
456 assert(p_base <= file_pos);
457 if (verbose && p_base < file_pos) {
458 Fseek(pfp, p_base, 0);
459 say1("The text leading up to this was:\n--------------------------\n");
460 while (ftell(pfp) < file_pos) {
465 say1("--------------------------\n");
468 Fseek(pfp, file_pos, 0);
469 p_input_line = file_line - 1;
473 * Make this a function for better debugging.
478 fatal3("malformed patch at line %ld: %s", p_input_line, buf);
479 /* about as informative as "Syntax error" in C */
483 * True if the line has been discarded (i.e. it is a line saying
484 * "\ No newline at end of file".)
487 remove_special_line(void)
495 } while (c != EOF && c != '\n');
501 fseek(pfp, -1, SEEK_CUR);
507 * True if there is more of the current diff listing to process.
514 Reg2 int context = 0;
517 if (p_end == p_efake)
518 p_end = p_bfake; /* don't free twice */
526 p_max = hunkmax; /* gets reduced when --- found */
527 if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
528 long line_beginning = ftell(pfp);
529 /* file pos of the current line */
530 LINENUM repl_beginning = 0; /* index of --- line */
531 Reg4 LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */
532 Reg5 LINENUM fillsrc; /* index of first line to copy */
533 Reg6 LINENUM filldst; /* index of first missing line */
534 bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
535 Reg9 bool repl_could_be_missing = TRUE;
536 /* no + or ! lines in this hunk */
537 bool repl_missing = FALSE; /* we are now backtracking */
538 long repl_backtrack_position = 0;
539 /* file pos of first repl line */
540 LINENUM repl_patch_line; /* input line number for same */
541 Reg7 LINENUM ptrn_copiable = 0;
542 /* # of copiable lines in ptrn */
546 if (len == 0 || strnNE(buf, "********", 8)) {
547 next_intuit_at(line_beginning,p_input_line);
551 p_hunk_beg = p_input_line + 1;
552 while (p_end < p_max) {
553 line_beginning = ftell(pfp);
557 if (p_max - p_end < 4)
558 Strcpy(buf, " \n"); /* assume blank lines got chopped */
560 if (repl_beginning && repl_could_be_missing) {
564 fatal1("unexpected end of file in patch\n");
568 assert(p_end < hunkmax);
569 p_Char[p_end] = *buf;
571 p_line[(short)p_end] = Nullch;
573 p_line[p_end] = Nullch;
577 if (strnEQ(buf, "********", 8)) {
578 if (repl_beginning && repl_could_be_missing) {
583 fatal2("unexpected end of hunk at line %ld\n",
587 if (repl_beginning && repl_could_be_missing) {
591 fatal3("unexpected *** at line %ld: %s", p_input_line, buf);
594 p_line[p_end] = savestr(buf);
599 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
602 if (strnEQ(s,"0,0",3))
604 p_first = (LINENUM) atol(s);
605 while (isdigit((unsigned char)*s)) s++;
607 for (; *s && !isdigit((unsigned char)*s); s++) ;
610 p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
618 p_max = p_ptrn_lines + 6; /* we need this much at least */
619 while (p_max >= hunkmax)
625 if (repl_beginning ||
626 (p_end != p_ptrn_lines + 1 + (p_Char[p_end-1] == '\n')))
629 /* `old' lines were omitted - set up to fill */
630 /* them in from 'new' context lines. */
631 p_end = p_ptrn_lines + 1;
634 fillcnt = p_ptrn_lines;
637 if (repl_beginning) {
638 if (repl_could_be_missing){
643 "duplicate \"---\" at line %ld--check line numbers at line %ld\n",
644 p_input_line, p_hunk_beg + repl_beginning);
648 "%s \"---\" at line %ld--check line numbers at line %ld\n",
649 (p_end <= p_ptrn_lines
652 p_input_line, p_hunk_beg);
656 repl_beginning = p_end;
657 repl_backtrack_position = ftell(pfp);
658 repl_patch_line = p_input_line;
659 p_line[p_end] = savestr(buf);
665 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
668 p_newfirst = (LINENUM) atol(s);
669 while (isdigit((unsigned char)*s)) s++;
671 for (; *s && !isdigit((unsigned char)*s); s++) ;
674 p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
682 p_max = p_repl_lines + p_end;
683 if (p_max > MAXHUNKSIZE)
684 fatal4("hunk too large (%ld lines) at line %ld: %s",
685 p_max, p_input_line, buf);
686 while (p_max >= hunkmax)
688 if (p_repl_lines != ptrn_copiable
689 && (p_context != 0 || p_repl_lines != 1))
690 repl_could_be_missing = FALSE;
695 repl_could_be_missing = FALSE;
697 if (buf[1] == '\n' && canonicalize)
699 if (!isspace((unsigned char)buf[1]) && buf[1] != '>' && buf[1] != '<' &&
700 repl_beginning && repl_could_be_missing) {
705 if (context < p_context)
709 p_line[p_end] = savestr(buf+2);
714 if (p_end == p_ptrn_lines)
716 if (remove_special_line()) {
719 len = strlen(p_line[p_end]) - 1;
720 (p_line[p_end])[len] = 0;
724 case '\t': case '\n': /* assume the 2 spaces got eaten */
725 if (repl_beginning && repl_could_be_missing &&
726 (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
730 p_line[p_end] = savestr(buf);
735 if (p_end != p_ptrn_lines + 1) {
736 ptrn_spaces_eaten |= (repl_beginning != 0);
744 if (!isspace((unsigned char)buf[1]) &&
745 repl_beginning && repl_could_be_missing) {
752 p_line[p_end] = savestr(buf+2);
759 if (repl_beginning && repl_could_be_missing) {
765 /* set up p_len for strncmp() so we don't have to */
766 /* assume null termination */
768 p_len[p_end] = strlen(p_line[p_end]);
774 if (p_end >=0 && !repl_beginning)
775 fatal2("no --- found in patch at line %ld\n", pch_hunk_beg());
779 /* reset state back to just after --- */
780 p_input_line = repl_patch_line;
781 for (p_end--; p_end > repl_beginning; p_end--)
783 Fseek(pfp, repl_backtrack_position, 0);
785 /* redundant 'new' context lines were omitted - set */
786 /* up to fill them in from the old file context */
787 if (!p_context && p_repl_lines == 1) {
792 filldst = repl_beginning+1;
793 fillcnt = p_repl_lines;
796 else if (!p_context && fillcnt == 1) {
797 /* the first hunk was a null hunk with no context */
798 /* and we were expecting one line -- fix it up. */
799 while (filldst < p_end) {
800 p_line[filldst] = p_line[filldst+1];
801 p_Char[filldst] = p_Char[filldst+1];
802 p_len[filldst] = p_len[filldst+1];
806 repl_beginning--; /* this doesn't need to be fixed */
809 p_first++; /* do append rather than insert */
814 if (diff_type == CONTEXT_DIFF &&
815 (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
818 "(Fascinating--this is really a new-style context diff but without",
819 "the telltale extra asterisks on the *** line that usually indicate",
820 "the new style...)");
821 diff_type = NEW_CONTEXT_DIFF;
824 /* if there were omitted context lines, fill them in now */
826 p_bfake = filldst; /* remember where not to free() */
827 p_efake = filldst + fillcnt - 1;
828 while (fillcnt-- > 0) {
829 while (fillsrc <= p_end && p_Char[fillsrc] != ' ')
832 fatal2("replacement text or line numbers mangled in hunk at line %ld\n",
834 p_line[filldst] = p_line[fillsrc];
835 p_Char[filldst] = p_Char[fillsrc];
836 p_len[filldst] = p_len[fillsrc];
837 fillsrc++; filldst++;
839 while (fillsrc <= p_end && fillsrc != repl_beginning &&
840 p_Char[fillsrc] != ' ')
844 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
845 fillsrc,filldst,repl_beginning,p_end+1);
847 assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
848 assert(filldst==p_end+1 || filldst==repl_beginning);
851 if (p_line[p_end] != NULL)
853 if (remove_special_line()) {
855 (p_line[p_end])[p_len[p_end]] = 0;
859 else if (diff_type == UNI_DIFF) {
860 long line_beginning = ftell(pfp);
861 /* file pos of the current line */
862 Reg4 LINENUM fillsrc; /* index of old lines */
863 Reg5 LINENUM filldst; /* index of new lines */
868 if (len == 0 || strnNE(buf, "@@ -", 4)) {
869 next_intuit_at(line_beginning,p_input_line);
875 p_first = (LINENUM) atol(s);
876 while (isdigit((unsigned char)*s)) s++;
878 p_ptrn_lines = (LINENUM) atol(++s);
879 while (isdigit((unsigned char)*s)) s++;
883 if (*s != '+' || !*++s)
885 p_newfirst = (LINENUM) atol(s);
886 while (isdigit((unsigned char)*s)) s++;
888 p_repl_lines = (LINENUM) atol(++s);
889 while (isdigit((unsigned char)*s)) s++;
896 p_first++; /* do append rather than insert */
897 p_max = p_ptrn_lines + p_repl_lines + 1;
898 while (p_max >= hunkmax)
901 filldst = fillsrc + p_ptrn_lines;
902 p_end = filldst + p_repl_lines;
903 Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
904 p_line[0] = savestr(buf);
910 Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
911 p_line[filldst] = savestr(buf);
916 p_Char[filldst++] = '=';
919 p_hunk_beg = p_input_line + 1;
920 while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
921 line_beginning = ftell(pfp);
925 if (p_max - filldst < 3)
926 Strcpy(buf, " \n"); /* assume blank lines got chopped */
928 fatal1("unexpected end of file in patch\n");
931 if (*buf == '\t' || *buf == '\n') {
932 ch = ' '; /* assume the space got eaten */
940 while (--filldst > p_ptrn_lines)
941 free(p_line[filldst]);
947 if (fillsrc > p_ptrn_lines) {
952 p_Char[fillsrc] = ch;
954 p_len[fillsrc++] = strlen(s);
955 if (fillsrc > p_ptrn_lines) {
956 if (remove_special_line()) {
957 p_len[fillsrc - 1] -= 1;
958 s[p_len[fillsrc - 1]] = 0;
966 if (fillsrc > p_ptrn_lines) {
968 while (--filldst > p_ptrn_lines)
969 free(p_line[filldst]);
974 p_Char[fillsrc] = ch;
976 p_len[fillsrc++] = strlen(s);
979 while (--filldst > p_ptrn_lines)
980 free(p_line[filldst]);
986 if (filldst > p_end) {
988 while (--filldst > p_ptrn_lines)
989 free(p_line[filldst]);
993 p_Char[filldst] = ch;
995 p_len[filldst++] = strlen(s);
996 if (fillsrc > p_ptrn_lines) {
997 if (remove_special_line()) {
998 p_len[filldst - 1] -= 1;
999 s[p_len[filldst - 1]] = 0;
1007 if (ch != ' ' && context > 0) {
1008 if (context < p_context)
1009 p_context = context;
1014 else { /* normal diff--fake it up */
1018 long line_beginning = ftell(pfp);
1023 if (len == 0 || !isdigit((unsigned char)*buf)) {
1024 next_intuit_at(line_beginning,p_input_line);
1027 p_first = (LINENUM)atol(buf);
1028 for (s=buf; isdigit((unsigned char)*s); s++) ;
1030 p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
1031 while (isdigit((unsigned char)*s)) s++;
1034 p_ptrn_lines = (*s != 'a');
1036 if (hunk_type == 'a')
1037 p_first++; /* do append rather than insert */
1038 min = (LINENUM)atol(++s);
1039 for (; isdigit((unsigned char)*s); s++) ;
1041 max = (LINENUM)atol(++s);
1044 if (hunk_type == 'd')
1046 p_end = p_ptrn_lines + 1 + max - min + 1;
1047 if (p_end > MAXHUNKSIZE)
1048 fatal4("hunk too large (%ld lines) at line %ld: %s",
1049 p_end, p_input_line, buf);
1050 while (p_end >= hunkmax)
1053 p_repl_lines = max - min + 1;
1054 Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
1055 p_line[0] = savestr(buf);
1061 for (i=1; i<=p_ptrn_lines; i++) {
1065 fatal2("unexpected end of file in patch at line %ld\n",
1068 fatal2("< expected at line %ld of patch\n", p_input_line);
1069 p_line[i] = savestr(buf+2);
1074 p_len[i] = strlen(p_line[i]);
1078 if (remove_special_line()) {
1080 (p_line[i-1])[p_len[i-1]] = 0;
1083 if (hunk_type == 'c') {
1087 fatal2("unexpected end of file in patch at line %ld\n",
1090 fatal2("--- expected at line %ld of patch\n", p_input_line);
1092 Sprintf(buf, "--- %ld,%ld\n", min, max);
1093 p_line[i] = savestr(buf);
1099 for (i++; i<=p_end; i++) {
1103 fatal2("unexpected end of file in patch at line %ld\n",
1106 fatal2("> expected at line %ld of patch\n", p_input_line);
1107 p_line[i] = savestr(buf+2);
1112 p_len[i] = strlen(p_line[i]);
1116 if (remove_special_line()) {
1118 (p_line[i-1])[p_len[i-1]] = 0;
1121 if (reverse) /* backwards patch? */
1123 say1("Not enough memory to swap next hunk!\n");
1129 for (i=0; i <= p_end; i++) {
1130 if (i == p_ptrn_lines)
1134 fprintf(stderr, "%3d %c %c %s", i, p_Char[i], special, p_line[i]);
1139 if (p_end+1 < hunkmax) /* paranoia reigns supreme... */
1140 p_Char[p_end+1] = '^'; /* add a stopper for apply_hunk */
1145 * Input a line from the patch file.
1146 * Worry about indentation if do_indent is true.
1147 * The line is read directly into the buf global variable which
1148 * is resized if necessary in order to hold the complete line.
1149 * Returns the number of characters read including the terminating
1153 pgets(bool do_indent)
1157 int indent = 0, skipped = 0;
1159 line = fgetln(pfp, &len);
1160 if (line != Nullch) {
1161 if (len + 1 > buf_size) {
1162 while (len + 1 > buf_size)
1165 buf = malloc(buf_size);
1167 fatal1("out of memory\n");
1169 if (do_indent == TRUE && p_indent) {
1171 indent < p_indent && (*line == ' ' || *line == '\t' || *line == 'X');
1172 line++, skipped++) {
1174 indent += 8 - (indent %7);
1179 Strncpy(buf, line, len - skipped);
1180 buf[len - skipped] = '\0';
1186 * Reverse the old and new portions of the current hunk.
1191 char **tp_line; /* the text of the hunk */
1192 short *tp_len; /* length of each line */
1193 char *tp_char; /* +, -, and ! */
1196 bool blankline = FALSE;
1200 p_first = p_newfirst;
1203 /* make a scratch copy */
1208 p_line = Null(char**); /* force set_hunkmax to allocate again */
1209 p_len = Null(short*);
1212 if (p_line == Null(char**) || p_len == Null(short*) || p_Char == Nullch) {
1214 if (p_line == Null(char**))
1215 free((char*)p_line);
1217 if (p_len == Null(short*))
1221 if (p_Char == Nullch)
1222 free((char*)p_Char);
1224 return FALSE; /* not enough memory to swap hunk! */
1227 /* now turn the new into the old */
1229 i = p_ptrn_lines + 1;
1230 if (tp_char[i] == '\n') { /* account for possible blank line */
1234 if (p_efake >= 0) { /* fix non-freeable ptr range */
1242 for (n=0; i <= p_end; i++,n++) {
1243 p_line[n] = tp_line[i];
1244 p_Char[n] = tp_char[i];
1245 if (p_Char[n] == '+')
1247 p_len[n] = tp_len[i];
1250 i = p_ptrn_lines + 1;
1251 p_line[n] = tp_line[i];
1252 p_Char[n] = tp_char[i];
1253 p_len[n] = tp_len[i];
1256 assert(p_Char[0] == '=');
1258 for (s=p_line[0]; *s; s++)
1262 /* now turn the old into the new */
1264 assert(tp_char[0] == '*');
1266 for (s=tp_line[0]; *s; s++)
1269 for (i=0; n <= p_end; i++,n++) {
1270 p_line[n] = tp_line[i];
1271 p_Char[n] = tp_char[i];
1272 if (p_Char[n] == '-')
1274 p_len[n] = tp_len[i];
1276 assert(i == p_ptrn_lines + 1);
1278 p_ptrn_lines = p_repl_lines;
1281 if (tp_line == Null(char**))
1282 free((char*)tp_line);
1283 if (tp_len == Null(short*))
1284 free((char*)tp_len);
1286 if (tp_char == Nullch)
1287 free((char*)tp_char);
1292 * Return the specified line position in the old file of the old context.
1301 * Return the number of lines of old context.
1304 pch_ptrn_lines(void)
1306 return p_ptrn_lines;
1310 * Return the probable line position in the new file of the first line.
1319 * Return the number of lines in the replacement text including context.
1322 pch_repl_lines(void)
1324 return p_repl_lines;
1328 * Return the number of lines in the whole hunk.
1337 * Return the number of context lines before the first changed line.
1346 * Return the length of a particular patch line.
1349 pch_line_len(LINENUM line)
1355 * Return the control character (+, -, *, !, etc) for a patch line.
1358 pch_char(LINENUM line)
1360 return p_Char[line];
1364 * Return a pointer to a particular patch line.
1367 pfetch(LINENUM line)
1369 return p_line[line];
1373 * Return where in the patch file this hunk began, for error messages.
1382 * Apply an ed script by feeding ed itself.
1388 Reg2 long beginning_of_this_line;
1389 Reg3 bool this_line_is_command = FALSE;
1392 if (!skip_rest_of_patch) {
1394 copy_file(filearg[0], TMPOUTNAME);
1396 Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
1398 Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
1399 pipefp = popen(buf, "w");
1402 beginning_of_this_line = ftell(pfp);
1403 if (pgets(TRUE) == 0) {
1404 next_intuit_at(beginning_of_this_line, p_input_line);
1408 for (t=buf; isdigit((unsigned char)*t) || *t == ','; t++)
1410 this_line_is_command = (isdigit((unsigned char)*buf) &&
1411 (*t == 'd' || *t == 'c' || *t == 'a') );
1412 if (this_line_is_command) {
1413 if (!skip_rest_of_patch)
1416 while (pgets(TRUE) != 0) {
1418 if (!skip_rest_of_patch)
1420 if (strEQ(buf, ".\n"))
1426 next_intuit_at(beginning_of_this_line,p_input_line);
1430 if (skip_rest_of_patch)
1432 fprintf(pipefp, "w\n");
1433 fprintf(pipefp, "q\n");
1437 if (move_file(TMPOUTNAME, outname) < 0) {
1439 chmod(TMPOUTNAME, filemode);
1442 chmod(outname, filemode);