Dialog: create own copy of colors.
[midnight-commander.git] / src / main.c
blob50d3abf229c55478cec18904257ab12beb9aadad
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 <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/wait.h>
40 #include <unistd.h>
41 #include <pwd.h>
43 #include "global.h"
45 #include "../src/tty/tty.h"
46 #include "../src/tty/color.h"
47 #include "../src/tty/mouse.h"
48 #include "../src/tty/key.h" /* For init_key() */
49 #include "../src/tty/win.h" /* xterm_flag */
51 #include "dir.h"
52 #include "dialog.h"
53 #include "menu.h"
54 #include "panel.h"
55 #include "main.h"
56 #include "option.h"
57 #include "tree.h"
58 #include "treestore.h"
59 #include "cons.saver.h"
60 #include "subshell.h"
61 #include "setup.h" /* save_setup() */
62 #include "../src/mcconfig/mcconfig.h"
63 #include "boxes.h" /* sort_box() */
64 #include "layout.h"
65 #include "cmd.h" /* Normal commands */
66 #include "hotlist.h"
67 #include "panelize.h"
68 #include "learn.h" /* learn_keys() */
69 #include "listmode.h"
70 #include "execute.h"
71 #include "ext.h" /* For flush_extension_file() */
72 #include "strutil.h"
73 #include <pwd.h> /* for username in xterm title */
75 /* Listbox for the command history feature */
76 #include "widget.h"
77 #include "command.h"
78 #include "wtools.h"
80 #include "chmod.h"
81 #include "chown.h"
82 #include "achown.h"
84 #ifdef WITH_SMBFS
85 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
86 #endif
88 #ifdef USE_INTERNAL_EDIT
89 # include "../edit/edit.h"
90 #endif
92 #ifdef HAVE_CHARSET
93 #include "charsets.h"
94 #endif /* HAVE_CHARSET */
96 #ifdef USE_VFS
97 #include "../vfs/gc.h"
98 #endif
100 #include "popt.h"
102 /* When the modes are active, left_panel, right_panel and tree_panel */
103 /* Point to a proper data structure. You should check with the functions */
104 /* get_current_type and get_other_type the types of the panels before using */
105 /* This pointer variables */
107 /* The structures for the panels */
108 WPanel *left_panel = NULL;
109 WPanel *right_panel = NULL;
111 /* The pointer to the tree */
112 WTree *the_tree = NULL;
114 /* The Menubar */
115 struct WMenu *the_menubar = NULL;
117 /* Pointers to the selected and unselected panel */
118 WPanel *current_panel = NULL;
120 /* Set if the command is being run from the "Right" menu */
121 int is_right = 0;
123 /* Set when main loop should be terminated */
124 volatile int quit = 0;
126 /* Set if you want the possible completions dialog for the first time */
127 int show_all_if_ambiguous = 0;
129 /* Set when cd symlink following is desirable (bash mode) */
130 int cd_symlinks = 1;
132 /* If set then dialogs just clean the screen when refreshing, else */
133 /* they do a complete refresh, refreshing all the parts of the program */
134 int fast_refresh = 0;
136 /* If true, marking a files moves the cursor down */
137 int mark_moves_down = 1;
139 /* If true, at startup the user-menu is invoked */
140 int auto_menu = 0;
142 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
143 and M-- and keypad + / - */
144 int alternate_plus_minus = 0;
146 /* If true, then the +, - and \ keys have their special meaning only if the
147 * command line is emtpy, otherwise they behave like regular letters
149 int only_leading_plus_minus = 1;
151 int pause_after_run = pause_on_dumb_terminals;
153 /* It true saves the setup when quitting */
154 int auto_save_setup = 1;
156 #ifndef HAVE_CHARSET
157 /* If true, allow characters in the range 160-255 */
158 int eight_bit_clean = 1;
161 * If true, also allow characters in the range 128-159.
162 * This is reported to break on many terminals (xterm, qansi-m).
164 int full_eight_bits = 0;
166 #endif /* !HAVE_CHARSET */
169 * If utf-8 terminal utf8_display = 1
170 * Display bits set UTF-8
173 int utf8_display = 0;
175 /* If true use the internal viewer */
176 int use_internal_view = 1;
178 /* Have we shown the fast-reload warning in the past? */
179 int fast_reload_w = 0;
181 /* Move page/item? When clicking on the top or bottom of a panel */
182 int mouse_move_pages = 1;
184 /* If true: l&r arrows are used to chdir if the input line is empty */
185 int navigate_with_arrows = 0;
187 /* If true program softkeys (HP terminals only) on startup and after every
188 command ran in the subshell to the description found in the termcap/terminfo
189 database */
190 int reset_hp_softkeys = 0;
192 /* The prompt */
193 const char *prompt = NULL;
195 /* The widget where we draw the prompt */
196 WLabel *the_prompt;
198 /* The hint bar */
199 WLabel *the_hint;
201 /* The button bar */
202 WButtonBar *the_bar;
204 /* For slow terminals */
205 static int slow_terminal = 0;
207 /* If true use +, -, | for line drawing */
208 static int ugly_line_drawing = 0;
210 /* Mouse type: GPM, xterm or none */
211 Mouse_Type use_mouse_p = MOUSE_NONE;
213 /* If true, assume we are running on an xterm terminal */
214 static int force_xterm = 0;
216 /* If on, default for "No" in delete operations */
217 int safe_delete = 0;
219 /* Controls screen clearing before an exec */
220 int clear_before_exec = 1;
222 /* Asks for confirmation before deleting a file */
223 int confirm_delete = 1;
225 /* Asks for confirmation before deleting a hotlist entry */
226 int confirm_directory_hotlist_delete = 1;
228 /* Asks for confirmation before overwriting a file */
229 int confirm_overwrite = 1;
231 /* Asks for confirmation before executing a program by pressing enter */
232 int confirm_execute = 0;
234 /* Asks for confirmation before leaving the program */
235 int confirm_exit = 1;
237 /* Asks for confirmation when using F3 to view a directory and there
238 are tagged files */
239 int confirm_view_dir = 0;
241 /* This flag indicates if the pull down menus by default drop down */
242 int drop_menus = 0;
244 /* if skip_check_codeset = 1 do not show warning about
245 * system and display codeset is different
247 int skip_check_codeset = 0;
249 /* The dialog handle for the main program */
250 Dlg_head *midnight_dlg = NULL;
252 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
253 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
254 int update_prompt = 0;
256 /* The home directory */
257 const char *home_dir = NULL;
259 /* The value of the other directory, only used when loading the setup */
260 char *other_dir = NULL;
262 /* Only used at program boot */
263 int boot_current_is_left = 1;
265 static char *this_dir = NULL;
267 /* If this is true, then when browsing the tree the other window will
268 * automatically reload it's directory with the contents of the currently
269 * selected directory.
271 int xtree_mode = 0;
273 /* If set, then print to the given file the last directory we were at */
274 static char *last_wd_file = NULL;
275 static char *last_wd_string = NULL;
276 /* Set to 1 to suppress printing the last directory */
277 static int print_last_revert = 0;
279 /* Set to force black and white display at program startup */
280 static gboolean disable_colors = FALSE;
281 /* Force colors, only used by Slang */
282 static gboolean force_colors = FALSE;
284 /* colors specified on the command line: they override any other setting */
285 char *command_line_colors = NULL;
287 /* File name to view if argument was supplied */
288 static const char *view_one_file = NULL;
290 /* File name to edit if argument was supplied */
291 const char *edit_one_file = NULL;
293 /* Line to start the editor on */
294 static int edit_one_file_start_line = 0;
296 /* Used so that widgets know if they are being destroyed or
297 shut down */
298 int midnight_shutdown = 0;
300 /* The user's shell */
301 char *shell = NULL;
303 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
304 char *mc_home = NULL;
306 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
307 char *mc_home_alt = NULL;
309 char cmd_buf[512];
311 static void
312 reload_panelized (WPanel *panel)
314 int i, j;
315 dir_list *list = &panel->dir;
317 if (panel != current_panel)
318 mc_chdir (panel->cwd);
320 for (i = 0, j = 0; i < panel->count; i++) {
321 if (list->list[i].f.marked) {
322 /* Unmark the file in advance. In case the following mc_lstat
323 * fails we are done, else we have to mark the file again
324 * (Note: do_file_mark depends on a valid "list->list [i].buf").
325 * IMO that's the best way to update the panel's summary status
326 * -- Norbert
328 do_file_mark (panel, i, 0);
330 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
331 g_free (list->list[i].fname);
332 continue;
334 if (list->list[i].f.marked)
335 do_file_mark (panel, i, 1);
336 if (j != i)
337 list->list[j] = list->list[i];
338 j++;
340 if (j == 0)
341 panel->count = set_zero_dir (list);
342 else
343 panel->count = j;
345 if (panel != current_panel)
346 mc_chdir (current_panel->cwd);
349 static void
350 update_one_panel_widget (WPanel *panel, int force_update,
351 const char *current_file)
353 int free_pointer;
354 char *my_current_file = NULL;
356 if (force_update & UP_RELOAD) {
357 panel->is_panelized = 0;
358 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
359 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
362 /* If current_file == -1 (an invalid pointer) then preserve selection */
363 if (current_file == UP_KEEPSEL) {
364 free_pointer = 1;
365 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
366 current_file = my_current_file;
367 } else
368 free_pointer = 0;
370 if (panel->is_panelized)
371 reload_panelized (panel);
372 else
373 panel_reload (panel);
375 try_to_select (panel, current_file);
376 panel->dirty = 1;
378 if (free_pointer)
379 g_free (my_current_file);
382 void
383 panel_clean_dir (WPanel *panel)
385 int count = panel->count;
387 panel->count = 0;
388 panel->top_file = 0;
389 panel->selected = 0;
390 panel->marked = 0;
391 panel->dirs_marked = 0;
392 panel->total = 0;
393 panel->searching = 0;
394 panel->is_panelized = 0;
395 panel->dirty = 1;
397 clean_dir (&panel->dir, count);
400 static void
401 update_one_panel (int which, int force_update, const char *current_file)
403 WPanel *panel;
405 if (get_display_type (which) != view_listing)
406 return;
408 panel = (WPanel *) get_panel_widget (which);
409 update_one_panel_widget (panel, force_update, current_file);
412 /* This routine reloads the directory in both panels. It tries to
413 * select current_file in current_panel and other_file in other_panel.
414 * If current_file == -1 then it automatically sets current_file and
415 * other_file to the currently selected files in the panels.
417 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
418 * will not reload the other panel.
420 void
421 update_panels (int force_update, const char *current_file)
423 int reload_other = !(force_update & UP_ONLY_CURRENT);
424 WPanel *panel;
426 update_one_panel (get_current_index (), force_update, current_file);
427 if (reload_other)
428 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
430 if (get_current_type () == view_listing)
431 panel = (WPanel *) get_panel_widget (get_current_index ());
432 else
433 panel = (WPanel *) get_panel_widget (get_other_index ());
435 mc_chdir (panel->cwd);
438 /* Save current stat of directories to avoid reloading the panels */
439 /* when no modifications have taken place */
440 void
441 save_cwds_stat (void)
443 if (fast_reload) {
444 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
445 if (get_other_type () == view_listing)
446 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
450 #ifdef HAVE_SUBSHELL_SUPPORT
451 void
452 do_possible_cd (const char *new_dir)
454 if (!do_cd (new_dir, cd_exact))
455 message (D_ERROR, _("Warning"),
456 _(" The Commander can't change to the directory that \n"
457 " the subshell claims you are in. Perhaps you have \n"
458 " deleted your working directory, or given yourself \n"
459 " extra access permissions with the \"su\" command? "));
462 void
463 do_update_prompt (void)
465 if (update_prompt) {
466 printf ("%s", subshell_prompt);
467 fflush (stdout);
468 update_prompt = 0;
471 #endif /* HAVE_SUBSHELL_SUPPORT */
473 void
474 change_panel (void)
476 free_completions (cmdline);
477 dlg_one_down (midnight_dlg);
480 /* Stop MC main dialog and the current dialog if it exists.
481 * Needed to provide fast exit from MC viewer or editor on shell exit */
482 static void
483 stop_dialogs (void)
485 midnight_dlg->running = 0;
486 if (current_dlg) {
487 current_dlg->running = 0;
491 static int
492 quit_cmd_internal (int quiet)
494 int q = quit;
496 if (quiet || !confirm_exit) {
497 q = 1;
498 } else {
499 if (query_dialog
500 (_(" The Midnight Commander "),
501 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
502 2, _("&Yes"), _("&No")) == 0)
503 q = 1;
505 if (q) {
506 #ifdef HAVE_SUBSHELL_SUPPORT
507 if (!use_subshell)
508 stop_dialogs ();
509 else if ((q = exit_subshell ()))
510 #endif
511 stop_dialogs ();
513 if (q)
514 quit |= 1;
515 return quit;
518 static void
519 quit_cmd (void)
521 quit_cmd_internal (0);
524 void
525 quiet_quit_cmd (void)
527 print_last_revert = 1;
528 quit_cmd_internal (1);
531 /* Wrapper for do_subshell_chdir, check for availability of subshell */
532 void
533 subshell_chdir (const char *directory)
535 #ifdef HAVE_SUBSHELL_SUPPORT
536 if (use_subshell) {
537 if (vfs_current_is_local ())
538 do_subshell_chdir (directory, 0, 1);
540 #endif /* HAVE_SUBSHELL_SUPPORT */
543 void
544 directory_history_add (struct WPanel *panel, const char *dir)
546 char *tmp;
548 tmp = g_strdup (dir);
549 strip_password (tmp, 1);
551 panel->dir_history = list_append_unique (panel->dir_history, tmp);
555 * If we moved to the parent directory move the selection pointer to
556 * the old directory name; If we leave VFS dir, remove FS specificator.
558 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
560 static const char *
561 get_parent_dir_name (const char *cwd, const char *lwd)
563 const char *p;
564 if (strlen (lwd) > strlen (cwd))
565 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
566 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
567 cwd[1] == '\0'))) {
568 return (p + 1);
570 return NULL;
574 * Changes the current directory of the panel.
575 * Don't record change in the directory history.
577 static int
578 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
580 const char *directory;
581 char *olddir;
582 char temp[MC_MAXPATHLEN];
583 char *translated_url;
585 if (cd_type == cd_parse_command) {
586 while (*new_dir == ' ')
587 new_dir++;
590 olddir = g_strdup (panel->cwd);
591 new_dir = translated_url = vfs_translate_url (new_dir);
593 /* Convert *new_path to a suitable pathname, handle ~user */
595 if (cd_type == cd_parse_command) {
596 if (!strcmp (new_dir, "-")) {
597 strcpy (temp, panel->lwd);
598 new_dir = temp;
601 directory = *new_dir ? new_dir : home_dir;
603 if (mc_chdir (directory) == -1) {
604 strcpy (panel->cwd, olddir);
605 g_free (olddir);
606 g_free (translated_url);
607 return 0;
609 g_free (translated_url);
611 /* Success: save previous directory, shutdown status of previous dir */
612 strcpy (panel->lwd, olddir);
613 free_completions (cmdline);
615 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
617 vfs_release_path (olddir);
619 subshell_chdir (panel->cwd);
621 /* Reload current panel */
622 panel_clean_dir (panel);
623 panel->count =
624 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
625 panel->reverse, panel->case_sensitive,
626 panel->exec_first, panel->filter);
627 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
628 load_hint (0);
629 panel->dirty = 1;
630 update_xterm_title_path ();
632 g_free (olddir);
634 return 1;
638 * Changes the current directory of the panel.
639 * Record change in the directory history.
642 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
644 int r;
646 r = _do_panel_cd (panel, new_dir, cd_type);
647 if (r)
648 directory_history_add (panel, panel->cwd);
649 return r;
653 do_cd (const char *new_dir, enum cd_enum exact)
655 return (do_panel_cd (current_panel, new_dir, exact));
658 void
659 directory_history_next (WPanel *panel)
661 GList *nextdir;
663 nextdir = g_list_next (panel->dir_history);
665 if (!nextdir)
666 return;
668 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
669 panel->dir_history = nextdir;
672 void
673 directory_history_prev (WPanel *panel)
675 GList *prevdir;
677 prevdir = g_list_previous (panel->dir_history);
679 if (!prevdir)
680 return;
682 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
683 panel->dir_history = prevdir;
686 void
687 directory_history_list (WPanel *panel)
689 char *s;
691 if (!panel->dir_history)
692 return;
694 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
696 if (!s)
697 return;
699 if (_do_panel_cd (panel, s, cd_exact))
700 directory_history_add (panel, panel->cwd);
701 else
702 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
703 g_free (s);
706 #ifdef HAVE_SUBSHELL_SUPPORT
708 load_prompt (int fd, void *unused)
710 (void) fd;
711 (void) unused;
713 if (!read_subshell_prompt ())
714 return 0;
716 /* Don't actually change the prompt if it's invisible */
717 if (current_dlg == midnight_dlg && command_prompt) {
718 char *tmp_prompt;
719 int prompt_len;
721 tmp_prompt = strip_ctrl_codes (subshell_prompt);
722 prompt_len = str_term_width1 (tmp_prompt);
724 /* Check for prompts too big */
725 if (COLS > 8 && prompt_len > COLS - 8) {
726 tmp_prompt[COLS - 8] = '\0';
727 prompt_len = COLS - 8;
729 prompt = tmp_prompt;
730 label_set_text (the_prompt, prompt);
731 winput_set_origin ((WInput *) cmdline, prompt_len,
732 COLS - prompt_len);
734 /* since the prompt has changed, and we are called from one of the
735 * tty_get_event channels, the prompt updating does not take place
736 * automatically: force a cursor update and a screen refresh
738 update_cursor (midnight_dlg);
739 tty_refresh ();
741 update_prompt = 1;
742 return 0;
744 #endif /* HAVE_SUBSHELL_SUPPORT */
746 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
748 maybe_cd (int move_up_dir)
750 if (navigate_with_arrows) {
751 if (!cmdline->buffer[0]) {
752 if (move_up_dir) {
753 do_cd ("..", cd_exact);
754 return 1;
756 if (S_ISDIR (selection (current_panel)->st.st_mode)
757 || link_isdir (selection (current_panel))) {
758 do_cd (selection (current_panel)->fname, cd_exact);
759 return 1;
763 return 0;
766 static void
767 sort_cmd (void)
769 WPanel *p;
770 sortfn *sort_order;
772 if (!SELECTED_IS_PANEL)
773 return;
775 p = MENU_PANEL;
776 sort_order = sort_box (p->sort_type, &p->reverse,
777 &p->case_sensitive,
778 &p->exec_first);
780 panel_set_sort_order (p, sort_order);
784 static void
785 treebox_cmd (void)
787 char *sel_dir;
789 sel_dir = tree_box (selection (current_panel)->fname);
790 if (sel_dir) {
791 do_cd (sel_dir, cd_exact);
792 g_free (sel_dir);
796 #ifdef LISTMODE_EDITOR
797 static void
798 listmode_cmd (void)
800 char *newmode;
802 if (get_current_type () != view_listing)
803 return;
805 newmode = listmode_edit (current_panel->user_format);
806 if (!newmode)
807 return;
809 g_free (current_panel->user_format);
810 current_panel->list_type = list_user;
811 current_panel->user_format = newmode;
812 set_panel_formats (current_panel);
814 do_refresh ();
816 #endif /* LISTMODE_EDITOR */
818 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
819 static menu_entry LeftMenu[] = {
820 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
821 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
822 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
823 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
824 {' ', "", NULL_HOTKEY, 0},
825 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
826 {' ', "", NULL_HOTKEY, 0},
827 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
828 #ifdef HAVE_CHARSET
829 {' ', "",NULL_HOTKEY, 0},
830 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
831 #endif
832 #ifdef USE_NETCODE
833 {' ', "", NULL_HOTKEY, 0},
834 #ifdef ENABLE_VFS_MCFS
835 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
836 #endif
837 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
838 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
839 #ifdef WITH_SMBFS
840 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
841 #endif
842 #endif
843 {' ', "", NULL_HOTKEY, 0},
844 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
847 static menu_entry RightMenu[] = {
848 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
849 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
850 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
851 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
852 {' ', "", NULL_HOTKEY, 0},
853 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
854 {' ', "", NULL_HOTKEY, 0},
855 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
856 #ifdef HAVE_CHARSET
857 {' ', "",NULL_HOTKEY, 0},
858 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
859 #endif
860 #ifdef USE_NETCODE
861 {' ', "", NULL_HOTKEY, 0},
862 #ifdef ENABLE_VFS_MCFS
863 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
864 #endif
865 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
866 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
867 #ifdef WITH_SMBFS
868 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
869 #endif
870 #endif
871 {' ', "", NULL_HOTKEY, 0},
872 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
875 static menu_entry FileMenu[] = {
876 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
877 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
878 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
879 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
880 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
881 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
882 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
883 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
884 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
885 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
886 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
887 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
888 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
889 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
890 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
891 {' ', "", NULL_HOTKEY, 0},
892 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
893 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
894 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
895 {' ', "", NULL_HOTKEY, 0},
896 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
899 static menu_entry CmdMenu[] = {
900 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
901 /* I know, I'm lazy, but the tree widget when it's not running
902 * as a panel still has some problems, I have not yet finished
903 * the WTree widget port, sorry.
905 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
906 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
907 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
908 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
909 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
910 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
911 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
912 {' ', "", NULL_HOTKEY, 0},
913 {' ', N_("Command &history M-h"), NULL_HOTKEY, history_cmd},
914 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
915 #ifdef USE_VFS
916 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
917 #endif
918 #ifdef WITH_BACKGROUND
919 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
920 #endif
921 {' ', "", NULL_HOTKEY, 0},
922 #ifdef USE_EXT2FSLIB
923 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
924 #endif
925 #ifdef LISTMODE_EDITOR
926 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
927 #endif
928 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
929 {' ', "", NULL_HOTKEY, 0},
930 #endif
931 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
932 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd}
935 /* Must keep in sync with the constants in menu_cmd */
936 static menu_entry OptMenu[] = {
937 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
938 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
939 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
940 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
941 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
942 #ifdef USE_VFS
943 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
944 #endif /* !USE_VFS */
945 {' ', "", NULL_HOTKEY, 0},
946 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
949 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
951 static Menu *MenuBar[5];
953 void
954 init_menu (void)
956 MenuBar[0] =
957 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
958 LeftMenu, menu_entries (LeftMenu),
959 "[Left and Right Menus]");
960 MenuBar[1] =
961 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
962 "[File Menu]");
963 MenuBar[2] =
964 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
965 "[Command Menu]");
966 MenuBar[3] =
967 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
968 "[Options Menu]");
969 MenuBar[4] =
970 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
971 RightMenu, menu_entries (RightMenu),
972 "[Left and Right Menus]");
975 void
976 done_menu (void)
978 int i;
980 for (i = 0; i < 5; i++) {
981 destroy_menu (MenuBar[i]);
985 static void
986 menu_last_selected_cmd (void)
988 the_menubar->active = 1;
989 the_menubar->dropped = drop_menus;
990 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
991 dlg_select_widget (the_menubar);
994 static void
995 menu_cmd (void)
997 if (the_menubar->active)
998 return;
1000 if ((get_current_index () == 0) ^ (!current_panel->active))
1001 the_menubar->selected = 0;
1002 else
1003 the_menubar->selected = 4;
1004 menu_last_selected_cmd ();
1007 /* Flag toggling functions */
1008 void
1009 toggle_fast_reload (void)
1011 fast_reload = !fast_reload;
1012 if (fast_reload_w == 0 && fast_reload) {
1013 message (D_NORMAL, _(" Information "),
1015 (" Using the fast reload option may not reflect the exact \n"
1016 " directory contents. In this case you'll need to do a \n"
1017 " manual reload of the directory. See the man page for \n"
1018 " the details. "));
1019 fast_reload_w = 1;
1023 void
1024 toggle_mix_all_files (void)
1026 mix_all_files = !mix_all_files;
1027 update_panels (UP_RELOAD, UP_KEEPSEL);
1030 void
1031 toggle_show_backup (void)
1033 show_backups = !show_backups;
1034 update_panels (UP_RELOAD, UP_KEEPSEL);
1037 void
1038 toggle_show_hidden (void)
1040 show_dot_files = !show_dot_files;
1041 update_panels (UP_RELOAD, UP_KEEPSEL);
1045 * Just a hack for allowing url-like pathnames to be accepted from the
1046 * command line.
1048 static void
1049 translated_mc_chdir (char *dir)
1051 char *newdir;
1053 newdir = vfs_translate_url (dir);
1054 mc_chdir (newdir);
1055 g_free (newdir);
1058 static void
1059 create_panels (void)
1061 int current_index;
1062 int other_index;
1063 int current_mode;
1064 int other_mode;
1065 char original_dir[1024];
1067 original_dir[0] = 0;
1069 if (boot_current_is_left) {
1070 current_index = 0;
1071 other_index = 1;
1072 current_mode = startup_left_mode;
1073 other_mode = startup_right_mode;
1074 } else {
1075 current_index = 1;
1076 other_index = 0;
1077 current_mode = startup_right_mode;
1078 other_mode = startup_left_mode;
1080 /* Creates the left panel */
1081 if (this_dir) {
1082 if (other_dir) {
1083 /* Ok, user has specified two dirs, save the original one,
1084 * since we may not be able to chdir to the proper
1085 * second directory later
1087 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1089 translated_mc_chdir (this_dir);
1091 set_display_type (current_index, current_mode);
1093 /* The other panel */
1094 if (other_dir) {
1095 if (original_dir[0])
1096 translated_mc_chdir (original_dir);
1097 translated_mc_chdir (other_dir);
1099 set_display_type (other_index, other_mode);
1101 if (startup_left_mode == view_listing) {
1102 current_panel = left_panel;
1103 } else {
1104 if (right_panel)
1105 current_panel = right_panel;
1106 else
1107 current_panel = left_panel;
1110 /* Create the nice widgets */
1111 cmdline = command_new (0, 0, 0);
1112 the_prompt = label_new (0, 0, prompt);
1113 the_prompt->transparent = 1;
1114 the_bar = buttonbar_new (keybar_visible);
1116 the_hint = label_new (0, 0, 0);
1117 the_hint->transparent = 1;
1118 the_hint->auto_adjust_cols = 0;
1119 the_hint->widget.cols = COLS;
1121 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1124 static void
1125 copy_current_pathname (void)
1127 char *cwd_path;
1128 if (!command_prompt)
1129 return;
1131 cwd_path = remove_encoding_from_path (current_panel->cwd);
1132 command_insert (cmdline, cwd_path, 0);
1134 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1135 command_insert (cmdline, PATH_SEP_STR, 0);
1136 g_free (cwd_path);
1139 static void
1140 copy_other_pathname (void)
1142 char *cwd_path;
1144 if (get_other_type () != view_listing)
1145 return;
1147 if (!command_prompt)
1148 return;
1150 cwd_path = remove_encoding_from_path (other_panel->cwd);
1151 command_insert (cmdline, cwd_path, 0);
1153 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1154 command_insert (cmdline, PATH_SEP_STR, 0);
1155 g_free (cwd_path);
1158 static void
1159 copy_readlink (WPanel *panel)
1161 if (!command_prompt)
1162 return;
1163 if (S_ISLNK (selection (panel)->st.st_mode)) {
1164 char buffer[MC_MAXPATHLEN];
1165 char *p =
1166 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1167 int i;
1169 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1170 g_free (p);
1171 if (i > 0) {
1172 buffer[i] = 0;
1173 command_insert (cmdline, buffer, 1);
1178 static void
1179 copy_current_readlink (void)
1181 copy_readlink (current_panel);
1184 static void
1185 copy_other_readlink (void)
1187 if (get_other_type () != view_listing)
1188 return;
1189 copy_readlink (other_panel);
1192 /* Insert the selected file name into the input line */
1193 static void
1194 copy_prog_name (void)
1196 char *tmp;
1197 if (!command_prompt)
1198 return;
1200 if (get_current_type () == view_tree) {
1201 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1202 tmp = tree_selected_name (tree);
1203 } else
1204 tmp = selection (current_panel)->fname;
1206 command_insert (cmdline, tmp, 1);
1209 static void
1210 copy_tagged (WPanel *panel)
1212 int i;
1214 if (!command_prompt)
1215 return;
1216 input_disable_update (cmdline);
1217 if (panel->marked) {
1218 for (i = 0; i < panel->count; i++) {
1219 if (panel->dir.list[i].f.marked)
1220 command_insert (cmdline, panel->dir.list[i].fname, 1);
1222 } else {
1223 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1226 input_enable_update (cmdline);
1229 static void
1230 copy_current_tagged (void)
1232 copy_tagged (current_panel);
1235 static void
1236 copy_other_tagged (void)
1238 if (get_other_type () != view_listing)
1239 return;
1240 copy_tagged (other_panel);
1243 static void
1244 init_labels (void)
1246 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1247 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1248 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1249 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1252 static const key_map ctl_x_map[] = {
1253 {XCTRL ('c'), quit_cmd},
1254 {'d', compare_dirs_cmd},
1255 #ifdef USE_VFS
1256 {'a', reselect_vfs},
1257 #endif /* USE_VFS */
1258 {'p', copy_current_pathname},
1259 {XCTRL ('p'), copy_other_pathname},
1260 {'t', copy_current_tagged},
1261 {XCTRL ('t'), copy_other_tagged},
1262 {'c', chmod_cmd},
1263 {'o', chown_cmd},
1264 {'r', copy_current_readlink},
1265 {XCTRL ('r'), copy_other_readlink},
1266 {'l', link_cmd},
1267 {'s', symlink_cmd},
1268 {XCTRL ('s'), edit_symlink_cmd},
1269 {'i', info_cmd_no_menu},
1270 {'q', quick_cmd_no_menu},
1271 {'h', add2hotlist_cmd},
1272 {'!', external_panelize},
1273 #ifdef WITH_BACKGROUND
1274 {'j', jobs_cmd},
1275 #endif /* WITH_BACKGROUND */
1276 {0, 0}
1279 static int ctl_x_map_enabled = 0;
1281 static void
1282 ctl_x_cmd (void)
1284 ctl_x_map_enabled = 1;
1287 static void
1288 nothing (void)
1292 static const key_map default_map[] = {
1293 {KEY_F (19), menu_last_selected_cmd},
1294 {KEY_F (20), quiet_quit_cmd},
1296 {XCTRL ('@'), smart_dirsize_cmd},
1298 /* Copy useful information to the command line */
1299 {ALT ('a'), copy_current_pathname},
1300 {ALT ('A'), copy_other_pathname},
1302 {ALT ('c'), quick_cd_cmd},
1304 /* To access the directory hotlist */
1305 {XCTRL ('\\'), quick_chdir_cmd},
1307 /* Suspend */
1308 {XCTRL ('z'), suspend_cmd},
1310 /* The filtered view command */
1311 {ALT ('!'), filtered_view_cmd},
1313 /* Find file */
1314 {ALT ('?'), find_cmd},
1316 /* Panel refresh */
1317 {XCTRL ('r'), reread_cmd},
1319 /* Toggle listing between long, user defined and full formats */
1320 {ALT ('t'), toggle_listing_cmd},
1322 /* Swap panels */
1323 {XCTRL ('u'), swap_cmd},
1325 /* View output */
1326 {XCTRL ('o'), view_other_cmd},
1328 /* Control-X keybindings */
1329 {XCTRL ('x'), ctl_x_cmd},
1331 /* Show/hide hidden files */
1332 {ALT ('.'), toggle_show_hidden},
1334 /* Trap dlg's exit commands */
1335 {ESC_CHAR, nothing},
1336 {XCTRL ('c'), nothing},
1337 {XCTRL ('g'), nothing},
1338 {0, 0},
1341 static void
1342 setup_sigwinch (void)
1344 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && defined(SIGWINCH)
1345 struct sigaction act, oact;
1346 act.sa_handler = flag_winch;
1347 sigemptyset (&act.sa_mask);
1348 act.sa_flags = 0;
1349 #ifdef SA_RESTART
1350 act.sa_flags |= SA_RESTART;
1351 #endif
1352 sigaction (SIGWINCH, &act, &oact);
1353 #endif
1356 static void
1357 setup_pre (void)
1359 /* Call all the inits */
1360 #ifdef HAVE_CHARSET
1362 * Don't restrict the output on the screen manager level,
1363 * the translation tables take care of it.
1365 #define full_eight_bits (1)
1366 #define eight_bit_clean (1)
1367 #endif /* !HAVE_CHARSET */
1369 #ifdef HAVE_SLANG
1370 tty_display_8bit (full_eight_bits != 0);
1371 #else
1372 tty_display_8bit (eight_bit_clean != 0);
1373 #endif
1376 static void
1377 init_xterm_support (void)
1379 const char *termvalue;
1381 termvalue = getenv ("TERM");
1382 if (!termvalue || !(*termvalue)) {
1383 fputs (_("The TERM environment variable is unset!\n"), stderr);
1384 exit (1);
1387 /* Check mouse capabilities */
1388 xmouse_seq = tty_tgetstr ("Km");
1390 if (strcmp (termvalue, "cygwin") == 0) {
1391 force_xterm = 1;
1392 use_mouse_p = MOUSE_DISABLED;
1395 if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
1396 || strncmp (termvalue, "rxvt", 4) == 0
1397 || strcmp (termvalue, "Eterm") == 0
1398 || strcmp (termvalue, "dtterm") == 0) {
1399 xterm_flag = 1;
1401 /* Default to the standard xterm sequence */
1402 if (!xmouse_seq) {
1403 xmouse_seq = ESC_STR "[M";
1406 /* Enable mouse unless explicitly disabled by --nomouse */
1407 if (use_mouse_p != MOUSE_DISABLED) {
1408 const char *color_term = getenv ("COLORTERM");
1409 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1410 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1411 strcmp (termvalue, "Eterm") == 0) {
1412 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1413 } else {
1414 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1420 static void
1421 setup_mc (void)
1423 setup_pre ();
1424 init_menu ();
1425 create_panels ();
1426 setup_panels ();
1428 #ifdef HAVE_SUBSHELL_SUPPORT
1429 if (use_subshell)
1430 add_select_channel (subshell_pty, load_prompt, 0);
1431 #endif /* !HAVE_SUBSHELL_SUPPORT */
1433 setup_sigwinch ();
1435 verbose = !((tty_baudrate () < 9600) || tty_is_slow ());
1437 init_mouse ();
1440 static void
1441 setup_dummy_mc ()
1443 char d[MC_MAXPATHLEN];
1445 mc_get_current_wd (d, MC_MAXPATHLEN);
1446 setup_mc ();
1447 mc_chdir (d);
1450 static void check_codeset()
1452 const char *_system_codepage = NULL;
1453 #ifdef HAVE_CHARSET
1454 const char *_source_codepage = NULL;
1455 const char *_display_codepage = NULL;
1456 int profile_changed = 0;
1458 #define CONFY 16
1459 #define CONFX 54
1461 if ( !skip_check_codeset ) {
1463 QuickWidget ecs_widgets [] = {
1464 { quick_button, 4, 6, 13, CONFY, N_("&Skip"),
1465 0, B_EXIT, 0, 0, NULL , NULL, NULL},
1466 { quick_button, 1, 11, 13, CONFY, N_("&Fix it"),
1467 0, B_ENTER, 0, 0, NULL , NULL, NULL},
1468 { quick_checkbox, 1, 13, 11, CONFY, N_("don't ask again"),
1469 11, 0, &skip_check_codeset, NULL, NULL , NULL, NULL},
1470 { quick_label, 2, 30, 3, CONFY, N_("Chosen display charset (Settings->Display bits)\n"
1471 "or source codeset (in mcedit ctrl-t) \n"
1472 "does not match one set via locale. \n"
1473 "Set correct codeset manually or press <<Fix it>> \n"
1474 "to set locale default.\n\n"
1475 "Or set \'don't ask again\' and press <<Skip>>"),
1476 0, 0, 0, 0, NULL , NULL, NULL},
1478 NULL_QuickWidget
1481 QuickDialog ecs =
1482 { CONFX, CONFY, -1, -1, N_(" Confirmation "), "[Confirmation]",
1483 ecs_widgets, 0
1487 _system_codepage = str_detect_termencoding();
1488 _source_codepage = get_codepage_id (source_codepage);
1489 _display_codepage = get_codepage_id (display_codepage);
1491 if ( (strcmp (_system_codepage, _display_codepage)) ||
1492 (strcmp (_system_codepage, _source_codepage)) ) {
1493 if (quick_dialog (&ecs) == B_ENTER){
1494 display_codepage = get_codepage_index (_system_codepage);
1495 cp_display = get_codepage_id (display_codepage);
1496 if ( !strcmp (cp_display, _system_codepage)) {
1497 mc_config_set_string(mc_main_config, "Misc", "display_codepage", cp_display);
1498 mc_config_set_string(mc_main_config, "Misc", "source_codepage", cp_display);
1499 display_codepage = get_codepage_index ( cp_display );
1500 utf8_display = str_isutf8 (_system_codepage);
1501 source_codepage = display_codepage;
1502 cp_source = cp_display;
1503 profile_changed = 1;
1505 } else {
1506 if ( skip_check_codeset ) {
1507 mc_config_set_int(mc_main_config, "Midnight-Commander", "skip_check_codeset", 1);
1508 profile_changed = 1;
1512 if ( profile_changed )
1513 save_configure ();
1515 #else /* HAVE_CHARSET */
1516 _system_codepage = str_detect_termencoding();
1517 utf8_display = str_isutf8 (_system_codepage);
1518 #endif /* HAVE_CHARSET */
1521 static void
1522 done_screen (void)
1524 if (!(quit & SUBSHELL_EXIT))
1525 clr_scr ();
1526 tty_reset_shell_mode ();
1527 tty_noraw_mode ();
1528 tty_keypad (FALSE);
1529 tty_colors_done ();
1532 static void
1533 done_mc (void)
1535 disable_mouse ();
1537 done_menu ();
1539 /* Setup shutdown
1541 * We sync the profiles since the hotlist may have changed, while
1542 * we only change the setup data if we have the auto save feature set
1545 if (auto_save_setup)
1546 save_setup (); /* does also call save_hotlist */
1547 else {
1548 save_hotlist ();
1549 save_panel_types ();
1551 done_screen ();
1552 vfs_add_current_stamps ();
1555 /* This should be called after destroy_dlg since panel widgets
1556 * save their state on the profiles
1558 static void
1559 done_mc_profile (void)
1561 done_setup ();
1564 static cb_ret_t
1565 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1567 int i;
1569 switch (msg) {
1571 case DLG_IDLE:
1572 /* We only need the first idle event */
1573 set_idle_proc (h, 0);
1574 if (auto_menu) {
1575 user_file_menu_cmd ();
1577 return MSG_HANDLED;
1579 case DLG_KEY:
1580 if (ctl_x_map_enabled) {
1581 ctl_x_map_enabled = 0;
1582 for (i = 0; ctl_x_map[i].key_code; i++)
1583 if (parm == ctl_x_map[i].key_code) {
1584 (*ctl_x_map[i].fn) ();
1585 return MSG_HANDLED;
1589 /* FIXME: should handle all menu shortcuts before this point */
1590 if (the_menubar->active)
1591 return MSG_NOT_HANDLED;
1593 if (parm == KEY_F (10)) {
1594 quit_cmd ();
1595 return MSG_HANDLED;
1598 if (parm == '\t')
1599 free_completions (cmdline);
1601 if (parm == '\n') {
1602 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1603 cmdline->buffer[i] == '\t'); i++);
1604 if (cmdline->buffer[i]) {
1605 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1606 return MSG_HANDLED;
1608 stuff (cmdline, "", 0);
1609 cmdline->point = 0;
1612 /* Ctrl-Enter and Alt-Enter */
1613 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1614 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1615 copy_prog_name ();
1616 return MSG_HANDLED;
1619 /* Ctrl-Shift-Enter */
1620 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1621 copy_current_pathname ();
1622 copy_prog_name ();
1623 return MSG_HANDLED;
1626 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1627 && !quote && !current_panel->searching) {
1628 if (!only_leading_plus_minus) {
1629 /* Special treatement, since the input line will eat them */
1630 if (parm == '+') {
1631 select_cmd ();
1632 return MSG_HANDLED;
1635 if (parm == '\\' || parm == '-') {
1636 unselect_cmd ();
1637 return MSG_HANDLED;
1640 if (parm == '*') {
1641 reverse_selection_cmd ();
1642 return MSG_HANDLED;
1644 } else if (!command_prompt || !cmdline->buffer[0]) {
1645 /* Special treatement '+', '-', '\', '*' only when this is
1646 * first char on input line
1649 if (parm == '+') {
1650 select_cmd ();
1651 return MSG_HANDLED;
1654 if (parm == '\\' || parm == '-') {
1655 unselect_cmd ();
1656 return MSG_HANDLED;
1659 if (parm == '*') {
1660 reverse_selection_cmd ();
1661 return MSG_HANDLED;
1665 return MSG_NOT_HANDLED;
1667 case DLG_HOTKEY_HANDLED:
1668 if ((get_current_type () == view_listing) && current_panel->searching) {
1669 current_panel->searching = 0;
1670 current_panel->dirty = 1;
1672 return MSG_HANDLED;
1674 case DLG_UNHANDLED_KEY:
1675 if (command_prompt) {
1676 int v;
1678 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1679 if (v)
1680 return v;
1682 if (ctl_x_map_enabled) {
1683 ctl_x_map_enabled = 0;
1684 for (i = 0; ctl_x_map[i].key_code; i++)
1685 if (parm == ctl_x_map[i].key_code) {
1686 (*ctl_x_map[i].fn) ();
1687 return MSG_HANDLED;
1689 } else {
1690 for (i = 0; default_map[i].key_code; i++) {
1691 if (parm == default_map[i].key_code) {
1692 (*default_map[i].fn) ();
1693 return MSG_HANDLED;
1697 return MSG_NOT_HANDLED;
1699 case DLG_DRAW:
1700 /* We handle the special case of the output lines */
1701 if (console_flag && output_lines)
1702 show_console_contents (output_start_y,
1703 LINES - output_lines - keybar_visible -
1704 1, LINES - keybar_visible - 1);
1705 return MSG_HANDLED;
1707 case DLG_POST_KEY:
1708 if (!the_menubar->active)
1709 update_dirty_panels ();
1710 return MSG_HANDLED;
1712 default:
1713 return default_dlg_callback (h, msg, parm);
1717 /* Show current directory in the xterm title */
1718 void
1719 update_xterm_title_path (void)
1721 const char *path;
1722 char host[BUF_TINY];
1723 char *p;
1724 struct passwd *pw = NULL;
1725 char *login = NULL;
1726 int res = 0;
1727 if (xterm_flag && xterm_title) {
1728 path = strip_home_and_password (current_panel->cwd);
1729 res = gethostname(host, sizeof (host));
1730 if ( res ) { /* On success, res = 0 */
1731 host[0] = '\0';
1732 } else {
1733 host[sizeof (host) - 1] = '\0';
1735 pw = getpwuid(getuid());
1736 if ( pw ) {
1737 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1738 } else {
1739 login = g_strdup (host);
1741 p = g_strdup_printf ("mc [%s]:%s", login, path);
1742 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1743 g_free (login);
1744 g_free (p);
1745 if (!alternate_plus_minus)
1746 numeric_keypad_mode ();
1747 fflush (stdout);
1752 * Load new hint and display it.
1753 * IF force is not 0, ignore the timeout.
1755 void
1756 load_hint (int force)
1758 char *hint;
1760 if (!the_hint->widget.parent)
1761 return;
1763 if (!message_visible) {
1764 label_set_text (the_hint, 0);
1765 return;
1768 if ((hint = get_random_hint (force))) {
1769 if (*hint)
1770 set_hintbar (hint);
1771 g_free (hint);
1772 } else {
1773 char text[BUF_SMALL];
1775 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1776 VERSION);
1777 set_hintbar (text);
1781 static void
1782 setup_panels_and_run_mc (void)
1784 add_widget (midnight_dlg, the_menubar);
1785 add_widget (midnight_dlg, get_panel_widget (0));
1786 add_widget (midnight_dlg, get_panel_widget (1));
1787 add_widget (midnight_dlg, the_hint);
1788 load_hint (1);
1789 add_widget (midnight_dlg, cmdline);
1790 add_widget (midnight_dlg, the_prompt);
1791 add_widget (midnight_dlg, the_bar);
1792 init_labels ();
1794 if (boot_current_is_left)
1795 dlg_select_widget (get_panel_widget (0));
1796 else
1797 dlg_select_widget (get_panel_widget (1));
1799 /* Run the Midnight Commander if no file was specified in the command line */
1800 run_dlg (midnight_dlg);
1803 /* result must be free'd (I think this should go in util.c) */
1804 static char *
1805 prepend_cwd_on_local (const char *filename)
1807 char *d;
1808 int l;
1810 if (vfs_file_is_local (filename)) {
1811 if (*filename == PATH_SEP) /* an absolute pathname */
1812 return g_strdup (filename);
1813 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1814 mc_get_current_wd (d, MC_MAXPATHLEN);
1815 l = strlen (d);
1816 d[l++] = PATH_SEP;
1817 strcpy (d + l, filename);
1818 canonicalize_pathname (d);
1819 return d;
1820 } else
1821 return g_strdup (filename);
1824 static int
1825 mc_maybe_editor_or_viewer (void)
1827 if (!(view_one_file || edit_one_file))
1828 return 0;
1830 setup_dummy_mc ();
1832 /* Invoke the internal view/edit routine with:
1833 * the default processing and forcing the internal viewer/editor
1835 if (view_one_file) {
1836 char *path = NULL;
1837 path = prepend_cwd_on_local (view_one_file);
1838 view_file (path, 0, 1);
1839 g_free (path);
1841 #ifdef USE_INTERNAL_EDIT
1842 else {
1843 edit_file (edit_one_file, edit_one_file_start_line);
1845 #endif /* USE_INTERNAL_EDIT */
1846 midnight_shutdown = 1;
1847 done_mc ();
1848 return 1;
1851 /* Run the main dialog that occupies the whole screen */
1852 static void
1853 do_nc (void)
1855 const int midnight_colors[4] =
1857 NORMAL_COLOR, /* NORMALC */
1858 REVERSE_COLOR, /* FOCUSC */
1859 INPUT_COLOR, /* HOT_NORMALC */
1860 NORMAL_COLOR /* HOT_FOCUSC */
1863 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1864 "[main]", NULL, DLG_WANT_IDLE);
1866 /* start check display_codepage and source_codepage */
1867 check_codeset();
1869 /* Check if we were invoked as an editor or file viewer */
1870 if (!mc_maybe_editor_or_viewer ()) {
1871 setup_mc ();
1872 setup_panels_and_run_mc ();
1874 /* Program end */
1875 midnight_shutdown = 1;
1877 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1878 if (last_wd_file && vfs_current_is_local ()) {
1879 last_wd_string = g_strdup (current_panel->cwd);
1881 done_mc ();
1883 destroy_dlg (midnight_dlg);
1884 current_panel = 0;
1885 done_mc_profile ();
1888 /* POSIX version. The only version we support. */
1889 static void
1890 OS_Setup (void)
1892 const char *shell_env = getenv ("SHELL");
1893 const char *mc_libdir;
1895 if ((shell_env == NULL) || (shell_env[0] == '\0')) {
1896 struct passwd *pwd;
1897 pwd = getpwuid (geteuid ());
1898 if (pwd != NULL)
1899 shell = g_strdup (pwd->pw_shell);
1900 } else
1901 shell = g_strdup (shell_env);
1903 if ((shell == NULL) || (shell[0] == '\0')) {
1904 g_free (shell);
1905 shell = g_strdup ("/bin/sh");
1908 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1909 /* and can be overriden by the MC_DATADIR environment variable */
1910 mc_libdir = getenv ("MC_DATADIR");
1911 if (mc_libdir != NULL) {
1912 mc_home = g_strdup (mc_libdir);
1913 mc_home_alt = g_strdup (SYSCONFDIR);
1914 } else {
1915 mc_home = g_strdup (SYSCONFDIR);
1916 mc_home_alt = g_strdup (DATADIR);
1919 /* This variable is used by the subshell */
1920 home_dir = getenv ("HOME");
1922 if (!home_dir)
1923 home_dir = mc_home;
1927 static void
1928 sigchld_handler_no_subshell (int sig)
1930 #ifdef __linux__
1931 int pid, status;
1933 if (!console_flag)
1934 return;
1936 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1937 the value of console_flag never changed, we could simply not install
1938 this handler at all if (!console_flag && !use_subshell). */
1940 /* That comment is no longer true. We need to wait() on a sigchld
1941 handler (that's at least what the tarfs code expects currently). */
1943 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1945 if (pid == cons_saver_pid) {
1947 if (WIFSTOPPED (status)) {
1948 /* Someone has stopped cons.saver - restart it */
1949 kill (pid, SIGCONT);
1950 } else {
1951 /* cons.saver has died - disable console saving */
1952 handle_console (CONSOLE_DONE);
1953 console_flag = 0;
1956 /* If we got here, some other child exited; ignore it */
1957 #endif /* __linux__ */
1959 (void) sig;
1962 static void
1963 init_sigchld (void)
1965 struct sigaction sigchld_action;
1967 sigchld_action.sa_handler =
1968 #ifdef HAVE_SUBSHELL_SUPPORT
1969 use_subshell ? sigchld_handler :
1970 #endif /* HAVE_SUBSHELL_SUPPORT */
1971 sigchld_handler_no_subshell;
1973 sigemptyset (&sigchld_action.sa_mask);
1975 #ifdef SA_RESTART
1976 sigchld_action.sa_flags = SA_RESTART;
1977 #else
1978 sigchld_action.sa_flags = 0;
1979 #endif /* !SA_RESTART */
1981 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1982 #ifdef HAVE_SUBSHELL_SUPPORT
1984 * This may happen on QNX Neutrino 6, where SA_RESTART
1985 * is defined but not implemented. Fallback to no subshell.
1987 use_subshell = 0;
1988 #endif /* HAVE_SUBSHELL_SUPPORT */
1992 static void
1993 print_mc_usage (poptContext ctx, FILE *stream)
1995 int leftColWidth;
1997 poptSetOtherOptionHelp (ctx,
1998 _("[flags] [this_dir] [other_panel_dir]\n"));
2000 /* print help for options */
2001 leftColWidth = poptPrintHelp (ctx, stream, 0);
2002 fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
2003 _("Set initial line number for the internal editor"));
2004 fputs (_
2005 ("\n"
2006 "Please send any bug reports (including the output of `mc -V')\n"
2007 "to mc-devel@gnome.org\n"), stream);
2008 show_version (0);
2011 static void
2012 print_color_usage (void)
2015 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
2016 * and editmarked. To preserve translations, lines should be split.
2018 /* TRANSLATORS: don't translate keywords and names of colors */
2019 fputs (_
2020 ("--colors KEYWORD={FORE},{BACK}\n\n"
2021 "{FORE} and {BACK} can be omitted, and the default will be used\n"
2022 "\n" "Keywords:\n"
2023 " Global: errors, reverse, gauge, input, viewunderline\n"
2024 " File display: normal, selected, marked, markselect\n"
2025 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
2026 " errdhotfocus\n"
2027 " Menus: menu, menuhot, menusel, menuhotsel\n"
2028 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
2029 " editlinestate\n"), stdout);
2030 fputs (_
2032 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
2033 " File types: directory, executable, link, stalelink, device, special, core\n"
2034 "\n" "Colors:\n"
2035 " black, gray, red, brightred, green, brightgreen, brown,\n"
2036 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
2037 " brightcyan, lightgray and white\n\n"), stdout);
2040 static void
2041 process_args (poptContext ctx, int c, const char *option_arg)
2043 switch (c) {
2044 case 'V':
2045 show_version (1);
2046 exit (0);
2047 break;
2049 case 'c':
2050 disable_colors = FALSE;
2051 force_colors = TRUE; /* for S-Lang only */
2052 break;
2054 case 'f':
2055 printf ("%s (%s)\n", mc_home, mc_home_alt);
2056 exit (0);
2057 break;
2059 #ifdef USE_NETCODE
2060 case 'l':
2061 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) option_arg);
2062 #ifdef WITH_SMBFS
2063 smbfs_set_debugf (option_arg);
2064 #endif /* WITH_SMBFS */
2065 break;
2067 #ifdef WITH_SMBFS
2068 case 'D':
2069 smbfs_set_debug (atoi (option_arg));
2070 break;
2071 #endif /* WITH_SMBFS */
2072 #endif /* USE_NETCODE */
2074 case 'd':
2075 use_mouse_p = MOUSE_DISABLED;
2076 break;
2078 #ifdef HAVE_SUBSHELL_SUPPORT
2079 case 'u':
2080 use_subshell = 0;
2081 break;
2082 #endif /* HAVE_SUBSHELL_SUPPORT */
2084 case 'H':
2085 print_color_usage ();
2086 exit (0);
2087 break;
2089 case 'h':
2090 print_mc_usage (ctx, stdout);
2091 exit (0);
2095 static const struct poptOption argument_table[] = {
2096 /* generic options */
2097 {"help", 'h', POPT_ARG_NONE, {NULL}, 'h',
2098 N_("Displays this help message"), NULL},
2099 {"version", 'V', POPT_ARG_NONE, {NULL}, 'V',
2100 N_("Displays the current version"), NULL},
2102 /* terminal options */
2103 {"xterm", 'x', POPT_ARG_NONE, {&force_xterm}, 0,
2104 N_("Forces xterm features"), NULL},
2105 {"nomouse", 'd', POPT_ARG_NONE, {NULL}, 'd',
2106 N_("Disable mouse support in text version"), NULL},
2107 #if defined(HAVE_SLANG)
2108 {"termcap", 't', 0, {&SLtt_Try_Termcap}, 0,
2109 N_("Tries to use termcap instead of terminfo"), NULL},
2110 #endif
2111 {"resetsoft", 'k', POPT_ARG_NONE, {&reset_hp_softkeys}, 0,
2112 N_("Resets soft keys on HP terminals"), NULL},
2113 {"slow", 's', POPT_ARG_NONE, {&slow_terminal}, 0,
2114 N_("To run on slow terminals"), NULL},
2115 {"stickchars", 'a', POPT_ARG_NONE, {&ugly_line_drawing}, 0,
2116 N_("Use stickchars to draw"), NULL},
2118 /* color options */
2119 {"nocolor", 'b', POPT_ARG_NONE, {&disable_colors}, 0,
2120 N_("Requests to run in black and white"), NULL},
2121 {"color", 'c', POPT_ARG_NONE, {NULL}, 'c',
2122 N_("Request to run in color mode"), NULL},
2123 {"colors", 'C', POPT_ARG_STRING, {&command_line_colors}, 0,
2124 N_("Specifies a color configuration"), NULL},
2125 {"help-colors", 'H', POPT_ARG_NONE, {NULL}, 'H',
2126 N_("Displays a help screen on how to change the color scheme"), NULL},
2128 /* debug options */
2129 #ifdef USE_NETCODE
2130 {"ftplog", 'l', POPT_ARG_STRING, {NULL}, 'l',
2131 N_("Log ftp dialog to specified file"), NULL},
2132 #ifdef WITH_SMBFS
2133 {"debuglevel", 'D', POPT_ARG_STRING, {NULL}, 'D',
2134 N_("Set debug level"), NULL},
2135 #endif
2136 #endif
2138 /* options for wrappers */
2139 {"datadir", 'f', POPT_ARG_NONE, {NULL}, 'f',
2140 N_("Print data directory"), NULL},
2141 {"printwd", 'P', POPT_ARG_STRING, {&last_wd_file}, 0,
2142 N_("Print last working directory to specified file"), NULL},
2144 /* subshell options */
2145 #ifdef HAVE_SUBSHELL_SUPPORT
2146 {"subshell", 'U', POPT_ARG_NONE, {&use_subshell}, 0,
2147 N_("Enables subshell support (default)"), NULL},
2148 {"nosubshell", 'u', POPT_ARG_NONE, {NULL}, 'u',
2149 N_("Disables subshell support"), NULL},
2150 #endif
2152 /* single file operations */
2153 {"view", 'v', POPT_ARG_STRING, {&view_one_file}, 0,
2154 N_("Launches the file viewer on a file"), NULL},
2155 #ifdef USE_INTERNAL_EDIT
2156 {"edit", 'e', POPT_ARG_STRING, {&edit_one_file}, 0,
2157 N_("Edits one file"), NULL},
2158 #endif
2160 {NULL, '\0', 0, {NULL}, 0, NULL , NULL}
2163 static void
2164 handle_args (int argc, char *argv[])
2166 char *tmp;
2167 poptContext ctx;
2168 const char *base;
2169 int c;
2171 ctx =
2172 poptGetContext ("mc", argc, argv, argument_table,
2173 POPT_CONTEXT_NO_EXEC);
2175 while ((c = poptGetNextOpt (ctx)) > 0) {
2176 process_args (ctx, c, poptGetOptArg (ctx));
2179 if (c < -1) {
2180 print_mc_usage (ctx, stderr);
2181 fprintf (stderr, "%s: %s\n",
2182 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2183 poptStrerror (c));
2184 exit (1);
2187 tmp = poptGetArg (ctx);
2190 * Check for special invocation names mcedit and mcview,
2191 * if none apply then set the current directory and the other
2192 * directory from the command line arguments
2194 base = x_basename (argv[0]);
2195 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2196 edit_one_file = "";
2197 if (tmp) {
2199 * Check for filename:lineno, followed by an optional colon.
2200 * This format is used by many programs (especially compilers)
2201 * in error messages and warnings. It is supported so that
2202 * users can quickly copy and paste file locations.
2204 char *end = tmp + strlen (tmp), *p = end;
2205 if (p > tmp && p[-1] == ':')
2206 p--;
2207 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2208 p--;
2209 if (tmp < p && p < end && p[-1] == ':') {
2210 struct stat st;
2211 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2213 * Check that the file before the colon actually exists.
2214 * If it doesn't exist, revert to the old behavior.
2216 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2217 edit_one_file = fname;
2218 edit_one_file_start_line = atoi (p);
2219 } else {
2220 g_free (fname);
2221 goto try_plus_filename;
2223 } else {
2224 try_plus_filename:
2225 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2226 int start_line = atoi (tmp);
2227 if (start_line > 0) {
2228 char *file = poptGetArg (ctx);
2229 if (file) {
2230 tmp = file;
2231 edit_one_file_start_line = start_line;
2235 edit_one_file = g_strdup (tmp);
2238 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2239 if (tmp)
2240 view_one_file = g_strdup (tmp);
2241 else {
2242 fputs ("No arguments given to the viewer\n", stderr);
2243 exit (1);
2245 } else {
2246 /* sets the current dir and the other dir */
2247 if (tmp) {
2248 this_dir = g_strdup (tmp);
2249 if ((tmp = poptGetArg (ctx)))
2250 other_dir = g_strdup (tmp);
2254 poptFreeContext (ctx);
2258 main (int argc, char *argv[])
2260 struct stat s;
2261 char *mc_dir;
2263 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2264 setlocale (LC_ALL, "");
2265 bindtextdomain ("mc", LOCALEDIR);
2266 textdomain ("mc");
2268 /* Set up temporary directory */
2269 mc_tmpdir ();
2271 OS_Setup ();
2273 str_init_strings (NULL);
2275 vfs_init ();
2277 #ifdef USE_INTERNAL_EDIT
2278 edit_stack_init ();
2279 #endif
2281 #ifdef HAVE_SLANG
2282 SLtt_Ignore_Beep = 1;
2283 #endif
2285 handle_args (argc, argv);
2287 /* NOTE: This has to be called before tty_init or whatever routine
2288 calls any define_sequence */
2289 init_key ();
2291 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2292 handle_console (CONSOLE_INIT);
2294 #ifdef HAVE_SUBSHELL_SUPPORT
2295 /* Don't use subshell when invoked as viewer or editor */
2296 if (edit_one_file || view_one_file)
2297 use_subshell = 0;
2299 if (use_subshell)
2300 subshell_get_console_attributes ();
2301 #endif /* HAVE_SUBSHELL_SUPPORT */
2303 /* Install the SIGCHLD handler; must be done before init_subshell() */
2304 init_sigchld ();
2306 /* We need this, since ncurses endwin () doesn't restore the signals */
2307 save_stop_handler ();
2309 /* Must be done before init_subshell, to set up the terminal size: */
2310 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2311 tty_init ((gboolean) slow_terminal, (gboolean) ugly_line_drawing);
2313 load_setup ();
2315 tty_init_colors (disable_colors, force_colors);
2316 dlg_set_default_colors ();
2318 /* create home directory */
2319 /* do it after the screen library initialization to show the error message */
2320 mc_dir = concat_dir_and_file (home_dir, MC_BASE);
2321 canonicalize_pathname (mc_dir);
2322 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2323 && mkdir (mc_dir, 0700) != 0)
2324 message (D_ERROR, _("Warning"),
2325 _("Cannot create %s directory"), mc_dir);
2326 g_free (mc_dir);
2328 init_xterm_support ();
2330 #ifdef HAVE_SUBSHELL_SUPPORT
2331 /* Done here to ensure that the subshell doesn't */
2332 /* inherit the file descriptors opened below, etc */
2333 if (use_subshell)
2334 init_subshell ();
2336 #endif /* HAVE_SUBSHELL_SUPPORT */
2338 /* Removing this from the X code let's us type C-c */
2339 load_key_defs ();
2341 /* Also done after init_subshell, to save any shell init file messages */
2342 if (console_flag)
2343 handle_console (CONSOLE_SAVE);
2345 if (alternate_plus_minus)
2346 application_keypad_mode ();
2348 #ifdef HAVE_SUBSHELL_SUPPORT
2349 if (use_subshell) {
2350 prompt = strip_ctrl_codes (subshell_prompt);
2351 if (!prompt)
2352 prompt = "";
2353 } else
2354 #endif /* HAVE_SUBSHELL_SUPPORT */
2355 prompt = (geteuid () == 0) ? "# " : "$ ";
2357 /* Program main loop */
2358 if (!midnight_shutdown)
2359 do_nc ();
2361 /* Save the tree store */
2362 tree_store_save ();
2364 /* Virtual File System shutdown */
2365 vfs_shut ();
2367 flush_extension_file (); /* does only free memory */
2369 tty_shutdown ();
2371 if (console_flag && !(quit & SUBSHELL_EXIT))
2372 handle_console (CONSOLE_RESTORE);
2373 if (alternate_plus_minus)
2374 numeric_keypad_mode ();
2376 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2378 if (console_flag)
2379 handle_console (CONSOLE_DONE);
2380 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2382 if (last_wd_file && last_wd_string && !print_last_revert
2383 && !edit_one_file && !view_one_file) {
2384 int last_wd_fd =
2385 open (last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2386 S_IRUSR | S_IWUSR);
2388 if (last_wd_fd != -1) {
2389 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2390 close (last_wd_fd);
2393 g_free (last_wd_string);
2395 g_free (mc_home_alt);
2396 g_free (mc_home);
2397 g_free (shell);
2399 done_key ();
2400 #ifdef HAVE_CHARSET
2401 free_codepages_list ();
2402 #endif
2403 str_uninit_strings ();
2405 g_free (this_dir);
2406 g_free (other_dir);
2408 #ifdef USE_INTERNAL_EDIT
2409 edit_stack_free ();
2410 #endif
2412 return 0;