Changes in SelectCodepage dialog:
[midnight-commander.git] / src / main.c
blobd847cc0ecd2669ca8c16dd30bb659f5ebc073267
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 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 #include <config.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
36 #include "global.h"
37 #include "tty.h"
38 #include "dir.h"
39 #include "color.h"
40 #include "dialog.h"
41 #include "menu.h"
42 #include "panel.h"
43 #include "main.h"
44 #include "win.h"
45 #include "mouse.h"
46 #include "option.h"
47 #include "tree.h"
48 #include "treestore.h"
49 #include "cons.saver.h"
50 #include "subshell.h"
51 #include "key.h" /* For init_key() and mi_getch() */
52 #include "setup.h" /* save_setup() */
53 #include "profile.h" /* free_profiles() */
54 #include "boxes.h" /* sort_box() */
55 #include "layout.h"
56 #include "cmd.h" /* Normal commands */
57 #include "hotlist.h"
58 #include "panelize.h"
59 #include "learn.h" /* learn_keys() */
60 #include "listmode.h"
61 #include "execute.h"
62 #include "ext.h" /* For flush_extension_file() */
63 #include "strutil.h"
65 /* Listbox for the command history feature */
66 #include "widget.h"
67 #include "command.h"
68 #include "wtools.h"
70 #include "chmod.h"
71 #include "chown.h"
72 #include "achown.h"
74 #ifdef WITH_SMBFS
75 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
76 #endif
78 #ifdef USE_INTERNAL_EDIT
79 # include "../edit/edit.h"
80 #endif
82 #ifdef HAVE_CHARSET
83 #include "charsets.h"
84 #endif /* HAVE_CHARSET */
86 #ifdef USE_VFS
87 #include "../vfs/gc.h"
88 #endif
90 #include "popt.h"
92 /* When the modes are active, left_panel, right_panel and tree_panel */
93 /* Point to a proper data structure. You should check with the functions */
94 /* get_current_type and get_other_type the types of the panels before using */
95 /* This pointer variables */
97 /* The structures for the panels */
98 WPanel *left_panel = NULL;
99 WPanel *right_panel = NULL;
101 /* The pointer to the tree */
102 WTree *the_tree = NULL;
104 /* The Menubar */
105 struct WMenu *the_menubar = NULL;
107 /* Pointers to the selected and unselected panel */
108 WPanel *current_panel = NULL;
110 /* Set if the command is being run from the "Right" menu */
111 int is_right = 0;
113 /* Set when main loop should be terminated */
114 volatile int quit = 0;
116 /* Set if you want the possible completions dialog for the first time */
117 int show_all_if_ambiguous = 0;
119 /* Set when cd symlink following is desirable (bash mode) */
120 int cd_symlinks = 1;
122 /* If set then dialogs just clean the screen when refreshing, else */
123 /* they do a complete refresh, refreshing all the parts of the program */
124 int fast_refresh = 0;
126 /* If true, marking a files moves the cursor down */
127 int mark_moves_down = 1;
129 /* If true, at startup the user-menu is invoked */
130 int auto_menu = 0;
132 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
133 and M-- and keypad + / - */
134 int alternate_plus_minus = 0;
136 /* If true, then the +, - and \ keys have their special meaning only if the
137 * command line is emtpy, otherwise they behave like regular letters
139 int only_leading_plus_minus = 1;
141 int pause_after_run = pause_on_dumb_terminals;
143 /* It true saves the setup when quitting */
144 int auto_save_setup = 1;
146 #ifndef HAVE_CHARSET
147 /* If true, allow characters in the range 160-255 */
148 int eight_bit_clean = 1;
151 * If true, also allow characters in the range 128-159.
152 * This is reported to break on many terminals (xterm, qansi-m).
154 int full_eight_bits = 0;
156 #endif /* !HAVE_CHARSET */
159 * If utf-8 terminal utf8_display = 1
160 * Display bits set UTF-8
163 int utf8_display = 0;
165 /* If true use the internal viewer */
166 int use_internal_view = 1;
168 /* Have we shown the fast-reload warning in the past? */
169 int fast_reload_w = 0;
171 /* Move page/item? When clicking on the top or bottom of a panel */
172 int mouse_move_pages = 1;
174 /* If true: l&r arrows are used to chdir if the input line is empty */
175 int navigate_with_arrows = 0;
177 /* If true use +, -, | for line drawing */
178 int force_ugly_line_drawing = 0;
180 /* If true program softkeys (HP terminals only) on startup and after every
181 command ran in the subshell to the description found in the termcap/terminfo
182 database */
183 int reset_hp_softkeys = 0;
185 /* The prompt */
186 const char *prompt = NULL;
188 /* The widget where we draw the prompt */
189 WLabel *the_prompt;
191 /* The hint bar */
192 WLabel *the_hint;
194 /* The button bar */
195 WButtonBar *the_bar;
197 /* For slow terminals */
198 int slow_terminal = 0;
200 /* Mouse type: GPM, xterm or none */
201 Mouse_Type use_mouse_p = MOUSE_NONE;
203 /* If true, assume we are running on an xterm terminal */
204 static int force_xterm = 0;
206 /* If on, default for "No" in delete operations */
207 int safe_delete = 0;
209 /* Controls screen clearing before an exec */
210 int clear_before_exec = 1;
212 /* Asks for confirmation before deleting a file */
213 int confirm_delete = 1;
215 /* Asks for confirmation before deleting a hotlist entry */
216 int confirm_directory_hotlist_delete = 1;
218 /* Asks for confirmation before overwriting a file */
219 int confirm_overwrite = 1;
221 /* Asks for confirmation before executing a program by pressing enter */
222 int confirm_execute = 0;
224 /* Asks for confirmation before leaving the program */
225 int confirm_exit = 1;
227 /* Asks for confirmation when using F3 to view a directory and there
228 are tagged files */
229 int confirm_view_dir = 0;
231 /* This flag indicates if the pull down menus by default drop down */
232 int drop_menus = 0;
234 /* The dialog handle for the main program */
235 Dlg_head *midnight_dlg;
237 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
238 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
239 int update_prompt = 0;
241 /* The home directory */
242 const char *home_dir = NULL;
244 /* The value of the other directory, only used when loading the setup */
245 char *other_dir = NULL;
247 /* Only used at program boot */
248 int boot_current_is_left = 1;
250 static char *this_dir = NULL;
252 /* If this is true, then when browsing the tree the other window will
253 * automatically reload it's directory with the contents of the currently
254 * selected directory.
256 int xtree_mode = 0;
258 /* If set, then print to the given file the last directory we were at */
259 static char *last_wd_file = NULL;
260 static char *last_wd_string = NULL;
261 /* Set to 1 to suppress printing the last directory */
262 static int print_last_revert = 0;
264 /* Force colors, only used by Slang */
265 int force_colors = 0;
267 /* colors specified on the command line: they override any other setting */
268 char *command_line_colors = NULL;
270 /* File name to view if argument was supplied */
271 static const char *view_one_file = NULL;
273 /* File name to edit if argument was supplied */
274 const char *edit_one_file = NULL;
276 /* Line to start the editor on */
277 static int edit_one_file_start_line = 0;
279 /* Used so that widgets know if they are being destroyed or
280 shut down */
281 int midnight_shutdown = 0;
283 /* The user's shell */
284 const char *shell = NULL;
286 /* mc_home: The home of MC */
287 char *mc_home = NULL;
289 char cmd_buf[512];
291 #ifdef USE_INTERNAL_EDIT
292 int edit_stack_iterator = 0;
293 struct edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
294 #endif
296 static void
297 reload_panelized (WPanel *panel)
299 int i, j;
300 dir_list *list = &panel->dir;
302 if (panel != current_panel)
303 mc_chdir (panel->cwd);
305 for (i = 0, j = 0; i < panel->count; i++) {
306 if (list->list[i].f.marked) {
307 /* Unmark the file in advance. In case the following mc_lstat
308 * fails we are done, else we have to mark the file again
309 * (Note: do_file_mark depends on a valid "list->list [i].buf").
310 * IMO that's the best way to update the panel's summary status
311 * -- Norbert
313 do_file_mark (panel, i, 0);
315 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
316 g_free (list->list[i].fname);
317 continue;
319 if (list->list[i].f.marked)
320 do_file_mark (panel, i, 1);
321 if (j != i)
322 list->list[j] = list->list[i];
323 j++;
325 if (j == 0)
326 panel->count = set_zero_dir (list);
327 else
328 panel->count = j;
330 if (panel != current_panel)
331 mc_chdir (current_panel->cwd);
334 static void
335 update_one_panel_widget (WPanel *panel, int force_update,
336 const char *current_file)
338 int free_pointer;
339 char *my_current_file = NULL;
341 if (force_update & UP_RELOAD) {
342 panel->is_panelized = 0;
343 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
344 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
347 /* If current_file == -1 (an invalid pointer) then preserve selection */
348 if (current_file == UP_KEEPSEL) {
349 free_pointer = 1;
350 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
351 current_file = my_current_file;
352 } else
353 free_pointer = 0;
355 if (panel->is_panelized)
356 reload_panelized (panel);
357 else
358 panel_reload (panel);
360 try_to_select (panel, current_file);
361 panel->dirty = 1;
363 if (free_pointer)
364 g_free (my_current_file);
367 void
368 panel_clean_dir (WPanel *panel)
370 int count = panel->count;
372 panel->count = 0;
373 panel->top_file = 0;
374 panel->selected = 0;
375 panel->marked = 0;
376 panel->dirs_marked = 0;
377 panel->total = 0;
378 panel->searching = 0;
379 panel->is_panelized = 0;
380 panel->dirty = 1;
382 clean_dir (&panel->dir, count);
385 static void
386 update_one_panel (int which, int force_update, const char *current_file)
388 WPanel *panel;
390 if (get_display_type (which) != view_listing)
391 return;
393 panel = (WPanel *) get_panel_widget (which);
394 update_one_panel_widget (panel, force_update, current_file);
397 /* This routine reloads the directory in both panels. It tries to
398 * select current_file in current_panel and other_file in other_panel.
399 * If current_file == -1 then it automatically sets current_file and
400 * other_file to the currently selected files in the panels.
402 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
403 * will not reload the other panel.
405 void
406 update_panels (int force_update, const char *current_file)
408 int reload_other = !(force_update & UP_ONLY_CURRENT);
409 WPanel *panel;
411 update_one_panel (get_current_index (), force_update, current_file);
412 if (reload_other)
413 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
415 if (get_current_type () == view_listing)
416 panel = (WPanel *) get_panel_widget (get_current_index ());
417 else
418 panel = (WPanel *) get_panel_widget (get_other_index ());
420 mc_chdir (panel->cwd);
423 /* Save current stat of directories to avoid reloading the panels */
424 /* when no modifications have taken place */
425 void
426 save_cwds_stat (void)
428 if (fast_reload) {
429 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
430 if (get_other_type () == view_listing)
431 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
435 #ifdef HAVE_SUBSHELL_SUPPORT
436 void
437 do_possible_cd (const char *new_dir)
439 if (!do_cd (new_dir, cd_exact))
440 message (D_ERROR, _("Warning"),
441 _(" The Commander can't change to the directory that \n"
442 " the subshell claims you are in. Perhaps you have \n"
443 " deleted your working directory, or given yourself \n"
444 " extra access permissions with the \"su\" command? "));
447 void
448 do_update_prompt (void)
450 if (update_prompt) {
451 printf ("%s", subshell_prompt);
452 fflush (stdout);
453 update_prompt = 0;
456 #endif /* HAVE_SUBSHELL_SUPPORT */
458 void
459 change_panel (void)
461 free_completions (cmdline);
462 dlg_one_down (midnight_dlg);
465 /* Stop MC main dialog and the current dialog if it exists.
466 * Needed to provide fast exit from MC viewer or editor on shell exit */
467 static void
468 stop_dialogs (void)
470 midnight_dlg->running = 0;
471 if (current_dlg) {
472 current_dlg->running = 0;
476 static int
477 quit_cmd_internal (int quiet)
479 int q = quit;
481 if (quiet || !confirm_exit) {
482 q = 1;
483 } else {
484 if (query_dialog
485 (_(" The Midnight Commander "),
486 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
487 2, _("&Yes"), _("&No")) == 0)
488 q = 1;
490 if (q) {
491 #ifdef HAVE_SUBSHELL_SUPPORT
492 if (!use_subshell)
493 stop_dialogs ();
494 else if ((q = exit_subshell ()))
495 #endif
496 stop_dialogs ();
498 if (q)
499 quit |= 1;
500 return quit;
503 static void
504 quit_cmd (void)
506 quit_cmd_internal (0);
509 void
510 quiet_quit_cmd (void)
512 print_last_revert = 1;
513 quit_cmd_internal (1);
517 * Touch window and refresh window functions
520 /* This routine untouches the first line on both panels in order */
521 /* to avoid the refreshing the menu bar */
523 void
524 repaint_screen (void)
526 do_refresh ();
527 mc_refresh ();
530 /* Wrapper for do_subshell_chdir, check for availability of subshell */
531 void
532 subshell_chdir (const char *directory)
534 #ifdef HAVE_SUBSHELL_SUPPORT
535 if (use_subshell) {
536 if (vfs_current_is_local ())
537 do_subshell_chdir (directory, 0, 1);
539 #endif /* HAVE_SUBSHELL_SUPPORT */
542 void
543 directory_history_add (struct WPanel *panel, const char *dir)
545 char *tmp;
547 tmp = g_strdup (dir);
548 strip_password (tmp, 1);
550 panel->dir_history = list_append_unique (panel->dir_history, tmp);
554 * If we moved to the parent directory move the selection pointer to
555 * the old directory name; If we leave VFS dir, remove FS specificator.
557 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
559 static const char *
560 get_parent_dir_name (const char *cwd, const char *lwd)
562 const char *p;
563 if (strlen (lwd) > strlen (cwd))
564 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
565 (strlen (cwd) == p - lwd || (p == lwd && cwd[0] == PATH_SEP &&
566 cwd[1] == '\0'))) {
567 return (p + 1);
569 return NULL;
573 * Changes the current directory of the panel.
574 * Don't record change in the directory history.
576 static int
577 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
579 const char *directory;
580 char *olddir;
581 char temp[MC_MAXPATHLEN];
582 char *translated_url;
584 if (cd_type == cd_parse_command) {
585 while (*new_dir == ' ')
586 new_dir++;
589 olddir = g_strdup (panel->cwd);
590 new_dir = translated_url = vfs_translate_url (new_dir);
592 /* Convert *new_path to a suitable pathname, handle ~user */
594 if (cd_type == cd_parse_command) {
595 if (!strcmp (new_dir, "-")) {
596 strcpy (temp, panel->lwd);
597 new_dir = temp;
600 directory = *new_dir ? new_dir : home_dir;
602 if (mc_chdir (directory) == -1) {
603 strcpy (panel->cwd, olddir);
604 g_free (olddir);
605 g_free (translated_url);
606 return 0;
608 g_free (translated_url);
610 /* Success: save previous directory, shutdown status of previous dir */
611 strcpy (panel->lwd, olddir);
612 free_completions (cmdline);
614 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
616 vfs_release_path (olddir);
618 subshell_chdir (panel->cwd);
620 /* Reload current panel */
621 panel_clean_dir (panel);
622 panel->count =
623 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
624 panel->reverse, panel->case_sensitive,
625 panel->exec_first, panel->filter);
626 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
627 load_hint (0);
628 panel->dirty = 1;
629 update_xterm_title_path ();
631 g_free (olddir);
633 return 1;
637 * Changes the current directory of the panel.
638 * Record change in the directory history.
641 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
643 int r;
645 r = _do_panel_cd (panel, new_dir, cd_type);
646 if (r)
647 directory_history_add (panel, panel->cwd);
648 return r;
652 do_cd (const char *new_dir, enum cd_enum exact)
654 return (do_panel_cd (current_panel, new_dir, exact));
657 void
658 directory_history_next (WPanel *panel)
660 GList *nextdir;
662 nextdir = g_list_next (panel->dir_history);
664 if (!nextdir)
665 return;
667 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
668 panel->dir_history = nextdir;
671 void
672 directory_history_prev (WPanel *panel)
674 GList *prevdir;
676 prevdir = g_list_previous (panel->dir_history);
678 if (!prevdir)
679 return;
681 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
682 panel->dir_history = prevdir;
685 void
686 directory_history_list (WPanel *panel)
688 char *s;
690 if (!panel->dir_history)
691 return;
693 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
695 if (!s)
696 return;
698 if (_do_panel_cd (panel, s, cd_exact))
699 directory_history_add (panel, panel->cwd);
700 else
701 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
702 g_free (s);
705 #ifdef HAVE_SUBSHELL_SUPPORT
707 load_prompt (int fd, void *unused)
709 (void) fd;
710 (void) unused;
712 if (!read_subshell_prompt ())
713 return 0;
715 /* Don't actually change the prompt if it's invisible */
716 if (current_dlg == midnight_dlg && command_prompt) {
717 char *tmp_prompt;
718 int prompt_len;
720 tmp_prompt = strip_ctrl_codes (subshell_prompt);
721 prompt_len = str_term_width1 (tmp_prompt);
723 /* Check for prompts too big */
724 if (COLS > 8 && prompt_len > COLS - 8) {
725 tmp_prompt[COLS - 8] = '\0';
726 prompt_len = COLS - 8;
728 prompt = tmp_prompt;
729 label_set_text (the_prompt, prompt);
730 winput_set_origin ((WInput *) cmdline, prompt_len,
731 COLS - prompt_len);
733 /* since the prompt has changed, and we are called from one of the
734 * get_event channels, the prompt updating does not take place
735 * automatically: force a cursor update and a screen refresh
737 update_cursor (midnight_dlg);
738 mc_refresh ();
740 update_prompt = 1;
741 return 0;
743 #endif /* HAVE_SUBSHELL_SUPPORT */
745 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
747 maybe_cd (int move_up_dir)
749 if (navigate_with_arrows) {
750 if (!cmdline->buffer[0]) {
751 if (move_up_dir) {
752 do_cd ("..", cd_exact);
753 return 1;
755 if (S_ISDIR (selection (current_panel)->st.st_mode)
756 || link_isdir (selection (current_panel))) {
757 do_cd (selection (current_panel)->fname, cd_exact);
758 return 1;
762 return 0;
765 static void
766 sort_cmd (void)
768 WPanel *p;
769 sortfn *sort_order;
771 if (!SELECTED_IS_PANEL)
772 return;
774 p = MENU_PANEL;
775 sort_order = sort_box (p->sort_type, &p->reverse,
776 &p->case_sensitive,
777 &p->exec_first);
779 panel_set_sort_order (p, sort_order);
783 static void
784 treebox_cmd (void)
786 char *sel_dir;
788 sel_dir = tree_box (selection (current_panel)->fname);
789 if (sel_dir) {
790 do_cd (sel_dir, cd_exact);
791 g_free (sel_dir);
795 #ifdef LISTMODE_EDITOR
796 static void
797 listmode_cmd (void)
799 char *newmode;
801 if (get_current_type () != view_listing)
802 return;
804 newmode = listmode_edit (current_panel->user_format);
805 if (!newmode)
806 return;
808 g_free (current_panel->user_format);
809 current_panel->list_type = list_user;
810 current_panel->user_format = newmode;
811 set_panel_formats (current_panel);
813 do_refresh ();
815 #endif /* LISTMODE_EDITOR */
817 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
818 static menu_entry LeftMenu[] = {
819 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
820 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
821 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
822 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
823 {' ', "", NULL_HOTKEY, 0},
824 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
825 {' ', "", NULL_HOTKEY, 0},
826 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
827 {' ', "",NULL_HOTKEY, 0},
828 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
829 #ifdef USE_NETCODE
830 {' ', "", NULL_HOTKEY, 0},
831 #ifdef WITH_MCFS
832 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
833 #endif
834 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
835 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
836 #ifdef WITH_SMBFS
837 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
838 #endif
839 #endif
840 {' ', "", NULL_HOTKEY, 0},
841 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
844 static menu_entry RightMenu[] = {
845 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
846 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
847 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
848 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
849 {' ', "", NULL_HOTKEY, 0},
850 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
851 {' ', "", NULL_HOTKEY, 0},
852 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
853 {' ', "",NULL_HOTKEY, 0},
854 {' ', N_("&Encoding... C-t"), NULL_HOTKEY, encoding_cmd},
855 #ifdef USE_NETCODE
856 {' ', "", NULL_HOTKEY, 0},
857 #ifdef WITH_MCFS
858 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
859 #endif
860 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
861 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
862 #ifdef WITH_SMBFS
863 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
864 #endif
865 #endif
866 {' ', "", NULL_HOTKEY, 0},
867 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
870 static menu_entry FileMenu[] = {
871 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
872 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
873 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
874 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
875 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
876 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
877 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
878 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
879 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
880 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
881 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
882 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
883 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
884 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
885 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
886 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
887 {' ', "", NULL_HOTKEY, 0},
888 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
889 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
890 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
891 {' ', "", NULL_HOTKEY, 0},
892 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
895 static menu_entry CmdMenu[] = {
896 /* I know, I'm lazy, but the tree widget when it's not running
897 * as a panel still has some problems, I have not yet finished
898 * the WTree widget port, sorry.
900 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
901 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
902 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
903 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
904 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
905 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
906 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
907 {' ', "", NULL_HOTKEY, 0},
908 {' ', N_("command &History"), NULL_HOTKEY, history_cmd},
909 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
910 #ifdef USE_VFS
911 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
912 #endif
913 #ifdef WITH_BACKGROUND
914 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
915 #endif
916 {' ', "", NULL_HOTKEY, 0},
917 #ifdef USE_EXT2FSLIB
918 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
919 #endif
920 #ifdef LISTMODE_EDITOR
921 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
922 #endif
923 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
924 {' ', "", NULL_HOTKEY, 0},
925 #endif
926 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
927 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd},
928 #ifdef USE_INTERNAL_EDIT
929 {' ', N_("Edit edi&tor menu file"), NULL_HOTKEY, edit_user_menu_cmd},
930 {' ', N_("Edit &syntax file"), NULL_HOTKEY, edit_syntax_cmd}
931 #endif /* USE_INTERNAL_EDIT */
934 /* Must keep in sync with the constants in menu_cmd */
935 static menu_entry OptMenu[] = {
936 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
937 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
938 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
939 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
940 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
941 #ifdef USE_VFS
942 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
943 #endif /* !USE_VFS */
944 {' ', "", NULL_HOTKEY, 0},
945 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
948 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
950 static Menu *MenuBar[5];
952 void
953 init_menu (void)
955 MenuBar[0] =
956 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
957 LeftMenu, menu_entries (LeftMenu),
958 "[Left and Right Menus]");
959 MenuBar[1] =
960 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
961 "[File Menu]");
962 MenuBar[2] =
963 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
964 "[Command Menu]");
965 MenuBar[3] =
966 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
967 "[Options Menu]");
968 MenuBar[4] =
969 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
970 RightMenu, menu_entries (RightMenu),
971 "[Left and Right Menus]");
974 void
975 done_menu (void)
977 int i;
979 for (i = 0; i < 5; i++) {
980 destroy_menu (MenuBar[i]);
984 static void
985 menu_last_selected_cmd (void)
987 the_menubar->active = 1;
988 the_menubar->dropped = drop_menus;
989 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
990 dlg_select_widget (the_menubar);
993 static void
994 menu_cmd (void)
996 if (the_menubar->active)
997 return;
999 if ((get_current_index () == 0) ^ (!current_panel->active))
1000 the_menubar->selected = 0;
1001 else
1002 the_menubar->selected = 4;
1003 menu_last_selected_cmd ();
1006 /* Flag toggling functions */
1007 void
1008 toggle_fast_reload (void)
1010 fast_reload = !fast_reload;
1011 if (fast_reload_w == 0 && fast_reload) {
1012 message (D_NORMAL, _(" Information "),
1014 (" Using the fast reload option may not reflect the exact \n"
1015 " directory contents. In this case you'll need to do a \n"
1016 " manual reload of the directory. See the man page for \n"
1017 " the details. "));
1018 fast_reload_w = 1;
1022 void
1023 toggle_mix_all_files (void)
1025 mix_all_files = !mix_all_files;
1026 update_panels (UP_RELOAD, UP_KEEPSEL);
1029 void
1030 toggle_show_backup (void)
1032 show_backups = !show_backups;
1033 update_panels (UP_RELOAD, UP_KEEPSEL);
1036 void
1037 toggle_show_hidden (void)
1039 show_dot_files = !show_dot_files;
1040 update_panels (UP_RELOAD, UP_KEEPSEL);
1044 * Just a hack for allowing url-like pathnames to be accepted from the
1045 * command line.
1047 static void
1048 translated_mc_chdir (char *dir)
1050 char *newdir;
1052 newdir = vfs_translate_url (dir);
1053 mc_chdir (newdir);
1054 g_free (newdir);
1057 static void
1058 create_panels (void)
1060 int current_index;
1061 int other_index;
1062 int current_mode;
1063 int other_mode;
1064 char original_dir[1024];
1066 original_dir[0] = 0;
1068 if (boot_current_is_left) {
1069 current_index = 0;
1070 other_index = 1;
1071 current_mode = startup_left_mode;
1072 other_mode = startup_right_mode;
1073 } else {
1074 current_index = 1;
1075 other_index = 0;
1076 current_mode = startup_right_mode;
1077 other_mode = startup_left_mode;
1079 /* Creates the left panel */
1080 if (this_dir) {
1081 if (other_dir) {
1082 /* Ok, user has specified two dirs, save the original one,
1083 * since we may not be able to chdir to the proper
1084 * second directory later
1086 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1088 translated_mc_chdir (this_dir);
1090 set_display_type (current_index, current_mode);
1092 /* The other panel */
1093 if (other_dir) {
1094 if (original_dir[0])
1095 translated_mc_chdir (original_dir);
1096 translated_mc_chdir (other_dir);
1098 set_display_type (other_index, other_mode);
1100 if (startup_left_mode == view_listing) {
1101 current_panel = left_panel;
1102 } else {
1103 if (right_panel)
1104 current_panel = right_panel;
1105 else
1106 current_panel = left_panel;
1109 /* Create the nice widgets */
1110 cmdline = command_new (0, 0, 0);
1111 the_prompt = label_new (0, 0, prompt);
1112 the_prompt->transparent = 1;
1113 the_bar = buttonbar_new (keybar_visible);
1115 the_hint = label_new (0, 0, 0);
1116 the_hint->transparent = 1;
1117 the_hint->auto_adjust_cols = 0;
1118 the_hint->widget.cols = COLS;
1120 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1123 static void
1124 copy_current_pathname (void)
1126 if (!command_prompt)
1127 return;
1129 command_insert (cmdline, current_panel->cwd, 0);
1130 if (current_panel->cwd[strlen (current_panel->cwd) - 1] != PATH_SEP)
1131 command_insert (cmdline, PATH_SEP_STR, 0);
1134 static void
1135 copy_other_pathname (void)
1137 if (get_other_type () != view_listing)
1138 return;
1140 if (!command_prompt)
1141 return;
1143 command_insert (cmdline, other_panel->cwd, 0);
1144 if (other_panel->cwd[strlen (other_panel->cwd) - 1] != PATH_SEP)
1145 command_insert (cmdline, PATH_SEP_STR, 0);
1148 static void
1149 copy_readlink (WPanel *panel)
1151 if (!command_prompt)
1152 return;
1153 if (S_ISLNK (selection (panel)->st.st_mode)) {
1154 char buffer[MC_MAXPATHLEN];
1155 char *p =
1156 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1157 int i;
1159 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1160 g_free (p);
1161 if (i > 0) {
1162 buffer[i] = 0;
1163 command_insert (cmdline, buffer, 1);
1168 static void
1169 copy_current_readlink (void)
1171 copy_readlink (current_panel);
1174 static void
1175 copy_other_readlink (void)
1177 if (get_other_type () != view_listing)
1178 return;
1179 copy_readlink (other_panel);
1182 /* Insert the selected file name into the input line */
1183 static void
1184 copy_prog_name (void)
1186 char *tmp;
1187 if (!command_prompt)
1188 return;
1190 if (get_current_type () == view_tree) {
1191 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1192 tmp = tree_selected_name (tree);
1193 } else
1194 tmp = selection (current_panel)->fname;
1196 command_insert (cmdline, tmp, 1);
1199 static void
1200 copy_tagged (WPanel *panel)
1202 int i;
1204 if (!command_prompt)
1205 return;
1206 input_disable_update (cmdline);
1207 if (panel->marked) {
1208 for (i = 0; i < panel->count; i++) {
1209 if (panel->dir.list[i].f.marked)
1210 command_insert (cmdline, panel->dir.list[i].fname, 1);
1212 } else {
1213 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1216 input_enable_update (cmdline);
1219 static void
1220 copy_current_tagged (void)
1222 copy_tagged (current_panel);
1225 static void
1226 copy_other_tagged (void)
1228 if (get_other_type () != view_listing)
1229 return;
1230 copy_tagged (other_panel);
1233 static void
1234 init_labels (void)
1236 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1237 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1238 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1239 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1242 static const key_map ctl_x_map[] = {
1243 {XCTRL ('c'), quit_cmd},
1244 {'d', compare_dirs_cmd},
1245 #ifdef USE_VFS
1246 {'a', reselect_vfs},
1247 #endif /* USE_VFS */
1248 {'p', copy_current_pathname},
1249 {XCTRL ('p'), copy_other_pathname},
1250 {'t', copy_current_tagged},
1251 {XCTRL ('t'), copy_other_tagged},
1252 {'c', chmod_cmd},
1253 {'o', chown_cmd},
1254 {'r', copy_current_readlink},
1255 {XCTRL ('r'), copy_other_readlink},
1256 {'l', link_cmd},
1257 {'s', symlink_cmd},
1258 {XCTRL ('s'), edit_symlink_cmd},
1259 {'i', info_cmd_no_menu},
1260 {'q', quick_cmd_no_menu},
1261 {'h', add2hotlist_cmd},
1262 {'!', external_panelize},
1263 #ifdef WITH_BACKGROUND
1264 {'j', jobs_cmd},
1265 #endif /* WITH_BACKGROUND */
1266 {0, 0}
1269 static int ctl_x_map_enabled = 0;
1271 static void
1272 ctl_x_cmd (void)
1274 ctl_x_map_enabled = 1;
1277 static void
1278 nothing (void)
1282 static const key_map default_map[] = {
1283 {KEY_F (19), menu_last_selected_cmd},
1284 {KEY_F (20), quiet_quit_cmd},
1286 {XCTRL ('@'), single_dirsize_cmd},
1288 /* Copy useful information to the command line */
1289 {ALT ('a'), copy_current_pathname},
1290 {ALT ('A'), copy_other_pathname},
1292 {ALT ('c'), quick_cd_cmd},
1294 /* To access the directory hotlist */
1295 {XCTRL ('\\'), quick_chdir_cmd},
1297 /* Suspend */
1298 {XCTRL ('z'), suspend_cmd},
1300 /* The filtered view command */
1301 {ALT ('!'), filtered_view_cmd},
1303 /* Find file */
1304 {ALT ('?'), find_cmd},
1306 /* Panel refresh */
1307 {XCTRL ('r'), reread_cmd},
1309 /* Toggle listing between long, user defined and full formats */
1310 {ALT ('t'), toggle_listing_cmd},
1312 /* Swap panels */
1313 {XCTRL ('u'), swap_cmd},
1315 /* View output */
1316 {XCTRL ('o'), view_other_cmd},
1318 /* Control-X keybindings */
1319 {XCTRL ('x'), ctl_x_cmd},
1321 /* Trap dlg's exit commands */
1322 {ESC_CHAR, nothing},
1323 {XCTRL ('c'), nothing},
1324 {XCTRL ('g'), nothing},
1325 {0, 0},
1328 static void
1329 setup_sigwinch (void)
1331 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && defined(SIGWINCH)
1332 struct sigaction act, oact;
1333 act.sa_handler = flag_winch;
1334 sigemptyset (&act.sa_mask);
1335 act.sa_flags = 0;
1336 #ifdef SA_RESTART
1337 act.sa_flags |= SA_RESTART;
1338 #endif
1339 sigaction (SIGWINCH, &act, &oact);
1340 #endif
1343 static void
1344 setup_pre (void)
1346 /* Call all the inits */
1347 #ifdef HAVE_CHARSET
1349 * Don't restrict the output on the screen manager level,
1350 * the translation tables take care of it.
1352 #define full_eight_bits (1)
1353 #define eight_bit_clean (1)
1354 #endif /* !HAVE_CHARSET */
1356 #ifndef HAVE_SLANG
1357 meta (stdscr, eight_bit_clean);
1358 #else
1359 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1360 #endif
1363 static void
1364 init_xterm_support (void)
1366 const char *termvalue;
1368 termvalue = getenv ("TERM");
1369 if (!termvalue || !(*termvalue)) {
1370 fputs (_("The TERM environment variable is unset!\n"), stderr);
1371 exit (1);
1374 /* Check mouse capabilities */
1375 xmouse_seq = tty_tgetstr ("Km");
1377 if (strcmp (termvalue, "cygwin") == 0) {
1378 force_xterm = 1;
1379 use_mouse_p = MOUSE_DISABLED;
1382 if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
1383 || strncmp (termvalue, "rxvt", 4) == 0
1384 || strcmp (termvalue, "Eterm") == 0
1385 || strcmp (termvalue, "dtterm") == 0) {
1386 xterm_flag = 1;
1388 /* Default to the standard xterm sequence */
1389 if (!xmouse_seq) {
1390 xmouse_seq = ESC_STR "[M";
1393 /* Enable mouse unless explicitly disabled by --nomouse */
1394 if (use_mouse_p != MOUSE_DISABLED) {
1395 const char *color_term = getenv ("COLORTERM");
1396 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1397 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1398 strcmp (termvalue, "Eterm") == 0) {
1399 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1400 } else {
1401 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1407 static void
1408 setup_mc (void)
1410 setup_pre ();
1411 init_menu ();
1412 create_panels ();
1413 setup_panels ();
1415 #ifdef HAVE_SUBSHELL_SUPPORT
1416 if (use_subshell)
1417 add_select_channel (subshell_pty, load_prompt, 0);
1418 #endif /* !HAVE_SUBSHELL_SUPPORT */
1420 setup_sigwinch ();
1422 if (baudrate () < 9600 || slow_terminal) {
1423 verbose = 0;
1425 init_mouse ();
1428 static void
1429 setup_dummy_mc ()
1431 char d[MC_MAXPATHLEN];
1433 mc_get_current_wd (d, MC_MAXPATHLEN);
1434 setup_mc ();
1435 mc_chdir (d);
1438 static void
1439 done_mc (void)
1441 disable_mouse ();
1443 done_menu ();
1445 /* Setup shutdown
1447 * We sync the profiles since the hotlist may have changed, while
1448 * we only change the setup data if we have the auto save feature set
1450 if (auto_save_setup)
1451 save_setup (); /* does also call save_hotlist */
1452 else
1453 save_hotlist ();
1454 done_screen ();
1455 vfs_add_current_stamps ();
1458 /* This should be called after destroy_dlg since panel widgets
1459 * save their state on the profiles
1461 static void
1462 done_mc_profile (void)
1464 if (!auto_save_setup)
1465 profile_forget_profile (profile_name);
1466 sync_profiles ();
1467 done_setup ();
1468 free_profiles ();
1471 static cb_ret_t
1472 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1474 int i;
1476 switch (msg) {
1478 case DLG_IDLE:
1479 /* We only need the first idle event */
1480 set_idle_proc (h, 0);
1481 if (auto_menu) {
1482 user_file_menu_cmd ();
1484 return MSG_HANDLED;
1486 case DLG_KEY:
1487 if (ctl_x_map_enabled) {
1488 ctl_x_map_enabled = 0;
1489 for (i = 0; ctl_x_map[i].key_code; i++)
1490 if (parm == ctl_x_map[i].key_code) {
1491 (*ctl_x_map[i].fn) ();
1492 return MSG_HANDLED;
1496 /* FIXME: should handle all menu shortcuts before this point */
1497 if (the_menubar->active)
1498 return MSG_NOT_HANDLED;
1500 if (parm == KEY_F (10)) {
1501 quit_cmd ();
1502 return MSG_HANDLED;
1505 if (parm == '\t')
1506 free_completions (cmdline);
1508 if (parm == '\n') {
1509 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1510 cmdline->buffer[i] == '\t'); i++);
1511 if (cmdline->buffer[i]) {
1512 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1513 return MSG_HANDLED;
1515 stuff (cmdline, "", 0);
1516 cmdline->point = 0;
1519 /* Ctrl-Enter and Alt-Enter */
1520 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1521 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1522 copy_prog_name ();
1523 return MSG_HANDLED;
1526 /* Ctrl-Shift-Enter */
1527 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1528 copy_current_pathname ();
1529 copy_prog_name ();
1530 return MSG_HANDLED;
1533 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1534 && !quote && !current_panel->searching) {
1535 if (!only_leading_plus_minus) {
1536 /* Special treatement, since the input line will eat them */
1537 if (parm == '+') {
1538 select_cmd ();
1539 return MSG_HANDLED;
1542 if (parm == '\\' || parm == '-') {
1543 unselect_cmd ();
1544 return MSG_HANDLED;
1547 if (parm == '*') {
1548 reverse_selection_cmd ();
1549 return MSG_HANDLED;
1551 } else if (!command_prompt || !cmdline->buffer[0]) {
1552 /* Special treatement '+', '-', '\', '*' only when this is
1553 * first char on input line
1556 if (parm == '+') {
1557 select_cmd ();
1558 return MSG_HANDLED;
1561 if (parm == '\\' || parm == '-') {
1562 unselect_cmd ();
1563 return MSG_HANDLED;
1566 if (parm == '*') {
1567 reverse_selection_cmd ();
1568 return MSG_HANDLED;
1572 return MSG_NOT_HANDLED;
1574 case DLG_HOTKEY_HANDLED:
1575 if ((get_current_type () == view_listing) && current_panel->searching) {
1576 current_panel->searching = 0;
1577 current_panel->dirty = 1;
1579 return MSG_HANDLED;
1581 case DLG_UNHANDLED_KEY:
1582 if (command_prompt) {
1583 int v;
1585 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1586 if (v)
1587 return v;
1589 if (ctl_x_map_enabled) {
1590 ctl_x_map_enabled = 0;
1591 for (i = 0; ctl_x_map[i].key_code; i++)
1592 if (parm == ctl_x_map[i].key_code) {
1593 (*ctl_x_map[i].fn) ();
1594 return MSG_HANDLED;
1596 } else {
1597 for (i = 0; default_map[i].key_code; i++) {
1598 if (parm == default_map[i].key_code) {
1599 (*default_map[i].fn) ();
1600 return MSG_HANDLED;
1604 return MSG_NOT_HANDLED;
1606 case DLG_DRAW:
1607 /* We handle the special case of the output lines */
1608 if (console_flag && output_lines)
1609 show_console_contents (output_start_y,
1610 LINES - output_lines - keybar_visible -
1611 1, LINES - keybar_visible - 1);
1612 return MSG_HANDLED;
1614 case DLG_POST_KEY:
1615 if (!the_menubar->active)
1616 update_dirty_panels ();
1617 return MSG_HANDLED;
1619 default:
1620 return default_dlg_callback (h, msg, parm);
1624 /* Show current directory in the xterm title */
1625 void
1626 update_xterm_title_path (void)
1628 const char *p;
1630 if (xterm_flag && xterm_title) {
1631 p = strip_home_and_password (current_panel->cwd);
1632 fprintf (stdout, "\33]0;mc - %s\7", str_term_form (p));
1633 if (!alternate_plus_minus)
1634 numeric_keypad_mode ();
1635 fflush (stdout);
1640 * Load new hint and display it.
1641 * IF force is not 0, ignore the timeout.
1643 void
1644 load_hint (int force)
1646 char *hint;
1648 if (!the_hint->widget.parent)
1649 return;
1651 if (!message_visible) {
1652 label_set_text (the_hint, 0);
1653 return;
1656 if ((hint = get_random_hint (force))) {
1657 if (*hint)
1658 set_hintbar (hint);
1659 g_free (hint);
1660 } else {
1661 char text[BUF_SMALL];
1663 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1664 VERSION);
1665 set_hintbar (text);
1669 static void
1670 setup_panels_and_run_mc (void)
1672 add_widget (midnight_dlg, the_menubar);
1673 add_widget (midnight_dlg, get_panel_widget (0));
1674 add_widget (midnight_dlg, get_panel_widget (1));
1675 add_widget (midnight_dlg, the_hint);
1676 load_hint (1);
1677 add_widget (midnight_dlg, cmdline);
1678 add_widget (midnight_dlg, the_prompt);
1679 add_widget (midnight_dlg, the_bar);
1680 init_labels ();
1682 if (boot_current_is_left)
1683 dlg_select_widget (get_panel_widget (0));
1684 else
1685 dlg_select_widget (get_panel_widget (1));
1687 /* Run the Midnight Commander if no file was specified in the command line */
1688 run_dlg (midnight_dlg);
1691 /* result must be free'd (I think this should go in util.c) */
1692 static char *
1693 prepend_cwd_on_local (const char *filename)
1695 char *d;
1696 int l;
1698 if (vfs_file_is_local (filename)) {
1699 if (*filename == PATH_SEP) /* an absolute pathname */
1700 return g_strdup (filename);
1701 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1702 mc_get_current_wd (d, MC_MAXPATHLEN);
1703 l = strlen (d);
1704 d[l++] = PATH_SEP;
1705 strcpy (d + l, filename);
1706 canonicalize_pathname (d);
1707 return d;
1708 } else
1709 return g_strdup (filename);
1712 static int
1713 mc_maybe_editor_or_viewer (void)
1715 if (!(view_one_file || edit_one_file))
1716 return 0;
1718 setup_dummy_mc ();
1720 /* Invoke the internal view/edit routine with:
1721 * the default processing and forcing the internal viewer/editor
1723 if (view_one_file) {
1724 char *path = NULL;
1725 path = prepend_cwd_on_local (view_one_file);
1726 view_file (path, 0, 1);
1727 g_free (path);
1729 #ifdef USE_INTERNAL_EDIT
1730 else {
1731 edit_file (edit_one_file, edit_one_file_start_line);
1733 #endif /* USE_INTERNAL_EDIT */
1734 midnight_shutdown = 1;
1735 done_mc ();
1736 return 1;
1739 /* Run the main dialog that occupies the whole screen */
1740 static void
1741 do_nc (void)
1743 int midnight_colors[4];
1745 midnight_colors[0] = NORMAL_COLOR; /* NORMALC */
1746 midnight_colors[1] = REVERSE_COLOR; /* FOCUSC */
1747 midnight_colors[2] = INPUT_COLOR; /* HOT_NORMALC */
1748 midnight_colors[3] = NORMAL_COLOR; /* HOT_FOCUSC */
1750 midnight_dlg =
1751 create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1752 "[main]", NULL, DLG_WANT_IDLE);
1754 /* Check if we were invoked as an editor or file viewer */
1755 if (mc_maybe_editor_or_viewer ())
1756 return;
1758 setup_mc ();
1760 setup_panels_and_run_mc ();
1762 /* Program end */
1763 midnight_shutdown = 1;
1765 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1766 if (last_wd_file && vfs_current_is_local ()) {
1767 last_wd_string = g_strdup (current_panel->cwd);
1769 done_mc ();
1771 destroy_dlg (midnight_dlg);
1772 current_panel = 0;
1773 done_mc_profile ();
1776 /* POSIX version. The only version we support. */
1777 static void
1778 OS_Setup (void)
1780 const char *mc_libdir;
1781 shell = getenv ("SHELL");
1782 if (!shell || !*shell) {
1783 struct passwd *pwd;
1784 pwd = getpwuid (geteuid ());
1785 if (pwd != NULL)
1786 shell = g_strdup (pwd->pw_shell);
1788 if (!shell || !*shell)
1789 shell = "/bin/sh";
1791 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1792 /* and can be overriden by the MC_DATADIR environment variable */
1793 if ((mc_libdir = getenv ("MC_DATADIR")) != NULL) {
1794 mc_home = g_strdup (mc_libdir);
1795 } else {
1796 mc_home = g_strdup (DATADIR);
1800 static void
1801 sigchld_handler_no_subshell (int sig)
1803 #ifdef __linux__
1804 int pid, status;
1806 if (!console_flag)
1807 return;
1809 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1810 the value of console_flag never changed, we could simply not install
1811 this handler at all if (!console_flag && !use_subshell). */
1813 /* That comment is no longer true. We need to wait() on a sigchld
1814 handler (that's at least what the tarfs code expects currently). */
1816 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1818 if (pid == cons_saver_pid) {
1820 if (WIFSTOPPED (status)) {
1821 /* Someone has stopped cons.saver - restart it */
1822 kill (pid, SIGCONT);
1823 } else {
1824 /* cons.saver has died - disable console saving */
1825 handle_console (CONSOLE_DONE);
1826 console_flag = 0;
1829 /* If we got here, some other child exited; ignore it */
1830 #endif /* __linux__ */
1832 (void) sig;
1835 static void
1836 init_sigchld (void)
1838 struct sigaction sigchld_action;
1840 sigchld_action.sa_handler =
1841 #ifdef HAVE_SUBSHELL_SUPPORT
1842 use_subshell ? sigchld_handler :
1843 #endif /* HAVE_SUBSHELL_SUPPORT */
1844 sigchld_handler_no_subshell;
1846 sigemptyset (&sigchld_action.sa_mask);
1848 #ifdef SA_RESTART
1849 sigchld_action.sa_flags = SA_RESTART;
1850 #else
1851 sigchld_action.sa_flags = 0;
1852 #endif /* !SA_RESTART */
1854 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1855 #ifdef HAVE_SUBSHELL_SUPPORT
1857 * This may happen on QNX Neutrino 6, where SA_RESTART
1858 * is defined but not implemented. Fallback to no subshell.
1860 use_subshell = 0;
1861 #endif /* HAVE_SUBSHELL_SUPPORT */
1865 static void
1866 print_mc_usage (poptContext ctx, FILE *stream)
1868 int leftColWidth;
1870 poptSetOtherOptionHelp (ctx,
1871 _("[flags] [this_dir] [other_panel_dir]\n"));
1873 /* print help for options */
1874 leftColWidth = poptPrintHelp (ctx, stream, 0);
1875 fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
1876 _("Set initial line number for the internal editor"));
1877 fputs (_
1878 ("\n"
1879 "Please send any bug reports (including the output of `mc -V')\n"
1880 "to mc-devel@gnome.org\n"), stream);
1881 show_version (0);
1884 static void
1885 print_color_usage (void)
1888 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
1889 * and editmarked. To preserve translations, lines should be split.
1891 /* TRANSLATORS: don't translate keywords and names of colors */
1892 fputs (_
1893 ("--colors KEYWORD={FORE},{BACK}\n\n"
1894 "{FORE} and {BACK} can be omitted, and the default will be used\n"
1895 "\n" "Keywords:\n"
1896 " Global: errors, reverse, gauge, input, viewunderline\n"
1897 " File display: normal, selected, marked, markselect\n"
1898 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
1899 " errdhotfocus\n"
1900 " Menus: menu, menuhot, menusel, menuhotsel\n"
1901 " Editor: editnormal, editbold, editmarked\n"
1902 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
1903 " File types: directory, executable, link, stalelink, device, special, core\n"
1904 "\n" "Colors:\n"
1905 " black, gray, red, brightred, green, brightgreen, brown,\n"
1906 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
1907 " brightcyan, lightgray and white\n\n"), stdout);
1910 static void
1911 process_args (poptContext ctx, int c, const char *option_arg)
1913 switch (c) {
1914 case 'V':
1915 show_version (1);
1916 exit (0);
1917 break;
1919 case 'c':
1920 disable_colors = 0;
1921 #ifdef HAVE_SLANG
1922 force_colors = 1;
1923 #endif /* HAVE_SLANG */
1924 break;
1926 case 'f':
1927 printf ("%s\n", mc_home);
1928 exit (0);
1929 break;
1931 #ifdef USE_NETCODE
1932 case 'l':
1933 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) option_arg);
1934 #ifdef WITH_SMBFS
1935 smbfs_set_debugf (option_arg);
1936 #endif /* WITH_SMBFS */
1937 break;
1939 #ifdef WITH_SMBFS
1940 case 'D':
1941 smbfs_set_debug (atoi (option_arg));
1942 break;
1943 #endif /* WITH_SMBFS */
1944 #endif /* USE_NETCODE */
1946 case 'd':
1947 use_mouse_p = MOUSE_DISABLED;
1948 break;
1950 #ifdef HAVE_SUBSHELL_SUPPORT
1951 case 'u':
1952 use_subshell = 0;
1953 break;
1954 #endif /* HAVE_SUBSHELL_SUPPORT */
1956 case 'H':
1957 print_color_usage ();
1958 exit (0);
1959 break;
1961 case 'h':
1962 print_mc_usage (ctx, stdout);
1963 exit (0);
1967 static const struct poptOption argument_table[] = {
1968 /* generic options */
1969 {"help", 'h', POPT_ARG_NONE, NULL, 'h',
1970 N_("Displays this help message"), NULL},
1971 {"version", 'V', POPT_ARG_NONE, NULL, 'V',
1972 N_("Displays the current version"), NULL},
1974 /* terminal options */
1975 {"xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
1976 N_("Forces xterm features"), NULL},
1977 {"nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
1978 N_("Disable mouse support in text version"), NULL},
1979 #if defined(HAVE_SLANG)
1980 {"termcap", 't', 0, &SLtt_Try_Termcap, 0,
1981 N_("Tries to use termcap instead of terminfo"), NULL},
1982 #endif
1983 {"resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
1984 N_("Resets soft keys on HP terminals"), NULL},
1985 {"slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
1986 N_("To run on slow terminals"), NULL},
1987 {"stickchars", 'a', 0, &force_ugly_line_drawing, 0,
1988 N_("Use stickchars to draw"), NULL},
1990 /* color options */
1991 {"nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
1992 N_("Requests to run in black and white"), NULL},
1993 {"color", 'c', POPT_ARG_NONE, NULL, 'c',
1994 N_("Request to run in color mode"), NULL},
1995 {"colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
1996 N_("Specifies a color configuration"), NULL},
1997 {"help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
1998 N_("Displays a help screen on how to change the color scheme"), NULL},
2000 /* debug options */
2001 #ifdef USE_NETCODE
2002 {"ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
2003 N_("Log ftp dialog to specified file"), NULL},
2004 #ifdef WITH_SMBFS
2005 {"debuglevel", 'D', POPT_ARG_STRING, NULL, 'D',
2006 N_("Set debug level"), NULL},
2007 #endif
2008 #endif
2010 /* options for wrappers */
2011 {"datadir", 'f', POPT_ARG_NONE, NULL, 'f',
2012 N_("Print data directory"), NULL},
2013 {"printwd", 'P', POPT_ARG_STRING, &last_wd_file, 0,
2014 N_("Print last working directory to specified file"), NULL},
2016 /* subshell options */
2017 #ifdef HAVE_SUBSHELL_SUPPORT
2018 {"subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2019 N_("Enables subshell support (default)"), NULL},
2020 {"nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2021 N_("Disables subshell support"), NULL},
2022 #endif
2024 /* single file operations */
2025 {"view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2026 N_("Launches the file viewer on a file"), NULL},
2027 #ifdef USE_INTERNAL_EDIT
2028 {"edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2029 N_("Edits one file"), NULL},
2030 #endif
2032 {NULL, '\0', 0, NULL, 0, NULL, NULL}
2035 static void
2036 handle_args (int argc, char *argv[])
2038 char *tmp;
2039 poptContext ctx;
2040 const char *base;
2041 int c;
2043 ctx =
2044 poptGetContext ("mc", argc, argv, argument_table,
2045 POPT_CONTEXT_NO_EXEC);
2047 while ((c = poptGetNextOpt (ctx)) > 0) {
2048 process_args (ctx, c, poptGetOptArg (ctx));
2051 if (c < -1) {
2052 print_mc_usage (ctx, stderr);
2053 fprintf (stderr, "%s: %s\n",
2054 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2055 poptStrerror (c));
2056 exit (1);
2059 tmp = poptGetArg (ctx);
2062 * Check for special invocation names mcedit and mcview,
2063 * if none apply then set the current directory and the other
2064 * directory from the command line arguments
2066 base = x_basename (argv[0]);
2067 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2068 edit_one_file = "";
2069 if (tmp) {
2071 * Check for filename:lineno, followed by an optional colon.
2072 * This format is used by many programs (especially compilers)
2073 * in error messages and warnings. It is supported so that
2074 * users can quickly copy and paste file locations.
2076 char *end = tmp + strlen (tmp), *p = end;
2077 if (p > tmp && p[-1] == ':')
2078 p--;
2079 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2080 p--;
2081 if (tmp < p && p < end && p[-1] == ':') {
2082 struct stat st;
2083 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2085 * Check that the file before the colon actually exists.
2086 * If it doesn't exist, revert to the old behavior.
2088 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2089 edit_one_file = fname;
2090 edit_one_file_start_line = atoi (p);
2091 } else {
2092 g_free (fname);
2093 goto try_plus_filename;
2095 } else {
2096 try_plus_filename:
2097 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2098 int start_line = atoi (tmp);
2099 if (start_line > 0) {
2100 char *file = poptGetArg (ctx);
2101 if (file) {
2102 tmp = file;
2103 edit_one_file_start_line = start_line;
2107 edit_one_file = g_strdup (tmp);
2110 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2111 if (tmp)
2112 view_one_file = g_strdup (tmp);
2113 else {
2114 fputs ("No arguments given to the viewer\n", stderr);
2115 exit (1);
2117 } else {
2118 /* sets the current dir and the other dir */
2119 if (tmp) {
2120 this_dir = g_strdup (tmp);
2121 if ((tmp = poptGetArg (ctx)))
2122 other_dir = g_strdup (tmp);
2126 poptFreeContext (ctx);
2130 main (int argc, char *argv[])
2132 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2133 setlocale (LC_ALL, "");
2134 bindtextdomain ("mc", LOCALEDIR);
2135 textdomain ("mc");
2137 /* Set up temporary directory */
2138 mc_tmpdir ();
2140 OS_Setup ();
2142 /* This variable is used by the subshell */
2143 home_dir = getenv ("HOME");
2144 if (!home_dir) {
2145 /* mc_home was computed by OS_Setup */
2146 home_dir = mc_home;
2149 str_init_strings (NULL);
2151 vfs_init ();
2153 #ifdef USE_INTERNAL_EDIT
2154 for ( int i = 0; i < MAX_HISTORY_MOVETO; i++ ) {
2155 edit_history_moveto[i].filename = NULL;
2156 edit_history_moveto[i].line = -1;
2158 #endif
2160 #ifdef HAVE_SLANG
2161 SLtt_Ignore_Beep = 1;
2162 #endif
2164 handle_args (argc, argv);
2166 /* NOTE: This has to be called before slang_init or whatever routine
2167 calls any define_sequence */
2168 init_key ();
2170 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2171 handle_console (CONSOLE_INIT);
2173 #ifdef HAVE_SUBSHELL_SUPPORT
2174 /* Don't use subshell when invoked as viewer or editor */
2175 if (edit_one_file || view_one_file)
2176 use_subshell = 0;
2178 if (use_subshell)
2179 subshell_get_console_attributes ();
2180 #endif /* HAVE_SUBSHELL_SUPPORT */
2182 /* Install the SIGCHLD handler; must be done before init_subshell() */
2183 init_sigchld ();
2185 /* We need this, since ncurses endwin () doesn't restore the signals */
2186 save_stop_handler ();
2188 /* Must be done before init_subshell, to set up the terminal size: */
2189 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2190 #ifdef HAVE_SLANG
2191 slang_init ();
2192 #endif
2193 /* NOTE: This call has to be after slang_init. It's the small part from
2194 the previous init_key which had to be moved after the call of slang_init */
2195 init_key_input_fd ();
2197 load_setup ();
2199 init_curses ();
2201 init_xterm_support ();
2203 #ifdef HAVE_SUBSHELL_SUPPORT
2205 /* Done here to ensure that the subshell doesn't */
2206 /* inherit the file descriptors opened below, etc */
2207 if (use_subshell)
2208 init_subshell ();
2210 #endif /* HAVE_SUBSHELL_SUPPORT */
2212 /* Removing this from the X code let's us type C-c */
2213 load_key_defs ();
2215 /* Also done after init_subshell, to save any shell init file messages */
2216 if (console_flag)
2217 handle_console (CONSOLE_SAVE);
2219 if (alternate_plus_minus)
2220 application_keypad_mode ();
2222 #ifdef HAVE_SUBSHELL_SUPPORT
2223 if (use_subshell) {
2224 prompt = strip_ctrl_codes (subshell_prompt);
2225 if (!prompt)
2226 prompt = "";
2227 } else
2228 #endif /* HAVE_SUBSHELL_SUPPORT */
2229 prompt = (geteuid () == 0) ? "# " : "$ ";
2231 /* Program main loop */
2232 if (!midnight_shutdown)
2233 do_nc ();
2235 /* Save the tree store */
2236 tree_store_save ();
2238 /* Virtual File System shutdown */
2239 vfs_shut ();
2241 flush_extension_file (); /* does only free memory */
2243 endwin ();
2244 #ifdef HAVE_SLANG
2245 slang_shutdown ();
2246 #endif
2248 if (console_flag && !(quit & SUBSHELL_EXIT))
2249 handle_console (CONSOLE_RESTORE);
2250 if (alternate_plus_minus)
2251 numeric_keypad_mode ();
2253 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2255 if (console_flag)
2256 handle_console (CONSOLE_DONE);
2257 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2259 if (last_wd_file && last_wd_string && !print_last_revert
2260 && !edit_one_file && !view_one_file) {
2261 int last_wd_fd =
2262 open (last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2263 S_IRUSR | S_IWUSR);
2265 if (last_wd_fd != -1) {
2266 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2267 close (last_wd_fd);
2270 g_free (last_wd_string);
2272 g_free (mc_home);
2273 done_key ();
2274 #ifdef HAVE_CHARSET
2275 free_codepages_list ();
2276 #endif
2277 str_uninit_strings ();
2279 g_free (this_dir);
2280 g_free (other_dir);
2282 #ifdef USE_INTERNAL_EDIT
2283 for ( int i = 0; i < MAX_HISTORY_MOVETO; i++ ) {
2284 g_free(edit_history_moveto[i].filename);
2286 #endif
2288 return 0;