miniopt.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Command line options parser.
  3. *
  4. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  5. *
  6. * Copyright (C) 1999-2020, Broadcom Corporation
  7. *
  8. * Unless you and Broadcom execute a separate written software license
  9. * agreement governing use of this software, this software is licensed to you
  10. * under the terms of the GNU General Public License version 2 (the "GPL"),
  11. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  12. * following added to such license:
  13. *
  14. * As a special exception, the copyright holders of this software give you
  15. * permission to link this software with independent modules, and to copy and
  16. * distribute the resulting executable under terms of your choice, provided that
  17. * you also meet, for each linked independent module, the terms and conditions of
  18. * the license of that module. An independent module is a module which is not
  19. * derived from this software. The special exception does not apply to any
  20. * modifications of the software.
  21. *
  22. * Notwithstanding the above, under no circumstances may you combine this
  23. * software in any way with any other Broadcom software provided under a license
  24. * other than the GPL, without Broadcom's express prior written consent.
  25. *
  26. *
  27. * <<Broadcom-WL-IPTag/Open:>>
  28. *
  29. * $Id: miniopt.h 672943 2016-11-30 08:54:06Z $
  30. */
  31. #ifndef MINI_OPT_H
  32. #define MINI_OPT_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif // endif
  36. /* ---- Include Files ---------------------------------------------------- */
  37. /* ---- Constants and Types ---------------------------------------------- */
  38. #define MINIOPT_MAXKEY 128 /* Max options */
  39. typedef struct miniopt {
  40. /* These are persistent after miniopt_init() */
  41. const char* name; /* name for prompt in error strings */
  42. const char* flags; /* option chars that take no args */
  43. bool longflags; /* long options may be flags */
  44. bool opt_end; /* at end of options (passed a "--") */
  45. /* These are per-call to miniopt() */
  46. int consumed; /* number of argv entries cosumed in
  47. * the most recent call to miniopt()
  48. */
  49. bool positional;
  50. bool good_int; /* 'val' member is the result of a sucessful
  51. * strtol conversion of the option value
  52. */
  53. char opt;
  54. char key[MINIOPT_MAXKEY];
  55. char* valstr; /* positional param, or value for the option,
  56. * or null if the option had
  57. * no accompanying value
  58. */
  59. uint uval; /* strtol translation of valstr */
  60. int val; /* strtol translation of valstr */
  61. } miniopt_t;
  62. void miniopt_init(miniopt_t *t, const char* name, const char* flags, bool longflags);
  63. int miniopt(miniopt_t *t, char **argv);
  64. /* ---- Variable Externs ------------------------------------------------- */
  65. /* ---- Function Prototypes ---------------------------------------------- */
  66. #ifdef __cplusplus
  67. }
  68. #endif // endif
  69. #endif /* MINI_OPT_H */