xref: /netbsd/bin/dd/args.c (revision 6550d01e)
1 /*	$NetBSD: args.c,v 1.34 2011/02/04 19:42:12 pooka 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.34 2011/02/04 19:42:12 pooka 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 	{ "iseek",	f_skip,		C_SKIP,	 C_SKIP },
90 	{ "obs",	f_obs,		C_OBS,	 C_BS|C_OBS },
91 	{ "of",		f_of,		C_OF,	 C_OF },
92 	{ "oseek",	f_seek,		C_SEEK,	 C_SEEK },
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 /*
99  * args -- parse JCL syntax of dd.
100  */
101 void
102 jcl(char **argv)
103 {
104 	struct arg *ap, tmp;
105 	char *oper, *arg;
106 
107 	in.dbsz = out.dbsz = 512;
108 
109 	while ((oper = *++argv) != NULL) {
110 		if ((oper = strdup(oper)) == NULL) {
111 			errx(EXIT_FAILURE,
112 			    "unable to allocate space for the argument %s",
113 			    *argv);
114 			/* NOTREACHED */
115 		}
116 		if ((arg = strchr(oper, '=')) == NULL) {
117 			errx(EXIT_FAILURE, "unknown operand %s", oper);
118 			/* NOTREACHED */
119 		}
120 		*arg++ = '\0';
121 		if (!*arg) {
122 			errx(EXIT_FAILURE, "no value specified for %s", oper);
123 			/* NOTREACHED */
124 		}
125 		tmp.name = oper;
126 		if (!(ap = bsearch(&tmp, args,
127 		    __arraycount(args), sizeof(*args), c_arg))) {
128 			errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
129 			/* NOTREACHED */
130 		}
131 		if (ddflags & ap->noset) {
132 			errx(EXIT_FAILURE,
133 			    "%s: illegal argument combination or already set",
134 			    tmp.name);
135 			/* NOTREACHED */
136 		}
137 		ddflags |= ap->set;
138 		ap->f(arg);
139 	}
140 
141 	/* Final sanity checks. */
142 
143 	if (ddflags & C_BS) {
144 		/*
145 		 * Bs is turned off by any conversion -- we assume the user
146 		 * just wanted to set both the input and output block sizes
147 		 * and didn't want the bs semantics, so we don't warn.
148 		 */
149 		if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
150 		    C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
151 			ddflags &= ~C_BS;
152 			ddflags |= C_IBS|C_OBS;
153 		}
154 
155 		/* Bs supersedes ibs and obs. */
156 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
157 			warnx("bs supersedes ibs and obs");
158 	}
159 
160 	/*
161 	 * Ascii/ebcdic and cbs implies block/unblock.
162 	 * Block/unblock requires cbs and vice-versa.
163 	 */
164 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
165 		if (!(ddflags & C_CBS)) {
166 			errx(EXIT_FAILURE, "record operations require cbs");
167 			/* NOTREACHED */
168 		}
169 		cfunc = ddflags & C_BLOCK ? block : unblock;
170 	} else if (ddflags & C_CBS) {
171 		if (ddflags & (C_ASCII|C_EBCDIC)) {
172 			if (ddflags & C_ASCII) {
173 				ddflags |= C_UNBLOCK;
174 				cfunc = unblock;
175 			} else {
176 				ddflags |= C_BLOCK;
177 				cfunc = block;
178 			}
179 		} else {
180 			errx(EXIT_FAILURE,
181 			    "cbs meaningless if not doing record operations");
182 			/* NOTREACHED */
183 		}
184 	} else
185 		cfunc = def;
186 
187 	/* Read, write and seek calls take off_t as arguments.
188 	 *
189 	 * The following check is not done because an off_t is a quad
190 	 *  for current NetBSD implementations.
191 	 *
192 	 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
193 	 *	errx(1, "seek offsets cannot be larger than %d", INT_MAX);
194 	 */
195 }
196 
197 static int
198 c_arg(const void *a, const void *b)
199 {
200 
201 	return (strcmp(((const struct arg *)a)->name,
202 	    ((const struct arg *)b)->name));
203 }
204 
205 static void
206 f_bs(char *arg)
207 {
208 
209 	in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
210 }
211 
212 static void
213 f_cbs(char *arg)
214 {
215 
216 	cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
217 }
218 
219 static void
220 f_count(char *arg)
221 {
222 
223 	cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
224 	if (!cpy_cnt)
225 		terminate(0);
226 }
227 
228 static void
229 f_files(char *arg)
230 {
231 
232 	files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
233 	if (!files_cnt)
234 		terminate(0);
235 }
236 
237 static void
238 f_ibs(char *arg)
239 {
240 
241 	if (!(ddflags & C_BS))
242 		in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
243 }
244 
245 static void
246 f_if(char *arg)
247 {
248 
249 	in.name = arg;
250 }
251 
252 static void
253 f_obs(char *arg)
254 {
255 
256 	if (!(ddflags & C_BS))
257 		out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
258 }
259 
260 static void
261 f_of(char *arg)
262 {
263 
264 	out.name = arg;
265 }
266 
267 static void
268 f_seek(char *arg)
269 {
270 
271 	out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
272 }
273 
274 static void
275 f_skip(char *arg)
276 {
277 
278 	in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
279 }
280 
281 static void
282 f_progress(char *arg)
283 {
284 
285 	progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
286 }
287 
288 #ifdef	NO_CONV
289 /* Build a small version (i.e. for a ramdisk root) */
290 static void
291 f_conv(char *arg)
292 {
293 
294 	errx(EXIT_FAILURE, "conv option disabled");
295 	/* NOTREACHED */
296 }
297 #else	/* NO_CONV */
298 
299 static const struct conv {
300 	const char *name;
301 	u_int set, noset;
302 	const u_char *ctab;
303 } clist[] = {
304 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
305 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
306 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
307 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm_POSIX },
308 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
309 	{ "noerror",	C_NOERROR,	0,		NULL },
310 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
311 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
312 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
313 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm_32V },
314 	{ "osync",	C_OSYNC,	C_BS,		NULL },
315 	{ "sparse",	C_SPARSE,	0,		NULL },
316 	{ "swab",	C_SWAB,		0,		NULL },
317 	{ "sync",	C_SYNC,		0,		NULL },
318 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
319 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
320 	/* If you add items to this table, be sure to add the
321 	 * conversions to the C_BS check in the jcl routine above.
322 	 */
323 };
324 
325 static void
326 f_conv(char *arg)
327 {
328 	struct conv *cp, tmp;
329 
330 	while (arg != NULL) {
331 		tmp.name = strsep(&arg, ",");
332 		if (!(cp = bsearch(&tmp, clist,
333 		    __arraycount(clist), sizeof(*clist), c_conv))) {
334 			errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
335 			/* NOTREACHED */
336 		}
337 		if (ddflags & cp->noset) {
338 			errx(EXIT_FAILURE,
339 			    "%s: illegal conversion combination", tmp.name);
340 			/* NOTREACHED */
341 		}
342 		ddflags |= cp->set;
343 		if (cp->ctab)
344 			ctab = cp->ctab;
345 	}
346 }
347 
348 static int
349 c_conv(const void *a, const void *b)
350 {
351 
352 	return (strcmp(((const struct conv *)a)->name,
353 	    ((const struct conv *)b)->name));
354 }
355 
356 #endif	/* NO_CONV */
357