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
41 const char * arg,
void * data)
56 static int show_option_defaults = 0;
74 {
"help",
'?', 0, NULL, (int)
'?',
N_(
"Show this help message"), NULL },
75 {
"usage",
'\0', 0, NULL, (int)
'u',
N_(
"Display brief usage message"), NULL },
85 {
"help",
'?', 0, NULL, (int)
'?',
N_(
"Show this help message"), NULL },
86 {
"usage",
'\0', 0, NULL, (int)
'u',
N_(
"Display brief usage message"), NULL },
89 N_(
"Display option defaults in message"), NULL },
98 #define _POPTHELP_MAXLINE ((size_t)79)
114 #if defined(TIOCGWINSZ)
116 int fdno = fileno(fp ? fp : stdout);
118 memset(&ws, 0,
sizeof(ws));
119 if (fdno >= 0 && !ioctl(fdno, TIOCGWINSZ, &ws)
120 && (
size_t)ws.ws_col > maxcols && ws.ws_col < 256)
121 maxcols = ws.ws_col - 1;
150 const char * translation_domain)
161 if (opt == (poptHelpOptions + 1)
162 || opt == (poptHelpOptions + 2)
164 || !strcmp(opt->
argDescrip,
N_(
"Options implemented via popt alias/exec:")))
184 default:
return POPT_(
"ARG");
199 const char * translation_domain)
203 const char * defstr =
D_(translation_domain,
"default");
204 char * le = malloc(4*lineLength + 1);
207 if (le == NULL)
return NULL;
210 strcpy(le, defstr); le += strlen(le);
217 {
long aLong = *((
int *)opt->
arg);
218 le += sprintf(le,
"%ld", aLong);
221 {
long aLong = *((
long *)opt->
arg);
222 le += sprintf(le,
"%ld", aLong);
225 {
double aDouble = *((
float *)opt->
arg);
226 le += sprintf(le,
"%g", aDouble);
229 {
double aDouble = *((
double *)opt->
arg);
230 le += sprintf(le,
"%g", aDouble);
233 le += sprintf(le,
"%p", opt->
arg);
236 {
const char * s = *(
const char **)opt->
arg;
238 strcpy(le,
"null"); le += strlen(le);
240 size_t slen = 4*lineLength - (le - l) -
sizeof(
"\"...\")");
242 strncpy(le, s, slen); le[slen] =
'\0'; le += strlen(le);
243 if (slen < strlen(s)) {
244 strcpy(le,
"..."); le += strlen(le);
270 const char * translation_domain)
274 size_t maxLeftCol = columns->
cur;
275 size_t indentLength = maxLeftCol + 5;
276 size_t lineLength = columns->
max - indentLength;
277 const char * help =
D_(translation_domain, opt->
descrip);
282 size_t nb = maxLeftCol + 1;
287 if (argDescrip) nb += strlen(argDescrip);
290 if (left == NULL)
return;
292 left[maxLeftCol] =
'\0';
295 sprintf(left,
"-%c, %s%s", opt->
shortName,
301 sprintf(left,
"%s%s",
305 if (!*left)
goto out;
308 char * le = left + strlen(left);
317 char * t = malloc((help ? strlen(help) : 0) +
318 strlen(defs) +
sizeof(
" "));
323 strcpy(te, help); te += strlen(te);
339 {
long aLong = opt->
val;
344 if (!ops && (aLong == 0L || aLong == 1L || aLong == -1L))
360 *le++ = (opt->
longName != NULL ?
'=' :
' ');
361 if (negate) *le++ =
'~';
363 le += sprintf(le, (ops ?
"0x%lx" :
"%ld"), aLong);
374 *le++ = (opt->
longName != NULL ?
'=' :
' ');
375 strcpy(le, argDescrip); le += strlen(le);
385 strcpy(le, argDescrip);
389 #ifdef POPT_WCHAR_HACK
394 memset ((
void *)&t, 0,
sizeof (t));
396 n = mbsrtowcs (NULL, &scopy, strlen(scopy), &t);
398 displaypad = (int) (lelen-n);
408 fprintf(fp,
" %-*s ", (
int)(maxLeftCol+displaypad), left);
410 fprintf(fp,
" %s\n", left);
419 helpLength = strlen(help);
420 while (helpLength > lineLength) {
424 ch = help + lineLength - 1;
427 if (ch == help)
break;
432 sprintf(format,
"%%.%ds\n%%%ds", (
int) (ch - help), (
int) indentLength);
434 fprintf(fp, format, help,
" ");
439 helpLength = strlen(help);
442 if (helpLength) fprintf(fp,
"%s\n", help);
459 const char * translation_domain)
471 if (len > max) max = len;
474 if (opt->
shortName !=
'\0') len +=
sizeof(
"-X")-1;
478 ?
sizeof(
"-")-1 :
sizeof(
"--")-1);
484 #ifdef POPT_WCHAR_HACK
487 const char * scopy = s;
491 memset ((
void *)&t, 0,
sizeof (t));
493 n = mbsrtowcs (NULL, &scopy, strlen(scopy), &t);
494 len +=
sizeof(
"=")-1 + n;
498 len +=
sizeof(
"=")-1 + strlen(s);
502 if (len > max) max = len;
522 const char * translation_domain)
530 for (i = 0, item = items; i < nitems; i++, item++) {
550 const char * translation_domain)
555 const char *sub_transdom;
557 if (table == poptAliasOptions) {
575 if (sub_transdom == NULL)
576 sub_transdom = translation_domain;
579 fprintf(fp,
"\n%s\n",
D_(sub_transdom, opt->
descrip));
593 size_t len = (size_t)6;
596 fprintf(fp,
POPT_(
"Usage:"));
601 if (fn == NULL)
return len;
602 if (strchr(fn,
'/')) fn = strrchr(fn,
'/') + 1;
603 fprintf(fp,
" %s", fn);
604 len += strlen(fn) + 1;
612 columns_t columns = calloc(1,
sizeof(*columns));
618 fprintf(fp,
" %s\n",
POPT_(
"[OPTION...]"));
637 const char *translation_domain)
641 size_t len = (size_t)4;
642 char shortStr[2] = {
'\0',
'\0' };
643 const char * item = shortStr;
664 if (!bingo)
return columns->
cur;
666 #ifdef POPT_WCHAR_HACK
673 memset ((
void *)&t, 0,
sizeof (t));
675 n = mbsrtowcs (NULL, &scopy, strlen(scopy), &t);
676 len +=
sizeof(
"=")-1 + n;
680 len +=
sizeof(
"=")-1 + strlen(argDescrip);
683 if ((columns->
cur + len) > columns->
max) {
685 columns->
cur = (size_t)7;
689 fprintf(fp,
" [-%c|-%s%s%s%s]",
692 (argDescrip ?
" " :
""),
693 (argDescrip ? argDescrip :
""));
695 fprintf(fp,
" [-%s%s%s%s]",
698 (argDescrip ? (opt->
shortName !=
'\0' ?
" " :
"=") :
""),
699 (argDescrip ? argDescrip :
""));
702 return columns->
cur + len + 1;
715 const char * translation_domain)
722 for (i = 0; i < nitems; i++, item++) {
726 translation_domain = (
const char *)opt->
arg;
758 const char * translation_domain,
766 translation_domain = (
const char *)opt->
arg;
770 if (done->
opts != NULL)
771 for (i = 0; i < done->
nopts; i++) {
772 const void * that = done->
opts[i];
773 if (that == NULL || that != opt->
arg)
778 if (opt->
arg == NULL || i < done->nopts)
784 translation_domain, done);
809 size_t nb = (size_t)300;
810 char * s = (str != NULL ? str : calloc(1, nb));
811 size_t len = (size_t)0;
826 if (s != str && *s !=
'\0') {
827 fprintf(fp,
" [-%s]", s);
828 len = strlen(s) +
sizeof(
" [-]")-1;
839 columns_t columns = calloc(1,
sizeof(*columns));
843 memset(done, 0,
sizeof(*done));
847 columns->
cur = done->maxopts *
sizeof(*done->opts);
849 done->opts = calloc(1, columns->
cur);
851 if (done->opts != NULL)
852 done->opts[done->nopts++] = (
const void *) con->
options;
863 if (columns->
cur > columns->
max) fprintf(fp,
"\n ");
868 if (done->opts != NULL)