Ticket #2170: Color collisions
[midnight-commander.git] / src / main.c
blob66e6b054b12eddb4b59c4a1326d0684baa88f84d
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
6 1994, 1995 Janne Kukonlehto
7 1997 Norbert Warmuth
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file main.c
24 * \brief Source: this is a main module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pwd.h> /* for username in xterm title */
42 #include "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/tty/mouse.h"
46 #include "lib/tty/key.h" /* For init_key() */
47 #include "lib/tty/win.h" /* xterm_flag */
48 #include "lib/skin.h"
49 #include "lib/mcconfig.h"
50 #include "lib/filehighlight.h"
51 #include "lib/fileloc.h" /* MC_USERCONF_DIR */
52 #include "lib/strutil.h"
54 #include "lib/vfs/mc-vfs/vfs.h" /* vfs_translate_url() */
55 #ifdef ENABLE_VFS_SMB
56 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debug() */
57 #endif /* ENABLE_VFS_SMB */
59 #include "args.h"
60 #include "dir.h"
61 #include "dialog.h"
62 #include "menu.h"
63 #include "panel.h"
64 #include "option.h"
65 #include "tree.h"
66 #include "treestore.h"
67 #include "consaver/cons.saver.h"
68 #include "subshell.h"
69 #include "setup.h" /* save_setup() */
70 #include "boxes.h" /* sort_box() */
71 #include "layout.h"
72 #include "cmd.h" /* Normal commands */
73 #include "hotlist.h"
74 #include "panelize.h"
75 #include "learn.h" /* learn_keys() */
76 #include "listmode.h"
77 #include "execute.h"
78 #include "ext.h" /* For flush_extension_file() */
79 #include "widget.h"
80 #include "command.h"
81 #include "wtools.h"
82 #include "cmddef.h" /* CK_ cmd name const */
83 #include "user.h" /* user_file_menu_cmd() */
84 #include "dialog-switch.h"
86 #include "chmod.h"
87 #include "chown.h"
88 #include "achown.h"
90 #include "main.h"
92 #ifdef USE_INTERNAL_EDIT
93 #include "src/editor/edit.h"
94 #endif
96 #ifdef USE_DIFF_VIEW
97 #include "src/diffviewer/ydiff.h"
98 #endif
100 #ifdef HAVE_CHARSET
101 #include "charsets.h"
102 #include "selcodepage.h"
103 #endif /* HAVE_CHARSET */
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, at startup the user-menu is invoked */
145 int auto_menu = 0;
147 /* If true, then the +, - and \ keys have their special meaning only if the
148 * command line is emtpy, otherwise they behave like regular letters
150 int only_leading_plus_minus = 1;
152 int pause_after_run = pause_on_dumb_terminals;
154 /* It true saves the setup when quitting */
155 int auto_save_setup = 1;
157 #ifdef HAVE_CHARSET
159 * Don't restrict the output on the screen manager level,
160 * the translation tables take care of it.
162 #define full_eight_bits (1)
163 #define eight_bit_clean (1)
164 #else /* HAVE_CHARSET */
165 /* If true, allow characters in the range 160-255 */
166 int eight_bit_clean = 1;
168 * If true, also allow characters in the range 128-159.
169 * This is reported to break on many terminals (xterm, qansi-m).
171 int full_eight_bits = 0;
172 #endif /* !HAVE_CHARSET */
175 * If utf-8 terminal utf8_display = 1
176 * Display bits set UTF-8
179 int utf8_display = 0;
181 /* If true use the internal viewer */
182 int use_internal_view = 1;
184 /* The prompt */
185 const char *mc_prompt = NULL;
187 /* The widget where we draw the prompt */
188 WLabel *the_prompt;
190 /* The hint bar */
191 WLabel *the_hint;
193 /* The button bar */
194 WButtonBar *the_bar;
196 /* Mouse type: GPM, xterm or none */
197 Mouse_Type use_mouse_p = MOUSE_NONE;
199 /* If on, default for "No" in delete operations */
200 int safe_delete = 0;
202 /* Controls screen clearing before an exec */
203 int clear_before_exec = 1;
205 /* Asks for confirmation before deleting a file */
206 int confirm_delete = 1;
208 /* Asks for confirmation before deleting a hotlist entry */
209 int confirm_directory_hotlist_delete = 1;
211 /* Asks for confirmation before overwriting a file */
212 int confirm_overwrite = 1;
214 /* Asks for confirmation before executing a program by pressing enter */
215 int confirm_execute = 0;
217 /* Asks for confirmation before leaving the program */
218 int confirm_exit = 1;
220 /* Asks for confirmation before clean up of history */
221 int confirm_history_cleanup = 1;
223 /* Asks for confirmation when using F3 to view a directory and there
224 are tagged files */
225 int confirm_view_dir = 0;
227 /* This flag indicates if the pull down menus by default drop down */
228 int drop_menus = 0;
230 /* The dialog handle for the main program */
231 Dlg_head *midnight_dlg = NULL;
233 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
234 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
235 int update_prompt = 0;
237 /* The home directory */
238 const char *home_dir = NULL;
240 /* Only used at program boot */
241 int boot_current_is_left = 1;
243 /* If this is true, then when browsing the tree the other window will
244 * automatically reload it's directory with the contents of the currently
245 * selected directory.
247 int xtree_mode = 0;
249 /* path to X clipboard utility */
250 char* clipboard_store_path = NULL;
251 char* clipboard_paste_path = NULL;
253 /* If set, then print to the given file the last directory we were at */
254 static char *last_wd_string = NULL;
255 /* Set to 1 to suppress printing the last directory */
256 static int print_last_revert = 0;
258 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
259 char *mc_run_param0 = NULL;
260 char *mc_run_param1 = NULL;
262 /* Used so that widgets know if they are being destroyed or
263 shut down */
264 int midnight_shutdown = 0;
266 /* The user's shell */
267 char *shell = NULL;
269 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
270 char *mc_home = NULL;
272 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
273 char *mc_home_alt = NULL;
275 /* Define this function for glib-style error handling */
276 GQuark
277 mc_main_error_quark (void)
279 return g_quark_from_static_string (PACKAGE);
282 /* Save current stat of directories to avoid reloading the panels */
283 /* when no modifications have taken place */
284 void
285 save_cwds_stat (void)
287 if (panels_options.fast_reload)
289 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
290 if (get_other_type () == view_listing)
291 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
295 #ifdef HAVE_SUBSHELL_SUPPORT
296 void
297 do_update_prompt (void)
299 if (update_prompt)
301 printf ("\r\n%s", subshell_prompt);
302 fflush (stdout);
303 update_prompt = 0;
306 #endif /* HAVE_SUBSHELL_SUPPORT */
308 void
309 change_panel (void)
311 free_completions (cmdline);
312 dlg_one_down (midnight_dlg);
315 /* Stop MC main dialog and the current dialog if it exists.
316 * Needed to provide fast exit from MC viewer or editor on shell exit */
317 static void
318 stop_dialogs (void)
320 midnight_dlg->state = DLG_CLOSED;
322 if ((top_dlg != NULL) && (top_dlg->data != NULL))
323 ((Dlg_head *) top_dlg->data)->state = DLG_CLOSED;
326 static gboolean
327 quit_cmd_internal (int quiet)
329 int q = quit;
330 size_t n = dialog_switch_num () - 1;
332 if (n != 0)
334 char msg[BUF_MEDIUM];
336 g_snprintf (msg, sizeof (msg),
337 ngettext ("You have %zd opened screen. Quit anyway?",
338 "You have %zd opened screens. Quit anyway?", n),
341 if (query_dialog (_("The Midnight Commander"), msg,
342 D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
343 return FALSE;
344 q = 1;
345 } else if (quiet || !confirm_exit)
346 q = 1;
347 else if (query_dialog (_("The Midnight Commander"),
348 _("Do you really want to quit the Midnight Commander?"),
349 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
350 q = 1;
352 if (q != 0)
354 #ifdef HAVE_SUBSHELL_SUPPORT
355 if (!use_subshell)
356 stop_dialogs ();
357 else if ((q = exit_subshell ()))
358 #endif
359 stop_dialogs ();
362 if (q != 0)
363 quit |= 1;
364 return (quit != 0);
367 static gboolean
368 quit_cmd (void)
370 return quit_cmd_internal (0);
373 gboolean
374 quiet_quit_cmd (void)
376 print_last_revert = 1;
377 return quit_cmd_internal (1);
380 /* Wrapper for do_subshell_chdir, check for availability of subshell */
381 void
382 subshell_chdir (const char *directory)
384 #ifdef HAVE_SUBSHELL_SUPPORT
385 if (use_subshell)
387 if (vfs_current_is_local ())
388 do_subshell_chdir (directory, 0, 1);
390 #endif /* HAVE_SUBSHELL_SUPPORT */
394 do_cd (const char *new_dir, enum cd_enum exact)
396 gboolean res;
398 res = do_panel_cd (current_panel, new_dir, exact);
400 #if HAVE_CHARSET
401 if (res)
403 const char *enc_name;
405 enc_name = vfs_get_encoding (current_panel->cwd);
406 if (enc_name != NULL)
407 current_panel->codepage = get_codepage_index (enc_name);
408 else
409 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
411 #endif /* HAVE_CHARSET */
413 return res ? 1 : 0;
416 #ifdef HAVE_SUBSHELL_SUPPORT
418 load_prompt (int fd, void *unused)
420 (void) fd;
421 (void) unused;
423 if (!read_subshell_prompt ())
424 return 0;
426 /* Don't actually change the prompt if it's invisible */
427 if (((Dlg_head *) top_dlg->data == midnight_dlg) && command_prompt)
429 char *tmp_prompt;
430 int prompt_len;
432 tmp_prompt = strip_ctrl_codes (subshell_prompt);
433 prompt_len = str_term_width1 (tmp_prompt);
435 /* Check for prompts too big */
436 if (COLS > 8 && prompt_len > COLS - 8)
438 prompt_len = COLS - 8;
439 tmp_prompt[prompt_len] = '\0';
441 mc_prompt = tmp_prompt;
442 label_set_text (the_prompt, mc_prompt);
443 winput_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
445 /* since the prompt has changed, and we are called from one of the
446 * tty_get_event channels, the prompt updating does not take place
447 * automatically: force a cursor update and a screen refresh
449 update_cursor (midnight_dlg);
450 mc_refresh ();
452 update_prompt = 1;
453 return 0;
455 #endif /* HAVE_SUBSHELL_SUPPORT */
457 void
458 sort_cmd (void)
460 WPanel *p;
461 const panel_field_t *sort_order;
463 if (!SELECTED_IS_PANEL)
464 return;
466 p = MENU_PANEL;
467 sort_order = sort_box (p->current_sort_field, &p->reverse, &p->case_sensitive, &p->exec_first);
469 panel_set_sort_order (p, sort_order);
473 static void
474 treebox_cmd (void)
476 char *sel_dir;
478 sel_dir = tree_box (selection (current_panel)->fname);
479 if (sel_dir)
481 do_cd (sel_dir, cd_exact);
482 g_free (sel_dir);
486 #ifdef LISTMODE_EDITOR
487 static void
488 listmode_cmd (void)
490 char *newmode;
492 if (get_current_type () != view_listing)
493 return;
495 newmode = listmode_edit (current_panel->user_format);
496 if (!newmode)
497 return;
499 g_free (current_panel->user_format);
500 current_panel->list_type = list_user;
501 current_panel->user_format = newmode;
502 set_panel_formats (current_panel);
504 do_refresh ();
506 #endif /* LISTMODE_EDITOR */
508 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
509 static GList *
510 create_panel_menu (void)
512 GList *entries = NULL;
514 entries = g_list_append (entries, menu_entry_create (_("File listin&g"), CK_ListingCmd));
515 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
516 entries = g_list_append (entries, menu_entry_create (_("&Info"), CK_InfoCmd));
517 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
518 entries = g_list_append (entries, menu_separator_create ());
519 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ChangeListingCmd));
520 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
521 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
522 #ifdef HAVE_CHARSET
523 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
524 #endif
525 #ifdef ENABLE_VFS_NET
526 entries = g_list_append (entries, menu_separator_create ());
527 #ifdef ENABLE_VFS_FTP
528 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
529 #endif
530 #ifdef ENABLE_VFS_FISH
531 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
532 #endif
533 #ifdef ENABLE_VFS_SMB
534 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
535 #endif
536 #endif /* ENABLE_VFS_NET */
537 entries = g_list_append (entries, menu_separator_create ());
538 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
540 return entries;
543 static GList *
544 create_file_menu (void)
546 GList *entries = NULL;
548 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
549 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
550 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
551 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
552 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
553 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
554 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
555 entries = g_list_append (entries, menu_entry_create (_("&Symlink"), CK_SymlinkCmd));
556 entries = g_list_append (entries, menu_entry_create (_("Relative symlin&k"), CK_RelativeSymlinkCmd));
557 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
558 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
559 entries =
560 g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
561 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
562 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
563 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
564 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
565 entries = g_list_append (entries, menu_separator_create ());
566 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
567 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
568 entries =
569 g_list_append (entries,
570 menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
571 entries = g_list_append (entries, menu_separator_create ());
572 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
574 return entries;
577 static GList *
578 create_command_menu (void)
580 /* I know, I'm lazy, but the tree widget when it's not running
581 * as a panel still has some problems, I have not yet finished
582 * the WTree widget port, sorry.
584 GList *entries = NULL;
586 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
587 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
588 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
589 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
590 entries =
591 g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
592 entries =
593 g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
594 #ifdef USE_DIFF_VIEW
595 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
596 #endif
597 entries =
598 g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
599 entries =
600 g_list_append (entries,
601 menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
602 entries = g_list_append (entries, menu_separator_create ());
603 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
604 entries =
605 g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
606 #ifdef ENABLE_VFS
607 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
608 #endif
609 #ifdef WITH_BACKGROUND
610 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
611 #endif
612 entries = g_list_append (entries, menu_entry_create (_("Screen lis&t"), CK_DialogListCmd));
613 entries = g_list_append (entries, menu_separator_create ());
614 #ifdef ENABLE_VFS_UNDELFS
615 entries =
616 g_list_append (entries,
617 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
618 #endif
619 #ifdef LISTMODE_EDITOR
620 entries =
621 g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
622 #endif
623 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
624 entries = g_list_append (entries, menu_separator_create ());
625 #endif
626 entries =
627 g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
628 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
629 entries =
630 g_list_append (entries,
631 menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
633 return entries;
636 static GList *
637 create_options_menu (void)
639 GList *entries = NULL;
641 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
642 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutBox));
643 entries = g_list_append (entries, menu_entry_create (_("&Panel options..."), CK_PanelOptionsBox));
644 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
645 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
646 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
647 #ifdef ENABLE_VFS
648 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
649 #endif
650 entries = g_list_append (entries, menu_separator_create ());
651 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
653 return entries;
656 void
657 init_menu (void)
659 menubar_add_menu (the_menubar,
660 create_menu (horizontal_split ? _("&Above") : _("&Left"),
661 create_panel_menu (), "[Left and Right Menus]"));
662 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
663 menubar_add_menu (the_menubar,
664 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
665 menubar_add_menu (the_menubar,
666 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
667 menubar_add_menu (the_menubar,
668 create_menu (horizontal_split ? _("&Below") : _("&Right"),
669 create_panel_menu (), "[Left and Right Menus]"));
672 void
673 done_menu (void)
675 menubar_set_menu (the_menubar, NULL);
678 static void
679 menu_last_selected_cmd (void)
681 the_menubar->is_active = TRUE;
682 the_menubar->is_dropped = (drop_menus != 0);
683 the_menubar->previous_widget = dlg_get_current_widget_id (midnight_dlg);
684 dlg_select_widget (the_menubar);
687 static void
688 menu_cmd (void)
690 if (the_menubar->is_active)
691 return;
693 if ((get_current_index () == 0) == (current_panel->active != 0))
694 the_menubar->selected = 0;
695 else
696 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
697 menu_last_selected_cmd ();
700 static char *
701 midnight_get_shortcut (unsigned long command)
703 const char *ext_map;
704 const char *shortcut = NULL;
706 shortcut = lookup_keymap_shortcut (main_map, command);
707 if (shortcut != NULL)
708 return g_strdup (shortcut);
710 shortcut = lookup_keymap_shortcut (panel_map, command);
711 if (shortcut != NULL)
712 return g_strdup (shortcut);
714 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
715 if (ext_map != NULL)
716 shortcut = lookup_keymap_shortcut (main_x_map, command);
717 if (shortcut != NULL)
718 return g_strdup_printf ("%s %s", ext_map, shortcut);
720 return NULL;
723 static char *
724 midnight_get_title (const Dlg_head *h, size_t len)
726 /* TODO: share code with update_xterm_title_path() */
728 const char *path;
729 char host[BUF_TINY];
730 char *p;
731 struct passwd *pw = NULL;
732 char *login = NULL;
733 int res = 0;
735 (void) h;
737 path = strip_home_and_password (current_panel->cwd);
738 res = gethostname (host, sizeof (host));
739 if (res != 0)
740 host[0] = '\0';
741 else
742 host [sizeof (host) - 1] = '\0';
744 pw = getpwuid (getuid ());
745 if (pw != NULL)
746 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
747 else
748 login = g_strdup (host);
750 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
751 path = str_trunc (p, len - 4);
752 g_free (login);
753 g_free (p);
755 return g_strdup (path);
758 void
759 toggle_show_hidden (void)
761 panels_options.show_dot_files = !panels_options.show_dot_files;
762 update_panels (UP_RELOAD, UP_KEEPSEL);
765 static void
766 toggle_panels_split (void)
768 horizontal_split = !horizontal_split;
769 layout_change ();
770 do_refresh ();
774 * Just a hack for allowing url-like pathnames to be accepted from the
775 * command line.
777 static void
778 translated_mc_chdir (char *dir)
780 char *newdir;
781 int ret;
783 newdir = vfs_translate_url (dir);
784 ret = mc_chdir (newdir);
785 g_free (newdir);
788 static void
789 create_panels (void)
791 int current_index;
792 int other_index;
793 panel_view_mode_t current_mode, other_mode;
794 char original_dir[BUF_1K] = "\0";
796 if (boot_current_is_left)
798 current_index = 0;
799 other_index = 1;
800 current_mode = startup_left_mode;
801 other_mode = startup_right_mode;
803 else
805 current_index = 1;
806 other_index = 0;
807 current_mode = startup_right_mode;
808 other_mode = startup_left_mode;
810 /* Creates the left panel */
811 if (mc_run_param0 != NULL)
813 if (mc_run_param1 != NULL)
815 /* Ok, user has specified two dirs, save the original one,
816 * since we may not be able to chdir to the proper
817 * second directory later
819 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
821 translated_mc_chdir (mc_run_param0);
823 set_display_type (current_index, current_mode);
825 /* The other panel */
826 if (mc_run_param1 != NULL)
828 if (original_dir[0] != '\0')
829 translated_mc_chdir (original_dir);
830 translated_mc_chdir (mc_run_param1);
832 set_display_type (other_index, other_mode);
834 if (startup_left_mode == view_listing)
836 current_panel = left_panel;
838 else
840 if (right_panel)
841 current_panel = right_panel;
842 else
843 current_panel = left_panel;
846 /* Create the nice widgets */
847 cmdline = command_new (0, 0, 0);
848 the_prompt = label_new (0, 0, mc_prompt);
849 the_prompt->transparent = 1;
850 the_bar = buttonbar_new (keybar_visible);
852 the_hint = label_new (0, 0, 0);
853 the_hint->transparent = 1;
854 the_hint->auto_adjust_cols = 0;
855 the_hint->widget.cols = COLS;
857 the_menubar = menubar_new (0, 0, COLS, NULL);
860 static void
861 copy_current_pathname (void)
863 char *cwd_path;
864 if (!command_prompt)
865 return;
867 cwd_path = remove_encoding_from_path (current_panel->cwd);
868 command_insert (cmdline, cwd_path, 0);
870 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
871 command_insert (cmdline, PATH_SEP_STR, 0);
872 g_free (cwd_path);
875 static void
876 copy_other_pathname (void)
878 char *cwd_path;
880 if (get_other_type () != view_listing)
881 return;
883 if (!command_prompt)
884 return;
886 cwd_path = remove_encoding_from_path (other_panel->cwd);
887 command_insert (cmdline, cwd_path, 0);
889 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
890 command_insert (cmdline, PATH_SEP_STR, 0);
891 g_free (cwd_path);
894 static void
895 copy_readlink (WPanel * panel)
897 if (!command_prompt)
898 return;
899 if (S_ISLNK (selection (panel)->st.st_mode))
901 char buffer[MC_MAXPATHLEN];
902 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
903 int i;
905 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
906 g_free (p);
907 if (i > 0)
909 buffer[i] = 0;
910 command_insert (cmdline, buffer, 1);
915 static void
916 copy_current_readlink (void)
918 copy_readlink (current_panel);
921 static void
922 copy_other_readlink (void)
924 if (get_other_type () == view_listing)
925 copy_readlink (other_panel);
928 /* Insert the selected file name into the input line */
929 static void
930 copy_prog_name (void)
932 char *tmp;
933 if (!command_prompt)
934 return;
936 if (get_current_type () == view_tree)
938 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
939 tmp = tree_selected_name (tree);
941 else
942 tmp = selection (current_panel)->fname;
944 command_insert (cmdline, tmp, 1);
947 static void
948 copy_tagged (WPanel * panel)
950 int i;
952 if (!command_prompt)
953 return;
954 input_disable_update (cmdline);
955 if (panel->marked)
957 for (i = 0; i < panel->count; i++)
959 if (panel->dir.list[i].f.marked)
960 command_insert (cmdline, panel->dir.list[i].fname, 1);
963 else
965 command_insert (cmdline, panel->dir.list[panel->selected].fname, 1);
967 input_enable_update (cmdline);
970 static void
971 copy_current_tagged (void)
973 copy_tagged (current_panel);
976 static void
977 copy_other_tagged (void)
979 if (get_other_type () == view_listing)
980 copy_tagged (other_panel);
983 void
984 midnight_set_buttonbar (WButtonBar * b)
986 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
987 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
988 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
989 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
990 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
991 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
992 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
993 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
994 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
995 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
998 static gboolean ctl_x_map_enabled = FALSE;
1000 static void
1001 ctl_x_cmd (void)
1003 ctl_x_map_enabled = TRUE;
1006 static cb_ret_t
1007 midnight_execute_cmd (Widget * sender, unsigned long command)
1009 cb_ret_t res = MSG_HANDLED;
1011 (void) sender;
1013 /* stop quick search before executing any command */
1014 send_message ((Widget *) current_panel, WIDGET_COMMAND, CK_PanelStopSearch);
1016 switch (command)
1018 case CK_AddHotlist:
1019 add2hotlist_cmd ();
1020 break;
1021 case CK_ChangeListingCmd:
1022 change_listing_cmd ();
1023 break;
1024 case CK_ChmodCmd:
1025 chmod_cmd ();
1026 break;
1027 case CK_ChownCmd:
1028 chown_cmd ();
1029 break;
1030 case CK_ChownAdvancedCmd:
1031 chown_advanced_cmd ();
1032 break;
1033 case CK_CompareDirsCmd:
1034 compare_dirs_cmd ();
1035 break;
1036 case CK_ConfigureBox:
1037 configure_box ();
1038 break;
1039 #ifdef ENABLE_VFS
1040 case CK_ConfigureVfs:
1041 configure_vfs ();
1042 break;
1043 #endif
1044 case CK_ConfirmBox:
1045 confirm_box ();
1046 break;
1047 case CK_CopyCmd:
1048 copy_cmd ();
1049 break;
1050 case CK_CopyCurrentPathname:
1051 copy_current_pathname ();
1052 break;
1053 case CK_CopyCurrentReadlink:
1054 copy_current_readlink ();
1055 break;
1056 case CK_CopyCurrentTagged:
1057 copy_current_tagged ();
1058 break;
1059 case CK_CopyOtherPathname:
1060 copy_other_pathname ();
1061 break;
1062 case CK_CopyOtherReadlink:
1063 copy_other_readlink ();
1064 break;
1065 case CK_CopyOtherTagged:
1066 copy_other_tagged ();
1067 break;
1068 case CK_DeleteCmd:
1069 delete_cmd ();
1070 break;
1071 case CK_DialogListCmd:
1072 dialog_switch_list ();
1073 break;
1074 #ifdef USE_DIFF_VIEW
1075 case CK_DiffViewCmd:
1076 diff_view_cmd ();
1077 break;
1078 #endif
1079 case CK_DisplayBitsBox:
1080 display_bits_box ();
1081 break;
1082 case CK_EditCmd:
1083 edit_cmd ();
1084 break;
1085 #ifdef USE_INTERNAL_EDIT
1086 case CK_EditForceInternalCmd:
1087 edit_cmd_force_internal ();
1088 break;
1089 #endif
1090 case CK_EditExtFileCmd:
1091 ext_cmd ();
1092 break;
1093 case CK_EditFhlFileCmd:
1094 edit_fhl_cmd ();
1095 break;
1096 case CK_EditMcMenuCmd:
1097 edit_mc_menu_cmd ();
1098 break;
1099 case CK_EditSymlinkCmd:
1100 edit_symlink_cmd ();
1101 break;
1102 case CK_ExternalPanelize:
1103 external_panelize ();
1104 break;
1105 case CK_FilterCmd:
1106 filter_cmd ();
1107 break;
1108 case CK_FilteredViewCmd:
1109 filtered_view_cmd ();
1110 break;
1111 case CK_FindCmd:
1112 find_cmd ();
1113 break;
1114 #ifdef ENABLE_VFS_FISH
1115 case CK_FishlinkCmd:
1116 fishlink_cmd ();
1117 break;
1118 #endif
1119 #ifdef ENABLE_VFS_FTP
1120 case CK_FtplinkCmd:
1121 ftplink_cmd ();
1122 break;
1123 #endif
1124 case CK_HelpCmd:
1125 help_cmd ();
1126 break;
1127 case CK_HistoryCmd:
1128 history_cmd ();
1129 break;
1130 case CK_InfoCmd:
1131 if (sender == (Widget *) the_menubar)
1132 info_cmd (); /* mwnu */
1133 else
1134 info_cmd_no_menu (); /* shortcut or buttonbar */
1135 break;
1136 #ifdef WITH_BACKGROUND
1137 case CK_JobsCmd:
1138 jobs_cmd ();
1139 break;
1140 #endif
1141 case CK_LayoutBox:
1142 layout_box ();
1143 break;
1144 case CK_LearnKeys:
1145 learn_keys ();
1146 break;
1147 case CK_LinkCmd:
1148 link_cmd (LINK_HARDLINK);
1149 break;
1150 case CK_ListingCmd:
1151 listing_cmd ();
1152 break;
1153 #ifdef LISTMODE_EDITOR
1154 case CK_ListmodeCmd:
1155 listmode_cmd ();
1156 break;
1157 #endif
1158 case CK_MenuCmd:
1159 menu_cmd ();
1160 break;
1161 case CK_MenuLastSelectedCmd:
1162 menu_last_selected_cmd ();
1163 break;
1164 case CK_MkdirCmd:
1165 mkdir_cmd ();
1166 break;
1167 case CK_PanelOptionsBox:
1168 panel_options_box ();
1169 break;
1170 #ifdef HAVE_CHARSET
1171 case CK_PanelSetPanelEncoding:
1172 encoding_cmd ();
1173 break;
1174 #endif
1175 case CK_QuickCdCmd:
1176 quick_cd_cmd ();
1177 break;
1178 case CK_QuickChdirCmd:
1179 quick_chdir_cmd ();
1180 break;
1181 case CK_QuickViewCmd:
1182 if (sender == (Widget *) the_menubar)
1183 quick_view_cmd (); /* menu */
1184 else
1185 quick_cmd_no_menu (); /* shortcut or buttonabr */
1186 break;
1187 case CK_QuietQuitCmd:
1188 quiet_quit_cmd ();
1189 break;
1190 case CK_QuitCmd:
1191 quit_cmd ();
1192 break;
1193 case CK_RelativeSymlinkCmd:
1194 link_cmd (LINK_SYMLINK_RELATIVE);
1195 break;
1196 case CK_RenameCmd:
1197 rename_cmd ();
1198 break;
1199 case CK_RereadCmd:
1200 reread_cmd ();
1201 break;
1202 #ifdef ENABLE_VFS
1203 case CK_ReselectVfs:
1204 reselect_vfs ();
1205 break;
1206 #endif
1207 case CK_ReverseSelectionCmd:
1208 reverse_selection_cmd ();
1209 break;
1210 case CK_SaveSetupCmd:
1211 save_setup_cmd ();
1212 break;
1213 case CK_SelectCmd:
1214 select_cmd ();
1215 break;
1216 case CK_ShowCommandLine:
1217 view_other_cmd ();
1218 break;
1219 case CK_SingleDirsizeCmd:
1220 smart_dirsize_cmd ();
1221 break;
1222 #ifdef ENABLE_VFS_SMB
1223 case CK_SmblinkCmd:
1224 smblink_cmd ();
1225 break;
1226 #endif /* ENABLE_VFS_SMB */
1227 case CK_Sort:
1228 sort_cmd ();
1229 break;
1230 case CK_StartExtMap1:
1231 ctl_x_cmd ();
1232 break;
1233 case CK_SuspendCmd:
1234 suspend_cmd ();
1235 break;
1236 case CK_SwapCmd:
1237 swap_cmd ();
1238 break;
1239 case CK_SymlinkCmd:
1240 link_cmd (LINK_SYMLINK_ABSOLUTE);
1241 break;
1242 case CK_ToggleListingCmd:
1243 toggle_listing_cmd ();
1244 break;
1245 case CK_ToggleShowHidden:
1246 toggle_show_hidden ();
1247 break;
1248 case CK_TogglePanelsSplit:
1249 toggle_panels_split ();
1250 break;
1251 case CK_TreeCmd:
1252 tree_cmd ();
1253 break;
1254 case CK_TreeBoxCmd:
1255 treebox_cmd ();
1256 break;
1257 #ifdef ENABLE_VFS_UNDELFS
1258 case CK_UndeleteCmd:
1259 undelete_cmd ();
1260 break;
1261 #endif
1262 case CK_UnselectCmd:
1263 unselect_cmd ();
1264 break;
1265 case CK_UserMenuCmd:
1266 user_file_menu_cmd ();
1267 break;
1268 case CK_ViewCmd:
1269 view_cmd ();
1270 break;
1271 case CK_ViewFileCmd:
1272 view_file_cmd ();
1273 break;
1274 default:
1275 res = MSG_NOT_HANDLED;
1278 return res;
1281 static void
1282 init_xterm_support (void)
1284 const char *termvalue;
1286 termvalue = getenv ("TERM");
1287 if (!termvalue || !(*termvalue))
1289 fputs (_("The TERM environment variable is unset!\n"), stderr);
1290 exit (EXIT_FAILURE);
1293 /* Check mouse capabilities */
1294 xmouse_seq = tty_tgetstr ("Km");
1296 if (strcmp (termvalue, "cygwin") == 0)
1298 mc_args__force_xterm = 1;
1299 use_mouse_p = MOUSE_DISABLED;
1302 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1303 || strncmp (termvalue, "konsole", 7) == 0
1304 || strncmp (termvalue, "rxvt", 4) == 0
1305 || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0)
1307 xterm_flag = 1;
1309 /* Default to the standard xterm sequence */
1310 if (!xmouse_seq)
1312 xmouse_seq = ESC_STR "[M";
1315 /* Enable mouse unless explicitly disabled by --nomouse */
1316 if (use_mouse_p != MOUSE_DISABLED)
1318 const char *color_term = getenv ("COLORTERM");
1319 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1320 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1321 strcmp (termvalue, "Eterm") == 0)
1323 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1325 else
1327 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1333 static void
1334 setup_mc (void)
1336 #ifdef HAVE_SLANG
1337 tty_display_8bit (full_eight_bits != 0);
1338 #else
1339 tty_display_8bit (eight_bit_clean != 0);
1340 #endif
1342 #ifdef HAVE_SUBSHELL_SUPPORT
1343 if (use_subshell)
1344 add_select_channel (subshell_pty, load_prompt, 0);
1345 #endif /* !HAVE_SUBSHELL_SUPPORT */
1347 tty_setup_sigwinch (sigwinch_handler);
1349 if ((tty_baudrate () < 9600) || tty_is_slow ())
1350 verbose = 0;
1352 init_xterm_support ();
1353 init_mouse ();
1356 static void
1357 setup_dummy_mc (void)
1359 char d[MC_MAXPATHLEN];
1360 int ret;
1362 mc_get_current_wd (d, MC_MAXPATHLEN);
1363 setup_mc ();
1364 ret = mc_chdir (d);
1367 static void
1368 check_codeset ()
1370 const char *current_system_codepage = NULL;
1372 current_system_codepage = str_detect_termencoding ();
1374 #ifdef HAVE_CHARSET
1376 const char *_display_codepage;
1378 _display_codepage = get_codepage_id (display_codepage);
1380 if (!strcmp (_display_codepage, current_system_codepage))
1382 utf8_display = str_isutf8 (current_system_codepage);
1383 return;
1386 display_codepage = get_codepage_index (current_system_codepage);
1387 if (display_codepage == -1)
1389 display_codepage = 0;
1392 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
1394 #endif
1395 utf8_display = str_isutf8 (current_system_codepage);
1398 static void
1399 done_screen (void)
1401 if (!(quit & SUBSHELL_EXIT))
1402 clr_scr ();
1403 tty_reset_shell_mode ();
1404 tty_noraw_mode ();
1405 tty_keypad (FALSE);
1406 tty_colors_done ();
1409 static void
1410 done_mc (void)
1412 disable_mouse ();
1414 /* Setup shutdown
1416 * We sync the profiles since the hotlist may have changed, while
1417 * we only change the setup data if we have the auto save feature set
1420 if (auto_save_setup)
1421 save_setup (); /* does also call save_hotlist */
1422 else
1424 save_hotlist ();
1425 save_panel_types ();
1427 done_screen ();
1429 vfs_stamp_path (vfs_get_current_dir ());
1431 if ((current_panel != NULL) && (get_current_type () == view_listing))
1432 vfs_stamp_path (current_panel->cwd);
1434 if ((other_panel != NULL) && (get_other_type () == view_listing))
1435 vfs_stamp_path (other_panel->cwd);
1438 static cb_ret_t
1439 midnight_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1441 unsigned long command;
1443 switch (msg)
1445 case DLG_INIT:
1446 setup_panels ();
1447 return MSG_HANDLED;
1449 case DLG_DRAW:
1450 load_hint (1);
1451 /* We handle the special case of the output lines */
1452 if (console_flag && output_lines)
1453 show_console_contents (output_start_y,
1454 LINES - output_lines - keybar_visible -
1455 1, LINES - keybar_visible - 1);
1456 return MSG_HANDLED;
1458 case DLG_RESIZE:
1459 setup_panels ();
1460 menubar_arrange (the_menubar);
1461 return MSG_HANDLED;
1463 case DLG_IDLE:
1464 /* We only need the first idle event to show user menu after start */
1465 set_idle_proc (h, 0);
1467 if (boot_current_is_left)
1468 dlg_select_widget (get_panel_widget (0));
1469 else
1470 dlg_select_widget (get_panel_widget (1));
1472 if (auto_menu)
1473 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1474 return MSG_HANDLED;
1476 case DLG_KEY:
1477 if (ctl_x_map_enabled)
1479 ctl_x_map_enabled = FALSE;
1480 command = lookup_keymap_command (main_x_map, parm);
1481 if (command != CK_Ignore_Key)
1482 return midnight_execute_cmd (NULL, command);
1485 /* FIXME: should handle all menu shortcuts before this point */
1486 if (the_menubar->is_active)
1487 return MSG_NOT_HANDLED;
1489 if (parm == '\t')
1490 free_completions (cmdline);
1492 if (parm == '\n')
1494 size_t i;
1496 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1497 cmdline->buffer[i] == '\t'); i++)
1499 if (cmdline->buffer[i])
1501 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1502 return MSG_HANDLED;
1504 stuff (cmdline, "", 0);
1505 cmdline->point = 0;
1508 /* Ctrl-Enter and Alt-Enter */
1509 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1511 copy_prog_name ();
1512 return MSG_HANDLED;
1515 /* Ctrl-Shift-Enter */
1516 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1518 copy_current_pathname ();
1519 copy_prog_name ();
1520 return MSG_HANDLED;
1523 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1524 && !quote && !current_panel->searching)
1526 if (!only_leading_plus_minus)
1528 /* Special treatement, since the input line will eat them */
1529 if (parm == '+')
1531 select_cmd ();
1532 return MSG_HANDLED;
1535 if (parm == '\\' || parm == '-')
1537 unselect_cmd ();
1538 return MSG_HANDLED;
1541 if (parm == '*')
1543 reverse_selection_cmd ();
1544 return MSG_HANDLED;
1547 else if (!command_prompt || !cmdline->buffer[0])
1549 /* Special treatement '+', '-', '\', '*' only when this is
1550 * first char on input line
1553 if (parm == '+')
1555 select_cmd ();
1556 return MSG_HANDLED;
1559 if (parm == '\\' || parm == '-')
1561 unselect_cmd ();
1562 return MSG_HANDLED;
1565 if (parm == '*')
1567 reverse_selection_cmd ();
1568 return MSG_HANDLED;
1572 return MSG_NOT_HANDLED;
1574 case DLG_HOTKEY_HANDLED:
1575 if ((get_current_type () == view_listing) && current_panel->searching)
1577 current_panel->dirty = 1; /* FIXME: unneeded? */
1578 send_message ((Widget *) current_panel, WIDGET_COMMAND, CK_PanelStopSearch);
1580 return MSG_HANDLED;
1582 case DLG_UNHANDLED_KEY:
1583 if (command_prompt)
1585 cb_ret_t v;
1587 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1588 if (v == MSG_HANDLED)
1589 return MSG_HANDLED;
1592 if (ctl_x_map_enabled)
1594 ctl_x_map_enabled = FALSE;
1595 command = lookup_keymap_command (main_x_map, parm);
1597 else
1598 command = lookup_keymap_command (main_map, parm);
1600 return (command == CK_Ignore_Key) ? MSG_NOT_HANDLED : midnight_execute_cmd (NULL, command);
1602 case DLG_POST_KEY:
1603 if (!the_menubar->is_active)
1604 update_dirty_panels ();
1605 return MSG_HANDLED;
1607 case DLG_ACTION:
1608 /* shortcut */
1609 if (sender == NULL)
1610 midnight_execute_cmd (NULL, parm);
1611 /* message from menu */
1612 else if (sender == (Widget *) the_menubar)
1613 midnight_execute_cmd (sender, parm);
1614 /* message from buttonbar */
1615 else if (sender == (Widget *) the_bar)
1617 if (data == NULL)
1618 midnight_execute_cmd (sender, parm);
1619 else
1620 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1622 return MSG_HANDLED;
1624 default:
1625 return default_dlg_callback (h, sender, msg, parm, data);
1629 /* Show current directory in the xterm title */
1630 void
1631 update_xterm_title_path (void)
1633 /* TODO: share code with midnight_get_title () */
1635 const char *path;
1636 char host[BUF_TINY];
1637 char *p;
1638 struct passwd *pw = NULL;
1639 char *login = NULL;
1640 int res = 0;
1642 if (xterm_flag && xterm_title)
1644 path = strip_home_and_password (current_panel->cwd);
1645 res = gethostname (host, sizeof (host));
1646 if (res)
1647 { /* On success, res = 0 */
1648 host[0] = '\0';
1650 else
1652 host[sizeof (host) - 1] = '\0';
1654 pw = getpwuid (getuid ());
1655 if (pw)
1657 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1659 else
1661 login = g_strdup (host);
1663 p = g_strdup_printf ("mc [%s]:%s", login, path);
1664 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1665 g_free (login);
1666 g_free (p);
1667 if (!alternate_plus_minus)
1668 numeric_keypad_mode ();
1669 fflush (stdout);
1674 * Load new hint and display it.
1675 * IF force is not 0, ignore the timeout.
1677 void
1678 load_hint (int force)
1680 char *hint;
1682 if (!the_hint->widget.owner)
1683 return;
1685 if (!message_visible)
1687 label_set_text (the_hint, NULL);
1688 return;
1691 hint = get_random_hint (force);
1693 if (hint != NULL)
1695 if (*hint)
1696 set_hintbar (hint);
1697 g_free (hint);
1699 else
1701 char text[BUF_SMALL];
1703 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1704 set_hintbar (text);
1708 static void
1709 create_panels_and_run_mc (void)
1711 midnight_dlg->get_shortcut = midnight_get_shortcut;
1712 midnight_dlg->get_title = midnight_get_title;
1714 create_panels ();
1716 add_widget (midnight_dlg, the_menubar);
1717 init_menu ();
1719 add_widget (midnight_dlg, get_panel_widget (0));
1720 add_widget (midnight_dlg, get_panel_widget (1));
1722 add_widget (midnight_dlg, the_hint);
1723 add_widget (midnight_dlg, cmdline);
1724 add_widget (midnight_dlg, the_prompt);
1726 add_widget (midnight_dlg, the_bar);
1727 midnight_set_buttonbar (the_bar);
1729 /* Run the Midnight Commander if no file was specified in the command line */
1730 run_dlg (midnight_dlg);
1733 /* result must be free'd (I think this should go in util.c) */
1734 static char *
1735 prepend_cwd_on_local (const char *filename)
1737 char *d;
1738 size_t l;
1740 if (!vfs_file_is_local (filename) || g_path_is_absolute (filename))
1741 return g_strdup (filename);
1743 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1744 mc_get_current_wd (d, MC_MAXPATHLEN);
1745 l = strlen (d);
1746 d[l++] = PATH_SEP;
1747 strcpy (d + l, filename);
1748 canonicalize_pathname (d);
1749 return d;
1752 /* Invoke the internal view/edit routine with:
1753 * the default processing and forcing the internal viewer/editor
1755 static void
1756 mc_maybe_editor_or_viewer (void)
1758 switch (mc_run_mode)
1760 #ifdef USE_INTERNAL_EDIT
1761 case MC_RUN_EDITOR:
1762 edit_file (mc_run_param0, mc_args__edit_start_line);
1763 break;
1764 #endif /* USE_INTERNAL_EDIT */
1765 case MC_RUN_VIEWER:
1767 char *path;
1768 path = prepend_cwd_on_local (mc_run_param0);
1769 view_file (path, 0, 1);
1770 g_free (path);
1771 break;
1773 #ifdef USE_DIFF_VIEW
1774 case MC_RUN_DIFFVIEWER:
1775 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1776 break;
1777 #endif /* USE_DIFF_VIEW */
1778 default:
1779 break;
1781 midnight_shutdown = 1;
1784 /* Run the main dialog that occupies the whole screen */
1785 static void
1786 do_nc (void)
1788 dlg_colors_t midnight_colors;
1789 midnight_colors[DLG_COLOR_NORMAL] = mc_skin_color_get ("dialog", "_default_");
1790 midnight_colors[DLG_COLOR_FOCUS] = mc_skin_color_get ("dialog", "focus");
1791 midnight_colors[DLG_COLOR_HOT_NORMAL] = mc_skin_color_get ("dialog", "hotnormal");
1792 midnight_colors[DLG_COLOR_HOT_FOCUS] = mc_skin_color_get ("dialog", "hotfocus");
1793 midnight_colors[DLG_COLOR_TITLE] = mc_skin_color_get ("dialog", "title");
1795 panel_init ();
1797 midnight_dlg = create_dlg (FALSE, 0, 0, LINES, COLS, midnight_colors, midnight_callback,
1798 "[main]", NULL, DLG_WANT_IDLE);
1800 if (mc_run_mode == MC_RUN_FULL)
1801 setup_mc ();
1802 else
1803 setup_dummy_mc ();
1805 /* start check display_codepage and source_codepage */
1806 check_codeset ();
1808 /* Check if we were invoked as an editor or file viewer */
1809 if (mc_run_mode != MC_RUN_FULL)
1810 mc_maybe_editor_or_viewer ();
1811 else
1813 create_panels_and_run_mc ();
1815 /* Program end */
1816 midnight_shutdown = 1;
1818 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1819 if (mc_args__last_wd_file && vfs_current_is_local ())
1820 last_wd_string = g_strdup (current_panel->cwd);
1824 dialog_switch_shutdown ();
1825 done_mc ();
1826 destroy_dlg (midnight_dlg);
1827 panel_deinit ();
1828 current_panel = 0;
1829 done_setup ();
1832 /* POSIX version. The only version we support. */
1833 static void
1834 OS_Setup (void)
1836 const char *shell_env = getenv ("SHELL");
1837 const char *mc_libdir;
1839 if ((shell_env == NULL) || (shell_env[0] == '\0'))
1841 struct passwd *pwd;
1842 pwd = getpwuid (geteuid ());
1843 if (pwd != NULL)
1844 shell = g_strdup (pwd->pw_shell);
1846 else
1847 shell = g_strdup (shell_env);
1849 if ((shell == NULL) || (shell[0] == '\0'))
1851 g_free (shell);
1852 shell = g_strdup ("/bin/sh");
1855 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1856 /* and can be overriden by the MC_DATADIR environment variable */
1857 mc_libdir = getenv ("MC_DATADIR");
1858 if (mc_libdir != NULL)
1860 mc_home = g_strdup (mc_libdir);
1861 mc_home_alt = g_strdup (SYSCONFDIR);
1863 else
1865 mc_home = g_strdup (SYSCONFDIR);
1866 mc_home_alt = g_strdup (DATADIR);
1869 /* This variable is used by the subshell */
1870 home_dir = getenv ("HOME");
1872 if (!home_dir)
1873 home_dir = mc_home;
1876 static void
1877 sigchld_handler_no_subshell (int sig)
1879 #ifdef __linux__
1880 int pid, status;
1882 if (!console_flag)
1883 return;
1885 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1886 the value of console_flag never changed, we could simply not install
1887 this handler at all if (!console_flag && !use_subshell). */
1889 /* That comment is no longer true. We need to wait() on a sigchld
1890 handler (that's at least what the tarfs code expects currently). */
1892 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1894 if (pid == cons_saver_pid)
1897 if (WIFSTOPPED (status))
1899 /* Someone has stopped cons.saver - restart it */
1900 kill (pid, SIGCONT);
1902 else
1904 /* cons.saver has died - disable console saving */
1905 handle_console (CONSOLE_DONE);
1906 console_flag = 0;
1909 /* If we got here, some other child exited; ignore it */
1910 #endif /* __linux__ */
1912 (void) sig;
1915 static void
1916 init_sigchld (void)
1918 struct sigaction sigchld_action;
1920 sigchld_action.sa_handler =
1921 #ifdef HAVE_SUBSHELL_SUPPORT
1922 use_subshell ? sigchld_handler :
1923 #endif /* HAVE_SUBSHELL_SUPPORT */
1924 sigchld_handler_no_subshell;
1926 sigemptyset (&sigchld_action.sa_mask);
1928 #ifdef SA_RESTART
1929 sigchld_action.sa_flags = SA_RESTART;
1930 #else
1931 sigchld_action.sa_flags = 0;
1932 #endif /* !SA_RESTART */
1934 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
1936 #ifdef HAVE_SUBSHELL_SUPPORT
1938 * This may happen on QNX Neutrino 6, where SA_RESTART
1939 * is defined but not implemented. Fallback to no subshell.
1941 use_subshell = 0;
1942 #endif /* HAVE_SUBSHELL_SUPPORT */
1947 main (int argc, char *argv[])
1949 struct stat s;
1950 char *mc_dir;
1951 GError *error = NULL;
1952 gboolean isInitialized;
1954 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
1955 setlocale (LC_ALL, "");
1956 bindtextdomain ("mc", LOCALEDIR);
1957 textdomain ("mc");
1959 /* Set up temporary directory */
1960 mc_tmpdir ();
1962 OS_Setup ();
1964 str_init_strings (NULL);
1966 vfs_init ();
1968 #ifdef USE_INTERNAL_EDIT
1969 edit_stack_init ();
1970 #endif
1972 #ifdef HAVE_SLANG
1973 SLtt_Ignore_Beep = 1;
1974 #endif
1976 if (!mc_args_handle (argc, argv, "mc"))
1977 exit (EXIT_FAILURE);
1979 /* NOTE: This has to be called before tty_init or whatever routine
1980 calls any define_sequence */
1981 init_key ();
1983 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
1984 handle_console (CONSOLE_INIT);
1986 #ifdef HAVE_SUBSHELL_SUPPORT
1987 /* Don't use subshell when invoked as viewer or editor */
1988 if (mc_run_mode != MC_RUN_FULL)
1989 use_subshell = 0;
1991 if (use_subshell)
1992 subshell_get_console_attributes ();
1993 #endif /* HAVE_SUBSHELL_SUPPORT */
1995 /* Install the SIGCHLD handler; must be done before init_subshell() */
1996 init_sigchld ();
1998 /* We need this, since ncurses endwin () doesn't restore the signals */
1999 save_stop_handler ();
2001 /* Must be done before init_subshell, to set up the terminal size: */
2002 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2003 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2005 load_setup ();
2007 /* Removing this from the X code let's us type C-c */
2008 load_key_defs ();
2010 load_keymap_defs ();
2012 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2014 isInitialized = mc_skin_init (&error);
2016 mc_filehighlight = mc_fhl_new (TRUE);
2018 dlg_set_default_colors ();
2020 if (!isInitialized)
2022 message (D_ERROR, _("Warning"), "%s", error->message);
2023 g_error_free (error);
2024 error = NULL;
2027 /* create home directory */
2028 /* do it after the screen library initialization to show the error message */
2029 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2030 canonicalize_pathname (mc_dir);
2031 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
2032 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
2033 g_free (mc_dir);
2035 #ifdef HAVE_SUBSHELL_SUPPORT
2036 /* Done here to ensure that the subshell doesn't */
2037 /* inherit the file descriptors opened below, etc */
2038 if (use_subshell)
2039 init_subshell ();
2041 #endif /* HAVE_SUBSHELL_SUPPORT */
2043 /* Also done after init_subshell, to save any shell init file messages */
2044 if (console_flag)
2045 handle_console (CONSOLE_SAVE);
2047 if (alternate_plus_minus)
2048 application_keypad_mode ();
2050 #ifdef HAVE_SUBSHELL_SUPPORT
2051 if (use_subshell)
2053 mc_prompt = strip_ctrl_codes (subshell_prompt);
2054 if (mc_prompt == NULL)
2055 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2057 else
2058 #endif /* HAVE_SUBSHELL_SUPPORT */
2059 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2061 /* Program main loop */
2062 if (!midnight_shutdown)
2063 do_nc ();
2065 /* Save the tree store */
2066 tree_store_save ();
2068 free_keymap_defs ();
2070 /* Virtual File System shutdown */
2071 vfs_shut ();
2073 flush_extension_file (); /* does only free memory */
2075 mc_fhl_free (&mc_filehighlight);
2076 mc_skin_deinit ();
2078 tty_shutdown ();
2080 if (console_flag && !(quit & SUBSHELL_EXIT))
2081 handle_console (CONSOLE_RESTORE);
2082 if (alternate_plus_minus)
2083 numeric_keypad_mode ();
2085 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2087 if (console_flag)
2088 handle_console (CONSOLE_DONE);
2089 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2091 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string && !print_last_revert)
2093 int last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2094 S_IRUSR | S_IWUSR);
2096 if (last_wd_fd != -1)
2098 ssize_t ret1;
2099 int ret2;
2100 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2101 ret2 = close (last_wd_fd);
2104 g_free (last_wd_string);
2106 g_free (mc_home_alt);
2107 g_free (mc_home);
2108 g_free (shell);
2110 done_key ();
2111 #ifdef HAVE_CHARSET
2112 free_codepages_list ();
2113 g_free (autodetect_codeset);
2114 #endif
2115 g_free (clipboard_store_path);
2116 g_free (clipboard_paste_path);
2118 str_uninit_strings ();
2120 g_free (mc_run_param0);
2121 g_free (mc_run_param1);
2123 #ifdef USE_INTERNAL_EDIT
2124 edit_stack_free ();
2125 #endif
2127 return 0;