-remove async ecc key generation, not needed
[oweals/gnunet.git] / src / util / getopt_helpers.c
1 /*
2      This file is part of GNUnet
3      (C) 2006, 2011 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 2, 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 src/util/getopt_helpers.c
23  * @brief implements command line that sets option
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30
31 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32
33
34 /**
35  * Print out program version (implements --version).
36  *
37  * @param ctx command line processing context
38  * @param scls additional closure (points to version string)
39  * @param option name of the option
40  * @param value not used (NULL)
41  * @return GNUNET_NO (do not continue, not an error)
42  */
43 int
44 GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext
45                               *ctx, void *scls, const char *option,
46                               const char *value)
47 {
48   const char *version = scls;
49
50   printf ("%s v%s\n", ctx->binaryName, version);
51   return GNUNET_NO;
52 }
53
54
55
56 #define BORDER 29
57
58 /**
59  * Print out details on command line options (implements --help).
60  *
61  * @param ctx command line processing context
62  * @param scls additional closure (points to about text)
63  * @param option name of the option
64  * @param value not used (NULL)
65  * @return GNUNET_NO (do not continue, not an error)
66  */
67 int
68 GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
69                             *ctx, void *scls, const char *option,
70                             const char *value)
71 {
72   const char *about = scls;
73   size_t slen;
74   unsigned int i;
75   int j;
76   size_t ml;
77   size_t p;
78   char *scp;
79   const char *trans;
80   const struct GNUNET_GETOPT_CommandLineOption *opt;
81
82   if (NULL != about)
83   {
84     printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
85     printf (_
86             ("Arguments mandatory for long options are also mandatory for short options.\n"));
87   }
88   i = 0;
89   opt = ctx->allOptions;
90   while (opt[i].description != NULL)
91   {
92     if (opt[i].shortName == '\0')
93       printf ("      ");
94     else
95       printf ("  -%c, ", opt[i].shortName);
96     printf ("--%s", opt[i].name);
97     slen = 8 + strlen (opt[i].name);
98     if (opt[i].argumentHelp != NULL)
99     {
100       printf ("=%s", opt[i].argumentHelp);
101       slen += 1 + strlen (opt[i].argumentHelp);
102     }
103     if (slen > BORDER)
104     {
105       printf ("\n%*s", BORDER, "");
106       slen = BORDER;
107     }
108     if (slen < BORDER)
109     {
110       printf ("%*s", (int) (BORDER - slen), "");
111       slen = BORDER;
112     }
113     if (0 < strlen (opt[i].description))
114       trans = gettext (opt[i].description);
115     else
116       trans = "";
117     ml = strlen (trans);
118     p = 0;
119 OUTER:
120     while (ml - p > 78 - slen)
121     {
122       for (j = p + 78 - slen; j > p; j--)
123       {
124         if (isspace ((unsigned char) trans[j]))
125         {
126           scp = GNUNET_malloc (j - p + 1);
127           memcpy (scp, &trans[p], j - p);
128           scp[j - p] = '\0';
129           printf ("%s\n%*s", scp, BORDER + 2, "");
130           GNUNET_free (scp);
131           p = j + 1;
132           slen = BORDER + 2;
133           goto OUTER;
134         }
135       }
136       /* could not find space to break line */
137       scp = GNUNET_malloc (78 - slen + 1);
138       memcpy (scp, &trans[p], 78 - slen);
139       scp[78 - slen] = '\0';
140       printf ("%s\n%*s", scp, BORDER + 2, "");
141       GNUNET_free (scp);
142       slen = BORDER + 2;
143       p = p + 78 - slen;
144     }
145     /* print rest */
146     if (p < ml)
147       printf ("%s\n", &trans[p]);
148     if (strlen (trans) == 0)
149       printf ("\n");
150     i++;
151   }
152   printf ("Report bugs to gnunet-developers@gnu.org.\n"
153           "GNUnet home page: http://www.gnu.org/software/gnunet/\n"
154           "General help using GNU software: http://www.gnu.org/gethelp/\n");
155   return GNUNET_NO;
156 }
157
158
159 /**
160  * Set an option of type 'unsigned int' from the command line. Each
161  * time the option flag is given, the value is incremented by one.
162  * A pointer to this function should be passed as part of the
163  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
164  * of this type.  It should be followed by a pointer to a value of
165  * type 'int'.
166  *
167  * @param ctx command line processing context
168  * @param scls additional closure (will point to the 'int')
169  * @param option name of the option
170  * @param value not used (NULL)
171  * @return GNUNET_OK
172  */
173 int
174 GNUNET_GETOPT_increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext
175                                *ctx, void *scls, const char *option,
176                                const char *value)
177 {
178   int *val = scls;
179
180   (*val)++;
181   return GNUNET_OK;
182 }
183
184
185 /**
186  * Set an option of type 'int' from the command line to 1 if the
187  * given option is present.
188  * A pointer to this function should be passed as part of the
189  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
190  * of this type.  It should be followed by a pointer to a value of
191  * type 'int'.
192  *
193  * @param ctx command line processing context
194  * @param scls additional closure (will point to the 'int')
195  * @param option name of the option
196  * @param value not used (NULL)
197  * @return GNUNET_OK
198  */
199 int
200 GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
201                        void *scls, const char *option, const char *value)
202 {
203   int *val = scls;
204
205   *val = 1;
206   return GNUNET_OK;
207 }
208
209
210 /**
211  * Set an option of type 'char *' from the command line.
212  * A pointer to this function should be passed as part of the
213  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
214  * of this type.  It should be followed by a pointer to a value of
215  * type 'char *', which will be allocated with the requested string.
216  *
217  * @param ctx command line processing context
218  * @param scls additional closure (will point to the 'char *',
219  *             which will be allocated)
220  * @param option name of the option
221  * @param value actual value of the option (a string)
222  * @return GNUNET_OK
223  */
224 int
225 GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
226                           void *scls, const char *option, const char *value)
227 {
228   char **val = scls;
229
230   GNUNET_assert (value != NULL);
231   GNUNET_free_non_null (*val);
232   *val = GNUNET_strdup (value);
233   return GNUNET_OK;
234 }
235
236
237 int
238 GNUNET_GETOPT_set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
239                             void *scls, const char *option, const char *value)
240 {
241   char **val = scls;
242
243   GNUNET_assert (value != NULL);
244   GNUNET_free_non_null (*val);
245   *val = GNUNET_STRINGS_filename_expand (value);
246   return GNUNET_OK;
247 }
248
249 /**
250  * Set an option of type 'unsigned long long' from the command line.
251  * A pointer to this function should be passed as part of the
252  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
253  * of this type.  It should be followed by a pointer to a value of
254  * type 'unsigned long long'.
255  *
256  * @param ctx command line processing context
257  * @param scls additional closure (will point to the 'unsigned long long')
258  * @param option name of the option
259  * @param value actual value of the option as a string.
260  * @return GNUNET_OK if parsing the value worked
261  */
262 int
263 GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
264                          void *scls, const char *option, const char *value)
265 {
266   unsigned long long *val = scls;
267
268   if (1 != SSCANF (value, "%llu", val))
269   {
270     FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option);
271     return GNUNET_SYSERR;
272   }
273   return GNUNET_OK;
274 }
275
276
277 /**
278  * Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
279  * A pointer to this function should be passed as part of the
280  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
281  * of this type.  It should be followed by a pointer to a value of
282  * type 'struct GNUNET_TIME_Relative'.
283  *
284  * @param ctx command line processing context
285  * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative')
286  * @param option name of the option
287  * @param value actual value of the option as a string.
288  * @return GNUNET_OK if parsing the value worked
289  */
290 int
291 GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
292                                  void *scls, const char *option, const char *value)
293 {
294   struct GNUNET_TIME_Relative *val = scls;
295
296   if (GNUNET_OK !=
297       GNUNET_STRINGS_fancy_time_to_relative (value,
298                                              val))
299   {
300     FPRINTF (stderr, _("You must pass relative time to the `%s' option.\n"), option);
301     return GNUNET_SYSERR;
302   }
303   return GNUNET_OK;
304 }
305
306
307 /**
308  * Set an option of type 'unsigned int' from the command line.
309  * A pointer to this function should be passed as part of the
310  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
311  * of this type.  It should be followed by a pointer to a value of
312  * type 'unsigned int'.
313  *
314  * @param ctx command line processing context
315  * @param scls additional closure (will point to the 'unsigned int')
316  * @param option name of the option
317  * @param value actual value of the option as a string.
318  * @return GNUNET_OK if parsing the value worked
319  */
320 int
321 GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
322                         void *scls, const char *option, const char *value)
323 {
324   unsigned int *val = scls;
325
326   if (1 != SSCANF (value, "%u", val))
327   {
328     FPRINTF (stderr, _("You must pass a number to the `%s' option.\n"), option);
329     return GNUNET_SYSERR;
330   }
331   return GNUNET_OK;
332 }
333
334
335 /* end of getopt_helpers.c */