Line data Source code
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 <pygetopt.h>
35 :
36 : #ifdef __cplusplus
37 : extern "C" {
38 : #endif
39 :
40 : int _PyOS_opterr = 1; /* generate error messages */
41 : int _PyOS_optind = 1; /* index into argv array */
42 : wchar_t *_PyOS_optarg = NULL; /* optional argument */
43 :
44 : static wchar_t *opt_ptr = L"";
45 :
46 0 : void _PyOS_ResetGetOpt(void)
47 : {
48 0 : _PyOS_opterr = 1;
49 0 : _PyOS_optind = 1;
50 0 : _PyOS_optarg = NULL;
51 0 : opt_ptr = L"";
52 0 : }
53 :
54 0 : int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
55 : {
56 : wchar_t *ptr;
57 : wchar_t option;
58 :
59 0 : if (*opt_ptr == '\0') {
60 :
61 0 : if (_PyOS_optind >= argc)
62 0 : return -1;
63 : #ifdef MS_WINDOWS
64 : else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
65 : ++_PyOS_optind;
66 : return 'h';
67 : }
68 : #endif
69 :
70 0 : else if (argv[_PyOS_optind][0] != L'-' ||
71 0 : argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
72 0 : return -1;
73 :
74 0 : else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
75 0 : ++_PyOS_optind;
76 0 : return -1;
77 : }
78 :
79 0 : else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
80 0 : ++_PyOS_optind;
81 0 : return 'h';
82 : }
83 :
84 0 : else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
85 0 : ++_PyOS_optind;
86 0 : return 'V';
87 : }
88 :
89 :
90 0 : opt_ptr = &argv[_PyOS_optind++][1];
91 : }
92 :
93 0 : if ( (option = *opt_ptr++) == L'\0')
94 0 : return -1;
95 :
96 0 : if (option == 'J') {
97 0 : fprintf(stderr, "-J is reserved for Jython\n");
98 0 : return '_';
99 : }
100 :
101 0 : if ((ptr = wcschr(optstring, option)) == NULL) {
102 0 : if (_PyOS_opterr)
103 0 : fprintf(stderr, "Unknown option: -%c\n", (char)option);
104 :
105 0 : return '_';
106 : }
107 :
108 0 : if (*(ptr + 1) == L':') {
109 0 : if (*opt_ptr != L'\0') {
110 0 : _PyOS_optarg = opt_ptr;
111 0 : opt_ptr = L"";
112 : }
113 :
114 : else {
115 0 : if (_PyOS_optind >= argc) {
116 0 : if (_PyOS_opterr)
117 0 : fprintf(stderr,
118 0 : "Argument expected for the -%c option\n", (char)option);
119 0 : return '_';
120 : }
121 :
122 0 : _PyOS_optarg = argv[_PyOS_optind++];
123 : }
124 : }
125 :
126 0 : return option;
127 : }
128 :
129 : #ifdef __cplusplus
130 : }
131 : #endif
132 :
|