[2] | 1 | /*---------------------------------------------------------------------------*
|
---|
| 2 | * <RCS keywords>
|
---|
| 3 | *
|
---|
| 4 | * C++ Library
|
---|
| 5 | *
|
---|
| 6 | * Copyright 1992-1994, David Gottner
|
---|
| 7 | *
|
---|
| 8 | * All Rights Reserved
|
---|
| 9 | *
|
---|
[391] | 10 | * Permission to use, copy, modify, and distribute this software and its
|
---|
| 11 | * documentation for any purpose and without fee is hereby granted,
|
---|
[2] | 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 |
|
---|
| 27 | /* Modified to support --help and --version, as well as /? on Windows
|
---|
| 28 | * by Georg Brandl. */
|
---|
| 29 |
|
---|
| 30 | #include <stdio.h>
|
---|
| 31 | #include <string.h>
|
---|
| 32 |
|
---|
| 33 | #ifdef __cplusplus
|
---|
| 34 | extern "C" {
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | int _PyOS_opterr = 1; /* generate error messages */
|
---|
| 38 | int _PyOS_optind = 1; /* index into argv array */
|
---|
| 39 | char *_PyOS_optarg = NULL; /* optional argument */
|
---|
[391] | 40 | static char *opt_ptr = "";
|
---|
[2] | 41 |
|
---|
[391] | 42 | void _PyOS_ResetGetOpt(void)
|
---|
| 43 | {
|
---|
| 44 | _PyOS_opterr = 1;
|
---|
| 45 | _PyOS_optind = 1;
|
---|
| 46 | _PyOS_optarg = NULL;
|
---|
| 47 | opt_ptr = "";
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[2] | 50 | int _PyOS_GetOpt(int argc, char **argv, char *optstring)
|
---|
| 51 | {
|
---|
[391] | 52 | char *ptr;
|
---|
| 53 | int option;
|
---|
[2] | 54 |
|
---|
[391] | 55 | if (*opt_ptr == '\0') {
|
---|
[2] | 56 |
|
---|
[391] | 57 | if (_PyOS_optind >= argc)
|
---|
| 58 | return -1;
|
---|
[2] | 59 | #ifdef MS_WINDOWS
|
---|
[391] | 60 | else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
|
---|
| 61 | ++_PyOS_optind;
|
---|
| 62 | return 'h';
|
---|
| 63 | }
|
---|
[2] | 64 | #endif
|
---|
| 65 |
|
---|
[391] | 66 | else if (argv[_PyOS_optind][0] != '-' ||
|
---|
| 67 | argv[_PyOS_optind][1] == '\0' /* lone dash */ )
|
---|
| 68 | return -1;
|
---|
[2] | 69 |
|
---|
[391] | 70 | else if (strcmp(argv[_PyOS_optind], "--") == 0) {
|
---|
| 71 | ++_PyOS_optind;
|
---|
| 72 | return -1;
|
---|
| 73 | }
|
---|
[2] | 74 |
|
---|
[391] | 75 | else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
|
---|
| 76 | ++_PyOS_optind;
|
---|
| 77 | return 'h';
|
---|
| 78 | }
|
---|
[2] | 79 |
|
---|
[391] | 80 | else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
|
---|
| 81 | ++_PyOS_optind;
|
---|
| 82 | return 'V';
|
---|
| 83 | }
|
---|
[2] | 84 |
|
---|
| 85 |
|
---|
[391] | 86 | opt_ptr = &argv[_PyOS_optind++][1];
|
---|
| 87 | }
|
---|
[2] | 88 |
|
---|
[391] | 89 | if ((option = *opt_ptr++) == '\0')
|
---|
| 90 | return -1;
|
---|
[2] | 91 |
|
---|
[391] | 92 | if (option == 'J') {
|
---|
| 93 | if (_PyOS_opterr)
|
---|
| 94 | fprintf(stderr, "-J is reserved for Jython\n");
|
---|
| 95 | return '_';
|
---|
| 96 | }
|
---|
[2] | 97 |
|
---|
[391] | 98 | if (option == 'X') {
|
---|
| 99 | if (_PyOS_opterr)
|
---|
| 100 | fprintf(stderr,
|
---|
| 101 | "-X is reserved for implementation-specific arguments\n");
|
---|
| 102 | return '_';
|
---|
| 103 | }
|
---|
[2] | 104 |
|
---|
[391] | 105 | if ((ptr = strchr(optstring, option)) == NULL) {
|
---|
| 106 | if (_PyOS_opterr)
|
---|
| 107 | fprintf(stderr, "Unknown option: -%c\n", option);
|
---|
[2] | 108 |
|
---|
[391] | 109 | return '_';
|
---|
| 110 | }
|
---|
[2] | 111 |
|
---|
[391] | 112 | if (*(ptr + 1) == ':') {
|
---|
| 113 | if (*opt_ptr != '\0') {
|
---|
| 114 | _PyOS_optarg = opt_ptr;
|
---|
| 115 | opt_ptr = "";
|
---|
| 116 | }
|
---|
[2] | 117 |
|
---|
[391] | 118 | else {
|
---|
| 119 | if (_PyOS_optind >= argc) {
|
---|
| 120 | if (_PyOS_opterr)
|
---|
| 121 | fprintf(stderr,
|
---|
| 122 | "Argument expected for the -%c option\n", option);
|
---|
| 123 | return '_';
|
---|
| 124 | }
|
---|
[2] | 125 |
|
---|
[391] | 126 | _PyOS_optarg = argv[_PyOS_optind++];
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
[2] | 129 |
|
---|
[391] | 130 | return option;
|
---|
[2] | 131 | }
|
---|
| 132 |
|
---|
| 133 | #ifdef __cplusplus
|
---|
| 134 | }
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|