*** empty log message ***
[midnight-commander.git] / src / main.c
blob2e976831f4cb243e61cb42921009427dd54dbcef
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 _OS_NT
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 _OS_NT
85 # include "drive.h"
86 #endif
88 #include "../vfs/vfs.h"
90 #ifdef HAVE_CHARSET
91 #include "charsets.h"
92 #endif /* HAVE_CHARSET */
94 #include "popt.h"
96 /* "$Id$" */
98 /* When the modes are active, left_panel, right_panel and tree_panel */
99 /* Point to a proper data structure. You should check with the functions */
100 /* get_current_type and get_other_type the types of the panels before using */
101 /* This pointer variables */
103 /* The structures for the panels */
104 WPanel *left_panel;
105 WPanel *right_panel;
107 /* The pointer to the tree */
108 WTree *the_tree;
110 /* The Menubar */
111 WMenu *the_menubar;
113 /* Pointers to the selected and unselected panel */
114 WPanel *current_panel = NULL;
116 /* Set when we want use advanced chmod command instead of chmod and/or chown */
117 int advanced_chfns = 0;
119 /* Set when main loop should be terminated */
120 volatile int quit = 0;
122 /* Set if you want the possible completions dialog for the first time */
123 int show_all_if_ambiguous = 0;
125 /* Set when cd symlink following is desirable (bash mode) */
126 int cd_symlinks = 1;
128 /* If set then dialogs just clean the screen when refreshing, else */
129 /* they do a complete refresh, refreshing all the parts of the program */
130 int fast_refresh = 0;
132 /* If true, marking a files moves the cursor down */
133 int mark_moves_down = 1;
135 /* If true, at startup the user-menu is invoked */
136 int auto_menu = 0;
138 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\
139 and M-- and keypad + / - */
140 int alternate_plus_minus = 0;
142 /* If true, then the +, - and \ keys have their special meaning only if the
143 * command line is emtpy, otherwise they behave like regular letters
145 int only_leading_plus_minus = 1;
147 /* If true, after executing a command, wait for a keystroke */
148 enum { pause_never, pause_on_dumb_terminals, pause_always };
150 int pause_after_run = pause_on_dumb_terminals;
152 /* It true saves the setup when quitting */
153 int auto_save_setup = 1;
155 #ifndef HAVE_CHARSET
156 /* If true, be eight bit clean */
157 int eight_bit_clean = 1;
159 /* If true, then display chars 0-255, else iso-8859-1,
160 requires eight_bit_clean */
161 int full_eight_bits = 1;
162 #endif /* !HAVE_CHARSET */
164 /* If true use the internal viewer */
165 int use_internal_view = 1;
167 /* Have we shown the fast-reload warning in the past? */
168 int fast_reload_w = 0;
170 /* Move page/item? When clicking on the top or bottom of a panel */
171 int mouse_move_pages = 1;
173 /* If true: l&r arrows are used to chdir if the input line is empty */
174 int navigate_with_arrows = 0;
176 /* If true use +, -, | for line drawing */
177 int force_ugly_line_drawing = 0;
179 /* If true message "The shell is already running a command" never */
180 int force_subshell_execution = 0;
182 /* If true program softkeys (HP terminals only) on startup and after every
183 command ran in the subshell to the description found in the termcap/terminfo
184 database */
185 int reset_hp_softkeys = 0;
187 /* The prompt */
188 char *prompt = 0;
190 /* The widget where we draw the prompt */
191 WLabel *the_prompt;
193 /* The hint bar */
194 WLabel *the_hint;
196 /* The button bar */
197 WButtonBar *the_bar;
199 /* For slow terminals */
200 int slow_terminal = 0;
202 /* Mouse type: GPM, xterm or none */
203 Mouse_Type use_mouse_p = MOUSE_NONE;
205 /* If true, assume we are running on an xterm terminal */
206 static int force_xterm = 0;
208 /* Controls screen clearing before an exec */
209 int clear_before_exec = 1;
211 /* Asks for confirmation before deleting a file */
212 int confirm_delete = 1;
214 /* Asks for confirmation before overwriting a file */
215 int confirm_overwrite = 1;
217 /* Asks for confirmation before executing a program by pressing enter */
218 int confirm_execute = 0;
220 /* Asks for confirmation before leaving the program */
221 int confirm_exit = 1;
223 /* Asks for confirmation when using F3 to view a directory and there
224 are tagged files */
225 int confirm_view_dir = 0;
227 /* This flag indicates if the pull down menus by default drop down */
228 int drop_menus = 0;
230 /* The dialog handle for the main program */
231 Dlg_head *midnight_dlg;
233 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
234 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
235 int update_prompt = 0;
237 #if 0
238 /* The name which was used to invoke mc */
239 char *program_name;
240 #endif
242 /* The home directory */
243 char *home_dir;
245 /* The value of the other directory, only used when loading the setup */
246 char *other_dir = 0;
248 /* Only used at program boot */
249 int boot_current_is_left = 1;
251 char *this_dir = 0;
253 /* If this is true, then when browsing the tree the other window will
254 * automatically reload it's directory with the contents of the currently
255 * selected directory.
257 int xtree_mode = 0;
259 /* If true, then print on stdout the last directory we were at */
260 static int print_last_wd = 0;
261 static char *last_wd_string;
262 static int print_last_revert = 0;
264 /* On OS/2 and on Windows NT, we need a batch file to do the -P magic */
265 #ifdef OS2_NT
266 static char *batch_file_name = 0;
267 #endif
269 /* Force colors, only used by Slang */
270 int force_colors = 0;
272 /* colors specified on the command line: they override any other setting */
273 char *command_line_colors;
275 /* File name to view if argument was supplied */
276 char *view_one_file = 0;
278 /* File name to edit if argument was supplied */
279 char *edit_one_file = 0;
280 static int edit_one_file_start_line = 1;
282 /* Used so that widgets know if they are being destroyed or
283 shut down */
284 int midnight_shutdown = 0;
286 /* to show nice prompts */
287 static int last_paused = 0;
289 /* Used for keeping track of the original stdout */
290 int stdout_fd = 0;
292 /* The user's shell */
293 char *shell;
295 /* mc_home: The home of MC */
296 char *mc_home;
298 /* if on, it displays the information that files have been moved to ~/.mc */
299 int show_change_notice = 0;
301 char cmd_buf [512];
303 /* Used during argument processing */
304 int finish_program = 0;
306 WPanel *
307 get_current_panel (void)
309 return current_panel;
312 WPanel *
313 get_other_panel (void)
315 return (WPanel *) get_panel_widget (get_other_index ());
318 static void
319 reload_panelized (WPanel *panel)
321 int i, j;
322 dir_list *list = &panel->dir;
324 if (panel != cpanel)
325 mc_chdir (panel->cwd);
327 for (i = 0, j = 0; i < panel->count; i++){
328 if (list->list [i].f.marked) {
329 /* Unmark the file in advance. In case the following mc_lstat
330 * fails we are done, else we have to mark the file again
331 * (Note: do_file_mark depends on a valid "list->list [i].buf").
332 * IMO that's the best way to update the panel's summary status
333 * -- Norbert
335 do_file_mark (panel, i, 0);
337 if (mc_lstat (list->list [i].fname, &list->list [i].buf)){
338 g_free (list->list [i].fname);
339 continue;
341 if (list->list [i].f.marked)
342 do_file_mark (panel, i, 1);
343 if (j != i)
344 list->list [j] = list->list [i];
345 j++;
347 if (j == 0)
348 panel->count = set_zero_dir (list);
349 else
350 panel->count = j;
352 if (panel != cpanel)
353 mc_chdir (cpanel->cwd);
356 static void
357 update_one_panel_widget (WPanel *panel, int force_update, char *current_file)
359 int free_pointer;
361 if (force_update & UP_RELOAD){
362 panel->is_panelized = 0;
364 #if 1
365 ftpfs_flushdir ();
366 #endif
367 /* FIXME: Should supply flushdir method */
368 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
371 /* If current_file == -1 (an invalid pointer) then preserve selection */
372 if (current_file == UP_KEEPSEL){
373 free_pointer = 1;
374 current_file = g_strdup (panel->dir.list [panel->selected].fname);
375 } else
376 free_pointer = 0;
378 if (panel->is_panelized)
379 reload_panelized (panel);
380 else
381 panel_reload (panel);
383 try_to_select (panel, current_file);
384 panel->dirty = 1;
386 if (free_pointer)
387 g_free (current_file);
390 void
391 panel_clean_dir (WPanel *panel)
393 int count = panel->count;
395 panel->count = 0;
396 panel->top_file = 0;
397 panel->selected = 0;
398 panel->marked = 0;
399 panel->dirs_marked = 0;
400 panel->total = 0;
401 panel->searching = 0;
402 panel->is_panelized = 0;
404 clean_dir (&panel->dir, count);
407 static void
408 update_one_panel (int which, int force_update, char *current_file)
410 WPanel *panel;
412 if (get_display_type (which) != view_listing)
413 return;
415 panel = (WPanel *) get_panel_widget (which);
416 update_one_panel_widget (panel, force_update, current_file);
419 /* This routine reloads the directory in both panels. It tries to
420 * select current_file in current_panel and other_file in other_panel.
421 * If current_file == -1 then it automatically sets current_file and
422 * other_file to the currently selected files in the panels.
424 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
425 * will not reload the other panel.
427 void
428 update_panels (int force_update, char *current_file)
430 int reload_other = !(force_update & UP_ONLY_CURRENT);
431 WPanel *panel;
433 update_one_panel (get_current_index (), force_update, current_file);
434 if (reload_other)
435 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
437 if (get_current_type () == view_listing)
438 panel = (WPanel *) get_panel_widget (get_current_index ());
439 else
440 panel = (WPanel *) get_panel_widget (get_other_index ());
442 mc_chdir (panel->cwd);
445 /* Sets up the terminal before executing a program */
446 static void
447 pre_exec (void)
449 use_dash (0);
450 edition_pre_exec ();
453 /* Save current stat of directories to avoid reloading the panels */
454 /* when no modifications have taken place */
455 void
456 save_cwds_stat (void)
458 if (fast_reload){
459 mc_stat (cpanel->cwd, &(cpanel->dir_stat));
460 if (get_other_type () == view_listing)
461 mc_stat (opanel->cwd, &(opanel->dir_stat));
465 #ifdef HAVE_SUBSHELL_SUPPORT
466 void
467 do_possible_cd (char *new_dir)
469 if (!do_cd (new_dir, cd_exact))
470 message (1, _(" Warning "),
471 _(" The Commander can't change to the directory that \n"
472 " the subshell claims you are in. Perhaps you have \n"
473 " deleted your working directory, or given yourself \n"
474 " extra access permissions with the \"su\" command? "));
477 void
478 do_update_prompt (void)
480 if (update_prompt){
481 printf ("%s", subshell_prompt);
482 fflush (stdout);
483 update_prompt = 0;
486 #endif /* HAVE_SUBSHELL_SUPPORT */
488 void
489 restore_console (void)
491 handle_console (CONSOLE_RESTORE);
494 void
495 exec_shell (void)
497 do_execute (shell, 0, 0);
500 void
501 do_execute (const char *shell, const char *command, int flags)
503 #ifdef HAVE_SUBSHELL_SUPPORT
504 char *new_dir = NULL;
505 #endif /* HAVE_SUBSHELL_SUPPORT */
507 #ifdef USE_VFS
508 char *old_vfs_dir = 0;
510 if (!vfs_current_is_local ())
511 old_vfs_dir = g_strdup (vfs_get_current_dir ());
512 #endif /* USE_VFS */
514 save_cwds_stat ();
515 pre_exec ();
516 if (console_flag)
517 restore_console ();
519 if (!use_subshell && !(flags & EXECUTE_INTERNAL && command)) {
520 printf ("%s%s%s\n", last_paused ? "\r\n":"", prompt, command);
521 last_paused = 0;
524 #ifdef HAVE_SUBSHELL_SUPPORT
525 if (use_subshell && !(flags & EXECUTE_INTERNAL)){
526 do_update_prompt ();
528 /* We don't care if it died, higher level takes care of this */
529 #ifdef USE_VFS
530 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
531 #else
532 invoke_subshell (command, VISIBLY, &new_dir);
533 #endif /* !USE_VFS */
534 } else
535 #endif /* HAVE_SUBSHELL_SUPPORT */
536 my_system (flags, shell, command);
538 if (!(flags & EXECUTE_INTERNAL)) {
539 if ((pause_after_run == pause_always ||
540 (pause_after_run == pause_on_dumb_terminals &&
541 !xterm_flag && !console_flag)) && !quit
542 #ifdef HAVE_SUBSHELL_SUPPORT
543 && subshell_state != RUNNING_COMMAND
544 #endif /* HAVE_SUBSHELL_SUPPORT */
546 printf ("%s\r\n", _("Press any key to continue..."));
547 last_paused = 1;
548 fflush (stdout);
549 mc_raw_mode ();
550 getch ();
552 if (console_flag) {
553 if (output_lines && keybar_visible) {
554 putchar('\n');
555 fflush(stdout);
560 if (console_flag)
561 handle_console (CONSOLE_SAVE);
562 edition_post_exec ();
564 #ifdef HAVE_SUBSHELL_SUPPORT
565 if (new_dir)
566 do_possible_cd (new_dir);
568 #endif /* HAVE_SUBSHELL_SUPPORT */
570 #ifdef USE_VFS
571 if (old_vfs_dir){
572 mc_chdir (old_vfs_dir);
573 g_free (old_vfs_dir);
575 #endif /* USE_VFS */
577 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
579 do_refresh ();
580 use_dash (TRUE);
583 /* Executes a command */
584 void
585 shell_execute (char *command, int flags)
587 #ifdef HAVE_SUBSHELL_SUPPORT
588 if (use_subshell)
589 if (subshell_state == INACTIVE || force_subshell_execution)
590 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
591 else
592 message (1, MSG_ERROR, _(" The shell is already running a command "));
593 else
594 #endif /* HAVE_SUBSHELL_SUPPORT */
595 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
598 void
599 execute (char *command)
601 shell_execute (command, 0);
604 void
605 change_panel (void)
607 free_completions (input_w (cmdline));
608 dlg_one_down (midnight_dlg);
611 /* Stop MC main dialog and the current dialog if it exists.
612 * Needed to provide fast exit from MC viewer or editor on shell exit */
613 static void stop_dialogs (void)
615 midnight_dlg->running = 0;
616 if (current_dlg) {
617 current_dlg->running = 0;
621 static int
622 quit_cmd_internal (int quiet)
624 int q = quit;
626 if (quiet || !confirm_exit){
627 q = 1;
628 } else {
629 if (query_dialog (_(" The Midnight Commander "),
630 _(" Do you really want to quit the Midnight Commander? "),
631 0, 2, _("&Yes"), _("&No")) == 0)
632 q = 1;
634 if (q){
635 #ifdef HAVE_SUBSHELL_SUPPORT
636 if (!use_subshell)
637 stop_dialogs ();
638 else
639 if ((q = exit_subshell ()))
640 #endif
641 stop_dialogs ();
643 if (q)
644 quit |= 1;
645 return quit;
648 int quit_cmd (void)
650 quit_cmd_internal (0);
651 return quit;
654 int quiet_quit_cmd (void)
656 print_last_revert = 1;
657 quit_cmd_internal (1);
658 return quit;
662 * Touch window and refresh window functions
665 /* This routine untouches the first line on both panels in order */
666 /* to avoid the refreshing the menu bar */
668 void
669 untouch_bar (void)
671 do_refresh ();
674 void
675 repaint_screen (void)
677 do_refresh ();
678 mc_refresh ();
681 /* Wrapper for do_subshell_chdir, check for availability of subshell */
682 void
683 subshell_chdir (char *directory)
685 #ifdef HAVE_SUBSHELL_SUPPORT
686 if (use_subshell){
687 if (vfs_current_is_local ())
688 do_subshell_chdir (directory, 0, 1);
690 #endif /* HAVE_SUBSHELL_SUPPORT */
693 void
694 directory_history_add (WPanel * panel, char *s)
696 if (!panel->dir_history) {
697 panel->dir_history = g_new0 (Hist, 1);
698 panel->dir_history->text = g_strdup (s);
699 strip_password (panel->dir_history->text, 1);
700 return;
702 if (!strcmp (panel->dir_history->text, s))
703 return;
704 if (panel->dir_history->next) {
705 if (panel->dir_history->next->text) {
706 g_free (panel->dir_history->next->text);
707 panel->dir_history->next->text = 0;
709 } else {
710 panel->dir_history->next = g_new0 (Hist, 1);
711 panel->dir_history->next->prev = panel->dir_history;
713 panel->dir_history = panel->dir_history->next;
714 panel->dir_history->text = g_strdup (s);
715 strip_password (panel->dir_history->text, 1);
719 * If we moved to the parent directory move the selection pointer to
720 * the old directory name; If we leave VFS dir, remove FS specificator.
721 * Warn: This code spoils lwd string.
723 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
725 static char*
726 get_parent_dir_name (char *cwd, char *lwd)
728 char *p;
729 if (strlen (lwd) > strlen (cwd))
730 if((p=strrchr(lwd, PATH_SEP)) && !strncmp(cwd, lwd, p-lwd)){
731 return (p+1);
733 return NULL;
736 /* Changes the current panel directory */
737 static int
738 _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
740 char *directory, *olddir;
741 char temp [MC_MAXPATHLEN];
742 char *translated_url;
743 #ifdef USE_VFS
744 vfs *oldvfs;
745 vfsid oldvfsid;
746 struct vfs_stamping *parent;
747 #endif
748 olddir = g_strdup (panel->cwd);
749 translated_url = new_dir = vfs_translate_url (new_dir);
751 /* Convert *new_path to a suitable pathname, handle ~user */
753 if (cd_type == cd_parse_command){
754 while (*new_dir == ' ')
755 new_dir++;
757 if (!strcmp (new_dir, "-")){
758 strcpy (temp, panel->lwd);
759 new_dir = temp;
762 directory = *new_dir ? new_dir : home_dir;
764 if (mc_chdir (directory) == -1){
765 strcpy (panel->cwd, olddir);
766 g_free (olddir);
767 g_free (translated_url);
768 return 0;
770 g_free (translated_url);
772 /* Success: save previous directory, shutdown status of previous dir */
773 strcpy (panel->lwd, olddir);
774 free_completions (input_w (cmdline));
776 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
778 #ifdef USE_VFS
779 oldvfs = vfs_type (olddir);
780 oldvfsid = vfs_ncs_getid (oldvfs, olddir, &parent);
781 vfs_add_noncurrent_stamps (oldvfs, oldvfsid, parent);
782 vfs_rm_parents (parent);
783 #endif
785 subshell_chdir (panel->cwd);
787 /* Reload current panel */
788 panel_clean_dir (panel);
789 panel->count = do_load_dir (&panel->dir, panel->sort_type,
790 panel->reverse, panel->case_sensitive, panel->filter);
791 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
792 load_hint ();
793 panel_update_contents (panel);
795 g_free (olddir);
797 return 1;
801 do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
803 int r;
805 r = _do_panel_cd (panel, new_dir, cd_type);
806 if (r)
807 directory_history_add (cpanel, cpanel->cwd);
808 return r;
812 do_cd (char *new_dir, enum cd_enum exact)
814 return (do_panel_cd (cpanel, new_dir, exact));
817 void
818 directory_history_next (WPanel * panel)
820 if (!panel->dir_history->next)
821 return;
822 if (_do_panel_cd (panel, panel->dir_history->next->text, cd_exact))
823 panel->dir_history = panel->dir_history->next;
826 void
827 directory_history_prev (WPanel * panel)
829 if (!panel->dir_history->prev)
830 return;
831 if (_do_panel_cd (panel, panel->dir_history->prev->text, cd_exact))
832 panel->dir_history = panel->dir_history->prev;
835 void
836 directory_history_list (WPanel * panel)
838 char *s;
839 /* must be at least two to show a history */
840 if (panel->dir_history) {
841 if (panel->dir_history->prev || panel->dir_history->next) {
842 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
843 if (s) {
844 int r;
845 r = _do_panel_cd (panel, s, cd_exact);
846 if (r)
847 directory_history_add (panel, panel->cwd);
848 else
849 message (1, MSG_ERROR, _("Could not change directory") );
850 g_free (s);
856 #ifdef HAVE_SUBSHELL_SUPPORT
858 load_prompt (int fd, void *unused)
860 if (!read_subshell_prompt ())
861 return 0;
863 /* Don't actually change the prompt if it's invisible */
864 if (current_dlg == midnight_dlg && command_prompt){
865 int prompt_len;
867 prompt = strip_ctrl_codes (subshell_prompt);
868 prompt_len = strlen (prompt);
870 /* Check for prompts too big */
871 if (prompt_len > COLS - 8) {
872 prompt [COLS - 8 ] = 0;
873 prompt_len = COLS - 8;
875 label_set_text (the_prompt, prompt);
876 winput_set_origin ((WInput *)cmdline, prompt_len, COLS-prompt_len);
878 /* since the prompt has changed, and we are called from one of the
879 * get_event channels, the prompt updating does not take place
880 * automatically: force a cursor update and a screen refresh
882 update_cursor (midnight_dlg);
883 mc_refresh ();
885 update_prompt = 1;
886 return 0;
888 #endif /* HAVE_SUBSHELL_SUPPORT */
890 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
892 maybe_cd (int char_code, int move_up_dir)
894 if (navigate_with_arrows){
895 if (!input_w (cmdline)->buffer [0]){
896 if (!move_up_dir){
897 do_cd ("..", cd_exact);
898 return 1;
900 if (S_ISDIR (selection (cpanel)->buf.st_mode)
901 || link_isdir (selection (cpanel))){
902 do_cd (selection (cpanel)->fname, cd_exact);
903 return 1;
907 return 0;
910 void
911 sort_cmd (void)
913 WPanel *p;
914 sortfn *sort_order;
916 if (!SELECTED_IS_PANEL)
917 return;
919 p = MENU_PANEL;
920 sort_order = sort_box (p->sort_type, &p->reverse, &p->case_sensitive);
922 panel_set_sort_order (p, sort_order);
926 static void
927 treebox_cmd (void)
929 char *sel_dir;
931 sel_dir = tree_box (selection (cpanel)->fname);
932 if (sel_dir){
933 do_cd(sel_dir, cd_exact);
934 g_free (sel_dir);
938 #ifdef LISTMODE_EDITOR
939 static void
940 listmode_cmd (void)
942 char *newmode;
943 newmode = listmode_edit ("half <type,>name,|,size:8,|,perm:4+");
944 message (0, _(" Listing format edit "), _(" New mode is \"%s\" "), newmode);
945 g_free (newmode);
947 #endif /* LISTMODE_EDITOR */
949 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
950 static menu_entry PanelMenu [] = {
951 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
952 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
953 { ' ', N_("&Info C-x i"), 'I', info_cmd },
954 { ' ', N_("&Tree"), 'T', tree_cmd },
955 { ' ', "", ' ', 0 },
956 { ' ', N_("&Sort order..."), 'S', sort_cmd },
957 { ' ', "", ' ', 0 },
958 { ' ', N_("&Filter..."), 'F', filter_cmd },
959 #ifdef USE_NETCODE
960 { ' ', "", ' ', 0 },
961 #ifdef WITH_MCFS
962 { ' ', N_("&Network link..."), 'N', netlink_cmd },
963 #endif
964 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
965 { ' ', N_("S&hell link..."), 'H', fishlink_cmd },
966 #ifdef WITH_SMBFS
967 { ' ', N_("SM&B link..."), 'B', smblink_cmd },
968 #endif
969 #endif
970 { ' ', "", ' ', 0 },
971 #ifdef OS2_NT
972 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_a },
973 #endif
974 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
977 static menu_entry RightMenu [] = {
978 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
979 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
980 { ' ', N_("&Info C-x i"), 'I', info_cmd },
981 { ' ', N_("&Tree"), 'T', tree_cmd },
982 { ' ', "", ' ', 0 },
983 { ' ', N_("&Sort order..."), 'S', sort_cmd },
984 { ' ', "", ' ', 0 },
985 { ' ', N_("&Filter..."), 'F', filter_cmd },
986 #ifdef USE_NETCODE
987 { ' ', "", ' ', 0 },
988 #ifdef WITH_MCFS
989 { ' ', N_("&Network link..."), 'N', netlink_cmd },
990 #endif
991 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
992 { ' ', N_("S&hell link..."), 'H', fishlink_cmd },
993 #ifdef WITH_SMBFS
994 { ' ', N_("SM&B link..."), 'B', smblink_cmd },
995 #endif
996 #endif
997 { ' ', "", ' ', 0 },
998 #ifdef OS2_NT
999 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_b },
1000 #endif
1001 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
1004 static menu_entry FileMenu [] = {
1005 { ' ', N_("&User menu F2"), 'U', user_file_menu_cmd },
1006 { ' ', N_("&View F3"), 'V', view_cmd },
1007 { ' ', N_("Vie&w file... "), 'W', view_file_cmd },
1008 { ' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd },
1009 { ' ', N_("&Edit F4"), 'E', edit_cmd },
1010 { ' ', N_("&Copy F5"), 'C', copy_cmd },
1011 { ' ', N_("c&Hmod C-x c"), 'H', chmod_cmd },
1012 #ifndef OS2_NT
1013 { ' ', N_("&Link C-x l"), 'L', link_cmd },
1014 { ' ', N_("&SymLink C-x s"), 'S', symlink_cmd },
1015 { ' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd },
1016 { ' ', N_("ch&Own C-x o"), 'O', chown_cmd },
1017 { ' ', N_("&Advanced chown "), 'A', chown_advanced_cmd },
1018 #endif
1019 { ' ', N_("&Rename/Move F6"), 'R', ren_cmd },
1020 { ' ', N_("&Mkdir F7"), 'M', mkdir_cmd },
1021 { ' ', N_("&Delete F8"), 'D', delete_cmd },
1022 { ' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd },
1023 { ' ', "", ' ', 0 },
1024 { ' ', N_("select &Group M-+"), 'G', select_cmd },
1025 { ' ', N_("u&Nselect group M-\\"),'N', unselect_cmd },
1026 { ' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd },
1027 { ' ', "", ' ', 0 },
1028 { ' ', N_("e&Xit F10"), 'X', (callfn) quit_cmd }
1031 static menu_entry CmdMenu [] = {
1032 /* I know, I'm lazy, but the tree widget when it's not running
1033 * as a panel still has some problems, I have not yet finished
1034 * the WTree widget port, sorry.
1036 { ' ', N_("&Directory tree"), 'D', treebox_cmd },
1037 { ' ', N_("&Find file M-?"), 'F', find_cmd },
1038 { ' ', N_("s&Wap panels C-u"), 'W', swap_cmd },
1039 { ' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd },
1040 { ' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd },
1041 { ' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize },
1042 { ' ', N_("show directory s&Izes"), 'I', dirsizes_cmd },
1043 { ' ', "", ' ', 0 },
1044 { ' ', N_("command &History"), 'H', history_cmd },
1045 { ' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd },
1046 #ifdef USE_VFS
1047 { ' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs },
1048 { ' ', N_("Fr&ee VFSs now"), 'E', free_vfs_now },
1049 #endif
1050 #ifdef WITH_BACKGROUND
1051 { ' ', N_("&Background jobs C-x j"), 'B', jobs_cmd },
1052 #endif
1053 { ' ', "", ' ', 0 },
1054 #ifdef USE_EXT2FSLIB
1055 { ' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd },
1056 #endif
1057 #ifdef LISTMODE_EDITOR
1058 { ' ', N_("&Listing format edit"), 'L', listmode_cmd},
1059 #endif
1060 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
1061 { ' ', "", ' ', 0 },
1062 #endif
1063 { ' ', N_("&Extension file edit"), 'E', ext_cmd },
1064 { ' ', N_("&Menu file edit"), 'M', menu_edit_cmd },
1065 #ifdef USE_INTERNAL_EDIT
1066 {' ', N_("Menu edi&Tor edit"), 'T', edit_user_menu_cmd},
1067 {' ', N_("&Syntax file edit"), 'S', edit_syntax_cmd}
1068 #endif /* USE_INTERNAL_EDIT */
1071 /* Must keep in sync with the constants in menu_cmd */
1072 static menu_entry OptMenu [] = {
1073 { ' ', N_("&Configuration..."), 'C', configure_box },
1074 { ' ', N_("&Layout..."), 'L', layout_cmd },
1075 { ' ', N_("c&Onfirmation..."), 'O', confirm_box },
1076 { ' ', N_("&Display bits..."), 'D', display_bits_box },
1077 #ifndef OS2_NT
1078 { ' ', N_("learn &Keys..."), 'K', learn_keys },
1079 #endif /* !OS2_NT */
1080 #ifdef USE_VFS
1081 { ' ', N_("&Virtual FS..."), 'V', configure_vfs },
1082 #endif /* !USE_VFS */
1083 { ' ', "", ' ', 0 },
1084 { ' ', N_("&Save setup"), 'S', save_setup_cmd }
1087 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
1089 static Menu MenuBar [5];
1091 void
1092 init_menu (void)
1094 MenuBar [0] = create_menu ( horizontal_split ? _(" &Above ") : _(" &Left "),
1095 PanelMenu, menu_entries (PanelMenu),
1096 "[Left and Right Menus]");
1097 MenuBar [1] = create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
1098 "[File Menu]");
1099 MenuBar [2] = create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
1100 "[Command Menu]");
1101 MenuBar [3] = create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
1102 "[Options Menu]");
1103 MenuBar [4] = create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
1104 RightMenu, menu_entries (PanelMenu),
1105 "[Left and Right Menus]");
1108 void
1109 done_menu (void)
1111 int i;
1113 for (i = 0; i < 5; i++){
1114 destroy_menu (MenuBar [i]);
1118 static void
1119 menu_last_selected_cmd (void)
1121 the_menubar->active = 1;
1122 the_menubar->dropped = drop_menus;
1123 the_menubar->previous_selection = dlg_item_number (midnight_dlg);
1124 dlg_select_widget (midnight_dlg, the_menubar);
1127 static void
1128 menu_cmd (void)
1130 if (the_menubar->active)
1131 return;
1133 if (get_current_index () == 0)
1134 the_menubar->selected = 0;
1135 else
1136 the_menubar->selected = 4;
1137 menu_last_selected_cmd ();
1140 /* Flag toggling functions */
1141 void
1142 toggle_fast_reload (void)
1144 fast_reload = !fast_reload;
1145 if (fast_reload_w == 0 && fast_reload){
1146 message (0, _(" Information "),
1147 _(" Using the fast reload option may not reflect the exact \n"
1148 " directory contents. In this cases you'll need to do a \n"
1149 " manual reload of the directory. See the man page for \n"
1150 " the details. "));
1151 fast_reload_w = 1;
1155 void
1156 toggle_mix_all_files (void)
1158 mix_all_files = !mix_all_files;
1159 update_panels (UP_RELOAD, UP_KEEPSEL);
1162 void
1163 toggle_show_backup (void)
1165 show_backups = !show_backups;
1166 update_panels (UP_RELOAD, UP_KEEPSEL);
1169 void
1170 toggle_show_hidden (void)
1172 show_dot_files = !show_dot_files;
1173 update_panels (UP_RELOAD, UP_KEEPSEL);
1177 * Just a hack for allowing url-like pathnames to be accepted from the
1178 * command line.
1180 static void
1181 translated_mc_chdir (char *dir)
1183 char *newdir;
1185 newdir = vfs_translate_url (dir);
1186 mc_chdir (newdir);
1187 g_free (newdir);
1190 void
1191 create_panels (void)
1193 int current_index;
1194 int other_index;
1195 int current_mode;
1196 int other_mode;
1197 char original_dir [1024];
1199 original_dir [0] = 0;
1201 if (boot_current_is_left){
1202 current_index = 0;
1203 other_index = 1;
1204 current_mode = startup_left_mode;
1205 other_mode = startup_right_mode;
1206 } else {
1207 current_index = 1;
1208 other_index = 0;
1209 current_mode = startup_right_mode;
1210 other_mode = startup_left_mode;
1212 /* Creates the left panel */
1213 if (this_dir){
1214 if (other_dir){
1215 /* Ok, user has specified two dirs, save the original one,
1216 * since we may not be able to chdir to the proper
1217 * second directory later
1219 mc_get_current_wd (original_dir, sizeof (original_dir)-2);
1221 translated_mc_chdir (this_dir);
1223 set_display_type (current_index, current_mode);
1225 /* The other panel */
1226 if (other_dir){
1227 if (original_dir [0])
1228 translated_mc_chdir (original_dir);
1229 translated_mc_chdir (other_dir);
1231 set_display_type (other_index, other_mode);
1233 if (startup_left_mode == view_listing){
1234 current_panel = left_panel;
1235 } else {
1236 if (right_panel)
1237 current_panel = right_panel;
1238 else
1239 current_panel = left_panel;
1242 /* Create the nice widgets */
1243 cmdline = command_new (0, 0, 0);
1244 the_prompt = label_new (0, 0, prompt, NULL);
1245 the_prompt->transparent = 1;
1246 the_bar = buttonbar_new (keybar_visible);
1248 the_hint = label_new (0, 0, 0, NULL);
1249 the_hint->transparent = 1;
1250 the_hint->auto_adjust_cols = 0;
1251 the_hint->widget.cols = COLS;
1253 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1256 static void copy_current_pathname (void)
1258 if (!command_prompt)
1259 return;
1261 stuff (input_w (cmdline), cpanel->cwd, 0);
1262 if (cpanel->cwd [strlen (cpanel->cwd) - 1] != PATH_SEP)
1263 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1266 static void copy_other_pathname (void)
1268 if (get_other_type () != view_listing)
1269 return;
1271 if (!command_prompt)
1272 return;
1274 stuff (input_w (cmdline), opanel->cwd, 0);
1275 if (cpanel->cwd [strlen (opanel->cwd) - 1] != PATH_SEP)
1276 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1279 static void copy_readlink (WPanel *panel)
1281 if (!command_prompt)
1282 return;
1283 if (S_ISLNK (selection (panel)->buf.st_mode)) {
1284 char buffer [MC_MAXPATHLEN];
1285 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1286 int i;
1288 i = mc_readlink (p, buffer, MC_MAXPATHLEN);
1289 g_free (p);
1290 if (i > 0) {
1291 buffer [i] = 0;
1292 stuff (input_w (cmdline), buffer, 0);
1297 static void copy_current_readlink (void)
1299 copy_readlink (cpanel);
1302 static void copy_other_readlink (void)
1304 if (get_other_type () != view_listing)
1305 return;
1306 copy_readlink (opanel);
1309 /* Inserts the selected file name into the input line */
1310 /* Exported so that the command modules uses it */
1311 void copy_prog_name (void)
1313 char *tmp;
1314 if (!command_prompt)
1315 return;
1317 if (get_current_type () == view_tree){
1318 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1319 tmp = name_quote (tree->selected_ptr->name, 1);
1320 } else
1321 tmp = name_quote (selection (cpanel)->fname, 1);
1322 stuff (input_w (cmdline), tmp, 1);
1323 g_free (tmp);
1326 static void copy_tagged (WPanel *panel)
1328 int i;
1330 if (!command_prompt)
1331 return;
1332 input_disable_update (input_w (cmdline));
1333 if (panel->marked){
1334 for (i = 0; i < panel->count; i++)
1335 if (panel->dir.list [i].f.marked) {
1336 char *tmp = name_quote (panel->dir.list [i].fname, 1);
1337 stuff (input_w (cmdline), tmp, 1);
1338 g_free (tmp);
1340 } else {
1341 char *tmp = name_quote (panel->dir.list [panel->selected].fname, 1);
1342 stuff (input_w (cmdline), tmp, 1);
1343 g_free (tmp);
1345 input_enable_update (input_w (cmdline));
1348 static void copy_current_tagged (void)
1350 copy_tagged (cpanel);
1353 static void copy_other_tagged (void)
1355 if (get_other_type () != view_listing)
1356 return;
1357 copy_tagged (opanel);
1360 static void do_suspend_cmd (void)
1362 pre_exec ();
1364 if (console_flag && !use_subshell)
1365 restore_console ();
1367 #ifdef SIGTSTP
1369 struct sigaction sigtstp_action;
1371 /* Make sure that the SIGTSTP below will suspend us directly,
1372 without calling ncurses' SIGTSTP handler; we *don't* want
1373 ncurses to redraw the screen immediately after the SIGCONT */
1374 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
1376 kill (getpid (), SIGTSTP);
1378 /* Restore previous SIGTSTP action */
1379 sigaction (SIGTSTP, &sigtstp_action, NULL);
1381 #endif /* SIGTSTP */
1383 if (console_flag && !use_subshell)
1384 handle_console (CONSOLE_SAVE);
1386 edition_post_exec ();
1389 void
1390 suspend_cmd (void)
1392 save_cwds_stat ();
1393 do_suspend_cmd ();
1394 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1395 do_refresh ();
1398 static void
1399 init_labels (Widget *paneletc)
1401 define_label (midnight_dlg, paneletc, 1, _("Help"), help_cmd);
1402 define_label (midnight_dlg, paneletc, 2, _("Menu"), user_file_menu_cmd);
1403 define_label (midnight_dlg, paneletc, 9, _("PullDn"), menu_cmd);
1404 define_label (midnight_dlg, paneletc, 10, _("Quit"), (voidfn) quit_cmd);
1407 static const key_map ctl_x_map [] = {
1408 { XCTRL('c'), (callfn) quit_cmd },
1409 { 'd', compare_dirs_cmd },
1410 #ifdef USE_VFS
1411 { 'a', reselect_vfs },
1412 #endif /* USE_VFS */
1413 { 'p', copy_current_pathname },
1414 { XCTRL('p'), copy_other_pathname },
1415 { 't', copy_current_tagged },
1416 { XCTRL('t'), copy_other_tagged },
1417 { 'c', chmod_cmd },
1418 #ifndef OS2_NT
1419 { 'o', chown_cmd },
1420 { 'r', copy_current_readlink },
1421 { XCTRL('r'), copy_other_readlink },
1422 { 'l', link_cmd },
1423 { XCTRL('l'), other_symlink_cmd },
1424 { 's', symlink_cmd },
1425 { XCTRL('s'), edit_symlink_cmd },
1426 #endif /* !OS2_NT */
1427 { 'i', info_cmd_no_menu },
1428 { 'q', quick_cmd_no_menu },
1429 { 'h', add2hotlist_cmd },
1430 { '!', external_panelize },
1431 #ifdef WITH_BACKGROUND
1432 { 'j', jobs_cmd },
1433 #endif /* WITH_BACKGROUND */
1434 #ifdef HAVE_SETSOCKOPT
1435 { '%', source_routing },
1436 #endif /* HAVE_SETSOCKOPT */
1437 { 0, 0 }
1440 static int ctl_x_map_enabled = 0;
1442 static void ctl_x_cmd (int ignore)
1444 ctl_x_map_enabled = 1;
1447 static void nothing (void)
1451 static const key_map default_map [] = {
1452 { KEY_F(19), menu_last_selected_cmd },
1453 { KEY_F(20), (key_callback) quiet_quit_cmd },
1455 /* Copy useful information to the command line */
1456 { ALT('\n'), copy_prog_name },
1457 { ALT('\r'), copy_prog_name },
1458 { ALT('a'), copy_current_pathname },
1459 { ALT('A'), copy_other_pathname },
1461 { ALT('c'), quick_cd_cmd },
1463 /* To access the directory hotlist */
1464 { XCTRL('\\'), quick_chdir_cmd },
1466 /* Suspend */
1467 { XCTRL('z'), suspend_cmd },
1468 /* The filtered view command */
1469 { ALT('!'), filtered_view_cmd_cpanel },
1471 /* Find file */
1472 { ALT('?'), find_cmd },
1474 /* Panel refresh */
1475 { XCTRL('r'), reread_cmd },
1477 /* Toggle listing between long, user defined and full formats */
1478 { ALT('t'), toggle_listing_cmd },
1480 /* Swap panels */
1481 { XCTRL('u'), swap_cmd },
1483 /* View output */
1484 { XCTRL('o'), view_other_cmd },
1486 /* Control-X keybindings */
1487 { XCTRL('x'), ctl_x_cmd },
1489 /* Trap dlg's exit commands */
1490 { ESC_CHAR, nothing },
1491 { XCTRL('c'), nothing },
1492 { XCTRL('g'), nothing },
1493 { 0, 0 },
1496 static void setup_sigwinch (void)
1498 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && \
1499 !defined(OS2_NT) && defined(SIGWINCH)
1500 struct sigaction act, oact;
1501 act.sa_handler = flag_winch;
1502 sigemptyset (&act.sa_mask);
1503 act.sa_flags = 0;
1504 # ifdef SA_RESTART
1505 act.sa_flags |= SA_RESTART;
1506 # endif
1507 sigaction (SIGWINCH, &act, &oact);
1508 #endif
1511 static void
1512 setup_pre (void)
1514 /* Call all the inits */
1515 #ifdef HAVE_CHARSET
1517 * Don't restrict the output on the screen manager level,
1518 * the translation tables take care of it.
1520 #define full_eight_bits (1)
1521 #define eight_bit_clean (1)
1522 #endif /* !HAVE_CHARSET */
1524 #ifndef HAVE_SLANG
1525 meta (stdscr, eight_bit_clean);
1526 #else
1527 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1528 #endif
1531 static void
1532 init_xterm_support (void)
1534 char *termvalue;
1535 #ifdef HAVE_SLANG
1536 char *term_entry;
1537 #endif
1539 termvalue = getenv ("TERM");
1540 if (!termvalue || !(*termvalue)){
1541 fprintf (stderr, _("The TERM environment variable is unset!\n"));
1542 exit (1);
1545 /* Check mouse capabilities */
1546 #ifdef HAVE_SLANG
1547 term_entry = SLtt_tigetent (termvalue);
1548 xmouse_seq = SLtt_tigetstr ("Km", &term_entry);
1549 #else
1550 xmouse_seq = tigetstr ("kmous");
1551 #endif
1553 /* -1 means invalid capability, shouldn't happen, but let's make it 0 */
1554 if ((long) xmouse_seq == -1) {
1555 xmouse_seq = NULL;
1558 if (strcmp (termvalue, "cygwin") == 0) {
1559 force_xterm = 1;
1560 use_mouse_p = MOUSE_DISABLED;
1563 if (force_xterm
1564 || strncmp (termvalue, "xterm", 5) == 0
1565 || strncmp (termvalue, "rxvt", 4) == 0
1566 || strcmp (termvalue, "dtterm") == 0) {
1567 xterm_flag = 1;
1569 /* Default to the standard xterm sequence */
1570 if (!xmouse_seq) {
1571 xmouse_seq = ESC_STR "[M";
1574 /* Enable mouse unless explicitly disabled by --nomouse */
1575 if (use_mouse_p != MOUSE_DISABLED) {
1576 use_mouse_p = MOUSE_XTERM;
1579 #if 0 /* It works on xterm, but not on rxvt */
1580 printf (ESC_STR "]0;GNU Midnight Commander\7");
1581 #endif
1585 static void setup_mc (void)
1587 setup_pre ();
1588 init_menu ();
1589 create_panels ();
1590 setup_panels ();
1592 #ifdef HAVE_SUBSHELL_SUPPORT
1593 if (use_subshell)
1594 add_select_channel (subshell_pty, load_prompt, 0);
1595 #endif /* !HAVE_SUBSHELL_SUPPORT */
1597 setup_sigwinch ();
1599 if (baudrate () < 9600 || slow_terminal){
1600 verbose = 0;
1602 init_mouse ();
1605 static void setup_dummy_mc (const char *file)
1607 char d[MC_MAXPATHLEN];
1609 mc_get_current_wd (d, MC_MAXPATHLEN);
1610 setup_mc ();
1611 mc_chdir (d);
1613 /* Create a fake current_panel, this is needed because the
1614 * expand_format routine will use current panel.
1616 strcpy (cpanel->cwd, d);
1617 cpanel->selected = 0;
1618 cpanel->count = 1;
1619 cpanel->dir.list[0].fname = (char *) file;
1622 static void done_mc (void)
1624 disable_mouse ();
1626 done_menu ();
1628 /* Setup shutdown
1630 * We sync the profiles since the hotlist may have changed, while
1631 * we only change the setup data if we have the auto save feature set
1633 if (auto_save_setup)
1634 save_setup (); /* does also call save_hotlist */
1635 else
1636 save_hotlist();
1637 done_screen ();
1638 vfs_add_current_stamps ();
1639 if (xterm_flag && xterm_hintbar)
1640 set_hintbar(_("Thank you for using GNU Midnight Commander"));
1643 /* This should be called after destroy_dlg since panel widgets
1644 * save their state on the profiles
1646 static void done_mc_profile (void)
1648 if (!auto_save_setup)
1649 profile_forget_profile (profile_name);
1650 sync_profiles ();
1651 done_setup ();
1652 free_profiles ();
1655 /* This routine only handles cpanel, and opanel, it is easy to
1656 * change to use npanels, just loop over the number of panels
1657 * and use get_panel_widget (i) and make the test done below
1659 static void
1660 make_panels_dirty (void)
1662 if (cpanel->dirty)
1663 panel_update_contents (cpanel);
1665 if ((get_other_type () == view_listing) && opanel->dirty)
1666 panel_update_contents (opanel);
1669 /* In OS/2 and Windows NT people want to actually type the '\' key frequently */
1670 #ifdef OS2_NT
1671 # define check_key_backslash(x) 0
1672 #else
1673 # define check_key_backslash(x) ((x) == '\\')
1674 #endif
1677 midnight_callback (struct Dlg_head *h, int id, int msg)
1679 int i;
1680 static int first_pre_event = 1;
1682 switch (msg){
1684 case DLG_PRE_EVENT:
1685 make_panels_dirty ();
1686 if (auto_menu && first_pre_event) {
1687 user_file_menu_cmd ();
1689 first_pre_event = 0;
1690 return MSG_HANDLED;
1692 case DLG_KEY:
1693 if (ctl_x_map_enabled){
1694 ctl_x_map_enabled = 0;
1695 for (i = 0; ctl_x_map [i].key_code; i++)
1696 if (id == ctl_x_map [i].key_code){
1697 (*ctl_x_map [i].fn)(id);
1698 return MSG_HANDLED;
1702 /* FIXME: should handle all menu shortcuts before this point */
1703 if (the_menubar->active)
1704 break;
1706 if (id == KEY_F(10)){
1707 quit_cmd ();
1708 return MSG_HANDLED;
1711 if (id == '\t')
1712 free_completions (input_w (cmdline));
1714 /* On Linux, we can tell the difference */
1715 if (id == '\n' && ctrl_pressed ()){
1716 copy_prog_name ();
1717 return MSG_HANDLED;
1720 if (id == '\n' && input_w (cmdline)->buffer [0]){
1721 send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1722 return MSG_HANDLED;
1725 if ((!alternate_plus_minus || !(console_flag || xterm_flag)) &&
1726 !quote && !cpanel->searching) {
1727 if(!only_leading_plus_minus) {
1728 /* Special treatement, since the input line will eat them */
1729 if (id == '+' ) {
1730 select_cmd ();
1731 return MSG_HANDLED;
1734 if (check_key_backslash (id) || id == '-'){
1735 unselect_cmd ();
1736 return MSG_HANDLED;
1739 if (id == '*') {
1740 reverse_selection_cmd ();
1741 return MSG_HANDLED;
1743 } else if (command_prompt && !strlen (input_w (cmdline)->buffer)) {
1744 /* Special treatement '+', '-', '\', '*' only when this is
1745 * first char on input line
1748 if (id == '+') {
1749 select_cmd ();
1750 return MSG_HANDLED;
1753 if (check_key_backslash (id) || id == '-') {
1754 unselect_cmd ();
1755 return MSG_HANDLED;
1758 if (id == '*') {
1759 reverse_selection_cmd ();
1760 return MSG_HANDLED;
1764 break;
1766 case DLG_HOTKEY_HANDLED:
1767 if (get_current_type () == view_listing)
1768 cpanel->searching = 0;
1769 break;
1771 case DLG_UNHANDLED_KEY:
1772 if (command_prompt){
1773 int v;
1775 v = send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1776 if (v)
1777 return v;
1779 if (ctl_x_map_enabled) {
1780 ctl_x_map_enabled = 0;
1781 for (i = 0; ctl_x_map [i].key_code; i++)
1782 if (id == ctl_x_map [i].key_code){
1783 (*ctl_x_map [i].fn)(id);
1784 return MSG_HANDLED;
1786 } else
1788 for (i = 0; default_map [i].key_code; i++){
1789 if (id == default_map [i].key_code){
1790 (*default_map [i].fn)(id);
1791 return MSG_HANDLED;
1795 return MSG_NOT_HANDLED;
1797 /* We handle the special case of the output lines */
1798 case DLG_DRAW:
1799 attrset (SELECTED_COLOR);
1800 if (console_flag && output_lines)
1801 show_console_contents (output_start_y,
1802 LINES-output_lines-keybar_visible-1,
1803 LINES-keybar_visible-1);
1804 return MSG_HANDLED;
1807 return default_dlg_callback (h, id, msg);
1810 #define xtoolkit_panel_setup()
1812 void
1813 load_hint (void)
1815 char *hint;
1817 if (!the_hint->widget.parent)
1818 return;
1820 if (!message_visible && (!xterm_flag || !xterm_hintbar)) {
1821 label_set_text (the_hint, 0);
1822 return;
1825 if ((hint = get_random_hint ())) {
1826 if (*hint)
1827 set_hintbar (hint);
1828 g_free (hint);
1829 } else {
1830 char text[BUF_SMALL];
1832 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"),
1833 VERSION);
1834 set_hintbar (text);
1838 static void
1839 setup_panels_and_run_mc (void)
1841 int first, second;
1843 xtoolkit_panel_setup ();
1844 add_widget (midnight_dlg, the_hint);
1845 load_hint ();
1846 add_widget (midnight_dlg, cmdline);
1847 add_widget (midnight_dlg, the_prompt);
1848 add_widget (midnight_dlg, the_bar);
1850 if (boot_current_is_left){
1851 first = 1;
1852 second = 0;
1853 } else {
1854 first = 0;
1855 second = 1;
1857 add_widget (midnight_dlg, get_panel_widget (first));
1858 add_widget (midnight_dlg, get_panel_widget (second));
1859 add_widget (midnight_dlg, the_menubar);
1861 init_labels (get_panel_widget (0));
1862 init_labels (get_panel_widget (1));
1864 /* Run the Midnight Commander if no file was specified in the command line */
1865 run_dlg (midnight_dlg);
1868 /* result must be free'd (I think this should go in util.c) */
1869 static char *
1870 prepend_cwd_on_local (char *filename)
1872 char *d;
1873 int l;
1875 if (vfs_file_is_local (filename)){
1876 if (*filename == PATH_SEP) /* an absolute pathname */
1877 return g_strdup (filename);
1878 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1879 mc_get_current_wd (d, MC_MAXPATHLEN);
1880 l = strlen(d);
1881 d[l++] = PATH_SEP;
1882 strcpy (d + l, filename);
1883 return canonicalize_pathname (d);
1884 } else
1885 return g_strdup (filename);
1888 static int
1889 mc_maybe_editor_or_viewer (void)
1891 char *path = NULL;
1893 if (!(view_one_file || edit_one_file))
1894 return 0;
1896 /* Invoke the internal view/edit routine with:
1897 * the default processing and forcing the internal viewer/editor
1899 if (view_one_file) {
1900 path = prepend_cwd_on_local (view_one_file);
1901 setup_dummy_mc (path);
1902 view_file (path, 0, 1);
1904 #ifdef USE_INTERNAL_EDIT
1905 else {
1906 path = prepend_cwd_on_local ("");
1907 setup_dummy_mc (path);
1908 edit (edit_one_file, edit_one_file_start_line);
1910 #endif /* USE_INTERNAL_EDIT */
1911 g_free (path);
1912 midnight_shutdown = 1;
1913 done_mc ();
1914 return 1;
1917 /* Run the main dialog that occupies the whole screen */
1918 static void
1919 do_nc (void)
1921 int midnight_colors [4];
1923 midnight_colors [0] = NORMAL_COLOR; /* NORMALC */
1924 midnight_colors [1] = REVERSE_COLOR; /* FOCUSC */
1925 midnight_colors [2] = INPUT_COLOR; /* HOT_NORMALC */
1926 midnight_colors [3] = NORMAL_COLOR; /* HOT_FOCUSC */
1928 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors,
1929 midnight_callback, "[main]", NULL,
1930 DLG_HAS_MENUBAR);
1932 /* Check if we were invoked as an editor or file viewer */
1933 if (mc_maybe_editor_or_viewer ())
1934 return;
1936 setup_mc ();
1938 setup_panels_and_run_mc ();
1940 /* Program end */
1941 midnight_shutdown = 1;
1943 /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */
1944 if (print_last_wd) {
1945 if (!vfs_current_is_local ())
1946 last_wd_string = g_strdup (".");
1947 else
1948 last_wd_string = g_strdup (cpanel->cwd);
1950 done_mc ();
1952 destroy_dlg (midnight_dlg);
1953 current_panel = 0;
1954 done_mc_profile ();
1957 #if defined (_OS_NT)
1958 /* Windows NT code */
1960 void
1961 OS_Setup (void)
1963 SetConsoleTitle ("GNU Midnight Commander");
1965 shell = getenv ("COMSPEC");
1966 if (!shell || !*shell)
1967 shell = get_default_shell ();
1969 /* Default opening mode for files is binary, not text (CR/LF translation) */
1970 #ifndef __EMX__
1971 _fmode = O_BINARY;
1972 #endif
1974 mc_home = get_mc_lib_dir ();
1977 static void
1978 sigchld_handler_no_subshell (int sig)
1982 void
1983 init_sigchld (void)
1987 void
1988 init_sigfatals (void)
1990 /* Nothing to be done on the OS/2, Windows/NT */
1994 #else
1996 /* Unix version */
1997 static void
1998 OS_Setup (void)
2000 char *mc_libdir;
2001 shell = getenv ("SHELL");
2002 if (!shell || !*shell)
2003 shell = g_strdup (getpwuid (geteuid ())->pw_shell);
2004 if (!shell || !*shell)
2005 shell = "/bin/sh";
2007 /* This is the directory, where MC was installed, on Unix this is LIBDIR */
2008 /* and can be overriden by the MC_LIBDIR environment variable */
2009 if ((mc_libdir = getenv ("MC_LIBDIR")) != NULL) {
2010 mc_home = g_strdup (mc_libdir);
2011 } else {
2012 mc_home = g_strdup (LIBDIR);
2016 static void
2017 sigchld_handler_no_subshell (int sig)
2019 #if defined(linux) || defined(__linux__)
2020 int pid, status;
2022 if (!console_flag)
2023 return;
2025 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2026 the value of console_flag never changed, we could simply not install
2027 this handler at all if (!console_flag && !use_subshell). */
2029 /* That comment is no longer true. We need to wait() on a sigchld
2030 handler (that's at least what the tarfs code expects currently). */
2032 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2034 if (pid == cons_saver_pid){
2036 if (WIFSTOPPED (status)) {
2037 /* Someone has stopped cons.saver - restart it */
2038 kill (pid, SIGCONT);
2039 } else {
2040 /* cons.saver has died - disable console saving */
2041 handle_console (CONSOLE_DONE);
2042 console_flag = 0;
2045 #endif /* linux || __linux__ */
2047 /* If we get here, some other child exited; ignore it */
2050 void
2051 init_sigchld (void)
2053 struct sigaction sigchld_action;
2055 sigchld_action.sa_handler =
2056 #ifdef HAVE_SUBSHELL_SUPPORT
2057 use_subshell ? sigchld_handler :
2058 #endif /* HAVE_SUBSHELL_SUPPORT */
2059 sigchld_handler_no_subshell;
2061 sigemptyset (&sigchld_action.sa_mask);
2063 #ifdef SA_RESTART
2064 sigchld_action.sa_flags = SA_RESTART;
2065 #else
2066 sigchld_action.sa_flags = 0;
2067 #endif /* !SA_RESTART */
2069 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2070 #ifdef HAVE_SUBSHELL_SUPPORT
2072 * This may happen on QNX Neutrino 6, where SA_RESTART
2073 * is defined but not implemented. Fallback to no subshell.
2075 use_subshell = 0;
2076 #endif /* HAVE_SUBSHELL_SUPPORT */
2080 #endif /* _OS_NT, UNIX */
2082 static void
2083 print_mc_usage (FILE *stream)
2085 const char * const * ptr;
2086 const char * const usage [] = {
2088 N_("Usage is:\n\n"
2089 "mc [flags] [this_dir] [other_panel_dir]\n\n"),
2090 #ifndef OS2_NT
2091 N_("-a, --stickchars Force use of +, -, | for line drawing.\n"),
2092 #endif /* !OS2_NT */
2093 N_("-b, --nocolor Force black and white display.\n"),
2094 #ifdef WITH_BACKGROUND
2095 N_("-B, --background [DEVEL-ONLY: Debug the background code]\n"),
2096 #endif
2097 N_("-c, --color Force color mode.\n"
2098 "-C, --colors Specify colors (use --help-colors to get a list).\n"
2099 "-d, --nomouse Disable mouse support.\n"),
2100 #ifdef USE_INTERNAL_EDIT
2101 N_("-e, --edit Startup the internal editor.\n"),
2102 #endif
2103 N_("-f, --libdir Print configured paths.\n"
2104 "-h, --help Shows this help message.\n"
2105 "-k, --resetsoft Reset softkeys (HP terminals only) to their terminfo/termcap\n"
2106 " default.\n"),
2107 #ifdef USE_NETCODE
2108 N_("-l, --ftplog file Log ftpfs commands to the file.\n"),
2109 #endif
2110 N_("-P, --printwd At exit, print the last working directory.\n"
2111 "-s, --slow Disables verbose operation (for slow terminals).\n"),
2112 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2113 N_("-t, --termcap Activate support for the TERMCAP variable.\n"),
2114 #endif
2115 #if defined(HAVE_SLANG) && defined(OS2_NT)
2116 N_("-S, --createcmdile Create command file to set default directory upon exit.\n"),
2117 #endif
2118 #ifdef HAVE_SUBSHELL_SUPPORT
2119 N_("-u, --nosubshell Disable the concurrent subshell mode.\n"
2120 "-U, --subshell Force the concurrent subshell mode.\n"
2121 "-r, --forceexec Force subshell execution.\n"),
2122 #endif
2123 N_("-v, --view fname Start up into the viewer mode.\n"
2124 "-V, --version Report version and configuration options.\n"
2125 "-x, --xterm Force xterm mouse support and screen save/restore.\n"
2126 "+number number it is the start line number of file for `mcedit'.\n"),
2127 N_("\n"
2128 "Please send any bug reports (including the output of `mc -V')\n"
2129 "to mc-devel@gnome.org\n"),
2130 NULL
2133 version (0);
2135 for (ptr = usage; *ptr; ptr++)
2136 fputs (_(*ptr), stream);
2139 static void
2140 print_color_usage (void)
2142 fputs (_("--colors KEYWORD={FORE},{BACK}\n\n"
2143 "{FORE} and {BACK} can be omitted, and the default will be used\n"
2144 "\n"
2145 "Keywords:\n"
2146 " Global: errors, reverse, gauge, input\n"
2147 " File display: normal, selected, marked, markselect\n"
2148 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus\n"
2149 " Menus: menu, menuhot, menusel, menuhotsel\n"
2150 " Help: helpnormal, helpitalic, helplink, helpslink\n"
2151 " File types: directory, execute, link, device, special, core\n"
2152 "\n"
2153 "Colors:\n"
2154 " black, gray, red, brightred, green, brightgreen, brown,\n"
2155 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
2156 " brightcyan, lightgray and white\n\n"), stdout);
2159 static void
2160 probably_finish_program (void)
2162 if (finish_program){
2163 if (print_last_wd)
2164 printf (".");
2165 exit (1);
2169 enum {
2170 GEOMETRY_KEY = -1,
2171 NOWIN_KEY = -2
2174 static void
2175 process_args (int c, const char *option_arg)
2177 switch (c) {
2178 case 'V':
2179 version (1);
2180 finish_program = 1;
2181 break;
2183 case 'c':
2184 disable_colors = 0;
2185 #ifdef HAVE_SLANG
2186 force_colors = 1;
2187 #endif
2188 break;
2190 case 'f':
2191 printf (_("Library directory for the Midnight Commander: %s\n"), mc_home);
2192 finish_program = 1;
2193 break;
2195 #ifdef USE_NETCODE
2196 case 'l':
2197 ftpfs_set_debug (option_arg);
2198 #ifdef WITH_SMBFS
2199 /* smbfs_set_debug (option_arg); */
2200 #endif
2201 break;
2202 #endif
2204 case 'm':
2205 fputs (_("Option -m is obsolete. Please look at Display Bits... in the Option's menu\n"), stderr);
2206 finish_program = 1;
2207 break;
2209 #ifdef OS2_NT
2210 case 'S':
2211 print_last_wd = 2;
2212 batch_file_name = option_arg;
2213 break;
2214 #endif
2216 case 'd':
2217 use_mouse_p = MOUSE_DISABLED;
2218 break;
2220 #ifdef HAVE_SUBSHELL_SUPPORT
2221 case 'u':
2222 use_subshell = 0;
2223 break;
2224 #endif /* HAVE_SUBSHELL_SUPPORT */
2226 case 'H':
2227 print_color_usage ();
2228 finish_program = 1;
2229 break;
2231 case 'h':
2232 print_mc_usage (stdout);
2233 finish_program = 1;
2237 static const struct poptOption argument_table [] = {
2238 #ifdef WITH_BACKGROUND
2239 { "background", 'B', POPT_ARG_NONE, &background_wait, 0,
2240 N_("Use to debug the background code") },
2241 #endif
2242 #if defined(HAVE_SLANG) && defined(OS2_NT)
2243 { "createcmdfile", 'S', POPT_ARG_STRING, NULL, 'S' },
2244 #endif
2245 { "color", 'c', POPT_ARG_NONE, NULL, 'c',
2246 N_("Request to run in color mode") },
2247 { "colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
2248 N_("Specifies a color configuration") },
2250 #ifdef USE_INTERNAL_EDIT
2251 { "edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2252 N_("Edits one file") },
2253 #endif
2255 { "help", 'h', POPT_ARG_NONE, NULL, 'h',
2256 N_("Displays this help message") },
2257 { "help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
2258 N_("Displays a help screen on how to change the color scheme") },
2259 #ifdef USE_NETCODE
2260 { "ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
2261 N_("Log ftp dialog to specified file") },
2262 #endif
2263 { "libdir", 'f', POPT_ARG_NONE, NULL, 'f' },
2264 { NULL, 'm', POPT_ARG_NONE, NULL, 'm',
2265 N_("Obsolete") },
2266 { "nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
2267 N_("Requests to run in black and white") },
2268 { "nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
2269 N_("Disable mouse support in text version") },
2270 #ifdef HAVE_SUBSHELL_SUPPORT
2271 { "nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2272 N_("Disables subshell support") },
2273 { "forceexec", 'r', POPT_ARG_NONE, &force_subshell_execution,0 },
2274 #endif
2275 { "printwd", 'P', POPT_ARG_NONE, &print_last_wd, 0,
2276 N_("Prints working directory at program exit") },
2277 { "resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
2278 N_("Resets soft keys on HP terminals") },
2279 { "slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
2280 N_("To run on slow terminals") },
2281 #ifndef OS2_NT
2282 { "stickchars", 'a', 0, &force_ugly_line_drawing, 0,
2283 N_("Use stickchars to draw") },
2284 #endif /* !OS2_NT */
2285 #ifdef HAVE_SUBSHELL_SUPPORT
2286 { "subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2287 N_("Enables subshell support (default)")},
2288 #endif
2289 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2290 { "termcap", 't', 0, &SLtt_Try_Termcap, 0,
2291 N_("Tries to use termcap instead of terminfo") },
2292 #endif
2293 { "version", 'V', POPT_ARG_NONE, NULL, 'V',
2294 N_("Displays the current version") },
2295 { "view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2296 N_("Launches the file viewer on a file") },
2297 { "xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
2298 N_("Forces xterm features") },
2300 { NULL, 0, 0, NULL, 0 }
2303 static void
2304 handle_args (int argc, char *argv [])
2306 char *tmp;
2307 poptContext ctx;
2308 char *option_arg, *base;
2309 int c;
2311 ctx = poptGetContext ("mc", argc, argv, argument_table,
2312 POPT_CONTEXT_NO_EXEC);
2314 #ifdef USE_TERMCAP
2315 SLtt_Try_Termcap = 1;
2316 #endif
2318 while ((c = poptGetNextOpt (ctx)) > 0){
2319 option_arg = poptGetOptArg (ctx);
2321 process_args (c, option_arg);
2324 if (c < -1){
2325 print_mc_usage (stderr);
2326 fprintf(stderr, "%s: %s\n",
2327 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2328 poptStrerror (c));
2329 finish_program = 1;
2331 probably_finish_program ();
2333 tmp = poptGetArg (ctx);
2336 * Check for special invocation names mcedit and mcview,
2337 * if none apply then set the current directory and the other
2338 * directory from the command line arguments
2340 base = x_basename (argv[0]);
2341 if (!STRNCOMP (base, "mce", 3) || !STRCOMP(base, "vi")) {
2342 edit_one_file = "";
2343 edit_one_file_start_line = 1;
2344 if (tmp) {
2345 if (*tmp == '+' && isdigit ((unsigned char) tmp[1])){
2346 int start_line = atoi (tmp);
2347 if (start_line > 0) {
2348 char *file = poptGetArg (ctx);
2349 if (file) {
2350 tmp = file;
2351 edit_one_file_start_line = start_line;
2355 edit_one_file = g_strdup (tmp);
2357 } else
2358 if (!STRNCOMP (base, "mcv", 3) || !STRCOMP(base, "view")) {
2359 if (tmp)
2360 view_one_file = g_strdup (tmp);
2361 else {
2362 fprintf (stderr, "No arguments given to the viewer\n");
2363 finish_program = 1;
2364 probably_finish_program ();
2366 } else {
2367 /* sets the current dir and the other dir */
2368 if (tmp) {
2369 this_dir = g_strdup (tmp);
2370 if ((tmp = poptGetArg (ctx)))
2371 other_dir = g_strdup (tmp);
2375 poptFreeContext (ctx);
2379 * The compatibility_move_mc_files routine is intended to
2380 * move all of the hidden .mc files into a private ~/.mc
2381 * directory in the home directory, to avoid cluttering the users.
2383 * Previous versions of the program had all of their files in
2384 * the $HOME, we are now putting them in $HOME/.mc
2386 #ifdef OS2_NT
2387 # define compatibility_move_mc_files()
2388 #else
2390 static int
2391 do_mc_filename_rename (char *mc_dir, char *o_name, char *n_name)
2393 char *full_o_name = concat_dir_and_file (home_dir, o_name);
2394 char *full_n_name = g_strconcat (home_dir, MC_BASE, n_name, NULL);
2395 int move;
2397 move = 0 == rename (full_o_name, full_n_name);
2398 g_free (full_o_name);
2399 g_free (full_n_name);
2400 return move;
2403 static void
2404 do_compatibility_move (char *mc_dir)
2406 struct stat s;
2407 int move;
2409 if (stat (mc_dir, &s) == 0)
2410 return;
2411 if (errno != ENOENT)
2412 return;
2414 if (mkdir (mc_dir, 0777) == -1)
2415 return;
2417 move = do_mc_filename_rename (mc_dir, ".mc.ini", "ini");
2418 move += do_mc_filename_rename (mc_dir, ".mc.hot", "hotlist");
2419 move += do_mc_filename_rename (mc_dir, ".mc.ext", "ext");
2420 move += do_mc_filename_rename (mc_dir, ".mc.menu", "menu");
2421 move += do_mc_filename_rename (mc_dir, ".mc.bashrc", "bashrc");
2422 move += do_mc_filename_rename (mc_dir, ".mc.inputrc", "inputrc");
2423 move += do_mc_filename_rename (mc_dir, ".mc.tcshrc", "tcshrc");
2424 move += do_mc_filename_rename (mc_dir, ".mc.tree", "tree");
2426 if (!move)
2427 return;
2429 show_change_notice = 1;
2432 static void
2433 compatibility_move_mc_files (void)
2435 char *mc_dir = concat_dir_and_file (home_dir, ".mc");
2437 do_compatibility_move (mc_dir);
2438 g_free (mc_dir);
2440 #endif /* OS2_NT */
2443 main (int argc, char *argv [])
2445 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2446 setlocale (LC_ALL, "");
2447 bindtextdomain ("mc", LOCALEDIR);
2448 textdomain ("mc");
2450 /* Initialize list of all user group for timur_clr_mode */
2451 init_groups ();
2453 OS_Setup ();
2455 /* This variable is used by the subshell */
2456 home_dir = getenv ("HOME");
2457 if (!home_dir) {
2458 /* mc_home was computed by OS_Setup */
2459 home_dir = mc_home;
2462 vfs_init ();
2464 #ifdef HAVE_SLANG
2465 SLtt_Ignore_Beep = 1;
2466 #endif
2468 /* NOTE: This has to be called before slang_init or whatever routine
2469 calls any define_sequence */
2470 init_key ();
2472 handle_args (argc, argv);
2474 /* Used to report the last working directory at program end */
2475 if (print_last_wd){
2476 #ifndef OS2_NT
2477 stdout_fd = dup (1);
2478 close (1);
2479 if (open (ttyname (0), O_RDWR) < 0)
2480 if (open ("/dev/tty", O_RDWR) < 0) {
2481 /* Try if stderr is not redirected as the last chance */
2482 char *p = g_strdup (ttyname (0));
2484 if (!strcmp (p, ttyname (2)))
2485 dup2 (2, 1);
2486 else {
2487 fprintf (stderr,
2488 _("Couldn't open tty line. You have to run mc without the -P flag.\n"
2489 "On some systems you may want to run # `which mc`\n"));
2490 exit (1);
2492 g_free (p);
2494 #endif
2497 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2498 handle_console (CONSOLE_INIT);
2500 # ifdef HAVE_SUBSHELL_SUPPORT
2501 subshell_get_console_attributes ();
2502 # endif
2504 /* Install the SIGCHLD handler; must be done before init_subshell() */
2505 init_sigchld ();
2507 compatibility_move_mc_files ();
2509 /* We need this, since ncurses endwin () doesn't restore the signals */
2510 save_stop_handler ();
2512 /* Must be done before init_subshell, to set up the terminal size: */
2513 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2514 #ifdef HAVE_SLANG
2515 slang_init ();
2516 #endif
2517 /* NOTE: This call has to be after slang_init. It's the small part from
2518 the previous init_key which had to be moved after the call of slang_init */
2519 init_key_input_fd ();
2521 load_setup ();
2523 init_curses ();
2525 init_xterm_support ();
2527 #ifdef HAVE_SUBSHELL_SUPPORT
2529 /* Don't use subshell when invoked as a viewer or editor */
2530 if (edit_one_file || view_one_file)
2531 use_subshell = 0;
2533 /* Done here to ensure that the subshell doesn't */
2534 /* inherit the file descriptors opened below, etc */
2535 if (use_subshell)
2536 init_subshell ();
2538 #endif /* HAVE_SUBSHELL_SUPPORT */
2540 /* Removing this from the X code let's us type C-c */
2541 load_key_defs ();
2543 /* Also done after init_subshell, to save any shell init file messages */
2544 if (console_flag)
2545 handle_console (CONSOLE_SAVE);
2547 if (alternate_plus_minus)
2548 application_keypad_mode ();
2550 if (show_change_notice){
2551 message (1, _(" Notice "),
2552 _(" The Midnight Commander configuration files \n"
2553 " are now stored in the ~/.mc directory, the \n"
2554 " files have been moved now\n"));
2557 #ifdef HAVE_SUBSHELL_SUPPORT
2558 if (use_subshell){
2559 prompt = strip_ctrl_codes (subshell_prompt);
2560 if (!prompt)
2561 prompt = "";
2562 } else
2563 #endif /* HAVE_SUBSHELL_SUPPORT */
2564 prompt = (geteuid () == 0) ? "# " : "$ ";
2566 /* Program main loop */
2567 do_nc ();
2569 /* Save the tree store */
2570 tree_store_save ();
2572 /* Virtual File System shutdown */
2573 vfs_shut ();
2575 /* Delete list of all user groups */
2576 destroy_groups ();
2578 flush_extension_file (); /* does only free memory */
2580 endwin ();
2581 #ifdef HAVE_SLANG
2582 slang_shutdown ();
2583 #endif
2585 if (console_flag && !(quit & SUBSHELL_EXIT))
2586 restore_console ();
2587 if (alternate_plus_minus)
2588 numeric_keypad_mode ();
2590 #ifndef OS2_NT
2591 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2592 #endif
2594 if (console_flag)
2595 handle_console (CONSOLE_DONE);
2596 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2598 #ifdef _OS_NT
2599 /* On NT, home_dir is malloced */
2600 g_free (home_dir);
2601 #endif
2602 #if defined(OS2_NT)
2603 if (print_last_wd == 2){
2604 FILE *bat_file;
2606 print_last_wd = 0;
2607 bat_file = fopen(batch_file_name, "w");
2608 if (bat_file != NULL){
2609 /* .ado: \r\n for Win95 */
2610 fprintf(bat_file, "@echo off\r\n");
2611 if (isalpha(last_wd_string[0]))
2612 fprintf(bat_file, "%c:\r\n", last_wd_string[0]);
2613 fprintf(bat_file, "cd %s\r\n", last_wd_string);
2614 fclose(bat_file);
2617 #endif
2618 if (print_last_wd) {
2619 if (print_last_revert || edit_one_file || view_one_file)
2620 write (stdout_fd, ".", 1);
2621 else
2622 write (stdout_fd, last_wd_string, strlen (last_wd_string));
2623 g_free (last_wd_string);
2626 #ifndef _OS_NT
2627 g_free (mc_home);
2628 #endif /* (_OS_NT) */
2629 done_key ();
2630 #ifdef HAVE_CHARSET
2631 free_codepages_list ();
2632 #endif
2633 if (this_dir)
2634 g_free (this_dir);
2635 if (other_dir)
2636 g_free (other_dir);
2638 return 0;