1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * Glenn Fowler
1N/A * AT&T Research
1N/A *
1N/A * command line option parser and usage formatter
1N/A * its a monster but its all in one place
1N/A * widen your window while you're at it
1N/A */
1N/A
1N/A#include <optlib.h>
1N/A#include <debug.h>
1N/A#include <ccode.h>
1N/A#include <ctype.h>
1N/A#include <errno.h>
1N/A
1N/A#define KEEP "*[A-Za-z][A-Za-z]*"
1N/A#define OMIT "*@(\\[[-+]*\\?*\\]|\\@\\(#\\)|Copyright \\(c\\)|\\$\\I\\d\\: )*"
1N/A
1N/A#define GO '{' /* group nest open */
1N/A#define OG '}' /* group nest close */
1N/A
1N/A#define OPT_WIDTH 80 /* default help text width */
1N/A#define OPT_MARGIN 10 /* default help text margin */
1N/A#define OPT_USAGE 7 /* usage continuation indent */
1N/A
1N/A#define OPT_flag 0x001 /* flag ( 0 or 1 ) */
1N/A#define OPT_hidden 0x002 /* remaining are hidden */
1N/A#define OPT_ignorecase 0x004 /* arg match ignores case */
1N/A#define OPT_invert 0x008 /* flag inverts long sense */
1N/A#define OPT_listof 0x010 /* arg is ' ' or ',' list */
1N/A#define OPT_number 0x020 /* arg is strtonll() number */
1N/A#define OPT_oneof 0x040 /* arg may be set once */
1N/A#define OPT_optional 0x080 /* arg is optional */
1N/A#define OPT_string 0x100 /* arg is string */
1N/A
1N/A#define OPT_preformat 0001 /* output preformat string */
1N/A#define OPT_proprietary 0002 /* proprietary docs */
1N/A
1N/A#define OPT_TYPE (OPT_flag|OPT_number|OPT_string)
1N/A
1N/A#define STYLE_posix 0 /* posix getopt usage */
1N/A#define STYLE_short 1 /* [default] short usage */
1N/A#define STYLE_long 2 /* long usage */
1N/A#define STYLE_match 3 /* long description of matches */
1N/A#define STYLE_options 4 /* short and long descriptions */
1N/A#define STYLE_man 5 /* pretty details */
1N/A#define STYLE_html 6 /* html details */
1N/A#define STYLE_nroff 7 /* nroff details */
1N/A#define STYLE_api 8 /* program details */
1N/A#define STYLE_keys 9 /* translation key strings */
1N/A#define STYLE_usage 10 /* escaped usage string */
1N/A
1N/A#define FONT_BOLD 1
1N/A#define FONT_ITALIC 2
1N/A#define FONT_LITERAL 4
1N/A
1N/A#define sep(c) ((c)=='-'||(c)=='_')
1N/A
1N/Atypedef struct Attr_s
1N/A{
1N/A const char* name;
1N/A int flag;
1N/A} Attr_t;
1N/A
1N/Atypedef struct Help_s
1N/A{
1N/A const char* match; /* builtin help match name */
1N/A const char* name; /* builtin help name */
1N/A int style; /* STYLE_* */
1N/A const char* text; /* --? text */
1N/A unsigned int size; /* strlen text */
1N/A} Help_t;
1N/A
1N/Atypedef struct Font_s
1N/A{
1N/A const char* html[2];
1N/A const char* nroff[2];
1N/A const char* term[2];
1N/A} Font_t;
1N/A
1N/Atypedef struct List_s
1N/A{
1N/A int type; /* { - + : } */
1N/A const char* name; /* list name */
1N/A const char* text; /* help text */
1N/A} List_t;
1N/A
1N/Atypedef struct Msg_s
1N/A{
1N/A const char* text; /* default message text */
1N/A Dtlink_t link; /* cdt link */
1N/A} Msg_t;
1N/A
1N/Atypedef struct Save_s
1N/A{
1N/A Dtlink_t link; /* cdt link */
1N/A char text[1]; /* saved text text */
1N/A} Save_t;
1N/A
1N/Atypedef struct Push_s
1N/A{
1N/A struct Push_s* next; /* next string */
1N/A char* ob; /* next char in old string */
1N/A char* oe; /* end of old string */
1N/A char* nb; /* next char in new string */
1N/A char* ne; /* end of new string */
1N/A int ch; /* localize() translation */
1N/A} Push_t;
1N/A
1N/Atypedef struct Indent_s
1N/A{
1N/A int stop; /* tab column position */
1N/A} Indent_t;
1N/A
1N/Astatic Indent_t indent[] =
1N/A{
1N/A 0,2, 4,10, 12,18, 20,26, 28,34, 36,42, 44,50, 0,0
1N/A};
1N/A
1N/Astatic const char term_off[] = {CC_esc,'[','0','m',0};
1N/Astatic const char term_B_on[] = {CC_esc,'[','1','m',0};
1N/Astatic const char term_I_on[] = {CC_esc,'[','1',';','4','m',0};
1N/A
1N/Astatic const Font_t fonts[] =
1N/A{
1N/A "", "", "", "", "", "",
1N/A "</B>", "<B>", "\\fP", "\\fB", &term_off[0], &term_B_on[0],
1N/A "</I>", "<I>", "\\fP", "\\fI", &term_off[0], &term_I_on[0],
1N/A "", "", "", "", "", "",
1N/A "</TT>","<TT>","\\fP", "\\f5", "", "",
1N/A};
1N/A
1N/Astatic char native[] = "";
1N/A
1N/Astatic unsigned char map[UCHAR_MAX];
1N/A
1N/Astatic Optstate_t state;
1N/A
1N/A#if !_PACKAGE_astsa
1N/A
1N/A#define ID ast.id
1N/A
1N/A#define C(s) ERROR_catalog(s)
1N/A#define D(s) (state.msgdict && dtmatch(state.msgdict, (s)))
1N/A#define T(i,c,m) (X(c)?translate(i,c,C(m)):(m))
1N/A#define X(c) (ERROR_translating()&&(c)!=native)
1N/A#define Z(x) C(x),sizeof(x)-1
1N/A
1N/A/*
1N/A * translate with C_LC_MESSAGES_libast[] check
1N/A */
1N/A
1N/Astatic char*
1N/Atranslate(const char* cmd, const char* cat, const char* msg)
1N/A{
1N/A if (!X(cat))
1N/A return (char*)msg;
1N/A if (cat != (const char*)ID && D(msg))
1N/A cat = (const char*)ID;
1N/A return errorx(NiL, cmd, cat, msg);
1N/A}
1N/A
1N/A#else
1N/A
1N/Astatic char ID[] = "ast";
1N/A
1N/A#define C(s) s
1N/A#define D(s) (state.msgdict && dtmatch(state.msgdict, (s)))
1N/A#define T(i,c,m) m
1N/A#define X(c) 0
1N/A#define Z(x) C(x),sizeof(x)-1
1N/A
1N/A#endif
1N/A
1N/Astatic const List_t help_head[] =
1N/A{
1N/A '-', 0,
1N/A 0,
1N/A '+', C("NAME"),
1N/A C("options available to all \bast\b commands"),
1N/A '+', C("DESCRIPTION"),
1N/A C("\b-?\b and \b--?\b* options are the same \
1N/Afor all \bast\b commands. For any \aitem\a below, if \b--\b\aitem\a is not \
1N/Asupported by a given command then it is equivalent to \b--\?\?\b\aitem\a. The \
1N/A\b--\?\?\b form should be used for portability. All output is written to the \
1N/Astandard error."),
1N/A};
1N/A
1N/Astatic const Help_t styles[] =
1N/A{
1N/A C("about"), "-", STYLE_match,
1N/A Z("List all implementation info."),
1N/A C("api"), "?api", STYLE_api,
1N/A Z("List detailed info in program readable form."),
1N/A C("help"), "", -1,
1N/A Z("List detailed help option info."),
1N/A C("html"), "?html", STYLE_html,
1N/A Z("List detailed info in html."),
1N/A C("keys"), "?keys", STYLE_keys,
1N/A Z("List the usage translation key strings with C style escapes."),
1N/A C("long"), "?long", STYLE_long,
1N/A Z("List long option usage."),
1N/A C("man"), "?man", STYLE_man,
1N/A Z("List detailed info in displayed man page form."),
1N/A C("nroff"), "?nroff", STYLE_nroff,
1N/A Z("List detailed info in nroff."),
1N/A C("options"), "?options", STYLE_options,
1N/A Z("List short and long option details."),
1N/A C("posix"), "?posix", STYLE_posix,
1N/A Z("List posix getopt usage."),
1N/A C("short"), "?short", STYLE_short,
1N/A Z("List short option usage."),
1N/A C("usage"), "?usage", STYLE_usage,
1N/A Z("List the usage string with C style escapes."),
1N/A};
1N/A
1N/Astatic const List_t help_tail[] =
1N/A{
1N/A ':', C("\?\?-\alabel\a"),
1N/A C("List implementation info matching \alabel\a*."),
1N/A ':', C("\?\?\aname\a"),
1N/A C("Equivalent to \b--help=\b\aname\a."),
1N/A ':', C("\?\?"),
1N/A C("Equivalent to \b--\?\?options\b."),
1N/A ':', C("\?\?\?\?"),
1N/A C("Equivalent to \b--\?\?man\b."),
1N/A ':', C("\?\?\?\?\?\?"),
1N/A C("Equivalent to \b--\?\?help\b."),
1N/A ':', C("\?\?\?\?\?\?\aitem\a"),
1N/A C("If the next argument is \b--\b\aoption\a then list \
1N/Athe \aoption\a output in the \aitem\a style. Otherwise print \
1N/A\bversion=\b\an\a where \an\a>0 if \b--\?\?\b\aitem\a is supported, \b0\b \
1N/Aif not."),
1N/A ':', C("\?\?\?\?\?\?ESC"),
1N/A C("Emit escape codes even if output is not a terminal."),
1N/A ':', C("\?\?\?\?\?\?MAN[=\asection\a]]"),
1N/A C("List the \bman\b(1) section title for \asection\a [the \
1N/Acurrent command]]."),
1N/A ':', C("\?\?\?\?\?\?SECTION"),
1N/A C("List the \bman\b(1) section number for the current command."),
1N/A ':', C("\?\?\?\?\?\?TEST"),
1N/A C("Massage the output for regression testing."),
1N/A};
1N/A
1N/Astatic const Attr_t attrs[] =
1N/A{
1N/A "flag", OPT_flag,
1N/A "hidden", OPT_hidden,
1N/A "ignorecase", OPT_ignorecase,
1N/A "invert", OPT_invert,
1N/A "listof", OPT_listof,
1N/A "number", OPT_number,
1N/A "oneof", OPT_oneof,
1N/A "optional", OPT_optional,
1N/A "string", OPT_string,
1N/A};
1N/A
1N/Astatic const char unknown[] = C("unknown option or attribute");
1N/A
1N/Astatic const char* heading[] =
1N/A{
1N/A C("INDEX"),
1N/A C("USER COMMANDS"),
1N/A C("SYSTEM LIBRARY"),
1N/A C("USER LIBRARY"),
1N/A C("FILE FORMATS"),
1N/A C("MISCELLANEOUS"),
1N/A C("GAMES and DEMOS"),
1N/A C("SPECIAL FILES"),
1N/A C("ADMINISTRATIVE COMMANDS"),
1N/A C("GUIs"),
1N/A};
1N/A
1N/A/*
1N/A * list of common man page strings
1N/A * NOTE: add but do not delete from this table
1N/A */
1N/A
1N/Astatic Msg_t C_LC_MESSAGES_libast[] =
1N/A{
1N/A { C("APPLICATION USAGE") },
1N/A { C("ASYNCHRONOUS EVENTS") },
1N/A { C("BUGS") },
1N/A { C("CAVEATS") },
1N/A { C("CONSEQUENCES OF ERRORS") },
1N/A { C("DESCRIPTION") },
1N/A { C("ENVIRONMENT VARIABLES") },
1N/A { C("EXAMPLES") },
1N/A { C("EXIT STATUS") },
1N/A { C("EXTENDED DESCRIPTION") },
1N/A { C("INPUT FILES") },
1N/A { C("LIBRARY") },
1N/A { C("NAME") },
1N/A { C("OPERANDS") },
1N/A { C("OPTIONS") },
1N/A { C("OUTPUT FILES") },
1N/A { C("PLUGIN") },
1N/A { C("SEE ALSO") },
1N/A { C("STDERR") },
1N/A { C("STDIN") },
1N/A { C("STDOUT") },
1N/A { C("SYNOPSIS") },
1N/A { C("author") },
1N/A { C("copyright") },
1N/A { C("license") },
1N/A { C("name") },
1N/A { C("path") },
1N/A { C("version") },
1N/A};
1N/A
1N/A/*
1N/A * 2007-03-19 move opt_info from _opt_info_ to (*_opt_data_)
1N/A * to allow future Opt_t growth
1N/A * by 2009 _opt_info_ can be static
1N/A */
1N/A
1N/A#if _BLD_ast && defined(__EXPORT__)
1N/A#define extern extern __EXPORT__
1N/A#endif
1N/A
1N/Aextern Opt_t _opt_info_;
1N/A
1N/AOpt_t _opt_info_ = { 0,0,0,0,0,0,0,{0},{0},0,0,0,{0},{0},&state };
1N/A
1N/A#undef extern
1N/A
1N/A__EXTERN__(Opt_t, _opt_info_);
1N/A
1N/A__EXTERN__(Opt_t*, _opt_infop_);
1N/A
1N/AOpt_t* _opt_infop_ = &_opt_info_;
1N/A
1N/AOptstate_t*
1N/Aoptstate(Opt_t* p)
1N/A{
1N/A return &state;
1N/A}
1N/A
1N/A#if DEBUG || _BLD_DEBUG
1N/A
1N/A/*
1N/A * debug usage string segment format
1N/A */
1N/A
1N/Astatic char*
1N/Ashow(register char* s)
1N/A{
1N/A register int c;
1N/A register char* t;
1N/A register char* e;
1N/A
1N/A static char buf[32];
1N/A
1N/A if (!s)
1N/A return "(null)";
1N/A t = buf;
1N/A e = buf + sizeof(buf) - 2;
1N/A while (t < e)
1N/A {
1N/A switch (c = *s++)
1N/A {
1N/A case 0:
1N/A goto done;
1N/A case '\a':
1N/A *t++ = '\\';
1N/A c = 'a';
1N/A break;
1N/A case '\b':
1N/A *t++ = '\\';
1N/A c = 'b';
1N/A break;
1N/A case '\f':
1N/A *t++ = '\\';
1N/A c = 'f';
1N/A break;
1N/A case '\n':
1N/A *t++ = '\\';
1N/A c = 'n';
1N/A break;
1N/A case '\t':
1N/A *t++ = '\\';
1N/A c = 't';
1N/A break;
1N/A case '\v':
1N/A *t++ = '\\';
1N/A c = 'v';
1N/A break;
1N/A }
1N/A *t++ = c;
1N/A }
1N/A done:
1N/A *t = 0;
1N/A return buf;
1N/A}
1N/A
1N/A#endif
1N/A
1N/Atypedef struct Section_s
1N/A{
1N/A const char section[4];
1N/A const char* name;
1N/A} Section_t;
1N/A
1N/Astatic const Section_t sections[] =
1N/A{
1N/A "1M", "MAKE ASSERTION OPERATORS AND RULES",
1N/A "1", "USER COMMANDS",
1N/A "2", "SYSTEM CALLS",
1N/A "3F", "FORTRAN LIBRARY ROUTINES",
1N/A "3K", "KERNEL VM LIBRARY FUNCTIONS",
1N/A "3L", "LIGHTWEIGHT PROCESSES LIBRARY",
1N/A "3M", "MATHEMATICAL LIBRARY",
1N/A "3N", "NETWORK FUNCTIONS",
1N/A "3R", "RPC SERVICES LIBRARY",
1N/A "3S", "STANDARD I/O FUNCTIONS",
1N/A "3V", "SYSTEM V LIBRARY",
1N/A "3", "C LIBRARY FUNCTIONS",
1N/A "4F", "PROTOCOL FAMILIES",
1N/A "4P", "PROTOCOLS",
1N/A "4", "DEVICES AND NETWORK INTERFACES",
1N/A "5P", "PLUGINS",
1N/A "5", "FILE FORMATS",
1N/A "6", "GAMES AND DEMOS",
1N/A "7", "PUBLIC FILES AND TABLES",
1N/A "8", "ADMINISTRATIVE COMMANDS",
1N/A "L", "LOCAL COMMANDS",
1N/A};
1N/A
1N/A/*
1N/A * return section name given abbreviation
1N/A */
1N/A
1N/Astatic char*
1N/Asecname(char* section)
1N/A{
1N/A int i;
1N/A char* b;
1N/A char* t;
1N/A const char* s;
1N/A
1N/A b = t = fmtbuf(64);
1N/A if (section[1])
1N/A {
1N/A switch (section[2] ? section[2] : section[1])
1N/A {
1N/A case 'C':
1N/A s = "COMPATIBILITY ";
1N/A break;
1N/A case 'U':
1N/A s = "UWIN ";
1N/A break;
1N/A case 'X':
1N/A s = "MISCELLANEOUS ";
1N/A break;
1N/A default:
1N/A s = 0;
1N/A break;
1N/A }
1N/A if (s)
1N/A t = strcopy(t, s);
1N/A }
1N/A s = 0;
1N/A for (i = 0; i < elementsof(sections); i++)
1N/A if (section[0] == sections[i].section[0] && (section[1] == sections[i].section[1] || !sections[i].section[1]))
1N/A {
1N/A s = sections[i].name;
1N/A break;
1N/A }
1N/A if (!s)
1N/A {
1N/A t = strcopy(t, "SECTION ");
1N/A s = section;
1N/A }
1N/A strcopy(t, s);
1N/A return b;
1N/A}
1N/A
1N/A/*
1N/A * pop the push stack
1N/A */
1N/A
1N/Astatic Push_t*
1N/Apop(register Push_t* psp)
1N/A{
1N/A register Push_t* tsp;
1N/A
1N/A while (tsp = psp)
1N/A {
1N/A psp = psp->next;
1N/A free(tsp);
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * skip over line space to the next token
1N/A */
1N/A
1N/Astatic char*
1N/Anext(register char* s, int version)
1N/A{
1N/A register char* b;
1N/A
1N/A while (*s == '\t' || *s == '\r' || version >= 1 && *s == ' ')
1N/A s++;
1N/A if (*s == '\n')
1N/A {
1N/A b = s;
1N/A while (*++s == ' ' || *s == '\t' || *s == '\r');
1N/A if (*s == '\n')
1N/A return b;
1N/A }
1N/A return s;
1N/A}
1N/A
1N/A/*
1N/A * skip to t1 or t2 or t3, whichever first, in s
1N/A * n==0 outside [...]
1N/A * n==1 inside [...] before ?
1N/A * n==2 inside [...] after ?
1N/A * b==0 outside {...}
1N/A * b==1 inside {...}
1N/A * past skips past the terminator to the next token
1N/A * otherwise a pointer to the terminator is returned
1N/A *
1N/A * ]] for ] inside [...]
1N/A * ?? for ? inside [...] before ?
1N/A * :: for : inside [...] before ?
1N/A */
1N/A
1N/Astatic char*
1N/Askip(register char* s, register int t1, register int t2, register int t3, register int n, register int b, int past, int version)
1N/A{
1N/A register int c;
1N/A register int on = n;
1N/A register int ob = b;
1N/A
1N/A if (version < 1)
1N/A {
1N/A n = n >= 1;
1N/A for (;;)
1N/A {
1N/A switch (*s++)
1N/A {
1N/A case 0:
1N/A break;
1N/A case '[':
1N/A n++;
1N/A continue;
1N/A case ']':
1N/A if (--n <= 0)
1N/A break;
1N/A continue;
1N/A default:
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A }
1N/A else while (c = *s++)
1N/A {
1N/A message((-22, "optget: skip t1=%c t2=%c t3=%c n=%d b=%d `%s'", t1 ? t1 : '@', t2 ? t2 : '@', t3 ? t3 : '@', n, b, show(s - 1)));
1N/A if (c == '[')
1N/A {
1N/A if (!n)
1N/A n = 1;
1N/A }
1N/A else if (c == ']')
1N/A {
1N/A if (n)
1N/A {
1N/A if (*s == ']')
1N/A s++;
1N/A else if (on == 1)
1N/A break;
1N/A else
1N/A n = 0;
1N/A }
1N/A }
1N/A else if (c == GO)
1N/A {
1N/A if (n == 0)
1N/A b++;
1N/A }
1N/A else if (c == OG)
1N/A {
1N/A if (n == 0 && b-- == ob)
1N/A break;
1N/A }
1N/A else if (c == '?')
1N/A {
1N/A if (n == 1)
1N/A {
1N/A if (*s == '?')
1N/A s++;
1N/A else
1N/A {
1N/A if (n == on && (c == t1 || c == t2 || c == t3))
1N/A break;
1N/A n = 2;
1N/A }
1N/A }
1N/A }
1N/A else if (n == on && (c == t1 || c == t2 || c == t3))
1N/A {
1N/A if (n == 1 && c == ':' && *s == c)
1N/A s++;
1N/A else
1N/A break;
1N/A }
1N/A }
1N/A return past && *(s - 1) ? next(s, version) : s - 1;
1N/A}
1N/A
1N/A/*
1N/A * *s points to '(' on input
1N/A * return is one past matching ')'
1N/A */
1N/A
1N/Astatic char*
1N/Anest(register char* s)
1N/A{
1N/A int n;
1N/A
1N/A n = 0;
1N/A for (;;)
1N/A {
1N/A switch (*s++)
1N/A {
1N/A case '(':
1N/A n++;
1N/A continue;
1N/A case ')':
1N/A if (!--n)
1N/A break;
1N/A continue;
1N/A default:
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A return s;
1N/A}
1N/A
1N/A/*
1N/A * match s with t
1N/A * t translated if possible
1N/A * embedded { - _ ' } ignored
1N/A * * separates required prefix from optional suffix
1N/A * otherwise prefix match
1N/A */
1N/A
1N/Astatic int
1N/Amatch(char* s, char* t, int version, const char* id, const char* catalog)
1N/A{
1N/A register char* w;
1N/A register char* x;
1N/A char* xw;
1N/A char* ww;
1N/A int n;
1N/A int v;
1N/A int j;
1N/A
1N/A for (n = 0; n < 2; n++)
1N/A {
1N/A if (n)
1N/A x = t;
1N/A else
1N/A {
1N/A if (catalog)
1N/A {
1N/A w = skip(t, ':', '?', 0, 1, 0, 0, version);
1N/A w = sfprints("%-.*s", w - t, t);
1N/A x = T(id, catalog, w);
1N/A if (x == w)
1N/A continue;
1N/A }
1N/A x = T(NiL, ID, t);
1N/A if (x == t)
1N/A continue;
1N/A }
1N/A do
1N/A {
1N/A v = 0;
1N/A xw = x;
1N/A w = ww = s;
1N/A while (*x && *w)
1N/A {
1N/A if (isupper(*x))
1N/A xw = x;
1N/A if (isupper(*w))
1N/A ww = w;
1N/A if (*x == '*' && !v++ || *x == '\a')
1N/A {
1N/A if (*x == '\a')
1N/A do
1N/A {
1N/A if (!*++x)
1N/A {
1N/A x--;
1N/A break;
1N/A }
1N/A } while (*x != '\a');
1N/A j = *(x + 1);
1N/A if (j == ':' || j == '|' || j == '?' || j == ']' || j == 0)
1N/A while (*w)
1N/A w++;
1N/A }
1N/A else if (sep(*x))
1N/A xw = ++x;
1N/A else if (sep(*w) && w != s)
1N/A ww = ++w;
1N/A else if (*x == *w)
1N/A {
1N/A x++;
1N/A w++;
1N/A }
1N/A else if (w == ww && x == xw)
1N/A break;
1N/A else
1N/A {
1N/A if (x != xw)
1N/A {
1N/A while (*x && !sep(*x) && !isupper(*x))
1N/A x++;
1N/A if (!*x)
1N/A break;
1N/A if (sep(*x))
1N/A x++;
1N/A xw = x;
1N/A }
1N/A while (w > ww && *w != *x)
1N/A w--;
1N/A }
1N/A }
1N/A if (!*w)
1N/A {
1N/A if (!v)
1N/A {
1N/A for (;;)
1N/A {
1N/A switch (*x++)
1N/A {
1N/A case 0:
1N/A case ':':
1N/A case '|':
1N/A case '?':
1N/A case ']':
1N/A return 1;
1N/A case '*':
1N/A break;
1N/A default:
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A break;
1N/A }
1N/A return 1;
1N/A }
1N/A } while (*(x = skip(x, '|', 0, 0, 1, 0, 0, version)) == '|' && x++);
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * prefix search for s in tab with num elements of size
1N/A * with optional translation
1N/A */
1N/A
1N/Astatic void*
1N/Asearch(const void* tab, size_t num, size_t siz, char* s)
1N/A{
1N/A register char* p;
1N/A register char* e;
1N/A
1N/A for (e = (p = (char*)tab) + num * siz; p < e; p += siz)
1N/A if (match(s, *((char**)p), -1, NiL, NiL))
1N/A return (void*)p;
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * save ap+bp+cp and return the saved pointer
1N/A */
1N/A
1N/Astatic char*
1N/Asave(const char* ap, size_t az, const char* bp, size_t bz, const char* cp, size_t cz)
1N/A{
1N/A char* b;
1N/A char* e;
1N/A const char* ep;
1N/A Save_t* p;
1N/A Dtdisc_t* d;
1N/A char buf[1024];
1N/A
1N/A static Dt_t* dict;
1N/A
1N/A if (!dict)
1N/A {
1N/A if (!(d = newof(0, Dtdisc_t, 1, 0)))
1N/A return (char*)ap;
1N/A d->key = offsetof(Save_t, text);
1N/A if (!(dict = dtopen(d, Dthash)))
1N/A return (char*)ap;
1N/A }
1N/A b = buf;
1N/A e = b + sizeof(buf) - 1;
1N/A for (ep = ap + az; b < e && ap < ep; *b++ = *ap++);
1N/A if (bp)
1N/A {
1N/A for (ep = bp + bz; b < e && bp < ep; *b++ = *bp++);
1N/A if (cp)
1N/A for (ep = cp + cz; b < e && cp < ep; *b++ = *cp++);
1N/A }
1N/A *b = 0;
1N/A if (!(p = (Save_t*)dtmatch(dict, buf)))
1N/A {
1N/A if (!(p = newof(0, Save_t, 1, b - buf)))
1N/A return (char*)ap;
1N/A strcpy(p->text, buf);
1N/A dtinsert(dict, p);
1N/A }
1N/A return p->text;
1N/A}
1N/A
1N/A/*
1N/A * expand \f...\f info
1N/A * *p set to next char after second \f
1N/A * expanded value returned
1N/A */
1N/A
1N/Astatic char*
1N/Aexpand(register char* s, register char* e, char** p, Sfio_t* ip, char* id)
1N/A{
1N/A register int c;
1N/A register char* b = s;
1N/A int n;
1N/A
1N/A n = sfstrtell(ip);
1N/A c = 1;
1N/A while ((!e || s < e) && (c = *s++) && c != '\f');
1N/A sfwrite(ip, b, s - b - 1);
1N/A sfputc(ip, 0);
1N/A b = sfstrbase(ip) + n;
1N/A n = sfstrtell(ip);
1N/A if (!c)
1N/A s--;
1N/A if (*b == '?')
1N/A {
1N/A if (!*++b || streq(b, "NAME"))
1N/A {
1N/A if (!(b = id))
1N/A b = "command";
1N/A sfstrseek(ip, 0, SEEK_SET);
1N/A sfputr(ip, b, -1);
1N/A n = 0;
1N/A }
1N/A else
1N/A n = 1;
1N/A }
1N/A else if (!opt_info.disc || !opt_info.disc->infof || (*opt_info.disc->infof)(&opt_info, ip, b, opt_info.disc) < 0)
1N/A n = 0;
1N/A *p = s;
1N/A if (s = sfstruse(ip))
1N/A s += n;
1N/A else
1N/A s = "error";
1N/A return s;
1N/A}
1N/A
1N/A/*
1N/A * initialize the translation dictionary and flag maps
1N/A */
1N/A
1N/Astatic void
1N/Ainitdict(void)
1N/A{
1N/A register int n;
1N/A
1N/A state.vp = sfstropen();
1N/A state.msgdisc.key = offsetof(Msg_t, text);
1N/A state.msgdisc.size = -1;
1N/A state.msgdisc.link = offsetof(Msg_t, link);
1N/A if (state.msgdict = dtopen(&state.msgdisc, Dthash))
1N/A for (n = 0; n < elementsof(C_LC_MESSAGES_libast); n++)
1N/A dtinsert(state.msgdict, C_LC_MESSAGES_libast + n);
1N/A}
1N/A
1N/A/*
1N/A * initialize the attributes for pass p from opt string s
1N/A */
1N/A
1N/Astatic int
1N/Ainit(register char* s, Optpass_t* p)
1N/A{
1N/A register char* t;
1N/A register char* u;
1N/A register int c;
1N/A register int a;
1N/A register int n;
1N/A char* e;
1N/A int l;
1N/A
1N/A if (!state.localized)
1N/A {
1N/A state.localized = 1;
1N/A#if !_PACKAGE_astsa
1N/A if (!ast.locale.serial)
1N/A setlocale(LC_ALL, "");
1N/A#endif
1N/A state.xp = sfstropen();
1N/A if (!map[OPT_FLAGS[0]])
1N/A for (n = 0, t = OPT_FLAGS; *t; t++)
1N/A map[*t] = ++n;
1N/A }
1N/A#if _BLD_DEBUG
1N/A error(-1, "optget debug");
1N/A#endif
1N/A p->oopts = s;
1N/A p->version = 0;
1N/A p->prefix = 2;
1N/A p->section[0] = '1';
1N/A p->section[1] = 0;
1N/A p->flags = 0;
1N/A p->id = error_info.id;
1N/A p->catalog = 0;
1N/A s = next(s, 0);
1N/A if (*s == ':')
1N/A s++;
1N/A if (*s == '+')
1N/A s++;
1N/A s = next(s, 0);
1N/A if (*s++ == '[')
1N/A {
1N/A if (*s == '+')
1N/A p->version = 1;
1N/A else if (*s++ == '-')
1N/A {
1N/A if (*s == '?' || *s == ']')
1N/A p->version = 1;
1N/A else
1N/A {
1N/A if (!isdigit(*s))
1N/A p->version = 1;
1N/A else
1N/A while (isdigit(*s))
1N/A p->version = p->version * 10 + (*s++ - '0');
1N/A while (*s && *s != ']')
1N/A {
1N/A if ((c = *s++) == '?')
1N/A {
1N/A p->release = s;
1N/A while (*s && *s != ']')
1N/A if (isspace(*s++))
1N/A p->release = s;
1N/A break;
1N/A }
1N/A else if (!isdigit(*s))
1N/A n = 1;
1N/A else
1N/A {
1N/A n = 0;
1N/A while (isdigit(*s))
1N/A n = n * 10 + (*s++ - '0');
1N/A }
1N/A switch (c)
1N/A {
1N/A case '+':
1N/A p->flags |= OPT_plus;
1N/A break;
1N/A case 'a':
1N/A p->flags |= OPT_append;
1N/A break;
1N/A case 'c':
1N/A p->flags |= OPT_cache;
1N/A break;
1N/A case 'i':
1N/A p->flags |= OPT_ignore;
1N/A break;
1N/A case 'l':
1N/A p->flags |= OPT_long;
1N/A break;
1N/A case 'm':
1N/A p->flags |= OPT_module;
1N/A break;
1N/A case 'n':
1N/A p->flags |= OPT_numeric;
1N/A break;
1N/A case 'o':
1N/A p->flags |= OPT_old;
1N/A break;
1N/A case 'p':
1N/A p->prefix = n;
1N/A break;
1N/A case 's':
1N/A if (n > 1 && n < 5)
1N/A {
1N/A p->flags |= OPT_functions;
1N/A p->prefix = 0;
1N/A }
1N/A p->section[0] = '0' + (n % 10);
1N/A n = 1;
1N/A if (isupper(*s))
1N/A p->section[n++] = *s++;
1N/A if (isupper(*s))
1N/A p->section[n++] = *s++;
1N/A p->section[n] = 0;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A while (*s)
1N/A if (*s++ == ']')
1N/A {
1N/A while (isspace(*s))
1N/A s++;
1N/A if (*s++ == '[')
1N/A {
1N/A if (*s++ != '-')
1N/A {
1N/A l = 0;
1N/A if (strneq(s - 1, "+NAME?", 6) && (s += 5) || strneq(s - 1, "+LIBRARY?", 9) && (s += 8) && (l = 1) || strneq(s - 1, "+PLUGIN?", 8) && (s += 7) && (l = 1))
1N/A {
1N/A for (; *s == '\a' || *s == '\b' || *s == '\v' || *s == ' '; s++);
1N/A if (*s == '\f')
1N/A {
1N/A if (*(s + 1) == '?' && *(s + 2) == '\f')
1N/A break;
1N/A s = expand(s + 1, NiL, &e, state.xp, p->id);
1N/A }
1N/A for (t = s; *t && *t != ' ' && *t != ']'; t++);
1N/A if (t > s)
1N/A {
1N/A u = t;
1N/A if (*(t - 1) == '\a' || *(t - 1) == '\b' || *(t - 1) == '\v')
1N/A t--;
1N/A if (t > s)
1N/A {
1N/A while (*u == ' ' || *u == '\\')
1N/A u++;
1N/A if (*u == '-' || *u == ']')
1N/A {
1N/A if (!l)
1N/A p->id = save(s, t - s, 0, 0, 0, 0);
1N/A else if ((a = strlen(p->id)) <= (n = t - s) || strncmp(p->id + a - n, s, n) || *(p->id + a - n - 1) != ':')
1N/A p->id = save(p->id, strlen(p->id), "::", 2, s, t - s);
1N/A }
1N/A }
1N/A }
1N/A }
1N/A break;
1N/A }
1N/A if (*s == '-')
1N/A s++;
1N/A if (strneq(s, "catalog?", 8))
1N/A p->catalog = s += 8;
1N/A }
1N/A }
1N/A }
1N/A if (!error_info.id)
1N/A {
1N/A if (!(error_info.id = p->id))
1N/A p->id = "command";
1N/A }
1N/A else if (p->id == error_info.id)
1N/A p->id = save(p->id, strlen(p->id), 0, 0, 0, 0);
1N/A if (s = p->catalog)
1N/A p->catalog = ((t = strchr(s, ']')) && (!p->id || (t - s) != strlen(p->id) || !strneq(s, p->id, t - s))) ? save(s, t - s, 0, 0, 0, 0) : (char*)0;
1N/A if (!p->catalog)
1N/A {
1N/A if (opt_info.disc && opt_info.disc->catalog && (!p->id || !streq(opt_info.disc->catalog, p->id)))
1N/A p->catalog = opt_info.disc->catalog;
1N/A else
1N/A p->catalog = ID;
1N/A }
1N/A s = p->oopts;
1N/A if (*s == ':')
1N/A s++;
1N/A if (*s == '+')
1N/A {
1N/A s++;
1N/A p->flags |= OPT_plus;
1N/A }
1N/A s = next(s, 0);
1N/A if (*s != '[')
1N/A for (t = s, a = 0; *t; t++)
1N/A if (!a && *t == '-')
1N/A {
1N/A p->flags |= OPT_minus;
1N/A break;
1N/A }
1N/A else if (*t == '[')
1N/A a++;
1N/A else if (*t == ']')
1N/A a--;
1N/A if (!p->version && (t = strchr(s, '(')) && strchr(t, ')') && (state.cp || (state.cp = sfstropen())))
1N/A {
1N/A /*
1N/A * solaris long option compatibility
1N/A */
1N/A
1N/A p->version = 1;
1N/A for (t = p->oopts; t < s; t++)
1N/A sfputc(state.cp, *t);
1N/A n = t - p->oopts;
1N/A sfputc(state.cp, '[');
1N/A sfputc(state.cp, '-');
1N/A sfputc(state.cp, ']');
1N/A c = *s++;
1N/A while (c)
1N/A {
1N/A sfputc(state.cp, '[');
1N/A sfputc(state.cp, c);
1N/A if (a = (c = *s++) == ':')
1N/A c = *s++;
1N/A if (c == '(')
1N/A {
1N/A sfputc(state.cp, ':');
1N/A for (;;)
1N/A {
1N/A while ((c = *s++) && c != ')')
1N/A sfputc(state.cp, c);
1N/A if (!c || (c = *s++) != '(')
1N/A break;
1N/A sfputc(state.cp, '|');
1N/A }
1N/A }
1N/A sfputc(state.cp, ']');
1N/A if (a)
1N/A sfputr(state.cp, ":[string]", -1);
1N/A }
1N/A if (!(p->oopts = s = sfstruse(state.cp)))
1N/A return -1;
1N/A s += n;
1N/A }
1N/A p->opts = s;
1N/A message((-1, "version=%d prefix=%d section=%s flags=%04x id=%s catalog=%s oopts=%p", p->version, p->prefix, p->section, p->flags, p->id, p->catalog, p->oopts));
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * return the bold set/unset sequence for style
1N/A */
1N/A
1N/Astatic const char*
1N/Afont(int f, int style, int set)
1N/A{
1N/A switch (style)
1N/A {
1N/A case STYLE_html:
1N/A return fonts[f].html[set];
1N/A case STYLE_nroff:
1N/A return fonts[f].nroff[set];
1N/A case STYLE_short:
1N/A case STYLE_long:
1N/A case STYLE_posix:
1N/A case STYLE_api:
1N/A break;
1N/A default:
1N/A if (state.emphasis > 0)
1N/A return fonts[f].term[set];
1N/A break;
1N/A }
1N/A return "";
1N/A}
1N/A
1N/A/*
1N/A * push \f...\f info
1N/A */
1N/A
1N/Astatic Push_t*
1N/Ainfo(Push_t* psp, char* s, char* e, Sfio_t* ip, char* id)
1N/A{
1N/A register char* b;
1N/A int n;
1N/A Push_t* tsp;
1N/A
1N/A static Push_t push;
1N/A
1N/A b = expand(s, e, &s, ip, id);
1N/A n = strlen(b);
1N/A if (tsp = newof(0, Push_t, 1, n + 1))
1N/A {
1N/A tsp->nb = (char*)(tsp + 1);
1N/A tsp->ne = tsp->nb + n;
1N/A strcpy(tsp->nb, b);
1N/A }
1N/A else
1N/A tsp = &push;
1N/A tsp->next = psp;
1N/A tsp->ob = s;
1N/A tsp->oe = e;
1N/A return tsp;
1N/A}
1N/A
1N/A/*
1N/A * push translation
1N/A */
1N/A
1N/Astatic Push_t*
1N/Alocalize(Push_t* psp, char* s, char* e, int term, int n, Sfio_t* ip, int version, char* id, char* catalog)
1N/A{
1N/A char* t;
1N/A char* u;
1N/A Push_t* tsp;
1N/A int c;
1N/A
1N/A t = skip(s, term, 0, 0, n, 0, 0, version);
1N/A if (e && t > e)
1N/A t = e;
1N/A while (s < t)
1N/A {
1N/A switch (c = *s++)
1N/A {
1N/A case ':':
1N/A case '?':
1N/A if (term && *s == c)
1N/A s++;
1N/A break;
1N/A case ']':
1N/A if (*s == c)
1N/A s++;
1N/A break;
1N/A }
1N/A sfputc(ip, c);
1N/A }
1N/A if (!(s = sfstruse(ip)) || (u = T(id, catalog, s)) == s)
1N/A return 0;
1N/A n = strlen(u);
1N/A if (tsp = newof(0, Push_t, 1, n + 1))
1N/A {
1N/A tsp->nb = (char*)(tsp + 1);
1N/A tsp->ne = tsp->nb + n;
1N/A strcpy(tsp->nb, u);
1N/A tsp->ob = t;
1N/A tsp->oe = e;
1N/A tsp->ch = 1;
1N/A }
1N/A tsp->next = psp;
1N/A return tsp;
1N/A}
1N/A
1N/A/*
1N/A * output label s from [ ...label...[?...] ] to sp
1N/A * 1 returned if the label was translated
1N/A */
1N/A
1N/Astatic int
1N/Alabel(register Sfio_t* sp, int sep, register char* s, int about, int z, int level, int style, int f, Sfio_t* ip, int version, char* id, char* catalog)
1N/A{
1N/A register int c;
1N/A register char* t;
1N/A register char* e;
1N/A int ostyle;
1N/A int a;
1N/A int i;
1N/A char* p;
1N/A char* q;
1N/A char* w;
1N/A char* y;
1N/A int va;
1N/A Push_t* tsp;
1N/A
1N/A int r = 0;
1N/A int n = 1;
1N/A Push_t* psp = 0;
1N/A
1N/A if ((ostyle = style) > (STYLE_nroff - (sep <= 0)) && f != FONT_LITERAL && f >= 0)
1N/A style = 0;
1N/A if (z < 0)
1N/A e = s + strlen(s);
1N/A else
1N/A e = s + z;
1N/A if (sep > 0)
1N/A {
1N/A if (sep == ' ' && style == STYLE_nroff)
1N/A sfputc(sp, '\\');
1N/A sfputc(sp, sep);
1N/A }
1N/A sep = !sep || z < 0;
1N/A va = 0;
1N/A y = 0;
1N/A if (about)
1N/A sfputc(sp, '(');
1N/A if (version < 1)
1N/A {
1N/A a = 0;
1N/A for (;;)
1N/A {
1N/A if (s >= e)
1N/A return r;
1N/A switch (c = *s++)
1N/A {
1N/A case '[':
1N/A a++;
1N/A break;
1N/A case ']':
1N/A if (--a < 0)
1N/A return r;
1N/A break;
1N/A }
1N/A sfputc(sp, c);
1N/A }
1N/A }
1N/A else if (level && (*(p = skip(s, 0, 0, 0, 1, level, 1, version)) == ':' || *p == '#'))
1N/A {
1N/A va = 0;
1N/A if (*++p == '?' || *p == *(p - 1))
1N/A {
1N/A p++;
1N/A va |= OPT_optional;
1N/A }
1N/A if (*(p = next(p, version)) == '[')
1N/A y = p + 1;
1N/A }
1N/A if (X(catalog) && (!level || *s == '\a' || *(s - 1) != '+') &&
1N/A (tsp = localize(psp, s, e, (sep || level) ? '?' : 0, sep || level, ip, version, id, catalog)))
1N/A {
1N/A psp = tsp;
1N/A s = psp->nb;
1N/A e = psp->ne;
1N/A r = psp->ch > 0;
1N/A }
1N/A switch (*s)
1N/A {
1N/A case '\a':
1N/A if (f == FONT_ITALIC || f < 0)
1N/A s++;
1N/A if (f > 0)
1N/A f = 0;
1N/A break;
1N/A case '\b':
1N/A if (f == FONT_BOLD || f < 0)
1N/A s++;
1N/A if (f > 0)
1N/A f = 0;
1N/A break;
1N/A case '\v':
1N/A if (f == FONT_LITERAL || f < 0)
1N/A s++;
1N/A if (f > 0)
1N/A f = 0;
1N/A break;
1N/A default:
1N/A if (f > 0)
1N/A sfputr(sp, font(f, style, 1), -1);
1N/A break;
1N/A }
1N/A for (;;)
1N/A {
1N/A if (s >= e)
1N/A {
1N/A if (!(tsp = psp))
1N/A goto restore;
1N/A s = psp->ob;
1N/A e = psp->oe;
1N/A psp = psp->next;
1N/A free(tsp);
1N/A continue;
1N/A }
1N/A switch (c = *s++)
1N/A {
1N/A case '(':
1N/A if (n)
1N/A {
1N/A n = 0;
1N/A if (f > 0)
1N/A {
1N/A sfputr(sp, font(f, style, 0), -1);
1N/A f = 0;
1N/A }
1N/A }
1N/A break;
1N/A case '?':
1N/A case ':':
1N/A case ']':
1N/A if (psp && psp->ch)
1N/A break;
1N/A if (y)
1N/A {
1N/A if (va & OPT_optional)
1N/A sfputc(sp, '[');
1N/A sfputc(sp, '=');
1N/A label(sp, 0, y, 0, -1, 0, style, f >= 0 ? FONT_ITALIC : f, ip, version, id, catalog);
1N/A if (va & OPT_optional)
1N/A sfputc(sp, ']');
1N/A y = 0;
1N/A }
1N/A switch (c)
1N/A {
1N/A case '?':
1N/A if (*s == '?')
1N/A s++;
1N/A else if (*s == ']' && *(s + 1) != ']')
1N/A continue;
1N/A else if (sep)
1N/A goto restore;
1N/A else if (X(catalog) && (tsp = localize(psp, s, e, 0, 1, ip, version, id, catalog)))
1N/A {
1N/A psp = tsp;
1N/A s = psp->nb;
1N/A e = psp->ne;
1N/A }
1N/A break;
1N/A case ']':
1N/A if (sep && *s++ != ']')
1N/A goto restore;
1N/A break;
1N/A case ':':
1N/A if (sep && *s++ != ':')
1N/A goto restore;
1N/A break;
1N/A }
1N/A break;
1N/A case '\a':
1N/A a = FONT_ITALIC;
1N/A setfont:
1N/A if (f >= 0)
1N/A {
1N/A if (f & ~a)
1N/A {
1N/A sfputr(sp, font(f, style, 0), -1);
1N/A f = 0;
1N/A }
1N/A if (!f && style == STYLE_html)
1N/A {
1N/A for (t = s; t < e && !isspace(*t) && !iscntrl(*t); t++);
1N/A if (*t == c && *++t == '(')
1N/A {
1N/A w = t;
1N/A if (++t < e && isdigit(*t))
1N/A while (++t < e && isupper(*t));
1N/A if (t < e && *t == ')' && t > w + 1)
1N/A {
1N/A sfprintf(sp, "<NOBR><A href=\"../man%-.*s/"
1N/A , t - w - 1, w + 1
1N/A );
1N/A for (q = s; q < w - 1; q++)
1N/A if (*q == ':' && q < w - 2 && *(q + 1) == ':')
1N/A {
1N/A sfputc(sp, '-');
1N/A q++;
1N/A }
1N/A else
1N/A sfputc(sp, *q);
1N/A sfprintf(sp, ".html\">%s%-.*s%s</A>%-.*s</NOBR>"
1N/A , font(a, style, 1)
1N/A , w - s - 1, s
1N/A , font(a, style, 0)
1N/A , t - w + 1, w
1N/A );
1N/A s = t + 1;
1N/A continue;
1N/A }
1N/A }
1N/A }
1N/A sfputr(sp, font(a, style, !!(f ^= a)), -1);
1N/A }
1N/A continue;
1N/A case '\b':
1N/A a = FONT_BOLD;
1N/A goto setfont;
1N/A case '\f':
1N/A psp = info(psp, s, e, ip, id);
1N/A if (psp->nb)
1N/A {
1N/A s = psp->nb;
1N/A e = psp->ne;
1N/A }
1N/A else
1N/A {
1N/A s = psp->ob;
1N/A psp = psp->next;
1N/A }
1N/A continue;
1N/A case '\n':
1N/A sfputc(sp, c);
1N/A for (i = 0; i < level; i++)
1N/A sfputc(sp, '\t');
1N/A continue;
1N/A case '\v':
1N/A a = FONT_LITERAL;
1N/A goto setfont;
1N/A case '<':
1N/A if (style == STYLE_html)
1N/A {
1N/A sfputr(sp, "&lt;", -1);
1N/A c = 0;
1N/A for (t = s; t < e; t++)
1N/A if (!isalnum(*t) && *t != '_' && *t != '.' && *t != '-')
1N/A {
1N/A if (*t == '@')
1N/A {
1N/A if (c)
1N/A break;
1N/A c = 1;
1N/A }
1N/A else if (*t == '>')
1N/A {
1N/A if (c)
1N/A {
1N/A sfprintf(sp, "<A href=\"mailto:%-.*s>%-.*s</A>&gt;", t - s, s, t - s, s);
1N/A s = t + 1;
1N/A }
1N/A break;
1N/A }
1N/A else
1N/A break;
1N/A }
1N/A continue;
1N/A }
1N/A break;
1N/A case '>':
1N/A if (style == STYLE_html)
1N/A {
1N/A sfputr(sp, "&gt;", -1);
1N/A continue;
1N/A }
1N/A break;
1N/A case '&':
1N/A if (style == STYLE_html)
1N/A {
1N/A sfputr(sp, "&amp;", -1);
1N/A continue;
1N/A }
1N/A break;
1N/A case '"':
1N/A if (style == STYLE_html)
1N/A {
1N/A sfputr(sp, "&quot;", -1);
1N/A continue;
1N/A }
1N/A break;
1N/A case '-':
1N/A if (ostyle == STYLE_nroff)
1N/A sfputc(sp, '\\');
1N/A break;
1N/A case '.':
1N/A if (ostyle == STYLE_nroff)
1N/A {
1N/A sfputc(sp, '\\');
1N/A sfputc(sp, '&');
1N/A }
1N/A break;
1N/A case '\\':
1N/A if (ostyle == STYLE_nroff)
1N/A {
1N/A c = 'e';
1N/A sfputc(sp, '\\');
1N/A }
1N/A break;
1N/A case ' ':
1N/A if (ostyle == STYLE_nroff)
1N/A sfputc(sp, '\\');
1N/A break;
1N/A }
1N/A sfputc(sp, c);
1N/A }
1N/A restore:
1N/A if (f > 0)
1N/A sfputr(sp, font(f, style, 0), -1);
1N/A if (about)
1N/A sfputc(sp, ')');
1N/A if (psp)
1N/A pop(psp);
1N/A return r;
1N/A}
1N/A
1N/A/*
1N/A * output args description to sp from p of length n
1N/A */
1N/A
1N/Astatic void
1N/Aargs(register Sfio_t* sp, register char* p, register int n, int flags, int style, Sfio_t* ip, int version, char* id, char* catalog)
1N/A{
1N/A register int i;
1N/A register char* t;
1N/A register char* o;
1N/A register char* a = 0;
1N/A char* b;
1N/A int sep;
1N/A
1N/A if (flags & OPT_functions)
1N/A sep = '\t';
1N/A else
1N/A {
1N/A sep = ' ';
1N/A o = T(NiL, ID, "options");
1N/A b = style == STYLE_nroff ? "\\ " : " ";
1N/A for (;;)
1N/A {
1N/A t = (char*)memchr(p, '\n', n);
1N/A if (style >= STYLE_man)
1N/A {
1N/A if (!(a = id))
1N/A a = "...";
1N/A sfprintf(sp, "\t%s%s%s%s[%s%s%s%s%s]", font(FONT_BOLD, style, 1), a, font(FONT_BOLD, style, 0), b, b, font(FONT_ITALIC, style, 1), o, font(FONT_ITALIC, style, 0), b);
1N/A }
1N/A else if (a)
1N/A sfprintf(sp, "%*.*s%s%s%s[%s%s%s]", OPT_USAGE - 1, OPT_USAGE - 1, T(NiL, ID, "Or:"), b, a, b, b, o, b);
1N/A else
1N/A {
1N/A if (!(a = error_info.id) && !(a = id))
1N/A a = "...";
1N/A if (!sfstrtell(sp))
1N/A sfprintf(sp, "[%s%s%s]", b, o, b);
1N/A }
1N/A if (!t)
1N/A break;
1N/A i = ++t - p;
1N/A if (i)
1N/A {
1N/A sfputr(sp, b, -1);
1N/A if (X(catalog))
1N/A {
1N/A sfwrite(ip, p, i);
1N/A if (b = sfstruse(ip))
1N/A sfputr(sp, T(id, catalog, b), -1);
1N/A else
1N/A sfwrite(sp, p, i);
1N/A }
1N/A else
1N/A sfwrite(sp, p, i);
1N/A }
1N/A if (style == STYLE_html)
1N/A sfputr(sp, "<BR>", '\n');
1N/A else if (style == STYLE_nroff)
1N/A sfputr(sp, ".br", '\n');
1N/A else if (style == STYLE_api)
1N/A sfputr(sp, ".BR", '\n');
1N/A p = t;
1N/A n -= i;
1N/A while (n > 0 && (*p == ' ' || *p == '\t'))
1N/A {
1N/A p++;
1N/A n--;
1N/A }
1N/A }
1N/A }
1N/A if (n)
1N/A label(sp, sep, p, 0, n, 0, style, 0, ip, version, id, catalog);
1N/A}
1N/A
1N/A/*
1N/A * output [+-...label...?...] label s to sp
1N/A * according to {...} level and style
1N/A * return 0:header 1:paragraph
1N/A */
1N/A
1N/Astatic int
1N/Aitem(Sfio_t* sp, char* s, int about, int level, int style, Sfio_t* ip, int version, char* id, char* catalog)
1N/A{
1N/A register char* t;
1N/A int n;
1N/A int par;
1N/A
1N/A sfputc(sp, '\n');
1N/A if (*s == '\n')
1N/A {
1N/A par = 0;
1N/A if (style >= STYLE_nroff)
1N/A sfprintf(sp, ".DS\n");
1N/A else
1N/A {
1N/A if (style == STYLE_html)
1N/A sfprintf(sp, "<PRE>\n");
1N/A else
1N/A sfputc(sp, '\n');
1N/A for (n = 0; n < level; n++)
1N/A sfputc(sp, '\t');
1N/A }
1N/A label(sp, 0, s + 1, about, -1, level, style, FONT_LITERAL, ip, version, id, catalog);
1N/A sfputc(sp, '\n');
1N/A if (style >= STYLE_nroff)
1N/A sfprintf(sp, ".DE");
1N/A else if (style == STYLE_html)
1N/A sfprintf(sp, "</PRE>");
1N/A }
1N/A else if (*s != ']' && (*s != '?' || *(s + 1) == '?'))
1N/A {
1N/A par = 0;
1N/A if (level)
1N/A {
1N/A if (style >= STYLE_nroff)
1N/A sfprintf(sp, ".H%d ", (level - (level > 2)) / 2);
1N/A else
1N/A for (n = 0; n < level; n++)
1N/A sfputc(sp, '\t');
1N/A }
1N/A if (style == STYLE_html)
1N/A {
1N/A if (!level)
1N/A sfputr(sp, "<H4>", -1);
1N/A sfputr(sp, "<A name=\"", -1);
1N/A if (s[-1] == '-' && s[0] == 'l' && s[1] == 'i' && s[2] == 'c' && s[3] == 'e' && s[4] == 'n' && s[5] == 's' && s[6] == 'e' && s[7] == '?')
1N/A for (t = s + 8; *t && *t != ']'; t++)
1N/A if (t[0] == 'p' && (!strncmp(t, "proprietary", 11) || !strncmp(t, "private", 7)) || t[0] == 'n' && !strncmp(t, "noncommercial", 13))
1N/A {
1N/A state.flags |= OPT_proprietary;
1N/A break;
1N/A }
1N/A label(sp, 0, s, about, -1, level, style, -1, ip, version, id, catalog);
1N/A sfputr(sp, "\">", -1);
1N/A label(sp, 0, s, about, -1, level, style, level ? FONT_BOLD : 0, ip, version, id, catalog);
1N/A sfputr(sp, "</A>", -1);
1N/A if (!level)
1N/A sfputr(sp, "</H4>", -1);
1N/A }
1N/A else
1N/A {
1N/A if (!level)
1N/A {
1N/A if (style >= STYLE_nroff)
1N/A sfprintf(sp, ".SH ");
1N/A else if (style == STYLE_man)
1N/A sfputc(sp, '\n');
1N/A else if (style != STYLE_options && style != STYLE_match || *s == '-' || *s == '+')
1N/A sfputc(sp, '\t');
1N/A }
1N/A label(sp, 0, s, about, -1, level, style, FONT_BOLD, ip, version, id, catalog);
1N/A }
1N/A }
1N/A else
1N/A {
1N/A par = 1;
1N/A if (style >= STYLE_nroff)
1N/A sfputr(sp, level ? ".SP" : ".PP", -1);
1N/A }
1N/A if (style >= STYLE_nroff || !level)
1N/A sfputc(sp, '\n');
1N/A if (par && style < STYLE_nroff)
1N/A for (n = 0; n < level; n++)
1N/A sfputc(sp, '\t');
1N/A return par;
1N/A}
1N/A
1N/A/*
1N/A * output text to sp from p according to style
1N/A */
1N/A
1N/A#if _BLD_DEBUG
1N/A
1N/Astatic char* textout(Sfio_t*, char*, char*, int, int, int, int, Sfio_t*, int, char*, char*);
1N/A
1N/Astatic char*
1N/Atrace_textout(Sfio_t* sp, register char* p, char* conform, int conformlen, int style, int level, int bump, Sfio_t* ip, int version, char* id, char* catalog, int line)
1N/A{
1N/A static int depth = 0;
1N/A
1N/A message((-21, "opthelp: txt#%d +++ %2d \"%s\" style=%d level=%d bump=%d", line, ++depth, show(p), style, level, bump));
1N/A p = textout(sp, p, conform, conformlen, style, level, bump, ip, version, id, catalog);
1N/A message((-21, "opthelp: txt#%d --- %2d \"%s\"", line, depth--, show(p)));
1N/A return p;
1N/A}
1N/A
1N/A#endif
1N/A
1N/Astatic char*
1N/Atextout(Sfio_t* sp, register char* s, char* conform, int conformlen, int style, int level, int bump, Sfio_t* ip, int version, char* id, char* catalog)
1N/A{
1N/A#if _BLD_DEBUG
1N/A#define textout(sp,s,conform,conformlen,style,level,bump,ip,version,id,catalog) trace_textout(sp,s,conform,conformlen,style,level,bump,ip,version,id,catalog,__LINE__)
1N/A#endif
1N/A register char* t;
1N/A register int c;
1N/A register int n;
1N/A char* w;
1N/A char* q;
1N/A int a;
1N/A int f;
1N/A int par;
1N/A int about;
1N/A Push_t* tsp;
1N/A
1N/A int ident = 0;
1N/A int lev = level;
1N/A Push_t* psp = 0;
1N/A
1N/A again:
1N/A about = 0;
1N/A if ((c = *s) == GO)
1N/A {
1N/A for (;;)
1N/A {
1N/A while (*(s = next(s + 1, version)) == '\n');
1N/A if (*s == GO)
1N/A {
1N/A if (level > 1)
1N/A level++;
1N/A level++;
1N/A }
1N/A else if (*s != OG)
1N/A {
1N/A if (level <= 1 || *s != '[' || *(s + 1) != '-' || style == STYLE_man && *(s + 2) == '?' || isalpha(*(s + 2)))
1N/A break;
1N/A s = skip(s, 0, 0, 0, 1, level, 0, version);
1N/A }
1N/A else if ((level -= 2) <= lev)
1N/A return s + 1;
1N/A }
1N/A if (*s == '\f')
1N/A {
1N/A psp = info(psp, s + 1, NiL, ip, id);
1N/A if (psp->nb)
1N/A s = psp->nb;
1N/A else
1N/A {
1N/A s = psp->ob;
1N/A psp = psp->next;
1N/A }
1N/A }
1N/A if (*s != '[')
1N/A return s;
1N/A c = *++s;
1N/A if (level > 1)
1N/A level++;
1N/A level++;
1N/A }
1N/A if (c == '-' && level > 1)
1N/A {
1N/A if (style == STYLE_man)
1N/A {
1N/A about = 1;
1N/A if (*(s + 1) == '-')
1N/A s++;
1N/A }
1N/A else
1N/A for (;;)
1N/A {
1N/A s = skip(s, 0, 0, 0, 1, level, 0, version);
1N/A while (*(s = next(s + 1, version)) == '\n');
1N/A if (*s == '[')
1N/A {
1N/A if ((c = *++s) != '-')
1N/A break;
1N/A }
1N/A else if (*s == GO)
1N/A goto again;
1N/A else if (*s == OG)
1N/A return s + 1;
1N/A }
1N/A }
1N/A if (c == '+' || c == '-' && (bump = 3) || c != ' ' && level > 1)
1N/A {
1N/A s = skip(t = s + 1, '?', 0, 0, 1, level, 0, version);
1N/A if (c == '-' && (*t == '?' || isdigit(*t) || *s == '?' && *(s + 1) == '\n'))
1N/A {
1N/A if ((c = *s) != '?')
1N/A return skip(s, 0, 0, 0, 1, level, 1, version);
1N/A w = C("version");
1N/A par = item(sp, w, about, level, style, ip, version, id, ID);
1N/A for (;;)
1N/A {
1N/A while (isspace(*(s + 1)))
1N/A s++;
1N/A w = s;
1N/A if (w[1] == '@' && w[2] == '(' && w[3] == '#' && w[4] == ')')
1N/A s = w + 4;
1N/A else if (w[1] == '$' && w[2] == 'I' && w[3] == 'd' && w[4] == ':' && w[5] == ' ')
1N/A {
1N/A s = w + 5;
1N/A ident = 1;
1N/A }
1N/A else
1N/A break;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A if (isdigit(c) && isdigit(*t))
1N/A {
1N/A while (isdigit(*t))
1N/A t++;
1N/A if (*t == ':')
1N/A t++;
1N/A }
1N/A else if (isalnum(c) && *t-- == ':')
1N/A {
1N/A if (X(catalog) || *t == *(t + 2))
1N/A t += 2;
1N/A else
1N/A {
1N/A sfprintf(ip, "%s", t);
1N/A if (w = sfstruse(ip))
1N/A *((t = w) + 1) = '|';
1N/A }
1N/A }
1N/A par = item(sp, t, about, level, style, ip, version, id, catalog);
1N/A c = *s;
1N/A }
1N/A if (!about && level)
1N/A par = 0;
1N/A }
1N/A else
1N/A {
1N/A if (style >= STYLE_nroff)
1N/A sfputc(sp, '\n');
1N/A else if (c == '?')
1N/A for (n = 0; n < level; n++)
1N/A sfputc(sp, '\t');
1N/A par = 0;
1N/A }
1N/A if (c == ':')
1N/A c = *(s = skip(s, '?', 0, 0, 1, 0, 0, version));
1N/A if ((c == ']' || c == '?' && *(s + 1) == ']' && *(s + 2) != ']' && s++) && (c = *(s = next(s + 1, version))) == GO)
1N/A {
1N/A s = textout(sp, s, conform, conformlen, style, level + bump + par + 1, 0, ip, version, id, catalog);
1N/A if (level > lev && *s && *(s = next(s, version)) == '[')
1N/A {
1N/A s++;
1N/A message((-21, "textout#%d s=%s", __LINE__, show(s)));
1N/A goto again;
1N/A }
1N/A }
1N/A else if (c == '?' || c == ' ')
1N/A {
1N/A s++;
1N/A if (c == ' ')
1N/A sfputc(sp, c);
1N/A else
1N/A {
1N/A if (X(catalog) && (tsp = localize(psp, s, NiL, 0, 1, ip, version, id, catalog)))
1N/A {
1N/A psp = tsp;
1N/A s = psp->nb;
1N/A }
1N/A if (style < STYLE_nroff)
1N/A for (n = 0; n < bump + 1; n++)
1N/A sfputc(sp, '\t');
1N/A }
1N/A if (conform)
1N/A {
1N/A sfprintf(sp, "[%-.*s %s] ", conformlen, conform, T(NiL, ID, "conformance"));
1N/A conform = 0;
1N/A }
1N/A f = 0;
1N/A for (;;)
1N/A {
1N/A switch (c = *s++)
1N/A {
1N/A case 0:
1N/A if (!(tsp = psp))
1N/A {
1N/A if (f)
1N/A sfputr(sp, font(f, style, 0), -1);
1N/A return s - 1;
1N/A }
1N/A s = psp->ob;
1N/A psp = psp->next;
1N/A free(tsp);
1N/A continue;
1N/A case ']':
1N/A if (psp && psp->ch)
1N/A break;
1N/A if (*s != ']')
1N/A {
1N/A if (f)
1N/A {
1N/A sfputr(sp, font(f, style, 0), -1);
1N/A f = 0;
1N/A }
1N/A for (;;)
1N/A {
1N/A if ((*s == '#' || *s == ':') && level > lev)
1N/A {
1N/A char* o;
1N/A char* v;
1N/A int j;
1N/A int m;
1N/A int ol;
1N/A int vl;
1N/A
1N/A a = 0;
1N/A o = 0;
1N/A v = 0;
1N/A if (*++s == '?' || *s == *(s - 1))
1N/A {
1N/A s++;
1N/A a |= OPT_optional;
1N/A }
1N/A if (*(s = next(s, version)) == '[')
1N/A {
1N/A s = skip(s + 1, ':', '?', 0, 1, 0, 0, version);
1N/A while (*s == ':')
1N/A {
1N/A s = skip(t = s + 1, ':', '?', 0, 1, 0, 0, version);
1N/A m = s - t;
1N/A if (*t == '!')
1N/A {
1N/A o = t + 1;
1N/A ol = m - 1;
1N/A }
1N/A else if (*t == '=')
1N/A {
1N/A v = t + 1;
1N/A vl = m - 1;
1N/A }
1N/A else
1N/A for (j = 0; j < elementsof(attrs); j++)
1N/A if (strneq(t, attrs[j].name, m))
1N/A {
1N/A a |= attrs[j].flag;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A if (a & OPT_optional)
1N/A {
1N/A if (o)
1N/A {
1N/A sfprintf(sp, " %s ", T(NiL, ID, "If the option value is omitted then"));
1N/A sfputr(sp, font(FONT_BOLD, style, 1), -1);
1N/A t = o + ol;
1N/A while (o < t)
1N/A {
1N/A if (((c = *o++) == ':' || c == '?') && *o == c)
1N/A o++;
1N/A sfputc(sp, c);
1N/A }
1N/A sfputr(sp, font(FONT_BOLD, style, 0), -1);
1N/A sfprintf(sp, " %s.", T(NiL, ID, "is assumed"));
1N/A }
1N/A else
1N/A sfprintf(sp, " %s", T(NiL, ID, "The option value may be omitted."));
1N/A }
1N/A if (v)
1N/A {
1N/A sfprintf(sp, " %s ", T(NiL, ID, "The default value is"));
1N/A sfputr(sp, font(FONT_BOLD, style, 1), -1);
1N/A t = v + vl;
1N/A while (v < t)
1N/A {
1N/A if (((c = *v++) == ':' || c == '?') && *v == c)
1N/A v++;
1N/A sfputc(sp, c);
1N/A }
1N/A sfputr(sp, font(FONT_BOLD, style, 0), -1);
1N/A sfputc(sp, '.');
1N/A }
1N/A s = skip(s, 0, 0, 0, 1, 0, 1, version);
1N/A }
1N/A if (*(s = next(s, version)) == GO)
1N/A {
1N/A s = textout(sp, s, 0, 0, style, level + bump + !level, 0, ip, version, id, catalog);
1N/A if (*s && *(s = next(s, version)) == '[' && !isalnum(*(s + 1)))
1N/A {
1N/A s++;
1N/A message((-21, "textout#%d s=%s", __LINE__, show(s)));
1N/A goto again;
1N/A }
1N/A }
1N/A else if (*s == '[' && level > lev)
1N/A {
1N/A s++;
1N/A goto again;
1N/A }
1N/A else if (*s == '\f')
1N/A {
1N/A s++;
1N/A if (style != STYLE_keys)
1N/A {
1N/A psp = info(psp, s, NiL, ip, id);
1N/A if (psp->nb)
1N/A s = psp->nb;
1N/A else
1N/A {
1N/A s = psp->ob;
1N/A psp = psp->next;
1N/A }
1N/A }
1N/A }
1N/A else if (!*s)
1N/A {
1N/A if (!(tsp = psp))
1N/A break;
1N/A s = psp->ob;
1N/A psp = psp->next;
1N/A free(tsp);
1N/A }
1N/A else if (*s != OG)
1N/A break;
1N/A else
1N/A {
1N/A s++;
1N/A if ((level -= 2) <= lev)
1N/A break;
1N/A }
1N/A }
1N/A return s;
1N/A }
1N/A s++;
1N/A break;
1N/A case '\a':
1N/A a = FONT_ITALIC;
1N/A setfont:
1N/A if (f & ~a)
1N/A {
1N/A sfputr(sp, font(f, style, 0), -1);
1N/A f = 0;
1N/A }
1N/A if (!f && style == STYLE_html)
1N/A {
1N/A for (t = s; *t && !isspace(*t) && !iscntrl(*t); t++);
1N/A if (*t == c && *++t == '(')
1N/A {
1N/A w = t;
1N/A if (isdigit(*++t))
1N/A while (isupper(*++t));
1N/A if (*t == ')' && t > w + 1)
1N/A {
1N/A sfprintf(sp, "<NOBR><A href=\"../man%-.*s/"
1N/A , t - w - 1, w + 1
1N/A );
1N/A for (q = s; q < w - 1; q++)
1N/A if (*q == ':' && q < w - 2 && *(q + 1) == ':')
1N/A {
1N/A sfputc(sp, '-');
1N/A q++;
1N/A }
1N/A else
1N/A sfputc(sp, *q);
1N/A sfprintf(sp, ".html\">%s%-.*s%s</A>%-.*s</NOBR>"
1N/A , font(a, style, 1)
1N/A , w - s - 1, s
1N/A , font(a, style, 0)
1N/A , t - w + 1, w
1N/A );
1N/A s = t + 1;
1N/A continue;
1N/A }
1N/A }
1N/A }
1N/A sfputr(sp, font(a, style, !!(f ^= a)), -1);
1N/A continue;
1N/A case '\b':
1N/A a = FONT_BOLD;
1N/A goto setfont;
1N/A case '\f':
1N/A if (style != STYLE_keys)
1N/A {
1N/A psp = info(psp, s, NiL, ip, id);
1N/A if (psp->nb)
1N/A s = psp->nb;
1N/A else
1N/A {
1N/A s = psp->ob;
1N/A psp = psp->next;
1N/A }
1N/A }
1N/A continue;
1N/A case '\v':
1N/A a = FONT_LITERAL;
1N/A goto setfont;
1N/A case ' ':
1N/A if (ident && *s == '$')
1N/A {
1N/A while (*++s)
1N/A if (*s == ']')
1N/A {
1N/A if (*(s + 1) != ']')
1N/A break;
1N/A s++;
1N/A }
1N/A continue;
1N/A }
1N/A case '\n':
1N/A case '\r':
1N/A case '\t':
1N/A while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
1N/A s++;
1N/A if (*s == ']' && *(s + 1) != ']' && (!psp || !psp->ch))
1N/A continue;
1N/A c = ' ';
1N/A break;
1N/A case '<':
1N/A if (style == STYLE_html)
1N/A {
1N/A sfputr(sp, "&lt;", -1);
1N/A c = 0;
1N/A for (t = s; *t; t++)
1N/A if (!isalnum(*t) && *t != '_' && *t != '.' && *t != '-')
1N/A {
1N/A if (*t == '@')
1N/A {
1N/A if (c)
1N/A break;
1N/A c = 1;
1N/A }
1N/A else if (*t == '>')
1N/A {
1N/A if (c)
1N/A {
1N/A sfprintf(sp, "<A href=\"mailto:%-.*s\">%-.*s</A>&gt;", t - s, s, t - s, s);
1N/A s = t + 1;
1N/A }
1N/A break;
1N/A }
1N/A else
1N/A break;
1N/A }
1N/A continue;
1N/A }
1N/A break;
1N/A case '>':
1N/A if (style == STYLE_html)
1N/A {
1N/A sfputr(sp, "&gt;", -1);
1N/A continue;
1N/A }
1N/A break;
1N/A case '&':
1N/A if (style == STYLE_html)
1N/A {
1N/A sfputr(sp, "&amp;", -1);
1N/A continue;
1N/A }
1N/A break;
1N/A case '-':
1N/A if (style == STYLE_nroff)
1N/A sfputc(sp, '\\');
1N/A break;
1N/A case '.':
1N/A if (style == STYLE_nroff)
1N/A {
1N/A sfputc(sp, '\\');
1N/A sfputc(sp, '&');
1N/A }
1N/A break;
1N/A case '\\':
1N/A if (style == STYLE_nroff)
1N/A {
1N/A sfputc(sp, c);
1N/A c = 'e';
1N/A }
1N/A break;
1N/A }
1N/A sfputc(sp, c);
1N/A }
1N/A }
1N/A else if (c == '[' && level > lev)
1N/A {
1N/A s++;
1N/A goto again;
1N/A }
1N/A return s;
1N/A}
1N/A
1N/A/*
1N/A * generate optget() help [...] list from lp
1N/A */
1N/A
1N/Astatic void
1N/Alist(Sfio_t* sp, register const List_t* lp)
1N/A{
1N/A sfprintf(sp, "[%c", lp->type);
1N/A if (lp->name)
1N/A {
1N/A sfprintf(sp, "%s", lp->name);
1N/A if (lp->text)
1N/A sfprintf(sp, "?%s", lp->text);
1N/A }
1N/A sfputc(sp, ']');
1N/A}
1N/A
1N/A/*
1N/A * return pointer to help message sans `Usage: command'
1N/A * if oopts is 0 then state.pass is used
1N/A * what:
1N/A * 0 ?short by default, ?long if any long options used
1N/A * * otherwise see help_text[] (--???)
1N/A * external formatter:
1N/A * \a...\a italic
1N/A * \b...\b bold
1N/A * \f...\f discipline infof callback on ...
1N/A * \v...\v literal
1N/A * internal formatter:
1N/A * \t indent
1N/A * \n newline
1N/A * margin flush pops to previous indent
1N/A */
1N/A
1N/Achar*
1N/Aopthelp(const char* oopts, const char* what)
1N/A{
1N/A register Sfio_t* sp;
1N/A register Sfio_t* mp;
1N/A register int c;
1N/A register char* p;
1N/A register Indent_t* ip;
1N/A char* t;
1N/A char* x;
1N/A char* w;
1N/A char* u;
1N/A char* y;
1N/A char* s;
1N/A char* d;
1N/A char* v;
1N/A char* cb;
1N/A char* ov;
1N/A char* pp;
1N/A char* rb;
1N/A char* re;
1N/A int f;
1N/A int i;
1N/A int j;
1N/A int m;
1N/A int n;
1N/A int a;
1N/A int cl;
1N/A int sl;
1N/A int vl;
1N/A int ol;
1N/A int wl;
1N/A int xl;
1N/A int rm;
1N/A int ts;
1N/A int co;
1N/A int z;
1N/A int style;
1N/A int head;
1N/A int margin;
1N/A int mode;
1N/A int mutex;
1N/A int prefix;
1N/A int version;
1N/A long tp;
1N/A char* id;
1N/A char* catalog;
1N/A Optpass_t* o;
1N/A Optpass_t* q;
1N/A Optpass_t* e;
1N/A Optpass_t one;
1N/A Optpass_t top;
1N/A Help_t* hp;
1N/A short ptstk[elementsof(indent) + 2];
1N/A short* pt;
1N/A Sfio_t* vp;
1N/A Push_t* tsp;
1N/A
1N/A char* opts = (char*)oopts;
1N/A char* section = "1";
1N/A int flags = 0;
1N/A int matched = 0;
1N/A int paragraph = 0;
1N/A Push_t* psp = 0;
1N/A Sfio_t* sp_help = 0;
1N/A Sfio_t* sp_text = 0;
1N/A Sfio_t* sp_plus = 0;
1N/A Sfio_t* sp_head = 0;
1N/A Sfio_t* sp_body = 0;
1N/A Sfio_t* sp_info = 0;
1N/A Sfio_t* sp_misc = 0;
1N/A
1N/A if (!(mp = state.mp) && !(mp = state.mp = sfstropen()))
1N/A goto nospace;
1N/A if (!what)
1N/A style = state.style;
1N/A else if (!*what)
1N/A style = STYLE_options;
1N/A else if (*what != '?')
1N/A style = STYLE_match;
1N/A else if (!*(what + 1))
1N/A style = STYLE_man;
1N/A else if ((hp = (Help_t*)search(styles, elementsof(styles), sizeof(styles[0]), (char*)what + 1)) && hp->style >= 0)
1N/A {
1N/A style = hp->style;
1N/A if (*hp->name != '?')
1N/A what = hp->name;
1N/A }
1N/A else
1N/A {
1N/A if ((style = state.force) < STYLE_man)
1N/A style = STYLE_man;
1N/A if (!(sp_help = sfstropen()))
1N/A goto nospace;
1N/A for (i = 0; i < elementsof(help_head); i++)
1N/A list(sp_help, &help_head[i]);
1N/A for (i = 0; i < elementsof(styles); i++)
1N/A sfprintf(sp_help, "[:%s?%s]", styles[i].match, styles[i].text);
1N/A for (i = 0; i < elementsof(help_tail); i++)
1N/A list(sp_help, &help_tail[i]);
1N/A if (!(opts = sfstruse(sp_help)))
1N/A goto nospace;
1N/A }
1N/A
1N/A /*
1N/A * this is a workaround for static optjoin() data
1N/A * clobbered by plugins/builtins that may be called
1N/A * evaluating \f...\f -- it would be good to hide
1N/A * optjoin() interactions a bit more ...
1N/A */
1N/A
1N/A top = state.pass[0];
1N/A again:
1N/A if (opts)
1N/A {
1N/A for (i = 0; i < state.npass; i++)
1N/A if (state.pass[i].oopts == opts)
1N/A {
1N/A o = &state.pass[i];
1N/A break;
1N/A }
1N/A if (i >= state.npass)
1N/A {
1N/A o = &one;
1N/A if (init((char*)opts, o))
1N/A goto nospace;
1N/A }
1N/A e = o + 1;
1N/A }
1N/A else
1N/A {
1N/A if (state.npass > 0)
1N/A {
1N/A o = state.pass;
1N/A e = o + state.npass;
1N/A }
1N/A else if (state.npass < 0)
1N/A {
1N/A o = &state.cache->pass;
1N/A e = o + 1;
1N/A }
1N/A else
1N/A return T(NiL, ID, "[* call optget() before opthelp() *]");
1N/A oopts = (const char*)state.pass[0].oopts;
1N/A }
1N/A if (style <= STYLE_usage)
1N/A {
1N/A if (!(sp_text = sfstropen()) || !(sp_info = sfstropen()))
1N/A goto nospace;
1N/A if (style >= STYLE_match && style < STYLE_keys && !(sp_body = sfstropen()))
1N/A goto nospace;
1N/A }
1N/A switch (style)
1N/A {
1N/A case STYLE_api:
1N/A case STYLE_html:
1N/A case STYLE_nroff:
1N/A state.emphasis = 0;
1N/A break;
1N/A case STYLE_usage:
1N/A case STYLE_keys:
1N/A for (q = o; q < e; q++)
1N/A if (!(q->flags & OPT_ignore) && !streq(q->catalog, o->catalog))
1N/A o = q;
1N/A /*FALLTHROUGH*/
1N/A case STYLE_posix:
1N/A sfputc(mp, '\f');
1N/A break;
1N/A default:
1N/A if (!state.emphasis)
1N/A {
1N/A if (x = getenv("ERROR_OPTIONS"))
1N/A {
1N/A if (strmatch(x, "*noemphasi*"))
1N/A break;
1N/A if (strmatch(x, "*emphasi*"))
1N/A {
1N/A state.emphasis = 1;
1N/A break;
1N/A }
1N/A }
1N/A if ((x = getenv("TERM")) && strmatch(x, "(ansi|vt100|xterm)*") && isatty(sffileno(sfstderr)))
1N/A state.emphasis = 1;
1N/A }
1N/A break;
1N/A }
1N/A x = "";
1N/A xl = 0;
1N/A for (q = o; q < e; q++)
1N/A {
1N/A if (q->flags & OPT_ignore)
1N/A continue;
1N/A section = q->section;
1N/A flags |= q->flags;
1N/A p = q->opts;
1N/A prefix = q->prefix;
1N/A version = q->version;
1N/A id = q->id;
1N/A catalog = q->catalog;
1N/A switch (style)
1N/A {
1N/A case STYLE_usage:
1N/A if (xl)
1N/A sfputc(mp, '\n');
1N/A else
1N/A xl = 1;
1N/A psp = 0;
1N/A for (;;)
1N/A {
1N/A switch (c = *p++)
1N/A {
1N/A case 0:
1N/A if (!(tsp = psp))
1N/A goto style_usage;
1N/A p = psp->ob;
1N/A psp = psp->next;
1N/A free(tsp);
1N/A continue;
1N/A case '\a':
1N/A c = 'a';
1N/A break;
1N/A case '\b':
1N/A c = 'b';
1N/A break;
1N/A case '\f':
1N/A psp = info(psp, p, NiL, sp_info, id);
1N/A if (psp->nb)
1N/A p = psp->nb;
1N/A else
1N/A {
1N/A p = psp->ob;
1N/A psp = psp->next;
1N/A }
1N/A continue;
1N/A case '\n':
1N/A c = 'n';
1N/A break;
1N/A case '\r':
1N/A c = 'r';
1N/A break;
1N/A case '\t':
1N/A c = 't';
1N/A break;
1N/A case '\v':
1N/A c = 'v';
1N/A break;
1N/A case '"':
1N/A c = '"';
1N/A break;
1N/A case '\'':
1N/A c = '\'';
1N/A break;
1N/A case '\\':
1N/A c = '\\';
1N/A break;
1N/A default:
1N/A sfputc(mp, c);
1N/A continue;
1N/A }
1N/A sfputc(mp, '\\');
1N/A sfputc(mp, c);
1N/A }
1N/A style_usage:
1N/A continue;
1N/A case STYLE_keys:
1N/A a = 0;
1N/A psp = 0;
1N/A vl = 0;
1N/A for (;;)
1N/A {
1N/A if (!(c = *p++))
1N/A {
1N/A if (!(tsp = psp))
1N/A break;
1N/A p = psp->ob;
1N/A psp = psp->next;
1N/A free(tsp);
1N/A continue;
1N/A }
1N/A if (c == '\f')
1N/A {
1N/A psp = info(psp, p, NiL, sp_info, id);
1N/A if (psp->nb)
1N/A p = psp->nb;
1N/A else
1N/A {
1N/A p = psp->ob;
1N/A psp = psp->next;
1N/A }
1N/A continue;
1N/A }
1N/A f = z = 1;
1N/A t = 0;
1N/A if (a == 0 && (c == ' ' || c == '\n' && *p == '\n'))
1N/A {
1N/A if (c == ' ' && *p == ']')
1N/A {
1N/A p++;
1N/A continue;
1N/A }
1N/A if (*p == '\n')
1N/A p++;
1N/A a = c;
1N/A }
1N/A else if (c == '\n')
1N/A {
1N/A if (a == ' ')
1N/A a = -1;
1N/A else if (a == '\n' || *p == '\n')
1N/A {
1N/A a = -1;
1N/A p++;
1N/A }
1N/A continue;
1N/A }
1N/A else if ((c == ':' || c == '#') && (*p == '[' || *p == '?' && *(p + 1) == '[' && p++))
1N/A p++;
1N/A else if (c != '[')
1N/A {
1N/A if (c == GO)
1N/A vl++;
1N/A else if (c == OG)
1N/A vl--;
1N/A continue;
1N/A }
1N/A else if (*p == ' ')
1N/A {
1N/A p++;
1N/A continue;
1N/A }
1N/A else if (*p == '-')
1N/A {
1N/A z = 0;
1N/A if (*++p == '-')
1N/A {
1N/A p = skip(p, 0, 0, 0, 1, 0, 1, version);
1N/A continue;
1N/A }
1N/A }
1N/A else if (*p == '+')
1N/A {
1N/A p++;
1N/A if (vl > 0 && *p != '\a')
1N/A {
1N/A f = 0;
1N/A p = skip(p, '?', 0, 0, 1, 0, 0, version);
1N/A if (*p == '?')
1N/A p++;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A if (*(p + 1) == '\f' && (vp = state.vp))
1N/A p = expand(p + 2, NiL, &t, vp, id);
1N/A p = skip(p, ':', '?', 0, 1, 0, 0, version);
1N/A if (*p == ':')
1N/A p++;
1N/A }
1N/A if (f && *p == '?' && *(p + 1) != '?')
1N/A {
1N/A f = 0;
1N/A if (z)
1N/A p++;
1N/A else
1N/A p = skip(p, 0, 0, 0, 1, 0, 0, version);
1N/A }
1N/A if (*p == ']' && *(p + 1) != ']')
1N/A {
1N/A p++;
1N/A continue;
1N/A }
1N/A if (!*p)
1N/A {
1N/A if (!t)
1N/A break;
1N/A p = t;
1N/A t = 0;
1N/A }
1N/A m = sfstrtell(mp);
1N/A sfputc(mp, '"');
1N/A xl = 1;
1N/A /*UNDENT...*/
1N/A
1N/A for (;;)
1N/A {
1N/A if (!(c = *p++))
1N/A {
1N/A if (t)
1N/A {
1N/A p = t;
1N/A t = 0;
1N/A }
1N/A if (!(tsp = psp))
1N/A {
1N/A p--;
1N/A break;
1N/A }
1N/A p = psp->ob;
1N/A psp = psp->next;
1N/A free(tsp);
1N/A continue;
1N/A }
1N/A if (a > 0)
1N/A {
1N/A if (c == '\n')
1N/A {
1N/A if (a == ' ')
1N/A {
1N/A a = -1;
1N/A break;
1N/A }
1N/A if (a == '\n' || *p == '\n')
1N/A {
1N/A a = -1;
1N/A p++;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A else if (c == ']')
1N/A {
1N/A if (*p != ']')
1N/A {
1N/A sfputc(mp, 0);
1N/A y = sfstrbase(mp) + m + 1;
1N/A if (D(y) || !strmatch(y, KEEP) || strmatch(y, OMIT))
1N/A {
1N/A sfstrseek(mp, m, SEEK_SET);
1N/A xl = 0;
1N/A }
1N/A else
1N/A sfstrseek(mp, -1, SEEK_CUR);
1N/A break;
1N/A }
1N/A sfputc(mp, *p++);
1N/A continue;
1N/A }
1N/A switch (c)
1N/A {
1N/A case '?':
1N/A if (f)
1N/A {
1N/A if (*p == '?')
1N/A {
1N/A p++;
1N/A sfputc(mp, c);
1N/A }
1N/A else
1N/A {
1N/A f = 0;
1N/A sfputc(mp, 0);
1N/A y = sfstrbase(mp) + m + 1;
1N/A if (D(y) || !strmatch(y, KEEP) || strmatch(y, OMIT))
1N/A {
1N/A sfstrseek(mp, m, SEEK_SET);
1N/A xl = 0;
1N/A }
1N/A else
1N/A sfstrseek(mp, -1, SEEK_CUR);
1N/A if (z && (*p != ']' || *(p + 1) == ']'))
1N/A {
1N/A if (xl)
1N/A {
1N/A sfputc(mp, '"');
1N/A sfputc(mp, '\n');
1N/A }
1N/A m = sfstrtell(mp);
1N/A sfputc(mp, '"');
1N/A xl = 1;
1N/A }
1N/A else
1N/A {
1N/A p = skip(p, 0, 0, 0, 1, 0, 0, version);
1N/A if (*p == '?')
1N/A p++;
1N/A }
1N/A }
1N/A }
1N/A else
1N/A sfputc(mp, c);
1N/A continue;
1N/A case ':':
1N/A if (f && *p == ':')
1N/A p++;
1N/A sfputc(mp, c);
1N/A continue;
1N/A case '\a':
1N/A c = 'a';
1N/A break;
1N/A case '\b':
1N/A c = 'b';
1N/A break;
1N/A case '\f':
1N/A c = 'f';
1N/A break;
1N/A case '\n':
1N/A c = 'n';
1N/A break;
1N/A case '\r':
1N/A c = 'r';
1N/A break;
1N/A case '\t':
1N/A c = 't';
1N/A break;
1N/A case '\v':
1N/A c = 'v';
1N/A break;
1N/A case '"':
1N/A c = '"';
1N/A break;
1N/A case '\\':
1N/A c = '\\';
1N/A break;
1N/A case CC_esc:
1N/A c = 'E';
1N/A break;
1N/A default:
1N/A sfputc(mp, c);
1N/A continue;
1N/A }
1N/A sfputc(mp, '\\');
1N/A sfputc(mp, c);
1N/A }
1N/A
1N/A /*...INDENT*/
1N/A if (xl)
1N/A {
1N/A sfputc(mp, '"');
1N/A sfputc(mp, '\n');
1N/A }
1N/A }
1N/A continue;
1N/A }
1N/A z = 0;
1N/A head = 0;
1N/A mode = 0;
1N/A mutex = 0;
1N/A if (style > STYLE_short && style < STYLE_nroff && version < 1)
1N/A {
1N/A style = STYLE_short;
1N/A if (sp_body)
1N/A {
1N/A sfclose(sp_body);
1N/A sp_body = 0;
1N/A }
1N/A }
1N/A else if (style == STYLE_short && prefix < 2)
1N/A style = STYLE_long;
1N/A if (*p == ':')
1N/A p++;
1N/A if (*p == '+')
1N/A {
1N/A p++;
1N/A if (!(sp = sp_plus) && !(sp = sp_plus = sfstropen()))
1N/A goto nospace;
1N/A }
1N/A else if (style >= STYLE_match)
1N/A sp = sp_body;
1N/A else
1N/A sp = sp_text;
1N/A psp = 0;
1N/A for (;;)
1N/A {
1N/A if (!(*(p = next(p, version))))
1N/A {
1N/A if (!(tsp = psp))
1N/A break;
1N/A p = psp->ob;
1N/A psp = psp->next;
1N/A free(tsp);
1N/A continue;
1N/A }
1N/A if (*p == '\f')
1N/A {
1N/A psp = info(psp, p + 1, NiL, sp_info, id);
1N/A if (psp->nb)
1N/A p = psp->nb;
1N/A else
1N/A {
1N/A p = psp->ob;
1N/A psp = psp->next;
1N/A }
1N/A continue;
1N/A }
1N/A if (*p == '\n' || *p == ' ')
1N/A {
1N/A if (*(x = p = next(p + 1, version)))
1N/A while (*++p)
1N/A if (*p == '\n')
1N/A {
1N/A while (*++p == ' ' || *p == '\t' || *p == '\r');
1N/A if (*p == '\n')
1N/A break;
1N/A }
1N/A xl = p - x;
1N/A if (!*p)
1N/A break;
1N/A continue;
1N/A }
1N/A if (*p == OG)
1N/A {
1N/A p++;
1N/A continue;
1N/A }
1N/A message((-20, "opthelp: opt %s", show(p)));
1N/A if (z < 0)
1N/A z = 0;
1N/A a = 0;
1N/A f = 0;
1N/A w = 0;
1N/A d = 0;
1N/A s = 0;
1N/A rb = re = 0;
1N/A sl = 0;
1N/A vl = 0;
1N/A if (*p == '[')
1N/A {
1N/A if ((c = *(p = next(p + 1, version))) == '(')
1N/A {
1N/A p = nest(cb = p);
1N/A cl = p - cb;
1N/A c = *p;
1N/A }
1N/A else
1N/A cb = 0;
1N/A if (c == '-')
1N/A {
1N/A if (style >= STYLE_man)
1N/A {
1N/A if (*(p + 1) != '-')
1N/A {
1N/A if (!sp_misc && !(sp_misc = sfstropen()))
1N/A goto nospace;
1N/A else
1N/A p = textout(sp_misc, p, cb, cl, style, 1, 3, sp_info, version, id, catalog);
1N/A continue;
1N/A }
1N/A }
1N/A else if (style == STYLE_match && *what == '-')
1N/A {
1N/A if (*(p + 1) == '?' || *(s = skip(p + 1, ':', '?', 0, 1, 0, 0, version)) == '?' && isspace(*(s + 1)))
1N/A s = C("version");
1N/A else
1N/A s = p + 1;
1N/A w = (char*)what;
1N/A if (*s != '-' || *(w + 1) == '-')
1N/A {
1N/A if (*s == '-')
1N/A s++;
1N/A if (*(w + 1) == '-')
1N/A w++;
1N/A if (match(w + 1, s, version, id, catalog))
1N/A {
1N/A if (*(p + 1) == '-')
1N/A p++;
1N/A p = textout(sp, p, cb, cl, style, 1, 3, sp_info, version, id, catalog);
1N/A matched = -1;
1N/A continue;
1N/A }
1N/A }
1N/A }
1N/A if (!z)
1N/A z = -1;
1N/A }
1N/A else if (c == '+')
1N/A {
1N/A if (style >= STYLE_man)
1N/A {
1N/A p = textout(sp_body, p, cb, cl, style, 0, 0, sp_info, version, id, catalog);
1N/A if (!sp_head)
1N/A {
1N/A sp_head = sp_body;
1N/A if (!(sp_body = sfstropen()))
1N/A goto nospace;
1N/A }
1N/A continue;
1N/A }
1N/A else if (style == STYLE_match && *what == '+')
1N/A {
1N/A if (paragraph)
1N/A {
1N/A if (p[1] == '?')
1N/A {
1N/A p = textout(sp, p, cb, cl, style, 1, 3, sp_info, version, id, catalog);
1N/A continue;
1N/A }
1N/A paragraph = 0;
1N/A }
1N/A if (match((char*)what + 1, p + 1, version, id, catalog))
1N/A {
1N/A p = textout(sp, p, cb, cl, style, 1, 3, sp_info, version, id, catalog);
1N/A matched = -1;
1N/A paragraph = 1;
1N/A continue;
1N/A }
1N/A }
1N/A if (!z)
1N/A z = -1;
1N/A }
1N/A else if (c == '[' || version < 1)
1N/A {
1N/A mutex++;
1N/A continue;
1N/A }
1N/A else
1N/A {
1N/A if (c == '!')
1N/A {
1N/A a |= OPT_invert;
1N/A p++;
1N/A }
1N/A rb = p;
1N/A if (*p != ':')
1N/A {
1N/A s = p;
1N/A if (*(p + 1) == '|')
1N/A {
1N/A while (*++p && *p != '=' && *p != '!' && *p != ':' && *p != '?');
1N/A if ((p - s) > 1)
1N/A sl = p - s;
1N/A if (*p == '!')
1N/A a |= OPT_invert;
1N/A }
1N/A if (*(p + 1) == '\f')
1N/A p++;
1N/A else
1N/A p = skip(p, ':', '?', 0, 1, 0, 0, version);
1N/A if (sl || (p - s) == 1 || *(s + 1) == '=' || *(s + 1) == '!' && (a |= OPT_invert) || *(s + 1) == '|')
1N/A f = *s;
1N/A }
1N/A re = p;
1N/A if (style <= STYLE_short)
1N/A {
1N/A if (!z && !f)
1N/A z = -1;
1N/A }
1N/A else
1N/A {
1N/A if (*p == '\f' && (vp = state.vp))
1N/A p = expand(p + 1, NiL, &t, vp, id);
1N/A else
1N/A t = 0;
1N/A if (*p == ':')
1N/A {
1N/A p = skip(w = p + 1, ':', '?', 0, 1, 0, 0, version);
1N/A if (!(wl = p - w))
1N/A w = 0;
1N/A }
1N/A else
1N/A wl = 0;
1N/A if (*p == ':' || *p == '?')
1N/A {
1N/A d = p;
1N/A p = skip(p, 0, 0, 0, 1, 0, 0, version);
1N/A }
1N/A else
1N/A d = 0;
1N/A if (style == STYLE_match)
1N/A {
1N/A if (wl && !match((char*)what, w, version, id, catalog))
1N/A wl = 0;
1N/A if ((!wl || *w == ':' || *w == '?') && (what[1] || sl && !memchr(s, what[0], sl) || !sl && what[0] != f))
1N/A {
1N/A w = 0;
1N/A if (!z)
1N/A z = -1;
1N/A }
1N/A else
1N/A matched = 1;
1N/A }
1N/A if (t)
1N/A {
1N/A p = t;
1N/A if (*p == ':' || *p == '?')
1N/A {
1N/A d = p;
1N/A p = skip(p, 0, 0, 0, 1, 0, 0, version);
1N/A }
1N/A }
1N/A }
1N/A }
1N/A p = skip(p, 0, 0, 0, 1, 0, 1, version);
1N/A if (*p == GO)
1N/A p = skip(p + 1, 0, 0, 0, 0, 1, 1, version);
1N/A }
1N/A else if (*p == ']')
1N/A {
1N/A if (mutex)
1N/A {
1N/A if (style >= STYLE_nroff)
1N/A sfputr(sp_body, "\n.OP - - anyof", '\n');
1N/A if (!(mutex & 1))
1N/A {
1N/A mutex--;
1N/A if (style <= STYLE_long)
1N/A {
1N/A sfputc(sp_body, ' ');
1N/A sfputc(sp_body, ']');
1N/A }
1N/A }
1N/A mutex--;
1N/A }
1N/A p++;
1N/A continue;
1N/A }
1N/A else if (*p == '?')
1N/A {
1N/A if (style < STYLE_match)
1N/A z = 1;
1N/A mode |= OPT_hidden;
1N/A p++;
1N/A continue;
1N/A }
1N/A else if (*p == '\\' && style==STYLE_posix)
1N/A {
1N/A if (*++p)
1N/A p++;
1N/A continue;
1N/A }
1N/A else
1N/A {
1N/A f = *p++;
1N/A s = 0;
1N/A if (style == STYLE_match && !z)
1N/A z = -1;
1N/A }
1N/A if (!z)
1N/A {
1N/A if (style == STYLE_long || prefix < 2 || (q->flags & OPT_long))
1N/A f = 0;
1N/A else if (style <= STYLE_short)
1N/A w = 0;
1N/A if (!f && !w)
1N/A z = -1;
1N/A }
1N/A ov = 0;
1N/A u = v = y = 0;
1N/A if (*p == ':' && (a |= OPT_string) || *p == '#' && (a |= OPT_number))
1N/A {
1N/A message((-21, "opthelp: arg %s", show(p)));
1N/A if (*++p == '?' || *p == *(p - 1))
1N/A {
1N/A p++;
1N/A a |= OPT_optional;
1N/A }
1N/A if (*(p = next(p, version)) == '[')
1N/A {
1N/A if (!z)
1N/A {
1N/A p = skip(y = p + 1, ':', '?', 0, 1, 0, 0, version);
1N/A while (*p == ':')
1N/A {
1N/A p = skip(t = p + 1, ':', '?', 0, 1, 0, 0, version);
1N/A m = p - t;
1N/A if (*t == '!')
1N/A {
1N/A ov = t + 1;
1N/A ol = m - 1;
1N/A }
1N/A else if (*t == '=')
1N/A {
1N/A v = t + 1;
1N/A vl = m - 1;
1N/A }
1N/A else
1N/A for (j = 0; j < elementsof(attrs); j++)
1N/A if (strneq(t, attrs[j].name, m))
1N/A {
1N/A a |= attrs[j].flag;
1N/A break;
1N/A }
1N/A }
1N/A if (*p == '?')
1N/A u = p;
1N/A p = skip(p, 0, 0, 0, 1, 0, 1, version);
1N/A }
1N/A else
1N/A p = skip(p + 1, 0, 0, 0, 1, 0, 1, version);
1N/A }
1N/A else
1N/A y = (a & OPT_number) ? T(NiL, ID, "#") : T(NiL, ID, "arg");
1N/A }
1N/A else
1N/A a |= OPT_flag;
1N/A if (!z)
1N/A {
1N/A if (style <= STYLE_short && !y && !mutex || style == STYLE_posix)
1N/A {
1N/A if (style != STYLE_posix && !sfstrtell(sp))
1N/A {
1N/A sfputc(sp, '[');
1N/A if (sp == sp_plus)
1N/A sfputc(sp, '+');
1N/A sfputc(sp, '-');
1N/A }
1N/A if (!sl)
1N/A sfputc(sp, f);
1N/A else
1N/A for (c = 0; c < sl; c++)
1N/A if (s[c] != '|')
1N/A sfputc(sp, s[c]);
1N/A if (style == STYLE_posix && y)
1N/A sfputc(sp, ':');
1N/A }
1N/A else
1N/A {
1N/A if (style >= STYLE_match)
1N/A {
1N/A sfputc(sp_body, '\n');
1N/A if (!head)
1N/A {
1N/A head = 1;
1N/A item(sp_body, (flags & OPT_functions) ? C("FUNCTIONS") : C("OPTIONS"), 0, 0, style, sp_info, version, id, ID);
1N/A }
1N/A if (style >= STYLE_nroff)
1N/A {
1N/A if (mutex & 1)
1N/A {
1N/A mutex++;
1N/A sfputr(sp_body, "\n.OP - - oneof", '\n');
1N/A }
1N/A }
1N/A else
1N/A sfputc(sp_body, '\t');
1N/A }
1N/A else
1N/A {
1N/A if (sp_body)
1N/A sfputc(sp_body, ' ');
1N/A else if (!(sp_body = sfstropen()))
1N/A goto nospace;
1N/A if (mutex)
1N/A {
1N/A if (mutex & 1)
1N/A {
1N/A mutex++;
1N/A sfputc(sp_body, '[');
1N/A }
1N/A else
1N/A sfputc(sp_body, '|');
1N/A sfputc(sp_body, ' ');
1N/A }
1N/A else
1N/A sfputc(sp_body, '[');
1N/A }
1N/A if (style >= STYLE_nroff)
1N/A {
1N/A if (flags & OPT_functions)
1N/A {
1N/A sfputr(sp_body, ".FN", ' ');
1N/A if (re > rb)
1N/A sfwrite(sp_body, rb, re - rb);
1N/A else
1N/A sfputr(sp, "void", -1);
1N/A if (w)
1N/A label(sp_body, ' ', w, 0, -1, 0, style, FONT_BOLD, sp_info, version, id, catalog);
1N/A }
1N/A else
1N/A {
1N/A sfputr(sp_body, ".OP", ' ');
1N/A if (sl)
1N/A sfwrite(sp_body, s, sl);
1N/A else
1N/A sfputc(sp_body, f ? f : '-');
1N/A sfputc(sp_body, ' ');
1N/A if (w)
1N/A {
1N/A if (label(sp_body, 0, w, 0, -1, 0, style, 0, sp_info, version, id, catalog))
1N/A {
1N/A sfputc(sp_body, '|');
1N/A label(sp_body, 0, w, 0, -1, 0, style, 0, sp_info, version, id, native);
1N/A }
1N/A }
1N/A else
1N/A sfputc(sp_body, '-');
1N/A sfputc(sp_body, ' ');
1N/A m = a & OPT_TYPE;
1N/A for (j = 0; j < elementsof(attrs); j++)
1N/A if (m & attrs[j].flag)
1N/A {
1N/A sfputr(sp_body, attrs[j].name, -1);
1N/A break;
1N/A }
1N/A if (m = (a & ~m) | mode)
1N/A for (j = 0; j < elementsof(attrs); j++)
1N/A if (m & attrs[j].flag)
1N/A {
1N/A sfputc(sp_body, ':');
1N/A sfputr(sp_body, attrs[j].name, -1);
1N/A }
1N/A sfputc(sp_body, ' ');
1N/A if (y)
1N/A label(sp_body, 0, y, 0, -1, 0, style, 0, sp_info, version, id, catalog);
1N/A else
1N/A sfputc(sp_body, '-');
1N/A if (v)
1N/A sfprintf(sp_body, " %-.*s", vl, v);
1N/A }
1N/A }
1N/A else
1N/A {
1N/A if (f)
1N/A {
1N/A if (sp_body == sp_plus)
1N/A sfputc(sp_body, '+');
1N/A sfputc(sp_body, '-');
1N/A sfputr(sp_body, font(FONT_BOLD, style, 1), -1);
1N/A if (!sl)
1N/A {
1N/A sfputc(sp_body, f);
1N/A if (f == '-' && y)
1N/A {
1N/A y = 0;
1N/A sfputr(sp_body, C("long-option[=value]"), -1);
1N/A }
1N/A }
1N/A else
1N/A sfwrite(sp_body, s, sl);
1N/A sfputr(sp_body, font(FONT_BOLD, style, 0), -1);
1N/A if (w)
1N/A {
1N/A sfputc(sp_body, ',');
1N/A sfputc(sp_body, ' ');
1N/A }
1N/A }
1N/A else if ((flags & OPT_functions) && re > rb)
1N/A {
1N/A sfwrite(sp_body, rb, re - rb);
1N/A sfputc(sp_body, ' ');
1N/A }
1N/A if (w)
1N/A {
1N/A if (prefix > 0)
1N/A {
1N/A sfputc(sp_body, '-');
1N/A if (prefix > 1)
1N/A sfputc(sp_body, '-');
1N/A }
1N/A if (label(sp_body, 0, w, 0, -1, 0, style, FONT_BOLD, sp_info, version, id, catalog))
1N/A {
1N/A sfputc(sp_body, '|');
1N/A label(sp_body, 0, w, 0, -1, 0, style, FONT_BOLD, sp_info, version, id, native);
1N/A }
1N/A }
1N/A if (y)
1N/A {
1N/A if (a & OPT_optional)
1N/A sfputc(sp_body, '[');
1N/A else if (!w)
1N/A sfputc(sp_body, ' ');
1N/A if (w)
1N/A sfputc(sp_body, prefix == 1 ? ' ' : '=');
1N/A label(sp_body, 0, y, 0, -1, 0, style, FONT_ITALIC, sp_info, version, id, catalog);
1N/A if (a & OPT_optional)
1N/A sfputc(sp_body, ']');
1N/A }
1N/A }
1N/A if (style >= STYLE_match)
1N/A {
1N/A if (d)
1N/A {
1N/A textout(sp_body, d, cb, cl, style, 0, 3, sp_info, version, id, catalog);
1N/A cb = 0;
1N/A }
1N/A if (u)
1N/A textout(sp_body, u, cb, cl, style, 0, 3, sp_info, version, id, catalog);
1N/A if ((a & OPT_invert) && w && (d || u))
1N/A {
1N/A u = skip(w, ':', '?', 0, 1, 0, 0, version);
1N/A if (f)
1N/A sfprintf(sp_info, " %s; -\b%c\b %s --\bno%-.*s\b.", T(NiL, ID, "On by default"), f, T(NiL, ID, "means"), u - w, w);
1N/A else
1N/A sfprintf(sp_info, " %s %s\bno%-.*s\b %s.", T(NiL, ID, "On by default; use"), "--"+2-prefix, u - w, w, T(NiL, ID, "to turn off"));
1N/A if (!(t = sfstruse(sp_info)))
1N/A goto nospace;
1N/A textout(sp_body, t, 0, 0, style, 0, 0, sp_info, version, NiL, NiL);
1N/A }
1N/A if (*p == GO)
1N/A {
1N/A p = u ? skip(p + 1, 0, 0, 0, 0, 1, 1, version) : textout(sp_body, p, 0, 0, style, 4, 0, sp_info, version, id, catalog);
1N/A y = "+?";
1N/A }
1N/A else
1N/A y = " ";
1N/A if (a & OPT_optional)
1N/A {
1N/A if (ov)
1N/A {
1N/A sfprintf(sp_info, "%s%s \b", y, T(NiL, ID, "If the option value is omitted then"));
1N/A t = ov + ol;
1N/A while (ov < t)
1N/A {
1N/A if (((c = *ov++) == ':' || c == '?') && *ov == c)
1N/A ov++;
1N/A sfputc(sp_info, c);
1N/A }
1N/A sfprintf(sp_info, "\b %s.", T(NiL, ID, "is assumed"));
1N/A }
1N/A else
1N/A sfprintf(sp_info, "%s%s", y, T(NiL, ID, "The option value may be omitted."));
1N/A if (!(t = sfstruse(sp_info)))
1N/A goto nospace;
1N/A textout(sp_body, t, 0, 0, style, 4, 0, sp_info, version, NiL, NiL);
1N/A y = " ";
1N/A }
1N/A if (v)
1N/A {
1N/A sfprintf(sp_info, "%s%s \b", y, T(NiL, ID, "The default value is"));
1N/A t = v + vl;
1N/A while (v < t)
1N/A {
1N/A if (((c = *v++) == ':' || c == '?') && *v == c)
1N/A v++;
1N/A sfputc(sp_info, c);
1N/A }
1N/A sfputc(sp_info, '\b');
1N/A sfputc(sp_info, '.');
1N/A if (!(t = sfstruse(sp_info)))
1N/A goto nospace;
1N/A textout(sp_body, t, 0, 0, style, 4, 0, sp_info, version, NiL, NiL);
1N/A }
1N/A }
1N/A else if (!mutex)
1N/A sfputc(sp_body, ']');
1N/A }
1N/A if (*p == GO)
1N/A {
1N/A if (style >= STYLE_match)
1N/A p = textout(sp_body, p, 0, 0, style, 4, 0, sp_info, version, id, catalog);
1N/A else
1N/A p = skip(p + 1, 0, 0, 0, 0, 1, 1, version);
1N/A }
1N/A }
1N/A else if (*p == GO)
1N/A p = skip(p + 1, 0, 0, 0, 0, 1, 1, version);
1N/A }
1N/A psp = pop(psp);
1N/A if (sp_misc)
1N/A {
1N/A if (!(p = sfstruse(sp_misc)))
1N/A goto nospace;
1N/A for (t = p; *t == '\t' || *t == '\n'; t++);
1N/A if (*t)
1N/A {
1N/A item(sp_body, C("IMPLEMENTATION"), 0, 0, style, sp_info, version, id, ID);
1N/A sfputr(sp_body, p, -1);
1N/A }
1N/A }
1N/A }
1N/A if (oopts != o->oopts && oopts == top.oopts)
1N/A state.pass[0] = top;
1N/A version = o->version;
1N/A id = o->id;
1N/A catalog = o->catalog;
1N/A if (style >= STYLE_keys)
1N/A {
1N/A if (sp_info)
1N/A sfclose(sp_info);
1N/A if (style == STYLE_keys && sfstrtell(mp) > 1)
1N/A sfstrseek(mp, -1, SEEK_CUR);
1N/A if (!(p = sfstruse(mp)))
1N/A goto nospace;
1N/A return opt_info.msg = p;
1N/A }
1N/A sp = sp_text;
1N/A if (sfstrtell(sp) && style != STYLE_posix)
1N/A sfputc(sp, ']');
1N/A if (style == STYLE_nroff)
1N/A {
1N/A char rd[64];
1N/A char ud[64];
1N/A
1N/A s = o->id;
1N/A t = ud;
1N/A while (t < &ud[sizeof(ud)-2] && (c = *s++))
1N/A {
1N/A if (islower(c))
1N/A c = toupper(c);
1N/A *t++ = c;
1N/A }
1N/A *t = 0;
1N/A t = rd;
1N/A if (s = o->release)
1N/A {
1N/A *t++ = ' ';
1N/A while (t < &rd[sizeof(rd)-2] && (c = *s++) && c != ']')
1N/A *t++ = c;
1N/A }
1N/A *t = 0;
1N/A sfprintf(sp, "\
1N/A.\\\" format with nroff|troff|groff -man\n\
1N/A.fp 5 CW\n\
1N/A.nr mH 5\n\
1N/A.de H0\n\
1N/A.nr mH 0\n\
1N/A.in 5n\n\
1N/A\\fB\\\\$1\\fP\n\
1N/A.in 7n\n\
1N/A..\n\
1N/A.de H1\n\
1N/A.nr mH 1\n\
1N/A.in 7n\n\
1N/A\\fB\\\\$1\\fP\n\
1N/A.in 9n\n\
1N/A..\n\
1N/A.de H2\n\
1N/A.nr mH 2\n\
1N/A.in 11n\n\
1N/A\\fB\\\\$1\\fP\n\
1N/A.in 13n\n\
1N/A..\n\
1N/A.de H3\n\
1N/A.nr mH 3\n\
1N/A.in 15n\n\
1N/A\\fB\\\\$1\\fP\n\
1N/A.in 17n\n\
1N/A..\n\
1N/A.de H4\n\
1N/A.nr mH 4\n\
1N/A.in 19n\n\
1N/A\\fB\\\\$1\\fP\n\
1N/A.in 21n\n\
1N/A..\n\
1N/A.de OP\n\
1N/A.nr mH 0\n\
1N/A.ie !'\\\\$1'-' \\{\n\
1N/A.ds mO \\\\fB\\\\-\\\\$1\\\\fP\n\
1N/A.ds mS ,\\\\0\n\
1N/A.\\}\n\
1N/A.el \\{\n\
1N/A.ds mO \\\\&\n\
1N/A.ds mS \\\\&\n\
1N/A.\\}\n\
1N/A.ie '\\\\$2'-' \\{\n\
1N/A.if !'\\\\$4'-' .as mO \\\\0\\\\fI\\\\$4\\\\fP\n\
1N/A.\\}\n\
1N/A.el \\{\n\
1N/A.as mO \\\\*(mS\\\\fB%s\\\\$2\\\\fP\n\
1N/A.if !'\\\\$4'-' .as mO =\\\\fI\\\\$4\\\\fP\n\
1N/A.\\}\n\
1N/A.in 5n\n\
1N/A\\\\*(mO\n\
1N/A.in 9n\n\
1N/A..\n\
1N/A.de SP\n\
1N/A.if \\\\n(mH==2 .in 9n\n\
1N/A.if \\\\n(mH==3 .in 13n\n\
1N/A.if \\\\n(mH==4 .in 17n\n\
1N/A..\n\
1N/A.de FN\n\
1N/A.nr mH 0\n\
1N/A.in 5n\n\
1N/A\\\\$1 \\\\$2\n\
1N/A.in 9n\n\
1N/A..\n\
1N/A.de DS\n\
1N/A.in +3n\n\
1N/A.ft 5\n\
1N/A.nf\n\
1N/A..\n\
1N/A.de DE\n\
1N/A.fi\n\
1N/A.ft R\n\
1N/A.in -3n\n\
1N/A..\n\
1N/A.TH %s %s%s\n\
1N/A"
1N/A, o->prefix == 2 ? "\\\\-\\\\-" : o->prefix == 1 ? "\\\\-" : ""
1N/A, ud
1N/A, section
1N/A, rd
1N/A);
1N/A }
1N/A if (style == STYLE_match)
1N/A {
1N/A if (!matched)
1N/A {
1N/A if (hp = (Help_t*)search(styles, elementsof(styles), sizeof(styles[0]), (char*)what))
1N/A {
1N/A if (!sp_help && !(sp_help = sfstropen()))
1N/A goto nospace;
1N/A sfprintf(sp_help, "[-][:%s?%s]", hp->match, hp->text);
1N/A if (!(opts = sfstruse(sp_help)))
1N/A goto nospace;
1N/A goto again;
1N/A }
1N/A s = (char*)unknown;
1N/A goto nope;
1N/A }
1N/A else if (matched < 0)
1N/A x = 0;
1N/A }
1N/A if (sp_plus)
1N/A {
1N/A if (sfstrtell(sp_plus))
1N/A {
1N/A if (sfstrtell(sp))
1N/A sfputc(sp, ' ');
1N/A if (!(t = sfstruse(sp_plus)))
1N/A goto nospace;
1N/A sfputr(sp, t, ']');
1N/A }
1N/A sfclose(sp_plus);
1N/A }
1N/A if (style >= STYLE_man)
1N/A {
1N/A if (sp_head)
1N/A {
1N/A if (!(t = sfstruse(sp_head)))
1N/A goto nospace;
1N/A for (; *t == '\n'; t++);
1N/A sfputr(sp, t, '\n');
1N/A sfclose(sp_head);
1N/A sp_head = 0;
1N/A }
1N/A if (x)
1N/A item(sp, C("SYNOPSIS"), 0, 0, style, sp_info, version, id, ID);
1N/A }
1N/A if (x)
1N/A {
1N/A for (t = x + xl; t > x && (*(t - 1) == '\n' || *(t - 1) == '\r'); t--);
1N/A xl = t - x;
1N/A if (style >= STYLE_match)
1N/A {
1N/A u = id;
1N/A if (o->flags & OPT_functions)
1N/A t = 0;
1N/A else if (t = strchr(u, ':'))
1N/A {
1N/A if ((o->flags & OPT_module) && *(t + 1) == ':' && *(t + 2))
1N/A {
1N/A u = t + 2;
1N/A t = 0;
1N/A }
1N/A else
1N/A *t = 0;
1N/A }
1N/A args(sp, x, xl, o->flags, style, sp_info, version, u, catalog);
1N/A if (t)
1N/A *t = ':';
1N/A x = 0;
1N/A }
1N/A }
1N/A if (sp_body)
1N/A {
1N/A if (sfstrtell(sp_body))
1N/A {
1N/A if (style < STYLE_match && sfstrtell(sp))
1N/A sfputc(sp, ' ');
1N/A if (!(t = sfstruse(sp_body)))
1N/A goto nospace;
1N/A sfputr(sp, t, -1);
1N/A }
1N/A sfclose(sp_body);
1N/A sp_body = 0;
1N/A }
1N/A if (x && style != STYLE_posix)
1N/A args(sp, x, xl, flags, style, sp_info, version, id, catalog);
1N/A if (sp_info)
1N/A {
1N/A sfclose(sp_info);
1N/A sp_info = 0;
1N/A }
1N/A if (sp_misc)
1N/A {
1N/A sfclose(sp_misc);
1N/A sp_misc = 0;
1N/A }
1N/A if (!(p = sfstruse(sp)))
1N/A goto nospace;
1N/A astwinsize(1, NiL, &state.width);
1N/A if (state.width < 20)
1N/A state.width = OPT_WIDTH;
1N/A m = strlen((style <= STYLE_long && error_info.id && !strchr(error_info.id, '/')) ? error_info.id : id) + 1;
1N/A margin = style == STYLE_api ? (8 * 1024) : (state.width - 1);
1N/A if (!(state.flags & OPT_preformat))
1N/A {
1N/A if (style >= STYLE_man || matched < 0)
1N/A {
1N/A sfputc(mp, '\f');
1N/A ts = 0;
1N/A }
1N/A else
1N/A ts = OPT_USAGE + m;
1N/A if (style == STYLE_html)
1N/A {
1N/A char ud[64];
1N/A
1N/A s = id;
1N/A t = ud;
1N/A while (t < &ud[sizeof(ud)-2] && (c = *s++))
1N/A {
1N/A if (islower(c))
1N/A c = toupper(c);
1N/A *t++ = c;
1N/A }
1N/A *t = 0;
1N/A sfprintf(mp, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n<HTML>\n<HEAD>\n<META name=\"generator\" content=\"optget (AT&T Research) 2010-04-22\">\n%s<TITLE>%s man document</TITLE>\n</HEAD>\n<BODY bgcolor=white>\n", (state.flags & OPT_proprietary) ? "<!--INTERNAL-->\n" : "", id);
1N/A sfprintf(mp, "<H4><TABLE width=100%%><TR><TH align=left>&nbsp;%s&nbsp;(&nbsp;%s&nbsp;)&nbsp;<TH align=center><A href=\".\" title=\"Index\">%s</A><TH align=right>%s&nbsp;(&nbsp;%s&nbsp;)</TR></TABLE></H4>\n<HR>\n", ud, section, T(NiL, ID, secname(section)), ud, section);
1N/A sfprintf(mp, "<DL compact>\n<DT>");
1N/A co = 2;
1N/A *(pt = ptstk) = 0;
1N/A }
1N/A else
1N/A co = 0;
1N/A if ((rm = margin - ts) < OPT_MARGIN)
1N/A rm = OPT_MARGIN;
1N/A ip = indent;
1N/A ip->stop = (ip+1)->stop = style >= STYLE_html ? 0 : 2;
1N/A tp = 0;
1N/A n = 0;
1N/A head = 1;
1N/A while (*p == '\n')
1N/A p++;
1N/A while (c = *p++)
1N/A {
1N/A if (c == '\n')
1N/A {
1N/A ip = indent;
1N/A n = 0;
1N/A tp = 0;
1N/A sfputc(mp, '\n');
1N/A co = 0;
1N/A rm = margin;
1N/A ts = ip->stop;
1N/A if (*p == '\n')
1N/A {
1N/A while (*++p == '\n');
1N/A if ((style == STYLE_man || style == STYLE_html) && (!head || *p != ' ' && *p != '\t'))
1N/A {
1N/A if (style == STYLE_man)
1N/A p--;
1N/A else
1N/A sfprintf(mp, "<P>\n");
1N/A }
1N/A }
1N/A head = *p != ' ' && *p != '\t';
1N/A if (style == STYLE_html && (*p != '<' || !strneq(p, "<BR>", 4) && !strneq(p, "<P>", 3)))
1N/A {
1N/A y = p;
1N/A while (*p == '\t')
1N/A p++;
1N/A if (*p == '\n')
1N/A continue;
1N/A j = p - y;
1N/A if (j > *pt)
1N/A {
1N/A if (pt > ptstk)
1N/A sfprintf(mp, "<DL compact>\n");
1N/A *++pt = j;
1N/A sfprintf(mp, "<DL compact>\n");
1N/A }
1N/A else while (j < *pt)
1N/A {
1N/A if (--pt > ptstk)
1N/A sfprintf(mp, "</DL>\n");
1N/A sfprintf(mp, "</DL>\n");
1N/A }
1N/A co += sfprintf(mp, "<DT>");
1N/A }
1N/A }
1N/A else if (c == '\t')
1N/A {
1N/A if (style == STYLE_html)
1N/A {
1N/A while (*p == '\t')
1N/A p++;
1N/A if (*p != '\n')
1N/A co += sfprintf(mp, "<DD>");
1N/A }
1N/A else
1N/A {
1N/A if ((ip+1)->stop)
1N/A {
1N/A do
1N/A {
1N/A ip++;
1N/A if (*p != '\t')
1N/A break;
1N/A p++;
1N/A } while ((ip+1)->stop);
1N/A if (*p == '\n')
1N/A continue;
1N/A ts = ip->stop;
1N/A if (co >= ts)
1N/A {
1N/A sfputc(mp, '\n');
1N/A co = 0;
1N/A rm = margin;
1N/A ts = ip->stop;
1N/A }
1N/A }
1N/A while (co < ts)
1N/A {
1N/A sfputc(mp, ' ');
1N/A co++;
1N/A }
1N/A }
1N/A }
1N/A else
1N/A {
1N/A if (c == ' ' && !n)
1N/A {
1N/A if (co >= rm)
1N/A tp = 0;
1N/A else
1N/A {
1N/A tp = sfstrtell(mp);
1N/A pp = p;
1N/A }
1N/A if (style == STYLE_nroff && !co)
1N/A continue;
1N/A }
1N/A else if (style == STYLE_html)
1N/A {
1N/A if (c == '<')
1N/A {
1N/A if (strneq(p, "NOBR>", 5))
1N/A n++;
1N/A else if (n && strneq(p, "/NOBR>", 6) && !--n)
1N/A {
1N/A for (y = p += 6; (c = *p) && c != ' ' && c != '\t' && c != '\n' && c != '<'; p++)
1N/A if (c == '[')
1N/A sfputr(mp, "&#0091;", -1);
1N/A else if (c == ']')
1N/A sfputr(mp, "&#0093;", -1);
1N/A else
1N/A sfputc(mp, c);
1N/A sfwrite(mp, "</NOBR", 6);
1N/A c = '>';
1N/A tp = 0;
1N/A co += p - y + 6;
1N/A }
1N/A }
1N/A else if (c == '>' && !n)
1N/A {
1N/A for (y = --p; (c = *p) && c != ' ' && c != '\t' && c != '\n' && c != '<'; p++)
1N/A if (c == '[')
1N/A sfputr(mp, "&#0091;", -1);
1N/A else if (c == ']')
1N/A sfputr(mp, "&#0093;", -1);
1N/A else
1N/A sfputc(mp, c);
1N/A c = *sfstrseek(mp, -1, SEEK_CUR);
1N/A if (p > y + 1)
1N/A {
1N/A tp = 0;
1N/A co += p - y - 1;
1N/A }
1N/A if (co >= rm)
1N/A tp = 0;
1N/A else
1N/A {
1N/A tp = sfstrtell(mp);
1N/A pp = p;
1N/A }
1N/A }
1N/A else if (c == '[')
1N/A {
1N/A sfputr(mp, "&#0091", -1);
1N/A c = ';';
1N/A }
1N/A else if (c == ']')
1N/A {
1N/A sfputr(mp, "&#0093", -1);
1N/A c = ';';
1N/A }
1N/A else if (c == 'h')
1N/A {
1N/A y = p;
1N/A if (*y++ == 't' && *y++ == 't' && *y++ == 'p' && (*y == ':' || *y++ == 's' && *y == ':') && *y++ == ':' && *y++ == '/' && *y++ == '/')
1N/A {
1N/A while (isalnum(*y) || *y == '_' || *y == '/' || *y == '-' || *y == '.')
1N/A y++;
1N/A if (*y == '?')
1N/A while (isalnum(*y) || *y == '_' || *y == '/' || *y == '-' || *y == '.' || *y == '?' || *y == '=' || *y == '%' || *y == '&' || *y == ';' || *y == '#')
1N/A y++;
1N/A if (*(y - 1) == '.')
1N/A y--;
1N/A p--;
1N/A sfprintf(mp, "<A href=\"%-.*s\">%-.*s</A", y - p, p, y - p, p);
1N/A p = y;
1N/A c = '>';
1N/A }
1N/A }
1N/A else if (c == 'C')
1N/A {
1N/A y = p;
1N/A if (*y++ == 'o' && *y++ == 'p' && *y++ == 'y' && *y++ == 'r' && *y++ == 'i' && *y++ == 'g' && *y++ == 'h' && *y++ == 't' && *y++ == ' ' && *y++ == '(' && (*y++ == 'c' || *(y - 1) == 'C') && *y++ == ')')
1N/A {
1N/A sfputr(mp, "Copyright &copy", -1);
1N/A p = y;
1N/A c = ';';
1N/A }
1N/A }
1N/A }
1N/A else if (c == ']')
1N/A {
1N/A if (n)
1N/A n--;
1N/A }
1N/A else if (c == '[')
1N/A n++;
1N/A if (c == CC_esc)
1N/A {
1N/A sfputc(mp, c);
1N/A do
1N/A {
1N/A if (!(c = *p++))
1N/A {
1N/A p--;
1N/A break;
1N/A }
1N/A sfputc(mp, c);
1N/A } while (c < 'a' || c > 'z');
1N/A }
1N/A else if (co++ >= rm && !n)
1N/A {
1N/A if (tp)
1N/A {
1N/A if (*sfstrseek(mp, tp, SEEK_SET) != ' ')
1N/A sfstrseek(mp, 1, SEEK_CUR);
1N/A tp = 0;
1N/A p = pp;
1N/A n = 0;
1N/A }
1N/A else if (c != ' ' && c != '\n')
1N/A sfputc(mp, c);
1N/A if (*p == ' ')
1N/A p++;
1N/A if (*p != '\n')
1N/A {
1N/A sfputc(mp, '\n');
1N/A for (co = 0; co < ts; co++)
1N/A sfputc(mp, ' ');
1N/A rm = margin;
1N/A }
1N/A }
1N/A else
1N/A sfputc(mp, c);
1N/A }
1N/A }
1N/A for (d = sfstrbase(mp), t = sfstrseek(mp, 0, SEEK_CUR); t > d && ((c = *(t - 1)) == '\n' || c == '\r' || c == ' ' || c == '\t'); t--);
1N/A sfstrseek(mp, t - d, SEEK_SET);
1N/A if (style == STYLE_html)
1N/A {
1N/A while (pt > ptstk)
1N/A {
1N/A if (--pt > ptstk)
1N/A sfprintf(mp, "\n</DL>");
1N/A sfprintf(mp, "\n</DL>");
1N/A }
1N/A sfprintf(mp, "</DL>\n</BODY>\n</HTML>");
1N/A }
1N/A }
1N/A else
1N/A sfputr(mp, p, 0);
1N/A if (!(p = sfstruse(mp)))
1N/A goto nospace;
1N/A if (sp)
1N/A sfclose(sp);
1N/A return opt_info.msg = p;
1N/A nospace:
1N/A s = T(NiL, ID, "[* out of space *]");
1N/A nope:
1N/A if (psp)
1N/A pop(psp);
1N/A if (sp_help)
1N/A sfclose(sp_help);
1N/A if (sp_text)
1N/A sfclose(sp_text);
1N/A if (sp_plus)
1N/A sfclose(sp_plus);
1N/A if (sp_info)
1N/A sfclose(sp_info);
1N/A if (sp_head)
1N/A sfclose(sp_head);
1N/A if (sp_body)
1N/A sfclose(sp_body);
1N/A if (sp_misc)
1N/A sfclose(sp_misc);
1N/A return s;
1N/A}
1N/A
1N/A/*
1N/A * compatibility wrapper to opthelp()
1N/A */
1N/A
1N/Achar*
1N/Aoptusage(const char* opts)
1N/A{
1N/A return opthelp(opts, NiL);
1N/A}
1N/A
1N/A/*
1N/A * convert number using strtonll() *except* that
1N/A * 0*[[:digit:]].* is treated as [[:digit:]].*
1N/A * i.e., it looks octal but isn't, to meet
1N/A * posix Utility Argument Syntax -- use
1N/A * 0x.* or <base>#* for alternate bases
1N/A */
1N/A
1N/Astatic intmax_t
1N/Aoptnumber(const char* s, char** t, int* e)
1N/A{
1N/A intmax_t n;
1N/A int oerrno;
1N/A
1N/A while (*s == '0' && isdigit(*(s + 1)))
1N/A s++;
1N/A oerrno = errno;
1N/A errno = 0;
1N/A n = strtonll(s, t, NiL, 0);
1N/A if (e)
1N/A *e = errno;
1N/A errno = oerrno;
1N/A return n;
1N/A}
1N/A
1N/A/*
1N/A * point opt_info.arg to an error/info message for opt_info.name
1N/A * p points to opts location for opt_info.name
1N/A * optget() return value is returned
1N/A */
1N/A
1N/Astatic int
1N/Aopterror(register char* p, int err, int version, char* id, char* catalog)
1N/A{
1N/A register Sfio_t* mp;
1N/A register Sfio_t* tp;
1N/A register char* s;
1N/A register int c;
1N/A
1N/A if (opt_info.num != LONG_MIN)
1N/A opt_info.num = (long)(opt_info.number = 0);
1N/A if (!p || !(mp = state.mp) && !(mp = state.mp = sfstropen()))
1N/A goto nospace;
1N/A s = *p == '-' ? p : opt_info.name;
1N/A if (*p == '!')
1N/A {
1N/A while (*s == '-')
1N/A sfputc(mp, *s++);
1N/A sfputc(mp, 'n');
1N/A sfputc(mp, 'o');
1N/A }
1N/A sfputr(mp, s, ':');
1N/A sfputc(mp, ' ');
1N/A if (*p == '#' || *p == ':')
1N/A {
1N/A if (*p == '#')
1N/A {
1N/A s = T(NiL, ID, "numeric");
1N/A sfputr(mp, s, ' ');
1N/A }
1N/A if (*(p = next(p + 1, version)) == '[')
1N/A {
1N/A p = skip(s = p + 1, ':', '?', 0, 1, 0, 0, version);
1N/A tp = X(catalog) ? state.xp : mp;
1N/A while (s < p)
1N/A {
1N/A if ((c = *s++) == '?' || c == ']')
1N/A s++;
1N/A sfputc(tp, c);
1N/A }
1N/A if (!X(catalog))
1N/A sfputc(mp, ' ');
1N/A else if (p = sfstruse(tp))
1N/A sfputr(mp, T(id, catalog, p), ' ');
1N/A else
1N/A goto nospace;
1N/A }
1N/A p = opt_info.name[2] ? C("value expected") : C("argument expected");
1N/A }
1N/A else if (*p == '*' || *p == '&')
1N/A {
1N/A sfputr(mp, opt_info.arg, ':');
1N/A sfputc(mp, ' ');
1N/A p = *p == '&' ? C("ambiguous option argument value") : C("unknown option argument value");
1N/A }
1N/A else if (*p == '=' || *p == '!')
1N/A p = C("value not expected");
1N/A else if (*p == '?')
1N/A p = *(p + 1) == '?' ? C("optget: option not supported") : C("ambiguous option");
1N/A else if (*p == '+')
1N/A p = C("section not found");
1N/A else
1N/A {
1N/A if (opt_info.option[0] != '?' && opt_info.option[0] != '-' || opt_info.option[1] != '?' && opt_info.option[1] != '-')
1N/A opt_info.option[0] = 0;
1N/A p = C("unknown option");
1N/A }
1N/A p = T(NiL, ID, p);
1N/A sfputr(mp, p, -1);
1N/A if (err)
1N/A sfputr(mp, " -- out of range", -1);
1N/A if (opt_info.arg = sfstruse(mp))
1N/A return ':';
1N/A nospace:
1N/A opt_info.arg = T(NiL, ID, "[* out of space *]");
1N/A return ':';
1N/A}
1N/A
1N/A/*
1N/A * argv: command line argv where argv[0] is command name
1N/A *
1N/A * opts: option control string
1N/A *
1N/A * '[' [flag][=][index][:<long-name>[|<alias-name>...]['?'description]] ']'
1N/A * long option name, index, description; -index returned
1N/A * ':' option takes string arg
1N/A * '#' option takes numeric arg (concat option may follow)
1N/A * '?' (option) following options not in usage
1N/A * (following # or :) optional arg
1N/A * '[' '[' ... ] ... '[' ... ']' ']'
1N/A * mutually exclusive option grouping
1N/A * '[' name [:attr]* [?description] ']'
1N/A * (following # or :) optional option arg description
1N/A * '\n'[' '|'\t']* ignored for legibility
1N/A * ' ' ... optional argument(s) description (to end of string)
1N/A * or after blank line
1N/A * ']]' literal ']' within '[' ... ']'
1N/A *
1N/A * return:
1N/A * 0 no more options
1N/A * '?' usage: opt_info.arg points to message sans
1N/A * `Usage: command '
1N/A * ':' error: opt_info.arg points to message sans `command: '
1N/A *
1N/A * ':' '#' ' ' '[' ']'
1N/A * invalid option chars
1N/A *
1N/A * -- terminates option list and returns 0
1N/A *
1N/A * + as first opts char makes + equivalent to -
1N/A *
1N/A * if any # option is specified then numeric options (e.g., -123)
1N/A * are associated with the leftmost # option in opts
1N/A *
1N/A * usage info in placed opt_info.arg when '?' returned
1N/A * see help_text[] (--???) for more info
1N/A */
1N/A
1N/Aint
1N/Aoptget(register char** argv, const char* oopts)
1N/A{
1N/A register int c;
1N/A register char* s;
1N/A char* a;
1N/A char* b;
1N/A char* e;
1N/A char* f;
1N/A char* g;
1N/A char* v;
1N/A char* w;
1N/A char* p;
1N/A char* q;
1N/A char* t;
1N/A char* y;
1N/A char* numopt;
1N/A char* opts;
1N/A char* id;
1N/A char* catalog;
1N/A int n;
1N/A int m;
1N/A int k;
1N/A int j;
1N/A int x;
1N/A int err;
1N/A int no;
1N/A int nov;
1N/A int num;
1N/A int numchr;
1N/A int prefix;
1N/A int version;
1N/A Help_t* hp;
1N/A Push_t* psp;
1N/A Push_t* tsp;
1N/A Sfio_t* vp;
1N/A Sfio_t* xp;
1N/A Optcache_t* cache;
1N/A Optcache_t* pcache;
1N/A Optpass_t* pass;
1N/A
1N/A#if !_PACKAGE_astsa && !_YOU_FIGURED_OUT_HOW_TO_GET_ALL_DLLS_TO_DO_THIS_
1N/A /*
1N/A * these are not initialized by all dlls!
1N/A */
1N/A
1N/A extern Error_info_t _error_info_;
1N/A extern Opt_t _opt_info_;
1N/A
1N/A if (!_error_infop_)
1N/A _error_infop_ = &_error_info_;
1N/A if (!_opt_infop_)
1N/A _opt_infop_ = &_opt_info_;
1N/A#endif
1N/A if (!oopts)
1N/A return 0;
1N/A state.pindex = opt_info.index;
1N/A state.poffset = opt_info.offset;
1N/A if (!opt_info.index)
1N/A {
1N/A opt_info.index = 1;
1N/A opt_info.offset = 0;
1N/A if (state.npass)
1N/A {
1N/A state.npass = 0;
1N/A state.join = 0;
1N/A }
1N/A }
1N/A if (!argv)
1N/A cache = 0;
1N/A else
1N/A for (pcache = 0, cache = state.cache; cache; pcache = cache, cache = cache->next)
1N/A if (cache->pass.oopts == (char*)oopts)
1N/A break;
1N/A if (cache)
1N/A {
1N/A if (pcache)
1N/A {
1N/A pcache->next = cache->next;
1N/A cache->next = state.cache;
1N/A state.cache = cache;
1N/A }
1N/A pass = &cache->pass;
1N/A state.npass = -1;
1N/A }
1N/A else
1N/A {
1N/A if (!argv)
1N/A n = state.npass ? state.npass : 1;
1N/A else if ((n = state.join - 1) < 0)
1N/A n = 0;
1N/A if (n >= state.npass || state.pass[n].oopts != (char*)oopts)
1N/A {
1N/A for (m = 0; m < state.npass && state.pass[m].oopts != (char*)oopts; m++);
1N/A if (m < state.npass)
1N/A n = m;
1N/A else
1N/A {
1N/A if (n >= elementsof(state.pass))
1N/A n = elementsof(state.pass) - 1;
1N/A init((char*)oopts, &state.pass[n]);
1N/A if (state.npass <= n)
1N/A state.npass = n + 1;
1N/A }
1N/A }
1N/A if (!argv)
1N/A return 0;
1N/A pass = &state.pass[n];
1N/A }
1N/A opts = pass->opts;
1N/A prefix = pass->prefix;
1N/A version = pass->version;
1N/A id = pass->id;
1N/A if (!(xp = state.xp) || (catalog = pass->catalog) && !X(catalog))
1N/A catalog = 0;
1N/A else /* if (!error_info.catalog) */
1N/A error_info.catalog = catalog;
1N/A again:
1N/A psp = 0;
1N/A
1N/A /*
1N/A * check if any options remain and determine if the
1N/A * next option is short or long
1N/A */
1N/A
1N/A opt_info.assignment = 0;
1N/A num = 1;
1N/A w = v = 0;
1N/A x = 0;
1N/A for (;;)
1N/A {
1N/A if (!opt_info.offset)
1N/A {
1N/A /*
1N/A * finished with the previous arg
1N/A */
1N/A
1N/A if (opt_info.index == 1 && opt_info.argv != state.strv)
1N/A {
1N/A opt_info.argv = 0;
1N/A state.argv[0] = 0;
1N/A if (argv[0] && (state.argv[0] = save(argv[0], strlen(argv[0]), 0, 0, 0, 0)))
1N/A opt_info.argv = state.argv;
1N/A state.style = STYLE_short;
1N/A }
1N/A if (!(s = argv[opt_info.index]))
1N/A return 0;
1N/A if (!prefix)
1N/A {
1N/A /*
1N/A * long with no prefix (dd style)
1N/A */
1N/A
1N/A n = 2;
1N/A if ((c = *s) != '-' && c != '+')
1N/A c = '-';
1N/A else if (*++s == c)
1N/A {
1N/A if (!*++s)
1N/A {
1N/A opt_info.index++;
1N/A return 0;
1N/A }
1N/A else if (*s == c)
1N/A return 0;
1N/A }
1N/A else if (*s == '?')
1N/A n = 1;
1N/A }
1N/A else if ((c = *s++) != '-' && (c != '+' || !(pass->flags & OPT_plus) && (!(pass->flags & OPT_numeric) || !isdigit(*s))))
1N/A {
1N/A if (!(pass->flags & OPT_old) || !isalpha(c))
1N/A return 0;
1N/A s--;
1N/A n = 1;
1N/A opt_info.offset--;
1N/A }
1N/A else if (*s == c)
1N/A {
1N/A if (!*++s)
1N/A {
1N/A /*
1N/A * -- or ++ end of options
1N/A */
1N/A
1N/A opt_info.index++;
1N/A return 0;
1N/A }
1N/A else if (*s == c)
1N/A {
1N/A /*
1N/A * ---* or +++* are operands
1N/A */
1N/A
1N/A return 0;
1N/A }
1N/A if (version || *s == '?' || !(pass->flags & OPT_minus))
1N/A {
1N/A /*
1N/A * long with double prefix
1N/A */
1N/A
1N/A n = 2;
1N/A }
1N/A else
1N/A {
1N/A /*
1N/A * short option char '-'
1N/A */
1N/A
1N/A s--;
1N/A n = 1;
1N/A }
1N/A }
1N/A else if (prefix == 1 && *s != '?')
1N/A {
1N/A /*
1N/A * long with single prefix (find style)
1N/A */
1N/A
1N/A n = 2;
1N/A }
1N/A else
1N/A {
1N/A /*
1N/A * short (always with single prefix)
1N/A */
1N/A
1N/A n = 1;
1N/A }
1N/A
1N/A /*
1N/A * just a prefix is an option (e.g., `-' == stdin)
1N/A */
1N/A
1N/A if (!*s)
1N/A return 0;
1N/A if (c == '+')
1N/A opt_info.arg = 0;
1N/A if (n == 2)
1N/A {
1N/A x = 0;
1N/A state.style = STYLE_long;
1N/A opt_info.option[0] = opt_info.name[0] = opt_info.name[1] = c;
1N/A w = &opt_info.name[prefix];
1N/A if ((*s == 'n' || *s == 'N') && (*(s + 1) == 'o' || *(s + 1) == 'O') && *(s + 2) && *(s + 2) != '=')
1N/A no = *(s + 2) == '-' ? 3 : 2;
1N/A else
1N/A no = 0;
1N/A for (c = *s; *s; s++)
1N/A {
1N/A if (*s == '=')
1N/A {
1N/A if (*(s + 1) == '=')
1N/A s++;
1N/A if (!isalnum(*(s - 1)) && *(w - 1) == (opt_info.assignment = *(s - 1)))
1N/A w--;
1N/A v = ++s;
1N/A break;
1N/A }
1N/A if (w < &opt_info.name[elementsof(opt_info.name) - 1] && *s != ':' && *s != '|' && *s != '[' && *s != ']')
1N/A *w++ = *s;
1N/A }
1N/A *w = 0;
1N/A w = &opt_info.name[prefix];
1N/A c = *w;
1N/A opt_info.offset = 0;
1N/A opt_info.index++;
1N/A break;
1N/A }
1N/A opt_info.offset++;
1N/A }
1N/A if (!argv[opt_info.index])
1N/A return 0;
1N/A if (c = argv[opt_info.index][opt_info.offset++])
1N/A {
1N/A if ((k = argv[opt_info.index][0]) != '-' && k != '+')
1N/A k = '-';
1N/A opt_info.option[0] = opt_info.name[0] = k;
1N/A opt_info.option[1] = opt_info.name[1] = c;
1N/A opt_info.option[2] = opt_info.name[2] = 0;
1N/A break;
1N/A }
1N/A opt_info.offset = 0;
1N/A opt_info.index++;
1N/A }
1N/A
1N/A /*
1N/A * at this point:
1N/A *
1N/A * c the first character of the option
1N/A * w long option name if != 0, otherwise short
1N/A * v long option value (via =) if w != 0
1N/A */
1N/A
1N/A if (c == '?')
1N/A {
1N/A /*
1N/A * ? always triggers internal help
1N/A */
1N/A
1N/A if (!state.msgdict)
1N/A initdict();
1N/A if (w)
1N/A {
1N/A if (!v && (*(w + 1) || !(v = argv[opt_info.index]) || !++opt_info.index))
1N/A v = w + 1;
1N/A else if (w[0] != '?' || w[1])
1N/A {
1N/A s = w;
1N/A w = v;
1N/A v = s + 1;
1N/A }
1N/A }
1N/A opt_info.option[1] = c;
1N/A opt_info.option[2] = 0;
1N/A if (!w)
1N/A {
1N/A opt_info.name[1] = c;
1N/A opt_info.name[2] = 0;
1N/A }
1N/A goto help;
1N/A }
1N/A else if (w && !state.msgdict)
1N/A initdict();
1N/A numopt = 0;
1N/A f = 0;
1N/A s = opts;
1N/A
1N/A /*
1N/A * no option can start with these characters
1N/A */
1N/A
1N/A if (c == ':' || c == '#' || c == ' ' || c == '[' || c == ']')
1N/A {
1N/A if (c != *s)
1N/A s = "";
1N/A }
1N/A else
1N/A {
1N/A a = 0;
1N/A if (!w && (pass->flags & OPT_cache))
1N/A {
1N/A if (cache)
1N/A {
1N/A if (k = cache->flags[map[c]])
1N/A {
1N/A opt_info.arg = 0;
1N/A
1N/A /*
1N/A * this is a ksh getopts workaround
1N/A */
1N/A
1N/A if (opt_info.num != LONG_MIN)
1N/A opt_info.num = (long)(opt_info.number = !(k & OPT_cache_invert));
1N/A if (!(k & (OPT_cache_string|OPT_cache_numeric)))
1N/A return c;
1N/A if (*(opt_info.arg = &argv[opt_info.index++][opt_info.offset]))
1N/A {
1N/A if (!(k & OPT_cache_numeric))
1N/A {
1N/A opt_info.offset = 0;
1N/A return c;
1N/A }
1N/A opt_info.num = (long)(opt_info.number = optnumber(opt_info.arg, &e, &err));
1N/A if (err || e == opt_info.arg)
1N/A {
1N/A if (!err && (k & OPT_cache_optional))
1N/A {
1N/A opt_info.arg = 0;
1N/A opt_info.index--;
1N/A return c;
1N/A }
1N/A }
1N/A else if (*e)
1N/A {
1N/A opt_info.offset += e - opt_info.arg;
1N/A opt_info.index--;
1N/A return c;
1N/A }
1N/A else
1N/A {
1N/A opt_info.offset = 0;
1N/A return c;
1N/A }
1N/A }
1N/A else if (opt_info.arg = argv[opt_info.index])
1N/A {
1N/A opt_info.index++;
1N/A if ((k & OPT_cache_optional) && (*opt_info.arg == '-' || (pass->flags & OPT_plus) && *opt_info.arg == '+') && *(opt_info.arg + 1))
1N/A {
1N/A opt_info.arg = 0;
1N/A opt_info.index--;
1N/A opt_info.offset = 0;
1N/A return c;
1N/A }
1N/A if (k & OPT_cache_string)
1N/A {
1N/A opt_info.offset = 0;
1N/A return c;
1N/A }
1N/A opt_info.num = (long)(opt_info.number = optnumber(opt_info.arg, &e, &err));
1N/A if (!err)
1N/A {
1N/A if (!*e)
1N/A {
1N/A opt_info.offset = 0;
1N/A return c;
1N/A }
1N/A if (k & OPT_cache_optional)
1N/A {
1N/A opt_info.arg = 0;
1N/A opt_info.index--;
1N/A opt_info.offset = 0;
1N/A return c;
1N/A }
1N/A }
1N/A }
1N/A else if (k & OPT_cache_optional)
1N/A {
1N/A opt_info.offset = 0;
1N/A return c;
1N/A }
1N/A opt_info.index--;
1N/A }
1N/A cache = 0;
1N/A }
1N/A else if (cache = newof(0, Optcache_t, 1, 0))
1N/A {
1N/A cache->caching = c;
1N/A c = 0;
1N/A cache->pass = *pass;
1N/A cache->next = state.cache;
1N/A state.cache = cache;
1N/A }
1N/A }
1N/A else
1N/A cache = 0;
1N/A for (;;)
1N/A {
1N/A if (!(*(s = next(s, version))) || *s == '\n' || *s == ' ')
1N/A {
1N/A if (!(tsp = psp))
1N/A {
1N/A if (cache)
1N/A {
1N/A /*
1N/A * the first loop pass
1N/A * initialized the cache
1N/A * so one more pass to
1N/A * check the cache or
1N/A * bail for a full scan
1N/A */
1N/A
1N/A cache->flags[0] = 0;
1N/A c = cache->caching;
1N/A cache->caching = 0;
1N/A cache = 0;
1N/A s = opts;
1N/A continue;
1N/A }
1N/A if (!x && catalog)
1N/A {
1N/A /*
1N/A * the first loop pass
1N/A * translated long
1N/A * options and there
1N/A * were no matches so
1N/A * one more pass for C
1N/A * locale
1N/A */
1N/A
1N/A catalog = 0;
1N/A s = opts;
1N/A continue;
1N/A }
1N/A s = "";
1N/A break;
1N/A }
1N/A s = psp->ob;
1N/A psp = psp->next;
1N/A free(tsp);
1N/A continue;
1N/A }
1N/A if (*s == '\f')
1N/A {
1N/A psp = info(psp, s + 1, NiL, xp, id);
1N/A if (psp->nb)
1N/A s = psp->nb;
1N/A else
1N/A {
1N/A s = psp->ob;
1N/A psp = psp->next;
1N/A }
1N/A continue;
1N/A }
1N/A message((-20, "optget: opt %s c %c w %s num %ld", show(s), c, w, num));
1N/A if (*s == c && !w)
1N/A break;
1N/A else if (*s == '[')
1N/A {
1N/A s = next(s + 1, version);
1N/A if (*s == '(')
1N/A {
1N/A s = nest(f = s);
1N/A if (!conformance(f, s - f))
1N/A goto disable;
1N/A }
1N/A k = *(f = s);
1N/A if (k == '+' || k == '-')
1N/A /* ignore */;
1N/A else if (k == '[' || version < 1)
1N/A continue;
1N/A else if (w && !cache)
1N/A {
1N/A nov = no;
1N/A if (*(s + 1) == '\f' && (vp = state.vp))
1N/A {
1N/A sfputc(vp, k);
1N/A s = expand(s + 2, NiL, &t, vp, id);
1N/A if (*s)
1N/A *(f = s - 1) = k;
1N/A else
1N/A {
1N/A f = sfstrbase(vp);
1N/A if (s = strrchr(f, ':'))
1N/A f = s - 1;
1N/A else
1N/A s = f + 1;
1N/A }
1N/A }
1N/A else
1N/A t = 0;
1N/A if (*s != ':')
1N/A s = skip(s, ':', '?', 0, 1, 0, 0, version);
1N/A if (*s == ':')
1N/A {
1N/A if (catalog)
1N/A {
1N/A p = skip(s + 1, '?', 0, 0, 1, 0, 0, version);
1N/A e = sfprints("%-.*s", p - (s + 1), s + 1);
1N/A g = T(id, catalog, e);
1N/A if (g == e)
1N/A p = 0;
1N/A else
1N/A {
1N/A sfprintf(xp, ":%s|%s?", g, e);
1N/A if (!(s = sfstruse(xp)))
1N/A goto nospace;
1N/A }
1N/A }
1N/A else
1N/A p = 0;
1N/A y = w;
1N/A for (;;)
1N/A {
1N/A n = m = 0;
1N/A e = s + 1;
1N/A while (*++s)
1N/A {
1N/A if (*s == '*' || *s == '\a')
1N/A {
1N/A if (*s == '\a')
1N/A do
1N/A {
1N/A if (!*++s)
1N/A {
1N/A s--;
1N/A break;
1N/A }
1N/A } while (*s != '\a');
1N/A j = *(s + 1);
1N/A if (j == ':' || j == '|' || j == '?' || j == ']' || j == 0)
1N/A {
1N/A while (*w)
1N/A w++;
1N/A m = 0;
1N/A break;
1N/A }
1N/A m = 1;
1N/A }
1N/A else if (*s == *w || sep(*s) && sep(*w))
1N/A w++;
1N/A else if (*w == 0)
1N/A break;
1N/A else if (!sep(*s))
1N/A {
1N/A if (sep(*w))
1N/A {
1N/A if (*++w == *s)
1N/A {
1N/A w++;
1N/A continue;
1N/A }
1N/A }
1N/A else if (w == y || sep(*(w - 1)) || isupper(*(w - 1)) && islower(*w))
1N/A break;
1N/A for (q = s; *q && !sep(*q) && *q != '|' && *q != '?' && *q != ']'; q++);
1N/A if (!sep(*q))
1N/A break;
1N/A for (s = q; w > y && *w != *(s + 1); w--);
1N/A }
1N/A else if (*w != *(s + 1))
1N/A break;
1N/A }
1N/A if (!*w)
1N/A {
1N/A nov = 0;
1N/A break;
1N/A }
1N/A if (n = no)
1N/A {
1N/A m = 0;
1N/A s = e - 1;
1N/A w = y + n;
1N/A while (*++s)
1N/A {
1N/A if (*s == '*' || *s == '\a')
1N/A {
1N/A if (*s == '\a')
1N/A do
1N/A {
1N/A if (!*++s)
1N/A {
1N/A s--;
1N/A break;
1N/A }
1N/A } while (*s != '\a');
1N/A j = *(s + 1);
1N/A if (j == ':' || j == '|' || j == '?' || j == ']' || j == 0)
1N/A {
1N/A while (*w)
1N/A w++;
1N/A m = 0;
1N/A break;
1N/A }
1N/A m = 1;
1N/A }
1N/A else if (*s == *w || sep(*s) && sep(*w))
1N/A w++;
1N/A else if (*w == 0)
1N/A break;
1N/A else if (!sep(*s))
1N/A {
1N/A if (sep(*w))
1N/A {
1N/A if (*++w == *s)
1N/A {
1N/A w++;
1N/A continue;
1N/A }
1N/A }
1N/A else if (w == y || sep(*(w - 1)) || isupper(*(w - 1)) && islower(*w))
1N/A break;
1N/A for (q = s; *q && !sep(*q) && *q != '|' && *q != '?' && *q != ']'; q++);
1N/A if (!sep(*q))
1N/A break;
1N/A for (s = q; w > y && *w != *(s + 1); w--);
1N/A }
1N/A else if (*w != *(s + 1))
1N/A break;
1N/A }
1N/A if (!*w)
1N/A break;
1N/A }
1N/A if (*(s = skip(s, ':', '|', '?', 1, 0, 0, version)) != '|')
1N/A break;
1N/A w = y;
1N/A }
1N/A if (p)
1N/A s = p;
1N/A if (!*w)
1N/A {
1N/A if (n)
1N/A num = 0;
1N/A if (!(n = (m || *s == ':' || *s == '|' || *s == '?' || *s == ']' || *s == 0)) && x)
1N/A {
1N/A psp = pop(psp);
1N/A return opterror("?", 0, version, id, catalog);
1N/A }
1N/A for (x = k; *(f + 1) == '|' && (j = *(f + 2)) && j != '!' && j != '=' && j != ':' && j != '?' && j != ']'; f += 2);
1N/A if (*f == ':')
1N/A {
1N/A x = -1;
1N/A opt_info.option[1] = '-';
1N/A opt_info.option[2] = 0;
1N/A }
1N/A else if (*(f + 1) == ':' || *(f + 1) == '!' && *(f + 2) == ':')
1N/A {
1N/A opt_info.option[1] = x;
1N/A opt_info.option[2] = 0;
1N/A }
1N/A else
1N/A {
1N/A a = f;
1N/A if (*a == '=')
1N/A a++;
1N/A else
1N/A {
1N/A if (*(a + 1) == '!')
1N/A a++;
1N/A if (*(a + 1) == '=')
1N/A a += 2;
1N/A }
1N/A x = -strtol(a, &b, 0);
1N/A if ((b - a) > sizeof(opt_info.option) - 2)
1N/A b = a + sizeof(opt_info.option) - 2;
1N/A memcpy(&opt_info.option[1], a, b - a);
1N/A opt_info.option[b - a + 1] = 0;
1N/A }
1N/A b = e;
1N/A if (t)
1N/A {
1N/A s = t;
1N/A t = 0;
1N/A }
1N/A a = s = skip(s, 0, 0, 0, 1, 0, 0, version);
1N/A if (n)
1N/A {
1N/A w = y;
1N/A break;
1N/A }
1N/A }
1N/A w = y;
1N/A }
1N/A else if (k == c && prefix == 1)
1N/A {
1N/A w = 0;
1N/A opt_info.name[1] = c;
1N/A opt_info.name[2] = 0;
1N/A opt_info.offset = 2;
1N/A opt_info.index--;
1N/A break;
1N/A }
1N/A if (t)
1N/A {
1N/A s = t;
1N/A if (a)
1N/A a = t;
1N/A }
1N/A }
1N/A disable:
1N/A s = skip(s, 0, 0, 0, 1, 0, 1, version);
1N/A if (*s == GO)
1N/A s = skip(s + 1, 0, 0, 0, 0, 1, 1, version);
1N/A if (cache)
1N/A {
1N/A m = OPT_cache_flag;
1N/A v = s;
1N/A if (*v == '#')
1N/A {
1N/A v++;
1N/A m |= OPT_cache_numeric;
1N/A }
1N/A else if (*v == ':')
1N/A {
1N/A v++;
1N/A m |= OPT_cache_string;
1N/A }
1N/A if (*v == '?')
1N/A {
1N/A v++;
1N/A m |= OPT_cache_optional;
1N/A }
1N/A else if (*v == *(v - 1))
1N/A v++;
1N/A if (*(v = next(v, version)) == '[')
1N/A v = skip(v + 1, 0, 0, 0, 1, 0, 1, version);
1N/A if (*v != GO)
1N/A {
1N/A v = f;
1N/A for (;;)
1N/A {
1N/A if (isdigit(*f) && isdigit(*(f + 1)))
1N/A while (isdigit(*(f + 1)))
1N/A f++;
1N/A else if (*(f + 1) == '=')
1N/A break;
1N/A else
1N/A cache->flags[map[*f]] = m;
1N/A j = 0;
1N/A while (*(f + 1) == '|')
1N/A {
1N/A f += 2;
1N/A if (!(j = *f) || j == '!' || j == '=' || j == ':' || j == '?' || j == ']')
1N/A break;
1N/A cache->flags[map[j]] = m;
1N/A }
1N/A if (j != '!' || (m & OPT_cache_invert))
1N/A break;
1N/A f = v;
1N/A m |= OPT_cache_invert;
1N/A }
1N/A }
1N/A }
1N/A else
1N/A {
1N/A m = 0;
1N/A if (!w)
1N/A {
1N/A if (isdigit(*f) && isdigit(*(f + 1)))
1N/A k = -1;
1N/A if (c == k)
1N/A m = 1;
1N/A while (*(f + 1) == '|')
1N/A {
1N/A f += 2;
1N/A if (!(j = *f))
1N/A {
1N/A m = 0;
1N/A break;
1N/A }
1N/A else if (j == c)
1N/A m = 1;
1N/A else if (j == '!' || j == '=' || j == ':' || j == '?' || j == ']')
1N/A break;
1N/A }
1N/A }
1N/A if (m)
1N/A {
1N/A s--;
1N/A if (*++f == '!')
1N/A {
1N/A f++;
1N/A num = 0;
1N/A }
1N/A if (*f == '=')
1N/A {
1N/A c = -strtol(++f, &b, 0);
1N/A if ((b - f) > sizeof(opt_info.option) - 2)
1N/A b = f + sizeof(opt_info.option) - 2;
1N/A memcpy(&opt_info.option[1], f, b - f);
1N/A opt_info.option[b - f + 1] = 0;
1N/A }
1N/A else
1N/A c = k;
1N/A break;
1N/A }
1N/A }
1N/A if (*s == '#')
1N/A {
1N/A if (!numopt && s > opts)
1N/A {
1N/A numopt = s - 1;
1N/A numchr = k;
1N/A if (*f == ':')
1N/A numchr = -1;
1N/A else if (*(f + 1) != ':' && *(f + 1) != '!' && *(f + 1) != ']')
1N/A {
1N/A a = f;
1N/A if (*a == '=')
1N/A a++;
1N/A else
1N/A {
1N/A if (*(a + 1) == '!')
1N/A a++;
1N/A if (*(a + 1) == '=')
1N/A a += 2;
1N/A }
1N/A numchr = -strtol(a, NiL, 0);
1N/A }
1N/A }
1N/A }
1N/A else if (*s != ':')
1N/A continue;
1N/A }
1N/A else if (*s == ']')
1N/A {
1N/A s++;
1N/A continue;
1N/A }
1N/A else if (*s == '#')
1N/A {
1N/A if (!numopt && s > opts)
1N/A numchr = *(numopt = s - 1);
1N/A }
1N/A else if (*s != ':')
1N/A {
1N/A if (cache)
1N/A {
1N/A m = OPT_cache_flag;
1N/A if (*(s + 1) == '#')
1N/A {
1N/A m |= OPT_cache_numeric;
1N/A if (*(s + 2) == '?')
1N/A m |= OPT_cache_optional;
1N/A }
1N/A else if (*(s + 1) == ':')
1N/A {
1N/A m |= OPT_cache_string;
1N/A if (*(s + 2) == '?')
1N/A m |= OPT_cache_optional;
1N/A }
1N/A cache->flags[map[*s]] = m;
1N/A }
1N/A s++;
1N/A continue;
1N/A }
1N/A message((-21, "optget: opt %s", show(s)));
1N/A if (*++s == '?' || *s == *(s - 1))
1N/A s++;
1N/A if (*(s = next(s, version)) == '[')
1N/A {
1N/A s = skip(s + 1, 0, 0, 0, 1, 0, 1, version);
1N/A if (*s == GO)
1N/A s = skip(s + 1, 0, 0, 0, 0, 1, 1, version);
1N/A }
1N/A message((-21, "optget: opt %s", show(s)));
1N/A }
1N/A if (w && x)
1N/A {
1N/A s = skip(b, '|', '?', 0, 1, 0, 0, version);
1N/A if (v && (a == 0 || *a == 0 || *(a + 1) != ':' && *(a + 1) != '#') && (*v == '0' || *v == '1') && !*(v + 1))
1N/A {
1N/A if (*v == '0')
1N/A num = !num;
1N/A v = 0;
1N/A }
1N/A if ((s - b) >= elementsof(opt_info.name))
1N/A s = b + elementsof(opt_info.name) - 1;
1N/A for (;;)
1N/A {
1N/A if (b >= s)
1N/A {
1N/A *w = 0;
1N/A break;
1N/A }
1N/A if (*b == '*')
1N/A break;
1N/A *w++ = *b++;
1N/A }
1N/A if (!num && v)
1N/A return opterror(no ? "!" : "=", 0, version, id, catalog);
1N/A w = &opt_info.name[prefix];
1N/A c = x;
1N/A s = a;
1N/A }
1N/A }
1N/A if (!*s)
1N/A {
1N/A if (w)
1N/A {
1N/A if (hp = (Help_t*)search(styles, elementsof(styles), sizeof(styles[0]), w))
1N/A {
1N/A if (!v)
1N/A v = (char*)hp->name;
1N/A goto help;
1N/A }
1N/A if (!v)
1N/A {
1N/A v = opt_info.name;
1N/A goto help;
1N/A }
1N/A }
1N/A if (w || !isdigit(c) || !numopt || !(pass->flags & OPT_numeric))
1N/A {
1N/A pop(psp);
1N/A return opterror("", 0, version, id, catalog);
1N/A }
1N/A s = numopt;
1N/A c = opt_info.option[1] = numchr;
1N/A opt_info.offset--;
1N/A }
1N/A opt_info.arg = 0;
1N/A
1N/A /*
1N/A * this is a ksh getopts workaround
1N/A */
1N/A
1N/A if (opt_info.num != LONG_MIN)
1N/A opt_info.num = (long)(opt_info.number = num);
1N/A if ((n = *++s == '#') || *s == ':' || w && !nov && v && (optnumber(v, &e, NiL), n = !*e))
1N/A {
1N/A if (w)
1N/A {
1N/A if (nov)
1N/A {
1N/A if (v)
1N/A {
1N/A pop(psp);
1N/A return opterror("!", 0, version, id, catalog);
1N/A }
1N/A opt_info.num = (long)(opt_info.number = 0);
1N/A }
1N/A else
1N/A {
1N/A if (!v && *(s + 1) != '?' && (v = argv[opt_info.index]))
1N/A {
1N/A opt_info.index++;
1N/A opt_info.offset = 0;
1N/A }
1N/A if (!(opt_info.arg = v) || (*v == '0' || *v == '1') && !*(v + 1))
1N/A {
1N/A if (*(s + 1) != '?')
1N/A {
1N/A if (!opt_info.arg)
1N/A {
1N/A pop(psp);
1N/A return opterror(s, 0, version, id, catalog);
1N/A }
1N/A }
1N/A else if (*(t = next(s + 2, version)) == '[')
1N/A while (*(t = skip(t, ':', 0, 0, 1, 0, 0, version)) == ':')
1N/A if (*++t == '!')
1N/A {
1N/A if (!v || *v == '1')
1N/A {
1N/A e = skip(t, ':', '?', ']', 1, 0, 0, version);
1N/A opt_info.arg = sfprints("%-.*s", e - t - 1, t + 1);
1N/A }
1N/A else
1N/A {
1N/A opt_info.arg = 0;
1N/A opt_info.num = (long)(opt_info.number = 0);
1N/A }
1N/A break;
1N/A }
1N/A }
1N/A if (opt_info.arg && n)
1N/A {
1N/A opt_info.num = (long)(opt_info.number = optnumber(opt_info.arg, &e, &err));
1N/A if (err || e == opt_info.arg)
1N/A {
1N/A pop(psp);
1N/A return opterror(s, err, version, id, catalog);
1N/A }
1N/A }
1N/A }
1N/A goto optarg;
1N/A }
1N/A else if (*(opt_info.arg = &argv[opt_info.index++][opt_info.offset]))
1N/A {
1N/A if (*s == '#')
1N/A {
1N/A opt_info.num = (long)(opt_info.number = optnumber(opt_info.arg, &e, &err));
1N/A if (err || e == opt_info.arg)
1N/A {
1N/A if (!err && *(s + 1) == '?')
1N/A {
1N/A opt_info.arg = 0;
1N/A opt_info.index--;
1N/A }
1N/A else
1N/A {
1N/A opt_info.offset = 0;
1N/A c = opterror(s, err, version, id, catalog);
1N/A }
1N/A pop(psp);
1N/A return c;
1N/A }
1N/A else if (*e)
1N/A {
1N/A opt_info.offset += e - opt_info.arg;
1N/A opt_info.index--;
1N/A pop(psp);
1N/A return c;
1N/A }
1N/A }
1N/A }
1N/A else if (opt_info.arg = argv[opt_info.index])
1N/A {
1N/A opt_info.index++;
1N/A if (*(s + 1) == '?' && (*opt_info.arg == '-' || (pass->flags & OPT_plus) && *opt_info.arg == '+') && *(opt_info.arg + 1))
1N/A {
1N/A opt_info.index--;
1N/A opt_info.arg = 0;
1N/A }
1N/A else if (*s == '#')
1N/A {
1N/A opt_info.num = (long)(opt_info.number = optnumber(opt_info.arg, &e, &err));
1N/A if (err || *e)
1N/A {
1N/A if (!err && *(s + 1) == '?')
1N/A {
1N/A opt_info.arg = 0;
1N/A opt_info.index--;
1N/A }
1N/A else
1N/A {
1N/A pop(psp);
1N/A opt_info.offset = 0;
1N/A return opterror(s, err, version, id, catalog);
1N/A }
1N/A }
1N/A }
1N/A }
1N/A else if (*(s + 1) != '?')
1N/A {
1N/A opt_info.index--;
1N/A pop(psp);
1N/A return opterror(s, 0, version, id, catalog);
1N/A }
1N/A opt_info.offset = 0;
1N/A optarg:
1N/A if (*s == ':' && *(s = skip(s, 0, 0, 0, 1, 0, 1, version)) == GO && *(s = next(s + 1, version)) == '[' && isalnum(*(s + 1)))
1N/A {
1N/A x = 0;
1N/A if (opt_info.arg)
1N/A {
1N/A do
1N/A {
1N/A w = y = opt_info.arg;
1N/A f = s = next(s + 1, version);
1N/A k = *f;
1N/A if (k == *w && isalpha(k) && !*(w + 1))
1N/A {
1N/A x = k;
1N/A break;
1N/A }
1N/A if (*s == '+' || *s == '-')
1N/A continue;
1N/A else if (*s == '[' || version < 1)
1N/A continue;
1N/A else
1N/A {
1N/A if (*s != ':')
1N/A s = skip(s, ':', '?', 0, 1, 0, 0, version);
1N/A if (*s == ':')
1N/A {
1N/A if (catalog)
1N/A {
1N/A p = skip(s + 1, '?', 0, 0, 1, 0, 0, version);
1N/A e = sfprints("%-.*s", p - (s + 1), s + 1);
1N/A b = T(id, catalog, e);
1N/A if (b == e)
1N/A p = 0;
1N/A else
1N/A {
1N/A sfprintf(xp, ":%s|%s?", b, e);
1N/A if (!(s = sfstruse(xp)))
1N/A goto nospace;
1N/A }
1N/A }
1N/A else
1N/A p = 0;
1N/A for (;;)
1N/A {
1N/A n = m = 0;
1N/A e = s + 1;
1N/A while (*++s)
1N/A {
1N/A if (*s == '*' || *s == '\a')
1N/A {
1N/A if (*s == '\a')
1N/A do
1N/A {
1N/A if (!*++s)
1N/A {
1N/A s--;
1N/A break;
1N/A }
1N/A } while (*s != '\a');
1N/A j = *(s + 1);
1N/A if (j == ':' || j == '|' || j == '?' || j == ']' || j == 0)
1N/A {
1N/A while (*w)
1N/A w++;
1N/A m = 0;
1N/A break;
1N/A }
1N/A m = 1;
1N/A }
1N/A else if (*s == *w || sep(*s) && sep(*w))
1N/A w++;
1N/A else if (*w == 0)
1N/A break;
1N/A else if (!sep(*s))
1N/A {
1N/A if (sep(*w))
1N/A {
1N/A if (*++w == *s)
1N/A {
1N/A w++;
1N/A continue;
1N/A }
1N/A }
1N/A else if (w == y || sep(*(w - 1)) || isupper(*(w - 1)) && islower(*w))
1N/A break;
1N/A for (q = s; *q && !sep(*q) && *q != '|' && *q != '?' && *q != ']'; q++);
1N/A if (!sep(*q))
1N/A break;
1N/A for (s = q; w > y && *w != *(s + 1); w--);
1N/A }
1N/A else if (*w != *(s + 1))
1N/A break;
1N/A }
1N/A if (!*w)
1N/A {
1N/A nov = 0;
1N/A break;
1N/A }
1N/A if (*(s = skip(s, ':', '|', '?', 1, 0, 0, version)) != '|')
1N/A break;
1N/A w = y;
1N/A }
1N/A if (p)
1N/A s = p;
1N/A if (!*w)
1N/A {
1N/A if (n)
1N/A num = 0;
1N/A if (!(n = (m || *s == ':' || *s == '|' || *s == '?' || *s == ']')) && x)
1N/A {
1N/A pop(psp);
1N/A return opterror("&", 0, version, id, catalog);
1N/A }
1N/A for (x = k; *(f + 1) == '|' && (j = *(f + 2)) && j != '!' && j != '=' && j != ':' && j != '?' && j != ']'; f += 2);
1N/A if (*f == ':')
1N/A x = -1;
1N/A else if (*(f + 1) == ':' || *(f + 1) == '!' && *(f + 2) == ':')
1N/A /* ok */;
1N/A else
1N/A {
1N/A a = f;
1N/A if (*a == '=')
1N/A a++;
1N/A else
1N/A {
1N/A if (*(a + 1) == '!')
1N/A a++;
1N/A if (*(a + 1) == '=')
1N/A a += 2;
1N/A }
1N/A x = -strtol(a, &b, 0);
1N/A }
1N/A b = e;
1N/A a = s = skip(s, 0, 0, 0, 1, 0, 0, version);
1N/A if (n)
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A } while (*(s = skip(s, 0, 0, 0, 1, 0, 1, version)) == '[');
1N/A if (!(opt_info.num = (long)(opt_info.number = x)))
1N/A {
1N/A pop(psp);
1N/A return opterror("*", 0, version, id, catalog);
1N/A }
1N/A }
1N/A }
1N/A }
1N/A else if (w && v)
1N/A {
1N/A pop(psp);
1N/A return opterror("=", 0, version, id, catalog);
1N/A }
1N/A else
1N/A {
1N/A opt_info.num = (long)(opt_info.number = num);
1N/A if (!w && !argv[opt_info.index][opt_info.offset])
1N/A {
1N/A opt_info.offset = 0;
1N/A opt_info.index++;
1N/A }
1N/A }
1N/A pop(psp);
1N/A return c;
1N/A help:
1N/A if (v && *v == '?' && *(v + 1) == '?' && *(v + 2))
1N/A {
1N/A s = v + 2;
1N/A if ((s[0] == 'n' || s[0] == 'N') && (s[1] == 'o' || s[1] == 'O'))
1N/A {
1N/A s += 2;
1N/A n = -1;
1N/A }
1N/A else
1N/A n = 1;
1N/A if (hp = (Help_t*)search(styles, elementsof(styles), sizeof(styles[0]), s))
1N/A {
1N/A if (hp->style < STYLE_man || !(s = argv[opt_info.index]) || s[0] != '-' || s[1] != '-' || !s[2])
1N/A {
1N/A opt_info.arg = sfprints("\fversion=%d", version);
1N/A pop(psp);
1N/A return '?';
1N/A }
1N/A state.force = hp->style;
1N/A }
1N/A else if (match(s, "CONFORMANCE", -1, ID, NiL))
1N/A {
1N/A opt_info.arg = sfprints("\f%s", conformance(w, 0));
1N/A pop(psp);
1N/A return '?';
1N/A }
1N/A else if (match(s, "ESC", -1, ID, NiL) || match(s, "EMPHASIS", -1, ID, NiL))
1N/A state.emphasis = n;
1N/A else if (match(s, "MAN", -1, ID, NiL))
1N/A {
1N/A opt_info.arg = sfprints("\f%s", secname(*w != '?' ? w : pass->section));
1N/A pop(psp);
1N/A return '?';
1N/A }
1N/A else if (match(s, "PREFORMAT", -1, ID, NiL))
1N/A state.flags |= OPT_preformat;
1N/A else if (match(s, "SECTION", -1, ID, NiL))
1N/A {
1N/A opt_info.arg = sfprints("\f%s", pass->section);
1N/A pop(psp);
1N/A return '?';
1N/A }
1N/A else if (match(s, "TEST", -1, ID, NiL))
1N/A {
1N/A state.width = OPT_WIDTH;
1N/A state.emphasis = 1;
1N/A }
1N/A else
1N/A {
1N/A pop(psp);
1N/A return opterror(v, 0, version, id, catalog);
1N/A }
1N/A psp = pop(psp);
1N/A if (argv == state.strv)
1N/A return '#';
1N/A goto again;
1N/A }
1N/A if ((opt_info.arg = opthelp(NiL, v)) == (char*)unknown)
1N/A {
1N/A pop(psp);
1N/A return opterror(v, 0, version, id, catalog);
1N/A }
1N/A pop(psp);
1N/A return '?';
1N/A nospace:
1N/A pop(psp);
1N/A return opterror(NiL, 0, 0, NiL, NiL);
1N/A}
1N/A
1N/A/*
1N/A * parse long options with 0,1,2 leading '-' or '+' from string and pass to optget()
1N/A * syntax is the unquoted
1N/A *
1N/A * <length> [-|+|--|++]<name>[[-+:|&=]=<value>\n (or \0 for the last)
1N/A *
1N/A * or the quoted
1N/A *
1N/A * [-|+|--|++][no]name[[-+:|&=]=['"{(]value[)}"']][, ]...
1N/A *
1N/A * with \x escapes passed to chresc()
1N/A *
1N/A * return '#' for `label:', with opt_info.name==label
1N/A * str[opt_info.offset] next arg
1N/A *
1N/A * optstr(s, 0)
1N/A * return '-' if arg, 0 otherwise
1N/A * optstr(0, opts)
1N/A * use previous parsed str
1N/A */
1N/A
1N/Aint
1N/Aoptstr(const char* str, const char* opts)
1N/A{
1N/A register char* s = (char*)str;
1N/A register Sfio_t* mp;
1N/A register int c;
1N/A register int ql;
1N/A register int qr;
1N/A register int qc;
1N/A int v;
1N/A char* e;
1N/A
1N/A again:
1N/A if (s)
1N/A {
1N/A if (!(mp = state.strp) && !(mp = state.strp = sfstropen()))
1N/A return 0;
1N/A if (state.str != s)
1N/A state.str = s;
1N/A else if (opt_info.index == 1)
1N/A s += opt_info.offset;
1N/A while (*s == ',' || *s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')
1N/A s++;
1N/A if (!*s)
1N/A {
1N/A state.str = 0;
1N/A return 0;
1N/A }
1N/A if (*s == '-' || *s == '+')
1N/A {
1N/A c = *s++;
1N/A sfputc(mp, c);
1N/A if (*s == c)
1N/A {
1N/A sfputc(mp, c);
1N/A s++;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A sfputc(mp, '-');
1N/A sfputc(mp, '-');
1N/A }
1N/A if (isdigit(*s) && (v = (int)strtol(s, &e, 10)) > 1 && isspace(*e) && --v <= strlen(s) && (s[v] == 0 || s[v] == '\n'))
1N/A {
1N/A s += v;
1N/A while (isspace(*++e));
1N/A sfwrite(mp, e, s - e);
1N/A }
1N/A else
1N/A {
1N/A while (*s && *s != ',' && *s != ' ' && *s != '\t' && *s != '\n' && *s != '\r' && *s != '=' && *s != ':')
1N/A sfputc(mp, *s++);
1N/A if ((c = *s) == ':' && *(s + 1) != '=')
1N/A {
1N/A opt_info.index = 1;
1N/A opt_info.offset = ++s - (char*)str;
1N/A if (!(s = sfstruse(mp)))
1N/A goto nospace;
1N/A s += 2;
1N/A e = opt_info.name;
1N/A while (e < &opt_info.name[sizeof(opt_info.name)-1] && (*e++ = *s++));
1N/A opt_info.arg = 0;
1N/A opt_info.num = (long)(opt_info.number = 0);
1N/A opt_info.option[0] = ':';
1N/A opt_info.option[1] = 0;
1N/A return '#';
1N/A }
1N/A if (c == ':' || c == '=')
1N/A {
1N/A sfputc(mp, c);
1N/A ql = qr = 0;
1N/A while (c = *++s)
1N/A {
1N/A if (c == '\\')
1N/A {
1N/A sfputc(mp, chresc(s, &e));
1N/A s = e - 1;
1N/A }
1N/A else if (c == qr)
1N/A {
1N/A if (qr != ql)
1N/A sfputc(mp, c);
1N/A if (--qc <= 0)
1N/A qr = ql = 0;
1N/A }
1N/A else if (c == ql)
1N/A {
1N/A sfputc(mp, c);
1N/A qc++;
1N/A }
1N/A else if (qr)
1N/A sfputc(mp, c);
1N/A else if (c == ',' || c == ' ' || c == '\t' || c == '\n' || c == '\r')
1N/A break;
1N/A else if (c == '"' || c == '\'')
1N/A {
1N/A ql = qr = c;
1N/A qc = 1;
1N/A }
1N/A else
1N/A {
1N/A sfputc(mp, c);
1N/A if (c == GO)
1N/A {
1N/A ql = c;
1N/A qr = OG;
1N/A qc = 1;
1N/A }
1N/A else if (c == '(')
1N/A {
1N/A ql = c;
1N/A qr = ')';
1N/A qc = 1;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A }
1N/A opt_info.argv = state.strv;
1N/A state.strv[0] = T(NiL, ID, "option");
1N/A if (!(state.strv[1] = sfstruse(mp)))
1N/A goto nospace;
1N/A state.strv[2] = 0;
1N/A opt_info.offset = s - (char*)str;
1N/A }
1N/A if (opts)
1N/A {
1N/A if (!state.strv[1])
1N/A {
1N/A state.str = 0;
1N/A return 0;
1N/A }
1N/A opt_info.index = 1;
1N/A v = opt_info.offset;
1N/A opt_info.offset = 0;
1N/A c = optget(state.strv, opts);
1N/A opt_info.index = 1;
1N/A opt_info.offset = v;
1N/A if (c == '#')
1N/A {
1N/A s = state.str;
1N/A goto again;
1N/A }
1N/A if ((c == '?' || c == ':') && (opt_info.arg[0] == '-' && opt_info.arg[1] == '-'))
1N/A opt_info.arg += 2;
1N/A s = opt_info.name;
1N/A if (*s++ == '-' && *s++ == '-' && *s)
1N/A {
1N/A e = opt_info.name;
1N/A while (*e++ = *s++);
1N/A }
1N/A }
1N/A else
1N/A c = '-';
1N/A return c;
1N/A nospace:
1N/A return opterror(NiL, 0, 0, NiL, NiL);
1N/A}