args.c revision 1.22 1 /* $NetBSD: args.c,v 1.22 2003/08/04 22:31:23 jschauma 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.22 2003/08/04 22:31:23 jschauma Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/time.h>
51
52 #include <err.h>
53 #include <errno.h>
54 #include <limits.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include "dd.h"
60 #include "extern.h"
61
62 static int c_arg(const void *, const void *);
63 #ifndef NO_CONV
64 static int c_conv(const void *, const void *);
65 #endif
66 static void f_bs(char *);
67 static void f_cbs(char *);
68 static void f_conv(char *);
69 static void f_count(char *);
70 static void f_files(char *);
71 static void f_ibs(char *);
72 static void f_if(char *);
73 static void f_obs(char *);
74 static void f_of(char *);
75 static void f_seek(char *);
76 static void f_skip(char *);
77 static void f_progress(char *);
78
79 static const struct arg {
80 const char *name;
81 void (*f)(char *);
82 u_int set, noset;
83 } args[] = {
84 /* the array needs to be sorted by the first column so
85 bsearch() can be used to find commands quickly */
86 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
87 { "cbs", f_cbs, C_CBS, C_CBS },
88 { "conv", f_conv, 0, 0 },
89 { "count", f_count, C_COUNT, C_COUNT },
90 { "files", f_files, C_FILES, C_FILES },
91 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
92 { "if", f_if, C_IF, C_IF },
93 { "obs", f_obs, C_OBS, C_BS|C_OBS },
94 { "of", f_of, C_OF, C_OF },
95 { "progress", f_progress, 0, 0 },
96 { "seek", f_seek, C_SEEK, C_SEEK },
97 { "skip", f_skip, C_SKIP, C_SKIP },
98 };
99
100 /*
101 * args -- parse JCL syntax of dd.
102 */
103 void
104 jcl(char **argv)
105 {
106 struct arg *ap, tmp;
107 char *oper, *arg;
108
109 in.dbsz = out.dbsz = 512;
110
111 while ((oper = *++argv) != NULL) {
112 if ((arg = strchr(oper, '=')) == NULL) {
113 errx(EXIT_FAILURE, "unknown operand %s", oper);
114 /* NOTREACHED */
115 }
116 *arg++ = '\0';
117 if (!*arg) {
118 errx(EXIT_FAILURE, "no value specified for %s", oper);
119 /* NOTREACHED */
120 }
121 tmp.name = oper;
122 if (!(ap = (struct arg *)bsearch(&tmp, args,
123 sizeof(args)/sizeof(struct arg), sizeof(struct arg),
124 c_arg))) {
125 errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
126 /* NOTREACHED */
127 }
128 if (ddflags & ap->noset) {
129 errx(EXIT_FAILURE,
130 "%s: illegal argument combination or already set",
131 tmp.name);
132 /* NOTREACHED */
133 }
134 ddflags |= ap->set;
135 ap->f(arg);
136 }
137
138 /* Final sanity checks. */
139
140 if (ddflags & C_BS) {
141 /*
142 * Bs is turned off by any conversion -- we assume the user
143 * just wanted to set both the input and output block sizes
144 * and didn't want the bs semantics, so we don't warn.
145 */
146 if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
147 ddflags &= ~C_BS;
148
149 /* Bs supersedes ibs and obs. */
150 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
151 warnx("bs supersedes ibs and obs");
152 }
153
154 /*
155 * Ascii/ebcdic and cbs implies block/unblock.
156 * Block/unblock requires cbs and vice-versa.
157 */
158 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
159 if (!(ddflags & C_CBS)) {
160 errx(EXIT_FAILURE, "record operations require cbs");
161 /* NOTREACHED */
162 }
163 cfunc = ddflags & C_BLOCK ? block : unblock;
164 } else if (ddflags & C_CBS) {
165 if (ddflags & (C_ASCII|C_EBCDIC)) {
166 if (ddflags & C_ASCII) {
167 ddflags |= C_UNBLOCK;
168 cfunc = unblock;
169 } else {
170 ddflags |= C_BLOCK;
171 cfunc = block;
172 }
173 } else {
174 errx(EXIT_FAILURE,
175 "cbs meaningless if not doing record operations");
176 /* NOTREACHED */
177 }
178 } else
179 cfunc = def;
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(const void *a, const void *b)
193 {
194
195 return (strcmp(((const struct arg *)a)->name,
196 ((const struct arg *)b)->name));
197 }
198
199 static void
200 f_bs(char *arg)
201 {
202
203 in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
204 }
205
206 static void
207 f_cbs(char *arg)
208 {
209
210 cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
211 }
212
213 static void
214 f_count(char *arg)
215 {
216
217 cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
218 if (!cpy_cnt)
219 terminate(0);
220 }
221
222 static void
223 f_files(char *arg)
224 {
225
226 files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
227 if (!files_cnt)
228 terminate(0);
229 }
230
231 static void
232 f_ibs(char *arg)
233 {
234
235 if (!(ddflags & C_BS))
236 in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
237 }
238
239 static void
240 f_if(char *arg)
241 {
242
243 in.name = arg;
244 }
245
246 static void
247 f_obs(char *arg)
248 {
249
250 if (!(ddflags & C_BS))
251 out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
252 }
253
254 static void
255 f_of(char *arg)
256 {
257
258 out.name = arg;
259 }
260
261 static void
262 f_seek(char *arg)
263 {
264
265 out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
266 }
267
268 static void
269 f_skip(char *arg)
270 {
271
272 in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
273 }
274
275 static void
276 f_progress(char *arg)
277 {
278
279 if (*arg != '0')
280 progress = 1;
281 }
282
283 #ifdef NO_CONV
284 /* Build a small version (i.e. for a ramdisk root) */
285 static void
286 f_conv(char *arg)
287 {
288
289 errx(EXIT_FAILURE, "conv option disabled");
290 /* NOTREACHED */
291 }
292 #else /* NO_CONV */
293
294 static const struct conv {
295 const char *name;
296 u_int set, noset;
297 const u_char *ctab;
298 } clist[] = {
299 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
300 { "block", C_BLOCK, C_UNBLOCK, NULL },
301 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
302 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
303 { "lcase", C_LCASE, C_UCASE, NULL },
304 { "noerror", C_NOERROR, 0, NULL },
305 { "notrunc", C_NOTRUNC, 0, NULL },
306 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
307 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
308 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
309 { "osync", C_OSYNC, C_BS, 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 };
315
316 static void
317 f_conv(char *arg)
318 {
319 struct conv *cp, tmp;
320
321 while (arg != NULL) {
322 tmp.name = strsep(&arg, ",");
323 if (!(cp = (struct conv *)bsearch(&tmp, clist,
324 sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
325 c_conv))) {
326 errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
327 /* NOTREACHED */
328 }
329 if (ddflags & cp->noset) {
330 errx(EXIT_FAILURE, "%s: illegal conversion combination", tmp.name);
331 /* NOTREACHED */
332 }
333 ddflags |= cp->set;
334 if (cp->ctab)
335 ctab = cp->ctab;
336 }
337 }
338
339 static int
340 c_conv(const void *a, const void *b)
341 {
342
343 return (strcmp(((const struct conv *)a)->name,
344 ((const struct conv *)b)->name));
345 }
346
347 #endif /* NO_CONV */
348