xref: /dragonfly/sbin/jscan/jscan.c (revision 1bf4b486)
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sbin/jscan/jscan.c,v 1.4 2005/07/06 06:06:44 dillon Exp $
35  */
36 
37 #include "jscan.h"
38 
39 enum jmode { JS_NONE, JS_DEBUG, JS_MIRROR };
40 
41 static void usage(const char *av0);
42 
43 int debug_opt;
44 
45 int
46 main(int ac, char **av)
47 {
48     int ch;
49     int i;
50     int jfflags = 0;
51     enum jdirection direction = JF_FORWARDS;
52     enum jmode jmode = JS_NONE;
53     struct jfile *jf;
54 
55     while ((ch = getopt(ac, av, "2dmrw:W:")) != -1) {
56 	switch(ch) {
57 	case '2':
58 	    jfflags |= JF_FULL_DUPLEX;
59 	    break;
60 	case 'd':
61 	    debug_opt = 1;
62 	    if (jmode == JS_NONE)
63 		jmode = JS_DEBUG;
64 	    break;
65 	case 'm':
66 	    jmode = JS_MIRROR;
67 	    break;
68 	case 'r':
69 	    direction = JF_BACKWARDS;
70 	    break;
71 	case 'W':
72 	    /* fallthrough */
73 	case 'w':
74 	    /* not implemented yet */
75 	default:
76 	    fprintf(stderr, "unknown option: -%c\n", optopt);
77 	    usage(av[0]);
78 	}
79     }
80     if (jmode == JS_NONE)
81 	usage(av[0]);
82     if (jmode == JS_MIRROR && direction == JF_BACKWARDS) {
83 	fprintf(stderr, "Cannot mirror in reverse scan mode\n");
84 	usage(av[0]);
85     }
86 
87     /*
88      * Using specified input streams.  If no files are specified, stdin
89      * is used.
90      */
91     if (ac == optind) {
92 	usage(av[0]);
93     } else {
94 	for (i = optind; i < ac; ++i) {
95 	    if (strcmp(av[i], "stdin") == 0)
96 		jf = jopen_fp(stdin, direction, jfflags);
97 	    else
98 		jf = jopen_stream(av[i], direction, jfflags);
99 	    if (jf != NULL) {
100 		switch(jmode) {
101 		case JS_MIRROR:
102 		    dump_mirror(jf);
103 		    break;
104 		case JS_DEBUG:
105 		    dump_debug(jf);
106 		    break;
107 		case JS_NONE:
108 		    break;
109 		}
110 		jclose_stream(jf);
111 	    } else {
112 		fprintf(stderr, "Unable to open %s: %s\n",
113 				av[i], strerror(errno));
114 	    }
115 	}
116     }
117     return(0);
118 }
119 
120 static void
121 usage(const char *av0)
122 {
123     fprintf(stderr, "%s [-dm] [journal_file/stdin]*\n", av0);
124     exit(1);
125 }
126 
127