[BACK]Return to sdiff.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sdiff

Annotation of src/usr.bin/sdiff/sdiff.c, Revision 1.27

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