Removed double check of panel history before show it.
[kaloumi3.git] / src / main.c
blob9c3b5c4d5f7c9cdcb90bd0a255f911b6021fabe2
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 "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/skin.h"
46 #include "lib/tty/mouse.h"
47 #include "lib/tty/key.h" /* For init_key() */
48 #include "lib/tty/win.h" /* xterm_flag */
50 #include "lib/mcconfig.h"
51 #include "lib/filehighlight.h"
52 #include "lib/fileloc.h" /* MC_USERCONF_DIR */
54 #include "lib/vfs/mc-vfs/vfs.h" /* vfs_translate_url() */
56 #ifdef ENABLE_VFS_SMB
57 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debug() */
58 #endif /* ENABLE_VFS_SMB */
60 #ifdef ENABLE_VFS
61 #include "lib/vfs/mc-vfs/gc.h"
62 #endif
64 #include "lib/strutil.h"
66 #include "src/args.h"
68 #include "dir.h"
69 #include "dialog.h"
70 #include "menu.h"
71 #include "panel.h"
72 #include "option.h"
73 #include "tree.h"
74 #include "treestore.h"
75 #include "consaver/cons.saver.h"
76 #include "subshell.h"
77 #include "setup.h" /* save_setup() */
78 #include "boxes.h" /* sort_box() */
79 #include "layout.h"
80 #include "cmd.h" /* Normal commands */
81 #include "hotlist.h"
82 #include "panelize.h"
83 #include "learn.h" /* learn_keys() */
84 #include "listmode.h"
85 #include "execute.h"
86 #include "ext.h" /* For flush_extension_file() */
87 #include "widget.h"
88 #include "command.h"
89 #include "wtools.h"
90 #include "cmddef.h" /* CK_ cmd name const */
91 #include "user.h" /* user_file_menu_cmd() */
94 #include "chmod.h"
95 #include "chown.h"
96 #include "achown.h"
98 #include "main.h"
101 #ifdef USE_INTERNAL_EDIT
102 # include "src/editor/edit.h"
103 #endif
105 #ifdef HAVE_CHARSET
106 #include "charsets.h"
107 #endif /* HAVE_CHARSET */
110 #include "keybind.h" /* type global_keymap_t */
112 /* When the modes are active, left_panel, right_panel and tree_panel */
113 /* Point to a proper data structure. You should check with the functions */
114 /* get_current_type and get_other_type the types of the panels before using */
115 /* This pointer variables */
117 /* The structures for the panels */
118 WPanel *left_panel = NULL;
119 WPanel *right_panel = NULL;
121 mc_fhl_t *mc_filehighlight;
123 /* The pointer to the tree */
124 WTree *the_tree = NULL;
126 /* The Menubar */
127 struct WMenuBar *the_menubar = NULL;
129 /* Pointers to the selected and unselected panel */
130 WPanel *current_panel = NULL;
132 /* Set if the command is being run from the "Right" menu */
133 int is_right = 0;
135 /* Set when main loop should be terminated */
136 volatile int quit = 0;
138 /* Set if you want the possible completions dialog for the first time */
139 int show_all_if_ambiguous = 0;
141 /* Set when cd symlink following is desirable (bash mode) */
142 int cd_symlinks = 1;
144 /* If set then dialogs just clean the screen when refreshing, else */
145 /* they do a complete refresh, refreshing all the parts of the program */
146 int fast_refresh = 0;
148 /* If true, marking a files moves the cursor down */
149 int mark_moves_down = 1;
151 /* If true, at startup the user-menu is invoked */
152 int auto_menu = 0;
154 /* If true, then the +, - and \ keys have their special meaning only if the
155 * command line is emtpy, otherwise they behave like regular letters
157 int only_leading_plus_minus = 1;
159 int pause_after_run = pause_on_dumb_terminals;
161 /* It true saves the setup when quitting */
162 int auto_save_setup = 1;
164 #ifdef HAVE_CHARSET
166 * Don't restrict the output on the screen manager level,
167 * the translation tables take care of it.
169 #define full_eight_bits (1)
170 #define eight_bit_clean (1)
171 #else /* HAVE_CHARSET */
172 /* If true, allow characters in the range 160-255 */
173 int eight_bit_clean = 1;
175 * If true, also allow characters in the range 128-159.
176 * This is reported to break on many terminals (xterm, qansi-m).
178 int full_eight_bits = 0;
179 #endif /* !HAVE_CHARSET */
182 * If utf-8 terminal utf8_display = 1
183 * Display bits set UTF-8
186 int utf8_display = 0;
188 /* If true use the internal viewer */
189 int use_internal_view = 1;
191 /* Have we shown the fast-reload warning in the past? */
192 int fast_reload_w = 0;
194 /* Move page/item? When clicking on the top or bottom of a panel */
195 int mouse_move_pages = 1;
197 /* If true: l&r arrows are used to chdir if the input line is empty */
198 int navigate_with_arrows = 0;
200 /* If true program softkeys (HP terminals only) on startup and after every
201 command ran in the subshell to the description found in the termcap/terminfo
202 database */
203 int reset_hp_softkeys = 0;
205 /* The prompt */
206 const char *mc_prompt = NULL;
208 /* The widget where we draw the prompt */
209 WLabel *the_prompt;
211 /* The hint bar */
212 WLabel *the_hint;
214 /* The button bar */
215 WButtonBar *the_bar;
217 /* Mouse type: GPM, xterm or none */
218 Mouse_Type use_mouse_p = MOUSE_NONE;
220 /* If on, default for "No" in delete operations */
221 int safe_delete = 0;
223 /* Controls screen clearing before an exec */
224 int clear_before_exec = 1;
226 /* Asks for confirmation before deleting a file */
227 int confirm_delete = 1;
229 /* Asks for confirmation before deleting a hotlist entry */
230 int confirm_directory_hotlist_delete = 1;
232 /* Asks for confirmation before overwriting a file */
233 int confirm_overwrite = 1;
235 /* Asks for confirmation before executing a program by pressing enter */
236 int confirm_execute = 0;
238 /* Asks for confirmation before leaving the program */
239 int confirm_exit = 1;
241 /* Asks for confirmation before clean up of history */
242 int confirm_history_cleanup = 1;
244 /* Asks for confirmation when using F3 to view a directory and there
245 are tagged files */
246 int confirm_view_dir = 0;
248 /* This flag indicates if the pull down menus by default drop down */
249 int drop_menus = 0;
251 /* The dialog handle for the main program */
252 Dlg_head *midnight_dlg = NULL;
254 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
255 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
256 int update_prompt = 0;
258 /* The home directory */
259 const char *home_dir = NULL;
261 /* Tab size */
262 int option_tab_spacing = 8;
264 /* The value of the other directory, only used when loading the setup */
265 char *other_dir = NULL;
267 /* Only used at program boot */
268 int boot_current_is_left = 1;
270 static char *this_dir = NULL;
272 /* If this is true, then when browsing the tree the other window will
273 * automatically reload it's directory with the contents of the currently
274 * selected directory.
276 int xtree_mode = 0;
278 /* If set, then print to the given file the last directory we were at */
279 static char *last_wd_string = NULL;
280 /* Set to 1 to suppress printing the last directory */
281 static int print_last_revert = 0;
283 /* File name to view if argument was supplied */
284 const char *view_one_file = NULL;
286 /* File name to edit if argument was supplied */
287 const char *edit_one_file = NULL;
289 /* Line to start the editor on */
290 static int edit_one_file_start_line = 0;
292 /* Used so that widgets know if they are being destroyed or
293 shut down */
294 int midnight_shutdown = 0;
296 /* The user's shell */
297 char *shell = NULL;
299 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
300 char *mc_home = NULL;
302 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
303 char *mc_home_alt = NULL;
305 /* Define this function for glib-style error handling */
306 GQuark
307 mc_main_error_quark (void)
309 return g_quark_from_static_string (PACKAGE);
312 #ifdef USE_INTERNAL_EDIT
313 GArray *editor_keymap = NULL;
314 GArray *editor_x_keymap = NULL;
315 #endif
316 GArray *viewer_keymap = NULL;
317 GArray *viewer_hex_keymap = NULL;
318 GArray *main_keymap = NULL;
319 GArray *main_x_keymap = NULL;
320 GArray *panel_keymap = NULL;
321 GArray *input_keymap = NULL;
322 GArray *tree_keymap = NULL;
323 GArray *help_keymap = NULL;
325 const global_keymap_t *main_map;
326 const global_keymap_t *main_x_map;
328 /* Save current stat of directories to avoid reloading the panels */
329 /* when no modifications have taken place */
330 void
331 save_cwds_stat (void)
333 if (fast_reload) {
334 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
335 if (get_other_type () == view_listing)
336 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
340 #ifdef HAVE_SUBSHELL_SUPPORT
341 void
342 do_update_prompt (void)
344 if (update_prompt) {
345 printf ("\r\n%s", subshell_prompt);
346 fflush (stdout);
347 update_prompt = 0;
350 #endif /* HAVE_SUBSHELL_SUPPORT */
352 void
353 change_panel (void)
355 free_completions (cmdline);
356 dlg_one_down (midnight_dlg);
359 /* Stop MC main dialog and the current dialog if it exists.
360 * Needed to provide fast exit from MC viewer or editor on shell exit */
361 static void
362 stop_dialogs (void)
364 midnight_dlg->running = 0;
365 if (current_dlg) {
366 current_dlg->running = 0;
370 static int
371 quit_cmd_internal (int quiet)
373 int q = quit;
375 if (quiet || !confirm_exit) {
376 q = 1;
377 } else {
378 if (query_dialog
379 (_(" The Midnight Commander "),
380 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
381 2, _("&Yes"), _("&No")) == 0)
382 q = 1;
384 if (q) {
385 #ifdef HAVE_SUBSHELL_SUPPORT
386 if (!use_subshell)
387 stop_dialogs ();
388 else if ((q = exit_subshell ()))
389 #endif
390 stop_dialogs ();
392 if (q)
393 quit |= 1;
394 return quit;
397 static void
398 quit_cmd (void)
400 quit_cmd_internal (0);
403 void
404 quiet_quit_cmd (void)
406 print_last_revert = 1;
407 quit_cmd_internal (1);
410 /* Wrapper for do_subshell_chdir, check for availability of subshell */
411 void
412 subshell_chdir (const char *directory)
414 #ifdef HAVE_SUBSHELL_SUPPORT
415 if (use_subshell) {
416 if (vfs_current_is_local ())
417 do_subshell_chdir (directory, 0, 1);
419 #endif /* HAVE_SUBSHELL_SUPPORT */
422 void
423 directory_history_add (struct WPanel *panel, const char *dir)
425 char *tmp;
427 tmp = g_strdup (dir);
428 strip_password (tmp, 1);
430 panel->dir_history = list_append_unique (panel->dir_history, tmp);
434 * If we moved to the parent directory move the selection pointer to
435 * the old directory name; If we leave VFS dir, remove FS specificator.
437 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
439 static const char *
440 get_parent_dir_name (const char *cwd, const char *lwd)
442 const char *p;
443 if (strlen (lwd) > strlen (cwd))
444 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
445 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
446 cwd[1] == '\0'))) {
447 return (p + 1);
449 return NULL;
453 * Changes the current directory of the panel.
454 * Don't record change in the directory history.
456 static int
457 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
459 const char *directory;
460 char *olddir;
461 char temp[MC_MAXPATHLEN];
462 char *translated_url;
464 if (cd_type == cd_parse_command) {
465 while (*new_dir == ' ')
466 new_dir++;
469 olddir = g_strdup (panel->cwd);
470 new_dir = translated_url = vfs_translate_url (new_dir);
472 /* Convert *new_path to a suitable pathname, handle ~user */
474 if (cd_type == cd_parse_command) {
475 if (!strcmp (new_dir, "-")) {
476 strcpy (temp, panel->lwd);
477 new_dir = temp;
480 directory = *new_dir ? new_dir : home_dir;
482 if (mc_chdir (directory) == -1) {
483 strcpy (panel->cwd, olddir);
484 g_free (olddir);
485 g_free (translated_url);
486 return 0;
488 g_free (translated_url);
490 /* Success: save previous directory, shutdown status of previous dir */
491 strcpy (panel->lwd, olddir);
492 free_completions (cmdline);
494 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
496 vfs_release_path (olddir);
498 subshell_chdir (panel->cwd);
500 /* Reload current panel */
501 panel_clean_dir (panel);
502 panel->count =
503 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
504 panel->reverse, panel->case_sensitive,
505 panel->exec_first, panel->filter);
506 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
507 load_hint (0);
508 panel->dirty = 1;
509 update_xterm_title_path ();
511 g_free (olddir);
513 return 1;
517 * Changes the current directory of the panel.
518 * Record change in the directory history.
521 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
523 int r;
525 r = _do_panel_cd (panel, new_dir, cd_type);
526 if (r)
527 directory_history_add (panel, panel->cwd);
528 return r;
532 do_cd (const char *new_dir, enum cd_enum exact)
534 return (do_panel_cd (current_panel, new_dir, exact));
537 void
538 directory_history_next (WPanel *panel)
540 GList *nextdir;
542 nextdir = g_list_next (panel->dir_history);
544 if (!nextdir)
545 return;
547 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
548 panel->dir_history = nextdir;
551 void
552 directory_history_prev (WPanel *panel)
554 GList *prevdir;
556 prevdir = g_list_previous (panel->dir_history);
558 if (!prevdir)
559 return;
561 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
562 panel->dir_history = prevdir;
565 void
566 directory_history_list (WPanel *panel)
568 char *s;
570 s = show_hist (&panel->dir_history, &panel->widget);
572 if (s != NULL) {
573 if (_do_panel_cd (panel, s, cd_exact))
574 directory_history_add (panel, panel->cwd);
575 else
576 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
577 g_free (s);
581 #ifdef HAVE_SUBSHELL_SUPPORT
583 load_prompt (int fd, void *unused)
585 (void) fd;
586 (void) unused;
588 if (!read_subshell_prompt ())
589 return 0;
591 /* Don't actually change the prompt if it's invisible */
592 if (current_dlg == midnight_dlg && command_prompt) {
593 char *tmp_prompt;
594 int prompt_len;
596 tmp_prompt = strip_ctrl_codes (subshell_prompt);
597 prompt_len = str_term_width1 (tmp_prompt);
599 /* Check for prompts too big */
600 if (COLS > 8 && prompt_len > COLS - 8) {
601 tmp_prompt[COLS - 8] = '\0';
602 prompt_len = COLS - 8;
604 mc_prompt = tmp_prompt;
605 label_set_text (the_prompt, mc_prompt);
606 winput_set_origin ((WInput *) cmdline, prompt_len,
607 COLS - prompt_len);
609 /* since the prompt has changed, and we are called from one of the
610 * tty_get_event channels, the prompt updating does not take place
611 * automatically: force a cursor update and a screen refresh
613 update_cursor (midnight_dlg);
614 mc_refresh ();
616 update_prompt = 1;
617 return 0;
619 #endif /* HAVE_SUBSHELL_SUPPORT */
621 void
622 sort_cmd (void)
624 WPanel *p;
625 const panel_field_t *sort_order;
627 if (!SELECTED_IS_PANEL)
628 return;
630 p = MENU_PANEL;
631 sort_order = sort_box (p->current_sort_field, &p->reverse,
632 &p->case_sensitive,
633 &p->exec_first);
635 panel_set_sort_order (p, sort_order);
639 static void
640 treebox_cmd (void)
642 char *sel_dir;
644 sel_dir = tree_box (selection (current_panel)->fname);
645 if (sel_dir) {
646 do_cd (sel_dir, cd_exact);
647 g_free (sel_dir);
651 #ifdef LISTMODE_EDITOR
652 static void
653 listmode_cmd (void)
655 char *newmode;
657 if (get_current_type () != view_listing)
658 return;
660 newmode = listmode_edit (current_panel->user_format);
661 if (!newmode)
662 return;
664 g_free (current_panel->user_format);
665 current_panel->list_type = list_user;
666 current_panel->user_format = newmode;
667 set_panel_formats (current_panel);
669 do_refresh ();
671 #endif /* LISTMODE_EDITOR */
673 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
674 static GList *
675 create_panel_menu (void)
677 GList *entries = NULL;
679 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
680 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
681 entries = g_list_append (entries, menu_entry_create (_("&Info" ), CK_InfoCmd));
682 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
683 entries = g_list_append (entries, menu_separator_create ());
684 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
685 entries = g_list_append (entries, menu_separator_create ());
686 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
687 #ifdef HAVE_CHARSET
688 entries = g_list_append (entries, menu_separator_create ());
689 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
690 #endif
691 #ifdef USE_NETCODE
692 entries = g_list_append (entries, menu_separator_create ());
693 #ifdef ENABLE_VFS_MCFS
694 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
695 #endif
696 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
697 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
698 #ifdef ENABLE_VFS_SMB
699 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
700 #endif /* ENABLE_VFS_SMB */
701 #endif
702 entries = g_list_append (entries, menu_separator_create ());
703 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
705 return entries;
708 static GList *
709 create_file_menu (void)
711 GList *entries = NULL;
713 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
714 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
715 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
716 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
717 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
718 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
719 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
720 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
721 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
722 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
723 entries = g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
724 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
725 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
726 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
727 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
728 entries = g_list_append (entries, menu_separator_create ());
729 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
730 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
731 entries = g_list_append (entries, menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
732 entries = g_list_append (entries, menu_separator_create ());
733 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
735 return entries;
738 static GList *
739 create_command_menu (void)
741 /* I know, I'm lazy, but the tree widget when it's not running
742 * as a panel still has some problems, I have not yet finished
743 * the WTree widget port, sorry.
745 GList *entries = NULL;
747 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
748 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
749 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
750 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
751 entries = g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
752 entries = g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
753 entries = g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
754 entries = g_list_append (entries, menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
755 entries = g_list_append (entries, menu_separator_create ());
756 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
757 entries = g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
758 #ifdef ENABLE_VFS
759 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
760 #endif
761 #ifdef WITH_BACKGROUND
762 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
763 #endif
764 entries = g_list_append (entries, menu_separator_create ());
765 #ifdef USE_EXT2FSLIB
766 entries = g_list_append (entries, menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
767 #endif
768 #ifdef LISTMODE_EDITOR
769 entries = g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
770 #endif
771 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
772 entries = g_list_append (entries, menu_separator_create ());
773 #endif
774 entries = g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
775 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
776 entries = g_list_append (entries, menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
778 return entries;
781 static GList *
782 create_options_menu (void)
784 GList *entries = NULL;
786 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
787 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
788 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
789 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
790 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
791 #ifdef ENABLE_VFS
792 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
793 #endif
794 entries = g_list_append (entries, menu_separator_create ());
795 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
797 return entries;
800 void
801 init_menu (void)
803 menubar_add_menu (the_menubar,
804 create_menu (horizontal_split ? _("&Above") : _("&Left"),
805 create_panel_menu (), "[Left and Right Menus]"));
806 menubar_add_menu (the_menubar,
807 create_menu (_("&File"), create_file_menu (), "[File Menu]"));
808 menubar_add_menu (the_menubar,
809 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
810 menubar_add_menu (the_menubar,
811 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
812 menubar_add_menu (the_menubar,
813 create_menu (horizontal_split ? _("&Below") : _("&Right"),
814 create_panel_menu (), "[Left and Right Menus]"));
817 void
818 done_menu (void)
820 menubar_set_menu (the_menubar, NULL);
823 static void
824 menu_last_selected_cmd (void)
826 the_menubar->is_active = TRUE;
827 the_menubar->is_dropped = (drop_menus != 0);
828 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
829 dlg_select_widget (the_menubar);
832 static void
833 menu_cmd (void)
835 if (the_menubar->is_active)
836 return;
838 if ((get_current_index () == 0) == (current_panel->active != 0))
839 the_menubar->selected = 0;
840 else
841 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
842 menu_last_selected_cmd ();
845 static char *
846 midnight_get_shortcut (unsigned long command)
848 const char *ext_map;
849 const char *shortcut = NULL;
851 shortcut = lookup_keymap_shortcut (main_map, command);
852 if (shortcut != NULL)
853 return g_strdup (shortcut);
855 shortcut = lookup_keymap_shortcut (panel_map, command);
856 if (shortcut != NULL)
857 return g_strdup (shortcut);
859 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
860 if (ext_map != NULL)
861 shortcut = lookup_keymap_shortcut (main_x_map, command);
862 if (shortcut != NULL)
863 return g_strdup_printf ("%s %s", ext_map, shortcut);
865 return NULL;
868 /* Flag toggling functions */
869 void
870 toggle_fast_reload (void)
872 fast_reload = !fast_reload;
873 if (fast_reload_w == 0 && fast_reload) {
874 message (D_NORMAL, _(" Information "),
876 (" Using the fast reload option may not reflect the exact \n"
877 " directory contents. In this case you'll need to do a \n"
878 " manual reload of the directory. See the man page for \n"
879 " the details. "));
880 fast_reload_w = 1;
884 void
885 toggle_mix_all_files (void)
887 mix_all_files = !mix_all_files;
888 update_panels (UP_RELOAD, UP_KEEPSEL);
891 void
892 toggle_show_backup (void)
894 show_backups = !show_backups;
895 update_panels (UP_RELOAD, UP_KEEPSEL);
898 void
899 toggle_show_hidden (void)
901 show_dot_files = !show_dot_files;
902 update_panels (UP_RELOAD, UP_KEEPSEL);
905 static void
906 toggle_panels_split (void)
908 horizontal_split = !horizontal_split;
909 layout_change ();
910 do_refresh();
913 void
914 toggle_kilobyte_si (void)
916 kilobyte_si = !kilobyte_si;
917 update_panels (UP_RELOAD, UP_KEEPSEL);
921 * Just a hack for allowing url-like pathnames to be accepted from the
922 * command line.
924 static void
925 translated_mc_chdir (char *dir)
927 char *newdir;
929 newdir = vfs_translate_url (dir);
930 mc_chdir (newdir);
931 g_free (newdir);
934 static void
935 create_panels (void)
937 int current_index;
938 int other_index;
939 panel_view_mode_t current_mode, other_mode;
940 char original_dir[1024];
942 original_dir[0] = 0;
944 if (boot_current_is_left) {
945 current_index = 0;
946 other_index = 1;
947 current_mode = startup_left_mode;
948 other_mode = startup_right_mode;
949 } else {
950 current_index = 1;
951 other_index = 0;
952 current_mode = startup_right_mode;
953 other_mode = startup_left_mode;
955 /* Creates the left panel */
956 if (this_dir) {
957 if (other_dir) {
958 /* Ok, user has specified two dirs, save the original one,
959 * since we may not be able to chdir to the proper
960 * second directory later
962 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
964 translated_mc_chdir (this_dir);
966 set_display_type (current_index, current_mode);
968 /* The other panel */
969 if (other_dir) {
970 if (original_dir[0])
971 translated_mc_chdir (original_dir);
972 translated_mc_chdir (other_dir);
974 set_display_type (other_index, other_mode);
976 if (startup_left_mode == view_listing) {
977 current_panel = left_panel;
978 } else {
979 if (right_panel)
980 current_panel = right_panel;
981 else
982 current_panel = left_panel;
985 /* Create the nice widgets */
986 cmdline = command_new (0, 0, 0);
987 the_prompt = label_new (0, 0, mc_prompt);
988 the_prompt->transparent = 1;
989 the_bar = buttonbar_new (keybar_visible);
991 the_hint = label_new (0, 0, 0);
992 the_hint->transparent = 1;
993 the_hint->auto_adjust_cols = 0;
994 the_hint->widget.cols = COLS;
996 the_menubar = menubar_new (0, 0, COLS, NULL);
999 static void
1000 copy_current_pathname (void)
1002 char *cwd_path;
1003 if (!command_prompt)
1004 return;
1006 cwd_path = remove_encoding_from_path (current_panel->cwd);
1007 command_insert (cmdline, cwd_path, 0);
1009 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1010 command_insert (cmdline, PATH_SEP_STR, 0);
1011 g_free (cwd_path);
1014 static void
1015 copy_other_pathname (void)
1017 char *cwd_path;
1019 if (get_other_type () != view_listing)
1020 return;
1022 if (!command_prompt)
1023 return;
1025 cwd_path = remove_encoding_from_path (other_panel->cwd);
1026 command_insert (cmdline, cwd_path, 0);
1028 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1029 command_insert (cmdline, PATH_SEP_STR, 0);
1030 g_free (cwd_path);
1033 static void
1034 copy_readlink (WPanel *panel)
1036 if (!command_prompt)
1037 return;
1038 if (S_ISLNK (selection (panel)->st.st_mode)) {
1039 char buffer[MC_MAXPATHLEN];
1040 char *p =
1041 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1042 int i;
1044 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1045 g_free (p);
1046 if (i > 0) {
1047 buffer[i] = 0;
1048 command_insert (cmdline, buffer, 1);
1053 static void
1054 copy_current_readlink (void)
1056 copy_readlink (current_panel);
1059 static void
1060 copy_other_readlink (void)
1062 if (get_other_type () == view_listing)
1063 copy_readlink (other_panel);
1066 /* Insert the selected file name into the input line */
1067 static void
1068 copy_prog_name (void)
1070 char *tmp;
1071 if (!command_prompt)
1072 return;
1074 if (get_current_type () == view_tree) {
1075 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1076 tmp = tree_selected_name (tree);
1077 } else
1078 tmp = selection (current_panel)->fname;
1080 command_insert (cmdline, tmp, 1);
1083 static void
1084 copy_tagged (WPanel *panel)
1086 int i;
1088 if (!command_prompt)
1089 return;
1090 input_disable_update (cmdline);
1091 if (panel->marked) {
1092 for (i = 0; i < panel->count; i++) {
1093 if (panel->dir.list[i].f.marked)
1094 command_insert (cmdline, panel->dir.list[i].fname, 1);
1096 } else {
1097 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1100 input_enable_update (cmdline);
1103 static void
1104 copy_current_tagged (void)
1106 copy_tagged (current_panel);
1109 static void
1110 copy_other_tagged (void)
1112 if (get_other_type () == view_listing)
1113 copy_tagged (other_panel);
1116 void
1117 midnight_set_buttonbar (WButtonBar *b)
1119 buttonbar_set_label (b, 1, Q_("ButtonBar|Help"), main_map, NULL);
1120 buttonbar_set_label (b, 2, Q_("ButtonBar|Menu"), main_map, NULL);
1121 buttonbar_set_label (b, 3, Q_("ButtonBar|View"), main_map, NULL);
1122 buttonbar_set_label (b, 4, Q_("ButtonBar|Edit"), main_map, NULL);
1123 buttonbar_set_label (b, 5, Q_("ButtonBar|Copy"), main_map, NULL);
1124 buttonbar_set_label (b, 6, Q_("ButtonBar|RenMov"), main_map, NULL);
1125 buttonbar_set_label (b, 7, Q_("ButtonBar|Mkdir"), main_map, NULL);
1126 buttonbar_set_label (b, 8, Q_("ButtonBar|Delete"), main_map, NULL);
1127 buttonbar_set_label (b, 9, Q_("ButtonBar|PullDn"), main_map, NULL);
1128 buttonbar_set_label (b, 10, Q_("ButtonBar|Quit"), main_map, NULL);
1131 static gboolean ctl_x_map_enabled = FALSE;
1133 static void
1134 ctl_x_cmd (void)
1136 ctl_x_map_enabled = TRUE;
1139 static cb_ret_t
1140 midnight_execute_cmd (Widget *sender, unsigned long command)
1142 cb_ret_t res = MSG_HANDLED;
1144 (void) sender;
1146 switch (command) {
1147 case CK_AddHotlist:
1148 add2hotlist_cmd ();
1149 break;
1150 case CK_ChmodCmd:
1151 chmod_cmd ();
1152 break;
1153 case CK_ChownCmd:
1154 chown_cmd ();
1155 break;
1156 case CK_ChownAdvancedCmd:
1157 chown_advanced_cmd ();
1158 break;
1159 case CK_CompareDirsCmd:
1160 compare_dirs_cmd ();
1161 break;
1162 case CK_ConfigureBox:
1163 configure_box ();
1164 break;
1165 #ifdef ENABLE_VFS
1166 case CK_ConfigureVfs:
1167 configure_vfs ();
1168 break;
1169 #endif
1170 case CK_ConfirmBox:
1171 confirm_box ();
1172 break;
1173 case CK_CopyCmd:
1174 copy_cmd ();
1175 break;
1176 case CK_CopyCurrentPathname:
1177 copy_current_pathname ();
1178 break;
1179 case CK_CopyCurrentReadlink:
1180 copy_current_readlink ();
1181 break;
1182 case CK_CopyCurrentTagged:
1183 copy_current_tagged ();
1184 break;
1185 case CK_CopyOtherPathname:
1186 copy_other_pathname ();
1187 break;
1188 case CK_CopyOtherReadlink:
1189 copy_other_readlink ();
1190 break;
1191 case CK_CopyOtherTagged:
1192 copy_other_tagged ();
1193 break;
1194 case CK_DeleteCmd:
1195 delete_cmd ();
1196 break;
1197 case CK_DisplayBitsBox:
1198 display_bits_box ();
1199 break;
1200 case CK_EditCmd:
1201 edit_cmd ();
1202 break;
1203 case CK_EditExtFileCmd:
1204 ext_cmd ();
1205 break;
1206 case CK_EditFhlFileCmd:
1207 edit_fhl_cmd ();
1208 break;
1209 case CK_EditMcMenuCmd:
1210 edit_mc_menu_cmd ();
1211 break;
1212 case CK_EditSymlinkCmd:
1213 edit_symlink_cmd ();
1214 break;
1215 case CK_ExternalPanelize:
1216 external_panelize ();
1217 break;
1218 case CK_FilterCmd:
1219 filter_cmd ();
1220 break;
1221 case CK_FilteredViewCmd:
1222 filtered_view_cmd ();
1223 break;
1224 case CK_FindCmd:
1225 find_cmd ();
1226 break;
1227 #if defined (USE_NETCODE)
1228 case CK_FishlinkCmd:
1229 fishlink_cmd ();
1230 break;
1231 case CK_FtplinkCmd:
1232 ftplink_cmd ();
1233 break;
1234 #endif
1235 case CK_HelpCmd:
1236 help_cmd ();
1237 break;
1238 case CK_HistoryCmd:
1239 history_cmd ();
1240 break;
1241 case CK_InfoCmd:
1242 if (sender == (Widget *) the_menubar)
1243 info_cmd (); /* mwnu */
1244 else
1245 info_cmd_no_menu (); /* shortcut or buttonbar */
1246 break;
1247 #ifdef WITH_BACKGROUND
1248 case CK_JobsCmd:
1249 jobs_cmd ();
1250 break;
1251 #endif
1252 case CK_LayoutCmd:
1253 layout_cmd ();
1254 break;
1255 case CK_LearnKeys:
1256 learn_keys ();
1257 break;
1258 case CK_LinkCmd:
1259 link_cmd ();
1260 break;
1261 case CK_ListingCmd:
1262 listing_cmd ();
1263 break;
1264 #ifdef LISTMODE_EDITOR
1265 case CK_ListmodeCmd:
1266 listmode_cmd ();
1267 break;
1268 #endif
1269 case CK_MenuCmd:
1270 menu_cmd ();
1271 break;
1272 case CK_MenuLastSelectedCmd:
1273 menu_last_selected_cmd ();
1274 break;
1275 case CK_MkdirCmd:
1276 mkdir_cmd ();
1277 break;
1278 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1279 case CK_NetlinkCmd:
1280 netlink_cmd ();
1281 break;
1282 #endif
1283 #ifdef HAVE_CHARSET
1284 case CK_PanelSetPanelEncoding:
1285 encoding_cmd ();
1286 break;
1287 #endif
1288 case CK_QuickCdCmd:
1289 quick_cd_cmd ();
1290 break;
1291 case CK_QuickChdirCmd:
1292 quick_chdir_cmd ();
1293 break;
1294 case CK_QuickViewCmd:
1295 if (sender == (Widget *) the_menubar)
1296 quick_view_cmd (); /* menu */
1297 else
1298 quick_cmd_no_menu (); /* shortcut or buttonabr */
1299 break;
1300 case CK_QuietQuitCmd:
1301 quiet_quit_cmd ();
1302 break;
1303 case CK_QuitCmd:
1304 quit_cmd ();
1305 break;
1306 case CK_RenameCmd:
1307 rename_cmd ();
1308 break;
1309 case CK_RereadCmd:
1310 reread_cmd ();
1311 break;
1312 #ifdef ENABLE_VFS
1313 case CK_ReselectVfs:
1314 reselect_vfs ();
1315 break;
1316 #endif
1317 case CK_ReverseSelectionCmd:
1318 reverse_selection_cmd ();
1319 break;
1320 case CK_SaveSetupCmd:
1321 save_setup_cmd ();
1322 break;
1323 case CK_SelectCmd:
1324 select_cmd ();
1325 break;
1326 case CK_ShowCommandLine:
1327 view_other_cmd ();
1328 break;
1329 case CK_SingleDirsizeCmd:
1330 smart_dirsize_cmd ();
1331 break;
1332 #if defined (USE_NETCODE) && defined (ENABLE_VFS_SMB)
1333 case CK_SmblinkCmd:
1334 smblink_cmd ();
1335 break;
1336 #endif /* USE_NETCODE && ENABLE_VFS_SMB */
1337 case CK_Sort:
1338 sort_cmd ();
1339 break;
1340 case CK_StartExtMap1:
1341 ctl_x_cmd ();
1342 break;
1343 case CK_SuspendCmd:
1344 suspend_cmd ();
1345 break;
1346 case CK_SwapCmd:
1347 swap_cmd ();
1348 break;
1349 case CK_SymlinkCmd:
1350 symlink_cmd ();
1351 break;
1352 case CK_ToggleListingCmd:
1353 toggle_listing_cmd ();
1354 break;
1355 case CK_ToggleShowHidden:
1356 toggle_show_hidden ();
1357 break;
1358 case CK_TogglePanelsSplit:
1359 toggle_panels_split ();
1360 break;
1361 case CK_TreeCmd:
1362 tree_cmd ();
1363 break;
1364 case CK_TreeBoxCmd:
1365 treebox_cmd ();
1366 break;
1367 #ifdef USE_EXT2FSLIB
1368 case CK_UndeleteCmd:
1369 undelete_cmd ();
1370 break;
1371 #endif
1372 case CK_UnselectCmd:
1373 unselect_cmd ();
1374 break;
1375 case CK_UserMenuCmd:
1376 user_file_menu_cmd ();
1377 break;
1378 case CK_ViewCmd:
1379 view_cmd ();
1380 break;
1381 case CK_ViewFileCmd:
1382 view_file_cmd ();
1383 break;
1384 default:
1385 res = MSG_NOT_HANDLED;
1388 return res;
1391 static void
1392 setup_pre (void)
1394 /* Call all the inits */
1396 #ifdef HAVE_SLANG
1397 tty_display_8bit (full_eight_bits != 0);
1398 #else
1399 tty_display_8bit (eight_bit_clean != 0);
1400 #endif
1403 static void
1404 init_xterm_support (void)
1406 const char *termvalue;
1408 termvalue = getenv ("TERM");
1409 if (!termvalue || !(*termvalue)) {
1410 fputs (_("The TERM environment variable is unset!\n"), stderr);
1411 exit (1);
1414 /* Check mouse capabilities */
1415 xmouse_seq = tty_tgetstr ("Km");
1417 if (strcmp (termvalue, "cygwin") == 0) {
1418 mc_args__force_xterm = 1;
1419 use_mouse_p = MOUSE_DISABLED;
1422 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1423 || strncmp (termvalue, "konsole", 7) == 0
1424 || strncmp (termvalue, "rxvt", 4) == 0
1425 || strcmp (termvalue, "Eterm") == 0
1426 || strcmp (termvalue, "dtterm") == 0) {
1427 xterm_flag = 1;
1429 /* Default to the standard xterm sequence */
1430 if (!xmouse_seq) {
1431 xmouse_seq = ESC_STR "[M";
1434 /* Enable mouse unless explicitly disabled by --nomouse */
1435 if (use_mouse_p != MOUSE_DISABLED) {
1436 const char *color_term = getenv ("COLORTERM");
1437 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1438 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1439 strcmp (termvalue, "Eterm") == 0) {
1440 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1441 } else {
1442 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1448 static void
1449 setup_mc (void)
1451 setup_pre ();
1452 create_panels ();
1453 setup_panels ();
1455 #ifdef HAVE_SUBSHELL_SUPPORT
1456 if (use_subshell)
1457 add_select_channel (subshell_pty, load_prompt, 0);
1458 #endif /* !HAVE_SUBSHELL_SUPPORT */
1460 tty_setup_sigwinch (sigwinch_handler);
1462 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1464 init_xterm_support ();
1465 init_mouse ();
1468 static void
1469 setup_dummy_mc (void)
1471 char d[MC_MAXPATHLEN];
1473 mc_get_current_wd (d, MC_MAXPATHLEN);
1474 setup_mc ();
1475 mc_chdir (d);
1478 static void check_codeset()
1480 const char *current_system_codepage = NULL;
1482 current_system_codepage = str_detect_termencoding();
1484 #ifdef HAVE_CHARSET
1486 const char *_display_codepage;
1488 _display_codepage = get_codepage_id (display_codepage);
1490 if (! strcmp(_display_codepage, current_system_codepage)) {
1491 utf8_display = str_isutf8 (current_system_codepage);
1492 return;
1495 display_codepage = get_codepage_index (current_system_codepage);
1496 if (display_codepage == -1) {
1497 display_codepage = 0;
1500 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1502 #endif
1503 utf8_display = str_isutf8 (current_system_codepage);
1506 static void
1507 done_screen (void)
1509 if (!(quit & SUBSHELL_EXIT))
1510 clr_scr ();
1511 tty_reset_shell_mode ();
1512 tty_noraw_mode ();
1513 tty_keypad (FALSE);
1514 tty_colors_done ();
1517 static void
1518 done_mc (void)
1520 disable_mouse ();
1522 /* Setup shutdown
1524 * We sync the profiles since the hotlist may have changed, while
1525 * we only change the setup data if we have the auto save feature set
1528 if (auto_save_setup)
1529 save_setup (); /* does also call save_hotlist */
1530 else {
1531 save_hotlist ();
1532 save_panel_types ();
1534 done_screen ();
1535 vfs_add_current_stamps ();
1538 /* This should be called after destroy_dlg since panel widgets
1539 * save their state on the profiles
1541 static void
1542 done_mc_profile (void)
1544 done_setup ();
1547 static cb_ret_t
1548 midnight_callback (Dlg_head *h, Widget *sender,
1549 dlg_msg_t msg, int parm, void *data)
1551 unsigned long command;
1553 switch (msg) {
1555 case DLG_DRAW:
1556 load_hint (1);
1557 /* We handle the special case of the output lines */
1558 if (console_flag && output_lines)
1559 show_console_contents (output_start_y,
1560 LINES - output_lines - keybar_visible -
1561 1, LINES - keybar_visible - 1);
1562 return MSG_HANDLED;
1564 case DLG_RESIZE:
1565 setup_panels ();
1566 menubar_arrange (the_menubar);
1567 return MSG_HANDLED;
1569 case DLG_IDLE:
1570 /* We only need the first idle event to show user menu after start */
1571 set_idle_proc (h, 0);
1572 if (auto_menu)
1573 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1574 return MSG_HANDLED;
1576 case DLG_KEY:
1577 if (ctl_x_map_enabled) {
1578 ctl_x_map_enabled = FALSE;
1579 command = lookup_keymap_command (main_x_map, parm);
1580 if (command != CK_Ignore_Key)
1581 return midnight_execute_cmd (NULL, command);
1584 /* FIXME: should handle all menu shortcuts before this point */
1585 if (the_menubar->is_active)
1586 return MSG_NOT_HANDLED;
1588 if (parm == '\t')
1589 free_completions (cmdline);
1591 if (parm == '\n') {
1592 size_t i;
1594 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1595 cmdline->buffer[i] == '\t'); i++)
1597 if (cmdline->buffer[i]) {
1598 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1599 return MSG_HANDLED;
1601 stuff (cmdline, "", 0);
1602 cmdline->point = 0;
1605 /* Ctrl-Enter and Alt-Enter */
1606 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1607 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1608 copy_prog_name ();
1609 return MSG_HANDLED;
1612 /* Ctrl-Shift-Enter */
1613 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1614 copy_current_pathname ();
1615 copy_prog_name ();
1616 return MSG_HANDLED;
1619 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1620 && !quote && !current_panel->searching) {
1621 if (!only_leading_plus_minus) {
1622 /* Special treatement, since the input line will eat them */
1623 if (parm == '+') {
1624 select_cmd ();
1625 return MSG_HANDLED;
1628 if (parm == '\\' || parm == '-') {
1629 unselect_cmd ();
1630 return MSG_HANDLED;
1633 if (parm == '*') {
1634 reverse_selection_cmd ();
1635 return MSG_HANDLED;
1637 } else if (!command_prompt || !cmdline->buffer[0]) {
1638 /* Special treatement '+', '-', '\', '*' only when this is
1639 * first char on input line
1642 if (parm == '+') {
1643 select_cmd ();
1644 return MSG_HANDLED;
1647 if (parm == '\\' || parm == '-') {
1648 unselect_cmd ();
1649 return MSG_HANDLED;
1652 if (parm == '*') {
1653 reverse_selection_cmd ();
1654 return MSG_HANDLED;
1658 return MSG_NOT_HANDLED;
1660 case DLG_HOTKEY_HANDLED:
1661 if ((get_current_type () == view_listing) && current_panel->searching) {
1662 current_panel->searching = 0;
1663 current_panel->dirty = 1;
1665 return MSG_HANDLED;
1667 case DLG_UNHANDLED_KEY:
1668 if (command_prompt) {
1669 cb_ret_t v;
1671 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1672 if (v == MSG_HANDLED)
1673 return MSG_HANDLED;
1676 if (ctl_x_map_enabled) {
1677 ctl_x_map_enabled = FALSE;
1678 command = lookup_keymap_command (main_x_map, parm);
1679 } else
1680 command = lookup_keymap_command (main_map, parm);
1682 return (command == CK_Ignore_Key)
1683 ? MSG_NOT_HANDLED
1684 : midnight_execute_cmd (NULL, command);
1686 case DLG_POST_KEY:
1687 if (!the_menubar->is_active)
1688 update_dirty_panels ();
1689 return MSG_HANDLED;
1691 case DLG_ACTION:
1692 /* shortcut */
1693 if (sender == NULL)
1694 midnight_execute_cmd (NULL, parm);
1695 /* message from menu */
1696 else if (sender == (Widget *) the_menubar)
1697 midnight_execute_cmd (sender, parm);
1698 /* message from buttonbar */
1699 else if (sender == (Widget *) the_bar) {
1700 if (data == NULL)
1701 midnight_execute_cmd (sender, parm);
1702 else
1703 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1705 return MSG_HANDLED;
1707 default:
1708 return default_dlg_callback (h, sender, msg, parm, data);
1712 /* Show current directory in the xterm title */
1713 void
1714 update_xterm_title_path (void)
1716 const char *path;
1717 char host[BUF_TINY];
1718 char *p;
1719 struct passwd *pw = NULL;
1720 char *login = NULL;
1721 int res = 0;
1722 if (xterm_flag && xterm_title) {
1723 path = strip_home_and_password (current_panel->cwd);
1724 res = gethostname(host, sizeof (host));
1725 if ( res ) { /* On success, res = 0 */
1726 host[0] = '\0';
1727 } else {
1728 host[sizeof (host) - 1] = '\0';
1730 pw = getpwuid(getuid());
1731 if ( pw ) {
1732 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1733 } else {
1734 login = g_strdup (host);
1736 p = g_strdup_printf ("mc [%s]:%s", login, path);
1737 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1738 g_free (login);
1739 g_free (p);
1740 if (!alternate_plus_minus)
1741 numeric_keypad_mode ();
1742 fflush (stdout);
1747 * Load new hint and display it.
1748 * IF force is not 0, ignore the timeout.
1750 void
1751 load_hint (int force)
1753 char *hint;
1755 if (!the_hint->widget.parent)
1756 return;
1758 if (!message_visible) {
1759 label_set_text (the_hint, NULL);
1760 return;
1763 hint = get_random_hint (force);
1765 if (hint != NULL) {
1766 if (*hint)
1767 set_hintbar (hint);
1768 g_free (hint);
1769 } else {
1770 char text[BUF_SMALL];
1772 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1773 VERSION);
1774 set_hintbar (text);
1778 static void
1779 setup_panels_and_run_mc (void)
1781 midnight_dlg->get_shortcut = midnight_get_shortcut;
1783 add_widget (midnight_dlg, the_menubar);
1784 init_menu ();
1786 add_widget (midnight_dlg, get_panel_widget (0));
1787 add_widget (midnight_dlg, get_panel_widget (1));
1788 add_widget (midnight_dlg, the_hint);
1789 add_widget (midnight_dlg, cmdline);
1790 add_widget (midnight_dlg, the_prompt);
1792 add_widget (midnight_dlg, the_bar);
1793 midnight_set_buttonbar (the_bar);
1795 if (boot_current_is_left)
1796 dlg_select_widget (get_panel_widget (0));
1797 else
1798 dlg_select_widget (get_panel_widget (1));
1800 /* Run the Midnight Commander if no file was specified in the command line */
1801 run_dlg (midnight_dlg);
1804 /* result must be free'd (I think this should go in util.c) */
1805 static char *
1806 prepend_cwd_on_local (const char *filename)
1808 char *d;
1809 int l;
1811 if (vfs_file_is_local (filename)) {
1812 if (*filename == PATH_SEP) /* an absolute pathname */
1813 return g_strdup (filename);
1814 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1815 mc_get_current_wd (d, MC_MAXPATHLEN);
1816 l = strlen (d);
1817 d[l++] = PATH_SEP;
1818 strcpy (d + l, filename);
1819 canonicalize_pathname (d);
1820 return d;
1821 } else
1822 return g_strdup (filename);
1825 static int
1826 mc_maybe_editor_or_viewer (void)
1828 if (!(view_one_file || edit_one_file))
1829 return 0;
1831 /* Invoke the internal view/edit routine with:
1832 * the default processing and forcing the internal viewer/editor
1834 if (view_one_file) {
1835 char *path = NULL;
1836 path = prepend_cwd_on_local (view_one_file);
1837 view_file (path, 0, 1);
1838 g_free (path);
1840 #ifdef USE_INTERNAL_EDIT
1841 else {
1842 edit_file (edit_one_file, edit_one_file_start_line);
1844 #endif /* USE_INTERNAL_EDIT */
1845 midnight_shutdown = 1;
1846 done_mc ();
1847 return 1;
1850 /* Run the main dialog that occupies the whole screen */
1851 static void
1852 do_nc (void)
1854 int midnight_colors[DLG_COLOR_NUM];
1855 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1856 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1857 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1858 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1860 panel_init ();
1862 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1863 "[main]", NULL, DLG_WANT_IDLE);
1865 if (view_one_file || edit_one_file)
1866 setup_dummy_mc ();
1867 else
1868 setup_mc ();
1870 /* start check display_codepage and source_codepage */
1871 check_codeset ();
1873 main_map = default_main_map;
1874 if (main_keymap && main_keymap->len > 0)
1875 main_map = (global_keymap_t *) main_keymap->data;
1877 main_x_map = default_main_x_map;
1878 if (main_x_keymap && main_x_keymap->len > 0)
1879 main_x_map = (global_keymap_t *) main_x_keymap->data;
1881 panel_map = default_panel_keymap;
1882 if (panel_keymap && panel_keymap->len > 0)
1883 panel_map = (global_keymap_t *) panel_keymap->data;
1885 input_map = default_input_keymap;
1886 if (input_keymap && input_keymap->len > 0)
1887 input_map = (global_keymap_t *) input_keymap->data;
1889 tree_map = default_tree_keymap;
1890 if (tree_keymap && tree_keymap->len > 0)
1891 tree_map = (global_keymap_t *) tree_keymap->data;
1893 help_map = default_help_keymap;
1894 if (help_keymap && help_keymap->len > 0)
1895 help_map = (global_keymap_t *) help_keymap->data;
1897 /* Check if we were invoked as an editor or file viewer */
1898 if (!mc_maybe_editor_or_viewer ()) {
1899 setup_panels_and_run_mc ();
1901 /* Program end */
1902 midnight_shutdown = 1;
1904 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1905 if (mc_args__last_wd_file && vfs_current_is_local ())
1906 last_wd_string = g_strdup (current_panel->cwd);
1908 done_mc ();
1911 destroy_dlg (midnight_dlg);
1912 panel_deinit ();
1913 current_panel = 0;
1914 done_mc_profile ();
1917 /* POSIX version. The only version we support. */
1918 static void
1919 OS_Setup (void)
1921 const char *shell_env = getenv ("SHELL");
1922 const char *mc_libdir;
1924 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1925 struct passwd *pwd;
1926 pwd = getpwuid (geteuid ());
1927 if (pwd != NULL)
1928 shell = g_strdup (pwd->pw_shell);
1929 } else
1930 shell = g_strdup (shell_env);
1932 if ((shell == NULL) || (shell[0] == '\0')) {
1933 g_free (shell);
1934 shell = g_strdup ("/bin/sh");
1937 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1938 /* and can be overriden by the MC_DATADIR environment variable */
1939 mc_libdir = getenv ("MC_DATADIR");
1940 if (mc_libdir != NULL) {
1941 mc_home = g_strdup (mc_libdir);
1942 mc_home_alt = g_strdup (SYSCONFDIR);
1943 } else {
1944 mc_home = g_strdup (SYSCONFDIR);
1945 mc_home_alt = g_strdup (DATADIR);
1948 /* This variable is used by the subshell */
1949 home_dir = getenv ("HOME");
1951 if (!home_dir)
1952 home_dir = mc_home;
1955 static void
1956 sigchld_handler_no_subshell (int sig)
1958 #ifdef __linux__
1959 int pid, status;
1961 if (!console_flag)
1962 return;
1964 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1965 the value of console_flag never changed, we could simply not install
1966 this handler at all if (!console_flag && !use_subshell). */
1968 /* That comment is no longer true. We need to wait() on a sigchld
1969 handler (that's at least what the tarfs code expects currently). */
1971 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1973 if (pid == cons_saver_pid) {
1975 if (WIFSTOPPED (status)) {
1976 /* Someone has stopped cons.saver - restart it */
1977 kill (pid, SIGCONT);
1978 } else {
1979 /* cons.saver has died - disable console saving */
1980 handle_console (CONSOLE_DONE);
1981 console_flag = 0;
1984 /* If we got here, some other child exited; ignore it */
1985 #endif /* __linux__ */
1987 (void) sig;
1990 static void
1991 init_sigchld (void)
1993 struct sigaction sigchld_action;
1995 sigchld_action.sa_handler =
1996 #ifdef HAVE_SUBSHELL_SUPPORT
1997 use_subshell ? sigchld_handler :
1998 #endif /* HAVE_SUBSHELL_SUPPORT */
1999 sigchld_handler_no_subshell;
2001 sigemptyset (&sigchld_action.sa_mask);
2003 #ifdef SA_RESTART
2004 sigchld_action.sa_flags = SA_RESTART;
2005 #else
2006 sigchld_action.sa_flags = 0;
2007 #endif /* !SA_RESTART */
2009 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2010 #ifdef HAVE_SUBSHELL_SUPPORT
2012 * This may happen on QNX Neutrino 6, where SA_RESTART
2013 * is defined but not implemented. Fallback to no subshell.
2015 use_subshell = 0;
2016 #endif /* HAVE_SUBSHELL_SUPPORT */
2020 static void
2021 mc_main__setup_by_args (int argc, char *argv[])
2023 const char *base;
2024 char *tmp;
2026 if (mc_args__nomouse)
2027 use_mouse_p = MOUSE_DISABLED;
2029 #ifdef USE_NETCODE
2030 if (mc_args__netfs_logfile != NULL) {
2031 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
2032 #ifdef ENABLE_VFS_SMB
2033 smbfs_set_debugf (mc_args__netfs_logfile);
2034 #endif /* ENABLE_VFS_SMB */
2037 #ifdef ENABLE_VFS_SMB
2038 if (mc_args__debug_level != 0)
2039 smbfs_set_debug (mc_args__debug_level);
2040 #endif /* ENABLE_VFS_SMB */
2041 #endif /* USE_NETCODE */
2043 base = x_basename (argv[0]);
2044 tmp = (argc > 0) ? argv[1] : NULL;
2046 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2047 edit_one_file = "";
2048 if (tmp) {
2050 * Check for filename:lineno, followed by an optional colon.
2051 * This format is used by many programs (especially compilers)
2052 * in error messages and warnings. It is supported so that
2053 * users can quickly copy and paste file locations.
2055 char *end = tmp + strlen (tmp), *p = end;
2056 if (p > tmp && p[-1] == ':')
2057 p--;
2058 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2059 p--;
2060 if (tmp < p && p < end && p[-1] == ':') {
2061 struct stat st;
2062 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2064 * Check that the file before the colon actually exists.
2065 * If it doesn't exist, revert to the old behavior.
2067 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2068 edit_one_file = fname;
2069 edit_one_file_start_line = atoi (p);
2070 } else {
2071 g_free (fname);
2072 goto try_plus_filename;
2074 } else {
2075 try_plus_filename:
2076 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2077 int start_line = atoi (tmp);
2078 if (start_line > 0) {
2079 char *file = (argc > 1) ? argv[2] : NULL;
2080 if (file) {
2081 tmp = file;
2082 edit_one_file_start_line = start_line;
2086 edit_one_file = g_strdup (tmp);
2089 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2090 if (tmp)
2091 view_one_file = g_strdup (tmp);
2092 else {
2093 fputs ("No arguments given to the viewer\n", stderr);
2094 exit (1);
2096 } else {
2097 /* sets the current dir and the other dir */
2098 if (tmp) {
2099 this_dir = g_strdup (tmp);
2100 tmp = (argc > 1) ? argv[2] : NULL;
2101 if (tmp)
2102 other_dir = g_strdup (tmp);
2108 main (int argc, char *argv[])
2110 struct stat s;
2111 char *mc_dir;
2112 GError *error = NULL;
2113 gboolean isInitialized;
2115 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2116 setlocale (LC_ALL, "");
2117 bindtextdomain ("mc", LOCALEDIR);
2118 textdomain ("mc");
2120 /* Set up temporary directory */
2121 mc_tmpdir ();
2123 OS_Setup ();
2125 str_init_strings (NULL);
2127 vfs_init ();
2129 #ifdef USE_INTERNAL_EDIT
2130 edit_stack_init ();
2131 #endif
2133 #ifdef HAVE_SLANG
2134 SLtt_Ignore_Beep = 1;
2135 #endif
2137 if ( !mc_args_handle (&argc, &argv, "mc"))
2138 return 1;
2140 mc_main__setup_by_args (argc,argv);
2142 /* NOTE: This has to be called before tty_init or whatever routine
2143 calls any define_sequence */
2144 init_key ();
2146 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2147 handle_console (CONSOLE_INIT);
2149 #ifdef HAVE_SUBSHELL_SUPPORT
2150 /* Don't use subshell when invoked as viewer or editor */
2151 if (edit_one_file || view_one_file)
2152 use_subshell = 0;
2154 if (use_subshell)
2155 subshell_get_console_attributes ();
2156 #endif /* HAVE_SUBSHELL_SUPPORT */
2158 /* Install the SIGCHLD handler; must be done before init_subshell() */
2159 init_sigchld ();
2161 /* We need this, since ncurses endwin () doesn't restore the signals */
2162 save_stop_handler ();
2164 /* Must be done before init_subshell, to set up the terminal size: */
2165 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2166 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2168 load_setup ();
2170 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2172 isInitialized = mc_skin_init(&error);
2174 mc_filehighlight = mc_fhl_new (TRUE);
2176 dlg_set_default_colors ();
2178 if ( ! isInitialized ) {
2179 message (D_ERROR, _("Warning"), "%s", error->message);
2180 g_error_free(error);
2181 error = NULL;
2184 /* create home directory */
2185 /* do it after the screen library initialization to show the error message */
2186 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2187 canonicalize_pathname (mc_dir);
2188 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2189 && mkdir (mc_dir, 0700) != 0)
2190 message (D_ERROR, _("Warning"),
2191 _("Cannot create %s directory"), mc_dir);
2192 g_free (mc_dir);
2194 #ifdef HAVE_SUBSHELL_SUPPORT
2195 /* Done here to ensure that the subshell doesn't */
2196 /* inherit the file descriptors opened below, etc */
2197 if (use_subshell)
2198 init_subshell ();
2200 #endif /* HAVE_SUBSHELL_SUPPORT */
2202 /* Removing this from the X code let's us type C-c */
2203 load_key_defs ();
2205 load_keymap_defs ();
2207 /* Also done after init_subshell, to save any shell init file messages */
2208 if (console_flag)
2209 handle_console (CONSOLE_SAVE);
2211 if (alternate_plus_minus)
2212 application_keypad_mode ();
2214 #ifdef HAVE_SUBSHELL_SUPPORT
2215 if (use_subshell) {
2216 mc_prompt = strip_ctrl_codes (subshell_prompt);
2217 if (mc_prompt == NULL)
2218 mc_prompt = "";
2219 } else
2220 #endif /* HAVE_SUBSHELL_SUPPORT */
2221 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2224 /* Program main loop */
2225 if (!midnight_shutdown)
2226 do_nc ();
2228 /* Save the tree store */
2229 tree_store_save ();
2231 free_keymap_defs ();
2233 /* Virtual File System shutdown */
2234 vfs_shut ();
2236 flush_extension_file (); /* does only free memory */
2238 mc_fhl_free (&mc_filehighlight);
2239 mc_skin_deinit ();
2241 tty_shutdown ();
2243 if (console_flag && !(quit & SUBSHELL_EXIT))
2244 handle_console (CONSOLE_RESTORE);
2245 if (alternate_plus_minus)
2246 numeric_keypad_mode ();
2248 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2250 if (console_flag)
2251 handle_console (CONSOLE_DONE);
2252 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2254 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2255 && !edit_one_file && !view_one_file) {
2256 int last_wd_fd =
2257 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2258 S_IRUSR | S_IWUSR);
2260 if (last_wd_fd != -1) {
2261 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2262 close (last_wd_fd);
2265 g_free (last_wd_string);
2267 g_free (mc_home_alt);
2268 g_free (mc_home);
2269 g_free (shell);
2271 done_key ();
2272 #ifdef HAVE_CHARSET
2273 free_codepages_list ();
2274 #endif
2275 str_uninit_strings ();
2277 g_free (this_dir);
2278 g_free (other_dir);
2280 #ifdef USE_INTERNAL_EDIT
2281 edit_stack_free ();
2282 #endif
2284 return 0;