Changes into src directory:
[midnight-commander.git] / src / main.c
blobd4b4d1c9409e54203a494d68321d0c464c1a7588
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/tty/mouse.h"
46 #include "lib/tty/key.h" /* For init_key() */
47 #include "lib/tty/win.h" /* xterm_flag */
49 #include "lib/skin.h"
51 #include "lib/mcconfig.h"
52 #include "lib/filehighlight.h"
53 #include "lib/fileloc.h" /* MC_USERCONF_DIR */
55 #include "lib/vfs/mc-vfs/vfs.h" /* vfs_translate_url() */
57 #ifdef ENABLE_VFS_SMB
58 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debug() */
59 #endif /* ENABLE_VFS_SMB */
61 #ifdef ENABLE_VFS
62 #include "lib/vfs/mc-vfs/gc.h"
63 #endif
65 #include "lib/strutil.h"
67 #include "src/args.h"
69 #include "dir.h"
70 #include "dialog.h"
71 #include "menu.h"
72 #include "panel.h"
73 #include "option.h"
74 #include "tree.h"
75 #include "treestore.h"
76 #include "consaver/cons.saver.h"
77 #include "subshell.h"
78 #include "setup.h" /* save_setup() */
79 #include "boxes.h" /* sort_box() */
80 #include "layout.h"
81 #include "cmd.h" /* Normal commands */
82 #include "hotlist.h"
83 #include "panelize.h"
84 #include "learn.h" /* learn_keys() */
85 #include "listmode.h"
86 #include "execute.h"
87 #include "ext.h" /* For flush_extension_file() */
88 #include "widget.h"
89 #include "command.h"
90 #include "wtools.h"
91 #include "cmddef.h" /* CK_ cmd name const */
92 #include "user.h" /* user_file_menu_cmd() */
94 #include "chmod.h"
95 #include "chown.h"
96 #include "achown.h"
98 #include "main.h"
100 #ifdef USE_INTERNAL_EDIT
101 #include "src/editor/edit.h"
102 #endif
104 #ifdef USE_DIFF_VIEW
105 #include "src/diffviewer/ydiff.h"
106 #endif
108 #ifdef HAVE_CHARSET
109 #include "charsets.h"
110 #endif
113 #include "keybind.h" /* type global_keymap_t */
115 /* When the modes are active, left_panel, right_panel and tree_panel */
116 /* Point to a proper data structure. You should check with the functions */
117 /* get_current_type and get_other_type the types of the panels before using */
118 /* This pointer variables */
120 /* The structures for the panels */
121 WPanel *left_panel = NULL;
122 WPanel *right_panel = NULL;
124 mc_fhl_t *mc_filehighlight;
126 /* The pointer to the tree */
127 WTree *the_tree = NULL;
129 /* The Menubar */
130 struct WMenuBar *the_menubar = NULL;
132 /* Pointers to the selected and unselected panel */
133 WPanel *current_panel = NULL;
135 /* Set if the command is being run from the "Right" menu */
136 int is_right = 0;
138 /* Set when main loop should be terminated */
139 volatile int quit = 0;
141 /* Set if you want the possible completions dialog for the first time */
142 int show_all_if_ambiguous = 0;
144 /* Set when cd symlink following is desirable (bash mode) */
145 int cd_symlinks = 1;
147 /* If set then dialogs just clean the screen when refreshing, else */
148 /* they do a complete refresh, refreshing all the parts of the program */
149 int fast_refresh = 0;
151 /* If true, marking a files moves the cursor down */
152 int mark_moves_down = 1;
154 /* If true, at startup the user-menu is invoked */
155 int auto_menu = 0;
157 /* If true, then the +, - and \ keys have their special meaning only if the
158 * command line is emtpy, otherwise they behave like regular letters
160 int only_leading_plus_minus = 1;
162 int pause_after_run = pause_on_dumb_terminals;
164 /* It true saves the setup when quitting */
165 int auto_save_setup = 1;
167 #ifdef HAVE_CHARSET
169 * Don't restrict the output on the screen manager level,
170 * the translation tables take care of it.
172 #define full_eight_bits (1)
173 #define eight_bit_clean (1)
174 #else /* HAVE_CHARSET */
175 /* If true, allow characters in the range 160-255 */
176 int eight_bit_clean = 1;
178 * If true, also allow characters in the range 128-159.
179 * This is reported to break on many terminals (xterm, qansi-m).
181 int full_eight_bits = 0;
182 #endif /* !HAVE_CHARSET */
185 * If utf-8 terminal utf8_display = 1
186 * Display bits set UTF-8
189 int utf8_display = 0;
191 /* If true use the internal viewer */
192 int use_internal_view = 1;
194 /* Have we shown the fast-reload warning in the past? */
195 int fast_reload_w = 0;
197 /* Move page/item? When clicking on the top or bottom of a panel */
198 int mouse_move_pages = 1;
200 /* If true: l&r arrows are used to chdir if the input line is empty */
201 int navigate_with_arrows = 0;
203 /* The prompt */
204 const char *mc_prompt = NULL;
206 /* The widget where we draw the prompt */
207 WLabel *the_prompt;
209 /* The hint bar */
210 WLabel *the_hint;
212 /* The button bar */
213 WButtonBar *the_bar;
215 /* Mouse type: GPM, xterm or none */
216 Mouse_Type use_mouse_p = MOUSE_NONE;
218 /* If on, default for "No" in delete operations */
219 int safe_delete = 0;
221 /* Controls screen clearing before an exec */
222 int clear_before_exec = 1;
224 /* Asks for confirmation before deleting a file */
225 int confirm_delete = 1;
227 /* Asks for confirmation before deleting a hotlist entry */
228 int confirm_directory_hotlist_delete = 1;
230 /* Asks for confirmation before overwriting a file */
231 int confirm_overwrite = 1;
233 /* Asks for confirmation before executing a program by pressing enter */
234 int confirm_execute = 0;
236 /* Asks for confirmation before leaving the program */
237 int confirm_exit = 1;
239 /* Asks for confirmation before clean up of history */
240 int confirm_history_cleanup = 1;
242 /* Asks for confirmation when using F3 to view a directory and there
243 are tagged files */
244 int confirm_view_dir = 0;
246 /* This flag indicates if the pull down menus by default drop down */
247 int drop_menus = 0;
249 /* The dialog handle for the main program */
250 Dlg_head *midnight_dlg = NULL;
252 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
253 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
254 int update_prompt = 0;
256 /* The home directory */
257 const char *home_dir = NULL;
259 /* Tab size */
260 int option_tab_spacing = 8;
262 /* Only used at program boot */
263 int boot_current_is_left = 1;
265 /* If this is true, then when browsing the tree the other window will
266 * automatically reload it's directory with the contents of the currently
267 * selected directory.
269 int xtree_mode = 0;
271 /* If set, then print to the given file the last directory we were at */
272 static char *last_wd_string = NULL;
273 /* Set to 1 to suppress printing the last directory */
274 static int print_last_revert = 0;
276 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
277 char *mc_run_param0 = NULL;
278 char *mc_run_param1 = NULL;
280 /* Line to start the editor on */
281 static int edit_one_file_start_line = 0;
283 /* Used so that widgets know if they are being destroyed or
284 shut down */
285 int midnight_shutdown = 0;
287 /* The user's shell */
288 char *shell = NULL;
290 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
291 char *mc_home = NULL;
293 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
294 char *mc_home_alt = NULL;
296 /* Define this function for glib-style error handling */
297 GQuark
298 mc_main_error_quark (void)
300 return g_quark_from_static_string (PACKAGE);
303 #ifdef USE_INTERNAL_EDIT
304 GArray *editor_keymap = NULL;
305 GArray *editor_x_keymap = NULL;
306 #endif
307 GArray *viewer_keymap = NULL;
308 GArray *viewer_hex_keymap = NULL;
309 GArray *main_keymap = NULL;
310 GArray *main_x_keymap = NULL;
311 GArray *panel_keymap = NULL;
312 GArray *input_keymap = NULL;
313 GArray *tree_keymap = NULL;
314 GArray *help_keymap = NULL;
315 #ifdef USE_DIFF_VIEW
316 GArray *diff_keymap = NULL;
317 #endif
318 const global_keymap_t *main_map;
319 const global_keymap_t *main_x_map;
321 /* Save current stat of directories to avoid reloading the panels */
322 /* when no modifications have taken place */
323 void
324 save_cwds_stat (void)
326 if (fast_reload) {
327 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
328 if (get_other_type () == view_listing)
329 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
333 #ifdef HAVE_SUBSHELL_SUPPORT
334 void
335 do_update_prompt (void)
337 if (update_prompt) {
338 printf ("\r\n%s", subshell_prompt);
339 fflush (stdout);
340 update_prompt = 0;
343 #endif /* HAVE_SUBSHELL_SUPPORT */
345 void
346 change_panel (void)
348 free_completions (cmdline);
349 dlg_one_down (midnight_dlg);
352 /* Stop MC main dialog and the current dialog if it exists.
353 * Needed to provide fast exit from MC viewer or editor on shell exit */
354 static void
355 stop_dialogs (void)
357 midnight_dlg->running = 0;
358 if (current_dlg) {
359 current_dlg->running = 0;
363 static int
364 quit_cmd_internal (int quiet)
366 int q = quit;
368 if (quiet || !confirm_exit) {
369 q = 1;
370 } else {
371 if (query_dialog
372 (_(" The Midnight Commander "),
373 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
374 2, _("&Yes"), _("&No")) == 0)
375 q = 1;
377 if (q) {
378 #ifdef HAVE_SUBSHELL_SUPPORT
379 if (!use_subshell)
380 stop_dialogs ();
381 else if ((q = exit_subshell ()))
382 #endif
383 stop_dialogs ();
385 if (q)
386 quit |= 1;
387 return quit;
390 static void
391 quit_cmd (void)
393 quit_cmd_internal (0);
396 void
397 quiet_quit_cmd (void)
399 print_last_revert = 1;
400 quit_cmd_internal (1);
403 /* Wrapper for do_subshell_chdir, check for availability of subshell */
404 void
405 subshell_chdir (const char *directory)
407 #ifdef HAVE_SUBSHELL_SUPPORT
408 if (use_subshell) {
409 if (vfs_current_is_local ())
410 do_subshell_chdir (directory, 0, 1);
412 #endif /* HAVE_SUBSHELL_SUPPORT */
415 void
416 directory_history_add (struct WPanel *panel, const char *dir)
418 char *tmp;
420 tmp = g_strdup (dir);
421 strip_password (tmp, 1);
423 panel->dir_history = list_append_unique (panel->dir_history, tmp);
427 * If we moved to the parent directory move the selection pointer to
428 * the old directory name; If we leave VFS dir, remove FS specificator.
430 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
432 static const char *
433 get_parent_dir_name (const char *cwd, const char *lwd)
435 const char *p;
436 if (strlen (lwd) > strlen (cwd))
437 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
438 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
439 cwd[1] == '\0'))) {
440 return (p + 1);
442 return NULL;
446 * Changes the current directory of the panel.
447 * Don't record change in the directory history.
449 static int
450 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
452 const char *directory;
453 char *olddir;
454 char temp[MC_MAXPATHLEN];
455 char *translated_url;
457 if (cd_type == cd_parse_command) {
458 while (*new_dir == ' ')
459 new_dir++;
462 olddir = g_strdup (panel->cwd);
463 new_dir = translated_url = vfs_translate_url (new_dir);
465 /* Convert *new_path to a suitable pathname, handle ~user */
467 if (cd_type == cd_parse_command) {
468 if (!strcmp (new_dir, "-")) {
469 strcpy (temp, panel->lwd);
470 new_dir = temp;
473 directory = *new_dir ? new_dir : home_dir;
475 if (mc_chdir (directory) == -1) {
476 strcpy (panel->cwd, olddir);
477 g_free (olddir);
478 g_free (translated_url);
479 return 0;
481 g_free (translated_url);
483 /* Success: save previous directory, shutdown status of previous dir */
484 strcpy (panel->lwd, olddir);
485 free_completions (cmdline);
487 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
489 vfs_release_path (olddir);
491 subshell_chdir (panel->cwd);
493 /* Reload current panel */
494 panel_clean_dir (panel);
495 panel->count =
496 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
497 panel->reverse, panel->case_sensitive,
498 panel->exec_first, panel->filter);
499 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
500 load_hint (0);
501 panel->dirty = 1;
502 update_xterm_title_path ();
504 g_free (olddir);
506 return 1;
510 * Changes the current directory of the panel.
511 * Record change in the directory history.
514 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
516 int r;
518 r = _do_panel_cd (panel, new_dir, cd_type);
519 if (r)
520 directory_history_add (panel, panel->cwd);
521 return r;
525 do_cd (const char *new_dir, enum cd_enum exact)
527 return (do_panel_cd (current_panel, new_dir, exact));
530 void
531 directory_history_next (WPanel *panel)
533 GList *nextdir;
535 nextdir = g_list_next (panel->dir_history);
537 if (!nextdir)
538 return;
540 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
541 panel->dir_history = nextdir;
544 void
545 directory_history_prev (WPanel *panel)
547 GList *prevdir;
549 prevdir = g_list_previous (panel->dir_history);
551 if (!prevdir)
552 return;
554 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
555 panel->dir_history = prevdir;
558 void
559 directory_history_list (WPanel *panel)
561 char *s;
563 s = show_hist (&panel->dir_history, &panel->widget);
565 if (s != NULL) {
566 if (_do_panel_cd (panel, s, cd_exact))
567 directory_history_add (panel, panel->cwd);
568 else
569 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
570 g_free (s);
574 #ifdef HAVE_SUBSHELL_SUPPORT
576 load_prompt (int fd, void *unused)
578 (void) fd;
579 (void) unused;
581 if (!read_subshell_prompt ())
582 return 0;
584 /* Don't actually change the prompt if it's invisible */
585 if (current_dlg == midnight_dlg && command_prompt) {
586 char *tmp_prompt;
587 int prompt_len;
589 tmp_prompt = strip_ctrl_codes (subshell_prompt);
590 prompt_len = str_term_width1 (tmp_prompt);
592 /* Check for prompts too big */
593 if (COLS > 8 && prompt_len > COLS - 8) {
594 tmp_prompt[COLS - 8] = '\0';
595 prompt_len = COLS - 8;
597 mc_prompt = tmp_prompt;
598 label_set_text (the_prompt, mc_prompt);
599 winput_set_origin ((WInput *) cmdline, prompt_len,
600 COLS - prompt_len);
602 /* since the prompt has changed, and we are called from one of the
603 * tty_get_event channels, the prompt updating does not take place
604 * automatically: force a cursor update and a screen refresh
606 update_cursor (midnight_dlg);
607 mc_refresh ();
609 update_prompt = 1;
610 return 0;
612 #endif /* HAVE_SUBSHELL_SUPPORT */
614 void
615 sort_cmd (void)
617 WPanel *p;
618 const panel_field_t *sort_order;
620 if (!SELECTED_IS_PANEL)
621 return;
623 p = MENU_PANEL;
624 sort_order = sort_box (p->current_sort_field, &p->reverse,
625 &p->case_sensitive,
626 &p->exec_first);
628 panel_set_sort_order (p, sort_order);
632 static void
633 treebox_cmd (void)
635 char *sel_dir;
637 sel_dir = tree_box (selection (current_panel)->fname);
638 if (sel_dir) {
639 do_cd (sel_dir, cd_exact);
640 g_free (sel_dir);
644 #ifdef LISTMODE_EDITOR
645 static void
646 listmode_cmd (void)
648 char *newmode;
650 if (get_current_type () != view_listing)
651 return;
653 newmode = listmode_edit (current_panel->user_format);
654 if (!newmode)
655 return;
657 g_free (current_panel->user_format);
658 current_panel->list_type = list_user;
659 current_panel->user_format = newmode;
660 set_panel_formats (current_panel);
662 do_refresh ();
664 #endif /* LISTMODE_EDITOR */
666 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
667 static GList *
668 create_panel_menu (void)
670 GList *entries = NULL;
672 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
673 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
674 entries = g_list_append (entries, menu_entry_create (_("&Info" ), CK_InfoCmd));
675 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
676 entries = g_list_append (entries, menu_separator_create ());
677 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
678 entries = g_list_append (entries, menu_separator_create ());
679 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
680 #ifdef HAVE_CHARSET
681 entries = g_list_append (entries, menu_separator_create ());
682 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
683 #endif
684 #ifdef USE_NETCODE
685 entries = g_list_append (entries, menu_separator_create ());
686 #ifdef ENABLE_VFS_MCFS
687 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
688 #endif
689 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
690 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
691 #ifdef ENABLE_VFS_SMB
692 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
693 #endif /* ENABLE_VFS_SMB */
694 #endif
695 entries = g_list_append (entries, menu_separator_create ());
696 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
698 return entries;
701 static GList *
702 create_file_menu (void)
704 GList *entries = NULL;
706 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
707 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
708 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
709 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
710 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
711 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
712 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
713 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
714 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
715 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
716 entries = g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
717 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
718 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
719 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
720 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
721 entries = g_list_append (entries, menu_separator_create ());
722 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
723 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
724 entries = g_list_append (entries, menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
725 entries = g_list_append (entries, menu_separator_create ());
726 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
728 return entries;
731 static GList *
732 create_command_menu (void)
734 /* I know, I'm lazy, but the tree widget when it's not running
735 * as a panel still has some problems, I have not yet finished
736 * the WTree widget port, sorry.
738 GList *entries = NULL;
740 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
741 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
742 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
743 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
744 entries = g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
745 entries = g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
746 #ifdef USE_DIFF_VIEW
747 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
748 #endif
749 entries = g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
750 entries = g_list_append (entries, menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
751 entries = g_list_append (entries, menu_separator_create ());
752 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
753 entries = g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
754 #ifdef ENABLE_VFS
755 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
756 #endif
757 #ifdef WITH_BACKGROUND
758 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
759 #endif
760 entries = g_list_append (entries, menu_separator_create ());
761 #ifdef USE_EXT2FSLIB
762 entries = g_list_append (entries, menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
763 #endif
764 #ifdef LISTMODE_EDITOR
765 entries = g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
766 #endif
767 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
768 entries = g_list_append (entries, menu_separator_create ());
769 #endif
770 entries = g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
771 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
772 entries = g_list_append (entries, menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
774 return entries;
777 static GList *
778 create_options_menu (void)
780 GList *entries = NULL;
782 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
783 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
784 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
785 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
786 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
787 #ifdef ENABLE_VFS
788 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
789 #endif
790 entries = g_list_append (entries, menu_separator_create ());
791 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
793 return entries;
796 void
797 init_menu (void)
799 menubar_add_menu (the_menubar,
800 create_menu (horizontal_split ? _("&Above") : _("&Left"),
801 create_panel_menu (), "[Left and Right Menus]"));
802 menubar_add_menu (the_menubar,
803 create_menu (_("&File"), create_file_menu (), "[File Menu]"));
804 menubar_add_menu (the_menubar,
805 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
806 menubar_add_menu (the_menubar,
807 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
808 menubar_add_menu (the_menubar,
809 create_menu (horizontal_split ? _("&Below") : _("&Right"),
810 create_panel_menu (), "[Left and Right Menus]"));
813 void
814 done_menu (void)
816 menubar_set_menu (the_menubar, NULL);
819 static void
820 menu_last_selected_cmd (void)
822 the_menubar->is_active = TRUE;
823 the_menubar->is_dropped = (drop_menus != 0);
824 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
825 dlg_select_widget (the_menubar);
828 static void
829 menu_cmd (void)
831 if (the_menubar->is_active)
832 return;
834 if ((get_current_index () == 0) == (current_panel->active != 0))
835 the_menubar->selected = 0;
836 else
837 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
838 menu_last_selected_cmd ();
841 static char *
842 midnight_get_shortcut (unsigned long command)
844 const char *ext_map;
845 const char *shortcut = NULL;
847 shortcut = lookup_keymap_shortcut (main_map, command);
848 if (shortcut != NULL)
849 return g_strdup (shortcut);
851 shortcut = lookup_keymap_shortcut (panel_map, command);
852 if (shortcut != NULL)
853 return g_strdup (shortcut);
855 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
856 if (ext_map != NULL)
857 shortcut = lookup_keymap_shortcut (main_x_map, command);
858 if (shortcut != NULL)
859 return g_strdup_printf ("%s %s", ext_map, shortcut);
861 return NULL;
864 /* Flag toggling functions */
865 void
866 toggle_fast_reload (void)
868 fast_reload = !fast_reload;
869 if (fast_reload_w == 0 && fast_reload) {
870 message (D_NORMAL, _(" Information "),
872 (" Using the fast reload option may not reflect the exact \n"
873 " directory contents. In this case you'll need to do a \n"
874 " manual reload of the directory. See the man page for \n"
875 " the details. "));
876 fast_reload_w = 1;
880 void
881 toggle_mix_all_files (void)
883 mix_all_files = !mix_all_files;
884 update_panels (UP_RELOAD, UP_KEEPSEL);
887 void
888 toggle_show_backup (void)
890 show_backups = !show_backups;
891 update_panels (UP_RELOAD, UP_KEEPSEL);
894 void
895 toggle_show_hidden (void)
897 show_dot_files = !show_dot_files;
898 update_panels (UP_RELOAD, UP_KEEPSEL);
901 static void
902 toggle_panels_split (void)
904 horizontal_split = !horizontal_split;
905 layout_change ();
906 do_refresh();
909 void
910 toggle_kilobyte_si (void)
912 kilobyte_si = !kilobyte_si;
913 update_panels (UP_RELOAD, UP_KEEPSEL);
917 * Just a hack for allowing url-like pathnames to be accepted from the
918 * command line.
920 static void
921 translated_mc_chdir (char *dir)
923 char *newdir;
924 int ret;
926 newdir = vfs_translate_url (dir);
927 ret = mc_chdir (newdir);
928 g_free (newdir);
931 static void
932 create_panels (void)
934 int current_index;
935 int other_index;
936 panel_view_mode_t current_mode, other_mode;
937 char original_dir[BUF_1K] = "\0";
939 if (boot_current_is_left) {
940 current_index = 0;
941 other_index = 1;
942 current_mode = startup_left_mode;
943 other_mode = startup_right_mode;
944 } else {
945 current_index = 1;
946 other_index = 0;
947 current_mode = startup_right_mode;
948 other_mode = startup_left_mode;
950 /* Creates the left panel */
951 if (mc_run_param0 != NULL) {
952 if (mc_run_param1 != NULL) {
953 /* Ok, user has specified two dirs, save the original one,
954 * since we may not be able to chdir to the proper
955 * second directory later
957 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
959 translated_mc_chdir (mc_run_param0);
961 set_display_type (current_index, current_mode);
963 /* The other panel */
964 if (mc_run_param1 != NULL) {
965 if (original_dir[0] != '\0')
966 translated_mc_chdir (original_dir);
967 translated_mc_chdir (mc_run_param1);
969 set_display_type (other_index, other_mode);
971 if (startup_left_mode == view_listing) {
972 current_panel = left_panel;
973 } else {
974 if (right_panel)
975 current_panel = right_panel;
976 else
977 current_panel = left_panel;
980 /* Create the nice widgets */
981 cmdline = command_new (0, 0, 0);
982 the_prompt = label_new (0, 0, mc_prompt);
983 the_prompt->transparent = 1;
984 the_bar = buttonbar_new (keybar_visible);
986 the_hint = label_new (0, 0, 0);
987 the_hint->transparent = 1;
988 the_hint->auto_adjust_cols = 0;
989 the_hint->widget.cols = COLS;
991 the_menubar = menubar_new (0, 0, COLS, NULL);
994 static void
995 copy_current_pathname (void)
997 char *cwd_path;
998 if (!command_prompt)
999 return;
1001 cwd_path = remove_encoding_from_path (current_panel->cwd);
1002 command_insert (cmdline, cwd_path, 0);
1004 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1005 command_insert (cmdline, PATH_SEP_STR, 0);
1006 g_free (cwd_path);
1009 static void
1010 copy_other_pathname (void)
1012 char *cwd_path;
1014 if (get_other_type () != view_listing)
1015 return;
1017 if (!command_prompt)
1018 return;
1020 cwd_path = remove_encoding_from_path (other_panel->cwd);
1021 command_insert (cmdline, cwd_path, 0);
1023 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1024 command_insert (cmdline, PATH_SEP_STR, 0);
1025 g_free (cwd_path);
1028 static void
1029 copy_readlink (WPanel *panel)
1031 if (!command_prompt)
1032 return;
1033 if (S_ISLNK (selection (panel)->st.st_mode)) {
1034 char buffer[MC_MAXPATHLEN];
1035 char *p =
1036 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1037 int i;
1039 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1040 g_free (p);
1041 if (i > 0) {
1042 buffer[i] = 0;
1043 command_insert (cmdline, buffer, 1);
1048 static void
1049 copy_current_readlink (void)
1051 copy_readlink (current_panel);
1054 static void
1055 copy_other_readlink (void)
1057 if (get_other_type () == view_listing)
1058 copy_readlink (other_panel);
1061 /* Insert the selected file name into the input line */
1062 static void
1063 copy_prog_name (void)
1065 char *tmp;
1066 if (!command_prompt)
1067 return;
1069 if (get_current_type () == view_tree) {
1070 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1071 tmp = tree_selected_name (tree);
1072 } else
1073 tmp = selection (current_panel)->fname;
1075 command_insert (cmdline, tmp, 1);
1078 static void
1079 copy_tagged (WPanel *panel)
1081 int i;
1083 if (!command_prompt)
1084 return;
1085 input_disable_update (cmdline);
1086 if (panel->marked) {
1087 for (i = 0; i < panel->count; i++) {
1088 if (panel->dir.list[i].f.marked)
1089 command_insert (cmdline, panel->dir.list[i].fname, 1);
1091 } else {
1092 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1095 input_enable_update (cmdline);
1098 static void
1099 copy_current_tagged (void)
1101 copy_tagged (current_panel);
1104 static void
1105 copy_other_tagged (void)
1107 if (get_other_type () == view_listing)
1108 copy_tagged (other_panel);
1111 void
1112 midnight_set_buttonbar (WButtonBar *b)
1114 buttonbar_set_label (b, 1, Q_("ButtonBar|Help"), main_map, NULL);
1115 buttonbar_set_label (b, 2, Q_("ButtonBar|Menu"), main_map, NULL);
1116 buttonbar_set_label (b, 3, Q_("ButtonBar|View"), main_map, NULL);
1117 buttonbar_set_label (b, 4, Q_("ButtonBar|Edit"), main_map, NULL);
1118 buttonbar_set_label (b, 5, Q_("ButtonBar|Copy"), main_map, NULL);
1119 buttonbar_set_label (b, 6, Q_("ButtonBar|RenMov"), main_map, NULL);
1120 buttonbar_set_label (b, 7, Q_("ButtonBar|Mkdir"), main_map, NULL);
1121 buttonbar_set_label (b, 8, Q_("ButtonBar|Delete"), main_map, NULL);
1122 buttonbar_set_label (b, 9, Q_("ButtonBar|PullDn"), main_map, NULL);
1123 buttonbar_set_label (b, 10, Q_("ButtonBar|Quit"), main_map, NULL);
1126 static gboolean ctl_x_map_enabled = FALSE;
1128 static void
1129 ctl_x_cmd (void)
1131 ctl_x_map_enabled = TRUE;
1134 static cb_ret_t
1135 midnight_execute_cmd (Widget *sender, unsigned long command)
1137 cb_ret_t res = MSG_HANDLED;
1139 (void) sender;
1141 switch (command) {
1142 case CK_AddHotlist:
1143 add2hotlist_cmd ();
1144 break;
1145 case CK_ChmodCmd:
1146 chmod_cmd ();
1147 break;
1148 case CK_ChownCmd:
1149 chown_cmd ();
1150 break;
1151 case CK_ChownAdvancedCmd:
1152 chown_advanced_cmd ();
1153 break;
1154 case CK_CompareDirsCmd:
1155 compare_dirs_cmd ();
1156 break;
1157 case CK_ConfigureBox:
1158 configure_box ();
1159 break;
1160 #ifdef ENABLE_VFS
1161 case CK_ConfigureVfs:
1162 configure_vfs ();
1163 break;
1164 #endif
1165 case CK_ConfirmBox:
1166 confirm_box ();
1167 break;
1168 case CK_CopyCmd:
1169 copy_cmd ();
1170 break;
1171 case CK_CopyCurrentPathname:
1172 copy_current_pathname ();
1173 break;
1174 case CK_CopyCurrentReadlink:
1175 copy_current_readlink ();
1176 break;
1177 case CK_CopyCurrentTagged:
1178 copy_current_tagged ();
1179 break;
1180 case CK_CopyOtherPathname:
1181 copy_other_pathname ();
1182 break;
1183 case CK_CopyOtherReadlink:
1184 copy_other_readlink ();
1185 break;
1186 case CK_CopyOtherTagged:
1187 copy_other_tagged ();
1188 break;
1189 case CK_DeleteCmd:
1190 delete_cmd ();
1191 break;
1192 #ifdef USE_DIFF_VIEW
1193 case CK_DiffViewCmd:
1194 diff_view_cmd ();
1195 break;
1196 #endif
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 init_xterm_support (void)
1394 const char *termvalue;
1396 termvalue = getenv ("TERM");
1397 if (!termvalue || !(*termvalue)) {
1398 fputs (_("The TERM environment variable is unset!\n"), stderr);
1399 exit (1);
1402 /* Check mouse capabilities */
1403 xmouse_seq = tty_tgetstr ("Km");
1405 if (strcmp (termvalue, "cygwin") == 0) {
1406 mc_args__force_xterm = 1;
1407 use_mouse_p = MOUSE_DISABLED;
1410 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1411 || strncmp (termvalue, "konsole", 7) == 0
1412 || strncmp (termvalue, "rxvt", 4) == 0
1413 || strcmp (termvalue, "Eterm") == 0
1414 || strcmp (termvalue, "dtterm") == 0) {
1415 xterm_flag = 1;
1417 /* Default to the standard xterm sequence */
1418 if (!xmouse_seq) {
1419 xmouse_seq = ESC_STR "[M";
1422 /* Enable mouse unless explicitly disabled by --nomouse */
1423 if (use_mouse_p != MOUSE_DISABLED) {
1424 const char *color_term = getenv ("COLORTERM");
1425 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1426 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1427 strcmp (termvalue, "Eterm") == 0) {
1428 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1429 } else {
1430 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1436 static void
1437 setup_mc (void)
1439 #ifdef HAVE_SLANG
1440 tty_display_8bit (full_eight_bits != 0);
1441 #else
1442 tty_display_8bit (eight_bit_clean != 0);
1443 #endif
1445 #ifdef HAVE_SUBSHELL_SUPPORT
1446 if (use_subshell)
1447 add_select_channel (subshell_pty, load_prompt, 0);
1448 #endif /* !HAVE_SUBSHELL_SUPPORT */
1450 tty_setup_sigwinch (sigwinch_handler);
1452 if ((tty_baudrate () < 9600) || tty_is_slow ())
1453 verbose = 0;
1455 init_xterm_support ();
1456 init_mouse ();
1459 static void
1460 setup_dummy_mc (void)
1462 char d[MC_MAXPATHLEN];
1463 int ret;
1465 mc_get_current_wd (d, MC_MAXPATHLEN);
1466 setup_mc ();
1467 ret = mc_chdir (d);
1470 static void check_codeset()
1472 const char *current_system_codepage = NULL;
1474 current_system_codepage = str_detect_termencoding();
1476 #ifdef HAVE_CHARSET
1478 const char *_display_codepage;
1480 _display_codepage = get_codepage_id (display_codepage);
1482 if (! strcmp(_display_codepage, current_system_codepage)) {
1483 utf8_display = str_isutf8 (current_system_codepage);
1484 return;
1487 display_codepage = get_codepage_index (current_system_codepage);
1488 if (display_codepage == -1) {
1489 display_codepage = 0;
1492 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1494 #endif
1495 utf8_display = str_isutf8 (current_system_codepage);
1498 static void
1499 done_screen (void)
1501 if (!(quit & SUBSHELL_EXIT))
1502 clr_scr ();
1503 tty_reset_shell_mode ();
1504 tty_noraw_mode ();
1505 tty_keypad (FALSE);
1506 tty_colors_done ();
1509 static void
1510 done_mc (void)
1512 disable_mouse ();
1514 /* Setup shutdown
1516 * We sync the profiles since the hotlist may have changed, while
1517 * we only change the setup data if we have the auto save feature set
1520 if (auto_save_setup)
1521 save_setup (); /* does also call save_hotlist */
1522 else {
1523 save_hotlist ();
1524 save_panel_types ();
1526 done_screen ();
1527 vfs_add_current_stamps ();
1530 /* This should be called after destroy_dlg since panel widgets
1531 * save their state on the profiles
1533 static void
1534 done_mc_profile (void)
1536 done_setup ();
1539 static cb_ret_t
1540 midnight_callback (Dlg_head *h, Widget *sender,
1541 dlg_msg_t msg, int parm, void *data)
1543 unsigned long command;
1545 switch (msg) {
1546 case DLG_INIT:
1547 setup_panels ();
1548 return MSG_HANDLED;
1550 case DLG_DRAW:
1551 load_hint (1);
1552 /* We handle the special case of the output lines */
1553 if (console_flag && output_lines)
1554 show_console_contents (output_start_y,
1555 LINES - output_lines - keybar_visible -
1556 1, LINES - keybar_visible - 1);
1557 return MSG_HANDLED;
1559 case DLG_RESIZE:
1560 setup_panels ();
1561 menubar_arrange (the_menubar);
1562 return MSG_HANDLED;
1564 case DLG_IDLE:
1565 /* We only need the first idle event to show user menu after start */
1566 set_idle_proc (h, 0);
1568 if (boot_current_is_left)
1569 dlg_select_widget (get_panel_widget (0));
1570 else
1571 dlg_select_widget (get_panel_widget (1));
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 create_panels_and_run_mc (void)
1782 midnight_dlg->get_shortcut = midnight_get_shortcut;
1784 create_panels ();
1786 add_widget (midnight_dlg, the_menubar);
1787 init_menu ();
1789 add_widget (midnight_dlg, get_panel_widget (0));
1790 add_widget (midnight_dlg, get_panel_widget (1));
1792 add_widget (midnight_dlg, the_hint);
1793 add_widget (midnight_dlg, cmdline);
1794 add_widget (midnight_dlg, the_prompt);
1796 add_widget (midnight_dlg, the_bar);
1797 midnight_set_buttonbar (the_bar);
1799 /* Run the Midnight Commander if no file was specified in the command line */
1800 run_dlg (midnight_dlg);
1803 /* result must be free'd (I think this should go in util.c) */
1804 static char *
1805 prepend_cwd_on_local (const char *filename)
1807 char *d;
1808 size_t l;
1810 if (!vfs_file_is_local (filename)
1811 || g_path_is_absolute (filename))
1812 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;
1823 /* Invoke the internal view/edit routine with:
1824 * the default processing and forcing the internal viewer/editor
1826 static void
1827 mc_maybe_editor_or_viewer (void)
1829 switch (mc_run_mode) {
1830 #ifdef USE_INTERNAL_EDIT
1831 case MC_RUN_EDITOR:
1832 edit_file (mc_run_param0, edit_one_file_start_line);
1833 break;
1834 #endif /* USE_INTERNAL_EDIT */
1835 case MC_RUN_VIEWER:
1837 char *path;
1838 path = prepend_cwd_on_local (mc_run_param0);
1839 view_file (path, 0, 1);
1840 g_free (path);
1841 break;
1843 #ifdef USE_DIFF_VIEW
1844 case MC_RUN_DIFFVIEWER:
1845 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1846 break;
1847 #endif /* USE_DIFF_VIEW */
1848 default:
1849 break;
1851 midnight_shutdown = 1;
1852 done_mc ();
1855 /* Run the main dialog that occupies the whole screen */
1856 static void
1857 do_nc (void)
1859 int midnight_colors[DLG_COLOR_NUM];
1860 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1861 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1862 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1863 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1865 panel_init ();
1867 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1868 "[main]", NULL, DLG_WANT_IDLE);
1870 if (mc_run_mode == MC_RUN_FULL)
1871 setup_mc ();
1872 else
1873 setup_dummy_mc ();
1875 /* start check display_codepage and source_codepage */
1876 check_codeset ();
1878 main_map = default_main_map;
1879 if (main_keymap && main_keymap->len > 0)
1880 main_map = (global_keymap_t *) main_keymap->data;
1882 main_x_map = default_main_x_map;
1883 if (main_x_keymap && main_x_keymap->len > 0)
1884 main_x_map = (global_keymap_t *) main_x_keymap->data;
1886 panel_map = default_panel_keymap;
1887 if (panel_keymap && panel_keymap->len > 0)
1888 panel_map = (global_keymap_t *) panel_keymap->data;
1890 input_map = default_input_keymap;
1891 if (input_keymap && input_keymap->len > 0)
1892 input_map = (global_keymap_t *) input_keymap->data;
1894 tree_map = default_tree_keymap;
1895 if (tree_keymap && tree_keymap->len > 0)
1896 tree_map = (global_keymap_t *) tree_keymap->data;
1898 help_map = default_help_keymap;
1899 if (help_keymap && help_keymap->len > 0)
1900 help_map = (global_keymap_t *) help_keymap->data;
1902 #ifdef USE_DIFF_VIEW
1903 diff_map = default_diff_keymap;
1904 if (diff_keymap && diff_keymap->len > 0)
1905 diff_map = (global_keymap_t *) diff_keymap->data;
1906 #endif
1908 /* Check if we were invoked as an editor or file viewer */
1909 if (mc_run_mode != MC_RUN_FULL)
1910 mc_maybe_editor_or_viewer ();
1911 else {
1912 create_panels_and_run_mc ();
1914 /* Program end */
1915 midnight_shutdown = 1;
1917 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1918 if (mc_args__last_wd_file && vfs_current_is_local ())
1919 last_wd_string = g_strdup (current_panel->cwd);
1921 done_mc ();
1924 destroy_dlg (midnight_dlg);
1925 panel_deinit ();
1926 current_panel = 0;
1927 done_mc_profile ();
1930 /* POSIX version. The only version we support. */
1931 static void
1932 OS_Setup (void)
1934 const char *shell_env = getenv ("SHELL");
1935 const char *mc_libdir;
1937 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1938 struct passwd *pwd;
1939 pwd = getpwuid (geteuid ());
1940 if (pwd != NULL)
1941 shell = g_strdup (pwd->pw_shell);
1942 } else
1943 shell = g_strdup (shell_env);
1945 if ((shell == NULL) || (shell[0] == '\0')) {
1946 g_free (shell);
1947 shell = g_strdup ("/bin/sh");
1950 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1951 /* and can be overriden by the MC_DATADIR environment variable */
1952 mc_libdir = getenv ("MC_DATADIR");
1953 if (mc_libdir != NULL) {
1954 mc_home = g_strdup (mc_libdir);
1955 mc_home_alt = g_strdup (SYSCONFDIR);
1956 } else {
1957 mc_home = g_strdup (SYSCONFDIR);
1958 mc_home_alt = g_strdup (DATADIR);
1961 /* This variable is used by the subshell */
1962 home_dir = getenv ("HOME");
1964 if (!home_dir)
1965 home_dir = mc_home;
1968 static void
1969 sigchld_handler_no_subshell (int sig)
1971 #ifdef __linux__
1972 int pid, status;
1974 if (!console_flag)
1975 return;
1977 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1978 the value of console_flag never changed, we could simply not install
1979 this handler at all if (!console_flag && !use_subshell). */
1981 /* That comment is no longer true. We need to wait() on a sigchld
1982 handler (that's at least what the tarfs code expects currently). */
1984 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1986 if (pid == cons_saver_pid) {
1988 if (WIFSTOPPED (status)) {
1989 /* Someone has stopped cons.saver - restart it */
1990 kill (pid, SIGCONT);
1991 } else {
1992 /* cons.saver has died - disable console saving */
1993 handle_console (CONSOLE_DONE);
1994 console_flag = 0;
1997 /* If we got here, some other child exited; ignore it */
1998 #endif /* __linux__ */
2000 (void) sig;
2003 static void
2004 init_sigchld (void)
2006 struct sigaction sigchld_action;
2008 sigchld_action.sa_handler =
2009 #ifdef HAVE_SUBSHELL_SUPPORT
2010 use_subshell ? sigchld_handler :
2011 #endif /* HAVE_SUBSHELL_SUPPORT */
2012 sigchld_handler_no_subshell;
2014 sigemptyset (&sigchld_action.sa_mask);
2016 #ifdef SA_RESTART
2017 sigchld_action.sa_flags = SA_RESTART;
2018 #else
2019 sigchld_action.sa_flags = 0;
2020 #endif /* !SA_RESTART */
2022 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2023 #ifdef HAVE_SUBSHELL_SUPPORT
2025 * This may happen on QNX Neutrino 6, where SA_RESTART
2026 * is defined but not implemented. Fallback to no subshell.
2028 use_subshell = 0;
2029 #endif /* HAVE_SUBSHELL_SUPPORT */
2033 static void
2034 mc_main__setup_by_args (int argc, char *argv[])
2036 const char *base;
2037 char *tmp;
2039 if (mc_args__nomouse)
2040 use_mouse_p = MOUSE_DISABLED;
2042 #ifdef USE_NETCODE
2043 if (mc_args__netfs_logfile != NULL) {
2044 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
2045 #ifdef ENABLE_VFS_SMB
2046 smbfs_set_debugf (mc_args__netfs_logfile);
2047 #endif /* ENABLE_VFS_SMB */
2050 #ifdef ENABLE_VFS_SMB
2051 if (mc_args__debug_level != 0)
2052 smbfs_set_debug (mc_args__debug_level);
2053 #endif /* ENABLE_VFS_SMB */
2054 #endif /* USE_NETCODE */
2056 base = x_basename (argv[0]);
2057 tmp = (argc > 0) ? argv[1] : NULL;
2059 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2060 mc_run_param0 = g_strdup ("");
2061 if (tmp != NULL) {
2063 * Check for filename:lineno, followed by an optional colon.
2064 * This format is used by many programs (especially compilers)
2065 * in error messages and warnings. It is supported so that
2066 * users can quickly copy and paste file locations.
2068 char *end = tmp + strlen (tmp), *p = end;
2069 if (p > tmp && p[-1] == ':')
2070 p--;
2071 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2072 p--;
2073 if (tmp < p && p < end && p[-1] == ':') {
2074 struct stat st;
2075 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2077 * Check that the file before the colon actually exists.
2078 * If it doesn't exist, revert to the old behavior.
2080 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2081 mc_run_param0 = fname;
2082 edit_one_file_start_line = atoi (p);
2083 } else {
2084 g_free (fname);
2085 goto try_plus_filename;
2087 } else {
2088 try_plus_filename:
2089 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2090 int start_line = atoi (tmp);
2091 if (start_line > 0) {
2092 char *file = (argc > 1) ? argv[2] : NULL;
2093 if (file) {
2094 tmp = file;
2095 edit_one_file_start_line = start_line;
2099 mc_run_param0 = g_strdup (tmp);
2102 mc_run_mode = MC_RUN_EDITOR;
2103 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2104 if (tmp != NULL)
2105 mc_run_param0 = g_strdup (tmp);
2106 else {
2107 fputs ("No arguments given to the viewer\n", stderr);
2108 exit (1);
2110 mc_run_mode = MC_RUN_VIEWER;
2111 #ifdef USE_DIFF_VIEW
2112 } else if (!STRNCOMP (base, "mcd", 3) || !STRCOMP (base, "diff")) {
2113 if (argc < 3) {
2114 fputs ("There 2 files are required to diffviewer\n", stderr);
2115 exit (1);
2116 } else if (tmp != NULL) {
2117 mc_run_param0 = g_strdup (tmp);
2118 tmp = (argc > 1) ? argv[2] : NULL;
2119 if (tmp != NULL)
2120 mc_run_param1 = g_strdup (tmp);
2121 mc_run_mode = MC_RUN_DIFFVIEWER;
2123 #endif /* USE_DIFF_VIEW */
2124 } else {
2125 /* sets the current dir and the other dir */
2126 if (tmp != NULL) {
2127 mc_run_param0 = g_strdup (tmp);
2128 tmp = (argc > 1) ? argv[2] : NULL;
2129 if (tmp != NULL)
2130 mc_run_param1 = g_strdup (tmp);
2132 mc_run_mode = MC_RUN_FULL;
2137 main (int argc, char *argv[])
2139 struct stat s;
2140 char *mc_dir;
2141 GError *error = NULL;
2142 gboolean isInitialized;
2144 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2145 setlocale (LC_ALL, "");
2146 bindtextdomain ("mc", LOCALEDIR);
2147 textdomain ("mc");
2149 /* Set up temporary directory */
2150 mc_tmpdir ();
2152 OS_Setup ();
2154 str_init_strings (NULL);
2156 vfs_init ();
2158 #ifdef USE_INTERNAL_EDIT
2159 edit_stack_init ();
2160 #endif
2162 #ifdef HAVE_SLANG
2163 SLtt_Ignore_Beep = 1;
2164 #endif
2166 if ( !mc_args_handle (&argc, &argv, "mc"))
2167 return 1;
2169 mc_main__setup_by_args (argc,argv);
2171 /* NOTE: This has to be called before tty_init or whatever routine
2172 calls any define_sequence */
2173 init_key ();
2175 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2176 handle_console (CONSOLE_INIT);
2178 #ifdef HAVE_SUBSHELL_SUPPORT
2179 /* Don't use subshell when invoked as viewer or editor */
2180 if (mc_run_mode != MC_RUN_FULL)
2181 use_subshell = 0;
2183 if (use_subshell)
2184 subshell_get_console_attributes ();
2185 #endif /* HAVE_SUBSHELL_SUPPORT */
2187 /* Install the SIGCHLD handler; must be done before init_subshell() */
2188 init_sigchld ();
2190 /* We need this, since ncurses endwin () doesn't restore the signals */
2191 save_stop_handler ();
2193 /* Must be done before init_subshell, to set up the terminal size: */
2194 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2195 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2197 load_setup ();
2199 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2201 isInitialized = mc_skin_init(&error);
2203 mc_filehighlight = mc_fhl_new (TRUE);
2205 dlg_set_default_colors ();
2207 if ( ! isInitialized ) {
2208 message (D_ERROR, _("Warning"), "%s", error->message);
2209 g_error_free(error);
2210 error = NULL;
2213 /* create home directory */
2214 /* do it after the screen library initialization to show the error message */
2215 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2216 canonicalize_pathname (mc_dir);
2217 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2218 && mkdir (mc_dir, 0700) != 0)
2219 message (D_ERROR, _("Warning"),
2220 _("Cannot create %s directory"), mc_dir);
2221 g_free (mc_dir);
2223 #ifdef HAVE_SUBSHELL_SUPPORT
2224 /* Done here to ensure that the subshell doesn't */
2225 /* inherit the file descriptors opened below, etc */
2226 if (use_subshell)
2227 init_subshell ();
2229 #endif /* HAVE_SUBSHELL_SUPPORT */
2231 /* Removing this from the X code let's us type C-c */
2232 load_key_defs ();
2234 load_keymap_defs ();
2236 /* Also done after init_subshell, to save any shell init file messages */
2237 if (console_flag)
2238 handle_console (CONSOLE_SAVE);
2240 if (alternate_plus_minus)
2241 application_keypad_mode ();
2243 #ifdef HAVE_SUBSHELL_SUPPORT
2244 if (use_subshell) {
2245 mc_prompt = strip_ctrl_codes (subshell_prompt);
2246 if (mc_prompt == NULL)
2247 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2248 } else
2249 #endif /* HAVE_SUBSHELL_SUPPORT */
2250 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2252 /* Program main loop */
2253 if (!midnight_shutdown)
2254 do_nc ();
2256 /* Save the tree store */
2257 tree_store_save ();
2259 free_keymap_defs ();
2261 /* Virtual File System shutdown */
2262 vfs_shut ();
2264 flush_extension_file (); /* does only free memory */
2266 mc_fhl_free (&mc_filehighlight);
2267 mc_skin_deinit ();
2269 tty_shutdown ();
2271 if (console_flag && !(quit & SUBSHELL_EXIT))
2272 handle_console (CONSOLE_RESTORE);
2273 if (alternate_plus_minus)
2274 numeric_keypad_mode ();
2276 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2278 if (console_flag)
2279 handle_console (CONSOLE_DONE);
2280 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2282 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string
2283 && !print_last_revert) {
2284 int last_wd_fd =
2285 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2286 S_IRUSR | S_IWUSR);
2288 if (last_wd_fd != -1) {
2289 ssize_t ret1;
2290 int ret2;
2291 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2292 ret2 = close (last_wd_fd);
2295 g_free (last_wd_string);
2297 g_free (mc_home_alt);
2298 g_free (mc_home);
2299 g_free (shell);
2301 done_key ();
2302 #ifdef HAVE_CHARSET
2303 free_codepages_list ();
2304 g_free (autodetect_codeset);
2305 #endif
2306 str_uninit_strings ();
2308 g_free (mc_run_param0);
2309 g_free (mc_run_param1);
2311 #ifdef USE_INTERNAL_EDIT
2312 edit_stack_free ();
2313 #endif
2315 return 0;