1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
4 * \file popt/popthelp.c
7 /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
8 file accompanying popt source distributions, available from
9 ftp://ftp.rpm.org/pub/rpm/dist. */
13 /*#define POPT_WCHAR_HACK*/
14 #ifdef POPT_WCHAR_HACK
15 #include <wchar.h> /* for mbsrtowcs */
16 /*@access mbstate_t @*/
20 /*@access poptContext@*/
26 * @param key option(s)
28 * @param data (unused)
30 static void displayArgs(poptContext con,
31 /*@unused@*/ UNUSED(enum poptCallbackReason foo),
32 struct poptOption * key,
33 /*@unused@*/ UNUSED(const char * arg), /*@unused@*/ UNUSED(void * data))
34 /*@globals fileSystem@*/
35 /*@modifies fileSystem@*/
37 if (key->shortName == '?')
38 poptPrintHelp(con, stdout, 0);
40 poptPrintUsage(con, stdout, 0);
46 static int show_option_defaults = 0;
50 * Empty table marker to enable displaying popt alias/exec options.
52 /*@observer@*/ /*@unchecked@*/
53 struct poptOption poptAliasOptions[] = {
58 * Auto help table options.
61 /*@observer@*/ /*@unchecked@*/
62 struct poptOption poptHelpOptions[] = {
63 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&displayArgs, '\0', NULL, NULL },
64 { "help", '?', 0, NULL, '?', N_("Show this help message"), NULL },
65 { "usage", '\0', 0, NULL, 'u', N_("Display brief usage message"), NULL },
69 /*@observer@*/ /*@unchecked@*/
70 static struct poptOption poptHelpOptions2[] = {
72 { NULL, '\0', POPT_ARG_INTL_DOMAIN, PACKAGE, 0, NULL, NULL},
74 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&displayArgs, '\0', NULL, NULL },
75 { "help", '?', 0, NULL, '?', N_("Show this help message"), NULL },
76 { "usage", '\0', 0, NULL, 'u', N_("Display brief usage message"), NULL },
78 { "defaults", '\0', POPT_ARG_NONE, &show_option_defaults, 0,
79 N_("Display option defaults in message"), NULL },
84 /*@observer@*/ /*@unchecked@*/
85 struct poptOption * poptHelpOptionsI18N = poptHelpOptions2;
89 * @param table option(s)
91 /*@observer@*/ /*@null@*/ static const char *
92 getTableTranslationDomain(/*@null@*/ const struct poptOption *table)
95 const struct poptOption *opt;
98 for (opt = table; opt->longName || opt->shortName || opt->arg; opt++) {
99 if (opt->argInfo == POPT_ARG_INTL_DOMAIN)
106 * @param opt option(s)
107 * @param translation_domain translation domain
109 /*@observer@*/ /*@null@*/ static const char *
110 getArgDescrip(const struct poptOption * opt,
111 /*@-paramuse@*/ /* FIX: i18n macros disabled with lclint */
112 /*@null@*/ UNUSED(const char * translation_domain))
116 if (!(opt->argInfo & POPT_ARG_MASK)) return NULL;
118 if (opt == (poptHelpOptions + 1) || opt == (poptHelpOptions + 2))
119 if (opt->argDescrip) return POPT_(opt->argDescrip);
121 if (opt->argDescrip) return D_(translation_domain, opt->argDescrip);
123 switch (opt->argInfo & POPT_ARG_MASK) {
124 /*case POPT_ARG_NONE: return POPT_("NONE");*/ /* impossible */
126 case POPT_ARG_VAL: return POPT_("VAL");
128 case POPT_ARG_VAL: return NULL;
130 case POPT_ARG_INT: return POPT_("INT");
131 case POPT_ARG_LONG: return POPT_("LONG");
132 case POPT_ARG_STRING: return POPT_("STRING");
133 case POPT_ARG_FLOAT: return POPT_("FLOAT");
134 case POPT_ARG_DOUBLE: return POPT_("DOUBLE");
135 default: return POPT_("ARG");
140 * Display default value for an option.
141 * @param lineLength display positions remaining
142 * @param opt option(s)
143 * @param translation_domain translation domain
146 static /*@only@*/ /*@null@*/ char *
147 singleOptionDefaultValue(size_t lineLength,
148 const struct poptOption * opt,
149 /*@-paramuse@*/ /* FIX: i18n macros disabled with lclint */
150 /*@null@*/ UNUSED(const char * translation_domain))
154 const char * defstr = D_(translation_domain, "default");
155 size_t limit, bufsize = 4*lineLength + 1;
156 char * le = malloc(bufsize);
159 if (le == NULL) return NULL; /* XXX can't happen */
162 le += strlcpy(le, defstr, bufsize - 3);
165 limit = bufsize - (le - l) - 1; /* -1 for closing paren */
166 if (opt->arg) /* XXX programmer error */
167 switch (opt->argInfo & POPT_ARG_MASK) {
170 { long aLong = *((int *)opt->arg);
171 le += snprintf(le, limit, "%ld", aLong);
174 { long aLong = *((long *)opt->arg);
175 le += snprintf(le, limit, "%ld", aLong);
178 { double aDouble = *((float *)opt->arg);
179 le += snprintf(le, limit, "%g", aDouble);
181 case POPT_ARG_DOUBLE:
182 { double aDouble = *((double *)opt->arg);
183 le += snprintf(le, limit, "%g", aDouble);
185 case POPT_ARG_STRING:
186 { const char * s = *(const char **)opt->arg;
188 le += strlcpy(le, "null", limit);
191 limit -= 2; /* make room for quotes */
193 len = strlcpy(le, s, limit);
196 *le++ = '.'; *le++ = '.'; *le++ = '.';
206 /*@notreached@*/ break;
216 * Display help text for an option.
217 * @param fp output file handle
218 * @param maxLeftCol largest argument display width
219 * @param opt option(s)
220 * @param translation_domain translation domain
222 static void singleOptionHelp(FILE * fp, size_t maxLeftCol,
223 const struct poptOption * opt,
224 /*@null@*/ UNUSED(const char * translation_domain))
225 /*@globals fileSystem @*/
226 /*@modifies *fp, fileSystem @*/
228 size_t indentLength = maxLeftCol + 5;
229 size_t lineLength = 79 - indentLength;
230 const char * help = D_(translation_domain, opt->descrip);
231 const char * argDescrip = getArgDescrip(opt, translation_domain);
236 size_t nb = maxLeftCol + 1;
239 /* Make sure there's more than enough room in target buffer. */
240 if (opt->longName) nb += strlen(opt->longName);
241 if (argDescrip) nb += strlen(argDescrip);
245 if (left == NULL) return; /* XXX can't happen */
247 left[maxLeftCol] = '\0';
249 if (opt->longName && opt->shortName)
250 snprintf(left, nb, "-%c, %s%s", opt->shortName,
251 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
253 else if (opt->shortName != '\0')
254 snprintf(left, nb, "-%c", opt->shortName);
255 else if (opt->longName)
256 snprintf(left, nb, "%s%s",
257 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
259 if (!*left) goto out;
262 char * le = left + strlen(left);
264 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
267 /* Choose type of output */
269 if (opt->argInfo & POPT_ARGFLAG_SHOW_DEFAULT) {
270 defs = singleOptionDefaultValue(lineLength, opt, translation_domain);
272 size_t bufsize = (help ? strlen(help) : 0) + sizeof " " + strlen(defs);
273 char * t = malloc(bufsize);
275 snprintf(t, bufsize, "%s %s", help ? help : "", defs);
283 if (opt->argDescrip == NULL) {
284 switch (opt->argInfo & POPT_ARG_MASK) {
288 #ifdef NOTNOW /* XXX pug ugly nerdy output */
289 { long aLong = opt->val;
290 int ops = (opt->argInfo & POPT_ARGFLAG_LOGICALOPS);
291 int negate = (opt->argInfo & POPT_ARGFLAG_NOT);
293 /* Don't bother displaying typical values */
294 if (!ops && (aLong == 0L || aLong == 1L || aLong == -1L))
298 case POPT_ARGFLAG_OR:
300 /*@innerbreak@*/ break;
301 case POPT_ARGFLAG_AND:
303 /*@innerbreak@*/ break;
304 case POPT_ARGFLAG_XOR:
306 /*@innerbreak@*/ break;
308 /*@innerbreak@*/ break;
310 *le++ = (opt->longName != NULL ? '=' : ' ');
311 if (negate) *le++ = '~';
313 limit = nb - (le - left);
314 lelen = snprintf(le, limit, (ops ? "0x%lx" : "%ld"), aLong);
315 le += lelen >= limit ? limit - 1 : lelen;
324 case POPT_ARG_DOUBLE:
325 case POPT_ARG_STRING:
326 *le++ = (opt->longName != NULL ? '=' : ' ');
327 limit = nb - (le - left);
328 lelen = strlcpy(le, argDescrip, limit);
329 le += lelen >= limit ? limit - 1 : lelen;
337 limit = nb - (le - left);
338 lelen = strlcpy(le, argDescrip, limit);
343 #ifdef POPT_WCHAR_HACK
344 { const char * scopy = argDescrip;
348 memset ((void *)&t, '\0', sizeof (t)); /* In initial state. */
349 /* Determine number of characters. */
350 n = mbsrtowcs (NULL, &scopy, strlen(scopy), &t);
352 displaypad = (int) (lelen-n);
356 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
363 fprintf(fp," %-*s ", (int)maxLeftCol+displaypad, left);
365 fprintf(fp," %s\n", left);
377 helpLength = strlen(help);
379 while (helpLength > lineLength) {
383 ch = help + lineLength - 1;
384 while (ch > help && !isSpace(ch)) ch--;
385 if (ch == help) break; /* give up */
386 while (ch > (help + 1) && isSpace(ch)) ch--;
389 snprintf(format, sizeof format, "%%.%ds\n%%%ds", (int) (ch - help), (int) indentLength);
391 fprintf(fp, format, help, " ");
394 while (isSpace(help) && *help) help++;
395 helpLength = strlen(help);
399 if (helpLength) fprintf(fp, "%s\n", help);
402 /*@-dependenttrans@*/
404 /*@=dependenttrans@*/
409 * Find display width for longest argument string.
410 * @param opt option(s)
411 * @param translation_domain translation domain
412 * @return display width
414 static size_t maxArgWidth(const struct poptOption * opt,
415 /*@null@*/ UNUSED(const char * translation_domain))
423 while (opt->longName || opt->shortName || opt->arg) {
424 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
425 if (opt->arg) /* XXX program error */
426 len = maxArgWidth(opt->arg, translation_domain);
427 if (len > max) max = len;
428 } else if (!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
430 if (opt->shortName != '\0') len += sizeof("-X")-1;
431 if (opt->shortName != '\0' && opt->longName) len += sizeof(", ")-1;
433 len += ((opt->argInfo & POPT_ARGFLAG_ONEDASH)
434 ? sizeof("-")-1 : sizeof("--")-1);
435 len += strlen(opt->longName);
438 s = getArgDescrip(opt, translation_domain);
440 #ifdef POPT_WCHAR_HACK
441 /* XXX Calculate no. of display characters. */
443 const char * scopy = s;
448 memset ((void *)&t, '\0', sizeof (t)); /* In initial state. */
450 /* Determine number of characters. */
451 n = mbsrtowcs (NULL, &scopy, strlen(scopy), &t);
452 len += sizeof("=")-1 + n;
456 len += sizeof("=")-1 + strlen(s);
459 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL) len += sizeof("[]")-1;
460 if (len > max) max = len;
470 * Display popt alias and exec help.
471 * @param fp output file handle
472 * @param items alias/exec array
473 * @param nitems no. of alias/exec entries
474 * @param left largest argument display width
475 * @param translation_domain translation domain
477 static void itemHelp(FILE * fp,
478 /*@null@*/ poptItem items, int nitems, size_t left,
479 /*@null@*/ UNUSED(const char * translation_domain))
480 /*@globals fileSystem @*/
481 /*@modifies *fp, fileSystem @*/
487 for (i = 0, item = items; i < nitems; i++, item++) {
488 const struct poptOption * opt;
490 if ((opt->longName || opt->shortName) &&
491 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
492 singleOptionHelp(fp, left, opt, translation_domain);
497 * Display help text for a table of options.
499 * @param fp output file handle
500 * @param table option(s)
501 * @param left largest argument display width
502 * @param translation_domain translation domain
504 static void singleTableHelp(poptContext con, FILE * fp,
505 /*@null@*/ const struct poptOption * table, size_t left,
506 /*@null@*/ UNUSED(const char * translation_domain))
507 /*@globals fileSystem @*/
508 /*@modifies *fp, fileSystem @*/
510 const struct poptOption * opt;
511 const char *sub_transdom;
513 if (table == poptAliasOptions) {
514 itemHelp(fp, con->aliases, con->numAliases, left, NULL);
515 itemHelp(fp, con->execs, con->numExecs, left, NULL);
520 for (opt = table; (opt->longName || opt->shortName || opt->arg); opt++) {
521 if ((opt->longName || opt->shortName) &&
522 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
523 singleOptionHelp(fp, left, opt, translation_domain);
527 for (opt = table; (opt->longName || opt->shortName || opt->arg); opt++) {
528 if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_INCLUDE_TABLE)
530 sub_transdom = getTableTranslationDomain(opt->arg);
531 if (sub_transdom == NULL)
532 sub_transdom = translation_domain;
535 fprintf(fp, "\n%s\n", D_(sub_transdom, opt->descrip));
537 singleTableHelp(con, fp, opt->arg, left, sub_transdom);
543 * @param fp output file handle
545 static int showHelpIntro(poptContext con, FILE * fp)
546 /*@globals fileSystem @*/
547 /*@modifies *fp, fileSystem @*/
552 fprintf(fp, POPT_("Usage:"));
553 if (!(con->flags & POPT_CONTEXT_KEEP_FIRST)) {
555 /*@-nullderef -type@*/ /* LCL: wazzup? */
556 fn = con->optionStack->argv[0];
557 /*@=nullderef =type@*/
559 if (fn == NULL) return len;
560 if (strchr(fn, '/')) fn = strrchr(fn, '/') + 1;
561 fprintf(fp, " %s", fn);
562 len += strlen(fn) + 1;
568 void poptPrintHelp(poptContext con, FILE * fp, /*@unused@*/ UNUSED(int flags))
572 (void) showHelpIntro(con, fp);
574 fprintf(fp, " %s\n", con->otherHelp);
576 fprintf(fp, " %s\n", POPT_("[OPTION...]"));
578 leftColWidth = maxArgWidth(con->options, NULL);
579 singleTableHelp(con, fp, con->options, leftColWidth, NULL);
583 * Display usage text for an option.
584 * @param fp output file handle
585 * @param cursor current display position
586 * @param opt option(s)
587 * @param translation_domain translation domain
589 static size_t singleOptionUsage(FILE * fp, size_t cursor,
590 const struct poptOption * opt,
591 /*@null@*/ const char *translation_domain)
592 /*@globals fileSystem @*/
593 /*@modifies *fp, fileSystem @*/
596 char shortStr[2] = { '\0', '\0' };
597 const char * item = shortStr;
598 const char * argDescrip = getArgDescrip(opt, translation_domain);
600 if (opt->shortName != '\0' && opt->longName != NULL) {
602 if (!(opt->argInfo & POPT_ARGFLAG_ONEDASH)) len++;
603 len += strlen(opt->longName);
604 } else if (opt->shortName != '\0') {
606 shortStr[0] = opt->shortName;
608 } else if (opt->longName) {
609 len += strlen(opt->longName);
610 if (!(opt->argInfo & POPT_ARGFLAG_ONEDASH)) len++;
611 item = opt->longName;
614 if (len == 4) return cursor;
616 #ifdef POPT_WCHAR_HACK
617 /* XXX Calculate no. of display characters. */
619 const char * scopy = argDescrip;
624 memset ((void *)&t, '\0', sizeof (t)); /* In initial state. */
626 /* Determine number of characters. */
627 n = mbsrtowcs (NULL, &scopy, strlen(scopy), &t);
628 len += sizeof("=")-1 + n;
632 len += sizeof("=")-1 + strlen(argDescrip);
635 if ((cursor + len) > 79) {
640 if (opt->longName && opt->shortName) {
641 fprintf(fp, " [-%c|-%s%s%s%s]",
642 opt->shortName, ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "" : "-"),
644 (argDescrip ? " " : ""),
645 (argDescrip ? argDescrip : ""));
647 fprintf(fp, " [-%s%s%s%s]",
648 ((opt->shortName || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) ? "" : "-"),
650 (argDescrip ? (opt->shortName != '\0' ? " " : "=") : ""),
651 (argDescrip ? argDescrip : ""));
654 return cursor + len + 1;
658 * Display popt alias and exec usage.
659 * @param fp output file handle
660 * @param cursor current display position
661 * @param item alias/exec array
662 * @param nitems no. of ara/exec entries
663 * @param translation_domain translation domain
665 static size_t itemUsage(FILE * fp, size_t cursor,
666 /*@null@*/ poptItem item, int nitems,
667 /*@null@*/ UNUSED(const char * translation_domain))
668 /*@globals fileSystem @*/
669 /*@modifies *fp, fileSystem @*/
673 /*@-branchstate@*/ /* FIX: W2DO? */
675 for (i = 0; i < nitems; i++, item++) {
676 const struct poptOption * opt;
678 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN) {
679 translation_domain = (const char *)opt->arg;
680 } else if ((opt->longName || opt->shortName) &&
681 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
682 cursor = singleOptionUsage(fp, cursor, opt, translation_domain);
691 * Keep track of option tables already processed.
693 typedef struct poptDone_s {
700 * Display usage text for a table of options.
702 * @param fp output file handle
703 * @param cursor current display position
704 * @param opt option(s)
705 * @param translation_domain translation domain
706 * @param done tables already processed
709 static size_t singleTableUsage(poptContext con, FILE * fp, size_t cursor,
710 /*@null@*/ const struct poptOption * opt,
711 /*@null@*/ UNUSED(const char * translation_domain),
712 /*@null@*/ poptDone done)
713 /*@globals fileSystem @*/
714 /*@modifies *fp, done, fileSystem @*/
716 /*@-branchstate@*/ /* FIX: W2DO? */
718 for (; (opt->longName || opt->shortName || opt->arg) ; opt++) {
719 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN) {
720 translation_domain = (const char *)opt->arg;
721 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
724 for (i = 0; i < done->nopts; i++) {
726 const void * that = done->opts[i];
728 if (that == NULL || that != opt->arg)
729 /*@innercontinue@*/ continue;
730 /*@innerbreak@*/ break;
732 /* Skip if this table has already been processed. */
733 if (opt->arg == NULL || i < done->nopts)
736 if (done->nopts < done->maxopts)
737 done->opts[done->nopts++] = (const void *) opt->arg;
740 cursor = singleTableUsage(con, fp, cursor, opt->arg,
741 translation_domain, done);
742 } else if ((opt->longName || opt->shortName) &&
743 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
744 cursor = singleOptionUsage(fp, cursor, opt, translation_domain);
753 * Return concatenated short options for display.
754 * @todo Sub-tables should be recursed.
755 * @param opt option(s)
756 * @param fp output file handle
757 * @retval str concatenation of short options
758 * @return length of display string
760 static int showShortOptions(const struct poptOption * opt, FILE * fp,
761 /*@null@*/ char * str)
762 /*@globals fileSystem @*/
763 /*@modifies *str, *fp, fileSystem @*/
764 /*@requires maxRead(str) >= 0 @*/
766 /* bufsize larger then the ascii set, lazy alloca on top level call. */
767 char * s = (str != NULL ? str : memset(alloca(300), 0, 300));
775 for (; (opt->longName || opt->shortName || opt->arg); opt++) {
776 if (opt->shortName && !(opt->argInfo & POPT_ARG_MASK))
777 s[strlen(s)] = opt->shortName;
778 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE)
779 if (opt->arg) /* XXX program error */
780 len = showShortOptions(opt->arg, fp, s);
784 /* On return to top level, print the short options, return print length. */
785 if (s == str && *s != '\0') {
786 fprintf(fp, " [-%s]", s);
787 len = strlen(s) + sizeof(" [-]")-1;
792 void poptPrintUsage(poptContext con, FILE * fp, /*@unused@*/ UNUSED(int flags))
794 poptDone done = memset(alloca(sizeof(*done)), 0, sizeof(*done));
799 cursor = done->maxopts * sizeof(*done->opts);
801 done->opts = memset(alloca(cursor), 0, cursor);
803 done->opts[done->nopts++] = (const void *) con->options;
807 cursor = showHelpIntro(con, fp);
808 cursor += showShortOptions(con->options, fp, NULL);
809 cursor = singleTableUsage(con, fp, cursor, con->options, NULL, done);
810 cursor = itemUsage(fp, cursor, con->aliases, con->numAliases, NULL);
811 cursor = itemUsage(fp, cursor, con->execs, con->numExecs, NULL);
813 if (con->otherHelp) {
814 cursor += strlen(con->otherHelp) + 1;
815 if (cursor > 79) fprintf(fp, "\n ");
816 fprintf(fp, " %s", con->otherHelp);
822 void poptSetOtherOptionHelp(poptContext con, const char * text)
824 con->otherHelp = _free(con->otherHelp);
825 con->otherHelp = xstrdup(text);