indentation
[oweals/gnunet.git] / src / fs / fs_getopt.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file fs/fs_getopt.c
23  * @brief helper functions for command-line argument processing
24  * @author Igor Wronsky, Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_fs_service.h"
28 #include "fs.h"
29
30 /* ******************** command-line option parsing API ******************** */
31
32 /**
33  * Command-line option parser function that allows the user
34  * to specify one or more '-k' options with keywords.  Each
35  * specified keyword will be added to the URI.  A pointer to
36  * the URI must be passed as the "scls" argument.
37  *
38  * @param ctx command line processor context
39  * @param scls must be of type "struct GNUNET_FS_Uri **"
40  * @param option name of the option (typically 'k')
41  * @param value command line argument given
42  * @return GNUNET_OK on success
43  */
44 int
45 GNUNET_FS_getopt_set_keywords (struct GNUNET_GETOPT_CommandLineProcessorContext
46                                *ctx, void *scls, const char *option,
47                                const char *value)
48 {
49   struct GNUNET_FS_Uri **uri = scls;
50   struct GNUNET_FS_Uri *u = *uri;
51   char *val;
52   size_t slen;
53
54   if (u == NULL)
55   {
56     u = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
57     *uri = u;
58     u->type = ksk;
59     u->data.ksk.keywordCount = 0;
60     u->data.ksk.keywords = NULL;
61   }
62   else
63   {
64     GNUNET_assert (u->type == ksk);
65   }
66   slen = strlen (value);
67   if (slen == 0)
68     return GNUNET_SYSERR;       /* cannot be empty */
69   if (value[0] == '+')
70   {
71     /* simply preserve the "mandatory" flag */
72     if (slen < 2)
73       return GNUNET_SYSERR;     /* empty keywords not allowed */
74     if ((value[1] == '"') && (slen > 3) && (value[slen - 1] == '"'))
75     {
76       /* remove the quotes, keep the '+' */
77       val = GNUNET_malloc (slen - 1);
78       val[0] = '+';
79       memcpy (&val[1], &value[2], slen - 3);
80       val[slen - 2] = '\0';
81     }
82     else
83     {
84       /* no quotes, just keep the '+' */
85       val = GNUNET_strdup (value);
86     }
87   }
88   else
89   {
90     if ((value[0] == '"') && (slen > 2) && (value[slen - 1] == '"'))
91     {
92       /* remove the quotes, add a space */
93       val = GNUNET_malloc (slen);
94       val[0] = ' ';
95       memcpy (&val[1], &value[1], slen - 2);
96       val[slen - 1] = '\0';
97     }
98     else
99     {
100       /* add a space to indicate "not mandatory" */
101       val = GNUNET_malloc (slen + 2);
102       strcpy (val, " ");
103       strcat (val, value);
104     }
105   }
106   GNUNET_array_append (u->data.ksk.keywords, u->data.ksk.keywordCount, val);
107   return GNUNET_OK;
108 }
109
110
111 /**
112  * Command-line option parser function that allows the user to specify
113  * one or more '-m' options with metadata.  Each specified entry of
114  * the form "type=value" will be added to the metadata.  A pointer to
115  * the metadata must be passed as the "scls" argument.
116  *
117  * @param ctx command line processor context
118  * @param scls must be of type "struct GNUNET_MetaData **"
119  * @param option name of the option (typically 'k')
120  * @param value command line argument given
121  * @return GNUNET_OK on success
122  */
123 int
124 GNUNET_FS_getopt_set_metadata (struct GNUNET_GETOPT_CommandLineProcessorContext
125                                *ctx, void *scls, const char *option,
126                                const char *value)
127 {
128   struct GNUNET_CONTAINER_MetaData **mm = scls;
129   enum EXTRACTOR_MetaType type;
130   const char *typename;
131   const char *typename_i18n;
132   struct GNUNET_CONTAINER_MetaData *meta;
133   char *tmp;
134
135   meta = *mm;
136   if (meta == NULL)
137   {
138     meta = GNUNET_CONTAINER_meta_data_create ();
139     *mm = meta;
140   }
141
142 #if ENABLE_NLS
143   tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), nl_langinfo (CODESET));
144 #else
145   tmp = GNUNET_STRINGS_to_utf8 (value, strlen (value), "utf-8");
146 #endif
147   type = EXTRACTOR_metatype_get_max ();
148   while (type > 0)
149   {
150     type--;
151     typename = EXTRACTOR_metatype_to_string (type);
152     typename_i18n = dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN, typename);
153     if ((strlen (tmp) >= strlen (typename) + 1) &&
154         (tmp[strlen (typename)] == ':') &&
155         (0 == strncmp (typename, tmp, strlen (typename))))
156     {
157       GNUNET_CONTAINER_meta_data_insert (meta,
158                                          "<gnunet>",
159                                          type,
160                                          EXTRACTOR_METAFORMAT_UTF8,
161                                          "text/plain",
162                                          &tmp[strlen (typename) + 1],
163                                          strlen (&tmp[strlen (typename) + 1]) +
164                                          1);
165       GNUNET_free (tmp);
166       tmp = NULL;
167       break;
168     }
169     if ((strlen (tmp) >= strlen (typename_i18n) + 1) &&
170         (tmp[strlen (typename_i18n)] == ':') &&
171         (0 == strncmp (typename_i18n, tmp, strlen (typename_i18n))))
172     {
173       GNUNET_CONTAINER_meta_data_insert (meta,
174                                          "<gnunet>",
175                                          type,
176                                          EXTRACTOR_METAFORMAT_UTF8,
177                                          "text/plain",
178                                          &tmp[strlen (typename_i18n) + 1],
179                                          strlen (&tmp
180                                                  [strlen (typename_i18n) + 1]) +
181                                          1);
182       GNUNET_free (tmp);
183       tmp = NULL;
184       break;
185     }
186   }
187   if (tmp != NULL)
188   {
189     GNUNET_CONTAINER_meta_data_insert (meta,
190                                        "<gnunet>",
191                                        EXTRACTOR_METATYPE_UNKNOWN,
192                                        EXTRACTOR_METAFORMAT_UTF8,
193                                        "text/plain", tmp, strlen (tmp) + 1);
194     GNUNET_free (tmp);
195     printf (_
196             ("Unknown metadata type in metadata option `%s'.  Using metadata type `unknown' instead.\n"),
197             value);
198   }
199   return GNUNET_OK;
200 }
201
202 /* end of fs_getopt.c */