Ticket #1991: toggle panels view
[midnight-commander.git] / src / main.c
blob43aa28a869eff922298ca483645cec8c06a6b646
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 when using F3 to view a directory and there
242 are tagged files */
243 int confirm_view_dir = 0;
245 /* This flag indicates if the pull down menus by default drop down */
246 int drop_menus = 0;
248 /* The dialog handle for the main program */
249 Dlg_head *midnight_dlg = NULL;
251 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
252 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
253 int update_prompt = 0;
255 /* The home directory */
256 const char *home_dir = NULL;
258 /* Tab size */
259 int option_tab_spacing = 8;
261 /* The value of the other directory, only used when loading the setup */
262 char *other_dir = NULL;
264 /* Only used at program boot */
265 int boot_current_is_left = 1;
267 static char *this_dir = NULL;
269 /* If this is true, then when browsing the tree the other window will
270 * automatically reload it's directory with the contents of the currently
271 * selected directory.
273 int xtree_mode = 0;
275 /* If set, then print to the given file the last directory we were at */
276 static char *last_wd_string = NULL;
277 /* Set to 1 to suppress printing the last directory */
278 static int print_last_revert = 0;
280 /* File name to view if argument was supplied */
281 const char *view_one_file = NULL;
283 /* File name to edit if argument was supplied */
284 const char *edit_one_file = NULL;
286 /* Line to start the editor on */
287 static int edit_one_file_start_line = 0;
289 /* Used so that widgets know if they are being destroyed or
290 shut down */
291 int midnight_shutdown = 0;
293 /* The user's shell */
294 char *shell = NULL;
296 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
297 char *mc_home = NULL;
299 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
300 char *mc_home_alt = NULL;
302 /* Define this function for glib-style error handling */
303 GQuark
304 mc_main_error_quark (void)
306 return g_quark_from_static_string (PACKAGE);
309 #ifdef USE_INTERNAL_EDIT
310 GArray *editor_keymap = NULL;
311 GArray *editor_x_keymap = NULL;
312 #endif
313 GArray *viewer_keymap = NULL;
314 GArray *viewer_hex_keymap = NULL;
315 GArray *main_keymap = NULL;
316 GArray *main_x_keymap = NULL;
317 GArray *panel_keymap = NULL;
318 GArray *input_keymap = NULL;
319 GArray *tree_keymap = NULL;
320 GArray *help_keymap = NULL;
322 const global_keymap_t *main_map;
323 const global_keymap_t *main_x_map;
325 /* Save current stat of directories to avoid reloading the panels */
326 /* when no modifications have taken place */
327 void
328 save_cwds_stat (void)
330 if (fast_reload) {
331 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
332 if (get_other_type () == view_listing)
333 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
337 #ifdef HAVE_SUBSHELL_SUPPORT
338 void
339 do_update_prompt (void)
341 if (update_prompt) {
342 printf ("\r\n%s", subshell_prompt);
343 fflush (stdout);
344 update_prompt = 0;
347 #endif /* HAVE_SUBSHELL_SUPPORT */
349 void
350 change_panel (void)
352 free_completions (cmdline);
353 dlg_one_down (midnight_dlg);
356 /* Stop MC main dialog and the current dialog if it exists.
357 * Needed to provide fast exit from MC viewer or editor on shell exit */
358 static void
359 stop_dialogs (void)
361 midnight_dlg->running = 0;
362 if (current_dlg) {
363 current_dlg->running = 0;
367 static int
368 quit_cmd_internal (int quiet)
370 int q = quit;
372 if (quiet || !confirm_exit) {
373 q = 1;
374 } else {
375 if (query_dialog
376 (_(" The Midnight Commander "),
377 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
378 2, _("&Yes"), _("&No")) == 0)
379 q = 1;
381 if (q) {
382 #ifdef HAVE_SUBSHELL_SUPPORT
383 if (!use_subshell)
384 stop_dialogs ();
385 else if ((q = exit_subshell ()))
386 #endif
387 stop_dialogs ();
389 if (q)
390 quit |= 1;
391 return quit;
394 static void
395 quit_cmd (void)
397 quit_cmd_internal (0);
400 void
401 quiet_quit_cmd (void)
403 print_last_revert = 1;
404 quit_cmd_internal (1);
407 /* Wrapper for do_subshell_chdir, check for availability of subshell */
408 void
409 subshell_chdir (const char *directory)
411 #ifdef HAVE_SUBSHELL_SUPPORT
412 if (use_subshell) {
413 if (vfs_current_is_local ())
414 do_subshell_chdir (directory, 0, 1);
416 #endif /* HAVE_SUBSHELL_SUPPORT */
419 void
420 directory_history_add (struct WPanel *panel, const char *dir)
422 char *tmp;
424 tmp = g_strdup (dir);
425 strip_password (tmp, 1);
427 panel->dir_history = list_append_unique (panel->dir_history, tmp);
431 * If we moved to the parent directory move the selection pointer to
432 * the old directory name; If we leave VFS dir, remove FS specificator.
434 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
436 static const char *
437 get_parent_dir_name (const char *cwd, const char *lwd)
439 const char *p;
440 if (strlen (lwd) > strlen (cwd))
441 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
442 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
443 cwd[1] == '\0'))) {
444 return (p + 1);
446 return NULL;
450 * Changes the current directory of the panel.
451 * Don't record change in the directory history.
453 static int
454 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
456 const char *directory;
457 char *olddir;
458 char temp[MC_MAXPATHLEN];
459 char *translated_url;
461 if (cd_type == cd_parse_command) {
462 while (*new_dir == ' ')
463 new_dir++;
466 olddir = g_strdup (panel->cwd);
467 new_dir = translated_url = vfs_translate_url (new_dir);
469 /* Convert *new_path to a suitable pathname, handle ~user */
471 if (cd_type == cd_parse_command) {
472 if (!strcmp (new_dir, "-")) {
473 strcpy (temp, panel->lwd);
474 new_dir = temp;
477 directory = *new_dir ? new_dir : home_dir;
479 if (mc_chdir (directory) == -1) {
480 strcpy (panel->cwd, olddir);
481 g_free (olddir);
482 g_free (translated_url);
483 return 0;
485 g_free (translated_url);
487 /* Success: save previous directory, shutdown status of previous dir */
488 strcpy (panel->lwd, olddir);
489 free_completions (cmdline);
491 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
493 vfs_release_path (olddir);
495 subshell_chdir (panel->cwd);
497 /* Reload current panel */
498 panel_clean_dir (panel);
499 panel->count =
500 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
501 panel->reverse, panel->case_sensitive,
502 panel->exec_first, panel->filter);
503 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
504 load_hint (0);
505 panel->dirty = 1;
506 update_xterm_title_path ();
508 g_free (olddir);
510 return 1;
514 * Changes the current directory of the panel.
515 * Record change in the directory history.
518 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
520 int r;
522 r = _do_panel_cd (panel, new_dir, cd_type);
523 if (r)
524 directory_history_add (panel, panel->cwd);
525 return r;
529 do_cd (const char *new_dir, enum cd_enum exact)
531 return (do_panel_cd (current_panel, new_dir, exact));
534 void
535 directory_history_next (WPanel *panel)
537 GList *nextdir;
539 nextdir = g_list_next (panel->dir_history);
541 if (!nextdir)
542 return;
544 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
545 panel->dir_history = nextdir;
548 void
549 directory_history_prev (WPanel *panel)
551 GList *prevdir;
553 prevdir = g_list_previous (panel->dir_history);
555 if (!prevdir)
556 return;
558 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
559 panel->dir_history = prevdir;
562 void
563 directory_history_list (WPanel *panel)
565 char *s;
567 if (!panel->dir_history)
568 return;
570 s = show_hist (panel->dir_history, &panel->widget);
572 if (!s)
573 return;
575 if (_do_panel_cd (panel, s, cd_exact))
576 directory_history_add (panel, panel->cwd);
577 else
578 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
579 g_free (s);
582 #ifdef HAVE_SUBSHELL_SUPPORT
584 load_prompt (int fd, void *unused)
586 (void) fd;
587 (void) unused;
589 if (!read_subshell_prompt ())
590 return 0;
592 /* Don't actually change the prompt if it's invisible */
593 if (current_dlg == midnight_dlg && command_prompt) {
594 char *tmp_prompt;
595 int prompt_len;
597 tmp_prompt = strip_ctrl_codes (subshell_prompt);
598 prompt_len = str_term_width1 (tmp_prompt);
600 /* Check for prompts too big */
601 if (COLS > 8 && prompt_len > COLS - 8) {
602 tmp_prompt[COLS - 8] = '\0';
603 prompt_len = COLS - 8;
605 mc_prompt = tmp_prompt;
606 label_set_text (the_prompt, mc_prompt);
607 winput_set_origin ((WInput *) cmdline, prompt_len,
608 COLS - prompt_len);
610 /* since the prompt has changed, and we are called from one of the
611 * tty_get_event channels, the prompt updating does not take place
612 * automatically: force a cursor update and a screen refresh
614 update_cursor (midnight_dlg);
615 mc_refresh ();
617 update_prompt = 1;
618 return 0;
620 #endif /* HAVE_SUBSHELL_SUPPORT */
622 void
623 sort_cmd (void)
625 WPanel *p;
626 const panel_field_t *sort_order;
628 if (!SELECTED_IS_PANEL)
629 return;
631 p = MENU_PANEL;
632 sort_order = sort_box (p->current_sort_field, &p->reverse,
633 &p->case_sensitive,
634 &p->exec_first);
636 panel_set_sort_order (p, sort_order);
640 static void
641 treebox_cmd (void)
643 char *sel_dir;
645 sel_dir = tree_box (selection (current_panel)->fname);
646 if (sel_dir) {
647 do_cd (sel_dir, cd_exact);
648 g_free (sel_dir);
652 #ifdef LISTMODE_EDITOR
653 static void
654 listmode_cmd (void)
656 char *newmode;
658 if (get_current_type () != view_listing)
659 return;
661 newmode = listmode_edit (current_panel->user_format);
662 if (!newmode)
663 return;
665 g_free (current_panel->user_format);
666 current_panel->list_type = list_user;
667 current_panel->user_format = newmode;
668 set_panel_formats (current_panel);
670 do_refresh ();
672 #endif /* LISTMODE_EDITOR */
674 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
675 static GList *
676 create_panel_menu (void)
678 GList *entries = NULL;
680 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
681 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
682 entries = g_list_append (entries, menu_entry_create (_("&Info" ), CK_InfoCmd));
683 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
684 entries = g_list_append (entries, menu_separator_create ());
685 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
686 entries = g_list_append (entries, menu_separator_create ());
687 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
688 #ifdef HAVE_CHARSET
689 entries = g_list_append (entries, menu_separator_create ());
690 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
691 #endif
692 #ifdef USE_NETCODE
693 entries = g_list_append (entries, menu_separator_create ());
694 #ifdef ENABLE_VFS_MCFS
695 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
696 #endif
697 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
698 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
699 #ifdef ENABLE_VFS_SMB
700 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
701 #endif /* ENABLE_VFS_SMB */
702 #endif
703 entries = g_list_append (entries, menu_separator_create ());
704 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
706 return entries;
709 static GList *
710 create_file_menu (void)
712 GList *entries = NULL;
714 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
715 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
716 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
717 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
718 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
719 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
720 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
721 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
722 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
723 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
724 entries = g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
725 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
726 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
727 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
728 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
729 entries = g_list_append (entries, menu_separator_create ());
730 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
731 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
732 entries = g_list_append (entries, menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
733 entries = g_list_append (entries, menu_separator_create ());
734 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
736 return entries;
739 static GList *
740 create_command_menu (void)
742 /* I know, I'm lazy, but the tree widget when it's not running
743 * as a panel still has some problems, I have not yet finished
744 * the WTree widget port, sorry.
746 GList *entries = NULL;
748 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
749 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
750 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
751 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
752 entries = g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
753 entries = g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
754 entries = g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
755 entries = g_list_append (entries, menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
756 entries = g_list_append (entries, menu_separator_create ());
757 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
758 entries = g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
759 #ifdef ENABLE_VFS
760 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
761 #endif
762 #ifdef WITH_BACKGROUND
763 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
764 #endif
765 entries = g_list_append (entries, menu_separator_create ());
766 #ifdef USE_EXT2FSLIB
767 entries = g_list_append (entries, menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
768 #endif
769 #ifdef LISTMODE_EDITOR
770 entries = g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
771 #endif
772 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
773 entries = g_list_append (entries, menu_separator_create ());
774 #endif
775 entries = g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
776 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
777 entries = g_list_append (entries, menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
779 return entries;
782 static GList *
783 create_options_menu (void)
785 GList *entries = NULL;
787 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
788 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
789 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
790 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
791 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
792 #ifdef ENABLE_VFS
793 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
794 #endif
795 entries = g_list_append (entries, menu_separator_create ());
796 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
798 return entries;
801 void
802 init_menu (void)
804 menubar_add_menu (the_menubar,
805 create_menu (horizontal_split ? _("&Above") : _("&Left"),
806 create_panel_menu (), "[Left and Right Menus]"));
807 menubar_add_menu (the_menubar,
808 create_menu (_("&File"), create_file_menu (), "[File Menu]"));
809 menubar_add_menu (the_menubar,
810 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
811 menubar_add_menu (the_menubar,
812 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
813 menubar_add_menu (the_menubar,
814 create_menu (horizontal_split ? _("&Below") : _("&Right"),
815 create_panel_menu (), "[Left and Right Menus]"));
818 void
819 done_menu (void)
821 menubar_set_menu (the_menubar, NULL);
824 static void
825 menu_last_selected_cmd (void)
827 the_menubar->is_active = TRUE;
828 the_menubar->is_dropped = (drop_menus != 0);
829 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
830 dlg_select_widget (the_menubar);
833 static void
834 menu_cmd (void)
836 if (the_menubar->is_active)
837 return;
839 if ((get_current_index () == 0) == (current_panel->active != 0))
840 the_menubar->selected = 0;
841 else
842 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
843 menu_last_selected_cmd ();
846 static char *
847 midnight_get_shortcut (unsigned long command)
849 const char *ext_map;
850 const char *shortcut = NULL;
852 shortcut = lookup_keymap_shortcut (main_map, command);
853 if (shortcut != NULL)
854 return g_strdup (shortcut);
856 shortcut = lookup_keymap_shortcut (panel_map, command);
857 if (shortcut != NULL)
858 return g_strdup (shortcut);
860 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
861 if (ext_map != NULL)
862 shortcut = lookup_keymap_shortcut (main_x_map, command);
863 if (shortcut != NULL)
864 return g_strdup_printf ("%s %s", ext_map, shortcut);
866 return NULL;
869 /* Flag toggling functions */
870 void
871 toggle_fast_reload (void)
873 fast_reload = !fast_reload;
874 if (fast_reload_w == 0 && fast_reload) {
875 message (D_NORMAL, _(" Information "),
877 (" Using the fast reload option may not reflect the exact \n"
878 " directory contents. In this case you'll need to do a \n"
879 " manual reload of the directory. See the man page for \n"
880 " the details. "));
881 fast_reload_w = 1;
885 void
886 toggle_mix_all_files (void)
888 mix_all_files = !mix_all_files;
889 update_panels (UP_RELOAD, UP_KEEPSEL);
892 void
893 toggle_show_backup (void)
895 show_backups = !show_backups;
896 update_panels (UP_RELOAD, UP_KEEPSEL);
899 void
900 toggle_show_hidden (void)
902 show_dot_files = !show_dot_files;
903 update_panels (UP_RELOAD, UP_KEEPSEL);
906 static void
907 toggle_panels_split (void)
909 horizontal_split = !horizontal_split;
910 layout_change ();
911 do_refresh();
914 void
915 toggle_kilobyte_si (void)
917 kilobyte_si = !kilobyte_si;
918 update_panels (UP_RELOAD, UP_KEEPSEL);
922 * Just a hack for allowing url-like pathnames to be accepted from the
923 * command line.
925 static void
926 translated_mc_chdir (char *dir)
928 char *newdir;
930 newdir = vfs_translate_url (dir);
931 mc_chdir (newdir);
932 g_free (newdir);
935 static void
936 create_panels (void)
938 int current_index;
939 int other_index;
940 panel_view_mode_t current_mode, other_mode;
941 char original_dir[1024];
943 original_dir[0] = 0;
945 if (boot_current_is_left) {
946 current_index = 0;
947 other_index = 1;
948 current_mode = startup_left_mode;
949 other_mode = startup_right_mode;
950 } else {
951 current_index = 1;
952 other_index = 0;
953 current_mode = startup_right_mode;
954 other_mode = startup_left_mode;
956 /* Creates the left panel */
957 if (this_dir) {
958 if (other_dir) {
959 /* Ok, user has specified two dirs, save the original one,
960 * since we may not be able to chdir to the proper
961 * second directory later
963 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
965 translated_mc_chdir (this_dir);
967 set_display_type (current_index, current_mode);
969 /* The other panel */
970 if (other_dir) {
971 if (original_dir[0])
972 translated_mc_chdir (original_dir);
973 translated_mc_chdir (other_dir);
975 set_display_type (other_index, other_mode);
977 if (startup_left_mode == view_listing) {
978 current_panel = left_panel;
979 } else {
980 if (right_panel)
981 current_panel = right_panel;
982 else
983 current_panel = left_panel;
986 /* Create the nice widgets */
987 cmdline = command_new (0, 0, 0);
988 the_prompt = label_new (0, 0, mc_prompt);
989 the_prompt->transparent = 1;
990 the_bar = buttonbar_new (keybar_visible);
992 the_hint = label_new (0, 0, 0);
993 the_hint->transparent = 1;
994 the_hint->auto_adjust_cols = 0;
995 the_hint->widget.cols = COLS;
997 the_menubar = menubar_new (0, 0, COLS, NULL);
1000 static void
1001 copy_current_pathname (void)
1003 char *cwd_path;
1004 if (!command_prompt)
1005 return;
1007 cwd_path = remove_encoding_from_path (current_panel->cwd);
1008 command_insert (cmdline, cwd_path, 0);
1010 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1011 command_insert (cmdline, PATH_SEP_STR, 0);
1012 g_free (cwd_path);
1015 static void
1016 copy_other_pathname (void)
1018 char *cwd_path;
1020 if (get_other_type () != view_listing)
1021 return;
1023 if (!command_prompt)
1024 return;
1026 cwd_path = remove_encoding_from_path (other_panel->cwd);
1027 command_insert (cmdline, cwd_path, 0);
1029 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1030 command_insert (cmdline, PATH_SEP_STR, 0);
1031 g_free (cwd_path);
1034 static void
1035 copy_readlink (WPanel *panel)
1037 if (!command_prompt)
1038 return;
1039 if (S_ISLNK (selection (panel)->st.st_mode)) {
1040 char buffer[MC_MAXPATHLEN];
1041 char *p =
1042 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1043 int i;
1045 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1046 g_free (p);
1047 if (i > 0) {
1048 buffer[i] = 0;
1049 command_insert (cmdline, buffer, 1);
1054 static void
1055 copy_current_readlink (void)
1057 copy_readlink (current_panel);
1060 static void
1061 copy_other_readlink (void)
1063 if (get_other_type () == view_listing)
1064 copy_readlink (other_panel);
1067 /* Insert the selected file name into the input line */
1068 static void
1069 copy_prog_name (void)
1071 char *tmp;
1072 if (!command_prompt)
1073 return;
1075 if (get_current_type () == view_tree) {
1076 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1077 tmp = tree_selected_name (tree);
1078 } else
1079 tmp = selection (current_panel)->fname;
1081 command_insert (cmdline, tmp, 1);
1084 static void
1085 copy_tagged (WPanel *panel)
1087 int i;
1089 if (!command_prompt)
1090 return;
1091 input_disable_update (cmdline);
1092 if (panel->marked) {
1093 for (i = 0; i < panel->count; i++) {
1094 if (panel->dir.list[i].f.marked)
1095 command_insert (cmdline, panel->dir.list[i].fname, 1);
1097 } else {
1098 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1101 input_enable_update (cmdline);
1104 static void
1105 copy_current_tagged (void)
1107 copy_tagged (current_panel);
1110 static void
1111 copy_other_tagged (void)
1113 if (get_other_type () == view_listing)
1114 copy_tagged (other_panel);
1117 void
1118 midnight_set_buttonbar (WButtonBar *b)
1120 buttonbar_set_label (b, 1, Q_("ButtonBar|Help"), main_map, NULL);
1121 buttonbar_set_label (b, 2, Q_("ButtonBar|Menu"), main_map, NULL);
1122 buttonbar_set_label (b, 3, Q_("ButtonBar|View"), main_map, NULL);
1123 buttonbar_set_label (b, 4, Q_("ButtonBar|Edit"), main_map, NULL);
1124 buttonbar_set_label (b, 5, Q_("ButtonBar|Copy"), main_map, NULL);
1125 buttonbar_set_label (b, 6, Q_("ButtonBar|RenMov"), main_map, NULL);
1126 buttonbar_set_label (b, 7, Q_("ButtonBar|Mkdir"), main_map, NULL);
1127 buttonbar_set_label (b, 8, Q_("ButtonBar|Delete"), main_map, NULL);
1128 buttonbar_set_label (b, 9, Q_("ButtonBar|PullDn"), main_map, NULL);
1129 buttonbar_set_label (b, 10, Q_("ButtonBar|Quit"), main_map, NULL);
1132 static gboolean ctl_x_map_enabled = FALSE;
1134 static void
1135 ctl_x_cmd (void)
1137 ctl_x_map_enabled = TRUE;
1140 static cb_ret_t
1141 midnight_execute_cmd (Widget *sender, unsigned long command)
1143 cb_ret_t res = MSG_HANDLED;
1145 (void) sender;
1147 switch (command) {
1148 case CK_AddHotlist:
1149 add2hotlist_cmd ();
1150 break;
1151 case CK_ChmodCmd:
1152 chmod_cmd ();
1153 break;
1154 case CK_ChownCmd:
1155 chown_cmd ();
1156 break;
1157 case CK_ChownAdvancedCmd:
1158 chown_advanced_cmd ();
1159 break;
1160 case CK_CompareDirsCmd:
1161 compare_dirs_cmd ();
1162 break;
1163 case CK_ConfigureBox:
1164 configure_box ();
1165 break;
1166 #ifdef ENABLE_VFS
1167 case CK_ConfigureVfs:
1168 configure_vfs ();
1169 break;
1170 #endif
1171 case CK_ConfirmBox:
1172 confirm_box ();
1173 break;
1174 case CK_CopyCmd:
1175 copy_cmd ();
1176 break;
1177 case CK_CopyCurrentPathname:
1178 copy_current_pathname ();
1179 break;
1180 case CK_CopyCurrentReadlink:
1181 copy_current_readlink ();
1182 break;
1183 case CK_CopyCurrentTagged:
1184 copy_current_tagged ();
1185 break;
1186 case CK_CopyOtherPathname:
1187 copy_other_pathname ();
1188 break;
1189 case CK_CopyOtherReadlink:
1190 copy_other_readlink ();
1191 break;
1192 case CK_CopyOtherTagged:
1193 copy_other_tagged ();
1194 break;
1195 case CK_DeleteCmd:
1196 delete_cmd ();
1197 break;
1198 case CK_DisplayBitsBox:
1199 display_bits_box ();
1200 break;
1201 case CK_EditCmd:
1202 edit_cmd ();
1203 break;
1204 case CK_EditExtFileCmd:
1205 ext_cmd ();
1206 break;
1207 case CK_EditFhlFileCmd:
1208 edit_fhl_cmd ();
1209 break;
1210 case CK_EditMcMenuCmd:
1211 edit_mc_menu_cmd ();
1212 break;
1213 case CK_EditSymlinkCmd:
1214 edit_symlink_cmd ();
1215 break;
1216 case CK_ExternalPanelize:
1217 external_panelize ();
1218 break;
1219 case CK_FilterCmd:
1220 filter_cmd ();
1221 break;
1222 case CK_FilteredViewCmd:
1223 filtered_view_cmd ();
1224 break;
1225 case CK_FindCmd:
1226 find_cmd ();
1227 break;
1228 #if defined (USE_NETCODE)
1229 case CK_FishlinkCmd:
1230 fishlink_cmd ();
1231 break;
1232 case CK_FtplinkCmd:
1233 ftplink_cmd ();
1234 break;
1235 #endif
1236 case CK_HelpCmd:
1237 help_cmd ();
1238 break;
1239 case CK_HistoryCmd:
1240 history_cmd ();
1241 break;
1242 case CK_InfoCmd:
1243 if (sender == (Widget *) the_menubar)
1244 info_cmd (); /* mwnu */
1245 else
1246 info_cmd_no_menu (); /* shortcut or buttonbar */
1247 break;
1248 #ifdef WITH_BACKGROUND
1249 case CK_JobsCmd:
1250 jobs_cmd ();
1251 break;
1252 #endif
1253 case CK_LayoutCmd:
1254 layout_cmd ();
1255 break;
1256 case CK_LearnKeys:
1257 learn_keys ();
1258 break;
1259 case CK_LinkCmd:
1260 link_cmd ();
1261 break;
1262 case CK_ListingCmd:
1263 listing_cmd ();
1264 break;
1265 #ifdef LISTMODE_EDITOR
1266 case CK_ListmodeCmd:
1267 listmode_cmd ();
1268 break;
1269 #endif
1270 case CK_MenuCmd:
1271 menu_cmd ();
1272 break;
1273 case CK_MenuLastSelectedCmd:
1274 menu_last_selected_cmd ();
1275 break;
1276 case CK_MkdirCmd:
1277 mkdir_cmd ();
1278 break;
1279 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1280 case CK_NetlinkCmd:
1281 netlink_cmd ();
1282 break;
1283 #endif
1284 #ifdef HAVE_CHARSET
1285 case CK_PanelSetPanelEncoding:
1286 encoding_cmd ();
1287 break;
1288 #endif
1289 case CK_QuickCdCmd:
1290 quick_cd_cmd ();
1291 break;
1292 case CK_QuickChdirCmd:
1293 quick_chdir_cmd ();
1294 break;
1295 case CK_QuickViewCmd:
1296 if (sender == (Widget *) the_menubar)
1297 quick_view_cmd (); /* menu */
1298 else
1299 quick_cmd_no_menu (); /* shortcut or buttonabr */
1300 break;
1301 case CK_QuietQuitCmd:
1302 quiet_quit_cmd ();
1303 break;
1304 case CK_QuitCmd:
1305 quit_cmd ();
1306 break;
1307 case CK_RenameCmd:
1308 rename_cmd ();
1309 break;
1310 case CK_RereadCmd:
1311 reread_cmd ();
1312 break;
1313 #ifdef ENABLE_VFS
1314 case CK_ReselectVfs:
1315 reselect_vfs ();
1316 break;
1317 #endif
1318 case CK_ReverseSelectionCmd:
1319 reverse_selection_cmd ();
1320 break;
1321 case CK_SaveSetupCmd:
1322 save_setup_cmd ();
1323 break;
1324 case CK_SelectCmd:
1325 select_cmd ();
1326 break;
1327 case CK_ShowCommandLine:
1328 view_other_cmd ();
1329 break;
1330 case CK_SingleDirsizeCmd:
1331 smart_dirsize_cmd ();
1332 break;
1333 #if defined (USE_NETCODE) && defined (ENABLE_VFS_SMB)
1334 case CK_SmblinkCmd:
1335 smblink_cmd ();
1336 break;
1337 #endif /* USE_NETCODE && ENABLE_VFS_SMB */
1338 case CK_Sort:
1339 sort_cmd ();
1340 break;
1341 case CK_StartExtMap1:
1342 ctl_x_cmd ();
1343 break;
1344 case CK_SuspendCmd:
1345 suspend_cmd ();
1346 break;
1347 case CK_SwapCmd:
1348 swap_cmd ();
1349 break;
1350 case CK_SymlinkCmd:
1351 symlink_cmd ();
1352 break;
1353 case CK_ToggleListingCmd:
1354 toggle_listing_cmd ();
1355 break;
1356 case CK_ToggleShowHidden:
1357 toggle_show_hidden ();
1358 break;
1359 case CK_TogglePanelsSplit:
1360 toggle_panels_split ();
1361 break;
1362 case CK_TreeCmd:
1363 tree_cmd ();
1364 break;
1365 case CK_TreeBoxCmd:
1366 treebox_cmd ();
1367 break;
1368 #ifdef USE_EXT2FSLIB
1369 case CK_UndeleteCmd:
1370 undelete_cmd ();
1371 break;
1372 #endif
1373 case CK_UnselectCmd:
1374 unselect_cmd ();
1375 break;
1376 case CK_UserMenuCmd:
1377 user_file_menu_cmd ();
1378 break;
1379 case CK_ViewCmd:
1380 view_cmd ();
1381 break;
1382 case CK_ViewFileCmd:
1383 view_file_cmd ();
1384 break;
1385 default:
1386 res = MSG_NOT_HANDLED;
1389 return res;
1392 static void
1393 setup_pre (void)
1395 /* Call all the inits */
1397 #ifdef HAVE_SLANG
1398 tty_display_8bit (full_eight_bits != 0);
1399 #else
1400 tty_display_8bit (eight_bit_clean != 0);
1401 #endif
1404 static void
1405 init_xterm_support (void)
1407 const char *termvalue;
1409 termvalue = getenv ("TERM");
1410 if (!termvalue || !(*termvalue)) {
1411 fputs (_("The TERM environment variable is unset!\n"), stderr);
1412 exit (1);
1415 /* Check mouse capabilities */
1416 xmouse_seq = tty_tgetstr ("Km");
1418 if (strcmp (termvalue, "cygwin") == 0) {
1419 mc_args__force_xterm = 1;
1420 use_mouse_p = MOUSE_DISABLED;
1423 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1424 || strncmp (termvalue, "konsole", 7) == 0
1425 || strncmp (termvalue, "rxvt", 4) == 0
1426 || strcmp (termvalue, "Eterm") == 0
1427 || strcmp (termvalue, "dtterm") == 0) {
1428 xterm_flag = 1;
1430 /* Default to the standard xterm sequence */
1431 if (!xmouse_seq) {
1432 xmouse_seq = ESC_STR "[M";
1435 /* Enable mouse unless explicitly disabled by --nomouse */
1436 if (use_mouse_p != MOUSE_DISABLED) {
1437 const char *color_term = getenv ("COLORTERM");
1438 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1439 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1440 strcmp (termvalue, "Eterm") == 0) {
1441 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1442 } else {
1443 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1449 static void
1450 setup_mc (void)
1452 setup_pre ();
1453 create_panels ();
1454 setup_panels ();
1456 #ifdef HAVE_SUBSHELL_SUPPORT
1457 if (use_subshell)
1458 add_select_channel (subshell_pty, load_prompt, 0);
1459 #endif /* !HAVE_SUBSHELL_SUPPORT */
1461 tty_setup_sigwinch (sigwinch_handler);
1463 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1465 init_xterm_support ();
1466 init_mouse ();
1469 static void
1470 setup_dummy_mc (void)
1472 char d[MC_MAXPATHLEN];
1474 mc_get_current_wd (d, MC_MAXPATHLEN);
1475 setup_mc ();
1476 mc_chdir (d);
1479 static void check_codeset()
1481 const char *current_system_codepage = NULL;
1483 current_system_codepage = str_detect_termencoding();
1485 #ifdef HAVE_CHARSET
1487 const char *_display_codepage;
1489 _display_codepage = get_codepage_id (display_codepage);
1491 if (! strcmp(_display_codepage, current_system_codepage)) {
1492 utf8_display = str_isutf8 (current_system_codepage);
1493 return;
1496 display_codepage = get_codepage_index (current_system_codepage);
1497 if (display_codepage == -1) {
1498 display_codepage = 0;
1501 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1503 #endif
1504 utf8_display = str_isutf8 (current_system_codepage);
1507 static void
1508 done_screen (void)
1510 if (!(quit & SUBSHELL_EXIT))
1511 clr_scr ();
1512 tty_reset_shell_mode ();
1513 tty_noraw_mode ();
1514 tty_keypad (FALSE);
1515 tty_colors_done ();
1518 static void
1519 done_mc (void)
1521 disable_mouse ();
1523 /* Setup shutdown
1525 * We sync the profiles since the hotlist may have changed, while
1526 * we only change the setup data if we have the auto save feature set
1529 if (auto_save_setup)
1530 save_setup (); /* does also call save_hotlist */
1531 else {
1532 save_hotlist ();
1533 save_panel_types ();
1535 done_screen ();
1536 vfs_add_current_stamps ();
1539 /* This should be called after destroy_dlg since panel widgets
1540 * save their state on the profiles
1542 static void
1543 done_mc_profile (void)
1545 done_setup ();
1548 static cb_ret_t
1549 midnight_callback (Dlg_head *h, Widget *sender,
1550 dlg_msg_t msg, int parm, void *data)
1552 unsigned long command;
1554 switch (msg) {
1556 case DLG_DRAW:
1557 load_hint (1);
1558 /* We handle the special case of the output lines */
1559 if (console_flag && output_lines)
1560 show_console_contents (output_start_y,
1561 LINES - output_lines - keybar_visible -
1562 1, LINES - keybar_visible - 1);
1563 return MSG_HANDLED;
1565 case DLG_RESIZE:
1566 setup_panels ();
1567 menubar_arrange (the_menubar);
1568 return MSG_HANDLED;
1570 case DLG_IDLE:
1571 /* We only need the first idle event to show user menu after start */
1572 set_idle_proc (h, 0);
1573 if (auto_menu)
1574 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1575 return MSG_HANDLED;
1577 case DLG_KEY:
1578 if (ctl_x_map_enabled) {
1579 ctl_x_map_enabled = FALSE;
1580 command = lookup_keymap_command (main_x_map, parm);
1581 if (command != CK_Ignore_Key)
1582 return midnight_execute_cmd (NULL, command);
1585 /* FIXME: should handle all menu shortcuts before this point */
1586 if (the_menubar->is_active)
1587 return MSG_NOT_HANDLED;
1589 if (parm == '\t')
1590 free_completions (cmdline);
1592 if (parm == '\n') {
1593 size_t i;
1595 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1596 cmdline->buffer[i] == '\t'); i++)
1598 if (cmdline->buffer[i]) {
1599 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1600 return MSG_HANDLED;
1602 stuff (cmdline, "", 0);
1603 cmdline->point = 0;
1606 /* Ctrl-Enter and Alt-Enter */
1607 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1608 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1609 copy_prog_name ();
1610 return MSG_HANDLED;
1613 /* Ctrl-Shift-Enter */
1614 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1615 copy_current_pathname ();
1616 copy_prog_name ();
1617 return MSG_HANDLED;
1620 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1621 && !quote && !current_panel->searching) {
1622 if (!only_leading_plus_minus) {
1623 /* Special treatement, since the input line will eat them */
1624 if (parm == '+') {
1625 select_cmd ();
1626 return MSG_HANDLED;
1629 if (parm == '\\' || parm == '-') {
1630 unselect_cmd ();
1631 return MSG_HANDLED;
1634 if (parm == '*') {
1635 reverse_selection_cmd ();
1636 return MSG_HANDLED;
1638 } else if (!command_prompt || !cmdline->buffer[0]) {
1639 /* Special treatement '+', '-', '\', '*' only when this is
1640 * first char on input line
1643 if (parm == '+') {
1644 select_cmd ();
1645 return MSG_HANDLED;
1648 if (parm == '\\' || parm == '-') {
1649 unselect_cmd ();
1650 return MSG_HANDLED;
1653 if (parm == '*') {
1654 reverse_selection_cmd ();
1655 return MSG_HANDLED;
1659 return MSG_NOT_HANDLED;
1661 case DLG_HOTKEY_HANDLED:
1662 if ((get_current_type () == view_listing) && current_panel->searching) {
1663 current_panel->searching = 0;
1664 current_panel->dirty = 1;
1666 return MSG_HANDLED;
1668 case DLG_UNHANDLED_KEY:
1669 if (command_prompt) {
1670 cb_ret_t v;
1672 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1673 if (v == MSG_HANDLED)
1674 return MSG_HANDLED;
1677 if (ctl_x_map_enabled) {
1678 ctl_x_map_enabled = FALSE;
1679 command = lookup_keymap_command (main_x_map, parm);
1680 } else
1681 command = lookup_keymap_command (main_map, parm);
1683 return (command == CK_Ignore_Key)
1684 ? MSG_NOT_HANDLED
1685 : midnight_execute_cmd (NULL, command);
1687 case DLG_POST_KEY:
1688 if (!the_menubar->is_active)
1689 update_dirty_panels ();
1690 return MSG_HANDLED;
1692 case DLG_ACTION:
1693 /* shortcut */
1694 if (sender == NULL)
1695 midnight_execute_cmd (NULL, parm);
1696 /* message from menu */
1697 else if (sender == (Widget *) the_menubar)
1698 midnight_execute_cmd (sender, parm);
1699 /* message from buttonbar */
1700 else if (sender == (Widget *) the_bar) {
1701 if (data == NULL)
1702 midnight_execute_cmd (sender, parm);
1703 else
1704 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1706 return MSG_HANDLED;
1708 default:
1709 return default_dlg_callback (h, sender, msg, parm, data);
1713 /* Show current directory in the xterm title */
1714 void
1715 update_xterm_title_path (void)
1717 const char *path;
1718 char host[BUF_TINY];
1719 char *p;
1720 struct passwd *pw = NULL;
1721 char *login = NULL;
1722 int res = 0;
1723 if (xterm_flag && xterm_title) {
1724 path = strip_home_and_password (current_panel->cwd);
1725 res = gethostname(host, sizeof (host));
1726 if ( res ) { /* On success, res = 0 */
1727 host[0] = '\0';
1728 } else {
1729 host[sizeof (host) - 1] = '\0';
1731 pw = getpwuid(getuid());
1732 if ( pw ) {
1733 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1734 } else {
1735 login = g_strdup (host);
1737 p = g_strdup_printf ("mc [%s]:%s", login, path);
1738 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1739 g_free (login);
1740 g_free (p);
1741 if (!alternate_plus_minus)
1742 numeric_keypad_mode ();
1743 fflush (stdout);
1748 * Load new hint and display it.
1749 * IF force is not 0, ignore the timeout.
1751 void
1752 load_hint (int force)
1754 char *hint;
1756 if (!the_hint->widget.parent)
1757 return;
1759 if (!message_visible) {
1760 label_set_text (the_hint, NULL);
1761 return;
1764 hint = get_random_hint (force);
1766 if (hint != NULL) {
1767 if (*hint)
1768 set_hintbar (hint);
1769 g_free (hint);
1770 } else {
1771 char text[BUF_SMALL];
1773 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1774 VERSION);
1775 set_hintbar (text);
1779 static void
1780 setup_panels_and_run_mc (void)
1782 midnight_dlg->get_shortcut = midnight_get_shortcut;
1784 add_widget (midnight_dlg, the_menubar);
1785 init_menu ();
1787 add_widget (midnight_dlg, get_panel_widget (0));
1788 add_widget (midnight_dlg, get_panel_widget (1));
1789 add_widget (midnight_dlg, the_hint);
1790 add_widget (midnight_dlg, cmdline);
1791 add_widget (midnight_dlg, the_prompt);
1793 add_widget (midnight_dlg, the_bar);
1794 midnight_set_buttonbar (the_bar);
1796 if (boot_current_is_left)
1797 dlg_select_widget (get_panel_widget (0));
1798 else
1799 dlg_select_widget (get_panel_widget (1));
1801 /* Run the Midnight Commander if no file was specified in the command line */
1802 run_dlg (midnight_dlg);
1805 /* result must be free'd (I think this should go in util.c) */
1806 static char *
1807 prepend_cwd_on_local (const char *filename)
1809 char *d;
1810 int l;
1812 if (vfs_file_is_local (filename)) {
1813 if (*filename == PATH_SEP) /* an absolute pathname */
1814 return g_strdup (filename);
1815 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1816 mc_get_current_wd (d, MC_MAXPATHLEN);
1817 l = strlen (d);
1818 d[l++] = PATH_SEP;
1819 strcpy (d + l, filename);
1820 canonicalize_pathname (d);
1821 return d;
1822 } else
1823 return g_strdup (filename);
1826 static int
1827 mc_maybe_editor_or_viewer (void)
1829 if (!(view_one_file || edit_one_file))
1830 return 0;
1832 /* Invoke the internal view/edit routine with:
1833 * the default processing and forcing the internal viewer/editor
1835 if (view_one_file) {
1836 char *path = NULL;
1837 path = prepend_cwd_on_local (view_one_file);
1838 view_file (path, 0, 1);
1839 g_free (path);
1841 #ifdef USE_INTERNAL_EDIT
1842 else {
1843 edit_file (edit_one_file, edit_one_file_start_line);
1845 #endif /* USE_INTERNAL_EDIT */
1846 midnight_shutdown = 1;
1847 done_mc ();
1848 return 1;
1851 /* Run the main dialog that occupies the whole screen */
1852 static void
1853 do_nc (void)
1855 int midnight_colors[DLG_COLOR_NUM];
1856 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1857 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1858 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1859 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1861 panel_init ();
1863 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1864 "[main]", NULL, DLG_WANT_IDLE);
1866 if (view_one_file || edit_one_file)
1867 setup_dummy_mc ();
1868 else
1869 setup_mc ();
1871 /* start check display_codepage and source_codepage */
1872 check_codeset ();
1874 main_map = default_main_map;
1875 if (main_keymap && main_keymap->len > 0)
1876 main_map = (global_keymap_t *) main_keymap->data;
1878 main_x_map = default_main_x_map;
1879 if (main_x_keymap && main_x_keymap->len > 0)
1880 main_x_map = (global_keymap_t *) main_x_keymap->data;
1882 panel_map = default_panel_keymap;
1883 if (panel_keymap && panel_keymap->len > 0)
1884 panel_map = (global_keymap_t *) panel_keymap->data;
1886 input_map = default_input_keymap;
1887 if (input_keymap && input_keymap->len > 0)
1888 input_map = (global_keymap_t *) input_keymap->data;
1890 tree_map = default_tree_keymap;
1891 if (tree_keymap && tree_keymap->len > 0)
1892 tree_map = (global_keymap_t *) tree_keymap->data;
1894 help_map = default_help_keymap;
1895 if (help_keymap && help_keymap->len > 0)
1896 help_map = (global_keymap_t *) help_keymap->data;
1898 /* Check if we were invoked as an editor or file viewer */
1899 if (!mc_maybe_editor_or_viewer ()) {
1900 setup_panels_and_run_mc ();
1902 /* Program end */
1903 midnight_shutdown = 1;
1905 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1906 if (mc_args__last_wd_file && vfs_current_is_local ())
1907 last_wd_string = g_strdup (current_panel->cwd);
1909 done_mc ();
1912 destroy_dlg (midnight_dlg);
1913 panel_deinit ();
1914 current_panel = 0;
1915 done_mc_profile ();
1918 /* POSIX version. The only version we support. */
1919 static void
1920 OS_Setup (void)
1922 const char *shell_env = getenv ("SHELL");
1923 const char *mc_libdir;
1925 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1926 struct passwd *pwd;
1927 pwd = getpwuid (geteuid ());
1928 if (pwd != NULL)
1929 shell = g_strdup (pwd->pw_shell);
1930 } else
1931 shell = g_strdup (shell_env);
1933 if ((shell == NULL) || (shell[0] == '\0')) {
1934 g_free (shell);
1935 shell = g_strdup ("/bin/sh");
1938 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1939 /* and can be overriden by the MC_DATADIR environment variable */
1940 mc_libdir = getenv ("MC_DATADIR");
1941 if (mc_libdir != NULL) {
1942 mc_home = g_strdup (mc_libdir);
1943 mc_home_alt = g_strdup (SYSCONFDIR);
1944 } else {
1945 mc_home = g_strdup (SYSCONFDIR);
1946 mc_home_alt = g_strdup (DATADIR);
1949 /* This variable is used by the subshell */
1950 home_dir = getenv ("HOME");
1952 if (!home_dir)
1953 home_dir = mc_home;
1956 static void
1957 sigchld_handler_no_subshell (int sig)
1959 #ifdef __linux__
1960 int pid, status;
1962 if (!console_flag)
1963 return;
1965 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1966 the value of console_flag never changed, we could simply not install
1967 this handler at all if (!console_flag && !use_subshell). */
1969 /* That comment is no longer true. We need to wait() on a sigchld
1970 handler (that's at least what the tarfs code expects currently). */
1972 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1974 if (pid == cons_saver_pid) {
1976 if (WIFSTOPPED (status)) {
1977 /* Someone has stopped cons.saver - restart it */
1978 kill (pid, SIGCONT);
1979 } else {
1980 /* cons.saver has died - disable console saving */
1981 handle_console (CONSOLE_DONE);
1982 console_flag = 0;
1985 /* If we got here, some other child exited; ignore it */
1986 #endif /* __linux__ */
1988 (void) sig;
1991 static void
1992 init_sigchld (void)
1994 struct sigaction sigchld_action;
1996 sigchld_action.sa_handler =
1997 #ifdef HAVE_SUBSHELL_SUPPORT
1998 use_subshell ? sigchld_handler :
1999 #endif /* HAVE_SUBSHELL_SUPPORT */
2000 sigchld_handler_no_subshell;
2002 sigemptyset (&sigchld_action.sa_mask);
2004 #ifdef SA_RESTART
2005 sigchld_action.sa_flags = SA_RESTART;
2006 #else
2007 sigchld_action.sa_flags = 0;
2008 #endif /* !SA_RESTART */
2010 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2011 #ifdef HAVE_SUBSHELL_SUPPORT
2013 * This may happen on QNX Neutrino 6, where SA_RESTART
2014 * is defined but not implemented. Fallback to no subshell.
2016 use_subshell = 0;
2017 #endif /* HAVE_SUBSHELL_SUPPORT */
2021 static void
2022 mc_main__setup_by_args (int argc, char *argv[])
2024 const char *base;
2025 char *tmp;
2027 if (mc_args__nomouse)
2028 use_mouse_p = MOUSE_DISABLED;
2030 #ifdef USE_NETCODE
2031 if (mc_args__netfs_logfile != NULL) {
2032 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
2033 #ifdef ENABLE_VFS_SMB
2034 smbfs_set_debugf (mc_args__netfs_logfile);
2035 #endif /* ENABLE_VFS_SMB */
2038 #ifdef ENABLE_VFS_SMB
2039 if (mc_args__debug_level != 0)
2040 smbfs_set_debug (mc_args__debug_level);
2041 #endif /* ENABLE_VFS_SMB */
2042 #endif /* USE_NETCODE */
2044 base = x_basename (argv[0]);
2045 tmp = (argc > 0) ? argv[1] : NULL;
2047 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2048 edit_one_file = "";
2049 if (tmp) {
2051 * Check for filename:lineno, followed by an optional colon.
2052 * This format is used by many programs (especially compilers)
2053 * in error messages and warnings. It is supported so that
2054 * users can quickly copy and paste file locations.
2056 char *end = tmp + strlen (tmp), *p = end;
2057 if (p > tmp && p[-1] == ':')
2058 p--;
2059 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2060 p--;
2061 if (tmp < p && p < end && p[-1] == ':') {
2062 struct stat st;
2063 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2065 * Check that the file before the colon actually exists.
2066 * If it doesn't exist, revert to the old behavior.
2068 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2069 edit_one_file = fname;
2070 edit_one_file_start_line = atoi (p);
2071 } else {
2072 g_free (fname);
2073 goto try_plus_filename;
2075 } else {
2076 try_plus_filename:
2077 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2078 int start_line = atoi (tmp);
2079 if (start_line > 0) {
2080 char *file = (argc > 1) ? argv[2] : NULL;
2081 if (file) {
2082 tmp = file;
2083 edit_one_file_start_line = start_line;
2087 edit_one_file = g_strdup (tmp);
2090 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2091 if (tmp)
2092 view_one_file = g_strdup (tmp);
2093 else {
2094 fputs ("No arguments given to the viewer\n", stderr);
2095 exit (1);
2097 } else {
2098 /* sets the current dir and the other dir */
2099 if (tmp) {
2100 this_dir = g_strdup (tmp);
2101 tmp = (argc > 1) ? argv[2] : NULL;
2102 if (tmp)
2103 other_dir = g_strdup (tmp);
2109 main (int argc, char *argv[])
2111 struct stat s;
2112 char *mc_dir;
2113 GError *error = NULL;
2114 gboolean isInitialized;
2116 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2117 setlocale (LC_ALL, "");
2118 bindtextdomain ("mc", LOCALEDIR);
2119 textdomain ("mc");
2121 /* Set up temporary directory */
2122 mc_tmpdir ();
2124 OS_Setup ();
2126 str_init_strings (NULL);
2128 vfs_init ();
2130 #ifdef USE_INTERNAL_EDIT
2131 edit_stack_init ();
2132 #endif
2134 #ifdef HAVE_SLANG
2135 SLtt_Ignore_Beep = 1;
2136 #endif
2138 if ( !mc_args_handle (&argc, &argv, "mc"))
2139 return 1;
2141 mc_main__setup_by_args (argc,argv);
2143 /* NOTE: This has to be called before tty_init or whatever routine
2144 calls any define_sequence */
2145 init_key ();
2147 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2148 handle_console (CONSOLE_INIT);
2150 #ifdef HAVE_SUBSHELL_SUPPORT
2151 /* Don't use subshell when invoked as viewer or editor */
2152 if (edit_one_file || view_one_file)
2153 use_subshell = 0;
2155 if (use_subshell)
2156 subshell_get_console_attributes ();
2157 #endif /* HAVE_SUBSHELL_SUPPORT */
2159 /* Install the SIGCHLD handler; must be done before init_subshell() */
2160 init_sigchld ();
2162 /* We need this, since ncurses endwin () doesn't restore the signals */
2163 save_stop_handler ();
2165 /* Must be done before init_subshell, to set up the terminal size: */
2166 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2167 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2169 load_setup ();
2171 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2173 isInitialized = mc_skin_init(&error);
2175 mc_filehighlight = mc_fhl_new (TRUE);
2177 dlg_set_default_colors ();
2179 if ( ! isInitialized ) {
2180 message (D_ERROR, _("Warning"), "%s", error->message);
2181 g_error_free(error);
2182 error = NULL;
2185 /* create home directory */
2186 /* do it after the screen library initialization to show the error message */
2187 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2188 canonicalize_pathname (mc_dir);
2189 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2190 && mkdir (mc_dir, 0700) != 0)
2191 message (D_ERROR, _("Warning"),
2192 _("Cannot create %s directory"), mc_dir);
2193 g_free (mc_dir);
2195 #ifdef HAVE_SUBSHELL_SUPPORT
2196 /* Done here to ensure that the subshell doesn't */
2197 /* inherit the file descriptors opened below, etc */
2198 if (use_subshell)
2199 init_subshell ();
2201 #endif /* HAVE_SUBSHELL_SUPPORT */
2203 /* Removing this from the X code let's us type C-c */
2204 load_key_defs ();
2206 load_keymap_defs ();
2208 /* Also done after init_subshell, to save any shell init file messages */
2209 if (console_flag)
2210 handle_console (CONSOLE_SAVE);
2212 if (alternate_plus_minus)
2213 application_keypad_mode ();
2215 #ifdef HAVE_SUBSHELL_SUPPORT
2216 if (use_subshell) {
2217 mc_prompt = strip_ctrl_codes (subshell_prompt);
2218 if (mc_prompt == NULL)
2219 mc_prompt = "";
2220 } else
2221 #endif /* HAVE_SUBSHELL_SUPPORT */
2222 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2225 /* Program main loop */
2226 if (!midnight_shutdown)
2227 do_nc ();
2229 /* Save the tree store */
2230 tree_store_save ();
2232 free_keymap_defs ();
2234 /* Virtual File System shutdown */
2235 vfs_shut ();
2237 flush_extension_file (); /* does only free memory */
2239 mc_fhl_free (&mc_filehighlight);
2240 mc_skin_deinit ();
2242 tty_shutdown ();
2244 if (console_flag && !(quit & SUBSHELL_EXIT))
2245 handle_console (CONSOLE_RESTORE);
2246 if (alternate_plus_minus)
2247 numeric_keypad_mode ();
2249 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2251 if (console_flag)
2252 handle_console (CONSOLE_DONE);
2253 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2255 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2256 && !edit_one_file && !view_one_file) {
2257 int last_wd_fd =
2258 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2259 S_IRUSR | S_IWUSR);
2261 if (last_wd_fd != -1) {
2262 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2263 close (last_wd_fd);
2266 g_free (last_wd_string);
2268 g_free (mc_home_alt);
2269 g_free (mc_home);
2270 g_free (shell);
2272 done_key ();
2273 #ifdef HAVE_CHARSET
2274 free_codepages_list ();
2275 #endif
2276 str_uninit_strings ();
2278 g_free (this_dir);
2279 g_free (other_dir);
2281 #ifdef USE_INTERNAL_EDIT
2282 edit_stack_free ();
2283 #endif
2285 return 0;