Forgot to remove some more .s strings and do a rename in order to prevent compiler...
[midnight-commander.git] / src / main.c
blob367663a72d8469140b00af09a218a030f2091971
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 <mhl/memory.h>
37 #include <mhl/string.h>
39 #include "global.h"
40 #include "tty.h"
41 #include "dir.h"
42 #include "color.h"
43 #include "dialog.h"
44 #include "menu.h"
45 #include "panel.h"
46 #include "main.h"
47 #include "win.h"
48 #include "mouse.h"
49 #include "option.h"
50 #include "tree.h"
51 #include "treestore.h"
52 #include "cons.saver.h"
53 #include "subshell.h"
54 #include "key.h" /* For init_key() and mi_getch() */
55 #include "setup.h" /* save_setup() */
56 #include "profile.h" /* free_profiles() */
57 #include "boxes.h" /* sort_box() */
58 #include "layout.h"
59 #include "cmd.h" /* Normal commands */
60 #include "hotlist.h"
61 #include "panelize.h"
62 #include "learn.h" /* learn_keys() */
63 #include "listmode.h"
64 #include "execute.h"
65 #include "ext.h" /* For flush_extension_file() */
67 /* Listbox for the command history feature */
68 #include "widget.h"
69 #include "command.h"
70 #include "wtools.h"
72 #include "chmod.h"
73 #include "chown.h"
74 #include "achown.h"
76 #ifdef WITH_SMBFS
77 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
78 #endif
80 #ifdef USE_INTERNAL_EDIT
81 # include "../edit/edit.h"
82 #endif
84 #ifdef HAVE_CHARSET
85 #include "charsets.h"
86 #endif /* HAVE_CHARSET */
88 #ifdef USE_VFS
89 #include "../vfs/gc.h"
90 #endif
92 #include "popt.h"
94 /* When the modes are active, left_panel, right_panel and tree_panel */
95 /* Point to a proper data structure. You should check with the functions */
96 /* get_current_type and get_other_type the types of the panels before using */
97 /* This pointer variables */
99 /* The structures for the panels */
100 WPanel *left_panel = NULL;
101 WPanel *right_panel = NULL;
103 /* The pointer to the tree */
104 WTree *the_tree = NULL;
106 /* The Menubar */
107 struct WMenu *the_menubar = NULL;
109 /* Pointers to the selected and unselected panel */
110 WPanel *current_panel = NULL;
112 /* Set if the command is being run from the "Right" menu */
113 int is_right = 0;
115 /* Set when main loop should be terminated */
116 volatile int quit = 0;
118 /* Set if you want the possible completions dialog for the first time */
119 int show_all_if_ambiguous = 0;
121 /* Set when cd symlink following is desirable (bash mode) */
122 int cd_symlinks = 1;
124 /* If set then dialogs just clean the screen when refreshing, else */
125 /* they do a complete refresh, refreshing all the parts of the program */
126 int fast_refresh = 0;
128 /* If true, marking a files moves the cursor down */
129 int mark_moves_down = 1;
131 /* If true, at startup the user-menu is invoked */
132 int auto_menu = 0;
134 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
135 and M-- and keypad + / - */
136 int alternate_plus_minus = 0;
138 /* If true, then the +, - and \ keys have their special meaning only if the
139 * command line is emtpy, otherwise they behave like regular letters
141 int only_leading_plus_minus = 1;
143 int pause_after_run = pause_on_dumb_terminals;
145 /* It true saves the setup when quitting */
146 int auto_save_setup = 1;
148 #ifndef HAVE_CHARSET
149 /* If true, allow characters in the range 160-255 */
150 int eight_bit_clean = 1;
153 * If true, also allow characters in the range 128-159.
154 * This is reported to break on many terminals (xterm, qansi-m).
156 int full_eight_bits = 0;
157 #endif /* !HAVE_CHARSET */
159 /* If true use the internal viewer */
160 int use_internal_view = 1;
162 /* Have we shown the fast-reload warning in the past? */
163 int fast_reload_w = 0;
165 /* Move page/item? When clicking on the top or bottom of a panel */
166 int mouse_move_pages = 1;
168 /* If true: l&r arrows are used to chdir if the input line is empty */
169 int navigate_with_arrows = 0;
171 /* If true use +, -, | for line drawing */
172 int force_ugly_line_drawing = 0;
174 /* If true program softkeys (HP terminals only) on startup and after every
175 command ran in the subshell to the description found in the termcap/terminfo
176 database */
177 int reset_hp_softkeys = 0;
179 /* The prompt */
180 const char *prompt = NULL;
182 /* The widget where we draw the prompt */
183 WLabel *the_prompt;
185 /* The hint bar */
186 WLabel *the_hint;
188 /* The button bar */
189 WButtonBar *the_bar;
191 /* For slow terminals */
192 int slow_terminal = 0;
194 /* Mouse type: GPM, xterm or none */
195 Mouse_Type use_mouse_p = MOUSE_NONE;
197 /* If true, assume we are running on an xterm terminal */
198 static int force_xterm = 0;
200 /* If on, default for "No" in delete operations */
201 int safe_delete = 0;
203 /* Controls screen clearing before an exec */
204 int clear_before_exec = 1;
206 /* Asks for confirmation before deleting a file */
207 int confirm_delete = 1;
209 /* Asks for confirmation before deleting a hotlist entry */
210 int confirm_directory_hotlist_delete = 1;
212 /* Asks for confirmation before overwriting a file */
213 int confirm_overwrite = 1;
215 /* Asks for confirmation before executing a program by pressing enter */
216 int confirm_execute = 0;
218 /* Asks for confirmation before leaving the program */
219 int confirm_exit = 1;
221 /* Asks for confirmation when using F3 to view a directory and there
222 are tagged files */
223 int confirm_view_dir = 0;
225 /* This flag indicates if the pull down menus by default drop down */
226 int drop_menus = 0;
228 /* The dialog handle for the main program */
229 Dlg_head *midnight_dlg;
231 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
232 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
233 int update_prompt = 0;
235 /* The home directory */
236 const char *home_dir = NULL;
238 /* The value of the other directory, only used when loading the setup */
239 char *other_dir = NULL;
241 /* Only used at program boot */
242 int boot_current_is_left = 1;
244 static char *this_dir = NULL;
246 /* If this is true, then when browsing the tree the other window will
247 * automatically reload it's directory with the contents of the currently
248 * selected directory.
250 int xtree_mode = 0;
252 /* If set, then print to the given file the last directory we were at */
253 static char *last_wd_file = NULL;
254 static char *last_wd_string = NULL;
255 /* Set to 1 to suppress printing the last directory */
256 static int print_last_revert = 0;
258 /* Force colors, only used by Slang */
259 int force_colors = 0;
261 /* colors specified on the command line: they override any other setting */
262 char *command_line_colors = NULL;
264 /* File name to view if argument was supplied */
265 static const char *view_one_file = NULL;
267 /* File name to edit if argument was supplied */
268 const char *edit_one_file = NULL;
270 /* Line to start the editor on */
271 static int edit_one_file_start_line = 0;
273 /* Used so that widgets know if they are being destroyed or
274 shut down */
275 int midnight_shutdown = 0;
277 /* The user's shell */
278 const char *shell = NULL;
280 /* mc_home: The home of MC */
281 char *mc_home = NULL;
283 char cmd_buf[512];
285 static void
286 reload_panelized (WPanel *panel)
288 int i, j;
289 dir_list *list = &panel->dir;
291 if (panel != current_panel)
292 mc_chdir (panel->cwd);
294 for (i = 0, j = 0; i < panel->count; i++) {
295 if (list->list[i].f.marked) {
296 /* Unmark the file in advance. In case the following mc_lstat
297 * fails we are done, else we have to mark the file again
298 * (Note: do_file_mark depends on a valid "list->list [i].buf").
299 * IMO that's the best way to update the panel's summary status
300 * -- Norbert
302 do_file_mark (panel, i, 0);
304 if (mc_lstat (list->list[i].fname, &list->list[i].st)) {
305 g_free (list->list[i].fname);
306 continue;
308 if (list->list[i].f.marked)
309 do_file_mark (panel, i, 1);
310 if (j != i)
311 list->list[j] = list->list[i];
312 j++;
314 if (j == 0)
315 panel->count = set_zero_dir (list);
316 else
317 panel->count = j;
319 if (panel != current_panel)
320 mc_chdir (current_panel->cwd);
323 static void
324 update_one_panel_widget (WPanel *panel, int force_update,
325 const char *current_file)
327 int free_pointer;
328 char *my_current_file = NULL;
330 if (force_update & UP_RELOAD) {
331 panel->is_panelized = 0;
332 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
333 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
336 /* If current_file == -1 (an invalid pointer) then preserve selection */
337 if (current_file == UP_KEEPSEL) {
338 free_pointer = 1;
339 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
340 current_file = my_current_file;
341 } else
342 free_pointer = 0;
344 if (panel->is_panelized)
345 reload_panelized (panel);
346 else
347 panel_reload (panel);
349 try_to_select (panel, current_file);
350 panel->dirty = 1;
352 if (free_pointer)
353 g_free (my_current_file);
356 void
357 panel_clean_dir (WPanel *panel)
359 int count = panel->count;
361 panel->count = 0;
362 panel->top_file = 0;
363 panel->selected = 0;
364 panel->marked = 0;
365 panel->dirs_marked = 0;
366 panel->total = 0;
367 panel->searching = 0;
368 panel->is_panelized = 0;
369 panel->dirty = 1;
371 clean_dir (&panel->dir, count);
374 static void
375 update_one_panel (int which, int force_update, const char *current_file)
377 WPanel *panel;
379 if (get_display_type (which) != view_listing)
380 return;
382 panel = (WPanel *) get_panel_widget (which);
383 update_one_panel_widget (panel, force_update, current_file);
386 /* This routine reloads the directory in both panels. It tries to
387 * select current_file in current_panel and other_file in other_panel.
388 * If current_file == -1 then it automatically sets current_file and
389 * other_file to the currently selected files in the panels.
391 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
392 * will not reload the other panel.
394 void
395 update_panels (int force_update, const char *current_file)
397 int reload_other = !(force_update & UP_ONLY_CURRENT);
398 WPanel *panel;
400 update_one_panel (get_current_index (), force_update, current_file);
401 if (reload_other)
402 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
404 if (get_current_type () == view_listing)
405 panel = (WPanel *) get_panel_widget (get_current_index ());
406 else
407 panel = (WPanel *) get_panel_widget (get_other_index ());
409 mc_chdir (panel->cwd);
412 /* Save current stat of directories to avoid reloading the panels */
413 /* when no modifications have taken place */
414 void
415 save_cwds_stat (void)
417 if (fast_reload) {
418 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
419 if (get_other_type () == view_listing)
420 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
424 #ifdef HAVE_SUBSHELL_SUPPORT
425 void
426 do_possible_cd (const char *new_dir)
428 if (!do_cd (new_dir, cd_exact))
429 message (D_ERROR, _("Warning"),
430 _(" The Commander can't change to the directory that \n"
431 " the subshell claims you are in. Perhaps you have \n"
432 " deleted your working directory, or given yourself \n"
433 " extra access permissions with the \"su\" command? "));
436 void
437 do_update_prompt (void)
439 if (update_prompt) {
440 printf ("%s", subshell_prompt);
441 fflush (stdout);
442 update_prompt = 0;
445 #endif /* HAVE_SUBSHELL_SUPPORT */
447 void
448 change_panel (void)
450 free_completions (cmdline);
451 dlg_one_down (midnight_dlg);
454 /* Stop MC main dialog and the current dialog if it exists.
455 * Needed to provide fast exit from MC viewer or editor on shell exit */
456 static void
457 stop_dialogs (void)
459 midnight_dlg->running = 0;
460 if (current_dlg) {
461 current_dlg->running = 0;
465 static int
466 quit_cmd_internal (int quiet)
468 int q = quit;
470 if (quiet || !confirm_exit) {
471 q = 1;
472 } else {
473 if (query_dialog
474 (_(" The Midnight Commander "),
475 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
476 2, _("&Yes"), _("&No")) == 0)
477 q = 1;
479 if (q) {
480 #ifdef HAVE_SUBSHELL_SUPPORT
481 if (!use_subshell)
482 stop_dialogs ();
483 else if ((q = exit_subshell ()))
484 #endif
485 stop_dialogs ();
487 if (q)
488 quit |= 1;
489 return quit;
492 static void
493 quit_cmd (void)
495 quit_cmd_internal (0);
498 void
499 quiet_quit_cmd (void)
501 print_last_revert = 1;
502 quit_cmd_internal (1);
506 * Touch window and refresh window functions
509 /* This routine untouches the first line on both panels in order */
510 /* to avoid the refreshing the menu bar */
512 void
513 repaint_screen (void)
515 do_refresh ();
516 mc_refresh ();
519 /* Wrapper for do_subshell_chdir, check for availability of subshell */
520 void
521 subshell_chdir (const char *directory)
523 #ifdef HAVE_SUBSHELL_SUPPORT
524 if (use_subshell) {
525 if (vfs_current_is_local ())
526 do_subshell_chdir (directory, 0, 1);
528 #endif /* HAVE_SUBSHELL_SUPPORT */
531 void
532 directory_history_add (struct WPanel *panel, const char *dir)
534 char *tmp;
536 tmp = g_strdup (dir);
537 strip_password (tmp, 1);
539 panel->dir_history = list_append_unique (panel->dir_history, tmp);
543 * If we moved to the parent directory move the selection pointer to
544 * the old directory name; If we leave VFS dir, remove FS specificator.
546 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
548 static const char *
549 get_parent_dir_name (const char *cwd, const char *lwd)
551 const char *p;
552 if (strlen (lwd) > strlen (cwd))
553 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
554 (strlen (cwd) == p - lwd || (p == lwd && cwd[0] == PATH_SEP &&
555 cwd[1] == '\0'))) {
556 return (p + 1);
558 return NULL;
562 * Changes the current directory of the panel.
563 * Don't record change in the directory history.
565 static int
566 _do_panel_cd (WPanel *panel, const char *new_dir, enum cd_enum cd_type)
568 const char *directory;
569 char *olddir;
570 char temp[MC_MAXPATHLEN];
571 char *translated_url;
573 if (cd_type == cd_parse_command) {
574 while (*new_dir == ' ')
575 new_dir++;
578 olddir = g_strdup (panel->cwd);
579 new_dir = translated_url = vfs_translate_url (new_dir);
581 /* Convert *new_path to a suitable pathname, handle ~user */
583 if (cd_type == cd_parse_command) {
584 if (!strcmp (new_dir, "-")) {
585 strcpy (temp, panel->lwd);
586 new_dir = temp;
589 directory = *new_dir ? new_dir : home_dir;
591 if (mc_chdir (directory) == -1) {
592 strcpy (panel->cwd, olddir);
593 g_free (olddir);
594 g_free (translated_url);
595 return 0;
597 g_free (translated_url);
599 /* Success: save previous directory, shutdown status of previous dir */
600 strcpy (panel->lwd, olddir);
601 free_completions (cmdline);
603 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
605 vfs_release_path (olddir);
607 subshell_chdir (panel->cwd);
609 /* Reload current panel */
610 panel_clean_dir (panel);
611 panel->count =
612 do_load_dir (panel->cwd, &panel->dir, panel->sort_type,
613 panel->reverse, panel->case_sensitive,
614 panel->exec_first, panel->filter);
615 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
616 load_hint (0);
617 panel->dirty = 1;
618 update_xterm_title_path ();
620 g_free (olddir);
622 return 1;
626 * Changes the current directory of the panel.
627 * Record change in the directory history.
630 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
632 int r;
634 r = _do_panel_cd (panel, new_dir, cd_type);
635 if (r)
636 directory_history_add (panel, panel->cwd);
637 return r;
641 do_cd (const char *new_dir, enum cd_enum exact)
643 return (do_panel_cd (current_panel, new_dir, exact));
646 void
647 directory_history_next (WPanel *panel)
649 GList *nextdir;
651 nextdir = g_list_next (panel->dir_history);
653 if (!nextdir)
654 return;
656 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
657 panel->dir_history = nextdir;
660 void
661 directory_history_prev (WPanel *panel)
663 GList *prevdir;
665 prevdir = g_list_previous (panel->dir_history);
667 if (!prevdir)
668 return;
670 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
671 panel->dir_history = prevdir;
674 void
675 directory_history_list (WPanel *panel)
677 char *s;
679 if (!panel->dir_history)
680 return;
682 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
684 if (!s)
685 return;
687 if (_do_panel_cd (panel, s, cd_exact))
688 directory_history_add (panel, panel->cwd);
689 else
690 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
691 g_free (s);
694 #ifdef HAVE_SUBSHELL_SUPPORT
696 load_prompt (int fd, void *unused)
698 (void) fd;
699 (void) unused;
701 if (!read_subshell_prompt ())
702 return 0;
704 /* Don't actually change the prompt if it's invisible */
705 if (current_dlg == midnight_dlg && command_prompt) {
706 char *tmp_prompt;
707 int prompt_len;
709 tmp_prompt = strip_ctrl_codes (subshell_prompt);
710 prompt_len = strlen (tmp_prompt);
712 /* Check for prompts too big */
713 if (COLS > 8 && prompt_len > COLS - 8) {
714 tmp_prompt[COLS - 8] = '\0';
715 prompt_len = COLS - 8;
717 prompt = tmp_prompt;
718 label_set_text (the_prompt, prompt);
719 winput_set_origin ((WInput *) cmdline, prompt_len,
720 COLS - prompt_len);
722 /* since the prompt has changed, and we are called from one of the
723 * get_event channels, the prompt updating does not take place
724 * automatically: force a cursor update and a screen refresh
726 update_cursor (midnight_dlg);
727 mc_refresh ();
729 update_prompt = 1;
730 return 0;
732 #endif /* HAVE_SUBSHELL_SUPPORT */
734 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
736 maybe_cd (int move_up_dir)
738 if (navigate_with_arrows) {
739 if (!cmdline->buffer[0]) {
740 if (move_up_dir) {
741 do_cd ("..", cd_exact);
742 return 1;
744 if (S_ISDIR (selection (current_panel)->st.st_mode)
745 || link_isdir (selection (current_panel))) {
746 do_cd (selection (current_panel)->fname, cd_exact);
747 return 1;
751 return 0;
754 static void
755 sort_cmd (void)
757 WPanel *p;
758 sortfn *sort_order;
760 if (!SELECTED_IS_PANEL)
761 return;
763 p = MENU_PANEL;
764 sort_order = sort_box (p->sort_type, &p->reverse,
765 &p->case_sensitive,
766 &p->exec_first);
768 panel_set_sort_order (p, sort_order);
772 static void
773 treebox_cmd (void)
775 char *sel_dir;
777 sel_dir = tree_box (selection (current_panel)->fname);
778 if (sel_dir) {
779 do_cd (sel_dir, cd_exact);
780 g_free (sel_dir);
784 #ifdef LISTMODE_EDITOR
785 static void
786 listmode_cmd (void)
788 char *newmode;
790 if (get_current_type () != view_listing)
791 return;
793 newmode = listmode_edit (current_panel->user_format);
794 if (!newmode)
795 return;
797 g_free (current_panel->user_format);
798 current_panel->list_type = list_user;
799 current_panel->user_format = newmode;
800 set_panel_formats (current_panel);
802 do_refresh ();
804 #endif /* LISTMODE_EDITOR */
806 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
807 static menu_entry LeftMenu[] = {
808 {' ', N_("&Listing mode..."), 'L', listing_cmd},
809 {' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd},
810 {' ', N_("&Info C-x i"), 'I', info_cmd},
811 {' ', N_("&Tree"), 'T', tree_cmd},
812 {' ', "", ' ', 0},
813 {' ', N_("&Sort order..."), 'S', sort_cmd},
814 {' ', "", ' ', 0},
815 {' ', N_("&Filter..."), 'F', filter_cmd},
816 #ifdef USE_NETCODE
817 {' ', "", ' ', 0},
818 #ifdef WITH_MCFS
819 {' ', N_("&Network link..."), 'N', netlink_cmd},
820 #endif
821 {' ', N_("FT&P link..."), 'P', ftplink_cmd},
822 {' ', N_("S&hell link..."), 'H', fishlink_cmd},
823 #ifdef WITH_SMBFS
824 {' ', N_("SM&B link..."), 'B', smblink_cmd},
825 #endif
826 #endif
827 {' ', "", ' ', 0},
828 {' ', N_("&Rescan C-r"), 'R', reread_cmd}
831 static menu_entry RightMenu[] = {
832 {' ', N_("&Listing mode..."), 'L', listing_cmd},
833 {' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd},
834 {' ', N_("&Info C-x i"), 'I', info_cmd},
835 {' ', N_("&Tree"), 'T', tree_cmd},
836 {' ', "", ' ', 0},
837 {' ', N_("&Sort order..."), 'S', sort_cmd},
838 {' ', "", ' ', 0},
839 {' ', N_("&Filter..."), 'F', filter_cmd},
840 #ifdef USE_NETCODE
841 {' ', "", ' ', 0},
842 #ifdef WITH_MCFS
843 {' ', N_("&Network link..."), 'N', netlink_cmd},
844 #endif
845 {' ', N_("FT&P link..."), 'P', ftplink_cmd},
846 {' ', N_("S&hell link..."), 'H', fishlink_cmd},
847 #ifdef WITH_SMBFS
848 {' ', N_("SM&B link..."), 'B', smblink_cmd},
849 #endif
850 #endif
851 {' ', "", ' ', 0},
852 {' ', N_("&Rescan C-r"), 'R', reread_cmd}
855 static menu_entry FileMenu[] = {
856 {' ', N_("&User menu F2"), 'U', user_file_menu_cmd},
857 {' ', N_("&View F3"), 'V', view_cmd},
858 {' ', N_("Vie&w file... "), 'W', view_file_cmd},
859 {' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd},
860 {' ', N_("&Edit F4"), 'E', edit_cmd},
861 {' ', N_("&Copy F5"), 'C', copy_cmd},
862 {' ', N_("c&Hmod C-x c"), 'H', chmod_cmd},
863 {' ', N_("&Link C-x l"), 'L', link_cmd},
864 {' ', N_("&SymLink C-x s"), 'S', symlink_cmd},
865 {' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd},
866 {' ', N_("ch&Own C-x o"), 'O', chown_cmd},
867 {' ', N_("&Advanced chown "), 'A', chown_advanced_cmd},
868 {' ', N_("&Rename/Move F6"), 'R', ren_cmd},
869 {' ', N_("&Mkdir F7"), 'M', mkdir_cmd},
870 {' ', N_("&Delete F8"), 'D', delete_cmd},
871 {' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd},
872 {' ', "", ' ', 0},
873 {' ', N_("select &Group M-+"), 'G', select_cmd},
874 {' ', N_("u&Nselect group M-\\"), 'N', unselect_cmd},
875 {' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd},
876 {' ', "", ' ', 0},
877 {' ', N_("e&Xit F10"), 'X', quit_cmd}
880 static menu_entry CmdMenu[] = {
881 /* I know, I'm lazy, but the tree widget when it's not running
882 * as a panel still has some problems, I have not yet finished
883 * the WTree widget port, sorry.
885 {' ', N_("&Directory tree"), 'D', treebox_cmd},
886 {' ', N_("&Find file M-?"), 'F', find_cmd},
887 {' ', N_("s&Wap panels C-u"), 'W', swap_cmd},
888 {' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd},
889 {' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd},
890 {' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize},
891 {' ', N_("show directory s&Izes"), 'I', dirsizes_cmd},
892 {' ', "", ' ', 0},
893 {' ', N_("command &History"), 'H', history_cmd},
894 {' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd},
895 #ifdef USE_VFS
896 {' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs},
897 #endif
898 #ifdef WITH_BACKGROUND
899 {' ', N_("&Background jobs C-x j"), 'B', jobs_cmd},
900 #endif
901 {' ', "", ' ', 0},
902 #ifdef USE_EXT2FSLIB
903 {' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd},
904 #endif
905 #ifdef LISTMODE_EDITOR
906 {' ', N_("&Listing format edit"), 'L', listmode_cmd},
907 #endif
908 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
909 {' ', "", ' ', 0},
910 #endif
911 {' ', N_("Edit &extension file"), 'E', ext_cmd},
912 {' ', N_("Edit &menu file"), 'M', edit_mc_menu_cmd},
913 #ifdef USE_INTERNAL_EDIT
914 {' ', N_("Edit edi&tor menu file"), 'T', edit_user_menu_cmd},
915 {' ', N_("Edit &syntax file"), 'S', edit_syntax_cmd}
916 #endif /* USE_INTERNAL_EDIT */
919 /* Must keep in sync with the constants in menu_cmd */
920 static menu_entry OptMenu[] = {
921 {' ', N_("&Configuration..."), 'C', configure_box},
922 {' ', N_("&Layout..."), 'L', layout_cmd},
923 {' ', N_("c&Onfirmation..."), 'O', confirm_box},
924 {' ', N_("&Display bits..."), 'D', display_bits_box},
925 {' ', N_("learn &Keys..."), 'K', learn_keys},
926 #ifdef USE_VFS
927 {' ', N_("&Virtual FS..."), 'V', configure_vfs},
928 #endif /* !USE_VFS */
929 {' ', "", ' ', 0},
930 {' ', N_("&Save setup"), 'S', save_setup_cmd}
933 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
935 static Menu *MenuBar[5];
937 void
938 init_menu (void)
940 MenuBar[0] =
941 create_menu (horizontal_split ? _(" &Above ") : _(" &Left "),
942 LeftMenu, menu_entries (LeftMenu),
943 "[Left and Right Menus]");
944 MenuBar[1] =
945 create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
946 "[File Menu]");
947 MenuBar[2] =
948 create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
949 "[Command Menu]");
950 MenuBar[3] =
951 create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
952 "[Options Menu]");
953 MenuBar[4] =
954 create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
955 RightMenu, menu_entries (RightMenu),
956 "[Left and Right Menus]");
959 void
960 done_menu (void)
962 int i;
964 for (i = 0; i < 5; i++) {
965 destroy_menu (MenuBar[i]);
969 static void
970 menu_last_selected_cmd (void)
972 the_menubar->active = 1;
973 the_menubar->dropped = drop_menus;
974 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
975 dlg_select_widget (the_menubar);
978 static void
979 menu_cmd (void)
981 if (the_menubar->active)
982 return;
984 if ((get_current_index () == 0) ^ (!current_panel->active))
985 the_menubar->selected = 0;
986 else
987 the_menubar->selected = 4;
988 menu_last_selected_cmd ();
991 /* Flag toggling functions */
992 void
993 toggle_fast_reload (void)
995 fast_reload = !fast_reload;
996 if (fast_reload_w == 0 && fast_reload) {
997 message (D_NORMAL, _(" Information "),
999 (" Using the fast reload option may not reflect the exact \n"
1000 " directory contents. In this case you'll need to do a \n"
1001 " manual reload of the directory. See the man page for \n"
1002 " the details. "));
1003 fast_reload_w = 1;
1007 void
1008 toggle_mix_all_files (void)
1010 mix_all_files = !mix_all_files;
1011 update_panels (UP_RELOAD, UP_KEEPSEL);
1014 void
1015 toggle_show_backup (void)
1017 show_backups = !show_backups;
1018 update_panels (UP_RELOAD, UP_KEEPSEL);
1021 void
1022 toggle_show_hidden (void)
1024 show_dot_files = !show_dot_files;
1025 update_panels (UP_RELOAD, UP_KEEPSEL);
1029 * Just a hack for allowing url-like pathnames to be accepted from the
1030 * command line.
1032 static void
1033 translated_mc_chdir (char *dir)
1035 char *newdir;
1037 newdir = vfs_translate_url (dir);
1038 mc_chdir (newdir);
1039 g_free (newdir);
1042 static void
1043 create_panels (void)
1045 int current_index;
1046 int other_index;
1047 int current_mode;
1048 int other_mode;
1049 char original_dir[1024];
1051 original_dir[0] = 0;
1053 if (boot_current_is_left) {
1054 current_index = 0;
1055 other_index = 1;
1056 current_mode = startup_left_mode;
1057 other_mode = startup_right_mode;
1058 } else {
1059 current_index = 1;
1060 other_index = 0;
1061 current_mode = startup_right_mode;
1062 other_mode = startup_left_mode;
1064 /* Creates the left panel */
1065 if (this_dir) {
1066 if (other_dir) {
1067 /* Ok, user has specified two dirs, save the original one,
1068 * since we may not be able to chdir to the proper
1069 * second directory later
1071 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1073 translated_mc_chdir (this_dir);
1075 set_display_type (current_index, current_mode);
1077 /* The other panel */
1078 if (other_dir) {
1079 if (original_dir[0])
1080 translated_mc_chdir (original_dir);
1081 translated_mc_chdir (other_dir);
1083 set_display_type (other_index, other_mode);
1085 if (startup_left_mode == view_listing) {
1086 current_panel = left_panel;
1087 } else {
1088 if (right_panel)
1089 current_panel = right_panel;
1090 else
1091 current_panel = left_panel;
1094 /* Create the nice widgets */
1095 cmdline = command_new (0, 0, 0);
1096 the_prompt = label_new (0, 0, prompt);
1097 the_prompt->transparent = 1;
1098 the_bar = buttonbar_new (keybar_visible);
1100 the_hint = label_new (0, 0, 0);
1101 the_hint->transparent = 1;
1102 the_hint->auto_adjust_cols = 0;
1103 the_hint->widget.cols = COLS;
1105 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1108 static void
1109 copy_current_pathname (void)
1111 if (!command_prompt)
1112 return;
1114 command_insert (cmdline, current_panel->cwd, 0);
1115 if (current_panel->cwd[strlen (current_panel->cwd) - 1] != PATH_SEP)
1116 command_insert (cmdline, PATH_SEP_STR, 0);
1119 static void
1120 copy_other_pathname (void)
1122 if (get_other_type () != view_listing)
1123 return;
1125 if (!command_prompt)
1126 return;
1128 command_insert (cmdline, other_panel->cwd, 0);
1129 if (other_panel->cwd[strlen (other_panel->cwd) - 1] != PATH_SEP)
1130 command_insert (cmdline, PATH_SEP_STR, 0);
1133 static void
1134 copy_readlink (WPanel *panel)
1136 if (!command_prompt)
1137 return;
1138 if (S_ISLNK (selection (panel)->st.st_mode)) {
1139 char buffer[MC_MAXPATHLEN];
1140 char *p =
1141 mhl_str_dir_plus_file (panel->cwd, selection (panel)->fname);
1142 int i;
1144 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1145 g_free (p);
1146 if (i > 0) {
1147 buffer[i] = 0;
1148 command_insert (cmdline, buffer, 1);
1153 static void
1154 copy_current_readlink (void)
1156 copy_readlink (current_panel);
1159 static void
1160 copy_other_readlink (void)
1162 if (get_other_type () != view_listing)
1163 return;
1164 copy_readlink (other_panel);
1167 /* Insert the selected file name into the input line */
1168 static void
1169 copy_prog_name (void)
1171 char *tmp;
1172 if (!command_prompt)
1173 return;
1175 if (get_current_type () == view_tree) {
1176 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1177 tmp = tree_selected_name (tree);
1178 } else
1179 tmp = selection (current_panel)->fname;
1181 command_insert (cmdline, tmp, 1);
1184 static void
1185 copy_tagged (WPanel *panel)
1187 int i;
1189 if (!command_prompt)
1190 return;
1191 input_disable_update (cmdline);
1192 if (panel->marked) {
1193 for (i = 0; i < panel->count; i++) {
1194 if (panel->dir.list[i].f.marked)
1195 command_insert (cmdline, panel->dir.list[i].fname, 1);
1197 } else {
1198 command_insert (cmdline, panel->dir.list[panel->selected].fname,
1201 input_enable_update (cmdline);
1204 static void
1205 copy_current_tagged (void)
1207 copy_tagged (current_panel);
1210 static void
1211 copy_other_tagged (void)
1213 if (get_other_type () != view_listing)
1214 return;
1215 copy_tagged (other_panel);
1218 static void
1219 init_labels (void)
1221 buttonbar_set_label (midnight_dlg, 1, _("Help"), help_cmd);
1222 buttonbar_set_label (midnight_dlg, 2, _("Menu"), user_file_menu_cmd);
1223 buttonbar_set_label (midnight_dlg, 9, _("PullDn"), menu_cmd);
1224 buttonbar_set_label (midnight_dlg, 10, _("Quit"), quit_cmd);
1227 static const key_map ctl_x_map[] = {
1228 {XCTRL ('c'), quit_cmd},
1229 {'d', compare_dirs_cmd},
1230 #ifdef USE_VFS
1231 {'a', reselect_vfs},
1232 #endif /* USE_VFS */
1233 {'p', copy_current_pathname},
1234 {XCTRL ('p'), copy_other_pathname},
1235 {'t', copy_current_tagged},
1236 {XCTRL ('t'), copy_other_tagged},
1237 {'c', chmod_cmd},
1238 {'o', chown_cmd},
1239 {'r', copy_current_readlink},
1240 {XCTRL ('r'), copy_other_readlink},
1241 {'l', link_cmd},
1242 {'s', symlink_cmd},
1243 {XCTRL ('s'), edit_symlink_cmd},
1244 {'i', info_cmd_no_menu},
1245 {'q', quick_cmd_no_menu},
1246 {'h', add2hotlist_cmd},
1247 {'!', external_panelize},
1248 #ifdef WITH_BACKGROUND
1249 {'j', jobs_cmd},
1250 #endif /* WITH_BACKGROUND */
1251 {0, 0}
1254 static int ctl_x_map_enabled = 0;
1256 static void
1257 ctl_x_cmd (void)
1259 ctl_x_map_enabled = 1;
1262 static void
1263 nothing (void)
1267 static const key_map default_map[] = {
1268 {KEY_F (19), menu_last_selected_cmd},
1269 {KEY_F (20), quiet_quit_cmd},
1271 {XCTRL ('@'), single_dirsize_cmd},
1273 /* Copy useful information to the command line */
1274 {ALT ('a'), copy_current_pathname},
1275 {ALT ('A'), copy_other_pathname},
1277 {ALT ('c'), quick_cd_cmd},
1279 /* To access the directory hotlist */
1280 {XCTRL ('\\'), quick_chdir_cmd},
1282 /* Suspend */
1283 {XCTRL ('z'), suspend_cmd},
1285 /* The filtered view command */
1286 {ALT ('!'), filtered_view_cmd},
1288 /* Find file */
1289 {ALT ('?'), find_cmd},
1291 /* Panel refresh */
1292 {XCTRL ('r'), reread_cmd},
1294 /* Toggle listing between long, user defined and full formats */
1295 {ALT ('t'), toggle_listing_cmd},
1297 /* Swap panels */
1298 {XCTRL ('u'), swap_cmd},
1300 /* View output */
1301 {XCTRL ('o'), view_other_cmd},
1303 /* Control-X keybindings */
1304 {XCTRL ('x'), ctl_x_cmd},
1306 /* Trap dlg's exit commands */
1307 {ESC_CHAR, nothing},
1308 {XCTRL ('c'), nothing},
1309 {XCTRL ('g'), nothing},
1310 {0, 0},
1313 static void
1314 setup_sigwinch (void)
1316 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && defined(SIGWINCH)
1317 struct sigaction act, oact;
1318 act.sa_handler = flag_winch;
1319 sigemptyset (&act.sa_mask);
1320 act.sa_flags = 0;
1321 #ifdef SA_RESTART
1322 act.sa_flags |= SA_RESTART;
1323 #endif
1324 sigaction (SIGWINCH, &act, &oact);
1325 #endif
1328 static void
1329 setup_pre (void)
1331 /* Call all the inits */
1332 #ifdef HAVE_CHARSET
1334 * Don't restrict the output on the screen manager level,
1335 * the translation tables take care of it.
1337 #define full_eight_bits (1)
1338 #define eight_bit_clean (1)
1339 #endif /* !HAVE_CHARSET */
1341 #ifndef HAVE_SLANG
1342 meta (stdscr, eight_bit_clean);
1343 #else
1344 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1345 #endif
1348 static void
1349 init_xterm_support (void)
1351 const char *termvalue;
1353 termvalue = getenv ("TERM");
1354 if (!termvalue || !(*termvalue)) {
1355 fputs (_("The TERM environment variable is unset!\n"), stderr);
1356 exit (1);
1359 /* Check mouse capabilities */
1360 xmouse_seq = tty_tgetstr ("Km");
1362 if (strcmp (termvalue, "cygwin") == 0) {
1363 force_xterm = 1;
1364 use_mouse_p = MOUSE_DISABLED;
1367 if (force_xterm || strncmp (termvalue, "xterm", 5) == 0
1368 || strncmp (termvalue, "rxvt", 4) == 0
1369 || strcmp (termvalue, "Eterm") == 0
1370 || strcmp (termvalue, "dtterm") == 0) {
1371 xterm_flag = 1;
1373 /* Default to the standard xterm sequence */
1374 if (!xmouse_seq) {
1375 xmouse_seq = ESC_STR "[M";
1378 /* Enable mouse unless explicitly disabled by --nomouse */
1379 if (use_mouse_p != MOUSE_DISABLED) {
1380 const char *color_term = getenv ("COLORTERM");
1381 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1382 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1383 strcmp (termvalue, "Eterm") == 0) {
1384 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1385 } else {
1386 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1392 static void
1393 setup_mc (void)
1395 setup_pre ();
1396 init_menu ();
1397 create_panels ();
1398 setup_panels ();
1400 #ifdef HAVE_SUBSHELL_SUPPORT
1401 if (use_subshell)
1402 add_select_channel (subshell_pty, load_prompt, 0);
1403 #endif /* !HAVE_SUBSHELL_SUPPORT */
1405 setup_sigwinch ();
1407 if (baudrate () < 9600 || slow_terminal) {
1408 verbose = 0;
1410 init_mouse ();
1413 static void
1414 setup_dummy_mc ()
1416 char d[MC_MAXPATHLEN];
1418 mc_get_current_wd (d, MC_MAXPATHLEN);
1419 setup_mc ();
1420 mc_chdir (d);
1423 static void
1424 done_mc (void)
1426 disable_mouse ();
1428 done_menu ();
1430 /* Setup shutdown
1432 * We sync the profiles since the hotlist may have changed, while
1433 * we only change the setup data if we have the auto save feature set
1435 if (auto_save_setup)
1436 save_setup (); /* does also call save_hotlist */
1437 else
1438 save_hotlist ();
1439 done_screen ();
1440 vfs_add_current_stamps ();
1443 /* This should be called after destroy_dlg since panel widgets
1444 * save their state on the profiles
1446 static void
1447 done_mc_profile (void)
1449 if (!auto_save_setup)
1450 profile_forget_profile (profile_name);
1451 sync_profiles ();
1452 done_setup ();
1453 free_profiles ();
1456 static cb_ret_t
1457 midnight_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
1459 int i;
1461 switch (msg) {
1463 case DLG_IDLE:
1464 /* We only need the first idle event */
1465 set_idle_proc (h, 0);
1466 if (auto_menu) {
1467 user_file_menu_cmd ();
1469 return MSG_HANDLED;
1471 case DLG_KEY:
1472 if (ctl_x_map_enabled) {
1473 ctl_x_map_enabled = 0;
1474 for (i = 0; ctl_x_map[i].key_code; i++)
1475 if (parm == ctl_x_map[i].key_code) {
1476 (*ctl_x_map[i].fn) ();
1477 return MSG_HANDLED;
1481 /* FIXME: should handle all menu shortcuts before this point */
1482 if (the_menubar->active)
1483 return MSG_NOT_HANDLED;
1485 if (parm == KEY_F (10)) {
1486 quit_cmd ();
1487 return MSG_HANDLED;
1490 if (parm == '\t')
1491 free_completions (cmdline);
1493 if (parm == '\n') {
1494 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1495 cmdline->buffer[i] == '\t'); i++);
1496 if (cmdline->buffer[i]) {
1497 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1498 return MSG_HANDLED;
1500 stuff (cmdline, "", 0);
1501 cmdline->point = 0;
1504 /* Ctrl-Enter and Alt-Enter */
1505 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n')
1506 && (parm & (KEY_M_CTRL | KEY_M_ALT))) {
1507 copy_prog_name ();
1508 return MSG_HANDLED;
1511 /* Ctrl-Shift-Enter */
1512 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n')) {
1513 copy_current_pathname ();
1514 copy_prog_name ();
1515 return MSG_HANDLED;
1518 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1519 && !quote && !current_panel->searching) {
1520 if (!only_leading_plus_minus) {
1521 /* Special treatement, since the input line will eat them */
1522 if (parm == '+') {
1523 select_cmd ();
1524 return MSG_HANDLED;
1527 if (parm == '\\' || parm == '-') {
1528 unselect_cmd ();
1529 return MSG_HANDLED;
1532 if (parm == '*') {
1533 reverse_selection_cmd ();
1534 return MSG_HANDLED;
1536 } else if (!command_prompt || !cmdline->buffer[0]) {
1537 /* Special treatement '+', '-', '\', '*' only when this is
1538 * first char on input line
1541 if (parm == '+') {
1542 select_cmd ();
1543 return MSG_HANDLED;
1546 if (parm == '\\' || parm == '-') {
1547 unselect_cmd ();
1548 return MSG_HANDLED;
1551 if (parm == '*') {
1552 reverse_selection_cmd ();
1553 return MSG_HANDLED;
1557 return MSG_NOT_HANDLED;
1559 case DLG_HOTKEY_HANDLED:
1560 if ((get_current_type () == view_listing) && current_panel->searching) {
1561 current_panel->searching = 0;
1562 current_panel->dirty = 1;
1564 return MSG_HANDLED;
1566 case DLG_UNHANDLED_KEY:
1567 if (command_prompt) {
1568 int v;
1570 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1571 if (v)
1572 return v;
1574 if (ctl_x_map_enabled) {
1575 ctl_x_map_enabled = 0;
1576 for (i = 0; ctl_x_map[i].key_code; i++)
1577 if (parm == ctl_x_map[i].key_code) {
1578 (*ctl_x_map[i].fn) ();
1579 return MSG_HANDLED;
1581 } else {
1582 for (i = 0; default_map[i].key_code; i++) {
1583 if (parm == default_map[i].key_code) {
1584 (*default_map[i].fn) ();
1585 return MSG_HANDLED;
1589 return MSG_NOT_HANDLED;
1591 case DLG_DRAW:
1592 /* We handle the special case of the output lines */
1593 if (console_flag && output_lines)
1594 show_console_contents (output_start_y,
1595 LINES - output_lines - keybar_visible -
1596 1, LINES - keybar_visible - 1);
1597 return MSG_HANDLED;
1599 case DLG_POST_KEY:
1600 if (!the_menubar->active)
1601 update_dirty_panels ();
1602 return MSG_HANDLED;
1604 default:
1605 return default_dlg_callback (h, msg, parm);
1609 /* Show current directory in the xterm title */
1610 void
1611 update_xterm_title_path (void)
1613 char *p, *s;
1615 if (xterm_flag && xterm_title) {
1616 p = s = g_strdup (strip_home_and_password (current_panel->cwd));
1617 do {
1618 if (!is_printable ((unsigned char) *s))
1619 *s = '?';
1620 } while (*++s);
1621 if (!alternate_plus_minus)
1622 numeric_keypad_mode ();
1623 fprintf (stdout, "\33]0;mc - %s\7", p);
1624 fflush (stdout);
1625 g_free (p);
1630 * Load new hint and display it.
1631 * IF force is not 0, ignore the timeout.
1633 void
1634 load_hint (int force)
1636 char *hint;
1638 if (!the_hint->widget.parent)
1639 return;
1641 if (!message_visible) {
1642 label_set_text (the_hint, 0);
1643 return;
1646 if ((hint = get_random_hint (force))) {
1647 if (*hint)
1648 set_hintbar (hint);
1649 g_free (hint);
1650 } else {
1651 char text[BUF_SMALL];
1653 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1654 VERSION);
1655 set_hintbar (text);
1659 static void
1660 setup_panels_and_run_mc (void)
1662 add_widget (midnight_dlg, the_menubar);
1663 add_widget (midnight_dlg, get_panel_widget (0));
1664 add_widget (midnight_dlg, get_panel_widget (1));
1665 add_widget (midnight_dlg, the_hint);
1666 load_hint (1);
1667 add_widget (midnight_dlg, cmdline);
1668 add_widget (midnight_dlg, the_prompt);
1669 add_widget (midnight_dlg, the_bar);
1670 init_labels ();
1672 if (boot_current_is_left)
1673 dlg_select_widget (get_panel_widget (0));
1674 else
1675 dlg_select_widget (get_panel_widget (1));
1677 /* Run the Midnight Commander if no file was specified in the command line */
1678 run_dlg (midnight_dlg);
1681 /* result must be free'd (I think this should go in util.c) */
1682 static char *
1683 prepend_cwd_on_local (const char *filename)
1685 char *d;
1686 int l;
1688 if (vfs_file_is_local (filename)) {
1689 if (*filename == PATH_SEP) /* an absolute pathname */
1690 return g_strdup (filename);
1691 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1692 mc_get_current_wd (d, MC_MAXPATHLEN);
1693 l = strlen (d);
1694 d[l++] = PATH_SEP;
1695 strcpy (d + l, filename);
1696 canonicalize_pathname (d);
1697 return d;
1698 } else
1699 return g_strdup (filename);
1702 static int
1703 mc_maybe_editor_or_viewer (void)
1705 if (!(view_one_file || edit_one_file))
1706 return 0;
1708 setup_dummy_mc ();
1710 /* Invoke the internal view/edit routine with:
1711 * the default processing and forcing the internal viewer/editor
1713 if (view_one_file) {
1714 char *path = NULL;
1715 path = prepend_cwd_on_local (view_one_file);
1716 view_file (path, 0, 1);
1717 g_free (path);
1719 #ifdef USE_INTERNAL_EDIT
1720 else {
1721 edit_file (edit_one_file, edit_one_file_start_line);
1723 #endif /* USE_INTERNAL_EDIT */
1724 midnight_shutdown = 1;
1725 done_mc ();
1726 return 1;
1729 /* Run the main dialog that occupies the whole screen */
1730 static void
1731 do_nc (void)
1733 int midnight_colors[4];
1735 midnight_colors[0] = NORMAL_COLOR; /* NORMALC */
1736 midnight_colors[1] = REVERSE_COLOR; /* FOCUSC */
1737 midnight_colors[2] = INPUT_COLOR; /* HOT_NORMALC */
1738 midnight_colors[3] = NORMAL_COLOR; /* HOT_FOCUSC */
1740 midnight_dlg =
1741 create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1742 "[main]", NULL, DLG_WANT_IDLE);
1744 /* Check if we were invoked as an editor or file viewer */
1745 if (mc_maybe_editor_or_viewer ())
1746 return;
1748 setup_mc ();
1750 setup_panels_and_run_mc ();
1752 /* Program end */
1753 midnight_shutdown = 1;
1755 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1756 if (last_wd_file && vfs_current_is_local ()) {
1757 last_wd_string = g_strdup (current_panel->cwd);
1759 done_mc ();
1761 destroy_dlg (midnight_dlg);
1762 current_panel = 0;
1763 done_mc_profile ();
1766 /* POSIX version. The only version we support. */
1767 static void
1768 OS_Setup (void)
1770 const char *mc_libdir;
1771 shell = getenv ("SHELL");
1772 if (!shell || !*shell) {
1773 struct passwd *pwd;
1774 pwd = getpwuid (geteuid ());
1775 if (pwd != NULL)
1776 shell = g_strdup (pwd->pw_shell);
1778 if (!shell || !*shell)
1779 shell = "/bin/sh";
1781 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1782 /* and can be overriden by the MC_DATADIR environment variable */
1783 if ((mc_libdir = getenv ("MC_DATADIR")) != NULL) {
1784 mc_home = g_strdup (mc_libdir);
1785 } else {
1786 mc_home = g_strdup (DATADIR);
1790 static void
1791 sigchld_handler_no_subshell (int sig)
1793 #ifdef __linux__
1794 int pid, status;
1796 if (!console_flag)
1797 return;
1799 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1800 the value of console_flag never changed, we could simply not install
1801 this handler at all if (!console_flag && !use_subshell). */
1803 /* That comment is no longer true. We need to wait() on a sigchld
1804 handler (that's at least what the tarfs code expects currently). */
1806 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1808 if (pid == cons_saver_pid) {
1810 if (WIFSTOPPED (status)) {
1811 /* Someone has stopped cons.saver - restart it */
1812 kill (pid, SIGCONT);
1813 } else {
1814 /* cons.saver has died - disable console saving */
1815 handle_console (CONSOLE_DONE);
1816 console_flag = 0;
1819 /* If we got here, some other child exited; ignore it */
1820 #endif /* __linux__ */
1822 (void) sig;
1825 static void
1826 init_sigchld (void)
1828 struct sigaction sigchld_action;
1830 sigchld_action.sa_handler =
1831 #ifdef HAVE_SUBSHELL_SUPPORT
1832 use_subshell ? sigchld_handler :
1833 #endif /* HAVE_SUBSHELL_SUPPORT */
1834 sigchld_handler_no_subshell;
1836 sigemptyset (&sigchld_action.sa_mask);
1838 #ifdef SA_RESTART
1839 sigchld_action.sa_flags = SA_RESTART;
1840 #else
1841 sigchld_action.sa_flags = 0;
1842 #endif /* !SA_RESTART */
1844 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
1845 #ifdef HAVE_SUBSHELL_SUPPORT
1847 * This may happen on QNX Neutrino 6, where SA_RESTART
1848 * is defined but not implemented. Fallback to no subshell.
1850 use_subshell = 0;
1851 #endif /* HAVE_SUBSHELL_SUPPORT */
1855 static void
1856 print_mc_usage (poptContext ctx, FILE *stream)
1858 int leftColWidth;
1860 poptSetOtherOptionHelp (ctx,
1861 _("[flags] [this_dir] [other_panel_dir]\n"));
1863 /* print help for options */
1864 leftColWidth = poptPrintHelp (ctx, stream, 0);
1865 fprintf (stream, " %-*s %s\n", leftColWidth, _("+number"),
1866 _("Set initial line number for the internal editor"));
1867 fputs (_
1868 ("\n"
1869 "Please send any bug reports (including the output of `mc -V')\n"
1870 "to mc-devel@gnome.org\n"), stream);
1871 show_version (0);
1874 static void
1875 print_color_usage (void)
1878 * FIXME: undocumented keywords: viewunderline, editnormal, editbold,
1879 * and editmarked. To preserve translations, lines should be split.
1881 /* TRANSLATORS: don't translate keywords and names of colors */
1882 fputs (_
1883 ("--colors KEYWORD={FORE},{BACK}\n\n"
1884 "{FORE} and {BACK} can be omitted, and the default will be used\n"
1885 "\n" "Keywords:\n"
1886 " Global: errors, reverse, gauge, input, viewunderline\n"
1887 " File display: normal, selected, marked, markselect\n"
1888 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus, errdhotnormal,\n"
1889 " errdhotfocus\n"
1890 " Menus: menu, menuhot, menusel, menuhotsel\n"
1891 " Editor: editnormal, editbold, editmarked\n"
1892 " Help: helpnormal, helpitalic, helpbold, helplink, helpslink\n"
1893 " File types: directory, executable, link, stalelink, device, special, core\n"
1894 "\n" "Colors:\n"
1895 " black, gray, red, brightred, green, brightgreen, brown,\n"
1896 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
1897 " brightcyan, lightgray and white\n\n"), stdout);
1900 static void
1901 process_args (poptContext ctx, int c, const char *option_arg)
1903 switch (c) {
1904 case 'V':
1905 show_version (1);
1906 exit (0);
1907 break;
1909 case 'c':
1910 disable_colors = 0;
1911 #ifdef HAVE_SLANG
1912 force_colors = 1;
1913 #endif /* HAVE_SLANG */
1914 break;
1916 case 'f':
1917 printf ("%s\n", mc_home);
1918 exit (0);
1919 break;
1921 #ifdef USE_NETCODE
1922 case 'l':
1923 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) option_arg);
1924 #ifdef WITH_SMBFS
1925 smbfs_set_debugf (option_arg);
1926 #endif /* WITH_SMBFS */
1927 break;
1929 #ifdef WITH_SMBFS
1930 case 'D':
1931 smbfs_set_debug (atoi (option_arg));
1932 break;
1933 #endif /* WITH_SMBFS */
1934 #endif /* USE_NETCODE */
1936 case 'd':
1937 use_mouse_p = MOUSE_DISABLED;
1938 break;
1940 #ifdef HAVE_SUBSHELL_SUPPORT
1941 case 'u':
1942 use_subshell = 0;
1943 break;
1944 #endif /* HAVE_SUBSHELL_SUPPORT */
1946 case 'H':
1947 print_color_usage ();
1948 exit (0);
1949 break;
1951 case 'h':
1952 print_mc_usage (ctx, stdout);
1953 exit (0);
1957 static const struct poptOption argument_table[] = {
1958 /* generic options */
1959 {"help", 'h', POPT_ARG_NONE, NULL, 'h',
1960 N_("Displays this help message"), NULL},
1961 {"version", 'V', POPT_ARG_NONE, NULL, 'V',
1962 N_("Displays the current version"), NULL},
1964 /* terminal options */
1965 {"xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
1966 N_("Forces xterm features"), NULL},
1967 {"nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
1968 N_("Disable mouse support in text version"), NULL},
1969 #if defined(HAVE_SLANG)
1970 {"termcap", 't', 0, &SLtt_Try_Termcap, 0,
1971 N_("Tries to use termcap instead of terminfo"), NULL},
1972 #endif
1973 {"resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
1974 N_("Resets soft keys on HP terminals"), NULL},
1975 {"slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
1976 N_("To run on slow terminals"), NULL},
1977 {"stickchars", 'a', 0, &force_ugly_line_drawing, 0,
1978 N_("Use stickchars to draw"), NULL},
1980 /* color options */
1981 {"nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
1982 N_("Requests to run in black and white"), NULL},
1983 {"color", 'c', POPT_ARG_NONE, NULL, 'c',
1984 N_("Request to run in color mode"), NULL},
1985 {"colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
1986 N_("Specifies a color configuration"), NULL},
1987 {"help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
1988 N_("Displays a help screen on how to change the color scheme"), NULL},
1990 /* debug options */
1991 #ifdef USE_NETCODE
1992 {"ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
1993 N_("Log ftp dialog to specified file"), NULL},
1994 #ifdef WITH_SMBFS
1995 {"debuglevel", 'D', POPT_ARG_STRING, NULL, 'D',
1996 N_("Set debug level"), NULL},
1997 #endif
1998 #endif
2000 /* options for wrappers */
2001 {"datadir", 'f', POPT_ARG_NONE, NULL, 'f',
2002 N_("Print data directory"), NULL},
2003 {"printwd", 'P', POPT_ARG_STRING, &last_wd_file, 0,
2004 N_("Print last working directory to specified file"), NULL},
2006 /* subshell options */
2007 #ifdef HAVE_SUBSHELL_SUPPORT
2008 {"subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2009 N_("Enables subshell support (default)"), NULL},
2010 {"nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2011 N_("Disables subshell support"), NULL},
2012 #endif
2014 /* single file operations */
2015 {"view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2016 N_("Launches the file viewer on a file"), NULL},
2017 #ifdef USE_INTERNAL_EDIT
2018 {"edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2019 N_("Edits one file"), NULL},
2020 #endif
2022 {NULL, '\0', 0, NULL, 0, NULL, NULL}
2025 static void
2026 handle_args (int argc, char *argv[])
2028 char *tmp;
2029 poptContext ctx;
2030 const char *base;
2031 int c;
2033 ctx =
2034 poptGetContext ("mc", argc, argv, argument_table,
2035 POPT_CONTEXT_NO_EXEC);
2037 while ((c = poptGetNextOpt (ctx)) > 0) {
2038 process_args (ctx, c, poptGetOptArg (ctx));
2041 if (c < -1) {
2042 print_mc_usage (ctx, stderr);
2043 fprintf (stderr, "%s: %s\n",
2044 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2045 poptStrerror (c));
2046 exit (1);
2049 tmp = poptGetArg (ctx);
2052 * Check for special invocation names mcedit and mcview,
2053 * if none apply then set the current directory and the other
2054 * directory from the command line arguments
2056 base = x_basename (argv[0]);
2057 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
2058 edit_one_file = "";
2059 if (tmp) {
2061 * Check for filename:lineno, followed by an optional colon.
2062 * This format is used by many programs (especially compilers)
2063 * in error messages and warnings. It is supported so that
2064 * users can quickly copy and paste file locations.
2066 char *end = tmp + strlen (tmp), *p = end;
2067 if (p > tmp && p[-1] == ':')
2068 p--;
2069 while (p > tmp && isdigit ((unsigned char) p[-1]))
2070 p--;
2071 if (tmp < p && p < end && p[-1] == ':') {
2072 struct stat st;
2073 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2075 * Check that the file before the colon actually exists.
2076 * If it doesn't exist, revert to the old behavior.
2078 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
2079 edit_one_file = fname;
2080 edit_one_file_start_line = atoi (p);
2081 } else {
2082 g_free (fname);
2083 goto try_plus_filename;
2085 } else {
2086 try_plus_filename:
2087 if (*tmp == '+' && isdigit ((unsigned char) tmp[1])) {
2088 int start_line = atoi (tmp);
2089 if (start_line > 0) {
2090 char *file = poptGetArg (ctx);
2091 if (file) {
2092 tmp = file;
2093 edit_one_file_start_line = start_line;
2097 edit_one_file = g_strdup (tmp);
2100 } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
2101 if (tmp)
2102 view_one_file = g_strdup (tmp);
2103 else {
2104 fputs ("No arguments given to the viewer\n", stderr);
2105 exit (1);
2107 } else {
2108 /* sets the current dir and the other dir */
2109 if (tmp) {
2110 this_dir = g_strdup (tmp);
2111 if ((tmp = poptGetArg (ctx)))
2112 other_dir = g_strdup (tmp);
2116 poptFreeContext (ctx);
2120 main (int argc, char *argv[])
2122 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2123 setlocale (LC_ALL, "");
2124 bindtextdomain ("mc", LOCALEDIR);
2125 textdomain ("mc");
2127 /* Set up temporary directory */
2128 mc_tmpdir ();
2130 OS_Setup ();
2132 /* This variable is used by the subshell */
2133 home_dir = getenv ("HOME");
2134 if (!home_dir) {
2135 /* mc_home was computed by OS_Setup */
2136 home_dir = mc_home;
2139 vfs_init ();
2141 #ifdef HAVE_SLANG
2142 SLtt_Ignore_Beep = 1;
2143 #endif
2145 handle_args (argc, argv);
2147 /* NOTE: This has to be called before slang_init or whatever routine
2148 calls any define_sequence */
2149 init_key ();
2151 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2152 handle_console (CONSOLE_INIT);
2154 #ifdef HAVE_SUBSHELL_SUPPORT
2155 /* Don't use subshell when invoked as viewer or editor */
2156 if (edit_one_file || view_one_file)
2157 use_subshell = 0;
2159 if (use_subshell)
2160 subshell_get_console_attributes ();
2161 #endif /* HAVE_SUBSHELL_SUPPORT */
2163 /* Install the SIGCHLD handler; must be done before init_subshell() */
2164 init_sigchld ();
2166 /* We need this, since ncurses endwin () doesn't restore the signals */
2167 save_stop_handler ();
2169 /* Must be done before init_subshell, to set up the terminal size: */
2170 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2171 #ifdef HAVE_SLANG
2172 slang_init ();
2173 #endif
2174 /* NOTE: This call has to be after slang_init. It's the small part from
2175 the previous init_key which had to be moved after the call of slang_init */
2176 init_key_input_fd ();
2178 load_setup ();
2180 init_curses ();
2182 init_xterm_support ();
2184 #ifdef HAVE_SUBSHELL_SUPPORT
2186 /* Done here to ensure that the subshell doesn't */
2187 /* inherit the file descriptors opened below, etc */
2188 if (use_subshell)
2189 init_subshell ();
2191 #endif /* HAVE_SUBSHELL_SUPPORT */
2193 /* Removing this from the X code let's us type C-c */
2194 load_key_defs ();
2196 /* Also done after init_subshell, to save any shell init file messages */
2197 if (console_flag)
2198 handle_console (CONSOLE_SAVE);
2200 if (alternate_plus_minus)
2201 application_keypad_mode ();
2203 #ifdef HAVE_SUBSHELL_SUPPORT
2204 if (use_subshell) {
2205 prompt = strip_ctrl_codes (subshell_prompt);
2206 if (!prompt)
2207 prompt = "";
2208 } else
2209 #endif /* HAVE_SUBSHELL_SUPPORT */
2210 prompt = (geteuid () == 0) ? "# " : "$ ";
2212 /* Program main loop */
2213 if (!midnight_shutdown)
2214 do_nc ();
2216 /* Save the tree store */
2217 tree_store_save ();
2219 /* Virtual File System shutdown */
2220 vfs_shut ();
2222 flush_extension_file (); /* does only free memory */
2224 endwin ();
2225 #ifdef HAVE_SLANG
2226 slang_shutdown ();
2227 #endif
2229 if (console_flag && !(quit & SUBSHELL_EXIT))
2230 handle_console (CONSOLE_RESTORE);
2231 if (alternate_plus_minus)
2232 numeric_keypad_mode ();
2234 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2236 if (console_flag)
2237 handle_console (CONSOLE_DONE);
2238 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2240 if (last_wd_file && last_wd_string && !print_last_revert
2241 && !edit_one_file && !view_one_file) {
2242 int last_wd_fd =
2243 open (last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2244 S_IRUSR | S_IWUSR);
2246 if (last_wd_fd != -1) {
2247 write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2248 close (last_wd_fd);
2251 g_free (last_wd_string);
2253 g_free (mc_home);
2254 done_key ();
2255 #ifdef HAVE_CHARSET
2256 free_codepages_list ();
2257 #endif
2258 g_free (this_dir);
2259 g_free (other_dir);
2261 return 0;