Thu, 17 Mar 2016 15:06:26 +0100
dav-sync add-directory shows list of repositories
11 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
185
cd42cccee550
updated copyright notice
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
182
diff
changeset
|
4 | * Copyright 2016 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 | } |
182
ca07f14f7bfe
added override option for copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
104 | case 'O': { |
ca07f14f7bfe
added override option for copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
105 | ucx_map_cstr_put(a->options, "override", NOARG); |
ca07f14f7bfe
added override option for copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
106 | break; |
ca07f14f7bfe
added override option for copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
107 | } |
199
f448fc8c9191
enabled cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
186
diff
changeset
|
108 | case 'i': { |
f448fc8c9191
enabled cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
186
diff
changeset
|
109 | ucx_map_cstr_put(a->options, "insecure", NOARG); |
f448fc8c9191
enabled cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
186
diff
changeset
|
110 | break; |
f448fc8c9191
enabled cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
186
diff
changeset
|
111 | } |
213
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
112 | case 'N': { |
212
01e80c7cfe96
added noinput option to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
209
diff
changeset
|
113 | ucx_map_cstr_put(a->options, "noinput", NOARG); |
01e80c7cfe96
added noinput option to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
209
diff
changeset
|
114 | break; |
01e80c7cfe96
added noinput option to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
209
diff
changeset
|
115 | } |
213
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
116 | case 'e': { |
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
117 | ucx_map_cstr_put(a->options, "extended", NOARG); |
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
118 | break; |
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
119 | } |
11 | 120 | case 'o': { |
121 | if(!option) { | |
122 | option = "output"; | |
123 | optchar = 'o'; | |
124 | } else { | |
125 | fprintf( | |
126 | stderr, | |
127 | "Missing argument for option -%c\n", | |
128 | optchar); | |
129 | cmd_args_free(a); | |
130 | return NULL; | |
131 | } | |
132 | break; | |
133 | } | |
30
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
134 | case 'u': { |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
135 | if(!option) { |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
136 | option = "update"; |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
137 | optchar = 'u'; |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
138 | } else { |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
139 | fprintf( |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
140 | stderr, |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
141 | "Missing argument for option -%c\n", |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
142 | optchar); |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
143 | cmd_args_free(a); |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
144 | return NULL; |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
145 | } |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
146 | break; |
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
147 | } |
62
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
148 | case 'n': { |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
149 | if(!option) { |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
150 | option = "namespace"; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
151 | optchar = 'n'; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
152 | } else { |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
153 | fprintf( |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
154 | stderr, |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
155 | "Missing argument for option -%c\n", |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
156 | optchar); |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
157 | cmd_args_free(a); |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
158 | return NULL; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
159 | } |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
160 | break; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
161 | } |
209
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
162 | case 'L': { |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
163 | if(!option) { |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
164 | option = "lock"; |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
165 | optchar = 'L'; |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
166 | } else { |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
167 | fprintf( |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
168 | stderr, |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
169 | "Missing argument for option -%c\n", |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
170 | optchar); |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
171 | cmd_args_free(a); |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
172 | return NULL; |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
173 | } |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
174 | break; |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
175 | } |
11 | 176 | } |
177 | } | |
178 | } else if(option) { | |
179 | ucx_map_cstr_put(a->options, option, arg); | |
180 | option = NULL; | |
181 | } else { | |
182 | a->argv[a->argc++] = arg; | |
183 | } | |
184 | } | |
185 | ||
186 | return a; | |
187 | } | |
188 | ||
189 | char* cmd_getoption(CmdArgs *arg, char *name) { | |
190 | return ucx_map_cstr_get(arg->options, name); | |
191 | } |