diff options
author | Christian Grothoff <christian@grothoff.org> | 2009-05-29 00:46:26 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2009-05-29 00:46:26 +0000 |
commit | 0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9 (patch) | |
tree | 6b552f40eb089db96409a312a98d9b12bd669102 /src/util/getopt_helpers.c | |
download | gnunet-0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9.tar.gz gnunet-0a217a8df1657b4334b55b0e4a6c7837a8dbcfd9.zip |
ng
Diffstat (limited to 'src/util/getopt_helpers.c')
-rw-r--r-- | src/util/getopt_helpers.c | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c new file mode 100644 index 000000000..3aea5d102 --- /dev/null +++ b/src/util/getopt_helpers.c | |||
@@ -0,0 +1,200 @@ | |||
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 | int slen; | ||
54 | int i; | ||
55 | int j; | ||
56 | int ml; | ||
57 | int 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 | slen = 0; | ||
66 | i = 0; | ||
67 | opt = ctx->allOptions; | ||
68 | while (opt[i].description != NULL) | ||
69 | { | ||
70 | if (opt[i].shortName == '\0') | ||
71 | printf (" "); | ||
72 | else | ||
73 | printf (" -%c, ", opt[i].shortName); | ||
74 | printf ("--%s", opt[i].name); | ||
75 | slen = 8 + strlen (opt[i].name); | ||
76 | if (opt[i].argumentHelp != NULL) | ||
77 | { | ||
78 | printf ("=%s", opt[i].argumentHelp); | ||
79 | slen += 1 + strlen (opt[i].argumentHelp); | ||
80 | } | ||
81 | if (slen > BORDER) | ||
82 | { | ||
83 | printf ("\n%*s", BORDER, ""); | ||
84 | slen = BORDER; | ||
85 | } | ||
86 | if (slen < BORDER) | ||
87 | { | ||
88 | printf ("%*s", BORDER - slen, ""); | ||
89 | slen = BORDER; | ||
90 | } | ||
91 | trans = gettext (opt[i].description); | ||
92 | ml = strlen (trans); | ||
93 | p = 0; | ||
94 | OUTER: | ||
95 | while (ml - p > 78 - slen) | ||
96 | { | ||
97 | for (j = p + 78 - slen; j > p; j--) | ||
98 | { | ||
99 | if (isspace (trans[j])) | ||
100 | { | ||
101 | scp = GNUNET_malloc (j - p + 1); | ||
102 | memcpy (scp, &trans[p], j - p); | ||
103 | scp[j - p] = '\0'; | ||
104 | printf ("%s\n%*s", scp, BORDER + 2, ""); | ||
105 | GNUNET_free (scp); | ||
106 | p = j + 1; | ||
107 | slen = BORDER + 2; | ||
108 | goto OUTER; | ||
109 | } | ||
110 | } | ||
111 | /* could not find space to break line */ | ||
112 | scp = GNUNET_malloc (78 - slen + 1); | ||
113 | memcpy (scp, &trans[p], 78 - slen); | ||
114 | scp[78 - slen] = '\0'; | ||
115 | printf ("%s\n%*s", scp, BORDER + 2, ""); | ||
116 | GNUNET_free (scp); | ||
117 | slen = BORDER + 2; | ||
118 | p = p + 78 - slen; | ||
119 | } | ||
120 | /* print rest */ | ||
121 | if (p < ml) | ||
122 | printf ("%s\n", &trans[p]); | ||
123 | if (strlen (trans) == 0) | ||
124 | printf ("\n"); | ||
125 | i++; | ||
126 | } | ||
127 | printf ("Report bugs to gnunet-developers@gnu.org.\n" | ||
128 | "GNUnet home page: http://www.gnu.org/software/gnunet/\n" | ||
129 | "General help using GNU software: http://www.gnu.org/gethelp/\n"); | ||
130 | return GNUNET_SYSERR; | ||
131 | } | ||
132 | |||
133 | |||
134 | int | ||
135 | GNUNET_GETOPT_increment_value (struct | ||
136 | GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
137 | void *scls, const char *cmdLineOption, | ||
138 | const char *value) | ||
139 | { | ||
140 | int *val = scls; | ||
141 | (*val)++; | ||
142 | return GNUNET_OK; | ||
143 | } | ||
144 | |||
145 | int | ||
146 | GNUNET_GETOPT_set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
147 | void *scls, const char *option, const char *value) | ||
148 | { | ||
149 | int *val = scls; | ||
150 | *val = 1; | ||
151 | return GNUNET_OK; | ||
152 | } | ||
153 | |||
154 | int | ||
155 | GNUNET_GETOPT_set_string (struct GNUNET_GETOPT_CommandLineProcessorContext | ||
156 | *ctx, void *scls, const char *option, | ||
157 | const char *value) | ||
158 | { | ||
159 | char **val = scls; | ||
160 | |||
161 | GNUNET_assert (value != NULL); | ||
162 | if (NULL != *val) | ||
163 | GNUNET_free (*val); | ||
164 | *val = GNUNET_strdup (value); | ||
165 | return GNUNET_OK; | ||
166 | } | ||
167 | |||
168 | int | ||
169 | GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext | ||
170 | *ctx, void *scls, const char *option, | ||
171 | const char *value) | ||
172 | { | ||
173 | unsigned long long *val = scls; | ||
174 | if (1 != SSCANF (value, "%llu", val)) | ||
175 | { | ||
176 | fprintf (stderr, | ||
177 | _("You must pass a number to the `%s' option.\n"), "-X"); | ||
178 | return GNUNET_SYSERR; | ||
179 | } | ||
180 | return GNUNET_OK; | ||
181 | } | ||
182 | |||
183 | |||
184 | int | ||
185 | GNUNET_GETOPT_set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
186 | void *scls, const char *option, const char *value) | ||
187 | { | ||
188 | unsigned int *val = scls; | ||
189 | |||
190 | if (1 != SSCANF (value, "%u", val)) | ||
191 | { | ||
192 | fprintf (stderr, | ||
193 | _("You must pass a number to the `%s' option.\n"), "-X"); | ||
194 | return GNUNET_SYSERR; | ||
195 | } | ||
196 | return GNUNET_OK; | ||
197 | } | ||
198 | |||
199 | |||
200 | /* end of getopt_helpers.c */ | ||