1 /* $FreeBSD: src/gnu/usr.bin/patch/pch.c,v 1.16.2.3 2002/04/30 20:40:02 gad Exp $
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;
80 /* Open the patch file at the beginning of time. */
83 open_patch_file(char *filename)
85 if (filename == Nullch || !*filename || strEQ(filename, "-")) {
86 pfp = fopen(TMPPATNAME, "w");
88 pfatal2("can't create %s", TMPPATNAME);
89 while (fgets(buf, sizeof buf, stdin) != Nullch)
92 filename = TMPPATNAME;
94 pfp = fopen(filename, "r");
96 pfatal2("patch file %s not found", filename);
97 Fstat(fileno(pfp), &filestat);
98 p_filesize = filestat.st_size;
99 next_intuit_at(0L,1L); /* start at the beginning */
103 /* Make sure our dynamically realloced tables are malloced to begin with. */
109 if (p_line == Null(char**))
110 p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
111 if (p_len == Null(short*))
112 p_len = (short*) malloc((MEM)hunkmax * sizeof(short));
114 if (p_Char == Nullch)
115 p_Char = (char*) malloc((MEM)hunkmax * sizeof(char));
118 /* Enlarge the arrays containing the current hunk of patch. */
125 * Note that on most systems, only the p_line array ever gets fresh memory
126 * since p_len can move into p_line's old space, and p_Char can move into
127 * p_len's old space. Not on PDP-11's however. But it doesn't matter.
129 assert(p_line != Null(char**) && p_len != Null(short*) && p_Char != Nullch);
131 p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
132 p_len = (short*) realloc((char*)p_len, (MEM)hunkmax * sizeof(short));
133 p_Char = (char*) realloc((char*)p_Char, (MEM)hunkmax * sizeof(char));
135 if (p_line != Null(char**) && p_len != Null(short*) && p_Char != Nullch)
138 fatal1("out of memory\n");
139 out_of_mem = TRUE; /* whatever is null will be allocated again */
140 /* from within plan_a(), of all places */
144 * True if the remainder of the patch file contains a diff of some sort.
147 there_is_another_patch(void)
149 if (p_base != 0L && p_base >= p_filesize) {
156 diff_type = intuit_diff_type();
160 say1(" Ignoring the trailing garbage.\ndone\n");
163 say1(" I can't seem to find a patch in there anywhere.\n");
167 say3(" %sooks like %s to me...\n",
168 (p_base == 0L ? "L" : "The next patch l"),
169 diff_type == UNI_DIFF ? "a unified diff" :
170 diff_type == CONTEXT_DIFF ? "a context diff" :
171 diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
172 diff_type == NORMAL_DIFF ? "a normal diff" :
174 if (p_indent && verbose)
175 say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
176 skip_to(p_start,p_sline);
177 while (filearg[0] == Nullch) {
178 if (force || batch || skip_rest_of_patch) {
179 say1("No file to patch. Skipping...\n");
180 filearg[0] = savestr(bestguess);
181 skip_rest_of_patch = TRUE;
184 (void) ask1("File to patch: ");
188 bestguess = savestr(buf);
189 filearg[0] = fetchname(buf, 0, FALSE);
191 if (filearg[0] == Nullch) {
192 if (ask1("No file found--skip this patch? [n] ")) {
198 say1("Skipping patch...\n");
199 filearg[0] = fetchname(bestguess, 0, TRUE);
200 skip_rest_of_patch = TRUE;
208 * Determine what kind of diff is in the remaining part of the patch file.
211 intuit_diff_type(void)
213 Reg4 long this_line = 0;
214 Reg5 long previous_line;
215 Reg6 long first_command_line = -1;
217 Reg7 bool last_line_was_command = FALSE;
218 Reg8 bool this_is_a_command = FALSE;
219 Reg9 bool stars_last_line = FALSE;
220 Reg10 bool stars_this_line = FALSE;
224 char *indtmp = Nullch;
225 char *oldtmp = Nullch;
226 char *newtmp = Nullch;
227 char *indname = Nullch;
228 char *oldname = Nullch;
229 char *newname = Nullch;
231 bool no_filearg = (filearg[0] == Nullch);
232 extern int index_first;
234 ok_to_create_file = FALSE;
235 Fseek(pfp, p_base, 0);
236 p_input_line = p_bline - 1;
238 previous_line = this_line;
239 last_line_was_command = this_is_a_command;
240 stars_last_line = stars_this_line;
241 this_line = ftell(pfp);
244 if (fgets(buf, sizeof buf, pfp) == Nullch) {
245 if (first_command_line >= 0L) {
246 /* nothing but deletes!? */
247 p_start = first_command_line;
254 p_sline = p_input_line;
259 for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
261 indent += 8 - (indent % 8);
265 for (t=s; isdigit((unsigned char)*t) || *t == ','; t++) ;
266 this_is_a_command = (isdigit((unsigned char)*s) &&
267 (*t == 'd' || *t == 'c' || *t == 'a') );
268 if (first_command_line < 0L && this_is_a_command) {
269 first_command_line = this_line;
270 fcl_line = p_input_line;
271 p_indent = indent; /* assume this for now */
273 if (!stars_last_line && strnEQ(s, "*** ", 4))
274 oldtmp = savestr(s+4);
275 else if (strnEQ(s, "--- ", 4))
276 newtmp = savestr(s+4);
277 else if (strnEQ(s, "+++ ", 4))
278 oldtmp = savestr(s+4); /* pretend it is the old name */
279 else if (strnEQ(s, "Index:", 6))
280 indtmp = savestr(s+6);
281 else if (strnEQ(s, "Prereq:", 7)) {
282 for (t=s+7; isspace((unsigned char)*t); t++) ;
283 revision = savestr(t);
284 for (t=revision; *t && !isspace((unsigned char)*t); t++) ;
291 if ((!diff_type || diff_type == ED_DIFF) &&
292 first_command_line >= 0L &&
295 p_start = first_command_line;
300 if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
302 ok_to_create_file = TRUE;
305 p_sline = p_input_line;
309 stars_this_line = strnEQ(s, "********", 8);
310 if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
311 strnEQ(s, "*** ", 4)) {
313 ok_to_create_file = TRUE;
314 /* if this is a new context diff the character just before */
315 /* the newline is a '*'. */
319 p_start = previous_line;
320 p_sline = p_input_line - 1;
321 retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
324 if ((!diff_type || diff_type == NORMAL_DIFF) &&
325 last_line_was_command &&
326 (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
327 p_start = previous_line;
328 p_sline = p_input_line - 1;
330 retval = NORMAL_DIFF;
336 if (indtmp != Nullch)
337 indname = fetchname(indtmp, strippath, ok_to_create_file);
338 if (oldtmp != Nullch)
339 oldname = fetchname(oldtmp, strippath, ok_to_create_file);
340 if (newtmp != Nullch)
341 newname = fetchname(newtmp, strippath, ok_to_create_file);
342 if (index_first && indname)
343 filearg[0] = savestr(indname);
344 else if (oldname && newname) {
345 if (strlen(oldname) < strlen(newname))
346 filearg[0] = savestr(oldname);
348 filearg[0] = savestr(newname);
350 filearg[0] = savestr(indname);
352 filearg[0] = savestr(oldname);
354 filearg[0] = savestr(newname);
360 if (filearg[0] != Nullch)
361 bestguess = savestr(filearg[0]);
362 else if (indtmp != Nullch)
363 bestguess = fetchname(indtmp, strippath, TRUE);
365 if (oldtmp != Nullch)
366 oldname = fetchname(oldtmp, strippath, TRUE);
367 if (newtmp != Nullch)
368 newname = fetchname(newtmp, strippath, TRUE);
369 if (oldname && newname) {
370 if (strlen(oldname) < strlen(newname))
371 bestguess = savestr(oldname);
373 bestguess = savestr(newname);
376 bestguess = savestr(oldname);
378 bestguess = savestr(newname);
380 if (indtmp != Nullch)
382 if (oldtmp != Nullch)
384 if (newtmp != Nullch)
386 if (indname != Nullch)
388 if (oldname != Nullch)
390 if (newname != Nullch)
396 * Remember where this patch ends so we know where to start up again.
399 next_intuit_at(long file_pos, long file_line)
406 * Basically a verbose fseek() to the actual diff listing.
409 skip_to(long file_pos, long file_line)
413 assert(p_base <= file_pos);
414 if (verbose && p_base < file_pos) {
415 Fseek(pfp, p_base, 0);
416 say1("The text leading up to this was:\n--------------------------\n");
417 while (ftell(pfp) < file_pos) {
418 ret = fgets(buf, sizeof buf, pfp);
419 assert(ret != Nullch);
422 say1("--------------------------\n");
425 Fseek(pfp, file_pos, 0);
426 p_input_line = file_line - 1;
429 /* Make this a function for better debugging. */
433 fatal3("malformed patch at line %ld: %s", p_input_line, buf);
434 /* about as informative as "Syntax error" in C */
438 * True if the line has been discarded (i.e. it is a line saying
439 * "\ No newline at end of file".)
442 remove_special_line(void)
450 } while (c != EOF && c != '\n');
456 fseek(pfp, -1, SEEK_CUR);
461 /* True if there is more of the current diff listing to process. */
468 Reg2 int context = 0;
471 if (p_end == p_efake)
472 p_end = p_bfake; /* don't free twice */
480 p_max = hunkmax; /* gets reduced when --- found */
481 if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
482 long line_beginning = ftell(pfp);
483 /* file pos of the current line */
484 LINENUM repl_beginning = 0; /* index of --- line */
485 Reg4 LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */
486 Reg5 LINENUM fillsrc; /* index of first line to copy */
487 Reg6 LINENUM filldst; /* index of first missing line */
488 bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
489 Reg9 bool repl_could_be_missing = TRUE;
490 /* no + or ! lines in this hunk */
491 bool repl_missing = FALSE; /* we are now backtracking */
492 long repl_backtrack_position = 0;
493 /* file pos of first repl line */
494 LINENUM repl_patch_line; /* input line number for same */
495 Reg7 LINENUM ptrn_copiable = 0;
496 /* # of copiable lines in ptrn */
498 ret = pgets(buf, sizeof buf, pfp);
500 if (ret == Nullch || strnNE(buf, "********", 8)) {
501 next_intuit_at(line_beginning,p_input_line);
505 p_hunk_beg = p_input_line + 1;
506 while (p_end < p_max) {
507 line_beginning = ftell(pfp);
508 ret = pgets(buf, sizeof buf, pfp);
511 if (p_max - p_end < 4)
512 Strcpy(buf, " \n"); /* assume blank lines got chopped */
514 if (repl_beginning && repl_could_be_missing) {
518 fatal1("unexpected end of file in patch\n");
522 assert(p_end < hunkmax);
523 p_Char[p_end] = *buf;
525 p_line[(short)p_end] = Nullch;
527 p_line[p_end] = Nullch;
531 if (strnEQ(buf, "********", 8)) {
532 if (repl_beginning && repl_could_be_missing) {
537 fatal2("unexpected end of hunk at line %ld\n",
541 if (repl_beginning && repl_could_be_missing) {
545 fatal3("unexpected *** at line %ld: %s", p_input_line, buf);
548 p_line[p_end] = savestr(buf);
553 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
556 if (strnEQ(s,"0,0",3))
558 p_first = (LINENUM) atol(s);
559 while (isdigit((unsigned char)*s)) s++;
561 for (; *s && !isdigit((unsigned char)*s); s++) ;
564 p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
572 p_max = p_ptrn_lines + 6; /* we need this much at least */
573 while (p_max >= hunkmax)
579 if (repl_beginning ||
580 (p_end != p_ptrn_lines + 1 + (p_Char[p_end-1] == '\n')))
583 /* `old' lines were omitted - set up to fill */
584 /* them in from 'new' context lines. */
585 p_end = p_ptrn_lines + 1;
588 fillcnt = p_ptrn_lines;
591 if (repl_beginning) {
592 if (repl_could_be_missing){
597 "duplicate \"---\" at line %ld--check line numbers at line %ld\n",
598 p_input_line, p_hunk_beg + repl_beginning);
602 "%s \"---\" at line %ld--check line numbers at line %ld\n",
603 (p_end <= p_ptrn_lines
606 p_input_line, p_hunk_beg);
610 repl_beginning = p_end;
611 repl_backtrack_position = ftell(pfp);
612 repl_patch_line = p_input_line;
613 p_line[p_end] = savestr(buf);
619 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
622 p_newfirst = (LINENUM) atol(s);
623 while (isdigit((unsigned char)*s)) s++;
625 for (; *s && !isdigit((unsigned char)*s); s++) ;
628 p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
636 p_max = p_repl_lines + p_end;
637 if (p_max > MAXHUNKSIZE)
638 fatal4("hunk too large (%ld lines) at line %ld: %s",
639 p_max, p_input_line, buf);
640 while (p_max >= hunkmax)
642 if (p_repl_lines != ptrn_copiable
643 && (p_context != 0 || p_repl_lines != 1))
644 repl_could_be_missing = FALSE;
649 repl_could_be_missing = FALSE;
651 if (buf[1] == '\n' && canonicalize)
653 if (!isspace((unsigned char)buf[1]) && buf[1] != '>' && buf[1] != '<' &&
654 repl_beginning && repl_could_be_missing) {
659 if (context < p_context)
663 p_line[p_end] = savestr(buf+2);
668 if (p_end == p_ptrn_lines)
670 if (remove_special_line()) {
673 len = strlen(p_line[p_end]) - 1;
674 (p_line[p_end])[len] = 0;
678 case '\t': case '\n': /* assume the 2 spaces got eaten */
679 if (repl_beginning && repl_could_be_missing &&
680 (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
684 p_line[p_end] = savestr(buf);
689 if (p_end != p_ptrn_lines + 1) {
690 ptrn_spaces_eaten |= (repl_beginning != 0);
698 if (!isspace((unsigned char)buf[1]) &&
699 repl_beginning && repl_could_be_missing) {
706 p_line[p_end] = savestr(buf+2);
713 if (repl_beginning && repl_could_be_missing) {
719 /* set up p_len for strncmp() so we don't have to */
720 /* assume null termination */
722 p_len[p_end] = strlen(p_line[p_end]);
728 if (p_end >=0 && !repl_beginning)
729 fatal2("no --- found in patch at line %ld\n", pch_hunk_beg());
733 /* reset state back to just after --- */
734 p_input_line = repl_patch_line;
735 for (p_end--; p_end > repl_beginning; p_end--)
737 Fseek(pfp, repl_backtrack_position, 0);
739 /* redundant 'new' context lines were omitted - set */
740 /* up to fill them in from the old file context */
741 if (!p_context && p_repl_lines == 1) {
746 filldst = repl_beginning+1;
747 fillcnt = p_repl_lines;
750 else if (!p_context && fillcnt == 1) {
751 /* the first hunk was a null hunk with no context */
752 /* and we were expecting one line -- fix it up. */
753 while (filldst < p_end) {
754 p_line[filldst] = p_line[filldst+1];
755 p_Char[filldst] = p_Char[filldst+1];
756 p_len[filldst] = p_len[filldst+1];
760 repl_beginning--; /* this doesn't need to be fixed */
763 p_first++; /* do append rather than insert */
768 if (diff_type == CONTEXT_DIFF &&
769 (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
772 "(Fascinating--this is really a new-style context diff but without",
773 "the telltale extra asterisks on the *** line that usually indicate",
774 "the new style...)");
775 diff_type = NEW_CONTEXT_DIFF;
778 /* if there were omitted context lines, fill them in now */
780 p_bfake = filldst; /* remember where not to free() */
781 p_efake = filldst + fillcnt - 1;
782 while (fillcnt-- > 0) {
783 while (fillsrc <= p_end && p_Char[fillsrc] != ' ')
786 fatal2("replacement text or line numbers mangled in hunk at line %ld\n",
788 p_line[filldst] = p_line[fillsrc];
789 p_Char[filldst] = p_Char[fillsrc];
790 p_len[filldst] = p_len[fillsrc];
791 fillsrc++; filldst++;
793 while (fillsrc <= p_end && fillsrc != repl_beginning &&
794 p_Char[fillsrc] != ' ')
798 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
799 fillsrc,filldst,repl_beginning,p_end+1);
801 assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
802 assert(filldst==p_end+1 || filldst==repl_beginning);
805 if (p_line[p_end] != NULL)
807 if (remove_special_line()) {
809 (p_line[p_end])[p_len[p_end]] = 0;
813 else if (diff_type == UNI_DIFF) {
814 long line_beginning = ftell(pfp);
815 /* file pos of the current line */
816 Reg4 LINENUM fillsrc; /* index of old lines */
817 Reg5 LINENUM filldst; /* index of new lines */
820 ret = pgets(buf, sizeof buf, pfp);
822 if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
823 next_intuit_at(line_beginning,p_input_line);
829 p_first = (LINENUM) atol(s);
830 while (isdigit((unsigned char)*s)) s++;
832 p_ptrn_lines = (LINENUM) atol(++s);
833 while (isdigit((unsigned char)*s)) s++;
837 if (*s != '+' || !*++s)
839 p_newfirst = (LINENUM) atol(s);
840 while (isdigit((unsigned char)*s)) s++;
842 p_repl_lines = (LINENUM) atol(++s);
843 while (isdigit((unsigned char)*s)) s++;
850 p_first++; /* do append rather than insert */
851 p_max = p_ptrn_lines + p_repl_lines + 1;
852 while (p_max >= hunkmax)
855 filldst = fillsrc + p_ptrn_lines;
856 p_end = filldst + p_repl_lines;
857 Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
858 p_line[0] = savestr(buf);
864 Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
865 p_line[filldst] = savestr(buf);
870 p_Char[filldst++] = '=';
873 p_hunk_beg = p_input_line + 1;
874 while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
875 line_beginning = ftell(pfp);
876 ret = pgets(buf, sizeof buf, pfp);
879 if (p_max - filldst < 3)
880 Strcpy(buf, " \n"); /* assume blank lines got chopped */
882 fatal1("unexpected end of file in patch\n");
885 if (*buf == '\t' || *buf == '\n') {
886 ch = ' '; /* assume the space got eaten */
894 while (--filldst > p_ptrn_lines)
895 free(p_line[filldst]);
901 if (fillsrc > p_ptrn_lines) {
906 p_Char[fillsrc] = ch;
908 p_len[fillsrc++] = strlen(s);
909 if (fillsrc > p_ptrn_lines) {
910 if (remove_special_line()) {
911 p_len[fillsrc - 1] -= 1;
912 s[p_len[fillsrc - 1]] = 0;
920 if (fillsrc > p_ptrn_lines) {
922 while (--filldst > p_ptrn_lines)
923 free(p_line[filldst]);
928 p_Char[fillsrc] = ch;
930 p_len[fillsrc++] = strlen(s);
933 while (--filldst > p_ptrn_lines)
934 free(p_line[filldst]);
940 if (filldst > p_end) {
942 while (--filldst > p_ptrn_lines)
943 free(p_line[filldst]);
947 p_Char[filldst] = ch;
949 p_len[filldst++] = strlen(s);
950 if (fillsrc > p_ptrn_lines) {
951 if (remove_special_line()) {
952 p_len[filldst - 1] -= 1;
953 s[p_len[filldst - 1]] = 0;
961 if (ch != ' ' && context > 0) {
962 if (context < p_context)
968 else { /* normal diff--fake it up */
972 long line_beginning = ftell(pfp);
975 ret = pgets(buf, sizeof buf, pfp);
977 if (ret == Nullch || !isdigit((unsigned char)*buf)) {
978 next_intuit_at(line_beginning,p_input_line);
981 p_first = (LINENUM)atol(buf);
982 for (s=buf; isdigit((unsigned char)*s); s++) ;
984 p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
985 while (isdigit((unsigned char)*s)) s++;
988 p_ptrn_lines = (*s != 'a');
990 if (hunk_type == 'a')
991 p_first++; /* do append rather than insert */
992 min = (LINENUM)atol(++s);
993 for (; isdigit((unsigned char)*s); s++) ;
995 max = (LINENUM)atol(++s);
998 if (hunk_type == 'd')
1000 p_end = p_ptrn_lines + 1 + max - min + 1;
1001 if (p_end > MAXHUNKSIZE)
1002 fatal4("hunk too large (%ld lines) at line %ld: %s",
1003 p_end, p_input_line, buf);
1004 while (p_end >= hunkmax)
1007 p_repl_lines = max - min + 1;
1008 Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
1009 p_line[0] = savestr(buf);
1015 for (i=1; i<=p_ptrn_lines; i++) {
1016 ret = pgets(buf, sizeof buf, pfp);
1019 fatal2("unexpected end of file in patch at line %ld\n",
1022 fatal2("< expected at line %ld of patch\n", p_input_line);
1023 p_line[i] = savestr(buf+2);
1028 p_len[i] = strlen(p_line[i]);
1032 if (remove_special_line()) {
1034 (p_line[i-1])[p_len[i-1]] = 0;
1037 if (hunk_type == 'c') {
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);
1046 Sprintf(buf, "--- %ld,%ld\n", min, max);
1047 p_line[i] = savestr(buf);
1053 for (i++; i<=p_end; i++) {
1054 ret = pgets(buf, sizeof buf, pfp);
1057 fatal2("unexpected end of file in patch at line %ld\n",
1060 fatal2("> expected at line %ld of patch\n", p_input_line);
1061 p_line[i] = savestr(buf+2);
1066 p_len[i] = strlen(p_line[i]);
1070 if (remove_special_line()) {
1072 (p_line[i-1])[p_len[i-1]] = 0;
1075 if (reverse) /* backwards patch? */
1077 say1("Not enough memory to swap next hunk!\n");
1083 for (i=0; i <= p_end; i++) {
1084 if (i == p_ptrn_lines)
1088 fprintf(stderr, "%3d %c %c %s", i, p_Char[i], special, p_line[i]);
1093 if (p_end+1 < hunkmax) /* paranoia reigns supreme... */
1094 p_Char[p_end+1] = '^'; /* add a stopper for apply_hunk */
1099 * Input a line from the patch file, worrying about indentation.
1102 pgets(char *bf, int sz, FILE *fp)
1104 char *ret = fgets(bf, sz, fp);
1106 Reg2 int indent = 0;
1108 if (p_indent && ret != Nullch) {
1110 indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
1112 indent += 8 - (indent % 7);
1124 * Reverse the old and new portions of the current hunk.
1129 char **tp_line; /* the text of the hunk */
1130 short *tp_len; /* length of each line */
1131 char *tp_char; /* +, -, and ! */
1134 bool blankline = FALSE;
1138 p_first = p_newfirst;
1141 /* make a scratch copy */
1146 p_line = Null(char**); /* force set_hunkmax to allocate again */
1147 p_len = Null(short*);
1150 if (p_line == Null(char**) || p_len == Null(short*) || p_Char == Nullch) {
1152 if (p_line == Null(char**))
1153 free((char*)p_line);
1155 if (p_len == Null(short*))
1159 if (p_Char == Nullch)
1160 free((char*)p_Char);
1162 return FALSE; /* not enough memory to swap hunk! */
1165 /* now turn the new into the old */
1167 i = p_ptrn_lines + 1;
1168 if (tp_char[i] == '\n') { /* account for possible blank line */
1172 if (p_efake >= 0) { /* fix non-freeable ptr range */
1180 for (n=0; i <= p_end; i++,n++) {
1181 p_line[n] = tp_line[i];
1182 p_Char[n] = tp_char[i];
1183 if (p_Char[n] == '+')
1185 p_len[n] = tp_len[i];
1188 i = p_ptrn_lines + 1;
1189 p_line[n] = tp_line[i];
1190 p_Char[n] = tp_char[i];
1191 p_len[n] = tp_len[i];
1194 assert(p_Char[0] == '=');
1196 for (s=p_line[0]; *s; s++)
1200 /* now turn the old into the new */
1202 assert(tp_char[0] == '*');
1204 for (s=tp_line[0]; *s; s++)
1207 for (i=0; n <= p_end; i++,n++) {
1208 p_line[n] = tp_line[i];
1209 p_Char[n] = tp_char[i];
1210 if (p_Char[n] == '-')
1212 p_len[n] = tp_len[i];
1214 assert(i == p_ptrn_lines + 1);
1216 p_ptrn_lines = p_repl_lines;
1219 if (tp_line == Null(char**))
1220 free((char*)tp_line);
1221 if (tp_len == Null(short*))
1222 free((char*)tp_len);
1224 if (tp_char == Nullch)
1225 free((char*)tp_char);
1230 * Return the specified line position in the old file of the old context.
1239 * Return the number of lines of old context.
1242 pch_ptrn_lines(void)
1244 return p_ptrn_lines;
1248 * Return the probable line position in the new file of the first line.
1257 * Return the number of lines in the replacement text including context.
1260 pch_repl_lines(void)
1262 return p_repl_lines;
1266 * Return the number of lines in the whole hunk.
1275 * Return the number of context lines before the first changed line.
1284 * Return the length of a particular patch line.
1287 pch_line_len(LINENUM line)
1293 * Return the control character (+, -, *, !, etc) for a patch line.
1296 pch_char(LINENUM line)
1298 return p_Char[line];
1302 * Return a pointer to a particular patch line.
1305 pfetch(LINENUM line)
1307 return p_line[line];
1311 * Return where in the patch file this hunk began, for error messages.
1320 * Apply an ed script by feeding ed itself.
1326 Reg2 long beginning_of_this_line;
1327 Reg3 bool this_line_is_command = FALSE;
1330 if (!skip_rest_of_patch) {
1332 copy_file(filearg[0], TMPOUTNAME);
1334 Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
1336 Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
1337 pipefp = popen(buf, "w");
1340 beginning_of_this_line = ftell(pfp);
1341 if (pgets(buf, sizeof buf, pfp) == Nullch) {
1342 next_intuit_at(beginning_of_this_line,p_input_line);
1346 for (t=buf; isdigit((unsigned char)*t) || *t == ','; t++) ;
1347 this_line_is_command = (isdigit((unsigned char)*buf) &&
1348 (*t == 'd' || *t == 'c' || *t == 'a') );
1349 if (this_line_is_command) {
1350 if (!skip_rest_of_patch)
1353 while (pgets(buf, sizeof buf, pfp) != Nullch) {
1355 if (!skip_rest_of_patch)
1357 if (strEQ(buf, ".\n"))
1363 next_intuit_at(beginning_of_this_line,p_input_line);
1367 if (skip_rest_of_patch)
1369 fprintf(pipefp, "w\n");
1370 fprintf(pipefp, "q\n");
1374 if (move_file(TMPOUTNAME, outname) < 0) {
1376 chmod(TMPOUTNAME, filemode);
1379 chmod(outname, filemode);