Merge branch '2161_del_clear_dialog_field'
[midnight-commander.git] / src / main.c
blob7ba1a6bf4d38666c5b3e9660e41e3423b7712246
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 /* HAVE_CHARSET */
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, at startup the user-menu is invoked */
152 int auto_menu = 0;
154 /* If true, then the +, - and \ keys have their special meaning only if the
155 * command line is emtpy, otherwise they behave like regular letters
157 int only_leading_plus_minus = 1;
159 int pause_after_run = pause_on_dumb_terminals;
161 /* It true saves the setup when quitting */
162 int auto_save_setup = 1;
164 #ifdef HAVE_CHARSET
166 * Don't restrict the output on the screen manager level,
167 * the translation tables take care of it.
169 #define full_eight_bits (1)
170 #define eight_bit_clean (1)
171 #else /* HAVE_CHARSET */
172 /* If true, allow characters in the range 160-255 */
173 int eight_bit_clean = 1;
175 * If true, also allow characters in the range 128-159.
176 * This is reported to break on many terminals (xterm, qansi-m).
178 int full_eight_bits = 0;
179 #endif /* !HAVE_CHARSET */
182 * If utf-8 terminal utf8_display = 1
183 * Display bits set UTF-8
186 int utf8_display = 0;
188 /* If true use the internal viewer */
189 int use_internal_view = 1;
191 /* The prompt */
192 const char *mc_prompt = NULL;
194 /* The widget where we draw the prompt */
195 WLabel *the_prompt;
197 /* The hint bar */
198 WLabel *the_hint;
200 /* The button bar */
201 WButtonBar *the_bar;
203 /* Mouse type: GPM, xterm or none */
204 Mouse_Type use_mouse_p = MOUSE_NONE;
206 /* If on, default for "No" in delete operations */
207 int safe_delete = 0;
209 /* Controls screen clearing before an exec */
210 int clear_before_exec = 1;
212 /* Asks for confirmation before deleting a file */
213 int confirm_delete = 1;
215 /* Asks for confirmation before deleting a hotlist entry */
216 int confirm_directory_hotlist_delete = 1;
218 /* Asks for confirmation before overwriting a file */
219 int confirm_overwrite = 1;
221 /* Asks for confirmation before executing a program by pressing enter */
222 int confirm_execute = 0;
224 /* Asks for confirmation before leaving the program */
225 int confirm_exit = 1;
227 /* Asks for confirmation before clean up of history */
228 int confirm_history_cleanup = 1;
230 /* Asks for confirmation when using F3 to view a directory and there
231 are tagged files */
232 int confirm_view_dir = 0;
234 /* This flag indicates if the pull down menus by default drop down */
235 int drop_menus = 0;
237 /* The dialog handle for the main program */
238 Dlg_head *midnight_dlg = NULL;
240 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
241 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
242 int update_prompt = 0;
244 /* The home directory */
245 const char *home_dir = NULL;
247 /* Tab size */
248 int option_tab_spacing = 8;
250 /* Only used at program boot */
251 int boot_current_is_left = 1;
253 /* If this is true, then when browsing the tree the other window will
254 * automatically reload it's directory with the contents of the currently
255 * selected directory.
257 int xtree_mode = 0;
259 /* If set, then print to the given file the last directory we were at */
260 static char *last_wd_string = NULL;
261 /* Set to 1 to suppress printing the last directory */
262 static int print_last_revert = 0;
264 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
265 char *mc_run_param0 = NULL;
266 char *mc_run_param1 = NULL;
268 /* Used so that widgets know if they are being destroyed or
269 shut down */
270 int midnight_shutdown = 0;
272 /* The user's shell */
273 char *shell = NULL;
275 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
276 char *mc_home = NULL;
278 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
279 char *mc_home_alt = NULL;
281 /* Define this function for glib-style error handling */
282 GQuark
283 mc_main_error_quark (void)
285 return g_quark_from_static_string (PACKAGE);
288 /* Save current stat of directories to avoid reloading the panels */
289 /* when no modifications have taken place */
290 void
291 save_cwds_stat (void)
293 if (panels_options.fast_reload)
295 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
296 if (get_other_type () == view_listing)
297 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
301 #ifdef HAVE_SUBSHELL_SUPPORT
302 void
303 do_update_prompt (void)
305 if (update_prompt)
307 printf ("\r\n%s", subshell_prompt);
308 fflush (stdout);
309 update_prompt = 0;
312 #endif /* HAVE_SUBSHELL_SUPPORT */
314 void
315 change_panel (void)
317 free_completions (cmdline);
318 dlg_one_down (midnight_dlg);
321 /* Stop MC main dialog and the current dialog if it exists.
322 * Needed to provide fast exit from MC viewer or editor on shell exit */
323 static void
324 stop_dialogs (void)
326 midnight_dlg->running = 0;
327 if (current_dlg)
329 current_dlg->running = 0;
333 static int
334 quit_cmd_internal (int quiet)
336 int q = quit;
338 if (quiet || !confirm_exit)
340 q = 1;
342 else
344 if (query_dialog
345 (_(" The Midnight Commander "),
346 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
347 2, _("&Yes"), _("&No")) == 0)
348 q = 1;
350 if (q)
352 #ifdef HAVE_SUBSHELL_SUPPORT
353 if (!use_subshell)
354 stop_dialogs ();
355 else if ((q = exit_subshell ()))
356 #endif
357 stop_dialogs ();
359 if (q)
360 quit |= 1;
361 return quit;
364 static void
365 quit_cmd (void)
367 quit_cmd_internal (0);
370 void
371 quiet_quit_cmd (void)
373 print_last_revert = 1;
374 quit_cmd_internal (1);
377 /* Wrapper for do_subshell_chdir, check for availability of subshell */
378 void
379 subshell_chdir (const char *directory)
381 #ifdef HAVE_SUBSHELL_SUPPORT
382 if (use_subshell)
384 if (vfs_current_is_local ())
385 do_subshell_chdir (directory, 0, 1);
387 #endif /* HAVE_SUBSHELL_SUPPORT */
390 void
391 directory_history_add (struct WPanel *panel, const char *dir)
393 char *tmp;
395 tmp = g_strdup (dir);
396 strip_password (tmp, 1);
398 panel->dir_history = list_append_unique (panel->dir_history, tmp);
402 * If we moved to the parent directory move the selection pointer to
403 * the old directory name; If we leave VFS dir, remove FS specificator.
405 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
407 static const char *
408 get_parent_dir_name (const char *cwd, const char *lwd)
410 size_t llen, clen;
412 llen = strlen (lwd);
413 clen = strlen (cwd);
415 if (llen > clen)
417 const char *p;
419 p = strrchr (lwd, PATH_SEP);
421 if ((p != NULL)
422 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
423 && (clen == (size_t) (p - lwd)
424 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
425 return (p + 1);
428 return NULL;
432 * Changes the current directory of the panel.
433 * Don't record change in the directory history.
435 static int
436 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
438 const char *directory;
439 char *olddir;
440 char temp[MC_MAXPATHLEN];
441 char *translated_url;
443 if (cd_type == cd_parse_command)
445 while (*new_dir == ' ')
446 new_dir++;
449 olddir = g_strdup (panel->cwd);
450 new_dir = translated_url = vfs_translate_url (new_dir);
452 /* Convert *new_path to a suitable pathname, handle ~user */
454 if (cd_type == cd_parse_command)
456 if (!strcmp (new_dir, "-"))
458 strcpy (temp, panel->lwd);
459 new_dir = temp;
462 directory = *new_dir ? new_dir : home_dir;
464 if (mc_chdir (directory) == -1)
466 strcpy (panel->cwd, olddir);
467 g_free (olddir);
468 g_free (translated_url);
469 return 0;
471 g_free (translated_url);
473 /* Success: save previous directory, shutdown status of previous dir */
474 strcpy (panel->lwd, olddir);
475 free_completions (cmdline);
477 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
479 vfs_release_path (olddir);
481 subshell_chdir (panel->cwd);
483 /* Reload current panel */
484 panel_clean_dir (panel);
485 panel->count =
486 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
487 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
488 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
489 load_hint (0);
490 panel->dirty = 1;
491 update_xterm_title_path ();
493 g_free (olddir);
495 return 1;
499 * Changes the current directory of the panel.
500 * Record change in the directory history.
503 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
505 int r;
507 r = _do_panel_cd (panel, new_dir, cd_type);
508 if (r)
509 directory_history_add (panel, panel->cwd);
510 return r;
514 do_cd (const char *new_dir, enum cd_enum exact)
516 return (do_panel_cd (current_panel, new_dir, exact));
519 void
520 directory_history_next (WPanel * panel)
522 GList *nextdir;
524 nextdir = g_list_next (panel->dir_history);
526 if (!nextdir)
527 return;
529 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
530 panel->dir_history = nextdir;
533 void
534 directory_history_prev (WPanel * panel)
536 GList *prevdir;
538 prevdir = g_list_previous (panel->dir_history);
540 if (!prevdir)
541 return;
543 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
544 panel->dir_history = prevdir;
547 void
548 directory_history_list (WPanel * panel)
550 char *s;
552 s = show_hist (&panel->dir_history, &panel->widget);
554 if (s != NULL)
556 if (_do_panel_cd (panel, s, cd_exact))
557 directory_history_add (panel, panel->cwd);
558 else
559 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
560 g_free (s);
564 #ifdef HAVE_SUBSHELL_SUPPORT
566 load_prompt (int fd, void *unused)
568 (void) fd;
569 (void) unused;
571 if (!read_subshell_prompt ())
572 return 0;
574 /* Don't actually change the prompt if it's invisible */
575 if (current_dlg == midnight_dlg && command_prompt)
577 char *tmp_prompt;
578 int prompt_len;
580 tmp_prompt = strip_ctrl_codes (subshell_prompt);
581 prompt_len = str_term_width1 (tmp_prompt);
583 /* Check for prompts too big */
584 if (COLS > 8 && prompt_len > COLS - 8)
586 tmp_prompt[COLS - 8] = '\0';
587 prompt_len = COLS - 8;
589 mc_prompt = tmp_prompt;
590 label_set_text (the_prompt, mc_prompt);
591 winput_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
593 /* since the prompt has changed, and we are called from one of the
594 * tty_get_event channels, the prompt updating does not take place
595 * automatically: force a cursor update and a screen refresh
597 update_cursor (midnight_dlg);
598 mc_refresh ();
600 update_prompt = 1;
601 return 0;
603 #endif /* HAVE_SUBSHELL_SUPPORT */
605 void
606 sort_cmd (void)
608 WPanel *p;
609 const panel_field_t *sort_order;
611 if (!SELECTED_IS_PANEL)
612 return;
614 p = MENU_PANEL;
615 sort_order = sort_box (p->current_sort_field, &p->reverse, &p->case_sensitive, &p->exec_first);
617 panel_set_sort_order (p, sort_order);
621 static void
622 treebox_cmd (void)
624 char *sel_dir;
626 sel_dir = tree_box (selection (current_panel)->fname);
627 if (sel_dir)
629 do_cd (sel_dir, cd_exact);
630 g_free (sel_dir);
634 #ifdef LISTMODE_EDITOR
635 static void
636 listmode_cmd (void)
638 char *newmode;
640 if (get_current_type () != view_listing)
641 return;
643 newmode = listmode_edit (current_panel->user_format);
644 if (!newmode)
645 return;
647 g_free (current_panel->user_format);
648 current_panel->list_type = list_user;
649 current_panel->user_format = newmode;
650 set_panel_formats (current_panel);
652 do_refresh ();
654 #endif /* LISTMODE_EDITOR */
656 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
657 static GList *
658 create_panel_menu (void)
660 GList *entries = NULL;
662 entries = g_list_append (entries, menu_entry_create (_("File listin&g"), CK_ListingCmd));
663 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
664 entries = g_list_append (entries, menu_entry_create (_("&Info" ), CK_InfoCmd));
665 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
666 entries = g_list_append (entries, menu_separator_create ());
667 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ChangeListingCmd));
668 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
669 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
670 #ifdef HAVE_CHARSET
671 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
672 #endif
673 #ifdef USE_NETCODE
674 entries = g_list_append (entries, menu_separator_create ());
675 #ifdef ENABLE_VFS_MCFS
676 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
677 #endif
678 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
679 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
680 #ifdef ENABLE_VFS_SMB
681 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
682 #endif /* ENABLE_VFS_SMB */
683 #endif /* USE_NETCODE */
684 entries = g_list_append (entries, menu_separator_create ());
685 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
687 return entries;
690 static GList *
691 create_file_menu (void)
693 GList *entries = NULL;
695 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
696 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
697 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
698 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
699 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
700 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
701 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
702 entries = g_list_append (entries, menu_entry_create (_("&Symlink"), CK_SymlinkCmd));
703 entries = g_list_append (entries, menu_entry_create (_("Relative symlin&k"), CK_RelativeSymlinkCmd));
704 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
705 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
706 entries =
707 g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
708 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
709 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
710 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
711 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
712 entries = g_list_append (entries, menu_separator_create ());
713 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
714 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
715 entries =
716 g_list_append (entries,
717 menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
718 entries = g_list_append (entries, menu_separator_create ());
719 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
721 return entries;
724 static GList *
725 create_command_menu (void)
727 /* I know, I'm lazy, but the tree widget when it's not running
728 * as a panel still has some problems, I have not yet finished
729 * the WTree widget port, sorry.
731 GList *entries = NULL;
733 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
734 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
735 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
736 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
737 entries =
738 g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
739 entries =
740 g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
741 #ifdef USE_DIFF_VIEW
742 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
743 #endif
744 entries =
745 g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
746 entries =
747 g_list_append (entries,
748 menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
749 entries = g_list_append (entries, menu_separator_create ());
750 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
751 entries =
752 g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
753 #ifdef ENABLE_VFS
754 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
755 #endif
756 #ifdef WITH_BACKGROUND
757 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
758 #endif
759 entries = g_list_append (entries, menu_separator_create ());
760 #ifdef USE_EXT2FSLIB
761 entries =
762 g_list_append (entries,
763 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
764 #endif
765 #ifdef LISTMODE_EDITOR
766 entries =
767 g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
768 #endif
769 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
770 entries = g_list_append (entries, menu_separator_create ());
771 #endif
772 entries =
773 g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
774 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
775 entries =
776 g_list_append (entries,
777 menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
779 return entries;
782 static GList *
783 create_options_menu (void)
785 GList *entries = NULL;
787 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
788 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutBox));
789 entries = g_list_append (entries, menu_entry_create (_("&Panel options..."), CK_PanelOptionsBox));
790 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
791 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
792 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
793 #ifdef ENABLE_VFS
794 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
795 #endif
796 entries = g_list_append (entries, menu_separator_create ());
797 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
799 return entries;
802 void
803 init_menu (void)
805 menubar_add_menu (the_menubar,
806 create_menu (horizontal_split ? _("&Above") : _("&Left"),
807 create_panel_menu (), "[Left and Right Menus]"));
808 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
809 menubar_add_menu (the_menubar,
810 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
811 menubar_add_menu (the_menubar,
812 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
813 menubar_add_menu (the_menubar,
814 create_menu (horizontal_split ? _("&Below") : _("&Right"),
815 create_panel_menu (), "[Left and Right Menus]"));
818 void
819 done_menu (void)
821 menubar_set_menu (the_menubar, NULL);
824 static void
825 menu_last_selected_cmd (void)
827 the_menubar->is_active = TRUE;
828 the_menubar->is_dropped = (drop_menus != 0);
829 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
830 dlg_select_widget (the_menubar);
833 static void
834 menu_cmd (void)
836 if (the_menubar->is_active)
837 return;
839 if ((get_current_index () == 0) == (current_panel->active != 0))
840 the_menubar->selected = 0;
841 else
842 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
843 menu_last_selected_cmd ();
846 static char *
847 midnight_get_shortcut (unsigned long command)
849 const char *ext_map;
850 const char *shortcut = NULL;
852 shortcut = lookup_keymap_shortcut (main_map, command);
853 if (shortcut != NULL)
854 return g_strdup (shortcut);
856 shortcut = lookup_keymap_shortcut (panel_map, command);
857 if (shortcut != NULL)
858 return g_strdup (shortcut);
860 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
861 if (ext_map != NULL)
862 shortcut = lookup_keymap_shortcut (main_x_map, command);
863 if (shortcut != NULL)
864 return g_strdup_printf ("%s %s", ext_map, shortcut);
866 return NULL;
869 void
870 toggle_show_hidden (void)
872 panels_options.show_dot_files = !panels_options.show_dot_files;
873 update_panels (UP_RELOAD, UP_KEEPSEL);
876 static void
877 toggle_panels_split (void)
879 horizontal_split = !horizontal_split;
880 layout_change ();
881 do_refresh ();
885 * Just a hack for allowing url-like pathnames to be accepted from the
886 * command line.
888 static void
889 translated_mc_chdir (char *dir)
891 char *newdir;
892 int ret;
894 newdir = vfs_translate_url (dir);
895 ret = mc_chdir (newdir);
896 g_free (newdir);
899 static void
900 create_panels (void)
902 int current_index;
903 int other_index;
904 panel_view_mode_t current_mode, other_mode;
905 char original_dir[BUF_1K] = "\0";
907 if (boot_current_is_left)
909 current_index = 0;
910 other_index = 1;
911 current_mode = startup_left_mode;
912 other_mode = startup_right_mode;
914 else
916 current_index = 1;
917 other_index = 0;
918 current_mode = startup_right_mode;
919 other_mode = startup_left_mode;
921 /* Creates the left panel */
922 if (mc_run_param0 != NULL)
924 if (mc_run_param1 != NULL)
926 /* Ok, user has specified two dirs, save the original one,
927 * since we may not be able to chdir to the proper
928 * second directory later
930 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
932 translated_mc_chdir (mc_run_param0);
934 set_display_type (current_index, current_mode);
936 /* The other panel */
937 if (mc_run_param1 != NULL)
939 if (original_dir[0] != '\0')
940 translated_mc_chdir (original_dir);
941 translated_mc_chdir (mc_run_param1);
943 set_display_type (other_index, other_mode);
945 if (startup_left_mode == view_listing)
947 current_panel = left_panel;
949 else
951 if (right_panel)
952 current_panel = right_panel;
953 else
954 current_panel = left_panel;
957 /* Create the nice widgets */
958 cmdline = command_new (0, 0, 0);
959 the_prompt = label_new (0, 0, mc_prompt);
960 the_prompt->transparent = 1;
961 the_bar = buttonbar_new (keybar_visible);
963 the_hint = label_new (0, 0, 0);
964 the_hint->transparent = 1;
965 the_hint->auto_adjust_cols = 0;
966 the_hint->widget.cols = COLS;
968 the_menubar = menubar_new (0, 0, COLS, NULL);
971 static void
972 copy_current_pathname (void)
974 char *cwd_path;
975 if (!command_prompt)
976 return;
978 cwd_path = remove_encoding_from_path (current_panel->cwd);
979 command_insert (cmdline, cwd_path, 0);
981 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
982 command_insert (cmdline, PATH_SEP_STR, 0);
983 g_free (cwd_path);
986 static void
987 copy_other_pathname (void)
989 char *cwd_path;
991 if (get_other_type () != view_listing)
992 return;
994 if (!command_prompt)
995 return;
997 cwd_path = remove_encoding_from_path (other_panel->cwd);
998 command_insert (cmdline, cwd_path, 0);
1000 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
1001 command_insert (cmdline, PATH_SEP_STR, 0);
1002 g_free (cwd_path);
1005 static void
1006 copy_readlink (WPanel * panel)
1008 if (!command_prompt)
1009 return;
1010 if (S_ISLNK (selection (panel)->st.st_mode))
1012 char buffer[MC_MAXPATHLEN];
1013 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1014 int i;
1016 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1017 g_free (p);
1018 if (i > 0)
1020 buffer[i] = 0;
1021 command_insert (cmdline, buffer, 1);
1026 static void
1027 copy_current_readlink (void)
1029 copy_readlink (current_panel);
1032 static void
1033 copy_other_readlink (void)
1035 if (get_other_type () == view_listing)
1036 copy_readlink (other_panel);
1039 /* Insert the selected file name into the input line */
1040 static void
1041 copy_prog_name (void)
1043 char *tmp;
1044 if (!command_prompt)
1045 return;
1047 if (get_current_type () == view_tree)
1049 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1050 tmp = tree_selected_name (tree);
1052 else
1053 tmp = selection (current_panel)->fname;
1055 command_insert (cmdline, tmp, 1);
1058 static void
1059 copy_tagged (WPanel * panel)
1061 int i;
1063 if (!command_prompt)
1064 return;
1065 input_disable_update (cmdline);
1066 if (panel->marked)
1068 for (i = 0; i < panel->count; i++)
1070 if (panel->dir.list[i].f.marked)
1071 command_insert (cmdline, panel->dir.list[i].fname, 1);
1074 else
1076 command_insert (cmdline, panel->dir.list[panel->selected].fname, 1);
1078 input_enable_update (cmdline);
1081 static void
1082 copy_current_tagged (void)
1084 copy_tagged (current_panel);
1087 static void
1088 copy_other_tagged (void)
1090 if (get_other_type () == view_listing)
1091 copy_tagged (other_panel);
1094 void
1095 midnight_set_buttonbar (WButtonBar * b)
1097 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1098 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1099 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1100 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1101 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1102 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1103 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1104 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1105 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1106 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1109 static gboolean ctl_x_map_enabled = FALSE;
1111 static void
1112 ctl_x_cmd (void)
1114 ctl_x_map_enabled = TRUE;
1117 static cb_ret_t
1118 midnight_execute_cmd (Widget * sender, unsigned long command)
1120 cb_ret_t res = MSG_HANDLED;
1122 (void) sender;
1124 switch (command)
1126 case CK_AddHotlist:
1127 add2hotlist_cmd ();
1128 break;
1129 case CK_ChangeListingCmd:
1130 change_listing_cmd ();
1131 break;
1132 case CK_ChmodCmd:
1133 chmod_cmd ();
1134 break;
1135 case CK_ChownCmd:
1136 chown_cmd ();
1137 break;
1138 case CK_ChownAdvancedCmd:
1139 chown_advanced_cmd ();
1140 break;
1141 case CK_CompareDirsCmd:
1142 compare_dirs_cmd ();
1143 break;
1144 case CK_ConfigureBox:
1145 configure_box ();
1146 break;
1147 #ifdef ENABLE_VFS
1148 case CK_ConfigureVfs:
1149 configure_vfs ();
1150 break;
1151 #endif
1152 case CK_ConfirmBox:
1153 confirm_box ();
1154 break;
1155 case CK_CopyCmd:
1156 copy_cmd ();
1157 break;
1158 case CK_CopyCurrentPathname:
1159 copy_current_pathname ();
1160 break;
1161 case CK_CopyCurrentReadlink:
1162 copy_current_readlink ();
1163 break;
1164 case CK_CopyCurrentTagged:
1165 copy_current_tagged ();
1166 break;
1167 case CK_CopyOtherPathname:
1168 copy_other_pathname ();
1169 break;
1170 case CK_CopyOtherReadlink:
1171 copy_other_readlink ();
1172 break;
1173 case CK_CopyOtherTagged:
1174 copy_other_tagged ();
1175 break;
1176 case CK_DeleteCmd:
1177 delete_cmd ();
1178 break;
1179 #ifdef USE_DIFF_VIEW
1180 case CK_DiffViewCmd:
1181 diff_view_cmd ();
1182 break;
1183 #endif
1184 case CK_DisplayBitsBox:
1185 display_bits_box ();
1186 break;
1187 case CK_EditCmd:
1188 edit_cmd ();
1189 break;
1190 case CK_EditExtFileCmd:
1191 ext_cmd ();
1192 break;
1193 case CK_EditFhlFileCmd:
1194 edit_fhl_cmd ();
1195 break;
1196 case CK_EditMcMenuCmd:
1197 edit_mc_menu_cmd ();
1198 break;
1199 case CK_EditSymlinkCmd:
1200 edit_symlink_cmd ();
1201 break;
1202 case CK_ExternalPanelize:
1203 external_panelize ();
1204 break;
1205 case CK_FilterCmd:
1206 filter_cmd ();
1207 break;
1208 case CK_FilteredViewCmd:
1209 filtered_view_cmd ();
1210 break;
1211 case CK_FindCmd:
1212 find_cmd ();
1213 break;
1214 #if defined (USE_NETCODE)
1215 case CK_FishlinkCmd:
1216 fishlink_cmd ();
1217 break;
1218 case CK_FtplinkCmd:
1219 ftplink_cmd ();
1220 break;
1221 #endif
1222 case CK_HelpCmd:
1223 help_cmd ();
1224 break;
1225 case CK_HistoryCmd:
1226 history_cmd ();
1227 break;
1228 case CK_InfoCmd:
1229 if (sender == (Widget *) the_menubar)
1230 info_cmd (); /* mwnu */
1231 else
1232 info_cmd_no_menu (); /* shortcut or buttonbar */
1233 break;
1234 #ifdef WITH_BACKGROUND
1235 case CK_JobsCmd:
1236 jobs_cmd ();
1237 break;
1238 #endif
1239 case CK_LayoutBox:
1240 layout_box ();
1241 break;
1242 case CK_LearnKeys:
1243 learn_keys ();
1244 break;
1245 case CK_LinkCmd:
1246 link_cmd (LINK_HARDLINK);
1247 break;
1248 case CK_ListingCmd:
1249 listing_cmd ();
1250 break;
1251 #ifdef LISTMODE_EDITOR
1252 case CK_ListmodeCmd:
1253 listmode_cmd ();
1254 break;
1255 #endif
1256 case CK_MenuCmd:
1257 menu_cmd ();
1258 break;
1259 case CK_MenuLastSelectedCmd:
1260 menu_last_selected_cmd ();
1261 break;
1262 case CK_MkdirCmd:
1263 mkdir_cmd ();
1264 break;
1265 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1266 case CK_NetlinkCmd:
1267 netlink_cmd ();
1268 break;
1269 #endif
1270 case CK_PanelOptionsBox:
1271 panel_options_box ();
1272 break;
1273 #ifdef HAVE_CHARSET
1274 case CK_PanelSetPanelEncoding:
1275 encoding_cmd ();
1276 break;
1277 #endif
1278 case CK_QuickCdCmd:
1279 quick_cd_cmd ();
1280 break;
1281 case CK_QuickChdirCmd:
1282 quick_chdir_cmd ();
1283 break;
1284 case CK_QuickViewCmd:
1285 if (sender == (Widget *) the_menubar)
1286 quick_view_cmd (); /* menu */
1287 else
1288 quick_cmd_no_menu (); /* shortcut or buttonabr */
1289 break;
1290 case CK_QuietQuitCmd:
1291 quiet_quit_cmd ();
1292 break;
1293 case CK_QuitCmd:
1294 quit_cmd ();
1295 break;
1296 case CK_RelativeSymlinkCmd:
1297 link_cmd (LINK_SYMLINK_RELATIVE);
1298 break;
1299 case CK_RenameCmd:
1300 rename_cmd ();
1301 break;
1302 case CK_RereadCmd:
1303 reread_cmd ();
1304 break;
1305 #ifdef ENABLE_VFS
1306 case CK_ReselectVfs:
1307 reselect_vfs ();
1308 break;
1309 #endif
1310 case CK_ReverseSelectionCmd:
1311 reverse_selection_cmd ();
1312 break;
1313 case CK_SaveSetupCmd:
1314 save_setup_cmd ();
1315 break;
1316 case CK_SelectCmd:
1317 select_cmd ();
1318 break;
1319 case CK_ShowCommandLine:
1320 view_other_cmd ();
1321 break;
1322 case CK_SingleDirsizeCmd:
1323 smart_dirsize_cmd ();
1324 break;
1325 #if defined (USE_NETCODE) && defined (ENABLE_VFS_SMB)
1326 case CK_SmblinkCmd:
1327 smblink_cmd ();
1328 break;
1329 #endif /* USE_NETCODE && ENABLE_VFS_SMB */
1330 case CK_Sort:
1331 sort_cmd ();
1332 break;
1333 case CK_StartExtMap1:
1334 ctl_x_cmd ();
1335 break;
1336 case CK_SuspendCmd:
1337 suspend_cmd ();
1338 break;
1339 case CK_SwapCmd:
1340 swap_cmd ();
1341 break;
1342 case CK_SymlinkCmd:
1343 link_cmd (LINK_SYMLINK_ABSOLUTE);
1344 break;
1345 case CK_ToggleListingCmd:
1346 toggle_listing_cmd ();
1347 break;
1348 case CK_ToggleShowHidden:
1349 toggle_show_hidden ();
1350 break;
1351 case CK_TogglePanelsSplit:
1352 toggle_panels_split ();
1353 break;
1354 case CK_TreeCmd:
1355 tree_cmd ();
1356 break;
1357 case CK_TreeBoxCmd:
1358 treebox_cmd ();
1359 break;
1360 #ifdef USE_EXT2FSLIB
1361 case CK_UndeleteCmd:
1362 undelete_cmd ();
1363 break;
1364 #endif
1365 case CK_UnselectCmd:
1366 unselect_cmd ();
1367 break;
1368 case CK_UserMenuCmd:
1369 user_file_menu_cmd ();
1370 break;
1371 case CK_ViewCmd:
1372 view_cmd ();
1373 break;
1374 case CK_ViewFileCmd:
1375 view_file_cmd ();
1376 break;
1377 default:
1378 res = MSG_NOT_HANDLED;
1381 return res;
1384 static void
1385 init_xterm_support (void)
1387 const char *termvalue;
1389 termvalue = getenv ("TERM");
1390 if (!termvalue || !(*termvalue))
1392 fputs (_("The TERM environment variable is unset!\n"), stderr);
1393 exit (EXIT_FAILURE);
1396 /* Check mouse capabilities */
1397 xmouse_seq = tty_tgetstr ("Km");
1399 if (strcmp (termvalue, "cygwin") == 0)
1401 mc_args__force_xterm = 1;
1402 use_mouse_p = MOUSE_DISABLED;
1405 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1406 || strncmp (termvalue, "konsole", 7) == 0
1407 || strncmp (termvalue, "rxvt", 4) == 0
1408 || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0)
1410 xterm_flag = 1;
1412 /* Default to the standard xterm sequence */
1413 if (!xmouse_seq)
1415 xmouse_seq = ESC_STR "[M";
1418 /* Enable mouse unless explicitly disabled by --nomouse */
1419 if (use_mouse_p != MOUSE_DISABLED)
1421 const char *color_term = getenv ("COLORTERM");
1422 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1423 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1424 strcmp (termvalue, "Eterm") == 0)
1426 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1428 else
1430 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1436 static void
1437 setup_mc (void)
1439 #ifdef HAVE_SLANG
1440 tty_display_8bit (full_eight_bits != 0);
1441 #else
1442 tty_display_8bit (eight_bit_clean != 0);
1443 #endif
1445 #ifdef HAVE_SUBSHELL_SUPPORT
1446 if (use_subshell)
1447 add_select_channel (subshell_pty, load_prompt, 0);
1448 #endif /* !HAVE_SUBSHELL_SUPPORT */
1450 tty_setup_sigwinch (sigwinch_handler);
1452 if ((tty_baudrate () < 9600) || tty_is_slow ())
1453 verbose = 0;
1455 init_xterm_support ();
1456 init_mouse ();
1459 static void
1460 setup_dummy_mc (void)
1462 char d[MC_MAXPATHLEN];
1463 int ret;
1465 mc_get_current_wd (d, MC_MAXPATHLEN);
1466 setup_mc ();
1467 ret = mc_chdir (d);
1470 static void
1471 check_codeset ()
1473 const char *current_system_codepage = NULL;
1475 current_system_codepage = str_detect_termencoding ();
1477 #ifdef HAVE_CHARSET
1479 const char *_display_codepage;
1481 _display_codepage = get_codepage_id (display_codepage);
1483 if (!strcmp (_display_codepage, current_system_codepage))
1485 utf8_display = str_isutf8 (current_system_codepage);
1486 return;
1489 display_codepage = get_codepage_index (current_system_codepage);
1490 if (display_codepage == -1)
1492 display_codepage = 0;
1495 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
1497 #endif
1498 utf8_display = str_isutf8 (current_system_codepage);
1501 static void
1502 done_screen (void)
1504 if (!(quit & SUBSHELL_EXIT))
1505 clr_scr ();
1506 tty_reset_shell_mode ();
1507 tty_noraw_mode ();
1508 tty_keypad (FALSE);
1509 tty_colors_done ();
1512 static void
1513 done_mc (void)
1515 disable_mouse ();
1517 /* Setup shutdown
1519 * We sync the profiles since the hotlist may have changed, while
1520 * we only change the setup data if we have the auto save feature set
1523 if (auto_save_setup)
1524 save_setup (); /* does also call save_hotlist */
1525 else
1527 save_hotlist ();
1528 save_panel_types ();
1530 done_screen ();
1531 vfs_add_current_stamps ();
1534 /* This should be called after destroy_dlg since panel widgets
1535 * save their state on the profiles
1537 static void
1538 done_mc_profile (void)
1540 done_setup ();
1543 static cb_ret_t
1544 midnight_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1546 unsigned long command;
1548 switch (msg)
1550 case DLG_INIT:
1551 setup_panels ();
1552 return MSG_HANDLED;
1554 case DLG_DRAW:
1555 load_hint (1);
1556 /* We handle the special case of the output lines */
1557 if (console_flag && output_lines)
1558 show_console_contents (output_start_y,
1559 LINES - output_lines - keybar_visible -
1560 1, LINES - keybar_visible - 1);
1561 return MSG_HANDLED;
1563 case DLG_RESIZE:
1564 setup_panels ();
1565 menubar_arrange (the_menubar);
1566 return MSG_HANDLED;
1568 case DLG_IDLE:
1569 /* We only need the first idle event to show user menu after start */
1570 set_idle_proc (h, 0);
1572 if (boot_current_is_left)
1573 dlg_select_widget (get_panel_widget (0));
1574 else
1575 dlg_select_widget (get_panel_widget (1));
1577 if (auto_menu)
1578 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1579 return MSG_HANDLED;
1581 case DLG_KEY:
1582 if (ctl_x_map_enabled)
1584 ctl_x_map_enabled = FALSE;
1585 command = lookup_keymap_command (main_x_map, parm);
1586 if (command != CK_Ignore_Key)
1587 return midnight_execute_cmd (NULL, command);
1590 /* FIXME: should handle all menu shortcuts before this point */
1591 if (the_menubar->is_active)
1592 return MSG_NOT_HANDLED;
1594 if (parm == '\t')
1595 free_completions (cmdline);
1597 if (parm == '\n')
1599 size_t i;
1601 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1602 cmdline->buffer[i] == '\t'); i++)
1604 if (cmdline->buffer[i])
1606 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1607 return MSG_HANDLED;
1609 stuff (cmdline, "", 0);
1610 cmdline->point = 0;
1613 /* Ctrl-Enter and Alt-Enter */
1614 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1616 copy_prog_name ();
1617 return MSG_HANDLED;
1620 /* Ctrl-Shift-Enter */
1621 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1623 copy_current_pathname ();
1624 copy_prog_name ();
1625 return MSG_HANDLED;
1628 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1629 && !quote && !current_panel->searching)
1631 if (!only_leading_plus_minus)
1633 /* Special treatement, since the input line will eat them */
1634 if (parm == '+')
1636 select_cmd ();
1637 return MSG_HANDLED;
1640 if (parm == '\\' || parm == '-')
1642 unselect_cmd ();
1643 return MSG_HANDLED;
1646 if (parm == '*')
1648 reverse_selection_cmd ();
1649 return MSG_HANDLED;
1652 else if (!command_prompt || !cmdline->buffer[0])
1654 /* Special treatement '+', '-', '\', '*' only when this is
1655 * first char on input line
1658 if (parm == '+')
1660 select_cmd ();
1661 return MSG_HANDLED;
1664 if (parm == '\\' || parm == '-')
1666 unselect_cmd ();
1667 return MSG_HANDLED;
1670 if (parm == '*')
1672 reverse_selection_cmd ();
1673 return MSG_HANDLED;
1677 return MSG_NOT_HANDLED;
1679 case DLG_HOTKEY_HANDLED:
1680 if ((get_current_type () == view_listing) && current_panel->searching)
1682 current_panel->searching = 0;
1683 current_panel->dirty = 1;
1685 return MSG_HANDLED;
1687 case DLG_UNHANDLED_KEY:
1688 if (command_prompt)
1690 cb_ret_t v;
1692 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1693 if (v == MSG_HANDLED)
1694 return MSG_HANDLED;
1697 if (ctl_x_map_enabled)
1699 ctl_x_map_enabled = FALSE;
1700 command = lookup_keymap_command (main_x_map, parm);
1702 else
1703 command = lookup_keymap_command (main_map, parm);
1705 return (command == CK_Ignore_Key) ? MSG_NOT_HANDLED : midnight_execute_cmd (NULL, command);
1707 case DLG_POST_KEY:
1708 if (!the_menubar->is_active)
1709 update_dirty_panels ();
1710 return MSG_HANDLED;
1712 case DLG_ACTION:
1713 /* shortcut */
1714 if (sender == NULL)
1715 midnight_execute_cmd (NULL, parm);
1716 /* message from menu */
1717 else if (sender == (Widget *) the_menubar)
1718 midnight_execute_cmd (sender, parm);
1719 /* message from buttonbar */
1720 else if (sender == (Widget *) the_bar)
1722 if (data == NULL)
1723 midnight_execute_cmd (sender, parm);
1724 else
1725 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1727 return MSG_HANDLED;
1729 default:
1730 return default_dlg_callback (h, sender, msg, parm, data);
1734 /* Show current directory in the xterm title */
1735 void
1736 update_xterm_title_path (void)
1738 const char *path;
1739 char host[BUF_TINY];
1740 char *p;
1741 struct passwd *pw = NULL;
1742 char *login = NULL;
1743 int res = 0;
1744 if (xterm_flag && xterm_title)
1746 path = strip_home_and_password (current_panel->cwd);
1747 res = gethostname (host, sizeof (host));
1748 if (res)
1749 { /* On success, res = 0 */
1750 host[0] = '\0';
1752 else
1754 host[sizeof (host) - 1] = '\0';
1756 pw = getpwuid (getuid ());
1757 if (pw)
1759 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1761 else
1763 login = g_strdup (host);
1765 p = g_strdup_printf ("mc [%s]:%s", login, path);
1766 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1767 g_free (login);
1768 g_free (p);
1769 if (!alternate_plus_minus)
1770 numeric_keypad_mode ();
1771 fflush (stdout);
1776 * Load new hint and display it.
1777 * IF force is not 0, ignore the timeout.
1779 void
1780 load_hint (int force)
1782 char *hint;
1784 if (!the_hint->widget.parent)
1785 return;
1787 if (!message_visible)
1789 label_set_text (the_hint, NULL);
1790 return;
1793 hint = get_random_hint (force);
1795 if (hint != NULL)
1797 if (*hint)
1798 set_hintbar (hint);
1799 g_free (hint);
1801 else
1803 char text[BUF_SMALL];
1805 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1806 set_hintbar (text);
1810 static void
1811 create_panels_and_run_mc (void)
1813 midnight_dlg->get_shortcut = midnight_get_shortcut;
1815 create_panels ();
1817 add_widget (midnight_dlg, the_menubar);
1818 init_menu ();
1820 add_widget (midnight_dlg, get_panel_widget (0));
1821 add_widget (midnight_dlg, get_panel_widget (1));
1823 add_widget (midnight_dlg, the_hint);
1824 add_widget (midnight_dlg, cmdline);
1825 add_widget (midnight_dlg, the_prompt);
1827 add_widget (midnight_dlg, the_bar);
1828 midnight_set_buttonbar (the_bar);
1830 /* Run the Midnight Commander if no file was specified in the command line */
1831 run_dlg (midnight_dlg);
1834 /* result must be free'd (I think this should go in util.c) */
1835 static char *
1836 prepend_cwd_on_local (const char *filename)
1838 char *d;
1839 size_t l;
1841 if (!vfs_file_is_local (filename) || g_path_is_absolute (filename))
1842 return g_strdup (filename);
1844 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1845 mc_get_current_wd (d, MC_MAXPATHLEN);
1846 l = strlen (d);
1847 d[l++] = PATH_SEP;
1848 strcpy (d + l, filename);
1849 canonicalize_pathname (d);
1850 return d;
1853 /* Invoke the internal view/edit routine with:
1854 * the default processing and forcing the internal viewer/editor
1856 static void
1857 mc_maybe_editor_or_viewer (void)
1859 switch (mc_run_mode)
1861 #ifdef USE_INTERNAL_EDIT
1862 case MC_RUN_EDITOR:
1863 edit_file (mc_run_param0, mc_args__edit_start_line);
1864 break;
1865 #endif /* USE_INTERNAL_EDIT */
1866 case MC_RUN_VIEWER:
1868 char *path;
1869 path = prepend_cwd_on_local (mc_run_param0);
1870 view_file (path, 0, 1);
1871 g_free (path);
1872 break;
1874 #ifdef USE_DIFF_VIEW
1875 case MC_RUN_DIFFVIEWER:
1876 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1877 break;
1878 #endif /* USE_DIFF_VIEW */
1879 default:
1880 break;
1882 midnight_shutdown = 1;
1883 done_mc ();
1886 /* Run the main dialog that occupies the whole screen */
1887 static void
1888 do_nc (void)
1890 int midnight_colors[DLG_COLOR_NUM];
1891 midnight_colors[0] = mc_skin_color_get ("dialog", "_default_");
1892 midnight_colors[1] = mc_skin_color_get ("dialog", "focus");
1893 midnight_colors[2] = mc_skin_color_get ("dialog", "hotnormal");
1894 midnight_colors[3] = mc_skin_color_get ("dialog", "hotfocus");
1896 panel_init ();
1898 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1899 "[main]", NULL, DLG_WANT_IDLE);
1901 if (mc_run_mode == MC_RUN_FULL)
1902 setup_mc ();
1903 else
1904 setup_dummy_mc ();
1906 /* start check display_codepage and source_codepage */
1907 check_codeset ();
1909 /* Check if we were invoked as an editor or file viewer */
1910 if (mc_run_mode != MC_RUN_FULL)
1911 mc_maybe_editor_or_viewer ();
1912 else
1914 create_panels_and_run_mc ();
1916 /* Program end */
1917 midnight_shutdown = 1;
1919 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1920 if (mc_args__last_wd_file && vfs_current_is_local ())
1921 last_wd_string = g_strdup (current_panel->cwd);
1923 done_mc ();
1926 destroy_dlg (midnight_dlg);
1927 panel_deinit ();
1928 current_panel = 0;
1929 done_mc_profile ();
1932 /* POSIX version. The only version we support. */
1933 static void
1934 OS_Setup (void)
1936 const char *shell_env = getenv ("SHELL");
1937 const char *mc_libdir;
1939 if ((shell_env == NULL) || (shell_env[0] == '\0'))
1941 struct passwd *pwd;
1942 pwd = getpwuid (geteuid ());
1943 if (pwd != NULL)
1944 shell = g_strdup (pwd->pw_shell);
1946 else
1947 shell = g_strdup (shell_env);
1949 if ((shell == NULL) || (shell[0] == '\0'))
1951 g_free (shell);
1952 shell = g_strdup ("/bin/sh");
1955 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1956 /* and can be overriden by the MC_DATADIR environment variable */
1957 mc_libdir = getenv ("MC_DATADIR");
1958 if (mc_libdir != NULL)
1960 mc_home = g_strdup (mc_libdir);
1961 mc_home_alt = g_strdup (SYSCONFDIR);
1963 else
1965 mc_home = g_strdup (SYSCONFDIR);
1966 mc_home_alt = g_strdup (DATADIR);
1969 /* This variable is used by the subshell */
1970 home_dir = getenv ("HOME");
1972 if (!home_dir)
1973 home_dir = mc_home;
1976 static void
1977 sigchld_handler_no_subshell (int sig)
1979 #ifdef __linux__
1980 int pid, status;
1982 if (!console_flag)
1983 return;
1985 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1986 the value of console_flag never changed, we could simply not install
1987 this handler at all if (!console_flag && !use_subshell). */
1989 /* That comment is no longer true. We need to wait() on a sigchld
1990 handler (that's at least what the tarfs code expects currently). */
1992 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1994 if (pid == cons_saver_pid)
1997 if (WIFSTOPPED (status))
1999 /* Someone has stopped cons.saver - restart it */
2000 kill (pid, SIGCONT);
2002 else
2004 /* cons.saver has died - disable console saving */
2005 handle_console (CONSOLE_DONE);
2006 console_flag = 0;
2009 /* If we got here, some other child exited; ignore it */
2010 #endif /* __linux__ */
2012 (void) sig;
2015 static void
2016 init_sigchld (void)
2018 struct sigaction sigchld_action;
2020 sigchld_action.sa_handler =
2021 #ifdef HAVE_SUBSHELL_SUPPORT
2022 use_subshell ? sigchld_handler :
2023 #endif /* HAVE_SUBSHELL_SUPPORT */
2024 sigchld_handler_no_subshell;
2026 sigemptyset (&sigchld_action.sa_mask);
2028 #ifdef SA_RESTART
2029 sigchld_action.sa_flags = SA_RESTART;
2030 #else
2031 sigchld_action.sa_flags = 0;
2032 #endif /* !SA_RESTART */
2034 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
2036 #ifdef HAVE_SUBSHELL_SUPPORT
2038 * This may happen on QNX Neutrino 6, where SA_RESTART
2039 * is defined but not implemented. Fallback to no subshell.
2041 use_subshell = 0;
2042 #endif /* HAVE_SUBSHELL_SUPPORT */
2047 main (int argc, char *argv[])
2049 struct stat s;
2050 char *mc_dir;
2051 GError *error = NULL;
2052 gboolean isInitialized;
2054 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2055 setlocale (LC_ALL, "");
2056 bindtextdomain ("mc", LOCALEDIR);
2057 textdomain ("mc");
2059 /* Set up temporary directory */
2060 mc_tmpdir ();
2062 OS_Setup ();
2064 str_init_strings (NULL);
2066 vfs_init ();
2068 #ifdef USE_INTERNAL_EDIT
2069 edit_stack_init ();
2070 #endif
2072 #ifdef HAVE_SLANG
2073 SLtt_Ignore_Beep = 1;
2074 #endif
2076 if (!mc_args_handle (argc, argv, "mc"))
2077 exit (EXIT_FAILURE);
2079 /* NOTE: This has to be called before tty_init or whatever routine
2080 calls any define_sequence */
2081 init_key ();
2083 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2084 handle_console (CONSOLE_INIT);
2086 #ifdef HAVE_SUBSHELL_SUPPORT
2087 /* Don't use subshell when invoked as viewer or editor */
2088 if (mc_run_mode != MC_RUN_FULL)
2089 use_subshell = 0;
2091 if (use_subshell)
2092 subshell_get_console_attributes ();
2093 #endif /* HAVE_SUBSHELL_SUPPORT */
2095 /* Install the SIGCHLD handler; must be done before init_subshell() */
2096 init_sigchld ();
2098 /* We need this, since ncurses endwin () doesn't restore the signals */
2099 save_stop_handler ();
2101 /* Must be done before init_subshell, to set up the terminal size: */
2102 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2103 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2105 load_setup ();
2107 /* Removing this from the X code let's us type C-c */
2108 load_key_defs ();
2110 load_keymap_defs ();
2112 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2114 isInitialized = mc_skin_init (&error);
2116 mc_filehighlight = mc_fhl_new (TRUE);
2118 dlg_set_default_colors ();
2120 if (!isInitialized)
2122 message (D_ERROR, _("Warning"), "%s", error->message);
2123 g_error_free (error);
2124 error = NULL;
2127 /* create home directory */
2128 /* do it after the screen library initialization to show the error message */
2129 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2130 canonicalize_pathname (mc_dir);
2131 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
2132 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
2133 g_free (mc_dir);
2135 #ifdef HAVE_SUBSHELL_SUPPORT
2136 /* Done here to ensure that the subshell doesn't */
2137 /* inherit the file descriptors opened below, etc */
2138 if (use_subshell)
2139 init_subshell ();
2141 #endif /* HAVE_SUBSHELL_SUPPORT */
2143 /* Also done after init_subshell, to save any shell init file messages */
2144 if (console_flag)
2145 handle_console (CONSOLE_SAVE);
2147 if (alternate_plus_minus)
2148 application_keypad_mode ();
2150 #ifdef HAVE_SUBSHELL_SUPPORT
2151 if (use_subshell)
2153 mc_prompt = strip_ctrl_codes (subshell_prompt);
2154 if (mc_prompt == NULL)
2155 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2157 else
2158 #endif /* HAVE_SUBSHELL_SUPPORT */
2159 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2161 /* Program main loop */
2162 if (!midnight_shutdown)
2163 do_nc ();
2165 /* Save the tree store */
2166 tree_store_save ();
2168 free_keymap_defs ();
2170 /* Virtual File System shutdown */
2171 vfs_shut ();
2173 flush_extension_file (); /* does only free memory */
2175 mc_fhl_free (&mc_filehighlight);
2176 mc_skin_deinit ();
2178 tty_shutdown ();
2180 if (console_flag && !(quit & SUBSHELL_EXIT))
2181 handle_console (CONSOLE_RESTORE);
2182 if (alternate_plus_minus)
2183 numeric_keypad_mode ();
2185 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2187 if (console_flag)
2188 handle_console (CONSOLE_DONE);
2189 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2191 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string && !print_last_revert)
2193 int last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2194 S_IRUSR | S_IWUSR);
2196 if (last_wd_fd != -1)
2198 ssize_t ret1;
2199 int ret2;
2200 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2201 ret2 = close (last_wd_fd);
2204 g_free (last_wd_string);
2206 g_free (mc_home_alt);
2207 g_free (mc_home);
2208 g_free (shell);
2210 done_key ();
2211 #ifdef HAVE_CHARSET
2212 free_codepages_list ();
2213 g_free (autodetect_codeset);
2214 #endif
2215 str_uninit_strings ();
2217 g_free (mc_run_param0);
2218 g_free (mc_run_param1);
2220 #ifdef USE_INTERNAL_EDIT
2221 edit_stack_free ();
2222 #endif
2224 return 0;