Skip to content
Newer
Older
100644 116 lines (100 sloc) 2.98 KB
8bee1b6 @trondn Refactor: Let the client specify the IO framework
trondn authored Sep 13, 2011
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3 * Copyright 2011 Couchbase, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "getopt.h"
19 #include <ctype.h>
20 #include <string.h>
21 #include <stdio.h>
22
23 char *optarg;
24 int opterr;
25 int optind;
26 int optopt;
27
28
29 static int parse_longopt(int argc, char **argv,
30 const struct option *longopts, int *longindex)
31 {
32 const struct option *p;
33 char *name = argv[optind] + 2;
34 if (*name == '\0') {
81d852e @trondn add a getopt test for win32
trondn authored Oct 7, 2011
35 ++optind;
8bee1b6 @trondn Refactor: Let the client specify the IO framework
trondn authored Sep 13, 2011
36 return -1;
37 }
38
39 optarg = strchr(name, '=');
40 if (optarg == NULL) {
41 // value comes next!
42 optarg = argv[optind + 1];
43 } else {
44 *optarg = '\0';
45 ++optarg;
46 }
47
48 for (p = longopts; p != NULL; ++p) {
49 if (strcmp(name, p->name) == 0) {
50 // This is it :)
51 if (p->has_arg) {
52 // we need a value!
53 if (optarg == argv[optind + 1]) {
54 ++optind;
55 }
81d852e @trondn add a getopt test for win32
trondn authored Oct 7, 2011
56 if (optarg == NULL || optind >= argc) {
57 fprintf(stderr, "%s: option requires an argument -- %s\n",
58 argv[0], name);
59 return '?';
8bee1b6 @trondn Refactor: Let the client specify the IO framework
trondn authored Sep 13, 2011
60 }
61
62 } else {
63 optarg = NULL;
64 }
65 return p->val;
66 }
67 }
68
69 return '?';
70 }
71
72
73 int getopt_long(int argc, char **argv, const char *optstring,
2b7cd18 @avsej make reformat
avsej authored Mar 5, 2012
74 const struct option *longopts, int *longindex)
75 {
8bee1b6 @trondn Refactor: Let the client specify the IO framework
trondn authored Sep 13, 2011
76
77 if (optind + 1 >= argc) {
78 // EOF
79 return -1;
80 }
81 ++optind;
82
83 if (argv[optind][0] != '-') {
84 return -1;
85 }
86
87 if (argv[optind][1] == '-') {
88 // this is a long option
89 return parse_longopt(argc, argv, longopts, longindex);
90 } else if (argv[optind][2] != '\0') {
91 fprintf(stderr, "You can't specify multiple options with this implementation\n");
81d852e @trondn add a getopt test for win32
trondn authored Oct 7, 2011
92 return '?';
8bee1b6 @trondn Refactor: Let the client specify the IO framework
trondn authored Sep 13, 2011
93 } else {
94 // this is a short option
95 const char *p = strchr(optstring, argv[optind][1]);
81d852e @trondn add a getopt test for win32
trondn authored Oct 7, 2011
96 int idx = optind;
97
8bee1b6 @trondn Refactor: Let the client specify the IO framework
trondn authored Sep 13, 2011
98 if (p == NULL) {
99 return '?';
100 }
101
102 if (*(p + 1) == ':') {
103 optind++;
104 optarg = argv[optind];
81d852e @trondn add a getopt test for win32
trondn authored Oct 7, 2011
105 if (optarg == NULL || optind >= argc) {
106 fprintf(stderr, "%s: option requires an argument -- %s\n",
107 argv[0], argv[idx] + 1);
108 return '?';
8bee1b6 @trondn Refactor: Let the client specify the IO framework
trondn authored Sep 13, 2011
109 }
110 } else {
111 optarg = NULL;
112 }
81d852e @trondn add a getopt test for win32
trondn authored Oct 7, 2011
113 return argv[idx][1];
8bee1b6 @trondn Refactor: Let the client specify the IO framework
trondn authored Sep 13, 2011
114 }
115 }
Something went wrong with that request. Please try again.