* main.c [NATIVE_WIN32]: Remove -S option, it's useless.
[midnight-commander.git] / src / main.c
blob5950bb984e994e727ba9a7ee52294473dd5aa856
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997 The Free Software Foundation
4 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
5 1994, 1995 Janne Kukonlehto
6 1997 Norbert Warmuth
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <locale.h>
25 #ifdef NATIVE_WIN32
26 # include <windows.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
34 #include <sys/stat.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
40 #include <errno.h>
41 #include <ctype.h>
42 #include <fcntl.h> /* For O_RDWR */
43 #include <signal.h>
45 /* Program include files */
46 #include "global.h"
47 #include "tty.h"
48 #include "dir.h"
49 #include "color.h"
50 #include "dialog.h"
51 #include "menu.h"
52 #include "file.h"
53 #include "panel.h"
54 #include "main.h"
55 #include "win.h"
56 #include "user.h"
57 #include "mouse.h"
58 #include "option.h"
59 #include "tree.h"
60 #include "cons.saver.h"
61 #include "subshell.h"
62 #include "key.h" /* For init_key() and mi_getch() */
63 #include "setup.h" /* save_setup() */
64 #include "profile.h" /* free_profiles() */
65 #include "boxes.h"
66 #include "layout.h"
67 #include "cmd.h" /* Normal commands */
68 #include "hotlist.h"
69 #include "panelize.h"
70 #include "learn.h"
71 #include "listmode.h"
72 #include "background.h"
73 #include "ext.h" /* For flush_extension_file() */
75 /* Listbox for the command history feature */
76 #include "widget.h"
77 #include "command.h"
78 #include "wtools.h"
79 #include "complete.h" /* For the free_completion */
81 #include "chmod.h"
82 #include "chown.h"
84 #ifdef NATIVE_WIN32
85 # include "drive.h"
86 #endif
88 #include "../vfs/vfs.h"
90 #ifdef WITH_SMBFS
91 #include "../vfs/smbfs.h" /* smbfs_set_debug() */
92 #endif
94 #ifdef HAVE_CHARSET
95 #include "charsets.h"
96 #endif /* HAVE_CHARSET */
98 #include "popt.h"
100 /* "$Id$" */
102 /* When the modes are active, left_panel, right_panel and tree_panel */
103 /* Point to a proper data structure. You should check with the functions */
104 /* get_current_type and get_other_type the types of the panels before using */
105 /* This pointer variables */
107 /* The structures for the panels */
108 WPanel *left_panel;
109 WPanel *right_panel;
111 /* The pointer to the tree */
112 WTree *the_tree;
114 /* The Menubar */
115 WMenu *the_menubar;
117 /* Pointers to the selected and unselected panel */
118 WPanel *current_panel = NULL;
120 /* Set when main loop should be terminated */
121 volatile int quit = 0;
123 /* Set if you want the possible completions dialog for the first time */
124 int show_all_if_ambiguous = 0;
126 /* Set when cd symlink following is desirable (bash mode) */
127 int cd_symlinks = 1;
129 /* If set then dialogs just clean the screen when refreshing, else */
130 /* they do a complete refresh, refreshing all the parts of the program */
131 int fast_refresh = 0;
133 /* If true, marking a files moves the cursor down */
134 int mark_moves_down = 1;
136 /* If true, at startup the user-menu is invoked */
137 int auto_menu = 0;
139 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
140 and M-- and keypad + / - */
141 int alternate_plus_minus = 0;
143 /* If true, then the +, - and \ keys have their special meaning only if the
144 * command line is emtpy, otherwise they behave like regular letters
146 int only_leading_plus_minus = 1;
148 /* If true, after executing a command, wait for a keystroke */
149 enum { pause_never, pause_on_dumb_terminals, pause_always };
151 int pause_after_run = pause_on_dumb_terminals;
153 /* It true saves the setup when quitting */
154 int auto_save_setup = 1;
156 #ifndef HAVE_CHARSET
157 /* If true, be eight bit clean */
158 int eight_bit_clean = 1;
160 /* If true, then display chars 0-255, else iso-8859-1,
161 requires eight_bit_clean */
162 int full_eight_bits = 1;
163 #endif /* !HAVE_CHARSET */
165 /* If true use the internal viewer */
166 int use_internal_view = 1;
168 /* Have we shown the fast-reload warning in the past? */
169 int fast_reload_w = 0;
171 /* Move page/item? When clicking on the top or bottom of a panel */
172 int mouse_move_pages = 1;
174 /* If true: l&r arrows are used to chdir if the input line is empty */
175 int navigate_with_arrows = 0;
177 /* If true use +, -, | for line drawing */
178 int force_ugly_line_drawing = 0;
180 /* If true message "The shell is already running a command" never */
181 int force_subshell_execution = 0;
183 /* If true program softkeys (HP terminals only) on startup and after every
184 command ran in the subshell to the description found in the termcap/terminfo
185 database */
186 int reset_hp_softkeys = 0;
188 /* The prompt */
189 char *prompt = 0;
191 /* The widget where we draw the prompt */
192 WLabel *the_prompt;
194 /* The hint bar */
195 WLabel *the_hint;
197 /* The button bar */
198 WButtonBar *the_bar;
200 /* For slow terminals */
201 int slow_terminal = 0;
203 /* Mouse type: GPM, xterm or none */
204 Mouse_Type use_mouse_p = MOUSE_NONE;
206 /* If true, assume we are running on an xterm terminal */
207 static int force_xterm = 0;
209 /* Controls screen clearing before an exec */
210 int clear_before_exec = 1;
212 /* Asks for confirmation before deleting a file */
213 int confirm_delete = 1;
215 /* Asks for confirmation before overwriting a file */
216 int confirm_overwrite = 1;
218 /* Asks for confirmation before executing a program by pressing enter */
219 int confirm_execute = 0;
221 /* Asks for confirmation before leaving the program */
222 int confirm_exit = 1;
224 /* Asks for confirmation when using F3 to view a directory and there
225 are tagged files */
226 int confirm_view_dir = 0;
228 /* This flag indicates if the pull down menus by default drop down */
229 int drop_menus = 0;
231 /* The dialog handle for the main program */
232 Dlg_head *midnight_dlg;
234 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
235 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
236 int update_prompt = 0;
238 #if 0
239 /* The name which was used to invoke mc */
240 char *program_name;
241 #endif
243 /* The home directory */
244 char *home_dir;
246 /* The value of the other directory, only used when loading the setup */
247 char *other_dir = 0;
249 /* Only used at program boot */
250 int boot_current_is_left = 1;
252 char *this_dir = 0;
254 /* If this is true, then when browsing the tree the other window will
255 * automatically reload it's directory with the contents of the currently
256 * selected directory.
258 int xtree_mode = 0;
260 /* If true, then print on stdout the last directory we were at */
261 static int print_last_wd = 0;
262 static char *last_wd_string;
263 static int print_last_revert = 0;
265 /* Force colors, only used by Slang */
266 int force_colors = 0;
268 /* colors specified on the command line: they override any other setting */
269 char *command_line_colors;
271 /* File name to view if argument was supplied */
272 char *view_one_file = 0;
274 /* File name to edit if argument was supplied */
275 char *edit_one_file = 0;
276 static int edit_one_file_start_line = 1;
278 /* Used so that widgets know if they are being destroyed or
279 shut down */
280 int midnight_shutdown = 0;
282 /* to show nice prompts */
283 static int last_paused = 0;
285 /* Used for keeping track of the original stdout */
286 int stdout_fd = 0;
288 /* The user's shell */
289 char *shell;
291 /* mc_home: The home of MC */
292 char *mc_home;
294 /* if on, it displays the information that files have been moved to ~/.mc */
295 int show_change_notice = 0;
297 char cmd_buf [512];
299 /* Used during argument processing */
300 int finish_program = 0;
302 WPanel *
303 get_current_panel (void)
305 return current_panel;
308 WPanel *
309 get_other_panel (void)
311 return (WPanel *) get_panel_widget (get_other_index ());
314 static void
315 reload_panelized (WPanel *panel)
317 int i, j;
318 dir_list *list = &panel->dir;
320 if (panel != cpanel)
321 mc_chdir (panel->cwd);
323 for (i = 0, j = 0; i < panel->count; i++){
324 if (list->list [i].f.marked) {
325 /* Unmark the file in advance. In case the following mc_lstat
326 * fails we are done, else we have to mark the file again
327 * (Note: do_file_mark depends on a valid "list->list [i].buf").
328 * IMO that's the best way to update the panel's summary status
329 * -- Norbert
331 do_file_mark (panel, i, 0);
333 if (mc_lstat (list->list [i].fname, &list->list [i].buf)){
334 g_free (list->list [i].fname);
335 continue;
337 if (list->list [i].f.marked)
338 do_file_mark (panel, i, 1);
339 if (j != i)
340 list->list [j] = list->list [i];
341 j++;
343 if (j == 0)
344 panel->count = set_zero_dir (list);
345 else
346 panel->count = j;
348 if (panel != cpanel)
349 mc_chdir (cpanel->cwd);
352 static void
353 update_one_panel_widget (WPanel *panel, int force_update, char *current_file)
355 int free_pointer;
357 if (force_update & UP_RELOAD){
358 panel->is_panelized = 0;
360 #if 1
361 ftpfs_flushdir ();
362 #endif
363 /* FIXME: Should supply flushdir method */
364 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
367 /* If current_file == -1 (an invalid pointer) then preserve selection */
368 if (current_file == UP_KEEPSEL){
369 free_pointer = 1;
370 current_file = g_strdup (panel->dir.list [panel->selected].fname);
371 } else
372 free_pointer = 0;
374 if (panel->is_panelized)
375 reload_panelized (panel);
376 else
377 panel_reload (panel);
379 try_to_select (panel, current_file);
380 panel->dirty = 1;
382 if (free_pointer)
383 g_free (current_file);
386 void
387 panel_clean_dir (WPanel *panel)
389 int count = panel->count;
391 panel->count = 0;
392 panel->top_file = 0;
393 panel->selected = 0;
394 panel->marked = 0;
395 panel->dirs_marked = 0;
396 panel->total = 0;
397 panel->searching = 0;
398 panel->is_panelized = 0;
400 clean_dir (&panel->dir, count);
403 static void
404 update_one_panel (int which, int force_update, char *current_file)
406 WPanel *panel;
408 if (get_display_type (which) != view_listing)
409 return;
411 panel = (WPanel *) get_panel_widget (which);
412 update_one_panel_widget (panel, force_update, current_file);
415 /* This routine reloads the directory in both panels. It tries to
416 * select current_file in current_panel and other_file in other_panel.
417 * If current_file == -1 then it automatically sets current_file and
418 * other_file to the currently selected files in the panels.
420 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
421 * will not reload the other panel.
423 void
424 update_panels (int force_update, char *current_file)
426 int reload_other = !(force_update & UP_ONLY_CURRENT);
427 WPanel *panel;
429 update_one_panel (get_current_index (), force_update, current_file);
430 if (reload_other)
431 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
433 if (get_current_type () == view_listing)
434 panel = (WPanel *) get_panel_widget (get_current_index ());
435 else
436 panel = (WPanel *) get_panel_widget (get_other_index ());
438 mc_chdir (panel->cwd);
441 /* Sets up the terminal before executing a program */
442 static void
443 pre_exec (void)
445 use_dash (0);
446 edition_pre_exec ();
449 /* Save current stat of directories to avoid reloading the panels */
450 /* when no modifications have taken place */
451 void
452 save_cwds_stat (void)
454 if (fast_reload){
455 mc_stat (cpanel->cwd, &(cpanel->dir_stat));
456 if (get_other_type () == view_listing)
457 mc_stat (opanel->cwd, &(opanel->dir_stat));
461 #ifdef HAVE_SUBSHELL_SUPPORT
462 void
463 do_possible_cd (char *new_dir)
465 if (!do_cd (new_dir, cd_exact))
466 message (1, _(" Warning "),
467 _(" The Commander can't change to the directory that \n"
468 " the subshell claims you are in. Perhaps you have \n"
469 " deleted your working directory, or given yourself \n"
470 " extra access permissions with the \"su\" command? "));
473 void
474 do_update_prompt (void)
476 if (update_prompt){
477 printf ("%s", subshell_prompt);
478 fflush (stdout);
479 update_prompt = 0;
482 #endif /* HAVE_SUBSHELL_SUPPORT */
484 void
485 restore_console (void)
487 handle_console (CONSOLE_RESTORE);
490 void
491 exec_shell (void)
493 do_execute (shell, 0, 0);
496 void
497 do_execute (const char *shell, const char *command, int flags)
499 #ifdef HAVE_SUBSHELL_SUPPORT
500 char *new_dir = NULL;
501 #endif /* HAVE_SUBSHELL_SUPPORT */
503 #ifdef USE_VFS
504 char *old_vfs_dir = 0;
506 if (!vfs_current_is_local ())
507 old_vfs_dir = g_strdup (vfs_get_current_dir ());
508 #endif /* USE_VFS */
510 save_cwds_stat ();
511 pre_exec ();
512 if (console_flag)
513 restore_console ();
515 if (!use_subshell && !(flags & EXECUTE_INTERNAL && command)) {
516 printf ("%s%s%s\n", last_paused ? "\r\n":"", prompt, command);
517 last_paused = 0;
520 #ifdef HAVE_SUBSHELL_SUPPORT
521 if (use_subshell && !(flags & EXECUTE_INTERNAL)){
522 do_update_prompt ();
524 /* We don't care if it died, higher level takes care of this */
525 #ifdef USE_VFS
526 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
527 #else
528 invoke_subshell (command, VISIBLY, &new_dir);
529 #endif /* !USE_VFS */
530 } else
531 #endif /* HAVE_SUBSHELL_SUPPORT */
532 my_system (flags, shell, command);
534 if (!(flags & EXECUTE_INTERNAL)) {
535 if ((pause_after_run == pause_always ||
536 (pause_after_run == pause_on_dumb_terminals &&
537 !xterm_flag && !console_flag)) && !quit
538 #ifdef HAVE_SUBSHELL_SUPPORT
539 && subshell_state != RUNNING_COMMAND
540 #endif /* HAVE_SUBSHELL_SUPPORT */
542 printf ("%s\r\n", _("Press any key to continue..."));
543 last_paused = 1;
544 fflush (stdout);
545 mc_raw_mode ();
546 getch ();
548 if (console_flag) {
549 if (output_lines && keybar_visible) {
550 putchar('\n');
551 fflush(stdout);
556 if (console_flag)
557 handle_console (CONSOLE_SAVE);
558 edition_post_exec ();
560 #ifdef HAVE_SUBSHELL_SUPPORT
561 if (new_dir)
562 do_possible_cd (new_dir);
564 #endif /* HAVE_SUBSHELL_SUPPORT */
566 #ifdef USE_VFS
567 if (old_vfs_dir){
568 mc_chdir (old_vfs_dir);
569 g_free (old_vfs_dir);
571 #endif /* USE_VFS */
573 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
575 do_refresh ();
576 use_dash (TRUE);
579 /* Executes a command */
580 void
581 shell_execute (char *command, int flags)
583 #ifdef HAVE_SUBSHELL_SUPPORT
584 if (use_subshell)
585 if (subshell_state == INACTIVE || force_subshell_execution)
586 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
587 else
588 message (1, MSG_ERROR, _(" The shell is already running a command "));
589 else
590 #endif /* HAVE_SUBSHELL_SUPPORT */
591 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
594 void
595 execute (char *command)
597 shell_execute (command, 0);
600 void
601 change_panel (void)
603 free_completions (input_w (cmdline));
604 dlg_one_down (midnight_dlg);
607 /* Stop MC main dialog and the current dialog if it exists.
608 * Needed to provide fast exit from MC viewer or editor on shell exit */
609 static void stop_dialogs (void)
611 midnight_dlg->running = 0;
612 if (current_dlg) {
613 current_dlg->running = 0;
617 static int
618 quit_cmd_internal (int quiet)
620 int q = quit;
622 if (quiet || !confirm_exit){
623 q = 1;
624 } else {
625 if (query_dialog (_(" The Midnight Commander "),
626 _(" Do you really want to quit the Midnight Commander? "),
627 0, 2, _("&Yes"), _("&No")) == 0)
628 q = 1;
630 if (q){
631 #ifdef HAVE_SUBSHELL_SUPPORT
632 if (!use_subshell)
633 stop_dialogs ();
634 else
635 if ((q = exit_subshell ()))
636 #endif
637 stop_dialogs ();
639 if (q)
640 quit |= 1;
641 return quit;
644 int quit_cmd (void)
646 quit_cmd_internal (0);
647 return quit;
650 int quiet_quit_cmd (void)
652 print_last_revert = 1;
653 quit_cmd_internal (1);
654 return quit;
658 * Touch window and refresh window functions
661 /* This routine untouches the first line on both panels in order */
662 /* to avoid the refreshing the menu bar */
664 void
665 untouch_bar (void)
667 do_refresh ();
670 void
671 repaint_screen (void)
673 do_refresh ();
674 mc_refresh ();
677 /* Wrapper for do_subshell_chdir, check for availability of subshell */
678 void
679 subshell_chdir (char *directory)
681 #ifdef HAVE_SUBSHELL_SUPPORT
682 if (use_subshell){
683 if (vfs_current_is_local ())
684 do_subshell_chdir (directory, 0, 1);
686 #endif /* HAVE_SUBSHELL_SUPPORT */
689 void
690 directory_history_add (WPanel * panel, char *s)
692 if (!panel->dir_history) {
693 panel->dir_history = g_new0 (Hist, 1);
694 panel->dir_history->text = g_strdup (s);
695 strip_password (panel->dir_history->text, 1);
696 return;
698 if (!strcmp (panel->dir_history->text, s))
699 return;
700 if (panel->dir_history->next) {
701 if (panel->dir_history->next->text) {
702 g_free (panel->dir_history->next->text);
703 panel->dir_history->next->text = 0;
705 } else {
706 panel->dir_history->next = g_new0 (Hist, 1);
707 panel->dir_history->next->prev = panel->dir_history;
709 panel->dir_history = panel->dir_history->next;
710 panel->dir_history->text = g_strdup (s);
711 strip_password (panel->dir_history->text, 1);
715 * If we moved to the parent directory move the selection pointer to
716 * the old directory name; If we leave VFS dir, remove FS specificator.
717 * Warn: This code spoils lwd string.
719 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
721 static char*
722 get_parent_dir_name (char *cwd, char *lwd)
724 char *p;
725 if (strlen (lwd) > strlen (cwd))
726 if((p=strrchr(lwd, PATH_SEP)) && !strncmp(cwd, lwd, p-lwd)){
727 return (p+1);
729 return NULL;
732 /* Changes the current panel directory */
733 static int
734 _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
736 char *directory, *olddir;
737 char temp [MC_MAXPATHLEN];
738 char *translated_url;
739 #ifdef USE_VFS
740 vfs *oldvfs;
741 vfsid oldvfsid;
742 struct vfs_stamping *parent;
743 #endif
744 olddir = g_strdup (panel->cwd);
745 translated_url = new_dir = vfs_translate_url (new_dir);
747 /* Convert *new_path to a suitable pathname, handle ~user */
749 if (cd_type == cd_parse_command){
750 while (*new_dir == ' ')
751 new_dir++;
753 if (!strcmp (new_dir, "-")){
754 strcpy (temp, panel->lwd);
755 new_dir = temp;
758 directory = *new_dir ? new_dir : home_dir;
760 if (mc_chdir (directory) == -1){
761 strcpy (panel->cwd, olddir);
762 g_free (olddir);
763 g_free (translated_url);
764 return 0;
766 g_free (translated_url);
768 /* Success: save previous directory, shutdown status of previous dir */
769 strcpy (panel->lwd, olddir);
770 free_completions (input_w (cmdline));
772 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
774 #ifdef USE_VFS
775 oldvfs = vfs_type (olddir);
776 oldvfsid = vfs_ncs_getid (oldvfs, olddir, &parent);
777 vfs_add_noncurrent_stamps (oldvfs, oldvfsid, parent);
778 vfs_rm_parents (parent);
779 #endif
781 subshell_chdir (panel->cwd);
783 /* Reload current panel */
784 panel_clean_dir (panel);
785 panel->count = do_load_dir (&panel->dir, panel->sort_type,
786 panel->reverse, panel->case_sensitive, panel->filter);
787 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
788 load_hint ();
789 panel_update_contents (panel);
791 g_free (olddir);
793 return 1;
797 do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
799 int r;
801 r = _do_panel_cd (panel, new_dir, cd_type);
802 if (r)
803 directory_history_add (cpanel, cpanel->cwd);
804 return r;
808 do_cd (char *new_dir, enum cd_enum exact)
810 return (do_panel_cd (cpanel, new_dir, exact));
813 void
814 directory_history_next (WPanel * panel)
816 if (!panel->dir_history->next)
817 return;
818 if (_do_panel_cd (panel, panel->dir_history->next->text, cd_exact))
819 panel->dir_history = panel->dir_history->next;
822 void
823 directory_history_prev (WPanel * panel)
825 if (!panel->dir_history->prev)
826 return;
827 if (_do_panel_cd (panel, panel->dir_history->prev->text, cd_exact))
828 panel->dir_history = panel->dir_history->prev;
831 void
832 directory_history_list (WPanel * panel)
834 char *s;
835 /* must be at least two to show a history */
836 if (panel->dir_history) {
837 if (panel->dir_history->prev || panel->dir_history->next) {
838 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
839 if (s) {
840 int r;
841 r = _do_panel_cd (panel, s, cd_exact);
842 if (r)
843 directory_history_add (panel, panel->cwd);
844 else
845 message (1, MSG_ERROR, _("Could not change directory") );
846 g_free (s);
852 #ifdef HAVE_SUBSHELL_SUPPORT
854 load_prompt (int fd, void *unused)
856 if (!read_subshell_prompt ())
857 return 0;
859 /* Don't actually change the prompt if it's invisible */
860 if (current_dlg == midnight_dlg && command_prompt){
861 int prompt_len;
863 prompt = strip_ctrl_codes (subshell_prompt);
864 prompt_len = strlen (prompt);
866 /* Check for prompts too big */
867 if (COLS > 8 && prompt_len > COLS - 8) {
868 prompt [COLS - 8 ] = 0;
869 prompt_len = COLS - 8;
871 label_set_text (the_prompt, prompt);
872 winput_set_origin ((WInput *)cmdline, prompt_len, COLS-prompt_len);
874 /* since the prompt has changed, and we are called from one of the
875 * get_event channels, the prompt updating does not take place
876 * automatically: force a cursor update and a screen refresh
878 update_cursor (midnight_dlg);
879 mc_refresh ();
881 update_prompt = 1;
882 return 0;
884 #endif /* HAVE_SUBSHELL_SUPPORT */
886 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
888 maybe_cd (int char_code, int move_up_dir)
890 if (navigate_with_arrows){
891 if (!input_w (cmdline)->buffer [0]){
892 if (!move_up_dir){
893 do_cd ("..", cd_exact);
894 return 1;
896 if (S_ISDIR (selection (cpanel)->buf.st_mode)
897 || link_isdir (selection (cpanel))){
898 do_cd (selection (cpanel)->fname, cd_exact);
899 return 1;
903 return 0;
906 void
907 sort_cmd (void)
909 WPanel *p;
910 sortfn *sort_order;
912 if (!SELECTED_IS_PANEL)
913 return;
915 p = MENU_PANEL;
916 sort_order = sort_box (p->sort_type, &p->reverse, &p->case_sensitive);
918 panel_set_sort_order (p, sort_order);
922 static void
923 treebox_cmd (void)
925 char *sel_dir;
927 sel_dir = tree_box (selection (cpanel)->fname);
928 if (sel_dir){
929 do_cd(sel_dir, cd_exact);
930 g_free (sel_dir);
934 #ifdef LISTMODE_EDITOR
935 static void
936 listmode_cmd (void)
938 char *newmode;
939 newmode = listmode_edit ("half <type,>name,|,size:8,|,perm:4+");
940 message (0, _(" Listing format edit "), _(" New mode is \"%s\" "), newmode);
941 g_free (newmode);
943 #endif /* LISTMODE_EDITOR */
945 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
946 static menu_entry PanelMenu [] = {
947 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
948 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
949 { ' ', N_("&Info C-x i"), 'I', info_cmd },
950 { ' ', N_("&Tree"), 'T', tree_cmd },
951 { ' ', "", ' ', 0 },
952 { ' ', N_("&Sort order..."), 'S', sort_cmd },
953 { ' ', "", ' ', 0 },
954 { ' ', N_("&Filter..."), 'F', filter_cmd },
955 #ifdef USE_NETCODE
956 { ' ', "", ' ', 0 },
957 #ifdef WITH_MCFS
958 { ' ', N_("&Network link..."), 'N', netlink_cmd },
959 #endif
960 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
961 { ' ', N_("S&hell link..."), 'H', fishlink_cmd },
962 #ifdef WITH_SMBFS
963 { ' ', N_("SM&B link..."), 'B', smblink_cmd },
964 #endif
965 #endif
966 { ' ', "", ' ', 0 },
967 #ifdef NATIVE_WIN32
968 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_a },
969 #endif
970 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
973 static menu_entry RightMenu [] = {
974 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
975 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
976 { ' ', N_("&Info C-x i"), 'I', info_cmd },
977 { ' ', N_("&Tree"), 'T', tree_cmd },
978 { ' ', "", ' ', 0 },
979 { ' ', N_("&Sort order..."), 'S', sort_cmd },
980 { ' ', "", ' ', 0 },
981 { ' ', N_("&Filter..."), 'F', filter_cmd },
982 #ifdef USE_NETCODE
983 { ' ', "", ' ', 0 },
984 #ifdef WITH_MCFS
985 { ' ', N_("&Network link..."), 'N', netlink_cmd },
986 #endif
987 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
988 { ' ', N_("S&hell link..."), 'H', fishlink_cmd },
989 #ifdef WITH_SMBFS
990 { ' ', N_("SM&B link..."), 'B', smblink_cmd },
991 #endif
992 #endif
993 { ' ', "", ' ', 0 },
994 #ifdef NATIVE_WIN32
995 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_b },
996 #endif
997 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
1000 static menu_entry FileMenu [] = {
1001 { ' ', N_("&User menu F2"), 'U', user_file_menu_cmd },
1002 { ' ', N_("&View F3"), 'V', view_cmd },
1003 { ' ', N_("Vie&w file... "), 'W', view_file_cmd },
1004 { ' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd },
1005 { ' ', N_("&Edit F4"), 'E', edit_cmd },
1006 { ' ', N_("&Copy F5"), 'C', copy_cmd },
1007 { ' ', N_("c&Hmod C-x c"), 'H', chmod_cmd },
1008 #ifndef NATIVE_WIN32
1009 { ' ', N_("&Link C-x l"), 'L', link_cmd },
1010 { ' ', N_("&SymLink C-x s"), 'S', symlink_cmd },
1011 { ' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd },
1012 { ' ', N_("ch&Own C-x o"), 'O', chown_cmd },
1013 { ' ', N_("&Advanced chown "), 'A', chown_advanced_cmd },
1014 #endif
1015 { ' ', N_("&Rename/Move F6"), 'R', ren_cmd },
1016 { ' ', N_("&Mkdir F7"), 'M', mkdir_cmd },
1017 { ' ', N_("&Delete F8"), 'D', delete_cmd },
1018 { ' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd },
1019 { ' ', "", ' ', 0 },
1020 { ' ', N_("select &Group M-+"), 'G', select_cmd },
1021 { ' ', N_("u&Nselect group M-\\"),'N', unselect_cmd },
1022 { ' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd },
1023 { ' ', "", ' ', 0 },
1024 { ' ', N_("e&Xit F10"), 'X', (callfn) quit_cmd }
1027 static menu_entry CmdMenu [] = {
1028 /* I know, I'm lazy, but the tree widget when it's not running
1029 * as a panel still has some problems, I have not yet finished
1030 * the WTree widget port, sorry.
1032 { ' ', N_("&Directory tree"), 'D', treebox_cmd },
1033 { ' ', N_("&Find file M-?"), 'F', find_cmd },
1034 { ' ', N_("s&Wap panels C-u"), 'W', swap_cmd },
1035 { ' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd },
1036 { ' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd },
1037 { ' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize },
1038 { ' ', N_("show directory s&Izes"), 'I', dirsizes_cmd },
1039 { ' ', "", ' ', 0 },
1040 { ' ', N_("command &History"), 'H', history_cmd },
1041 { ' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd },
1042 #ifdef USE_VFS
1043 { ' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs },
1044 { ' ', N_("Fr&ee VFSs now"), 'E', free_vfs_now },
1045 #endif
1046 #ifdef WITH_BACKGROUND
1047 { ' ', N_("&Background jobs C-x j"), 'B', jobs_cmd },
1048 #endif
1049 { ' ', "", ' ', 0 },
1050 #ifdef USE_EXT2FSLIB
1051 { ' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd },
1052 #endif
1053 #ifdef LISTMODE_EDITOR
1054 { ' ', N_("&Listing format edit"), 'L', listmode_cmd},
1055 #endif
1056 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
1057 { ' ', "", ' ', 0 },
1058 #endif
1059 { ' ', N_("&Extension file edit"), 'E', ext_cmd },
1060 { ' ', N_("&Menu file edit"), 'M', menu_edit_cmd },
1061 #ifdef USE_INTERNAL_EDIT
1062 {' ', N_("Menu edi&Tor edit"), 'T', edit_user_menu_cmd},
1063 {' ', N_("&Syntax file edit"), 'S', edit_syntax_cmd}
1064 #endif /* USE_INTERNAL_EDIT */
1067 /* Must keep in sync with the constants in menu_cmd */
1068 static menu_entry OptMenu [] = {
1069 { ' ', N_("&Configuration..."), 'C', configure_box },
1070 { ' ', N_("&Layout..."), 'L', layout_cmd },
1071 { ' ', N_("c&Onfirmation..."), 'O', confirm_box },
1072 { ' ', N_("&Display bits..."), 'D', display_bits_box },
1073 #ifndef NATIVE_WIN32
1074 { ' ', N_("learn &Keys..."), 'K', learn_keys },
1075 #endif /* !NATIVE_WIN32 */
1076 #ifdef USE_VFS
1077 { ' ', N_("&Virtual FS..."), 'V', configure_vfs },
1078 #endif /* !USE_VFS */
1079 { ' ', "", ' ', 0 },
1080 { ' ', N_("&Save setup"), 'S', save_setup_cmd }
1083 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
1085 static Menu MenuBar [5];
1087 void
1088 init_menu (void)
1090 MenuBar [0] = create_menu ( horizontal_split ? _(" &Above ") : _(" &Left "),
1091 PanelMenu, menu_entries (PanelMenu),
1092 "[Left and Right Menus]");
1093 MenuBar [1] = create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
1094 "[File Menu]");
1095 MenuBar [2] = create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
1096 "[Command Menu]");
1097 MenuBar [3] = create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
1098 "[Options Menu]");
1099 MenuBar [4] = create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
1100 RightMenu, menu_entries (PanelMenu),
1101 "[Left and Right Menus]");
1104 void
1105 done_menu (void)
1107 int i;
1109 for (i = 0; i < 5; i++){
1110 destroy_menu (MenuBar [i]);
1114 static void
1115 menu_last_selected_cmd (void)
1117 the_menubar->active = 1;
1118 the_menubar->dropped = drop_menus;
1119 the_menubar->previous_selection = dlg_item_number (midnight_dlg);
1120 dlg_select_widget (midnight_dlg, the_menubar);
1123 static void
1124 menu_cmd (void)
1126 if (the_menubar->active)
1127 return;
1129 if (get_current_index () == 0)
1130 the_menubar->selected = 0;
1131 else
1132 the_menubar->selected = 4;
1133 menu_last_selected_cmd ();
1136 /* Flag toggling functions */
1137 void
1138 toggle_fast_reload (void)
1140 fast_reload = !fast_reload;
1141 if (fast_reload_w == 0 && fast_reload){
1142 message (0, _(" Information "),
1143 _(" Using the fast reload option may not reflect the exact \n"
1144 " directory contents. In this cases you'll need to do a \n"
1145 " manual reload of the directory. See the man page for \n"
1146 " the details. "));
1147 fast_reload_w = 1;
1151 void
1152 toggle_mix_all_files (void)
1154 mix_all_files = !mix_all_files;
1155 update_panels (UP_RELOAD, UP_KEEPSEL);
1158 void
1159 toggle_show_backup (void)
1161 show_backups = !show_backups;
1162 update_panels (UP_RELOAD, UP_KEEPSEL);
1165 void
1166 toggle_show_hidden (void)
1168 show_dot_files = !show_dot_files;
1169 update_panels (UP_RELOAD, UP_KEEPSEL);
1173 * Just a hack for allowing url-like pathnames to be accepted from the
1174 * command line.
1176 static void
1177 translated_mc_chdir (char *dir)
1179 char *newdir;
1181 newdir = vfs_translate_url (dir);
1182 mc_chdir (newdir);
1183 g_free (newdir);
1186 void
1187 create_panels (void)
1189 int current_index;
1190 int other_index;
1191 int current_mode;
1192 int other_mode;
1193 char original_dir [1024];
1195 original_dir [0] = 0;
1197 if (boot_current_is_left){
1198 current_index = 0;
1199 other_index = 1;
1200 current_mode = startup_left_mode;
1201 other_mode = startup_right_mode;
1202 } else {
1203 current_index = 1;
1204 other_index = 0;
1205 current_mode = startup_right_mode;
1206 other_mode = startup_left_mode;
1208 /* Creates the left panel */
1209 if (this_dir){
1210 if (other_dir){
1211 /* Ok, user has specified two dirs, save the original one,
1212 * since we may not be able to chdir to the proper
1213 * second directory later
1215 mc_get_current_wd (original_dir, sizeof (original_dir)-2);
1217 translated_mc_chdir (this_dir);
1219 set_display_type (current_index, current_mode);
1221 /* The other panel */
1222 if (other_dir){
1223 if (original_dir [0])
1224 translated_mc_chdir (original_dir);
1225 translated_mc_chdir (other_dir);
1227 set_display_type (other_index, other_mode);
1229 if (startup_left_mode == view_listing){
1230 current_panel = left_panel;
1231 } else {
1232 if (right_panel)
1233 current_panel = right_panel;
1234 else
1235 current_panel = left_panel;
1238 /* Create the nice widgets */
1239 cmdline = command_new (0, 0, 0);
1240 the_prompt = label_new (0, 0, prompt, NULL);
1241 the_prompt->transparent = 1;
1242 the_bar = buttonbar_new (keybar_visible);
1244 the_hint = label_new (0, 0, 0, NULL);
1245 the_hint->transparent = 1;
1246 the_hint->auto_adjust_cols = 0;
1247 the_hint->widget.cols = COLS;
1249 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1252 static void copy_current_pathname (void)
1254 if (!command_prompt)
1255 return;
1257 stuff (input_w (cmdline), cpanel->cwd, 0);
1258 if (cpanel->cwd [strlen (cpanel->cwd) - 1] != PATH_SEP)
1259 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1262 static void copy_other_pathname (void)
1264 if (get_other_type () != view_listing)
1265 return;
1267 if (!command_prompt)
1268 return;
1270 stuff (input_w (cmdline), opanel->cwd, 0);
1271 if (cpanel->cwd [strlen (opanel->cwd) - 1] != PATH_SEP)
1272 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1275 static void copy_readlink (WPanel *panel)
1277 if (!command_prompt)
1278 return;
1279 if (S_ISLNK (selection (panel)->buf.st_mode)) {
1280 char buffer [MC_MAXPATHLEN];
1281 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1282 int i;
1284 i = mc_readlink (p, buffer, MC_MAXPATHLEN);
1285 g_free (p);
1286 if (i > 0) {
1287 buffer [i] = 0;
1288 stuff (input_w (cmdline), buffer, 0);
1293 static void copy_current_readlink (void)
1295 copy_readlink (cpanel);
1298 static void copy_other_readlink (void)
1300 if (get_other_type () != view_listing)
1301 return;
1302 copy_readlink (opanel);
1305 /* Inserts the selected file name into the input line */
1306 /* Exported so that the command modules uses it */
1307 void copy_prog_name (void)
1309 char *tmp;
1310 if (!command_prompt)
1311 return;
1313 if (get_current_type () == view_tree){
1314 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1315 tmp = name_quote (tree->selected_ptr->name, 1);
1316 } else
1317 tmp = name_quote (selection (cpanel)->fname, 1);
1318 stuff (input_w (cmdline), tmp, 1);
1319 g_free (tmp);
1322 static void copy_tagged (WPanel *panel)
1324 int i;
1326 if (!command_prompt)
1327 return;
1328 input_disable_update (input_w (cmdline));
1329 if (panel->marked){
1330 for (i = 0; i < panel->count; i++)
1331 if (panel->dir.list [i].f.marked) {
1332 char *tmp = name_quote (panel->dir.list [i].fname, 1);
1333 stuff (input_w (cmdline), tmp, 1);
1334 g_free (tmp);
1336 } else {
1337 char *tmp = name_quote (panel->dir.list [panel->selected].fname, 1);
1338 stuff (input_w (cmdline), tmp, 1);
1339 g_free (tmp);
1341 input_enable_update (input_w (cmdline));
1344 static void copy_current_tagged (void)
1346 copy_tagged (cpanel);
1349 static void copy_other_tagged (void)
1351 if (get_other_type () != view_listing)
1352 return;
1353 copy_tagged (opanel);
1356 static void do_suspend_cmd (void)
1358 pre_exec ();
1360 if (console_flag && !use_subshell)
1361 restore_console ();
1363 #ifdef SIGTSTP
1365 struct sigaction sigtstp_action;
1367 /* Make sure that the SIGTSTP below will suspend us directly,
1368 without calling ncurses' SIGTSTP handler; we *don't* want
1369 ncurses to redraw the screen immediately after the SIGCONT */
1370 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
1372 kill (getpid (), SIGTSTP);
1374 /* Restore previous SIGTSTP action */
1375 sigaction (SIGTSTP, &sigtstp_action, NULL);
1377 #endif /* SIGTSTP */
1379 if (console_flag && !use_subshell)
1380 handle_console (CONSOLE_SAVE);
1382 edition_post_exec ();
1385 void
1386 suspend_cmd (void)
1388 save_cwds_stat ();
1389 do_suspend_cmd ();
1390 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1391 do_refresh ();
1394 static void
1395 init_labels (Widget *paneletc)
1397 define_label (midnight_dlg, paneletc, 1, _("Help"), help_cmd);
1398 define_label (midnight_dlg, paneletc, 2, _("Menu"), user_file_menu_cmd);
1399 define_label (midnight_dlg, paneletc, 9, _("PullDn"), menu_cmd);
1400 define_label (midnight_dlg, paneletc, 10, _("Quit"), (voidfn) quit_cmd);
1403 static const key_map ctl_x_map [] = {
1404 { XCTRL('c'), (callfn) quit_cmd },
1405 { 'd', compare_dirs_cmd },
1406 #ifdef USE_VFS
1407 { 'a', reselect_vfs },
1408 #endif /* USE_VFS */
1409 { 'p', copy_current_pathname },
1410 { XCTRL('p'), copy_other_pathname },
1411 { 't', copy_current_tagged },
1412 { XCTRL('t'), copy_other_tagged },
1413 { 'c', chmod_cmd },
1414 #ifndef NATIVE_WIN32
1415 { 'o', chown_cmd },
1416 { 'r', copy_current_readlink },
1417 { XCTRL('r'), copy_other_readlink },
1418 { 'l', link_cmd },
1419 { 's', symlink_cmd },
1420 { XCTRL('s'), edit_symlink_cmd },
1421 #endif /* !NATIVE_WIN32 */
1422 { 'i', info_cmd_no_menu },
1423 { 'q', quick_cmd_no_menu },
1424 { 'h', add2hotlist_cmd },
1425 { '!', external_panelize },
1426 #ifdef WITH_BACKGROUND
1427 { 'j', jobs_cmd },
1428 #endif /* WITH_BACKGROUND */
1429 #ifdef HAVE_SETSOCKOPT
1430 { '%', source_routing },
1431 #endif /* HAVE_SETSOCKOPT */
1432 { 0, 0 }
1435 static int ctl_x_map_enabled = 0;
1437 static void ctl_x_cmd (int ignore)
1439 ctl_x_map_enabled = 1;
1442 static void nothing (void)
1446 static const key_map default_map [] = {
1447 { KEY_F(19), menu_last_selected_cmd },
1448 { KEY_F(20), (key_callback) quiet_quit_cmd },
1450 /* Copy useful information to the command line */
1451 { ALT('\n'), copy_prog_name },
1452 { ALT('\r'), copy_prog_name },
1453 { ALT('a'), copy_current_pathname },
1454 { ALT('A'), copy_other_pathname },
1456 { ALT('c'), quick_cd_cmd },
1458 /* To access the directory hotlist */
1459 { XCTRL('\\'), quick_chdir_cmd },
1461 /* Suspend */
1462 { XCTRL('z'), suspend_cmd },
1463 /* The filtered view command */
1464 { ALT('!'), filtered_view_cmd_cpanel },
1466 /* Find file */
1467 { ALT('?'), find_cmd },
1469 /* Panel refresh */
1470 { XCTRL('r'), reread_cmd },
1472 /* Toggle listing between long, user defined and full formats */
1473 { ALT('t'), toggle_listing_cmd },
1475 /* Swap panels */
1476 { XCTRL('u'), swap_cmd },
1478 /* View output */
1479 { XCTRL('o'), view_other_cmd },
1481 /* Control-X keybindings */
1482 { XCTRL('x'), ctl_x_cmd },
1484 /* Trap dlg's exit commands */
1485 { ESC_CHAR, nothing },
1486 { XCTRL('c'), nothing },
1487 { XCTRL('g'), nothing },
1488 { 0, 0 },
1491 static void setup_sigwinch (void)
1493 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && \
1494 !defined(NATIVE_WIN32) && defined(SIGWINCH)
1495 struct sigaction act, oact;
1496 act.sa_handler = flag_winch;
1497 sigemptyset (&act.sa_mask);
1498 act.sa_flags = 0;
1499 # ifdef SA_RESTART
1500 act.sa_flags |= SA_RESTART;
1501 # endif
1502 sigaction (SIGWINCH, &act, &oact);
1503 #endif
1506 static void
1507 setup_pre (void)
1509 /* Call all the inits */
1510 #ifdef HAVE_CHARSET
1512 * Don't restrict the output on the screen manager level,
1513 * the translation tables take care of it.
1515 #define full_eight_bits (1)
1516 #define eight_bit_clean (1)
1517 #endif /* !HAVE_CHARSET */
1519 #ifndef HAVE_SLANG
1520 meta (stdscr, eight_bit_clean);
1521 #else
1522 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1523 #endif
1526 static void
1527 init_xterm_support (void)
1529 char *termvalue;
1530 #ifdef HAVE_SLANG
1531 char *term_entry;
1532 #endif
1534 termvalue = getenv ("TERM");
1535 if (!termvalue || !(*termvalue)){
1536 fprintf (stderr, _("The TERM environment variable is unset!\n"));
1537 exit (1);
1540 /* Check mouse capabilities */
1541 #ifdef HAVE_SLANG
1542 term_entry = SLtt_tigetent (termvalue);
1543 xmouse_seq = SLtt_tigetstr ("Km", &term_entry);
1544 #else
1545 xmouse_seq = tigetstr ("kmous");
1546 #endif
1548 /* -1 means invalid capability, shouldn't happen, but let's make it 0 */
1549 if ((long) xmouse_seq == -1) {
1550 xmouse_seq = NULL;
1553 if (strcmp (termvalue, "cygwin") == 0) {
1554 force_xterm = 1;
1555 use_mouse_p = MOUSE_DISABLED;
1558 if (force_xterm
1559 || strncmp (termvalue, "xterm", 5) == 0
1560 || strncmp (termvalue, "rxvt", 4) == 0
1561 || strcmp (termvalue, "dtterm") == 0) {
1562 xterm_flag = 1;
1564 /* Default to the standard xterm sequence */
1565 if (!xmouse_seq) {
1566 xmouse_seq = ESC_STR "[M";
1569 /* Enable mouse unless explicitly disabled by --nomouse */
1570 if (use_mouse_p != MOUSE_DISABLED) {
1571 use_mouse_p = MOUSE_XTERM;
1574 #if 0 /* It works on xterm, but not on rxvt */
1575 printf (ESC_STR "]0;GNU Midnight Commander\7");
1576 #endif
1580 static void setup_mc (void)
1582 setup_pre ();
1583 init_menu ();
1584 create_panels ();
1585 setup_panels ();
1587 #ifdef HAVE_SUBSHELL_SUPPORT
1588 if (use_subshell)
1589 add_select_channel (subshell_pty, load_prompt, 0);
1590 #endif /* !HAVE_SUBSHELL_SUPPORT */
1592 setup_sigwinch ();
1594 if (baudrate () < 9600 || slow_terminal){
1595 verbose = 0;
1597 init_mouse ();
1600 static void setup_dummy_mc (const char *file)
1602 char d[MC_MAXPATHLEN];
1604 mc_get_current_wd (d, MC_MAXPATHLEN);
1605 setup_mc ();
1606 mc_chdir (d);
1608 /* Create a fake current_panel, this is needed because the
1609 * expand_format routine will use current panel.
1611 strcpy (cpanel->cwd, d);
1612 cpanel->selected = 0;
1613 cpanel->count = 1;
1614 cpanel->dir.list[0].fname = (char *) file;
1617 static void done_mc (void)
1619 disable_mouse ();
1621 done_menu ();
1623 /* Setup shutdown
1625 * We sync the profiles since the hotlist may have changed, while
1626 * we only change the setup data if we have the auto save feature set
1628 if (auto_save_setup)
1629 save_setup (); /* does also call save_hotlist */
1630 else
1631 save_hotlist();
1632 done_screen ();
1633 vfs_add_current_stamps ();
1634 if (xterm_flag && xterm_hintbar)
1635 set_hintbar(_("Thank you for using GNU Midnight Commander"));
1638 /* This should be called after destroy_dlg since panel widgets
1639 * save their state on the profiles
1641 static void done_mc_profile (void)
1643 if (!auto_save_setup)
1644 profile_forget_profile (profile_name);
1645 sync_profiles ();
1646 done_setup ();
1647 free_profiles ();
1650 /* This routine only handles cpanel, and opanel, it is easy to
1651 * change to use npanels, just loop over the number of panels
1652 * and use get_panel_widget (i) and make the test done below
1654 static void
1655 make_panels_dirty (void)
1657 if (cpanel->dirty)
1658 panel_update_contents (cpanel);
1660 if ((get_other_type () == view_listing) && opanel->dirty)
1661 panel_update_contents (opanel);
1664 /* In Windows people want to actually type the '\' key frequently */
1665 #ifdef NATIVE_WIN32
1666 # define check_key_backslash(x) 0
1667 #else
1668 # define check_key_backslash(x) ((x) == '\\')
1669 #endif
1672 midnight_callback (struct Dlg_head *h, int id, int msg)
1674 int i;
1675 static int first_pre_event = 1;
1677 switch (msg){
1679 case DLG_PRE_EVENT:
1680 make_panels_dirty ();
1681 if (auto_menu && first_pre_event) {
1682 user_file_menu_cmd ();
1684 first_pre_event = 0;
1685 return MSG_HANDLED;
1687 case DLG_KEY:
1688 if (ctl_x_map_enabled){
1689 ctl_x_map_enabled = 0;
1690 for (i = 0; ctl_x_map [i].key_code; i++)
1691 if (id == ctl_x_map [i].key_code){
1692 (*ctl_x_map [i].fn)(id);
1693 return MSG_HANDLED;
1697 /* FIXME: should handle all menu shortcuts before this point */
1698 if (the_menubar->active)
1699 break;
1701 if (id == KEY_F(10)){
1702 quit_cmd ();
1703 return MSG_HANDLED;
1706 if (id == '\t')
1707 free_completions (input_w (cmdline));
1709 /* On Linux, we can tell the difference */
1710 if (id == '\n' && ctrl_pressed ()){
1711 copy_prog_name ();
1712 return MSG_HANDLED;
1715 if (id == '\n' && input_w (cmdline)->buffer [0]){
1716 send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1717 return MSG_HANDLED;
1720 if ((!alternate_plus_minus || !(console_flag || xterm_flag)) &&
1721 !quote && !cpanel->searching) {
1722 if(!only_leading_plus_minus) {
1723 /* Special treatement, since the input line will eat them */
1724 if (id == '+' ) {
1725 select_cmd ();
1726 return MSG_HANDLED;
1729 if (check_key_backslash (id) || id == '-'){
1730 unselect_cmd ();
1731 return MSG_HANDLED;
1734 if (id == '*') {
1735 reverse_selection_cmd ();
1736 return MSG_HANDLED;
1738 } else if (command_prompt && !strlen (input_w (cmdline)->buffer)) {
1739 /* Special treatement '+', '-', '\', '*' only when this is
1740 * first char on input line
1743 if (id == '+') {
1744 select_cmd ();
1745 return MSG_HANDLED;
1748 if (check_key_backslash (id) || id == '-') {
1749 unselect_cmd ();
1750 return MSG_HANDLED;
1753 if (id == '*') {
1754 reverse_selection_cmd ();
1755 return MSG_HANDLED;
1759 break;
1761 case DLG_HOTKEY_HANDLED:
1762 if (get_current_type () == view_listing)
1763 cpanel->searching = 0;
1764 break;
1766 case DLG_UNHANDLED_KEY:
1767 if (command_prompt){
1768 int v;
1770 v = send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1771 if (v)
1772 return v;
1774 if (ctl_x_map_enabled) {
1775 ctl_x_map_enabled = 0;
1776 for (i = 0; ctl_x_map [i].key_code; i++)
1777 if (id == ctl_x_map [i].key_code){
1778 (*ctl_x_map [i].fn)(id);
1779 return MSG_HANDLED;
1781 } else
1783 for (i = 0; default_map [i].key_code; i++){
1784 if (id == default_map [i].key_code){
1785 (*default_map [i].fn)(id);
1786 return MSG_HANDLED;
1790 return MSG_NOT_HANDLED;
1792 /* We handle the special case of the output lines */
1793 case DLG_DRAW:
1794 attrset (SELECTED_COLOR);
1795 if (console_flag && output_lines)
1796 show_console_contents (output_start_y,
1797 LINES-output_lines-keybar_visible-1,
1798 LINES-keybar_visible-1);
1799 return MSG_HANDLED;
1802 return default_dlg_callback (h, id, msg);
1805 #define xtoolkit_panel_setup()
1807 void
1808 load_hint (void)
1810 char *hint;
1812 if (!the_hint->widget.parent)
1813 return;
1815 if (!message_visible && (!xterm_flag || !xterm_hintbar)) {
1816 label_set_text (the_hint, 0);
1817 return;
1820 if ((hint = get_random_hint ())) {
1821 if (*hint)
1822 set_hintbar (hint);
1823 g_free (hint);
1824 } else {
1825 char text[BUF_SMALL];
1827 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1828 VERSION);
1829 set_hintbar (text);
1833 static void
1834 setup_panels_and_run_mc (void)
1836 int first, second;
1838 xtoolkit_panel_setup ();
1839 add_widget (midnight_dlg, the_hint);
1840 load_hint ();
1841 add_widget (midnight_dlg, cmdline);
1842 add_widget (midnight_dlg, the_prompt);
1843 add_widget (midnight_dlg, the_bar);
1845 if (boot_current_is_left){
1846 first = 1;
1847 second = 0;
1848 } else {
1849 first = 0;
1850 second = 1;
1852 add_widget (midnight_dlg, get_panel_widget (first));
1853 add_widget (midnight_dlg, get_panel_widget (second));
1854 add_widget (midnight_dlg, the_menubar);
1856 init_labels (get_panel_widget (0));
1857 init_labels (get_panel_widget (1));
1859 /* Run the Midnight Commander if no file was specified in the command line */
1860 run_dlg (midnight_dlg);
1863 /* result must be free'd (I think this should go in util.c) */
1864 static char *
1865 prepend_cwd_on_local (char *filename)
1867 char *d;
1868 int l;
1870 if (vfs_file_is_local (filename)){
1871 if (*filename == PATH_SEP) /* an absolute pathname */
1872 return g_strdup (filename);
1873 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1874 mc_get_current_wd (d, MC_MAXPATHLEN);
1875 l = strlen(d);
1876 d[l++] = PATH_SEP;
1877 strcpy (d + l, filename);
1878 return canonicalize_pathname (d);
1879 } else
1880 return g_strdup (filename);
1883 static int
1884 mc_maybe_editor_or_viewer (void)
1886 char *path = NULL;
1888 if (!(view_one_file || edit_one_file))
1889 return 0;
1891 /* Invoke the internal view/edit routine with:
1892 * the default processing and forcing the internal viewer/editor
1894 if (view_one_file) {
1895 path = prepend_cwd_on_local (view_one_file);
1896 setup_dummy_mc (path);
1897 view_file (path, 0, 1);
1899 #ifdef USE_INTERNAL_EDIT
1900 else {
1901 path = prepend_cwd_on_local ("");
1902 setup_dummy_mc (path);
1903 edit (edit_one_file, edit_one_file_start_line);
1905 #endif /* USE_INTERNAL_EDIT */
1906 g_free (path);
1907 midnight_shutdown = 1;
1908 done_mc ();
1909 return 1;
1912 /* Run the main dialog that occupies the whole screen */
1913 static void
1914 do_nc (void)
1916 int midnight_colors [4];
1918 midnight_colors [0] = NORMAL_COLOR; /* NORMALC */
1919 midnight_colors [1] = REVERSE_COLOR; /* FOCUSC */
1920 midnight_colors [2] = INPUT_COLOR; /* HOT_NORMALC */
1921 midnight_colors [3] = NORMAL_COLOR; /* HOT_FOCUSC */
1923 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors,
1924 midnight_callback, "[main]", NULL,
1925 DLG_HAS_MENUBAR);
1927 /* Check if we were invoked as an editor or file viewer */
1928 if (mc_maybe_editor_or_viewer ())
1929 return;
1931 setup_mc ();
1933 setup_panels_and_run_mc ();
1935 /* Program end */
1936 midnight_shutdown = 1;
1938 /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */
1939 if (print_last_wd) {
1940 if (!vfs_current_is_local ())
1941 last_wd_string = g_strdup (".");
1942 else
1943 last_wd_string = g_strdup (cpanel->cwd);
1945 done_mc ();
1947 destroy_dlg (midnight_dlg);
1948 current_panel = 0;
1949 done_mc_profile ();
1952 #if defined (NATIVE_WIN32)
1953 /* Windows NT code */
1955 void
1956 OS_Setup (void)
1958 SetConsoleTitle ("GNU Midnight Commander");
1960 shell = getenv ("COMSPEC");
1961 if (!shell || !*shell)
1962 shell = get_default_shell ();
1964 /* Default opening mode for files is binary, not text (CR/LF translation) */
1965 #ifndef __EMX__
1966 _fmode = O_BINARY;
1967 #endif
1969 mc_home = get_mc_lib_dir ();
1972 /* Nothing to be done on Windows */
1973 static void
1974 sigchld_handler_no_subshell (int sig)
1978 void
1979 init_sigchld (void)
1983 void
1984 init_sigfatals (void)
1989 #else
1991 /* Unix version */
1992 static void
1993 OS_Setup (void)
1995 char *mc_libdir;
1996 shell = getenv ("SHELL");
1997 if (!shell || !*shell)
1998 shell = g_strdup (getpwuid (geteuid ())->pw_shell);
1999 if (!shell || !*shell)
2000 shell = "/bin/sh";
2002 /* This is the directory, where MC was installed, on Unix this is LIBDIR */
2003 /* and can be overriden by the MC_LIBDIR environment variable */
2004 if ((mc_libdir = getenv ("MC_LIBDIR")) != NULL) {
2005 mc_home = g_strdup (mc_libdir);
2006 } else {
2007 mc_home = g_strdup (LIBDIR);
2011 static void
2012 sigchld_handler_no_subshell (int sig)
2014 #if defined(linux) || defined(__linux__)
2015 int pid, status;
2017 if (!console_flag)
2018 return;
2020 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2021 the value of console_flag never changed, we could simply not install
2022 this handler at all if (!console_flag && !use_subshell). */
2024 /* That comment is no longer true. We need to wait() on a sigchld
2025 handler (that's at least what the tarfs code expects currently). */
2027 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2029 if (pid == cons_saver_pid){
2031 if (WIFSTOPPED (status)) {
2032 /* Someone has stopped cons.saver - restart it */
2033 kill (pid, SIGCONT);
2034 } else {
2035 /* cons.saver has died - disable console saving */
2036 handle_console (CONSOLE_DONE);
2037 console_flag = 0;
2040 #endif /* linux || __linux__ */
2042 /* If we get here, some other child exited; ignore it */
2045 void
2046 init_sigchld (void)
2048 struct sigaction sigchld_action;
2050 sigchld_action.sa_handler =
2051 #ifdef HAVE_SUBSHELL_SUPPORT
2052 use_subshell ? sigchld_handler :
2053 #endif /* HAVE_SUBSHELL_SUPPORT */
2054 sigchld_handler_no_subshell;
2056 sigemptyset (&sigchld_action.sa_mask);
2058 #ifdef SA_RESTART
2059 sigchld_action.sa_flags = SA_RESTART;
2060 #else
2061 sigchld_action.sa_flags = 0;
2062 #endif /* !SA_RESTART */
2064 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2065 #ifdef HAVE_SUBSHELL_SUPPORT
2067 * This may happen on QNX Neutrino 6, where SA_RESTART
2068 * is defined but not implemented. Fallback to no subshell.
2070 use_subshell = 0;
2071 #endif /* HAVE_SUBSHELL_SUPPORT */
2075 #endif /* NATIVE_WIN32, UNIX */
2077 static void
2078 print_mc_usage (poptContext ctx, FILE * stream)
2080 fprintf (stream,
2081 _("Usage is:\n\n"
2082 "mc [flags] [this_dir] [other_panel_dir]\n\n"));
2084 /* print help for options */
2085 poptPrintHelp (ctx, stream, 0);
2087 fprintf (stream, " %s %s", _("+number"),
2088 _("number is the start line number of file for `mcedit'.\n"));
2089 fprintf (stream,
2090 _("\n"
2091 "Please send any bug reports (including the output of `mc -V')\n"
2092 "to mc-devel@gnome.org\n"));
2093 version (0);
2096 static void
2097 print_color_usage (void)
2099 fputs (_("--colors KEYWORD={FORE},{BACK}\n\n"
2100 "{FORE} and {BACK} can be omitted, and the default will be used\n"
2101 "\n"
2102 "Keywords:\n"
2103 " Global: errors, reverse, gauge, input\n"
2104 " File display: normal, selected, marked, markselect\n"
2105 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus\n"
2106 " Menus: menu, menuhot, menusel, menuhotsel\n"
2107 " Help: helpnormal, helpitalic, helplink, helpslink\n"
2108 " File types: directory, execute, link, device, special, core\n"
2109 "\n"
2110 "Colors:\n"
2111 " black, gray, red, brightred, green, brightgreen, brown,\n"
2112 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
2113 " brightcyan, lightgray and white\n\n"), stdout);
2116 static void
2117 probably_finish_program (void)
2119 if (finish_program){
2120 if (print_last_wd)
2121 printf (".");
2122 exit (1);
2126 enum {
2127 GEOMETRY_KEY = -1,
2128 NOWIN_KEY = -2
2131 static void
2132 process_args (poptContext ctx, int c, const char *option_arg)
2134 switch (c) {
2135 case 'V':
2136 version (1);
2137 finish_program = 1;
2138 break;
2140 case 'c':
2141 disable_colors = 0;
2142 #ifdef HAVE_SLANG
2143 force_colors = 1;
2144 #endif /* HAVE_SLANG */
2145 break;
2147 case 'f':
2148 printf (_("Library directory for the Midnight Commander: %s\n"), mc_home);
2149 finish_program = 1;
2150 break;
2152 #ifdef USE_NETCODE
2153 case 'l':
2154 ftpfs_set_debug (option_arg);
2155 #ifdef WITH_SMBFS
2156 smbfs_set_debugf (option_arg);
2157 #endif /* WITH_SMBFS */
2158 break;
2160 #ifdef WITH_SMBFS
2161 case 'D':
2162 smbfs_set_debug (atoi (option_arg));
2163 break;
2164 #endif /* WITH_SMBFS */
2165 #endif /* USE_NETCODE */
2167 case 'd':
2168 use_mouse_p = MOUSE_DISABLED;
2169 break;
2171 #ifdef HAVE_SUBSHELL_SUPPORT
2172 case 'u':
2173 use_subshell = 0;
2174 break;
2175 #endif /* HAVE_SUBSHELL_SUPPORT */
2177 case 'H':
2178 print_color_usage ();
2179 finish_program = 1;
2180 break;
2182 case 'h':
2183 print_mc_usage (ctx, stdout);
2184 finish_program = 1;
2188 static const struct poptOption argument_table [] = {
2189 #ifdef WITH_BACKGROUND
2190 { "background", 'B', POPT_ARG_NONE, &background_wait, 0,
2191 N_("Use to debug the background code") },
2192 #endif
2193 { "color", 'c', POPT_ARG_NONE, NULL, 'c',
2194 N_("Request to run in color mode") },
2195 { "colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
2196 N_("Specifies a color configuration") },
2198 #ifdef USE_INTERNAL_EDIT
2199 { "edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2200 N_("Edits one file") },
2201 #endif
2203 { "help", 'h', POPT_ARG_NONE, NULL, 'h',
2204 N_("Displays this help message") },
2205 { "help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
2206 N_("Displays a help screen on how to change the color scheme") },
2207 #ifdef USE_NETCODE
2208 { "ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
2209 N_("Log ftp dialog to specified file") },
2210 #ifdef WITH_SMBFS
2211 { "debuglevel", 'D', POPT_ARG_STRING, NULL, 'D'},
2212 #endif
2213 #endif
2214 { "libdir", 'f', POPT_ARG_NONE, NULL, 'f' },
2215 { "nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
2216 N_("Requests to run in black and white") },
2217 { "nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
2218 N_("Disable mouse support in text version") },
2219 #ifdef HAVE_SUBSHELL_SUPPORT
2220 { "nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2221 N_("Disables subshell support") },
2222 { "forceexec", 'r', POPT_ARG_NONE, &force_subshell_execution,0 },
2223 #endif
2224 { "printwd", 'P', POPT_ARG_NONE, &print_last_wd, 0,
2225 N_("Prints working directory at program exit") },
2226 { "resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
2227 N_("Resets soft keys on HP terminals") },
2228 { "slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
2229 N_("To run on slow terminals") },
2230 #ifndef NATIVE_WIN32
2231 { "stickchars", 'a', 0, &force_ugly_line_drawing, 0,
2232 N_("Use stickchars to draw") },
2233 #endif /* !NATIVE_WIN32 */
2234 #ifdef HAVE_SUBSHELL_SUPPORT
2235 { "subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2236 N_("Enables subshell support (default)")},
2237 #endif
2238 #if defined(HAVE_SLANG) && !defined(NATIVE_WIN32)
2239 { "termcap", 't', 0, &SLtt_Try_Termcap, 0,
2240 N_("Tries to use termcap instead of terminfo") },
2241 #endif
2242 { "version", 'V', POPT_ARG_NONE, NULL, 'V',
2243 N_("Displays the current version") },
2244 { "view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2245 N_("Launches the file viewer on a file") },
2246 { "xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
2247 N_("Forces xterm features") },
2249 { NULL, 0, 0, NULL, 0 }
2252 static void
2253 handle_args (int argc, char *argv [])
2255 char *tmp;
2256 poptContext ctx;
2257 char *base;
2258 int c;
2260 ctx = poptGetContext ("mc", argc, argv, argument_table,
2261 POPT_CONTEXT_NO_EXEC);
2263 #ifdef USE_TERMCAP
2264 SLtt_Try_Termcap = 1;
2265 #endif
2267 while ((c = poptGetNextOpt (ctx)) > 0){
2268 process_args (ctx, c, poptGetOptArg (ctx));
2271 if (c < -1){
2272 print_mc_usage (ctx, stderr);
2273 fprintf(stderr, "%s: %s\n",
2274 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2275 poptStrerror (c));
2276 finish_program = 1;
2278 probably_finish_program ();
2280 tmp = poptGetArg (ctx);
2283 * Check for special invocation names mcedit and mcview,
2284 * if none apply then set the current directory and the other
2285 * directory from the command line arguments
2287 base = x_basename (argv[0]);
2288 if (!STRNCOMP (base, "mce", 3) || !STRCOMP(base, "vi")) {
2289 edit_one_file = "";
2290 edit_one_file_start_line = 1;
2291 if (tmp) {
2292 if (*tmp == '+' && isdigit ((unsigned char) tmp[1])){
2293 int start_line = atoi (tmp);
2294 if (start_line > 0) {
2295 char *file = poptGetArg (ctx);
2296 if (file) {
2297 tmp = file;
2298 edit_one_file_start_line = start_line;
2302 edit_one_file = g_strdup (tmp);
2304 } else
2305 if (!STRNCOMP (base, "mcv", 3) || !STRCOMP(base, "view")) {
2306 if (tmp)
2307 view_one_file = g_strdup (tmp);
2308 else {
2309 fprintf (stderr, "No arguments given to the viewer\n");
2310 finish_program = 1;
2311 probably_finish_program ();
2313 } else {
2314 /* sets the current dir and the other dir */
2315 if (tmp) {
2316 this_dir = g_strdup (tmp);
2317 if ((tmp = poptGetArg (ctx)))
2318 other_dir = g_strdup (tmp);
2322 poptFreeContext (ctx);
2326 * The compatibility_move_mc_files routine is intended to
2327 * move all of the hidden .mc files into a private ~/.mc
2328 * directory in the home directory, to avoid cluttering the users.
2330 * Previous versions of the program had all of their files in
2331 * the $HOME, we are now putting them in $HOME/.mc
2333 #ifdef NATIVE_WIN32
2334 # define compatibility_move_mc_files()
2335 #else
2337 static int
2338 do_mc_filename_rename (char *mc_dir, char *o_name, char *n_name)
2340 char *full_o_name = concat_dir_and_file (home_dir, o_name);
2341 char *full_n_name = g_strconcat (home_dir, MC_BASE, n_name, NULL);
2342 int move;
2344 move = 0 == rename (full_o_name, full_n_name);
2345 g_free (full_o_name);
2346 g_free (full_n_name);
2347 return move;
2350 static void
2351 do_compatibility_move (char *mc_dir)
2353 struct stat s;
2354 int move;
2356 if (stat (mc_dir, &s) == 0)
2357 return;
2358 if (errno != ENOENT)
2359 return;
2361 if (mkdir (mc_dir, 0777) == -1)
2362 return;
2364 move = do_mc_filename_rename (mc_dir, ".mc.ini", "ini");
2365 move += do_mc_filename_rename (mc_dir, ".mc.hot", "hotlist");
2366 move += do_mc_filename_rename (mc_dir, ".mc.ext", "ext");
2367 move += do_mc_filename_rename (mc_dir, ".mc.menu", "menu");
2368 move += do_mc_filename_rename (mc_dir, ".mc.bashrc", "bashrc");
2369 move += do_mc_filename_rename (mc_dir, ".mc.inputrc", "inputrc");
2370 move += do_mc_filename_rename (mc_dir, ".mc.tcshrc", "tcshrc");
2371 move += do_mc_filename_rename (mc_dir, ".mc.tree", "tree");
2373 if (!move)
2374 return;
2376 show_change_notice = 1;
2379 static void
2380 compatibility_move_mc_files (void)
2382 char *mc_dir = concat_dir_and_file (home_dir, ".mc");
2384 do_compatibility_move (mc_dir);
2385 g_free (mc_dir);
2387 #endif /* NATIVE_WIN32 */
2390 main (int argc, char *argv [])
2392 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2393 setlocale (LC_ALL, "");
2394 bindtextdomain ("mc", LOCALEDIR);
2395 textdomain ("mc");
2397 /* Initialize list of all user group for timur_clr_mode */
2398 init_groups ();
2400 /* Set up temporary directory */
2401 mc_tmpdir ();
2403 OS_Setup ();
2405 /* This variable is used by the subshell */
2406 home_dir = getenv ("HOME");
2407 if (!home_dir) {
2408 /* mc_home was computed by OS_Setup */
2409 home_dir = mc_home;
2412 vfs_init ();
2414 #ifdef HAVE_SLANG
2415 SLtt_Ignore_Beep = 1;
2416 #endif
2418 /* NOTE: This has to be called before slang_init or whatever routine
2419 calls any define_sequence */
2420 init_key ();
2422 handle_args (argc, argv);
2424 /* Used to report the last working directory at program end */
2425 if (print_last_wd){
2426 #ifndef NATIVE_WIN32
2427 stdout_fd = dup (1);
2428 close (1);
2429 if (open (ttyname (0), O_RDWR) < 0)
2430 if (open ("/dev/tty", O_RDWR) < 0) {
2431 /* Try if stderr is not redirected as the last chance */
2432 char *p = g_strdup (ttyname (0));
2434 if (!strcmp (p, ttyname (2)))
2435 dup2 (2, 1);
2436 else {
2437 fprintf (stderr,
2438 _("Couldn't open tty line. You have to run mc without the -P flag.\n"
2439 "On some systems you may want to run # `which mc`\n"));
2440 exit (1);
2442 g_free (p);
2444 #endif
2447 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2448 handle_console (CONSOLE_INIT);
2450 # ifdef HAVE_SUBSHELL_SUPPORT
2451 subshell_get_console_attributes ();
2452 # endif
2454 /* Install the SIGCHLD handler; must be done before init_subshell() */
2455 init_sigchld ();
2457 compatibility_move_mc_files ();
2459 /* We need this, since ncurses endwin () doesn't restore the signals */
2460 save_stop_handler ();
2462 /* Must be done before init_subshell, to set up the terminal size: */
2463 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2464 #ifdef HAVE_SLANG
2465 slang_init ();
2466 #endif
2467 /* NOTE: This call has to be after slang_init. It's the small part from
2468 the previous init_key which had to be moved after the call of slang_init */
2469 init_key_input_fd ();
2471 load_setup ();
2473 init_curses ();
2475 init_xterm_support ();
2477 #ifdef HAVE_SUBSHELL_SUPPORT
2479 /* Don't use subshell when invoked as a viewer or editor */
2480 if (edit_one_file || view_one_file)
2481 use_subshell = 0;
2483 /* Done here to ensure that the subshell doesn't */
2484 /* inherit the file descriptors opened below, etc */
2485 if (use_subshell)
2486 init_subshell ();
2488 #endif /* HAVE_SUBSHELL_SUPPORT */
2490 /* Removing this from the X code let's us type C-c */
2491 load_key_defs ();
2493 /* Also done after init_subshell, to save any shell init file messages */
2494 if (console_flag)
2495 handle_console (CONSOLE_SAVE);
2497 if (alternate_plus_minus)
2498 application_keypad_mode ();
2500 if (show_change_notice){
2501 message (1, _(" Notice "),
2502 _(" The Midnight Commander configuration files \n"
2503 " are now stored in the ~/.mc directory, the \n"
2504 " files have been moved now\n"));
2507 #ifdef HAVE_SUBSHELL_SUPPORT
2508 if (use_subshell){
2509 prompt = strip_ctrl_codes (subshell_prompt);
2510 if (!prompt)
2511 prompt = "";
2512 } else
2513 #endif /* HAVE_SUBSHELL_SUPPORT */
2514 prompt = (geteuid () == 0) ? "# " : "$ ";
2516 /* Program main loop */
2517 do_nc ();
2519 /* Save the tree store */
2520 tree_store_save ();
2522 /* Virtual File System shutdown */
2523 vfs_shut ();
2525 /* Delete list of all user groups */
2526 destroy_groups ();
2528 flush_extension_file (); /* does only free memory */
2530 endwin ();
2531 #ifdef HAVE_SLANG
2532 slang_shutdown ();
2533 #endif
2535 if (console_flag && !(quit & SUBSHELL_EXIT))
2536 restore_console ();
2537 if (alternate_plus_minus)
2538 numeric_keypad_mode ();
2540 #ifndef NATIVE_WIN32
2541 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2542 #endif
2544 if (console_flag)
2545 handle_console (CONSOLE_DONE);
2546 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2548 #ifdef NATIVE_WIN32
2549 /* On NT, home_dir is malloced */
2550 g_free (home_dir);
2551 #endif
2552 if (print_last_wd) {
2553 if (print_last_revert || edit_one_file || view_one_file)
2554 write (stdout_fd, ".", 1);
2555 else
2556 write (stdout_fd, last_wd_string, strlen (last_wd_string));
2557 g_free (last_wd_string);
2560 #ifndef NATIVE_WIN32
2561 g_free (mc_home);
2562 #endif /* (NATIVE_WIN32) */
2563 done_key ();
2564 #ifdef HAVE_CHARSET
2565 free_codepages_list ();
2566 #endif
2567 if (this_dir)
2568 g_free (this_dir);
2569 if (other_dir)
2570 g_free (other_dir);
2572 return 0;