Ticket #1790: mc crashes on start
[midnight-commander.git] / src / main.c
blobb231aba8f6ef485b199cf6de184ff4c6687d8ad6
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
6 1994, 1995 Janne Kukonlehto
7 1997 Norbert Warmuth
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file main.c
24 * \brief Source: this is a main module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pwd.h> /* for username in xterm title */
42 #include "global.h"
44 #include "../src/tty/tty.h"
45 #include "../src/skin/skin.h"
46 #include "../src/tty/mouse.h"
47 #include "../src/tty/key.h" /* For init_key() */
48 #include "../src/tty/win.h" /* xterm_flag */
50 #include "../src/mcconfig/mcconfig.h"
51 #include "../src/args.h"
52 #include "../src/skin/skin.h"
53 #include "../src/filehighlight/fhl.h"
55 #include "dir.h"
56 #include "dialog.h"
57 #include "menu.h"
58 #include "panel.h"
59 #include "main.h"
60 #include "option.h"
61 #include "tree.h"
62 #include "treestore.h"
63 #include "cons.saver.h"
64 #include "subshell.h"
65 #include "setup.h" /* save_setup() */
66 #include "boxes.h" /* sort_box() */
67 #include "layout.h"
68 #include "cmd.h" /* Normal commands */
69 #include "hotlist.h"
70 #include "panelize.h"
71 #include "learn.h" /* learn_keys() */
72 #include "listmode.h"
73 #include "execute.h"
74 #include "ext.h" /* For flush_extension_file() */
75 #include "strutil.h"
76 /* Listbox for the command history feature */
77 #include "widget.h"
78 #include "command.h"
79 #include "wtools.h"
80 #include "cmddef.h" /* CK_ cmd name const */
81 #include "fileloc.h" /* MC_USERCONF_DIR */
83 #include "../vfs/vfs.h" /* vfs_translate_url() */
85 #include "chmod.h"
86 #include "chown.h"
87 #include "achown.h"
89 #ifdef WITH_SMBFS
90 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
91 #endif
93 #ifdef USE_INTERNAL_EDIT
94 # include "../edit/edit.h"
95 #endif
97 #ifdef HAVE_CHARSET
98 #include "charsets.h"
99 #endif /* HAVE_CHARSET */
101 #ifdef USE_VFS
102 #include "../vfs/gc.h"
103 #endif
105 #include "keybind.h" /* type global_keymap_t */
107 /* When the modes are active, left_panel, right_panel and tree_panel */
108 /* Point to a proper data structure. You should check with the functions */
109 /* get_current_type and get_other_type the types of the panels before using */
110 /* This pointer variables */
112 /* The structures for the panels */
113 WPanel *left_panel = NULL;
114 WPanel *right_panel = NULL;
116 mc_fhl_t *mc_filehighlight;
118 /* The pointer to the tree */
119 WTree *the_tree = NULL;
121 /* The Menubar */
122 struct WMenuBar *the_menubar = NULL;
124 /* Pointers to the selected and unselected panel */
125 WPanel *current_panel = NULL;
127 /* Set if the command is being run from the "Right" menu */
128 int is_right = 0;
130 /* Set when main loop should be terminated */
131 volatile int quit = 0;
133 /* Set if you want the possible completions dialog for the first time */
134 int show_all_if_ambiguous = 0;
136 /* Set when cd symlink following is desirable (bash mode) */
137 int cd_symlinks = 1;
139 /* If set then dialogs just clean the screen when refreshing, else */
140 /* they do a complete refresh, refreshing all the parts of the program */
141 int fast_refresh = 0;
143 /* If true, marking a files moves the cursor down */
144 int mark_moves_down = 1;
146 /* If true, at startup the user-menu is invoked */
147 int auto_menu = 0;
149 /* If true, then the +, - and \ keys have their special meaning only if the
150 * command line is emtpy, otherwise they behave like regular letters
152 int only_leading_plus_minus = 1;
154 int pause_after_run = pause_on_dumb_terminals;
156 /* It true saves the setup when quitting */
157 int auto_save_setup = 1;
159 #ifdef HAVE_CHARSET
161 * Don't restrict the output on the screen manager level,
162 * the translation tables take care of it.
164 #define full_eight_bits (1)
165 #define eight_bit_clean (1)
166 #else /* HAVE_CHARSET */
167 /* If true, allow characters in the range 160-255 */
168 int eight_bit_clean = 1;
170 * If true, also allow characters in the range 128-159.
171 * This is reported to break on many terminals (xterm, qansi-m).
173 int full_eight_bits = 0;
174 #endif /* !HAVE_CHARSET */
177 * If utf-8 terminal utf8_display = 1
178 * Display bits set UTF-8
181 int utf8_display = 0;
183 /* If true use the internal viewer */
184 int use_internal_view = 1;
186 /* Have we shown the fast-reload warning in the past? */
187 int fast_reload_w = 0;
189 /* Move page/item? When clicking on the top or bottom of a panel */
190 int mouse_move_pages = 1;
192 /* If true: l&r arrows are used to chdir if the input line is empty */
193 int navigate_with_arrows = 0;
195 /* If true program softkeys (HP terminals only) on startup and after every
196 command ran in the subshell to the description found in the termcap/terminfo
197 database */
198 int reset_hp_softkeys = 0;
200 /* The prompt */
201 const char *prompt = NULL;
203 /* The widget where we draw the prompt */
204 WLabel *the_prompt;
206 /* The hint bar */
207 WLabel *the_hint;
209 /* The button bar */
210 WButtonBar *the_bar;
212 /* Mouse type: GPM, xterm or none */
213 Mouse_Type use_mouse_p = MOUSE_NONE;
215 /* If on, default for "No" in delete operations */
216 int safe_delete = 0;
218 /* Controls screen clearing before an exec */
219 int clear_before_exec = 1;
221 /* Asks for confirmation before deleting a file */
222 int confirm_delete = 1;
224 /* Asks for confirmation before deleting a hotlist entry */
225 int confirm_directory_hotlist_delete = 1;
227 /* Asks for confirmation before overwriting a file */
228 int confirm_overwrite = 1;
230 /* Asks for confirmation before executing a program by pressing enter */
231 int confirm_execute = 0;
233 /* Asks for confirmation before leaving the program */
234 int confirm_exit = 1;
236 /* Asks for confirmation when using F3 to view a directory and there
237 are tagged files */
238 int confirm_view_dir = 0;
240 /* This flag indicates if the pull down menus by default drop down */
241 int drop_menus = 0;
243 /* if skip_check_codeset = 1 do not show warning about
244 * system and display codeset is different
246 int skip_check_codeset = 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;
320 const global_keymap_t *main_map;
321 const global_keymap_t *main_x_map;
323 /* Save current stat of directories to avoid reloading the panels */
324 /* when no modifications have taken place */
325 void
326 save_cwds_stat (void)
328 if (fast_reload) {
329 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
330 if (get_other_type () == view_listing)
331 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
335 #ifdef HAVE_SUBSHELL_SUPPORT
336 void
337 do_update_prompt (void)
339 if (update_prompt) {
340 printf ("\r\n%s", subshell_prompt);
341 fflush (stdout);
342 update_prompt = 0;
345 #endif /* HAVE_SUBSHELL_SUPPORT */
347 void
348 change_panel (void)
350 free_completions (cmdline);
351 dlg_one_down (midnight_dlg);
354 /* Stop MC main dialog and the current dialog if it exists.
355 * Needed to provide fast exit from MC viewer or editor on shell exit */
356 static void
357 stop_dialogs (void)
359 midnight_dlg->running = 0;
360 if (current_dlg) {
361 current_dlg->running = 0;
365 static int
366 quit_cmd_internal (int quiet)
368 int q = quit;
370 if (quiet || !confirm_exit) {
371 q = 1;
372 } else {
373 if (query_dialog
374 (_(" The Midnight Commander "),
375 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
376 2, _("&Yes"), _("&No")) == 0)
377 q = 1;
379 if (q) {
380 #ifdef HAVE_SUBSHELL_SUPPORT
381 if (!use_subshell)
382 stop_dialogs ();
383 else if ((q = exit_subshell ()))
384 #endif
385 stop_dialogs ();
387 if (q)
388 quit |= 1;
389 return quit;
392 static void
393 quit_cmd (void)
395 quit_cmd_internal (0);
398 void
399 quiet_quit_cmd (void)
401 print_last_revert = 1;
402 quit_cmd_internal (1);
405 /* Wrapper for do_subshell_chdir, check for availability of subshell */
406 void
407 subshell_chdir (const char *directory)
409 #ifdef HAVE_SUBSHELL_SUPPORT
410 if (use_subshell) {
411 if (vfs_current_is_local ())
412 do_subshell_chdir (directory, 0, 1);
414 #endif /* HAVE_SUBSHELL_SUPPORT */
417 void
418 directory_history_add (struct WPanel *panel, const char *dir)
420 char *tmp;
422 tmp = g_strdup (dir);
423 strip_password (tmp, 1);
425 panel->dir_history = list_append_unique (panel->dir_history, tmp);
429 * If we moved to the parent directory move the selection pointer to
430 * the old directory name; If we leave VFS dir, remove FS specificator.
432 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
434 static const char *
435 get_parent_dir_name (const char *cwd, const char *lwd)
437 const char *p;
438 if (strlen (lwd) > strlen (cwd))
439 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
440 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
441 cwd[1] == '\0'))) {
442 return (p + 1);
444 return NULL;
448 * Changes the current directory of the panel.
449 * Don't record change in the directory history.
451 static int
452 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
454 const char *directory;
455 char *olddir;
456 char temp[MC_MAXPATHLEN];
457 char *translated_url;
459 if (cd_type == cd_parse_command) {
460 while (*new_dir == ' ')
461 new_dir++;
464 olddir = g_strdup (panel->cwd);
465 new_dir = translated_url = vfs_translate_url (new_dir);
467 /* Convert *new_path to a suitable pathname, handle ~user */
469 if (cd_type == cd_parse_command) {
470 if (!strcmp (new_dir, "-")) {
471 strcpy (temp, panel->lwd);
472 new_dir = temp;
475 directory = *new_dir ? new_dir : home_dir;
477 if (mc_chdir (directory) == -1) {
478 strcpy (panel->cwd, olddir);
479 g_free (olddir);
480 g_free (translated_url);
481 return 0;
483 g_free (translated_url);
485 /* Success: save previous directory, shutdown status of previous dir */
486 strcpy (panel->lwd, olddir);
487 free_completions (cmdline);
489 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
491 vfs_release_path (olddir);
493 subshell_chdir (panel->cwd);
495 /* Reload current panel */
496 panel_clean_dir (panel);
497 panel->count =
498 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
499 panel->reverse, panel->case_sensitive,
500 panel->exec_first, panel->filter);
501 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
502 load_hint (0);
503 panel->dirty = 1;
504 update_xterm_title_path ();
506 g_free (olddir);
508 return 1;
512 * Changes the current directory of the panel.
513 * Record change in the directory history.
516 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
518 int r;
520 r = _do_panel_cd (panel, new_dir, cd_type);
521 if (r)
522 directory_history_add (panel, panel->cwd);
523 return r;
527 do_cd (const char *new_dir, enum cd_enum exact)
529 return (do_panel_cd (current_panel, new_dir, exact));
532 void
533 directory_history_next (WPanel *panel)
535 GList *nextdir;
537 nextdir = g_list_next (panel->dir_history);
539 if (!nextdir)
540 return;
542 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
543 panel->dir_history = nextdir;
546 void
547 directory_history_prev (WPanel *panel)
549 GList *prevdir;
551 prevdir = g_list_previous (panel->dir_history);
553 if (!prevdir)
554 return;
556 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
557 panel->dir_history = prevdir;
560 void
561 directory_history_list (WPanel *panel)
563 char *s;
565 if (!panel->dir_history)
566 return;
568 s = show_hist (panel->dir_history, &panel->widget);
570 if (!s)
571 return;
573 if (_do_panel_cd (panel, s, cd_exact))
574 directory_history_add (panel, panel->cwd);
575 else
576 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
577 g_free (s);
580 #ifdef HAVE_SUBSHELL_SUPPORT
582 load_prompt (int fd, void *unused)
584 (void) fd;
585 (void) unused;
587 if (!read_subshell_prompt ())
588 return 0;
590 /* Don't actually change the prompt if it's invisible */
591 if (current_dlg == midnight_dlg && command_prompt) {
592 char *tmp_prompt;
593 int prompt_len;
595 tmp_prompt = strip_ctrl_codes (subshell_prompt);
596 prompt_len = str_term_width1 (tmp_prompt);
598 /* Check for prompts too big */
599 if (COLS > 8 && prompt_len > COLS - 8) {
600 tmp_prompt[COLS - 8] = '\0';
601 prompt_len = COLS - 8;
603 prompt = tmp_prompt;
604 label_set_text (the_prompt, prompt);
605 winput_set_origin ((WInput *) cmdline, prompt_len,
606 COLS - prompt_len);
608 /* since the prompt has changed, and we are called from one of the
609 * tty_get_event channels, the prompt updating does not take place
610 * automatically: force a cursor update and a screen refresh
612 update_cursor (midnight_dlg);
613 mc_refresh ();
615 update_prompt = 1;
616 return 0;
618 #endif /* HAVE_SUBSHELL_SUPPORT */
620 void
621 sort_cmd (void)
623 WPanel *p;
624 const panel_field_t *sort_order;
626 if (!SELECTED_IS_PANEL)
627 return;
629 p = MENU_PANEL;
630 sort_order = sort_box (p->current_sort_field, &p->reverse,
631 &p->case_sensitive,
632 &p->exec_first);
634 panel_set_sort_order (p, sort_order);
638 static void
639 treebox_cmd (void)
641 char *sel_dir;
643 sel_dir = tree_box (selection (current_panel)->fname);
644 if (sel_dir) {
645 do_cd (sel_dir, cd_exact);
646 g_free (sel_dir);
650 #ifdef LISTMODE_EDITOR
651 static void
652 listmode_cmd (void)
654 char *newmode;
656 if (get_current_type () != view_listing)
657 return;
659 newmode = listmode_edit (current_panel->user_format);
660 if (!newmode)
661 return;
663 g_free (current_panel->user_format);
664 current_panel->list_type = list_user;
665 current_panel->user_format = newmode;
666 set_panel_formats (current_panel);
668 do_refresh ();
670 #endif /* LISTMODE_EDITOR */
672 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
673 static GList *
674 create_panel_menu (void)
676 GList *entries = NULL;
678 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
679 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_MenuQuickViewCmd));
680 entries = g_list_append (entries, menu_entry_create (_("&Info" ), CK_MenuInfoCmd));
681 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
682 entries = g_list_append (entries, menu_separator_create ());
683 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
684 entries = g_list_append (entries, menu_separator_create ());
685 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
686 #ifdef HAVE_CHARSET
687 entries = g_list_append (entries, menu_separator_create ());
688 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
689 #endif
690 #ifdef USE_NETCODE
691 entries = g_list_append (entries, menu_separator_create ());
692 #ifdef ENABLE_VFS_MCFS
693 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
694 #endif
695 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
696 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
697 #ifdef WITH_SMBFS
698 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
699 #endif
700 #endif
701 entries = g_list_append (entries, menu_separator_create ());
702 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
704 return entries;
707 static GList *
708 create_file_menu (void)
710 GList *entries = NULL;
712 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
713 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
714 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
715 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
716 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
717 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
718 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
719 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
720 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
721 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
722 entries = g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
723 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
724 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
725 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
726 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
727 entries = g_list_append (entries, menu_separator_create ());
728 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
729 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
730 entries = g_list_append (entries, menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
731 entries = g_list_append (entries, menu_separator_create ());
732 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
734 return entries;
737 static GList *
738 create_command_menu (void)
740 /* I know, I'm lazy, but the tree widget when it's not running
741 * as a panel still has some problems, I have not yet finished
742 * the WTree widget port, sorry.
744 GList *entries = NULL;
746 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
747 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
748 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
749 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
750 entries = g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
751 entries = g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
752 entries = g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
753 entries = g_list_append (entries, menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
754 entries = g_list_append (entries, menu_separator_create ());
755 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
756 entries = g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
757 #ifdef USE_VFS
758 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
759 #endif
760 #ifdef WITH_BACKGROUND
761 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
762 #endif
763 entries = g_list_append (entries, menu_separator_create ());
764 #ifdef USE_EXT2FSLIB
765 entries = g_list_append (entries, menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
766 #endif
767 #ifdef LISTMODE_EDITOR
768 entries = g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
769 #endif
770 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
771 entries = g_list_append (entries, menu_separator_create ());
772 #endif
773 entries = g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
774 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
775 entries = g_list_append (entries, menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
777 return entries;
780 static GList *
781 create_options_menu (void)
783 GList *entries = NULL;
785 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
786 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
787 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
788 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
789 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
790 #ifdef USE_VFS
791 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
792 #endif
793 entries = g_list_append (entries, menu_separator_create ());
794 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
796 return entries;
799 void
800 init_menu (void)
802 menubar_add_menu (the_menubar,
803 create_menu (horizontal_split ? _("&Above") : _("&Left"),
804 create_panel_menu (), "[Left and Right Menus]"));
805 menubar_add_menu (the_menubar,
806 create_menu (_("&File"), create_file_menu (), "[File Menu]"));
807 menubar_add_menu (the_menubar,
808 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
809 menubar_add_menu (the_menubar,
810 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
811 menubar_add_menu (the_menubar,
812 create_menu (horizontal_split ? _("&Below") : _("&Right"),
813 create_panel_menu (), "[Left and Right Menus]"));
816 void
817 done_menu (void)
819 menubar_set_menu (the_menubar, NULL);
822 static void
823 menu_last_selected_cmd (void)
825 the_menubar->is_active = TRUE;
826 the_menubar->is_dropped = (drop_menus != 0);
827 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
828 dlg_select_widget (the_menubar);
831 static void
832 menu_cmd (void)
834 if (the_menubar->is_active)
835 return;
837 if ((get_current_index () == 0) == (current_panel->active != 0))
838 the_menubar->selected = 0;
839 else
840 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
841 menu_last_selected_cmd ();
844 static char *
845 midnight_get_shortcut (int command)
847 const char *ext_map;
848 const char *shortcut = NULL;
850 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
852 if (ext_map != NULL)
853 shortcut = lookup_keymap_shortcut (main_x_map, command);
854 if (shortcut != NULL)
855 return g_strdup_printf ("%s %s", ext_map, shortcut);
857 shortcut = lookup_keymap_shortcut (main_map, command);
858 if (shortcut != NULL)
859 return g_strdup (shortcut);
861 shortcut = lookup_keymap_shortcut (panel_map, command);
862 if (shortcut != NULL)
863 return g_strdup (shortcut);
865 return NULL;
868 /* Flag toggling functions */
869 void
870 toggle_fast_reload (void)
872 fast_reload = !fast_reload;
873 if (fast_reload_w == 0 && fast_reload) {
874 message (D_NORMAL, _(" Information "),
876 (" Using the fast reload option may not reflect the exact \n"
877 " directory contents. In this case you'll need to do a \n"
878 " manual reload of the directory. See the man page for \n"
879 " the details. "));
880 fast_reload_w = 1;
884 void
885 toggle_mix_all_files (void)
887 mix_all_files = !mix_all_files;
888 update_panels (UP_RELOAD, UP_KEEPSEL);
891 void
892 toggle_show_backup (void)
894 show_backups = !show_backups;
895 update_panels (UP_RELOAD, UP_KEEPSEL);
898 void
899 toggle_show_hidden (void)
901 show_dot_files = !show_dot_files;
902 update_panels (UP_RELOAD, UP_KEEPSEL);
905 void
906 toggle_kilobyte_si (void)
908 kilobyte_si = !kilobyte_si;
909 update_panels (UP_RELOAD, UP_KEEPSEL);
913 * Just a hack for allowing url-like pathnames to be accepted from the
914 * command line.
916 static void
917 translated_mc_chdir (char *dir)
919 char *newdir;
921 newdir = vfs_translate_url (dir);
922 mc_chdir (newdir);
923 g_free (newdir);
926 static void
927 create_panels (void)
929 int current_index;
930 int other_index;
931 int current_mode;
932 int other_mode;
933 char original_dir[1024];
935 original_dir[0] = 0;
937 if (boot_current_is_left) {
938 current_index = 0;
939 other_index = 1;
940 current_mode = startup_left_mode;
941 other_mode = startup_right_mode;
942 } else {
943 current_index = 1;
944 other_index = 0;
945 current_mode = startup_right_mode;
946 other_mode = startup_left_mode;
948 /* Creates the left panel */
949 if (this_dir) {
950 if (other_dir) {
951 /* Ok, user has specified two dirs, save the original one,
952 * since we may not be able to chdir to the proper
953 * second directory later
955 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
957 translated_mc_chdir (this_dir);
959 set_display_type (current_index, current_mode);
961 /* The other panel */
962 if (other_dir) {
963 if (original_dir[0])
964 translated_mc_chdir (original_dir);
965 translated_mc_chdir (other_dir);
967 set_display_type (other_index, other_mode);
969 if (startup_left_mode == view_listing) {
970 current_panel = left_panel;
971 } else {
972 if (right_panel)
973 current_panel = right_panel;
974 else
975 current_panel = left_panel;
978 /* Create the nice widgets */
979 cmdline = command_new (0, 0, 0);
980 the_prompt = label_new (0, 0, prompt);
981 the_prompt->transparent = 1;
982 the_bar = buttonbar_new (keybar_visible);
984 the_hint = label_new (0, 0, 0);
985 the_hint->transparent = 1;
986 the_hint->auto_adjust_cols = 0;
987 the_hint->widget.cols = COLS;
989 the_menubar = menubar_new (0, 0, COLS, NULL);
992 static void
993 copy_current_pathname (void)
995 char *cwd_path;
996 if (!command_prompt)
997 return;
999 cwd_path = remove_encoding_from_path (current_panel->cwd);
1000 command_insert (cmdline, cwd_path, 0);
1002 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1003 command_insert (cmdline, PATH_SEP_STR, 0);
1004 g_free (cwd_path);
1007 static void
1008 copy_other_pathname (void)
1010 char *cwd_path;
1012 if (get_other_type () != view_listing)
1013 return;
1015 if (!command_prompt)
1016 return;
1018 cwd_path = remove_encoding_from_path (other_panel->cwd);
1019 command_insert (cmdline, cwd_path, 0);
1021 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1022 command_insert (cmdline, PATH_SEP_STR, 0);
1023 g_free (cwd_path);
1026 static void
1027 copy_readlink (WPanel *panel)
1029 if (!command_prompt)
1030 return;
1031 if (S_ISLNK (selection (panel)->st.st_mode)) {
1032 char buffer[MC_MAXPATHLEN];
1033 char *p =
1034 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1035 int i;
1037 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1038 g_free (p);
1039 if (i > 0) {
1040 buffer[i] = 0;
1041 command_insert (cmdline, buffer, 1);
1046 static void
1047 copy_current_readlink (void)
1049 copy_readlink (current_panel);
1052 static void
1053 copy_other_readlink (void)
1055 if (get_other_type () != view_listing)
1056 return;
1057 copy_readlink (other_panel);
1060 /* Insert the selected file name into the input line */
1061 static void
1062 copy_prog_name (void)
1064 char *tmp;
1065 if (!command_prompt)
1066 return;
1068 if (get_current_type () == view_tree) {
1069 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1070 tmp = tree_selected_name (tree);
1071 } else
1072 tmp = selection (current_panel)->fname;
1074 command_insert (cmdline, tmp, 1);
1077 static void
1078 copy_tagged (WPanel *panel)
1080 int i;
1082 if (!command_prompt)
1083 return;
1084 input_disable_update (cmdline);
1085 if (panel->marked) {
1086 for (i = 0; i < panel->count; i++) {
1087 if (panel->dir.list[i].f.marked)
1088 command_insert (cmdline, panel->dir.list[i].fname, 1);
1090 } else {
1091 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1094 input_enable_update (cmdline);
1097 static void
1098 copy_current_tagged (void)
1100 copy_tagged (current_panel);
1103 static void
1104 copy_other_tagged (void)
1106 if (get_other_type () != view_listing)
1107 return;
1108 copy_tagged (other_panel);
1111 static void
1112 init_labels (void)
1114 buttonbar_set_label (midnight_dlg, 1, Q_("ButtonBar|Help"), help_cmd);
1115 buttonbar_set_label (midnight_dlg, 2, Q_("ButtonBar|Menu"), user_file_menu_cmd);
1116 buttonbar_set_label (midnight_dlg, 9, Q_("ButtonBar|PullDn"), menu_cmd);
1117 buttonbar_set_label (midnight_dlg, 10, Q_("ButtonBar|Quit"), quit_cmd);
1120 static int ctl_x_map_enabled = 0;
1122 static void
1123 ctl_x_cmd (void)
1125 ctl_x_map_enabled = 1;
1128 static cb_ret_t
1129 midnight_execute_cmd (int command)
1131 cb_ret_t res = MSG_HANDLED;
1133 switch (command) {
1134 case CK_AddHotlist:
1135 add2hotlist_cmd ();
1136 break;
1137 case CK_ChmodCmd:
1138 chmod_cmd ();
1139 break;
1140 case CK_ChownCmd:
1141 chown_cmd ();
1142 break;
1143 case CK_ChownAdvancedCmd:
1144 chown_advanced_cmd ();
1145 break;
1146 case CK_CompareDirsCmd:
1147 compare_dirs_cmd ();
1148 break;
1149 case CK_ConfigureBox:
1150 configure_box ();
1151 break;
1152 #ifdef USE_VFS
1153 case CK_ConfigureVfs:
1154 configure_vfs ();
1155 break;
1156 #endif
1157 case CK_ConfirmBox:
1158 confirm_box ();
1159 break;
1160 case CK_CopyCmd:
1161 copy_cmd ();
1162 break;
1163 case CK_CopyCurrentPathname:
1164 copy_current_pathname ();
1165 break;
1166 case CK_CopyCurrentReadlink:
1167 copy_current_readlink ();
1168 break;
1169 case CK_CopyCurrentTagged:
1170 copy_current_tagged ();
1171 break;
1172 case CK_CopyOtherPathname:
1173 copy_other_pathname ();
1174 break;
1175 case CK_CopyOtherReadlink:
1176 copy_other_readlink ();
1177 break;
1178 case CK_CopyOtherTagged:
1179 copy_other_tagged ();
1180 break;
1181 case CK_DeleteCmd:
1182 delete_cmd ();
1183 break;
1184 case CK_DisplayBitsBox:
1185 display_bits_box ();
1186 break;
1187 case CK_EditCmd:
1188 edit_cmd ();
1189 break;
1190 case CK_EditExtFileCmd:
1191 ext_cmd ();
1192 break;
1193 case CK_EditFhlFileCmd:
1194 edit_fhl_cmd ();
1195 break;
1196 case CK_EditMcMenuCmd:
1197 edit_mc_menu_cmd ();
1198 break;
1199 case CK_EditSymlinkCmd:
1200 edit_symlink_cmd ();
1201 break;
1202 case CK_ExternalPanelize:
1203 external_panelize ();
1204 break;
1205 case CK_FilterCmd:
1206 filter_cmd ();
1207 break;
1208 case CK_FilteredViewCmd:
1209 filtered_view_cmd ();
1210 break;
1211 case CK_FindCmd:
1212 find_cmd ();
1213 break;
1214 #if defined (USE_NETCODE)
1215 case CK_FishlinkCmd:
1216 fishlink_cmd ();
1217 break;
1218 case CK_FtplinkCmd:
1219 ftplink_cmd ();
1220 break;
1221 #endif
1222 case CK_HistoryCmd:
1223 history_cmd ();
1224 break;
1225 case CK_InfoCmd:
1226 info_cmd_no_menu ();
1227 break;
1228 #ifdef WITH_BACKGROUND
1229 case CK_JobsCmd:
1230 jobs_cmd ();
1231 break;
1232 #endif
1233 case CK_LayoutCmd:
1234 layout_cmd ();
1235 break;
1236 case CK_LearnKeys:
1237 learn_keys ();
1238 break;
1239 case CK_LinkCmd:
1240 link_cmd ();
1241 break;
1242 case CK_ListingCmd:
1243 listing_cmd ();
1244 break;
1245 #ifdef LISTMODE_EDITOR
1246 case CK_ListmodeCmd:
1247 listmode_cmd ();
1248 break;
1249 #endif
1250 case CK_MenuInfoCmd:
1251 info_cmd ();
1252 break;
1253 case CK_MenuLastSelectedCmd:
1254 menu_last_selected_cmd ();
1255 break;
1256 case CK_MenuQuickViewCmd:
1257 quick_view_cmd ();
1258 break;
1259 case CK_MkdirCmd:
1260 mkdir_cmd ();
1261 break;
1262 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1263 case CK_NetlinkCmd:
1264 netlink_cmd ();
1265 break;
1266 #endif
1267 #ifdef HAVE_CHARSET
1268 case CK_PanelSetPanelEncoding:
1269 encoding_cmd ();
1270 break;
1271 #endif
1272 case CK_QuickCdCmd:
1273 quick_cd_cmd ();
1274 break;
1275 case CK_QuickChdirCmd:
1276 quick_chdir_cmd ();
1277 break;
1278 case CK_QuickViewCmd:
1279 quick_cmd_no_menu ();
1280 break;
1281 case CK_QuietQuitCmd:
1282 quiet_quit_cmd ();
1283 break;
1284 case CK_QuitCmd:
1285 quit_cmd ();
1286 break;
1287 case CK_RenameCmd:
1288 rename_cmd ();
1289 break;
1290 case CK_RereadCmd:
1291 reread_cmd ();
1292 break;
1293 #ifdef USE_VFS
1294 case CK_ReselectVfs:
1295 reselect_vfs ();
1296 break;
1297 #endif
1298 case CK_ReverseSelectionCmd:
1299 reverse_selection_cmd ();
1300 break;
1301 case CK_SaveSetupCmd:
1302 save_setup_cmd ();
1303 break;
1304 case CK_SelectCmd:
1305 select_cmd ();
1306 break;
1307 case CK_ShowCommandLine:
1308 view_other_cmd ();
1309 break;
1310 case CK_SingleDirsizeCmd:
1311 smart_dirsize_cmd ();
1312 break;
1313 #if defined (USE_NETCODE) && defined (WITH_SMBFS)
1314 case CK_SmblinkCmd:
1315 smblink_cmd ();
1316 break;
1317 #endif
1318 case CK_Sort:
1319 sort_cmd ();
1320 break;
1321 case CK_StartExtMap1:
1322 ctl_x_cmd ();
1323 break;
1324 case CK_SuspendCmd:
1325 suspend_cmd ();
1326 break;
1327 case CK_SwapCmd:
1328 swap_cmd ();
1329 break;
1330 case CK_SymlinkCmd:
1331 symlink_cmd ();
1332 break;
1333 case CK_ToggleListingCmd:
1334 toggle_listing_cmd ();
1335 break;
1336 case CK_ToggleShowHidden:
1337 toggle_show_hidden ();
1338 break;
1339 case CK_TreeCmd:
1340 tree_cmd ();
1341 break;
1342 case CK_TreeBoxCmd:
1343 treebox_cmd ();
1344 break;
1345 #ifdef USE_EXT2FSLIB
1346 case CK_UndeleteCmd:
1347 undelete_cmd ();
1348 break;
1349 #endif
1350 case CK_UnselectCmd:
1351 unselect_cmd ();
1352 break;
1353 case CK_UserMenuCmd:
1354 user_file_menu_cmd ();
1355 break;
1356 case CK_ViewCmd:
1357 view_cmd ();
1358 break;
1359 case CK_ViewFileCmd:
1360 view_file_cmd ();
1361 break;
1362 default:
1363 res = MSG_NOT_HANDLED;
1366 return res;
1369 static void
1370 setup_pre (void)
1372 /* Call all the inits */
1374 #ifdef HAVE_SLANG
1375 tty_display_8bit (full_eight_bits != 0);
1376 #else
1377 tty_display_8bit (eight_bit_clean != 0);
1378 #endif
1381 static void
1382 init_xterm_support (void)
1384 const char *termvalue;
1386 termvalue = getenv ("TERM");
1387 if (!termvalue || !(*termvalue)) {
1388 fputs (_("The TERM environment variable is unset!\n"), stderr);
1389 exit (1);
1392 /* Check mouse capabilities */
1393 xmouse_seq = tty_tgetstr ("Km");
1395 if (strcmp (termvalue, "cygwin") == 0) {
1396 mc_args__force_xterm = 1;
1397 use_mouse_p = MOUSE_DISABLED;
1400 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1401 || strncmp (termvalue, "konsole", 7) == 0
1402 || strncmp (termvalue, "rxvt", 4) == 0
1403 || strcmp (termvalue, "Eterm") == 0
1404 || strcmp (termvalue, "dtterm") == 0) {
1405 xterm_flag = 1;
1407 /* Default to the standard xterm sequence */
1408 if (!xmouse_seq) {
1409 xmouse_seq = ESC_STR "[M";
1412 /* Enable mouse unless explicitly disabled by --nomouse */
1413 if (use_mouse_p != MOUSE_DISABLED) {
1414 const char *color_term = getenv ("COLORTERM");
1415 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1416 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1417 strcmp (termvalue, "Eterm") == 0) {
1418 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1419 } else {
1420 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1426 static void
1427 setup_mc (void)
1429 setup_pre ();
1430 create_panels ();
1431 setup_panels ();
1433 #ifdef HAVE_SUBSHELL_SUPPORT
1434 if (use_subshell)
1435 add_select_channel (subshell_pty, load_prompt, 0);
1436 #endif /* !HAVE_SUBSHELL_SUPPORT */
1438 tty_setup_sigwinch (sigwinch_handler);
1440 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1442 init_xterm_support ();
1443 init_mouse ();
1446 static void
1447 setup_dummy_mc (void)
1449 char d[MC_MAXPATHLEN];
1451 mc_get_current_wd (d, MC_MAXPATHLEN);
1452 setup_mc ();
1453 mc_chdir (d);
1456 static void check_codeset()
1458 const char *_system_codepage = NULL;
1459 #ifdef HAVE_CHARSET
1460 const char *_source_codepage = NULL;
1461 const char *_display_codepage = NULL;
1462 int profile_changed = 0;
1464 #define CONFY 16
1465 #define CONFX 54
1467 if ( !skip_check_codeset ) {
1469 QuickWidget ecs_widgets [] = {
1470 QUICK_BUTTON (4, 6, 13, CONFY, N_("&Skip"), B_EXIT, NULL),
1471 QUICK_BUTTON (1, 11, 13, CONFY, N_("&Fix it"), B_ENTER, NULL),
1472 QUICK_CHECKBOX (1, 13, 11, CONFY, N_("don't ask again"), &skip_check_codeset),
1473 QUICK_LABEL (2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
1474 "or source codeset (in mcedit ctrl-t) \n"
1475 "does not match one set via locale. \n"
1476 "Set correct codeset manually or press <<Fix it>> \n"
1477 "to set locale default.\n\n"
1478 "Or set \'don't ask again\' and press <<Skip>>")),
1479 QUICK_END
1482 QuickDialog ecs =
1484 CONFX, CONFY, -1, -1, N_(" Confirmation "),
1485 "[Confirmation]", ecs_widgets, FALSE
1489 _system_codepage = str_detect_termencoding();
1490 _source_codepage = get_codepage_id (source_codepage);
1491 _display_codepage = get_codepage_id (display_codepage);
1492 if ( (strcmp (_system_codepage, _display_codepage)) ||
1493 (strcmp (_system_codepage, _source_codepage)) ) {
1494 if (quick_dialog (&ecs) == B_ENTER){
1495 display_codepage = get_codepage_index (_system_codepage);
1496 cp_display = get_codepage_id (display_codepage);
1497 if ( !strcmp (cp_display, _system_codepage)) {
1498 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1499 mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
1500 display_codepage = get_codepage_index ( cp_display );
1501 utf8_display = str_isutf8 (_system_codepage);
1502 source_codepage = display_codepage;
1503 cp_source = cp_display;
1504 profile_changed = 1;
1505 } else {
1506 utf8_display = str_isutf8 (_system_codepage);
1507 cp_source = cp_display = _system_codepage;
1509 } else {
1510 if ( skip_check_codeset ) {
1511 mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
1512 profile_changed = 1;
1516 if ( profile_changed )
1517 save_configure ();
1519 #else /* HAVE_CHARSET */
1520 _system_codepage = str_detect_termencoding();
1521 utf8_display = str_isutf8 (_system_codepage);
1522 #endif /* HAVE_CHARSET */
1525 static void
1526 done_screen (void)
1528 if (!(quit & SUBSHELL_EXIT))
1529 clr_scr ();
1530 tty_reset_shell_mode ();
1531 tty_noraw_mode ();
1532 tty_keypad (FALSE);
1533 tty_colors_done ();
1536 static void
1537 done_mc (void)
1539 disable_mouse ();
1541 done_menu ();
1543 /* Setup shutdown
1545 * We sync the profiles since the hotlist may have changed, while
1546 * we only change the setup data if we have the auto save feature set
1549 if (auto_save_setup)
1550 save_setup (); /* does also call save_hotlist */
1551 else {
1552 save_hotlist ();
1553 save_panel_types ();
1555 done_screen ();
1556 vfs_add_current_stamps ();
1559 /* This should be called after destroy_dlg since panel widgets
1560 * save their state on the profiles
1562 static void
1563 done_mc_profile (void)
1565 done_setup ();
1568 static cb_ret_t
1569 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1571 int i;
1572 switch (msg) {
1574 case DLG_IDLE:
1575 /* We only need the first idle event */
1576 set_idle_proc (h, 0);
1577 if (auto_menu) {
1578 user_file_menu_cmd ();
1580 return MSG_HANDLED;
1582 case DLG_KEY:
1583 if (ctl_x_map_enabled) {
1584 ctl_x_map_enabled = 0;
1585 for (i = 0; main_x_map[i].key; i++)
1586 if (parm == main_x_map[i].key)
1587 return midnight_execute_cmd (main_x_map[i].command);
1590 /* FIXME: should handle all menu shortcuts before this point */
1591 if (the_menubar->is_active)
1592 return MSG_NOT_HANDLED;
1594 if (parm == KEY_F (10)) {
1595 quit_cmd ();
1596 return MSG_HANDLED;
1599 if (parm == '\t')
1600 free_completions (cmdline);
1602 if (parm == '\n') {
1603 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1604 cmdline->buffer[i] == '\t'); i++);
1605 if (cmdline->buffer[i]) {
1606 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1607 return MSG_HANDLED;
1609 stuff (cmdline, "", 0);
1610 cmdline->point = 0;
1613 /* Ctrl-Enter and Alt-Enter */
1614 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1615 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1616 copy_prog_name ();
1617 return MSG_HANDLED;
1620 /* Ctrl-Shift-Enter */
1621 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1622 copy_current_pathname ();
1623 copy_prog_name ();
1624 return MSG_HANDLED;
1627 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1628 && !quote && !current_panel->searching) {
1629 if (!only_leading_plus_minus) {
1630 /* Special treatement, since the input line will eat them */
1631 if (parm == '+') {
1632 select_cmd ();
1633 return MSG_HANDLED;
1636 if (parm == '\\' || parm == '-') {
1637 unselect_cmd ();
1638 return MSG_HANDLED;
1641 if (parm == '*') {
1642 reverse_selection_cmd ();
1643 return MSG_HANDLED;
1645 } else if (!command_prompt || !cmdline->buffer[0]) {
1646 /* Special treatement '+', '-', '\', '*' only when this is
1647 * first char on input line
1650 if (parm == '+') {
1651 select_cmd ();
1652 return MSG_HANDLED;
1655 if (parm == '\\' || parm == '-') {
1656 unselect_cmd ();
1657 return MSG_HANDLED;
1660 if (parm == '*') {
1661 reverse_selection_cmd ();
1662 return MSG_HANDLED;
1666 return MSG_NOT_HANDLED;
1668 case DLG_HOTKEY_HANDLED:
1669 if ((get_current_type () == view_listing) && current_panel->searching) {
1670 current_panel->searching = 0;
1671 current_panel->dirty = 1;
1673 return MSG_HANDLED;
1675 case DLG_UNHANDLED_KEY:
1676 if (command_prompt) {
1677 cb_ret_t v;
1679 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1680 if (v == MSG_HANDLED)
1681 return MSG_HANDLED;
1683 if (ctl_x_map_enabled) {
1684 ctl_x_map_enabled = 0;
1685 for (i = 0; main_x_map[i].key; i++)
1686 if (parm == main_x_map[i].key)
1687 return midnight_execute_cmd (main_x_map[i].command);
1688 } else {
1689 for (i = 0; main_map[i].key; i++) {
1690 if (parm == main_map[i].key)
1691 return midnight_execute_cmd (main_map[i].command);
1694 return MSG_NOT_HANDLED;
1696 case DLG_DRAW:
1697 load_hint (1);
1698 /* We handle the special case of the output lines */
1699 if (console_flag && output_lines)
1700 show_console_contents (output_start_y,
1701 LINES - output_lines - keybar_visible -
1702 1, LINES - keybar_visible - 1);
1703 return MSG_HANDLED;
1705 case DLG_POST_KEY:
1706 if (!the_menubar->is_active)
1707 update_dirty_panels ();
1708 return MSG_HANDLED;
1710 default:
1711 return default_dlg_callback (h, msg, parm);
1715 /* Show current directory in the xterm title */
1716 void
1717 update_xterm_title_path (void)
1719 const char *path;
1720 char host[BUF_TINY];
1721 char *p;
1722 struct passwd *pw = NULL;
1723 char *login = NULL;
1724 int res = 0;
1725 if (xterm_flag && xterm_title) {
1726 path = strip_home_and_password (current_panel->cwd);
1727 res = gethostname(host, sizeof (host));
1728 if ( res ) { /* On success, res = 0 */
1729 host[0] = '\0';
1730 } else {
1731 host[sizeof (host) - 1] = '\0';
1733 pw = getpwuid(getuid());
1734 if ( pw ) {
1735 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1736 } else {
1737 login = g_strdup (host);
1739 p = g_strdup_printf ("mc [%s]:%s", login, path);
1740 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1741 g_free (login);
1742 g_free (p);
1743 if (!alternate_plus_minus)
1744 numeric_keypad_mode ();
1745 fflush (stdout);
1750 * Load new hint and display it.
1751 * IF force is not 0, ignore the timeout.
1753 void
1754 load_hint (int force)
1756 char *hint;
1758 if (!the_hint->widget.parent)
1759 return;
1761 if (!message_visible) {
1762 label_set_text (the_hint, NULL);
1763 return;
1766 hint = get_random_hint (force);
1768 if (hint != NULL) {
1769 if (*hint)
1770 set_hintbar (hint);
1771 g_free (hint);
1772 } else {
1773 char text[BUF_SMALL];
1775 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1776 VERSION);
1777 set_hintbar (text);
1781 static void
1782 setup_panels_and_run_mc (void)
1784 midnight_dlg->menu_executor = midnight_execute_cmd;
1785 midnight_dlg->get_shortcut = midnight_get_shortcut;
1787 add_widget (midnight_dlg, the_menubar);
1788 init_menu ();
1790 add_widget (midnight_dlg, get_panel_widget (0));
1791 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);
1795 add_widget (midnight_dlg, the_bar);
1796 init_labels ();
1799 if (boot_current_is_left)
1800 dlg_select_widget (get_panel_widget (0));
1801 else
1802 dlg_select_widget (get_panel_widget (1));
1804 /* Run the Midnight Commander if no file was specified in the command line */
1805 run_dlg (midnight_dlg);
1808 /* result must be free'd (I think this should go in util.c) */
1809 static char *
1810 prepend_cwd_on_local (const char *filename)
1812 char *d;
1813 int l;
1815 if (vfs_file_is_local (filename)) {
1816 if (*filename == PATH_SEP) /* an absolute pathname */
1817 return g_strdup (filename);
1818 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1819 mc_get_current_wd (d, MC_MAXPATHLEN);
1820 l = strlen (d);
1821 d[l++] = PATH_SEP;
1822 strcpy (d + l, filename);
1823 canonicalize_pathname (d);
1824 return d;
1825 } else
1826 return g_strdup (filename);
1829 static int
1830 mc_maybe_editor_or_viewer (void)
1832 if (!(view_one_file || edit_one_file))
1833 return 0;
1835 /* Invoke the internal view/edit routine with:
1836 * the default processing and forcing the internal viewer/editor
1838 if (view_one_file) {
1839 char *path = NULL;
1840 path = prepend_cwd_on_local (view_one_file);
1841 view_file (path, 0, 1);
1842 g_free (path);
1844 #ifdef USE_INTERNAL_EDIT
1845 else {
1846 edit_file (edit_one_file, edit_one_file_start_line);
1848 #endif /* USE_INTERNAL_EDIT */
1849 midnight_shutdown = 1;
1850 done_mc ();
1851 return 1;
1854 /* Run the main dialog that occupies the whole screen */
1855 static void
1856 do_nc (void)
1858 int midnight_colors[DLG_COLOR_NUM];
1859 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1860 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1861 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1862 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1864 panel_init ();
1866 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1867 "[main]", NULL, DLG_WANT_IDLE);
1869 if (view_one_file || edit_one_file)
1870 setup_dummy_mc ();
1871 else
1872 setup_mc ();
1874 /* start check display_codepage and source_codepage */
1875 check_codeset ();
1877 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;
1884 if (main_x_keymap && main_x_keymap->len > 0)
1885 main_x_map = (global_keymap_t *) main_x_keymap->data;
1887 panel_map = default_panel_keymap;
1889 if (panel_keymap && panel_keymap->len > 0) {
1890 panel_map = (global_keymap_t *) panel_keymap->data;
1893 input_map = default_input_keymap;
1895 if (input_keymap && input_keymap->len > 0)
1896 input_map = (global_keymap_t *) input_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 ();
1911 destroy_dlg (midnight_dlg);
1912 panel_deinit ();
1913 current_panel = 0;
1914 done_mc_profile ();
1917 /* POSIX version. The only version we support. */
1918 static void
1919 OS_Setup (void)
1921 const char *shell_env = getenv ("SHELL");
1922 const char *mc_libdir;
1924 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1925 struct passwd *pwd;
1926 pwd = getpwuid (geteuid ());
1927 if (pwd != NULL)
1928 shell = g_strdup (pwd->pw_shell);
1929 } else
1930 shell = g_strdup (shell_env);
1932 if ((shell == NULL) || (shell[0] == '\0')) {
1933 g_free (shell);
1934 shell = g_strdup ("/bin/sh");
1937 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1938 /* and can be overriden by the MC_DATADIR environment variable */
1939 mc_libdir = getenv ("MC_DATADIR");
1940 if (mc_libdir != NULL) {
1941 mc_home = g_strdup (mc_libdir);
1942 mc_home_alt = g_strdup (SYSCONFDIR);
1943 } else {
1944 mc_home = g_strdup (SYSCONFDIR);
1945 mc_home_alt = g_strdup (DATADIR);
1948 /* This variable is used by the subshell */
1949 home_dir = getenv ("HOME");
1951 if (!home_dir)
1952 home_dir = mc_home;
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)
2033 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
2034 #ifdef WITH_SMBFS
2035 smbfs_set_debugf (mc_args__netfs_logfile);
2036 #endif /* WITH_SMBFS */
2039 #ifdef WITH_SMBFS
2040 if (mc_args__debug_level != 0)
2042 smbfs_set_debug (mc_args__debug_level);
2044 #endif /* WITH_SMBFS */
2045 #endif /* USE_NETCODE */
2048 base = x_basename (argv[0]);
2049 tmp = (argc > 0)? argv[1] : NULL;
2052 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2053 edit_one_file = "";
2054 if (tmp) {
2056 * Check for filename:lineno, followed by an optional colon.
2057 * This format is used by many programs (especially compilers)
2058 * in error messages and warnings. It is supported so that
2059 * users can quickly copy and paste file locations.
2061 char *end = tmp + strlen (tmp), *p = end;
2062 if (p > tmp && p[-1] == ':')
2063 p--;
2064 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2065 p--;
2066 if (tmp < p && p < end && p[-1] == ':') {
2067 struct stat st;
2068 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2070 * Check that the file before the colon actually exists.
2071 * If it doesn't exist, revert to the old behavior.
2073 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2074 edit_one_file = fname;
2075 edit_one_file_start_line = atoi (p);
2076 } else {
2077 g_free (fname);
2078 goto try_plus_filename;
2080 } else {
2081 try_plus_filename:
2082 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2083 int start_line = atoi (tmp);
2084 if (start_line > 0) {
2085 char *file = (argc > 1) ? argv[2] : NULL;
2086 if (file) {
2087 tmp = file;
2088 edit_one_file_start_line = start_line;
2092 edit_one_file = g_strdup (tmp);
2095 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2096 if (tmp)
2097 view_one_file = g_strdup (tmp);
2098 else {
2099 fputs ("No arguments given to the viewer\n", stderr);
2100 exit (1);
2102 } else {
2103 /* sets the current dir and the other dir */
2104 if (tmp) {
2105 this_dir = g_strdup (tmp);
2106 tmp = (argc > 1) ? argv[2] : NULL;
2107 if (tmp)
2108 other_dir = g_strdup (tmp);
2116 main (int argc, char *argv[])
2118 struct stat s;
2119 char *mc_dir;
2120 GError *error = NULL;
2121 gboolean isInitialized;
2123 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2124 setlocale (LC_ALL, "");
2125 bindtextdomain ("mc", LOCALEDIR);
2126 textdomain ("mc");
2128 /* Set up temporary directory */
2129 mc_tmpdir ();
2131 OS_Setup ();
2133 str_init_strings (NULL);
2135 vfs_init ();
2137 #ifdef USE_INTERNAL_EDIT
2138 edit_stack_init ();
2139 #endif
2141 #ifdef HAVE_SLANG
2142 SLtt_Ignore_Beep = 1;
2143 #endif
2145 if ( !mc_args_handle (&argc, &argv, "mc"))
2146 return 1;
2147 mc_main__setup_by_args(argc,argv);
2150 /* NOTE: This has to be called before tty_init or whatever routine
2151 calls any define_sequence */
2152 init_key ();
2154 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2155 handle_console (CONSOLE_INIT);
2157 #ifdef HAVE_SUBSHELL_SUPPORT
2158 /* Don't use subshell when invoked as viewer or editor */
2159 if (edit_one_file || view_one_file)
2160 use_subshell = 0;
2162 if (use_subshell)
2163 subshell_get_console_attributes ();
2164 #endif /* HAVE_SUBSHELL_SUPPORT */
2166 /* Install the SIGCHLD handler; must be done before init_subshell() */
2167 init_sigchld ();
2169 /* We need this, since ncurses endwin () doesn't restore the signals */
2170 save_stop_handler ();
2172 /* Must be done before init_subshell, to set up the terminal size: */
2173 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2174 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2176 load_setup ();
2178 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2180 isInitialized = mc_skin_init(&error);
2182 mc_filehighlight = mc_fhl_new (TRUE);
2184 dlg_set_default_colors ();
2186 if ( ! isInitialized ) {
2187 message (D_ERROR, _("Warning"), error->message);
2188 g_error_free(error);
2189 error = NULL;
2192 /* create home directory */
2193 /* do it after the screen library initialization to show the error message */
2194 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2195 canonicalize_pathname (mc_dir);
2196 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2197 && mkdir (mc_dir, 0700) != 0)
2198 message (D_ERROR, _("Warning"),
2199 _("Cannot create %s directory"), mc_dir);
2200 g_free (mc_dir);
2202 #ifdef HAVE_SUBSHELL_SUPPORT
2203 /* Done here to ensure that the subshell doesn't */
2204 /* inherit the file descriptors opened below, etc */
2205 if (use_subshell)
2206 init_subshell ();
2208 #endif /* HAVE_SUBSHELL_SUPPORT */
2210 /* Removing this from the X code let's us type C-c */
2211 load_key_defs ();
2213 load_keymap_defs ();
2215 /* Also done after init_subshell, to save any shell init file messages */
2216 if (console_flag)
2217 handle_console (CONSOLE_SAVE);
2219 if (alternate_plus_minus)
2220 application_keypad_mode ();
2222 #ifdef HAVE_SUBSHELL_SUPPORT
2223 if (use_subshell) {
2224 prompt = strip_ctrl_codes (subshell_prompt);
2225 if (!prompt)
2226 prompt = "";
2227 } else
2228 #endif /* HAVE_SUBSHELL_SUPPORT */
2229 prompt = (geteuid () == 0) ? "# " : "$ ";
2232 /* Program main loop */
2233 if (!midnight_shutdown)
2234 do_nc ();
2236 /* Save the tree store */
2237 tree_store_save ();
2239 free_keymap_defs ();
2241 /* Virtual File System shutdown */
2242 vfs_shut ();
2244 flush_extension_file (); /* does only free memory */
2246 mc_fhl_free (&mc_filehighlight);
2247 mc_skin_deinit ();
2249 tty_shutdown ();
2251 if (console_flag && !(quit & SUBSHELL_EXIT))
2252 handle_console (CONSOLE_RESTORE);
2253 if (alternate_plus_minus)
2254 numeric_keypad_mode ();
2256 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2258 if (console_flag)
2259 handle_console (CONSOLE_DONE);
2260 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2262 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2263 && !edit_one_file && !view_one_file) {
2264 int last_wd_fd =
2265 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2266 S_IRUSR | S_IWUSR);
2268 if (last_wd_fd != -1) {
2269 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2270 close (last_wd_fd);
2273 g_free (last_wd_string);
2275 g_free (mc_home_alt);
2276 g_free (mc_home);
2277 g_free (shell);
2279 done_key ();
2280 #ifdef HAVE_CHARSET
2281 free_codepages_list ();
2282 #endif
2283 str_uninit_strings ();
2285 g_free (this_dir);
2286 g_free (other_dir);
2288 #ifdef USE_INTERNAL_EDIT
2289 edit_stack_free ();
2290 #endif
2292 return 0;