Ticket #380: About colors schemes (reopened ticket)
[midnight-commander.git] / src / main.c
blob2ebd1345cd22a812386811697362e337ccc40915
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 /* Define this function for glib-style error handling */
298 GQuark
299 mc_main_error_quark (void)
301 return g_quark_from_static_string (PACKAGE);
305 /* Save current stat of directories to avoid reloading the panels */
306 /* when no modifications have taken place */
307 void
308 save_cwds_stat (void)
310 if (fast_reload) {
311 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
312 if (get_other_type () == view_listing)
313 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
317 #ifdef HAVE_SUBSHELL_SUPPORT
318 void
319 do_update_prompt (void)
321 if (update_prompt) {
322 printf ("\r\n%s", subshell_prompt);
323 fflush (stdout);
324 update_prompt = 0;
327 #endif /* HAVE_SUBSHELL_SUPPORT */
329 void
330 change_panel (void)
332 free_completions (cmdline);
333 dlg_one_down (midnight_dlg);
336 /* Stop MC main dialog and the current dialog if it exists.
337 * Needed to provide fast exit from MC viewer or editor on shell exit */
338 static void
339 stop_dialogs (void)
341 midnight_dlg->running = 0;
342 if (current_dlg) {
343 current_dlg->running = 0;
347 static int
348 quit_cmd_internal (int quiet)
350 int q = quit;
352 if (quiet || !confirm_exit) {
353 q = 1;
354 } else {
355 if (query_dialog
356 (_(" The Midnight Commander "),
357 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
358 2, _("&Yes"), _("&No")) == 0)
359 q = 1;
361 if (q) {
362 #ifdef HAVE_SUBSHELL_SUPPORT
363 if (!use_subshell)
364 stop_dialogs ();
365 else if ((q = exit_subshell ()))
366 #endif
367 stop_dialogs ();
369 if (q)
370 quit |= 1;
371 return quit;
374 static void
375 quit_cmd (void)
377 quit_cmd_internal (0);
380 void
381 quiet_quit_cmd (void)
383 print_last_revert = 1;
384 quit_cmd_internal (1);
387 /* Wrapper for do_subshell_chdir, check for availability of subshell */
388 void
389 subshell_chdir (const char *directory)
391 #ifdef HAVE_SUBSHELL_SUPPORT
392 if (use_subshell) {
393 if (vfs_current_is_local ())
394 do_subshell_chdir (directory, 0, 1);
396 #endif /* HAVE_SUBSHELL_SUPPORT */
399 void
400 directory_history_add (struct WPanel *panel, const char *dir)
402 char *tmp;
404 tmp = g_strdup (dir);
405 strip_password (tmp, 1);
407 panel->dir_history = list_append_unique (panel->dir_history, tmp);
411 * If we moved to the parent directory move the selection pointer to
412 * the old directory name; If we leave VFS dir, remove FS specificator.
414 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
416 static const char *
417 get_parent_dir_name (const char *cwd, const char *lwd)
419 const char *p;
420 if (strlen (lwd) > strlen (cwd))
421 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
422 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
423 cwd[1] == '\0'))) {
424 return (p + 1);
426 return NULL;
430 * Changes the current directory of the panel.
431 * Don't record change in the directory history.
433 static int
434 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
436 const char *directory;
437 char *olddir;
438 char temp[MC_MAXPATHLEN];
439 char *translated_url;
441 if (cd_type == cd_parse_command) {
442 while (*new_dir == ' ')
443 new_dir++;
446 olddir = g_strdup (panel->cwd);
447 new_dir = translated_url = vfs_translate_url (new_dir);
449 /* Convert *new_path to a suitable pathname, handle ~user */
451 if (cd_type == cd_parse_command) {
452 if (!strcmp (new_dir, "-")) {
453 strcpy (temp, panel->lwd);
454 new_dir = temp;
457 directory = *new_dir ? new_dir : home_dir;
459 if (mc_chdir (directory) == -1) {
460 strcpy (panel->cwd, olddir);
461 g_free (olddir);
462 g_free (translated_url);
463 return 0;
465 g_free (translated_url);
467 /* Success: save previous directory, shutdown status of previous dir */
468 strcpy (panel->lwd, olddir);
469 free_completions (cmdline);
471 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
473 vfs_release_path (olddir);
475 subshell_chdir (panel->cwd);
477 /* Reload current panel */
478 panel_clean_dir (panel);
479 panel->count =
480 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
481 panel->reverse, panel->case_sensitive,
482 panel->exec_first, panel->filter);
483 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
484 load_hint (0);
485 panel->dirty = 1;
486 update_xterm_title_path ();
488 g_free (olddir);
490 return 1;
494 * Changes the current directory of the panel.
495 * Record change in the directory history.
498 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
500 int r;
502 r = _do_panel_cd (panel, new_dir, cd_type);
503 if (r)
504 directory_history_add (panel, panel->cwd);
505 return r;
509 do_cd (const char *new_dir, enum cd_enum exact)
511 return (do_panel_cd (current_panel, new_dir, exact));
514 void
515 directory_history_next (WPanel *panel)
517 GList *nextdir;
519 nextdir = g_list_next (panel->dir_history);
521 if (!nextdir)
522 return;
524 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
525 panel->dir_history = nextdir;
528 void
529 directory_history_prev (WPanel *panel)
531 GList *prevdir;
533 prevdir = g_list_previous (panel->dir_history);
535 if (!prevdir)
536 return;
538 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
539 panel->dir_history = prevdir;
542 void
543 directory_history_list (WPanel *panel)
545 char *s;
547 if (!panel->dir_history)
548 return;
550 s = show_hist (panel->dir_history, &panel->widget);
552 if (!s)
553 return;
555 if (_do_panel_cd (panel, s, cd_exact))
556 directory_history_add (panel, panel->cwd);
557 else
558 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
559 g_free (s);
562 #ifdef HAVE_SUBSHELL_SUPPORT
564 load_prompt (int fd, void *unused)
566 (void) fd;
567 (void) unused;
569 if (!read_subshell_prompt ())
570 return 0;
572 /* Don't actually change the prompt if it's invisible */
573 if (current_dlg == midnight_dlg && command_prompt) {
574 char *tmp_prompt;
575 int prompt_len;
577 tmp_prompt = strip_ctrl_codes (subshell_prompt);
578 prompt_len = str_term_width1 (tmp_prompt);
580 /* Check for prompts too big */
581 if (COLS > 8 && prompt_len > COLS - 8) {
582 tmp_prompt[COLS - 8] = '\0';
583 prompt_len = COLS - 8;
585 prompt = tmp_prompt;
586 label_set_text (the_prompt, prompt);
587 winput_set_origin ((WInput *) cmdline, prompt_len,
588 COLS - prompt_len);
590 /* since the prompt has changed, and we are called from one of the
591 * tty_get_event channels, the prompt updating does not take place
592 * automatically: force a cursor update and a screen refresh
594 update_cursor (midnight_dlg);
595 mc_refresh ();
597 update_prompt = 1;
598 return 0;
600 #endif /* HAVE_SUBSHELL_SUPPORT */
602 static void
603 sort_cmd (void)
605 WPanel *p;
606 sortfn *sort_order;
608 if (!SELECTED_IS_PANEL)
609 return;
611 p = MENU_PANEL;
612 sort_order = sort_box (p->sort_type, &p->reverse,
613 &p->case_sensitive,
614 &p->exec_first);
616 panel_set_sort_order (p, sort_order);
620 static void
621 treebox_cmd (void)
623 char *sel_dir;
625 sel_dir = tree_box (selection (current_panel)->fname);
626 if (sel_dir) {
627 do_cd (sel_dir, cd_exact);
628 g_free (sel_dir);
632 #ifdef LISTMODE_EDITOR
633 static void
634 listmode_cmd (void)
636 char *newmode;
638 if (get_current_type () != view_listing)
639 return;
641 newmode = listmode_edit (current_panel->user_format);
642 if (!newmode)
643 return;
645 g_free (current_panel->user_format);
646 current_panel->list_type = list_user;
647 current_panel->user_format = newmode;
648 set_panel_formats (current_panel);
650 do_refresh ();
652 #endif /* LISTMODE_EDITOR */
654 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
655 static menu_entry LeftMenu[] = {
656 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
657 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
658 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
659 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
660 {' ', "", NULL_HOTKEY, 0},
661 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
662 {' ', "", NULL_HOTKEY, 0},
663 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
664 #ifdef HAVE_CHARSET
665 {' ', "",NULL_HOTKEY, 0},
666 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
667 #endif
668 #ifdef USE_NETCODE
669 {' ', "", NULL_HOTKEY, 0},
670 #ifdef ENABLE_VFS_MCFS
671 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
672 #endif
673 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
674 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
675 #ifdef WITH_SMBFS
676 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
677 #endif
678 #endif
679 {' ', "", NULL_HOTKEY, 0},
680 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
683 static menu_entry RightMenu[] = {
684 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
685 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
686 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
687 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
688 {' ', "", NULL_HOTKEY, 0},
689 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
690 {' ', "", NULL_HOTKEY, 0},
691 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
692 #ifdef HAVE_CHARSET
693 {' ', "",NULL_HOTKEY, 0},
694 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
695 #endif
696 #ifdef USE_NETCODE
697 {' ', "", NULL_HOTKEY, 0},
698 #ifdef ENABLE_VFS_MCFS
699 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
700 #endif
701 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
702 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
703 #ifdef WITH_SMBFS
704 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
705 #endif
706 #endif
707 {' ', "", NULL_HOTKEY, 0},
708 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
711 static menu_entry FileMenu[] = {
712 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
713 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
714 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
715 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
716 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
717 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
718 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
719 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
720 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
721 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
722 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
723 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
724 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
725 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
726 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
727 {' ', "", NULL_HOTKEY, 0},
728 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
729 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
730 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
731 {' ', "", NULL_HOTKEY, 0},
732 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
735 static menu_entry CmdMenu[] = {
736 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
737 /* I know, I'm lazy, but the tree widget when it's not running
738 * as a panel still has some problems, I have not yet finished
739 * the WTree widget port, sorry.
741 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
742 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
743 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
744 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
745 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
746 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
747 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
748 {' ', "", NULL_HOTKEY, 0},
749 {' ', N_("Command &history M-h"), NULL_HOTKEY, history_cmd},
750 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
751 #ifdef USE_VFS
752 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
753 #endif
754 #ifdef WITH_BACKGROUND
755 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
756 #endif
757 {' ', "", NULL_HOTKEY, 0},
758 #ifdef USE_EXT2FSLIB
759 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
760 #endif
761 #ifdef LISTMODE_EDITOR
762 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
763 #endif
764 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
765 {' ', "", NULL_HOTKEY, 0},
766 #endif
767 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
768 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd},
769 {' ', N_("Edit &highlighting group file"), NULL_HOTKEY, edit_fhl_cmd}
772 /* Must keep in sync with the constants in menu_cmd */
773 static menu_entry OptMenu[] = {
774 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
775 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
776 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
777 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
778 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
779 #ifdef USE_VFS
780 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
781 #endif /* !USE_VFS */
782 {' ', "", NULL_HOTKEY, 0},
783 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
786 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
788 static Menu *MenuBar[5];
790 void
791 init_menu (void)
793 MenuBar[0] =
794 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
795 LeftMenu, menu_entries (LeftMenu),
796 "[Left and Right Menus]");
797 MenuBar[1] =
798 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
799 "[File Menu]");
800 MenuBar[2] =
801 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
802 "[Command Menu]");
803 MenuBar[3] =
804 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
805 "[Options Menu]");
806 MenuBar[4] =
807 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
808 RightMenu, menu_entries (RightMenu),
809 "[Left and Right Menus]");
812 void
813 done_menu (void)
815 int i;
817 for (i = 0; i < 5; i++) {
818 destroy_menu (MenuBar[i]);
822 static void
823 menu_last_selected_cmd (void)
825 the_menubar->active = 1;
826 the_menubar->dropped = drop_menus;
827 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
828 dlg_select_widget (the_menubar);
831 static void
832 menu_cmd (void)
834 if (the_menubar->active)
835 return;
837 if ((get_current_index () == 0) ^ (!current_panel->active))
838 the_menubar->selected = 0;
839 else
840 the_menubar->selected = 4;
841 menu_last_selected_cmd ();
844 /* Flag toggling functions */
845 void
846 toggle_fast_reload (void)
848 fast_reload = !fast_reload;
849 if (fast_reload_w == 0 && fast_reload) {
850 message (D_NORMAL, _(" Information "),
852 (" Using the fast reload option may not reflect the exact \n"
853 " directory contents. In this case you'll need to do a \n"
854 " manual reload of the directory. See the man page for \n"
855 " the details. "));
856 fast_reload_w = 1;
860 void
861 toggle_mix_all_files (void)
863 mix_all_files = !mix_all_files;
864 update_panels (UP_RELOAD, UP_KEEPSEL);
867 void
868 toggle_show_backup (void)
870 show_backups = !show_backups;
871 update_panels (UP_RELOAD, UP_KEEPSEL);
874 void
875 toggle_show_hidden (void)
877 show_dot_files = !show_dot_files;
878 update_panels (UP_RELOAD, UP_KEEPSEL);
881 void
882 toggle_kilobyte_si (void)
884 kilobyte_si = !kilobyte_si;
885 update_panels (UP_RELOAD, UP_KEEPSEL);
889 * Just a hack for allowing url-like pathnames to be accepted from the
890 * command line.
892 static void
893 translated_mc_chdir (char *dir)
895 char *newdir;
897 newdir = vfs_translate_url (dir);
898 mc_chdir (newdir);
899 g_free (newdir);
902 static void
903 create_panels (void)
905 int current_index;
906 int other_index;
907 int current_mode;
908 int other_mode;
909 char original_dir[1024];
911 original_dir[0] = 0;
913 if (boot_current_is_left) {
914 current_index = 0;
915 other_index = 1;
916 current_mode = startup_left_mode;
917 other_mode = startup_right_mode;
918 } else {
919 current_index = 1;
920 other_index = 0;
921 current_mode = startup_right_mode;
922 other_mode = startup_left_mode;
924 /* Creates the left panel */
925 if (this_dir) {
926 if (other_dir) {
927 /* Ok, user has specified two dirs, save the original one,
928 * since we may not be able to chdir to the proper
929 * second directory later
931 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
933 translated_mc_chdir (this_dir);
935 set_display_type (current_index, current_mode);
937 /* The other panel */
938 if (other_dir) {
939 if (original_dir[0])
940 translated_mc_chdir (original_dir);
941 translated_mc_chdir (other_dir);
943 set_display_type (other_index, other_mode);
945 if (startup_left_mode == view_listing) {
946 current_panel = left_panel;
947 } else {
948 if (right_panel)
949 current_panel = right_panel;
950 else
951 current_panel = left_panel;
954 /* Create the nice widgets */
955 cmdline = command_new (0, 0, 0);
956 the_prompt = label_new (0, 0, prompt);
957 the_prompt->transparent = 1;
958 the_bar = buttonbar_new (keybar_visible);
960 the_hint = label_new (0, 0, 0);
961 the_hint->transparent = 1;
962 the_hint->auto_adjust_cols = 0;
963 the_hint->widget.cols = COLS;
965 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
968 static void
969 copy_current_pathname (void)
971 char *cwd_path;
972 if (!command_prompt)
973 return;
975 cwd_path = remove_encoding_from_path (current_panel->cwd);
976 command_insert (cmdline, cwd_path, 0);
978 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
979 command_insert (cmdline, PATH_SEP_STR, 0);
980 g_free (cwd_path);
983 static void
984 copy_other_pathname (void)
986 char *cwd_path;
988 if (get_other_type () != view_listing)
989 return;
991 if (!command_prompt)
992 return;
994 cwd_path = remove_encoding_from_path (other_panel->cwd);
995 command_insert (cmdline, cwd_path, 0);
997 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
998 command_insert (cmdline, PATH_SEP_STR, 0);
999 g_free (cwd_path);
1002 static void
1003 copy_readlink (WPanel *panel)
1005 if (!command_prompt)
1006 return;
1007 if (S_ISLNK (selection (panel)->st.st_mode)) {
1008 char buffer[MC_MAXPATHLEN];
1009 char *p =
1010 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1011 int i;
1013 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1014 g_free (p);
1015 if (i > 0) {
1016 buffer[i] = 0;
1017 command_insert (cmdline, buffer, 1);
1022 static void
1023 copy_current_readlink (void)
1025 copy_readlink (current_panel);
1028 static void
1029 copy_other_readlink (void)
1031 if (get_other_type () != view_listing)
1032 return;
1033 copy_readlink (other_panel);
1036 /* Insert the selected file name into the input line */
1037 static void
1038 copy_prog_name (void)
1040 char *tmp;
1041 if (!command_prompt)
1042 return;
1044 if (get_current_type () == view_tree) {
1045 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1046 tmp = tree_selected_name (tree);
1047 } else
1048 tmp = selection (current_panel)->fname;
1050 command_insert (cmdline, tmp, 1);
1053 static void
1054 copy_tagged (WPanel *panel)
1056 int i;
1058 if (!command_prompt)
1059 return;
1060 input_disable_update (cmdline);
1061 if (panel->marked) {
1062 for (i = 0; i < panel->count; i++) {
1063 if (panel->dir.list[i].f.marked)
1064 command_insert (cmdline, panel->dir.list[i].fname, 1);
1066 } else {
1067 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1070 input_enable_update (cmdline);
1073 static void
1074 copy_current_tagged (void)
1076 copy_tagged (current_panel);
1079 static void
1080 copy_other_tagged (void)
1082 if (get_other_type () != view_listing)
1083 return;
1084 copy_tagged (other_panel);
1087 static void
1088 init_labels (void)
1090 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1091 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1092 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1093 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1096 static const key_map ctl_x_map[] = {
1097 {XCTRL ('c'), quit_cmd},
1098 {'d', compare_dirs_cmd},
1099 #ifdef USE_VFS
1100 {'a', reselect_vfs},
1101 #endif /* USE_VFS */
1102 {'p', copy_current_pathname},
1103 {XCTRL ('p'), copy_other_pathname},
1104 {'t', copy_current_tagged},
1105 {XCTRL ('t'), copy_other_tagged},
1106 {'c', chmod_cmd},
1107 {'o', chown_cmd},
1108 {'r', copy_current_readlink},
1109 {XCTRL ('r'), copy_other_readlink},
1110 {'l', link_cmd},
1111 {'s', symlink_cmd},
1112 {XCTRL ('s'), edit_symlink_cmd},
1113 {'i', info_cmd_no_menu},
1114 {'q', quick_cmd_no_menu},
1115 {'h', add2hotlist_cmd},
1116 {'!', external_panelize},
1117 #ifdef WITH_BACKGROUND
1118 {'j', jobs_cmd},
1119 #endif /* WITH_BACKGROUND */
1120 {0, 0}
1123 static int ctl_x_map_enabled = 0;
1125 static void
1126 ctl_x_cmd (void)
1128 ctl_x_map_enabled = 1;
1131 static void
1132 nothing (void)
1136 static const key_map default_map[] = {
1137 {KEY_F (19), menu_last_selected_cmd},
1138 {KEY_F (20), quiet_quit_cmd},
1140 {XCTRL ('@'), smart_dirsize_cmd},
1142 /* Copy useful information to the command line */
1143 {ALT ('a'), copy_current_pathname},
1144 {ALT ('A'), copy_other_pathname},
1146 {ALT ('c'), quick_cd_cmd},
1148 /* To access the directory hotlist */
1149 {XCTRL ('\\'), quick_chdir_cmd},
1151 /* Suspend */
1152 {XCTRL ('z'), suspend_cmd},
1154 /* The filtered view command */
1155 {ALT ('!'), filtered_view_cmd},
1157 /* Find file */
1158 {ALT ('?'), find_cmd},
1160 /* Panel refresh */
1161 {XCTRL ('r'), reread_cmd},
1163 /* Toggle listing between long, user defined and full formats */
1164 {ALT ('t'), toggle_listing_cmd},
1166 /* Swap panels */
1167 {XCTRL ('u'), swap_cmd},
1169 /* View output */
1170 {XCTRL ('o'), view_other_cmd},
1172 /* Control-X keybindings */
1173 {XCTRL ('x'), ctl_x_cmd},
1175 /* Show/hide hidden files */
1176 {ALT ('.'), toggle_show_hidden},
1178 /* Trap dlg's exit commands */
1179 {ESC_CHAR, nothing},
1180 {XCTRL ('c'), nothing},
1181 {XCTRL ('g'), nothing},
1182 {0, 0},
1185 static void
1186 setup_pre (void)
1188 /* Call all the inits */
1190 #ifdef HAVE_SLANG
1191 tty_display_8bit (full_eight_bits != 0);
1192 #else
1193 tty_display_8bit (eight_bit_clean != 0);
1194 #endif
1197 static void
1198 init_xterm_support (void)
1200 const char *termvalue;
1202 termvalue = getenv ("TERM");
1203 if (!termvalue || !(*termvalue)) {
1204 fputs (_("The TERM environment variable is unset!\n"), stderr);
1205 exit (1);
1208 /* Check mouse capabilities */
1209 xmouse_seq = tty_tgetstr ("Km");
1211 if (strcmp (termvalue, "cygwin") == 0) {
1212 mc_args__force_xterm = 1;
1213 use_mouse_p = MOUSE_DISABLED;
1216 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1217 || strncmp (termvalue, "konsole", 7) == 0
1218 || strncmp (termvalue, "rxvt", 4) == 0
1219 || strcmp (termvalue, "Eterm") == 0
1220 || strcmp (termvalue, "dtterm") == 0) {
1221 xterm_flag = 1;
1223 /* Default to the standard xterm sequence */
1224 if (!xmouse_seq) {
1225 xmouse_seq = ESC_STR "[M";
1228 /* Enable mouse unless explicitly disabled by --nomouse */
1229 if (use_mouse_p != MOUSE_DISABLED) {
1230 const char *color_term = getenv ("COLORTERM");
1231 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1232 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1233 strcmp (termvalue, "Eterm") == 0) {
1234 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1235 } else {
1236 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1242 static void
1243 setup_mc (void)
1245 setup_pre ();
1246 init_menu ();
1247 create_panels ();
1248 setup_panels ();
1250 #ifdef HAVE_SUBSHELL_SUPPORT
1251 if (use_subshell)
1252 add_select_channel (subshell_pty, load_prompt, 0);
1253 #endif /* !HAVE_SUBSHELL_SUPPORT */
1255 tty_setup_sigwinch (sigwinch_handler);
1257 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1259 init_xterm_support ();
1260 init_mouse ();
1263 static void
1264 setup_dummy_mc (void)
1266 char d[MC_MAXPATHLEN];
1268 mc_get_current_wd (d, MC_MAXPATHLEN);
1269 setup_mc ();
1270 mc_chdir (d);
1273 static void check_codeset()
1275 const char *_system_codepage = NULL;
1276 #ifdef HAVE_CHARSET
1277 const char *_source_codepage = NULL;
1278 const char *_display_codepage = NULL;
1279 int profile_changed = 0;
1281 #define CONFY 16
1282 #define CONFX 54
1284 if ( !skip_check_codeset ) {
1286 QuickWidget ecs_widgets [] = {
1287 QUICK_BUTTON (4, 6, 13, CONFY, N_("&Skip"), B_EXIT, NULL),
1288 QUICK_BUTTON (1, 11, 13, CONFY, N_("&Fix it"), B_ENTER, NULL),
1289 QUICK_CHECKBOX (1, 13, 11, CONFY, N_("don't ask again"), &skip_check_codeset),
1290 QUICK_LABEL (2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
1291 "or source codeset (in mcedit ctrl-t) \n"
1292 "does not match one set via locale. \n"
1293 "Set correct codeset manually or press <<Fix it>> \n"
1294 "to set locale default.\n\n"
1295 "Or set \'don't ask again\' and press <<Skip>>")),
1296 QUICK_END
1299 QuickDialog ecs =
1301 CONFX, CONFY, -1, -1, N_(" Confirmation "),
1302 "[Confirmation]", ecs_widgets, FALSE
1306 _system_codepage = str_detect_termencoding();
1307 _source_codepage = get_codepage_id (source_codepage);
1308 _display_codepage = get_codepage_id (display_codepage);
1309 if ( (strcmp (_system_codepage, _display_codepage)) ||
1310 (strcmp (_system_codepage, _source_codepage)) ) {
1311 if (quick_dialog (&ecs) == B_ENTER){
1312 display_codepage = get_codepage_index (_system_codepage);
1313 cp_display = get_codepage_id (display_codepage);
1314 if ( !strcmp (cp_display, _system_codepage)) {
1315 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1316 mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
1317 display_codepage = get_codepage_index ( cp_display );
1318 utf8_display = str_isutf8 (_system_codepage);
1319 source_codepage = display_codepage;
1320 cp_source = cp_display;
1321 profile_changed = 1;
1322 } else {
1323 utf8_display = str_isutf8 (_system_codepage);
1324 cp_source = cp_display = _system_codepage;
1326 } else {
1327 if ( skip_check_codeset ) {
1328 mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
1329 profile_changed = 1;
1333 if ( profile_changed )
1334 save_configure ();
1336 #else /* HAVE_CHARSET */
1337 _system_codepage = str_detect_termencoding();
1338 utf8_display = str_isutf8 (_system_codepage);
1339 #endif /* HAVE_CHARSET */
1342 static void
1343 done_screen (void)
1345 if (!(quit & SUBSHELL_EXIT))
1346 clr_scr ();
1347 tty_reset_shell_mode ();
1348 tty_noraw_mode ();
1349 tty_keypad (FALSE);
1350 tty_colors_done ();
1353 static void
1354 done_mc (void)
1356 disable_mouse ();
1358 done_menu ();
1360 /* Setup shutdown
1362 * We sync the profiles since the hotlist may have changed, while
1363 * we only change the setup data if we have the auto save feature set
1366 if (auto_save_setup)
1367 save_setup (); /* does also call save_hotlist */
1368 else {
1369 save_hotlist ();
1370 save_panel_types ();
1372 done_screen ();
1373 vfs_add_current_stamps ();
1376 /* This should be called after destroy_dlg since panel widgets
1377 * save their state on the profiles
1379 static void
1380 done_mc_profile (void)
1382 done_setup ();
1385 static cb_ret_t
1386 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1388 int i;
1390 switch (msg) {
1392 case DLG_IDLE:
1393 /* We only need the first idle event */
1394 set_idle_proc (h, 0);
1395 if (auto_menu) {
1396 user_file_menu_cmd ();
1398 return MSG_HANDLED;
1400 case DLG_KEY:
1401 if (ctl_x_map_enabled) {
1402 ctl_x_map_enabled = 0;
1403 for (i = 0; ctl_x_map[i].key_code; i++)
1404 if (parm == ctl_x_map[i].key_code) {
1405 (*ctl_x_map[i].fn) ();
1406 return MSG_HANDLED;
1410 /* FIXME: should handle all menu shortcuts before this point */
1411 if (the_menubar->active)
1412 return MSG_NOT_HANDLED;
1414 if (parm == KEY_F (10)) {
1415 quit_cmd ();
1416 return MSG_HANDLED;
1419 if (parm == '\t')
1420 free_completions (cmdline);
1422 if (parm == '\n') {
1423 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1424 cmdline->buffer[i] == '\t'); i++);
1425 if (cmdline->buffer[i]) {
1426 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1427 return MSG_HANDLED;
1429 stuff (cmdline, "", 0);
1430 cmdline->point = 0;
1433 /* Ctrl-Enter and Alt-Enter */
1434 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1435 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1436 copy_prog_name ();
1437 return MSG_HANDLED;
1440 /* Ctrl-Shift-Enter */
1441 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1442 copy_current_pathname ();
1443 copy_prog_name ();
1444 return MSG_HANDLED;
1447 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1448 && !quote && !current_panel->searching) {
1449 if (!only_leading_plus_minus) {
1450 /* Special treatement, since the input line will eat them */
1451 if (parm == '+') {
1452 select_cmd ();
1453 return MSG_HANDLED;
1456 if (parm == '\\' || parm == '-') {
1457 unselect_cmd ();
1458 return MSG_HANDLED;
1461 if (parm == '*') {
1462 reverse_selection_cmd ();
1463 return MSG_HANDLED;
1465 } else if (!command_prompt || !cmdline->buffer[0]) {
1466 /* Special treatement '+', '-', '\', '*' only when this is
1467 * first char on input line
1470 if (parm == '+') {
1471 select_cmd ();
1472 return MSG_HANDLED;
1475 if (parm == '\\' || parm == '-') {
1476 unselect_cmd ();
1477 return MSG_HANDLED;
1480 if (parm == '*') {
1481 reverse_selection_cmd ();
1482 return MSG_HANDLED;
1486 return MSG_NOT_HANDLED;
1488 case DLG_HOTKEY_HANDLED:
1489 if ((get_current_type () == view_listing) && current_panel->searching) {
1490 current_panel->searching = 0;
1491 current_panel->dirty = 1;
1493 return MSG_HANDLED;
1495 case DLG_UNHANDLED_KEY:
1496 if (command_prompt) {
1497 int v;
1499 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1500 if (v)
1501 return v;
1503 if (ctl_x_map_enabled) {
1504 ctl_x_map_enabled = 0;
1505 for (i = 0; ctl_x_map[i].key_code; i++)
1506 if (parm == ctl_x_map[i].key_code) {
1507 (*ctl_x_map[i].fn) ();
1508 return MSG_HANDLED;
1510 } else {
1511 for (i = 0; default_map[i].key_code; i++) {
1512 if (parm == default_map[i].key_code) {
1513 (*default_map[i].fn) ();
1514 return MSG_HANDLED;
1518 return MSG_NOT_HANDLED;
1520 case DLG_DRAW:
1521 load_hint (1);
1522 /* We handle the special case of the output lines */
1523 if (console_flag && output_lines)
1524 show_console_contents (output_start_y,
1525 LINES - output_lines - keybar_visible -
1526 1, LINES - keybar_visible - 1);
1527 return MSG_HANDLED;
1529 case DLG_POST_KEY:
1530 if (!the_menubar->active)
1531 update_dirty_panels ();
1532 return MSG_HANDLED;
1534 default:
1535 return default_dlg_callback (h, msg, parm);
1539 /* Show current directory in the xterm title */
1540 void
1541 update_xterm_title_path (void)
1543 const char *path;
1544 char host[BUF_TINY];
1545 char *p;
1546 struct passwd *pw = NULL;
1547 char *login = NULL;
1548 int res = 0;
1549 if (xterm_flag && xterm_title) {
1550 path = strip_home_and_password (current_panel->cwd);
1551 res = gethostname(host, sizeof (host));
1552 if ( res ) { /* On success, res = 0 */
1553 host[0] = '\0';
1554 } else {
1555 host[sizeof (host) - 1] = '\0';
1557 pw = getpwuid(getuid());
1558 if ( pw ) {
1559 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1560 } else {
1561 login = g_strdup (host);
1563 p = g_strdup_printf ("mc [%s]:%s", login, path);
1564 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1565 g_free (login);
1566 g_free (p);
1567 if (!alternate_plus_minus)
1568 numeric_keypad_mode ();
1569 fflush (stdout);
1574 * Load new hint and display it.
1575 * IF force is not 0, ignore the timeout.
1577 void
1578 load_hint (int force)
1580 char *hint;
1582 if (!the_hint->widget.parent)
1583 return;
1585 if (!message_visible) {
1586 label_set_text (the_hint, NULL);
1587 return;
1590 hint = get_random_hint (force);
1592 if (hint != NULL) {
1593 if (*hint)
1594 set_hintbar (hint);
1595 g_free (hint);
1596 } else {
1597 char text[BUF_SMALL];
1599 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1600 VERSION);
1601 set_hintbar (text);
1605 static void
1606 setup_panels_and_run_mc (void)
1608 add_widget (midnight_dlg, the_menubar);
1609 add_widget (midnight_dlg, get_panel_widget (0));
1610 add_widget (midnight_dlg, get_panel_widget (1));
1611 add_widget (midnight_dlg, the_hint);
1612 add_widget (midnight_dlg, cmdline);
1613 add_widget (midnight_dlg, the_prompt);
1614 add_widget (midnight_dlg, the_bar);
1615 init_labels ();
1617 if (boot_current_is_left)
1618 dlg_select_widget (get_panel_widget (0));
1619 else
1620 dlg_select_widget (get_panel_widget (1));
1622 /* Run the Midnight Commander if no file was specified in the command line */
1623 run_dlg (midnight_dlg);
1626 /* result must be free'd (I think this should go in util.c) */
1627 static char *
1628 prepend_cwd_on_local (const char *filename)
1630 char *d;
1631 int l;
1633 if (vfs_file_is_local (filename)) {
1634 if (*filename == PATH_SEP) /* an absolute pathname */
1635 return g_strdup (filename);
1636 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1637 mc_get_current_wd (d, MC_MAXPATHLEN);
1638 l = strlen (d);
1639 d[l++] = PATH_SEP;
1640 strcpy (d + l, filename);
1641 canonicalize_pathname (d);
1642 return d;
1643 } else
1644 return g_strdup (filename);
1647 static int
1648 mc_maybe_editor_or_viewer (void)
1650 if (!(view_one_file || edit_one_file))
1651 return 0;
1653 /* Invoke the internal view/edit routine with:
1654 * the default processing and forcing the internal viewer/editor
1656 if (view_one_file) {
1657 char *path = NULL;
1658 path = prepend_cwd_on_local (view_one_file);
1659 view_file (path, 0, 1);
1660 g_free (path);
1662 #ifdef USE_INTERNAL_EDIT
1663 else {
1664 edit_file (edit_one_file, edit_one_file_start_line);
1666 #endif /* USE_INTERNAL_EDIT */
1667 midnight_shutdown = 1;
1668 done_mc ();
1669 return 1;
1672 /* Run the main dialog that occupies the whole screen */
1673 static void
1674 do_nc (void)
1676 int midnight_colors[DLG_COLOR_NUM];
1677 midnight_colors[0] = mc_skin_color_get("dialog", "_default_");
1678 midnight_colors[1] = mc_skin_color_get("dialog", "focus");
1679 midnight_colors[2] = mc_skin_color_get("dialog", "hotnormal");
1680 midnight_colors[3] = mc_skin_color_get("dialog", "hotfocus");
1682 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1683 "[main]", NULL, DLG_WANT_IDLE);
1685 if (view_one_file || edit_one_file)
1686 setup_dummy_mc ();
1687 else
1688 setup_mc ();
1690 /* start check display_codepage and source_codepage */
1691 check_codeset();
1693 /* Check if we were invoked as an editor or file viewer */
1694 if (!mc_maybe_editor_or_viewer ()) {
1695 setup_panels_and_run_mc ();
1697 /* Program end */
1698 midnight_shutdown = 1;
1700 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1701 if (mc_args__last_wd_file && vfs_current_is_local ()) {
1702 last_wd_string = g_strdup (current_panel->cwd);
1704 done_mc ();
1706 destroy_dlg (midnight_dlg);
1707 current_panel = 0;
1708 done_mc_profile ();
1711 /* POSIX version. The only version we support. */
1712 static void
1713 OS_Setup (void)
1715 const char *shell_env = getenv ("SHELL");
1716 const char *mc_libdir;
1718 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1719 struct passwd *pwd;
1720 pwd = getpwuid (geteuid ());
1721 if (pwd != NULL)
1722 shell = g_strdup (pwd->pw_shell);
1723 } else
1724 shell = g_strdup (shell_env);
1726 if ((shell == NULL) || (shell[0] == '\0')) {
1727 g_free (shell);
1728 shell = g_strdup ("/bin/sh");
1731 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1732 /* and can be overriden by the MC_DATADIR environment variable */
1733 mc_libdir = getenv ("MC_DATADIR");
1734 if (mc_libdir != NULL) {
1735 mc_home = g_strdup (mc_libdir);
1736 mc_home_alt = g_strdup (SYSCONFDIR);
1737 } else {
1738 mc_home = g_strdup (SYSCONFDIR);
1739 mc_home_alt = g_strdup (DATADIR);
1742 /* This variable is used by the subshell */
1743 home_dir = getenv ("HOME");
1745 if (!home_dir)
1746 home_dir = mc_home;
1750 static void
1751 sigchld_handler_no_subshell (int sig)
1753 #ifdef __linux__
1754 int pid, status;
1756 if (!console_flag)
1757 return;
1759 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1760 the value of console_flag never changed, we could simply not install
1761 this handler at all if (!console_flag && !use_subshell). */
1763 /* That comment is no longer true. We need to wait() on a sigchld
1764 handler (that's at least what the tarfs code expects currently). */
1766 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1768 if (pid == cons_saver_pid) {
1770 if (WIFSTOPPED (status)) {
1771 /* Someone has stopped cons.saver - restart it */
1772 kill (pid, SIGCONT);
1773 } else {
1774 /* cons.saver has died - disable console saving */
1775 handle_console (CONSOLE_DONE);
1776 console_flag = 0;
1779 /* If we got here, some other child exited; ignore it */
1780 #endif /* __linux__ */
1782 (void) sig;
1785 static void
1786 init_sigchld (void)
1788 struct sigaction sigchld_action;
1790 sigchld_action.sa_handler =
1791 #ifdef HAVE_SUBSHELL_SUPPORT
1792 use_subshell ? sigchld_handler :
1793 #endif /* HAVE_SUBSHELL_SUPPORT */
1794 sigchld_handler_no_subshell;
1796 sigemptyset (&sigchld_action.sa_mask);
1798 #ifdef SA_RESTART
1799 sigchld_action.sa_flags = SA_RESTART;
1800 #else
1801 sigchld_action.sa_flags = 0;
1802 #endif /* !SA_RESTART */
1804 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1805 #ifdef HAVE_SUBSHELL_SUPPORT
1807 * This may happen on QNX Neutrino 6, where SA_RESTART
1808 * is defined but not implemented. Fallback to no subshell.
1810 use_subshell = 0;
1811 #endif /* HAVE_SUBSHELL_SUPPORT */
1815 static void
1816 mc_main__setup_by_args(int argc, char *argv[])
1818 const char *base;
1819 char *tmp;
1821 if (mc_args__nomouse)
1822 use_mouse_p = MOUSE_DISABLED;
1824 #ifdef USE_NETCODE
1825 if (mc_args__netfs_logfile != NULL)
1827 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
1828 #ifdef WITH_SMBFS
1829 smbfs_set_debugf (mc_args__netfs_logfile);
1830 #endif /* WITH_SMBFS */
1833 #ifdef WITH_SMBFS
1834 if (mc_args__debug_level != 0)
1836 smbfs_set_debug (mc_args__debug_level);
1838 #endif /* WITH_SMBFS */
1839 #endif /* USE_NETCODE */
1842 base = x_basename (argv[0]);
1843 tmp = (argc > 0)? argv[1] : NULL;
1846 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
1847 edit_one_file = "";
1848 if (tmp) {
1850 * Check for filename:lineno, followed by an optional colon.
1851 * This format is used by many programs (especially compilers)
1852 * in error messages and warnings. It is supported so that
1853 * users can quickly copy and paste file locations.
1855 char *end = tmp + strlen (tmp), *p = end;
1856 if (p > tmp && p[-1] == ':')
1857 p--;
1858 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
1859 p--;
1860 if (tmp < p && p < end && p[-1] == ':') {
1861 struct stat st;
1862 gchar *fname = g_strndup (tmp, p - 1 - tmp);
1864 * Check that the file before the colon actually exists.
1865 * If it doesn't exist, revert to the old behavior.
1867 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
1868 edit_one_file = fname;
1869 edit_one_file_start_line = atoi (p);
1870 } else {
1871 g_free (fname);
1872 goto try_plus_filename;
1874 } else {
1875 try_plus_filename:
1876 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
1877 int start_line = atoi (tmp);
1878 if (start_line > 0) {
1879 char *file = (argc > 1) ? argv[2] : NULL;
1880 if (file) {
1881 tmp = file;
1882 edit_one_file_start_line = start_line;
1886 edit_one_file = g_strdup (tmp);
1889 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
1890 if (tmp)
1891 view_one_file = g_strdup (tmp);
1892 else {
1893 fputs ("No arguments given to the viewer\n", stderr);
1894 exit (1);
1896 } else {
1897 /* sets the current dir and the other dir */
1898 if (tmp) {
1899 this_dir = g_strdup (tmp);
1900 tmp = (argc > 1) ? argv[2] : NULL;
1901 if (tmp)
1902 other_dir = g_strdup (tmp);
1910 main (int argc, char *argv[])
1912 struct stat s;
1913 char *mc_dir;
1914 GError *error = NULL;
1915 gboolean isInitialized;
1917 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
1918 setlocale (LC_ALL, "");
1919 bindtextdomain ("mc", LOCALEDIR);
1920 textdomain ("mc");
1922 /* Set up temporary directory */
1923 mc_tmpdir ();
1925 OS_Setup ();
1927 str_init_strings (NULL);
1929 vfs_init ();
1931 #ifdef USE_INTERNAL_EDIT
1932 edit_stack_init ();
1933 #endif
1935 #ifdef HAVE_SLANG
1936 SLtt_Ignore_Beep = 1;
1937 #endif
1939 if ( !mc_args_handle (&argc, &argv, "mc"))
1940 return 1;
1941 mc_main__setup_by_args(argc,argv);
1944 /* NOTE: This has to be called before tty_init or whatever routine
1945 calls any define_sequence */
1946 init_key ();
1948 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
1949 handle_console (CONSOLE_INIT);
1951 #ifdef HAVE_SUBSHELL_SUPPORT
1952 /* Don't use subshell when invoked as viewer or editor */
1953 if (edit_one_file || view_one_file)
1954 use_subshell = 0;
1956 if (use_subshell)
1957 subshell_get_console_attributes ();
1958 #endif /* HAVE_SUBSHELL_SUPPORT */
1960 /* Install the SIGCHLD handler; must be done before init_subshell() */
1961 init_sigchld ();
1963 /* We need this, since ncurses endwin () doesn't restore the signals */
1964 save_stop_handler ();
1966 /* Must be done before init_subshell, to set up the terminal size: */
1967 /* FIXME: Should be removed and LINES and COLS computed on subshell */
1968 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
1970 load_setup ();
1972 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
1974 isInitialized = mc_skin_init(&error);
1976 mc_filehighlight = mc_fhl_new (TRUE);
1978 dlg_set_default_colors ();
1980 if ( ! isInitialized ) {
1981 message (D_ERROR, _("Warning"), error->message);
1982 g_error_free(error);
1983 error = NULL;
1986 /* create home directory */
1987 /* do it after the screen library initialization to show the error message */
1988 mc_dir = concat_dir_and_file (home_dir, MC_BASE);
1989 canonicalize_pathname (mc_dir);
1990 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
1991 && mkdir (mc_dir, 0700) != 0)
1992 message (D_ERROR, _("Warning"),
1993 _("Cannot create %s directory"), mc_dir);
1994 g_free (mc_dir);
1996 #ifdef HAVE_SUBSHELL_SUPPORT
1997 /* Done here to ensure that the subshell doesn't */
1998 /* inherit the file descriptors opened below, etc */
1999 if (use_subshell)
2000 init_subshell ();
2002 #endif /* HAVE_SUBSHELL_SUPPORT */
2004 /* Removing this from the X code let's us type C-c */
2005 load_key_defs ();
2007 /* Also done after init_subshell, to save any shell init file messages */
2008 if (console_flag)
2009 handle_console (CONSOLE_SAVE);
2011 if (alternate_plus_minus)
2012 application_keypad_mode ();
2014 #ifdef HAVE_SUBSHELL_SUPPORT
2015 if (use_subshell) {
2016 prompt = strip_ctrl_codes (subshell_prompt);
2017 if (!prompt)
2018 prompt = "";
2019 } else
2020 #endif /* HAVE_SUBSHELL_SUPPORT */
2021 prompt = (geteuid () == 0) ? "# " : "$ ";
2024 /* Program main loop */
2025 if (!midnight_shutdown)
2026 do_nc ();
2028 /* Save the tree store */
2029 tree_store_save ();
2031 /* Virtual File System shutdown */
2032 vfs_shut ();
2034 flush_extension_file (); /* does only free memory */
2036 mc_fhl_free (&mc_filehighlight);
2037 mc_skin_deinit();
2039 tty_shutdown ();
2041 if (console_flag && !(quit & SUBSHELL_EXIT))
2042 handle_console (CONSOLE_RESTORE);
2043 if (alternate_plus_minus)
2044 numeric_keypad_mode ();
2046 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2048 if (console_flag)
2049 handle_console (CONSOLE_DONE);
2050 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2052 if (mc_args__last_wd_file && last_wd_string && !print_last_revert
2053 && !edit_one_file && !view_one_file) {
2054 int last_wd_fd =
2055 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2056 S_IRUSR | S_IWUSR);
2058 if (last_wd_fd != -1) {
2059 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2060 close (last_wd_fd);
2063 g_free (last_wd_string);
2065 g_free (mc_home_alt);
2066 g_free (mc_home);
2067 g_free (shell);
2069 done_key ();
2070 #ifdef HAVE_CHARSET
2071 free_codepages_list ();
2072 #endif
2073 str_uninit_strings ();
2075 g_free (this_dir);
2076 g_free (other_dir);
2078 #ifdef USE_INTERNAL_EDIT
2079 edit_stack_free ();
2080 #endif
2082 return 0;