Removed type SHELL_ESCAPED_STR in favour of plain char*
[midnight-commander.git] / src / setup.c
blob8e6ae1d83d70f79b61b509f9fcce893411b84110
1 /* Setup loading/saving.
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 #include <config.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
26 #include <mhl/memory.h>
27 #include <mhl/string.h>
29 #include "global.h"
30 #include "tty.h"
31 #include "dir.h"
32 #include "panel.h"
33 #include "main.h"
34 #include "tree.h" /* xtree_mode */
35 #include "profile.h"
36 #include "setup.h"
37 #include "mouse.h" /* To make view.h happy */
38 #include "view.h" /* For the externs */
39 #include "key.h" /* For the externs */
40 #include "hotlist.h" /* load/save/done hotlist */
41 #include "panelize.h" /* load/save/done panelize */
42 #include "layout.h"
43 #include "menu.h" /* menubar_visible declaration */
44 #include "win.h" /* lookup_key */
45 #include "cmd.h"
46 #include "file.h" /* safe_delete */
48 #ifdef USE_VFS
49 #include "../vfs/gc.h"
50 #endif
52 #ifdef HAVE_CHARSET
53 #include "charsets.h"
54 #endif
56 #ifdef USE_NETCODE
57 # include "../vfs/ftpfs.h"
58 # include "../vfs/fish.h"
59 #endif
61 #ifdef USE_INTERNAL_EDIT
62 # include "../edit/edit.h"
63 #endif
66 extern char *find_ignore_dirs;
68 extern int num_history_items_recorded;
70 char *profile_name; /* .mc/ini */
71 char *global_profile_name; /* mc.lib */
73 char setup_color_string [4096];
74 char term_color_string [4096];
75 char color_terminal_string [512];
77 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
78 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
79 #define save_string WritePrivateProfileString
81 int startup_left_mode;
82 int startup_right_mode;
84 /* Ugly hack to allow panel_save_setup to work as a place holder for */
85 /* default panel values */
86 int saving_setup;
88 static const struct {
89 const char *key;
90 sortfn *sort_type;
91 } sort_names [] = {
92 { "name", (sortfn *) sort_name },
93 { "extension", (sortfn *) sort_ext },
94 { "time", (sortfn *) sort_time },
95 { "atime", (sortfn *) sort_atime },
96 { "ctime", (sortfn *) sort_ctime },
97 { "size", (sortfn *) sort_size },
98 { "inode", (sortfn *) sort_inode },
99 { "unsorted", (sortfn *) unsorted },
100 { 0, 0 }
103 static const struct {
104 const char *key;
105 int list_type;
106 } list_types [] = {
107 { "full", list_full },
108 { "brief", list_brief },
109 { "long", list_long },
110 { "user", list_user },
111 { 0, 0 }
114 static const struct {
115 const char *opt_name;
116 int opt_type;
117 } panel_types [] = {
118 { "listing", view_listing },
119 { "quickview", view_quick },
120 { "info", view_info },
121 { "tree", view_tree },
122 { 0, 0 }
125 static const struct {
126 const char *opt_name;
127 int *opt_addr;
128 } layout [] = {
129 { "equal_split", &equal_split },
130 { "first_panel_size", &first_panel_size },
131 { "message_visible", &message_visible },
132 { "keybar_visible", &keybar_visible },
133 { "xterm_title", &xterm_title },
134 { "output_lines", &output_lines },
135 { "command_prompt", &command_prompt },
136 { "menubar_visible", &menubar_visible },
137 { "show_mini_info", &show_mini_info },
138 { "permission_mode", &permission_mode },
139 { "filetype_mode", &filetype_mode },
140 { 0, 0 }
143 static const struct {
144 const char *opt_name;
145 int *opt_addr;
146 } int_options [] = {
147 { "show_backups", &show_backups },
148 { "show_dot_files", &show_dot_files },
149 { "verbose", &verbose },
150 { "mark_moves_down", &mark_moves_down },
151 { "pause_after_run", &pause_after_run },
152 { "shell_patterns", &easy_patterns },
153 { "auto_save_setup", &auto_save_setup },
154 { "auto_menu", &auto_menu },
155 { "use_internal_view", &use_internal_view },
156 { "use_internal_edit", &use_internal_edit },
157 { "clear_before_exec", &clear_before_exec },
158 { "mix_all_files", &mix_all_files },
159 { "fast_reload", &fast_reload },
160 { "fast_reload_msg_shown", &fast_reload_w },
161 { "confirm_delete", &confirm_delete },
162 { "confirm_overwrite", &confirm_overwrite },
163 { "confirm_execute", &confirm_execute },
164 { "confirm_exit", &confirm_exit },
165 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
166 { "safe_delete", &safe_delete },
167 { "mouse_repeat_rate", &mou_auto_repeat },
168 { "double_click_speed", &double_click_speed },
169 #ifndef HAVE_CHARSET
170 { "eight_bit_clean", &eight_bit_clean },
171 { "full_eight_bits", &full_eight_bits },
172 #endif /* !HAVE_CHARSET */
173 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
174 { "confirm_view_dir", &confirm_view_dir },
175 { "mouse_move_pages", &mouse_move_pages },
176 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
177 { "fast_refresh", &fast_refresh },
178 { "navigate_with_arrows", &navigate_with_arrows },
179 { "drop_menus", &drop_menus },
180 { "wrap_mode", &global_wrap_mode},
181 { "old_esc_mode", &old_esc_mode },
182 { "cd_symlinks", &cd_symlinks },
183 { "show_all_if_ambiguous", &show_all_if_ambiguous },
184 { "max_dirt_limit", &max_dirt_limit },
185 { "torben_fj_mode", &torben_fj_mode },
186 { "use_file_to_guess_type", &use_file_to_check_type },
187 { "alternate_plus_minus", &alternate_plus_minus },
188 { "only_leading_plus_minus", &only_leading_plus_minus },
189 { "show_output_starts_shell", &output_starts_shell },
190 { "panel_scroll_pages", &panel_scroll_pages },
191 { "xtree_mode", &xtree_mode },
192 { "num_history_items_recorded", &num_history_items_recorded },
193 { "file_op_compute_totals", &file_op_compute_totals },
194 #ifdef USE_VFS
195 { "vfs_timeout", &vfs_timeout },
196 #ifdef USE_NETCODE
197 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
198 { "use_netrc", &use_netrc },
199 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
200 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
201 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
202 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
203 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
204 { "fish_directory_timeout", &fish_directory_timeout },
205 #endif /* USE_NETCODE */
206 #endif /* USE_VFS */
207 #ifdef USE_INTERNAL_EDIT
208 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
209 { "editor_key_emulation", &edit_key_emulation },
210 { "editor_tab_spacing", &option_tab_spacing },
211 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
212 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
213 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
214 { "editor_fake_half_tabs", &option_fake_half_tabs },
215 { "editor_option_save_mode", &option_save_mode },
216 { "editor_option_save_position", &option_save_position },
217 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
218 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
219 { "editor_edit_confirm_save", &edit_confirm_save },
220 { "editor_syntax_highlighting", &option_syntax_highlighting },
221 { "editor_visible_tabs", &visible_tabs },
222 { "editor_visible_spaces", &visible_tws },
223 #endif /* USE_INTERNAL_EDIT */
225 { "nice_rotating_dash", &nice_rotating_dash },
226 { "horizontal_split", &horizontal_split },
227 { "mcview_remember_file_position", &mcview_remember_file_position },
228 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
229 { 0, 0 }
232 static const struct {
233 const char *opt_name;
234 char **opt_addr;
235 const char *opt_defval;
236 } str_options [] = {
237 #ifdef USE_INTERNAL_EDIT
238 { "editor_backup_extension", &option_backup_ext, "~" },
239 #endif
240 { NULL, NULL }
243 void
244 panel_save_setup (struct WPanel *panel, const char *section)
246 char buffer [BUF_TINY];
247 int i;
249 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
250 save_string (section, "reverse", buffer, profile_name);
251 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
252 save_string (section, "case_sensitive", buffer, profile_name);
253 g_snprintf (buffer, sizeof (buffer), "%d", panel->exec_first);
254 save_string (section, "exec_first", buffer, profile_name);
255 for (i = 0; sort_names [i].key; i++)
256 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
257 save_string (section, "sort_order",
258 sort_names [i].key, profile_name);
259 break;
262 for (i = 0; list_types [i].key; i++)
263 if (list_types [i].list_type == panel->list_type){
264 save_string (section, "list_mode", list_types [i].key, profile_name);
265 break;
268 save_string (section, "user_format",
269 panel->user_format, profile_name);
271 for (i = 0; i < LIST_TYPES; i++){
272 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
273 save_string (section, buffer,
274 panel->user_status_format [i], profile_name);
277 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
278 save_string (section, "user_mini_status", buffer,
279 profile_name);
282 void
283 save_layout (void)
285 char *profile;
286 int i;
287 char buffer [BUF_TINY];
289 profile = mhl_str_dir_plus_file (home_dir, PROFILE_NAME);
291 /* Save integer options */
292 for (i = 0; layout [i].opt_name; i++){
293 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
294 save_string ("Layout", layout [i].opt_name, buffer, profile);
297 g_free (profile);
300 void
301 save_configure (void)
303 char *profile;
304 int i;
306 profile = mhl_str_dir_plus_file (home_dir, PROFILE_NAME);
308 /* Save integer options */
309 for (i = 0; int_options[i].opt_name; i++)
310 set_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
312 /* Save string options */
313 for (i = 0; str_options[i].opt_name != NULL; i++)
314 set_config_string (profile, str_options[i].opt_name,
315 *str_options[i].opt_addr);
317 g_free (profile);
320 static void
321 panel_save_type (const char *section, int type)
323 int i;
325 for (i = 0; panel_types [i].opt_name; i++)
326 if (panel_types [i].opt_type == type){
327 save_string (section, "display", panel_types [i].opt_name,
328 profile_name);
329 break;
333 static void
334 save_panel_types (void)
336 int type;
338 type = get_display_type (0);
339 panel_save_type ("New Left Panel", type);
340 if (type == view_listing)
341 panel_save_setup (left_panel, left_panel->panel_name);
342 type = get_display_type (1);
343 panel_save_type ("New Right Panel", type);
344 if (type == view_listing)
345 panel_save_setup (right_panel, right_panel->panel_name);
348 void
349 save_setup (void)
351 char *profile;
353 saving_setup = 1;
354 profile = mhl_str_dir_plus_file (home_dir, PROFILE_NAME);
356 save_configure ();
358 save_layout ();
359 save_string ("Dirs", "other_dir",
360 get_other_type () == view_listing
361 ? other_panel->cwd : ".", profile);
362 if (current_panel != NULL)
363 WritePrivateProfileString ("Dirs", "current_is_left",
364 get_current_index () == 0 ? "1" : "0", profile);
365 save_hotlist ();
367 save_panelize ();
368 save_panel_types ();
369 /* directory_history_save (); */
371 #if defined(USE_VFS) && defined (USE_NETCODE)
372 WritePrivateProfileString ("Misc", "ftpfs_password",
373 ftpfs_anonymous_passwd, profile);
374 if (ftpfs_proxy_host)
375 WritePrivateProfileString ("Misc", "ftp_proxy_host",
376 ftpfs_proxy_host, profile);
377 #endif /* USE_VFS && USE_NETCODE */
379 #ifdef HAVE_CHARSET
380 save_string( "Misc", "display_codepage",
381 get_codepage_id( display_codepage ), profile_name );
382 #endif /* HAVE_CHARSET */
384 g_free (profile);
385 saving_setup = 0;
388 void
389 panel_load_setup (WPanel *panel, const char *section)
391 int i;
392 char buffer [BUF_TINY];
394 panel->reverse = load_int (section, "reverse", 0);
395 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
396 panel->exec_first = load_int (section, "exec_first", 0);
398 /* Load sort order */
399 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
400 panel->sort_type = (sortfn *) sort_name;
401 for (i = 0; sort_names [i].key; i++)
402 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
403 panel->sort_type = sort_names [i].sort_type;
404 break;
407 /* Load the listing mode */
408 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
409 panel->list_type = list_full;
410 for (i = 0; list_types [i].key; i++)
411 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
412 panel->list_type = list_types [i].list_type;
413 break;
416 /* User formats */
417 g_free (panel->user_format);
418 panel->user_format = mhl_str_dup (get_profile_string (section, "user_format",
419 DEFAULT_USER_FORMAT,
420 profile_name));
421 for (i = 0; i < LIST_TYPES; i++){
422 g_free (panel->user_status_format [i]);
423 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
424 panel->user_status_format [i] =
425 mhl_str_dup (get_profile_string (section, buffer,
426 DEFAULT_USER_FORMAT, profile_name));
429 panel->user_mini_status =
430 load_int (section, "user_mini_status", 0);
434 static void
435 load_layout (char *profile_name)
437 int i;
439 for (i = 0; layout [i].opt_name; i++)
440 *layout [i].opt_addr =
441 load_int ("Layout", layout [i].opt_name,
442 *layout [i].opt_addr);
445 static int
446 load_mode (const char *section)
448 char buffer [20];
449 int i;
451 int mode = view_listing;
453 /* Load the display mode */
454 load_string (section, "display", "listing", buffer, sizeof (buffer));
456 for (i = 0; panel_types [i].opt_name; i++)
457 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
458 mode = panel_types [i].opt_type;
459 break;
462 return mode;
465 #ifdef USE_NETCODE
466 static char *
467 do_load_string (const char *s, const char *ss, const char *def)
469 char *buffer = g_malloc (BUF_SMALL);
470 char *p;
472 load_string (s, ss, def, buffer, BUF_SMALL);
474 p = g_strdup (buffer);
475 g_free (buffer);
476 return p;
478 #endif /* !USE_NETCODE */
480 char *
481 setup_init (void)
483 char *profile;
484 char *inifile;
486 if (profile_name)
487 return profile_name;
489 profile = mhl_str_dir_plus_file (home_dir, PROFILE_NAME);
490 if (!exist_file (profile)){
491 inifile = mhl_str_dir_plus_file (mc_home, "mc.ini");
492 if (exist_file (inifile)){
493 g_free (profile);
494 profile = inifile;
495 } else
496 g_free (inifile);
499 profile_name = profile;
501 return profile;
504 void
505 load_setup (void)
507 char *profile;
508 int i;
510 profile = setup_init ();
512 /* mc.lib is common for all users, but has priority lower than
513 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
514 global_profile_name = mhl_str_dir_plus_file (mc_home, "mc.lib");
516 /* Load integer boolean options */
517 for (i = 0; int_options[i].opt_name; i++)
518 *int_options[i].opt_addr =
519 get_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
521 /* Load string options */
522 for (i = 0; str_options[i].opt_name != NULL; i++)
523 *str_options[i].opt_addr = get_config_string (profile,
524 str_options[i].opt_name, str_options[i].opt_defval);
526 load_layout (profile);
528 load_panelize ();
530 startup_left_mode = load_mode ("New Left Panel");
531 startup_right_mode = load_mode ("New Right Panel");
533 /* At least one of the panels is a listing panel */
534 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
535 startup_left_mode = view_listing;
537 if (!other_dir){
538 char *buffer;
540 buffer = (char*) g_malloc (MC_MAXPATHLEN);
541 load_string ("Dirs", "other_dir", ".", buffer,
542 MC_MAXPATHLEN);
543 if (vfs_file_is_local (buffer))
544 other_dir = buffer;
545 else
546 g_free (buffer);
549 boot_current_is_left =
550 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
552 #ifdef USE_NETCODE
553 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
554 #endif
556 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
557 sizeof (setup_color_string));
558 if (setup_color_string [0])
559 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", (char *) NULL);
561 /* The default color and the terminal dependent color */
562 load_string ("Colors", "base_color", "", setup_color_string,
563 sizeof (setup_color_string));
564 load_string ("Colors", getenv ("TERM"), "",
565 term_color_string, sizeof (term_color_string));
566 load_string ("Colors", "color_terminals", "",
567 color_terminal_string, sizeof (color_terminal_string));
569 /* Load the directory history */
570 /* directory_history_load (); */
571 /* Remove the temporal entries */
572 profile_clean_section ("Temporal:New Left Panel", profile_name);
573 profile_clean_section ("Temporal:New Right Panel", profile_name);
574 #if defined(USE_VFS) && defined (USE_NETCODE)
575 ftpfs_init_passwd ();
576 #endif /* USE_VFS && USE_NETCODE */
578 #ifdef HAVE_CHARSET
579 if ( load_codepages_list() > 0 ) {
580 char cpname[128];
581 load_string( "Misc", "display_codepage", "",
582 cpname, sizeof(cpname) );
583 if ( cpname[0] != '\0' )
584 display_codepage = get_codepage_index( cpname );
587 init_translation_table( source_codepage, display_codepage );
588 #endif /* HAVE_CHARSET */
591 #if defined(USE_VFS) && defined (USE_NETCODE)
592 char *
593 load_anon_passwd ()
595 char buffer [255];
597 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
598 if (buffer [0])
599 return g_strdup (buffer);
600 else
601 return 0;
603 #endif /* USE_VFS && USE_NETCODE */
605 void done_setup (void)
607 g_free (profile_name);
608 g_free (global_profile_name);
609 done_hotlist ();
610 done_panelize ();
611 /* directory_history_free (); */
614 static void
615 load_keys_from_section (const char *terminal, const char *profile_name)
617 char *section_name;
618 void *profile_keys;
619 char *key, *value, *valcopy;
620 int key_code;
622 if (!terminal)
623 return;
625 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
626 profile_keys = profile_init_iterator (section_name, profile_name);
627 g_free (section_name);
628 while (profile_keys){
629 profile_keys = profile_iterator_next (profile_keys, &key, &value);
631 /* copy=other causes all keys from [terminal:other] to be loaded. */
632 if (g_strcasecmp (key, "copy") == 0) {
633 load_keys_from_section (value, profile_name);
634 continue;
637 key_code = lookup_key (key);
638 if (key_code){
639 valcopy = convert_controls (value);
640 define_sequence (key_code, valcopy, MCKEY_NOACTION);
641 g_free (valcopy);
646 void load_key_defs (void)
649 * Load keys from mc.lib before ~/.mc/ini, so that the user
650 * definitions override global settings.
652 load_keys_from_section ("general", global_profile_name);
653 load_keys_from_section (getenv ("TERM"), global_profile_name);
654 load_keys_from_section ("general", profile_name);
655 load_keys_from_section (getenv ("TERM"), profile_name);
657 /* We don't want a huge database loaded in core */
658 free_profile_name (global_profile_name);