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