args.c revision 1.8 1 /* $NetBSD: args.c,v 1.8 1997/07/20 21:58:36 christos 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.8 1997/07/20 21:58:36 christos Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/types.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57
58 #include "dd.h"
59 #include "extern.h"
60
61 static int c_arg __P((const void *, const void *));
62 static int c_conv __P((const void *, const void *));
63 static void f_bs __P((char *));
64 static void f_cbs __P((char *));
65 static void f_conv __P((char *));
66 static void f_count __P((char *));
67 static void f_files __P((char *));
68 static void f_ibs __P((char *));
69 static void f_if __P((char *));
70 static void f_obs __P((char *));
71 static void f_of __P((char *));
72 static void f_seek __P((char *));
73 static void f_skip __P((char *));
74 static u_long get_bsz __P((char *));
75
76 const static struct arg {
77 char *name;
78 void (*f) __P((char *));
79 u_int set, noset;
80 } args[] = {
81 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
82 { "cbs", f_cbs, C_CBS, C_CBS },
83 { "conv", f_conv, 0, 0 },
84 { "count", f_count, C_COUNT, C_COUNT },
85 { "files", f_files, C_FILES, C_FILES },
86 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
87 { "if", f_if, C_IF, C_IF },
88 { "obs", f_obs, C_OBS, C_BS|C_OBS },
89 { "of", f_of, C_OF, C_OF },
90 { "seek", f_seek, C_SEEK, C_SEEK },
91 { "skip", f_skip, C_SKIP, C_SKIP },
92 };
93
94 static char *oper;
95
96 /*
97 * args -- parse JCL syntax of dd.
98 */
99 void
100 jcl(argv)
101 char **argv;
102 {
103 struct arg *ap, tmp;
104 char *arg;
105
106 in.dbsz = out.dbsz = 512;
107
108 while ((oper = *++argv) != NULL) {
109 if ((arg = strchr(oper, '=')) == NULL)
110 errx(1, "unknown operand %s", oper);
111 *arg++ = '\0';
112 if (!*arg)
113 errx(1, "no value specified for %s", oper);
114 tmp.name = oper;
115 if (!(ap = (struct arg *)bsearch(&tmp, args,
116 sizeof(args)/sizeof(struct arg), sizeof(struct arg),
117 c_arg)))
118 errx(1, "unknown operand %s", tmp.name);
119 if (ddflags & ap->noset)
120 errx(1, "%s: illegal argument combination or already set",
121 tmp.name);
122 ddflags |= ap->set;
123 ap->f(arg);
124 }
125
126 /* Final sanity checks. */
127
128 if (ddflags & C_BS) {
129 /*
130 * Bs is turned off by any conversion -- we assume the user
131 * just wanted to set both the input and output block sizes
132 * and didn't want the bs semantics, so we don't warn.
133 */
134 if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
135 ddflags &= ~C_BS;
136
137 /* Bs supersedes ibs and obs. */
138 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
139 warnx("bs supersedes ibs and obs");
140 }
141
142 /*
143 * Ascii/ebcdic and cbs implies block/unblock.
144 * Block/unblock requires cbs and vice-versa.
145 */
146 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
147 if (!(ddflags & C_CBS))
148 errx(1, "record operations require cbs");
149 if (cbsz == 0)
150 errx(1, "cbs cannot be zero");
151 cfunc = ddflags & C_BLOCK ? block : unblock;
152 } else if (ddflags & C_CBS) {
153 if (ddflags & (C_ASCII|C_EBCDIC)) {
154 if (ddflags & C_ASCII) {
155 ddflags |= C_UNBLOCK;
156 cfunc = unblock;
157 } else {
158 ddflags |= C_BLOCK;
159 cfunc = block;
160 }
161 } else
162 errx(1, "cbs meaningless if not doing record operations");
163 if (cbsz == 0)
164 errx(1, "cbs cannot be zero");
165 } else
166 cfunc = def;
167
168 if (in.dbsz == 0 || out.dbsz == 0)
169 errx(1, "buffer sizes cannot be zero");
170
171 /*
172 * Read, write and seek calls take ints as arguments. Seek sizes
173 * could be larger if we wanted to do it in stages or check only
174 * regular files, but it's probably not worth it.
175 */
176 if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
177 errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
178 if (in.offset > INT_MAX / in.dbsz || out.offset > INT_MAX / out.dbsz)
179 errx(1, "seek offsets cannot be larger than %d", INT_MAX);
180 }
181
182 static int
183 c_arg(a, b)
184 const void *a, *b;
185 {
186
187 return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
188 }
189
190 static void
191 f_bs(arg)
192 char *arg;
193 {
194
195 in.dbsz = out.dbsz = (int)get_bsz(arg);
196 }
197
198 static void
199 f_cbs(arg)
200 char *arg;
201 {
202
203 cbsz = (int)get_bsz(arg);
204 }
205
206 static void
207 f_count(arg)
208 char *arg;
209 {
210
211 cpy_cnt = (u_int)get_bsz(arg);
212 if (!cpy_cnt)
213 terminate(0);
214 }
215
216 static void
217 f_files(arg)
218 char *arg;
219 {
220
221 files_cnt = (int)get_bsz(arg);
222 }
223
224 static void
225 f_ibs(arg)
226 char *arg;
227 {
228
229 if (!(ddflags & C_BS))
230 in.dbsz = (int)get_bsz(arg);
231 }
232
233 static void
234 f_if(arg)
235 char *arg;
236 {
237
238 in.name = arg;
239 }
240
241 static void
242 f_obs(arg)
243 char *arg;
244 {
245
246 if (!(ddflags & C_BS))
247 out.dbsz = (int)get_bsz(arg);
248 }
249
250 static void
251 f_of(arg)
252 char *arg;
253 {
254
255 out.name = arg;
256 }
257
258 static void
259 f_seek(arg)
260 char *arg;
261 {
262
263 out.offset = (u_int)get_bsz(arg);
264 }
265
266 static void
267 f_skip(arg)
268 char *arg;
269 {
270
271 in.offset = (u_int)get_bsz(arg);
272 }
273
274 #ifdef NO_CONV
275 /* Build a small version (i.e. for a ramdisk root) */
276 static void
277 f_conv(arg)
278 char *arg;
279 {
280 errx(1, "conv option disabled");
281 }
282 #else /* NO_CONV */
283
284 const static struct conv {
285 char *name;
286 u_int set, noset;
287 const u_char *ctab;
288 } clist[] = {
289 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
290 { "block", C_BLOCK, C_UNBLOCK, NULL },
291 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
292 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
293 { "lcase", C_LCASE, C_UCASE, NULL },
294 { "noerror", C_NOERROR, 0, NULL },
295 { "notrunc", C_NOTRUNC, 0, NULL },
296 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
297 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
298 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
299 { "osync", C_OSYNC, C_BS, NULL },
300 { "swab", C_SWAB, 0, NULL },
301 { "sync", C_SYNC, 0, NULL },
302 { "ucase", C_UCASE, C_LCASE, NULL },
303 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
304 };
305
306 static void
307 f_conv(arg)
308 char *arg;
309 {
310 struct conv *cp, tmp;
311
312 while (arg != NULL) {
313 tmp.name = strsep(&arg, ",");
314 if (!(cp = (struct conv *)bsearch(&tmp, clist,
315 sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
316 c_conv)))
317 errx(1, "unknown conversion %s", tmp.name);
318 if (ddflags & cp->noset)
319 errx(1, "%s: illegal conversion combination", tmp.name);
320 ddflags |= cp->set;
321 if (cp->ctab)
322 ctab = cp->ctab;
323 }
324 }
325
326 static int
327 c_conv(a, b)
328 const void *a, *b;
329 {
330
331 return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
332 }
333
334 #endif /* NO_CONV */
335
336 /*
337 * Convert an expression of the following forms to an unsigned long.
338 * 1) A positive decimal number.
339 * 2) A positive decimal number followed by a b (mult by 512).
340 * 3) A positive decimal number followed by a k (mult by 1024).
341 * 4) A positive decimal number followed by a m (mult by 512).
342 * 5) A positive decimal number followed by a w (mult by sizeof int)
343 * 6) Two or more positive decimal numbers (with/without k,b or w).
344 * seperated by x (also * for backwards compatibility), specifying
345 * the product of the indicated values.
346 */
347 static u_long
348 get_bsz(val)
349 char *val;
350 {
351 u_long num, t;
352 char *expr;
353
354 num = strtoul(val, &expr, 0);
355 if (num == ULONG_MAX) /* Overflow. */
356 err(1, "%s", oper);
357 if (expr == val) /* No digits. */
358 errx(1, "%s: illegal numeric value", oper);
359
360 switch(*expr) {
361 case 'b':
362 t = num;
363 num *= 512;
364 if (t > num)
365 goto erange;
366 ++expr;
367 break;
368 case 'k':
369 t = num;
370 num *= 1024;
371 if (t > num)
372 goto erange;
373 ++expr;
374 break;
375 case 'm':
376 t = num;
377 num *= 1048576;
378 if (t > num)
379 goto erange;
380 ++expr;
381 break;
382 case 'w':
383 t = num;
384 num *= sizeof(int);
385 if (t > num)
386 goto erange;
387 ++expr;
388 break;
389 }
390
391 switch(*expr) {
392 case '\0':
393 break;
394 case '*': /* Backward compatible. */
395 case 'x':
396 t = num;
397 num *= get_bsz(expr + 1);
398 if (t > num)
399 erange: errx(1, "%s: %s", oper, strerror(ERANGE));
400 break;
401 default:
402 errx(1, "%s: illegal numeric value", oper);
403 }
404 return (num);
405 }
406