Clarify macro variables for USE_INTERNAL_EDIT.
[midnight-commander.git] / lib / mcconfig / paths.c
blobbfcf3f48d257453853b6fab80f2304d2dfa82e1c
1 /*
2 paths to configuration files
4 Copyright (C) 2010-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2010.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
32 #include "lib/global.h"
33 #include "lib/fileloc.h"
34 #include "lib/vfs/vfs.h"
35 #include "lib/util.h" /* unix_error_string() */
37 #include "lib/mcconfig.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 #define MC_OLD_USERCONF_DIR ".mc"
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 static gboolean xdg_vars_initialized = FALSE;
50 static char *mc_config_str = NULL;
51 static char *mc_cache_str = NULL;
52 static char *mc_data_str = NULL;
54 static const char *homedir = NULL;
55 /* value of $MC_HOME */
56 static const char *mc_home = NULL;
58 static gboolean config_dir_present = FALSE;
60 static const struct
62 const char *old_filename;
64 char **new_basedir;
65 const char *new_filename;
66 } mc_config_files_reference[] =
68 /* *INDENT-OFF* */
69 /* config */
70 { "ini", &mc_config_str, MC_CONFIG_FILE},
71 { "filehighlight.ini", &mc_config_str, MC_FHL_INI_FILE},
72 { "hotlist", &mc_config_str, MC_HOTLIST_FILE},
73 { "mc.keymap", &mc_config_str, GLOBAL_KEYMAP_FILE},
74 { "menu", &mc_config_str, MC_USERMENU_FILE},
75 { "cedit" PATH_SEP_STR "Syntax", &mc_config_str, EDIT_SYNTAX_FILE},
76 { "cedit" PATH_SEP_STR "menu", &mc_config_str, EDIT_HOME_MENU},
77 { "cedit" PATH_SEP_STR "edit.indent.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc"},
78 { "cedit" PATH_SEP_STR "edit.spell.rc", &mc_config_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc"},
79 { "panels.ini", &mc_config_str, MC_PANELS_FILE},
81 /* User should move this file with applying some changes in file */
82 { "", &mc_config_str, MC_FILEBIND_FILE},
84 /* data */
85 { "skins", &mc_data_str, MC_SKINS_SUBDIR},
86 { "fish", &mc_data_str, FISH_PREFIX},
87 { "ashrc", &mc_data_str, "ashrc"},
88 { "bashrc", &mc_data_str, "bashrc"},
89 { "inputrc", &mc_data_str, "inputrc"},
90 { "extfs.d", &mc_data_str, MC_EXTFS_DIR},
91 { "history", &mc_data_str, MC_HISTORY_FILE},
92 { "filepos", &mc_data_str, MC_FILEPOS_FILE},
93 { "cedit" PATH_SEP_STR "cooledit.clip", &mc_data_str, EDIT_CLIP_FILE},
94 { "", &mc_data_str, MC_MACRO_FILE},
96 /* cache */
97 { "log", &mc_cache_str, "mc.log"},
98 { "Tree", &mc_cache_str, MC_TREESTORE_FILE},
99 { "cedit" PATH_SEP_STR "cooledit.temp", &mc_cache_str, EDIT_TEMP_FILE},
100 { "cedit" PATH_SEP_STR "cooledit.block", &mc_cache_str, EDIT_BLOCK_FILE},
102 {NULL, NULL, NULL}
103 /* *INDENT-ON* */
106 #if MC_HOMEDIR_XDG
107 static const struct
109 char **old_basedir;
110 const char *filename;
112 char **new_basedir;
113 } mc_config_migrate_rules_fix[] =
115 /* *INDENT-OFF* */
116 { &mc_data_str, MC_USERMENU_FILE, &mc_config_str},
117 { &mc_data_str, EDIT_SYNTAX_FILE, &mc_config_str},
118 { &mc_data_str, EDIT_HOME_MENU, &mc_config_str},
119 { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.indent.rc", &mc_config_str},
120 { &mc_data_str, EDIT_DIR PATH_SEP_STR "edit.spell.rc", &mc_config_str},
121 { &mc_data_str, MC_FILEBIND_FILE, &mc_config_str},
123 { &mc_cache_str, MC_HISTORY_FILE, &mc_data_str},
124 { &mc_cache_str, MC_FILEPOS_FILE, &mc_data_str},
125 { &mc_cache_str, EDIT_CLIP_FILE, &mc_data_str},
127 { &mc_cache_str, MC_PANELS_FILE, &mc_config_str},
129 {NULL, NULL, NULL}
130 /* *INDENT-ON* */
132 #endif /* MC_HOMEDIR_XDG */
134 /*** file scope functions *********************************************************************** */
135 /* --------------------------------------------------------------------------------------------- */
137 static void
138 mc_config_mkdir (const char *directory_name, GError ** mcerror)
140 mc_return_if_error (mcerror);
142 if ((!g_file_test (directory_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) &&
143 (g_mkdir_with_parents (directory_name, 0700) != 0))
144 mc_propagate_error (mcerror, 0, _("Cannot create %s directory"), directory_name);
147 /* --------------------------------------------------------------------------------------------- */
149 static char *
150 mc_config_init_one_config_path (const char *path_base, const char *subdir, GError ** mcerror)
152 char *full_path;
154 mc_return_val_if_error (mcerror, FALSE);
156 full_path = g_build_filename (path_base, subdir, NULL);
157 if (g_file_test (full_path, G_FILE_TEST_EXISTS))
159 if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
161 config_dir_present = TRUE;
163 else
165 fprintf (stderr, "%s %s\n", _("FATAL: not a directory:"), full_path);
166 exit (EXIT_FAILURE);
170 mc_config_mkdir (full_path, mcerror);
171 if (mcerror != NULL && *mcerror != NULL)
172 MC_PTR_FREE (full_path);
174 return full_path;
177 /* --------------------------------------------------------------------------------------------- */
179 static char *
180 mc_config_get_deprecated_path (void)
182 return g_build_filename (mc_config_get_home_dir (), MC_OLD_USERCONF_DIR, NULL);
185 /* --------------------------------------------------------------------------------------------- */
187 static void
188 mc_config_copy (const char *old_name, const char *new_name, GError ** mcerror)
190 mc_return_if_error (mcerror);
192 if (g_file_test (old_name, G_FILE_TEST_IS_REGULAR))
194 char *contents = NULL;
195 size_t length;
197 if (g_file_get_contents (old_name, &contents, &length, mcerror))
198 g_file_set_contents (new_name, contents, length, mcerror);
200 g_free (contents);
201 return;
204 if (g_file_test (old_name, G_FILE_TEST_IS_DIR))
207 GDir *dir;
208 const char *dir_name;
210 dir = g_dir_open (old_name, 0, mcerror);
211 if (dir == NULL)
212 return;
214 if (g_mkdir_with_parents (new_name, 0700) == -1)
216 g_dir_close (dir);
217 mc_propagate_error (mcerror, 0,
218 _("An error occurred while migrating user settings: %s"),
219 unix_error_string (errno));
220 return;
223 while ((dir_name = g_dir_read_name (dir)) != NULL)
225 char *old_name2, *new_name2;
227 old_name2 = g_build_filename (old_name, dir_name, NULL);
228 new_name2 = g_build_filename (new_name, dir_name, NULL);
229 mc_config_copy (old_name2, new_name2, mcerror);
230 g_free (new_name2);
231 g_free (old_name2);
236 /* --------------------------------------------------------------------------------------------- */
238 #if MC_HOMEDIR_XDG
239 static void
240 mc_config_fix_migrated_rules (void)
242 size_t rule_index;
244 for (rule_index = 0; mc_config_migrate_rules_fix[rule_index].old_basedir != NULL; rule_index++)
246 char *old_name;
248 old_name =
249 g_build_filename (*mc_config_migrate_rules_fix[rule_index].old_basedir,
250 mc_config_migrate_rules_fix[rule_index].filename, NULL);
252 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
254 char *new_name;
255 const char *basedir = *mc_config_migrate_rules_fix[rule_index].new_basedir;
256 const char *filename = mc_config_migrate_rules_fix[rule_index].filename;
258 new_name = g_build_filename (basedir, filename, NULL);
259 rename (old_name, new_name);
260 g_free (new_name);
262 g_free (old_name);
265 #endif /* MC_HOMEDIR_XDG */
267 /* --------------------------------------------------------------------------------------------- */
269 static gboolean
270 mc_config_deprecated_dir_present (void)
272 char *old_dir;
273 gboolean is_present;
275 old_dir = mc_config_get_deprecated_path ();
276 is_present = g_file_test (old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
277 g_free (old_dir);
279 return is_present && !config_dir_present;
282 /* --------------------------------------------------------------------------------------------- */
283 /*** public functions ****************************************************************************/
284 /* --------------------------------------------------------------------------------------------- */
286 void
287 mc_config_init_config_paths (GError ** mcerror)
289 char *dir;
290 #if MC_HOMEDIR_XDG == 0
291 char *defined_userconf_dir;
292 #endif
294 mc_return_if_error (mcerror);
296 if (xdg_vars_initialized)
297 return;
299 /* init mc_home and homedir if not yet */
300 (void) mc_config_get_home_dir ();
302 #if MC_HOMEDIR_XDG
303 if (mc_home != NULL)
305 dir = g_build_filename (mc_home, ".config", (char *) NULL);
306 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
307 g_free (dir);
309 dir = g_build_filename (mc_home, ".cache", (char *) NULL);
310 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
311 g_free (dir);
313 dir = g_build_filename (mc_home, ".local", "share", (char *) NULL);
314 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
315 g_free (dir);
317 else
319 dir = (char *) g_get_user_config_dir ();
320 if (dir != NULL && *dir != '\0')
321 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
322 else
324 dir = g_build_filename (homedir, ".config", (char *) NULL);
325 mc_config_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
326 g_free (dir);
329 dir = (char *) g_get_user_cache_dir ();
330 if (dir != NULL && *dir != '\0')
331 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
332 else
334 dir = g_build_filename (homedir, ".cache", (char *) NULL);
335 mc_cache_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
336 g_free (dir);
339 dir = (char *) g_get_user_data_dir ();
340 if (dir != NULL && *dir != '\0')
341 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
342 else
344 dir = g_build_filename (homedir, ".local", "share", (char *) NULL);
345 mc_data_str = mc_config_init_one_config_path (dir, MC_USERCONF_DIR, mcerror);
346 g_free (dir);
350 mc_config_fix_migrated_rules ();
351 #else /* MC_HOMEDIR_XDG */
352 defined_userconf_dir = tilde_expand (MC_USERCONF_DIR);
353 if (g_path_is_absolute (defined_userconf_dir))
354 dir = defined_userconf_dir;
355 else
357 g_free (defined_userconf_dir);
358 dir = g_build_filename (mc_config_get_home_dir (), MC_USERCONF_DIR, (char *) NULL);
361 mc_data_str = mc_cache_str = mc_config_str = mc_config_init_one_config_path (dir, "", mcerror);
363 g_free (dir);
364 #endif /* MC_HOMEDIR_XDG */
366 xdg_vars_initialized = TRUE;
369 /* --------------------------------------------------------------------------------------------- */
371 void
372 mc_config_deinit_config_paths (void)
374 if (!xdg_vars_initialized)
375 return;
377 g_free (mc_config_str);
378 #if MC_HOMEDIR_XDG
379 g_free (mc_cache_str);
380 g_free (mc_data_str);
381 #endif /* MC_HOMEDIR_XDG */
383 g_free (mc_global.share_data_dir);
384 g_free (mc_global.sysconfig_dir);
386 xdg_vars_initialized = FALSE;
389 /* --------------------------------------------------------------------------------------------- */
391 const char *
392 mc_config_get_data_path (void)
394 if (!xdg_vars_initialized)
395 mc_config_init_config_paths (NULL);
397 return (const char *) mc_data_str;
400 /* --------------------------------------------------------------------------------------------- */
402 const char *
403 mc_config_get_cache_path (void)
405 if (!xdg_vars_initialized)
406 mc_config_init_config_paths (NULL);
408 return (const char *) mc_cache_str;
411 /* --------------------------------------------------------------------------------------------- */
413 const char *
414 mc_config_get_home_dir (void)
416 if (homedir == NULL)
418 homedir = g_getenv ("MC_HOME");
419 if (homedir == NULL || *homedir == '\0')
420 homedir = g_getenv ("HOME");
421 else
422 mc_home = homedir;
423 if (homedir == NULL || *homedir == '\0')
424 homedir = g_get_home_dir ();
426 return homedir;
429 /* --------------------------------------------------------------------------------------------- */
431 const char *
432 mc_config_get_path (void)
434 if (!xdg_vars_initialized)
435 mc_config_init_config_paths (NULL);
437 return (const char *) mc_config_str;
440 /* --------------------------------------------------------------------------------------------- */
442 gboolean
443 mc_config_migrate_from_old_place (GError ** mcerror, char **msg)
445 char *old_dir;
446 size_t rule_index;
448 mc_return_val_if_error (mcerror, FALSE);
450 if (!mc_config_deprecated_dir_present ())
451 return FALSE;
453 old_dir = mc_config_get_deprecated_path ();
455 g_free (mc_config_init_one_config_path (mc_config_str, EDIT_DIR, mcerror));
456 #if MC_HOMEDIR_XDG
457 g_free (mc_config_init_one_config_path (mc_cache_str, EDIT_DIR, mcerror));
458 g_free (mc_config_init_one_config_path (mc_data_str, EDIT_DIR, mcerror));
459 #endif /* MC_HOMEDIR_XDG */
461 mc_return_val_if_error (mcerror, FALSE);
463 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
465 char *old_name;
466 if (*mc_config_files_reference[rule_index].old_filename == '\0')
467 continue;
469 old_name =
470 g_build_filename (old_dir, mc_config_files_reference[rule_index].old_filename, NULL);
472 if (g_file_test (old_name, G_FILE_TEST_EXISTS))
474 char *new_name;
475 const char *basedir = *mc_config_files_reference[rule_index].new_basedir;
476 const char *filename = mc_config_files_reference[rule_index].new_filename;
478 new_name = g_build_filename (basedir, filename, NULL);
479 mc_config_copy (old_name, new_name, mcerror);
480 g_free (new_name);
482 g_free (old_name);
485 #if MC_HOMEDIR_XDG
486 *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
487 "to Freedesktop recommended dirs.\n"
488 "To get more info, please visit\n"
489 "http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"),
490 old_dir);
491 #else /* MC_HOMEDIR_XDG */
492 *msg = g_strdup_printf (_("Your old settings were migrated from %s\n"
493 "to %s\n"), old_dir, mc_config_str);
494 #endif /* MC_HOMEDIR_XDG */
496 g_free (old_dir);
498 return TRUE;
501 /* --------------------------------------------------------------------------------------------- */
503 * Get full path to config file by short name.
505 * @param config_name short name
506 * @return full path to config file
509 char *
510 mc_config_get_full_path (const char *config_name)
512 size_t rule_index;
514 if (config_name == NULL)
515 return NULL;
517 if (!xdg_vars_initialized)
518 mc_config_init_config_paths (NULL);
520 for (rule_index = 0; mc_config_files_reference[rule_index].old_filename != NULL; rule_index++)
522 if (strcmp (config_name, mc_config_files_reference[rule_index].new_filename) == 0)
524 return g_build_filename (*mc_config_files_reference[rule_index].new_basedir,
525 mc_config_files_reference[rule_index].new_filename, NULL);
528 return NULL;
531 /* --------------------------------------------------------------------------------------------- */
533 * Get full path to config file by short name.
535 * @param config_name short name
536 * @return object with full path to config file
539 vfs_path_t *
540 mc_config_get_full_vpath (const char *config_name)
542 vfs_path_t *ret_vpath;
543 char *str_path;
545 str_path = mc_config_get_full_path (config_name);
547 ret_vpath = vfs_path_from_str (str_path);
548 g_free (str_path);
549 return ret_vpath;
552 /* --------------------------------------------------------------------------------------------- */