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