Renamed keybind-related functions:
[pantumic.git] / src / main.c
blob2ecd621d605baa586704b1bab4f70c4b156f6d0b
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 "lib/util.h"
60 #include "lib/widget.h"
62 #include "args.h"
63 #include "dir.h"
64 #include "panel.h"
65 #include "option.h"
66 #include "tree.h"
67 #include "treestore.h"
68 #include "consaver/cons.saver.h"
69 #include "subshell.h"
70 #include "setup.h" /* save_setup() */
71 #include "boxes.h" /* sort_box() */
72 #include "layout.h"
73 #include "cmd.h" /* Normal commands */
74 #include "hotlist.h"
75 #include "panelize.h"
76 #include "learn.h" /* learn_keys() */
77 #include "listmode.h"
78 #include "execute.h"
79 #include "ext.h" /* For flush_extension_file() */
80 #include "command.h"
81 #include "cmddef.h" /* CK_ cmd name const */
82 #include "user.h" /* user_file_menu_cmd() */
84 #include "chmod.h"
85 #include "chown.h"
86 #include "achown.h"
88 #include "main.h"
90 #ifdef USE_INTERNAL_EDIT
91 #include "src/editor/edit.h"
92 #endif
94 #ifdef USE_DIFF_VIEW
95 #include "src/diffviewer/ydiff.h"
96 #endif
98 #ifdef HAVE_CHARSET
99 #include "lib/charsets.h"
100 #include "selcodepage.h"
101 #endif /* HAVE_CHARSET */
103 #include "keybind.h" /* type global_keymap_t */
105 /*** global variables ****************************************************************************/
107 /* When the modes are active, left_panel, right_panel and tree_panel */
108 /* Point to a proper data structure. You should check with the functions */
109 /* get_current_type and get_other_type the types of the panels before using */
110 /* This pointer variables */
112 /* The structures for the panels */
113 WPanel *left_panel = NULL;
114 WPanel *right_panel = NULL;
116 mc_fhl_t *mc_filehighlight;
118 /* The pointer to the tree */
119 WTree *the_tree = NULL;
121 /* The Menubar */
122 struct WMenuBar *the_menubar = NULL;
123 static Menu *left_menu, *right_menu;
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 #ifndef HAVE_CHARSET
158 /* If true, allow characters in the range 160-255 */
159 int eight_bit_clean = 1;
161 * If true, also allow characters in the range 128-159.
162 * This is reported to break on many terminals (xterm, qansi-m).
164 int full_eight_bits = 0;
165 #endif /* !HAVE_CHARSET */
168 * If utf-8 terminal utf8_display = 1
169 * Display bits set UTF-8
172 int utf8_display = 0;
174 /* If true use the internal viewer */
175 int use_internal_view = 1;
177 /* The prompt */
178 const char *mc_prompt = NULL;
180 /* The widget where we draw the prompt */
181 WLabel *the_prompt;
183 /* The hint bar */
184 WLabel *the_hint;
186 /* The button bar */
187 WButtonBar *the_bar;
189 /* Mouse type: GPM, xterm or none */
190 Mouse_Type use_mouse_p = MOUSE_NONE;
192 /* If on, default for "No" in delete operations */
193 int safe_delete = 0;
195 /* Controls screen clearing before an exec */
196 int clear_before_exec = 1;
198 /* Asks for confirmation before deleting a file */
199 int confirm_delete = 1;
201 /* Asks for confirmation before deleting a hotlist entry */
202 int confirm_directory_hotlist_delete = 1;
204 /* Asks for confirmation before overwriting a file */
205 int confirm_overwrite = 1;
207 /* Asks for confirmation before executing a program by pressing enter */
208 int confirm_execute = 0;
210 /* Asks for confirmation before leaving the program */
211 int confirm_exit = 1;
213 /* Asks for confirmation before clean up of history */
214 int confirm_history_cleanup = 1;
216 /* Asks for confirmation when using F3 to view a directory and there
217 are tagged files */
218 int confirm_view_dir = 0;
220 /* This flag indicates if the pull down menus by default drop down */
221 int drop_menus = 0;
223 /* The dialog handle for the main program */
224 Dlg_head *midnight_dlg = NULL;
226 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
227 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
228 int update_prompt = 0;
230 /* The home directory */
231 const char *home_dir = NULL;
233 /* Only used at program boot */
234 int boot_current_is_left = 1;
236 /* If this is true, then when browsing the tree the other window will
237 * automatically reload it's directory with the contents of the currently
238 * selected directory.
240 int xtree_mode = 0;
242 /* path to X clipboard utility */
243 char *clipboard_store_path = NULL;
244 char *clipboard_paste_path = NULL;
246 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
247 char *mc_run_param0 = NULL;
248 char *mc_run_param1 = NULL;
250 /* Used so that widgets know if they are being destroyed or
251 shut down */
252 int midnight_shutdown = 0;
254 /* The user's shell */
255 char *shell = NULL;
257 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
258 char *mc_home = NULL;
260 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
261 char *mc_home_alt = NULL;
263 /*** file scope macro definitions ****************************************************************/
265 #ifdef HAVE_CHARSET
267 * Don't restrict the output on the screen manager level,
268 * the translation tables take care of it.
270 #define full_eight_bits (1)
271 #define eight_bit_clean (1)
272 #endif /* !HAVE_CHARSET */
274 /*** file scope type declarations ****************************************************************/
276 /*** file scope variables ************************************************************************/
278 /* If set, then print to the given file the last directory we were at */
279 static char *last_wd_string = NULL;
280 /* Set to 1 to suppress printing the last directory */
281 static int print_last_revert = 0;
283 static gboolean ctl_x_map_enabled = FALSE;
285 /*** file scope functions ************************************************************************/
286 /* --------------------------------------------------------------------------------------------- */
288 /** Stop MC main dialog and the current dialog if it exists.
289 * Needed to provide fast exit from MC viewer or editor on shell exit */
291 static void
292 stop_dialogs (void)
294 midnight_dlg->state = DLG_CLOSED;
296 if ((top_dlg != NULL) && (top_dlg->data != NULL))
297 ((Dlg_head *) top_dlg->data)->state = DLG_CLOSED;
300 /* --------------------------------------------------------------------------------------------- */
302 static gboolean
303 quit_cmd_internal (int quiet)
305 int q = quit;
306 size_t n = dialog_switch_num () - 1;
308 if (n != 0)
310 char msg[BUF_MEDIUM];
312 g_snprintf (msg, sizeof (msg),
313 ngettext ("You have %zd opened screen. Quit anyway?",
314 "You have %zd opened screens. Quit anyway?", n), n);
316 if (query_dialog (_("The Midnight Commander"), msg, D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
317 return FALSE;
318 q = 1;
320 else if (quiet || !confirm_exit)
321 q = 1;
322 else if (query_dialog (_("The Midnight Commander"),
323 _("Do you really want to quit the Midnight Commander?"),
324 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
325 q = 1;
327 if (q != 0)
329 #ifdef HAVE_SUBSHELL_SUPPORT
330 if (!use_subshell)
331 stop_dialogs ();
332 else if ((q = exit_subshell ()))
333 #endif
334 stop_dialogs ();
337 if (q != 0)
338 quit |= 1;
339 return (quit != 0);
342 /* --------------------------------------------------------------------------------------------- */
344 static gboolean
345 quit_cmd (void)
347 return quit_cmd_internal (0);
350 /* --------------------------------------------------------------------------------------------- */
352 static void
353 treebox_cmd (void)
355 char *sel_dir;
357 sel_dir = tree_box (selection (current_panel)->fname);
358 if (sel_dir)
360 do_cd (sel_dir, cd_exact);
361 g_free (sel_dir);
365 /* --------------------------------------------------------------------------------------------- */
367 #ifdef LISTMODE_EDITOR
368 static void
369 listmode_cmd (void)
371 char *newmode;
373 if (get_current_type () != view_listing)
374 return;
376 newmode = listmode_edit (current_panel->user_format);
377 if (!newmode)
378 return;
380 g_free (current_panel->user_format);
381 current_panel->list_type = list_user;
382 current_panel->user_format = newmode;
383 set_panel_formats (current_panel);
385 do_refresh ();
387 #endif /* LISTMODE_EDITOR */
389 /* --------------------------------------------------------------------------------------------- */
390 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
392 static GList *
393 create_panel_menu (void)
395 GList *entries = NULL;
397 entries = g_list_append (entries, menu_entry_create (_("File listin&g"), CK_ListingCmd));
398 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
399 entries = g_list_append (entries, menu_entry_create (_("&Info"), CK_InfoCmd));
400 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
401 entries = g_list_append (entries, menu_separator_create ());
402 entries =
403 g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ChangeListingCmd));
404 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
405 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
406 #ifdef HAVE_CHARSET
407 entries =
408 g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
409 #endif
410 #ifdef ENABLE_VFS_NET
411 entries = g_list_append (entries, menu_separator_create ());
412 #ifdef ENABLE_VFS_FTP
413 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
414 #endif
415 #ifdef ENABLE_VFS_FISH
416 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
417 #endif
418 #ifdef ENABLE_VFS_SMB
419 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
420 #endif
421 #endif /* ENABLE_VFS_NET */
422 entries = g_list_append (entries, menu_separator_create ());
423 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
425 return entries;
428 /* --------------------------------------------------------------------------------------------- */
430 static GList *
431 create_file_menu (void)
433 GList *entries = NULL;
435 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
436 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
437 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
438 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
439 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
440 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
441 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
442 entries = g_list_append (entries, menu_entry_create (_("&Symlink"), CK_SymlinkCmd));
443 entries =
444 g_list_append (entries, menu_entry_create (_("Relative symlin&k"), CK_RelativeSymlinkCmd));
445 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
446 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
447 entries =
448 g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
449 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
450 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
451 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
452 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
453 entries = g_list_append (entries, menu_separator_create ());
454 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
455 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
456 entries =
457 g_list_append (entries,
458 menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
459 entries = g_list_append (entries, menu_separator_create ());
460 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
462 return entries;
465 /* --------------------------------------------------------------------------------------------- */
467 static GList *
468 create_command_menu (void)
470 /* I know, I'm lazy, but the tree widget when it's not running
471 * as a panel still has some problems, I have not yet finished
472 * the WTree widget port, sorry.
474 GList *entries = NULL;
476 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
477 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
478 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
479 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
480 entries =
481 g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
482 entries =
483 g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
484 #ifdef USE_DIFF_VIEW
485 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
486 #endif
487 entries =
488 g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
489 entries =
490 g_list_append (entries,
491 menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
492 entries = g_list_append (entries, menu_separator_create ());
493 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
494 entries =
495 g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
496 #ifdef ENABLE_VFS
497 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
498 #endif
499 #ifdef WITH_BACKGROUND
500 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
501 #endif
502 entries = g_list_append (entries, menu_entry_create (_("Screen lis&t"), CK_DialogListCmd));
503 entries = g_list_append (entries, menu_separator_create ());
504 #ifdef ENABLE_VFS_UNDELFS
505 entries =
506 g_list_append (entries,
507 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
508 #endif
509 #ifdef LISTMODE_EDITOR
510 entries =
511 g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
512 #endif
513 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
514 entries = g_list_append (entries, menu_separator_create ());
515 #endif
516 entries =
517 g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
518 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
519 entries =
520 g_list_append (entries,
521 menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
523 return entries;
526 /* --------------------------------------------------------------------------------------------- */
528 static GList *
529 create_options_menu (void)
531 GList *entries = NULL;
533 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
534 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutBox));
535 entries =
536 g_list_append (entries, menu_entry_create (_("&Panel options..."), CK_PanelOptionsBox));
537 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
538 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
539 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
540 #ifdef ENABLE_VFS
541 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
542 #endif
543 entries = g_list_append (entries, menu_separator_create ());
544 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
546 return entries;
549 /* --------------------------------------------------------------------------------------------- */
551 static void
552 init_menu (void)
554 left_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
555 menubar_add_menu (the_menubar, left_menu);
556 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
557 menubar_add_menu (the_menubar,
558 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
559 menubar_add_menu (the_menubar,
560 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
561 right_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
562 menubar_add_menu (the_menubar, right_menu);
563 update_menu ();
564 menubar_set_visible (the_menubar, menubar_visible);
567 /* --------------------------------------------------------------------------------------------- */
569 static void
570 menu_last_selected_cmd (void)
572 the_menubar->is_active = TRUE;
573 the_menubar->is_dropped = (drop_menus != 0);
574 the_menubar->previous_widget = dlg_get_current_widget_id (midnight_dlg);
575 dlg_select_widget (the_menubar);
578 /* --------------------------------------------------------------------------------------------- */
580 static void
581 menu_cmd (void)
583 if (the_menubar->is_active)
584 return;
586 if ((get_current_index () == 0) == (current_panel->active != 0))
587 the_menubar->selected = 0;
588 else
589 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
590 menu_last_selected_cmd ();
593 /* --------------------------------------------------------------------------------------------- */
595 static char *
596 midnight_get_shortcut (unsigned long command)
598 const char *ext_map;
599 const char *shortcut = NULL;
601 shortcut = keybind_lookup_keymap_shortcut (main_map, command);
602 if (shortcut != NULL)
603 return g_strdup (shortcut);
605 shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
606 if (shortcut != NULL)
607 return g_strdup (shortcut);
609 ext_map = keybind_lookup_keymap_shortcut (main_map, CK_StartExtMap1);
610 if (ext_map != NULL)
611 shortcut = keybind_lookup_keymap_shortcut (main_x_map, command);
612 if (shortcut != NULL)
613 return g_strdup_printf ("%s %s", ext_map, shortcut);
615 return NULL;
618 /* --------------------------------------------------------------------------------------------- */
620 static char *
621 midnight_get_title (const Dlg_head * h, size_t len)
623 /* TODO: share code with update_xterm_title_path() */
625 const char *path;
626 char host[BUF_TINY];
627 char *p;
628 struct passwd *pw = NULL;
629 char *login = NULL;
630 int res = 0;
632 (void) h;
634 path = strip_home_and_password (current_panel->cwd);
635 res = gethostname (host, sizeof (host));
636 if (res != 0)
637 host[0] = '\0';
638 else
639 host[sizeof (host) - 1] = '\0';
641 pw = getpwuid (getuid ());
642 if (pw != NULL)
643 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
644 else
645 login = g_strdup (host);
647 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
648 path = str_trunc (p, len - 4);
649 g_free (login);
650 g_free (p);
652 return g_strdup (path);
655 /* --------------------------------------------------------------------------------------------- */
657 static void
658 toggle_panels_split (void)
660 horizontal_split = !horizontal_split;
661 layout_change ();
662 do_refresh ();
665 /* --------------------------------------------------------------------------------------------- */
667 * Just a hack for allowing url-like pathnames to be accepted from the
668 * command line.
671 static void
672 translated_mc_chdir (char *dir)
674 char *newdir;
675 int ret;
677 newdir = vfs_translate_url (dir);
678 ret = mc_chdir (newdir);
679 g_free (newdir);
682 /* --------------------------------------------------------------------------------------------- */
684 static void
685 create_panels (void)
687 int current_index;
688 int other_index;
689 panel_view_mode_t current_mode, other_mode;
690 char original_dir[BUF_1K] = "\0";
692 if (boot_current_is_left)
694 current_index = 0;
695 other_index = 1;
696 current_mode = startup_left_mode;
697 other_mode = startup_right_mode;
699 else
701 current_index = 1;
702 other_index = 0;
703 current_mode = startup_right_mode;
704 other_mode = startup_left_mode;
706 /* Creates the left panel */
707 if (mc_run_param0 != NULL)
709 if (mc_run_param1 != NULL)
711 /* Ok, user has specified two dirs, save the original one,
712 * since we may not be able to chdir to the proper
713 * second directory later
715 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
717 translated_mc_chdir (mc_run_param0);
719 set_display_type (current_index, current_mode);
721 /* The other panel */
722 if (mc_run_param1 != NULL)
724 if (original_dir[0] != '\0')
725 translated_mc_chdir (original_dir);
726 translated_mc_chdir (mc_run_param1);
728 set_display_type (other_index, other_mode);
730 if (startup_left_mode == view_listing)
732 current_panel = left_panel;
734 else
736 if (right_panel)
737 current_panel = right_panel;
738 else
739 current_panel = left_panel;
742 /* Create the nice widgets */
743 cmdline = command_new (0, 0, 0);
744 the_prompt = label_new (0, 0, mc_prompt);
745 the_prompt->transparent = 1;
746 the_bar = buttonbar_new (keybar_visible);
748 the_hint = label_new (0, 0, 0);
749 the_hint->transparent = 1;
750 the_hint->auto_adjust_cols = 0;
751 the_hint->widget.cols = COLS;
753 the_menubar = menubar_new (0, 0, COLS, NULL);
756 /* --------------------------------------------------------------------------------------------- */
758 static void
759 copy_current_pathname (void)
761 char *cwd_path;
762 if (!command_prompt)
763 return;
765 cwd_path = remove_encoding_from_path (current_panel->cwd);
766 command_insert (cmdline, cwd_path, FALSE);
768 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
769 command_insert (cmdline, PATH_SEP_STR, FALSE);
770 g_free (cwd_path);
773 /* --------------------------------------------------------------------------------------------- */
775 static void
776 copy_other_pathname (void)
778 char *cwd_path;
780 if (get_other_type () != view_listing)
781 return;
783 if (!command_prompt)
784 return;
786 cwd_path = remove_encoding_from_path (other_panel->cwd);
787 command_insert (cmdline, cwd_path, FALSE);
789 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
790 command_insert (cmdline, PATH_SEP_STR, FALSE);
791 g_free (cwd_path);
794 /* --------------------------------------------------------------------------------------------- */
796 static void
797 copy_readlink (WPanel * panel)
799 if (!command_prompt)
800 return;
801 if (S_ISLNK (selection (panel)->st.st_mode))
803 char buffer[MC_MAXPATHLEN];
804 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
805 int i;
807 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
808 g_free (p);
809 if (i > 0)
811 buffer[i] = 0;
812 command_insert (cmdline, buffer, TRUE);
817 /* --------------------------------------------------------------------------------------------- */
819 static void
820 copy_current_readlink (void)
822 copy_readlink (current_panel);
825 /* --------------------------------------------------------------------------------------------- */
827 static void
828 copy_other_readlink (void)
830 if (get_other_type () == view_listing)
831 copy_readlink (other_panel);
834 /* --------------------------------------------------------------------------------------------- */
835 /** Insert the selected file name into the input line */
837 static void
838 copy_prog_name (void)
840 char *tmp;
841 if (!command_prompt)
842 return;
844 if (get_current_type () == view_tree)
846 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
847 tmp = tree_selected_name (tree);
849 else
850 tmp = selection (current_panel)->fname;
852 command_insert (cmdline, tmp, TRUE);
855 /* --------------------------------------------------------------------------------------------- */
857 static void
858 copy_tagged (WPanel * panel)
860 int i;
862 if (!command_prompt)
863 return;
864 input_disable_update (cmdline);
865 if (panel->marked)
867 for (i = 0; i < panel->count; i++)
869 if (panel->dir.list[i].f.marked)
870 command_insert (cmdline, panel->dir.list[i].fname, TRUE);
873 else
875 command_insert (cmdline, panel->dir.list[panel->selected].fname, TRUE);
877 input_enable_update (cmdline);
880 /* --------------------------------------------------------------------------------------------- */
882 static void
883 copy_current_tagged (void)
885 copy_tagged (current_panel);
888 /* --------------------------------------------------------------------------------------------- */
890 static void
891 copy_other_tagged (void)
893 if (get_other_type () == view_listing)
894 copy_tagged (other_panel);
897 /* --------------------------------------------------------------------------------------------- */
899 static void
900 ctl_x_cmd (void)
902 ctl_x_map_enabled = TRUE;
905 /* --------------------------------------------------------------------------------------------- */
907 static cb_ret_t
908 midnight_execute_cmd (Widget * sender, unsigned long command)
910 cb_ret_t res = MSG_HANDLED;
912 (void) sender;
914 /* stop quick search before executing any command */
915 send_message ((Widget *) current_panel, WIDGET_COMMAND, CK_PanelStopSearch);
917 switch (command)
919 case CK_AddHotlist:
920 add2hotlist_cmd ();
921 break;
922 case CK_ChangeListingCmd:
923 change_listing_cmd ();
924 break;
925 case CK_ChmodCmd:
926 chmod_cmd ();
927 break;
928 case CK_ChownCmd:
929 chown_cmd ();
930 break;
931 case CK_ChownAdvancedCmd:
932 chown_advanced_cmd ();
933 break;
934 case CK_CompareDirsCmd:
935 compare_dirs_cmd ();
936 break;
937 case CK_ConfigureBox:
938 configure_box ();
939 break;
940 #ifdef ENABLE_VFS
941 case CK_ConfigureVfs:
942 configure_vfs ();
943 break;
944 #endif
945 case CK_ConfirmBox:
946 confirm_box ();
947 break;
948 case CK_CopyCmd:
949 copy_cmd ();
950 break;
951 case CK_CopyCurrentPathname:
952 copy_current_pathname ();
953 break;
954 case CK_CopyCurrentReadlink:
955 copy_current_readlink ();
956 break;
957 case CK_CopyCurrentTagged:
958 copy_current_tagged ();
959 break;
960 case CK_CopyOtherPathname:
961 copy_other_pathname ();
962 break;
963 case CK_CopyOtherReadlink:
964 copy_other_readlink ();
965 break;
966 case CK_CopyOtherTagged:
967 copy_other_tagged ();
968 break;
969 case CK_DeleteCmd:
970 delete_cmd ();
971 break;
972 case CK_DialogListCmd:
973 dialog_switch_list ();
974 break;
975 #ifdef USE_DIFF_VIEW
976 case CK_DiffViewCmd:
977 diff_view_cmd ();
978 break;
979 #endif
980 case CK_DisplayBitsBox:
981 display_bits_box ();
982 break;
983 case CK_EditCmd:
984 edit_cmd ();
985 break;
986 #ifdef USE_INTERNAL_EDIT
987 case CK_EditForceInternalCmd:
988 edit_cmd_force_internal ();
989 break;
990 #endif
991 case CK_EditExtFileCmd:
992 ext_cmd ();
993 break;
994 case CK_EditFhlFileCmd:
995 edit_fhl_cmd ();
996 break;
997 case CK_EditMcMenuCmd:
998 edit_mc_menu_cmd ();
999 break;
1000 case CK_EditSymlinkCmd:
1001 edit_symlink_cmd ();
1002 break;
1003 case CK_ExternalPanelize:
1004 external_panelize ();
1005 break;
1006 case CK_FilterCmd:
1007 filter_cmd ();
1008 break;
1009 case CK_FilteredViewCmd:
1010 filtered_view_cmd ();
1011 break;
1012 case CK_FindCmd:
1013 find_cmd ();
1014 break;
1015 #ifdef ENABLE_VFS_FISH
1016 case CK_FishlinkCmd:
1017 fishlink_cmd ();
1018 break;
1019 #endif
1020 #ifdef ENABLE_VFS_FTP
1021 case CK_FtplinkCmd:
1022 ftplink_cmd ();
1023 break;
1024 #endif
1025 case CK_HelpCmd:
1026 help_cmd ();
1027 break;
1028 case CK_HistoryCmd:
1029 history_cmd ();
1030 break;
1031 case CK_InfoCmd:
1032 if (sender == (Widget *) the_menubar)
1033 info_cmd (); /* mwnu */
1034 else
1035 info_cmd_no_menu (); /* shortcut or buttonbar */
1036 break;
1037 #ifdef WITH_BACKGROUND
1038 case CK_JobsCmd:
1039 jobs_cmd ();
1040 break;
1041 #endif
1042 case CK_LayoutBox:
1043 layout_box ();
1044 break;
1045 case CK_LearnKeys:
1046 learn_keys ();
1047 break;
1048 case CK_LinkCmd:
1049 link_cmd (LINK_HARDLINK);
1050 break;
1051 case CK_ListingCmd:
1052 listing_cmd ();
1053 break;
1054 #ifdef LISTMODE_EDITOR
1055 case CK_ListmodeCmd:
1056 listmode_cmd ();
1057 break;
1058 #endif
1059 case CK_MenuCmd:
1060 menu_cmd ();
1061 break;
1062 case CK_MenuLastSelectedCmd:
1063 menu_last_selected_cmd ();
1064 break;
1065 case CK_MkdirCmd:
1066 mkdir_cmd ();
1067 break;
1068 case CK_PanelOptionsBox:
1069 panel_options_box ();
1070 break;
1071 #ifdef HAVE_CHARSET
1072 case CK_PanelSetPanelEncoding:
1073 encoding_cmd ();
1074 break;
1075 #endif
1076 case CK_QuickCdCmd:
1077 quick_cd_cmd ();
1078 break;
1079 case CK_QuickChdirCmd:
1080 quick_chdir_cmd ();
1081 break;
1082 case CK_QuickViewCmd:
1083 if (sender == (Widget *) the_menubar)
1084 quick_view_cmd (); /* menu */
1085 else
1086 quick_cmd_no_menu (); /* shortcut or buttonabr */
1087 break;
1088 case CK_QuietQuitCmd:
1089 quiet_quit_cmd ();
1090 break;
1091 case CK_QuitCmd:
1092 quit_cmd ();
1093 break;
1094 case CK_RelativeSymlinkCmd:
1095 link_cmd (LINK_SYMLINK_RELATIVE);
1096 break;
1097 case CK_RenameCmd:
1098 rename_cmd ();
1099 break;
1100 case CK_RereadCmd:
1101 reread_cmd ();
1102 break;
1103 #ifdef ENABLE_VFS
1104 case CK_ReselectVfs:
1105 reselect_vfs ();
1106 break;
1107 #endif
1108 case CK_ReverseSelectionCmd:
1109 reverse_selection_cmd ();
1110 break;
1111 case CK_SaveSetupCmd:
1112 save_setup_cmd ();
1113 break;
1114 case CK_SelectCmd:
1115 select_cmd ();
1116 break;
1117 case CK_ShowCommandLine:
1118 view_other_cmd ();
1119 break;
1120 case CK_SingleDirsizeCmd:
1121 smart_dirsize_cmd ();
1122 break;
1123 #ifdef ENABLE_VFS_SMB
1124 case CK_SmblinkCmd:
1125 smblink_cmd ();
1126 break;
1127 #endif /* ENABLE_VFS_SMB */
1128 case CK_Sort:
1129 sort_cmd ();
1130 break;
1131 case CK_StartExtMap1:
1132 ctl_x_cmd ();
1133 break;
1134 case CK_SuspendCmd:
1135 suspend_cmd ();
1136 break;
1137 case CK_SwapCmd:
1138 swap_cmd ();
1139 break;
1140 case CK_SymlinkCmd:
1141 link_cmd (LINK_SYMLINK_ABSOLUTE);
1142 break;
1143 case CK_ToggleListingCmd:
1144 toggle_listing_cmd ();
1145 break;
1146 case CK_ToggleShowHidden:
1147 toggle_show_hidden ();
1148 break;
1149 case CK_TogglePanelsSplit:
1150 toggle_panels_split ();
1151 break;
1152 case CK_TreeCmd:
1153 tree_cmd ();
1154 break;
1155 case CK_TreeBoxCmd:
1156 treebox_cmd ();
1157 break;
1158 #ifdef ENABLE_VFS_UNDELFS
1159 case CK_UndeleteCmd:
1160 undelete_cmd ();
1161 break;
1162 #endif
1163 case CK_UnselectCmd:
1164 unselect_cmd ();
1165 break;
1166 case CK_UserMenuCmd:
1167 user_file_menu_cmd ();
1168 break;
1169 case CK_ViewCmd:
1170 view_cmd ();
1171 break;
1172 case CK_ViewFileCmd:
1173 view_file_cmd ();
1174 break;
1175 default:
1176 res = MSG_NOT_HANDLED;
1179 return res;
1182 /* --------------------------------------------------------------------------------------------- */
1184 static void
1185 init_xterm_support (void)
1187 const char *termvalue;
1189 termvalue = getenv ("TERM");
1190 if (!termvalue || !(*termvalue))
1192 fputs (_("The TERM environment variable is unset!\n"), stderr);
1193 exit (EXIT_FAILURE);
1196 /* Check mouse capabilities */
1197 xmouse_seq = tty_tgetstr ("Km");
1199 if (strcmp (termvalue, "cygwin") == 0)
1201 mc_args__force_xterm = 1;
1202 use_mouse_p = MOUSE_DISABLED;
1205 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1206 || strncmp (termvalue, "konsole", 7) == 0
1207 || strncmp (termvalue, "rxvt", 4) == 0
1208 || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0)
1210 xterm_flag = 1;
1212 /* Default to the standard xterm sequence */
1213 if (!xmouse_seq)
1215 xmouse_seq = ESC_STR "[M";
1218 /* Enable mouse unless explicitly disabled by --nomouse */
1219 if (use_mouse_p != MOUSE_DISABLED)
1221 const char *color_term = getenv ("COLORTERM");
1222 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1223 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1224 strcmp (termvalue, "Eterm") == 0)
1226 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1228 else
1230 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1236 /* --------------------------------------------------------------------------------------------- */
1238 static void
1239 setup_mc (void)
1241 #ifdef HAVE_SLANG
1242 tty_display_8bit (full_eight_bits != 0);
1243 #else
1244 tty_display_8bit (eight_bit_clean != 0);
1245 #endif
1247 #ifdef HAVE_SUBSHELL_SUPPORT
1248 if (use_subshell)
1249 add_select_channel (subshell_pty, load_prompt, 0);
1250 #endif /* !HAVE_SUBSHELL_SUPPORT */
1252 tty_setup_sigwinch (sigwinch_handler);
1254 if ((tty_baudrate () < 9600) || tty_is_slow ())
1255 verbose = 0;
1257 init_xterm_support ();
1258 init_mouse ();
1261 /* --------------------------------------------------------------------------------------------- */
1263 static void
1264 setup_dummy_mc (void)
1266 char d[MC_MAXPATHLEN];
1267 int ret;
1269 mc_get_current_wd (d, MC_MAXPATHLEN);
1270 setup_mc ();
1271 ret = mc_chdir (d);
1274 /* --------------------------------------------------------------------------------------------- */
1276 static void
1277 check_codeset ()
1279 const char *current_system_codepage = NULL;
1281 current_system_codepage = str_detect_termencoding ();
1283 #ifdef HAVE_CHARSET
1285 const char *_display_codepage;
1287 _display_codepage = get_codepage_id (display_codepage);
1289 if (!strcmp (_display_codepage, current_system_codepage))
1291 utf8_display = str_isutf8 (current_system_codepage);
1292 return;
1295 display_codepage = get_codepage_index (current_system_codepage);
1296 if (display_codepage == -1)
1298 display_codepage = 0;
1301 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
1303 #endif
1304 utf8_display = str_isutf8 (current_system_codepage);
1307 /* --------------------------------------------------------------------------------------------- */
1309 static void
1310 done_screen (void)
1312 if (!(quit & SUBSHELL_EXIT))
1313 clr_scr ();
1314 tty_reset_shell_mode ();
1315 tty_noraw_mode ();
1316 tty_keypad (FALSE);
1317 tty_colors_done ();
1320 /* --------------------------------------------------------------------------------------------- */
1322 static void
1323 done_mc (void)
1325 disable_mouse ();
1327 /* Setup shutdown
1329 * We sync the profiles since the hotlist may have changed, while
1330 * we only change the setup data if we have the auto save feature set
1333 if (auto_save_setup)
1334 save_setup (); /* does also call save_hotlist */
1335 else
1337 save_hotlist ();
1338 save_panel_types ();
1340 done_screen ();
1342 vfs_stamp_path (vfs_get_current_dir ());
1344 if ((current_panel != NULL) && (get_current_type () == view_listing))
1345 vfs_stamp_path (current_panel->cwd);
1347 if ((other_panel != NULL) && (get_other_type () == view_listing))
1348 vfs_stamp_path (other_panel->cwd);
1351 /* --------------------------------------------------------------------------------------------- */
1353 static cb_ret_t
1354 midnight_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1356 unsigned long command;
1358 switch (msg)
1360 case DLG_INIT:
1361 setup_panels ();
1362 return MSG_HANDLED;
1364 case DLG_DRAW:
1365 load_hint (1);
1366 /* We handle the special case of the output lines */
1367 if (console_flag && output_lines)
1368 show_console_contents (output_start_y,
1369 LINES - output_lines - keybar_visible -
1370 1, LINES - keybar_visible - 1);
1371 return MSG_HANDLED;
1373 case DLG_RESIZE:
1374 setup_panels ();
1375 menubar_arrange (the_menubar);
1376 return MSG_HANDLED;
1378 case DLG_IDLE:
1379 /* We only need the first idle event to show user menu after start */
1380 set_idle_proc (h, 0);
1382 if (boot_current_is_left)
1383 dlg_select_widget (get_panel_widget (0));
1384 else
1385 dlg_select_widget (get_panel_widget (1));
1387 if (auto_menu)
1388 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1389 return MSG_HANDLED;
1391 case DLG_KEY:
1392 if (ctl_x_map_enabled)
1394 ctl_x_map_enabled = FALSE;
1395 command = keybind_lookup_keymap_command (main_x_map, parm);
1396 if (command != CK_Ignore_Key)
1397 return midnight_execute_cmd (NULL, command);
1400 /* FIXME: should handle all menu shortcuts before this point */
1401 if (the_menubar->is_active)
1402 return MSG_NOT_HANDLED;
1404 if (parm == '\t')
1405 input_free_completions (cmdline);
1407 if (parm == '\n')
1409 size_t i;
1411 for (i = 0; cmdline->buffer[i] != '\0' &&
1412 (cmdline->buffer[i] == ' ' || cmdline->buffer[i] == '\t'); i++)
1415 if (cmdline->buffer[i] != '\0')
1417 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1418 return MSG_HANDLED;
1421 input_insert (cmdline, "", FALSE);
1422 cmdline->point = 0;
1425 /* Ctrl-Enter and Alt-Enter */
1426 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1428 copy_prog_name ();
1429 return MSG_HANDLED;
1432 /* Ctrl-Shift-Enter */
1433 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1435 copy_current_pathname ();
1436 copy_prog_name ();
1437 return MSG_HANDLED;
1440 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1441 && !quote && !current_panel->searching)
1443 if (!only_leading_plus_minus)
1445 /* Special treatement, since the input line will eat them */
1446 if (parm == '+')
1448 select_cmd ();
1449 return MSG_HANDLED;
1452 if (parm == '\\' || parm == '-')
1454 unselect_cmd ();
1455 return MSG_HANDLED;
1458 if (parm == '*')
1460 reverse_selection_cmd ();
1461 return MSG_HANDLED;
1464 else if (!command_prompt || !cmdline->buffer[0])
1466 /* Special treatement '+', '-', '\', '*' only when this is
1467 * first char on input line
1470 if (parm == '+')
1472 select_cmd ();
1473 return MSG_HANDLED;
1476 if (parm == '\\' || parm == '-')
1478 unselect_cmd ();
1479 return MSG_HANDLED;
1482 if (parm == '*')
1484 reverse_selection_cmd ();
1485 return MSG_HANDLED;
1489 return MSG_NOT_HANDLED;
1491 case DLG_HOTKEY_HANDLED:
1492 if ((get_current_type () == view_listing) && current_panel->searching)
1494 current_panel->dirty = 1; /* FIXME: unneeded? */
1495 send_message ((Widget *) current_panel, WIDGET_COMMAND, CK_PanelStopSearch);
1497 return MSG_HANDLED;
1499 case DLG_UNHANDLED_KEY:
1500 if (command_prompt)
1502 cb_ret_t v;
1504 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1505 if (v == MSG_HANDLED)
1506 return MSG_HANDLED;
1509 if (ctl_x_map_enabled)
1511 ctl_x_map_enabled = FALSE;
1512 command = keybind_lookup_keymap_command (main_x_map, parm);
1514 else
1515 command = keybind_lookup_keymap_command (main_map, parm);
1517 return (command == CK_Ignore_Key) ? MSG_NOT_HANDLED : midnight_execute_cmd (NULL, command);
1519 case DLG_POST_KEY:
1520 if (!the_menubar->is_active)
1521 update_dirty_panels ();
1522 return MSG_HANDLED;
1524 case DLG_ACTION:
1525 /* shortcut */
1526 if (sender == NULL)
1527 midnight_execute_cmd (NULL, parm);
1528 /* message from menu */
1529 else if (sender == (Widget *) the_menubar)
1530 midnight_execute_cmd (sender, parm);
1531 /* message from buttonbar */
1532 else if (sender == (Widget *) the_bar)
1534 if (data == NULL)
1535 midnight_execute_cmd (sender, parm);
1536 else
1537 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1539 return MSG_HANDLED;
1541 default:
1542 return default_dlg_callback (h, sender, msg, parm, data);
1546 /* --------------------------------------------------------------------------------------------- */
1548 static void
1549 create_panels_and_run_mc (void)
1551 midnight_dlg->get_shortcut = midnight_get_shortcut;
1552 midnight_dlg->get_title = midnight_get_title;
1554 create_panels ();
1556 add_widget (midnight_dlg, the_menubar);
1557 init_menu ();
1559 add_widget (midnight_dlg, get_panel_widget (0));
1560 add_widget (midnight_dlg, get_panel_widget (1));
1562 add_widget (midnight_dlg, the_hint);
1563 add_widget (midnight_dlg, cmdline);
1564 add_widget (midnight_dlg, the_prompt);
1566 add_widget (midnight_dlg, the_bar);
1567 midnight_set_buttonbar (the_bar);
1569 /* Run the Midnight Commander if no file was specified in the command line */
1570 run_dlg (midnight_dlg);
1573 /* --------------------------------------------------------------------------------------------- */
1574 /** result must be free'd (I think this should go in util.c) */
1576 static char *
1577 prepend_cwd_on_local (const char *filename)
1579 char *d;
1580 size_t l;
1582 if (!vfs_file_is_local (filename) || g_path_is_absolute (filename))
1583 return g_strdup (filename);
1585 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1586 mc_get_current_wd (d, MC_MAXPATHLEN);
1587 l = strlen (d);
1588 d[l++] = PATH_SEP;
1589 strcpy (d + l, filename);
1590 canonicalize_pathname (d);
1591 return d;
1594 /* --------------------------------------------------------------------------------------------- */
1595 /** Invoke the internal view/edit routine with:
1596 * the default processing and forcing the internal viewer/editor
1599 static void
1600 mc_maybe_editor_or_viewer (void)
1602 switch (mc_run_mode)
1604 #ifdef USE_INTERNAL_EDIT
1605 case MC_RUN_EDITOR:
1606 edit_file (mc_run_param0, mc_args__edit_start_line);
1607 break;
1608 #endif /* USE_INTERNAL_EDIT */
1609 case MC_RUN_VIEWER:
1611 char *path;
1612 path = prepend_cwd_on_local (mc_run_param0);
1613 view_file (path, 0, 1);
1614 g_free (path);
1615 break;
1617 #ifdef USE_DIFF_VIEW
1618 case MC_RUN_DIFFVIEWER:
1619 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1620 break;
1621 #endif /* USE_DIFF_VIEW */
1622 default:
1623 break;
1625 midnight_shutdown = 1;
1628 /* --------------------------------------------------------------------------------------------- */
1629 /** Run the main dialog that occupies the whole screen */
1631 static void
1632 do_nc (void)
1634 dlg_colors_t midnight_colors;
1635 midnight_colors[DLG_COLOR_NORMAL] = mc_skin_color_get ("dialog", "_default_");
1636 midnight_colors[DLG_COLOR_FOCUS] = mc_skin_color_get ("dialog", "focus");
1637 midnight_colors[DLG_COLOR_HOT_NORMAL] = mc_skin_color_get ("dialog", "hotnormal");
1638 midnight_colors[DLG_COLOR_HOT_FOCUS] = mc_skin_color_get ("dialog", "hotfocus");
1639 midnight_colors[DLG_COLOR_TITLE] = mc_skin_color_get ("dialog", "title");
1641 panel_init ();
1643 midnight_dlg = create_dlg (FALSE, 0, 0, LINES, COLS, midnight_colors, midnight_callback,
1644 "[main]", NULL, DLG_WANT_IDLE);
1646 if (mc_run_mode == MC_RUN_FULL)
1647 setup_mc ();
1648 else
1649 setup_dummy_mc ();
1651 /* start check display_codepage and source_codepage */
1652 check_codeset ();
1654 /* Check if we were invoked as an editor or file viewer */
1655 if (mc_run_mode != MC_RUN_FULL)
1656 mc_maybe_editor_or_viewer ();
1657 else
1659 create_panels_and_run_mc ();
1661 /* Program end */
1662 midnight_shutdown = 1;
1664 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1665 if (mc_args__last_wd_file && vfs_current_is_local ())
1666 last_wd_string = g_strdup (current_panel->cwd);
1670 dialog_switch_shutdown ();
1671 done_mc ();
1672 destroy_dlg (midnight_dlg);
1673 panel_deinit ();
1674 current_panel = 0;
1675 done_setup ();
1678 /* --------------------------------------------------------------------------------------------- */
1679 /** POSIX version. The only version we support. */
1681 static void
1682 OS_Setup (void)
1684 const char *shell_env = getenv ("SHELL");
1685 const char *mc_libdir;
1687 if ((shell_env == NULL) || (shell_env[0] == '\0'))
1689 struct passwd *pwd;
1690 pwd = getpwuid (geteuid ());
1691 if (pwd != NULL)
1692 shell = g_strdup (pwd->pw_shell);
1694 else
1695 shell = g_strdup (shell_env);
1697 if ((shell == NULL) || (shell[0] == '\0'))
1699 g_free (shell);
1700 shell = g_strdup ("/bin/sh");
1703 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1704 /* and can be overriden by the MC_DATADIR environment variable */
1705 mc_libdir = getenv ("MC_DATADIR");
1706 if (mc_libdir != NULL)
1708 mc_home = g_strdup (mc_libdir);
1709 mc_home_alt = g_strdup (SYSCONFDIR);
1711 else
1713 mc_home = g_strdup (SYSCONFDIR);
1714 mc_home_alt = g_strdup (DATADIR);
1717 /* This variable is used by the subshell */
1718 home_dir = getenv ("HOME");
1720 if (!home_dir)
1721 home_dir = mc_home;
1724 /* --------------------------------------------------------------------------------------------- */
1726 static void
1727 sigchld_handler_no_subshell (int sig)
1729 #ifdef __linux__
1730 int pid, status;
1732 if (!console_flag)
1733 return;
1735 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1736 the value of console_flag never changed, we could simply not install
1737 this handler at all if (!console_flag && !use_subshell). */
1739 /* That comment is no longer true. We need to wait() on a sigchld
1740 handler (that's at least what the tarfs code expects currently). */
1742 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1744 if (pid == cons_saver_pid)
1747 if (WIFSTOPPED (status))
1749 /* Someone has stopped cons.saver - restart it */
1750 kill (pid, SIGCONT);
1752 else
1754 /* cons.saver has died - disable console saving */
1755 handle_console (CONSOLE_DONE);
1756 console_flag = 0;
1759 /* If we got here, some other child exited; ignore it */
1760 #endif /* __linux__ */
1762 (void) sig;
1765 /* --------------------------------------------------------------------------------------------- */
1767 static void
1768 init_sigchld (void)
1770 struct sigaction sigchld_action;
1772 sigchld_action.sa_handler =
1773 #ifdef HAVE_SUBSHELL_SUPPORT
1774 use_subshell ? sigchld_handler :
1775 #endif /* HAVE_SUBSHELL_SUPPORT */
1776 sigchld_handler_no_subshell;
1778 sigemptyset (&sigchld_action.sa_mask);
1780 #ifdef SA_RESTART
1781 sigchld_action.sa_flags = SA_RESTART;
1782 #else
1783 sigchld_action.sa_flags = 0;
1784 #endif /* !SA_RESTART */
1786 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
1788 #ifdef HAVE_SUBSHELL_SUPPORT
1790 * This may happen on QNX Neutrino 6, where SA_RESTART
1791 * is defined but not implemented. Fallback to no subshell.
1793 use_subshell = 0;
1794 #endif /* HAVE_SUBSHELL_SUPPORT */
1798 /* --------------------------------------------------------------------------------------------- */
1799 /*** public functions ****************************************************************************/
1800 /* --------------------------------------------------------------------------------------------- */
1802 /** Define this function for glib-style error handling */
1803 GQuark
1804 mc_main_error_quark (void)
1806 return g_quark_from_static_string (PACKAGE);
1809 /* --------------------------------------------------------------------------------------------- */
1810 /** Save current stat of directories to avoid reloading the panels
1811 * when no modifications have taken place
1814 void
1815 save_cwds_stat (void)
1817 if (panels_options.fast_reload)
1819 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
1820 if (get_other_type () == view_listing)
1821 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
1825 /* --------------------------------------------------------------------------------------------- */
1827 #ifdef HAVE_SUBSHELL_SUPPORT
1828 void
1829 do_update_prompt (void)
1831 if (update_prompt)
1833 printf ("\r\n%s", subshell_prompt);
1834 fflush (stdout);
1835 update_prompt = 0;
1838 #endif /* HAVE_SUBSHELL_SUPPORT */
1840 /* --------------------------------------------------------------------------------------------- */
1842 void
1843 change_panel (void)
1845 input_free_completions (cmdline);
1846 dlg_one_down (midnight_dlg);
1849 /* --------------------------------------------------------------------------------------------- */
1851 void
1852 update_menu (void)
1854 menu_set_name (left_menu, horizontal_split ? _("&Above") : _("&Left"));
1855 menu_set_name (right_menu, horizontal_split ? _("&Below") : _("&Right"));
1856 menubar_arrange (the_menubar);
1859 /* --------------------------------------------------------------------------------------------- */
1861 gboolean
1862 quiet_quit_cmd (void)
1864 print_last_revert = 1;
1865 return quit_cmd_internal (1);
1868 /* --------------------------------------------------------------------------------------------- */
1869 /** Wrapper for do_subshell_chdir, check for availability of subshell */
1871 void
1872 subshell_chdir (const char *directory)
1874 #ifdef HAVE_SUBSHELL_SUPPORT
1875 if (use_subshell)
1877 if (vfs_current_is_local ())
1878 do_subshell_chdir (directory, 0, 1);
1880 #endif /* HAVE_SUBSHELL_SUPPORT */
1883 /* --------------------------------------------------------------------------------------------- */
1886 do_cd (const char *new_dir, enum cd_enum exact)
1888 gboolean res;
1890 res = do_panel_cd (current_panel, new_dir, exact);
1892 #if HAVE_CHARSET
1893 if (res)
1895 const char *enc_name;
1897 enc_name = vfs_get_encoding (current_panel->cwd);
1898 if (enc_name != NULL)
1899 current_panel->codepage = get_codepage_index (enc_name);
1900 else
1901 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
1903 #endif /* HAVE_CHARSET */
1905 return res ? 1 : 0;
1908 /* --------------------------------------------------------------------------------------------- */
1910 #ifdef HAVE_SUBSHELL_SUPPORT
1912 load_prompt (int fd, void *unused)
1914 (void) fd;
1915 (void) unused;
1917 if (!read_subshell_prompt ())
1918 return 0;
1920 /* Don't actually change the prompt if it's invisible */
1921 if (((Dlg_head *) top_dlg->data == midnight_dlg) && command_prompt)
1923 char *tmp_prompt;
1924 int prompt_len;
1926 tmp_prompt = strip_ctrl_codes (subshell_prompt);
1927 prompt_len = str_term_width1 (tmp_prompt);
1929 /* Check for prompts too big */
1930 if (COLS > 8 && prompt_len > COLS - 8)
1932 prompt_len = COLS - 8;
1933 tmp_prompt[prompt_len] = '\0';
1935 mc_prompt = tmp_prompt;
1936 label_set_text (the_prompt, mc_prompt);
1937 input_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
1939 /* since the prompt has changed, and we are called from one of the
1940 * tty_get_event channels, the prompt updating does not take place
1941 * automatically: force a cursor update and a screen refresh
1943 update_cursor (midnight_dlg);
1944 mc_refresh ();
1946 update_prompt = 1;
1947 return 0;
1949 #endif /* HAVE_SUBSHELL_SUPPORT */
1951 /* --------------------------------------------------------------------------------------------- */
1953 void
1954 sort_cmd (void)
1956 WPanel *p;
1957 const panel_field_t *sort_order;
1959 if (!SELECTED_IS_PANEL)
1960 return;
1962 p = MENU_PANEL;
1963 sort_order = sort_box (p->current_sort_field, &p->reverse, &p->case_sensitive, &p->exec_first);
1965 panel_set_sort_order (p, sort_order);
1969 /* --------------------------------------------------------------------------------------------- */
1971 void
1972 toggle_show_hidden (void)
1974 panels_options.show_dot_files = !panels_options.show_dot_files;
1975 update_panels (UP_RELOAD, UP_KEEPSEL);
1978 /* --------------------------------------------------------------------------------------------- */
1980 void
1981 midnight_set_buttonbar (WButtonBar * b)
1983 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1984 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1985 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1986 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1987 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1988 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1989 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1990 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1991 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1992 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1995 /* --------------------------------------------------------------------------------------------- */
1996 /** Show current directory in the xterm title */
1998 void
1999 update_xterm_title_path (void)
2001 /* TODO: share code with midnight_get_title () */
2003 const char *path;
2004 char host[BUF_TINY];
2005 char *p;
2006 struct passwd *pw = NULL;
2007 char *login = NULL;
2008 int res = 0;
2010 if (xterm_flag && xterm_title)
2012 path = strip_home_and_password (current_panel->cwd);
2013 res = gethostname (host, sizeof (host));
2014 if (res)
2015 { /* On success, res = 0 */
2016 host[0] = '\0';
2018 else
2020 host[sizeof (host) - 1] = '\0';
2022 pw = getpwuid (getuid ());
2023 if (pw)
2025 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
2027 else
2029 login = g_strdup (host);
2031 p = g_strdup_printf ("mc [%s]:%s", login, path);
2032 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
2033 g_free (login);
2034 g_free (p);
2035 if (!alternate_plus_minus)
2036 numeric_keypad_mode ();
2037 fflush (stdout);
2041 /* --------------------------------------------------------------------------------------------- */
2043 * Load new hint and display it.
2044 * IF force is not 0, ignore the timeout.
2047 void
2048 load_hint (int force)
2050 char *hint;
2052 if (!the_hint->widget.owner)
2053 return;
2055 if (!message_visible)
2057 label_set_text (the_hint, NULL);
2058 return;
2061 hint = get_random_hint (force);
2063 if (hint != NULL)
2065 if (*hint)
2066 set_hintbar (hint);
2067 g_free (hint);
2069 else
2071 char text[BUF_SMALL];
2073 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
2074 set_hintbar (text);
2078 /* --------------------------------------------------------------------------------------------- */
2081 main (int argc, char *argv[])
2083 struct stat s;
2084 char *mc_dir;
2085 GError *error = NULL;
2086 gboolean isInitialized;
2088 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2089 setlocale (LC_ALL, "");
2090 bindtextdomain ("mc", LOCALEDIR);
2091 textdomain ("mc");
2093 /* Set up temporary directory */
2094 mc_tmpdir ();
2096 OS_Setup ();
2098 str_init_strings (NULL);
2100 vfs_init ();
2102 #ifdef USE_INTERNAL_EDIT
2103 edit_stack_init ();
2104 #endif
2106 if (!mc_args_handle (argc, argv, "mc"))
2107 exit (EXIT_FAILURE);
2109 /* NOTE: This has to be called before tty_init or whatever routine
2110 calls any define_sequence */
2111 init_key ();
2113 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2114 handle_console (CONSOLE_INIT);
2116 #ifdef HAVE_SUBSHELL_SUPPORT
2117 /* Don't use subshell when invoked as viewer or editor */
2118 if (mc_run_mode != MC_RUN_FULL)
2119 use_subshell = 0;
2121 if (use_subshell)
2122 subshell_get_console_attributes ();
2123 #endif /* HAVE_SUBSHELL_SUPPORT */
2125 /* Install the SIGCHLD handler; must be done before init_subshell() */
2126 init_sigchld ();
2128 /* We need this, since ncurses endwin () doesn't restore the signals */
2129 save_stop_handler ();
2131 /* Must be done before init_subshell, to set up the terminal size: */
2132 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2133 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2135 load_setup ();
2137 /* Removing this from the X code let's us type C-c */
2138 load_key_defs ();
2140 load_keymap_defs ();
2142 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2144 isInitialized = mc_skin_init (&error);
2146 mc_filehighlight = mc_fhl_new (TRUE);
2148 dlg_set_default_colors ();
2150 if (!isInitialized)
2152 message (D_ERROR, _("Warning"), "%s", error->message);
2153 g_error_free (error);
2154 error = NULL;
2157 /* create home directory */
2158 /* do it after the screen library initialization to show the error message */
2159 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2160 canonicalize_pathname (mc_dir);
2161 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
2162 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
2163 g_free (mc_dir);
2165 #ifdef HAVE_SUBSHELL_SUPPORT
2166 /* Done here to ensure that the subshell doesn't */
2167 /* inherit the file descriptors opened below, etc */
2168 if (use_subshell)
2169 init_subshell ();
2171 #endif /* HAVE_SUBSHELL_SUPPORT */
2173 /* Also done after init_subshell, to save any shell init file messages */
2174 if (console_flag)
2175 handle_console (CONSOLE_SAVE);
2177 if (alternate_plus_minus)
2178 application_keypad_mode ();
2180 #ifdef HAVE_SUBSHELL_SUPPORT
2181 if (use_subshell)
2183 mc_prompt = strip_ctrl_codes (subshell_prompt);
2184 if (mc_prompt == NULL)
2185 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2187 else
2188 #endif /* HAVE_SUBSHELL_SUPPORT */
2189 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2191 /* Program main loop */
2192 if (!midnight_shutdown)
2193 do_nc ();
2195 /* Save the tree store */
2196 tree_store_save ();
2198 free_keymap_defs ();
2200 /* Virtual File System shutdown */
2201 vfs_shut ();
2203 flush_extension_file (); /* does only free memory */
2205 mc_fhl_free (&mc_filehighlight);
2206 mc_skin_deinit ();
2208 tty_shutdown ();
2210 if (console_flag && !(quit & SUBSHELL_EXIT))
2211 handle_console (CONSOLE_RESTORE);
2212 if (alternate_plus_minus)
2213 numeric_keypad_mode ();
2215 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2217 if (console_flag)
2218 handle_console (CONSOLE_DONE);
2219 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2221 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string && !print_last_revert)
2223 int last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2224 S_IRUSR | S_IWUSR);
2226 if (last_wd_fd != -1)
2228 ssize_t ret1;
2229 int ret2;
2230 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2231 ret2 = close (last_wd_fd);
2234 g_free (last_wd_string);
2236 g_free (mc_home_alt);
2237 g_free (mc_home);
2238 g_free (shell);
2240 done_key ();
2241 #ifdef HAVE_CHARSET
2242 free_codepages_list ();
2243 g_free (autodetect_codeset);
2244 #endif
2245 g_free (clipboard_store_path);
2246 g_free (clipboard_paste_path);
2248 str_uninit_strings ();
2250 g_free (mc_run_param0);
2251 g_free (mc_run_param1);
2253 #ifdef USE_INTERNAL_EDIT
2254 edit_stack_free ();
2255 #endif
2257 return 0;
2260 /* --------------------------------------------------------------------------------------------- */