/src/Python-3.8.3/Python/getopt.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
27 | | /* Modified to support --help and --version, as well as /? on Windows |
28 | | * by Georg Brandl. */ |
29 | | |
30 | | #include <Python.h> |
31 | | #include <stdio.h> |
32 | | #include <string.h> |
33 | | #include <wchar.h> |
34 | | #include "pycore_getopt.h" |
35 | | |
36 | | #ifdef __cplusplus |
37 | | extern "C" { |
38 | | #endif |
39 | | |
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 */ |
43 | | |
44 | | static const wchar_t *opt_ptr = L""; |
45 | | |
46 | | /* Python command line short and long options */ |
47 | | |
48 | 0 | #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 | | |
56 | | void _PyOS_ResetGetOpt(void) |
57 | 0 | { |
58 | 0 | _PyOS_opterr = 1; |
59 | 0 | _PyOS_optind = 1; |
60 | 0 | _PyOS_optarg = NULL; |
61 | 0 | opt_ptr = L""; |
62 | 0 | } |
63 | | |
64 | | int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex) |
65 | 0 | { |
66 | 0 | wchar_t *ptr; |
67 | 0 | wchar_t option; |
68 | |
|
69 | 0 | if (*opt_ptr == '\0') { |
70 | |
|
71 | 0 | if (_PyOS_optind >= argc) |
72 | 0 | return -1; |
73 | | #ifdef MS_WINDOWS |
74 | | else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) { |
75 | | ++_PyOS_optind; |
76 | | return 'h'; |
77 | | } |
78 | | #endif |
79 | | |
80 | 0 | else if (argv[_PyOS_optind][0] != L'-' || |
81 | 0 | argv[_PyOS_optind][1] == L'\0' /* lone dash */ ) |
82 | 0 | return -1; |
83 | | |
84 | 0 | else if (wcscmp(argv[_PyOS_optind], L"--") == 0) { |
85 | 0 | ++_PyOS_optind; |
86 | 0 | return -1; |
87 | 0 | } |
88 | | |
89 | 0 | else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) { |
90 | 0 | ++_PyOS_optind; |
91 | 0 | return 'h'; |
92 | 0 | } |
93 | | |
94 | 0 | else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) { |
95 | 0 | ++_PyOS_optind; |
96 | 0 | return 'V'; |
97 | 0 | } |
98 | | |
99 | 0 | opt_ptr = &argv[_PyOS_optind++][1]; |
100 | 0 | } |
101 | | |
102 | 0 | if ((option = *opt_ptr++) == L'\0') |
103 | 0 | return -1; |
104 | | |
105 | 0 | if (option == L'-') { |
106 | | // Parse long option. |
107 | 0 | if (*opt_ptr == L'\0') { |
108 | 0 | if (_PyOS_opterr) { |
109 | 0 | fprintf(stderr, "expected long option\n"); |
110 | 0 | } |
111 | 0 | return -1; |
112 | 0 | } |
113 | 0 | *longindex = 0; |
114 | 0 | const _PyOS_LongOption *opt; |
115 | 0 | for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) { |
116 | 0 | if (!wcscmp(opt->name, opt_ptr)) |
117 | 0 | break; |
118 | 0 | } |
119 | 0 | if (!opt->name) { |
120 | 0 | if (_PyOS_opterr) { |
121 | 0 | fprintf(stderr, "unknown option %ls\n", argv[_PyOS_optind - 1]); |
122 | 0 | } |
123 | 0 | return '_'; |
124 | 0 | } |
125 | 0 | opt_ptr = L""; |
126 | 0 | if (!opt->has_arg) { |
127 | 0 | return opt->val; |
128 | 0 | } |
129 | 0 | if (_PyOS_optind >= argc) { |
130 | 0 | if (_PyOS_opterr) { |
131 | 0 | fprintf(stderr, "Argument expected for the %ls options\n", |
132 | 0 | argv[_PyOS_optind - 1]); |
133 | 0 | } |
134 | 0 | return '_'; |
135 | 0 | } |
136 | 0 | _PyOS_optarg = argv[_PyOS_optind++]; |
137 | 0 | return opt->val; |
138 | 0 | } |
139 | | |
140 | 0 | if (option == 'J') { |
141 | 0 | if (_PyOS_opterr) { |
142 | 0 | fprintf(stderr, "-J is reserved for Jython\n"); |
143 | 0 | } |
144 | 0 | return '_'; |
145 | 0 | } |
146 | | |
147 | 0 | if ((ptr = wcschr(SHORT_OPTS, option)) == NULL) { |
148 | 0 | if (_PyOS_opterr) { |
149 | 0 | fprintf(stderr, "Unknown option: -%c\n", (char)option); |
150 | 0 | } |
151 | 0 | return '_'; |
152 | 0 | } |
153 | | |
154 | 0 | if (*(ptr + 1) == L':') { |
155 | 0 | if (*opt_ptr != L'\0') { |
156 | 0 | _PyOS_optarg = opt_ptr; |
157 | 0 | opt_ptr = L""; |
158 | 0 | } |
159 | | |
160 | 0 | else { |
161 | 0 | if (_PyOS_optind >= argc) { |
162 | 0 | if (_PyOS_opterr) { |
163 | 0 | fprintf(stderr, |
164 | 0 | "Argument expected for the -%c option\n", (char)option); |
165 | 0 | } |
166 | 0 | return '_'; |
167 | 0 | } |
168 | | |
169 | 0 | _PyOS_optarg = argv[_PyOS_optind++]; |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | 0 | return option; |
174 | 0 | } |
175 | | |
176 | | #ifdef __cplusplus |
177 | | } |
178 | | #endif |
179 | | |