* samba/lib/util_file.c: Comment out all but fgets_slash().
[midnight-commander.git] / src / setup.c
blobe467cb6d435ba741ba85988876567fb244001105
1 /* Setup loading/saving.
2 Copyright (C) 1994 Miguel de Icaza
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <config.h>
19 #include <sys/types.h> /* Needed to include local .h files */
20 #include <sys/stat.h>
21 #include <string.h>
22 #include <stdio.h>
24 #include "global.h"
25 #include "tty.h"
26 #include "dir.h"
27 #include "panel.h"
28 #include "main.h"
29 #include "tree.h" /* xtree_mode */
30 #include "profile.h"
31 #define WANT_WIDGETS
32 #include "setup.h"
33 #include "mouse.h" /* To make view.h happy */
34 #include "view.h" /* For the externs */
35 #include "key.h" /* For the externs */
36 #include "hotlist.h" /* load/save/done hotlist */
37 #include "panelize.h" /* load/save/done panelize */
38 #include "layout.h"
39 #include "menu.h" /* menubar_visible declaration */
40 #include "win.h" /* lookup_key */
41 #include "cmd.h"
42 #include "file.h" /* safe_delete */
44 #ifdef USE_VFS
45 #include "../vfs/gc.h"
46 #endif
48 #ifdef HAVE_CHARSET
49 #include "charsets.h"
50 #endif
52 #ifdef USE_NETCODE
53 # include "../vfs/ftpfs.h"
54 #endif
56 #ifdef USE_INTERNAL_EDIT
57 # include "../edit/edit.h"
58 #endif
61 extern char *find_ignore_dirs;
63 extern int num_history_items_recorded;
65 char *profile_name; /* .mc/ini */
66 char *global_profile_name; /* mc.lib */
68 char setup_color_string [4096];
69 char term_color_string [4096];
70 char color_terminal_string [512];
72 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
73 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
74 #define save_string WritePrivateProfileString
76 int startup_left_mode;
77 int startup_right_mode;
79 /* Ugly hack to allow panel_save_setup to work as a place holder for */
80 /* default panel values */
81 int saving_setup;
83 static const struct {
84 const char *key;
85 sortfn *sort_type;
86 } sort_names [] = {
87 { "name", (sortfn *) sort_name },
88 { "extension", (sortfn *) sort_ext },
89 { "time", (sortfn *) sort_time },
90 { "atime", (sortfn *) sort_atime },
91 { "ctime", (sortfn *) sort_ctime },
92 { "size", (sortfn *) sort_size },
93 { "inode", (sortfn *) sort_inode },
94 { "unsorted", (sortfn *) unsorted },
95 { 0, 0 }
98 static const struct {
99 const char *key;
100 int list_type;
101 } list_types [] = {
102 { "full", list_full },
103 { "brief", list_brief },
104 { "long", list_long },
105 { "user", list_user },
106 { 0, 0 }
109 static const struct {
110 const char *opt_name;
111 int opt_type;
112 } panel_types [] = {
113 { "listing", view_listing },
114 { "quickview", view_quick },
115 { "info", view_info },
116 { "tree", view_tree },
117 { 0, 0 }
120 static const struct {
121 const char *opt_name;
122 int *opt_addr;
123 } layout [] = {
124 { "equal_split", &equal_split },
125 { "first_panel_size", &first_panel_size },
126 { "message_visible", &message_visible },
127 { "keybar_visible", &keybar_visible },
128 { "xterm_title", &xterm_title },
129 { "output_lines", &output_lines },
130 { "command_prompt", &command_prompt },
131 { "menubar_visible", &menubar_visible },
132 { "show_mini_info", &show_mini_info },
133 { "permission_mode", &permission_mode },
134 { "filetype_mode", &filetype_mode },
135 { 0, 0 }
138 static const struct {
139 const char *opt_name;
140 int *opt_addr;
141 } options [] = {
142 { "show_backups", &show_backups },
143 { "show_dot_files", &show_dot_files },
144 { "verbose", &verbose },
145 { "mark_moves_down", &mark_moves_down },
146 { "pause_after_run", &pause_after_run },
147 { "shell_patterns", &easy_patterns },
148 { "auto_save_setup", &auto_save_setup },
149 { "auto_menu", &auto_menu },
150 { "use_internal_view", &use_internal_view },
151 { "use_internal_edit", &use_internal_edit },
152 { "clear_before_exec", &clear_before_exec },
153 { "mix_all_files", &mix_all_files },
154 { "fast_reload", &fast_reload },
155 { "fast_reload_msg_shown", &fast_reload_w },
156 { "confirm_delete", &confirm_delete },
157 { "confirm_overwrite", &confirm_overwrite },
158 { "confirm_execute", &confirm_execute },
159 { "confirm_exit", &confirm_exit },
160 { "safe_delete", &safe_delete },
161 { "mouse_repeat_rate", &mou_auto_repeat },
162 { "double_click_speed", &double_click_speed },
163 #ifndef HAVE_CHARSET
164 { "eight_bit_clean", &eight_bit_clean },
165 { "full_eight_bits", &full_eight_bits },
166 #endif /* !HAVE_CHARSET */
167 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
168 { "confirm_view_dir", &confirm_view_dir },
169 { "mouse_move_pages", &mouse_move_pages },
170 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
171 { "fast_refresh", &fast_refresh },
172 { "navigate_with_arrows", &navigate_with_arrows },
173 { "drop_menus", &drop_menus },
174 { "wrap_mode", &global_wrap_mode},
175 { "old_esc_mode", &old_esc_mode },
176 { "cd_symlinks", &cd_symlinks },
177 { "show_all_if_ambiguous", &show_all_if_ambiguous },
178 { "have_fast_cpu", &have_fast_cpu },
179 { "max_dirt_limit", &max_dirt_limit },
180 { "torben_fj_mode", &torben_fj_mode },
181 { "use_file_to_guess_type", &use_file_to_check_type },
182 { "alternate_plus_minus", &alternate_plus_minus },
183 { "only_leading_plus_minus", &only_leading_plus_minus },
184 { "show_output_starts_shell", &output_starts_shell },
185 { "panel_scroll_pages", &panel_scroll_pages },
186 { "xtree_mode", &xtree_mode },
187 { "num_history_items_recorded", &num_history_items_recorded },
188 { "file_op_compute_totals", &file_op_compute_totals },
189 #ifdef USE_VFS
190 { "vfs_timeout", &vfs_timeout },
191 #ifdef USE_NETCODE
192 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
193 { "use_netrc", &use_netrc },
194 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
195 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
196 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
197 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
198 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
199 #endif /* USE_NETCODE */
200 #endif /* USE_VFS */
201 #ifdef USE_INTERNAL_EDIT
202 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
203 { "editor_key_emulation", &edit_key_emulation },
204 { "editor_tab_spacing", &option_tab_spacing },
205 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
206 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
207 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
208 { "editor_fake_half_tabs", &option_fake_half_tabs },
209 { "editor_option_save_mode", &option_save_mode },
210 { "editor_option_save_position", &option_save_position },
211 { "editor_option_backup_ext_int", &option_backup_ext_int },
212 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
213 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
214 { "editor_edit_confirm_save", &edit_confirm_save },
215 { "editor_syntax_highlighting", &option_syntax_highlighting },
216 #endif /* USE_INTERNAL_EDIT */
218 { "nice_rotating_dash", &nice_rotating_dash },
219 { "horizontal_split", &horizontal_split },
220 { 0, 0 }
223 void
224 panel_save_setup (struct WPanel *panel, const char *section)
226 char buffer [BUF_TINY];
227 int i;
229 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
230 save_string (section, "reverse", buffer, profile_name);
231 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
232 save_string (section, "case_sensitive", buffer, profile_name);
233 for (i = 0; sort_names [i].key; i++)
234 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
235 save_string (section, "sort_order",
236 sort_names [i].key, profile_name);
237 break;
240 for (i = 0; list_types [i].key; i++)
241 if (list_types [i].list_type == panel->list_type){
242 save_string (section, "list_mode", list_types [i].key, profile_name);
243 break;
246 save_string (section, "user_format",
247 panel->user_format, profile_name);
249 for (i = 0; i < LIST_TYPES; i++){
250 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
251 save_string (section, buffer,
252 panel->user_status_format [i], profile_name);
255 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
256 save_string (section, "user_mini_status", buffer,
257 profile_name);
260 void
261 save_layout (void)
263 char *profile;
264 int i;
265 char buffer [BUF_TINY];
267 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
269 /* Save integer options */
270 for (i = 0; layout [i].opt_name; i++){
271 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
272 save_string ("Layout", layout [i].opt_name, buffer, profile);
275 g_free (profile);
278 void
279 save_configure (void)
281 char *profile;
282 int i;
284 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
286 /* Save integer options */
287 for (i = 0; options [i].opt_name; i++)
288 set_int (profile, options [i].opt_name, *options [i].opt_addr);
290 g_free (profile);
293 static void
294 panel_save_type (const char *section, int type)
296 int i;
298 for (i = 0; panel_types [i].opt_name; i++)
299 if (panel_types [i].opt_type == type){
300 save_string (section, "display", panel_types [i].opt_name,
301 profile_name);
302 break;
306 static void
307 save_panel_types (void)
309 int type;
311 type = get_display_type (0);
312 panel_save_type ("New Left Panel", type);
313 if (type == view_listing)
314 panel_save_setup (left_panel, left_panel->panel_name);
315 type = get_display_type (1);
316 panel_save_type ("New Right Panel", type);
317 if (type == view_listing)
318 panel_save_setup (right_panel, right_panel->panel_name);
321 void
322 save_setup (void)
324 char *profile;
326 saving_setup = 1;
327 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
329 save_configure ();
331 save_layout ();
332 save_string ("Dirs", "other_dir",
333 get_other_type () == view_listing
334 ? other_panel->cwd : ".", profile);
335 if (current_panel != NULL)
336 WritePrivateProfileString ("Dirs", "current_is_left",
337 get_current_index () == 0 ? "1" : "0", profile);
338 save_hotlist ();
340 save_panelize ();
341 save_panel_types ();
342 /* directory_history_save (); */
344 #if defined(USE_VFS) && defined (USE_NETCODE)
345 WritePrivateProfileString ("Misc", "ftpfs_password",
346 ftpfs_anonymous_passwd, profile);
347 if (ftpfs_proxy_host)
348 WritePrivateProfileString ("Misc", "ftp_proxy_host",
349 ftpfs_proxy_host, profile);
350 #endif /* USE_VFS && USE_NETCODE */
352 #ifdef HAVE_CHARSET
353 save_string( "Misc", "display_codepage",
354 get_codepage_id( display_codepage ), profile_name );
355 #endif /* HAVE_CHARSET */
357 g_free (profile);
358 saving_setup = 0;
361 void
362 panel_load_setup (WPanel *panel, const char *section)
364 int i;
365 char buffer [BUF_TINY];
367 panel->reverse = load_int (section, "reverse", 0);
368 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
370 /* Load sort order */
371 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
372 panel->sort_type = (sortfn *) sort_name;
373 for (i = 0; sort_names [i].key; i++)
374 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
375 panel->sort_type = sort_names [i].sort_type;
376 break;
379 /* Load the listing mode */
380 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
381 panel->list_type = list_full;
382 for (i = 0; list_types [i].key; i++)
383 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
384 panel->list_type = list_types [i].list_type;
385 break;
388 /* User formats */
389 g_free (panel->user_format);
390 panel->user_format = g_strdup (get_profile_string (section, "user_format",
391 DEFAULT_USER_FORMAT,
392 profile_name));
393 for (i = 0; i < LIST_TYPES; i++){
394 g_free (panel->user_status_format [i]);
395 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
396 panel->user_status_format [i] =
397 g_strdup (get_profile_string (section, buffer,
398 DEFAULT_USER_FORMAT, profile_name));
401 panel->user_mini_status =
402 load_int (section, "user_mini_status", 0);
406 static void
407 load_layout (char *profile_name)
409 int i;
411 for (i = 0; layout [i].opt_name; i++)
412 *layout [i].opt_addr =
413 load_int ("Layout", layout [i].opt_name,
414 *layout [i].opt_addr);
417 static int
418 load_mode (const char *section)
420 char buffer [20];
421 int i;
423 int mode = view_listing;
425 /* Load the display mode */
426 load_string (section, "display", "listing", buffer, sizeof (buffer));
428 for (i = 0; panel_types [i].opt_name; i++)
429 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
430 mode = panel_types [i].opt_type;
431 break;
434 return mode;
437 #ifdef USE_NETCODE
438 static char *
439 do_load_string (const char *s, const char *ss, const char *def)
441 char *buffer = g_malloc (BUF_SMALL);
442 char *p;
444 load_string (s, ss, def, buffer, BUF_SMALL);
446 p = g_strdup (buffer);
447 g_free (buffer);
448 return p;
450 #endif /* !USE_NETCODE */
452 char *
453 setup_init (void)
455 char *profile;
456 char *inifile;
458 if (profile_name)
459 return profile_name;
461 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
462 if (!exist_file (profile)){
463 inifile = concat_dir_and_file (mc_home, "mc.ini");
464 if (exist_file (inifile)){
465 g_free (profile);
466 profile = inifile;
467 } else
468 g_free (inifile);
471 profile_name = profile;
473 return profile;
476 void
477 load_setup (void)
479 char *profile;
480 int i;
482 profile = setup_init ();
484 /* mc.lib is common for all users, but has priority lower than
485 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
486 global_profile_name = concat_dir_and_file (mc_home, "mc.lib");
488 /* Load integer boolean options */
489 for (i = 0; options [i].opt_name; i++)
490 *options [i].opt_addr =
491 get_int (profile, options [i].opt_name, *options [i].opt_addr);
493 load_layout (profile);
495 load_panelize ();
497 startup_left_mode = load_mode ("New Left Panel");
498 startup_right_mode = load_mode ("New Right Panel");
500 /* At least one of the panels is a listing panel */
501 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
502 startup_left_mode = view_listing;
504 if (!other_dir){
505 char *buffer;
507 buffer = (char*) g_malloc (MC_MAXPATHLEN);
508 load_string ("Dirs", "other_dir", ".", buffer,
509 MC_MAXPATHLEN);
510 if (vfs_file_is_local (buffer))
511 other_dir = buffer;
512 else
513 g_free (buffer);
516 boot_current_is_left =
517 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
519 #ifdef USE_NETCODE
520 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
521 #endif
523 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
524 sizeof (setup_color_string));
525 if (setup_color_string [0])
526 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", (char *) NULL);
528 /* The default color and the terminal dependent color */
529 load_string ("Colors", "base_color", "", setup_color_string,
530 sizeof (setup_color_string));
531 load_string ("Colors", getenv ("TERM"), "",
532 term_color_string, sizeof (term_color_string));
533 load_string ("Colors", "color_terminals", "",
534 color_terminal_string, sizeof (color_terminal_string));
536 /* Load the directory history */
537 /* directory_history_load (); */
538 /* Remove the temporal entries */
539 profile_clean_section ("Temporal:New Left Panel", profile_name);
540 profile_clean_section ("Temporal:New Right Panel", profile_name);
541 #if defined(USE_VFS) && defined (USE_NETCODE)
542 ftpfs_init_passwd ();
543 #endif /* USE_VFS && USE_NETCODE */
545 #ifdef HAVE_CHARSET
546 if ( load_codepages_list() > 0 ) {
547 char cpname[128];
548 load_string( "Misc", "display_codepage", "",
549 cpname, sizeof(cpname) );
550 if ( cpname[0] != '\0' )
551 display_codepage = get_codepage_index( cpname );
554 init_translation_table( source_codepage, display_codepage );
555 #endif /* HAVE_CHARSET */
558 #if defined(USE_VFS) && defined (USE_NETCODE)
559 char *
560 load_anon_passwd ()
562 char buffer [255];
564 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
565 if (buffer [0])
566 return g_strdup (buffer);
567 else
568 return 0;
570 #endif /* USE_VFS && USE_NETCODE */
572 void done_setup (void)
574 g_free (profile_name);
575 g_free (global_profile_name);
576 done_hotlist ();
577 done_panelize ();
578 /* directory_history_free (); */
581 static void
582 load_keys_from_section (const char *terminal, const char *profile_name)
584 char *section_name;
585 void *profile_keys;
586 char *key, *value, *valcopy;
587 int key_code;
589 if (!terminal)
590 return;
592 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
593 profile_keys = profile_init_iterator (section_name, profile_name);
594 g_free (section_name);
595 while (profile_keys){
596 profile_keys = profile_iterator_next (profile_keys, &key, &value);
598 /* copy=other causes all keys from [terminal:other] to be loaded. */
599 if (g_strcasecmp (key, "copy") == 0) {
600 load_keys_from_section (value, profile_name);
601 continue;
604 key_code = lookup_key (key);
605 if (key_code){
606 valcopy = convert_controls (value);
607 define_sequence (key_code, valcopy, MCKEY_NOACTION);
608 g_free (valcopy);
613 void load_key_defs (void)
616 * Load keys from mc.lib before ~/.mc/ini, so that the user
617 * definitions override global settings.
619 load_keys_from_section ("general", global_profile_name);
620 load_keys_from_section (getenv ("TERM"), global_profile_name);
621 load_keys_from_section ("general", profile_name);
622 load_keys_from_section (getenv ("TERM"), profile_name);
624 /* We don't want a huge database loaded in core */
625 free_profile_name (global_profile_name);