Ticket #2021 (MarkFileDown/MarkFileUp)
[free-mc.git] / src / main.c
blob54dfb9bab1568c49a08a99cd2a9b63ab68c6f6ec
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, marking a files moves the cursor down */
152 int mark_moves_down = 1;
154 /* If true, at startup the user-menu is invoked */
155 int auto_menu = 0;
157 /* If true, then the +, - and \ keys have their special meaning only if the
158 * command line is emtpy, otherwise they behave like regular letters
160 int only_leading_plus_minus = 1;
162 int pause_after_run = pause_on_dumb_terminals;
164 /* It true saves the setup when quitting */
165 int auto_save_setup = 1;
167 #ifdef HAVE_CHARSET
169 * Don't restrict the output on the screen manager level,
170 * the translation tables take care of it.
172 #define full_eight_bits (1)
173 #define eight_bit_clean (1)
174 #else /* HAVE_CHARSET */
175 /* If true, allow characters in the range 160-255 */
176 int eight_bit_clean = 1;
178 * If true, also allow characters in the range 128-159.
179 * This is reported to break on many terminals (xterm, qansi-m).
181 int full_eight_bits = 0;
182 #endif /* !HAVE_CHARSET */
185 * If utf-8 terminal utf8_display = 1
186 * Display bits set UTF-8
189 int utf8_display = 0;
191 /* If true use the internal viewer */
192 int use_internal_view = 1;
194 /* Have we shown the fast-reload warning in the past? */
195 int fast_reload_w = 0;
197 /* Move page/item? When clicking on the top or bottom of a panel */
198 int mouse_move_pages = 1;
200 /* If true: l&r arrows are used to chdir if the input line is empty */
201 int navigate_with_arrows = 0;
203 /* The prompt */
204 const char *mc_prompt = NULL;
206 /* The widget where we draw the prompt */
207 WLabel *the_prompt;
209 /* The hint bar */
210 WLabel *the_hint;
212 /* The button bar */
213 WButtonBar *the_bar;
215 /* Mouse type: GPM, xterm or none */
216 Mouse_Type use_mouse_p = MOUSE_NONE;
218 /* If on, default for "No" in delete operations */
219 int safe_delete = 0;
221 /* Controls screen clearing before an exec */
222 int clear_before_exec = 1;
224 /* Asks for confirmation before deleting a file */
225 int confirm_delete = 1;
227 /* Asks for confirmation before deleting a hotlist entry */
228 int confirm_directory_hotlist_delete = 1;
230 /* Asks for confirmation before overwriting a file */
231 int confirm_overwrite = 1;
233 /* Asks for confirmation before executing a program by pressing enter */
234 int confirm_execute = 0;
236 /* Asks for confirmation before leaving the program */
237 int confirm_exit = 1;
239 /* Asks for confirmation before clean up of history */
240 int confirm_history_cleanup = 1;
242 /* Asks for confirmation when using F3 to view a directory and there
243 are tagged files */
244 int confirm_view_dir = 0;
246 /* This flag indicates if the pull down menus by default drop down */
247 int drop_menus = 0;
249 /* The dialog handle for the main program */
250 Dlg_head *midnight_dlg = NULL;
252 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
253 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
254 int update_prompt = 0;
256 /* The home directory */
257 const char *home_dir = NULL;
259 /* Tab size */
260 int option_tab_spacing = 8;
262 /* Only used at program boot */
263 int boot_current_is_left = 1;
265 /* If this is true, then when browsing the tree the other window will
266 * automatically reload it's directory with the contents of the currently
267 * selected directory.
269 int xtree_mode = 0;
271 /* If set, then print to the given file the last directory we were at */
272 static char *last_wd_string = NULL;
273 /* Set to 1 to suppress printing the last directory */
274 static int print_last_revert = 0;
276 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
277 char *mc_run_param0 = NULL;
278 char *mc_run_param1 = NULL;
280 /* Used so that widgets know if they are being destroyed or
281 shut down */
282 int midnight_shutdown = 0;
284 /* The user's shell */
285 char *shell = NULL;
287 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
288 char *mc_home = NULL;
290 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
291 char *mc_home_alt = NULL;
293 /* Define this function for glib-style error handling */
294 GQuark
295 mc_main_error_quark (void)
297 return g_quark_from_static_string (PACKAGE);
300 /* Save current stat of directories to avoid reloading the panels */
301 /* when no modifications have taken place */
302 void
303 save_cwds_stat (void)
305 if (fast_reload)
307 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
308 if (get_other_type () == view_listing)
309 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
313 #ifdef HAVE_SUBSHELL_SUPPORT
314 void
315 do_update_prompt (void)
317 if (update_prompt)
319 printf ("\r\n%s", subshell_prompt);
320 fflush (stdout);
321 update_prompt = 0;
324 #endif /* HAVE_SUBSHELL_SUPPORT */
326 void
327 change_panel (void)
329 free_completions (cmdline);
330 dlg_one_down (midnight_dlg);
333 /* Stop MC main dialog and the current dialog if it exists.
334 * Needed to provide fast exit from MC viewer or editor on shell exit */
335 static void
336 stop_dialogs (void)
338 midnight_dlg->running = 0;
339 if (current_dlg)
341 current_dlg->running = 0;
345 static int
346 quit_cmd_internal (int quiet)
348 int q = quit;
350 if (quiet || !confirm_exit)
352 q = 1;
354 else
356 if (query_dialog
357 (_(" The Midnight Commander "),
358 _(" Do you really want to quit the Midnight Commander? "), D_NORMAL,
359 2, _("&Yes"), _("&No")) == 0)
360 q = 1;
362 if (q)
364 #ifdef HAVE_SUBSHELL_SUPPORT
365 if (!use_subshell)
366 stop_dialogs ();
367 else if ((q = exit_subshell ()))
368 #endif
369 stop_dialogs ();
371 if (q)
372 quit |= 1;
373 return quit;
376 static void
377 quit_cmd (void)
379 quit_cmd_internal (0);
382 void
383 quiet_quit_cmd (void)
385 print_last_revert = 1;
386 quit_cmd_internal (1);
389 /* Wrapper for do_subshell_chdir, check for availability of subshell */
390 void
391 subshell_chdir (const char *directory)
393 #ifdef HAVE_SUBSHELL_SUPPORT
394 if (use_subshell)
396 if (vfs_current_is_local ())
397 do_subshell_chdir (directory, 0, 1);
399 #endif /* HAVE_SUBSHELL_SUPPORT */
402 void
403 directory_history_add (struct WPanel *panel, const char *dir)
405 char *tmp;
407 tmp = g_strdup (dir);
408 strip_password (tmp, 1);
410 panel->dir_history = list_append_unique (panel->dir_history, tmp);
414 * If we moved to the parent directory move the selection pointer to
415 * the old directory name; If we leave VFS dir, remove FS specificator.
417 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
419 static const char *
420 get_parent_dir_name (const char *cwd, const char *lwd)
422 size_t llen, clen;
424 llen = strlen (lwd);
425 clen = strlen (cwd);
427 if (llen > clen)
429 const char *p;
431 p = strrchr (lwd, PATH_SEP);
433 if ((p != NULL)
434 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
435 && (clen == (size_t) (p - lwd)
436 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
437 return (p + 1);
440 return NULL;
444 * Changes the current directory of the panel.
445 * Don't record change in the directory history.
447 static int
448 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
450 const char *directory;
451 char *olddir;
452 char temp[MC_MAXPATHLEN];
453 char *translated_url;
455 if (cd_type == cd_parse_command)
457 while (*new_dir == ' ')
458 new_dir++;
461 olddir = g_strdup (panel->cwd);
462 new_dir = translated_url = vfs_translate_url (new_dir);
464 /* Convert *new_path to a suitable pathname, handle ~user */
466 if (cd_type == cd_parse_command)
468 if (!strcmp (new_dir, "-"))
470 strcpy (temp, panel->lwd);
471 new_dir = temp;
474 directory = *new_dir ? new_dir : home_dir;
476 if (mc_chdir (directory) == -1)
478 strcpy (panel->cwd, olddir);
479 g_free (olddir);
480 g_free (translated_url);
481 return 0;
483 g_free (translated_url);
485 /* Success: save previous directory, shutdown status of previous dir */
486 strcpy (panel->lwd, olddir);
487 free_completions (cmdline);
489 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
491 vfs_release_path (olddir);
493 subshell_chdir (panel->cwd);
495 /* Reload current panel */
496 panel_clean_dir (panel);
497 panel->count =
498 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
499 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
500 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
501 load_hint (0);
502 panel->dirty = 1;
503 update_xterm_title_path ();
505 g_free (olddir);
507 return 1;
511 * Changes the current directory of the panel.
512 * Record change in the directory history.
515 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
517 int r;
519 r = _do_panel_cd (panel, new_dir, cd_type);
520 if (r)
521 directory_history_add (panel, panel->cwd);
522 return r;
526 do_cd (const char *new_dir, enum cd_enum exact)
528 return (do_panel_cd (current_panel, new_dir, exact));
531 void
532 directory_history_next (WPanel * panel)
534 GList *nextdir;
536 nextdir = g_list_next (panel->dir_history);
538 if (!nextdir)
539 return;
541 if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact))
542 panel->dir_history = nextdir;
545 void
546 directory_history_prev (WPanel * panel)
548 GList *prevdir;
550 prevdir = g_list_previous (panel->dir_history);
552 if (!prevdir)
553 return;
555 if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact))
556 panel->dir_history = prevdir;
559 void
560 directory_history_list (WPanel * panel)
562 char *s;
564 s = show_hist (&panel->dir_history, &panel->widget);
566 if (s != NULL)
568 if (_do_panel_cd (panel, s, cd_exact))
569 directory_history_add (panel, panel->cwd);
570 else
571 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
572 g_free (s);
576 #ifdef HAVE_SUBSHELL_SUPPORT
578 load_prompt (int fd, void *unused)
580 (void) fd;
581 (void) unused;
583 if (!read_subshell_prompt ())
584 return 0;
586 /* Don't actually change the prompt if it's invisible */
587 if (current_dlg == midnight_dlg && command_prompt)
589 char *tmp_prompt;
590 int prompt_len;
592 tmp_prompt = strip_ctrl_codes (subshell_prompt);
593 prompt_len = str_term_width1 (tmp_prompt);
595 /* Check for prompts too big */
596 if (COLS > 8 && prompt_len > COLS - 8)
598 tmp_prompt[COLS - 8] = '\0';
599 prompt_len = COLS - 8;
601 mc_prompt = tmp_prompt;
602 label_set_text (the_prompt, mc_prompt);
603 winput_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
605 /* since the prompt has changed, and we are called from one of the
606 * tty_get_event channels, the prompt updating does not take place
607 * automatically: force a cursor update and a screen refresh
609 update_cursor (midnight_dlg);
610 mc_refresh ();
612 update_prompt = 1;
613 return 0;
615 #endif /* HAVE_SUBSHELL_SUPPORT */
617 void
618 sort_cmd (void)
620 WPanel *p;
621 const panel_field_t *sort_order;
623 if (!SELECTED_IS_PANEL)
624 return;
626 p = MENU_PANEL;
627 sort_order = sort_box (p->current_sort_field, &p->reverse, &p->case_sensitive, &p->exec_first);
629 panel_set_sort_order (p, sort_order);
633 static void
634 treebox_cmd (void)
636 char *sel_dir;
638 sel_dir = tree_box (selection (current_panel)->fname);
639 if (sel_dir)
641 do_cd (sel_dir, cd_exact);
642 g_free (sel_dir);
646 #ifdef LISTMODE_EDITOR
647 static void
648 listmode_cmd (void)
650 char *newmode;
652 if (get_current_type () != view_listing)
653 return;
655 newmode = listmode_edit (current_panel->user_format);
656 if (!newmode)
657 return;
659 g_free (current_panel->user_format);
660 current_panel->list_type = list_user;
661 current_panel->user_format = newmode;
662 set_panel_formats (current_panel);
664 do_refresh ();
666 #endif /* LISTMODE_EDITOR */
668 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
669 static GList *
670 create_panel_menu (void)
672 GList *entries = NULL;
674 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ListingCmd));
675 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
676 entries = g_list_append (entries, menu_entry_create (_("&Info"), CK_InfoCmd));
677 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
678 entries = g_list_append (entries, menu_separator_create ());
679 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
680 entries = g_list_append (entries, menu_separator_create ());
681 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
682 #ifdef HAVE_CHARSET
683 entries = g_list_append (entries, menu_separator_create ());
684 entries =
685 g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
686 #endif
687 #ifdef USE_NETCODE
688 entries = g_list_append (entries, menu_separator_create ());
689 #ifdef ENABLE_VFS_MCFS
690 entries = g_list_append (entries, menu_entry_create (_("&Network link..."), CK_NetlinkCmd));
691 #endif
692 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
693 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
694 #ifdef ENABLE_VFS_SMB
695 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
696 #endif /* ENABLE_VFS_SMB */
697 #endif
698 entries = g_list_append (entries, menu_separator_create ());
699 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
701 return entries;
704 static GList *
705 create_file_menu (void)
707 GList *entries = NULL;
709 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
710 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
711 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
712 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
713 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
714 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
715 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
716 entries = g_list_append (entries, menu_entry_create (_("&SymLink"), CK_SymlinkCmd));
717 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
718 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
719 entries =
720 g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
721 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
722 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
723 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
724 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
725 entries = g_list_append (entries, menu_separator_create ());
726 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
727 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
728 entries =
729 g_list_append (entries,
730 menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
731 entries = g_list_append (entries, menu_separator_create ());
732 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
734 return entries;
737 static GList *
738 create_command_menu (void)
740 /* I know, I'm lazy, but the tree widget when it's not running
741 * as a panel still has some problems, I have not yet finished
742 * the WTree widget port, sorry.
744 GList *entries = NULL;
746 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
747 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
748 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
749 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
750 entries =
751 g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
752 entries =
753 g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
754 #ifdef USE_DIFF_VIEW
755 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
756 #endif
757 entries =
758 g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
759 entries =
760 g_list_append (entries,
761 menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
762 entries = g_list_append (entries, menu_separator_create ());
763 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
764 entries =
765 g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
766 #ifdef ENABLE_VFS
767 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
768 #endif
769 #ifdef WITH_BACKGROUND
770 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
771 #endif
772 entries = g_list_append (entries, menu_separator_create ());
773 #ifdef USE_EXT2FSLIB
774 entries =
775 g_list_append (entries,
776 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
777 #endif
778 #ifdef LISTMODE_EDITOR
779 entries =
780 g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
781 #endif
782 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
783 entries = g_list_append (entries, menu_separator_create ());
784 #endif
785 entries =
786 g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
787 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
788 entries =
789 g_list_append (entries,
790 menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
792 return entries;
795 static GList *
796 create_options_menu (void)
798 GList *entries = NULL;
800 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
801 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutCmd));
802 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
803 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
804 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
805 #ifdef ENABLE_VFS
806 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
807 #endif
808 entries = g_list_append (entries, menu_separator_create ());
809 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
811 return entries;
814 void
815 init_menu (void)
817 menubar_add_menu (the_menubar,
818 create_menu (horizontal_split ? _("&Above") : _("&Left"),
819 create_panel_menu (), "[Left and Right Menus]"));
820 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
821 menubar_add_menu (the_menubar,
822 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
823 menubar_add_menu (the_menubar,
824 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
825 menubar_add_menu (the_menubar,
826 create_menu (horizontal_split ? _("&Below") : _("&Right"),
827 create_panel_menu (), "[Left and Right Menus]"));
830 void
831 done_menu (void)
833 menubar_set_menu (the_menubar, NULL);
836 static void
837 menu_last_selected_cmd (void)
839 the_menubar->is_active = TRUE;
840 the_menubar->is_dropped = (drop_menus != 0);
841 the_menubar->previous_widget = midnight_dlg->current->dlg_id;
842 dlg_select_widget (the_menubar);
845 static void
846 menu_cmd (void)
848 if (the_menubar->is_active)
849 return;
851 if ((get_current_index () == 0) == (current_panel->active != 0))
852 the_menubar->selected = 0;
853 else
854 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
855 menu_last_selected_cmd ();
858 static char *
859 midnight_get_shortcut (unsigned long command)
861 const char *ext_map;
862 const char *shortcut = NULL;
864 shortcut = lookup_keymap_shortcut (main_map, command);
865 if (shortcut != NULL)
866 return g_strdup (shortcut);
868 shortcut = lookup_keymap_shortcut (panel_map, command);
869 if (shortcut != NULL)
870 return g_strdup (shortcut);
872 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
873 if (ext_map != NULL)
874 shortcut = lookup_keymap_shortcut (main_x_map, command);
875 if (shortcut != NULL)
876 return g_strdup_printf ("%s %s", ext_map, shortcut);
878 return NULL;
881 /* Flag toggling functions */
882 void
883 toggle_fast_reload (void)
885 fast_reload = !fast_reload;
886 if (fast_reload_w == 0 && fast_reload)
888 message (D_NORMAL, _(" Information "),
890 (" Using the fast reload option may not reflect the exact \n"
891 " directory contents. In this case you'll need to do a \n"
892 " manual reload of the directory. See the man page for \n"
893 " the details. "));
894 fast_reload_w = 1;
898 void
899 toggle_mix_all_files (void)
901 mix_all_files = !mix_all_files;
902 update_panels (UP_RELOAD, UP_KEEPSEL);
905 void
906 toggle_show_backup (void)
908 show_backups = !show_backups;
909 update_panels (UP_RELOAD, UP_KEEPSEL);
912 void
913 toggle_show_hidden (void)
915 show_dot_files = !show_dot_files;
916 update_panels (UP_RELOAD, UP_KEEPSEL);
919 static void
920 toggle_panels_split (void)
922 horizontal_split = !horizontal_split;
923 layout_change ();
924 do_refresh ();
927 void
928 toggle_kilobyte_si (void)
930 kilobyte_si = !kilobyte_si;
931 update_panels (UP_RELOAD, UP_KEEPSEL);
935 * Just a hack for allowing url-like pathnames to be accepted from the
936 * command line.
938 static void
939 translated_mc_chdir (char *dir)
941 char *newdir;
942 int ret;
944 newdir = vfs_translate_url (dir);
945 ret = mc_chdir (newdir);
946 g_free (newdir);
949 static void
950 create_panels (void)
952 int current_index;
953 int other_index;
954 panel_view_mode_t current_mode, other_mode;
955 char original_dir[BUF_1K] = "\0";
957 if (boot_current_is_left)
959 current_index = 0;
960 other_index = 1;
961 current_mode = startup_left_mode;
962 other_mode = startup_right_mode;
964 else
966 current_index = 1;
967 other_index = 0;
968 current_mode = startup_right_mode;
969 other_mode = startup_left_mode;
971 /* Creates the left panel */
972 if (mc_run_param0 != NULL)
974 if (mc_run_param1 != NULL)
976 /* Ok, user has specified two dirs, save the original one,
977 * since we may not be able to chdir to the proper
978 * second directory later
980 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
982 translated_mc_chdir (mc_run_param0);
984 set_display_type (current_index, current_mode);
986 /* The other panel */
987 if (mc_run_param1 != NULL)
989 if (original_dir[0] != '\0')
990 translated_mc_chdir (original_dir);
991 translated_mc_chdir (mc_run_param1);
993 set_display_type (other_index, other_mode);
995 if (startup_left_mode == view_listing)
997 current_panel = left_panel;
999 else
1001 if (right_panel)
1002 current_panel = right_panel;
1003 else
1004 current_panel = left_panel;
1007 /* Create the nice widgets */
1008 cmdline = command_new (0, 0, 0);
1009 the_prompt = label_new (0, 0, mc_prompt);
1010 the_prompt->transparent = 1;
1011 the_bar = buttonbar_new (keybar_visible);
1013 the_hint = label_new (0, 0, 0);
1014 the_hint->transparent = 1;
1015 the_hint->auto_adjust_cols = 0;
1016 the_hint->widget.cols = COLS;
1018 the_menubar = menubar_new (0, 0, COLS, NULL);
1021 static void
1022 copy_current_pathname (void)
1024 char *cwd_path;
1025 if (!command_prompt)
1026 return;
1028 cwd_path = remove_encoding_from_path (current_panel->cwd);
1029 command_insert (cmdline, cwd_path, 0);
1031 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
1032 command_insert (cmdline, PATH_SEP_STR, 0);
1033 g_free (cwd_path);
1036 static void
1037 copy_other_pathname (void)
1039 char *cwd_path;
1041 if (get_other_type () != view_listing)
1042 return;
1044 if (!command_prompt)
1045 return;
1047 cwd_path = remove_encoding_from_path (other_panel->cwd);
1048 command_insert (cmdline, cwd_path, 0);
1050 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
1051 command_insert (cmdline, PATH_SEP_STR, 0);
1052 g_free (cwd_path);
1055 static void
1056 copy_readlink (WPanel * panel)
1058 if (!command_prompt)
1059 return;
1060 if (S_ISLNK (selection (panel)->st.st_mode))
1062 char buffer[MC_MAXPATHLEN];
1063 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1064 int i;
1066 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
1067 g_free (p);
1068 if (i > 0)
1070 buffer[i] = 0;
1071 command_insert (cmdline, buffer, 1);
1076 static void
1077 copy_current_readlink (void)
1079 copy_readlink (current_panel);
1082 static void
1083 copy_other_readlink (void)
1085 if (get_other_type () == view_listing)
1086 copy_readlink (other_panel);
1089 /* Insert the selected file name into the input line */
1090 static void
1091 copy_prog_name (void)
1093 char *tmp;
1094 if (!command_prompt)
1095 return;
1097 if (get_current_type () == view_tree)
1099 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1100 tmp = tree_selected_name (tree);
1102 else
1103 tmp = selection (current_panel)->fname;
1105 command_insert (cmdline, tmp, 1);
1108 static void
1109 copy_tagged (WPanel * panel)
1111 int i;
1113 if (!command_prompt)
1114 return;
1115 input_disable_update (cmdline);
1116 if (panel->marked)
1118 for (i = 0; i < panel->count; i++)
1120 if (panel->dir.list[i].f.marked)
1121 command_insert (cmdline, panel->dir.list[i].fname, 1);
1124 else
1126 command_insert (cmdline, panel->dir.list[panel->selected].fname, 1);
1128 input_enable_update (cmdline);
1131 static void
1132 copy_current_tagged (void)
1134 copy_tagged (current_panel);
1137 static void
1138 copy_other_tagged (void)
1140 if (get_other_type () == view_listing)
1141 copy_tagged (other_panel);
1144 void
1145 midnight_set_buttonbar (WButtonBar * b)
1147 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1148 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1149 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1150 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1151 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1152 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1153 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1154 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1155 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1156 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1159 static gboolean ctl_x_map_enabled = FALSE;
1161 static void
1162 ctl_x_cmd (void)
1164 ctl_x_map_enabled = TRUE;
1167 static cb_ret_t
1168 midnight_execute_cmd (Widget * sender, unsigned long command)
1170 cb_ret_t res = MSG_HANDLED;
1172 (void) sender;
1174 switch (command)
1176 case CK_AddHotlist:
1177 add2hotlist_cmd ();
1178 break;
1179 case CK_ChmodCmd:
1180 chmod_cmd ();
1181 break;
1182 case CK_ChownCmd:
1183 chown_cmd ();
1184 break;
1185 case CK_ChownAdvancedCmd:
1186 chown_advanced_cmd ();
1187 break;
1188 case CK_CompareDirsCmd:
1189 compare_dirs_cmd ();
1190 break;
1191 case CK_ConfigureBox:
1192 configure_box ();
1193 break;
1194 #ifdef ENABLE_VFS
1195 case CK_ConfigureVfs:
1196 configure_vfs ();
1197 break;
1198 #endif
1199 case CK_ConfirmBox:
1200 confirm_box ();
1201 break;
1202 case CK_CopyCmd:
1203 copy_cmd ();
1204 break;
1205 case CK_CopyCurrentPathname:
1206 copy_current_pathname ();
1207 break;
1208 case CK_CopyCurrentReadlink:
1209 copy_current_readlink ();
1210 break;
1211 case CK_CopyCurrentTagged:
1212 copy_current_tagged ();
1213 break;
1214 case CK_CopyOtherPathname:
1215 copy_other_pathname ();
1216 break;
1217 case CK_CopyOtherReadlink:
1218 copy_other_readlink ();
1219 break;
1220 case CK_CopyOtherTagged:
1221 copy_other_tagged ();
1222 break;
1223 case CK_DeleteCmd:
1224 delete_cmd ();
1225 break;
1226 #ifdef USE_DIFF_VIEW
1227 case CK_DiffViewCmd:
1228 diff_view_cmd ();
1229 break;
1230 #endif
1231 case CK_DisplayBitsBox:
1232 display_bits_box ();
1233 break;
1234 case CK_EditCmd:
1235 edit_cmd ();
1236 break;
1237 case CK_EditExtFileCmd:
1238 ext_cmd ();
1239 break;
1240 case CK_EditFhlFileCmd:
1241 edit_fhl_cmd ();
1242 break;
1243 case CK_EditMcMenuCmd:
1244 edit_mc_menu_cmd ();
1245 break;
1246 case CK_EditSymlinkCmd:
1247 edit_symlink_cmd ();
1248 break;
1249 case CK_ExternalPanelize:
1250 external_panelize ();
1251 break;
1252 case CK_FilterCmd:
1253 filter_cmd ();
1254 break;
1255 case CK_FilteredViewCmd:
1256 filtered_view_cmd ();
1257 break;
1258 case CK_FindCmd:
1259 find_cmd ();
1260 break;
1261 #if defined (USE_NETCODE)
1262 case CK_FishlinkCmd:
1263 fishlink_cmd ();
1264 break;
1265 case CK_FtplinkCmd:
1266 ftplink_cmd ();
1267 break;
1268 #endif
1269 case CK_HelpCmd:
1270 help_cmd ();
1271 break;
1272 case CK_HistoryCmd:
1273 history_cmd ();
1274 break;
1275 case CK_InfoCmd:
1276 if (sender == (Widget *) the_menubar)
1277 info_cmd (); /* mwnu */
1278 else
1279 info_cmd_no_menu (); /* shortcut or buttonbar */
1280 break;
1281 #ifdef WITH_BACKGROUND
1282 case CK_JobsCmd:
1283 jobs_cmd ();
1284 break;
1285 #endif
1286 case CK_LayoutCmd:
1287 layout_cmd ();
1288 break;
1289 case CK_LearnKeys:
1290 learn_keys ();
1291 break;
1292 case CK_LinkCmd:
1293 link_cmd ();
1294 break;
1295 case CK_ListingCmd:
1296 listing_cmd ();
1297 break;
1298 #ifdef LISTMODE_EDITOR
1299 case CK_ListmodeCmd:
1300 listmode_cmd ();
1301 break;
1302 #endif
1303 case CK_MenuCmd:
1304 menu_cmd ();
1305 break;
1306 case CK_MenuLastSelectedCmd:
1307 menu_last_selected_cmd ();
1308 break;
1309 case CK_MkdirCmd:
1310 mkdir_cmd ();
1311 break;
1312 #if defined (USE_NETCODE) && defined (ENABLE_VFS_MCFS)
1313 case CK_NetlinkCmd:
1314 netlink_cmd ();
1315 break;
1316 #endif
1317 #ifdef HAVE_CHARSET
1318 case CK_PanelSetPanelEncoding:
1319 encoding_cmd ();
1320 break;
1321 #endif
1322 case CK_QuickCdCmd:
1323 quick_cd_cmd ();
1324 break;
1325 case CK_QuickChdirCmd:
1326 quick_chdir_cmd ();
1327 break;
1328 case CK_QuickViewCmd:
1329 if (sender == (Widget *) the_menubar)
1330 quick_view_cmd (); /* menu */
1331 else
1332 quick_cmd_no_menu (); /* shortcut or buttonabr */
1333 break;
1334 case CK_QuietQuitCmd:
1335 quiet_quit_cmd ();
1336 break;
1337 case CK_QuitCmd:
1338 quit_cmd ();
1339 break;
1340 case CK_RenameCmd:
1341 rename_cmd ();
1342 break;
1343 case CK_RereadCmd:
1344 reread_cmd ();
1345 break;
1346 #ifdef ENABLE_VFS
1347 case CK_ReselectVfs:
1348 reselect_vfs ();
1349 break;
1350 #endif
1351 case CK_ReverseSelectionCmd:
1352 reverse_selection_cmd ();
1353 break;
1354 case CK_SaveSetupCmd:
1355 save_setup_cmd ();
1356 break;
1357 case CK_SelectCmd:
1358 select_cmd ();
1359 break;
1360 case CK_ShowCommandLine:
1361 view_other_cmd ();
1362 break;
1363 case CK_SingleDirsizeCmd:
1364 smart_dirsize_cmd ();
1365 break;
1366 #if defined (USE_NETCODE) && defined (ENABLE_VFS_SMB)
1367 case CK_SmblinkCmd:
1368 smblink_cmd ();
1369 break;
1370 #endif /* USE_NETCODE && ENABLE_VFS_SMB */
1371 case CK_Sort:
1372 sort_cmd ();
1373 break;
1374 case CK_StartExtMap1:
1375 ctl_x_cmd ();
1376 break;
1377 case CK_SuspendCmd:
1378 suspend_cmd ();
1379 break;
1380 case CK_SwapCmd:
1381 swap_cmd ();
1382 break;
1383 case CK_SymlinkCmd:
1384 symlink_cmd ();
1385 break;
1386 case CK_ToggleListingCmd:
1387 toggle_listing_cmd ();
1388 break;
1389 case CK_ToggleShowHidden:
1390 toggle_show_hidden ();
1391 break;
1392 case CK_TogglePanelsSplit:
1393 toggle_panels_split ();
1394 break;
1395 case CK_TreeCmd:
1396 tree_cmd ();
1397 break;
1398 case CK_TreeBoxCmd:
1399 treebox_cmd ();
1400 break;
1401 #ifdef USE_EXT2FSLIB
1402 case CK_UndeleteCmd:
1403 undelete_cmd ();
1404 break;
1405 #endif
1406 case CK_UnselectCmd:
1407 unselect_cmd ();
1408 break;
1409 case CK_UserMenuCmd:
1410 user_file_menu_cmd ();
1411 break;
1412 case CK_ViewCmd:
1413 view_cmd ();
1414 break;
1415 case CK_ViewFileCmd:
1416 view_file_cmd ();
1417 break;
1418 default:
1419 res = MSG_NOT_HANDLED;
1422 return res;
1425 static void
1426 init_xterm_support (void)
1428 const char *termvalue;
1430 termvalue = getenv ("TERM");
1431 if (!termvalue || !(*termvalue))
1433 fputs (_("The TERM environment variable is unset!\n"), stderr);
1434 exit (EXIT_FAILURE);
1437 /* Check mouse capabilities */
1438 xmouse_seq = tty_tgetstr ("Km");
1440 if (strcmp (termvalue, "cygwin") == 0)
1442 mc_args__force_xterm = 1;
1443 use_mouse_p = MOUSE_DISABLED;
1446 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1447 || strncmp (termvalue, "konsole", 7) == 0
1448 || strncmp (termvalue, "rxvt", 4) == 0
1449 || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0)
1451 xterm_flag = 1;
1453 /* Default to the standard xterm sequence */
1454 if (!xmouse_seq)
1456 xmouse_seq = ESC_STR "[M";
1459 /* Enable mouse unless explicitly disabled by --nomouse */
1460 if (use_mouse_p != MOUSE_DISABLED)
1462 const char *color_term = getenv ("COLORTERM");
1463 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1464 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1465 strcmp (termvalue, "Eterm") == 0)
1467 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1469 else
1471 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1477 static void
1478 setup_mc (void)
1480 #ifdef HAVE_SLANG
1481 tty_display_8bit (full_eight_bits != 0);
1482 #else
1483 tty_display_8bit (eight_bit_clean != 0);
1484 #endif
1486 #ifdef HAVE_SUBSHELL_SUPPORT
1487 if (use_subshell)
1488 add_select_channel (subshell_pty, load_prompt, 0);
1489 #endif /* !HAVE_SUBSHELL_SUPPORT */
1491 tty_setup_sigwinch (sigwinch_handler);
1493 if ((tty_baudrate () < 9600) || tty_is_slow ())
1494 verbose = 0;
1496 init_xterm_support ();
1497 init_mouse ();
1500 static void
1501 setup_dummy_mc (void)
1503 char d[MC_MAXPATHLEN];
1504 int ret;
1506 mc_get_current_wd (d, MC_MAXPATHLEN);
1507 setup_mc ();
1508 ret = mc_chdir (d);
1511 static void
1512 check_codeset ()
1514 const char *current_system_codepage = NULL;
1516 current_system_codepage = str_detect_termencoding ();
1518 #ifdef HAVE_CHARSET
1520 const char *_display_codepage;
1522 _display_codepage = get_codepage_id (display_codepage);
1524 if (!strcmp (_display_codepage, current_system_codepage))
1526 utf8_display = str_isutf8 (current_system_codepage);
1527 return;
1530 display_codepage = get_codepage_index (current_system_codepage);
1531 if (display_codepage == -1)
1533 display_codepage = 0;
1536 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
1538 #endif
1539 utf8_display = str_isutf8 (current_system_codepage);
1542 static void
1543 done_screen (void)
1545 if (!(quit & SUBSHELL_EXIT))
1546 clr_scr ();
1547 tty_reset_shell_mode ();
1548 tty_noraw_mode ();
1549 tty_keypad (FALSE);
1550 tty_colors_done ();
1553 static void
1554 done_mc (void)
1556 disable_mouse ();
1558 /* Setup shutdown
1560 * We sync the profiles since the hotlist may have changed, while
1561 * we only change the setup data if we have the auto save feature set
1564 if (auto_save_setup)
1565 save_setup (); /* does also call save_hotlist */
1566 else
1568 save_hotlist ();
1569 save_panel_types ();
1571 done_screen ();
1572 vfs_add_current_stamps ();
1575 /* This should be called after destroy_dlg since panel widgets
1576 * save their state on the profiles
1578 static void
1579 done_mc_profile (void)
1581 done_setup ();
1584 static cb_ret_t
1585 midnight_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1587 unsigned long command;
1589 switch (msg)
1591 case DLG_INIT:
1592 setup_panels ();
1593 return MSG_HANDLED;
1595 case DLG_DRAW:
1596 load_hint (1);
1597 /* We handle the special case of the output lines */
1598 if (console_flag && output_lines)
1599 show_console_contents (output_start_y,
1600 LINES - output_lines - keybar_visible -
1601 1, LINES - keybar_visible - 1);
1602 return MSG_HANDLED;
1604 case DLG_RESIZE:
1605 setup_panels ();
1606 menubar_arrange (the_menubar);
1607 return MSG_HANDLED;
1609 case DLG_IDLE:
1610 /* We only need the first idle event to show user menu after start */
1611 set_idle_proc (h, 0);
1613 if (boot_current_is_left)
1614 dlg_select_widget (get_panel_widget (0));
1615 else
1616 dlg_select_widget (get_panel_widget (1));
1618 if (auto_menu)
1619 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1620 return MSG_HANDLED;
1622 case DLG_KEY:
1623 if (ctl_x_map_enabled)
1625 ctl_x_map_enabled = FALSE;
1626 command = lookup_keymap_command (main_x_map, parm);
1627 if (command != CK_Ignore_Key)
1628 return midnight_execute_cmd (NULL, command);
1631 /* FIXME: should handle all menu shortcuts before this point */
1632 if (the_menubar->is_active)
1633 return MSG_NOT_HANDLED;
1635 if (parm == '\t')
1636 free_completions (cmdline);
1638 if (parm == '\n')
1640 size_t i;
1642 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1643 cmdline->buffer[i] == '\t'); i++)
1645 if (cmdline->buffer[i])
1647 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1648 return MSG_HANDLED;
1650 stuff (cmdline, "", 0);
1651 cmdline->point = 0;
1654 /* Ctrl-Enter and Alt-Enter */
1655 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1657 copy_prog_name ();
1658 return MSG_HANDLED;
1661 /* Ctrl-Shift-Enter */
1662 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1664 copy_current_pathname ();
1665 copy_prog_name ();
1666 return MSG_HANDLED;
1669 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1670 && !quote && !current_panel->searching)
1672 if (!only_leading_plus_minus)
1674 /* Special treatement, since the input line will eat them */
1675 if (parm == '+')
1677 select_cmd ();
1678 return MSG_HANDLED;
1681 if (parm == '\\' || parm == '-')
1683 unselect_cmd ();
1684 return MSG_HANDLED;
1687 if (parm == '*')
1689 reverse_selection_cmd ();
1690 return MSG_HANDLED;
1693 else if (!command_prompt || !cmdline->buffer[0])
1695 /* Special treatement '+', '-', '\', '*' only when this is
1696 * first char on input line
1699 if (parm == '+')
1701 select_cmd ();
1702 return MSG_HANDLED;
1705 if (parm == '\\' || parm == '-')
1707 unselect_cmd ();
1708 return MSG_HANDLED;
1711 if (parm == '*')
1713 reverse_selection_cmd ();
1714 return MSG_HANDLED;
1718 return MSG_NOT_HANDLED;
1720 case DLG_HOTKEY_HANDLED:
1721 if ((get_current_type () == view_listing) && current_panel->searching)
1723 current_panel->searching = 0;
1724 current_panel->dirty = 1;
1726 return MSG_HANDLED;
1728 case DLG_UNHANDLED_KEY:
1729 if (command_prompt)
1731 cb_ret_t v;
1733 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1734 if (v == MSG_HANDLED)
1735 return MSG_HANDLED;
1738 if (ctl_x_map_enabled)
1740 ctl_x_map_enabled = FALSE;
1741 command = lookup_keymap_command (main_x_map, parm);
1743 else
1744 command = lookup_keymap_command (main_map, parm);
1746 return (command == CK_Ignore_Key) ? MSG_NOT_HANDLED : midnight_execute_cmd (NULL, command);
1748 case DLG_POST_KEY:
1749 if (!the_menubar->is_active)
1750 update_dirty_panels ();
1751 return MSG_HANDLED;
1753 case DLG_ACTION:
1754 /* shortcut */
1755 if (sender == NULL)
1756 midnight_execute_cmd (NULL, parm);
1757 /* message from menu */
1758 else if (sender == (Widget *) the_menubar)
1759 midnight_execute_cmd (sender, parm);
1760 /* message from buttonbar */
1761 else if (sender == (Widget *) the_bar)
1763 if (data == NULL)
1764 midnight_execute_cmd (sender, parm);
1765 else
1766 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1768 return MSG_HANDLED;
1770 default:
1771 return default_dlg_callback (h, sender, msg, parm, data);
1775 /* Show current directory in the xterm title */
1776 void
1777 update_xterm_title_path (void)
1779 const char *path;
1780 char host[BUF_TINY];
1781 char *p;
1782 struct passwd *pw = NULL;
1783 char *login = NULL;
1784 int res = 0;
1785 if (xterm_flag && xterm_title)
1787 path = strip_home_and_password (current_panel->cwd);
1788 res = gethostname (host, sizeof (host));
1789 if (res)
1790 { /* On success, res = 0 */
1791 host[0] = '\0';
1793 else
1795 host[sizeof (host) - 1] = '\0';
1797 pw = getpwuid (getuid ());
1798 if (pw)
1800 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1802 else
1804 login = g_strdup (host);
1806 p = g_strdup_printf ("mc [%s]:%s", login, path);
1807 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1808 g_free (login);
1809 g_free (p);
1810 if (!alternate_plus_minus)
1811 numeric_keypad_mode ();
1812 fflush (stdout);
1817 * Load new hint and display it.
1818 * IF force is not 0, ignore the timeout.
1820 void
1821 load_hint (int force)
1823 char *hint;
1825 if (!the_hint->widget.parent)
1826 return;
1828 if (!message_visible)
1830 label_set_text (the_hint, NULL);
1831 return;
1834 hint = get_random_hint (force);
1836 if (hint != NULL)
1838 if (*hint)
1839 set_hintbar (hint);
1840 g_free (hint);
1842 else
1844 char text[BUF_SMALL];
1846 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1847 set_hintbar (text);
1851 static void
1852 create_panels_and_run_mc (void)
1854 midnight_dlg->get_shortcut = midnight_get_shortcut;
1856 create_panels ();
1858 add_widget (midnight_dlg, the_menubar);
1859 init_menu ();
1861 add_widget (midnight_dlg, get_panel_widget (0));
1862 add_widget (midnight_dlg, get_panel_widget (1));
1864 add_widget (midnight_dlg, the_hint);
1865 add_widget (midnight_dlg, cmdline);
1866 add_widget (midnight_dlg, the_prompt);
1868 add_widget (midnight_dlg, the_bar);
1869 midnight_set_buttonbar (the_bar);
1871 /* Run the Midnight Commander if no file was specified in the command line */
1872 run_dlg (midnight_dlg);
1875 /* result must be free'd (I think this should go in util.c) */
1876 static char *
1877 prepend_cwd_on_local (const char *filename)
1879 char *d;
1880 size_t l;
1882 if (!vfs_file_is_local (filename) || g_path_is_absolute (filename))
1883 return g_strdup (filename);
1885 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1886 mc_get_current_wd (d, MC_MAXPATHLEN);
1887 l = strlen (d);
1888 d[l++] = PATH_SEP;
1889 strcpy (d + l, filename);
1890 canonicalize_pathname (d);
1891 return d;
1894 /* Invoke the internal view/edit routine with:
1895 * the default processing and forcing the internal viewer/editor
1897 static void
1898 mc_maybe_editor_or_viewer (void)
1900 switch (mc_run_mode)
1902 #ifdef USE_INTERNAL_EDIT
1903 case MC_RUN_EDITOR:
1904 edit_file (mc_run_param0, mc_args__edit_start_line);
1905 break;
1906 #endif /* USE_INTERNAL_EDIT */
1907 case MC_RUN_VIEWER:
1909 char *path;
1910 path = prepend_cwd_on_local (mc_run_param0);
1911 view_file (path, 0, 1);
1912 g_free (path);
1913 break;
1915 #ifdef USE_DIFF_VIEW
1916 case MC_RUN_DIFFVIEWER:
1917 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1918 break;
1919 #endif /* USE_DIFF_VIEW */
1920 default:
1921 break;
1923 midnight_shutdown = 1;
1924 done_mc ();
1927 /* Run the main dialog that occupies the whole screen */
1928 static void
1929 do_nc (void)
1931 int midnight_colors[DLG_COLOR_NUM];
1932 midnight_colors[0] = mc_skin_color_get ("dialog", "_default_");
1933 midnight_colors[1] = mc_skin_color_get ("dialog", "focus");
1934 midnight_colors[2] = mc_skin_color_get ("dialog", "hotnormal");
1935 midnight_colors[3] = mc_skin_color_get ("dialog", "hotfocus");
1937 panel_init ();
1939 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback,
1940 "[main]", NULL, DLG_WANT_IDLE);
1942 if (mc_run_mode == MC_RUN_FULL)
1943 setup_mc ();
1944 else
1945 setup_dummy_mc ();
1947 /* start check display_codepage and source_codepage */
1948 check_codeset ();
1950 /* Check if we were invoked as an editor or file viewer */
1951 if (mc_run_mode != MC_RUN_FULL)
1952 mc_maybe_editor_or_viewer ();
1953 else
1955 create_panels_and_run_mc ();
1957 /* Program end */
1958 midnight_shutdown = 1;
1960 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1961 if (mc_args__last_wd_file && vfs_current_is_local ())
1962 last_wd_string = g_strdup (current_panel->cwd);
1964 done_mc ();
1967 destroy_dlg (midnight_dlg);
1968 panel_deinit ();
1969 current_panel = 0;
1970 done_mc_profile ();
1973 /* POSIX version. The only version we support. */
1974 static void
1975 OS_Setup (void)
1977 const char *shell_env = getenv ("SHELL");
1978 const char *mc_libdir;
1980 if ((shell_env == NULL) || (shell_env[0] == '\0'))
1982 struct passwd *pwd;
1983 pwd = getpwuid (geteuid ());
1984 if (pwd != NULL)
1985 shell = g_strdup (pwd->pw_shell);
1987 else
1988 shell = g_strdup (shell_env);
1990 if ((shell == NULL) || (shell[0] == '\0'))
1992 g_free (shell);
1993 shell = g_strdup ("/bin/sh");
1996 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1997 /* and can be overriden by the MC_DATADIR environment variable */
1998 mc_libdir = getenv ("MC_DATADIR");
1999 if (mc_libdir != NULL)
2001 mc_home = g_strdup (mc_libdir);
2002 mc_home_alt = g_strdup (SYSCONFDIR);
2004 else
2006 mc_home = g_strdup (SYSCONFDIR);
2007 mc_home_alt = g_strdup (DATADIR);
2010 /* This variable is used by the subshell */
2011 home_dir = getenv ("HOME");
2013 if (!home_dir)
2014 home_dir = mc_home;
2017 static void
2018 sigchld_handler_no_subshell (int sig)
2020 #ifdef __linux__
2021 int pid, status;
2023 if (!console_flag)
2024 return;
2026 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2027 the value of console_flag never changed, we could simply not install
2028 this handler at all if (!console_flag && !use_subshell). */
2030 /* That comment is no longer true. We need to wait() on a sigchld
2031 handler (that's at least what the tarfs code expects currently). */
2033 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2035 if (pid == cons_saver_pid)
2038 if (WIFSTOPPED (status))
2040 /* Someone has stopped cons.saver - restart it */
2041 kill (pid, SIGCONT);
2043 else
2045 /* cons.saver has died - disable console saving */
2046 handle_console (CONSOLE_DONE);
2047 console_flag = 0;
2050 /* If we got here, some other child exited; ignore it */
2051 #endif /* __linux__ */
2053 (void) sig;
2056 static void
2057 init_sigchld (void)
2059 struct sigaction sigchld_action;
2061 sigchld_action.sa_handler =
2062 #ifdef HAVE_SUBSHELL_SUPPORT
2063 use_subshell ? sigchld_handler :
2064 #endif /* HAVE_SUBSHELL_SUPPORT */
2065 sigchld_handler_no_subshell;
2067 sigemptyset (&sigchld_action.sa_mask);
2069 #ifdef SA_RESTART
2070 sigchld_action.sa_flags = SA_RESTART;
2071 #else
2072 sigchld_action.sa_flags = 0;
2073 #endif /* !SA_RESTART */
2075 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
2077 #ifdef HAVE_SUBSHELL_SUPPORT
2079 * This may happen on QNX Neutrino 6, where SA_RESTART
2080 * is defined but not implemented. Fallback to no subshell.
2082 use_subshell = 0;
2083 #endif /* HAVE_SUBSHELL_SUPPORT */
2088 main (int argc, char *argv[])
2090 struct stat s;
2091 char *mc_dir;
2092 GError *error = NULL;
2093 gboolean isInitialized;
2095 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2096 setlocale (LC_ALL, "");
2097 bindtextdomain ("mc", LOCALEDIR);
2098 textdomain ("mc");
2100 /* Set up temporary directory */
2101 mc_tmpdir ();
2103 OS_Setup ();
2105 str_init_strings (NULL);
2107 vfs_init ();
2109 #ifdef USE_INTERNAL_EDIT
2110 edit_stack_init ();
2111 #endif
2113 #ifdef HAVE_SLANG
2114 SLtt_Ignore_Beep = 1;
2115 #endif
2117 if (!mc_args_handle (argc, argv, "mc"))
2118 exit (EXIT_FAILURE);
2120 /* NOTE: This has to be called before tty_init or whatever routine
2121 calls any define_sequence */
2122 init_key ();
2124 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2125 handle_console (CONSOLE_INIT);
2127 #ifdef HAVE_SUBSHELL_SUPPORT
2128 /* Don't use subshell when invoked as viewer or editor */
2129 if (mc_run_mode != MC_RUN_FULL)
2130 use_subshell = 0;
2132 if (use_subshell)
2133 subshell_get_console_attributes ();
2134 #endif /* HAVE_SUBSHELL_SUPPORT */
2136 /* Install the SIGCHLD handler; must be done before init_subshell() */
2137 init_sigchld ();
2139 /* We need this, since ncurses endwin () doesn't restore the signals */
2140 save_stop_handler ();
2142 /* Must be done before init_subshell, to set up the terminal size: */
2143 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2144 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
2146 load_setup ();
2148 /* Removing this from the X code let's us type C-c */
2149 load_key_defs ();
2151 load_keymap_defs ();
2153 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
2155 isInitialized = mc_skin_init (&error);
2157 mc_filehighlight = mc_fhl_new (TRUE);
2159 dlg_set_default_colors ();
2161 if (!isInitialized)
2163 message (D_ERROR, _("Warning"), "%s", error->message);
2164 g_error_free (error);
2165 error = NULL;
2168 /* create home directory */
2169 /* do it after the screen library initialization to show the error message */
2170 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2171 canonicalize_pathname (mc_dir);
2172 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
2173 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
2174 g_free (mc_dir);
2176 #ifdef HAVE_SUBSHELL_SUPPORT
2177 /* Done here to ensure that the subshell doesn't */
2178 /* inherit the file descriptors opened below, etc */
2179 if (use_subshell)
2180 init_subshell ();
2182 #endif /* HAVE_SUBSHELL_SUPPORT */
2184 /* Also done after init_subshell, to save any shell init file messages */
2185 if (console_flag)
2186 handle_console (CONSOLE_SAVE);
2188 if (alternate_plus_minus)
2189 application_keypad_mode ();
2191 #ifdef HAVE_SUBSHELL_SUPPORT
2192 if (use_subshell)
2194 mc_prompt = strip_ctrl_codes (subshell_prompt);
2195 if (mc_prompt == NULL)
2196 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2198 else
2199 #endif /* HAVE_SUBSHELL_SUPPORT */
2200 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2202 /* Program main loop */
2203 if (!midnight_shutdown)
2204 do_nc ();
2206 /* Save the tree store */
2207 tree_store_save ();
2209 free_keymap_defs ();
2211 /* Virtual File System shutdown */
2212 vfs_shut ();
2214 flush_extension_file (); /* does only free memory */
2216 mc_fhl_free (&mc_filehighlight);
2217 mc_skin_deinit ();
2219 tty_shutdown ();
2221 if (console_flag && !(quit & SUBSHELL_EXIT))
2222 handle_console (CONSOLE_RESTORE);
2223 if (alternate_plus_minus)
2224 numeric_keypad_mode ();
2226 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2228 if (console_flag)
2229 handle_console (CONSOLE_DONE);
2230 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2232 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string && !print_last_revert)
2234 int last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2235 S_IRUSR | S_IWUSR);
2237 if (last_wd_fd != -1)
2239 ssize_t ret1;
2240 int ret2;
2241 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2242 ret2 = close (last_wd_fd);
2245 g_free (last_wd_string);
2247 g_free (mc_home_alt);
2248 g_free (mc_home);
2249 g_free (shell);
2251 done_key ();
2252 #ifdef HAVE_CHARSET
2253 free_codepages_list ();
2254 g_free (autodetect_codeset);
2255 #endif
2256 str_uninit_strings ();
2258 g_free (mc_run_param0);
2259 g_free (mc_run_param1);
2261 #ifdef USE_INTERNAL_EDIT
2262 edit_stack_free ();
2263 #endif
2265 return 0;