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.

136 lines
3.5 KiB

  1. /*---------------------------------------------------------------------------*
  2. * <RCS keywords>
  3. *
  4. * C++ Library
  5. *
  6. * Copyright 1992-1994, David Gottner
  7. *
  8. * All Rights Reserved
  9. *
  10. * Permission to use, copy, modify, and distribute this software and its
  11. * documentation for any purpose and without fee is hereby granted,
  12. * provided that the above copyright notice, this permission notice and
  13. * the following disclaimer notice appear unmodified in all copies.
  14. *
  15. * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I
  17. * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
  18. * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
  19. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  20. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. *
  22. * Nevertheless, I would like to know about bugs in this library or
  23. * suggestions for improvment. Send bug reports and feedback to
  24. * davegottner@delphi.com.
  25. *---------------------------------------------------------------------------*/
  26. /* Modified to support --help and --version, as well as /? on Windows
  27. * by Georg Brandl. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. int _PyOS_opterr = 1; /* generate error messages */
  34. int _PyOS_optind = 1; /* index into argv array */
  35. char *_PyOS_optarg = NULL; /* optional argument */
  36. static char *opt_ptr = "";
  37. void _PyOS_ResetGetOpt(void)
  38. {
  39. _PyOS_opterr = 1;
  40. _PyOS_optind = 1;
  41. _PyOS_optarg = NULL;
  42. opt_ptr = "";
  43. }
  44. int _PyOS_GetOpt(int argc, char **argv, char *optstring)
  45. {
  46. char *ptr;
  47. int option;
  48. if (*opt_ptr == '\0') {
  49. if (_PyOS_optind >= argc)
  50. return -1;
  51. #ifdef MS_WINDOWS
  52. else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
  53. ++_PyOS_optind;
  54. return 'h';
  55. }
  56. #endif
  57. else if (argv[_PyOS_optind][0] != '-' ||
  58. argv[_PyOS_optind][1] == '\0' /* lone dash */ )
  59. return -1;
  60. else if (strcmp(argv[_PyOS_optind], "--") == 0) {
  61. ++_PyOS_optind;
  62. return -1;
  63. }
  64. else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
  65. ++_PyOS_optind;
  66. return 'h';
  67. }
  68. else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
  69. ++_PyOS_optind;
  70. return 'V';
  71. }
  72. opt_ptr = &argv[_PyOS_optind++][1];
  73. }
  74. if ((option = *opt_ptr++) == '\0')
  75. return -1;
  76. if (option == 'J') {
  77. if (_PyOS_opterr)
  78. fprintf(stderr, "-J is reserved for Jython\n");
  79. return '_';
  80. }
  81. if (option == 'X') {
  82. if (_PyOS_opterr)
  83. fprintf(stderr,
  84. "-X is reserved for implementation-specific arguments\n");
  85. return '_';
  86. }
  87. if ((ptr = strchr(optstring, option)) == NULL) {
  88. if (_PyOS_opterr)
  89. fprintf(stderr, "Unknown option: -%c\n", option);
  90. return '_';
  91. }
  92. if (*(ptr + 1) == ':') {
  93. if (*opt_ptr != '\0') {
  94. _PyOS_optarg = opt_ptr;
  95. opt_ptr = "";
  96. }
  97. else {
  98. if (_PyOS_optind >= argc) {
  99. if (_PyOS_opterr)
  100. fprintf(stderr,
  101. "Argument expected for the -%c option\n", option);
  102. return '_';
  103. }
  104. _PyOS_optarg = argv[_PyOS_optind++];
  105. }
  106. }
  107. return option;
  108. }
  109. #ifdef __cplusplus
  110. }
  111. #endif