2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Configure module for the Midnight Commander
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 1994-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2011-10-15 14:56:47 +04:00
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2009-05-20 10:33:53 +04: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
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-05-20 10:33:53 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2009-06-21 21:06:29 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2010-03-30 12:10:25 +04:00
|
|
|
#include <errno.h> /* extern int errno */
|
2009-10-27 13:14:32 +03:00
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2011-02-10 16:32:38 +03:00
|
|
|
#include "lib/vfs/vfs.h" /* mc_stat */
|
2010-11-11 16:58:29 +03:00
|
|
|
#include "lib/util.h"
|
2019-07-28 10:28:26 +03:00
|
|
|
|
2010-01-21 16:06:15 +03:00
|
|
|
#include "lib/mcconfig.h"
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/*** global variables ****************************************************************************/
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/*** file scope macro definitions ****************************************************************/
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
2009-05-20 10:33:53 +04:00
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-10-05 14:15:30 +04:00
|
|
|
|
|
|
|
static gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_new_or_override_file (mc_config_t *mc_config, const gchar *ini_path, GError **mcerror)
|
2009-10-05 14:15:30 +04:00
|
|
|
{
|
|
|
|
gchar *data, *written_data;
|
|
|
|
gsize len, total_written;
|
|
|
|
gboolean ret;
|
|
|
|
int fd;
|
|
|
|
ssize_t cur_written;
|
2011-07-22 00:33:45 +04:00
|
|
|
vfs_path_t *ini_vpath;
|
2009-10-05 14:15:30 +04:00
|
|
|
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_return_val_if_error (mcerror, FALSE);
|
|
|
|
|
2009-10-05 14:15:30 +04:00
|
|
|
data = g_key_file_to_data (mc_config->handle, &len, NULL);
|
2010-03-30 12:10:25 +04:00
|
|
|
if (!exist_file (ini_path))
|
|
|
|
{
|
2014-07-15 16:53:06 +04:00
|
|
|
ret = g_file_set_contents (ini_path, data, len, mcerror);
|
2009-10-05 14:15:30 +04:00
|
|
|
g_free (data);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-03-02 16:33:21 +04:00
|
|
|
|
2009-10-05 18:32:46 +04:00
|
|
|
mc_util_make_backup_if_possible (ini_path, "~");
|
2009-10-05 14:15:30 +04:00
|
|
|
|
2013-03-02 16:33:21 +04:00
|
|
|
ini_vpath = vfs_path_from_str (ini_path);
|
2011-07-22 00:33:45 +04:00
|
|
|
fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
|
2021-02-21 19:30:18 +03:00
|
|
|
vfs_path_free (ini_vpath, TRUE);
|
2013-03-02 16:33:21 +04:00
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
|
2010-03-30 12:10:25 +04:00
|
|
|
g_free (data);
|
2009-10-05 14:15:30 +04:00
|
|
|
return FALSE;
|
2009-10-27 13:14:32 +03:00
|
|
|
}
|
2009-10-05 14:15:30 +04:00
|
|
|
|
|
|
|
for (written_data = data, total_written = len;
|
|
|
|
(cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
|
2013-03-02 16:33:21 +04:00
|
|
|
written_data += cur_written, total_written -= cur_written)
|
|
|
|
;
|
2019-07-28 10:28:26 +03:00
|
|
|
|
2009-10-05 14:15:30 +04:00
|
|
|
mc_close (fd);
|
|
|
|
g_free (data);
|
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
if (cur_written == -1)
|
|
|
|
{
|
2009-10-05 18:32:46 +04:00
|
|
|
mc_util_restore_from_backup_if_possible (ini_path, "~");
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno));
|
2009-10-05 14:15:30 +04:00
|
|
|
return FALSE;
|
2009-10-05 18:32:46 +04:00
|
|
|
}
|
2009-10-05 14:15:30 +04:00
|
|
|
|
2009-10-05 18:32:46 +04:00
|
|
|
mc_util_unlink_backup_if_possible (ini_path, "~");
|
2009-10-05 14:15:30 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
mc_config_t *
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_init (const gchar *ini_path, gboolean read_only)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
|
|
|
mc_config_t *mc_config;
|
2009-06-21 21:06:29 +04:00
|
|
|
struct stat st;
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
mc_config = g_try_malloc0 (sizeof (mc_config_t));
|
|
|
|
if (mc_config == NULL)
|
2009-10-05 14:15:30 +04:00
|
|
|
return NULL;
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
mc_config->handle = g_key_file_new ();
|
2010-03-30 12:10:25 +04:00
|
|
|
if (mc_config->handle == NULL)
|
|
|
|
{
|
2009-10-05 14:15:30 +04:00
|
|
|
g_free (mc_config);
|
|
|
|
return NULL;
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
2019-07-28 10:28:26 +03:00
|
|
|
|
2011-02-14 14:10:33 +03:00
|
|
|
if (ini_path == NULL)
|
2009-10-05 14:15:30 +04:00
|
|
|
return mc_config;
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2011-07-18 01:30:58 +04:00
|
|
|
if (exist_file (ini_path))
|
2010-03-30 12:10:25 +04:00
|
|
|
{
|
2011-07-18 01:30:58 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str (ini_path);
|
|
|
|
if (mc_stat (vpath, &st) == 0 && st.st_size != 0)
|
|
|
|
{
|
2012-07-28 14:19:27 +04:00
|
|
|
GKeyFileFlags flags = G_KEY_FILE_NONE;
|
|
|
|
|
|
|
|
if (!read_only)
|
|
|
|
flags |= G_KEY_FILE_KEEP_COMMENTS;
|
|
|
|
|
2011-07-18 01:30:58 +04:00
|
|
|
/* file exists and not empty */
|
2012-07-28 14:19:27 +04:00
|
|
|
g_key_file_load_from_file (mc_config->handle, ini_path, flags, NULL);
|
2011-07-18 01:30:58 +04:00
|
|
|
}
|
2021-02-21 19:30:18 +03:00
|
|
|
vfs_path_free (vpath, TRUE);
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
2009-06-21 21:06:29 +04:00
|
|
|
|
2009-06-24 13:48:32 +04:00
|
|
|
mc_config->ini_path = g_strdup (ini_path);
|
|
|
|
return mc_config;
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_deinit (mc_config_t *mc_config)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
2010-11-04 15:13:41 +03:00
|
|
|
if (mc_config != NULL)
|
|
|
|
{
|
|
|
|
g_free (mc_config->ini_path);
|
|
|
|
g_key_file_free (mc_config->handle);
|
|
|
|
g_free (mc_config);
|
|
|
|
}
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_has_param (const mc_config_t *mc_config, const char *group, const gchar *param)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
2022-07-26 16:13:57 +03:00
|
|
|
char *value;
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (mc_config != NULL, FALSE);
|
|
|
|
|
|
|
|
value = g_key_file_get_value (mc_config->handle, group, param, NULL);
|
|
|
|
ret = value != NULL;
|
|
|
|
g_free (value);
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2022-07-26 16:13:57 +03:00
|
|
|
return ret;
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_has_group (mc_config_t *mc_config, const char *group)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
2019-07-28 10:28:26 +03:00
|
|
|
if (mc_config == NULL || group == NULL)
|
2009-10-05 14:15:30 +04:00
|
|
|
return FALSE;
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
return g_key_file_has_group (mc_config->handle, group);
|
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_del_key (mc_config_t *mc_config, const char *group, const gchar *param)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
2019-07-28 10:28:26 +03:00
|
|
|
if (mc_config == NULL || group == NULL || param == NULL)
|
2009-10-05 14:15:30 +04:00
|
|
|
return FALSE;
|
2015-11-15 09:31:19 +03:00
|
|
|
|
2009-05-20 10:33:53 +04:00
|
|
|
return g_key_file_remove_key (mc_config->handle, group, param, NULL);
|
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_del_group (mc_config_t *mc_config, const char *group)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
2019-07-28 10:28:26 +03:00
|
|
|
if (mc_config == NULL || group == NULL)
|
2009-10-05 14:15:30 +04:00
|
|
|
return FALSE;
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
return g_key_file_remove_group (mc_config->handle, group, NULL);
|
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_read_file (mc_config_t *mc_config, const gchar *ini_path, gboolean read_only,
|
2012-07-28 14:19:27 +04:00
|
|
|
gboolean remove_empty)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
|
|
|
mc_config_t *tmp_config;
|
|
|
|
gchar **groups, **curr_grp;
|
|
|
|
gchar *value;
|
2014-05-04 12:05:30 +04:00
|
|
|
gboolean ok;
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
if (mc_config == NULL)
|
2009-10-05 14:15:30 +04:00
|
|
|
return FALSE;
|
2009-05-20 10:33:53 +04:00
|
|
|
|
2012-07-28 14:19:27 +04:00
|
|
|
tmp_config = mc_config_init (ini_path, read_only);
|
2009-05-20 10:33:53 +04:00
|
|
|
if (tmp_config == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
groups = mc_config_get_groups (tmp_config, NULL);
|
2014-05-04 12:05:30 +04:00
|
|
|
ok = (*groups != NULL);
|
2009-12-11 15:48:01 +03:00
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
|
|
|
|
{
|
2014-05-04 11:38:25 +04:00
|
|
|
gchar **keys, **curr_key;
|
|
|
|
|
2009-10-05 14:15:30 +04:00
|
|
|
keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
|
2014-05-04 11:38:25 +04:00
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
for (curr_key = keys; *curr_key != NULL; curr_key++)
|
|
|
|
{
|
2009-05-20 10:33:53 +04:00
|
|
|
value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
|
2011-02-23 15:19:37 +03:00
|
|
|
if (value != NULL)
|
|
|
|
{
|
|
|
|
if (*value == '\0' && remove_empty)
|
|
|
|
g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
|
|
|
|
else
|
|
|
|
g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
|
|
|
|
g_free (value);
|
|
|
|
}
|
|
|
|
else if (remove_empty)
|
|
|
|
g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
2009-10-05 14:15:30 +04:00
|
|
|
g_strfreev (keys);
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
2014-05-04 12:05:30 +04:00
|
|
|
|
2009-10-05 14:15:30 +04:00
|
|
|
g_strfreev (groups);
|
|
|
|
mc_config_deinit (tmp_config);
|
2014-05-04 12:05:30 +04:00
|
|
|
|
|
|
|
return ok;
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_save_file (mc_config_t *mc_config, GError **mcerror)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_return_val_if_error (mcerror, FALSE);
|
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
if (mc_config == NULL || mc_config->ini_path == NULL)
|
2009-10-05 14:15:30 +04:00
|
|
|
return FALSE;
|
2014-07-15 16:53:06 +04:00
|
|
|
|
|
|
|
return mc_config_new_or_override_file (mc_config, mc_config->ini_path, mcerror);
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-20 10:33:53 +04:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_save_to_file (mc_config_t *mc_config, const gchar *ini_path, GError **mcerror)
|
2009-05-20 10:33:53 +04:00
|
|
|
{
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_return_val_if_error (mcerror, FALSE);
|
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
if (mc_config == NULL)
|
2009-10-05 14:15:30 +04:00
|
|
|
return FALSE;
|
2014-07-15 16:53:06 +04:00
|
|
|
|
|
|
|
return mc_config_new_or_override_file (mc_config, ini_path, mcerror);
|
2009-05-20 10:33:53 +04:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|