]> git.neil.brown.name Git - wiggle.git/blob - wiggle.c
Browser: add 'c' command to accept a conflict.
[wiggle.git] / wiggle.c
1 /*
2  * wiggle - apply rejected patches
3  *
4  * Copyright (C) 2003 Neil Brown <neilb@cse.unsw.edu.au>
5  * Copyright (C) 2010 Neil Brown <neilb@suse.de>
6  *
7  *
8  *    This program is free software; you can redistribute it and/or modify
9  *    it under the terms of the GNU General Public License as published by
10  *    the Free Software Foundation; either version 2 of the License, or
11  *    (at your option) any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License
19  *    along with this program; if not, write to the Free Software Foundation, Inc.,
20  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  *    Author: Neil Brown
23  *    Email: <neilb@suse.de>
24  */
25
26 /*
27  * Wiggle is a tool for working with patches that don't quite apply properly.
28  * It provides functionality similar to 'diff' and 'merge' but can
29  * work at the level of individual words thus allowing the merging of
30  * two changes that affect the same line, but not the same parts of that line.
31  *
32  * Wiggle can also read patch and merge files.  Unlike 'merge' it does not
33  * need to be given three separate files, but can be given a file and a patch
34  * and it will extract the pieces of the two other files that it needs from
35  * the patch.
36  *
37  * Wiggle performs one of three core function:
38  *   --extract -x    extract part of a patch or merge file
39  *   --diff -d       report differences between two files
40  *   --merge -m      merge the changes between two files into a third file
41  *
42  * This is also a --browse (-B) mode which provides interactive access
43  * to the merger.
44  *
45  * To perform these, wiggle requires 1, 2, or 3 input streams respectively.
46  * I can get these from individual files, from a diff (unified or context) or
47  * from a merge file.
48  *
49  * For merge:
50  *    If one file is given, it is a merge file (output of 'merge').
51  *    If two files are given, the second is assumed to be a patch,
52  *         the first is a normal file.
53  *    If three files are given, they are taken to be normal files.
54  *
55  * For diff:
56  *    If one file is given, it is a patch
57  *    If two files are given, they are normal files.
58  *
59  * For extract:
60  *    Only one file can be given. -p indicates it is a patch,
61  *        otherwise it is a merge.
62  *    One of the flags -1 -2 or -3 must also be given and they indicate which
63  *    part of the patch or merge to extract.
64  *
65  * Difference calculation and merging is performed on lines (-l) or words (-w).
66  * Each 'word' is either 1/all alphnumeric (or '_'), 2/ all space or tab,
67  * or 3/ any other single character.
68  *
69  * In the case of -w, an initial diff is computed based on non-trivial words
70  * which includes alhpanumeric words and newlines.
71  *
72  * This diff is computed from the ends of the file and is used to find
73  * a suitable starting point and range.  Then a more precise diff is
74  * computed over that restricted range
75  *
76  * Other options available are:
77  *   --replace -r   replace first file with  result of merge.
78  *   --help -h      provide help
79  *   --version -v   version
80  *
81  * Defaults are --merge --words
82  *
83  */
84 #define _GNU_SOURCE
85 #include        "wiggle.h"
86 #include        <errno.h>
87 #include        <fcntl.h>
88 #include        <unistd.h>
89 #include        <stdlib.h>
90 #include        <stdio.h>
91 #include        <ctype.h>
92 #include        <sys/stat.h>
93
94 char *Cmd = "wiggle";
95 int do_trace = 0;
96
97 void die(char *reason)
98 {
99         fprintf(stderr, "%s: fatal error: %s failure\n", Cmd, reason);
100         exit(3);
101 }
102
103 void check_dir(char *name, int fd)
104 {
105         struct stat st;
106         if (fstat(fd, &st) != 0) {
107                 fprintf(stderr, "%s: fatal: %s is strange\n", Cmd, name);
108                 exit(3);
109         }
110         if (S_ISDIR(st.st_mode)) {
111                 fprintf(stderr, "%s: %s is a directory\n", Cmd, name);
112                 exit(3);
113         }
114 }
115
116 void *xmalloc(int size)
117 {
118         void *rv = malloc(size);
119         if (size && !rv) {
120                 char *msg = "Failed to allocate memory - aborting\n";
121                 write(2, msg, strlen(msg));
122                 exit(3);
123         }
124         return rv;
125 }
126
127 void printword(FILE *f, struct elmnt e)
128 {
129         if (e.start[0])
130                 fprintf(f, "%.*s", e.plen + e.prefix,
131                         e.start - e.prefix);
132         else {
133                 int a, b, c;
134                 sscanf(e.start+1, "%d %d %d", &a, &b, &c);
135                 fprintf(f, "*** %d,%d **** %d\n", b, c, a);
136         }
137 }
138
139 static void printsep(struct elmnt e1, struct elmnt e2)
140 {
141         int a, b, c, d, e, f;
142         sscanf(e1.start+1, "%d %d %d", &a, &b, &c);
143         sscanf(e2.start+1, "%d %d %d", &d, &e, &f);
144         printf("@@ -%d,%d +%d,%d @@\n", b, c, e, f);
145 }
146
147 static int extract(int argc, char *argv[], int ispatch, int which)
148 {
149         /* extract a branch of a diff or diff3 or merge output
150          * We need one file
151          */
152         struct stream f, flist[3];
153
154         if (argc == 0) {
155                 fprintf(stderr,
156                         "%s: no file given for --extract\n", Cmd);
157                 return 2;
158         }
159         if (argc > 1) {
160                 fprintf(stderr,
161                         "%s: only give one file for --extract\n", Cmd);
162                 return 2;
163         }
164         f = load_file(argv[0]);
165         if (f.body == NULL) {
166                 fprintf(stderr,
167                         "%s: cannot load file '%s' - %s\n", Cmd,
168                         argv[0], strerror(errno));
169                 return 2;
170         }
171         if (ispatch) {
172                 if (split_patch(f, &flist[0], &flist[1]) == 0) {
173                         fprintf(stderr,
174                                 "%s: No chunk found in patch: %s\n", Cmd,
175                                 argv[0]);
176                         return 0;
177                 }
178         } else {
179                 if (!split_merge(f, &flist[0], &flist[1], &flist[2])) {
180                         fprintf(stderr,
181                                 "%s: merge file %s looks bad.\n", Cmd,
182                                 argv[0]);
183                         return 2;
184                 }
185         }
186         if (flist[which-'1'].body == NULL) {
187                 fprintf(stderr,
188                         "%s: %s has no -%c component.\n", Cmd,
189                         argv[0], which);
190                 return 2;
191         } else {
192                 if (write(1, flist[which-'1'].body,
193                           flist[which-'1'].len)
194                     != flist[which-'1'].len)
195                         return 2;
196         }
197         return 0;
198 }
199
200 static int do_diff_lines(struct file fl[2], struct csl *csl)
201 {
202         int a, b;
203         int exit_status = 0;
204         a = b = 0;
205         while (a < fl[0].elcnt || b < fl[1].elcnt) {
206                 if (a < csl->a) {
207                         if (fl[0].list[a].start[0]) {
208                                 printf("-");
209                                 printword(stdout,
210                                           fl[0].list[a]);
211                         }
212                         a++;
213                         exit_status++;
214                 } else if (b < csl->b) {
215                         if (fl[1].list[b].start[0]) {
216                                 printf("+");
217                                 printword(stdout,
218                                           fl[1].list[b]);
219                         }
220                         b++;
221                         exit_status++;
222                 } else {
223                         if (fl[0].list[a].start[0] == '\0')
224                                 printsep(fl[0].list[a],
225                                          fl[1].list[b]);
226                         else {
227                                 printf(" ");
228                                 printword(stdout,
229                                           fl[0].list[a]);
230                         }
231                         a++;
232                         b++;
233                         if (a >= csl->a+csl->len)
234                                 csl++;
235                 }
236         }
237         return exit_status;
238 }
239
240 static int do_diff_words(struct file fl[2], struct csl *csl)
241 {
242         int a, b;
243         int exit_status  = 0;
244         int sol = 1; /* start of line */
245         a = b = 0;
246         while (a < fl[0].elcnt || b < fl[1].elcnt) {
247                 if (a < csl->a) {
248                         exit_status++;
249                         if (sol) {
250                                 int a1;
251                                 /* If we remove a
252                                  * whole line, output
253                                  * +line else clear
254                                  * sol and retry */
255                                 sol = 0;
256                                 for (a1 = a; a1 < csl->a ; a1++)
257                                         if (ends_line(fl[0].list[a1])) {
258                                                 sol = 1;
259                                                 break;
260                                         }
261                                 if (sol) {
262                                         printf("-");
263                                         for (; a < csl->a ; a++) {
264                                                 printword(stdout, fl[0].list[a]);
265                                                 if (ends_line(fl[0].list[a])) {
266                                                         a++;
267                                                         break;
268                                                 }
269                                         }
270                                 } else
271                                         printf("|");
272                         }
273                         if (!sol) {
274                                 printf("<<<--");
275                                 do {
276                                         if (sol)
277                                                 printf("|");
278                                         printword(stdout, fl[0].list[a]);
279                                         sol = ends_line(fl[0].list[a]);
280                                         a++;
281                                 } while (a < csl->a);
282                                 printf("%s-->>>", sol ? "|" : "");
283                                 sol = 0;
284                         }
285                 } else if (b < csl->b) {
286                         exit_status++;
287                         if (sol) {
288                                 int b1;
289                                 sol = 0;
290                                 for (b1 = b; b1 < csl->b; b1++)
291                                         if (ends_line(fl[1].list[b1])) {
292                                                 sol = 1;
293                                                 break;
294                                         }
295                                 if (sol) {
296                                         printf("+");
297                                         for (; b < csl->b ; b++) {
298                                                 printword(stdout, fl[1].list[b]);
299                                                 if (ends_line(fl[1].list[b])) {
300                                                         b++;
301                                                         break;
302                                                 }
303                                         }
304                                 } else
305                                         printf("|");
306                         }
307                         if (!sol) {
308                                 printf("<<<++");
309                                 do {
310                                         if (sol)
311                                                 printf("|");
312                                         printword(stdout, fl[1].list[b]);
313                                         sol = ends_line(fl[1].list[b]);
314                                         b++;
315                                 } while (b < csl->b);
316                                 printf("%s++>>>", sol ? "|" : "");
317                                 sol = 0;
318                         }
319                 } else {
320                         if (sol) {
321                                 int a1;
322                                 sol = 0;
323                                 for (a1 = a; a1 < csl->a+csl->len; a1++)
324                                         if (ends_line(fl[0].list[a1]))
325                                                 sol = 1;
326                                 if (sol) {
327                                         if (fl[0].list[a].start[0]) {
328                                                 printf(" ");
329                                                 for (; a < csl->a+csl->len; a++, b++) {
330                                                         printword(stdout, fl[0].list[a]);
331                                                         if (ends_line(fl[0].list[a])) {
332                                                                 a++, b++;
333                                                                 break;
334                                                         }
335                                                 }
336                                         } else {
337                                                 printsep(fl[0].list[a], fl[1].list[b]);
338                                                 a++; b++;
339                                         }
340                                 } else
341                                         printf("|");
342                         }
343                         if (!sol) {
344                                 printword(stdout, fl[0].list[a]);
345                                 if (ends_line(fl[0].list[a]))
346                                         sol = 1;
347                                 a++;
348                                 b++;
349                         }
350                         if (a >= csl->a+csl->len)
351                                 csl++;
352                 }
353         }
354         return exit_status;
355 }
356
357 static int do_diff(int argc, char *argv[], int obj, int ispatch,
358                    int which, int reverse)
359 {
360         /* create a diff (line or char) of two streams */
361         struct stream f, flist[3];
362         int chunks1 = 0, chunks2 = 0, chunks3 = 0;
363         int exit_status = 0;
364         struct file fl[2];
365         struct csl *csl;
366
367         switch (argc) {
368         case 0:
369                 fprintf(stderr, "%s: no file given for --diff\n", Cmd);
370                 return 2;
371         case 1:
372                 f = load_file(argv[0]);
373                 if (f.body == NULL) {
374                         fprintf(stderr,
375                                 "%s: cannot load file '%s' - %s\n", Cmd,
376                                 argv[0], strerror(errno));
377                         return 2;
378                 }
379                 chunks1 = chunks2 =
380                         split_patch(f, &flist[0], &flist[1]);
381                 if (!flist[0].body || !flist[1].body) {
382                         fprintf(stderr,
383                                 "%s: couldn't parse patch %s\n", Cmd,
384                                 argv[0]);
385                         return 2;
386                 }
387                 break;
388         case 2:
389                 flist[0] = load_file(argv[0]);
390                 if (flist[0].body == NULL) {
391                         fprintf(stderr,
392                                 "%s: cannot load file '%s' - %s\n", Cmd,
393                                 argv[0], strerror(errno));
394                         return 2;
395                 }
396                 if (ispatch) {
397                         f = load_file(argv[1]);
398                         if (f.body == NULL) {
399                                 fprintf(stderr,
400                                         "%s: cannot load patch '%s' - %s\n", Cmd,
401                                         argv[1], strerror(errno));
402                                 return 2;
403                         }
404                         if (which == '2')
405                                 chunks2 = chunks3 =
406                                         split_patch(f, &flist[2],
407                                                     &flist[1]);
408                         else
409                                 chunks2 = chunks3 =
410                                         split_patch(f, &flist[1],
411                                                     &flist[2]);
412
413                 } else
414                         flist[1] = load_file(argv[1]);
415                 if (flist[1].body == NULL) {
416                         fprintf(stderr,
417                                 "%s: cannot load file '%s' - %s\n", Cmd,
418                                 argv[1], strerror(errno));
419                         return 2;
420                 }
421                 break;
422         default:
423                 fprintf(stderr,
424                         "%s: too many files given for --diff\n", Cmd);
425                 return 2;
426         }
427         if (reverse) {
428                 f = flist[1];
429                 flist[1] = flist[2];
430                 flist[2] = f;
431         }
432         fl[0] = split_stream(flist[0], obj);
433         fl[1] = split_stream(flist[1], obj);
434         if (chunks2 && !chunks1)
435                 csl = pdiff(fl[0], fl[1], chunks2);
436         else
437                 csl = diff_patch(fl[0], fl[1]);
438         if ((obj & ByMask) == ByLine) {
439                 if (!chunks1)
440                         printf("@@ -1,%d +1,%d @@\n",
441                                fl[0].elcnt, fl[1].elcnt);
442                 exit_status = do_diff_lines(fl, csl);
443         } else {
444                 if (!chunks1) {
445                         /* count lines in each file */
446                         int l1, l2, i;
447                         l1 = l2 = 0;
448                         for (i = 0 ; i < fl[0].elcnt ; i++)
449                                 if (ends_line(fl[0].list[i]))
450                                         l1++;
451                         for (i = 0 ; i < fl[1].elcnt ; i++)
452                                 if (ends_line(fl[1].list[i]))
453                                         l2++;
454                         printf("@@ -1,%d +1,%d @@\n", l1, l2);
455                 }
456                 exit_status = do_diff_words(fl, csl);
457         }
458         return exit_status;
459 }
460
461 static int do_merge(int argc, char *argv[], int obj, int blanks,
462                     int reverse, int replace, int ignore, int show_wiggles,
463                     int quiet)
464 {
465         /* merge three files, A B C, so changed between B and C get made to A
466          */
467         struct stream f, flist[3];
468         struct file fl[3];
469         int i;
470         int chunks1 = 0, chunks2 = 0, chunks3 = 0;
471         char *replacename = NULL, *orignew = NULL;
472         struct csl *csl1, *csl2;
473         struct ci ci;
474         FILE *outfile = stdout;
475
476         switch (argc) {
477         case 0:
478                 fprintf(stderr, "%s: no files given for --merge\n", Cmd);
479                 return 2;
480         case 3:
481         case 2:
482         case 1:
483                 for (i = 0; i < argc; i++) {
484                         flist[i] = load_file(argv[i]);
485                         if (flist[i].body == NULL) {
486                                 fprintf(stderr, "%s: cannot load file '%s' - %s\n",
487                                         Cmd,
488                                         argv[i], strerror(errno));
489                                 return 2;
490                         }
491                 }
492                 break;
493         default:
494                 fprintf(stderr, "%s: too many files given for --merge\n",
495                         Cmd);
496                 return 2;
497         }
498         switch (argc) {
499         case 1: /* a merge file */
500                 f = flist[0];
501                 if (!split_merge(f, &flist[0], &flist[1], &flist[2])) {
502                         fprintf(stderr, "%s: merge file %s looks bad.\n",
503                                 Cmd,
504                                 argv[0]);
505                         return 2;
506                 }
507                 break;
508         case 2: /* a file and a patch */
509                 f = flist[1];
510                 chunks2 = chunks3 = split_patch(f, &flist[1], &flist[2]);
511                 break;
512         case 3: /* three separate files */
513                 break;
514         }
515         if (reverse) {
516                 f = flist[1];
517                 flist[1] = flist[2];
518                 flist[2] = f;
519         }
520
521         for (i = 0; i < 3; i++) {
522                 if (flist[i].body == NULL) {
523                         fprintf(stderr, "%s: file %d missing\n", Cmd, i);
524                         return 2;
525                 }
526         }
527         if (replace) {
528                 int fd;
529                 replacename = xmalloc(strlen(argv[0]) + 20);
530                 orignew = xmalloc(strlen(argv[0]) + 20);
531                 strcpy(replacename, argv[0]);
532                 strcpy(orignew, argv[0]);
533                 strcat(orignew, ".porig");
534                 if (open(orignew, O_RDONLY) >= 0 ||
535                     errno != ENOENT) {
536                         fprintf(stderr, "%s: %s already exists\n",
537                                 Cmd,
538                                 orignew);
539                         return 2;
540                 }
541                 strcat(replacename, "XXXXXX");
542                 fd = mkstemp(replacename);
543                 if (fd == -1) {
544                         fprintf(stderr,
545                                 "%s: could not create temporary file for %s\n",
546                                 Cmd,
547                                 replacename);
548                         return 2;
549                 }
550                 outfile = fdopen(fd, "w");
551         }
552
553         if (obj == 'l')
554                 blanks |= ByLine;
555         else
556                 blanks |= ByWord;
557         fl[0] = split_stream(flist[0], blanks);
558         fl[1] = split_stream(flist[1], blanks);
559         fl[2] = split_stream(flist[2], blanks);
560
561         if (chunks2 && !chunks1)
562                 csl1 = pdiff(fl[0], fl[1], chunks2);
563         else
564                 csl1 = diff(fl[0], fl[1]);
565         csl2 = diff_patch(fl[1], fl[2]);
566
567         ci = make_merger(fl[0], fl[1], fl[2], csl1, csl2,
568                          obj == 'w', ignore, show_wiggles);
569         print_merge(outfile, &fl[0], &fl[1], &fl[2],
570                     obj == 'w', ci.merger);
571         if (!quiet && ci.conflicts)
572                 fprintf(stderr,
573                         "%d unresolved conflict%s found\n",
574                         ci.conflicts,
575                         ci.conflicts == 1 ? "" : "s");
576         if (!quiet && ci.ignored)
577                 fprintf(stderr,
578                         "%d already-applied change%s ignored\n",
579                         ci.ignored,
580                         ci.ignored == 1 ? "" : "s");
581
582         if (replace) {
583                 fclose(outfile);
584                 if (rename(argv[0], orignew) == 0 &&
585                     rename(replacename, argv[0]) == 0)
586                         /* all ok */;
587                 else {
588                         fprintf(stderr,
589                                 "%s: failed to move new file into place.\n",
590                                 Cmd);
591                         return 2;
592                 }
593         }
594         return (ci.conflicts > 0);
595 }
596
597 static int multi_merge(int argc, char *argv[], int obj, int blanks,
598                        int reverse, int ignore, int show_wiggles,
599                        int replace, int strip,
600                        int quiet)
601 {
602         FILE *f;
603         char *filename;
604         struct plist *pl;
605         int num_patches;
606         int rv = 0;
607         int i;
608
609         if (!replace) {
610                 fprintf(stderr,
611                         "%s: -p in merge mode requires -r\n",
612                         Cmd);
613                 return 2;
614         }
615         if (argc != 1) {
616                 fprintf(stderr,
617                         "%s: -p in merge mode requires exactly one file\n",
618                         Cmd);
619                 return 2;
620         }
621         filename = argv[0];
622         f = fopen(filename, "r");
623         if (!f) {
624                 fprintf(stderr, "%s: cannot open %s\n",
625                         Cmd, filename);
626                 return 2;
627         }
628         check_dir(filename, fileno(f));
629         pl = parse_patch(f, NULL, &num_patches);
630         fclose(f);
631         if (set_prefix(pl, num_patches, strip) == 0) {
632                 fprintf(stderr, "%s: aborting\n", Cmd);
633                 return 2;
634         }
635         for (i = 0; i < num_patches; i++) {
636                 char *name;
637                 char *av[2];
638                 asprintf(&name, "_wiggle_:%d:%d:%s",
639                          pl[i].start, pl[i].end, filename);
640                 av[0] = pl[i].file;
641                 av[1] = name;
642                 rv |= do_merge(2, av, obj, blanks, reverse, 1, ignore,
643                          show_wiggles, quiet);
644         }
645         return rv;
646 }
647
648 int main(int argc, char *argv[])
649 {
650         int opt;
651         int option_index;
652         int mode = 0;
653         int obj = 0;
654         int replace = 0;
655         int which = 0;
656         int ispatch = 0;
657         int reverse = 0;
658         int verbose = 0, quiet = 0;
659         int strip = -1;
660         int exit_status = 0;
661         int ignore = 1;
662         int show_wiggles = 0;
663         char *helpmsg;
664         char *trace;
665         int selftest = 0;
666         int ignore_blanks = 0;
667
668         trace = getenv("WIGGLE_TRACE");
669         if (trace && *trace)
670                 do_trace = 1;
671
672         while ((opt = getopt_long(argc, argv,
673                                   short_options, long_options,
674                                   &option_index)) != -1)
675                 switch (opt) {
676                 case 'h':
677                         helpmsg = Help;
678                         switch (mode) {
679                         case 'x':
680                                 helpmsg = HelpExtract;
681                                 break;
682                         case 'd':
683                                 helpmsg = HelpDiff;
684                                 break;
685                         case 'm':
686                                 helpmsg = HelpMerge;
687                                 break;
688                         case 'B':
689                                 helpmsg = HelpBrowse;
690                                 break;
691                         }
692                         fputs(helpmsg, stderr);
693                         exit(0);
694
695                 case 'V':
696                         fputs(Version, stderr);
697                         exit(0);
698                 case ':':
699                 case '?':
700                 default:
701                         fputs(Usage, stderr);
702                         exit(2);
703
704                 case 'B':
705                 case 'x':
706                 case 'd':
707                 case 'm':
708                         if (mode == 0) {
709                                 mode = opt;
710                                 continue;
711                         }
712                         fprintf(stderr,
713                                 "%s: mode is '%c' - cannot set to '%c'\n",
714                                 Cmd, mode, opt);
715                         exit(2);
716
717                 case 'w':
718                 case 'l':
719                         if (obj == 0 || obj == opt) {
720                                 obj = opt;
721                                 continue;
722                         }
723                         fprintf(stderr,
724                                 "%s: cannot select both words and lines.\n", Cmd);
725                         exit(2);
726
727                 case 'r':
728                         replace = 1;
729                         continue;
730                 case 'R':
731                         reverse = 1;
732                         continue;
733
734                 case 'b':
735                         ignore_blanks = IgnoreBlanks;
736                         continue;
737
738                 case 'i':
739                         ignore = 0;
740                         continue;
741                 case 'W':
742                         show_wiggles = 1;
743                         ignore = 0;
744                         continue;
745
746                 case '1':
747                 case '2':
748                 case '3':
749                         if (which == 0 || which == opt) {
750                                 which = opt;
751                                 continue;
752                         }
753                         fprintf(stderr,
754                                 "%s: can only select one of -1, -2, -3\n", Cmd);
755                         exit(2);
756
757                 case 'p': /* 'patch' or 'strip' */
758                         if (optarg)
759                                 strip = atol(optarg);
760                         ispatch = 1;
761                         continue;
762
763                 case 'v':
764                         verbose++;
765                         continue;
766                 case 'q':
767                         quiet = 1;
768                         continue;
769
770                 case SELF_TEST:
771                         selftest = 1;
772                         continue;
773                 }
774         if (!mode)
775                 mode = 'm';
776
777         if (mode == 'B') {
778                 vpatch(argc-optind, argv+optind, ispatch,
779                        strip, reverse, replace, selftest,
780                        ignore_blanks);
781                 /* should not return */
782                 exit(1);
783         }
784
785         if (obj && mode == 'x') {
786                 fprintf(stderr,
787                         "%s: cannot specify --line or --word with --extract\n",
788                         Cmd);
789                 exit(2);
790         }
791         if (mode != 'm' && !obj)
792                 obj = 'w';
793         if (replace && mode != 'm') {
794                 fprintf(stderr,
795                         "%s: --replace only allowed with --merge\n", Cmd);
796                 exit(2);
797         }
798         if (mode == 'x' && !which) {
799                 fprintf(stderr,
800                         "%s: must specify -1, -2 or -3 with --extract\n", Cmd);
801                 exit(2);
802         }
803         if (mode != 'x' && mode != 'd' && which) {
804                 fprintf(stderr,
805                         "%s: -1, -2 or -3 only allowed with --extract or --diff\n",
806                         Cmd);
807                 exit(2);
808         }
809
810         if (ispatch && which == '3') {
811                 fprintf(stderr,
812                         "%s: cannot extract -3 from a patch.\n", Cmd);
813                 exit(2);
814         }
815
816         switch (mode) {
817         case 'x':
818                 exit_status = extract(argc-optind, argv+optind, ispatch, which);
819                 break;
820         case 'd':
821                 exit_status = do_diff(argc-optind, argv+optind,
822                                       (obj == 'l' ? ByLine : ByWord)
823                                       | ignore_blanks,
824                                       ispatch, which, reverse);
825                 break;
826         case 'm':
827                 if (ispatch)
828                         exit_status = multi_merge(argc-optind,
829                                                   argv+optind, obj,
830                                                   ignore_blanks,
831                                                   reverse, ignore,
832                                                   show_wiggles,
833                                                   replace, strip,
834                                                   quiet);
835                 else
836                         exit_status = do_merge(
837                                 argc-optind, argv+optind,
838                                 obj, ignore_blanks, reverse, replace,
839                                 ignore, show_wiggles, quiet);
840                 break;
841         }
842         exit(exit_status);
843 }