args.c revision 1.29 1 /* $NetBSD: args.c,v 1.29 2010/12/09 10:24:56 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.29 2010/12/09 10:24:56 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 #ifndef SMALL
59 #include <rump/rumpclient.h>
60 #endif
61
62 static int c_arg(const void *, const void *);
63 #ifndef NO_CONV
64 static int c_conv(const void *, const void *);
65 #endif
66 static void f_bs(char *);
67 static void f_cbs(char *);
68 static void f_conv(char *);
69 static void f_count(char *);
70 static void f_files(char *);
71 static void f_ibs(char *);
72 static void f_if(char *);
73 static void f_obs(char *);
74 static void f_of(char *);
75 static void f_seek(char *);
76 static void f_skip(char *);
77 static void f_progress(char *);
78
79 #ifndef SMALL
80 static void f_rif(char *);
81 static void f_rof(char *);
82 #endif
83
84 static const struct arg {
85 const char *name;
86 void (*f)(char *);
87 u_int set, noset;
88 } args[] = {
89 /* the array needs to be sorted by the first column so
90 bsearch() can be used to find commands quickly */
91 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
92 { "cbs", f_cbs, C_CBS, C_CBS },
93 { "conv", f_conv, 0, 0 },
94 { "count", f_count, C_COUNT, C_COUNT },
95 { "files", f_files, C_FILES, C_FILES },
96 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
97 { "if", f_if, C_IF, C_IF|C_RIF },
98 { "obs", f_obs, C_OBS, C_BS|C_OBS },
99 { "of", f_of, C_OF, C_OF|C_ROF },
100 { "progress", f_progress, 0, 0 },
101 #ifndef SMALL
102 { "rif", f_rif, C_RIF|C_RUMP, C_RIF|C_IF },
103 { "rof", f_rof, C_ROF|C_RUMP, C_ROF|C_OF },
104 #endif
105 { "seek", f_seek, C_SEEK, C_SEEK },
106 { "skip", f_skip, C_SKIP, C_SKIP },
107 };
108
109 /*
110 * args -- parse JCL syntax of dd.
111 */
112 void
113 jcl(char **argv)
114 {
115 struct arg *ap, tmp;
116 char *oper, *arg;
117
118 in.dbsz = out.dbsz = 512;
119
120 while ((oper = *++argv) != NULL) {
121 if ((arg = strchr(oper, '=')) == NULL) {
122 errx(EXIT_FAILURE, "unknown operand %s", oper);
123 /* NOTREACHED */
124 }
125 *arg++ = '\0';
126 if (!*arg) {
127 errx(EXIT_FAILURE, "no value specified for %s", oper);
128 /* NOTREACHED */
129 }
130 tmp.name = oper;
131 if (!(ap = (struct arg *)bsearch(&tmp, args,
132 sizeof(args)/sizeof(struct arg), sizeof(struct arg),
133 c_arg))) {
134 errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
135 /* NOTREACHED */
136 }
137 if (ddflags & ap->noset) {
138 errx(EXIT_FAILURE,
139 "%s: illegal argument combination or already set",
140 tmp.name);
141 /* NOTREACHED */
142 }
143 ddflags |= ap->set;
144 ap->f(arg);
145 }
146
147 /* Final sanity checks. */
148
149 if (ddflags & C_BS) {
150 /*
151 * Bs is turned off by any conversion -- we assume the user
152 * just wanted to set both the input and output block sizes
153 * and didn't want the bs semantics, so we don't warn.
154 */
155 if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
156 C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
157 ddflags &= ~C_BS;
158 ddflags |= C_IBS|C_OBS;
159 }
160
161 /* Bs supersedes ibs and obs. */
162 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
163 warnx("bs supersedes ibs and obs");
164 }
165
166 /*
167 * Ascii/ebcdic and cbs implies block/unblock.
168 * Block/unblock requires cbs and vice-versa.
169 */
170 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
171 if (!(ddflags & C_CBS)) {
172 errx(EXIT_FAILURE, "record operations require cbs");
173 /* NOTREACHED */
174 }
175 cfunc = ddflags & C_BLOCK ? block : unblock;
176 } else if (ddflags & C_CBS) {
177 if (ddflags & (C_ASCII|C_EBCDIC)) {
178 if (ddflags & C_ASCII) {
179 ddflags |= C_UNBLOCK;
180 cfunc = unblock;
181 } else {
182 ddflags |= C_BLOCK;
183 cfunc = block;
184 }
185 } else {
186 errx(EXIT_FAILURE,
187 "cbs meaningless if not doing record operations");
188 /* NOTREACHED */
189 }
190 } else
191 cfunc = def;
192
193 /* Read, write and seek calls take off_t as arguments.
194 *
195 * The following check is not done because an off_t is a quad
196 * for current NetBSD implementations.
197 *
198 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
199 * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
200 */
201
202 #ifndef SMALL
203 if (ddflags & C_RUMP)
204 if (rumpclient_init() == -1)
205 err(1, "rumpclient init failed");
206 #endif
207 }
208
209 static int
210 c_arg(const void *a, const void *b)
211 {
212
213 return (strcmp(((const struct arg *)a)->name,
214 ((const struct arg *)b)->name));
215 }
216
217 static void
218 f_bs(char *arg)
219 {
220
221 in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
222 }
223
224 static void
225 f_cbs(char *arg)
226 {
227
228 cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
229 }
230
231 static void
232 f_count(char *arg)
233 {
234
235 cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
236 if (!cpy_cnt)
237 terminate(0);
238 }
239
240 static void
241 f_files(char *arg)
242 {
243
244 files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
245 if (!files_cnt)
246 terminate(0);
247 }
248
249 static void
250 f_ibs(char *arg)
251 {
252
253 if (!(ddflags & C_BS))
254 in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
255 }
256
257 static void
258 f_if(char *arg)
259 {
260
261 in.name = arg;
262 }
263
264 static void
265 f_obs(char *arg)
266 {
267
268 if (!(ddflags & C_BS))
269 out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
270 }
271
272 static void
273 f_of(char *arg)
274 {
275
276 out.name = arg;
277 }
278
279 #ifndef SMALL
280 #include <rump/rump.h>
281 #include <rump/rump_syscalls.h>
282
283 static const struct ddfops ddfops_rump = {
284 .op_open = rump_sys_open,
285 .op_close = rump_sys_close,
286 .op_fcntl = rump_sys_fcntl,
287 .op_ioctl = rump_sys_ioctl,
288 .op_fstat = rump_sys_fstat,
289 .op_fsync = rump_sys_fsync,
290 .op_ftruncate = rump_sys_ftruncate,
291 .op_lseek = rump_sys_lseek,
292 .op_read = rump_sys_read,
293 .op_write = rump_sys_write,
294 };
295
296 static void
297 f_rif(char *arg)
298 {
299
300 in.name = arg;
301 in.ops = &ddfops_rump;
302 }
303
304 static void
305 f_rof(char *arg)
306 {
307
308 out.name = arg;
309 out.ops = &ddfops_rump;
310 }
311 #endif
312
313 static void
314 f_seek(char *arg)
315 {
316
317 out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
318 }
319
320 static void
321 f_skip(char *arg)
322 {
323
324 in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
325 }
326
327 static void
328 f_progress(char *arg)
329 {
330
331 progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
332 }
333
334 #ifdef NO_CONV
335 /* Build a small version (i.e. for a ramdisk root) */
336 static void
337 f_conv(char *arg)
338 {
339
340 errx(EXIT_FAILURE, "conv option disabled");
341 /* NOTREACHED */
342 }
343 #else /* NO_CONV */
344
345 static const struct conv {
346 const char *name;
347 u_int set, noset;
348 const u_char *ctab;
349 } clist[] = {
350 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
351 { "block", C_BLOCK, C_UNBLOCK, NULL },
352 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
353 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
354 { "lcase", C_LCASE, C_UCASE, NULL },
355 { "noerror", C_NOERROR, 0, NULL },
356 { "notrunc", C_NOTRUNC, 0, NULL },
357 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
358 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
359 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
360 { "osync", C_OSYNC, C_BS, NULL },
361 { "sparse", C_SPARSE, 0, NULL },
362 { "swab", C_SWAB, 0, NULL },
363 { "sync", C_SYNC, 0, NULL },
364 { "ucase", C_UCASE, C_LCASE, NULL },
365 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
366 /* If you add items to this table, be sure to add the
367 * conversions to the C_BS check in the jcl routine above.
368 */
369 };
370
371 static void
372 f_conv(char *arg)
373 {
374 struct conv *cp, tmp;
375
376 while (arg != NULL) {
377 tmp.name = strsep(&arg, ",");
378 if (!(cp = (struct conv *)bsearch(&tmp, clist,
379 sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
380 c_conv))) {
381 errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
382 /* NOTREACHED */
383 }
384 if (ddflags & cp->noset) {
385 errx(EXIT_FAILURE,
386 "%s: illegal conversion combination", tmp.name);
387 /* NOTREACHED */
388 }
389 ddflags |= cp->set;
390 if (cp->ctab)
391 ctab = cp->ctab;
392 }
393 }
394
395 static int
396 c_conv(const void *a, const void *b)
397 {
398
399 return (strcmp(((const struct conv *)a)->name,
400 ((const struct conv *)b)->name));
401 }
402
403 #endif /* NO_CONV */
404