Ticket #2237: Automatic date and version substitution for man pages
[midnight-commander.git] / src / main.c
blobe3e4ffb0fa055357ec4aadd626fc0874c3280e21
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 */
48 #include "lib/skin.h"
49 #include "lib/mcconfig.h"
50 #include "lib/filehighlight.h"
51 #include "lib/fileloc.h" /* MC_USERCONF_DIR */
52 #include "lib/strutil.h"
54 #include "lib/vfs/mc-vfs/vfs.h" /* vfs_translate_url() */
55 #ifdef ENABLE_VFS_SMB
56 #include "lib/vfs/mc-vfs/smbfs.h" /* smbfs_set_debug() */
57 #endif /* ENABLE_VFS_SMB */
59 #include "args.h"
60 #include "dir.h"
61 #include "dialog.h"
62 #include "menu.h"
63 #include "panel.h"
64 #include "option.h"
65 #include "tree.h"
66 #include "treestore.h"
67 #include "consaver/cons.saver.h"
68 #include "subshell.h"
69 #include "setup.h" /* save_setup() */
70 #include "boxes.h" /* sort_box() */
71 #include "layout.h"
72 #include "cmd.h" /* Normal commands */
73 #include "hotlist.h"
74 #include "panelize.h"
75 #include "learn.h" /* learn_keys() */
76 #include "listmode.h"
77 #include "execute.h"
78 #include "ext.h" /* For flush_extension_file() */
79 #include "widget.h"
80 #include "command.h"
81 #include "wtools.h"
82 #include "cmddef.h" /* CK_ cmd name const */
83 #include "user.h" /* user_file_menu_cmd() */
84 #include "dialog-switch.h"
86 #include "chmod.h"
87 #include "chown.h"
88 #include "achown.h"
90 #include "main.h"
92 #ifdef USE_INTERNAL_EDIT
93 #include "src/editor/edit.h"
94 #endif
96 #ifdef USE_DIFF_VIEW
97 #include "src/diffviewer/ydiff.h"
98 #endif
100 #ifdef HAVE_CHARSET
101 #include "charsets.h"
102 #endif /* HAVE_CHARSET */
105 #include "keybind.h" /* type global_keymap_t */
107 /* When the modes are active, left_panel, right_panel and tree_panel */
108 /* Point to a proper data structure. You should check with the functions */
109 /* get_current_type and get_other_type the types of the panels before using */
110 /* This pointer variables */
112 /* The structures for the panels */
113 WPanel *left_panel = NULL;
114 WPanel *right_panel = NULL;
116 mc_fhl_t *mc_filehighlight;
118 /* The pointer to the tree */
119 WTree *the_tree = NULL;
121 /* The Menubar */
122 struct WMenuBar *the_menubar = NULL;
124 /* Pointers to the selected and unselected panel */
125 WPanel *current_panel = NULL;
127 /* Set if the command is being run from the "Right" menu */
128 int is_right = 0;
130 /* Set when main loop should be terminated */
131 volatile int quit = 0;
133 /* Set if you want the possible completions dialog for the first time */
134 int show_all_if_ambiguous = 0;
136 /* Set when cd symlink following is desirable (bash mode) */
137 int cd_symlinks = 1;
139 /* If set then dialogs just clean the screen when refreshing, else */
140 /* they do a complete refresh, refreshing all the parts of the program */
141 int fast_refresh = 0;
143 /* If true, at startup the user-menu is invoked */
144 int auto_menu = 0;
146 /* If true, then the +, - and \ keys have their special meaning only if the
147 * command line is emtpy, otherwise they behave like regular letters
149 int only_leading_plus_minus = 1;
151 int pause_after_run = pause_on_dumb_terminals;
153 /* It true saves the setup when quitting */
154 int auto_save_setup = 1;
156 #ifdef HAVE_CHARSET
158 * Don't restrict the output on the screen manager level,
159 * the translation tables take care of it.
161 #define full_eight_bits (1)
162 #define eight_bit_clean (1)
163 #else /* HAVE_CHARSET */
164 /* If true, allow characters in the range 160-255 */
165 int eight_bit_clean = 1;
167 * If true, also allow characters in the range 128-159.
168 * This is reported to break on many terminals (xterm, qansi-m).
170 int full_eight_bits = 0;
171 #endif /* !HAVE_CHARSET */
174 * If utf-8 terminal utf8_display = 1
175 * Display bits set UTF-8
178 int utf8_display = 0;
180 /* If true use the internal viewer */
181 int use_internal_view = 1;
183 /* The prompt */
184 const char *mc_prompt = NULL;
186 /* The widget where we draw the prompt */
187 WLabel *the_prompt;
189 /* The hint bar */
190 WLabel *the_hint;
192 /* The button bar */
193 WButtonBar *the_bar;
195 /* Mouse type: GPM, xterm or none */
196 Mouse_Type use_mouse_p = MOUSE_NONE;
198 /* If on, default for "No" in delete operations */
199 int safe_delete = 0;
201 /* Controls screen clearing before an exec */
202 int clear_before_exec = 1;
204 /* Asks for confirmation before deleting a file */
205 int confirm_delete = 1;
207 /* Asks for confirmation before deleting a hotlist entry */
208 int confirm_directory_hotlist_delete = 1;
210 /* Asks for confirmation before overwriting a file */
211 int confirm_overwrite = 1;
213 /* Asks for confirmation before executing a program by pressing enter */
214 int confirm_execute = 0;
216 /* Asks for confirmation before leaving the program */
217 int confirm_exit = 1;
219 /* Asks for confirmation before clean up of history */
220 int confirm_history_cleanup = 1;
222 /* Asks for confirmation when using F3 to view a directory and there
223 are tagged files */
224 int confirm_view_dir = 0;
226 /* This flag indicates if the pull down menus by default drop down */
227 int drop_menus = 0;
229 /* The dialog handle for the main program */
230 Dlg_head *midnight_dlg = NULL;
232 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
233 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
234 int update_prompt = 0;
236 /* The home directory */
237 const char *home_dir = NULL;
239 /* Only used at program boot */
240 int boot_current_is_left = 1;
242 /* If this is true, then when browsing the tree the other window will
243 * automatically reload it's directory with the contents of the currently
244 * selected directory.
246 int xtree_mode = 0;
248 /* path to X clipboard utility */
249 char* clipboard_store_path = NULL;
250 char* clipboard_paste_path = NULL;
252 /* If set, then print to the given file the last directory we were at */
253 static char *last_wd_string = NULL;
254 /* Set to 1 to suppress printing the last directory */
255 static int print_last_revert = 0;
257 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
258 char *mc_run_param0 = NULL;
259 char *mc_run_param1 = NULL;
261 /* Used so that widgets know if they are being destroyed or
262 shut down */
263 int midnight_shutdown = 0;
265 /* The user's shell */
266 char *shell = NULL;
268 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
269 char *mc_home = NULL;
271 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
272 char *mc_home_alt = NULL;
274 /* Define this function for glib-style error handling */
275 GQuark
276 mc_main_error_quark (void)
278 return g_quark_from_static_string (PACKAGE);
281 /* Save current stat of directories to avoid reloading the panels */
282 /* when no modifications have taken place */
283 void
284 save_cwds_stat (void)
286 if (panels_options.fast_reload)
288 mc_stat (current_panel->cwd, &(current_panel->dir_stat));
289 if (get_other_type () == view_listing)
290 mc_stat (other_panel->cwd, &(other_panel->dir_stat));
294 #ifdef HAVE_SUBSHELL_SUPPORT
295 void
296 do_update_prompt (void)
298 if (update_prompt)
300 printf ("\r\n%s", subshell_prompt);
301 fflush (stdout);
302 update_prompt = 0;
305 #endif /* HAVE_SUBSHELL_SUPPORT */
307 void
308 change_panel (void)
310 free_completions (cmdline);
311 dlg_one_down (midnight_dlg);
314 /* Stop MC main dialog and the current dialog if it exists.
315 * Needed to provide fast exit from MC viewer or editor on shell exit */
316 static void
317 stop_dialogs (void)
319 midnight_dlg->state = DLG_CLOSED;
321 if ((top_dlg != NULL) && (top_dlg->data != NULL))
322 ((Dlg_head *) top_dlg->data)->state = DLG_CLOSED;
325 static gboolean
326 quit_cmd_internal (int quiet)
328 int q = quit;
329 size_t n = dialog_switch_num () - 1;
331 if (n != 0)
333 char msg[BUF_MEDIUM];
335 g_snprintf (msg, sizeof (msg),
336 ngettext ("You have %zd opened screen. Quit anyway?",
337 "You have %zd opened screens. Quit anyway?", n),
340 if (query_dialog (_("The Midnight Commander"), msg,
341 D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
342 return FALSE;
343 q = 1;
344 } else if (quiet || !confirm_exit)
345 q = 1;
346 else if (query_dialog (_("The Midnight Commander"),
347 _("Do you really want to quit the Midnight Commander?"),
348 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
349 q = 1;
351 if (q != 0)
353 #ifdef HAVE_SUBSHELL_SUPPORT
354 if (!use_subshell)
355 stop_dialogs ();
356 else if ((q = exit_subshell ()))
357 #endif
358 stop_dialogs ();
361 if (q != 0)
362 quit |= 1;
363 return (quit != 0);
366 static gboolean
367 quit_cmd (void)
369 return quit_cmd_internal (0);
372 gboolean
373 quiet_quit_cmd (void)
375 print_last_revert = 1;
376 return quit_cmd_internal (1);
379 /* Wrapper for do_subshell_chdir, check for availability of subshell */
380 void
381 subshell_chdir (const char *directory)
383 #ifdef HAVE_SUBSHELL_SUPPORT
384 if (use_subshell)
386 if (vfs_current_is_local ())
387 do_subshell_chdir (directory, 0, 1);
389 #endif /* HAVE_SUBSHELL_SUPPORT */
393 do_cd (const char *new_dir, enum cd_enum exact)
395 return (do_panel_cd (current_panel, new_dir, exact));
398 #ifdef HAVE_SUBSHELL_SUPPORT
400 load_prompt (int fd, void *unused)
402 (void) fd;
403 (void) unused;
405 if (!read_subshell_prompt ())
406 return 0;
408 /* Don't actually change the prompt if it's invisible */
409 if (((Dlg_head *) top_dlg->data == midnight_dlg) && command_prompt)
411 char *tmp_prompt;
412 int prompt_len;
414 tmp_prompt = strip_ctrl_codes (subshell_prompt);
415 prompt_len = str_term_width1 (tmp_prompt);
417 /* Check for prompts too big */
418 if (COLS > 8 && prompt_len > COLS - 8)
420 prompt_len = COLS - 8;
421 tmp_prompt[prompt_len] = '\0';
423 mc_prompt = tmp_prompt;
424 label_set_text (the_prompt, mc_prompt);
425 winput_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
427 /* since the prompt has changed, and we are called from one of the
428 * tty_get_event channels, the prompt updating does not take place
429 * automatically: force a cursor update and a screen refresh
431 update_cursor (midnight_dlg);
432 mc_refresh ();
434 update_prompt = 1;
435 return 0;
437 #endif /* HAVE_SUBSHELL_SUPPORT */
439 void
440 sort_cmd (void)
442 WPanel *p;
443 const panel_field_t *sort_order;
445 if (!SELECTED_IS_PANEL)
446 return;
448 p = MENU_PANEL;
449 sort_order = sort_box (p->current_sort_field, &p->reverse, &p->case_sensitive, &p->exec_first);
451 panel_set_sort_order (p, sort_order);
455 static void
456 treebox_cmd (void)
458 char *sel_dir;
460 sel_dir = tree_box (selection (current_panel)->fname);
461 if (sel_dir)
463 do_cd (sel_dir, cd_exact);
464 g_free (sel_dir);
468 #ifdef LISTMODE_EDITOR
469 static void
470 listmode_cmd (void)
472 char *newmode;
474 if (get_current_type () != view_listing)
475 return;
477 newmode = listmode_edit (current_panel->user_format);
478 if (!newmode)
479 return;
481 g_free (current_panel->user_format);
482 current_panel->list_type = list_user;
483 current_panel->user_format = newmode;
484 set_panel_formats (current_panel);
486 do_refresh ();
488 #endif /* LISTMODE_EDITOR */
490 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
491 static GList *
492 create_panel_menu (void)
494 GList *entries = NULL;
496 entries = g_list_append (entries, menu_entry_create (_("File listin&g"), CK_ListingCmd));
497 entries = g_list_append (entries, menu_entry_create (_("&Quick view"), CK_QuickViewCmd));
498 entries = g_list_append (entries, menu_entry_create (_("&Info"), CK_InfoCmd));
499 entries = g_list_append (entries, menu_entry_create (_("&Tree"), CK_TreeCmd));
500 entries = g_list_append (entries, menu_separator_create ());
501 entries = g_list_append (entries, menu_entry_create (_("&Listing mode..."), CK_ChangeListingCmd));
502 entries = g_list_append (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
503 entries = g_list_append (entries, menu_entry_create (_("&Filter..."), CK_FilterCmd));
504 #ifdef HAVE_CHARSET
505 entries = g_list_append (entries, menu_entry_create (_("&Encoding..."), CK_PanelSetPanelEncoding));
506 #endif
507 #ifdef ENABLE_VFS_NET
508 entries = g_list_append (entries, menu_separator_create ());
509 #ifdef ENABLE_VFS_FTP
510 entries = g_list_append (entries, menu_entry_create (_("FT&P link..."), CK_FtplinkCmd));
511 #endif
512 #ifdef ENABLE_VFS_FISH
513 entries = g_list_append (entries, menu_entry_create (_("S&hell link..."), CK_FishlinkCmd));
514 #endif
515 #ifdef ENABLE_VFS_SMB
516 entries = g_list_append (entries, menu_entry_create (_("SM&B link..."), CK_SmblinkCmd));
517 #endif
518 #endif /* ENABLE_VFS_NET */
519 entries = g_list_append (entries, menu_separator_create ());
520 entries = g_list_append (entries, menu_entry_create (_("&Rescan"), CK_RereadCmd));
522 return entries;
525 static GList *
526 create_file_menu (void)
528 GList *entries = NULL;
530 entries = g_list_append (entries, menu_entry_create (_("&View"), CK_ViewCmd));
531 entries = g_list_append (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFileCmd));
532 entries = g_list_append (entries, menu_entry_create (_("&Filtered view"), CK_FilteredViewCmd));
533 entries = g_list_append (entries, menu_entry_create (_("&Edit"), CK_EditCmd));
534 entries = g_list_append (entries, menu_entry_create (_("&Copy"), CK_CopyCmd));
535 entries = g_list_append (entries, menu_entry_create (_("C&hmod"), CK_ChmodCmd));
536 entries = g_list_append (entries, menu_entry_create (_("&Link"), CK_LinkCmd));
537 entries = g_list_append (entries, menu_entry_create (_("&Symlink"), CK_SymlinkCmd));
538 entries = g_list_append (entries, menu_entry_create (_("Relative symlin&k"), CK_RelativeSymlinkCmd));
539 entries = g_list_append (entries, menu_entry_create (_("Edit s&ymlink"), CK_EditSymlinkCmd));
540 entries = g_list_append (entries, menu_entry_create (_("Ch&own"), CK_ChownCmd));
541 entries =
542 g_list_append (entries, menu_entry_create (_("&Advanced chown"), CK_ChownAdvancedCmd));
543 entries = g_list_append (entries, menu_entry_create (_("&Rename/Move"), CK_RenameCmd));
544 entries = g_list_append (entries, menu_entry_create (_("&Mkdir"), CK_MkdirCmd));
545 entries = g_list_append (entries, menu_entry_create (_("&Delete"), CK_DeleteCmd));
546 entries = g_list_append (entries, menu_entry_create (_("&Quick cd"), CK_QuickCdCmd));
547 entries = g_list_append (entries, menu_separator_create ());
548 entries = g_list_append (entries, menu_entry_create (_("Select &group"), CK_SelectCmd));
549 entries = g_list_append (entries, menu_entry_create (_("U&nselect group"), CK_UnselectCmd));
550 entries =
551 g_list_append (entries,
552 menu_entry_create (_("Reverse selec&tion"), CK_ReverseSelectionCmd));
553 entries = g_list_append (entries, menu_separator_create ());
554 entries = g_list_append (entries, menu_entry_create (_("E&xit"), CK_QuitCmd));
556 return entries;
559 static GList *
560 create_command_menu (void)
562 /* I know, I'm lazy, but the tree widget when it's not running
563 * as a panel still has some problems, I have not yet finished
564 * the WTree widget port, sorry.
566 GList *entries = NULL;
568 entries = g_list_append (entries, menu_entry_create (_("&User menu"), CK_UserMenuCmd));
569 entries = g_list_append (entries, menu_entry_create (_("&Directory tree"), CK_TreeBoxCmd));
570 entries = g_list_append (entries, menu_entry_create (_("&Find file"), CK_FindCmd));
571 entries = g_list_append (entries, menu_entry_create (_("S&wap panels"), CK_SwapCmd));
572 entries =
573 g_list_append (entries, menu_entry_create (_("Switch &panels on/off"), CK_ShowCommandLine));
574 entries =
575 g_list_append (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirsCmd));
576 #ifdef USE_DIFF_VIEW
577 entries = g_list_append (entries, menu_entry_create (_("&View diff files"), CK_DiffViewCmd));
578 #endif
579 entries =
580 g_list_append (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
581 entries =
582 g_list_append (entries,
583 menu_entry_create (_("Show directory s&izes"), CK_SingleDirsizeCmd));
584 entries = g_list_append (entries, menu_separator_create ());
585 entries = g_list_append (entries, menu_entry_create (_("Command &history"), CK_HistoryCmd));
586 entries =
587 g_list_append (entries, menu_entry_create (_("Di&rectory hotlist"), CK_QuickChdirCmd));
588 #ifdef ENABLE_VFS
589 entries = g_list_append (entries, menu_entry_create (_("&Active VFS list"), CK_ReselectVfs));
590 #endif
591 #ifdef WITH_BACKGROUND
592 entries = g_list_append (entries, menu_entry_create (_("&Background jobs"), CK_JobsCmd));
593 #endif
594 entries = g_list_append (entries, menu_entry_create (_("Screen lis&t"), CK_DialogListCmd));
595 entries = g_list_append (entries, menu_separator_create ());
596 #ifdef ENABLE_VFS_UNDELFS
597 entries =
598 g_list_append (entries,
599 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_UndeleteCmd));
600 #endif
601 #ifdef LISTMODE_EDITOR
602 entries =
603 g_list_append (entries, menu_entry_create (_("&Listing format edit"), CK_ListmodeCmd));
604 #endif
605 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
606 entries = g_list_append (entries, menu_separator_create ());
607 #endif
608 entries =
609 g_list_append (entries, menu_entry_create (_("Edit &extension file"), CK_EditExtFileCmd));
610 entries = g_list_append (entries, menu_entry_create (_("Edit &menu file"), CK_EditMcMenuCmd));
611 entries =
612 g_list_append (entries,
613 menu_entry_create (_("Edit hi&ghlighting group file"), CK_EditFhlFileCmd));
615 return entries;
618 static GList *
619 create_options_menu (void)
621 GList *entries = NULL;
623 entries = g_list_append (entries, menu_entry_create (_("&Configuration..."), CK_ConfigureBox));
624 entries = g_list_append (entries, menu_entry_create (_("&Layout..."), CK_LayoutBox));
625 entries = g_list_append (entries, menu_entry_create (_("&Panel options..."), CK_PanelOptionsBox));
626 entries = g_list_append (entries, menu_entry_create (_("C&onfirmation..."), CK_ConfirmBox));
627 entries = g_list_append (entries, menu_entry_create (_("&Display bits..."), CK_DisplayBitsBox));
628 entries = g_list_append (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
629 #ifdef ENABLE_VFS
630 entries = g_list_append (entries, menu_entry_create (_("&Virtual FS..."), CK_ConfigureVfs));
631 #endif
632 entries = g_list_append (entries, menu_separator_create ());
633 entries = g_list_append (entries, menu_entry_create (_("&Save setup"), CK_SaveSetupCmd));
635 return entries;
638 void
639 init_menu (void)
641 menubar_add_menu (the_menubar,
642 create_menu (horizontal_split ? _("&Above") : _("&Left"),
643 create_panel_menu (), "[Left and Right Menus]"));
644 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
645 menubar_add_menu (the_menubar,
646 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
647 menubar_add_menu (the_menubar,
648 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
649 menubar_add_menu (the_menubar,
650 create_menu (horizontal_split ? _("&Below") : _("&Right"),
651 create_panel_menu (), "[Left and Right Menus]"));
654 void
655 done_menu (void)
657 menubar_set_menu (the_menubar, NULL);
660 static void
661 menu_last_selected_cmd (void)
663 the_menubar->is_active = TRUE;
664 the_menubar->is_dropped = (drop_menus != 0);
665 the_menubar->previous_widget = dlg_get_current_widget_id (midnight_dlg);
666 dlg_select_widget (the_menubar);
669 static void
670 menu_cmd (void)
672 if (the_menubar->is_active)
673 return;
675 if ((get_current_index () == 0) == (current_panel->active != 0))
676 the_menubar->selected = 0;
677 else
678 the_menubar->selected = g_list_length (the_menubar->menu) - 1;
679 menu_last_selected_cmd ();
682 static char *
683 midnight_get_shortcut (unsigned long command)
685 const char *ext_map;
686 const char *shortcut = NULL;
688 shortcut = lookup_keymap_shortcut (main_map, command);
689 if (shortcut != NULL)
690 return g_strdup (shortcut);
692 shortcut = lookup_keymap_shortcut (panel_map, command);
693 if (shortcut != NULL)
694 return g_strdup (shortcut);
696 ext_map = lookup_keymap_shortcut (main_map, CK_StartExtMap1);
697 if (ext_map != NULL)
698 shortcut = lookup_keymap_shortcut (main_x_map, command);
699 if (shortcut != NULL)
700 return g_strdup_printf ("%s %s", ext_map, shortcut);
702 return NULL;
705 static char *
706 midnight_get_title (const Dlg_head *h, size_t len)
708 /* TODO: share code with update_xterm_title_path() */
710 const char *path;
711 char host[BUF_TINY];
712 char *p;
713 struct passwd *pw = NULL;
714 char *login = NULL;
715 int res = 0;
717 (void) h;
719 path = strip_home_and_password (current_panel->cwd);
720 res = gethostname (host, sizeof (host));
721 if (res != 0)
722 host[0] = '\0';
723 else
724 host [sizeof (host) - 1] = '\0';
726 pw = getpwuid (getuid ());
727 if (pw != NULL)
728 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
729 else
730 login = g_strdup (host);
732 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
733 path = str_trunc (p, len - 4);
734 g_free (login);
735 g_free (p);
737 return g_strdup (path);
740 void
741 toggle_show_hidden (void)
743 panels_options.show_dot_files = !panels_options.show_dot_files;
744 update_panels (UP_RELOAD, UP_KEEPSEL);
747 static void
748 toggle_panels_split (void)
750 horizontal_split = !horizontal_split;
751 layout_change ();
752 do_refresh ();
756 * Just a hack for allowing url-like pathnames to be accepted from the
757 * command line.
759 static void
760 translated_mc_chdir (char *dir)
762 char *newdir;
763 int ret;
765 newdir = vfs_translate_url (dir);
766 ret = mc_chdir (newdir);
767 g_free (newdir);
770 static void
771 create_panels (void)
773 int current_index;
774 int other_index;
775 panel_view_mode_t current_mode, other_mode;
776 char original_dir[BUF_1K] = "\0";
778 if (boot_current_is_left)
780 current_index = 0;
781 other_index = 1;
782 current_mode = startup_left_mode;
783 other_mode = startup_right_mode;
785 else
787 current_index = 1;
788 other_index = 0;
789 current_mode = startup_right_mode;
790 other_mode = startup_left_mode;
792 /* Creates the left panel */
793 if (mc_run_param0 != NULL)
795 if (mc_run_param1 != NULL)
797 /* Ok, user has specified two dirs, save the original one,
798 * since we may not be able to chdir to the proper
799 * second directory later
801 mc_get_current_wd (original_dir, sizeof (original_dir) - 2);
803 translated_mc_chdir (mc_run_param0);
805 set_display_type (current_index, current_mode);
807 /* The other panel */
808 if (mc_run_param1 != NULL)
810 if (original_dir[0] != '\0')
811 translated_mc_chdir (original_dir);
812 translated_mc_chdir (mc_run_param1);
814 set_display_type (other_index, other_mode);
816 if (startup_left_mode == view_listing)
818 current_panel = left_panel;
820 else
822 if (right_panel)
823 current_panel = right_panel;
824 else
825 current_panel = left_panel;
828 /* Create the nice widgets */
829 cmdline = command_new (0, 0, 0);
830 the_prompt = label_new (0, 0, mc_prompt);
831 the_prompt->transparent = 1;
832 the_bar = buttonbar_new (keybar_visible);
834 the_hint = label_new (0, 0, 0);
835 the_hint->transparent = 1;
836 the_hint->auto_adjust_cols = 0;
837 the_hint->widget.cols = COLS;
839 the_menubar = menubar_new (0, 0, COLS, NULL);
842 static void
843 copy_current_pathname (void)
845 char *cwd_path;
846 if (!command_prompt)
847 return;
849 cwd_path = remove_encoding_from_path (current_panel->cwd);
850 command_insert (cmdline, cwd_path, 0);
852 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
853 command_insert (cmdline, PATH_SEP_STR, 0);
854 g_free (cwd_path);
857 static void
858 copy_other_pathname (void)
860 char *cwd_path;
862 if (get_other_type () != view_listing)
863 return;
865 if (!command_prompt)
866 return;
868 cwd_path = remove_encoding_from_path (other_panel->cwd);
869 command_insert (cmdline, cwd_path, 0);
871 if (cwd_path[strlen (cwd_path) - 1] != PATH_SEP)
872 command_insert (cmdline, PATH_SEP_STR, 0);
873 g_free (cwd_path);
876 static void
877 copy_readlink (WPanel * panel)
879 if (!command_prompt)
880 return;
881 if (S_ISLNK (selection (panel)->st.st_mode))
883 char buffer[MC_MAXPATHLEN];
884 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
885 int i;
887 i = mc_readlink (p, buffer, MC_MAXPATHLEN - 1);
888 g_free (p);
889 if (i > 0)
891 buffer[i] = 0;
892 command_insert (cmdline, buffer, 1);
897 static void
898 copy_current_readlink (void)
900 copy_readlink (current_panel);
903 static void
904 copy_other_readlink (void)
906 if (get_other_type () == view_listing)
907 copy_readlink (other_panel);
910 /* Insert the selected file name into the input line */
911 static void
912 copy_prog_name (void)
914 char *tmp;
915 if (!command_prompt)
916 return;
918 if (get_current_type () == view_tree)
920 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
921 tmp = tree_selected_name (tree);
923 else
924 tmp = selection (current_panel)->fname;
926 command_insert (cmdline, tmp, 1);
929 static void
930 copy_tagged (WPanel * panel)
932 int i;
934 if (!command_prompt)
935 return;
936 input_disable_update (cmdline);
937 if (panel->marked)
939 for (i = 0; i < panel->count; i++)
941 if (panel->dir.list[i].f.marked)
942 command_insert (cmdline, panel->dir.list[i].fname, 1);
945 else
947 command_insert (cmdline, panel->dir.list[panel->selected].fname, 1);
949 input_enable_update (cmdline);
952 static void
953 copy_current_tagged (void)
955 copy_tagged (current_panel);
958 static void
959 copy_other_tagged (void)
961 if (get_other_type () == view_listing)
962 copy_tagged (other_panel);
965 void
966 midnight_set_buttonbar (WButtonBar * b)
968 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
969 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
970 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
971 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
972 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
973 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
974 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
975 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
976 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
977 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
980 static gboolean ctl_x_map_enabled = FALSE;
982 static void
983 ctl_x_cmd (void)
985 ctl_x_map_enabled = TRUE;
988 static cb_ret_t
989 midnight_execute_cmd (Widget * sender, unsigned long command)
991 cb_ret_t res = MSG_HANDLED;
993 (void) sender;
995 /* stop quick search before executing any command */
996 send_message ((Widget *) current_panel, WIDGET_COMMAND, CK_PanelStopSearch);
998 switch (command)
1000 case CK_AddHotlist:
1001 add2hotlist_cmd ();
1002 break;
1003 case CK_ChangeListingCmd:
1004 change_listing_cmd ();
1005 break;
1006 case CK_ChmodCmd:
1007 chmod_cmd ();
1008 break;
1009 case CK_ChownCmd:
1010 chown_cmd ();
1011 break;
1012 case CK_ChownAdvancedCmd:
1013 chown_advanced_cmd ();
1014 break;
1015 case CK_CompareDirsCmd:
1016 compare_dirs_cmd ();
1017 break;
1018 case CK_ConfigureBox:
1019 configure_box ();
1020 break;
1021 #ifdef ENABLE_VFS
1022 case CK_ConfigureVfs:
1023 configure_vfs ();
1024 break;
1025 #endif
1026 case CK_ConfirmBox:
1027 confirm_box ();
1028 break;
1029 case CK_CopyCmd:
1030 copy_cmd ();
1031 break;
1032 case CK_CopyCurrentPathname:
1033 copy_current_pathname ();
1034 break;
1035 case CK_CopyCurrentReadlink:
1036 copy_current_readlink ();
1037 break;
1038 case CK_CopyCurrentTagged:
1039 copy_current_tagged ();
1040 break;
1041 case CK_CopyOtherPathname:
1042 copy_other_pathname ();
1043 break;
1044 case CK_CopyOtherReadlink:
1045 copy_other_readlink ();
1046 break;
1047 case CK_CopyOtherTagged:
1048 copy_other_tagged ();
1049 break;
1050 case CK_DeleteCmd:
1051 delete_cmd ();
1052 break;
1053 case CK_DialogListCmd:
1054 dialog_switch_list ();
1055 break;
1056 #ifdef USE_DIFF_VIEW
1057 case CK_DiffViewCmd:
1058 diff_view_cmd ();
1059 break;
1060 #endif
1061 case CK_DisplayBitsBox:
1062 display_bits_box ();
1063 break;
1064 case CK_EditCmd:
1065 edit_cmd ();
1066 break;
1067 #ifdef USE_INTERNAL_EDIT
1068 case CK_EditForceInternalCmd:
1069 edit_cmd_force_internal ();
1070 break;
1071 #endif
1072 case CK_EditExtFileCmd:
1073 ext_cmd ();
1074 break;
1075 case CK_EditFhlFileCmd:
1076 edit_fhl_cmd ();
1077 break;
1078 case CK_EditMcMenuCmd:
1079 edit_mc_menu_cmd ();
1080 break;
1081 case CK_EditSymlinkCmd:
1082 edit_symlink_cmd ();
1083 break;
1084 case CK_ExternalPanelize:
1085 external_panelize ();
1086 break;
1087 case CK_FilterCmd:
1088 filter_cmd ();
1089 break;
1090 case CK_FilteredViewCmd:
1091 filtered_view_cmd ();
1092 break;
1093 case CK_FindCmd:
1094 find_cmd ();
1095 break;
1096 #ifdef ENABLE_VFS_FISH
1097 case CK_FishlinkCmd:
1098 fishlink_cmd ();
1099 break;
1100 #endif
1101 #ifdef ENABLE_VFS_FTP
1102 case CK_FtplinkCmd:
1103 ftplink_cmd ();
1104 break;
1105 #endif
1106 case CK_HelpCmd:
1107 help_cmd ();
1108 break;
1109 case CK_HistoryCmd:
1110 history_cmd ();
1111 break;
1112 case CK_InfoCmd:
1113 if (sender == (Widget *) the_menubar)
1114 info_cmd (); /* mwnu */
1115 else
1116 info_cmd_no_menu (); /* shortcut or buttonbar */
1117 break;
1118 #ifdef WITH_BACKGROUND
1119 case CK_JobsCmd:
1120 jobs_cmd ();
1121 break;
1122 #endif
1123 case CK_LayoutBox:
1124 layout_box ();
1125 break;
1126 case CK_LearnKeys:
1127 learn_keys ();
1128 break;
1129 case CK_LinkCmd:
1130 link_cmd (LINK_HARDLINK);
1131 break;
1132 case CK_ListingCmd:
1133 listing_cmd ();
1134 break;
1135 #ifdef LISTMODE_EDITOR
1136 case CK_ListmodeCmd:
1137 listmode_cmd ();
1138 break;
1139 #endif
1140 case CK_MenuCmd:
1141 menu_cmd ();
1142 break;
1143 case CK_MenuLastSelectedCmd:
1144 menu_last_selected_cmd ();
1145 break;
1146 case CK_MkdirCmd:
1147 mkdir_cmd ();
1148 break;
1149 case CK_PanelOptionsBox:
1150 panel_options_box ();
1151 break;
1152 #ifdef HAVE_CHARSET
1153 case CK_PanelSetPanelEncoding:
1154 encoding_cmd ();
1155 break;
1156 #endif
1157 case CK_QuickCdCmd:
1158 quick_cd_cmd ();
1159 break;
1160 case CK_QuickChdirCmd:
1161 quick_chdir_cmd ();
1162 break;
1163 case CK_QuickViewCmd:
1164 if (sender == (Widget *) the_menubar)
1165 quick_view_cmd (); /* menu */
1166 else
1167 quick_cmd_no_menu (); /* shortcut or buttonabr */
1168 break;
1169 case CK_QuietQuitCmd:
1170 quiet_quit_cmd ();
1171 break;
1172 case CK_QuitCmd:
1173 quit_cmd ();
1174 break;
1175 case CK_RelativeSymlinkCmd:
1176 link_cmd (LINK_SYMLINK_RELATIVE);
1177 break;
1178 case CK_RenameCmd:
1179 rename_cmd ();
1180 break;
1181 case CK_RereadCmd:
1182 reread_cmd ();
1183 break;
1184 #ifdef ENABLE_VFS
1185 case CK_ReselectVfs:
1186 reselect_vfs ();
1187 break;
1188 #endif
1189 case CK_ReverseSelectionCmd:
1190 reverse_selection_cmd ();
1191 break;
1192 case CK_SaveSetupCmd:
1193 save_setup_cmd ();
1194 break;
1195 case CK_SelectCmd:
1196 select_cmd ();
1197 break;
1198 case CK_ShowCommandLine:
1199 view_other_cmd ();
1200 break;
1201 case CK_SingleDirsizeCmd:
1202 smart_dirsize_cmd ();
1203 break;
1204 #ifdef ENABLE_VFS_SMB
1205 case CK_SmblinkCmd:
1206 smblink_cmd ();
1207 break;
1208 #endif /* ENABLE_VFS_SMB */
1209 case CK_Sort:
1210 sort_cmd ();
1211 break;
1212 case CK_StartExtMap1:
1213 ctl_x_cmd ();
1214 break;
1215 case CK_SuspendCmd:
1216 suspend_cmd ();
1217 break;
1218 case CK_SwapCmd:
1219 swap_cmd ();
1220 break;
1221 case CK_SymlinkCmd:
1222 link_cmd (LINK_SYMLINK_ABSOLUTE);
1223 break;
1224 case CK_ToggleListingCmd:
1225 toggle_listing_cmd ();
1226 break;
1227 case CK_ToggleShowHidden:
1228 toggle_show_hidden ();
1229 break;
1230 case CK_TogglePanelsSplit:
1231 toggle_panels_split ();
1232 break;
1233 case CK_TreeCmd:
1234 tree_cmd ();
1235 break;
1236 case CK_TreeBoxCmd:
1237 treebox_cmd ();
1238 break;
1239 #ifdef ENABLE_VFS_UNDELFS
1240 case CK_UndeleteCmd:
1241 undelete_cmd ();
1242 break;
1243 #endif
1244 case CK_UnselectCmd:
1245 unselect_cmd ();
1246 break;
1247 case CK_UserMenuCmd:
1248 user_file_menu_cmd ();
1249 break;
1250 case CK_ViewCmd:
1251 view_cmd ();
1252 break;
1253 case CK_ViewFileCmd:
1254 view_file_cmd ();
1255 break;
1256 default:
1257 res = MSG_NOT_HANDLED;
1260 return res;
1263 static void
1264 init_xterm_support (void)
1266 const char *termvalue;
1268 termvalue = getenv ("TERM");
1269 if (!termvalue || !(*termvalue))
1271 fputs (_("The TERM environment variable is unset!\n"), stderr);
1272 exit (EXIT_FAILURE);
1275 /* Check mouse capabilities */
1276 xmouse_seq = tty_tgetstr ("Km");
1278 if (strcmp (termvalue, "cygwin") == 0)
1280 mc_args__force_xterm = 1;
1281 use_mouse_p = MOUSE_DISABLED;
1284 if (mc_args__force_xterm || strncmp (termvalue, "xterm", 5) == 0
1285 || strncmp (termvalue, "konsole", 7) == 0
1286 || strncmp (termvalue, "rxvt", 4) == 0
1287 || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0)
1289 xterm_flag = 1;
1291 /* Default to the standard xterm sequence */
1292 if (!xmouse_seq)
1294 xmouse_seq = ESC_STR "[M";
1297 /* Enable mouse unless explicitly disabled by --nomouse */
1298 if (use_mouse_p != MOUSE_DISABLED)
1300 const char *color_term = getenv ("COLORTERM");
1301 if (strncmp (termvalue, "rxvt", 4) == 0 ||
1302 (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
1303 strcmp (termvalue, "Eterm") == 0)
1305 use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
1307 else
1309 use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
1315 static void
1316 setup_mc (void)
1318 #ifdef HAVE_SLANG
1319 tty_display_8bit (full_eight_bits != 0);
1320 #else
1321 tty_display_8bit (eight_bit_clean != 0);
1322 #endif
1324 #ifdef HAVE_SUBSHELL_SUPPORT
1325 if (use_subshell)
1326 add_select_channel (subshell_pty, load_prompt, 0);
1327 #endif /* !HAVE_SUBSHELL_SUPPORT */
1329 tty_setup_sigwinch (sigwinch_handler);
1331 if ((tty_baudrate () < 9600) || tty_is_slow ())
1332 verbose = 0;
1334 init_xterm_support ();
1335 init_mouse ();
1338 static void
1339 setup_dummy_mc (void)
1341 char d[MC_MAXPATHLEN];
1342 int ret;
1344 mc_get_current_wd (d, MC_MAXPATHLEN);
1345 setup_mc ();
1346 ret = mc_chdir (d);
1349 static void
1350 check_codeset ()
1352 const char *current_system_codepage = NULL;
1354 current_system_codepage = str_detect_termencoding ();
1356 #ifdef HAVE_CHARSET
1358 const char *_display_codepage;
1360 _display_codepage = get_codepage_id (display_codepage);
1362 if (!strcmp (_display_codepage, current_system_codepage))
1364 utf8_display = str_isutf8 (current_system_codepage);
1365 return;
1368 display_codepage = get_codepage_index (current_system_codepage);
1369 if (display_codepage == -1)
1371 display_codepage = 0;
1374 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
1376 #endif
1377 utf8_display = str_isutf8 (current_system_codepage);
1380 static void
1381 done_screen (void)
1383 if (!(quit & SUBSHELL_EXIT))
1384 clr_scr ();
1385 tty_reset_shell_mode ();
1386 tty_noraw_mode ();
1387 tty_keypad (FALSE);
1388 tty_colors_done ();
1391 static void
1392 done_mc (void)
1394 disable_mouse ();
1396 /* Setup shutdown
1398 * We sync the profiles since the hotlist may have changed, while
1399 * we only change the setup data if we have the auto save feature set
1402 if (auto_save_setup)
1403 save_setup (); /* does also call save_hotlist */
1404 else
1406 save_hotlist ();
1407 save_panel_types ();
1409 done_screen ();
1411 #ifdef ENABLE_VFS
1412 vfs_stamp_path (vfs_get_current_dir ());
1414 if ((current_panel != NULL)
1415 && (get_current_type () == view_listing))
1416 vfs_stamp_path (current_panel->cwd);
1418 if ((other_panel != NULL)
1419 && (get_other_type () == view_listing))
1420 vfs_stamp_path (other_panel->cwd);
1421 #endif
1424 static cb_ret_t
1425 midnight_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1427 unsigned long command;
1429 switch (msg)
1431 case DLG_INIT:
1432 setup_panels ();
1433 return MSG_HANDLED;
1435 case DLG_DRAW:
1436 load_hint (1);
1437 /* We handle the special case of the output lines */
1438 if (console_flag && output_lines)
1439 show_console_contents (output_start_y,
1440 LINES - output_lines - keybar_visible -
1441 1, LINES - keybar_visible - 1);
1442 return MSG_HANDLED;
1444 case DLG_RESIZE:
1445 setup_panels ();
1446 menubar_arrange (the_menubar);
1447 return MSG_HANDLED;
1449 case DLG_IDLE:
1450 /* We only need the first idle event to show user menu after start */
1451 set_idle_proc (h, 0);
1453 if (boot_current_is_left)
1454 dlg_select_widget (get_panel_widget (0));
1455 else
1456 dlg_select_widget (get_panel_widget (1));
1458 if (auto_menu)
1459 midnight_execute_cmd (NULL, CK_UserMenuCmd);
1460 return MSG_HANDLED;
1462 case DLG_KEY:
1463 if (ctl_x_map_enabled)
1465 ctl_x_map_enabled = FALSE;
1466 command = lookup_keymap_command (main_x_map, parm);
1467 if (command != CK_Ignore_Key)
1468 return midnight_execute_cmd (NULL, command);
1471 /* FIXME: should handle all menu shortcuts before this point */
1472 if (the_menubar->is_active)
1473 return MSG_NOT_HANDLED;
1475 if (parm == '\t')
1476 free_completions (cmdline);
1478 if (parm == '\n')
1480 size_t i;
1482 for (i = 0; cmdline->buffer[i] && (cmdline->buffer[i] == ' ' ||
1483 cmdline->buffer[i] == '\t'); i++)
1485 if (cmdline->buffer[i])
1487 send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1488 return MSG_HANDLED;
1490 stuff (cmdline, "", 0);
1491 cmdline->point = 0;
1494 /* Ctrl-Enter and Alt-Enter */
1495 if (((parm & ~(KEY_M_CTRL | KEY_M_ALT)) == '\n') && (parm & (KEY_M_CTRL | KEY_M_ALT)))
1497 copy_prog_name ();
1498 return MSG_HANDLED;
1501 /* Ctrl-Shift-Enter */
1502 if (parm == (KEY_M_CTRL | KEY_M_SHIFT | '\n'))
1504 copy_current_pathname ();
1505 copy_prog_name ();
1506 return MSG_HANDLED;
1509 if ((!alternate_plus_minus || !(console_flag || xterm_flag))
1510 && !quote && !current_panel->searching)
1512 if (!only_leading_plus_minus)
1514 /* Special treatement, since the input line will eat them */
1515 if (parm == '+')
1517 select_cmd ();
1518 return MSG_HANDLED;
1521 if (parm == '\\' || parm == '-')
1523 unselect_cmd ();
1524 return MSG_HANDLED;
1527 if (parm == '*')
1529 reverse_selection_cmd ();
1530 return MSG_HANDLED;
1533 else if (!command_prompt || !cmdline->buffer[0])
1535 /* Special treatement '+', '-', '\', '*' only when this is
1536 * first char on input line
1539 if (parm == '+')
1541 select_cmd ();
1542 return MSG_HANDLED;
1545 if (parm == '\\' || parm == '-')
1547 unselect_cmd ();
1548 return MSG_HANDLED;
1551 if (parm == '*')
1553 reverse_selection_cmd ();
1554 return MSG_HANDLED;
1558 return MSG_NOT_HANDLED;
1560 case DLG_HOTKEY_HANDLED:
1561 if ((get_current_type () == view_listing) && current_panel->searching)
1563 current_panel->dirty = 1; /* FIXME: unneeded? */
1564 send_message ((Widget *) current_panel, WIDGET_COMMAND, CK_PanelStopSearch);
1566 return MSG_HANDLED;
1568 case DLG_UNHANDLED_KEY:
1569 if (command_prompt)
1571 cb_ret_t v;
1573 v = send_message ((Widget *) cmdline, WIDGET_KEY, parm);
1574 if (v == MSG_HANDLED)
1575 return MSG_HANDLED;
1578 if (ctl_x_map_enabled)
1580 ctl_x_map_enabled = FALSE;
1581 command = lookup_keymap_command (main_x_map, parm);
1583 else
1584 command = lookup_keymap_command (main_map, parm);
1586 return (command == CK_Ignore_Key) ? MSG_NOT_HANDLED : midnight_execute_cmd (NULL, command);
1588 case DLG_POST_KEY:
1589 if (!the_menubar->is_active)
1590 update_dirty_panels ();
1591 return MSG_HANDLED;
1593 case DLG_ACTION:
1594 /* shortcut */
1595 if (sender == NULL)
1596 midnight_execute_cmd (NULL, parm);
1597 /* message from menu */
1598 else if (sender == (Widget *) the_menubar)
1599 midnight_execute_cmd (sender, parm);
1600 /* message from buttonbar */
1601 else if (sender == (Widget *) the_bar)
1603 if (data == NULL)
1604 midnight_execute_cmd (sender, parm);
1605 else
1606 send_message ((Widget *) data, WIDGET_COMMAND, parm);
1608 return MSG_HANDLED;
1610 default:
1611 return default_dlg_callback (h, sender, msg, parm, data);
1615 /* Show current directory in the xterm title */
1616 void
1617 update_xterm_title_path (void)
1619 /* TODO: share code with midnight_get_title () */
1621 const char *path;
1622 char host[BUF_TINY];
1623 char *p;
1624 struct passwd *pw = NULL;
1625 char *login = NULL;
1626 int res = 0;
1628 if (xterm_flag && xterm_title)
1630 path = strip_home_and_password (current_panel->cwd);
1631 res = gethostname (host, sizeof (host));
1632 if (res)
1633 { /* On success, res = 0 */
1634 host[0] = '\0';
1636 else
1638 host[sizeof (host) - 1] = '\0';
1640 pw = getpwuid (getuid ());
1641 if (pw)
1643 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
1645 else
1647 login = g_strdup (host);
1649 p = g_strdup_printf ("mc [%s]:%s", login, path);
1650 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
1651 g_free (login);
1652 g_free (p);
1653 if (!alternate_plus_minus)
1654 numeric_keypad_mode ();
1655 fflush (stdout);
1660 * Load new hint and display it.
1661 * IF force is not 0, ignore the timeout.
1663 void
1664 load_hint (int force)
1666 char *hint;
1668 if (!the_hint->widget.owner)
1669 return;
1671 if (!message_visible)
1673 label_set_text (the_hint, NULL);
1674 return;
1677 hint = get_random_hint (force);
1679 if (hint != NULL)
1681 if (*hint)
1682 set_hintbar (hint);
1683 g_free (hint);
1685 else
1687 char text[BUF_SMALL];
1689 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1690 set_hintbar (text);
1694 static void
1695 create_panels_and_run_mc (void)
1697 midnight_dlg->get_shortcut = midnight_get_shortcut;
1698 midnight_dlg->get_title = midnight_get_title;
1700 create_panels ();
1702 add_widget (midnight_dlg, the_menubar);
1703 init_menu ();
1705 add_widget (midnight_dlg, get_panel_widget (0));
1706 add_widget (midnight_dlg, get_panel_widget (1));
1708 add_widget (midnight_dlg, the_hint);
1709 add_widget (midnight_dlg, cmdline);
1710 add_widget (midnight_dlg, the_prompt);
1712 add_widget (midnight_dlg, the_bar);
1713 midnight_set_buttonbar (the_bar);
1715 /* Run the Midnight Commander if no file was specified in the command line */
1716 run_dlg (midnight_dlg);
1719 /* result must be free'd (I think this should go in util.c) */
1720 static char *
1721 prepend_cwd_on_local (const char *filename)
1723 char *d;
1724 size_t l;
1726 if (!vfs_file_is_local (filename) || g_path_is_absolute (filename))
1727 return g_strdup (filename);
1729 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1730 mc_get_current_wd (d, MC_MAXPATHLEN);
1731 l = strlen (d);
1732 d[l++] = PATH_SEP;
1733 strcpy (d + l, filename);
1734 canonicalize_pathname (d);
1735 return d;
1738 /* Invoke the internal view/edit routine with:
1739 * the default processing and forcing the internal viewer/editor
1741 static void
1742 mc_maybe_editor_or_viewer (void)
1744 switch (mc_run_mode)
1746 #ifdef USE_INTERNAL_EDIT
1747 case MC_RUN_EDITOR:
1748 edit_file (mc_run_param0, mc_args__edit_start_line);
1749 break;
1750 #endif /* USE_INTERNAL_EDIT */
1751 case MC_RUN_VIEWER:
1753 char *path;
1754 path = prepend_cwd_on_local (mc_run_param0);
1755 view_file (path, 0, 1);
1756 g_free (path);
1757 break;
1759 #ifdef USE_DIFF_VIEW
1760 case MC_RUN_DIFFVIEWER:
1761 diff_view (mc_run_param0, mc_run_param1, mc_run_param0, mc_run_param1);
1762 break;
1763 #endif /* USE_DIFF_VIEW */
1764 default:
1765 break;
1767 midnight_shutdown = 1;
1770 /* Run the main dialog that occupies the whole screen */
1771 static void
1772 do_nc (void)
1774 int midnight_colors[DLG_COLOR_NUM];
1775 midnight_colors[0] = mc_skin_color_get ("dialog", "_default_");
1776 midnight_colors[1] = mc_skin_color_get ("dialog", "focus");
1777 midnight_colors[2] = mc_skin_color_get ("dialog", "hotnormal");
1778 midnight_colors[3] = mc_skin_color_get ("dialog", "hotfocus");
1780 panel_init ();
1782 midnight_dlg = create_dlg (FALSE, 0, 0, LINES, COLS, midnight_colors, midnight_callback,
1783 "[main]", NULL, DLG_WANT_IDLE);
1785 if (mc_run_mode == MC_RUN_FULL)
1786 setup_mc ();
1787 else
1788 setup_dummy_mc ();
1790 /* start check display_codepage and source_codepage */
1791 check_codeset ();
1793 /* Check if we were invoked as an editor or file viewer */
1794 if (mc_run_mode != MC_RUN_FULL)
1795 mc_maybe_editor_or_viewer ();
1796 else
1798 create_panels_and_run_mc ();
1800 /* Program end */
1801 midnight_shutdown = 1;
1803 /* destroy_dlg destroys even current_panel->cwd, so we have to save a copy :) */
1804 if (mc_args__last_wd_file && vfs_current_is_local ())
1805 last_wd_string = g_strdup (current_panel->cwd);
1809 dialog_switch_shutdown ();
1810 done_mc ();
1811 destroy_dlg (midnight_dlg);
1812 panel_deinit ();
1813 current_panel = 0;
1814 done_setup ();
1817 /* POSIX version. The only version we support. */
1818 static void
1819 OS_Setup (void)
1821 const char *shell_env = getenv ("SHELL");
1822 const char *mc_libdir;
1824 if ((shell_env == NULL) || (shell_env[0] == '\0'))
1826 struct passwd *pwd;
1827 pwd = getpwuid (geteuid ());
1828 if (pwd != NULL)
1829 shell = g_strdup (pwd->pw_shell);
1831 else
1832 shell = g_strdup (shell_env);
1834 if ((shell == NULL) || (shell[0] == '\0'))
1836 g_free (shell);
1837 shell = g_strdup ("/bin/sh");
1840 /* This is the directory, where MC was installed, on Unix this is DATADIR */
1841 /* and can be overriden by the MC_DATADIR environment variable */
1842 mc_libdir = getenv ("MC_DATADIR");
1843 if (mc_libdir != NULL)
1845 mc_home = g_strdup (mc_libdir);
1846 mc_home_alt = g_strdup (SYSCONFDIR);
1848 else
1850 mc_home = g_strdup (SYSCONFDIR);
1851 mc_home_alt = g_strdup (DATADIR);
1854 /* This variable is used by the subshell */
1855 home_dir = getenv ("HOME");
1857 if (!home_dir)
1858 home_dir = mc_home;
1861 static void
1862 sigchld_handler_no_subshell (int sig)
1864 #ifdef __linux__
1865 int pid, status;
1867 if (!console_flag)
1868 return;
1870 /* COMMENT: if it were true that after the call to handle_console(..INIT)
1871 the value of console_flag never changed, we could simply not install
1872 this handler at all if (!console_flag && !use_subshell). */
1874 /* That comment is no longer true. We need to wait() on a sigchld
1875 handler (that's at least what the tarfs code expects currently). */
1877 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
1879 if (pid == cons_saver_pid)
1882 if (WIFSTOPPED (status))
1884 /* Someone has stopped cons.saver - restart it */
1885 kill (pid, SIGCONT);
1887 else
1889 /* cons.saver has died - disable console saving */
1890 handle_console (CONSOLE_DONE);
1891 console_flag = 0;
1894 /* If we got here, some other child exited; ignore it */
1895 #endif /* __linux__ */
1897 (void) sig;
1900 static void
1901 init_sigchld (void)
1903 struct sigaction sigchld_action;
1905 sigchld_action.sa_handler =
1906 #ifdef HAVE_SUBSHELL_SUPPORT
1907 use_subshell ? sigchld_handler :
1908 #endif /* HAVE_SUBSHELL_SUPPORT */
1909 sigchld_handler_no_subshell;
1911 sigemptyset (&sigchld_action.sa_mask);
1913 #ifdef SA_RESTART
1914 sigchld_action.sa_flags = SA_RESTART;
1915 #else
1916 sigchld_action.sa_flags = 0;
1917 #endif /* !SA_RESTART */
1919 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
1921 #ifdef HAVE_SUBSHELL_SUPPORT
1923 * This may happen on QNX Neutrino 6, where SA_RESTART
1924 * is defined but not implemented. Fallback to no subshell.
1926 use_subshell = 0;
1927 #endif /* HAVE_SUBSHELL_SUPPORT */
1932 main (int argc, char *argv[])
1934 struct stat s;
1935 char *mc_dir;
1936 GError *error = NULL;
1937 gboolean isInitialized;
1939 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
1940 setlocale (LC_ALL, "");
1941 bindtextdomain ("mc", LOCALEDIR);
1942 textdomain ("mc");
1944 /* Set up temporary directory */
1945 mc_tmpdir ();
1947 OS_Setup ();
1949 str_init_strings (NULL);
1951 vfs_init ();
1953 #ifdef USE_INTERNAL_EDIT
1954 edit_stack_init ();
1955 #endif
1957 #ifdef HAVE_SLANG
1958 SLtt_Ignore_Beep = 1;
1959 #endif
1961 if (!mc_args_handle (argc, argv, "mc"))
1962 exit (EXIT_FAILURE);
1964 /* NOTE: This has to be called before tty_init or whatever routine
1965 calls any define_sequence */
1966 init_key ();
1968 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
1969 handle_console (CONSOLE_INIT);
1971 #ifdef HAVE_SUBSHELL_SUPPORT
1972 /* Don't use subshell when invoked as viewer or editor */
1973 if (mc_run_mode != MC_RUN_FULL)
1974 use_subshell = 0;
1976 if (use_subshell)
1977 subshell_get_console_attributes ();
1978 #endif /* HAVE_SUBSHELL_SUPPORT */
1980 /* Install the SIGCHLD handler; must be done before init_subshell() */
1981 init_sigchld ();
1983 /* We need this, since ncurses endwin () doesn't restore the signals */
1984 save_stop_handler ();
1986 /* Must be done before init_subshell, to set up the terminal size: */
1987 /* FIXME: Should be removed and LINES and COLS computed on subshell */
1988 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
1990 load_setup ();
1992 /* Removing this from the X code let's us type C-c */
1993 load_key_defs ();
1995 load_keymap_defs ();
1997 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
1999 isInitialized = mc_skin_init (&error);
2001 mc_filehighlight = mc_fhl_new (TRUE);
2003 dlg_set_default_colors ();
2005 if (!isInitialized)
2007 message (D_ERROR, _("Warning"), "%s", error->message);
2008 g_error_free (error);
2009 error = NULL;
2012 /* create home directory */
2013 /* do it after the screen library initialization to show the error message */
2014 mc_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
2015 canonicalize_pathname (mc_dir);
2016 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
2017 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
2018 g_free (mc_dir);
2020 #ifdef HAVE_SUBSHELL_SUPPORT
2021 /* Done here to ensure that the subshell doesn't */
2022 /* inherit the file descriptors opened below, etc */
2023 if (use_subshell)
2024 init_subshell ();
2026 #endif /* HAVE_SUBSHELL_SUPPORT */
2028 /* Also done after init_subshell, to save any shell init file messages */
2029 if (console_flag)
2030 handle_console (CONSOLE_SAVE);
2032 if (alternate_plus_minus)
2033 application_keypad_mode ();
2035 #ifdef HAVE_SUBSHELL_SUPPORT
2036 if (use_subshell)
2038 mc_prompt = strip_ctrl_codes (subshell_prompt);
2039 if (mc_prompt == NULL)
2040 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2042 else
2043 #endif /* HAVE_SUBSHELL_SUPPORT */
2044 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
2046 /* Program main loop */
2047 if (!midnight_shutdown)
2048 do_nc ();
2050 /* Save the tree store */
2051 tree_store_save ();
2053 free_keymap_defs ();
2055 /* Virtual File System shutdown */
2056 vfs_shut ();
2058 flush_extension_file (); /* does only free memory */
2060 mc_fhl_free (&mc_filehighlight);
2061 mc_skin_deinit ();
2063 tty_shutdown ();
2065 if (console_flag && !(quit & SUBSHELL_EXIT))
2066 handle_console (CONSOLE_RESTORE);
2067 if (alternate_plus_minus)
2068 numeric_keypad_mode ();
2070 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2072 if (console_flag)
2073 handle_console (CONSOLE_DONE);
2074 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2076 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file && last_wd_string && !print_last_revert)
2078 int last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
2079 S_IRUSR | S_IWUSR);
2081 if (last_wd_fd != -1)
2083 ssize_t ret1;
2084 int ret2;
2085 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
2086 ret2 = close (last_wd_fd);
2089 g_free (last_wd_string);
2091 g_free (mc_home_alt);
2092 g_free (mc_home);
2093 g_free (shell);
2095 done_key ();
2096 #ifdef HAVE_CHARSET
2097 free_codepages_list ();
2098 g_free (autodetect_codeset);
2099 #endif
2100 g_free (clipboard_store_path);
2101 g_free (clipboard_paste_path);
2103 str_uninit_strings ();
2105 g_free (mc_run_param0);
2106 g_free (mc_run_param1);
2108 #ifdef USE_INTERNAL_EDIT
2109 edit_stack_free ();
2110 #endif
2112 return 0;