• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "internal/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 
_PyOS_ResetGetOpt(void)46 void _PyOS_ResetGetOpt(void)
47 {
48     _PyOS_opterr = 1;
49     _PyOS_optind = 1;
50     _PyOS_optarg = NULL;
51     opt_ptr = L"";
52 }
53 
_PyOS_GetOpt(int argc,wchar_t ** argv,wchar_t * optstring,const _PyOS_LongOption * longopts,int * longindex)54 int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring,
55                  const _PyOS_LongOption *longopts, int *longindex)
56 {
57     wchar_t *ptr;
58     wchar_t option;
59 
60     if (*opt_ptr == '\0') {
61 
62         if (_PyOS_optind >= argc)
63             return -1;
64 #ifdef MS_WINDOWS
65         else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
66             ++_PyOS_optind;
67             return 'h';
68         }
69 #endif
70 
71         else if (argv[_PyOS_optind][0] != L'-' ||
72                  argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
73             return -1;
74 
75         else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
76             ++_PyOS_optind;
77             return -1;
78         }
79 
80         else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
81             ++_PyOS_optind;
82             return 'h';
83         }
84 
85         else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
86             ++_PyOS_optind;
87             return 'V';
88         }
89 
90         opt_ptr = &argv[_PyOS_optind++][1];
91     }
92 
93     if ((option = *opt_ptr++) == L'\0')
94         return -1;
95 
96     if (option == L'-') {
97         // Parse long option.
98         if (*opt_ptr == L'\0') {
99             fprintf(stderr, "expected long option\n");
100             return -1;
101         }
102         *longindex = 0;
103         const _PyOS_LongOption *opt;
104         for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) {
105             if (!wcscmp(opt->name, opt_ptr))
106                 break;
107         }
108         if (!opt->name) {
109             fprintf(stderr, "unknown option %ls\n", argv[_PyOS_optind - 1]);
110             return '_';
111         }
112         opt_ptr = L"";
113         if (!opt->has_arg) {
114             return opt->val;
115         }
116         if (_PyOS_optind >= argc) {
117             fprintf(stderr, "Argument expected for the %ls options\n",
118                     argv[_PyOS_optind - 1]);
119             return '_';
120         }
121         _PyOS_optarg = argv[_PyOS_optind++];
122         return opt->val;
123     }
124 
125     if (option == 'J') {
126         if (_PyOS_opterr)
127             fprintf(stderr, "-J is reserved for Jython\n");
128         return '_';
129     }
130 
131     if ((ptr = wcschr(optstring, option)) == NULL) {
132         if (_PyOS_opterr)
133             fprintf(stderr, "Unknown option: -%c\n", (char)option);
134         return '_';
135     }
136 
137     if (*(ptr + 1) == L':') {
138         if (*opt_ptr != L'\0') {
139             _PyOS_optarg  = opt_ptr;
140             opt_ptr = L"";
141         }
142 
143         else {
144             if (_PyOS_optind >= argc) {
145                 if (_PyOS_opterr)
146                     fprintf(stderr,
147                         "Argument expected for the -%c option\n", (char)option);
148                 return '_';
149             }
150 
151             _PyOS_optarg = argv[_PyOS_optind++];
152         }
153     }
154 
155     return option;
156 }
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 
162