cmdline.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. * Command line parser for tar.
  27. */
  28. #include "bsdcat_platform.h"
  29. __FBSDID("$FreeBSD$");
  30. #ifdef HAVE_ERRNO_H
  31. #include <errno.h>
  32. #endif
  33. #ifdef HAVE_STDLIB_H
  34. #include <stdlib.h>
  35. #endif
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. #include "bsdcat.h"
  40. #include "err.h"
  41. /*
  42. * Short options for tar. Please keep this sorted.
  43. */
  44. static const char *short_options = "h";
  45. /*
  46. * Long options for tar. Please keep this list sorted.
  47. *
  48. * The symbolic names for options that lack a short equivalent are
  49. * defined in bsdcat.h. Also note that so far I've found no need
  50. * to support optional arguments to long options. That would be
  51. * a small change to the code below.
  52. */
  53. static const struct bsdcat_option {
  54. const char *name;
  55. int required; /* 1 if this option requires an argument. */
  56. int equivalent; /* Equivalent short option. */
  57. } tar_longopts[] = {
  58. { "help", 0, 'h' },
  59. { "version", 0, OPTION_VERSION },
  60. { NULL, 0, 0 }
  61. };
  62. /*
  63. * This getopt implementation has two key features that common
  64. * getopt_long() implementations lack. Apart from those, it's a
  65. * straightforward option parser, considerably simplified by not
  66. * needing to support the wealth of exotic getopt_long() features. It
  67. * has, of course, been shamelessly tailored for bsdcat. (If you're
  68. * looking for a generic getopt_long() implementation for your
  69. * project, I recommend Gregory Pietsch's public domain getopt_long()
  70. * implementation.) The two additional features are:
  71. *
  72. * Old-style tar arguments: The original tar implementation treated
  73. * the first argument word as a list of single-character option
  74. * letters. All arguments follow as separate words. For example,
  75. * tar xbf 32 /dev/tape
  76. * Here, the "xbf" is three option letters, "32" is the argument for
  77. * "b" and "/dev/tape" is the argument for "f". We support this usage
  78. * if the first command-line argument does not begin with '-'. We
  79. * also allow regular short and long options to follow, e.g.,
  80. * tar xbf 32 /dev/tape -P --format=pax
  81. *
  82. * -W long options: There's an obscure GNU convention (only rarely
  83. * supported even there) that allows "-W option=argument" as an
  84. * alternative way to support long options. This was supported in
  85. * early bsdcat as a way to access long options on platforms that did
  86. * not support getopt_long() and is preserved here for backwards
  87. * compatibility. (Of course, if I'd started with a custom
  88. * command-line parser from the beginning, I would have had normal
  89. * long option support on every platform so that hack wouldn't have
  90. * been necessary. Oh, well. Some mistakes you just have to live
  91. * with.)
  92. *
  93. * TODO: We should be able to use this to pull files and intermingled
  94. * options (such as -C) from the command line in write mode. That
  95. * will require a little rethinking of the argument handling in
  96. * bsdcat.c.
  97. *
  98. * TODO: If we want to support arbitrary command-line options from -T
  99. * input (as GNU tar does), we may need to extend this to handle option
  100. * words from sources other than argv/argc. I'm not really sure if I
  101. * like that feature of GNU tar, so it's certainly not a priority.
  102. */
  103. int
  104. bsdcat_getopt(struct bsdcat *bsdcat)
  105. {
  106. enum { state_start = 0, state_old_tar, state_next_word,
  107. state_short, state_long };
  108. const struct bsdcat_option *popt, *match = NULL, *match2 = NULL;
  109. const char *p, *long_prefix = "--";
  110. size_t optlength;
  111. int opt = '?';
  112. int required = 0;
  113. bsdcat->argument = NULL;
  114. /* First time through, initialize everything. */
  115. if (bsdcat->getopt_state == state_start) {
  116. /* Skip program name. */
  117. ++bsdcat->argv;
  118. --bsdcat->argc;
  119. if (*bsdcat->argv == NULL)
  120. return (-1);
  121. /* Decide between "new style" and "old style" arguments. */
  122. bsdcat->getopt_state = state_next_word;
  123. }
  124. /*
  125. * We're ready to look at the next word in argv.
  126. */
  127. if (bsdcat->getopt_state == state_next_word) {
  128. /* No more arguments, so no more options. */
  129. if (bsdcat->argv[0] == NULL)
  130. return (-1);
  131. /* Doesn't start with '-', so no more options. */
  132. if (bsdcat->argv[0][0] != '-')
  133. return (-1);
  134. /* "--" marks end of options; consume it and return. */
  135. if (strcmp(bsdcat->argv[0], "--") == 0) {
  136. ++bsdcat->argv;
  137. --bsdcat->argc;
  138. return (-1);
  139. }
  140. /* Get next word for parsing. */
  141. bsdcat->getopt_word = *bsdcat->argv++;
  142. --bsdcat->argc;
  143. if (bsdcat->getopt_word[1] == '-') {
  144. /* Set up long option parser. */
  145. bsdcat->getopt_state = state_long;
  146. bsdcat->getopt_word += 2; /* Skip leading '--' */
  147. } else {
  148. /* Set up short option parser. */
  149. bsdcat->getopt_state = state_short;
  150. ++bsdcat->getopt_word; /* Skip leading '-' */
  151. }
  152. }
  153. /*
  154. * We're parsing a group of POSIX-style single-character options.
  155. */
  156. if (bsdcat->getopt_state == state_short) {
  157. /* Peel next option off of a group of short options. */
  158. opt = *bsdcat->getopt_word++;
  159. if (opt == '\0') {
  160. /* End of this group; recurse to get next option. */
  161. bsdcat->getopt_state = state_next_word;
  162. return bsdcat_getopt(bsdcat);
  163. }
  164. /* Does this option take an argument? */
  165. p = strchr(short_options, opt);
  166. if (p == NULL)
  167. return ('?');
  168. if (p[1] == ':')
  169. required = 1;
  170. /* If it takes an argument, parse that. */
  171. if (required) {
  172. /* If arg is run-in, bsdcat->getopt_word already points to it. */
  173. if (bsdcat->getopt_word[0] == '\0') {
  174. /* Otherwise, pick up the next word. */
  175. bsdcat->getopt_word = *bsdcat->argv;
  176. if (bsdcat->getopt_word == NULL) {
  177. lafe_warnc(0,
  178. "Option -%c requires an argument",
  179. opt);
  180. return ('?');
  181. }
  182. ++bsdcat->argv;
  183. --bsdcat->argc;
  184. }
  185. if (opt == 'W') {
  186. bsdcat->getopt_state = state_long;
  187. long_prefix = "-W "; /* For clearer errors. */
  188. } else {
  189. bsdcat->getopt_state = state_next_word;
  190. bsdcat->argument = bsdcat->getopt_word;
  191. }
  192. }
  193. }
  194. /* We're reading a long option, including -W long=arg convention. */
  195. if (bsdcat->getopt_state == state_long) {
  196. /* After this long option, we'll be starting a new word. */
  197. bsdcat->getopt_state = state_next_word;
  198. /* Option name ends at '=' if there is one. */
  199. p = strchr(bsdcat->getopt_word, '=');
  200. if (p != NULL) {
  201. optlength = (size_t)(p - bsdcat->getopt_word);
  202. bsdcat->argument = (char *)(uintptr_t)(p + 1);
  203. } else {
  204. optlength = strlen(bsdcat->getopt_word);
  205. }
  206. /* Search the table for an unambiguous match. */
  207. for (popt = tar_longopts; popt->name != NULL; popt++) {
  208. /* Short-circuit if first chars don't match. */
  209. if (popt->name[0] != bsdcat->getopt_word[0])
  210. continue;
  211. /* If option is a prefix of name in table, record it.*/
  212. if (strncmp(bsdcat->getopt_word, popt->name, optlength) == 0) {
  213. match2 = match; /* Record up to two matches. */
  214. match = popt;
  215. /* If it's an exact match, we're done. */
  216. if (strlen(popt->name) == optlength) {
  217. match2 = NULL; /* Forget the others. */
  218. break;
  219. }
  220. }
  221. }
  222. /* Fail if there wasn't a unique match. */
  223. if (match == NULL) {
  224. lafe_warnc(0,
  225. "Option %s%s is not supported",
  226. long_prefix, bsdcat->getopt_word);
  227. return ('?');
  228. }
  229. if (match2 != NULL) {
  230. lafe_warnc(0,
  231. "Ambiguous option %s%s (matches --%s and --%s)",
  232. long_prefix, bsdcat->getopt_word, match->name, match2->name);
  233. return ('?');
  234. }
  235. /* We've found a unique match; does it need an argument? */
  236. if (match->required) {
  237. /* Argument required: get next word if necessary. */
  238. if (bsdcat->argument == NULL) {
  239. bsdcat->argument = *bsdcat->argv;
  240. if (bsdcat->argument == NULL) {
  241. lafe_warnc(0,
  242. "Option %s%s requires an argument",
  243. long_prefix, match->name);
  244. return ('?');
  245. }
  246. ++bsdcat->argv;
  247. --bsdcat->argc;
  248. }
  249. } else {
  250. /* Argument forbidden: fail if there is one. */
  251. if (bsdcat->argument != NULL) {
  252. lafe_warnc(0,
  253. "Option %s%s does not allow an argument",
  254. long_prefix, match->name);
  255. return ('?');
  256. }
  257. }
  258. return (match->equivalent);
  259. }
  260. return (opt);
  261. }