ยปCore Development>Code coverage>Python/getopt.c

Python code coverage for Python/getopt.c

#countcontent
1n/a/*---------------------------------------------------------------------------*
2n/a * <RCS keywords>
3n/a *
4n/a * C++ Library
5n/a *
6n/a * Copyright 1992-1994, David Gottner
7n/a *
8n/a * All Rights Reserved
9n/a *
10n/a * Permission to use, copy, modify, and distribute this software and its
11n/a * documentation for any purpose and without fee is hereby granted,
12n/a * provided that the above copyright notice, this permission notice and
13n/a * the following disclaimer notice appear unmodified in all copies.
14n/a *
15n/a * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16n/a * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I
17n/a * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
18n/a * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
19n/a * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20n/a * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21n/a *
22n/a * Nevertheless, I would like to know about bugs in this library or
23n/a * suggestions for improvment. Send bug reports and feedback to
24n/a * davegottner@delphi.com.
25n/a *---------------------------------------------------------------------------*/
26n/a
27n/a/* Modified to support --help and --version, as well as /? on Windows
28n/a * by Georg Brandl. */
29n/a
30n/a#include <Python.h>
31n/a#include <stdio.h>
32n/a#include <string.h>
33n/a#include <wchar.h>
34n/a#include <pygetopt.h>
35n/a
36n/a#ifdef __cplusplus
37n/aextern "C" {
38n/a#endif
39n/a
40n/aint _PyOS_opterr = 1; /* generate error messages */
41n/aint _PyOS_optind = 1; /* index into argv array */
42n/awchar_t *_PyOS_optarg = NULL; /* optional argument */
43n/a
44n/astatic wchar_t *opt_ptr = L"";
45n/a
46n/avoid _PyOS_ResetGetOpt(void)
47n/a{
48n/a _PyOS_opterr = 1;
49n/a _PyOS_optind = 1;
50n/a _PyOS_optarg = NULL;
51n/a opt_ptr = L"";
52n/a}
53n/a
54n/aint _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
55n/a{
56n/a wchar_t *ptr;
57n/a wchar_t option;
58n/a
59n/a if (*opt_ptr == '\0') {
60n/a
61n/a if (_PyOS_optind >= argc)
62n/a return -1;
63n/a#ifdef MS_WINDOWS
64n/a else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
65n/a ++_PyOS_optind;
66n/a return 'h';
67n/a }
68n/a#endif
69n/a
70n/a else if (argv[_PyOS_optind][0] != L'-' ||
71n/a argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
72n/a return -1;
73n/a
74n/a else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
75n/a ++_PyOS_optind;
76n/a return -1;
77n/a }
78n/a
79n/a else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
80n/a ++_PyOS_optind;
81n/a return 'h';
82n/a }
83n/a
84n/a else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
85n/a ++_PyOS_optind;
86n/a return 'V';
87n/a }
88n/a
89n/a
90n/a opt_ptr = &argv[_PyOS_optind++][1];
91n/a }
92n/a
93n/a if ((option = *opt_ptr++) == L'\0')
94n/a return -1;
95n/a
96n/a if (option == 'J') {
97n/a if (_PyOS_opterr)
98n/a fprintf(stderr, "-J is reserved for Jython\n");
99n/a return '_';
100n/a }
101n/a
102n/a if ((ptr = wcschr(optstring, option)) == NULL) {
103n/a if (_PyOS_opterr)
104n/a fprintf(stderr, "Unknown option: -%c\n", (char)option);
105n/a return '_';
106n/a }
107n/a
108n/a if (*(ptr + 1) == L':') {
109n/a if (*opt_ptr != L'\0') {
110n/a _PyOS_optarg = opt_ptr;
111n/a opt_ptr = L"";
112n/a }
113n/a
114n/a else {
115n/a if (_PyOS_optind >= argc) {
116n/a if (_PyOS_opterr)
117n/a fprintf(stderr,
118n/a "Argument expected for the -%c option\n", (char)option);
119n/a return '_';
120n/a }
121n/a
122n/a _PyOS_optarg = argv[_PyOS_optind++];
123n/a }
124n/a }
125n/a
126n/a return option;
127n/a}
128n/a
129n/a#ifdef __cplusplus
130n/a}
131n/a#endif
132n/a