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.

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