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