1 /*- 2 * Copyright (c) 2003-2008 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 /* 27 * Command line parser for tar. 28 */ 29 30 #include "bsdtar_platform.h" 31 __FBSDID("$FreeBSD$"); 32 33 #ifdef HAVE_ERRNO_H 34 #include <errno.h> 35 #endif 36 #ifdef HAVE_STDLIB_H 37 #include <stdlib.h> 38 #endif 39 #ifdef HAVE_STRING_H 40 #include <string.h> 41 #endif 42 43 #include "bsdtar.h" 44 #include "err.h" 45 46 /* 47 * Short options for tar. Please keep this sorted. 48 */ 49 static const char *short_options 50 = "aBb:C:cf:HhI:JjkLlmnOoPpqrSs:T:tUuvW:wX:xyZz"; 51 52 /* 53 * Long options for tar. Please keep this list sorted. 54 * 55 * The symbolic names for options that lack a short equivalent are 56 * defined in bsdtar.h. Also note that so far I've found no need 57 * to support optional arguments to long options. That would be 58 * a small change to the code below. 59 */ 60 61 static const struct bsdtar_option { 62 const char *name; 63 int required; /* 1 if this option requires an argument. */ 64 int equivalent; /* Equivalent short option. */ 65 } tar_longopts[] = { 66 { "absolute-paths", 0, 'P' }, 67 { "append", 0, 'r' }, 68 { "acls", 0, OPTION_ACLS }, 69 { "auto-compress", 0, 'a' }, 70 { "b64encode", 0, OPTION_B64ENCODE }, 71 { "block-size", 1, 'b' }, 72 { "blocking-factor", 1, 'b' }, 73 { "bunzip2", 0, 'j' }, 74 { "bzip", 0, 'j' }, 75 { "bzip2", 0, 'j' }, 76 { "cd", 1, 'C' }, 77 { "check-links", 0, OPTION_CHECK_LINKS }, 78 { "chroot", 0, OPTION_CHROOT }, 79 { "clear-nochange-fflags", 0, OPTION_CLEAR_NOCHANGE_FFLAGS }, 80 { "compress", 0, 'Z' }, 81 { "confirmation", 0, 'w' }, 82 { "create", 0, 'c' }, 83 { "dereference", 0, 'L' }, 84 { "directory", 1, 'C' }, 85 { "disable-copyfile", 0, OPTION_NO_MAC_METADATA }, 86 { "exclude", 1, OPTION_EXCLUDE }, 87 { "exclude-from", 1, 'X' }, 88 { "exclude-vcs", 0, OPTION_EXCLUDE_VCS }, 89 { "extract", 0, 'x' }, 90 { "fast-read", 0, 'q' }, 91 { "fflags", 0, OPTION_FFLAGS }, 92 { "file", 1, 'f' }, 93 { "files-from", 1, 'T' }, 94 { "format", 1, OPTION_FORMAT }, 95 { "gid", 1, OPTION_GID }, 96 { "gname", 1, OPTION_GNAME }, 97 { "grzip", 0, OPTION_GRZIP }, 98 { "gunzip", 0, 'z' }, 99 { "gzip", 0, 'z' }, 100 { "help", 0, OPTION_HELP }, 101 { "hfsCompression", 0, OPTION_HFS_COMPRESSION }, 102 { "ignore-zeros", 0, OPTION_IGNORE_ZEROS }, 103 { "include", 1, OPTION_INCLUDE }, 104 { "insecure", 0, 'P' }, 105 { "interactive", 0, 'w' }, 106 { "keep-newer-files", 0, OPTION_KEEP_NEWER_FILES }, 107 { "keep-old-files", 0, 'k' }, 108 { "list", 0, 't' }, 109 { "lrzip", 0, OPTION_LRZIP }, 110 { "lz4", 0, OPTION_LZ4 }, 111 { "lzip", 0, OPTION_LZIP }, 112 { "lzma", 0, OPTION_LZMA }, 113 { "lzop", 0, OPTION_LZOP }, 114 { "mac-metadata", 0, OPTION_MAC_METADATA }, 115 { "modification-time", 0, 'm' }, 116 { "newer", 1, OPTION_NEWER_CTIME }, 117 { "newer-ctime", 1, OPTION_NEWER_CTIME }, 118 { "newer-ctime-than", 1, OPTION_NEWER_CTIME_THAN }, 119 { "newer-mtime", 1, OPTION_NEWER_MTIME }, 120 { "newer-mtime-than", 1, OPTION_NEWER_MTIME_THAN }, 121 { "newer-than", 1, OPTION_NEWER_CTIME_THAN }, 122 { "no-acls", 0, OPTION_NO_ACLS }, 123 { "no-fflags", 0, OPTION_NO_FFLAGS }, 124 { "no-mac-metadata", 0, OPTION_NO_MAC_METADATA }, 125 { "no-recursion", 0, 'n' }, 126 { "no-same-owner", 0, OPTION_NO_SAME_OWNER }, 127 { "no-same-permissions", 0, OPTION_NO_SAME_PERMISSIONS }, 128 { "no-xattr", 0, OPTION_NO_XATTRS }, 129 { "no-xattrs", 0, OPTION_NO_XATTRS }, 130 { "nodump", 0, OPTION_NODUMP }, 131 { "nopreserveHFSCompression",0, OPTION_NOPRESERVE_HFS_COMPRESSION }, 132 { "norecurse", 0, 'n' }, 133 { "null", 0, OPTION_NULL }, 134 { "numeric-owner", 0, OPTION_NUMERIC_OWNER }, 135 { "older", 1, OPTION_OLDER_CTIME }, 136 { "older-ctime", 1, OPTION_OLDER_CTIME }, 137 { "older-ctime-than", 1, OPTION_OLDER_CTIME_THAN }, 138 { "older-mtime", 1, OPTION_OLDER_MTIME }, 139 { "older-mtime-than", 1, OPTION_OLDER_MTIME_THAN }, 140 { "older-than", 1, OPTION_OLDER_CTIME_THAN }, 141 { "one-file-system", 0, OPTION_ONE_FILE_SYSTEM }, 142 { "options", 1, OPTION_OPTIONS }, 143 { "passphrase", 1, OPTION_PASSPHRASE }, 144 { "posix", 0, OPTION_POSIX }, 145 { "preserve-permissions", 0, 'p' }, 146 { "read-full-blocks", 0, 'B' }, 147 { "same-owner", 0, OPTION_SAME_OWNER }, 148 { "same-permissions", 0, 'p' }, 149 { "strip-components", 1, OPTION_STRIP_COMPONENTS }, 150 { "to-stdout", 0, 'O' }, 151 { "totals", 0, OPTION_TOTALS }, 152 { "uid", 1, OPTION_UID }, 153 { "uname", 1, OPTION_UNAME }, 154 { "uncompress", 0, 'Z' }, 155 { "unlink", 0, 'U' }, 156 { "unlink-first", 0, 'U' }, 157 { "update", 0, 'u' }, 158 { "use-compress-program", 1, OPTION_USE_COMPRESS_PROGRAM }, 159 { "uuencode", 0, OPTION_UUENCODE }, 160 { "verbose", 0, 'v' }, 161 { "version", 0, OPTION_VERSION }, 162 { "xattrs", 0, OPTION_XATTRS }, 163 { "xz", 0, 'J' }, 164 { "zstd", 0, OPTION_ZSTD }, 165 { NULL, 0, 0 } 166 }; 167 168 /* 169 * This getopt implementation has two key features that common 170 * getopt_long() implementations lack. Apart from those, it's a 171 * straightforward option parser, considerably simplified by not 172 * needing to support the wealth of exotic getopt_long() features. It 173 * has, of course, been shamelessly tailored for bsdtar. (If you're 174 * looking for a generic getopt_long() implementation for your 175 * project, I recommend Gregory Pietsch's public domain getopt_long() 176 * implementation.) The two additional features are: 177 * 178 * Old-style tar arguments: The original tar implementation treated 179 * the first argument word as a list of single-character option 180 * letters. All arguments follow as separate words. For example, 181 * tar xbf 32 /dev/tape 182 * Here, the "xbf" is three option letters, "32" is the argument for 183 * "b" and "/dev/tape" is the argument for "f". We support this usage 184 * if the first command-line argument does not begin with '-'. We 185 * also allow regular short and long options to follow, e.g., 186 * tar xbf 32 /dev/tape -P --format=pax 187 * 188 * -W long options: There's an obscure GNU convention (only rarely 189 * supported even there) that allows "-W option=argument" as an 190 * alternative way to support long options. This was supported in 191 * early bsdtar as a way to access long options on platforms that did 192 * not support getopt_long() and is preserved here for backwards 193 * compatibility. (Of course, if I'd started with a custom 194 * command-line parser from the beginning, I would have had normal 195 * long option support on every platform so that hack wouldn't have 196 * been necessary. Oh, well. Some mistakes you just have to live 197 * with.) 198 * 199 * TODO: We should be able to use this to pull files and intermingled 200 * options (such as -C) from the command line in write mode. That 201 * will require a little rethinking of the argument handling in 202 * bsdtar.c. 203 * 204 * TODO: If we want to support arbitrary command-line options from -T 205 * input (as GNU tar does), we may need to extend this to handle option 206 * words from sources other than argv/argc. I'm not really sure if I 207 * like that feature of GNU tar, so it's certainly not a priority. 208 */ 209 210 int 211 bsdtar_getopt(struct bsdtar *bsdtar) 212 { 213 enum { state_start = 0, state_old_tar, state_next_word, 214 state_short, state_long }; 215 216 const struct bsdtar_option *popt, *match = NULL, *match2 = NULL; 217 const char *p, *long_prefix = "--"; 218 size_t optlength; 219 int opt = '?'; 220 int required = 0; 221 222 bsdtar->argument = NULL; 223 224 /* First time through, initialize everything. */ 225 if (bsdtar->getopt_state == state_start) { 226 /* Skip program name. */ 227 ++bsdtar->argv; 228 --bsdtar->argc; 229 if (*bsdtar->argv == NULL) 230 return (-1); 231 /* Decide between "new style" and "old style" arguments. */ 232 if (bsdtar->argv[0][0] == '-') { 233 bsdtar->getopt_state = state_next_word; 234 } else { 235 bsdtar->getopt_state = state_old_tar; 236 bsdtar->getopt_word = *bsdtar->argv++; 237 --bsdtar->argc; 238 } 239 } 240 241 /* 242 * We're parsing old-style tar arguments 243 */ 244 if (bsdtar->getopt_state == state_old_tar) { 245 /* Get the next option character. */ 246 opt = *bsdtar->getopt_word++; 247 if (opt == '\0') { 248 /* New-style args can follow old-style. */ 249 bsdtar->getopt_state = state_next_word; 250 } else { 251 /* See if it takes an argument. */ 252 p = strchr(short_options, opt); 253 if (p == NULL) 254 return ('?'); 255 if (p[1] == ':') { 256 bsdtar->argument = *bsdtar->argv; 257 if (bsdtar->argument == NULL) { 258 lafe_warnc(0, 259 "Option %c requires an argument", 260 opt); 261 return ('?'); 262 } 263 ++bsdtar->argv; 264 --bsdtar->argc; 265 } 266 } 267 } 268 269 /* 270 * We're ready to look at the next word in argv. 271 */ 272 if (bsdtar->getopt_state == state_next_word) { 273 /* No more arguments, so no more options. */ 274 if (bsdtar->argv[0] == NULL) 275 return (-1); 276 /* Doesn't start with '-', so no more options. */ 277 if (bsdtar->argv[0][0] != '-') 278 return (-1); 279 /* "--" marks end of options; consume it and return. */ 280 if (strcmp(bsdtar->argv[0], "--") == 0) { 281 ++bsdtar->argv; 282 --bsdtar->argc; 283 return (-1); 284 } 285 /* Get next word for parsing. */ 286 bsdtar->getopt_word = *bsdtar->argv++; 287 --bsdtar->argc; 288 if (bsdtar->getopt_word[1] == '-') { 289 /* Set up long option parser. */ 290 bsdtar->getopt_state = state_long; 291 bsdtar->getopt_word += 2; /* Skip leading '--' */ 292 } else { 293 /* Set up short option parser. */ 294 bsdtar->getopt_state = state_short; 295 ++bsdtar->getopt_word; /* Skip leading '-' */ 296 } 297 } 298 299 /* 300 * We're parsing a group of POSIX-style single-character options. 301 */ 302 if (bsdtar->getopt_state == state_short) { 303 /* Peel next option off of a group of short options. */ 304 opt = *bsdtar->getopt_word++; 305 if (opt == '\0') { 306 /* End of this group; recurse to get next option. */ 307 bsdtar->getopt_state = state_next_word; 308 return bsdtar_getopt(bsdtar); 309 } 310 311 /* Does this option take an argument? */ 312 p = strchr(short_options, opt); 313 if (p == NULL) 314 return ('?'); 315 if (p[1] == ':') 316 required = 1; 317 318 /* If it takes an argument, parse that. */ 319 if (required) { 320 /* If arg is run-in, bsdtar->getopt_word already points to it. */ 321 if (bsdtar->getopt_word[0] == '\0') { 322 /* Otherwise, pick up the next word. */ 323 bsdtar->getopt_word = *bsdtar->argv; 324 if (bsdtar->getopt_word == NULL) { 325 lafe_warnc(0, 326 "Option -%c requires an argument", 327 opt); 328 return ('?'); 329 } 330 ++bsdtar->argv; 331 --bsdtar->argc; 332 } 333 if (opt == 'W') { 334 bsdtar->getopt_state = state_long; 335 long_prefix = "-W "; /* For clearer errors. */ 336 } else { 337 bsdtar->getopt_state = state_next_word; 338 bsdtar->argument = bsdtar->getopt_word; 339 } 340 } 341 } 342 343 /* We're reading a long option, including -W long=arg convention. */ 344 if (bsdtar->getopt_state == state_long) { 345 /* After this long option, we'll be starting a new word. */ 346 bsdtar->getopt_state = state_next_word; 347 348 /* Option name ends at '=' if there is one. */ 349 p = strchr(bsdtar->getopt_word, '='); 350 if (p != NULL) { 351 optlength = (size_t)(p - bsdtar->getopt_word); 352 bsdtar->argument = (char *)(uintptr_t)(p + 1); 353 } else { 354 optlength = strlen(bsdtar->getopt_word); 355 } 356 357 /* Search the table for an unambiguous match. */ 358 for (popt = tar_longopts; popt->name != NULL; popt++) { 359 /* Short-circuit if first chars don't match. */ 360 if (popt->name[0] != bsdtar->getopt_word[0]) 361 continue; 362 /* If option is a prefix of name in table, record it.*/ 363 if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) { 364 match2 = match; /* Record up to two matches. */ 365 match = popt; 366 /* If it's an exact match, we're done. */ 367 if (strlen(popt->name) == optlength) { 368 match2 = NULL; /* Forget the others. */ 369 break; 370 } 371 } 372 } 373 374 /* Fail if there wasn't a unique match. */ 375 if (match == NULL) { 376 lafe_warnc(0, 377 "Option %s%s is not supported", 378 long_prefix, bsdtar->getopt_word); 379 return ('?'); 380 } 381 if (match2 != NULL) { 382 lafe_warnc(0, 383 "Ambiguous option %s%s (matches --%s and --%s)", 384 long_prefix, bsdtar->getopt_word, match->name, match2->name); 385 return ('?'); 386 } 387 388 /* We've found a unique match; does it need an argument? */ 389 if (match->required) { 390 /* Argument required: get next word if necessary. */ 391 if (bsdtar->argument == NULL) { 392 bsdtar->argument = *bsdtar->argv; 393 if (bsdtar->argument == NULL) { 394 lafe_warnc(0, 395 "Option %s%s requires an argument", 396 long_prefix, match->name); 397 return ('?'); 398 } 399 ++bsdtar->argv; 400 --bsdtar->argc; 401 } 402 } else { 403 /* Argument forbidden: fail if there is one. */ 404 if (bsdtar->argument != NULL) { 405 lafe_warnc(0, 406 "Option %s%s does not allow an argument", 407 long_prefix, match->name); 408 return ('?'); 409 } 410 } 411 return (match->equivalent); 412 } 413 414 return (opt); 415 } 416