pch.c revision 1.10 1 /* $NetBSD: pch.c,v 1.10 2002/03/15 19:44:54 kristerw Exp $ */
2 #include <sys/cdefs.h>
3 #ifndef lint
4 __RCSID("$NetBSD: pch.c,v 1.10 2002/03/15 19:44:54 kristerw Exp $");
5 #endif /* not lint */
6
7 #include "EXTERN.h"
8 #include "common.h"
9 #include "util.h"
10 #include "INTERN.h"
11 #include "pch.h"
12
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 /* Patch (diff listing) abstract type. */
17
18 static long p_filesize; /* size of the patch file */
19 static LINENUM p_first; /* 1st line number */
20 static LINENUM p_newfirst; /* 1st line number of replacement */
21 static LINENUM p_ptrn_lines; /* # lines in pattern */
22 static LINENUM p_repl_lines; /* # lines in replacement text */
23 static LINENUM p_end = -1; /* last line in hunk */
24 static LINENUM p_max; /* max allowed value of p_end */
25 static LINENUM p_context = 3; /* # of context lines */
26 static LINENUM p_input_line = 0; /* current line # from patch file */
27 static char **p_line = NULL; /* the text of the hunk */
28 static short *p_len = NULL; /* length of each line */
29 static char *p_char = NULL; /* +, -, and ! */
30 static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */
31 static int p_indent; /* indent to patch */
32 static LINENUM p_base; /* where to intuit this time */
33 static LINENUM p_bline; /* line # of p_base */
34 static LINENUM p_start; /* where intuit found a patch */
35 static LINENUM p_sline; /* and the line number for it */
36 static LINENUM p_hunk_beg; /* line number of current hunk */
37 static LINENUM p_efake = -1; /* end of faked up lines--don't free */
38 static LINENUM p_bfake = -1; /* beg of faked up lines */
39
40 /* Prepare to look for the next patch in the patch file. */
41 static void malformed(void);
42
43 void
44 re_patch(void)
45 {
46 p_first = Nulline;
47 p_newfirst = Nulline;
48 p_ptrn_lines = Nulline;
49 p_repl_lines = Nulline;
50 p_end = -1;
51 p_max = Nulline;
52 p_indent = 0;
53 }
54
55 /*
56 * Open the patch file at the beginning of time.
57 */
58 void
59 open_patch_file(char *filename)
60 {
61 if (filename == NULL || !*filename || strEQ(filename, "-")) {
62 pfp = fopen(TMPPATNAME, "w");
63 if (pfp == NULL)
64 pfatal("can't create %s", TMPPATNAME);
65 while (fgets(buf, sizeof buf, stdin) != NULL)
66 fputs(buf, pfp);
67 Fclose(pfp);
68 filename = TMPPATNAME;
69 }
70 pfp = fopen(filename, "r");
71 if (pfp == NULL)
72 pfatal("patch file %s not found", filename);
73 Fstat(fileno(pfp), &filestat);
74 p_filesize = filestat.st_size;
75 next_intuit_at(0L,1L); /* start at the beginning */
76 set_hunkmax();
77 }
78
79 /*
80 * Make sure our dynamically realloced tables are malloced to begin with.
81 */
82 void
83 set_hunkmax(void)
84 {
85 if (p_line == NULL)
86 p_line = malloc(hunkmax * sizeof(char *));
87 if (p_len == NULL)
88 p_len = malloc(hunkmax * sizeof(short));
89 if (p_char == NULL)
90 p_char = malloc(hunkmax * sizeof(char));
91 }
92
93 /*
94 * Enlarge the arrays containing the current hunk of patch.
95 */
96 void
97 grow_hunkmax(void)
98 {
99 hunkmax *= 2;
100 /*
101 * Note that on most systems, only the p_line array ever gets fresh
102 * memory since p_len can move into p_line's old space, and p_char
103 * can move into p_len's old space. Not on PDP-11's however. But
104 * it doesn't matter.
105 */
106 assert(p_line != NULL && p_len != NULL && p_char != NULL);
107 p_line = realloc(p_line, hunkmax * sizeof(char *));
108 p_len = realloc(p_len, hunkmax * sizeof(short));
109 p_char = realloc(p_char, hunkmax * sizeof(char));
110 if (p_line != NULL && p_len != NULL && p_char != NULL)
111 return;
112 if (!using_plan_a)
113 fatal("out of memory\n");
114 out_of_mem = TRUE; /* whatever is null will be allocated again */
115 /* from within plan_a(), of all places */
116 }
117
118 /*
119 * True if the remainder of the patch file contains a diff of some sort.
120 */
121 bool
122 there_is_another_patch(void)
123 {
124 if (p_base != 0L && p_base >= p_filesize) {
125 if (verbose)
126 say("done\n");
127 return FALSE;
128 }
129 if (verbose)
130 say("Hmm...");
131 diff_type = intuit_diff_type();
132 if (!diff_type) {
133 if (p_base != 0L) {
134 if (verbose)
135 say(" Ignoring the trailing garbage.\ndone\n");
136 }
137 else
138 say(" I can't seem to find a patch in there anywhere.\n");
139 return FALSE;
140 }
141 if (verbose)
142 say(" %sooks like %s to me...\n",
143 (p_base == 0L ? "L" : "The next patch l"),
144 diff_type == UNI_DIFF ? "a unified diff" :
145 diff_type == CONTEXT_DIFF ? "a context diff" :
146 diff_type == NEW_CONTEXT_DIFF ?
147 "a new-style context diff" :
148 diff_type == NORMAL_DIFF ? "a normal diff" :
149 "an ed script" );
150 if (p_indent && verbose)
151 say("(Patch is indented %d space%s.)\n",
152 p_indent, p_indent==1?"":"s");
153 skip_to(p_start,p_sline);
154 while (filearg[0] == NULL) {
155 if (force || batch) {
156 say("No file to patch. Skipping...\n");
157 filearg[0] = savestr(bestguess);
158 skip_rest_of_patch = TRUE;
159 return TRUE;
160 }
161 ask("File to patch: ");
162 if (*buf != '\n') {
163 if (bestguess)
164 free(bestguess);
165 bestguess = savestr(buf);
166 filearg[0] = fetchname(buf, 0, FALSE);
167 }
168 if (filearg[0] == NULL) {
169 ask("No file found--skip this patch? [n] ");
170 if (*buf != 'y') {
171 continue;
172 }
173 if (verbose)
174 say("Skipping patch...\n");
175 filearg[0] = fetchname(bestguess, 0, TRUE);
176 skip_rest_of_patch = TRUE;
177 return TRUE;
178 }
179 }
180 return TRUE;
181 }
182
183 /*
184 * Determine what kind of diff is in the remaining part of the patch file.
185 */
186 int
187 intuit_diff_type(void)
188 {
189 long this_line = 0;
190 long previous_line;
191 long first_command_line = -1;
192 long fcl_line = -1;
193 bool last_line_was_command = FALSE;
194 bool this_is_a_command = FALSE;
195 bool stars_last_line = FALSE;
196 bool stars_this_line = FALSE;
197 int indent;
198 char *s;
199 char *t;
200 char *indtmp = NULL;
201 char *oldtmp = NULL;
202 char *newtmp = NULL;
203 char *indname = NULL;
204 char *oldname = NULL;
205 char *newname = NULL;
206 int retval;
207 bool no_filearg = (filearg[0] == NULL);
208
209 ok_to_create_file = FALSE;
210 old_file_is_dev_null = FALSE;
211 Fseek(pfp, p_base, 0);
212 p_input_line = p_bline - 1;
213 for (;;) {
214 previous_line = this_line;
215 last_line_was_command = this_is_a_command;
216 stars_last_line = stars_this_line;
217 this_line = ftell(pfp);
218 indent = 0;
219 p_input_line++;
220 if (fgets(buf, sizeof buf, pfp) == NULL) {
221 if (first_command_line >= 0L) {
222 /* nothing but deletes!? */
223 p_start = first_command_line;
224 p_sline = fcl_line;
225 retval = ED_DIFF;
226 goto scan_exit;
227 }
228 else {
229 p_start = this_line;
230 p_sline = p_input_line;
231 retval = 0;
232 goto scan_exit;
233 }
234 }
235 for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
236 if (*s == '\t')
237 indent += 8 - (indent % 8);
238 else
239 indent++;
240 }
241 for (t=s; isdigit((unsigned char)*t) || *t == ','; t++) ;
242 this_is_a_command = (isdigit((unsigned char)*s) &&
243 (*t == 'd' || *t == 'c' || *t == 'a') );
244 if (first_command_line < 0L && this_is_a_command) {
245 first_command_line = this_line;
246 fcl_line = p_input_line;
247 p_indent = indent; /* assume this for now */
248 }
249 if (!stars_last_line && strnEQ(s, "*** ", 4))
250 oldtmp = savestr(s+4);
251 else if (strnEQ(s, "--- ", 4))
252 newtmp = savestr(s+4);
253 else if (strnEQ(s, "+++ ", 4))
254 oldtmp = savestr(s+4); /* pretend it is the old name */
255 else if (strnEQ(s, "Index:", 6))
256 indtmp = savestr(s+6);
257 else if (strnEQ(s, "Prereq:", 7)) {
258 for (t = s + 7; isspace((unsigned char)*t); t++)
259 ;
260 revision = savestr(t);
261 for (t = revision;
262 *t && !isspace((unsigned char)*t);
263 t++)
264 ;
265 *t = '\0';
266 if (!*revision) {
267 free(revision);
268 revision = NULL;
269 }
270 }
271 if ((!diff_type || diff_type == ED_DIFF) &&
272 first_command_line >= 0L &&
273 strEQ(s, ".\n") ) {
274 p_indent = indent;
275 p_start = first_command_line;
276 p_sline = fcl_line;
277 retval = ED_DIFF;
278 goto scan_exit;
279 }
280 if ((!diff_type || diff_type == UNI_DIFF) &&
281 strnEQ(s, "@@ -", 4)) {
282 if (!atol(s+3))
283 ok_to_create_file = TRUE;
284 p_indent = indent;
285 p_start = this_line;
286 p_sline = p_input_line;
287 retval = UNI_DIFF;
288 goto scan_exit;
289 }
290 stars_this_line = strnEQ(s, "********", 8);
291 if ((!diff_type || diff_type == CONTEXT_DIFF) &&
292 stars_last_line &&
293 strnEQ(s, "*** ", 4)) {
294 if (!atol(s+4))
295 ok_to_create_file = TRUE;
296 /*
297 * If this is a new context diff the character just
298 * before the newline is a '*'.
299 */
300 while (*s != '\n')
301 s++;
302 p_indent = indent;
303 p_start = previous_line;
304 p_sline = p_input_line - 1;
305 retval = (*(s-1) == '*' ?
306 NEW_CONTEXT_DIFF : CONTEXT_DIFF);
307 goto scan_exit;
308 }
309 if ((!diff_type || diff_type == NORMAL_DIFF) &&
310 last_line_was_command &&
311 (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
312 p_start = previous_line;
313 p_sline = p_input_line - 1;
314 p_indent = indent;
315 retval = NORMAL_DIFF;
316 goto scan_exit;
317 }
318 }
319 scan_exit:
320 if (no_filearg) {
321 if (indtmp != NULL)
322 indname = fetchname(indtmp,
323 strippath,
324 ok_to_create_file);
325 if (oldtmp != NULL) {
326 oldname = fetchname(oldtmp,
327 strippath,
328 ok_to_create_file);
329 old_file_is_dev_null = filename_is_dev_null;
330 }
331 if (newtmp != NULL)
332 newname = fetchname(newtmp,
333 strippath,
334 ok_to_create_file);
335 if (oldname && newname) {
336 if (strlen(oldname) < strlen(newname))
337 filearg[0] = savestr(oldname);
338 else
339 filearg[0] = savestr(newname);
340 }
341 else if (oldname)
342 filearg[0] = savestr(oldname);
343 else if (newname)
344 filearg[0] = savestr(newname);
345 else if (indname)
346 filearg[0] = savestr(indname);
347 }
348 if (bestguess) {
349 free(bestguess);
350 bestguess = NULL;
351 }
352 if (filearg[0] != NULL)
353 bestguess = savestr(filearg[0]);
354 else if (indtmp != NULL)
355 bestguess = fetchname(indtmp, strippath, TRUE);
356 else {
357 if (oldtmp != NULL) {
358 oldname = fetchname(oldtmp, strippath, TRUE);
359 old_file_is_dev_null = filename_is_dev_null;
360 }
361 if (newtmp != NULL)
362 newname = fetchname(newtmp, strippath, TRUE);
363 if (oldname && newname) {
364 if (strlen(oldname) < strlen(newname))
365 bestguess = savestr(oldname);
366 else
367 bestguess = savestr(newname);
368 }
369 else if (oldname)
370 bestguess = savestr(oldname);
371 else if (newname)
372 bestguess = savestr(newname);
373 }
374 if (indtmp != NULL)
375 free(indtmp);
376 if (oldtmp != NULL)
377 free(oldtmp);
378 if (newtmp != NULL)
379 free(newtmp);
380 if (indname != NULL)
381 free(indname);
382 if (oldname != NULL)
383 free(oldname);
384 if (newname != NULL)
385 free(newname);
386 return retval;
387 }
388
389 /*
390 * Remember where this patch ends so we know where to start up again.
391 */
392 void
393 next_intuit_at(long file_pos, long file_line)
394 {
395 p_base = file_pos;
396 p_bline = file_line;
397 }
398
399 /*
400 * Basically a verbose fseek() to the actual diff listing.
401 */
402 void
403 skip_to(long file_pos, long file_line)
404 {
405 char *ret;
406
407 assert(p_base <= file_pos);
408 if (verbose && p_base < file_pos) {
409 Fseek(pfp, p_base, 0);
410 say("The text leading up to this was:\n--------------------------\n");
411 while (ftell(pfp) < file_pos) {
412 ret = fgets(buf, sizeof buf, pfp);
413 assert(ret != NULL);
414 say("|%s", buf);
415 }
416 say("--------------------------\n");
417 }
418 else
419 Fseek(pfp, file_pos, 0);
420 p_input_line = file_line - 1;
421 }
422
423 /*
424 * Make this a function for better debugging.
425 */
426 static void
427 malformed(void)
428 {
429 fatal("malformed patch at line %ld: %s", p_input_line, buf);
430 /* about as informative as "Syntax error" in C */
431 }
432
433 /*
434 * True if there is more of the current diff listing to process.
435 */
436 bool
437 another_hunk(void)
438 {
439 char *s;
440 char *ret;
441 int context = 0;
442
443 while (p_end >= 0) {
444 if (p_end == p_efake)
445 p_end = p_bfake; /* don't free twice */
446 else
447 free(p_line[p_end]);
448 p_end--;
449 }
450 assert(p_end == -1);
451 p_efake = -1;
452
453 p_max = hunkmax; /* gets reduced when --- found */
454 if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
455 long line_beginning = ftell(pfp);
456 /* file pos of the current line */
457 LINENUM repl_beginning = 0; /* index of --- line */
458 LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */
459 LINENUM fillsrc = 0; /* index of first line to copy */
460 LINENUM filldst = 0; /* index of first missing line */
461 bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
462 bool repl_could_be_missing = TRUE;
463 /* no + or ! lines in this hunk */
464 bool repl_missing = FALSE; /* we are now backtracking */
465 long repl_backtrack_position = 0;
466 /* file pos of first repl line */
467 LINENUM repl_patch_line = 0; /* input line number for same */
468 LINENUM ptrn_copiable = 0; /* # of copiable lines in ptrn */
469
470 ret = pgets(buf, sizeof buf, pfp);
471 p_input_line++;
472 if (ret == NULL || strnNE(buf, "********", 8)) {
473 next_intuit_at(line_beginning,p_input_line);
474 return FALSE;
475 }
476 p_context = 100;
477 p_hunk_beg = p_input_line + 1;
478 while (p_end < p_max) {
479 line_beginning = ftell(pfp);
480 ret = pgets(buf, sizeof buf, pfp);
481 p_input_line++;
482 if (ret == NULL) {
483 if (p_max - p_end < 4)
484 Strcpy(buf, " \n"); /* assume blank lines got chopped */
485 else {
486 if (repl_beginning && repl_could_be_missing) {
487 repl_missing = TRUE;
488 goto hunk_done;
489 }
490 fatal("unexpected end of file in patch\n");
491 }
492 }
493 p_end++;
494 assert(p_end < hunkmax);
495 p_char[p_end] = *buf;
496 p_line[p_end] = NULL;
497 switch (*buf) {
498 case '*':
499 if (strnEQ(buf, "********", 8)) {
500 if (repl_beginning && repl_could_be_missing) {
501 repl_missing = TRUE;
502 goto hunk_done;
503 }
504 else
505 fatal("unexpected end of hunk at line %ld\n",
506 p_input_line);
507 }
508 if (p_end != 0) {
509 if (repl_beginning && repl_could_be_missing) {
510 repl_missing = TRUE;
511 goto hunk_done;
512 }
513 fatal("unexpected *** at line %ld: %s", p_input_line, buf);
514 }
515 context = 0;
516 p_line[p_end] = savestr(buf);
517 if (out_of_mem) {
518 p_end--;
519 return FALSE;
520 }
521 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
522 if (!*s)
523 malformed ();
524 if (strnEQ(s,"0,0",3))
525 strcpy(s,s+2);
526 p_first = atol(s);
527 while (isdigit((unsigned char)*s)) s++;
528 if (*s == ',') {
529 for (; *s && !isdigit((unsigned char)*s); s++) ;
530 if (!*s)
531 malformed ();
532 p_ptrn_lines = atol(s) - p_first + 1;
533 }
534 else if (p_first)
535 p_ptrn_lines = 1;
536 else {
537 p_ptrn_lines = 0;
538 p_first = 1;
539 }
540 p_max = p_ptrn_lines + 6; /* we need this much at least */
541 while (p_max >= hunkmax)
542 grow_hunkmax();
543 p_max = hunkmax;
544 break;
545 case '-':
546 if (buf[1] == '-') {
547 if (repl_beginning ||
548 (p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
549 {
550 if (p_end == 1) {
551 /* `old' lines were omitted - set up to fill */
552 /* them in from 'new' context lines. */
553 p_end = p_ptrn_lines + 1;
554 fillsrc = p_end + 1;
555 filldst = 1;
556 fillcnt = p_ptrn_lines;
557 }
558 else {
559 if (repl_beginning) {
560 if (repl_could_be_missing){
561 repl_missing = TRUE;
562 goto hunk_done;
563 }
564 fatal(
565 "duplicate \"---\" at line %ld--check line numbers at line %ld\n",
566 p_input_line, p_hunk_beg + repl_beginning);
567 }
568 else {
569 fatal(
570 "%s \"---\" at line %ld--check line numbers at line %ld\n",
571 (p_end <= p_ptrn_lines
572 ? "Premature"
573 : "Overdue" ),
574 p_input_line, p_hunk_beg);
575 }
576 }
577 }
578 repl_beginning = p_end;
579 repl_backtrack_position = ftell(pfp);
580 repl_patch_line = p_input_line;
581 p_line[p_end] = savestr(buf);
582 if (out_of_mem) {
583 p_end--;
584 return FALSE;
585 }
586 p_char[p_end] = '=';
587 for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
588 if (!*s)
589 malformed ();
590 p_newfirst = atol(s);
591 while (isdigit((unsigned char)*s)) s++;
592 if (*s == ',') {
593 for (; *s && !isdigit((unsigned char)*s); s++) ;
594 if (!*s)
595 malformed ();
596 p_repl_lines = atol(s) - p_newfirst + 1;
597 }
598 else if (p_newfirst)
599 p_repl_lines = 1;
600 else {
601 p_repl_lines = 0;
602 p_newfirst = 1;
603 }
604 p_max = p_repl_lines + p_end;
605 if (p_max > MAXHUNKSIZE)
606 fatal("hunk too large (%ld lines) at line %ld: %s",
607 p_max, p_input_line, buf);
608 while (p_max >= hunkmax)
609 grow_hunkmax();
610 if (p_repl_lines != ptrn_copiable
611 && (p_context != 0 || p_repl_lines != 1))
612 repl_could_be_missing = FALSE;
613 break;
614 }
615 goto change_line;
616 case '+': case '!':
617 repl_could_be_missing = FALSE;
618 change_line:
619 if (buf[1] == '\n' && canonicalize)
620 strcpy(buf+1," \n");
621 if (!isspace((unsigned char)buf[1]) && buf[1] != '>' && buf[1] != '<' &&
622 repl_beginning && repl_could_be_missing) {
623 repl_missing = TRUE;
624 goto hunk_done;
625 }
626 if (context >= 0) {
627 if (context < p_context)
628 p_context = context;
629 context = -1000;
630 }
631 p_line[p_end] = savestr(buf+2);
632 if (out_of_mem) {
633 p_end--;
634 return FALSE;
635 }
636 break;
637 case '\t': case '\n': /* assume the 2 spaces got eaten */
638 if (repl_beginning && repl_could_be_missing &&
639 (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
640 repl_missing = TRUE;
641 goto hunk_done;
642 }
643 p_line[p_end] = savestr(buf);
644 if (out_of_mem) {
645 p_end--;
646 return FALSE;
647 }
648 if (p_end != p_ptrn_lines + 1) {
649 ptrn_spaces_eaten |= (repl_beginning != 0);
650 context++;
651 if (!repl_beginning)
652 ptrn_copiable++;
653 p_char[p_end] = ' ';
654 }
655 break;
656 case ' ':
657 if (!isspace((unsigned char)buf[1]) &&
658 repl_beginning && repl_could_be_missing) {
659 repl_missing = TRUE;
660 goto hunk_done;
661 }
662 context++;
663 if (!repl_beginning)
664 ptrn_copiable++;
665 p_line[p_end] = savestr(buf+2);
666 if (out_of_mem) {
667 p_end--;
668 return FALSE;
669 }
670 break;
671 default:
672 if (repl_beginning && repl_could_be_missing) {
673 repl_missing = TRUE;
674 goto hunk_done;
675 }
676 malformed ();
677 }
678 /* set up p_len for strncmp() so we don't have to */
679 /* assume null termination */
680 if (p_line[p_end])
681 p_len[p_end] = strlen(p_line[p_end]);
682 else
683 p_len[p_end] = 0;
684 }
685
686 hunk_done:
687 if (p_end >=0 && !repl_beginning)
688 fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
689
690 if (repl_missing) {
691
692 /* reset state back to just after --- */
693 p_input_line = repl_patch_line;
694 for (p_end--; p_end > repl_beginning; p_end--)
695 free(p_line[p_end]);
696 Fseek(pfp, repl_backtrack_position, 0);
697
698 /* redundant 'new' context lines were omitted - set */
699 /* up to fill them in from the old file context */
700 if (!p_context && p_repl_lines == 1) {
701 p_repl_lines = 0;
702 p_max--;
703 }
704 fillsrc = 1;
705 filldst = repl_beginning+1;
706 fillcnt = p_repl_lines;
707 p_end = p_max;
708 }
709 else if (!p_context && fillcnt == 1) {
710 /* the first hunk was a null hunk with no context */
711 /* and we were expecting one line -- fix it up. */
712 while (filldst < p_end) {
713 p_line[filldst] = p_line[filldst+1];
714 p_char[filldst] = p_char[filldst+1];
715 p_len[filldst] = p_len[filldst+1];
716 filldst++;
717 }
718 #if 0
719 repl_beginning--; /* this doesn't need to be fixed */
720 #endif
721 p_end--;
722 p_first++; /* do append rather than insert */
723 fillcnt = 0;
724 p_ptrn_lines = 0;
725 }
726
727 if (diff_type == CONTEXT_DIFF &&
728 (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
729 if (verbose)
730 say("%s\n%s\n%s\n",
731 "(Fascinating--this is really a new-style context diff but without",
732 "the telltale extra asterisks on the *** line that usually indicate",
733 "the new style...)");
734 diff_type = NEW_CONTEXT_DIFF;
735 }
736
737 /* if there were omitted context lines, fill them in now */
738 if (fillcnt) {
739 p_bfake = filldst; /* remember where not to free() */
740 p_efake = filldst + fillcnt - 1;
741 while (fillcnt-- > 0) {
742 while (fillsrc <= p_end && p_char[fillsrc] != ' ')
743 fillsrc++;
744 if (fillsrc > p_end)
745 fatal("replacement text or line numbers mangled in hunk at line %ld\n",
746 p_hunk_beg);
747 p_line[filldst] = p_line[fillsrc];
748 p_char[filldst] = p_char[fillsrc];
749 p_len[filldst] = p_len[fillsrc];
750 fillsrc++; filldst++;
751 }
752 while (fillsrc <= p_end && fillsrc != repl_beginning &&
753 p_char[fillsrc] != ' ')
754 fillsrc++;
755 #ifdef DEBUGGING
756 if (debug & 64)
757 printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
758 fillsrc,filldst,repl_beginning,p_end+1);
759 #endif
760 assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
761 assert(filldst==p_end+1 || filldst==repl_beginning);
762 }
763 }
764 else if (diff_type == UNI_DIFF) {
765 long line_beginning = ftell(pfp);
766 /* file pos of the current line */
767 LINENUM fillsrc; /* index of old lines */
768 LINENUM filldst; /* index of new lines */
769 char ch;
770
771 ret = pgets(buf, sizeof buf, pfp);
772 p_input_line++;
773 if (ret == NULL || strnNE(buf, "@@ -", 4)) {
774 next_intuit_at(line_beginning,p_input_line);
775 return FALSE;
776 }
777 s = buf+4;
778 if (!*s)
779 malformed ();
780 p_first = atol(s);
781 while (isdigit((unsigned char)*s)) s++;
782 if (*s == ',') {
783 p_ptrn_lines = atol(++s);
784 while (isdigit((unsigned char)*s)) s++;
785 } else
786 p_ptrn_lines = 1;
787 if (*s == ' ') s++;
788 if (*s != '+' || !*++s)
789 malformed ();
790 p_newfirst = atol(s);
791 while (isdigit((unsigned char)*s)) s++;
792 if (*s == ',') {
793 p_repl_lines = atol(++s);
794 while (isdigit((unsigned char)*s)) s++;
795 } else
796 p_repl_lines = 1;
797 if (*s == ' ') s++;
798 if (*s != '@')
799 malformed ();
800 if (!p_ptrn_lines)
801 p_first++; /* do append rather than insert */
802 p_max = p_ptrn_lines + p_repl_lines + 1;
803 while (p_max >= hunkmax)
804 grow_hunkmax();
805 fillsrc = 1;
806 filldst = fillsrc + p_ptrn_lines;
807 p_end = filldst + p_repl_lines;
808 Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
809 p_line[0] = savestr(buf);
810 if (out_of_mem) {
811 p_end = -1;
812 return FALSE;
813 }
814 p_char[0] = '*';
815 Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
816 p_line[filldst] = savestr(buf);
817 if (out_of_mem) {
818 p_end = 0;
819 return FALSE;
820 }
821 p_char[filldst++] = '=';
822 p_context = 100;
823 context = 0;
824 p_hunk_beg = p_input_line + 1;
825 while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
826 line_beginning = ftell(pfp);
827 ret = pgets(buf, sizeof buf, pfp);
828 p_input_line++;
829 if (ret == NULL) {
830 if (p_max - filldst < 3)
831 Strcpy(buf, " \n"); /* assume blank lines got chopped */
832 else {
833 fatal("unexpected end of file in patch\n");
834 }
835 }
836 if (*buf == '\t' || *buf == '\n') {
837 ch = ' '; /* assume the space got eaten */
838 s = savestr(buf);
839 }
840 else {
841 ch = *buf;
842 s = savestr(buf+1);
843 }
844 if (out_of_mem) {
845 while (--filldst > p_ptrn_lines)
846 free(p_line[filldst]);
847 p_end = fillsrc-1;
848 return FALSE;
849 }
850 switch (ch) {
851 case '-':
852 if (fillsrc > p_ptrn_lines) {
853 free(s);
854 p_end = filldst-1;
855 malformed ();
856 }
857 p_char[fillsrc] = ch;
858 p_line[fillsrc] = s;
859 p_len[fillsrc++] = strlen(s);
860 break;
861 case '=':
862 ch = ' ';
863 /* FALL THROUGH */
864 case ' ':
865 if (fillsrc > p_ptrn_lines) {
866 free(s);
867 while (--filldst > p_ptrn_lines)
868 free(p_line[filldst]);
869 p_end = fillsrc-1;
870 malformed ();
871 }
872 context++;
873 p_char[fillsrc] = ch;
874 p_line[fillsrc] = s;
875 p_len[fillsrc++] = strlen(s);
876 s = savestr(s);
877 if (out_of_mem) {
878 while (--filldst > p_ptrn_lines)
879 free(p_line[filldst]);
880 p_end = fillsrc-1;
881 return FALSE;
882 }
883 /* FALL THROUGH */
884 case '+':
885 if (filldst > p_end) {
886 free(s);
887 while (--filldst > p_ptrn_lines)
888 free(p_line[filldst]);
889 p_end = fillsrc-1;
890 malformed ();
891 }
892 p_char[filldst] = ch;
893 p_line[filldst] = s;
894 p_len[filldst++] = strlen(s);
895 break;
896 default:
897 p_end = filldst;
898 malformed ();
899 }
900 if (ch != ' ' && context > 0) {
901 if (context < p_context)
902 p_context = context;
903 context = -1000;
904 }
905 }/* while */
906 }
907 else { /* normal diff--fake it up */
908 char hunk_type;
909 int i;
910 LINENUM min, max;
911 long line_beginning = ftell(pfp);
912
913 p_context = 0;
914 ret = pgets(buf, sizeof buf, pfp);
915 p_input_line++;
916 if (ret == NULL || !isdigit((unsigned char)*buf)) {
917 next_intuit_at(line_beginning,p_input_line);
918 return FALSE;
919 }
920 p_first = atol(buf);
921 for (s=buf; isdigit((unsigned char)*s); s++) ;
922 if (*s == ',') {
923 p_ptrn_lines = atol(++s) - p_first + 1;
924 while (isdigit((unsigned char)*s)) s++;
925 }
926 else
927 p_ptrn_lines = (*s != 'a');
928 hunk_type = *s;
929 if (hunk_type == 'a')
930 p_first++; /* do append rather than insert */
931 min = atol(++s);
932 for (; isdigit((unsigned char)*s); s++) ;
933 if (*s == ',')
934 max = atol(++s);
935 else
936 max = min;
937 if (hunk_type == 'd')
938 min++;
939 p_end = p_ptrn_lines + 1 + max - min + 1;
940 if (p_end > MAXHUNKSIZE)
941 fatal("hunk too large (%ld lines) at line %ld: %s",
942 p_end, p_input_line, buf);
943 while (p_end >= hunkmax)
944 grow_hunkmax();
945 p_newfirst = min;
946 p_repl_lines = max - min + 1;
947 Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
948 p_line[0] = savestr(buf);
949 if (out_of_mem) {
950 p_end = -1;
951 return FALSE;
952 }
953 p_char[0] = '*';
954 for (i=1; i<=p_ptrn_lines; i++) {
955 ret = pgets(buf, sizeof buf, pfp);
956 p_input_line++;
957 if (ret == NULL)
958 fatal("unexpected end of file in patch at line %ld\n",
959 p_input_line);
960 if (*buf != '<')
961 fatal("< expected at line %ld of patch\n", p_input_line);
962 p_line[i] = savestr(buf+2);
963 if (out_of_mem) {
964 p_end = i-1;
965 return FALSE;
966 }
967 p_len[i] = strlen(p_line[i]);
968 p_char[i] = '-';
969 }
970 if (hunk_type == 'c') {
971 ret = pgets(buf, sizeof buf, pfp);
972 p_input_line++;
973 if (ret == NULL)
974 fatal("unexpected end of file in patch at line %ld\n",
975 p_input_line);
976 if (*buf != '-')
977 fatal("--- expected at line %ld of patch\n", p_input_line);
978 }
979 Sprintf(buf, "--- %ld,%ld\n", min, max);
980 p_line[i] = savestr(buf);
981 if (out_of_mem) {
982 p_end = i-1;
983 return FALSE;
984 }
985 p_char[i] = '=';
986 for (i++; i<=p_end; i++) {
987 ret = pgets(buf, sizeof buf, pfp);
988 p_input_line++;
989 if (ret == NULL)
990 fatal("unexpected end of file in patch at line %ld\n",
991 p_input_line);
992 if (*buf != '>')
993 fatal("> expected at line %ld of patch\n", p_input_line);
994 p_line[i] = savestr(buf+2);
995 if (out_of_mem) {
996 p_end = i-1;
997 return FALSE;
998 }
999 p_len[i] = strlen(p_line[i]);
1000 p_char[i] = '+';
1001 }
1002 }
1003 if (reverse) /* backwards patch? */
1004 if (!pch_swap())
1005 say("Not enough memory to swap next hunk!\n");
1006 #ifdef DEBUGGING
1007 if (debug & 2) {
1008 int i;
1009 char special;
1010
1011 for (i=0; i <= p_end; i++) {
1012 if (i == p_ptrn_lines)
1013 special = '^';
1014 else
1015 special = ' ';
1016 fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
1017 Fflush(stderr);
1018 }
1019 }
1020 #endif
1021 if (p_end+1 < hunkmax) /* paranoia reigns supreme... */
1022 p_char[p_end+1] = '^'; /* add a stopper for apply_hunk */
1023 return TRUE;
1024 }
1025
1026 /*
1027 * Input a line from the patch file, worrying about indentation.
1028 */
1029 char *
1030 pgets(char *bf, int sz, FILE *fp)
1031 {
1032 char *ret = fgets(bf, sz, fp);
1033 char *s;
1034 int indent = 0;
1035
1036 if (p_indent && ret != NULL) {
1037 for (s=buf;
1038 indent < p_indent &&
1039 (*s == ' ' || *s == '\t' || *s == 'X');
1040 s++) {
1041 if (*s == '\t')
1042 indent += 8 - (indent % 7);
1043 else
1044 indent++;
1045 }
1046 if (buf != s)
1047 Strcpy(buf, s);
1048 }
1049 return ret;
1050 }
1051
1052 /*
1053 * Reverse the old and new portions of the current hunk.
1054 */
1055 bool
1056 pch_swap(void)
1057 {
1058 char **tp_line; /* the text of the hunk */
1059 short *tp_len; /* length of each line */
1060 char *tp_char; /* +, -, and ! */
1061 LINENUM i;
1062 LINENUM n;
1063 bool blankline = FALSE;
1064 char *s;
1065
1066 i = p_first;
1067 p_first = p_newfirst;
1068 p_newfirst = i;
1069
1070 /* make a scratch copy */
1071
1072 tp_line = p_line;
1073 tp_len = p_len;
1074 tp_char = p_char;
1075 p_line = NULL; /* force set_hunkmax to allocate again */
1076 p_len = NULL;
1077 p_char = NULL;
1078 set_hunkmax();
1079 if (p_line == NULL || p_len == NULL || p_char == NULL) {
1080 if (p_line == NULL)
1081 free(p_line);
1082 p_line = tp_line;
1083 if (p_len == NULL)
1084 free(p_len);
1085 p_len = tp_len;
1086 if (p_char == NULL)
1087 free(p_char);
1088 p_char = tp_char;
1089 return FALSE; /* not enough memory to swap hunk! */
1090 }
1091
1092 /* now turn the new into the old */
1093
1094 i = p_ptrn_lines + 1;
1095 if (tp_char[i] == '\n') { /* account for possible blank line */
1096 blankline = TRUE;
1097 i++;
1098 }
1099 if (p_efake >= 0) { /* fix non-freeable ptr range */
1100 if (p_efake <= i)
1101 n = p_end - i + 1;
1102 else
1103 n = -i;
1104 p_efake += n;
1105 p_bfake += n;
1106 }
1107 for (n=0; i <= p_end; i++,n++) {
1108 p_line[n] = tp_line[i];
1109 p_char[n] = tp_char[i];
1110 if (p_char[n] == '+')
1111 p_char[n] = '-';
1112 p_len[n] = tp_len[i];
1113 }
1114 if (blankline) {
1115 i = p_ptrn_lines + 1;
1116 p_line[n] = tp_line[i];
1117 p_char[n] = tp_char[i];
1118 p_len[n] = tp_len[i];
1119 n++;
1120 }
1121 assert(p_char[0] == '=');
1122 p_char[0] = '*';
1123 for (s=p_line[0]; *s; s++)
1124 if (*s == '-')
1125 *s = '*';
1126
1127 /* now turn the old into the new */
1128
1129 assert(tp_char[0] == '*');
1130 tp_char[0] = '=';
1131 for (s=tp_line[0]; *s; s++)
1132 if (*s == '*')
1133 *s = '-';
1134 for (i=0; n <= p_end; i++,n++) {
1135 p_line[n] = tp_line[i];
1136 p_char[n] = tp_char[i];
1137 if (p_char[n] == '-')
1138 p_char[n] = '+';
1139 p_len[n] = tp_len[i];
1140 }
1141 assert(i == p_ptrn_lines + 1);
1142 i = p_ptrn_lines;
1143 p_ptrn_lines = p_repl_lines;
1144 p_repl_lines = i;
1145 if (tp_line == NULL)
1146 free(tp_line);
1147 if (tp_len == NULL)
1148 free(tp_len);
1149 if (tp_char == NULL)
1150 free(tp_char);
1151 return TRUE;
1152 }
1153
1154 /*
1155 * Return the specified line position in the old file of the old context.
1156 */
1157 LINENUM
1158 pch_first(void)
1159 {
1160 return p_first;
1161 }
1162
1163 /*
1164 * Return the number of lines of old context.
1165 */
1166 LINENUM
1167 pch_ptrn_lines(void)
1168 {
1169 return p_ptrn_lines;
1170 }
1171
1172 /*
1173 * Return the probable line position in the new file of the first line.
1174 */
1175 LINENUM
1176 pch_newfirst(void)
1177 {
1178 return p_newfirst;
1179 }
1180
1181 /*
1182 * Return the number of lines in the replacement text including context.
1183 */
1184 LINENUM
1185 pch_repl_lines(void)
1186 {
1187 return p_repl_lines;
1188 }
1189
1190 /*
1191 * Return the number of lines in the whole hunk.
1192 */
1193 LINENUM
1194 pch_end(void)
1195 {
1196 return p_end;
1197 }
1198
1199 /*
1200 * Return the number of context lines before the first changed line.
1201 */
1202 LINENUM
1203 pch_context(void)
1204 {
1205 return p_context;
1206 }
1207
1208 /*
1209 * Return the length of a particular patch line.
1210 */
1211 short
1212 pch_line_len(LINENUM line)
1213 {
1214 return p_len[line];
1215 }
1216
1217 /*
1218 * Return the control character (+, -, *, !, etc) for a patch line.
1219 */
1220 char
1221 pch_char(LINENUM line)
1222 {
1223 return p_char[line];
1224 }
1225
1226 /*
1227 * Return a pointer to a particular patch line.
1228 */
1229 char *
1230 pfetch(LINENUM line)
1231 {
1232 return p_line[line];
1233 }
1234
1235 /*
1236 * Return where in the patch file this hunk began, for error messages.
1237 */
1238 LINENUM
1239 pch_hunk_beg(void)
1240 {
1241 return p_hunk_beg;
1242 }
1243
1244 /*
1245 * Apply an ed script by feeding ed itself.
1246 */
1247 void
1248 do_ed_script(void)
1249 {
1250 char *t;
1251 long beginning_of_this_line;
1252 bool this_line_is_command = FALSE;
1253 FILE *pipefp = NULL;
1254
1255 if (!skip_rest_of_patch) {
1256 Unlink(TMPOUTNAME);
1257 copy_file(filearg[0], TMPOUTNAME);
1258 if (verbose)
1259 Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
1260 else
1261 Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
1262 pipefp = popen(buf, "w");
1263 }
1264 for (;;) {
1265 beginning_of_this_line = ftell(pfp);
1266 if (pgets(buf, sizeof buf, pfp) == NULL) {
1267 next_intuit_at(beginning_of_this_line,p_input_line);
1268 break;
1269 }
1270 p_input_line++;
1271 for (t=buf; isdigit((unsigned char)*t) || *t == ','; t++) ;
1272 this_line_is_command = (isdigit((unsigned char)*buf) &&
1273 (*t == 'd' || *t == 'c' || *t == 'a'));
1274 if (this_line_is_command) {
1275 if (!skip_rest_of_patch)
1276 fputs(buf, pipefp);
1277 if (*t != 'd') {
1278 while (pgets(buf, sizeof buf, pfp) != NULL) {
1279 p_input_line++;
1280 if (!skip_rest_of_patch)
1281 fputs(buf, pipefp);
1282 if (strEQ(buf, ".\n"))
1283 break;
1284 }
1285 }
1286 }
1287 else {
1288 next_intuit_at(beginning_of_this_line,p_input_line);
1289 break;
1290 }
1291 }
1292 if (skip_rest_of_patch)
1293 return;
1294 fprintf(pipefp, "w\n");
1295 fprintf(pipefp, "q\n");
1296 Fflush(pipefp);
1297 Pclose(pipefp);
1298 ignore_signals();
1299 if (move_file(TMPOUTNAME, outname) < 0) {
1300 toutkeep = TRUE;
1301 chmod(TMPOUTNAME, filemode);
1302 }
1303 else
1304 chmod(outname, filemode);
1305 set_signals(1);
1306 }
1307