Ticket #305 (dont work rename/copy on some chars)
[midnight-commander.git] / src / main.c
blobf9418f94ff7ec1866c58e99bf8369136cbd2b015
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 <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
40 #include "global.h"
41 #include "tty.h"
42 #include "dir.h"
43 #include "color.h"
44 #include "dialog.h"
45 #include "menu.h"
46 #include "panel.h"
47 #include "main.h"
48 #include "win.h"
49 #include "mouse.h"
50 #include "option.h"
51 #include "tree.h"
52 #include "treestore.h"
53 #include "cons.saver.h"
54 #include "subshell.h"
55 #include "key.h" /* For init_key() and mi_getch() */
56 #include "setup.h" /* save_setup() */
57 #include "../src/mcconfig/mcconfig.h"
58 #include "boxes.h" /* sort_box() */
59 #include "layout.h"
60 #include "cmd.h" /* Normal commands */
61 #include "hotlist.h"
62 #include "panelize.h"
63 #include "learn.h" /* learn_keys() */
64 #include "listmode.h"
65 #include "execute.h"
66 #include "ext.h" /* For flush_extension_file() */
67 #include "strutil.h"
69 /* Listbox for the command history feature */
70 #include "widget.h"
71 #include "command.h"
72 #include "wtools.h"
74 #include "chmod.h"
75 #include "chown.h"
76 #include "achown.h"
78 #ifdef WITH_SMBFS
79 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
80 #endif
82 #ifdef USE_INTERNAL_EDIT
83 # include "../edit/edit.h"
84 #endif
86 #ifdef HAVE_CHARSET
87 #include "charsets.h"
88 #endif /* HAVE_CHARSET */
90 #ifdef USE_VFS
91 #include "../vfs/gc.h"
92 #endif
94 #include "popt.h"
96 /* When the modes are active, left_panel, right_panel and tree_panel */
97 /* Point to a proper data structure. You should check with the functions */
98 /* get_current_type and get_other_type the types of the panels before using */
99 /* This pointer variables */
101 /* The structures for the panels */
102 WPanel *left_panel = NULL;
103 WPanel *right_panel = NULL;
105 /* The pointer to the tree */
106 WTree *the_tree = NULL;
108 /* The Menubar */
109 struct WMenu *the_menubar = NULL;
111 /* Pointers to the selected and unselected panel */
112 WPanel *current_panel = NULL;
114 /* Set if the command is being run from the "Right" menu */
115 int is_right = 0;
117 /* Set when main loop should be terminated */
118 volatile int quit = 0;
120 /* Set if you want the possible completions dialog for the first time */
121 int show_all_if_ambiguous = 0;
123 /* Set when cd symlink following is desirable (bash mode) */
124 int cd_symlinks = 1;
126 /* If set then dialogs just clean the screen when refreshing, else */
127 /* they do a complete refresh, refreshing all the parts of the program */
128 int fast_refresh = 0;
130 /* If true, marking a files moves the cursor down */
131 int mark_moves_down = 1;
133 /* If true, at startup the user-menu is invoked */
134 int auto_menu = 0;
136 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
137 and M-- and keypad + / - */
138 int alternate_plus_minus = 0;
140 /* If true, then the +, - and \ keys have their special meaning only if the
141 * command line is emtpy, otherwise they behave like regular letters
143 int only_leading_plus_minus = 1;
145 int pause_after_run = pause_on_dumb_terminals;
147 /* It true saves the setup when quitting */
148 int auto_save_setup = 1;
150 #ifndef HAVE_CHARSET
151 /* If true, allow characters in the range 160-255 */
152 int eight_bit_clean = 1;
155 * If true, also allow characters in the range 128-159.
156 * This is reported to break on many terminals (xterm, qansi-m).
158 int full_eight_bits = 0;
160 #endif /* !HAVE_CHARSET */
163 * If utf-8 terminal utf8_display = 1
164 * Display bits set UTF-8
167 int utf8_display = 0;
169 /* If true use the internal viewer */
170 int use_internal_view = 1;
172 /* Have we shown the fast-reload warning in the past? */
173 int fast_reload_w = 0;
175 /* Move page/item? When clicking on the top or bottom of a panel */
176 int mouse_move_pages = 1;
178 /* If true: l&r arrows are used to chdir if the input line is empty */
179 int navigate_with_arrows = 0;
181 /* If true use +, -, | for line drawing */
182 int force_ugly_line_drawing = 0;
184 /* If true program softkeys (HP terminals only) on startup and after every
185 command ran in the subshell to the description found in the termcap/terminfo
186 database */
187 int reset_hp_softkeys = 0;
189 /* The prompt */
190 const char *prompt = NULL;
192 /* The widget where we draw the prompt */
193 WLabel *the_prompt;
195 /* The hint bar */
196 WLabel *the_hint;
198 /* The button bar */
199 WButtonBar *the_bar;
201 /* For slow terminals */
202 int slow_terminal = 0;
204 /* Mouse type: GPM, xterm or none */
205 Mouse_Type use_mouse_p = MOUSE_NONE;
207 /* If true, assume we are running on an xterm terminal */
208 static int force_xterm = 0;
210 /* If on, default for "No" in delete operations */
211 int safe_delete = 0;
213 /* Controls screen clearing before an exec */
214 int clear_before_exec = 1;
216 /* Asks for confirmation before deleting a file */
217 int confirm_delete = 1;
219 /* Asks for confirmation before deleting a hotlist entry */
220 int confirm_directory_hotlist_delete = 1;
222 /* Asks for confirmation before overwriting a file */
223 int confirm_overwrite = 1;
225 /* Asks for confirmation before executing a program by pressing enter */
226 int confirm_execute = 0;
228 /* Asks for confirmation before leaving the program */
229 int confirm_exit = 1;
231 /* Asks for confirmation when using F3 to view a directory and there
232 are tagged files */
233 int confirm_view_dir = 0;
235 /* This flag indicates if the pull down menus by default drop down */
236 int drop_menus = 0;
238 /* The dialog handle for the main program */
239 Dlg_head *midnight_dlg;
241 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
242 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
243 int update_prompt = 0;
245 /* The home directory */
246 const char *home_dir = NULL;
248 /* The value of the other directory, only used when loading the setup */
249 char *other_dir = NULL;
251 /* Only used at program boot */
252 int boot_current_is_left = 1;
254 static char *this_dir = NULL;
256 /* If this is true, then when browsing the tree the other window will
257 * automatically reload it's directory with the contents of the currently
258 * selected directory.
260 int xtree_mode = 0;
262 /* If set, then print to the given file the last directory we were at */
263 static char *last_wd_file = NULL;
264 static char *last_wd_string = NULL;
265 /* Set to 1 to suppress printing the last directory */
266 static int print_last_revert = 0;
268 /* Force colors, only used by Slang */
269 int force_colors = 0;
271 /* colors specified on the command line: they override any other setting */
272 char *command_line_colors = NULL;
274 /* File name to view if argument was supplied */
275 static const char *view_one_file = NULL;
277 /* File name to edit if argument was supplied */
278 const char *edit_one_file = NULL;
280 /* Line to start the editor on */
281 static int edit_one_file_start_line = 0;
283 /* Used so that widgets know if they are being destroyed or
284 shut down */
285 int midnight_shutdown = 0;
287 /* The user's shell */
288 const char *shell = NULL;
290 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
291 char *mc_home = NULL;
293 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
294 char *mc_home_alt = NULL;
296 char cmd_buf[512];
298 #ifdef USE_INTERNAL_EDIT
299 int edit_stack_iterator = 0;
300 struct edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
301 #endif
303 static void
304 reload_panelized (WPanel *panel)
306 int i, j;
307 dir_list *list = &panel->dir;
309 if (panel != current_panel)
310 mc_chdir (panel->cwd);
312 for (i = 0, j = 0; i < panel->count; i++) {
313 if (list->list[i].f.marked) {
314 /* Unmark the file in advance. In case the following mc_lstat
315 * fails we are done, else we have to mark the file again
316 * (Note: do_file_mark depends on a valid "list->list [i].buf").
317 * IMO that's the best way to update the panel's summary status
318 * -- Norbert
320 do_file_mark (panel, i, 0);
322 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
323 g_free (list->list[i].fname);
324 continue;
326 if (list->list[i].f.marked)
327 do_file_mark (panel, i, 1);
328 if (j != i)
329 list->list[j] = list->list[i];
330 j++;
332 if (j == 0)
333 panel->count = set_zero_dir (list);
334 else
335 panel->count = j;
337 if (panel != current_panel)
338 mc_chdir (current_panel->cwd);
341 static void
342 update_one_panel_widget (WPanel *panel, int force_update,
343 const char *current_file)
345 int free_pointer;
346 char *my_current_file = NULL;
348 if (force_update & UP_RELOAD) {
349 panel->is_panelized = 0;
350 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
351 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
354 /* If current_file == -1 (an invalid pointer) then preserve selection */
355 if (current_file == UP_KEEPSEL) {
356 free_pointer = 1;
357 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
358 current_file = my_current_file;
359 } else
360 free_pointer = 0;
362 if (panel->is_panelized)
363 reload_panelized (panel);
364 else
365 panel_reload (panel);
367 try_to_select (panel, current_file);
368 panel->dirty = 1;
370 if (free_pointer)
371 g_free (my_current_file);
374 void
375 panel_clean_dir (WPanel *panel)
377 int count = panel->count;
379 panel->count = 0;
380 panel->top_file = 0;
381 panel->selected = 0;
382 panel->marked = 0;
383 panel->dirs_marked = 0;
384 panel->total = 0;
385 panel->searching = 0;
386 panel->is_panelized = 0;
387 panel->dirty = 1;
389 clean_dir (&panel->dir, count);
392 static void
393 update_one_panel (int which, int force_update, const char *current_file)
395 WPanel *panel;
397 if (get_display_type (which) != view_listing)
398 return;
400 panel = (WPanel *) get_panel_widget (which);
401 update_one_panel_widget (panel, force_update, current_file);
404 /* This routine reloads the directory in both panels. It tries to
405 * select current_file in current_panel and other_file in other_panel.
406 * If current_file == -1 then it automatically sets current_file and
407 * other_file to the currently selected files in the panels.
409 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
410 * will not reload the other panel.
412 void
413 update_panels (int force_update, const char *current_file)
415 int reload_other = !(force_update & UP_ONLY_CURRENT);
416 WPanel *panel;
418 update_one_panel (get_current_index (), force_update, current_file);
419 if (reload_other)
420 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
422 if (get_current_type () == view_listing)
423 panel = (WPanel *) get_panel_widget (get_current_index ());
424 else
425 panel = (WPanel *) get_panel_widget (get_other_index ());
427 mc_chdir (panel->cwd);
430 /* Save current stat of directories to avoid reloading the panels */
431 /* when no modifications have taken place */
432 void
433 save_cwds_stat (void)
435 if (fast_reload) {
436 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
437 if (get_other_type () == view_listing)
438 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
442 #ifdef HAVE_SUBSHELL_SUPPORT
443 void
444 do_possible_cd (const char *new_dir)
446 if (!do_cd (new_dir, cd_exact))
447 message (D_ERROR, _("Warning"),
448 _(" The Commander can't change to the directory that \n"
449 " the subshell claims you are in. Perhaps you have \n"
450 " deleted your working directory, or given yourself \n"
451 " extra access permissions with the \"su\" command? "));
454 void
455 do_update_prompt (void)
457 if (update_prompt) {
458 printf ("%s", subshell_prompt);
459 fflush (stdout);
460 update_prompt = 0;
463 #endif /* HAVE_SUBSHELL_SUPPORT */
465 void
466 change_panel (void)
468 free_completions (cmdline);
469 dlg_one_down (midnight_dlg);
472 /* Stop MC main dialog and the current dialog if it exists.
473 * Needed to provide fast exit from MC viewer or editor on shell exit */
474 static void
475 stop_dialogs (void)
477 midnight_dlg->running = 0;
478 if (current_dlg) {
479 current_dlg->running = 0;
483 static int
484 quit_cmd_internal (int quiet)
486 int q = quit;
488 if (quiet || !confirm_exit) {
489 q = 1;
490 } else {
491 if (query_dialog
492 (_(" The Midnight Commander "),
493 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
494 2, _("&Yes"), _("&No")) == 0)
495 q = 1;
497 if (q) {
498 #ifdef HAVE_SUBSHELL_SUPPORT
499 if (!use_subshell)
500 stop_dialogs ();
501 else if ((q = exit_subshell ()))
502 #endif
503 stop_dialogs ();
505 if (q)
506 quit |= 1;
507 return quit;
510 static void
511 quit_cmd (void)
513 quit_cmd_internal (0);
516 void
517 quiet_quit_cmd (void)
519 print_last_revert = 1;
520 quit_cmd_internal (1);
524 * Touch window and refresh window functions
527 /* This routine untouches the first line on both panels in order */
528 /* to avoid the refreshing the menu bar */
530 void
531 repaint_screen (void)
533 do_refresh ();
534 mc_refresh ();
537 /* Wrapper for do_subshell_chdir, check for availability of subshell */
538 void
539 subshell_chdir (const char *directory)
541 #ifdef HAVE_SUBSHELL_SUPPORT
542 if (use_subshell) {
543 if (vfs_current_is_local ())
544 do_subshell_chdir (directory, 0, 1);
546 #endif /* HAVE_SUBSHELL_SUPPORT */
549 void
550 directory_history_add (struct WPanel *panel, const char *dir)
552 char *tmp;
554 tmp = g_strdup (dir);
555 strip_password (tmp, 1);
557 panel->dir_history = list_append_unique (panel->dir_history, tmp);
561 * If we moved to the parent directory move the selection pointer to
562 * the old directory name; If we leave VFS dir, remove FS specificator.
564 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
566 static const char *
567 get_parent_dir_name (const char *cwd, const char *lwd)
569 const char *p;
570 if (strlen (lwd) > strlen (cwd))
571 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
572 ((gsize)strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
573 cwd[1] == '\0'))) {
574 return (p + 1);
576 return NULL;
580 * Changes the current directory of the panel.
581 * Don't record change in the directory history.
583 static int
584 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
586 const char *directory;
587 char *olddir;
588 char temp[MC_MAXPATHLEN];
589 char *translated_url;
591 if (cd_type == cd_parse_command) {
592 while (*new_dir == ' ')
593 new_dir++;
596 olddir = g_strdup (panel->cwd);
597 new_dir = translated_url = vfs_translate_url (new_dir);
599 /* Convert *new_path to a suitable pathname, handle ~user */
601 if (cd_type == cd_parse_command) {
602 if (!strcmp (new_dir, "-")) {
603 strcpy (temp, panel->lwd);
604 new_dir = temp;
607 directory = *new_dir ? new_dir : home_dir;
609 if (mc_chdir (directory) == -1) {
610 strcpy (panel->cwd, olddir);
611 g_free (olddir);
612 g_free (translated_url);
613 return 0;
615 g_free (translated_url);
617 /* Success: save previous directory, shutdown status of previous dir */
618 strcpy (panel->lwd, olddir);
619 free_completions (cmdline);
621 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
623 vfs_release_path (olddir);
625 subshell_chdir (panel->cwd);
627 /* Reload current panel */
628 panel_clean_dir (panel);
629 panel->count =
630 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
631 panel->reverse, panel->case_sensitive,
632 panel->exec_first, panel->filter);
633 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
634 load_hint (0);
635 panel->dirty = 1;
636 update_xterm_title_path ();
638 g_free (olddir);
640 return 1;
644 * Changes the current directory of the panel.
645 * Record change in the directory history.
648 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
650 int r;
652 r = _do_panel_cd (panel, new_dir, cd_type);
653 if (r)
654 directory_history_add (panel, panel->cwd);
655 return r;
659 do_cd (const char *new_dir, enum cd_enum exact)
661 return (do_panel_cd (current_panel, new_dir, exact));
664 void
665 directory_history_next (WPanel *panel)
667 GList *nextdir;
669 nextdir = g_list_next (panel->dir_history);
671 if (!nextdir)
672 return;
674 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
675 panel->dir_history = nextdir;
678 void
679 directory_history_prev (WPanel *panel)
681 GList *prevdir;
683 prevdir = g_list_previous (panel->dir_history);
685 if (!prevdir)
686 return;
688 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
689 panel->dir_history = prevdir;
692 void
693 directory_history_list (WPanel *panel)
695 char *s;
697 if (!panel->dir_history)
698 return;
700 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
702 if (!s)
703 return;
705 if (_do_panel_cd (panel, s, cd_exact))
706 directory_history_add (panel, panel->cwd);
707 else
708 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
709 g_free (s);
712 #ifdef HAVE_SUBSHELL_SUPPORT
714 load_prompt (int fd, void *unused)
716 (void) fd;
717 (void) unused;
719 if (!read_subshell_prompt ())
720 return 0;
722 /* Don't actually change the prompt if it's invisible */
723 if (current_dlg == midnight_dlg && command_prompt) {
724 char *tmp_prompt;
725 int prompt_len;
727 tmp_prompt = strip_ctrl_codes (subshell_prompt);
728 prompt_len = str_term_width1 (tmp_prompt);
730 /* Check for prompts too big */
731 if (COLS > 8 && prompt_len > COLS - 8) {
732 tmp_prompt[COLS - 8] = '\0';
733 prompt_len = COLS - 8;
735 prompt = tmp_prompt;
736 label_set_text (the_prompt, prompt);
737 winput_set_origin ((WInput *) cmdline, prompt_len,
738 COLS - prompt_len);
740 /* since the prompt has changed, and we are called from one of the
741 * get_event channels, the prompt updating does not take place
742 * automatically: force a cursor update and a screen refresh
744 update_cursor (midnight_dlg);
745 mc_refresh ();
747 update_prompt = 1;
748 return 0;
750 #endif /* HAVE_SUBSHELL_SUPPORT */
752 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
754 maybe_cd (int move_up_dir)
756 if (navigate_with_arrows) {
757 if (!cmdline->buffer[0]) {
758 if (move_up_dir) {
759 do_cd ("..", cd_exact);
760 return 1;
762 if (S_ISDIR (selection (current_panel)->st.st_mode)
763 || link_isdir (selection (current_panel))) {
764 do_cd (selection (current_panel)->fname, cd_exact);
765 return 1;
769 return 0;
772 static void
773 sort_cmd (void)
775 WPanel *p;
776 sortfn *sort_order;
778 if (!SELECTED_IS_PANEL)
779 return;
781 p = MENU_PANEL;
782 sort_order = sort_box (p->sort_type, &p->reverse,
783 &p->case_sensitive,
784 &p->exec_first);
786 panel_set_sort_order (p, sort_order);
790 static void
791 treebox_cmd (void)
793 char *sel_dir;
795 sel_dir = tree_box (selection (current_panel)->fname);
796 if (sel_dir) {
797 do_cd (sel_dir, cd_exact);
798 g_free (sel_dir);
802 #ifdef LISTMODE_EDITOR
803 static void
804 listmode_cmd (void)
806 char *newmode;
808 if (get_current_type () != view_listing)
809 return;
811 newmode = listmode_edit (current_panel->user_format);
812 if (!newmode)
813 return;
815 g_free (current_panel->user_format);
816 current_panel->list_type = list_user;
817 current_panel->user_format = newmode;
818 set_panel_formats (current_panel);
820 do_refresh ();
822 #endif /* LISTMODE_EDITOR */
824 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
825 static menu_entry LeftMenu[] = {
826 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
827 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
828 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
829 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
830 {' ', "", NULL_HOTKEY, 0},
831 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
832 {' ', "", NULL_HOTKEY, 0},
833 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
834 #ifdef HAVE_CHARSET
835 {' ', "",NULL_HOTKEY, 0},
836 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
837 #endif
838 #ifdef USE_NETCODE
839 {' ', "", NULL_HOTKEY, 0},
840 #ifdef ENABLE_VFS_MCFS
841 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
842 #endif
843 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
844 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
845 #ifdef WITH_SMBFS
846 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
847 #endif
848 #endif
849 {' ', "", NULL_HOTKEY, 0},
850 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
853 static menu_entry RightMenu[] = {
854 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
855 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
856 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
857 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
858 {' ', "", NULL_HOTKEY, 0},
859 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
860 {' ', "", NULL_HOTKEY, 0},
861 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
862 #ifdef HAVE_CHARSET
863 {' ', "",NULL_HOTKEY, 0},
864 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
865 #endif
866 #ifdef USE_NETCODE
867 {' ', "", NULL_HOTKEY, 0},
868 #ifdef ENABLE_VFS_MCFS
869 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
870 #endif
871 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
872 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
873 #ifdef WITH_SMBFS
874 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
875 #endif
876 #endif
877 {' ', "", NULL_HOTKEY, 0},
878 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
881 static menu_entry FileMenu[] = {
882 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
883 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
884 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
885 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
886 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
887 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
888 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
889 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
890 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
891 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
892 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
893 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
894 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
895 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
896 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
897 {' ', "", NULL_HOTKEY, 0},
898 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
899 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
900 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
901 {' ', "", NULL_HOTKEY, 0},
902 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
905 static menu_entry CmdMenu[] = {
906 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
907 /* I know, I'm lazy, but the tree widget when it's not running
908 * as a panel still has some problems, I have not yet finished
909 * the WTree widget port, sorry.
911 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
912 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
913 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
914 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
915 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
916 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
917 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
918 {' ', "", NULL_HOTKEY, 0},
919 {' ', N_("command &History"), NULL_HOTKEY, history_cmd},
920 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
921 #ifdef USE_VFS
922 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
923 #endif
924 #ifdef WITH_BACKGROUND
925 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
926 #endif
927 {' ', "", NULL_HOTKEY, 0},
928 #ifdef USE_EXT2FSLIB
929 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
930 #endif
931 #ifdef LISTMODE_EDITOR
932 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
933 #endif
934 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
935 {' ', "", NULL_HOTKEY, 0},
936 #endif
937 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
938 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd}
941 /* Must keep in sync with the constants in menu_cmd */
942 static menu_entry OptMenu[] = {
943 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
944 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
945 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
946 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
947 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
948 #ifdef USE_VFS
949 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
950 #endif /* !USE_VFS */
951 {' ', "", NULL_HOTKEY, 0},
952 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
955 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
957 static Menu *MenuBar[5];
959 void
960 init_menu (void)
962 MenuBar[0] =
963 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
964 LeftMenu, menu_entries (LeftMenu),
965 "[Left and Right Menus]");
966 MenuBar[1] =
967 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
968 "[File Menu]");
969 MenuBar[2] =
970 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
971 "[Command Menu]");
972 MenuBar[3] =
973 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
974 "[Options Menu]");
975 MenuBar[4] =
976 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
977 RightMenu, menu_entries (RightMenu),
978 "[Left and Right Menus]");
981 void
982 done_menu (void)
984 int i;
986 for (i = 0; i < 5; i++) {
987 destroy_menu (MenuBar[i]);
991 static void
992 menu_last_selected_cmd (void)
994 the_menubar->active = 1;
995 the_menubar->dropped = drop_menus;
996 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
997 dlg_select_widget (the_menubar);
1000 static void
1001 menu_cmd (void)
1003 if (the_menubar->active)
1004 return;
1006 if ((get_current_index () == 0) ^ (!current_panel->active))
1007 the_menubar->selected = 0;
1008 else
1009 the_menubar->selected = 4;
1010 menu_last_selected_cmd ();
1013 /* Flag toggling functions */
1014 void
1015 toggle_fast_reload (void)
1017 fast_reload = !fast_reload;
1018 if (fast_reload_w == 0 && fast_reload) {
1019 message (D_NORMAL, _(" Information "),
1021 (" Using the fast reload option may not reflect the exact \n"
1022 " directory contents. In this case you'll need to do a \n"
1023 " manual reload of the directory. See the man page for \n"
1024 " the details. "));
1025 fast_reload_w = 1;
1029 void
1030 toggle_mix_all_files (void)
1032 mix_all_files = !mix_all_files;
1033 update_panels (UP_RELOAD, UP_KEEPSEL);
1036 void
1037 toggle_show_backup (void)
1039 show_backups = !show_backups;
1040 update_panels (UP_RELOAD, UP_KEEPSEL);
1043 void
1044 toggle_show_hidden (void)
1046 show_dot_files = !show_dot_files;
1047 update_panels (UP_RELOAD, UP_KEEPSEL);
1051 * Just a hack for allowing url-like pathnames to be accepted from the
1052 * command line.
1054 static void
1055 translated_mc_chdir (char *dir)
1057 char *newdir;
1059 newdir = vfs_translate_url (dir);
1060 mc_chdir (newdir);
1061 g_free (newdir);
1064 static void
1065 create_panels (void)
1067 int current_index;
1068 int other_index;
1069 int current_mode;
1070 int other_mode;
1071 char original_dir[1024];
1073 original_dir[0] = 0;
1075 if (boot_current_is_left) {
1076 current_index = 0;
1077 other_index = 1;
1078 current_mode = startup_left_mode;
1079 other_mode = startup_right_mode;
1080 } else {
1081 current_index = 1;
1082 other_index = 0;
1083 current_mode = startup_right_mode;
1084 other_mode = startup_left_mode;
1086 /* Creates the left panel */
1087 if (this_dir) {
1088 if (other_dir) {
1089 /* Ok, user has specified two dirs, save the original one,
1090 * since we may not be able to chdir to the proper
1091 * second directory later
1093 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1095 translated_mc_chdir (this_dir);
1097 set_display_type (current_index, current_mode);
1099 /* The other panel */
1100 if (other_dir) {
1101 if (original_dir[0])
1102 translated_mc_chdir (original_dir);
1103 translated_mc_chdir (other_dir);
1105 set_display_type (other_index, other_mode);
1107 if (startup_left_mode == view_listing) {
1108 current_panel = left_panel;
1109 } else {
1110 if (right_panel)
1111 current_panel = right_panel;
1112 else
1113 current_panel = left_panel;
1116 /* Create the nice widgets */
1117 cmdline = command_new (0, 0, 0);
1118 the_prompt = label_new (0, 0, prompt);
1119 the_prompt->transparent = 1;
1120 the_bar = buttonbar_new (keybar_visible);
1122 the_hint = label_new (0, 0, 0);
1123 the_hint->transparent = 1;
1124 the_hint->auto_adjust_cols = 0;
1125 the_hint->widget.cols = COLS;
1127 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1130 static void
1131 copy_current_pathname (void)
1133 char *cwd_path;
1134 if (!command_prompt)
1135 return;
1137 cwd_path = remove_encoding_from_path (current_panel->cwd);
1138 command_insert (cmdline, cwd_path, 0);
1140 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1141 command_insert (cmdline, PATH_SEP_STR, 0);
1142 g_free (cwd_path);
1145 static void
1146 copy_other_pathname (void)
1148 char *cwd_path;
1150 if (get_other_type () != view_listing)
1151 return;
1153 if (!command_prompt)
1154 return;
1156 cwd_path = remove_encoding_from_path (other_panel->cwd);
1157 command_insert (cmdline, cwd_path, 0);
1159 if (cwd_path [strlen (cwd_path ) - 1] != PATH_SEP)
1160 command_insert (cmdline, PATH_SEP_STR, 0);
1161 g_free (cwd_path);
1164 static void
1165 copy_readlink (WPanel *panel)
1167 if (!command_prompt)
1168 return;
1169 if (S_ISLNK (selection (panel)->st.st_mode)) {
1170 char buffer[MC_MAXPATHLEN];
1171 char *p =
1172 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1173 int i;
1175 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1176 g_free (p);
1177 if (i > 0) {
1178 buffer[i] = 0;
1179 command_insert (cmdline, buffer, 1);
1184 static void
1185 copy_current_readlink (void)
1187 copy_readlink (current_panel);
1190 static void
1191 copy_other_readlink (void)
1193 if (get_other_type () != view_listing)
1194 return;
1195 copy_readlink (other_panel);
1198 /* Insert the selected file name into the input line */
1199 static void
1200 copy_prog_name (void)
1202 char *tmp;
1203 if (!command_prompt)
1204 return;
1206 if (get_current_type () == view_tree) {
1207 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1208 tmp = tree_selected_name (tree);
1209 } else
1210 tmp = selection (current_panel)->fname;
1212 command_insert (cmdline, tmp, 1);
1215 static void
1216 copy_tagged (WPanel *panel)
1218 int i;
1220 if (!command_prompt)
1221 return;
1222 input_disable_update (cmdline);
1223 if (panel->marked) {
1224 for (i = 0; i < panel->count; i++) {
1225 if (panel->dir.list[i].f.marked)
1226 command_insert (cmdline, panel->dir.list[i].fname, 1);
1228 } else {
1229 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1232 input_enable_update (cmdline);
1235 static void
1236 copy_current_tagged (void)
1238 copy_tagged (current_panel);
1241 static void
1242 copy_other_tagged (void)
1244 if (get_other_type () != view_listing)
1245 return;
1246 copy_tagged (other_panel);
1249 static void
1250 init_labels (void)
1252 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1253 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1254 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1255 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1258 static const key_map ctl_x_map[] = {
1259 {XCTRL ('c'), quit_cmd},
1260 {'d', compare_dirs_cmd},
1261 #ifdef USE_VFS
1262 {'a', reselect_vfs},
1263 #endif /* USE_VFS */
1264 {'p', copy_current_pathname},
1265 {XCTRL ('p'), copy_other_pathname},
1266 {'t', copy_current_tagged},
1267 {XCTRL ('t'), copy_other_tagged},
1268 {'c', chmod_cmd},
1269 {'o', chown_cmd},
1270 {'r', copy_current_readlink},
1271 {XCTRL ('r'), copy_other_readlink},
1272 {'l', link_cmd},
1273 {'s', symlink_cmd},
1274 {XCTRL ('s'), edit_symlink_cmd},
1275 {'i', info_cmd_no_menu},
1276 {'q', quick_cmd_no_menu},
1277 {'h', add2hotlist_cmd},
1278 {'!', external_panelize},
1279 #ifdef WITH_BACKGROUND
1280 {'j', jobs_cmd},
1281 #endif /* WITH_BACKGROUND */
1282 {0, 0}
1285 static int ctl_x_map_enabled = 0;
1287 static void
1288 ctl_x_cmd (void)
1290 ctl_x_map_enabled = 1;
1293 static void
1294 nothing (void)
1298 static const key_map default_map[] = {
1299 {KEY_F (19), menu_last_selected_cmd},
1300 {KEY_F (20), quiet_quit_cmd},
1302 {XCTRL ('@'), single_dirsize_cmd},
1304 /* Copy useful information to the command line */
1305 {ALT ('a'), copy_current_pathname},
1306 {ALT ('A'), copy_other_pathname},
1308 {ALT ('c'), quick_cd_cmd},
1310 /* To access the directory hotlist */
1311 {XCTRL ('\\'), quick_chdir_cmd},
1313 /* Suspend */
1314 {XCTRL ('z'), suspend_cmd},
1316 /* The filtered view command */
1317 {ALT ('!'), filtered_view_cmd},
1319 /* Find file */
1320 {ALT ('?'), find_cmd},
1322 /* Panel refresh */
1323 {XCTRL ('r'), reread_cmd},
1325 /* Toggle listing between long, user defined and full formats */
1326 {ALT ('t'), toggle_listing_cmd},
1328 /* Swap panels */
1329 {XCTRL ('u'), swap_cmd},
1331 /* View output */
1332 {XCTRL ('o'), view_other_cmd},
1334 /* Control-X keybindings */
1335 {XCTRL ('x'), ctl_x_cmd},
1337 /* Trap dlg's exit commands */
1338 {ESC_CHAR, nothing},
1339 {XCTRL ('c'), nothing},
1340 {XCTRL ('g'), nothing},
1341 {0, 0},
1344 static void
1345 setup_sigwinch (void)
1347 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && defined(SIGWINCH)
1348 struct sigaction act, oact;
1349 act.sa_handler = flag_winch;
1350 sigemptyset (&act.sa_mask);
1351 act.sa_flags = 0;
1352 #ifdef SA_RESTART
1353 act.sa_flags |= SA_RESTART;
1354 #endif
1355 sigaction (SIGWINCH, &act, &oact);
1356 #endif
1359 static void
1360 setup_pre (void)
1362 /* Call all the inits */
1363 #ifdef HAVE_CHARSET
1365 * Don't restrict the output on the screen manager level,
1366 * the translation tables take care of it.
1368 #define full_eight_bits (1)
1369 #define eight_bit_clean (1)
1370 #endif /* !HAVE_CHARSET */
1372 #ifndef HAVE_SLANG
1373 meta (stdscr, eight_bit_clean);
1374 #else
1375 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1376 #endif
1379 static void
1380 init_xterm_support (void)
1382 const char *termvalue;
1384 termvalue = getenv ("TERM");
1385 if (!termvalue || !(*termvalue)) {
1386 fputs (_("The TERM environment variable is unset!\n"), stderr);
1387 exit (1);
1390 /* Check mouse capabilities */
1391 xmouse_seq = tty_tgetstr ("Km");
1393 if (strcmp (termvalue, "cygwin") == 0) {
1394 force_xterm = 1;
1395 use_mouse_p = MOUSE_DISABLED;
1398 if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
1399 || strncmp (termvalue, "rxvt", 4) == 0
1400 || strcmp (termvalue, "Eterm") == 0
1401 || strcmp (termvalue, "dtterm") == 0) {
1402 xterm_flag = 1;
1404 /* Default to the standard xterm sequence */
1405 if (!xmouse_seq) {
1406 xmouse_seq = ESC_STR "[M";
1409 /* Enable mouse unless explicitly disabled by --nomouse */
1410 if (use_mouse_p != MOUSE_DISABLED) {
1411 const char *color_term = getenv ("COLORTERM");
1412 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1413 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1414 strcmp (termvalue, "Eterm") == 0) {
1415 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1416 } else {
1417 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1423 static void
1424 setup_mc (void)
1426 setup_pre ();
1427 init_menu ();
1428 create_panels ();
1429 setup_panels ();
1431 #ifdef HAVE_SUBSHELL_SUPPORT
1432 if (use_subshell)
1433 add_select_channel (subshell_pty, load_prompt, 0);
1434 #endif /* !HAVE_SUBSHELL_SUPPORT */
1436 setup_sigwinch ();
1438 if (baudrate () < 9600 || slow_terminal) {
1439 verbose = 0;
1441 init_mouse ();
1444 static void
1445 setup_dummy_mc ()
1447 char d[MC_MAXPATHLEN];
1449 mc_get_current_wd (d, MC_MAXPATHLEN);
1450 setup_mc ();
1451 mc_chdir (d);
1454 static void
1455 done_mc (void)
1457 disable_mouse ();
1459 done_menu ();
1461 /* Setup shutdown
1463 * We sync the profiles since the hotlist may have changed, while
1464 * we only change the setup data if we have the auto save feature set
1467 if (auto_save_setup)
1468 save_setup (); /* does also call save_hotlist */
1469 else {
1470 save_hotlist ();
1471 save_panel_types ();
1473 done_screen ();
1474 vfs_add_current_stamps ();
1477 /* This should be called after destroy_dlg since panel widgets
1478 * save their state on the profiles
1480 static void
1481 done_mc_profile (void)
1483 done_setup ();
1486 static cb_ret_t
1487 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1489 int i;
1491 switch (msg) {
1493 case DLG_IDLE:
1494 /* We only need the first idle event */
1495 set_idle_proc (h, 0);
1496 if (auto_menu) {
1497 user_file_menu_cmd ();
1499 return MSG_HANDLED;
1501 case DLG_KEY:
1502 if (ctl_x_map_enabled) {
1503 ctl_x_map_enabled = 0;
1504 for (i = 0; ctl_x_map[i].key_code; i++)
1505 if (parm == ctl_x_map[i].key_code) {
1506 (*ctl_x_map[i].fn) ();
1507 return MSG_HANDLED;
1511 /* FIXME: should handle all menu shortcuts before this point */
1512 if (the_menubar->active)
1513 return MSG_NOT_HANDLED;
1515 if (parm == KEY_F (10)) {
1516 quit_cmd ();
1517 return MSG_HANDLED;
1520 if (parm == '\t')
1521 free_completions (cmdline);
1523 if (parm == '\n') {
1524 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1525 cmdline->buffer[i] == '\t'); i++);
1526 if (cmdline->buffer[i]) {
1527 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1528 return MSG_HANDLED;
1530 stuff (cmdline, "", 0);
1531 cmdline->point = 0;
1534 /* Ctrl-Enter and Alt-Enter */
1535 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1536 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1537 copy_prog_name ();
1538 return MSG_HANDLED;
1541 /* Ctrl-Shift-Enter */
1542 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1543 copy_current_pathname ();
1544 copy_prog_name ();
1545 return MSG_HANDLED;
1548 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1549 && !quote && !current_panel->searching) {
1550 if (!only_leading_plus_minus) {
1551 /* Special treatement, since the input line will eat them */
1552 if (parm == '+') {
1553 select_cmd ();
1554 return MSG_HANDLED;
1557 if (parm == '\\' || parm == '-') {
1558 unselect_cmd ();
1559 return MSG_HANDLED;
1562 if (parm == '*') {
1563 reverse_selection_cmd ();
1564 return MSG_HANDLED;
1566 } else if (!command_prompt || !cmdline->buffer[0]) {
1567 /* Special treatement '+', '-', '\', '*' only when this is
1568 * first char on input line
1571 if (parm == '+') {
1572 select_cmd ();
1573 return MSG_HANDLED;
1576 if (parm == '\\' || parm == '-') {
1577 unselect_cmd ();
1578 return MSG_HANDLED;
1581 if (parm == '*') {
1582 reverse_selection_cmd ();
1583 return MSG_HANDLED;
1587 return MSG_NOT_HANDLED;
1589 case DLG_HOTKEY_HANDLED:
1590 if ((get_current_type () == view_listing) && current_panel->searching) {
1591 current_panel->searching = 0;
1592 current_panel->dirty = 1;
1594 return MSG_HANDLED;
1596 case DLG_UNHANDLED_KEY:
1597 if (command_prompt) {
1598 int v;
1600 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1601 if (v)
1602 return v;
1604 if (ctl_x_map_enabled) {
1605 ctl_x_map_enabled = 0;
1606 for (i = 0; ctl_x_map[i].key_code; i++)
1607 if (parm == ctl_x_map[i].key_code) {
1608 (*ctl_x_map[i].fn) ();
1609 return MSG_HANDLED;
1611 } else {
1612 for (i = 0; default_map[i].key_code; i++) {
1613 if (parm == default_map[i].key_code) {
1614 (*default_map[i].fn) ();
1615 return MSG_HANDLED;
1619 return MSG_NOT_HANDLED;
1621 case DLG_DRAW:
1622 /* We handle the special case of the output lines */
1623 if (console_flag && output_lines)
1624 show_console_contents (output_start_y,
1625 LINES - output_lines - keybar_visible -
1626 1, LINES - keybar_visible - 1);
1627 return MSG_HANDLED;
1629 case DLG_POST_KEY:
1630 if (!the_menubar->active)
1631 update_dirty_panels ();
1632 return MSG_HANDLED;
1634 default:
1635 return default_dlg_callback (h, msg, parm);
1639 /* Show current directory in the xterm title */
1640 void
1641 update_xterm_title_path (void)
1643 const char *p;
1645 if (xterm_flag && xterm_title) {
1646 p = strip_home_and_password (current_panel->cwd);
1647 fprintf (stdout, "\33]0;mc - %s\7", str_term_form (p));
1648 if (!alternate_plus_minus)
1649 numeric_keypad_mode ();
1650 fflush (stdout);
1655 * Load new hint and display it.
1656 * IF force is not 0, ignore the timeout.
1658 void
1659 load_hint (int force)
1661 char *hint;
1663 if (!the_hint->widget.parent)
1664 return;
1666 if (!message_visible) {
1667 label_set_text (the_hint, 0);
1668 return;
1671 if ((hint = get_random_hint (force))) {
1672 if (*hint)
1673 set_hintbar (hint);
1674 g_free (hint);
1675 } else {
1676 char text[BUF_SMALL];
1678 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1679 VERSION);
1680 set_hintbar (text);
1684 static void
1685 setup_panels_and_run_mc (void)
1687 add_widget (midnight_dlg, the_menubar);
1688 add_widget (midnight_dlg, get_panel_widget (0));
1689 add_widget (midnight_dlg, get_panel_widget (1));
1690 add_widget (midnight_dlg, the_hint);
1691 load_hint (1);
1692 add_widget (midnight_dlg, cmdline);
1693 add_widget (midnight_dlg, the_prompt);
1694 add_widget (midnight_dlg, the_bar);
1695 init_labels ();
1697 if (boot_current_is_left)
1698 dlg_select_widget (get_panel_widget (0));
1699 else
1700 dlg_select_widget (get_panel_widget (1));
1702 /* Run the Midnight Commander if no file was specified in the command line */
1703 run_dlg (midnight_dlg);
1706 /* result must be free'd (I think this should go in util.c) */
1707 static char *
1708 prepend_cwd_on_local (const char *filename)
1710 char *d;
1711 int l;
1713 if (vfs_file_is_local (filename)) {
1714 if (*filename == PATH_SEP) /* an absolute pathname */
1715 return g_strdup (filename);
1716 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1717 mc_get_current_wd (d, MC_MAXPATHLEN);
1718 l = strlen (d);
1719 d[l++] = PATH_SEP;
1720 strcpy (d + l, filename);
1721 canonicalize_pathname (d);
1722 return d;
1723 } else
1724 return g_strdup (filename);
1727 static int
1728 mc_maybe_editor_or_viewer (void)
1730 if (!(view_one_file || edit_one_file))
1731 return 0;
1733 setup_dummy_mc ();
1735 /* Invoke the internal view/edit routine with:
1736 * the default processing and forcing the internal viewer/editor
1738 if (view_one_file) {
1739 char *path = NULL;
1740 path = prepend_cwd_on_local (view_one_file);
1741 view_file (path, 0, 1);
1742 g_free (path);
1744 #ifdef USE_INTERNAL_EDIT
1745 else {
1746 edit_file (edit_one_file, edit_one_file_start_line);
1748 #endif /* USE_INTERNAL_EDIT */
1749 midnight_shutdown = 1;
1750 done_mc ();
1751 return 1;
1754 /* Run the main dialog that occupies the whole screen */
1755 static void
1756 do_nc (void)
1758 int midnight_colors[4];
1760 midnight_colors[0] = NORMAL_COLOR; /* NORMALC */
1761 midnight_colors[1] = REVERSE_COLOR; /* FOCUSC */
1762 midnight_colors[2] = INPUT_COLOR; /* HOT_NORMALC */
1763 midnight_colors[3] = NORMAL_COLOR; /* HOT_FOCUSC */
1765 midnight_dlg =
1766 create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1767 "[main]", NULL, DLG_WANT_IDLE);
1769 /* Check if we were invoked as an editor or file viewer */
1770 if (mc_maybe_editor_or_viewer ())
1771 return;
1773 setup_mc ();
1775 setup_panels_and_run_mc ();
1777 /* Program end */
1778 midnight_shutdown = 1;
1780 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1781 if (last_wd_file && vfs_current_is_local ()) {
1782 last_wd_string = g_strdup (current_panel->cwd);
1784 done_mc ();
1786 destroy_dlg (midnight_dlg);
1787 current_panel = 0;
1788 done_mc_profile ();
1791 /* POSIX version. The only version we support. */
1792 static void
1793 OS_Setup (void)
1795 const char *mc_libdir;
1796 shell = getenv ("SHELL");
1797 if (!shell || !*shell) {
1798 struct passwd *pwd;
1799 pwd = getpwuid (geteuid ());
1800 if (pwd != NULL)
1801 shell = g_strdup (pwd->pw_shell);
1803 if (!shell || !*shell)
1804 shell = "/bin/sh";
1806 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1807 /* and can be overriden by the MC_DATADIR environment variable */
1808 if ((mc_libdir = getenv ("MC_DATADIR")) != NULL) {
1809 mc_home = g_strdup (mc_libdir);
1810 } else {
1811 mc_home = g_strdup (SYSCONFDIR);
1813 mc_home_alt = mc_libdir != NULL ? g_strdup (SYSCONFDIR) : g_strdup (DATADIR);
1816 static void
1817 sigchld_handler_no_subshell (int sig)
1819 #ifdef __linux__
1820 int pid, status;
1822 if (!console_flag)
1823 return;
1825 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1826 the value of console_flag never changed, we could simply not install
1827 this handler at all if (!console_flag && !use_subshell). */
1829 /* That comment is no longer true. We need to wait() on a sigchld
1830 handler (that's at least what the tarfs code expects currently). */
1832 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1834 if (pid == cons_saver_pid) {
1836 if (WIFSTOPPED (status)) {
1837 /* Someone has stopped cons.saver - restart it */
1838 kill (pid, SIGCONT);
1839 } else {
1840 /* cons.saver has died - disable console saving */
1841 handle_console (CONSOLE_DONE);
1842 console_flag = 0;
1845 /* If we got here, some other child exited; ignore it */
1846 #endif /* __linux__ */
1848 (void) sig;
1851 static void
1852 init_sigchld (void)
1854 struct sigaction sigchld_action;
1856 sigchld_action.sa_handler =
1857 #ifdef HAVE_SUBSHELL_SUPPORT
1858 use_subshell ? sigchld_handler :
1859 #endif /* HAVE_SUBSHELL_SUPPORT */
1860 sigchld_handler_no_subshell;
1862 sigemptyset (&sigchld_action.sa_mask);
1864 #ifdef SA_RESTART
1865 sigchld_action.sa_flags = SA_RESTART;
1866 #else
1867 sigchld_action.sa_flags = 0;
1868 #endif /* !SA_RESTART */
1870 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1871 #ifdef HAVE_SUBSHELL_SUPPORT
1873 * This may happen on QNX Neutrino 6, where SA_RESTART
1874 * is defined but not implemented. Fallback to no subshell.
1876 use_subshell = 0;
1877 #endif /* HAVE_SUBSHELL_SUPPORT */
1881 static void
1882 print_mc_usage (poptContext ctx, FILE *stream)
1884 int leftColWidth;
1886 poptSetOtherOptionHelp (ctx,
1887 _("[flags] [this_dir] [other_panel_dir]\n"));
1889 /* print help for options */
1890 leftColWidth = poptPrintHelp (ctx, stream, 0);
1891 fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
1892 _("Set initial line number for the internal editor"));
1893 fputs (_
1894 ("\n"
1895 "Please send any bug reports (including the output of `mc -V')\n"
1896 "to mc-devel@gnome.org\n"), stream);
1897 show_version (0);
1900 static void
1901 print_color_usage (void)
1904 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
1905 * and editmarked. To preserve translations, lines should be split.
1907 /* TRANSLATORS: don't translate keywords and names of colors */
1908 fputs (_
1909 ("--colors KEYWORD={FORE},{BACK}\n\n"
1910 "{FORE} and {BACK} can be omitted, and the default will be used\n"
1911 "\n" "Keywords:\n"
1912 " Global: errors, reverse, gauge, input, viewunderline\n"
1913 " File display: normal, selected, marked, markselect\n"
1914 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
1915 " errdhotfocus\n"
1916 " Menus: menu, menuhot, menusel, menuhotsel\n"
1917 " Editor: editnormal, editbold, editmarked, editwhitespace,\n"
1918 " editlinestate\n"), stdout);
1919 fputs (_
1921 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
1922 " File types: directory, executable, link, stalelink, device, special, core\n"
1923 "\n" "Colors:\n"
1924 " black, gray, red, brightred, green, brightgreen, brown,\n"
1925 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
1926 " brightcyan, lightgray and white\n\n"), stdout);
1929 static void
1930 process_args (poptContext ctx, int c, const char *option_arg)
1932 switch (c) {
1933 case 'V':
1934 show_version (1);
1935 exit (0);
1936 break;
1938 case 'c':
1939 disable_colors = 0;
1940 #ifdef HAVE_SLANG
1941 force_colors = 1;
1942 #endif /* HAVE_SLANG */
1943 break;
1945 case 'f':
1946 printf ("%s (%s)\n", mc_home, mc_home_alt);
1947 exit (0);
1948 break;
1950 #ifdef USE_NETCODE
1951 case 'l':
1952 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) option_arg);
1953 #ifdef WITH_SMBFS
1954 smbfs_set_debugf (option_arg);
1955 #endif /* WITH_SMBFS */
1956 break;
1958 #ifdef WITH_SMBFS
1959 case 'D':
1960 smbfs_set_debug (atoi (option_arg));
1961 break;
1962 #endif /* WITH_SMBFS */
1963 #endif /* USE_NETCODE */
1965 case 'd':
1966 use_mouse_p = MOUSE_DISABLED;
1967 break;
1969 #ifdef HAVE_SUBSHELL_SUPPORT
1970 case 'u':
1971 use_subshell = 0;
1972 break;
1973 #endif /* HAVE_SUBSHELL_SUPPORT */
1975 case 'H':
1976 print_color_usage ();
1977 exit (0);
1978 break;
1980 case 'h':
1981 print_mc_usage (ctx, stdout);
1982 exit (0);
1986 static const struct poptOption argument_table[] = {
1987 /* generic options */
1988 {"help", 'h', POPT_ARG_NONE, {NULL}, 'h',
1989 N_("Displays this help message"), NULL},
1990 {"version", 'V', POPT_ARG_NONE, {NULL}, 'V',
1991 N_("Displays the current version"), NULL},
1993 /* terminal options */
1994 {"xterm", 'x', POPT_ARG_NONE, {&force_xterm}, 0,
1995 N_("Forces xterm features"), NULL},
1996 {"nomouse", 'd', POPT_ARG_NONE, {NULL}, 'd',
1997 N_("Disable mouse support in text version"), NULL},
1998 #if defined(HAVE_SLANG)
1999 {"termcap", 't', 0, {&SLtt_Try_Termcap}, 0,
2000 N_("Tries to use termcap instead of terminfo"), NULL},
2001 #endif
2002 {"resetsoft", 'k', POPT_ARG_NONE, {&reset_hp_softkeys}, 0,
2003 N_("Resets soft keys on HP terminals"), NULL},
2004 {"slow", 's', POPT_ARG_NONE, {&slow_terminal}, 0,
2005 N_("To run on slow terminals"), NULL},
2006 {"stickchars", 'a', 0, {&force_ugly_line_drawing}, 0,
2007 N_("Use stickchars to draw"), NULL},
2009 /* color options */
2010 {"nocolor", 'b', POPT_ARG_NONE, {&disable_colors}, 0,
2011 N_("Requests to run in black and white"), NULL},
2012 {"color", 'c', POPT_ARG_NONE, {NULL}, 'c',
2013 N_("Request to run in color mode"), NULL},
2014 {"colors", 'C', POPT_ARG_STRING, {&command_line_colors}, 0,
2015 N_("Specifies a color configuration"), NULL},
2016 {"help-colors", 'H', POPT_ARG_NONE, {NULL}, 'H',
2017 N_("Displays a help screen on how to change the color scheme"), NULL},
2019 /* debug options */
2020 #ifdef USE_NETCODE
2021 {"ftplog", 'l', POPT_ARG_STRING, {NULL}, 'l',
2022 N_("Log ftp dialog to specified file"), NULL},
2023 #ifdef WITH_SMBFS
2024 {"debuglevel", 'D', POPT_ARG_STRING, {NULL}, 'D',
2025 N_("Set debug level"), NULL},
2026 #endif
2027 #endif
2029 /* options for wrappers */
2030 {"datadir", 'f', POPT_ARG_NONE, {NULL}, 'f',
2031 N_("Print data directory"), NULL},
2032 {"printwd", 'P', POPT_ARG_STRING, {&last_wd_file}, 0,
2033 N_("Print last working directory to specified file"), NULL},
2035 /* subshell options */
2036 #ifdef HAVE_SUBSHELL_SUPPORT
2037 {"subshell", 'U', POPT_ARG_NONE, {&use_subshell}, 0,
2038 N_("Enables subshell support (default)"), NULL},
2039 {"nosubshell", 'u', POPT_ARG_NONE, {NULL}, 'u',
2040 N_("Disables subshell support"), NULL},
2041 #endif
2043 /* single file operations */
2044 {"view", 'v', POPT_ARG_STRING, {&view_one_file}, 0,
2045 N_("Launches the file viewer on a file"), NULL},
2046 #ifdef USE_INTERNAL_EDIT
2047 {"edit", 'e', POPT_ARG_STRING, {&edit_one_file}, 0,
2048 N_("Edits one file"), NULL},
2049 #endif
2051 {NULL, '\0', 0, {NULL}, 0, NULL , NULL}
2054 static void
2055 handle_args (int argc, char *argv[])
2057 char *tmp;
2058 poptContext ctx;
2059 const char *base;
2060 int c;
2062 ctx =
2063 poptGetContext ("mc", argc, argv, argument_table,
2064 POPT_CONTEXT_NO_EXEC);
2066 while ((c = poptGetNextOpt (ctx)) > 0) {
2067 process_args (ctx, c, poptGetOptArg (ctx));
2070 if (c < -1) {
2071 print_mc_usage (ctx, stderr);
2072 fprintf (stderr, "%s: %s\n",
2073 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2074 poptStrerror (c));
2075 exit (1);
2078 tmp = poptGetArg (ctx);
2081 * Check for special invocation names mcedit and mcview,
2082 * if none apply then set the current directory and the other
2083 * directory from the command line arguments
2085 base = x_basename (argv[0]);
2086 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2087 edit_one_file = "";
2088 if (tmp) {
2090 * Check for filename:lineno, followed by an optional colon.
2091 * This format is used by many programs (especially compilers)
2092 * in error messages and warnings. It is supported so that
2093 * users can quickly copy and paste file locations.
2095 char *end = tmp + strlen (tmp), *p = end;
2096 if (p > tmp && p[-1] == ':')
2097 p--;
2098 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2099 p--;
2100 if (tmp < p && p < end && p[-1] == ':') {
2101 struct stat st;
2102 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2104 * Check that the file before the colon actually exists.
2105 * If it doesn't exist, revert to the old behavior.
2107 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2108 edit_one_file = fname;
2109 edit_one_file_start_line = atoi (p);
2110 } else {
2111 g_free (fname);
2112 goto try_plus_filename;
2114 } else {
2115 try_plus_filename:
2116 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2117 int start_line = atoi (tmp);
2118 if (start_line > 0) {
2119 char *file = poptGetArg (ctx);
2120 if (file) {
2121 tmp = file;
2122 edit_one_file_start_line = start_line;
2126 edit_one_file = g_strdup (tmp);
2129 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2130 if (tmp)
2131 view_one_file = g_strdup (tmp);
2132 else {
2133 fputs ("No arguments given to the viewer\n", stderr);
2134 exit (1);
2136 } else {
2137 /* sets the current dir and the other dir */
2138 if (tmp) {
2139 this_dir = g_strdup (tmp);
2140 if ((tmp = poptGetArg (ctx)))
2141 other_dir = g_strdup (tmp);
2145 poptFreeContext (ctx);
2149 main (int argc, char *argv[])
2151 struct stat s;
2152 char *mc_dir;
2154 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2155 int i;
2157 setlocale (LC_ALL, "");
2158 bindtextdomain ("mc", LOCALEDIR);
2159 textdomain ("mc");
2161 /* Set up temporary directory */
2162 mc_tmpdir ();
2164 OS_Setup ();
2166 /* This variable is used by the subshell */
2167 home_dir = getenv ("HOME");
2168 if (!home_dir) {
2169 /* mc_home was computed by OS_Setup */
2170 home_dir = mc_home;
2173 str_init_strings (NULL);
2175 vfs_init ();
2177 #ifdef USE_INTERNAL_EDIT
2178 for ( i = 0; i < MAX_HISTORY_MOVETO; i++ ) {
2179 edit_history_moveto[i].filename = NULL;
2180 edit_history_moveto[i].line = -1;
2182 #endif
2184 #ifdef HAVE_SLANG
2185 SLtt_Ignore_Beep = 1;
2186 #endif
2188 handle_args (argc, argv);
2190 /* NOTE: This has to be called before slang_init or whatever routine
2191 calls any define_sequence */
2192 init_key ();
2194 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2195 handle_console (CONSOLE_INIT);
2197 #ifdef HAVE_SUBSHELL_SUPPORT
2198 /* Don't use subshell when invoked as viewer or editor */
2199 if (edit_one_file || view_one_file)
2200 use_subshell = 0;
2202 if (use_subshell)
2203 subshell_get_console_attributes ();
2204 #endif /* HAVE_SUBSHELL_SUPPORT */
2206 /* Install the SIGCHLD handler; must be done before init_subshell() */
2207 init_sigchld ();
2209 /* We need this, since ncurses endwin () doesn't restore the signals */
2210 save_stop_handler ();
2212 /* Must be done before init_subshell, to set up the terminal size: */
2213 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2214 #ifdef HAVE_SLANG
2215 slang_init ();
2216 #endif
2217 /* NOTE: This call has to be after slang_init. It's the small part from
2218 the previous init_key which had to be moved after the call of slang_init */
2219 init_key_input_fd ();
2221 load_setup ();
2223 init_curses ();
2225 /* create home directory */
2226 /* do it after the screen library initialization to show the error message */
2227 mc_dir = concat_dir_and_file (home_dir, MC_BASE);
2228 canonicalize_pathname (mc_dir);
2229 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT)
2230 && mkdir (mc_dir, 0700) != 0)
2231 message (D_ERROR, _("Warning"),
2232 _("Cannot create %s directory"), mc_dir);
2233 g_free (mc_dir);
2235 init_xterm_support ();
2237 #ifdef HAVE_SUBSHELL_SUPPORT
2239 /* Done here to ensure that the subshell doesn't */
2240 /* inherit the file descriptors opened below, etc */
2241 if (use_subshell)
2242 init_subshell ();
2244 #endif /* HAVE_SUBSHELL_SUPPORT */
2246 /* Removing this from the X code let's us type C-c */
2247 load_key_defs ();
2249 /* Also done after init_subshell, to save any shell init file messages */
2250 if (console_flag)
2251 handle_console (CONSOLE_SAVE);
2253 if (alternate_plus_minus)
2254 application_keypad_mode ();
2256 #ifdef HAVE_SUBSHELL_SUPPORT
2257 if (use_subshell) {
2258 prompt = strip_ctrl_codes (subshell_prompt);
2259 if (!prompt)
2260 prompt = "";
2261 } else
2262 #endif /* HAVE_SUBSHELL_SUPPORT */
2263 prompt = (geteuid () == 0) ? "# " : "$ ";
2265 /* Program main loop */
2266 if (!midnight_shutdown)
2267 do_nc ();
2269 /* Save the tree store */
2270 tree_store_save ();
2272 /* Virtual File System shutdown */
2273 vfs_shut ();
2275 flush_extension_file (); /* does only free memory */
2277 endwin ();
2278 #ifdef HAVE_SLANG
2279 slang_shutdown ();
2280 #endif
2282 if (console_flag && !(quit & SUBSHELL_EXIT))
2283 handle_console (CONSOLE_RESTORE);
2284 if (alternate_plus_minus)
2285 numeric_keypad_mode ();
2287 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2289 if (console_flag)
2290 handle_console (CONSOLE_DONE);
2291 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2293 if (last_wd_file && last_wd_string && !print_last_revert
2294 && !edit_one_file && !view_one_file) {
2295 int last_wd_fd =
2296 open (last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2297 S_IRUSR | S_IWUSR);
2299 if (last_wd_fd != -1) {
2300 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2301 close (last_wd_fd);
2304 g_free (last_wd_string);
2306 g_free (mc_home_alt);
2307 g_free (mc_home);
2308 done_key ();
2309 #ifdef HAVE_CHARSET
2310 free_codepages_list ();
2311 #endif
2312 str_uninit_strings ();
2314 g_free (this_dir);
2315 g_free (other_dir);
2317 #ifdef USE_INTERNAL_EDIT
2318 for ( i = 0; i < MAX_HISTORY_MOVETO; i++ ) {
2319 g_free(edit_history_moveto[i].filename);
2321 #endif
2323 return 0;