Rework strutils for usage GString instread of self-made buffers.
[midnight-commander.git] / src / main.c
blobd9b6d783ddbe02f9c41da087239a606207d404f4
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;
155 #endif /* !HAVE_CHARSET */
157 /* If true use the internal viewer */
158 int use_internal_view = 1;
160 /* Have we shown the fast-reload warning in the past? */
161 int fast_reload_w = 0;
163 /* Move page/item? When clicking on the top or bottom of a panel */
164 int mouse_move_pages = 1;
166 /* If true: l&r arrows are used to chdir if the input line is empty */
167 int navigate_with_arrows = 0;
169 /* If true use +, -, | for line drawing */
170 int force_ugly_line_drawing = 0;
172 /* If true program softkeys (HP terminals only) on startup and after every
173 command ran in the subshell to the description found in the termcap/terminfo
174 database */
175 int reset_hp_softkeys = 0;
177 /* The prompt */
178 const char *prompt = NULL;
180 /* The widget where we draw the prompt */
181 WLabel *the_prompt;
183 /* The hint bar */
184 WLabel *the_hint;
186 /* The button bar */
187 WButtonBar *the_bar;
189 /* For slow terminals */
190 int slow_terminal = 0;
192 /* Mouse type: GPM, xterm or none */
193 Mouse_Type use_mouse_p = MOUSE_NONE;
195 /* If true, assume we are running on an xterm terminal */
196 static int force_xterm = 0;
198 /* If on, default for "No" in delete operations */
199 int safe_delete = 0;
201 /* Controls screen clearing before an exec */
202 int clear_before_exec = 1;
204 /* Asks for confirmation before deleting a file */
205 int confirm_delete = 1;
207 /* Asks for confirmation before deleting a hotlist entry */
208 int confirm_directory_hotlist_delete = 1;
210 /* Asks for confirmation before overwriting a file */
211 int confirm_overwrite = 1;
213 /* Asks for confirmation before executing a program by pressing enter */
214 int confirm_execute = 0;
216 /* Asks for confirmation before leaving the program */
217 int confirm_exit = 1;
219 /* Asks for confirmation when using F3 to view a directory and there
220 are tagged files */
221 int confirm_view_dir = 0;
223 /* This flag indicates if the pull down menus by default drop down */
224 int drop_menus = 0;
226 /* The dialog handle for the main program */
227 Dlg_head *midnight_dlg;
229 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
230 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
231 int update_prompt = 0;
233 /* The home directory */
234 const char *home_dir = NULL;
236 /* The value of the other directory, only used when loading the setup */
237 char *other_dir = NULL;
239 /* Only used at program boot */
240 int boot_current_is_left = 1;
242 static char *this_dir = NULL;
244 /* If this is true, then when browsing the tree the other window will
245 * automatically reload it's directory with the contents of the currently
246 * selected directory.
248 int xtree_mode = 0;
250 /* If set, then print to the given file the last directory we were at */
251 static char *last_wd_file = NULL;
252 static char *last_wd_string = NULL;
253 /* Set to 1 to suppress printing the last directory */
254 static int print_last_revert = 0;
256 /* Force colors, only used by Slang */
257 int force_colors = 0;
259 /* colors specified on the command line: they override any other setting */
260 char *command_line_colors = NULL;
262 /* File name to view if argument was supplied */
263 static const char *view_one_file = NULL;
265 /* File name to edit if argument was supplied */
266 const char *edit_one_file = NULL;
268 /* Line to start the editor on */
269 static int edit_one_file_start_line = 0;
271 /* Used so that widgets know if they are being destroyed or
272 shut down */
273 int midnight_shutdown = 0;
275 /* The user's shell */
276 const char *shell = NULL;
278 /* mc_home: The home of MC */
279 char *mc_home = NULL;
281 char cmd_buf[512];
283 #ifdef USE_INTERNAL_EDIT
284 int edit_stack_iterator = 0;
285 struct edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
286 #endif
288 static void
289 reload_panelized (WPanel *panel)
291 int i, j;
292 dir_list *list = &panel->dir;
294 if (panel != current_panel)
295 mc_chdir (panel->cwd);
297 for (i = 0, j = 0; i < panel->count; i++) {
298 if (list->list[i].f.marked) {
299 /* Unmark the file in advance. In case the following mc_lstat
300 * fails we are done, else we have to mark the file again
301 * (Note: do_file_mark depends on a valid "list->list [i].buf").
302 * IMO that's the best way to update the panel's summary status
303 * -- Norbert
305 do_file_mark (panel, i, 0);
307 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
308 g_free (list->list[i].fname);
309 continue;
311 if (list->list[i].f.marked)
312 do_file_mark (panel, i, 1);
313 if (j != i)
314 list->list[j] = list->list[i];
315 j++;
317 if (j == 0)
318 panel->count = set_zero_dir (list);
319 else
320 panel->count = j;
322 if (panel != current_panel)
323 mc_chdir (current_panel->cwd);
326 static void
327 update_one_panel_widget (WPanel *panel, int force_update,
328 const char *current_file)
330 int free_pointer;
331 char *my_current_file = NULL;
333 if (force_update & UP_RELOAD) {
334 panel->is_panelized = 0;
335 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
336 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
339 /* If current_file == -1 (an invalid pointer) then preserve selection */
340 if (current_file == UP_KEEPSEL) {
341 free_pointer = 1;
342 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
343 current_file = my_current_file;
344 } else
345 free_pointer = 0;
347 if (panel->is_panelized)
348 reload_panelized (panel);
349 else
350 panel_reload (panel);
352 try_to_select (panel, current_file);
353 panel->dirty = 1;
355 if (free_pointer)
356 g_free (my_current_file);
359 void
360 panel_clean_dir (WPanel *panel)
362 int count = panel->count;
364 panel->count = 0;
365 panel->top_file = 0;
366 panel->selected = 0;
367 panel->marked = 0;
368 panel->dirs_marked = 0;
369 panel->total = 0;
370 panel->searching = 0;
371 panel->is_panelized = 0;
372 panel->dirty = 1;
374 clean_dir (&panel->dir, count);
377 static void
378 update_one_panel (int which, int force_update, const char *current_file)
380 WPanel *panel;
382 if (get_display_type (which) != view_listing)
383 return;
385 panel = (WPanel *) get_panel_widget (which);
386 update_one_panel_widget (panel, force_update, current_file);
389 /* This routine reloads the directory in both panels. It tries to
390 * select current_file in current_panel and other_file in other_panel.
391 * If current_file == -1 then it automatically sets current_file and
392 * other_file to the currently selected files in the panels.
394 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
395 * will not reload the other panel.
397 void
398 update_panels (int force_update, const char *current_file)
400 int reload_other = !(force_update & UP_ONLY_CURRENT);
401 WPanel *panel;
403 update_one_panel (get_current_index (), force_update, current_file);
404 if (reload_other)
405 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
407 if (get_current_type () == view_listing)
408 panel = (WPanel *) get_panel_widget (get_current_index ());
409 else
410 panel = (WPanel *) get_panel_widget (get_other_index ());
412 mc_chdir (panel->cwd);
415 /* Save current stat of directories to avoid reloading the panels */
416 /* when no modifications have taken place */
417 void
418 save_cwds_stat (void)
420 if (fast_reload) {
421 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
422 if (get_other_type () == view_listing)
423 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
427 #ifdef HAVE_SUBSHELL_SUPPORT
428 void
429 do_possible_cd (const char *new_dir)
431 if (!do_cd (new_dir, cd_exact))
432 message (D_ERROR, _("Warning"),
433 _(" The Commander can't change to the directory that \n"
434 " the subshell claims you are in. Perhaps you have \n"
435 " deleted your working directory, or given yourself \n"
436 " extra access permissions with the \"su\" command? "));
439 void
440 do_update_prompt (void)
442 if (update_prompt) {
443 printf ("%s", subshell_prompt);
444 fflush (stdout);
445 update_prompt = 0;
448 #endif /* HAVE_SUBSHELL_SUPPORT */
450 void
451 change_panel (void)
453 free_completions (cmdline);
454 dlg_one_down (midnight_dlg);
457 /* Stop MC main dialog and the current dialog if it exists.
458 * Needed to provide fast exit from MC viewer or editor on shell exit */
459 static void
460 stop_dialogs (void)
462 midnight_dlg->running = 0;
463 if (current_dlg) {
464 current_dlg->running = 0;
468 static int
469 quit_cmd_internal (int quiet)
471 int q = quit;
473 if (quiet || !confirm_exit) {
474 q = 1;
475 } else {
476 if (query_dialog
477 (_(" The Midnight Commander "),
478 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
479 2, _("&Yes"), _("&No")) == 0)
480 q = 1;
482 if (q) {
483 #ifdef HAVE_SUBSHELL_SUPPORT
484 if (!use_subshell)
485 stop_dialogs ();
486 else if ((q = exit_subshell ()))
487 #endif
488 stop_dialogs ();
490 if (q)
491 quit |= 1;
492 return quit;
495 static void
496 quit_cmd (void)
498 quit_cmd_internal (0);
501 void
502 quiet_quit_cmd (void)
504 print_last_revert = 1;
505 quit_cmd_internal (1);
509 * Touch window and refresh window functions
512 /* This routine untouches the first line on both panels in order */
513 /* to avoid the refreshing the menu bar */
515 void
516 repaint_screen (void)
518 do_refresh ();
519 mc_refresh ();
522 /* Wrapper for do_subshell_chdir, check for availability of subshell */
523 void
524 subshell_chdir (const char *directory)
526 #ifdef HAVE_SUBSHELL_SUPPORT
527 if (use_subshell) {
528 if (vfs_current_is_local ())
529 do_subshell_chdir (directory, 0, 1);
531 #endif /* HAVE_SUBSHELL_SUPPORT */
534 void
535 directory_history_add (struct WPanel *panel, const char *dir)
537 char *tmp;
539 tmp = g_strdup (dir);
540 strip_password (tmp, 1);
542 panel->dir_history = list_append_unique (panel->dir_history, tmp);
546 * If we moved to the parent directory move the selection pointer to
547 * the old directory name; If we leave VFS dir, remove FS specificator.
549 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
551 static const char *
552 get_parent_dir_name (const char *cwd, const char *lwd)
554 const char *p;
555 if (strlen (lwd) > strlen (cwd))
556 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
557 (strlen (cwd) == p - lwd || (p == lwd && cwd[0] == PATH_SEP &&
558 cwd[1] == '\0'))) {
559 return (p + 1);
561 return NULL;
565 * Changes the current directory of the panel.
566 * Don't record change in the directory history.
568 static int
569 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
571 const char *directory;
572 char *olddir;
573 char temp[MC_MAXPATHLEN];
574 char *translated_url;
576 if (cd_type == cd_parse_command) {
577 while (*new_dir == ' ')
578 new_dir++;
581 olddir = g_strdup (panel->cwd);
582 new_dir = translated_url = vfs_translate_url (new_dir);
584 /* Convert *new_path to a suitable pathname, handle ~user */
586 if (cd_type == cd_parse_command) {
587 if (!strcmp (new_dir, "-")) {
588 strcpy (temp, panel->lwd);
589 new_dir = temp;
592 directory = *new_dir ? new_dir : home_dir;
594 if (mc_chdir (directory) == -1) {
595 strcpy (panel->cwd, olddir);
596 g_free (olddir);
597 g_free (translated_url);
598 return 0;
600 g_free (translated_url);
602 /* Success: save previous directory, shutdown status of previous dir */
603 strcpy (panel->lwd, olddir);
604 free_completions (cmdline);
606 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
608 vfs_release_path (olddir);
610 subshell_chdir (panel->cwd);
612 /* Reload current panel */
613 panel_clean_dir (panel);
614 panel->count =
615 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
616 panel->reverse, panel->case_sensitive,
617 panel->exec_first, panel->filter);
618 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
619 load_hint (0);
620 panel->dirty = 1;
621 update_xterm_title_path ();
623 g_free (olddir);
625 return 1;
629 * Changes the current directory of the panel.
630 * Record change in the directory history.
633 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
635 int r;
637 r = _do_panel_cd (panel, new_dir, cd_type);
638 if (r)
639 directory_history_add (panel, panel->cwd);
640 return r;
644 do_cd (const char *new_dir, enum cd_enum exact)
646 return (do_panel_cd (current_panel, new_dir, exact));
649 void
650 directory_history_next (WPanel *panel)
652 GList *nextdir;
654 nextdir = g_list_next (panel->dir_history);
656 if (!nextdir)
657 return;
659 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
660 panel->dir_history = nextdir;
663 void
664 directory_history_prev (WPanel *panel)
666 GList *prevdir;
668 prevdir = g_list_previous (panel->dir_history);
670 if (!prevdir)
671 return;
673 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
674 panel->dir_history = prevdir;
677 void
678 directory_history_list (WPanel *panel)
680 char *s;
682 if (!panel->dir_history)
683 return;
685 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
687 if (!s)
688 return;
690 if (_do_panel_cd (panel, s, cd_exact))
691 directory_history_add (panel, panel->cwd);
692 else
693 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
694 g_free (s);
697 #ifdef HAVE_SUBSHELL_SUPPORT
699 load_prompt (int fd, void *unused)
701 (void) fd;
702 (void) unused;
704 if (!read_subshell_prompt ())
705 return 0;
707 /* Don't actually change the prompt if it's invisible */
708 if (current_dlg == midnight_dlg && command_prompt) {
709 char *tmp_prompt;
710 int prompt_len;
712 tmp_prompt = strip_ctrl_codes (subshell_prompt);
713 prompt_len = str_term_width1 (tmp_prompt);
715 /* Check for prompts too big */
716 if (COLS > 8 && prompt_len > COLS - 8) {
717 tmp_prompt[COLS - 8] = '\0';
718 prompt_len = COLS - 8;
720 prompt = tmp_prompt;
721 label_set_text (the_prompt, prompt);
722 winput_set_origin ((WInput *) cmdline, prompt_len,
723 COLS - prompt_len);
725 /* since the prompt has changed, and we are called from one of the
726 * get_event channels, the prompt updating does not take place
727 * automatically: force a cursor update and a screen refresh
729 update_cursor (midnight_dlg);
730 mc_refresh ();
732 update_prompt = 1;
733 return 0;
735 #endif /* HAVE_SUBSHELL_SUPPORT */
737 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
739 maybe_cd (int move_up_dir)
741 if (navigate_with_arrows) {
742 if (!cmdline->buffer[0]) {
743 if (move_up_dir) {
744 do_cd ("..", cd_exact);
745 return 1;
747 if (S_ISDIR (selection (current_panel)->st.st_mode)
748 || link_isdir (selection (current_panel))) {
749 do_cd (selection (current_panel)->fname, cd_exact);
750 return 1;
754 return 0;
757 static void
758 sort_cmd (void)
760 WPanel *p;
761 sortfn *sort_order;
763 if (!SELECTED_IS_PANEL)
764 return;
766 p = MENU_PANEL;
767 sort_order = sort_box (p->sort_type, &p->reverse,
768 &p->case_sensitive,
769 &p->exec_first);
771 panel_set_sort_order (p, sort_order);
775 static void
776 treebox_cmd (void)
778 char *sel_dir;
780 sel_dir = tree_box (selection (current_panel)->fname);
781 if (sel_dir) {
782 do_cd (sel_dir, cd_exact);
783 g_free (sel_dir);
787 #ifdef LISTMODE_EDITOR
788 static void
789 listmode_cmd (void)
791 char *newmode;
793 if (get_current_type () != view_listing)
794 return;
796 newmode = listmode_edit (current_panel->user_format);
797 if (!newmode)
798 return;
800 g_free (current_panel->user_format);
801 current_panel->list_type = list_user;
802 current_panel->user_format = newmode;
803 set_panel_formats (current_panel);
805 do_refresh ();
807 #endif /* LISTMODE_EDITOR */
809 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
810 static menu_entry LeftMenu[] = {
811 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
812 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
813 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
814 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
815 {' ', "", NULL_HOTKEY, 0},
816 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
817 {' ', "", NULL_HOTKEY, 0},
818 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
819 {' ', "",NULL_HOTKEY, 0},
820 {' ', N_("&Encoding..."), NULL_HOTKEY, encoding_cmd},
821 #ifdef USE_NETCODE
822 {' ', "", NULL_HOTKEY, 0},
823 #ifdef WITH_MCFS
824 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
825 #endif
826 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
827 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
828 #ifdef WITH_SMBFS
829 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
830 #endif
831 #endif
832 {' ', "", NULL_HOTKEY, 0},
833 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
836 static menu_entry RightMenu[] = {
837 {' ', N_("&Listing mode..."), NULL_HOTKEY, listing_cmd},
838 {' ', N_("&Quick view C-x q"), NULL_HOTKEY, quick_view_cmd},
839 {' ', N_("&Info C-x i"), NULL_HOTKEY, info_cmd},
840 {' ', N_("&Tree"), NULL_HOTKEY, tree_cmd},
841 {' ', "", NULL_HOTKEY, 0},
842 {' ', N_("&Sort order..."), NULL_HOTKEY, sort_cmd},
843 {' ', "", NULL_HOTKEY, 0},
844 {' ', N_("&Filter..."), NULL_HOTKEY, filter_cmd},
845 {' ', "",NULL_HOTKEY, 0},
846 {' ', N_("&Encoding..."), NULL_HOTKEY, encoding_cmd},
847 #ifdef USE_NETCODE
848 {' ', "", NULL_HOTKEY, 0},
849 #ifdef WITH_MCFS
850 {' ', N_("&Network link..."), NULL_HOTKEY, netlink_cmd},
851 #endif
852 {' ', N_("FT&P link..."), NULL_HOTKEY, ftplink_cmd},
853 {' ', N_("S&hell link..."), NULL_HOTKEY, fishlink_cmd},
854 #ifdef WITH_SMBFS
855 {' ', N_("SM&B link..."), NULL_HOTKEY, smblink_cmd},
856 #endif
857 #endif
858 {' ', "", NULL_HOTKEY, 0},
859 {' ', N_("&Rescan C-r"), NULL_HOTKEY, reread_cmd}
862 static menu_entry FileMenu[] = {
863 {' ', N_("&User menu F2"), NULL_HOTKEY, user_file_menu_cmd},
864 {' ', N_("&View F3"), NULL_HOTKEY, view_cmd},
865 {' ', N_("Vie&w file... "), NULL_HOTKEY, view_file_cmd},
866 {' ', N_("&Filtered view M-!"), NULL_HOTKEY, filtered_view_cmd},
867 {' ', N_("&Edit F4"), NULL_HOTKEY, edit_cmd},
868 {' ', N_("&Copy F5"), NULL_HOTKEY, copy_cmd},
869 {' ', N_("c&Hmod C-x c"), NULL_HOTKEY, chmod_cmd},
870 {' ', N_("&Link C-x l"), NULL_HOTKEY, link_cmd},
871 {' ', N_("&SymLink C-x s"), NULL_HOTKEY, symlink_cmd},
872 {' ', N_("edit s&Ymlink C-x C-s"), NULL_HOTKEY, edit_symlink_cmd},
873 {' ', N_("ch&Own C-x o"), NULL_HOTKEY, chown_cmd},
874 {' ', N_("&Advanced chown "), NULL_HOTKEY, chown_advanced_cmd},
875 {' ', N_("&Rename/Move F6"), NULL_HOTKEY, ren_cmd},
876 {' ', N_("&Mkdir F7"), NULL_HOTKEY, mkdir_cmd},
877 {' ', N_("&Delete F8"), NULL_HOTKEY, delete_cmd},
878 {' ', N_("&Quick cd M-c"), NULL_HOTKEY, quick_cd_cmd},
879 {' ', "", NULL_HOTKEY, 0},
880 {' ', N_("select &Group M-+"), NULL_HOTKEY, select_cmd},
881 {' ', N_("u&Nselect group M-\\"), NULL_HOTKEY, unselect_cmd},
882 {' ', N_("reverse selec&Tion M-*"), NULL_HOTKEY, reverse_selection_cmd},
883 {' ', "", NULL_HOTKEY, 0},
884 {' ', N_("e&Xit F10"), NULL_HOTKEY, quit_cmd}
887 static menu_entry CmdMenu[] = {
888 /* I know, I'm lazy, but the tree widget when it's not running
889 * as a panel still has some problems, I have not yet finished
890 * the WTree widget port, sorry.
892 {' ', N_("&Directory tree"), NULL_HOTKEY, treebox_cmd},
893 {' ', N_("&Find file M-?"), NULL_HOTKEY, find_cmd},
894 {' ', N_("s&Wap panels C-u"), NULL_HOTKEY, swap_cmd},
895 {' ', N_("switch &Panels on/off C-o"), NULL_HOTKEY, view_other_cmd},
896 {' ', N_("&Compare directories C-x d"), NULL_HOTKEY, compare_dirs_cmd},
897 {' ', N_("e&Xternal panelize C-x !"), NULL_HOTKEY, external_panelize},
898 {' ', N_("show directory s&Izes"), NULL_HOTKEY, dirsizes_cmd},
899 {' ', "", NULL_HOTKEY, 0},
900 {' ', N_("command &History"), NULL_HOTKEY, history_cmd},
901 {' ', N_("di&Rectory hotlist C-\\"), NULL_HOTKEY, quick_chdir_cmd},
902 #ifdef USE_VFS
903 {' ', N_("&Active VFS list C-x a"), NULL_HOTKEY, reselect_vfs},
904 #endif
905 #ifdef WITH_BACKGROUND
906 {' ', N_("&Background jobs C-x j"), NULL_HOTKEY, jobs_cmd},
907 #endif
908 {' ', "", NULL_HOTKEY, 0},
909 #ifdef USE_EXT2FSLIB
910 {' ', N_("&Undelete files (ext2fs only)"), NULL_HOTKEY, undelete_cmd},
911 #endif
912 #ifdef LISTMODE_EDITOR
913 {' ', N_("&Listing format edit"), NULL_HOTKEY, listmode_cmd},
914 #endif
915 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
916 {' ', "", NULL_HOTKEY, 0},
917 #endif
918 {' ', N_("Edit &extension file"), NULL_HOTKEY, ext_cmd},
919 {' ', N_("Edit &menu file"), NULL_HOTKEY, edit_mc_menu_cmd},
920 #ifdef USE_INTERNAL_EDIT
921 {' ', N_("Edit edi&tor menu file"), NULL_HOTKEY, edit_user_menu_cmd},
922 {' ', N_("Edit &syntax file"), NULL_HOTKEY, edit_syntax_cmd}
923 #endif /* USE_INTERNAL_EDIT */
926 /* Must keep in sync with the constants in menu_cmd */
927 static menu_entry OptMenu[] = {
928 {' ', N_("&Configuration..."), NULL_HOTKEY, configure_box},
929 {' ', N_("&Layout..."), NULL_HOTKEY, layout_cmd},
930 {' ', N_("c&Onfirmation..."), NULL_HOTKEY, confirm_box},
931 {' ', N_("&Display bits..."), NULL_HOTKEY, display_bits_box},
932 {' ', N_("learn &Keys..."), NULL_HOTKEY, learn_keys},
933 #ifdef USE_VFS
934 {' ', N_("&Virtual FS..."), NULL_HOTKEY, configure_vfs},
935 #endif /* !USE_VFS */
936 {' ', "", NULL_HOTKEY, 0},
937 {' ', N_("&Save setup"), NULL_HOTKEY, save_setup_cmd}
940 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
942 static Menu *MenuBar[5];
944 void
945 init_menu (void)
947 MenuBar[0] =
948 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
949 LeftMenu, menu_entries (LeftMenu),
950 "[Left and Right Menus]");
951 MenuBar[1] =
952 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
953 "[File Menu]");
954 MenuBar[2] =
955 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
956 "[Command Menu]");
957 MenuBar[3] =
958 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
959 "[Options Menu]");
960 MenuBar[4] =
961 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
962 RightMenu, menu_entries (RightMenu),
963 "[Left and Right Menus]");
966 void
967 done_menu (void)
969 int i;
971 for (i = 0; i < 5; i++) {
972 destroy_menu (MenuBar[i]);
976 static void
977 menu_last_selected_cmd (void)
979 the_menubar->active = 1;
980 the_menubar->dropped = drop_menus;
981 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
982 dlg_select_widget (the_menubar);
985 static void
986 menu_cmd (void)
988 if (the_menubar->active)
989 return;
991 if ((get_current_index () == 0) ^ (!current_panel->active))
992 the_menubar->selected = 0;
993 else
994 the_menubar->selected = 4;
995 menu_last_selected_cmd ();
998 /* Flag toggling functions */
999 void
1000 toggle_fast_reload (void)
1002 fast_reload = !fast_reload;
1003 if (fast_reload_w == 0 && fast_reload) {
1004 message (D_NORMAL, _(" Information "),
1006 (" Using the fast reload option may not reflect the exact \n"
1007 " directory contents. In this case you'll need to do a \n"
1008 " manual reload of the directory. See the man page for \n"
1009 " the details. "));
1010 fast_reload_w = 1;
1014 void
1015 toggle_mix_all_files (void)
1017 mix_all_files = !mix_all_files;
1018 update_panels (UP_RELOAD, UP_KEEPSEL);
1021 void
1022 toggle_show_backup (void)
1024 show_backups = !show_backups;
1025 update_panels (UP_RELOAD, UP_KEEPSEL);
1028 void
1029 toggle_show_hidden (void)
1031 show_dot_files = !show_dot_files;
1032 update_panels (UP_RELOAD, UP_KEEPSEL);
1036 * Just a hack for allowing url-like pathnames to be accepted from the
1037 * command line.
1039 static void
1040 translated_mc_chdir (char *dir)
1042 char *newdir;
1044 newdir = vfs_translate_url (dir);
1045 mc_chdir (newdir);
1046 g_free (newdir);
1049 static void
1050 create_panels (void)
1052 int current_index;
1053 int other_index;
1054 int current_mode;
1055 int other_mode;
1056 char original_dir[1024];
1058 original_dir[0] = 0;
1060 if (boot_current_is_left) {
1061 current_index = 0;
1062 other_index = 1;
1063 current_mode = startup_left_mode;
1064 other_mode = startup_right_mode;
1065 } else {
1066 current_index = 1;
1067 other_index = 0;
1068 current_mode = startup_right_mode;
1069 other_mode = startup_left_mode;
1071 /* Creates the left panel */
1072 if (this_dir) {
1073 if (other_dir) {
1074 /* Ok, user has specified two dirs, save the original one,
1075 * since we may not be able to chdir to the proper
1076 * second directory later
1078 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1080 translated_mc_chdir (this_dir);
1082 set_display_type (current_index, current_mode);
1084 /* The other panel */
1085 if (other_dir) {
1086 if (original_dir[0])
1087 translated_mc_chdir (original_dir);
1088 translated_mc_chdir (other_dir);
1090 set_display_type (other_index, other_mode);
1092 if (startup_left_mode == view_listing) {
1093 current_panel = left_panel;
1094 } else {
1095 if (right_panel)
1096 current_panel = right_panel;
1097 else
1098 current_panel = left_panel;
1101 /* Create the nice widgets */
1102 cmdline = command_new (0, 0, 0);
1103 the_prompt = label_new (0, 0, prompt);
1104 the_prompt->transparent = 1;
1105 the_bar = buttonbar_new (keybar_visible);
1107 the_hint = label_new (0, 0, 0);
1108 the_hint->transparent = 1;
1109 the_hint->auto_adjust_cols = 0;
1110 the_hint->widget.cols = COLS;
1112 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1115 static void
1116 copy_current_pathname (void)
1118 if (!command_prompt)
1119 return;
1121 command_insert (cmdline, current_panel->cwd, 0);
1122 if (current_panel->cwd[strlen (current_panel->cwd) - 1] != PATH_SEP)
1123 command_insert (cmdline, PATH_SEP_STR, 0);
1126 static void
1127 copy_other_pathname (void)
1129 if (get_other_type () != view_listing)
1130 return;
1132 if (!command_prompt)
1133 return;
1135 command_insert (cmdline, other_panel->cwd, 0);
1136 if (other_panel->cwd[strlen (other_panel->cwd) - 1] != PATH_SEP)
1137 command_insert (cmdline, PATH_SEP_STR, 0);
1140 static void
1141 copy_readlink (WPanel *panel)
1143 if (!command_prompt)
1144 return;
1145 if (S_ISLNK (selection (panel)->st.st_mode)) {
1146 char buffer[MC_MAXPATHLEN];
1147 char *p =
1148 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1149 int i;
1151 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1152 g_free (p);
1153 if (i > 0) {
1154 buffer[i] = 0;
1155 command_insert (cmdline, buffer, 1);
1160 static void
1161 copy_current_readlink (void)
1163 copy_readlink (current_panel);
1166 static void
1167 copy_other_readlink (void)
1169 if (get_other_type () != view_listing)
1170 return;
1171 copy_readlink (other_panel);
1174 /* Insert the selected file name into the input line */
1175 static void
1176 copy_prog_name (void)
1178 char *tmp;
1179 if (!command_prompt)
1180 return;
1182 if (get_current_type () == view_tree) {
1183 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1184 tmp = tree_selected_name (tree);
1185 } else
1186 tmp = selection (current_panel)->fname;
1188 command_insert (cmdline, tmp, 1);
1191 static void
1192 copy_tagged (WPanel *panel)
1194 int i;
1196 if (!command_prompt)
1197 return;
1198 input_disable_update (cmdline);
1199 if (panel->marked) {
1200 for (i = 0; i < panel->count; i++) {
1201 if (panel->dir.list[i].f.marked)
1202 command_insert (cmdline, panel->dir.list[i].fname, 1);
1204 } else {
1205 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1208 input_enable_update (cmdline);
1211 static void
1212 copy_current_tagged (void)
1214 copy_tagged (current_panel);
1217 static void
1218 copy_other_tagged (void)
1220 if (get_other_type () != view_listing)
1221 return;
1222 copy_tagged (other_panel);
1225 static void
1226 init_labels (void)
1228 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1229 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1230 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1231 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1234 static const key_map ctl_x_map[] = {
1235 {XCTRL ('c'), quit_cmd},
1236 {'d', compare_dirs_cmd},
1237 #ifdef USE_VFS
1238 {'a', reselect_vfs},
1239 #endif /* USE_VFS */
1240 {'p', copy_current_pathname},
1241 {XCTRL ('p'), copy_other_pathname},
1242 {'t', copy_current_tagged},
1243 {XCTRL ('t'), copy_other_tagged},
1244 {'c', chmod_cmd},
1245 {'o', chown_cmd},
1246 {'r', copy_current_readlink},
1247 {XCTRL ('r'), copy_other_readlink},
1248 {'l', link_cmd},
1249 {'s', symlink_cmd},
1250 {XCTRL ('s'), edit_symlink_cmd},
1251 {'i', info_cmd_no_menu},
1252 {'q', quick_cmd_no_menu},
1253 {'h', add2hotlist_cmd},
1254 {'!', external_panelize},
1255 #ifdef WITH_BACKGROUND
1256 {'j', jobs_cmd},
1257 #endif /* WITH_BACKGROUND */
1258 {0, 0}
1261 static int ctl_x_map_enabled = 0;
1263 static void
1264 ctl_x_cmd (void)
1266 ctl_x_map_enabled = 1;
1269 static void
1270 nothing (void)
1274 static const key_map default_map[] = {
1275 {KEY_F (19), menu_last_selected_cmd},
1276 {KEY_F (20), quiet_quit_cmd},
1278 {XCTRL ('@'), single_dirsize_cmd},
1280 /* Copy useful information to the command line */
1281 {ALT ('a'), copy_current_pathname},
1282 {ALT ('A'), copy_other_pathname},
1284 {ALT ('c'), quick_cd_cmd},
1286 /* To access the directory hotlist */
1287 {XCTRL ('\\'), quick_chdir_cmd},
1289 /* Suspend */
1290 {XCTRL ('z'), suspend_cmd},
1292 /* The filtered view command */
1293 {ALT ('!'), filtered_view_cmd},
1295 /* Find file */
1296 {ALT ('?'), find_cmd},
1298 /* Panel refresh */
1299 {XCTRL ('r'), reread_cmd},
1301 /* Toggle listing between long, user defined and full formats */
1302 {ALT ('t'), toggle_listing_cmd},
1304 /* Swap panels */
1305 {XCTRL ('u'), swap_cmd},
1307 /* View output */
1308 {XCTRL ('o'), view_other_cmd},
1310 /* Control-X keybindings */
1311 {XCTRL ('x'), ctl_x_cmd},
1313 /* Trap dlg's exit commands */
1314 {ESC_CHAR, nothing},
1315 {XCTRL ('c'), nothing},
1316 {XCTRL ('g'), nothing},
1317 {0, 0},
1320 static void
1321 setup_sigwinch (void)
1323 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && defined(SIGWINCH)
1324 struct sigaction act, oact;
1325 act.sa_handler = flag_winch;
1326 sigemptyset (&act.sa_mask);
1327 act.sa_flags = 0;
1328 #ifdef SA_RESTART
1329 act.sa_flags |= SA_RESTART;
1330 #endif
1331 sigaction (SIGWINCH, &act, &oact);
1332 #endif
1335 static void
1336 setup_pre (void)
1338 /* Call all the inits */
1339 #ifdef HAVE_CHARSET
1341 * Don't restrict the output on the screen manager level,
1342 * the translation tables take care of it.
1344 #define full_eight_bits (1)
1345 #define eight_bit_clean (1)
1346 #endif /* !HAVE_CHARSET */
1348 #ifndef HAVE_SLANG
1349 meta (stdscr, eight_bit_clean);
1350 #else
1351 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1352 #endif
1355 static void
1356 init_xterm_support (void)
1358 const char *termvalue;
1360 termvalue = getenv ("TERM");
1361 if (!termvalue || !(*termvalue)) {
1362 fputs (_("The TERM environment variable is unset!\n"), stderr);
1363 exit (1);
1366 /* Check mouse capabilities */
1367 xmouse_seq = tty_tgetstr ("Km");
1369 if (strcmp (termvalue, "cygwin") == 0) {
1370 force_xterm = 1;
1371 use_mouse_p = MOUSE_DISABLED;
1374 if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
1375 || strncmp (termvalue, "rxvt", 4) == 0
1376 || strcmp (termvalue, "Eterm") == 0
1377 || strcmp (termvalue, "dtterm") == 0) {
1378 xterm_flag = 1;
1380 /* Default to the standard xterm sequence */
1381 if (!xmouse_seq) {
1382 xmouse_seq = ESC_STR "[M";
1385 /* Enable mouse unless explicitly disabled by --nomouse */
1386 if (use_mouse_p != MOUSE_DISABLED) {
1387 const char *color_term = getenv ("COLORTERM");
1388 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1389 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1390 strcmp (termvalue, "Eterm") == 0) {
1391 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1392 } else {
1393 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1399 static void
1400 setup_mc (void)
1402 setup_pre ();
1403 init_menu ();
1404 create_panels ();
1405 setup_panels ();
1407 #ifdef HAVE_SUBSHELL_SUPPORT
1408 if (use_subshell)
1409 add_select_channel (subshell_pty, load_prompt, 0);
1410 #endif /* !HAVE_SUBSHELL_SUPPORT */
1412 setup_sigwinch ();
1414 if (baudrate () < 9600 || slow_terminal) {
1415 verbose = 0;
1417 init_mouse ();
1420 static void
1421 setup_dummy_mc ()
1423 char d[MC_MAXPATHLEN];
1425 mc_get_current_wd (d, MC_MAXPATHLEN);
1426 setup_mc ();
1427 mc_chdir (d);
1430 static void
1431 done_mc (void)
1433 disable_mouse ();
1435 done_menu ();
1437 /* Setup shutdown
1439 * We sync the profiles since the hotlist may have changed, while
1440 * we only change the setup data if we have the auto save feature set
1442 if (auto_save_setup)
1443 save_setup (); /* does also call save_hotlist */
1444 else
1445 save_hotlist ();
1446 done_screen ();
1447 vfs_add_current_stamps ();
1450 /* This should be called after destroy_dlg since panel widgets
1451 * save their state on the profiles
1453 static void
1454 done_mc_profile (void)
1456 if (!auto_save_setup)
1457 profile_forget_profile (profile_name);
1458 sync_profiles ();
1459 done_setup ();
1460 free_profiles ();
1463 static cb_ret_t
1464 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1466 int i;
1468 switch (msg) {
1470 case DLG_IDLE:
1471 /* We only need the first idle event */
1472 set_idle_proc (h, 0);
1473 if (auto_menu) {
1474 user_file_menu_cmd ();
1476 return MSG_HANDLED;
1478 case DLG_KEY:
1479 if (ctl_x_map_enabled) {
1480 ctl_x_map_enabled = 0;
1481 for (i = 0; ctl_x_map[i].key_code; i++)
1482 if (parm == ctl_x_map[i].key_code) {
1483 (*ctl_x_map[i].fn) ();
1484 return MSG_HANDLED;
1488 /* FIXME: should handle all menu shortcuts before this point */
1489 if (the_menubar->active)
1490 return MSG_NOT_HANDLED;
1492 if (parm == KEY_F (10)) {
1493 quit_cmd ();
1494 return MSG_HANDLED;
1497 if (parm == '\t')
1498 free_completions (cmdline);
1500 if (parm == '\n') {
1501 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1502 cmdline->buffer[i] == '\t'); i++);
1503 if (cmdline->buffer[i]) {
1504 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1505 return MSG_HANDLED;
1507 stuff (cmdline, "", 0);
1508 cmdline->point = 0;
1511 /* Ctrl-Enter and Alt-Enter */
1512 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1513 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1514 copy_prog_name ();
1515 return MSG_HANDLED;
1518 /* Ctrl-Shift-Enter */
1519 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1520 copy_current_pathname ();
1521 copy_prog_name ();
1522 return MSG_HANDLED;
1525 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1526 && !quote && !current_panel->searching) {
1527 if (!only_leading_plus_minus) {
1528 /* Special treatement, since the input line will eat them */
1529 if (parm == '+') {
1530 select_cmd ();
1531 return MSG_HANDLED;
1534 if (parm == '\\' || parm == '-') {
1535 unselect_cmd ();
1536 return MSG_HANDLED;
1539 if (parm == '*') {
1540 reverse_selection_cmd ();
1541 return MSG_HANDLED;
1543 } else if (!command_prompt || !cmdline->buffer[0]) {
1544 /* Special treatement '+', '-', '\', '*' only when this is
1545 * first char on input line
1548 if (parm == '+') {
1549 select_cmd ();
1550 return MSG_HANDLED;
1553 if (parm == '\\' || parm == '-') {
1554 unselect_cmd ();
1555 return MSG_HANDLED;
1558 if (parm == '*') {
1559 reverse_selection_cmd ();
1560 return MSG_HANDLED;
1564 return MSG_NOT_HANDLED;
1566 case DLG_HOTKEY_HANDLED:
1567 if ((get_current_type () == view_listing) && current_panel->searching) {
1568 current_panel->searching = 0;
1569 current_panel->dirty = 1;
1571 return MSG_HANDLED;
1573 case DLG_UNHANDLED_KEY:
1574 if (command_prompt) {
1575 int v;
1577 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1578 if (v)
1579 return v;
1581 if (ctl_x_map_enabled) {
1582 ctl_x_map_enabled = 0;
1583 for (i = 0; ctl_x_map[i].key_code; i++)
1584 if (parm == ctl_x_map[i].key_code) {
1585 (*ctl_x_map[i].fn) ();
1586 return MSG_HANDLED;
1588 } else {
1589 for (i = 0; default_map[i].key_code; i++) {
1590 if (parm == default_map[i].key_code) {
1591 (*default_map[i].fn) ();
1592 return MSG_HANDLED;
1596 return MSG_NOT_HANDLED;
1598 case DLG_DRAW:
1599 /* We handle the special case of the output lines */
1600 if (console_flag && output_lines)
1601 show_console_contents (output_start_y,
1602 LINES - output_lines - keybar_visible -
1603 1, LINES - keybar_visible - 1);
1604 return MSG_HANDLED;
1606 case DLG_POST_KEY:
1607 if (!the_menubar->active)
1608 update_dirty_panels ();
1609 return MSG_HANDLED;
1611 default:
1612 return default_dlg_callback (h, msg, parm);
1616 /* Show current directory in the xterm title */
1617 void
1618 update_xterm_title_path (void)
1620 const char *p;
1622 if (xterm_flag && xterm_title) {
1623 p = strip_home_and_password (current_panel->cwd);
1624 fprintf (stdout, "\33]0;mc - %s\7", str_term_form (p));
1625 if (!alternate_plus_minus)
1626 numeric_keypad_mode ();
1627 fflush (stdout);
1632 * Load new hint and display it.
1633 * IF force is not 0, ignore the timeout.
1635 void
1636 load_hint (int force)
1638 char *hint;
1640 if (!the_hint->widget.parent)
1641 return;
1643 if (!message_visible) {
1644 label_set_text (the_hint, 0);
1645 return;
1648 if ((hint = get_random_hint (force))) {
1649 if (*hint)
1650 set_hintbar (hint);
1651 g_free (hint);
1652 } else {
1653 char text[BUF_SMALL];
1655 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1656 VERSION);
1657 set_hintbar (text);
1661 static void
1662 setup_panels_and_run_mc (void)
1664 add_widget (midnight_dlg, the_menubar);
1665 add_widget (midnight_dlg, get_panel_widget (0));
1666 add_widget (midnight_dlg, get_panel_widget (1));
1667 add_widget (midnight_dlg, the_hint);
1668 load_hint (1);
1669 add_widget (midnight_dlg, cmdline);
1670 add_widget (midnight_dlg, the_prompt);
1671 add_widget (midnight_dlg, the_bar);
1672 init_labels ();
1674 if (boot_current_is_left)
1675 dlg_select_widget (get_panel_widget (0));
1676 else
1677 dlg_select_widget (get_panel_widget (1));
1679 /* Run the Midnight Commander if no file was specified in the command line */
1680 run_dlg (midnight_dlg);
1683 /* result must be free'd (I think this should go in util.c) */
1684 static char *
1685 prepend_cwd_on_local (const char *filename)
1687 char *d;
1688 int l;
1690 if (vfs_file_is_local (filename)) {
1691 if (*filename == PATH_SEP) /* an absolute pathname */
1692 return g_strdup (filename);
1693 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1694 mc_get_current_wd (d, MC_MAXPATHLEN);
1695 l = strlen (d);
1696 d[l++] = PATH_SEP;
1697 strcpy (d + l, filename);
1698 canonicalize_pathname (d);
1699 return d;
1700 } else
1701 return g_strdup (filename);
1704 static int
1705 mc_maybe_editor_or_viewer (void)
1707 if (!(view_one_file || edit_one_file))
1708 return 0;
1710 setup_dummy_mc ();
1712 /* Invoke the internal view/edit routine with:
1713 * the default processing and forcing the internal viewer/editor
1715 if (view_one_file) {
1716 char *path = NULL;
1717 path = prepend_cwd_on_local (view_one_file);
1718 view_file (path, 0, 1);
1719 g_free (path);
1721 #ifdef USE_INTERNAL_EDIT
1722 else {
1723 edit_file (edit_one_file, edit_one_file_start_line);
1725 #endif /* USE_INTERNAL_EDIT */
1726 midnight_shutdown = 1;
1727 done_mc ();
1728 return 1;
1731 /* Run the main dialog that occupies the whole screen */
1732 static void
1733 do_nc (void)
1735 int midnight_colors[4];
1737 midnight_colors[0] = NORMAL_COLOR; /* NORMALC */
1738 midnight_colors[1] = REVERSE_COLOR; /* FOCUSC */
1739 midnight_colors[2] = INPUT_COLOR; /* HOT_NORMALC */
1740 midnight_colors[3] = NORMAL_COLOR; /* HOT_FOCUSC */
1742 midnight_dlg =
1743 create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1744 "[main]", NULL, DLG_WANT_IDLE);
1746 /* Check if we were invoked as an editor or file viewer */
1747 if (mc_maybe_editor_or_viewer ())
1748 return;
1750 setup_mc ();
1752 setup_panels_and_run_mc ();
1754 /* Program end */
1755 midnight_shutdown = 1;
1757 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1758 if (last_wd_file && vfs_current_is_local ()) {
1759 last_wd_string = g_strdup (current_panel->cwd);
1761 done_mc ();
1763 destroy_dlg (midnight_dlg);
1764 current_panel = 0;
1765 done_mc_profile ();
1768 /* POSIX version. The only version we support. */
1769 static void
1770 OS_Setup (void)
1772 const char *mc_libdir;
1773 shell = getenv ("SHELL");
1774 if (!shell || !*shell) {
1775 struct passwd *pwd;
1776 pwd = getpwuid (geteuid ());
1777 if (pwd != NULL)
1778 shell = g_strdup (pwd->pw_shell);
1780 if (!shell || !*shell)
1781 shell = "/bin/sh";
1783 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1784 /* and can be overriden by the MC_DATADIR environment variable */
1785 if ((mc_libdir = getenv ("MC_DATADIR")) != NULL) {
1786 mc_home = g_strdup (mc_libdir);
1787 } else {
1788 mc_home = g_strdup (DATADIR);
1792 static void
1793 sigchld_handler_no_subshell (int sig)
1795 #ifdef __linux__
1796 int pid, status;
1798 if (!console_flag)
1799 return;
1801 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1802 the value of console_flag never changed, we could simply not install
1803 this handler at all if (!console_flag && !use_subshell). */
1805 /* That comment is no longer true. We need to wait() on a sigchld
1806 handler (that's at least what the tarfs code expects currently). */
1808 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1810 if (pid == cons_saver_pid) {
1812 if (WIFSTOPPED (status)) {
1813 /* Someone has stopped cons.saver - restart it */
1814 kill (pid, SIGCONT);
1815 } else {
1816 /* cons.saver has died - disable console saving */
1817 handle_console (CONSOLE_DONE);
1818 console_flag = 0;
1821 /* If we got here, some other child exited; ignore it */
1822 #endif /* __linux__ */
1824 (void) sig;
1827 static void
1828 init_sigchld (void)
1830 struct sigaction sigchld_action;
1832 sigchld_action.sa_handler =
1833 #ifdef HAVE_SUBSHELL_SUPPORT
1834 use_subshell ? sigchld_handler :
1835 #endif /* HAVE_SUBSHELL_SUPPORT */
1836 sigchld_handler_no_subshell;
1838 sigemptyset (&sigchld_action.sa_mask);
1840 #ifdef SA_RESTART
1841 sigchld_action.sa_flags = SA_RESTART;
1842 #else
1843 sigchld_action.sa_flags = 0;
1844 #endif /* !SA_RESTART */
1846 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1847 #ifdef HAVE_SUBSHELL_SUPPORT
1849 * This may happen on QNX Neutrino 6, where SA_RESTART
1850 * is defined but not implemented. Fallback to no subshell.
1852 use_subshell = 0;
1853 #endif /* HAVE_SUBSHELL_SUPPORT */
1857 static void
1858 print_mc_usage (poptContext ctx, FILE *stream)
1860 int leftColWidth;
1862 poptSetOtherOptionHelp (ctx,
1863 _("[flags] [this_dir] [other_panel_dir]\n"));
1865 /* print help for options */
1866 leftColWidth = poptPrintHelp (ctx, stream, 0);
1867 fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
1868 _("Set initial line number for the internal editor"));
1869 fputs (_
1870 ("\n"
1871 "Please send any bug reports (including the output of `mc -V')\n"
1872 "to mc-devel@gnome.org\n"), stream);
1873 show_version (0);
1876 static void
1877 print_color_usage (void)
1880 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
1881 * and editmarked. To preserve translations, lines should be split.
1883 /* TRANSLATORS: don't translate keywords and names of colors */
1884 fputs (_
1885 ("--colors KEYWORD={FORE},{BACK}\n\n"
1886 "{FORE} and {BACK} can be omitted, and the default will be used\n"
1887 "\n" "Keywords:\n"
1888 " Global: errors, reverse, gauge, input, viewunderline\n"
1889 " File display: normal, selected, marked, markselect\n"
1890 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
1891 " errdhotfocus\n"
1892 " Menus: menu, menuhot, menusel, menuhotsel\n"
1893 " Editor: editnormal, editbold, editmarked\n"
1894 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
1895 " File types: directory, executable, link, stalelink, device, special, core\n"
1896 "\n" "Colors:\n"
1897 " black, gray, red, brightred, green, brightgreen, brown,\n"
1898 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
1899 " brightcyan, lightgray and white\n\n"), stdout);
1902 static void
1903 process_args (poptContext ctx, int c, const char *option_arg)
1905 switch (c) {
1906 case 'V':
1907 show_version (1);
1908 exit (0);
1909 break;
1911 case 'c':
1912 disable_colors = 0;
1913 #ifdef HAVE_SLANG
1914 force_colors = 1;
1915 #endif /* HAVE_SLANG */
1916 break;
1918 case 'f':
1919 printf ("%s\n", mc_home);
1920 exit (0);
1921 break;
1923 #ifdef USE_NETCODE
1924 case 'l':
1925 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) option_arg);
1926 #ifdef WITH_SMBFS
1927 smbfs_set_debugf (option_arg);
1928 #endif /* WITH_SMBFS */
1929 break;
1931 #ifdef WITH_SMBFS
1932 case 'D':
1933 smbfs_set_debug (atoi (option_arg));
1934 break;
1935 #endif /* WITH_SMBFS */
1936 #endif /* USE_NETCODE */
1938 case 'd':
1939 use_mouse_p = MOUSE_DISABLED;
1940 break;
1942 #ifdef HAVE_SUBSHELL_SUPPORT
1943 case 'u':
1944 use_subshell = 0;
1945 break;
1946 #endif /* HAVE_SUBSHELL_SUPPORT */
1948 case 'H':
1949 print_color_usage ();
1950 exit (0);
1951 break;
1953 case 'h':
1954 print_mc_usage (ctx, stdout);
1955 exit (0);
1959 static const struct poptOption argument_table[] = {
1960 /* generic options */
1961 {"help", 'h', POPT_ARG_NONE, NULL, 'h',
1962 N_("Displays this help message"), NULL},
1963 {"version", 'V', POPT_ARG_NONE, NULL, 'V',
1964 N_("Displays the current version"), NULL},
1966 /* terminal options */
1967 {"xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
1968 N_("Forces xterm features"), NULL},
1969 {"nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
1970 N_("Disable mouse support in text version"), NULL},
1971 #if defined(HAVE_SLANG)
1972 {"termcap", 't', 0, &SLtt_Try_Termcap, 0,
1973 N_("Tries to use termcap instead of terminfo"), NULL},
1974 #endif
1975 {"resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
1976 N_("Resets soft keys on HP terminals"), NULL},
1977 {"slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
1978 N_("To run on slow terminals"), NULL},
1979 {"stickchars", 'a', 0, &force_ugly_line_drawing, 0,
1980 N_("Use stickchars to draw"), NULL},
1982 /* color options */
1983 {"nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
1984 N_("Requests to run in black and white"), NULL},
1985 {"color", 'c', POPT_ARG_NONE, NULL, 'c',
1986 N_("Request to run in color mode"), NULL},
1987 {"colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
1988 N_("Specifies a color configuration"), NULL},
1989 {"help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
1990 N_("Displays a help screen on how to change the color scheme"), NULL},
1992 /* debug options */
1993 #ifdef USE_NETCODE
1994 {"ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
1995 N_("Log ftp dialog to specified file"), NULL},
1996 #ifdef WITH_SMBFS
1997 {"debuglevel", 'D', POPT_ARG_STRING, NULL, 'D',
1998 N_("Set debug level"), NULL},
1999 #endif
2000 #endif
2002 /* options for wrappers */
2003 {"datadir", 'f', POPT_ARG_NONE, NULL, 'f',
2004 N_("Print data directory"), NULL},
2005 {"printwd", 'P', POPT_ARG_STRING, &last_wd_file, 0,
2006 N_("Print last working directory to specified file"), NULL},
2008 /* subshell options */
2009 #ifdef HAVE_SUBSHELL_SUPPORT
2010 {"subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2011 N_("Enables subshell support (default)"), NULL},
2012 {"nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2013 N_("Disables subshell support"), NULL},
2014 #endif
2016 /* single file operations */
2017 {"view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2018 N_("Launches the file viewer on a file"), NULL},
2019 #ifdef USE_INTERNAL_EDIT
2020 {"edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2021 N_("Edits one file"), NULL},
2022 #endif
2024 {NULL, '\0', 0, NULL, 0, NULL, NULL}
2027 static void
2028 handle_args (int argc, char *argv[])
2030 char *tmp;
2031 poptContext ctx;
2032 const char *base;
2033 int c;
2035 ctx =
2036 poptGetContext ("mc", argc, argv, argument_table,
2037 POPT_CONTEXT_NO_EXEC);
2039 while ((c = poptGetNextOpt (ctx)) > 0) {
2040 process_args (ctx, c, poptGetOptArg (ctx));
2043 if (c < -1) {
2044 print_mc_usage (ctx, stderr);
2045 fprintf (stderr, "%s: %s\n",
2046 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2047 poptStrerror (c));
2048 exit (1);
2051 tmp = poptGetArg (ctx);
2054 * Check for special invocation names mcedit and mcview,
2055 * if none apply then set the current directory and the other
2056 * directory from the command line arguments
2058 base = x_basename (argv[0]);
2059 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2060 edit_one_file = "";
2061 if (tmp) {
2063 * Check for filename:lineno, followed by an optional colon.
2064 * This format is used by many programs (especially compilers)
2065 * in error messages and warnings. It is supported so that
2066 * users can quickly copy and paste file locations.
2068 char *end = tmp + strlen (tmp), *p = end;
2069 if (p > tmp && p[-1] == ':')
2070 p--;
2071 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2072 p--;
2073 if (tmp < p && p < end && p[-1] == ':') {
2074 struct stat st;
2075 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2077 * Check that the file before the colon actually exists.
2078 * If it doesn't exist, revert to the old behavior.
2080 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2081 edit_one_file = fname;
2082 edit_one_file_start_line = atoi (p);
2083 } else {
2084 g_free (fname);
2085 goto try_plus_filename;
2087 } else {
2088 try_plus_filename:
2089 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1])) {
2090 int start_line = atoi (tmp);
2091 if (start_line > 0) {
2092 char *file = poptGetArg (ctx);
2093 if (file) {
2094 tmp = file;
2095 edit_one_file_start_line = start_line;
2099 edit_one_file = g_strdup (tmp);
2102 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2103 if (tmp)
2104 view_one_file = g_strdup (tmp);
2105 else {
2106 fputs ("No arguments given to the viewer\n", stderr);
2107 exit (1);
2109 } else {
2110 /* sets the current dir and the other dir */
2111 if (tmp) {
2112 this_dir = g_strdup (tmp);
2113 if ((tmp = poptGetArg (ctx)))
2114 other_dir = g_strdup (tmp);
2118 poptFreeContext (ctx);
2122 main (int argc, char *argv[])
2124 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2125 setlocale (LC_ALL, "");
2126 bindtextdomain ("mc", LOCALEDIR);
2127 textdomain ("mc");
2129 /* Set up temporary directory */
2130 mc_tmpdir ();
2132 OS_Setup ();
2134 /* This variable is used by the subshell */
2135 home_dir = getenv ("HOME");
2136 if (!home_dir) {
2137 /* mc_home was computed by OS_Setup */
2138 home_dir = mc_home;
2141 str_init_strings (NULL);
2143 vfs_init ();
2145 #ifdef USE_INTERNAL_EDIT
2146 for ( int i = 0; i < MAX_HISTORY_MOVETO; i++ ) {
2147 edit_history_moveto[i].filename = NULL;
2148 edit_history_moveto[i].line = -1;
2150 #endif
2152 #ifdef HAVE_SLANG
2153 SLtt_Ignore_Beep = 1;
2154 #endif
2156 handle_args (argc, argv);
2158 /* NOTE: This has to be called before slang_init or whatever routine
2159 calls any define_sequence */
2160 init_key ();
2162 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2163 handle_console (CONSOLE_INIT);
2165 #ifdef HAVE_SUBSHELL_SUPPORT
2166 /* Don't use subshell when invoked as viewer or editor */
2167 if (edit_one_file || view_one_file)
2168 use_subshell = 0;
2170 if (use_subshell)
2171 subshell_get_console_attributes ();
2172 #endif /* HAVE_SUBSHELL_SUPPORT */
2174 /* Install the SIGCHLD handler; must be done before init_subshell() */
2175 init_sigchld ();
2177 /* We need this, since ncurses endwin () doesn't restore the signals */
2178 save_stop_handler ();
2180 /* Must be done before init_subshell, to set up the terminal size: */
2181 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2182 #ifdef HAVE_SLANG
2183 slang_init ();
2184 #endif
2185 /* NOTE: This call has to be after slang_init. It's the small part from
2186 the previous init_key which had to be moved after the call of slang_init */
2187 init_key_input_fd ();
2189 load_setup ();
2191 init_curses ();
2193 init_xterm_support ();
2195 #ifdef HAVE_SUBSHELL_SUPPORT
2197 /* Done here to ensure that the subshell doesn't */
2198 /* inherit the file descriptors opened below, etc */
2199 if (use_subshell)
2200 init_subshell ();
2202 #endif /* HAVE_SUBSHELL_SUPPORT */
2204 /* Removing this from the X code let's us type C-c */
2205 load_key_defs ();
2207 /* Also done after init_subshell, to save any shell init file messages */
2208 if (console_flag)
2209 handle_console (CONSOLE_SAVE);
2211 if (alternate_plus_minus)
2212 application_keypad_mode ();
2214 #ifdef HAVE_SUBSHELL_SUPPORT
2215 if (use_subshell) {
2216 prompt = strip_ctrl_codes (subshell_prompt);
2217 if (!prompt)
2218 prompt = "";
2219 } else
2220 #endif /* HAVE_SUBSHELL_SUPPORT */
2221 prompt = (geteuid () == 0) ? "# " : "$ ";
2223 /* Program main loop */
2224 if (!midnight_shutdown)
2225 do_nc ();
2227 /* Save the tree store */
2228 tree_store_save ();
2230 /* Virtual File System shutdown */
2231 vfs_shut ();
2233 flush_extension_file (); /* does only free memory */
2235 endwin ();
2236 #ifdef HAVE_SLANG
2237 slang_shutdown ();
2238 #endif
2240 if (console_flag && !(quit & SUBSHELL_EXIT))
2241 handle_console (CONSOLE_RESTORE);
2242 if (alternate_plus_minus)
2243 numeric_keypad_mode ();
2245 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2247 if (console_flag)
2248 handle_console (CONSOLE_DONE);
2249 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2251 if (last_wd_file && last_wd_string && !print_last_revert
2252 && !edit_one_file && !view_one_file) {
2253 int last_wd_fd =
2254 open (last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2255 S_IRUSR | S_IWUSR);
2257 if (last_wd_fd != -1) {
2258 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2259 close (last_wd_fd);
2262 g_free (last_wd_string);
2264 g_free (mc_home);
2265 done_key ();
2266 #ifdef HAVE_CHARSET
2267 free_codepages_list ();
2268 #endif
2269 str_uninit_strings ();
2271 g_free (this_dir);
2272 g_free (other_dir);
2274 #ifdef USE_INTERNAL_EDIT
2275 for ( int i = 0; i < MAX_HISTORY_MOVETO; i++ ) {
2276 g_free(edit_history_moveto[i].filename);
2278 #endif
2280 return 0;