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