*** empty log message ***
[midnight-commander.git] / src / main.c
blobe9e41ca5689a417dca331ff7753a529ed325cdb9
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 /* widget colors for the midnight commander */
270 int midnight_colors [4];
272 /* Force colors, only used by Slang */
273 int force_colors = 0;
275 /* colors specified on the command line: they override any other setting */
276 char *command_line_colors;
278 /* File name to view if argument was supplied */
279 char *view_one_file = 0;
281 /* File name to edit if argument was supplied */
282 char *edit_one_file = 0;
283 static int edit_one_file_start_line = 1;
285 /* Used so that widgets know if they are being destroyed or
286 shut down */
287 int midnight_shutdown = 0;
289 /* to show nice prompts */
290 static int last_paused = 0;
292 /* Used for keeping track of the original stdout */
293 int stdout_fd = 0;
295 /* The user's shell */
296 char *shell;
298 /* mc_home: The home of MC */
299 char *mc_home;
301 /* if on, it displays the information that files have been moved to ~/.mc */
302 int show_change_notice = 0;
304 char cmd_buf [512];
306 /* Used during argument processing */
307 int finish_program = 0;
309 WPanel *
310 get_current_panel (void)
312 return current_panel;
315 WPanel *
316 get_other_panel (void)
318 return (WPanel *) get_panel_widget (get_other_index ());
321 static void
322 reload_panelized (WPanel *panel)
324 int i, j;
325 dir_list *list = &panel->dir;
327 if (panel != cpanel)
328 mc_chdir (panel->cwd);
330 for (i = 0, j = 0; i < panel->count; i++){
331 if (list->list [i].f.marked) {
332 /* Unmark the file in advance. In case the following mc_lstat
333 * fails we are done, else we have to mark the file again
334 * (Note: do_file_mark depends on a valid "list->list [i].buf").
335 * IMO that's the best way to update the panel's summary status
336 * -- Norbert
338 do_file_mark (panel, i, 0);
340 if (mc_lstat (list->list [i].fname, &list->list [i].buf)){
341 g_free (list->list [i].fname);
342 continue;
344 if (list->list [i].f.marked)
345 do_file_mark (panel, i, 1);
346 if (j != i)
347 list->list [j] = list->list [i];
348 j++;
350 if (j == 0)
351 panel->count = set_zero_dir (list);
352 else
353 panel->count = j;
355 if (panel != cpanel)
356 mc_chdir (cpanel->cwd);
359 static void
360 update_one_panel_widget (WPanel *panel, int force_update, char *current_file)
362 int free_pointer;
364 if (force_update & UP_RELOAD){
365 panel->is_panelized = 0;
367 #if 1
368 ftpfs_flushdir ();
369 #endif
370 /* FIXME: Should supply flushdir method */
371 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
374 /* If current_file == -1 (an invalid pointer) then preserve selection */
375 if (current_file == UP_KEEPSEL){
376 free_pointer = 1;
377 current_file = g_strdup (panel->dir.list [panel->selected].fname);
378 } else
379 free_pointer = 0;
381 if (panel->is_panelized)
382 reload_panelized (panel);
383 else
384 panel_reload (panel);
386 try_to_select (panel, current_file);
387 panel->dirty = 1;
389 if (free_pointer)
390 g_free (current_file);
393 void
394 panel_clean_dir (WPanel *panel)
396 int count = panel->count;
398 panel->count = 0;
399 panel->top_file = 0;
400 panel->selected = 0;
401 panel->marked = 0;
402 panel->dirs_marked = 0;
403 panel->total = 0;
404 panel->searching = 0;
405 panel->is_panelized = 0;
407 clean_dir (&panel->dir, count);
410 static void
411 update_one_panel (int which, int force_update, char *current_file)
413 WPanel *panel;
415 if (get_display_type (which) != view_listing)
416 return;
418 panel = (WPanel *) get_panel_widget (which);
419 update_one_panel_widget (panel, force_update, current_file);
422 /* This routine reloads the directory in both panels. It tries to
423 * select current_file in current_panel and other_file in other_panel.
424 * If current_file == -1 then it automatically sets current_file and
425 * other_file to the currently selected files in the panels.
427 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
428 * will not reload the other panel.
430 void
431 update_panels (int force_update, char *current_file)
433 int reload_other = !(force_update & UP_ONLY_CURRENT);
434 WPanel *panel;
436 update_one_panel (get_current_index (), force_update, current_file);
437 if (reload_other)
438 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
440 if (get_current_type () == view_listing)
441 panel = (WPanel *) get_panel_widget (get_current_index ());
442 else
443 panel = (WPanel *) get_panel_widget (get_other_index ());
445 mc_chdir (panel->cwd);
448 /* Sets up the terminal before executing a program */
449 static void
450 pre_exec (void)
452 use_dash (0);
453 edition_pre_exec ();
456 /* Save current stat of directories to avoid reloading the panels */
457 /* when no modifications have taken place */
458 void
459 save_cwds_stat (void)
461 if (fast_reload){
462 mc_stat (cpanel->cwd, &(cpanel->dir_stat));
463 if (get_other_type () == view_listing)
464 mc_stat (opanel->cwd, &(opanel->dir_stat));
468 #ifdef HAVE_SUBSHELL_SUPPORT
469 void
470 do_possible_cd (char *new_dir)
472 if (!do_cd (new_dir, cd_exact))
473 message (1, _(" Warning "),
474 _(" The Commander can't change to the directory that \n"
475 " the subshell claims you are in. Perhaps you have \n"
476 " deleted your working directory, or given yourself \n"
477 " extra access permissions with the \"su\" command? "));
480 void
481 do_update_prompt (void)
483 if (update_prompt){
484 printf ("%s", subshell_prompt);
485 fflush (stdout);
486 update_prompt = 0;
489 #endif /* HAVE_SUBSHELL_SUPPORT */
491 void
492 restore_console (void)
494 handle_console (CONSOLE_RESTORE);
497 void
498 exec_shell (void)
500 do_execute (shell, 0, 0);
503 void
504 do_execute (const char *shell, const char *command, int flags)
506 #ifdef HAVE_SUBSHELL_SUPPORT
507 char *new_dir = NULL;
508 #endif /* HAVE_SUBSHELL_SUPPORT */
510 #ifdef USE_VFS
511 char *old_vfs_dir = 0;
513 if (!vfs_current_is_local ())
514 old_vfs_dir = g_strdup (vfs_get_current_dir ());
515 #endif /* USE_VFS */
517 save_cwds_stat ();
518 pre_exec ();
519 if (console_flag)
520 restore_console ();
522 if (!use_subshell && !(flags & EXECUTE_INTERNAL && command)) {
523 printf ("%s%s%s\n", last_paused ? "\r\n":"", prompt, command);
524 last_paused = 0;
527 #ifdef HAVE_SUBSHELL_SUPPORT
528 if (use_subshell && !(flags & EXECUTE_INTERNAL)){
529 do_update_prompt ();
531 /* We don't care if it died, higher level takes care of this */
532 #ifdef USE_VFS
533 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
534 #else
535 invoke_subshell (command, VISIBLY, &new_dir);
536 #endif /* !USE_VFS */
537 } else
538 #endif /* HAVE_SUBSHELL_SUPPORT */
539 my_system (flags, shell, command);
541 if (!(flags & EXECUTE_INTERNAL)) {
542 if ((pause_after_run == pause_always ||
543 (pause_after_run == pause_on_dumb_terminals &&
544 !xterm_flag && !console_flag)) && !quit
545 #ifdef HAVE_SUBSHELL_SUPPORT
546 && subshell_state != RUNNING_COMMAND
547 #endif /* HAVE_SUBSHELL_SUPPORT */
549 printf ("%s\r\n", _("Press any key to continue..."));
550 last_paused = 1;
551 fflush (stdout);
552 mc_raw_mode ();
553 xgetch ();
555 if (console_flag) {
556 if (output_lines && keybar_visible) {
557 putchar('\n');
558 fflush(stdout);
563 if (console_flag)
564 handle_console (CONSOLE_SAVE);
565 edition_post_exec ();
567 #ifdef HAVE_SUBSHELL_SUPPORT
568 if (new_dir)
569 do_possible_cd (new_dir);
571 #endif /* HAVE_SUBSHELL_SUPPORT */
573 #ifdef USE_VFS
574 if (old_vfs_dir){
575 mc_chdir (old_vfs_dir);
576 g_free (old_vfs_dir);
578 #endif /* USE_VFS */
580 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
582 do_refresh ();
583 use_dash (TRUE);
586 /* Executes a command */
587 void
588 shell_execute (char *command, int flags)
590 #ifdef HAVE_SUBSHELL_SUPPORT
591 if (use_subshell)
592 if (subshell_state == INACTIVE || force_subshell_execution)
593 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
594 else
595 message (1, MSG_ERROR, _(" The shell is already running a command "));
596 else
597 #endif /* HAVE_SUBSHELL_SUPPORT */
598 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
601 void
602 execute (char *command)
604 shell_execute (command, 0);
607 void
608 change_panel (void)
610 free_completions (input_w (cmdline));
611 dlg_one_down (midnight_dlg);
614 /* Stop MC main dialog and the current dialog if it exists.
615 * Needed to provide fast exit from MC viewer or editor on shell exit */
616 static void stop_dialogs (void)
618 midnight_dlg->running = 0;
619 if (current_dlg) {
620 current_dlg->running = 0;
624 static int
625 quit_cmd_internal (int quiet)
627 int q = quit;
629 if (quiet || !confirm_exit){
630 q = 1;
631 } else {
632 if (query_dialog (_(" The Midnight Commander "),
633 _(" Do you really want to quit the Midnight Commander? "),
634 0, 2, _("&Yes"), _("&No")) == 0)
635 q = 1;
637 if (q){
638 #ifdef HAVE_SUBSHELL_SUPPORT
639 if (!use_subshell)
640 stop_dialogs ();
641 else
642 if ((q = exit_subshell ()))
643 #endif
644 stop_dialogs ();
646 if (q)
647 quit |= 1;
648 return quit;
651 int quit_cmd (void)
653 quit_cmd_internal (0);
654 return quit;
657 int quiet_quit_cmd (void)
659 print_last_revert = 1;
660 quit_cmd_internal (1);
661 return quit;
665 * Touch window and refresh window functions
668 /* This routine untouches the first line on both panels in order */
669 /* to avoid the refreshing the menu bar */
671 void
672 untouch_bar (void)
674 do_refresh ();
677 void
678 repaint_screen (void)
680 do_refresh ();
681 mc_refresh ();
684 /* Wrapper for do_subshell_chdir, check for availability of subshell */
685 void
686 subshell_chdir (char *directory)
688 #ifdef HAVE_SUBSHELL_SUPPORT
689 if (use_subshell){
690 if (vfs_current_is_local ())
691 do_subshell_chdir (directory, 0, 1);
693 #endif /* HAVE_SUBSHELL_SUPPORT */
696 void
697 directory_history_add (WPanel * panel, char *s)
699 if (!panel->dir_history) {
700 panel->dir_history = g_new0 (Hist, 1);
701 panel->dir_history->text = g_strdup (s);
702 strip_password (panel->dir_history->text, 1);
703 return;
705 if (!strcmp (panel->dir_history->text, s))
706 return;
707 if (panel->dir_history->next) {
708 if (panel->dir_history->next->text) {
709 g_free (panel->dir_history->next->text);
710 panel->dir_history->next->text = 0;
712 } else {
713 panel->dir_history->next = g_new0 (Hist, 1);
714 panel->dir_history->next->prev = panel->dir_history;
716 panel->dir_history = panel->dir_history->next;
717 panel->dir_history->text = g_strdup (s);
718 strip_password (panel->dir_history->text, 1);
722 * If we moved to the parent directory move the selection pointer to
723 * the old directory name; If we leave VFS dir, remove FS specificator.
724 * Warn: This code spoils lwd string.
726 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
728 static char*
729 get_parent_dir_name (char *cwd, char *lwd)
731 char *p;
732 if (strlen (lwd) > strlen (cwd))
733 if((p=strrchr(lwd, PATH_SEP)) && !strncmp(cwd, lwd, p-lwd)){
734 return (p+1);
736 return NULL;
739 /* Changes the current panel directory */
740 static int
741 _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
743 char *directory, *olddir;
744 char temp [MC_MAXPATHLEN];
745 char *translated_url;
746 #ifdef USE_VFS
747 vfs *oldvfs;
748 vfsid oldvfsid;
749 struct vfs_stamping *parent;
750 #endif
751 olddir = g_strdup (panel->cwd);
752 translated_url = new_dir = vfs_translate_url (new_dir);
754 /* Convert *new_path to a suitable pathname, handle ~user */
756 if (cd_type == cd_parse_command){
757 while (*new_dir == ' ')
758 new_dir++;
760 if (!strcmp (new_dir, "-")){
761 strcpy (temp, panel->lwd);
762 new_dir = temp;
765 directory = *new_dir ? new_dir : home_dir;
767 if (mc_chdir (directory) == -1){
768 strcpy (panel->cwd, olddir);
769 g_free (olddir);
770 g_free (translated_url);
771 return 0;
773 g_free (translated_url);
775 /* Success: save previous directory, shutdown status of previous dir */
776 strcpy (panel->lwd, olddir);
777 free_completions (input_w (cmdline));
779 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
781 #ifdef USE_VFS
782 oldvfs = vfs_type (olddir);
783 oldvfsid = vfs_ncs_getid (oldvfs, olddir, &parent);
784 vfs_add_noncurrent_stamps (oldvfs, oldvfsid, parent);
785 vfs_rm_parents (parent);
786 #endif
788 subshell_chdir (panel->cwd);
790 /* Reload current panel */
791 panel_clean_dir (panel);
792 panel->count = do_load_dir (&panel->dir, panel->sort_type,
793 panel->reverse, panel->case_sensitive, panel->filter);
794 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
795 load_hint ();
796 panel_update_contents (panel);
798 g_free (olddir);
800 return 1;
804 do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
806 int r;
808 r = _do_panel_cd (panel, new_dir, cd_type);
809 if (r)
810 directory_history_add (cpanel, cpanel->cwd);
811 return r;
815 do_cd (char *new_dir, enum cd_enum exact)
817 return (do_panel_cd (cpanel, new_dir, exact));
820 void
821 directory_history_next (WPanel * panel)
823 if (!panel->dir_history->next)
824 return;
825 if (_do_panel_cd (panel, panel->dir_history->next->text, cd_exact))
826 panel->dir_history = panel->dir_history->next;
829 void
830 directory_history_prev (WPanel * panel)
832 if (!panel->dir_history->prev)
833 return;
834 if (_do_panel_cd (panel, panel->dir_history->prev->text, cd_exact))
835 panel->dir_history = panel->dir_history->prev;
838 void
839 directory_history_list (WPanel * panel)
841 char *s;
842 /* must be at least two to show a history */
843 if (panel->dir_history) {
844 if (panel->dir_history->prev || panel->dir_history->next) {
845 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
846 if (s) {
847 int r;
848 r = _do_panel_cd (panel, s, cd_exact);
849 if (r)
850 directory_history_add (panel, panel->cwd);
851 else
852 message (1, MSG_ERROR, _("Could not change directory") );
853 g_free (s);
859 #ifdef HAVE_SUBSHELL_SUPPORT
861 load_prompt (int fd, void *unused)
863 if (!read_subshell_prompt ())
864 return 0;
866 /* Don't actually change the prompt if it's invisible */
867 if (current_dlg == midnight_dlg && command_prompt){
868 int prompt_len;
870 prompt = strip_ctrl_codes (subshell_prompt);
871 prompt_len = strlen (prompt);
873 /* Check for prompts too big */
874 if (prompt_len > COLS - 8) {
875 prompt [COLS - 8 ] = 0;
876 prompt_len = COLS - 8;
878 label_set_text (the_prompt, prompt);
879 winput_set_origin ((WInput *)cmdline, prompt_len, COLS-prompt_len);
881 /* since the prompt has changed, and we are called from one of the
882 * get_event channels, the prompt updating does not take place
883 * automatically: force a cursor update and a screen refresh
885 update_cursor (midnight_dlg);
886 mc_refresh ();
888 update_prompt = 1;
889 return 0;
891 #endif /* HAVE_SUBSHELL_SUPPORT */
893 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
895 maybe_cd (int char_code, int move_up_dir)
897 if (navigate_with_arrows){
898 if (!input_w (cmdline)->buffer [0]){
899 if (!move_up_dir){
900 do_cd ("..", cd_exact);
901 return 1;
903 if (S_ISDIR (selection (cpanel)->buf.st_mode)
904 || link_isdir (selection (cpanel))){
905 do_cd (selection (cpanel)->fname, cd_exact);
906 return 1;
910 return 0;
913 void
914 sort_cmd (void)
916 WPanel *p;
917 sortfn *sort_order;
919 if (!SELECTED_IS_PANEL)
920 return;
922 p = MENU_PANEL;
923 sort_order = sort_box (p->sort_type, &p->reverse, &p->case_sensitive);
925 panel_set_sort_order (p, sort_order);
929 static void
930 treebox_cmd (void)
932 char *sel_dir;
934 sel_dir = tree_box (selection (cpanel)->fname);
935 if (sel_dir){
936 do_cd(sel_dir, cd_exact);
937 g_free (sel_dir);
941 #ifdef LISTMODE_EDITOR
942 static void
943 listmode_cmd (void)
945 char *newmode;
946 newmode = listmode_edit ("half <type,>name,|,size:8,|,perm:4+");
947 message (0, _(" Listing format edit "), _(" New mode is \"%s\" "), newmode);
948 g_free (newmode);
950 #endif /* LISTMODE_EDITOR */
952 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
953 static menu_entry PanelMenu [] = {
954 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
955 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
956 { ' ', N_("&Info C-x i"), 'I', info_cmd },
957 { ' ', N_("&Tree"), 'T', tree_cmd },
958 { ' ', "", ' ', 0 },
959 { ' ', N_("&Sort order..."), 'S', sort_cmd },
960 { ' ', "", ' ', 0 },
961 { ' ', N_("&Filter..."), 'F', filter_cmd },
962 #ifdef USE_NETCODE
963 { ' ', "", ' ', 0 },
964 #ifdef WITH_MCFS
965 { ' ', N_("&Network link..."), 'N', netlink_cmd },
966 #endif
967 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
968 { ' ', N_("S&hell link..."), 'H', fishlink_cmd },
969 #ifdef WITH_SMBFS
970 { ' ', N_("SM&B link..."), 'B', smblink_cmd },
971 #endif
972 #endif
973 { ' ', "", ' ', 0 },
974 #ifdef OS2_NT
975 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_a },
976 #endif
977 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
980 static menu_entry RightMenu [] = {
981 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
982 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
983 { ' ', N_("&Info C-x i"), 'I', info_cmd },
984 { ' ', N_("&Tree"), 'T', tree_cmd },
985 { ' ', "", ' ', 0 },
986 { ' ', N_("&Sort order..."), 'S', sort_cmd },
987 { ' ', "", ' ', 0 },
988 { ' ', N_("&Filter..."), 'F', filter_cmd },
989 #ifdef USE_NETCODE
990 { ' ', "", ' ', 0 },
991 #ifdef WITH_MCFS
992 { ' ', N_("&Network link..."), 'N', netlink_cmd },
993 #endif
994 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
995 { ' ', N_("S&hell link..."), 'H', fishlink_cmd },
996 #ifdef WITH_SMBFS
997 { ' ', N_("SM&B link..."), 'B', smblink_cmd },
998 #endif
999 #endif
1000 { ' ', "", ' ', 0 },
1001 #ifdef OS2_NT
1002 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_b },
1003 #endif
1004 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
1007 static menu_entry FileMenu [] = {
1008 { ' ', N_("&User menu F2"), 'U', user_file_menu_cmd },
1009 { ' ', N_("&View F3"), 'V', view_cmd },
1010 { ' ', N_("Vie&w file... "), 'W', view_file_cmd },
1011 { ' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd },
1012 { ' ', N_("&Edit F4"), 'E', edit_cmd },
1013 { ' ', N_("&Copy F5"), 'C', copy_cmd },
1014 { ' ', N_("c&Hmod C-x c"), 'H', chmod_cmd },
1015 #ifndef OS2_NT
1016 { ' ', N_("&Link C-x l"), 'L', link_cmd },
1017 { ' ', N_("&SymLink C-x s"), 'S', symlink_cmd },
1018 { ' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd },
1019 { ' ', N_("ch&Own C-x o"), 'O', chown_cmd },
1020 { ' ', N_("&Advanced chown "), 'A', chown_advanced_cmd },
1021 #endif
1022 { ' ', N_("&Rename/Move F6"), 'R', ren_cmd },
1023 { ' ', N_("&Mkdir F7"), 'M', mkdir_cmd },
1024 { ' ', N_("&Delete F8"), 'D', delete_cmd },
1025 { ' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd },
1026 { ' ', "", ' ', 0 },
1027 { ' ', N_("select &Group M-+"), 'G', select_cmd },
1028 { ' ', N_("u&Nselect group M-\\"),'N', unselect_cmd },
1029 { ' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd },
1030 { ' ', "", ' ', 0 },
1031 { ' ', N_("e&Xit F10"), 'X', (callfn) quit_cmd }
1034 static menu_entry CmdMenu [] = {
1035 /* I know, I'm lazy, but the tree widget when it's not running
1036 * as a panel still has some problems, I have not yet finished
1037 * the WTree widget port, sorry.
1039 { ' ', N_("&Directory tree"), 'D', treebox_cmd },
1040 { ' ', N_("&Find file M-?"), 'F', find_cmd },
1041 { ' ', N_("s&Wap panels C-u"), 'W', swap_cmd },
1042 { ' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd },
1043 { ' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd },
1044 { ' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize },
1045 { ' ', N_("show directory s&Izes"), 'I', dirsizes_cmd },
1046 { ' ', "", ' ', 0 },
1047 { ' ', N_("command &History"), 'H', history_cmd },
1048 { ' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd },
1049 #ifdef USE_VFS
1050 { ' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs },
1051 { ' ', N_("Fr&ee VFSs now"), 'E', free_vfs_now },
1052 #endif
1053 #ifdef WITH_BACKGROUND
1054 { ' ', N_("&Background jobs C-x j"), 'B', jobs_cmd },
1055 #endif
1056 { ' ', "", ' ', 0 },
1057 #ifdef USE_EXT2FSLIB
1058 { ' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd },
1059 #endif
1060 #ifdef LISTMODE_EDITOR
1061 { ' ', N_("&Listing format edit"), 'L', listmode_cmd},
1062 #endif
1063 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
1064 { ' ', "", ' ', 0 },
1065 #endif
1066 { ' ', N_("&Extension file edit"), 'E', ext_cmd },
1067 { ' ', N_("&Menu file edit"), 'M', menu_edit_cmd },
1068 #ifdef USE_INTERNAL_EDIT
1069 {' ', N_("Menu edi&Tor edit"), 'T', edit_user_menu_cmd},
1070 {' ', N_("&Syntax file edit"), 'S', edit_syntax_cmd}
1071 #endif /* USE_INTERNAL_EDIT */
1074 /* Must keep in sync with the constants in menu_cmd */
1075 static menu_entry OptMenu [] = {
1076 { ' ', N_("&Configuration..."), 'C', configure_box },
1077 { ' ', N_("&Layout..."), 'L', layout_cmd },
1078 { ' ', N_("c&Onfirmation..."), 'O', confirm_box },
1079 { ' ', N_("&Display bits..."), 'D', display_bits_box },
1080 #ifndef OS2_NT
1081 { ' ', N_("learn &Keys..."), 'K', learn_keys },
1082 #endif /* !OS2_NT */
1083 #ifdef USE_VFS
1084 { ' ', N_("&Virtual FS..."), 'V', configure_vfs },
1085 #endif /* !USE_VFS */
1086 { ' ', "", ' ', 0 },
1087 { ' ', N_("&Save setup"), 'S', save_setup_cmd }
1090 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
1092 static Menu MenuBar [5];
1094 void
1095 init_menu (void)
1097 MenuBar [0] = create_menu ( horizontal_split ? _(" &Above ") : _(" &Left "),
1098 PanelMenu, menu_entries (PanelMenu),
1099 "[Left and Right Menus]");
1100 MenuBar [1] = create_menu (_(" &File "), FileMenu, menu_entries (FileMenu),
1101 "[File Menu]");
1102 MenuBar [2] = create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu),
1103 "[Command Menu]");
1104 MenuBar [3] = create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu),
1105 "[Options Menu]");
1106 MenuBar [4] = create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
1107 RightMenu, menu_entries (PanelMenu),
1108 "[Left and Right Menus]");
1111 void
1112 done_menu (void)
1114 int i;
1116 for (i = 0; i < 5; i++){
1117 destroy_menu (MenuBar [i]);
1121 static void
1122 menu_last_selected_cmd (void)
1124 the_menubar->active = 1;
1125 the_menubar->dropped = drop_menus;
1126 the_menubar->previous_selection = dlg_item_number (midnight_dlg);
1127 dlg_select_widget (midnight_dlg, the_menubar);
1130 static void
1131 menu_cmd (void)
1133 if (the_menubar->active)
1134 return;
1136 if (get_current_index () == 0)
1137 the_menubar->selected = 0;
1138 else
1139 the_menubar->selected = 4;
1140 menu_last_selected_cmd ();
1143 /* Flag toggling functions */
1144 void
1145 toggle_fast_reload (void)
1147 fast_reload = !fast_reload;
1148 if (fast_reload_w == 0 && fast_reload){
1149 message (0, _(" Information "),
1150 _(" Using the fast reload option may not reflect the exact \n"
1151 " directory contents. In this cases you'll need to do a \n"
1152 " manual reload of the directory. See the man page for \n"
1153 " the details. "));
1154 fast_reload_w = 1;
1158 void
1159 toggle_mix_all_files (void)
1161 mix_all_files = !mix_all_files;
1162 update_panels (UP_RELOAD, UP_KEEPSEL);
1165 void
1166 toggle_show_backup (void)
1168 show_backups = !show_backups;
1169 update_panels (UP_RELOAD, UP_KEEPSEL);
1172 void
1173 toggle_show_hidden (void)
1175 show_dot_files = !show_dot_files;
1176 update_panels (UP_RELOAD, UP_KEEPSEL);
1180 * Just a hack for allowing url-like pathnames to be accepted from the
1181 * command line.
1183 static void
1184 translated_mc_chdir (char *dir)
1186 char *newdir;
1188 newdir = vfs_translate_url (dir);
1189 mc_chdir (newdir);
1190 g_free (newdir);
1193 void
1194 create_panels (void)
1196 int current_index;
1197 int other_index;
1198 int current_mode;
1199 int other_mode;
1200 char original_dir [1024];
1202 original_dir [0] = 0;
1204 if (boot_current_is_left){
1205 current_index = 0;
1206 other_index = 1;
1207 current_mode = startup_left_mode;
1208 other_mode = startup_right_mode;
1209 } else {
1210 current_index = 1;
1211 other_index = 0;
1212 current_mode = startup_right_mode;
1213 other_mode = startup_left_mode;
1215 /* Creates the left panel */
1216 if (this_dir){
1217 if (other_dir){
1218 /* Ok, user has specified two dirs, save the original one,
1219 * since we may not be able to chdir to the proper
1220 * second directory later
1222 mc_get_current_wd (original_dir, sizeof (original_dir)-2);
1224 translated_mc_chdir (this_dir);
1226 set_display_type (current_index, current_mode);
1228 /* The other panel */
1229 if (other_dir){
1230 if (original_dir [0])
1231 translated_mc_chdir (original_dir);
1232 translated_mc_chdir (other_dir);
1234 set_display_type (other_index, other_mode);
1236 if (startup_left_mode == view_listing){
1237 current_panel = left_panel;
1238 } else {
1239 if (right_panel)
1240 current_panel = right_panel;
1241 else
1242 current_panel = left_panel;
1245 /* Create the nice widgets */
1246 cmdline = command_new (0, 0, 0);
1247 the_prompt = label_new (0, 0, prompt, NULL);
1248 the_prompt->transparent = 1;
1249 the_bar = buttonbar_new (keybar_visible);
1251 the_hint = label_new (0, 0, 0, NULL);
1252 the_hint->transparent = 1;
1253 the_hint->auto_adjust_cols = 0;
1254 the_hint->widget.cols = COLS;
1256 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1259 static void copy_current_pathname (void)
1261 if (!command_prompt)
1262 return;
1264 stuff (input_w (cmdline), cpanel->cwd, 0);
1265 if (cpanel->cwd [strlen (cpanel->cwd) - 1] != PATH_SEP)
1266 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1269 static void copy_other_pathname (void)
1271 if (get_other_type () != view_listing)
1272 return;
1274 if (!command_prompt)
1275 return;
1277 stuff (input_w (cmdline), opanel->cwd, 0);
1278 if (cpanel->cwd [strlen (opanel->cwd) - 1] != PATH_SEP)
1279 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1282 static void copy_readlink (WPanel *panel)
1284 if (!command_prompt)
1285 return;
1286 if (S_ISLNK (selection (panel)->buf.st_mode)) {
1287 char buffer [MC_MAXPATHLEN];
1288 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1289 int i;
1291 i = mc_readlink (p, buffer, MC_MAXPATHLEN);
1292 g_free (p);
1293 if (i > 0) {
1294 buffer [i] = 0;
1295 stuff (input_w (cmdline), buffer, 0);
1300 static void copy_current_readlink (void)
1302 copy_readlink (cpanel);
1305 static void copy_other_readlink (void)
1307 if (get_other_type () != view_listing)
1308 return;
1309 copy_readlink (opanel);
1312 /* Inserts the selected file name into the input line */
1313 /* Exported so that the command modules uses it */
1314 void copy_prog_name (void)
1316 char *tmp;
1317 if (!command_prompt)
1318 return;
1320 if (get_current_type () == view_tree){
1321 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1322 tmp = name_quote (tree->selected_ptr->name, 1);
1323 } else
1324 tmp = name_quote (selection (cpanel)->fname, 1);
1325 stuff (input_w (cmdline), tmp, 1);
1326 g_free (tmp);
1329 static void copy_tagged (WPanel *panel)
1331 int i;
1333 if (!command_prompt)
1334 return;
1335 input_disable_update (input_w (cmdline));
1336 if (panel->marked){
1337 for (i = 0; i < panel->count; i++)
1338 if (panel->dir.list [i].f.marked) {
1339 char *tmp = name_quote (panel->dir.list [i].fname, 1);
1340 stuff (input_w (cmdline), tmp, 1);
1341 g_free (tmp);
1343 } else {
1344 char *tmp = name_quote (panel->dir.list [panel->selected].fname, 1);
1345 stuff (input_w (cmdline), tmp, 1);
1346 g_free (tmp);
1348 input_enable_update (input_w (cmdline));
1351 static void copy_current_tagged (void)
1353 copy_tagged (cpanel);
1356 static void copy_other_tagged (void)
1358 if (get_other_type () != view_listing)
1359 return;
1360 copy_tagged (opanel);
1363 static void do_suspend_cmd (void)
1365 pre_exec ();
1367 if (console_flag && !use_subshell)
1368 restore_console ();
1370 #ifdef SIGTSTP
1372 struct sigaction sigtstp_action;
1374 /* Make sure that the SIGTSTP below will suspend us directly,
1375 without calling ncurses' SIGTSTP handler; we *don't* want
1376 ncurses to redraw the screen immediately after the SIGCONT */
1377 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
1379 kill (getpid (), SIGTSTP);
1381 /* Restore previous SIGTSTP action */
1382 sigaction (SIGTSTP, &sigtstp_action, NULL);
1384 #endif /* SIGTSTP */
1386 if (console_flag && !use_subshell)
1387 handle_console (CONSOLE_SAVE);
1389 edition_post_exec ();
1392 void
1393 suspend_cmd (void)
1395 save_cwds_stat ();
1396 do_suspend_cmd ();
1397 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1398 do_refresh ();
1401 static void
1402 init_labels (Widget *paneletc)
1404 define_label (midnight_dlg, paneletc, 1, _("Help"), help_cmd);
1405 define_label (midnight_dlg, paneletc, 2, _("Menu"), user_file_menu_cmd);
1406 define_label (midnight_dlg, paneletc, 9, _("PullDn"), menu_cmd);
1407 define_label (midnight_dlg, paneletc, 10, _("Quit"), (voidfn) quit_cmd);
1410 static const key_map ctl_x_map [] = {
1411 { XCTRL('c'), (callfn) quit_cmd },
1412 { 'd', compare_dirs_cmd },
1413 #ifdef USE_VFS
1414 { 'a', reselect_vfs },
1415 #endif /* USE_VFS */
1416 { 'p', copy_current_pathname },
1417 { XCTRL('p'), copy_other_pathname },
1418 { 't', copy_current_tagged },
1419 { XCTRL('t'), copy_other_tagged },
1420 { 'c', chmod_cmd },
1421 #ifndef OS2_NT
1422 { 'o', chown_cmd },
1423 { 'r', copy_current_readlink },
1424 { XCTRL('r'), copy_other_readlink },
1425 { 'l', link_cmd },
1426 { XCTRL('l'), other_symlink_cmd },
1427 { 's', symlink_cmd },
1428 { XCTRL('s'), edit_symlink_cmd },
1429 #endif /* !OS2_NT */
1430 { 'i', info_cmd_no_menu },
1431 { 'q', quick_cmd_no_menu },
1432 { 'h', add2hotlist_cmd },
1433 { '!', external_panelize },
1434 #ifdef WITH_BACKGROUND
1435 { 'j', jobs_cmd },
1436 #endif /* WITH_BACKGROUND */
1437 #ifdef HAVE_SETSOCKOPT
1438 { '%', source_routing },
1439 #endif /* HAVE_SETSOCKOPT */
1440 { 0, 0 }
1443 static int ctl_x_map_enabled = 0;
1445 static void ctl_x_cmd (int ignore)
1447 ctl_x_map_enabled = 1;
1450 static void nothing (void)
1454 static const key_map default_map [] = {
1455 { KEY_F(19), menu_last_selected_cmd },
1456 { KEY_F(20), (key_callback) quiet_quit_cmd },
1458 /* Copy useful information to the command line */
1459 { ALT('\n'), copy_prog_name },
1460 { ALT('\r'), copy_prog_name },
1461 { ALT('a'), copy_current_pathname },
1462 { ALT('A'), copy_other_pathname },
1464 { ALT('c'), quick_cd_cmd },
1466 /* To access the directory hotlist */
1467 { XCTRL('\\'), quick_chdir_cmd },
1469 /* Suspend */
1470 { XCTRL('z'), suspend_cmd },
1471 /* The filtered view command */
1472 { ALT('!'), filtered_view_cmd_cpanel },
1474 /* Find file */
1475 { ALT('?'), find_cmd },
1477 /* Panel refresh */
1478 { XCTRL('r'), reread_cmd },
1480 /* Toggle listing between long, user defined and full formats */
1481 { ALT('t'), toggle_listing_cmd },
1483 /* Swap panels */
1484 { XCTRL('u'), swap_cmd },
1486 /* View output */
1487 { XCTRL('o'), view_other_cmd },
1489 /* Control-X keybindings */
1490 { XCTRL('x'), ctl_x_cmd },
1492 /* Trap dlg's exit commands */
1493 { ESC_CHAR, nothing },
1494 { XCTRL('c'), nothing },
1495 { XCTRL('g'), nothing },
1496 { 0, 0 },
1499 static void setup_sigwinch (void)
1501 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && \
1502 !defined(OS2_NT) && defined(SIGWINCH)
1503 struct sigaction act, oact;
1504 act.sa_handler = flag_winch;
1505 sigemptyset (&act.sa_mask);
1506 act.sa_flags = 0;
1507 # ifdef SA_RESTART
1508 act.sa_flags |= SA_RESTART;
1509 # endif
1510 sigaction (SIGWINCH, &act, &oact);
1511 #endif
1514 static void
1515 setup_pre (void)
1517 /* Call all the inits */
1518 #ifdef HAVE_CHARSET
1520 * Don't restrict the output on the screen manager level,
1521 * the translation tables take care of it.
1523 #define full_eight_bits (1)
1524 #define eight_bit_clean (1)
1525 #endif /* !HAVE_CHARSET */
1527 #ifndef HAVE_SLANG
1528 meta (stdscr, eight_bit_clean);
1529 #else
1530 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1531 #endif
1534 static void
1535 init_xterm_support (void)
1537 char *termvalue;
1538 #ifdef HAVE_SLANG
1539 char *term_entry;
1540 #endif
1542 termvalue = getenv ("TERM");
1543 if (!termvalue || !(*termvalue)){
1544 fprintf (stderr, _("The TERM environment variable is unset!\n"));
1545 exit (1);
1548 /* Check mouse capabilities */
1549 #ifdef HAVE_SLANG
1550 term_entry = SLtt_tigetent (termvalue);
1551 xmouse_seq = SLtt_tigetstr ("Km", &term_entry);
1552 #else
1553 xmouse_seq = tigetstr ("kmous");
1554 #endif
1556 /* -1 means invalid capability, shouldn't happen, but let's make it 0 */
1557 if ((long) xmouse_seq == -1) {
1558 xmouse_seq = NULL;
1561 if (force_xterm
1562 || strncmp (termvalue, "xterm", 5) == 0
1563 || strncmp (termvalue, "rxvt", 4) == 0
1564 || strcmp (termvalue, "dtterm") == 0) {
1565 xterm_flag = 1;
1567 /* Default to the standard xterm sequence */
1568 if (!xmouse_seq) {
1569 xmouse_seq = ESC_STR "[M";
1572 /* Enable mouse unless explicitly disabled by --nomouse */
1573 if (use_mouse_p != MOUSE_DISABLED) {
1574 use_mouse_p = MOUSE_XTERM;
1577 #if 0 /* It works on xterm, but not on rxvt */
1578 printf (ESC_STR "]0;GNU Midnight Commander\7");
1579 #endif
1583 static void setup_mc (void)
1585 setup_pre ();
1586 init_menu ();
1587 create_panels ();
1588 setup_panels ();
1590 #ifdef HAVE_SUBSHELL_SUPPORT
1591 if (use_subshell)
1592 add_select_channel (subshell_pty, load_prompt, 0);
1593 #endif /* !HAVE_SUBSHELL_SUPPORT */
1595 setup_sigwinch ();
1597 if (baudrate () < 9600 || slow_terminal){
1598 verbose = 0;
1600 init_mouse ();
1602 midnight_colors [0] = 0;
1603 midnight_colors [1] = REVERSE_COLOR; /* FOCUSC */
1604 midnight_colors [2] = INPUT_COLOR; /* HOT_NORMALC */
1605 midnight_colors [3] = NORMAL_COLOR; /* HOT_FOCUSC */
1608 static void setup_dummy_mc (const char *file)
1610 char d[MC_MAXPATHLEN];
1612 mc_get_current_wd (d, MC_MAXPATHLEN);
1613 setup_mc ();
1614 mc_chdir (d);
1616 /* Create a fake current_panel, this is needed because the
1617 * expand_format routine will use current panel.
1619 strcpy (cpanel->cwd, d);
1620 cpanel->selected = 0;
1621 cpanel->count = 1;
1622 cpanel->dir.list[0].fname = (char *) file;
1625 static void done_mc (void)
1627 done_menu ();
1629 /* Setup shutdown
1631 * We sync the profiles since the hotlist may have changed, while
1632 * we only change the setup data if we have the auto save feature set
1634 if (auto_save_setup)
1635 save_setup (); /* does also call save_hotlist */
1636 else
1637 save_hotlist();
1638 done_screen ();
1639 vfs_add_current_stamps ();
1640 if (xterm_flag && xterm_hintbar)
1641 set_hintbar(_("Thank you for using GNU Midnight Commander"));
1644 /* This should be called after destroy_dlg since panel widgets
1645 * save their state on the profiles
1647 static void done_mc_profile (void)
1649 if (!auto_save_setup)
1650 profile_forget_profile (profile_name);
1651 sync_profiles ();
1652 done_setup ();
1653 free_profiles ();
1656 /* This routine only handles cpanel, and opanel, it is easy to
1657 * change to use npanels, just loop over the number of panels
1658 * and use get_panel_widget (i) and make the test done below
1660 static void
1661 make_panels_dirty (void)
1663 if (cpanel->dirty)
1664 panel_update_contents (cpanel);
1666 if ((get_other_type () == view_listing) && opanel->dirty)
1667 panel_update_contents (opanel);
1670 /* In OS/2 and Windows NT people want to actually type the '\' key frequently */
1671 #ifdef OS2_NT
1672 # define check_key_backslash(x) 0
1673 #else
1674 # define check_key_backslash(x) ((x) == '\\')
1675 #endif
1678 midnight_callback (struct Dlg_head *h, int id, int msg)
1680 int i;
1681 static int first_pre_event = 1;
1683 switch (msg){
1685 case DLG_PRE_EVENT:
1686 make_panels_dirty ();
1687 if (auto_menu && first_pre_event) {
1688 user_file_menu_cmd ();
1690 first_pre_event = 0;
1691 return MSG_HANDLED;
1693 case DLG_KEY:
1694 if (ctl_x_map_enabled){
1695 ctl_x_map_enabled = 0;
1696 for (i = 0; ctl_x_map [i].key_code; i++)
1697 if (id == ctl_x_map [i].key_code){
1698 (*ctl_x_map [i].fn)(id);
1699 return MSG_HANDLED;
1703 /* FIXME: should handle all menu shortcuts before this point */
1704 if (the_menubar->active)
1705 break;
1707 if (id == KEY_F(10)){
1708 quit_cmd ();
1709 return MSG_HANDLED;
1712 if (id == '\t')
1713 free_completions (input_w (cmdline));
1715 /* On Linux, we can tell the difference */
1716 if (id == '\n' && ctrl_pressed ()){
1717 copy_prog_name ();
1718 return MSG_HANDLED;
1721 if (id == '\n' && input_w (cmdline)->buffer [0]){
1722 send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1723 return MSG_HANDLED;
1726 if ((!alternate_plus_minus || !(console_flag || xterm_flag)) &&
1727 !quote && !cpanel->searching) {
1728 if(!only_leading_plus_minus) {
1729 /* Special treatement, since the input line will eat them */
1730 if (id == '+' ) {
1731 select_cmd ();
1732 return MSG_HANDLED;
1735 if (check_key_backslash (id) || id == '-'){
1736 unselect_cmd ();
1737 return MSG_HANDLED;
1740 if (id == '*') {
1741 reverse_selection_cmd ();
1742 return MSG_HANDLED;
1744 } else if (command_prompt && !strlen (input_w (cmdline)->buffer)) {
1745 /* Special treatement '+', '-', '\', '*' only when this is
1746 * first char on input line
1749 if (id == '+') {
1750 select_cmd ();
1751 return MSG_HANDLED;
1754 if (check_key_backslash (id) || id == '-') {
1755 unselect_cmd ();
1756 return MSG_HANDLED;
1759 if (id == '*') {
1760 reverse_selection_cmd ();
1761 return MSG_HANDLED;
1765 break;
1767 case DLG_HOTKEY_HANDLED:
1768 if (get_current_type () == view_listing)
1769 cpanel->searching = 0;
1770 break;
1772 case DLG_UNHANDLED_KEY:
1773 if (command_prompt){
1774 int v;
1776 v = send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1777 if (v)
1778 return v;
1780 if (ctl_x_map_enabled) {
1781 ctl_x_map_enabled = 0;
1782 for (i = 0; ctl_x_map [i].key_code; i++)
1783 if (id == ctl_x_map [i].key_code){
1784 (*ctl_x_map [i].fn)(id);
1785 return MSG_HANDLED;
1787 } else
1789 for (i = 0; default_map [i].key_code; i++){
1790 if (id == default_map [i].key_code){
1791 (*default_map [i].fn)(id);
1792 return MSG_HANDLED;
1796 return MSG_NOT_HANDLED;
1798 /* We handle the special case of the output lines */
1799 case DLG_DRAW:
1800 attrset (SELECTED_COLOR);
1801 if (console_flag && output_lines)
1802 show_console_contents (output_start_y,
1803 LINES-output_lines-keybar_visible-1,
1804 LINES-keybar_visible-1);
1805 break;
1808 return default_dlg_callback (h, id, msg);
1811 #define xtoolkit_panel_setup()
1813 void 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 set_hintbar ("The Midnight Commander " VERSION
1831 " (C) 1995-1997 the Free Software Foundation");
1835 static void
1836 setup_panels_and_run_mc (void)
1838 int first, second;
1840 xtoolkit_panel_setup ();
1841 add_widget (midnight_dlg, the_hint);
1842 load_hint ();
1843 add_widget (midnight_dlg, cmdline);
1844 add_widget (midnight_dlg, the_prompt);
1845 add_widget (midnight_dlg, the_bar);
1847 if (boot_current_is_left){
1848 first = 1;
1849 second = 0;
1850 } else {
1851 first = 0;
1852 second = 1;
1854 add_widget (midnight_dlg, get_panel_widget (first));
1855 add_widget (midnight_dlg, get_panel_widget (second));
1856 add_widget (midnight_dlg, the_menubar);
1858 init_labels (get_panel_widget (0));
1859 init_labels (get_panel_widget (1));
1861 /* Run the Midnight Commander if no file was specified in the command line */
1862 run_dlg (midnight_dlg);
1865 /* result must be free'd (I think this should go in util.c) */
1866 static char *
1867 prepend_cwd_on_local (char *filename)
1869 char *d;
1870 int l;
1872 if (vfs_file_is_local (filename)){
1873 if (*filename == PATH_SEP) /* an absolute pathname */
1874 return g_strdup (filename);
1875 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
1876 mc_get_current_wd (d, MC_MAXPATHLEN);
1877 l = strlen(d);
1878 d[l++] = PATH_SEP;
1879 strcpy (d + l, filename);
1880 return canonicalize_pathname (d);
1881 } else
1882 return g_strdup (filename);
1885 static int
1886 mc_maybe_editor_or_viewer (void)
1888 char *path = NULL;
1890 if (!(view_one_file || edit_one_file))
1891 return 0;
1893 /* Invoke the internal view/edit routine with:
1894 * the default processing and forcing the internal viewer/editor
1896 if (view_one_file) {
1897 path = prepend_cwd_on_local (view_one_file);
1898 setup_dummy_mc (path);
1899 view_file (path, 0, 1);
1901 #ifdef USE_INTERNAL_EDIT
1902 else {
1903 path = prepend_cwd_on_local ("");
1904 setup_dummy_mc (path);
1905 edit (edit_one_file, edit_one_file_start_line);
1907 #endif /* USE_INTERNAL_EDIT */
1908 g_free (path);
1909 midnight_shutdown = 1;
1910 done_mc ();
1911 return 1;
1914 static void
1915 do_nc (void)
1917 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors,
1918 midnight_callback, "[main]", "midnight",
1919 DLG_HAS_MENUBAR);
1921 /* Check if we were invoked as an editor or file viewer */
1922 if (mc_maybe_editor_or_viewer ())
1923 return;
1925 setup_mc ();
1927 setup_panels_and_run_mc ();
1929 /* Program end */
1930 midnight_shutdown = 1;
1932 /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */
1933 if (print_last_wd) {
1934 if (!vfs_current_is_local ())
1935 last_wd_string = g_strdup (".");
1936 else
1937 last_wd_string = g_strdup (cpanel->cwd);
1939 done_mc ();
1941 destroy_dlg (midnight_dlg);
1942 current_panel = 0;
1943 done_mc_profile ();
1946 #if defined (_OS_NT)
1947 /* Windows NT code */
1949 void
1950 OS_Setup (void)
1952 SetConsoleTitle ("GNU Midnight Commander");
1954 shell = getenv ("COMSPEC");
1955 if (!shell || !*shell)
1956 shell = get_default_shell ();
1958 /* Default opening mode for files is binary, not text (CR/LF translation) */
1959 #ifndef __EMX__
1960 _fmode = O_BINARY;
1961 #endif
1963 mc_home = get_mc_lib_dir ();
1966 static void
1967 sigchld_handler_no_subshell (int sig)
1971 void
1972 init_sigchld (void)
1976 void
1977 init_sigfatals (void)
1979 /* Nothing to be done on the OS/2, Windows/NT */
1983 #else
1985 /* Unix version */
1986 static void
1987 OS_Setup (void)
1989 char *mc_libdir;
1990 shell = getenv ("SHELL");
1991 if (!shell || !*shell)
1992 shell = g_strdup (getpwuid (geteuid ())->pw_shell);
1993 if (!shell || !*shell)
1994 shell = "/bin/sh";
1996 /* This is the directory, where MC was installed, on Unix this is LIBDIR */
1997 /* and can be overriden by the MC_LIBDIR environment variable */
1998 if ((mc_libdir = getenv ("MC_LIBDIR")) != NULL) {
1999 mc_home = g_strdup (mc_libdir);
2000 } else {
2001 mc_home = g_strdup (LIBDIR);
2005 static void
2006 sigchld_handler_no_subshell (int sig)
2008 #if defined(linux) || defined(__linux__)
2009 int pid, status;
2011 if (!console_flag)
2012 return;
2014 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2015 the value of console_flag never changed, we could simply not install
2016 this handler at all if (!console_flag && !use_subshell). */
2018 /* That comment is no longer true. We need to wait() on a sigchld
2019 handler (that's at least what the tarfs code expects currently). */
2021 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2023 if (pid == cons_saver_pid){
2025 if (WIFSTOPPED (status)) {
2026 /* Someone has stopped cons.saver - restart it */
2027 kill (pid, SIGCONT);
2028 } else {
2029 /* cons.saver has died - disable console saving */
2030 handle_console (CONSOLE_DONE);
2031 console_flag = 0;
2034 #endif /* linux || __linux__ */
2036 /* If we get here, some other child exited; ignore it */
2039 void
2040 init_sigchld (void)
2042 struct sigaction sigchld_action;
2044 sigchld_action.sa_handler =
2045 #ifdef HAVE_SUBSHELL_SUPPORT
2046 use_subshell ? sigchld_handler :
2047 #endif /* HAVE_SUBSHELL_SUPPORT */
2048 sigchld_handler_no_subshell;
2050 sigemptyset (&sigchld_action.sa_mask);
2052 #ifdef SA_RESTART
2053 sigchld_action.sa_flags = SA_RESTART;
2054 #else
2055 sigchld_action.sa_flags = 0;
2056 #endif /* !SA_RESTART */
2058 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2059 #ifdef HAVE_SUBSHELL_SUPPORT
2061 * This may happen on QNX Neutrino 6, where SA_RESTART
2062 * is defined but not implemented. Fallback to no subshell.
2064 use_subshell = 0;
2065 #endif /* HAVE_SUBSHELL_SUPPORT */
2069 #endif /* _OS_NT, UNIX */
2071 static void
2072 print_mc_usage (FILE *stream)
2074 const char * const * ptr;
2075 const char * const usage [] = {
2077 N_("Usage is:\n\n"
2078 "mc [flags] [this_dir] [other_panel_dir]\n\n"),
2079 #ifndef OS2_NT
2080 N_("-a, --stickchars Force use of +, -, | for line drawing.\n"),
2081 #endif /* !OS2_NT */
2082 N_("-b, --nocolor Force black and white display.\n"),
2083 #ifdef WITH_BACKGROUND
2084 N_("-B, --background [DEVEL-ONLY: Debug the background code]\n"),
2085 #endif
2086 N_("-c, --color Force color mode.\n"
2087 "-C, --colors Specify colors (use --help-colors to get a list).\n"
2088 "-d, --nomouse Disable mouse support.\n"),
2089 #ifdef USE_INTERNAL_EDIT
2090 N_("-e, --edit Startup the internal editor.\n"),
2091 #endif
2092 N_("-f, --libdir Print configured paths.\n"
2093 "-h, --help Shows this help message.\n"
2094 "-k, --resetsoft Reset softkeys (HP terminals only) to their terminfo/termcap\n"
2095 " default.\n"),
2096 #ifdef USE_NETCODE
2097 N_("-l, --ftplog file Log ftpfs commands to the file.\n"),
2098 #endif
2099 N_("-P, --printwd At exit, print the last working directory.\n"
2100 "-s, --slow Disables verbose operation (for slow terminals).\n"),
2101 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2102 N_("-t, --termcap Activate support for the TERMCAP variable.\n"),
2103 #endif
2104 #if defined(HAVE_SLANG) && defined(OS2_NT)
2105 N_("-S, --createcmdile Create command file to set default directory upon exit.\n"),
2106 #endif
2107 #ifdef HAVE_SUBSHELL_SUPPORT
2108 N_("-u, --nosubshell Disable the concurrent subshell mode.\n"
2109 "-U, --subshell Force the concurrent subshell mode.\n"
2110 "-r, --forceexec Force subshell execution.\n"),
2111 #endif
2112 N_("-v, --view fname Start up into the viewer mode.\n"
2113 "-V, --version Report version and configuration options.\n"
2114 "-x, --xterm Force xterm mouse support and screen save/restore.\n"
2115 "+number number it is the start line number of file for `mcedit'.\n"),
2116 N_("\n"
2117 "Please send any bug reports (including the output of `mc -V')\n"
2118 "to mc-devel@gnome.org\n"),
2119 NULL
2122 version (0);
2124 for (ptr = usage; *ptr; ptr++)
2125 fputs (_(*ptr), stream);
2128 static void
2129 print_color_usage (void)
2131 fputs (_("--colors KEYWORD={FORE},{BACK}\n\n"
2132 "{FORE} and {BACK} can be omitted, and the default will be used\n"
2133 "\n"
2134 "Keywords:\n"
2135 " Global: errors, reverse, gauge, input\n"
2136 " File display: normal, selected, marked, markselect\n"
2137 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus\n"
2138 " Menus: menu, menuhot, menusel, menuhotsel\n"
2139 " Help: helpnormal, helpitalic, helplink, helpslink\n"
2140 " File types: directory, execute, link, device, special, core\n"
2141 "\n"
2142 "Colors:\n"
2143 " black, gray, red, brightred, green, brightgreen, brown,\n"
2144 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
2145 " brightcyan, lightgray and white\n\n"), stdout);
2148 static void
2149 probably_finish_program (void)
2151 if (finish_program){
2152 if (print_last_wd)
2153 printf (".");
2154 exit (1);
2158 enum {
2159 GEOMETRY_KEY = -1,
2160 NOWIN_KEY = -2
2163 static void
2164 process_args (int c, const char *option_arg)
2166 switch (c) {
2167 case 'V':
2168 version (1);
2169 finish_program = 1;
2170 break;
2172 case 'c':
2173 disable_colors = 0;
2174 #ifdef HAVE_SLANG
2175 force_colors = 1;
2176 #endif
2177 break;
2179 case 'f':
2180 printf (_("Library directory for the Midnight Commander: %s\n"), mc_home);
2181 finish_program = 1;
2182 break;
2184 #ifdef USE_NETCODE
2185 case 'l':
2186 ftpfs_set_debug (option_arg);
2187 #ifdef WITH_SMBFS
2188 /* smbfs_set_debug (option_arg); */
2189 #endif
2190 break;
2191 #endif
2193 case 'm':
2194 fputs (_("Option -m is obsolete. Please look at Display Bits... in the Option's menu\n"), stderr);
2195 finish_program = 1;
2196 break;
2198 #ifdef OS2_NT
2199 case 'S':
2200 print_last_wd = 2;
2201 batch_file_name = option_arg;
2202 break;
2203 #endif
2205 case 'd':
2206 use_mouse_p = MOUSE_DISABLED;
2207 break;
2209 #ifdef HAVE_SUBSHELL_SUPPORT
2210 case 'u':
2211 use_subshell = 0;
2212 break;
2213 #endif /* HAVE_SUBSHELL_SUPPORT */
2215 case 'H':
2216 print_color_usage ();
2217 finish_program = 1;
2218 break;
2220 case 'h':
2221 print_mc_usage (stdout);
2222 finish_program = 1;
2226 static const struct poptOption argument_table [] = {
2227 #ifdef WITH_BACKGROUND
2228 { "background", 'B', POPT_ARG_NONE, &background_wait, 0,
2229 N_("Use to debug the background code") },
2230 #endif
2231 #if defined(HAVE_SLANG) && defined(OS2_NT)
2232 { "createcmdfile", 'S', POPT_ARG_STRING, NULL, 'S' },
2233 #endif
2234 { "color", 'c', POPT_ARG_NONE, NULL, 'c',
2235 N_("Request to run in color mode") },
2236 { "colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
2237 N_("Specifies a color configuration") },
2239 #ifdef USE_INTERNAL_EDIT
2240 { "edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2241 N_("Edits one file") },
2242 #endif
2244 { "help", 'h', POPT_ARG_NONE, NULL, 'h',
2245 N_("Displays this help message") },
2246 { "help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
2247 N_("Displays a help screen on how to change the color scheme") },
2248 #ifdef USE_NETCODE
2249 { "ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
2250 N_("Log ftp dialog to specified file") },
2251 #endif
2252 { "libdir", 'f', POPT_ARG_NONE, NULL, 'f' },
2253 { NULL, 'm', POPT_ARG_NONE, NULL, 'm',
2254 N_("Obsolete") },
2255 { "nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
2256 N_("Requests to run in black and white") },
2257 { "nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
2258 N_("Disable mouse support in text version") },
2259 #ifdef HAVE_SUBSHELL_SUPPORT
2260 { "nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2261 N_("Disables subshell support") },
2262 { "forceexec", 'r', POPT_ARG_NONE, &force_subshell_execution,0 },
2263 #endif
2264 { "printwd", 'P', POPT_ARG_NONE, &print_last_wd, 0,
2265 N_("Prints working directory at program exit") },
2266 { "resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
2267 N_("Resets soft keys on HP terminals") },
2268 { "slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
2269 N_("To run on slow terminals") },
2270 #ifndef OS2_NT
2271 { "stickchars", 'a', 0, &force_ugly_line_drawing, 0,
2272 N_("Use stickchars to draw") },
2273 #endif /* !OS2_NT */
2274 #ifdef HAVE_SUBSHELL_SUPPORT
2275 { "subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2276 N_("Enables subshell support (default)")},
2277 #endif
2278 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2279 { "termcap", 't', 0, &SLtt_Try_Termcap, 0,
2280 N_("Tries to use termcap instead of terminfo") },
2281 #endif
2282 { "version", 'V', POPT_ARG_NONE, NULL, 'V',
2283 N_("Displays the current version") },
2284 { "view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2285 N_("Launches the file viewer on a file") },
2286 { "xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
2287 N_("Forces xterm features") },
2289 { NULL, 0, 0, NULL, 0 }
2292 static void
2293 handle_args (int argc, char *argv [])
2295 char *tmp;
2296 poptContext ctx;
2297 char *option_arg, *base;
2298 int c;
2300 ctx = poptGetContext ("mc", argc, argv, argument_table,
2301 POPT_CONTEXT_NO_EXEC);
2303 #ifdef USE_TERMCAP
2304 SLtt_Try_Termcap = 1;
2305 #endif
2307 while ((c = poptGetNextOpt (ctx)) > 0){
2308 option_arg = poptGetOptArg (ctx);
2310 process_args (c, option_arg);
2313 if (c < -1){
2314 print_mc_usage (stderr);
2315 fprintf(stderr, "%s: %s\n",
2316 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2317 poptStrerror (c));
2318 finish_program = 1;
2320 probably_finish_program ();
2322 tmp = poptGetArg (ctx);
2325 * Check for special invocation names mcedit and mcview,
2326 * if none apply then set the current directory and the other
2327 * directory from the command line arguments
2329 base = x_basename (argv[0]);
2330 if (!STRNCOMP (base, "mce", 3) || !STRCOMP(base, "vi")) {
2331 edit_one_file = "";
2332 edit_one_file_start_line = 1;
2333 if (tmp) {
2334 if (*tmp == '+' && isdigit ((unsigned char) tmp[1])){
2335 int start_line = atoi (tmp);
2336 if (start_line > 0) {
2337 char *file = poptGetArg (ctx);
2338 if (file) {
2339 tmp = file;
2340 edit_one_file_start_line = start_line;
2344 edit_one_file = g_strdup (tmp);
2346 } else
2347 if (!STRNCOMP (base, "mcv", 3) || !STRCOMP(base, "view")) {
2348 if (tmp)
2349 view_one_file = g_strdup (tmp);
2350 else {
2351 fprintf (stderr, "No arguments given to the viewer\n");
2352 finish_program = 1;
2353 probably_finish_program ();
2355 } else {
2356 /* sets the current dir and the other dir */
2357 if (tmp) {
2358 this_dir = g_strdup (tmp);
2359 if ((tmp = poptGetArg (ctx)))
2360 other_dir = g_strdup (tmp);
2364 poptFreeContext (ctx);
2368 * The compatibility_move_mc_files routine is intended to
2369 * move all of the hidden .mc files into a private ~/.mc
2370 * directory in the home directory, to avoid cluttering the users.
2372 * Previous versions of the program had all of their files in
2373 * the $HOME, we are now putting them in $HOME/.mc
2375 #ifdef OS2_NT
2376 # define compatibility_move_mc_files()
2377 #else
2379 static int
2380 do_mc_filename_rename (char *mc_dir, char *o_name, char *n_name)
2382 char *full_o_name = concat_dir_and_file (home_dir, o_name);
2383 char *full_n_name = g_strconcat (home_dir, MC_BASE, n_name, NULL);
2384 int move;
2386 move = 0 == rename (full_o_name, full_n_name);
2387 g_free (full_o_name);
2388 g_free (full_n_name);
2389 return move;
2392 static void
2393 do_compatibility_move (char *mc_dir)
2395 struct stat s;
2396 int move;
2398 if (stat (mc_dir, &s) == 0)
2399 return;
2400 if (errno != ENOENT)
2401 return;
2403 if (mkdir (mc_dir, 0777) == -1)
2404 return;
2406 move = do_mc_filename_rename (mc_dir, ".mc.ini", "ini");
2407 move += do_mc_filename_rename (mc_dir, ".mc.hot", "hotlist");
2408 move += do_mc_filename_rename (mc_dir, ".mc.ext", "ext");
2409 move += do_mc_filename_rename (mc_dir, ".mc.menu", "menu");
2410 move += do_mc_filename_rename (mc_dir, ".mc.bashrc", "bashrc");
2411 move += do_mc_filename_rename (mc_dir, ".mc.inputrc", "inputrc");
2412 move += do_mc_filename_rename (mc_dir, ".mc.tcshrc", "tcshrc");
2413 move += do_mc_filename_rename (mc_dir, ".mc.tree", "tree");
2415 if (!move)
2416 return;
2418 show_change_notice = 1;
2421 static void
2422 compatibility_move_mc_files (void)
2424 char *mc_dir = concat_dir_and_file (home_dir, ".mc");
2426 do_compatibility_move (mc_dir);
2427 g_free (mc_dir);
2429 #endif /* OS2_NT */
2432 main (int argc, char *argv [])
2434 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2435 setlocale (LC_ALL, "");
2436 bindtextdomain ("mc", LOCALEDIR);
2437 textdomain ("mc");
2439 /* Initialize list of all user group for timur_clr_mode */
2440 init_groups ();
2442 OS_Setup ();
2444 /* This variable is used by the subshell */
2445 home_dir = getenv ("HOME");
2446 if (!home_dir) {
2447 /* mc_home was computed by OS_Setup */
2448 home_dir = mc_home;
2451 vfs_init ();
2453 #ifdef HAVE_SLANG
2454 SLtt_Ignore_Beep = 1;
2455 #endif
2457 /* NOTE: This has to be called before slang_init or whatever routine
2458 calls any define_sequence */
2459 init_key ();
2461 handle_args (argc, argv);
2463 /* Used to report the last working directory at program end */
2464 if (print_last_wd){
2465 #ifndef OS2_NT
2466 stdout_fd = dup (1);
2467 close (1);
2468 if (open (ttyname (0), O_RDWR) < 0)
2469 if (open ("/dev/tty", O_RDWR) < 0) {
2470 /* Try if stderr is not redirected as the last chance */
2471 char *p = g_strdup (ttyname (0));
2473 if (!strcmp (p, ttyname (2)))
2474 dup2 (2, 1);
2475 else {
2476 fprintf (stderr,
2477 _("Couldn't open tty line. You have to run mc without the -P flag.\n"
2478 "On some systems you may want to run # `which mc`\n"));
2479 exit (1);
2481 g_free (p);
2483 #endif
2486 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2487 handle_console (CONSOLE_INIT);
2489 # ifdef HAVE_SUBSHELL_SUPPORT
2490 subshell_get_console_attributes ();
2491 # endif
2493 /* Install the SIGCHLD handler; must be done before init_subshell() */
2494 init_sigchld ();
2496 compatibility_move_mc_files ();
2498 /* We need this, since ncurses endwin () doesn't restore the signals */
2499 save_stop_handler ();
2501 /* Must be done before init_subshell, to set up the terminal size: */
2502 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2503 #ifdef HAVE_SLANG
2504 slang_init ();
2505 #endif
2506 /* NOTE: This call has to be after slang_init. It's the small part from
2507 the previous init_key which had to be moved after the call of slang_init */
2508 init_key_input_fd ();
2510 load_setup ();
2512 init_curses ();
2514 init_xterm_support ();
2516 #ifdef HAVE_SUBSHELL_SUPPORT
2518 /* Don't use subshell when invoked as a viewer or editor */
2519 if (edit_one_file || view_one_file)
2520 use_subshell = 0;
2522 /* Done here to ensure that the subshell doesn't */
2523 /* inherit the file descriptors opened below, etc */
2524 if (use_subshell)
2525 init_subshell ();
2527 #endif /* HAVE_SUBSHELL_SUPPORT */
2529 /* Removing this from the X code let's us type C-c */
2530 load_key_defs ();
2532 /* Also done after init_subshell, to save any shell init file messages */
2533 if (console_flag)
2534 handle_console (CONSOLE_SAVE);
2536 if (alternate_plus_minus)
2537 application_keypad_mode ();
2539 if (show_change_notice){
2540 message (1, _(" Notice "),
2541 _(" The Midnight Commander configuration files \n"
2542 " are now stored in the ~/.mc directory, the \n"
2543 " files have been moved now\n"));
2546 #ifdef HAVE_SUBSHELL_SUPPORT
2547 if (use_subshell){
2548 prompt = strip_ctrl_codes (subshell_prompt);
2549 if (!prompt)
2550 prompt = "";
2551 } else
2552 #endif /* HAVE_SUBSHELL_SUPPORT */
2553 prompt = (geteuid () == 0) ? "# " : "$ ";
2555 /* Program main loop */
2556 do_nc ();
2558 /* Save the tree store */
2559 tree_store_save ();
2561 /* Virtual File System shutdown */
2562 vfs_shut ();
2564 /* Delete list of all user groups */
2565 destroy_groups ();
2567 flush_extension_file (); /* does only free memory */
2569 /* Miguel, maybe the fix in slang is not required and
2570 * it could be done by removing the slang_done_screen.
2571 * Do I need to call slang_reset_tty then?
2573 endwin ();
2574 slang_shutdown ();
2576 if (console_flag && !(quit & SUBSHELL_EXIT))
2577 restore_console ();
2578 if (alternate_plus_minus)
2579 numeric_keypad_mode ();
2581 #ifndef OS2_NT
2582 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2583 #endif
2585 if (console_flag)
2586 handle_console (CONSOLE_DONE);
2587 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2589 #ifdef _OS_NT
2590 /* On NT, home_dir is malloced */
2591 g_free (home_dir);
2592 #endif
2593 #if defined(OS2_NT)
2594 if (print_last_wd == 2){
2595 FILE *bat_file;
2597 print_last_wd = 0;
2598 bat_file = fopen(batch_file_name, "w");
2599 if (bat_file != NULL){
2600 /* .ado: \r\n for Win95 */
2601 fprintf(bat_file, "@echo off\r\n");
2602 if (isalpha(last_wd_string[0]))
2603 fprintf(bat_file, "%c:\r\n", last_wd_string[0]);
2604 fprintf(bat_file, "cd %s\r\n", last_wd_string);
2605 fclose(bat_file);
2608 #endif
2609 if (print_last_wd) {
2610 if (print_last_revert || edit_one_file || view_one_file)
2611 write (stdout_fd, ".", 1);
2612 else
2613 write (stdout_fd, last_wd_string, strlen (last_wd_string));
2614 g_free (last_wd_string);
2617 #ifndef _OS_NT
2618 g_free (mc_home);
2619 #endif /* (_OS_NT) */
2620 done_key ();
2621 #ifdef HAVE_CHARSET
2622 free_codepages_list ();
2623 #endif
2624 if (this_dir)
2625 g_free (this_dir);
2626 if (other_dir)
2627 g_free (other_dir);
2629 return 0;