help.c
7.79 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* help.c -- general-purpose command line option parser
Copyright (C) 2016 Free Software Foundation, Inc.
GNU Mailutils 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, or (at
your option) any later version.
GNU Mailutils 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/alloc.h>
#include <mailutils/opt.h>
#include <mailutils/cctype.h>
#include <mailutils/nls.h>
#define LMARGIN 2
#define DESCRCOLUMN 30
#define RMARGIN 79
#define GROUPCOLUMN 2
#define USAGECOLUMN 13
static void
indent (size_t start, size_t col)
{
for (; start < col; start++)
putchar (' ');
}
static void
print_option_descr (const char *descr, size_t lmargin, size_t rmargin)
{
while (*descr)
{
size_t s = 0;
size_t i;
size_t width = rmargin - lmargin;
for (i = 0; ; i++)
{
if (descr[i] == 0 || descr[i] == ' ' || descr[i] == '\t')
{
if (i > width)
break;
s = i;
if (descr[i] == 0)
break;
}
}
fwrite (descr, 1, s, stdout);
fputc ('\n', stdout);
descr += s;
if (*descr)
{
indent (0, lmargin);
descr++;
}
}
}
static size_t
print_option (struct mu_option **optbuf, size_t optcnt, size_t num,
int *argsused)
{
struct mu_option *opt = optbuf[num];
size_t next, i;
int delim;
int w;
if (MU_OPTION_IS_GROUP_HEADER (opt))
{
if (num)
putchar ('\n');
indent (0, GROUPCOLUMN);
print_option_descr (gettext (opt->opt_doc), GROUPCOLUMN, RMARGIN);
putchar ('\n');
return num + 1;
}
/* count aliases */
for (next = num + 1;
next < optcnt && optbuf[next]->opt_flags & MU_OPTION_ALIAS;
next++);
if (opt->opt_flags & MU_OPTION_HIDDEN)
return next;
w = 0;
for (i = num; i < next; i++)
{
if (MU_OPTION_IS_VALID_SHORT_OPTION (optbuf[i]))
{
if (w == 0)
{
indent (0, LMARGIN);
w = LMARGIN;
}
else
w += printf (", ");
w += printf ("-%c", optbuf[i]->opt_short);
delim = ' ';
}
}
for (i = num; i < next; i++)
{
if (MU_OPTION_IS_VALID_LONG_OPTION (optbuf[i]))
{
if (w == 0)
{
indent (0, LMARGIN);
w = LMARGIN;
}
else
w += printf (", ");
w += printf ("--%s", optbuf[i]->opt_long);
delim = '=';
}
}
if (opt->opt_arg)
{
*argsused = 1;
if (opt->opt_flags & MU_OPTION_ARG_OPTIONAL)
{
if (delim == '=')
w += printf ("[=%s]", gettext (opt->opt_arg));
else
w += printf ("[%s]", gettext (opt->opt_arg));
}
else
w += printf ("%c%s", delim, gettext (opt->opt_arg));
}
if (w >= DESCRCOLUMN)
{
putchar ('\n');
w = 0;
}
indent (w, DESCRCOLUMN);
print_option_descr (gettext (opt->opt_doc), DESCRCOLUMN, RMARGIN);
return next;
}
void
mu_option_describe_options (struct mu_option **optbuf, size_t optcnt)
{
unsigned i;
int argsused = 0;
for (i = 0; i < optcnt; )
i = print_option (optbuf, optcnt, i, &argsused);
putchar ('\n');
if (argsused)
{
print_option_descr (_("Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options."), 0, RMARGIN);
putchar ('\n');
}
}
void
mu_program_help (struct mu_parseopt *po)
{
printf ("%s", _("Usage:"));
if (po->po_prog_name)
printf (" %s", po->po_prog_name);
printf (" [%s]...", _("OPTION"));
if (po->po_prog_args)
printf (" %s", gettext (po->po_prog_args));
putchar ('\n');
if (po->po_prog_doc)
print_option_descr (gettext (po->po_prog_doc), 0, RMARGIN);
putchar ('\n');
mu_option_describe_options (po->po_optv, po->po_optc);
if (po->po_help_hook)
po->po_help_hook (stdout);
if (po->po_bug_address)
/* TRANSLATORS: The placeholder indicates the bug-reporting address
for this package. Please add _another line_ saying
"Report translation bugs to <...>\n" with the address for translation
bugs (typically your translation team's web or email address). */
printf (_("Report bugs to %s.\n"), po->po_bug_address);
if (po->po_package_name && po->po_package_url)
printf (_("%s home page: <%s>\n"),
po->po_package_name, po->po_package_url);
}
static struct mu_option **option_tab;
static int
cmpidx_short (const void *a, const void *b)
{
unsigned const *ai = (unsigned const *)a;
unsigned const *bi = (unsigned const *)b;
return option_tab[*ai]->opt_short - option_tab[*bi]->opt_short;
}
static int
cmpidx_long (const void *a, const void *b)
{
unsigned const *ai = (unsigned const *)a;
unsigned const *bi = (unsigned const *)b;
struct mu_option const *ap = option_tab[*ai];
struct mu_option const *bp = option_tab[*bi];
return strcmp (ap->opt_long, bp->opt_long);
}
void
mu_program_usage (struct mu_parseopt *po)
{
unsigned i;
unsigned n;
char buf[RMARGIN+1];
unsigned *idxbuf;
unsigned nidx;
struct mu_option **optbuf = po->po_optv;
size_t optcnt = po->po_optc;
#define FLUSH \
do \
{ \
buf[n] = 0; \
printf ("%s\n", buf); \
n = USAGECOLUMN; \
memset (buf, ' ', n); \
} \
while (0)
#define ADDC(c) \
do \
{ \
if (n == RMARGIN) FLUSH; \
buf[n++] = c; \
} \
while (0)
option_tab = optbuf;
idxbuf = mu_calloc (optcnt, sizeof (idxbuf[0]));
n = snprintf (buf, sizeof buf, "%s %s ", _("Usage:"), mu_progname);
/* Print a list of short options without arguments. */
for (i = nidx = 0; i < optcnt; i++)
if (MU_OPTION_IS_VALID_SHORT_OPTION (optbuf[i]) && !optbuf[i]->opt_arg)
idxbuf[nidx++] = i;
if (nidx)
{
qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_short);
ADDC ('[');
ADDC ('-');
for (i = 0; i < nidx; i++)
{
ADDC (optbuf[idxbuf[i]]->opt_short);
}
ADDC (']');
}
/* Print a list of short options with arguments. */
for (i = nidx = 0; i < optcnt; i++)
{
if (MU_OPTION_IS_VALID_SHORT_OPTION (optbuf[i]) && optbuf[i]->opt_arg)
idxbuf[nidx++] = i;
}
if (nidx)
{
qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_short);
for (i = 0; i < nidx; i++)
{
struct mu_option *opt = optbuf[idxbuf[i]];
const char *arg = gettext (opt->opt_arg);
size_t len = 5 + strlen (arg) + 1;
if (n + len > RMARGIN) FLUSH;
buf[n++] = ' ';
buf[n++] = '[';
buf[n++] = '-';
buf[n++] = opt->opt_short;
buf[n++] = ' ';
strcpy (&buf[n], arg);
n += strlen (arg);
buf[n++] = ']';
}
}
/* Print a list of long options */
for (i = nidx = 0; i < optcnt; i++)
{
if (MU_OPTION_IS_VALID_LONG_OPTION (optbuf[i]))
idxbuf[nidx++] = i;
}
if (nidx)
{
qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_long);
for (i = 0; i < nidx; i++)
{
struct mu_option *opt = optbuf[idxbuf[i]];
const char *arg = opt->opt_arg ? gettext (opt->opt_arg) : NULL;
size_t len = 3 + strlen (opt->opt_long)
+ (arg ? 1 + strlen (arg) : 0);
if (n + len > RMARGIN) FLUSH;
buf[n++] = ' ';
buf[n++] = '[';
buf[n++] = '-';
buf[n++] = '-';
strcpy (&buf[n], opt->opt_long);
n += strlen (opt->opt_long);
if (opt->opt_arg)
{
buf[n++] = '=';
strcpy (&buf[n], arg);
n += strlen (arg);
}
buf[n++] = ']';
}
}
FLUSH;
free (idxbuf);
}