help.c
12.4 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/* 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 <limits.h>
#include <errno.h>
#include <unistd.h>
#include <mailutils/alloc.h>
#include <mailutils/opt.h>
#include <mailutils/cctype.h>
#include <mailutils/nls.h>
#include <mailutils/wordsplit.h>
unsigned short_opt_col = 2;
unsigned long_opt_col = 6;
/*FIXME: doc_opt_col? */
unsigned header_col = 1;
unsigned opt_doc_col = 29;
unsigned usage_indent = 12;
unsigned rmargin = 79;
unsigned dup_args = 0;
unsigned dup_args_note = 1;
enum usage_var_type
{
usage_var_column,
usage_var_bool
};
static struct usage_var
{
char *name;
unsigned *valptr;
enum usage_var_type type;
} usage_var[] = {
{ "short-opt-col", &short_opt_col, usage_var_column },
{ "header-col", &header_col, usage_var_column },
{ "opt-doc-col", &opt_doc_col, usage_var_column },
{ "usage-indent", &usage_indent, usage_var_column },
{ "rmargin", &rmargin, usage_var_column },
{ "dup-args", &dup_args, usage_var_bool },
{ "dup-args-note", &dup_args_note, usage_var_bool },
{ "long-opt-col", &long_opt_col, usage_var_column },
{ "doc_opt_col", NULL, usage_var_column },
{ NULL }
};
unsigned
mu_parseopt_getcolumn (const char *name)
{
struct usage_var *p;
unsigned retval = 0;
for (p = usage_var; p->name; p++)
{
if (strcmp (p->name, name) == 0)
{
if (p->valptr)
retval = *p->valptr;
break;
}
}
return retval;
}
static void
set_usage_var (struct mu_parseopt *po, char const *id)
{
struct usage_var *p;
size_t len;
int boolval = 1;
if (strlen (id) > 3 && memcmp (id, "no-", 3) == 0)
{
id += 3;
boolval = 0;
}
len = strcspn (id, "=");
for (p = usage_var; p->name; p++)
{
if (strlen (p->name) == len && memcmp (p->name, id, len) == 0)
{
if (!p->valptr)
return;
if (p->type == usage_var_bool)
{
if (id[len])
{
if (po->po_prog_name)
fprintf (stderr, "%s: ", po->po_prog_name);
fprintf (stderr,
"error in ARGP_HELP_FMT: improper usage of [no-]%s\n",
id);
return;
}
*p->valptr = boolval;
return;
}
if (id[len])
{
char *endp;
unsigned long val;
errno = 0;
val = strtoul (id + len + 1, &endp, 10);
if (errno || *endp)
{
if (po->po_prog_name)
fprintf (stderr, "%s: ", po->po_prog_name);
fprintf (stderr,
"error in ARGP_HELP_FMT: bad value for %s\n",
id);
}
else if (val > UINT_MAX)
{
if (po->po_prog_name)
fprintf (stderr, "%s: ", po->po_prog_name);
fprintf (stderr,
"error in ARGP_HELP_FMT: %s value is out of range\n",
id);
}
else
*p->valptr = val;
}
else
{
if (po->po_prog_name)
fprintf (stderr, "%s: ", po->po_prog_name);
fprintf (stderr,
"%s: ARGP_HELP_FMT parameter requires a value\n",
id);
return;
}
return;
}
}
if (po->po_prog_name)
fprintf (stderr, "%s: ", po->po_prog_name);
fprintf (stderr,
"%s: Unknown ARGP_HELP_FMT parameter\n",
id);
}
static void
init_usage_vars (struct mu_parseopt *po)
{
char *fmt;
struct mu_wordsplit ws;
size_t i;
fmt = getenv ("ARGP_HELP_FMT");
if (!fmt)
return;
ws.ws_delim = ",";
if (mu_wordsplit (fmt, &ws,
MU_WRDSF_DELIM | MU_WRDSF_NOVAR | MU_WRDSF_NOCMD
| MU_WRDSF_WS | MU_WRDSF_SHOWERR))
return;
for (i = 0; i < ws.ws_wordc; i++)
{
set_usage_var (po, ws.ws_wordv[i]);
}
mu_wordsplit_free (&ws);
}
static int
indent (int start, int col)
{
for (; start < col; start++)
putchar (' ');
return start;
}
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;
}
else if (descr[i] == '\n')
{
s = i;
break;
}
}
fwrite (descr, 1, s, stdout);
fputc ('\n', stdout);
descr += s;
if (*descr)
{
indent (0, lmargin);
descr++;
}
}
}
void
mu_parseopt_fmt_text (const char *text, size_t col)
{
print_option_descr (text, col, rmargin);
}
static size_t
print_opt_arg (struct mu_option *opt, int delim)
{
int w = 0;
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));
return w;
}
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');
if (opt->opt_doc[0])
{
indent (0, header_col);
print_option_descr (gettext (opt->opt_doc), header_col, rmargin);
}
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, short_opt_col);
w = short_opt_col;
}
else
w += printf (", ");
w += printf ("-%c", optbuf[i]->opt_short);
delim = ' ';
if (opt->opt_arg && dup_args)
w += print_opt_arg (opt, delim);
}
}
for (i = num; i < next; i++)
{
if (MU_OPTION_IS_VALID_LONG_OPTION (optbuf[i]))
{
if (w == 0)
{
indent (0, short_opt_col);
w = short_opt_col;
}
else
w += printf (", ");
if (i == num)
w = indent (w, long_opt_col);
w += printf ("--%s", optbuf[i]->opt_long);
delim = '=';
if (opt->opt_arg && dup_args)
w += print_opt_arg (opt, delim);
}
}
if (opt->opt_arg)
{
*argsused = 1;
if (!dup_args)
w += print_opt_arg (opt, delim);
}
if (w >= opt_doc_col)
{
putchar ('\n');
w = 0;
}
indent (w, opt_doc_col);
print_option_descr (gettext (opt->opt_doc), opt_doc_col, 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 && dup_args_note)
{
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)
{
init_usage_vars (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 (po, 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);
if (po->po_flags & MU_PARSEOPT_EXTRA_INFO)
print_option_descr (_(po->po_extra_info), 0, rmargin);
}
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;
int ac = option_tab[*ai]->opt_short;
int bc = option_tab[*bi]->opt_short;
int d;
if (mu_isalpha (ac))
{
if (!mu_isalpha (bc))
return -1;
}
else if (mu_isalpha (bc))
return 1;
d = mu_tolower (ac) - mu_tolower (bc);
if (d == 0)
d = mu_isupper (ac) ? 1 : -1;
return d;
}
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 = usage_indent; \
if (n) memset (buf, ' ', n); \
} \
while (0)
#define ADDC(c) \
do \
{ \
if (n == rmargin) FLUSH; \
buf[n++] = c; \
} \
while (0)
init_usage_vars (po);
option_tab = optbuf;
idxbuf = mu_calloc (optcnt, sizeof (idxbuf[0]));
n = snprintf (buf, sizeof buf, "%s %s ", _("Usage:"), po->po_prog_name);
/* 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;
else
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 = 5 + strlen (opt->opt_long)
+ (arg ? 1 + strlen (arg) : 0);
if (n + len >= rmargin)
FLUSH;
else
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++] = ']';
}
}
if (po->po_flags & MU_PARSEOPT_PROG_ARGS)
{
char const *p = po->po_prog_args;
if (n + 1 >= rmargin)
FLUSH;
buf[n++] = ' ';
while (*p)
{
size_t len = strcspn (p, " \t\n");
if (len == 0)
len = 1;
if (n + len >= rmargin)
FLUSH;
else
{
memcpy (buf + n, p, len);
p += len;
n += len;
}
}
}
FLUSH;
free (idxbuf);
}