13#define POPT_USE_TIOCGWINSZ
14#ifdef POPT_USE_TIOCGWINSZ
18#define POPT_WCHAR_HACK
49static int show_option_defaults = 0;
64 {
"help",
'?', 0, NULL, (int)
'?',
N_(
"Show this help message"), NULL },
65 {
"usage",
'\0', 0, NULL, (int)
'u',
N_(
"Display brief usage message"), NULL },
72 {
"help",
'?', 0, NULL, (int)
'?',
N_(
"Show this help message"), NULL },
73 {
"usage",
'\0', 0, NULL, (int)
'u',
N_(
"Display brief usage message"), NULL },
76 N_(
"Display option defaults in message"), NULL },
78 { NULL,
'\0', 0, NULL, 0,
N_(
"Terminate options"), NULL },
84#define _POPTHELP_MAXLINE ((size_t)79)
99#if defined(TIOCGWINSZ)
101 int fdno = fileno(fp ? fp : stdout);
103 memset(&ws, 0,
sizeof(ws));
104 if (fdno >= 0 && !ioctl(fdno, (
unsigned long)TIOCGWINSZ, &ws)) {
105 size_t ws_col = (size_t)ws.ws_col;
106 if (ws_col > maxcols && ws_col < (
size_t)256)
107 maxcols = ws_col - 1;
120 size_t n = strlen(s);
121#ifdef POPT_WCHAR_HACK
124 memset ((
void *)&t, 0,
sizeof (t));
126 n = mbsrtowcs (NULL, &s, n, &t);
157 const char * translation_domain)
171 || !strcmp(opt->
argDescrip,
N_(
"Options implemented via popt alias/exec:")))
194 default:
return POPT_(
"ARG");
209 const char * translation_domain)
211 const char * defstr =
D_(translation_domain,
"default");
212 char * le = malloc(4*lineLength + 1);
215 if (le == NULL)
return NULL;
226 le += sprintf(le,
"%d", arg.
intp[0]);
229 le += sprintf(le,
"%hd", arg.
shortp[0]);
232 le += sprintf(le,
"%ld", arg.
longp[0]);
235 le += sprintf(le,
"%lld", arg.
longlongp[0]);
238 {
double aDouble = (double) arg.
floatp[0];
239 le += sprintf(le,
"%g", aDouble);
242 le += sprintf(le,
"%g", arg.
doublep[0]);
245 le += sprintf(le,
"%p", opt->
arg);
248 le += sprintf(le,
"%p", opt->
arg);
251 {
const char * s = arg.
argv[0];
255 size_t limit = 4*lineLength - (le - l) -
sizeof(
"\"\")");
258 strncpy(le, s, limit); le[limit] =
'\0'; le += (slen = strlen(le));
259 if (slen == limit && s[limit])
260 le[-1] = le[-2] = le[-3] =
'.';
286 const char * translation_domain)
288 size_t maxLeftCol = columns->
cur;
289 size_t indentLength = maxLeftCol + 5;
290 size_t lineLength = columns->
max - indentLength;
291 const char * help =
D_(translation_domain, opt->
descrip);
292 const char * argDescrip =
getArgDescrip(opt, translation_domain);
298 size_t nb = maxLeftCol + 1;
303 if (
F_ISSET(opt, TOGGLE)) nb +=
sizeof(
"[no]") - 1;
304 if (argDescrip) nb += strlen(argDescrip);
307 if (left == NULL)
return;
309 left[maxLeftCol] =
'\0';
311#define prtlong (opt->longName != NULL)
315 char *dash =
F_ISSET(opt, ONEDASH) ?
"-" :
"--";
319 }
else if (prtshort) {
326 : (
F_ISSET(opt, ONEDASH) ?
"-" :
"--");
327 const char *longName = opt->
longName;
331 if (longName[0] ==
'n' && longName[1] ==
'o') {
332 longName +=
sizeof(
"no") - 1;
333 if (longName[0] ==
'-')
343 char * le = left + strlen(left);
349 if (
F_ISSET(opt, SHOW_DEFAULT)) {
352 char * t = malloc((help ? strlen(help) : 0) +
353 strlen(defs) +
sizeof(
" "));
372 {
long aLong = opt->
val;
373 int ops =
F_ISSET(opt, LOGICALOPS);
374 int negate =
F_ISSET(opt, NOT);
377 if (!ops && (aLong == 0L || aLong == 1L || aLong == -1L))
393 *le++ = (opt->
longName != NULL ?
'=' :
' ');
394 if (negate) *le++ =
'~';
395 le += sprintf(le, (ops ?
"0x%lx" :
"%ld"), aLong);
407 *le++ = (opt->
longName != NULL ?
'=' :
' ');
408 le =
stpcpy(le, argDescrip);
417 if (!strchr(
" =(", argDescrip[0]))
420 le =
stpcpy(leo = le, argDescrip);
431 POPT_fprintf(fp,
" %-*s ", (
int)(maxLeftCol+displaypad), left);
441 helpLength = strlen(help);
442 while (helpLength > lineLength) {
446 ch = help + lineLength - 1;
449 if (ch == help)
break;
458 {
char * fmthelp =
xstrdup(help);
460 fmthelp[ch - help] =
'\0';
461 sprintf(format,
"%%s\n%%%ds", (
int) indentLength);
470 helpLength = strlen(help);
473 if (helpLength) fprintf(fp,
"%s\n", help);
488 const char * translation_domain)
492 const char * argDescrip;
497 void * arg = opt->
arg;
503 if (len > max) max = len;
504 }
else if (!
F_ISSET(opt, DOC_HIDDEN)) {
507 len +=
sizeof(
"-X, ")-1;
509 len += (
F_ISSET(opt, ONEDASH) ?
sizeof(
"-") :
sizeof(
"--")) - 1;
518 if (!strchr(
" =(", argDescrip[0])) len +=
sizeof(
"=")-1;
524 if (
F_ISSET(opt, OPTIONAL)) len +=
sizeof(
"[]")-1;
525 if (len > max) max = len;
544 const char * translation_domain)
550 for (i = 0, item = items; i < nitems; i++, item++) {
569 const char * translation_domain)
572 const char *sub_transdom;
574 if (con == NULL)
return;
597 if (sub_transdom == NULL)
598 sub_transdom = translation_domain;
616 size_t len = (size_t)6;
621 const char * fn = (os->
argv ? os->
argv[0] : NULL);
622 if (fn == NULL)
return len;
623 if (strchr(fn,
'/')) fn = strrchr(fn,
'/') + 1;
625 fprintf(fp,
" %s", fn);
626 len += strlen(fn) + 1;
634 columns_t columns = calloc((
size_t)1,
sizeof(*columns));
636 if (con == NULL)
return;
661 const char *translation_domain)
663 size_t len =
sizeof(
" []")-1;
664 const char * argDescrip =
getArgDescrip(opt, translation_domain);
668#define prtlong (opt->longName != NULL)
672 len =
sizeof(
" []")-1;
674 len +=
sizeof(
"-c")-1;
676 if (prtshort) len +=
sizeof(
"|")-1;
677 len += (
F_ISSET(opt, ONEDASH) ?
sizeof(
"-") :
sizeof(
"--")) - 1;
684 if (!strchr(
" =(", argDescrip[0])) len +=
sizeof(
"=")-1;
690 if ((columns->
cur + len) > columns->
max) {
692 columns->
cur = (size_t)7;
699 fprintf(fp,
"%s%s%s",
700 (prtshort ?
"|" :
""),
701 (
F_ISSET(opt, ONEDASH) ?
"-" :
"--"),
707 if (!strchr(
" =(", argDescrip[0])) fprintf(fp,
"=");
708 fprintf(fp,
"%s", argDescrip);
712 return columns->
cur + len + 1;
725 const char * translation_domain)
730 for (i = 0; i < nitems; i++, item++) {
734 translation_domain = (
const char *)opt->
arg;
765 const char * translation_domain,
768 if (con != NULL && opt != NULL)
771 translation_domain = (
const char *)opt->
arg;
774 void * arg = opt->
arg;
780 if (done->
opts != NULL)
781 for (i = 0; i < done->
nopts; i++) {
782 const void * that = done->
opts[i];
783 if (that == NULL || that != arg)
788 if (arg == NULL || i < done->nopts)
791 done->
opts[done->
nopts++] = (
const void *) arg;
794 translation_domain, done);
816 size_t nb = (size_t)300;
817 char * s = (str != NULL ? str : calloc((
size_t)1, nb));
818 size_t len = (size_t)0;
832 void * arg = opt->
arg;
842 if (s != str && *s !=
'\0') {
843 fprintf(fp,
" [-%s]", s);
844 len = strlen(s) +
sizeof(
" [-]")-1;
853 columns_t columns = calloc((
size_t)1,
sizeof(*columns));
857 if (con == NULL)
return;
859 memset(done, 0,
sizeof(*done));
863 columns->
cur = done->maxopts *
sizeof(*done->opts);
865 done->opts = calloc((
size_t)1, columns->
cur);
866 if (done->opts != NULL)
867 done->opts[done->nopts++] = (
const void *) con->
options;
877 if (columns->
cur > columns->
max) fprintf(fp,
"\n ");
882 if (done->opts != NULL)
890 if (con == NULL)
return;
poptContext poptFreeContext(poptContext con)
Destroy context.
#define POPT_CONTEXT_KEEP_FIRST
#define POPT_ARG_MAINCALL
#define POPT_ARG_INTL_DOMAIN
#define POPT_ARG_INCLUDE_TABLE
#define POPT_ARG_LONGLONG
#define POPT_ARG_CALLBACK
static size_t showShortOptions(const struct poptOption *opt, FILE *fp, char *str)
Return concatenated short options for display.
struct poptOption poptHelpOptions[]
Auto help table options.
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 void itemHelp(FILE *fp, poptItem items, int nitems, columns_t columns, const char *translation_domain)
Display popt alias and exec help.
static void singleOptionHelp(FILE *fp, columns_t columns, const struct poptOption *opt, const char *translation_domain)
Display help text for an option.
static struct poptOption poptHelpOptions2[]
struct columns_s * columns_t
static size_t singleOptionUsage(FILE *fp, columns_t columns, const struct poptOption *opt, const char *translation_domain)
Display usage text for an option.
static void displayArgs(poptContext con, enum poptCallbackReason foo, struct poptOption *key, const char *arg, void *data)
Display arguments.
void poptPrintHelp(poptContext con, FILE *fp, int flags)
Print detailed description of options.
static const char * getTableTranslationDomain(const struct poptOption *opt)
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.
static size_t itemUsage(FILE *fp, columns_t columns, poptItem item, int nitems, const char *translation_domain)
Display popt alias and exec usage.
static size_t showHelpIntro(poptContext con, FILE *fp)
#define _POPTHELP_MAXLINE
struct poptOption * poptHelpOptionsI18N
struct poptOption poptAliasOptions[]
Empty table marker to enable displaying popt alias/exec options.
static const char * getArgDescrip(const struct poptOption *opt, const char *translation_domain)
void poptSetOtherOptionHelp(poptContext con, const char *text)
Provide text to replace default "[OPTION...]" in help/usage output.
static size_t stringDisplayWidth(const char *s)
Determine number of display characters in a string.
static size_t maxColumnWidth(FILE *fp)
Return no.
struct poptDone_s * poptDone
Keep track of option tables already processed.
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.
void poptPrintUsage(poptContext con, FILE *fp, int flags)
Print terse description of options.
const char * POPT_prev_char(const char *str)
const char * POPT_next_char(const char *str)
int POPT_fprintf(FILE *stream, const char *format,...)
#define F_ISSET(_opt, _FLAG)
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
#define poptArgType(_opt)
const struct poptOption * options
struct optionStackEntry optionStack[POPT_OPTION_DEPTH]
Keep track of option tables already processed.
A popt alias or exec argument for poptAddItem().
#define _isspaceptr(_chp)
static char * stpcpy(char *dest, const char *src)
A union to simplify opt->arg access without casting.