fix: main.c keys handler
[midnight-commander.git] / src / main.c
blob69403de02353cee056b699770d6d6b07798e1d47
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
6 1994, 1995 Janne Kukonlehto
7 1997 Norbert Warmuth
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file main.c
24 * \brief Source: this is a main module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pwd.h> /* for username in xterm title */
42 #include "global.h"
44 #include "../src/tty/tty.h"
45 #include "../src/skin/skin.h"
46 #include "../src/tty/mouse.h"
47 #include "../src/tty/key.h" /* For init_key() */
48 #include "../src/tty/win.h" /* xterm_flag */
50 #include "../src/mcconfig/mcconfig.h"
51 #include "../src/args.h"
52 #include "../src/skin/skin.h"
53 #include "../src/filehighlight/fhl.h"
55 #include "dir.h"
56 #include "dialog.h"
57 #include "menu.h"
58 #include "panel.h"
59 #include "main.h"
60 #include "option.h"
61 #include "tree.h"
62 #include "treestore.h"
63 #include "cons.saver.h"
64 #include "subshell.h"
65 #include "setup.h" /* save_setup() */
66 #include "boxes.h" /* sort_box() */
67 #include "layout.h"
68 #include "cmd.h" /* Normal commands */
69 #include "hotlist.h"
70 #include "panelize.h"
71 #include "learn.h" /* learn_keys() */
72 #include "listmode.h"
73 #include "execute.h"
74 #include "ext.h" /* For flush_extension_file() */
75 #include "strutil.h"
76 /* Listbox for the command history feature */
77 #include "widget.h"
78 #include "command.h"
79 #include "wtools.h"
80 #include "cmddef.h" /* CK_ cmd name const */
82 #include "../vfs/vfs.h" /* vfs_translate_url() */
84 #include "chmod.h"
85 #include "chown.h"
86 #include "achown.h"
88 #ifdef WITH_SMBFS
89 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
90 #endif
92 #ifdef USE_INTERNAL_EDIT
93 # include "../edit/edit.h"
94 #endif
96 #ifdef HAVE_CHARSET
97 #include "charsets.h"
98 #endif /* HAVE_CHARSET */
100 #ifdef USE_VFS
101 #include "../vfs/gc.h"
102 #endif
104 #include "keybind.h" /* type global_key_map_t */
106 /* When the modes are active, left_panel, right_panel and tree_panel */
107 /* Point to a proper data structure. You should check with the functions */
108 /* get_current_type and get_other_type the types of the panels before using */
109 /* This pointer variables */
111 /* The structures for the panels */
112 WPanel *left_panel = NULL;
113 WPanel *right_panel = NULL;
115 mc_fhl_t *mc_filehighlight;
117 /* The pointer to the tree */
118 WTree *the_tree = NULL;
120 /* The Menubar */
121 struct WMenu *the_menubar = NULL;
123 /* Pointers to the selected and unselected panel */
124 WPanel *current_panel = NULL;
126 /* Set if the command is being run from the "Right" menu */
127 int is_right = 0;
129 /* Set when main loop should be terminated */
130 volatile int quit = 0;
132 /* Set if you want the possible completions dialog for the first time */
133 int show_all_if_ambiguous = 0;
135 /* Set when cd symlink following is desirable (bash mode) */
136 int cd_symlinks = 1;
138 /* If set then dialogs just clean the screen when refreshing, else */
139 /* they do a complete refresh, refreshing all the parts of the program */
140 int fast_refresh = 0;
142 /* If true, marking a files moves the cursor down */
143 int mark_moves_down = 1;
145 /* If true, at startup the user-menu is invoked */
146 int auto_menu = 0;
148 /* If true, then the +, - and \ keys have their special meaning only if the
149 * command line is emtpy, otherwise they behave like regular letters
151 int only_leading_plus_minus = 1;
153 int pause_after_run = pause_on_dumb_terminals;
155 /* It true saves the setup when quitting */
156 int auto_save_setup = 1;
158 #ifdef HAVE_CHARSET
160 * Don't restrict the output on the screen manager level,
161 * the translation tables take care of it.
163 #define full_eight_bits (1)
164 #define eight_bit_clean (1)
165 #else /* HAVE_CHARSET */
166 /* If true, allow characters in the range 160-255 */
167 int eight_bit_clean = 1;
169 * If true, also allow characters in the range 128-159.
170 * This is reported to break on many terminals (xterm, qansi-m).
172 int full_eight_bits = 0;
173 #endif /* !HAVE_CHARSET */
176 * If utf-8 terminal utf8_display = 1
177 * Display bits set UTF-8
180 int utf8_display = 0;
182 /* If true use the internal viewer */
183 int use_internal_view = 1;
185 /* Have we shown the fast-reload warning in the past? */
186 int fast_reload_w = 0;
188 /* Move page/item? When clicking on the top or bottom of a panel */
189 int mouse_move_pages = 1;
191 /* If true: l&r arrows are used to chdir if the input line is empty */
192 int navigate_with_arrows = 0;
194 /* If true program softkeys (HP terminals only) on startup and after every
195 command ran in the subshell to the description found in the termcap/terminfo
196 database */
197 int reset_hp_softkeys = 0;
199 /* The prompt */
200 const char *prompt = NULL;
202 /* The widget where we draw the prompt */
203 WLabel *the_prompt;
205 /* The hint bar */
206 WLabel *the_hint;
208 /* The button bar */
209 WButtonBar *the_bar;
211 /* Mouse type: GPM, xterm or none */
212 Mouse_Type use_mouse_p = MOUSE_NONE;
214 /* If on, default for "No" in delete operations */
215 int safe_delete = 0;
217 /* Controls screen clearing before an exec */
218 int clear_before_exec = 1;
220 /* Asks for confirmation before deleting a file */
221 int confirm_delete = 1;
223 /* Asks for confirmation before deleting a hotlist entry */
224 int confirm_directory_hotlist_delete = 1;
226 /* Asks for confirmation before overwriting a file */
227 int confirm_overwrite = 1;
229 /* Asks for confirmation before executing a program by pressing enter */
230 int confirm_execute = 0;
232 /* Asks for confirmation before leaving the program */
233 int confirm_exit = 1;
235 /* Asks for confirmation when using F3 to view a directory and there
236 are tagged files */
237 int confirm_view_dir = 0;
239 /* This flag indicates if the pull down menus by default drop down */
240 int drop_menus = 0;
242 /* if skip_check_codeset = 1 do not show warning about
243 * system and display codeset is different
245 int skip_check_codeset = 0;
247 /* The dialog handle for the main program */
248 Dlg_head *midnight_dlg = NULL;
250 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
251 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
252 int update_prompt = 0;
254 /* The home directory */
255 const char *home_dir = NULL;
257 /* The value of the other directory, only used when loading the setup */
258 char *other_dir = NULL;
260 /* Only used at program boot */
261 int boot_current_is_left = 1;
263 static char *this_dir = NULL;
265 /* If this is true, then when browsing the tree the other window will
266 * automatically reload it's directory with the contents of the currently
267 * selected directory.
269 int xtree_mode = 0;
271 /* If set, then print to the given file the last directory we were at */
272 static char *last_wd_string = NULL;
273 /* Set to 1 to suppress printing the last directory */
274 static int print_last_revert = 0;
276 /* File name to view if argument was supplied */
277 const char *view_one_file = NULL;
279 /* File name to edit if argument was supplied */
280 const char *edit_one_file = NULL;
282 /* Line to start the editor on */
283 static int edit_one_file_start_line = 0;
285 /* Used so that widgets know if they are being destroyed or
286 shut down */
287 int midnight_shutdown = 0;
289 /* The user's shell */
290 char *shell = NULL;
292 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
293 char *mc_home = NULL;
295 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
296 char *mc_home_alt = NULL;
298 char cmd_buf[512];
300 /* Define this function for glib-style error handling */
301 GQuark
302 mc_main_error_quark (void)
304 return g_quark_from_static_string (PACKAGE);
307 GArray *editor_keymap = NULL;
308 GArray *viewer_keymap = NULL;
309 GArray *viewer_hex_keymap = NULL;
310 GArray *main_keymap = NULL;
311 GArray *main_x_keymap = NULL;
312 GArray *panel_keymap = NULL;
313 GArray *input_keymap = NULL;
315 const global_key_map_t *main_map;
316 const global_key_map_t *main_x_map;
318 static void
319 reload_panelized (WPanel *panel)
321 int i, j;
322 dir_list *list = &panel->dir;
324 if (panel != current_panel)
325 mc_chdir (panel->cwd);
327 for (i = 0, j = 0; i < panel->count; i++) {
328 if (list->list[i].f.marked) {
329 /* Unmark the file in advance. In case the following mc_lstat
330 * fails we are done, else we have to mark the file again
331 * (Note: do_file_mark depends on a valid "list->list [i].buf").
332 * IMO that's the best way to update the panel's summary status
333 * -- Norbert
335 do_file_mark (panel, i, 0);
337 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
338 g_free (list->list[i].fname);
339 continue;
341 if (list->list[i].f.marked)
342 do_file_mark (panel, i, 1);
343 if (j != i)
344 list->list[j] = list->list[i];
345 j++;
347 if (j == 0)
348 panel->count = set_zero_dir (list);
349 else
350 panel->count = j;
352 if (panel != current_panel)
353 mc_chdir (current_panel->cwd);
356 static void
357 update_one_panel_widget (WPanel *panel, int force_update,
358 const char *current_file)
360 int free_pointer;
361 char *my_current_file = NULL;
363 if (force_update & UP_RELOAD) {
364 panel->is_panelized = 0;
365 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
366 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
369 /* If current_file == -1 (an invalid pointer) then preserve selection */
370 if (current_file == UP_KEEPSEL) {
371 free_pointer = 1;
372 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
373 current_file = my_current_file;
374 } else
375 free_pointer = 0;
377 if (panel->is_panelized)
378 reload_panelized (panel);
379 else
380 panel_reload (panel);
382 try_to_select (panel, current_file);
383 panel->dirty = 1;
385 if (free_pointer)
386 g_free (my_current_file);
389 static void
390 update_one_panel (int which, int force_update, const char *current_file)
392 WPanel *panel;
394 if (get_display_type (which) != view_listing)
395 return;
397 panel = (WPanel *) get_panel_widget (which);
398 update_one_panel_widget (panel, force_update, current_file);
401 /* Save current stat of directories to avoid reloading the panels */
402 /* when no modifications have taken place */
403 void
404 save_cwds_stat (void)
406 if (fast_reload) {
407 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
408 if (get_other_type () == view_listing)
409 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
413 #ifdef HAVE_SUBSHELL_SUPPORT
414 void
415 do_update_prompt (void)
417 if (update_prompt) {
418 printf ("\r\n%s", subshell_prompt);
419 fflush (stdout);
420 update_prompt = 0;
423 #endif /* HAVE_SUBSHELL_SUPPORT */
425 void
426 change_panel (void)
428 free_completions (cmdline);
429 dlg_one_down (midnight_dlg);
432 /* Stop MC main dialog and the current dialog if it exists.
433 * Needed to provide fast exit from MC viewer or editor on shell exit */
434 static void
435 stop_dialogs (void)
437 midnight_dlg->running = 0;
438 if (current_dlg) {
439 current_dlg->running = 0;
443 static int
444 quit_cmd_internal (int quiet)
446 int q = quit;
448 if (quiet || !confirm_exit) {
449 q = 1;
450 } else {
451 if (query_dialog
452 (_(" The Midnight Commander "),
453 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
454 2, _("&Yes"), _("&No")) == 0)
455 q = 1;
457 if (q) {
458 #ifdef HAVE_SUBSHELL_SUPPORT
459 if (!use_subshell)
460 stop_dialogs ();
461 else if ((q = exit_subshell ()))
462 #endif
463 stop_dialogs ();
465 if (q)
466 quit |= 1;
467 return quit;
470 static void
471 quit_cmd (void)
473 quit_cmd_internal (0);
476 void
477 quiet_quit_cmd (void)
479 print_last_revert = 1;
480 quit_cmd_internal (1);
483 /* Wrapper for do_subshell_chdir, check for availability of subshell */
484 void
485 subshell_chdir (const char *directory)
487 #ifdef HAVE_SUBSHELL_SUPPORT
488 if (use_subshell) {
489 if (vfs_current_is_local ())
490 do_subshell_chdir (directory, 0, 1);
492 #endif /* HAVE_SUBSHELL_SUPPORT */
495 void
496 directory_history_add (struct WPanel *panel, const char *dir)
498 char *tmp;
500 tmp = g_strdup (dir);
501 strip_password (tmp, 1);
503 panel->dir_history = list_append_unique (panel->dir_history, tmp);
507 * If we moved to the parent directory move the selection pointer to
508 * the old directory name; If we leave VFS dir, remove FS specificator.
510 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
512 static const char *
513 get_parent_dir_name (const char *cwd, const char *lwd)
515 const char *p;
516 if (strlen (lwd) > strlen (cwd))
517 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
518 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
519 cwd[1] == '\0'))) {
520 return (p + 1);
522 return NULL;
526 * Changes the current directory of the panel.
527 * Don't record change in the directory history.
529 static int
530 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
532 const char *directory;
533 char *olddir;
534 char temp[MC_MAXPATHLEN];
535 char *translated_url;
537 if (cd_type == cd_parse_command) {
538 while (*new_dir == ' ')
539 new_dir++;
542 olddir = g_strdup (panel->cwd);
543 new_dir = translated_url = vfs_translate_url (new_dir);
545 /* Convert *new_path to a suitable pathname, handle ~user */
547 if (cd_type == cd_parse_command) {
548 if (!strcmp (new_dir, "-")) {
549 strcpy (temp, panel->lwd);
550 new_dir = temp;
553 directory = *new_dir ? new_dir : home_dir;
555 if (mc_chdir (directory) == -1) {
556 strcpy (panel->cwd, olddir);
557 g_free (olddir);
558 g_free (translated_url);
559 return 0;
561 g_free (translated_url);
563 /* Success: save previous directory, shutdown status of previous dir */
564 strcpy (panel->lwd, olddir);
565 free_completions (cmdline);
567 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
569 vfs_release_path (olddir);
571 subshell_chdir (panel->cwd);
573 /* Reload current panel */
574 panel_clean_dir (panel);
575 panel->count =
576 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
577 panel->reverse, panel->case_sensitive,
578 panel->exec_first, panel->filter);
579 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
580 load_hint (0);
581 panel->dirty = 1;
582 update_xterm_title_path ();
584 g_free (olddir);
586 return 1;
590 * Changes the current directory of the panel.
591 * Record change in the directory history.
594 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
596 int r;
598 r = _do_panel_cd (panel, new_dir, cd_type);
599 if (r)
600 directory_history_add (panel, panel->cwd);
601 return r;
605 do_cd (const char *new_dir, enum cd_enum exact)
607 return (do_panel_cd (current_panel, new_dir, exact));
610 void
611 directory_history_next (WPanel *panel)
613 GList *nextdir;
615 nextdir = g_list_next (panel->dir_history);
617 if (!nextdir)
618 return;
620 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
621 panel->dir_history = nextdir;
624 void
625 directory_history_prev (WPanel *panel)
627 GList *prevdir;
629 prevdir = g_list_previous (panel->dir_history);
631 if (!prevdir)
632 return;
634 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
635 panel->dir_history = prevdir;
638 void
639 directory_history_list (WPanel *panel)
641 char *s;
643 if (!panel->dir_history)
644 return;
646 s = show_hist (panel->dir_history, &panel->widget);
648 if (!s)
649 return;
651 if (_do_panel_cd (panel, s, cd_exact))
652 directory_history_add (panel, panel->cwd);
653 else
654 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
655 g_free (s);
658 #ifdef HAVE_SUBSHELL_SUPPORT
660 load_prompt (int fd, void *unused)
662 (void) fd;
663 (void) unused;
665 if (!read_subshell_prompt ())
666 return 0;
668 /* Don't actually change the prompt if it's invisible */
669 if (current_dlg == midnight_dlg && command_prompt) {
670 char *tmp_prompt;
671 int prompt_len;
673 tmp_prompt = strip_ctrl_codes (subshell_prompt);
674 prompt_len = str_term_width1 (tmp_prompt);
676 /* Check for prompts too big */
677 if (COLS > 8 && prompt_len > COLS - 8) {
678 tmp_prompt[COLS - 8] = '\0';
679 prompt_len = COLS - 8;
681 prompt = tmp_prompt;
682 label_set_text (the_prompt, prompt);
683 winput_set_origin ((WInput *) cmdline, prompt_len,
684 COLS - prompt_len);
686 /* since the prompt has changed, and we are called from one of the
687 * tty_get_event channels, the prompt updating does not take place
688 * automatically: force a cursor update and a screen refresh
690 update_cursor (midnight_dlg);
691 mc_refresh ();
693 update_prompt = 1;
694 return 0;
696 #endif /* HAVE_SUBSHELL_SUPPORT */
698 static void
699 sort_cmd (void)
701 WPanel *p;
702 sortfn *sort_order;
704 if (!SELECTED_IS_PANEL)
705 return;
707 p = MENU_PANEL;
708 sort_order = sort_box (p->sort_type, &p->reverse,
709 &p->case_sensitive,
710 &p->exec_first);
712 panel_set_sort_order (p, sort_order);
716 static void
717 treebox_cmd (void)
719 char *sel_dir;
721 sel_dir = tree_box (selection (current_panel)->fname);
722 if (sel_dir) {
723 do_cd (sel_dir, cd_exact);
724 g_free (sel_dir);
728 #ifdef LISTMODE_EDITOR
729 static void
730 listmode_cmd (void)
732 char *newmode;
734 if (get_current_type () != view_listing)
735 return;
737 newmode = listmode_edit (current_panel->user_format);
738 if (!newmode)
739 return;
741 g_free (current_panel->user_format);
742 current_panel->list_type = list_user;
743 current_panel->user_format = newmode;
744 set_panel_formats (current_panel);
746 do_refresh ();
748 #endif /* LISTMODE_EDITOR */
750 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
751 static menu_entry LeftMenu[] = {
752 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
753 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
754 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
755 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
756 {' ', "", NULL_HOTKEY, 0},
757 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
758 {' ', "", NULL_HOTKEY, 0},
759 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
760 #ifdef HAVE_CHARSET
761 {' ', "",NULL_HOTKEY, 0},
762 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
763 #endif
764 #ifdef USE_NETCODE
765 {' ', "", NULL_HOTKEY, 0},
766 #ifdef ENABLE_VFS_MCFS
767 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
768 #endif
769 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
770 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
771 #ifdef WITH_SMBFS
772 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
773 #endif
774 #endif
775 {' ', "", NULL_HOTKEY, 0},
776 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
779 static menu_entry RightMenu[] = {
780 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
781 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
782 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
783 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
784 {' ', "", NULL_HOTKEY, 0},
785 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
786 {' ', "", NULL_HOTKEY, 0},
787 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
788 #ifdef HAVE_CHARSET
789 {' ', "",NULL_HOTKEY, 0},
790 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
791 #endif
792 #ifdef USE_NETCODE
793 {' ', "", NULL_HOTKEY, 0},
794 #ifdef ENABLE_VFS_MCFS
795 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
796 #endif
797 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
798 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
799 #ifdef WITH_SMBFS
800 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
801 #endif
802 #endif
803 {' ', "", NULL_HOTKEY, 0},
804 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
807 static menu_entry FileMenu[] = {
808 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
809 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
810 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
811 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
812 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
813 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
814 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
815 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
816 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
817 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
818 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
819 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
820 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
821 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
822 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
823 {' ', "", NULL_HOTKEY, 0},
824 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
825 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
826 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
827 {' ', "", NULL_HOTKEY, 0},
828 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
831 static menu_entry CmdMenu[] = {
832 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
833 /* I know, I'm lazy, but the tree widget when it's not running
834 * as a panel still has some problems, I have not yet finished
835 * the WTree widget port, sorry.
837 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
838 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
839 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
840 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
841 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
842 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
843 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
844 {' ', "", NULL_HOTKEY, 0},
845 {' ', N_("Command &history M-h"), NULL_HOTKEY, history_cmd},
846 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
847 #ifdef USE_VFS
848 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
849 #endif
850 #ifdef WITH_BACKGROUND
851 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
852 #endif
853 {' ', "", NULL_HOTKEY, 0},
854 #ifdef USE_EXT2FSLIB
855 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
856 #endif
857 #ifdef LISTMODE_EDITOR
858 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
859 #endif
860 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
861 {' ', "", NULL_HOTKEY, 0},
862 #endif
863 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
864 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd},
865 {' ', N_("Edit &highlighting group file"), NULL_HOTKEY, edit_fhl_cmd}
868 /* Must keep in sync with the constants in menu_cmd */
869 static menu_entry OptMenu[] = {
870 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
871 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
872 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
873 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
874 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
875 #ifdef USE_VFS
876 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
877 #endif /* !USE_VFS */
878 {' ', "", NULL_HOTKEY, 0},
879 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
882 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
884 static Menu *MenuBar[5];
886 void
887 init_menu (void)
889 MenuBar[0] =
890 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
891 LeftMenu, menu_entries (LeftMenu),
892 "[Left and Right Menus]");
893 MenuBar[1] =
894 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
895 "[File Menu]");
896 MenuBar[2] =
897 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
898 "[Command Menu]");
899 MenuBar[3] =
900 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
901 "[Options Menu]");
902 MenuBar[4] =
903 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
904 RightMenu, menu_entries (RightMenu),
905 "[Left and Right Menus]");
908 void
909 done_menu (void)
911 int i;
913 for (i = 0; i < 5; i++) {
914 destroy_menu (MenuBar[i]);
918 static void
919 menu_last_selected_cmd (void)
921 the_menubar->active = 1;
922 the_menubar->dropped = drop_menus;
923 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
924 dlg_select_widget (the_menubar);
927 static void
928 menu_cmd (void)
930 if (the_menubar->active)
931 return;
933 if ((get_current_index () == 0) ^ (!current_panel->active))
934 the_menubar->selected = 0;
935 else
936 the_menubar->selected = 4;
937 menu_last_selected_cmd ();
940 /* Flag toggling functions */
941 void
942 toggle_fast_reload (void)
944 fast_reload = !fast_reload;
945 if (fast_reload_w == 0 && fast_reload) {
946 message (D_NORMAL, _(" Information "),
948 (" Using the fast reload option may not reflect the exact \n"
949 " directory contents. In this case you'll need to do a \n"
950 " manual reload of the directory. See the man page for \n"
951 " the details. "));
952 fast_reload_w = 1;
956 void
957 toggle_mix_all_files (void)
959 mix_all_files = !mix_all_files;
960 update_panels (UP_RELOAD, UP_KEEPSEL);
963 void
964 toggle_show_backup (void)
966 show_backups = !show_backups;
967 update_panels (UP_RELOAD, UP_KEEPSEL);
970 void
971 toggle_show_hidden (void)
973 show_dot_files = !show_dot_files;
974 update_panels (UP_RELOAD, UP_KEEPSEL);
977 void
978 toggle_kilobyte_si (void)
980 kilobyte_si = !kilobyte_si;
981 update_panels (UP_RELOAD, UP_KEEPSEL);
985 * Just a hack for allowing url-like pathnames to be accepted from the
986 * command line.
988 static void
989 translated_mc_chdir (char *dir)
991 char *newdir;
993 newdir = vfs_translate_url (dir);
994 mc_chdir (newdir);
995 g_free (newdir);
998 static void
999 create_panels (void)
1001 int current_index;
1002 int other_index;
1003 int current_mode;
1004 int other_mode;
1005 char original_dir[1024];
1007 original_dir[0] = 0;
1009 if (boot_current_is_left) {
1010 current_index = 0;
1011 other_index = 1;
1012 current_mode = startup_left_mode;
1013 other_mode = startup_right_mode;
1014 } else {
1015 current_index = 1;
1016 other_index = 0;
1017 current_mode = startup_right_mode;
1018 other_mode = startup_left_mode;
1020 /* Creates the left panel */
1021 if (this_dir) {
1022 if (other_dir) {
1023 /* Ok, user has specified two dirs, save the original one,
1024 * since we may not be able to chdir to the proper
1025 * second directory later
1027 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1029 translated_mc_chdir (this_dir);
1031 set_display_type (current_index, current_mode);
1033 /* The other panel */
1034 if (other_dir) {
1035 if (original_dir[0])
1036 translated_mc_chdir (original_dir);
1037 translated_mc_chdir (other_dir);
1039 set_display_type (other_index, other_mode);
1041 if (startup_left_mode == view_listing) {
1042 current_panel = left_panel;
1043 } else {
1044 if (right_panel)
1045 current_panel = right_panel;
1046 else
1047 current_panel = left_panel;
1050 /* Create the nice widgets */
1051 cmdline = command_new (0, 0, 0);
1052 the_prompt = label_new (0, 0, prompt);
1053 the_prompt->transparent = 1;
1054 the_bar = buttonbar_new (keybar_visible);
1056 the_hint = label_new (0, 0, 0);
1057 the_hint->transparent = 1;
1058 the_hint->auto_adjust_cols = 0;
1059 the_hint->widget.cols = COLS;
1061 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1064 static void
1065 copy_current_pathname (void)
1067 char *cwd_path;
1068 if (!command_prompt)
1069 return;
1071 cwd_path = remove_encoding_from_path (current_panel->cwd);
1072 command_insert (cmdline, cwd_path, 0);
1074 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1075 command_insert (cmdline, PATH_SEP_STR, 0);
1076 g_free (cwd_path);
1079 static void
1080 copy_other_pathname (void)
1082 char *cwd_path;
1084 if (get_other_type () != view_listing)
1085 return;
1087 if (!command_prompt)
1088 return;
1090 cwd_path = remove_encoding_from_path (other_panel->cwd);
1091 command_insert (cmdline, cwd_path, 0);
1093 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1094 command_insert (cmdline, PATH_SEP_STR, 0);
1095 g_free (cwd_path);
1098 static void
1099 copy_readlink (WPanel *panel)
1101 if (!command_prompt)
1102 return;
1103 if (S_ISLNK (selection (panel)->st.st_mode)) {
1104 char buffer[MC_MAXPATHLEN];
1105 char *p =
1106 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1107 int i;
1109 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1110 g_free (p);
1111 if (i > 0) {
1112 buffer[i] = 0;
1113 command_insert (cmdline, buffer, 1);
1118 static void
1119 copy_current_readlink (void)
1121 copy_readlink (current_panel);
1124 static void
1125 copy_other_readlink (void)
1127 if (get_other_type () != view_listing)
1128 return;
1129 copy_readlink (other_panel);
1132 /* Insert the selected file name into the input line */
1133 static void
1134 copy_prog_name (void)
1136 char *tmp;
1137 if (!command_prompt)
1138 return;
1140 if (get_current_type () == view_tree) {
1141 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1142 tmp = tree_selected_name (tree);
1143 } else
1144 tmp = selection (current_panel)->fname;
1146 command_insert (cmdline, tmp, 1);
1149 static void
1150 copy_tagged (WPanel *panel)
1152 int i;
1154 if (!command_prompt)
1155 return;
1156 input_disable_update (cmdline);
1157 if (panel->marked) {
1158 for (i = 0; i < panel->count; i++) {
1159 if (panel->dir.list[i].f.marked)
1160 command_insert (cmdline, panel->dir.list[i].fname, 1);
1162 } else {
1163 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1166 input_enable_update (cmdline);
1169 static void
1170 copy_current_tagged (void)
1172 copy_tagged (current_panel);
1175 static void
1176 copy_other_tagged (void)
1178 if (get_other_type () != view_listing)
1179 return;
1180 copy_tagged (other_panel);
1183 static void
1184 init_labels (void)
1186 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1187 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1188 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1189 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1192 static int ctl_x_map_enabled = 0;
1194 static void
1195 ctl_x_cmd (void)
1197 ctl_x_map_enabled = 1;
1200 static void
1201 nothing (void)
1205 static cb_ret_t
1206 midnight_execute_cmd(int command)
1208 cb_ret_t res = MSG_HANDLED;
1210 switch (command) {
1211 case CK_MenuLastSelectedCmd:
1212 menu_last_selected_cmd ();
1213 break;
1214 case CK_QuietQuitCmd:
1215 quiet_quit_cmd ();
1216 break;
1217 case CK_SingleDirsizeCmd:
1218 smart_dirsize_cmd ();
1219 break;
1220 case CK_CopyCurrentPathname:
1221 copy_current_pathname ();
1222 break;
1223 case CK_CopyOtherPathname:
1224 copy_other_pathname ();
1225 break;
1226 case CK_QuickCdCmd:
1227 quick_cd_cmd ();
1228 break;
1229 case CK_QuickChdirCmd:
1230 quick_chdir_cmd ();
1231 break;
1232 case CK_SuspendCmd:
1233 suspend_cmd ();
1234 break;
1235 case CK_FilteredViewCmd:
1236 filtered_view_cmd ();
1237 break;
1238 case CK_FindCmd:
1239 find_cmd ();
1240 break;
1241 case CK_RereadCmd:
1242 reread_cmd ();
1243 break;
1244 case CK_ToggleListingCmd:
1245 toggle_listing_cmd ();
1246 break;
1247 case CK_SwapCmd:
1248 swap_cmd ();
1249 break;
1250 case CK_ShowCommandLine:
1251 view_other_cmd ();
1252 break;
1253 case CK_QuitCmd:
1254 quit_cmd ();
1255 break;
1256 case CK_CompareDirsCmd:
1257 compare_dirs_cmd ();
1258 break;
1259 case CK_ReselectVfs:
1260 reselect_vfs ();
1261 break;
1262 case CK_CopyCurrentTagged:
1263 copy_current_tagged ();
1264 break;
1265 case CK_CopyOtherTarget:
1266 copy_other_tagged ();
1267 break;
1268 case CK_CopyCurrentReadlink:
1269 copy_current_readlink ();
1270 break;
1271 case CK_CopyOtherReadlink:
1272 copy_other_readlink ();
1273 break;
1274 case CK_ChmodCmd:
1275 chmod_cmd ();
1276 break;
1277 case CK_ChownCmd:
1278 chown_cmd ();
1279 break;
1280 case CK_LinkCmd:
1281 link_cmd () ;
1282 break;
1283 case CK_SymlinkCmd:
1284 symlink_cmd ();
1285 break;
1286 case CK_EditSymlinkCmd:
1287 edit_symlink_cmd ();
1288 break;
1289 case CK_InfoCmd:
1290 info_cmd_no_menu ();
1291 break;
1292 case CK_QuickViewCmd:
1293 quick_cmd_no_menu ();
1294 break;
1295 case CK_AddHotlist:
1296 add2hotlist_cmd ();
1297 break;
1298 case CK_ExternalPanelize:
1299 external_panelize ();
1300 break;
1301 #ifdef WITH_BACKGROUND
1302 case CK_JobsCmd:
1303 jobs_cmd ();
1304 break;
1305 #endif
1306 case CK_ToggleShowHidden:
1307 toggle_show_hidden ();
1308 break;
1309 case CK_StartExtMap1:
1310 ctl_x_cmd ();
1311 break;
1312 default:
1313 res = MSG_NOT_HANDLED;
1316 return res;
1319 static void
1320 setup_pre (void)
1322 /* Call all the inits */
1324 #ifdef HAVE_SLANG
1325 tty_display_8bit (full_eight_bits != 0);
1326 #else
1327 tty_display_8bit (eight_bit_clean != 0);
1328 #endif
1331 static void
1332 init_xterm_support (void)
1334 const char *termvalue;
1336 termvalue = getenv ("TERM");
1337 if (!termvalue || !(*termvalue)) {
1338 fputs (_("The TERM environment variable is unset!\n"), stderr);
1339 exit (1);
1342 /* Check mouse capabilities */
1343 xmouse_seq = tty_tgetstr ("Km");
1345 if (strcmp (termvalue, "cygwin") == 0) {
1346 mc_args__force_xterm = 1;
1347 use_mouse_p = MOUSE_DISABLED;
1350 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1351 || strncmp (termvalue, "konsole", 7) == 0
1352 || strncmp (termvalue, "rxvt", 4) == 0
1353 || strcmp (termvalue, "Eterm") == 0
1354 || strcmp (termvalue, "dtterm") == 0) {
1355 xterm_flag = 1;
1357 /* Default to the standard xterm sequence */
1358 if (!xmouse_seq) {
1359 xmouse_seq = ESC_STR "[M";
1362 /* Enable mouse unless explicitly disabled by --nomouse */
1363 if (use_mouse_p != MOUSE_DISABLED) {
1364 const char *color_term = getenv ("COLORTERM");
1365 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1366 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1367 strcmp (termvalue, "Eterm") == 0) {
1368 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1369 } else {
1370 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1376 static void
1377 setup_mc (void)
1379 setup_pre ();
1380 init_menu ();
1381 create_panels ();
1382 setup_panels ();
1384 #ifdef HAVE_SUBSHELL_SUPPORT
1385 if (use_subshell)
1386 add_select_channel (subshell_pty, load_prompt, 0);
1387 #endif /* !HAVE_SUBSHELL_SUPPORT */
1389 tty_setup_sigwinch (sigwinch_handler);
1391 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1393 init_xterm_support ();
1394 init_mouse ();
1397 static void
1398 setup_dummy_mc (void)
1400 char d[MC_MAXPATHLEN];
1402 mc_get_current_wd (d, MC_MAXPATHLEN);
1403 setup_mc ();
1404 mc_chdir (d);
1407 static void check_codeset()
1409 const char *_system_codepage = NULL;
1410 #ifdef HAVE_CHARSET
1411 const char *_source_codepage = NULL;
1412 const char *_display_codepage = NULL;
1413 int profile_changed = 0;
1415 #define CONFY 16
1416 #define CONFX 54
1418 if ( !skip_check_codeset ) {
1420 QuickWidget ecs_widgets [] = {
1421 QUICK_BUTTON (4, 6, 13, CONFY, N_("&Skip"), B_EXIT, NULL),
1422 QUICK_BUTTON (1, 11, 13, CONFY, N_("&Fix it"), B_ENTER, NULL),
1423 QUICK_CHECKBOX (1, 13, 11, CONFY, N_("don't ask again"), &skip_check_codeset),
1424 QUICK_LABEL (2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
1425 "or source codeset (in mcedit ctrl-t) \n"
1426 "does not match one set via locale. \n"
1427 "Set correct codeset manually or press <<Fix it>> \n"
1428 "to set locale default.\n\n"
1429 "Or set \'don't ask again\' and press <<Skip>>")),
1430 QUICK_END
1433 QuickDialog ecs =
1435 CONFX, CONFY, -1, -1, N_(" Confirmation "),
1436 "[Confirmation]", ecs_widgets, FALSE
1440 _system_codepage = str_detect_termencoding();
1441 _source_codepage = get_codepage_id (source_codepage);
1442 _display_codepage = get_codepage_id (display_codepage);
1443 if ( (strcmp (_system_codepage, _display_codepage)) ||
1444 (strcmp (_system_codepage, _source_codepage)) ) {
1445 if (quick_dialog (&ecs) == B_ENTER){
1446 display_codepage = get_codepage_index (_system_codepage);
1447 cp_display = get_codepage_id (display_codepage);
1448 if ( !strcmp (cp_display, _system_codepage)) {
1449 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1450 mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
1451 display_codepage = get_codepage_index ( cp_display );
1452 utf8_display = str_isutf8 (_system_codepage);
1453 source_codepage = display_codepage;
1454 cp_source = cp_display;
1455 profile_changed = 1;
1456 } else {
1457 utf8_display = str_isutf8 (_system_codepage);
1458 cp_source = cp_display = _system_codepage;
1460 } else {
1461 if ( skip_check_codeset ) {
1462 mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
1463 profile_changed = 1;
1467 if ( profile_changed )
1468 save_configure ();
1470 #else /* HAVE_CHARSET */
1471 _system_codepage = str_detect_termencoding();
1472 utf8_display = str_isutf8 (_system_codepage);
1473 #endif /* HAVE_CHARSET */
1476 static void
1477 done_screen (void)
1479 if (!(quit & SUBSHELL_EXIT))
1480 clr_scr ();
1481 tty_reset_shell_mode ();
1482 tty_noraw_mode ();
1483 tty_keypad (FALSE);
1484 tty_colors_done ();
1487 static void
1488 done_mc (void)
1490 disable_mouse ();
1492 done_menu ();
1494 /* Setup shutdown
1496 * We sync the profiles since the hotlist may have changed, while
1497 * we only change the setup data if we have the auto save feature set
1500 if (auto_save_setup)
1501 save_setup (); /* does also call save_hotlist */
1502 else {
1503 save_hotlist ();
1504 save_panel_types ();
1506 done_screen ();
1507 vfs_add_current_stamps ();
1510 /* This should be called after destroy_dlg since panel widgets
1511 * save their state on the profiles
1513 static void
1514 done_mc_profile (void)
1516 done_setup ();
1519 static cb_ret_t
1520 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1522 int i;
1524 switch (msg) {
1526 case DLG_IDLE:
1527 /* We only need the first idle event */
1528 set_idle_proc (h, 0);
1529 if (auto_menu) {
1530 user_file_menu_cmd ();
1532 return MSG_HANDLED;
1534 case DLG_KEY:
1535 if (ctl_x_map_enabled) {
1536 ctl_x_map_enabled = 0;
1537 for (i = 0; main_x_map[i].key; i++)
1538 if (parm == main_x_map[i].key)
1539 return midnight_execute_cmd (main_x_map[i].command);
1542 /* FIXME: should handle all menu shortcuts before this point */
1543 if (the_menubar->active)
1544 return MSG_NOT_HANDLED;
1546 if (parm == KEY_F (10)) {
1547 quit_cmd ();
1548 return MSG_HANDLED;
1551 if (parm == '\t')
1552 free_completions (cmdline);
1554 if (parm == '\n') {
1555 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1556 cmdline->buffer[i] == '\t'); i++);
1557 if (cmdline->buffer[i]) {
1558 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1559 return MSG_HANDLED;
1561 stuff (cmdline, "", 0);
1562 cmdline->point = 0;
1565 /* Ctrl-Enter and Alt-Enter */
1566 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1567 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1568 copy_prog_name ();
1569 return MSG_HANDLED;
1572 /* Ctrl-Shift-Enter */
1573 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1574 copy_current_pathname ();
1575 copy_prog_name ();
1576 return MSG_HANDLED;
1579 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1580 && !quote && !current_panel->searching) {
1581 if (!only_leading_plus_minus) {
1582 /* Special treatement, since the input line will eat them */
1583 if (parm == '+') {
1584 select_cmd ();
1585 return MSG_HANDLED;
1588 if (parm == '\\' || parm == '-') {
1589 unselect_cmd ();
1590 return MSG_HANDLED;
1593 if (parm == '*') {
1594 reverse_selection_cmd ();
1595 return MSG_HANDLED;
1597 } else if (!command_prompt || !cmdline->buffer[0]) {
1598 /* Special treatement '+', '-', '\', '*' only when this is
1599 * first char on input line
1602 if (parm == '+') {
1603 select_cmd ();
1604 return MSG_HANDLED;
1607 if (parm == '\\' || parm == '-') {
1608 unselect_cmd ();
1609 return MSG_HANDLED;
1612 if (parm == '*') {
1613 reverse_selection_cmd ();
1614 return MSG_HANDLED;
1618 return MSG_NOT_HANDLED;
1620 case DLG_HOTKEY_HANDLED:
1621 if ((get_current_type () == view_listing) && current_panel->searching) {
1622 current_panel->searching = 0;
1623 current_panel->dirty = 1;
1625 return MSG_HANDLED;
1627 case DLG_UNHANDLED_KEY:
1628 if (command_prompt) {
1629 cb_ret_t v;
1631 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1632 if (v == MSG_HANDLED)
1633 return MSG_HANDLED;
1635 if (ctl_x_map_enabled) {
1636 ctl_x_map_enabled = 0;
1637 for (i = 0; main_x_map[i].key; i++)
1638 if (parm == main_x_map[i].key)
1639 return midnight_execute_cmd (main_x_map[i].command);
1640 } else {
1641 for (i = 0; main_map[i].key; i++) {
1642 if (parm == main_map[i].key)
1643 return midnight_execute_cmd (main_map[i].command);
1646 return MSG_NOT_HANDLED;
1648 case DLG_DRAW:
1649 load_hint (1);
1650 /* We handle the special case of the output lines */
1651 if (console_flag && output_lines)
1652 show_console_contents (output_start_y,
1653 LINES - output_lines - keybar_visible -
1654 1, LINES - keybar_visible - 1);
1655 return MSG_HANDLED;
1657 case DLG_POST_KEY:
1658 if (!the_menubar->active)
1659 update_dirty_panels ();
1660 return MSG_HANDLED;
1662 default:
1663 return default_dlg_callback (h, msg, parm);
1667 /* Show current directory in the xterm title */
1668 void
1669 update_xterm_title_path (void)
1671 const char *path;
1672 char host[BUF_TINY];
1673 char *p;
1674 struct passwd *pw = NULL;
1675 char *login = NULL;
1676 int res = 0;
1677 if (xterm_flag && xterm_title) {
1678 path = strip_home_and_password (current_panel->cwd);
1679 res = gethostname(host, sizeof (host));
1680 if ( res ) { /* On success, res = 0 */
1681 host[0] = '\0';
1682 } else {
1683 host[sizeof (host) - 1] = '\0';
1685 pw = getpwuid(getuid());
1686 if ( pw ) {
1687 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1688 } else {
1689 login = g_strdup (host);
1691 p = g_strdup_printf ("mc [%s]:%s", login, path);
1692 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1693 g_free (login);
1694 g_free (p);
1695 if (!alternate_plus_minus)
1696 numeric_keypad_mode ();
1697 fflush (stdout);
1702 * Load new hint and display it.
1703 * IF force is not 0, ignore the timeout.
1705 void
1706 load_hint (int force)
1708 char *hint;
1710 if (!the_hint->widget.parent)
1711 return;
1713 if (!message_visible) {
1714 label_set_text (the_hint, NULL);
1715 return;
1718 hint = get_random_hint (force);
1720 if (hint != NULL) {
1721 if (*hint)
1722 set_hintbar (hint);
1723 g_free (hint);
1724 } else {
1725 char text[BUF_SMALL];
1727 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1728 VERSION);
1729 set_hintbar (text);
1733 static void
1734 setup_panels_and_run_mc (void)
1736 add_widget (midnight_dlg, the_menubar);
1737 add_widget (midnight_dlg, get_panel_widget (0));
1738 add_widget (midnight_dlg, get_panel_widget (1));
1739 add_widget (midnight_dlg, the_hint);
1740 add_widget (midnight_dlg, cmdline);
1741 add_widget (midnight_dlg, the_prompt);
1742 add_widget (midnight_dlg, the_bar);
1743 init_labels ();
1745 if (boot_current_is_left)
1746 dlg_select_widget (get_panel_widget (0));
1747 else
1748 dlg_select_widget (get_panel_widget (1));
1750 /* Run the Midnight Commander if no file was specified in the command line */
1751 run_dlg (midnight_dlg);
1754 /* result must be free'd (I think this should go in util.c) */
1755 static char *
1756 prepend_cwd_on_local (const char *filename)
1758 char *d;
1759 int l;
1761 if (vfs_file_is_local (filename)) {
1762 if (*filename == PATH_SEP) /* an absolute pathname */
1763 return g_strdup (filename);
1764 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1765 mc_get_current_wd (d, MC_MAXPATHLEN);
1766 l = strlen (d);
1767 d[l++] = PATH_SEP;
1768 strcpy (d + l, filename);
1769 canonicalize_pathname (d);
1770 return d;
1771 } else
1772 return g_strdup (filename);
1775 static int
1776 mc_maybe_editor_or_viewer (void)
1778 if (!(view_one_file || edit_one_file))
1779 return 0;
1781 /* Invoke the internal view/edit routine with:
1782 * the default processing and forcing the internal viewer/editor
1784 if (view_one_file) {
1785 char *path = NULL;
1786 path = prepend_cwd_on_local (view_one_file);
1787 view_file (path, 0, 1);
1788 g_free (path);
1790 #ifdef USE_INTERNAL_EDIT
1791 else {
1792 edit_file (edit_one_file, edit_one_file_start_line);
1794 #endif /* USE_INTERNAL_EDIT */
1795 midnight_shutdown = 1;
1796 done_mc ();
1797 return 1;
1800 /* Run the main dialog that occupies the whole screen */
1801 static void
1802 do_nc (void)
1804 int midnight_colors[DLG_COLOR_NUM];
1805 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1806 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1807 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1808 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1810 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1811 "[main]", NULL, DLG_WANT_IDLE);
1813 if (view_one_file || edit_one_file)
1814 setup_dummy_mc ();
1815 else
1816 setup_mc ();
1818 /* start check display_codepage and source_codepage */
1819 check_codeset();
1820 main_map = default_main_map;
1822 if (main_keymap && main_keymap->len > 0)
1823 main_map = (global_key_map_t *) main_keymap->data;
1825 main_x_map = default_main_x_map;
1827 if (main_x_keymap && main_x_keymap->len > 0)
1828 main_x_map = (global_key_map_t *) main_x_keymap->data;
1830 panel_map = default_panel_keymap;
1832 if (panel_keymap && panel_keymap->len > 0) {
1833 panel_map = (global_key_map_t *) panel_keymap->data;
1836 input_map = default_input_keymap;
1838 if (input_keymap && input_keymap->len > 0)
1839 input_map = (global_key_map_t *) input_keymap->data;
1841 /* Check if we were invoked as an editor or file viewer */
1842 if (!mc_maybe_editor_or_viewer ()) {
1843 setup_panels_and_run_mc ();
1845 /* Program end */
1846 midnight_shutdown = 1;
1848 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1849 if (mc_args__last_wd_file && vfs_current_is_local ()) {
1850 last_wd_string = g_strdup (current_panel->cwd);
1852 done_mc ();
1854 destroy_dlg (midnight_dlg);
1855 current_panel = 0;
1856 done_mc_profile ();
1859 /* POSIX version. The only version we support. */
1860 static void
1861 OS_Setup (void)
1863 const char *shell_env = getenv ("SHELL");
1864 const char *mc_libdir;
1866 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1867 struct passwd *pwd;
1868 pwd = getpwuid (geteuid ());
1869 if (pwd != NULL)
1870 shell = g_strdup (pwd->pw_shell);
1871 } else
1872 shell = g_strdup (shell_env);
1874 if ((shell == NULL) || (shell[0] == '\0')) {
1875 g_free (shell);
1876 shell = g_strdup ("/bin/sh");
1879 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1880 /* and can be overriden by the MC_DATADIR environment variable */
1881 mc_libdir = getenv ("MC_DATADIR");
1882 if (mc_libdir != NULL) {
1883 mc_home = g_strdup (mc_libdir);
1884 mc_home_alt = g_strdup (SYSCONFDIR);
1885 } else {
1886 mc_home = g_strdup (SYSCONFDIR);
1887 mc_home_alt = g_strdup (DATADIR);
1890 /* This variable is used by the subshell */
1891 home_dir = getenv ("HOME");
1893 if (!home_dir)
1894 home_dir = mc_home;
1898 static void
1899 sigchld_handler_no_subshell (int sig)
1901 #ifdef __linux__
1902 int pid, status;
1904 if (!console_flag)
1905 return;
1907 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1908 the value of console_flag never changed, we could simply not install
1909 this handler at all if (!console_flag && !use_subshell). */
1911 /* That comment is no longer true. We need to wait() on a sigchld
1912 handler (that's at least what the tarfs code expects currently). */
1914 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1916 if (pid == cons_saver_pid) {
1918 if (WIFSTOPPED (status)) {
1919 /* Someone has stopped cons.saver - restart it */
1920 kill (pid, SIGCONT);
1921 } else {
1922 /* cons.saver has died - disable console saving */
1923 handle_console (CONSOLE_DONE);
1924 console_flag = 0;
1927 /* If we got here, some other child exited; ignore it */
1928 #endif /* __linux__ */
1930 (void) sig;
1933 static void
1934 init_sigchld (void)
1936 struct sigaction sigchld_action;
1938 sigchld_action.sa_handler =
1939 #ifdef HAVE_SUBSHELL_SUPPORT
1940 use_subshell ? sigchld_handler :
1941 #endif /* HAVE_SUBSHELL_SUPPORT */
1942 sigchld_handler_no_subshell;
1944 sigemptyset (&sigchld_action.sa_mask);
1946 #ifdef SA_RESTART
1947 sigchld_action.sa_flags = SA_RESTART;
1948 #else
1949 sigchld_action.sa_flags = 0;
1950 #endif /* !SA_RESTART */
1952 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1953 #ifdef HAVE_SUBSHELL_SUPPORT
1955 * This may happen on QNX Neutrino 6, where SA_RESTART
1956 * is defined but not implemented. Fallback to no subshell.
1958 use_subshell = 0;
1959 #endif /* HAVE_SUBSHELL_SUPPORT */
1963 static void
1964 mc_main__setup_by_args(int argc, char *argv[])
1966 const char *base;
1967 char *tmp;
1969 if (mc_args__nomouse)
1970 use_mouse_p = MOUSE_DISABLED;
1972 #ifdef USE_NETCODE
1973 if (mc_args__netfs_logfile != NULL)
1975 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
1976 #ifdef WITH_SMBFS
1977 smbfs_set_debugf (mc_args__netfs_logfile);
1978 #endif /* WITH_SMBFS */
1981 #ifdef WITH_SMBFS
1982 if (mc_args__debug_level != 0)
1984 smbfs_set_debug (mc_args__debug_level);
1986 #endif /* WITH_SMBFS */
1987 #endif /* USE_NETCODE */
1990 base = x_basename (argv[0]);
1991 tmp = (argc > 0)? argv[1] : NULL;
1994 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
1995 edit_one_file = "";
1996 if (tmp) {
1998 * Check for filename:lineno, followed by an optional colon.
1999 * This format is used by many programs (especially compilers)
2000 * in error messages and warnings. It is supported so that
2001 * users can quickly copy and paste file locations.
2003 char *end = tmp + strlen (tmp), *p = end;
2004 if (p > tmp && p[-1] == ':')
2005 p--;
2006 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2007 p--;
2008 if (tmp < p && p < end && p[-1] == ':') {
2009 struct stat st;
2010 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2012 * Check that the file before the colon actually exists.
2013 * If it doesn't exist, revert to the old behavior.
2015 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2016 edit_one_file = fname;
2017 edit_one_file_start_line = atoi (p);
2018 } else {
2019 g_free (fname);
2020 goto try_plus_filename;
2022 } else {
2023 try_plus_filename:
2024 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2025 int start_line = atoi (tmp);
2026 if (start_line > 0) {
2027 char *file = (argc > 1) ? argv[2] : NULL;
2028 if (file) {
2029 tmp = file;
2030 edit_one_file_start_line = start_line;
2034 edit_one_file = g_strdup (tmp);
2037 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2038 if (tmp)
2039 view_one_file = g_strdup (tmp);
2040 else {
2041 fputs ("No arguments given to the viewer\n", stderr);
2042 exit (1);
2044 } else {
2045 /* sets the current dir and the other dir */
2046 if (tmp) {
2047 this_dir = g_strdup (tmp);
2048 tmp = (argc > 1) ? argv[2] : NULL;
2049 if (tmp)
2050 other_dir = g_strdup (tmp);
2058 main (int argc, char *argv[])
2060 struct stat s;
2061 char *mc_dir;
2062 GError *error = NULL;
2063 gboolean isInitialized;
2065 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2066 setlocale (LC_ALL, "");
2067 bindtextdomain ("mc", LOCALEDIR);
2068 textdomain ("mc");
2070 /* Set up temporary directory */
2071 mc_tmpdir ();
2073 OS_Setup ();
2075 str_init_strings (NULL);
2077 vfs_init ();
2079 #ifdef USE_INTERNAL_EDIT
2080 edit_stack_init ();
2081 #endif
2083 #ifdef HAVE_SLANG
2084 SLtt_Ignore_Beep = 1;
2085 #endif
2087 if ( !mc_args_handle (&argc, &argv, "mc"))
2088 return 1;
2089 mc_main__setup_by_args(argc,argv);
2092 /* NOTE: This has to be called before tty_init or whatever routine
2093 calls any define_sequence */
2094 init_key ();
2096 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2097 handle_console (CONSOLE_INIT);
2099 #ifdef HAVE_SUBSHELL_SUPPORT
2100 /* Don't use subshell when invoked as viewer or editor */
2101 if (edit_one_file || view_one_file)
2102 use_subshell = 0;
2104 if (use_subshell)
2105 subshell_get_console_attributes ();
2106 #endif /* HAVE_SUBSHELL_SUPPORT */
2108 /* Install the SIGCHLD handler; must be done before init_subshell() */
2109 init_sigchld ();
2111 /* We need this, since ncurses endwin () doesn't restore the signals */
2112 save_stop_handler ();
2114 /* Must be done before init_subshell, to set up the terminal size: */
2115 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2116 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2118 load_setup ();
2120 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2122 isInitialized = mc_skin_init(&error);
2124 mc_filehighlight = mc_fhl_new (TRUE);
2126 dlg_set_default_colors ();
2128 if ( ! isInitialized ) {
2129 message (D_ERROR, _("Warning"), error->message);
2130 g_error_free(error);
2131 error = NULL;
2134 /* create home directory */
2135 /* do it after the screen library initialization to show the error message */
2136 mc_dir = concat_dir_and_file (home_dir, MC_BASE);
2137 canonicalize_pathname (mc_dir);
2138 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2139 && mkdir (mc_dir, 0700) != 0)
2140 message (D_ERROR, _("Warning"),
2141 _("Cannot create %s directory"), mc_dir);
2142 g_free (mc_dir);
2144 #ifdef HAVE_SUBSHELL_SUPPORT
2145 /* Done here to ensure that the subshell doesn't */
2146 /* inherit the file descriptors opened below, etc */
2147 if (use_subshell)
2148 init_subshell ();
2150 #endif /* HAVE_SUBSHELL_SUPPORT */
2152 /* Removing this from the X code let's us type C-c */
2153 load_key_defs ();
2155 load_keymap_defs ();
2157 /* Also done after init_subshell, to save any shell init file messages */
2158 if (console_flag)
2159 handle_console (CONSOLE_SAVE);
2161 if (alternate_plus_minus)
2162 application_keypad_mode ();
2164 #ifdef HAVE_SUBSHELL_SUPPORT
2165 if (use_subshell) {
2166 prompt = strip_ctrl_codes (subshell_prompt);
2167 if (!prompt)
2168 prompt = "";
2169 } else
2170 #endif /* HAVE_SUBSHELL_SUPPORT */
2171 prompt = (geteuid () == 0) ? "# " : "$ ";
2174 /* Program main loop */
2175 if (!midnight_shutdown)
2176 do_nc ();
2178 /* Save the tree store */
2179 tree_store_save ();
2181 /* Virtual File System shutdown */
2182 vfs_shut ();
2184 flush_extension_file (); /* does only free memory */
2186 mc_fhl_free (&mc_filehighlight);
2187 mc_skin_deinit();
2189 tty_shutdown ();
2191 if (console_flag && !(quit & SUBSHELL_EXIT))
2192 handle_console (CONSOLE_RESTORE);
2193 if (alternate_plus_minus)
2194 numeric_keypad_mode ();
2196 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2198 if (console_flag)
2199 handle_console (CONSOLE_DONE);
2200 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2202 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2203 && !edit_one_file && !view_one_file) {
2204 int last_wd_fd =
2205 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2206 S_IRUSR | S_IWUSR);
2208 if (last_wd_fd != -1) {
2209 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2210 close (last_wd_fd);
2213 g_free (last_wd_string);
2215 g_free (mc_home_alt);
2216 g_free (mc_home);
2217 g_free (shell);
2219 done_key ();
2220 #ifdef HAVE_CHARSET
2221 free_codepages_list ();
2222 #endif
2223 str_uninit_strings ();
2225 g_free (this_dir);
2226 g_free (other_dir);
2228 #ifdef USE_INTERNAL_EDIT
2229 edit_stack_free ();
2230 #endif
2232 return 0;