2007-12-04 12:23:31 +01:00
|
|
|
/* helpfile.c - GnuPG's helpfile feature
|
|
|
|
* Copyright (C) 2007 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2007-12-04 12:23:31 +01:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2007-12-04 12:23:31 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2007-12-04 12:23:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "i18n.h"
|
|
|
|
#include "membuf.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Try to find KEY in the file FNAME. */
|
|
|
|
static char *
|
|
|
|
findkey_fname (const char *key, const char *fname)
|
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
2020-10-20 11:52:16 +02:00
|
|
|
estream_t fp;
|
2007-12-04 12:23:31 +01:00
|
|
|
int lnr = 0;
|
|
|
|
int c;
|
|
|
|
char *p, line[256];
|
|
|
|
int in_item = 0;
|
|
|
|
membuf_t mb = MEMBUF_ZERO;
|
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
fp = es_fopen (fname, "r");
|
2007-12-04 12:23:31 +01:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("can't open '%s': %s\n"), fname, gpg_strerror (err));
|
2007-12-04 12:23:31 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
while (es_fgets (line, DIM(line)-1, fp))
|
2007-12-04 12:23:31 +01:00
|
|
|
{
|
|
|
|
lnr++;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-12-04 12:23:31 +01:00
|
|
|
if (!*line || line[strlen(line)-1] != '\n')
|
|
|
|
{
|
|
|
|
/* Eat until end of line. */
|
2020-10-20 11:52:16 +02:00
|
|
|
while ((c = es_getc (fp)) != EOF && c != '\n')
|
2007-12-04 12:23:31 +01:00
|
|
|
;
|
|
|
|
err = gpg_error (*line? GPG_ERR_LINE_TOO_LONG
|
|
|
|
: GPG_ERR_INCOMPLETE_LINE);
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("file '%s', line %d: %s\n"),
|
2007-12-04 12:23:31 +01:00
|
|
|
fname, lnr, gpg_strerror (err));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
line[strlen(line)-1] = 0; /* Chop the LF. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-12-04 12:23:31 +01:00
|
|
|
again:
|
|
|
|
if (!in_item)
|
|
|
|
{
|
|
|
|
/* Allow for empty lines and spaces while not in an item. */
|
|
|
|
for (p=line; spacep (p); p++)
|
|
|
|
;
|
|
|
|
if (!*p || *p == '#')
|
|
|
|
continue;
|
|
|
|
if (*line != '.' || spacep(line+1))
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_info (_("file '%s', line %d: %s\n"),
|
2007-12-04 12:23:31 +01:00
|
|
|
fname, lnr, _("ignoring garbage line"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
trim_trailing_spaces (line);
|
|
|
|
in_item = 1;
|
|
|
|
if (!strcmp (line+1, key))
|
|
|
|
{
|
|
|
|
/* Found. Start collecting. */
|
|
|
|
init_membuf (&mb, 1024);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If in an item only allow for comments in the first column
|
|
|
|
and provide ". " as an escape sequence to allow for
|
|
|
|
leading dots and hash marks in the actual text. */
|
|
|
|
if (*line == '#')
|
|
|
|
continue;
|
|
|
|
if (*line == '.')
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2007-12-04 12:23:31 +01:00
|
|
|
if (spacep(line+1))
|
|
|
|
p = line + 2;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trim_trailing_spaces (line);
|
|
|
|
in_item = 0;
|
|
|
|
if (is_membuf_ready (&mb))
|
|
|
|
break; /* Yep, found and collected the item. */
|
|
|
|
if (!line[1])
|
|
|
|
continue; /* Just an end of text dot. */
|
|
|
|
goto again; /* A new key line. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p = line;
|
|
|
|
|
|
|
|
if (is_membuf_ready (&mb))
|
|
|
|
{
|
|
|
|
put_membuf_str (&mb, p);
|
|
|
|
put_membuf (&mb, "\n", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-10-20 11:52:16 +02:00
|
|
|
if ( !err && es_ferror (fp) )
|
2007-12-04 12:23:31 +01:00
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("error reading '%s', line %d: %s\n"),
|
2007-12-04 12:23:31 +01:00
|
|
|
fname, lnr, gpg_strerror (err));
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fclose (fp);
|
2007-12-04 12:23:31 +01:00
|
|
|
if (is_membuf_ready (&mb))
|
|
|
|
{
|
|
|
|
/* We have collected something. */
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
xfree (get_membuf (&mb, NULL));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
put_membuf (&mb, "", 1); /* Terminate string. */
|
|
|
|
return get_membuf (&mb, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Try the help files depending on the locale. */
|
|
|
|
static char *
|
2007-12-04 16:00:14 +01:00
|
|
|
findkey_locale (const char *key, const char *locname,
|
|
|
|
int only_current_locale, const char *dirname)
|
2007-12-04 12:23:31 +01:00
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
char *fname, *ext, *p;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
fname = xtrymalloc (strlen (dirname) + 6 + strlen (locname) + 4 + 1);
|
|
|
|
if (!fname)
|
|
|
|
return NULL;
|
|
|
|
ext = stpcpy (stpcpy (fname, dirname), "/help.");
|
|
|
|
/* Search with locale name and territory. ("help.LL_TT.txt") */
|
|
|
|
if (strchr (locname, '_'))
|
|
|
|
{
|
|
|
|
strcpy (stpcpy (ext, locname), ".txt");
|
|
|
|
result = findkey_fname (key, fname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = NULL; /* No territory. */
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
/* Search with just the locale name - if any. ("help.LL.txt") */
|
|
|
|
if (*locname)
|
|
|
|
{
|
|
|
|
for (p=ext, s=locname; *s && *s != '_';)
|
|
|
|
*p++ = *s++;
|
|
|
|
strcpy (p, ".txt");
|
|
|
|
result = findkey_fname (key, fname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = NULL;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-12-04 16:00:14 +01:00
|
|
|
if (!result && (!only_current_locale || !*locname) )
|
2007-12-04 12:23:31 +01:00
|
|
|
{
|
2007-12-04 16:00:14 +01:00
|
|
|
/* Last try: Search in file without any locale info. ("help.txt") */
|
2007-12-04 12:23:31 +01:00
|
|
|
strcpy (ext, "txt");
|
|
|
|
result = findkey_fname (key, fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
xfree (fname);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Return a malloced help text as identified by KEY. The system takes
|
|
|
|
the string from an UTF-8 encoded file to be created by an
|
|
|
|
administrator or as distributed with GnuPG. On a GNU or Unix
|
|
|
|
system the entry is searched in these files:
|
|
|
|
|
|
|
|
/etc/gnupg/help.LL.txt
|
|
|
|
/etc/gnupg/help.txt
|
|
|
|
/usr/share/gnupg/help.LL.txt
|
|
|
|
/usr/share/gnupg/help.txt
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-12-04 12:23:31 +01:00
|
|
|
Here LL denotes the two digit language code of the current locale.
|
2015-11-16 12:41:46 +01:00
|
|
|
If ONLY_CURRENT_LOCALE is set, the function won't fallback to the
|
2007-12-04 16:00:14 +01:00
|
|
|
english valiant ("help.txt") unless that locale has been requested.
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-12-04 12:23:31 +01:00
|
|
|
The help file needs to be encoded in UTF-8, lines with a '#' in the
|
|
|
|
first column are comment lines and entirely ignored. Help keys are
|
|
|
|
identified by a key consisting of a single word with a single dot
|
|
|
|
as the first character. All key lines listed without any
|
|
|
|
intervening lines (except for comment lines) lead to the same help
|
|
|
|
text. Lines following the key lines make up the actual hep texts.
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-12-04 12:23:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
2007-12-04 16:00:14 +01:00
|
|
|
gnupg_get_help_string (const char *key, int only_current_locale)
|
2007-12-04 12:23:31 +01:00
|
|
|
{
|
|
|
|
static const char *locname;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
if (!locname)
|
|
|
|
{
|
|
|
|
char *buffer, *p;
|
|
|
|
int count = 0;
|
|
|
|
const char *s = gnupg_messages_locale_name ();
|
|
|
|
buffer = xtrystrdup (s);
|
|
|
|
if (!buffer)
|
|
|
|
locname = "";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (p = buffer; *p; p++)
|
|
|
|
if (*p == '.' || *p == '@' || *p == '/' /*(safeguard)*/)
|
|
|
|
*p = 0;
|
|
|
|
else if (*p == '_')
|
|
|
|
{
|
|
|
|
if (count++)
|
2007-12-04 16:00:14 +01:00
|
|
|
*p = 0; /* Also cut at a underscore in the territory. */
|
2007-12-04 12:23:31 +01:00
|
|
|
}
|
|
|
|
locname = buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key || !*key)
|
|
|
|
return NULL;
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
result = findkey_locale (key, locname, only_current_locale,
|
2007-12-04 16:00:14 +01:00
|
|
|
gnupg_sysconfdir ());
|
2007-12-04 12:23:31 +01:00
|
|
|
if (!result)
|
2007-12-04 16:00:14 +01:00
|
|
|
result = findkey_locale (key, locname, only_current_locale,
|
|
|
|
gnupg_datadir ());
|
2007-12-06 20:02:42 +01:00
|
|
|
|
|
|
|
if (result)
|
|
|
|
trim_trailing_spaces (result);
|
|
|
|
|
2007-12-04 12:23:31 +01:00
|
|
|
return result;
|
|
|
|
}
|