Home | History | Annotate | Line # | Download | only in dd
args.c revision 1.14
      1 /*	$NetBSD: args.c,v 1.14 1999/07/29 19:03:31 hubertf 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.14 1999/07/29 19:03:31 hubertf 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 void	f_progress __P((char *));
     75 static u_long	get_bsz __P((char *));
     76 
     77 static const struct arg {
     78 	char *name;
     79 	void (*f) __P((char *));
     80 	u_int set, noset;
     81 } args[] = {
     82      /* the array needs to be sorted by the first column so
     83         bsearch() can be used to find commands quickly */
     84 	{ "bs",		f_bs,		C_BS,	 C_BS|C_IBS|C_OBS|C_OSYNC },
     85 	{ "cbs",	f_cbs,		C_CBS,	 C_CBS },
     86 	{ "conv",	f_conv,		0,	 0 },
     87 	{ "count",	f_count,	C_COUNT, C_COUNT },
     88 	{ "files",	f_files,	C_FILES, C_FILES },
     89 	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
     90 	{ "if",		f_if,		C_IF,	 C_IF },
     91 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
     92 	{ "of",		f_of,		C_OF,	 C_OF },
     93 	{ "progress",	f_progress,	0,	 0 },
     94 	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
     95 	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
     96 };
     97 
     98 static char *oper;
     99 
    100 /*
    101  * args -- parse JCL syntax of dd.
    102  */
    103 void
    104 jcl(argv)
    105 	char **argv;
    106 {
    107 	struct arg *ap, tmp;
    108 	char *arg;
    109 
    110 	in.dbsz = out.dbsz = 512;
    111 
    112 	while ((oper = *++argv) != NULL) {
    113 		if ((arg = strchr(oper, '=')) == NULL)
    114 			errx(1, "unknown operand %s", oper);
    115 		*arg++ = '\0';
    116 		if (!*arg)
    117 			errx(1, "no value specified for %s", oper);
    118 		tmp.name = oper;
    119 		if (!(ap = (struct arg *)bsearch(&tmp, args,
    120 		    sizeof(args)/sizeof(struct arg), sizeof(struct arg),
    121 		    c_arg)))
    122 			errx(1, "unknown operand %s", tmp.name);
    123 		if (ddflags & ap->noset)
    124 			errx(1, "%s: illegal argument combination or already set",
    125 			    tmp.name);
    126 		ddflags |= ap->set;
    127 		ap->f(arg);
    128 	}
    129 
    130 	/* Final sanity checks. */
    131 
    132 	if (ddflags & C_BS) {
    133 		/*
    134 		 * Bs is turned off by any conversion -- we assume the user
    135 		 * just wanted to set both the input and output block sizes
    136 		 * and didn't want the bs semantics, so we don't warn.
    137 		 */
    138 		if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
    139 			ddflags &= ~C_BS;
    140 
    141 		/* Bs supersedes ibs and obs. */
    142 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
    143 			warnx("bs supersedes ibs and obs");
    144 	}
    145 
    146 	/*
    147 	 * Ascii/ebcdic and cbs implies block/unblock.
    148 	 * Block/unblock requires cbs and vice-versa.
    149 	 */
    150 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
    151 		if (!(ddflags & C_CBS))
    152 			errx(1, "record operations require cbs");
    153 		if (cbsz == 0)
    154 			errx(1, "cbs cannot be zero");
    155 		cfunc = ddflags & C_BLOCK ? block : unblock;
    156 	} else if (ddflags & C_CBS) {
    157 		if (ddflags & (C_ASCII|C_EBCDIC)) {
    158 			if (ddflags & C_ASCII) {
    159 				ddflags |= C_UNBLOCK;
    160 				cfunc = unblock;
    161 			} else {
    162 				ddflags |= C_BLOCK;
    163 				cfunc = block;
    164 			}
    165 		} else
    166 			errx(1, "cbs meaningless if not doing record operations");
    167 		if (cbsz == 0)
    168 			errx(1, "cbs cannot be zero");
    169 	} else
    170 		cfunc = def;
    171 
    172 	if (in.dbsz == 0 || out.dbsz == 0)
    173 		errx(1, "buffer sizes cannot be zero");
    174 
    175 	/*
    176 	 * Check to make sure that the buffers are not too large.
    177 	 */
    178 	if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
    179 		errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
    180 
    181 	/* Read, write and seek calls take off_t as arguments.
    182 	 *
    183 	 * The following check is not done because an off_t is a quad
    184 	 *  for current NetBSD implementations.
    185 	 *
    186 	 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
    187 	 *	errx(1, "seek offsets cannot be larger than %d", INT_MAX);
    188 	 */
    189 }
    190 
    191 static int
    192 c_arg(a, b)
    193 	const void *a, *b;
    194 {
    195 	return (strcmp(((const struct arg *)a)->name,
    196 	    ((const struct arg *)b)->name));
    197 }
    198 
    199 static void
    200 f_bs(arg)
    201 	char *arg;
    202 {
    203 	in.dbsz = out.dbsz = (int)get_bsz(arg);
    204 }
    205 
    206 static void
    207 f_cbs(arg)
    208 	char *arg;
    209 {
    210 	cbsz = (int)get_bsz(arg);
    211 }
    212 
    213 static void
    214 f_count(arg)
    215 	char *arg;
    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 	files_cnt = (int)get_bsz(arg);
    227 }
    228 
    229 static void
    230 f_ibs(arg)
    231 	char *arg;
    232 {
    233 	if (!(ddflags & C_BS))
    234 		in.dbsz = (int)get_bsz(arg);
    235 }
    236 
    237 static void
    238 f_if(arg)
    239 	char *arg;
    240 {
    241 	in.name = arg;
    242 }
    243 
    244 static void
    245 f_obs(arg)
    246 	char *arg;
    247 {
    248 	if (!(ddflags & C_BS))
    249 		out.dbsz = (int)get_bsz(arg);
    250 }
    251 
    252 static void
    253 f_of(arg)
    254 	char *arg;
    255 {
    256 	out.name = arg;
    257 }
    258 
    259 static void
    260 f_seek(arg)
    261 	char *arg;
    262 {
    263 	out.offset = (u_int)get_bsz(arg);
    264 }
    265 
    266 static void
    267 f_skip(arg)
    268 	char *arg;
    269 {
    270 	in.offset = (u_int)get_bsz(arg);
    271 }
    272 
    273 static void
    274 f_progress(arg)
    275 	char *arg;
    276 {
    277 	if (*arg != '0')
    278 		progress = 1;
    279 }
    280 
    281 #ifdef	NO_CONV
    282 /* Build a small version (i.e. for a ramdisk root) */
    283 static void
    284 f_conv(arg)
    285 	char *arg;
    286 {
    287 	errx(1, "conv option disabled");
    288 }
    289 #else	/* NO_CONV */
    290 
    291 static const struct conv {
    292 	char *name;
    293 	u_int set, noset;
    294 	const u_char *ctab;
    295 } clist[] = {
    296 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
    297 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
    298 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
    299 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
    300 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
    301 	{ "noerror",	C_NOERROR,	0,		NULL },
    302 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
    303 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
    304 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
    305 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
    306 	{ "osync",	C_OSYNC,	C_BS,		NULL },
    307 	{ "swab",	C_SWAB,		0,		NULL },
    308 	{ "sync",	C_SYNC,		0,		NULL },
    309 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
    310 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
    311 };
    312 
    313 static void
    314 f_conv(arg)
    315 	char *arg;
    316 {
    317 	struct conv *cp, tmp;
    318 
    319 	while (arg != NULL) {
    320 		tmp.name = strsep(&arg, ",");
    321 		if (!(cp = (struct conv *)bsearch(&tmp, clist,
    322 		    sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
    323 		    c_conv)))
    324 			errx(1, "unknown conversion %s", tmp.name);
    325 		if (ddflags & cp->noset)
    326 			errx(1, "%s: illegal conversion combination", tmp.name);
    327 		ddflags |= cp->set;
    328 		if (cp->ctab)
    329 			ctab = cp->ctab;
    330 	}
    331 }
    332 
    333 static int
    334 c_conv(a, b)
    335 	const void *a, *b;
    336 {
    337 
    338 	return (strcmp(((const struct conv *)a)->name,
    339 	    ((const struct conv *)b)->name));
    340 }
    341 
    342 #endif	/* NO_CONV */
    343 
    344 /*
    345  * Convert an expression of the following forms to an unsigned long.
    346  * 	1) A positive decimal number.
    347  *	2) A positive decimal number followed by a b (mult by 512).
    348  *	3) A positive decimal number followed by a k (mult by 1024).
    349  *	4) A positive decimal number followed by a m (mult by 512).
    350  *	5) A positive decimal number followed by a w (mult by sizeof int)
    351  *	6) Two or more positive decimal numbers (with/without k,b or w).
    352  *	   seperated by x (also * for backwards compatibility), specifying
    353  *	   the product of the indicated values.
    354  */
    355 static u_long
    356 get_bsz(val)
    357 	char *val;
    358 {
    359 	u_long num, t;
    360 	char *expr;
    361 
    362 	num = strtoul(val, &expr, 0);
    363 	if (num == ULONG_MAX)			/* Overflow. */
    364 		err(1, "%s", oper);
    365 	if (expr == val)			/* No digits. */
    366 		errx(1, "%s: illegal numeric value", oper);
    367 
    368 	switch (*expr) {
    369 	case 'b':
    370 		t = num;
    371 		num *= 512;
    372 		if (t > num)
    373 			goto erange;
    374 		++expr;
    375 		break;
    376 	case 'k':
    377 		t = num;
    378 		num *= 1024;
    379 		if (t > num)
    380 			goto erange;
    381 		++expr;
    382 		break;
    383 	case 'm':
    384 		t = num;
    385 		num *= 1048576;
    386 		if (t > num)
    387 			goto erange;
    388 		++expr;
    389 		break;
    390 	case 'w':
    391 		t = num;
    392 		num *= sizeof(int);
    393 		if (t > num)
    394 			goto erange;
    395 		++expr;
    396 		break;
    397 	}
    398 
    399 	switch (*expr) {
    400 	case '\0':
    401 		break;
    402 	case '*':				/* Backward compatible. */
    403 	case 'x':
    404 		t = num;
    405 		num *= get_bsz(expr + 1);
    406 		if (t > num)
    407 erange:			errx(1, "%s: %s", oper, strerror(ERANGE));
    408 		break;
    409 	default:
    410 		errx(1, "%s: illegal numeric value", oper);
    411 	}
    412 	return (num);
    413 }
    414