GNUnet 0.22.2
fs_getopt.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
26#include "platform.h"
27
28#include "gnunet_fs_service.h"
29#include "fs_api.h"
30
31/* ******************** command-line option parsing API ******************** */
32
45static int
47 void *scls,
48 const char *option,
49 const char *value)
50{
51 struct GNUNET_FS_Uri **uri = scls;
52 struct GNUNET_FS_Uri *u = *uri;
53 char *val;
54 size_t slen;
55
56 if (NULL == u)
57 {
58 u = GNUNET_new (struct GNUNET_FS_Uri);
59 *uri = u;
60 u->type = GNUNET_FS_URI_KSK;
61 u->data.ksk.keywordCount = 0;
62 u->data.ksk.keywords = NULL;
63 }
64 else
65 {
67 }
68 slen = strlen (value);
69 if (0 == slen)
70 return GNUNET_SYSERR; /* cannot be empty */
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 {
88 /* no quotes, just keep the '+' */
89 val = GNUNET_strdup (value);
90 }
91 }
92 else
93 {
94 if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
95 {
96 /* remove the quotes, add a space */
97 val = GNUNET_malloc (slen);
98 val[0] = ' ';
99 GNUNET_memcpy (&val[1],
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 }
111 }
112 GNUNET_array_append (u->data.ksk.keywords,
113 u->data.ksk.keywordCount,
114 val);
115 return GNUNET_OK;
116}
117
118
130 const char *name,
131 const char *argumentHelp,
132 const char *description,
133 struct GNUNET_FS_Uri **topKeywords)
134{
137 .name = name,
138 .argumentHelp = argumentHelp,
139 .description = description,
140 .require_argument = 1,
141 .processor = &getopt_set_keywords,
142 .scls = (void *) topKeywords
143 };
144
145 return clo;
146}
147
148
161static int
163 void *scls,
164 const char *option,
165 const char *value)
166{
167 struct GNUNET_FS_MetaData **mm = scls;
168
169#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
170 enum EXTRACTOR_MetaType type;
171 const char *typename;
172 const char *typename_i18n;
173#endif
174 struct GNUNET_FS_MetaData *meta;
175 char *tmp;
176
177 meta = *mm;
178 if (meta == NULL)
179 {
181 *mm = meta;
182 }
183
184 tmp = GNUNET_strdup (value);
185#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
186 type = EXTRACTOR_metatype_get_max ();
187 while (type > 0)
188 {
189 type--;
190 typename = EXTRACTOR_metatype_to_string (type);
191 typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
192 if ((strlen (tmp) >= strlen (typename) + 1) &&
193 (tmp[strlen (typename)] == ':') &&
194 (0 == strncmp (typename, tmp, strlen (typename))))
195 {
197 EXTRACTOR_METAFORMAT_UTF8,
198 "text/plain",
199 &tmp[strlen (typename) + 1],
200 strlen (&tmp[strlen (typename) + 1])
201 + 1);
202 GNUNET_free (tmp);
203 tmp = NULL;
204 break;
205 }
206 if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
207 (tmp[strlen (typename_i18n)] == ':') &&
208 (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
209 {
211 EXTRACTOR_METAFORMAT_UTF8,
212 "text/plain",
213 &tmp[strlen (typename_i18n) + 1],
214 strlen (&tmp
215 [strlen (typename_i18n) + 1])
216 + 1);
217 GNUNET_free (tmp);
218 tmp = NULL;
219 break;
220 }
221 }
222#endif
223
224 if (NULL != tmp)
225 {
226 GNUNET_FS_meta_data_insert (meta, "<gnunet>",
227 EXTRACTOR_METATYPE_UNKNOWN,
228 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
229 tmp, strlen (tmp) + 1);
230 GNUNET_free (tmp);
231 printf (_
232 (
233 "Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"),
234 value);
235 }
236 return GNUNET_OK;
237}
238
239
251 const char *name,
252 const char *argumentHelp,
253 const char *description,
254 struct GNUNET_FS_MetaData **meta)
255{
258 .name = name,
259 .argumentHelp = argumentHelp,
260 .description = description,
261 .require_argument = 1,
262 .processor = &getopt_set_metadata,
263 .scls = (void *) meta
264 };
265
266 return clo;
267}
268
269
270/* end of fs_getopt.c */
static mp_limb_t u[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
shared definitions for the FS library
@ GNUNET_FS_URI_KSK
Keyword search key (query with keywords).
Definition: fs_api.h:154
static int getopt_set_keywords(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Command-line option parser function that allows the user to specify one or more '-k' options with key...
Definition: fs_getopt.c:46
static int getopt_set_metadata(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Command-line option parser function that allows the user to specify one or more '-m' options with met...
Definition: fs_getopt.c:162
#define dgettext(Domainname, Msgid)
Definition: gettext.h:51
static struct GNUNET_FS_Handle * ctx
static char * name
Name (label) of the records to list.
static char * value
Value of the record to add/remove.
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static struct GNUNET_FS_MetaData * meta
Meta-data provided via command-line option.
static struct GNUNET_FS_Uri * topKeywords
Keywords provided via command-line option.
API for file sharing via GNUnet.
struct GNUNET_GETOPT_CommandLineOption GNUNET_FS_GETOPT_METADATA(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_FS_MetaData **meta)
Allow user to specify metadata.
Definition: fs_getopt.c:250
struct GNUNET_GETOPT_CommandLineOption GNUNET_FS_GETOPT_KEYWORDS(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_FS_Uri **topKeywords)
Allow user to specify keywords.
Definition: fs_getopt.c:129
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_FS_MetaData * GNUNET_FS_meta_data_create(void)
Create a fresh meta data container.
Definition: meta_data.c:127
int GNUNET_FS_meta_data_insert(struct GNUNET_FS_MetaData *md, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_size)
Extend metadata.
Definition: meta_data.c:254
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
#define LIBEXTRACTOR_GETTEXT_DOMAIN
Definition: platform.h:179
Meta data to associate with a file, directory or namespace.
Definition: meta_data.c:92
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:167
Definition of a command line option.
const char * description
Help text for the option (description)
const char * argumentHelp
Name of the argument for the user in help text.
void * scls
Specific closure to pass to the processor.
const char shortName
Short name of the option.
General context for command line processors.