Fix Makefile.am for correct run 'make dist' command
[midnight-commander.git] / src / main.c
blob17a6eca8841711cecac3a4bdd8759fd197d574fb
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
6 1994, 1995 Janne Kukonlehto
7 1997 Norbert Warmuth
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file main.c
24 * \brief Source: this is a main module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pwd.h> /* for username in xterm title */
42 #include "global.h"
44 #include "../src/tty/tty.h"
45 #include "../src/skin/skin.h"
46 #include "../src/tty/mouse.h"
47 #include "../src/tty/key.h" /* For init_key() */
48 #include "../src/tty/win.h" /* xterm_flag */
50 #include "../src/mcconfig/mcconfig.h"
51 #include "../src/args.h"
52 #include "../src/skin/skin.h"
53 #include "../src/filehighlight/fhl.h"
55 #include "dir.h"
56 #include "dialog.h"
57 #include "menu.h"
58 #include "panel.h"
59 #include "main.h"
60 #include "option.h"
61 #include "tree.h"
62 #include "treestore.h"
63 #include "cons.saver.h"
64 #include "subshell.h"
65 #include "setup.h" /* save_setup() */
66 #include "boxes.h" /* sort_box() */
67 #include "layout.h"
68 #include "cmd.h" /* Normal commands */
69 #include "hotlist.h"
70 #include "panelize.h"
71 #include "learn.h" /* learn_keys() */
72 #include "listmode.h"
73 #include "execute.h"
74 #include "ext.h" /* For flush_extension_file() */
75 #include "strutil.h"
76 /* Listbox for the command history feature */
77 #include "widget.h"
78 #include "command.h"
79 #include "wtools.h"
80 #include "cmddef.h" /* CK_ cmd name const */
82 #include "../vfs/vfs.h" /* vfs_translate_url() */
84 #include "chmod.h"
85 #include "chown.h"
86 #include "achown.h"
88 #ifdef WITH_SMBFS
89 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
90 #endif
92 #ifdef USE_INTERNAL_EDIT
93 # include "../edit/edit.h"
94 #endif
96 #ifdef HAVE_CHARSET
97 #include "charsets.h"
98 #endif /* HAVE_CHARSET */
100 #ifdef USE_VFS
101 #include "../vfs/gc.h"
102 #endif
104 #include "keybind.h" /* type global_key_map_t */
106 /* When the modes are active, left_panel, right_panel and tree_panel */
107 /* Point to a proper data structure. You should check with the functions */
108 /* get_current_type and get_other_type the types of the panels before using */
109 /* This pointer variables */
111 /* The structures for the panels */
112 WPanel *left_panel = NULL;
113 WPanel *right_panel = NULL;
115 mc_fhl_t *mc_filehighlight;
117 /* The pointer to the tree */
118 WTree *the_tree = NULL;
120 /* The Menubar */
121 struct WMenu *the_menubar = NULL;
123 /* Pointers to the selected and unselected panel */
124 WPanel *current_panel = NULL;
126 /* Set if the command is being run from the "Right" menu */
127 int is_right = 0;
129 /* Set when main loop should be terminated */
130 volatile int quit = 0;
132 /* Set if you want the possible completions dialog for the first time */
133 int show_all_if_ambiguous = 0;
135 /* Set when cd symlink following is desirable (bash mode) */
136 int cd_symlinks = 1;
138 /* If set then dialogs just clean the screen when refreshing, else */
139 /* they do a complete refresh, refreshing all the parts of the program */
140 int fast_refresh = 0;
142 /* If true, marking a files moves the cursor down */
143 int mark_moves_down = 1;
145 /* If true, at startup the user-menu is invoked */
146 int auto_menu = 0;
148 /* If true, then the +, - and \ keys have their special meaning only if the
149 * command line is emtpy, otherwise they behave like regular letters
151 int only_leading_plus_minus = 1;
153 int pause_after_run = pause_on_dumb_terminals;
155 /* It true saves the setup when quitting */
156 int auto_save_setup = 1;
158 #ifdef HAVE_CHARSET
160 * Don't restrict the output on the screen manager level,
161 * the translation tables take care of it.
163 #define full_eight_bits (1)
164 #define eight_bit_clean (1)
165 #else /* HAVE_CHARSET */
166 /* If true, allow characters in the range 160-255 */
167 int eight_bit_clean = 1;
169 * If true, also allow characters in the range 128-159.
170 * This is reported to break on many terminals (xterm, qansi-m).
172 int full_eight_bits = 0;
173 #endif /* !HAVE_CHARSET */
176 * If utf-8 terminal utf8_display = 1
177 * Display bits set UTF-8
180 int utf8_display = 0;
182 /* If true use the internal viewer */
183 int use_internal_view = 1;
185 /* Have we shown the fast-reload warning in the past? */
186 int fast_reload_w = 0;
188 /* Move page/item? When clicking on the top or bottom of a panel */
189 int mouse_move_pages = 1;
191 /* If true: l&r arrows are used to chdir if the input line is empty */
192 int navigate_with_arrows = 0;
194 /* If true program softkeys (HP terminals only) on startup and after every
195 command ran in the subshell to the description found in the termcap/terminfo
196 database */
197 int reset_hp_softkeys = 0;
199 /* The prompt */
200 const char *prompt = NULL;
202 /* The widget where we draw the prompt */
203 WLabel *the_prompt;
205 /* The hint bar */
206 WLabel *the_hint;
208 /* The button bar */
209 WButtonBar *the_bar;
211 /* Mouse type: GPM, xterm or none */
212 Mouse_Type use_mouse_p = MOUSE_NONE;
214 /* If on, default for "No" in delete operations */
215 int safe_delete = 0;
217 /* Controls screen clearing before an exec */
218 int clear_before_exec = 1;
220 /* Asks for confirmation before deleting a file */
221 int confirm_delete = 1;
223 /* Asks for confirmation before deleting a hotlist entry */
224 int confirm_directory_hotlist_delete = 1;
226 /* Asks for confirmation before overwriting a file */
227 int confirm_overwrite = 1;
229 /* Asks for confirmation before executing a program by pressing enter */
230 int confirm_execute = 0;
232 /* Asks for confirmation before leaving the program */
233 int confirm_exit = 1;
235 /* Asks for confirmation when using F3 to view a directory and there
236 are tagged files */
237 int confirm_view_dir = 0;
239 /* This flag indicates if the pull down menus by default drop down */
240 int drop_menus = 0;
242 /* if skip_check_codeset = 1 do not show warning about
243 * system and display codeset is different
245 int skip_check_codeset = 0;
247 /* The dialog handle for the main program */
248 Dlg_head *midnight_dlg = NULL;
250 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
251 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
252 int update_prompt = 0;
254 /* The home directory */
255 const char *home_dir = NULL;
257 /* The value of the other directory, only used when loading the setup */
258 char *other_dir = NULL;
260 /* Only used at program boot */
261 int boot_current_is_left = 1;
263 static char *this_dir = NULL;
265 /* If this is true, then when browsing the tree the other window will
266 * automatically reload it's directory with the contents of the currently
267 * selected directory.
269 int xtree_mode = 0;
271 /* If set, then print to the given file the last directory we were at */
272 static char *last_wd_string = NULL;
273 /* Set to 1 to suppress printing the last directory */
274 static int print_last_revert = 0;
276 /* File name to view if argument was supplied */
277 const char *view_one_file = NULL;
279 /* File name to edit if argument was supplied */
280 const char *edit_one_file = NULL;
282 /* Line to start the editor on */
283 static int edit_one_file_start_line = 0;
285 /* Used so that widgets know if they are being destroyed or
286 shut down */
287 int midnight_shutdown = 0;
289 /* The user's shell */
290 char *shell = NULL;
292 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
293 char *mc_home = NULL;
295 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
296 char *mc_home_alt = NULL;
298 char cmd_buf[512];
300 /* Define this function for glib-style error handling */
301 GQuark
302 mc_main_error_quark (void)
304 return g_quark_from_static_string (PACKAGE);
307 GArray *editor_keymap = NULL;
308 GArray *viewer_keymap = NULL;
309 GArray *viewer_hex_keymap = NULL;
310 GArray *main_keymap = NULL;
311 GArray *main_x_keymap = NULL;
312 GArray *panel_keymap = NULL;
313 GArray *input_keymap = NULL;
315 const global_key_map_t *main_map;
316 const global_key_map_t *main_x_map;
318 /* Save current stat of directories to avoid reloading the panels */
319 /* when no modifications have taken place */
320 void
321 save_cwds_stat (void)
323 if (fast_reload) {
324 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
325 if (get_other_type () == view_listing)
326 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
330 #ifdef HAVE_SUBSHELL_SUPPORT
331 void
332 do_update_prompt (void)
334 if (update_prompt) {
335 printf ("\r\n%s", subshell_prompt);
336 fflush (stdout);
337 update_prompt = 0;
340 #endif /* HAVE_SUBSHELL_SUPPORT */
342 void
343 change_panel (void)
345 free_completions (cmdline);
346 dlg_one_down (midnight_dlg);
349 /* Stop MC main dialog and the current dialog if it exists.
350 * Needed to provide fast exit from MC viewer or editor on shell exit */
351 static void
352 stop_dialogs (void)
354 midnight_dlg->running = 0;
355 if (current_dlg) {
356 current_dlg->running = 0;
360 static int
361 quit_cmd_internal (int quiet)
363 int q = quit;
365 if (quiet || !confirm_exit) {
366 q = 1;
367 } else {
368 if (query_dialog
369 (_(" The Midnight Commander "),
370 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
371 2, _("&Yes"), _("&No")) == 0)
372 q = 1;
374 if (q) {
375 #ifdef HAVE_SUBSHELL_SUPPORT
376 if (!use_subshell)
377 stop_dialogs ();
378 else if ((q = exit_subshell ()))
379 #endif
380 stop_dialogs ();
382 if (q)
383 quit |= 1;
384 return quit;
387 static void
388 quit_cmd (void)
390 quit_cmd_internal (0);
393 void
394 quiet_quit_cmd (void)
396 print_last_revert = 1;
397 quit_cmd_internal (1);
400 /* Wrapper for do_subshell_chdir, check for availability of subshell */
401 void
402 subshell_chdir (const char *directory)
404 #ifdef HAVE_SUBSHELL_SUPPORT
405 if (use_subshell) {
406 if (vfs_current_is_local ())
407 do_subshell_chdir (directory, 0, 1);
409 #endif /* HAVE_SUBSHELL_SUPPORT */
412 void
413 directory_history_add (struct WPanel *panel, const char *dir)
415 char *tmp;
417 tmp = g_strdup (dir);
418 strip_password (tmp, 1);
420 panel->dir_history = list_append_unique (panel->dir_history, tmp);
424 * If we moved to the parent directory move the selection pointer to
425 * the old directory name; If we leave VFS dir, remove FS specificator.
427 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
429 static const char *
430 get_parent_dir_name (const char *cwd, const char *lwd)
432 const char *p;
433 if (strlen (lwd) > strlen (cwd))
434 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
435 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
436 cwd[1] == '\0'))) {
437 return (p + 1);
439 return NULL;
443 * Changes the current directory of the panel.
444 * Don't record change in the directory history.
446 static int
447 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
449 const char *directory;
450 char *olddir;
451 char temp[MC_MAXPATHLEN];
452 char *translated_url;
454 if (cd_type == cd_parse_command) {
455 while (*new_dir == ' ')
456 new_dir++;
459 olddir = g_strdup (panel->cwd);
460 new_dir = translated_url = vfs_translate_url (new_dir);
462 /* Convert *new_path to a suitable pathname, handle ~user */
464 if (cd_type == cd_parse_command) {
465 if (!strcmp (new_dir, "-")) {
466 strcpy (temp, panel->lwd);
467 new_dir = temp;
470 directory = *new_dir ? new_dir : home_dir;
472 if (mc_chdir (directory) == -1) {
473 strcpy (panel->cwd, olddir);
474 g_free (olddir);
475 g_free (translated_url);
476 return 0;
478 g_free (translated_url);
480 /* Success: save previous directory, shutdown status of previous dir */
481 strcpy (panel->lwd, olddir);
482 free_completions (cmdline);
484 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
486 vfs_release_path (olddir);
488 subshell_chdir (panel->cwd);
490 /* Reload current panel */
491 panel_clean_dir (panel);
492 panel->count =
493 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
494 panel->reverse, panel->case_sensitive,
495 panel->exec_first, panel->filter);
496 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
497 load_hint (0);
498 panel->dirty = 1;
499 update_xterm_title_path ();
501 g_free (olddir);
503 return 1;
507 * Changes the current directory of the panel.
508 * Record change in the directory history.
511 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
513 int r;
515 r = _do_panel_cd (panel, new_dir, cd_type);
516 if (r)
517 directory_history_add (panel, panel->cwd);
518 return r;
522 do_cd (const char *new_dir, enum cd_enum exact)
524 return (do_panel_cd (current_panel, new_dir, exact));
527 void
528 directory_history_next (WPanel *panel)
530 GList *nextdir;
532 nextdir = g_list_next (panel->dir_history);
534 if (!nextdir)
535 return;
537 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
538 panel->dir_history = nextdir;
541 void
542 directory_history_prev (WPanel *panel)
544 GList *prevdir;
546 prevdir = g_list_previous (panel->dir_history);
548 if (!prevdir)
549 return;
551 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
552 panel->dir_history = prevdir;
555 void
556 directory_history_list (WPanel *panel)
558 char *s;
560 if (!panel->dir_history)
561 return;
563 s = show_hist (panel->dir_history, &panel->widget);
565 if (!s)
566 return;
568 if (_do_panel_cd (panel, s, cd_exact))
569 directory_history_add (panel, panel->cwd);
570 else
571 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
572 g_free (s);
575 #ifdef HAVE_SUBSHELL_SUPPORT
577 load_prompt (int fd, void *unused)
579 (void) fd;
580 (void) unused;
582 if (!read_subshell_prompt ())
583 return 0;
585 /* Don't actually change the prompt if it's invisible */
586 if (current_dlg == midnight_dlg && command_prompt) {
587 char *tmp_prompt;
588 int prompt_len;
590 tmp_prompt = strip_ctrl_codes (subshell_prompt);
591 prompt_len = str_term_width1 (tmp_prompt);
593 /* Check for prompts too big */
594 if (COLS > 8 && prompt_len > COLS - 8) {
595 tmp_prompt[COLS - 8] = '\0';
596 prompt_len = COLS - 8;
598 prompt = tmp_prompt;
599 label_set_text (the_prompt, prompt);
600 winput_set_origin ((WInput *) cmdline, prompt_len,
601 COLS - prompt_len);
603 /* since the prompt has changed, and we are called from one of the
604 * tty_get_event channels, the prompt updating does not take place
605 * automatically: force a cursor update and a screen refresh
607 update_cursor (midnight_dlg);
608 mc_refresh ();
610 update_prompt = 1;
611 return 0;
613 #endif /* HAVE_SUBSHELL_SUPPORT */
615 static void
616 sort_cmd (void)
618 WPanel *p;
619 sortfn *sort_order;
621 if (!SELECTED_IS_PANEL)
622 return;
624 p = MENU_PANEL;
625 sort_order = sort_box (p->sort_type, &p->reverse,
626 &p->case_sensitive,
627 &p->exec_first);
629 panel_set_sort_order (p, sort_order);
633 static void
634 treebox_cmd (void)
636 char *sel_dir;
638 sel_dir = tree_box (selection (current_panel)->fname);
639 if (sel_dir) {
640 do_cd (sel_dir, cd_exact);
641 g_free (sel_dir);
645 #ifdef LISTMODE_EDITOR
646 static void
647 listmode_cmd (void)
649 char *newmode;
651 if (get_current_type () != view_listing)
652 return;
654 newmode = listmode_edit (current_panel->user_format);
655 if (!newmode)
656 return;
658 g_free (current_panel->user_format);
659 current_panel->list_type = list_user;
660 current_panel->user_format = newmode;
661 set_panel_formats (current_panel);
663 do_refresh ();
665 #endif /* LISTMODE_EDITOR */
667 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
668 static menu_entry LeftMenu[] = {
669 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
670 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
671 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
672 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
673 {' ', "", NULL_HOTKEY, 0},
674 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
675 {' ', "", NULL_HOTKEY, 0},
676 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
677 #ifdef HAVE_CHARSET
678 {' ', "",NULL_HOTKEY, 0},
679 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
680 #endif
681 #ifdef USE_NETCODE
682 {' ', "", NULL_HOTKEY, 0},
683 #ifdef ENABLE_VFS_MCFS
684 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
685 #endif
686 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
687 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
688 #ifdef WITH_SMBFS
689 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
690 #endif
691 #endif
692 {' ', "", NULL_HOTKEY, 0},
693 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
696 static menu_entry RightMenu[] = {
697 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
698 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
699 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
700 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
701 {' ', "", NULL_HOTKEY, 0},
702 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
703 {' ', "", NULL_HOTKEY, 0},
704 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
705 #ifdef HAVE_CHARSET
706 {' ', "",NULL_HOTKEY, 0},
707 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
708 #endif
709 #ifdef USE_NETCODE
710 {' ', "", NULL_HOTKEY, 0},
711 #ifdef ENABLE_VFS_MCFS
712 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
713 #endif
714 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
715 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
716 #ifdef WITH_SMBFS
717 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
718 #endif
719 #endif
720 {' ', "", NULL_HOTKEY, 0},
721 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
724 static menu_entry FileMenu[] = {
725 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
726 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
727 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
728 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
729 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
730 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
731 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
732 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
733 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
734 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
735 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
736 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
737 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
738 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
739 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
740 {' ', "", NULL_HOTKEY, 0},
741 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
742 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
743 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
744 {' ', "", NULL_HOTKEY, 0},
745 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
748 static menu_entry CmdMenu[] = {
749 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
750 /* I know, I'm lazy, but the tree widget when it's not running
751 * as a panel still has some problems, I have not yet finished
752 * the WTree widget port, sorry.
754 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
755 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
756 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
757 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
758 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
759 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
760 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
761 {' ', "", NULL_HOTKEY, 0},
762 {' ', N_("Command &history M-h"), NULL_HOTKEY, history_cmd},
763 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
764 #ifdef USE_VFS
765 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
766 #endif
767 #ifdef WITH_BACKGROUND
768 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
769 #endif
770 {' ', "", NULL_HOTKEY, 0},
771 #ifdef USE_EXT2FSLIB
772 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
773 #endif
774 #ifdef LISTMODE_EDITOR
775 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
776 #endif
777 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
778 {' ', "", NULL_HOTKEY, 0},
779 #endif
780 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
781 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd},
782 {' ', N_("Edit hi&ghlighting group file"), NULL_HOTKEY, edit_fhl_cmd}
785 /* Must keep in sync with the constants in menu_cmd */
786 static menu_entry OptMenu[] = {
787 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
788 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
789 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
790 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
791 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
792 #ifdef USE_VFS
793 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
794 #endif /* !USE_VFS */
795 {' ', "", NULL_HOTKEY, 0},
796 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
799 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
801 static Menu *MenuBar[5];
803 void
804 init_menu (void)
806 MenuBar[0] =
807 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
808 LeftMenu, menu_entries (LeftMenu),
809 "[Left and Right Menus]");
810 MenuBar[1] =
811 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
812 "[File Menu]");
813 MenuBar[2] =
814 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
815 "[Command Menu]");
816 MenuBar[3] =
817 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
818 "[Options Menu]");
819 MenuBar[4] =
820 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
821 RightMenu, menu_entries (RightMenu),
822 "[Left and Right Menus]");
825 void
826 done_menu (void)
828 int i;
830 for (i = 0; i < 5; i++) {
831 destroy_menu (MenuBar[i]);
835 static void
836 menu_last_selected_cmd (void)
838 the_menubar->active = 1;
839 the_menubar->dropped = drop_menus;
840 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
841 dlg_select_widget (the_menubar);
844 static void
845 menu_cmd (void)
847 if (the_menubar->active)
848 return;
850 if ((get_current_index () == 0) ^ (!current_panel->active))
851 the_menubar->selected = 0;
852 else
853 the_menubar->selected = 4;
854 menu_last_selected_cmd ();
857 /* Flag toggling functions */
858 void
859 toggle_fast_reload (void)
861 fast_reload = !fast_reload;
862 if (fast_reload_w == 0 && fast_reload) {
863 message (D_NORMAL, _(" Information "),
865 (" Using the fast reload option may not reflect the exact \n"
866 " directory contents. In this case you'll need to do a \n"
867 " manual reload of the directory. See the man page for \n"
868 " the details. "));
869 fast_reload_w = 1;
873 void
874 toggle_mix_all_files (void)
876 mix_all_files = !mix_all_files;
877 update_panels (UP_RELOAD, UP_KEEPSEL);
880 void
881 toggle_show_backup (void)
883 show_backups = !show_backups;
884 update_panels (UP_RELOAD, UP_KEEPSEL);
887 void
888 toggle_show_hidden (void)
890 show_dot_files = !show_dot_files;
891 update_panels (UP_RELOAD, UP_KEEPSEL);
894 void
895 toggle_kilobyte_si (void)
897 kilobyte_si = !kilobyte_si;
898 update_panels (UP_RELOAD, UP_KEEPSEL);
902 * Just a hack for allowing url-like pathnames to be accepted from the
903 * command line.
905 static void
906 translated_mc_chdir (char *dir)
908 char *newdir;
910 newdir = vfs_translate_url (dir);
911 mc_chdir (newdir);
912 g_free (newdir);
915 static void
916 create_panels (void)
918 int current_index;
919 int other_index;
920 int current_mode;
921 int other_mode;
922 char original_dir[1024];
924 original_dir[0] = 0;
926 if (boot_current_is_left) {
927 current_index = 0;
928 other_index = 1;
929 current_mode = startup_left_mode;
930 other_mode = startup_right_mode;
931 } else {
932 current_index = 1;
933 other_index = 0;
934 current_mode = startup_right_mode;
935 other_mode = startup_left_mode;
937 /* Creates the left panel */
938 if (this_dir) {
939 if (other_dir) {
940 /* Ok, user has specified two dirs, save the original one,
941 * since we may not be able to chdir to the proper
942 * second directory later
944 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
946 translated_mc_chdir (this_dir);
948 set_display_type (current_index, current_mode);
950 /* The other panel */
951 if (other_dir) {
952 if (original_dir[0])
953 translated_mc_chdir (original_dir);
954 translated_mc_chdir (other_dir);
956 set_display_type (other_index, other_mode);
958 if (startup_left_mode == view_listing) {
959 current_panel = left_panel;
960 } else {
961 if (right_panel)
962 current_panel = right_panel;
963 else
964 current_panel = left_panel;
967 /* Create the nice widgets */
968 cmdline = command_new (0, 0, 0);
969 the_prompt = label_new (0, 0, prompt);
970 the_prompt->transparent = 1;
971 the_bar = buttonbar_new (keybar_visible);
973 the_hint = label_new (0, 0, 0);
974 the_hint->transparent = 1;
975 the_hint->auto_adjust_cols = 0;
976 the_hint->widget.cols = COLS;
978 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
981 static void
982 copy_current_pathname (void)
984 char *cwd_path;
985 if (!command_prompt)
986 return;
988 cwd_path = remove_encoding_from_path (current_panel->cwd);
989 command_insert (cmdline, cwd_path, 0);
991 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
992 command_insert (cmdline, PATH_SEP_STR, 0);
993 g_free (cwd_path);
996 static void
997 copy_other_pathname (void)
999 char *cwd_path;
1001 if (get_other_type () != view_listing)
1002 return;
1004 if (!command_prompt)
1005 return;
1007 cwd_path = remove_encoding_from_path (other_panel->cwd);
1008 command_insert (cmdline, cwd_path, 0);
1010 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1011 command_insert (cmdline, PATH_SEP_STR, 0);
1012 g_free (cwd_path);
1015 static void
1016 copy_readlink (WPanel *panel)
1018 if (!command_prompt)
1019 return;
1020 if (S_ISLNK (selection (panel)->st.st_mode)) {
1021 char buffer[MC_MAXPATHLEN];
1022 char *p =
1023 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1024 int i;
1026 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1027 g_free (p);
1028 if (i > 0) {
1029 buffer[i] = 0;
1030 command_insert (cmdline, buffer, 1);
1035 static void
1036 copy_current_readlink (void)
1038 copy_readlink (current_panel);
1041 static void
1042 copy_other_readlink (void)
1044 if (get_other_type () != view_listing)
1045 return;
1046 copy_readlink (other_panel);
1049 /* Insert the selected file name into the input line */
1050 static void
1051 copy_prog_name (void)
1053 char *tmp;
1054 if (!command_prompt)
1055 return;
1057 if (get_current_type () == view_tree) {
1058 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1059 tmp = tree_selected_name (tree);
1060 } else
1061 tmp = selection (current_panel)->fname;
1063 command_insert (cmdline, tmp, 1);
1066 static void
1067 copy_tagged (WPanel *panel)
1069 int i;
1071 if (!command_prompt)
1072 return;
1073 input_disable_update (cmdline);
1074 if (panel->marked) {
1075 for (i = 0; i < panel->count; i++) {
1076 if (panel->dir.list[i].f.marked)
1077 command_insert (cmdline, panel->dir.list[i].fname, 1);
1079 } else {
1080 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1083 input_enable_update (cmdline);
1086 static void
1087 copy_current_tagged (void)
1089 copy_tagged (current_panel);
1092 static void
1093 copy_other_tagged (void)
1095 if (get_other_type () != view_listing)
1096 return;
1097 copy_tagged (other_panel);
1100 static void
1101 init_labels (void)
1103 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1104 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1105 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1106 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1109 static int ctl_x_map_enabled = 0;
1111 static void
1112 ctl_x_cmd (void)
1114 ctl_x_map_enabled = 1;
1117 static cb_ret_t
1118 midnight_execute_cmd(int command)
1120 cb_ret_t res = MSG_HANDLED;
1122 switch (command) {
1123 case CK_MenuLastSelectedCmd:
1124 menu_last_selected_cmd ();
1125 break;
1126 case CK_QuietQuitCmd:
1127 quiet_quit_cmd ();
1128 break;
1129 case CK_SingleDirsizeCmd:
1130 smart_dirsize_cmd ();
1131 break;
1132 case CK_CopyCurrentPathname:
1133 copy_current_pathname ();
1134 break;
1135 case CK_CopyOtherPathname:
1136 copy_other_pathname ();
1137 break;
1138 case CK_QuickCdCmd:
1139 quick_cd_cmd ();
1140 break;
1141 case CK_QuickChdirCmd:
1142 quick_chdir_cmd ();
1143 break;
1144 case CK_SuspendCmd:
1145 suspend_cmd ();
1146 break;
1147 case CK_FilteredViewCmd:
1148 filtered_view_cmd ();
1149 break;
1150 case CK_FindCmd:
1151 find_cmd ();
1152 break;
1153 case CK_RereadCmd:
1154 reread_cmd ();
1155 break;
1156 case CK_ToggleListingCmd:
1157 toggle_listing_cmd ();
1158 break;
1159 case CK_SwapCmd:
1160 swap_cmd ();
1161 break;
1162 case CK_ShowCommandLine:
1163 view_other_cmd ();
1164 break;
1165 case CK_QuitCmd:
1166 quit_cmd ();
1167 break;
1168 case CK_CompareDirsCmd:
1169 compare_dirs_cmd ();
1170 break;
1171 case CK_ReselectVfs:
1172 reselect_vfs ();
1173 break;
1174 case CK_CopyCurrentTagged:
1175 copy_current_tagged ();
1176 break;
1177 case CK_CopyOtherTarget:
1178 copy_other_tagged ();
1179 break;
1180 case CK_CopyCurrentReadlink:
1181 copy_current_readlink ();
1182 break;
1183 case CK_CopyOtherReadlink:
1184 copy_other_readlink ();
1185 break;
1186 case CK_ChmodCmd:
1187 chmod_cmd ();
1188 break;
1189 case CK_ChownCmd:
1190 chown_cmd ();
1191 break;
1192 case CK_LinkCmd:
1193 link_cmd () ;
1194 break;
1195 case CK_SymlinkCmd:
1196 symlink_cmd ();
1197 break;
1198 case CK_EditSymlinkCmd:
1199 edit_symlink_cmd ();
1200 break;
1201 case CK_InfoCmd:
1202 info_cmd_no_menu ();
1203 break;
1204 case CK_QuickViewCmd:
1205 quick_cmd_no_menu ();
1206 break;
1207 case CK_AddHotlist:
1208 add2hotlist_cmd ();
1209 break;
1210 case CK_ExternalPanelize:
1211 external_panelize ();
1212 break;
1213 #ifdef WITH_BACKGROUND
1214 case CK_JobsCmd:
1215 jobs_cmd ();
1216 break;
1217 #endif
1218 case CK_ToggleShowHidden:
1219 toggle_show_hidden ();
1220 break;
1221 case CK_StartExtMap1:
1222 ctl_x_cmd ();
1223 break;
1224 default:
1225 res = MSG_NOT_HANDLED;
1228 return res;
1231 static void
1232 setup_pre (void)
1234 /* Call all the inits */
1236 #ifdef HAVE_SLANG
1237 tty_display_8bit (full_eight_bits != 0);
1238 #else
1239 tty_display_8bit (eight_bit_clean != 0);
1240 #endif
1243 static void
1244 init_xterm_support (void)
1246 const char *termvalue;
1248 termvalue = getenv ("TERM");
1249 if (!termvalue || !(*termvalue)) {
1250 fputs (_("The TERM environment variable is unset!\n"), stderr);
1251 exit (1);
1254 /* Check mouse capabilities */
1255 xmouse_seq = tty_tgetstr ("Km");
1257 if (strcmp (termvalue, "cygwin") == 0) {
1258 mc_args__force_xterm = 1;
1259 use_mouse_p = MOUSE_DISABLED;
1262 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1263 || strncmp (termvalue, "konsole", 7) == 0
1264 || strncmp (termvalue, "rxvt", 4) == 0
1265 || strcmp (termvalue, "Eterm") == 0
1266 || strcmp (termvalue, "dtterm") == 0) {
1267 xterm_flag = 1;
1269 /* Default to the standard xterm sequence */
1270 if (!xmouse_seq) {
1271 xmouse_seq = ESC_STR "[M";
1274 /* Enable mouse unless explicitly disabled by --nomouse */
1275 if (use_mouse_p != MOUSE_DISABLED) {
1276 const char *color_term = getenv ("COLORTERM");
1277 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1278 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1279 strcmp (termvalue, "Eterm") == 0) {
1280 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1281 } else {
1282 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1288 static void
1289 setup_mc (void)
1291 setup_pre ();
1292 init_menu ();
1293 create_panels ();
1294 setup_panels ();
1296 #ifdef HAVE_SUBSHELL_SUPPORT
1297 if (use_subshell)
1298 add_select_channel (subshell_pty, load_prompt, 0);
1299 #endif /* !HAVE_SUBSHELL_SUPPORT */
1301 tty_setup_sigwinch (sigwinch_handler);
1303 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1305 init_xterm_support ();
1306 init_mouse ();
1309 static void
1310 setup_dummy_mc (void)
1312 char d[MC_MAXPATHLEN];
1314 mc_get_current_wd (d, MC_MAXPATHLEN);
1315 setup_mc ();
1316 mc_chdir (d);
1319 static void check_codeset()
1321 const char *_system_codepage = NULL;
1322 #ifdef HAVE_CHARSET
1323 const char *_source_codepage = NULL;
1324 const char *_display_codepage = NULL;
1325 int profile_changed = 0;
1327 #define CONFY 16
1328 #define CONFX 54
1330 if ( !skip_check_codeset ) {
1332 QuickWidget ecs_widgets [] = {
1333 QUICK_BUTTON (4, 6, 13, CONFY, N_("&Skip"), B_EXIT, NULL),
1334 QUICK_BUTTON (1, 11, 13, CONFY, N_("&Fix it"), B_ENTER, NULL),
1335 QUICK_CHECKBOX (1, 13, 11, CONFY, N_("don't ask again"), &skip_check_codeset),
1336 QUICK_LABEL (2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
1337 "or source codeset (in mcedit ctrl-t) \n"
1338 "does not match one set via locale. \n"
1339 "Set correct codeset manually or press <<Fix it>> \n"
1340 "to set locale default.\n\n"
1341 "Or set \'don't ask again\' and press <<Skip>>")),
1342 QUICK_END
1345 QuickDialog ecs =
1347 CONFX, CONFY, -1, -1, N_(" Confirmation "),
1348 "[Confirmation]", ecs_widgets, FALSE
1352 _system_codepage = str_detect_termencoding();
1353 _source_codepage = get_codepage_id (source_codepage);
1354 _display_codepage = get_codepage_id (display_codepage);
1355 if ( (strcmp (_system_codepage, _display_codepage)) ||
1356 (strcmp (_system_codepage, _source_codepage)) ) {
1357 if (quick_dialog (&ecs) == B_ENTER){
1358 display_codepage = get_codepage_index (_system_codepage);
1359 cp_display = get_codepage_id (display_codepage);
1360 if ( !strcmp (cp_display, _system_codepage)) {
1361 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1362 mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
1363 display_codepage = get_codepage_index ( cp_display );
1364 utf8_display = str_isutf8 (_system_codepage);
1365 source_codepage = display_codepage;
1366 cp_source = cp_display;
1367 profile_changed = 1;
1368 } else {
1369 utf8_display = str_isutf8 (_system_codepage);
1370 cp_source = cp_display = _system_codepage;
1372 } else {
1373 if ( skip_check_codeset ) {
1374 mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
1375 profile_changed = 1;
1379 if ( profile_changed )
1380 save_configure ();
1382 #else /* HAVE_CHARSET */
1383 _system_codepage = str_detect_termencoding();
1384 utf8_display = str_isutf8 (_system_codepage);
1385 #endif /* HAVE_CHARSET */
1388 static void
1389 done_screen (void)
1391 if (!(quit & SUBSHELL_EXIT))
1392 clr_scr ();
1393 tty_reset_shell_mode ();
1394 tty_noraw_mode ();
1395 tty_keypad (FALSE);
1396 tty_colors_done ();
1399 static void
1400 done_mc (void)
1402 disable_mouse ();
1404 done_menu ();
1406 /* Setup shutdown
1408 * We sync the profiles since the hotlist may have changed, while
1409 * we only change the setup data if we have the auto save feature set
1412 if (auto_save_setup)
1413 save_setup (); /* does also call save_hotlist */
1414 else {
1415 save_hotlist ();
1416 save_panel_types ();
1418 done_screen ();
1419 vfs_add_current_stamps ();
1422 /* This should be called after destroy_dlg since panel widgets
1423 * save their state on the profiles
1425 static void
1426 done_mc_profile (void)
1428 done_setup ();
1431 static cb_ret_t
1432 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1434 int i;
1436 switch (msg) {
1438 case DLG_IDLE:
1439 /* We only need the first idle event */
1440 set_idle_proc (h, 0);
1441 if (auto_menu) {
1442 user_file_menu_cmd ();
1444 return MSG_HANDLED;
1446 case DLG_KEY:
1447 if (ctl_x_map_enabled) {
1448 ctl_x_map_enabled = 0;
1449 for (i = 0; main_x_map[i].key; i++)
1450 if (parm == main_x_map[i].key)
1451 return midnight_execute_cmd (main_x_map[i].command);
1454 /* FIXME: should handle all menu shortcuts before this point */
1455 if (the_menubar->active)
1456 return MSG_NOT_HANDLED;
1458 if (parm == KEY_F (10)) {
1459 quit_cmd ();
1460 return MSG_HANDLED;
1463 if (parm == '\t')
1464 free_completions (cmdline);
1466 if (parm == '\n') {
1467 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1468 cmdline->buffer[i] == '\t'); i++);
1469 if (cmdline->buffer[i]) {
1470 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1471 return MSG_HANDLED;
1473 stuff (cmdline, "", 0);
1474 cmdline->point = 0;
1477 /* Ctrl-Enter and Alt-Enter */
1478 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1479 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1480 copy_prog_name ();
1481 return MSG_HANDLED;
1484 /* Ctrl-Shift-Enter */
1485 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1486 copy_current_pathname ();
1487 copy_prog_name ();
1488 return MSG_HANDLED;
1491 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1492 && !quote && !current_panel->searching) {
1493 if (!only_leading_plus_minus) {
1494 /* Special treatement, since the input line will eat them */
1495 if (parm == '+') {
1496 select_cmd ();
1497 return MSG_HANDLED;
1500 if (parm == '\\' || parm == '-') {
1501 unselect_cmd ();
1502 return MSG_HANDLED;
1505 if (parm == '*') {
1506 reverse_selection_cmd ();
1507 return MSG_HANDLED;
1509 } else if (!command_prompt || !cmdline->buffer[0]) {
1510 /* Special treatement '+', '-', '\', '*' only when this is
1511 * first char on input line
1514 if (parm == '+') {
1515 select_cmd ();
1516 return MSG_HANDLED;
1519 if (parm == '\\' || parm == '-') {
1520 unselect_cmd ();
1521 return MSG_HANDLED;
1524 if (parm == '*') {
1525 reverse_selection_cmd ();
1526 return MSG_HANDLED;
1530 return MSG_NOT_HANDLED;
1532 case DLG_HOTKEY_HANDLED:
1533 if ((get_current_type () == view_listing) && current_panel->searching) {
1534 current_panel->searching = 0;
1535 current_panel->dirty = 1;
1537 return MSG_HANDLED;
1539 case DLG_UNHANDLED_KEY:
1540 if (command_prompt) {
1541 cb_ret_t v;
1543 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1544 if (v == MSG_HANDLED)
1545 return MSG_HANDLED;
1547 if (ctl_x_map_enabled) {
1548 ctl_x_map_enabled = 0;
1549 for (i = 0; main_x_map[i].key; i++)
1550 if (parm == main_x_map[i].key)
1551 return midnight_execute_cmd (main_x_map[i].command);
1552 } else {
1553 for (i = 0; main_map[i].key; i++) {
1554 if (parm == main_map[i].key)
1555 return midnight_execute_cmd (main_map[i].command);
1558 return MSG_NOT_HANDLED;
1560 case DLG_DRAW:
1561 load_hint (1);
1562 /* We handle the special case of the output lines */
1563 if (console_flag && output_lines)
1564 show_console_contents (output_start_y,
1565 LINES - output_lines - keybar_visible -
1566 1, LINES - keybar_visible - 1);
1567 return MSG_HANDLED;
1569 case DLG_POST_KEY:
1570 if (!the_menubar->active)
1571 update_dirty_panels ();
1572 return MSG_HANDLED;
1574 default:
1575 return default_dlg_callback (h, msg, parm);
1579 /* Show current directory in the xterm title */
1580 void
1581 update_xterm_title_path (void)
1583 const char *path;
1584 char host[BUF_TINY];
1585 char *p;
1586 struct passwd *pw = NULL;
1587 char *login = NULL;
1588 int res = 0;
1589 if (xterm_flag && xterm_title) {
1590 path = strip_home_and_password (current_panel->cwd);
1591 res = gethostname(host, sizeof (host));
1592 if ( res ) { /* On success, res = 0 */
1593 host[0] = '\0';
1594 } else {
1595 host[sizeof (host) - 1] = '\0';
1597 pw = getpwuid(getuid());
1598 if ( pw ) {
1599 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1600 } else {
1601 login = g_strdup (host);
1603 p = g_strdup_printf ("mc [%s]:%s", login, path);
1604 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1605 g_free (login);
1606 g_free (p);
1607 if (!alternate_plus_minus)
1608 numeric_keypad_mode ();
1609 fflush (stdout);
1614 * Load new hint and display it.
1615 * IF force is not 0, ignore the timeout.
1617 void
1618 load_hint (int force)
1620 char *hint;
1622 if (!the_hint->widget.parent)
1623 return;
1625 if (!message_visible) {
1626 label_set_text (the_hint, NULL);
1627 return;
1630 hint = get_random_hint (force);
1632 if (hint != NULL) {
1633 if (*hint)
1634 set_hintbar (hint);
1635 g_free (hint);
1636 } else {
1637 char text[BUF_SMALL];
1639 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1640 VERSION);
1641 set_hintbar (text);
1645 static void
1646 setup_panels_and_run_mc (void)
1648 add_widget (midnight_dlg, the_menubar);
1649 add_widget (midnight_dlg, get_panel_widget (0));
1650 add_widget (midnight_dlg, get_panel_widget (1));
1651 add_widget (midnight_dlg, the_hint);
1652 add_widget (midnight_dlg, cmdline);
1653 add_widget (midnight_dlg, the_prompt);
1654 add_widget (midnight_dlg, the_bar);
1655 init_labels ();
1657 if (boot_current_is_left)
1658 dlg_select_widget (get_panel_widget (0));
1659 else
1660 dlg_select_widget (get_panel_widget (1));
1662 /* Run the Midnight Commander if no file was specified in the command line */
1663 run_dlg (midnight_dlg);
1666 /* result must be free'd (I think this should go in util.c) */
1667 static char *
1668 prepend_cwd_on_local (const char *filename)
1670 char *d;
1671 int l;
1673 if (vfs_file_is_local (filename)) {
1674 if (*filename == PATH_SEP) /* an absolute pathname */
1675 return g_strdup (filename);
1676 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1677 mc_get_current_wd (d, MC_MAXPATHLEN);
1678 l = strlen (d);
1679 d[l++] = PATH_SEP;
1680 strcpy (d + l, filename);
1681 canonicalize_pathname (d);
1682 return d;
1683 } else
1684 return g_strdup (filename);
1687 static int
1688 mc_maybe_editor_or_viewer (void)
1690 if (!(view_one_file || edit_one_file))
1691 return 0;
1693 /* Invoke the internal view/edit routine with:
1694 * the default processing and forcing the internal viewer/editor
1696 if (view_one_file) {
1697 char *path = NULL;
1698 path = prepend_cwd_on_local (view_one_file);
1699 view_file (path, 0, 1);
1700 g_free (path);
1702 #ifdef USE_INTERNAL_EDIT
1703 else {
1704 edit_file (edit_one_file, edit_one_file_start_line);
1706 #endif /* USE_INTERNAL_EDIT */
1707 midnight_shutdown = 1;
1708 done_mc ();
1709 return 1;
1712 /* Run the main dialog that occupies the whole screen */
1713 static void
1714 do_nc (void)
1716 int midnight_colors[DLG_COLOR_NUM];
1717 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1718 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1719 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1720 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1722 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1723 "[main]", NULL, DLG_WANT_IDLE);
1725 if (view_one_file || edit_one_file)
1726 setup_dummy_mc ();
1727 else
1728 setup_mc ();
1730 /* start check display_codepage and source_codepage */
1731 check_codeset();
1732 main_map = default_main_map;
1734 if (main_keymap && main_keymap->len > 0)
1735 main_map = (global_key_map_t *) main_keymap->data;
1737 main_x_map = default_main_x_map;
1739 if (main_x_keymap && main_x_keymap->len > 0)
1740 main_x_map = (global_key_map_t *) main_x_keymap->data;
1742 panel_map = default_panel_keymap;
1744 if (panel_keymap && panel_keymap->len > 0) {
1745 panel_map = (global_key_map_t *) panel_keymap->data;
1748 input_map = default_input_keymap;
1750 if (input_keymap && input_keymap->len > 0)
1751 input_map = (global_key_map_t *) input_keymap->data;
1753 /* Check if we were invoked as an editor or file viewer */
1754 if (!mc_maybe_editor_or_viewer ()) {
1755 setup_panels_and_run_mc ();
1757 /* Program end */
1758 midnight_shutdown = 1;
1760 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1761 if (mc_args__last_wd_file && vfs_current_is_local ()) {
1762 last_wd_string = g_strdup (current_panel->cwd);
1764 done_mc ();
1766 destroy_dlg (midnight_dlg);
1767 current_panel = 0;
1768 done_mc_profile ();
1771 /* POSIX version. The only version we support. */
1772 static void
1773 OS_Setup (void)
1775 const char *shell_env = getenv ("SHELL");
1776 const char *mc_libdir;
1778 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1779 struct passwd *pwd;
1780 pwd = getpwuid (geteuid ());
1781 if (pwd != NULL)
1782 shell = g_strdup (pwd->pw_shell);
1783 } else
1784 shell = g_strdup (shell_env);
1786 if ((shell == NULL) || (shell[0] == '\0')) {
1787 g_free (shell);
1788 shell = g_strdup ("/bin/sh");
1791 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1792 /* and can be overriden by the MC_DATADIR environment variable */
1793 mc_libdir = getenv ("MC_DATADIR");
1794 if (mc_libdir != NULL) {
1795 mc_home = g_strdup (mc_libdir);
1796 mc_home_alt = g_strdup (SYSCONFDIR);
1797 } else {
1798 mc_home = g_strdup (SYSCONFDIR);
1799 mc_home_alt = g_strdup (DATADIR);
1802 /* This variable is used by the subshell */
1803 home_dir = getenv ("HOME");
1805 if (!home_dir)
1806 home_dir = mc_home;
1810 static void
1811 sigchld_handler_no_subshell (int sig)
1813 #ifdef __linux__
1814 int pid, status;
1816 if (!console_flag)
1817 return;
1819 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1820 the value of console_flag never changed, we could simply not install
1821 this handler at all if (!console_flag && !use_subshell). */
1823 /* That comment is no longer true. We need to wait() on a sigchld
1824 handler (that's at least what the tarfs code expects currently). */
1826 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1828 if (pid == cons_saver_pid) {
1830 if (WIFSTOPPED (status)) {
1831 /* Someone has stopped cons.saver - restart it */
1832 kill (pid, SIGCONT);
1833 } else {
1834 /* cons.saver has died - disable console saving */
1835 handle_console (CONSOLE_DONE);
1836 console_flag = 0;
1839 /* If we got here, some other child exited; ignore it */
1840 #endif /* __linux__ */
1842 (void) sig;
1845 static void
1846 init_sigchld (void)
1848 struct sigaction sigchld_action;
1850 sigchld_action.sa_handler =
1851 #ifdef HAVE_SUBSHELL_SUPPORT
1852 use_subshell ? sigchld_handler :
1853 #endif /* HAVE_SUBSHELL_SUPPORT */
1854 sigchld_handler_no_subshell;
1856 sigemptyset (&sigchld_action.sa_mask);
1858 #ifdef SA_RESTART
1859 sigchld_action.sa_flags = SA_RESTART;
1860 #else
1861 sigchld_action.sa_flags = 0;
1862 #endif /* !SA_RESTART */
1864 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1865 #ifdef HAVE_SUBSHELL_SUPPORT
1867 * This may happen on QNX Neutrino 6, where SA_RESTART
1868 * is defined but not implemented. Fallback to no subshell.
1870 use_subshell = 0;
1871 #endif /* HAVE_SUBSHELL_SUPPORT */
1875 static void
1876 mc_main__setup_by_args(int argc, char *argv[])
1878 const char *base;
1879 char *tmp;
1881 if (mc_args__nomouse)
1882 use_mouse_p = MOUSE_DISABLED;
1884 #ifdef USE_NETCODE
1885 if (mc_args__netfs_logfile != NULL)
1887 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
1888 #ifdef WITH_SMBFS
1889 smbfs_set_debugf (mc_args__netfs_logfile);
1890 #endif /* WITH_SMBFS */
1893 #ifdef WITH_SMBFS
1894 if (mc_args__debug_level != 0)
1896 smbfs_set_debug (mc_args__debug_level);
1898 #endif /* WITH_SMBFS */
1899 #endif /* USE_NETCODE */
1902 base = x_basename (argv[0]);
1903 tmp = (argc > 0)? argv[1] : NULL;
1906 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
1907 edit_one_file = "";
1908 if (tmp) {
1910 * Check for filename:lineno, followed by an optional colon.
1911 * This format is used by many programs (especially compilers)
1912 * in error messages and warnings. It is supported so that
1913 * users can quickly copy and paste file locations.
1915 char *end = tmp + strlen (tmp), *p = end;
1916 if (p > tmp && p[-1] == ':')
1917 p--;
1918 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
1919 p--;
1920 if (tmp < p && p < end && p[-1] == ':') {
1921 struct stat st;
1922 gchar *fname = g_strndup (tmp, p - 1 - tmp);
1924 * Check that the file before the colon actually exists.
1925 * If it doesn't exist, revert to the old behavior.
1927 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
1928 edit_one_file = fname;
1929 edit_one_file_start_line = atoi (p);
1930 } else {
1931 g_free (fname);
1932 goto try_plus_filename;
1934 } else {
1935 try_plus_filename:
1936 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
1937 int start_line = atoi (tmp);
1938 if (start_line > 0) {
1939 char *file = (argc > 1) ? argv[2] : NULL;
1940 if (file) {
1941 tmp = file;
1942 edit_one_file_start_line = start_line;
1946 edit_one_file = g_strdup (tmp);
1949 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
1950 if (tmp)
1951 view_one_file = g_strdup (tmp);
1952 else {
1953 fputs ("No arguments given to the viewer\n", stderr);
1954 exit (1);
1956 } else {
1957 /* sets the current dir and the other dir */
1958 if (tmp) {
1959 this_dir = g_strdup (tmp);
1960 tmp = (argc > 1) ? argv[2] : NULL;
1961 if (tmp)
1962 other_dir = g_strdup (tmp);
1970 main (int argc, char *argv[])
1972 struct stat s;
1973 char *mc_dir;
1974 GError *error = NULL;
1975 gboolean isInitialized;
1977 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
1978 setlocale (LC_ALL, "");
1979 bindtextdomain ("mc", LOCALEDIR);
1980 textdomain ("mc");
1982 /* Set up temporary directory */
1983 mc_tmpdir ();
1985 OS_Setup ();
1987 str_init_strings (NULL);
1989 vfs_init ();
1991 #ifdef USE_INTERNAL_EDIT
1992 edit_stack_init ();
1993 #endif
1995 #ifdef HAVE_SLANG
1996 SLtt_Ignore_Beep = 1;
1997 #endif
1999 if ( !mc_args_handle (&argc, &argv, "mc"))
2000 return 1;
2001 mc_main__setup_by_args(argc,argv);
2004 /* NOTE: This has to be called before tty_init or whatever routine
2005 calls any define_sequence */
2006 init_key ();
2008 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2009 handle_console (CONSOLE_INIT);
2011 #ifdef HAVE_SUBSHELL_SUPPORT
2012 /* Don't use subshell when invoked as viewer or editor */
2013 if (edit_one_file || view_one_file)
2014 use_subshell = 0;
2016 if (use_subshell)
2017 subshell_get_console_attributes ();
2018 #endif /* HAVE_SUBSHELL_SUPPORT */
2020 /* Install the SIGCHLD handler; must be done before init_subshell() */
2021 init_sigchld ();
2023 /* We need this, since ncurses endwin () doesn't restore the signals */
2024 save_stop_handler ();
2026 /* Must be done before init_subshell, to set up the terminal size: */
2027 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2028 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2030 load_setup ();
2032 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2034 isInitialized = mc_skin_init(&error);
2036 mc_filehighlight = mc_fhl_new (TRUE);
2038 dlg_set_default_colors ();
2040 if ( ! isInitialized ) {
2041 message (D_ERROR, _("Warning"), error->message);
2042 g_error_free(error);
2043 error = NULL;
2046 /* create home directory */
2047 /* do it after the screen library initialization to show the error message */
2048 mc_dir = concat_dir_and_file (home_dir, MC_BASE);
2049 canonicalize_pathname (mc_dir);
2050 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2051 && mkdir (mc_dir, 0700) != 0)
2052 message (D_ERROR, _("Warning"),
2053 _("Cannot create %s directory"), mc_dir);
2054 g_free (mc_dir);
2056 #ifdef HAVE_SUBSHELL_SUPPORT
2057 /* Done here to ensure that the subshell doesn't */
2058 /* inherit the file descriptors opened below, etc */
2059 if (use_subshell)
2060 init_subshell ();
2062 #endif /* HAVE_SUBSHELL_SUPPORT */
2064 /* Removing this from the X code let's us type C-c */
2065 load_key_defs ();
2067 load_keymap_defs ();
2069 /* Also done after init_subshell, to save any shell init file messages */
2070 if (console_flag)
2071 handle_console (CONSOLE_SAVE);
2073 if (alternate_plus_minus)
2074 application_keypad_mode ();
2076 #ifdef HAVE_SUBSHELL_SUPPORT
2077 if (use_subshell) {
2078 prompt = strip_ctrl_codes (subshell_prompt);
2079 if (!prompt)
2080 prompt = "";
2081 } else
2082 #endif /* HAVE_SUBSHELL_SUPPORT */
2083 prompt = (geteuid () == 0) ? "# " : "$ ";
2086 /* Program main loop */
2087 if (!midnight_shutdown)
2088 do_nc ();
2090 /* Save the tree store */
2091 tree_store_save ();
2093 /* Virtual File System shutdown */
2094 vfs_shut ();
2096 flush_extension_file (); /* does only free memory */
2098 mc_fhl_free (&mc_filehighlight);
2099 mc_skin_deinit();
2101 tty_shutdown ();
2103 if (console_flag && !(quit & SUBSHELL_EXIT))
2104 handle_console (CONSOLE_RESTORE);
2105 if (alternate_plus_minus)
2106 numeric_keypad_mode ();
2108 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2110 if (console_flag)
2111 handle_console (CONSOLE_DONE);
2112 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2114 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2115 && !edit_one_file && !view_one_file) {
2116 int last_wd_fd =
2117 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2118 S_IRUSR | S_IWUSR);
2120 if (last_wd_fd != -1) {
2121 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2122 close (last_wd_fd);
2125 g_free (last_wd_string);
2127 g_free (mc_home_alt);
2128 g_free (mc_home);
2129 g_free (shell);
2131 done_key ();
2132 #ifdef HAVE_CHARSET
2133 free_codepages_list ();
2134 #endif
2135 str_uninit_strings ();
2137 g_free (this_dir);
2138 g_free (other_dir);
2140 #ifdef USE_INTERNAL_EDIT
2141 edit_stack_free ();
2142 #endif
2144 return 0;