popt  1.16
popthelp.c
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 
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. */
10 
11 #include "system.h"
12 
13 #define POPT_USE_TIOCGWINSZ
14 #ifdef POPT_USE_TIOCGWINSZ
15 #include <sys/ioctl.h>
16 #endif
17 
18 #define POPT_WCHAR_HACK
19 #ifdef POPT_WCHAR_HACK
20 #include <wchar.h> /* for mbsrtowcs */
21 /*@access mbstate_t @*/
22 #endif
23 #include "poptint.h"
24 
25 /*@access poptContext@*/
26 
35 /*@exits@*/
36 static void displayArgs(poptContext con,
37  /*@unused@*/ UNUSED(enum poptCallbackReason foo),
38  struct poptOption * key,
39  /*@unused@*/ UNUSED(const char * arg),
40  /*@unused@*/ UNUSED(void * data))
41  /*@globals fileSystem@*/
42  /*@modifies fileSystem@*/
43 {
44  if (key->shortName == '?')
45  poptPrintHelp(con, stdout, 0);
46  else
47  poptPrintUsage(con, stdout, 0);
48 
49 #if !defined(__LCLINT__) /* XXX keep both splint & valgrind happy */
50  con = poptFreeContext(con);
51 #endif
52  exit(0);
53 }
54 
55 #ifdef NOTYET
56 /*@unchecked@*/
57 static int show_option_defaults = 0;
58 #endif
59 
63 /*@observer@*/ /*@unchecked@*/
66 };
67 
71 /*@-castfcnptr@*/
72 /*@observer@*/ /*@unchecked@*/
74  { NULL, '\0', POPT_ARG_CALLBACK, (void *)displayArgs, 0, NULL, NULL },
75  { "help", '?', 0, NULL, (int)'?', N_("Show this help message"), NULL },
76  { "usage", '\0', 0, NULL, (int)'u', N_("Display brief usage message"), NULL },
78 } ;
79 
80 /*@observer@*/ /*@unchecked@*/
81 static struct poptOption poptHelpOptions2[] = {
82 /*@-readonlytrans@*/
83  { NULL, '\0', POPT_ARG_INTL_DOMAIN, PACKAGE, 0, NULL, NULL},
84 /*@=readonlytrans@*/
85  { NULL, '\0', POPT_ARG_CALLBACK, (void *)displayArgs, 0, NULL, NULL },
86  { "help", '?', 0, NULL, (int)'?', N_("Show this help message"), NULL },
87  { "usage", '\0', 0, NULL, (int)'u', N_("Display brief usage message"), NULL },
88 #ifdef NOTYET
89  { "defaults", '\0', POPT_ARG_NONE, &show_option_defaults, 0,
90  N_("Display option defaults in message"), NULL },
91 #endif
92  { "", '\0', 0, NULL, 0, N_("Terminate options"), NULL },
94 } ;
95 
96 /*@observer@*/ /*@unchecked@*/
98 /*@=castfcnptr@*/
99 
100 #define _POPTHELP_MAXLINE ((size_t)79)
101 
102 typedef struct columns_s {
103  size_t cur;
104  size_t max;
105 } * columns_t;
106 
112 static size_t maxColumnWidth(FILE *fp)
113  /*@*/
114 {
115  size_t maxcols = _POPTHELP_MAXLINE;
116 #if defined(TIOCGWINSZ)
117  struct winsize ws;
118  int fdno = fileno(fp ? fp : stdout);
119 
120  memset(&ws, 0, sizeof(ws));
121  if (fdno >= 0 && !ioctl(fdno, (unsigned long)TIOCGWINSZ, &ws)) {
122  size_t ws_col = (size_t)ws.ws_col;
123  if (ws_col > maxcols && ws_col < (size_t)256)
124  maxcols = ws_col - 1;
125  }
126 #endif
127  return maxcols;
128 }
129 
135 static inline size_t stringDisplayWidth(const char *s)
136  /*@*/
137 {
138  size_t n = strlen(s);
139 #ifdef POPT_WCHAR_HACK
140  mbstate_t t;
141 
142  memset ((void *)&t, 0, sizeof (t)); /* In initial state. */
143  /* Determine number of display characters. */
144  n = mbsrtowcs (NULL, &s, n, &t);
145 #else
146  n = 0;
147  for (; *s; s = POPT_next_char(s))
148  n++;
149 #endif
150 
151  return n;
152 }
153 
157 /*@observer@*/ /*@null@*/ static const char *
158 getTableTranslationDomain(/*@null@*/ const struct poptOption *opt)
159  /*@*/
160 {
161  if (opt != NULL)
162  for (; opt->longName || opt->shortName || opt->arg; opt++) {
163  if (opt->argInfo == POPT_ARG_INTL_DOMAIN)
164  return opt->arg;
165  }
166  return NULL;
167 }
168 
173 /*@observer@*/ /*@null@*/ static const char *
174 getArgDescrip(const struct poptOption * opt,
175  /*@-paramuse@*/ /* FIX: i18n macros disabled with lclint */
176  /*@null@*/ const char * translation_domain)
177  /*@=paramuse@*/
178  /*@*/
179 {
180  if (!poptArgType(opt)) return NULL;
181 
182  if (poptArgType(opt) == POPT_ARG_MAINCALL)
183  return opt->argDescrip;
184  if (poptArgType(opt) == POPT_ARG_ARGV)
185  return opt->argDescrip;
186 
187  if (opt->argDescrip) {
188  /* Some strings need popt library, not application, i18n domain. */
189  if (opt == (poptHelpOptions + 1)
190  || opt == (poptHelpOptions + 2)
191  || !strcmp(opt->argDescrip, N_("Help options:"))
192  || !strcmp(opt->argDescrip, N_("Options implemented via popt alias/exec:")))
193  return POPT_(opt->argDescrip);
194 
195  /* Use the application i18n domain. */
196  return D_(translation_domain, opt->argDescrip);
197  }
198 
199  switch (poptArgType(opt)) {
200  case POPT_ARG_NONE: return POPT_("NONE");
201 #ifdef DYING
202  case POPT_ARG_VAL: return POPT_("VAL");
203 #else
204  case POPT_ARG_VAL: return NULL;
205 #endif
206  case POPT_ARG_INT: return POPT_("INT");
207  case POPT_ARG_SHORT: return POPT_("SHORT");
208  case POPT_ARG_LONG: return POPT_("LONG");
209  case POPT_ARG_LONGLONG: return POPT_("LONGLONG");
210  case POPT_ARG_STRING: return POPT_("STRING");
211  case POPT_ARG_FLOAT: return POPT_("FLOAT");
212  case POPT_ARG_DOUBLE: return POPT_("DOUBLE");
213  case POPT_ARG_MAINCALL: return NULL;
214  case POPT_ARG_ARGV: return NULL;
215  default: return POPT_("ARG");
216  }
217 }
218 
226 static /*@only@*/ /*@null@*/ char *
227 singleOptionDefaultValue(size_t lineLength,
228  const struct poptOption * opt,
229  /*@-paramuse@*/ /* FIX: i18n macros disabled with lclint */
230  /*@null@*/ const char * translation_domain)
231  /*@=paramuse@*/
232  /*@*/
233 {
234  const char * defstr = D_(translation_domain, "default");
235  char * le = malloc(4*lineLength + 1);
236  char * l = le;
237 
238  if (le == NULL) return NULL; /* XXX can't happen */
239  *le = '\0';
240  *le++ = '(';
241  le = stpcpy(le, defstr);
242  *le++ = ':';
243  *le++ = ' ';
244  if (opt->arg) { /* XXX programmer error */
245  poptArg arg = { .ptr = opt->arg };
246  switch (poptArgType(opt)) {
247  case POPT_ARG_VAL:
248  case POPT_ARG_INT:
249  le += sprintf(le, "%d", arg.intp[0]);
250  break;
251  case POPT_ARG_SHORT:
252  le += sprintf(le, "%hd", arg.shortp[0]);
253  break;
254  case POPT_ARG_LONG:
255  le += sprintf(le, "%ld", arg.longp[0]);
256  break;
257  case POPT_ARG_LONGLONG:
258  le += sprintf(le, "%lld", arg.longlongp[0]);
259  break;
260  case POPT_ARG_FLOAT:
261  { double aDouble = (double) arg.floatp[0];
262  le += sprintf(le, "%g", aDouble);
263  } break;
264  case POPT_ARG_DOUBLE:
265  le += sprintf(le, "%g", arg.doublep[0]);
266  break;
267  case POPT_ARG_MAINCALL:
268  le += sprintf(le, "%p", opt->arg);
269  break;
270  case POPT_ARG_ARGV:
271  le += sprintf(le, "%p", opt->arg);
272  break;
273  case POPT_ARG_STRING:
274  { const char * s = arg.argv[0];
275  if (s == NULL)
276  le = stpcpy(le, "null");
277  else {
278  size_t limit = 4*lineLength - (le - l) - sizeof("\"\")");
279  size_t slen;
280  *le++ = '"';
281  strncpy(le, s, limit); le[limit] = '\0'; le += (slen = strlen(le));
282  if (slen == limit && s[limit])
283  le[-1] = le[-2] = le[-3] = '.';
284  *le++ = '"';
285  }
286  } break;
287  case POPT_ARG_NONE:
288  default:
289  l = _free(l);
290  return NULL;
291  /*@notreached@*/ break;
292  }
293  }
294  *le++ = ')';
295  *le = '\0';
296 
297  return l;
298 }
299 
307 static void singleOptionHelp(FILE * fp, columns_t columns,
308  const struct poptOption * opt,
309  /*@null@*/ const char * translation_domain)
310  /*@globals fileSystem @*/
311  /*@modifies fp, fileSystem @*/
312 {
313  size_t maxLeftCol = columns->cur;
314  size_t indentLength = maxLeftCol + 5;
315  size_t lineLength = columns->max - indentLength;
316  const char * help = D_(translation_domain, opt->descrip);
317  const char * argDescrip = getArgDescrip(opt, translation_domain);
318  /* Display shortName iff printable non-space. */
319  int prtshort = (int)(isprint((int)opt->shortName) && opt->shortName != ' ');
320  size_t helpLength;
321  char * defs = NULL;
322  char * left;
323  size_t nb = maxLeftCol + 1;
324  int displaypad = 0;
325  int xx;
326 
327  /* Make sure there's more than enough room in target buffer. */
328  if (opt->longName) nb += strlen(opt->longName);
329  if (F_ISSET(opt, TOGGLE)) nb += sizeof("[no]") - 1;
330  if (argDescrip) nb += strlen(argDescrip);
331 
332  left = malloc(nb);
333  if (left == NULL) return; /* XXX can't happen */
334  left[0] = '\0';
335  left[maxLeftCol] = '\0';
336 
337 #define prtlong (opt->longName != NULL) /* XXX splint needs a clue */
338  if (!(prtshort || prtlong))
339  goto out;
340  if (prtshort && prtlong) {
341  char *dash = F_ISSET(opt, ONEDASH) ? "-" : "--";
342  left[0] = '-';
343  left[1] = opt->shortName;
344  (void) stpcpy(stpcpy(stpcpy(left+2, ", "), dash), opt->longName);
345  } else if (prtshort) {
346  left[0] = '-';
347  left[1] = opt->shortName;
348  left[2] = '\0';
349  } else if (prtlong) {
350  /* XXX --long always padded for alignment with/without "-X, ". */
351  char *dash = poptArgType(opt) == POPT_ARG_MAINCALL ? ""
352  : (F_ISSET(opt, ONEDASH) ? "-" : "--");
353  const char *longName = opt->longName;
354  const char *toggle;
355  if (F_ISSET(opt, TOGGLE)) {
356  toggle = "[no]";
357  if (longName[0] == 'n' && longName[1] == 'o') {
358  longName += sizeof("no") - 1;
359  if (longName[0] == '-')
360  longName++;
361  }
362  } else
363  toggle = "";
364  (void) stpcpy(stpcpy(stpcpy(stpcpy(left, " "), dash), toggle), longName);
365  }
366 #undef prtlong
367 
368  if (argDescrip) {
369  char * le = left + strlen(left);
370 
371  if (F_ISSET(opt, OPTIONAL))
372  *le++ = '[';
373 
374  /* Choose type of output */
375  if (F_ISSET(opt, SHOW_DEFAULT)) {
376  defs = singleOptionDefaultValue(lineLength, opt, translation_domain);
377  if (defs) {
378  char * t = malloc((help ? strlen(help) : 0) +
379  strlen(defs) + sizeof(" "));
380  if (t) {
381  char * te = t;
382  if (help)
383  te = stpcpy(te, help);
384  *te++ = ' ';
385  strcpy(te, defs);
386  defs = _free(defs);
387  defs = t;
388  }
389  }
390  }
391 
392  if (opt->argDescrip == NULL) {
393  switch (poptArgType(opt)) {
394  case POPT_ARG_NONE:
395  break;
396  case POPT_ARG_VAL:
397 #ifdef NOTNOW /* XXX pug ugly nerdy output */
398  { long aLong = opt->val;
399  int ops = F_ISSET(opt, LOGICALOPS);
400  int negate = F_ISSET(opt, NOT);
401 
402  /* Don't bother displaying typical values */
403  if (!ops && (aLong == 0L || aLong == 1L || aLong == -1L))
404  break;
405  *le++ = '[';
406  switch (ops) {
407  case POPT_ARGFLAG_OR:
408  *le++ = '|';
409  /*@innerbreak@*/ break;
410  case POPT_ARGFLAG_AND:
411  *le++ = '&';
412  /*@innerbreak@*/ break;
413  case POPT_ARGFLAG_XOR:
414  *le++ = '^';
415  /*@innerbreak@*/ break;
416  default:
417  /*@innerbreak@*/ break;
418  }
419  *le++ = (opt->longName != NULL ? '=' : ' ');
420  if (negate) *le++ = '~';
421  /*@-formatconst@*/
422  le += sprintf(le, (ops ? "0x%lx" : "%ld"), aLong);
423  /*@=formatconst@*/
424  *le++ = ']';
425  }
426 #endif
427  break;
428  case POPT_ARG_INT:
429  case POPT_ARG_SHORT:
430  case POPT_ARG_LONG:
431  case POPT_ARG_LONGLONG:
432  case POPT_ARG_FLOAT:
433  case POPT_ARG_DOUBLE:
434  case POPT_ARG_STRING:
435  *le++ = (opt->longName != NULL ? '=' : ' ');
436  le = stpcpy(le, argDescrip);
437  break;
438  default:
439  break;
440  }
441  } else {
442  char *leo;
443 
444  /* XXX argDescrip[0] determines "--foo=bar" or "--foo bar". */
445  if (!strchr(" =(", argDescrip[0]))
446  *le++ = ((poptArgType(opt) == POPT_ARG_MAINCALL) ? ' ' :
447  (poptArgType(opt) == POPT_ARG_ARGV) ? ' ' : '=');
448  le = stpcpy(leo = le, argDescrip);
449 
450  /* Adjust for (possible) wide characters. */
451  displaypad = (int)((le - leo) - stringDisplayWidth(argDescrip));
452  }
453  if (F_ISSET(opt, OPTIONAL))
454  *le++ = ']';
455  *le = '\0';
456  }
457 
458  if (help)
459  xx = POPT_fprintf(fp," %-*s ", (int)(maxLeftCol+displaypad), left);
460  else {
461  xx = POPT_fprintf(fp," %s\n", left);
462  goto out;
463  }
464 
465  left = _free(left);
466  if (defs)
467  help = defs;
468 
469  helpLength = strlen(help);
470  while (helpLength > lineLength) {
471  const char * ch;
472  char format[16];
473 
474  ch = help + lineLength - 1;
475  while (ch > help && !_isspaceptr(ch))
476  ch = POPT_prev_char(ch);
477  if (ch == help) break; /* give up */
478  while (ch > (help + 1) && _isspaceptr(ch))
479  ch = POPT_prev_char (ch);
480  ch = POPT_next_char(ch);
481 
482  /*
483  * XXX strdup is necessary to add NUL terminator so that an unknown
484  * no. of (possible) multi-byte characters can be displayed.
485  */
486  { char * fmthelp = xstrdup(help);
487  if (fmthelp) {
488  fmthelp[ch - help] = '\0';
489  sprintf(format, "%%s\n%%%ds", (int) indentLength);
490  /*@-formatconst@*/
491  xx = POPT_fprintf(fp, format, fmthelp, " ");
492  /*@=formatconst@*/
493  free(fmthelp);
494  }
495  }
496 
497  help = ch;
498  while (_isspaceptr(help) && *help)
499  help = POPT_next_char(help);
500  helpLength = strlen(help);
501  }
502 
503  if (helpLength) fprintf(fp, "%s\n", help);
504  help = NULL;
505 
506 out:
507  /*@-dependenttrans@*/
508  defs = _free(defs);
509  /*@=dependenttrans@*/
510  left = _free(left);
511 }
512 
519 static size_t maxArgWidth(const struct poptOption * opt,
520  /*@null@*/ const char * translation_domain)
521  /*@*/
522 {
523  size_t max = 0;
524  size_t len = 0;
525  const char * argDescrip;
526 
527  if (opt != NULL)
528  while (opt->longName || opt->shortName || opt->arg) {
529  if (poptArgType(opt) == POPT_ARG_INCLUDE_TABLE) {
530  if (opt->arg) /* XXX program error */
531  len = maxArgWidth(opt->arg, translation_domain);
532  if (len > max) max = len;
533  } else if (!F_ISSET(opt, DOC_HIDDEN)) {
534  len = sizeof(" ")-1;
535  /* XXX --long always padded for alignment with/without "-X, ". */
536  len += sizeof("-X, ")-1;
537  if (opt->longName) {
538  len += (F_ISSET(opt, ONEDASH) ? sizeof("-") : sizeof("--")) - 1;
539  len += strlen(opt->longName);
540  }
541 
542  argDescrip = getArgDescrip(opt, translation_domain);
543 
544  if (argDescrip) {
545 
546  /* XXX argDescrip[0] determines "--foo=bar" or "--foo bar". */
547  if (!strchr(" =(", argDescrip[0])) len += sizeof("=")-1;
548 
549  /* Adjust for (possible) wide characters. */
550  len += stringDisplayWidth(argDescrip);
551  }
552 
553  if (F_ISSET(opt, OPTIONAL)) len += sizeof("[]")-1;
554  if (len > max) max = len;
555  }
556  opt++;
557  }
558 
559  return max;
560 }
561 
570 static void itemHelp(FILE * fp,
571  /*@null@*/ poptItem items, int nitems,
572  columns_t columns,
573  /*@null@*/ const char * translation_domain)
574  /*@globals fileSystem @*/
575  /*@modifies fp, fileSystem @*/
576 {
577  poptItem item;
578  int i;
579 
580  if (items != NULL)
581  for (i = 0, item = items; i < nitems; i++, item++) {
582  const struct poptOption * opt;
583  opt = &item->option;
584  if ((opt->longName || opt->shortName) && !F_ISSET(opt, DOC_HIDDEN))
585  singleOptionHelp(fp, columns, opt, translation_domain);
586  }
587 }
588 
597 static void singleTableHelp(poptContext con, FILE * fp,
598  /*@null@*/ const struct poptOption * table,
599  columns_t columns,
600  /*@null@*/ const char * translation_domain)
601  /*@globals fileSystem @*/
602  /*@modifies fp, columns->cur, fileSystem @*/
603 {
604  const struct poptOption * opt;
605  const char *sub_transdom;
606  int xx;
607 
608  if (con == NULL) return;
609 
610  if (table == poptAliasOptions) {
611  itemHelp(fp, con->aliases, con->numAliases, columns, NULL);
612  itemHelp(fp, con->execs, con->numExecs, columns, NULL);
613  return;
614  }
615 
616  if (table != NULL)
617  for (opt = table; opt->longName || opt->shortName || opt->arg; opt++) {
618  if ((opt->longName || opt->shortName) && !F_ISSET(opt, DOC_HIDDEN))
619  singleOptionHelp(fp, columns, opt, translation_domain);
620  }
621 
622  if (table != NULL)
623  for (opt = table; opt->longName || opt->shortName || opt->arg; opt++) {
625  continue;
626  sub_transdom = getTableTranslationDomain(opt->arg);
627  if (sub_transdom == NULL)
628  sub_transdom = translation_domain;
629 
630  /* If no popt aliases/execs, skip poptAliasOption processing. */
631  if (opt->arg == poptAliasOptions && !(con->numAliases || con->numExecs))
632  continue;
633  if (opt->descrip)
634  xx = POPT_fprintf(fp, "\n%s\n", D_(sub_transdom, opt->descrip));
635 
636  singleTableHelp(con, fp, opt->arg, columns, sub_transdom);
637  }
638 }
639 
644 static size_t showHelpIntro(poptContext con, FILE * fp)
645  /*@globals fileSystem @*/
646  /*@modifies fp, fileSystem @*/
647 {
648  size_t len = (size_t)6;
649  int xx;
650 
651  xx = POPT_fprintf(fp, POPT_("Usage:"));
652  if (!(con->flags & POPT_CONTEXT_KEEP_FIRST)) {
653  struct optionStackEntry * os = con->optionStack;
654  const char * fn = (os->argv ? os->argv[0] : NULL);
655  if (fn == NULL) return len;
656  if (strchr(fn, '/')) fn = strrchr(fn, '/') + 1;
657  /* XXX POPT_fprintf not needed for argv[0] display. */
658  fprintf(fp, " %s", fn);
659  len += strlen(fn) + 1;
660  }
661 
662  return len;
663 }
664 
665 void poptPrintHelp(poptContext con, FILE * fp, /*@unused@*/ UNUSED(int flags))
666 {
667  columns_t columns = calloc((size_t)1, sizeof(*columns));
668  int xx;
669 
670  if (con == NULL) return;
671 
672  (void) showHelpIntro(con, fp);
673  if (con->otherHelp)
674  xx = POPT_fprintf(fp, " %s\n", con->otherHelp);
675  else
676  xx = POPT_fprintf(fp, " %s\n", POPT_("[OPTION...]"));
677 
678  if (columns) {
679  columns->cur = maxArgWidth(con->options, NULL);
680  columns->max = maxColumnWidth(fp);
681  singleTableHelp(con, fp, con->options, columns, NULL);
682  free(columns);
683  }
684 }
685 
693 static size_t singleOptionUsage(FILE * fp, columns_t columns,
694  const struct poptOption * opt,
695  /*@null@*/ const char *translation_domain)
696  /*@globals fileSystem @*/
697  /*@modifies fp, columns->cur, fileSystem @*/
698 {
699  size_t len = sizeof(" []")-1;
700  const char * argDescrip = getArgDescrip(opt, translation_domain);
701  /* Display shortName iff printable non-space. */
702  int prtshort = (int)(isprint((int)opt->shortName) && opt->shortName != ' ');
703 
704 #define prtlong (opt->longName != NULL) /* XXX splint needs a clue */
705  if (!(prtshort || prtlong))
706  return columns->cur;
707 
708  len = sizeof(" []")-1;
709  if (prtshort)
710  len += sizeof("-c")-1;
711  if (prtlong) {
712  if (prtshort) len += sizeof("|")-1;
713  len += (F_ISSET(opt, ONEDASH) ? sizeof("-") : sizeof("--")) - 1;
714  len += strlen(opt->longName);
715  }
716 
717  if (argDescrip) {
718 
719  /* XXX argDescrip[0] determines "--foo=bar" or "--foo bar". */
720  if (!strchr(" =(", argDescrip[0])) len += sizeof("=")-1;
721 
722  /* Adjust for (possible) wide characters. */
723  len += stringDisplayWidth(argDescrip);
724  }
725 
726  if ((columns->cur + len) > columns->max) {
727  fprintf(fp, "\n ");
728  columns->cur = (size_t)7;
729  }
730 
731  fprintf(fp, " [");
732  if (prtshort)
733  fprintf(fp, "-%c", opt->shortName);
734  if (prtlong)
735  fprintf(fp, "%s%s%s",
736  (prtshort ? "|" : ""),
737  (F_ISSET(opt, ONEDASH) ? "-" : "--"),
738  opt->longName);
739 #undef prtlong
740 
741  if (argDescrip) {
742  /* XXX argDescrip[0] determines "--foo=bar" or "--foo bar". */
743  if (!strchr(" =(", argDescrip[0])) fprintf(fp, "=");
744  fprintf(fp, "%s", argDescrip);
745  }
746  fprintf(fp, "]");
747 
748  return columns->cur + len + 1;
749 }
750 
759 static size_t itemUsage(FILE * fp, columns_t columns,
760  /*@null@*/ poptItem item, int nitems,
761  /*@null@*/ const char * translation_domain)
762  /*@globals fileSystem @*/
763  /*@modifies fp, columns->cur, fileSystem @*/
764 {
765  int i;
766 
767  if (item != NULL)
768  for (i = 0; i < nitems; i++, item++) {
769  const struct poptOption * opt;
770  opt = &item->option;
771  if (poptArgType(opt) == POPT_ARG_INTL_DOMAIN) {
772  translation_domain = (const char *)opt->arg;
773  } else
774  if ((opt->longName || opt->shortName) && !F_ISSET(opt, DOC_HIDDEN)) {
775  columns->cur = singleOptionUsage(fp, columns, opt, translation_domain);
776  }
777  }
778 
779  return columns->cur;
780 }
781 
785 typedef struct poptDone_s {
786  int nopts;
787  int maxopts;
788 /*@null@*/
789  const void ** opts;
790 } * poptDone;
791 
802 static size_t singleTableUsage(poptContext con, FILE * fp, columns_t columns,
803  /*@null@*/ const struct poptOption * opt,
804  /*@null@*/ const char * translation_domain,
805  /*@null@*/ poptDone done)
806  /*@globals fileSystem @*/
807  /*@modifies fp, columns->cur, done, fileSystem @*/
808 {
809  if (con != NULL && opt != NULL)
810  for (; (opt->longName || opt->shortName || opt->arg) ; opt++) {
811  if (poptArgType(opt) == POPT_ARG_INTL_DOMAIN) {
812  translation_domain = (const char *)opt->arg;
813  } else
814  if (poptArgType(opt) == POPT_ARG_INCLUDE_TABLE) {
815  if (done) {
816  int i = 0;
817  if (done->opts != NULL)
818  for (i = 0; i < done->nopts; i++) {
819  const void * that = done->opts[i];
820  if (that == NULL || that != opt->arg)
821  /*@innercontinue@*/ continue;
822  /*@innerbreak@*/ break;
823  }
824  /* Skip if this table has already been processed. */
825  if (opt->arg == NULL || i < done->nopts)
826  continue;
827  if (done->opts != NULL && done->nopts < done->maxopts)
828  done->opts[done->nopts++] = (const void *) opt->arg;
829  }
830  columns->cur = singleTableUsage(con, fp, columns, opt->arg,
831  translation_domain, done);
832  } else
833  if ((opt->longName || opt->shortName) && !F_ISSET(opt, DOC_HIDDEN)) {
834  columns->cur = singleOptionUsage(fp, columns, opt, translation_domain);
835  }
836  }
837 
838  return columns->cur;
839 }
840 
849 static size_t showShortOptions(const struct poptOption * opt, FILE * fp,
850  /*@null@*/ char * str)
851  /*@globals fileSystem @*/
852  /*@modifies str, *fp, fileSystem @*/
853  /*@requires maxRead(str) >= 0 @*/
854 {
855  /* bufsize larger then the ascii set, lazy allocation on top level call. */
856  size_t nb = (size_t)300;
857  char * s = (str != NULL ? str : calloc((size_t)1, nb));
858  size_t len = (size_t)0;
859 
860  if (s == NULL)
861  return 0;
862 
863  if (opt != NULL)
864  for (; (opt->longName || opt->shortName || opt->arg); opt++) {
865  if (!F_ISSET(opt, DOC_HIDDEN) && opt->shortName && !poptArgType(opt))
866  {
867  /* Display shortName iff unique printable non-space. */
868  if (!strchr(s, opt->shortName) && isprint((int)opt->shortName)
869  && opt->shortName != ' ')
870  s[strlen(s)] = opt->shortName;
871  } else if (poptArgType(opt) == POPT_ARG_INCLUDE_TABLE)
872  if (opt->arg) /* XXX program error */
873  len = showShortOptions(opt->arg, fp, s);
874  }
875 
876  /* On return to top level, print the short options, return print length. */
877  if (s != str && *s != '\0') {
878  fprintf(fp, " [-%s]", s);
879  len = strlen(s) + sizeof(" [-]")-1;
880  }
881 /*@-temptrans@*/ /* LCL: local s, not str arg, is being freed. */
882  if (s != str)
883  free(s);
884 /*@=temptrans@*/
885  return len;
886 }
887 
888 void poptPrintUsage(poptContext con, FILE * fp, /*@unused@*/ UNUSED(int flags))
889 {
890  columns_t columns = calloc((size_t)1, sizeof(*columns));
891  struct poptDone_s done_buf;
892  poptDone done = &done_buf;
893 
894  if (con == NULL) return;
895 
896  memset(done, 0, sizeof(*done));
897  done->nopts = 0;
898  done->maxopts = 64;
899  if (columns) {
900  columns->cur = done->maxopts * sizeof(*done->opts);
901  columns->max = maxColumnWidth(fp);
902  done->opts = calloc((size_t)1, columns->cur);
903  /*@-keeptrans@*/
904  if (done->opts != NULL)
905  done->opts[done->nopts++] = (const void *) con->options;
906  /*@=keeptrans@*/
907 
908  columns->cur = showHelpIntro(con, fp);
909  columns->cur += showShortOptions(con->options, fp, NULL);
910  columns->cur = singleTableUsage(con, fp, columns, con->options, NULL, done);
911  columns->cur = itemUsage(fp, columns, con->aliases, con->numAliases, NULL);
912  columns->cur = itemUsage(fp, columns, con->execs, con->numExecs, NULL);
913 
914  if (con->otherHelp) {
915  columns->cur += strlen(con->otherHelp) + 1;
916  if (columns->cur > columns->max) fprintf(fp, "\n ");
917  fprintf(fp, " %s", con->otherHelp);
918  }
919 
920  fprintf(fp, "\n");
921  if (done->opts != NULL)
922  free(done->opts);
923  free(columns);
924  }
925 }
926 
927 void poptSetOtherOptionHelp(poptContext con, const char * text)
928 {
929  if (con == NULL) return;
930 
931  con->otherHelp = _free(con->otherHelp);
932  con->otherHelp = xstrdup(text);
933 }
poptArg_u::intp
int * intp
Definition: poptint.h:66
POPT_ARG_LONG
#define POPT_ARG_LONG
Definition: popt.h:23
poptCallbackReason
poptCallbackReason
Definition: popt.h:216
showShortOptions
static size_t showShortOptions(const struct poptOption *opt, FILE *fp, char *str)
Return concatenated short options for display.
Definition: popthelp.c:849
poptArg_u::shortp
short * shortp
Definition: poptint.h:67
poptArg_u::doublep
double * doublep
Definition: poptint.h:71
maxArgWidth
static size_t maxArgWidth(const struct poptOption *opt, const char *translation_domain)
Find display width for longest argument string.
Definition: popthelp.c:519
singleOptionDefaultValue
static char * singleOptionDefaultValue(size_t lineLength, const struct poptOption *opt, const char *translation_domain)
Display default value for an option.
Definition: popthelp.c:227
showHelpIntro
static size_t showHelpIntro(poptContext con, FILE *fp)
Definition: popthelp.c:644
POPT_ARG_INCLUDE_TABLE
#define POPT_ARG_INCLUDE_TABLE
Definition: popt.h:24
stpcpy
static char * stpcpy(char *dest, const char *src)
Definition: system.h:69
POPT_ARG_FLOAT
#define POPT_ARG_FLOAT
Definition: popt.h:34
POPT_ARGFLAG_XOR
#define POPT_ARGFLAG_XOR
Definition: popt.h:61
N_
#define N_(foo)
Definition: poptint.h:221
poptOption::argDescrip
const char * argDescrip
Definition: popt.h:138
_free
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: poptint.h:20
columns_s::cur
size_t cur
Definition: popthelp.c:103
singleOptionUsage
static size_t singleOptionUsage(FILE *fp, columns_t columns, const struct poptOption *opt, const char *translation_domain)
Display usage text for an option.
Definition: popthelp.c:693
POPT_ARG_MAINCALL
#define POPT_ARG_MAINCALL
Definition: popt.h:38
poptFreeContext
poptContext poptFreeContext(poptContext con)
Definition: popt.c:1624
POPT_ARG_VAL
#define POPT_ARG_VAL
Definition: popt.h:33
POPT_TABLEEND
#define POPT_TABLEEND
Definition: popt.h:196
poptContext_s
Definition: poptint.h:115
poptArg_u::ptr
void * ptr
Definition: poptint.h:65
poptArgType
#define poptArgType(_opt)
Definition: poptint.h:86
poptDone_s::opts
const void ** opts
Definition: popthelp.c:789
poptContext_s::flags
unsigned int flags
Definition: poptint.h:131
POPT_next_char
const char * POPT_next_char(const char *str)
Definition: poptint.c:37
poptHelpOptionsI18N
struct poptOption * poptHelpOptionsI18N
Definition: popthelp.c:97
stringDisplayWidth
static size_t stringDisplayWidth(const char *s)
Determine number of display characters in a string.
Definition: popthelp.c:135
itemHelp
static void itemHelp(FILE *fp, poptItem items, int nitems, columns_t columns, const char *translation_domain)
Display popt alias and exec help.
Definition: popthelp.c:570
poptOption::argInfo
unsigned int argInfo
Definition: popt.h:131
poptSetOtherOptionHelp
void poptSetOtherOptionHelp(poptContext con, const char *text)
Definition: popthelp.c:927
D_
#define D_(dom, str)
Definition: poptint.h:217
poptContext_s::aliases
poptItem aliases
Definition: poptint.h:129
poptArg_u::floatp
float * floatp
Definition: poptint.h:70
poptArg_u::argv
const char ** argv
Definition: poptint.h:72
poptOption::longName
const char * longName
Definition: popt.h:129
getArgDescrip
static const char * getArgDescrip(const struct poptOption *opt, const char *translation_domain)
Definition: popthelp.c:174
poptint.h
poptContext_s::numExecs
int numExecs
Definition: poptint.h:134
prtlong
#define prtlong
columns_s
Definition: popthelp.c:102
poptDone
struct poptDone_s * poptDone
Keep track of option tables already processed.
poptHelpOptions2
static struct poptOption poptHelpOptions2[]
Definition: popthelp.c:81
POPT_ARG_INTL_DOMAIN
#define POPT_ARG_INTL_DOMAIN
Definition: popt.h:29
POPT_ARG_CALLBACK
#define POPT_ARG_CALLBACK
Definition: popt.h:25
poptArg_u::longlongp
long long * longlongp
Definition: poptint.h:69
poptOption::arg
void * arg
Definition: popt.h:133
F_ISSET
#define F_ISSET(_opt, _FLAG)
Definition: poptint.h:89
POPT_ARG_LONGLONG
#define POPT_ARG_LONGLONG
Definition: popt.h:36
POPT_ARGFLAG_OR
#define POPT_ARGFLAG_OR
Definition: popt.h:57
poptAliasOptions
struct poptOption poptAliasOptions[]
Empty table marker to enable displaying popt alias/exec options.
Definition: popthelp.c:64
columns_s::max
size_t max
Definition: popthelp.c:104
POPT_fprintf
int POPT_fprintf(FILE *stream, const char *format,...)
Definition: poptint.c:150
UNUSED
#define UNUSED(x)
Definition: system.h:105
POPT_ARG_DOUBLE
#define POPT_ARG_DOUBLE
Definition: popt.h:35
poptContext_s::numAliases
int numAliases
Definition: poptint.h:130
POPT_ARG_STRING
#define POPT_ARG_STRING
Definition: popt.h:21
poptOption::shortName
char shortName
Definition: popt.h:130
poptArg_u
Definition: poptint.h:63
system.h
singleOptionHelp
static void singleOptionHelp(FILE *fp, columns_t columns, const struct poptOption *opt, const char *translation_domain)
Display help text for an option.
Definition: popthelp.c:307
optionStackEntry::argv
poptArgv argv
Definition: poptint.h:102
POPT_ARG_SHORT
#define POPT_ARG_SHORT
Definition: popt.h:40
POPT_CONTEXT_KEEP_FIRST
#define POPT_CONTEXT_KEEP_FIRST
Definition: popt.h:120
columns_t
struct columns_s * columns_t
poptOption::val
int val
Definition: popt.h:134
poptContext_s::optionStack
struct optionStackEntry optionStack[POPT_OPTION_DEPTH]
Definition: poptint.h:116
poptDone_s::nopts
int nopts
Definition: popthelp.c:786
POPT_ARGFLAG_AND
#define POPT_ARGFLAG_AND
Definition: popt.h:59
poptPrintHelp
void poptPrintHelp(poptContext con, FILE *fp, int flags)
Definition: popthelp.c:665
poptItem_s
Definition: popt.h:157
POPT_ARG_NONE
#define POPT_ARG_NONE
Definition: popt.h:20
poptOption::descrip
const char * descrip
Definition: popt.h:136
poptHelpOptions
struct poptOption poptHelpOptions[]
Auto help table options.
Definition: popthelp.c:73
POPT_prev_char
const char * POPT_prev_char(const char *str)
Definition: poptint.c:25
poptContext_s::execs
poptItem execs
Definition: poptint.h:133
_isspaceptr
#define _isspaceptr(_chp)
Definition: system.h:21
poptContext_s::options
const struct poptOption * options
Definition: poptint.h:124
POPT_ARG_INT
#define POPT_ARG_INT
Definition: popt.h:22
_POPTHELP_MAXLINE
#define _POPTHELP_MAXLINE
Definition: popthelp.c:100
poptContext_s::otherHelp
const char * otherHelp
Definition: poptint.h:147
itemUsage
static size_t itemUsage(FILE *fp, columns_t columns, poptItem item, int nitems, const char *translation_domain)
Display popt alias and exec usage.
Definition: popthelp.c:759
poptDone_s::maxopts
int maxopts
Definition: popthelp.c:787
poptArg_u::longp
long * longp
Definition: poptint.h:68
poptItem_s::option
struct poptOption option
Definition: popt.h:158
xstrdup
char * xstrdup(const char *str)
singleTableUsage
static size_t singleTableUsage(poptContext con, FILE *fp, columns_t columns, const struct poptOption *opt, const char *translation_domain, poptDone done)
Display usage text for a table of options.
Definition: popthelp.c:802
poptDone_s
Keep track of option tables already processed.
Definition: popthelp.c:785
POPT_ARG_ARGV
#define POPT_ARG_ARGV
Definition: popt.h:39
poptOption
Definition: popt.h:127
poptPrintUsage
void poptPrintUsage(poptContext con, FILE *fp, int flags)
Definition: popthelp.c:888
getTableTranslationDomain
static const char * getTableTranslationDomain(const struct poptOption *opt)
Definition: popthelp.c:158
displayArgs
static void displayArgs(poptContext con, enum poptCallbackReason foo, struct poptOption *key, const char *arg, void *data)
Display arguments.
Definition: popthelp.c:36
POPT_
#define POPT_(foo)
Definition: poptint.h:218
optionStackEntry
Definition: poptint.h:99
singleTableHelp
static void singleTableHelp(poptContext con, FILE *fp, const struct poptOption *table, columns_t columns, const char *translation_domain)
Display help text for a table of options.
Definition: popthelp.c:597
maxColumnWidth
static size_t maxColumnWidth(FILE *fp)
Return no.
Definition: popthelp.c:112

Generated for popt by  doxygen 1.8.17