12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871 |
- #ifndef _GNU_SOURCE
- # define _GNU_SOURCE 1
- #endif
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #ifndef __GNUC__
- # if HAVE_ALLOCA_H || defined _LIBC
- # include <alloca.h>
- # else
- # ifdef _AIX
- #pragma alloca
- # else
- # ifndef alloca
- char *alloca ();
- # endif
- # endif
- # endif
- #endif
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include <stdarg.h>
- #include <ctype.h>
- #include <limits.h>
- #ifdef _LIBC
- # include <wchar.h>
- #endif
- #include <features.h>
- #ifndef _
- # define dgettext(domain, msgid) (msgid)
- #endif
- #ifndef _LIBC
- # if HAVE_STRERROR_R
- # if !HAVE_DECL_STRERROR_R
- char *strerror_r (int errnum, char *buf, size_t buflen);
- # endif
- # else
- # if !HAVE_DECL_STRERROR
- char *strerror (int errnum);
- # endif
- # endif
- #endif
- #include <argp.h>
- #include "argp-fmtstream.h"
- #include <stdbool.h>
- #include <stdint.h>
- #ifndef SIZE_MAX
- # define SIZE_MAX ((size_t) -1)
- #endif
- #define DUP_ARGS 0
- #define DUP_ARGS_NOTE 1
- #define SHORT_OPT_COL 2
- #define LONG_OPT_COL 6
- #define DOC_OPT_COL 2
- #define OPT_DOC_COL 29
- #define HEADER_COL 1
- #define USAGE_INDENT 12
- #define RMARGIN 79
- struct uparams
- {
-
- int dup_args;
-
- int dup_args_note;
-
- int short_opt_col;
- int long_opt_col;
- int doc_opt_col;
- int opt_doc_col;
- int header_col;
- int usage_indent;
- int rmargin;
- };
- static struct uparams uparams = {
- DUP_ARGS, DUP_ARGS_NOTE,
- SHORT_OPT_COL, LONG_OPT_COL, DOC_OPT_COL, OPT_DOC_COL, HEADER_COL,
- USAGE_INDENT, RMARGIN
- };
- struct uparam_name
- {
- const char name[14];
- bool is_bool;
- uint8_t uparams_offs;
- };
- static const struct uparam_name uparam_names[] =
- {
- { "dup-args", true, offsetof (struct uparams, dup_args) },
- { "dup-args-note", true, offsetof (struct uparams, dup_args_note) },
- { "short-opt-col", false, offsetof (struct uparams, short_opt_col) },
- { "long-opt-col", false, offsetof (struct uparams, long_opt_col) },
- { "doc-opt-col", false, offsetof (struct uparams, doc_opt_col) },
- { "opt-doc-col", false, offsetof (struct uparams, opt_doc_col) },
- { "header-col", false, offsetof (struct uparams, header_col) },
- { "usage-indent", false, offsetof (struct uparams, usage_indent) },
- { "rmargin", false, offsetof (struct uparams, rmargin) }
- };
- #define nuparam_names (sizeof (uparam_names) / sizeof (uparam_names[0]))
- static void
- fill_in_uparams (const struct argp_state *state)
- {
- const char *var = getenv ("ARGP_HELP_FMT");
- #define SKIPWS(p) do { while (isspace (*p)) p++; } while (0);
- if (var)
-
- while (*var)
- {
- SKIPWS (var);
- if (isalpha (*var))
- {
- size_t var_len;
- const struct uparam_name *un;
- int unspec = 0, val = 0;
- const char *arg = var;
- while (isalnum (*arg) || *arg == '-' || *arg == '_')
- arg++;
- var_len = arg - var;
- SKIPWS (arg);
- if (*arg == '\0' || *arg == ',')
- unspec = 1;
- else if (*arg == '=')
- {
- arg++;
- SKIPWS (arg);
- }
- if (unspec)
- {
- if (var[0] == 'n' && var[1] == 'o' && var[2] == '-')
- {
- val = 0;
- var += 3;
- var_len -= 3;
- }
- else
- val = 1;
- }
- else if (isdigit (*arg))
- {
- val = atoi (arg);
- while (isdigit (*arg))
- arg++;
- SKIPWS (arg);
- }
- un = uparam_names;
- size_t u;
- for (u = 0; u < nuparam_names; ++un, ++u)
- if (strlen (un->name) == var_len
- && strncmp (var, un->name, var_len) == 0)
- {
- if (unspec && !un->is_bool)
- argp_failure (state, 0, 0,
- dgettext (state == NULL ? NULL
- : state->root_argp->argp_domain,
- "\
- %.*s: ARGP_HELP_FMT parameter requires a value"),
- (int) var_len, var);
- else
- *(int *)((char *)&uparams + un->uparams_offs) = val;
- break;
- }
- if (u == nuparam_names)
- argp_failure (state, 0, 0,
- dgettext (state == NULL ? NULL
- : state->root_argp->argp_domain, "\
- %.*s: Unknown ARGP_HELP_FMT parameter"),
- (int) var_len, var);
- var = arg;
- if (*var == ',')
- var++;
- }
- else if (*var)
- {
- argp_failure (state, 0, 0,
- dgettext (state == NULL ? NULL
- : state->root_argp->argp_domain,
- "Garbage in ARGP_HELP_FMT: %s"), var);
- break;
- }
- }
- }
- #define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN))
- #define oalias(opt) ((opt)->flags & OPTION_ALIAS)
- #define odoc(opt) ((opt)->flags & OPTION_DOC)
- #define oend(opt) __option_is_end (opt)
- #define oshort(opt) __option_is_short (opt)
- static int
- find_char (char ch, char *beg, char *end)
- {
- while (beg < end)
- if (*beg == ch)
- return 1;
- else
- beg++;
- return 0;
- }
- struct hol_cluster;
- struct hol_entry
- {
-
- const struct argp_option *opt;
-
- unsigned num;
-
- char *short_options;
-
- int group;
-
- struct hol_cluster *cluster;
-
- const struct argp *argp;
- };
- struct hol_cluster
- {
-
- const char *header;
-
- int index;
-
- int group;
-
- struct hol_cluster *parent;
-
- const struct argp *argp;
-
- int depth;
-
- struct hol_cluster *next;
- };
- struct hol
- {
-
- struct hol_entry *entries;
-
- unsigned num_entries;
-
- char *short_options;
-
- struct hol_cluster *clusters;
- };
- static struct hol *
- make_hol (const struct argp *argp, struct hol_cluster *cluster)
- {
- char *so;
- const struct argp_option *o;
- const struct argp_option *opts = argp->options;
- struct hol_entry *entry;
- unsigned num_short_options = 0;
- struct hol *hol = malloc (sizeof (struct hol));
- assert (hol);
- hol->num_entries = 0;
- hol->clusters = 0;
- if (opts)
- {
- int cur_group = 0;
-
- assert (! oalias (opts));
-
- for (o = opts; ! oend (o); o++)
- {
- if (! oalias (o))
- hol->num_entries++;
- if (oshort (o))
- num_short_options++;
- }
- hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries);
- hol->short_options = malloc (num_short_options + 1);
- assert (hol->entries && hol->short_options);
- #if SIZE_MAX <= UINT_MAX
- assert (hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry));
- #endif
-
- so = hol->short_options;
- for (o = opts, entry = hol->entries; ! oend (o); entry++)
- {
- entry->opt = o;
- entry->num = 0;
- entry->short_options = so;
- entry->group = cur_group =
- o->group
- ? o->group
- : ((!o->name && !o->key)
- ? cur_group + 1
- : cur_group);
- entry->cluster = cluster;
- entry->argp = argp;
- do
- {
- entry->num++;
- if (oshort (o) && ! find_char (o->key, hol->short_options, so))
-
- *so++ = o->key;
- o++;
- }
- while (! oend (o) && oalias (o));
- }
- *so = '\0';
- }
- return hol;
- }
- static struct hol_cluster *
- hol_add_cluster (struct hol *hol, int group, const char *header, int index,
- struct hol_cluster *parent, const struct argp *argp)
- {
- struct hol_cluster *cl = malloc (sizeof (struct hol_cluster));
- if (cl)
- {
- cl->group = group;
- cl->header = header;
- cl->index = index;
- cl->parent = parent;
- cl->argp = argp;
- cl->depth = parent ? parent->depth + 1 : 0;
- cl->next = hol->clusters;
- hol->clusters = cl;
- }
- return cl;
- }
- static void
- hol_free (struct hol *hol)
- {
- struct hol_cluster *cl = hol->clusters;
- while (cl)
- {
- struct hol_cluster *next = cl->next;
- free (cl);
- cl = next;
- }
- if (hol->num_entries > 0)
- {
- free (hol->entries);
- free (hol->short_options);
- }
- free (hol);
- }
- static int
- hol_entry_short_iterate (const struct hol_entry *entry,
- int (*func)(const struct argp_option *opt,
- const struct argp_option *real,
- const char *domain, void *cookie),
- const char *domain, void *cookie)
- {
- unsigned nopts;
- int val = 0;
- const struct argp_option *opt, *real = entry->opt;
- char *so = entry->short_options;
- for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
- if (oshort (opt) && *so == opt->key)
- {
- if (!oalias (opt))
- real = opt;
- if (ovisible (opt))
- val = (*func)(opt, real, domain, cookie);
- so++;
- }
- return val;
- }
- static __inline__ int
- __attribute__ ((always_inline))
- hol_entry_long_iterate (const struct hol_entry *entry,
- int (*func)(const struct argp_option *opt,
- const struct argp_option *real,
- const char *domain, void *cookie),
- const char *domain, void *cookie)
- {
- unsigned nopts;
- int val = 0;
- const struct argp_option *opt, *real = entry->opt;
- for (opt = real, nopts = entry->num; nopts > 0 && !val; opt++, nopts--)
- if (opt->name)
- {
- if (!oalias (opt))
- real = opt;
- if (ovisible (opt))
- val = (*func)(opt, real, domain, cookie);
- }
- return val;
- }
- static __inline__ int
- until_short (const struct argp_option *opt, const struct argp_option *real,
- const char *domain, void *cookie)
- {
- return oshort (opt) ? opt->key : 0;
- }
- static char
- hol_entry_first_short (const struct hol_entry *entry)
- {
- return hol_entry_short_iterate (entry, until_short,
- entry->argp->argp_domain, 0);
- }
- static const char *
- hol_entry_first_long (const struct hol_entry *entry)
- {
- const struct argp_option *opt;
- unsigned num;
- for (opt = entry->opt, num = entry->num; num > 0; opt++, num--)
- if (opt->name && ovisible (opt))
- return opt->name;
- return 0;
- }
- static struct hol_entry *
- hol_find_entry (struct hol *hol, const char *name)
- {
- struct hol_entry *entry = hol->entries;
- unsigned num_entries = hol->num_entries;
- while (num_entries-- > 0)
- {
- const struct argp_option *opt = entry->opt;
- unsigned num_opts = entry->num;
- while (num_opts-- > 0)
- if (opt->name && ovisible (opt) && strcmp (opt->name, name) == 0)
- return entry;
- else
- opt++;
- entry++;
- }
- return 0;
- }
- static void
- hol_set_group (struct hol *hol, const char *name, int group)
- {
- struct hol_entry *entry = hol_find_entry (hol, name);
- if (entry)
- entry->group = group;
- }
- static int
- group_cmp (int group1, int group2, int eq)
- {
- if (group1 == group2)
- return eq;
- else if ((group1 < 0 && group2 < 0) || (group1 >= 0 && group2 >= 0))
- return group1 - group2;
- else
- return group2 - group1;
- }
- static int
- hol_cluster_cmp (const struct hol_cluster *cl1, const struct hol_cluster *cl2)
- {
-
- while (cl1->depth > cl2->depth)
- cl1 = cl1->parent;
- while (cl2->depth > cl1->depth)
- cl2 = cl2->parent;
-
- while (cl1->parent != cl2->parent)
- cl1 = cl1->parent, cl2 = cl2->parent;
- return group_cmp (cl1->group, cl2->group, cl2->index - cl1->index);
- }
- static struct hol_cluster *
- hol_cluster_base (struct hol_cluster *cl)
- {
- while (cl->parent)
- cl = cl->parent;
- return cl;
- }
- static int
- hol_cluster_is_child (const struct hol_cluster *cl1,
- const struct hol_cluster *cl2)
- {
- while (cl1 && cl1 != cl2)
- cl1 = cl1->parent;
- return cl1 == cl2;
- }
- static int
- canon_doc_option (const char **name)
- {
- int non_opt;
-
- while (isspace (**name))
- (*name)++;
-
- non_opt = (**name != '-');
-
- while (**name && !isalnum (**name))
- (*name)++;
- return non_opt;
- }
- static int
- hol_entry_cmp (const struct hol_entry *entry1,
- const struct hol_entry *entry2)
- {
-
- int group1 = entry1->group, group2 = entry2->group;
- if (entry1->cluster != entry2->cluster)
- {
-
- if (! entry1->cluster)
-
- return group_cmp (group1, hol_cluster_base (entry2->cluster)->group, -1);
- else if (! entry2->cluster)
-
- return group_cmp (hol_cluster_base (entry1->cluster)->group, group2, 1);
- else
-
- return hol_cluster_cmp (entry1->cluster, entry2->cluster);
- }
- else if (group1 == group2)
-
- {
- int short1 = hol_entry_first_short (entry1);
- int short2 = hol_entry_first_short (entry2);
- int doc1 = odoc (entry1->opt);
- int doc2 = odoc (entry2->opt);
- const char *long1 = hol_entry_first_long (entry1);
- const char *long2 = hol_entry_first_long (entry2);
- if (doc1)
- doc1 = long1 != NULL && canon_doc_option (&long1);
- if (doc2)
- doc2 = long2 != NULL && canon_doc_option (&long2);
- if (doc1 != doc2)
-
- return doc1 - doc2;
- else if (!short1 && !short2 && long1 && long2)
-
- return strcasecmp (long1, long2);
- else
-
- {
- char first1 = short1 ? short1 : long1 ? *long1 : 0;
- char first2 = short2 ? short2 : long2 ? *long2 : 0;
- #ifdef _tolower
- int lower_cmp = _tolower (first1) - _tolower (first2);
- #else
- int lower_cmp = tolower (first1) - tolower (first2);
- #endif
-
- return lower_cmp ? lower_cmp : first2 - first1;
- }
- }
- else
-
- return group_cmp (group1, group2, 0);
- }
- static int
- hol_entry_qcmp (const void *entry1_v, const void *entry2_v)
- {
- return hol_entry_cmp (entry1_v, entry2_v);
- }
- static void
- hol_sort (struct hol *hol)
- {
- if (hol->num_entries > 0)
- qsort (hol->entries, hol->num_entries, sizeof (struct hol_entry),
- hol_entry_qcmp);
- }
- static void
- hol_append (struct hol *hol, struct hol *more)
- {
- struct hol_cluster **cl_end = &hol->clusters;
-
- while (*cl_end)
- cl_end = &(*cl_end)->next;
- *cl_end = more->clusters;
- more->clusters = 0;
-
- if (more->num_entries > 0)
- {
- if (hol->num_entries == 0)
- {
- hol->num_entries = more->num_entries;
- hol->entries = more->entries;
- hol->short_options = more->short_options;
- more->num_entries = 0;
- }
- else
-
- {
- unsigned left;
- char *so, *more_so;
- struct hol_entry *e;
- unsigned num_entries = hol->num_entries + more->num_entries;
- struct hol_entry *entries =
- malloc (num_entries * sizeof (struct hol_entry));
- unsigned hol_so_len = strlen (hol->short_options);
- char *short_options =
- malloc (hol_so_len + strlen (more->short_options) + 1);
- assert (entries && short_options);
- #if SIZE_MAX <= UINT_MAX
- assert (num_entries <= SIZE_MAX / sizeof (struct hol_entry));
- #endif
- mempcpy (mempcpy (entries, hol->entries,
- hol->num_entries * sizeof (struct hol_entry)),
- more->entries,
- more->num_entries * sizeof (struct hol_entry));
- mempcpy (short_options, hol->short_options, hol_so_len);
-
- for (e = entries, left = hol->num_entries; left > 0; e++, left--)
- e->short_options += (short_options - hol->short_options);
-
- so = short_options + hol_so_len;
- more_so = more->short_options;
- for (left = more->num_entries; left > 0; e++, left--)
- {
- int opts_left;
- const struct argp_option *opt;
- e->short_options = so;
- for (opts_left = e->num, opt = e->opt; opts_left; opt++, opts_left--)
- {
- int ch = *more_so;
- if (oshort (opt) && ch == opt->key)
-
- {
- if (! find_char (ch, short_options,
- short_options + hol_so_len))
-
- *so++ = ch;
- more_so++;
- }
- }
- }
- *so = '\0';
- free (hol->entries);
- free (hol->short_options);
- hol->entries = entries;
- hol->num_entries = num_entries;
- hol->short_options = short_options;
- }
- }
- hol_free (more);
- }
- static void
- indent_to (argp_fmtstream_t stream, unsigned col)
- {
- int needed = col - __argp_fmtstream_point (stream);
- while (needed-- > 0)
- __argp_fmtstream_putc (stream, ' ');
- }
- static void
- space (argp_fmtstream_t stream, size_t ensure)
- {
- if (__argp_fmtstream_point (stream) + ensure
- >= __argp_fmtstream_rmargin (stream))
- __argp_fmtstream_putc (stream, '\n');
- else
- __argp_fmtstream_putc (stream, ' ');
- }
- static void
- arg (const struct argp_option *real, const char *req_fmt, const char *opt_fmt,
- const char *domain, argp_fmtstream_t stream)
- {
- if (real->arg)
- {
- if (real->flags & OPTION_ARG_OPTIONAL)
- __argp_fmtstream_printf (stream, opt_fmt,
- dgettext (domain, real->arg));
- else
- __argp_fmtstream_printf (stream, req_fmt,
- dgettext (domain, real->arg));
- }
- }
- struct hol_help_state
- {
-
- struct hol_entry *prev_entry;
-
- int sep_groups;
-
- int suppressed_dup_arg;
- };
- struct pentry_state
- {
- const struct hol_entry *entry;
- argp_fmtstream_t stream;
- struct hol_help_state *hhstate;
-
- int first;
-
- const struct argp_state *state;
- };
- static const char *
- filter_doc (const char *doc, int key, const struct argp *argp,
- const struct argp_state *state)
- {
- if (argp && argp->help_filter)
-
- {
- void *input = __argp_input (argp, state);
- return (*argp->help_filter) (key, doc, input);
- }
- else
-
- return doc;
- }
- static void
- print_header (const char *str, const struct argp *argp,
- struct pentry_state *pest)
- {
- const char *tstr = dgettext (argp->argp_domain, str);
- const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_HEADER, argp, pest->state);
- if (fstr)
- {
- if (*fstr)
- {
- if (pest->hhstate->prev_entry)
-
- __argp_fmtstream_putc (pest->stream, '\n');
- indent_to (pest->stream, uparams.header_col);
- __argp_fmtstream_set_lmargin (pest->stream, uparams.header_col);
- __argp_fmtstream_set_wmargin (pest->stream, uparams.header_col);
- __argp_fmtstream_puts (pest->stream, fstr);
- __argp_fmtstream_set_lmargin (pest->stream, 0);
- __argp_fmtstream_putc (pest->stream, '\n');
- }
- pest->hhstate->sep_groups = 1;
- }
- if (fstr != tstr)
- free ((char *) fstr);
- }
- static void
- comma (unsigned col, struct pentry_state *pest)
- {
- if (pest->first)
- {
- const struct hol_entry *pe = pest->hhstate->prev_entry;
- const struct hol_cluster *cl = pest->entry->cluster;
- if (pest->hhstate->sep_groups && pe && pest->entry->group != pe->group)
- __argp_fmtstream_putc (pest->stream, '\n');
- if (cl && cl->header && *cl->header
- && (!pe
- || (pe->cluster != cl
- && !hol_cluster_is_child (pe->cluster, cl))))
-
- {
- int old_wm = __argp_fmtstream_wmargin (pest->stream);
- print_header (cl->header, cl->argp, pest);
- __argp_fmtstream_set_wmargin (pest->stream, old_wm);
- }
- pest->first = 0;
- }
- else
- __argp_fmtstream_puts (pest->stream, ", ");
- indent_to (pest->stream, col);
- }
- static void
- hol_entry_help (struct hol_entry *entry, const struct argp_state *state,
- argp_fmtstream_t stream, struct hol_help_state *hhstate)
- {
- unsigned num;
- const struct argp_option *real = entry->opt, *opt;
- char *so = entry->short_options;
- int have_long_opt = 0;
-
- int old_lm = __argp_fmtstream_set_lmargin (stream, 0);
- int old_wm = __argp_fmtstream_wmargin (stream);
-
- struct pentry_state pest = { entry, stream, hhstate, 1, state };
- if (! odoc (real))
- for (opt = real, num = entry->num; num > 0; opt++, num--)
- if (opt->name && ovisible (opt))
- {
- have_long_opt = 1;
- break;
- }
-
- __argp_fmtstream_set_wmargin (stream, uparams.short_opt_col);
- for (opt = real, num = entry->num; num > 0; opt++, num--)
- if (oshort (opt) && opt->key == *so)
-
- {
- if (ovisible (opt))
- {
- comma (uparams.short_opt_col, &pest);
- __argp_fmtstream_putc (stream, '-');
- __argp_fmtstream_putc (stream, *so);
- if (!have_long_opt || uparams.dup_args)
- arg (real, " %s", "[%s]",
- state == NULL ? NULL : state->root_argp->argp_domain,
- stream);
- else if (real->arg)
- hhstate->suppressed_dup_arg = 1;
- }
- so++;
- }
-
- if (odoc (real))
-
- {
- __argp_fmtstream_set_wmargin (stream, uparams.doc_opt_col);
- for (opt = real, num = entry->num; num > 0; opt++, num--)
- if (opt->name && ovisible (opt))
- {
- comma (uparams.doc_opt_col, &pest);
-
- __argp_fmtstream_puts (stream,
- dgettext (state == NULL ? NULL
- : state->root_argp->argp_domain,
- opt->name));
- }
- }
- else
-
- {
- __argp_fmtstream_set_wmargin (stream, uparams.long_opt_col);
- for (opt = real, num = entry->num; num > 0; opt++, num--)
- if (opt->name && ovisible (opt))
- {
- comma (uparams.long_opt_col, &pest);
- __argp_fmtstream_printf (stream, "--%s", opt->name);
- arg (real, "=%s", "[=%s]",
- state == NULL ? NULL : state->root_argp->argp_domain, stream);
- }
- }
-
- __argp_fmtstream_set_lmargin (stream, 0);
- if (pest.first)
- {
-
- if (!oshort (real) && !real->name)
-
- print_header (real->doc, entry->argp, &pest);
- else
-
- goto cleanup;
- }
- else
- {
- const char *tstr = real->doc ? dgettext (state == NULL ? NULL
- : state->root_argp->argp_domain,
- real->doc) : 0;
- const char *fstr = filter_doc (tstr, real->key, entry->argp, state);
- if (fstr && *fstr)
- {
- unsigned int col = __argp_fmtstream_point (stream);
- __argp_fmtstream_set_lmargin (stream, uparams.opt_doc_col);
- __argp_fmtstream_set_wmargin (stream, uparams.opt_doc_col);
- if (col > (unsigned int) (uparams.opt_doc_col + 3))
- __argp_fmtstream_putc (stream, '\n');
- else if (col >= (unsigned int) uparams.opt_doc_col)
- __argp_fmtstream_puts (stream, " ");
- else
- indent_to (stream, uparams.opt_doc_col);
- __argp_fmtstream_puts (stream, fstr);
- }
- if (fstr && fstr != tstr)
- free ((char *) fstr);
-
- __argp_fmtstream_set_lmargin (stream, 0);
- __argp_fmtstream_putc (stream, '\n');
- }
- hhstate->prev_entry = entry;
- cleanup:
- __argp_fmtstream_set_lmargin (stream, old_lm);
- __argp_fmtstream_set_wmargin (stream, old_wm);
- }
- static void
- hol_help (struct hol *hol, const struct argp_state *state,
- argp_fmtstream_t stream)
- {
- unsigned num;
- struct hol_entry *entry;
- struct hol_help_state hhstate = { 0, 0, 0 };
- for (entry = hol->entries, num = hol->num_entries; num > 0; entry++, num--)
- hol_entry_help (entry, state, stream, &hhstate);
- if (hhstate.suppressed_dup_arg && uparams.dup_args_note)
- {
- const char *tstr = dgettext (state == NULL ? NULL
- : state->root_argp->argp_domain, "\
- Mandatory or optional arguments to long options are also mandatory or \
- optional for any corresponding short options.");
- const char *fstr = filter_doc (tstr, ARGP_KEY_HELP_DUP_ARGS_NOTE,
- state ? state->root_argp : 0, state);
- if (fstr && *fstr)
- {
- __argp_fmtstream_putc (stream, '\n');
- __argp_fmtstream_puts (stream, fstr);
- __argp_fmtstream_putc (stream, '\n');
- }
- if (fstr && fstr != tstr)
- free ((char *) fstr);
- }
- }
- static int
- add_argless_short_opt (const struct argp_option *opt,
- const struct argp_option *real,
- const char *domain, void *cookie)
- {
- char **snao_end = cookie;
- if (!(opt->arg || real->arg)
- && !((opt->flags | real->flags) & OPTION_NO_USAGE))
- *(*snao_end)++ = opt->key;
- return 0;
- }
- static int
- usage_argful_short_opt (const struct argp_option *opt,
- const struct argp_option *real,
- const char *domain, void *cookie)
- {
- argp_fmtstream_t stream = cookie;
- const char *arg = opt->arg;
- int flags = opt->flags | real->flags;
- if (! arg)
- arg = real->arg;
- if (arg && !(flags & OPTION_NO_USAGE))
- {
- arg = dgettext (domain, arg);
- if (flags & OPTION_ARG_OPTIONAL)
- __argp_fmtstream_printf (stream, " [-%c[%s]]", opt->key, arg);
- else
- {
-
- space (stream, 6 + strlen (arg));
- __argp_fmtstream_printf (stream, "[-%c %s]", opt->key, arg);
- }
- }
- return 0;
- }
- static int
- usage_long_opt (const struct argp_option *opt,
- const struct argp_option *real,
- const char *domain, void *cookie)
- {
- argp_fmtstream_t stream = cookie;
- const char *arg = opt->arg;
- int flags = opt->flags | real->flags;
- if (! arg)
- arg = real->arg;
- if (! (flags & OPTION_NO_USAGE))
- {
- if (arg)
- {
- arg = dgettext (domain, arg);
- if (flags & OPTION_ARG_OPTIONAL)
- __argp_fmtstream_printf (stream, " [--%s[=%s]]", opt->name, arg);
- else
- __argp_fmtstream_printf (stream, " [--%s=%s]", opt->name, arg);
- }
- else
- __argp_fmtstream_printf (stream, " [--%s]", opt->name);
- }
- return 0;
- }
- static void
- hol_usage (struct hol *hol, argp_fmtstream_t stream)
- {
- if (hol->num_entries > 0)
- {
- unsigned nentries;
- struct hol_entry *entry;
- char *short_no_arg_opts = alloca (strlen (hol->short_options) + 1);
- char *snao_end = short_no_arg_opts;
-
- for (entry = hol->entries, nentries = hol->num_entries
- ; nentries > 0
- ; entry++, nentries--)
- hol_entry_short_iterate (entry, add_argless_short_opt,
- entry->argp->argp_domain, &snao_end);
- if (snao_end > short_no_arg_opts)
- {
- *snao_end++ = 0;
- __argp_fmtstream_printf (stream, " [-%s]", short_no_arg_opts);
- }
-
- for (entry = hol->entries, nentries = hol->num_entries
- ; nentries > 0
- ; entry++, nentries--)
- hol_entry_short_iterate (entry, usage_argful_short_opt,
- entry->argp->argp_domain, stream);
-
- for (entry = hol->entries, nentries = hol->num_entries
- ; nentries > 0
- ; entry++, nentries--)
- hol_entry_long_iterate (entry, usage_long_opt,
- entry->argp->argp_domain, stream);
- }
- }
- static struct hol *
- argp_hol (const struct argp *argp, struct hol_cluster *cluster)
- {
- const struct argp_child *child = argp->children;
- struct hol *hol = make_hol (argp, cluster);
- if (child)
- while (child->argp)
- {
- struct hol_cluster *child_cluster =
- ((child->group || child->header)
- /* Put CHILD->argp within its own cluster. */
- ? hol_add_cluster (hol, child->group, child->header,
- child - argp->children, cluster, argp)
- /* Just merge it into the parent's cluster. */
- : cluster);
- hol_append (hol, argp_hol (child->argp, child_cluster)) ;
- child++;
- }
- return hol;
- }
- static size_t
- argp_args_levels (const struct argp *argp)
- {
- size_t levels = 0;
- const struct argp_child *child = argp->children;
- if (argp->args_doc && strchr (argp->args_doc, '\n'))
- levels++;
- if (child)
- while (child->argp)
- levels += argp_args_levels ((child++)->argp);
- return levels;
- }
- static int
- argp_args_usage (const struct argp *argp, const struct argp_state *state,
- char **levels, int advance, argp_fmtstream_t stream)
- {
- char *our_level = *levels;
- int multiple = 0;
- const struct argp_child *child = argp->children;
- const char *tdoc = dgettext (argp->argp_domain, argp->args_doc), *nl = 0;
- const char *fdoc = filter_doc (tdoc, ARGP_KEY_HELP_ARGS_DOC, argp, state);
- if (fdoc)
- {
- const char *cp = fdoc;
- nl = strchrnul (cp, '\n');
- if (*nl != '\0')
-
- {
- int i;
- multiple = 1;
- for (i = 0; i < *our_level; i++)
- cp = nl + 1, nl = strchrnul (cp, '\n');
- (*levels)++;
- }
-
- space (stream, 1 + nl - cp);
- __argp_fmtstream_write (stream, cp, nl - cp);
- }
- if (fdoc && fdoc != tdoc)
- free ((char *)fdoc);
- if (child)
- while (child->argp)
- advance = !argp_args_usage ((child++)->argp, state, levels, advance, stream);
- if (advance && multiple)
- {
-
- if (*nl)
-
- {
- (*our_level)++;
- advance = 0;
- }
- else if (*our_level > 0)
-
- *our_level = 0;
- }
- return !advance;
- }
- static int
- argp_doc (const struct argp *argp, const struct argp_state *state,
- int post, int pre_blank, int first_only,
- argp_fmtstream_t stream)
- {
- const char *text;
- const char *inp_text;
- void *input = 0;
- int anything = 0;
- size_t inp_text_limit = 0;
- const char *doc = dgettext (argp->argp_domain, argp->doc);
- const struct argp_child *child = argp->children;
- if (doc)
- {
- char *vt = strchr (doc, '\v');
- inp_text = post ? (vt ? vt + 1 : 0) : doc;
- inp_text_limit = (!post && vt) ? (vt - doc) : 0;
- }
- else
- inp_text = 0;
- if (argp->help_filter)
-
- {
- if (inp_text_limit)
-
- inp_text = strndup (inp_text, inp_text_limit);
- input = __argp_input (argp, state);
- text =
- (*argp->help_filter) (post
- ? ARGP_KEY_HELP_POST_DOC
- : ARGP_KEY_HELP_PRE_DOC,
- inp_text, input);
- }
- else
- text = (const char *) inp_text;
- if (text)
- {
- if (pre_blank)
- __argp_fmtstream_putc (stream, '\n');
- if (text == inp_text && inp_text_limit)
- __argp_fmtstream_write (stream, inp_text, inp_text_limit);
- else
- __argp_fmtstream_puts (stream, text);
- if (__argp_fmtstream_point (stream) > __argp_fmtstream_lmargin (stream))
- __argp_fmtstream_putc (stream, '\n');
- anything = 1;
- }
- if (text && text != inp_text)
- free ((char *) text);
- if (inp_text && inp_text_limit && argp->help_filter)
- free ((char *) inp_text);
- if (post && argp->help_filter)
-
- {
- text = (*argp->help_filter) (ARGP_KEY_HELP_EXTRA, 0, input);
- if (text)
- {
- if (anything || pre_blank)
- __argp_fmtstream_putc (stream, '\n');
- __argp_fmtstream_puts (stream, text);
- free ((char *) text);
- if (__argp_fmtstream_point (stream)
- > __argp_fmtstream_lmargin (stream))
- __argp_fmtstream_putc (stream, '\n');
- anything = 1;
- }
- }
- if (child)
- while (child->argp && !(first_only && anything))
- anything |=
- argp_doc ((child++)->argp, state,
- post, anything || pre_blank, first_only,
- stream);
- return anything;
- }
- static void
- _help (const struct argp *argp, const struct argp_state *state, FILE *stream,
- unsigned flags, char *name)
- {
- int anything = 0;
- struct hol *hol = 0;
- argp_fmtstream_t fs;
- if (! stream)
- return;
- #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
- flockfile (stream);
- #endif
- fill_in_uparams (state);
- fs = __argp_make_fmtstream (stream, 0, uparams.rmargin, 0);
- if (! fs)
- {
- #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
- funlockfile (stream);
- #endif
- return;
- }
- if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG))
- {
- hol = argp_hol (argp, 0);
-
- hol_set_group (hol, "help", -1);
- hol_set_group (hol, "version", -1);
- hol_sort (hol);
- }
- if (flags & (ARGP_HELP_USAGE | ARGP_HELP_SHORT_USAGE))
-
- {
- int first_pattern = 1, more_patterns;
- size_t num_pattern_levels = argp_args_levels (argp);
- char *pattern_levels = alloca (num_pattern_levels);
- memset (pattern_levels, 0, num_pattern_levels);
- do
- {
- int old_lm;
- int old_wm = __argp_fmtstream_set_wmargin (fs, uparams.usage_indent);
- char *levels = pattern_levels;
- if (first_pattern)
- __argp_fmtstream_printf (fs, "%s %s",
- dgettext (argp->argp_domain, "Usage:"),
- name);
- else
- __argp_fmtstream_printf (fs, "%s %s",
- dgettext (argp->argp_domain, " or: "),
- name);
-
- old_lm = __argp_fmtstream_set_lmargin (fs, uparams.usage_indent);
- if (flags & ARGP_HELP_SHORT_USAGE)
-
- {
- if (hol->num_entries > 0)
- __argp_fmtstream_puts (fs, dgettext (argp->argp_domain,
- " [OPTION...]"));
- }
- else
-
- {
- hol_usage (hol, fs);
- flags |= ARGP_HELP_SHORT_USAGE;
- }
- more_patterns = argp_args_usage (argp, state, &levels, 1, fs);
- __argp_fmtstream_set_wmargin (fs, old_wm);
- __argp_fmtstream_set_lmargin (fs, old_lm);
- __argp_fmtstream_putc (fs, '\n');
- anything = 1;
- first_pattern = 0;
- }
- while (more_patterns);
- }
- if (flags & ARGP_HELP_PRE_DOC)
- anything |= argp_doc (argp, state, 0, 0, 1, fs);
- if (flags & ARGP_HELP_SEE)
- {
- __argp_fmtstream_printf (fs, dgettext (argp->argp_domain, "\
- Try `%s --help' or `%s --usage' for more information.\n"),
- name, name);
- anything = 1;
- }
- if (flags & ARGP_HELP_LONG)
-
- {
-
- if (hol->num_entries > 0)
- {
- if (anything)
- __argp_fmtstream_putc (fs, '\n');
- hol_help (hol, state, fs);
- anything = 1;
- }
- }
- if (flags & ARGP_HELP_POST_DOC)
-
- anything |= argp_doc (argp, state, 1, anything, 0, fs);
- if ((flags & ARGP_HELP_BUG_ADDR) && argp_program_bug_address)
- {
- if (anything)
- __argp_fmtstream_putc (fs, '\n');
- __argp_fmtstream_printf (fs, dgettext (argp->argp_domain,
- "Report bugs to %s.\n"),
- argp_program_bug_address);
- anything = 1;
- }
- #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
- funlockfile (stream);
- #endif
- if (hol)
- hol_free (hol);
- __argp_fmtstream_free (fs);
- }
- void argp_help (const struct argp *argp, FILE *stream,
- unsigned flags, char *name)
- {
- _help (argp, 0, stream, flags, name);
- }
- char *
- __argp_short_program_name (void)
- {
- # ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
- return (char *) program_invocation_short_name;
- # else
-
- # if __GNUC__
- # warning No reasonable value to return
- # endif
- return "";
- # endif
- }
- void
- argp_state_help (const struct argp_state *state, FILE *stream, unsigned flags)
- {
- if ((!state || ! (state->flags & ARGP_NO_ERRS)) && stream)
- {
- if (state && (state->flags & ARGP_LONG_ONLY))
- flags |= ARGP_HELP_LONG_ONLY;
- _help (state ? state->root_argp : 0, state, stream, flags,
- state ? state->name : __argp_short_program_name ());
- if (!state || ! (state->flags & ARGP_NO_EXIT))
- {
- if (flags & ARGP_HELP_EXIT_ERR)
- exit (argp_err_exit_status);
- if (flags & ARGP_HELP_EXIT_OK)
- exit (0);
- }
- }
- }
- void
- argp_error (const struct argp_state *state, const char *fmt, ...)
- {
- if (!state || !(state->flags & ARGP_NO_ERRS))
- {
- FILE *stream = state ? state->err_stream : stderr;
- if (stream)
- {
- va_list ap;
- #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
- flockfile (stream);
- #endif
- va_start (ap, fmt);
- #if defined _LIBC && defined USE_IN_LIBIO
- char *buf;
- if (_IO_vasprintf (&buf, fmt, ap) < 0)
- buf = NULL;
- __fxprintf (stream, "%s: %s\n",
- state ? state->name : __argp_short_program_name (), buf);
- free (buf);
- #else
- fputs_unlocked (state ? state->name : __argp_short_program_name (),
- stream);
- putc_unlocked (':', stream);
- putc_unlocked (' ', stream);
- vfprintf (stream, fmt, ap);
- putc_unlocked ('\n', stream);
- #endif
- argp_state_help (state, stream, ARGP_HELP_STD_ERR);
- va_end (ap);
- #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
- funlockfile (stream);
- #endif
- }
- }
- }
- void
- argp_failure (const struct argp_state *state, int status, int errnum,
- const char *fmt, ...)
- {
- if (!state || !(state->flags & ARGP_NO_ERRS))
- {
- FILE *stream = state ? state->err_stream : stderr;
- if (stream)
- {
- #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
- flockfile (stream);
- #endif
- #if defined _LIBC && defined USE_IN_LIBIO
- __fxprintf (stream, "%s",
- state ? state->name : __argp_short_program_name ());
- #else
- fputs_unlocked (state ? state->name : __argp_short_program_name (),
- stream);
- #endif
- if (fmt)
- {
- va_list ap;
- va_start (ap, fmt);
- #if defined _LIBC && defined USE_IN_LIBIO
- char *buf;
- if (_IO_vasprintf (&buf, fmt, ap) < 0)
- buf = NULL;
- __fxprintf (stream, ": %s", buf);
- free (buf);
- #else
- putc_unlocked (':', stream);
- putc_unlocked (' ', stream);
- vfprintf (stream, fmt, ap);
- #endif
- va_end (ap);
- }
- if (errnum)
- {
- #if (defined _LIBC && defined USE_IN_LIBIO) || defined HAVE_STRERROR_R
- char buf[200];
- #endif
- #if defined _LIBC && defined USE_IN_LIBIO
- __fxprintf (stream, ": %s",
- strerror_r (errnum, buf, sizeof (buf)));
- #else
- putc_unlocked (':', stream);
- putc_unlocked (' ', stream);
- # ifdef HAVE_STRERROR_R
- fputs (strerror_r (errnum, buf, sizeof (buf)), stream);
- # else
- fputs (strerror (errnum), stream);
- # endif
- #endif
- }
- #ifdef USE_IN_LIBIO
- if (_IO_fwide (stream, 0) > 0)
- putwc_unlocked (L'\n', stream);
- else
- #endif
- putc_unlocked ('\n', stream);
- #if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
- funlockfile (stream);
- #endif
- if (status && (!state || !(state->flags & ARGP_NO_EXIT)))
- exit (status);
- }
- }
- }
|