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
45 *ctx, void *scls, const char *option,
48 struct GNUNET_FS_Uri **uri = scls;
49 struct GNUNET_FS_Uri *u = *uri;
55 u = GNUNET_new (struct GNUNET_FS_Uri);
57 u->type = GNUNET_FS_URI_KSK;
58 u->data.ksk.keywordCount = 0;
59 u->data.ksk.keywords = NULL;
63 GNUNET_assert (u->type == GNUNET_FS_URI_KSK);
65 slen = strlen (value);
67 return GNUNET_SYSERR; /* cannot be empty */
70 /* simply preserve the "mandatory" flag */
72 return GNUNET_SYSERR; /* empty keywords not allowed */
73 if ((value[1] == '"') && (slen > 3) && (value[slen - 1] == '"'))
75 /* remove the quotes, keep the '+' */
76 val = GNUNET_malloc (slen - 1);
78 GNUNET_memcpy (&val[1], &value[2], slen - 3);
83 /* no quotes, just keep the '+' */
84 val = GNUNET_strdup (value);
89 if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
91 /* remove the quotes, add a space */
92 val = GNUNET_malloc (slen);
94 GNUNET_memcpy (&val[1], &value[1], slen - 2);
99 /* add a space to indicate "not mandatory" */
100 val = GNUNET_malloc (slen + 2);
105 GNUNET_array_append (u->data.ksk.keywords, u->data.ksk.keywordCount, val);
110 * Allow user to specify keywords.
112 * @param shortName short name of the option
113 * @param name long name of the option
114 * @param argumentHelp help text for the option argument
115 * @param description long help text for the option
116 * @param[out] topKeywords set to the desired value
118 struct GNUNET_GETOPT_CommandLineOption
119 GNUNET_FS_GETOPT_KEYWORDS (char shortName,
121 const char *argumentHelp,
122 const char *description,
123 struct GNUNET_FS_Uri **topKeywords)
125 struct GNUNET_GETOPT_CommandLineOption clo = {
126 .shortName = shortName,
128 .argumentHelp = argumentHelp,
129 .description = description,
130 .require_argument = 1,
131 .processor = &getopt_set_keywords,
132 .scls = (void *) topKeywords
139 * Command-line option parser function that allows the user to specify
140 * one or more '-m' options with metadata. Each specified entry of
141 * the form "type=value" will be added to the metadata. A pointer to
142 * the metadata must be passed as the "scls" argument.
144 * @param ctx command line processor context
145 * @param scls must be of type "struct GNUNET_MetaData **"
146 * @param option name of the option (typically 'k')
147 * @param value command line argument given
148 * @return #GNUNET_OK on success
151 getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
156 struct GNUNET_CONTAINER_MetaData **mm = scls;
157 #if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
158 enum EXTRACTOR_MetaType type;
159 const char *typename;
160 const char *typename_i18n;
162 struct GNUNET_CONTAINER_MetaData *meta;
168 meta = GNUNET_CONTAINER_meta_data_create ();
172 /* Use GNUNET_STRINGS_get_utf8_args() in main() to acquire utf-8-encoded
173 * commandline arguments, so that the following line is not needed.
175 /*tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), locale_charset ());*/
176 tmp = GNUNET_strdup (value);
177 #if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
178 type = EXTRACTOR_metatype_get_max ();
182 typename = EXTRACTOR_metatype_to_string (type);
183 typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
184 if ((strlen (tmp) >= strlen (typename) + 1) &&
185 (tmp[strlen (typename)] == ':') &&
186 (0 == strncmp (typename, tmp, strlen (typename))))
188 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
189 EXTRACTOR_METAFORMAT_UTF8,
191 &tmp[strlen (typename) + 1],
192 strlen (&tmp[strlen (typename) + 1]) +
198 if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
199 (tmp[strlen (typename_i18n)] == ':') &&
200 (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
202 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
203 EXTRACTOR_METAFORMAT_UTF8,
205 &tmp[strlen (typename_i18n) + 1],
207 [strlen (typename_i18n) + 1]) +
218 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
219 EXTRACTOR_METATYPE_UNKNOWN,
220 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
221 tmp, strlen (tmp) + 1);
224 ("Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"),
231 * Allow user to specify metadata.
233 * @param shortName short name of the option
234 * @param name long name of the option
235 * @param argumentHelp help text for the option argument
236 * @param description long help text for the option
237 * @param[out] metadata set to the desired value
239 struct GNUNET_GETOPT_CommandLineOption
240 GNUNET_FS_GETOPT_METADATA (char shortName,
242 const char *argumentHelp,
243 const char *description,
244 struct GNUNET_CONTAINER_MetaData **meta)
246 struct GNUNET_GETOPT_CommandLineOption clo = {
247 .shortName = shortName,
249 .argumentHelp = argumentHelp,
250 .description = description,
251 .require_argument = 1,
252 .processor = &getopt_set_metadata,
253 .scls = (void *) meta
262 /* end of fs_getopt.c */