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