args.c revision 1.33 1 1.33 jym /* $NetBSD: args.c,v 1.33 2011/01/13 23:45:13 jym Exp $ */
2 1.4 cgd
3 1.1 glass /*-
4 1.3 mycroft * Copyright (c) 1991, 1993, 1994
5 1.3 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 glass *
7 1.1 glass * This code is derived from software contributed to Berkeley by
8 1.1 glass * Keith Muller of the University of California, San Diego and Lance
9 1.1 glass * Visser of Convex Computer Corporation.
10 1.1 glass *
11 1.1 glass * Redistribution and use in source and binary forms, with or without
12 1.1 glass * modification, are permitted provided that the following conditions
13 1.1 glass * are met:
14 1.1 glass * 1. Redistributions of source code must retain the above copyright
15 1.1 glass * notice, this list of conditions and the following disclaimer.
16 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 glass * notice, this list of conditions and the following disclaimer in the
18 1.1 glass * documentation and/or other materials provided with the distribution.
19 1.23 agc * 3. Neither the name of the University nor the names of its contributors
20 1.1 glass * may be used to endorse or promote products derived from this software
21 1.1 glass * without specific prior written permission.
22 1.1 glass *
23 1.1 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 glass * SUCH DAMAGE.
34 1.1 glass */
35 1.1 glass
36 1.8 christos #include <sys/cdefs.h>
37 1.1 glass #ifndef lint
38 1.4 cgd #if 0
39 1.4 cgd static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
40 1.4 cgd #else
41 1.33 jym __RCSID("$NetBSD: args.c,v 1.33 2011/01/13 23:45:13 jym Exp $");
42 1.4 cgd #endif
43 1.1 glass #endif /* not lint */
44 1.1 glass
45 1.1 glass #include <sys/types.h>
46 1.15 ross #include <sys/time.h>
47 1.3 mycroft
48 1.3 mycroft #include <err.h>
49 1.3 mycroft #include <errno.h>
50 1.1 glass #include <limits.h>
51 1.1 glass #include <stdio.h>
52 1.1 glass #include <stdlib.h>
53 1.1 glass #include <string.h>
54 1.3 mycroft
55 1.1 glass #include "dd.h"
56 1.1 glass #include "extern.h"
57 1.1 glass
58 1.30 pooka #if !defined(SMALL) && defined(__NetBSD__)
59 1.30 pooka #define _HAVE_RUMPOPS
60 1.30 pooka
61 1.27 pooka #include <rump/rumpclient.h>
62 1.27 pooka #endif
63 1.27 pooka
64 1.17 lukem static int c_arg(const void *, const void *);
65 1.18 lukem #ifndef NO_CONV
66 1.17 lukem static int c_conv(const void *, const void *);
67 1.18 lukem #endif
68 1.17 lukem static void f_bs(char *);
69 1.17 lukem static void f_cbs(char *);
70 1.17 lukem static void f_conv(char *);
71 1.17 lukem static void f_count(char *);
72 1.17 lukem static void f_files(char *);
73 1.17 lukem static void f_ibs(char *);
74 1.17 lukem static void f_if(char *);
75 1.17 lukem static void f_obs(char *);
76 1.17 lukem static void f_of(char *);
77 1.17 lukem static void f_seek(char *);
78 1.17 lukem static void f_skip(char *);
79 1.17 lukem static void f_progress(char *);
80 1.1 glass
81 1.30 pooka #ifdef _HAVE_RUMPOPS
82 1.27 pooka static void f_rif(char *);
83 1.27 pooka static void f_rof(char *);
84 1.27 pooka #endif
85 1.27 pooka
86 1.12 msaitoh static const struct arg {
87 1.17 lukem const char *name;
88 1.17 lukem void (*f)(char *);
89 1.1 glass u_int set, noset;
90 1.1 glass } args[] = {
91 1.14 hubertf /* the array needs to be sorted by the first column so
92 1.20 enami bsearch() can be used to find commands quickly */
93 1.3 mycroft { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
94 1.3 mycroft { "cbs", f_cbs, C_CBS, C_CBS },
95 1.3 mycroft { "conv", f_conv, 0, 0 },
96 1.3 mycroft { "count", f_count, C_COUNT, C_COUNT },
97 1.3 mycroft { "files", f_files, C_FILES, C_FILES },
98 1.3 mycroft { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
99 1.27 pooka { "if", f_if, C_IF, C_IF|C_RIF },
100 1.32 enami { "iseek", f_skip, C_SKIP, C_SKIP },
101 1.3 mycroft { "obs", f_obs, C_OBS, C_BS|C_OBS },
102 1.27 pooka { "of", f_of, C_OF, C_OF|C_ROF },
103 1.32 enami { "oseek", f_seek, C_SEEK, C_SEEK },
104 1.14 hubertf { "progress", f_progress, 0, 0 },
105 1.30 pooka #ifdef _HAVE_RUMPOPS
106 1.27 pooka { "rif", f_rif, C_RIF|C_RUMP, C_RIF|C_IF },
107 1.28 jym { "rof", f_rof, C_ROF|C_RUMP, C_ROF|C_OF },
108 1.27 pooka #endif
109 1.3 mycroft { "seek", f_seek, C_SEEK, C_SEEK },
110 1.3 mycroft { "skip", f_skip, C_SKIP, C_SKIP },
111 1.1 glass };
112 1.1 glass
113 1.1 glass /*
114 1.1 glass * args -- parse JCL syntax of dd.
115 1.1 glass */
116 1.1 glass void
117 1.17 lukem jcl(char **argv)
118 1.1 glass {
119 1.3 mycroft struct arg *ap, tmp;
120 1.19 lukem char *oper, *arg;
121 1.1 glass
122 1.1 glass in.dbsz = out.dbsz = 512;
123 1.1 glass
124 1.8 christos while ((oper = *++argv) != NULL) {
125 1.31 enami if ((oper = strdup(oper)) == NULL) {
126 1.31 enami errx(EXIT_FAILURE,
127 1.31 enami "unable to allocate space for the argument %s",
128 1.31 enami *argv);
129 1.31 enami /* NOTREACHED */
130 1.31 enami }
131 1.22 jschauma if ((arg = strchr(oper, '=')) == NULL) {
132 1.22 jschauma errx(EXIT_FAILURE, "unknown operand %s", oper);
133 1.22 jschauma /* NOTREACHED */
134 1.22 jschauma }
135 1.1 glass *arg++ = '\0';
136 1.22 jschauma if (!*arg) {
137 1.22 jschauma errx(EXIT_FAILURE, "no value specified for %s", oper);
138 1.22 jschauma /* NOTREACHED */
139 1.22 jschauma }
140 1.1 glass tmp.name = oper;
141 1.33 jym if (!(ap = bsearch(&tmp, args,
142 1.33 jym __arraycount(args), sizeof(*args), c_arg))) {
143 1.22 jschauma errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
144 1.22 jschauma /* NOTREACHED */
145 1.22 jschauma }
146 1.22 jschauma if (ddflags & ap->noset) {
147 1.22 jschauma errx(EXIT_FAILURE,
148 1.17 lukem "%s: illegal argument combination or already set",
149 1.1 glass tmp.name);
150 1.22 jschauma /* NOTREACHED */
151 1.22 jschauma }
152 1.1 glass ddflags |= ap->set;
153 1.1 glass ap->f(arg);
154 1.1 glass }
155 1.1 glass
156 1.1 glass /* Final sanity checks. */
157 1.1 glass
158 1.1 glass if (ddflags & C_BS) {
159 1.1 glass /*
160 1.1 glass * Bs is turned off by any conversion -- we assume the user
161 1.1 glass * just wanted to set both the input and output block sizes
162 1.1 glass * and didn't want the bs semantics, so we don't warn.
163 1.1 glass */
164 1.24 dbj if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
165 1.25 dbj C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
166 1.1 glass ddflags &= ~C_BS;
167 1.24 dbj ddflags |= C_IBS|C_OBS;
168 1.24 dbj }
169 1.1 glass
170 1.1 glass /* Bs supersedes ibs and obs. */
171 1.1 glass if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
172 1.3 mycroft warnx("bs supersedes ibs and obs");
173 1.1 glass }
174 1.1 glass
175 1.1 glass /*
176 1.1 glass * Ascii/ebcdic and cbs implies block/unblock.
177 1.1 glass * Block/unblock requires cbs and vice-versa.
178 1.1 glass */
179 1.1 glass if (ddflags & (C_BLOCK|C_UNBLOCK)) {
180 1.22 jschauma if (!(ddflags & C_CBS)) {
181 1.22 jschauma errx(EXIT_FAILURE, "record operations require cbs");
182 1.22 jschauma /* NOTREACHED */
183 1.22 jschauma }
184 1.1 glass cfunc = ddflags & C_BLOCK ? block : unblock;
185 1.1 glass } else if (ddflags & C_CBS) {
186 1.1 glass if (ddflags & (C_ASCII|C_EBCDIC)) {
187 1.1 glass if (ddflags & C_ASCII) {
188 1.1 glass ddflags |= C_UNBLOCK;
189 1.1 glass cfunc = unblock;
190 1.1 glass } else {
191 1.1 glass ddflags |= C_BLOCK;
192 1.1 glass cfunc = block;
193 1.1 glass }
194 1.22 jschauma } else {
195 1.22 jschauma errx(EXIT_FAILURE,
196 1.17 lukem "cbs meaningless if not doing record operations");
197 1.22 jschauma /* NOTREACHED */
198 1.22 jschauma }
199 1.1 glass } else
200 1.1 glass cfunc = def;
201 1.1 glass
202 1.20 enami /* Read, write and seek calls take off_t as arguments.
203 1.9 phil *
204 1.9 phil * The following check is not done because an off_t is a quad
205 1.20 enami * for current NetBSD implementations.
206 1.9 phil *
207 1.9 phil * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
208 1.9 phil * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
209 1.9 phil */
210 1.27 pooka
211 1.30 pooka #ifdef _HAVE_RUMPOPS
212 1.27 pooka if (ddflags & C_RUMP)
213 1.27 pooka if (rumpclient_init() == -1)
214 1.27 pooka err(1, "rumpclient init failed");
215 1.27 pooka #endif
216 1.1 glass }
217 1.1 glass
218 1.1 glass static int
219 1.17 lukem c_arg(const void *a, const void *b)
220 1.1 glass {
221 1.17 lukem
222 1.13 mycroft return (strcmp(((const struct arg *)a)->name,
223 1.13 mycroft ((const struct arg *)b)->name));
224 1.1 glass }
225 1.1 glass
226 1.1 glass static void
227 1.17 lukem f_bs(char *arg)
228 1.1 glass {
229 1.17 lukem
230 1.21 lukem in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
231 1.1 glass }
232 1.1 glass
233 1.1 glass static void
234 1.17 lukem f_cbs(char *arg)
235 1.1 glass {
236 1.17 lukem
237 1.21 lukem cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
238 1.1 glass }
239 1.1 glass
240 1.1 glass static void
241 1.17 lukem f_count(char *arg)
242 1.1 glass {
243 1.17 lukem
244 1.21 lukem cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
245 1.1 glass if (!cpy_cnt)
246 1.1 glass terminate(0);
247 1.1 glass }
248 1.1 glass
249 1.1 glass static void
250 1.17 lukem f_files(char *arg)
251 1.1 glass {
252 1.17 lukem
253 1.21 lukem files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
254 1.19 lukem if (!files_cnt)
255 1.19 lukem terminate(0);
256 1.1 glass }
257 1.1 glass
258 1.1 glass static void
259 1.17 lukem f_ibs(char *arg)
260 1.1 glass {
261 1.17 lukem
262 1.1 glass if (!(ddflags & C_BS))
263 1.21 lukem in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
264 1.1 glass }
265 1.1 glass
266 1.1 glass static void
267 1.17 lukem f_if(char *arg)
268 1.1 glass {
269 1.17 lukem
270 1.1 glass in.name = arg;
271 1.1 glass }
272 1.1 glass
273 1.1 glass static void
274 1.17 lukem f_obs(char *arg)
275 1.1 glass {
276 1.17 lukem
277 1.1 glass if (!(ddflags & C_BS))
278 1.21 lukem out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
279 1.1 glass }
280 1.1 glass
281 1.1 glass static void
282 1.17 lukem f_of(char *arg)
283 1.1 glass {
284 1.17 lukem
285 1.1 glass out.name = arg;
286 1.1 glass }
287 1.1 glass
288 1.30 pooka #ifdef _HAVE_RUMPOPS
289 1.27 pooka #include <rump/rump.h>
290 1.27 pooka #include <rump/rump_syscalls.h>
291 1.27 pooka
292 1.27 pooka static const struct ddfops ddfops_rump = {
293 1.27 pooka .op_open = rump_sys_open,
294 1.27 pooka .op_close = rump_sys_close,
295 1.27 pooka .op_fcntl = rump_sys_fcntl,
296 1.27 pooka .op_ioctl = rump_sys_ioctl,
297 1.27 pooka .op_fstat = rump_sys_fstat,
298 1.27 pooka .op_fsync = rump_sys_fsync,
299 1.27 pooka .op_ftruncate = rump_sys_ftruncate,
300 1.27 pooka .op_lseek = rump_sys_lseek,
301 1.27 pooka .op_read = rump_sys_read,
302 1.27 pooka .op_write = rump_sys_write,
303 1.27 pooka };
304 1.27 pooka
305 1.27 pooka static void
306 1.27 pooka f_rif(char *arg)
307 1.27 pooka {
308 1.27 pooka
309 1.27 pooka in.name = arg;
310 1.27 pooka in.ops = &ddfops_rump;
311 1.27 pooka }
312 1.27 pooka
313 1.27 pooka static void
314 1.27 pooka f_rof(char *arg)
315 1.27 pooka {
316 1.27 pooka
317 1.27 pooka out.name = arg;
318 1.27 pooka out.ops = &ddfops_rump;
319 1.27 pooka }
320 1.27 pooka #endif
321 1.27 pooka
322 1.1 glass static void
323 1.17 lukem f_seek(char *arg)
324 1.1 glass {
325 1.17 lukem
326 1.21 lukem out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
327 1.1 glass }
328 1.1 glass
329 1.1 glass static void
330 1.17 lukem f_skip(char *arg)
331 1.1 glass {
332 1.17 lukem
333 1.21 lukem in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
334 1.14 hubertf }
335 1.3 mycroft
336 1.14 hubertf static void
337 1.17 lukem f_progress(char *arg)
338 1.14 hubertf {
339 1.17 lukem
340 1.26 apb progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
341 1.1 glass }
342 1.1 glass
343 1.5 gwr #ifdef NO_CONV
344 1.5 gwr /* Build a small version (i.e. for a ramdisk root) */
345 1.5 gwr static void
346 1.17 lukem f_conv(char *arg)
347 1.5 gwr {
348 1.17 lukem
349 1.22 jschauma errx(EXIT_FAILURE, "conv option disabled");
350 1.22 jschauma /* NOTREACHED */
351 1.5 gwr }
352 1.5 gwr #else /* NO_CONV */
353 1.5 gwr
354 1.12 msaitoh static const struct conv {
355 1.17 lukem const char *name;
356 1.1 glass u_int set, noset;
357 1.6 jtc const u_char *ctab;
358 1.1 glass } clist[] = {
359 1.3 mycroft { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
360 1.3 mycroft { "block", C_BLOCK, C_UNBLOCK, NULL },
361 1.3 mycroft { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
362 1.3 mycroft { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
363 1.3 mycroft { "lcase", C_LCASE, C_UCASE, NULL },
364 1.3 mycroft { "noerror", C_NOERROR, 0, NULL },
365 1.3 mycroft { "notrunc", C_NOTRUNC, 0, NULL },
366 1.3 mycroft { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
367 1.3 mycroft { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
368 1.3 mycroft { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
369 1.3 mycroft { "osync", C_OSYNC, C_BS, NULL },
370 1.25 dbj { "sparse", C_SPARSE, 0, NULL },
371 1.3 mycroft { "swab", C_SWAB, 0, NULL },
372 1.3 mycroft { "sync", C_SYNC, 0, NULL },
373 1.3 mycroft { "ucase", C_UCASE, C_LCASE, NULL },
374 1.3 mycroft { "unblock", C_UNBLOCK, C_BLOCK, NULL },
375 1.24 dbj /* If you add items to this table, be sure to add the
376 1.24 dbj * conversions to the C_BS check in the jcl routine above.
377 1.24 dbj */
378 1.1 glass };
379 1.1 glass
380 1.1 glass static void
381 1.17 lukem f_conv(char *arg)
382 1.1 glass {
383 1.3 mycroft struct conv *cp, tmp;
384 1.1 glass
385 1.1 glass while (arg != NULL) {
386 1.1 glass tmp.name = strsep(&arg, ",");
387 1.33 jym if (!(cp = bsearch(&tmp, clist,
388 1.33 jym __arraycount(clist), sizeof(*clist), c_conv))) {
389 1.22 jschauma errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
390 1.22 jschauma /* NOTREACHED */
391 1.22 jschauma }
392 1.22 jschauma if (ddflags & cp->noset) {
393 1.29 enami errx(EXIT_FAILURE,
394 1.29 enami "%s: illegal conversion combination", tmp.name);
395 1.22 jschauma /* NOTREACHED */
396 1.22 jschauma }
397 1.1 glass ddflags |= cp->set;
398 1.1 glass if (cp->ctab)
399 1.1 glass ctab = cp->ctab;
400 1.1 glass }
401 1.1 glass }
402 1.1 glass
403 1.1 glass static int
404 1.17 lukem c_conv(const void *a, const void *b)
405 1.1 glass {
406 1.3 mycroft
407 1.13 mycroft return (strcmp(((const struct conv *)a)->name,
408 1.13 mycroft ((const struct conv *)b)->name));
409 1.1 glass }
410 1.5 gwr
411 1.5 gwr #endif /* NO_CONV */
412