Removed calls to strerror() function from own libsamba implementation.
[midnight-commander.git] / src / main.c
blobdd9dc253237a3fc0e9f737573e65825ea74f74a9
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
6 1994, 1995 Janne Kukonlehto
7 1997 Norbert Warmuth
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file main.c
24 * \brief Source: this is a main module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pwd.h> /* for username in xterm title */
42 #include "global.h"
44 #include "../src/tty/tty.h"
45 #include "../src/skin/skin.h"
46 #include "../src/tty/mouse.h"
47 #include "../src/tty/key.h" /* For init_key() */
48 #include "../src/tty/win.h" /* xterm_flag */
50 #include "../src/mcconfig/mcconfig.h"
51 #include "../src/args.h"
52 #include "../src/skin/skin.h"
53 #include "../src/filehighlight/fhl.h"
55 #include "dir.h"
56 #include "dialog.h"
57 #include "menu.h"
58 #include "panel.h"
59 #include "option.h"
60 #include "tree.h"
61 #include "treestore.h"
62 #include "cons.saver.h"
63 #include "subshell.h"
64 #include "setup.h" /* save_setup() */
65 #include "boxes.h" /* sort_box() */
66 #include "layout.h"
67 #include "cmd.h" /* Normal commands */
68 #include "hotlist.h"
69 #include "panelize.h"
70 #include "learn.h" /* learn_keys() */
71 #include "listmode.h"
72 #include "execute.h"
73 #include "ext.h" /* For flush_extension_file() */
74 #include "strutil.h"
75 #include "widget.h"
76 #include "command.h"
77 #include "wtools.h"
78 #include "cmddef.h" /* CK_ cmd name const */
79 #include "fileloc.h" /* MC_USERCONF_DIR */
80 #include "user.h" /* user_file_menu_cmd() */
82 #include "../vfs/vfs.h" /* vfs_translate_url() */
84 #include "chmod.h"
85 #include "chown.h"
86 #include "achown.h"
88 #include "main.h"
90 #ifdef WITH_SMBFS
91 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
92 #endif
94 #ifdef USE_INTERNAL_EDIT
95 # include "../edit/edit.h"
96 #endif
98 #ifdef HAVE_CHARSET
99 #include "charsets.h"
100 #endif /* HAVE_CHARSET */
102 #ifdef ENABLE_VFS
103 #include "../vfs/gc.h"
104 #endif
106 #include "keybind.h" /* type global_keymap_t */
108 /* When the modes are active, left_panel, right_panel and tree_panel */
109 /* Point to a proper data structure. You should check with the functions */
110 /* get_current_type and get_other_type the types of the panels before using */
111 /* This pointer variables */
113 /* The structures for the panels */
114 WPanel *left_panel = NULL;
115 WPanel *right_panel = NULL;
117 mc_fhl_t *mc_filehighlight;
119 /* The pointer to the tree */
120 WTree *the_tree = NULL;
122 /* The Menubar */
123 struct WMenuBar *the_menubar = NULL;
125 /* Pointers to the selected and unselected panel */
126 WPanel *current_panel = NULL;
128 /* Set if the command is being run from the "Right" menu */
129 int is_right = 0;
131 /* Set when main loop should be terminated */
132 volatile int quit = 0;
134 /* Set if you want the possible completions dialog for the first time */
135 int show_all_if_ambiguous = 0;
137 /* Set when cd symlink following is desirable (bash mode) */
138 int cd_symlinks = 1;
140 /* If set then dialogs just clean the screen when refreshing, else */
141 /* they do a complete refresh, refreshing all the parts of the program */
142 int fast_refresh = 0;
144 /* If true, marking a files moves the cursor down */
145 int mark_moves_down = 1;
147 /* If true, at startup the user-menu is invoked */
148 int auto_menu = 0;
150 /* If true, then the +, - and \ keys have their special meaning only if the
151 * command line is emtpy, otherwise they behave like regular letters
153 int only_leading_plus_minus = 1;
155 int pause_after_run = pause_on_dumb_terminals;
157 /* It true saves the setup when quitting */
158 int auto_save_setup = 1;
160 #ifdef HAVE_CHARSET
162 * Don't restrict the output on the screen manager level,
163 * the translation tables take care of it.
165 #define full_eight_bits (1)
166 #define eight_bit_clean (1)
167 #else /* HAVE_CHARSET */
168 /* If true, allow characters in the range 160-255 */
169 int eight_bit_clean = 1;
171 * If true, also allow characters in the range 128-159.
172 * This is reported to break on many terminals (xterm, qansi-m).
174 int full_eight_bits = 0;
175 #endif /* !HAVE_CHARSET */
178 * If utf-8 terminal utf8_display = 1
179 * Display bits set UTF-8
182 int utf8_display = 0;
184 /* If true use the internal viewer */
185 int use_internal_view = 1;
187 /* Have we shown the fast-reload warning in the past? */
188 int fast_reload_w = 0;
190 /* Move page/item? When clicking on the top or bottom of a panel */
191 int mouse_move_pages = 1;
193 /* If true: l&r arrows are used to chdir if the input line is empty */
194 int navigate_with_arrows = 0;
196 /* If true program softkeys (HP terminals only) on startup and after every
197 command ran in the subshell to the description found in the termcap/terminfo
198 database */
199 int reset_hp_softkeys = 0;
201 /* The prompt */
202 const char *prompt = NULL;
204 /* The widget where we draw the prompt */
205 WLabel *the_prompt;
207 /* The hint bar */
208 WLabel *the_hint;
210 /* The button bar */
211 WButtonBar *the_bar;
213 /* Mouse type: GPM, xterm or none */
214 Mouse_Type use_mouse_p = MOUSE_NONE;
216 /* If on, default for "No" in delete operations */
217 int safe_delete = 0;
219 /* Controls screen clearing before an exec */
220 int clear_before_exec = 1;
222 /* Asks for confirmation before deleting a file */
223 int confirm_delete = 1;
225 /* Asks for confirmation before deleting a hotlist entry */
226 int confirm_directory_hotlist_delete = 1;
228 /* Asks for confirmation before overwriting a file */
229 int confirm_overwrite = 1;
231 /* Asks for confirmation before executing a program by pressing enter */
232 int confirm_execute = 0;
234 /* Asks for confirmation before leaving the program */
235 int confirm_exit = 1;
237 /* Asks for confirmation when using F3 to view a directory and there
238 are tagged files */
239 int confirm_view_dir = 0;
241 /* This flag indicates if the pull down menus by default drop down */
242 int drop_menus = 0;
244 /* The dialog handle for the main program */
245 Dlg_head *midnight_dlg = NULL;
247 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
248 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
249 int update_prompt = 0;
251 /* The home directory */
252 const char *home_dir = NULL;
254 /* Tab size */
255 int option_tab_spacing = 8;
257 /* The value of the other directory, only used when loading the setup */
258 char *other_dir = NULL;
260 /* Only used at program boot */
261 int boot_current_is_left = 1;
263 static char *this_dir = NULL;
265 /* If this is true, then when browsing the tree the other window will
266 * automatically reload it's directory with the contents of the currently
267 * selected directory.
269 int xtree_mode = 0;
271 /* If set, then print to the given file the last directory we were at */
272 static char *last_wd_string = NULL;
273 /* Set to 1 to suppress printing the last directory */
274 static int print_last_revert = 0;
276 /* File name to view if argument was supplied */
277 const char *view_one_file = NULL;
279 /* File name to edit if argument was supplied */
280 const char *edit_one_file = NULL;
282 /* Line to start the editor on */
283 static int edit_one_file_start_line = 0;
285 /* Used so that widgets know if they are being destroyed or
286 shut down */
287 int midnight_shutdown = 0;
289 /* The user's shell */
290 char *shell = NULL;
292 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
293 char *mc_home = NULL;
295 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
296 char *mc_home_alt = NULL;
298 /* Define this function for glib-style error handling */
299 GQuark
300 mc_main_error_quark (void)
302 return g_quark_from_static_string (PACKAGE);
305 #ifdef USE_INTERNAL_EDIT
306 GArray *editor_keymap = NULL;
307 GArray *editor_x_keymap = NULL;
308 #endif
309 GArray *viewer_keymap = NULL;
310 GArray *viewer_hex_keymap = NULL;
311 GArray *main_keymap = NULL;
312 GArray *main_x_keymap = NULL;
313 GArray *panel_keymap = NULL;
314 GArray *input_keymap = NULL;
315 GArray *tree_keymap = NULL;
316 GArray *help_keymap = NULL;
318 const global_keymap_t *main_map;
319 const global_keymap_t *main_x_map;
321 /* Save current stat of directories to avoid reloading the panels */
322 /* when no modifications have taken place */
323 void
324 save_cwds_stat (void)
326 if (fast_reload) {
327 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
328 if (get_other_type () == view_listing)
329 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
333 #ifdef HAVE_SUBSHELL_SUPPORT
334 void
335 do_update_prompt (void)
337 if (update_prompt) {
338 printf ("\r\n%s", subshell_prompt);
339 fflush (stdout);
340 update_prompt = 0;
343 #endif /* HAVE_SUBSHELL_SUPPORT */
345 void
346 change_panel (void)
348 free_completions (cmdline);
349 dlg_one_down (midnight_dlg);
352 /* Stop MC main dialog and the current dialog if it exists.
353 * Needed to provide fast exit from MC viewer or editor on shell exit */
354 static void
355 stop_dialogs (void)
357 midnight_dlg->running = 0;
358 if (current_dlg) {
359 current_dlg->running = 0;
363 static int
364 quit_cmd_internal (int quiet)
366 int q = quit;
368 if (quiet || !confirm_exit) {
369 q = 1;
370 } else {
371 if (query_dialog
372 (_(" The Midnight Commander "),
373 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
374 2, _("&Yes"), _("&No")) == 0)
375 q = 1;
377 if (q) {
378 #ifdef HAVE_SUBSHELL_SUPPORT
379 if (!use_subshell)
380 stop_dialogs ();
381 else if ((q = exit_subshell ()))
382 #endif
383 stop_dialogs ();
385 if (q)
386 quit |= 1;
387 return quit;
390 static void
391 quit_cmd (void)
393 quit_cmd_internal (0);
396 void
397 quiet_quit_cmd (void)
399 print_last_revert = 1;
400 quit_cmd_internal (1);
403 /* Wrapper for do_subshell_chdir, check for availability of subshell */
404 void
405 subshell_chdir (const char *directory)
407 #ifdef HAVE_SUBSHELL_SUPPORT
408 if (use_subshell) {
409 if (vfs_current_is_local ())
410 do_subshell_chdir (directory, 0, 1);
412 #endif /* HAVE_SUBSHELL_SUPPORT */
415 void
416 directory_history_add (struct WPanel *panel, const char *dir)
418 char *tmp;
420 tmp = g_strdup (dir);
421 strip_password (tmp, 1);
423 panel->dir_history = list_append_unique (panel->dir_history, tmp);
427 * If we moved to the parent directory move the selection pointer to
428 * the old directory name; If we leave VFS dir, remove FS specificator.
430 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
432 static const char *
433 get_parent_dir_name (const char *cwd, const char *lwd)
435 const char *p;
436 if (strlen (lwd) > strlen (cwd))
437 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
438 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
439 cwd[1] == '\0'))) {
440 return (p + 1);
442 return NULL;
446 * Changes the current directory of the panel.
447 * Don't record change in the directory history.
449 static int
450 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
452 const char *directory;
453 char *olddir;
454 char temp[MC_MAXPATHLEN];
455 char *translated_url;
457 if (cd_type == cd_parse_command) {
458 while (*new_dir == ' ')
459 new_dir++;
462 olddir = g_strdup (panel->cwd);
463 new_dir = translated_url = vfs_translate_url (new_dir);
465 /* Convert *new_path to a suitable pathname, handle ~user */
467 if (cd_type == cd_parse_command) {
468 if (!strcmp (new_dir, "-")) {
469 strcpy (temp, panel->lwd);
470 new_dir = temp;
473 directory = *new_dir ? new_dir : home_dir;
475 if (mc_chdir (directory) == -1) {
476 strcpy (panel->cwd, olddir);
477 g_free (olddir);
478 g_free (translated_url);
479 return 0;
481 g_free (translated_url);
483 /* Success: save previous directory, shutdown status of previous dir */
484 strcpy (panel->lwd, olddir);
485 free_completions (cmdline);
487 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
489 vfs_release_path (olddir);
491 subshell_chdir (panel->cwd);
493 /* Reload current panel */
494 panel_clean_dir (panel);
495 panel->count =
496 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
497 panel->reverse, panel->case_sensitive,
498 panel->exec_first, panel->filter);
499 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
500 load_hint (0);
501 panel->dirty = 1;
502 update_xterm_title_path ();
504 g_free (olddir);
506 return 1;
510 * Changes the current directory of the panel.
511 * Record change in the directory history.
514 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
516 int r;
518 r = _do_panel_cd (panel, new_dir, cd_type);
519 if (r)
520 directory_history_add (panel, panel->cwd);
521 return r;
525 do_cd (const char *new_dir, enum cd_enum exact)
527 return (do_panel_cd (current_panel, new_dir, exact));
530 void
531 directory_history_next (WPanel *panel)
533 GList *nextdir;
535 nextdir = g_list_next (panel->dir_history);
537 if (!nextdir)
538 return;
540 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
541 panel->dir_history = nextdir;
544 void
545 directory_history_prev (WPanel *panel)
547 GList *prevdir;
549 prevdir = g_list_previous (panel->dir_history);
551 if (!prevdir)
552 return;
554 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
555 panel->dir_history = prevdir;
558 void
559 directory_history_list (WPanel *panel)
561 char *s;
563 if (!panel->dir_history)
564 return;
566 s = show_hist (panel->dir_history, &panel->widget);
568 if (!s)
569 return;
571 if (_do_panel_cd (panel, s, cd_exact))
572 directory_history_add (panel, panel->cwd);
573 else
574 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
575 g_free (s);
578 #ifdef HAVE_SUBSHELL_SUPPORT
580 load_prompt (int fd, void *unused)
582 (void) fd;
583 (void) unused;
585 if (!read_subshell_prompt ())
586 return 0;
588 /* Don't actually change the prompt if it's invisible */
589 if (current_dlg == midnight_dlg && command_prompt) {
590 char *tmp_prompt;
591 int prompt_len;
593 tmp_prompt = strip_ctrl_codes (subshell_prompt);
594 prompt_len = str_term_width1 (tmp_prompt);
596 /* Check for prompts too big */
597 if (COLS > 8 && prompt_len > COLS - 8) {
598 tmp_prompt[COLS - 8] = '\0';
599 prompt_len = COLS - 8;
601 prompt = tmp_prompt;
602 label_set_text (the_prompt, prompt);
603 winput_set_origin ((WInput *) cmdline, prompt_len,
604 COLS - prompt_len);
606 /* since the prompt has changed, and we are called from one of the
607 * tty_get_event channels, the prompt updating does not take place
608 * automatically: force a cursor update and a screen refresh
610 update_cursor (midnight_dlg);
611 mc_refresh ();
613 update_prompt = 1;
614 return 0;
616 #endif /* HAVE_SUBSHELL_SUPPORT */
618 void
619 sort_cmd (void)
621 WPanel *p;
622 const panel_field_t *sort_order;
624 if (!SELECTED_IS_PANEL)
625 return;
627 p = MENU_PANEL;
628 sort_order = sort_box (p->current_sort_field, &p->reverse,
629 &p->case_sensitive,
630 &p->exec_first);
632 panel_set_sort_order (p, sort_order);
636 static void
637 treebox_cmd (void)
639 char *sel_dir;
641 sel_dir = tree_box (selection (current_panel)->fname);
642 if (sel_dir) {
643 do_cd (sel_dir, cd_exact);
644 g_free (sel_dir);
648 #ifdef LISTMODE_EDITOR
649 static void
650 listmode_cmd (void)
652 char *newmode;
654 if (get_current_type () != view_listing)
655 return;
657 newmode = listmode_edit (current_panel->user_format);
658 if (!newmode)
659 return;
661 g_free (current_panel->user_format);
662 current_panel->list_type = list_user;
663 current_panel->user_format = newmode;
664 set_panel_formats (current_panel);
666 do_refresh ();
668 #endif /* LISTMODE_EDITOR */
670 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
671 static GList *
672 create_panel_menu (void)
674 GList *entries = NULL;
676 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
677 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
678 entries = g_list_append (entries, menu_entry_create (_("&Info" ), CK_InfoCmd));
679 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
680 entries = g_list_append (entries, menu_separator_create ());
681 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
682 entries = g_list_append (entries, menu_separator_create ());
683 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
684 #ifdef HAVE_CHARSET
685 entries = g_list_append (entries, menu_separator_create ());
686 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
687 #endif
688 #ifdef USE_NETCODE
689 entries = g_list_append (entries, menu_separator_create ());
690 #ifdef ENABLE_VFS_MCFS
691 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
692 #endif
693 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
694 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
695 #ifdef WITH_SMBFS
696 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
697 #endif
698 #endif
699 entries = g_list_append (entries, menu_separator_create ());
700 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
702 return entries;
705 static GList *
706 create_file_menu (void)
708 GList *entries = NULL;
710 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
711 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
712 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
713 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
714 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
715 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
716 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
717 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
718 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
719 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
720 entries = g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
721 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
722 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
723 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
724 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
725 entries = g_list_append (entries, menu_separator_create ());
726 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
727 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
728 entries = g_list_append (entries, menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
729 entries = g_list_append (entries, menu_separator_create ());
730 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
732 return entries;
735 static GList *
736 create_command_menu (void)
738 /* I know, I'm lazy, but the tree widget when it's not running
739 * as a panel still has some problems, I have not yet finished
740 * the WTree widget port, sorry.
742 GList *entries = NULL;
744 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
745 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
746 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
747 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
748 entries = g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
749 entries = g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
750 entries = g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
751 entries = g_list_append (entries, menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
752 entries = g_list_append (entries, menu_separator_create ());
753 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
754 entries = g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
755 #ifdef ENABLE_VFS
756 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
757 #endif
758 #ifdef WITH_BACKGROUND
759 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
760 #endif
761 entries = g_list_append (entries, menu_separator_create ());
762 #ifdef USE_EXT2FSLIB
763 entries = g_list_append (entries, menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
764 #endif
765 #ifdef LISTMODE_EDITOR
766 entries = g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
767 #endif
768 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
769 entries = g_list_append (entries, menu_separator_create ());
770 #endif
771 entries = g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
772 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
773 entries = g_list_append (entries, menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
775 return entries;
778 static GList *
779 create_options_menu (void)
781 GList *entries = NULL;
783 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
784 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
785 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
786 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
787 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
788 #ifdef ENABLE_VFS
789 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
790 #endif
791 entries = g_list_append (entries, menu_separator_create ());
792 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
794 return entries;
797 void
798 init_menu (void)
800 menubar_add_menu (the_menubar,
801 create_menu (horizontal_split ? _("&Above") : _("&Left"),
802 create_panel_menu (), "[Left and Right Menus]"));
803 menubar_add_menu (the_menubar,
804 create_menu (_("&File"), create_file_menu (), "[File Menu]"));
805 menubar_add_menu (the_menubar,
806 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
807 menubar_add_menu (the_menubar,
808 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
809 menubar_add_menu (the_menubar,
810 create_menu (horizontal_split ? _("&Below") : _("&Right"),
811 create_panel_menu (), "[Left and Right Menus]"));
814 void
815 done_menu (void)
817 menubar_set_menu (the_menubar, NULL);
820 static void
821 menu_last_selected_cmd (void)
823 the_menubar->is_active = TRUE;
824 the_menubar->is_dropped = (drop_menus != 0);
825 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
826 dlg_select_widget (the_menubar);
829 static void
830 menu_cmd (void)
832 if (the_menubar->is_active)
833 return;
835 if ((get_current_index () == 0) == (current_panel->active != 0))
836 the_menubar->selected = 0;
837 else
838 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
839 menu_last_selected_cmd ();
842 static char *
843 midnight_get_shortcut (unsigned long command)
845 const char *ext_map;
846 const char *shortcut = NULL;
848 shortcut = lookup_keymap_shortcut (main_map, command);
849 if (shortcut != NULL)
850 return g_strdup (shortcut);
852 shortcut = lookup_keymap_shortcut (panel_map, command);
853 if (shortcut != NULL)
854 return g_strdup (shortcut);
856 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
857 if (ext_map != NULL)
858 shortcut = lookup_keymap_shortcut (main_x_map, command);
859 if (shortcut != NULL)
860 return g_strdup_printf ("%s %s", ext_map, shortcut);
862 return NULL;
865 /* Flag toggling functions */
866 void
867 toggle_fast_reload (void)
869 fast_reload = !fast_reload;
870 if (fast_reload_w == 0 && fast_reload) {
871 message (D_NORMAL, _(" Information "),
873 (" Using the fast reload option may not reflect the exact \n"
874 " directory contents. In this case you'll need to do a \n"
875 " manual reload of the directory. See the man page for \n"
876 " the details. "));
877 fast_reload_w = 1;
881 void
882 toggle_mix_all_files (void)
884 mix_all_files = !mix_all_files;
885 update_panels (UP_RELOAD, UP_KEEPSEL);
888 void
889 toggle_show_backup (void)
891 show_backups = !show_backups;
892 update_panels (UP_RELOAD, UP_KEEPSEL);
895 void
896 toggle_show_hidden (void)
898 show_dot_files = !show_dot_files;
899 update_panels (UP_RELOAD, UP_KEEPSEL);
902 void
903 toggle_kilobyte_si (void)
905 kilobyte_si = !kilobyte_si;
906 update_panels (UP_RELOAD, UP_KEEPSEL);
910 * Just a hack for allowing url-like pathnames to be accepted from the
911 * command line.
913 static void
914 translated_mc_chdir (char *dir)
916 char *newdir;
918 newdir = vfs_translate_url (dir);
919 mc_chdir (newdir);
920 g_free (newdir);
923 static void
924 create_panels (void)
926 int current_index;
927 int other_index;
928 int current_mode;
929 int other_mode;
930 char original_dir[1024];
932 original_dir[0] = 0;
934 if (boot_current_is_left) {
935 current_index = 0;
936 other_index = 1;
937 current_mode = startup_left_mode;
938 other_mode = startup_right_mode;
939 } else {
940 current_index = 1;
941 other_index = 0;
942 current_mode = startup_right_mode;
943 other_mode = startup_left_mode;
945 /* Creates the left panel */
946 if (this_dir) {
947 if (other_dir) {
948 /* Ok, user has specified two dirs, save the original one,
949 * since we may not be able to chdir to the proper
950 * second directory later
952 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
954 translated_mc_chdir (this_dir);
956 set_display_type (current_index, current_mode);
958 /* The other panel */
959 if (other_dir) {
960 if (original_dir[0])
961 translated_mc_chdir (original_dir);
962 translated_mc_chdir (other_dir);
964 set_display_type (other_index, other_mode);
966 if (startup_left_mode == view_listing) {
967 current_panel = left_panel;
968 } else {
969 if (right_panel)
970 current_panel = right_panel;
971 else
972 current_panel = left_panel;
975 /* Create the nice widgets */
976 cmdline = command_new (0, 0, 0);
977 the_prompt = label_new (0, 0, prompt);
978 the_prompt->transparent = 1;
979 the_bar = buttonbar_new (keybar_visible);
981 the_hint = label_new (0, 0, 0);
982 the_hint->transparent = 1;
983 the_hint->auto_adjust_cols = 0;
984 the_hint->widget.cols = COLS;
986 the_menubar = menubar_new (0, 0, COLS, NULL);
989 static void
990 copy_current_pathname (void)
992 char *cwd_path;
993 if (!command_prompt)
994 return;
996 cwd_path = remove_encoding_from_path (current_panel->cwd);
997 command_insert (cmdline, cwd_path, 0);
999 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1000 command_insert (cmdline, PATH_SEP_STR, 0);
1001 g_free (cwd_path);
1004 static void
1005 copy_other_pathname (void)
1007 char *cwd_path;
1009 if (get_other_type () != view_listing)
1010 return;
1012 if (!command_prompt)
1013 return;
1015 cwd_path = remove_encoding_from_path (other_panel->cwd);
1016 command_insert (cmdline, cwd_path, 0);
1018 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1019 command_insert (cmdline, PATH_SEP_STR, 0);
1020 g_free (cwd_path);
1023 static void
1024 copy_readlink (WPanel *panel)
1026 if (!command_prompt)
1027 return;
1028 if (S_ISLNK (selection (panel)->st.st_mode)) {
1029 char buffer[MC_MAXPATHLEN];
1030 char *p =
1031 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1032 int i;
1034 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1035 g_free (p);
1036 if (i > 0) {
1037 buffer[i] = 0;
1038 command_insert (cmdline, buffer, 1);
1043 static void
1044 copy_current_readlink (void)
1046 copy_readlink (current_panel);
1049 static void
1050 copy_other_readlink (void)
1052 if (get_other_type () != view_listing)
1053 return;
1054 copy_readlink (other_panel);
1057 /* Insert the selected file name into the input line */
1058 static void
1059 copy_prog_name (void)
1061 char *tmp;
1062 if (!command_prompt)
1063 return;
1065 if (get_current_type () == view_tree) {
1066 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1067 tmp = tree_selected_name (tree);
1068 } else
1069 tmp = selection (current_panel)->fname;
1071 command_insert (cmdline, tmp, 1);
1074 static void
1075 copy_tagged (WPanel *panel)
1077 int i;
1079 if (!command_prompt)
1080 return;
1081 input_disable_update (cmdline);
1082 if (panel->marked) {
1083 for (i = 0; i < panel->count; i++) {
1084 if (panel->dir.list[i].f.marked)
1085 command_insert (cmdline, panel->dir.list[i].fname, 1);
1087 } else {
1088 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1091 input_enable_update (cmdline);
1094 static void
1095 copy_current_tagged (void)
1097 copy_tagged (current_panel);
1100 static void
1101 copy_other_tagged (void)
1103 if (get_other_type () != view_listing)
1104 return;
1105 copy_tagged (other_panel);
1108 void
1109 midnight_set_buttonbar (WButtonBar *b)
1111 buttonbar_set_label (b, 1, Q_("ButtonBar|Help"), main_map, NULL);
1112 buttonbar_set_label (b, 2, Q_("ButtonBar|Menu"), main_map, NULL);
1113 buttonbar_set_label (b, 3, Q_("ButtonBar|View"), main_map, NULL);
1114 buttonbar_set_label (b, 4, Q_("ButtonBar|Edit"), main_map, NULL);
1115 buttonbar_set_label (b, 5, Q_("ButtonBar|Copy"), main_map, NULL);
1116 buttonbar_set_label (b, 6, Q_("ButtonBar|RenMov"), main_map, NULL);
1117 buttonbar_set_label (b, 7, Q_("ButtonBar|Mkdir"), main_map, NULL);
1118 buttonbar_set_label (b, 8, Q_("ButtonBar|Delete"), main_map, NULL);
1119 buttonbar_set_label (b, 9, Q_("ButtonBar|PullDn"), main_map, NULL);
1120 buttonbar_set_label (b, 10, Q_("ButtonBar|Quit"), main_map, NULL);
1123 static gboolean ctl_x_map_enabled = FALSE;
1125 static void
1126 ctl_x_cmd (void)
1128 ctl_x_map_enabled = TRUE;
1131 static cb_ret_t
1132 midnight_execute_cmd (Widget *sender, unsigned long command)
1134 cb_ret_t res = MSG_HANDLED;
1136 (void) sender;
1138 switch (command) {
1139 case CK_AddHotlist:
1140 add2hotlist_cmd ();
1141 break;
1142 case CK_ChmodCmd:
1143 chmod_cmd ();
1144 break;
1145 case CK_ChownCmd:
1146 chown_cmd ();
1147 break;
1148 case CK_ChownAdvancedCmd:
1149 chown_advanced_cmd ();
1150 break;
1151 case CK_CompareDirsCmd:
1152 compare_dirs_cmd ();
1153 break;
1154 case CK_ConfigureBox:
1155 configure_box ();
1156 break;
1157 #ifdef ENABLE_VFS
1158 case CK_ConfigureVfs:
1159 configure_vfs ();
1160 break;
1161 #endif
1162 case CK_ConfirmBox:
1163 confirm_box ();
1164 break;
1165 case CK_CopyCmd:
1166 copy_cmd ();
1167 break;
1168 case CK_CopyCurrentPathname:
1169 copy_current_pathname ();
1170 break;
1171 case CK_CopyCurrentReadlink:
1172 copy_current_readlink ();
1173 break;
1174 case CK_CopyCurrentTagged:
1175 copy_current_tagged ();
1176 break;
1177 case CK_CopyOtherPathname:
1178 copy_other_pathname ();
1179 break;
1180 case CK_CopyOtherReadlink:
1181 copy_other_readlink ();
1182 break;
1183 case CK_CopyOtherTagged:
1184 copy_other_tagged ();
1185 break;
1186 case CK_DeleteCmd:
1187 delete_cmd ();
1188 break;
1189 case CK_DisplayBitsBox:
1190 display_bits_box ();
1191 break;
1192 case CK_EditCmd:
1193 edit_cmd ();
1194 break;
1195 case CK_EditExtFileCmd:
1196 ext_cmd ();
1197 break;
1198 case CK_EditFhlFileCmd:
1199 edit_fhl_cmd ();
1200 break;
1201 case CK_EditMcMenuCmd:
1202 edit_mc_menu_cmd ();
1203 break;
1204 case CK_EditSymlinkCmd:
1205 edit_symlink_cmd ();
1206 break;
1207 case CK_ExternalPanelize:
1208 external_panelize ();
1209 break;
1210 case CK_FilterCmd:
1211 filter_cmd ();
1212 break;
1213 case CK_FilteredViewCmd:
1214 filtered_view_cmd ();
1215 break;
1216 case CK_FindCmd:
1217 find_cmd ();
1218 break;
1219 #if defined (USE_NETCODE)
1220 case CK_FishlinkCmd:
1221 fishlink_cmd ();
1222 break;
1223 case CK_FtplinkCmd:
1224 ftplink_cmd ();
1225 break;
1226 #endif
1227 case CK_HelpCmd:
1228 help_cmd ();
1229 break;
1230 case CK_HistoryCmd:
1231 history_cmd ();
1232 break;
1233 case CK_InfoCmd:
1234 if (sender == (Widget *) the_menubar)
1235 info_cmd (); /* mwnu */
1236 else
1237 info_cmd_no_menu (); /* shortcut or buttonbar */
1238 break;
1239 #ifdef WITH_BACKGROUND
1240 case CK_JobsCmd:
1241 jobs_cmd ();
1242 break;
1243 #endif
1244 case CK_LayoutCmd:
1245 layout_cmd ();
1246 break;
1247 case CK_LearnKeys:
1248 learn_keys ();
1249 break;
1250 case CK_LinkCmd:
1251 link_cmd ();
1252 break;
1253 case CK_ListingCmd:
1254 listing_cmd ();
1255 break;
1256 #ifdef LISTMODE_EDITOR
1257 case CK_ListmodeCmd:
1258 listmode_cmd ();
1259 break;
1260 #endif
1261 case CK_MenuCmd:
1262 menu_cmd ();
1263 break;
1264 case CK_MenuLastSelectedCmd:
1265 menu_last_selected_cmd ();
1266 break;
1267 case CK_MkdirCmd:
1268 mkdir_cmd ();
1269 break;
1270 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1271 case CK_NetlinkCmd:
1272 netlink_cmd ();
1273 break;
1274 #endif
1275 #ifdef HAVE_CHARSET
1276 case CK_PanelSetPanelEncoding:
1277 encoding_cmd ();
1278 break;
1279 #endif
1280 case CK_QuickCdCmd:
1281 quick_cd_cmd ();
1282 break;
1283 case CK_QuickChdirCmd:
1284 quick_chdir_cmd ();
1285 break;
1286 case CK_QuickViewCmd:
1287 if (sender == (Widget *) the_menubar)
1288 quick_view_cmd (); /* menu */
1289 else
1290 quick_cmd_no_menu (); /* shortcut or buttonabr */
1291 break;
1292 case CK_QuietQuitCmd:
1293 quiet_quit_cmd ();
1294 break;
1295 case CK_QuitCmd:
1296 quit_cmd ();
1297 break;
1298 case CK_RenameCmd:
1299 rename_cmd ();
1300 break;
1301 case CK_RereadCmd:
1302 reread_cmd ();
1303 break;
1304 #ifdef ENABLE_VFS
1305 case CK_ReselectVfs:
1306 reselect_vfs ();
1307 break;
1308 #endif
1309 case CK_ReverseSelectionCmd:
1310 reverse_selection_cmd ();
1311 break;
1312 case CK_SaveSetupCmd:
1313 save_setup_cmd ();
1314 break;
1315 case CK_SelectCmd:
1316 select_cmd ();
1317 break;
1318 case CK_ShowCommandLine:
1319 view_other_cmd ();
1320 break;
1321 case CK_SingleDirsizeCmd:
1322 smart_dirsize_cmd ();
1323 break;
1324 #if defined (USE_NETCODE) && defined (WITH_SMBFS)
1325 case CK_SmblinkCmd:
1326 smblink_cmd ();
1327 break;
1328 #endif
1329 case CK_Sort:
1330 sort_cmd ();
1331 break;
1332 case CK_StartExtMap1:
1333 ctl_x_cmd ();
1334 break;
1335 case CK_SuspendCmd:
1336 suspend_cmd ();
1337 break;
1338 case CK_SwapCmd:
1339 swap_cmd ();
1340 break;
1341 case CK_SymlinkCmd:
1342 symlink_cmd ();
1343 break;
1344 case CK_ToggleListingCmd:
1345 toggle_listing_cmd ();
1346 break;
1347 case CK_ToggleShowHidden:
1348 toggle_show_hidden ();
1349 break;
1350 case CK_TreeCmd:
1351 tree_cmd ();
1352 break;
1353 case CK_TreeBoxCmd:
1354 treebox_cmd ();
1355 break;
1356 #ifdef USE_EXT2FSLIB
1357 case CK_UndeleteCmd:
1358 undelete_cmd ();
1359 break;
1360 #endif
1361 case CK_UnselectCmd:
1362 unselect_cmd ();
1363 break;
1364 case CK_UserMenuCmd:
1365 user_file_menu_cmd ();
1366 break;
1367 case CK_ViewCmd:
1368 view_cmd ();
1369 break;
1370 case CK_ViewFileCmd:
1371 view_file_cmd ();
1372 break;
1373 default:
1374 res = MSG_NOT_HANDLED;
1377 return res;
1380 static void
1381 setup_pre (void)
1383 /* Call all the inits */
1385 #ifdef HAVE_SLANG
1386 tty_display_8bit (full_eight_bits != 0);
1387 #else
1388 tty_display_8bit (eight_bit_clean != 0);
1389 #endif
1392 static void
1393 init_xterm_support (void)
1395 const char *termvalue;
1397 termvalue = getenv ("TERM");
1398 if (!termvalue || !(*termvalue)) {
1399 fputs (_("The TERM environment variable is unset!\n"), stderr);
1400 exit (1);
1403 /* Check mouse capabilities */
1404 xmouse_seq = tty_tgetstr ("Km");
1406 if (strcmp (termvalue, "cygwin") == 0) {
1407 mc_args__force_xterm = 1;
1408 use_mouse_p = MOUSE_DISABLED;
1411 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1412 || strncmp (termvalue, "konsole", 7) == 0
1413 || strncmp (termvalue, "rxvt", 4) == 0
1414 || strcmp (termvalue, "Eterm") == 0
1415 || strcmp (termvalue, "dtterm") == 0) {
1416 xterm_flag = 1;
1418 /* Default to the standard xterm sequence */
1419 if (!xmouse_seq) {
1420 xmouse_seq = ESC_STR "[M";
1423 /* Enable mouse unless explicitly disabled by --nomouse */
1424 if (use_mouse_p != MOUSE_DISABLED) {
1425 const char *color_term = getenv ("COLORTERM");
1426 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1427 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1428 strcmp (termvalue, "Eterm") == 0) {
1429 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1430 } else {
1431 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1437 static void
1438 setup_mc (void)
1440 setup_pre ();
1441 create_panels ();
1442 setup_panels ();
1444 #ifdef HAVE_SUBSHELL_SUPPORT
1445 if (use_subshell)
1446 add_select_channel (subshell_pty, load_prompt, 0);
1447 #endif /* !HAVE_SUBSHELL_SUPPORT */
1449 tty_setup_sigwinch (sigwinch_handler);
1451 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1453 init_xterm_support ();
1454 init_mouse ();
1457 static void
1458 setup_dummy_mc (void)
1460 char d[MC_MAXPATHLEN];
1462 mc_get_current_wd (d, MC_MAXPATHLEN);
1463 setup_mc ();
1464 mc_chdir (d);
1467 static void check_codeset()
1469 const char *current_system_codepage = NULL;
1471 current_system_codepage = str_detect_termencoding();
1473 #ifdef HAVE_CHARSET
1475 const char *_display_codepage;
1477 _display_codepage = get_codepage_id (display_codepage);
1479 if (! strcmp(_display_codepage, current_system_codepage)) {
1480 utf8_display = str_isutf8 (current_system_codepage);
1481 return;
1484 display_codepage = get_codepage_index (current_system_codepage);
1485 if (display_codepage == -1) {
1486 display_codepage = 0;
1489 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1491 #endif
1492 utf8_display = str_isutf8 (current_system_codepage);
1495 static void
1496 done_screen (void)
1498 if (!(quit & SUBSHELL_EXIT))
1499 clr_scr ();
1500 tty_reset_shell_mode ();
1501 tty_noraw_mode ();
1502 tty_keypad (FALSE);
1503 tty_colors_done ();
1506 static void
1507 done_mc (void)
1509 disable_mouse ();
1511 /* Setup shutdown
1513 * We sync the profiles since the hotlist may have changed, while
1514 * we only change the setup data if we have the auto save feature set
1517 if (auto_save_setup)
1518 save_setup (); /* does also call save_hotlist */
1519 else {
1520 save_hotlist ();
1521 save_panel_types ();
1523 done_screen ();
1524 vfs_add_current_stamps ();
1527 /* This should be called after destroy_dlg since panel widgets
1528 * save their state on the profiles
1530 static void
1531 done_mc_profile (void)
1533 done_setup ();
1536 static cb_ret_t
1537 midnight_callback (Dlg_head *h, Widget *sender,
1538 dlg_msg_t msg, int parm, void *data)
1540 unsigned long command;
1542 switch (msg) {
1544 case DLG_DRAW:
1545 load_hint (1);
1546 /* We handle the special case of the output lines */
1547 if (console_flag && output_lines)
1548 show_console_contents (output_start_y,
1549 LINES - output_lines - keybar_visible -
1550 1, LINES - keybar_visible - 1);
1551 return MSG_HANDLED;
1553 case DLG_RESIZE:
1554 setup_panels ();
1555 menubar_arrange (the_menubar);
1556 return MSG_HANDLED;
1558 case DLG_IDLE:
1559 /* We only need the first idle event to show user menu after start */
1560 set_idle_proc (h, 0);
1561 if (auto_menu)
1562 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1563 return MSG_HANDLED;
1565 case DLG_KEY:
1566 if (ctl_x_map_enabled) {
1567 ctl_x_map_enabled = FALSE;
1568 command = lookup_keymap_command (main_x_map, parm);
1569 if (command != CK_Ignore_Key)
1570 return midnight_execute_cmd (NULL, command);
1573 /* FIXME: should handle all menu shortcuts before this point */
1574 if (the_menubar->is_active)
1575 return MSG_NOT_HANDLED;
1577 if (parm == '\t')
1578 free_completions (cmdline);
1580 if (parm == '\n') {
1581 size_t i;
1583 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1584 cmdline->buffer[i] == '\t'); i++)
1586 if (cmdline->buffer[i]) {
1587 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1588 return MSG_HANDLED;
1590 stuff (cmdline, "", 0);
1591 cmdline->point = 0;
1594 /* Ctrl-Enter and Alt-Enter */
1595 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1596 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1597 copy_prog_name ();
1598 return MSG_HANDLED;
1601 /* Ctrl-Shift-Enter */
1602 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1603 copy_current_pathname ();
1604 copy_prog_name ();
1605 return MSG_HANDLED;
1608 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1609 && !quote && !current_panel->searching) {
1610 if (!only_leading_plus_minus) {
1611 /* Special treatement, since the input line will eat them */
1612 if (parm == '+') {
1613 select_cmd ();
1614 return MSG_HANDLED;
1617 if (parm == '\\' || parm == '-') {
1618 unselect_cmd ();
1619 return MSG_HANDLED;
1622 if (parm == '*') {
1623 reverse_selection_cmd ();
1624 return MSG_HANDLED;
1626 } else if (!command_prompt || !cmdline->buffer[0]) {
1627 /* Special treatement '+', '-', '\', '*' only when this is
1628 * first char on input line
1631 if (parm == '+') {
1632 select_cmd ();
1633 return MSG_HANDLED;
1636 if (parm == '\\' || parm == '-') {
1637 unselect_cmd ();
1638 return MSG_HANDLED;
1641 if (parm == '*') {
1642 reverse_selection_cmd ();
1643 return MSG_HANDLED;
1647 return MSG_NOT_HANDLED;
1649 case DLG_HOTKEY_HANDLED:
1650 if ((get_current_type () == view_listing) && current_panel->searching) {
1651 current_panel->searching = 0;
1652 current_panel->dirty = 1;
1654 return MSG_HANDLED;
1656 case DLG_UNHANDLED_KEY:
1657 if (command_prompt) {
1658 cb_ret_t v;
1660 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1661 if (v == MSG_HANDLED)
1662 return MSG_HANDLED;
1665 if (ctl_x_map_enabled) {
1666 ctl_x_map_enabled = FALSE;
1667 command = lookup_keymap_command (main_x_map, parm);
1668 } else
1669 command = lookup_keymap_command (main_map, parm);
1671 return (command == CK_Ignore_Key)
1672 ? MSG_NOT_HANDLED
1673 : midnight_execute_cmd (NULL, command);
1675 case DLG_POST_KEY:
1676 if (!the_menubar->is_active)
1677 update_dirty_panels ();
1678 return MSG_HANDLED;
1680 case DLG_ACTION:
1681 /* shortcut */
1682 if (sender == NULL)
1683 midnight_execute_cmd (NULL, parm);
1684 /* message from menu */
1685 else if (sender == (Widget *) the_menubar)
1686 midnight_execute_cmd (sender, parm);
1687 /* message from buttonbar */
1688 else if (sender == (Widget *) the_bar) {
1689 if (data == NULL)
1690 midnight_execute_cmd (sender, parm);
1691 else
1692 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1694 return MSG_HANDLED;
1696 default:
1697 return default_dlg_callback (h, sender, msg, parm, data);
1701 /* Show current directory in the xterm title */
1702 void
1703 update_xterm_title_path (void)
1705 const char *path;
1706 char host[BUF_TINY];
1707 char *p;
1708 struct passwd *pw = NULL;
1709 char *login = NULL;
1710 int res = 0;
1711 if (xterm_flag && xterm_title) {
1712 path = strip_home_and_password (current_panel->cwd);
1713 res = gethostname(host, sizeof (host));
1714 if ( res ) { /* On success, res = 0 */
1715 host[0] = '\0';
1716 } else {
1717 host[sizeof (host) - 1] = '\0';
1719 pw = getpwuid(getuid());
1720 if ( pw ) {
1721 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1722 } else {
1723 login = g_strdup (host);
1725 p = g_strdup_printf ("mc [%s]:%s", login, path);
1726 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1727 g_free (login);
1728 g_free (p);
1729 if (!alternate_plus_minus)
1730 numeric_keypad_mode ();
1731 fflush (stdout);
1736 * Load new hint and display it.
1737 * IF force is not 0, ignore the timeout.
1739 void
1740 load_hint (int force)
1742 char *hint;
1744 if (!the_hint->widget.parent)
1745 return;
1747 if (!message_visible) {
1748 label_set_text (the_hint, NULL);
1749 return;
1752 hint = get_random_hint (force);
1754 if (hint != NULL) {
1755 if (*hint)
1756 set_hintbar (hint);
1757 g_free (hint);
1758 } else {
1759 char text[BUF_SMALL];
1761 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1762 VERSION);
1763 set_hintbar (text);
1767 static void
1768 setup_panels_and_run_mc (void)
1770 midnight_dlg->get_shortcut = midnight_get_shortcut;
1772 add_widget (midnight_dlg, the_menubar);
1773 init_menu ();
1775 add_widget (midnight_dlg, get_panel_widget (0));
1776 add_widget (midnight_dlg, get_panel_widget (1));
1777 add_widget (midnight_dlg, the_hint);
1778 add_widget (midnight_dlg, cmdline);
1779 add_widget (midnight_dlg, the_prompt);
1781 add_widget (midnight_dlg, the_bar);
1782 midnight_set_buttonbar (the_bar);
1784 if (boot_current_is_left)
1785 dlg_select_widget (get_panel_widget (0));
1786 else
1787 dlg_select_widget (get_panel_widget (1));
1789 /* Run the Midnight Commander if no file was specified in the command line */
1790 run_dlg (midnight_dlg);
1793 /* result must be free'd (I think this should go in util.c) */
1794 static char *
1795 prepend_cwd_on_local (const char *filename)
1797 char *d;
1798 int l;
1800 if (vfs_file_is_local (filename)) {
1801 if (*filename == PATH_SEP) /* an absolute pathname */
1802 return g_strdup (filename);
1803 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1804 mc_get_current_wd (d, MC_MAXPATHLEN);
1805 l = strlen (d);
1806 d[l++] = PATH_SEP;
1807 strcpy (d + l, filename);
1808 canonicalize_pathname (d);
1809 return d;
1810 } else
1811 return g_strdup (filename);
1814 static int
1815 mc_maybe_editor_or_viewer (void)
1817 if (!(view_one_file || edit_one_file))
1818 return 0;
1820 /* Invoke the internal view/edit routine with:
1821 * the default processing and forcing the internal viewer/editor
1823 if (view_one_file) {
1824 char *path = NULL;
1825 path = prepend_cwd_on_local (view_one_file);
1826 view_file (path, 0, 1);
1827 g_free (path);
1829 #ifdef USE_INTERNAL_EDIT
1830 else {
1831 edit_file (edit_one_file, edit_one_file_start_line);
1833 #endif /* USE_INTERNAL_EDIT */
1834 midnight_shutdown = 1;
1835 done_mc ();
1836 return 1;
1839 /* Run the main dialog that occupies the whole screen */
1840 static void
1841 do_nc (void)
1843 int midnight_colors[DLG_COLOR_NUM];
1844 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1845 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1846 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1847 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1849 panel_init ();
1851 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1852 "[main]", NULL, DLG_WANT_IDLE);
1854 if (view_one_file || edit_one_file)
1855 setup_dummy_mc ();
1856 else
1857 setup_mc ();
1859 /* start check display_codepage and source_codepage */
1860 check_codeset ();
1862 main_map = default_main_map;
1863 if (main_keymap && main_keymap->len > 0)
1864 main_map = (global_keymap_t *) main_keymap->data;
1866 main_x_map = default_main_x_map;
1867 if (main_x_keymap && main_x_keymap->len > 0)
1868 main_x_map = (global_keymap_t *) main_x_keymap->data;
1870 panel_map = default_panel_keymap;
1871 if (panel_keymap && panel_keymap->len > 0)
1872 panel_map = (global_keymap_t *) panel_keymap->data;
1874 input_map = default_input_keymap;
1875 if (input_keymap && input_keymap->len > 0)
1876 input_map = (global_keymap_t *) input_keymap->data;
1878 tree_map = default_tree_keymap;
1879 if (tree_keymap && tree_keymap->len > 0)
1880 tree_map = (global_keymap_t *) tree_keymap->data;
1882 help_map = default_help_keymap;
1883 if (help_keymap && help_keymap->len > 0)
1884 help_map = (global_keymap_t *) help_keymap->data;
1886 /* Check if we were invoked as an editor or file viewer */
1887 if (!mc_maybe_editor_or_viewer ()) {
1888 setup_panels_and_run_mc ();
1890 /* Program end */
1891 midnight_shutdown = 1;
1893 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1894 if (mc_args__last_wd_file && vfs_current_is_local ())
1895 last_wd_string = g_strdup (current_panel->cwd);
1897 done_mc ();
1900 destroy_dlg (midnight_dlg);
1901 panel_deinit ();
1902 current_panel = 0;
1903 done_mc_profile ();
1906 /* POSIX version. The only version we support. */
1907 static void
1908 OS_Setup (void)
1910 const char *shell_env = getenv ("SHELL");
1911 const char *mc_libdir;
1913 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1914 struct passwd *pwd;
1915 pwd = getpwuid (geteuid ());
1916 if (pwd != NULL)
1917 shell = g_strdup (pwd->pw_shell);
1918 } else
1919 shell = g_strdup (shell_env);
1921 if ((shell == NULL) || (shell[0] == '\0')) {
1922 g_free (shell);
1923 shell = g_strdup ("/bin/sh");
1926 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1927 /* and can be overriden by the MC_DATADIR environment variable */
1928 mc_libdir = getenv ("MC_DATADIR");
1929 if (mc_libdir != NULL) {
1930 mc_home = g_strdup (mc_libdir);
1931 mc_home_alt = g_strdup (SYSCONFDIR);
1932 } else {
1933 mc_home = g_strdup (SYSCONFDIR);
1934 mc_home_alt = g_strdup (DATADIR);
1937 /* This variable is used by the subshell */
1938 home_dir = getenv ("HOME");
1940 if (!home_dir)
1941 home_dir = mc_home;
1944 static void
1945 sigchld_handler_no_subshell (int sig)
1947 #ifdef __linux__
1948 int pid, status;
1950 if (!console_flag)
1951 return;
1953 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1954 the value of console_flag never changed, we could simply not install
1955 this handler at all if (!console_flag && !use_subshell). */
1957 /* That comment is no longer true. We need to wait() on a sigchld
1958 handler (that's at least what the tarfs code expects currently). */
1960 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1962 if (pid == cons_saver_pid) {
1964 if (WIFSTOPPED (status)) {
1965 /* Someone has stopped cons.saver - restart it */
1966 kill (pid, SIGCONT);
1967 } else {
1968 /* cons.saver has died - disable console saving */
1969 handle_console (CONSOLE_DONE);
1970 console_flag = 0;
1973 /* If we got here, some other child exited; ignore it */
1974 #endif /* __linux__ */
1976 (void) sig;
1979 static void
1980 init_sigchld (void)
1982 struct sigaction sigchld_action;
1984 sigchld_action.sa_handler =
1985 #ifdef HAVE_SUBSHELL_SUPPORT
1986 use_subshell ? sigchld_handler :
1987 #endif /* HAVE_SUBSHELL_SUPPORT */
1988 sigchld_handler_no_subshell;
1990 sigemptyset (&sigchld_action.sa_mask);
1992 #ifdef SA_RESTART
1993 sigchld_action.sa_flags = SA_RESTART;
1994 #else
1995 sigchld_action.sa_flags = 0;
1996 #endif /* !SA_RESTART */
1998 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1999 #ifdef HAVE_SUBSHELL_SUPPORT
2001 * This may happen on QNX Neutrino 6, where SA_RESTART
2002 * is defined but not implemented. Fallback to no subshell.
2004 use_subshell = 0;
2005 #endif /* HAVE_SUBSHELL_SUPPORT */
2009 static void
2010 mc_main__setup_by_args (int argc, char *argv[])
2012 const char *base;
2013 char *tmp;
2015 if (mc_args__nomouse)
2016 use_mouse_p = MOUSE_DISABLED;
2018 #ifdef USE_NETCODE
2019 if (mc_args__netfs_logfile != NULL) {
2020 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
2021 #ifdef WITH_SMBFS
2022 smbfs_set_debugf (mc_args__netfs_logfile);
2023 #endif /* WITH_SMBFS */
2026 #ifdef WITH_SMBFS
2027 if (mc_args__debug_level != 0)
2028 smbfs_set_debug (mc_args__debug_level);
2029 #endif /* WITH_SMBFS */
2030 #endif /* USE_NETCODE */
2032 base = x_basename (argv[0]);
2033 tmp = (argc > 0) ? argv[1] : NULL;
2035 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2036 edit_one_file = "";
2037 if (tmp) {
2039 * Check for filename:lineno, followed by an optional colon.
2040 * This format is used by many programs (especially compilers)
2041 * in error messages and warnings. It is supported so that
2042 * users can quickly copy and paste file locations.
2044 char *end = tmp + strlen (tmp), *p = end;
2045 if (p > tmp && p[-1] == ':')
2046 p--;
2047 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2048 p--;
2049 if (tmp < p && p < end && p[-1] == ':') {
2050 struct stat st;
2051 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2053 * Check that the file before the colon actually exists.
2054 * If it doesn't exist, revert to the old behavior.
2056 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2057 edit_one_file = fname;
2058 edit_one_file_start_line = atoi (p);
2059 } else {
2060 g_free (fname);
2061 goto try_plus_filename;
2063 } else {
2064 try_plus_filename:
2065 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2066 int start_line = atoi (tmp);
2067 if (start_line > 0) {
2068 char *file = (argc > 1) ? argv[2] : NULL;
2069 if (file) {
2070 tmp = file;
2071 edit_one_file_start_line = start_line;
2075 edit_one_file = g_strdup (tmp);
2078 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2079 if (tmp)
2080 view_one_file = g_strdup (tmp);
2081 else {
2082 fputs ("No arguments given to the viewer\n", stderr);
2083 exit (1);
2085 } else {
2086 /* sets the current dir and the other dir */
2087 if (tmp) {
2088 this_dir = g_strdup (tmp);
2089 tmp = (argc > 1) ? argv[2] : NULL;
2090 if (tmp)
2091 other_dir = g_strdup (tmp);
2097 main (int argc, char *argv[])
2099 struct stat s;
2100 char *mc_dir;
2101 GError *error = NULL;
2102 gboolean isInitialized;
2104 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2105 setlocale (LC_ALL, "");
2106 bindtextdomain ("mc", LOCALEDIR);
2107 textdomain ("mc");
2109 /* Set up temporary directory */
2110 mc_tmpdir ();
2112 OS_Setup ();
2114 str_init_strings (NULL);
2116 vfs_init ();
2118 #ifdef USE_INTERNAL_EDIT
2119 edit_stack_init ();
2120 #endif
2122 #ifdef HAVE_SLANG
2123 SLtt_Ignore_Beep = 1;
2124 #endif
2126 if ( !mc_args_handle (&argc, &argv, "mc"))
2127 return 1;
2129 mc_main__setup_by_args (argc,argv);
2131 /* NOTE: This has to be called before tty_init or whatever routine
2132 calls any define_sequence */
2133 init_key ();
2135 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2136 handle_console (CONSOLE_INIT);
2138 #ifdef HAVE_SUBSHELL_SUPPORT
2139 /* Don't use subshell when invoked as viewer or editor */
2140 if (edit_one_file || view_one_file)
2141 use_subshell = 0;
2143 if (use_subshell)
2144 subshell_get_console_attributes ();
2145 #endif /* HAVE_SUBSHELL_SUPPORT */
2147 /* Install the SIGCHLD handler; must be done before init_subshell() */
2148 init_sigchld ();
2150 /* We need this, since ncurses endwin () doesn't restore the signals */
2151 save_stop_handler ();
2153 /* Must be done before init_subshell, to set up the terminal size: */
2154 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2155 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2157 load_setup ();
2159 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2161 isInitialized = mc_skin_init(&error);
2163 mc_filehighlight = mc_fhl_new (TRUE);
2165 dlg_set_default_colors ();
2167 if ( ! isInitialized ) {
2168 message (D_ERROR, _("Warning"), "%s", error->message);
2169 g_error_free(error);
2170 error = NULL;
2173 /* create home directory */
2174 /* do it after the screen library initialization to show the error message */
2175 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2176 canonicalize_pathname (mc_dir);
2177 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2178 && mkdir (mc_dir, 0700) != 0)
2179 message (D_ERROR, _("Warning"),
2180 _("Cannot create %s directory"), mc_dir);
2181 g_free (mc_dir);
2183 #ifdef HAVE_SUBSHELL_SUPPORT
2184 /* Done here to ensure that the subshell doesn't */
2185 /* inherit the file descriptors opened below, etc */
2186 if (use_subshell)
2187 init_subshell ();
2189 #endif /* HAVE_SUBSHELL_SUPPORT */
2191 /* Removing this from the X code let's us type C-c */
2192 load_key_defs ();
2194 load_keymap_defs ();
2196 /* Also done after init_subshell, to save any shell init file messages */
2197 if (console_flag)
2198 handle_console (CONSOLE_SAVE);
2200 if (alternate_plus_minus)
2201 application_keypad_mode ();
2203 #ifdef HAVE_SUBSHELL_SUPPORT
2204 if (use_subshell) {
2205 prompt = strip_ctrl_codes (subshell_prompt);
2206 if (!prompt)
2207 prompt = "";
2208 } else
2209 #endif /* HAVE_SUBSHELL_SUPPORT */
2210 prompt = (geteuid () == 0) ? "# " : "$ ";
2213 /* Program main loop */
2214 if (!midnight_shutdown)
2215 do_nc ();
2217 /* Save the tree store */
2218 tree_store_save ();
2220 free_keymap_defs ();
2222 /* Virtual File System shutdown */
2223 vfs_shut ();
2225 flush_extension_file (); /* does only free memory */
2227 mc_fhl_free (&mc_filehighlight);
2228 mc_skin_deinit ();
2230 tty_shutdown ();
2232 if (console_flag && !(quit & SUBSHELL_EXIT))
2233 handle_console (CONSOLE_RESTORE);
2234 if (alternate_plus_minus)
2235 numeric_keypad_mode ();
2237 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2239 if (console_flag)
2240 handle_console (CONSOLE_DONE);
2241 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2243 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2244 && !edit_one_file && !view_one_file) {
2245 int last_wd_fd =
2246 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2247 S_IRUSR | S_IWUSR);
2249 if (last_wd_fd != -1) {
2250 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2251 close (last_wd_fd);
2254 g_free (last_wd_string);
2256 g_free (mc_home_alt);
2257 g_free (mc_home);
2258 g_free (shell);
2260 done_key ();
2261 #ifdef HAVE_CHARSET
2262 free_codepages_list ();
2263 #endif
2264 str_uninit_strings ();
2266 g_free (this_dir);
2267 g_free (other_dir);
2269 #ifdef USE_INTERNAL_EDIT
2270 edit_stack_free ();
2271 #endif
2273 return 0;