aboutsummaryrefslogtreecommitdiff
path: root/src/fs/fs_getopt.c
blob: 5206a25d93d38a3fcf825256515bbf00673b4ce0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
     This file is part of GNUnet.
     Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.
*/

/**
 * @file fs/fs_getopt.c
 * @brief helper functions for command-line argument processing
 * @author Igor Wronsky, Christian Grothoff
 */
#include "platform.h"
#include "gnunet_fs_service.h"
#include "gnunet_getopt_lib.h"
#include "fs_api.h"

/* ******************** command-line option parsing API ******************** */

/**
 * 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.
 *
 * @param ctx command line processor context
 * @param scls must be of type "struct GNUNET_FS_Uri **"
 * @param option name of the option (typically 'k')
 * @param value command line argument given
 * @return GNUNET_OK on success
 */
static int
getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext
                     *ctx, void *scls, const char *option,
                      const char *value)
{
  struct GNUNET_FS_Uri **uri = scls;
  struct GNUNET_FS_Uri *u = *uri;
  char *val;
  size_t slen;

  if (u == NULL)
  {
    u = GNUNET_new (struct GNUNET_FS_Uri);
    *uri = u;
    u->type = GNUNET_FS_URI_KSK;
    u->data.ksk.keywordCount = 0;
    u->data.ksk.keywords = NULL;
  }
  else
  {
    GNUNET_assert (u->type == GNUNET_FS_URI_KSK);
  }
  slen = strlen (value);
  if (slen == 0)
    return GNUNET_SYSERR;       /* cannot be empty */
  if (value[0] == '+')
  {
    /* simply preserve the "mandatory" flag */
    if (slen < 2)
      return GNUNET_SYSERR;     /* empty keywords not allowed */
    if ((value[1] == '"') && (slen > 3) && (value[slen - 1] == '"'))
    {
      /* remove the quotes, keep the '+' */
      val = GNUNET_malloc (slen - 1);
      val[0] = '+';
      GNUNET_memcpy (&val[1], &value[2], slen - 3);
      val[slen - 2] = '\0';
    }
    else
    {
      /* no quotes, just keep the '+' */
      val = GNUNET_strdup (value);
    }
  }
  else
  {
    if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
    {
      /* remove the quotes, add a space */
      val = GNUNET_malloc (slen);
      val[0] = ' ';
      GNUNET_memcpy (&val[1], &value[1], slen - 2);
      val[slen - 1] = '\0';
    }
    else
    {
      /* add a space to indicate "not mandatory" */
      val = GNUNET_malloc (slen + 2);
      strcpy (val, " ");
      strcat (val, value);
    }
  }
  GNUNET_array_append (u->data.ksk.keywords, u->data.ksk.keywordCount, val);
  return GNUNET_OK;
}

/**
 * Allow user to specify keywords.
 *
 * @param shortName short name of the option
 * @param name long name of the option
 * @param argumentHelp help text for the option argument
 * @param description long help text for the option
 * @param[out] topKeywords set to the desired value
 */
struct GNUNET_GETOPT_CommandLineOption
GNUNET_FS_GETOPT_KEYWORDS (char shortName,
                           const char *name,
                           const char *argumentHelp,
                           const char *description,
                           struct GNUNET_FS_Uri **topKeywords)
{
  struct GNUNET_GETOPT_CommandLineOption clo = {
    .shortName = shortName,
    .name = name,
    .argumentHelp = argumentHelp,
    .description = description,
    .require_argument = 1,
    .processor = &getopt_set_keywords,
    .scls = (void *) topKeywords  
  };

  return clo;
}

/**
 * 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.
 *
 * @param ctx command line processor context
 * @param scls must be of type "struct GNUNET_MetaData **"
 * @param option name of the option (typically 'k')
 * @param value command line argument given
 * @return #GNUNET_OK on success
 */
static int
getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
                     void *scls,
                     const char *option,
                     const char *value)
{
  struct GNUNET_CONTAINER_MetaData **mm = scls;
#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
  enum EXTRACTOR_MetaType type;
  const char *typename;
  const char *typename_i18n;
#endif
  struct GNUNET_CONTAINER_MetaData *meta;
  char *tmp;

  meta = *mm;
  if (meta == NULL)
  {
    meta = GNUNET_CONTAINER_meta_data_create ();
    *mm = meta;
  }

  /* Use GNUNET_STRINGS_get_utf8_args() in main() to acquire utf-8-encoded
   * commandline arguments, so that the following line is not needed.
   */
  /*tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), locale_charset ());*/
  tmp = GNUNET_strdup (value);
#if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
  type = EXTRACTOR_metatype_get_max ();
  while (type > 0)
  {
    type--;
    typename = EXTRACTOR_metatype_to_string (type);
    typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
    if ((strlen (tmp) >= strlen (typename) + 1) &&
        (tmp[strlen (typename)] == ':') &&
        (0 == strncmp (typename, tmp, strlen (typename))))
    {
      GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
                                         EXTRACTOR_METAFORMAT_UTF8,
                                         "text/plain",
                                         &tmp[strlen (typename) + 1],
                                         strlen (&tmp[strlen (typename) + 1]) +
                                         1);
      GNUNET_free (tmp);
      tmp = NULL;
      break;
    }
    if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
        (tmp[strlen (typename_i18n)] == ':') &&
        (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
    {
      GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", type,
                                         EXTRACTOR_METAFORMAT_UTF8,
                                         "text/plain",
                                         &tmp[strlen (typename_i18n) + 1],
                                         strlen (&tmp
                                                 [strlen (typename_i18n) + 1]) +
                                         1);
      GNUNET_free (tmp);
      tmp = NULL;
      break;
    }
  }
#endif

  if (NULL != tmp)
  {
    GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>",
                                       EXTRACTOR_METATYPE_UNKNOWN,
                                       EXTRACTOR_METAFORMAT_UTF8, "text/plain",
                                       tmp, strlen (tmp) + 1);
    GNUNET_free (tmp);
    printf (_
            ("Unknown metadata type in metadata option `%s'.  Using metadata type `unknown' instead.\n"),
            value);
  }
  return GNUNET_OK;
}

/**
 * Allow user to specify metadata.
 *
 * @param shortName short name of the option
 * @param name long name of the option
 * @param argumentHelp help text for the option argument
 * @param description long help text for the option
 * @param[out] metadata set to the desired value
 */
struct GNUNET_GETOPT_CommandLineOption
GNUNET_FS_GETOPT_METADATA (char shortName,
                           const char *name,
                           const char *argumentHelp,
                           const char *description,
                           struct GNUNET_CONTAINER_MetaData **meta)
{
  struct GNUNET_GETOPT_CommandLineOption clo = {
    .shortName = shortName,
    .name = name,
    .argumentHelp = argumentHelp,
    .description = description,
    .require_argument = 1,
    .processor = &getopt_set_metadata,
    .scls = (void *) meta
  };

  return clo;
}




/* end of fs_getopt.c */