Annotation of src/usr.bin/sdiff/sdiff.c, Revision 1.31
1.31 ! deraadt 1: /* $OpenBSD: sdiff.c,v 1.30 2013/11/26 21:08:12 deraadt Exp $ */
1.1 tedu 2:
3: /*
4: * Written by Raymond Lai <ray@cyth.net>.
5: * Public domain.
6: */
7:
8: #include <sys/queue.h>
1.17 otto 9: #include <sys/stat.h>
1.1 tedu 10: #include <sys/types.h>
11: #include <sys/wait.h>
12:
13: #include <ctype.h>
14: #include <err.h>
1.17 otto 15: #include <errno.h>
16: #include <fcntl.h>
1.1 tedu 17: #include <getopt.h>
18: #include <limits.h>
1.19 ray 19: #include <paths.h>
1.1 tedu 20: #include <stdio.h>
21: #include <stdlib.h>
22: #include <string.h>
23: #include <unistd.h>
24: #include <util.h>
25:
1.17 otto 26: #include "common.h"
1.1 tedu 27: #include "extern.h"
28:
29: #define WIDTH 130
30: /*
31: * Each column must be at least one character wide, plus three
32: * characters between the columns (space, [<|>], space).
33: */
34: #define WIDTH_MIN 5
35:
36: /* A single diff line. */
37: struct diffline {
38: SIMPLEQ_ENTRY(diffline) diffentries;
1.14 otto 39: char *left;
40: char div;
41: char *right;
1.1 tedu 42: };
43:
44: static void astrcat(char **, const char *);
1.14 otto 45: static void enqueue(char *, char, char *);
1.17 otto 46: static char *mktmpcpy(const char *);
1.14 otto 47: static void freediff(struct diffline *);
1.1 tedu 48: static void int_usage(void);
1.13 otto 49: static int parsecmd(FILE *, FILE *, FILE *);
1.1 tedu 50: static void printa(FILE *, size_t);
51: static void printc(FILE *, size_t, FILE *, size_t);
52: static void printcol(const char *, size_t *, const size_t);
1.13 otto 53: static void printd(FILE *, size_t);
1.1 tedu 54: static void println(const char *, const char, const char *);
55: static void processq(void);
56: static void prompt(const char *, const char *);
57: __dead static void usage(void);
58: static char *xfgets(FILE *);
59:
60: SIMPLEQ_HEAD(, diffline) diffhead = SIMPLEQ_HEAD_INITIALIZER(diffhead);
61: size_t line_width; /* width of a line (two columns and divider) */
62: size_t width; /* width of each column */
63: size_t file1ln, file2ln; /* line number of file1 and file2 */
1.13 otto 64: int Iflag = 0; /* ignore sets matching regexp */
1.1 tedu 65: int lflag; /* print only left column for identical lines */
66: int sflag; /* skip identical lines */
1.28 ray 67: FILE *outfp; /* file to save changes to */
1.19 ray 68: const char *tmpdir; /* TMPDIR or /tmp */
1.1 tedu 69:
70: static struct option longopts[] = {
71: { "text", no_argument, NULL, 'a' },
72: { "ignore-blank-lines", no_argument, NULL, 'B' },
73: { "ignore-space-change", no_argument, NULL, 'b' },
74: { "minimal", no_argument, NULL, 'd' },
75: { "ignore-tab-expansion", no_argument, NULL, 'E' },
76: { "diff-program", required_argument, NULL, 'F' },
77: { "speed-large-files", no_argument, NULL, 'H' },
78: { "ignore-matching-lines", required_argument, NULL, 'I' },
1.21 ray 79: { "ignore-case", no_argument, NULL, 'i' },
1.1 tedu 80: { "left-column", no_argument, NULL, 'l' },
81: { "output", required_argument, NULL, 'o' },
82: { "strip-trailing-cr", no_argument, NULL, 'S' },
83: { "suppress-common-lines", no_argument, NULL, 's' },
84: { "expand-tabs", no_argument, NULL, 't' },
85: { "ignore-all-space", no_argument, NULL, 'W' },
86: { "width", required_argument, NULL, 'w' },
87: { NULL, 0, NULL, 0 }
88: };
89:
1.17 otto 90: /*
91: * Create temporary file if source_file is not a regular file.
92: * Returns temporary file name if one was malloced, NULL if unnecessary.
93: */
94: static char *
95: mktmpcpy(const char *source_file)
96: {
97: struct stat sb;
98: ssize_t rcount;
99: int ifd, ofd;
100: u_char buf[BUFSIZ];
101: char *target_file;
102:
103: /* Open input and output. */
104: ifd = open(source_file, O_RDONLY, 0);
105: /* File was opened successfully. */
106: if (ifd != -1) {
107: if (fstat(ifd, &sb) == -1)
108: err(2, "error getting file status from %s", source_file);
109:
110: /* Regular file. */
1.25 ray 111: if (S_ISREG(sb.st_mode)) {
112: close(ifd);
1.17 otto 113: return (NULL);
1.25 ray 114: }
1.17 otto 115: } else {
116: /* If ``-'' does not exist the user meant stdin. */
117: if (errno == ENOENT && strcmp(source_file, "-") == 0)
118: ifd = STDIN_FILENO;
119: else
120: err(2, "error opening %s", source_file);
121: }
122:
123: /* Not a regular file, so copy input into temporary file. */
1.19 ray 124: if (asprintf(&target_file, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1)
125: err(2, "asprintf");
126: if ((ofd = mkstemp(target_file)) == -1) {
1.17 otto 127: warn("error opening %s", target_file);
128: goto FAIL;
129: }
130: while ((rcount = read(ifd, buf, sizeof(buf))) != -1 &&
131: rcount != 0) {
132: ssize_t wcount;
133:
134: wcount = write(ofd, buf, (size_t)rcount);
135: if (-1 == wcount || rcount != wcount) {
136: warn("error writing to %s", target_file);
137: goto FAIL;
138: }
139: }
140: if (rcount == -1) {
141: warn("error reading from %s", source_file);
142: goto FAIL;
143: }
144:
145: close(ifd);
146: close(ofd);
147:
148: return (target_file);
149:
150: FAIL:
151: unlink(target_file);
152: exit(2);
153: }
154:
1.1 tedu 155: int
156: main(int argc, char **argv)
157: {
1.13 otto 158: FILE *diffpipe, *file1, *file2;
1.10 deraadt 159: size_t diffargc = 0, wflag = WIDTH;
1.1 tedu 160: int ch, fd[2], status;
161: pid_t pid;
1.28 ray 162: const char *outfile = NULL;
1.17 otto 163: char **diffargv, *diffprog = "diff", *filename1, *filename2,
164: *tmp1, *tmp2, *s1, *s2;
1.1 tedu 165:
166: /*
167: * Process diff flags.
168: */
169: /*
170: * Allocate memory for diff arguments and NULL.
171: * Each flag has at most one argument, so doubling argc gives an
172: * upper limit of how many diff args can be passed. argv[0],
173: * file1, and file2 won't have arguments so doubling them will
174: * waste some memory; however we need an extra space for the
175: * NULL at the end, so it sort of works out.
176: */
1.23 deraadt 177: if (!(diffargv = calloc(argc, sizeof(char **) * 2)))
1.6 tedu 178: err(2, "main");
1.1 tedu 179:
180: /* Add first argument, the program name. */
181: diffargv[diffargc++] = diffprog;
182:
1.8 tedu 183: while ((ch = getopt_long(argc, argv, "aBbdEHI:ilo:stWw:",
1.1 tedu 184: longopts, NULL)) != -1) {
185: const char *errstr;
186:
187: switch (ch) {
188: case 'a':
189: diffargv[diffargc++] = "-a";
190: break;
191: case 'B':
192: diffargv[diffargc++] = "-B";
193: break;
194: case 'b':
195: diffargv[diffargc++] = "-b";
196: break;
197: case 'd':
198: diffargv[diffargc++] = "-d";
199: break;
200: case 'E':
201: diffargv[diffargc++] = "-E";
202: break;
203: case 'F':
1.4 tedu 204: diffargv[0] = diffprog = optarg;
1.1 tedu 205: break;
206: case 'H':
207: diffargv[diffargc++] = "-H";
208: break;
209: case 'I':
1.13 otto 210: Iflag = 1;
1.1 tedu 211: diffargv[diffargc++] = "-I";
212: diffargv[diffargc++] = optarg;
213: break;
214: case 'i':
215: diffargv[diffargc++] = "-i";
216: break;
217: case 'l':
218: lflag = 1;
219: break;
220: case 'o':
1.28 ray 221: outfile = optarg;
1.1 tedu 222: break;
223: case 'S':
224: diffargv[diffargc++] = "--strip-trailing-cr";
225: break;
226: case 's':
227: sflag = 1;
228: break;
229: case 't':
230: diffargv[diffargc++] = "-t";
231: break;
232: case 'W':
233: diffargv[diffargc++] = "-w";
234: break;
235: case 'w':
236: wflag = strtonum(optarg, WIDTH_MIN,
1.9 tedu 237: INT_MAX, &errstr);
1.1 tedu 238: if (errstr)
239: errx(2, "width is %s: %s", errstr, optarg);
240: break;
241: default:
242: usage();
243: }
244:
245: }
246: argc -= optind;
247: argv += optind;
248:
1.18 ray 249: if (argc != 2)
1.4 tedu 250: usage();
1.19 ray 251:
1.28 ray 252: if (outfile && (outfp = fopen(outfile, "w")) == NULL)
253: err(2, "could not open: %s", optarg);
254:
1.26 ray 255: if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
1.19 ray 256: tmpdir = _PATH_TMP;
1.4 tedu 257:
1.17 otto 258: filename1 = argv[0];
259: filename2 = argv[1];
260:
261: /*
262: * Create temporary files for diff and sdiff to share if file1
263: * or file2 are not regular files. This allows sdiff and diff
264: * to read the same inputs if one or both inputs are stdin.
265: *
266: * If any temporary files were created, their names would be
267: * saved in tmp1 or tmp2. tmp1 should never equal tmp2.
268: */
269: tmp1 = tmp2 = NULL;
270: /* file1 and file2 are the same, so copy to same temp file. */
271: if (strcmp(filename1, filename2) == 0) {
272: if ((tmp1 = mktmpcpy(filename1)))
273: filename1 = filename2 = tmp1;
274: /* Copy file1 and file2 into separate temp files. */
275: } else {
276: if ((tmp1 = mktmpcpy(filename1)))
277: filename1 = tmp1;
278: if ((tmp2 = mktmpcpy(filename2)))
279: filename2 = tmp2;
280: }
281:
282: diffargv[diffargc++] = filename1;
283: diffargv[diffargc++] = filename2;
1.1 tedu 284: /* Add NULL to end of array to indicate end of array. */
285: diffargv[diffargc++] = NULL;
286:
287: /* Subtract column divider and divide by two. */
288: width = (wflag - 3) / 2;
289: /* Make sure line_width can fit in size_t. */
1.27 ray 290: if (width > (SIZE_MAX - 3) / 2)
1.1 tedu 291: errx(2, "width is too large: %zu", width);
292: line_width = width * 2 + 3;
293:
294: if (pipe(fd))
295: err(2, "pipe");
296:
297: switch(pid = fork()) {
298: case 0:
299: /* child */
300: /* We don't read from the pipe. */
1.5 tedu 301: close(fd[0]);
1.1 tedu 302: if (dup2(fd[1], STDOUT_FILENO) == -1)
303: err(2, "child could not duplicate descriptor");
304: /* Free unused descriptor. */
1.5 tedu 305: close(fd[1]);
1.1 tedu 306:
1.14 otto 307: execvp(diffprog, diffargv);
1.1 tedu 308: err(2, "could not execute diff: %s", diffprog);
309: case -1:
310: err(2, "could not fork");
311: }
312:
313: /* parent */
314: /* We don't write to the pipe. */
1.5 tedu 315: close(fd[1]);
1.1 tedu 316:
317: /* Open pipe to diff command. */
1.13 otto 318: if ((diffpipe = fdopen(fd[0], "r")) == NULL)
1.1 tedu 319: err(2, "could not open diff pipe");
1.17 otto 320: if ((file1 = fopen(filename1, "r")) == NULL)
321: err(2, "could not open %s", filename1);
322: if ((file2 = fopen(filename2, "r")) == NULL)
323: err(2, "could not open %s", filename2);
324:
1.1 tedu 325: /* Line numbers start at one. */
326: file1ln = file2ln = 1;
327:
328: /* Read and parse diff output. */
1.13 otto 329: while (parsecmd(diffpipe, file1, file2) != EOF)
1.1 tedu 330: ;
1.13 otto 331: fclose(diffpipe);
1.1 tedu 332:
333: /* Wait for diff to exit. */
334: if (waitpid(pid, &status, 0) == -1 || !WIFEXITED(status) ||
335: WEXITSTATUS(status) >= 2)
336: err(2, "diff exited abnormally");
1.17 otto 337:
338: /* Delete and free unneeded temporary files. */
339: if (tmp1)
340: if (unlink(tmp1))
341: warn("error deleting %s", tmp1);
342: if (tmp2)
343: if (unlink(tmp2))
344: warn("error deleting %s", tmp2);
345: free(tmp1);
346: free(tmp2);
347: filename1 = filename2 = tmp1 = tmp2 = NULL;
1.1 tedu 348:
349: /* No more diffs, so print common lines. */
1.13 otto 350: if (lflag)
351: while ((s1 = xfgets(file1)))
352: enqueue(s1, ' ', NULL);
353: else
354: for (;;) {
355: s1 = xfgets(file1);
356: s2 = xfgets(file2);
357: if (s1 || s2)
358: enqueue(s1, ' ', s2);
359: else
360: break;
361: }
362: fclose(file1);
363: fclose(file2);
1.1 tedu 364: /* Process unmodified lines. */
365: processq();
366:
367: /* Return diff exit status. */
368: return (WEXITSTATUS(status));
369: }
370:
371: /*
372: * Prints an individual column (left or right), taking into account
373: * that tabs are variable-width. Takes a string, the current column
1.12 deraadt 374: * the cursor is on the screen, and the maximum value of the column.
1.1 tedu 375: * The column value is updated as we go along.
376: */
377: static void
378: printcol(const char *s, size_t *col, const size_t col_max)
379: {
380: for (; *s && *col < col_max; ++s) {
381: size_t new_col;
382:
383: switch (*s) {
384: case '\t':
385: /*
386: * If rounding to next multiple of eight causes
387: * an integer overflow, just return.
388: */
1.27 ray 389: if (*col > SIZE_MAX - 8)
1.1 tedu 390: return;
391:
392: /* Round to next multiple of eight. */
393: new_col = (*col / 8 + 1) * 8;
394:
395: /*
396: * If printing the tab goes past the column
397: * width, don't print it and just quit.
398: */
399: if (new_col > col_max)
400: return;
401: *col = new_col;
402: break;
403:
404: default:
405: ++(*col);
406: }
407:
408: putchar(*s);
409: }
410: }
411:
412: /*
413: * Prompts user to either choose between two strings or edit one, both,
414: * or neither.
415: */
416: static void
417: prompt(const char *s1, const char *s2)
418: {
1.14 otto 419: char *cmd;
1.1 tedu 420:
421: /* Print command prompt. */
422: putchar('%');
423:
424: /* Get user input. */
1.14 otto 425: for (; (cmd = xfgets(stdin)); free(cmd)) {
1.1 tedu 426: const char *p;
427:
428: /* Skip leading whitespace. */
1.30 deraadt 429: for (p = cmd; isspace((unsigned char)*p); ++p)
1.1 tedu 430: ;
431:
432: switch (*p) {
433: case 'e':
434: /* Skip `e'. */
435: ++p;
436:
437: if (eparse(p, s1, s2) == -1)
438: goto USAGE;
439: break;
440:
441: case 'l':
1.22 ray 442: case '1':
1.1 tedu 443: /* Choose left column as-is. */
444: if (s1 != NULL)
1.28 ray 445: fprintf(outfp, "%s\n", s1);
1.1 tedu 446:
447: /* End of command parsing. */
448: break;
449:
450: case 'q':
451: goto QUIT;
452:
453: case 'r':
1.22 ray 454: case '2':
1.1 tedu 455: /* Choose right column as-is. */
456: if (s2 != NULL)
1.28 ray 457: fprintf(outfp, "%s\n", s2);
1.1 tedu 458:
459: /* End of command parsing. */
460: break;
461:
462: case 's':
463: sflag = 1;
464: goto PROMPT;
465:
466: case 'v':
467: sflag = 0;
468: /* FALLTHROUGH */
469:
470: default:
471: /* Interactive usage help. */
472: USAGE:
473: int_usage();
474: PROMPT:
475: putchar('%');
476:
477: /* Prompt user again. */
478: continue;
479: }
480:
1.14 otto 481: free(cmd);
1.1 tedu 482: return;
483: }
484:
485: /*
486: * If there was no error, we received an EOF from stdin, so we
487: * should quit.
488: */
489: QUIT:
1.28 ray 490: fclose(outfp);
1.1 tedu 491: exit(0);
492: }
493:
494: /*
495: * Takes two strings, separated by a column divider. NULL strings are
496: * treated as empty columns. If the divider is the ` ' character, the
497: * second column is not printed (-l flag). In this case, the second
498: * string must be NULL. When the second column is NULL, the divider
499: * does not print the trailing space following the divider character.
500: *
501: * Takes into account that tabs can take multiple columns.
502: */
503: static void
504: println(const char *s1, const char div, const char *s2)
505: {
506: size_t col;
507:
508: /* Print first column. Skips if s1 == NULL. */
509: col = 0;
510: if (s1) {
511: /* Skip angle bracket and space. */
512: printcol(s1, &col, width);
513:
514: }
515:
516: /* Only print left column. */
517: if (div == ' ' && !s2) {
518: putchar('\n');
519: return;
520: }
521:
522: /* Otherwise, we pad this column up to width. */
523: for (; col < width; ++col)
524: putchar(' ');
525:
526: /*
527: * Print column divider. If there is no second column, we don't
528: * need to add the space for padding.
529: */
530: if (!s2) {
531: printf(" %c\n", div);
532: return;
533: }
534: printf(" %c ", div);
535: col += 3;
536:
537: /* Skip angle bracket and space. */
538: printcol(s2, &col, line_width);
539:
540: putchar('\n');
541: }
542:
543: /*
544: * Reads a line from file and returns as a string. If EOF is reached,
545: * NULL is returned. The returned string must be freed afterwards.
546: */
547: static char *
548: xfgets(FILE *file)
549: {
550: const char delim[3] = {'\0', '\0', '\0'};
551: char *s;
552:
553: /* XXX - Is this necessary? */
554: clearerr(file);
555:
556: if (!(s = fparseln(file, NULL, NULL, delim, 0)) &&
557: ferror(file))
558: err(2, "error reading file");
559:
560: if (!s) {
561: return (NULL);
562: }
563:
564: return (s);
565: }
566:
567: /*
1.13 otto 568: * Parse ed commands from diffpipe and print lines from file1 (lines
569: * to change or delete) or file2 (lines to add or change).
570: * Returns EOF or 0.
1.1 tedu 571: */
572: static int
1.13 otto 573: parsecmd(FILE *diffpipe, FILE *file1, FILE *file2)
1.1 tedu 574: {
1.13 otto 575: size_t file1start, file1end, file2start, file2end, n;
1.1 tedu 576: /* ed command line and pointer to characters in line */
1.11 tedu 577: char *line, *p, *q;
578: const char *errstr;
579: char c, cmd;
1.1 tedu 580:
581: /* Read ed command. */
1.13 otto 582: if (!(line = xfgets(diffpipe)))
1.1 tedu 583: return (EOF);
584:
585: p = line;
586: /* Go to character after line number. */
1.30 deraadt 587: while (isdigit((unsigned char)*p))
1.1 tedu 588: ++p;
1.11 tedu 589: c = *p;
590: *p++ = 0;
591: file1start = strtonum(line, 0, INT_MAX, &errstr);
592: if (errstr)
593: errx(2, "file1 start is %s: %s", errstr, line);
1.1 tedu 594:
595: /* A range is specified for file1. */
1.11 tedu 596: if (c == ',') {
1.1 tedu 597:
1.11 tedu 598: q = p;
599: /* Go to character after file2end. */
1.30 deraadt 600: while (isdigit((unsigned char)*p))
1.11 tedu 601: ++p;
602: c = *p;
603: *p++ = 0;
604: file1end = strtonum(q, 0, INT_MAX, &errstr);
605: if (errstr)
606: errx(2, "file1 end is %s: %s", errstr, line);
1.1 tedu 607: if (file1start > file1end)
608: errx(2, "invalid line range in file1: %s", line);
609:
610: } else
611: file1end = file1start;
612:
1.11 tedu 613: cmd = c;
1.1 tedu 614: /* Check that cmd is valid. */
615: if (!(cmd == 'a' || cmd == 'c' || cmd == 'd'))
616: errx(2, "ed command not recognized: %c: %s", cmd, line);
617:
1.11 tedu 618: q = p;
1.1 tedu 619: /* Go to character after line number. */
1.30 deraadt 620: while (isdigit((unsigned char)*p))
1.1 tedu 621: ++p;
1.11 tedu 622: c = *p;
623: *p++ = 0;
624: file2start = strtonum(q, 0, INT_MAX, &errstr);
625: if (errstr)
626: errx(2, "file2 start is %s: %s", errstr, line);
1.1 tedu 627:
628: /*
629: * There should either be a comma signifying a second line
630: * number or the line should just end here.
631: */
1.11 tedu 632: if (c != ',' && c != '\0')
633: errx(2, "invalid line range in file2: %c: %s", c, line);
1.1 tedu 634:
1.11 tedu 635: if (c == ',') {
1.1 tedu 636:
1.11 tedu 637: file2end = strtonum(p, 0, INT_MAX, &errstr);
638: if (errstr)
639: errx(2, "file2 end is %s: %s", errstr, line);
1.1 tedu 640: if (file2start >= file2end)
641: errx(2, "invalid line range in file2: %s", line);
642: } else
643: file2end = file2start;
644:
645: /* Appends happen _after_ stated line. */
646: if (cmd == 'a') {
647: if (file1start != file1end)
648: errx(2, "append cannot have a file1 range: %s",
649: line);
1.27 ray 650: if (file1start == SIZE_MAX)
1.1 tedu 651: errx(2, "file1 line range too high: %s", line);
652: file1start = ++file1end;
653: }
654: /*
655: * I'm not sure what the deal is with the line numbers for
656: * deletes, though.
657: */
658: else if (cmd == 'd') {
659: if (file2start != file2end)
660: errx(2, "delete cannot have a file2 range: %s",
661: line);
1.27 ray 662: if (file2start == SIZE_MAX)
1.1 tedu 663: errx(2, "file2 line range too high: %s", line);
664: file2start = ++file2end;
665: }
666:
1.13 otto 667: /*
668: * Continue reading file1 and file2 until we reach line numbers
669: * specified by diff. Should only happen with -I flag.
670: */
671: for (; file1ln < file1start && file2ln < file2start;
672: ++file1ln, ++file2ln) {
1.14 otto 673: char *s1, *s2;
1.1 tedu 674:
1.13 otto 675: if (!(s1 = xfgets(file1)))
1.1 tedu 676: errx(2, "file1 shorter than expected");
1.13 otto 677: if (!(s2 = xfgets(file2)))
678: errx(2, "file2 shorter than expected");
1.1 tedu 679:
680: /* If the -l flag was specified, print only left column. */
1.13 otto 681: if (lflag) {
1.14 otto 682: free(s2);
1.13 otto 683: /*
684: * XXX - If -l and -I are both specified, all
685: * unchanged or ignored lines are shown with a
686: * `(' divider. This matches GNU sdiff, but I
687: * believe it is a bug. Just check out:
688: * gsdiff -l -I '^$' samefile samefile.
689: */
690: if (Iflag)
691: enqueue(s1, '(', NULL);
692: else
693: enqueue(s1, ' ', NULL);
694: } else
695: enqueue(s1, ' ', s2);
696: }
697: /* Ignore deleted lines. */
698: for (; file1ln < file1start; ++file1ln) {
1.14 otto 699: char *s;
1.13 otto 700:
701: if (!(s = xfgets(file1)))
702: errx(2, "file1 shorter than expected");
703:
704: enqueue(s, '(', NULL);
1.1 tedu 705: }
1.13 otto 706: /* Ignore added lines. */
707: for (; file2ln < file2start; ++file2ln) {
1.14 otto 708: char *s;
1.13 otto 709:
710: if (!(s = xfgets(file2)))
711: errx(2, "file2 shorter than expected");
712:
713: /* If -l flag was given, don't print right column. */
714: if (lflag)
1.14 otto 715: free(s);
1.13 otto 716: else
717: enqueue(NULL, ')', s);
718: }
719:
720: /* Process unmodified or skipped lines. */
1.1 tedu 721: processq();
722:
723: switch (cmd) {
724: case 'a':
1.13 otto 725: printa(file2, file2end);
726: n = file2end - file2start + 1;
1.1 tedu 727: break;
728:
729: case 'c':
1.13 otto 730: printc(file1, file1end, file2, file2end);
731: n = file1end - file1start + 1 + 1 + file2end - file2start + 1;
1.1 tedu 732: break;
733:
734: case 'd':
1.13 otto 735: printd(file1, file1end);
736: n = file1end - file1start + 1;
1.1 tedu 737: break;
738:
739: default:
740: errx(2, "invalid diff command: %c: %s", cmd, line);
741: }
1.29 tobias 742: free(line);
1.1 tedu 743:
1.13 otto 744: /* Skip to next ed line. */
1.29 tobias 745: while (n--) {
746: if (!(line = xfgets(diffpipe)))
1.13 otto 747: errx(2, "diff ended early");
1.29 tobias 748: free(line);
749: }
1.13 otto 750:
1.3 tedu 751: return (0);
1.1 tedu 752: }
753:
754: /*
755: * Queues up a diff line.
756: */
757: static void
1.14 otto 758: enqueue(char *left, char div, char *right)
1.1 tedu 759: {
760: struct diffline *diffp;
761:
762: if (!(diffp = malloc(sizeof(struct diffline))))
1.6 tedu 763: err(2, "enqueue");
1.1 tedu 764: diffp->left = left;
765: diffp->div = div;
766: diffp->right = right;
767: SIMPLEQ_INSERT_TAIL(&diffhead, diffp, diffentries);
768: }
769:
770: /*
771: * Free a diffline structure and its elements.
772: */
773: static void
1.14 otto 774: freediff(struct diffline *diffp)
1.1 tedu 775: {
1.14 otto 776: free(diffp->left);
777: free(diffp->right);
778: free(diffp);
1.1 tedu 779: }
780:
781: /*
782: * Append second string into first. Repeated appends to the same string
783: * are cached, making this an O(n) function, where n = strlen(append).
784: */
785: static void
786: astrcat(char **s, const char *append)
787: {
788: /* Length of string in previous run. */
789: static size_t offset = 0;
1.15 otto 790: size_t newsiz;
1.1 tedu 791: /*
792: * String from previous run. Compared to *s to see if we are
793: * dealing with the same string. If so, we can use offset.
794: */
1.14 otto 795: static const char *oldstr = NULL;
1.1 tedu 796: char *newstr;
797:
798:
799: /*
800: * First string is NULL, so just copy append.
801: */
802: if (!*s) {
803: if (!(*s = strdup(append)))
1.6 tedu 804: err(2, "astrcat");
1.1 tedu 805:
806: /* Keep track of string. */
807: offset = strlen(*s);
808: oldstr = *s;
809:
810: return;
811: }
812:
813: /*
814: * *s is a string so concatenate.
815: */
816:
817: /* Did we process the same string in the last run? */
818: /*
819: * If this is a different string from the one we just processed
820: * cache new string.
821: */
822: if (oldstr != *s) {
823: offset = strlen(*s);
824: oldstr = *s;
825: }
826:
1.15 otto 827: /* Size = strlen(*s) + \n + strlen(append) + '\0'. */
828: newsiz = offset + 1 + strlen(append) + 1;
1.1 tedu 829:
830: /* Resize *s to fit new string. */
1.15 otto 831: newstr = realloc(*s, newsiz);
1.1 tedu 832: if (newstr == NULL)
1.6 tedu 833: err(2, "astrcat");
1.1 tedu 834: *s = newstr;
835:
1.15 otto 836: /* *s + offset should be end of string. */
1.1 tedu 837: /* Concatenate. */
1.15 otto 838: strlcpy(*s + offset, "\n", newsiz - offset);
839: strlcat(*s + offset, append, newsiz - offset);
1.1 tedu 840:
1.15 otto 841: /* New string length should be exactly newsiz - 1 characters. */
1.1 tedu 842: /* Store generated string's values. */
1.15 otto 843: offset = newsiz - 1;
1.1 tedu 844: oldstr = *s;
845: }
846:
847: /*
848: * Process diff set queue, printing, prompting, and saving each diff
849: * line stored in queue.
850: */
851: static void
852: processq(void)
853: {
854: struct diffline *diffp;
1.14 otto 855: char divc, *left, *right;
1.1 tedu 856:
857: /* Don't process empty queue. */
858: if (SIMPLEQ_EMPTY(&diffhead))
859: return;
860:
1.16 otto 861: /* Remember the divider. */
862: divc = SIMPLEQ_FIRST(&diffhead)->div;
863:
1.1 tedu 864: left = NULL;
865: right = NULL;
866: /*
867: * Go through set of diffs, concatenating each line in left or
868: * right column into two long strings, `left' and `right'.
869: */
870: SIMPLEQ_FOREACH(diffp, &diffhead, diffentries) {
871: /*
1.13 otto 872: * Print changed lines if -s was given,
873: * print all lines if -s was not given.
1.1 tedu 874: */
1.16 otto 875: if (!sflag || diffp->div == '|' || diffp->div == '<' ||
876: diffp->div == '>')
1.1 tedu 877: println(diffp->left, diffp->div, diffp->right);
878:
879: /* Append new lines to diff set. */
880: if (diffp->left)
881: astrcat(&left, diffp->left);
882: if (diffp->right)
883: astrcat(&right, diffp->right);
884: }
885:
886: /* Empty queue and free each diff line and its elements. */
887: while (!SIMPLEQ_EMPTY(&diffhead)) {
888: diffp = SIMPLEQ_FIRST(&diffhead);
1.13 otto 889: SIMPLEQ_REMOVE_HEAD(&diffhead, diffentries);
1.1 tedu 890: freediff(diffp);
891: }
892:
1.28 ray 893: /* Write to outfp, prompting user if lines are different. */
894: if (outfp)
1.14 otto 895: switch (divc) {
1.13 otto 896: case ' ': case '(': case ')':
1.28 ray 897: fprintf(outfp, "%s\n", left);
1.13 otto 898: break;
899: case '|': case '<': case '>':
1.1 tedu 900: prompt(left, right);
1.13 otto 901: break;
902: default:
1.14 otto 903: errx(2, "invalid divider: %c", divc);
1.13 otto 904: }
1.1 tedu 905:
906: /* Free left and right. */
1.14 otto 907: free(left);
908: free(right);
1.1 tedu 909: }
910:
911: /*
912: * Print lines following an (a)ppend command.
913: */
914: static void
915: printa(FILE *file, size_t line2)
916: {
917: char *line;
918:
919: for (; file2ln <= line2; ++file2ln) {
920: if (!(line = xfgets(file)))
921: errx(2, "append ended early");
922: enqueue(NULL, '>', line);
923: }
924:
925: processq();
926: }
927:
928: /*
929: * Print lines following a (c)hange command, from file1ln to file1end
930: * and from file2ln to file2end.
931: */
932: static void
933: printc(FILE *file1, size_t file1end, FILE *file2, size_t file2end)
934: {
935: struct fileline {
1.14 otto 936: SIMPLEQ_ENTRY(fileline) fileentries;
937: char *line;
1.1 tedu 938: };
939: SIMPLEQ_HEAD(, fileline) delqhead = SIMPLEQ_HEAD_INITIALIZER(delqhead);
940:
941: /* Read lines to be deleted. */
942: for (; file1ln <= file1end; ++file1ln) {
943: struct fileline *linep;
1.14 otto 944: char *line1;
1.1 tedu 945:
946: /* Read lines from both. */
947: if (!(line1 = xfgets(file1)))
948: errx(2, "error reading file1 in delete in change");
949:
950: /* Add to delete queue. */
951: if (!(linep = malloc(sizeof(struct fileline))))
1.6 tedu 952: err(2, "printc");
1.1 tedu 953: linep->line = line1;
954: SIMPLEQ_INSERT_TAIL(&delqhead, linep, fileentries);
955: }
956:
957: /* Process changed lines.. */
958: for (; !SIMPLEQ_EMPTY(&delqhead) && file2ln <= file2end;
959: ++file2ln) {
960: struct fileline *del;
961: char *add;
962:
963: /* Get add line. */
1.13 otto 964: if (!(add = xfgets(file2)))
965: errx(2, "error reading add in change");
1.1 tedu 966:
967: del = SIMPLEQ_FIRST(&delqhead);
968: enqueue(del->line, '|', add);
969: SIMPLEQ_REMOVE_HEAD(&delqhead, fileentries);
970: /*
971: * Free fileline structure but not its elements since
972: * they are queued up.
973: */
974: free(del);
975: }
976: processq();
977:
978: /* Process remaining lines to add. */
979: for (; file2ln <= file2end; ++file2ln) {
980: char *add;
981:
982: /* Get add line. */
1.13 otto 983: if (!(add = xfgets(file2)))
984: errx(2, "error reading add in change");
1.1 tedu 985:
986: enqueue(NULL, '>', add);
987: }
988: processq();
989:
990: /* Process remaining lines to delete. */
991: while (!SIMPLEQ_EMPTY(&delqhead)) {
992: struct fileline *filep;
993:
994: filep = SIMPLEQ_FIRST(&delqhead);
995: enqueue(filep->line, '<', NULL);
996: SIMPLEQ_REMOVE_HEAD(&delqhead, fileentries);
997: free(filep);
998: }
999: processq();
1000: }
1001:
1002: /*
1003: * Print deleted lines from file, from file1ln to file1end.
1004: */
1005: static void
1.13 otto 1006: printd(FILE *file1, size_t file1end)
1.1 tedu 1007: {
1.14 otto 1008: char *line1;
1.1 tedu 1009:
1010: /* Print out lines file1ln to line2. */
1011: for (; file1ln <= file1end; ++file1ln) {
1012: if (!(line1 = xfgets(file1)))
1013: errx(2, "file1 ended early in delete");
1014: enqueue(line1, '<', NULL);
1015: }
1016: processq();
1017: }
1018:
1019: /*
1020: * Interactive mode usage.
1021: */
1022: static void
1023: int_usage(void)
1024: {
1025: puts("e:\tedit blank diff\n"
1026: "eb:\tedit both diffs concatenated\n"
1027: "el:\tedit left diff\n"
1028: "er:\tedit right diff\n"
1.22 ray 1029: "l | 1:\tchoose left diff\n"
1030: "r | 2:\tchoose right diff\n"
1.1 tedu 1031: "s:\tsilent mode--don't print identical lines\n"
1032: "v:\tverbose mode--print identical lines\n"
1033: "q:\tquit");
1034: }
1035:
1036: static void
1037: usage(void)
1038: {
1039: extern char *__progname;
1040:
1041: fprintf(stderr,
1.8 tedu 1042: "usage: %s [-abdilstW] [-I regexp] [-o outfile] [-w width] file1 file2\n",
1.1 tedu 1043: __progname);
1044: exit(2);
1045: }