1c886b605Sbostic /*-
2*dc928f48Spendry * Copyright (c) 1991, 1993, 1994
36a4e3636Sbostic * The Regents of the University of California. All rights reserved.
4c886b605Sbostic *
5c886b605Sbostic * This code is derived from software contributed to Berkeley by
6a254c6d4Sbostic * Keith Muller of the University of California, San Diego and Lance
7a254c6d4Sbostic * Visser of Convex Computer Corporation.
8c886b605Sbostic *
9c886b605Sbostic * %sccs.include.redist.c%
10c886b605Sbostic */
11c886b605Sbostic
12c886b605Sbostic #ifndef lint
13*dc928f48Spendry static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 04/02/94";
14c886b605Sbostic #endif /* not lint */
15c886b605Sbostic
16c886b605Sbostic #include <sys/types.h>
17*dc928f48Spendry
18*dc928f48Spendry #include <err.h>
19c886b605Sbostic #include <errno.h>
20*dc928f48Spendry #include <limits.h>
21c886b605Sbostic #include <stdio.h>
22c886b605Sbostic #include <stdlib.h>
23c886b605Sbostic #include <string.h>
24*dc928f48Spendry
25c886b605Sbostic #include "dd.h"
26c886b605Sbostic #include "extern.h"
27c886b605Sbostic
28*dc928f48Spendry static int c_arg __P((const void *, const void *));
29*dc928f48Spendry static int c_conv __P((const void *, const void *));
30c886b605Sbostic static void f_bs __P((char *));
31c886b605Sbostic static void f_cbs __P((char *));
32c886b605Sbostic static void f_conv __P((char *));
33c886b605Sbostic static void f_count __P((char *));
34c886b605Sbostic static void f_files __P((char *));
35c886b605Sbostic static void f_ibs __P((char *));
36c886b605Sbostic static void f_if __P((char *));
37c886b605Sbostic static void f_obs __P((char *));
38c886b605Sbostic static void f_of __P((char *));
39c886b605Sbostic static void f_seek __P((char *));
40c886b605Sbostic static void f_skip __P((char *));
41*dc928f48Spendry static u_long get_bsz __P((char *));
42c886b605Sbostic
43c886b605Sbostic static struct arg {
44c886b605Sbostic char *name;
45c886b605Sbostic void (*f) __P((char *));
46c886b605Sbostic u_int set, noset;
47c886b605Sbostic } args[] = {
48*dc928f48Spendry { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
49*dc928f48Spendry { "cbs", f_cbs, C_CBS, C_CBS },
50*dc928f48Spendry { "conv", f_conv, 0, 0 },
51*dc928f48Spendry { "count", f_count, C_COUNT, C_COUNT },
52*dc928f48Spendry { "files", f_files, C_FILES, C_FILES },
53*dc928f48Spendry { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
54*dc928f48Spendry { "if", f_if, C_IF, C_IF },
55*dc928f48Spendry { "obs", f_obs, C_OBS, C_BS|C_OBS },
56*dc928f48Spendry { "of", f_of, C_OF, C_OF },
57*dc928f48Spendry { "seek", f_seek, C_SEEK, C_SEEK },
58*dc928f48Spendry { "skip", f_skip, C_SKIP, C_SKIP },
59c886b605Sbostic };
60c886b605Sbostic
61c886b605Sbostic static char *oper;
62c886b605Sbostic
63c886b605Sbostic /*
6469489919Sbostic * args -- parse JCL syntax of dd.
65c886b605Sbostic */
66c886b605Sbostic void
jcl(argv)67c886b605Sbostic jcl(argv)
68*dc928f48Spendry char **argv;
69c886b605Sbostic {
70*dc928f48Spendry struct arg *ap, tmp;
71c886b605Sbostic char *arg;
72c886b605Sbostic
73c886b605Sbostic in.dbsz = out.dbsz = 512;
74c886b605Sbostic
75c886b605Sbostic while (oper = *++argv) {
76*dc928f48Spendry if ((arg = strchr(oper, '=')) == NULL)
77*dc928f48Spendry errx(1, "unknown operand %s", oper);
78c886b605Sbostic *arg++ = '\0';
79c886b605Sbostic if (!*arg)
80*dc928f48Spendry errx(1, "no value specified for %s", oper);
81c886b605Sbostic tmp.name = oper;
82c886b605Sbostic if (!(ap = (struct arg *)bsearch(&tmp, args,
83c886b605Sbostic sizeof(args)/sizeof(struct arg), sizeof(struct arg),
84c886b605Sbostic c_arg)))
85*dc928f48Spendry errx(1, "unknown operand %s", tmp.name);
86c886b605Sbostic if (ddflags & ap->noset)
87*dc928f48Spendry errx(1, "%s: illegal argument combination or already set",
88c886b605Sbostic tmp.name);
89c886b605Sbostic ddflags |= ap->set;
90c886b605Sbostic ap->f(arg);
91c886b605Sbostic }
92c886b605Sbostic
93c886b605Sbostic /* Final sanity checks. */
94c886b605Sbostic
95c886b605Sbostic if (ddflags & C_BS) {
96c886b605Sbostic /*
97c886b605Sbostic * Bs is turned off by any conversion -- we assume the user
98c886b605Sbostic * just wanted to set both the input and output block sizes
99c886b605Sbostic * and didn't want the bs semantics, so we don't warn.
100c886b605Sbostic */
1010c77611aSbostic if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
102c886b605Sbostic ddflags &= ~C_BS;
103c886b605Sbostic
104c886b605Sbostic /* Bs supersedes ibs and obs. */
105c886b605Sbostic if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
106*dc928f48Spendry warnx("bs supersedes ibs and obs");
107c886b605Sbostic }
108c886b605Sbostic
10969489919Sbostic /*
11069489919Sbostic * Ascii/ebcdic and cbs implies block/unblock.
11169489919Sbostic * Block/unblock requires cbs and vice-versa.
11269489919Sbostic */
113c886b605Sbostic if (ddflags & (C_BLOCK|C_UNBLOCK)) {
11469489919Sbostic if (!(ddflags & C_CBS))
115*dc928f48Spendry errx(1, "record operations require cbs");
116c886b605Sbostic if (cbsz == 0)
117*dc928f48Spendry errx(1, "cbs cannot be zero");
11869489919Sbostic cfunc = ddflags & C_BLOCK ? block : unblock;
11969489919Sbostic } else if (ddflags & C_CBS) {
12069489919Sbostic if (ddflags & (C_ASCII|C_EBCDIC)) {
12169489919Sbostic if (ddflags & C_ASCII) {
12269489919Sbostic ddflags |= C_UNBLOCK;
12369489919Sbostic cfunc = unblock;
12469489919Sbostic } else {
12569489919Sbostic ddflags |= C_BLOCK;
12669489919Sbostic cfunc = block;
12769489919Sbostic }
128c886b605Sbostic } else
129*dc928f48Spendry errx(1, "cbs meaningless if not doing record operations");
13069489919Sbostic if (cbsz == 0)
131*dc928f48Spendry errx(1, "cbs cannot be zero");
13269489919Sbostic } else
13369489919Sbostic cfunc = def;
134c886b605Sbostic
135c886b605Sbostic if (in.dbsz == 0 || out.dbsz == 0)
136*dc928f48Spendry errx(1, "buffer sizes cannot be zero");
137c886b605Sbostic
13869489919Sbostic /*
13969489919Sbostic * Read, write and seek calls take ints as arguments. Seek sizes
14069489919Sbostic * could be larger if we wanted to do it in stages or check only
14169489919Sbostic * regular files, but it's probably not worth it.
14269489919Sbostic */
14369489919Sbostic if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
144*dc928f48Spendry errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
14569489919Sbostic if (in.offset > INT_MAX / in.dbsz || out.offset > INT_MAX / out.dbsz)
146*dc928f48Spendry errx(1, "seek offsets cannot be larger than %d", INT_MAX);
147c886b605Sbostic }
148c886b605Sbostic
149c886b605Sbostic static int
c_arg(a,b)150c886b605Sbostic c_arg(a, b)
151c886b605Sbostic const void *a, *b;
152c886b605Sbostic {
153*dc928f48Spendry
154c886b605Sbostic return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
155c886b605Sbostic }
156c886b605Sbostic
157c886b605Sbostic static void
f_bs(arg)158c886b605Sbostic f_bs(arg)
159c886b605Sbostic char *arg;
160c886b605Sbostic {
161*dc928f48Spendry
162c886b605Sbostic in.dbsz = out.dbsz = (int)get_bsz(arg);
163c886b605Sbostic }
164c886b605Sbostic
165c886b605Sbostic static void
f_cbs(arg)166c886b605Sbostic f_cbs(arg)
167c886b605Sbostic char *arg;
168c886b605Sbostic {
169*dc928f48Spendry
170c886b605Sbostic cbsz = (int)get_bsz(arg);
171c886b605Sbostic }
172c886b605Sbostic
173c886b605Sbostic static void
f_count(arg)174c886b605Sbostic f_count(arg)
175c886b605Sbostic char *arg;
176c886b605Sbostic {
177*dc928f48Spendry
178c886b605Sbostic cpy_cnt = (u_int)get_bsz(arg);
17969489919Sbostic if (!cpy_cnt)
180c886b605Sbostic terminate(0);
181c886b605Sbostic }
182c886b605Sbostic
183c886b605Sbostic static void
f_files(arg)18469489919Sbostic f_files(arg)
185c886b605Sbostic char *arg;
186c886b605Sbostic {
187*dc928f48Spendry
188c886b605Sbostic files_cnt = (int)get_bsz(arg);
189c886b605Sbostic }
190c886b605Sbostic
191c886b605Sbostic static void
f_ibs(arg)192c886b605Sbostic f_ibs(arg)
193c886b605Sbostic char *arg;
194c886b605Sbostic {
195*dc928f48Spendry
196c886b605Sbostic if (!(ddflags & C_BS))
197c886b605Sbostic in.dbsz = (int)get_bsz(arg);
198c886b605Sbostic }
199c886b605Sbostic
200c886b605Sbostic static void
f_if(arg)201c886b605Sbostic f_if(arg)
202c886b605Sbostic char *arg;
203c886b605Sbostic {
204*dc928f48Spendry
205c886b605Sbostic in.name = arg;
206c886b605Sbostic }
207c886b605Sbostic
208c886b605Sbostic static void
f_obs(arg)209c886b605Sbostic f_obs(arg)
210c886b605Sbostic char *arg;
211c886b605Sbostic {
212*dc928f48Spendry
213c886b605Sbostic if (!(ddflags & C_BS))
214c886b605Sbostic out.dbsz = (int)get_bsz(arg);
215c886b605Sbostic }
216c886b605Sbostic
217c886b605Sbostic static void
f_of(arg)218c886b605Sbostic f_of(arg)
219c886b605Sbostic char *arg;
220c886b605Sbostic {
221*dc928f48Spendry
222c886b605Sbostic out.name = arg;
223c886b605Sbostic }
224c886b605Sbostic
225c886b605Sbostic static void
f_seek(arg)226c886b605Sbostic f_seek(arg)
227c886b605Sbostic char *arg;
228c886b605Sbostic {
229*dc928f48Spendry
230c886b605Sbostic out.offset = (u_int)get_bsz(arg);
231c886b605Sbostic }
232c886b605Sbostic
233c886b605Sbostic static void
f_skip(arg)234c886b605Sbostic f_skip(arg)
235c886b605Sbostic char *arg;
236c886b605Sbostic {
237*dc928f48Spendry
238c886b605Sbostic in.offset = (u_int)get_bsz(arg);
239c886b605Sbostic }
240c886b605Sbostic
241c886b605Sbostic static struct conv {
242c886b605Sbostic char *name;
243c886b605Sbostic u_int set, noset;
24469489919Sbostic u_char *ctab;
245c886b605Sbostic } clist[] = {
246*dc928f48Spendry { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
247*dc928f48Spendry { "block", C_BLOCK, C_UNBLOCK, NULL },
248*dc928f48Spendry { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
249*dc928f48Spendry { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
250*dc928f48Spendry { "lcase", C_LCASE, C_UCASE, NULL },
251*dc928f48Spendry { "noerror", C_NOERROR, 0, NULL },
252*dc928f48Spendry { "notrunc", C_NOTRUNC, 0, NULL },
253*dc928f48Spendry { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
254*dc928f48Spendry { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
255*dc928f48Spendry { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
256*dc928f48Spendry { "osync", C_OSYNC, C_BS, NULL },
257*dc928f48Spendry { "swab", C_SWAB, 0, NULL },
258*dc928f48Spendry { "sync", C_SYNC, 0, NULL },
259*dc928f48Spendry { "ucase", C_UCASE, C_LCASE, NULL },
260*dc928f48Spendry { "unblock", C_UNBLOCK, C_BLOCK, NULL },
261c886b605Sbostic };
262c886b605Sbostic
263c886b605Sbostic static void
f_conv(arg)264c886b605Sbostic f_conv(arg)
265c886b605Sbostic char *arg;
266c886b605Sbostic {
267*dc928f48Spendry struct conv *cp, tmp;
268c886b605Sbostic
269c886b605Sbostic while (arg != NULL) {
270c886b605Sbostic tmp.name = strsep(&arg, ",");
271c886b605Sbostic if (!(cp = (struct conv *)bsearch(&tmp, clist,
272c886b605Sbostic sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
273c886b605Sbostic c_conv)))
274*dc928f48Spendry errx(1, "unknown conversion %s", tmp.name);
275c886b605Sbostic if (ddflags & cp->noset)
276*dc928f48Spendry errx(1, "%s: illegal conversion combination", tmp.name);
277c886b605Sbostic ddflags |= cp->set;
27869489919Sbostic if (cp->ctab)
27969489919Sbostic ctab = cp->ctab;
280c886b605Sbostic }
281c886b605Sbostic }
282c886b605Sbostic
283c886b605Sbostic static int
c_conv(a,b)284c886b605Sbostic c_conv(a, b)
285c886b605Sbostic const void *a, *b;
286c886b605Sbostic {
287*dc928f48Spendry
288c886b605Sbostic return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
289c886b605Sbostic }
290c886b605Sbostic
291c886b605Sbostic /*
292c886b605Sbostic * Convert an expression of the following forms to an unsigned long.
293c886b605Sbostic * 1) A positive decimal number.
29469489919Sbostic * 2) A positive decimal number followed by a b (mult by 512).
29569489919Sbostic * 3) A positive decimal number followed by a k (mult by 1024).
29669489919Sbostic * 4) A positive decimal number followed by a m (mult by 512).
29769489919Sbostic * 5) A positive decimal number followed by a w (mult by sizeof int)
29869489919Sbostic * 6) Two or more positive decimal numbers (with/without k,b or w).
299c886b605Sbostic * seperated by x (also * for backwards compatibility), specifying
300c886b605Sbostic * the product of the indicated values.
301c886b605Sbostic */
302c886b605Sbostic static u_long
get_bsz(val)303c886b605Sbostic get_bsz(val)
304c886b605Sbostic char *val;
305c886b605Sbostic {
306c886b605Sbostic u_long num, t;
307*dc928f48Spendry char *expr;
308c886b605Sbostic
309c886b605Sbostic num = strtoul(val, &expr, 0);
310c886b605Sbostic if (num == ULONG_MAX) /* Overflow. */
311*dc928f48Spendry err(1, "%s", oper);
312c886b605Sbostic if (expr == val) /* No digits. */
313*dc928f48Spendry errx(1, "%s: illegal numeric value", oper);
314c886b605Sbostic
315c886b605Sbostic switch(*expr) {
316c886b605Sbostic case 'b':
317c886b605Sbostic t = num;
318c886b605Sbostic num *= 512;
319c886b605Sbostic if (t > num)
320c886b605Sbostic goto erange;
321c886b605Sbostic ++expr;
322c886b605Sbostic break;
323c886b605Sbostic case 'k':
324c886b605Sbostic t = num;
325c886b605Sbostic num *= 1024;
326c886b605Sbostic if (t > num)
327c886b605Sbostic goto erange;
328c886b605Sbostic ++expr;
329c886b605Sbostic break;
33034f35428Sbostic case 'm':
33134f35428Sbostic t = num;
33234f35428Sbostic num *= 1048576;
33334f35428Sbostic if (t > num)
33434f35428Sbostic goto erange;
33534f35428Sbostic ++expr;
33634f35428Sbostic break;
33769489919Sbostic case 'w':
338c886b605Sbostic t = num;
339c886b605Sbostic num *= sizeof(int);
340c886b605Sbostic if (t > num)
341c886b605Sbostic goto erange;
342c886b605Sbostic ++expr;
343c886b605Sbostic break;
344c886b605Sbostic }
345c886b605Sbostic
346c886b605Sbostic switch(*expr) {
347c886b605Sbostic case '\0':
348c886b605Sbostic break;
34934f35428Sbostic case '*': /* Backward compatible. */
350c886b605Sbostic case 'x':
351c886b605Sbostic t = num;
352c886b605Sbostic num *= get_bsz(expr + 1);
353c886b605Sbostic if (t > num)
354*dc928f48Spendry erange: errx(1, "%s: %s", oper, strerror(ERANGE));
355c886b605Sbostic break;
356c886b605Sbostic default:
357*dc928f48Spendry errx(1, "%s: illegal numeric value", oper);
358c886b605Sbostic }
359c886b605Sbostic return (num);
360c886b605Sbostic }
361