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 40 static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94"; 41 #endif 42 static const char rcsid[] = 43 "$FreeBSD$"; 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 57 static int c_arg __P((const void *, const void *)); 58 static int c_conv __P((const void *, const void *)); 59 static void f_bs __P((char *)); 60 static void f_cbs __P((char *)); 61 static void f_conv __P((char *)); 62 static void f_count __P((char *)); 63 static void f_files __P((char *)); 64 static void f_ibs __P((char *)); 65 static void f_if __P((char *)); 66 static void f_obs __P((char *)); 67 static void f_of __P((char *)); 68 static void f_seek __P((char *)); 69 static void f_skip __P((char *)); 70 static quad_t get_bsz __P((char *)); 71 72 static struct arg { 73 char *name; 74 void (*f) __P((char *)); 75 u_int set, noset; 76 } args[] = { 77 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC }, 78 { "cbs", f_cbs, C_CBS, C_CBS }, 79 { "conv", f_conv, 0, 0 }, 80 { "count", f_count, C_COUNT, C_COUNT }, 81 { "files", f_files, C_FILES, C_FILES }, 82 { "ibs", f_ibs, C_IBS, C_BS|C_IBS }, 83 { "if", f_if, C_IF, C_IF }, 84 { "obs", f_obs, C_OBS, C_BS|C_OBS }, 85 { "of", f_of, C_OF, C_OF }, 86 { "seek", f_seek, C_SEEK, C_SEEK }, 87 { "skip", f_skip, C_SKIP, C_SKIP }, 88 }; 89 90 static char *oper; 91 92 /* 93 * args -- parse JCL syntax of dd. 94 */ 95 void 96 jcl(argv) 97 char **argv; 98 { 99 struct arg *ap, tmp; 100 char *arg; 101 102 in.dbsz = out.dbsz = 512; 103 104 while ((oper = *++argv) != NULL) { 105 if ((oper = strdup(oper)) == NULL) 106 errx(1, "unable to allocate space for the argument \"%s\"", *argv); 107 if ((arg = strchr(oper, '=')) == NULL) 108 errx(1, "unknown operand %s", oper); 109 *arg++ = '\0'; 110 if (!*arg) 111 errx(1, "no value specified for %s", oper); 112 tmp.name = oper; 113 if (!(ap = (struct arg *)bsearch(&tmp, args, 114 sizeof(args)/sizeof(struct arg), sizeof(struct arg), 115 c_arg))) 116 errx(1, "unknown operand %s", tmp.name); 117 if (ddflags & ap->noset) 118 errx(1, "%s: illegal argument combination or already set", 119 tmp.name); 120 ddflags |= ap->set; 121 ap->f(arg); 122 } 123 124 /* Final sanity checks. */ 125 126 if (ddflags & C_BS) { 127 /* 128 * Bs is turned off by any conversion -- we assume the user 129 * just wanted to set both the input and output block sizes 130 * and didn't want the bs semantics, so we don't warn. 131 */ 132 if (ddflags & (C_BLOCK|C_LCASE|C_SWAB|C_UCASE|C_UNBLOCK)) 133 ddflags &= ~C_BS; 134 135 /* Bs supersedes ibs and obs. */ 136 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS)) 137 warnx("bs supersedes ibs and obs"); 138 } 139 140 /* 141 * Ascii/ebcdic and cbs implies block/unblock. 142 * Block/unblock requires cbs and vice-versa. 143 */ 144 if (ddflags & (C_BLOCK|C_UNBLOCK)) { 145 if (!(ddflags & C_CBS)) 146 errx(1, "record operations require cbs"); 147 if (cbsz == 0) 148 errx(1, "cbs cannot be zero"); 149 cfunc = ddflags & C_BLOCK ? block : unblock; 150 } else if (ddflags & C_CBS) { 151 if (ddflags & (C_ASCII|C_EBCDIC)) { 152 if (ddflags & C_ASCII) { 153 ddflags |= C_UNBLOCK; 154 cfunc = unblock; 155 } else { 156 ddflags |= C_BLOCK; 157 cfunc = block; 158 } 159 } else 160 errx(1, "cbs meaningless if not doing record operations"); 161 } else 162 cfunc = def; 163 } 164 165 static int 166 c_arg(a, b) 167 const void *a, *b; 168 { 169 170 return (strcmp(((struct arg *)a)->name, ((struct arg *)b)->name)); 171 } 172 173 static void 174 f_bs(arg) 175 char *arg; 176 { 177 quad_t res = get_bsz(arg); 178 179 if (res < 1 || res > INT_MAX) 180 errx(1, "bs must be between 1 and %d", INT_MAX); 181 in.dbsz = out.dbsz = (int)res; 182 } 183 184 static void 185 f_cbs(arg) 186 char *arg; 187 { 188 quad_t res = get_bsz(arg); 189 190 if (res < 1 || res > INT_MAX) 191 errx(1, "cbs must be between 1 and %d", INT_MAX); 192 193 cbsz = (int)res; 194 } 195 196 static void 197 f_count(arg) 198 char *arg; 199 { 200 201 cpy_cnt = get_bsz(arg); 202 if (!cpy_cnt) 203 terminate(0); 204 if (cpy_cnt < 0) 205 errx(1, "count cannot be negative"); 206 } 207 208 static void 209 f_files(arg) 210 char *arg; 211 { 212 quad_t res = get_bsz(arg); 213 214 files_cnt = res; 215 } 216 217 static void 218 f_ibs(arg) 219 char *arg; 220 { 221 222 if (!(ddflags & C_BS)) { 223 quad_t res = get_bsz(arg); 224 225 if (res < 1 || res > INT_MAX) 226 errx(1, "ibs must be between 1 and %d", INT_MAX); 227 in.dbsz = (int)res; 228 } 229 } 230 231 static void 232 f_if(arg) 233 char *arg; 234 { 235 236 in.name = arg; 237 } 238 239 static void 240 f_obs(arg) 241 char *arg; 242 { 243 244 if (!(ddflags & C_BS)) { 245 quad_t res = get_bsz(arg); 246 247 if (res < 1 || res > INT_MAX) 248 errx(1, "ibs must be between 1 and %d", INT_MAX); 249 out.dbsz = (int)res; 250 } 251 } 252 253 static void 254 f_of(arg) 255 char *arg; 256 { 257 258 out.name = arg; 259 } 260 261 static void 262 f_seek(arg) 263 char *arg; 264 { 265 266 out.offset = get_bsz(arg); 267 } 268 269 static void 270 f_skip(arg) 271 char *arg; 272 { 273 274 in.offset = get_bsz(arg); 275 } 276 277 static struct conv { 278 char *name; 279 u_int set, noset; 280 u_char *ctab; 281 } clist[] = { 282 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX }, 283 { "block", C_BLOCK, C_UNBLOCK, NULL }, 284 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX }, 285 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX }, 286 { "lcase", C_LCASE, C_UCASE, NULL }, 287 { "noerror", C_NOERROR, 0, NULL }, 288 { "notrunc", C_NOTRUNC, 0, NULL }, 289 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V }, 290 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V }, 291 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V }, 292 { "osync", C_OSYNC, C_BS, NULL }, 293 { "sparse", C_SPARSE, 0, NULL }, 294 { "swab", C_SWAB, 0, NULL }, 295 { "sync", C_SYNC, 0, NULL }, 296 { "ucase", C_UCASE, C_LCASE, NULL }, 297 { "unblock", C_UNBLOCK, C_BLOCK, NULL }, 298 }; 299 300 static void 301 f_conv(arg) 302 char *arg; 303 { 304 struct conv *cp, tmp; 305 306 while (arg != NULL) { 307 tmp.name = strsep(&arg, ","); 308 if (!(cp = (struct conv *)bsearch(&tmp, clist, 309 sizeof(clist)/sizeof(struct conv), sizeof(struct conv), 310 c_conv))) 311 errx(1, "unknown conversion %s", tmp.name); 312 if (ddflags & cp->noset) 313 errx(1, "%s: illegal conversion combination", tmp.name); 314 ddflags |= cp->set; 315 if (cp->ctab) 316 ctab = cp->ctab; 317 } 318 } 319 320 static int 321 c_conv(a, b) 322 const void *a, *b; 323 { 324 325 return (strcmp(((struct conv *)a)->name, ((struct conv *)b)->name)); 326 } 327 328 /* 329 * Convert an expression of the following forms to a quad_t. 330 * 1) A positive decimal number. 331 * 2) A positive decimal number followed by a b (mult by 512.) 332 * 3) A positive decimal number followed by a k (mult by 1 << 10.) 333 * 4) A positive decimal number followed by a m (mult by 1 << 20.) 334 * 5) A positive decimal number followed by a g (mult by 1 << 30.) 335 * 5) A positive decimal number followed by a w (mult by sizeof int.) 336 * 6) Two or more positive decimal numbers (with/without [bkmgw]) 337 * separated by x (also * for backwards compatibility), specifying 338 * the product of the indicated values. 339 */ 340 static quad_t 341 get_bsz(val) 342 char *val; 343 { 344 quad_t num, t; 345 char *expr; 346 347 errno = 0; 348 num = strtoq(val, &expr, 0); 349 if (num == QUAD_MAX && errno) /* Overflow. */ 350 err(1, "%s", oper); 351 /* 352 * XXX (BFF) - The checks in individual f_* functions are 353 * now redundant, but this is only temporary. 354 */ 355 356 if (expr == val || num < 0) /* No digits or negative. */ 357 errx(1, "%s: illegal numeric value", oper); 358 359 switch(*expr) { 360 case 'b': 361 t = num; 362 num *= 512; 363 if (t > num) 364 goto erange; 365 ++expr; 366 break; 367 case 'k': 368 t = num; 369 num *= 1 << 10; 370 if (t > num) 371 goto erange; 372 ++expr; 373 break; 374 case 'm': 375 t = num; 376 num *= 1 << 20; 377 if (t > num) 378 goto erange; 379 ++expr; 380 break; 381 case 'g': 382 t = num; 383 num *= 1 << 30; 384 if (t > num) 385 goto erange; 386 ++expr; 387 break; 388 case 'w': 389 t = num; 390 num *= sizeof(int); 391 if (t > num) 392 goto erange; 393 ++expr; 394 break; 395 } 396 397 switch(*expr) { 398 case '\0': 399 break; 400 case '*': /* Backward compatible. */ 401 case 'x': 402 t = num; 403 num *= get_bsz(expr + 1); 404 if (t > num) 405 erange: errx(1, "%s: %s", oper, strerror(ERANGE)); 406 break; 407 default: 408 errx(1, "%s: illegal numeric value", oper); 409 } 410 return (num); 411 } 412