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.

126 lines
3.3 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. int _PyOS_GetOpt(int argc, char **argv, char *optstring)
  37. {
  38. static char *opt_ptr = "";
  39. char *ptr;
  40. int option;
  41. if (*opt_ptr == '\0') {
  42. if (_PyOS_optind >= argc)
  43. return -1;
  44. #ifdef MS_WINDOWS
  45. else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
  46. ++_PyOS_optind;
  47. return 'h';
  48. }
  49. #endif
  50. else if (argv[_PyOS_optind][0] != '-' ||
  51. argv[_PyOS_optind][1] == '\0' /* lone dash */ )
  52. return -1;
  53. else if (strcmp(argv[_PyOS_optind], "--") == 0) {
  54. ++_PyOS_optind;
  55. return -1;
  56. }
  57. else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
  58. ++_PyOS_optind;
  59. return 'h';
  60. }
  61. else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
  62. ++_PyOS_optind;
  63. return 'V';
  64. }
  65. opt_ptr = &argv[_PyOS_optind++][1];
  66. }
  67. if ( (option = *opt_ptr++) == '\0')
  68. return -1;
  69. if (option == 'J') {
  70. fprintf(stderr, "-J is reserved for Jython\n");
  71. return '_';
  72. }
  73. if (option == 'X') {
  74. fprintf(stderr,
  75. "-X is reserved for implementation-specific arguments\n");
  76. return '_';
  77. }
  78. if ((ptr = strchr(optstring, option)) == NULL) {
  79. if (_PyOS_opterr)
  80. fprintf(stderr, "Unknown option: -%c\n", option);
  81. return '_';
  82. }
  83. if (*(ptr + 1) == ':') {
  84. if (*opt_ptr != '\0') {
  85. _PyOS_optarg = opt_ptr;
  86. opt_ptr = "";
  87. }
  88. else {
  89. if (_PyOS_optind >= argc) {
  90. if (_PyOS_opterr)
  91. fprintf(stderr,
  92. "Argument expected for the -%c option\n", option);
  93. return '_';
  94. }
  95. _PyOS_optarg = argv[_PyOS_optind++];
  96. }
  97. }
  98. return option;
  99. }
  100. #ifdef __cplusplus
  101. }
  102. #endif