main.c revision 1.3 1 1.1 riastrad /* $NetBSD: main.c,v 1.3 2014/01/22 06:17:25 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.1 riastrad __RCSID("$NetBSD: main.c,v 1.3 2014/01/22 06:17:25 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <assert.h>
36 1.1 riastrad #include <err.h>
37 1.1 riastrad #include <getopt.h>
38 1.1 riastrad #include <stdio.h>
39 1.1 riastrad #include <stdlib.h>
40 1.1 riastrad #include <string.h>
41 1.1 riastrad
42 1.1 riastrad #include "common.h"
43 1.1 riastrad
44 1.1 riastrad static int (*operation)(int, char **, const struct options *) = &vndcompress;
45 1.1 riastrad
46 1.1 riastrad int
47 1.1 riastrad main(int argc, char **argv)
48 1.1 riastrad {
49 1.1 riastrad static const struct options zero_options;
50 1.1 riastrad struct options options = zero_options, *O = &options;
51 1.1 riastrad int ch;
52 1.1 riastrad
53 1.1 riastrad setprogname(argv[0]);
54 1.1 riastrad
55 1.1 riastrad if (strcmp(getprogname(), "vndcompress") == 0)
56 1.1 riastrad operation = &vndcompress;
57 1.1 riastrad else if (strcmp(getprogname(), "vnduncompress") == 0)
58 1.1 riastrad operation = &vnduncompress;
59 1.1 riastrad else
60 1.1 riastrad warnx("unknown program name, defaulting to vndcompress: %s",
61 1.1 riastrad getprogname());
62 1.1 riastrad
63 1.3 riastrad while ((ch = getopt(argc, argv, "b:cdk:l:p:rRw:")) != -1) {
64 1.1 riastrad switch (ch) {
65 1.3 riastrad case 'b':
66 1.3 riastrad if (ISSET(O->flags, FLAG_b)) {
67 1.3 riastrad warnx("-b may be supplied only once");
68 1.3 riastrad usage();
69 1.3 riastrad }
70 1.3 riastrad O->flags |= FLAG_b;
71 1.3 riastrad __CTASSERT(MIN_BLOCKSIZE <= MAX_BLOCKSIZE);
72 1.3 riastrad __CTASSERT(MAX_BLOCKSIZE <= LLONG_MAX);
73 1.3 riastrad O->blocksize = strsuftoll("block size", optarg,
74 1.3 riastrad MIN_BLOCKSIZE, MAX_BLOCKSIZE);
75 1.3 riastrad break;
76 1.3 riastrad
77 1.1 riastrad case 'c':
78 1.1 riastrad if (ISSET(O->flags, FLAG_d)) {
79 1.1 riastrad warnx("-c and -d are mutually exclusive");
80 1.1 riastrad usage();
81 1.1 riastrad }
82 1.1 riastrad O->flags |= FLAG_c;
83 1.1 riastrad operation = &vndcompress;
84 1.1 riastrad break;
85 1.1 riastrad
86 1.1 riastrad case 'd':
87 1.1 riastrad if (ISSET(O->flags, FLAG_c)) {
88 1.1 riastrad warnx("-c and -d are mutually exclusive");
89 1.1 riastrad usage();
90 1.1 riastrad }
91 1.1 riastrad O->flags |= FLAG_d;
92 1.1 riastrad operation = &vnduncompress;
93 1.1 riastrad break;
94 1.1 riastrad
95 1.1 riastrad case 'k':
96 1.1 riastrad if (ISSET(O->flags, FLAG_k)) {
97 1.1 riastrad warnx("-k may be supplied only once");
98 1.1 riastrad usage();
99 1.1 riastrad }
100 1.1 riastrad O->flags |= FLAG_k;
101 1.1 riastrad O->checkpoint_blocks = strsuftoll("checkpoint blocks",
102 1.1 riastrad optarg,
103 1.1 riastrad 0, MIN(UINT32_MAX, (OFF_MAX / MIN_BLOCKSIZE)));
104 1.1 riastrad break;
105 1.1 riastrad
106 1.1 riastrad case 'l':
107 1.1 riastrad if (ISSET(O->flags, FLAG_l)) {
108 1.1 riastrad warnx("-l may be supplied only once");
109 1.1 riastrad usage();
110 1.1 riastrad }
111 1.1 riastrad O->flags |= FLAG_l;
112 1.1 riastrad O->length = strsuftoll("length", optarg,
113 1.1 riastrad 0, MIN(OFF_MAX, UINT64_MAX));
114 1.1 riastrad break;
115 1.1 riastrad
116 1.1 riastrad case 'p':
117 1.1 riastrad O->flags |= FLAG_p;
118 1.1 riastrad __CTASSERT(OFF_MAX <= LLONG_MAX);
119 1.1 riastrad O->end_block = strsuftoll("end block", optarg,
120 1.1 riastrad 0, MIN(UINT32_MAX, (OFF_MAX / MIN_BLOCKSIZE)));
121 1.1 riastrad break;
122 1.1 riastrad
123 1.1 riastrad case 'r':
124 1.1 riastrad O->flags |= FLAG_r;
125 1.1 riastrad break;
126 1.1 riastrad
127 1.1 riastrad case 'R':
128 1.1 riastrad O->flags |= FLAG_R;
129 1.1 riastrad break;
130 1.1 riastrad
131 1.2 riastrad case 'w':
132 1.2 riastrad if (ISSET(O->flags, FLAG_w)) {
133 1.2 riastrad warnx("-w may be supplied only once");
134 1.2 riastrad usage();
135 1.2 riastrad }
136 1.2 riastrad O->flags |= FLAG_w;
137 1.2 riastrad O->window_size = strsuftoll("window size", optarg,
138 1.2 riastrad 0, MAX_WINDOW_SIZE);
139 1.2 riastrad break;
140 1.2 riastrad
141 1.1 riastrad case '?':
142 1.1 riastrad default:
143 1.1 riastrad usage();
144 1.1 riastrad }
145 1.1 riastrad }
146 1.1 riastrad
147 1.1 riastrad argc -= optind;
148 1.1 riastrad argv += optind;
149 1.1 riastrad
150 1.1 riastrad if (operation == &vnduncompress) {
151 1.2 riastrad if (ISSET(O->flags, ~(FLAG_d | FLAG_w)))
152 1.1 riastrad usage();
153 1.1 riastrad } else {
154 1.1 riastrad assert(operation == &vndcompress);
155 1.3 riastrad if (ISSET(O->flags, ~(FLAG_b | FLAG_c | FLAG_k | FLAG_l |
156 1.3 riastrad FLAG_p | FLAG_r | FLAG_R | FLAG_w)))
157 1.1 riastrad usage();
158 1.1 riastrad if (ISSET(O->flags, FLAG_R) && !ISSET(O->flags, FLAG_r)) {
159 1.1 riastrad warnx("-R makes no sense without -r");
160 1.1 riastrad usage();
161 1.1 riastrad }
162 1.1 riastrad }
163 1.1 riastrad
164 1.1 riastrad return (*operation)(argc, argv, O);
165 1.1 riastrad }
166 1.1 riastrad
167 1.1 riastrad void __dead
168 1.1 riastrad usage(void)
169 1.1 riastrad {
170 1.1 riastrad
171 1.1 riastrad (void)fprintf(stderr,
172 1.3 riastrad "Usage: %s -c [-rR] [-b <blocksize>] [-k <checkpoint-blocks>]\n"
173 1.3 riastrad " [-l <length>] [-p <partial-offset>] [-w <winsize>]\n"
174 1.1 riastrad " <image> <compressed-image> [<blocksize>]\n"
175 1.1 riastrad " %s -d <compressed-image> <image>\n",
176 1.1 riastrad getprogname(), getprogname());
177 1.1 riastrad exit(1);
178 1.1 riastrad }
179