Wed, 30 May 2018 16:25:57 +0200
introduces tag filter string parser (currently supporting a simple tag list)
11 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
364
3769ba002fd1
updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
355
diff
changeset
|
4 | * Copyright 2018 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" | |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
34 | #include "sopt.h" |
11 | 35 | |
36 | void cmd_args_free(CmdArgs *args) { | |
219
9792cfe63b80
added resolve-conflicts and delete-conflicts to help text and fixed crash with unknown cmd options
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
213
diff
changeset
|
37 | if(args) { |
9792cfe63b80
added resolve-conflicts and delete-conflicts to help text and fixed crash with unknown cmd options
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
213
diff
changeset
|
38 | ucx_map_free(args->options); |
9792cfe63b80
added resolve-conflicts and delete-conflicts to help text and fixed crash with unknown cmd options
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
213
diff
changeset
|
39 | free(args->argv); |
9792cfe63b80
added resolve-conflicts and delete-conflicts to help text and fixed crash with unknown cmd options
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
213
diff
changeset
|
40 | free(args); |
9792cfe63b80
added resolve-conflicts and delete-conflicts to help text and fixed crash with unknown cmd options
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
213
diff
changeset
|
41 | } |
11 | 42 | } |
43 | ||
44 | CmdArgs* cmd_parse_args(int argc, char **argv) { | |
45 | CmdArgs *a = malloc(sizeof(CmdArgs)); | |
46 | a->options = ucx_map_new(16); | |
47 | a->argv = calloc(argc, sizeof(char*)); | |
48 | a->argc = 0; | |
49 | ||
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
50 | const char *NOARG = ""; |
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
51 | |
11 | 52 | char *option = NULL; |
53 | char optchar = 0; | |
54 | for(int i=0;i<argc;i++) { | |
55 | char *arg = argv[i]; | |
56 | size_t len = strlen(arg); | |
57 | if(len > 1 && arg[0] == '-') { | |
58 | for(int c=1;c<len;c++) { | |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
59 | if (option) { |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
60 | fprintf(stderr, |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
61 | "Missing argument for option -%c\n\n", optchar); |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
62 | cmd_args_free(a); |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
63 | return NULL; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
64 | } |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
65 | |
11 | 66 | switch(arg[c]) { |
67 | default: { | |
219
9792cfe63b80
added resolve-conflicts and delete-conflicts to help text and fixed crash with unknown cmd options
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
213
diff
changeset
|
68 | fprintf(stderr, "Unknown option -%c\n\n", arg[c]); |
11 | 69 | cmd_args_free(a); |
70 | return NULL; | |
71 | } | |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
72 | case 'v': { |
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
73 | 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
|
74 | break; |
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
75 | } |
11 | 76 | case 'k': { |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
77 | option = "key"; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
78 | optchar = 'k'; |
11 | 79 | break; |
80 | } | |
81 | case 'p': { | |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
82 | ucx_map_cstr_put(a->options, "plain", NOARG); |
11 | 83 | break; |
84 | } | |
45
e3839719b079
added crypt command line option
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
85 | case 'c': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
86 | 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
|
87 | break; |
e3839719b079
added crypt command line option
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
88 | } |
14
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
89 | case 'a': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
90 | 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
|
91 | break; |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
92 | } |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
93 | case 'l': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
94 | 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
|
95 | break; |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
96 | } |
297
3cbe57eeaed1
adds option to sort dav list by lastmodified date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
97 | case 'd': { |
3cbe57eeaed1
adds option to sort dav list by lastmodified date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
98 | ucx_map_cstr_put(a->options, "date", NOARG); |
3cbe57eeaed1
adds option to sort dav list by lastmodified date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
99 | break; |
3cbe57eeaed1
adds option to sort dav list by lastmodified date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
100 | } |
14
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
101 | case 't': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
102 | 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
|
103 | break; |
d1a43035d3a2
added some options to list command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
11
diff
changeset
|
104 | } |
18
651989681053
recursive list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
105 | case 'R': { |
68
f6d3db6113d3
added verbose option + encryption flag fixes
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
106 | ucx_map_cstr_put(a->options, "recursive", NOARG); |
18
651989681053
recursive list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
107 | break; |
651989681053
recursive list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
108 | } |
182
ca07f14f7bfe
added override option for copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
109 | case 'O': { |
ca07f14f7bfe
added override option for copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
110 | 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
|
111 | break; |
ca07f14f7bfe
added override option for copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
119
diff
changeset
|
112 | } |
199
f448fc8c9191
enabled cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
186
diff
changeset
|
113 | case 'i': { |
f448fc8c9191
enabled cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
186
diff
changeset
|
114 | ucx_map_cstr_put(a->options, "insecure", NOARG); |
f448fc8c9191
enabled cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
186
diff
changeset
|
115 | break; |
f448fc8c9191
enabled cert verification
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
186
diff
changeset
|
116 | } |
213
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
117 | case 'N': { |
212
01e80c7cfe96
added noinput option to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
209
diff
changeset
|
118 | ucx_map_cstr_put(a->options, "noinput", NOARG); |
01e80c7cfe96
added noinput option to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
209
diff
changeset
|
119 | break; |
01e80c7cfe96
added noinput option to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
209
diff
changeset
|
120 | } |
213
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
121 | case 'e': { |
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
122 | 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
|
123 | break; |
cd870d5f8659
added options for extended list flags and changed version output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
212
diff
changeset
|
124 | } |
338
c7f3fe4abdb2
dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
334
diff
changeset
|
125 | case 'x': { |
c7f3fe4abdb2
dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
334
diff
changeset
|
126 | ucx_map_cstr_put(a->options, "xml", NOARG); |
c7f3fe4abdb2
dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
334
diff
changeset
|
127 | break; |
c7f3fe4abdb2
dav get-property supports xml property output
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
334
diff
changeset
|
128 | } |
309
8a474d2ad209
adds option to dav get for downloading only the directory structure
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
297
diff
changeset
|
129 | case 'S': { |
8a474d2ad209
adds option to dav get for downloading only the directory structure
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
297
diff
changeset
|
130 | // undocumented hidden feature |
8a474d2ad209
adds option to dav get for downloading only the directory structure
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
297
diff
changeset
|
131 | ucx_map_cstr_put(a->options, "structure", NOARG); |
8a474d2ad209
adds option to dav get for downloading only the directory structure
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
297
diff
changeset
|
132 | break; |
8a474d2ad209
adds option to dav get for downloading only the directory structure
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
297
diff
changeset
|
133 | } |
330
54819e984a19
adds dav get option to keep existing files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
315
diff
changeset
|
134 | case 'K': { |
54819e984a19
adds dav get option to keep existing files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
315
diff
changeset
|
135 | ucx_map_cstr_put(a->options, "keep", NOARG); |
54819e984a19
adds dav get option to keep existing files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
315
diff
changeset
|
136 | break; |
54819e984a19
adds dav get option to keep existing files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
315
diff
changeset
|
137 | } |
11 | 138 | case 'o': { |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
139 | option = "output"; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
140 | optchar = 'o'; |
11 | 141 | break; |
142 | } | |
30
9a5a7a513a96
added update option for list and get
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
18
diff
changeset
|
143 | case 'u': { |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
144 | option = "update"; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
145 | optchar = 'u'; |
30
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': { |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
149 | option = "namespace"; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
150 | optchar = 'n'; |
62
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
151 | break; |
3b813c57c106
added property-set and property-get command
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
45
diff
changeset
|
152 | } |
209
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
153 | case 'L': { |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
154 | option = "lock"; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
155 | optchar = 'L'; |
209
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
156 | break; |
d24761f1e07c
added lock and unlock commands to dav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
199
diff
changeset
|
157 | } |
315
7db4dbf4e6f9
adds lock timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
309
diff
changeset
|
158 | case 'T': { |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
159 | option = "timeout"; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
160 | optchar = 'T'; |
315
7db4dbf4e6f9
adds lock timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
309
diff
changeset
|
161 | break; |
7db4dbf4e6f9
adds lock timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
309
diff
changeset
|
162 | } |
355
5da2cf15eb44
adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
338
diff
changeset
|
163 | case 'F': { |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
164 | option = "progressfile"; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
165 | optchar = 'F'; |
355
5da2cf15eb44
adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
338
diff
changeset
|
166 | break; |
5da2cf15eb44
adds primitive progress indicator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
338
diff
changeset
|
167 | } |
11 | 168 | } |
169 | } | |
170 | } else if(option) { | |
171 | ucx_map_cstr_put(a->options, option, arg); | |
172 | option = NULL; | |
173 | } else { | |
174 | a->argv[a->argc++] = arg; | |
175 | } | |
176 | } | |
375
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
177 | if(option) { |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
178 | fprintf(stderr, |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
179 | "Missing argument for option -%c\n\n", optchar); |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
180 | cmd_args_free(a); |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
181 | return NULL; |
39fd327f81da
fixes options parser to correctly report missing arguments
Mike Becker <universe@uap-core.de>
parents:
364
diff
changeset
|
182 | } |
11 | 183 | |
184 | return a; | |
185 | } | |
186 | ||
187 | char* cmd_getoption(CmdArgs *arg, char *name) { | |
188 | return ucx_map_cstr_get(arg->options, name); | |
189 | } |