00001
00002
00003
00008
00009
00010
00011
00012 #include "system.h"
00013 #include "poptint.h"
00014
00023 static void displayArgs(poptContext con,
00024 enum poptCallbackReason foo,
00025 struct poptOption * key,
00026 const char * arg, void * data)
00027
00028
00029 {
00030 if (key->shortName == '?')
00031 poptPrintHelp(con, stdout, 0);
00032 else
00033 poptPrintUsage(con, stdout, 0);
00034 exit(0);
00035 }
00036
00037 #ifdef NOTYET
00038
00039 static int show_option_defaults = 0;
00040 #endif
00041
00045
00046 struct poptOption poptAliasOptions[] = {
00047 POPT_TABLEEND
00048 };
00049
00053
00054
00055 struct poptOption poptHelpOptions[] = {
00056 #if NOTYET
00057 { NULL, '\0', POPT_ARG_INTL_DOMAIN, PACKAGE, 0, NULL, NULL},
00058 #endif
00059 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&displayArgs, '\0', NULL, NULL },
00060 { "help", '?', 0, NULL, '?', N_("Show this help message"), NULL },
00061 { "usage", '\0', 0, NULL, 'u', N_("Display brief usage message"), NULL },
00062 #ifdef NOTYET
00063 { "defaults", '\0', POPT_ARG_NONE, &show_option_defaults, 0,
00064 N_("Display option defaults in message"), NULL },
00065 #endif
00066 POPT_TABLEEND
00067 } ;
00068
00069
00073 static const char *const
00074 getTableTranslationDomain( const struct poptOption *table)
00075
00076 {
00077 const struct poptOption *opt;
00078
00079 if (table != NULL)
00080 for (opt = table; opt->longName || opt->shortName || opt->arg; opt++) {
00081 if (opt->argInfo == POPT_ARG_INTL_DOMAIN)
00082 return opt->arg;
00083 }
00084 return NULL;
00085 }
00086
00091 static const char *const
00092 getArgDescrip(const struct poptOption * opt,
00093
00094 const char * translation_domain)
00095
00096
00097 {
00098 if (!(opt->argInfo & POPT_ARG_MASK)) return NULL;
00099
00100 if (opt == (poptHelpOptions + 1) || opt == (poptHelpOptions + 2))
00101 if (opt->argDescrip) return POPT_(opt->argDescrip);
00102
00103 if (opt->argDescrip) return D_(translation_domain, opt->argDescrip);
00104
00105 switch (opt->argInfo & POPT_ARG_MASK) {
00106 case POPT_ARG_NONE: return POPT_("NONE");
00107 #ifdef DYING
00108 case POPT_ARG_VAL: return POPT_("VAL");
00109 #else
00110 case POPT_ARG_VAL: return NULL;
00111 #endif
00112 case POPT_ARG_INT: return POPT_("INT");
00113 case POPT_ARG_LONG: return POPT_("LONG");
00114 case POPT_ARG_STRING: return POPT_("STRING");
00115 case POPT_ARG_FLOAT: return POPT_("FLOAT");
00116 case POPT_ARG_DOUBLE: return POPT_("DOUBLE");
00117 default: return POPT_("ARG");
00118 }
00119 }
00120
00128 static char *
00129 singleOptionDefaultValue(int lineLength,
00130 const struct poptOption * opt,
00131
00132 const char * translation_domain)
00133
00134
00135 {
00136 const char * defstr = D_(translation_domain, "default");
00137 char * le = malloc(4*lineLength + 1);
00138 char * l = le;
00139
00140 if (le == NULL) return NULL;
00141
00142 *le = '\0';
00143 *le++ = '(';
00144 strcpy(le, defstr); le += strlen(le);
00145 *le++ = ':';
00146 *le++ = ' ';
00147 if (opt->arg)
00148 switch (opt->argInfo & POPT_ARG_MASK) {
00149 case POPT_ARG_VAL:
00150 case POPT_ARG_INT:
00151 { long aLong = *((int *)opt->arg);
00152 le += sprintf(le, "%ld", aLong);
00153 } break;
00154 case POPT_ARG_LONG:
00155 { long aLong = *((long *)opt->arg);
00156 le += sprintf(le, "%ld", aLong);
00157 } break;
00158 case POPT_ARG_FLOAT:
00159 { double aDouble = *((float *)opt->arg);
00160 le += sprintf(le, "%g", aDouble);
00161 } break;
00162 case POPT_ARG_DOUBLE:
00163 { double aDouble = *((double *)opt->arg);
00164 le += sprintf(le, "%g", aDouble);
00165 } break;
00166 case POPT_ARG_STRING:
00167 { const char * s = *(const char **)opt->arg;
00168 if (s == NULL) {
00169 strcpy(le, "null"); le += strlen(le);
00170 } else {
00171 size_t slen = 4*lineLength - (le - l) - sizeof("\"...\")");
00172 *le++ = '"';
00173 strncpy(le, s, slen); le[slen] = '\0'; le += strlen(le);
00174 if (slen < strlen(s)) {
00175 strcpy(le, "..."); le += strlen(le);
00176 }
00177 *le++ = '"';
00178 }
00179 } break;
00180 case POPT_ARG_NONE:
00181 default:
00182 l = _free(l);
00183 return NULL;
00184 break;
00185 }
00186 *le++ = ')';
00187 *le = '\0';
00188
00189
00190 return l;
00191 }
00192
00200 static void singleOptionHelp(FILE * fp, int maxLeftCol,
00201 const struct poptOption * opt,
00202 const char * translation_domain)
00203
00204
00205 {
00206 int indentLength = maxLeftCol + 5;
00207 int lineLength = 79 - indentLength;
00208 const char * help = D_(translation_domain, opt->descrip);
00209 const char * argDescrip = getArgDescrip(opt, translation_domain);
00210 int helpLength;
00211 char * defs = NULL;
00212 char * left;
00213 int nb = maxLeftCol + 1;
00214
00215
00216 if (opt->longName) nb += strlen(opt->longName);
00217 if (argDescrip) nb += strlen(argDescrip);
00218
00219
00220 left = malloc(nb);
00221 if (left == NULL) return;
00222 left[0] = '\0';
00223 left[maxLeftCol] = '\0';
00224
00225 if (opt->longName && opt->shortName)
00226 sprintf(left, "-%c, %s%s", opt->shortName,
00227 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
00228 opt->longName);
00229 else if (opt->shortName != '\0')
00230 sprintf(left, "-%c", opt->shortName);
00231 else if (opt->longName)
00232 sprintf(left, "%s%s",
00233 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
00234 opt->longName);
00235 if (!*left) goto out;
00236
00237 if (argDescrip) {
00238 char * le = left + strlen(left);
00239
00240 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
00241 *le++ = '[';
00242
00243
00244
00245 if (opt->argInfo & POPT_ARGFLAG_SHOW_DEFAULT) {
00246 defs = singleOptionDefaultValue(lineLength, opt, translation_domain);
00247 if (defs) {
00248 char * t = malloc((help ? strlen(help) : 0) +
00249 strlen(defs) + sizeof(" "));
00250 if (t) {
00251 char * te = t;
00252 *te = '\0';
00253 if (help) {
00254 strcpy(te, help); te += strlen(te);
00255 }
00256 *te++ = ' ';
00257 strcpy(te, defs);
00258 defs = _free(defs);
00259 }
00260 defs = t;
00261 }
00262 }
00263
00264
00265 if (opt->argDescrip == NULL) {
00266 switch (opt->argInfo & POPT_ARG_MASK) {
00267 case POPT_ARG_NONE:
00268 break;
00269 case POPT_ARG_VAL:
00270 #ifdef NOTNOW
00271 { long aLong = opt->val;
00272 int ops = (opt->argInfo & POPT_ARGFLAG_LOGICALOPS);
00273 int negate = (opt->argInfo & POPT_ARGFLAG_NOT);
00274
00275
00276 if (!ops && (aLong == 0L || aLong == 1L || aLong == -1L))
00277 break;
00278 *le++ = '[';
00279 switch (ops) {
00280 case POPT_ARGFLAG_OR:
00281 *le++ = '|';
00282 break;
00283 case POPT_ARGFLAG_AND:
00284 *le++ = '&';
00285 break;
00286 case POPT_ARGFLAG_XOR:
00287 *le++ = '^';
00288 break;
00289 default:
00290 break;
00291 }
00292 *le++ = '=';
00293 if (negate) *le++ = '~';
00294
00295 le += sprintf(le, (ops ? "0x%lx" : "%ld"), aLong);
00296
00297 *le++ = ']';
00298 }
00299 #endif
00300 break;
00301 case POPT_ARG_INT:
00302 case POPT_ARG_LONG:
00303 case POPT_ARG_FLOAT:
00304 case POPT_ARG_DOUBLE:
00305 case POPT_ARG_STRING:
00306 *le++ = '=';
00307 strcpy(le, argDescrip); le += strlen(le);
00308 break;
00309 default:
00310 break;
00311 }
00312 } else {
00313 *le++ = '=';
00314 strcpy(le, argDescrip); le += strlen(le);
00315 }
00316 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
00317 *le++ = ']';
00318 *le = '\0';
00319 }
00320
00321
00322 if (help)
00323 fprintf(fp," %-*s ", maxLeftCol, left);
00324 else {
00325 fprintf(fp," %s\n", left);
00326 goto out;
00327 }
00328
00329 left = _free(left);
00330 if (defs) {
00331 help = defs; defs = NULL;
00332 }
00333
00334 helpLength = strlen(help);
00335
00336 while (helpLength > lineLength) {
00337 const char * ch;
00338 char format[16];
00339
00340 ch = help + lineLength - 1;
00341 while (ch > help && !isspace(*ch)) ch--;
00342 if (ch == help) break;
00343 while (ch > (help + 1) && isspace(*ch)) ch--;
00344 ch++;
00345
00346 sprintf(format, "%%.%ds\n%%%ds", (int) (ch - help), indentLength);
00347
00348 fprintf(fp, format, help, " ");
00349
00350 help = ch;
00351 while (isspace(*help) && *help) help++;
00352 helpLength = strlen(help);
00353 }
00354
00355
00356 if (helpLength) fprintf(fp, "%s\n", help);
00357
00358 out:
00359
00360 defs = _free(defs);
00361
00362 left = _free(left);
00363 }
00364
00369 static int maxArgWidth(const struct poptOption * opt,
00370 const char * translation_domain)
00371
00372 {
00373 int max = 0;
00374 int len = 0;
00375 const char * s;
00376
00377 if (opt != NULL)
00378 while (opt->longName || opt->shortName || opt->arg) {
00379 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
00380 if (opt->arg)
00381 len = maxArgWidth(opt->arg, translation_domain);
00382 if (len > max) max = len;
00383 } else if (!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
00384 len = sizeof(" ")-1;
00385 if (opt->shortName != '\0') len += sizeof("-X")-1;
00386 if (opt->shortName != '\0' && opt->longName) len += sizeof(", ")-1;
00387 if (opt->longName) {
00388 len += ((opt->argInfo & POPT_ARGFLAG_ONEDASH)
00389 ? sizeof("-")-1 : sizeof("--")-1);
00390 len += strlen(opt->longName);
00391 }
00392
00393 s = getArgDescrip(opt, translation_domain);
00394 if (s)
00395 len += sizeof("=")-1 + strlen(s);
00396 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL) len += sizeof("[]")-1;
00397 if (len > max) max = len;
00398 }
00399
00400 opt++;
00401 }
00402
00403 return max;
00404 }
00405
00414 static void itemHelp(FILE * fp,
00415 poptItem items, int nitems, int left,
00416 const char * translation_domain)
00417
00418
00419 {
00420 poptItem item;
00421 int i;
00422
00423 if (items != NULL)
00424 for (i = 0, item = items; i < nitems; i++, item++) {
00425 const struct poptOption * opt;
00426 opt = &item->option;
00427 if ((opt->longName || opt->shortName) &&
00428 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
00429 singleOptionHelp(fp, left, opt, translation_domain);
00430 }
00431 }
00432
00441 static void singleTableHelp(poptContext con, FILE * fp,
00442 const struct poptOption * table, int left,
00443 const char * translation_domain)
00444
00445
00446 {
00447 const struct poptOption * opt;
00448 const char *sub_transdom;
00449
00450 if (table == poptAliasOptions) {
00451 itemHelp(fp, con->aliases, con->numAliases, left, NULL);
00452 itemHelp(fp, con->execs, con->numExecs, left, NULL);
00453 return;
00454 }
00455
00456 if (table != NULL)
00457 for (opt = table; (opt->longName || opt->shortName || opt->arg); opt++) {
00458 if ((opt->longName || opt->shortName) &&
00459 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
00460 singleOptionHelp(fp, left, opt, translation_domain);
00461 }
00462
00463 if (table != NULL)
00464 for (opt = table; (opt->longName || opt->shortName || opt->arg); opt++) {
00465 if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_INCLUDE_TABLE)
00466 continue;
00467 sub_transdom = getTableTranslationDomain(opt->arg);
00468 if (sub_transdom == NULL)
00469 sub_transdom = translation_domain;
00470
00471 if (opt->descrip)
00472 fprintf(fp, "\n%s\n", D_(sub_transdom, opt->descrip));
00473
00474 singleTableHelp(con, fp, opt->arg, left, sub_transdom);
00475 }
00476 }
00477
00482 static int showHelpIntro(poptContext con, FILE * fp)
00483
00484
00485 {
00486 int len = 6;
00487 const char * fn;
00488
00489 fprintf(fp, POPT_("Usage:"));
00490 if (!(con->flags & POPT_CONTEXT_KEEP_FIRST)) {
00491
00492
00493 fn = con->optionStack->argv[0];
00494
00495
00496 if (fn == NULL) return len;
00497 if (strchr(fn, '/')) fn = strrchr(fn, '/') + 1;
00498 fprintf(fp, " %s", fn);
00499 len += strlen(fn) + 1;
00500 }
00501
00502 return len;
00503 }
00504
00505 void poptPrintHelp(poptContext con, FILE * fp, int flags)
00506 {
00507 int leftColWidth;
00508
00509 (void) showHelpIntro(con, fp);
00510 if (con->otherHelp)
00511 fprintf(fp, " %s\n", con->otherHelp);
00512 else
00513 fprintf(fp, " %s\n", POPT_("[OPTION...]"));
00514
00515 leftColWidth = maxArgWidth(con->options, NULL);
00516 singleTableHelp(con, fp, con->options, leftColWidth, NULL);
00517 }
00518
00525 static int singleOptionUsage(FILE * fp, int cursor,
00526 const struct poptOption * opt,
00527 const char *translation_domain)
00528
00529
00530 {
00531 int len = 4;
00532 char shortStr[2] = { '\0', '\0' };
00533 const char * item = shortStr;
00534 const char * argDescrip = getArgDescrip(opt, translation_domain);
00535
00536 if (opt->shortName != '\0' && opt->longName != NULL) {
00537 len += 2;
00538 if (!(opt->argInfo & POPT_ARGFLAG_ONEDASH)) len++;
00539 len += strlen(opt->longName);
00540 } else if (opt->shortName != '\0') {
00541 len++;
00542 shortStr[0] = opt->shortName;
00543 shortStr[1] = '\0';
00544 } else if (opt->longName) {
00545 len += strlen(opt->longName);
00546 if (!(opt->argInfo & POPT_ARGFLAG_ONEDASH)) len++;
00547 item = opt->longName;
00548 }
00549
00550 if (len == 4) return cursor;
00551
00552 if (argDescrip)
00553 len += strlen(argDescrip) + 1;
00554
00555 if ((cursor + len) > 79) {
00556 fprintf(fp, "\n ");
00557 cursor = 7;
00558 }
00559
00560 if (opt->longName && opt->shortName) {
00561 fprintf(fp, " [-%c|-%s%s%s%s]",
00562 opt->shortName, ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "" : "-"),
00563 opt->longName,
00564 (argDescrip ? " " : ""),
00565 (argDescrip ? argDescrip : ""));
00566 } else {
00567 fprintf(fp, " [-%s%s%s%s]",
00568 ((opt->shortName || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) ? "" : "-"),
00569 item,
00570 (argDescrip ? (opt->shortName != '\0' ? " " : "=") : ""),
00571 (argDescrip ? argDescrip : ""));
00572 }
00573
00574 return cursor + len + 1;
00575 }
00576
00585 static int itemUsage(FILE * fp, int cursor, poptItem item, int nitems,
00586 const char * translation_domain)
00587
00588
00589 {
00590 int i;
00591
00592
00593 if (item != NULL)
00594 for (i = 0; i < nitems; i++, item++) {
00595 const struct poptOption * opt;
00596 opt = &item->option;
00597 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN) {
00598 translation_domain = (const char *)opt->arg;
00599 } else if ((opt->longName || opt->shortName) &&
00600 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
00601 cursor = singleOptionUsage(fp, cursor, opt, translation_domain);
00602 }
00603 }
00604
00605
00606 return cursor;
00607 }
00608
00612 typedef struct poptDone_s {
00613 int nopts;
00614 int maxopts;
00615 const void ** opts;
00616 } * poptDone;
00617
00628 static int singleTableUsage(poptContext con, FILE * fp, int cursor,
00629 const struct poptOption * opt,
00630 const char * translation_domain,
00631 poptDone done)
00632
00633
00634 {
00635
00636 if (opt != NULL)
00637 for (; (opt->longName || opt->shortName || opt->arg) ; opt++) {
00638 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN) {
00639 translation_domain = (const char *)opt->arg;
00640 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
00641 if (done) {
00642 int i = 0;
00643 for (i = 0; i < done->nopts; i++) {
00644
00645 const void * that = done->opts[i];
00646
00647 if (that == NULL || that != opt->arg)
00648 continue;
00649 break;
00650 }
00651
00652 if (opt->arg == NULL || i < done->nopts)
00653 continue;
00654
00655 if (done->nopts < done->maxopts)
00656 done->opts[done->nopts++] = (const void *) opt->arg;
00657
00658 }
00659 cursor = singleTableUsage(con, fp, cursor, opt->arg,
00660 translation_domain, done);
00661 } else if ((opt->longName || opt->shortName) &&
00662 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
00663 cursor = singleOptionUsage(fp, cursor, opt, translation_domain);
00664 }
00665 }
00666
00667
00668 return cursor;
00669 }
00670
00679 static int showShortOptions(const struct poptOption * opt, FILE * fp,
00680 char * str)
00681
00682
00683
00684 {
00685
00686 char * s = (str != NULL ? str : memset(alloca(300), 0, 300));
00687 int len = 0;
00688
00689
00690 if (opt != NULL)
00691 for (; (opt->longName || opt->shortName || opt->arg); opt++) {
00692 if (opt->shortName && !(opt->argInfo & POPT_ARG_MASK))
00693 s[strlen(s)] = opt->shortName;
00694 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE)
00695 if (opt->arg)
00696 len = showShortOptions(opt->arg, fp, s);
00697 }
00698
00699
00700
00701 if (s == str && *s != '\0') {
00702 fprintf(fp, " [-%s]", s);
00703 len = strlen(s) + sizeof(" [-]")-1;
00704 }
00705 return len;
00706 }
00707
00708 void poptPrintUsage(poptContext con, FILE * fp, int flags)
00709 {
00710 poptDone done = memset(alloca(sizeof(*done)), 0, sizeof(*done));
00711 int cursor;
00712
00713 done->nopts = 0;
00714 done->maxopts = 64;
00715 cursor = done->maxopts * sizeof(*done->opts);
00716
00717 done->opts = memset(alloca(cursor), 0, cursor);
00718 done->opts[done->nopts++] = (const void *) con->options;
00719
00720
00721 cursor = showHelpIntro(con, fp);
00722 cursor += showShortOptions(con->options, fp, NULL);
00723 cursor = singleTableUsage(con, fp, cursor, con->options, NULL, done);
00724 cursor = itemUsage(fp, cursor, con->aliases, con->numAliases, NULL);
00725 cursor = itemUsage(fp, cursor, con->execs, con->numExecs, NULL);
00726
00727 if (con->otherHelp) {
00728 cursor += strlen(con->otherHelp) + 1;
00729 if (cursor > 79) fprintf(fp, "\n ");
00730 fprintf(fp, " %s", con->otherHelp);
00731 }
00732
00733 fprintf(fp, "\n");
00734 }
00735
00736 void poptSetOtherOptionHelp(poptContext con, const char * text)
00737 {
00738 con->otherHelp = _free(con->otherHelp);
00739 con->otherHelp = xstrdup(text);
00740 }
00741