Final Indentation of all touched files
[midnight-commander.git] / src / main.c
blob7059b4cb9778fa3a7395e1c109a16bd1eeb83f01
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
6 1994, 1995 Janne Kukonlehto
7 1997 Norbert Warmuth
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file main.c
24 * \brief Source: this is a main module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pwd.h> /* for username in xterm title */
42 #include "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/tty/mouse.h"
46 #include "lib/tty/key.h" /* For init_key() */
47 #include "lib/tty/win.h" /* xterm_flag */
49 #include "lib/skin.h"
51 #include "lib/mcconfig.h"
52 #include "lib/filehighlight.h"
53 #include "lib/fileloc.h" /* MC_USERCONF_DIR */
55 #include "lib/vfs/mc-vfs/vfs.h" /* vfs_translate_url() */
57 #ifdef ENABLE_VFS_SMB
58 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debug() */
59 #endif /* ENABLE_VFS_SMB */
61 #ifdef ENABLE_VFS
62 #include "lib/vfs/mc-vfs/gc.h"
63 #endif
65 #include "lib/strutil.h"
67 #include "src/args.h"
69 #include "dir.h"
70 #include "dialog.h"
71 #include "menu.h"
72 #include "panel.h"
73 #include "option.h"
74 #include "tree.h"
75 #include "treestore.h"
76 #include "consaver/cons.saver.h"
77 #include "subshell.h"
78 #include "setup.h" /* save_setup() */
79 #include "boxes.h" /* sort_box() */
80 #include "layout.h"
81 #include "cmd.h" /* Normal commands */
82 #include "hotlist.h"
83 #include "panelize.h"
84 #include "learn.h" /* learn_keys() */
85 #include "listmode.h"
86 #include "execute.h"
87 #include "ext.h" /* For flush_extension_file() */
88 #include "widget.h"
89 #include "command.h"
90 #include "wtools.h"
91 #include "cmddef.h" /* CK_ cmd name const */
92 #include "user.h" /* user_file_menu_cmd() */
94 #include "chmod.h"
95 #include "chown.h"
96 #include "achown.h"
98 #include "main.h"
100 #ifdef USE_INTERNAL_EDIT
101 #include "src/editor/edit.h"
102 #endif
104 #ifdef USE_DIFF_VIEW
105 #include "src/diffviewer/ydiff.h"
106 #endif
108 #ifdef HAVE_CHARSET
109 #include "charsets.h"
110 #endif
113 #include "keybind.h" /* type global_keymap_t */
115 /* When the modes are active, left_panel, right_panel and tree_panel */
116 /* Point to a proper data structure. You should check with the functions */
117 /* get_current_type and get_other_type the types of the panels before using */
118 /* This pointer variables */
120 /* The structures for the panels */
121 WPanel *left_panel = NULL;
122 WPanel *right_panel = NULL;
124 mc_fhl_t *mc_filehighlight;
126 /* The pointer to the tree */
127 WTree *the_tree = NULL;
129 /* The Menubar */
130 struct WMenuBar *the_menubar = NULL;
132 /* Pointers to the selected and unselected panel */
133 WPanel *current_panel = NULL;
135 /* Set if the command is being run from the "Right" menu */
136 int is_right = 0;
138 /* Set when main loop should be terminated */
139 volatile int quit = 0;
141 /* Set if you want the possible completions dialog for the first time */
142 int show_all_if_ambiguous = 0;
144 /* Set when cd symlink following is desirable (bash mode) */
145 int cd_symlinks = 1;
147 /* If set then dialogs just clean the screen when refreshing, else */
148 /* they do a complete refresh, refreshing all the parts of the program */
149 int fast_refresh = 0;
151 /* If true, marking a files moves the cursor down */
152 int mark_moves_down = 1;
154 /* If true, at startup the user-menu is invoked */
155 int auto_menu = 0;
157 /* If true, then the +, - and \ keys have their special meaning only if the
158 * command line is emtpy, otherwise they behave like regular letters
160 int only_leading_plus_minus = 1;
162 int pause_after_run = pause_on_dumb_terminals;
164 /* It true saves the setup when quitting */
165 int auto_save_setup = 1;
167 #ifdef HAVE_CHARSET
169 * Don't restrict the output on the screen manager level,
170 * the translation tables take care of it.
172 #define full_eight_bits (1)
173 #define eight_bit_clean (1)
174 #else /* HAVE_CHARSET */
175 /* If true, allow characters in the range 160-255 */
176 int eight_bit_clean = 1;
178 * If true, also allow characters in the range 128-159.
179 * This is reported to break on many terminals (xterm, qansi-m).
181 int full_eight_bits = 0;
182 #endif /* !HAVE_CHARSET */
185 * If utf-8 terminal utf8_display = 1
186 * Display bits set UTF-8
189 int utf8_display = 0;
191 /* If true use the internal viewer */
192 int use_internal_view = 1;
194 /* Have we shown the fast-reload warning in the past? */
195 int fast_reload_w = 0;
197 /* Move page/item? When clicking on the top or bottom of a panel */
198 int mouse_move_pages = 1;
200 /* If true: l&r arrows are used to chdir if the input line is empty */
201 int navigate_with_arrows = 0;
203 /* The prompt */
204 const char *mc_prompt = NULL;
206 /* The widget where we draw the prompt */
207 WLabel *the_prompt;
209 /* The hint bar */
210 WLabel *the_hint;
212 /* The button bar */
213 WButtonBar *the_bar;
215 /* Mouse type: GPM, xterm or none */
216 Mouse_Type use_mouse_p = MOUSE_NONE;
218 /* If on, default for "No" in delete operations */
219 int safe_delete = 0;
221 /* Controls screen clearing before an exec */
222 int clear_before_exec = 1;
224 /* Asks for confirmation before deleting a file */
225 int confirm_delete = 1;
227 /* Asks for confirmation before deleting a hotlist entry */
228 int confirm_directory_hotlist_delete = 1;
230 /* Asks for confirmation before overwriting a file */
231 int confirm_overwrite = 1;
233 /* Asks for confirmation before executing a program by pressing enter */
234 int confirm_execute = 0;
236 /* Asks for confirmation before leaving the program */
237 int confirm_exit = 1;
239 /* Asks for confirmation before clean up of history */
240 int confirm_history_cleanup = 1;
242 /* Asks for confirmation when using F3 to view a directory and there
243 are tagged files */
244 int confirm_view_dir = 0;
246 /* This flag indicates if the pull down menus by default drop down */
247 int drop_menus = 0;
249 /* The dialog handle for the main program */
250 Dlg_head *midnight_dlg = NULL;
252 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
253 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
254 int update_prompt = 0;
256 /* The home directory */
257 const char *home_dir = NULL;
259 /* Tab size */
260 int option_tab_spacing = 8;
262 /* Only used at program boot */
263 int boot_current_is_left = 1;
265 /* If this is true, then when browsing the tree the other window will
266 * automatically reload it's directory with the contents of the currently
267 * selected directory.
269 int xtree_mode = 0;
271 /* If set, then print to the given file the last directory we were at */
272 static char *last_wd_string = NULL;
273 /* Set to 1 to suppress printing the last directory */
274 static int print_last_revert = 0;
276 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
277 char *mc_run_param0 = NULL;
278 char *mc_run_param1 = NULL;
280 /* Line to start the editor on */
281 static int edit_one_file_start_line = 0;
283 /* Used so that widgets know if they are being destroyed or
284 shut down */
285 int midnight_shutdown = 0;
287 /* The user's shell */
288 char *shell = NULL;
290 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
291 char *mc_home = NULL;
293 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
294 char *mc_home_alt = NULL;
296 /* Define this function for glib-style error handling */
297 GQuark
298 mc_main_error_quark (void)
300 return g_quark_from_static_string (PACKAGE);
303 #ifdef USE_INTERNAL_EDIT
304 GArray *editor_keymap = NULL;
305 GArray *editor_x_keymap = NULL;
306 #endif
307 GArray *viewer_keymap = NULL;
308 GArray *viewer_hex_keymap = NULL;
309 GArray *main_keymap = NULL;
310 GArray *main_x_keymap = NULL;
311 GArray *panel_keymap = NULL;
312 GArray *input_keymap = NULL;
313 GArray *tree_keymap = NULL;
314 GArray *help_keymap = NULL;
315 #ifdef USE_DIFF_VIEW
316 GArray *diff_keymap = NULL;
317 #endif
318 const global_keymap_t *main_map;
319 const global_keymap_t *main_x_map;
321 /* Save current stat of directories to avoid reloading the panels */
322 /* when no modifications have taken place */
323 void
324 save_cwds_stat (void)
326 if (fast_reload)
328 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
329 if (get_other_type () == view_listing)
330 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
334 #ifdef HAVE_SUBSHELL_SUPPORT
335 void
336 do_update_prompt (void)
338 if (update_prompt)
340 printf ("\r\n%s", subshell_prompt);
341 fflush (stdout);
342 update_prompt = 0;
345 #endif /* HAVE_SUBSHELL_SUPPORT */
347 void
348 change_panel (void)
350 free_completions (cmdline);
351 dlg_one_down (midnight_dlg);
354 /* Stop MC main dialog and the current dialog if it exists.
355 * Needed to provide fast exit from MC viewer or editor on shell exit */
356 static void
357 stop_dialogs (void)
359 midnight_dlg->running = 0;
360 if (current_dlg)
362 current_dlg->running = 0;
366 static int
367 quit_cmd_internal (int quiet)
369 int q = quit;
371 if (quiet || !confirm_exit)
373 q = 1;
375 else
377 if (query_dialog
378 (_(" The Midnight Commander "),
379 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
380 2, _("&Yes"), _("&No")) == 0)
381 q = 1;
383 if (q)
385 #ifdef HAVE_SUBSHELL_SUPPORT
386 if (!use_subshell)
387 stop_dialogs ();
388 else if ((q = exit_subshell ()))
389 #endif
390 stop_dialogs ();
392 if (q)
393 quit |= 1;
394 return quit;
397 static void
398 quit_cmd (void)
400 quit_cmd_internal (0);
403 void
404 quiet_quit_cmd (void)
406 print_last_revert = 1;
407 quit_cmd_internal (1);
410 /* Wrapper for do_subshell_chdir, check for availability of subshell */
411 void
412 subshell_chdir (const char *directory)
414 #ifdef HAVE_SUBSHELL_SUPPORT
415 if (use_subshell)
417 if (vfs_current_is_local ())
418 do_subshell_chdir (directory, 0, 1);
420 #endif /* HAVE_SUBSHELL_SUPPORT */
423 void
424 directory_history_add (struct WPanel *panel, const char *dir)
426 char *tmp;
428 tmp = g_strdup (dir);
429 strip_password (tmp, 1);
431 panel->dir_history = list_append_unique (panel->dir_history, tmp);
435 * If we moved to the parent directory move the selection pointer to
436 * the old directory name; If we leave VFS dir, remove FS specificator.
438 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
440 static const char *
441 get_parent_dir_name (const char *cwd, const char *lwd)
443 const char *p;
444 if (strlen (lwd) > strlen (cwd))
445 if ((p = strrchr (lwd, PATH_SEP)) && !strncmp (cwd, lwd, p - lwd) &&
446 ((gsize) strlen (cwd) == (gsize) p - (gsize) lwd || (p == lwd && cwd[0] == PATH_SEP &&
447 cwd[1] == '\0')))
449 return (p + 1);
451 return NULL;
455 * Changes the current directory of the panel.
456 * Don't record change in the directory history.
458 static int
459 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
461 const char *directory;
462 char *olddir;
463 char temp[MC_MAXPATHLEN];
464 char *translated_url;
466 if (cd_type == cd_parse_command)
468 while (*new_dir == ' ')
469 new_dir++;
472 olddir = g_strdup (panel->cwd);
473 new_dir = translated_url = vfs_translate_url (new_dir);
475 /* Convert *new_path to a suitable pathname, handle ~user */
477 if (cd_type == cd_parse_command)
479 if (!strcmp (new_dir, "-"))
481 strcpy (temp, panel->lwd);
482 new_dir = temp;
485 directory = *new_dir ? new_dir : home_dir;
487 if (mc_chdir (directory) == -1)
489 strcpy (panel->cwd, olddir);
490 g_free (olddir);
491 g_free (translated_url);
492 return 0;
494 g_free (translated_url);
496 /* Success: save previous directory, shutdown status of previous dir */
497 strcpy (panel->lwd, olddir);
498 free_completions (cmdline);
500 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
502 vfs_release_path (olddir);
504 subshell_chdir (panel->cwd);
506 /* Reload current panel */
507 panel_clean_dir (panel);
508 panel->count =
509 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
510 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
511 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
512 load_hint (0);
513 panel->dirty = 1;
514 update_xterm_title_path ();
516 g_free (olddir);
518 return 1;
522 * Changes the current directory of the panel.
523 * Record change in the directory history.
526 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
528 int r;
530 r = _do_panel_cd (panel, new_dir, cd_type);
531 if (r)
532 directory_history_add (panel, panel->cwd);
533 return r;
537 do_cd (const char *new_dir, enum cd_enum exact)
539 return (do_panel_cd (current_panel, new_dir, exact));
542 void
543 directory_history_next (WPanel * panel)
545 GList *nextdir;
547 nextdir = g_list_next (panel->dir_history);
549 if (!nextdir)
550 return;
552 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
553 panel->dir_history = nextdir;
556 void
557 directory_history_prev (WPanel * panel)
559 GList *prevdir;
561 prevdir = g_list_previous (panel->dir_history);
563 if (!prevdir)
564 return;
566 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
567 panel->dir_history = prevdir;
570 void
571 directory_history_list (WPanel * panel)
573 char *s;
575 s = show_hist (&panel->dir_history, &panel->widget);
577 if (s != NULL)
579 if (_do_panel_cd (panel, s, cd_exact))
580 directory_history_add (panel, panel->cwd);
581 else
582 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
583 g_free (s);
587 #ifdef HAVE_SUBSHELL_SUPPORT
589 load_prompt (int fd, void *unused)
591 (void) fd;
592 (void) unused;
594 if (!read_subshell_prompt ())
595 return 0;
597 /* Don't actually change the prompt if it's invisible */
598 if (current_dlg == midnight_dlg && command_prompt)
600 char *tmp_prompt;
601 int prompt_len;
603 tmp_prompt = strip_ctrl_codes (subshell_prompt);
604 prompt_len = str_term_width1 (tmp_prompt);
606 /* Check for prompts too big */
607 if (COLS > 8 && prompt_len > COLS - 8)
609 tmp_prompt[COLS - 8] = '\0';
610 prompt_len = COLS - 8;
612 mc_prompt = tmp_prompt;
613 label_set_text (the_prompt, mc_prompt);
614 winput_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
616 /* since the prompt has changed, and we are called from one of the
617 * tty_get_event channels, the prompt updating does not take place
618 * automatically: force a cursor update and a screen refresh
620 update_cursor (midnight_dlg);
621 mc_refresh ();
623 update_prompt = 1;
624 return 0;
626 #endif /* HAVE_SUBSHELL_SUPPORT */
628 void
629 sort_cmd (void)
631 WPanel *p;
632 const panel_field_t *sort_order;
634 if (!SELECTED_IS_PANEL)
635 return;
637 p = MENU_PANEL;
638 sort_order = sort_box (p->current_sort_field, &p->reverse, &p->case_sensitive, &p->exec_first);
640 panel_set_sort_order (p, sort_order);
644 static void
645 treebox_cmd (void)
647 char *sel_dir;
649 sel_dir = tree_box (selection (current_panel)->fname);
650 if (sel_dir)
652 do_cd (sel_dir, cd_exact);
653 g_free (sel_dir);
657 #ifdef LISTMODE_EDITOR
658 static void
659 listmode_cmd (void)
661 char *newmode;
663 if (get_current_type () != view_listing)
664 return;
666 newmode = listmode_edit (current_panel->user_format);
667 if (!newmode)
668 return;
670 g_free (current_panel->user_format);
671 current_panel->list_type = list_user;
672 current_panel->user_format = newmode;
673 set_panel_formats (current_panel);
675 do_refresh ();
677 #endif /* LISTMODE_EDITOR */
679 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
680 static GList *
681 create_panel_menu (void)
683 GList *entries = NULL;
685 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
686 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
687 entries = g_list_append (entries, menu_entry_create (_("&Info"), CK_InfoCmd));
688 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
689 entries = g_list_append (entries, menu_separator_create ());
690 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
691 entries = g_list_append (entries, menu_separator_create ());
692 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
693 #ifdef HAVE_CHARSET
694 entries = g_list_append (entries, menu_separator_create ());
695 entries =
696 g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
697 #endif
698 #ifdef USE_NETCODE
699 entries = g_list_append (entries, menu_separator_create ());
700 #ifdef ENABLE_VFS_MCFS
701 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
702 #endif
703 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
704 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
705 #ifdef ENABLE_VFS_SMB
706 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
707 #endif /* ENABLE_VFS_SMB */
708 #endif
709 entries = g_list_append (entries, menu_separator_create ());
710 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
712 return entries;
715 static GList *
716 create_file_menu (void)
718 GList *entries = NULL;
720 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
721 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
722 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
723 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
724 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
725 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
726 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
727 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
728 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
729 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
730 entries =
731 g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
732 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
733 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
734 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
735 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
736 entries = g_list_append (entries, menu_separator_create ());
737 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
738 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
739 entries =
740 g_list_append (entries,
741 menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
742 entries = g_list_append (entries, menu_separator_create ());
743 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
745 return entries;
748 static GList *
749 create_command_menu (void)
751 /* I know, I'm lazy, but the tree widget when it's not running
752 * as a panel still has some problems, I have not yet finished
753 * the WTree widget port, sorry.
755 GList *entries = NULL;
757 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
758 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
759 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
760 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
761 entries =
762 g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
763 entries =
764 g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
765 #ifdef USE_DIFF_VIEW
766 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
767 #endif
768 entries =
769 g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
770 entries =
771 g_list_append (entries,
772 menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
773 entries = g_list_append (entries, menu_separator_create ());
774 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
775 entries =
776 g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
777 #ifdef ENABLE_VFS
778 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
779 #endif
780 #ifdef WITH_BACKGROUND
781 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
782 #endif
783 entries = g_list_append (entries, menu_separator_create ());
784 #ifdef USE_EXT2FSLIB
785 entries =
786 g_list_append (entries,
787 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
788 #endif
789 #ifdef LISTMODE_EDITOR
790 entries =
791 g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
792 #endif
793 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
794 entries = g_list_append (entries, menu_separator_create ());
795 #endif
796 entries =
797 g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
798 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
799 entries =
800 g_list_append (entries,
801 menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
803 return entries;
806 static GList *
807 create_options_menu (void)
809 GList *entries = NULL;
811 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
812 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
813 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
814 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
815 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
816 #ifdef ENABLE_VFS
817 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
818 #endif
819 entries = g_list_append (entries, menu_separator_create ());
820 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
822 return entries;
825 void
826 init_menu (void)
828 menubar_add_menu (the_menubar,
829 create_menu (horizontal_split ? _("&Above") : _("&Left"),
830 create_panel_menu (), "[Left and Right Menus]"));
831 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
832 menubar_add_menu (the_menubar,
833 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
834 menubar_add_menu (the_menubar,
835 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
836 menubar_add_menu (the_menubar,
837 create_menu (horizontal_split ? _("&Below") : _("&Right"),
838 create_panel_menu (), "[Left and Right Menus]"));
841 void
842 done_menu (void)
844 menubar_set_menu (the_menubar, NULL);
847 static void
848 menu_last_selected_cmd (void)
850 the_menubar->is_active = TRUE;
851 the_menubar->is_dropped = (drop_menus != 0);
852 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
853 dlg_select_widget (the_menubar);
856 static void
857 menu_cmd (void)
859 if (the_menubar->is_active)
860 return;
862 if ((get_current_index () == 0) == (current_panel->active != 0))
863 the_menubar->selected = 0;
864 else
865 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
866 menu_last_selected_cmd ();
869 static char *
870 midnight_get_shortcut (unsigned long command)
872 const char *ext_map;
873 const char *shortcut = NULL;
875 shortcut = lookup_keymap_shortcut (main_map, command);
876 if (shortcut != NULL)
877 return g_strdup (shortcut);
879 shortcut = lookup_keymap_shortcut (panel_map, command);
880 if (shortcut != NULL)
881 return g_strdup (shortcut);
883 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
884 if (ext_map != NULL)
885 shortcut = lookup_keymap_shortcut (main_x_map, command);
886 if (shortcut != NULL)
887 return g_strdup_printf ("%s %s", ext_map, shortcut);
889 return NULL;
892 /* Flag toggling functions */
893 void
894 toggle_fast_reload (void)
896 fast_reload = !fast_reload;
897 if (fast_reload_w == 0 && fast_reload)
899 message (D_NORMAL, _(" Information "),
901 (" Using the fast reload option may not reflect the exact \n"
902 " directory contents. In this case you'll need to do a \n"
903 " manual reload of the directory. See the man page for \n"
904 " the details. "));
905 fast_reload_w = 1;
909 void
910 toggle_mix_all_files (void)
912 mix_all_files = !mix_all_files;
913 update_panels (UP_RELOAD, UP_KEEPSEL);
916 void
917 toggle_show_backup (void)
919 show_backups = !show_backups;
920 update_panels (UP_RELOAD, UP_KEEPSEL);
923 void
924 toggle_show_hidden (void)
926 show_dot_files = !show_dot_files;
927 update_panels (UP_RELOAD, UP_KEEPSEL);
930 static void
931 toggle_panels_split (void)
933 horizontal_split = !horizontal_split;
934 layout_change ();
935 do_refresh ();
938 void
939 toggle_kilobyte_si (void)
941 kilobyte_si = !kilobyte_si;
942 update_panels (UP_RELOAD, UP_KEEPSEL);
946 * Just a hack for allowing url-like pathnames to be accepted from the
947 * command line.
949 static void
950 translated_mc_chdir (char *dir)
952 char *newdir;
953 int ret;
955 newdir = vfs_translate_url (dir);
956 ret = mc_chdir (newdir);
957 g_free (newdir);
960 static void
961 create_panels (void)
963 int current_index;
964 int other_index;
965 panel_view_mode_t current_mode, other_mode;
966 char original_dir[BUF_1K] = "\0";
968 if (boot_current_is_left)
970 current_index = 0;
971 other_index = 1;
972 current_mode = startup_left_mode;
973 other_mode = startup_right_mode;
975 else
977 current_index = 1;
978 other_index = 0;
979 current_mode = startup_right_mode;
980 other_mode = startup_left_mode;
982 /* Creates the left panel */
983 if (mc_run_param0 != NULL)
985 if (mc_run_param1 != NULL)
987 /* Ok, user has specified two dirs, save the original one,
988 * since we may not be able to chdir to the proper
989 * second directory later
991 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
993 translated_mc_chdir (mc_run_param0);
995 set_display_type (current_index, current_mode);
997 /* The other panel */
998 if (mc_run_param1 != NULL)
1000 if (original_dir[0] != '\0')
1001 translated_mc_chdir (original_dir);
1002 translated_mc_chdir (mc_run_param1);
1004 set_display_type (other_index, other_mode);
1006 if (startup_left_mode == view_listing)
1008 current_panel = left_panel;
1010 else
1012 if (right_panel)
1013 current_panel = right_panel;
1014 else
1015 current_panel = left_panel;
1018 /* Create the nice widgets */
1019 cmdline = command_new (0, 0, 0);
1020 the_prompt = label_new (0, 0, mc_prompt);
1021 the_prompt->transparent = 1;
1022 the_bar = buttonbar_new (keybar_visible);
1024 the_hint = label_new (0, 0, 0);
1025 the_hint->transparent = 1;
1026 the_hint->auto_adjust_cols = 0;
1027 the_hint->widget.cols = COLS;
1029 the_menubar = menubar_new (0, 0, COLS, NULL);
1032 static void
1033 copy_current_pathname (void)
1035 char *cwd_path;
1036 if (!command_prompt)
1037 return;
1039 cwd_path = remove_encoding_from_path (current_panel->cwd);
1040 command_insert (cmdline, cwd_path, 0);
1042 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
1043 command_insert (cmdline, PATH_SEP_STR, 0);
1044 g_free (cwd_path);
1047 static void
1048 copy_other_pathname (void)
1050 char *cwd_path;
1052 if (get_other_type () != view_listing)
1053 return;
1055 if (!command_prompt)
1056 return;
1058 cwd_path = remove_encoding_from_path (other_panel->cwd);
1059 command_insert (cmdline, cwd_path, 0);
1061 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
1062 command_insert (cmdline, PATH_SEP_STR, 0);
1063 g_free (cwd_path);
1066 static void
1067 copy_readlink (WPanel * panel)
1069 if (!command_prompt)
1070 return;
1071 if (S_ISLNK (selection (panel)->st.st_mode))
1073 char buffer[MC_MAXPATHLEN];
1074 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1075 int i;
1077 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1078 g_free (p);
1079 if (i > 0)
1081 buffer[i] = 0;
1082 command_insert (cmdline, buffer, 1);
1087 static void
1088 copy_current_readlink (void)
1090 copy_readlink (current_panel);
1093 static void
1094 copy_other_readlink (void)
1096 if (get_other_type () == view_listing)
1097 copy_readlink (other_panel);
1100 /* Insert the selected file name into the input line */
1101 static void
1102 copy_prog_name (void)
1104 char *tmp;
1105 if (!command_prompt)
1106 return;
1108 if (get_current_type () == view_tree)
1110 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1111 tmp = tree_selected_name (tree);
1113 else
1114 tmp = selection (current_panel)->fname;
1116 command_insert (cmdline, tmp, 1);
1119 static void
1120 copy_tagged (WPanel * panel)
1122 int i;
1124 if (!command_prompt)
1125 return;
1126 input_disable_update (cmdline);
1127 if (panel->marked)
1129 for (i = 0; i < panel->count; i++)
1131 if (panel->dir.list[i].f.marked)
1132 command_insert (cmdline, panel->dir.list[i].fname, 1);
1135 else
1137 command_insert (cmdline, panel->dir.list[panel->selected].fname, 1);
1139 input_enable_update (cmdline);
1142 static void
1143 copy_current_tagged (void)
1145 copy_tagged (current_panel);
1148 static void
1149 copy_other_tagged (void)
1151 if (get_other_type () == view_listing)
1152 copy_tagged (other_panel);
1155 void
1156 midnight_set_buttonbar (WButtonBar * b)
1158 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1159 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1160 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1161 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1162 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1163 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1164 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1165 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1166 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1167 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1170 static gboolean ctl_x_map_enabled = FALSE;
1172 static void
1173 ctl_x_cmd (void)
1175 ctl_x_map_enabled = TRUE;
1178 static cb_ret_t
1179 midnight_execute_cmd (Widget * sender, unsigned long command)
1181 cb_ret_t res = MSG_HANDLED;
1183 (void) sender;
1185 switch (command)
1187 case CK_AddHotlist:
1188 add2hotlist_cmd ();
1189 break;
1190 case CK_ChmodCmd:
1191 chmod_cmd ();
1192 break;
1193 case CK_ChownCmd:
1194 chown_cmd ();
1195 break;
1196 case CK_ChownAdvancedCmd:
1197 chown_advanced_cmd ();
1198 break;
1199 case CK_CompareDirsCmd:
1200 compare_dirs_cmd ();
1201 break;
1202 case CK_ConfigureBox:
1203 configure_box ();
1204 break;
1205 #ifdef ENABLE_VFS
1206 case CK_ConfigureVfs:
1207 configure_vfs ();
1208 break;
1209 #endif
1210 case CK_ConfirmBox:
1211 confirm_box ();
1212 break;
1213 case CK_CopyCmd:
1214 copy_cmd ();
1215 break;
1216 case CK_CopyCurrentPathname:
1217 copy_current_pathname ();
1218 break;
1219 case CK_CopyCurrentReadlink:
1220 copy_current_readlink ();
1221 break;
1222 case CK_CopyCurrentTagged:
1223 copy_current_tagged ();
1224 break;
1225 case CK_CopyOtherPathname:
1226 copy_other_pathname ();
1227 break;
1228 case CK_CopyOtherReadlink:
1229 copy_other_readlink ();
1230 break;
1231 case CK_CopyOtherTagged:
1232 copy_other_tagged ();
1233 break;
1234 case CK_DeleteCmd:
1235 delete_cmd ();
1236 break;
1237 #ifdef USE_DIFF_VIEW
1238 case CK_DiffViewCmd:
1239 diff_view_cmd ();
1240 break;
1241 #endif
1242 case CK_DisplayBitsBox:
1243 display_bits_box ();
1244 break;
1245 case CK_EditCmd:
1246 edit_cmd ();
1247 break;
1248 case CK_EditExtFileCmd:
1249 ext_cmd ();
1250 break;
1251 case CK_EditFhlFileCmd:
1252 edit_fhl_cmd ();
1253 break;
1254 case CK_EditMcMenuCmd:
1255 edit_mc_menu_cmd ();
1256 break;
1257 case CK_EditSymlinkCmd:
1258 edit_symlink_cmd ();
1259 break;
1260 case CK_ExternalPanelize:
1261 external_panelize ();
1262 break;
1263 case CK_FilterCmd:
1264 filter_cmd ();
1265 break;
1266 case CK_FilteredViewCmd:
1267 filtered_view_cmd ();
1268 break;
1269 case CK_FindCmd:
1270 find_cmd ();
1271 break;
1272 #if defined (USE_NETCODE)
1273 case CK_FishlinkCmd:
1274 fishlink_cmd ();
1275 break;
1276 case CK_FtplinkCmd:
1277 ftplink_cmd ();
1278 break;
1279 #endif
1280 case CK_HelpCmd:
1281 help_cmd ();
1282 break;
1283 case CK_HistoryCmd:
1284 history_cmd ();
1285 break;
1286 case CK_InfoCmd:
1287 if (sender == (Widget *) the_menubar)
1288 info_cmd (); /* mwnu */
1289 else
1290 info_cmd_no_menu (); /* shortcut or buttonbar */
1291 break;
1292 #ifdef WITH_BACKGROUND
1293 case CK_JobsCmd:
1294 jobs_cmd ();
1295 break;
1296 #endif
1297 case CK_LayoutCmd:
1298 layout_cmd ();
1299 break;
1300 case CK_LearnKeys:
1301 learn_keys ();
1302 break;
1303 case CK_LinkCmd:
1304 link_cmd ();
1305 break;
1306 case CK_ListingCmd:
1307 listing_cmd ();
1308 break;
1309 #ifdef LISTMODE_EDITOR
1310 case CK_ListmodeCmd:
1311 listmode_cmd ();
1312 break;
1313 #endif
1314 case CK_MenuCmd:
1315 menu_cmd ();
1316 break;
1317 case CK_MenuLastSelectedCmd:
1318 menu_last_selected_cmd ();
1319 break;
1320 case CK_MkdirCmd:
1321 mkdir_cmd ();
1322 break;
1323 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1324 case CK_NetlinkCmd:
1325 netlink_cmd ();
1326 break;
1327 #endif
1328 #ifdef HAVE_CHARSET
1329 case CK_PanelSetPanelEncoding:
1330 encoding_cmd ();
1331 break;
1332 #endif
1333 case CK_QuickCdCmd:
1334 quick_cd_cmd ();
1335 break;
1336 case CK_QuickChdirCmd:
1337 quick_chdir_cmd ();
1338 break;
1339 case CK_QuickViewCmd:
1340 if (sender == (Widget *) the_menubar)
1341 quick_view_cmd (); /* menu */
1342 else
1343 quick_cmd_no_menu (); /* shortcut or buttonabr */
1344 break;
1345 case CK_QuietQuitCmd:
1346 quiet_quit_cmd ();
1347 break;
1348 case CK_QuitCmd:
1349 quit_cmd ();
1350 break;
1351 case CK_RenameCmd:
1352 rename_cmd ();
1353 break;
1354 case CK_RereadCmd:
1355 reread_cmd ();
1356 break;
1357 #ifdef ENABLE_VFS
1358 case CK_ReselectVfs:
1359 reselect_vfs ();
1360 break;
1361 #endif
1362 case CK_ReverseSelectionCmd:
1363 reverse_selection_cmd ();
1364 break;
1365 case CK_SaveSetupCmd:
1366 save_setup_cmd ();
1367 break;
1368 case CK_SelectCmd:
1369 select_cmd ();
1370 break;
1371 case CK_ShowCommandLine:
1372 view_other_cmd ();
1373 break;
1374 case CK_SingleDirsizeCmd:
1375 smart_dirsize_cmd ();
1376 break;
1377 #if defined (USE_NETCODE) && defined (ENABLE_VFS_SMB)
1378 case CK_SmblinkCmd:
1379 smblink_cmd ();
1380 break;
1381 #endif /* USE_NETCODE && ENABLE_VFS_SMB */
1382 case CK_Sort:
1383 sort_cmd ();
1384 break;
1385 case CK_StartExtMap1:
1386 ctl_x_cmd ();
1387 break;
1388 case CK_SuspendCmd:
1389 suspend_cmd ();
1390 break;
1391 case CK_SwapCmd:
1392 swap_cmd ();
1393 break;
1394 case CK_SymlinkCmd:
1395 symlink_cmd ();
1396 break;
1397 case CK_ToggleListingCmd:
1398 toggle_listing_cmd ();
1399 break;
1400 case CK_ToggleShowHidden:
1401 toggle_show_hidden ();
1402 break;
1403 case CK_TogglePanelsSplit:
1404 toggle_panels_split ();
1405 break;
1406 case CK_TreeCmd:
1407 tree_cmd ();
1408 break;
1409 case CK_TreeBoxCmd:
1410 treebox_cmd ();
1411 break;
1412 #ifdef USE_EXT2FSLIB
1413 case CK_UndeleteCmd:
1414 undelete_cmd ();
1415 break;
1416 #endif
1417 case CK_UnselectCmd:
1418 unselect_cmd ();
1419 break;
1420 case CK_UserMenuCmd:
1421 user_file_menu_cmd ();
1422 break;
1423 case CK_ViewCmd:
1424 view_cmd ();
1425 break;
1426 case CK_ViewFileCmd:
1427 view_file_cmd ();
1428 break;
1429 default:
1430 res = MSG_NOT_HANDLED;
1433 return res;
1436 static void
1437 init_xterm_support (void)
1439 const char *termvalue;
1441 termvalue = getenv ("TERM");
1442 if (!termvalue || !(*termvalue))
1444 fputs (_("The TERM environment variable is unset!\n"), stderr);
1445 exit (1);
1448 /* Check mouse capabilities */
1449 xmouse_seq = tty_tgetstr ("Km");
1451 if (strcmp (termvalue, "cygwin") == 0)
1453 mc_args__force_xterm = 1;
1454 use_mouse_p = MOUSE_DISABLED;
1457 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1458 || strncmp (termvalue, "konsole", 7) == 0
1459 || strncmp (termvalue, "rxvt", 4) == 0
1460 || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0)
1462 xterm_flag = 1;
1464 /* Default to the standard xterm sequence */
1465 if (!xmouse_seq)
1467 xmouse_seq = ESC_STR "[M";
1470 /* Enable mouse unless explicitly disabled by --nomouse */
1471 if (use_mouse_p != MOUSE_DISABLED)
1473 const char *color_term = getenv ("COLORTERM");
1474 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1475 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1476 strcmp (termvalue, "Eterm") == 0)
1478 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1480 else
1482 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1488 static void
1489 setup_mc (void)
1491 #ifdef HAVE_SLANG
1492 tty_display_8bit (full_eight_bits != 0);
1493 #else
1494 tty_display_8bit (eight_bit_clean != 0);
1495 #endif
1497 #ifdef HAVE_SUBSHELL_SUPPORT
1498 if (use_subshell)
1499 add_select_channel (subshell_pty, load_prompt, 0);
1500 #endif /* !HAVE_SUBSHELL_SUPPORT */
1502 tty_setup_sigwinch (sigwinch_handler);
1504 if ((tty_baudrate () < 9600) || tty_is_slow ())
1505 verbose = 0;
1507 init_xterm_support ();
1508 init_mouse ();
1511 static void
1512 setup_dummy_mc (void)
1514 char d[MC_MAXPATHLEN];
1515 int ret;
1517 mc_get_current_wd (d, MC_MAXPATHLEN);
1518 setup_mc ();
1519 ret = mc_chdir (d);
1522 static void
1523 check_codeset ()
1525 const char *current_system_codepage = NULL;
1527 current_system_codepage = str_detect_termencoding ();
1529 #ifdef HAVE_CHARSET
1531 const char *_display_codepage;
1533 _display_codepage = get_codepage_id (display_codepage);
1535 if (!strcmp (_display_codepage, current_system_codepage))
1537 utf8_display = str_isutf8 (current_system_codepage);
1538 return;
1541 display_codepage = get_codepage_index (current_system_codepage);
1542 if (display_codepage == -1)
1544 display_codepage = 0;
1547 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
1549 #endif
1550 utf8_display = str_isutf8 (current_system_codepage);
1553 static void
1554 done_screen (void)
1556 if (!(quit & SUBSHELL_EXIT))
1557 clr_scr ();
1558 tty_reset_shell_mode ();
1559 tty_noraw_mode ();
1560 tty_keypad (FALSE);
1561 tty_colors_done ();
1564 static void
1565 done_mc (void)
1567 disable_mouse ();
1569 /* Setup shutdown
1571 * We sync the profiles since the hotlist may have changed, while
1572 * we only change the setup data if we have the auto save feature set
1575 if (auto_save_setup)
1576 save_setup (); /* does also call save_hotlist */
1577 else
1579 save_hotlist ();
1580 save_panel_types ();
1582 done_screen ();
1583 vfs_add_current_stamps ();
1586 /* This should be called after destroy_dlg since panel widgets
1587 * save their state on the profiles
1589 static void
1590 done_mc_profile (void)
1592 done_setup ();
1595 static cb_ret_t
1596 midnight_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1598 unsigned long command;
1600 switch (msg)
1602 case DLG_INIT:
1603 setup_panels ();
1604 return MSG_HANDLED;
1606 case DLG_DRAW:
1607 load_hint (1);
1608 /* We handle the special case of the output lines */
1609 if (console_flag && output_lines)
1610 show_console_contents (output_start_y,
1611 LINES - output_lines - keybar_visible -
1612 1, LINES - keybar_visible - 1);
1613 return MSG_HANDLED;
1615 case DLG_RESIZE:
1616 setup_panels ();
1617 menubar_arrange (the_menubar);
1618 return MSG_HANDLED;
1620 case DLG_IDLE:
1621 /* We only need the first idle event to show user menu after start */
1622 set_idle_proc (h, 0);
1624 if (boot_current_is_left)
1625 dlg_select_widget (get_panel_widget (0));
1626 else
1627 dlg_select_widget (get_panel_widget (1));
1629 if (auto_menu)
1630 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1631 return MSG_HANDLED;
1633 case DLG_KEY:
1634 if (ctl_x_map_enabled)
1636 ctl_x_map_enabled = FALSE;
1637 command = lookup_keymap_command (main_x_map, parm);
1638 if (command != CK_Ignore_Key)
1639 return midnight_execute_cmd (NULL, command);
1642 /* FIXME: should handle all menu shortcuts before this point */
1643 if (the_menubar->is_active)
1644 return MSG_NOT_HANDLED;
1646 if (parm == '\t')
1647 free_completions (cmdline);
1649 if (parm == '\n')
1651 size_t i;
1653 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1654 cmdline->buffer[i] == '\t'); i++)
1656 if (cmdline->buffer[i])
1658 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1659 return MSG_HANDLED;
1661 stuff (cmdline, "", 0);
1662 cmdline->point = 0;
1665 /* Ctrl-Enter and Alt-Enter */
1666 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1668 copy_prog_name ();
1669 return MSG_HANDLED;
1672 /* Ctrl-Shift-Enter */
1673 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1675 copy_current_pathname ();
1676 copy_prog_name ();
1677 return MSG_HANDLED;
1680 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1681 && !quote && !current_panel->searching)
1683 if (!only_leading_plus_minus)
1685 /* Special treatement, since the input line will eat them */
1686 if (parm == '+')
1688 select_cmd ();
1689 return MSG_HANDLED;
1692 if (parm == '\\' || parm == '-')
1694 unselect_cmd ();
1695 return MSG_HANDLED;
1698 if (parm == '*')
1700 reverse_selection_cmd ();
1701 return MSG_HANDLED;
1704 else if (!command_prompt || !cmdline->buffer[0])
1706 /* Special treatement '+', '-', '\', '*' only when this is
1707 * first char on input line
1710 if (parm == '+')
1712 select_cmd ();
1713 return MSG_HANDLED;
1716 if (parm == '\\' || parm == '-')
1718 unselect_cmd ();
1719 return MSG_HANDLED;
1722 if (parm == '*')
1724 reverse_selection_cmd ();
1725 return MSG_HANDLED;
1729 return MSG_NOT_HANDLED;
1731 case DLG_HOTKEY_HANDLED:
1732 if ((get_current_type () == view_listing) && current_panel->searching)
1734 current_panel->searching = 0;
1735 current_panel->dirty = 1;
1737 return MSG_HANDLED;
1739 case DLG_UNHANDLED_KEY:
1740 if (command_prompt)
1742 cb_ret_t v;
1744 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1745 if (v == MSG_HANDLED)
1746 return MSG_HANDLED;
1749 if (ctl_x_map_enabled)
1751 ctl_x_map_enabled = FALSE;
1752 command = lookup_keymap_command (main_x_map, parm);
1754 else
1755 command = lookup_keymap_command (main_map, parm);
1757 return (command == CK_Ignore_Key) ? MSG_NOT_HANDLED : midnight_execute_cmd (NULL, command);
1759 case DLG_POST_KEY:
1760 if (!the_menubar->is_active)
1761 update_dirty_panels ();
1762 return MSG_HANDLED;
1764 case DLG_ACTION:
1765 /* shortcut */
1766 if (sender == NULL)
1767 midnight_execute_cmd (NULL, parm);
1768 /* message from menu */
1769 else if (sender == (Widget *) the_menubar)
1770 midnight_execute_cmd (sender, parm);
1771 /* message from buttonbar */
1772 else if (sender == (Widget *) the_bar)
1774 if (data == NULL)
1775 midnight_execute_cmd (sender, parm);
1776 else
1777 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1779 return MSG_HANDLED;
1781 default:
1782 return default_dlg_callback (h, sender, msg, parm, data);
1786 /* Show current directory in the xterm title */
1787 void
1788 update_xterm_title_path (void)
1790 const char *path;
1791 char host[BUF_TINY];
1792 char *p;
1793 struct passwd *pw = NULL;
1794 char *login = NULL;
1795 int res = 0;
1796 if (xterm_flag && xterm_title)
1798 path = strip_home_and_password (current_panel->cwd);
1799 res = gethostname (host, sizeof (host));
1800 if (res)
1801 { /* On success, res = 0 */
1802 host[0] = '\0';
1804 else
1806 host[sizeof (host) - 1] = '\0';
1808 pw = getpwuid (getuid ());
1809 if (pw)
1811 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1813 else
1815 login = g_strdup (host);
1817 p = g_strdup_printf ("mc [%s]:%s", login, path);
1818 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1819 g_free (login);
1820 g_free (p);
1821 if (!alternate_plus_minus)
1822 numeric_keypad_mode ();
1823 fflush (stdout);
1828 * Load new hint and display it.
1829 * IF force is not 0, ignore the timeout.
1831 void
1832 load_hint (int force)
1834 char *hint;
1836 if (!the_hint->widget.parent)
1837 return;
1839 if (!message_visible)
1841 label_set_text (the_hint, NULL);
1842 return;
1845 hint = get_random_hint (force);
1847 if (hint != NULL)
1849 if (*hint)
1850 set_hintbar (hint);
1851 g_free (hint);
1853 else
1855 char text[BUF_SMALL];
1857 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1858 set_hintbar (text);
1862 static void
1863 create_panels_and_run_mc (void)
1865 midnight_dlg->get_shortcut = midnight_get_shortcut;
1867 create_panels ();
1869 add_widget (midnight_dlg, the_menubar);
1870 init_menu ();
1872 add_widget (midnight_dlg, get_panel_widget (0));
1873 add_widget (midnight_dlg, get_panel_widget (1));
1875 add_widget (midnight_dlg, the_hint);
1876 add_widget (midnight_dlg, cmdline);
1877 add_widget (midnight_dlg, the_prompt);
1879 add_widget (midnight_dlg, the_bar);
1880 midnight_set_buttonbar (the_bar);
1882 /* Run the Midnight Commander if no file was specified in the command line */
1883 run_dlg (midnight_dlg);
1886 /* result must be free'd (I think this should go in util.c) */
1887 static char *
1888 prepend_cwd_on_local (const char *filename)
1890 char *d;
1891 size_t l;
1893 if (!vfs_file_is_local (filename) || g_path_is_absolute (filename))
1894 return g_strdup (filename);
1896 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1897 mc_get_current_wd (d, MC_MAXPATHLEN);
1898 l = strlen (d);
1899 d[l++] = PATH_SEP;
1900 strcpy (d + l, filename);
1901 canonicalize_pathname (d);
1902 return d;
1905 /* Invoke the internal view/edit routine with:
1906 * the default processing and forcing the internal viewer/editor
1908 static void
1909 mc_maybe_editor_or_viewer (void)
1911 switch (mc_run_mode)
1913 #ifdef USE_INTERNAL_EDIT
1914 case MC_RUN_EDITOR:
1915 edit_file (mc_run_param0, edit_one_file_start_line);
1916 break;
1917 #endif /* USE_INTERNAL_EDIT */
1918 case MC_RUN_VIEWER:
1920 char *path;
1921 path = prepend_cwd_on_local (mc_run_param0);
1922 view_file (path, 0, 1);
1923 g_free (path);
1924 break;
1926 #ifdef USE_DIFF_VIEW
1927 case MC_RUN_DIFFVIEWER:
1928 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1929 break;
1930 #endif /* USE_DIFF_VIEW */
1931 default:
1932 break;
1934 midnight_shutdown = 1;
1935 done_mc ();
1938 /* Run the main dialog that occupies the whole screen */
1939 static void
1940 do_nc (void)
1942 int midnight_colors[DLG_COLOR_NUM];
1943 midnight_colors[0] = mc_skin_color_get ("dialog", "_default_");
1944 midnight_colors[1] = mc_skin_color_get ("dialog", "focus");
1945 midnight_colors[2] = mc_skin_color_get ("dialog", "hotnormal");
1946 midnight_colors[3] = mc_skin_color_get ("dialog", "hotfocus");
1948 panel_init ();
1950 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1951 "[main]", NULL, DLG_WANT_IDLE);
1953 if (mc_run_mode == MC_RUN_FULL)
1954 setup_mc ();
1955 else
1956 setup_dummy_mc ();
1958 /* start check display_codepage and source_codepage */
1959 check_codeset ();
1961 main_map = default_main_map;
1962 if (main_keymap && main_keymap->len > 0)
1963 main_map = (global_keymap_t *) main_keymap->data;
1965 main_x_map = default_main_x_map;
1966 if (main_x_keymap && main_x_keymap->len > 0)
1967 main_x_map = (global_keymap_t *) main_x_keymap->data;
1969 panel_map = default_panel_keymap;
1970 if (panel_keymap && panel_keymap->len > 0)
1971 panel_map = (global_keymap_t *) panel_keymap->data;
1973 input_map = default_input_keymap;
1974 if (input_keymap && input_keymap->len > 0)
1975 input_map = (global_keymap_t *) input_keymap->data;
1977 tree_map = default_tree_keymap;
1978 if (tree_keymap && tree_keymap->len > 0)
1979 tree_map = (global_keymap_t *) tree_keymap->data;
1981 help_map = default_help_keymap;
1982 if (help_keymap && help_keymap->len > 0)
1983 help_map = (global_keymap_t *) help_keymap->data;
1985 #ifdef USE_DIFF_VIEW
1986 diff_map = default_diff_keymap;
1987 if (diff_keymap && diff_keymap->len > 0)
1988 diff_map = (global_keymap_t *) diff_keymap->data;
1989 #endif
1991 /* Check if we were invoked as an editor or file viewer */
1992 if (mc_run_mode != MC_RUN_FULL)
1993 mc_maybe_editor_or_viewer ();
1994 else
1996 create_panels_and_run_mc ();
1998 /* Program end */
1999 midnight_shutdown = 1;
2001 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
2002 if (mc_args__last_wd_file && vfs_current_is_local ())
2003 last_wd_string = g_strdup (current_panel->cwd);
2005 done_mc ();
2008 destroy_dlg (midnight_dlg);
2009 panel_deinit ();
2010 current_panel = 0;
2011 done_mc_profile ();
2014 /* POSIX version. The only version we support. */
2015 static void
2016 OS_Setup (void)
2018 const char *shell_env = getenv ("SHELL");
2019 const char *mc_libdir;
2021 if ((shell_env == NULL) || (shell_env[0] == '\0'))
2023 struct passwd *pwd;
2024 pwd = getpwuid (geteuid ());
2025 if (pwd != NULL)
2026 shell = g_strdup (pwd->pw_shell);
2028 else
2029 shell = g_strdup (shell_env);
2031 if ((shell == NULL) || (shell[0] == '\0'))
2033 g_free (shell);
2034 shell = g_strdup ("/bin/sh");
2037 /* This is the directory, where MC was installed, on Unix this is DATADIR */
2038 /* and can be overriden by the MC_DATADIR environment variable */
2039 mc_libdir = getenv ("MC_DATADIR");
2040 if (mc_libdir != NULL)
2042 mc_home = g_strdup (mc_libdir);
2043 mc_home_alt = g_strdup (SYSCONFDIR);
2045 else
2047 mc_home = g_strdup (SYSCONFDIR);
2048 mc_home_alt = g_strdup (DATADIR);
2051 /* This variable is used by the subshell */
2052 home_dir = getenv ("HOME");
2054 if (!home_dir)
2055 home_dir = mc_home;
2058 static void
2059 sigchld_handler_no_subshell (int sig)
2061 #ifdef __linux__
2062 int pid, status;
2064 if (!console_flag)
2065 return;
2067 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2068 the value of console_flag never changed, we could simply not install
2069 this handler at all if (!console_flag && !use_subshell). */
2071 /* That comment is no longer true. We need to wait() on a sigchld
2072 handler (that's at least what the tarfs code expects currently). */
2074 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2076 if (pid == cons_saver_pid)
2079 if (WIFSTOPPED (status))
2081 /* Someone has stopped cons.saver - restart it */
2082 kill (pid, SIGCONT);
2084 else
2086 /* cons.saver has died - disable console saving */
2087 handle_console (CONSOLE_DONE);
2088 console_flag = 0;
2091 /* If we got here, some other child exited; ignore it */
2092 #endif /* __linux__ */
2094 (void) sig;
2097 static void
2098 init_sigchld (void)
2100 struct sigaction sigchld_action;
2102 sigchld_action.sa_handler =
2103 #ifdef HAVE_SUBSHELL_SUPPORT
2104 use_subshell ? sigchld_handler :
2105 #endif /* HAVE_SUBSHELL_SUPPORT */
2106 sigchld_handler_no_subshell;
2108 sigemptyset (&sigchld_action.sa_mask);
2110 #ifdef SA_RESTART
2111 sigchld_action.sa_flags = SA_RESTART;
2112 #else
2113 sigchld_action.sa_flags = 0;
2114 #endif /* !SA_RESTART */
2116 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
2118 #ifdef HAVE_SUBSHELL_SUPPORT
2120 * This may happen on QNX Neutrino 6, where SA_RESTART
2121 * is defined but not implemented. Fallback to no subshell.
2123 use_subshell = 0;
2124 #endif /* HAVE_SUBSHELL_SUPPORT */
2128 static void
2129 mc_main__setup_by_args (int argc, char *argv[])
2131 const char *base;
2132 char *tmp;
2134 if (mc_args__nomouse)
2135 use_mouse_p = MOUSE_DISABLED;
2137 #ifdef USE_NETCODE
2138 if (mc_args__netfs_logfile != NULL)
2140 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
2141 #ifdef ENABLE_VFS_SMB
2142 smbfs_set_debugf (mc_args__netfs_logfile);
2143 #endif /* ENABLE_VFS_SMB */
2146 #ifdef ENABLE_VFS_SMB
2147 if (mc_args__debug_level != 0)
2148 smbfs_set_debug (mc_args__debug_level);
2149 #endif /* ENABLE_VFS_SMB */
2150 #endif /* USE_NETCODE */
2152 base = x_basename (argv[0]);
2153 tmp = (argc > 0) ? argv[1] : NULL;
2155 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi"))
2157 mc_run_param0 = g_strdup ("");
2158 if (tmp != NULL)
2161 * Check for filename:lineno, followed by an optional colon.
2162 * This format is used by many programs (especially compilers)
2163 * in error messages and warnings. It is supported so that
2164 * users can quickly copy and paste file locations.
2166 char *end = tmp + strlen (tmp), *p = end;
2167 if (p > tmp && p[-1] == ':')
2168 p--;
2169 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2170 p--;
2171 if (tmp < p && p < end && p[-1] == ':')
2173 struct stat st;
2174 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2176 * Check that the file before the colon actually exists.
2177 * If it doesn't exist, revert to the old behavior.
2179 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1)
2181 mc_run_param0 = fname;
2182 edit_one_file_start_line = atoi (p);
2184 else
2186 g_free (fname);
2187 goto try_plus_filename;
2190 else
2192 try_plus_filename:
2193 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
2195 int start_line = atoi (tmp);
2196 if (start_line > 0)
2198 char *file = (argc > 1) ? argv[2] : NULL;
2199 if (file)
2201 tmp = file;
2202 edit_one_file_start_line = start_line;
2206 mc_run_param0 = g_strdup (tmp);
2209 mc_run_mode = MC_RUN_EDITOR;
2211 else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view"))
2213 if (tmp != NULL)
2214 mc_run_param0 = g_strdup (tmp);
2215 else
2217 fputs ("No arguments given to the viewer\n", stderr);
2218 exit (1);
2220 mc_run_mode = MC_RUN_VIEWER;
2221 #ifdef USE_DIFF_VIEW
2223 else if (!STRNCOMP (base, "mcd", 3) || !STRCOMP (base, "diff"))
2225 if (argc < 3)
2227 fputs ("There 2 files are required to diffviewer\n", stderr);
2228 exit (1);
2230 else if (tmp != NULL)
2232 mc_run_param0 = g_strdup (tmp);
2233 tmp = (argc > 1) ? argv[2] : NULL;
2234 if (tmp != NULL)
2235 mc_run_param1 = g_strdup (tmp);
2236 mc_run_mode = MC_RUN_DIFFVIEWER;
2238 #endif /* USE_DIFF_VIEW */
2240 else
2242 /* sets the current dir and the other dir */
2243 if (tmp != NULL)
2245 mc_run_param0 = g_strdup (tmp);
2246 tmp = (argc > 1) ? argv[2] : NULL;
2247 if (tmp != NULL)
2248 mc_run_param1 = g_strdup (tmp);
2250 mc_run_mode = MC_RUN_FULL;
2255 main (int argc, char *argv[])
2257 struct stat s;
2258 char *mc_dir;
2259 GError *error = NULL;
2260 gboolean isInitialized;
2262 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2263 setlocale (LC_ALL, "");
2264 bindtextdomain ("mc", LOCALEDIR);
2265 textdomain ("mc");
2267 /* Set up temporary directory */
2268 mc_tmpdir ();
2270 OS_Setup ();
2272 str_init_strings (NULL);
2274 vfs_init ();
2276 #ifdef USE_INTERNAL_EDIT
2277 edit_stack_init ();
2278 #endif
2280 #ifdef HAVE_SLANG
2281 SLtt_Ignore_Beep = 1;
2282 #endif
2284 if (!mc_args_handle (&argc, &argv, "mc"))
2285 return 1;
2287 mc_main__setup_by_args (argc, argv);
2289 /* NOTE: This has to be called before tty_init or whatever routine
2290 calls any define_sequence */
2291 init_key ();
2293 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2294 handle_console (CONSOLE_INIT);
2296 #ifdef HAVE_SUBSHELL_SUPPORT
2297 /* Don't use subshell when invoked as viewer or editor */
2298 if (mc_run_mode != MC_RUN_FULL)
2299 use_subshell = 0;
2301 if (use_subshell)
2302 subshell_get_console_attributes ();
2303 #endif /* HAVE_SUBSHELL_SUPPORT */
2305 /* Install the SIGCHLD handler; must be done before init_subshell() */
2306 init_sigchld ();
2308 /* We need this, since ncurses endwin () doesn't restore the signals */
2309 save_stop_handler ();
2311 /* Must be done before init_subshell, to set up the terminal size: */
2312 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2313 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2315 load_setup ();
2317 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2319 isInitialized = mc_skin_init (&error);
2321 mc_filehighlight = mc_fhl_new (TRUE);
2323 dlg_set_default_colors ();
2325 if (!isInitialized)
2327 message (D_ERROR, _("Warning"), "%s", error->message);
2328 g_error_free (error);
2329 error = NULL;
2332 /* create home directory */
2333 /* do it after the screen library initialization to show the error message */
2334 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2335 canonicalize_pathname (mc_dir);
2336 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
2337 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
2338 g_free (mc_dir);
2340 #ifdef HAVE_SUBSHELL_SUPPORT
2341 /* Done here to ensure that the subshell doesn't */
2342 /* inherit the file descriptors opened below, etc */
2343 if (use_subshell)
2344 init_subshell ();
2346 #endif /* HAVE_SUBSHELL_SUPPORT */
2348 /* Removing this from the X code let's us type C-c */
2349 load_key_defs ();
2351 load_keymap_defs ();
2353 /* Also done after init_subshell, to save any shell init file messages */
2354 if (console_flag)
2355 handle_console (CONSOLE_SAVE);
2357 if (alternate_plus_minus)
2358 application_keypad_mode ();
2360 #ifdef HAVE_SUBSHELL_SUPPORT
2361 if (use_subshell)
2363 mc_prompt = strip_ctrl_codes (subshell_prompt);
2364 if (mc_prompt == NULL)
2365 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2367 else
2368 #endif /* HAVE_SUBSHELL_SUPPORT */
2369 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2371 /* Program main loop */
2372 if (!midnight_shutdown)
2373 do_nc ();
2375 /* Save the tree store */
2376 tree_store_save ();
2378 free_keymap_defs ();
2380 /* Virtual File System shutdown */
2381 vfs_shut ();
2383 flush_extension_file (); /* does only free memory */
2385 mc_fhl_free (&mc_filehighlight);
2386 mc_skin_deinit ();
2388 tty_shutdown ();
2390 if (console_flag && !(quit & SUBSHELL_EXIT))
2391 handle_console (CONSOLE_RESTORE);
2392 if (alternate_plus_minus)
2393 numeric_keypad_mode ();
2395 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2397 if (console_flag)
2398 handle_console (CONSOLE_DONE);
2399 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2401 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string && !print_last_revert)
2403 int last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2404 S_IRUSR | S_IWUSR);
2406 if (last_wd_fd != -1)
2408 ssize_t ret1;
2409 int ret2;
2410 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2411 ret2 = close (last_wd_fd);
2414 g_free (last_wd_string);
2416 g_free (mc_home_alt);
2417 g_free (mc_home);
2418 g_free (shell);
2420 done_key ();
2421 #ifdef HAVE_CHARSET
2422 free_codepages_list ();
2423 g_free (autodetect_codeset);
2424 #endif
2425 str_uninit_strings ();
2427 g_free (mc_run_param0);
2428 g_free (mc_run_param1);
2430 #ifdef USE_INTERNAL_EDIT
2431 edit_stack_free ();
2432 #endif
2434 return 0;