main.c revision 1.2 1 1.1 riastrad /* $NetBSD: main.c,v 1.2 2014/01/22 06:15:57 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.2 2014/01/22 06:15:57 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.2 riastrad while ((ch = getopt(argc, argv, "cdk:l:p:rRs:w:")) != -1) {
64 1.1 riastrad switch (ch) {
65 1.1 riastrad case 'c':
66 1.1 riastrad if (ISSET(O->flags, FLAG_d)) {
67 1.1 riastrad warnx("-c and -d are mutually exclusive");
68 1.1 riastrad usage();
69 1.1 riastrad }
70 1.1 riastrad O->flags |= FLAG_c;
71 1.1 riastrad operation = &vndcompress;
72 1.1 riastrad break;
73 1.1 riastrad
74 1.1 riastrad case 'd':
75 1.1 riastrad if (ISSET(O->flags, FLAG_c)) {
76 1.1 riastrad warnx("-c and -d are mutually exclusive");
77 1.1 riastrad usage();
78 1.1 riastrad }
79 1.1 riastrad O->flags |= FLAG_d;
80 1.1 riastrad operation = &vnduncompress;
81 1.1 riastrad break;
82 1.1 riastrad
83 1.1 riastrad case 'k':
84 1.1 riastrad if (ISSET(O->flags, FLAG_k)) {
85 1.1 riastrad warnx("-k may be supplied only once");
86 1.1 riastrad usage();
87 1.1 riastrad }
88 1.1 riastrad O->flags |= FLAG_k;
89 1.1 riastrad O->checkpoint_blocks = strsuftoll("checkpoint blocks",
90 1.1 riastrad optarg,
91 1.1 riastrad 0, MIN(UINT32_MAX, (OFF_MAX / MIN_BLOCKSIZE)));
92 1.1 riastrad break;
93 1.1 riastrad
94 1.1 riastrad case 'l':
95 1.1 riastrad if (ISSET(O->flags, FLAG_l)) {
96 1.1 riastrad warnx("-l may be supplied only once");
97 1.1 riastrad usage();
98 1.1 riastrad }
99 1.1 riastrad O->flags |= FLAG_l;
100 1.1 riastrad O->length = strsuftoll("length", optarg,
101 1.1 riastrad 0, MIN(OFF_MAX, UINT64_MAX));
102 1.1 riastrad break;
103 1.1 riastrad
104 1.1 riastrad case 'p':
105 1.1 riastrad O->flags |= FLAG_p;
106 1.1 riastrad __CTASSERT(OFF_MAX <= LLONG_MAX);
107 1.1 riastrad O->end_block = strsuftoll("end block", optarg,
108 1.1 riastrad 0, MIN(UINT32_MAX, (OFF_MAX / MIN_BLOCKSIZE)));
109 1.1 riastrad break;
110 1.1 riastrad
111 1.1 riastrad case 'r':
112 1.1 riastrad O->flags |= FLAG_r;
113 1.1 riastrad break;
114 1.1 riastrad
115 1.1 riastrad case 'R':
116 1.1 riastrad O->flags |= FLAG_R;
117 1.1 riastrad break;
118 1.1 riastrad
119 1.1 riastrad case 's':
120 1.1 riastrad if (ISSET(O->flags, FLAG_s)) {
121 1.1 riastrad warnx("-s may be supplied only once");
122 1.1 riastrad usage();
123 1.1 riastrad }
124 1.1 riastrad O->flags |= FLAG_s;
125 1.1 riastrad __CTASSERT(MIN_BLOCKSIZE <= MAX_BLOCKSIZE);
126 1.1 riastrad __CTASSERT(MAX_BLOCKSIZE <= LLONG_MAX);
127 1.1 riastrad O->blocksize = strsuftoll("block size", optarg,
128 1.1 riastrad MIN_BLOCKSIZE, MAX_BLOCKSIZE);
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.1 riastrad if (ISSET(O->flags, ~(FLAG_c | FLAG_k | FLAG_l | FLAG_p |
156 1.2 riastrad FLAG_r | FLAG_s | 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.1 riastrad "Usage: %s -c [-rR] [-k <checkpoint-blocks>] [-l <length>]\n"
173 1.2 riastrad " [-p <partial-offset>] [-s <blocksize>] [-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