2 This file is part of GNUnet.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file fs/fs_getopt.c
23 * @brief helper functions for command-line argument processing
24 * @author Igor Wronsky, Christian Grothoff
27 #include "gnunet_fs_service.h"
28 #include "gnunet_getopt_lib.h"
31 /* ******************** command-line option parsing API ******************** */
34 * Command-line option parser function that allows the user
35 * to specify one or more '-k' options with keywords. Each
36 * specified keyword will be added to the URI. A pointer to
37 * the URI must be passed as the "scls" argument.
39 * @param ctx command line processor context
40 * @param scls must be of type "struct GNUNET_FS_Uri **"
41 * @param option name of the option (typically 'k')
42 * @param value command line argument given
43 * @return #GNUNET_OK on success
46 getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
51 struct GNUNET_FS_Uri **uri = scls;
52 struct GNUNET_FS_Uri *u = *uri;
58 u = GNUNET_new (struct GNUNET_FS_Uri);
60 u->type = GNUNET_FS_URI_KSK;
61 u->data.ksk.keywordCount = 0;
62 u->data.ksk.keywords = NULL;
66 GNUNET_assert (GNUNET_FS_URI_KSK == u->type);
68 slen = strlen (value);
70 return GNUNET_SYSERR; /* cannot be empty */
73 /* simply preserve the "mandatory" flag */
75 return GNUNET_SYSERR; /* empty keywords not allowed */
76 if ((value[1] == '"') && (slen > 3) && (value[slen - 1] == '"'))
78 /* remove the quotes, keep the '+' */
79 val = GNUNET_malloc (slen - 1);
81 GNUNET_memcpy (&val[1],
88 /* no quotes, just keep the '+' */
89 val = GNUNET_strdup (value);
94 if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
96 /* remove the quotes, add a space */
97 val = GNUNET_malloc (slen);
99 GNUNET_memcpy (&val[1],
102 val[slen - 1] = '\0';
106 /* add a space to indicate "not mandatory" */
107 val = GNUNET_malloc (slen + 2);
112 GNUNET_array_append (u->data.ksk.keywords,
113 u->data.ksk.keywordCount,
120 * Allow user to specify keywords.
122 * @param shortName short name of the option
123 * @param name long name of the option
124 * @param argumentHelp help text for the option argument
125 * @param description long help text for the option
126 * @param[out] topKeywords set to the desired value
128 struct GNUNET_GETOPT_CommandLineOption
129 GNUNET_FS_GETOPT_KEYWORDS (char shortName,
131 const char *argumentHelp,
132 const char *description,
133 struct GNUNET_FS_Uri **topKeywords)
135 struct GNUNET_GETOPT_CommandLineOption clo = {
136 .shortName = shortName,
138 .argumentHelp = argumentHelp,
139 .description = description,
140 .require_argument = 1,
141 .processor = &getopt_set_keywords,
142 .scls = (void *) topKeywords
150 * Command-line option parser function that allows the user to specify
151 * one or more '-m' options with metadata. Each specified entry of
152 * the form "type=value" will be added to the metadata. A pointer to
153 * the metadata must be passed as the "scls" argument.
155 * @param ctx command line processor context
156 * @param scls must be of type "struct GNUNET_MetaData **"
157 * @param option name of the option (typically 'k')
158 * @param value command line argument given
159 * @return #GNUNET_OK on success
162 getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
167 struct GNUNET_CONTAINER_MetaData **mm = scls;
168 #if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
169 enum EXTRACTOR_MetaType type;
170 const char *typename;
171 const char *typename_i18n;
173 struct GNUNET_CONTAINER_MetaData *meta;
179 meta = GNUNET_CONTAINER_meta_data_create ();
183 /* Use GNUNET_STRINGS_get_utf8_args() in main() to acquire utf-8-encoded
184 * commandline arguments, so that the following line is not needed.
186 /*tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), locale_charset ());*/
187 tmp = GNUNET_strdup (value);
188 #if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
189 type = EXTRACTOR_metatype_get_max ();
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))))
199 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
200 EXTRACTOR_METAFORMAT_UTF8,
202 &tmp[strlen (typename) + 1],
203 strlen (&tmp[strlen (typename) + 1]) +
209 if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
210 (tmp[strlen (typename_i18n)] == ':') &&
211 (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
213 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
214 EXTRACTOR_METAFORMAT_UTF8,
216 &tmp[strlen (typename_i18n) + 1],
218 [strlen (typename_i18n) + 1]) +
229 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
230 EXTRACTOR_METATYPE_UNKNOWN,
231 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
232 tmp, strlen (tmp) + 1);
235 ("Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"),
242 * Allow user to specify metadata.
244 * @param shortName short name of the option
245 * @param name long name of the option
246 * @param argumentHelp help text for the option argument
247 * @param description long help text for the option
248 * @param[out] metadata set to the desired value
250 struct GNUNET_GETOPT_CommandLineOption
251 GNUNET_FS_GETOPT_METADATA (char shortName,
253 const char *argumentHelp,
254 const char *description,
255 struct GNUNET_CONTAINER_MetaData **meta)
257 struct GNUNET_GETOPT_CommandLineOption clo = {
258 .shortName = shortName,
260 .argumentHelp = argumentHelp,
261 .description = description,
262 .require_argument = 1,
263 .processor = &getopt_set_metadata,
264 .scls = (void *) meta
273 /* end of fs_getopt.c */