Home | History | Annotate | Line # | Download | only in dd
args.c revision 1.13
      1 /*	$NetBSD: args.c,v 1.13 1998/07/28 03:47:15 mycroft 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.13 1998/07/28 03:47:15 mycroft 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 static const 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 	 * Check to make sure that the buffers are not too large.
    173 	 */
    174 	if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
    175 		errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
    176 
    177 	/* Read, write and seek calls take off_t as arguments.
    178 	 *
    179 	 * The following check is not done because an off_t is a quad
    180 	 *  for current NetBSD implementations.
    181 	 *
    182 	 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
    183 	 *	errx(1, "seek offsets cannot be larger than %d", INT_MAX);
    184 	 */
    185 }
    186 
    187 static int
    188 c_arg(a, b)
    189 	const void *a, *b;
    190 {
    191 
    192 	return (strcmp(((const struct arg *)a)->name,
    193 	    ((const struct arg *)b)->name));
    194 }
    195 
    196 static void
    197 f_bs(arg)
    198 	char *arg;
    199 {
    200 
    201 	in.dbsz = out.dbsz = (int)get_bsz(arg);
    202 }
    203 
    204 static void
    205 f_cbs(arg)
    206 	char *arg;
    207 {
    208 
    209 	cbsz = (int)get_bsz(arg);
    210 }
    211 
    212 static void
    213 f_count(arg)
    214 	char *arg;
    215 {
    216 
    217 	cpy_cnt = (u_int)get_bsz(arg);
    218 	if (!cpy_cnt)
    219 		terminate(0);
    220 }
    221 
    222 static void
    223 f_files(arg)
    224 	char *arg;
    225 {
    226 
    227 	files_cnt = (int)get_bsz(arg);
    228 }
    229 
    230 static void
    231 f_ibs(arg)
    232 	char *arg;
    233 {
    234 
    235 	if (!(ddflags & C_BS))
    236 		in.dbsz = (int)get_bsz(arg);
    237 }
    238 
    239 static void
    240 f_if(arg)
    241 	char *arg;
    242 {
    243 
    244 	in.name = arg;
    245 }
    246 
    247 static void
    248 f_obs(arg)
    249 	char *arg;
    250 {
    251 
    252 	if (!(ddflags & C_BS))
    253 		out.dbsz = (int)get_bsz(arg);
    254 }
    255 
    256 static void
    257 f_of(arg)
    258 	char *arg;
    259 {
    260 
    261 	out.name = arg;
    262 }
    263 
    264 static void
    265 f_seek(arg)
    266 	char *arg;
    267 {
    268 
    269 	out.offset = (u_int)get_bsz(arg);
    270 }
    271 
    272 static void
    273 f_skip(arg)
    274 	char *arg;
    275 {
    276 
    277 	in.offset = (u_int)get_bsz(arg);
    278 }
    279 
    280 #ifdef	NO_CONV
    281 /* Build a small version (i.e. for a ramdisk root) */
    282 static void
    283 f_conv(arg)
    284 	char *arg;
    285 {
    286 	errx(1, "conv option disabled");
    287 }
    288 #else	/* NO_CONV */
    289 
    290 static const struct conv {
    291 	char *name;
    292 	u_int set, noset;
    293 	const u_char *ctab;
    294 } clist[] = {
    295 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
    296 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
    297 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
    298 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
    299 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
    300 	{ "noerror",	C_NOERROR,	0,		NULL },
    301 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
    302 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
    303 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
    304 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
    305 	{ "osync",	C_OSYNC,	C_BS,		NULL },
    306 	{ "swab",	C_SWAB,		0,		NULL },
    307 	{ "sync",	C_SYNC,		0,		NULL },
    308 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
    309 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
    310 };
    311 
    312 static void
    313 f_conv(arg)
    314 	char *arg;
    315 {
    316 	struct conv *cp, tmp;
    317 
    318 	while (arg != NULL) {
    319 		tmp.name = strsep(&arg, ",");
    320 		if (!(cp = (struct conv *)bsearch(&tmp, clist,
    321 		    sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
    322 		    c_conv)))
    323 			errx(1, "unknown conversion %s", tmp.name);
    324 		if (ddflags & cp->noset)
    325 			errx(1, "%s: illegal conversion combination", tmp.name);
    326 		ddflags |= cp->set;
    327 		if (cp->ctab)
    328 			ctab = cp->ctab;
    329 	}
    330 }
    331 
    332 static int
    333 c_conv(a, b)
    334 	const void *a, *b;
    335 {
    336 
    337 	return (strcmp(((const struct conv *)a)->name,
    338 	    ((const struct conv *)b)->name));
    339 }
    340 
    341 #endif	/* NO_CONV */
    342 
    343 /*
    344  * Convert an expression of the following forms to an unsigned long.
    345  * 	1) A positive decimal number.
    346  *	2) A positive decimal number followed by a b (mult by 512).
    347  *	3) A positive decimal number followed by a k (mult by 1024).
    348  *	4) A positive decimal number followed by a m (mult by 512).
    349  *	5) A positive decimal number followed by a w (mult by sizeof int)
    350  *	6) Two or more positive decimal numbers (with/without k,b or w).
    351  *	   seperated by x (also * for backwards compatibility), specifying
    352  *	   the product of the indicated values.
    353  */
    354 static u_long
    355 get_bsz(val)
    356 	char *val;
    357 {
    358 	u_long num, t;
    359 	char *expr;
    360 
    361 	num = strtoul(val, &expr, 0);
    362 	if (num == ULONG_MAX)			/* Overflow. */
    363 		err(1, "%s", oper);
    364 	if (expr == val)			/* No digits. */
    365 		errx(1, "%s: illegal numeric value", oper);
    366 
    367 	switch (*expr) {
    368 	case 'b':
    369 		t = num;
    370 		num *= 512;
    371 		if (t > num)
    372 			goto erange;
    373 		++expr;
    374 		break;
    375 	case 'k':
    376 		t = num;
    377 		num *= 1024;
    378 		if (t > num)
    379 			goto erange;
    380 		++expr;
    381 		break;
    382 	case 'm':
    383 		t = num;
    384 		num *= 1048576;
    385 		if (t > num)
    386 			goto erange;
    387 		++expr;
    388 		break;
    389 	case 'w':
    390 		t = num;
    391 		num *= sizeof(int);
    392 		if (t > num)
    393 			goto erange;
    394 		++expr;
    395 		break;
    396 	}
    397 
    398 	switch (*expr) {
    399 	case '\0':
    400 		break;
    401 	case '*':				/* Backward compatible. */
    402 	case 'x':
    403 		t = num;
    404 		num *= get_bsz(expr + 1);
    405 		if (t > num)
    406 erange:			errx(1, "%s: %s", oper, strerror(ERANGE));
    407 		break;
    408 	default:
    409 		errx(1, "%s: illegal numeric value", oper);
    410 	}
    411 	return (num);
    412 }
    413