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