args.c revision 1.15 1 1.15 ross /* $NetBSD: args.c,v 1.15 2001/04/28 22:47:23 ross Exp $ */
2 1.4 cgd
3 1.1 glass /*-
4 1.3 mycroft * Copyright (c) 1991, 1993, 1994
5 1.3 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 glass *
7 1.1 glass * This code is derived from software contributed to Berkeley by
8 1.1 glass * Keith Muller of the University of California, San Diego and Lance
9 1.1 glass * Visser of Convex Computer Corporation.
10 1.1 glass *
11 1.1 glass * Redistribution and use in source and binary forms, with or without
12 1.1 glass * modification, are permitted provided that the following conditions
13 1.1 glass * are met:
14 1.1 glass * 1. Redistributions of source code must retain the above copyright
15 1.1 glass * notice, this list of conditions and the following disclaimer.
16 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 glass * notice, this list of conditions and the following disclaimer in the
18 1.1 glass * documentation and/or other materials provided with the distribution.
19 1.1 glass * 3. All advertising materials mentioning features or use of this software
20 1.1 glass * must display the following acknowledgement:
21 1.1 glass * This product includes software developed by the University of
22 1.1 glass * California, Berkeley and its contributors.
23 1.1 glass * 4. Neither the name of the University nor the names of its contributors
24 1.1 glass * may be used to endorse or promote products derived from this software
25 1.1 glass * without specific prior written permission.
26 1.1 glass *
27 1.1 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.1 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.1 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.1 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.1 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.1 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.1 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.1 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.1 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.1 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.1 glass * SUCH DAMAGE.
38 1.1 glass */
39 1.1 glass
40 1.8 christos #include <sys/cdefs.h>
41 1.1 glass #ifndef lint
42 1.4 cgd #if 0
43 1.4 cgd static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
44 1.4 cgd #else
45 1.15 ross __RCSID("$NetBSD: args.c,v 1.15 2001/04/28 22:47:23 ross Exp $");
46 1.4 cgd #endif
47 1.1 glass #endif /* not lint */
48 1.1 glass
49 1.1 glass #include <sys/types.h>
50 1.15 ross #include <sys/time.h>
51 1.3 mycroft
52 1.3 mycroft #include <err.h>
53 1.3 mycroft #include <errno.h>
54 1.1 glass #include <limits.h>
55 1.1 glass #include <stdio.h>
56 1.1 glass #include <stdlib.h>
57 1.1 glass #include <string.h>
58 1.3 mycroft
59 1.1 glass #include "dd.h"
60 1.1 glass #include "extern.h"
61 1.1 glass
62 1.3 mycroft static int c_arg __P((const void *, const void *));
63 1.3 mycroft static int c_conv __P((const void *, const void *));
64 1.3 mycroft static void f_bs __P((char *));
65 1.3 mycroft static void f_cbs __P((char *));
66 1.3 mycroft static void f_conv __P((char *));
67 1.3 mycroft static void f_count __P((char *));
68 1.3 mycroft static void f_files __P((char *));
69 1.3 mycroft static void f_ibs __P((char *));
70 1.3 mycroft static void f_if __P((char *));
71 1.3 mycroft static void f_obs __P((char *));
72 1.3 mycroft static void f_of __P((char *));
73 1.3 mycroft static void f_seek __P((char *));
74 1.3 mycroft static void f_skip __P((char *));
75 1.14 hubertf static void f_progress __P((char *));
76 1.3 mycroft static u_long get_bsz __P((char *));
77 1.1 glass
78 1.12 msaitoh static const struct arg {
79 1.1 glass char *name;
80 1.1 glass void (*f) __P((char *));
81 1.1 glass u_int set, noset;
82 1.1 glass } args[] = {
83 1.14 hubertf /* the array needs to be sorted by the first column so
84 1.14 hubertf bsearch() can be used to find commands quickly */
85 1.3 mycroft { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
86 1.3 mycroft { "cbs", f_cbs, C_CBS, C_CBS },
87 1.3 mycroft { "conv", f_conv, 0, 0 },
88 1.3 mycroft { "count", f_count, C_COUNT, C_COUNT },
89 1.3 mycroft { "files", f_files, C_FILES, C_FILES },
90 1.3 mycroft { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
91 1.3 mycroft { "if", f_if, C_IF, C_IF },
92 1.3 mycroft { "obs", f_obs, C_OBS, C_BS|C_OBS },
93 1.3 mycroft { "of", f_of, C_OF, C_OF },
94 1.14 hubertf { "progress", f_progress, 0, 0 },
95 1.3 mycroft { "seek", f_seek, C_SEEK, C_SEEK },
96 1.3 mycroft { "skip", f_skip, C_SKIP, C_SKIP },
97 1.1 glass };
98 1.1 glass
99 1.1 glass static char *oper;
100 1.1 glass
101 1.1 glass /*
102 1.1 glass * args -- parse JCL syntax of dd.
103 1.1 glass */
104 1.1 glass void
105 1.1 glass jcl(argv)
106 1.3 mycroft char **argv;
107 1.1 glass {
108 1.3 mycroft struct arg *ap, tmp;
109 1.1 glass char *arg;
110 1.1 glass
111 1.1 glass in.dbsz = out.dbsz = 512;
112 1.1 glass
113 1.8 christos while ((oper = *++argv) != NULL) {
114 1.3 mycroft if ((arg = strchr(oper, '=')) == NULL)
115 1.3 mycroft errx(1, "unknown operand %s", oper);
116 1.1 glass *arg++ = '\0';
117 1.1 glass if (!*arg)
118 1.3 mycroft errx(1, "no value specified for %s", oper);
119 1.1 glass tmp.name = oper;
120 1.1 glass if (!(ap = (struct arg *)bsearch(&tmp, args,
121 1.1 glass sizeof(args)/sizeof(struct arg), sizeof(struct arg),
122 1.1 glass c_arg)))
123 1.3 mycroft errx(1, "unknown operand %s", tmp.name);
124 1.1 glass if (ddflags & ap->noset)
125 1.3 mycroft errx(1, "%s: illegal argument combination or already set",
126 1.1 glass tmp.name);
127 1.1 glass ddflags |= ap->set;
128 1.1 glass ap->f(arg);
129 1.1 glass }
130 1.1 glass
131 1.1 glass /* Final sanity checks. */
132 1.1 glass
133 1.1 glass if (ddflags & C_BS) {
134 1.1 glass /*
135 1.1 glass * Bs is turned off by any conversion -- we assume the user
136 1.1 glass * just wanted to set both the input and output block sizes
137 1.1 glass * and didn't want the bs semantics, so we don't warn.
138 1.1 glass */
139 1.1 glass if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK))
140 1.1 glass ddflags &= ~C_BS;
141 1.1 glass
142 1.1 glass /* Bs supersedes ibs and obs. */
143 1.1 glass if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
144 1.3 mycroft warnx("bs supersedes ibs and obs");
145 1.1 glass }
146 1.1 glass
147 1.1 glass /*
148 1.1 glass * Ascii/ebcdic and cbs implies block/unblock.
149 1.1 glass * Block/unblock requires cbs and vice-versa.
150 1.1 glass */
151 1.1 glass if (ddflags & (C_BLOCK|C_UNBLOCK)) {
152 1.1 glass if (!(ddflags & C_CBS))
153 1.3 mycroft errx(1, "record operations require cbs");
154 1.1 glass if (cbsz == 0)
155 1.3 mycroft errx(1, "cbs cannot be zero");
156 1.1 glass cfunc = ddflags & C_BLOCK ? block : unblock;
157 1.1 glass } else if (ddflags & C_CBS) {
158 1.1 glass if (ddflags & (C_ASCII|C_EBCDIC)) {
159 1.1 glass if (ddflags & C_ASCII) {
160 1.1 glass ddflags |= C_UNBLOCK;
161 1.1 glass cfunc = unblock;
162 1.1 glass } else {
163 1.1 glass ddflags |= C_BLOCK;
164 1.1 glass cfunc = block;
165 1.1 glass }
166 1.1 glass } else
167 1.3 mycroft errx(1, "cbs meaningless if not doing record operations");
168 1.1 glass if (cbsz == 0)
169 1.3 mycroft errx(1, "cbs cannot be zero");
170 1.1 glass } else
171 1.1 glass cfunc = def;
172 1.1 glass
173 1.1 glass if (in.dbsz == 0 || out.dbsz == 0)
174 1.3 mycroft errx(1, "buffer sizes cannot be zero");
175 1.1 glass
176 1.1 glass /*
177 1.9 phil * Check to make sure that the buffers are not too large.
178 1.1 glass */
179 1.1 glass if (in.dbsz > INT_MAX || out.dbsz > INT_MAX)
180 1.3 mycroft errx(1, "buffer sizes cannot be greater than %d", INT_MAX);
181 1.9 phil
182 1.9 phil /* Read, write and seek calls take off_t as arguments.
183 1.9 phil *
184 1.9 phil * The following check is not done because an off_t is a quad
185 1.9 phil * for current NetBSD implementations.
186 1.9 phil *
187 1.9 phil * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
188 1.9 phil * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
189 1.9 phil */
190 1.1 glass }
191 1.1 glass
192 1.1 glass static int
193 1.1 glass c_arg(a, b)
194 1.1 glass const void *a, *b;
195 1.1 glass {
196 1.13 mycroft return (strcmp(((const struct arg *)a)->name,
197 1.13 mycroft ((const struct arg *)b)->name));
198 1.1 glass }
199 1.1 glass
200 1.1 glass static void
201 1.1 glass f_bs(arg)
202 1.1 glass char *arg;
203 1.1 glass {
204 1.1 glass in.dbsz = out.dbsz = (int)get_bsz(arg);
205 1.1 glass }
206 1.1 glass
207 1.1 glass static void
208 1.1 glass f_cbs(arg)
209 1.1 glass char *arg;
210 1.1 glass {
211 1.1 glass cbsz = (int)get_bsz(arg);
212 1.1 glass }
213 1.1 glass
214 1.1 glass static void
215 1.1 glass f_count(arg)
216 1.1 glass char *arg;
217 1.1 glass {
218 1.1 glass cpy_cnt = (u_int)get_bsz(arg);
219 1.1 glass if (!cpy_cnt)
220 1.1 glass terminate(0);
221 1.1 glass }
222 1.1 glass
223 1.1 glass static void
224 1.1 glass f_files(arg)
225 1.1 glass char *arg;
226 1.1 glass {
227 1.1 glass files_cnt = (int)get_bsz(arg);
228 1.1 glass }
229 1.1 glass
230 1.1 glass static void
231 1.1 glass f_ibs(arg)
232 1.1 glass char *arg;
233 1.1 glass {
234 1.1 glass if (!(ddflags & C_BS))
235 1.1 glass in.dbsz = (int)get_bsz(arg);
236 1.1 glass }
237 1.1 glass
238 1.1 glass static void
239 1.1 glass f_if(arg)
240 1.1 glass char *arg;
241 1.1 glass {
242 1.1 glass in.name = arg;
243 1.1 glass }
244 1.1 glass
245 1.1 glass static void
246 1.1 glass f_obs(arg)
247 1.1 glass char *arg;
248 1.1 glass {
249 1.1 glass if (!(ddflags & C_BS))
250 1.1 glass out.dbsz = (int)get_bsz(arg);
251 1.1 glass }
252 1.1 glass
253 1.1 glass static void
254 1.1 glass f_of(arg)
255 1.1 glass char *arg;
256 1.1 glass {
257 1.1 glass out.name = arg;
258 1.1 glass }
259 1.1 glass
260 1.1 glass static void
261 1.1 glass f_seek(arg)
262 1.1 glass char *arg;
263 1.1 glass {
264 1.1 glass out.offset = (u_int)get_bsz(arg);
265 1.1 glass }
266 1.1 glass
267 1.1 glass static void
268 1.1 glass f_skip(arg)
269 1.1 glass char *arg;
270 1.1 glass {
271 1.14 hubertf in.offset = (u_int)get_bsz(arg);
272 1.14 hubertf }
273 1.3 mycroft
274 1.14 hubertf static void
275 1.14 hubertf f_progress(arg)
276 1.14 hubertf char *arg;
277 1.14 hubertf {
278 1.14 hubertf if (*arg != '0')
279 1.14 hubertf progress = 1;
280 1.1 glass }
281 1.1 glass
282 1.5 gwr #ifdef NO_CONV
283 1.5 gwr /* Build a small version (i.e. for a ramdisk root) */
284 1.5 gwr static void
285 1.5 gwr f_conv(arg)
286 1.5 gwr char *arg;
287 1.5 gwr {
288 1.5 gwr errx(1, "conv option disabled");
289 1.5 gwr }
290 1.5 gwr #else /* NO_CONV */
291 1.5 gwr
292 1.12 msaitoh static const struct conv {
293 1.1 glass char *name;
294 1.1 glass u_int set, noset;
295 1.6 jtc const u_char *ctab;
296 1.1 glass } clist[] = {
297 1.3 mycroft { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
298 1.3 mycroft { "block", C_BLOCK, C_UNBLOCK, NULL },
299 1.3 mycroft { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
300 1.3 mycroft { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
301 1.3 mycroft { "lcase", C_LCASE, C_UCASE, NULL },
302 1.3 mycroft { "noerror", C_NOERROR, 0, NULL },
303 1.3 mycroft { "notrunc", C_NOTRUNC, 0, NULL },
304 1.3 mycroft { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
305 1.3 mycroft { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
306 1.3 mycroft { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
307 1.3 mycroft { "osync", C_OSYNC, C_BS, NULL },
308 1.3 mycroft { "swab", C_SWAB, 0, NULL },
309 1.3 mycroft { "sync", C_SYNC, 0, NULL },
310 1.3 mycroft { "ucase", C_UCASE, C_LCASE, NULL },
311 1.3 mycroft { "unblock", C_UNBLOCK, C_BLOCK, NULL },
312 1.1 glass };
313 1.1 glass
314 1.1 glass static void
315 1.1 glass f_conv(arg)
316 1.1 glass char *arg;
317 1.1 glass {
318 1.3 mycroft struct conv *cp, tmp;
319 1.1 glass
320 1.1 glass while (arg != NULL) {
321 1.1 glass tmp.name = strsep(&arg, ",");
322 1.1 glass if (!(cp = (struct conv *)bsearch(&tmp, clist,
323 1.1 glass sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
324 1.1 glass c_conv)))
325 1.3 mycroft errx(1, "unknown conversion %s", tmp.name);
326 1.1 glass if (ddflags & cp->noset)
327 1.3 mycroft errx(1, "%s: illegal conversion combination", tmp.name);
328 1.1 glass ddflags |= cp->set;
329 1.1 glass if (cp->ctab)
330 1.1 glass ctab = cp->ctab;
331 1.1 glass }
332 1.1 glass }
333 1.1 glass
334 1.1 glass static int
335 1.1 glass c_conv(a, b)
336 1.1 glass const void *a, *b;
337 1.1 glass {
338 1.3 mycroft
339 1.13 mycroft return (strcmp(((const struct conv *)a)->name,
340 1.13 mycroft ((const struct conv *)b)->name));
341 1.1 glass }
342 1.5 gwr
343 1.5 gwr #endif /* NO_CONV */
344 1.1 glass
345 1.1 glass /*
346 1.1 glass * Convert an expression of the following forms to an unsigned long.
347 1.1 glass * 1) A positive decimal number.
348 1.1 glass * 2) A positive decimal number followed by a b (mult by 512).
349 1.1 glass * 3) A positive decimal number followed by a k (mult by 1024).
350 1.1 glass * 4) A positive decimal number followed by a m (mult by 512).
351 1.1 glass * 5) A positive decimal number followed by a w (mult by sizeof int)
352 1.1 glass * 6) Two or more positive decimal numbers (with/without k,b or w).
353 1.1 glass * seperated by x (also * for backwards compatibility), specifying
354 1.1 glass * the product of the indicated values.
355 1.1 glass */
356 1.1 glass static u_long
357 1.1 glass get_bsz(val)
358 1.1 glass char *val;
359 1.1 glass {
360 1.3 mycroft u_long num, t;
361 1.1 glass char *expr;
362 1.1 glass
363 1.1 glass num = strtoul(val, &expr, 0);
364 1.1 glass if (num == ULONG_MAX) /* Overflow. */
365 1.3 mycroft err(1, "%s", oper);
366 1.1 glass if (expr == val) /* No digits. */
367 1.3 mycroft errx(1, "%s: illegal numeric value", oper);
368 1.1 glass
369 1.10 enami switch (*expr) {
370 1.1 glass case 'b':
371 1.1 glass t = num;
372 1.1 glass num *= 512;
373 1.1 glass if (t > num)
374 1.1 glass goto erange;
375 1.1 glass ++expr;
376 1.1 glass break;
377 1.1 glass case 'k':
378 1.1 glass t = num;
379 1.1 glass num *= 1024;
380 1.1 glass if (t > num)
381 1.1 glass goto erange;
382 1.1 glass ++expr;
383 1.1 glass break;
384 1.1 glass case 'm':
385 1.1 glass t = num;
386 1.1 glass num *= 1048576;
387 1.1 glass if (t > num)
388 1.1 glass goto erange;
389 1.1 glass ++expr;
390 1.1 glass break;
391 1.1 glass case 'w':
392 1.1 glass t = num;
393 1.1 glass num *= sizeof(int);
394 1.1 glass if (t > num)
395 1.1 glass goto erange;
396 1.1 glass ++expr;
397 1.1 glass break;
398 1.1 glass }
399 1.1 glass
400 1.10 enami switch (*expr) {
401 1.11 enami case '\0':
402 1.11 enami break;
403 1.11 enami case '*': /* Backward compatible. */
404 1.11 enami case 'x':
405 1.11 enami t = num;
406 1.11 enami num *= get_bsz(expr + 1);
407 1.11 enami if (t > num)
408 1.11 enami erange: errx(1, "%s: %s", oper, strerror(ERANGE));
409 1.11 enami break;
410 1.11 enami default:
411 1.11 enami errx(1, "%s: illegal numeric value", oper);
412 1.1 glass }
413 1.3 mycroft return (num);
414 1.1 glass }
415