Ticket #2097: ChangeLog in its current form does not strictly make any sense
[midnight-commander.git] / src / main.c
blobba24bdb95743487a12a28beae144cedf0e4bad6f
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 size_t llen, clen;
445 llen = strlen (lwd);
446 clen = strlen (cwd);
448 if (llen > clen)
450 const char *p;
452 p = strrchr (lwd, PATH_SEP);
454 if ((p != NULL)
455 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
456 && (clen == (size_t) (p - lwd)
457 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
458 return (p + 1);
461 return NULL;
465 * Changes the current directory of the panel.
466 * Don't record change in the directory history.
468 static int
469 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
471 const char *directory;
472 char *olddir;
473 char temp[MC_MAXPATHLEN];
474 char *translated_url;
476 if (cd_type == cd_parse_command)
478 while (*new_dir == ' ')
479 new_dir++;
482 olddir = g_strdup (panel->cwd);
483 new_dir = translated_url = vfs_translate_url (new_dir);
485 /* Convert *new_path to a suitable pathname, handle ~user */
487 if (cd_type == cd_parse_command)
489 if (!strcmp (new_dir, "-"))
491 strcpy (temp, panel->lwd);
492 new_dir = temp;
495 directory = *new_dir ? new_dir : home_dir;
497 if (mc_chdir (directory) == -1)
499 strcpy (panel->cwd, olddir);
500 g_free (olddir);
501 g_free (translated_url);
502 return 0;
504 g_free (translated_url);
506 /* Success: save previous directory, shutdown status of previous dir */
507 strcpy (panel->lwd, olddir);
508 free_completions (cmdline);
510 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
512 vfs_release_path (olddir);
514 subshell_chdir (panel->cwd);
516 /* Reload current panel */
517 panel_clean_dir (panel);
518 panel->count =
519 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
520 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
521 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
522 load_hint (0);
523 panel->dirty = 1;
524 update_xterm_title_path ();
526 g_free (olddir);
528 return 1;
532 * Changes the current directory of the panel.
533 * Record change in the directory history.
536 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
538 int r;
540 r = _do_panel_cd (panel, new_dir, cd_type);
541 if (r)
542 directory_history_add (panel, panel->cwd);
543 return r;
547 do_cd (const char *new_dir, enum cd_enum exact)
549 return (do_panel_cd (current_panel, new_dir, exact));
552 void
553 directory_history_next (WPanel * panel)
555 GList *nextdir;
557 nextdir = g_list_next (panel->dir_history);
559 if (!nextdir)
560 return;
562 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
563 panel->dir_history = nextdir;
566 void
567 directory_history_prev (WPanel * panel)
569 GList *prevdir;
571 prevdir = g_list_previous (panel->dir_history);
573 if (!prevdir)
574 return;
576 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
577 panel->dir_history = prevdir;
580 void
581 directory_history_list (WPanel * panel)
583 char *s;
585 s = show_hist (&panel->dir_history, &panel->widget);
587 if (s != NULL)
589 if (_do_panel_cd (panel, s, cd_exact))
590 directory_history_add (panel, panel->cwd);
591 else
592 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
593 g_free (s);
597 #ifdef HAVE_SUBSHELL_SUPPORT
599 load_prompt (int fd, void *unused)
601 (void) fd;
602 (void) unused;
604 if (!read_subshell_prompt ())
605 return 0;
607 /* Don't actually change the prompt if it's invisible */
608 if (current_dlg == midnight_dlg && command_prompt)
610 char *tmp_prompt;
611 int prompt_len;
613 tmp_prompt = strip_ctrl_codes (subshell_prompt);
614 prompt_len = str_term_width1 (tmp_prompt);
616 /* Check for prompts too big */
617 if (COLS > 8 && prompt_len > COLS - 8)
619 tmp_prompt[COLS - 8] = '\0';
620 prompt_len = COLS - 8;
622 mc_prompt = tmp_prompt;
623 label_set_text (the_prompt, mc_prompt);
624 winput_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
626 /* since the prompt has changed, and we are called from one of the
627 * tty_get_event channels, the prompt updating does not take place
628 * automatically: force a cursor update and a screen refresh
630 update_cursor (midnight_dlg);
631 mc_refresh ();
633 update_prompt = 1;
634 return 0;
636 #endif /* HAVE_SUBSHELL_SUPPORT */
638 void
639 sort_cmd (void)
641 WPanel *p;
642 const panel_field_t *sort_order;
644 if (!SELECTED_IS_PANEL)
645 return;
647 p = MENU_PANEL;
648 sort_order = sort_box (p->current_sort_field, &p->reverse, &p->case_sensitive, &p->exec_first);
650 panel_set_sort_order (p, sort_order);
654 static void
655 treebox_cmd (void)
657 char *sel_dir;
659 sel_dir = tree_box (selection (current_panel)->fname);
660 if (sel_dir)
662 do_cd (sel_dir, cd_exact);
663 g_free (sel_dir);
667 #ifdef LISTMODE_EDITOR
668 static void
669 listmode_cmd (void)
671 char *newmode;
673 if (get_current_type () != view_listing)
674 return;
676 newmode = listmode_edit (current_panel->user_format);
677 if (!newmode)
678 return;
680 g_free (current_panel->user_format);
681 current_panel->list_type = list_user;
682 current_panel->user_format = newmode;
683 set_panel_formats (current_panel);
685 do_refresh ();
687 #endif /* LISTMODE_EDITOR */
689 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
690 static GList *
691 create_panel_menu (void)
693 GList *entries = NULL;
695 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
696 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
697 entries = g_list_append (entries, menu_entry_create (_("&Info"), CK_InfoCmd));
698 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
699 entries = g_list_append (entries, menu_separator_create ());
700 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
701 entries = g_list_append (entries, menu_separator_create ());
702 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
703 #ifdef HAVE_CHARSET
704 entries = g_list_append (entries, menu_separator_create ());
705 entries =
706 g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
707 #endif
708 #ifdef USE_NETCODE
709 entries = g_list_append (entries, menu_separator_create ());
710 #ifdef ENABLE_VFS_MCFS
711 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
712 #endif
713 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
714 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
715 #ifdef ENABLE_VFS_SMB
716 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
717 #endif /* ENABLE_VFS_SMB */
718 #endif
719 entries = g_list_append (entries, menu_separator_create ());
720 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
722 return entries;
725 static GList *
726 create_file_menu (void)
728 GList *entries = NULL;
730 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
731 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
732 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
733 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
734 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
735 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
736 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
737 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
738 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
739 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
740 entries =
741 g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
742 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
743 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
744 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
745 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
746 entries = g_list_append (entries, menu_separator_create ());
747 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
748 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
749 entries =
750 g_list_append (entries,
751 menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
752 entries = g_list_append (entries, menu_separator_create ());
753 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
755 return entries;
758 static GList *
759 create_command_menu (void)
761 /* I know, I'm lazy, but the tree widget when it's not running
762 * as a panel still has some problems, I have not yet finished
763 * the WTree widget port, sorry.
765 GList *entries = NULL;
767 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
768 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
769 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
770 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
771 entries =
772 g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
773 entries =
774 g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
775 #ifdef USE_DIFF_VIEW
776 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
777 #endif
778 entries =
779 g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
780 entries =
781 g_list_append (entries,
782 menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
783 entries = g_list_append (entries, menu_separator_create ());
784 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
785 entries =
786 g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
787 #ifdef ENABLE_VFS
788 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
789 #endif
790 #ifdef WITH_BACKGROUND
791 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
792 #endif
793 entries = g_list_append (entries, menu_separator_create ());
794 #ifdef USE_EXT2FSLIB
795 entries =
796 g_list_append (entries,
797 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
798 #endif
799 #ifdef LISTMODE_EDITOR
800 entries =
801 g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
802 #endif
803 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
804 entries = g_list_append (entries, menu_separator_create ());
805 #endif
806 entries =
807 g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
808 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
809 entries =
810 g_list_append (entries,
811 menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
813 return entries;
816 static GList *
817 create_options_menu (void)
819 GList *entries = NULL;
821 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
822 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
823 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
824 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
825 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
826 #ifdef ENABLE_VFS
827 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
828 #endif
829 entries = g_list_append (entries, menu_separator_create ());
830 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
832 return entries;
835 void
836 init_menu (void)
838 menubar_add_menu (the_menubar,
839 create_menu (horizontal_split ? _("&Above") : _("&Left"),
840 create_panel_menu (), "[Left and Right Menus]"));
841 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
842 menubar_add_menu (the_menubar,
843 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
844 menubar_add_menu (the_menubar,
845 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
846 menubar_add_menu (the_menubar,
847 create_menu (horizontal_split ? _("&Below") : _("&Right"),
848 create_panel_menu (), "[Left and Right Menus]"));
851 void
852 done_menu (void)
854 menubar_set_menu (the_menubar, NULL);
857 static void
858 menu_last_selected_cmd (void)
860 the_menubar->is_active = TRUE;
861 the_menubar->is_dropped = (drop_menus != 0);
862 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
863 dlg_select_widget (the_menubar);
866 static void
867 menu_cmd (void)
869 if (the_menubar->is_active)
870 return;
872 if ((get_current_index () == 0) == (current_panel->active != 0))
873 the_menubar->selected = 0;
874 else
875 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
876 menu_last_selected_cmd ();
879 static char *
880 midnight_get_shortcut (unsigned long command)
882 const char *ext_map;
883 const char *shortcut = NULL;
885 shortcut = lookup_keymap_shortcut (main_map, command);
886 if (shortcut != NULL)
887 return g_strdup (shortcut);
889 shortcut = lookup_keymap_shortcut (panel_map, command);
890 if (shortcut != NULL)
891 return g_strdup (shortcut);
893 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
894 if (ext_map != NULL)
895 shortcut = lookup_keymap_shortcut (main_x_map, command);
896 if (shortcut != NULL)
897 return g_strdup_printf ("%s %s", ext_map, shortcut);
899 return NULL;
902 /* Flag toggling functions */
903 void
904 toggle_fast_reload (void)
906 fast_reload = !fast_reload;
907 if (fast_reload_w == 0 && fast_reload)
909 message (D_NORMAL, _(" Information "),
911 (" Using the fast reload option may not reflect the exact \n"
912 " directory contents. In this case you'll need to do a \n"
913 " manual reload of the directory. See the man page for \n"
914 " the details. "));
915 fast_reload_w = 1;
919 void
920 toggle_mix_all_files (void)
922 mix_all_files = !mix_all_files;
923 update_panels (UP_RELOAD, UP_KEEPSEL);
926 void
927 toggle_show_backup (void)
929 show_backups = !show_backups;
930 update_panels (UP_RELOAD, UP_KEEPSEL);
933 void
934 toggle_show_hidden (void)
936 show_dot_files = !show_dot_files;
937 update_panels (UP_RELOAD, UP_KEEPSEL);
940 static void
941 toggle_panels_split (void)
943 horizontal_split = !horizontal_split;
944 layout_change ();
945 do_refresh ();
948 void
949 toggle_kilobyte_si (void)
951 kilobyte_si = !kilobyte_si;
952 update_panels (UP_RELOAD, UP_KEEPSEL);
956 * Just a hack for allowing url-like pathnames to be accepted from the
957 * command line.
959 static void
960 translated_mc_chdir (char *dir)
962 char *newdir;
963 int ret;
965 newdir = vfs_translate_url (dir);
966 ret = mc_chdir (newdir);
967 g_free (newdir);
970 static void
971 create_panels (void)
973 int current_index;
974 int other_index;
975 panel_view_mode_t current_mode, other_mode;
976 char original_dir[BUF_1K] = "\0";
978 if (boot_current_is_left)
980 current_index = 0;
981 other_index = 1;
982 current_mode = startup_left_mode;
983 other_mode = startup_right_mode;
985 else
987 current_index = 1;
988 other_index = 0;
989 current_mode = startup_right_mode;
990 other_mode = startup_left_mode;
992 /* Creates the left panel */
993 if (mc_run_param0 != NULL)
995 if (mc_run_param1 != NULL)
997 /* Ok, user has specified two dirs, save the original one,
998 * since we may not be able to chdir to the proper
999 * second directory later
1001 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
1003 translated_mc_chdir (mc_run_param0);
1005 set_display_type (current_index, current_mode);
1007 /* The other panel */
1008 if (mc_run_param1 != NULL)
1010 if (original_dir[0] != '\0')
1011 translated_mc_chdir (original_dir);
1012 translated_mc_chdir (mc_run_param1);
1014 set_display_type (other_index, other_mode);
1016 if (startup_left_mode == view_listing)
1018 current_panel = left_panel;
1020 else
1022 if (right_panel)
1023 current_panel = right_panel;
1024 else
1025 current_panel = left_panel;
1028 /* Create the nice widgets */
1029 cmdline = command_new (0, 0, 0);
1030 the_prompt = label_new (0, 0, mc_prompt);
1031 the_prompt->transparent = 1;
1032 the_bar = buttonbar_new (keybar_visible);
1034 the_hint = label_new (0, 0, 0);
1035 the_hint->transparent = 1;
1036 the_hint->auto_adjust_cols = 0;
1037 the_hint->widget.cols = COLS;
1039 the_menubar = menubar_new (0, 0, COLS, NULL);
1042 static void
1043 copy_current_pathname (void)
1045 char *cwd_path;
1046 if (!command_prompt)
1047 return;
1049 cwd_path = remove_encoding_from_path (current_panel->cwd);
1050 command_insert (cmdline, cwd_path, 0);
1052 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
1053 command_insert (cmdline, PATH_SEP_STR, 0);
1054 g_free (cwd_path);
1057 static void
1058 copy_other_pathname (void)
1060 char *cwd_path;
1062 if (get_other_type () != view_listing)
1063 return;
1065 if (!command_prompt)
1066 return;
1068 cwd_path = remove_encoding_from_path (other_panel->cwd);
1069 command_insert (cmdline, cwd_path, 0);
1071 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
1072 command_insert (cmdline, PATH_SEP_STR, 0);
1073 g_free (cwd_path);
1076 static void
1077 copy_readlink (WPanel * panel)
1079 if (!command_prompt)
1080 return;
1081 if (S_ISLNK (selection (panel)->st.st_mode))
1083 char buffer[MC_MAXPATHLEN];
1084 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1085 int i;
1087 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1088 g_free (p);
1089 if (i > 0)
1091 buffer[i] = 0;
1092 command_insert (cmdline, buffer, 1);
1097 static void
1098 copy_current_readlink (void)
1100 copy_readlink (current_panel);
1103 static void
1104 copy_other_readlink (void)
1106 if (get_other_type () == view_listing)
1107 copy_readlink (other_panel);
1110 /* Insert the selected file name into the input line */
1111 static void
1112 copy_prog_name (void)
1114 char *tmp;
1115 if (!command_prompt)
1116 return;
1118 if (get_current_type () == view_tree)
1120 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1121 tmp = tree_selected_name (tree);
1123 else
1124 tmp = selection (current_panel)->fname;
1126 command_insert (cmdline, tmp, 1);
1129 static void
1130 copy_tagged (WPanel * panel)
1132 int i;
1134 if (!command_prompt)
1135 return;
1136 input_disable_update (cmdline);
1137 if (panel->marked)
1139 for (i = 0; i < panel->count; i++)
1141 if (panel->dir.list[i].f.marked)
1142 command_insert (cmdline, panel->dir.list[i].fname, 1);
1145 else
1147 command_insert (cmdline, panel->dir.list[panel->selected].fname, 1);
1149 input_enable_update (cmdline);
1152 static void
1153 copy_current_tagged (void)
1155 copy_tagged (current_panel);
1158 static void
1159 copy_other_tagged (void)
1161 if (get_other_type () == view_listing)
1162 copy_tagged (other_panel);
1165 void
1166 midnight_set_buttonbar (WButtonBar * b)
1168 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1169 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1170 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1171 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1172 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1173 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1174 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1175 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1176 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1177 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1180 static gboolean ctl_x_map_enabled = FALSE;
1182 static void
1183 ctl_x_cmd (void)
1185 ctl_x_map_enabled = TRUE;
1188 static cb_ret_t
1189 midnight_execute_cmd (Widget * sender, unsigned long command)
1191 cb_ret_t res = MSG_HANDLED;
1193 (void) sender;
1195 switch (command)
1197 case CK_AddHotlist:
1198 add2hotlist_cmd ();
1199 break;
1200 case CK_ChmodCmd:
1201 chmod_cmd ();
1202 break;
1203 case CK_ChownCmd:
1204 chown_cmd ();
1205 break;
1206 case CK_ChownAdvancedCmd:
1207 chown_advanced_cmd ();
1208 break;
1209 case CK_CompareDirsCmd:
1210 compare_dirs_cmd ();
1211 break;
1212 case CK_ConfigureBox:
1213 configure_box ();
1214 break;
1215 #ifdef ENABLE_VFS
1216 case CK_ConfigureVfs:
1217 configure_vfs ();
1218 break;
1219 #endif
1220 case CK_ConfirmBox:
1221 confirm_box ();
1222 break;
1223 case CK_CopyCmd:
1224 copy_cmd ();
1225 break;
1226 case CK_CopyCurrentPathname:
1227 copy_current_pathname ();
1228 break;
1229 case CK_CopyCurrentReadlink:
1230 copy_current_readlink ();
1231 break;
1232 case CK_CopyCurrentTagged:
1233 copy_current_tagged ();
1234 break;
1235 case CK_CopyOtherPathname:
1236 copy_other_pathname ();
1237 break;
1238 case CK_CopyOtherReadlink:
1239 copy_other_readlink ();
1240 break;
1241 case CK_CopyOtherTagged:
1242 copy_other_tagged ();
1243 break;
1244 case CK_DeleteCmd:
1245 delete_cmd ();
1246 break;
1247 #ifdef USE_DIFF_VIEW
1248 case CK_DiffViewCmd:
1249 diff_view_cmd ();
1250 break;
1251 #endif
1252 case CK_DisplayBitsBox:
1253 display_bits_box ();
1254 break;
1255 case CK_EditCmd:
1256 edit_cmd ();
1257 break;
1258 case CK_EditExtFileCmd:
1259 ext_cmd ();
1260 break;
1261 case CK_EditFhlFileCmd:
1262 edit_fhl_cmd ();
1263 break;
1264 case CK_EditMcMenuCmd:
1265 edit_mc_menu_cmd ();
1266 break;
1267 case CK_EditSymlinkCmd:
1268 edit_symlink_cmd ();
1269 break;
1270 case CK_ExternalPanelize:
1271 external_panelize ();
1272 break;
1273 case CK_FilterCmd:
1274 filter_cmd ();
1275 break;
1276 case CK_FilteredViewCmd:
1277 filtered_view_cmd ();
1278 break;
1279 case CK_FindCmd:
1280 find_cmd ();
1281 break;
1282 #if defined (USE_NETCODE)
1283 case CK_FishlinkCmd:
1284 fishlink_cmd ();
1285 break;
1286 case CK_FtplinkCmd:
1287 ftplink_cmd ();
1288 break;
1289 #endif
1290 case CK_HelpCmd:
1291 help_cmd ();
1292 break;
1293 case CK_HistoryCmd:
1294 history_cmd ();
1295 break;
1296 case CK_InfoCmd:
1297 if (sender == (Widget *) the_menubar)
1298 info_cmd (); /* mwnu */
1299 else
1300 info_cmd_no_menu (); /* shortcut or buttonbar */
1301 break;
1302 #ifdef WITH_BACKGROUND
1303 case CK_JobsCmd:
1304 jobs_cmd ();
1305 break;
1306 #endif
1307 case CK_LayoutCmd:
1308 layout_cmd ();
1309 break;
1310 case CK_LearnKeys:
1311 learn_keys ();
1312 break;
1313 case CK_LinkCmd:
1314 link_cmd ();
1315 break;
1316 case CK_ListingCmd:
1317 listing_cmd ();
1318 break;
1319 #ifdef LISTMODE_EDITOR
1320 case CK_ListmodeCmd:
1321 listmode_cmd ();
1322 break;
1323 #endif
1324 case CK_MenuCmd:
1325 menu_cmd ();
1326 break;
1327 case CK_MenuLastSelectedCmd:
1328 menu_last_selected_cmd ();
1329 break;
1330 case CK_MkdirCmd:
1331 mkdir_cmd ();
1332 break;
1333 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1334 case CK_NetlinkCmd:
1335 netlink_cmd ();
1336 break;
1337 #endif
1338 #ifdef HAVE_CHARSET
1339 case CK_PanelSetPanelEncoding:
1340 encoding_cmd ();
1341 break;
1342 #endif
1343 case CK_QuickCdCmd:
1344 quick_cd_cmd ();
1345 break;
1346 case CK_QuickChdirCmd:
1347 quick_chdir_cmd ();
1348 break;
1349 case CK_QuickViewCmd:
1350 if (sender == (Widget *) the_menubar)
1351 quick_view_cmd (); /* menu */
1352 else
1353 quick_cmd_no_menu (); /* shortcut or buttonabr */
1354 break;
1355 case CK_QuietQuitCmd:
1356 quiet_quit_cmd ();
1357 break;
1358 case CK_QuitCmd:
1359 quit_cmd ();
1360 break;
1361 case CK_RenameCmd:
1362 rename_cmd ();
1363 break;
1364 case CK_RereadCmd:
1365 reread_cmd ();
1366 break;
1367 #ifdef ENABLE_VFS
1368 case CK_ReselectVfs:
1369 reselect_vfs ();
1370 break;
1371 #endif
1372 case CK_ReverseSelectionCmd:
1373 reverse_selection_cmd ();
1374 break;
1375 case CK_SaveSetupCmd:
1376 save_setup_cmd ();
1377 break;
1378 case CK_SelectCmd:
1379 select_cmd ();
1380 break;
1381 case CK_ShowCommandLine:
1382 view_other_cmd ();
1383 break;
1384 case CK_SingleDirsizeCmd:
1385 smart_dirsize_cmd ();
1386 break;
1387 #if defined (USE_NETCODE) && defined (ENABLE_VFS_SMB)
1388 case CK_SmblinkCmd:
1389 smblink_cmd ();
1390 break;
1391 #endif /* USE_NETCODE && ENABLE_VFS_SMB */
1392 case CK_Sort:
1393 sort_cmd ();
1394 break;
1395 case CK_StartExtMap1:
1396 ctl_x_cmd ();
1397 break;
1398 case CK_SuspendCmd:
1399 suspend_cmd ();
1400 break;
1401 case CK_SwapCmd:
1402 swap_cmd ();
1403 break;
1404 case CK_SymlinkCmd:
1405 symlink_cmd ();
1406 break;
1407 case CK_ToggleListingCmd:
1408 toggle_listing_cmd ();
1409 break;
1410 case CK_ToggleShowHidden:
1411 toggle_show_hidden ();
1412 break;
1413 case CK_TogglePanelsSplit:
1414 toggle_panels_split ();
1415 break;
1416 case CK_TreeCmd:
1417 tree_cmd ();
1418 break;
1419 case CK_TreeBoxCmd:
1420 treebox_cmd ();
1421 break;
1422 #ifdef USE_EXT2FSLIB
1423 case CK_UndeleteCmd:
1424 undelete_cmd ();
1425 break;
1426 #endif
1427 case CK_UnselectCmd:
1428 unselect_cmd ();
1429 break;
1430 case CK_UserMenuCmd:
1431 user_file_menu_cmd ();
1432 break;
1433 case CK_ViewCmd:
1434 view_cmd ();
1435 break;
1436 case CK_ViewFileCmd:
1437 view_file_cmd ();
1438 break;
1439 default:
1440 res = MSG_NOT_HANDLED;
1443 return res;
1446 static void
1447 init_xterm_support (void)
1449 const char *termvalue;
1451 termvalue = getenv ("TERM");
1452 if (!termvalue || !(*termvalue))
1454 fputs (_("The TERM environment variable is unset!\n"), stderr);
1455 exit (EXIT_FAILURE);
1458 /* Check mouse capabilities */
1459 xmouse_seq = tty_tgetstr ("Km");
1461 if (strcmp (termvalue, "cygwin") == 0)
1463 mc_args__force_xterm = 1;
1464 use_mouse_p = MOUSE_DISABLED;
1467 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1468 || strncmp (termvalue, "konsole", 7) == 0
1469 || strncmp (termvalue, "rxvt", 4) == 0
1470 || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0)
1472 xterm_flag = 1;
1474 /* Default to the standard xterm sequence */
1475 if (!xmouse_seq)
1477 xmouse_seq = ESC_STR "[M";
1480 /* Enable mouse unless explicitly disabled by --nomouse */
1481 if (use_mouse_p != MOUSE_DISABLED)
1483 const char *color_term = getenv ("COLORTERM");
1484 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1485 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1486 strcmp (termvalue, "Eterm") == 0)
1488 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1490 else
1492 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1498 static void
1499 setup_mc (void)
1501 #ifdef HAVE_SLANG
1502 tty_display_8bit (full_eight_bits != 0);
1503 #else
1504 tty_display_8bit (eight_bit_clean != 0);
1505 #endif
1507 #ifdef HAVE_SUBSHELL_SUPPORT
1508 if (use_subshell)
1509 add_select_channel (subshell_pty, load_prompt, 0);
1510 #endif /* !HAVE_SUBSHELL_SUPPORT */
1512 tty_setup_sigwinch (sigwinch_handler);
1514 if ((tty_baudrate () < 9600) || tty_is_slow ())
1515 verbose = 0;
1517 init_xterm_support ();
1518 init_mouse ();
1521 static void
1522 setup_dummy_mc (void)
1524 char d[MC_MAXPATHLEN];
1525 int ret;
1527 mc_get_current_wd (d, MC_MAXPATHLEN);
1528 setup_mc ();
1529 ret = mc_chdir (d);
1532 static void
1533 check_codeset ()
1535 const char *current_system_codepage = NULL;
1537 current_system_codepage = str_detect_termencoding ();
1539 #ifdef HAVE_CHARSET
1541 const char *_display_codepage;
1543 _display_codepage = get_codepage_id (display_codepage);
1545 if (!strcmp (_display_codepage, current_system_codepage))
1547 utf8_display = str_isutf8 (current_system_codepage);
1548 return;
1551 display_codepage = get_codepage_index (current_system_codepage);
1552 if (display_codepage == -1)
1554 display_codepage = 0;
1557 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
1559 #endif
1560 utf8_display = str_isutf8 (current_system_codepage);
1563 static void
1564 done_screen (void)
1566 if (!(quit & SUBSHELL_EXIT))
1567 clr_scr ();
1568 tty_reset_shell_mode ();
1569 tty_noraw_mode ();
1570 tty_keypad (FALSE);
1571 tty_colors_done ();
1574 static void
1575 done_mc (void)
1577 disable_mouse ();
1579 /* Setup shutdown
1581 * We sync the profiles since the hotlist may have changed, while
1582 * we only change the setup data if we have the auto save feature set
1585 if (auto_save_setup)
1586 save_setup (); /* does also call save_hotlist */
1587 else
1589 save_hotlist ();
1590 save_panel_types ();
1592 done_screen ();
1593 vfs_add_current_stamps ();
1596 /* This should be called after destroy_dlg since panel widgets
1597 * save their state on the profiles
1599 static void
1600 done_mc_profile (void)
1602 done_setup ();
1605 static cb_ret_t
1606 midnight_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1608 unsigned long command;
1610 switch (msg)
1612 case DLG_INIT:
1613 setup_panels ();
1614 return MSG_HANDLED;
1616 case DLG_DRAW:
1617 load_hint (1);
1618 /* We handle the special case of the output lines */
1619 if (console_flag && output_lines)
1620 show_console_contents (output_start_y,
1621 LINES - output_lines - keybar_visible -
1622 1, LINES - keybar_visible - 1);
1623 return MSG_HANDLED;
1625 case DLG_RESIZE:
1626 setup_panels ();
1627 menubar_arrange (the_menubar);
1628 return MSG_HANDLED;
1630 case DLG_IDLE:
1631 /* We only need the first idle event to show user menu after start */
1632 set_idle_proc (h, 0);
1634 if (boot_current_is_left)
1635 dlg_select_widget (get_panel_widget (0));
1636 else
1637 dlg_select_widget (get_panel_widget (1));
1639 if (auto_menu)
1640 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1641 return MSG_HANDLED;
1643 case DLG_KEY:
1644 if (ctl_x_map_enabled)
1646 ctl_x_map_enabled = FALSE;
1647 command = lookup_keymap_command (main_x_map, parm);
1648 if (command != CK_Ignore_Key)
1649 return midnight_execute_cmd (NULL, command);
1652 /* FIXME: should handle all menu shortcuts before this point */
1653 if (the_menubar->is_active)
1654 return MSG_NOT_HANDLED;
1656 if (parm == '\t')
1657 free_completions (cmdline);
1659 if (parm == '\n')
1661 size_t i;
1663 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1664 cmdline->buffer[i] == '\t'); i++)
1666 if (cmdline->buffer[i])
1668 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1669 return MSG_HANDLED;
1671 stuff (cmdline, "", 0);
1672 cmdline->point = 0;
1675 /* Ctrl-Enter and Alt-Enter */
1676 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1678 copy_prog_name ();
1679 return MSG_HANDLED;
1682 /* Ctrl-Shift-Enter */
1683 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1685 copy_current_pathname ();
1686 copy_prog_name ();
1687 return MSG_HANDLED;
1690 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1691 && !quote && !current_panel->searching)
1693 if (!only_leading_plus_minus)
1695 /* Special treatement, since the input line will eat them */
1696 if (parm == '+')
1698 select_cmd ();
1699 return MSG_HANDLED;
1702 if (parm == '\\' || parm == '-')
1704 unselect_cmd ();
1705 return MSG_HANDLED;
1708 if (parm == '*')
1710 reverse_selection_cmd ();
1711 return MSG_HANDLED;
1714 else if (!command_prompt || !cmdline->buffer[0])
1716 /* Special treatement '+', '-', '\', '*' only when this is
1717 * first char on input line
1720 if (parm == '+')
1722 select_cmd ();
1723 return MSG_HANDLED;
1726 if (parm == '\\' || parm == '-')
1728 unselect_cmd ();
1729 return MSG_HANDLED;
1732 if (parm == '*')
1734 reverse_selection_cmd ();
1735 return MSG_HANDLED;
1739 return MSG_NOT_HANDLED;
1741 case DLG_HOTKEY_HANDLED:
1742 if ((get_current_type () == view_listing) && current_panel->searching)
1744 current_panel->searching = 0;
1745 current_panel->dirty = 1;
1747 return MSG_HANDLED;
1749 case DLG_UNHANDLED_KEY:
1750 if (command_prompt)
1752 cb_ret_t v;
1754 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1755 if (v == MSG_HANDLED)
1756 return MSG_HANDLED;
1759 if (ctl_x_map_enabled)
1761 ctl_x_map_enabled = FALSE;
1762 command = lookup_keymap_command (main_x_map, parm);
1764 else
1765 command = lookup_keymap_command (main_map, parm);
1767 return (command == CK_Ignore_Key) ? MSG_NOT_HANDLED : midnight_execute_cmd (NULL, command);
1769 case DLG_POST_KEY:
1770 if (!the_menubar->is_active)
1771 update_dirty_panels ();
1772 return MSG_HANDLED;
1774 case DLG_ACTION:
1775 /* shortcut */
1776 if (sender == NULL)
1777 midnight_execute_cmd (NULL, parm);
1778 /* message from menu */
1779 else if (sender == (Widget *) the_menubar)
1780 midnight_execute_cmd (sender, parm);
1781 /* message from buttonbar */
1782 else if (sender == (Widget *) the_bar)
1784 if (data == NULL)
1785 midnight_execute_cmd (sender, parm);
1786 else
1787 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1789 return MSG_HANDLED;
1791 default:
1792 return default_dlg_callback (h, sender, msg, parm, data);
1796 /* Show current directory in the xterm title */
1797 void
1798 update_xterm_title_path (void)
1800 const char *path;
1801 char host[BUF_TINY];
1802 char *p;
1803 struct passwd *pw = NULL;
1804 char *login = NULL;
1805 int res = 0;
1806 if (xterm_flag && xterm_title)
1808 path = strip_home_and_password (current_panel->cwd);
1809 res = gethostname (host, sizeof (host));
1810 if (res)
1811 { /* On success, res = 0 */
1812 host[0] = '\0';
1814 else
1816 host[sizeof (host) - 1] = '\0';
1818 pw = getpwuid (getuid ());
1819 if (pw)
1821 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1823 else
1825 login = g_strdup (host);
1827 p = g_strdup_printf ("mc [%s]:%s", login, path);
1828 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1829 g_free (login);
1830 g_free (p);
1831 if (!alternate_plus_minus)
1832 numeric_keypad_mode ();
1833 fflush (stdout);
1838 * Load new hint and display it.
1839 * IF force is not 0, ignore the timeout.
1841 void
1842 load_hint (int force)
1844 char *hint;
1846 if (!the_hint->widget.parent)
1847 return;
1849 if (!message_visible)
1851 label_set_text (the_hint, NULL);
1852 return;
1855 hint = get_random_hint (force);
1857 if (hint != NULL)
1859 if (*hint)
1860 set_hintbar (hint);
1861 g_free (hint);
1863 else
1865 char text[BUF_SMALL];
1867 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1868 set_hintbar (text);
1872 static void
1873 create_panels_and_run_mc (void)
1875 midnight_dlg->get_shortcut = midnight_get_shortcut;
1877 create_panels ();
1879 add_widget (midnight_dlg, the_menubar);
1880 init_menu ();
1882 add_widget (midnight_dlg, get_panel_widget (0));
1883 add_widget (midnight_dlg, get_panel_widget (1));
1885 add_widget (midnight_dlg, the_hint);
1886 add_widget (midnight_dlg, cmdline);
1887 add_widget (midnight_dlg, the_prompt);
1889 add_widget (midnight_dlg, the_bar);
1890 midnight_set_buttonbar (the_bar);
1892 /* Run the Midnight Commander if no file was specified in the command line */
1893 run_dlg (midnight_dlg);
1896 /* result must be free'd (I think this should go in util.c) */
1897 static char *
1898 prepend_cwd_on_local (const char *filename)
1900 char *d;
1901 size_t l;
1903 if (!vfs_file_is_local (filename) || g_path_is_absolute (filename))
1904 return g_strdup (filename);
1906 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1907 mc_get_current_wd (d, MC_MAXPATHLEN);
1908 l = strlen (d);
1909 d[l++] = PATH_SEP;
1910 strcpy (d + l, filename);
1911 canonicalize_pathname (d);
1912 return d;
1915 /* Invoke the internal view/edit routine with:
1916 * the default processing and forcing the internal viewer/editor
1918 static void
1919 mc_maybe_editor_or_viewer (void)
1921 switch (mc_run_mode)
1923 #ifdef USE_INTERNAL_EDIT
1924 case MC_RUN_EDITOR:
1925 edit_file (mc_run_param0, edit_one_file_start_line);
1926 break;
1927 #endif /* USE_INTERNAL_EDIT */
1928 case MC_RUN_VIEWER:
1930 char *path;
1931 path = prepend_cwd_on_local (mc_run_param0);
1932 view_file (path, 0, 1);
1933 g_free (path);
1934 break;
1936 #ifdef USE_DIFF_VIEW
1937 case MC_RUN_DIFFVIEWER:
1938 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1939 break;
1940 #endif /* USE_DIFF_VIEW */
1941 default:
1942 break;
1944 midnight_shutdown = 1;
1945 done_mc ();
1948 /* Run the main dialog that occupies the whole screen */
1949 static void
1950 do_nc (void)
1952 int midnight_colors[DLG_COLOR_NUM];
1953 midnight_colors[0] = mc_skin_color_get ("dialog", "_default_");
1954 midnight_colors[1] = mc_skin_color_get ("dialog", "focus");
1955 midnight_colors[2] = mc_skin_color_get ("dialog", "hotnormal");
1956 midnight_colors[3] = mc_skin_color_get ("dialog", "hotfocus");
1958 panel_init ();
1960 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1961 "[main]", NULL, DLG_WANT_IDLE);
1963 if (mc_run_mode == MC_RUN_FULL)
1964 setup_mc ();
1965 else
1966 setup_dummy_mc ();
1968 /* start check display_codepage and source_codepage */
1969 check_codeset ();
1971 main_map = default_main_map;
1972 if (main_keymap && main_keymap->len > 0)
1973 main_map = (global_keymap_t *) main_keymap->data;
1975 main_x_map = default_main_x_map;
1976 if (main_x_keymap && main_x_keymap->len > 0)
1977 main_x_map = (global_keymap_t *) main_x_keymap->data;
1979 panel_map = default_panel_keymap;
1980 if (panel_keymap && panel_keymap->len > 0)
1981 panel_map = (global_keymap_t *) panel_keymap->data;
1983 input_map = default_input_keymap;
1984 if (input_keymap && input_keymap->len > 0)
1985 input_map = (global_keymap_t *) input_keymap->data;
1987 tree_map = default_tree_keymap;
1988 if (tree_keymap && tree_keymap->len > 0)
1989 tree_map = (global_keymap_t *) tree_keymap->data;
1991 help_map = default_help_keymap;
1992 if (help_keymap && help_keymap->len > 0)
1993 help_map = (global_keymap_t *) help_keymap->data;
1995 #ifdef USE_DIFF_VIEW
1996 diff_map = default_diff_keymap;
1997 if (diff_keymap && diff_keymap->len > 0)
1998 diff_map = (global_keymap_t *) diff_keymap->data;
1999 #endif
2001 /* Check if we were invoked as an editor or file viewer */
2002 if (mc_run_mode != MC_RUN_FULL)
2003 mc_maybe_editor_or_viewer ();
2004 else
2006 create_panels_and_run_mc ();
2008 /* Program end */
2009 midnight_shutdown = 1;
2011 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
2012 if (mc_args__last_wd_file && vfs_current_is_local ())
2013 last_wd_string = g_strdup (current_panel->cwd);
2015 done_mc ();
2018 destroy_dlg (midnight_dlg);
2019 panel_deinit ();
2020 current_panel = 0;
2021 done_mc_profile ();
2024 /* POSIX version. The only version we support. */
2025 static void
2026 OS_Setup (void)
2028 const char *shell_env = getenv ("SHELL");
2029 const char *mc_libdir;
2031 if ((shell_env == NULL) || (shell_env[0] == '\0'))
2033 struct passwd *pwd;
2034 pwd = getpwuid (geteuid ());
2035 if (pwd != NULL)
2036 shell = g_strdup (pwd->pw_shell);
2038 else
2039 shell = g_strdup (shell_env);
2041 if ((shell == NULL) || (shell[0] == '\0'))
2043 g_free (shell);
2044 shell = g_strdup ("/bin/sh");
2047 /* This is the directory, where MC was installed, on Unix this is DATADIR */
2048 /* and can be overriden by the MC_DATADIR environment variable */
2049 mc_libdir = getenv ("MC_DATADIR");
2050 if (mc_libdir != NULL)
2052 mc_home = g_strdup (mc_libdir);
2053 mc_home_alt = g_strdup (SYSCONFDIR);
2055 else
2057 mc_home = g_strdup (SYSCONFDIR);
2058 mc_home_alt = g_strdup (DATADIR);
2061 /* This variable is used by the subshell */
2062 home_dir = getenv ("HOME");
2064 if (!home_dir)
2065 home_dir = mc_home;
2068 static void
2069 sigchld_handler_no_subshell (int sig)
2071 #ifdef __linux__
2072 int pid, status;
2074 if (!console_flag)
2075 return;
2077 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2078 the value of console_flag never changed, we could simply not install
2079 this handler at all if (!console_flag && !use_subshell). */
2081 /* That comment is no longer true. We need to wait() on a sigchld
2082 handler (that's at least what the tarfs code expects currently). */
2084 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2086 if (pid == cons_saver_pid)
2089 if (WIFSTOPPED (status))
2091 /* Someone has stopped cons.saver - restart it */
2092 kill (pid, SIGCONT);
2094 else
2096 /* cons.saver has died - disable console saving */
2097 handle_console (CONSOLE_DONE);
2098 console_flag = 0;
2101 /* If we got here, some other child exited; ignore it */
2102 #endif /* __linux__ */
2104 (void) sig;
2107 static void
2108 init_sigchld (void)
2110 struct sigaction sigchld_action;
2112 sigchld_action.sa_handler =
2113 #ifdef HAVE_SUBSHELL_SUPPORT
2114 use_subshell ? sigchld_handler :
2115 #endif /* HAVE_SUBSHELL_SUPPORT */
2116 sigchld_handler_no_subshell;
2118 sigemptyset (&sigchld_action.sa_mask);
2120 #ifdef SA_RESTART
2121 sigchld_action.sa_flags = SA_RESTART;
2122 #else
2123 sigchld_action.sa_flags = 0;
2124 #endif /* !SA_RESTART */
2126 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
2128 #ifdef HAVE_SUBSHELL_SUPPORT
2130 * This may happen on QNX Neutrino 6, where SA_RESTART
2131 * is defined but not implemented. Fallback to no subshell.
2133 use_subshell = 0;
2134 #endif /* HAVE_SUBSHELL_SUPPORT */
2138 static void
2139 mc_main__setup_by_args (int argc, char *argv[])
2141 const char *base;
2142 char *tmp;
2144 if (mc_args__nomouse)
2145 use_mouse_p = MOUSE_DISABLED;
2147 #ifdef USE_NETCODE
2148 if (mc_args__netfs_logfile != NULL)
2150 mc_setctl ("/#ftp:", VFS_SETCTL_LOGFILE, (void *) mc_args__netfs_logfile);
2151 #ifdef ENABLE_VFS_SMB
2152 smbfs_set_debugf (mc_args__netfs_logfile);
2153 #endif /* ENABLE_VFS_SMB */
2156 #ifdef ENABLE_VFS_SMB
2157 if (mc_args__debug_level != 0)
2158 smbfs_set_debug (mc_args__debug_level);
2159 #endif /* ENABLE_VFS_SMB */
2160 #endif /* USE_NETCODE */
2162 base = x_basename (argv[0]);
2163 tmp = (argc > 0) ? argv[1] : NULL;
2165 if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi"))
2167 mc_run_param0 = g_strdup ("");
2168 if (tmp != NULL)
2171 * Check for filename:lineno, followed by an optional colon.
2172 * This format is used by many programs (especially compilers)
2173 * in error messages and warnings. It is supported so that
2174 * users can quickly copy and paste file locations.
2176 char *end = tmp + strlen (tmp), *p = end;
2177 if (p > tmp && p[-1] == ':')
2178 p--;
2179 while (p > tmp && g_ascii_isdigit ((gchar) p[-1]))
2180 p--;
2181 if (tmp < p && p < end && p[-1] == ':')
2183 struct stat st;
2184 gchar *fname = g_strndup (tmp, p - 1 - tmp);
2186 * Check that the file before the colon actually exists.
2187 * If it doesn't exist, revert to the old behavior.
2189 if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1)
2191 mc_run_param0 = fname;
2192 edit_one_file_start_line = atoi (p);
2194 else
2196 g_free (fname);
2197 goto try_plus_filename;
2200 else
2202 try_plus_filename:
2203 if (*tmp == '+' && g_ascii_isdigit ((gchar) tmp[1]))
2205 int start_line = atoi (tmp);
2206 if (start_line > 0)
2208 char *file = (argc > 1) ? argv[2] : NULL;
2209 if (file)
2211 tmp = file;
2212 edit_one_file_start_line = start_line;
2216 mc_run_param0 = g_strdup (tmp);
2219 mc_run_mode = MC_RUN_EDITOR;
2221 else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view"))
2223 if (tmp != NULL)
2224 mc_run_param0 = g_strdup (tmp);
2225 else
2227 fputs ("No arguments given to the viewer\n", stderr);
2228 exit (EXIT_FAILURE);
2230 mc_run_mode = MC_RUN_VIEWER;
2231 #ifdef USE_DIFF_VIEW
2233 else if (!STRNCOMP (base, "mcd", 3) || !STRCOMP (base, "diff"))
2235 if (argc < 3)
2237 fputs ("There 2 files are required to diffviewer\n", stderr);
2238 exit (EXIT_FAILURE);
2240 else if (tmp != NULL)
2242 mc_run_param0 = g_strdup (tmp);
2243 tmp = (argc > 1) ? argv[2] : NULL;
2244 if (tmp != NULL)
2245 mc_run_param1 = g_strdup (tmp);
2246 mc_run_mode = MC_RUN_DIFFVIEWER;
2248 #endif /* USE_DIFF_VIEW */
2250 else
2252 /* sets the current dir and the other dir */
2253 if (tmp != NULL)
2255 mc_run_param0 = g_strdup (tmp);
2256 tmp = (argc > 1) ? argv[2] : NULL;
2257 if (tmp != NULL)
2258 mc_run_param1 = g_strdup (tmp);
2260 mc_run_mode = MC_RUN_FULL;
2265 main (int argc, char *argv[])
2267 struct stat s;
2268 char *mc_dir;
2269 GError *error = NULL;
2270 gboolean isInitialized;
2272 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2273 setlocale (LC_ALL, "");
2274 bindtextdomain ("mc", LOCALEDIR);
2275 textdomain ("mc");
2277 /* Set up temporary directory */
2278 mc_tmpdir ();
2280 OS_Setup ();
2282 str_init_strings (NULL);
2284 vfs_init ();
2286 #ifdef USE_INTERNAL_EDIT
2287 edit_stack_init ();
2288 #endif
2290 #ifdef HAVE_SLANG
2291 SLtt_Ignore_Beep = 1;
2292 #endif
2294 if (!mc_args_handle (&argc, &argv, "mc"))
2295 return 1;
2297 mc_main__setup_by_args (argc, argv);
2299 /* NOTE: This has to be called before tty_init or whatever routine
2300 calls any define_sequence */
2301 init_key ();
2303 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2304 handle_console (CONSOLE_INIT);
2306 #ifdef HAVE_SUBSHELL_SUPPORT
2307 /* Don't use subshell when invoked as viewer or editor */
2308 if (mc_run_mode != MC_RUN_FULL)
2309 use_subshell = 0;
2311 if (use_subshell)
2312 subshell_get_console_attributes ();
2313 #endif /* HAVE_SUBSHELL_SUPPORT */
2315 /* Install the SIGCHLD handler; must be done before init_subshell() */
2316 init_sigchld ();
2318 /* We need this, since ncurses endwin () doesn't restore the signals */
2319 save_stop_handler ();
2321 /* Must be done before init_subshell, to set up the terminal size: */
2322 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2323 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2325 load_setup ();
2327 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2329 isInitialized = mc_skin_init (&error);
2331 mc_filehighlight = mc_fhl_new (TRUE);
2333 dlg_set_default_colors ();
2335 if (!isInitialized)
2337 message (D_ERROR, _("Warning"), "%s", error->message);
2338 g_error_free (error);
2339 error = NULL;
2342 /* create home directory */
2343 /* do it after the screen library initialization to show the error message */
2344 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2345 canonicalize_pathname (mc_dir);
2346 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
2347 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
2348 g_free (mc_dir);
2350 #ifdef HAVE_SUBSHELL_SUPPORT
2351 /* Done here to ensure that the subshell doesn't */
2352 /* inherit the file descriptors opened below, etc */
2353 if (use_subshell)
2354 init_subshell ();
2356 #endif /* HAVE_SUBSHELL_SUPPORT */
2358 /* Removing this from the X code let's us type C-c */
2359 load_key_defs ();
2361 load_keymap_defs ();
2363 /* Also done after init_subshell, to save any shell init file messages */
2364 if (console_flag)
2365 handle_console (CONSOLE_SAVE);
2367 if (alternate_plus_minus)
2368 application_keypad_mode ();
2370 #ifdef HAVE_SUBSHELL_SUPPORT
2371 if (use_subshell)
2373 mc_prompt = strip_ctrl_codes (subshell_prompt);
2374 if (mc_prompt == NULL)
2375 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2377 else
2378 #endif /* HAVE_SUBSHELL_SUPPORT */
2379 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2381 /* Program main loop */
2382 if (!midnight_shutdown)
2383 do_nc ();
2385 /* Save the tree store */
2386 tree_store_save ();
2388 free_keymap_defs ();
2390 /* Virtual File System shutdown */
2391 vfs_shut ();
2393 flush_extension_file (); /* does only free memory */
2395 mc_fhl_free (&mc_filehighlight);
2396 mc_skin_deinit ();
2398 tty_shutdown ();
2400 if (console_flag && !(quit & SUBSHELL_EXIT))
2401 handle_console (CONSOLE_RESTORE);
2402 if (alternate_plus_minus)
2403 numeric_keypad_mode ();
2405 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2407 if (console_flag)
2408 handle_console (CONSOLE_DONE);
2409 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2411 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string && !print_last_revert)
2413 int last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2414 S_IRUSR | S_IWUSR);
2416 if (last_wd_fd != -1)
2418 ssize_t ret1;
2419 int ret2;
2420 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2421 ret2 = close (last_wd_fd);
2424 g_free (last_wd_string);
2426 g_free (mc_home_alt);
2427 g_free (mc_home);
2428 g_free (shell);
2430 done_key ();
2431 #ifdef HAVE_CHARSET
2432 free_codepages_list ();
2433 g_free (autodetect_codeset);
2434 #endif
2435 str_uninit_strings ();
2437 g_free (mc_run_param0);
2438 g_free (mc_run_param1);
2440 #ifdef USE_INTERNAL_EDIT
2441 edit_stack_free ();
2442 #endif
2444 return 0;