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/>.
20 * @file fs/fs_getopt.c
21 * @brief helper functions for command-line argument processing
22 * @author Igor Wronsky, Christian Grothoff
25 #include "gnunet_fs_service.h"
26 #include "gnunet_getopt_lib.h"
29 /* ******************** command-line option parsing API ******************** */
32 * Command-line option parser function that allows the user
33 * to specify one or more '-k' options with keywords. Each
34 * specified keyword will be added to the URI. A pointer to
35 * the URI must be passed as the "scls" argument.
37 * @param ctx command line processor context
38 * @param scls must be of type "struct GNUNET_FS_Uri **"
39 * @param option name of the option (typically 'k')
40 * @param value command line argument given
41 * @return #GNUNET_OK on success
44 getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
49 struct GNUNET_FS_Uri **uri = scls;
50 struct GNUNET_FS_Uri *u = *uri;
56 u = GNUNET_new (struct GNUNET_FS_Uri);
58 u->type = GNUNET_FS_URI_KSK;
59 u->data.ksk.keywordCount = 0;
60 u->data.ksk.keywords = NULL;
64 GNUNET_assert (GNUNET_FS_URI_KSK == u->type);
66 slen = strlen (value);
68 return GNUNET_SYSERR; /* cannot be empty */
71 /* simply preserve the "mandatory" flag */
73 return GNUNET_SYSERR; /* empty keywords not allowed */
74 if ((value[1] == '"') && (slen > 3) && (value[slen - 1] == '"'))
76 /* remove the quotes, keep the '+' */
77 val = GNUNET_malloc (slen - 1);
79 GNUNET_memcpy (&val[1],
86 /* no quotes, just keep the '+' */
87 val = GNUNET_strdup (value);
92 if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
94 /* remove the quotes, add a space */
95 val = GNUNET_malloc (slen);
97 GNUNET_memcpy (&val[1],
100 val[slen - 1] = '\0';
104 /* add a space to indicate "not mandatory" */
105 val = GNUNET_malloc (slen + 2);
110 GNUNET_array_append (u->data.ksk.keywords,
111 u->data.ksk.keywordCount,
118 * Allow user to specify keywords.
120 * @param shortName short name of the option
121 * @param name long name of the option
122 * @param argumentHelp help text for the option argument
123 * @param description long help text for the option
124 * @param[out] topKeywords set to the desired value
126 struct GNUNET_GETOPT_CommandLineOption
127 GNUNET_FS_GETOPT_KEYWORDS (char shortName,
129 const char *argumentHelp,
130 const char *description,
131 struct GNUNET_FS_Uri **topKeywords)
133 struct GNUNET_GETOPT_CommandLineOption clo = {
134 .shortName = shortName,
136 .argumentHelp = argumentHelp,
137 .description = description,
138 .require_argument = 1,
139 .processor = &getopt_set_keywords,
140 .scls = (void *) topKeywords
148 * Command-line option parser function that allows the user to specify
149 * one or more '-m' options with metadata. Each specified entry of
150 * the form "type=value" will be added to the metadata. A pointer to
151 * the metadata must be passed as the "scls" argument.
153 * @param ctx command line processor context
154 * @param scls must be of type "struct GNUNET_MetaData **"
155 * @param option name of the option (typically 'k')
156 * @param value command line argument given
157 * @return #GNUNET_OK on success
160 getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
165 struct GNUNET_CONTAINER_MetaData **mm = scls;
166 #if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
167 enum EXTRACTOR_MetaType type;
168 const char *typename;
169 const char *typename_i18n;
171 struct GNUNET_CONTAINER_MetaData *meta;
177 meta = GNUNET_CONTAINER_meta_data_create ();
181 /* Use GNUNET_STRINGS_get_utf8_args() in main() to acquire utf-8-encoded
182 * commandline arguments, so that the following line is not needed.
184 /*tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), locale_charset ());*/
185 tmp = GNUNET_strdup (value);
186 #if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
187 type = EXTRACTOR_metatype_get_max ();
191 typename = EXTRACTOR_metatype_to_string (type);
192 typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
193 if ((strlen (tmp) >= strlen (typename) + 1) &&
194 (tmp[strlen (typename)] == ':') &&
195 (0 == strncmp (typename, tmp, strlen (typename))))
197 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
198 EXTRACTOR_METAFORMAT_UTF8,
200 &tmp[strlen (typename) + 1],
201 strlen (&tmp[strlen (typename) + 1]) +
207 if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
208 (tmp[strlen (typename_i18n)] == ':') &&
209 (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
211 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
212 EXTRACTOR_METAFORMAT_UTF8,
214 &tmp[strlen (typename_i18n) + 1],
216 [strlen (typename_i18n) + 1]) +
227 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
228 EXTRACTOR_METATYPE_UNKNOWN,
229 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
230 tmp, strlen (tmp) + 1);
233 ("Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"),
240 * Allow user to specify metadata.
242 * @param shortName short name of the option
243 * @param name long name of the option
244 * @param argumentHelp help text for the option argument
245 * @param description long help text for the option
246 * @param[out] metadata set to the desired value
248 struct GNUNET_GETOPT_CommandLineOption
249 GNUNET_FS_GETOPT_METADATA (char shortName,
251 const char *argumentHelp,
252 const char *description,
253 struct GNUNET_CONTAINER_MetaData **meta)
255 struct GNUNET_GETOPT_CommandLineOption clo = {
256 .shortName = shortName,
258 .argumentHelp = argumentHelp,
259 .description = description,
260 .require_argument = 1,
261 .processor = &getopt_set_metadata,
262 .scls = (void *) meta
271 /* end of fs_getopt.c */