Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 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 | *---------------------------------------------------------------------------*/ |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 30 | int _PyOS_opterr = 1; /* generate error messages */ |
| 31 | int _PyOS_optind = 1; /* index into argv array */ |
| 32 | char *_PyOS_optarg = NULL; /* optional argument */ |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 34 | int _PyOS_GetOpt(int argc, char **argv, char *optstring) |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 35 | { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 36 | static char *opt_ptr = ""; |
| 37 | char *ptr; |
| 38 | int option; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 40 | if (*opt_ptr == '\0') { |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 42 | if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' || |
| 43 | argv[_PyOS_optind][1] == '\0' /* lone dash */ ) |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 44 | return -1; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 45 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 46 | else if (strcmp(argv[_PyOS_optind], "--") == 0) { |
| 47 | ++_PyOS_optind; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 48 | return -1; |
| 49 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 50 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 51 | opt_ptr = &argv[_PyOS_optind++][1]; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 52 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | b4102bf | 1997-09-30 22:00:13 +0000 | [diff] [blame] | 54 | if ( (option = *opt_ptr++) == '\0') |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 55 | return -1; |
Guido van Rossum | b4102bf | 1997-09-30 22:00:13 +0000 | [diff] [blame] | 56 | |
| 57 | if ((ptr = strchr(optstring, option)) == NULL) { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 58 | if (_PyOS_opterr) |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 59 | fprintf(stderr, "Unknown option: -%c\n", option); |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 61 | return '?'; |
| 62 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 64 | if (*(ptr + 1) == ':') { |
| 65 | if (*opt_ptr != '\0') { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 66 | _PyOS_optarg = opt_ptr; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 67 | opt_ptr = ""; |
| 68 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 70 | else { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 71 | if (_PyOS_optind >= argc) { |
| 72 | if (_PyOS_opterr) |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 73 | fprintf(stderr, |
| 74 | "Argument expected for the -%c option\n", option); |
| 75 | return '?'; |
| 76 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 78 | _PyOS_optarg = argv[_PyOS_optind++]; |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 871b805 | 1994-04-28 12:33:58 +0000 | [diff] [blame] | 82 | return option; |
Guido van Rossum | 2508ade | 1994-04-14 14:08:22 +0000 | [diff] [blame] | 83 | } |