argp-help.c 58 KB

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