[12589] | 1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
| 2 | * |
---|
| 3 | * Permission to use, copy, modify, and distribute this |
---|
| 4 | * software and its documentation for any purpose and without |
---|
| 5 | * fee is hereby granted, provided that the above copyright |
---|
| 6 | * notice appear in all copies and that both that copyright |
---|
| 7 | * notice and this permission notice appear in supporting |
---|
| 8 | * documentation, and that the name of M.I.T. not be used in |
---|
| 9 | * advertising or publicity pertaining to distribution of the |
---|
| 10 | * software without specific, written prior permission. |
---|
| 11 | * M.I.T. makes no representations about the suitability of |
---|
| 12 | * this software for any purpose. It is provided "as is" |
---|
| 13 | * without express or implied warranty. |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | /* This file defines attach_getopt, a version of getopt with support |
---|
| 17 | * for long arguments in two different styles. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | static const char rcsid[] = "$Id: agetopt.c,v 1.1 1999-02-26 23:12:58 danw Exp $"; |
---|
| 21 | |
---|
| 22 | #include <stdio.h> |
---|
| 23 | #include <string.h> |
---|
| 24 | |
---|
| 25 | #include "agetopt.h" |
---|
| 26 | |
---|
| 27 | extern char *whoami; |
---|
| 28 | |
---|
| 29 | int attach_getopt(int argc, char **argv, struct agetopt_option *options) |
---|
| 30 | { |
---|
| 31 | int i; |
---|
| 32 | char *name; |
---|
| 33 | static char *last = NULL; |
---|
| 34 | |
---|
| 35 | if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') |
---|
| 36 | return -1; |
---|
| 37 | |
---|
| 38 | if (!strcmp(argv[optind], "--")) |
---|
| 39 | { |
---|
| 40 | optind++; |
---|
| 41 | return -1; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /* Remaining possibilities: |
---|
| 45 | * Long arg name: --foo |
---|
| 46 | * Short arg name: -f |
---|
| 47 | * Short arg name with data: -foo |
---|
| 48 | * Old-style long arg name: -foo |
---|
| 49 | * Short args concatenated: -foo |
---|
| 50 | * |
---|
| 51 | * Too bad we can't necessarily tell the last few apart. We assume |
---|
| 52 | * that anything that matches a long arg is a long arg, not a |
---|
| 53 | * bunch of short args. Also, if we're in the midst of parsing |
---|
| 54 | * concatenated short args, we can't switch to long args in mid word. |
---|
| 55 | */ |
---|
| 56 | |
---|
| 57 | if (last) |
---|
| 58 | { |
---|
| 59 | /* We are parsing concatenated short args. Pick up where we |
---|
| 60 | * left off. |
---|
| 61 | */ |
---|
| 62 | name = last; |
---|
| 63 | } |
---|
| 64 | else |
---|
| 65 | { |
---|
| 66 | /* This is a new argument. Figure out what kind. */ |
---|
| 67 | int doubledash = 0; |
---|
| 68 | |
---|
| 69 | name = argv[optind] + 1; |
---|
| 70 | if (*name == '-') |
---|
| 71 | { |
---|
| 72 | doubledash = 1; |
---|
| 73 | name++; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /* Look for a long arg. */ |
---|
| 77 | for (i = 0; options[i].longname; i++) |
---|
| 78 | { |
---|
| 79 | if (!strcmp(name, options[i].longname)) |
---|
| 80 | { |
---|
| 81 | #ifdef DEPRECATE_SINGLEDASH |
---|
| 82 | if (!doubledash) |
---|
| 83 | { |
---|
| 84 | fprintf(stderr, "%s: -%s is deprecated. Use --%s instead.\n", |
---|
| 85 | whoami, name, name); |
---|
| 86 | } |
---|
| 87 | #endif |
---|
| 88 | optarg = argv[optind + 1]; |
---|
| 89 | optind += 1 + options[i].arg; |
---|
| 90 | return options[i].shortname; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /* If we had a "--option" and didn't match any long arg, we lose. */ |
---|
| 95 | if (doubledash) |
---|
| 96 | return '?'; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* Look for a short arg. */ |
---|
| 100 | for (i = 0; options[i].longname; i++) |
---|
| 101 | { |
---|
| 102 | if (*name == options[i].shortname) |
---|
| 103 | { |
---|
| 104 | if (*(name + 1)) |
---|
| 105 | { |
---|
| 106 | if (options[i].arg) |
---|
| 107 | { |
---|
| 108 | /* Option argument is rest of argv[optind]. */ |
---|
| 109 | optarg = name + 1; |
---|
| 110 | last = NULL; |
---|
| 111 | optind++; |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | { |
---|
| 115 | /* There are more options to decode in argv[optind]. */ |
---|
| 116 | last = name + 1; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | else |
---|
| 120 | { |
---|
| 121 | /* Done with argv[optind]. Move on. */ |
---|
| 122 | last = NULL; |
---|
| 123 | optarg = argv[optind + 1]; |
---|
| 124 | optind += 1 + options[i].arg; |
---|
| 125 | } |
---|
| 126 | return options[i].shortname; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* No match. */ |
---|
| 131 | return '?'; |
---|
| 132 | } |
---|