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