Just a little correction at the it.po file.
[midnight-commander.git] / src / main.c
bloba039e8a89ffb971ffe4a3b344c1ad08d68f89e5d
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 program softkeys (HP terminals only) on startup and after every
183 command ran in the subshell to the description found in the termcap/terminfo
184 database */
185 int reset_hp_softkeys = 0;
187 /* The prompt */
188 char *prompt = 0;
190 /* The widget where we draw the prompt */
191 WLabel *the_prompt;
193 /* The hint bar */
194 WLabel *the_hint;
196 /* The button bar */
197 WButtonBar *the_bar;
199 /* For slow terminals */
200 int slow_terminal = 0;
202 /* Mouse type: GPM, xterm or none */
203 Mouse_Type use_mouse_p = MOUSE_NONE;
205 /* If true, assume we are running on an xterm terminal */
206 static int force_xterm = 0;
208 /* Controls screen clearing before an exec */
209 int clear_before_exec = 1;
211 /* Asks for confirmation before deleting a file */
212 int confirm_delete = 1;
214 /* Asks for confirmation before overwriting a file */
215 int confirm_overwrite = 1;
217 /* Asks for confirmation before executing a program by pressing enter */
218 int confirm_execute = 0;
220 /* Asks for confirmation before leaving the program */
221 int confirm_exit = 1;
223 /* Asks for confirmation when using F3 to view a directory and there
224 are tagged files */
225 int confirm_view_dir = 0;
227 /* This flag indicates if the pull down menus by default drop down */
228 int drop_menus = 0;
230 /* The dialog handle for the main program */
231 Dlg_head *midnight_dlg;
233 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
234 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
235 int update_prompt = 0;
237 #if 0
238 /* The name which was used to invoke mc */
239 char *program_name;
240 #endif
242 /* The home directory */
243 char *home_dir;
245 /* The value of the other directory, only used when loading the setup */
246 char *other_dir = 0;
248 /* Only used at program boot */
249 int boot_current_is_left = 1;
251 char *this_dir = 0;
253 /* If this is true, then when browsing the tree the other window will
254 * automatically reload it's directory with the contents of the currently
255 * selected directory.
257 int xtree_mode = 0;
259 /* If set, then print to the given file the last directory we were at */
260 static char *last_wd_file;
261 static char *last_wd_string;
262 /* Set to 1 to suppress printing the last directory */
263 static int print_last_revert = 0;
265 /* Force colors, only used by Slang */
266 int force_colors = 0;
268 /* colors specified on the command line: they override any other setting */
269 char *command_line_colors;
271 /* File name to view if argument was supplied */
272 char *view_one_file = 0;
274 /* File name to edit if argument was supplied */
275 char *edit_one_file = 0;
277 /* Line to start the editor on */
278 static int edit_one_file_start_line = 0;
280 /* Used so that widgets know if they are being destroyed or
281 shut down */
282 int midnight_shutdown = 0;
284 /* to show nice prompts */
285 static int last_paused = 0;
287 /* Used for keeping track of the original stdout */
288 int stdout_fd = 0;
290 /* The user's shell */
291 char *shell;
293 /* mc_home: The home of MC */
294 char *mc_home;
296 char cmd_buf[512];
298 WPanel *
299 get_current_panel (void)
301 return current_panel;
304 WPanel *
305 get_other_panel (void)
307 return (WPanel *) get_panel_widget (get_other_index ());
310 static void
311 reload_panelized (WPanel *panel)
313 int i, j;
314 dir_list *list = &panel->dir;
316 if (panel != cpanel)
317 mc_chdir (panel->cwd);
319 for (i = 0, j = 0; i < panel->count; i++) {
320 if (list->list[i].f.marked) {
321 /* Unmark the file in advance. In case the following mc_lstat
322 * fails we are done, else we have to mark the file again
323 * (Note: do_file_mark depends on a valid "list->list [i].buf").
324 * IMO that's the best way to update the panel's summary status
325 * -- Norbert
327 do_file_mark (panel, i, 0);
329 if (mc_lstat (list->list[i].fname, &list->list[i].buf)) {
330 g_free (list->list[i].fname);
331 continue;
333 if (list->list[i].f.marked)
334 do_file_mark (panel, i, 1);
335 if (j != i)
336 list->list[j] = list->list[i];
337 j++;
339 if (j == 0)
340 panel->count = set_zero_dir (list);
341 else
342 panel->count = j;
344 if (panel != cpanel)
345 mc_chdir (cpanel->cwd);
348 static void
349 update_one_panel_widget (WPanel *panel, int force_update,
350 char *current_file)
352 int free_pointer;
354 if (force_update & UP_RELOAD) {
355 panel->is_panelized = 0;
357 #if 1
358 ftpfs_flushdir ();
359 #endif
360 /* FIXME: Should supply flushdir method */
361 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
364 /* If current_file == -1 (an invalid pointer) then preserve selection */
365 if (current_file == UP_KEEPSEL) {
366 free_pointer = 1;
367 current_file = g_strdup (panel->dir.list[panel->selected].fname);
368 } else
369 free_pointer = 0;
371 if (panel->is_panelized)
372 reload_panelized (panel);
373 else
374 panel_reload (panel);
376 try_to_select (panel, current_file);
377 panel->dirty = 1;
379 if (free_pointer)
380 g_free (current_file);
383 void
384 panel_clean_dir (WPanel *panel)
386 int count = panel->count;
388 panel->count = 0;
389 panel->top_file = 0;
390 panel->selected = 0;
391 panel->marked = 0;
392 panel->dirs_marked = 0;
393 panel->total = 0;
394 panel->searching = 0;
395 panel->is_panelized = 0;
397 clean_dir (&panel->dir, count);
400 static void
401 update_one_panel (int which, int force_update, char *current_file)
403 WPanel *panel;
405 if (get_display_type (which) != view_listing)
406 return;
408 panel = (WPanel *) get_panel_widget (which);
409 update_one_panel_widget (panel, force_update, current_file);
412 /* This routine reloads the directory in both panels. It tries to
413 * select current_file in current_panel and other_file in other_panel.
414 * If current_file == -1 then it automatically sets current_file and
415 * other_file to the currently selected files in the panels.
417 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
418 * will not reload the other panel.
420 void
421 update_panels (int force_update, char *current_file)
423 int reload_other = !(force_update & UP_ONLY_CURRENT);
424 WPanel *panel;
426 update_one_panel (get_current_index (), force_update, current_file);
427 if (reload_other)
428 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
430 if (get_current_type () == view_listing)
431 panel = (WPanel *) get_panel_widget (get_current_index ());
432 else
433 panel = (WPanel *) get_panel_widget (get_other_index ());
435 mc_chdir (panel->cwd);
438 /* Sets up the terminal before executing a program */
439 static void
440 pre_exec (void)
442 use_dash (0);
443 edition_pre_exec ();
446 /* Save current stat of directories to avoid reloading the panels */
447 /* when no modifications have taken place */
448 void
449 save_cwds_stat (void)
451 if (fast_reload) {
452 mc_stat (cpanel->cwd, &(cpanel->dir_stat));
453 if (get_other_type () == view_listing)
454 mc_stat (opanel->cwd, &(opanel->dir_stat));
458 #ifdef HAVE_SUBSHELL_SUPPORT
459 void
460 do_possible_cd (char *new_dir)
462 if (!do_cd (new_dir, cd_exact))
463 message (1, _("Warning"),
464 _(" The Commander can't change to the directory that \n"
465 " the subshell claims you are in. Perhaps you have \n"
466 " deleted your working directory, or given yourself \n"
467 " extra access permissions with the \"su\" command? "));
470 void
471 do_update_prompt (void)
473 if (update_prompt) {
474 printf ("%s", subshell_prompt);
475 fflush (stdout);
476 update_prompt = 0;
479 #endif /* HAVE_SUBSHELL_SUPPORT */
481 void
482 restore_console (void)
484 handle_console (CONSOLE_RESTORE);
487 void
488 exec_shell (void)
490 do_execute (shell, 0, 0);
493 void
494 do_execute (const char *shell, const char *command, int flags)
496 #ifdef HAVE_SUBSHELL_SUPPORT
497 char *new_dir = NULL;
498 #endif /* HAVE_SUBSHELL_SUPPORT */
500 #ifdef USE_VFS
501 char *old_vfs_dir = 0;
503 if (!vfs_current_is_local ())
504 old_vfs_dir = g_strdup (vfs_get_current_dir ());
505 #endif /* USE_VFS */
507 save_cwds_stat ();
508 pre_exec ();
509 if (console_flag)
510 restore_console ();
512 if (!use_subshell && !(flags & EXECUTE_INTERNAL && command)) {
513 printf ("%s%s%s\n", last_paused ? "\r\n" : "", prompt, command);
514 last_paused = 0;
516 #ifdef HAVE_SUBSHELL_SUPPORT
517 if (use_subshell && !(flags & EXECUTE_INTERNAL)) {
518 do_update_prompt ();
520 /* We don't care if it died, higher level takes care of this */
521 #ifdef USE_VFS
522 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
523 #else
524 invoke_subshell (command, VISIBLY, &new_dir);
525 #endif /* !USE_VFS */
526 } else
527 #endif /* HAVE_SUBSHELL_SUPPORT */
528 my_system (flags, shell, command);
530 if (!(flags & EXECUTE_INTERNAL)) {
531 if ((pause_after_run == pause_always
532 || (pause_after_run == pause_on_dumb_terminals && !xterm_flag
533 && !console_flag)) && !quit
534 #ifdef HAVE_SUBSHELL_SUPPORT
535 && subshell_state != RUNNING_COMMAND
536 #endif /* HAVE_SUBSHELL_SUPPORT */
538 printf ("%s\r\n", _("Press any key to continue..."));
539 last_paused = 1;
540 fflush (stdout);
541 mc_raw_mode ();
542 getch ();
544 if (console_flag) {
545 if (output_lines && keybar_visible) {
546 putchar ('\n');
547 fflush (stdout);
552 if (console_flag)
553 handle_console (CONSOLE_SAVE);
554 edition_post_exec ();
556 #ifdef HAVE_SUBSHELL_SUPPORT
557 if (new_dir)
558 do_possible_cd (new_dir);
560 #endif /* HAVE_SUBSHELL_SUPPORT */
562 #ifdef USE_VFS
563 if (old_vfs_dir) {
564 mc_chdir (old_vfs_dir);
565 g_free (old_vfs_dir);
567 #endif /* USE_VFS */
569 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
570 update_xterm_title_path ();
572 do_refresh ();
573 use_dash (TRUE);
576 /* Executes a command */
577 void
578 shell_execute (char *command, int flags)
580 #ifdef HAVE_SUBSHELL_SUPPORT
581 if (use_subshell)
582 if (subshell_state == INACTIVE)
583 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
584 else
585 message (1, MSG_ERROR,
586 _(" The shell is already running a command "));
587 else
588 #endif /* HAVE_SUBSHELL_SUPPORT */
589 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
592 void
593 execute (char *command)
595 shell_execute (command, 0);
598 void
599 change_panel (void)
601 free_completions (cmdline);
602 dlg_one_down (midnight_dlg);
605 /* Stop MC main dialog and the current dialog if it exists.
606 * Needed to provide fast exit from MC viewer or editor on shell exit */
607 static void
608 stop_dialogs (void)
610 midnight_dlg->running = 0;
611 if (current_dlg) {
612 current_dlg->running = 0;
616 static int
617 quit_cmd_internal (int quiet)
619 int q = quit;
621 if (quiet || !confirm_exit) {
622 q = 1;
623 } else {
624 if (query_dialog
625 (_(" The Midnight Commander "),
626 _(" Do you really want to quit the Midnight Commander? "), 0,
627 2, _("&Yes"), _("&No")) == 0)
628 q = 1;
630 if (q) {
631 #ifdef HAVE_SUBSHELL_SUPPORT
632 if (!use_subshell)
633 stop_dialogs ();
634 else if ((q = exit_subshell ()))
635 #endif
636 stop_dialogs ();
638 if (q)
639 quit |= 1;
640 return quit;
643 static void
644 quit_cmd (void)
646 quit_cmd_internal (0);
650 quiet_quit_cmd (void)
652 print_last_revert = 1;
653 quit_cmd_internal (1);
654 return quit;
658 * Touch window and refresh window functions
661 /* This routine untouches the first line on both panels in order */
662 /* to avoid the refreshing the menu bar */
664 void
665 untouch_bar (void)
667 do_refresh ();
670 void
671 repaint_screen (void)
673 do_refresh ();
674 mc_refresh ();
677 /* Wrapper for do_subshell_chdir, check for availability of subshell */
678 void
679 subshell_chdir (char *directory)
681 #ifdef HAVE_SUBSHELL_SUPPORT
682 if (use_subshell) {
683 if (vfs_current_is_local ())
684 do_subshell_chdir (directory, 0, 1);
686 #endif /* HAVE_SUBSHELL_SUPPORT */
689 void
690 directory_history_add (struct WPanel *panel, char *s)
692 char *text;
694 text = g_strdup (s);
695 strip_password (s, 1);
697 panel->dir_history = list_append_unique (panel->dir_history, text);
701 * If we moved to the parent directory move the selection pointer to
702 * the old directory name; If we leave VFS dir, remove FS specificator.
703 * Warn: This code spoils lwd string.
705 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
707 static char *
708 get_parent_dir_name (char *cwd, char *lwd)
710 char *p;
711 if (strlen (lwd) > strlen (cwd))
712 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd)) {
713 return (p + 1);
715 return NULL;
719 * Changes the current directory of the panel.
720 * Don't record change in the directory history.
722 static int
723 _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
725 char *directory, *olddir;
726 char temp[MC_MAXPATHLEN];
727 char *translated_url;
728 #ifdef USE_VFS
729 vfs *oldvfs;
730 vfsid oldvfsid;
731 struct vfs_stamping *parent;
732 #endif
733 olddir = g_strdup (panel->cwd);
734 translated_url = new_dir = vfs_translate_url (new_dir);
736 /* Convert *new_path to a suitable pathname, handle ~user */
738 if (cd_type == cd_parse_command) {
739 while (*new_dir == ' ')
740 new_dir++;
742 if (!strcmp (new_dir, "-")) {
743 strcpy (temp, panel->lwd);
744 new_dir = temp;
747 directory = *new_dir ? new_dir : home_dir;
749 if (mc_chdir (directory) == -1) {
750 strcpy (panel->cwd, olddir);
751 g_free (olddir);
752 g_free (translated_url);
753 return 0;
755 g_free (translated_url);
757 /* Success: save previous directory, shutdown status of previous dir */
758 strcpy (panel->lwd, olddir);
759 free_completions (cmdline);
761 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
763 #ifdef USE_VFS
764 oldvfs = vfs_type (olddir);
765 oldvfsid = vfs_ncs_getid (oldvfs, olddir, &parent);
766 vfs_add_noncurrent_stamps (oldvfs, oldvfsid, parent);
767 vfs_rm_parents (parent);
768 #endif
770 subshell_chdir (panel->cwd);
772 /* Reload current panel */
773 panel_clean_dir (panel);
774 panel->count =
775 do_load_dir (&panel->dir, panel->sort_type, panel->reverse,
776 panel->case_sensitive, panel->filter);
777 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
778 load_hint (0);
779 panel_update_contents (panel);
780 update_xterm_title_path ();
782 g_free (olddir);
784 return 1;
788 * Changes the current directory of the panel.
789 * Record change in the directory history.
792 do_panel_cd (struct WPanel *panel, char *new_dir, enum cd_enum cd_type)
794 int r;
796 r = _do_panel_cd (panel, new_dir, cd_type);
797 if (r)
798 directory_history_add (panel, panel->cwd);
799 return r;
803 do_cd (char *new_dir, enum cd_enum exact)
805 return (do_panel_cd (cpanel, new_dir, exact));
808 void
809 directory_history_next (WPanel *panel)
811 GList *nextdir;
813 nextdir = g_list_next (panel->dir_history);
815 if (!nextdir)
816 return;
818 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
819 panel->dir_history = nextdir;
822 void
823 directory_history_prev (WPanel *panel)
825 GList *prevdir;
827 prevdir = g_list_previous (panel->dir_history);
829 if (!prevdir)
830 return;
832 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
833 panel->dir_history = prevdir;
836 void
837 directory_history_list (WPanel *panel)
839 char *s;
841 if (!panel->dir_history)
842 return;
844 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
846 if (!s)
847 return;
849 if (_do_panel_cd (panel, s, cd_exact))
850 directory_history_add (panel, panel->cwd);
851 else
852 message (1, MSG_ERROR, _("Cannot change directory"));
853 g_free (s);
856 #ifdef HAVE_SUBSHELL_SUPPORT
858 load_prompt (int fd, void *unused)
860 if (!read_subshell_prompt ())
861 return 0;
863 /* Don't actually change the prompt if it's invisible */
864 if (current_dlg == midnight_dlg && command_prompt) {
865 int prompt_len;
867 prompt = strip_ctrl_codes (subshell_prompt);
868 prompt_len = strlen (prompt);
870 /* Check for prompts too big */
871 if (COLS > 8 && prompt_len > COLS - 8) {
872 prompt[COLS - 8] = 0;
873 prompt_len = COLS - 8;
875 label_set_text (the_prompt, prompt);
876 winput_set_origin ((WInput *) cmdline, prompt_len,
877 COLS - prompt_len);
879 /* since the prompt has changed, and we are called from one of the
880 * get_event channels, the prompt updating does not take place
881 * automatically: force a cursor update and a screen refresh
883 update_cursor (midnight_dlg);
884 mc_refresh ();
886 update_prompt = 1;
887 return 0;
889 #endif /* HAVE_SUBSHELL_SUPPORT */
891 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
893 maybe_cd (int char_code, int move_up_dir)
895 if (navigate_with_arrows) {
896 if (!cmdline->buffer[0]) {
897 if (!move_up_dir) {
898 do_cd ("..", cd_exact);
899 return 1;
901 if (S_ISDIR (selection (cpanel)->buf.st_mode)
902 || link_isdir (selection (cpanel))) {
903 do_cd (selection (cpanel)->fname, cd_exact);
904 return 1;
908 return 0;
911 static void
912 sort_cmd (void)
914 WPanel *p;
915 sortfn *sort_order;
917 if (!SELECTED_IS_PANEL)
918 return;
920 p = MENU_PANEL;
921 sort_order = sort_box (p->sort_type, &p->reverse, &p->case_sensitive);
923 panel_set_sort_order (p, sort_order);
927 static void
928 treebox_cmd (void)
930 char *sel_dir;
932 sel_dir = tree_box (selection (cpanel)->fname);
933 if (sel_dir) {
934 do_cd (sel_dir, cd_exact);
935 g_free (sel_dir);
939 #ifdef LISTMODE_EDITOR
940 static void
941 listmode_cmd (void)
943 char *newmode;
944 newmode = listmode_edit ("half <type,>name,|,size:8,|,perm:4+");
945 message (0, _(" Listing format edit "), _(" New mode is \"%s\" "),
946 newmode);
947 g_free (newmode);
949 #endif /* LISTMODE_EDITOR */
951 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
952 static menu_entry PanelMenu[] = {
953 {' ', N_("&Listing mode..."), 'L', listing_cmd},
954 {' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd},
955 {' ', N_("&Info C-x i"), 'I', info_cmd},
956 {' ', N_("&Tree"), 'T', tree_cmd},
957 {' ', "", ' ', 0},
958 {' ', N_("&Sort order..."), 'S', sort_cmd},
959 {' ', "", ' ', 0},
960 {' ', N_("&Filter..."), 'F', filter_cmd},
961 #ifdef USE_NETCODE
962 {' ', "", ' ', 0},
963 #ifdef WITH_MCFS
964 {' ', N_("&Network link..."), 'N', netlink_cmd},
965 #endif
966 {' ', N_("FT&P link..."), 'P', ftplink_cmd},
967 {' ', N_("S&hell link..."), 'H', fishlink_cmd},
968 #ifdef WITH_SMBFS
969 {' ', N_("SM&B link..."), 'B', smblink_cmd},
970 #endif
971 #endif
972 {' ', "", ' ', 0},
973 #ifdef NATIVE_WIN32
974 {' ', N_("&Drive... M-d"), 'D', drive_cmd_a},
975 #endif
976 {' ', N_("&Rescan C-r"), 'R', reread_cmd}
979 static menu_entry RightMenu[] = {
980 {' ', N_("&Listing mode..."), 'L', listing_cmd},
981 {' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd},
982 {' ', N_("&Info C-x i"), 'I', info_cmd},
983 {' ', N_("&Tree"), 'T', tree_cmd},
984 {' ', "", ' ', 0},
985 {' ', N_("&Sort order..."), 'S', sort_cmd},
986 {' ', "", ' ', 0},
987 {' ', N_("&Filter..."), 'F', filter_cmd},
988 #ifdef USE_NETCODE
989 {' ', "", ' ', 0},
990 #ifdef WITH_MCFS
991 {' ', N_("&Network link..."), 'N', netlink_cmd},
992 #endif
993 {' ', N_("FT&P link..."), 'P', ftplink_cmd},
994 {' ', N_("S&hell link..."), 'H', fishlink_cmd},
995 #ifdef WITH_SMBFS
996 {' ', N_("SM&B link..."), 'B', smblink_cmd},
997 #endif
998 #endif
999 {' ', "", ' ', 0},
1000 #ifdef NATIVE_WIN32
1001 {' ', N_("&Drive... M-d"), 'D', drive_cmd_b},
1002 #endif
1003 {' ', N_("&Rescan C-r"), 'R', reread_cmd}
1006 static menu_entry FileMenu[] = {
1007 {' ', N_("&User menu F2"), 'U', user_file_menu_cmd},
1008 {' ', N_("&View F3"), 'V', view_cmd},
1009 {' ', N_("Vie&w file... "), 'W', view_file_cmd},
1010 {' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd},
1011 {' ', N_("&Edit F4"), 'E', edit_cmd},
1012 {' ', N_("&Copy F5"), 'C', copy_cmd},
1013 {' ', N_("c&Hmod C-x c"), 'H', chmod_cmd},
1014 #ifndef NATIVE_WIN32
1015 {' ', N_("&Link C-x l"), 'L', link_cmd},
1016 {' ', N_("&SymLink C-x s"), 'S', symlink_cmd},
1017 {' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd},
1018 {' ', N_("ch&Own C-x o"), 'O', chown_cmd},
1019 {' ', N_("&Advanced chown "), 'A', chown_advanced_cmd},
1020 #endif
1021 {' ', N_("&Rename/Move F6"), 'R', ren_cmd},
1022 {' ', N_("&Mkdir F7"), 'M', mkdir_cmd},
1023 {' ', N_("&Delete F8"), 'D', delete_cmd},
1024 {' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd},
1025 {' ', "", ' ', 0},
1026 {' ', N_("select &Group M-+"), 'G', select_cmd},
1027 {' ', N_("u&Nselect group M-\\"), 'N', unselect_cmd},
1028 {' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd},
1029 {' ', "", ' ', 0},
1030 {' ', N_("e&Xit F10"), 'X', quit_cmd}
1033 static menu_entry CmdMenu[] = {
1034 /* I know, I'm lazy, but the tree widget when it's not running
1035 * as a panel still has some problems, I have not yet finished
1036 * the WTree widget port, sorry.
1038 {' ', N_("&Directory tree"), 'D', treebox_cmd},
1039 {' ', N_("&Find file M-?"), 'F', find_cmd},
1040 {' ', N_("s&Wap panels C-u"), 'W', swap_cmd},
1041 {' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd},
1042 {' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd},
1043 {' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize},
1044 {' ', N_("show directory s&Izes"), 'I', dirsizes_cmd},
1045 {' ', "", ' ', 0},
1046 {' ', N_("command &History"), 'H', history_cmd},
1047 {' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd},
1048 #ifdef USE_VFS
1049 {' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs},
1050 {' ', N_("Fr&ee VFSs now"), 'E', free_vfs_now},
1051 #endif
1052 #ifdef WITH_BACKGROUND
1053 {' ', N_("&Background jobs C-x j"), 'B', jobs_cmd},
1054 #endif
1055 {' ', "", ' ', 0},
1056 #ifdef USE_EXT2FSLIB
1057 {' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd},
1058 #endif
1059 #ifdef LISTMODE_EDITOR
1060 {' ', N_("&Listing format edit"), 'L', listmode_cmd},
1061 #endif
1062 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
1063 {' ', "", ' ', 0},
1064 #endif
1065 {' ', N_("Edit &extension file"), 'E', ext_cmd},
1066 {' ', N_("Edit &menu file"), 'M', edit_mc_menu_cmd},
1067 #ifdef USE_INTERNAL_EDIT
1068 {' ', N_("Edit edi&tor menu file"), 'T', edit_user_menu_cmd},
1069 {' ', N_("Edit &syntax file"), 'S', edit_syntax_cmd}
1070 #endif /* USE_INTERNAL_EDIT */
1073 /* Must keep in sync with the constants in menu_cmd */
1074 static menu_entry OptMenu[] = {
1075 {' ', N_("&Configuration..."), 'C', configure_box},
1076 {' ', N_("&Layout..."), 'L', layout_cmd},
1077 {' ', N_("c&Onfirmation..."), 'O', confirm_box},
1078 {' ', N_("&Display bits..."), 'D', display_bits_box},
1079 #ifndef NATIVE_WIN32
1080 {' ', N_("learn &Keys..."), 'K', learn_keys},
1081 #endif /* !NATIVE_WIN32 */
1082 #ifdef USE_VFS
1083 {' ', N_("&Virtual FS..."), 'V', configure_vfs},
1084 #endif /* !USE_VFS */
1085 {' ', "", ' ', 0},
1086 {' ', N_("&Save setup"), 'S', save_setup_cmd}
1089 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
1091 static Menu *MenuBar[5];
1093 void
1094 init_menu (void)
1096 MenuBar[0] =
1097 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
1098 PanelMenu, menu_entries (PanelMenu),
1099 "[Left and Right Menus]");
1100 MenuBar[1] =
1101 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
1102 "[File Menu]");
1103 MenuBar[2] =
1104 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
1105 "[Command Menu]");
1106 MenuBar[3] =
1107 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
1108 "[Options Menu]");
1109 MenuBar[4] =
1110 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
1111 RightMenu, menu_entries (PanelMenu),
1112 "[Left and Right Menus]");
1115 void
1116 done_menu (void)
1118 int i;
1120 for (i = 0; i < 5; i++) {
1121 destroy_menu (MenuBar[i]);
1125 static void
1126 menu_last_selected_cmd (void)
1128 the_menubar->active = 1;
1129 the_menubar->dropped = drop_menus;
1130 the_menubar->previous_selection = dlg_item_number (midnight_dlg);
1131 dlg_select_widget (midnight_dlg, the_menubar);
1134 static void
1135 menu_cmd (void)
1137 if (the_menubar->active)
1138 return;
1140 if (get_current_index () == 0)
1141 the_menubar->selected = 0;
1142 else
1143 the_menubar->selected = 4;
1144 menu_last_selected_cmd ();
1147 /* Flag toggling functions */
1148 void
1149 toggle_fast_reload (void)
1151 fast_reload = !fast_reload;
1152 if (fast_reload_w == 0 && fast_reload) {
1153 message (0, _(" Information "),
1155 (" Using the fast reload option may not reflect the exact \n"
1156 " directory contents. In this cases you'll need to do a \n"
1157 " manual reload of the directory. See the man page for \n"
1158 " the details. "));
1159 fast_reload_w = 1;
1163 void
1164 toggle_mix_all_files (void)
1166 mix_all_files = !mix_all_files;
1167 update_panels (UP_RELOAD, UP_KEEPSEL);
1170 void
1171 toggle_show_backup (void)
1173 show_backups = !show_backups;
1174 update_panels (UP_RELOAD, UP_KEEPSEL);
1177 void
1178 toggle_show_hidden (void)
1180 show_dot_files = !show_dot_files;
1181 update_panels (UP_RELOAD, UP_KEEPSEL);
1185 * Just a hack for allowing url-like pathnames to be accepted from the
1186 * command line.
1188 static void
1189 translated_mc_chdir (char *dir)
1191 char *newdir;
1193 newdir = vfs_translate_url (dir);
1194 mc_chdir (newdir);
1195 g_free (newdir);
1198 void
1199 create_panels (void)
1201 int current_index;
1202 int other_index;
1203 int current_mode;
1204 int other_mode;
1205 char original_dir[1024];
1207 original_dir[0] = 0;
1209 if (boot_current_is_left) {
1210 current_index = 0;
1211 other_index = 1;
1212 current_mode = startup_left_mode;
1213 other_mode = startup_right_mode;
1214 } else {
1215 current_index = 1;
1216 other_index = 0;
1217 current_mode = startup_right_mode;
1218 other_mode = startup_left_mode;
1220 /* Creates the left panel */
1221 if (this_dir) {
1222 if (other_dir) {
1223 /* Ok, user has specified two dirs, save the original one,
1224 * since we may not be able to chdir to the proper
1225 * second directory later
1227 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1229 translated_mc_chdir (this_dir);
1231 set_display_type (current_index, current_mode);
1233 /* The other panel */
1234 if (other_dir) {
1235 if (original_dir[0])
1236 translated_mc_chdir (original_dir);
1237 translated_mc_chdir (other_dir);
1239 set_display_type (other_index, other_mode);
1241 if (startup_left_mode == view_listing) {
1242 current_panel = left_panel;
1243 } else {
1244 if (right_panel)
1245 current_panel = right_panel;
1246 else
1247 current_panel = left_panel;
1250 /* Create the nice widgets */
1251 cmdline = command_new (0, 0, 0);
1252 the_prompt = label_new (0, 0, prompt, NULL);
1253 the_prompt->transparent = 1;
1254 the_bar = buttonbar_new (keybar_visible);
1256 the_hint = label_new (0, 0, 0, NULL);
1257 the_hint->transparent = 1;
1258 the_hint->auto_adjust_cols = 0;
1259 the_hint->widget.cols = COLS;
1261 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1264 static void
1265 copy_current_pathname (void)
1267 if (!command_prompt)
1268 return;
1270 command_insert (cmdline, cpanel->cwd, 0);
1271 if (cpanel->cwd[strlen (cpanel->cwd) - 1] != PATH_SEP)
1272 command_insert (cmdline, PATH_SEP_STR, 0);
1275 static void
1276 copy_other_pathname (void)
1278 if (get_other_type () != view_listing)
1279 return;
1281 if (!command_prompt)
1282 return;
1284 command_insert (cmdline, opanel->cwd, 0);
1285 if (cpanel->cwd[strlen (opanel->cwd) - 1] != PATH_SEP)
1286 command_insert (cmdline, PATH_SEP_STR, 0);
1289 static void
1290 copy_readlink (WPanel *panel)
1292 if (!command_prompt)
1293 return;
1294 if (S_ISLNK (selection (panel)->buf.st_mode)) {
1295 char buffer[MC_MAXPATHLEN];
1296 char *p =
1297 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1298 int i;
1300 i = mc_readlink (p, buffer, MC_MAXPATHLEN);
1301 g_free (p);
1302 if (i > 0) {
1303 buffer[i] = 0;
1304 command_insert (cmdline, buffer, 1);
1309 static void
1310 copy_current_readlink (void)
1312 copy_readlink (cpanel);
1315 static void
1316 copy_other_readlink (void)
1318 if (get_other_type () != view_listing)
1319 return;
1320 copy_readlink (opanel);
1323 /* Insert the selected file name into the input line */
1324 static void
1325 copy_prog_name (void)
1327 char *tmp;
1328 if (!command_prompt)
1329 return;
1331 if (get_current_type () == view_tree) {
1332 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1333 tmp = tree_selected_name (tree);
1334 } else
1335 tmp = selection (cpanel)->fname;
1337 command_insert (cmdline, tmp, 1);
1340 static void
1341 copy_tagged (WPanel *panel)
1343 int i;
1345 if (!command_prompt)
1346 return;
1347 input_disable_update (cmdline);
1348 if (panel->marked) {
1349 for (i = 0; i < panel->count; i++) {
1350 if (panel->dir.list[i].f.marked)
1351 command_insert (cmdline, panel->dir.list[i].fname, 1);
1353 } else {
1354 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1357 input_enable_update (cmdline);
1360 static void
1361 copy_current_tagged (void)
1363 copy_tagged (cpanel);
1366 static void
1367 copy_other_tagged (void)
1369 if (get_other_type () != view_listing)
1370 return;
1371 copy_tagged (opanel);
1374 static void
1375 do_suspend_cmd (void)
1377 pre_exec ();
1379 if (console_flag && !use_subshell)
1380 restore_console ();
1382 #ifdef SIGTSTP
1384 struct sigaction sigtstp_action;
1386 /* Make sure that the SIGTSTP below will suspend us directly,
1387 without calling ncurses' SIGTSTP handler; we *don't* want
1388 ncurses to redraw the screen immediately after the SIGCONT */
1389 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
1391 kill (getpid (), SIGTSTP);
1393 /* Restore previous SIGTSTP action */
1394 sigaction (SIGTSTP, &sigtstp_action, NULL);
1396 #endif /* SIGTSTP */
1398 if (console_flag && !use_subshell)
1399 handle_console (CONSOLE_SAVE);
1401 edition_post_exec ();
1404 void
1405 suspend_cmd (void)
1407 save_cwds_stat ();
1408 do_suspend_cmd ();
1409 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1410 do_refresh ();
1413 static void
1414 init_labels (void)
1416 define_label (midnight_dlg, 1, _("Help"), help_cmd);
1417 define_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1418 define_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1419 define_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1422 static const key_map ctl_x_map[] = {
1423 {XCTRL ('c'), quit_cmd},
1424 {'d', compare_dirs_cmd},
1425 #ifdef USE_VFS
1426 {'a', reselect_vfs},
1427 #endif /* USE_VFS */
1428 {'p', copy_current_pathname},
1429 {XCTRL ('p'), copy_other_pathname},
1430 {'t', copy_current_tagged},
1431 {XCTRL ('t'), copy_other_tagged},
1432 {'c', chmod_cmd},
1433 #ifndef NATIVE_WIN32
1434 {'o', chown_cmd},
1435 {'r', copy_current_readlink},
1436 {XCTRL ('r'), copy_other_readlink},
1437 {'l', link_cmd},
1438 {'s', symlink_cmd},
1439 {XCTRL ('s'), edit_symlink_cmd},
1440 #endif /* !NATIVE_WIN32 */
1441 {'i', info_cmd_no_menu},
1442 {'q', quick_cmd_no_menu},
1443 {'h', add2hotlist_cmd},
1444 {'!', external_panelize},
1445 #ifdef WITH_BACKGROUND
1446 {'j', jobs_cmd},
1447 #endif /* WITH_BACKGROUND */
1448 #ifdef HAVE_SETSOCKOPT
1449 {'%', source_routing},
1450 #endif /* HAVE_SETSOCKOPT */
1451 {0, 0}
1454 static int ctl_x_map_enabled = 0;
1456 static void
1457 ctl_x_cmd (int ignore)
1459 ctl_x_map_enabled = 1;
1462 static void
1463 nothing (void)
1467 static const key_map default_map[] = {
1468 {KEY_F (19), menu_last_selected_cmd},
1469 {KEY_F (20), (key_callback) quiet_quit_cmd},
1471 /* Copy useful information to the command line */
1472 {ALT ('a'), copy_current_pathname},
1473 {ALT ('A'), copy_other_pathname},
1475 {ALT ('c'), quick_cd_cmd},
1477 /* To access the directory hotlist */
1478 {XCTRL ('\\'), quick_chdir_cmd},
1480 /* Suspend */
1481 {XCTRL ('z'), suspend_cmd},
1483 /* The filtered view command */
1484 {ALT ('!'), filtered_view_cmd},
1486 /* Find file */
1487 {ALT ('?'), find_cmd},
1489 /* Panel refresh */
1490 {XCTRL ('r'), reread_cmd},
1492 /* Toggle listing between long, user defined and full formats */
1493 {ALT ('t'), toggle_listing_cmd},
1495 /* Swap panels */
1496 {XCTRL ('u'), swap_cmd},
1498 /* View output */
1499 {XCTRL ('o'), view_other_cmd},
1501 /* Control-X keybindings */
1502 {XCTRL ('x'), ctl_x_cmd},
1504 /* Trap dlg's exit commands */
1505 {ESC_CHAR, nothing},
1506 {XCTRL ('c'), nothing},
1507 {XCTRL ('g'), nothing},
1508 {0, 0},
1511 static void
1512 setup_sigwinch (void)
1514 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && \
1515 !defined(NATIVE_WIN32) && defined(SIGWINCH)
1516 struct sigaction act, oact;
1517 act.sa_handler = flag_winch;
1518 sigemptyset (&act.sa_mask);
1519 act.sa_flags = 0;
1520 # ifdef SA_RESTART
1521 act.sa_flags |= SA_RESTART;
1522 # endif
1523 sigaction (SIGWINCH, &act, &oact);
1524 #endif
1527 static void
1528 setup_pre (void)
1530 /* Call all the inits */
1531 #ifdef HAVE_CHARSET
1533 * Don't restrict the output on the screen manager level,
1534 * the translation tables take care of it.
1536 #define full_eight_bits (1)
1537 #define eight_bit_clean (1)
1538 #endif /* !HAVE_CHARSET */
1540 #ifndef HAVE_SLANG
1541 meta (stdscr, eight_bit_clean);
1542 #else
1543 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1544 #endif
1547 static void
1548 init_xterm_support (void)
1550 char *termvalue;
1551 #ifdef HAVE_SLANG
1552 char *term_entry;
1553 #endif
1555 termvalue = getenv ("TERM");
1556 if (!termvalue || !(*termvalue)) {
1557 fputs (_("The TERM environment variable is unset!\n"), stderr);
1558 exit (1);
1561 /* Check mouse capabilities */
1562 #ifdef HAVE_SLANG
1563 term_entry = SLtt_tigetent (termvalue);
1564 xmouse_seq = SLtt_tigetstr ("Km", &term_entry);
1565 #else
1566 xmouse_seq = tigetstr ("kmous");
1567 #endif
1569 /* -1 means invalid capability, shouldn't happen, but let's make it 0 */
1570 if ((long) xmouse_seq == -1) {
1571 xmouse_seq = NULL;
1574 if (strcmp (termvalue, "cygwin") == 0) {
1575 force_xterm = 1;
1576 use_mouse_p = MOUSE_DISABLED;
1579 if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
1580 || strncmp (termvalue, "rxvt", 4) == 0
1581 || strcmp (termvalue, "dtterm") == 0) {
1582 xterm_flag = 1;
1584 /* Default to the standard xterm sequence */
1585 if (!xmouse_seq) {
1586 xmouse_seq = ESC_STR "[M";
1589 /* Enable mouse unless explicitly disabled by --nomouse */
1590 if (use_mouse_p != MOUSE_DISABLED) {
1591 use_mouse_p = MOUSE_XTERM;
1596 static void
1597 setup_mc (void)
1599 setup_pre ();
1600 init_menu ();
1601 create_panels ();
1602 setup_panels ();
1604 #ifdef HAVE_SUBSHELL_SUPPORT
1605 if (use_subshell)
1606 add_select_channel (subshell_pty, load_prompt, 0);
1607 #endif /* !HAVE_SUBSHELL_SUPPORT */
1609 setup_sigwinch ();
1611 if (baudrate () < 9600 || slow_terminal) {
1612 verbose = 0;
1614 init_mouse ();
1617 static void
1618 setup_dummy_mc (const char *file)
1620 char d[MC_MAXPATHLEN];
1622 mc_get_current_wd (d, MC_MAXPATHLEN);
1623 setup_mc ();
1624 mc_chdir (d);
1626 /* Create a fake current_panel, this is needed because the
1627 * expand_format routine will use current panel.
1629 strcpy (cpanel->cwd, d);
1630 cpanel->selected = 0;
1631 cpanel->count = 1;
1632 cpanel->dir.list[0].fname = (char *) file;
1635 static void
1636 done_mc (void)
1638 disable_mouse ();
1640 done_menu ();
1642 /* Setup shutdown
1644 * We sync the profiles since the hotlist may have changed, while
1645 * we only change the setup data if we have the auto save feature set
1647 if (auto_save_setup)
1648 save_setup (); /* does also call save_hotlist */
1649 else
1650 save_hotlist ();
1651 done_screen ();
1652 vfs_add_current_stamps ();
1655 /* This should be called after destroy_dlg since panel widgets
1656 * save their state on the profiles
1658 static void
1659 done_mc_profile (void)
1661 if (!auto_save_setup)
1662 profile_forget_profile (profile_name);
1663 sync_profiles ();
1664 done_setup ();
1665 free_profiles ();
1669 * Partly repaint the contents of the panels.
1670 * Ideally, all painting should be done in the panel's callback.
1671 * Since we are bypassing the standard widget library by forcing
1672 * the repaint, the cursor position needs to be preserved.
1674 static void
1675 make_panels_dirty (void)
1677 int col, row;
1679 /* Preserve current cursor position */
1680 getyx (stdscr, row, col);
1682 if (cpanel->dirty)
1683 panel_update_contents (cpanel);
1685 if ((get_other_type () == view_listing) && opanel->dirty)
1686 panel_update_contents (opanel);
1688 /* Restore cursor position */
1689 move (row, col);
1692 /* In Windows people want to actually type the '\' key frequently */
1693 #ifdef NATIVE_WIN32
1694 # define check_key_backslash(x) 0
1695 #else
1696 # define check_key_backslash(x) ((x) == '\\')
1697 #endif
1699 static int
1700 midnight_callback (struct Dlg_head *h, int id, int msg)
1702 int i;
1703 static int first_pre_event = 1;
1705 switch (msg) {
1707 case DLG_PRE_EVENT:
1708 make_panels_dirty ();
1709 if (auto_menu && first_pre_event) {
1710 user_file_menu_cmd ();
1712 first_pre_event = 0;
1713 return MSG_HANDLED;
1715 case DLG_KEY:
1716 if (ctl_x_map_enabled) {
1717 ctl_x_map_enabled = 0;
1718 for (i = 0; ctl_x_map[i].key_code; i++)
1719 if (id == ctl_x_map[i].key_code) {
1720 (*ctl_x_map[i].fn) (id);
1721 return MSG_HANDLED;
1725 /* FIXME: should handle all menu shortcuts before this point */
1726 if (the_menubar->active)
1727 break;
1729 if (id == KEY_F (10)) {
1730 quit_cmd ();
1731 return MSG_HANDLED;
1734 if (id == '\t')
1735 free_completions (cmdline);
1737 if (id == '\n' && cmdline->buffer[0]) {
1738 send_message ((Widget *) cmdline, WIDGET_KEY, id);
1739 return MSG_HANDLED;
1742 /* Ctrl-Enter and Alt-Enter */
1743 if (((id & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1744 && (id & (KEY_M_CTRL | KEY_M_ALT))) {
1745 copy_prog_name ();
1746 return MSG_HANDLED;
1749 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1750 && !quote && !cpanel->searching) {
1751 if (!only_leading_plus_minus) {
1752 /* Special treatement, since the input line will eat them */
1753 if (id == '+') {
1754 select_cmd ();
1755 return MSG_HANDLED;
1758 if (check_key_backslash (id) || id == '-') {
1759 unselect_cmd ();
1760 return MSG_HANDLED;
1763 if (id == '*') {
1764 reverse_selection_cmd ();
1765 return MSG_HANDLED;
1767 } else if (command_prompt && !strlen (cmdline->buffer)) {
1768 /* Special treatement '+', '-', '\', '*' only when this is
1769 * first char on input line
1772 if (id == '+') {
1773 select_cmd ();
1774 return MSG_HANDLED;
1777 if (check_key_backslash (id) || id == '-') {
1778 unselect_cmd ();
1779 return MSG_HANDLED;
1782 if (id == '*') {
1783 reverse_selection_cmd ();
1784 return MSG_HANDLED;
1788 break;
1790 case DLG_HOTKEY_HANDLED:
1791 if (get_current_type () == view_listing)
1792 cpanel->searching = 0;
1793 break;
1795 case DLG_UNHANDLED_KEY:
1796 if (command_prompt) {
1797 int v;
1799 v = send_message ((Widget *) cmdline, WIDGET_KEY, id);
1800 if (v)
1801 return v;
1803 if (ctl_x_map_enabled) {
1804 ctl_x_map_enabled = 0;
1805 for (i = 0; ctl_x_map[i].key_code; i++)
1806 if (id == ctl_x_map[i].key_code) {
1807 (*ctl_x_map[i].fn) (id);
1808 return MSG_HANDLED;
1810 } else {
1811 for (i = 0; default_map[i].key_code; i++) {
1812 if (id == default_map[i].key_code) {
1813 (*default_map[i].fn) (id);
1814 return MSG_HANDLED;
1818 return MSG_NOT_HANDLED;
1820 /* We handle the special case of the output lines */
1821 case DLG_DRAW:
1822 attrset (SELECTED_COLOR);
1823 if (console_flag && output_lines)
1824 show_console_contents (output_start_y,
1825 LINES - output_lines - keybar_visible -
1826 1, LINES - keybar_visible - 1);
1827 return MSG_HANDLED;
1830 return default_dlg_callback (h, id, msg);
1833 #define xtoolkit_panel_setup()
1835 /* Show current directory in the xterm title */
1836 void
1837 update_xterm_title_path (void)
1839 unsigned char *p, *s;
1841 if (xterm_flag && xterm_title) {
1842 p = s = g_strdup (strip_home_and_password (cpanel->cwd));
1843 do {
1844 if (!is_printable (*s))
1845 *s = '?';
1846 } while (*++s);
1847 fprintf (stdout, "\33]0;mc - %s\7", p);
1848 fflush (stdout);
1849 g_free (p);
1854 * Load new hint and display it.
1855 * IF force is not 0, ignore the timeout.
1857 void
1858 load_hint (int force)
1860 char *hint;
1862 if (!the_hint->widget.parent)
1863 return;
1865 if (!message_visible) {
1866 label_set_text (the_hint, 0);
1867 return;
1870 if ((hint = get_random_hint (force))) {
1871 if (*hint)
1872 set_hintbar (hint);
1873 g_free (hint);
1874 } else {
1875 char text[BUF_SMALL];
1877 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1878 VERSION);
1879 set_hintbar (text);
1883 static void
1884 setup_panels_and_run_mc (void)
1886 int first, second;
1888 xtoolkit_panel_setup ();
1889 add_widget (midnight_dlg, the_hint);
1890 load_hint (1);
1891 add_widget (midnight_dlg, cmdline);
1892 add_widget (midnight_dlg, the_prompt);
1893 add_widget (midnight_dlg, the_bar);
1895 if (boot_current_is_left) {
1896 first = 1;
1897 second = 0;
1898 } else {
1899 first = 0;
1900 second = 1;
1902 add_widget (midnight_dlg, get_panel_widget (first));
1903 add_widget (midnight_dlg, get_panel_widget (second));
1904 add_widget (midnight_dlg, the_menubar);
1906 init_labels ();
1908 /* Run the Midnight Commander if no file was specified in the command line */
1909 run_dlg (midnight_dlg);
1912 /* result must be free'd (I think this should go in util.c) */
1913 static char *
1914 prepend_cwd_on_local (char *filename)
1916 char *d;
1917 int l;
1919 if (vfs_file_is_local (filename)) {
1920 if (*filename == PATH_SEP) /* an absolute pathname */
1921 return g_strdup (filename);
1922 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1923 mc_get_current_wd (d, MC_MAXPATHLEN);
1924 l = strlen (d);
1925 d[l++] = PATH_SEP;
1926 strcpy (d + l, filename);
1927 return canonicalize_pathname (d);
1928 } else
1929 return g_strdup (filename);
1932 static int
1933 mc_maybe_editor_or_viewer (void)
1935 char *path = NULL;
1937 if (!(view_one_file || edit_one_file))
1938 return 0;
1940 /* Invoke the internal view/edit routine with:
1941 * the default processing and forcing the internal viewer/editor
1943 if (view_one_file) {
1944 path = prepend_cwd_on_local (view_one_file);
1945 setup_dummy_mc (path);
1946 view_file (path, 0, 1);
1948 #ifdef USE_INTERNAL_EDIT
1949 else {
1950 path = prepend_cwd_on_local ("");
1951 setup_dummy_mc (path);
1952 edit (edit_one_file, edit_one_file_start_line);
1954 #endif /* USE_INTERNAL_EDIT */
1955 g_free (path);
1956 midnight_shutdown = 1;
1957 done_mc ();
1958 return 1;
1961 /* Run the main dialog that occupies the whole screen */
1962 static void
1963 do_nc (void)
1965 int midnight_colors[4];
1967 midnight_colors[0] = NORMAL_COLOR; /* NORMALC */
1968 midnight_colors[1] = REVERSE_COLOR; /* FOCUSC */
1969 midnight_colors[2] = INPUT_COLOR; /* HOT_NORMALC */
1970 midnight_colors[3] = NORMAL_COLOR; /* HOT_FOCUSC */
1972 midnight_dlg =
1973 create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1974 "[main]", NULL, DLG_HAS_MENUBAR);
1976 /* Check if we were invoked as an editor or file viewer */
1977 if (mc_maybe_editor_or_viewer ())
1978 return;
1980 setup_mc ();
1982 setup_panels_and_run_mc ();
1984 /* Program end */
1985 midnight_shutdown = 1;
1987 /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */
1988 if (last_wd_file && vfs_current_is_local ()) {
1989 last_wd_string = g_strdup (cpanel->cwd);
1991 done_mc ();
1993 destroy_dlg (midnight_dlg);
1994 current_panel = 0;
1995 done_mc_profile ();
1998 #if defined (NATIVE_WIN32)
1999 /* Windows NT code */
2001 void
2002 OS_Setup (void)
2004 SetConsoleTitle ("GNU Midnight Commander");
2006 shell = getenv ("COMSPEC");
2007 if (!shell || !*shell)
2008 shell = get_default_shell ();
2010 /* Default opening mode for files is binary, not text (CR/LF translation) */
2011 #ifndef __EMX__
2012 _fmode = O_BINARY;
2013 #endif
2015 mc_home = get_mc_lib_dir ();
2018 /* Nothing to be done on Windows */
2019 static void
2020 sigchld_handler_no_subshell (int sig)
2024 void
2025 init_sigchld (void)
2029 void
2030 init_sigfatals (void)
2035 #else
2037 /* Unix version */
2038 static void
2039 OS_Setup (void)
2041 char *mc_libdir;
2042 shell = getenv ("SHELL");
2043 if (!shell || !*shell)
2044 shell = g_strdup (getpwuid (geteuid ())->pw_shell);
2045 if (!shell || !*shell)
2046 shell = "/bin/sh";
2048 /* This is the directory, where MC was installed, on Unix this is DATADIR */
2049 /* and can be overriden by the MC_DATADIR environment variable */
2050 if ((mc_libdir = getenv ("MC_DATADIR")) != NULL) {
2051 mc_home = g_strdup (mc_libdir);
2052 } else {
2053 mc_home = g_strdup (DATADIR);
2057 static void
2058 sigchld_handler_no_subshell (int sig)
2060 #ifdef __linux__
2061 int pid, status;
2063 if (!console_flag)
2064 return;
2066 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2067 the value of console_flag never changed, we could simply not install
2068 this handler at all if (!console_flag && !use_subshell). */
2070 /* That comment is no longer true. We need to wait() on a sigchld
2071 handler (that's at least what the tarfs code expects currently). */
2073 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2075 if (pid == cons_saver_pid) {
2077 if (WIFSTOPPED (status)) {
2078 /* Someone has stopped cons.saver - restart it */
2079 kill (pid, SIGCONT);
2080 } else {
2081 /* cons.saver has died - disable console saving */
2082 handle_console (CONSOLE_DONE);
2083 console_flag = 0;
2086 #endif /* __linux__ */
2088 /* If we got here, some other child exited; ignore it */
2091 void
2092 init_sigchld (void)
2094 struct sigaction sigchld_action;
2096 sigchld_action.sa_handler =
2097 #ifdef HAVE_SUBSHELL_SUPPORT
2098 use_subshell ? sigchld_handler :
2099 #endif /* HAVE_SUBSHELL_SUPPORT */
2100 sigchld_handler_no_subshell;
2102 sigemptyset (&sigchld_action.sa_mask);
2104 #ifdef SA_RESTART
2105 sigchld_action.sa_flags = SA_RESTART;
2106 #else
2107 sigchld_action.sa_flags = 0;
2108 #endif /* !SA_RESTART */
2110 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2111 #ifdef HAVE_SUBSHELL_SUPPORT
2113 * This may happen on QNX Neutrino 6, where SA_RESTART
2114 * is defined but not implemented. Fallback to no subshell.
2116 use_subshell = 0;
2117 #endif /* HAVE_SUBSHELL_SUPPORT */
2121 #endif /* NATIVE_WIN32, UNIX */
2123 static void
2124 print_mc_usage (poptContext ctx, FILE *stream)
2126 int leftColWidth;
2128 poptSetOtherOptionHelp (ctx,
2129 _("[flags] [this_dir] [other_panel_dir]\n"));
2131 /* print help for options */
2132 leftColWidth = poptPrintHelp (ctx, stream, 0);
2133 fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
2134 _("Set initial line number for the internal editor"));
2135 fputs (_
2136 ("\n"
2137 "Please send any bug reports (including the output of `mc -V')\n"
2138 "to mc-devel@gnome.org\n"), stream);
2139 version (0);
2142 static void
2143 print_color_usage (void)
2146 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
2147 * and editmarked. To preserve translations, lines should be split.
2149 /* TRANSLATORS: don't translate keywords and names of colors */
2150 fputs (_
2151 ("--colors KEYWORD={FORE},{BACK}\n\n"
2152 "{FORE} and {BACK} can be omitted, and the default will be used\n"
2153 "\n" "Keywords:\n"
2154 " Global: errors, reverse, gauge, input\n"
2155 " File display: normal, selected, marked, markselect\n"
2156 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus\n"
2157 " Menus: menu, menuhot, menusel, menuhotsel\n"
2158 " Help: helpnormal, helpitalic, helplink, helpslink\n"
2159 " File types: directory, executable, link, stalelink, device, special, core\n"
2160 "\n" "Colors:\n"
2161 " black, gray, red, brightred, green, brightgreen, brown,\n"
2162 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
2163 " brightcyan, lightgray and white\n\n"), stdout);
2166 static void
2167 process_args (poptContext ctx, int c, const char *option_arg)
2169 switch (c) {
2170 case 'V':
2171 version (1);
2172 exit (0);
2173 break;
2175 case 'c':
2176 disable_colors = 0;
2177 #ifdef HAVE_SLANG
2178 force_colors = 1;
2179 #endif /* HAVE_SLANG */
2180 break;
2182 case 'f':
2183 printf ("%s\n", mc_home);
2184 exit (0);
2185 break;
2187 #ifdef USE_NETCODE
2188 case 'l':
2189 ftpfs_set_debug (option_arg);
2190 #ifdef WITH_SMBFS
2191 smbfs_set_debugf (option_arg);
2192 #endif /* WITH_SMBFS */
2193 break;
2195 #ifdef WITH_SMBFS
2196 case 'D':
2197 smbfs_set_debug (atoi (option_arg));
2198 break;
2199 #endif /* WITH_SMBFS */
2200 #endif /* USE_NETCODE */
2202 case 'd':
2203 use_mouse_p = MOUSE_DISABLED;
2204 break;
2206 #ifdef HAVE_SUBSHELL_SUPPORT
2207 case 'u':
2208 use_subshell = 0;
2209 break;
2210 #endif /* HAVE_SUBSHELL_SUPPORT */
2212 case 'H':
2213 print_color_usage ();
2214 exit (0);
2215 break;
2217 case 'h':
2218 print_mc_usage (ctx, stdout);
2219 exit (0);
2223 static const struct poptOption argument_table[] = {
2224 #ifdef WITH_BACKGROUND
2225 {"background", 'B', POPT_ARG_NONE, &background_wait, 0,
2226 N_("Use to debug the background code")},
2227 #endif
2228 {"color", 'c', POPT_ARG_NONE, NULL, 'c',
2229 N_("Request to run in color mode")},
2230 {"colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
2231 N_("Specifies a color configuration")},
2233 #ifdef USE_INTERNAL_EDIT
2234 {"edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2235 N_("Edits one file")},
2236 #endif
2238 {"help", 'h', POPT_ARG_NONE, NULL, 'h',
2239 N_("Displays this help message")},
2240 {"help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
2241 N_("Displays a help screen on how to change the color scheme")},
2242 #ifdef USE_NETCODE
2243 {"ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
2244 N_("Log ftp dialog to specified file")},
2245 #ifdef WITH_SMBFS
2246 {"debuglevel", 'D', POPT_ARG_STRING, NULL, 'D',
2247 N_("Set debug level")},
2248 #endif
2249 #endif
2250 {"datadir", 'f', POPT_ARG_NONE, NULL, 'f',
2251 N_("Print data directory")},
2252 {"nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
2253 N_("Requests to run in black and white")},
2254 {"nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
2255 N_("Disable mouse support in text version")},
2256 #ifdef HAVE_SUBSHELL_SUPPORT
2257 {"nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2258 N_("Disables subshell support")},
2259 #endif
2260 {"printwd", 'P', POPT_ARG_STRING, &last_wd_file, 0,
2261 N_("Print last working directory to specified file")},
2262 {"resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
2263 N_("Resets soft keys on HP terminals")},
2264 {"slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
2265 N_("To run on slow terminals")},
2266 #ifndef NATIVE_WIN32
2267 {"stickchars", 'a', 0, &force_ugly_line_drawing, 0,
2268 N_("Use stickchars to draw")},
2269 #endif /* !NATIVE_WIN32 */
2270 #ifdef HAVE_SUBSHELL_SUPPORT
2271 {"subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2272 N_("Enables subshell support (default)")},
2273 #endif
2274 #if defined(HAVE_SLANG) && !defined(NATIVE_WIN32)
2275 {"termcap", 't', 0, &SLtt_Try_Termcap, 0,
2276 N_("Tries to use termcap instead of terminfo")},
2277 #endif
2278 {"version", 'V', POPT_ARG_NONE, NULL, 'V',
2279 N_("Displays the current version")},
2280 {"view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2281 N_("Launches the file viewer on a file")},
2282 {"xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
2283 N_("Forces xterm features")},
2285 {NULL, 0, 0, NULL, 0}
2288 static void
2289 handle_args (int argc, char *argv[])
2291 char *tmp;
2292 poptContext ctx;
2293 char *base;
2294 int c;
2296 ctx =
2297 poptGetContext ("mc", argc, argv, argument_table,
2298 POPT_CONTEXT_NO_EXEC);
2300 #ifdef USE_TERMCAP
2301 SLtt_Try_Termcap = 1;
2302 #endif
2304 while ((c = poptGetNextOpt (ctx)) > 0) {
2305 process_args (ctx, c, poptGetOptArg (ctx));
2308 if (c < -1) {
2309 print_mc_usage (ctx, stderr);
2310 fprintf (stderr, "%s: %s\n",
2311 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2312 poptStrerror (c));
2313 exit (1);
2316 tmp = poptGetArg (ctx);
2319 * Check for special invocation names mcedit and mcview,
2320 * if none apply then set the current directory and the other
2321 * directory from the command line arguments
2323 base = x_basename (argv[0]);
2324 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2325 edit_one_file = "";
2326 if (tmp) {
2327 if (*tmp == '+' && isdigit ((unsigned char) tmp[1])) {
2328 int start_line = atoi (tmp);
2329 if (start_line > 0) {
2330 char *file = poptGetArg (ctx);
2331 if (file) {
2332 tmp = file;
2333 edit_one_file_start_line = start_line;
2337 edit_one_file = g_strdup (tmp);
2339 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2340 if (tmp)
2341 view_one_file = g_strdup (tmp);
2342 else {
2343 fputs ("No arguments given to the viewer\n", stderr);
2344 exit (1);
2346 } else {
2347 /* sets the current dir and the other dir */
2348 if (tmp) {
2349 this_dir = g_strdup (tmp);
2350 if ((tmp = poptGetArg (ctx)))
2351 other_dir = g_strdup (tmp);
2355 poptFreeContext (ctx);
2359 * The compatibility_move_mc_files routine is intended to
2360 * move all of the hidden .mc files into a private ~/.mc
2361 * directory in the home directory, to avoid cluttering the users.
2363 * Previous versions of the program had all of their files in
2364 * the $HOME, we are now putting them in $HOME/.mc
2366 #ifdef NATIVE_WIN32
2367 # define compatibility_move_mc_files() 0
2368 #else
2370 static int
2371 do_mc_filename_rename (char *mc_dir, char *o_name, char *n_name)
2373 char *full_o_name = concat_dir_and_file (home_dir, o_name);
2374 char *full_n_name = g_strconcat (home_dir, MC_BASE, n_name, NULL);
2375 int move;
2377 move = 0 == rename (full_o_name, full_n_name);
2378 g_free (full_o_name);
2379 g_free (full_n_name);
2380 return move;
2383 static int
2384 compatibility_move_mc_files (void)
2386 struct stat s;
2387 int move = 0;
2388 char *mc_dir = concat_dir_and_file (home_dir, ".mc");
2390 if (stat (mc_dir, &s) && (errno == ENOENT)
2391 && (mkdir (mc_dir, 0777) != -1)) {
2393 move = do_mc_filename_rename (mc_dir, ".mc.ini", "ini");
2394 move += do_mc_filename_rename (mc_dir, ".mc.hot", "hotlist");
2395 move += do_mc_filename_rename (mc_dir, ".mc.ext", "bindings");
2396 move += do_mc_filename_rename (mc_dir, ".mc.menu", "menu");
2397 move += do_mc_filename_rename (mc_dir, ".mc.bashrc", "bashrc");
2398 move += do_mc_filename_rename (mc_dir, ".mc.inputrc", "inputrc");
2399 move += do_mc_filename_rename (mc_dir, ".mc.tcshrc", "tcshrc");
2400 move += do_mc_filename_rename (mc_dir, ".mc.tree", "Tree");
2402 g_free (mc_dir);
2403 return move;
2405 #endif /* NATIVE_WIN32 */
2408 main (int argc, char *argv[])
2410 /* if on, it displays the information that files have been moved to ~/.mc */
2411 int show_change_notice = 0;
2413 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2414 setlocale (LC_ALL, "");
2415 bindtextdomain ("mc", LOCALEDIR);
2416 textdomain ("mc");
2418 /* Initialize list of all user group for timur_clr_mode */
2419 init_groups ();
2421 /* Set up temporary directory */
2422 mc_tmpdir ();
2424 OS_Setup ();
2426 /* This variable is used by the subshell */
2427 home_dir = getenv ("HOME");
2428 if (!home_dir) {
2429 /* mc_home was computed by OS_Setup */
2430 home_dir = mc_home;
2433 vfs_init ();
2435 #ifdef HAVE_SLANG
2436 SLtt_Ignore_Beep = 1;
2437 #endif
2439 /* NOTE: This has to be called before slang_init or whatever routine
2440 calls any define_sequence */
2441 init_key ();
2443 handle_args (argc, argv);
2445 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2446 handle_console (CONSOLE_INIT);
2448 #ifdef HAVE_SUBSHELL_SUPPORT
2449 /* Don't use subshell when invoked as viewer or editor */
2450 if (edit_one_file || view_one_file)
2451 use_subshell = 0;
2453 if (use_subshell)
2454 subshell_get_console_attributes ();
2455 #endif /* HAVE_SUBSHELL_SUPPORT */
2457 /* Install the SIGCHLD handler; must be done before init_subshell() */
2458 init_sigchld ();
2460 show_change_notice = compatibility_move_mc_files ();
2462 /* We need this, since ncurses endwin () doesn't restore the signals */
2463 save_stop_handler ();
2465 /* Must be done before init_subshell, to set up the terminal size: */
2466 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2467 #ifdef HAVE_SLANG
2468 slang_init ();
2469 #endif
2470 /* NOTE: This call has to be after slang_init. It's the small part from
2471 the previous init_key which had to be moved after the call of slang_init */
2472 init_key_input_fd ();
2474 load_setup ();
2476 init_curses ();
2478 init_xterm_support ();
2480 #ifdef HAVE_SUBSHELL_SUPPORT
2482 /* Done here to ensure that the subshell doesn't */
2483 /* inherit the file descriptors opened below, etc */
2484 if (use_subshell)
2485 init_subshell ();
2487 #endif /* HAVE_SUBSHELL_SUPPORT */
2489 /* Removing this from the X code let's us type C-c */
2490 load_key_defs ();
2492 /* Also done after init_subshell, to save any shell init file messages */
2493 if (console_flag)
2494 handle_console (CONSOLE_SAVE);
2496 if (alternate_plus_minus)
2497 application_keypad_mode ();
2499 if (show_change_notice) {
2500 message (1, _(" Notice "),
2501 _(" The Midnight Commander configuration files \n"
2502 " are now stored in the ~/.mc directory, the \n"
2503 " files have been moved now\n"));
2505 #ifdef HAVE_SUBSHELL_SUPPORT
2506 if (use_subshell) {
2507 prompt = strip_ctrl_codes (subshell_prompt);
2508 if (!prompt)
2509 prompt = "";
2510 } else
2511 #endif /* HAVE_SUBSHELL_SUPPORT */
2512 prompt = (geteuid () == 0) ? "# " : "$ ";
2514 /* Program main loop */
2515 if (!midnight_shutdown)
2516 do_nc ();
2518 /* Save the tree store */
2519 tree_store_save ();
2521 /* Virtual File System shutdown */
2522 vfs_shut ();
2524 /* Delete list of all user groups */
2525 destroy_groups ();
2527 flush_extension_file (); /* does only free memory */
2529 endwin ();
2530 #ifdef HAVE_SLANG
2531 slang_shutdown ();
2532 #endif
2534 if (console_flag && !(quit & SUBSHELL_EXIT))
2535 restore_console ();
2536 if (alternate_plus_minus)
2537 numeric_keypad_mode ();
2539 #ifndef NATIVE_WIN32
2540 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2541 #endif
2543 if (console_flag)
2544 handle_console (CONSOLE_DONE);
2545 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2547 #ifdef NATIVE_WIN32
2548 /* On NT, home_dir is malloced */
2549 g_free (home_dir);
2550 #endif
2551 if (last_wd_file && last_wd_string && !print_last_revert
2552 && !edit_one_file && !view_one_file) {
2553 int last_wd_fd =
2554 open (last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2555 S_IRUSR | S_IWUSR);
2557 if (last_wd_fd != -1) {
2558 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2559 close (last_wd_fd);
2562 g_free (last_wd_string);
2564 #ifndef NATIVE_WIN32
2565 g_free (mc_home);
2566 #endif /* NATIVE_WIN32 */
2567 done_key ();
2568 #ifdef HAVE_CHARSET
2569 free_codepages_list ();
2570 #endif
2571 if (this_dir)
2572 g_free (this_dir);
2573 if (other_dir)
2574 g_free (other_dir);
2576 return 0;