cmdline.c 15 KB

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