Home | History | Annotate | Line # | Download | only in dd
args.c revision 1.25
      1 /*	$NetBSD: args.c,v 1.25 2004/01/17 20:48:57 dbj 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. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)args.c	8.3 (Berkeley) 4/2/94";
     40 #else
     41 __RCSID("$NetBSD: args.c,v 1.25 2004/01/17 20:48:57 dbj Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/time.h>
     47 
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <limits.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 
     55 #include "dd.h"
     56 #include "extern.h"
     57 
     58 static int	c_arg(const void *, const void *);
     59 #ifndef	NO_CONV
     60 static int	c_conv(const void *, const void *);
     61 #endif
     62 static void	f_bs(char *);
     63 static void	f_cbs(char *);
     64 static void	f_conv(char *);
     65 static void	f_count(char *);
     66 static void	f_files(char *);
     67 static void	f_ibs(char *);
     68 static void	f_if(char *);
     69 static void	f_obs(char *);
     70 static void	f_of(char *);
     71 static void	f_seek(char *);
     72 static void	f_skip(char *);
     73 static void	f_progress(char *);
     74 
     75 static const struct arg {
     76 	const char *name;
     77 	void (*f)(char *);
     78 	u_int set, noset;
     79 } args[] = {
     80      /* the array needs to be sorted by the first column so
     81 	bsearch() can be used to find commands quickly */
     82 	{ "bs",		f_bs,		C_BS,	 C_BS|C_IBS|C_OBS|C_OSYNC },
     83 	{ "cbs",	f_cbs,		C_CBS,	 C_CBS },
     84 	{ "conv",	f_conv,		0,	 0 },
     85 	{ "count",	f_count,	C_COUNT, C_COUNT },
     86 	{ "files",	f_files,	C_FILES, C_FILES },
     87 	{ "ibs",	f_ibs,		C_IBS,	 C_BS|C_IBS },
     88 	{ "if",		f_if,		C_IF,	 C_IF },
     89 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
     90 	{ "of",		f_of,		C_OF,	 C_OF },
     91 	{ "progress",	f_progress,	0,	 0 },
     92 	{ "seek",	f_seek,		C_SEEK,	 C_SEEK },
     93 	{ "skip",	f_skip,		C_SKIP,	 C_SKIP },
     94 };
     95 
     96 /*
     97  * args -- parse JCL syntax of dd.
     98  */
     99 void
    100 jcl(char **argv)
    101 {
    102 	struct arg *ap, tmp;
    103 	char *oper, *arg;
    104 
    105 	in.dbsz = out.dbsz = 512;
    106 
    107 	while ((oper = *++argv) != NULL) {
    108 		if ((arg = strchr(oper, '=')) == NULL) {
    109 			errx(EXIT_FAILURE, "unknown operand %s", oper);
    110 			/* NOTREACHED */
    111 		}
    112 		*arg++ = '\0';
    113 		if (!*arg) {
    114 			errx(EXIT_FAILURE, "no value specified for %s", oper);
    115 			/* NOTREACHED */
    116 		}
    117 		tmp.name = oper;
    118 		if (!(ap = (struct arg *)bsearch(&tmp, args,
    119 		    sizeof(args)/sizeof(struct arg), sizeof(struct arg),
    120 		    c_arg))) {
    121 			errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
    122 			/* NOTREACHED */
    123 		}
    124 		if (ddflags & ap->noset) {
    125 			errx(EXIT_FAILURE,
    126 			    "%s: illegal argument combination or already set",
    127 			    tmp.name);
    128 			/* NOTREACHED */
    129 		}
    130 		ddflags |= ap->set;
    131 		ap->f(arg);
    132 	}
    133 
    134 	/* Final sanity checks. */
    135 
    136 	if (ddflags & C_BS) {
    137 		/*
    138 		 * Bs is turned off by any conversion -- we assume the user
    139 		 * just wanted to set both the input and output block sizes
    140 		 * and didn't want the bs semantics, so we don't warn.
    141 		 */
    142 		if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
    143 		    C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
    144 			ddflags &= ~C_BS;
    145 			ddflags |= C_IBS|C_OBS;
    146 		}
    147 
    148 		/* Bs supersedes ibs and obs. */
    149 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
    150 			warnx("bs supersedes ibs and obs");
    151 	}
    152 
    153 	/*
    154 	 * Ascii/ebcdic and cbs implies block/unblock.
    155 	 * Block/unblock requires cbs and vice-versa.
    156 	 */
    157 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
    158 		if (!(ddflags & C_CBS)) {
    159 			errx(EXIT_FAILURE, "record operations require cbs");
    160 			/* NOTREACHED */
    161 		}
    162 		cfunc = ddflags & C_BLOCK ? block : unblock;
    163 	} else if (ddflags & C_CBS) {
    164 		if (ddflags & (C_ASCII|C_EBCDIC)) {
    165 			if (ddflags & C_ASCII) {
    166 				ddflags |= C_UNBLOCK;
    167 				cfunc = unblock;
    168 			} else {
    169 				ddflags |= C_BLOCK;
    170 				cfunc = block;
    171 			}
    172 		} else {
    173 			errx(EXIT_FAILURE,
    174 			    "cbs meaningless if not doing record operations");
    175 			/* NOTREACHED */
    176 		}
    177 	} else
    178 		cfunc = def;
    179 
    180 	/* Read, write and seek calls take off_t as arguments.
    181 	 *
    182 	 * The following check is not done because an off_t is a quad
    183 	 *  for current NetBSD implementations.
    184 	 *
    185 	 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
    186 	 *	errx(1, "seek offsets cannot be larger than %d", INT_MAX);
    187 	 */
    188 }
    189 
    190 static int
    191 c_arg(const void *a, const void *b)
    192 {
    193 
    194 	return (strcmp(((const struct arg *)a)->name,
    195 	    ((const struct arg *)b)->name));
    196 }
    197 
    198 static void
    199 f_bs(char *arg)
    200 {
    201 
    202 	in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
    203 }
    204 
    205 static void
    206 f_cbs(char *arg)
    207 {
    208 
    209 	cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
    210 }
    211 
    212 static void
    213 f_count(char *arg)
    214 {
    215 
    216 	cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
    217 	if (!cpy_cnt)
    218 		terminate(0);
    219 }
    220 
    221 static void
    222 f_files(char *arg)
    223 {
    224 
    225 	files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
    226 	if (!files_cnt)
    227 		terminate(0);
    228 }
    229 
    230 static void
    231 f_ibs(char *arg)
    232 {
    233 
    234 	if (!(ddflags & C_BS))
    235 		in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
    236 }
    237 
    238 static void
    239 f_if(char *arg)
    240 {
    241 
    242 	in.name = arg;
    243 }
    244 
    245 static void
    246 f_obs(char *arg)
    247 {
    248 
    249 	if (!(ddflags & C_BS))
    250 		out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
    251 }
    252 
    253 static void
    254 f_of(char *arg)
    255 {
    256 
    257 	out.name = arg;
    258 }
    259 
    260 static void
    261 f_seek(char *arg)
    262 {
    263 
    264 	out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
    265 }
    266 
    267 static void
    268 f_skip(char *arg)
    269 {
    270 
    271 	in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
    272 }
    273 
    274 static void
    275 f_progress(char *arg)
    276 {
    277 
    278 	if (*arg != '0')
    279 		progress = 1;
    280 }
    281 
    282 #ifdef	NO_CONV
    283 /* Build a small version (i.e. for a ramdisk root) */
    284 static void
    285 f_conv(char *arg)
    286 {
    287 
    288 	errx(EXIT_FAILURE, "conv option disabled");
    289 	/* NOTREACHED */
    290 }
    291 #else	/* NO_CONV */
    292 
    293 static const struct conv {
    294 	const char *name;
    295 	u_int set, noset;
    296 	const u_char *ctab;
    297 } clist[] = {
    298 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
    299 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
    300 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
    301 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
    302 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
    303 	{ "noerror",	C_NOERROR,	0,		NULL },
    304 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
    305 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
    306 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
    307 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
    308 	{ "osync",	C_OSYNC,	C_BS,		NULL },
    309 	{ "sparse",	C_SPARSE,	0,		NULL },
    310 	{ "swab",	C_SWAB,		0,		NULL },
    311 	{ "sync",	C_SYNC,		0,		NULL },
    312 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
    313 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
    314 	/* If you add items to this table, be sure to add the
    315 	 * conversions to the C_BS check in the jcl routine above.
    316 	 */
    317 };
    318 
    319 static void
    320 f_conv(char *arg)
    321 {
    322 	struct conv *cp, tmp;
    323 
    324 	while (arg != NULL) {
    325 		tmp.name = strsep(&arg, ",");
    326 		if (!(cp = (struct conv *)bsearch(&tmp, clist,
    327 		    sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
    328 		    c_conv))) {
    329 			errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
    330 			/* NOTREACHED */
    331 		}
    332 		if (ddflags & cp->noset) {
    333 			errx(EXIT_FAILURE, "%s: illegal conversion combination", tmp.name);
    334 			/* NOTREACHED */
    335 		}
    336 		ddflags |= cp->set;
    337 		if (cp->ctab)
    338 			ctab = cp->ctab;
    339 	}
    340 }
    341 
    342 static int
    343 c_conv(const void *a, const void *b)
    344 {
    345 
    346 	return (strcmp(((const struct conv *)a)->name,
    347 	    ((const struct conv *)b)->name));
    348 }
    349 
    350 #endif	/* NO_CONV */
    351