2005-09-15 09:58:31 -04:00
|
|
|
/* Config file manipulation */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h> /* OS/2 needs this after sys/types.h */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "config/conf.h"
|
|
|
|
#include "config/dialogs.h"
|
2007-08-30 18:03:14 -04:00
|
|
|
#include "config/domain.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "config/home.h"
|
|
|
|
#include "config/kbdbind.h"
|
|
|
|
#include "config/options.h"
|
|
|
|
#include "config/opttypes.h"
|
2021-08-08 15:25:08 -04:00
|
|
|
#include "intl/libintl.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "osdep/osdep.h"
|
|
|
|
#include "terminal/terminal.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/secsave.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Config file has only very simple grammar:
|
|
|
|
*
|
|
|
|
* /set *option *= *value/
|
|
|
|
* /bind *keymap *keystroke *= *action/
|
|
|
|
* /include *file/
|
|
|
|
* /#.*$/
|
|
|
|
*
|
|
|
|
* Where option consists from any number of categories separated by dots and
|
|
|
|
* name of the option itself. Both category and option name consists from
|
|
|
|
* [a-zA-Z0-9_-*] - using uppercase letters is not recommended, though. '*' is
|
|
|
|
* reserved and is used only as escape character in place of '.' originally in
|
|
|
|
* option name.
|
|
|
|
*
|
|
|
|
* Value can consist from:
|
|
|
|
* - number (it will be converted to int/long)
|
|
|
|
* - enum (like on, off; true, fake, last_url; etc ;) - in planning state yet
|
|
|
|
* - string - "blah blah" (keymap, keystroke and action and file looks like that too)
|
|
|
|
*
|
|
|
|
* "set" command is parsed first, and then type-specific function is called,
|
|
|
|
* with option as one parameter and value as a second. Usually it just assigns
|
|
|
|
* value to an option, but sometimes you may want to first create the option
|
|
|
|
* ;). Then this will come handy. */
|
|
|
|
|
2016-04-20 14:57:09 -04:00
|
|
|
struct conf_parsing_pos {
|
|
|
|
/** Points to the next character to be parsed from the
|
|
|
|
* configuration file. */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *look;
|
2016-04-20 14:57:09 -04:00
|
|
|
|
|
|
|
/** The line number corresponding to #look. This is
|
|
|
|
* shown in error messages. */
|
|
|
|
int line;
|
|
|
|
};
|
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
struct conf_parsing_state {
|
|
|
|
/** This part may be copied to a local variable as a bookmark
|
|
|
|
* and restored later. So it must not contain any pointers
|
|
|
|
* that would have to be freed in that situation. */
|
2016-04-20 14:57:09 -04:00
|
|
|
struct conf_parsing_pos pos;
|
2008-02-02 18:52:33 -05:00
|
|
|
|
|
|
|
/** When ELinks is rewriting the configuration file, @c mirrored
|
|
|
|
* indicates the end of the part that has already been copied
|
|
|
|
* to the mirror string. Otherwise, @c mirrored is not used.
|
|
|
|
*
|
|
|
|
* @invariant @c mirrored @<= @c pos.look */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *mirrored;
|
2008-02-02 20:15:57 -05:00
|
|
|
|
|
|
|
/** File name for error messages. If NULL then do not display
|
|
|
|
* error messages. */
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *filename;
|
2008-02-02 17:16:38 -05:00
|
|
|
};
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 20:15:57 -05:00
|
|
|
/** Tell the user about an error in the configuration file.
|
|
|
|
* @return @a err, for convenience. */
|
|
|
|
static enum parse_error
|
|
|
|
show_parse_error(const struct conf_parsing_state *state, enum parse_error err)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
static const char error_msg[][40] = {
|
2008-02-02 20:15:57 -05:00
|
|
|
"no error", /* ERROR_NONE */
|
|
|
|
"unknown command", /* ERROR_COMMAND */
|
|
|
|
"parse error", /* ERROR_PARSE */
|
|
|
|
"unknown option", /* ERROR_OPTION */
|
|
|
|
"bad value", /* ERROR_VALUE */
|
|
|
|
"no memory left", /* ERROR_NOMEM */
|
|
|
|
};
|
|
|
|
|
|
|
|
if (state->filename) {
|
|
|
|
fprintf(stderr, "%s:%d: %s\n",
|
|
|
|
state->filename, state->pos.line, error_msg[err]);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
/** Skip comments and whitespace. */
|
|
|
|
static void
|
|
|
|
skip_white(struct conf_parsing_pos *pos)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *start = pos->look;
|
2008-02-02 17:16:38 -05:00
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
while (*start) {
|
|
|
|
while (isspace(*start)) {
|
|
|
|
if (*start == '\n') {
|
2008-02-02 17:16:38 -05:00
|
|
|
pos->line++;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*start == '#') {
|
|
|
|
start += strcspn(start, "\n");
|
|
|
|
} else {
|
2008-02-02 17:16:38 -05:00
|
|
|
pos->look = start;
|
|
|
|
return;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
pos->look = start;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2008-02-02 21:05:03 -05:00
|
|
|
/** Skip a quoted string.
|
|
|
|
* This function allows "mismatching quotes' because str_rd() does so. */
|
|
|
|
static void
|
|
|
|
skip_quoted(struct conf_parsing_pos *pos)
|
|
|
|
{
|
|
|
|
assert(isquote(*pos->look));
|
|
|
|
if_assert_failed return;
|
|
|
|
pos->look++;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!*pos->look)
|
|
|
|
return;
|
|
|
|
if (isquote(*pos->look)) {
|
|
|
|
pos->look++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*pos->look == '\\' && pos->look[1])
|
|
|
|
pos->look++;
|
|
|
|
if (*pos->look == '\n')
|
|
|
|
pos->line++;
|
|
|
|
pos->look++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Skip the value of an option.
|
|
|
|
*
|
|
|
|
* This job is normally done by the reader function that corresponds
|
|
|
|
* to the type of the option. However, if ELinks does not recognize
|
|
|
|
* the name of the option, it cannot look up the type and has to use
|
|
|
|
* this function instead. */
|
|
|
|
static void
|
|
|
|
skip_option_value(struct conf_parsing_pos *pos)
|
|
|
|
{
|
|
|
|
if (isquote(*pos->look)) {
|
|
|
|
/* Looks like OPT_STRING, OPT_CODEPAGE, OPT_LANGUAGE,
|
|
|
|
* or OPT_COLOR. */
|
|
|
|
skip_quoted(pos);
|
|
|
|
} else {
|
|
|
|
/* Looks like OPT_BOOL, OPT_INT, or OPT_LONG. */
|
|
|
|
while (isasciialnum(*pos->look) || *pos->look == '.'
|
|
|
|
|| *pos->look == '+' || *pos->look == '-')
|
|
|
|
pos->look++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-02 21:38:24 -05:00
|
|
|
/** Skip to the next newline or comment that is not part of a quoted
|
|
|
|
* string. When ELinks hits a parse error in the configuration file,
|
|
|
|
* it calls this in order to find the place where is should resume
|
|
|
|
* parsing. This is intended to prevent ELinks from treating words
|
|
|
|
* in strings as commands. */
|
|
|
|
static void
|
|
|
|
skip_to_unquoted_newline_or_comment(struct conf_parsing_pos *pos)
|
|
|
|
{
|
|
|
|
while (*pos->look && *pos->look != '#' && *pos->look != '\n') {
|
|
|
|
if (isquote(*pos->look))
|
|
|
|
skip_quoted(pos);
|
|
|
|
else
|
|
|
|
pos->look++;
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse a command. Returns error code. */
|
|
|
|
/* If dynamic string credentials are supplied, we will mirror the command at
|
|
|
|
* the end of the string; however, we won't load the option value to the tree,
|
2008-02-03 06:07:43 -05:00
|
|
|
* and we will even write option value from the tree to the output string.
|
2008-02-03 07:36:48 -05:00
|
|
|
* We will only possibly set or clear OPT_MUST_SAVE flag in the option. */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
static enum parse_error
|
2008-02-03 15:22:00 -05:00
|
|
|
parse_set_common(struct option *opt_tree, struct conf_parsing_state *state,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *mirror, int is_system_conf, int want_domain)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *domain_orig = NULL;
|
2008-02-03 15:22:00 -05:00
|
|
|
size_t domain_len = 0;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *domain_copy = NULL;
|
|
|
|
const char *optname_orig;
|
2008-02-02 23:48:15 -05:00
|
|
|
size_t optname_len;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *optname_copy;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
skip_white(&state->pos);
|
2008-02-02 20:15:57 -05:00
|
|
|
if (!*state->pos.look) return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-08-30 16:29:40 -04:00
|
|
|
if (want_domain) {
|
2008-02-03 15:22:00 -05:00
|
|
|
domain_orig = state->pos.look;
|
|
|
|
while (isident(*state->pos.look) || *state->pos.look == '*'
|
|
|
|
|| *state->pos.look == '.' || *state->pos.look == '+')
|
|
|
|
state->pos.look++;
|
|
|
|
domain_len = state->pos.look - domain_orig;
|
2007-08-30 16:29:40 -04:00
|
|
|
|
2008-02-03 15:22:00 -05:00
|
|
|
skip_white(&state->pos);
|
2007-08-30 16:29:40 -04:00
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* Option name */
|
2008-02-02 23:48:15 -05:00
|
|
|
optname_orig = state->pos.look;
|
2009-08-19 18:01:05 -04:00
|
|
|
while (is_option_name_char(*state->pos.look)
|
|
|
|
|| *state->pos.look == '.')
|
2008-02-02 17:16:38 -05:00
|
|
|
state->pos.look++;
|
2008-02-02 23:48:15 -05:00
|
|
|
optname_len = state->pos.look - optname_orig;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
skip_white(&state->pos);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Equal sign */
|
2008-02-02 23:48:15 -05:00
|
|
|
if (*state->pos.look != '=')
|
2008-02-02 20:15:57 -05:00
|
|
|
return show_parse_error(state, ERROR_PARSE);
|
2008-02-02 17:16:38 -05:00
|
|
|
state->pos.look++; /* '=' */
|
|
|
|
skip_white(&state->pos);
|
2008-02-02 23:48:15 -05:00
|
|
|
if (!*state->pos.look)
|
2008-02-02 20:15:57 -05:00
|
|
|
return show_parse_error(state, ERROR_VALUE);
|
2008-02-02 23:48:15 -05:00
|
|
|
|
|
|
|
optname_copy = memacpy(optname_orig, optname_len);
|
|
|
|
if (!optname_copy) return show_parse_error(state, ERROR_NOMEM);
|
2008-02-03 15:22:00 -05:00
|
|
|
if (want_domain) {
|
|
|
|
domain_copy = memacpy(domain_orig, domain_len);
|
|
|
|
if (!domain_copy) {
|
|
|
|
mem_free(optname_copy);
|
|
|
|
return show_parse_error(state, ERROR_NOMEM);
|
|
|
|
}
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Option value */
|
|
|
|
{
|
|
|
|
struct option *opt;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *val;
|
2008-02-02 21:05:03 -05:00
|
|
|
const struct conf_parsing_pos pos_before_value = state->pos;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-03 15:22:00 -05:00
|
|
|
if (want_domain && *domain_copy) {
|
2007-08-30 16:29:40 -04:00
|
|
|
struct option *domain_tree;
|
2007-09-14 09:14:34 -04:00
|
|
|
|
2008-02-03 15:22:00 -05:00
|
|
|
domain_tree = get_domain_tree(domain_copy);
|
2007-08-30 16:29:40 -04:00
|
|
|
if (!domain_tree) {
|
2008-02-03 15:22:00 -05:00
|
|
|
mem_free(domain_copy);
|
|
|
|
mem_free(optname_copy);
|
|
|
|
skip_option_value(&state->pos);
|
|
|
|
return show_parse_error(state, ERROR_NOMEM);
|
2007-08-30 16:29:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mirror) {
|
2008-02-03 15:22:00 -05:00
|
|
|
opt = get_opt_rec_real(domain_tree,
|
|
|
|
optname_copy);
|
2007-08-30 16:29:40 -04:00
|
|
|
} else {
|
2008-02-03 15:22:00 -05:00
|
|
|
opt = get_opt_rec(opt_tree, optname_copy);
|
2007-08-30 16:29:40 -04:00
|
|
|
if (opt) {
|
|
|
|
opt = get_option_shadow(opt, opt_tree,
|
|
|
|
domain_tree);
|
|
|
|
if (!opt) {
|
2008-02-03 15:22:00 -05:00
|
|
|
mem_free(domain_copy);
|
|
|
|
mem_free(optname_copy);
|
|
|
|
skip_option_value(&state->pos);
|
|
|
|
return show_parse_error(state,
|
|
|
|
ERROR_NOMEM);
|
2007-08-30 16:29:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2008-02-03 15:22:00 -05:00
|
|
|
opt = mirror
|
|
|
|
? get_opt_rec_real(opt_tree, optname_copy)
|
|
|
|
: get_opt_rec(opt_tree, optname_copy);
|
2007-08-30 16:29:40 -04:00
|
|
|
}
|
|
|
|
if (want_domain)
|
2008-02-03 15:22:00 -05:00
|
|
|
mem_free(domain_copy);
|
|
|
|
domain_copy = NULL;
|
2008-02-02 23:48:15 -05:00
|
|
|
mem_free(optname_copy);
|
|
|
|
optname_copy = NULL;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 21:05:03 -05:00
|
|
|
if (!opt || (opt->flags & OPT_HIDDEN)) {
|
|
|
|
show_parse_error(state, ERROR_OPTION);
|
|
|
|
skip_option_value(&state->pos);
|
2005-09-15 09:58:31 -04:00
|
|
|
return ERROR_OPTION;
|
2008-02-03 06:07:43 -05:00
|
|
|
/* TODO: Distinguish between two scenarios:
|
|
|
|
* - A newer version of ELinks has saved an
|
|
|
|
* option that this version does not recognize.
|
|
|
|
* The option must be preserved. (This works.)
|
|
|
|
* - The user has added an option, saved
|
|
|
|
* elinks.conf, restarted ELinks, deleted the
|
|
|
|
* option, and is now saving elinks.conf again.
|
|
|
|
* The option should be rewritten to "unset".
|
|
|
|
* (This does not work yet.)
|
|
|
|
* In both cases, ELinks has no struct option
|
|
|
|
* for that name. Possible fixes:
|
|
|
|
* - If the tree has OPT_AUTOCREATE, then
|
|
|
|
* assume the user had created that option,
|
|
|
|
* and rewrite it to "unset". Otherwise,
|
|
|
|
* keep it.
|
|
|
|
* - When the user deletes an option, just mark
|
|
|
|
* it with OPT_DELETED, and keep it in memory
|
|
|
|
* as long as OPT_TOUCHED is set. */
|
2008-02-02 21:05:03 -05:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 21:05:03 -05:00
|
|
|
if (!option_types[opt->type].read) {
|
|
|
|
show_parse_error(state, ERROR_VALUE);
|
|
|
|
skip_option_value(&state->pos);
|
2005-09-15 09:58:31 -04:00
|
|
|
return ERROR_VALUE;
|
2008-02-02 21:05:03 -05:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
val = option_types[opt->type].read(opt, &state->pos.look,
|
|
|
|
&state->pos.line);
|
2008-02-02 21:05:03 -05:00
|
|
|
if (!val) {
|
|
|
|
/* The reader function failed. Jump back to
|
|
|
|
* the beginning of the value and skip it with
|
|
|
|
* the generic code. For the error message,
|
|
|
|
* use the line number at the beginning of the
|
|
|
|
* value, because the ending position is not
|
|
|
|
* interesting if there is an unclosed quote. */
|
|
|
|
state->pos = pos_before_value;
|
|
|
|
show_parse_error(state, ERROR_VALUE);
|
|
|
|
skip_option_value(&state->pos);
|
|
|
|
return ERROR_VALUE;
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-03 05:41:34 -05:00
|
|
|
if (!mirror) {
|
|
|
|
/* loading a configuration file */
|
|
|
|
if (!option_types[opt->type].set
|
|
|
|
|| !option_types[opt->type].set(opt, val)) {
|
|
|
|
mem_free(val);
|
|
|
|
return show_parse_error(state, ERROR_VALUE);
|
|
|
|
}
|
2008-02-03 07:36:48 -05:00
|
|
|
} else if (is_system_conf) {
|
|
|
|
/* scanning a file that will not be rewritten */
|
|
|
|
struct option *flagsite = indirect_option(opt);
|
|
|
|
|
|
|
|
if (!(flagsite->flags & OPT_DELETED)
|
|
|
|
&& option_types[opt->type].equals
|
|
|
|
&& option_types[opt->type].equals(opt, val))
|
|
|
|
flagsite->flags &= ~OPT_MUST_SAVE;
|
2005-09-15 09:58:31 -04:00
|
|
|
else
|
2008-02-03 07:36:48 -05:00
|
|
|
flagsite->flags |= OPT_MUST_SAVE;
|
2008-02-03 05:41:34 -05:00
|
|
|
} else {
|
|
|
|
/* rewriting a configuration file */
|
2008-02-03 06:57:13 -05:00
|
|
|
struct option *flagsite = indirect_option(opt);
|
|
|
|
|
|
|
|
if (flagsite->flags & OPT_DELETED) {
|
2008-02-03 06:07:43 -05:00
|
|
|
/* Replace the "set" command with an
|
|
|
|
* "unset" command. */
|
|
|
|
add_to_string(mirror, "unset ");
|
|
|
|
add_bytes_to_string(mirror, optname_orig,
|
|
|
|
optname_len);
|
|
|
|
state->mirrored = state->pos.look;
|
|
|
|
} else if (option_types[opt->type].write) {
|
2008-02-03 05:49:49 -05:00
|
|
|
add_bytes_to_string(mirror, state->mirrored,
|
|
|
|
pos_before_value.look
|
|
|
|
- state->mirrored);
|
2005-09-15 09:58:31 -04:00
|
|
|
option_types[opt->type].write(opt, mirror);
|
2008-02-02 18:52:33 -05:00
|
|
|
state->mirrored = state->pos.look;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
2008-02-03 06:07:43 -05:00
|
|
|
/* Remember that the option need not be
|
|
|
|
* written to the end of the file. */
|
2008-02-03 06:57:13 -05:00
|
|
|
flagsite->flags &= ~OPT_MUST_SAVE;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
mem_free(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
2007-08-30 16:29:40 -04:00
|
|
|
static enum parse_error
|
2008-02-03 15:22:00 -05:00
|
|
|
parse_set_domain(struct option *opt_tree, struct conf_parsing_state *state,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *mirror, int is_system_conf)
|
2007-08-30 16:29:40 -04:00
|
|
|
{
|
2008-02-03 15:22:00 -05:00
|
|
|
return parse_set_common(opt_tree, state, mirror, is_system_conf, 1);
|
2007-08-30 16:29:40 -04:00
|
|
|
}
|
|
|
|
|
2007-08-29 09:30:40 -04:00
|
|
|
static enum parse_error
|
2008-02-03 15:22:00 -05:00
|
|
|
parse_set(struct option *opt_tree, struct conf_parsing_state *state,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *mirror, int is_system_conf)
|
2007-08-29 09:30:40 -04:00
|
|
|
{
|
2008-02-03 15:22:00 -05:00
|
|
|
return parse_set_common(opt_tree, state, mirror, is_system_conf, 0);
|
2007-08-29 09:30:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
static enum parse_error
|
2008-02-02 17:16:38 -05:00
|
|
|
parse_unset(struct option *opt_tree, struct conf_parsing_state *state,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *mirror, int is_system_conf)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *optname_orig;
|
2008-02-02 23:48:15 -05:00
|
|
|
size_t optname_len;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *optname_copy;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
skip_white(&state->pos);
|
2008-02-02 20:15:57 -05:00
|
|
|
if (!*state->pos.look) return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Option name */
|
2008-02-02 23:48:15 -05:00
|
|
|
optname_orig = state->pos.look;
|
2009-08-19 18:01:05 -04:00
|
|
|
while (is_option_name_char(*state->pos.look)
|
|
|
|
|| *state->pos.look == '.')
|
2008-02-02 17:16:38 -05:00
|
|
|
state->pos.look++;
|
2008-02-02 23:48:15 -05:00
|
|
|
optname_len = state->pos.look - optname_orig;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 23:48:15 -05:00
|
|
|
optname_copy = memacpy(optname_orig, optname_len);
|
|
|
|
if (!optname_copy) return show_parse_error(state, ERROR_NOMEM);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
struct option *opt;
|
|
|
|
|
2008-02-02 23:48:15 -05:00
|
|
|
opt = get_opt_rec_real(opt_tree, optname_copy);
|
|
|
|
mem_free(optname_copy);
|
|
|
|
optname_copy = NULL;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-03 05:35:56 -05:00
|
|
|
if (!opt || (opt->flags & OPT_HIDDEN)) {
|
|
|
|
/* The user wanted to delete the option, and
|
|
|
|
* it has already been deleted; this is not an
|
|
|
|
* error. This might happen if a version of
|
|
|
|
* ELinks has a built-in URL rewriting rule,
|
|
|
|
* the user disables it, and a later version
|
|
|
|
* no longer has it. */
|
|
|
|
return ERROR_NONE;
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!mirror) {
|
2008-02-03 05:41:34 -05:00
|
|
|
/* loading a configuration file */
|
2005-09-15 09:58:31 -04:00
|
|
|
if (opt->flags & OPT_ALLOC) delete_option(opt);
|
2008-02-03 13:23:58 -05:00
|
|
|
else mark_option_as_deleted(opt);
|
2008-02-03 07:36:48 -05:00
|
|
|
} else if (is_system_conf) {
|
|
|
|
/* scanning a file that will not be rewritten */
|
|
|
|
struct option *flagsite = indirect_option(opt);
|
|
|
|
|
|
|
|
if (flagsite->flags & OPT_DELETED)
|
|
|
|
flagsite->flags &= ~OPT_MUST_SAVE;
|
2005-09-15 09:58:31 -04:00
|
|
|
else
|
2008-02-03 07:36:48 -05:00
|
|
|
flagsite->flags |= OPT_MUST_SAVE;
|
2005-09-15 09:58:31 -04:00
|
|
|
} else {
|
2008-02-03 05:41:34 -05:00
|
|
|
/* rewriting a configuration file */
|
2008-02-03 06:57:13 -05:00
|
|
|
struct option *flagsite = indirect_option(opt);
|
|
|
|
|
|
|
|
if (flagsite->flags & OPT_DELETED) {
|
2008-02-03 06:07:43 -05:00
|
|
|
/* The "unset" command is already in the file,
|
|
|
|
* and unlike with "set", there is no value
|
|
|
|
* to be updated. */
|
|
|
|
} else if (option_types[opt->type].write) {
|
|
|
|
/* Replace the "unset" command with a
|
|
|
|
* "set" command. */
|
|
|
|
add_to_string(mirror, "set ");
|
|
|
|
add_bytes_to_string(mirror, optname_orig,
|
|
|
|
optname_len);
|
|
|
|
add_to_string(mirror, " = ");
|
|
|
|
option_types[opt->type].write(opt, mirror);
|
|
|
|
state->mirrored = state->pos.look;
|
|
|
|
}
|
|
|
|
/* Remember that the option need not be
|
|
|
|
* written to the end of the file. */
|
2008-02-03 06:57:13 -05:00
|
|
|
flagsite->flags &= ~OPT_MUST_SAVE;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum parse_error
|
2008-02-02 17:16:38 -05:00
|
|
|
parse_bind(struct option *opt_tree, struct conf_parsing_state *state,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *mirror, int is_system_conf)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *keymap, *keystroke, *action;
|
2005-09-15 09:58:31 -04:00
|
|
|
enum parse_error err = ERROR_NONE;
|
2008-02-02 21:38:24 -05:00
|
|
|
struct conf_parsing_pos before_error;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
skip_white(&state->pos);
|
2008-02-02 20:15:57 -05:00
|
|
|
if (!*state->pos.look) return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Keymap */
|
2008-02-02 21:38:24 -05:00
|
|
|
before_error = state->pos;
|
2008-02-02 17:16:38 -05:00
|
|
|
keymap = option_types[OPT_STRING].read(NULL, &state->pos.look,
|
|
|
|
&state->pos.line);
|
|
|
|
skip_white(&state->pos);
|
2008-02-02 21:38:24 -05:00
|
|
|
if (!keymap || !*state->pos.look) {
|
|
|
|
state->pos = before_error;
|
|
|
|
return show_parse_error(state, ERROR_PARSE);
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Keystroke */
|
2008-02-02 21:38:24 -05:00
|
|
|
before_error = state->pos;
|
2008-02-02 17:16:38 -05:00
|
|
|
keystroke = option_types[OPT_STRING].read(NULL, &state->pos.look,
|
|
|
|
&state->pos.line);
|
|
|
|
skip_white(&state->pos);
|
|
|
|
if (!keystroke || !*state->pos.look) {
|
2008-02-02 21:17:21 -05:00
|
|
|
mem_free(keymap); mem_free_if(keystroke);
|
2008-02-02 21:38:24 -05:00
|
|
|
state->pos = before_error;
|
|
|
|
return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Equal sign */
|
2008-02-02 17:16:38 -05:00
|
|
|
skip_white(&state->pos);
|
|
|
|
if (*state->pos.look != '=') {
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(keymap); mem_free(keystroke);
|
2008-02-02 20:15:57 -05:00
|
|
|
return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
2008-02-02 17:16:38 -05:00
|
|
|
state->pos.look++; /* '=' */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
skip_white(&state->pos);
|
|
|
|
if (!*state->pos.look) {
|
2005-09-15 09:58:31 -04:00
|
|
|
mem_free(keymap); mem_free(keystroke);
|
2008-02-02 20:15:57 -05:00
|
|
|
return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Action */
|
2008-02-02 21:38:24 -05:00
|
|
|
before_error = state->pos;
|
2008-02-02 17:16:38 -05:00
|
|
|
action = option_types[OPT_STRING].read(NULL, &state->pos.look,
|
|
|
|
&state->pos.line);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!action) {
|
2008-02-02 14:05:49 -05:00
|
|
|
mem_free(keymap); mem_free(keystroke);
|
2008-02-02 21:38:24 -05:00
|
|
|
state->pos = before_error;
|
|
|
|
return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2008-02-03 05:41:34 -05:00
|
|
|
if (!mirror) {
|
|
|
|
/* loading a configuration file */
|
2005-09-15 09:58:31 -04:00
|
|
|
/* We don't bother to bind() if -default-keys. */
|
|
|
|
if (!get_cmd_opt_bool("default-keys")
|
|
|
|
&& bind_do(keymap, keystroke, action, is_system_conf)) {
|
|
|
|
/* bind_do() tried but failed. */
|
2008-02-03 05:41:34 -05:00
|
|
|
err = show_parse_error(state, ERROR_VALUE);
|
2005-09-15 09:58:31 -04:00
|
|
|
} else {
|
|
|
|
err = ERROR_NONE;
|
|
|
|
}
|
2008-02-03 07:36:48 -05:00
|
|
|
} else if (is_system_conf) {
|
|
|
|
/* scanning a file that will not be rewritten */
|
|
|
|
/* TODO */
|
2008-02-03 05:41:34 -05:00
|
|
|
} else {
|
|
|
|
/* rewriting a configuration file */
|
|
|
|
/* Mirror what we already have. If the keystroke has
|
|
|
|
* been unbound, then act_str is simply "none" and
|
|
|
|
* this does not require special handling. */
|
2021-01-02 10:20:27 -05:00
|
|
|
char *act_str = bind_act(keymap, keystroke);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (act_str) {
|
2008-02-02 18:52:33 -05:00
|
|
|
add_bytes_to_string(mirror, state->mirrored,
|
2008-02-02 21:38:24 -05:00
|
|
|
before_error.look - state->mirrored);
|
2005-09-15 09:58:31 -04:00
|
|
|
add_to_string(mirror, act_str);
|
|
|
|
mem_free(act_str);
|
2008-02-02 18:52:33 -05:00
|
|
|
state->mirrored = state->pos.look;
|
2005-09-15 09:58:31 -04:00
|
|
|
} else {
|
2008-02-02 20:15:57 -05:00
|
|
|
err = show_parse_error(state, ERROR_VALUE);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mem_free(keymap); mem_free(keystroke); mem_free(action);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static int load_config_file(char *, char *, struct option *,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *, int);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
static enum parse_error
|
2008-02-02 17:16:38 -05:00
|
|
|
parse_include(struct option *opt_tree, struct conf_parsing_state *state,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *mirror, int is_system_conf)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *fname;
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string dumbstring;
|
2008-02-02 21:38:24 -05:00
|
|
|
struct conf_parsing_pos before_error;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 20:15:57 -05:00
|
|
|
if (!init_string(&dumbstring))
|
|
|
|
return show_parse_error(state, ERROR_NOMEM);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
skip_white(&state->pos);
|
|
|
|
if (!*state->pos.look) {
|
2008-02-02 14:05:49 -05:00
|
|
|
done_string(&dumbstring);
|
2008-02-02 20:15:57 -05:00
|
|
|
return show_parse_error(state, ERROR_PARSE);
|
2008-02-02 14:05:49 -05:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* File name */
|
2008-02-02 21:38:24 -05:00
|
|
|
before_error = state->pos;
|
2008-02-02 17:16:38 -05:00
|
|
|
fname = option_types[OPT_STRING].read(NULL, &state->pos.look,
|
|
|
|
&state->pos.line);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!fname) {
|
|
|
|
done_string(&dumbstring);
|
2008-02-02 21:38:24 -05:00
|
|
|
state->pos = before_error;
|
|
|
|
return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We want load_config_file() to watermark stuff, but not to load
|
|
|
|
* anything, polluting our beloved options tree - thus, we will feed it
|
|
|
|
* with some dummy string which we will destroy later; still better
|
|
|
|
* than cloning whole options tree or polluting interface with another
|
|
|
|
* rarely-used option ;). */
|
|
|
|
/* XXX: We should try CONFDIR/<file> when proceeding
|
|
|
|
* CONFDIR/<otherfile> ;). --pasky */
|
2021-01-02 10:20:27 -05:00
|
|
|
if (load_config_file(fname[0] == '/' ? (char *) ""
|
2005-09-15 09:58:31 -04:00
|
|
|
: elinks_home,
|
2008-02-03 07:36:48 -05:00
|
|
|
fname, opt_tree,
|
|
|
|
mirror ? &dumbstring : NULL, 1)) {
|
2005-09-15 09:58:31 -04:00
|
|
|
done_string(&dumbstring);
|
|
|
|
mem_free(fname);
|
2008-02-02 20:15:57 -05:00
|
|
|
return show_parse_error(state, ERROR_VALUE);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
done_string(&dumbstring);
|
|
|
|
mem_free(fname);
|
|
|
|
return ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct parse_handler {
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *command;
|
2005-09-15 09:58:31 -04:00
|
|
|
enum parse_error (*handler)(struct option *opt_tree,
|
2008-02-02 17:16:38 -05:00
|
|
|
struct conf_parsing_state *state,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *mirror, int is_system_conf);
|
2005-09-15 09:58:31 -04:00
|
|
|
};
|
|
|
|
|
2008-01-26 17:22:44 -05:00
|
|
|
static const struct parse_handler parse_handlers[] = {
|
2007-08-30 16:29:40 -04:00
|
|
|
{ "set_domain", parse_set_domain },
|
2005-09-15 09:58:31 -04:00
|
|
|
{ "set", parse_set },
|
|
|
|
{ "unset", parse_unset },
|
|
|
|
{ "bind", parse_bind },
|
|
|
|
{ "include", parse_include },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-02-02 17:20:07 -05:00
|
|
|
static enum parse_error
|
2008-02-02 17:16:38 -05:00
|
|
|
parse_config_command(struct option *options, struct conf_parsing_state *state,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *mirror, int is_system_conf)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2008-01-26 17:22:44 -05:00
|
|
|
const struct parse_handler *handler;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 18:52:33 -05:00
|
|
|
/* If we're mirroring, then everything up to this point must
|
|
|
|
* have already been mirrored. */
|
|
|
|
assert(mirror == NULL || state->mirrored == state->pos.look);
|
2008-02-02 20:15:57 -05:00
|
|
|
if_assert_failed return show_parse_error(state, ERROR_PARSE);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
for (handler = parse_handlers; handler->command;
|
|
|
|
handler++) {
|
|
|
|
int cmdlen = strlen(handler->command);
|
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
if (!strncmp(state->pos.look, handler->command, cmdlen)
|
|
|
|
&& isspace(state->pos.look[cmdlen])) {
|
2005-09-15 09:58:31 -04:00
|
|
|
enum parse_error err;
|
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
state->pos.look += cmdlen;
|
2008-02-02 18:52:33 -05:00
|
|
|
err = handler->handler(options, state, mirror,
|
2005-09-15 09:58:31 -04:00
|
|
|
is_system_conf);
|
2008-02-02 18:52:33 -05:00
|
|
|
if (mirror) {
|
|
|
|
/* Mirror any characters that the handler
|
|
|
|
* consumed but did not already mirror. */
|
|
|
|
add_bytes_to_string(mirror, state->mirrored,
|
|
|
|
state->pos.look - state->mirrored);
|
|
|
|
state->mirrored = state->pos.look;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-02 20:15:57 -05:00
|
|
|
return show_parse_error(state, ERROR_COMMAND);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2008-02-02 17:20:07 -05:00
|
|
|
#ifdef CONFIG_EXMODE
|
|
|
|
enum parse_error
|
2021-01-02 10:20:27 -05:00
|
|
|
parse_config_exmode_command(char *cmd)
|
2008-02-02 17:20:07 -05:00
|
|
|
{
|
2008-02-02 17:16:38 -05:00
|
|
|
struct conf_parsing_state state = {{ 0 }};
|
|
|
|
|
|
|
|
state.pos.look = cmd;
|
|
|
|
state.pos.line = 0;
|
2008-02-02 18:52:33 -05:00
|
|
|
state.mirrored = NULL; /* not read because mirror is NULL too */
|
2008-02-02 20:15:57 -05:00
|
|
|
state.filename = NULL; /* prevent error messages */
|
2008-02-02 17:20:07 -05:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
return parse_config_command(config_options, &state, NULL, 0);
|
2008-02-02 17:20:07 -05:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_EXMODE */
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
2021-01-02 10:20:27 -05:00
|
|
|
parse_config_file(struct option *options, char *name,
|
|
|
|
char *file, struct string *mirror,
|
2005-09-15 09:58:31 -04:00
|
|
|
int is_system_conf)
|
|
|
|
{
|
2008-02-02 17:16:38 -05:00
|
|
|
struct conf_parsing_state state = {{ 0 }};
|
2008-01-26 23:09:18 -05:00
|
|
|
int error_occurred = 0;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
state.pos.look = file;
|
|
|
|
state.pos.line = 1;
|
2008-02-02 18:52:33 -05:00
|
|
|
state.mirrored = file;
|
2008-02-02 20:15:57 -05:00
|
|
|
if (!mirror && get_cmd_opt_int("verbose") >= VERBOSE_WARNINGS)
|
|
|
|
state.filename = name;
|
2008-02-02 17:16:38 -05:00
|
|
|
|
|
|
|
while (state.pos.look && *state.pos.look) {
|
2008-02-02 20:15:57 -05:00
|
|
|
enum parse_error err = ERROR_NONE;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Skip all possible comments and whitespace. */
|
2008-02-02 17:16:38 -05:00
|
|
|
skip_white(&state.pos);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Mirror what we already have */
|
2008-02-02 18:52:33 -05:00
|
|
|
if (mirror) {
|
|
|
|
add_bytes_to_string(mirror, state.mirrored,
|
|
|
|
state.pos.look - state.mirrored);
|
|
|
|
state.mirrored = state.pos.look;
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Second chance to escape from the hell. */
|
2008-02-02 17:16:38 -05:00
|
|
|
if (!*state.pos.look) break;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 17:16:38 -05:00
|
|
|
err = parse_config_command(options, &state, mirror,
|
2005-09-15 09:58:31 -04:00
|
|
|
is_system_conf);
|
2008-02-02 21:38:24 -05:00
|
|
|
switch (err) {
|
|
|
|
case ERROR_NONE:
|
|
|
|
break;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-02-02 21:38:24 -05:00
|
|
|
case ERROR_COMMAND:
|
|
|
|
case ERROR_PARSE:
|
2005-09-15 09:58:31 -04:00
|
|
|
/* Jump over this crap we can't understand. */
|
2008-02-02 21:38:24 -05:00
|
|
|
skip_to_unquoted_newline_or_comment(&state.pos);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* Mirror what we already have */
|
2008-02-02 18:52:33 -05:00
|
|
|
if (mirror) {
|
|
|
|
add_bytes_to_string(mirror, state.mirrored,
|
|
|
|
state.pos.look - state.mirrored);
|
|
|
|
state.mirrored = state.pos.look;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2008-02-02 21:38:24 -05:00
|
|
|
/* fall through */
|
|
|
|
default:
|
2008-02-02 20:15:57 -05:00
|
|
|
error_occurred = 1;
|
2008-02-02 21:38:24 -05:00
|
|
|
break;
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-02 20:15:57 -05:00
|
|
|
if (!error_occurred || !state.filename) return;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* If an error occurred make sure that the user is notified and is able
|
|
|
|
* to see it. First sound the bell. Then, if the text viewer is going to
|
|
|
|
* be started, sleep for a while so the message will be visible before
|
|
|
|
* ELinks starts drawing to on the screen and overwriting any error
|
|
|
|
* messages. This should not be necessary for terminals supporting the
|
|
|
|
* alternate screen mode but better to be safe. (debian bug 305017) */
|
|
|
|
|
|
|
|
fputc('\a', stderr);
|
|
|
|
|
|
|
|
if (get_cmd_opt_bool("dump")
|
|
|
|
|| get_cmd_opt_bool("source"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static char *
|
|
|
|
read_config_file(char *name)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
#define FILE_BUF 1024
|
2021-01-02 10:20:27 -05:00
|
|
|
char cfg_buffer[FILE_BUF];
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string string;
|
2005-09-15 09:58:31 -04:00
|
|
|
int fd;
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
fd = open(name, O_RDONLY | O_NOCTTY);
|
|
|
|
if (fd < 0) return NULL;
|
|
|
|
set_bin(fd);
|
|
|
|
|
|
|
|
if (!init_string(&string)) return NULL;
|
|
|
|
|
|
|
|
while ((r = safe_read(fd, cfg_buffer, FILE_BUF)) > 0) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Clear problems ;). */
|
|
|
|
for (i = 0; i < r; i++)
|
|
|
|
if (!cfg_buffer[i])
|
|
|
|
cfg_buffer[i] = ' ';
|
|
|
|
|
|
|
|
add_bytes_to_string(&string, cfg_buffer, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0) done_string(&string);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return string.source;
|
|
|
|
#undef FILE_BUF
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return 0 on success. */
|
|
|
|
static int
|
2021-01-02 10:20:27 -05:00
|
|
|
load_config_file(char *prefix, char *name,
|
2019-04-21 06:27:40 -04:00
|
|
|
struct option *options, struct string *mirror,
|
2005-09-15 09:58:31 -04:00
|
|
|
int is_system_conf)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *config_str, *config_file;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-03-11 06:59:11 -04:00
|
|
|
config_file = straconcat(prefix, STRING_DIR_SEP, name,
|
2021-01-02 10:20:27 -05:00
|
|
|
(char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!config_file) return 1;
|
|
|
|
|
|
|
|
config_str = read_config_file(config_file);
|
|
|
|
if (!config_str) {
|
|
|
|
mem_free(config_file);
|
2007-03-11 06:59:11 -04:00
|
|
|
config_file = straconcat(prefix, STRING_DIR_SEP, ".", name,
|
2021-01-02 10:20:27 -05:00
|
|
|
(char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!config_file) return 2;
|
|
|
|
|
|
|
|
config_str = read_config_file(config_file);
|
|
|
|
if (!config_str) {
|
|
|
|
mem_free(config_file);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_config_file(options, config_file, config_str, mirror,
|
|
|
|
is_system_conf);
|
|
|
|
|
|
|
|
mem_free(config_str);
|
|
|
|
mem_free(config_file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
load_config_from(char *file, struct option *tree)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
load_config_file(CONFDIR, file, tree, NULL, 1);
|
|
|
|
load_config_file(empty_string_or_(elinks_home), file, tree, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
load_config(void)
|
|
|
|
{
|
|
|
|
load_config_from(get_cmd_opt_str("config-file"),
|
|
|
|
config_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int indentation = 2;
|
|
|
|
/* 0 -> none, 1 -> only option full name+type, 2 -> only desc, 3 -> both */
|
|
|
|
static int comments = 3;
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static inline char *
|
|
|
|
conf_i18n(char *s, int i18n)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
if (i18n) return gettext(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-04-21 06:27:40 -04:00
|
|
|
add_indent_to_string(struct string *string, int depth)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
if (!depth) return;
|
|
|
|
|
|
|
|
add_xchar_to_string(string, ' ', depth * indentation);
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
2021-01-02 10:20:27 -05:00
|
|
|
wrap_option_desc(struct string *out, const char *src,
|
2019-04-21 06:27:40 -04:00
|
|
|
const struct string *indent, int maxwidth)
|
2009-03-08 06:30:09 -04:00
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
const char *last_space = NULL;
|
|
|
|
const char *uncopied = src;
|
2009-03-08 06:30:09 -04:00
|
|
|
int width = 0;
|
|
|
|
|
|
|
|
/* TODO: multibyte or fullwidth characters */
|
|
|
|
for (; *src; src++, width++) {
|
|
|
|
if (*src == '\n') {
|
|
|
|
last_space = src;
|
|
|
|
goto split;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*src == ' ') last_space = src;
|
|
|
|
|
|
|
|
if (width >= maxwidth && last_space) {
|
|
|
|
split:
|
|
|
|
if (!add_string_to_string(out, indent))
|
|
|
|
return NULL;
|
|
|
|
if (!add_bytes_to_string(out, uncopied,
|
|
|
|
last_space - uncopied))
|
|
|
|
return NULL;
|
|
|
|
if (!add_char_to_string(out, '\n'))
|
|
|
|
return NULL;
|
|
|
|
uncopied = last_space + 1;
|
|
|
|
width = src - uncopied;
|
|
|
|
last_space = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*uncopied) {
|
|
|
|
if (!add_string_to_string(out, indent))
|
|
|
|
return NULL;
|
|
|
|
if (!add_to_string(out, uncopied))
|
|
|
|
return NULL;
|
|
|
|
if (!add_char_to_string(out, '\n'))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-04-21 06:27:40 -04:00
|
|
|
output_option_desc_as_comment(struct string *out, const struct option *option,
|
2009-03-08 06:30:09 -04:00
|
|
|
int i18n, int depth)
|
|
|
|
{
|
2021-01-02 10:20:27 -05:00
|
|
|
char *desc_i18n = conf_i18n(option->desc, i18n);
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string indent;
|
2009-03-08 06:30:09 -04:00
|
|
|
|
|
|
|
if (!init_string(&indent)) return;
|
|
|
|
|
|
|
|
add_indent_to_string(&indent, depth);
|
|
|
|
if (!add_to_string(&indent, "# ")) goto out_of_memory;
|
|
|
|
if (!wrap_option_desc(out, desc_i18n, &indent, 80 - indent.length))
|
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
out_of_memory:
|
|
|
|
done_string(&indent);
|
|
|
|
}
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
static char *smart_config_output_fn_domain;
|
2007-08-30 16:29:40 -04:00
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
static void
|
2019-04-21 06:27:40 -04:00
|
|
|
smart_config_output_fn(struct string *string, struct option *option,
|
2021-01-02 10:20:27 -05:00
|
|
|
char *path, int depth, int do_print_comment,
|
2005-09-15 09:58:31 -04:00
|
|
|
int action, int i18n)
|
|
|
|
{
|
|
|
|
if (option->type == OPT_ALIAS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case 0:
|
|
|
|
if (!(comments & 1)) break;
|
|
|
|
|
|
|
|
add_indent_to_string(string, depth);
|
|
|
|
|
|
|
|
add_to_string(string, "## ");
|
|
|
|
if (path) {
|
|
|
|
add_to_string(string, path);
|
|
|
|
add_char_to_string(string, '.');
|
|
|
|
}
|
|
|
|
add_to_string(string, option->name);
|
|
|
|
add_char_to_string(string, ' ');
|
|
|
|
add_to_string(string, option_types[option->type].help_str);
|
|
|
|
add_char_to_string(string, '\n');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
if (!(comments & 2)) break;
|
|
|
|
|
|
|
|
if (!option->desc || !do_print_comment)
|
|
|
|
break;
|
|
|
|
|
2009-03-08 06:30:09 -04:00
|
|
|
output_option_desc_as_comment(string, option,
|
|
|
|
i18n, depth);
|
2005-09-15 09:58:31 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
|
|
add_indent_to_string(string, depth);
|
|
|
|
if (option->flags & OPT_DELETED) {
|
|
|
|
add_to_string(string, "un");
|
|
|
|
}
|
2007-08-30 16:29:40 -04:00
|
|
|
if (smart_config_output_fn_domain) {
|
|
|
|
add_to_string(string, "set_domain ");
|
|
|
|
add_to_string(string, smart_config_output_fn_domain);
|
|
|
|
add_char_to_string(string, ' ');
|
|
|
|
} else {
|
|
|
|
add_to_string(string, "set ");
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
if (path) {
|
|
|
|
add_to_string(string, path);
|
|
|
|
add_char_to_string(string, '.');
|
|
|
|
}
|
|
|
|
add_to_string(string, option->name);
|
|
|
|
if (!(option->flags & OPT_DELETED)) {
|
|
|
|
add_to_string(string, " = ");
|
|
|
|
/* OPT_ALIAS won't ever. OPT_TREE won't reach action 2.
|
|
|
|
* OPT_SPECIAL makes no sense in the configuration
|
|
|
|
* context. */
|
|
|
|
assert(option_types[option->type].write);
|
|
|
|
option_types[option->type].write(option, string);
|
|
|
|
}
|
|
|
|
add_char_to_string(string, '\n');
|
|
|
|
if (do_print_comment) add_char_to_string(string, '\n');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if (do_print_comment < 2)
|
|
|
|
add_char_to_string(string, '\n');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 12:14:25 -05:00
|
|
|
static void
|
|
|
|
smart_config_output_fn_html(struct string *string, struct option *option,
|
|
|
|
char *path, int depth, int do_print_comment,
|
|
|
|
int action, int i18n)
|
|
|
|
{
|
|
|
|
static unsigned int counter;
|
|
|
|
|
|
|
|
if (option->type == OPT_ALIAS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (option->flags & OPT_DELETED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smart_config_output_fn_domain) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
counter++;
|
|
|
|
add_to_string(string, "<tr><td>");
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
add_to_string(string, path);
|
|
|
|
add_char_to_string(string, '.');
|
|
|
|
}
|
|
|
|
add_to_string(string, option->name);
|
|
|
|
add_to_string(string, "</td><td>");
|
|
|
|
|
|
|
|
add_format_to_string(string, "<form name=\"el%d\" action=\"elinkscgi://config\" method=\"GET\">", counter);
|
|
|
|
add_to_string(string, "<input type=\"hidden\" name=\"option\" value=\"");
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
add_to_string(string, path);
|
|
|
|
add_char_to_string(string, '.');
|
|
|
|
}
|
|
|
|
add_to_string(string, option->name);
|
|
|
|
add_to_string(string, "\"/><input type=\"text\" name=\"val\" value=\"");
|
|
|
|
|
|
|
|
assert(option_types[option->type].write);
|
|
|
|
{
|
|
|
|
struct string tmp;
|
|
|
|
|
|
|
|
init_string(&tmp);
|
|
|
|
option_types[option->type].write(option, &tmp);
|
|
|
|
|
|
|
|
if (tmp.length >= 2 && tmp.source[0] == '"' && tmp.source[tmp.length - 1] == '"') {
|
|
|
|
add_bytes_to_string(string, tmp.source + 1, tmp.length - 2);
|
|
|
|
} else {
|
|
|
|
add_string_to_string(string, &tmp);
|
|
|
|
}
|
|
|
|
done_string(&tmp);
|
|
|
|
}
|
|
|
|
add_to_string(string, "\"/><input type=\"submit\" name=\"set\" value=\"Set\"/>"
|
|
|
|
"<input type=\"submit\" name=\"save\" value=\"Save\"/></form></td></tr>\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
static void
|
2021-01-02 10:20:27 -05:00
|
|
|
add_cfg_header_to_string(struct string *string, char *text)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int n = strlen(text) + 2;
|
|
|
|
|
|
|
|
int_bounds(&n, 10, 80);
|
|
|
|
|
|
|
|
add_to_string(string, "\n\n\n");
|
|
|
|
add_xchar_to_string(string, '#', n);
|
|
|
|
add_to_string(string, "\n# ");
|
|
|
|
add_to_string(string, text);
|
|
|
|
add_to_string(string, "#\n\n");
|
|
|
|
}
|
|
|
|
|
2021-12-29 12:14:25 -05:00
|
|
|
char *
|
|
|
|
create_about_config_string(void)
|
|
|
|
{
|
|
|
|
struct option *options = config_options;
|
|
|
|
struct string config;
|
|
|
|
/* Don't write headers if nothing will be added anyway. */
|
|
|
|
struct string tmpstring;
|
|
|
|
int origlen;
|
|
|
|
|
|
|
|
if (!init_string(&config)) return NULL;
|
|
|
|
|
|
|
|
{
|
|
|
|
int set_all = 1;
|
|
|
|
struct domain_tree *domain;
|
|
|
|
|
|
|
|
prepare_mustsave_flags(options->value.tree, set_all);
|
|
|
|
foreach (domain, domain_trees) {
|
|
|
|
prepare_mustsave_flags(domain->tree->value.tree,
|
|
|
|
set_all);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scaring. */
|
|
|
|
if (!init_string(&tmpstring)) goto get_me_out;
|
|
|
|
|
|
|
|
add_to_string(&tmpstring, "<table border=\"1\"><tr><th>Option name</th><th>Value</th></tr>\n");
|
|
|
|
|
|
|
|
origlen = tmpstring.length;
|
|
|
|
smart_config_string(&tmpstring, 2, 0, options->value.tree, NULL, 0,
|
|
|
|
smart_config_output_fn_html);
|
|
|
|
|
|
|
|
{
|
|
|
|
struct domain_tree *domain;
|
|
|
|
|
|
|
|
foreach (domain, domain_trees) {
|
|
|
|
smart_config_output_fn_domain = domain->name;
|
|
|
|
smart_config_string(&tmpstring, 2, 0,
|
|
|
|
domain->tree->value.tree,
|
|
|
|
NULL, 0,
|
|
|
|
smart_config_output_fn_html);
|
|
|
|
}
|
|
|
|
|
|
|
|
smart_config_output_fn_domain = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_to_string(&tmpstring, "</table>");
|
|
|
|
|
|
|
|
if (tmpstring.length > origlen)
|
|
|
|
add_string_to_string(&config, &tmpstring);
|
|
|
|
done_string(&tmpstring);
|
|
|
|
|
|
|
|
get_me_out:
|
|
|
|
return config.source;
|
|
|
|
}
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
char *
|
|
|
|
create_config_string(char *prefix, char *name)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2007-08-29 09:43:15 -04:00
|
|
|
struct option *options = config_options;
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string config;
|
2005-09-15 09:58:31 -04:00
|
|
|
/* Don't write headers if nothing will be added anyway. */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string tmpstring;
|
2005-09-15 09:58:31 -04:00
|
|
|
int origlen;
|
2007-08-28 12:41:18 -04:00
|
|
|
int savestyle = get_opt_int("config.saving_style", NULL);
|
|
|
|
int i18n = get_opt_bool("config.i18n", NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!init_string(&config)) return NULL;
|
|
|
|
|
2008-02-03 15:22:00 -05:00
|
|
|
{
|
|
|
|
int set_all = (savestyle == 1 || savestyle == 2);
|
|
|
|
struct domain_tree *domain;
|
|
|
|
|
|
|
|
prepare_mustsave_flags(options->value.tree, set_all);
|
|
|
|
foreach (domain, domain_trees) {
|
|
|
|
prepare_mustsave_flags(domain->tree->value.tree,
|
|
|
|
set_all);
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Scaring. */
|
|
|
|
if (savestyle == 2
|
2008-02-03 06:07:43 -05:00
|
|
|
|| load_config_file(prefix, name, options, &config, 0)
|
|
|
|
|| !config.length) {
|
2005-09-15 09:58:31 -04:00
|
|
|
/* At first line, and in English, write ELinks version, may be
|
|
|
|
* of some help in future. Please keep that format for it.
|
|
|
|
* --Zas */
|
|
|
|
add_to_string(&config, "## ELinks " VERSION " configuration file\n\n");
|
2008-02-03 06:07:43 -05:00
|
|
|
assert(savestyle >= 0 && savestyle <= 3);
|
2005-09-15 09:58:31 -04:00
|
|
|
switch (savestyle) {
|
|
|
|
case 0:
|
|
|
|
add_to_string(&config, conf_i18n(N_(
|
|
|
|
"## This is ELinks configuration file. You can edit it manually,\n"
|
|
|
|
"## if you wish so; this file is edited by ELinks when you save\n"
|
|
|
|
"## options through UI, however only option values will be altered\n"
|
|
|
|
"## and all your formatting, own comments etc will be kept as-is.\n"),
|
|
|
|
i18n));
|
|
|
|
break;
|
2008-02-03 06:07:43 -05:00
|
|
|
case 1: case 3:
|
2005-09-15 09:58:31 -04:00
|
|
|
add_to_string(&config, conf_i18n(N_(
|
|
|
|
"## This is ELinks configuration file. You can edit it manually,\n"
|
|
|
|
"## if you wish so; this file is edited by ELinks when you save\n"
|
|
|
|
"## options through UI, however only option values will be altered\n"
|
|
|
|
"## and missing options will be added at the end of file; if option\n"
|
|
|
|
"## is not written in this file, but in some file included from it,\n"
|
|
|
|
"## it is NOT counted as missing. Note that all your formatting,\n"
|
|
|
|
"## own comments and so on will be kept as-is.\n"), i18n));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
add_to_string(&config, conf_i18n(N_(
|
|
|
|
"## This is ELinks configuration file. You can edit it manually,\n"
|
|
|
|
"## if you wish so, but keep in mind that this file is overwritten\n"
|
|
|
|
"## by ELinks when you save options through UI and you are out of\n"
|
|
|
|
"## luck with your formatting and own comments then, so beware.\n"),
|
|
|
|
i18n));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_to_string(&config, "##\n");
|
|
|
|
|
|
|
|
add_to_string(&config, conf_i18n(N_(
|
|
|
|
"## Obviously, if you don't like what ELinks is going to do with\n"
|
|
|
|
"## this file, you can change it by altering the config.saving_style\n"
|
|
|
|
"## option. Come on, aren't we friendly guys after all?\n"), i18n));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (savestyle == 0) goto get_me_out;
|
|
|
|
|
2007-08-28 12:41:18 -04:00
|
|
|
indentation = get_opt_int("config.indentation", NULL);
|
|
|
|
comments = get_opt_int("config.comments", NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!init_string(&tmpstring)) goto get_me_out;
|
|
|
|
|
|
|
|
add_cfg_header_to_string(&tmpstring,
|
|
|
|
conf_i18n(N_("Automatically saved options\n"), i18n));
|
|
|
|
|
|
|
|
origlen = tmpstring.length;
|
|
|
|
smart_config_string(&tmpstring, 2, i18n, options->value.tree, NULL, 0,
|
|
|
|
smart_config_output_fn);
|
2007-08-30 16:29:40 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
struct domain_tree *domain;
|
|
|
|
|
|
|
|
foreach (domain, domain_trees) {
|
|
|
|
smart_config_output_fn_domain = domain->name;
|
|
|
|
smart_config_string(&tmpstring, 2, i18n,
|
|
|
|
domain->tree->value.tree,
|
|
|
|
NULL, 0,
|
|
|
|
smart_config_output_fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
smart_config_output_fn_domain = NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
if (tmpstring.length > origlen)
|
2007-03-18 14:25:18 -04:00
|
|
|
add_string_to_string(&config, &tmpstring);
|
2005-09-15 09:58:31 -04:00
|
|
|
done_string(&tmpstring);
|
|
|
|
|
|
|
|
if (!init_string(&tmpstring)) goto get_me_out;
|
|
|
|
|
|
|
|
add_cfg_header_to_string(&tmpstring,
|
|
|
|
conf_i18n(N_("Automatically saved keybindings\n"), i18n));
|
|
|
|
|
|
|
|
origlen = tmpstring.length;
|
|
|
|
bind_config_string(&tmpstring);
|
|
|
|
if (tmpstring.length > origlen)
|
2007-03-18 14:25:18 -04:00
|
|
|
add_string_to_string(&config, &tmpstring);
|
2005-09-15 09:58:31 -04:00
|
|
|
done_string(&tmpstring);
|
|
|
|
|
|
|
|
get_me_out:
|
|
|
|
return config.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-01-02 10:20:27 -05:00
|
|
|
write_config_file(char *prefix, char *name,
|
2007-08-29 09:43:15 -04:00
|
|
|
struct terminal *term)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
struct secure_save_info *ssi;
|
2021-01-02 10:20:27 -05:00
|
|
|
char *config_file = NULL;
|
|
|
|
char *cfg_str = create_config_string(prefix, name);
|
2005-09-15 09:58:31 -04:00
|
|
|
int prefixlen = strlen(prefix);
|
|
|
|
int prefix_has_slash = (prefixlen && dir_sep(prefix[prefixlen - 1]));
|
|
|
|
int name_has_slash = dir_sep(name[0]);
|
2021-01-02 10:20:27 -05:00
|
|
|
char *slash = name_has_slash || prefix_has_slash ? "" : STRING_DIR_SEP;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
if (!cfg_str) return -1;
|
|
|
|
|
|
|
|
if (name_has_slash && prefix_has_slash) name++;
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
config_file = straconcat(prefix, slash, name, (char *) NULL);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (!config_file) goto free_cfg_str;
|
|
|
|
|
2006-01-10 17:49:35 -05:00
|
|
|
ssi = secure_open(config_file);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (ssi) {
|
|
|
|
secure_fputs(ssi, cfg_str);
|
|
|
|
ret = secure_close(ssi);
|
2008-02-03 15:22:00 -05:00
|
|
|
if (!ret) {
|
|
|
|
struct domain_tree *domain;
|
|
|
|
|
|
|
|
untouch_options(config_options->value.tree);
|
|
|
|
foreach (domain, domain_trees)
|
|
|
|
untouch_options(domain->tree->value.tree);
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
write_config_dialog(term, config_file, secsave_errno, ret);
|
|
|
|
mem_free(config_file);
|
|
|
|
|
|
|
|
free_cfg_str:
|
|
|
|
mem_free(cfg_str);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
write_config(struct terminal *term)
|
|
|
|
{
|
|
|
|
assert(term);
|
|
|
|
|
|
|
|
if (!elinks_home) {
|
|
|
|
write_config_dialog(term, get_cmd_opt_str("config-file"),
|
|
|
|
SS_ERR_DISABLED, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return write_config_file(elinks_home, get_cmd_opt_str("config-file"),
|
2007-08-29 09:43:15 -04:00
|
|
|
term);
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|