Modified ButtonBar engine to allow bind keys to unused buttons.
[midnight-commander.git] / src / main.c
blob3ca40b259a2f9d849d8da90fba2d6dd6af4fe25b
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 "option.h"
60 #include "tree.h"
61 #include "treestore.h"
62 #include "cons.saver.h"
63 #include "subshell.h"
64 #include "setup.h" /* save_setup() */
65 #include "boxes.h" /* sort_box() */
66 #include "layout.h"
67 #include "cmd.h" /* Normal commands */
68 #include "hotlist.h"
69 #include "panelize.h"
70 #include "learn.h" /* learn_keys() */
71 #include "listmode.h"
72 #include "execute.h"
73 #include "ext.h" /* For flush_extension_file() */
74 #include "strutil.h"
75 #include "widget.h"
76 #include "command.h"
77 #include "wtools.h"
78 #include "cmddef.h" /* CK_ cmd name const */
79 #include "fileloc.h" /* MC_USERCONF_DIR */
80 #include "user.h" /* user_file_menu_cmd() */
82 #include "../vfs/vfs.h" /* vfs_translate_url() */
84 #include "chmod.h"
85 #include "chown.h"
86 #include "achown.h"
88 #include "main.h"
90 #ifdef WITH_SMBFS
91 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
92 #endif
94 #ifdef USE_INTERNAL_EDIT
95 # include "../edit/edit.h"
96 #endif
98 #ifdef HAVE_CHARSET
99 #include "charsets.h"
100 #endif /* HAVE_CHARSET */
102 #ifdef USE_VFS
103 #include "../vfs/gc.h"
104 #endif
106 #include "keybind.h" /* type global_keymap_t */
108 /* When the modes are active, left_panel, right_panel and tree_panel */
109 /* Point to a proper data structure. You should check with the functions */
110 /* get_current_type and get_other_type the types of the panels before using */
111 /* This pointer variables */
113 /* The structures for the panels */
114 WPanel *left_panel = NULL;
115 WPanel *right_panel = NULL;
117 mc_fhl_t *mc_filehighlight;
119 /* The pointer to the tree */
120 WTree *the_tree = NULL;
122 /* The Menubar */
123 struct WMenuBar *the_menubar = NULL;
125 /* Pointers to the selected and unselected panel */
126 WPanel *current_panel = NULL;
128 /* Set if the command is being run from the "Right" menu */
129 int is_right = 0;
131 /* Set when main loop should be terminated */
132 volatile int quit = 0;
134 /* Set if you want the possible completions dialog for the first time */
135 int show_all_if_ambiguous = 0;
137 /* Set when cd symlink following is desirable (bash mode) */
138 int cd_symlinks = 1;
140 /* If set then dialogs just clean the screen when refreshing, else */
141 /* they do a complete refresh, refreshing all the parts of the program */
142 int fast_refresh = 0;
144 /* If true, marking a files moves the cursor down */
145 int mark_moves_down = 1;
147 /* If true, at startup the user-menu is invoked */
148 int auto_menu = 0;
150 /* If true, then the +, - and \ keys have their special meaning only if the
151 * command line is emtpy, otherwise they behave like regular letters
153 int only_leading_plus_minus = 1;
155 int pause_after_run = pause_on_dumb_terminals;
157 /* It true saves the setup when quitting */
158 int auto_save_setup = 1;
160 #ifdef HAVE_CHARSET
162 * Don't restrict the output on the screen manager level,
163 * the translation tables take care of it.
165 #define full_eight_bits (1)
166 #define eight_bit_clean (1)
167 #else /* HAVE_CHARSET */
168 /* If true, allow characters in the range 160-255 */
169 int eight_bit_clean = 1;
171 * If true, also allow characters in the range 128-159.
172 * This is reported to break on many terminals (xterm, qansi-m).
174 int full_eight_bits = 0;
175 #endif /* !HAVE_CHARSET */
178 * If utf-8 terminal utf8_display = 1
179 * Display bits set UTF-8
182 int utf8_display = 0;
184 /* If true use the internal viewer */
185 int use_internal_view = 1;
187 /* Have we shown the fast-reload warning in the past? */
188 int fast_reload_w = 0;
190 /* Move page/item? When clicking on the top or bottom of a panel */
191 int mouse_move_pages = 1;
193 /* If true: l&r arrows are used to chdir if the input line is empty */
194 int navigate_with_arrows = 0;
196 /* If true program softkeys (HP terminals only) on startup and after every
197 command ran in the subshell to the description found in the termcap/terminfo
198 database */
199 int reset_hp_softkeys = 0;
201 /* The prompt */
202 const char *prompt = NULL;
204 /* The widget where we draw the prompt */
205 WLabel *the_prompt;
207 /* The hint bar */
208 WLabel *the_hint;
210 /* The button bar */
211 WButtonBar *the_bar;
213 /* Mouse type: GPM, xterm or none */
214 Mouse_Type use_mouse_p = MOUSE_NONE;
216 /* If on, default for "No" in delete operations */
217 int safe_delete = 0;
219 /* Controls screen clearing before an exec */
220 int clear_before_exec = 1;
222 /* Asks for confirmation before deleting a file */
223 int confirm_delete = 1;
225 /* Asks for confirmation before deleting a hotlist entry */
226 int confirm_directory_hotlist_delete = 1;
228 /* Asks for confirmation before overwriting a file */
229 int confirm_overwrite = 1;
231 /* Asks for confirmation before executing a program by pressing enter */
232 int confirm_execute = 0;
234 /* Asks for confirmation before leaving the program */
235 int confirm_exit = 1;
237 /* Asks for confirmation when using F3 to view a directory and there
238 are tagged files */
239 int confirm_view_dir = 0;
241 /* This flag indicates if the pull down menus by default drop down */
242 int drop_menus = 0;
244 /* if skip_check_codeset = 1 do not show warning about
245 * system and display codeset is different
247 int skip_check_codeset = 0;
249 /* The dialog handle for the main program */
250 Dlg_head *midnight_dlg = NULL;
252 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
253 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
254 int update_prompt = 0;
256 /* The home directory */
257 const char *home_dir = NULL;
259 /* Tab size */
260 int option_tab_spacing = 8;
262 /* The value of the other directory, only used when loading the setup */
263 char *other_dir = NULL;
265 /* Only used at program boot */
266 int boot_current_is_left = 1;
268 static char *this_dir = NULL;
270 /* If this is true, then when browsing the tree the other window will
271 * automatically reload it's directory with the contents of the currently
272 * selected directory.
274 int xtree_mode = 0;
276 /* If set, then print to the given file the last directory we were at */
277 static char *last_wd_string = NULL;
278 /* Set to 1 to suppress printing the last directory */
279 static int print_last_revert = 0;
281 /* File name to view if argument was supplied */
282 const char *view_one_file = NULL;
284 /* File name to edit if argument was supplied */
285 const char *edit_one_file = NULL;
287 /* Line to start the editor on */
288 static int edit_one_file_start_line = 0;
290 /* Used so that widgets know if they are being destroyed or
291 shut down */
292 int midnight_shutdown = 0;
294 /* The user's shell */
295 char *shell = NULL;
297 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
298 char *mc_home = NULL;
300 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
301 char *mc_home_alt = NULL;
303 /* Define this function for glib-style error handling */
304 GQuark
305 mc_main_error_quark (void)
307 return g_quark_from_static_string (PACKAGE);
310 #ifdef USE_INTERNAL_EDIT
311 GArray *editor_keymap = NULL;
312 GArray *editor_x_keymap = NULL;
313 #endif
314 GArray *viewer_keymap = NULL;
315 GArray *viewer_hex_keymap = NULL;
316 GArray *main_keymap = NULL;
317 GArray *main_x_keymap = NULL;
318 GArray *panel_keymap = NULL;
319 GArray *input_keymap = NULL;
320 GArray *tree_keymap = NULL;
321 GArray *help_keymap = NULL;
323 const global_keymap_t *main_map;
324 const global_keymap_t *main_x_map;
326 /* Save current stat of directories to avoid reloading the panels */
327 /* when no modifications have taken place */
328 void
329 save_cwds_stat (void)
331 if (fast_reload) {
332 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
333 if (get_other_type () == view_listing)
334 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
338 #ifdef HAVE_SUBSHELL_SUPPORT
339 void
340 do_update_prompt (void)
342 if (update_prompt) {
343 printf ("\r\n%s", subshell_prompt);
344 fflush (stdout);
345 update_prompt = 0;
348 #endif /* HAVE_SUBSHELL_SUPPORT */
350 void
351 change_panel (void)
353 free_completions (cmdline);
354 dlg_one_down (midnight_dlg);
357 /* Stop MC main dialog and the current dialog if it exists.
358 * Needed to provide fast exit from MC viewer or editor on shell exit */
359 static void
360 stop_dialogs (void)
362 midnight_dlg->running = 0;
363 if (current_dlg) {
364 current_dlg->running = 0;
368 static int
369 quit_cmd_internal (int quiet)
371 int q = quit;
373 if (quiet || !confirm_exit) {
374 q = 1;
375 } else {
376 if (query_dialog
377 (_(" The Midnight Commander "),
378 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
379 2, _("&Yes"), _("&No")) == 0)
380 q = 1;
382 if (q) {
383 #ifdef HAVE_SUBSHELL_SUPPORT
384 if (!use_subshell)
385 stop_dialogs ();
386 else if ((q = exit_subshell ()))
387 #endif
388 stop_dialogs ();
390 if (q)
391 quit |= 1;
392 return quit;
395 static void
396 quit_cmd (void)
398 quit_cmd_internal (0);
401 void
402 quiet_quit_cmd (void)
404 print_last_revert = 1;
405 quit_cmd_internal (1);
408 /* Wrapper for do_subshell_chdir, check for availability of subshell */
409 void
410 subshell_chdir (const char *directory)
412 #ifdef HAVE_SUBSHELL_SUPPORT
413 if (use_subshell) {
414 if (vfs_current_is_local ())
415 do_subshell_chdir (directory, 0, 1);
417 #endif /* HAVE_SUBSHELL_SUPPORT */
420 void
421 directory_history_add (struct WPanel *panel, const char *dir)
423 char *tmp;
425 tmp = g_strdup (dir);
426 strip_password (tmp, 1);
428 panel->dir_history = list_append_unique (panel->dir_history, tmp);
432 * If we moved to the parent directory move the selection pointer to
433 * the old directory name; If we leave VFS dir, remove FS specificator.
435 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
437 static const char *
438 get_parent_dir_name (const char *cwd, const char *lwd)
440 const char *p;
441 if (strlen (lwd) > strlen (cwd))
442 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
443 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
444 cwd[1] == '\0'))) {
445 return (p + 1);
447 return NULL;
451 * Changes the current directory of the panel.
452 * Don't record change in the directory history.
454 static int
455 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
457 const char *directory;
458 char *olddir;
459 char temp[MC_MAXPATHLEN];
460 char *translated_url;
462 if (cd_type == cd_parse_command) {
463 while (*new_dir == ' ')
464 new_dir++;
467 olddir = g_strdup (panel->cwd);
468 new_dir = translated_url = vfs_translate_url (new_dir);
470 /* Convert *new_path to a suitable pathname, handle ~user */
472 if (cd_type == cd_parse_command) {
473 if (!strcmp (new_dir, "-")) {
474 strcpy (temp, panel->lwd);
475 new_dir = temp;
478 directory = *new_dir ? new_dir : home_dir;
480 if (mc_chdir (directory) == -1) {
481 strcpy (panel->cwd, olddir);
482 g_free (olddir);
483 g_free (translated_url);
484 return 0;
486 g_free (translated_url);
488 /* Success: save previous directory, shutdown status of previous dir */
489 strcpy (panel->lwd, olddir);
490 free_completions (cmdline);
492 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
494 vfs_release_path (olddir);
496 subshell_chdir (panel->cwd);
498 /* Reload current panel */
499 panel_clean_dir (panel);
500 panel->count =
501 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
502 panel->reverse, panel->case_sensitive,
503 panel->exec_first, panel->filter);
504 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
505 load_hint (0);
506 panel->dirty = 1;
507 update_xterm_title_path ();
509 g_free (olddir);
511 return 1;
515 * Changes the current directory of the panel.
516 * Record change in the directory history.
519 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
521 int r;
523 r = _do_panel_cd (panel, new_dir, cd_type);
524 if (r)
525 directory_history_add (panel, panel->cwd);
526 return r;
530 do_cd (const char *new_dir, enum cd_enum exact)
532 return (do_panel_cd (current_panel, new_dir, exact));
535 void
536 directory_history_next (WPanel *panel)
538 GList *nextdir;
540 nextdir = g_list_next (panel->dir_history);
542 if (!nextdir)
543 return;
545 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
546 panel->dir_history = nextdir;
549 void
550 directory_history_prev (WPanel *panel)
552 GList *prevdir;
554 prevdir = g_list_previous (panel->dir_history);
556 if (!prevdir)
557 return;
559 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
560 panel->dir_history = prevdir;
563 void
564 directory_history_list (WPanel *panel)
566 char *s;
568 if (!panel->dir_history)
569 return;
571 s = show_hist (panel->dir_history, &panel->widget);
573 if (!s)
574 return;
576 if (_do_panel_cd (panel, s, cd_exact))
577 directory_history_add (panel, panel->cwd);
578 else
579 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
580 g_free (s);
583 #ifdef HAVE_SUBSHELL_SUPPORT
585 load_prompt (int fd, void *unused)
587 (void) fd;
588 (void) unused;
590 if (!read_subshell_prompt ())
591 return 0;
593 /* Don't actually change the prompt if it's invisible */
594 if (current_dlg == midnight_dlg && command_prompt) {
595 char *tmp_prompt;
596 int prompt_len;
598 tmp_prompt = strip_ctrl_codes (subshell_prompt);
599 prompt_len = str_term_width1 (tmp_prompt);
601 /* Check for prompts too big */
602 if (COLS > 8 && prompt_len > COLS - 8) {
603 tmp_prompt[COLS - 8] = '\0';
604 prompt_len = COLS - 8;
606 prompt = tmp_prompt;
607 label_set_text (the_prompt, prompt);
608 winput_set_origin ((WInput *) cmdline, prompt_len,
609 COLS - prompt_len);
611 /* since the prompt has changed, and we are called from one of the
612 * tty_get_event channels, the prompt updating does not take place
613 * automatically: force a cursor update and a screen refresh
615 update_cursor (midnight_dlg);
616 mc_refresh ();
618 update_prompt = 1;
619 return 0;
621 #endif /* HAVE_SUBSHELL_SUPPORT */
623 void
624 sort_cmd (void)
626 WPanel *p;
627 const panel_field_t *sort_order;
629 if (!SELECTED_IS_PANEL)
630 return;
632 p = MENU_PANEL;
633 sort_order = sort_box (p->current_sort_field, &p->reverse,
634 &p->case_sensitive,
635 &p->exec_first);
637 panel_set_sort_order (p, sort_order);
641 static void
642 treebox_cmd (void)
644 char *sel_dir;
646 sel_dir = tree_box (selection (current_panel)->fname);
647 if (sel_dir) {
648 do_cd (sel_dir, cd_exact);
649 g_free (sel_dir);
653 #ifdef LISTMODE_EDITOR
654 static void
655 listmode_cmd (void)
657 char *newmode;
659 if (get_current_type () != view_listing)
660 return;
662 newmode = listmode_edit (current_panel->user_format);
663 if (!newmode)
664 return;
666 g_free (current_panel->user_format);
667 current_panel->list_type = list_user;
668 current_panel->user_format = newmode;
669 set_panel_formats (current_panel);
671 do_refresh ();
673 #endif /* LISTMODE_EDITOR */
675 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
676 static GList *
677 create_panel_menu (void)
679 GList *entries = NULL;
681 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
682 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
683 entries = g_list_append (entries, menu_entry_create (_("&Info" ), CK_InfoCmd));
684 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
685 entries = g_list_append (entries, menu_separator_create ());
686 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
687 entries = g_list_append (entries, menu_separator_create ());
688 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
689 #ifdef HAVE_CHARSET
690 entries = g_list_append (entries, menu_separator_create ());
691 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
692 #endif
693 #ifdef USE_NETCODE
694 entries = g_list_append (entries, menu_separator_create ());
695 #ifdef ENABLE_VFS_MCFS
696 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
697 #endif
698 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
699 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
700 #ifdef WITH_SMBFS
701 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
702 #endif
703 #endif
704 entries = g_list_append (entries, menu_separator_create ());
705 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
707 return entries;
710 static GList *
711 create_file_menu (void)
713 GList *entries = NULL;
715 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
716 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
717 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
718 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
719 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
720 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
721 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
722 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
723 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
724 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
725 entries = g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
726 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
727 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
728 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
729 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
730 entries = g_list_append (entries, menu_separator_create ());
731 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
732 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
733 entries = g_list_append (entries, menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
734 entries = g_list_append (entries, menu_separator_create ());
735 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
737 return entries;
740 static GList *
741 create_command_menu (void)
743 /* I know, I'm lazy, but the tree widget when it's not running
744 * as a panel still has some problems, I have not yet finished
745 * the WTree widget port, sorry.
747 GList *entries = NULL;
749 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
750 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
751 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
752 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
753 entries = g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
754 entries = g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
755 entries = g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
756 entries = g_list_append (entries, menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
757 entries = g_list_append (entries, menu_separator_create ());
758 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
759 entries = g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
760 #ifdef USE_VFS
761 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
762 #endif
763 #ifdef WITH_BACKGROUND
764 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
765 #endif
766 entries = g_list_append (entries, menu_separator_create ());
767 #ifdef USE_EXT2FSLIB
768 entries = g_list_append (entries, menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
769 #endif
770 #ifdef LISTMODE_EDITOR
771 entries = g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
772 #endif
773 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
774 entries = g_list_append (entries, menu_separator_create ());
775 #endif
776 entries = g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
777 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
778 entries = g_list_append (entries, menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
780 return entries;
783 static GList *
784 create_options_menu (void)
786 GList *entries = NULL;
788 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
789 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
790 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
791 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
792 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
793 #ifdef USE_VFS
794 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
795 #endif
796 entries = g_list_append (entries, menu_separator_create ());
797 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
799 return entries;
802 void
803 init_menu (void)
805 menubar_add_menu (the_menubar,
806 create_menu (horizontal_split ? _("&Above") : _("&Left"),
807 create_panel_menu (), "[Left and Right Menus]"));
808 menubar_add_menu (the_menubar,
809 create_menu (_("&File"), create_file_menu (), "[File Menu]"));
810 menubar_add_menu (the_menubar,
811 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
812 menubar_add_menu (the_menubar,
813 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
814 menubar_add_menu (the_menubar,
815 create_menu (horizontal_split ? _("&Below") : _("&Right"),
816 create_panel_menu (), "[Left and Right Menus]"));
819 void
820 done_menu (void)
822 menubar_set_menu (the_menubar, NULL);
825 static void
826 menu_last_selected_cmd (void)
828 the_menubar->is_active = TRUE;
829 the_menubar->is_dropped = (drop_menus != 0);
830 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
831 dlg_select_widget (the_menubar);
834 static void
835 menu_cmd (void)
837 if (the_menubar->is_active)
838 return;
840 if ((get_current_index () == 0) == (current_panel->active != 0))
841 the_menubar->selected = 0;
842 else
843 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
844 menu_last_selected_cmd ();
847 static char *
848 midnight_get_shortcut (unsigned long command)
850 const char *ext_map;
851 const char *shortcut = NULL;
853 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
855 if (ext_map != NULL)
856 shortcut = lookup_keymap_shortcut (main_x_map, command);
857 if (shortcut != NULL)
858 return g_strdup_printf ("%s %s", ext_map, shortcut);
860 shortcut = lookup_keymap_shortcut (main_map, command);
861 if (shortcut != NULL)
862 return g_strdup (shortcut);
864 shortcut = lookup_keymap_shortcut (panel_map, command);
865 if (shortcut != NULL)
866 return g_strdup (shortcut);
868 return NULL;
871 /* Flag toggling functions */
872 void
873 toggle_fast_reload (void)
875 fast_reload = !fast_reload;
876 if (fast_reload_w == 0 && fast_reload) {
877 message (D_NORMAL, _(" Information "),
879 (" Using the fast reload option may not reflect the exact \n"
880 " directory contents. In this case you'll need to do a \n"
881 " manual reload of the directory. See the man page for \n"
882 " the details. "));
883 fast_reload_w = 1;
887 void
888 toggle_mix_all_files (void)
890 mix_all_files = !mix_all_files;
891 update_panels (UP_RELOAD, UP_KEEPSEL);
894 void
895 toggle_show_backup (void)
897 show_backups = !show_backups;
898 update_panels (UP_RELOAD, UP_KEEPSEL);
901 void
902 toggle_show_hidden (void)
904 show_dot_files = !show_dot_files;
905 update_panels (UP_RELOAD, UP_KEEPSEL);
908 void
909 toggle_kilobyte_si (void)
911 kilobyte_si = !kilobyte_si;
912 update_panels (UP_RELOAD, UP_KEEPSEL);
916 * Just a hack for allowing url-like pathnames to be accepted from the
917 * command line.
919 static void
920 translated_mc_chdir (char *dir)
922 char *newdir;
924 newdir = vfs_translate_url (dir);
925 mc_chdir (newdir);
926 g_free (newdir);
929 static void
930 create_panels (void)
932 int current_index;
933 int other_index;
934 int current_mode;
935 int other_mode;
936 char original_dir[1024];
938 original_dir[0] = 0;
940 if (boot_current_is_left) {
941 current_index = 0;
942 other_index = 1;
943 current_mode = startup_left_mode;
944 other_mode = startup_right_mode;
945 } else {
946 current_index = 1;
947 other_index = 0;
948 current_mode = startup_right_mode;
949 other_mode = startup_left_mode;
951 /* Creates the left panel */
952 if (this_dir) {
953 if (other_dir) {
954 /* Ok, user has specified two dirs, save the original one,
955 * since we may not be able to chdir to the proper
956 * second directory later
958 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
960 translated_mc_chdir (this_dir);
962 set_display_type (current_index, current_mode);
964 /* The other panel */
965 if (other_dir) {
966 if (original_dir[0])
967 translated_mc_chdir (original_dir);
968 translated_mc_chdir (other_dir);
970 set_display_type (other_index, other_mode);
972 if (startup_left_mode == view_listing) {
973 current_panel = left_panel;
974 } else {
975 if (right_panel)
976 current_panel = right_panel;
977 else
978 current_panel = left_panel;
981 /* Create the nice widgets */
982 cmdline = command_new (0, 0, 0);
983 the_prompt = label_new (0, 0, prompt);
984 the_prompt->transparent = 1;
985 the_bar = buttonbar_new (keybar_visible);
987 the_hint = label_new (0, 0, 0);
988 the_hint->transparent = 1;
989 the_hint->auto_adjust_cols = 0;
990 the_hint->widget.cols = COLS;
992 the_menubar = menubar_new (0, 0, COLS, NULL);
995 static void
996 copy_current_pathname (void)
998 char *cwd_path;
999 if (!command_prompt)
1000 return;
1002 cwd_path = remove_encoding_from_path (current_panel->cwd);
1003 command_insert (cmdline, cwd_path, 0);
1005 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1006 command_insert (cmdline, PATH_SEP_STR, 0);
1007 g_free (cwd_path);
1010 static void
1011 copy_other_pathname (void)
1013 char *cwd_path;
1015 if (get_other_type () != view_listing)
1016 return;
1018 if (!command_prompt)
1019 return;
1021 cwd_path = remove_encoding_from_path (other_panel->cwd);
1022 command_insert (cmdline, cwd_path, 0);
1024 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1025 command_insert (cmdline, PATH_SEP_STR, 0);
1026 g_free (cwd_path);
1029 static void
1030 copy_readlink (WPanel *panel)
1032 if (!command_prompt)
1033 return;
1034 if (S_ISLNK (selection (panel)->st.st_mode)) {
1035 char buffer[MC_MAXPATHLEN];
1036 char *p =
1037 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1038 int i;
1040 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1041 g_free (p);
1042 if (i > 0) {
1043 buffer[i] = 0;
1044 command_insert (cmdline, buffer, 1);
1049 static void
1050 copy_current_readlink (void)
1052 copy_readlink (current_panel);
1055 static void
1056 copy_other_readlink (void)
1058 if (get_other_type () != view_listing)
1059 return;
1060 copy_readlink (other_panel);
1063 /* Insert the selected file name into the input line */
1064 static void
1065 copy_prog_name (void)
1067 char *tmp;
1068 if (!command_prompt)
1069 return;
1071 if (get_current_type () == view_tree) {
1072 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1073 tmp = tree_selected_name (tree);
1074 } else
1075 tmp = selection (current_panel)->fname;
1077 command_insert (cmdline, tmp, 1);
1080 static void
1081 copy_tagged (WPanel *panel)
1083 int i;
1085 if (!command_prompt)
1086 return;
1087 input_disable_update (cmdline);
1088 if (panel->marked) {
1089 for (i = 0; i < panel->count; i++) {
1090 if (panel->dir.list[i].f.marked)
1091 command_insert (cmdline, panel->dir.list[i].fname, 1);
1093 } else {
1094 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1097 input_enable_update (cmdline);
1100 static void
1101 copy_current_tagged (void)
1103 copy_tagged (current_panel);
1106 static void
1107 copy_other_tagged (void)
1109 if (get_other_type () != view_listing)
1110 return;
1111 copy_tagged (other_panel);
1114 void
1115 midnight_set_buttonbar (WButtonBar *b)
1117 buttonbar_set_label (b, 1, Q_("ButtonBar|Help"), main_map, NULL);
1118 buttonbar_set_label (b, 2, Q_("ButtonBar|Menu"), main_map, NULL);
1119 buttonbar_set_label (b, 3, Q_("ButtonBar|View"), main_map, NULL);
1120 buttonbar_set_label (b, 4, Q_("ButtonBar|Edit"), main_map, NULL);
1121 buttonbar_set_label (b, 5, Q_("ButtonBar|Copy"), main_map, NULL);
1122 buttonbar_set_label (b, 6, Q_("ButtonBar|RenMov"), main_map, NULL);
1123 buttonbar_set_label (b, 7, Q_("ButtonBar|Mkdir"), main_map, NULL);
1124 buttonbar_set_label (b, 8, Q_("ButtonBar|Delete"), main_map, NULL);
1125 buttonbar_set_label (b, 9, Q_("ButtonBar|PullDn"), main_map, NULL);
1126 buttonbar_set_label (b, 10, Q_("ButtonBar|Quit"), main_map, NULL);
1129 static gboolean ctl_x_map_enabled = FALSE;
1131 static void
1132 ctl_x_cmd (void)
1134 ctl_x_map_enabled = TRUE;
1137 static cb_ret_t
1138 midnight_execute_cmd (Widget *sender, unsigned long command)
1140 cb_ret_t res = MSG_HANDLED;
1142 (void) sender;
1144 switch (command) {
1145 case CK_AddHotlist:
1146 add2hotlist_cmd ();
1147 break;
1148 case CK_ChmodCmd:
1149 chmod_cmd ();
1150 break;
1151 case CK_ChownCmd:
1152 chown_cmd ();
1153 break;
1154 case CK_ChownAdvancedCmd:
1155 chown_advanced_cmd ();
1156 break;
1157 case CK_CompareDirsCmd:
1158 compare_dirs_cmd ();
1159 break;
1160 case CK_ConfigureBox:
1161 configure_box ();
1162 break;
1163 #ifdef USE_VFS
1164 case CK_ConfigureVfs:
1165 configure_vfs ();
1166 break;
1167 #endif
1168 case CK_ConfirmBox:
1169 confirm_box ();
1170 break;
1171 case CK_CopyCmd:
1172 copy_cmd ();
1173 break;
1174 case CK_CopyCurrentPathname:
1175 copy_current_pathname ();
1176 break;
1177 case CK_CopyCurrentReadlink:
1178 copy_current_readlink ();
1179 break;
1180 case CK_CopyCurrentTagged:
1181 copy_current_tagged ();
1182 break;
1183 case CK_CopyOtherPathname:
1184 copy_other_pathname ();
1185 break;
1186 case CK_CopyOtherReadlink:
1187 copy_other_readlink ();
1188 break;
1189 case CK_CopyOtherTagged:
1190 copy_other_tagged ();
1191 break;
1192 case CK_DeleteCmd:
1193 delete_cmd ();
1194 break;
1195 case CK_DisplayBitsBox:
1196 display_bits_box ();
1197 break;
1198 case CK_EditCmd:
1199 edit_cmd ();
1200 break;
1201 case CK_EditExtFileCmd:
1202 ext_cmd ();
1203 break;
1204 case CK_EditFhlFileCmd:
1205 edit_fhl_cmd ();
1206 break;
1207 case CK_EditMcMenuCmd:
1208 edit_mc_menu_cmd ();
1209 break;
1210 case CK_EditSymlinkCmd:
1211 edit_symlink_cmd ();
1212 break;
1213 case CK_ExternalPanelize:
1214 external_panelize ();
1215 break;
1216 case CK_FilterCmd:
1217 filter_cmd ();
1218 break;
1219 case CK_FilteredViewCmd:
1220 filtered_view_cmd ();
1221 break;
1222 case CK_FindCmd:
1223 find_cmd ();
1224 break;
1225 #if defined (USE_NETCODE)
1226 case CK_FishlinkCmd:
1227 fishlink_cmd ();
1228 break;
1229 case CK_FtplinkCmd:
1230 ftplink_cmd ();
1231 break;
1232 #endif
1233 case CK_HelpCmd:
1234 help_cmd ();
1235 break;
1236 case CK_HistoryCmd:
1237 history_cmd ();
1238 break;
1239 case CK_InfoCmd:
1240 if (sender == (Widget *) the_menubar)
1241 info_cmd (); /* mwnu */
1242 else
1243 info_cmd_no_menu (); /* shortcut or buttonbar */
1244 break;
1245 #ifdef WITH_BACKGROUND
1246 case CK_JobsCmd:
1247 jobs_cmd ();
1248 break;
1249 #endif
1250 case CK_LayoutCmd:
1251 layout_cmd ();
1252 break;
1253 case CK_LearnKeys:
1254 learn_keys ();
1255 break;
1256 case CK_LinkCmd:
1257 link_cmd ();
1258 break;
1259 case CK_ListingCmd:
1260 listing_cmd ();
1261 break;
1262 #ifdef LISTMODE_EDITOR
1263 case CK_ListmodeCmd:
1264 listmode_cmd ();
1265 break;
1266 #endif
1267 case CK_MenuCmd:
1268 menu_cmd ();
1269 break;
1270 case CK_MenuLastSelectedCmd:
1271 menu_last_selected_cmd ();
1272 break;
1273 case CK_MkdirCmd:
1274 mkdir_cmd ();
1275 break;
1276 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1277 case CK_NetlinkCmd:
1278 netlink_cmd ();
1279 break;
1280 #endif
1281 #ifdef HAVE_CHARSET
1282 case CK_PanelSetPanelEncoding:
1283 encoding_cmd ();
1284 break;
1285 #endif
1286 case CK_QuickCdCmd:
1287 quick_cd_cmd ();
1288 break;
1289 case CK_QuickChdirCmd:
1290 quick_chdir_cmd ();
1291 break;
1292 case CK_QuickViewCmd:
1293 if (sender == (Widget *) the_menubar)
1294 quick_view_cmd (); /* menu */
1295 else
1296 quick_cmd_no_menu (); /* shortcut or buttonabr */
1297 break;
1298 case CK_QuietQuitCmd:
1299 quiet_quit_cmd ();
1300 break;
1301 case CK_QuitCmd:
1302 quit_cmd ();
1303 break;
1304 case CK_RenameCmd:
1305 rename_cmd ();
1306 break;
1307 case CK_RereadCmd:
1308 reread_cmd ();
1309 break;
1310 #ifdef USE_VFS
1311 case CK_ReselectVfs:
1312 reselect_vfs ();
1313 break;
1314 #endif
1315 case CK_ReverseSelectionCmd:
1316 reverse_selection_cmd ();
1317 break;
1318 case CK_SaveSetupCmd:
1319 save_setup_cmd ();
1320 break;
1321 case CK_SelectCmd:
1322 select_cmd ();
1323 break;
1324 case CK_ShowCommandLine:
1325 view_other_cmd ();
1326 break;
1327 case CK_SingleDirsizeCmd:
1328 smart_dirsize_cmd ();
1329 break;
1330 #if defined (USE_NETCODE) && defined (WITH_SMBFS)
1331 case CK_SmblinkCmd:
1332 smblink_cmd ();
1333 break;
1334 #endif
1335 case CK_Sort:
1336 sort_cmd ();
1337 break;
1338 case CK_StartExtMap1:
1339 ctl_x_cmd ();
1340 break;
1341 case CK_SuspendCmd:
1342 suspend_cmd ();
1343 break;
1344 case CK_SwapCmd:
1345 swap_cmd ();
1346 break;
1347 case CK_SymlinkCmd:
1348 symlink_cmd ();
1349 break;
1350 case CK_ToggleListingCmd:
1351 toggle_listing_cmd ();
1352 break;
1353 case CK_ToggleShowHidden:
1354 toggle_show_hidden ();
1355 break;
1356 case CK_TreeCmd:
1357 tree_cmd ();
1358 break;
1359 case CK_TreeBoxCmd:
1360 treebox_cmd ();
1361 break;
1362 #ifdef USE_EXT2FSLIB
1363 case CK_UndeleteCmd:
1364 undelete_cmd ();
1365 break;
1366 #endif
1367 case CK_UnselectCmd:
1368 unselect_cmd ();
1369 break;
1370 case CK_UserMenuCmd:
1371 user_file_menu_cmd ();
1372 break;
1373 case CK_ViewCmd:
1374 view_cmd ();
1375 break;
1376 case CK_ViewFileCmd:
1377 view_file_cmd ();
1378 break;
1379 default:
1380 res = MSG_NOT_HANDLED;
1383 return res;
1386 static void
1387 setup_pre (void)
1389 /* Call all the inits */
1391 #ifdef HAVE_SLANG
1392 tty_display_8bit (full_eight_bits != 0);
1393 #else
1394 tty_display_8bit (eight_bit_clean != 0);
1395 #endif
1398 static void
1399 init_xterm_support (void)
1401 const char *termvalue;
1403 termvalue = getenv ("TERM");
1404 if (!termvalue || !(*termvalue)) {
1405 fputs (_("The TERM environment variable is unset!\n"), stderr);
1406 exit (1);
1409 /* Check mouse capabilities */
1410 xmouse_seq = tty_tgetstr ("Km");
1412 if (strcmp (termvalue, "cygwin") == 0) {
1413 mc_args__force_xterm = 1;
1414 use_mouse_p = MOUSE_DISABLED;
1417 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1418 || strncmp (termvalue, "konsole", 7) == 0
1419 || strncmp (termvalue, "rxvt", 4) == 0
1420 || strcmp (termvalue, "Eterm") == 0
1421 || strcmp (termvalue, "dtterm") == 0) {
1422 xterm_flag = 1;
1424 /* Default to the standard xterm sequence */
1425 if (!xmouse_seq) {
1426 xmouse_seq = ESC_STR "[M";
1429 /* Enable mouse unless explicitly disabled by --nomouse */
1430 if (use_mouse_p != MOUSE_DISABLED) {
1431 const char *color_term = getenv ("COLORTERM");
1432 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1433 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1434 strcmp (termvalue, "Eterm") == 0) {
1435 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1436 } else {
1437 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1443 static void
1444 setup_mc (void)
1446 setup_pre ();
1447 create_panels ();
1448 setup_panels ();
1450 #ifdef HAVE_SUBSHELL_SUPPORT
1451 if (use_subshell)
1452 add_select_channel (subshell_pty, load_prompt, 0);
1453 #endif /* !HAVE_SUBSHELL_SUPPORT */
1455 tty_setup_sigwinch (sigwinch_handler);
1457 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1459 init_xterm_support ();
1460 init_mouse ();
1463 static void
1464 setup_dummy_mc (void)
1466 char d[MC_MAXPATHLEN];
1468 mc_get_current_wd (d, MC_MAXPATHLEN);
1469 setup_mc ();
1470 mc_chdir (d);
1473 static void check_codeset()
1475 const char *_system_codepage = NULL;
1476 #ifdef HAVE_CHARSET
1477 const char *_source_codepage = NULL;
1478 const char *_display_codepage = NULL;
1479 int profile_changed = 0;
1481 #define CONFY 16
1482 #define CONFX 54
1484 if ( !skip_check_codeset ) {
1486 QuickWidget ecs_widgets [] = {
1487 QUICK_BUTTON (4, 6, 13, CONFY, N_("&Skip"), B_EXIT, NULL),
1488 QUICK_BUTTON (1, 11, 13, CONFY, N_("&Fix it"), B_ENTER, NULL),
1489 QUICK_CHECKBOX (1, 13, 11, CONFY, N_("don't ask again"), &skip_check_codeset),
1490 QUICK_LABEL (2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
1491 "or source codeset (in mcedit ctrl-t) \n"
1492 "does not match one set via locale. \n"
1493 "Set correct codeset manually or press <<Fix it>> \n"
1494 "to set locale default.\n\n"
1495 "Or set \'don't ask again\' and press <<Skip>>")),
1496 QUICK_END
1499 QuickDialog ecs =
1501 CONFX, CONFY, -1, -1, N_(" Confirmation "),
1502 "[Confirmation]", ecs_widgets, FALSE
1506 _system_codepage = str_detect_termencoding();
1507 _source_codepage = get_codepage_id (source_codepage);
1508 _display_codepage = get_codepage_id (display_codepage);
1509 if ( (strcmp (_system_codepage, _display_codepage)) ||
1510 (strcmp (_system_codepage, _source_codepage)) ) {
1511 if (quick_dialog (&ecs) == B_ENTER){
1512 display_codepage = get_codepage_index (_system_codepage);
1513 cp_display = get_codepage_id (display_codepage);
1514 if ( !strcmp (cp_display, _system_codepage)) {
1515 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1516 mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
1517 display_codepage = get_codepage_index ( cp_display );
1518 utf8_display = str_isutf8 (_system_codepage);
1519 source_codepage = display_codepage;
1520 cp_source = cp_display;
1521 profile_changed = 1;
1522 } else {
1523 utf8_display = str_isutf8 (_system_codepage);
1524 cp_source = cp_display = _system_codepage;
1526 } else {
1527 if ( skip_check_codeset ) {
1528 mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
1529 profile_changed = 1;
1533 if ( profile_changed )
1534 save_configure ();
1536 #else /* HAVE_CHARSET */
1537 _system_codepage = str_detect_termencoding();
1538 utf8_display = str_isutf8 (_system_codepage);
1539 #endif /* HAVE_CHARSET */
1542 static void
1543 done_screen (void)
1545 if (!(quit & SUBSHELL_EXIT))
1546 clr_scr ();
1547 tty_reset_shell_mode ();
1548 tty_noraw_mode ();
1549 tty_keypad (FALSE);
1550 tty_colors_done ();
1553 static void
1554 done_mc (void)
1556 disable_mouse ();
1558 done_menu ();
1560 /* Setup shutdown
1562 * We sync the profiles since the hotlist may have changed, while
1563 * we only change the setup data if we have the auto save feature set
1566 if (auto_save_setup)
1567 save_setup (); /* does also call save_hotlist */
1568 else {
1569 save_hotlist ();
1570 save_panel_types ();
1572 done_screen ();
1573 vfs_add_current_stamps ();
1576 /* This should be called after destroy_dlg since panel widgets
1577 * save their state on the profiles
1579 static void
1580 done_mc_profile (void)
1582 done_setup ();
1585 static cb_ret_t
1586 midnight_callback (Dlg_head *h, Widget *sender,
1587 dlg_msg_t msg, int parm, void *data)
1589 unsigned long command;
1591 switch (msg) {
1593 case DLG_DRAW:
1594 load_hint (1);
1595 /* We handle the special case of the output lines */
1596 if (console_flag && output_lines)
1597 show_console_contents (output_start_y,
1598 LINES - output_lines - keybar_visible -
1599 1, LINES - keybar_visible - 1);
1600 return MSG_HANDLED;
1602 case DLG_IDLE:
1603 /* We only need the first idle event to show user menu after start */
1604 set_idle_proc (h, 0);
1605 if (auto_menu)
1606 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1607 return MSG_HANDLED;
1609 case DLG_KEY:
1610 if (ctl_x_map_enabled) {
1611 ctl_x_map_enabled = FALSE;
1612 command = lookup_keymap_command (main_x_map, parm);
1613 if (command != CK_Ignore_Key)
1614 return midnight_execute_cmd (NULL, command);
1617 /* FIXME: should handle all menu shortcuts before this point */
1618 if (the_menubar->is_active)
1619 return MSG_NOT_HANDLED;
1621 if (parm == '\t')
1622 free_completions (cmdline);
1624 if (parm == '\n') {
1625 size_t i;
1627 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1628 cmdline->buffer[i] == '\t'); i++)
1630 if (cmdline->buffer[i]) {
1631 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1632 return MSG_HANDLED;
1634 stuff (cmdline, "", 0);
1635 cmdline->point = 0;
1638 /* Ctrl-Enter and Alt-Enter */
1639 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1640 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1641 copy_prog_name ();
1642 return MSG_HANDLED;
1645 /* Ctrl-Shift-Enter */
1646 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1647 copy_current_pathname ();
1648 copy_prog_name ();
1649 return MSG_HANDLED;
1652 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1653 && !quote && !current_panel->searching) {
1654 if (!only_leading_plus_minus) {
1655 /* Special treatement, since the input line will eat them */
1656 if (parm == '+') {
1657 select_cmd ();
1658 return MSG_HANDLED;
1661 if (parm == '\\' || parm == '-') {
1662 unselect_cmd ();
1663 return MSG_HANDLED;
1666 if (parm == '*') {
1667 reverse_selection_cmd ();
1668 return MSG_HANDLED;
1670 } else if (!command_prompt || !cmdline->buffer[0]) {
1671 /* Special treatement '+', '-', '\', '*' only when this is
1672 * first char on input line
1675 if (parm == '+') {
1676 select_cmd ();
1677 return MSG_HANDLED;
1680 if (parm == '\\' || parm == '-') {
1681 unselect_cmd ();
1682 return MSG_HANDLED;
1685 if (parm == '*') {
1686 reverse_selection_cmd ();
1687 return MSG_HANDLED;
1691 return MSG_NOT_HANDLED;
1693 case DLG_HOTKEY_HANDLED:
1694 if ((get_current_type () == view_listing) && current_panel->searching) {
1695 current_panel->searching = 0;
1696 current_panel->dirty = 1;
1698 return MSG_HANDLED;
1700 case DLG_UNHANDLED_KEY:
1701 if (command_prompt) {
1702 cb_ret_t v;
1704 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1705 if (v == MSG_HANDLED)
1706 return MSG_HANDLED;
1709 if (ctl_x_map_enabled) {
1710 ctl_x_map_enabled = FALSE;
1711 command = lookup_keymap_command (main_x_map, parm);
1712 } else
1713 command = lookup_keymap_command (main_map, parm);
1715 return (command == CK_Ignore_Key)
1716 ? MSG_NOT_HANDLED
1717 : midnight_execute_cmd (NULL, command);
1719 case DLG_POST_KEY:
1720 if (!the_menubar->is_active)
1721 update_dirty_panels ();
1722 return MSG_HANDLED;
1724 case DLG_ACTION:
1725 /* shortcut */
1726 if (sender == NULL)
1727 midnight_execute_cmd (NULL, parm);
1728 /* message from menu */
1729 else if (sender == (Widget *) the_menubar)
1730 midnight_execute_cmd (sender, parm);
1731 /* message from buttonbar */
1732 else if (sender == (Widget *) the_bar) {
1733 if (data == NULL)
1734 midnight_execute_cmd (sender, parm);
1735 else
1736 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1738 return MSG_HANDLED;
1740 default:
1741 return default_dlg_callback (h, sender, msg, parm, data);
1745 /* Show current directory in the xterm title */
1746 void
1747 update_xterm_title_path (void)
1749 const char *path;
1750 char host[BUF_TINY];
1751 char *p;
1752 struct passwd *pw = NULL;
1753 char *login = NULL;
1754 int res = 0;
1755 if (xterm_flag && xterm_title) {
1756 path = strip_home_and_password (current_panel->cwd);
1757 res = gethostname(host, sizeof (host));
1758 if ( res ) { /* On success, res = 0 */
1759 host[0] = '\0';
1760 } else {
1761 host[sizeof (host) - 1] = '\0';
1763 pw = getpwuid(getuid());
1764 if ( pw ) {
1765 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1766 } else {
1767 login = g_strdup (host);
1769 p = g_strdup_printf ("mc [%s]:%s", login, path);
1770 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1771 g_free (login);
1772 g_free (p);
1773 if (!alternate_plus_minus)
1774 numeric_keypad_mode ();
1775 fflush (stdout);
1780 * Load new hint and display it.
1781 * IF force is not 0, ignore the timeout.
1783 void
1784 load_hint (int force)
1786 char *hint;
1788 if (!the_hint->widget.parent)
1789 return;
1791 if (!message_visible) {
1792 label_set_text (the_hint, NULL);
1793 return;
1796 hint = get_random_hint (force);
1798 if (hint != NULL) {
1799 if (*hint)
1800 set_hintbar (hint);
1801 g_free (hint);
1802 } else {
1803 char text[BUF_SMALL];
1805 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1806 VERSION);
1807 set_hintbar (text);
1811 static void
1812 setup_panels_and_run_mc (void)
1814 midnight_dlg->get_shortcut = midnight_get_shortcut;
1816 add_widget (midnight_dlg, the_menubar);
1817 init_menu ();
1819 add_widget (midnight_dlg, get_panel_widget (0));
1820 add_widget (midnight_dlg, get_panel_widget (1));
1821 add_widget (midnight_dlg, the_hint);
1822 add_widget (midnight_dlg, cmdline);
1823 add_widget (midnight_dlg, the_prompt);
1825 add_widget (midnight_dlg, the_bar);
1826 midnight_set_buttonbar (the_bar);
1828 if (boot_current_is_left)
1829 dlg_select_widget (get_panel_widget (0));
1830 else
1831 dlg_select_widget (get_panel_widget (1));
1833 /* Run the Midnight Commander if no file was specified in the command line */
1834 run_dlg (midnight_dlg);
1837 /* result must be free'd (I think this should go in util.c) */
1838 static char *
1839 prepend_cwd_on_local (const char *filename)
1841 char *d;
1842 int l;
1844 if (vfs_file_is_local (filename)) {
1845 if (*filename == PATH_SEP) /* an absolute pathname */
1846 return g_strdup (filename);
1847 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1848 mc_get_current_wd (d, MC_MAXPATHLEN);
1849 l = strlen (d);
1850 d[l++] = PATH_SEP;
1851 strcpy (d + l, filename);
1852 canonicalize_pathname (d);
1853 return d;
1854 } else
1855 return g_strdup (filename);
1858 static int
1859 mc_maybe_editor_or_viewer (void)
1861 if (!(view_one_file || edit_one_file))
1862 return 0;
1864 /* Invoke the internal view/edit routine with:
1865 * the default processing and forcing the internal viewer/editor
1867 if (view_one_file) {
1868 char *path = NULL;
1869 path = prepend_cwd_on_local (view_one_file);
1870 view_file (path, 0, 1);
1871 g_free (path);
1873 #ifdef USE_INTERNAL_EDIT
1874 else {
1875 edit_file (edit_one_file, edit_one_file_start_line);
1877 #endif /* USE_INTERNAL_EDIT */
1878 midnight_shutdown = 1;
1879 done_mc ();
1880 return 1;
1883 /* Run the main dialog that occupies the whole screen */
1884 static void
1885 do_nc (void)
1887 int midnight_colors[DLG_COLOR_NUM];
1888 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1889 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1890 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1891 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1893 panel_init ();
1895 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1896 "[main]", NULL, DLG_WANT_IDLE);
1898 if (view_one_file || edit_one_file)
1899 setup_dummy_mc ();
1900 else
1901 setup_mc ();
1903 /* start check display_codepage and source_codepage */
1904 check_codeset ();
1906 main_map = default_main_map;
1907 if (main_keymap && main_keymap->len > 0)
1908 main_map = (global_keymap_t *) main_keymap->data;
1910 main_x_map = default_main_x_map;
1911 if (main_x_keymap && main_x_keymap->len > 0)
1912 main_x_map = (global_keymap_t *) main_x_keymap->data;
1914 panel_map = default_panel_keymap;
1915 if (panel_keymap && panel_keymap->len > 0)
1916 panel_map = (global_keymap_t *) panel_keymap->data;
1918 input_map = default_input_keymap;
1919 if (input_keymap && input_keymap->len > 0)
1920 input_map = (global_keymap_t *) input_keymap->data;
1922 tree_map = default_tree_keymap;
1923 if (tree_keymap && tree_keymap->len > 0)
1924 tree_map = (global_keymap_t *) tree_keymap->data;
1926 help_map = default_help_keymap;
1927 if (help_keymap && help_keymap->len > 0)
1928 help_map = (global_keymap_t *) help_keymap->data;
1930 /* Check if we were invoked as an editor or file viewer */
1931 if (!mc_maybe_editor_or_viewer ()) {
1932 setup_panels_and_run_mc ();
1934 /* Program end */
1935 midnight_shutdown = 1;
1937 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1938 if (mc_args__last_wd_file && vfs_current_is_local ())
1939 last_wd_string = g_strdup (current_panel->cwd);
1941 done_mc ();
1944 destroy_dlg (midnight_dlg);
1945 panel_deinit ();
1946 current_panel = 0;
1947 done_mc_profile ();
1950 /* POSIX version. The only version we support. */
1951 static void
1952 OS_Setup (void)
1954 const char *shell_env = getenv ("SHELL");
1955 const char *mc_libdir;
1957 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1958 struct passwd *pwd;
1959 pwd = getpwuid (geteuid ());
1960 if (pwd != NULL)
1961 shell = g_strdup (pwd->pw_shell);
1962 } else
1963 shell = g_strdup (shell_env);
1965 if ((shell == NULL) || (shell[0] == '\0')) {
1966 g_free (shell);
1967 shell = g_strdup ("/bin/sh");
1970 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1971 /* and can be overriden by the MC_DATADIR environment variable */
1972 mc_libdir = getenv ("MC_DATADIR");
1973 if (mc_libdir != NULL) {
1974 mc_home = g_strdup (mc_libdir);
1975 mc_home_alt = g_strdup (SYSCONFDIR);
1976 } else {
1977 mc_home = g_strdup (SYSCONFDIR);
1978 mc_home_alt = g_strdup (DATADIR);
1981 /* This variable is used by the subshell */
1982 home_dir = getenv ("HOME");
1984 if (!home_dir)
1985 home_dir = mc_home;
1988 static void
1989 sigchld_handler_no_subshell (int sig)
1991 #ifdef __linux__
1992 int pid, status;
1994 if (!console_flag)
1995 return;
1997 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1998 the value of console_flag never changed, we could simply not install
1999 this handler at all if (!console_flag && !use_subshell). */
2001 /* That comment is no longer true. We need to wait() on a sigchld
2002 handler (that's at least what the tarfs code expects currently). */
2004 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2006 if (pid == cons_saver_pid) {
2008 if (WIFSTOPPED (status)) {
2009 /* Someone has stopped cons.saver - restart it */
2010 kill (pid, SIGCONT);
2011 } else {
2012 /* cons.saver has died - disable console saving */
2013 handle_console (CONSOLE_DONE);
2014 console_flag = 0;
2017 /* If we got here, some other child exited; ignore it */
2018 #endif /* __linux__ */
2020 (void) sig;
2023 static void
2024 init_sigchld (void)
2026 struct sigaction sigchld_action;
2028 sigchld_action.sa_handler =
2029 #ifdef HAVE_SUBSHELL_SUPPORT
2030 use_subshell ? sigchld_handler :
2031 #endif /* HAVE_SUBSHELL_SUPPORT */
2032 sigchld_handler_no_subshell;
2034 sigemptyset (&sigchld_action.sa_mask);
2036 #ifdef SA_RESTART
2037 sigchld_action.sa_flags = SA_RESTART;
2038 #else
2039 sigchld_action.sa_flags = 0;
2040 #endif /* !SA_RESTART */
2042 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2043 #ifdef HAVE_SUBSHELL_SUPPORT
2045 * This may happen on QNX Neutrino 6, where SA_RESTART
2046 * is defined but not implemented. Fallback to no subshell.
2048 use_subshell = 0;
2049 #endif /* HAVE_SUBSHELL_SUPPORT */
2053 static void
2054 mc_main__setup_by_args (int argc, char *argv[])
2056 const char *base;
2057 char *tmp;
2059 if (mc_args__nomouse)
2060 use_mouse_p = MOUSE_DISABLED;
2062 #ifdef USE_NETCODE
2063 if (mc_args__netfs_logfile != NULL) {
2064 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
2065 #ifdef WITH_SMBFS
2066 smbfs_set_debugf (mc_args__netfs_logfile);
2067 #endif /* WITH_SMBFS */
2070 #ifdef WITH_SMBFS
2071 if (mc_args__debug_level != 0)
2072 smbfs_set_debug (mc_args__debug_level);
2073 #endif /* WITH_SMBFS */
2074 #endif /* USE_NETCODE */
2076 base = x_basename (argv[0]);
2077 tmp = (argc > 0) ? argv[1] : NULL;
2079 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2080 edit_one_file = "";
2081 if (tmp) {
2083 * Check for filename:lineno, followed by an optional colon.
2084 * This format is used by many programs (especially compilers)
2085 * in error messages and warnings. It is supported so that
2086 * users can quickly copy and paste file locations.
2088 char *end = tmp + strlen (tmp), *p = end;
2089 if (p > tmp && p[-1] == ':')
2090 p--;
2091 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2092 p--;
2093 if (tmp < p && p < end && p[-1] == ':') {
2094 struct stat st;
2095 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2097 * Check that the file before the colon actually exists.
2098 * If it doesn't exist, revert to the old behavior.
2100 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2101 edit_one_file = fname;
2102 edit_one_file_start_line = atoi (p);
2103 } else {
2104 g_free (fname);
2105 goto try_plus_filename;
2107 } else {
2108 try_plus_filename:
2109 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2110 int start_line = atoi (tmp);
2111 if (start_line > 0) {
2112 char *file = (argc > 1) ? argv[2] : NULL;
2113 if (file) {
2114 tmp = file;
2115 edit_one_file_start_line = start_line;
2119 edit_one_file = g_strdup (tmp);
2122 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2123 if (tmp)
2124 view_one_file = g_strdup (tmp);
2125 else {
2126 fputs ("No arguments given to the viewer\n", stderr);
2127 exit (1);
2129 } else {
2130 /* sets the current dir and the other dir */
2131 if (tmp) {
2132 this_dir = g_strdup (tmp);
2133 tmp = (argc > 1) ? argv[2] : NULL;
2134 if (tmp)
2135 other_dir = g_strdup (tmp);
2141 main (int argc, char *argv[])
2143 struct stat s;
2144 char *mc_dir;
2145 GError *error = NULL;
2146 gboolean isInitialized;
2148 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2149 setlocale (LC_ALL, "");
2150 bindtextdomain ("mc", LOCALEDIR);
2151 textdomain ("mc");
2153 /* Set up temporary directory */
2154 mc_tmpdir ();
2156 OS_Setup ();
2158 str_init_strings (NULL);
2160 vfs_init ();
2162 #ifdef USE_INTERNAL_EDIT
2163 edit_stack_init ();
2164 #endif
2166 #ifdef HAVE_SLANG
2167 SLtt_Ignore_Beep = 1;
2168 #endif
2170 if ( !mc_args_handle (&argc, &argv, "mc"))
2171 return 1;
2173 mc_main__setup_by_args (argc,argv);
2175 /* NOTE: This has to be called before tty_init or whatever routine
2176 calls any define_sequence */
2177 init_key ();
2179 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2180 handle_console (CONSOLE_INIT);
2182 #ifdef HAVE_SUBSHELL_SUPPORT
2183 /* Don't use subshell when invoked as viewer or editor */
2184 if (edit_one_file || view_one_file)
2185 use_subshell = 0;
2187 if (use_subshell)
2188 subshell_get_console_attributes ();
2189 #endif /* HAVE_SUBSHELL_SUPPORT */
2191 /* Install the SIGCHLD handler; must be done before init_subshell() */
2192 init_sigchld ();
2194 /* We need this, since ncurses endwin () doesn't restore the signals */
2195 save_stop_handler ();
2197 /* Must be done before init_subshell, to set up the terminal size: */
2198 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2199 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2201 load_setup ();
2203 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2205 isInitialized = mc_skin_init(&error);
2207 mc_filehighlight = mc_fhl_new (TRUE);
2209 dlg_set_default_colors ();
2211 if ( ! isInitialized ) {
2212 message (D_ERROR, _("Warning"), error->message);
2213 g_error_free(error);
2214 error = NULL;
2217 /* create home directory */
2218 /* do it after the screen library initialization to show the error message */
2219 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2220 canonicalize_pathname (mc_dir);
2221 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2222 && mkdir (mc_dir, 0700) != 0)
2223 message (D_ERROR, _("Warning"),
2224 _("Cannot create %s directory"), mc_dir);
2225 g_free (mc_dir);
2227 #ifdef HAVE_SUBSHELL_SUPPORT
2228 /* Done here to ensure that the subshell doesn't */
2229 /* inherit the file descriptors opened below, etc */
2230 if (use_subshell)
2231 init_subshell ();
2233 #endif /* HAVE_SUBSHELL_SUPPORT */
2235 /* Removing this from the X code let's us type C-c */
2236 load_key_defs ();
2238 load_keymap_defs ();
2240 /* Also done after init_subshell, to save any shell init file messages */
2241 if (console_flag)
2242 handle_console (CONSOLE_SAVE);
2244 if (alternate_plus_minus)
2245 application_keypad_mode ();
2247 #ifdef HAVE_SUBSHELL_SUPPORT
2248 if (use_subshell) {
2249 prompt = strip_ctrl_codes (subshell_prompt);
2250 if (!prompt)
2251 prompt = "";
2252 } else
2253 #endif /* HAVE_SUBSHELL_SUPPORT */
2254 prompt = (geteuid () == 0) ? "# " : "$ ";
2257 /* Program main loop */
2258 if (!midnight_shutdown)
2259 do_nc ();
2261 /* Save the tree store */
2262 tree_store_save ();
2264 free_keymap_defs ();
2266 /* Virtual File System shutdown */
2267 vfs_shut ();
2269 flush_extension_file (); /* does only free memory */
2271 mc_fhl_free (&mc_filehighlight);
2272 mc_skin_deinit ();
2274 tty_shutdown ();
2276 if (console_flag && !(quit & SUBSHELL_EXIT))
2277 handle_console (CONSOLE_RESTORE);
2278 if (alternate_plus_minus)
2279 numeric_keypad_mode ();
2281 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2283 if (console_flag)
2284 handle_console (CONSOLE_DONE);
2285 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2287 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2288 && !edit_one_file && !view_one_file) {
2289 int last_wd_fd =
2290 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2291 S_IRUSR | S_IWUSR);
2293 if (last_wd_fd != -1) {
2294 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2295 close (last_wd_fd);
2298 g_free (last_wd_string);
2300 g_free (mc_home_alt);
2301 g_free (mc_home);
2302 g_free (shell);
2304 done_key ();
2305 #ifdef HAVE_CHARSET
2306 free_codepages_list ();
2307 #endif
2308 str_uninit_strings ();
2310 g_free (this_dir);
2311 g_free (other_dir);
2313 #ifdef USE_INTERNAL_EDIT
2314 edit_stack_free ();
2315 #endif
2317 return 0;