84
213
if (opt->longName) nb += strlen(opt->longName);
85
214
if (argDescrip) nb += strlen(argDescrip);
87
217
left = malloc(nb);
88
left[0] = left[maxLeftCol] = '\0';
218
if (left == NULL) return; /* XXX can't happen */
220
left[maxLeftCol] = '\0';
90
222
if (opt->longName && opt->shortName)
91
223
sprintf(left, "-%c, %s%s", opt->shortName,
92
224
((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
94
else if (opt->shortName)
226
else if (opt->shortName != '\0')
95
227
sprintf(left, "-%c", opt->shortName);
96
228
else if (opt->longName)
97
229
sprintf(left, "%s%s",
98
230
((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
100
232
if (!*left) goto out;
101
234
if (argDescrip) {
102
235
char * le = left + strlen(left);
103
237
if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
240
/* Choose type of output */
242
if (opt->argInfo & POPT_ARGFLAG_SHOW_DEFAULT) {
243
defs = singleOptionDefaultValue(lineLength, opt, translation_domain);
245
char * t = malloc((help ? strlen(help) : 0) +
246
strlen(defs) + sizeof(" "));
251
strcpy(te, help); te += strlen(te);
105
262
if (opt->argDescrip == NULL) {
106
263
switch (opt->argInfo & POPT_ARG_MASK) {
107
264
case POPT_ARG_NONE:
108
sprintf(le, "[true]");
110
266
case POPT_ARG_VAL:
111
{ long aLong = opt->val;
267
#ifdef NOTNOW /* XXX pug ugly nerdy output */
268
{ long aLong = opt->val;
269
int ops = (opt->argInfo & POPT_ARGFLAG_LOGICALOPS);
270
int negate = (opt->argInfo & POPT_ARGFLAG_NOT);
113
if (opt->argInfo & POPT_ARGFLAG_NOT) aLong = ~aLong;
114
switch (opt->argInfo & POPT_ARGFLAG_LOGICALOPS) {
272
/* Don't bother displaying typical values */
273
if (!ops && (aLong == 0L || aLong == 1L || aLong == -1L))
115
277
case POPT_ARGFLAG_OR:
116
sprintf(le, "[|=0x%lx]", aLong); break;
279
/*@innerbreak@*/ break;
117
280
case POPT_ARGFLAG_AND:
118
sprintf(le, "[&=0x%lx]", aLong); break;
282
/*@innerbreak@*/ break;
119
283
case POPT_ARGFLAG_XOR:
120
sprintf(le, "[^=0x%lx]", aLong); break;
285
/*@innerbreak@*/ break;
122
if (!(aLong == 0L || aLong == 1L || aLong == -1L))
123
sprintf(le, "[=%ld]", aLong);
287
/*@innerbreak@*/ break;
290
if (negate) *le++ = '~';
292
le += sprintf(le, (ops ? "0x%lx" : "%ld"), aLong);
127
298
case POPT_ARG_INT:
128
299
case POPT_ARG_LONG:
129
case POPT_ARG_STRING:
130
300
case POPT_ARG_FLOAT:
131
301
case POPT_ARG_DOUBLE:
132
sprintf(le, "=%s", argDescrip);
302
case POPT_ARG_STRING:
304
strcpy(le, argDescrip); le += strlen(le);
136
sprintf(le, "=%s", argDescrip);
311
strcpy(le, argDescrip); le += strlen(le);
139
313
if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
145
fprintf(f," %-*s ", maxLeftCol, left);
320
fprintf(fp," %-*s ", maxLeftCol, left);
147
fprintf(f," %s\n", left);
322
fprintf(fp," %s\n", left);
328
help = defs; defs = NULL;
151
331
helpLength = strlen(help);
152
333
while (helpLength > lineLength) {
156
337
ch = help + lineLength - 1;
157
338
while (ch > help && !isspace(*ch)) ch--;
209
static void singleTableHelp(FILE * f, const struct poptOption * table,
211
const char *translation_domain)
404
* Display popt alias and exec help.
405
* @param fp output file handle
406
* @param items alias/exec array
407
* @param nitems no. of alias/exec entries
409
* @param translation_domain translation domain
411
static void itemHelp(FILE * fp,
412
/*@null@*/ poptItem items, int nitems, int left,
413
/*@null@*/ const char * translation_domain)
414
/*@globals fileSystem @*/
415
/*@modifies *fp, fileSystem @*/
421
for (i = 0, item = items; i < nitems; i++, item++) {
422
const struct poptOption * opt;
424
if ((opt->longName || opt->shortName) &&
425
!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
426
singleOptionHelp(fp, left, opt, translation_domain);
431
* Display help text for a table of options.
433
* @param fp output file handle
434
* @param table option(s)
436
* @param translation_domain translation domain
438
static void singleTableHelp(poptContext con, FILE * fp,
439
/*@null@*/ const struct poptOption * table, int left,
440
/*@null@*/ const char * translation_domain)
441
/*@globals fileSystem @*/
442
/*@modifies *fp, fileSystem @*/
213
444
const struct poptOption * opt;
214
445
const char *sub_transdom;
447
if (table == poptAliasOptions) {
448
itemHelp(fp, con->aliases, con->numAliases, left, NULL);
449
itemHelp(fp, con->execs, con->numExecs, left, NULL);
216
454
for (opt = table; (opt->longName || opt->shortName || opt->arg); opt++) {
217
455
if ((opt->longName || opt->shortName) &&
218
456
!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
219
singleOptionHelp(f, left, opt, translation_domain);
457
singleOptionHelp(fp, left, opt, translation_domain);
222
461
for (opt = table; (opt->longName || opt->shortName || opt->arg); opt++) {
223
if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
224
sub_transdom = getTableTranslationDomain(opt->arg);
226
sub_transdom = translation_domain;
462
if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_INCLUDE_TABLE)
464
sub_transdom = getTableTranslationDomain(opt->arg);
465
if (sub_transdom == NULL)
466
sub_transdom = translation_domain;
229
fprintf(f, "\n%s\n", D_(sub_transdom, opt->descrip));
469
fprintf(fp, "\n%s\n", D_(sub_transdom, opt->descrip));
231
singleTableHelp(f, opt->arg, left, sub_transdom);
471
singleTableHelp(con, fp, opt->arg, left, sub_transdom);
236
static int showHelpIntro(poptContext con, FILE * f)
477
* @param fp output file handle
479
static int showHelpIntro(poptContext con, FILE * fp)
480
/*@globals fileSystem @*/
481
/*@modifies *fp, fileSystem @*/
241
fprintf(f, POPT_("Usage:"));
486
fprintf(fp, POPT_("Usage:"));
242
487
if (!(con->flags & POPT_CONTEXT_KEEP_FIRST)) {
489
/*@-nullderef@*/ /* LCL: wazzup? */
243
490
fn = con->optionStack->argv[0];
493
if (fn == NULL) return len;
244
494
if (strchr(fn, '/')) fn = strrchr(fn, '/') + 1;
245
fprintf(f, " %s", fn);
495
fprintf(fp, " %s", fn);
246
496
len += strlen(fn) + 1;
252
void poptPrintHelp(poptContext con, FILE * f, /*@unused@*/ int flags)
502
void poptPrintHelp(poptContext con, FILE * fp, /*@unused@*/ int flags)
254
504
int leftColWidth;
256
showHelpIntro(con, f);
506
(void) showHelpIntro(con, fp);
257
507
if (con->otherHelp)
258
fprintf(f, " %s\n", con->otherHelp);
508
fprintf(fp, " %s\n", con->otherHelp);
260
fprintf(f, " %s\n", POPT_("[OPTION...]"));
510
fprintf(fp, " %s\n", POPT_("[OPTION...]"));
262
512
leftColWidth = maxArgWidth(con->options, NULL);
263
singleTableHelp(f, con->options, leftColWidth, NULL);
513
singleTableHelp(con, fp, con->options, leftColWidth, NULL);
266
static int singleOptionUsage(FILE * f, int cursor,
267
const struct poptOption * opt,
268
const char *translation_domain)
517
* @param fp output file handle
519
* @param opt option(s)
520
* @param translation_domain translation domain
522
static int singleOptionUsage(FILE * fp, int cursor,
523
const struct poptOption * opt,
524
/*@null@*/ const char *translation_domain)
525
/*@globals fileSystem @*/
526
/*@modifies *fp, fileSystem @*/
271
529
char shortStr[2] = { '\0', '\0' };
272
530
const char * item = shortStr;
273
531
const char * argDescrip = getArgDescrip(opt, translation_domain);
275
if (opt->shortName) {
276
if (!(opt->argInfo & POPT_ARG_MASK))
277
return cursor; /* we did these already */
533
if (opt->shortName != '\0' && opt->longName != NULL) {
535
if (!(opt->argInfo & POPT_ARGFLAG_ONEDASH)) len++;
536
len += strlen(opt->longName);
537
} else if (opt->shortName != '\0') {
279
539
shortStr[0] = opt->shortName;
280
540
shortStr[1] = '\0';
281
541
} else if (opt->longName) {
282
len += 1 + strlen(opt->longName);
542
len += strlen(opt->longName);
543
if (!(opt->argInfo & POPT_ARGFLAG_ONEDASH)) len++;
283
544
item = opt->longName;
286
if (len == 3) return cursor;
547
if (len == 4) return cursor;
289
550
len += strlen(argDescrip) + 1;
291
552
if ((cursor + len) > 79) {
296
fprintf(f, " [-%s%s%s%s]",
297
((opt->shortName || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) ? "" : "-"),
299
(argDescrip ? (opt->shortName ? " " : "=") : ""),
300
(argDescrip ? argDescrip : ""));
557
if (opt->longName && opt->shortName) {
558
fprintf(fp, " [-%c|-%s%s%s%s]",
559
opt->shortName, ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "" : "-"),
561
(argDescrip ? " " : ""),
562
(argDescrip ? argDescrip : ""));
564
fprintf(fp, " [-%s%s%s%s]",
565
((opt->shortName || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) ? "" : "-"),
567
(argDescrip ? (opt->shortName != '\0' ? " " : "=") : ""),
568
(argDescrip ? argDescrip : ""));
302
571
return cursor + len + 1;
305
static int singleTableUsage(FILE * f, int cursor,
306
const struct poptOption * opt, const char * translation_domain)
575
* Display popt alias and exec usage.
576
* @param fp output file handle
578
* @param item alias/exec array
579
* @param nitems no. of ara/exec entries
580
* @param translation_domain translation domain
582
static int itemUsage(FILE * fp, int cursor, poptItem item, int nitems,
583
/*@null@*/ const char * translation_domain)
584
/*@globals fileSystem @*/
585
/*@modifies *fp, fileSystem @*/
589
/*@-branchstate@*/ /* FIX: W2DO? */
591
for (i = 0; i < nitems; i++, item++) {
592
const struct poptOption * opt;
594
if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN) {
595
translation_domain = (const char *)opt->arg;
596
} else if ((opt->longName || opt->shortName) &&
597
!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
598
cursor = singleOptionUsage(fp, cursor, opt, translation_domain);
607
* Keep track of option tables already processed.
609
typedef struct poptDone_s {
616
* Display usage text for a table of options.
618
* @param fp output file handle
620
* @param opt option(s)
621
* @param translation_domain translation domain
622
* @param done tables already processed
625
static int singleTableUsage(poptContext con, FILE * fp, int cursor,
626
/*@null@*/ const struct poptOption * opt,
627
/*@null@*/ const char * translation_domain,
628
/*@null@*/ poptDone done)
629
/*@globals fileSystem @*/
630
/*@modifies *fp, done, fileSystem @*/
632
/*@-branchstate@*/ /* FIX: W2DO? */
308
634
for (; (opt->longName || opt->shortName || opt->arg) ; opt++) {
309
if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN)
635
if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN) {
310
636
translation_domain = (const char *)opt->arg;
311
else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE)
312
cursor = singleTableUsage(f, cursor, opt->arg, translation_domain);
313
else if ((opt->longName || opt->shortName) &&
314
!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
315
cursor = singleOptionUsage(f, cursor, opt, translation_domain);
637
} else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
640
for (i = 0; i < done->nopts; i++) {
642
const void * that = done->opts[i];
644
if (that == NULL || that != opt->arg)
645
/*@innercontinue@*/ continue;
646
/*@innerbreak@*/ break;
648
/* Skip if this table has already been processed. */
649
if (opt->arg == NULL || i < done->nopts)
652
if (done->nopts < done->maxopts)
653
done->opts[done->nopts++] = (const void *) opt->arg;
656
cursor = singleTableUsage(con, fp, cursor, opt->arg,
657
translation_domain, done);
658
} else if ((opt->longName || opt->shortName) &&
659
!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
660
cursor = singleOptionUsage(fp, cursor, opt, translation_domain);
321
static int showShortOptions(const struct poptOption * opt, FILE * f, char * str)
669
* Return concatenated short options for display.
670
* @todo Sub-tables should be recursed.
671
* @param opt option(s)
672
* @param fp output file handle
673
* @retval str concatenation of short options
674
* @return length of display string
676
static int showShortOptions(const struct poptOption * opt, FILE * fp,
677
/*@null@*/ char * str)
678
/*@globals fileSystem @*/
679
/*@modifies *str, *fp, fileSystem @*/
323
char s[300]; /* this is larger then the ascii set, so
324
it should do just fine */
681
char * s = alloca(300); /* larger then the ascii set */
684
/*@-branchstate@*/ /* FIX: W2DO? */
327
685
if (str == NULL) {
328
686
memset(s, 0, sizeof(s));
332
693
for (; (opt->longName || opt->shortName || opt->arg); opt++) {
333
694
if (opt->shortName && !(opt->argInfo & POPT_ARG_MASK))
334
695
str[strlen(str)] = opt->shortName;
335
696
else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE)
336
showShortOptions(opt->arg, f, str);
697
if (opt->arg) /* XXX program error */
698
(void) showShortOptions(opt->arg, fp, str);
702
if (s != str || *s != '\0')
342
fprintf(f, " [-%s]", s);
705
fprintf(fp, " [-%s]", s);
343
706
return strlen(s) + 4;
346
void poptPrintUsage(poptContext con, FILE * f, /*@unused@*/ int flags)
709
void poptPrintUsage(poptContext con, FILE * fp, /*@unused@*/ int flags)
711
poptDone done = memset(alloca(sizeof(*done)), 0, sizeof(*done));
350
cursor = showHelpIntro(con, f);
351
cursor += showShortOptions(con->options, f, NULL);
352
singleTableUsage(f, cursor, con->options, NULL);
716
cursor = done->maxopts * sizeof(*done->opts);
718
done->opts = memset(alloca(cursor), 0, cursor);
719
done->opts[done->nopts++] = (const void *) con->options;
722
cursor = showHelpIntro(con, fp);
723
cursor += showShortOptions(con->options, fp, NULL);
724
cursor = singleTableUsage(con, fp, cursor, con->options, NULL, done);
725
cursor = itemUsage(fp, cursor, con->aliases, con->numAliases, NULL);
726
cursor = itemUsage(fp, cursor, con->execs, con->numExecs, NULL);
354
728
if (con->otherHelp) {
355
729
cursor += strlen(con->otherHelp) + 1;
356
if (cursor > 79) fprintf(f, "\n ");
357
fprintf(f, " %s", con->otherHelp);
730
if (cursor > 79) fprintf(fp, "\n ");
731
fprintf(fp, " %s", con->otherHelp);
363
void poptSetOtherOptionHelp(poptContext con, const char * text) {
364
if (con->otherHelp) free((void *)con->otherHelp);
737
void poptSetOtherOptionHelp(poptContext con, const char * text)
739
con->otherHelp = _free(con->otherHelp);
365
740
con->otherHelp = xstrdup(text);