cmdline.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*-
  2. * Copyright 2006-2025 Tarsnap Backup Inc.
  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. { "noatime", 0, OPTION_NOATIME },
  120. { "nodump", 0, OPTION_NODUMP },
  121. { "noisy-warnings", 0, OPTION_NOISY_WARNINGS },
  122. { "norecurse", 0, 'n' },
  123. { "normalmem", 0, OPTION_NORMALMEM },
  124. { "no-aggressive-networking", 0, OPTION_NO_AGGRESSIVE_NETWORKING },
  125. { "no-config-exclude", 0, OPTION_NO_CONFIG_EXCLUDE },
  126. { "no-config-include", 0, OPTION_NO_CONFIG_INCLUDE },
  127. { "no-default-config", 0, OPTION_NO_DEFAULT_CONFIG },
  128. { "no-disk-pause", 0, OPTION_NO_DISK_PAUSE },
  129. { "no-force-resources", 0, OPTION_NO_FORCE_RESOURCES },
  130. { "no-humanize-numbers", 0, OPTION_NO_HUMANIZE_NUMBERS },
  131. { "no-insane-filesystems", 0, OPTION_NO_INSANE_FILESYSTEMS },
  132. { "no-iso-dates", 0, OPTION_NO_ISO_DATES },
  133. { "no-maxbw", 0, OPTION_NO_MAXBW },
  134. { "no-maxbw-rate-down", 0, OPTION_NO_MAXBW_RATE_DOWN },
  135. { "no-maxbw-rate-up", 0, OPTION_NO_MAXBW_RATE_UP },
  136. { "no-noatime", 0, OPTION_NO_NOATIME },
  137. { "no-nodump", 0, OPTION_NO_NODUMP },
  138. { "no-print-stats", 0, OPTION_NO_PRINT_STATS },
  139. { "no-progress-bytes", 0, OPTION_NO_PROGRESS_BYTES },
  140. { "no-quiet", 0, OPTION_NO_QUIET },
  141. { "no-recursion", 0, 'n' },
  142. { "no-retry-forever", 0, OPTION_NO_RETRY_FOREVER },
  143. { "no-same-owner", 0, OPTION_NO_SAME_OWNER },
  144. { "no-same-permissions", 0, OPTION_NO_SAME_PERMISSIONS },
  145. { "no-snaptime", 0, OPTION_NO_SNAPTIME },
  146. { "no-store-atime", 0, OPTION_NO_STORE_ATIME },
  147. { "no-totals", 0, OPTION_NO_TOTALS },
  148. { "nuke", 0, OPTION_NUKE },
  149. { "null", 0, OPTION_NULL },
  150. { "null-input", 0, OPTION_NULL_INPUT },
  151. { "null-output", 0, OPTION_NULL_OUTPUT },
  152. { "numeric-owner", 0, OPTION_NUMERIC_OWNER },
  153. { "one-file-system", 0, OPTION_ONE_FILE_SYSTEM },
  154. { "passphrase", 1, OPTION_PASSPHRASE },
  155. { "preserve-permissions", 0, 'p' },
  156. { "print-stats", 0, OPTION_PRINT_STATS },
  157. { "quiet", 0, OPTION_QUIET },
  158. { "read-full-blocks", 0, 'B' },
  159. { "recover", 0, OPTION_RECOVER },
  160. { "resume-extract", 0, OPTION_RESUME_EXTRACT },
  161. { "retry-forever", 0, OPTION_RETRY_FOREVER },
  162. { "same-owner", 0, OPTION_SAME_OWNER },
  163. { "same-permissions", 0, 'p' },
  164. { "snaptime", 1, OPTION_SNAPTIME },
  165. { "store-atime", 0, OPTION_STORE_ATIME },
  166. { "strip-components", 1, OPTION_STRIP_COMPONENTS },
  167. { "to-stdout", 0, 'O' },
  168. { "totals", 0, OPTION_TOTALS },
  169. { "unlink", 0, 'U' },
  170. { "unlink-first", 0, 'U' },
  171. { "progress-bytes", 1, OPTION_PROGRESS_BYTES },
  172. { "verify-config", 0, OPTION_VERIFY_CONFIG },
  173. { "verbose", 0, 'v' },
  174. { "version", 0, OPTION_VERSION },
  175. { "verylowmem", 0, OPTION_VERYLOWMEM },
  176. { NULL, 0, 0 }
  177. };
  178. /*
  179. * This getopt implementation has two key features that common
  180. * getopt_long() implementations lack. Apart from those, it's a
  181. * straightforward option parser, considerably simplified by not
  182. * needing to support the wealth of exotic getopt_long() features. It
  183. * has, of course, been shamelessly tailored for bsdtar. (If you're
  184. * looking for a generic getopt_long() implementation for your
  185. * project, I recommend Gregory Pietsch's public domain getopt_long()
  186. * implementation.) The two additional features are:
  187. *
  188. * Old-style tar arguments: The original tar implementation treated
  189. * the first argument word as a list of single-character option
  190. * letters. All arguments follow as separate words. For example,
  191. * tar xbf 32 /dev/tape
  192. * Here, the "xbf" is three option letters, "32" is the argument for
  193. * "b" and "/dev/tape" is the argument for "f". We support this usage
  194. * if the first command-line argument does not begin with '-'. We
  195. * also allow regular short and long options to follow, e.g.,
  196. * tar xbf 32 /dev/tape -P --format=pax
  197. *
  198. * -W long options: There's an obscure GNU convention (only rarely
  199. * supported even there) that allows "-W option=argument" as an
  200. * alternative way to support long options. This was supported in
  201. * early bsdtar as a way to access long options on platforms that did
  202. * not support getopt_long() and is preserved here for backwards
  203. * compatibility. (Of course, if I'd started with a custom
  204. * command-line parser from the beginning, I would have had normal
  205. * long option support on every platform so that hack wouldn't have
  206. * been necessary. Oh, well. Some mistakes you just have to live
  207. * with.)
  208. *
  209. * TODO: We should be able to use this to pull files and intermingled
  210. * options (such as -C) from the command line in write mode. That
  211. * will require a little rethinking of the argument handling in
  212. * bsdtar.c.
  213. *
  214. * TODO: If we want to support arbitrary command-line options from -T
  215. * input (as GNU tar does), we may need to extend this to handle option
  216. * words from sources other than argv/argc. I'm not really sure if I
  217. * like that feature of GNU tar, so it's certainly not a priority.
  218. */
  219. int
  220. bsdtar_getopt(struct bsdtar *bsdtar)
  221. {
  222. enum { state_start = 0, state_old_tar, state_next_word,
  223. state_short, state_long };
  224. static int state = state_start;
  225. static char *opt_word;
  226. const struct option *popt, *match = NULL, *match2 = NULL;
  227. const char *p, *long_prefix = "--";
  228. size_t optlength;
  229. int opt = '?';
  230. int required = 0;
  231. bsdtar->optarg = NULL;
  232. /* First time through, initialize everything. */
  233. if (state == state_start) {
  234. /* Skip program name. */
  235. ++bsdtar->argv;
  236. --bsdtar->argc;
  237. if (*bsdtar->argv == NULL)
  238. return (-1);
  239. /* Decide between "new style" and "old style" arguments. */
  240. if (bsdtar->argv[0][0] == '-') {
  241. state = state_next_word;
  242. } else {
  243. state = state_old_tar;
  244. opt_word = *bsdtar->argv++;
  245. --bsdtar->argc;
  246. }
  247. }
  248. /*
  249. * We're parsing old-style tar arguments
  250. */
  251. if (state == state_old_tar) {
  252. /* Get the next option character. */
  253. opt = *opt_word++;
  254. if (opt == '\0') {
  255. /* New-style args can follow old-style. */
  256. state = state_next_word;
  257. } else {
  258. /* See if it takes an argument. */
  259. p = strchr(short_options, opt);
  260. if (p == NULL)
  261. return ('?');
  262. if (p[1] == ':') {
  263. bsdtar->optarg = *bsdtar->argv;
  264. if (bsdtar->optarg == NULL) {
  265. bsdtar_warnc(bsdtar, 0,
  266. "Option %c requires an argument",
  267. opt);
  268. return ('?');
  269. }
  270. ++bsdtar->argv;
  271. --bsdtar->argc;
  272. }
  273. }
  274. }
  275. /*
  276. * We're ready to look at the next word in argv.
  277. */
  278. if (state == state_next_word) {
  279. /* No more arguments, so no more options. */
  280. if (bsdtar->argv[0] == NULL)
  281. return (-1);
  282. /* Doesn't start with '-', so no more options. */
  283. if (bsdtar->argv[0][0] != '-')
  284. return (-1);
  285. /* "--" marks end of options; consume it and return. */
  286. if (strcmp(bsdtar->argv[0], "--") == 0) {
  287. ++bsdtar->argv;
  288. --bsdtar->argc;
  289. return (-1);
  290. }
  291. /* Get next word for parsing. */
  292. opt_word = *bsdtar->argv++;
  293. --bsdtar->argc;
  294. if (opt_word[1] == '-') {
  295. /* Set up long option parser. */
  296. state = state_long;
  297. opt_word += 2; /* Skip leading '--' */
  298. } else {
  299. /* Set up short option parser. */
  300. state = state_short;
  301. ++opt_word; /* Skip leading '-' */
  302. }
  303. }
  304. /*
  305. * We're parsing a group of POSIX-style single-character options.
  306. */
  307. if (state == state_short) {
  308. /* Peel next option off of a group of short options. */
  309. opt = *opt_word++;
  310. if (opt == '\0') {
  311. /* End of this group; recurse to get next option. */
  312. state = state_next_word;
  313. return bsdtar_getopt(bsdtar);
  314. }
  315. /* Does this option take an argument? */
  316. p = strchr(short_options, opt);
  317. if (p == NULL)
  318. return ('?');
  319. if (p[1] == ':')
  320. required = 1;
  321. /* If it takes an argument, parse that. */
  322. if (required) {
  323. /* If arg is run-in, opt_word already points to it. */
  324. if (opt_word[0] == '\0') {
  325. /* Otherwise, pick up the next word. */
  326. opt_word = *bsdtar->argv;
  327. if (opt_word == NULL) {
  328. bsdtar_warnc(bsdtar, 0,
  329. "Option -%c requires an argument",
  330. opt);
  331. return ('?');
  332. }
  333. ++bsdtar->argv;
  334. --bsdtar->argc;
  335. }
  336. if (opt == 'W') {
  337. state = state_long;
  338. long_prefix = "-W "; /* For clearer errors. */
  339. } else {
  340. state = state_next_word;
  341. bsdtar->optarg = opt_word;
  342. }
  343. }
  344. }
  345. /* We're reading a long option, including -W long=arg convention. */
  346. if (state == state_long) {
  347. /* After this long option, we'll be starting a new word. */
  348. state = state_next_word;
  349. /* Option name ends at '=' if there is one. */
  350. p = strchr(opt_word, '=');
  351. if (p != NULL) {
  352. optlength = (size_t)(p - opt_word);
  353. bsdtar->optarg = (char *)(uintptr_t)(p + 1);
  354. } else {
  355. optlength = strlen(opt_word);
  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] != opt_word[0])
  361. continue;
  362. /* If option is a prefix of name in table, record it. */
  363. if (strncmp(opt_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. /* Fail if there wasn't a unique match. */
  374. if (match == NULL) {
  375. bsdtar_warnc(bsdtar, 0,
  376. "Option %s%s is not supported",
  377. long_prefix, opt_word);
  378. return ('?');
  379. }
  380. if (match2 != NULL) {
  381. bsdtar_warnc(bsdtar, 0,
  382. "Ambiguous option %s%s (matches --%s and --%s)",
  383. long_prefix, opt_word, match->name, match2->name);
  384. return ('?');
  385. }
  386. /* We've found a unique match; does it need an argument? */
  387. if (match->required) {
  388. /* Argument required: get next word if necessary. */
  389. if (bsdtar->optarg == NULL) {
  390. bsdtar->optarg = *bsdtar->argv;
  391. if (bsdtar->optarg == NULL) {
  392. bsdtar_warnc(bsdtar, 0,
  393. "Option %s%s requires an argument",
  394. long_prefix, match->name);
  395. return ('?');
  396. }
  397. ++bsdtar->argv;
  398. --bsdtar->argc;
  399. }
  400. } else {
  401. /* Argument forbidden: fail if there is one. */
  402. if (bsdtar->optarg != NULL) {
  403. bsdtar_warnc(bsdtar, 0,
  404. "Option %s%s does not allow an argument",
  405. long_prefix, match->name);
  406. return ('?');
  407. }
  408. }
  409. return (match->equivalent);
  410. }
  411. return (opt);
  412. }