argp-help.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. /* Hierarchical argument parsing help output
  2. Copyright (C) 1995-2005, 2007, 2009-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Written by Miles Bader <miles@gnu.ai.mit.edu>.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef _GNU_SOURCE
  16. # define _GNU_SOURCE 1
  17. #endif
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <alloca.h>
  22. #include <errno.h>
  23. #include <stddef.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <strings.h>
  27. #include <assert.h>
  28. #include <stdarg.h>
  29. #include <ctype.h>
  30. #include <limits.h>
  31. #ifdef USE_IN_LIBIO
  32. # include <wchar.h>
  33. #endif
  34. #ifdef _LIBC
  35. # include <libintl.h>
  36. # undef dgettext
  37. # define dgettext(domain, msgid) \
  38. INTUSE(__dcgettext) (domain, msgid, LC_MESSAGES)
  39. #else
  40. # include "gettext.h"
  41. #endif
  42. #include "argp.h"
  43. #include "argp-fmtstream.h"
  44. #include "argp-namefrob.h"
  45. #include "mbswidth.h"
  46. #ifndef SIZE_MAX
  47. # define SIZE_MAX ((size_t) -1)
  48. #endif
  49. /* User-selectable (using an environment variable) formatting parameters.
  50. These may be specified in an environment variable called 'ARGP_HELP_FMT',
  51. with a contents like: VAR1=VAL1,VAR2=VAL2,BOOLVAR2,no-BOOLVAR2
  52. Where VALn must be a positive integer. The list of variables is in the
  53. UPARAM_NAMES vector, below. */
  54. /* Default parameters. */
  55. #define DUP_ARGS 0 /* True if option argument can be duplicated. */
  56. #define DUP_ARGS_NOTE 1 /* True to print a note about duplicate args. */
  57. #define SHORT_OPT_COL 2 /* column in which short options start */
  58. #define LONG_OPT_COL 6 /* column in which long options start */
  59. #define DOC_OPT_COL 2 /* column in which doc options start */
  60. #define OPT_DOC_COL 29 /* column in which option text starts */
  61. #define HEADER_COL 1 /* column in which group headers are printed */
  62. #define USAGE_INDENT 12 /* indentation of wrapped usage lines */
  63. #define RMARGIN 79 /* right margin used for wrapping */
  64. /* User-selectable (using an environment variable) formatting parameters.
  65. They must all be of type 'int' for the parsing code to work. */
  66. struct uparams
  67. {
  68. /* If true, arguments for an option are shown with both short and long
  69. options, even when a given option has both, e.g. '-x ARG, --longx=ARG'.
  70. If false, then if an option has both, the argument is only shown with
  71. the long one, e.g., '-x, --longx=ARG', and a message indicating that
  72. this really means both is printed below the options. */
  73. int dup_args;
  74. /* This is true if when DUP_ARGS is false, and some duplicate arguments have
  75. been suppressed, an explanatory message should be printed. */
  76. int dup_args_note;
  77. /* Various output columns. */
  78. int short_opt_col; /* column in which short options start */
  79. int long_opt_col; /* column in which long options start */
  80. int doc_opt_col; /* column in which doc options start */
  81. int opt_doc_col; /* column in which option text starts */
  82. int header_col; /* column in which group headers are printed */
  83. int usage_indent; /* indentation of wrapped usage lines */
  84. int rmargin; /* right margin used for wrapping */
  85. int valid; /* True when the values in here are valid. */
  86. };
  87. /* This is a global variable, as user options are only ever read once. */
  88. static struct uparams uparams = {
  89. DUP_ARGS, DUP_ARGS_NOTE,
  90. SHORT_OPT_COL, LONG_OPT_COL, DOC_OPT_COL, OPT_DOC_COL, HEADER_COL,
  91. USAGE_INDENT, RMARGIN,
  92. 0
  93. };
  94. /* A particular uparam, and what the user name is. */
  95. struct uparam_name
  96. {
  97. const char *name; /* User name. */
  98. int is_bool; /* Whether it's 'boolean'. */
  99. size_t uparams_offs; /* Location of the (int) field in UPARAMS. */
  100. };
  101. /* The name-field mappings we know about. */
  102. static const struct uparam_name uparam_names[] =
  103. {
  104. { "dup-args", 1, offsetof (struct uparams, dup_args) },
  105. { "dup-args-note", 1, offsetof (struct uparams, dup_args_note) },
  106. { "short-opt-col", 0, offsetof (struct uparams, short_opt_col) },
  107. { "long-opt-col", 0, offsetof (struct uparams, long_opt_col) },
  108. { "doc-opt-col", 0, offsetof (struct uparams, doc_opt_col) },
  109. { "opt-doc-col", 0, offsetof (struct uparams, opt_doc_col) },
  110. { "header-col", 0, offsetof (struct uparams, header_col) },
  111. { "usage-indent", 0, offsetof (struct uparams, usage_indent) },
  112. { "rmargin", 0, offsetof (struct uparams, rmargin) },
  113. { 0 }
  114. };
  115. static void
  116. validate_uparams (const struct argp_state *state, struct uparams *upptr)
  117. {
  118. const struct uparam_name *up;
  119. for (up = uparam_names; up->name; up++)
  120. {
  121. if (up->is_bool
  122. || up->uparams_offs == offsetof (struct uparams, rmargin))
  123. continue;
  124. if (*(int *)((char *)upptr + up->uparams_offs) >= upptr->rmargin)
  125. {
  126. __argp_failure (state, 0, 0,
  127. dgettext (state->root_argp->argp_domain,
  128. "\
  129. ARGP_HELP_FMT: %s value is less than or equal to %s"),
  130. "rmargin", up->name);
  131. return;
  132. }
  133. }
  134. uparams = *upptr;
  135. uparams.valid = 1;
  136. }
  137. /* Read user options from the environment, and fill in UPARAMS appropriately. */
  138. static void
  139. fill_in_uparams (const struct argp_state *state)
  140. {
  141. const char *var = getenv ("ARGP_HELP_FMT");
  142. struct uparams new_params = uparams;
  143. #define SKIPWS(p) do { while (isspace ((unsigned char) *p)) p++; } while (0);
  144. if (var)
  145. {
  146. /* Parse var. */
  147. while (*var)
  148. {
  149. SKIPWS (var);
  150. if (isalpha ((unsigned char) *var))
  151. {
  152. size_t var_len;
  153. const struct uparam_name *un;
  154. int unspec = 0, val = 0;
  155. const char *arg = var;
  156. while (isalnum ((unsigned char) *arg) || *arg == '-' || *arg == '_')
  157. arg++;
  158. var_len = arg - var;
  159. SKIPWS (arg);
  160. if (*arg == '\0' || *arg == ',')
  161. unspec = 1;
  162. else if (*arg == '=')
  163. {
  164. arg++;
  165. SKIPWS (arg);
  166. }
  167. if (unspec)
  168. {
  169. if (var[0] == 'n' && var[1] == 'o' && var[2] == '-')
  170. {
  171. val = 0;
  172. var += 3;
  173. var_len -= 3;
  174. }
  175. else
  176. val = 1;
  177. }
  178. else if (isdigit ((unsigned char) *arg))
  179. {
  180. val = atoi (arg);
  181. while (isdigit ((unsigned char) *arg))
  182. arg++;
  183. SKIPWS (arg);
  184. }
  185. for (un = uparam_names; un->name; un++)
  186. if (strlen (un->name) == var_len
  187. && strncmp (var, un->name, var_len) == 0)
  188. {
  189. if (unspec && !un->is_bool)
  190. __argp_failure (state, 0, 0,
  191. dgettext (state->root_argp->argp_domain,
  192. "\
  193. %.*s: ARGP_HELP_FMT parameter requires a value"),
  194. (int) var_len, var);
  195. else if (val < 0)
  196. __argp_failure (state, 0, 0,
  197. dgettext (state->root_argp->argp_domain,
  198. "\
  199. %.*s: ARGP_HELP_FMT parameter must be positive"),
  200. (int) var_len, var);
  201. else
  202. *(int *)((char *)&new_params + un->uparams_offs) = val;
  203. break;
  204. }
  205. if (! un->name)
  206. __argp_failure (state, 0, 0,
  207. dgettext (state->root_argp->argp_domain, "\
  208. %.*s: Unknown ARGP_HELP_FMT parameter"),
  209. (int) var_len, var);
  210. var = arg;
  211. if (*var == ',')
  212. var++;
  213. }
  214. else if (*var)
  215. {
  216. __argp_failure (state, 0, 0,
  217. dgettext (state->root_argp->argp_domain,
  218. "Garbage in ARGP_HELP_FMT: %s"), var);
  219. break;
  220. }
  221. }
  222. validate_uparams (state, &new_params);
  223. }
  224. }
  225. /* Returns true if OPT hasn't been marked invisible. Visibility only affects
  226. whether OPT is displayed or used in sorting, not option shadowing. */
  227. #define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN))
  228. /* Returns true if OPT is an alias for an earlier option. */
  229. #define oalias(opt) ((opt)->flags & OPTION_ALIAS)
  230. /* Returns true if OPT is a documentation-only entry. */
  231. #define odoc(opt) ((opt)->flags & OPTION_DOC)
  232. /* Returns true if OPT should not be translated */
  233. #define onotrans(opt) ((opt)->flags & OPTION_NO_TRANS)
  234. /* Returns true if OPT is the end-of-list marker for a list of options. */
  235. #define oend(opt) __option_is_end (opt)
  236. /* Returns true if OPT has a short option. */
  237. #define oshort(opt) __option_is_short (opt)
  238. /*
  239. The help format for a particular option is like:
  240. -xARG, -yARG, --long1=ARG, --long2=ARG Documentation...
  241. Where ARG will be omitted if there's no argument, for this option, or
  242. will be surrounded by "[" and "]" appropriately if the argument is
  243. optional. The documentation string is word-wrapped appropriately, and if
  244. the list of options is long enough, it will be started on a separate line.
  245. If there are no short options for a given option, the first long option is
  246. indented slightly in a way that's supposed to make most long options appear
  247. to be in a separate column.
  248. For example, the following output (from ps):
  249. -p PID, --pid=PID List the process PID
  250. --pgrp=PGRP List processes in the process group PGRP
  251. -P, -x, --no-parent Include processes without parents
  252. -Q, --all-fields Don't elide unusable fields (normally if there's
  253. some reason ps can't print a field for any
  254. process, it's removed from the output entirely)
  255. -r, --reverse, --gratuitously-long-reverse-option
  256. Reverse the order of any sort
  257. --session[=SID] Add the processes from the session SID (which
  258. defaults to the sid of the current process)
  259. Here are some more options:
  260. -f ZOT, --foonly=ZOT Glork a foonly
  261. -z, --zaza Snit a zar
  262. -?, --help Give this help list
  263. --usage Give a short usage message
  264. -V, --version Print program version
  265. The struct argp_option array for the above could look like:
  266. {
  267. {"pid", 'p', "PID", 0, "List the process PID"},
  268. {"pgrp", OPT_PGRP, "PGRP", 0, "List processes in the process group PGRP"},
  269. {"no-parent", 'P', 0, 0, "Include processes without parents"},
  270. {0, 'x', 0, OPTION_ALIAS},
  271. {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
  272. " if there's some reason ps can't"
  273. " print a field for any process, it's"
  274. " removed from the output entirely)" },
  275. {"reverse", 'r', 0, 0, "Reverse the order of any sort"},
  276. {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
  277. {"session", OPT_SESS, "SID", OPTION_ARG_OPTIONAL,
  278. "Add the processes from the session"
  279. " SID (which defaults to the sid of"
  280. " the current process)" },
  281. {0,0,0,0, "Here are some more options:"},
  282. {"foonly", 'f', "ZOT", 0, "Glork a foonly"},
  283. {"zaza", 'z', 0, 0, "Snit a zar"},
  284. {0}
  285. }
  286. Note that the last three options are automatically supplied by argp_parse,
  287. unless you tell it not to with ARGP_NO_HELP.
  288. */
  289. /* Returns true if CH occurs between BEG and END. */
  290. static int
  291. find_char (char ch, char *beg, char *end)
  292. {
  293. while (beg < end)
  294. if (*beg == ch)
  295. return 1;
  296. else
  297. beg++;
  298. return 0;
  299. }
  300. struct hol_cluster; /* fwd decl */
  301. struct hol_entry
  302. {
  303. /* First option. */
  304. const struct argp_option *opt;
  305. /* Number of options (including aliases). */
  306. unsigned num;
  307. /* A pointers into the HOL's short_options field, to the first short option
  308. letter for this entry. The order of the characters following this point
  309. corresponds to the order of options pointed to by OPT, and there are at
  310. most NUM. A short option recorded in an option following OPT is only
  311. valid if it occurs in the right place in SHORT_OPTIONS (otherwise it's
  312. probably been shadowed by some other entry). */
  313. char *short_options;
  314. /* Entries are sorted by their group first, in the order:
  315. 1, 2, ..., n, 0, -m, ..., -2, -1
  316. and then alphabetically within each group. The default is 0. */
  317. int group;
  318. /* The cluster of options this entry belongs to, or 0 if none. */
  319. struct hol_cluster *cluster;
  320. /* The argp from which this option came. */
  321. const struct argp *argp;
  322. /* Position in the array */
  323. unsigned ord;
  324. };
  325. /* A cluster of entries to reflect the argp tree structure. */
  326. struct hol_cluster
  327. {
  328. /* A descriptive header printed before options in this cluster. */
  329. const char *header;
  330. /* Used to order clusters within the same group with the same parent,
  331. according to the order in which they occurred in the parent argp's child
  332. list. */
  333. int index;
  334. /* How to sort this cluster with respect to options and other clusters at the
  335. same depth (clusters always follow options in the same group). */
  336. int group;
  337. /* The cluster to which this cluster belongs, or 0 if it's at the base
  338. level. */
  339. struct hol_cluster *parent;
  340. /* The argp from which this cluster is (eventually) derived. */
  341. const struct argp *argp;
  342. /* The distance this cluster is from the root. */
  343. int depth;
  344. /* Clusters in a given hol are kept in a linked list, to make freeing them
  345. possible. */
  346. struct hol_cluster *next;
  347. };
  348. /* A list of options for help. */
  349. struct hol
  350. {
  351. /* An array of hol_entry's. */
  352. struct hol_entry *entries;
  353. /* The number of entries in this hol. If this field is zero, the others
  354. are undefined. */
  355. unsigned num_entries;
  356. /* A string containing all short options in this HOL. Each entry contains
  357. pointers into this string, so the order can't be messed with blindly. */
  358. char *short_options;
  359. /* Clusters of entries in this hol. */
  360. struct hol_cluster *clusters;
  361. };
  362. /* Create a struct hol from the options in ARGP. CLUSTER is the
  363. hol_cluster in which these entries occur, or 0, if at the root. */
  364. static struct hol *
  365. make_hol (const struct argp *argp, struct hol_cluster *cluster)
  366. {
  367. char *so;
  368. const struct argp_option *o;
  369. const struct argp_option *opts = argp->options;
  370. struct hol_entry *entry;
  371. unsigned num_short_options = 0;
  372. struct hol *hol = malloc (sizeof (struct hol));
  373. assert (hol);
  374. hol->num_entries = 0;
  375. hol->clusters = 0;
  376. if (opts)
  377. {
  378. int cur_group = 0;
  379. /* The first option must not be an alias. */
  380. assert (! oalias (opts));
  381. /* Calculate the space needed. */
  382. for (o = opts; ! oend (o); o++)
  383. {
  384. if (! oalias (o))
  385. hol->num_entries++;
  386. if (oshort (o))
  387. num_short_options++; /* This is an upper bound. */
  388. }
  389. hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries);
  390. hol->short_options = malloc (num_short_options + 1);
  391. assert (hol->entries && hol->short_options);
  392. if (SIZE_MAX <= UINT_MAX)
  393. assert (hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry));
  394. /* Fill in the entries. */
  395. so = hol->short_options;
  396. for (o = opts, entry = hol->entries; ! oend (o); entry++)
  397. {
  398. entry->opt = o;
  399. entry->num = 0;
  400. entry->short_options = so;
  401. entry->group = cur_group =
  402. o->group
  403. ? o->group
  404. : ((!o->name && !o->key)
  405. ? cur_group + 1
  406. : cur_group);
  407. entry->cluster = cluster;
  408. entry->argp = argp;
  409. do
  410. {
  411. entry->num++;
  412. if (oshort (o) && ! find_char (o->key, hol->short_options, so))
  413. /* O has a valid short option which hasn't already been used.*/
  414. *so++ = o->key;
  415. o++;
  416. }
  417. while (! oend (o) && oalias (o));
  418. }
  419. *so = '\0'; /* null terminated so we can find the length */
  420. }
  421. return hol;
  422. }
  423. /* Add a new cluster to HOL, with the given GROUP and HEADER (taken from the
  424. associated argp child list entry), INDEX, and PARENT, and return a pointer
  425. to it. ARGP is the argp that this cluster results from. */
  426. static struct hol_cluster *
  427. hol_add_cluster (struct hol *hol, int group, const char *header, int index,
  428. struct hol_cluster *parent, const struct argp *argp)
  429. {
  430. struct hol_cluster *cl = malloc (sizeof (struct hol_cluster));
  431. if (cl)
  432. {
  433. cl->group = group;
  434. cl->header = header;
  435. cl->index = index;
  436. cl->parent = parent;
  437. cl->argp = argp;
  438. cl->depth = parent ? parent->depth + 1 : 0;
  439. cl->next = hol->clusters;
  440. hol->clusters = cl;
  441. }
  442. return cl;
  443. }
  444. /* Free HOL and any resources it uses. */
  445. static void
  446. hol_free (struct hol *hol)
  447. {
  448. struct hol_cluster *cl = hol->clusters;
  449. while (cl)
  450. {
  451. struct hol_cluster *next = cl->next;
  452. free (cl);
  453. cl = next;
  454. }
  455. if (hol->num_entries > 0)
  456. {
  457. free (hol->entries);
  458. free (hol->short_options);
  459. }
  460. free (hol);
  461. }
  462. static int
  463. hol_entry_short_iterate (const struct hol_entry *entry,
  464. int (*func)(const struct argp_option *opt,
  465. const struct argp_option *real,
  466. const char *domain, void *cookie),
  467. const char *domain, void *cookie)
  468. {
  469. unsigned nopts;
  470. int val = 0;
  471. const struct argp_option *opt, *real = entry->opt;
  472. char *so = entry->short_options;
  473. for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
  474. if (oshort (opt) && *so == opt->key)
  475. {
  476. if (!oalias (opt))
  477. real = opt;
  478. if (ovisible (opt))
  479. val = (*func)(opt, real, domain, cookie);
  480. so++;
  481. }
  482. return val;
  483. }
  484. static inline int
  485. #if __GNUC__ >= 3
  486. __attribute__ ((always_inline))
  487. #endif
  488. hol_entry_long_iterate (const struct hol_entry *entry,
  489. int (*func)(const struct argp_option *opt,
  490. const struct argp_option *real,
  491. const char *domain, void *cookie),
  492. const char *domain, void *cookie)
  493. {
  494. unsigned nopts;
  495. int val = 0;
  496. const struct argp_option *opt, *real = entry->opt;
  497. for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
  498. if (opt->name)
  499. {
  500. if (!oalias (opt))
  501. real = opt;
  502. if (ovisible (opt))
  503. val = (*func)(opt, real, domain, cookie);
  504. }
  505. return val;
  506. }
  507. /* Iterator that returns true for the first short option. */
  508. static int
  509. until_short (const struct argp_option *opt, const struct argp_option *real,
  510. const char *domain, void *cookie)
  511. {
  512. return oshort (opt) ? opt->key : 0;
  513. }
  514. /* Returns the first valid short option in ENTRY, or 0 if there is none. */
  515. static char
  516. hol_entry_first_short (const struct hol_entry *entry)
  517. {
  518. return hol_entry_short_iterate (entry, until_short,
  519. entry->argp->argp_domain, 0);
  520. }
  521. /* Returns the first valid long option in ENTRY, or 0 if there is none. */
  522. static const char *
  523. hol_entry_first_long (const struct hol_entry *entry)
  524. {
  525. const struct argp_option *opt;
  526. unsigned num;
  527. for (opt = entry->opt, num = entry->num; num > 0; opt++, num--)
  528. if (opt->name && ovisible (opt))
  529. return opt->name;
  530. return 0;
  531. }
  532. /* Returns the entry in HOL with the long option name NAME, or 0 if there is
  533. none. */
  534. static struct hol_entry *
  535. hol_find_entry (struct hol *hol, const char *name)
  536. {
  537. struct hol_entry *entry = hol->entries;
  538. unsigned num_entries = hol->num_entries;
  539. while (num_entries-- > 0)
  540. {
  541. const struct argp_option *opt = entry->opt;
  542. unsigned num_opts = entry->num;
  543. while (num_opts-- > 0)
  544. if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0)
  545. return entry;
  546. else
  547. opt++;
  548. entry++;
  549. }
  550. return 0;
  551. }
  552. /* If an entry with the long option NAME occurs in HOL, set its special
  553. sort position to GROUP. */
  554. static void
  555. hol_set_group (struct hol *hol, const char *name, int group)
  556. {
  557. struct hol_entry *entry = hol_find_entry (hol, name);
  558. if (entry)
  559. entry->group = group;
  560. }
  561. /* Order by group: 0, 1, 2, ..., n, -m, ..., -2, -1.
  562. EQ is what to return if GROUP1 and GROUP2 are the same. */
  563. static int
  564. group_cmp (int group1, int group2, int eq)
  565. {
  566. if (group1 == group2)
  567. return eq;
  568. else if ((group1 < 0 && group2 < 0) || (group1 >= 0 && group2 >= 0))
  569. return group1 - group2;
  570. else
  571. return group2 - group1;
  572. }
  573. /* Compare clusters CL1 & CL2 by the order that they should appear in
  574. output. */
  575. static int
  576. hol_cluster_cmp (const struct hol_cluster *cl1, const struct hol_cluster *cl2)
  577. {
  578. /* If one cluster is deeper than the other, use its ancestor at the same
  579. level, so that finding the common ancestor is straightforward.
  580. clN->depth > 0 means that clN->parent != NULL (see hol_add_cluster) */
  581. while (cl1->depth > cl2->depth)
  582. cl1 = cl1->parent;
  583. while (cl2->depth > cl1->depth)
  584. cl2 = cl2->parent;
  585. /* Now reduce both clusters to their ancestors at the point where both have
  586. a common parent; these can be directly compared. */
  587. while (cl1->parent != cl2->parent)
  588. cl1 = cl1->parent, cl2 = cl2->parent;
  589. return group_cmp (cl1->group, cl2->group, cl2->index - cl1->index);
  590. }
  591. /* Return the ancestor of CL that's just below the root (i.e., has a parent
  592. of 0). */
  593. static struct hol_cluster *
  594. hol_cluster_base (struct hol_cluster *cl)
  595. {
  596. while (cl->parent)
  597. cl = cl->parent;
  598. return cl;
  599. }
  600. /* Return true if CL1 is a child of CL2. */
  601. static int
  602. hol_cluster_is_child (const struct hol_cluster *cl1,
  603. const struct hol_cluster *cl2)
  604. {
  605. while (cl1 && cl1 != cl2)
  606. cl1 = cl1->parent;
  607. return cl1 == cl2;
  608. }
  609. /* Given the name of an OPTION_DOC option, modifies NAME to start at the tail
  610. that should be used for comparisons, and returns true iff it should be
  611. treated as a non-option. */
  612. static int
  613. canon_doc_option (const char **name)
  614. {
  615. int non_opt;
  616. if (!*name)
  617. non_opt = 1;
  618. else
  619. {
  620. /* Skip initial whitespace. */
  621. while (isspace ((unsigned char) **name))
  622. (*name)++;
  623. /* Decide whether this looks like an option (leading '-') or not. */
  624. non_opt = (**name != '-');
  625. /* Skip until part of name used for sorting. */
  626. while (**name && !isalnum ((unsigned char) **name))
  627. (*name)++;
  628. }
  629. return non_opt;
  630. }
  631. #define HOL_ENTRY_PTRCMP(a,b) ((a)->ord < (b)->ord ? -1 : 1)
  632. /* Order ENTRY1 & ENTRY2 by the order which they should appear in a help
  633. listing. */
  634. static int
  635. hol_entry_cmp (const struct hol_entry *entry1,
  636. const struct hol_entry *entry2)
  637. {
  638. /* The group numbers by which the entries should be ordered; if either is
  639. in a cluster, then this is just the group within the cluster. */
  640. int group1 = entry1->group, group2 = entry2->group;
  641. int rc;
  642. if (entry1->cluster != entry2->cluster)
  643. {
  644. /* The entries are not within the same cluster, so we can't compare them
  645. directly, we have to use the appropriate clustering level too. */
  646. if (! entry1->cluster)
  647. /* ENTRY1 is at the "base level", not in a cluster, so we have to
  648. compare it's group number with that of the base cluster in which
  649. ENTRY2 resides. Note that if they're in the same group, the
  650. clustered option always comes laster. */
  651. return group_cmp (group1, hol_cluster_base (entry2->cluster)->group, -1);
  652. else if (! entry2->cluster)
  653. /* Likewise, but ENTRY2's not in a cluster. */
  654. return group_cmp (hol_cluster_base (entry1->cluster)->group, group2, 1);
  655. else
  656. /* Both entries are in clusters, we can just compare the clusters. */
  657. return (rc = hol_cluster_cmp (entry1->cluster, entry2->cluster)) ?
  658. rc : HOL_ENTRY_PTRCMP (entry1, entry2);
  659. }
  660. else if (group1 == group2)
  661. /* The entries are both in the same cluster and group, so compare them
  662. alphabetically. */
  663. {
  664. int short1 = hol_entry_first_short (entry1);
  665. int short2 = hol_entry_first_short (entry2);
  666. int doc1 = odoc (entry1->opt);
  667. int doc2 = odoc (entry2->opt);
  668. const char *long1 = hol_entry_first_long (entry1);
  669. const char *long2 = hol_entry_first_long (entry2);
  670. if (doc1)
  671. doc1 = canon_doc_option (&long1);
  672. if (doc2)
  673. doc2 = canon_doc_option (&long2);
  674. if (doc1 != doc2)
  675. /* "documentation" options always follow normal options (or
  676. documentation options that *look* like normal options). */
  677. return doc1 - doc2;
  678. else if (!short1 && !short2 && long1 && long2)
  679. /* Only long options. */
  680. return (rc = __strcasecmp (long1, long2)) ?
  681. rc : HOL_ENTRY_PTRCMP (entry1, entry2);
  682. else
  683. /* Compare short/short, long/short, short/long, using the first
  684. character of long options. Entries without *any* valid
  685. options (such as options with OPTION_HIDDEN set) will be put
  686. first, but as they're not displayed, it doesn't matter where
  687. they are. */
  688. {
  689. unsigned char first1 = short1 ? short1 : long1 ? *long1 : 0;
  690. unsigned char first2 = short2 ? short2 : long2 ? *long2 : 0;
  691. /* Use tolower, not _tolower, since only the former is
  692. guaranteed to work on something already lower case. */
  693. int lower_cmp = tolower (first1) - tolower (first2);
  694. /* Compare ignoring case, except when the options are both the
  695. same letter, in which case lower-case always comes first. */
  696. return lower_cmp ? lower_cmp :
  697. (rc = first2 - first1) ?
  698. rc : HOL_ENTRY_PTRCMP (entry1, entry2);
  699. }
  700. }
  701. else
  702. /* Within the same cluster, but not the same group, so just compare
  703. groups. */
  704. return group_cmp (group1, group2, HOL_ENTRY_PTRCMP (entry1, entry2));
  705. }
  706. /* Version of hol_entry_cmp with correct signature for qsort. */
  707. static int
  708. hol_entry_qcmp (const void *entry1_v, const void *entry2_v)
  709. {
  710. return hol_entry_cmp (entry1_v, entry2_v);
  711. }
  712. /* Sort HOL by group and alphabetically by option name (with short options
  713. taking precedence over long). Since the sorting is for display purposes
  714. only, the shadowing of options isn't effected. */
  715. static void
  716. hol_sort (struct hol *hol)
  717. {
  718. if (hol->num_entries > 0)
  719. {
  720. unsigned i;
  721. struct hol_entry *e;
  722. for (i = 0, e = hol->entries; i < hol->num_entries; i++, e++)
  723. e->ord = i;
  724. qsort (hol->entries, hol->num_entries, sizeof (struct hol_entry),
  725. hol_entry_qcmp);
  726. }
  727. }
  728. /* Append MORE to HOL, destroying MORE in the process. Options in HOL shadow
  729. any in MORE with the same name. */
  730. static void
  731. hol_append (struct hol *hol, struct hol *more)
  732. {
  733. struct hol_cluster **cl_end = &hol->clusters;
  734. /* Steal MORE's cluster list, and add it to the end of HOL's. */
  735. while (*cl_end)
  736. cl_end = &(*cl_end)->next;
  737. *cl_end = more->clusters;
  738. more->clusters = 0;
  739. /* Merge entries. */
  740. if (more->num_entries > 0)
  741. {
  742. if (hol->num_entries == 0)
  743. {
  744. hol->num_entries = more->num_entries;
  745. hol->entries = more->entries;
  746. hol->short_options = more->short_options;
  747. more->num_entries = 0; /* Mark MORE's fields as invalid. */
  748. }
  749. else
  750. /* Append the entries in MORE to those in HOL, taking care to only add
  751. non-shadowed SHORT_OPTIONS values. */
  752. {
  753. unsigned left;
  754. char *so, *more_so;
  755. struct hol_entry *e;
  756. unsigned num_entries = hol->num_entries + more->num_entries;
  757. struct hol_entry *entries =
  758. malloc (num_entries * sizeof (struct hol_entry));
  759. unsigned hol_so_len = strlen (hol->short_options);
  760. char *short_options =
  761. malloc (hol_so_len + strlen (more->short_options) + 1);
  762. assert (entries && short_options);
  763. if (SIZE_MAX <= UINT_MAX)
  764. assert (num_entries <= SIZE_MAX / sizeof (struct hol_entry));
  765. __mempcpy (__mempcpy (entries, hol->entries,
  766. hol->num_entries * sizeof (struct hol_entry)),
  767. more->entries,
  768. more->num_entries * sizeof (struct hol_entry));
  769. __mempcpy (short_options, hol->short_options, hol_so_len);
  770. /* Fix up the short options pointers from HOL. */
  771. for (e = entries, left = hol->num_entries; left > 0; e++, left--)
  772. e->short_options =
  773. short_options + (e->short_options - hol->short_options);
  774. /* Now add the short options from MORE, fixing up its entries
  775. too. */
  776. so = short_options + hol_so_len;
  777. more_so = more->short_options;
  778. for (left = more->num_entries; left > 0; e++, left--)
  779. {
  780. int opts_left;
  781. const struct argp_option *opt;
  782. e->short_options = so;
  783. for (opts_left = e->num, opt = e->opt; opts_left; opt++, opts_left--)
  784. {
  785. int ch = *more_so;
  786. if (oshort (opt) && ch == opt->key)
  787. /* The next short option in MORE_SO, CH, is from OPT. */
  788. {
  789. if (! find_char (ch, short_options,
  790. short_options + hol_so_len))
  791. /* The short option CH isn't shadowed by HOL's options,
  792. so add it to the sum. */
  793. *so++ = ch;
  794. more_so++;
  795. }
  796. }
  797. }
  798. *so = '\0';
  799. free (hol->entries);
  800. free (hol->short_options);
  801. hol->entries = entries;
  802. hol->num_entries = num_entries;
  803. hol->short_options = short_options;
  804. }
  805. }
  806. hol_free (more);
  807. }
  808. /* Inserts enough spaces to make sure STREAM is at column COL. */
  809. static void
  810. indent_to (argp_fmtstream_t stream, unsigned col)
  811. {
  812. int needed = col - __argp_fmtstream_point (stream);
  813. while (needed-- > 0)
  814. __argp_fmtstream_putc (stream, ' ');
  815. }
  816. /* Output to STREAM either a space, or a newline if there isn't room for at
  817. least ENSURE characters before the right margin. */
  818. static void
  819. space (argp_fmtstream_t stream, size_t ensure)
  820. {
  821. if (__argp_fmtstream_point (stream) + ensure
  822. >= __argp_fmtstream_rmargin (stream))
  823. __argp_fmtstream_putc (stream, '\n');
  824. else
  825. __argp_fmtstream_putc (stream, ' ');
  826. }
  827. /* If the option REAL has an argument, we print it in using the printf
  828. format REQ_FMT or OPT_FMT depending on whether it's a required or
  829. optional argument. */
  830. static void
  831. arg (const struct argp_option *real, const char *req_fmt, const char *opt_fmt,
  832. const char *domain, argp_fmtstream_t stream)
  833. {
  834. if (real->arg)
  835. {
  836. if (real->flags & OPTION_ARG_OPTIONAL)
  837. __argp_fmtstream_printf (stream, opt_fmt,
  838. dgettext (domain, real->arg));
  839. else
  840. __argp_fmtstream_printf (stream, req_fmt,
  841. dgettext (domain, real->arg));
  842. }
  843. }
  844. /* Helper functions for hol_entry_help. */
  845. /* State used during the execution of hol_help. */
  846. struct hol_help_state
  847. {
  848. /* PREV_ENTRY should contain the previous entry printed, or 0. */
  849. struct hol_entry *prev_entry;
  850. /* If an entry is in a different group from the previous one, and SEP_GROUPS
  851. is true, then a blank line will be printed before any output. */
  852. int sep_groups;
  853. /* True if a duplicate option argument was suppressed (only ever set if
  854. UPARAMS.dup_args is false). */
  855. int suppressed_dup_arg;
  856. };
  857. /* Some state used while printing a help entry (used to communicate with
  858. helper functions). See the doc for hol_entry_help for more info, as most
  859. of the fields are copied from its arguments. */
  860. struct pentry_state
  861. {
  862. const struct hol_entry *entry;
  863. argp_fmtstream_t stream;
  864. struct hol_help_state *hhstate;
  865. /* True if nothing's been printed so far. */
  866. int first;
  867. /* If non-zero, the state that was used to print this help. */
  868. const struct argp_state *state;
  869. };
  870. /* If a user doc filter should be applied to DOC, do so. */
  871. static const char *
  872. filter_doc (const char *doc, int key, const struct argp *argp,
  873. const struct argp_state *state)
  874. {
  875. if (argp->help_filter)
  876. /* We must apply a user filter to this output. */
  877. {
  878. void *input = __argp_input (argp, state);
  879. return (*argp->help_filter) (key, doc, input);
  880. }
  881. else
  882. /* No filter. */
  883. return doc;
  884. }
  885. /* Prints STR as a header line, with the margin lines set appropriately, and
  886. notes the fact that groups should be separated with a blank line. ARGP is
  887. the argp that should dictate any user doc filtering to take place. Note
  888. that the previous wrap margin isn't restored, but the left margin is reset
  889. to 0. */
  890. static void
  891. print_header (const char *str, const struct argp *argp,
  892. struct pentry_state *pest)
  893. {
  894. const char *tstr = dgettext (argp->argp_domain, str);
  895. const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_HEADER, argp, pest->state);
  896. if (fstr)
  897. {
  898. if (*fstr)
  899. {
  900. if (pest->hhstate->prev_entry)
  901. /* Precede with a blank line. */
  902. __argp_fmtstream_putc (pest->stream, '\n');
  903. indent_to (pest->stream, uparams.header_col);
  904. __argp_fmtstream_set_lmargin (pest->stream, uparams.header_col);
  905. __argp_fmtstream_set_wmargin (pest->stream, uparams.header_col);
  906. __argp_fmtstream_puts (pest->stream, fstr);
  907. __argp_fmtstream_set_lmargin (pest->stream, 0);
  908. __argp_fmtstream_putc (pest->stream, '\n');
  909. }
  910. pest->hhstate->sep_groups = 1; /* Separate subsequent groups. */
  911. }
  912. if (fstr != tstr)
  913. free ((char *) fstr);
  914. }
  915. /* Inserts a comma if this isn't the first item on the line, and then makes
  916. sure we're at least to column COL. If this *is* the first item on a line,
  917. prints any pending whitespace/headers that should precede this line. Also
  918. clears FIRST. */
  919. static void
  920. comma (unsigned col, struct pentry_state *pest)
  921. {
  922. if (pest->first)
  923. {
  924. const struct hol_entry *pe = pest->hhstate->prev_entry;
  925. const struct hol_cluster *cl = pest->entry->cluster;
  926. if (pest->hhstate->sep_groups && pe && pest->entry->group != pe->group)
  927. __argp_fmtstream_putc (pest->stream, '\n');
  928. if (cl && cl->header && *cl->header
  929. && (!pe
  930. || (pe->cluster != cl
  931. && !hol_cluster_is_child (pe->cluster, cl))))
  932. /* If we're changing clusters, then this must be the start of the
  933. ENTRY's cluster unless that is an ancestor of the previous one
  934. (in which case we had just popped into a sub-cluster for a bit).
  935. If so, then print the cluster's header line. */
  936. {
  937. int old_wm = __argp_fmtstream_wmargin (pest->stream);
  938. print_header (cl->header, cl->argp, pest);
  939. __argp_fmtstream_set_wmargin (pest->stream, old_wm);
  940. }
  941. pest->first = 0;
  942. }
  943. else
  944. __argp_fmtstream_puts (pest->stream, ", ");
  945. indent_to (pest->stream, col);
  946. }
  947. /* Print help for ENTRY to STREAM. */
  948. static void
  949. hol_entry_help (struct hol_entry *entry, const struct argp_state *state,
  950. argp_fmtstream_t stream, struct hol_help_state *hhstate)
  951. {
  952. unsigned num;
  953. const struct argp_option *real = entry->opt, *opt;
  954. char *so = entry->short_options;
  955. int have_long_opt = 0; /* We have any long options. */
  956. /* Saved margins. */
  957. int old_lm = __argp_fmtstream_set_lmargin (stream, 0);
  958. int old_wm = __argp_fmtstream_wmargin (stream);
  959. /* PEST is a state block holding some of our variables that we'd like to
  960. share with helper functions. */
  961. struct pentry_state pest;
  962. pest.entry = entry;
  963. pest.stream = stream;
  964. pest.hhstate = hhstate;
  965. pest.first = 1;
  966. pest.state = state;
  967. if (! odoc (real))
  968. for (opt = real, num = entry->num; num > 0; opt++, num--)
  969. if (opt->name && ovisible (opt))
  970. {
  971. have_long_opt = 1;
  972. break;
  973. }
  974. /* First emit short options. */
  975. __argp_fmtstream_set_wmargin (stream, uparams.short_opt_col); /* For truly bizarre cases. */
  976. for (opt = real, num = entry->num; num > 0; opt++, num--)
  977. if (oshort (opt) && opt->key == *so)
  978. /* OPT has a valid (non shadowed) short option. */
  979. {
  980. if (ovisible (opt))
  981. {
  982. comma (uparams.short_opt_col, &pest);
  983. __argp_fmtstream_putc (stream, '-');
  984. __argp_fmtstream_putc (stream, *so);
  985. if (!have_long_opt || uparams.dup_args)
  986. arg (real, " %s", "[%s]", state->root_argp->argp_domain, stream);
  987. else if (real->arg)
  988. hhstate->suppressed_dup_arg = 1;
  989. }
  990. so++;
  991. }
  992. /* Now, long options. */
  993. if (odoc (real))
  994. /* A "documentation" option. */
  995. {
  996. __argp_fmtstream_set_wmargin (stream, uparams.doc_opt_col);
  997. for (opt = real, num = entry->num; num > 0; opt++, num--)
  998. if (opt->name && *opt->name && ovisible (opt))
  999. {
  1000. comma (uparams.doc_opt_col, &pest);
  1001. /* Calling dgettext here isn't quite right, since sorting will
  1002. have been done on the original; but documentation options
  1003. should be pretty rare anyway... */
  1004. __argp_fmtstream_puts (stream,
  1005. onotrans (opt) ?
  1006. opt->name :
  1007. dgettext (state->root_argp->argp_domain,
  1008. opt->name));
  1009. }
  1010. }
  1011. else
  1012. /* A real long option. */
  1013. {
  1014. int first_long_opt = 1;
  1015. __argp_fmtstream_set_wmargin (stream, uparams.long_opt_col);
  1016. for (opt = real, num = entry->num; num > 0; opt++, num--)
  1017. if (opt->name && ovisible (opt))
  1018. {
  1019. comma (uparams.long_opt_col, &pest);
  1020. __argp_fmtstream_printf (stream, "--%s", opt->name);
  1021. if (first_long_opt || uparams.dup_args)
  1022. arg (real, "=%s", "[=%s]", state->root_argp->argp_domain,
  1023. stream);
  1024. else if (real->arg)
  1025. hhstate->suppressed_dup_arg = 1;
  1026. }
  1027. }
  1028. /* Next, documentation strings. */
  1029. __argp_fmtstream_set_lmargin (stream, 0);
  1030. if (pest.first)
  1031. {
  1032. /* Didn't print any switches, what's up? */
  1033. if (!oshort (real) && !real->name)
  1034. /* This is a group header, print it nicely. */
  1035. print_header (real->doc, entry->argp, &pest);
  1036. else
  1037. /* Just a totally shadowed option or null header; print nothing. */
  1038. goto cleanup; /* Just return, after cleaning up. */
  1039. }
  1040. else
  1041. {
  1042. const char *tstr = real->doc ? dgettext (state->root_argp->argp_domain,
  1043. real->doc) : 0;
  1044. const char *fstr = filter_doc (tstr, real->key, entry->argp, state);
  1045. if (fstr && *fstr)
  1046. {
  1047. unsigned int col = __argp_fmtstream_point (stream);
  1048. __argp_fmtstream_set_lmargin (stream, uparams.opt_doc_col);
  1049. __argp_fmtstream_set_wmargin (stream, uparams.opt_doc_col);
  1050. if (col > (unsigned int) (uparams.opt_doc_col + 3))
  1051. __argp_fmtstream_putc (stream, '\n');
  1052. else if (col >= (unsigned int) uparams.opt_doc_col)
  1053. __argp_fmtstream_puts (stream, " ");
  1054. else
  1055. indent_to (stream, uparams.opt_doc_col);
  1056. __argp_fmtstream_puts (stream, fstr);
  1057. }
  1058. if (fstr && fstr != tstr)
  1059. free ((char *) fstr);
  1060. /* Reset the left margin. */
  1061. __argp_fmtstream_set_lmargin (stream, 0);
  1062. __argp_fmtstream_putc (stream, '\n');
  1063. }
  1064. hhstate->prev_entry = entry;
  1065. cleanup:
  1066. __argp_fmtstream_set_lmargin (stream, old_lm);
  1067. __argp_fmtstream_set_wmargin (stream, old_wm);
  1068. }
  1069. /* Output a long help message about the options in HOL to STREAM. */
  1070. static void
  1071. hol_help (struct hol *hol, const struct argp_state *state,
  1072. argp_fmtstream_t stream)
  1073. {
  1074. unsigned num;
  1075. struct hol_entry *entry;
  1076. struct hol_help_state hhstate = { 0, 0, 0 };
  1077. for (entry = hol->entries, num = hol->num_entries; num > 0; entry++, num--)
  1078. hol_entry_help (entry, state, stream, &hhstate);
  1079. if (hhstate.suppressed_dup_arg && uparams.dup_args_note)
  1080. {
  1081. const char *tstr = dgettext (state->root_argp->argp_domain, "\
  1082. Mandatory or optional arguments to long options are also mandatory or \
  1083. optional for any corresponding short options.");
  1084. const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_DUP_ARGS_NOTE,
  1085. state ? state->root_argp : 0, state);
  1086. if (fstr && *fstr)
  1087. {
  1088. __argp_fmtstream_putc (stream, '\n');
  1089. __argp_fmtstream_puts (stream, fstr);
  1090. __argp_fmtstream_putc (stream, '\n');
  1091. }
  1092. if (fstr && fstr != tstr)
  1093. free ((char *) fstr);
  1094. }
  1095. }
  1096. /* Helper functions for hol_usage. */
  1097. /* If OPT is a short option without an arg, append its key to the string
  1098. pointer pointer to by COOKIE, and advance the pointer. */
  1099. static int
  1100. add_argless_short_opt (const struct argp_option *opt,
  1101. const struct argp_option *real,
  1102. const char *domain, void *cookie)
  1103. {
  1104. char **snao_end = cookie;
  1105. if (!(opt->arg || real->arg)
  1106. && !((opt->flags | real->flags) & OPTION_NO_USAGE))
  1107. *(*snao_end)++ = opt->key;
  1108. return 0;
  1109. }
  1110. /* If OPT is a short option with an arg, output a usage entry for it to the
  1111. stream pointed at by COOKIE. */
  1112. static int
  1113. usage_argful_short_opt (const struct argp_option *opt,
  1114. const struct argp_option *real,
  1115. const char *domain, void *cookie)
  1116. {
  1117. argp_fmtstream_t stream = cookie;
  1118. const char *arg = opt->arg;
  1119. int flags = opt->flags | real->flags;
  1120. if (! arg)
  1121. arg = real->arg;
  1122. if (arg && !(flags & OPTION_NO_USAGE))
  1123. {
  1124. arg = dgettext (domain, arg);
  1125. if (flags & OPTION_ARG_OPTIONAL)
  1126. __argp_fmtstream_printf (stream, " [-%c[%s]]", opt->key, arg);
  1127. else
  1128. {
  1129. /* Manually do line wrapping so that it (probably) won't
  1130. get wrapped at the embedded space. */
  1131. space (stream, 6 + strlen (arg));
  1132. __argp_fmtstream_printf (stream, "[-%c %s]", opt->key, arg);
  1133. }
  1134. }
  1135. return 0;
  1136. }
  1137. /* Output a usage entry for the long option opt to the stream pointed at by
  1138. COOKIE. */
  1139. static int
  1140. usage_long_opt (const struct argp_option *opt,
  1141. const struct argp_option *real,
  1142. const char *domain, void *cookie)
  1143. {
  1144. argp_fmtstream_t stream = cookie;
  1145. const char *arg = opt->arg;
  1146. int flags = opt->flags | real->flags;
  1147. if (! arg)
  1148. arg = real->arg;
  1149. if (! (flags & OPTION_NO_USAGE) && !odoc (opt))
  1150. {
  1151. if (arg)
  1152. {
  1153. arg = dgettext (domain, arg);
  1154. if (flags & OPTION_ARG_OPTIONAL)
  1155. __argp_fmtstream_printf (stream, " [--%s[=%s]]", opt->name, arg);
  1156. else
  1157. __argp_fmtstream_printf (stream, " [--%s=%s]", opt->name, arg);
  1158. }
  1159. else
  1160. __argp_fmtstream_printf (stream, " [--%s]", opt->name);
  1161. }
  1162. return 0;
  1163. }
  1164. /* Print a short usage description for the arguments in HOL to STREAM. */
  1165. static void
  1166. hol_usage (struct hol *hol, argp_fmtstream_t stream)
  1167. {
  1168. if (hol->num_entries > 0)
  1169. {
  1170. unsigned nentries;
  1171. struct hol_entry *entry;
  1172. char *short_no_arg_opts = alloca (strlen (hol->short_options) + 1);
  1173. char *snao_end = short_no_arg_opts;
  1174. /* First we put a list of short options without arguments. */
  1175. for (entry = hol->entries, nentries = hol->num_entries
  1176. ; nentries > 0
  1177. ; entry++, nentries--)
  1178. hol_entry_short_iterate (entry, add_argless_short_opt,
  1179. entry->argp->argp_domain, &snao_end);
  1180. if (snao_end > short_no_arg_opts)
  1181. {
  1182. *snao_end++ = 0;
  1183. __argp_fmtstream_printf (stream, " [-%s]", short_no_arg_opts);
  1184. }
  1185. /* Now a list of short options *with* arguments. */
  1186. for (entry = hol->entries, nentries = hol->num_entries
  1187. ; nentries > 0
  1188. ; entry++, nentries--)
  1189. hol_entry_short_iterate (entry, usage_argful_short_opt,
  1190. entry->argp->argp_domain, stream);
  1191. /* Finally, a list of long options (whew!). */
  1192. for (entry = hol->entries, nentries = hol->num_entries
  1193. ; nentries > 0
  1194. ; entry++, nentries--)
  1195. hol_entry_long_iterate (entry, usage_long_opt,
  1196. entry->argp->argp_domain, stream);
  1197. }
  1198. }
  1199. /* Make a HOL containing all levels of options in ARGP. CLUSTER is the
  1200. cluster in which ARGP's entries should be clustered, or 0. */
  1201. static struct hol *
  1202. argp_hol (const struct argp *argp, struct hol_cluster *cluster)
  1203. {
  1204. const struct argp_child *child = argp->children;
  1205. struct hol *hol = make_hol (argp, cluster);
  1206. if (child)
  1207. while (child->argp)
  1208. {
  1209. struct hol_cluster *child_cluster =
  1210. ((child->group || child->header)
  1211. /* Put CHILD->argp within its own cluster. */
  1212. ? hol_add_cluster (hol, child->group, child->header,
  1213. child - argp->children, cluster, argp)
  1214. /* Just merge it into the parent's cluster. */
  1215. : cluster);
  1216. hol_append (hol, argp_hol (child->argp, child_cluster)) ;
  1217. child++;
  1218. }
  1219. return hol;
  1220. }
  1221. /* Calculate how many different levels with alternative args strings exist in
  1222. ARGP. */
  1223. static size_t
  1224. argp_args_levels (const struct argp *argp)
  1225. {
  1226. size_t levels = 0;
  1227. const struct argp_child *child = argp->children;
  1228. if (argp->args_doc && strchr (argp->args_doc, '\n'))
  1229. levels++;
  1230. if (child)
  1231. while (child->argp)
  1232. levels += argp_args_levels ((child++)->argp);
  1233. return levels;
  1234. }
  1235. /* Print all the non-option args documented in ARGP to STREAM. Any output is
  1236. preceded by a space. LEVELS is a pointer to a byte vector the length
  1237. returned by argp_args_levels; it should be initialized to zero, and
  1238. updated by this routine for the next call if ADVANCE is true. True is
  1239. returned as long as there are more patterns to output. */
  1240. static int
  1241. argp_args_usage (const struct argp *argp, const struct argp_state *state,
  1242. char **levels, int advance, argp_fmtstream_t stream)
  1243. {
  1244. char *our_level = *levels;
  1245. int multiple = 0;
  1246. const struct argp_child *child = argp->children;
  1247. const char *tdoc = dgettext (argp->argp_domain, argp->args_doc), *nl = 0;
  1248. const char *fdoc = filter_doc (tdoc, ARGP_KEY_HELP_ARGS_DOC, argp, state);
  1249. if (fdoc)
  1250. {
  1251. const char *cp = fdoc;
  1252. nl = __strchrnul (cp, '\n');
  1253. if (*nl != '\0')
  1254. /* This is a "multi-level" args doc; advance to the correct position
  1255. as determined by our state in LEVELS, and update LEVELS. */
  1256. {
  1257. int i;
  1258. multiple = 1;
  1259. for (i = 0; i < *our_level; i++)
  1260. cp = nl + 1, nl = __strchrnul (cp, '\n');
  1261. (*levels)++;
  1262. }
  1263. /* Manually do line wrapping so that it (probably) won't get wrapped at
  1264. any embedded spaces. */
  1265. space (stream, 1 + mbsnwidth (cp, nl - cp, MBSW_STOP_AT_NUL));
  1266. __argp_fmtstream_write (stream, cp, nl - cp);
  1267. }
  1268. if (fdoc && fdoc != tdoc)
  1269. free ((char *)fdoc); /* Free user's modified doc string. */
  1270. if (child)
  1271. while (child->argp)
  1272. advance = !argp_args_usage ((child++)->argp, state, levels, advance, stream);
  1273. if (advance && multiple)
  1274. {
  1275. /* Need to increment our level. */
  1276. if (*nl)
  1277. /* There's more we can do here. */
  1278. {
  1279. (*our_level)++;
  1280. advance = 0; /* Our parent shouldn't advance also. */
  1281. }
  1282. else if (*our_level > 0)
  1283. /* We had multiple levels, but used them up; reset to zero. */
  1284. *our_level = 0;
  1285. }
  1286. return !advance;
  1287. }
  1288. /* Print the documentation for ARGP to STREAM; if POST is false, then
  1289. everything preceding a '\v' character in the documentation strings (or
  1290. the whole string, for those with none) is printed, otherwise, everything
  1291. following the '\v' character (nothing for strings without). Each separate
  1292. bit of documentation is separated a blank line, and if PRE_BLANK is true,
  1293. then the first is as well. If FIRST_ONLY is true, only the first
  1294. occurrence is output. Returns true if anything was output. */
  1295. static int
  1296. argp_doc (const struct argp *argp, const struct argp_state *state,
  1297. int post, int pre_blank, int first_only,
  1298. argp_fmtstream_t stream)
  1299. {
  1300. const char *text;
  1301. const char *inp_text;
  1302. size_t inp_text_len = 0;
  1303. const char *trans_text;
  1304. void *input = 0;
  1305. int anything = 0;
  1306. const struct argp_child *child = argp->children;
  1307. if (argp->doc)
  1308. {
  1309. char *vt = strchr (argp->doc, '\v');
  1310. if (vt)
  1311. {
  1312. if (post)
  1313. {
  1314. inp_text = vt + 1;
  1315. if (! *inp_text)
  1316. inp_text = 0;
  1317. }
  1318. else
  1319. {
  1320. inp_text_len = vt - argp->doc;
  1321. inp_text = inp_text_len ? __strndup (argp->doc, inp_text_len) : 0;
  1322. }
  1323. }
  1324. else
  1325. inp_text = post ? 0 : argp->doc;
  1326. trans_text = inp_text ? dgettext (argp->argp_domain, inp_text) : NULL;
  1327. }
  1328. else
  1329. trans_text = inp_text = 0;
  1330. if (argp->help_filter)
  1331. /* We have to filter the doc strings. */
  1332. {
  1333. input = __argp_input (argp, state);
  1334. text =
  1335. (*argp->help_filter) (post
  1336. ? ARGP_KEY_HELP_POST_DOC
  1337. : ARGP_KEY_HELP_PRE_DOC,
  1338. trans_text, input);
  1339. }
  1340. else
  1341. text = (const char *) trans_text;
  1342. if (text)
  1343. {
  1344. if (pre_blank)
  1345. __argp_fmtstream_putc (stream, '\n');
  1346. __argp_fmtstream_puts (stream, text);
  1347. if (__argp_fmtstream_point (stream) > __argp_fmtstream_lmargin (stream))
  1348. __argp_fmtstream_putc (stream, '\n');
  1349. anything = 1;
  1350. }
  1351. if (text && text != trans_text)
  1352. free ((char *) text); /* Free TEXT returned from the help filter. */
  1353. if (inp_text && inp_text_len)
  1354. free ((char *) inp_text); /* We copied INP_TEXT, so free it now. */
  1355. if (post && argp->help_filter)
  1356. /* Now see if we have to output an ARGP_KEY_HELP_EXTRA text. */
  1357. {
  1358. text = (*argp->help_filter) (ARGP_KEY_HELP_EXTRA, 0, input);
  1359. if (text)
  1360. {
  1361. if (anything || pre_blank)
  1362. __argp_fmtstream_putc (stream, '\n');
  1363. __argp_fmtstream_puts (stream, text);
  1364. free ((char *) text);
  1365. if (__argp_fmtstream_point (stream)
  1366. > __argp_fmtstream_lmargin (stream))
  1367. __argp_fmtstream_putc (stream, '\n');
  1368. anything = 1;
  1369. }
  1370. }
  1371. if (child)
  1372. while (child->argp && !(first_only && anything))
  1373. anything |=
  1374. argp_doc ((child++)->argp, state,
  1375. post, anything || pre_blank, first_only,
  1376. stream);
  1377. return anything;
  1378. }
  1379. /* Output a usage message for ARGP to STREAM. If called from
  1380. argp_state_help, STATE is the relevant parsing state. FLAGS are from the
  1381. set ARGP_HELP_*. NAME is what to use wherever a "program name" is
  1382. needed. */
  1383. static void
  1384. _help (const struct argp *argp, const struct argp_state *state, FILE *stream,
  1385. unsigned flags, char *name)
  1386. {
  1387. int anything = 0; /* Whether we've output anything. */
  1388. struct hol *hol = 0;
  1389. argp_fmtstream_t fs;
  1390. if (! stream)
  1391. return;
  1392. #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
  1393. __flockfile (stream);
  1394. #endif
  1395. if (! uparams.valid)
  1396. fill_in_uparams (state);
  1397. fs = __argp_make_fmtstream (stream, 0, uparams.rmargin, 0);
  1398. if (! fs)
  1399. {
  1400. #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
  1401. __funlockfile (stream);
  1402. #endif
  1403. return;
  1404. }
  1405. if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG))
  1406. {
  1407. hol = argp_hol (argp, 0);
  1408. /* If present, these options always come last. */
  1409. hol_set_group (hol, "help", -1);
  1410. hol_set_group (hol, "version", -1);
  1411. hol_sort (hol);
  1412. }
  1413. if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE))
  1414. /* Print a short "Usage:" message. */
  1415. {
  1416. int first_pattern = 1, more_patterns;
  1417. size_t num_pattern_levels = argp_args_levels (argp);
  1418. char *pattern_levels = alloca (num_pattern_levels);
  1419. memset (pattern_levels, 0, num_pattern_levels);
  1420. do
  1421. {
  1422. int old_lm;
  1423. int old_wm = __argp_fmtstream_set_wmargin (fs, uparams.usage_indent);
  1424. char *levels = pattern_levels;
  1425. if (first_pattern)
  1426. __argp_fmtstream_printf (fs, "%s %s",
  1427. dgettext (argp->argp_domain, "Usage:"),
  1428. name);
  1429. else
  1430. __argp_fmtstream_printf (fs, "%s %s",
  1431. dgettext (argp->argp_domain, " or: "),
  1432. name);
  1433. /* We set the lmargin as well as the wmargin, because hol_usage
  1434. manually wraps options with newline to avoid annoying breaks. */
  1435. old_lm = __argp_fmtstream_set_lmargin (fs, uparams.usage_indent);
  1436. if (flags & ARGP_HELP_SHORT_USAGE)
  1437. /* Just show where the options go. */
  1438. {
  1439. if (hol->num_entries > 0)
  1440. __argp_fmtstream_puts (fs, dgettext (argp->argp_domain,
  1441. " [OPTION...]"));
  1442. }
  1443. else
  1444. /* Actually print the options. */
  1445. {
  1446. hol_usage (hol, fs);
  1447. flags |= ARGP_HELP_SHORT_USAGE; /* But only do so once. */
  1448. }
  1449. more_patterns = argp_args_usage (argp, state, &levels, 1, fs);
  1450. __argp_fmtstream_set_wmargin (fs, old_wm);
  1451. __argp_fmtstream_set_lmargin (fs, old_lm);
  1452. __argp_fmtstream_putc (fs, '\n');
  1453. anything = 1;
  1454. first_pattern = 0;
  1455. }
  1456. while (more_patterns);
  1457. }
  1458. if (flags & ARGP_HELP_PRE_DOC)
  1459. anything |= argp_doc (argp, state, 0, 0, 1, fs);
  1460. if (flags & ARGP_HELP_SEE)
  1461. {
  1462. __argp_fmtstream_printf (fs, dgettext (argp->argp_domain, "\
  1463. Try '%s --help' or '%s --usage' for more information.\n"),
  1464. name, name);
  1465. anything = 1;
  1466. }
  1467. if (flags & ARGP_HELP_LONG)
  1468. /* Print a long, detailed help message. */
  1469. {
  1470. /* Print info about all the options. */
  1471. if (hol->num_entries > 0)
  1472. {
  1473. if (anything)
  1474. __argp_fmtstream_putc (fs, '\n');
  1475. hol_help (hol, state, fs);
  1476. anything = 1;
  1477. }
  1478. }
  1479. if (flags & ARGP_HELP_POST_DOC)
  1480. /* Print any documentation strings at the end. */
  1481. anything |= argp_doc (argp, state, 1, anything, 0, fs);
  1482. if ((flags & ARGP_HELP_BUG_ADDR) && argp_program_bug_address)
  1483. {
  1484. if (anything)
  1485. __argp_fmtstream_putc (fs, '\n');
  1486. __argp_fmtstream_printf (fs, dgettext (argp->argp_domain,
  1487. "Report bugs to %s.\n"),
  1488. argp_program_bug_address);
  1489. anything = 1;
  1490. }
  1491. #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
  1492. __funlockfile (stream);
  1493. #endif
  1494. if (hol)
  1495. hol_free (hol);
  1496. __argp_fmtstream_free (fs);
  1497. }
  1498. /* Output a usage message for ARGP to STREAM. FLAGS are from the set
  1499. ARGP_HELP_*. NAME is what to use wherever a "program name" is needed. */
  1500. void __argp_help (const struct argp *argp, FILE *stream,
  1501. unsigned flags, char *name)
  1502. {
  1503. struct argp_state state;
  1504. memset (&state, 0, sizeof state);
  1505. state.root_argp = argp;
  1506. _help (argp, &state, stream, flags, name);
  1507. }
  1508. #ifdef weak_alias
  1509. weak_alias (__argp_help, argp_help)
  1510. #endif
  1511. #if ! (defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
  1512. char *
  1513. __argp_short_program_name (void)
  1514. {
  1515. # if HAVE_DECL_PROGRAM_INVOCATION_NAME
  1516. return __argp_base_name (program_invocation_name);
  1517. # else
  1518. /* FIXME: What now? Miles suggests that it is better to use NULL,
  1519. but currently the value is passed on directly to fputs_unlocked,
  1520. so that requires more changes. */
  1521. # if __GNUC__
  1522. # warning No reasonable value to return
  1523. # endif /* __GNUC__ */
  1524. return "";
  1525. # endif
  1526. }
  1527. #endif
  1528. /* Output, if appropriate, a usage message for STATE to STREAM. FLAGS are
  1529. from the set ARGP_HELP_*. */
  1530. void
  1531. __argp_state_help (const struct argp_state *state, FILE *stream, unsigned flags)
  1532. {
  1533. if ((!state || ! (state->flags & ARGP_NO_ERRS)) && stream)
  1534. {
  1535. if (state && (state->flags & ARGP_LONG_ONLY))
  1536. flags |= ARGP_HELP_LONG_ONLY;
  1537. _help (state ? state->root_argp : 0, state, stream, flags,
  1538. state ? state->name : __argp_short_program_name ());
  1539. if (!state || ! (state->flags & ARGP_NO_EXIT))
  1540. {
  1541. if (flags & ARGP_HELP_EXIT_ERR)
  1542. exit (argp_err_exit_status);
  1543. if (flags & ARGP_HELP_EXIT_OK)
  1544. exit (0);
  1545. }
  1546. }
  1547. }
  1548. #ifdef weak_alias
  1549. weak_alias (__argp_state_help, argp_state_help)
  1550. #endif
  1551. /* If appropriate, print the printf string FMT and following args, preceded
  1552. by the program name and ':', to stderr, and followed by a "Try ... --help"
  1553. message, then exit (1). */
  1554. void
  1555. __argp_error (const struct argp_state *state, const char *fmt, ...)
  1556. {
  1557. if (!state || !(state->flags & ARGP_NO_ERRS))
  1558. {
  1559. FILE *stream = state ? state->err_stream : stderr;
  1560. if (stream)
  1561. {
  1562. va_list ap;
  1563. #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
  1564. __flockfile (stream);
  1565. #endif
  1566. va_start (ap, fmt);
  1567. #ifdef USE_IN_LIBIO
  1568. if (_IO_fwide (stream, 0) > 0)
  1569. {
  1570. char *buf;
  1571. if (__asprintf (&buf, fmt, ap) < 0)
  1572. buf = NULL;
  1573. __fwprintf (stream, L"%s: %s\n",
  1574. state ? state->name : __argp_short_program_name (),
  1575. buf);
  1576. free (buf);
  1577. }
  1578. else
  1579. #endif
  1580. {
  1581. fputs_unlocked (state
  1582. ? state->name : __argp_short_program_name (),
  1583. stream);
  1584. putc_unlocked (':', stream);
  1585. putc_unlocked (' ', stream);
  1586. vfprintf (stream, fmt, ap);
  1587. putc_unlocked ('\n', stream);
  1588. }
  1589. __argp_state_help (state, stream, ARGP_HELP_STD_ERR);
  1590. va_end (ap);
  1591. #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
  1592. __funlockfile (stream);
  1593. #endif
  1594. }
  1595. }
  1596. }
  1597. #ifdef weak_alias
  1598. weak_alias (__argp_error, argp_error)
  1599. #endif
  1600. /* Similar to the standard gnu error-reporting function error(), but will
  1601. respect the ARGP_NO_EXIT and ARGP_NO_ERRS flags in STATE, and will print
  1602. to STATE->err_stream. This is useful for argument parsing code that is
  1603. shared between program startup (when exiting is desired) and runtime
  1604. option parsing (when typically an error code is returned instead). The
  1605. difference between this function and argp_error is that the latter is for
  1606. *parsing errors*, and the former is for other problems that occur during
  1607. parsing but don't reflect a (syntactic) problem with the input. */
  1608. void
  1609. __argp_failure (const struct argp_state *state, int status, int errnum,
  1610. const char *fmt, ...)
  1611. {
  1612. if (!state || !(state->flags & ARGP_NO_ERRS))
  1613. {
  1614. FILE *stream = state ? state->err_stream : stderr;
  1615. if (stream)
  1616. {
  1617. #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
  1618. __flockfile (stream);
  1619. #endif
  1620. #ifdef USE_IN_LIBIO
  1621. if (_IO_fwide (stream, 0) > 0)
  1622. __fwprintf (stream, L"%s",
  1623. state ? state->name : __argp_short_program_name ());
  1624. else
  1625. #endif
  1626. fputs_unlocked (state
  1627. ? state->name : __argp_short_program_name (),
  1628. stream);
  1629. if (fmt)
  1630. {
  1631. va_list ap;
  1632. va_start (ap, fmt);
  1633. #ifdef USE_IN_LIBIO
  1634. if (_IO_fwide (stream, 0) > 0)
  1635. {
  1636. char *buf;
  1637. if (__asprintf (&buf, fmt, ap) < 0)
  1638. buf = NULL;
  1639. __fwprintf (stream, L": %s", buf);
  1640. free (buf);
  1641. }
  1642. else
  1643. #endif
  1644. {
  1645. putc_unlocked (':', stream);
  1646. putc_unlocked (' ', stream);
  1647. vfprintf (stream, fmt, ap);
  1648. }
  1649. va_end (ap);
  1650. }
  1651. if (errnum)
  1652. {
  1653. char buf[200];
  1654. #ifdef USE_IN_LIBIO
  1655. if (_IO_fwide (stream, 0) > 0)
  1656. __fwprintf (stream, L": %s",
  1657. __strerror_r (errnum, buf, sizeof (buf)));
  1658. else
  1659. #endif
  1660. {
  1661. char const *s = NULL;
  1662. putc_unlocked (':', stream);
  1663. putc_unlocked (' ', stream);
  1664. #if _LIBC || (HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P && !defined strerror_r)
  1665. s = __strerror_r (errnum, buf, sizeof buf);
  1666. #elif HAVE_DECL_STRERROR_R
  1667. if (__strerror_r (errnum, buf, sizeof buf) == 0)
  1668. s = buf;
  1669. #endif
  1670. #if !_LIBC
  1671. if (! s && ! (s = strerror (errnum)))
  1672. s = dgettext (state->root_argp->argp_domain,
  1673. "Unknown system error");
  1674. #endif
  1675. fputs (s, stream);
  1676. }
  1677. }
  1678. #ifdef USE_IN_LIBIO
  1679. if (_IO_fwide (stream, 0) > 0)
  1680. putwc_unlocked (L'\n', stream);
  1681. else
  1682. #endif
  1683. putc_unlocked ('\n', stream);
  1684. #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
  1685. __funlockfile (stream);
  1686. #endif
  1687. if (status && (!state || !(state->flags & ARGP_NO_EXIT)))
  1688. exit (status);
  1689. }
  1690. }
  1691. }
  1692. #ifdef weak_alias
  1693. weak_alias (__argp_failure, argp_failure)
  1694. #endif