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, 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 */
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 * Determine what kind of diff is in the remaining part of the patch file.
218 intuit_diff_type(void)
220 Reg4 long this_line = 0;
221 Reg5 long previous_line;
222 Reg6 long first_command_line = -1;
224 Reg7 bool last_line_was_command = FALSE;
225 Reg8 bool this_is_a_command = FALSE;
226 Reg9 bool stars_last_line = FALSE;
227 Reg10 bool stars_this_line = FALSE;
231 char *indtmp = Nullch;
232 char *oldtmp = Nullch;
233 char *newtmp = Nullch;
234 char *indname = Nullch;
235 char *oldname = Nullch;
236 char *newname = Nullch;
238 bool no_filearg = (filearg[0] == Nullch);
239 extern int index_first;
241 ok_to_create_file = FALSE;
242 Fseek(pfp, p_base, 0);
243 p_input_line = p_bline - 1;
245 previous_line = this_line;
246 last_line_was_command = this_is_a_command;
247 stars_last_line = stars_this_line;
248 this_line = ftell(pfp);
251 if (fgets(buf, sizeof buf, pfp) == Nullch) {
252 if (first_command_line >= 0L) {
253 /* nothing but deletes!? */
254 p_start = first_command_line;
261 p_sline = p_input_line;
266 for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
268 indent += 8 - (indent % 8);
272 for (t=s; isdigit((unsigned char)*t) || *t == ','; t++)
274 this_is_a_command = (isdigit((unsigned char)*s) &&
275 (*t == 'd' || *t == 'c' || *t == 'a') );
276 if (first_command_line < 0L && this_is_a_command) {
277 first_command_line = this_line;
278 fcl_line = p_input_line;
279 p_indent = indent; /* assume this for now */
281 if (!stars_last_line && strnEQ(s, "*** ", 4))
282 oldtmp = savestr(s+4);
283 else if (strnEQ(s, "--- ", 4))
284 newtmp = savestr(s+4);
285 else if (strnEQ(s, "+++ ", 4))
286 oldtmp = savestr(s+4); /* pretend it is the old name */
287 else if (strnEQ(s, "Index:", 6))
288 indtmp = savestr(s+6);
289 else if (strnEQ(s, "Prereq:", 7)) {
290 for (t = s + 7; isspace((unsigned char)*t); t++)
292 revision = savestr(t);
293 for (t = revision; *t && !isspace((unsigned char)*t);
302 if ((!diff_type || diff_type == ED_DIFF) &&
303 first_command_line >= 0L &&
306 p_start = first_command_line;
311 if ((!diff_type || diff_type == UNI_DIFF) &&
312 strnEQ(s, "@@ -", 4)) {
314 ok_to_create_file = TRUE;
317 p_sline = p_input_line;
321 stars_this_line = strnEQ(s, "********", 8);
322 if ((!diff_type || diff_type == CONTEXT_DIFF) &&
324 strnEQ(s, "*** ", 4)) {
326 ok_to_create_file = TRUE;
328 * If this is a new context diff the character just
329 * before the newline is a '*'.
334 p_start = previous_line;
335 p_sline = p_input_line - 1;
336 retval = (*(s-1) == '*' ?
337 NEW_CONTEXT_DIFF : CONTEXT_DIFF);
340 if ((!diff_type || diff_type == NORMAL_DIFF) &&
341 last_line_was_command &&
342 (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
343 p_start = previous_line;
344 p_sline = p_input_line - 1;
346 retval = NORMAL_DIFF;
352 if (indtmp != Nullch)
353 indname = fetchname(indtmp, strippath,
355 if (oldtmp != Nullch)
356 oldname = fetchname(oldtmp, strippath,
358 if (newtmp != Nullch)
359 newname = fetchname(newtmp, strippath,
361 if (index_first && indname)
362 filearg[0] = savestr(indname);
363 else if (oldname && newname) {
364 if (strlen(oldname) < strlen(newname))
365 filearg[0] = savestr(oldname);
367 filearg[0] = savestr(newname);
369 filearg[0] = savestr(indname);
371 filearg[0] = savestr(oldname);
373 filearg[0] = savestr(newname);
379 if (filearg[0] != Nullch)
380 bestguess = savestr(filearg[0]);
381 else if (indtmp != Nullch)
382 bestguess = fetchname(indtmp, strippath, TRUE);
384 if (oldtmp != Nullch)
385 oldname = fetchname(oldtmp, strippath, TRUE);
386 if (newtmp != Nullch)
387 newname = fetchname(newtmp, strippath, TRUE);
388 if (oldname && newname) {
389 if (strlen(oldname) < strlen(newname))
390 bestguess = savestr(oldname);
392 bestguess = savestr(newname);
395 bestguess = savestr(oldname);
397 bestguess = savestr(newname);
399 if (indtmp != Nullch)
401 if (oldtmp != Nullch)
403 if (newtmp != Nullch)
405 if (indname != Nullch)
407 if (oldname != Nullch)
409 if (newname != Nullch)
415 * Remember where this patch ends so we know where to start up again.
418 next_intuit_at(long file_pos, long file_line)
425 * Basically a verbose fseek() to the actual diff listing.
428 skip_to(long file_pos, long file_line)
432 assert(p_base <= file_pos);
433 if (verbose && p_base < file_pos) {
434 Fseek(pfp, p_base, 0);
435 say1("The text leading up to this was:\n--------------------------\n");
436 while (ftell(pfp) < file_pos) {
437 ret = fgets(buf, sizeof buf, pfp);
438 assert(ret != Nullch);
441 say1("--------------------------\n");
444 Fseek(pfp, file_pos, 0);
445 p_input_line = file_line - 1;
449 * Make this a function for better debugging.
454 fatal3("malformed patch at line %ld: %s", p_input_line, buf);
455 /* about as informative as "Syntax error" in C */
459 * True if the line has been discarded (i.e. it is a line saying
460 * "\ No newline at end of file".)
463 remove_special_line(void)
471 } while (c != EOF && c != '\n');
477 fseek(pfp, -1, SEEK_CUR);
483 * True if there is more of the current diff listing to process.
490 Reg2 int context = 0;
493 if (p_end == p_efake)
494 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 long line_beginning = ftell(pfp);
505 /* file pos of the current line */
506 LINENUM repl_beginning = 0; /* index of --- line */
507 Reg4 LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */
508 Reg5 LINENUM fillsrc; /* index of first line to copy */
509 Reg6 LINENUM filldst; /* index of first missing line */
510 bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
511 Reg9 bool repl_could_be_missing = TRUE;
512 /* no + or ! lines in this hunk */
513 bool repl_missing = FALSE; /* we are now backtracking */
514 long repl_backtrack_position = 0;
515 /* file pos of first repl line */
516 LINENUM repl_patch_line; /* input line number for same */
517 Reg7 LINENUM ptrn_copiable = 0;
518 /* # of copiable lines in ptrn */
520 ret = pgets(buf, sizeof buf, pfp);
522 if (ret == Nullch || strnNE(buf, "********", 8)) {
523 next_intuit_at(line_beginning,p_input_line);
527 p_hunk_beg = p_input_line + 1;
528 while (p_end < p_max) {
529 line_beginning = ftell(pfp);
530 ret = pgets(buf, sizeof buf, pfp);
533 if (p_max - p_end < 4)
534 Strcpy(buf, " \n"); /* assume blank lines got chopped */
536 if (repl_beginning && repl_could_be_missing) {
540 fatal1("unexpected end of file in patch\n");
544 assert(p_end < hunkmax);
545 p_Char[p_end] = *buf;
547 p_line[(short)p_end] = Nullch;
549 p_line[p_end] = Nullch;
553 if (strnEQ(buf, "********", 8)) {
554 if (repl_beginning && repl_could_be_missing) {
559 fatal2("unexpected end of hunk at line %ld\n",
563 if (repl_beginning && repl_could_be_missing) {
567 fatal3("unexpected *** at line %ld: %s", p_input_line, buf);
570 p_line[p_end] = savestr(buf);
575 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
578 if (strnEQ(s,"0,0",3))
580 p_first = (LINENUM) atol(s);
581 while (isdigit((unsigned char)*s)) s++;
583 for (; *s && !isdigit((unsigned char)*s); s++) ;
586 p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
594 p_max = p_ptrn_lines + 6; /* we need this much at least */
595 while (p_max >= hunkmax)
601 if (repl_beginning ||
602 (p_end != p_ptrn_lines + 1 + (p_Char[p_end-1] == '\n')))
605 /* `old' lines were omitted - set up to fill */
606 /* them in from 'new' context lines. */
607 p_end = p_ptrn_lines + 1;
610 fillcnt = p_ptrn_lines;
613 if (repl_beginning) {
614 if (repl_could_be_missing){
619 "duplicate \"---\" at line %ld--check line numbers at line %ld\n",
620 p_input_line, p_hunk_beg + repl_beginning);
624 "%s \"---\" at line %ld--check line numbers at line %ld\n",
625 (p_end <= p_ptrn_lines
628 p_input_line, p_hunk_beg);
632 repl_beginning = p_end;
633 repl_backtrack_position = ftell(pfp);
634 repl_patch_line = p_input_line;
635 p_line[p_end] = savestr(buf);
641 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
644 p_newfirst = (LINENUM) atol(s);
645 while (isdigit((unsigned char)*s)) s++;
647 for (; *s && !isdigit((unsigned char)*s); s++) ;
650 p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
658 p_max = p_repl_lines + p_end;
659 if (p_max > MAXHUNKSIZE)
660 fatal4("hunk too large (%ld lines) at line %ld: %s",
661 p_max, p_input_line, buf);
662 while (p_max >= hunkmax)
664 if (p_repl_lines != ptrn_copiable
665 && (p_context != 0 || p_repl_lines != 1))
666 repl_could_be_missing = FALSE;
671 repl_could_be_missing = FALSE;
673 if (buf[1] == '\n' && canonicalize)
675 if (!isspace((unsigned char)buf[1]) && buf[1] != '>' && buf[1] != '<' &&
676 repl_beginning && repl_could_be_missing) {
681 if (context < p_context)
685 p_line[p_end] = savestr(buf+2);
690 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;
700 case '\t': case '\n': /* assume the 2 spaces got eaten */
701 if (repl_beginning && repl_could_be_missing &&
702 (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
706 p_line[p_end] = savestr(buf);
711 if (p_end != p_ptrn_lines + 1) {
712 ptrn_spaces_eaten |= (repl_beginning != 0);
720 if (!isspace((unsigned char)buf[1]) &&
721 repl_beginning && repl_could_be_missing) {
728 p_line[p_end] = savestr(buf+2);
735 if (repl_beginning && repl_could_be_missing) {
741 /* set up p_len for strncmp() so we don't have to */
742 /* assume null termination */
744 p_len[p_end] = strlen(p_line[p_end]);
750 if (p_end >=0 && !repl_beginning)
751 fatal2("no --- found in patch at line %ld\n", pch_hunk_beg());
755 /* reset state back to just after --- */
756 p_input_line = repl_patch_line;
757 for (p_end--; p_end > repl_beginning; p_end--)
759 Fseek(pfp, repl_backtrack_position, 0);
761 /* redundant 'new' context lines were omitted - set */
762 /* up to fill them in from the old file context */
763 if (!p_context && p_repl_lines == 1) {
768 filldst = repl_beginning+1;
769 fillcnt = p_repl_lines;
772 else if (!p_context && fillcnt == 1) {
773 /* the first hunk was a null hunk with no context */
774 /* and we were expecting one line -- fix it up. */
775 while (filldst < p_end) {
776 p_line[filldst] = p_line[filldst+1];
777 p_Char[filldst] = p_Char[filldst+1];
778 p_len[filldst] = p_len[filldst+1];
782 repl_beginning--; /* this doesn't need to be fixed */
785 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;
800 /* if there were omitted context lines, fill them in now */
802 p_bfake = filldst; /* remember where not to free() */
803 p_efake = filldst + fillcnt - 1;
804 while (fillcnt-- > 0) {
805 while (fillsrc <= p_end && p_Char[fillsrc] != ' ')
808 fatal2("replacement text or line numbers mangled in hunk at line %ld\n",
810 p_line[filldst] = p_line[fillsrc];
811 p_Char[filldst] = p_Char[fillsrc];
812 p_len[filldst] = p_len[fillsrc];
813 fillsrc++; filldst++;
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 assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
824 assert(filldst==p_end+1 || filldst==repl_beginning);
827 if (p_line[p_end] != NULL)
829 if (remove_special_line()) {
831 (p_line[p_end])[p_len[p_end]] = 0;
835 else if (diff_type == UNI_DIFF) {
836 long line_beginning = ftell(pfp);
837 /* file pos of the current line */
838 Reg4 LINENUM fillsrc; /* index of old lines */
839 Reg5 LINENUM filldst; /* index of new lines */
842 ret = pgets(buf, sizeof buf, pfp);
844 if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
845 next_intuit_at(line_beginning,p_input_line);
851 p_first = (LINENUM) atol(s);
852 while (isdigit((unsigned char)*s)) s++;
854 p_ptrn_lines = (LINENUM) atol(++s);
855 while (isdigit((unsigned char)*s)) s++;
859 if (*s != '+' || !*++s)
861 p_newfirst = (LINENUM) atol(s);
862 while (isdigit((unsigned char)*s)) s++;
864 p_repl_lines = (LINENUM) atol(++s);
865 while (isdigit((unsigned char)*s)) s++;
872 p_first++; /* do append rather than insert */
873 p_max = p_ptrn_lines + p_repl_lines + 1;
874 while (p_max >= hunkmax)
877 filldst = fillsrc + p_ptrn_lines;
878 p_end = filldst + p_repl_lines;
879 Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
880 p_line[0] = savestr(buf);
886 Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
887 p_line[filldst] = savestr(buf);
892 p_Char[filldst++] = '=';
895 p_hunk_beg = p_input_line + 1;
896 while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
897 line_beginning = ftell(pfp);
898 ret = pgets(buf, sizeof buf, pfp);
901 if (p_max - filldst < 3)
902 Strcpy(buf, " \n"); /* assume blank lines got chopped */
904 fatal1("unexpected end of file in patch\n");
907 if (*buf == '\t' || *buf == '\n') {
908 ch = ' '; /* assume the space got eaten */
916 while (--filldst > p_ptrn_lines)
917 free(p_line[filldst]);
923 if (fillsrc > p_ptrn_lines) {
928 p_Char[fillsrc] = ch;
930 p_len[fillsrc++] = strlen(s);
931 if (fillsrc > p_ptrn_lines) {
932 if (remove_special_line()) {
933 p_len[fillsrc - 1] -= 1;
934 s[p_len[fillsrc - 1]] = 0;
942 if (fillsrc > p_ptrn_lines) {
944 while (--filldst > p_ptrn_lines)
945 free(p_line[filldst]);
950 p_Char[fillsrc] = ch;
952 p_len[fillsrc++] = strlen(s);
955 while (--filldst > p_ptrn_lines)
956 free(p_line[filldst]);
962 if (filldst > p_end) {
964 while (--filldst > p_ptrn_lines)
965 free(p_line[filldst]);
969 p_Char[filldst] = ch;
971 p_len[filldst++] = strlen(s);
972 if (fillsrc > p_ptrn_lines) {
973 if (remove_special_line()) {
974 p_len[filldst - 1] -= 1;
975 s[p_len[filldst - 1]] = 0;
983 if (ch != ' ' && context > 0) {
984 if (context < p_context)
990 else { /* normal diff--fake it up */
994 long line_beginning = ftell(pfp);
997 ret = pgets(buf, sizeof buf, pfp);
999 if (ret == Nullch || !isdigit((unsigned char)*buf)) {
1000 next_intuit_at(line_beginning,p_input_line);
1003 p_first = (LINENUM)atol(buf);
1004 for (s=buf; isdigit((unsigned char)*s); s++) ;
1006 p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
1007 while (isdigit((unsigned char)*s)) s++;
1010 p_ptrn_lines = (*s != 'a');
1012 if (hunk_type == 'a')
1013 p_first++; /* do append rather than insert */
1014 min = (LINENUM)atol(++s);
1015 for (; isdigit((unsigned char)*s); s++) ;
1017 max = (LINENUM)atol(++s);
1020 if (hunk_type == 'd')
1022 p_end = p_ptrn_lines + 1 + max - min + 1;
1023 if (p_end > MAXHUNKSIZE)
1024 fatal4("hunk too large (%ld lines) at line %ld: %s",
1025 p_end, p_input_line, buf);
1026 while (p_end >= hunkmax)
1029 p_repl_lines = max - min + 1;
1030 Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
1031 p_line[0] = savestr(buf);
1037 for (i=1; i<=p_ptrn_lines; i++) {
1038 ret = pgets(buf, sizeof buf, pfp);
1041 fatal2("unexpected end of file in patch at line %ld\n",
1044 fatal2("< expected at line %ld of patch\n", p_input_line);
1045 p_line[i] = savestr(buf+2);
1050 p_len[i] = strlen(p_line[i]);
1054 if (remove_special_line()) {
1056 (p_line[i-1])[p_len[i-1]] = 0;
1059 if (hunk_type == 'c') {
1060 ret = pgets(buf, sizeof buf, pfp);
1063 fatal2("unexpected end of file in patch at line %ld\n",
1066 fatal2("--- expected at line %ld of patch\n", p_input_line);
1068 Sprintf(buf, "--- %ld,%ld\n", min, max);
1069 p_line[i] = savestr(buf);
1075 for (i++; i<=p_end; i++) {
1076 ret = pgets(buf, sizeof buf, pfp);
1079 fatal2("unexpected end of file in patch at line %ld\n",
1082 fatal2("> expected at line %ld of patch\n", p_input_line);
1083 p_line[i] = savestr(buf+2);
1088 p_len[i] = strlen(p_line[i]);
1092 if (remove_special_line()) {
1094 (p_line[i-1])[p_len[i-1]] = 0;
1097 if (reverse) /* backwards patch? */
1099 say1("Not enough memory to swap next hunk!\n");
1105 for (i=0; i <= p_end; i++) {
1106 if (i == p_ptrn_lines)
1110 fprintf(stderr, "%3d %c %c %s", i, p_Char[i], special, p_line[i]);
1115 if (p_end+1 < hunkmax) /* paranoia reigns supreme... */
1116 p_Char[p_end+1] = '^'; /* add a stopper for apply_hunk */
1121 * Input a line from the patch file, worrying about indentation.
1124 pgets(char *bf, int sz, FILE *fp)
1126 char *ret = fgets(bf, sz, fp);
1128 Reg2 int indent = 0;
1130 if (p_indent && ret != Nullch) {
1132 indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X');
1135 indent += 8 - (indent % 7);
1146 * Reverse the old and new portions of the current hunk.
1151 char **tp_line; /* the text of the hunk */
1152 short *tp_len; /* length of each line */
1153 char *tp_char; /* +, -, and ! */
1156 bool blankline = FALSE;
1160 p_first = p_newfirst;
1163 /* make a scratch copy */
1168 p_line = Null(char**); /* force set_hunkmax to allocate again */
1169 p_len = Null(short*);
1172 if (p_line == Null(char**) || p_len == Null(short*) || p_Char == Nullch) {
1174 if (p_line == Null(char**))
1175 free((char*)p_line);
1177 if (p_len == Null(short*))
1181 if (p_Char == Nullch)
1182 free((char*)p_Char);
1184 return FALSE; /* not enough memory to swap hunk! */
1187 /* now turn the new into the old */
1189 i = p_ptrn_lines + 1;
1190 if (tp_char[i] == '\n') { /* account for possible blank line */
1194 if (p_efake >= 0) { /* fix non-freeable ptr range */
1202 for (n=0; i <= p_end; i++,n++) {
1203 p_line[n] = tp_line[i];
1204 p_Char[n] = tp_char[i];
1205 if (p_Char[n] == '+')
1207 p_len[n] = tp_len[i];
1210 i = p_ptrn_lines + 1;
1211 p_line[n] = tp_line[i];
1212 p_Char[n] = tp_char[i];
1213 p_len[n] = tp_len[i];
1216 assert(p_Char[0] == '=');
1218 for (s=p_line[0]; *s; s++)
1222 /* now turn the old into the new */
1224 assert(tp_char[0] == '*');
1226 for (s=tp_line[0]; *s; s++)
1229 for (i=0; n <= p_end; i++,n++) {
1230 p_line[n] = tp_line[i];
1231 p_Char[n] = tp_char[i];
1232 if (p_Char[n] == '-')
1234 p_len[n] = tp_len[i];
1236 assert(i == p_ptrn_lines + 1);
1238 p_ptrn_lines = p_repl_lines;
1241 if (tp_line == Null(char**))
1242 free((char*)tp_line);
1243 if (tp_len == Null(short*))
1244 free((char*)tp_len);
1246 if (tp_char == Nullch)
1247 free((char*)tp_char);
1252 * Return the specified line position in the old file of the old context.
1261 * Return the number of lines of old context.
1264 pch_ptrn_lines(void)
1266 return p_ptrn_lines;
1270 * Return the probable line position in the new file of the first line.
1279 * Return the number of lines in the replacement text including context.
1282 pch_repl_lines(void)
1284 return p_repl_lines;
1288 * Return the number of lines in the whole hunk.
1297 * Return the number of context lines before the first changed line.
1306 * Return the length of a particular patch line.
1309 pch_line_len(LINENUM line)
1315 * Return the control character (+, -, *, !, etc) for a patch line.
1318 pch_char(LINENUM line)
1320 return p_Char[line];
1324 * Return a pointer to a particular patch line.
1327 pfetch(LINENUM line)
1329 return p_line[line];
1333 * Return where in the patch file this hunk began, for error messages.
1342 * Apply an ed script by feeding ed itself.
1348 Reg2 long beginning_of_this_line;
1349 Reg3 bool this_line_is_command = FALSE;
1352 if (!skip_rest_of_patch) {
1354 copy_file(filearg[0], TMPOUTNAME);
1356 Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
1358 Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
1359 pipefp = popen(buf, "w");
1362 beginning_of_this_line = ftell(pfp);
1363 if (pgets(buf, sizeof buf, pfp) == Nullch) {
1364 next_intuit_at(beginning_of_this_line, p_input_line);
1368 for (t=buf; isdigit((unsigned char)*t) || *t == ','; t++)
1370 this_line_is_command = (isdigit((unsigned char)*buf) &&
1371 (*t == 'd' || *t == 'c' || *t == 'a') );
1372 if (this_line_is_command) {
1373 if (!skip_rest_of_patch)
1376 while (pgets(buf, sizeof buf, pfp) != Nullch) {
1378 if (!skip_rest_of_patch)
1380 if (strEQ(buf, ".\n"))
1386 next_intuit_at(beginning_of_this_line,p_input_line);
1390 if (skip_rest_of_patch)
1392 fprintf(pipefp, "w\n");
1393 fprintf(pipefp, "q\n");
1397 if (move_file(TMPOUTNAME, outname) < 0) {
1399 chmod(TMPOUTNAME, filemode);
1402 chmod(outname, filemode);