replaced calls to g_strdup() by mhl_str_dup()
[midnight-commander.git] / src / setup.c
blob4e044acd849fd66be021a162d2140f66e8f2afb1
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/string.h>
28 #include "global.h"
29 #include "tty.h"
30 #include "dir.h"
31 #include "panel.h"
32 #include "main.h"
33 #include "tree.h" /* xtree_mode */
34 #include "profile.h"
35 #include "setup.h"
36 #include "mouse.h" /* To make view.h happy */
37 #include "view.h" /* For the externs */
38 #include "key.h" /* For the externs */
39 #include "hotlist.h" /* load/save/done hotlist */
40 #include "panelize.h" /* load/save/done panelize */
41 #include "layout.h"
42 #include "menu.h" /* menubar_visible declaration */
43 #include "win.h" /* lookup_key */
44 #include "cmd.h"
45 #include "file.h" /* safe_delete */
47 #ifdef USE_VFS
48 #include "../vfs/gc.h"
49 #endif
51 #ifdef HAVE_CHARSET
52 #include "charsets.h"
53 #endif
55 #ifdef USE_NETCODE
56 # include "../vfs/ftpfs.h"
57 # include "../vfs/fish.h"
58 #endif
60 #ifdef USE_INTERNAL_EDIT
61 # include "../edit/edit.h"
62 #endif
65 extern char *find_ignore_dirs;
67 extern int num_history_items_recorded;
69 char *profile_name; /* .mc/ini */
70 char *global_profile_name; /* mc.lib */
72 char setup_color_string [4096];
73 char term_color_string [4096];
74 char color_terminal_string [512];
76 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
77 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
78 #define save_string WritePrivateProfileString
80 int startup_left_mode;
81 int startup_right_mode;
83 /* Ugly hack to allow panel_save_setup to work as a place holder for */
84 /* default panel values */
85 int saving_setup;
87 static const struct {
88 const char *key;
89 sortfn *sort_type;
90 } sort_names [] = {
91 { "name", (sortfn *) sort_name },
92 { "extension", (sortfn *) sort_ext },
93 { "time", (sortfn *) sort_time },
94 { "atime", (sortfn *) sort_atime },
95 { "ctime", (sortfn *) sort_ctime },
96 { "size", (sortfn *) sort_size },
97 { "inode", (sortfn *) sort_inode },
98 { "unsorted", (sortfn *) unsorted },
99 { 0, 0 }
102 static const struct {
103 const char *key;
104 int list_type;
105 } list_types [] = {
106 { "full", list_full },
107 { "brief", list_brief },
108 { "long", list_long },
109 { "user", list_user },
110 { 0, 0 }
113 static const struct {
114 const char *opt_name;
115 int opt_type;
116 } panel_types [] = {
117 { "listing", view_listing },
118 { "quickview", view_quick },
119 { "info", view_info },
120 { "tree", view_tree },
121 { 0, 0 }
124 static const struct {
125 const char *opt_name;
126 int *opt_addr;
127 } layout [] = {
128 { "equal_split", &equal_split },
129 { "first_panel_size", &first_panel_size },
130 { "message_visible", &message_visible },
131 { "keybar_visible", &keybar_visible },
132 { "xterm_title", &xterm_title },
133 { "output_lines", &output_lines },
134 { "command_prompt", &command_prompt },
135 { "menubar_visible", &menubar_visible },
136 { "show_mini_info", &show_mini_info },
137 { "permission_mode", &permission_mode },
138 { "filetype_mode", &filetype_mode },
139 { 0, 0 }
142 static const struct {
143 const char *opt_name;
144 int *opt_addr;
145 } int_options [] = {
146 { "show_backups", &show_backups },
147 { "show_dot_files", &show_dot_files },
148 { "verbose", &verbose },
149 { "mark_moves_down", &mark_moves_down },
150 { "pause_after_run", &pause_after_run },
151 { "shell_patterns", &easy_patterns },
152 { "auto_save_setup", &auto_save_setup },
153 { "auto_menu", &auto_menu },
154 { "use_internal_view", &use_internal_view },
155 { "use_internal_edit", &use_internal_edit },
156 { "clear_before_exec", &clear_before_exec },
157 { "mix_all_files", &mix_all_files },
158 { "fast_reload", &fast_reload },
159 { "fast_reload_msg_shown", &fast_reload_w },
160 { "confirm_delete", &confirm_delete },
161 { "confirm_overwrite", &confirm_overwrite },
162 { "confirm_execute", &confirm_execute },
163 { "confirm_exit", &confirm_exit },
164 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
165 { "safe_delete", &safe_delete },
166 { "mouse_repeat_rate", &mou_auto_repeat },
167 { "double_click_speed", &double_click_speed },
168 #ifndef HAVE_CHARSET
169 { "eight_bit_clean", &eight_bit_clean },
170 { "full_eight_bits", &full_eight_bits },
171 #endif /* !HAVE_CHARSET */
172 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
173 { "confirm_view_dir", &confirm_view_dir },
174 { "mouse_move_pages", &mouse_move_pages },
175 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
176 { "fast_refresh", &fast_refresh },
177 { "navigate_with_arrows", &navigate_with_arrows },
178 { "drop_menus", &drop_menus },
179 { "wrap_mode", &global_wrap_mode},
180 { "old_esc_mode", &old_esc_mode },
181 { "cd_symlinks", &cd_symlinks },
182 { "show_all_if_ambiguous", &show_all_if_ambiguous },
183 { "max_dirt_limit", &max_dirt_limit },
184 { "torben_fj_mode", &torben_fj_mode },
185 { "use_file_to_guess_type", &use_file_to_check_type },
186 { "alternate_plus_minus", &alternate_plus_minus },
187 { "only_leading_plus_minus", &only_leading_plus_minus },
188 { "show_output_starts_shell", &output_starts_shell },
189 { "panel_scroll_pages", &panel_scroll_pages },
190 { "xtree_mode", &xtree_mode },
191 { "num_history_items_recorded", &num_history_items_recorded },
192 { "file_op_compute_totals", &file_op_compute_totals },
193 #ifdef USE_VFS
194 { "vfs_timeout", &vfs_timeout },
195 #ifdef USE_NETCODE
196 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
197 { "use_netrc", &use_netrc },
198 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
199 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
200 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
201 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
202 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
203 { "fish_directory_timeout", &fish_directory_timeout },
204 #endif /* USE_NETCODE */
205 #endif /* USE_VFS */
206 #ifdef USE_INTERNAL_EDIT
207 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
208 { "editor_key_emulation", &edit_key_emulation },
209 { "editor_tab_spacing", &option_tab_spacing },
210 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
211 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
212 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
213 { "editor_fake_half_tabs", &option_fake_half_tabs },
214 { "editor_option_save_mode", &option_save_mode },
215 { "editor_option_save_position", &option_save_position },
216 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
217 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
218 { "editor_edit_confirm_save", &edit_confirm_save },
219 { "editor_syntax_highlighting", &option_syntax_highlighting },
220 { "editor_visible_tabs", &visible_tabs },
221 { "editor_visible_spaces", &visible_tws },
222 #endif /* USE_INTERNAL_EDIT */
224 { "nice_rotating_dash", &nice_rotating_dash },
225 { "horizontal_split", &horizontal_split },
226 { "mcview_remember_file_position", &mcview_remember_file_position },
227 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
228 { 0, 0 }
231 static const struct {
232 const char *opt_name;
233 char **opt_addr;
234 const char *opt_defval;
235 } str_options [] = {
236 #ifdef USE_INTERNAL_EDIT
237 { "editor_backup_extension", &option_backup_ext, "~" },
238 #endif
239 { NULL, NULL }
242 void
243 panel_save_setup (struct WPanel *panel, const char *section)
245 char buffer [BUF_TINY];
246 int i;
248 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
249 save_string (section, "reverse", buffer, profile_name);
250 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
251 save_string (section, "case_sensitive", buffer, profile_name);
252 g_snprintf (buffer, sizeof (buffer), "%d", panel->exec_first);
253 save_string (section, "exec_first", buffer, profile_name);
254 for (i = 0; sort_names [i].key; i++)
255 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
256 save_string (section, "sort_order",
257 sort_names [i].key, profile_name);
258 break;
261 for (i = 0; list_types [i].key; i++)
262 if (list_types [i].list_type == panel->list_type){
263 save_string (section, "list_mode", list_types [i].key, profile_name);
264 break;
267 save_string (section, "user_format",
268 panel->user_format, profile_name);
270 for (i = 0; i < LIST_TYPES; i++){
271 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
272 save_string (section, buffer,
273 panel->user_status_format [i], profile_name);
276 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
277 save_string (section, "user_mini_status", buffer,
278 profile_name);
281 void
282 save_layout (void)
284 char *profile;
285 int i;
286 char buffer [BUF_TINY];
288 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
290 /* Save integer options */
291 for (i = 0; layout [i].opt_name; i++){
292 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
293 save_string ("Layout", layout [i].opt_name, buffer, profile);
296 g_free (profile);
299 void
300 save_configure (void)
302 char *profile;
303 int i;
305 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
307 /* Save integer options */
308 for (i = 0; int_options[i].opt_name; i++)
309 set_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
311 /* Save string options */
312 for (i = 0; str_options[i].opt_name != NULL; i++)
313 set_config_string (profile, str_options[i].opt_name,
314 *str_options[i].opt_addr);
316 g_free (profile);
319 static void
320 panel_save_type (const char *section, int type)
322 int i;
324 for (i = 0; panel_types [i].opt_name; i++)
325 if (panel_types [i].opt_type == type){
326 save_string (section, "display", panel_types [i].opt_name,
327 profile_name);
328 break;
332 static void
333 save_panel_types (void)
335 int type;
337 type = get_display_type (0);
338 panel_save_type ("New Left Panel", type);
339 if (type == view_listing)
340 panel_save_setup (left_panel, left_panel->panel_name);
341 type = get_display_type (1);
342 panel_save_type ("New Right Panel", type);
343 if (type == view_listing)
344 panel_save_setup (right_panel, right_panel->panel_name);
347 void
348 save_setup (void)
350 char *profile;
352 saving_setup = 1;
353 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
355 save_configure ();
357 save_layout ();
358 save_string ("Dirs", "other_dir",
359 get_other_type () == view_listing
360 ? other_panel->cwd : ".", profile);
361 if (current_panel != NULL)
362 WritePrivateProfileString ("Dirs", "current_is_left",
363 get_current_index () == 0 ? "1" : "0", profile);
364 save_hotlist ();
366 save_panelize ();
367 save_panel_types ();
368 /* directory_history_save (); */
370 #if defined(USE_VFS) && defined (USE_NETCODE)
371 WritePrivateProfileString ("Misc", "ftpfs_password",
372 ftpfs_anonymous_passwd, profile);
373 if (ftpfs_proxy_host)
374 WritePrivateProfileString ("Misc", "ftp_proxy_host",
375 ftpfs_proxy_host, profile);
376 #endif /* USE_VFS && USE_NETCODE */
378 #ifdef HAVE_CHARSET
379 save_string( "Misc", "display_codepage",
380 get_codepage_id( display_codepage ), profile_name );
381 #endif /* HAVE_CHARSET */
383 g_free (profile);
384 saving_setup = 0;
387 void
388 panel_load_setup (WPanel *panel, const char *section)
390 int i;
391 char buffer [BUF_TINY];
393 panel->reverse = load_int (section, "reverse", 0);
394 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
395 panel->exec_first = load_int (section, "exec_first", 0);
397 /* Load sort order */
398 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
399 panel->sort_type = (sortfn *) sort_name;
400 for (i = 0; sort_names [i].key; i++)
401 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
402 panel->sort_type = sort_names [i].sort_type;
403 break;
406 /* Load the listing mode */
407 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
408 panel->list_type = list_full;
409 for (i = 0; list_types [i].key; i++)
410 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
411 panel->list_type = list_types [i].list_type;
412 break;
415 /* User formats */
416 g_free (panel->user_format);
417 panel->user_format = mhl_str_dup (get_profile_string (section, "user_format",
418 DEFAULT_USER_FORMAT,
419 profile_name));
420 for (i = 0; i < LIST_TYPES; i++){
421 g_free (panel->user_status_format [i]);
422 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
423 panel->user_status_format [i] =
424 mhl_str_dup (get_profile_string (section, buffer,
425 DEFAULT_USER_FORMAT, profile_name));
428 panel->user_mini_status =
429 load_int (section, "user_mini_status", 0);
433 static void
434 load_layout (char *profile_name)
436 int i;
438 for (i = 0; layout [i].opt_name; i++)
439 *layout [i].opt_addr =
440 load_int ("Layout", layout [i].opt_name,
441 *layout [i].opt_addr);
444 static int
445 load_mode (const char *section)
447 char buffer [20];
448 int i;
450 int mode = view_listing;
452 /* Load the display mode */
453 load_string (section, "display", "listing", buffer, sizeof (buffer));
455 for (i = 0; panel_types [i].opt_name; i++)
456 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
457 mode = panel_types [i].opt_type;
458 break;
461 return mode;
464 #ifdef USE_NETCODE
465 static char *
466 do_load_string (const char *s, const char *ss, const char *def)
468 char *buffer = g_malloc (BUF_SMALL);
469 char *p;
471 load_string (s, ss, def, buffer, BUF_SMALL);
473 p = mhl_str_dup (buffer);
474 g_free (buffer);
475 return p;
477 #endif /* !USE_NETCODE */
479 char *
480 setup_init (void)
482 char *profile;
483 char *inifile;
485 if (profile_name)
486 return profile_name;
488 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
489 if (!exist_file (profile)){
490 inifile = concat_dir_and_file (mc_home, "mc.ini");
491 if (exist_file (inifile)){
492 g_free (profile);
493 profile = inifile;
494 } else
495 g_free (inifile);
498 profile_name = profile;
500 return profile;
503 void
504 load_setup (void)
506 char *profile;
507 int i;
509 profile = setup_init ();
511 /* mc.lib is common for all users, but has priority lower than
512 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
513 global_profile_name = concat_dir_and_file (mc_home, "mc.lib");
515 /* Load integer boolean options */
516 for (i = 0; int_options[i].opt_name; i++)
517 *int_options[i].opt_addr =
518 get_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
520 /* Load string options */
521 for (i = 0; str_options[i].opt_name != NULL; i++)
522 *str_options[i].opt_addr = get_config_string (profile,
523 str_options[i].opt_name, str_options[i].opt_defval);
525 load_layout (profile);
527 load_panelize ();
529 startup_left_mode = load_mode ("New Left Panel");
530 startup_right_mode = load_mode ("New Right Panel");
532 /* At least one of the panels is a listing panel */
533 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
534 startup_left_mode = view_listing;
536 if (!other_dir){
537 char *buffer;
539 buffer = (char*) g_malloc (MC_MAXPATHLEN);
540 load_string ("Dirs", "other_dir", ".", buffer,
541 MC_MAXPATHLEN);
542 if (vfs_file_is_local (buffer))
543 other_dir = buffer;
544 else
545 g_free (buffer);
548 boot_current_is_left =
549 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
551 #ifdef USE_NETCODE
552 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
553 #endif
555 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
556 sizeof (setup_color_string));
557 if (setup_color_string [0])
558 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", (char *) NULL);
560 /* The default color and the terminal dependent color */
561 load_string ("Colors", "base_color", "", setup_color_string,
562 sizeof (setup_color_string));
563 load_string ("Colors", getenv ("TERM"), "",
564 term_color_string, sizeof (term_color_string));
565 load_string ("Colors", "color_terminals", "",
566 color_terminal_string, sizeof (color_terminal_string));
568 /* Load the directory history */
569 /* directory_history_load (); */
570 /* Remove the temporal entries */
571 profile_clean_section ("Temporal:New Left Panel", profile_name);
572 profile_clean_section ("Temporal:New Right Panel", profile_name);
573 #if defined(USE_VFS) && defined (USE_NETCODE)
574 ftpfs_init_passwd ();
575 #endif /* USE_VFS && USE_NETCODE */
577 #ifdef HAVE_CHARSET
578 if ( load_codepages_list() > 0 ) {
579 char cpname[128];
580 load_string( "Misc", "display_codepage", "",
581 cpname, sizeof(cpname) );
582 if ( cpname[0] != '\0' )
583 display_codepage = get_codepage_index( cpname );
586 init_translation_table( source_codepage, display_codepage );
587 #endif /* HAVE_CHARSET */
590 #if defined(USE_VFS) && defined (USE_NETCODE)
591 char *
592 load_anon_passwd ()
594 char buffer [255];
596 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
597 if (buffer [0])
598 return mhl_str_dup (buffer);
599 else
600 return 0;
602 #endif /* USE_VFS && USE_NETCODE */
604 void done_setup (void)
606 g_free (profile_name);
607 g_free (global_profile_name);
608 done_hotlist ();
609 done_panelize ();
610 /* directory_history_free (); */
613 static void
614 load_keys_from_section (const char *terminal, const char *profile_name)
616 char *section_name;
617 void *profile_keys;
618 char *key, *value, *valcopy;
619 int key_code;
621 if (!terminal)
622 return;
624 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
625 profile_keys = profile_init_iterator (section_name, profile_name);
626 g_free (section_name);
627 while (profile_keys){
628 profile_keys = profile_iterator_next (profile_keys, &key, &value);
630 /* copy=other causes all keys from [terminal:other] to be loaded. */
631 if (g_strcasecmp (key, "copy") == 0) {
632 load_keys_from_section (value, profile_name);
633 continue;
636 key_code = lookup_key (key);
637 if (key_code){
638 valcopy = convert_controls (value);
639 define_sequence (key_code, valcopy, MCKEY_NOACTION);
640 g_free (valcopy);
645 void load_key_defs (void)
648 * Load keys from mc.lib before ~/.mc/ini, so that the user
649 * definitions override global settings.
651 load_keys_from_section ("general", global_profile_name);
652 load_keys_from_section (getenv ("TERM"), global_profile_name);
653 load_keys_from_section ("general", profile_name);
654 load_keys_from_section (getenv ("TERM"), profile_name);
656 /* We don't want a huge database loaded in core */
657 free_profile_name (global_profile_name);