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 | f95a1b3 | 2010-05-09 15:52:27 +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 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 27 | /* Modified to support --help and --version, as well as /? on Windows |
| 28 | * by Georg Brandl. */ |
| 29 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 30 | #include <Python.h> |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <string.h> |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 33 | #include <wchar.h> |
Victor Stinner | 27e2d1f | 2018-11-01 00:52:28 +0100 | [diff] [blame] | 34 | #include "pycore_getopt.h" |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 35 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 40 | int _PyOS_opterr = 1; /* generate error messages */ |
| 41 | Py_ssize_t _PyOS_optind = 1; /* index into argv array */ |
| 42 | const wchar_t *_PyOS_optarg = NULL; /* optional argument */ |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 43 | |
Victor Stinner | 870b035 | 2019-05-17 03:15:12 +0200 | [diff] [blame] | 44 | static const wchar_t *opt_ptr = L""; |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 45 | |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 46 | /* Python command line short and long options */ |
| 47 | |
| 48 | #define SHORT_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?" |
| 49 | |
| 50 | static const _PyOS_LongOption longopts[] = { |
| 51 | {L"check-hash-based-pycs", 1, 0}, |
| 52 | {NULL, 0, 0}, |
| 53 | }; |
| 54 | |
| 55 | |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 56 | void _PyOS_ResetGetOpt(void) |
| 57 | { |
Ezio Melotti | a0dd22e | 2012-11-23 18:48:32 +0200 | [diff] [blame] | 58 | _PyOS_opterr = 1; |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 59 | _PyOS_optind = 1; |
| 60 | _PyOS_optarg = NULL; |
| 61 | opt_ptr = L""; |
| 62 | } |
| 63 | |
Victor Stinner | 870b035 | 2019-05-17 03:15:12 +0200 | [diff] [blame] | 64 | int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex) |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 65 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | wchar_t *ptr; |
| 67 | wchar_t option; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 68 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | if (*opt_ptr == '\0') { |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | if (_PyOS_optind >= argc) |
| 72 | return -1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 73 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) { |
| 75 | ++_PyOS_optind; |
| 76 | return 'h'; |
| 77 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | else if (argv[_PyOS_optind][0] != L'-' || |
| 81 | argv[_PyOS_optind][1] == L'\0' /* lone dash */ ) |
| 82 | return -1; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | else if (wcscmp(argv[_PyOS_optind], L"--") == 0) { |
| 85 | ++_PyOS_optind; |
| 86 | return -1; |
| 87 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 88 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) { |
| 90 | ++_PyOS_optind; |
| 91 | return 'h'; |
| 92 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) { |
| 95 | ++_PyOS_optind; |
| 96 | return 'V'; |
| 97 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 98 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | opt_ptr = &argv[_PyOS_optind++][1]; |
| 100 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 101 | |
Ezio Melotti | 7c66319 | 2012-11-18 13:55:52 +0200 | [diff] [blame] | 102 | if ((option = *opt_ptr++) == L'\0') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | return -1; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 104 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 105 | if (option == L'-') { |
| 106 | // Parse long option. |
| 107 | if (*opt_ptr == L'\0') { |
| 108 | fprintf(stderr, "expected long option\n"); |
| 109 | return -1; |
| 110 | } |
| 111 | *longindex = 0; |
| 112 | const _PyOS_LongOption *opt; |
| 113 | for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) { |
| 114 | if (!wcscmp(opt->name, opt_ptr)) |
| 115 | break; |
| 116 | } |
| 117 | if (!opt->name) { |
| 118 | fprintf(stderr, "unknown option %ls\n", argv[_PyOS_optind - 1]); |
| 119 | return '_'; |
| 120 | } |
| 121 | opt_ptr = L""; |
| 122 | if (!opt->has_arg) { |
| 123 | return opt->val; |
| 124 | } |
| 125 | if (_PyOS_optind >= argc) { |
| 126 | fprintf(stderr, "Argument expected for the %ls options\n", |
| 127 | argv[_PyOS_optind - 1]); |
| 128 | return '_'; |
| 129 | } |
| 130 | _PyOS_optarg = argv[_PyOS_optind++]; |
| 131 | return opt->val; |
| 132 | } |
| 133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | if (option == 'J') { |
Ezio Melotti | 7c66319 | 2012-11-18 13:55:52 +0200 | [diff] [blame] | 135 | if (_PyOS_opterr) |
| 136 | fprintf(stderr, "-J is reserved for Jython\n"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | return '_'; |
| 138 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 139 | |
Victor Stinner | 6dcb542 | 2019-03-05 02:44:12 +0100 | [diff] [blame] | 140 | if ((ptr = wcschr(SHORT_OPTS, option)) == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | if (_PyOS_opterr) |
Ezio Melotti | 7c66319 | 2012-11-18 13:55:52 +0200 | [diff] [blame] | 142 | fprintf(stderr, "Unknown option: -%c\n", (char)option); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | return '_'; |
| 144 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | if (*(ptr + 1) == L':') { |
| 147 | if (*opt_ptr != L'\0') { |
| 148 | _PyOS_optarg = opt_ptr; |
| 149 | opt_ptr = L""; |
| 150 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | else { |
| 153 | if (_PyOS_optind >= argc) { |
| 154 | if (_PyOS_opterr) |
| 155 | fprintf(stderr, |
| 156 | "Argument expected for the -%c option\n", (char)option); |
| 157 | return '_'; |
| 158 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | _PyOS_optarg = argv[_PyOS_optind++]; |
| 161 | } |
| 162 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | return option; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 165 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 166 | |
| 167 | #ifdef __cplusplus |
| 168 | } |
| 169 | #endif |
| 170 | |