Ticket #30 (use external clipboard utility)
[midnight-commander.git] / src / main.c
blob83be833f7a17faf555b6aec92b92e8e7d7f98e40
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 /* path to X clipboard utility */
260 char* clipbord_store_path = NULL;
261 char* clipbord_paste_path = NULL;
263 /* If set, then print to the given file the last directory we were at */
264 static char *last_wd_string = NULL;
265 /* Set to 1 to suppress printing the last directory */
266 static int print_last_revert = 0;
268 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
269 char *mc_run_param0 = NULL;
270 char *mc_run_param1 = NULL;
272 /* Used so that widgets know if they are being destroyed or
273 shut down */
274 int midnight_shutdown = 0;
276 /* The user's shell */
277 char *shell = NULL;
279 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
280 char *mc_home = NULL;
282 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
283 char *mc_home_alt = NULL;
285 /* Define this function for glib-style error handling */
286 GQuark
287 mc_main_error_quark (void)
289 return g_quark_from_static_string (PACKAGE);
292 /* Save current stat of directories to avoid reloading the panels */
293 /* when no modifications have taken place */
294 void
295 save_cwds_stat (void)
297 if (panels_options.fast_reload)
299 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
300 if (get_other_type () == view_listing)
301 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
305 #ifdef HAVE_SUBSHELL_SUPPORT
306 void
307 do_update_prompt (void)
309 if (update_prompt)
311 printf ("\r\n%s", subshell_prompt);
312 fflush (stdout);
313 update_prompt = 0;
316 #endif /* HAVE_SUBSHELL_SUPPORT */
318 void
319 change_panel (void)
321 free_completions (cmdline);
322 dlg_one_down (midnight_dlg);
325 /* Stop MC main dialog and the current dialog if it exists.
326 * Needed to provide fast exit from MC viewer or editor on shell exit */
327 static void
328 stop_dialogs (void)
330 midnight_dlg->running = 0;
331 if (current_dlg)
333 current_dlg->running = 0;
337 static int
338 quit_cmd_internal (int quiet)
340 int q = quit;
342 if (quiet || !confirm_exit)
344 q = 1;
346 else
348 if (query_dialog
349 (_("The Midnight Commander"),
350 _("Do you really want to quit the Midnight Commander?"), D_NORMAL,
351 2, _("&Yes"), _("&No")) == 0)
352 q = 1;
354 if (q)
356 #ifdef HAVE_SUBSHELL_SUPPORT
357 if (!use_subshell)
358 stop_dialogs ();
359 else if ((q = exit_subshell ()))
360 #endif
361 stop_dialogs ();
363 if (q)
364 quit |= 1;
365 return quit;
368 static void
369 quit_cmd (void)
371 quit_cmd_internal (0);
374 void
375 quiet_quit_cmd (void)
377 print_last_revert = 1;
378 quit_cmd_internal (1);
381 /* Wrapper for do_subshell_chdir, check for availability of subshell */
382 void
383 subshell_chdir (const char *directory)
385 #ifdef HAVE_SUBSHELL_SUPPORT
386 if (use_subshell)
388 if (vfs_current_is_local ())
389 do_subshell_chdir (directory, 0, 1);
391 #endif /* HAVE_SUBSHELL_SUPPORT */
394 void
395 directory_history_add (struct WPanel *panel, const char *dir)
397 char *tmp;
399 tmp = g_strdup (dir);
400 strip_password (tmp, 1);
402 panel->dir_history = list_append_unique (panel->dir_history, tmp);
406 * If we moved to the parent directory move the selection pointer to
407 * the old directory name; If we leave VFS dir, remove FS specificator.
409 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
411 static const char *
412 get_parent_dir_name (const char *cwd, const char *lwd)
414 size_t llen, clen;
416 llen = strlen (lwd);
417 clen = strlen (cwd);
419 if (llen > clen)
421 const char *p;
423 p = strrchr (lwd, PATH_SEP);
425 if ((p != NULL)
426 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
427 && (clen == (size_t) (p - lwd)
428 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
429 return (p + 1);
432 return NULL;
436 * Changes the current directory of the panel.
437 * Don't record change in the directory history.
439 static int
440 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
442 const char *directory;
443 char *olddir;
444 char temp[MC_MAXPATHLEN];
445 char *translated_url;
447 if (cd_type == cd_parse_command)
449 while (*new_dir == ' ')
450 new_dir++;
453 olddir = g_strdup (panel->cwd);
454 new_dir = translated_url = vfs_translate_url (new_dir);
456 /* Convert *new_path to a suitable pathname, handle ~user */
458 if (cd_type == cd_parse_command)
460 if (!strcmp (new_dir, "-"))
462 strcpy (temp, panel->lwd);
463 new_dir = temp;
466 directory = *new_dir ? new_dir : home_dir;
468 if (mc_chdir (directory) == -1)
470 strcpy (panel->cwd, olddir);
471 g_free (olddir);
472 g_free (translated_url);
473 return 0;
475 g_free (translated_url);
477 /* Success: save previous directory, shutdown status of previous dir */
478 strcpy (panel->lwd, olddir);
479 free_completions (cmdline);
481 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
483 vfs_release_path (olddir);
485 subshell_chdir (panel->cwd);
487 /* Reload current panel */
488 panel_clean_dir (panel);
489 panel->count =
490 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
491 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
492 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
493 load_hint (0);
494 panel->dirty = 1;
495 update_xterm_title_path ();
497 g_free (olddir);
499 return 1;
503 * Changes the current directory of the panel.
504 * Record change in the directory history.
507 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
509 int r;
511 r = _do_panel_cd (panel, new_dir, cd_type);
512 if (r)
513 directory_history_add (panel, panel->cwd);
514 return r;
518 do_cd (const char *new_dir, enum cd_enum exact)
520 return (do_panel_cd (current_panel, new_dir, exact));
523 void
524 directory_history_next (WPanel * panel)
526 GList *nextdir;
528 nextdir = g_list_next (panel->dir_history);
530 if (!nextdir)
531 return;
533 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
534 panel->dir_history = nextdir;
537 void
538 directory_history_prev (WPanel * panel)
540 GList *prevdir;
542 prevdir = g_list_previous (panel->dir_history);
544 if (!prevdir)
545 return;
547 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
548 panel->dir_history = prevdir;
551 void
552 directory_history_list (WPanel * panel)
554 char *s;
556 s = show_hist (&panel->dir_history, &panel->widget);
558 if (s != NULL)
560 if (_do_panel_cd (panel, s, cd_exact))
561 directory_history_add (panel, panel->cwd);
562 else
563 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
564 g_free (s);
568 #ifdef HAVE_SUBSHELL_SUPPORT
570 load_prompt (int fd, void *unused)
572 (void) fd;
573 (void) unused;
575 if (!read_subshell_prompt ())
576 return 0;
578 /* Don't actually change the prompt if it's invisible */
579 if (current_dlg == midnight_dlg && command_prompt)
581 char *tmp_prompt;
582 int prompt_len;
584 tmp_prompt = strip_ctrl_codes (subshell_prompt);
585 prompt_len = str_term_width1 (tmp_prompt);
587 /* Check for prompts too big */
588 if (COLS > 8 && prompt_len > COLS - 8)
590 tmp_prompt[COLS - 8] = '\0';
591 prompt_len = COLS - 8;
593 mc_prompt = tmp_prompt;
594 label_set_text (the_prompt, mc_prompt);
595 winput_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
597 /* since the prompt has changed, and we are called from one of the
598 * tty_get_event channels, the prompt updating does not take place
599 * automatically: force a cursor update and a screen refresh
601 update_cursor (midnight_dlg);
602 mc_refresh ();
604 update_prompt = 1;
605 return 0;
607 #endif /* HAVE_SUBSHELL_SUPPORT */
609 void
610 sort_cmd (void)
612 WPanel *p;
613 const panel_field_t *sort_order;
615 if (!SELECTED_IS_PANEL)
616 return;
618 p = MENU_PANEL;
619 sort_order = sort_box (p->current_sort_field, &p->reverse, &p->case_sensitive, &p->exec_first);
621 panel_set_sort_order (p, sort_order);
625 static void
626 treebox_cmd (void)
628 char *sel_dir;
630 sel_dir = tree_box (selection (current_panel)->fname);
631 if (sel_dir)
633 do_cd (sel_dir, cd_exact);
634 g_free (sel_dir);
638 #ifdef LISTMODE_EDITOR
639 static void
640 listmode_cmd (void)
642 char *newmode;
644 if (get_current_type () != view_listing)
645 return;
647 newmode = listmode_edit (current_panel->user_format);
648 if (!newmode)
649 return;
651 g_free (current_panel->user_format);
652 current_panel->list_type = list_user;
653 current_panel->user_format = newmode;
654 set_panel_formats (current_panel);
656 do_refresh ();
658 #endif /* LISTMODE_EDITOR */
660 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
661 static GList *
662 create_panel_menu (void)
664 GList *entries = NULL;
666 entries = g_list_append (entries, menu_entry_create (_("File listin&g"), CK_ListingCmd));
667 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
668 entries = g_list_append (entries, menu_entry_create (_("&Info"), CK_InfoCmd));
669 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
670 entries = g_list_append (entries, menu_separator_create ());
671 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ChangeListingCmd));
672 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
673 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
674 #ifdef HAVE_CHARSET
675 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
676 #endif
677 #ifdef USE_NETCODE
678 entries = g_list_append (entries, menu_separator_create ());
679 #ifdef ENABLE_VFS_MCFS
680 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
681 #endif
682 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
683 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
684 #ifdef ENABLE_VFS_SMB
685 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
686 #endif /* ENABLE_VFS_SMB */
687 #endif /* USE_NETCODE */
688 entries = g_list_append (entries, menu_separator_create ());
689 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
691 return entries;
694 static GList *
695 create_file_menu (void)
697 GList *entries = NULL;
699 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
700 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
701 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
702 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
703 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
704 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
705 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
706 entries = g_list_append (entries, menu_entry_create (_("&Symlink"), CK_SymlinkCmd));
707 entries = g_list_append (entries, menu_entry_create (_("Relative symlin&k"), CK_RelativeSymlinkCmd));
708 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
709 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
710 entries =
711 g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
712 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
713 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
714 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
715 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
716 entries = g_list_append (entries, menu_separator_create ());
717 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
718 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
719 entries =
720 g_list_append (entries,
721 menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
722 entries = g_list_append (entries, menu_separator_create ());
723 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
725 return entries;
728 static GList *
729 create_command_menu (void)
731 /* I know, I'm lazy, but the tree widget when it's not running
732 * as a panel still has some problems, I have not yet finished
733 * the WTree widget port, sorry.
735 GList *entries = NULL;
737 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
738 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
739 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
740 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
741 entries =
742 g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
743 entries =
744 g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
745 #ifdef USE_DIFF_VIEW
746 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
747 #endif
748 entries =
749 g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
750 entries =
751 g_list_append (entries,
752 menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
753 entries = g_list_append (entries, menu_separator_create ());
754 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
755 entries =
756 g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
757 #ifdef ENABLE_VFS
758 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
759 #endif
760 #ifdef WITH_BACKGROUND
761 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
762 #endif
763 entries = g_list_append (entries, menu_separator_create ());
764 #ifdef USE_EXT2FSLIB
765 entries =
766 g_list_append (entries,
767 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
768 #endif
769 #ifdef LISTMODE_EDITOR
770 entries =
771 g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
772 #endif
773 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
774 entries = g_list_append (entries, menu_separator_create ());
775 #endif
776 entries =
777 g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
778 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
779 entries =
780 g_list_append (entries,
781 menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
783 return entries;
786 static GList *
787 create_options_menu (void)
789 GList *entries = NULL;
791 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
792 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutBox));
793 entries = g_list_append (entries, menu_entry_create (_("&Panel options..."), CK_PanelOptionsBox));
794 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
795 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
796 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
797 #ifdef ENABLE_VFS
798 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
799 #endif
800 entries = g_list_append (entries, menu_separator_create ());
801 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
803 return entries;
806 void
807 init_menu (void)
809 menubar_add_menu (the_menubar,
810 create_menu (horizontal_split ? _("&Above") : _("&Left"),
811 create_panel_menu (), "[Left and Right Menus]"));
812 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
813 menubar_add_menu (the_menubar,
814 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
815 menubar_add_menu (the_menubar,
816 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
817 menubar_add_menu (the_menubar,
818 create_menu (horizontal_split ? _("&Below") : _("&Right"),
819 create_panel_menu (), "[Left and Right Menus]"));
822 void
823 done_menu (void)
825 menubar_set_menu (the_menubar, NULL);
828 static void
829 menu_last_selected_cmd (void)
831 the_menubar->is_active = TRUE;
832 the_menubar->is_dropped = (drop_menus != 0);
833 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
834 dlg_select_widget (the_menubar);
837 static void
838 menu_cmd (void)
840 if (the_menubar->is_active)
841 return;
843 if ((get_current_index () == 0) == (current_panel->active != 0))
844 the_menubar->selected = 0;
845 else
846 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
847 menu_last_selected_cmd ();
850 static char *
851 midnight_get_shortcut (unsigned long command)
853 const char *ext_map;
854 const char *shortcut = NULL;
856 shortcut = lookup_keymap_shortcut (main_map, command);
857 if (shortcut != NULL)
858 return g_strdup (shortcut);
860 shortcut = lookup_keymap_shortcut (panel_map, command);
861 if (shortcut != NULL)
862 return g_strdup (shortcut);
864 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
865 if (ext_map != NULL)
866 shortcut = lookup_keymap_shortcut (main_x_map, command);
867 if (shortcut != NULL)
868 return g_strdup_printf ("%s %s", ext_map, shortcut);
870 return NULL;
873 void
874 toggle_show_hidden (void)
876 panels_options.show_dot_files = !panels_options.show_dot_files;
877 update_panels (UP_RELOAD, UP_KEEPSEL);
880 static void
881 toggle_panels_split (void)
883 horizontal_split = !horizontal_split;
884 layout_change ();
885 do_refresh ();
889 * Just a hack for allowing url-like pathnames to be accepted from the
890 * command line.
892 static void
893 translated_mc_chdir (char *dir)
895 char *newdir;
896 int ret;
898 newdir = vfs_translate_url (dir);
899 ret = mc_chdir (newdir);
900 g_free (newdir);
903 static void
904 create_panels (void)
906 int current_index;
907 int other_index;
908 panel_view_mode_t current_mode, other_mode;
909 char original_dir[BUF_1K] = "\0";
911 if (boot_current_is_left)
913 current_index = 0;
914 other_index = 1;
915 current_mode = startup_left_mode;
916 other_mode = startup_right_mode;
918 else
920 current_index = 1;
921 other_index = 0;
922 current_mode = startup_right_mode;
923 other_mode = startup_left_mode;
925 /* Creates the left panel */
926 if (mc_run_param0 != NULL)
928 if (mc_run_param1 != NULL)
930 /* Ok, user has specified two dirs, save the original one,
931 * since we may not be able to chdir to the proper
932 * second directory later
934 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
936 translated_mc_chdir (mc_run_param0);
938 set_display_type (current_index, current_mode);
940 /* The other panel */
941 if (mc_run_param1 != NULL)
943 if (original_dir[0] != '\0')
944 translated_mc_chdir (original_dir);
945 translated_mc_chdir (mc_run_param1);
947 set_display_type (other_index, other_mode);
949 if (startup_left_mode == view_listing)
951 current_panel = left_panel;
953 else
955 if (right_panel)
956 current_panel = right_panel;
957 else
958 current_panel = left_panel;
961 /* Create the nice widgets */
962 cmdline = command_new (0, 0, 0);
963 the_prompt = label_new (0, 0, mc_prompt);
964 the_prompt->transparent = 1;
965 the_bar = buttonbar_new (keybar_visible);
967 the_hint = label_new (0, 0, 0);
968 the_hint->transparent = 1;
969 the_hint->auto_adjust_cols = 0;
970 the_hint->widget.cols = COLS;
972 the_menubar = menubar_new (0, 0, COLS, NULL);
975 static void
976 copy_current_pathname (void)
978 char *cwd_path;
979 if (!command_prompt)
980 return;
982 cwd_path = remove_encoding_from_path (current_panel->cwd);
983 command_insert (cmdline, cwd_path, 0);
985 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
986 command_insert (cmdline, PATH_SEP_STR, 0);
987 g_free (cwd_path);
990 static void
991 copy_other_pathname (void)
993 char *cwd_path;
995 if (get_other_type () != view_listing)
996 return;
998 if (!command_prompt)
999 return;
1001 cwd_path = remove_encoding_from_path (other_panel->cwd);
1002 command_insert (cmdline, cwd_path, 0);
1004 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
1005 command_insert (cmdline, PATH_SEP_STR, 0);
1006 g_free (cwd_path);
1009 static void
1010 copy_readlink (WPanel * panel)
1012 if (!command_prompt)
1013 return;
1014 if (S_ISLNK (selection (panel)->st.st_mode))
1016 char buffer[MC_MAXPATHLEN];
1017 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1018 int i;
1020 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1021 g_free (p);
1022 if (i > 0)
1024 buffer[i] = 0;
1025 command_insert (cmdline, buffer, 1);
1030 static void
1031 copy_current_readlink (void)
1033 copy_readlink (current_panel);
1036 static void
1037 copy_other_readlink (void)
1039 if (get_other_type () == view_listing)
1040 copy_readlink (other_panel);
1043 /* Insert the selected file name into the input line */
1044 static void
1045 copy_prog_name (void)
1047 char *tmp;
1048 if (!command_prompt)
1049 return;
1051 if (get_current_type () == view_tree)
1053 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1054 tmp = tree_selected_name (tree);
1056 else
1057 tmp = selection (current_panel)->fname;
1059 command_insert (cmdline, tmp, 1);
1062 static void
1063 copy_tagged (WPanel * panel)
1065 int i;
1067 if (!command_prompt)
1068 return;
1069 input_disable_update (cmdline);
1070 if (panel->marked)
1072 for (i = 0; i < panel->count; i++)
1074 if (panel->dir.list[i].f.marked)
1075 command_insert (cmdline, panel->dir.list[i].fname, 1);
1078 else
1080 command_insert (cmdline, panel->dir.list[panel->selected].fname, 1);
1082 input_enable_update (cmdline);
1085 static void
1086 copy_current_tagged (void)
1088 copy_tagged (current_panel);
1091 static void
1092 copy_other_tagged (void)
1094 if (get_other_type () == view_listing)
1095 copy_tagged (other_panel);
1098 void
1099 midnight_set_buttonbar (WButtonBar * b)
1101 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1102 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1103 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1104 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1105 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1106 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1107 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1108 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1109 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1110 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1113 static gboolean ctl_x_map_enabled = FALSE;
1115 static void
1116 ctl_x_cmd (void)
1118 ctl_x_map_enabled = TRUE;
1121 static cb_ret_t
1122 midnight_execute_cmd (Widget * sender, unsigned long command)
1124 cb_ret_t res = MSG_HANDLED;
1126 (void) sender;
1128 switch (command)
1130 case CK_AddHotlist:
1131 add2hotlist_cmd ();
1132 break;
1133 case CK_ChangeListingCmd:
1134 change_listing_cmd ();
1135 break;
1136 case CK_ChmodCmd:
1137 chmod_cmd ();
1138 break;
1139 case CK_ChownCmd:
1140 chown_cmd ();
1141 break;
1142 case CK_ChownAdvancedCmd:
1143 chown_advanced_cmd ();
1144 break;
1145 case CK_CompareDirsCmd:
1146 compare_dirs_cmd ();
1147 break;
1148 case CK_ConfigureBox:
1149 configure_box ();
1150 break;
1151 #ifdef ENABLE_VFS
1152 case CK_ConfigureVfs:
1153 configure_vfs ();
1154 break;
1155 #endif
1156 case CK_ConfirmBox:
1157 confirm_box ();
1158 break;
1159 case CK_CopyCmd:
1160 copy_cmd ();
1161 break;
1162 case CK_CopyCurrentPathname:
1163 copy_current_pathname ();
1164 break;
1165 case CK_CopyCurrentReadlink:
1166 copy_current_readlink ();
1167 break;
1168 case CK_CopyCurrentTagged:
1169 copy_current_tagged ();
1170 break;
1171 case CK_CopyOtherPathname:
1172 copy_other_pathname ();
1173 break;
1174 case CK_CopyOtherReadlink:
1175 copy_other_readlink ();
1176 break;
1177 case CK_CopyOtherTagged:
1178 copy_other_tagged ();
1179 break;
1180 case CK_DeleteCmd:
1181 delete_cmd ();
1182 break;
1183 #ifdef USE_DIFF_VIEW
1184 case CK_DiffViewCmd:
1185 diff_view_cmd ();
1186 break;
1187 #endif
1188 case CK_DisplayBitsBox:
1189 display_bits_box ();
1190 break;
1191 case CK_EditCmd:
1192 edit_cmd ();
1193 break;
1194 case CK_EditExtFileCmd:
1195 ext_cmd ();
1196 break;
1197 case CK_EditFhlFileCmd:
1198 edit_fhl_cmd ();
1199 break;
1200 case CK_EditMcMenuCmd:
1201 edit_mc_menu_cmd ();
1202 break;
1203 case CK_EditSymlinkCmd:
1204 edit_symlink_cmd ();
1205 break;
1206 case CK_ExternalPanelize:
1207 external_panelize ();
1208 break;
1209 case CK_FilterCmd:
1210 filter_cmd ();
1211 break;
1212 case CK_FilteredViewCmd:
1213 filtered_view_cmd ();
1214 break;
1215 case CK_FindCmd:
1216 find_cmd ();
1217 break;
1218 #if defined (USE_NETCODE)
1219 case CK_FishlinkCmd:
1220 fishlink_cmd ();
1221 break;
1222 case CK_FtplinkCmd:
1223 ftplink_cmd ();
1224 break;
1225 #endif
1226 case CK_HelpCmd:
1227 help_cmd ();
1228 break;
1229 case CK_HistoryCmd:
1230 history_cmd ();
1231 break;
1232 case CK_InfoCmd:
1233 if (sender == (Widget *) the_menubar)
1234 info_cmd (); /* mwnu */
1235 else
1236 info_cmd_no_menu (); /* shortcut or buttonbar */
1237 break;
1238 #ifdef WITH_BACKGROUND
1239 case CK_JobsCmd:
1240 jobs_cmd ();
1241 break;
1242 #endif
1243 case CK_LayoutBox:
1244 layout_box ();
1245 break;
1246 case CK_LearnKeys:
1247 learn_keys ();
1248 break;
1249 case CK_LinkCmd:
1250 link_cmd (LINK_HARDLINK);
1251 break;
1252 case CK_ListingCmd:
1253 listing_cmd ();
1254 break;
1255 #ifdef LISTMODE_EDITOR
1256 case CK_ListmodeCmd:
1257 listmode_cmd ();
1258 break;
1259 #endif
1260 case CK_MenuCmd:
1261 menu_cmd ();
1262 break;
1263 case CK_MenuLastSelectedCmd:
1264 menu_last_selected_cmd ();
1265 break;
1266 case CK_MkdirCmd:
1267 mkdir_cmd ();
1268 break;
1269 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1270 case CK_NetlinkCmd:
1271 netlink_cmd ();
1272 break;
1273 #endif
1274 case CK_PanelOptionsBox:
1275 panel_options_box ();
1276 break;
1277 #ifdef HAVE_CHARSET
1278 case CK_PanelSetPanelEncoding:
1279 encoding_cmd ();
1280 break;
1281 #endif
1282 case CK_QuickCdCmd:
1283 quick_cd_cmd ();
1284 break;
1285 case CK_QuickChdirCmd:
1286 quick_chdir_cmd ();
1287 break;
1288 case CK_QuickViewCmd:
1289 if (sender == (Widget *) the_menubar)
1290 quick_view_cmd (); /* menu */
1291 else
1292 quick_cmd_no_menu (); /* shortcut or buttonabr */
1293 break;
1294 case CK_QuietQuitCmd:
1295 quiet_quit_cmd ();
1296 break;
1297 case CK_QuitCmd:
1298 quit_cmd ();
1299 break;
1300 case CK_RelativeSymlinkCmd:
1301 link_cmd (LINK_SYMLINK_RELATIVE);
1302 break;
1303 case CK_RenameCmd:
1304 rename_cmd ();
1305 break;
1306 case CK_RereadCmd:
1307 reread_cmd ();
1308 break;
1309 #ifdef ENABLE_VFS
1310 case CK_ReselectVfs:
1311 reselect_vfs ();
1312 break;
1313 #endif
1314 case CK_ReverseSelectionCmd:
1315 reverse_selection_cmd ();
1316 break;
1317 case CK_SaveSetupCmd:
1318 save_setup_cmd ();
1319 break;
1320 case CK_SelectCmd:
1321 select_cmd ();
1322 break;
1323 case CK_ShowCommandLine:
1324 view_other_cmd ();
1325 break;
1326 case CK_SingleDirsizeCmd:
1327 smart_dirsize_cmd ();
1328 break;
1329 #if defined (USE_NETCODE) && defined (ENABLE_VFS_SMB)
1330 case CK_SmblinkCmd:
1331 smblink_cmd ();
1332 break;
1333 #endif /* USE_NETCODE && ENABLE_VFS_SMB */
1334 case CK_Sort:
1335 sort_cmd ();
1336 break;
1337 case CK_StartExtMap1:
1338 ctl_x_cmd ();
1339 break;
1340 case CK_SuspendCmd:
1341 suspend_cmd ();
1342 break;
1343 case CK_SwapCmd:
1344 swap_cmd ();
1345 break;
1346 case CK_SymlinkCmd:
1347 link_cmd (LINK_SYMLINK_ABSOLUTE);
1348 break;
1349 case CK_ToggleListingCmd:
1350 toggle_listing_cmd ();
1351 break;
1352 case CK_ToggleShowHidden:
1353 toggle_show_hidden ();
1354 break;
1355 case CK_TogglePanelsSplit:
1356 toggle_panels_split ();
1357 break;
1358 case CK_TreeCmd:
1359 tree_cmd ();
1360 break;
1361 case CK_TreeBoxCmd:
1362 treebox_cmd ();
1363 break;
1364 #ifdef USE_EXT2FSLIB
1365 case CK_UndeleteCmd:
1366 undelete_cmd ();
1367 break;
1368 #endif
1369 case CK_UnselectCmd:
1370 unselect_cmd ();
1371 break;
1372 case CK_UserMenuCmd:
1373 user_file_menu_cmd ();
1374 break;
1375 case CK_ViewCmd:
1376 view_cmd ();
1377 break;
1378 case CK_ViewFileCmd:
1379 view_file_cmd ();
1380 break;
1381 default:
1382 res = MSG_NOT_HANDLED;
1385 return res;
1388 static void
1389 init_xterm_support (void)
1391 const char *termvalue;
1393 termvalue = getenv ("TERM");
1394 if (!termvalue || !(*termvalue))
1396 fputs (_("The TERM environment variable is unset!\n"), stderr);
1397 exit (EXIT_FAILURE);
1400 /* Check mouse capabilities */
1401 xmouse_seq = tty_tgetstr ("Km");
1403 if (strcmp (termvalue, "cygwin") == 0)
1405 mc_args__force_xterm = 1;
1406 use_mouse_p = MOUSE_DISABLED;
1409 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1410 || strncmp (termvalue, "konsole", 7) == 0
1411 || strncmp (termvalue, "rxvt", 4) == 0
1412 || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0)
1414 xterm_flag = 1;
1416 /* Default to the standard xterm sequence */
1417 if (!xmouse_seq)
1419 xmouse_seq = ESC_STR "[M";
1422 /* Enable mouse unless explicitly disabled by --nomouse */
1423 if (use_mouse_p != MOUSE_DISABLED)
1425 const char *color_term = getenv ("COLORTERM");
1426 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1427 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1428 strcmp (termvalue, "Eterm") == 0)
1430 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1432 else
1434 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1440 static void
1441 setup_mc (void)
1443 #ifdef HAVE_SLANG
1444 tty_display_8bit (full_eight_bits != 0);
1445 #else
1446 tty_display_8bit (eight_bit_clean != 0);
1447 #endif
1449 #ifdef HAVE_SUBSHELL_SUPPORT
1450 if (use_subshell)
1451 add_select_channel (subshell_pty, load_prompt, 0);
1452 #endif /* !HAVE_SUBSHELL_SUPPORT */
1454 tty_setup_sigwinch (sigwinch_handler);
1456 if ((tty_baudrate () < 9600) || tty_is_slow ())
1457 verbose = 0;
1459 init_xterm_support ();
1460 init_mouse ();
1463 static void
1464 setup_dummy_mc (void)
1466 char d[MC_MAXPATHLEN];
1467 int ret;
1469 mc_get_current_wd (d, MC_MAXPATHLEN);
1470 setup_mc ();
1471 ret = mc_chdir (d);
1474 static void
1475 check_codeset ()
1477 const char *current_system_codepage = NULL;
1479 current_system_codepage = str_detect_termencoding ();
1481 #ifdef HAVE_CHARSET
1483 const char *_display_codepage;
1485 _display_codepage = get_codepage_id (display_codepage);
1487 if (!strcmp (_display_codepage, current_system_codepage))
1489 utf8_display = str_isutf8 (current_system_codepage);
1490 return;
1493 display_codepage = get_codepage_index (current_system_codepage);
1494 if (display_codepage == -1)
1496 display_codepage = 0;
1499 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
1501 #endif
1502 utf8_display = str_isutf8 (current_system_codepage);
1505 static void
1506 done_screen (void)
1508 if (!(quit & SUBSHELL_EXIT))
1509 clr_scr ();
1510 tty_reset_shell_mode ();
1511 tty_noraw_mode ();
1512 tty_keypad (FALSE);
1513 tty_colors_done ();
1516 static void
1517 done_mc (void)
1519 disable_mouse ();
1521 /* Setup shutdown
1523 * We sync the profiles since the hotlist may have changed, while
1524 * we only change the setup data if we have the auto save feature set
1527 if (auto_save_setup)
1528 save_setup (); /* does also call save_hotlist */
1529 else
1531 save_hotlist ();
1532 save_panel_types ();
1534 done_screen ();
1535 vfs_add_current_stamps ();
1538 /* This should be called after destroy_dlg since panel widgets
1539 * save their state on the profiles
1541 static void
1542 done_mc_profile (void)
1544 done_setup ();
1547 static cb_ret_t
1548 midnight_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1550 unsigned long command;
1552 switch (msg)
1554 case DLG_INIT:
1555 setup_panels ();
1556 return MSG_HANDLED;
1558 case DLG_DRAW:
1559 load_hint (1);
1560 /* We handle the special case of the output lines */
1561 if (console_flag && output_lines)
1562 show_console_contents (output_start_y,
1563 LINES - output_lines - keybar_visible -
1564 1, LINES - keybar_visible - 1);
1565 return MSG_HANDLED;
1567 case DLG_RESIZE:
1568 setup_panels ();
1569 menubar_arrange (the_menubar);
1570 return MSG_HANDLED;
1572 case DLG_IDLE:
1573 /* We only need the first idle event to show user menu after start */
1574 set_idle_proc (h, 0);
1576 if (boot_current_is_left)
1577 dlg_select_widget (get_panel_widget (0));
1578 else
1579 dlg_select_widget (get_panel_widget (1));
1581 if (auto_menu)
1582 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1583 return MSG_HANDLED;
1585 case DLG_KEY:
1586 if (ctl_x_map_enabled)
1588 ctl_x_map_enabled = FALSE;
1589 command = lookup_keymap_command (main_x_map, parm);
1590 if (command != CK_Ignore_Key)
1591 return midnight_execute_cmd (NULL, command);
1594 /* FIXME: should handle all menu shortcuts before this point */
1595 if (the_menubar->is_active)
1596 return MSG_NOT_HANDLED;
1598 if (parm == '\t')
1599 free_completions (cmdline);
1601 if (parm == '\n')
1603 size_t i;
1605 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1606 cmdline->buffer[i] == '\t'); i++)
1608 if (cmdline->buffer[i])
1610 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1611 return MSG_HANDLED;
1613 stuff (cmdline, "", 0);
1614 cmdline->point = 0;
1617 /* Ctrl-Enter and Alt-Enter */
1618 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1620 copy_prog_name ();
1621 return MSG_HANDLED;
1624 /* Ctrl-Shift-Enter */
1625 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1627 copy_current_pathname ();
1628 copy_prog_name ();
1629 return MSG_HANDLED;
1632 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1633 && !quote && !current_panel->searching)
1635 if (!only_leading_plus_minus)
1637 /* Special treatement, since the input line will eat them */
1638 if (parm == '+')
1640 select_cmd ();
1641 return MSG_HANDLED;
1644 if (parm == '\\' || parm == '-')
1646 unselect_cmd ();
1647 return MSG_HANDLED;
1650 if (parm == '*')
1652 reverse_selection_cmd ();
1653 return MSG_HANDLED;
1656 else if (!command_prompt || !cmdline->buffer[0])
1658 /* Special treatement '+', '-', '\', '*' only when this is
1659 * first char on input line
1662 if (parm == '+')
1664 select_cmd ();
1665 return MSG_HANDLED;
1668 if (parm == '\\' || parm == '-')
1670 unselect_cmd ();
1671 return MSG_HANDLED;
1674 if (parm == '*')
1676 reverse_selection_cmd ();
1677 return MSG_HANDLED;
1681 return MSG_NOT_HANDLED;
1683 case DLG_HOTKEY_HANDLED:
1684 if ((get_current_type () == view_listing) && current_panel->searching)
1686 current_panel->searching = 0;
1687 current_panel->dirty = 1;
1689 return MSG_HANDLED;
1691 case DLG_UNHANDLED_KEY:
1692 if (command_prompt)
1694 cb_ret_t v;
1696 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1697 if (v == MSG_HANDLED)
1698 return MSG_HANDLED;
1701 if (ctl_x_map_enabled)
1703 ctl_x_map_enabled = FALSE;
1704 command = lookup_keymap_command (main_x_map, parm);
1706 else
1707 command = lookup_keymap_command (main_map, parm);
1709 return (command == CK_Ignore_Key) ? MSG_NOT_HANDLED : midnight_execute_cmd (NULL, command);
1711 case DLG_POST_KEY:
1712 if (!the_menubar->is_active)
1713 update_dirty_panels ();
1714 return MSG_HANDLED;
1716 case DLG_ACTION:
1717 /* shortcut */
1718 if (sender == NULL)
1719 midnight_execute_cmd (NULL, parm);
1720 /* message from menu */
1721 else if (sender == (Widget *) the_menubar)
1722 midnight_execute_cmd (sender, parm);
1723 /* message from buttonbar */
1724 else if (sender == (Widget *) the_bar)
1726 if (data == NULL)
1727 midnight_execute_cmd (sender, parm);
1728 else
1729 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1731 return MSG_HANDLED;
1733 default:
1734 return default_dlg_callback (h, sender, msg, parm, data);
1738 /* Show current directory in the xterm title */
1739 void
1740 update_xterm_title_path (void)
1742 const char *path;
1743 char host[BUF_TINY];
1744 char *p;
1745 struct passwd *pw = NULL;
1746 char *login = NULL;
1747 int res = 0;
1748 if (xterm_flag && xterm_title)
1750 path = strip_home_and_password (current_panel->cwd);
1751 res = gethostname (host, sizeof (host));
1752 if (res)
1753 { /* On success, res = 0 */
1754 host[0] = '\0';
1756 else
1758 host[sizeof (host) - 1] = '\0';
1760 pw = getpwuid (getuid ());
1761 if (pw)
1763 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1765 else
1767 login = g_strdup (host);
1769 p = g_strdup_printf ("mc [%s]:%s", login, path);
1770 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1771 g_free (login);
1772 g_free (p);
1773 if (!alternate_plus_minus)
1774 numeric_keypad_mode ();
1775 fflush (stdout);
1780 * Load new hint and display it.
1781 * IF force is not 0, ignore the timeout.
1783 void
1784 load_hint (int force)
1786 char *hint;
1788 if (!the_hint->widget.parent)
1789 return;
1791 if (!message_visible)
1793 label_set_text (the_hint, NULL);
1794 return;
1797 hint = get_random_hint (force);
1799 if (hint != NULL)
1801 if (*hint)
1802 set_hintbar (hint);
1803 g_free (hint);
1805 else
1807 char text[BUF_SMALL];
1809 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1810 set_hintbar (text);
1814 static void
1815 create_panels_and_run_mc (void)
1817 midnight_dlg->get_shortcut = midnight_get_shortcut;
1819 create_panels ();
1821 add_widget (midnight_dlg, the_menubar);
1822 init_menu ();
1824 add_widget (midnight_dlg, get_panel_widget (0));
1825 add_widget (midnight_dlg, get_panel_widget (1));
1827 add_widget (midnight_dlg, the_hint);
1828 add_widget (midnight_dlg, cmdline);
1829 add_widget (midnight_dlg, the_prompt);
1831 add_widget (midnight_dlg, the_bar);
1832 midnight_set_buttonbar (the_bar);
1834 /* Run the Midnight Commander if no file was specified in the command line */
1835 run_dlg (midnight_dlg);
1838 /* result must be free'd (I think this should go in util.c) */
1839 static char *
1840 prepend_cwd_on_local (const char *filename)
1842 char *d;
1843 size_t l;
1845 if (!vfs_file_is_local (filename) || g_path_is_absolute (filename))
1846 return g_strdup (filename);
1848 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1849 mc_get_current_wd (d, MC_MAXPATHLEN);
1850 l = strlen (d);
1851 d[l++] = PATH_SEP;
1852 strcpy (d + l, filename);
1853 canonicalize_pathname (d);
1854 return d;
1857 /* Invoke the internal view/edit routine with:
1858 * the default processing and forcing the internal viewer/editor
1860 static void
1861 mc_maybe_editor_or_viewer (void)
1863 switch (mc_run_mode)
1865 #ifdef USE_INTERNAL_EDIT
1866 case MC_RUN_EDITOR:
1867 edit_file (mc_run_param0, mc_args__edit_start_line);
1868 break;
1869 #endif /* USE_INTERNAL_EDIT */
1870 case MC_RUN_VIEWER:
1872 char *path;
1873 path = prepend_cwd_on_local (mc_run_param0);
1874 view_file (path, 0, 1);
1875 g_free (path);
1876 break;
1878 #ifdef USE_DIFF_VIEW
1879 case MC_RUN_DIFFVIEWER:
1880 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1881 break;
1882 #endif /* USE_DIFF_VIEW */
1883 default:
1884 break;
1886 midnight_shutdown = 1;
1887 done_mc ();
1890 /* Run the main dialog that occupies the whole screen */
1891 static void
1892 do_nc (void)
1894 int midnight_colors[DLG_COLOR_NUM];
1895 midnight_colors[0] = mc_skin_color_get ("dialog", "_default_");
1896 midnight_colors[1] = mc_skin_color_get ("dialog", "focus");
1897 midnight_colors[2] = mc_skin_color_get ("dialog", "hotnormal");
1898 midnight_colors[3] = mc_skin_color_get ("dialog", "hotfocus");
1900 panel_init ();
1902 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1903 "[main]", NULL, DLG_WANT_IDLE);
1905 if (mc_run_mode == MC_RUN_FULL)
1906 setup_mc ();
1907 else
1908 setup_dummy_mc ();
1910 /* start check display_codepage and source_codepage */
1911 check_codeset ();
1913 /* Check if we were invoked as an editor or file viewer */
1914 if (mc_run_mode != MC_RUN_FULL)
1915 mc_maybe_editor_or_viewer ();
1916 else
1918 create_panels_and_run_mc ();
1920 /* Program end */
1921 midnight_shutdown = 1;
1923 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1924 if (mc_args__last_wd_file && vfs_current_is_local ())
1925 last_wd_string = g_strdup (current_panel->cwd);
1927 done_mc ();
1930 destroy_dlg (midnight_dlg);
1931 panel_deinit ();
1932 current_panel = 0;
1933 done_mc_profile ();
1936 /* POSIX version. The only version we support. */
1937 static void
1938 OS_Setup (void)
1940 const char *shell_env = getenv ("SHELL");
1941 const char *mc_libdir;
1943 if ((shell_env == NULL) || (shell_env[0] == '\0'))
1945 struct passwd *pwd;
1946 pwd = getpwuid (geteuid ());
1947 if (pwd != NULL)
1948 shell = g_strdup (pwd->pw_shell);
1950 else
1951 shell = g_strdup (shell_env);
1953 if ((shell == NULL) || (shell[0] == '\0'))
1955 g_free (shell);
1956 shell = g_strdup ("/bin/sh");
1959 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1960 /* and can be overriden by the MC_DATADIR environment variable */
1961 mc_libdir = getenv ("MC_DATADIR");
1962 if (mc_libdir != NULL)
1964 mc_home = g_strdup (mc_libdir);
1965 mc_home_alt = g_strdup (SYSCONFDIR);
1967 else
1969 mc_home = g_strdup (SYSCONFDIR);
1970 mc_home_alt = g_strdup (DATADIR);
1973 /* This variable is used by the subshell */
1974 home_dir = getenv ("HOME");
1976 if (!home_dir)
1977 home_dir = mc_home;
1980 static void
1981 sigchld_handler_no_subshell (int sig)
1983 #ifdef __linux__
1984 int pid, status;
1986 if (!console_flag)
1987 return;
1989 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1990 the value of console_flag never changed, we could simply not install
1991 this handler at all if (!console_flag && !use_subshell). */
1993 /* That comment is no longer true. We need to wait() on a sigchld
1994 handler (that's at least what the tarfs code expects currently). */
1996 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1998 if (pid == cons_saver_pid)
2001 if (WIFSTOPPED (status))
2003 /* Someone has stopped cons.saver - restart it */
2004 kill (pid, SIGCONT);
2006 else
2008 /* cons.saver has died - disable console saving */
2009 handle_console (CONSOLE_DONE);
2010 console_flag = 0;
2013 /* If we got here, some other child exited; ignore it */
2014 #endif /* __linux__ */
2016 (void) sig;
2019 static void
2020 init_sigchld (void)
2022 struct sigaction sigchld_action;
2024 sigchld_action.sa_handler =
2025 #ifdef HAVE_SUBSHELL_SUPPORT
2026 use_subshell ? sigchld_handler :
2027 #endif /* HAVE_SUBSHELL_SUPPORT */
2028 sigchld_handler_no_subshell;
2030 sigemptyset (&sigchld_action.sa_mask);
2032 #ifdef SA_RESTART
2033 sigchld_action.sa_flags = SA_RESTART;
2034 #else
2035 sigchld_action.sa_flags = 0;
2036 #endif /* !SA_RESTART */
2038 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
2040 #ifdef HAVE_SUBSHELL_SUPPORT
2042 * This may happen on QNX Neutrino 6, where SA_RESTART
2043 * is defined but not implemented. Fallback to no subshell.
2045 use_subshell = 0;
2046 #endif /* HAVE_SUBSHELL_SUPPORT */
2051 main (int argc, char *argv[])
2053 struct stat s;
2054 char *mc_dir;
2055 GError *error = NULL;
2056 gboolean isInitialized;
2058 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2059 setlocale (LC_ALL, "");
2060 bindtextdomain ("mc", LOCALEDIR);
2061 textdomain ("mc");
2063 /* Set up temporary directory */
2064 mc_tmpdir ();
2066 OS_Setup ();
2068 str_init_strings (NULL);
2070 vfs_init ();
2072 #ifdef USE_INTERNAL_EDIT
2073 edit_stack_init ();
2074 #endif
2076 #ifdef HAVE_SLANG
2077 SLtt_Ignore_Beep = 1;
2078 #endif
2080 if (!mc_args_handle (argc, argv, "mc"))
2081 exit (EXIT_FAILURE);
2083 /* NOTE: This has to be called before tty_init or whatever routine
2084 calls any define_sequence */
2085 init_key ();
2087 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2088 handle_console (CONSOLE_INIT);
2090 #ifdef HAVE_SUBSHELL_SUPPORT
2091 /* Don't use subshell when invoked as viewer or editor */
2092 if (mc_run_mode != MC_RUN_FULL)
2093 use_subshell = 0;
2095 if (use_subshell)
2096 subshell_get_console_attributes ();
2097 #endif /* HAVE_SUBSHELL_SUPPORT */
2099 /* Install the SIGCHLD handler; must be done before init_subshell() */
2100 init_sigchld ();
2102 /* We need this, since ncurses endwin () doesn't restore the signals */
2103 save_stop_handler ();
2105 /* Must be done before init_subshell, to set up the terminal size: */
2106 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2107 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2109 load_setup ();
2111 /* Removing this from the X code let's us type C-c */
2112 load_key_defs ();
2114 load_keymap_defs ();
2116 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2118 isInitialized = mc_skin_init (&error);
2120 mc_filehighlight = mc_fhl_new (TRUE);
2122 dlg_set_default_colors ();
2124 if (!isInitialized)
2126 message (D_ERROR, _("Warning"), "%s", error->message);
2127 g_error_free (error);
2128 error = NULL;
2131 /* create home directory */
2132 /* do it after the screen library initialization to show the error message */
2133 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2134 canonicalize_pathname (mc_dir);
2135 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
2136 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
2137 g_free (mc_dir);
2139 #ifdef HAVE_SUBSHELL_SUPPORT
2140 /* Done here to ensure that the subshell doesn't */
2141 /* inherit the file descriptors opened below, etc */
2142 if (use_subshell)
2143 init_subshell ();
2145 #endif /* HAVE_SUBSHELL_SUPPORT */
2147 /* Also done after init_subshell, to save any shell init file messages */
2148 if (console_flag)
2149 handle_console (CONSOLE_SAVE);
2151 if (alternate_plus_minus)
2152 application_keypad_mode ();
2154 #ifdef HAVE_SUBSHELL_SUPPORT
2155 if (use_subshell)
2157 mc_prompt = strip_ctrl_codes (subshell_prompt);
2158 if (mc_prompt == NULL)
2159 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2161 else
2162 #endif /* HAVE_SUBSHELL_SUPPORT */
2163 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2165 /* Program main loop */
2166 if (!midnight_shutdown)
2167 do_nc ();
2169 /* Save the tree store */
2170 tree_store_save ();
2172 free_keymap_defs ();
2174 /* Virtual File System shutdown */
2175 vfs_shut ();
2177 flush_extension_file (); /* does only free memory */
2179 mc_fhl_free (&mc_filehighlight);
2180 mc_skin_deinit ();
2182 tty_shutdown ();
2184 if (console_flag && !(quit & SUBSHELL_EXIT))
2185 handle_console (CONSOLE_RESTORE);
2186 if (alternate_plus_minus)
2187 numeric_keypad_mode ();
2189 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2191 if (console_flag)
2192 handle_console (CONSOLE_DONE);
2193 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2195 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string && !print_last_revert)
2197 int last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2198 S_IRUSR | S_IWUSR);
2200 if (last_wd_fd != -1)
2202 ssize_t ret1;
2203 int ret2;
2204 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2205 ret2 = close (last_wd_fd);
2208 g_free (last_wd_string);
2210 g_free (mc_home_alt);
2211 g_free (mc_home);
2212 g_free (shell);
2214 done_key ();
2215 #ifdef HAVE_CHARSET
2216 free_codepages_list ();
2217 g_free (autodetect_codeset);
2218 #endif
2219 g_free (clipbord_store_path);
2220 g_free (clipbord_paste_path);
2222 str_uninit_strings ();
2224 g_free (mc_run_param0);
2225 g_free (mc_run_param1);
2227 #ifdef USE_INTERNAL_EDIT
2228 edit_stack_free ();
2229 #endif
2231 return 0;