Home | History | Annotate | Line # | Download | only in dd
args.c revision 1.1.1.1
      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 
     38 #ifndef lint
     39 static char sccsid[] = "@(#)args.c	8.3 (Berkeley) 4/2/94";
     40 #endif /* not lint */
     41 
     42 #include <sys/types.h>
     43 
     44 #include <err.h>
     45 #include <errno.h>
     46 #include <limits.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 
     51 #include "dd.h"
     52 #include "extern.h"
     53 
     54 static int	c_arg __P((const void *, const void *));
     55 static int	c_conv __P((const void *, const void *));
     56 static void	f_bs __P((char *));
     57 static void	f_cbs __P((char *));
     58 static void	f_conv __P((char *));
     59 static void	f_count __P((char *));
     60 static void	f_files __P((char *));
     61 static void	f_ibs __P((char *));
     62 static void	f_if __P((char *));
     63 static void	f_obs __P((char *));
     64 static void	f_of __P((char *));
     65 static void	f_seek __P((char *));
     66 static void	f_skip __P((char *));
     67 static u_long	get_bsz __P((char *));
     68 
     69 static struct arg {
     70 	char *name;
     71 	void (*f) __P((char *));
     72 	u_int set, noset;
     73 } args[] = {
     74 	{ "bs",		f_bs,		C_BS,	 C_BS|C_IBS|C_OBS|C_OSYNC },
     75 	{ "cbs",	f_cbs,		C_CBS,	 C_CBS },
     76 	{ "conv",	f_conv,		0,	 0 },
     77 	{ "count",	f_count,	C_COUNT, C_COUNT },
     78 	{ "files",	f_files,	C_FILES, C_FILES },
     79 	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
     80 	{ "if",		f_if,		C_IF,	 C_IF },
     81 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
     82 	{ "of",		f_of,		C_OF,	 C_OF },
     83 	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
     84 	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
     85 };
     86 
     87 static char *oper;
     88 
     89 /*
     90  * args -- parse JCL syntax of dd.
     91  */
     92 void
     93 jcl(argv)
     94 	char **argv;
     95 {
     96 	struct arg *ap, tmp;
     97 	char *arg;
     98 
     99 	in.dbsz = out.dbsz = 512;
    100 
    101 	while (oper = *++argv) {
    102 		if ((arg = strchr(oper, '=')) == NULL)
    103 			errx(1, "unknown operand %s", oper);
    104 		*arg++ = '\0';
    105 		if (!*arg)
    106 			errx(1, "no value specified for %s", oper);
    107 		tmp.name = oper;
    108 		if (!(ap = (struct arg *)bsearch(&tmp, args,
    109 		    sizeof(args)/sizeof(struct arg), sizeof(struct arg),
    110 		    c_arg)))
    111 			errx(1, "unknown operand %s", tmp.name);
    112 		if (ddflags & ap->noset)
    113 			errx(1, "%s: illegal argument combination or already set",
    114 			    tmp.name);
    115 		ddflags |= ap->set;
    116 		ap->f(arg);
    117 	}
    118 
    119 	/* Final sanity checks. */
    120 
    121 	if (ddflags & C_BS) {
    122 		/*
    123 		 * Bs is turned off by any conversion -- we assume the user
    124 		 * just wanted to set both the input and output block sizes
    125 		 * and didn't want the bs semantics, so we don't warn.
    126 		 */
    127 		if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
    128 			ddflags &= ~C_BS;
    129 
    130 		/* Bs supersedes ibs and obs. */
    131 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
    132 			warnx("bs supersedes ibs and obs");
    133 	}
    134 
    135 	/*
    136 	 * Ascii/ebcdic and cbs implies block/unblock.
    137 	 * Block/unblock requires cbs and vice-versa.
    138 	 */
    139 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
    140 		if (!(ddflags & C_CBS))
    141 			errx(1, "record operations require cbs");
    142 		if (cbsz == 0)
    143 			errx(1, "cbs cannot be zero");
    144 		cfunc = ddflags & C_BLOCK ? block : unblock;
    145 	} else if (ddflags & C_CBS) {
    146 		if (ddflags & (C_ASCII|C_EBCDIC)) {
    147 			if (ddflags & C_ASCII) {
    148 				ddflags |= C_UNBLOCK;
    149 				cfunc = unblock;
    150 			} else {
    151 				ddflags |= C_BLOCK;
    152 				cfunc = block;
    153 			}
    154 		} else
    155 			errx(1, "cbs meaningless if not doing record operations");
    156 		if (cbsz == 0)
    157 			errx(1, "cbs cannot be zero");
    158 	} else
    159 		cfunc = def;
    160 
    161 	if (in.dbsz == 0 || out.dbsz == 0)
    162 		errx(1, "buffer sizes cannot be zero");
    163 
    164 	/*
    165 	 * Read, write and seek calls take ints as arguments.  Seek sizes
    166 	 * could be larger if we wanted to do it in stages or check only
    167 	 * regular files, but it's probably not worth it.
    168 	 */
    169 	if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
    170 		errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
    171 	if (in.offset > INT_MAX / in.dbsz || out.offset > INT_MAX / out.dbsz)
    172 		errx(1, "seek offsets cannot be larger than %d", INT_MAX);
    173 }
    174 
    175 static int
    176 c_arg(a, b)
    177 	const void *a, *b;
    178 {
    179 
    180 	return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name));
    181 }
    182 
    183 static void
    184 f_bs(arg)
    185 	char *arg;
    186 {
    187 
    188 	in.dbsz = out.dbsz = (int)get_bsz(arg);
    189 }
    190 
    191 static void
    192 f_cbs(arg)
    193 	char *arg;
    194 {
    195 
    196 	cbsz = (int)get_bsz(arg);
    197 }
    198 
    199 static void
    200 f_count(arg)
    201 	char *arg;
    202 {
    203 
    204 	cpy_cnt = (u_int)get_bsz(arg);
    205 	if (!cpy_cnt)
    206 		terminate(0);
    207 }
    208 
    209 static void
    210 f_files(arg)
    211 	char *arg;
    212 {
    213 
    214 	files_cnt = (int)get_bsz(arg);
    215 }
    216 
    217 static void
    218 f_ibs(arg)
    219 	char *arg;
    220 {
    221 
    222 	if (!(ddflags & C_BS))
    223 		in.dbsz = (int)get_bsz(arg);
    224 }
    225 
    226 static void
    227 f_if(arg)
    228 	char *arg;
    229 {
    230 
    231 	in.name = arg;
    232 }
    233 
    234 static void
    235 f_obs(arg)
    236 	char *arg;
    237 {
    238 
    239 	if (!(ddflags & C_BS))
    240 		out.dbsz = (int)get_bsz(arg);
    241 }
    242 
    243 static void
    244 f_of(arg)
    245 	char *arg;
    246 {
    247 
    248 	out.name = arg;
    249 }
    250 
    251 static void
    252 f_seek(arg)
    253 	char *arg;
    254 {
    255 
    256 	out.offset = (u_int)get_bsz(arg);
    257 }
    258 
    259 static void
    260 f_skip(arg)
    261 	char *arg;
    262 {
    263 
    264 	in.offset = (u_int)get_bsz(arg);
    265 }
    266 
    267 static struct conv {
    268 	char *name;
    269 	u_int set, noset;
    270 	u_char *ctab;
    271 } clist[] = {
    272 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
    273 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
    274 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
    275 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
    276 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
    277 	{ "noerror",	C_NOERROR,	0,		NULL },
    278 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
    279 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
    280 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
    281 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
    282 	{ "osync",	C_OSYNC,	C_BS,		NULL },
    283 	{ "swab",	C_SWAB,		0,		NULL },
    284 	{ "sync",	C_SYNC,		0,		NULL },
    285 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
    286 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
    287 };
    288 
    289 static void
    290 f_conv(arg)
    291 	char *arg;
    292 {
    293 	struct conv *cp, tmp;
    294 
    295 	while (arg != NULL) {
    296 		tmp.name = strsep(&arg, ",");
    297 		if (!(cp = (struct conv *)bsearch(&tmp, clist,
    298 		    sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
    299 		    c_conv)))
    300 			errx(1, "unknown conversion %s", tmp.name);
    301 		if (ddflags & cp->noset)
    302 			errx(1, "%s: illegal conversion combination", tmp.name);
    303 		ddflags |= cp->set;
    304 		if (cp->ctab)
    305 			ctab = cp->ctab;
    306 	}
    307 }
    308 
    309 static int
    310 c_conv(a, b)
    311 	const void *a, *b;
    312 {
    313 
    314 	return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name));
    315 }
    316 
    317 /*
    318  * Convert an expression of the following forms to an unsigned long.
    319  * 	1) A positive decimal number.
    320  *	2) A positive decimal number followed by a b (mult by 512).
    321  *	3) A positive decimal number followed by a k (mult by 1024).
    322  *	4) A positive decimal number followed by a m (mult by 512).
    323  *	5) A positive decimal number followed by a w (mult by sizeof int)
    324  *	6) Two or more positive decimal numbers (with/without k,b or w).
    325  *	   seperated by x (also * for backwards compatibility), specifying
    326  *	   the product of the indicated values.
    327  */
    328 static u_long
    329 get_bsz(val)
    330 	char *val;
    331 {
    332 	u_long num, t;
    333 	char *expr;
    334 
    335 	num = strtoul(val, &expr, 0);
    336 	if (num == ULONG_MAX)			/* Overflow. */
    337 		err(1, "%s", oper);
    338 	if (expr == val)			/* No digits. */
    339 		errx(1, "%s: illegal numeric value", oper);
    340 
    341 	switch(*expr) {
    342 	case 'b':
    343 		t = num;
    344 		num *= 512;
    345 		if (t > num)
    346 			goto erange;
    347 		++expr;
    348 		break;
    349 	case 'k':
    350 		t = num;
    351 		num *= 1024;
    352 		if (t > num)
    353 			goto erange;
    354 		++expr;
    355 		break;
    356 	case 'm':
    357 		t = num;
    358 		num *= 1048576;
    359 		if (t > num)
    360 			goto erange;
    361 		++expr;
    362 		break;
    363 	case 'w':
    364 		t = num;
    365 		num *= sizeof(int);
    366 		if (t > num)
    367 			goto erange;
    368 		++expr;
    369 		break;
    370 	}
    371 
    372 	switch(*expr) {
    373 		case '\0':
    374 			break;
    375 		case '*':			/* Backward compatible. */
    376 		case 'x':
    377 			t = num;
    378 			num *= get_bsz(expr + 1);
    379 			if (t > num)
    380 erange:				errx(1, "%s: %s", oper, strerror(ERANGE));
    381 			break;
    382 		default:
    383 			errx(1, "%s: illegal numeric value", oper);
    384 	}
    385 	return (num);
    386 }
    387