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