2010-12-29 18:12:59 +03:00
|
|
|
/*
|
|
|
|
paths to configuration files
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 2010-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2010-12-29 18:12:59 +03:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2010.
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
2010-12-29 18:12:59 +03:00
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
2011-10-15 14:56:47 +04:00
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2010-12-29 18:12:59 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander 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 General Public License for more details.
|
2010-12-29 18:12:59 +03:00
|
|
|
|
|
|
|
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/>.
|
2010-12-29 18:12:59 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2011-12-05 18:52:46 +04:00
|
|
|
#include <stdlib.h>
|
2010-12-29 18:12:59 +03:00
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
#include "lib/fileloc.h"
|
2011-02-15 16:44:17 +03:00
|
|
|
#include "lib/vfs/vfs.h"
|
2011-10-15 14:56:47 +04:00
|
|
|
#include "lib/util.h" /* unix_error_string() */
|
2011-07-07 10:36:56 +04:00
|
|
|
|
|
|
|
#include "lib/mcconfig.h"
|
2010-12-29 18:12:59 +03:00
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
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) *************************************************/
|
|
|
|
|
2010-12-29 18:12:59 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
static gboolean xdg_vars_initialized = FALSE;
|
2011-11-07 18:24:23 +04:00
|
|
|
static char *mc_config_str = NULL;
|
|
|
|
static char *mc_cache_str = NULL;
|
|
|
|
static char *mc_data_str = NULL;
|
2010-12-29 18:12:59 +03:00
|
|
|
|
|
|
|
static gboolean config_dir_present = FALSE;
|
|
|
|
|
|
|
|
static const struct
|
|
|
|
{
|
2021-02-07 16:14:43 +03:00
|
|
|
char **basedir;
|
|
|
|
const char *filename;
|
2024-06-01 21:12:14 +03:00
|
|
|
} mc_config_files_reference[] = {
|
2010-12-29 18:12:59 +03:00
|
|
|
/* *INDENT-OFF* */
|
|
|
|
/* config */
|
2021-02-07 16:14:43 +03:00
|
|
|
{ &mc_config_str, MC_CONFIG_FILE },
|
|
|
|
{ &mc_config_str, MC_FHL_INI_FILE },
|
|
|
|
{ &mc_config_str, MC_HOTLIST_FILE },
|
|
|
|
{ &mc_config_str, GLOBAL_KEYMAP_FILE },
|
|
|
|
{ &mc_config_str, MC_USERMENU_FILE },
|
|
|
|
{ &mc_config_str, EDIT_HOME_MENU },
|
|
|
|
{ &mc_config_str, MC_PANELS_FILE },
|
2011-11-24 02:28:07 +04:00
|
|
|
|
2011-11-07 19:01:42 +04:00
|
|
|
/* User should move this file with applying some changes in file */
|
2022-07-22 20:12:59 +03:00
|
|
|
{ &mc_config_str, MC_EXT_FILE },
|
2022-07-22 20:18:16 +03:00
|
|
|
{ &mc_config_str, MC_EXT_OLD_FILE },
|
2010-12-29 18:12:59 +03:00
|
|
|
|
|
|
|
/* data */
|
2021-02-07 16:14:43 +03:00
|
|
|
{ &mc_data_str, MC_SKINS_DIR },
|
2023-09-24 17:32:22 +03:00
|
|
|
{ &mc_data_str, VFS_SHELL_PREFIX },
|
2021-02-18 10:28:34 +03:00
|
|
|
{ &mc_data_str, MC_ASHRC_FILE },
|
|
|
|
{ &mc_data_str, MC_BASHRC_FILE },
|
|
|
|
{ &mc_data_str, MC_INPUTRC_FILE },
|
2021-02-28 17:56:46 +03:00
|
|
|
{ &mc_data_str, MC_ZSHRC_FILE },
|
2021-02-07 16:14:43 +03:00
|
|
|
{ &mc_data_str, MC_EXTFS_DIR },
|
|
|
|
{ &mc_data_str, MC_HISTORY_FILE },
|
|
|
|
{ &mc_data_str, MC_FILEPOS_FILE },
|
2022-12-03 12:11:27 +03:00
|
|
|
{ &mc_data_str, EDIT_SYNTAX_FILE },
|
2021-02-07 16:14:43 +03:00
|
|
|
{ &mc_data_str, EDIT_HOME_CLIP_FILE },
|
|
|
|
{ &mc_data_str, MC_MACRO_FILE },
|
2010-12-29 18:12:59 +03:00
|
|
|
|
|
|
|
/* cache */
|
2021-02-07 16:14:43 +03:00
|
|
|
{ &mc_cache_str, "mc.log" },
|
|
|
|
{ &mc_cache_str, MC_TREESTORE_FILE },
|
|
|
|
{ &mc_cache_str, EDIT_HOME_TEMP_FILE },
|
|
|
|
{ &mc_cache_str, EDIT_HOME_BLOCK_FILE },
|
2010-12-29 18:12:59 +03:00
|
|
|
|
2021-02-07 16:14:43 +03:00
|
|
|
{ NULL, NULL }
|
2010-12-29 18:12:59 +03:00
|
|
|
/* *INDENT-ON* */
|
|
|
|
};
|
|
|
|
|
2019-07-28 10:28:26 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-12-29 18:12:59 +03:00
|
|
|
/*** file scope functions *********************************************************************** */
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_mkdir (const char *directory_name, GError **mcerror)
|
2010-12-29 18:12:59 +03:00
|
|
|
{
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_return_if_error (mcerror);
|
|
|
|
|
2010-12-29 18:12:59 +03:00
|
|
|
if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
|
|
|
|
(g_mkdir_with_parents (directory_name, 0700) != 0))
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
|
2010-12-29 18:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static char *
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_init_one_config_path (const char *path_base, const char *subdir, GError **mcerror)
|
2010-12-29 18:12:59 +03:00
|
|
|
{
|
|
|
|
char *full_path;
|
|
|
|
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_return_val_if_error (mcerror, FALSE);
|
2010-12-29 18:12:59 +03:00
|
|
|
|
2016-03-25 21:33:44 +03:00
|
|
|
full_path = g_build_filename (path_base, subdir, (char *) NULL);
|
2019-07-28 10:28:26 +03:00
|
|
|
|
2011-12-05 18:52:46 +04:00
|
|
|
if (g_file_test (full_path, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
|
|
|
|
config_dir_present = TRUE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2010-12-29 18:12:59 +03:00
|
|
|
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_config_mkdir (full_path, mcerror);
|
|
|
|
if (mcerror != NULL && *mcerror != NULL)
|
2014-08-05 11:18:30 +04:00
|
|
|
MC_PTR_FREE (full_path);
|
|
|
|
|
2010-12-29 18:12:59 +03:00
|
|
|
return full_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_config_init_config_paths (GError **mcerror)
|
2010-12-29 18:12:59 +03:00
|
|
|
{
|
2016-12-01 12:00:33 +03:00
|
|
|
const char *profile_root;
|
2012-03-16 14:53:34 +04:00
|
|
|
char *dir;
|
2010-12-29 18:12:59 +03:00
|
|
|
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_return_if_error (mcerror);
|
|
|
|
|
2010-12-29 18:12:59 +03:00
|
|
|
if (xdg_vars_initialized)
|
|
|
|
return;
|
|
|
|
|
2016-12-01 12:00:33 +03:00
|
|
|
profile_root = mc_get_profile_root ();
|
|
|
|
|
|
|
|
if (strcmp (profile_root, mc_config_get_home_dir ()) != 0)
|
2012-03-29 13:50:45 +04:00
|
|
|
{
|
2016-09-08 17:02:43 +03:00
|
|
|
/*
|
|
|
|
* The user overrode the default profile root.
|
|
|
|
*
|
|
|
|
* In this case we can't use GLib's g_get_user_{config,cache,data}_dir()
|
|
|
|
* as these functions use the user's home dir as the root.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dir = g_build_filename (profile_root, ".config", (char *) NULL);
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
|
2012-03-29 13:50:45 +04:00
|
|
|
g_free (dir);
|
2010-12-29 18:12:59 +03:00
|
|
|
|
2016-09-08 17:02:43 +03:00
|
|
|
dir = g_build_filename (profile_root, ".cache", (char *) NULL);
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
|
2012-03-29 13:50:45 +04:00
|
|
|
g_free (dir);
|
2010-12-29 18:12:59 +03:00
|
|
|
|
2016-09-08 17:02:43 +03:00
|
|
|
dir = g_build_filename (profile_root, ".local", "share", (char *) NULL);
|
2014-07-15 16:53:06 +04:00
|
|
|
mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
|
2012-03-29 13:50:45 +04:00
|
|
|
g_free (dir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-08 00:51:20 +03:00
|
|
|
mc_config_str =
|
|
|
|
mc_config_init_one_config_path (g_get_user_config_dir (), MC_USERCONF_DIR, mcerror);
|
|
|
|
mc_cache_str =
|
|
|
|
mc_config_init_one_config_path (g_get_user_cache_dir (), MC_USERCONF_DIR, mcerror);
|
|
|
|
mc_data_str =
|
|
|
|
mc_config_init_one_config_path (g_get_user_data_dir (), MC_USERCONF_DIR, mcerror);
|
2012-03-29 13:50:45 +04:00
|
|
|
}
|
2011-02-16 15:23:29 +03:00
|
|
|
|
2010-12-29 18:12:59 +03:00
|
|
|
xdg_vars_initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
mc_config_deinit_config_paths (void)
|
|
|
|
{
|
|
|
|
if (!xdg_vars_initialized)
|
|
|
|
return;
|
|
|
|
|
2011-11-07 18:24:23 +04:00
|
|
|
g_free (mc_config_str);
|
|
|
|
g_free (mc_cache_str);
|
|
|
|
g_free (mc_data_str);
|
2010-12-29 18:12:59 +03:00
|
|
|
|
2011-02-16 15:23:29 +03:00
|
|
|
g_free (mc_global.share_data_dir);
|
|
|
|
g_free (mc_global.sysconfig_dir);
|
|
|
|
|
2010-12-29 18:12:59 +03:00
|
|
|
xdg_vars_initialized = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
const char *
|
|
|
|
mc_config_get_data_path (void)
|
|
|
|
{
|
|
|
|
if (!xdg_vars_initialized)
|
|
|
|
mc_config_init_config_paths (NULL);
|
|
|
|
|
2011-11-07 18:24:23 +04:00
|
|
|
return (const char *) mc_data_str;
|
2010-12-29 18:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
const char *
|
|
|
|
mc_config_get_cache_path (void)
|
|
|
|
{
|
|
|
|
if (!xdg_vars_initialized)
|
|
|
|
mc_config_init_config_paths (NULL);
|
|
|
|
|
2011-11-07 18:24:23 +04:00
|
|
|
return (const char *) mc_cache_str;
|
2010-12-29 18:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
const char *
|
|
|
|
mc_config_get_home_dir (void)
|
|
|
|
{
|
2016-09-08 17:07:27 +03:00
|
|
|
static const char *homedir = NULL;
|
|
|
|
|
2010-12-29 18:12:59 +03:00
|
|
|
if (homedir == NULL)
|
|
|
|
{
|
2016-09-08 00:51:20 +03:00
|
|
|
/* Prior to GLib 2.36, g_get_home_dir() ignores $HOME, which is why
|
|
|
|
* we read it ourselves. As that function's documentation explains,
|
|
|
|
* using $HOME is good for compatibility with other programs and
|
|
|
|
* for running from test frameworks. */
|
2016-09-08 17:02:43 +03:00
|
|
|
homedir = g_getenv ("HOME");
|
2012-03-16 14:53:34 +04:00
|
|
|
if (homedir == NULL || *homedir == '\0')
|
2010-12-29 18:12:59 +03:00
|
|
|
homedir = g_get_home_dir ();
|
|
|
|
}
|
2019-07-28 10:28:26 +03:00
|
|
|
|
2010-12-29 18:12:59 +03:00
|
|
|
return homedir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
const char *
|
|
|
|
mc_config_get_path (void)
|
|
|
|
{
|
|
|
|
if (!xdg_vars_initialized)
|
|
|
|
mc_config_init_config_paths (NULL);
|
|
|
|
|
2011-11-07 18:24:23 +04:00
|
|
|
return (const char *) mc_config_str;
|
2010-12-29 18:12:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-11-24 02:28:07 +04:00
|
|
|
/**
|
|
|
|
* Get full path to config file by short name.
|
|
|
|
*
|
|
|
|
* @param config_name short name
|
|
|
|
* @return full path to config file
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
mc_config_get_full_path (const char *config_name)
|
|
|
|
{
|
|
|
|
size_t rule_index;
|
|
|
|
|
|
|
|
if (config_name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-12-02 00:04:05 +04:00
|
|
|
if (!xdg_vars_initialized)
|
|
|
|
mc_config_init_config_paths (NULL);
|
|
|
|
|
2021-02-07 16:14:43 +03:00
|
|
|
for (rule_index = 0; mc_config_files_reference[rule_index].filename != NULL; rule_index++)
|
|
|
|
if (strcmp (config_name, mc_config_files_reference[rule_index].filename) == 0)
|
|
|
|
return g_build_filename (*mc_config_files_reference[rule_index].basedir,
|
|
|
|
mc_config_files_reference[rule_index].filename, (char *) NULL);
|
2019-07-28 10:28:26 +03:00
|
|
|
|
2011-11-24 02:28:07 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-07-18 01:30:58 +04:00
|
|
|
/**
|
|
|
|
* Get full path to config file by short name.
|
|
|
|
*
|
|
|
|
* @param config_name short name
|
|
|
|
* @return object with full path to config file
|
|
|
|
*/
|
|
|
|
|
|
|
|
vfs_path_t *
|
|
|
|
mc_config_get_full_vpath (const char *config_name)
|
|
|
|
{
|
|
|
|
vfs_path_t *ret_vpath;
|
|
|
|
char *str_path;
|
|
|
|
|
|
|
|
str_path = mc_config_get_full_path (config_name);
|
|
|
|
|
2012-03-29 13:50:45 +04:00
|
|
|
ret_vpath = vfs_path_from_str (str_path);
|
2011-07-18 01:30:58 +04:00
|
|
|
g_free (str_path);
|
2019-07-28 10:28:26 +03:00
|
|
|
|
2011-07-18 01:30:58 +04:00
|
|
|
return ret_vpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|