\n"
"
%s
\n", name);
else
fprintf (fp, ".SH %s\n", name);
in_section = 1;
}
}
/* Render a @item line to HTML. (LINE,LEN) gives the arguments of
* @item. Use NULL for LINE to close a possible open
. ITEMX
* flags a @itemx line. */
static void
write_html_item (FILE *fp, const char *line, size_t len, int itemx)
{
static int in_li;
const char *rest;
size_t n, n0;
int eol_action = 0;
int table_level = 0;
if (!itemx && in_li)
{
fprintf (fp, "\n");
in_li = 0;
}
if (line)
{
/* Trim the LF and skip leading spaces. */
if (len && line[len-1] == '\n')
len--;
for (; len && (*line == ' ' || *line == '\t'); len--, line++)
;
if (len)
{
rest = line;
for (n=0; n < len && !(*rest == ' ' || *rest == '\t'); n++, rest++)
;
n0 = n;
for (; n < len && (*rest == ' ' || *rest == '\t'); n++, rest++)
;
len -= n;
/* Now the first word is (LINE,N0) and the args are (REST,LEN) */
fprintf (fp, "%s
%.*s",
itemx? " ":"", (int)n0, line);
if (len)
{
fputs (" ", fp);
proc_texi_buffer (fp, rest, len, &table_level, &eol_action);
fputs ("", fp);
}
fputs ("\n", fp);
in_li = 1;
}
}
}
/* Process the texinfo command COMMAND (without the leading @) and
write output if needed to FP. REST is the remainder of the line
which should either point to an opening brace or to a white space.
The function returns the number of characters already processed
from REST. LEN is the usable length of REST. TABLE_LEVEL is used to
control the indentation of tables. */
static size_t
proc_texi_cmd (FILE *fp, const char *command, const char *rest, size_t len,
int *table_level, int *eol_action)
{
static struct {
const char *name; /* Name of the command. */
int what; /* What to do with this command. */
const char *lead_in; /* String to print with a opening brace. */
const char *lead_out;/* String to print with the closing brace. */
const char *html_in; /* Same as LEAD_IN but for HTML. */
const char *html_out;/* Same as LEAD_OUT but for HTML. */
} cmdtbl[] = {
{ "command", 0, "\\fB", "\\fR", "
", "" },
{ "code", 0, "\\fB", "\\fR", "
", "" },
{ "url", 0, "\\fB", "\\fR", "
", "" },
{ "sc", 0, "\\fB", "\\fR", "
", "" },
{ "var", 0, "\\fI", "\\fR", "
", "" },
{ "samp", 0, "\\(oq", "\\(cq" },
{ "kbd", 0, "\\(oq", "\\(cq" },
{ "file", 0, "\\(oq\\fI","\\fR\\(cq" },
{ "env", 0, "\\(oq\\fI","\\fR\\(cq" },
{ "acronym", 0 },
{ "dfn", 0 },
{ "option", 0, "\\fB", "\\fR", "
", "" },
{ "example", 1, ".RS 2\n.nf\n", NULL, "\n
\n", "\n
\n" },
{ "smallexample", 1, ".RS 2\n.nf\n", NULL, "\n
\n", "\n
\n" },
{ "asis", 7 },
{ "anchor", 7 },
{ "cartouche", 1 },
{ "ref", 0, "[", "]" },
{ "xref", 0, "See: [", "]" },
{ "pxref", 0, "see: [", "]" },
{ "uref", 0, "(\\fB", "\\fR)" },
{ "footnote",0, " ([", "])" },
{ "emph", 0, "\\fI", "\\fR", "
", "" },
{ "w", 1 },
{ "c", 5 },
{ "efindex", 1 },
{ "opindex", 1 },
{ "cpindex", 1 },
{ "cindex", 1 },
{ "noindent", 0 },
{ "section", 1 },
{ "chapter", 1 },
{ "subsection", 6, "\n.SS " },
{ "chapheading", 0},
{ "item", 2, ".TP\n.B " },
{ "itemx", 2, ".TQ\n.B " },
{ "table", 3, NULL, NULL, "
\n" },
{ "itemize", 3 },
{ "bullet", 0, "* " },
{ "*", 0, "\n.br"},
{ "/", 0 },
{ "end", 4 },
{ "quotation",1, ".RS\n\\fB" },
{ "value", 8 },
{ NULL }
};
size_t n;
int i;
const char *s;
const char *lead_out = NULL;
const char *html_out = NULL;
int ignore_args = 0;
for (i=0; cmdtbl[i].name && strcmp (cmdtbl[i].name, command); i++)
;
if (cmdtbl[i].name)
{
writestr (cmdtbl[i].lead_in, cmdtbl[i].html_in, fp);
lead_out = cmdtbl[i].lead_out;
html_out = cmdtbl[i].html_out;
switch (cmdtbl[i].what)
{
case 1: /* Throw away the entire line. */
s = memchr (rest, '\n', len);
return s? (s-rest)+1 : len;
case 2: /* Handle @item. */
if (htmlmode)
{
s = memchr (rest, '\n', len);
n = s? (s-rest)+1 : len;
write_html_item (fp, rest, n, !strcmp(cmdtbl[i].name, "itemx"));
return n;
}
break;
case 3: /* Handle table. */
if (++(*table_level) > 1)
{
write_html_item (fp, NULL, 0, 0);
writestr (".RS\n", "
\n", fp);
}
/* Now throw away the entire line. */
s = memchr (rest, '\n', len);
return s? (s-rest)+1 : len;
break;
case 4: /* Handle end. */
for (s=rest, n=len; n && (*s == ' ' || *s == '\t'); s++, n--)
;
if (n >= 5 && !memcmp (s, "table", 5)
&& (!n || s[5] == ' ' || s[5] == '\t' || s[5] == '\n'))
{
if (htmlmode)
write_html_item (fp, NULL, 0, 0);
if ((*table_level)-- > 1)
writestr (".RE\n", "
\n", fp);
else
writestr (".P\n", "\n", fp);
}
else if (n >= 7 && !memcmp (s, "example", 7)
&& (!n || s[7] == ' ' || s[7] == '\t' || s[7] == '\n'))
{
writestr (".fi\n.RE\n", "\n", fp);
}
else if (n >= 12 && !memcmp (s, "smallexample", 12)
&& (!n || s[12] == ' ' || s[12] == '\t' || s[12] == '\n'))
{
writestr (".fi\n.RE\n", "\n", fp);
}
else if (n >= 9 && !memcmp (s, "quotation", 9)
&& (!n || s[9] == ' ' || s[9] == '\t' || s[9] == '\n'))
{
writestr ("\\fR\n.RE\n", "xx", fp);
}
/* Now throw away the entire line. */
s = memchr (rest, '\n', len);
return s? (s-rest)+1 : len;
case 5: /* Handle special comments. */
for (s=rest, n=len; n && (*s == ' ' || *s == '\t'); s++, n--)
;
if (n >= 4 && !memcmp (s, "man:", 4))
{
s += 4;
n -= 4;
if (htmlmode)
{
if (!strncmp (s, ".RE\n", 4)
|| !strncmp (s, ".RS\n", 4))
;
else
inf ("unknown special comment \"man:\"");
}
else
{
for (; n && *s != '\n'; n--, s++)
writechr (*s, fp);
writechr ('\n', fp);
}
}
/* Now throw away the entire line. */
s = memchr (rest, '\n', len);
return s? (s-rest)+1 : len;
case 6:
*eol_action = 1;
break;
case 7:
ignore_args = 1;
break;
case 8:
ignore_args = 1;
if (*rest != '{')
{
err ("opening brace for command '%s' missing", command);
return len;
}
else
{
/* Find closing brace. */
for (s=rest+1, n=1; *s && n < len; s++, n++)
if (*s == '}')
break;
if (*s != '}')
{
err ("closing brace for command '%s' not found", command);
return len;
}
else
{
size_t rlen = s - (rest + 1);
macro_t m;
for (m = variablelist; m; m = m->next)
{
if (strlen (m->name) == rlen
&& !strncmp (m->name, rest+1, rlen))
break;
}
if (m)
writestr (m->value, m->value, fp);
else
inf ("texinfo variable '%.*s' is not set",
(int)rlen, rest+1);
}
}
break;
default:
break;
}
}
else /* macro */
{
macro_t m;
for (m = macrolist; m ; m = m->next)
if (!strcmp (m->name, command))
break;
if (m)
{
proc_texi_buffer (fp, m->value, strlen (m->value),
table_level, eol_action);
ignore_args = 1; /* Parameterized macros are not yet supported. */
}
else
inf ("texinfo command '%s' not supported (%.*s)", command,
(int)((s = memchr (rest, '\n', len)), (s? (s-rest) : len)), rest);
}
if (*rest == '{')
{
/* Find matching closing brace. */
for (s=rest+1, n=1, i=1; i && *s && n < len; s++, n++)
if (*s == '{')
i++;
else if (*s == '}')
i--;
if (i)
{
err ("closing brace for command '%s' not found", command);
return len;
}
if (n > 2 && !ignore_args)
proc_texi_buffer (fp, rest+1, n-2, table_level, eol_action);
}
else
n = 0;
writestr (lead_out, html_out, fp);
return n;
}
/* Process the string LINE with LEN bytes of Texinfo content. */
static void
proc_texi_buffer (FILE *fp, const char *line, size_t len,
int *table_level, int *eol_action)
{
const char *s;
char cmdbuf[256];
int cmdidx = 0;
int in_cmd = 0;
size_t n;
for (s=line; *s && len; s++, len--)
{
if (in_cmd)
{
if (in_cmd == 1)
{
switch (*s)
{
case '@': case '{': case '}':
writechr (*s, fp); in_cmd = 0;
break;
case ':': /* Not ending a sentence flag. */
in_cmd = 0;
break;
case '.': case '!': case '?': /* Ending a sentence. */
writechr (*s, fp); in_cmd = 0;
break;
case ' ': case '\t': case '\n': /* Non collapsing spaces. */
writechr (*s, fp); in_cmd = 0;
break;
default:
cmdidx = 0;
cmdbuf[cmdidx++] = *s;
in_cmd++;
break;
}
}
else if (*s == '{' || *s == ' ' || *s == '\t' || *s == '\n')
{
cmdbuf[cmdidx] = 0;
n = proc_texi_cmd (fp, cmdbuf, s, len, table_level, eol_action);
assert (n <= len);
s += n; len -= n;
s--; len++;
in_cmd = 0;
}
else if (cmdidx < sizeof cmdbuf -1)
cmdbuf[cmdidx++] = *s;
else
{
err ("texinfo command too long - ignored");
in_cmd = 0;
}
}
else if (*s == '@')
in_cmd = 1;
else if (*s == '\n')
{
switch (*eol_action)
{
case 1: /* Create a dummy paragraph. */
writestr ("\n\\ \n", "\n<-- dummy par -->\n", fp);
break;
default:
writechr (*s, fp);
}
*eol_action = 0;
}
else if (*s == '\\')
writestr ("\\\\", "\\\\", fp);
else
writechr (*s, fp);
}
if (in_cmd > 1)
{
cmdbuf[cmdidx] = 0;
n = proc_texi_cmd (fp, cmdbuf, s, len, table_level, eol_action);
assert (n <= len);
s += n; len -= n;
s--; len++;
/* in_cmd = 0; -- doc only */
}
}
/* Do something with the Texinfo line LINE. */
static void
parse_texi_line (FILE *fp, const char *line, int *table_level)
{
int eol_action = 0;
/* A quick test whether there are any texinfo commands. */
if (!strchr (line, '@'))
{
/* FIXME: In html mode escape HTML stuff. */
writestr (line, line, fp);
writechr ('\n', fp);
return;
}
proc_texi_buffer (fp, line, strlen (line), table_level, &eol_action);
writechr ('\n', fp);
}
/* Write all the lines LINES to FP. */
static void
write_content (FILE *fp, line_buffer_t lines)
{
line_buffer_t line;
int table_level = 0;
for (line = lines; line; line = line->next)
{
if (line->verbatim)
{
/* FIXME: IN HTML mode we need to employ a parser for roff
* markup. */
writestr (line->line, line->line, fp);
writechr ('\n', fp);
}
else
{
/* fputs ("TEXI---", fp); */
/* fputs (line->line, fp); */
/* fputs ("---\n", fp); */
parse_texi_line (fp, line->line, &table_level);
}
}
}
static int
is_standard_section (const char *name)
{
int i;
const char *s;
for (i=0; (s=standard_sections[i]); i++)
if (!strcmp (s, name))
return 1;
return 0;
}
/* Finish a page; that is sort the data and write it out to the file. */
static void
finish_page (void)
{
FILE *fp;
section_buffer_t sect = NULL;
int idx;
const char *s;
int i;
if (!thepage.name)
return; /* No page active. */
if (verbose)
inf ("finishing page '%s'", thepage.name);
if (opt_select)
{
if (!strcmp (opt_select, thepage.name))
{
inf ("selected '%s'", thepage.name );
fp = stdout;
}
else
{
fp = fopen ( "/dev/null", "w" );
if (!fp)
die ("failed to open /dev/null: %s\n", strerror (errno));
}
}
else if (opt_store)
{
if (verbose)
inf ("writing '%s'", thepage.name );
fp = fopen ( thepage.name, "w" );
if (!fp)
die ("failed to create '%s': %s\n", thepage.name, strerror (errno));
}
else
fp = stdout;
if (write_th (fp))
goto leave;
for (idx=0; (s=standard_sections[idx]); idx++)
{
for (i=0; i < thepage.n_sections; i++)
{
sect = thepage.sections + i;
if (sect->name && !strcmp (s, sect->name))
break;
}
if (i == thepage.n_sections)
sect = NULL;
if (sect)
{
write_sh (fp, sect->name);
write_content (fp, sect->lines);
/* Now continue with all non standard sections directly
following this one. */
for (i++; i < thepage.n_sections; i++)
{
sect = thepage.sections + i;
if (sect->name && is_standard_section (sect->name))
break;
if (sect->name)
{
write_sh (fp, sect->name);
write_content (fp, sect->lines);
}
}
}
}
write_sh (fp, NULL);
if (write_bottom (fp))
goto leave;
leave:
if (fp != stdout)
fclose (fp);
free (thepage.name);
thepage.name = NULL;
/* FIXME: Cleanup the content. */
}
/* Parse one Texinfo file and create manpages according to the
embedded instructions. */
static void
parse_file (const char *fname, FILE *fp, char **section_name, int in_pause)
{
char *line;
int lnr = 0;
/* Fixme: The following state variables don't carry over to include
files. */
int skip_to_end = 0; /* Used to skip over menu entries. */
int skip_sect_line = 0; /* Skip after @mansect. */
int item_indent = 0; /* How far is the current @item indented. */
/* Helper to define a macro. */
char *macroname = NULL;
char *macrovalue = NULL;
size_t macrovaluesize = 0;
size_t macrovalueused = 0;
line = xmalloc (LINESIZE);
while (fgets (line, LINESIZE, fp))
{
size_t n = strlen (line);
int got_line = 0;
char *p, *pend;
lnr++;
if (!n || line[n-1] != '\n')
{
err ("%s:%d: trailing linefeed missing, line too long or "
"embedded Nul character", fname, lnr);
break;
}
line[--n] = 0;
/* Kludge to allow indentation of tables. */
for (p=line; *p == ' ' || *p == '\t'; p++)
;
if (*p)
{
if (*p == '@' && !strncmp (p+1, "item", 4))
item_indent = p - line; /* Set a new indent level. */
else if (p - line < item_indent)
item_indent = 0; /* Switch off indention. */
if (item_indent)
{
memmove (line, line+item_indent, n - item_indent + 1);
n -= item_indent;
}
}
if (*line == '@')
{
for (p=line+1, n=1; *p && *p != ' ' && *p != '\t'; p++)
n++;
while (*p == ' ' || *p == '\t')
p++;
}
else
p = line;
/* Take action on macro. */
if (macroname)
{
if (n == 4 && !memcmp (line, "@end", 4)
&& (line[4]==' '||line[4]=='\t'||!line[4])
&& !strncmp (p, "macro", 5)
&& (p[5]==' '||p[5]=='\t'||!p[5]))
{
if (macrovalueused)
macrovalue[--macrovalueused] = 0; /* Kill the last LF. */
macrovalue[macrovalueused] = 0; /* Terminate macro. */
macrovalue = xrealloc (macrovalue, macrovalueused+1);
set_macro (macroname, macrovalue);
macrovalue = NULL;
free (macroname);
macroname = NULL;
}
else
{
if (macrovalueused + strlen (line) + 2 >= macrovaluesize)
{
macrovaluesize += strlen (line) + 256;
macrovalue = xrealloc (macrovalue, macrovaluesize);
}
strcpy (macrovalue+macrovalueused, line);
macrovalueused += strlen (line);
macrovalue[macrovalueused++] = '\n';
}
continue;
}
if (n >= 5 && !memcmp (line, "@node", 5)
&& (line[5]==' '||line[5]=='\t'||!line[5]))
{
/* Completey ignore @node lines. */
continue;
}
if (skip_sect_line)
{
skip_sect_line = 0;
if (!strncmp (line, "@section", 8)
|| !strncmp (line, "@subsection", 11)
|| !strncmp (line, "@chapheading", 12))
continue;
}
/* We only parse lines we need and ignore the rest. There are a
few macros used to control this as well as one @ifset
command. Parts we know about are saved away into containers
separate for each section. */
/* First process ifset/ifclear commands. */
if (*line == '@')
{
if (n == 6 && !memcmp (line, "@ifset", 6)
&& (line[6]==' '||line[6]=='\t'))
{
for (p=line+7; *p == ' ' || *p == '\t'; p++)
;
if (!*p)
{
err ("%s:%d: name missing after \"@ifset\"", fname, lnr);
continue;
}
for (pend=p; *pend && *pend != ' ' && *pend != '\t'; pend++)
;
*pend = 0; /* Ignore rest of the line. */
push_condition (p, 1, fname, lnr);
continue;
}
else if (n == 8 && !memcmp (line, "@ifclear", 8)
&& (line[8]==' '||line[8]=='\t'))
{
for (p=line+9; *p == ' ' || *p == '\t'; p++)
;
if (!*p)
{
err ("%s:%d: name missing after \"@ifsclear\"", fname, lnr);
continue;
}
for (pend=p; *pend && *pend != ' ' && *pend != '\t'; pend++)
;
*pend = 0; /* Ignore rest of the line. */
push_condition (p, 0, fname, lnr);
continue;
}
else if (n == 4 && !memcmp (line, "@end", 4)
&& (line[4]==' '||line[4]=='\t')
&& !strncmp (p, "ifset", 5)
&& (p[5]==' '||p[5]=='\t'||!p[5]))
{
pop_condition (1, fname, lnr);
continue;
}
else if (n == 4 && !memcmp (line, "@end", 4)
&& (line[4]==' '||line[4]=='\t')
&& !strncmp (p, "ifclear", 7)
&& (p[7]==' '||p[7]=='\t'||!p[7]))
{
pop_condition (0, fname, lnr);
continue;
}
}
/* Take action on ifset/ifclear. */
if (!cond_is_active)
continue;
/* Process commands. */
if (*line == '@')
{
if (skip_to_end
&& n == 4 && !memcmp (line, "@end", 4)
&& (line[4]==' '||line[4]=='\t'||!line[4]))
{
skip_to_end = 0;
}
else if (cond_in_verbatim)
{
got_line = 1;
}
else if (n == 6 && !memcmp (line, "@macro", 6))
{
macroname = xstrdup (p);
macrovalue = xmalloc ((macrovaluesize = 1024));
macrovalueused = 0;
}
else if (n == 4 && !memcmp (line, "@set", 4))
{
set_variable (p);
}
else if (n == 8 && !memcmp (line, "@manpage", 8))
{
free (*section_name);
*section_name = NULL;
finish_page ();
start_page (p);
in_pause = 0;
}
else if (n == 8 && !memcmp (line, "@mansect", 8))
{
if (!thepage.name)
err ("%s:%d: section outside of a man page", fname, lnr);
else
{
free (*section_name);
*section_name = ascii_strupr (xstrdup (p));
in_pause = 0;
skip_sect_line = 1;
}
}
else if (n == 9 && !memcmp (line, "@manpause", 9))
{
if (!*section_name)
err ("%s:%d: pausing outside of a man section", fname, lnr);
else if (in_pause)
err ("%s:%d: already pausing", fname, lnr);
else
in_pause = 1;
}
else if (n == 8 && !memcmp (line, "@mancont", 8))
{
if (!*section_name)
err ("%s:%d: continue outside of a man section", fname, lnr);
else if (!in_pause)
err ("%s:%d: continue while not pausing", fname, lnr);
else
in_pause = 0;
}
else if (n == 5 && !memcmp (line, "@menu", 5)
&& (line[5]==' '||line[5]=='\t'||!line[5]))
{
skip_to_end = 1;
}
else if (n == 8 && !memcmp (line, "@include", 8)
&& (line[8]==' '||line[8]=='\t'||!line[8]))
{
char *incname = xstrdup (p);
FILE *incfp = fopen (incname, "r");
if (!incfp && opt_include && *opt_include && *p != '/')
{
free (incname);
incname = xmalloc (strlen (opt_include) + 1
+ strlen (p) + 1);
strcpy (incname, opt_include);
if ( incname[strlen (incname)-1] != '/' )
strcat (incname, "/");
strcat (incname, p);
incfp = fopen (incname, "r");
}
if (!incfp)
err ("can't open include file '%s': %s",
incname, strerror (errno));
else
{
parse_file (incname, incfp, section_name, in_pause);
fclose (incfp);
}
free (incname);
}
else if (n == 4 && !memcmp (line, "@bye", 4)
&& (line[4]==' '||line[4]=='\t'||!line[4]))
{
break;
}
else if (!skip_to_end)
got_line = 1;
}
else if (!skip_to_end)
got_line = 1;
if (got_line && cond_in_verbatim)
add_content (*section_name, line, 1);
else if (got_line && thepage.name && *section_name && !in_pause)
add_content (*section_name, line, 0);
}
if (ferror (fp))
err ("%s:%d: read error: %s", fname, lnr, strerror (errno));
free (macroname);
free (macrovalue);
free (line);
}
static void
top_parse_file (const char *fname, FILE *fp)
{
char *section_name = NULL; /* Name of the current section or NULL
if not in a section. */
macro_t m;
while (macrolist)
{
macro_t next = macrolist->next;
free (macrolist->value);
free (macrolist);
macrolist = next;
}
while (variablelist)
{
macro_t next = variablelist->next;
free (variablelist->value);
free (variablelist);
variablelist = next;
}
for (m=predefinedmacrolist; m; m = m->next)
set_macro (m->name, xstrdup ("1"));
cond_is_active = 1;
cond_in_verbatim = 0;
parse_file (fname, fp, §ion_name, 0);
free (section_name);
finish_page ();
}
int
main (int argc, char **argv)
{
int last_argc = -1;
const char *s;
opt_source = "GNU";
opt_release = "";
/* Define default macros. The trick is that these macros are not
defined when using the actual texinfo renderer. */
add_predefined_macro ("isman");
add_predefined_macro ("manverb");
/* Option parsing. */
if (argc)
{
argc--; argv++;
}
while (argc && last_argc != argc )
{
last_argc = argc;
if (!strcmp (*argv, "--"))
{
argc--; argv++;
break;
}
else if (!strcmp (*argv, "--help"))
{
puts (
"Usage: " PGM " [OPTION] [FILE]\n"
"Extract man pages from a Texinfo source.\n\n"
" --html render output as HTML\n"
" --source NAME use NAME as source field\n"
" --release STRING use STRING as the release field\n"
" --date EPOCH use EPOCH as publication date\n"
" --store write output using @manpage name\n"
" --select NAME only output pages with @manpage NAME\n"
" --verbose enable extra informational output\n"
" --debug enable additional debug output\n"
" --help display this help and exit\n"
" -I DIR also search in include DIR\n"
" -D MACRO define MACRO to 1\n\n"
"With no FILE, or when FILE is -, read standard input.\n\n"
"Report bugs to
.");
exit (0);
}
else if (!strcmp (*argv, "--version"))
{
puts (PGM " " VERSION "\n"
"Copyright (C) 2005, 2017 g10 Code GmbH\n"
"This program comes with ABSOLUTELY NO WARRANTY.\n"
"This is free software, and you are welcome to redistribute it\n"
"under certain conditions. See the file COPYING for details.");
exit (0);
}
else if (!strcmp (*argv, "--html"))
{
htmlmode = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--verbose"))
{
verbose = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--quiet"))
{
quiet = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--debug"))
{
verbose = debug = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--source"))
{
argc--; argv++;
if (argc)
{
opt_source = *argv;
argc--; argv++;
}
}
else if (!strcmp (*argv, "--release"))
{
argc--; argv++;
if (argc)
{
opt_release = *argv;
argc--; argv++;
}
}
else if (!strcmp (*argv, "--date"))
{
argc--; argv++;
if (argc)
{
opt_date = *argv;
argc--; argv++;
}
}
else if (!strcmp (*argv, "--store"))
{
opt_store = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--select"))
{
argc--; argv++;
if (argc)
{
opt_select = strrchr (*argv, '/');
if (opt_select)
opt_select++;
else
opt_select = *argv;
argc--; argv++;
}
}
else if (!strcmp (*argv, "-I"))
{
argc--; argv++;
if (argc)
{
opt_include = *argv;
argc--; argv++;
}
}
else if (!strcmp (*argv, "-D"))
{
argc--; argv++;
if (argc)
{
add_predefined_macro (*argv);
argc--; argv++;
}
}
}
if (argc > 1)
die ("usage: " PGM " [OPTION] [FILE] (try --help for more information)\n");
/* Take care of supplied timestamp for reproducible builds. See
* https://reproducible-builds.org/specs/source-date-epoch/ */
if (!opt_date && (s = getenv ("SOURCE_DATE_EPOCH")) && *s)
opt_date = s;
/* Start processing. */
if (argc && strcmp (*argv, "-"))
{
FILE *fp = fopen (*argv, "rb");
if (!fp)
die ("%s:0: can't open file: %s", *argv, strerror (errno));
top_parse_file (*argv, fp);
fclose (fp);
}
else
top_parse_file ("-", stdin);
return !!any_error;
}
/*
Local Variables:
compile-command: "gcc -Wall -g -Wall -o yat2m yat2m.c"
End:
*/
diff --git a/src/g10lib.h b/src/g10lib.h
index eff6295f..6be0ab21 100644
--- a/src/g10lib.h
+++ b/src/g10lib.h
@@ -1,493 +1,493 @@
/* g10lib.h - Internal definitions for libgcrypt
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005
* 2007, 2011 Free Software Foundation, Inc.
*
* This file is part of Libgcrypt.
*
* Libgcrypt is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser general Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* Libgcrypt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see .
*/
/* This header is to be used inside of libgcrypt in place of gcrypt.h.
This way we can better distinguish between internal and external
usage of gcrypt.h. */
#ifndef G10LIB_H
#define G10LIB_H 1
#ifdef _GCRYPT_H
#error gcrypt.h already included
#endif
#ifndef _GCRYPT_IN_LIBGCRYPT
#error something is wrong with config.h
#endif
#include
#include
#include "visibility.h"
#include "types.h"
/* Attribute handling macros. */
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
#define JNLIB_GCC_M_FUNCTION 1
-#define JNLIB_GCC_A_NR __attribute__ ((noreturn))
+#define JNLIB_GCC_A_NR __attribute__ ((__noreturn__))
#define JNLIB_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a)))
#define JNLIB_GCC_A_NR_PRINTF( f, a ) \
- __attribute__ ((noreturn, format (printf,f,a)))
+ __attribute__ ((__noreturn__, format (printf,f,a)))
#define GCC_ATTR_NORETURN __attribute__ ((__noreturn__))
#else
#define JNLIB_GCC_A_NR
#define JNLIB_GCC_A_PRINTF( f, a )
#define JNLIB_GCC_A_NR_PRINTF( f, a )
#define GCC_ATTR_NORETURN
#endif
#if __GNUC__ >= 3
/* According to glibc this attribute is available since 2.8 however we
better play safe and use it only with gcc 3 or newer. */
#define GCC_ATTR_FORMAT_ARG(a) __attribute__ ((format_arg (a)))
#else
#define GCC_ATTR_FORMAT_ARG(a)
#endif
/* I am not sure since when the unused attribute is really supported.
In any case it it only needed for gcc versions which print a
warning. Thus let us require gcc >= 3.5. */
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 5 )
#define GCC_ATTR_UNUSED __attribute__ ((unused))
#else
#define GCC_ATTR_UNUSED
#endif
#if __GNUC__ > 3
#define NOINLINE_FUNC __attribute__((noinline))
#else
#define NOINLINE_FUNC
#endif
#if __GNUC__ >= 3
#define LIKELY(expr) __builtin_expect( !!(expr), 1 )
#define UNLIKELY(expr) __builtin_expect( !!(expr), 0 )
#define CONSTANT_P(expr) __builtin_constant_p( expr )
#else
#define LIKELY(expr) (!!(expr))
#define UNLIKELY(expr) (!!(expr))
#define CONSTANT_P(expr) (0)
#endif
/* Gettext macros. */
#define _(a) _gcry_gettext(a)
#define N_(a) (a)
/* Some handy macros */
#ifndef STR
#define STR(v) #v
#endif
#define STR2(v) STR(v)
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
#define DIMof(type,member) DIM(((type *)0)->member)
#define my_isascii(c) (!((c) & 0x80))
/*-- src/global.c -*/
extern int _gcry_global_any_init_done;
int _gcry_global_is_operational (void);
gcry_err_code_t _gcry_vcontrol (enum gcry_ctl_cmds cmd, va_list arg_ptr);
void _gcry_pre_syscall (void);
void _gcry_post_syscall (void);
int _gcry_get_debug_flag (unsigned int mask);
char *_gcry_get_config (int mode, const char *what);
/* Malloc functions and common wrapper macros. */
void *_gcry_malloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_calloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_malloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_calloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_realloc (void *a, size_t n);
char *_gcry_strdup (const char *string) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_xmalloc (size_t n) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_xcalloc (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_xmalloc_secure (size_t n) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_xcalloc_secure (size_t n, size_t m) _GCRY_GCC_ATTR_MALLOC;
void *_gcry_xrealloc (void *a, size_t n);
char *_gcry_xstrdup (const char * a) _GCRY_GCC_ATTR_MALLOC;
void _gcry_free (void *a);
int _gcry_is_secure (const void *a) _GCRY_GCC_ATTR_PURE;
#define xtrymalloc(a) _gcry_malloc ((a))
#define xtrycalloc(a,b) _gcry_calloc ((a),(b))
#define xtrymalloc_secure(a) _gcry_malloc_secure ((a))
#define xtrycalloc_secure(a,b) _gcry_calloc_secure ((a),(b))
#define xtryrealloc(a,b) _gcry_realloc ((a),(b))
#define xtrystrdup(a) _gcry_strdup ((a))
#define xmalloc(a) _gcry_xmalloc ((a))
#define xcalloc(a,b) _gcry_xcalloc ((a),(b))
#define xmalloc_secure(a) _gcry_xmalloc_secure ((a))
#define xcalloc_secure(a,b) _gcry_xcalloc_secure ((a),(b))
#define xrealloc(a,b) _gcry_xrealloc ((a),(b))
#define xstrdup(a) _gcry_xstrdup ((a))
#define xfree(a) _gcry_free ((a))
/*-- src/misc.c --*/
#if defined(JNLIB_GCC_M_FUNCTION) || __STDC_VERSION__ >= 199901L
void _gcry_bug (const char *file, int line,
const char *func) GCC_ATTR_NORETURN;
void _gcry_assert_failed (const char *expr, const char *file, int line,
const char *func) GCC_ATTR_NORETURN;
#else
void _gcry_bug (const char *file, int line);
void _gcry_assert_failed (const char *expr, const char *file, int line);
#endif
void _gcry_divide_by_zero (void) JNLIB_GCC_A_NR;
const char *_gcry_gettext (const char *key) GCC_ATTR_FORMAT_ARG(1);
void _gcry_fatal_error(int rc, const char *text ) JNLIB_GCC_A_NR;
void _gcry_logv (int level,
const char *fmt, va_list arg_ptr) JNLIB_GCC_A_PRINTF(2,0);
void _gcry_log( int level, const char *fmt, ... ) JNLIB_GCC_A_PRINTF(2,3);
void _gcry_log_bug( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2);
void _gcry_log_fatal( const char *fmt, ... ) JNLIB_GCC_A_NR_PRINTF(1,2);
void _gcry_log_error( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2);
void _gcry_log_info( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2);
void _gcry_log_debug( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2);
void _gcry_log_printf ( const char *fmt, ... ) JNLIB_GCC_A_PRINTF(1,2);
void _gcry_log_printhex (const char *text, const void *buffer, size_t length);
void _gcry_log_printmpi (const char *text, gcry_mpi_t mpi);
void _gcry_log_printsxp (const char *text, gcry_sexp_t sexp);
void _gcry_set_log_verbosity( int level );
int _gcry_log_verbosity( int level );
#ifdef JNLIB_GCC_M_FUNCTION
#define BUG() _gcry_bug( __FILE__ , __LINE__, __FUNCTION__ )
#define gcry_assert(expr) (LIKELY(expr)? (void)0 \
: _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __FUNCTION__))
#elif __STDC_VERSION__ >= 199901L
#define BUG() _gcry_bug( __FILE__ , __LINE__, __func__ )
#define gcry_assert(expr) (LIKELY(expr)? (void)0 \
: _gcry_assert_failed (STR(expr), __FILE__, __LINE__, __func__))
#else
#define BUG() _gcry_bug( __FILE__ , __LINE__ )
#define gcry_assert(expr) (LIKELY(expr)? (void)0 \
: _gcry_assert_failed (STR(expr), __FILE__, __LINE__))
#endif
#define log_bug _gcry_log_bug
#define log_fatal _gcry_log_fatal
#define log_error _gcry_log_error
#define log_info _gcry_log_info
#define log_debug _gcry_log_debug
#define log_printf _gcry_log_printf
#define log_printhex _gcry_log_printhex
#define log_printmpi _gcry_log_printmpi
#define log_printsxp _gcry_log_printsxp
/* Compatibility macro. */
#define log_mpidump _gcry_log_printmpi
/* Tokeninze STRING and return a malloced array. */
char **_gcry_strtokenize (const char *string, const char *delim);
/*-- src/hwfeatures.c --*/
#if defined(HAVE_CPU_ARCH_X86)
#define HWF_PADLOCK_RNG (1 << 0)
#define HWF_PADLOCK_AES (1 << 1)
#define HWF_PADLOCK_SHA (1 << 2)
#define HWF_PADLOCK_MMUL (1 << 3)
#define HWF_INTEL_CPU (1 << 4)
#define HWF_INTEL_FAST_SHLD (1 << 5)
#define HWF_INTEL_BMI2 (1 << 6)
#define HWF_INTEL_SSSE3 (1 << 7)
#define HWF_INTEL_SSE4_1 (1 << 8)
#define HWF_INTEL_PCLMUL (1 << 9)
#define HWF_INTEL_AESNI (1 << 10)
#define HWF_INTEL_RDRAND (1 << 11)
#define HWF_INTEL_AVX (1 << 12)
#define HWF_INTEL_AVX2 (1 << 13)
#define HWF_INTEL_FAST_VPGATHER (1 << 14)
#define HWF_INTEL_RDTSC (1 << 15)
#define HWF_INTEL_SHAEXT (1 << 16)
#define HWF_INTEL_VAES_VPCLMUL (1 << 17)
#define HWF_INTEL_AVX512 (1 << 18)
#define HWF_INTEL_GFNI (1 << 19)
#elif defined(HAVE_CPU_ARCH_ARM)
#define HWF_ARM_NEON (1 << 0)
#define HWF_ARM_AES (1 << 1)
#define HWF_ARM_SHA1 (1 << 2)
#define HWF_ARM_SHA2 (1 << 3)
#define HWF_ARM_PMULL (1 << 4)
#define HWF_ARM_SHA3 (1 << 5)
#define HWF_ARM_SM3 (1 << 6)
#define HWF_ARM_SM4 (1 << 7)
#define HWF_ARM_SHA512 (1 << 8)
#define HWF_ARM_SVE (1 << 9)
#define HWF_ARM_SVE2 (1 << 10)
#define HWF_ARM_SVEAES (1 << 11)
#define HWF_ARM_SVEPMULL (1 << 12)
#define HWF_ARM_SVESHA3 (1 << 13)
#define HWF_ARM_SVESM4 (1 << 14)
#elif defined(HAVE_CPU_ARCH_PPC)
#define HWF_PPC_VCRYPTO (1 << 0)
#define HWF_PPC_ARCH_3_00 (1 << 1)
#define HWF_PPC_ARCH_2_07 (1 << 2)
#define HWF_PPC_ARCH_3_10 (1 << 3)
#elif defined(HAVE_CPU_ARCH_S390X)
#define HWF_S390X_MSA (1 << 0)
#define HWF_S390X_MSA_4 (1 << 1)
#define HWF_S390X_MSA_8 (1 << 2)
#define HWF_S390X_MSA_9 (1 << 3)
#define HWF_S390X_VX (1 << 4)
#endif
gpg_err_code_t _gcry_disable_hw_feature (const char *name);
void _gcry_detect_hw_features (void);
unsigned int _gcry_get_hw_features (void);
const char *_gcry_enum_hw_features (int idx, unsigned int *r_feature);
/*-- mpi/mpiutil.c --*/
const char *_gcry_mpi_get_hw_config (void);
/*-- cipher/pubkey.c --*/
/* FIXME: shouldn't this go into mpi.h? */
#ifndef mpi_powm
#define mpi_powm(w,b,e,m) gcry_mpi_powm( (w), (b), (e), (m) )
#endif
/*-- primegen.c --*/
gcry_err_code_t _gcry_primegen_init (void);
gcry_mpi_t _gcry_generate_secret_prime (unsigned int nbits,
gcry_random_level_t random_level,
int (*extra_check)(void*, gcry_mpi_t),
void *extra_check_arg);
gcry_mpi_t _gcry_generate_public_prime (unsigned int nbits,
gcry_random_level_t random_level,
int (*extra_check)(void*, gcry_mpi_t),
void *extra_check_arg);
gcry_err_code_t _gcry_generate_elg_prime (int mode,
unsigned int pbits,
unsigned int qbits,
gcry_mpi_t g,
gcry_mpi_t *r_prime,
gcry_mpi_t **factors);
gcry_mpi_t _gcry_derive_x931_prime (const gcry_mpi_t xp,
const gcry_mpi_t xp1, const gcry_mpi_t xp2,
const gcry_mpi_t e,
gcry_mpi_t *r_p1, gcry_mpi_t *r_p2);
gpg_err_code_t _gcry_generate_fips186_2_prime
(unsigned int pbits, unsigned int qbits,
const void *seed, size_t seedlen,
gcry_mpi_t *r_q, gcry_mpi_t *r_p,
int *r_counter,
void **r_seed, size_t *r_seedlen);
gpg_err_code_t _gcry_generate_fips186_3_prime
(unsigned int pbits, unsigned int qbits,
const void *seed, size_t seedlen,
gcry_mpi_t *r_q, gcry_mpi_t *r_p,
int *r_counter,
void **r_seed, size_t *r_seedlen, int *r_hashalgo);
gpg_err_code_t _gcry_fips186_4_prime_check (const gcry_mpi_t x,
unsigned int bits);
/* Replacements of missing functions (missing-string.c). */
#ifndef HAVE_STPCPY
char *stpcpy (char *a, const char *b);
#endif
#ifndef HAVE_STRCASECMP
int strcasecmp (const char *a, const char *b) _GCRY_GCC_ATTR_PURE;
#endif
#include "../compat/libcompat.h"
/* Macros used to rename missing functions. */
#ifndef HAVE_STRTOUL
#define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c)))
#endif
#ifndef HAVE_MEMMOVE
#define memmove(d, s, n) bcopy((s), (d), (n))
#endif
#ifndef HAVE_STRICMP
#define stricmp(a,b) strcasecmp( (a), (b) )
#endif
#ifndef HAVE_ATEXIT
#define atexit(a) (on_exit((a),0))
#endif
#ifndef HAVE_RAISE
#define raise(a) kill(getpid(), (a))
#endif
/* Stack burning. */
#ifdef HAVE_GCC_ASM_VOLATILE_MEMORY
#define __gcry_burn_stack_dummy() asm volatile ("":::"memory")
#else
void __gcry_burn_stack_dummy (void);
#endif
void __gcry_burn_stack (unsigned int bytes);
#define _gcry_burn_stack(bytes) \
do { __gcry_burn_stack (bytes); \
__gcry_burn_stack_dummy (); } while(0)
/* To avoid that a compiler optimizes certain memset calls away, this
macro may be used instead. For constant length buffers, memory
wiping is inlined. Dead store elimination of inlined memset is
avoided here by using assembly block after memset. For non-constant
length buffers, memory is wiped through _gcry_fast_wipememory. */
#ifdef HAVE_GCC_ASM_VOLATILE_MEMORY
#define fast_wipememory2_inline(_ptr,_set,_len) do { \
memset((_ptr), (_set), (_len)); \
asm volatile ("\n" :: "r" (_ptr) : "memory"); \
} while(0)
#else
#define fast_wipememory2_inline(_ptr,_set,_len) \
_gcry_fast_wipememory2((void *)_ptr, _set, _len)
#endif
#define wipememory2(_ptr,_set,_len) do { \
if (!CONSTANT_P(_len) || !CONSTANT_P(_set)) { \
if (CONSTANT_P(_set) && (_set) == 0) \
_gcry_fast_wipememory((void *)(_ptr), (_len)); \
else \
_gcry_fast_wipememory2((void *)(_ptr), (_set), (_len)); \
} else { \
fast_wipememory2_inline((void *)(_ptr), (_set), (_len)); \
} \
} while(0)
#define wipememory(_ptr,_len) wipememory2((_ptr),0,(_len))
void _gcry_fast_wipememory(void *ptr, size_t len);
void _gcry_fast_wipememory2(void *ptr, int set, size_t len);
/* Digit predicates. */
#define digitp(p) (*(p) >= '0' && *(p) <= '9')
#define octdigitp(p) (*(p) >= '0' && *(p) <= '7')
#define alphap(a) ( (*(a) >= 'A' && *(a) <= 'Z') \
|| (*(a) >= 'a' && *(a) <= 'z'))
#define hexdigitp(a) (digitp (a) \
|| (*(a) >= 'A' && *(a) <= 'F') \
|| (*(a) >= 'a' && *(a) <= 'f'))
/* Init functions. */
gcry_err_code_t _gcry_cipher_init (void);
gcry_err_code_t _gcry_md_init (void);
gcry_err_code_t _gcry_mac_init (void);
gcry_err_code_t _gcry_pk_init (void);
gcry_err_code_t _gcry_secmem_module_init (void);
gcry_err_code_t _gcry_mpi_init (void);
/* Memory management. */
#define GCRY_ALLOC_FLAG_SECURE (1 << 0)
#define GCRY_ALLOC_FLAG_XHINT (1 << 1) /* Called from xmalloc. */
/*-- sexp.c --*/
gcry_err_code_t _gcry_sexp_vbuild (gcry_sexp_t *retsexp, size_t *erroff,
const char *format, va_list arg_ptr);
char *_gcry_sexp_nth_string (const gcry_sexp_t list, int number);
gpg_err_code_t _gcry_sexp_vextract_param (gcry_sexp_t sexp, const char *path,
const char *list, va_list arg_ptr);
/*-- fips.c --*/
extern int _gcry_no_fips_mode_required;
void _gcry_initialize_fips_mode (int force);
int _gcry_fips_to_activate (void);
/* This macro returns true if fips mode is enabled. This is
independent of the fips required finite state machine and only used
to enable fips specific code.
No locking is required because we have the requirement that this
variable is only initialized once with no other threads
existing. */
#define fips_mode() (!_gcry_no_fips_mode_required)
void _gcry_fips_signal_error (const char *srcfile,
int srcline,
const char *srcfunc,
int is_fatal,
const char *description);
#ifdef JNLIB_GCC_M_FUNCTION
# define fips_signal_error(a) \
_gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 0, (a))
# define fips_signal_fatal_error(a) \
_gcry_fips_signal_error (__FILE__, __LINE__, __FUNCTION__, 1, (a))
#else
# define fips_signal_error(a) \
_gcry_fips_signal_error (__FILE__, __LINE__, NULL, 0, (a))
# define fips_signal_fatal_error(a) \
_gcry_fips_signal_error (__FILE__, __LINE__, NULL, 1, (a))
#endif
int _gcry_fips_indicator_cipher (va_list arg_ptr);
int _gcry_fips_indicator_kdf (va_list arg_ptr);
int _gcry_fips_indicator_function (va_list arg_ptr);
int _gcry_fips_is_operational (void);
/* Return true if the library is in the operational state. */
#define fips_is_operational() \
(!_gcry_global_any_init_done ? \
_gcry_global_is_operational() : \
(!fips_mode () || _gcry_global_is_operational ()))
#define fips_not_operational() (GPG_ERR_NOT_OPERATIONAL)
int _gcry_fips_test_operational (void);
int _gcry_fips_test_error_or_operational (void);
gpg_err_code_t _gcry_fips_run_selftests (int extended);
void _gcry_fips_noreturn (void);
#define fips_noreturn() (_gcry_fips_noreturn ())
#endif /* G10LIB_H */