xref: /netbsd-src/bin/dd/args.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: args.c,v 1.7 1996/03/01 01:18:58 jtc 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.7 1996/03/01 01:18:58 jtc 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 const 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 #ifdef	NO_CONV
274 /* Build a small version (i.e. for a ramdisk root) */
275 static void
276 f_conv(arg)
277 	char *arg;
278 {
279 	errx(1, "conv option disabled");
280 }
281 #else	/* NO_CONV */
282 
283 const static struct conv {
284 	char *name;
285 	u_int set, noset;
286 	const u_char *ctab;
287 } clist[] = {
288 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
289 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
290 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
291 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
292 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
293 	{ "noerror",	C_NOERROR,	0,		NULL },
294 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
295 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
296 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
297 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
298 	{ "osync",	C_OSYNC,	C_BS,		NULL },
299 	{ "swab",	C_SWAB,		0,		NULL },
300 	{ "sync",	C_SYNC,		0,		NULL },
301 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
302 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
303 };
304 
305 static void
306 f_conv(arg)
307 	char *arg;
308 {
309 	struct conv *cp, tmp;
310 
311 	while (arg != NULL) {
312 		tmp.name = strsep(&arg, ",");
313 		if (!(cp = (struct conv *)bsearch(&tmp, clist,
314 		    sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
315 		    c_conv)))
316 			errx(1, "unknown conversion %s", tmp.name);
317 		if (ddflags & cp->noset)
318 			errx(1, "%s: illegal conversion combination", tmp.name);
319 		ddflags |= cp->set;
320 		if (cp->ctab)
321 			ctab = cp->ctab;
322 	}
323 }
324 
325 static int
326 c_conv(a, b)
327 	const void *a, *b;
328 {
329 
330 	return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
331 }
332 
333 #endif	/* NO_CONV */
334 
335 /*
336  * Convert an expression of the following forms to an unsigned long.
337  * 	1) A positive decimal number.
338  *	2) A positive decimal number followed by a b (mult by 512).
339  *	3) A positive decimal number followed by a k (mult by 1024).
340  *	4) A positive decimal number followed by a m (mult by 512).
341  *	5) A positive decimal number followed by a w (mult by sizeof int)
342  *	6) Two or more positive decimal numbers (with/without k,b or w).
343  *	   seperated by x (also * for backwards compatibility), specifying
344  *	   the product of the indicated values.
345  */
346 static u_long
347 get_bsz(val)
348 	char *val;
349 {
350 	u_long num, t;
351 	char *expr;
352 
353 	num = strtoul(val, &expr, 0);
354 	if (num == ULONG_MAX)			/* Overflow. */
355 		err(1, "%s", oper);
356 	if (expr == val)			/* No digits. */
357 		errx(1, "%s: illegal numeric value", oper);
358 
359 	switch(*expr) {
360 	case 'b':
361 		t = num;
362 		num *= 512;
363 		if (t > num)
364 			goto erange;
365 		++expr;
366 		break;
367 	case 'k':
368 		t = num;
369 		num *= 1024;
370 		if (t > num)
371 			goto erange;
372 		++expr;
373 		break;
374 	case 'm':
375 		t = num;
376 		num *= 1048576;
377 		if (t > num)
378 			goto erange;
379 		++expr;
380 		break;
381 	case 'w':
382 		t = num;
383 		num *= sizeof(int);
384 		if (t > num)
385 			goto erange;
386 		++expr;
387 		break;
388 	}
389 
390 	switch(*expr) {
391 		case '\0':
392 			break;
393 		case '*':			/* Backward compatible. */
394 		case 'x':
395 			t = num;
396 			num *= get_bsz(expr + 1);
397 			if (t > num)
398 erange:				errx(1, "%s: %s", oper, strerror(ERANGE));
399 			break;
400 		default:
401 			errx(1, "%s: illegal numeric value", oper);
402 	}
403 	return (num);
404 }
405