Committed some stilistic changes to the italian manual pages.
[midnight-commander.git] / src / main.c
blob262f35a9f519b10099c1a8f9ce73290d1bcfe6bd
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997 The Free Software Foundation
4 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
5 1994, 1995 Janne Kukonlehto
6 1997 Norbert Warmuth
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <locale.h>
25 #ifdef NATIVE_WIN32
26 # include <windows.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
34 #include <sys/stat.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
40 #include <errno.h>
41 #include <ctype.h>
42 #include <signal.h>
44 /* Program include files */
45 #include "global.h"
46 #include "tty.h"
47 #include "dir.h"
48 #include "color.h"
49 #include "dialog.h"
50 #include "menu.h"
51 #include "panel.h"
52 #include "main.h"
53 #include "win.h"
54 #include "mouse.h"
55 #include "option.h"
56 #include "tree.h"
57 #include "cons.saver.h"
58 #include "subshell.h"
59 #include "key.h" /* For init_key() and mi_getch() */
60 #include "setup.h" /* save_setup() */
61 #include "profile.h" /* free_profiles() */
62 #include "boxes.h" /* sort_box() */
63 #include "layout.h"
64 #include "cmd.h" /* Normal commands */
65 #include "hotlist.h"
66 #include "panelize.h"
67 #include "learn.h" /* learn_keys() */
68 #include "listmode.h"
69 #include "background.h"
70 #include "ext.h" /* For flush_extension_file() */
72 /* Listbox for the command history feature */
73 #include "widget.h"
74 #include "command.h"
75 #include "wtools.h"
76 #include "complete.h" /* For the free_completion */
78 #include "chmod.h"
79 #include "chown.h"
81 #ifdef NATIVE_WIN32
82 # include "drive.h"
83 #endif
85 #ifdef WITH_SMBFS
86 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
87 #endif
89 #ifdef USE_INTERNAL_EDIT
90 # include "../edit/edit.h"
91 #endif
93 #ifdef HAVE_CHARSET
94 #include "charsets.h"
95 #endif /* HAVE_CHARSET */
97 #include "popt.h"
99 /* When the modes are active, left_panel, right_panel and tree_panel */
100 /* Point to a proper data structure. You should check with the functions */
101 /* get_current_type and get_other_type the types of the panels before using */
102 /* This pointer variables */
104 /* The structures for the panels */
105 WPanel *left_panel;
106 WPanel *right_panel;
108 /* The pointer to the tree */
109 WTree *the_tree;
111 /* The Menubar */
112 struct WMenu *the_menubar;
114 /* Pointers to the selected and unselected panel */
115 WPanel *current_panel = NULL;
117 /* Set if the command is being run from the "Right" menu */
118 int is_right;
120 /* Set when main loop should be terminated */
121 volatile int quit = 0;
123 /* Set if you want the possible completions dialog for the first time */
124 int show_all_if_ambiguous = 0;
126 /* Set when cd symlink following is desirable (bash mode) */
127 int cd_symlinks = 1;
129 /* If set then dialogs just clean the screen when refreshing, else */
130 /* they do a complete refresh, refreshing all the parts of the program */
131 int fast_refresh = 0;
133 /* If true, marking a files moves the cursor down */
134 int mark_moves_down = 1;
136 /* If true, at startup the user-menu is invoked */
137 int auto_menu = 0;
139 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
140 and M-- and keypad + / - */
141 int alternate_plus_minus = 0;
143 /* If true, then the +, - and \ keys have their special meaning only if the
144 * command line is emtpy, otherwise they behave like regular letters
146 int only_leading_plus_minus = 1;
148 /* If true, after executing a command, wait for a keystroke */
149 enum { pause_never, pause_on_dumb_terminals, pause_always };
151 int pause_after_run = pause_on_dumb_terminals;
153 /* It true saves the setup when quitting */
154 int auto_save_setup = 1;
156 #ifndef HAVE_CHARSET
157 /* If true, allow characters in the range 160-255 */
158 int eight_bit_clean = 1;
161 * If true, also allow characters in the range 128-159.
162 * This is reported to break on many terminals (xterm, qansi-m).
164 int full_eight_bits = 0;
165 #endif /* !HAVE_CHARSET */
167 /* If true use the internal viewer */
168 int use_internal_view = 1;
170 /* Have we shown the fast-reload warning in the past? */
171 int fast_reload_w = 0;
173 /* Move page/item? When clicking on the top or bottom of a panel */
174 int mouse_move_pages = 1;
176 /* If true: l&r arrows are used to chdir if the input line is empty */
177 int navigate_with_arrows = 0;
179 /* If true use +, -, | for line drawing */
180 int force_ugly_line_drawing = 0;
182 /* If true message "The shell is already running a command" never */
183 int force_subshell_execution = 0;
185 /* If true program softkeys (HP terminals only) on startup and after every
186 command ran in the subshell to the description found in the termcap/terminfo
187 database */
188 int reset_hp_softkeys = 0;
190 /* The prompt */
191 char *prompt = 0;
193 /* The widget where we draw the prompt */
194 WLabel *the_prompt;
196 /* The hint bar */
197 WLabel *the_hint;
199 /* The button bar */
200 WButtonBar *the_bar;
202 /* For slow terminals */
203 int slow_terminal = 0;
205 /* Mouse type: GPM, xterm or none */
206 Mouse_Type use_mouse_p = MOUSE_NONE;
208 /* If true, assume we are running on an xterm terminal */
209 static int force_xterm = 0;
211 /* Controls screen clearing before an exec */
212 int clear_before_exec = 1;
214 /* Asks for confirmation before deleting a file */
215 int confirm_delete = 1;
217 /* Asks for confirmation before overwriting a file */
218 int confirm_overwrite = 1;
220 /* Asks for confirmation before executing a program by pressing enter */
221 int confirm_execute = 0;
223 /* Asks for confirmation before leaving the program */
224 int confirm_exit = 1;
226 /* Asks for confirmation when using F3 to view a directory and there
227 are tagged files */
228 int confirm_view_dir = 0;
230 /* This flag indicates if the pull down menus by default drop down */
231 int drop_menus = 0;
233 /* The dialog handle for the main program */
234 Dlg_head *midnight_dlg;
236 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
237 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
238 int update_prompt = 0;
240 #if 0
241 /* The name which was used to invoke mc */
242 char *program_name;
243 #endif
245 /* The home directory */
246 char *home_dir;
248 /* The value of the other directory, only used when loading the setup */
249 char *other_dir = 0;
251 /* Only used at program boot */
252 int boot_current_is_left = 1;
254 char *this_dir = 0;
256 /* If this is true, then when browsing the tree the other window will
257 * automatically reload it's directory with the contents of the currently
258 * selected directory.
260 int xtree_mode = 0;
262 /* If set, then print to the given file the last directory we were at */
263 static char *last_wd_file;
264 static char *last_wd_string;
265 /* Set to 1 to suppress printing the last directory */
266 static int print_last_revert = 0;
268 /* Force colors, only used by Slang */
269 int force_colors = 0;
271 /* colors specified on the command line: they override any other setting */
272 char *command_line_colors;
274 /* File name to view if argument was supplied */
275 char *view_one_file = 0;
277 /* File name to edit if argument was supplied */
278 char *edit_one_file = 0;
280 /* Line to start the editor on */
281 static int edit_one_file_start_line = 0;
283 /* Used so that widgets know if they are being destroyed or
284 shut down */
285 int midnight_shutdown = 0;
287 /* to show nice prompts */
288 static int last_paused = 0;
290 /* Used for keeping track of the original stdout */
291 int stdout_fd = 0;
293 /* The user's shell */
294 char *shell;
296 /* mc_home: The home of MC */
297 char *mc_home;
299 char cmd_buf[512];
301 WPanel *
302 get_current_panel (void)
304 return current_panel;
307 WPanel *
308 get_other_panel (void)
310 return (WPanel *) get_panel_widget (get_other_index ());
313 static void
314 reload_panelized (WPanel *panel)
316 int i, j;
317 dir_list *list = &panel->dir;
319 if (panel != cpanel)
320 mc_chdir (panel->cwd);
322 for (i = 0, j = 0; i < panel->count; i++) {
323 if (list->list[i].f.marked) {
324 /* Unmark the file in advance. In case the following mc_lstat
325 * fails we are done, else we have to mark the file again
326 * (Note: do_file_mark depends on a valid "list->list [i].buf").
327 * IMO that's the best way to update the panel's summary status
328 * -- Norbert
330 do_file_mark (panel, i, 0);
332 if (mc_lstat (list->list[i].fname, &list->list[i].buf)) {
333 g_free (list->list[i].fname);
334 continue;
336 if (list->list[i].f.marked)
337 do_file_mark (panel, i, 1);
338 if (j != i)
339 list->list[j] = list->list[i];
340 j++;
342 if (j == 0)
343 panel->count = set_zero_dir (list);
344 else
345 panel->count = j;
347 if (panel != cpanel)
348 mc_chdir (cpanel->cwd);
351 static void
352 update_one_panel_widget (WPanel *panel, int force_update,
353 char *current_file)
355 int free_pointer;
357 if (force_update & UP_RELOAD) {
358 panel->is_panelized = 0;
360 #if 1
361 ftpfs_flushdir ();
362 #endif
363 /* FIXME: Should supply flushdir method */
364 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
367 /* If current_file == -1 (an invalid pointer) then preserve selection */
368 if (current_file == UP_KEEPSEL) {
369 free_pointer = 1;
370 current_file = g_strdup (panel->dir.list[panel->selected].fname);
371 } else
372 free_pointer = 0;
374 if (panel->is_panelized)
375 reload_panelized (panel);
376 else
377 panel_reload (panel);
379 try_to_select (panel, current_file);
380 panel->dirty = 1;
382 if (free_pointer)
383 g_free (current_file);
386 void
387 panel_clean_dir (WPanel *panel)
389 int count = panel->count;
391 panel->count = 0;
392 panel->top_file = 0;
393 panel->selected = 0;
394 panel->marked = 0;
395 panel->dirs_marked = 0;
396 panel->total = 0;
397 panel->searching = 0;
398 panel->is_panelized = 0;
400 clean_dir (&panel->dir, count);
403 static void
404 update_one_panel (int which, int force_update, char *current_file)
406 WPanel *panel;
408 if (get_display_type (which) != view_listing)
409 return;
411 panel = (WPanel *) get_panel_widget (which);
412 update_one_panel_widget (panel, force_update, current_file);
415 /* This routine reloads the directory in both panels. It tries to
416 * select current_file in current_panel and other_file in other_panel.
417 * If current_file == -1 then it automatically sets current_file and
418 * other_file to the currently selected files in the panels.
420 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
421 * will not reload the other panel.
423 void
424 update_panels (int force_update, char *current_file)
426 int reload_other = !(force_update & UP_ONLY_CURRENT);
427 WPanel *panel;
429 update_one_panel (get_current_index (), force_update, current_file);
430 if (reload_other)
431 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
433 if (get_current_type () == view_listing)
434 panel = (WPanel *) get_panel_widget (get_current_index ());
435 else
436 panel = (WPanel *) get_panel_widget (get_other_index ());
438 mc_chdir (panel->cwd);
441 /* Sets up the terminal before executing a program */
442 static void
443 pre_exec (void)
445 use_dash (0);
446 edition_pre_exec ();
449 /* Save current stat of directories to avoid reloading the panels */
450 /* when no modifications have taken place */
451 void
452 save_cwds_stat (void)
454 if (fast_reload) {
455 mc_stat (cpanel->cwd, &(cpanel->dir_stat));
456 if (get_other_type () == view_listing)
457 mc_stat (opanel->cwd, &(opanel->dir_stat));
461 #ifdef HAVE_SUBSHELL_SUPPORT
462 void
463 do_possible_cd (char *new_dir)
465 if (!do_cd (new_dir, cd_exact))
466 message (1, _("Warning"),
467 _(" The Commander can't change to the directory that \n"
468 " the subshell claims you are in. Perhaps you have \n"
469 " deleted your working directory, or given yourself \n"
470 " extra access permissions with the \"su\" command? "));
473 void
474 do_update_prompt (void)
476 if (update_prompt) {
477 printf ("%s", subshell_prompt);
478 fflush (stdout);
479 update_prompt = 0;
482 #endif /* HAVE_SUBSHELL_SUPPORT */
484 void
485 restore_console (void)
487 handle_console (CONSOLE_RESTORE);
490 void
491 exec_shell (void)
493 do_execute (shell, 0, 0);
496 void
497 do_execute (const char *shell, const char *command, int flags)
499 #ifdef HAVE_SUBSHELL_SUPPORT
500 char *new_dir = NULL;
501 #endif /* HAVE_SUBSHELL_SUPPORT */
503 #ifdef USE_VFS
504 char *old_vfs_dir = 0;
506 if (!vfs_current_is_local ())
507 old_vfs_dir = g_strdup (vfs_get_current_dir ());
508 #endif /* USE_VFS */
510 save_cwds_stat ();
511 pre_exec ();
512 if (console_flag)
513 restore_console ();
515 if (!use_subshell && !(flags & EXECUTE_INTERNAL && command)) {
516 printf ("%s%s%s\n", last_paused ? "\r\n" : "", prompt, command);
517 last_paused = 0;
519 #ifdef HAVE_SUBSHELL_SUPPORT
520 if (use_subshell && !(flags & EXECUTE_INTERNAL)) {
521 do_update_prompt ();
523 /* We don't care if it died, higher level takes care of this */
524 #ifdef USE_VFS
525 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
526 #else
527 invoke_subshell (command, VISIBLY, &new_dir);
528 #endif /* !USE_VFS */
529 } else
530 #endif /* HAVE_SUBSHELL_SUPPORT */
531 my_system (flags, shell, command);
533 if (!(flags & EXECUTE_INTERNAL)) {
534 if ((pause_after_run == pause_always
535 || (pause_after_run == pause_on_dumb_terminals && !xterm_flag
536 && !console_flag)) && !quit
537 #ifdef HAVE_SUBSHELL_SUPPORT
538 && subshell_state != RUNNING_COMMAND
539 #endif /* HAVE_SUBSHELL_SUPPORT */
541 printf ("%s\r\n", _("Press any key to continue..."));
542 last_paused = 1;
543 fflush (stdout);
544 mc_raw_mode ();
545 getch ();
547 if (console_flag) {
548 if (output_lines && keybar_visible) {
549 putchar ('\n');
550 fflush (stdout);
555 if (console_flag)
556 handle_console (CONSOLE_SAVE);
557 edition_post_exec ();
559 #ifdef HAVE_SUBSHELL_SUPPORT
560 if (new_dir)
561 do_possible_cd (new_dir);
563 #endif /* HAVE_SUBSHELL_SUPPORT */
565 #ifdef USE_VFS
566 if (old_vfs_dir) {
567 mc_chdir (old_vfs_dir);
568 g_free (old_vfs_dir);
570 #endif /* USE_VFS */
572 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
573 update_xterm_title_path ();
575 do_refresh ();
576 use_dash (TRUE);
579 /* Executes a command */
580 void
581 shell_execute (char *command, int flags)
583 #ifdef HAVE_SUBSHELL_SUPPORT
584 if (use_subshell)
585 if (subshell_state == INACTIVE || force_subshell_execution)
586 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
587 else
588 message (1, MSG_ERROR,
589 _(" The shell is already running a command "));
590 else
591 #endif /* HAVE_SUBSHELL_SUPPORT */
592 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
595 void
596 execute (char *command)
598 shell_execute (command, 0);
601 void
602 change_panel (void)
604 free_completions (cmdline);
605 dlg_one_down (midnight_dlg);
608 /* Stop MC main dialog and the current dialog if it exists.
609 * Needed to provide fast exit from MC viewer or editor on shell exit */
610 static void
611 stop_dialogs (void)
613 midnight_dlg->running = 0;
614 if (current_dlg) {
615 current_dlg->running = 0;
619 static int
620 quit_cmd_internal (int quiet)
622 int q = quit;
624 if (quiet || !confirm_exit) {
625 q = 1;
626 } else {
627 if (query_dialog
628 (_(" The Midnight Commander "),
629 _(" Do you really want to quit the Midnight Commander? "), 0,
630 2, _("&Yes"), _("&No")) == 0)
631 q = 1;
633 if (q) {
634 #ifdef HAVE_SUBSHELL_SUPPORT
635 if (!use_subshell)
636 stop_dialogs ();
637 else if ((q = exit_subshell ()))
638 #endif
639 stop_dialogs ();
641 if (q)
642 quit |= 1;
643 return quit;
646 static void
647 quit_cmd (void)
649 quit_cmd_internal (0);
653 quiet_quit_cmd (void)
655 print_last_revert = 1;
656 quit_cmd_internal (1);
657 return quit;
661 * Touch window and refresh window functions
664 /* This routine untouches the first line on both panels in order */
665 /* to avoid the refreshing the menu bar */
667 void
668 untouch_bar (void)
670 do_refresh ();
673 void
674 repaint_screen (void)
676 do_refresh ();
677 mc_refresh ();
680 /* Wrapper for do_subshell_chdir, check for availability of subshell */
681 void
682 subshell_chdir (char *directory)
684 #ifdef HAVE_SUBSHELL_SUPPORT
685 if (use_subshell) {
686 if (vfs_current_is_local ())
687 do_subshell_chdir (directory, 0, 1);
689 #endif /* HAVE_SUBSHELL_SUPPORT */
692 void
693 directory_history_add (struct WPanel *panel, char *s)
695 char *text;
697 text = g_strdup (s);
698 strip_password (s, 1);
700 panel->dir_history = list_append_unique (panel->dir_history, text);
704 * If we moved to the parent directory move the selection pointer to
705 * the old directory name; If we leave VFS dir, remove FS specificator.
706 * Warn: This code spoils lwd string.
708 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
710 static char *
711 get_parent_dir_name (char *cwd, char *lwd)
713 char *p;
714 if (strlen (lwd) > strlen (cwd))
715 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd)) {
716 return (p + 1);
718 return NULL;
722 * Changes the current directory of the panel.
723 * Don't record change in the directory history.
725 static int
726 _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
728 char *directory, *olddir;
729 char temp[MC_MAXPATHLEN];
730 char *translated_url;
731 #ifdef USE_VFS
732 vfs *oldvfs;
733 vfsid oldvfsid;
734 struct vfs_stamping *parent;
735 #endif
736 olddir = g_strdup (panel->cwd);
737 translated_url = new_dir = vfs_translate_url (new_dir);
739 /* Convert *new_path to a suitable pathname, handle ~user */
741 if (cd_type == cd_parse_command) {
742 while (*new_dir == ' ')
743 new_dir++;
745 if (!strcmp (new_dir, "-")) {
746 strcpy (temp, panel->lwd);
747 new_dir = temp;
750 directory = *new_dir ? new_dir : home_dir;
752 if (mc_chdir (directory) == -1) {
753 strcpy (panel->cwd, olddir);
754 g_free (olddir);
755 g_free (translated_url);
756 return 0;
758 g_free (translated_url);
760 /* Success: save previous directory, shutdown status of previous dir */
761 strcpy (panel->lwd, olddir);
762 free_completions (cmdline);
764 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
766 #ifdef USE_VFS
767 oldvfs = vfs_type (olddir);
768 oldvfsid = vfs_ncs_getid (oldvfs, olddir, &parent);
769 vfs_add_noncurrent_stamps (oldvfs, oldvfsid, parent);
770 vfs_rm_parents (parent);
771 #endif
773 subshell_chdir (panel->cwd);
775 /* Reload current panel */
776 panel_clean_dir (panel);
777 panel->count =
778 do_load_dir (&panel->dir, panel->sort_type, panel->reverse,
779 panel->case_sensitive, panel->filter);
780 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
781 load_hint (0);
782 panel_update_contents (panel);
783 update_xterm_title_path ();
785 g_free (olddir);
787 return 1;
791 * Changes the current directory of the panel.
792 * Record change in the directory history.
795 do_panel_cd (struct WPanel *panel, char *new_dir, enum cd_enum cd_type)
797 int r;
799 r = _do_panel_cd (panel, new_dir, cd_type);
800 if (r)
801 directory_history_add (panel, panel->cwd);
802 return r;
806 do_cd (char *new_dir, enum cd_enum exact)
808 return (do_panel_cd (cpanel, new_dir, exact));
811 void
812 directory_history_next (WPanel *panel)
814 GList *nextdir;
816 nextdir = g_list_next (panel->dir_history);
818 if (!nextdir)
819 return;
821 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
822 panel->dir_history = nextdir;
825 void
826 directory_history_prev (WPanel *panel)
828 GList *prevdir;
830 prevdir = g_list_previous (panel->dir_history);
832 if (!prevdir)
833 return;
835 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
836 panel->dir_history = prevdir;
839 void
840 directory_history_list (WPanel *panel)
842 char *s;
844 if (!panel->dir_history)
845 return;
847 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
849 if (!s)
850 return;
852 if (_do_panel_cd (panel, s, cd_exact))
853 directory_history_add (panel, panel->cwd);
854 else
855 message (1, MSG_ERROR, _("Cannot change directory"));
856 g_free (s);
859 #ifdef HAVE_SUBSHELL_SUPPORT
861 load_prompt (int fd, void *unused)
863 if (!read_subshell_prompt ())
864 return 0;
866 /* Don't actually change the prompt if it's invisible */
867 if (current_dlg == midnight_dlg && command_prompt) {
868 int prompt_len;
870 prompt = strip_ctrl_codes (subshell_prompt);
871 prompt_len = strlen (prompt);
873 /* Check for prompts too big */
874 if (COLS > 8 && prompt_len > COLS - 8) {
875 prompt[COLS - 8] = 0;
876 prompt_len = COLS - 8;
878 label_set_text (the_prompt, prompt);
879 winput_set_origin ((WInput *) cmdline, prompt_len,
880 COLS - prompt_len);
882 /* since the prompt has changed, and we are called from one of the
883 * get_event channels, the prompt updating does not take place
884 * automatically: force a cursor update and a screen refresh
886 update_cursor (midnight_dlg);
887 mc_refresh ();
889 update_prompt = 1;
890 return 0;
892 #endif /* HAVE_SUBSHELL_SUPPORT */
894 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
896 maybe_cd (int char_code, int move_up_dir)
898 if (navigate_with_arrows) {
899 if (!cmdline->buffer[0]) {
900 if (!move_up_dir) {
901 do_cd ("..", cd_exact);
902 return 1;
904 if (S_ISDIR (selection (cpanel)->buf.st_mode)
905 || link_isdir (selection (cpanel))) {
906 do_cd (selection (cpanel)->fname, cd_exact);
907 return 1;
911 return 0;
914 static void
915 sort_cmd (void)
917 WPanel *p;
918 sortfn *sort_order;
920 if (!SELECTED_IS_PANEL)
921 return;
923 p = MENU_PANEL;
924 sort_order = sort_box (p->sort_type, &p->reverse, &p->case_sensitive);
926 panel_set_sort_order (p, sort_order);
930 static void
931 treebox_cmd (void)
933 char *sel_dir;
935 sel_dir = tree_box (selection (cpanel)->fname);
936 if (sel_dir) {
937 do_cd (sel_dir, cd_exact);
938 g_free (sel_dir);
942 #ifdef LISTMODE_EDITOR
943 static void
944 listmode_cmd (void)
946 char *newmode;
947 newmode = listmode_edit ("half <type,>name,|,size:8,|,perm:4+");
948 message (0, _(" Listing format edit "), _(" New mode is \"%s\" "),
949 newmode);
950 g_free (newmode);
952 #endif /* LISTMODE_EDITOR */
954 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
955 static menu_entry PanelMenu[] = {
956 {' ', N_("&Listing mode..."), 'L', listing_cmd},
957 {' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd},
958 {' ', N_("&Info C-x i"), 'I', info_cmd},
959 {' ', N_("&Tree"), 'T', tree_cmd},
960 {' ', "", ' ', 0},
961 {' ', N_("&Sort order..."), 'S', sort_cmd},
962 {' ', "", ' ', 0},
963 {' ', N_("&Filter..."), 'F', filter_cmd},
964 #ifdef USE_NETCODE
965 {' ', "", ' ', 0},
966 #ifdef WITH_MCFS
967 {' ', N_("&Network link..."), 'N', netlink_cmd},
968 #endif
969 {' ', N_("FT&P link..."), 'P', ftplink_cmd},
970 {' ', N_("S&hell link..."), 'H', fishlink_cmd},
971 #ifdef WITH_SMBFS
972 {' ', N_("SM&B link..."), 'B', smblink_cmd},
973 #endif
974 #endif
975 {' ', "", ' ', 0},
976 #ifdef NATIVE_WIN32
977 {' ', N_("&Drive... M-d"), 'D', drive_cmd_a},
978 #endif
979 {' ', N_("&Rescan C-r"), 'R', reread_cmd}
982 static menu_entry RightMenu[] = {
983 {' ', N_("&Listing mode..."), 'L', listing_cmd},
984 {' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd},
985 {' ', N_("&Info C-x i"), 'I', info_cmd},
986 {' ', N_("&Tree"), 'T', tree_cmd},
987 {' ', "", ' ', 0},
988 {' ', N_("&Sort order..."), 'S', sort_cmd},
989 {' ', "", ' ', 0},
990 {' ', N_("&Filter..."), 'F', filter_cmd},
991 #ifdef USE_NETCODE
992 {' ', "", ' ', 0},
993 #ifdef WITH_MCFS
994 {' ', N_("&Network link..."), 'N', netlink_cmd},
995 #endif
996 {' ', N_("FT&P link..."), 'P', ftplink_cmd},
997 {' ', N_("S&hell link..."), 'H', fishlink_cmd},
998 #ifdef WITH_SMBFS
999 {' ', N_("SM&B link..."), 'B', smblink_cmd},
1000 #endif
1001 #endif
1002 {' ', "", ' ', 0},
1003 #ifdef NATIVE_WIN32
1004 {' ', N_("&Drive... M-d"), 'D', drive_cmd_b},
1005 #endif
1006 {' ', N_("&Rescan C-r"), 'R', reread_cmd}
1009 static menu_entry FileMenu[] = {
1010 {' ', N_("&User menu F2"), 'U', user_file_menu_cmd},
1011 {' ', N_("&View F3"), 'V', view_cmd},
1012 {' ', N_("Vie&w file... "), 'W', view_file_cmd},
1013 {' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd},
1014 {' ', N_("&Edit F4"), 'E', edit_cmd},
1015 {' ', N_("&Copy F5"), 'C', copy_cmd},
1016 {' ', N_("c&Hmod C-x c"), 'H', chmod_cmd},
1017 #ifndef NATIVE_WIN32
1018 {' ', N_("&Link C-x l"), 'L', link_cmd},
1019 {' ', N_("&SymLink C-x s"), 'S', symlink_cmd},
1020 {' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd},
1021 {' ', N_("ch&Own C-x o"), 'O', chown_cmd},
1022 {' ', N_("&Advanced chown "), 'A', chown_advanced_cmd},
1023 #endif
1024 {' ', N_("&Rename/Move F6"), 'R', ren_cmd},
1025 {' ', N_("&Mkdir F7"), 'M', mkdir_cmd},
1026 {' ', N_("&Delete F8"), 'D', delete_cmd},
1027 {' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd},
1028 {' ', "", ' ', 0},
1029 {' ', N_("select &Group M-+"), 'G', select_cmd},
1030 {' ', N_("u&Nselect group M-\\"), 'N', unselect_cmd},
1031 {' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd},
1032 {' ', "", ' ', 0},
1033 {' ', N_("e&Xit F10"), 'X', quit_cmd}
1036 static menu_entry CmdMenu[] = {
1037 /* I know, I'm lazy, but the tree widget when it's not running
1038 * as a panel still has some problems, I have not yet finished
1039 * the WTree widget port, sorry.
1041 {' ', N_("&Directory tree"), 'D', treebox_cmd},
1042 {' ', N_("&Find file M-?"), 'F', find_cmd},
1043 {' ', N_("s&Wap panels C-u"), 'W', swap_cmd},
1044 {' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd},
1045 {' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd},
1046 {' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize},
1047 {' ', N_("show directory s&Izes"), 'I', dirsizes_cmd},
1048 {' ', "", ' ', 0},
1049 {' ', N_("command &History"), 'H', history_cmd},
1050 {' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd},
1051 #ifdef USE_VFS
1052 {' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs},
1053 {' ', N_("Fr&ee VFSs now"), 'E', free_vfs_now},
1054 #endif
1055 #ifdef WITH_BACKGROUND
1056 {' ', N_("&Background jobs C-x j"), 'B', jobs_cmd},
1057 #endif
1058 {' ', "", ' ', 0},
1059 #ifdef USE_EXT2FSLIB
1060 {' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd},
1061 #endif
1062 #ifdef LISTMODE_EDITOR
1063 {' ', N_("&Listing format edit"), 'L', listmode_cmd},
1064 #endif
1065 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
1066 {' ', "", ' ', 0},
1067 #endif
1068 {' ', N_("Edit &extension file"), 'E', ext_cmd},
1069 {' ', N_("Edit &menu file"), 'M', edit_mc_menu_cmd},
1070 #ifdef USE_INTERNAL_EDIT
1071 {' ', N_("Edit edi&tor menu file"), 'T', edit_user_menu_cmd},
1072 {' ', N_("Edit &syntax file"), 'S', edit_syntax_cmd}
1073 #endif /* USE_INTERNAL_EDIT */
1076 /* Must keep in sync with the constants in menu_cmd */
1077 static menu_entry OptMenu[] = {
1078 {' ', N_("&Configuration..."), 'C', configure_box},
1079 {' ', N_("&Layout..."), 'L', layout_cmd},
1080 {' ', N_("c&Onfirmation..."), 'O', confirm_box},
1081 {' ', N_("&Display bits..."), 'D', display_bits_box},
1082 #ifndef NATIVE_WIN32
1083 {' ', N_("learn &Keys..."), 'K', learn_keys},
1084 #endif /* !NATIVE_WIN32 */
1085 #ifdef USE_VFS
1086 {' ', N_("&Virtual FS..."), 'V', configure_vfs},
1087 #endif /* !USE_VFS */
1088 {' ', "", ' ', 0},
1089 {' ', N_("&Save setup"), 'S', save_setup_cmd}
1092 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
1094 static Menu *MenuBar[5];
1096 void
1097 init_menu (void)
1099 MenuBar[0] =
1100 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
1101 PanelMenu, menu_entries (PanelMenu),
1102 "[Left and Right Menus]");
1103 MenuBar[1] =
1104 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
1105 "[File Menu]");
1106 MenuBar[2] =
1107 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
1108 "[Command Menu]");
1109 MenuBar[3] =
1110 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
1111 "[Options Menu]");
1112 MenuBar[4] =
1113 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
1114 RightMenu, menu_entries (PanelMenu),
1115 "[Left and Right Menus]");
1118 void
1119 done_menu (void)
1121 int i;
1123 for (i = 0; i < 5; i++) {
1124 destroy_menu (MenuBar[i]);
1128 static void
1129 menu_last_selected_cmd (void)
1131 the_menubar->active = 1;
1132 the_menubar->dropped = drop_menus;
1133 the_menubar->previous_selection = dlg_item_number (midnight_dlg);
1134 dlg_select_widget (midnight_dlg, the_menubar);
1137 static void
1138 menu_cmd (void)
1140 if (the_menubar->active)
1141 return;
1143 if (get_current_index () == 0)
1144 the_menubar->selected = 0;
1145 else
1146 the_menubar->selected = 4;
1147 menu_last_selected_cmd ();
1150 /* Flag toggling functions */
1151 void
1152 toggle_fast_reload (void)
1154 fast_reload = !fast_reload;
1155 if (fast_reload_w == 0 && fast_reload) {
1156 message (0, _(" Information "),
1158 (" Using the fast reload option may not reflect the exact \n"
1159 " directory contents. In this cases you'll need to do a \n"
1160 " manual reload of the directory. See the man page for \n"
1161 " the details. "));
1162 fast_reload_w = 1;
1166 void
1167 toggle_mix_all_files (void)
1169 mix_all_files = !mix_all_files;
1170 update_panels (UP_RELOAD, UP_KEEPSEL);
1173 void
1174 toggle_show_backup (void)
1176 show_backups = !show_backups;
1177 update_panels (UP_RELOAD, UP_KEEPSEL);
1180 void
1181 toggle_show_hidden (void)
1183 show_dot_files = !show_dot_files;
1184 update_panels (UP_RELOAD, UP_KEEPSEL);
1188 * Just a hack for allowing url-like pathnames to be accepted from the
1189 * command line.
1191 static void
1192 translated_mc_chdir (char *dir)
1194 char *newdir;
1196 newdir = vfs_translate_url (dir);
1197 mc_chdir (newdir);
1198 g_free (newdir);
1201 void
1202 create_panels (void)
1204 int current_index;
1205 int other_index;
1206 int current_mode;
1207 int other_mode;
1208 char original_dir[1024];
1210 original_dir[0] = 0;
1212 if (boot_current_is_left) {
1213 current_index = 0;
1214 other_index = 1;
1215 current_mode = startup_left_mode;
1216 other_mode = startup_right_mode;
1217 } else {
1218 current_index = 1;
1219 other_index = 0;
1220 current_mode = startup_right_mode;
1221 other_mode = startup_left_mode;
1223 /* Creates the left panel */
1224 if (this_dir) {
1225 if (other_dir) {
1226 /* Ok, user has specified two dirs, save the original one,
1227 * since we may not be able to chdir to the proper
1228 * second directory later
1230 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1232 translated_mc_chdir (this_dir);
1234 set_display_type (current_index, current_mode);
1236 /* The other panel */
1237 if (other_dir) {
1238 if (original_dir[0])
1239 translated_mc_chdir (original_dir);
1240 translated_mc_chdir (other_dir);
1242 set_display_type (other_index, other_mode);
1244 if (startup_left_mode == view_listing) {
1245 current_panel = left_panel;
1246 } else {
1247 if (right_panel)
1248 current_panel = right_panel;
1249 else
1250 current_panel = left_panel;
1253 /* Create the nice widgets */
1254 cmdline = command_new (0, 0, 0);
1255 the_prompt = label_new (0, 0, prompt, NULL);
1256 the_prompt->transparent = 1;
1257 the_bar = buttonbar_new (keybar_visible);
1259 the_hint = label_new (0, 0, 0, NULL);
1260 the_hint->transparent = 1;
1261 the_hint->auto_adjust_cols = 0;
1262 the_hint->widget.cols = COLS;
1264 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1267 static void
1268 copy_current_pathname (void)
1270 if (!command_prompt)
1271 return;
1273 command_insert (cmdline, cpanel->cwd, 0);
1274 if (cpanel->cwd[strlen (cpanel->cwd) - 1] != PATH_SEP)
1275 command_insert (cmdline, PATH_SEP_STR, 0);
1278 static void
1279 copy_other_pathname (void)
1281 if (get_other_type () != view_listing)
1282 return;
1284 if (!command_prompt)
1285 return;
1287 command_insert (cmdline, opanel->cwd, 0);
1288 if (cpanel->cwd[strlen (opanel->cwd) - 1] != PATH_SEP)
1289 command_insert (cmdline, PATH_SEP_STR, 0);
1292 static void
1293 copy_readlink (WPanel *panel)
1295 if (!command_prompt)
1296 return;
1297 if (S_ISLNK (selection (panel)->buf.st_mode)) {
1298 char buffer[MC_MAXPATHLEN];
1299 char *p =
1300 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1301 int i;
1303 i = mc_readlink (p, buffer, MC_MAXPATHLEN);
1304 g_free (p);
1305 if (i > 0) {
1306 buffer[i] = 0;
1307 command_insert (cmdline, buffer, 1);
1312 static void
1313 copy_current_readlink (void)
1315 copy_readlink (cpanel);
1318 static void
1319 copy_other_readlink (void)
1321 if (get_other_type () != view_listing)
1322 return;
1323 copy_readlink (opanel);
1326 /* Insert the selected file name into the input line */
1327 static void
1328 copy_prog_name (void)
1330 char *tmp;
1331 if (!command_prompt)
1332 return;
1334 if (get_current_type () == view_tree) {
1335 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1336 tmp = tree_selected_name (tree);
1337 } else
1338 tmp = selection (cpanel)->fname;
1340 command_insert (cmdline, tmp, 1);
1343 static void
1344 copy_tagged (WPanel *panel)
1346 int i;
1348 if (!command_prompt)
1349 return;
1350 input_disable_update (cmdline);
1351 if (panel->marked) {
1352 for (i = 0; i < panel->count; i++) {
1353 if (panel->dir.list[i].f.marked)
1354 command_insert (cmdline, panel->dir.list[i].fname, 1);
1356 } else {
1357 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1360 input_enable_update (cmdline);
1363 static void
1364 copy_current_tagged (void)
1366 copy_tagged (cpanel);
1369 static void
1370 copy_other_tagged (void)
1372 if (get_other_type () != view_listing)
1373 return;
1374 copy_tagged (opanel);
1377 static void
1378 do_suspend_cmd (void)
1380 pre_exec ();
1382 if (console_flag && !use_subshell)
1383 restore_console ();
1385 #ifdef SIGTSTP
1387 struct sigaction sigtstp_action;
1389 /* Make sure that the SIGTSTP below will suspend us directly,
1390 without calling ncurses' SIGTSTP handler; we *don't* want
1391 ncurses to redraw the screen immediately after the SIGCONT */
1392 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
1394 kill (getpid (), SIGTSTP);
1396 /* Restore previous SIGTSTP action */
1397 sigaction (SIGTSTP, &sigtstp_action, NULL);
1399 #endif /* SIGTSTP */
1401 if (console_flag && !use_subshell)
1402 handle_console (CONSOLE_SAVE);
1404 edition_post_exec ();
1407 void
1408 suspend_cmd (void)
1410 save_cwds_stat ();
1411 do_suspend_cmd ();
1412 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1413 do_refresh ();
1416 static void
1417 init_labels (void)
1419 define_label (midnight_dlg, 1, _("Help"), help_cmd);
1420 define_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1421 define_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1422 define_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1425 static const key_map ctl_x_map[] = {
1426 {XCTRL ('c'), quit_cmd},
1427 {'d', compare_dirs_cmd},
1428 #ifdef USE_VFS
1429 {'a', reselect_vfs},
1430 #endif /* USE_VFS */
1431 {'p', copy_current_pathname},
1432 {XCTRL ('p'), copy_other_pathname},
1433 {'t', copy_current_tagged},
1434 {XCTRL ('t'), copy_other_tagged},
1435 {'c', chmod_cmd},
1436 #ifndef NATIVE_WIN32
1437 {'o', chown_cmd},
1438 {'r', copy_current_readlink},
1439 {XCTRL ('r'), copy_other_readlink},
1440 {'l', link_cmd},
1441 {'s', symlink_cmd},
1442 {XCTRL ('s'), edit_symlink_cmd},
1443 #endif /* !NATIVE_WIN32 */
1444 {'i', info_cmd_no_menu},
1445 {'q', quick_cmd_no_menu},
1446 {'h', add2hotlist_cmd},
1447 {'!', external_panelize},
1448 #ifdef WITH_BACKGROUND
1449 {'j', jobs_cmd},
1450 #endif /* WITH_BACKGROUND */
1451 #ifdef HAVE_SETSOCKOPT
1452 {'%', source_routing},
1453 #endif /* HAVE_SETSOCKOPT */
1454 {0, 0}
1457 static int ctl_x_map_enabled = 0;
1459 static void
1460 ctl_x_cmd (int ignore)
1462 ctl_x_map_enabled = 1;
1465 static void
1466 nothing (void)
1470 static const key_map default_map[] = {
1471 {KEY_F (19), menu_last_selected_cmd},
1472 {KEY_F (20), (key_callback) quiet_quit_cmd},
1474 /* Copy useful information to the command line */
1475 {ALT ('a'), copy_current_pathname},
1476 {ALT ('A'), copy_other_pathname},
1478 {ALT ('c'), quick_cd_cmd},
1480 /* To access the directory hotlist */
1481 {XCTRL ('\\'), quick_chdir_cmd},
1483 /* Suspend */
1484 {XCTRL ('z'), suspend_cmd},
1486 /* The filtered view command */
1487 {ALT ('!'), filtered_view_cmd},
1489 /* Find file */
1490 {ALT ('?'), find_cmd},
1492 /* Panel refresh */
1493 {XCTRL ('r'), reread_cmd},
1495 /* Toggle listing between long, user defined and full formats */
1496 {ALT ('t'), toggle_listing_cmd},
1498 /* Swap panels */
1499 {XCTRL ('u'), swap_cmd},
1501 /* View output */
1502 {XCTRL ('o'), view_other_cmd},
1504 /* Control-X keybindings */
1505 {XCTRL ('x'), ctl_x_cmd},
1507 /* Trap dlg's exit commands */
1508 {ESC_CHAR, nothing},
1509 {XCTRL ('c'), nothing},
1510 {XCTRL ('g'), nothing},
1511 {0, 0},
1514 static void
1515 setup_sigwinch (void)
1517 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && \
1518 !defined(NATIVE_WIN32) && defined(SIGWINCH)
1519 struct sigaction act, oact;
1520 act.sa_handler = flag_winch;
1521 sigemptyset (&act.sa_mask);
1522 act.sa_flags = 0;
1523 # ifdef SA_RESTART
1524 act.sa_flags |= SA_RESTART;
1525 # endif
1526 sigaction (SIGWINCH, &act, &oact);
1527 #endif
1530 static void
1531 setup_pre (void)
1533 /* Call all the inits */
1534 #ifdef HAVE_CHARSET
1536 * Don't restrict the output on the screen manager level,
1537 * the translation tables take care of it.
1539 #define full_eight_bits (1)
1540 #define eight_bit_clean (1)
1541 #endif /* !HAVE_CHARSET */
1543 #ifndef HAVE_SLANG
1544 meta (stdscr, eight_bit_clean);
1545 #else
1546 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1547 #endif
1550 static void
1551 init_xterm_support (void)
1553 char *termvalue;
1554 #ifdef HAVE_SLANG
1555 char *term_entry;
1556 #endif
1558 termvalue = getenv ("TERM");
1559 if (!termvalue || !(*termvalue)) {
1560 fputs (_("The TERM environment variable is unset!\n"), stderr);
1561 exit (1);
1564 /* Check mouse capabilities */
1565 #ifdef HAVE_SLANG
1566 term_entry = SLtt_tigetent (termvalue);
1567 xmouse_seq = SLtt_tigetstr ("Km", &term_entry);
1568 #else
1569 xmouse_seq = tigetstr ("kmous");
1570 #endif
1572 /* -1 means invalid capability, shouldn't happen, but let's make it 0 */
1573 if ((long) xmouse_seq == -1) {
1574 xmouse_seq = NULL;
1577 if (strcmp (termvalue, "cygwin") == 0) {
1578 force_xterm = 1;
1579 use_mouse_p = MOUSE_DISABLED;
1582 if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
1583 || strncmp (termvalue, "rxvt", 4) == 0
1584 || strcmp (termvalue, "dtterm") == 0) {
1585 xterm_flag = 1;
1587 /* Default to the standard xterm sequence */
1588 if (!xmouse_seq) {
1589 xmouse_seq = ESC_STR "[M";
1592 /* Enable mouse unless explicitly disabled by --nomouse */
1593 if (use_mouse_p != MOUSE_DISABLED) {
1594 use_mouse_p = MOUSE_XTERM;
1599 static void
1600 setup_mc (void)
1602 setup_pre ();
1603 init_menu ();
1604 create_panels ();
1605 setup_panels ();
1607 #ifdef HAVE_SUBSHELL_SUPPORT
1608 if (use_subshell)
1609 add_select_channel (subshell_pty, load_prompt, 0);
1610 #endif /* !HAVE_SUBSHELL_SUPPORT */
1612 setup_sigwinch ();
1614 if (baudrate () < 9600 || slow_terminal) {
1615 verbose = 0;
1617 init_mouse ();
1620 static void
1621 setup_dummy_mc (const char *file)
1623 char d[MC_MAXPATHLEN];
1625 mc_get_current_wd (d, MC_MAXPATHLEN);
1626 setup_mc ();
1627 mc_chdir (d);
1629 /* Create a fake current_panel, this is needed because the
1630 * expand_format routine will use current panel.
1632 strcpy (cpanel->cwd, d);
1633 cpanel->selected = 0;
1634 cpanel->count = 1;
1635 cpanel->dir.list[0].fname = (char *) file;
1638 static void
1639 done_mc (void)
1641 disable_mouse ();
1643 done_menu ();
1645 /* Setup shutdown
1647 * We sync the profiles since the hotlist may have changed, while
1648 * we only change the setup data if we have the auto save feature set
1650 if (auto_save_setup)
1651 save_setup (); /* does also call save_hotlist */
1652 else
1653 save_hotlist ();
1654 done_screen ();
1655 vfs_add_current_stamps ();
1658 /* This should be called after destroy_dlg since panel widgets
1659 * save their state on the profiles
1661 static void
1662 done_mc_profile (void)
1664 if (!auto_save_setup)
1665 profile_forget_profile (profile_name);
1666 sync_profiles ();
1667 done_setup ();
1668 free_profiles ();
1672 * Partly repaint the contents of the panels.
1673 * Ideally, all painting should be done in the panel's callback.
1674 * Since we are bypassing the standard widget library by forcing
1675 * the repaint, the cursor position needs to be preserved.
1677 static void
1678 make_panels_dirty (void)
1680 int col, row;
1682 /* Preserve current cursor position */
1683 getyx (stdscr, row, col);
1685 if (cpanel->dirty)
1686 panel_update_contents (cpanel);
1688 if ((get_other_type () == view_listing) && opanel->dirty)
1689 panel_update_contents (opanel);
1691 /* Restore cursor position */
1692 move (row, col);
1695 /* In Windows people want to actually type the '\' key frequently */
1696 #ifdef NATIVE_WIN32
1697 # define check_key_backslash(x) 0
1698 #else
1699 # define check_key_backslash(x) ((x) == '\\')
1700 #endif
1702 static int
1703 midnight_callback (struct Dlg_head *h, int id, int msg)
1705 int i;
1706 static int first_pre_event = 1;
1708 switch (msg) {
1710 case DLG_PRE_EVENT:
1711 make_panels_dirty ();
1712 if (auto_menu && first_pre_event) {
1713 user_file_menu_cmd ();
1715 first_pre_event = 0;
1716 return MSG_HANDLED;
1718 case DLG_KEY:
1719 if (ctl_x_map_enabled) {
1720 ctl_x_map_enabled = 0;
1721 for (i = 0; ctl_x_map[i].key_code; i++)
1722 if (id == ctl_x_map[i].key_code) {
1723 (*ctl_x_map[i].fn) (id);
1724 return MSG_HANDLED;
1728 /* FIXME: should handle all menu shortcuts before this point */
1729 if (the_menubar->active)
1730 break;
1732 if (id == KEY_F (10)) {
1733 quit_cmd ();
1734 return MSG_HANDLED;
1737 if (id == '\t')
1738 free_completions (cmdline);
1740 if (id == '\n' && cmdline->buffer[0]) {
1741 send_message ((Widget *) cmdline, WIDGET_KEY, id);
1742 return MSG_HANDLED;
1745 /* Ctrl-Enter and Alt-Enter */
1746 if (((id & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1747 && (id & (KEY_M_CTRL | KEY_M_ALT))) {
1748 copy_prog_name ();
1749 return MSG_HANDLED;
1752 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1753 && !quote && !cpanel->searching) {
1754 if (!only_leading_plus_minus) {
1755 /* Special treatement, since the input line will eat them */
1756 if (id == '+') {
1757 select_cmd ();
1758 return MSG_HANDLED;
1761 if (check_key_backslash (id) || id == '-') {
1762 unselect_cmd ();
1763 return MSG_HANDLED;
1766 if (id == '*') {
1767 reverse_selection_cmd ();
1768 return MSG_HANDLED;
1770 } else if (command_prompt && !strlen (cmdline->buffer)) {
1771 /* Special treatement '+', '-', '\', '*' only when this is
1772 * first char on input line
1775 if (id == '+') {
1776 select_cmd ();
1777 return MSG_HANDLED;
1780 if (check_key_backslash (id) || id == '-') {
1781 unselect_cmd ();
1782 return MSG_HANDLED;
1785 if (id == '*') {
1786 reverse_selection_cmd ();
1787 return MSG_HANDLED;
1791 break;
1793 case DLG_HOTKEY_HANDLED:
1794 if (get_current_type () == view_listing)
1795 cpanel->searching = 0;
1796 break;
1798 case DLG_UNHANDLED_KEY:
1799 if (command_prompt) {
1800 int v;
1802 v = send_message ((Widget *) cmdline, WIDGET_KEY, id);
1803 if (v)
1804 return v;
1806 if (ctl_x_map_enabled) {
1807 ctl_x_map_enabled = 0;
1808 for (i = 0; ctl_x_map[i].key_code; i++)
1809 if (id == ctl_x_map[i].key_code) {
1810 (*ctl_x_map[i].fn) (id);
1811 return MSG_HANDLED;
1813 } else {
1814 for (i = 0; default_map[i].key_code; i++) {
1815 if (id == default_map[i].key_code) {
1816 (*default_map[i].fn) (id);
1817 return MSG_HANDLED;
1821 return MSG_NOT_HANDLED;
1823 /* We handle the special case of the output lines */
1824 case DLG_DRAW:
1825 attrset (SELECTED_COLOR);
1826 if (console_flag && output_lines)
1827 show_console_contents (output_start_y,
1828 LINES - output_lines - keybar_visible -
1829 1, LINES - keybar_visible - 1);
1830 return MSG_HANDLED;
1833 return default_dlg_callback (h, id, msg);
1836 #define xtoolkit_panel_setup()
1838 /* Show current directory in the xterm title */
1839 void
1840 update_xterm_title_path (void)
1842 unsigned char *p, *s;
1844 if (xterm_flag && xterm_title) {
1845 p = s = g_strdup (strip_home_and_password (cpanel->cwd));
1846 do {
1847 if (!is_printable (*s))
1848 *s = '?';
1849 } while (*++s);
1850 fprintf (stdout, "\33]0;mc - %s\7", p);
1851 fflush (stdout);
1852 g_free (p);
1857 * Load new hint and display it.
1858 * IF force is not 0, ignore the timeout.
1860 void
1861 load_hint (int force)
1863 char *hint;
1865 if (!the_hint->widget.parent)
1866 return;
1868 if (!message_visible) {
1869 label_set_text (the_hint, 0);
1870 return;
1873 if ((hint = get_random_hint (force))) {
1874 if (*hint)
1875 set_hintbar (hint);
1876 g_free (hint);
1877 } else {
1878 char text[BUF_SMALL];
1880 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1881 VERSION);
1882 set_hintbar (text);
1886 static void
1887 setup_panels_and_run_mc (void)
1889 int first, second;
1891 xtoolkit_panel_setup ();
1892 add_widget (midnight_dlg, the_hint);
1893 load_hint (1);
1894 add_widget (midnight_dlg, cmdline);
1895 add_widget (midnight_dlg, the_prompt);
1896 add_widget (midnight_dlg, the_bar);
1898 if (boot_current_is_left) {
1899 first = 1;
1900 second = 0;
1901 } else {
1902 first = 0;
1903 second = 1;
1905 add_widget (midnight_dlg, get_panel_widget (first));
1906 add_widget (midnight_dlg, get_panel_widget (second));
1907 add_widget (midnight_dlg, the_menubar);
1909 init_labels ();
1911 /* Run the Midnight Commander if no file was specified in the command line */
1912 run_dlg (midnight_dlg);
1915 /* result must be free'd (I think this should go in util.c) */
1916 static char *
1917 prepend_cwd_on_local (char *filename)
1919 char *d;
1920 int l;
1922 if (vfs_file_is_local (filename)) {
1923 if (*filename == PATH_SEP) /* an absolute pathname */
1924 return g_strdup (filename);
1925 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1926 mc_get_current_wd (d, MC_MAXPATHLEN);
1927 l = strlen (d);
1928 d[l++] = PATH_SEP;
1929 strcpy (d + l, filename);
1930 return canonicalize_pathname (d);
1931 } else
1932 return g_strdup (filename);
1935 static int
1936 mc_maybe_editor_or_viewer (void)
1938 char *path = NULL;
1940 if (!(view_one_file || edit_one_file))
1941 return 0;
1943 /* Invoke the internal view/edit routine with:
1944 * the default processing and forcing the internal viewer/editor
1946 if (view_one_file) {
1947 path = prepend_cwd_on_local (view_one_file);
1948 setup_dummy_mc (path);
1949 view_file (path, 0, 1);
1951 #ifdef USE_INTERNAL_EDIT
1952 else {
1953 path = prepend_cwd_on_local ("");
1954 setup_dummy_mc (path);
1955 edit (edit_one_file, edit_one_file_start_line);
1957 #endif /* USE_INTERNAL_EDIT */
1958 g_free (path);
1959 midnight_shutdown = 1;
1960 done_mc ();
1961 return 1;
1964 /* Run the main dialog that occupies the whole screen */
1965 static void
1966 do_nc (void)
1968 int midnight_colors[4];
1970 midnight_colors[0] = NORMAL_COLOR; /* NORMALC */
1971 midnight_colors[1] = REVERSE_COLOR; /* FOCUSC */
1972 midnight_colors[2] = INPUT_COLOR; /* HOT_NORMALC */
1973 midnight_colors[3] = NORMAL_COLOR; /* HOT_FOCUSC */
1975 midnight_dlg =
1976 create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1977 "[main]", NULL, DLG_HAS_MENUBAR);
1979 /* Check if we were invoked as an editor or file viewer */
1980 if (mc_maybe_editor_or_viewer ())
1981 return;
1983 setup_mc ();
1985 setup_panels_and_run_mc ();
1987 /* Program end */
1988 midnight_shutdown = 1;
1990 /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */
1991 if (last_wd_file && vfs_current_is_local ()) {
1992 last_wd_string = g_strdup (cpanel->cwd);
1994 done_mc ();
1996 destroy_dlg (midnight_dlg);
1997 current_panel = 0;
1998 done_mc_profile ();
2001 #if defined (NATIVE_WIN32)
2002 /* Windows NT code */
2004 void
2005 OS_Setup (void)
2007 SetConsoleTitle ("GNU Midnight Commander");
2009 shell = getenv ("COMSPEC");
2010 if (!shell || !*shell)
2011 shell = get_default_shell ();
2013 /* Default opening mode for files is binary, not text (CR/LF translation) */
2014 #ifndef __EMX__
2015 _fmode = O_BINARY;
2016 #endif
2018 mc_home = get_mc_lib_dir ();
2021 /* Nothing to be done on Windows */
2022 static void
2023 sigchld_handler_no_subshell (int sig)
2027 void
2028 init_sigchld (void)
2032 void
2033 init_sigfatals (void)
2038 #else
2040 /* Unix version */
2041 static void
2042 OS_Setup (void)
2044 char *mc_libdir;
2045 shell = getenv ("SHELL");
2046 if (!shell || !*shell)
2047 shell = g_strdup (getpwuid (geteuid ())->pw_shell);
2048 if (!shell || !*shell)
2049 shell = "/bin/sh";
2051 /* This is the directory, where MC was installed, on Unix this is DATADIR */
2052 /* and can be overriden by the MC_DATADIR environment variable */
2053 if ((mc_libdir = getenv ("MC_DATADIR")) != NULL) {
2054 mc_home = g_strdup (mc_libdir);
2055 } else {
2056 mc_home = g_strdup (DATADIR);
2060 static void
2061 sigchld_handler_no_subshell (int sig)
2063 #if defined(linux) || defined(__linux__)
2064 int pid, status;
2066 if (!console_flag)
2067 return;
2069 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2070 the value of console_flag never changed, we could simply not install
2071 this handler at all if (!console_flag && !use_subshell). */
2073 /* That comment is no longer true. We need to wait() on a sigchld
2074 handler (that's at least what the tarfs code expects currently). */
2076 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2078 if (pid == cons_saver_pid) {
2080 if (WIFSTOPPED (status)) {
2081 /* Someone has stopped cons.saver - restart it */
2082 kill (pid, SIGCONT);
2083 } else {
2084 /* cons.saver has died - disable console saving */
2085 handle_console (CONSOLE_DONE);
2086 console_flag = 0;
2089 #endif /* linux || __linux__ */
2091 /* If we get here, some other child exited; ignore it */
2094 void
2095 init_sigchld (void)
2097 struct sigaction sigchld_action;
2099 sigchld_action.sa_handler =
2100 #ifdef HAVE_SUBSHELL_SUPPORT
2101 use_subshell ? sigchld_handler :
2102 #endif /* HAVE_SUBSHELL_SUPPORT */
2103 sigchld_handler_no_subshell;
2105 sigemptyset (&sigchld_action.sa_mask);
2107 #ifdef SA_RESTART
2108 sigchld_action.sa_flags = SA_RESTART;
2109 #else
2110 sigchld_action.sa_flags = 0;
2111 #endif /* !SA_RESTART */
2113 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2114 #ifdef HAVE_SUBSHELL_SUPPORT
2116 * This may happen on QNX Neutrino 6, where SA_RESTART
2117 * is defined but not implemented. Fallback to no subshell.
2119 use_subshell = 0;
2120 #endif /* HAVE_SUBSHELL_SUPPORT */
2124 #endif /* NATIVE_WIN32, UNIX */
2126 static void
2127 print_mc_usage (poptContext ctx, FILE *stream)
2129 int leftColWidth;
2131 poptSetOtherOptionHelp (ctx,
2132 _("[flags] [this_dir] [other_panel_dir]\n"));
2134 /* print help for options */
2135 leftColWidth = poptPrintHelp (ctx, stream, 0);
2136 fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
2137 _("Set initial line number for the internal editor"));
2138 fputs (_
2139 ("\n"
2140 "Please send any bug reports (including the output of `mc -V')\n"
2141 "to mc-devel@gnome.org\n"), stream);
2142 version (0);
2145 static void
2146 print_color_usage (void)
2149 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
2150 * and editmarked. To preserve translations, lines should be split.
2152 /* TRANSLATORS: don't translate keywords and names of colors */
2153 fputs (_
2154 ("--colors KEYWORD={FORE},{BACK}\n\n"
2155 "{FORE} and {BACK} can be omitted, and the default will be used\n"
2156 "\n" "Keywords:\n"
2157 " Global: errors, reverse, gauge, input\n"
2158 " File display: normal, selected, marked, markselect\n"
2159 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus\n"
2160 " Menus: menu, menuhot, menusel, menuhotsel\n"
2161 " Help: helpnormal, helpitalic, helplink, helpslink\n"
2162 " File types: directory, executable, link, stalelink, device, special, core\n"
2163 "\n" "Colors:\n"
2164 " black, gray, red, brightred, green, brightgreen, brown,\n"
2165 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
2166 " brightcyan, lightgray and white\n\n"), stdout);
2169 static void
2170 process_args (poptContext ctx, int c, const char *option_arg)
2172 switch (c) {
2173 case 'V':
2174 version (1);
2175 exit (0);
2176 break;
2178 case 'c':
2179 disable_colors = 0;
2180 #ifdef HAVE_SLANG
2181 force_colors = 1;
2182 #endif /* HAVE_SLANG */
2183 break;
2185 case 'f':
2186 printf ("%s\n", mc_home);
2187 exit (0);
2188 break;
2190 #ifdef USE_NETCODE
2191 case 'l':
2192 ftpfs_set_debug (option_arg);
2193 #ifdef WITH_SMBFS
2194 smbfs_set_debugf (option_arg);
2195 #endif /* WITH_SMBFS */
2196 break;
2198 #ifdef WITH_SMBFS
2199 case 'D':
2200 smbfs_set_debug (atoi (option_arg));
2201 break;
2202 #endif /* WITH_SMBFS */
2203 #endif /* USE_NETCODE */
2205 case 'd':
2206 use_mouse_p = MOUSE_DISABLED;
2207 break;
2209 #ifdef HAVE_SUBSHELL_SUPPORT
2210 case 'u':
2211 use_subshell = 0;
2212 break;
2213 #endif /* HAVE_SUBSHELL_SUPPORT */
2215 case 'H':
2216 print_color_usage ();
2217 exit (0);
2218 break;
2220 case 'h':
2221 print_mc_usage (ctx, stdout);
2222 exit (0);
2226 static const struct poptOption argument_table[] = {
2227 #ifdef WITH_BACKGROUND
2228 {"background", 'B', POPT_ARG_NONE, &background_wait, 0,
2229 N_("Use to debug the background code")},
2230 #endif
2231 {"color", 'c', POPT_ARG_NONE, NULL, 'c',
2232 N_("Request to run in color mode")},
2233 {"colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
2234 N_("Specifies a color configuration")},
2236 #ifdef USE_INTERNAL_EDIT
2237 {"edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2238 N_("Edits one file")},
2239 #endif
2241 {"help", 'h', POPT_ARG_NONE, NULL, 'h',
2242 N_("Displays this help message")},
2243 {"help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
2244 N_("Displays a help screen on how to change the color scheme")},
2245 #ifdef USE_NETCODE
2246 {"ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
2247 N_("Log ftp dialog to specified file")},
2248 #ifdef WITH_SMBFS
2249 {"debuglevel", 'D', POPT_ARG_STRING, NULL, 'D',
2250 N_("Set debug level")},
2251 #endif
2252 #endif
2253 {"datadir", 'f', POPT_ARG_NONE, NULL, 'f',
2254 N_("Print data directory")},
2255 {"nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
2256 N_("Requests to run in black and white")},
2257 {"nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
2258 N_("Disable mouse support in text version")},
2259 #ifdef HAVE_SUBSHELL_SUPPORT
2260 {"nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2261 N_("Disables subshell support")},
2262 {"forceexec", 'r', POPT_ARG_NONE, &force_subshell_execution, 0,
2263 N_("Force subshell execution")},
2264 #endif
2265 {"printwd", 'P', POPT_ARG_STRING, &last_wd_file, 0,
2266 N_("Print last working directory to specified file")},
2267 {"resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
2268 N_("Resets soft keys on HP terminals")},
2269 {"slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
2270 N_("To run on slow terminals")},
2271 #ifndef NATIVE_WIN32
2272 {"stickchars", 'a', 0, &force_ugly_line_drawing, 0,
2273 N_("Use stickchars to draw")},
2274 #endif /* !NATIVE_WIN32 */
2275 #ifdef HAVE_SUBSHELL_SUPPORT
2276 {"subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2277 N_("Enables subshell support (default)")},
2278 #endif
2279 #if defined(HAVE_SLANG) && !defined(NATIVE_WIN32)
2280 {"termcap", 't', 0, &SLtt_Try_Termcap, 0,
2281 N_("Tries to use termcap instead of terminfo")},
2282 #endif
2283 {"version", 'V', POPT_ARG_NONE, NULL, 'V',
2284 N_("Displays the current version")},
2285 {"view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2286 N_("Launches the file viewer on a file")},
2287 {"xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
2288 N_("Forces xterm features")},
2290 {NULL, 0, 0, NULL, 0}
2293 static void
2294 handle_args (int argc, char *argv[])
2296 char *tmp;
2297 poptContext ctx;
2298 char *base;
2299 int c;
2301 ctx =
2302 poptGetContext ("mc", argc, argv, argument_table,
2303 POPT_CONTEXT_NO_EXEC);
2305 #ifdef USE_TERMCAP
2306 SLtt_Try_Termcap = 1;
2307 #endif
2309 while ((c = poptGetNextOpt (ctx)) > 0) {
2310 process_args (ctx, c, poptGetOptArg (ctx));
2313 if (c < -1) {
2314 print_mc_usage (ctx, stderr);
2315 fprintf (stderr, "%s: %s\n",
2316 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2317 poptStrerror (c));
2318 exit (1);
2321 tmp = poptGetArg (ctx);
2324 * Check for special invocation names mcedit and mcview,
2325 * if none apply then set the current directory and the other
2326 * directory from the command line arguments
2328 base = x_basename (argv[0]);
2329 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2330 edit_one_file = "";
2331 if (tmp) {
2332 if (*tmp == '+' && isdigit ((unsigned char) tmp[1])) {
2333 int start_line = atoi (tmp);
2334 if (start_line > 0) {
2335 char *file = poptGetArg (ctx);
2336 if (file) {
2337 tmp = file;
2338 edit_one_file_start_line = start_line;
2342 edit_one_file = g_strdup (tmp);
2344 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2345 if (tmp)
2346 view_one_file = g_strdup (tmp);
2347 else {
2348 fputs ("No arguments given to the viewer\n", stderr);
2349 exit (1);
2351 } else {
2352 /* sets the current dir and the other dir */
2353 if (tmp) {
2354 this_dir = g_strdup (tmp);
2355 if ((tmp = poptGetArg (ctx)))
2356 other_dir = g_strdup (tmp);
2360 poptFreeContext (ctx);
2364 * The compatibility_move_mc_files routine is intended to
2365 * move all of the hidden .mc files into a private ~/.mc
2366 * directory in the home directory, to avoid cluttering the users.
2368 * Previous versions of the program had all of their files in
2369 * the $HOME, we are now putting them in $HOME/.mc
2371 #ifdef NATIVE_WIN32
2372 # define compatibility_move_mc_files() 0
2373 #else
2375 static int
2376 do_mc_filename_rename (char *mc_dir, char *o_name, char *n_name)
2378 char *full_o_name = concat_dir_and_file (home_dir, o_name);
2379 char *full_n_name = g_strconcat (home_dir, MC_BASE, n_name, NULL);
2380 int move;
2382 move = 0 == rename (full_o_name, full_n_name);
2383 g_free (full_o_name);
2384 g_free (full_n_name);
2385 return move;
2388 static int
2389 compatibility_move_mc_files (void)
2391 struct stat s;
2392 int move = 0;
2393 char *mc_dir = concat_dir_and_file (home_dir, ".mc");
2395 if (stat (mc_dir, &s) && (errno == ENOENT)
2396 && (mkdir (mc_dir, 0777) != -1)) {
2398 move = do_mc_filename_rename (mc_dir, ".mc.ini", "ini");
2399 move += do_mc_filename_rename (mc_dir, ".mc.hot", "hotlist");
2400 move += do_mc_filename_rename (mc_dir, ".mc.ext", "bindings");
2401 move += do_mc_filename_rename (mc_dir, ".mc.menu", "menu");
2402 move += do_mc_filename_rename (mc_dir, ".mc.bashrc", "bashrc");
2403 move += do_mc_filename_rename (mc_dir, ".mc.inputrc", "inputrc");
2404 move += do_mc_filename_rename (mc_dir, ".mc.tcshrc", "tcshrc");
2405 move += do_mc_filename_rename (mc_dir, ".mc.tree", "Tree");
2407 g_free (mc_dir);
2408 return move;
2410 #endif /* NATIVE_WIN32 */
2413 main (int argc, char *argv[])
2415 /* if on, it displays the information that files have been moved to ~/.mc */
2416 int show_change_notice = 0;
2418 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2419 setlocale (LC_ALL, "");
2420 bindtextdomain ("mc", LOCALEDIR);
2421 textdomain ("mc");
2423 /* Initialize list of all user group for timur_clr_mode */
2424 init_groups ();
2426 /* Set up temporary directory */
2427 mc_tmpdir ();
2429 OS_Setup ();
2431 /* This variable is used by the subshell */
2432 home_dir = getenv ("HOME");
2433 if (!home_dir) {
2434 /* mc_home was computed by OS_Setup */
2435 home_dir = mc_home;
2438 vfs_init ();
2440 #ifdef HAVE_SLANG
2441 SLtt_Ignore_Beep = 1;
2442 #endif
2444 /* NOTE: This has to be called before slang_init or whatever routine
2445 calls any define_sequence */
2446 init_key ();
2448 handle_args (argc, argv);
2450 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2451 handle_console (CONSOLE_INIT);
2453 #ifdef HAVE_SUBSHELL_SUPPORT
2454 /* Don't use subshell when invoked as viewer or editor */
2455 if (edit_one_file || view_one_file)
2456 use_subshell = 0;
2458 if (use_subshell)
2459 subshell_get_console_attributes ();
2460 #endif /* HAVE_SUBSHELL_SUPPORT */
2462 /* Install the SIGCHLD handler; must be done before init_subshell() */
2463 init_sigchld ();
2465 show_change_notice = compatibility_move_mc_files ();
2467 /* We need this, since ncurses endwin () doesn't restore the signals */
2468 save_stop_handler ();
2470 /* Must be done before init_subshell, to set up the terminal size: */
2471 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2472 #ifdef HAVE_SLANG
2473 slang_init ();
2474 #endif
2475 /* NOTE: This call has to be after slang_init. It's the small part from
2476 the previous init_key which had to be moved after the call of slang_init */
2477 init_key_input_fd ();
2479 load_setup ();
2481 init_curses ();
2483 init_xterm_support ();
2485 #ifdef HAVE_SUBSHELL_SUPPORT
2487 /* Done here to ensure that the subshell doesn't */
2488 /* inherit the file descriptors opened below, etc */
2489 if (use_subshell)
2490 init_subshell ();
2492 #endif /* HAVE_SUBSHELL_SUPPORT */
2494 /* Removing this from the X code let's us type C-c */
2495 load_key_defs ();
2497 /* Also done after init_subshell, to save any shell init file messages */
2498 if (console_flag)
2499 handle_console (CONSOLE_SAVE);
2501 if (alternate_plus_minus)
2502 application_keypad_mode ();
2504 if (show_change_notice) {
2505 message (1, _(" Notice "),
2506 _(" The Midnight Commander configuration files \n"
2507 " are now stored in the ~/.mc directory, the \n"
2508 " files have been moved now\n"));
2510 #ifdef HAVE_SUBSHELL_SUPPORT
2511 if (use_subshell) {
2512 prompt = strip_ctrl_codes (subshell_prompt);
2513 if (!prompt)
2514 prompt = "";
2515 } else
2516 #endif /* HAVE_SUBSHELL_SUPPORT */
2517 prompt = (geteuid () == 0) ? "# " : "$ ";
2519 /* Program main loop */
2520 do_nc ();
2522 /* Save the tree store */
2523 tree_store_save ();
2525 /* Virtual File System shutdown */
2526 vfs_shut ();
2528 /* Delete list of all user groups */
2529 destroy_groups ();
2531 flush_extension_file (); /* does only free memory */
2533 endwin ();
2534 #ifdef HAVE_SLANG
2535 slang_shutdown ();
2536 #endif
2538 if (console_flag && !(quit & SUBSHELL_EXIT))
2539 restore_console ();
2540 if (alternate_plus_minus)
2541 numeric_keypad_mode ();
2543 #ifndef NATIVE_WIN32
2544 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2545 #endif
2547 if (console_flag)
2548 handle_console (CONSOLE_DONE);
2549 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2551 #ifdef NATIVE_WIN32
2552 /* On NT, home_dir is malloced */
2553 g_free (home_dir);
2554 #endif
2555 if (last_wd_file && last_wd_string && !print_last_revert
2556 && !edit_one_file && !view_one_file) {
2557 int last_wd_fd =
2558 open (last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2559 S_IRUSR | S_IWUSR);
2561 if (last_wd_fd != -1) {
2562 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2563 close (last_wd_fd);
2566 g_free (last_wd_string);
2568 #ifndef NATIVE_WIN32
2569 g_free (mc_home);
2570 #endif /* NATIVE_WIN32 */
2571 done_key ();
2572 #ifdef HAVE_CHARSET
2573 free_codepages_list ();
2574 #endif
2575 if (this_dir)
2576 g_free (this_dir);
2577 if (other_dir)
2578 g_free (other_dir);
2580 return 0;