Merge branch '44_more_functionally_u7z'
[pantumic.git] / src / main.c
blob9805e403a474afa54b2b1e3cc1ad3d7eef948302
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
6 1994, 1995 Janne Kukonlehto
7 1997 Norbert Warmuth
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file main.c
24 * \brief Source: this is a main module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pwd.h> /* for username in xterm title */
42 #include "global.h"
44 #include "../src/tty/tty.h"
45 #include "../src/skin/skin.h"
46 #include "../src/tty/mouse.h"
47 #include "../src/tty/key.h" /* For init_key() */
48 #include "../src/tty/win.h" /* xterm_flag */
50 #include "../src/mcconfig/mcconfig.h"
51 #include "../src/args.h"
52 #include "../src/skin/skin.h"
53 #include "../src/filehighlight/fhl.h"
55 #include "dir.h"
56 #include "dialog.h"
57 #include "menu.h"
58 #include "panel.h"
59 #include "main.h"
60 #include "option.h"
61 #include "tree.h"
62 #include "treestore.h"
63 #include "cons.saver.h"
64 #include "subshell.h"
65 #include "setup.h" /* save_setup() */
66 #include "boxes.h" /* sort_box() */
67 #include "layout.h"
68 #include "cmd.h" /* Normal commands */
69 #include "hotlist.h"
70 #include "panelize.h"
71 #include "learn.h" /* learn_keys() */
72 #include "listmode.h"
73 #include "execute.h"
74 #include "ext.h" /* For flush_extension_file() */
75 #include "strutil.h"
76 /* Listbox for the command history feature */
77 #include "widget.h"
78 #include "command.h"
79 #include "wtools.h"
81 #include "../vfs/vfs.h" /* vfs_translate_url() */
83 #include "chmod.h"
84 #include "chown.h"
85 #include "achown.h"
87 #ifdef WITH_SMBFS
88 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
89 #endif
91 #ifdef USE_INTERNAL_EDIT
92 # include "../edit/edit.h"
93 #endif
95 #ifdef HAVE_CHARSET
96 #include "charsets.h"
97 #endif /* HAVE_CHARSET */
99 #ifdef USE_VFS
100 #include "../vfs/gc.h"
101 #endif
103 /* When the modes are active, left_panel, right_panel and tree_panel */
104 /* Point to a proper data structure. You should check with the functions */
105 /* get_current_type and get_other_type the types of the panels before using */
106 /* This pointer variables */
108 /* The structures for the panels */
109 WPanel *left_panel = NULL;
110 WPanel *right_panel = NULL;
112 mc_fhl_t *mc_filehighlight;
114 /* The pointer to the tree */
115 WTree *the_tree = NULL;
117 /* The Menubar */
118 struct WMenu *the_menubar = NULL;
120 /* Pointers to the selected and unselected panel */
121 WPanel *current_panel = NULL;
123 /* Set if the command is being run from the "Right" menu */
124 int is_right = 0;
126 /* Set when main loop should be terminated */
127 volatile int quit = 0;
129 /* Set if you want the possible completions dialog for the first time */
130 int show_all_if_ambiguous = 0;
132 /* Set when cd symlink following is desirable (bash mode) */
133 int cd_symlinks = 1;
135 /* If set then dialogs just clean the screen when refreshing, else */
136 /* they do a complete refresh, refreshing all the parts of the program */
137 int fast_refresh = 0;
139 /* If true, marking a files moves the cursor down */
140 int mark_moves_down = 1;
142 /* If true, at startup the user-menu is invoked */
143 int auto_menu = 0;
145 /* If true, then the +, - and \ keys have their special meaning only if the
146 * command line is emtpy, otherwise they behave like regular letters
148 int only_leading_plus_minus = 1;
150 int pause_after_run = pause_on_dumb_terminals;
152 /* It true saves the setup when quitting */
153 int auto_save_setup = 1;
155 #ifdef HAVE_CHARSET
157 * Don't restrict the output on the screen manager level,
158 * the translation tables take care of it.
160 #define full_eight_bits (1)
161 #define eight_bit_clean (1)
162 #else /* HAVE_CHARSET */
163 /* If true, allow characters in the range 160-255 */
164 int eight_bit_clean = 1;
166 * If true, also allow characters in the range 128-159.
167 * This is reported to break on many terminals (xterm, qansi-m).
169 int full_eight_bits = 0;
170 #endif /* !HAVE_CHARSET */
173 * If utf-8 terminal utf8_display = 1
174 * Display bits set UTF-8
177 int utf8_display = 0;
179 /* If true use the internal viewer */
180 int use_internal_view = 1;
182 /* Have we shown the fast-reload warning in the past? */
183 int fast_reload_w = 0;
185 /* Move page/item? When clicking on the top or bottom of a panel */
186 int mouse_move_pages = 1;
188 /* If true: l&r arrows are used to chdir if the input line is empty */
189 int navigate_with_arrows = 0;
191 /* If true program softkeys (HP terminals only) on startup and after every
192 command ran in the subshell to the description found in the termcap/terminfo
193 database */
194 int reset_hp_softkeys = 0;
196 /* The prompt */
197 const char *prompt = NULL;
199 /* The widget where we draw the prompt */
200 WLabel *the_prompt;
202 /* The hint bar */
203 WLabel *the_hint;
205 /* The button bar */
206 WButtonBar *the_bar;
208 /* Mouse type: GPM, xterm or none */
209 Mouse_Type use_mouse_p = MOUSE_NONE;
211 /* If on, default for "No" in delete operations */
212 int safe_delete = 0;
214 /* Controls screen clearing before an exec */
215 int clear_before_exec = 1;
217 /* Asks for confirmation before deleting a file */
218 int confirm_delete = 1;
220 /* Asks for confirmation before deleting a hotlist entry */
221 int confirm_directory_hotlist_delete = 1;
223 /* Asks for confirmation before overwriting a file */
224 int confirm_overwrite = 1;
226 /* Asks for confirmation before executing a program by pressing enter */
227 int confirm_execute = 0;
229 /* Asks for confirmation before leaving the program */
230 int confirm_exit = 1;
232 /* Asks for confirmation when using F3 to view a directory and there
233 are tagged files */
234 int confirm_view_dir = 0;
236 /* This flag indicates if the pull down menus by default drop down */
237 int drop_menus = 0;
239 /* if skip_check_codeset = 1 do not show warning about
240 * system and display codeset is different
242 int skip_check_codeset = 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 /* The value of the other directory, only used when loading the setup */
255 char *other_dir = NULL;
257 /* Only used at program boot */
258 int boot_current_is_left = 1;
260 static char *this_dir = NULL;
262 /* If this is true, then when browsing the tree the other window will
263 * automatically reload it's directory with the contents of the currently
264 * selected directory.
266 int xtree_mode = 0;
268 /* If set, then print to the given file the last directory we were at */
269 static char *last_wd_string = NULL;
270 /* Set to 1 to suppress printing the last directory */
271 static int print_last_revert = 0;
273 /* File name to view if argument was supplied */
274 const char *view_one_file = NULL;
276 /* File name to edit if argument was supplied */
277 const char *edit_one_file = NULL;
279 /* Line to start the editor on */
280 static int edit_one_file_start_line = 0;
282 /* Used so that widgets know if they are being destroyed or
283 shut down */
284 int midnight_shutdown = 0;
286 /* The user's shell */
287 char *shell = NULL;
289 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
290 char *mc_home = NULL;
292 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
293 char *mc_home_alt = NULL;
295 char cmd_buf[512];
297 /* Save current stat of directories to avoid reloading the panels */
298 /* when no modifications have taken place */
299 void
300 save_cwds_stat (void)
302 if (fast_reload) {
303 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
304 if (get_other_type () == view_listing)
305 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
309 #ifdef HAVE_SUBSHELL_SUPPORT
310 void
311 do_update_prompt (void)
313 if (update_prompt) {
314 printf ("\r\n%s", subshell_prompt);
315 fflush (stdout);
316 update_prompt = 0;
319 #endif /* HAVE_SUBSHELL_SUPPORT */
321 void
322 change_panel (void)
324 free_completions (cmdline);
325 dlg_one_down (midnight_dlg);
328 /* Stop MC main dialog and the current dialog if it exists.
329 * Needed to provide fast exit from MC viewer or editor on shell exit */
330 static void
331 stop_dialogs (void)
333 midnight_dlg->running = 0;
334 if (current_dlg) {
335 current_dlg->running = 0;
339 static int
340 quit_cmd_internal (int quiet)
342 int q = quit;
344 if (quiet || !confirm_exit) {
345 q = 1;
346 } else {
347 if (query_dialog
348 (_(" The Midnight Commander "),
349 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
350 2, _("&Yes"), _("&No")) == 0)
351 q = 1;
353 if (q) {
354 #ifdef HAVE_SUBSHELL_SUPPORT
355 if (!use_subshell)
356 stop_dialogs ();
357 else if ((q = exit_subshell ()))
358 #endif
359 stop_dialogs ();
361 if (q)
362 quit |= 1;
363 return quit;
366 static void
367 quit_cmd (void)
369 quit_cmd_internal (0);
372 void
373 quiet_quit_cmd (void)
375 print_last_revert = 1;
376 quit_cmd_internal (1);
379 /* Wrapper for do_subshell_chdir, check for availability of subshell */
380 void
381 subshell_chdir (const char *directory)
383 #ifdef HAVE_SUBSHELL_SUPPORT
384 if (use_subshell) {
385 if (vfs_current_is_local ())
386 do_subshell_chdir (directory, 0, 1);
388 #endif /* HAVE_SUBSHELL_SUPPORT */
391 void
392 directory_history_add (struct WPanel *panel, const char *dir)
394 char *tmp;
396 tmp = g_strdup (dir);
397 strip_password (tmp, 1);
399 panel->dir_history = list_append_unique (panel->dir_history, tmp);
403 * If we moved to the parent directory move the selection pointer to
404 * the old directory name; If we leave VFS dir, remove FS specificator.
406 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
408 static const char *
409 get_parent_dir_name (const char *cwd, const char *lwd)
411 const char *p;
412 if (strlen (lwd) > strlen (cwd))
413 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
414 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
415 cwd[1] == '\0'))) {
416 return (p + 1);
418 return NULL;
422 * Changes the current directory of the panel.
423 * Don't record change in the directory history.
425 static int
426 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
428 const char *directory;
429 char *olddir;
430 char temp[MC_MAXPATHLEN];
431 char *translated_url;
433 if (cd_type == cd_parse_command) {
434 while (*new_dir == ' ')
435 new_dir++;
438 olddir = g_strdup (panel->cwd);
439 new_dir = translated_url = vfs_translate_url (new_dir);
441 /* Convert *new_path to a suitable pathname, handle ~user */
443 if (cd_type == cd_parse_command) {
444 if (!strcmp (new_dir, "-")) {
445 strcpy (temp, panel->lwd);
446 new_dir = temp;
449 directory = *new_dir ? new_dir : home_dir;
451 if (mc_chdir (directory) == -1) {
452 strcpy (panel->cwd, olddir);
453 g_free (olddir);
454 g_free (translated_url);
455 return 0;
457 g_free (translated_url);
459 /* Success: save previous directory, shutdown status of previous dir */
460 strcpy (panel->lwd, olddir);
461 free_completions (cmdline);
463 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
465 vfs_release_path (olddir);
467 subshell_chdir (panel->cwd);
469 /* Reload current panel */
470 panel_clean_dir (panel);
471 panel->count =
472 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
473 panel->reverse, panel->case_sensitive,
474 panel->exec_first, panel->filter);
475 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
476 load_hint (0);
477 panel->dirty = 1;
478 update_xterm_title_path ();
480 g_free (olddir);
482 return 1;
486 * Changes the current directory of the panel.
487 * Record change in the directory history.
490 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
492 int r;
494 r = _do_panel_cd (panel, new_dir, cd_type);
495 if (r)
496 directory_history_add (panel, panel->cwd);
497 return r;
501 do_cd (const char *new_dir, enum cd_enum exact)
503 return (do_panel_cd (current_panel, new_dir, exact));
506 void
507 directory_history_next (WPanel *panel)
509 GList *nextdir;
511 nextdir = g_list_next (panel->dir_history);
513 if (!nextdir)
514 return;
516 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
517 panel->dir_history = nextdir;
520 void
521 directory_history_prev (WPanel *panel)
523 GList *prevdir;
525 prevdir = g_list_previous (panel->dir_history);
527 if (!prevdir)
528 return;
530 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
531 panel->dir_history = prevdir;
534 void
535 directory_history_list (WPanel *panel)
537 char *s;
539 if (!panel->dir_history)
540 return;
542 s = show_hist (panel->dir_history, &panel->widget);
544 if (!s)
545 return;
547 if (_do_panel_cd (panel, s, cd_exact))
548 directory_history_add (panel, panel->cwd);
549 else
550 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
551 g_free (s);
554 #ifdef HAVE_SUBSHELL_SUPPORT
556 load_prompt (int fd, void *unused)
558 (void) fd;
559 (void) unused;
561 if (!read_subshell_prompt ())
562 return 0;
564 /* Don't actually change the prompt if it's invisible */
565 if (current_dlg == midnight_dlg && command_prompt) {
566 char *tmp_prompt;
567 int prompt_len;
569 tmp_prompt = strip_ctrl_codes (subshell_prompt);
570 prompt_len = str_term_width1 (tmp_prompt);
572 /* Check for prompts too big */
573 if (COLS > 8 && prompt_len > COLS - 8) {
574 tmp_prompt[COLS - 8] = '\0';
575 prompt_len = COLS - 8;
577 prompt = tmp_prompt;
578 label_set_text (the_prompt, prompt);
579 winput_set_origin ((WInput *) cmdline, prompt_len,
580 COLS - prompt_len);
582 /* since the prompt has changed, and we are called from one of the
583 * tty_get_event channels, the prompt updating does not take place
584 * automatically: force a cursor update and a screen refresh
586 update_cursor (midnight_dlg);
587 mc_refresh ();
589 update_prompt = 1;
590 return 0;
592 #endif /* HAVE_SUBSHELL_SUPPORT */
594 static void
595 sort_cmd (void)
597 WPanel *p;
598 sortfn *sort_order;
600 if (!SELECTED_IS_PANEL)
601 return;
603 p = MENU_PANEL;
604 sort_order = sort_box (p->sort_type, &p->reverse,
605 &p->case_sensitive,
606 &p->exec_first);
608 panel_set_sort_order (p, sort_order);
612 static void
613 treebox_cmd (void)
615 char *sel_dir;
617 sel_dir = tree_box (selection (current_panel)->fname);
618 if (sel_dir) {
619 do_cd (sel_dir, cd_exact);
620 g_free (sel_dir);
624 #ifdef LISTMODE_EDITOR
625 static void
626 listmode_cmd (void)
628 char *newmode;
630 if (get_current_type () != view_listing)
631 return;
633 newmode = listmode_edit (current_panel->user_format);
634 if (!newmode)
635 return;
637 g_free (current_panel->user_format);
638 current_panel->list_type = list_user;
639 current_panel->user_format = newmode;
640 set_panel_formats (current_panel);
642 do_refresh ();
644 #endif /* LISTMODE_EDITOR */
646 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
647 static menu_entry LeftMenu[] = {
648 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
649 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
650 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
651 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
652 {' ', "", NULL_HOTKEY, 0},
653 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
654 {' ', "", NULL_HOTKEY, 0},
655 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
656 #ifdef HAVE_CHARSET
657 {' ', "",NULL_HOTKEY, 0},
658 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
659 #endif
660 #ifdef USE_NETCODE
661 {' ', "", NULL_HOTKEY, 0},
662 #ifdef ENABLE_VFS_MCFS
663 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
664 #endif
665 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
666 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
667 #ifdef WITH_SMBFS
668 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
669 #endif
670 #endif
671 {' ', "", NULL_HOTKEY, 0},
672 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
675 static menu_entry RightMenu[] = {
676 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
677 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
678 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
679 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
680 {' ', "", NULL_HOTKEY, 0},
681 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
682 {' ', "", NULL_HOTKEY, 0},
683 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
684 #ifdef HAVE_CHARSET
685 {' ', "",NULL_HOTKEY, 0},
686 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
687 #endif
688 #ifdef USE_NETCODE
689 {' ', "", NULL_HOTKEY, 0},
690 #ifdef ENABLE_VFS_MCFS
691 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
692 #endif
693 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
694 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
695 #ifdef WITH_SMBFS
696 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
697 #endif
698 #endif
699 {' ', "", NULL_HOTKEY, 0},
700 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
703 static menu_entry FileMenu[] = {
704 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
705 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
706 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
707 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
708 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
709 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
710 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
711 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
712 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
713 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
714 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
715 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
716 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
717 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
718 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
719 {' ', "", NULL_HOTKEY, 0},
720 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
721 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
722 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
723 {' ', "", NULL_HOTKEY, 0},
724 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
727 static menu_entry CmdMenu[] = {
728 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
729 /* I know, I'm lazy, but the tree widget when it's not running
730 * as a panel still has some problems, I have not yet finished
731 * the WTree widget port, sorry.
733 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
734 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
735 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
736 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
737 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
738 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
739 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
740 {' ', "", NULL_HOTKEY, 0},
741 {' ', N_("Command &history M-h"), NULL_HOTKEY, history_cmd},
742 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
743 #ifdef USE_VFS
744 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
745 #endif
746 #ifdef WITH_BACKGROUND
747 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
748 #endif
749 {' ', "", NULL_HOTKEY, 0},
750 #ifdef USE_EXT2FSLIB
751 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
752 #endif
753 #ifdef LISTMODE_EDITOR
754 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
755 #endif
756 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
757 {' ', "", NULL_HOTKEY, 0},
758 #endif
759 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
760 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd},
761 {' ', N_("Edit &highlighting group file"), NULL_HOTKEY, edit_fhl_cmd}
764 /* Must keep in sync with the constants in menu_cmd */
765 static menu_entry OptMenu[] = {
766 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
767 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
768 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
769 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
770 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
771 #ifdef USE_VFS
772 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
773 #endif /* !USE_VFS */
774 {' ', "", NULL_HOTKEY, 0},
775 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
778 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
780 static Menu *MenuBar[5];
782 void
783 init_menu (void)
785 MenuBar[0] =
786 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
787 LeftMenu, menu_entries (LeftMenu),
788 "[Left and Right Menus]");
789 MenuBar[1] =
790 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
791 "[File Menu]");
792 MenuBar[2] =
793 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
794 "[Command Menu]");
795 MenuBar[3] =
796 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
797 "[Options Menu]");
798 MenuBar[4] =
799 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
800 RightMenu, menu_entries (RightMenu),
801 "[Left and Right Menus]");
804 void
805 done_menu (void)
807 int i;
809 for (i = 0; i < 5; i++) {
810 destroy_menu (MenuBar[i]);
814 static void
815 menu_last_selected_cmd (void)
817 the_menubar->active = 1;
818 the_menubar->dropped = drop_menus;
819 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
820 dlg_select_widget (the_menubar);
823 static void
824 menu_cmd (void)
826 if (the_menubar->active)
827 return;
829 if ((get_current_index () == 0) ^ (!current_panel->active))
830 the_menubar->selected = 0;
831 else
832 the_menubar->selected = 4;
833 menu_last_selected_cmd ();
836 /* Flag toggling functions */
837 void
838 toggle_fast_reload (void)
840 fast_reload = !fast_reload;
841 if (fast_reload_w == 0 && fast_reload) {
842 message (D_NORMAL, _(" Information "),
844 (" Using the fast reload option may not reflect the exact \n"
845 " directory contents. In this case you'll need to do a \n"
846 " manual reload of the directory. See the man page for \n"
847 " the details. "));
848 fast_reload_w = 1;
852 void
853 toggle_mix_all_files (void)
855 mix_all_files = !mix_all_files;
856 update_panels (UP_RELOAD, UP_KEEPSEL);
859 void
860 toggle_show_backup (void)
862 show_backups = !show_backups;
863 update_panels (UP_RELOAD, UP_KEEPSEL);
866 void
867 toggle_show_hidden (void)
869 show_dot_files = !show_dot_files;
870 update_panels (UP_RELOAD, UP_KEEPSEL);
873 void
874 toggle_kilobyte_si (void)
876 kilobyte_si = !kilobyte_si;
877 update_panels (UP_RELOAD, UP_KEEPSEL);
881 * Just a hack for allowing url-like pathnames to be accepted from the
882 * command line.
884 static void
885 translated_mc_chdir (char *dir)
887 char *newdir;
889 newdir = vfs_translate_url (dir);
890 mc_chdir (newdir);
891 g_free (newdir);
894 static void
895 create_panels (void)
897 int current_index;
898 int other_index;
899 int current_mode;
900 int other_mode;
901 char original_dir[1024];
903 original_dir[0] = 0;
905 if (boot_current_is_left) {
906 current_index = 0;
907 other_index = 1;
908 current_mode = startup_left_mode;
909 other_mode = startup_right_mode;
910 } else {
911 current_index = 1;
912 other_index = 0;
913 current_mode = startup_right_mode;
914 other_mode = startup_left_mode;
916 /* Creates the left panel */
917 if (this_dir) {
918 if (other_dir) {
919 /* Ok, user has specified two dirs, save the original one,
920 * since we may not be able to chdir to the proper
921 * second directory later
923 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
925 translated_mc_chdir (this_dir);
927 set_display_type (current_index, current_mode);
929 /* The other panel */
930 if (other_dir) {
931 if (original_dir[0])
932 translated_mc_chdir (original_dir);
933 translated_mc_chdir (other_dir);
935 set_display_type (other_index, other_mode);
937 if (startup_left_mode == view_listing) {
938 current_panel = left_panel;
939 } else {
940 if (right_panel)
941 current_panel = right_panel;
942 else
943 current_panel = left_panel;
946 /* Create the nice widgets */
947 cmdline = command_new (0, 0, 0);
948 the_prompt = label_new (0, 0, prompt);
949 the_prompt->transparent = 1;
950 the_bar = buttonbar_new (keybar_visible);
952 the_hint = label_new (0, 0, 0);
953 the_hint->transparent = 1;
954 the_hint->auto_adjust_cols = 0;
955 the_hint->widget.cols = COLS;
957 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
960 static void
961 copy_current_pathname (void)
963 char *cwd_path;
964 if (!command_prompt)
965 return;
967 cwd_path = remove_encoding_from_path (current_panel->cwd);
968 command_insert (cmdline, cwd_path, 0);
970 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
971 command_insert (cmdline, PATH_SEP_STR, 0);
972 g_free (cwd_path);
975 static void
976 copy_other_pathname (void)
978 char *cwd_path;
980 if (get_other_type () != view_listing)
981 return;
983 if (!command_prompt)
984 return;
986 cwd_path = remove_encoding_from_path (other_panel->cwd);
987 command_insert (cmdline, cwd_path, 0);
989 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
990 command_insert (cmdline, PATH_SEP_STR, 0);
991 g_free (cwd_path);
994 static void
995 copy_readlink (WPanel *panel)
997 if (!command_prompt)
998 return;
999 if (S_ISLNK (selection (panel)->st.st_mode)) {
1000 char buffer[MC_MAXPATHLEN];
1001 char *p =
1002 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1003 int i;
1005 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1006 g_free (p);
1007 if (i > 0) {
1008 buffer[i] = 0;
1009 command_insert (cmdline, buffer, 1);
1014 static void
1015 copy_current_readlink (void)
1017 copy_readlink (current_panel);
1020 static void
1021 copy_other_readlink (void)
1023 if (get_other_type () != view_listing)
1024 return;
1025 copy_readlink (other_panel);
1028 /* Insert the selected file name into the input line */
1029 static void
1030 copy_prog_name (void)
1032 char *tmp;
1033 if (!command_prompt)
1034 return;
1036 if (get_current_type () == view_tree) {
1037 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1038 tmp = tree_selected_name (tree);
1039 } else
1040 tmp = selection (current_panel)->fname;
1042 command_insert (cmdline, tmp, 1);
1045 static void
1046 copy_tagged (WPanel *panel)
1048 int i;
1050 if (!command_prompt)
1051 return;
1052 input_disable_update (cmdline);
1053 if (panel->marked) {
1054 for (i = 0; i < panel->count; i++) {
1055 if (panel->dir.list[i].f.marked)
1056 command_insert (cmdline, panel->dir.list[i].fname, 1);
1058 } else {
1059 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1062 input_enable_update (cmdline);
1065 static void
1066 copy_current_tagged (void)
1068 copy_tagged (current_panel);
1071 static void
1072 copy_other_tagged (void)
1074 if (get_other_type () != view_listing)
1075 return;
1076 copy_tagged (other_panel);
1079 static void
1080 init_labels (void)
1082 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1083 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1084 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1085 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1088 static const key_map ctl_x_map[] = {
1089 {XCTRL ('c'), quit_cmd},
1090 {'d', compare_dirs_cmd},
1091 #ifdef USE_VFS
1092 {'a', reselect_vfs},
1093 #endif /* USE_VFS */
1094 {'p', copy_current_pathname},
1095 {XCTRL ('p'), copy_other_pathname},
1096 {'t', copy_current_tagged},
1097 {XCTRL ('t'), copy_other_tagged},
1098 {'c', chmod_cmd},
1099 {'o', chown_cmd},
1100 {'r', copy_current_readlink},
1101 {XCTRL ('r'), copy_other_readlink},
1102 {'l', link_cmd},
1103 {'s', symlink_cmd},
1104 {XCTRL ('s'), edit_symlink_cmd},
1105 {'i', info_cmd_no_menu},
1106 {'q', quick_cmd_no_menu},
1107 {'h', add2hotlist_cmd},
1108 {'!', external_panelize},
1109 #ifdef WITH_BACKGROUND
1110 {'j', jobs_cmd},
1111 #endif /* WITH_BACKGROUND */
1112 {0, 0}
1115 static int ctl_x_map_enabled = 0;
1117 static void
1118 ctl_x_cmd (void)
1120 ctl_x_map_enabled = 1;
1123 static void
1124 nothing (void)
1128 static const key_map default_map[] = {
1129 {KEY_F (19), menu_last_selected_cmd},
1130 {KEY_F (20), quiet_quit_cmd},
1132 {XCTRL ('@'), smart_dirsize_cmd},
1134 /* Copy useful information to the command line */
1135 {ALT ('a'), copy_current_pathname},
1136 {ALT ('A'), copy_other_pathname},
1138 {ALT ('c'), quick_cd_cmd},
1140 /* To access the directory hotlist */
1141 {XCTRL ('\\'), quick_chdir_cmd},
1143 /* Suspend */
1144 {XCTRL ('z'), suspend_cmd},
1146 /* The filtered view command */
1147 {ALT ('!'), filtered_view_cmd},
1149 /* Find file */
1150 {ALT ('?'), find_cmd},
1152 /* Panel refresh */
1153 {XCTRL ('r'), reread_cmd},
1155 /* Toggle listing between long, user defined and full formats */
1156 {ALT ('t'), toggle_listing_cmd},
1158 /* Swap panels */
1159 {XCTRL ('u'), swap_cmd},
1161 /* View output */
1162 {XCTRL ('o'), view_other_cmd},
1164 /* Control-X keybindings */
1165 {XCTRL ('x'), ctl_x_cmd},
1167 /* Show/hide hidden files */
1168 {ALT ('.'), toggle_show_hidden},
1170 /* Trap dlg's exit commands */
1171 {ESC_CHAR, nothing},
1172 {XCTRL ('c'), nothing},
1173 {XCTRL ('g'), nothing},
1174 {0, 0},
1177 static void
1178 setup_pre (void)
1180 /* Call all the inits */
1182 #ifdef HAVE_SLANG
1183 tty_display_8bit (full_eight_bits != 0);
1184 #else
1185 tty_display_8bit (eight_bit_clean != 0);
1186 #endif
1189 static void
1190 init_xterm_support (void)
1192 const char *termvalue;
1194 termvalue = getenv ("TERM");
1195 if (!termvalue || !(*termvalue)) {
1196 fputs (_("The TERM environment variable is unset!\n"), stderr);
1197 exit (1);
1200 /* Check mouse capabilities */
1201 xmouse_seq = tty_tgetstr ("Km");
1203 if (strcmp (termvalue, "cygwin") == 0) {
1204 mc_args__force_xterm = 1;
1205 use_mouse_p = MOUSE_DISABLED;
1208 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1209 || strncmp (termvalue, "konsole", 7) == 0
1210 || strncmp (termvalue, "rxvt", 4) == 0
1211 || strcmp (termvalue, "Eterm") == 0
1212 || strcmp (termvalue, "dtterm") == 0) {
1213 xterm_flag = 1;
1215 /* Default to the standard xterm sequence */
1216 if (!xmouse_seq) {
1217 xmouse_seq = ESC_STR "[M";
1220 /* Enable mouse unless explicitly disabled by --nomouse */
1221 if (use_mouse_p != MOUSE_DISABLED) {
1222 const char *color_term = getenv ("COLORTERM");
1223 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1224 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1225 strcmp (termvalue, "Eterm") == 0) {
1226 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1227 } else {
1228 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1234 static void
1235 setup_mc (void)
1237 setup_pre ();
1238 init_menu ();
1239 create_panels ();
1240 setup_panels ();
1242 #ifdef HAVE_SUBSHELL_SUPPORT
1243 if (use_subshell)
1244 add_select_channel (subshell_pty, load_prompt, 0);
1245 #endif /* !HAVE_SUBSHELL_SUPPORT */
1247 tty_setup_sigwinch (sigwinch_handler);
1249 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1251 init_xterm_support ();
1252 init_mouse ();
1255 static void
1256 setup_dummy_mc (void)
1258 char d[MC_MAXPATHLEN];
1260 mc_get_current_wd (d, MC_MAXPATHLEN);
1261 setup_mc ();
1262 mc_chdir (d);
1265 static void check_codeset()
1267 const char *_system_codepage = NULL;
1268 #ifdef HAVE_CHARSET
1269 const char *_source_codepage = NULL;
1270 const char *_display_codepage = NULL;
1271 int profile_changed = 0;
1273 #define CONFY 16
1274 #define CONFX 54
1276 if ( !skip_check_codeset ) {
1278 QuickWidget ecs_widgets [] = {
1279 QUICK_BUTTON (4, 6, 13, CONFY, N_("&Skip"), B_EXIT, NULL),
1280 QUICK_BUTTON (1, 11, 13, CONFY, N_("&Fix it"), B_ENTER, NULL),
1281 QUICK_CHECKBOX (1, 13, 11, CONFY, N_("don't ask again"), &skip_check_codeset),
1282 QUICK_LABEL (2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
1283 "or source codeset (in mcedit ctrl-t) \n"
1284 "does not match one set via locale. \n"
1285 "Set correct codeset manually or press <<Fix it>> \n"
1286 "to set locale default.\n\n"
1287 "Or set \'don't ask again\' and press <<Skip>>")),
1288 QUICK_END
1291 QuickDialog ecs =
1293 CONFX, CONFY, -1, -1, N_(" Confirmation "),
1294 "[Confirmation]", ecs_widgets, FALSE
1298 _system_codepage = str_detect_termencoding();
1299 _source_codepage = get_codepage_id (source_codepage);
1300 _display_codepage = get_codepage_id (display_codepage);
1301 if ( (strcmp (_system_codepage, _display_codepage)) ||
1302 (strcmp (_system_codepage, _source_codepage)) ) {
1303 if (quick_dialog (&ecs) == B_ENTER){
1304 display_codepage = get_codepage_index (_system_codepage);
1305 cp_display = get_codepage_id (display_codepage);
1306 if ( !strcmp (cp_display, _system_codepage)) {
1307 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1308 mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
1309 display_codepage = get_codepage_index ( cp_display );
1310 utf8_display = str_isutf8 (_system_codepage);
1311 source_codepage = display_codepage;
1312 cp_source = cp_display;
1313 profile_changed = 1;
1314 } else {
1315 utf8_display = str_isutf8 (_system_codepage);
1316 cp_source = cp_display = _system_codepage;
1318 } else {
1319 if ( skip_check_codeset ) {
1320 mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
1321 profile_changed = 1;
1325 if ( profile_changed )
1326 save_configure ();
1328 #else /* HAVE_CHARSET */
1329 _system_codepage = str_detect_termencoding();
1330 utf8_display = str_isutf8 (_system_codepage);
1331 #endif /* HAVE_CHARSET */
1334 static void
1335 done_screen (void)
1337 if (!(quit & SUBSHELL_EXIT))
1338 clr_scr ();
1339 tty_reset_shell_mode ();
1340 tty_noraw_mode ();
1341 tty_keypad (FALSE);
1342 tty_colors_done ();
1345 static void
1346 done_mc (void)
1348 disable_mouse ();
1350 done_menu ();
1352 /* Setup shutdown
1354 * We sync the profiles since the hotlist may have changed, while
1355 * we only change the setup data if we have the auto save feature set
1358 if (auto_save_setup)
1359 save_setup (); /* does also call save_hotlist */
1360 else {
1361 save_hotlist ();
1362 save_panel_types ();
1364 done_screen ();
1365 vfs_add_current_stamps ();
1368 /* This should be called after destroy_dlg since panel widgets
1369 * save their state on the profiles
1371 static void
1372 done_mc_profile (void)
1374 done_setup ();
1377 static cb_ret_t
1378 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1380 int i;
1382 switch (msg) {
1384 case DLG_IDLE:
1385 /* We only need the first idle event */
1386 set_idle_proc (h, 0);
1387 if (auto_menu) {
1388 user_file_menu_cmd ();
1390 return MSG_HANDLED;
1392 case DLG_KEY:
1393 if (ctl_x_map_enabled) {
1394 ctl_x_map_enabled = 0;
1395 for (i = 0; ctl_x_map[i].key_code; i++)
1396 if (parm == ctl_x_map[i].key_code) {
1397 (*ctl_x_map[i].fn) ();
1398 return MSG_HANDLED;
1402 /* FIXME: should handle all menu shortcuts before this point */
1403 if (the_menubar->active)
1404 return MSG_NOT_HANDLED;
1406 if (parm == KEY_F (10)) {
1407 quit_cmd ();
1408 return MSG_HANDLED;
1411 if (parm == '\t')
1412 free_completions (cmdline);
1414 if (parm == '\n') {
1415 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1416 cmdline->buffer[i] == '\t'); i++);
1417 if (cmdline->buffer[i]) {
1418 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1419 return MSG_HANDLED;
1421 stuff (cmdline, "", 0);
1422 cmdline->point = 0;
1425 /* Ctrl-Enter and Alt-Enter */
1426 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1427 && (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')) {
1434 copy_current_pathname ();
1435 copy_prog_name ();
1436 return MSG_HANDLED;
1439 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1440 && !quote && !current_panel->searching) {
1441 if (!only_leading_plus_minus) {
1442 /* Special treatement, since the input line will eat them */
1443 if (parm == '+') {
1444 select_cmd ();
1445 return MSG_HANDLED;
1448 if (parm == '\\' || parm == '-') {
1449 unselect_cmd ();
1450 return MSG_HANDLED;
1453 if (parm == '*') {
1454 reverse_selection_cmd ();
1455 return MSG_HANDLED;
1457 } else if (!command_prompt || !cmdline->buffer[0]) {
1458 /* Special treatement '+', '-', '\', '*' only when this is
1459 * first char on input line
1462 if (parm == '+') {
1463 select_cmd ();
1464 return MSG_HANDLED;
1467 if (parm == '\\' || parm == '-') {
1468 unselect_cmd ();
1469 return MSG_HANDLED;
1472 if (parm == '*') {
1473 reverse_selection_cmd ();
1474 return MSG_HANDLED;
1478 return MSG_NOT_HANDLED;
1480 case DLG_HOTKEY_HANDLED:
1481 if ((get_current_type () == view_listing) && current_panel->searching) {
1482 current_panel->searching = 0;
1483 current_panel->dirty = 1;
1485 return MSG_HANDLED;
1487 case DLG_UNHANDLED_KEY:
1488 if (command_prompt) {
1489 int v;
1491 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1492 if (v)
1493 return v;
1495 if (ctl_x_map_enabled) {
1496 ctl_x_map_enabled = 0;
1497 for (i = 0; ctl_x_map[i].key_code; i++)
1498 if (parm == ctl_x_map[i].key_code) {
1499 (*ctl_x_map[i].fn) ();
1500 return MSG_HANDLED;
1502 } else {
1503 for (i = 0; default_map[i].key_code; i++) {
1504 if (parm == default_map[i].key_code) {
1505 (*default_map[i].fn) ();
1506 return MSG_HANDLED;
1510 return MSG_NOT_HANDLED;
1512 case DLG_DRAW:
1513 load_hint (1);
1514 /* We handle the special case of the output lines */
1515 if (console_flag && output_lines)
1516 show_console_contents (output_start_y,
1517 LINES - output_lines - keybar_visible -
1518 1, LINES - keybar_visible - 1);
1519 return MSG_HANDLED;
1521 case DLG_POST_KEY:
1522 if (!the_menubar->active)
1523 update_dirty_panels ();
1524 return MSG_HANDLED;
1526 default:
1527 return default_dlg_callback (h, msg, parm);
1531 /* Show current directory in the xterm title */
1532 void
1533 update_xterm_title_path (void)
1535 const char *path;
1536 char host[BUF_TINY];
1537 char *p;
1538 struct passwd *pw = NULL;
1539 char *login = NULL;
1540 int res = 0;
1541 if (xterm_flag && xterm_title) {
1542 path = strip_home_and_password (current_panel->cwd);
1543 res = gethostname(host, sizeof (host));
1544 if ( res ) { /* On success, res = 0 */
1545 host[0] = '\0';
1546 } else {
1547 host[sizeof (host) - 1] = '\0';
1549 pw = getpwuid(getuid());
1550 if ( pw ) {
1551 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1552 } else {
1553 login = g_strdup (host);
1555 p = g_strdup_printf ("mc [%s]:%s", login, path);
1556 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1557 g_free (login);
1558 g_free (p);
1559 if (!alternate_plus_minus)
1560 numeric_keypad_mode ();
1561 fflush (stdout);
1566 * Load new hint and display it.
1567 * IF force is not 0, ignore the timeout.
1569 void
1570 load_hint (int force)
1572 char *hint;
1574 if (!the_hint->widget.parent)
1575 return;
1577 if (!message_visible) {
1578 label_set_text (the_hint, NULL);
1579 return;
1582 hint = get_random_hint (force);
1584 if (hint != NULL) {
1585 if (*hint)
1586 set_hintbar (hint);
1587 g_free (hint);
1588 } else {
1589 char text[BUF_SMALL];
1591 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1592 VERSION);
1593 set_hintbar (text);
1597 static void
1598 setup_panels_and_run_mc (void)
1600 add_widget (midnight_dlg, the_menubar);
1601 add_widget (midnight_dlg, get_panel_widget (0));
1602 add_widget (midnight_dlg, get_panel_widget (1));
1603 add_widget (midnight_dlg, the_hint);
1604 add_widget (midnight_dlg, cmdline);
1605 add_widget (midnight_dlg, the_prompt);
1606 add_widget (midnight_dlg, the_bar);
1607 init_labels ();
1609 if (boot_current_is_left)
1610 dlg_select_widget (get_panel_widget (0));
1611 else
1612 dlg_select_widget (get_panel_widget (1));
1614 /* Run the Midnight Commander if no file was specified in the command line */
1615 run_dlg (midnight_dlg);
1618 /* result must be free'd (I think this should go in util.c) */
1619 static char *
1620 prepend_cwd_on_local (const char *filename)
1622 char *d;
1623 int l;
1625 if (vfs_file_is_local (filename)) {
1626 if (*filename == PATH_SEP) /* an absolute pathname */
1627 return g_strdup (filename);
1628 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1629 mc_get_current_wd (d, MC_MAXPATHLEN);
1630 l = strlen (d);
1631 d[l++] = PATH_SEP;
1632 strcpy (d + l, filename);
1633 canonicalize_pathname (d);
1634 return d;
1635 } else
1636 return g_strdup (filename);
1639 static int
1640 mc_maybe_editor_or_viewer (void)
1642 if (!(view_one_file || edit_one_file))
1643 return 0;
1645 /* Invoke the internal view/edit routine with:
1646 * the default processing and forcing the internal viewer/editor
1648 if (view_one_file) {
1649 char *path = NULL;
1650 path = prepend_cwd_on_local (view_one_file);
1651 view_file (path, 0, 1);
1652 g_free (path);
1654 #ifdef USE_INTERNAL_EDIT
1655 else {
1656 edit_file (edit_one_file, edit_one_file_start_line);
1658 #endif /* USE_INTERNAL_EDIT */
1659 midnight_shutdown = 1;
1660 done_mc ();
1661 return 1;
1664 /* Run the main dialog that occupies the whole screen */
1665 static void
1666 do_nc (void)
1668 int midnight_colors[DLG_COLOR_NUM];
1669 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1670 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1671 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1672 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1674 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1675 "[main]", NULL, DLG_WANT_IDLE);
1677 if (view_one_file || edit_one_file)
1678 setup_dummy_mc ();
1679 else
1680 setup_mc ();
1682 /* start check display_codepage and source_codepage */
1683 check_codeset();
1685 /* Check if we were invoked as an editor or file viewer */
1686 if (!mc_maybe_editor_or_viewer ()) {
1687 setup_panels_and_run_mc ();
1689 /* Program end */
1690 midnight_shutdown = 1;
1692 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1693 if (mc_args__last_wd_file && vfs_current_is_local ()) {
1694 last_wd_string = g_strdup (current_panel->cwd);
1696 done_mc ();
1698 destroy_dlg (midnight_dlg);
1699 current_panel = 0;
1700 done_mc_profile ();
1703 /* POSIX version. The only version we support. */
1704 static void
1705 OS_Setup (void)
1707 const char *shell_env = getenv ("SHELL");
1708 const char *mc_libdir;
1710 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1711 struct passwd *pwd;
1712 pwd = getpwuid (geteuid ());
1713 if (pwd != NULL)
1714 shell = g_strdup (pwd->pw_shell);
1715 } else
1716 shell = g_strdup (shell_env);
1718 if ((shell == NULL) || (shell[0] == '\0')) {
1719 g_free (shell);
1720 shell = g_strdup ("/bin/sh");
1723 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1724 /* and can be overriden by the MC_DATADIR environment variable */
1725 mc_libdir = getenv ("MC_DATADIR");
1726 if (mc_libdir != NULL) {
1727 mc_home = g_strdup (mc_libdir);
1728 mc_home_alt = g_strdup (SYSCONFDIR);
1729 } else {
1730 mc_home = g_strdup (SYSCONFDIR);
1731 mc_home_alt = g_strdup (DATADIR);
1734 /* This variable is used by the subshell */
1735 home_dir = getenv ("HOME");
1737 if (!home_dir)
1738 home_dir = mc_home;
1742 static void
1743 sigchld_handler_no_subshell (int sig)
1745 #ifdef __linux__
1746 int pid, status;
1748 if (!console_flag)
1749 return;
1751 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1752 the value of console_flag never changed, we could simply not install
1753 this handler at all if (!console_flag && !use_subshell). */
1755 /* That comment is no longer true. We need to wait() on a sigchld
1756 handler (that's at least what the tarfs code expects currently). */
1758 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1760 if (pid == cons_saver_pid) {
1762 if (WIFSTOPPED (status)) {
1763 /* Someone has stopped cons.saver - restart it */
1764 kill (pid, SIGCONT);
1765 } else {
1766 /* cons.saver has died - disable console saving */
1767 handle_console (CONSOLE_DONE);
1768 console_flag = 0;
1771 /* If we got here, some other child exited; ignore it */
1772 #endif /* __linux__ */
1774 (void) sig;
1777 static void
1778 init_sigchld (void)
1780 struct sigaction sigchld_action;
1782 sigchld_action.sa_handler =
1783 #ifdef HAVE_SUBSHELL_SUPPORT
1784 use_subshell ? sigchld_handler :
1785 #endif /* HAVE_SUBSHELL_SUPPORT */
1786 sigchld_handler_no_subshell;
1788 sigemptyset (&sigchld_action.sa_mask);
1790 #ifdef SA_RESTART
1791 sigchld_action.sa_flags = SA_RESTART;
1792 #else
1793 sigchld_action.sa_flags = 0;
1794 #endif /* !SA_RESTART */
1796 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1797 #ifdef HAVE_SUBSHELL_SUPPORT
1799 * This may happen on QNX Neutrino 6, where SA_RESTART
1800 * is defined but not implemented. Fallback to no subshell.
1802 use_subshell = 0;
1803 #endif /* HAVE_SUBSHELL_SUPPORT */
1807 static void
1808 mc_main__setup_by_args(int argc, char *argv[])
1810 const char *base;
1811 char *tmp;
1813 if (mc_args__nomouse)
1814 use_mouse_p = MOUSE_DISABLED;
1816 #ifdef USE_NETCODE
1817 if (mc_args__netfs_logfile != NULL)
1819 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
1820 #ifdef WITH_SMBFS
1821 smbfs_set_debugf (mc_args__netfs_logfile);
1822 #endif /* WITH_SMBFS */
1825 #ifdef WITH_SMBFS
1826 if (mc_args__debug_level != 0)
1828 smbfs_set_debug (mc_args__debug_level);
1830 #endif /* WITH_SMBFS */
1831 #endif /* USE_NETCODE */
1834 base = x_basename (argv[0]);
1835 tmp = (argc > 0)? argv[1] : NULL;
1838 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
1839 edit_one_file = "";
1840 if (tmp) {
1842 * Check for filename:lineno, followed by an optional colon.
1843 * This format is used by many programs (especially compilers)
1844 * in error messages and warnings. It is supported so that
1845 * users can quickly copy and paste file locations.
1847 char *end = tmp + strlen (tmp), *p = end;
1848 if (p > tmp && p[-1] == ':')
1849 p--;
1850 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
1851 p--;
1852 if (tmp < p && p < end && p[-1] == ':') {
1853 struct stat st;
1854 gchar *fname = g_strndup (tmp, p - 1 - tmp);
1856 * Check that the file before the colon actually exists.
1857 * If it doesn't exist, revert to the old behavior.
1859 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
1860 edit_one_file = fname;
1861 edit_one_file_start_line = atoi (p);
1862 } else {
1863 g_free (fname);
1864 goto try_plus_filename;
1866 } else {
1867 try_plus_filename:
1868 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
1869 int start_line = atoi (tmp);
1870 if (start_line > 0) {
1871 char *file = (argc > 1) ? argv[2] : NULL;
1872 if (file) {
1873 tmp = file;
1874 edit_one_file_start_line = start_line;
1878 edit_one_file = g_strdup (tmp);
1881 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
1882 if (tmp)
1883 view_one_file = g_strdup (tmp);
1884 else {
1885 fputs ("No arguments given to the viewer\n", stderr);
1886 exit (1);
1888 } else {
1889 /* sets the current dir and the other dir */
1890 if (tmp) {
1891 this_dir = g_strdup (tmp);
1892 tmp = (argc > 1) ? argv[2] : NULL;
1893 if (tmp)
1894 other_dir = g_strdup (tmp);
1902 main (int argc, char *argv[])
1904 struct stat s;
1905 char *mc_dir;
1907 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
1908 setlocale (LC_ALL, "");
1909 bindtextdomain ("mc", LOCALEDIR);
1910 textdomain ("mc");
1912 /* Set up temporary directory */
1913 mc_tmpdir ();
1915 OS_Setup ();
1917 str_init_strings (NULL);
1919 vfs_init ();
1921 #ifdef USE_INTERNAL_EDIT
1922 edit_stack_init ();
1923 #endif
1925 #ifdef HAVE_SLANG
1926 SLtt_Ignore_Beep = 1;
1927 #endif
1929 if ( !mc_args_handle (&argc, &argv, "mc"))
1930 return 1;
1931 mc_main__setup_by_args(argc,argv);
1934 /* NOTE: This has to be called before tty_init or whatever routine
1935 calls any define_sequence */
1936 init_key ();
1938 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
1939 handle_console (CONSOLE_INIT);
1941 #ifdef HAVE_SUBSHELL_SUPPORT
1942 /* Don't use subshell when invoked as viewer or editor */
1943 if (edit_one_file || view_one_file)
1944 use_subshell = 0;
1946 if (use_subshell)
1947 subshell_get_console_attributes ();
1948 #endif /* HAVE_SUBSHELL_SUPPORT */
1950 /* Install the SIGCHLD handler; must be done before init_subshell() */
1951 init_sigchld ();
1953 /* We need this, since ncurses endwin () doesn't restore the signals */
1954 save_stop_handler ();
1956 /* Must be done before init_subshell, to set up the terminal size: */
1957 /* FIXME: Should be removed and LINES and COLS computed on subshell */
1958 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
1960 load_setup ();
1962 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
1964 mc_skin_init();
1965 mc_filehighlight = mc_fhl_new (TRUE);
1967 dlg_set_default_colors ();
1969 /* create home directory */
1970 /* do it after the screen library initialization to show the error message */
1971 mc_dir = concat_dir_and_file (home_dir, MC_BASE);
1972 canonicalize_pathname (mc_dir);
1973 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
1974 && mkdir (mc_dir, 0700) != 0)
1975 message (D_ERROR, _("Warning"),
1976 _("Cannot create %s directory"), mc_dir);
1977 g_free (mc_dir);
1979 #ifdef HAVE_SUBSHELL_SUPPORT
1980 /* Done here to ensure that the subshell doesn't */
1981 /* inherit the file descriptors opened below, etc */
1982 if (use_subshell)
1983 init_subshell ();
1985 #endif /* HAVE_SUBSHELL_SUPPORT */
1987 /* Removing this from the X code let's us type C-c */
1988 load_key_defs ();
1990 /* Also done after init_subshell, to save any shell init file messages */
1991 if (console_flag)
1992 handle_console (CONSOLE_SAVE);
1994 if (alternate_plus_minus)
1995 application_keypad_mode ();
1997 #ifdef HAVE_SUBSHELL_SUPPORT
1998 if (use_subshell) {
1999 prompt = strip_ctrl_codes (subshell_prompt);
2000 if (!prompt)
2001 prompt = "";
2002 } else
2003 #endif /* HAVE_SUBSHELL_SUPPORT */
2004 prompt = (geteuid () == 0) ? "# " : "$ ";
2007 /* Program main loop */
2008 if (!midnight_shutdown)
2009 do_nc ();
2011 /* Save the tree store */
2012 tree_store_save ();
2014 /* Virtual File System shutdown */
2015 vfs_shut ();
2017 flush_extension_file (); /* does only free memory */
2019 mc_fhl_free (&mc_filehighlight);
2020 mc_skin_deinit();
2022 tty_shutdown ();
2024 if (console_flag && !(quit & SUBSHELL_EXIT))
2025 handle_console (CONSOLE_RESTORE);
2026 if (alternate_plus_minus)
2027 numeric_keypad_mode ();
2029 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2031 if (console_flag)
2032 handle_console (CONSOLE_DONE);
2033 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2035 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2036 && !edit_one_file && !view_one_file) {
2037 int last_wd_fd =
2038 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2039 S_IRUSR | S_IWUSR);
2041 if (last_wd_fd != -1) {
2042 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2043 close (last_wd_fd);
2046 g_free (last_wd_string);
2048 g_free (mc_home_alt);
2049 g_free (mc_home);
2050 g_free (shell);
2052 done_key ();
2053 #ifdef HAVE_CHARSET
2054 free_codepages_list ();
2055 #endif
2056 str_uninit_strings ();
2058 g_free (this_dir);
2059 g_free (other_dir);
2061 #ifdef USE_INTERNAL_EDIT
2062 edit_stack_free ();
2063 #endif
2065 return 0;