00001
00002
00007
00008
00009
00010
00011 #include "system.h"
00012 #include "poptint.h"
00013
00018 static void displayArgs(poptContext con,
00019 enum poptCallbackReason foo,
00020 struct poptOption * key,
00021 const char * arg, void * data)
00022 {
00023 if (key->shortName == '?')
00024 poptPrintHelp(con, stdout, 0);
00025 else
00026 poptPrintUsage(con, stdout, 0);
00027 exit(0);
00028 }
00029
00030 #ifdef NOTYET
00031 static int show_option_defaults = 0;
00032 #endif
00033
00037 struct poptOption poptAliasOptions[] = {
00038 POPT_TABLEEND
00039 };
00040
00044
00045 struct poptOption poptHelpOptions[] = {
00046 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&displayArgs, '\0', NULL, NULL },
00047 { "help", '?', 0, NULL, '?', N_("Show this help message"), NULL },
00048 { "usage", '\0', 0, NULL, 'u', N_("Display brief usage message"), NULL },
00049 #ifdef NOTYET
00050 { "defaults", '\0', POPT_ARG_NONE, &show_option_defaults, 0,
00051 N_("Display option defaults in message"), NULL },
00052 #endif
00053 POPT_TABLEEND
00054 } ;
00055
00056
00060 static const char *const
00061 getTableTranslationDomain( const struct poptOption *table)
00062
00063 {
00064 const struct poptOption *opt;
00065
00066 if (table != NULL)
00067 for (opt = table; opt->longName || opt->shortName || opt->arg; opt++) {
00068 if (opt->argInfo == POPT_ARG_INTL_DOMAIN)
00069 return opt->arg;
00070 }
00071 return NULL;
00072 }
00073
00078 static const char *const
00079 getArgDescrip(const struct poptOption * opt,
00080
00081 const char * translation_domain)
00082
00083
00084 {
00085 if (!(opt->argInfo & POPT_ARG_MASK)) return NULL;
00086
00087 if (opt == (poptHelpOptions + 1) || opt == (poptHelpOptions + 2))
00088 if (opt->argDescrip) return POPT_(opt->argDescrip);
00089
00090 if (opt->argDescrip) return D_(translation_domain, opt->argDescrip);
00091
00092 switch (opt->argInfo & POPT_ARG_MASK) {
00093 case POPT_ARG_NONE: return POPT_("NONE");
00094 case POPT_ARG_VAL: return POPT_("VAL");
00095 case POPT_ARG_INT: return POPT_("INT");
00096 case POPT_ARG_LONG: return POPT_("LONG");
00097 case POPT_ARG_STRING: return POPT_("STRING");
00098 case POPT_ARG_FLOAT: return POPT_("FLOAT");
00099 case POPT_ARG_DOUBLE: return POPT_("DOUBLE");
00100 default: return POPT_("ARG");
00101 }
00102 }
00103
00108 static char *
00109 singleOptionDefaultValue(int lineLength,
00110 const struct poptOption * opt,
00111
00112 const char * translation_domain)
00113
00114
00115 {
00116 const char * defstr = D_(translation_domain, "default");
00117 char * le = malloc(4*lineLength + 1);
00118 char * l = le;
00119
00120 if (l == NULL) return NULL;
00121 *le = '\0';
00122 *le++ = '(';
00123 strcpy(le, defstr); le += strlen(le);
00124 *le++ = ':';
00125 *le++ = ' ';
00126 if (opt->arg)
00127 switch (opt->argInfo & POPT_ARG_MASK) {
00128 case POPT_ARG_VAL:
00129 case POPT_ARG_INT:
00130 { long aLong = *((int *)opt->arg);
00131 le += sprintf(le, "%ld", aLong);
00132 } break;
00133 case POPT_ARG_LONG:
00134 { long aLong = *((long *)opt->arg);
00135 le += sprintf(le, "%ld", aLong);
00136 } break;
00137 case POPT_ARG_FLOAT:
00138 { double aDouble = *((float *)opt->arg);
00139 le += sprintf(le, "%g", aDouble);
00140 } break;
00141 case POPT_ARG_DOUBLE:
00142 { double aDouble = *((double *)opt->arg);
00143 le += sprintf(le, "%g", aDouble);
00144 } break;
00145 case POPT_ARG_STRING:
00146 { const char * s = *(const char **)opt->arg;
00147 if (s == NULL) {
00148 strcpy(le, "null"); le += strlen(le);
00149 } else {
00150 size_t slen = 4*lineLength - (le - l) - sizeof("\"...\")");
00151 *le++ = '"';
00152 strncpy(le, s, slen); le[slen] = '\0'; le += strlen(le);
00153 if (slen < strlen(s)) {
00154 strcpy(le, "..."); le += strlen(le);
00155 }
00156 *le++ = '"';
00157 }
00158 } break;
00159 case POPT_ARG_NONE:
00160 default:
00161 l = _free(l);
00162 return NULL;
00163 break;
00164 }
00165 *le++ = ')';
00166 *le = '\0';
00167
00168 return l;
00169 }
00170
00176 static void singleOptionHelp(FILE * fp, int maxLeftCol,
00177 const struct poptOption * opt,
00178 const char * translation_domain)
00179
00180 {
00181 int indentLength = maxLeftCol + 5;
00182 int lineLength = 79 - indentLength;
00183 const char * help = D_(translation_domain, opt->descrip);
00184 const char * argDescrip = getArgDescrip(opt, translation_domain);
00185 int helpLength;
00186 char * defs = NULL;
00187 char * left;
00188 int nb = maxLeftCol + 1;
00189
00190
00191 if (opt->longName) nb += strlen(opt->longName);
00192 if (argDescrip) nb += strlen(argDescrip);
00193
00194 left = malloc(nb);
00195 if (left == NULL) return;
00196 left[0] = '\0';
00197 left[maxLeftCol] = '\0';
00198
00199 if (opt->longName && opt->shortName)
00200 sprintf(left, "-%c, %s%s", opt->shortName,
00201 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
00202 opt->longName);
00203 else if (opt->shortName != '\0')
00204 sprintf(left, "-%c", opt->shortName);
00205 else if (opt->longName)
00206 sprintf(left, "%s%s",
00207 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
00208 opt->longName);
00209 if (!*left) goto out;
00210 if (argDescrip) {
00211 char * le = left + strlen(left);
00212
00213 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
00214 *le++ = '[';
00215
00216
00217 if (opt->argInfo & POPT_ARGFLAG_SHOW_DEFAULT) {
00218 defs = singleOptionDefaultValue(lineLength, opt, translation_domain);
00219 if (defs) {
00220 char * t = malloc((help ? strlen(help) : 0) +
00221 strlen(defs) + sizeof(" "));
00222 if (t) {
00223 char * te = t;
00224 *te = '\0';
00225 if (help) {
00226 strcpy(te, help); te += strlen(te);
00227 }
00228 *te++ = ' ';
00229 strcpy(te, defs);
00230 defs = _free(defs);
00231 }
00232 defs = t;
00233 }
00234 }
00235
00236 if (opt->argDescrip == NULL) {
00237 switch (opt->argInfo & POPT_ARG_MASK) {
00238 case POPT_ARG_NONE:
00239 break;
00240 case POPT_ARG_VAL:
00241 { long aLong = opt->val;
00242 int ops = (opt->argInfo & POPT_ARGFLAG_LOGICALOPS);
00243 int negate = (opt->argInfo & POPT_ARGFLAG_NOT);
00244
00245
00246 if (!ops && (aLong == 0L || aLong == 1L || aLong == -1L))
00247 break;
00248 *le++ = '[';
00249 switch (ops) {
00250 case POPT_ARGFLAG_OR: *le++ = '|'; break;
00251 case POPT_ARGFLAG_AND: *le++ = '&'; break;
00252 case POPT_ARGFLAG_XOR: *le++ = '^'; break;
00253 default: break;
00254 }
00255 *le++ = '=';
00256 if (negate) *le++ = '~';
00257
00258 le += sprintf(le, (ops ? "0x%lx" : "%ld"), aLong);
00259
00260 *le++ = ']';
00261 } break;
00262 case POPT_ARG_INT:
00263 case POPT_ARG_LONG:
00264 case POPT_ARG_FLOAT:
00265 case POPT_ARG_DOUBLE:
00266 case POPT_ARG_STRING:
00267 *le++ = '=';
00268 strcpy(le, argDescrip); le += strlen(le);
00269 break;
00270 default:
00271 break;
00272 }
00273 } else {
00274 *le++ = '=';
00275 strcpy(le, argDescrip); le += strlen(le);
00276 }
00277 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
00278 *le++ = ']';
00279 *le = '\0';
00280 }
00281
00282 if (help)
00283 fprintf(fp," %-*s ", maxLeftCol, left);
00284 else {
00285 fprintf(fp," %s\n", left);
00286 goto out;
00287 }
00288
00289 left = _free(left);
00290 if (defs) {
00291 help = defs; defs = NULL;
00292 }
00293
00294 helpLength = strlen(help);
00295 while (helpLength > lineLength) {
00296 const char * ch;
00297 char format[10];
00298
00299 ch = help + lineLength - 1;
00300 while (ch > help && !isspace(*ch)) ch--;
00301 if (ch == help) break;
00302 while (ch > (help + 1) && isspace(*ch)) ch--;
00303 ch++;
00304
00305 sprintf(format, "%%.%ds\n%%%ds", (int) (ch - help), indentLength);
00306
00307 fprintf(fp, format, help, " ");
00308
00309 help = ch;
00310 while (isspace(*help) && *help) help++;
00311 helpLength = strlen(help);
00312 }
00313
00314 if (helpLength) fprintf(fp, "%s\n", help);
00315
00316 out:
00317 defs = _free(defs);
00318 left = _free(left);
00319 }
00320
00325 static int maxArgWidth(const struct poptOption * opt,
00326 const char * translation_domain)
00327
00328 {
00329 int max = 0;
00330 int len = 0;
00331 const char * s;
00332
00333 if (opt != NULL)
00334 while (opt->longName || opt->shortName || opt->arg) {
00335 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
00336 if (opt->arg)
00337 len = maxArgWidth(opt->arg, translation_domain);
00338 if (len > max) max = len;
00339 } else if (!(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
00340 len = sizeof(" ")-1;
00341 if (opt->shortName != '\0') len += sizeof("-X")-1;
00342 if (opt->shortName != '\0' && opt->longName) len += sizeof(", ")-1;
00343 if (opt->longName) {
00344 len += ((opt->argInfo & POPT_ARGFLAG_ONEDASH)
00345 ? sizeof("-")-1 : sizeof("--")-1);
00346 len += strlen(opt->longName);
00347 }
00348
00349 s = getArgDescrip(opt, translation_domain);
00350 if (s)
00351 len += sizeof("=")-1 + strlen(s);
00352 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL) len += sizeof("[]")-1;
00353 if (len > max) max = len;
00354 }
00355
00356 opt++;
00357 }
00358
00359 return max;
00360 }
00361
00369 static void itemHelp(FILE * fp,
00370 poptItem items, int nitems, int left,
00371 const char * translation_domain)
00372
00373 {
00374 poptItem item;
00375 int i;
00376
00377 if (items != NULL)
00378 for (i = 0, item = items; i < nitems; i++, item++) {
00379 const struct poptOption * opt;
00380 opt = &item->option;
00381 if ((opt->longName || opt->shortName) &&
00382 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
00383 singleOptionHelp(fp, left, opt, translation_domain);
00384 }
00385 }
00386
00392 static void singleTableHelp(poptContext con, FILE * fp,
00393 const struct poptOption * table, int left,
00394 const char * translation_domain)
00395
00396 {
00397 const struct poptOption * opt;
00398 const char *sub_transdom;
00399
00400 if (table == poptAliasOptions) {
00401 itemHelp(fp, con->aliases, con->numAliases, left, NULL);
00402 itemHelp(fp, con->execs, con->numExecs, left, NULL);
00403 return;
00404 }
00405
00406 if (table != NULL)
00407 for (opt = table; (opt->longName || opt->shortName || opt->arg); opt++) {
00408 if ((opt->longName || opt->shortName) &&
00409 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN))
00410 singleOptionHelp(fp, left, opt, translation_domain);
00411 }
00412
00413 if (table != NULL)
00414 for (opt = table; (opt->longName || opt->shortName || opt->arg); opt++) {
00415 if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_INCLUDE_TABLE)
00416 continue;
00417 sub_transdom = getTableTranslationDomain(opt->arg);
00418 if (sub_transdom == NULL)
00419 sub_transdom = translation_domain;
00420
00421 if (opt->descrip)
00422 fprintf(fp, "\n%s\n", D_(sub_transdom, opt->descrip));
00423
00424 singleTableHelp(con, fp, opt->arg, left, sub_transdom);
00425 }
00426 }
00427
00432 static int showHelpIntro(poptContext con, FILE * fp)
00433
00434 {
00435 int len = 6;
00436 const char * fn;
00437
00438 fprintf(fp, POPT_("Usage:"));
00439 if (!(con->flags & POPT_CONTEXT_KEEP_FIRST)) {
00440
00441 fn = con->optionStack->argv[0];
00442
00443 if (fn == NULL) return len;
00444 if (strchr(fn, '/')) fn = strrchr(fn, '/') + 1;
00445 fprintf(fp, " %s", fn);
00446 len += strlen(fn) + 1;
00447 }
00448
00449 return len;
00450 }
00451
00452 void poptPrintHelp(poptContext con, FILE * fp, int flags)
00453 {
00454 int leftColWidth;
00455
00456 (void) showHelpIntro(con, fp);
00457 if (con->otherHelp)
00458 fprintf(fp, " %s\n", con->otherHelp);
00459 else
00460 fprintf(fp, " %s\n", POPT_("[OPTION...]"));
00461
00462 leftColWidth = maxArgWidth(con->options, NULL);
00463 singleTableHelp(con, fp, con->options, leftColWidth, NULL);
00464 }
00465
00471 static int singleOptionUsage(FILE * fp, int cursor,
00472 const struct poptOption * opt,
00473 const char *translation_domain)
00474
00475 {
00476 int len = 3;
00477 char shortStr[2] = { '\0', '\0' };
00478 const char * item = shortStr;
00479 const char * argDescrip = getArgDescrip(opt, translation_domain);
00480
00481 if (opt->shortName!= '\0' ) {
00482 if (!(opt->argInfo & POPT_ARG_MASK))
00483 return cursor;
00484 len++;
00485 shortStr[0] = opt->shortName;
00486 shortStr[1] = '\0';
00487 } else if (opt->longName) {
00488 len += 1 + strlen(opt->longName);
00489 item = opt->longName;
00490 }
00491
00492 if (len == 3) return cursor;
00493
00494 if (argDescrip)
00495 len += strlen(argDescrip) + 1;
00496
00497 if ((cursor + len) > 79) {
00498 fprintf(fp, "\n ");
00499 cursor = 7;
00500 }
00501
00502 fprintf(fp, " [-%s%s%s%s]",
00503 ((opt->shortName || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) ? "" : "-"),
00504 item,
00505 (argDescrip ? (opt->shortName != '\0' ? " " : "=") : ""),
00506 (argDescrip ? argDescrip : ""));
00507
00508 return cursor + len + 1;
00509 }
00510
00518 static int itemUsage(FILE * fp, int cursor, poptItem item, int nitems,
00519 const char * translation_domain)
00520
00521 {
00522 int i;
00523
00524
00525 if (item != NULL)
00526 for (i = 0; i < nitems; i++, item++) {
00527 const struct poptOption * opt;
00528 opt = &item->option;
00529 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN) {
00530 translation_domain = (const char *)opt->arg;
00531 } else if ((opt->longName || opt->shortName) &&
00532 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
00533 cursor = singleOptionUsage(fp, cursor, opt, translation_domain);
00534 }
00535 }
00536
00537
00538 return cursor;
00539 }
00540
00546 static int singleTableUsage(poptContext con, FILE * fp,
00547 int cursor, const struct poptOption * opt,
00548 const char * translation_domain)
00549
00550 {
00551
00552 if (opt != NULL)
00553 for (; (opt->longName || opt->shortName || opt->arg) ; opt++) {
00554 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INTL_DOMAIN) {
00555 translation_domain = (const char *)opt->arg;
00556 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
00557 if (opt->arg)
00558 cursor = singleTableUsage(con, fp, cursor, opt->arg,
00559 translation_domain);
00560 } else if ((opt->longName || opt->shortName) &&
00561 !(opt->argInfo & POPT_ARGFLAG_DOC_HIDDEN)) {
00562 cursor = singleOptionUsage(fp, cursor, opt, translation_domain);
00563 }
00564 }
00565
00566
00567 return cursor;
00568 }
00569
00577 static int showShortOptions(const struct poptOption * opt, FILE * fp,
00578 char * str)
00579
00580 {
00581 char * s = alloca(300);
00582
00583 s[0] = '\0';
00584
00585 if (str == NULL) {
00586 memset(s, 0, sizeof(s));
00587 str = s;
00588 }
00589
00590
00591 if (opt != NULL)
00592 for (; (opt->longName || opt->shortName || opt->arg); opt++) {
00593 if (opt->shortName && !(opt->argInfo & POPT_ARG_MASK))
00594 str[strlen(str)] = opt->shortName;
00595 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE)
00596 if (opt->arg)
00597 (void) showShortOptions(opt->arg, fp, str);
00598 }
00599
00600 if (s != str || *s != '\0')
00601 return 0;
00602
00603 fprintf(fp, " [-%s]", s);
00604 return strlen(s) + 4;
00605 }
00606
00607 void poptPrintUsage(poptContext con, FILE * fp, int flags)
00608 {
00609 int cursor;
00610
00611 cursor = showHelpIntro(con, fp);
00612 cursor += showShortOptions(con->options, fp, NULL);
00613 (void) singleTableUsage(con, fp, cursor, con->options, NULL);
00614 (void) itemUsage(fp, cursor, con->aliases, con->numAliases, NULL);
00615 (void) itemUsage(fp, cursor, con->execs, con->numExecs, NULL);
00616
00617 if (con->otherHelp) {
00618 cursor += strlen(con->otherHelp) + 1;
00619 if (cursor > 79) fprintf(fp, "\n ");
00620 fprintf(fp, " %s", con->otherHelp);
00621 }
00622
00623 fprintf(fp, "\n");
00624 }
00625
00626 void poptSetOtherOptionHelp(poptContext con, const char * text)
00627 {
00628 con->otherHelp = _free(con->otherHelp);
00629 con->otherHelp = xstrdup(text);
00630 }