GNUnet 0.21.1
fs_getopt.c File Reference

helper functions for command-line argument processing More...

#include "platform.h"
#include "gnunet_fs_service.h"
#include "fs_api.h"
Include dependency graph for fs_getopt.c:

Go to the source code of this file.

Functions

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 keywords. More...
 
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. More...
 
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 metadata. More...
 
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. More...
 

Detailed Description

helper functions for command-line argument processing

Author
Igor Wronsky, Christian Grothoff

Definition in file fs_getopt.c.

Function Documentation

◆ getopt_set_keywords()

static int getopt_set_keywords ( struct GNUNET_GETOPT_CommandLineProcessorContext ctx,
void *  scls,
const char *  option,
const char *  value 
)
static

Command-line option parser function that allows the user to specify one or more '-k' options with keywords.

Each specified keyword will be added to the URI. A pointer to the URI must be passed as the "scls" argument.

Parameters
ctxcommand line processor context
sclsmust be of type "struct GNUNET_FS_Uri **"
optionname of the option (typically 'k')
valuecommand line argument given
Returns
GNUNET_OK on success

Definition at line 46 of file fs_getopt.c.

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}
static mp_limb_t u[(((256)+GMP_NUMB_BITS - 1)/GMP_NUMB_BITS)]
@ GNUNET_FS_URI_KSK
Keyword search key (query with keywords).
Definition: fs_api.h:154
static char * value
Value of the record to add/remove.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
#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).
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:167

References GNUNET_array_append, GNUNET_assert, GNUNET_FS_URI_KSK, GNUNET_malloc, GNUNET_memcpy, GNUNET_new, GNUNET_OK, GNUNET_strdup, GNUNET_SYSERR, u, uri, and value.

Referenced by GNUNET_FS_GETOPT_KEYWORDS().

Here is the caller graph for this function:

◆ getopt_set_metadata()

static int getopt_set_metadata ( struct GNUNET_GETOPT_CommandLineProcessorContext ctx,
void *  scls,
const char *  option,
const char *  value 
)
static

Command-line option parser function that allows the user to specify one or more '-m' options with metadata.

Each specified entry of the form "type=value" will be added to the metadata. A pointer to the metadata must be passed as the "scls" argument.

Parameters
ctxcommand line processor context
sclsmust be of type "struct GNUNET_MetaData **"
optionname of the option (typically 'k')
valuecommand line argument given
Returns
GNUNET_OK on success

Definition at line 162 of file fs_getopt.c.

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 /* Use GNUNET_STRINGS_get_utf8_args() in main() to acquire utf-8-encoded
185 * commandline arguments, so that the following line is not needed.
186 */
187 /*tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), locale_charset ());*/
188 tmp = GNUNET_strdup (value);
189#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
190 type = EXTRACTOR_metatype_get_max ();
191 while (type > 0)
192 {
193 type--;
194 typename = EXTRACTOR_metatype_to_string (type);
195 typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
196 if ((strlen (tmp) >= strlen (typename) + 1) &&
197 (tmp[strlen (typename)] == ':') &&
198 (0 == strncmp (typename, tmp, strlen (typename))))
199 {
201 EXTRACTOR_METAFORMAT_UTF8,
202 "text/plain",
203 &tmp[strlen (typename) + 1],
204 strlen (&tmp[strlen (typename) + 1])
205 + 1);
206 GNUNET_free (tmp);
207 tmp = NULL;
208 break;
209 }
210 if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
211 (tmp[strlen (typename_i18n)] == ':') &&
212 (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
213 {
215 EXTRACTOR_METAFORMAT_UTF8,
216 "text/plain",
217 &tmp[strlen (typename_i18n) + 1],
218 strlen (&tmp
219 [strlen (typename_i18n) + 1])
220 + 1);
221 GNUNET_free (tmp);
222 tmp = NULL;
223 break;
224 }
225 }
226#endif
227
228 if (NULL != tmp)
229 {
230 GNUNET_FS_meta_data_insert (meta, "<gnunet>",
231 EXTRACTOR_METATYPE_UNKNOWN,
232 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
233 tmp, strlen (tmp) + 1);
234 GNUNET_free (tmp);
235 printf (_
236 (
237 "Unknown metadata type in metadata option `%s'. Using metadata type `unknown' instead.\n"),
238 value);
239 }
240 return GNUNET_OK;
241}
#define dgettext(Domainname, Msgid)
Definition: gettext.h:47
static uint32_t type
Type string converted to DNS type value.
static struct GNUNET_FS_MetaData * meta
Meta-data provided via command-line option.
#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

References _, dgettext, GNUNET_free, GNUNET_FS_meta_data_create(), GNUNET_FS_meta_data_insert(), GNUNET_OK, GNUNET_strdup, LIBEXTRACTOR_GETTEXT_DOMAIN, meta, type, and value.

Referenced by GNUNET_FS_GETOPT_METADATA().

Here is the call graph for this function:
Here is the caller graph for this function: