Fri, 29 May 2015 09:48:10 +0200
added initial code for config commands
11 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
75
56962faf2b42
copyright update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
74
diff
changeset
|
4 | * Copyright 2015 Olaf Wintermann. All rights reserved. |
11 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | ||
33 | #include "optparser.h" | |
34 | ||
35 | void cmd_args_free(CmdArgs *args) { | |
36 | ucx_map_free(args->options); | |
37 | free(args->argv); | |
74
da079dc0724c
fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
68
diff
changeset
|
38 | free(args); |
11 | 39 | } |
40 | ||
41 | CmdArgs* cmd_parse_args(int argc, char **argv) { | |
42 | CmdArgs *a = malloc(sizeof(CmdArgs)); | |
43 | a->options = ucx_map_new(16); | |
44 | a->argv = calloc(argc, sizeof(char*)); | |
45 | a->argc = 0; | |
46 | ||
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
47 | const char *NOARG = ""; |
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
48 | |
11 | 49 | char *option = NULL; |
50 | char optchar = 0; | |
51 | for(int i=0;i<argc;i++) { | |
52 | char *arg = argv[i]; | |
53 | size_t len = strlen(arg); | |
54 | if(len > 1 && arg[0] == '-') { | |
55 | for(int c=1;c<len;c++) { | |
56 | switch(arg[c]) { | |
57 | default: { | |
58 | fprintf(stderr, "Unknown option -%c", arg[c]); | |
59 | cmd_args_free(a); | |
60 | return NULL; | |
61 | } | |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
62 | case 'v': { |
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
63 | ucx_map_cstr_put(a->options, "verbose", NOARG); |
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
64 | break; |
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
65 | } |
11 | 66 | case 'k': { |
67 | if(!option) { | |
68 | option = "key"; | |
69 | optchar = 'k'; | |
70 | } else { | |
71 | fprintf( | |
72 | stderr, | |
73 | "Missing argument for option -%c\n", | |
74 | optchar); | |
75 | cmd_args_free(a); | |
76 | return NULL; | |
77 | } | |
78 | break; | |
79 | } | |
80 | case 'p': { | |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
81 | ucx_map_cstr_put(a->options, "plain", NOARG); |
11 | 82 | break; |
83 | } | |
45
e3839719b079
added crypt command line option
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
84 | case 'c': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
85 | ucx_map_cstr_put(a->options, "crypt", NOARG); |
45
e3839719b079
added crypt command line option
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
86 | break; |
e3839719b079
added crypt command line option
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
87 | } |
14
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
88 | case 'a': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
89 | ucx_map_cstr_put(a->options, "all", NOARG); |
14
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
90 | break; |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
91 | } |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
92 | case 'l': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
93 | ucx_map_cstr_put(a->options, "list", NOARG); |
14
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
94 | break; |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
95 | } |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
96 | case 't': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
97 | ucx_map_cstr_put(a->options, "type", NOARG); |
14
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
98 | break; |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
99 | } |
18
651989681053
recursive list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
100 | case 'R': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
101 | ucx_map_cstr_put(a->options, "recursive", NOARG); |
18
651989681053
recursive list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
102 | break; |
651989681053
recursive list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
103 | } |
11 | 104 | case 'o': { |
105 | if(!option) { | |
106 | option = "output"; | |
107 | optchar = 'o'; | |
108 | } else { | |
109 | fprintf( | |
110 | stderr, | |
111 | "Missing argument for option -%c\n", | |
112 | optchar); | |
113 | cmd_args_free(a); | |
114 | return NULL; | |
115 | } | |
116 | break; | |
117 | } | |
30
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
118 | case 'u': { |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
119 | if(!option) { |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
120 | option = "update"; |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
121 | optchar = 'u'; |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
122 | } else { |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
123 | fprintf( |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
124 | stderr, |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
125 | "Missing argument for option -%c\n", |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
126 | optchar); |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
127 | cmd_args_free(a); |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
128 | return NULL; |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
129 | } |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
130 | break; |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
131 | } |
62
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
132 | case 'n': { |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
133 | if(!option) { |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
134 | option = "namespace"; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
135 | optchar = 'n'; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
136 | } else { |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
137 | fprintf( |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
138 | stderr, |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
139 | "Missing argument for option -%c\n", |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
140 | optchar); |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
141 | cmd_args_free(a); |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
142 | return NULL; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
143 | } |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
144 | break; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
145 | } |
119
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
146 | case 'i': { |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
147 | ucx_map_cstr_put(a->options, "interactive", NOARG); |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
148 | break; |
451607eeff05
added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
149 | } |
11 | 150 | } |
151 | } | |
152 | } else if(option) { | |
153 | ucx_map_cstr_put(a->options, option, arg); | |
154 | option = NULL; | |
155 | } else { | |
156 | a->argv[a->argc++] = arg; | |
157 | } | |
158 | } | |
159 | ||
160 | return a; | |
161 | } | |
162 | ||
163 | char* cmd_getoption(CmdArgs *arg, char *name) { | |
164 | return ucx_map_cstr_get(arg->options, name); | |
165 | } |