2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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_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 GNUNET_FS_getopt_configure_set_keywords (GNUNET_GETOPT_CommandLineProcessorContext* ctx,
51 struct GNUNET_FS_Uri **uri = scls;
52 struct GNUNET_FS_Uri *u = *uri;
58 u = GNUNET_malloc (sizeof (struct GNUNET_ECRS_URI));
61 u->data.ksk.keywordCount = 0;
62 u->data.ksk.keywords = NULL;
66 GNUNET_assert (u->type == ksk);
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 memcpy (&val[1], &value[2], slen - 3);
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 memcpy (&val[1], &value[1], slen - 2);
102 /* add a space to indicate "not mandatory" */
103 val = GNUNET_malloc (slen + 2);
108 GNUNET_array_grow (u->data.ksk.keywords,
109 u->data.ksk.keywordCount, u->data.ksk.keywordCount + 1);
110 u->data.ksk.keywords[u->data.ksk.keywordCount - 1] = val;
116 * Command-line option parser function that allows the user to specify
117 * one or more '-m' options with metadata. Each specified entry of
118 * the form "type=value" will be added to the metadata. A pointer to
119 * the metadata must be passed as the "scls" argument.
121 * @param ctx command line processor context
122 * @param scls must be of type "struct GNUNET_MetaData **"
123 * @param option name of the option (typically 'k')
124 * @param value command line argument given
125 * @return GNUNET_OK on success
128 GNUNET_FS_getopt_configure_set_metadata (GNUNET_GETOPT_CommandLineProcessorContext* ctx,
134 struct GNUNET_CONTAINER_MetaData **mm = scls;
135 EXTRACTOR_KeywordType type;
136 const char *typename;
137 const char *typename_i18n;
138 struct GNUNET_CONTAINER_MetaData *meta;
144 meta = GNUNET_CONTAINER_meta_data_create ();
148 tmp = GNUNET_STRINGS_to_utf8 (NULL, value, strlen (value),
150 nl_langinfo (CODESET)
155 type = EXTRACTOR_getHighestKeywordTypeNumber ();
159 typename = EXTRACTOR_getKeywordTypeAsString (type);
160 typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
161 if ((strlen (tmp) >= strlen (typename) + 1) &&
162 (tmp[strlen (typename)] == ':') &&
163 (0 == strncmp (typename, tmp, strlen (typename))))
165 GNUNET_CONTAINER_meta_data_insert (meta, type, &tmp[strlen (typename) + 1]);
170 if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
171 (tmp[strlen (typename_i18n)] == ':') &&
172 (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
174 GNUNET_CONTAINER_meta_data_insert (meta, type,
175 &tmp[strlen (typename_i18n) + 1]);
183 GNUNET_CONTAINER_meta_data_insert (meta, EXTRACTOR_UNKNOWN, tmp);
186 ("Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"),
192 /* end of fs_getopt.c */