Merge commit '315633ecce31cd5af92112ab9903f6e7e8ae8df7'
[free-mc.git] / src / main.c
blob949dcf9653fcfb694a522484d5afecc0af7194d6
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>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
37 #include "global.h"
38 #include "tty.h"
39 #include "dir.h"
40 #include "color.h"
41 #include "dialog.h"
42 #include "menu.h"
43 #include "panel.h"
44 #include "main.h"
45 #include "win.h"
46 #include "mouse.h"
47 #include "option.h"
48 #include "tree.h"
49 #include "treestore.h"
50 #include "cons.saver.h"
51 #include "subshell.h"
52 #include "key.h" /* For init_key() and mi_getch() */
53 #include "setup.h" /* save_setup() */
54 #include "profile.h" /* free_profiles() */
55 #include "boxes.h" /* sort_box() */
56 #include "layout.h"
57 #include "cmd.h" /* Normal commands */
58 #include "hotlist.h"
59 #include "panelize.h"
60 #include "learn.h" /* learn_keys() */
61 #include "listmode.h"
62 #include "execute.h"
63 #include "ext.h" /* For flush_extension_file() */
65 /* Listbox for the command history feature */
66 #include "widget.h"
67 #include "command.h"
68 #include "wtools.h"
69 #include "complete.h" /* For the free_completion */
71 #include "chmod.h"
72 #include "chown.h"
73 #include "achown.h"
75 #ifdef WITH_SMBFS
76 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
77 #endif
79 #ifdef USE_INTERNAL_EDIT
80 # include "../edit/edit.h"
81 #endif
83 #ifdef HAVE_CHARSET
84 #include "charsets.h"
85 #endif /* HAVE_CHARSET */
87 #ifdef USE_VFS
88 #include "../vfs/gc.h"
89 #endif
91 #include "popt.h"
93 /* When the modes are active, left_panel, right_panel and tree_panel */
94 /* Point to a proper data structure. You should check with the functions */
95 /* get_current_type and get_other_type the types of the panels before using */
96 /* This pointer variables */
98 /* The structures for the panels */
99 WPanel *left_panel = NULL;
100 WPanel *right_panel = NULL;
102 /* The pointer to the tree */
103 WTree *the_tree = NULL;
105 /* The Menubar */
106 struct WMenu *the_menubar = NULL;
108 /* Pointers to the selected and unselected panel */
109 WPanel *current_panel = NULL;
111 /* Set if the command is being run from the "Right" menu */
112 int is_right = 0;
114 /* Set when main loop should be terminated */
115 volatile int quit = 0;
117 /* Set if you want the possible completions dialog for the first time */
118 int show_all_if_ambiguous = 0;
120 /* Set when cd symlink following is desirable (bash mode) */
121 int cd_symlinks = 1;
123 /* If set then dialogs just clean the screen when refreshing, else */
124 /* they do a complete refresh, refreshing all the parts of the program */
125 int fast_refresh = 0;
127 /* If true, marking a files moves the cursor down */
128 int mark_moves_down = 1;
130 /* If true, at startup the user-menu is invoked */
131 int auto_menu = 0;
133 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
134 and M-- and keypad + / - */
135 int alternate_plus_minus = 0;
137 /* If true, then the +, - and \ keys have their special meaning only if the
138 * command line is emtpy, otherwise they behave like regular letters
140 int only_leading_plus_minus = 1;
142 int pause_after_run = pause_on_dumb_terminals;
144 /* It true saves the setup when quitting */
145 int auto_save_setup = 1;
147 #ifndef HAVE_CHARSET
148 /* If true, allow characters in the range 160-255 */
149 int eight_bit_clean = 1;
152 * If true, also allow characters in the range 128-159.
153 * This is reported to break on many terminals (xterm, qansi-m).
155 int full_eight_bits = 0;
156 #endif /* !HAVE_CHARSET */
158 /* If true use the internal viewer */
159 int use_internal_view = 1;
161 /* Have we shown the fast-reload warning in the past? */
162 int fast_reload_w = 0;
164 /* Move page/item? When clicking on the top or bottom of a panel */
165 int mouse_move_pages = 1;
167 /* If true: l&r arrows are used to chdir if the input line is empty */
168 int navigate_with_arrows = 0;
170 /* If true use +, -, | for line drawing */
171 int force_ugly_line_drawing = 0;
173 /* If true program softkeys (HP terminals only) on startup and after every
174 command ran in the subshell to the description found in the termcap/terminfo
175 database */
176 int reset_hp_softkeys = 0;
178 /* The prompt */
179 const char *prompt = NULL;
181 /* The widget where we draw the prompt */
182 WLabel *the_prompt;
184 /* The hint bar */
185 WLabel *the_hint;
187 /* The button bar */
188 WButtonBar *the_bar;
190 /* For slow terminals */
191 int slow_terminal = 0;
193 /* Mouse type: GPM, xterm or none */
194 Mouse_Type use_mouse_p = MOUSE_NONE;
196 /* If true, assume we are running on an xterm terminal */
197 static int force_xterm = 0;
199 /* If on, default for "No" in delete operations */
200 int safe_delete = 0;
202 /* Controls screen clearing before an exec */
203 int clear_before_exec = 1;
205 /* Asks for confirmation before deleting a file */
206 int confirm_delete = 1;
208 /* Asks for confirmation before deleting a hotlist entry */
209 int confirm_directory_hotlist_delete = 1;
211 /* Asks for confirmation before overwriting a file */
212 int confirm_overwrite = 1;
214 /* Asks for confirmation before executing a program by pressing enter */
215 int confirm_execute = 0;
217 /* Asks for confirmation before leaving the program */
218 int confirm_exit = 1;
220 /* Asks for confirmation when using F3 to view a directory and there
221 are tagged files */
222 int confirm_view_dir = 0;
224 /* This flag indicates if the pull down menus by default drop down */
225 int drop_menus = 0;
227 /* The dialog handle for the main program */
228 Dlg_head *midnight_dlg;
230 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
231 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
232 int update_prompt = 0;
234 /* The home directory */
235 const char *home_dir = NULL;
237 /* The value of the other directory, only used when loading the setup */
238 char *other_dir = NULL;
240 /* Only used at program boot */
241 int boot_current_is_left = 1;
243 static char *this_dir = NULL;
245 /* If this is true, then when browsing the tree the other window will
246 * automatically reload it's directory with the contents of the currently
247 * selected directory.
249 int xtree_mode = 0;
251 /* If set, then print to the given file the last directory we were at */
252 static char *last_wd_file = NULL;
253 static char *last_wd_string = NULL;
254 /* Set to 1 to suppress printing the last directory */
255 static int print_last_revert = 0;
257 /* Force colors, only used by Slang */
258 int force_colors = 0;
260 /* colors specified on the command line: they override any other setting */
261 char *command_line_colors = NULL;
263 /* File name to view if argument was supplied */
264 static const char *view_one_file = NULL;
266 /* File name to edit if argument was supplied */
267 const char *edit_one_file = NULL;
269 /* Line to start the editor on */
270 static int edit_one_file_start_line = 0;
272 /* Used so that widgets know if they are being destroyed or
273 shut down */
274 int midnight_shutdown = 0;
276 /* The user's shell */
277 const char *shell = NULL;
279 /* mc_home: The home of MC */
280 char *mc_home = NULL;
282 char cmd_buf[512];
284 static void
285 reload_panelized (WPanel *panel)
287 int i, j;
288 dir_list *list = &panel->dir;
290 if (panel != current_panel)
291 mc_chdir (panel->cwd);
293 for (i = 0, j = 0; i < panel->count; i++) {
294 if (list->list[i].f.marked) {
295 /* Unmark the file in advance. In case the following mc_lstat
296 * fails we are done, else we have to mark the file again
297 * (Note: do_file_mark depends on a valid "list->list [i].buf").
298 * IMO that's the best way to update the panel's summary status
299 * -- Norbert
301 do_file_mark (panel, i, 0);
303 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
304 g_free (list->list[i].fname);
305 continue;
307 if (list->list[i].f.marked)
308 do_file_mark (panel, i, 1);
309 if (j != i)
310 list->list[j] = list->list[i];
311 j++;
313 if (j == 0)
314 panel->count = set_zero_dir (list);
315 else
316 panel->count = j;
318 if (panel != current_panel)
319 mc_chdir (current_panel->cwd);
322 static void
323 update_one_panel_widget (WPanel *panel, int force_update,
324 const char *current_file)
326 int free_pointer;
327 char *my_current_file = NULL;
329 if (force_update & UP_RELOAD) {
330 panel->is_panelized = 0;
331 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
332 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
335 /* If current_file == -1 (an invalid pointer) then preserve selection */
336 if (current_file == UP_KEEPSEL) {
337 free_pointer = 1;
338 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
339 current_file = my_current_file;
340 } else
341 free_pointer = 0;
343 if (panel->is_panelized)
344 reload_panelized (panel);
345 else
346 panel_reload (panel);
348 try_to_select (panel, current_file);
349 panel->dirty = 1;
351 if (free_pointer)
352 g_free (my_current_file);
355 void
356 panel_clean_dir (WPanel *panel)
358 int count = panel->count;
360 panel->count = 0;
361 panel->top_file = 0;
362 panel->selected = 0;
363 panel->marked = 0;
364 panel->dirs_marked = 0;
365 panel->total = 0;
366 panel->searching = 0;
367 panel->is_panelized = 0;
368 panel->dirty = 1;
370 clean_dir (&panel->dir, count);
373 static void
374 update_one_panel (int which, int force_update, const char *current_file)
376 WPanel *panel;
378 if (get_display_type (which) != view_listing)
379 return;
381 panel = (WPanel *) get_panel_widget (which);
382 update_one_panel_widget (panel, force_update, current_file);
385 /* This routine reloads the directory in both panels. It tries to
386 * select current_file in current_panel and other_file in other_panel.
387 * If current_file == -1 then it automatically sets current_file and
388 * other_file to the currently selected files in the panels.
390 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
391 * will not reload the other panel.
393 void
394 update_panels (int force_update, const char *current_file)
396 int reload_other = !(force_update & UP_ONLY_CURRENT);
397 WPanel *panel;
399 update_one_panel (get_current_index (), force_update, current_file);
400 if (reload_other)
401 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
403 if (get_current_type () == view_listing)
404 panel = (WPanel *) get_panel_widget (get_current_index ());
405 else
406 panel = (WPanel *) get_panel_widget (get_other_index ());
408 mc_chdir (panel->cwd);
411 /* Save current stat of directories to avoid reloading the panels */
412 /* when no modifications have taken place */
413 void
414 save_cwds_stat (void)
416 if (fast_reload) {
417 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
418 if (get_other_type () == view_listing)
419 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
423 #ifdef HAVE_SUBSHELL_SUPPORT
424 void
425 do_possible_cd (const char *new_dir)
427 if (!do_cd (new_dir, cd_exact))
428 message (1, _("Warning"),
429 _(" The Commander can't change to the directory that \n"
430 " the subshell claims you are in. Perhaps you have \n"
431 " deleted your working directory, or given yourself \n"
432 " extra access permissions with the \"su\" command? "));
435 void
436 do_update_prompt (void)
438 if (update_prompt) {
439 printf ("%s", subshell_prompt);
440 fflush (stdout);
441 update_prompt = 0;
444 #endif /* HAVE_SUBSHELL_SUPPORT */
446 void
447 change_panel (void)
449 free_completions (cmdline);
450 dlg_one_down (midnight_dlg);
453 /* Stop MC main dialog and the current dialog if it exists.
454 * Needed to provide fast exit from MC viewer or editor on shell exit */
455 static void
456 stop_dialogs (void)
458 midnight_dlg->running = 0;
459 if (current_dlg) {
460 current_dlg->running = 0;
464 static int
465 quit_cmd_internal (int quiet)
467 int q = quit;
469 if (quiet || !confirm_exit) {
470 q = 1;
471 } else {
472 if (query_dialog
473 (_(" The Midnight Commander "),
474 _(" Do you really want to quit the Midnight Commander? "), 0,
475 2, _("&Yes"), _("&No")) == 0)
476 q = 1;
478 if (q) {
479 #ifdef HAVE_SUBSHELL_SUPPORT
480 if (!use_subshell)
481 stop_dialogs ();
482 else if ((q = exit_subshell ()))
483 #endif
484 stop_dialogs ();
486 if (q)
487 quit |= 1;
488 return quit;
491 static void
492 quit_cmd (void)
494 quit_cmd_internal (0);
497 void
498 quiet_quit_cmd (void)
500 print_last_revert = 1;
501 quit_cmd_internal (1);
505 * Touch window and refresh window functions
508 /* This routine untouches the first line on both panels in order */
509 /* to avoid the refreshing the menu bar */
511 void
512 repaint_screen (void)
514 do_refresh ();
515 mc_refresh ();
518 /* Wrapper for do_subshell_chdir, check for availability of subshell */
519 void
520 subshell_chdir (const char *directory)
522 #ifdef HAVE_SUBSHELL_SUPPORT
523 if (use_subshell) {
524 if (vfs_current_is_local ())
525 do_subshell_chdir (directory, 0, 1);
527 #endif /* HAVE_SUBSHELL_SUPPORT */
530 void
531 directory_history_add (struct WPanel *panel, const char *dir)
533 char *tmp;
535 tmp = g_strdup (dir);
536 strip_password (tmp, 1);
538 panel->dir_history = list_append_unique (panel->dir_history, tmp);
542 * If we moved to the parent directory move the selection pointer to
543 * the old directory name; If we leave VFS dir, remove FS specificator.
545 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
547 static const char *
548 get_parent_dir_name (const char *cwd, const char *lwd)
550 const char *p;
551 if (strlen (lwd) > strlen (cwd))
552 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
553 (strlen (cwd) == p - lwd || (p == lwd && cwd[0] == PATH_SEP &&
554 cwd[1] == '\0'))) {
555 return (p + 1);
557 return NULL;
561 * Changes the current directory of the panel.
562 * Don't record change in the directory history.
564 static int
565 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
567 const char *directory;
568 char *olddir;
569 char temp[MC_MAXPATHLEN];
570 char *translated_url;
572 if (cd_type == cd_parse_command) {
573 while (*new_dir == ' ')
574 new_dir++;
577 olddir = g_strdup (panel->cwd);
578 new_dir = translated_url = vfs_translate_url (new_dir);
580 /* Convert *new_path to a suitable pathname, handle ~user */
582 if (cd_type == cd_parse_command) {
583 if (!strcmp (new_dir, "-")) {
584 strcpy (temp, panel->lwd);
585 new_dir = temp;
588 directory = *new_dir ? new_dir : home_dir;
590 if (mc_chdir (directory) == -1) {
591 strcpy (panel->cwd, olddir);
592 g_free (olddir);
593 g_free (translated_url);
594 return 0;
596 g_free (translated_url);
598 /* Success: save previous directory, shutdown status of previous dir */
599 strcpy (panel->lwd, olddir);
600 free_completions (cmdline);
602 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
604 vfs_release_path (olddir);
606 subshell_chdir (panel->cwd);
608 /* Reload current panel */
609 panel_clean_dir (panel);
610 panel->count =
611 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
612 panel->reverse, panel->case_sensitive, panel->filter);
613 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
614 load_hint (0);
615 panel->dirty = 1;
616 update_xterm_title_path ();
618 g_free (olddir);
620 return 1;
624 * Changes the current directory of the panel.
625 * Record change in the directory history.
628 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
630 int r;
632 r = _do_panel_cd (panel, new_dir, cd_type);
633 if (r)
634 directory_history_add (panel, panel->cwd);
635 return r;
639 do_cd (const char *new_dir, enum cd_enum exact)
641 return (do_panel_cd (current_panel, new_dir, exact));
644 void
645 directory_history_next (WPanel *panel)
647 GList *nextdir;
649 nextdir = g_list_next (panel->dir_history);
651 if (!nextdir)
652 return;
654 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
655 panel->dir_history = nextdir;
658 void
659 directory_history_prev (WPanel *panel)
661 GList *prevdir;
663 prevdir = g_list_previous (panel->dir_history);
665 if (!prevdir)
666 return;
668 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
669 panel->dir_history = prevdir;
672 void
673 directory_history_list (WPanel *panel)
675 char *s;
677 if (!panel->dir_history)
678 return;
680 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
682 if (!s)
683 return;
685 if (_do_panel_cd (panel, s, cd_exact))
686 directory_history_add (panel, panel->cwd);
687 else
688 message (1, MSG_ERROR, _("Cannot change directory"));
689 g_free (s);
692 #ifdef HAVE_SUBSHELL_SUPPORT
694 load_prompt (int fd, void *unused)
696 (void) fd;
697 (void) unused;
699 if (!read_subshell_prompt ())
700 return 0;
702 /* Don't actually change the prompt if it's invisible */
703 if (current_dlg == midnight_dlg && command_prompt) {
704 char *tmp_prompt;
705 int prompt_len;
707 tmp_prompt = strip_ctrl_codes (subshell_prompt);
708 prompt_len = strlen (tmp_prompt);
710 /* Check for prompts too big */
711 if (COLS > 8 && prompt_len > COLS - 8) {
712 tmp_prompt[COLS - 8] = '\0';
713 prompt_len = COLS - 8;
715 prompt = tmp_prompt;
716 label_set_text (the_prompt, prompt);
717 winput_set_origin ((WInput *) cmdline, prompt_len,
718 COLS - prompt_len);
720 /* since the prompt has changed, and we are called from one of the
721 * get_event channels, the prompt updating does not take place
722 * automatically: force a cursor update and a screen refresh
724 update_cursor (midnight_dlg);
725 mc_refresh ();
727 update_prompt = 1;
728 return 0;
730 #endif /* HAVE_SUBSHELL_SUPPORT */
732 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
734 maybe_cd (int move_up_dir)
736 if (navigate_with_arrows) {
737 if (!cmdline->buffer[0]) {
738 if (move_up_dir) {
739 do_cd ("..", cd_exact);
740 return 1;
742 if (S_ISDIR (selection (current_panel)->st.st_mode)
743 || link_isdir (selection (current_panel))) {
744 do_cd (selection (current_panel)->fname, cd_exact);
745 return 1;
749 return 0;
752 static void
753 sort_cmd (void)
755 WPanel *p;
756 sortfn *sort_order;
758 if (!SELECTED_IS_PANEL)
759 return;
761 p = MENU_PANEL;
762 sort_order = sort_box (p->sort_type, &p->reverse, &p->case_sensitive);
764 panel_set_sort_order (p, sort_order);
768 static void
769 treebox_cmd (void)
771 char *sel_dir;
773 sel_dir = tree_box (selection (current_panel)->fname);
774 if (sel_dir) {
775 do_cd (sel_dir, cd_exact);
776 g_free (sel_dir);
780 #ifdef LISTMODE_EDITOR
781 static void
782 listmode_cmd (void)
784 char *newmode;
786 if (get_current_type () != view_listing)
787 return;
789 newmode = listmode_edit (current_panel->user_format);
790 if (!newmode)
791 return;
793 g_free (current_panel->user_format);
794 current_panel->list_type = list_user;
795 current_panel->user_format = newmode;
796 set_panel_formats (current_panel);
798 do_refresh ();
800 #endif /* LISTMODE_EDITOR */
802 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
803 static menu_entry LeftMenu[] = {
804 {' ', N_("&Listing mode..."), 'L', listing_cmd},
805 {' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd},
806 {' ', N_("&Info C-x i"), 'I', info_cmd},
807 {' ', N_("&Tree"), 'T', tree_cmd},
808 {' ', "", ' ', 0},
809 {' ', N_("&Sort order..."), 'S', sort_cmd},
810 {' ', "", ' ', 0},
811 {' ', N_("&Filter..."), 'F', filter_cmd},
812 #ifdef USE_NETCODE
813 {' ', "", ' ', 0},
814 #ifdef WITH_MCFS
815 {' ', N_("&Network link..."), 'N', netlink_cmd},
816 #endif
817 {' ', N_("FT&P link..."), 'P', ftplink_cmd},
818 {' ', N_("S&hell link..."), 'H', fishlink_cmd},
819 #ifdef WITH_SMBFS
820 {' ', N_("SM&B link..."), 'B', smblink_cmd},
821 #endif
822 #endif
823 {' ', "", ' ', 0},
824 {' ', N_("&Rescan C-r"), 'R', reread_cmd}
827 static menu_entry RightMenu[] = {
828 {' ', N_("&Listing mode..."), 'L', listing_cmd},
829 {' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd},
830 {' ', N_("&Info C-x i"), 'I', info_cmd},
831 {' ', N_("&Tree"), 'T', tree_cmd},
832 {' ', "", ' ', 0},
833 {' ', N_("&Sort order..."), 'S', sort_cmd},
834 {' ', "", ' ', 0},
835 {' ', N_("&Filter..."), 'F', filter_cmd},
836 #ifdef USE_NETCODE
837 {' ', "", ' ', 0},
838 #ifdef WITH_MCFS
839 {' ', N_("&Network link..."), 'N', netlink_cmd},
840 #endif
841 {' ', N_("FT&P link..."), 'P', ftplink_cmd},
842 {' ', N_("S&hell link..."), 'H', fishlink_cmd},
843 #ifdef WITH_SMBFS
844 {' ', N_("SM&B link..."), 'B', smblink_cmd},
845 #endif
846 #endif
847 {' ', "", ' ', 0},
848 {' ', N_("&Rescan C-r"), 'R', reread_cmd}
851 static menu_entry FileMenu[] = {
852 {' ', N_("&User menu F2"), 'U', user_file_menu_cmd},
853 {' ', N_("&View F3"), 'V', view_cmd},
854 {' ', N_("Vie&w file... "), 'W', view_file_cmd},
855 {' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd},
856 {' ', N_("&Edit F4"), 'E', edit_cmd},
857 {' ', N_("&Copy F5"), 'C', copy_cmd},
858 {' ', N_("c&Hmod C-x c"), 'H', chmod_cmd},
859 {' ', N_("&Link C-x l"), 'L', link_cmd},
860 {' ', N_("&SymLink C-x s"), 'S', symlink_cmd},
861 {' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd},
862 {' ', N_("ch&Own C-x o"), 'O', chown_cmd},
863 {' ', N_("&Advanced chown "), 'A', chown_advanced_cmd},
864 {' ', N_("&Rename/Move F6"), 'R', ren_cmd},
865 {' ', N_("&Mkdir F7"), 'M', mkdir_cmd},
866 {' ', N_("&Delete F8"), 'D', delete_cmd},
867 {' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd},
868 {' ', "", ' ', 0},
869 {' ', N_("select &Group M-+"), 'G', select_cmd},
870 {' ', N_("u&Nselect group M-\\"), 'N', unselect_cmd},
871 {' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd},
872 {' ', "", ' ', 0},
873 {' ', N_("e&Xit F10"), 'X', quit_cmd}
876 static menu_entry CmdMenu[] = {
877 /* I know, I'm lazy, but the tree widget when it's not running
878 * as a panel still has some problems, I have not yet finished
879 * the WTree widget port, sorry.
881 {' ', N_("&Directory tree"), 'D', treebox_cmd},
882 {' ', N_("&Find file M-?"), 'F', find_cmd},
883 {' ', N_("s&Wap panels C-u"), 'W', swap_cmd},
884 {' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd},
885 {' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd},
886 {' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize},
887 {' ', N_("show directory s&Izes"), 'I', dirsizes_cmd},
888 {' ', "", ' ', 0},
889 {' ', N_("command &History"), 'H', history_cmd},
890 {' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd},
891 #ifdef USE_VFS
892 {' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs},
893 #endif
894 #ifdef WITH_BACKGROUND
895 {' ', N_("&Background jobs C-x j"), 'B', jobs_cmd},
896 #endif
897 {' ', "", ' ', 0},
898 #ifdef USE_EXT2FSLIB
899 {' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd},
900 #endif
901 #ifdef LISTMODE_EDITOR
902 {' ', N_("&Listing format edit"), 'L', listmode_cmd},
903 #endif
904 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
905 {' ', "", ' ', 0},
906 #endif
907 {' ', N_("Edit &extension file"), 'E', ext_cmd},
908 {' ', N_("Edit &menu file"), 'M', edit_mc_menu_cmd},
909 #ifdef USE_INTERNAL_EDIT
910 {' ', N_("Edit edi&tor menu file"), 'T', edit_user_menu_cmd},
911 {' ', N_("Edit &syntax file"), 'S', edit_syntax_cmd}
912 #endif /* USE_INTERNAL_EDIT */
915 /* Must keep in sync with the constants in menu_cmd */
916 static menu_entry OptMenu[] = {
917 {' ', N_("&Configuration..."), 'C', configure_box},
918 {' ', N_("&Layout..."), 'L', layout_cmd},
919 {' ', N_("c&Onfirmation..."), 'O', confirm_box},
920 {' ', N_("&Display bits..."), 'D', display_bits_box},
921 {' ', N_("learn &Keys..."), 'K', learn_keys},
922 #ifdef USE_VFS
923 {' ', N_("&Virtual FS..."), 'V', configure_vfs},
924 #endif /* !USE_VFS */
925 {' ', "", ' ', 0},
926 {' ', N_("&Save setup"), 'S', save_setup_cmd}
929 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
931 static Menu *MenuBar[5];
933 void
934 init_menu (void)
936 MenuBar[0] =
937 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
938 LeftMenu, menu_entries (LeftMenu),
939 "[Left and Right Menus]");
940 MenuBar[1] =
941 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
942 "[File Menu]");
943 MenuBar[2] =
944 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
945 "[Command Menu]");
946 MenuBar[3] =
947 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
948 "[Options Menu]");
949 MenuBar[4] =
950 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
951 RightMenu, menu_entries (RightMenu),
952 "[Left and Right Menus]");
955 void
956 done_menu (void)
958 int i;
960 for (i = 0; i < 5; i++) {
961 destroy_menu (MenuBar[i]);
965 static void
966 menu_last_selected_cmd (void)
968 the_menubar->active = 1;
969 the_menubar->dropped = drop_menus;
970 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
971 dlg_select_widget (the_menubar);
974 static void
975 menu_cmd (void)
977 if (the_menubar->active)
978 return;
980 if ((get_current_index () == 0) ^ (!current_panel->active))
981 the_menubar->selected = 0;
982 else
983 the_menubar->selected = 4;
984 menu_last_selected_cmd ();
987 /* Flag toggling functions */
988 void
989 toggle_fast_reload (void)
991 fast_reload = !fast_reload;
992 if (fast_reload_w == 0 && fast_reload) {
993 message (0, _(" Information "),
995 (" Using the fast reload option may not reflect the exact \n"
996 " directory contents. In this case you'll need to do a \n"
997 " manual reload of the directory. See the man page for \n"
998 " the details. "));
999 fast_reload_w = 1;
1003 void
1004 toggle_mix_all_files (void)
1006 mix_all_files = !mix_all_files;
1007 update_panels (UP_RELOAD, UP_KEEPSEL);
1010 void
1011 toggle_show_backup (void)
1013 show_backups = !show_backups;
1014 update_panels (UP_RELOAD, UP_KEEPSEL);
1017 void
1018 toggle_show_hidden (void)
1020 show_dot_files = !show_dot_files;
1021 update_panels (UP_RELOAD, UP_KEEPSEL);
1025 * Just a hack for allowing url-like pathnames to be accepted from the
1026 * command line.
1028 static void
1029 translated_mc_chdir (char *dir)
1031 char *newdir;
1033 newdir = vfs_translate_url (dir);
1034 mc_chdir (newdir);
1035 g_free (newdir);
1038 static void
1039 create_panels (void)
1041 int current_index;
1042 int other_index;
1043 int current_mode;
1044 int other_mode;
1045 char original_dir[1024];
1047 original_dir[0] = 0;
1049 if (boot_current_is_left) {
1050 current_index = 0;
1051 other_index = 1;
1052 current_mode = startup_left_mode;
1053 other_mode = startup_right_mode;
1054 } else {
1055 current_index = 1;
1056 other_index = 0;
1057 current_mode = startup_right_mode;
1058 other_mode = startup_left_mode;
1060 /* Creates the left panel */
1061 if (this_dir) {
1062 if (other_dir) {
1063 /* Ok, user has specified two dirs, save the original one,
1064 * since we may not be able to chdir to the proper
1065 * second directory later
1067 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1069 translated_mc_chdir (this_dir);
1071 set_display_type (current_index, current_mode);
1073 /* The other panel */
1074 if (other_dir) {
1075 if (original_dir[0])
1076 translated_mc_chdir (original_dir);
1077 translated_mc_chdir (other_dir);
1079 set_display_type (other_index, other_mode);
1081 if (startup_left_mode == view_listing) {
1082 current_panel = left_panel;
1083 } else {
1084 if (right_panel)
1085 current_panel = right_panel;
1086 else
1087 current_panel = left_panel;
1090 /* Create the nice widgets */
1091 cmdline = command_new (0, 0, 0);
1092 the_prompt = label_new (0, 0, prompt);
1093 the_prompt->transparent = 1;
1094 the_bar = buttonbar_new (keybar_visible);
1096 the_hint = label_new (0, 0, 0);
1097 the_hint->transparent = 1;
1098 the_hint->auto_adjust_cols = 0;
1099 the_hint->widget.cols = COLS;
1101 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1104 static void
1105 copy_current_pathname (void)
1107 if (!command_prompt)
1108 return;
1110 command_insert (cmdline, current_panel->cwd, 0);
1111 if (current_panel->cwd[strlen (current_panel->cwd) - 1] != PATH_SEP)
1112 command_insert (cmdline, PATH_SEP_STR, 0);
1115 static void
1116 copy_other_pathname (void)
1118 if (get_other_type () != view_listing)
1119 return;
1121 if (!command_prompt)
1122 return;
1124 command_insert (cmdline, other_panel->cwd, 0);
1125 if (other_panel->cwd[strlen (other_panel->cwd) - 1] != PATH_SEP)
1126 command_insert (cmdline, PATH_SEP_STR, 0);
1129 static void
1130 copy_readlink (WPanel *panel)
1132 if (!command_prompt)
1133 return;
1134 if (S_ISLNK (selection (panel)->st.st_mode)) {
1135 char buffer[MC_MAXPATHLEN];
1136 char *p =
1137 concat_dir_and_file (panel->cwd, selection (panel)->fname);
1138 int i;
1140 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1141 g_free (p);
1142 if (i > 0) {
1143 buffer[i] = 0;
1144 command_insert (cmdline, buffer, 1);
1149 static void
1150 copy_current_readlink (void)
1152 copy_readlink (current_panel);
1155 static void
1156 copy_other_readlink (void)
1158 if (get_other_type () != view_listing)
1159 return;
1160 copy_readlink (other_panel);
1163 /* Insert the selected file name into the input line */
1164 static void
1165 copy_prog_name (void)
1167 char *tmp;
1168 if (!command_prompt)
1169 return;
1171 if (get_current_type () == view_tree) {
1172 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1173 tmp = tree_selected_name (tree);
1174 } else
1175 tmp = selection (current_panel)->fname;
1177 command_insert (cmdline, tmp, 1);
1180 static void
1181 copy_tagged (WPanel *panel)
1183 int i;
1185 if (!command_prompt)
1186 return;
1187 input_disable_update (cmdline);
1188 if (panel->marked) {
1189 for (i = 0; i < panel->count; i++) {
1190 if (panel->dir.list[i].f.marked)
1191 command_insert (cmdline, panel->dir.list[i].fname, 1);
1193 } else {
1194 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1197 input_enable_update (cmdline);
1200 static void
1201 copy_current_tagged (void)
1203 copy_tagged (current_panel);
1206 static void
1207 copy_other_tagged (void)
1209 if (get_other_type () != view_listing)
1210 return;
1211 copy_tagged (other_panel);
1214 static void
1215 init_labels (void)
1217 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1218 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1219 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1220 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1223 static const key_map ctl_x_map[] = {
1224 {XCTRL ('c'), quit_cmd},
1225 {'d', compare_dirs_cmd},
1226 #ifdef USE_VFS
1227 {'a', reselect_vfs},
1228 #endif /* USE_VFS */
1229 {'p', copy_current_pathname},
1230 {XCTRL ('p'), copy_other_pathname},
1231 {'t', copy_current_tagged},
1232 {XCTRL ('t'), copy_other_tagged},
1233 {'c', chmod_cmd},
1234 {'o', chown_cmd},
1235 {'r', copy_current_readlink},
1236 {XCTRL ('r'), copy_other_readlink},
1237 {'l', link_cmd},
1238 {'s', symlink_cmd},
1239 {XCTRL ('s'), edit_symlink_cmd},
1240 {'i', info_cmd_no_menu},
1241 {'q', quick_cmd_no_menu},
1242 {'h', add2hotlist_cmd},
1243 {'!', external_panelize},
1244 #ifdef WITH_BACKGROUND
1245 {'j', jobs_cmd},
1246 #endif /* WITH_BACKGROUND */
1247 {0, 0}
1250 static int ctl_x_map_enabled = 0;
1252 static void
1253 ctl_x_cmd (void)
1255 ctl_x_map_enabled = 1;
1258 static void
1259 nothing (void)
1263 static const key_map default_map[] = {
1264 {KEY_F (19), menu_last_selected_cmd},
1265 {KEY_F (20), quiet_quit_cmd},
1267 {XCTRL ('@'), single_dirsize_cmd},
1269 /* Copy useful information to the command line */
1270 {ALT ('a'), copy_current_pathname},
1271 {ALT ('A'), copy_other_pathname},
1273 {ALT ('c'), quick_cd_cmd},
1275 /* To access the directory hotlist */
1276 {XCTRL ('\\'), quick_chdir_cmd},
1278 /* Suspend */
1279 {XCTRL ('z'), suspend_cmd},
1281 /* The filtered view command */
1282 {ALT ('!'), filtered_view_cmd},
1284 /* Find file */
1285 {ALT ('?'), find_cmd},
1287 /* Panel refresh */
1288 {XCTRL ('r'), reread_cmd},
1290 /* Toggle listing between long, user defined and full formats */
1291 {ALT ('t'), toggle_listing_cmd},
1293 /* Swap panels */
1294 {XCTRL ('u'), swap_cmd},
1296 /* View output */
1297 {XCTRL ('o'), view_other_cmd},
1299 /* Control-X keybindings */
1300 {XCTRL ('x'), ctl_x_cmd},
1302 /* Trap dlg's exit commands */
1303 {ESC_CHAR, nothing},
1304 {XCTRL ('c'), nothing},
1305 {XCTRL ('g'), nothing},
1306 {0, 0},
1309 static void
1310 setup_sigwinch (void)
1312 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && defined(SIGWINCH)
1313 struct sigaction act, oact;
1314 act.sa_handler = flag_winch;
1315 sigemptyset (&act.sa_mask);
1316 act.sa_flags = 0;
1317 #ifdef SA_RESTART
1318 act.sa_flags |= SA_RESTART;
1319 #endif
1320 sigaction (SIGWINCH, &act, &oact);
1321 #endif
1324 static void
1325 setup_pre (void)
1327 /* Call all the inits */
1328 #ifdef HAVE_CHARSET
1330 * Don't restrict the output on the screen manager level,
1331 * the translation tables take care of it.
1333 #define full_eight_bits (1)
1334 #define eight_bit_clean (1)
1335 #endif /* !HAVE_CHARSET */
1337 #ifndef HAVE_SLANG
1338 meta (stdscr, eight_bit_clean);
1339 #else
1340 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1341 #endif
1344 static void
1345 init_xterm_support (void)
1347 const char *termvalue;
1349 termvalue = getenv ("TERM");
1350 if (!termvalue || !(*termvalue)) {
1351 fputs (_("The TERM environment variable is unset!\n"), stderr);
1352 exit (1);
1355 /* Check mouse capabilities */
1356 xmouse_seq = tty_tgetstr ("Km");
1358 if (strcmp (termvalue, "cygwin") == 0) {
1359 force_xterm = 1;
1360 use_mouse_p = MOUSE_DISABLED;
1363 if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
1364 || strncmp (termvalue, "rxvt", 4) == 0
1365 || strcmp (termvalue, "Eterm") == 0
1366 || strcmp (termvalue, "dtterm") == 0) {
1367 xterm_flag = 1;
1369 /* Default to the standard xterm sequence */
1370 if (!xmouse_seq) {
1371 xmouse_seq = ESC_STR "[M";
1374 /* Enable mouse unless explicitly disabled by --nomouse */
1375 if (use_mouse_p != MOUSE_DISABLED) {
1376 const char *color_term = getenv ("COLORTERM");
1377 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1378 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1379 strcmp (termvalue, "Eterm") == 0) {
1380 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1381 } else {
1382 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1388 static void
1389 setup_mc (void)
1391 setup_pre ();
1392 init_menu ();
1393 create_panels ();
1394 setup_panels ();
1396 #ifdef HAVE_SUBSHELL_SUPPORT
1397 if (use_subshell)
1398 add_select_channel (subshell_pty, load_prompt, 0);
1399 #endif /* !HAVE_SUBSHELL_SUPPORT */
1401 setup_sigwinch ();
1403 if (baudrate () < 9600 || slow_terminal) {
1404 verbose = 0;
1406 init_mouse ();
1409 static void
1410 setup_dummy_mc ()
1412 char d[MC_MAXPATHLEN];
1414 mc_get_current_wd (d, MC_MAXPATHLEN);
1415 setup_mc ();
1416 mc_chdir (d);
1419 static void
1420 done_mc (void)
1422 disable_mouse ();
1424 done_menu ();
1426 /* Setup shutdown
1428 * We sync the profiles since the hotlist may have changed, while
1429 * we only change the setup data if we have the auto save feature set
1431 if (auto_save_setup)
1432 save_setup (); /* does also call save_hotlist */
1433 else
1434 save_hotlist ();
1435 done_screen ();
1436 vfs_add_current_stamps ();
1439 /* This should be called after destroy_dlg since panel widgets
1440 * save their state on the profiles
1442 static void
1443 done_mc_profile (void)
1445 if (!auto_save_setup)
1446 profile_forget_profile (profile_name);
1447 sync_profiles ();
1448 done_setup ();
1449 free_profiles ();
1452 static cb_ret_t
1453 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1455 int i;
1457 switch (msg) {
1459 case DLG_IDLE:
1460 /* We only need the first idle event */
1461 set_idle_proc (h, 0);
1462 if (auto_menu) {
1463 user_file_menu_cmd ();
1465 return MSG_HANDLED;
1467 case DLG_KEY:
1468 if (ctl_x_map_enabled) {
1469 ctl_x_map_enabled = 0;
1470 for (i = 0; ctl_x_map[i].key_code; i++)
1471 if (parm == ctl_x_map[i].key_code) {
1472 (*ctl_x_map[i].fn) ();
1473 return MSG_HANDLED;
1477 /* FIXME: should handle all menu shortcuts before this point */
1478 if (the_menubar->active)
1479 return MSG_NOT_HANDLED;
1481 if (parm == KEY_F (10)) {
1482 quit_cmd ();
1483 return MSG_HANDLED;
1486 if (parm == '\t')
1487 free_completions (cmdline);
1489 if (parm == '\n') {
1490 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1491 cmdline->buffer[i] == '\t'); i++);
1492 if (cmdline->buffer[i]) {
1493 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1494 return MSG_HANDLED;
1496 stuff (cmdline, "", 0);
1497 cmdline->point = 0;
1500 /* Ctrl-Enter and Alt-Enter */
1501 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1502 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1503 copy_prog_name ();
1504 return MSG_HANDLED;
1507 /* Ctrl-Shift-Enter */
1508 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1509 copy_current_pathname ();
1510 copy_prog_name ();
1511 return MSG_HANDLED;
1514 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1515 && !quote && !current_panel->searching) {
1516 if (!only_leading_plus_minus) {
1517 /* Special treatement, since the input line will eat them */
1518 if (parm == '+') {
1519 select_cmd ();
1520 return MSG_HANDLED;
1523 if (parm == '\\' || parm == '-') {
1524 unselect_cmd ();
1525 return MSG_HANDLED;
1528 if (parm == '*') {
1529 reverse_selection_cmd ();
1530 return MSG_HANDLED;
1532 } else if (!command_prompt || !cmdline->buffer[0]) {
1533 /* Special treatement '+', '-', '\', '*' only when this is
1534 * first char on input line
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;
1553 return MSG_NOT_HANDLED;
1555 case DLG_HOTKEY_HANDLED:
1556 if ((get_current_type () == view_listing) && current_panel->searching) {
1557 current_panel->searching = 0;
1558 current_panel->dirty = 1;
1560 return MSG_HANDLED;
1562 case DLG_UNHANDLED_KEY:
1563 if (command_prompt) {
1564 int v;
1566 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1567 if (v)
1568 return v;
1570 if (ctl_x_map_enabled) {
1571 ctl_x_map_enabled = 0;
1572 for (i = 0; ctl_x_map[i].key_code; i++)
1573 if (parm == ctl_x_map[i].key_code) {
1574 (*ctl_x_map[i].fn) ();
1575 return MSG_HANDLED;
1577 } else {
1578 for (i = 0; default_map[i].key_code; i++) {
1579 if (parm == default_map[i].key_code) {
1580 (*default_map[i].fn) ();
1581 return MSG_HANDLED;
1585 return MSG_NOT_HANDLED;
1587 case DLG_DRAW:
1588 /* We handle the special case of the output lines */
1589 if (console_flag && output_lines)
1590 show_console_contents (output_start_y,
1591 LINES - output_lines - keybar_visible -
1592 1, LINES - keybar_visible - 1);
1593 return MSG_HANDLED;
1595 case DLG_POST_KEY:
1596 if (!the_menubar->active)
1597 update_dirty_panels ();
1598 return MSG_HANDLED;
1600 default:
1601 return default_dlg_callback (h, msg, parm);
1605 /* Show current directory in the xterm title */
1606 void
1607 update_xterm_title_path (void)
1609 char *p, *s;
1611 if (xterm_flag && xterm_title) {
1612 p = s = g_strdup (strip_home_and_password (current_panel->cwd));
1613 do {
1614 if (!is_printable ((unsigned char) *s))
1615 *s = '?';
1616 } while (*++s);
1617 fprintf (stdout, "\33]0;mc - %s\7", p);
1618 fflush (stdout);
1619 g_free (p);
1624 * Load new hint and display it.
1625 * IF force is not 0, ignore the timeout.
1627 void
1628 load_hint (int force)
1630 char *hint;
1632 if (!the_hint->widget.parent)
1633 return;
1635 if (!message_visible) {
1636 label_set_text (the_hint, 0);
1637 return;
1640 if ((hint = get_random_hint (force))) {
1641 if (*hint)
1642 set_hintbar (hint);
1643 g_free (hint);
1644 } else {
1645 char text[BUF_SMALL];
1647 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1648 VERSION);
1649 set_hintbar (text);
1653 static void
1654 setup_panels_and_run_mc (void)
1656 add_widget (midnight_dlg, the_menubar);
1657 add_widget (midnight_dlg, get_panel_widget (0));
1658 add_widget (midnight_dlg, get_panel_widget (1));
1659 add_widget (midnight_dlg, the_hint);
1660 load_hint (1);
1661 add_widget (midnight_dlg, cmdline);
1662 add_widget (midnight_dlg, the_prompt);
1663 add_widget (midnight_dlg, the_bar);
1664 init_labels ();
1666 if (boot_current_is_left)
1667 dlg_select_widget (get_panel_widget (0));
1668 else
1669 dlg_select_widget (get_panel_widget (1));
1671 /* Run the Midnight Commander if no file was specified in the command line */
1672 run_dlg (midnight_dlg);
1675 /* result must be free'd (I think this should go in util.c) */
1676 static char *
1677 prepend_cwd_on_local (const char *filename)
1679 char *d;
1680 int l;
1682 if (vfs_file_is_local (filename)) {
1683 if (*filename == PATH_SEP) /* an absolute pathname */
1684 return g_strdup (filename);
1685 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1686 mc_get_current_wd (d, MC_MAXPATHLEN);
1687 l = strlen (d);
1688 d[l++] = PATH_SEP;
1689 strcpy (d + l, filename);
1690 canonicalize_pathname (d);
1691 return d;
1692 } else
1693 return g_strdup (filename);
1696 static int
1697 mc_maybe_editor_or_viewer (void)
1699 if (!(view_one_file || edit_one_file))
1700 return 0;
1702 setup_dummy_mc ();
1704 /* Invoke the internal view/edit routine with:
1705 * the default processing and forcing the internal viewer/editor
1707 if (view_one_file) {
1708 char *path = NULL;
1709 path = prepend_cwd_on_local (view_one_file);
1710 view_file (path, 0, 1);
1711 g_free (path);
1713 #ifdef USE_INTERNAL_EDIT
1714 else {
1715 edit_file (edit_one_file, edit_one_file_start_line);
1717 #endif /* USE_INTERNAL_EDIT */
1718 midnight_shutdown = 1;
1719 done_mc ();
1720 return 1;
1723 /* Run the main dialog that occupies the whole screen */
1724 static void
1725 do_nc (void)
1727 int midnight_colors[4];
1729 midnight_colors[0] = NORMAL_COLOR; /* NORMALC */
1730 midnight_colors[1] = REVERSE_COLOR; /* FOCUSC */
1731 midnight_colors[2] = INPUT_COLOR; /* HOT_NORMALC */
1732 midnight_colors[3] = NORMAL_COLOR; /* HOT_FOCUSC */
1734 midnight_dlg =
1735 create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1736 "[main]", NULL, DLG_WANT_IDLE);
1738 /* Check if we were invoked as an editor or file viewer */
1739 if (mc_maybe_editor_or_viewer ())
1740 return;
1742 setup_mc ();
1744 setup_panels_and_run_mc ();
1746 /* Program end */
1747 midnight_shutdown = 1;
1749 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1750 if (last_wd_file && vfs_current_is_local ()) {
1751 last_wd_string = g_strdup (current_panel->cwd);
1753 done_mc ();
1755 destroy_dlg (midnight_dlg);
1756 current_panel = 0;
1757 done_mc_profile ();
1760 /* POSIX version. The only version we support. */
1761 static void
1762 OS_Setup (void)
1764 const char *mc_libdir;
1765 shell = getenv ("SHELL");
1766 if (!shell || !*shell) {
1767 struct passwd *pwd;
1768 pwd = getpwuid (geteuid ());
1769 if (pwd != NULL)
1770 shell = g_strdup (pwd->pw_shell);
1772 if (!shell || !*shell)
1773 shell = "/bin/sh";
1775 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1776 /* and can be overriden by the MC_DATADIR environment variable */
1777 if ((mc_libdir = getenv ("MC_DATADIR")) != NULL) {
1778 mc_home = g_strdup (mc_libdir);
1779 } else {
1780 mc_home = g_strdup (DATADIR);
1784 static void
1785 sigchld_handler_no_subshell (int sig)
1787 #ifdef __linux__
1788 int pid, status;
1790 if (!console_flag)
1791 return;
1793 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1794 the value of console_flag never changed, we could simply not install
1795 this handler at all if (!console_flag && !use_subshell). */
1797 /* That comment is no longer true. We need to wait() on a sigchld
1798 handler (that's at least what the tarfs code expects currently). */
1800 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1802 if (pid == cons_saver_pid) {
1804 if (WIFSTOPPED (status)) {
1805 /* Someone has stopped cons.saver - restart it */
1806 kill (pid, SIGCONT);
1807 } else {
1808 /* cons.saver has died - disable console saving */
1809 handle_console (CONSOLE_DONE);
1810 console_flag = 0;
1813 /* If we got here, some other child exited; ignore it */
1814 #endif /* __linux__ */
1816 (void) sig;
1819 static void
1820 init_sigchld (void)
1822 struct sigaction sigchld_action;
1824 sigchld_action.sa_handler =
1825 #ifdef HAVE_SUBSHELL_SUPPORT
1826 use_subshell ? sigchld_handler :
1827 #endif /* HAVE_SUBSHELL_SUPPORT */
1828 sigchld_handler_no_subshell;
1830 sigemptyset (&sigchld_action.sa_mask);
1832 #ifdef SA_RESTART
1833 sigchld_action.sa_flags = SA_RESTART;
1834 #else
1835 sigchld_action.sa_flags = 0;
1836 #endif /* !SA_RESTART */
1838 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1839 #ifdef HAVE_SUBSHELL_SUPPORT
1841 * This may happen on QNX Neutrino 6, where SA_RESTART
1842 * is defined but not implemented. Fallback to no subshell.
1844 use_subshell = 0;
1845 #endif /* HAVE_SUBSHELL_SUPPORT */
1849 static void
1850 print_mc_usage (poptContext ctx, FILE *stream)
1852 int leftColWidth;
1854 poptSetOtherOptionHelp (ctx,
1855 _("[flags] [this_dir] [other_panel_dir]\n"));
1857 /* print help for options */
1858 leftColWidth = poptPrintHelp (ctx, stream, 0);
1859 fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
1860 _("Set initial line number for the internal editor"));
1861 fputs (_
1862 ("\n"
1863 "Please send any bug reports (including the output of `mc -V')\n"
1864 "to mc-devel@gnome.org\n"), stream);
1865 show_version (0);
1868 static void
1869 print_color_usage (void)
1872 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
1873 * and editmarked. To preserve translations, lines should be split.
1875 /* TRANSLATORS: don't translate keywords and names of colors */
1876 fputs (_
1877 ("--colors KEYWORD={FORE},{BACK}\n\n"
1878 "{FORE} and {BACK} can be omitted, and the default will be used\n"
1879 "\n" "Keywords:\n"
1880 " Global: errors, reverse, gauge, input, viewunderline\n"
1881 " File display: normal, selected, marked, markselect\n"
1882 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
1883 " errdhotfocus\n"
1884 " Menus: menu, menuhot, menusel, menuhotsel\n"
1885 " Editor: editnormal, editbold, editmarked\n"
1886 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
1887 " File types: directory, executable, link, stalelink, device, special, core\n"
1888 "\n" "Colors:\n"
1889 " black, gray, red, brightred, green, brightgreen, brown,\n"
1890 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
1891 " brightcyan, lightgray and white\n\n"), stdout);
1894 static void
1895 process_args (poptContext ctx, int c, const char *option_arg)
1897 switch (c) {
1898 case 'V':
1899 show_version (1);
1900 exit (0);
1901 break;
1903 case 'c':
1904 disable_colors = 0;
1905 #ifdef HAVE_SLANG
1906 force_colors = 1;
1907 #endif /* HAVE_SLANG */
1908 break;
1910 case 'f':
1911 printf ("%s\n", mc_home);
1912 exit (0);
1913 break;
1915 #ifdef USE_NETCODE
1916 case 'l':
1917 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) option_arg);
1918 #ifdef WITH_SMBFS
1919 smbfs_set_debugf (option_arg);
1920 #endif /* WITH_SMBFS */
1921 break;
1923 #ifdef WITH_SMBFS
1924 case 'D':
1925 smbfs_set_debug (atoi (option_arg));
1926 break;
1927 #endif /* WITH_SMBFS */
1928 #endif /* USE_NETCODE */
1930 case 'd':
1931 use_mouse_p = MOUSE_DISABLED;
1932 break;
1934 #ifdef HAVE_SUBSHELL_SUPPORT
1935 case 'u':
1936 use_subshell = 0;
1937 break;
1938 #endif /* HAVE_SUBSHELL_SUPPORT */
1940 case 'H':
1941 print_color_usage ();
1942 exit (0);
1943 break;
1945 case 'h':
1946 print_mc_usage (ctx, stdout);
1947 exit (0);
1951 static const struct poptOption argument_table[] = {
1952 /* generic options */
1953 {"help", 'h', POPT_ARG_NONE, NULL, 'h',
1954 N_("Displays this help message"), NULL},
1955 {"version", 'V', POPT_ARG_NONE, NULL, 'V',
1956 N_("Displays the current version"), NULL},
1958 /* terminal options */
1959 {"xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
1960 N_("Forces xterm features"), NULL},
1961 {"nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
1962 N_("Disable mouse support in text version"), NULL},
1963 #if defined(HAVE_SLANG)
1964 {"termcap", 't', 0, &SLtt_Try_Termcap, 0,
1965 N_("Tries to use termcap instead of terminfo"), NULL},
1966 #endif
1967 {"resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
1968 N_("Resets soft keys on HP terminals"), NULL},
1969 {"slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
1970 N_("To run on slow terminals"), NULL},
1971 {"stickchars", 'a', 0, &force_ugly_line_drawing, 0,
1972 N_("Use stickchars to draw"), NULL},
1974 /* color options */
1975 {"nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
1976 N_("Requests to run in black and white"), NULL},
1977 {"color", 'c', POPT_ARG_NONE, NULL, 'c',
1978 N_("Request to run in color mode"), NULL},
1979 {"colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
1980 N_("Specifies a color configuration"), NULL},
1981 {"help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
1982 N_("Displays a help screen on how to change the color scheme"), NULL},
1984 /* debug options */
1985 #ifdef USE_NETCODE
1986 {"ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
1987 N_("Log ftp dialog to specified file"), NULL},
1988 #ifdef WITH_SMBFS
1989 {"debuglevel", 'D', POPT_ARG_STRING, NULL, 'D',
1990 N_("Set debug level"), NULL},
1991 #endif
1992 #endif
1994 /* options for wrappers */
1995 {"datadir", 'f', POPT_ARG_NONE, NULL, 'f',
1996 N_("Print data directory"), NULL},
1997 {"printwd", 'P', POPT_ARG_STRING, &last_wd_file, 0,
1998 N_("Print last working directory to specified file"), NULL},
2000 /* subshell options */
2001 #ifdef HAVE_SUBSHELL_SUPPORT
2002 {"subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2003 N_("Enables subshell support (default)"), NULL},
2004 {"nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2005 N_("Disables subshell support"), NULL},
2006 #endif
2008 /* single file operations */
2009 {"view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2010 N_("Launches the file viewer on a file"), NULL},
2011 #ifdef USE_INTERNAL_EDIT
2012 {"edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2013 N_("Edits one file"), NULL},
2014 #endif
2016 {NULL, '\0', 0, NULL, 0, NULL, NULL}
2019 static void
2020 handle_args (int argc, char *argv[])
2022 char *tmp;
2023 poptContext ctx;
2024 const char *base;
2025 int c;
2027 ctx =
2028 poptGetContext ("mc", argc, argv, argument_table,
2029 POPT_CONTEXT_NO_EXEC);
2031 #ifdef USE_TERMCAP
2032 SLtt_Try_Termcap = 1;
2033 #endif
2035 while ((c = poptGetNextOpt (ctx)) > 0) {
2036 process_args (ctx, c, poptGetOptArg (ctx));
2039 if (c < -1) {
2040 print_mc_usage (ctx, stderr);
2041 fprintf (stderr, "%s: %s\n",
2042 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2043 poptStrerror (c));
2044 exit (1);
2047 tmp = poptGetArg (ctx);
2050 * Check for special invocation names mcedit and mcview,
2051 * if none apply then set the current directory and the other
2052 * directory from the command line arguments
2054 base = x_basename (argv[0]);
2055 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2056 edit_one_file = "";
2057 if (tmp) {
2059 * Check for filename:lineno, followed by an optional colon.
2060 * This format is used by many programs (especially compilers)
2061 * in error messages and warnings. It is supported so that
2062 * users can quickly copy and paste file locations.
2064 char *end = tmp + strlen (tmp), *p = end;
2065 if (p > tmp && p[-1] == ':')
2066 p--;
2067 while (p > tmp && isdigit ((unsigned char) p[-1]))
2068 p--;
2069 if (tmp < p && p < end && p[-1] == ':') {
2070 struct stat st;
2071 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2073 * Check that the file before the colon actually exists.
2074 * If it doesn't exist, revert to the old behavior.
2076 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2077 edit_one_file = fname;
2078 edit_one_file_start_line = atoi (p);
2079 } else {
2080 g_free (fname);
2081 goto try_plus_filename;
2083 } else {
2084 try_plus_filename:
2085 if (*tmp == '+' && isdigit ((unsigned char) tmp[1])) {
2086 int start_line = atoi (tmp);
2087 if (start_line > 0) {
2088 char *file = poptGetArg (ctx);
2089 if (file) {
2090 tmp = file;
2091 edit_one_file_start_line = start_line;
2095 edit_one_file = g_strdup (tmp);
2098 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2099 if (tmp)
2100 view_one_file = g_strdup (tmp);
2101 else {
2102 fputs ("No arguments given to the viewer\n", stderr);
2103 exit (1);
2105 } else {
2106 /* sets the current dir and the other dir */
2107 if (tmp) {
2108 this_dir = g_strdup (tmp);
2109 if ((tmp = poptGetArg (ctx)))
2110 other_dir = g_strdup (tmp);
2114 poptFreeContext (ctx);
2117 static int
2118 do_mc_filename_rename (const char *o_name, const char *n_name)
2120 char *full_o_name = concat_dir_and_file (home_dir, o_name);
2121 char *full_n_name = g_strconcat (home_dir, MC_BASE, n_name, (char *) NULL);
2122 int move;
2124 move = 0 == rename (full_o_name, full_n_name);
2125 g_free (full_o_name);
2126 g_free (full_n_name);
2127 return move;
2131 main (int argc, char *argv[])
2133 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2134 setlocale (LC_ALL, "");
2135 bindtextdomain ("mc", LOCALEDIR);
2136 textdomain ("mc");
2138 /* Set up temporary directory */
2139 mc_tmpdir ();
2141 OS_Setup ();
2143 /* This variable is used by the subshell */
2144 home_dir = getenv ("HOME");
2145 if (!home_dir) {
2146 /* mc_home was computed by OS_Setup */
2147 home_dir = mc_home;
2150 vfs_init ();
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 g_free (this_dir);
2270 g_free (other_dir);
2272 return 0;