blob: d80f60721e21d333a2a1c3d74fa124f4e06822b0 [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 Wouters2cffc7d2000-11-03 08:18:37 +000030int _PyOS_opterr = 1; /* generate error messages */
31int _PyOS_optind = 1; /* index into argv array */
32char *_PyOS_optarg = NULL; /* optional argument */
Guido van Rossum2508ade1994-04-14 14:08:22 +000033
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000034int _PyOS_GetOpt(int argc, char **argv, char *optstring)
Guido van Rossum2508ade1994-04-14 14:08:22 +000035{
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000036 static char *opt_ptr = "";
37 char *ptr;
38 int option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000039
Guido van Rossum871b8051994-04-28 12:33:58 +000040 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000041
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000042 if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' ||
43 argv[_PyOS_optind][1] == '\0' /* lone dash */ )
Guido van Rossum871b8051994-04-28 12:33:58 +000044 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000045
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000046 else if (strcmp(argv[_PyOS_optind], "--") == 0) {
47 ++_PyOS_optind;
Guido van Rossum871b8051994-04-28 12:33:58 +000048 return -1;
49 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000050
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000051 opt_ptr = &argv[_PyOS_optind++][1];
Guido van Rossum871b8051994-04-28 12:33:58 +000052 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000053
Guido van Rossumb4102bf1997-09-30 22:00:13 +000054 if ( (option = *opt_ptr++) == '\0')
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000055 return -1;
Guido van Rossumb4102bf1997-09-30 22:00:13 +000056
57 if ((ptr = strchr(optstring, option)) == NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000058 if (_PyOS_opterr)
Guido van Rossum871b8051994-04-28 12:33:58 +000059 fprintf(stderr, "Unknown option: -%c\n", option);
Guido van Rossum2508ade1994-04-14 14:08:22 +000060
Guido van Rossum871b8051994-04-28 12:33:58 +000061 return '?';
62 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000063
Guido van Rossum871b8051994-04-28 12:33:58 +000064 if (*(ptr + 1) == ':') {
65 if (*opt_ptr != '\0') {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000066 _PyOS_optarg = opt_ptr;
Guido van Rossum871b8051994-04-28 12:33:58 +000067 opt_ptr = "";
68 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000069
Guido van Rossum871b8051994-04-28 12:33:58 +000070 else {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000071 if (_PyOS_optind >= argc) {
72 if (_PyOS_opterr)
Guido van Rossum871b8051994-04-28 12:33:58 +000073 fprintf(stderr,
74 "Argument expected for the -%c option\n", option);
75 return '?';
76 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000077
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000078 _PyOS_optarg = argv[_PyOS_optind++];
Guido van Rossum871b8051994-04-28 12:33:58 +000079 }
80 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000081
Guido van Rossum871b8051994-04-28 12:33:58 +000082 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000083}