You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getopt.h 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef _GETOPT_H
  2. #define _GETOPT_H
  3. /** @file
  4. *
  5. * Parse command-line options
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <stddef.h>
  10. enum getopt_argument_requirement {
  11. /** Option does not take an argument */
  12. no_argument = 0,
  13. /** Option requires an argument */
  14. required_argument = 1,
  15. /** Option may have an argument */
  16. optional_argument = 2,
  17. };
  18. /** A long option, as used for getopt_long() */
  19. struct option {
  20. /** Long name of this option */
  21. const char *name;
  22. /** Option takes an argument
  23. *
  24. * Must be one of @c no_argument, @c required_argument, or @c
  25. * optional_argument.
  26. */
  27. int has_arg;
  28. /** Location into which to store @c val, or NULL.
  29. *
  30. * See the description for @c val for more details.
  31. */
  32. int *flag;
  33. /** Value to return
  34. *
  35. * If @c flag is NULL, then this is the value that will be
  36. * returned by getopt_long() when this option is found, and
  37. * should therefore be set to the equivalent short option
  38. * character.
  39. *
  40. * If @c flag is non-NULL, then this value will be written to
  41. * the location pointed to by @flag, and getopt_long() will
  42. * return 0.
  43. */
  44. int val;
  45. };
  46. extern char *optarg;
  47. extern int optind;
  48. extern int nextchar;
  49. extern int optopt;
  50. extern int getopt_long ( int argc, char * const argv[], const char *optstring,
  51. const struct option *longopts, int *longindex );
  52. /**
  53. * Parse command-line options
  54. *
  55. * @v argv Argument count
  56. * @v argv Argument list
  57. * @v optstring Option specification string
  58. * @ret option Option found, or -1 for no more options
  59. *
  60. * See getopt_long() for full details.
  61. */
  62. static inline int getopt ( int argc, char * const argv[],
  63. const char *optstring ) {
  64. static const struct option no_options[] = {
  65. { NULL, 0, NULL, 0 }
  66. };
  67. return getopt_long ( argc, argv, optstring, no_options, NULL );
  68. }
  69. /**
  70. * Reset getopt() internal state
  71. *
  72. * Due to a limitation of the POSIX getopt() API, it is necessary to
  73. * add a call to reset_getopt() before each set of calls to getopt()
  74. * or getopt_long(). This arises because POSIX assumes that each
  75. * process will parse command line arguments no more than once; this
  76. * assumption is not valid within Etherboot. We work around the
  77. * limitation by arranging for execv() to call reset_getopt() before
  78. * executing the command.
  79. */
  80. static inline void reset_getopt ( void ) {
  81. optind = 1;
  82. nextchar = 0;
  83. }
  84. #endif /* _GETOPT_H */