aboutsummaryrefslogtreecommitdiff
path: root/src/fs/fs_getopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/fs_getopt.c')
-rw-r--r--src/fs/fs_getopt.c239
1 files changed, 120 insertions, 119 deletions
diff --git a/src/fs/fs_getopt.c b/src/fs/fs_getopt.c
index 0d5907a40..4f53f64a5 100644
--- a/src/fs/fs_getopt.c
+++ b/src/fs/fs_getopt.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file fs/fs_getopt.c 22 * @file fs/fs_getopt.c
@@ -43,10 +43,10 @@
43 * @return #GNUNET_OK on success 43 * @return #GNUNET_OK on success
44 */ 44 */
45static int 45static int
46getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 46getopt_set_keywords(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
47 void *scls, 47 void *scls,
48 const char *option, 48 const char *option,
49 const char *value) 49 const char *value)
50{ 50{
51 struct GNUNET_FS_Uri **uri = scls; 51 struct GNUNET_FS_Uri **uri = scls;
52 struct GNUNET_FS_Uri *u = *uri; 52 struct GNUNET_FS_Uri *u = *uri;
@@ -54,64 +54,64 @@ getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
54 size_t slen; 54 size_t slen;
55 55
56 if (NULL == u) 56 if (NULL == u)
57 { 57 {
58 u = GNUNET_new (struct GNUNET_FS_Uri); 58 u = GNUNET_new(struct GNUNET_FS_Uri);
59 *uri = u; 59 *uri = u;
60 u->type = GNUNET_FS_URI_KSK; 60 u->type = GNUNET_FS_URI_KSK;
61 u->data.ksk.keywordCount = 0; 61 u->data.ksk.keywordCount = 0;
62 u->data.ksk.keywords = NULL; 62 u->data.ksk.keywords = NULL;
63 } 63 }
64 else 64 else
65 { 65 {
66 GNUNET_assert (GNUNET_FS_URI_KSK == u->type); 66 GNUNET_assert(GNUNET_FS_URI_KSK == u->type);
67 } 67 }
68 slen = strlen (value); 68 slen = strlen(value);
69 if (0 == slen) 69 if (0 == slen)
70 return GNUNET_SYSERR; /* cannot be empty */ 70 return GNUNET_SYSERR; /* cannot be empty */
71 if (value[0] == '+') 71 if (value[0] == '+')
72 {
73 /* simply preserve the "mandatory" flag */
74 if (slen < 2)
75 return GNUNET_SYSERR; /* empty keywords not allowed */
76 if ((value[1] == '"') && (slen > 3) && (value[slen - 1] == '"'))
77 {
78 /* remove the quotes, keep the '+' */
79 val = GNUNET_malloc (slen - 1);
80 val[0] = '+';
81 GNUNET_memcpy (&val[1],
82 &value[2],
83 slen - 3);
84 val[slen - 2] = '\0';
85 }
86 else
87 { 72 {
88 /* no quotes, just keep the '+' */ 73 /* simply preserve the "mandatory" flag */
89 val = GNUNET_strdup (value); 74 if (slen < 2)
75 return GNUNET_SYSERR; /* empty keywords not allowed */
76 if ((value[1] == '"') && (slen > 3) && (value[slen - 1] == '"'))
77 {
78 /* remove the quotes, keep the '+' */
79 val = GNUNET_malloc(slen - 1);
80 val[0] = '+';
81 GNUNET_memcpy(&val[1],
82 &value[2],
83 slen - 3);
84 val[slen - 2] = '\0';
85 }
86 else
87 {
88 /* no quotes, just keep the '+' */
89 val = GNUNET_strdup(value);
90 }
90 } 91 }
91 }
92 else 92 else
93 {
94 if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
95 { 93 {
96 /* remove the quotes, add a space */ 94 if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
97 val = GNUNET_malloc (slen); 95 {
98 val[0] = ' '; 96 /* remove the quotes, add a space */
99 GNUNET_memcpy (&val[1], 97 val = GNUNET_malloc(slen);
100 &value[1], 98 val[0] = ' ';
101 slen - 2); 99 GNUNET_memcpy(&val[1],
102 val[slen - 1] = '\0'; 100 &value[1],
101 slen - 2);
102 val[slen - 1] = '\0';
103 }
104 else
105 {
106 /* add a space to indicate "not mandatory" */
107 val = GNUNET_malloc(slen + 2);
108 strcpy(val, " ");
109 strcat(val, value);
110 }
103 } 111 }
104 else 112 GNUNET_array_append(u->data.ksk.keywords,
105 { 113 u->data.ksk.keywordCount,
106 /* add a space to indicate "not mandatory" */ 114 val);
107 val = GNUNET_malloc (slen + 2);
108 strcpy (val, " ");
109 strcat (val, value);
110 }
111 }
112 GNUNET_array_append (u->data.ksk.keywords,
113 u->data.ksk.keywordCount,
114 val);
115 return GNUNET_OK; 115 return GNUNET_OK;
116} 116}
117 117
@@ -126,11 +126,11 @@ getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
126 * @param[out] topKeywords set to the desired value 126 * @param[out] topKeywords set to the desired value
127 */ 127 */
128struct GNUNET_GETOPT_CommandLineOption 128struct GNUNET_GETOPT_CommandLineOption
129GNUNET_FS_GETOPT_KEYWORDS (char shortName, 129GNUNET_FS_GETOPT_KEYWORDS(char shortName,
130 const char *name, 130 const char *name,
131 const char *argumentHelp, 131 const char *argumentHelp,
132 const char *description, 132 const char *description,
133 struct GNUNET_FS_Uri **topKeywords) 133 struct GNUNET_FS_Uri **topKeywords)
134{ 134{
135 struct GNUNET_GETOPT_CommandLineOption clo = { 135 struct GNUNET_GETOPT_CommandLineOption clo = {
136 .shortName = shortName, 136 .shortName = shortName,
@@ -139,7 +139,7 @@ GNUNET_FS_GETOPT_KEYWORDS (char shortName,
139 .description = description, 139 .description = description,
140 .require_argument = 1, 140 .require_argument = 1,
141 .processor = &getopt_set_keywords, 141 .processor = &getopt_set_keywords,
142 .scls = (void *) topKeywords 142 .scls = (void *)topKeywords
143 }; 143 };
144 144
145 return clo; 145 return clo;
@@ -159,12 +159,13 @@ GNUNET_FS_GETOPT_KEYWORDS (char shortName,
159 * @return #GNUNET_OK on success 159 * @return #GNUNET_OK on success
160 */ 160 */
161static int 161static int
162getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 162getopt_set_metadata(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
163 void *scls, 163 void *scls,
164 const char *option, 164 const char *option,
165 const char *value) 165 const char *value)
166{ 166{
167 struct GNUNET_CONTAINER_MetaData **mm = scls; 167 struct GNUNET_CONTAINER_MetaData **mm = scls;
168
168#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR 169#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
169 enum EXTRACTOR_MetaType type; 170 enum EXTRACTOR_MetaType type;
170 const char *typename; 171 const char *typename;
@@ -175,66 +176,66 @@ getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
175 176
176 meta = *mm; 177 meta = *mm;
177 if (meta == NULL) 178 if (meta == NULL)
178 { 179 {
179 meta = GNUNET_CONTAINER_meta_data_create (); 180 meta = GNUNET_CONTAINER_meta_data_create();
180 *mm = meta; 181 *mm = meta;
181 } 182 }
182 183
183 /* Use GNUNET_STRINGS_get_utf8_args() in main() to acquire utf-8-encoded 184 /* Use GNUNET_STRINGS_get_utf8_args() in main() to acquire utf-8-encoded
184 * commandline arguments, so that the following line is not needed. 185 * commandline arguments, so that the following line is not needed.
185 */ 186 */
186 /*tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), locale_charset ());*/ 187 /*tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), locale_charset ());*/
187 tmp = GNUNET_strdup (value); 188 tmp = GNUNET_strdup(value);
188#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR 189#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
189 type = EXTRACTOR_metatype_get_max (); 190 type = EXTRACTOR_metatype_get_max();
190 while (type > 0) 191 while (type > 0)
191 {
192 type--;
193 typename = EXTRACTOR_metatype_to_string (type);
194 typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
195 if ((strlen (tmp) >= strlen (typename) + 1) &&
196 (tmp[strlen (typename)] == ':') &&
197 (0 == strncmp (typename, tmp, strlen (typename))))
198 { 192 {
199 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type, 193 type--;
200 EXTRACTOR_METAFORMAT_UTF8, 194 typename = EXTRACTOR_metatype_to_string(type);
201 "text/plain", 195 typename_i18n = dgettext(LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
202 &tmp[strlen (typename) + 1], 196 if ((strlen(tmp) >= strlen(typename) + 1) &&
203 strlen (&tmp[strlen (typename) + 1]) + 197 (tmp[strlen(typename)] == ':') &&
204 1); 198 (0 == strncmp(typename, tmp, strlen(typename))))
205 GNUNET_free (tmp); 199 {
206 tmp = NULL; 200 GNUNET_CONTAINER_meta_data_insert(meta, "<gnunet>", type,
207 break; 201 EXTRACTOR_METAFORMAT_UTF8,
202 "text/plain",
203 &tmp[strlen(typename) + 1],
204 strlen(&tmp[strlen(typename) + 1]) +
205 1);
206 GNUNET_free(tmp);
207 tmp = NULL;
208 break;
209 }
210 if ((strlen(tmp) >= strlen(typename_i18n) + 1) &&
211 (tmp[strlen(typename_i18n)] == ':') &&
212 (0 == strncmp(typename_i18n, tmp, strlen(typename_i18n))))
213 {
214 GNUNET_CONTAINER_meta_data_insert(meta, "<gnunet>", type,
215 EXTRACTOR_METAFORMAT_UTF8,
216 "text/plain",
217 &tmp[strlen(typename_i18n) + 1],
218 strlen(&tmp
219 [strlen(typename_i18n) + 1]) +
220 1);
221 GNUNET_free(tmp);
222 tmp = NULL;
223 break;
224 }
208 } 225 }
209 if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
210 (tmp[strlen (typename_i18n)] == ':') &&
211 (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
212 {
213 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
214 EXTRACTOR_METAFORMAT_UTF8,
215 "text/plain",
216 &tmp[strlen (typename_i18n) + 1],
217 strlen (&tmp
218 [strlen (typename_i18n) + 1]) +
219 1);
220 GNUNET_free (tmp);
221 tmp = NULL;
222 break;
223 }
224 }
225#endif 226#endif
226 227
227 if (NULL != tmp) 228 if (NULL != tmp)
228 { 229 {
229 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", 230 GNUNET_CONTAINER_meta_data_insert(meta, "<gnunet>",
230 EXTRACTOR_METATYPE_UNKNOWN, 231 EXTRACTOR_METATYPE_UNKNOWN,
231 EXTRACTOR_METAFORMAT_UTF8, "text/plain", 232 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
232 tmp, strlen (tmp) + 1); 233 tmp, strlen(tmp) + 1);
233 GNUNET_free (tmp); 234 GNUNET_free(tmp);
234 printf (_ 235 printf(_
235 ("Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"), 236 ("Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"),
236 value); 237 value);
237 } 238 }
238 return GNUNET_OK; 239 return GNUNET_OK;
239} 240}
240 241
@@ -248,11 +249,11 @@ getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
248 * @param[out] metadata set to the desired value 249 * @param[out] metadata set to the desired value
249 */ 250 */
250struct GNUNET_GETOPT_CommandLineOption 251struct GNUNET_GETOPT_CommandLineOption
251GNUNET_FS_GETOPT_METADATA (char shortName, 252GNUNET_FS_GETOPT_METADATA(char shortName,
252 const char *name, 253 const char *name,
253 const char *argumentHelp, 254 const char *argumentHelp,
254 const char *description, 255 const char *description,
255 struct GNUNET_CONTAINER_MetaData **meta) 256 struct GNUNET_CONTAINER_MetaData **meta)
256{ 257{
257 struct GNUNET_GETOPT_CommandLineOption clo = { 258 struct GNUNET_GETOPT_CommandLineOption clo = {
258 .shortName = shortName, 259 .shortName = shortName,
@@ -261,7 +262,7 @@ GNUNET_FS_GETOPT_METADATA (char shortName,
261 .description = description, 262 .description = description,
262 .require_argument = 1, 263 .require_argument = 1,
263 .processor = &getopt_set_metadata, 264 .processor = &getopt_set_metadata,
264 .scls = (void *) meta 265 .scls = (void *)meta
265 }; 266 };
266 267
267 return clo; 268 return clo;