Merge branch '1618_rpm_spec_fix'
[midnight-commander.git] / src / main.c
blob0bce872417937c8747dcf9a8f82924bdffd4aa13
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 /* Tab size */
258 int option_tab_spacing = 8;
260 /* The value of the other directory, only used when loading the setup */
261 char *other_dir = NULL;
263 /* Only used at program boot */
264 int boot_current_is_left = 1;
266 static char *this_dir = NULL;
268 /* If this is true, then when browsing the tree the other window will
269 * automatically reload it's directory with the contents of the currently
270 * selected directory.
272 int xtree_mode = 0;
274 /* If set, then print to the given file the last directory we were at */
275 static char *last_wd_string = NULL;
276 /* Set to 1 to suppress printing the last directory */
277 static int print_last_revert = 0;
279 /* File name to view if argument was supplied */
280 const char *view_one_file = NULL;
282 /* File name to edit if argument was supplied */
283 const char *edit_one_file = NULL;
285 /* Line to start the editor on */
286 static int edit_one_file_start_line = 0;
288 /* Used so that widgets know if they are being destroyed or
289 shut down */
290 int midnight_shutdown = 0;
292 /* The user's shell */
293 char *shell = NULL;
295 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
296 char *mc_home = NULL;
298 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
299 char *mc_home_alt = NULL;
301 /* Define this function for glib-style error handling */
302 GQuark
303 mc_main_error_quark (void)
305 return g_quark_from_static_string (PACKAGE);
308 GArray *editor_keymap = NULL;
309 GArray *viewer_keymap = NULL;
310 GArray *viewer_hex_keymap = NULL;
311 GArray *main_keymap = NULL;
312 GArray *main_x_keymap = NULL;
313 GArray *panel_keymap = NULL;
314 GArray *input_keymap = NULL;
316 const global_key_map_t *main_map;
317 const global_key_map_t *main_x_map;
319 /* Save current stat of directories to avoid reloading the panels */
320 /* when no modifications have taken place */
321 void
322 save_cwds_stat (void)
324 if (fast_reload) {
325 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
326 if (get_other_type () == view_listing)
327 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
331 #ifdef HAVE_SUBSHELL_SUPPORT
332 void
333 do_update_prompt (void)
335 if (update_prompt) {
336 printf ("\r\n%s", subshell_prompt);
337 fflush (stdout);
338 update_prompt = 0;
341 #endif /* HAVE_SUBSHELL_SUPPORT */
343 void
344 change_panel (void)
346 free_completions (cmdline);
347 dlg_one_down (midnight_dlg);
350 /* Stop MC main dialog and the current dialog if it exists.
351 * Needed to provide fast exit from MC viewer or editor on shell exit */
352 static void
353 stop_dialogs (void)
355 midnight_dlg->running = 0;
356 if (current_dlg) {
357 current_dlg->running = 0;
361 static int
362 quit_cmd_internal (int quiet)
364 int q = quit;
366 if (quiet || !confirm_exit) {
367 q = 1;
368 } else {
369 if (query_dialog
370 (_(" The Midnight Commander "),
371 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
372 2, _("&Yes"), _("&No")) == 0)
373 q = 1;
375 if (q) {
376 #ifdef HAVE_SUBSHELL_SUPPORT
377 if (!use_subshell)
378 stop_dialogs ();
379 else if ((q = exit_subshell ()))
380 #endif
381 stop_dialogs ();
383 if (q)
384 quit |= 1;
385 return quit;
388 static void
389 quit_cmd (void)
391 quit_cmd_internal (0);
394 void
395 quiet_quit_cmd (void)
397 print_last_revert = 1;
398 quit_cmd_internal (1);
401 /* Wrapper for do_subshell_chdir, check for availability of subshell */
402 void
403 subshell_chdir (const char *directory)
405 #ifdef HAVE_SUBSHELL_SUPPORT
406 if (use_subshell) {
407 if (vfs_current_is_local ())
408 do_subshell_chdir (directory, 0, 1);
410 #endif /* HAVE_SUBSHELL_SUPPORT */
413 void
414 directory_history_add (struct WPanel *panel, const char *dir)
416 char *tmp;
418 tmp = g_strdup (dir);
419 strip_password (tmp, 1);
421 panel->dir_history = list_append_unique (panel->dir_history, tmp);
425 * If we moved to the parent directory move the selection pointer to
426 * the old directory name; If we leave VFS dir, remove FS specificator.
428 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
430 static const char *
431 get_parent_dir_name (const char *cwd, const char *lwd)
433 const char *p;
434 if (strlen (lwd) > strlen (cwd))
435 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
436 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
437 cwd[1] == '\0'))) {
438 return (p + 1);
440 return NULL;
444 * Changes the current directory of the panel.
445 * Don't record change in the directory history.
447 static int
448 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
450 const char *directory;
451 char *olddir;
452 char temp[MC_MAXPATHLEN];
453 char *translated_url;
455 if (cd_type == cd_parse_command) {
456 while (*new_dir == ' ')
457 new_dir++;
460 olddir = g_strdup (panel->cwd);
461 new_dir = translated_url = vfs_translate_url (new_dir);
463 /* Convert *new_path to a suitable pathname, handle ~user */
465 if (cd_type == cd_parse_command) {
466 if (!strcmp (new_dir, "-")) {
467 strcpy (temp, panel->lwd);
468 new_dir = temp;
471 directory = *new_dir ? new_dir : home_dir;
473 if (mc_chdir (directory) == -1) {
474 strcpy (panel->cwd, olddir);
475 g_free (olddir);
476 g_free (translated_url);
477 return 0;
479 g_free (translated_url);
481 /* Success: save previous directory, shutdown status of previous dir */
482 strcpy (panel->lwd, olddir);
483 free_completions (cmdline);
485 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
487 vfs_release_path (olddir);
489 subshell_chdir (panel->cwd);
491 /* Reload current panel */
492 panel_clean_dir (panel);
493 panel->count =
494 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
495 panel->reverse, panel->case_sensitive,
496 panel->exec_first, panel->filter);
497 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
498 load_hint (0);
499 panel->dirty = 1;
500 update_xterm_title_path ();
502 g_free (olddir);
504 return 1;
508 * Changes the current directory of the panel.
509 * Record change in the directory history.
512 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
514 int r;
516 r = _do_panel_cd (panel, new_dir, cd_type);
517 if (r)
518 directory_history_add (panel, panel->cwd);
519 return r;
523 do_cd (const char *new_dir, enum cd_enum exact)
525 return (do_panel_cd (current_panel, new_dir, exact));
528 void
529 directory_history_next (WPanel *panel)
531 GList *nextdir;
533 nextdir = g_list_next (panel->dir_history);
535 if (!nextdir)
536 return;
538 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
539 panel->dir_history = nextdir;
542 void
543 directory_history_prev (WPanel *panel)
545 GList *prevdir;
547 prevdir = g_list_previous (panel->dir_history);
549 if (!prevdir)
550 return;
552 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
553 panel->dir_history = prevdir;
556 void
557 directory_history_list (WPanel *panel)
559 char *s;
561 if (!panel->dir_history)
562 return;
564 s = show_hist (panel->dir_history, &panel->widget);
566 if (!s)
567 return;
569 if (_do_panel_cd (panel, s, cd_exact))
570 directory_history_add (panel, panel->cwd);
571 else
572 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
573 g_free (s);
576 #ifdef HAVE_SUBSHELL_SUPPORT
578 load_prompt (int fd, void *unused)
580 (void) fd;
581 (void) unused;
583 if (!read_subshell_prompt ())
584 return 0;
586 /* Don't actually change the prompt if it's invisible */
587 if (current_dlg == midnight_dlg && command_prompt) {
588 char *tmp_prompt;
589 int prompt_len;
591 tmp_prompt = strip_ctrl_codes (subshell_prompt);
592 prompt_len = str_term_width1 (tmp_prompt);
594 /* Check for prompts too big */
595 if (COLS > 8 && prompt_len > COLS - 8) {
596 tmp_prompt[COLS - 8] = '\0';
597 prompt_len = COLS - 8;
599 prompt = tmp_prompt;
600 label_set_text (the_prompt, prompt);
601 winput_set_origin ((WInput *) cmdline, prompt_len,
602 COLS - prompt_len);
604 /* since the prompt has changed, and we are called from one of the
605 * tty_get_event channels, the prompt updating does not take place
606 * automatically: force a cursor update and a screen refresh
608 update_cursor (midnight_dlg);
609 mc_refresh ();
611 update_prompt = 1;
612 return 0;
614 #endif /* HAVE_SUBSHELL_SUPPORT */
616 void
617 sort_cmd (void)
619 WPanel *p;
620 const panel_field_t *sort_order;
622 if (!SELECTED_IS_PANEL)
623 return;
625 p = MENU_PANEL;
626 sort_order = sort_box (p->current_sort_field, &p->reverse,
627 &p->case_sensitive,
628 &p->exec_first);
630 panel_set_sort_order (p, sort_order);
634 static void
635 treebox_cmd (void)
637 char *sel_dir;
639 sel_dir = tree_box (selection (current_panel)->fname);
640 if (sel_dir) {
641 do_cd (sel_dir, cd_exact);
642 g_free (sel_dir);
646 #ifdef LISTMODE_EDITOR
647 static void
648 listmode_cmd (void)
650 char *newmode;
652 if (get_current_type () != view_listing)
653 return;
655 newmode = listmode_edit (current_panel->user_format);
656 if (!newmode)
657 return;
659 g_free (current_panel->user_format);
660 current_panel->list_type = list_user;
661 current_panel->user_format = newmode;
662 set_panel_formats (current_panel);
664 do_refresh ();
666 #endif /* LISTMODE_EDITOR */
668 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
669 static menu_entry LeftMenu[] = {
670 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
671 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
672 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
673 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
674 {' ', "", NULL_HOTKEY, 0},
675 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
676 {' ', "", NULL_HOTKEY, 0},
677 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
678 #ifdef HAVE_CHARSET
679 {' ', "",NULL_HOTKEY, 0},
680 {' ', N_("&Encoding... M-e"), NULL_HOTKEY, encoding_cmd},
681 #endif
682 #ifdef USE_NETCODE
683 {' ', "", NULL_HOTKEY, 0},
684 #ifdef ENABLE_VFS_MCFS
685 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
686 #endif
687 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
688 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
689 #ifdef WITH_SMBFS
690 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
691 #endif
692 #endif
693 {' ', "", NULL_HOTKEY, 0},
694 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
697 static menu_entry RightMenu[] = {
698 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
699 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
700 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
701 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
702 {' ', "", NULL_HOTKEY, 0},
703 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
704 {' ', "", NULL_HOTKEY, 0},
705 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
706 #ifdef HAVE_CHARSET
707 {' ', "",NULL_HOTKEY, 0},
708 {' ', N_("&Encoding... M-e"), NULL_HOTKEY, encoding_cmd},
709 #endif
710 #ifdef USE_NETCODE
711 {' ', "", NULL_HOTKEY, 0},
712 #ifdef ENABLE_VFS_MCFS
713 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
714 #endif
715 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
716 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
717 #ifdef WITH_SMBFS
718 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
719 #endif
720 #endif
721 {' ', "", NULL_HOTKEY, 0},
722 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
725 static menu_entry FileMenu[] = {
726 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
727 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
728 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
729 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
730 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
731 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
732 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
733 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
734 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
735 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
736 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
737 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
738 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
739 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
740 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
741 {' ', "", NULL_HOTKEY, 0},
742 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
743 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
744 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
745 {' ', "", NULL_HOTKEY, 0},
746 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
749 static menu_entry CmdMenu[] = {
750 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
751 /* I know, I'm lazy, but the tree widget when it's not running
752 * as a panel still has some problems, I have not yet finished
753 * the WTree widget port, sorry.
755 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
756 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
757 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
758 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
759 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
760 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
761 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
762 {' ', "", NULL_HOTKEY, 0},
763 {' ', N_("Command &history M-h"), NULL_HOTKEY, history_cmd},
764 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
765 #ifdef USE_VFS
766 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
767 #endif
768 #ifdef WITH_BACKGROUND
769 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
770 #endif
771 {' ', "", NULL_HOTKEY, 0},
772 #ifdef USE_EXT2FSLIB
773 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
774 #endif
775 #ifdef LISTMODE_EDITOR
776 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
777 #endif
778 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
779 {' ', "", NULL_HOTKEY, 0},
780 #endif
781 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
782 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd},
783 {' ', N_("Edit hi&ghlighting group file"), NULL_HOTKEY, edit_fhl_cmd}
786 /* Must keep in sync with the constants in menu_cmd */
787 static menu_entry OptMenu[] = {
788 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
789 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
790 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
791 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
792 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
793 #ifdef USE_VFS
794 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
795 #endif /* !USE_VFS */
796 {' ', "", NULL_HOTKEY, 0},
797 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
800 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
802 static Menu *MenuBar[5];
804 void
805 init_menu (void)
807 MenuBar[0] =
808 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
809 LeftMenu, menu_entries (LeftMenu),
810 "[Left and Right Menus]");
811 MenuBar[1] =
812 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
813 "[File Menu]");
814 MenuBar[2] =
815 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
816 "[Command Menu]");
817 MenuBar[3] =
818 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
819 "[Options Menu]");
820 MenuBar[4] =
821 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
822 RightMenu, menu_entries (RightMenu),
823 "[Left and Right Menus]");
826 void
827 done_menu (void)
829 int i;
831 for (i = 0; i < 5; i++) {
832 destroy_menu (MenuBar[i]);
836 static void
837 menu_last_selected_cmd (void)
839 the_menubar->active = 1;
840 the_menubar->dropped = drop_menus;
841 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
842 dlg_select_widget (the_menubar);
845 static void
846 menu_cmd (void)
848 if (the_menubar->active)
849 return;
851 if ((get_current_index () == 0) ^ (!current_panel->active))
852 the_menubar->selected = 0;
853 else
854 the_menubar->selected = 4;
855 menu_last_selected_cmd ();
858 /* Flag toggling functions */
859 void
860 toggle_fast_reload (void)
862 fast_reload = !fast_reload;
863 if (fast_reload_w == 0 && fast_reload) {
864 message (D_NORMAL, _(" Information "),
866 (" Using the fast reload option may not reflect the exact \n"
867 " directory contents. In this case you'll need to do a \n"
868 " manual reload of the directory. See the man page for \n"
869 " the details. "));
870 fast_reload_w = 1;
874 void
875 toggle_mix_all_files (void)
877 mix_all_files = !mix_all_files;
878 update_panels (UP_RELOAD, UP_KEEPSEL);
881 void
882 toggle_show_backup (void)
884 show_backups = !show_backups;
885 update_panels (UP_RELOAD, UP_KEEPSEL);
888 void
889 toggle_show_hidden (void)
891 show_dot_files = !show_dot_files;
892 update_panels (UP_RELOAD, UP_KEEPSEL);
895 void
896 toggle_kilobyte_si (void)
898 kilobyte_si = !kilobyte_si;
899 update_panels (UP_RELOAD, UP_KEEPSEL);
903 * Just a hack for allowing url-like pathnames to be accepted from the
904 * command line.
906 static void
907 translated_mc_chdir (char *dir)
909 char *newdir;
911 newdir = vfs_translate_url (dir);
912 mc_chdir (newdir);
913 g_free (newdir);
916 static void
917 create_panels (void)
919 int current_index;
920 int other_index;
921 int current_mode;
922 int other_mode;
923 char original_dir[1024];
925 original_dir[0] = 0;
927 if (boot_current_is_left) {
928 current_index = 0;
929 other_index = 1;
930 current_mode = startup_left_mode;
931 other_mode = startup_right_mode;
932 } else {
933 current_index = 1;
934 other_index = 0;
935 current_mode = startup_right_mode;
936 other_mode = startup_left_mode;
938 /* Creates the left panel */
939 if (this_dir) {
940 if (other_dir) {
941 /* Ok, user has specified two dirs, save the original one,
942 * since we may not be able to chdir to the proper
943 * second directory later
945 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
947 translated_mc_chdir (this_dir);
949 set_display_type (current_index, current_mode);
951 /* The other panel */
952 if (other_dir) {
953 if (original_dir[0])
954 translated_mc_chdir (original_dir);
955 translated_mc_chdir (other_dir);
957 set_display_type (other_index, other_mode);
959 if (startup_left_mode == view_listing) {
960 current_panel = left_panel;
961 } else {
962 if (right_panel)
963 current_panel = right_panel;
964 else
965 current_panel = left_panel;
968 /* Create the nice widgets */
969 cmdline = command_new (0, 0, 0);
970 the_prompt = label_new (0, 0, prompt);
971 the_prompt->transparent = 1;
972 the_bar = buttonbar_new (keybar_visible);
974 the_hint = label_new (0, 0, 0);
975 the_hint->transparent = 1;
976 the_hint->auto_adjust_cols = 0;
977 the_hint->widget.cols = COLS;
979 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
982 static void
983 copy_current_pathname (void)
985 char *cwd_path;
986 if (!command_prompt)
987 return;
989 cwd_path = remove_encoding_from_path (current_panel->cwd);
990 command_insert (cmdline, cwd_path, 0);
992 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
993 command_insert (cmdline, PATH_SEP_STR, 0);
994 g_free (cwd_path);
997 static void
998 copy_other_pathname (void)
1000 char *cwd_path;
1002 if (get_other_type () != view_listing)
1003 return;
1005 if (!command_prompt)
1006 return;
1008 cwd_path = remove_encoding_from_path (other_panel->cwd);
1009 command_insert (cmdline, cwd_path, 0);
1011 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1012 command_insert (cmdline, PATH_SEP_STR, 0);
1013 g_free (cwd_path);
1016 static void
1017 copy_readlink (WPanel *panel)
1019 if (!command_prompt)
1020 return;
1021 if (S_ISLNK (selection (panel)->st.st_mode)) {
1022 char buffer[MC_MAXPATHLEN];
1023 char *p =
1024 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1025 int i;
1027 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1028 g_free (p);
1029 if (i > 0) {
1030 buffer[i] = 0;
1031 command_insert (cmdline, buffer, 1);
1036 static void
1037 copy_current_readlink (void)
1039 copy_readlink (current_panel);
1042 static void
1043 copy_other_readlink (void)
1045 if (get_other_type () != view_listing)
1046 return;
1047 copy_readlink (other_panel);
1050 /* Insert the selected file name into the input line */
1051 static void
1052 copy_prog_name (void)
1054 char *tmp;
1055 if (!command_prompt)
1056 return;
1058 if (get_current_type () == view_tree) {
1059 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1060 tmp = tree_selected_name (tree);
1061 } else
1062 tmp = selection (current_panel)->fname;
1064 command_insert (cmdline, tmp, 1);
1067 static void
1068 copy_tagged (WPanel *panel)
1070 int i;
1072 if (!command_prompt)
1073 return;
1074 input_disable_update (cmdline);
1075 if (panel->marked) {
1076 for (i = 0; i < panel->count; i++) {
1077 if (panel->dir.list[i].f.marked)
1078 command_insert (cmdline, panel->dir.list[i].fname, 1);
1080 } else {
1081 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1084 input_enable_update (cmdline);
1087 static void
1088 copy_current_tagged (void)
1090 copy_tagged (current_panel);
1093 static void
1094 copy_other_tagged (void)
1096 if (get_other_type () != view_listing)
1097 return;
1098 copy_tagged (other_panel);
1101 static void
1102 init_labels (void)
1104 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1105 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1106 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1107 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1110 static int ctl_x_map_enabled = 0;
1112 static void
1113 ctl_x_cmd (void)
1115 ctl_x_map_enabled = 1;
1118 static cb_ret_t
1119 midnight_execute_cmd(int command)
1121 cb_ret_t res = MSG_HANDLED;
1123 switch (command) {
1124 case CK_MenuLastSelectedCmd:
1125 menu_last_selected_cmd ();
1126 break;
1127 case CK_QuietQuitCmd:
1128 quiet_quit_cmd ();
1129 break;
1130 case CK_SingleDirsizeCmd:
1131 smart_dirsize_cmd ();
1132 break;
1133 case CK_CopyCurrentPathname:
1134 copy_current_pathname ();
1135 break;
1136 case CK_CopyOtherPathname:
1137 copy_other_pathname ();
1138 break;
1139 case CK_QuickCdCmd:
1140 quick_cd_cmd ();
1141 break;
1142 case CK_QuickChdirCmd:
1143 quick_chdir_cmd ();
1144 break;
1145 case CK_SuspendCmd:
1146 suspend_cmd ();
1147 break;
1148 case CK_FilteredViewCmd:
1149 filtered_view_cmd ();
1150 break;
1151 case CK_FindCmd:
1152 find_cmd ();
1153 break;
1154 case CK_RereadCmd:
1155 reread_cmd ();
1156 break;
1157 case CK_ToggleListingCmd:
1158 toggle_listing_cmd ();
1159 break;
1160 case CK_SwapCmd:
1161 swap_cmd ();
1162 break;
1163 case CK_ShowCommandLine:
1164 view_other_cmd ();
1165 break;
1166 case CK_QuitCmd:
1167 quit_cmd ();
1168 break;
1169 case CK_CompareDirsCmd:
1170 compare_dirs_cmd ();
1171 break;
1172 case CK_ReselectVfs:
1173 reselect_vfs ();
1174 break;
1175 case CK_CopyCurrentTagged:
1176 copy_current_tagged ();
1177 break;
1178 case CK_CopyOtherTarget:
1179 copy_other_tagged ();
1180 break;
1181 case CK_CopyCurrentReadlink:
1182 copy_current_readlink ();
1183 break;
1184 case CK_CopyOtherReadlink:
1185 copy_other_readlink ();
1186 break;
1187 case CK_ChmodCmd:
1188 chmod_cmd ();
1189 break;
1190 case CK_ChownCmd:
1191 chown_cmd ();
1192 break;
1193 case CK_LinkCmd:
1194 link_cmd () ;
1195 break;
1196 case CK_SymlinkCmd:
1197 symlink_cmd ();
1198 break;
1199 case CK_EditSymlinkCmd:
1200 edit_symlink_cmd ();
1201 break;
1202 case CK_InfoCmd:
1203 info_cmd_no_menu ();
1204 break;
1205 case CK_QuickViewCmd:
1206 quick_cmd_no_menu ();
1207 break;
1208 case CK_AddHotlist:
1209 add2hotlist_cmd ();
1210 break;
1211 case CK_ExternalPanelize:
1212 external_panelize ();
1213 break;
1214 #ifdef WITH_BACKGROUND
1215 case CK_JobsCmd:
1216 jobs_cmd ();
1217 break;
1218 #endif
1219 case CK_ToggleShowHidden:
1220 toggle_show_hidden ();
1221 break;
1222 case CK_StartExtMap1:
1223 ctl_x_cmd ();
1224 break;
1225 default:
1226 res = MSG_NOT_HANDLED;
1229 return res;
1232 static void
1233 setup_pre (void)
1235 /* Call all the inits */
1237 #ifdef HAVE_SLANG
1238 tty_display_8bit (full_eight_bits != 0);
1239 #else
1240 tty_display_8bit (eight_bit_clean != 0);
1241 #endif
1244 static void
1245 init_xterm_support (void)
1247 const char *termvalue;
1249 termvalue = getenv ("TERM");
1250 if (!termvalue || !(*termvalue)) {
1251 fputs (_("The TERM environment variable is unset!\n"), stderr);
1252 exit (1);
1255 /* Check mouse capabilities */
1256 xmouse_seq = tty_tgetstr ("Km");
1258 if (strcmp (termvalue, "cygwin") == 0) {
1259 mc_args__force_xterm = 1;
1260 use_mouse_p = MOUSE_DISABLED;
1263 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1264 || strncmp (termvalue, "konsole", 7) == 0
1265 || strncmp (termvalue, "rxvt", 4) == 0
1266 || strcmp (termvalue, "Eterm") == 0
1267 || strcmp (termvalue, "dtterm") == 0) {
1268 xterm_flag = 1;
1270 /* Default to the standard xterm sequence */
1271 if (!xmouse_seq) {
1272 xmouse_seq = ESC_STR "[M";
1275 /* Enable mouse unless explicitly disabled by --nomouse */
1276 if (use_mouse_p != MOUSE_DISABLED) {
1277 const char *color_term = getenv ("COLORTERM");
1278 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1279 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1280 strcmp (termvalue, "Eterm") == 0) {
1281 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1282 } else {
1283 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1289 static void
1290 setup_mc (void)
1292 setup_pre ();
1293 init_menu ();
1294 create_panels ();
1295 setup_panels ();
1297 #ifdef HAVE_SUBSHELL_SUPPORT
1298 if (use_subshell)
1299 add_select_channel (subshell_pty, load_prompt, 0);
1300 #endif /* !HAVE_SUBSHELL_SUPPORT */
1302 tty_setup_sigwinch (sigwinch_handler);
1304 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1306 init_xterm_support ();
1307 init_mouse ();
1310 static void
1311 setup_dummy_mc (void)
1313 char d[MC_MAXPATHLEN];
1315 mc_get_current_wd (d, MC_MAXPATHLEN);
1316 setup_mc ();
1317 mc_chdir (d);
1320 static void check_codeset()
1322 const char *_system_codepage = NULL;
1323 #ifdef HAVE_CHARSET
1324 const char *_source_codepage = NULL;
1325 const char *_display_codepage = NULL;
1326 int profile_changed = 0;
1328 #define CONFY 16
1329 #define CONFX 54
1331 if ( !skip_check_codeset ) {
1333 QuickWidget ecs_widgets [] = {
1334 QUICK_BUTTON (4, 6, 13, CONFY, N_("&Skip"), B_EXIT, NULL),
1335 QUICK_BUTTON (1, 11, 13, CONFY, N_("&Fix it"), B_ENTER, NULL),
1336 QUICK_CHECKBOX (1, 13, 11, CONFY, N_("don't ask again"), &skip_check_codeset),
1337 QUICK_LABEL (2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
1338 "or source codeset (in mcedit ctrl-t) \n"
1339 "does not match one set via locale. \n"
1340 "Set correct codeset manually or press <<Fix it>> \n"
1341 "to set locale default.\n\n"
1342 "Or set \'don't ask again\' and press <<Skip>>")),
1343 QUICK_END
1346 QuickDialog ecs =
1348 CONFX, CONFY, -1, -1, N_(" Confirmation "),
1349 "[Confirmation]", ecs_widgets, FALSE
1353 _system_codepage = str_detect_termencoding();
1354 _source_codepage = get_codepage_id (source_codepage);
1355 _display_codepage = get_codepage_id (display_codepage);
1356 if ( (strcmp (_system_codepage, _display_codepage)) ||
1357 (strcmp (_system_codepage, _source_codepage)) ) {
1358 if (quick_dialog (&ecs) == B_ENTER){
1359 display_codepage = get_codepage_index (_system_codepage);
1360 cp_display = get_codepage_id (display_codepage);
1361 if ( !strcmp (cp_display, _system_codepage)) {
1362 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1363 mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
1364 display_codepage = get_codepage_index ( cp_display );
1365 utf8_display = str_isutf8 (_system_codepage);
1366 source_codepage = display_codepage;
1367 cp_source = cp_display;
1368 profile_changed = 1;
1369 } else {
1370 utf8_display = str_isutf8 (_system_codepage);
1371 cp_source = cp_display = _system_codepage;
1373 } else {
1374 if ( skip_check_codeset ) {
1375 mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
1376 profile_changed = 1;
1380 if ( profile_changed )
1381 save_configure ();
1383 #else /* HAVE_CHARSET */
1384 _system_codepage = str_detect_termencoding();
1385 utf8_display = str_isutf8 (_system_codepage);
1386 #endif /* HAVE_CHARSET */
1389 static void
1390 done_screen (void)
1392 if (!(quit & SUBSHELL_EXIT))
1393 clr_scr ();
1394 tty_reset_shell_mode ();
1395 tty_noraw_mode ();
1396 tty_keypad (FALSE);
1397 tty_colors_done ();
1400 static void
1401 done_mc (void)
1403 disable_mouse ();
1405 done_menu ();
1407 /* Setup shutdown
1409 * We sync the profiles since the hotlist may have changed, while
1410 * we only change the setup data if we have the auto save feature set
1413 if (auto_save_setup)
1414 save_setup (); /* does also call save_hotlist */
1415 else {
1416 save_hotlist ();
1417 save_panel_types ();
1419 done_screen ();
1420 vfs_add_current_stamps ();
1423 /* This should be called after destroy_dlg since panel widgets
1424 * save their state on the profiles
1426 static void
1427 done_mc_profile (void)
1429 done_setup ();
1432 static cb_ret_t
1433 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1435 int i;
1437 switch (msg) {
1439 case DLG_IDLE:
1440 /* We only need the first idle event */
1441 set_idle_proc (h, 0);
1442 if (auto_menu) {
1443 user_file_menu_cmd ();
1445 return MSG_HANDLED;
1447 case DLG_KEY:
1448 if (ctl_x_map_enabled) {
1449 ctl_x_map_enabled = 0;
1450 for (i = 0; main_x_map[i].key; i++)
1451 if (parm == main_x_map[i].key)
1452 return midnight_execute_cmd (main_x_map[i].command);
1455 /* FIXME: should handle all menu shortcuts before this point */
1456 if (the_menubar->active)
1457 return MSG_NOT_HANDLED;
1459 if (parm == KEY_F (10)) {
1460 quit_cmd ();
1461 return MSG_HANDLED;
1464 if (parm == '\t')
1465 free_completions (cmdline);
1467 if (parm == '\n') {
1468 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1469 cmdline->buffer[i] == '\t'); i++);
1470 if (cmdline->buffer[i]) {
1471 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1472 return MSG_HANDLED;
1474 stuff (cmdline, "", 0);
1475 cmdline->point = 0;
1478 /* Ctrl-Enter and Alt-Enter */
1479 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1480 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1481 copy_prog_name ();
1482 return MSG_HANDLED;
1485 /* Ctrl-Shift-Enter */
1486 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1487 copy_current_pathname ();
1488 copy_prog_name ();
1489 return MSG_HANDLED;
1492 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1493 && !quote && !current_panel->searching) {
1494 if (!only_leading_plus_minus) {
1495 /* Special treatement, since the input line will eat them */
1496 if (parm == '+') {
1497 select_cmd ();
1498 return MSG_HANDLED;
1501 if (parm == '\\' || parm == '-') {
1502 unselect_cmd ();
1503 return MSG_HANDLED;
1506 if (parm == '*') {
1507 reverse_selection_cmd ();
1508 return MSG_HANDLED;
1510 } else if (!command_prompt || !cmdline->buffer[0]) {
1511 /* Special treatement '+', '-', '\', '*' only when this is
1512 * first char on input line
1515 if (parm == '+') {
1516 select_cmd ();
1517 return MSG_HANDLED;
1520 if (parm == '\\' || parm == '-') {
1521 unselect_cmd ();
1522 return MSG_HANDLED;
1525 if (parm == '*') {
1526 reverse_selection_cmd ();
1527 return MSG_HANDLED;
1531 return MSG_NOT_HANDLED;
1533 case DLG_HOTKEY_HANDLED:
1534 if ((get_current_type () == view_listing) && current_panel->searching) {
1535 current_panel->searching = 0;
1536 current_panel->dirty = 1;
1538 return MSG_HANDLED;
1540 case DLG_UNHANDLED_KEY:
1541 if (command_prompt) {
1542 cb_ret_t v;
1544 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1545 if (v == MSG_HANDLED)
1546 return MSG_HANDLED;
1548 if (ctl_x_map_enabled) {
1549 ctl_x_map_enabled = 0;
1550 for (i = 0; main_x_map[i].key; i++)
1551 if (parm == main_x_map[i].key)
1552 return midnight_execute_cmd (main_x_map[i].command);
1553 } else {
1554 for (i = 0; main_map[i].key; i++) {
1555 if (parm == main_map[i].key)
1556 return midnight_execute_cmd (main_map[i].command);
1559 return MSG_NOT_HANDLED;
1561 case DLG_DRAW:
1562 load_hint (1);
1563 /* We handle the special case of the output lines */
1564 if (console_flag && output_lines)
1565 show_console_contents (output_start_y,
1566 LINES - output_lines - keybar_visible -
1567 1, LINES - keybar_visible - 1);
1568 return MSG_HANDLED;
1570 case DLG_POST_KEY:
1571 if (!the_menubar->active)
1572 update_dirty_panels ();
1573 return MSG_HANDLED;
1575 default:
1576 return default_dlg_callback (h, msg, parm);
1580 /* Show current directory in the xterm title */
1581 void
1582 update_xterm_title_path (void)
1584 const char *path;
1585 char host[BUF_TINY];
1586 char *p;
1587 struct passwd *pw = NULL;
1588 char *login = NULL;
1589 int res = 0;
1590 if (xterm_flag && xterm_title) {
1591 path = strip_home_and_password (current_panel->cwd);
1592 res = gethostname(host, sizeof (host));
1593 if ( res ) { /* On success, res = 0 */
1594 host[0] = '\0';
1595 } else {
1596 host[sizeof (host) - 1] = '\0';
1598 pw = getpwuid(getuid());
1599 if ( pw ) {
1600 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1601 } else {
1602 login = g_strdup (host);
1604 p = g_strdup_printf ("mc [%s]:%s", login, path);
1605 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1606 g_free (login);
1607 g_free (p);
1608 if (!alternate_plus_minus)
1609 numeric_keypad_mode ();
1610 fflush (stdout);
1615 * Load new hint and display it.
1616 * IF force is not 0, ignore the timeout.
1618 void
1619 load_hint (int force)
1621 char *hint;
1623 if (!the_hint->widget.parent)
1624 return;
1626 if (!message_visible) {
1627 label_set_text (the_hint, NULL);
1628 return;
1631 hint = get_random_hint (force);
1633 if (hint != NULL) {
1634 if (*hint)
1635 set_hintbar (hint);
1636 g_free (hint);
1637 } else {
1638 char text[BUF_SMALL];
1640 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1641 VERSION);
1642 set_hintbar (text);
1646 static void
1647 setup_panels_and_run_mc (void)
1649 add_widget (midnight_dlg, the_menubar);
1650 add_widget (midnight_dlg, get_panel_widget (0));
1651 add_widget (midnight_dlg, get_panel_widget (1));
1652 add_widget (midnight_dlg, the_hint);
1653 add_widget (midnight_dlg, cmdline);
1654 add_widget (midnight_dlg, the_prompt);
1655 add_widget (midnight_dlg, the_bar);
1656 init_labels ();
1658 if (boot_current_is_left)
1659 dlg_select_widget (get_panel_widget (0));
1660 else
1661 dlg_select_widget (get_panel_widget (1));
1663 /* Run the Midnight Commander if no file was specified in the command line */
1664 run_dlg (midnight_dlg);
1667 /* result must be free'd (I think this should go in util.c) */
1668 static char *
1669 prepend_cwd_on_local (const char *filename)
1671 char *d;
1672 int l;
1674 if (vfs_file_is_local (filename)) {
1675 if (*filename == PATH_SEP) /* an absolute pathname */
1676 return g_strdup (filename);
1677 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1678 mc_get_current_wd (d, MC_MAXPATHLEN);
1679 l = strlen (d);
1680 d[l++] = PATH_SEP;
1681 strcpy (d + l, filename);
1682 canonicalize_pathname (d);
1683 return d;
1684 } else
1685 return g_strdup (filename);
1688 static int
1689 mc_maybe_editor_or_viewer (void)
1691 if (!(view_one_file || edit_one_file))
1692 return 0;
1694 /* Invoke the internal view/edit routine with:
1695 * the default processing and forcing the internal viewer/editor
1697 if (view_one_file) {
1698 char *path = NULL;
1699 path = prepend_cwd_on_local (view_one_file);
1700 view_file (path, 0, 1);
1701 g_free (path);
1703 #ifdef USE_INTERNAL_EDIT
1704 else {
1705 edit_file (edit_one_file, edit_one_file_start_line);
1707 #endif /* USE_INTERNAL_EDIT */
1708 midnight_shutdown = 1;
1709 done_mc ();
1710 return 1;
1713 /* Run the main dialog that occupies the whole screen */
1714 static void
1715 do_nc (void)
1717 int midnight_colors[DLG_COLOR_NUM];
1718 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1719 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1720 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1721 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1723 panel_init();
1725 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1726 "[main]", NULL, DLG_WANT_IDLE);
1728 if (view_one_file || edit_one_file)
1729 setup_dummy_mc ();
1730 else
1731 setup_mc ();
1733 /* start check display_codepage and source_codepage */
1734 check_codeset();
1735 main_map = default_main_map;
1737 if (main_keymap && main_keymap->len > 0)
1738 main_map = (global_key_map_t *) main_keymap->data;
1740 main_x_map = default_main_x_map;
1742 if (main_x_keymap && main_x_keymap->len > 0)
1743 main_x_map = (global_key_map_t *) main_x_keymap->data;
1745 panel_map = default_panel_keymap;
1747 if (panel_keymap && panel_keymap->len > 0) {
1748 panel_map = (global_key_map_t *) panel_keymap->data;
1751 input_map = default_input_keymap;
1753 if (input_keymap && input_keymap->len > 0)
1754 input_map = (global_key_map_t *) input_keymap->data;
1756 /* Check if we were invoked as an editor or file viewer */
1757 if (!mc_maybe_editor_or_viewer ()) {
1758 setup_panels_and_run_mc ();
1760 /* Program end */
1761 midnight_shutdown = 1;
1763 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1764 if (mc_args__last_wd_file && vfs_current_is_local ()) {
1765 last_wd_string = g_strdup (current_panel->cwd);
1767 done_mc ();
1769 destroy_dlg (midnight_dlg);
1770 panel_deinit();
1771 current_panel = 0;
1772 done_mc_profile ();
1775 /* POSIX version. The only version we support. */
1776 static void
1777 OS_Setup (void)
1779 const char *shell_env = getenv ("SHELL");
1780 const char *mc_libdir;
1782 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1783 struct passwd *pwd;
1784 pwd = getpwuid (geteuid ());
1785 if (pwd != NULL)
1786 shell = g_strdup (pwd->pw_shell);
1787 } else
1788 shell = g_strdup (shell_env);
1790 if ((shell == NULL) || (shell[0] == '\0')) {
1791 g_free (shell);
1792 shell = g_strdup ("/bin/sh");
1795 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1796 /* and can be overriden by the MC_DATADIR environment variable */
1797 mc_libdir = getenv ("MC_DATADIR");
1798 if (mc_libdir != NULL) {
1799 mc_home = g_strdup (mc_libdir);
1800 mc_home_alt = g_strdup (SYSCONFDIR);
1801 } else {
1802 mc_home = g_strdup (SYSCONFDIR);
1803 mc_home_alt = g_strdup (DATADIR);
1806 /* This variable is used by the subshell */
1807 home_dir = getenv ("HOME");
1809 if (!home_dir)
1810 home_dir = mc_home;
1814 static void
1815 sigchld_handler_no_subshell (int sig)
1817 #ifdef __linux__
1818 int pid, status;
1820 if (!console_flag)
1821 return;
1823 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1824 the value of console_flag never changed, we could simply not install
1825 this handler at all if (!console_flag && !use_subshell). */
1827 /* That comment is no longer true. We need to wait() on a sigchld
1828 handler (that's at least what the tarfs code expects currently). */
1830 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1832 if (pid == cons_saver_pid) {
1834 if (WIFSTOPPED (status)) {
1835 /* Someone has stopped cons.saver - restart it */
1836 kill (pid, SIGCONT);
1837 } else {
1838 /* cons.saver has died - disable console saving */
1839 handle_console (CONSOLE_DONE);
1840 console_flag = 0;
1843 /* If we got here, some other child exited; ignore it */
1844 #endif /* __linux__ */
1846 (void) sig;
1849 static void
1850 init_sigchld (void)
1852 struct sigaction sigchld_action;
1854 sigchld_action.sa_handler =
1855 #ifdef HAVE_SUBSHELL_SUPPORT
1856 use_subshell ? sigchld_handler :
1857 #endif /* HAVE_SUBSHELL_SUPPORT */
1858 sigchld_handler_no_subshell;
1860 sigemptyset (&sigchld_action.sa_mask);
1862 #ifdef SA_RESTART
1863 sigchld_action.sa_flags = SA_RESTART;
1864 #else
1865 sigchld_action.sa_flags = 0;
1866 #endif /* !SA_RESTART */
1868 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1869 #ifdef HAVE_SUBSHELL_SUPPORT
1871 * This may happen on QNX Neutrino 6, where SA_RESTART
1872 * is defined but not implemented. Fallback to no subshell.
1874 use_subshell = 0;
1875 #endif /* HAVE_SUBSHELL_SUPPORT */
1879 static void
1880 mc_main__setup_by_args(int argc, char *argv[])
1882 const char *base;
1883 char *tmp;
1885 if (mc_args__nomouse)
1886 use_mouse_p = MOUSE_DISABLED;
1888 #ifdef USE_NETCODE
1889 if (mc_args__netfs_logfile != NULL)
1891 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
1892 #ifdef WITH_SMBFS
1893 smbfs_set_debugf (mc_args__netfs_logfile);
1894 #endif /* WITH_SMBFS */
1897 #ifdef WITH_SMBFS
1898 if (mc_args__debug_level != 0)
1900 smbfs_set_debug (mc_args__debug_level);
1902 #endif /* WITH_SMBFS */
1903 #endif /* USE_NETCODE */
1906 base = x_basename (argv[0]);
1907 tmp = (argc > 0)? argv[1] : NULL;
1910 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
1911 edit_one_file = "";
1912 if (tmp) {
1914 * Check for filename:lineno, followed by an optional colon.
1915 * This format is used by many programs (especially compilers)
1916 * in error messages and warnings. It is supported so that
1917 * users can quickly copy and paste file locations.
1919 char *end = tmp + strlen (tmp), *p = end;
1920 if (p > tmp && p[-1] == ':')
1921 p--;
1922 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
1923 p--;
1924 if (tmp < p && p < end && p[-1] == ':') {
1925 struct stat st;
1926 gchar *fname = g_strndup (tmp, p - 1 - tmp);
1928 * Check that the file before the colon actually exists.
1929 * If it doesn't exist, revert to the old behavior.
1931 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
1932 edit_one_file = fname;
1933 edit_one_file_start_line = atoi (p);
1934 } else {
1935 g_free (fname);
1936 goto try_plus_filename;
1938 } else {
1939 try_plus_filename:
1940 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
1941 int start_line = atoi (tmp);
1942 if (start_line > 0) {
1943 char *file = (argc > 1) ? argv[2] : NULL;
1944 if (file) {
1945 tmp = file;
1946 edit_one_file_start_line = start_line;
1950 edit_one_file = g_strdup (tmp);
1953 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
1954 if (tmp)
1955 view_one_file = g_strdup (tmp);
1956 else {
1957 fputs ("No arguments given to the viewer\n", stderr);
1958 exit (1);
1960 } else {
1961 /* sets the current dir and the other dir */
1962 if (tmp) {
1963 this_dir = g_strdup (tmp);
1964 tmp = (argc > 1) ? argv[2] : NULL;
1965 if (tmp)
1966 other_dir = g_strdup (tmp);
1974 main (int argc, char *argv[])
1976 struct stat s;
1977 char *mc_dir;
1978 GError *error = NULL;
1979 gboolean isInitialized;
1981 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
1982 setlocale (LC_ALL, "");
1983 bindtextdomain ("mc", LOCALEDIR);
1984 textdomain ("mc");
1986 /* Set up temporary directory */
1987 mc_tmpdir ();
1989 OS_Setup ();
1991 str_init_strings (NULL);
1993 vfs_init ();
1995 #ifdef USE_INTERNAL_EDIT
1996 edit_stack_init ();
1997 #endif
1999 #ifdef HAVE_SLANG
2000 SLtt_Ignore_Beep = 1;
2001 #endif
2003 if ( !mc_args_handle (&argc, &argv, "mc"))
2004 return 1;
2005 mc_main__setup_by_args(argc,argv);
2008 /* NOTE: This has to be called before tty_init or whatever routine
2009 calls any define_sequence */
2010 init_key ();
2012 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2013 handle_console (CONSOLE_INIT);
2015 #ifdef HAVE_SUBSHELL_SUPPORT
2016 /* Don't use subshell when invoked as viewer or editor */
2017 if (edit_one_file || view_one_file)
2018 use_subshell = 0;
2020 if (use_subshell)
2021 subshell_get_console_attributes ();
2022 #endif /* HAVE_SUBSHELL_SUPPORT */
2024 /* Install the SIGCHLD handler; must be done before init_subshell() */
2025 init_sigchld ();
2027 /* We need this, since ncurses endwin () doesn't restore the signals */
2028 save_stop_handler ();
2030 /* Must be done before init_subshell, to set up the terminal size: */
2031 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2032 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2034 load_setup ();
2036 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2038 isInitialized = mc_skin_init(&error);
2040 mc_filehighlight = mc_fhl_new (TRUE);
2042 dlg_set_default_colors ();
2044 if ( ! isInitialized ) {
2045 message (D_ERROR, _("Warning"), error->message);
2046 g_error_free(error);
2047 error = NULL;
2050 /* create home directory */
2051 /* do it after the screen library initialization to show the error message */
2052 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2053 canonicalize_pathname (mc_dir);
2054 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2055 && mkdir (mc_dir, 0700) != 0)
2056 message (D_ERROR, _("Warning"),
2057 _("Cannot create %s directory"), mc_dir);
2058 g_free (mc_dir);
2060 #ifdef HAVE_SUBSHELL_SUPPORT
2061 /* Done here to ensure that the subshell doesn't */
2062 /* inherit the file descriptors opened below, etc */
2063 if (use_subshell)
2064 init_subshell ();
2066 #endif /* HAVE_SUBSHELL_SUPPORT */
2068 /* Removing this from the X code let's us type C-c */
2069 load_key_defs ();
2071 load_keymap_defs ();
2073 /* Also done after init_subshell, to save any shell init file messages */
2074 if (console_flag)
2075 handle_console (CONSOLE_SAVE);
2077 if (alternate_plus_minus)
2078 application_keypad_mode ();
2080 #ifdef HAVE_SUBSHELL_SUPPORT
2081 if (use_subshell) {
2082 prompt = strip_ctrl_codes (subshell_prompt);
2083 if (!prompt)
2084 prompt = "";
2085 } else
2086 #endif /* HAVE_SUBSHELL_SUPPORT */
2087 prompt = (geteuid () == 0) ? "# " : "$ ";
2090 /* Program main loop */
2091 if (!midnight_shutdown)
2092 do_nc ();
2094 /* Save the tree store */
2095 tree_store_save ();
2097 /* Virtual File System shutdown */
2098 vfs_shut ();
2100 flush_extension_file (); /* does only free memory */
2102 mc_fhl_free (&mc_filehighlight);
2103 mc_skin_deinit();
2105 tty_shutdown ();
2107 if (console_flag && !(quit & SUBSHELL_EXIT))
2108 handle_console (CONSOLE_RESTORE);
2109 if (alternate_plus_minus)
2110 numeric_keypad_mode ();
2112 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2114 if (console_flag)
2115 handle_console (CONSOLE_DONE);
2116 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2118 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2119 && !edit_one_file && !view_one_file) {
2120 int last_wd_fd =
2121 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2122 S_IRUSR | S_IWUSR);
2124 if (last_wd_fd != -1) {
2125 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2126 close (last_wd_fd);
2129 g_free (last_wd_string);
2131 g_free (mc_home_alt);
2132 g_free (mc_home);
2133 g_free (shell);
2135 done_key ();
2136 #ifdef HAVE_CHARSET
2137 free_codepages_list ();
2138 #endif
2139 str_uninit_strings ();
2141 g_free (this_dir);
2142 g_free (other_dir);
2144 #ifdef USE_INTERNAL_EDIT
2145 edit_stack_free ();
2146 #endif
2148 return 0;