cmdline.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*-
  2. * Copyright 2006-2009 Colin Percival
  3. * All rights reserved.
  4. *
  5. * Portions of the file below are covered by the following license:
  6. *
  7. * Copyright (c) 2003-2008 Tim Kientzle
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * Command line parser for tar.
  32. */
  33. #include "bsdtar_platform.h"
  34. __FBSDID("$FreeBSD$");
  35. #ifdef HAVE_ERRNO_H
  36. #include <errno.h>
  37. #endif
  38. #ifdef HAVE_STDLIB_H
  39. #include <stdlib.h>
  40. #endif
  41. #ifdef HAVE_STRING_H
  42. #include <string.h>
  43. #endif
  44. #include "bsdtar.h"
  45. /*
  46. * Short options for tar. Please keep this sorted.
  47. */
  48. static const char *short_options
  49. = "BC:cdf:HhI:kLlmnOoPpqrSs:T:tUvW:wX:x";
  50. /*
  51. * Long options for tar. Please keep this list sorted.
  52. *
  53. * The symbolic names for options that lack a short equivalent are
  54. * defined in bsdtar.h. Also note that so far I've found no need
  55. * to support optional arguments to long options. That would be
  56. * a small change to the code below.
  57. */
  58. static struct option {
  59. const char *name;
  60. int required; /* 1 if this option requires an argument. */
  61. int equivalent; /* Equivalent short option. */
  62. } tar_longopts[] = {
  63. { "absolute-paths", 0, 'P' },
  64. { "aggressive-networking",0, OPTION_AGGRESSIVE_NETWORKING },
  65. { "archive-names", 1, OPTION_ARCHIVE_NAMES },
  66. { "cachedir", 1, OPTION_CACHEDIR },
  67. { "cd", 1, 'C' },
  68. { "check-links", 0, OPTION_CHECK_LINKS },
  69. { "checkpoint-bytes", 1, OPTION_CHECKPOINT_BYTES },
  70. { "chroot", 0, OPTION_CHROOT },
  71. { "configfile", 1, OPTION_CONFIGFILE },
  72. { "confirmation", 0, 'w' },
  73. { "create", 0, 'c' },
  74. { "creationtime", 1, OPTION_CREATIONTIME },
  75. { "csv-file", 1, OPTION_CSV_FILE },
  76. { "debug-network-stats", 0, OPTION_DEBUG_NETWORK_STATS },
  77. { "dereference", 0, 'L' },
  78. { "directory", 1, 'C' },
  79. { "disk-pause", 1, OPTION_DISK_PAUSE },
  80. { "dry-run", 0, OPTION_DRYRUN },
  81. { "dry-run-metadata", 0, OPTION_DRYRUN_METADATA },
  82. { "dump-config", 0, OPTION_DUMP_CONFIG },
  83. { "exclude", 1, OPTION_EXCLUDE },
  84. { "exclude-from", 1, 'X' },
  85. { "extract", 0, 'x' },
  86. { "fast-read", 0, 'q' },
  87. { "file", 1, 'f' },
  88. { "files-from", 1, 'T' },
  89. { "force-resources", 0, OPTION_FORCE_RESOURCES },
  90. { "fsck", 0, OPTION_FSCK },
  91. { "fsck-prune", 0, OPTION_FSCK_PRUNE },
  92. { "hashes", 0, OPTION_HASHES },
  93. { "help", 0, OPTION_HELP },
  94. { "humanize-numbers", 0, OPTION_HUMANIZE_NUMBERS },
  95. { "include", 1, OPTION_INCLUDE },
  96. { "initialize-cachedir", 0, OPTION_INITIALIZE_CACHEDIR },
  97. { "insane-filesystems", 0, OPTION_INSANE_FILESYSTEMS },
  98. { "iso-dates", 0, OPTION_ISO_DATES },
  99. { "insecure", 0, 'P' },
  100. { "interactive", 0, 'w' },
  101. { "keep-going", 0, OPTION_KEEP_GOING },
  102. { "keep-newer-files", 0, OPTION_KEEP_NEWER_FILES },
  103. { "keep-old-files", 0, 'k' },
  104. { "keyfile", 1, OPTION_KEYFILE },
  105. { "list", 0, 't' },
  106. { "list-archives", 0, OPTION_LIST_ARCHIVES },
  107. { "lowmem", 0, OPTION_LOWMEM },
  108. { "maxbw", 1, OPTION_MAXBW },
  109. { "maxbw-rate", 1, OPTION_MAXBW_RATE },
  110. { "maxbw-rate-down", 1, OPTION_MAXBW_RATE_DOWN },
  111. { "maxbw-rate-up", 1, OPTION_MAXBW_RATE_UP },
  112. { "modification-time", 0, 'm' },
  113. { "newer", 1, OPTION_NEWER_CTIME },
  114. { "newer-ctime", 1, OPTION_NEWER_CTIME },
  115. { "newer-ctime-than", 1, OPTION_NEWER_CTIME_THAN },
  116. { "newer-mtime", 1, OPTION_NEWER_MTIME },
  117. { "newer-mtime-than", 1, OPTION_NEWER_MTIME_THAN },
  118. { "newer-than", 1, OPTION_NEWER_CTIME_THAN },
  119. { "nodump", 0, OPTION_NODUMP },
  120. { "noisy-warnings", 0, OPTION_NOISY_WARNINGS },
  121. { "norecurse", 0, 'n' },
  122. { "normalmem", 0, OPTION_NORMALMEM },
  123. { "no-aggressive-networking", 0, OPTION_NO_AGGRESSIVE_NETWORKING },
  124. { "no-config-exclude", 0, OPTION_NO_CONFIG_EXCLUDE },
  125. { "no-config-include", 0, OPTION_NO_CONFIG_INCLUDE },
  126. { "no-default-config", 0, OPTION_NO_DEFAULT_CONFIG },
  127. { "no-disk-pause", 0, OPTION_NO_DISK_PAUSE },
  128. { "no-force-resources", 0, OPTION_NO_FORCE_RESOURCES },
  129. { "no-humanize-numbers", 0, OPTION_NO_HUMANIZE_NUMBERS },
  130. { "no-insane-filesystems", 0, OPTION_NO_INSANE_FILESYSTEMS },
  131. { "no-iso-dates", 0, OPTION_NO_ISO_DATES },
  132. { "no-maxbw", 0, OPTION_NO_MAXBW },
  133. { "no-maxbw-rate-down", 0, OPTION_NO_MAXBW_RATE_DOWN },
  134. { "no-maxbw-rate-up", 0, OPTION_NO_MAXBW_RATE_UP },
  135. { "no-nodump", 0, OPTION_NO_NODUMP },
  136. { "no-print-stats", 0, OPTION_NO_PRINT_STATS },
  137. { "no-progress-bytes", 0, OPTION_NO_PROGRESS_BYTES },
  138. { "no-quiet", 0, OPTION_NO_QUIET },
  139. { "no-recursion", 0, 'n' },
  140. { "no-retry-forever", 0, OPTION_NO_RETRY_FOREVER },
  141. { "no-same-owner", 0, OPTION_NO_SAME_OWNER },
  142. { "no-same-permissions", 0, OPTION_NO_SAME_PERMISSIONS },
  143. { "no-snaptime", 0, OPTION_NO_SNAPTIME },
  144. { "no-store-atime", 0, OPTION_NO_STORE_ATIME },
  145. { "no-totals", 0, OPTION_NO_TOTALS },
  146. { "nuke", 0, OPTION_NUKE },
  147. { "null", 0, OPTION_NULL },
  148. { "numeric-owner", 0, OPTION_NUMERIC_OWNER },
  149. { "one-file-system", 0, OPTION_ONE_FILE_SYSTEM },
  150. { "passphrase", 1, OPTION_PASSPHRASE },
  151. { "preserve-permissions", 0, 'p' },
  152. { "print-stats", 0, OPTION_PRINT_STATS },
  153. { "quiet", 0, OPTION_QUIET },
  154. { "read-full-blocks", 0, 'B' },
  155. { "recover", 0, OPTION_RECOVER },
  156. { "resume-extract", 0, OPTION_RESUME_EXTRACT },
  157. { "retry-forever", 0, OPTION_RETRY_FOREVER },
  158. { "same-owner", 0, OPTION_SAME_OWNER },
  159. { "same-permissions", 0, 'p' },
  160. { "snaptime", 1, OPTION_SNAPTIME },
  161. { "store-atime", 0, OPTION_STORE_ATIME },
  162. { "strip-components", 1, OPTION_STRIP_COMPONENTS },
  163. { "to-stdout", 0, 'O' },
  164. { "totals", 0, OPTION_TOTALS },
  165. { "unlink", 0, 'U' },
  166. { "unlink-first", 0, 'U' },
  167. { "progress-bytes", 1, OPTION_PROGRESS_BYTES },
  168. { "verify-config", 0, OPTION_VERIFY_CONFIG },
  169. { "verbose", 0, 'v' },
  170. { "version", 0, OPTION_VERSION },
  171. { "verylowmem", 0, OPTION_VERYLOWMEM },
  172. { NULL, 0, 0 }
  173. };
  174. /*
  175. * This getopt implementation has two key features that common
  176. * getopt_long() implementations lack. Apart from those, it's a
  177. * straightforward option parser, considerably simplified by not
  178. * needing to support the wealth of exotic getopt_long() features. It
  179. * has, of course, been shamelessly tailored for bsdtar. (If you're
  180. * looking for a generic getopt_long() implementation for your
  181. * project, I recommend Gregory Pietsch's public domain getopt_long()
  182. * implementation.) The two additional features are:
  183. *
  184. * Old-style tar arguments: The original tar implementation treated
  185. * the first argument word as a list of single-character option
  186. * letters. All arguments follow as separate words. For example,
  187. * tar xbf 32 /dev/tape
  188. * Here, the "xbf" is three option letters, "32" is the argument for
  189. * "b" and "/dev/tape" is the argument for "f". We support this usage
  190. * if the first command-line argument does not begin with '-'. We
  191. * also allow regular short and long options to follow, e.g.,
  192. * tar xbf 32 /dev/tape -P --format=pax
  193. *
  194. * -W long options: There's an obscure GNU convention (only rarely
  195. * supported even there) that allows "-W option=argument" as an
  196. * alternative way to support long options. This was supported in
  197. * early bsdtar as a way to access long options on platforms that did
  198. * not support getopt_long() and is preserved here for backwards
  199. * compatibility. (Of course, if I'd started with a custom
  200. * command-line parser from the beginning, I would have had normal
  201. * long option support on every platform so that hack wouldn't have
  202. * been necessary. Oh, well. Some mistakes you just have to live
  203. * with.)
  204. *
  205. * TODO: We should be able to use this to pull files and intermingled
  206. * options (such as -C) from the command line in write mode. That
  207. * will require a little rethinking of the argument handling in
  208. * bsdtar.c.
  209. *
  210. * TODO: If we want to support arbitrary command-line options from -T
  211. * input (as GNU tar does), we may need to extend this to handle option
  212. * words from sources other than argv/argc. I'm not really sure if I
  213. * like that feature of GNU tar, so it's certainly not a priority.
  214. */
  215. int
  216. bsdtar_getopt(struct bsdtar *bsdtar)
  217. {
  218. enum { state_start = 0, state_old_tar, state_next_word,
  219. state_short, state_long };
  220. static int state = state_start;
  221. static char *opt_word;
  222. const struct option *popt, *match = NULL, *match2 = NULL;
  223. const char *p, *long_prefix = "--";
  224. size_t optlength;
  225. int opt = '?';
  226. int required = 0;
  227. bsdtar->optarg = NULL;
  228. /* First time through, initialize everything. */
  229. if (state == state_start) {
  230. /* Skip program name. */
  231. ++bsdtar->argv;
  232. --bsdtar->argc;
  233. if (*bsdtar->argv == NULL)
  234. return (-1);
  235. /* Decide between "new style" and "old style" arguments. */
  236. if (bsdtar->argv[0][0] == '-') {
  237. state = state_next_word;
  238. } else {
  239. state = state_old_tar;
  240. opt_word = *bsdtar->argv++;
  241. --bsdtar->argc;
  242. }
  243. }
  244. /*
  245. * We're parsing old-style tar arguments
  246. */
  247. if (state == state_old_tar) {
  248. /* Get the next option character. */
  249. opt = *opt_word++;
  250. if (opt == '\0') {
  251. /* New-style args can follow old-style. */
  252. state = state_next_word;
  253. } else {
  254. /* See if it takes an argument. */
  255. p = strchr(short_options, opt);
  256. if (p == NULL)
  257. return ('?');
  258. if (p[1] == ':') {
  259. bsdtar->optarg = *bsdtar->argv;
  260. if (bsdtar->optarg == NULL) {
  261. bsdtar_warnc(bsdtar, 0,
  262. "Option %c requires an argument",
  263. opt);
  264. return ('?');
  265. }
  266. ++bsdtar->argv;
  267. --bsdtar->argc;
  268. }
  269. }
  270. }
  271. /*
  272. * We're ready to look at the next word in argv.
  273. */
  274. if (state == state_next_word) {
  275. /* No more arguments, so no more options. */
  276. if (bsdtar->argv[0] == NULL)
  277. return (-1);
  278. /* Doesn't start with '-', so no more options. */
  279. if (bsdtar->argv[0][0] != '-')
  280. return (-1);
  281. /* "--" marks end of options; consume it and return. */
  282. if (strcmp(bsdtar->argv[0], "--") == 0) {
  283. ++bsdtar->argv;
  284. --bsdtar->argc;
  285. return (-1);
  286. }
  287. /* Get next word for parsing. */
  288. opt_word = *bsdtar->argv++;
  289. --bsdtar->argc;
  290. if (opt_word[1] == '-') {
  291. /* Set up long option parser. */
  292. state = state_long;
  293. opt_word += 2; /* Skip leading '--' */
  294. } else {
  295. /* Set up short option parser. */
  296. state = state_short;
  297. ++opt_word; /* Skip leading '-' */
  298. }
  299. }
  300. /*
  301. * We're parsing a group of POSIX-style single-character options.
  302. */
  303. if (state == state_short) {
  304. /* Peel next option off of a group of short options. */
  305. opt = *opt_word++;
  306. if (opt == '\0') {
  307. /* End of this group; recurse to get next option. */
  308. state = state_next_word;
  309. return bsdtar_getopt(bsdtar);
  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. /* If it takes an argument, parse that. */
  318. if (required) {
  319. /* If arg is run-in, opt_word already points to it. */
  320. if (opt_word[0] == '\0') {
  321. /* Otherwise, pick up the next word. */
  322. opt_word = *bsdtar->argv;
  323. if (opt_word == NULL) {
  324. bsdtar_warnc(bsdtar, 0,
  325. "Option -%c requires an argument",
  326. opt);
  327. return ('?');
  328. }
  329. ++bsdtar->argv;
  330. --bsdtar->argc;
  331. }
  332. if (opt == 'W') {
  333. state = state_long;
  334. long_prefix = "-W "; /* For clearer errors. */
  335. } else {
  336. state = state_next_word;
  337. bsdtar->optarg = opt_word;
  338. }
  339. }
  340. }
  341. /* We're reading a long option, including -W long=arg convention. */
  342. if (state == state_long) {
  343. /* After this long option, we'll be starting a new word. */
  344. state = state_next_word;
  345. /* Option name ends at '=' if there is one. */
  346. p = strchr(opt_word, '=');
  347. if (p != NULL) {
  348. optlength = (size_t)(p - opt_word);
  349. bsdtar->optarg = (char *)(uintptr_t)(p + 1);
  350. } else {
  351. optlength = strlen(opt_word);
  352. }
  353. /* Search the table for an unambiguous match. */
  354. for (popt = tar_longopts; popt->name != NULL; popt++) {
  355. /* Short-circuit if first chars don't match. */
  356. if (popt->name[0] != opt_word[0])
  357. continue;
  358. /* If option is a prefix of name in table, record it. */
  359. if (strncmp(opt_word, popt->name, optlength) == 0) {
  360. match2 = match; /* Record up to two matches. */
  361. match = popt;
  362. /* If it's an exact match, we're done. */
  363. if (strlen(popt->name) == optlength) {
  364. match2 = NULL; /* Forget the others. */
  365. break;
  366. }
  367. }
  368. }
  369. /* Fail if there wasn't a unique match. */
  370. if (match == NULL) {
  371. bsdtar_warnc(bsdtar, 0,
  372. "Option %s%s is not supported",
  373. long_prefix, opt_word);
  374. return ('?');
  375. }
  376. if (match2 != NULL) {
  377. bsdtar_warnc(bsdtar, 0,
  378. "Ambiguous option %s%s (matches --%s and --%s)",
  379. long_prefix, opt_word, match->name, match2->name);
  380. return ('?');
  381. }
  382. /* We've found a unique match; does it need an argument? */
  383. if (match->required) {
  384. /* Argument required: get next word if necessary. */
  385. if (bsdtar->optarg == NULL) {
  386. bsdtar->optarg = *bsdtar->argv;
  387. if (bsdtar->optarg == NULL) {
  388. bsdtar_warnc(bsdtar, 0,
  389. "Option %s%s requires an argument",
  390. long_prefix, match->name);
  391. return ('?');
  392. }
  393. ++bsdtar->argv;
  394. --bsdtar->argc;
  395. }
  396. } else {
  397. /* Argument forbidden: fail if there is one. */
  398. if (bsdtar->optarg != NULL) {
  399. bsdtar_warnc(bsdtar, 0,
  400. "Option %s%s does not allow an argument",
  401. long_prefix, match->name);
  402. return ('?');
  403. }
  404. }
  405. return (match->equivalent);
  406. }
  407. return (opt);
  408. }