/home/mdboom/Work/builds/cpython/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 | |
23 | /* Modified to support --help and --version, as well as /? on Windows |
24 | * by Georg Brandl. */ |
25 | |
26 | #include <Python.h> |
27 | #include <stdio.h> |
28 | #include <string.h> |
29 | #include <wchar.h> |
30 | #include "pycore_getopt.h" |
31 | |
32 | #ifdef __cplusplus |
33 | extern "C" { |
34 | #endif |
35 | |
36 | int _PyOS_opterr = 1; /* generate error messages */ |
37 | Py_ssize_t _PyOS_optind = 1; /* index into argv array */ |
38 | const wchar_t *_PyOS_optarg = NULL; /* optional argument */ |
39 | |
40 | static const wchar_t *opt_ptr = L""; |
41 | |
42 | /* Python command line short and long options */ |
43 | |
44 | #define SHORT_OPTS L"bBc:dEhiIJm:OPqRsStuvVW:xX:?" |
45 | |
46 | static const _PyOS_LongOption longopts[] = { |
47 | /* name, has_arg, val (used in switch in initconfig.c) */ |
48 | {L"check-hash-based-pycs", 1, 0}, |
49 | {L"help-all", 0, 1}, |
50 | {L"help-env", 0, 2}, |
51 | {L"help-xoptions", 0, 3}, |
52 | {NULL, 0, -1}, /* sentinel */ |
53 | }; |
54 | |
55 | |
56 | void _PyOS_ResetGetOpt(void) |
57 | { |
58 | _PyOS_opterr = 1; |
59 | _PyOS_optind = 1; |
60 | _PyOS_optarg = NULL; |
61 | opt_ptr = L""; |
62 | } |
63 | |
64 | int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex) |
65 | { |
66 | wchar_t *ptr; |
67 | wchar_t option; |
68 | |
69 | if (*opt_ptr == '\0') { Branch (69:9): [True: 181, False: 0]
|
70 | |
71 | if (_PyOS_optind >= argc) Branch (71:13): [True: 41, False: 140]
|
72 | 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 | else if (argv[_PyOS_optind][0] != L'-' || Branch (80:18): [True: 6, False: 134]
|
81 | argv[_PyOS_optind][1] == L'\0'134 /* lone dash */ ) Branch (81:18): [True: 0, False: 134]
|
82 | return -1; |
83 | |
84 | else if (wcscmp(argv[_PyOS_optind], L"--") == 0) { Branch (84:18): [True: 0, False: 134]
|
85 | ++_PyOS_optind; |
86 | return -1; |
87 | } |
88 | |
89 | else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) { Branch (89:18): [True: 0, False: 134]
|
90 | ++_PyOS_optind; |
91 | return 'h'; |
92 | } |
93 | |
94 | else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) { Branch (94:18): [True: 0, False: 134]
|
95 | ++_PyOS_optind; |
96 | return 'V'; |
97 | } |
98 | |
99 | opt_ptr = &argv[_PyOS_optind++][1]; |
100 | } |
101 | |
102 | if ((option = *opt_ptr++) == L'\0') Branch (102:9): [True: 0, False: 134]
|
103 | return -1; |
104 | |
105 | if (option == L'-') { Branch (105:9): [True: 0, False: 134]
|
106 | // Parse long option. |
107 | if (*opt_ptr == L'\0') { Branch (107:13): [True: 0, False: 0]
|
108 | if (_PyOS_opterr) { Branch (108:17): [True: 0, False: 0]
|
109 | fprintf(stderr, "expected long option\n"); |
110 | } |
111 | return -1; |
112 | } |
113 | *longindex = 0; |
114 | const _PyOS_LongOption *opt; |
115 | for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) { Branch (115:43): [True: 0, False: 0]
|
116 | if (!wcscmp(opt->name, opt_ptr)) Branch (116:17): [True: 0, False: 0]
|
117 | break; |
118 | } |
119 | if (!opt->name) { Branch (119:13): [True: 0, False: 0]
|
120 | if (_PyOS_opterr) { Branch (120:17): [True: 0, False: 0]
|
121 | fprintf(stderr, "unknown option %ls\n", argv[_PyOS_optind - 1]); |
122 | } |
123 | return '_'; |
124 | } |
125 | opt_ptr = L""; |
126 | if (!opt->has_arg) { Branch (126:13): [True: 0, False: 0]
|
127 | return opt->val; |
128 | } |
129 | if (_PyOS_optind >= argc) { Branch (129:13): [True: 0, False: 0]
|
130 | if (_PyOS_opterr) { Branch (130:17): [True: 0, False: 0]
|
131 | fprintf(stderr, "Argument expected for the %ls options\n", |
132 | argv[_PyOS_optind - 1]); |
133 | } |
134 | return '_'; |
135 | } |
136 | _PyOS_optarg = argv[_PyOS_optind++]; |
137 | return opt->val; |
138 | } |
139 | |
140 | if (option == 'J') { Branch (140:9): [True: 0, False: 134]
|
141 | if (_PyOS_opterr) { Branch (141:13): [True: 0, False: 0]
|
142 | fprintf(stderr, "-J is reserved for Jython\n"); |
143 | } |
144 | return '_'; |
145 | } |
146 | |
147 | if ((ptr = wcschr(SHORT_OPTS, option)) == NULL) { Branch (147:9): [True: 0, False: 134]
|
148 | if (_PyOS_opterr) { Branch (148:13): [True: 0, False: 0]
|
149 | fprintf(stderr, "Unknown option: -%c\n", (char)option); |
150 | } |
151 | return '_'; |
152 | } |
153 | |
154 | if (*(ptr + 1) == L':') { Branch (154:9): [True: 125, False: 9]
|
155 | if (*opt_ptr != L'\0') { Branch (155:13): [True: 4, False: 121]
|
156 | _PyOS_optarg = opt_ptr; |
157 | opt_ptr = L""; |
158 | } |
159 | |
160 | else { |
161 | if (_PyOS_optind >= argc) { Branch (161:17): [True: 0, False: 121]
|
162 | if (_PyOS_opterr) { Branch (162:21): [True: 0, False: 0]
|
163 | fprintf(stderr, |
164 | "Argument expected for the -%c option\n", (char)option); |
165 | } |
166 | return '_'; |
167 | } |
168 | |
169 | _PyOS_optarg = argv[_PyOS_optind++]; |
170 | } |
171 | } |
172 | |
173 | return option; |
174 | } |
175 | |
176 | #ifdef __cplusplus |
177 | } |
178 | #endif |
179 | |