blob: 5429fac5ad5563eb30f4d6a063087dc876561f70 [file] [log] [blame]
Guido van Rossum871b8051994-04-28 12:33:58 +00001/*---------------------------------------------------------------------------*
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 *---------------------------------------------------------------------------*/
Guido van Rossum2508ade1994-04-14 14:08:22 +000026
27#include <stdio.h>
28#include <string.h>
29
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030#ifdef __cplusplus
31extern "C" {
32#endif
33
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000034int _PyOS_opterr = 1; /* generate error messages */
35int _PyOS_optind = 1; /* index into argv array */
36char *_PyOS_optarg = NULL; /* optional argument */
Guido van Rossum2508ade1994-04-14 14:08:22 +000037
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000038int _PyOS_GetOpt(int argc, char **argv, char *optstring)
Guido van Rossum2508ade1994-04-14 14:08:22 +000039{
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000040 static char *opt_ptr = "";
41 char *ptr;
42 int option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000043
Guido van Rossum871b8051994-04-28 12:33:58 +000044 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000045
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000046 if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' ||
47 argv[_PyOS_optind][1] == '\0' /* lone dash */ )
Guido van Rossum871b8051994-04-28 12:33:58 +000048 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000049
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000050 else if (strcmp(argv[_PyOS_optind], "--") == 0) {
51 ++_PyOS_optind;
Guido van Rossum871b8051994-04-28 12:33:58 +000052 return -1;
53 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000054
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000055 opt_ptr = &argv[_PyOS_optind++][1];
Guido van Rossum871b8051994-04-28 12:33:58 +000056 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000057
Guido van Rossumb4102bf1997-09-30 22:00:13 +000058 if ( (option = *opt_ptr++) == '\0')
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000059 return -1;
Guido van Rossumb4102bf1997-09-30 22:00:13 +000060
61 if ((ptr = strchr(optstring, option)) == NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000062 if (_PyOS_opterr)
Guido van Rossum871b8051994-04-28 12:33:58 +000063 fprintf(stderr, "Unknown option: -%c\n", option);
Guido van Rossum2508ade1994-04-14 14:08:22 +000064
Guido van Rossum871b8051994-04-28 12:33:58 +000065 return '?';
66 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000067
Guido van Rossum871b8051994-04-28 12:33:58 +000068 if (*(ptr + 1) == ':') {
69 if (*opt_ptr != '\0') {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000070 _PyOS_optarg = opt_ptr;
Guido van Rossum871b8051994-04-28 12:33:58 +000071 opt_ptr = "";
72 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000073
Guido van Rossum871b8051994-04-28 12:33:58 +000074 else {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000075 if (_PyOS_optind >= argc) {
76 if (_PyOS_opterr)
Guido van Rossum871b8051994-04-28 12:33:58 +000077 fprintf(stderr,
78 "Argument expected for the -%c option\n", option);
79 return '?';
80 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000081
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000082 _PyOS_optarg = argv[_PyOS_optind++];
Guido van Rossum871b8051994-04-28 12:33:58 +000083 }
84 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000085
Guido van Rossum871b8051994-04-28 12:33:58 +000086 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000087}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088
89#ifdef __cplusplus
90}
91#endif
92