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