Go to the documentation of this file.
13 #define POPT_USE_TIOCGWINSZ
14 #ifdef POPT_USE_TIOCGWINSZ
15 #include <sys/ioctl.h>
18 #define POPT_WCHAR_HACK
19 #ifdef POPT_WCHAR_HACK
49 #if !defined(__LCLINT__)
57 static int show_option_defaults = 0;
75 {
"help",
'?', 0, NULL, (int)
'?',
N_(
"Show this help message"), NULL },
76 {
"usage",
'\0', 0, NULL, (int)
'u',
N_(
"Display brief usage message"), 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 },
90 N_(
"Display option defaults in message"), NULL },
92 {
"",
'\0', 0, NULL, 0,
N_(
"Terminate options"), NULL },
100 #define _POPTHELP_MAXLINE ((size_t)79)
116 #if defined(TIOCGWINSZ)
118 int fdno = fileno(fp ? fp : stdout);
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;
138 size_t n = strlen(s);
139 #ifdef POPT_WCHAR_HACK
142 memset ((
void *)&t, 0,
sizeof (t));
144 n = mbsrtowcs (NULL, &s, n, &t);
176 const char * translation_domain)
192 || !strcmp(opt->
argDescrip,
N_(
"Options implemented via popt alias/exec:")))
215 default:
return POPT_(
"ARG");
230 const char * translation_domain)
234 const char * defstr =
D_(translation_domain,
"default");
235 char * le = malloc(4*lineLength + 1);
238 if (le == NULL)
return NULL;
249 le += sprintf(le,
"%d", arg.
intp[0]);
252 le += sprintf(le,
"%hd", arg.
shortp[0]);
255 le += sprintf(le,
"%ld", arg.
longp[0]);
258 le += sprintf(le,
"%lld", arg.
longlongp[0]);
261 {
double aDouble = (double) arg.
floatp[0];
262 le += sprintf(le,
"%g", aDouble);
265 le += sprintf(le,
"%g", arg.
doublep[0]);
268 le += sprintf(le,
"%p", opt->
arg);
271 le += sprintf(le,
"%p", opt->
arg);
274 {
const char * s = arg.
argv[0];
278 size_t limit = 4*lineLength - (le - l) -
sizeof(
"\"\")");
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] =
'.';
309 const char * translation_domain)
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);
323 size_t nb = maxLeftCol + 1;
329 if (
F_ISSET(opt, TOGGLE)) nb +=
sizeof(
"[no]") - 1;
330 if (argDescrip) nb += strlen(argDescrip);
333 if (left == NULL)
return;
335 left[maxLeftCol] =
'\0';
337 #define prtlong (opt->longName != NULL)
341 char *dash =
F_ISSET(opt, ONEDASH) ?
"-" :
"--";
345 }
else if (prtshort) {
352 : (
F_ISSET(opt, ONEDASH) ?
"-" :
"--");
353 const char *longName = opt->
longName;
357 if (longName[0] ==
'n' && longName[1] ==
'o') {
358 longName +=
sizeof(
"no") - 1;
359 if (longName[0] ==
'-')
369 char * le = left + strlen(left);
375 if (
F_ISSET(opt, SHOW_DEFAULT)) {
378 char * t = malloc((help ? strlen(help) : 0) +
379 strlen(defs) +
sizeof(
" "));
398 {
long aLong = opt->
val;
399 int ops =
F_ISSET(opt, LOGICALOPS);
400 int negate =
F_ISSET(opt, NOT);
403 if (!ops && (aLong == 0L || aLong == 1L || aLong == -1L))
419 *le++ = (opt->
longName != NULL ?
'=' :
' ');
420 if (negate) *le++ =
'~';
422 le += sprintf(le, (ops ?
"0x%lx" :
"%ld"), aLong);
435 *le++ = (opt->
longName != NULL ?
'=' :
' ');
436 le =
stpcpy(le, argDescrip);
445 if (!strchr(
" =(", argDescrip[0]))
448 le =
stpcpy(leo = le, argDescrip);
459 xx =
POPT_fprintf(fp,
" %-*s ", (
int)(maxLeftCol+displaypad), left);
469 helpLength = strlen(help);
470 while (helpLength > lineLength) {
474 ch = help + lineLength - 1;
477 if (ch == help)
break;
486 {
char * fmthelp =
xstrdup(help);
488 fmthelp[ch - help] =
'\0';
489 sprintf(format,
"%%s\n%%%ds", (
int) indentLength);
500 helpLength = strlen(help);
503 if (helpLength) fprintf(fp,
"%s\n", help);
520 const char * translation_domain)
525 const char * argDescrip;
532 if (len > max) max = len;
533 }
else if (!
F_ISSET(opt, DOC_HIDDEN)) {
536 len +=
sizeof(
"-X, ")-1;
538 len += (
F_ISSET(opt, ONEDASH) ?
sizeof(
"-") :
sizeof(
"--")) - 1;
547 if (!strchr(
" =(", argDescrip[0])) len +=
sizeof(
"=")-1;
553 if (
F_ISSET(opt, OPTIONAL)) len +=
sizeof(
"[]")-1;
554 if (len > max) max = len;
573 const char * translation_domain)
581 for (i = 0, item = items; i < nitems; i++, item++) {
600 const char * translation_domain)
605 const char *sub_transdom;
608 if (con == NULL)
return;
627 if (sub_transdom == NULL)
628 sub_transdom = translation_domain;
648 size_t len = (size_t)6;
654 const char * fn = (os->
argv ? os->
argv[0] : NULL);
655 if (fn == NULL)
return len;
656 if (strchr(fn,
'/')) fn = strrchr(fn,
'/') + 1;
658 fprintf(fp,
" %s", fn);
659 len += strlen(fn) + 1;
667 columns_t columns = calloc((
size_t)1,
sizeof(*columns));
670 if (con == NULL)
return;
695 const char *translation_domain)
699 size_t len =
sizeof(
" []")-1;
700 const char * argDescrip =
getArgDescrip(opt, translation_domain);
704 #define prtlong (opt->longName != NULL)
708 len =
sizeof(
" []")-1;
710 len +=
sizeof(
"-c")-1;
712 if (prtshort) len +=
sizeof(
"|")-1;
713 len += (
F_ISSET(opt, ONEDASH) ?
sizeof(
"-") :
sizeof(
"--")) - 1;
720 if (!strchr(
" =(", argDescrip[0])) len +=
sizeof(
"=")-1;
726 if ((columns->
cur + len) > columns->
max) {
728 columns->
cur = (size_t)7;
735 fprintf(fp,
"%s%s%s",
736 (prtshort ?
"|" :
""),
737 (
F_ISSET(opt, ONEDASH) ?
"-" :
"--"),
743 if (!strchr(
" =(", argDescrip[0])) fprintf(fp,
"=");
744 fprintf(fp,
"%s", argDescrip);
748 return columns->
cur + len + 1;
761 const char * translation_domain)
768 for (i = 0; i < nitems; i++, item++) {
772 translation_domain = (
const char *)opt->
arg;
804 const char * translation_domain,
809 if (con != NULL && opt != NULL)
812 translation_domain = (
const char *)opt->
arg;
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)
825 if (opt->
arg == NULL || i < done->nopts)
831 translation_domain, done);
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;
877 if (s != str && *s !=
'\0') {
878 fprintf(fp,
" [-%s]", s);
879 len = strlen(s) +
sizeof(
" [-]")-1;
890 columns_t columns = calloc((
size_t)1,
sizeof(*columns));
894 if (con == NULL)
return;
896 memset(done, 0,
sizeof(*done));
900 columns->
cur = done->maxopts *
sizeof(*done->opts);
902 done->opts = calloc((
size_t)1, columns->
cur);
904 if (done->opts != NULL)
905 done->opts[done->nopts++] = (
const void *) con->
options;
916 if (columns->
cur > columns->
max) fprintf(fp,
"\n ");
921 if (done->opts != NULL)
929 if (con == NULL)
return;
static size_t showShortOptions(const struct poptOption *opt, FILE *fp, char *str)
Return concatenated short options for display.
static size_t maxArgWidth(const struct poptOption *opt, const char *translation_domain)
Find display width for longest argument string.
static char * singleOptionDefaultValue(size_t lineLength, const struct poptOption *opt, const char *translation_domain)
Display default value for an option.
static size_t showHelpIntro(poptContext con, FILE *fp)
#define POPT_ARG_INCLUDE_TABLE
static char * stpcpy(char *dest, const char *src)
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
static size_t singleOptionUsage(FILE *fp, columns_t columns, const struct poptOption *opt, const char *translation_domain)
Display usage text for an option.
#define POPT_ARG_MAINCALL
poptContext poptFreeContext(poptContext con)
#define poptArgType(_opt)
const char * POPT_next_char(const char *str)
struct poptOption * poptHelpOptionsI18N
static size_t stringDisplayWidth(const char *s)
Determine number of display characters in a string.
static void itemHelp(FILE *fp, poptItem items, int nitems, columns_t columns, const char *translation_domain)
Display popt alias and exec help.
void poptSetOtherOptionHelp(poptContext con, const char *text)
static const char * getArgDescrip(const struct poptOption *opt, const char *translation_domain)
struct poptDone_s * poptDone
Keep track of option tables already processed.
static struct poptOption poptHelpOptions2[]
#define POPT_ARG_INTL_DOMAIN
#define POPT_ARG_CALLBACK
#define F_ISSET(_opt, _FLAG)
#define POPT_ARG_LONGLONG
struct poptOption poptAliasOptions[]
Empty table marker to enable displaying popt alias/exec options.
int POPT_fprintf(FILE *stream, const char *format,...)
static void singleOptionHelp(FILE *fp, columns_t columns, const struct poptOption *opt, const char *translation_domain)
Display help text for an option.
#define POPT_CONTEXT_KEEP_FIRST
struct columns_s * columns_t
struct optionStackEntry optionStack[POPT_OPTION_DEPTH]
void poptPrintHelp(poptContext con, FILE *fp, int flags)
struct poptOption poptHelpOptions[]
Auto help table options.
const char * POPT_prev_char(const char *str)
#define _isspaceptr(_chp)
const struct poptOption * options
#define _POPTHELP_MAXLINE
static size_t itemUsage(FILE *fp, columns_t columns, poptItem item, int nitems, const char *translation_domain)
Display popt alias and exec usage.
char * xstrdup(const char *str)
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.
Keep track of option tables already processed.
void poptPrintUsage(poptContext con, FILE *fp, int flags)
static const char * getTableTranslationDomain(const struct poptOption *opt)
static void displayArgs(poptContext con, enum poptCallbackReason foo, struct poptOption *key, const char *arg, void *data)
Display arguments.
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.
static size_t maxColumnWidth(FILE *fp)
Return no.
Generated for popt by
1.8.17