Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / getopt_helpers.c
1 /*
2      This file is part of GNUnet
3      (C) 2006 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_getopt_lib.h"
30
31
32 int
33 GNUNET_GETOPT_print_version_ (struct GNUNET_GETOPT_CommandLineProcessorContext
34                               *ctx, void *scls, const char *option,
35                               const char *value)
36 {
37   const char *version = scls;
38
39   printf ("%s v%s\n", ctx->binaryName, version);
40   return GNUNET_SYSERR;
41 }
42
43
44
45 #define BORDER 29
46
47 int
48 GNUNET_GETOPT_format_help_ (struct GNUNET_GETOPT_CommandLineProcessorContext
49                             *ctx, void *scls, const char *option,
50                             const char *value)
51 {
52   const char *about = scls;
53   size_t slen;
54   unsigned int i;
55   int j;
56   size_t ml;
57   size_t p;
58   char *scp;
59   const char *trans;
60   const struct GNUNET_GETOPT_CommandLineOption *opt;
61
62   printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
63   printf (_
64           ("Arguments mandatory for long options are also mandatory for short options.\n"));
65   i = 0;
66   opt = ctx->allOptions;
67   while (opt[i].description != NULL)
68     {
69       if (opt[i].shortName == '\0')
70         printf ("      ");
71       else
72         printf ("  -%c, ", opt[i].shortName);
73       printf ("--%s", opt[i].name);
74       slen = 8 + strlen (opt[i].name);
75       if (opt[i].argumentHelp != NULL)
76         {
77           printf ("=%s", opt[i].argumentHelp);
78           slen += 1 + strlen (opt[i].argumentHelp);
79         }
80       if (slen > BORDER)
81         {
82           printf ("\n%*s", BORDER, "");
83           slen = BORDER;
84         }
85       if (slen < BORDER)
86         {
87           printf ("%*s", (int) (BORDER - slen), "");
88           slen = BORDER;
89         }
90       if (0 < strlen (opt[i].description))
91         trans = gettext (opt[i].description);
92       else
93         trans = "";
94       ml = strlen (trans);
95       p = 0;
96     OUTER:
97       while (ml - p > 78 - slen)
98         {
99           for (j = p + 78 - slen; j > p; j--)
100             {
101               if (isspace ( (unsigned char) trans[j]))
102                 {
103                   scp = GNUNET_malloc (j - p + 1);
104                   memcpy (scp, &trans[p], j - p);
105                   scp[j - p] = '\0';
106                   printf ("%s\n%*s", scp, BORDER + 2, "");
107                   GNUNET_free (scp);
108                   p = j + 1;
109                   slen = BORDER + 2;
110                   goto OUTER;
111                 }
112             }
113           /* could not find space to break line */
114           scp = GNUNET_malloc (78 - slen + 1);
115           memcpy (scp, &trans[p], 78 - slen);
116           scp[78 - slen] = '\0';
117           printf ("%s\n%*s", scp, BORDER + 2, "");
118           GNUNET_free (scp);
119           slen = BORDER + 2;
120           p = p + 78 - slen;
121         }
122       /* print rest */
123       if (p < ml)
124         printf ("%s\n", &trans[p]);
125       if (strlen (trans) == 0)
126         printf ("\n");
127       i++;
128     }
129   printf ("Report bugs to gnunet-developers@gnu.org.\n"
130           "GNUnet home page: http://www.gnu.org/software/gnunet/\n"
131           "General help using GNU software: http://www.gnu.org/gethelp/\n");
132   return GNUNET_SYSERR;
133 }
134
135
136 int
137 GNUNET_GETOPT_increment_value (struct
138                                GNUNET_GETOPT_CommandLineProcessorContext *ctx,
139                                void *scls, const char *cmdLineOption,
140                                const char *value)
141 {
142   int *val = scls;
143   (*val)++;
144   return GNUNET_OK;
145 }
146
147 int
148 GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
149                        void *scls, const char *option, const char *value)
150 {
151   int *val = scls;
152   *val = 1;
153   return GNUNET_OK;
154 }
155
156 int
157 GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext
158                           *ctx, void *scls, const char *option,
159                           const char *value)
160 {
161   char **val = scls;
162
163   GNUNET_assert (value != NULL);
164   if (NULL != *val)
165     GNUNET_free (*val);
166   *val = GNUNET_strdup (value);
167   return GNUNET_OK;
168 }
169
170 int
171 GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext
172                          *ctx, void *scls, const char *option,
173                          const char *value)
174 {
175   unsigned long long *val = scls;
176   if (1 != SSCANF (value, "%llu", val))
177     {
178       fprintf (stderr,
179                _("You must pass a number to the `%s' option.\n"), "-X");
180       return GNUNET_SYSERR;
181     }
182   return GNUNET_OK;
183 }
184
185
186 int
187 GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
188                         void *scls, const char *option, const char *value)
189 {
190   unsigned int *val = scls;
191
192   if (1 != SSCANF (value, "%u", val))
193     {
194       fprintf (stderr,
195                _("You must pass a number to the `%s' option.\n"), "-X");
196       return GNUNET_SYSERR;
197     }
198   return GNUNET_OK;
199 }
200
201
202 /* end of getopt_helpers.c */