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