* vfs.c (vfs_init) [!WITH_MCFS]: Don't register mcfs.
[midnight-commander.git] / src / main.c
blob8d4831b7ba991bd37581b6ed7ae728e0715e53df
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 void
322 try_to_select (WPanel *panel, char *name)
324 Xtry_to_select (panel, name);
325 select_item (panel);
326 display_mini_info (panel);
329 static void
330 reload_panelized (WPanel *panel)
332 int i, j;
333 dir_list *list = &panel->dir;
335 if (panel != cpanel)
336 mc_chdir (panel->cwd);
338 for (i = 0, j = 0; i < panel->count; i++){
339 if (list->list [i].f.marked) {
340 /* Unmark the file in advance. In case the following mc_lstat
341 * fails we are done, else we have to mark the file again
342 * (Note: do_file_mark depends on a valid "list->list [i].buf").
343 * IMO that's the best way to update the panel's summary status
344 * -- Norbert
346 do_file_mark (panel, i, 0);
348 if (mc_lstat (list->list [i].fname, &list->list [i].buf)){
349 g_free (list->list [i].fname);
350 continue;
352 if (list->list [i].f.marked)
353 do_file_mark (panel, i, 1);
354 if (j != i)
355 list->list [j] = list->list [i];
356 j++;
358 if (j == 0)
359 panel->count = set_zero_dir (list);
360 else
361 panel->count = j;
363 if (panel != cpanel)
364 mc_chdir (cpanel->cwd);
367 void
368 update_one_panel_widget (WPanel *panel, int force_update, char *current_file)
370 int free_pointer;
372 if (force_update & UP_RELOAD){
373 panel->is_panelized = 0;
375 #if 1
376 ftpfs_flushdir ();
377 #endif
378 /* FIXME: Should supply flushdir method */
379 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
382 /* If current_file == -1 (an invalid pointer) then preserve selection */
383 if (current_file == UP_KEEPSEL){
384 free_pointer = 1;
385 current_file = g_strdup (panel->dir.list [panel->selected].fname);
386 } else
387 free_pointer = 0;
389 if (panel->is_panelized)
390 reload_panelized (panel);
391 else
392 panel_reload (panel);
394 try_to_select (panel, current_file);
395 panel->dirty = 1;
397 if (free_pointer)
398 g_free (current_file);
401 void
402 panel_clean_dir (WPanel *panel)
404 int count = panel->count;
406 panel->count = 0;
407 panel->top_file = 0;
408 panel->selected = 0;
409 panel->marked = 0;
410 panel->dirs_marked = 0;
411 panel->total = 0;
412 panel->searching = 0;
413 panel->is_panelized = 0;
415 clean_dir (&panel->dir, count);
418 static void
419 update_one_panel (int which, int force_update, char *current_file)
421 WPanel *panel;
423 if (get_display_type (which) != view_listing)
424 return;
426 panel = (WPanel *) get_panel_widget (which);
427 update_one_panel_widget (panel, force_update, current_file);
430 /* This routine reloads the directory in both panels. It tries to
431 * select current_file in current_panel and other_file in other_panel.
432 * If current_file == -1 then it automatically sets current_file and
433 * other_file to the currently selected files in the panels.
435 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
436 * will not reload the other panel.
438 void
439 update_panels (int force_update, char *current_file)
441 int reload_other = !(force_update & UP_ONLY_CURRENT);
442 WPanel *panel;
444 update_one_panel (get_current_index (), force_update, current_file);
445 if (reload_other)
446 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
448 if (get_current_type () == view_listing)
449 panel = (WPanel *) get_panel_widget (get_current_index ());
450 else
451 panel = (WPanel *) get_panel_widget (get_other_index ());
453 mc_chdir (panel->cwd);
456 #ifdef WANT_PARSE
457 static void select_by_index (WPanel *panel, int i);
459 /* Called by parse_control_file */
460 static int index_by_name (file_entry *list, int count)
462 char *name;
463 int i;
465 name = strtok (NULL, " \t\n");
466 if (!name || !*name)
467 return -1;
468 for (i = 0; i < count; i++){
469 if (strcmp (name, list[i].fname) == 0)
470 return i;
472 return -1;
475 /* Called by parse_control_file */
476 static void select_by_index (WPanel *panel, int i)
478 if (i >= panel->count)
479 return;
481 unselect_item (panel);
482 panel->selected = i;
484 while (panel->selected - panel->top_file >= ITEMS (panel)){
485 /* Scroll window half screen */
486 panel->top_file += ITEMS (panel)/2;
487 paint_dir (panel);
488 select_item (panel);
490 while (panel->selected < panel->top_file){
491 /* Scroll window half screen */
492 panel->top_file -= ITEMS (panel)/2;
493 if (panel->top_file < 0) panel->top_file = 0;
494 paint_dir (panel);
496 select_item (panel);
499 /* Called by my_system
500 No error reporting, just exits on the first sign of trouble */
501 static void parse_control_file (void)
503 char *data, *current;
504 WPanel *panel;
505 file_entry *list;
506 int i;
507 FILE *file;
508 struct stat s;
510 if ((file = fopen (control_file, "r")) == NULL){
511 return;
513 /* Use of fstat prevents race conditions */
514 if (fstat (fileno (file), &s) != 0){
515 fclose (file);
516 return;
518 #ifndef OS2_NT
519 /* Security: Check that the user owns the control file and this file
520 is not group/world writable to prevent other users from playing tricks
521 on him/her. */
522 if (s.st_uid != getuid () || ((s.st_mode) & (S_IWGRP|S_IWOTH))) {
523 fclose (file);
524 return;
526 #endif /* !OS2_NT */
527 data = (char *) g_malloc (s.st_size+1);
528 if (!data){
529 fclose (file);
530 return;
532 if (s.st_size != fread (data, 1, s.st_size, file)){
533 g_free (data);
534 fclose (file);
535 return;
537 data [s.st_size] = 0;
538 fclose (file);
540 /* The Control file has now been loaded to memory -> start parsing. */
541 current = strtok (data, " \t\n");
542 while (current && *current){
543 if (isupper (*current)){
544 if (get_other_type () != view_listing)
545 break;
546 else
547 panel = other_panel;
548 } else
549 panel = cpanel;
551 list = panel->dir.list;
552 *current = tolower (*current);
554 if (strcmp (current, "clear_tags") == 0){
555 unmark_files (panel);
556 } else if (strcmp (current, "tag") == 0){
557 i = index_by_name (list, panel->count);
558 if (i >= 0) {
559 do_file_mark (panel, i, 1);
561 } else if (strcmp (current, "untag") == 0){
562 i = index_by_name (list, panel->count);
563 if (i >= 0){
564 do_file_mark (panel, i, 0);
566 } else if (strcmp (current, "select") == 0){
567 i = index_by_name (list, panel->count);
568 if (i >= 0){
569 select_by_index (panel, i);
571 } else if (strcmp (current, "change_panel") == 0){
572 change_panel ();
573 } else if (strcmp (current, "cd") == 0){
574 int change = 0;
575 current = strtok (NULL, " \t\n");
576 if (!current) break;
577 if (cpanel != panel){
578 change_panel ();
579 change = 1;
581 do_cd (current, cd_parse_command);
582 if (change)
583 change_panel ();
584 } else {
585 /* Unknown command -> let's give up */
586 break;
588 current = strtok (NULL, " \t\n");
591 g_free (data);
592 paint_panel (cpanel);
593 paint_panel (opanel);
595 #else
596 #define parse_control_file()
597 #endif /* WANT_PARSE */
599 /* Sets up the terminal before executing a program */
600 static void
601 pre_exec (void)
603 use_dash (0);
604 edition_pre_exec ();
607 /* Save current stat of directories to avoid reloading the panels */
608 /* when no modifications have taken place */
609 void
610 save_cwds_stat (void)
612 if (fast_reload){
613 mc_stat (cpanel->cwd, &(cpanel->dir_stat));
614 if (get_other_type () == view_listing)
615 mc_stat (opanel->cwd, &(opanel->dir_stat));
619 #ifdef HAVE_SUBSHELL_SUPPORT
620 void
621 do_possible_cd (char *new_dir)
623 if (!do_cd (new_dir, cd_exact))
624 message (1, _(" Warning "),
625 _(" The Commander can't change to the directory that \n"
626 " the subshell claims you are in. Perhaps you have \n"
627 " deleted your working directory, or given yourself \n"
628 " extra access permissions with the \"su\" command? "));
631 void
632 do_update_prompt (void)
634 if (update_prompt){
635 printf ("%s", subshell_prompt);
636 fflush (stdout);
637 update_prompt = 0;
640 #endif /* HAVE_SUBSHELL_SUPPORT */
642 void
643 restore_console (void)
645 handle_console (CONSOLE_RESTORE);
648 void
649 exec_shell (void)
651 do_execute (shell, 0, 0);
654 void
655 do_execute (const char *shell, const char *command, int flags)
657 #ifdef HAVE_SUBSHELL_SUPPORT
658 char *new_dir = NULL;
659 #endif /* HAVE_SUBSHELL_SUPPORT */
661 #ifdef USE_VFS
662 char *old_vfs_dir = 0;
664 if (!vfs_current_is_local ())
665 old_vfs_dir = g_strdup (vfs_get_current_dir ());
666 #endif /* USE_VFS */
668 save_cwds_stat ();
669 pre_exec ();
670 if (console_flag)
671 restore_console ();
673 unlink (control_file);
674 if (!use_subshell && !(flags & EXECUTE_INTERNAL && command)) {
675 printf ("%s%s%s\n", last_paused ? "\r\n":"", prompt, command);
676 last_paused = 0;
679 #ifdef HAVE_SUBSHELL_SUPPORT
680 if (use_subshell && !(flags & EXECUTE_INTERNAL)){
681 do_update_prompt ();
683 /* We don't care if it died, higher level takes care of this */
684 #ifdef USE_VFS
685 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
686 #else
687 invoke_subshell (command, VISIBLY, &new_dir);
688 #endif /* !USE_VFS */
689 } else
690 #endif /* HAVE_SUBSHELL_SUPPORT */
691 my_system (flags, shell, command);
693 if (!(flags & EXECUTE_INTERNAL)){
694 if ((pause_after_run == pause_always ||
695 (pause_after_run == pause_on_dumb_terminals &&
696 !xterm_flag && !console_flag)) && !quit){
697 printf (_("Press any key to continue..."));
698 last_paused = 1;
699 fflush (stdout);
700 mc_raw_mode ();
701 xgetch ();
703 if (console_flag){
704 if (output_lines && keybar_visible) {
705 putchar('\n');
706 fflush(stdout);
711 if (console_flag)
712 handle_console (CONSOLE_SAVE);
713 edition_post_exec ();
715 #ifdef HAVE_SUBSHELL_SUPPORT
716 if (new_dir)
717 do_possible_cd (new_dir);
719 #endif /* HAVE_SUBSHELL_SUPPORT */
721 #ifdef USE_VFS
722 if (old_vfs_dir){
723 mc_chdir (old_vfs_dir);
724 g_free (old_vfs_dir);
726 #endif /* USE_VFS */
728 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
730 parse_control_file ();
731 unlink (control_file);
732 do_refresh ();
733 use_dash (TRUE);
736 /* Executes a command */
737 void
738 shell_execute (char *command, int flags)
740 #ifdef HAVE_SUBSHELL_SUPPORT
741 if (use_subshell)
742 if (subshell_state == INACTIVE || force_subshell_execution)
743 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
744 else
745 message (1, MSG_ERROR, _(" The shell is already running a command "));
746 else
747 #endif /* HAVE_SUBSHELL_SUPPORT */
748 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
751 void
752 execute (char *command)
754 shell_execute (command, 0);
757 void
758 change_panel (void)
760 free_completions (input_w (cmdline));
761 dlg_one_down (midnight_dlg);
764 static int
765 quit_cmd_internal (int quiet)
767 int q = quit;
769 if (quiet || !confirm_exit){
770 q = 1;
771 } else {
772 if (query_dialog (_(" The Midnight Commander "),
773 _(" Do you really want to quit the Midnight Commander? "),
774 0, 2, _("&Yes"), _("&No")) == 0)
775 q = 1;
777 if (q){
778 #ifdef HAVE_SUBSHELL_SUPPORT
779 if (!use_subshell)
780 midnight_dlg->running = 0;
781 else
782 if ((q = exit_subshell ()))
783 #endif
784 midnight_dlg->running = 0;
786 if (q)
787 quit |= 1;
788 return quit;
791 int quit_cmd (void)
793 quit_cmd_internal (0);
794 return quit;
797 int quiet_quit_cmd (void)
799 print_last_revert = 1;
800 quit_cmd_internal (1);
801 return quit;
805 * Touch window and refresh window functions
808 /* This routine untouches the first line on both panels in order */
809 /* to avoid the refreshing the menu bar */
811 void
812 untouch_bar (void)
814 do_refresh ();
817 void
818 repaint_screen (void)
820 do_refresh ();
821 mc_refresh ();
824 /* Wrapper for do_subshell_chdir, check for availability of subshell */
825 void
826 subshell_chdir (char *directory)
828 #ifdef HAVE_SUBSHELL_SUPPORT
829 if (use_subshell){
830 if (vfs_current_is_local ())
831 do_subshell_chdir (directory, 0, 1);
833 #endif /* HAVE_SUBSHELL_SUPPORT */
836 void
837 directory_history_add (WPanel * panel, char *s)
839 if (!panel->dir_history) {
840 panel->dir_history = g_new0 (Hist, 1);
841 panel->dir_history->text = g_strdup (s);
842 strip_password (panel->dir_history->text, 1);
843 return;
845 if (!strcmp (panel->dir_history->text, s))
846 return;
847 if (panel->dir_history->next) {
848 if (panel->dir_history->next->text) {
849 g_free (panel->dir_history->next->text);
850 panel->dir_history->next->text = 0;
852 } else {
853 panel->dir_history->next = g_new0 (Hist, 1);
854 panel->dir_history->next->prev = panel->dir_history;
856 panel->dir_history = panel->dir_history->next;
857 panel->dir_history->text = g_strdup (s);
858 strip_password (panel->dir_history->text, 1);
860 panel_update_marks (panel);
864 * If we moved to the parent directory move the selection pointer to
865 * the old directory name; If we leave VFS dir, remove FS specificator.
866 * Warn: This code spoils lwd string.
868 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
870 static char*
871 get_parent_dir_name (char *cwd, char *lwd)
873 char *p;
874 if (strlen (lwd) > strlen (cwd))
875 if((p=strrchr(lwd, PATH_SEP)) && !strncmp(cwd, lwd, p-lwd)){
876 return (p+1);
878 return NULL;
881 /* Changes the current panel directory */
882 static int
883 _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
885 char *directory, *olddir;
886 char temp [MC_MAXPATHLEN];
887 char *translated_url;
888 #ifdef USE_VFS
889 vfs *oldvfs;
890 vfsid oldvfsid;
891 struct vfs_stamping *parent;
892 #endif
893 olddir = g_strdup (panel->cwd);
894 translated_url = new_dir = vfs_translate_url (new_dir);
896 /* Convert *new_path to a suitable pathname, handle ~user */
898 if (cd_type == cd_parse_command){
899 while (*new_dir == ' ')
900 new_dir++;
902 if (!strcmp (new_dir, "-")){
903 strcpy (temp, panel->lwd);
904 new_dir = temp;
907 directory = *new_dir ? new_dir : home_dir;
909 if (mc_chdir (directory) == -1){
910 strcpy (panel->cwd, olddir);
911 g_free (olddir);
912 g_free (translated_url);
913 return 0;
915 g_free (translated_url);
917 /* Success: save previous directory, shutdown status of previous dir */
918 strcpy (panel->lwd, olddir);
919 free_completions (input_w (cmdline));
921 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
923 #ifdef USE_VFS
924 oldvfs = vfs_type (olddir);
925 oldvfsid = vfs_ncs_getid (oldvfs, olddir, &parent);
926 vfs_add_noncurrent_stamps (oldvfs, oldvfsid, parent);
927 vfs_rm_parents (parent);
928 #endif
930 subshell_chdir (panel->cwd);
932 /* Reload current panel */
933 panel_clean_dir (panel);
934 panel->count = do_load_dir (&panel->dir, panel->sort_type,
935 panel->reverse, panel->case_sensitive, panel->filter);
936 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
937 load_hint ();
938 panel_update_contents (panel);
940 g_free (olddir);
942 return 1;
946 do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
948 int r;
950 r = _do_panel_cd (panel, new_dir, cd_type);
951 if (r)
952 directory_history_add (cpanel, cpanel->cwd);
953 return r;
957 do_cd (char *new_dir, enum cd_enum exact)
959 return (do_panel_cd (cpanel, new_dir, exact));
962 void
963 directory_history_next (WPanel * panel)
965 if (!panel->dir_history->next)
966 return;
967 if (_do_panel_cd (panel, panel->dir_history->next->text, cd_exact))
968 panel->dir_history = panel->dir_history->next;
969 panel_update_marks (panel);
972 void
973 directory_history_prev (WPanel * panel)
975 if (!panel->dir_history->prev)
976 return;
977 if (_do_panel_cd (panel, panel->dir_history->prev->text, cd_exact))
978 panel->dir_history = panel->dir_history->prev;
979 panel_update_marks (panel);
982 void
983 directory_history_list (WPanel * panel)
985 char *s;
986 /* must be at least two to show a history */
987 if (panel->dir_history) {
988 if (panel->dir_history->prev || panel->dir_history->next) {
989 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
990 if (s) {
991 int r;
992 r = _do_panel_cd (panel, s, cd_exact);
993 if (r)
994 directory_history_add (panel, panel->cwd);
995 g_free (s);
1001 #ifdef HAVE_SUBSHELL_SUPPORT
1003 load_prompt (int fd, void *unused)
1005 if (!read_subshell_prompt (QUIETLY))
1006 return 0;
1008 /* Don't actually change the prompt if it's invisible */
1009 if (current_dlg == midnight_dlg && command_prompt){
1010 int prompt_len;
1012 prompt = strip_ctrl_codes (subshell_prompt);
1013 prompt_len = strlen (prompt);
1015 /* Check for prompts too big */
1016 if (prompt_len > COLS - 8) {
1017 prompt [COLS - 8 ] = 0;
1018 prompt_len = COLS - 8;
1020 label_set_text (the_prompt, prompt);
1021 winput_set_origin ((WInput *)cmdline, prompt_len, COLS-prompt_len);
1023 /* since the prompt has changed, and we are called from one of the
1024 * get_event channels, the prompt updating does not take place
1025 * automatically: force a cursor update and a screen refresh
1027 update_cursor (midnight_dlg);
1028 mc_refresh ();
1030 update_prompt = 1;
1031 return 0;
1033 #endif /* HAVE_SUBSHELL_SUPPORT */
1035 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1037 maybe_cd (int char_code, int move_up_dir)
1039 if (navigate_with_arrows){
1040 if (!input_w (cmdline)->buffer [0]){
1041 if (!move_up_dir){
1042 do_cd ("..", cd_exact);
1043 return 1;
1045 if (S_ISDIR (selection (cpanel)->buf.st_mode)
1046 || link_isdir (selection (cpanel))){
1047 do_cd (selection (cpanel)->fname, cd_exact);
1048 return 1;
1052 return 0;
1055 void
1056 sort_cmd (void)
1058 WPanel *p;
1059 sortfn *sort_order;
1061 if (!SELECTED_IS_PANEL)
1062 return;
1064 p = MENU_PANEL;
1065 sort_order = sort_box (p->sort_type, &p->reverse, &p->case_sensitive);
1067 panel_set_sort_order (p, sort_order);
1071 static void
1072 treebox_cmd (void)
1074 char *sel_dir;
1076 sel_dir = tree_box (selection (cpanel)->fname);
1077 if (sel_dir){
1078 do_cd(sel_dir, cd_exact);
1079 g_free (sel_dir);
1083 #ifdef LISTMODE_EDITOR
1084 static void
1085 listmode_cmd (void)
1087 char *newmode;
1088 newmode = listmode_edit ("half <type,>name,|,size:8,|,perm:4+");
1089 message (0, _(" Listing format edit "), _(" New mode is \"%s\" "), newmode);
1090 g_free (newmode);
1092 #endif /* LISTMODE_EDITOR */
1094 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
1095 static menu_entry PanelMenu [] = {
1096 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
1097 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
1098 { ' ', N_("&Info C-x i"), 'I', info_cmd },
1099 { ' ', N_("&Tree"), 'T', tree_cmd },
1100 { ' ', "", ' ', 0 },
1101 { ' ', N_("&Sort order..."), 'S', sort_cmd },
1102 { ' ', "", ' ', 0 },
1103 { ' ', N_("&Filter..."), 'F', filter_cmd },
1104 #ifdef USE_NETCODE
1105 { ' ', "", ' ', 0 },
1106 #ifdef WITH_MCFS
1107 { ' ', N_("&Network link..."), 'N', netlink_cmd },
1108 #endif
1109 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
1110 #ifdef WITH_SMBFS
1111 { ' ', N_("SM&B link..."), 'B', smblink_cmd },
1112 #endif
1113 #endif
1114 { ' ', "", ' ', 0 },
1115 #ifdef OS2_NT
1116 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_a },
1117 #endif
1118 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
1121 static menu_entry RightMenu [] = {
1122 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
1123 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
1124 { ' ', N_("&Info C-x i"), 'I', info_cmd },
1125 { ' ', N_("&Tree"), 'T', tree_cmd },
1126 { ' ', "", ' ', 0 },
1127 { ' ', N_("&Sort order..."), 'S', sort_cmd },
1128 { ' ', "", ' ', 0 },
1129 { ' ', N_("&Filter..."), 'F', filter_cmd },
1130 #ifdef USE_NETCODE
1131 { ' ', "", ' ', 0 },
1132 #ifdef WITH_MCFS
1133 { ' ', N_("&Network link..."), 'N', netlink_cmd },
1134 #endif
1135 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
1136 #ifdef WITH_SMBFS
1137 { ' ', N_("SM&B link..."), 'B', smblink_cmd },
1138 #endif
1139 #endif
1140 { ' ', "", ' ', 0 },
1141 #ifdef OS2_NT
1142 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_b },
1143 #endif
1144 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
1147 static menu_entry FileMenu [] = {
1148 { ' ', N_("&User menu F2"), 'U', user_file_menu_cmd },
1149 { ' ', N_("&View F3"), 'V', view_cmd },
1150 { ' ', N_("Vie&w file... "), 'W', view_file_cmd },
1151 { ' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd },
1152 { ' ', N_("&Edit F4"), 'E', edit_cmd },
1153 { ' ', N_("&Copy F5"), 'C', copy_cmd },
1154 { ' ', N_("c&Hmod C-x c"), 'H', chmod_cmd },
1155 #ifndef OS2_NT
1156 { ' ', N_("&Link C-x l"), 'L', link_cmd },
1157 { ' ', N_("&SymLink C-x s"), 'S', symlink_cmd },
1158 { ' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd },
1159 { ' ', N_("ch&Own C-x o"), 'O', chown_cmd },
1160 { ' ', N_("&Advanced chown "), 'A', chown_advanced_cmd },
1161 #endif
1162 { ' ', N_("&Rename/Move F6"), 'R', ren_cmd },
1163 { ' ', N_("&Mkdir F7"), 'M', mkdir_cmd },
1164 { ' ', N_("&Delete F8"), 'D', delete_cmd },
1165 { ' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd },
1166 { ' ', "", ' ', 0 },
1167 { ' ', N_("select &Group M-+"), 'G', select_cmd },
1168 { ' ', N_("u&Nselect group M-\\"),'N', unselect_cmd },
1169 { ' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd },
1170 { ' ', "", ' ', 0 },
1171 { ' ', N_("e&Xit F10"), 'X', (callfn) quit_cmd }
1174 static menu_entry CmdMenu [] = {
1175 /* I know, I'm lazy, but the tree widget when it's not running
1176 * as a panel still has some problems, I have not yet finished
1177 * the WTree widget port, sorry.
1179 { ' ', N_("&Directory tree"), 'D', treebox_cmd },
1180 { ' ', N_("&Find file M-?"), 'F', find_cmd },
1181 { ' ', N_("s&Wap panels C-u"), 'W', swap_cmd },
1182 { ' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd },
1183 { ' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd },
1184 { ' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize },
1185 { ' ', N_("show directory s&Izes"), 'I', dirsizes_cmd },
1186 { ' ', "", ' ', 0 },
1187 { ' ', N_("command &History"), 'H', history_cmd },
1188 { ' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd },
1189 #ifdef USE_VFS
1190 { ' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs },
1191 { ' ', N_("Fr&ee VFSs now"), 'E', free_vfs_now },
1192 #endif
1193 #ifdef WITH_BACKGROUND
1194 { ' ', N_("&Background jobs C-x j"), 'B', jobs_cmd },
1195 #endif
1196 { ' ', "", ' ', 0 },
1197 #ifdef USE_EXT2FSLIB
1198 { ' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd },
1199 #endif
1200 #ifdef LISTMODE_EDITOR
1201 { ' ', N_("&Listing format edit"), 'L', listmode_cmd},
1202 #endif
1203 #if defined (USE_EXT2FSLIB) || defined (LISTMODE_EDITOR)
1204 { ' ', "", ' ', 0 },
1205 #endif
1206 { ' ', N_("&Extension file edit"), 'E', ext_cmd },
1207 { ' ', N_("&Menu file edit"), 'M', menu_edit_cmd },
1208 #ifdef USE_INTERNAL_EDIT
1209 {' ', N_("Menu edi&Tor edit"), 'T', edit_user_menu_cmd},
1210 {' ', N_("&Syntax file edit"), 'S', edit_syntax_cmd}
1211 #endif /* USE_INTERNAL_EDIT */
1214 /* Must keep in sync with the constants in menu_cmd */
1215 static menu_entry OptMenu [] = {
1216 { ' ', N_("&Configuration..."), 'C', configure_box },
1217 { ' ', N_("&Layout..."), 'L', layout_cmd },
1218 { ' ', N_("c&Onfirmation..."), 'O', confirm_box },
1219 { ' ', N_("&Display bits..."), 'D', display_bits_box },
1220 #ifndef OS2_NT
1221 { ' ', N_("learn &Keys..."), 'K', learn_keys },
1222 #endif /* !OS2_NT */
1223 #ifdef USE_VFS
1224 { ' ', N_("&Virtual FS..."), 'V', configure_vfs },
1225 #endif /* !USE_VFS */
1226 { ' ', "", ' ', 0 },
1227 { ' ', N_("&Save setup"), 'S', save_setup_cmd }
1230 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
1232 static Menu MenuBar [5];
1234 void
1235 init_menu (void)
1237 MenuBar [0] = create_menu ( horizontal_split ? _(" &Above ") : _(" &Left "),
1238 PanelMenu, menu_entries (PanelMenu));
1239 MenuBar [1] = create_menu (_(" &File "), FileMenu, menu_entries (FileMenu));
1240 MenuBar [2] = create_menu (_(" &Command "), CmdMenu, menu_entries (CmdMenu));
1241 MenuBar [3] = create_menu (_(" &Options "), OptMenu, menu_entries (OptMenu));
1242 MenuBar [4] = create_menu (horizontal_split ? _(" &Below ") : _(" &Right "),
1243 RightMenu, menu_entries (PanelMenu));
1246 void
1247 done_menu (void)
1249 int i;
1251 for (i = 0; i < 5; i++){
1252 destroy_menu (MenuBar [i]);
1256 static void
1257 menu_last_selected_cmd (void)
1259 the_menubar->active = 1;
1260 the_menubar->dropped = drop_menus;
1261 the_menubar->previous_selection = dlg_item_number (midnight_dlg);
1262 dlg_select_widget (midnight_dlg, the_menubar);
1265 static void
1266 menu_cmd (void)
1268 if (the_menubar->active)
1269 return;
1271 if (get_current_index () == 0)
1272 the_menubar->selected = 0;
1273 else
1274 the_menubar->selected = 4;
1275 menu_last_selected_cmd ();
1278 /* Flag toggling functions */
1279 void
1280 toggle_fast_reload (void)
1282 fast_reload = !fast_reload;
1283 if (fast_reload_w == 0 && fast_reload){
1284 message (0, _(" Information "),
1285 _(" Using the fast reload option may not reflect the exact \n"
1286 " directory contents. In this cases you'll need to do a \n"
1287 " manual reload of the directory. See the man page for \n"
1288 " the details. "));
1289 fast_reload_w = 1;
1293 void
1294 toggle_mix_all_files (void)
1296 mix_all_files = !mix_all_files;
1297 update_panels (UP_RELOAD, UP_KEEPSEL);
1300 void
1301 toggle_show_backup (void)
1303 show_backups = !show_backups;
1304 update_panels (UP_RELOAD, UP_KEEPSEL);
1307 void
1308 toggle_show_hidden (void)
1310 show_dot_files = !show_dot_files;
1311 update_panels (UP_RELOAD, UP_KEEPSEL);
1315 * Just a hack for allowing url-like pathnames to be accepted from the
1316 * command line.
1318 static void
1319 translated_mc_chdir (char *dir)
1321 char *newdir;
1323 newdir = vfs_translate_url (dir);
1324 mc_chdir (newdir);
1325 g_free (newdir);
1328 void
1329 create_panels (void)
1331 int current_index;
1332 int other_index;
1333 int current_mode;
1334 int other_mode;
1335 char original_dir [1024];
1337 original_dir [0] = 0;
1339 if (boot_current_is_left){
1340 current_index = 0;
1341 other_index = 1;
1342 current_mode = startup_left_mode;
1343 other_mode = startup_right_mode;
1344 } else {
1345 current_index = 1;
1346 other_index = 0;
1347 current_mode = startup_right_mode;
1348 other_mode = startup_left_mode;
1350 /* Creates the left panel */
1351 if (this_dir){
1352 if (other_dir){
1353 /* Ok, user has specified two dirs, save the original one,
1354 * since we may not be able to chdir to the proper
1355 * second directory later
1357 mc_get_current_wd (original_dir, sizeof (original_dir)-2);
1359 translated_mc_chdir (this_dir);
1361 set_display_type (current_index, current_mode);
1363 /* The other panel */
1364 if (other_dir){
1365 if (original_dir [0])
1366 translated_mc_chdir (original_dir);
1367 translated_mc_chdir (other_dir);
1369 set_display_type (other_index, other_mode);
1371 if (startup_left_mode == view_listing){
1372 current_panel = left_panel;
1373 } else {
1374 if (right_panel)
1375 current_panel = right_panel;
1376 else
1377 current_panel = left_panel;
1380 /* Create the nice widgets */
1381 cmdline = command_new (0, 0, 0);
1382 the_prompt = label_new (0, 0, prompt, NULL);
1383 the_prompt->transparent = 1;
1384 the_bar = buttonbar_new (keybar_visible);
1386 the_hint = label_new (0, 0, 0, NULL);
1387 the_hint->transparent = 1;
1388 the_hint->auto_adjust_cols = 0;
1389 the_hint->widget.cols = COLS;
1391 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1394 static void copy_current_pathname (void)
1396 if (!command_prompt)
1397 return;
1399 stuff (input_w (cmdline), cpanel->cwd, 0);
1400 if (cpanel->cwd [strlen (cpanel->cwd) - 1] != PATH_SEP)
1401 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1404 static void copy_other_pathname (void)
1406 if (get_other_type () != view_listing)
1407 return;
1409 if (!command_prompt)
1410 return;
1412 stuff (input_w (cmdline), opanel->cwd, 0);
1413 if (cpanel->cwd [strlen (opanel->cwd) - 1] != PATH_SEP)
1414 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1417 static void copy_readlink (WPanel *panel)
1419 if (!command_prompt)
1420 return;
1421 if (S_ISLNK (selection (panel)->buf.st_mode)) {
1422 char buffer [MC_MAXPATHLEN];
1423 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1424 int i;
1426 i = mc_readlink (p, buffer, MC_MAXPATHLEN);
1427 g_free (p);
1428 if (i > 0) {
1429 buffer [i] = 0;
1430 stuff (input_w (cmdline), buffer, 0);
1435 static void copy_current_readlink (void)
1437 copy_readlink (cpanel);
1440 static void copy_other_readlink (void)
1442 if (get_other_type () != view_listing)
1443 return;
1444 copy_readlink (opanel);
1447 /* Inserts the selected file name into the input line */
1448 /* Exported so that the command modules uses it */
1449 void copy_prog_name (void)
1451 char *tmp;
1452 if (!command_prompt)
1453 return;
1455 if (get_current_type () == view_tree){
1456 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1457 tmp = name_quote (tree->selected_ptr->name, 1);
1458 } else
1459 tmp = name_quote (selection (cpanel)->fname, 1);
1460 stuff (input_w (cmdline), tmp, 1);
1461 g_free (tmp);
1464 static void copy_tagged (WPanel *panel)
1466 int i;
1468 if (!command_prompt)
1469 return;
1470 input_disable_update (input_w (cmdline));
1471 if (panel->marked){
1472 for (i = 0; i < panel->count; i++)
1473 if (panel->dir.list [i].f.marked) {
1474 char *tmp = name_quote (panel->dir.list [i].fname, 1);
1475 stuff (input_w (cmdline), tmp, 1);
1476 g_free (tmp);
1478 } else {
1479 char *tmp = name_quote (panel->dir.list [panel->selected].fname, 1);
1480 stuff (input_w (cmdline), tmp, 1);
1481 g_free (tmp);
1483 input_enable_update (input_w (cmdline));
1486 static void copy_current_tagged (void)
1488 copy_tagged (cpanel);
1491 static void copy_other_tagged (void)
1493 if (get_other_type () != view_listing)
1494 return;
1495 copy_tagged (opanel);
1498 static void do_suspend_cmd (void)
1500 pre_exec ();
1502 if (console_flag && !use_subshell)
1503 restore_console ();
1505 #ifdef SIGTSTP
1507 struct sigaction sigtstp_action;
1509 /* Make sure that the SIGTSTP below will suspend us directly,
1510 without calling ncurses' SIGTSTP handler; we *don't* want
1511 ncurses to redraw the screen immediately after the SIGCONT */
1512 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
1514 kill (getpid (), SIGTSTP);
1516 /* Restore previous SIGTSTP action */
1517 sigaction (SIGTSTP, &sigtstp_action, NULL);
1519 #endif /* SIGTSTP */
1521 if (console_flag && !use_subshell)
1522 handle_console (CONSOLE_SAVE);
1524 edition_post_exec ();
1527 void
1528 suspend_cmd (void)
1530 save_cwds_stat ();
1531 do_suspend_cmd ();
1532 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1533 do_refresh ();
1536 static void
1537 init_labels (Widget *paneletc)
1539 define_label (midnight_dlg, paneletc, 1, _("Help"), help_cmd);
1540 define_label (midnight_dlg, paneletc, 2, _("Menu"), user_file_menu_cmd);
1541 define_label (midnight_dlg, paneletc, 9, _("PullDn"), menu_cmd);
1542 define_label (midnight_dlg, paneletc, 10, _("Quit"), (voidfn) quit_cmd);
1545 static const key_map ctl_x_map [] = {
1546 { XCTRL('c'), (callfn) quit_cmd },
1547 { 'd', compare_dirs_cmd },
1548 #ifdef USE_VFS
1549 { 'a', reselect_vfs },
1550 #endif /* USE_VFS */
1551 { 'p', copy_current_pathname },
1552 { XCTRL('p'), copy_other_pathname },
1553 { 't', copy_current_tagged },
1554 { XCTRL('t'), copy_other_tagged },
1555 { 'c', chmod_cmd },
1556 #ifndef OS2_NT
1557 { 'o', chown_cmd },
1558 { 'r', copy_current_readlink },
1559 { XCTRL('r'), copy_other_readlink },
1560 { 'l', link_cmd },
1561 { XCTRL('l'), other_symlink_cmd },
1562 { 's', symlink_cmd },
1563 { XCTRL('s'), edit_symlink_cmd },
1564 #endif /* !OS2_NT */
1565 { 'i', info_cmd_no_menu },
1566 { 'q', quick_cmd_no_menu },
1567 { 'h', add2hotlist_cmd },
1568 { '!', external_panelize },
1569 #ifdef WITH_BACKGROUND
1570 { 'j', jobs_cmd },
1571 #endif /* WITH_BACKGROUND */
1572 #ifdef HAVE_SETSOCKOPT
1573 { '%', source_routing },
1574 #endif /* HAVE_SETSOCKOPT */
1575 { 0, 0 }
1578 static int ctl_x_map_enabled = 0;
1580 static void ctl_x_cmd (int ignore)
1582 ctl_x_map_enabled = 1;
1585 static void nothing (void)
1589 static const key_map default_map [] = {
1590 { KEY_F(19), menu_last_selected_cmd },
1591 { KEY_F(20), (key_callback) quiet_quit_cmd },
1593 /* Copy useful information to the command line */
1594 { ALT('\n'), copy_prog_name },
1595 { ALT('\r'), copy_prog_name },
1596 { ALT('a'), copy_current_pathname },
1597 { ALT('A'), copy_other_pathname },
1599 { ALT('c'), quick_cd_cmd },
1601 /* To access the directory hotlist */
1602 { XCTRL('\\'), quick_chdir_cmd },
1604 /* Suspend */
1605 { XCTRL('z'), suspend_cmd },
1606 /* The filtered view command */
1607 { ALT('!'), filtered_view_cmd_cpanel },
1609 /* Find file */
1610 { ALT('?'), find_cmd },
1612 /* Panel refresh */
1613 { XCTRL('r'), reread_cmd },
1615 /* Toggle listing between long, user defined and full formats */
1616 { ALT('t'), toggle_listing_cmd },
1618 /* Swap panels */
1619 { XCTRL('u'), swap_cmd },
1621 /* View output */
1622 { XCTRL('o'), view_other_cmd },
1624 /* Control-X keybindings */
1625 { XCTRL('x'), ctl_x_cmd },
1627 /* Trap dlg's exit commands */
1628 { ESC_CHAR, nothing },
1629 { XCTRL('c'), nothing },
1630 { XCTRL('g'), nothing },
1631 { 0, 0 },
1634 static void setup_sigwinch (void)
1636 #if (defined(HAVE_SLANG) || (NCURSES_VERSION_MAJOR >= 4)) && \
1637 !defined(OS2_NT) && defined(SIGWINCH)
1638 struct sigaction act, oact;
1639 act.sa_handler = flag_winch;
1640 sigemptyset (&act.sa_mask);
1641 act.sa_flags = 0;
1642 # ifdef SA_RESTART
1643 act.sa_flags |= SA_RESTART;
1644 # endif
1645 sigaction (SIGWINCH, &act, &oact);
1646 #endif
1649 static void
1650 setup_pre (void)
1652 /* Call all the inits */
1653 #ifdef HAVE_CHARSET
1655 * Don't restrict the output on the screen manager level,
1656 * the translation tables take care of it.
1658 #define full_eight_bits (1)
1659 #define eight_bit_clean (1)
1660 #endif /* !HAVE_CHARSET */
1662 #ifndef HAVE_SLANG
1663 meta (stdscr, eight_bit_clean);
1664 #else
1665 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1666 #endif
1669 static void
1670 init_xterm_support (void)
1672 char *termvalue;
1673 #ifdef HAVE_SLANG
1674 char *term_entry;
1675 #endif
1677 termvalue = getenv ("TERM");
1678 if (!termvalue || !(*termvalue)){
1679 fprintf (stderr, _("The TERM environment variable is unset!\n"));
1680 exit (1);
1683 /* Check mouse capabilities */
1684 #ifdef HAVE_SLANG
1685 term_entry = SLtt_tigetent (termvalue);
1686 xmouse_seq = SLtt_tigetstr ("Km", &term_entry);
1687 #else
1688 xmouse_seq = tigetstr ("kmous");
1689 #endif
1691 /* -1 means invalid capability, shouldn't happen, but let's make it 0 */
1692 if ((long) xmouse_seq == -1) {
1693 xmouse_seq = NULL;
1696 if (force_xterm
1697 || strncmp (termvalue, "xterm", 5) == 0
1698 || strncmp (termvalue, "rxvt", 4) == 0
1699 || strcmp (termvalue, "dtterm") == 0) {
1700 xterm_flag = 1;
1702 /* Default to the standard xterm sequence */
1703 if (!xmouse_seq) {
1704 xmouse_seq = ESC_STR "[M";
1707 /* Enable mouse unless explicitly disabled by --nomouse */
1708 if (use_mouse_p != MOUSE_DISABLED) {
1709 use_mouse_p = MOUSE_XTERM;
1712 #if 0 /* It works on xterm, but not on rxvt */
1713 printf (ESC_STR "]0;GNU Midnight Commander\7");
1714 #endif
1718 static void setup_mc (void)
1720 setup_pre ();
1721 init_menu ();
1722 create_panels ();
1723 setup_panels ();
1725 #ifdef HAVE_SUBSHELL_SUPPORT
1726 if (use_subshell)
1727 add_select_channel (subshell_pty, load_prompt, 0);
1728 #endif /* !HAVE_SUBSHELL_SUPPORT */
1730 setup_sigwinch ();
1732 if (baudrate () < 9600 || slow_terminal){
1733 verbose = 0;
1735 init_mouse ();
1737 midnight_colors [0] = 0;
1738 midnight_colors [1] = REVERSE_COLOR; /* FOCUSC */
1739 midnight_colors [2] = INPUT_COLOR; /* HOT_NORMALC */
1740 midnight_colors [3] = NORMAL_COLOR; /* HOT_FOCUSC */
1743 static void setup_dummy_mc (const char *file)
1745 char d[MC_MAXPATHLEN];
1747 mc_get_current_wd (d, MC_MAXPATHLEN);
1748 setup_mc ();
1749 mc_chdir (d);
1751 /* Create a fake current_panel, this is needed because the
1752 * expand_format routine will use current panel.
1754 strcpy (cpanel->cwd, d);
1755 cpanel->selected = 0;
1756 cpanel->count = 1;
1757 cpanel->dir.list[0].fname = (char *) file;
1760 static void done_mc (void)
1762 done_menu ();
1764 /* Setup shutdown
1766 * We sync the profiles since the hotlist may have changed, while
1767 * we only change the setup data if we have the auto save feature set
1769 if (auto_save_setup)
1770 save_setup (); /* does also call save_hotlist */
1771 else
1772 save_hotlist();
1773 done_screen ();
1774 vfs_add_current_stamps ();
1775 if (xterm_flag && xterm_hintbar)
1776 set_hintbar(_("Thank you for using GNU Midnight Commander"));
1779 /* This should be called after destroy_dlg since panel widgets
1780 * save their state on the profiles
1782 static void done_mc_profile (void)
1784 if (!auto_save_setup)
1785 profile_forget_profile (profile_name);
1786 sync_profiles ();
1787 done_setup ();
1788 free_profiles ();
1791 /* This routine only handles cpanel, and opanel, it is easy to
1792 * change to use npanels, just loop over the number of panels
1793 * and use get_panel_widget (i) and make the test done below
1795 static void
1796 make_panels_dirty (void)
1798 if (cpanel->dirty)
1799 panel_update_contents (cpanel);
1801 if ((get_other_type () == view_listing) && opanel->dirty)
1802 panel_update_contents (opanel);
1805 /* In OS/2 and Windows NT people want to actually type the '\' key frequently */
1806 #ifdef OS2_NT
1807 # define check_key_backslash(x) 0
1808 #else
1809 # define check_key_backslash(x) ((x) == '\\')
1810 #endif
1813 midnight_callback (struct Dlg_head *h, int id, int msg)
1815 int i;
1816 static int first_pre_event = 1;
1818 switch (msg){
1820 case DLG_PRE_EVENT:
1821 make_panels_dirty ();
1822 if (auto_menu && first_pre_event) {
1823 user_file_menu_cmd ();
1825 first_pre_event = 0;
1826 return MSG_HANDLED;
1828 case DLG_KEY:
1829 if (ctl_x_map_enabled){
1830 ctl_x_map_enabled = 0;
1831 for (i = 0; ctl_x_map [i].key_code; i++)
1832 if (id == ctl_x_map [i].key_code){
1833 (*ctl_x_map [i].fn)(id);
1834 return MSG_HANDLED;
1838 /* FIXME: should handle all menu shortcuts before this point */
1839 if (the_menubar->active)
1840 break;
1842 if (id == KEY_F(10)){
1843 quit_cmd ();
1844 return MSG_HANDLED;
1847 if (id == '\t')
1848 free_completions (input_w (cmdline));
1850 /* On Linux, we can tell the difference */
1851 if (id == '\n' && ctrl_pressed ()){
1852 copy_prog_name ();
1853 return MSG_HANDLED;
1856 if (id == '\n' && input_w (cmdline)->buffer [0]){
1857 send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1858 return MSG_HANDLED;
1861 if ((!alternate_plus_minus || !(console_flag || xterm_flag)) &&
1862 !quote && !cpanel->searching) {
1863 if(!only_leading_plus_minus) {
1864 /* Special treatement, since the input line will eat them */
1865 if (id == '+' ) {
1866 select_cmd ();
1867 return MSG_HANDLED;
1870 if (check_key_backslash (id) || id == '-'){
1871 unselect_cmd ();
1872 return MSG_HANDLED;
1875 if (id == '*') {
1876 reverse_selection_cmd ();
1877 return MSG_HANDLED;
1879 } else if (command_prompt && !strlen (input_w (cmdline)->buffer)) {
1880 /* Special treatement '+', '-', '\', '*' only when this is
1881 * first char on input line
1884 if (id == '+') {
1885 select_cmd ();
1886 return MSG_HANDLED;
1889 if (check_key_backslash (id) || id == '-') {
1890 unselect_cmd ();
1891 return MSG_HANDLED;
1894 if (id == '*') {
1895 reverse_selection_cmd ();
1896 return MSG_HANDLED;
1900 break;
1902 case DLG_HOTKEY_HANDLED:
1903 if (get_current_type () == view_listing)
1904 cpanel->searching = 0;
1905 break;
1907 case DLG_UNHANDLED_KEY:
1908 if (command_prompt){
1909 int v;
1911 v = send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1912 if (v)
1913 return v;
1915 if (ctl_x_map_enabled) {
1916 ctl_x_map_enabled = 0;
1917 for (i = 0; ctl_x_map [i].key_code; i++)
1918 if (id == ctl_x_map [i].key_code){
1919 (*ctl_x_map [i].fn)(id);
1920 return MSG_HANDLED;
1922 } else
1924 for (i = 0; default_map [i].key_code; i++){
1925 if (id == default_map [i].key_code){
1926 (*default_map [i].fn)(id);
1927 return MSG_HANDLED;
1931 return MSG_NOT_HANDLED;
1933 /* We handle the special case of the output lines */
1934 case DLG_DRAW:
1935 attrset (SELECTED_COLOR);
1936 if (console_flag && output_lines)
1937 show_console_contents (output_start_y,
1938 LINES-output_lines-keybar_visible-1,
1939 LINES-keybar_visible-1);
1940 break;
1943 return default_dlg_callback (h, id, msg);
1946 #define xtoolkit_panel_setup()
1948 void load_hint (void)
1950 char *hint;
1952 if (!the_hint->widget.parent)
1953 return;
1955 if (!message_visible && (!xterm_flag || !xterm_hintbar)){
1956 label_set_text (the_hint, 0);
1957 return;
1960 if ((hint = get_random_hint ())){
1961 if (*hint)
1962 set_hintbar (hint);
1963 g_free (hint);
1964 } else {
1965 set_hintbar ("The Midnight Commander " VERSION
1966 " (C) 1995-1997 the Free Software Foundation");
1970 static void
1971 setup_panels_and_run_mc (void)
1973 int first, second;
1975 xtoolkit_panel_setup ();
1976 add_widget (midnight_dlg, the_hint);
1977 load_hint ();
1978 add_widget (midnight_dlg, cmdline);
1979 add_widget (midnight_dlg, the_prompt);
1980 add_widget (midnight_dlg, the_bar);
1982 if (boot_current_is_left){
1983 first = 1;
1984 second = 0;
1985 } else {
1986 first = 0;
1987 second = 1;
1989 add_widget (midnight_dlg, get_panel_widget (first));
1990 add_widget (midnight_dlg, get_panel_widget (second));
1991 add_widget (midnight_dlg, the_menubar);
1993 init_labels (get_panel_widget (0));
1994 init_labels (get_panel_widget (1));
1996 /* Run the Midnight Commander if no file was specified in the command line */
1997 run_dlg (midnight_dlg);
2000 /* result must be free'd (I think this should go in util.c) */
2001 static char *
2002 prepend_cwd_on_local (char *filename)
2004 char *d;
2005 int l;
2007 if (vfs_file_is_local (filename)){
2008 if (*filename == PATH_SEP) /* an absolute pathname */
2009 return g_strdup (filename);
2010 d = g_malloc (MC_MAXPATHLEN + strlen (filename) + 2);
2011 mc_get_current_wd (d, MC_MAXPATHLEN);
2012 l = strlen(d);
2013 d[l++] = PATH_SEP;
2014 strcpy (d + l, filename);
2015 return canonicalize_pathname (d);
2016 } else
2017 return g_strdup (filename);
2020 static int
2021 mc_maybe_editor_or_viewer (void)
2023 char *path = NULL;
2025 if (!(view_one_file || edit_one_file))
2026 return 0;
2028 /* Invoke the internal view/edit routine with:
2029 * the default processing and forcing the internal viewer/editor
2031 if (view_one_file) {
2032 path = prepend_cwd_on_local (view_one_file);
2033 setup_dummy_mc (path);
2034 view_file (path, 0, 1);
2036 #ifdef USE_INTERNAL_EDIT
2037 else {
2038 path = prepend_cwd_on_local ("");
2039 setup_dummy_mc (path);
2040 edit (edit_one_file, edit_one_file_start_line);
2042 #endif /* USE_INTERNAL_EDIT */
2043 g_free (path);
2044 midnight_shutdown = 1;
2045 done_mc ();
2046 return 1;
2049 static void
2050 do_nc (void)
2052 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback, "[main]", "midnight", 0);
2053 midnight_dlg->has_menubar = 1;
2055 /* Check if we were invoked as an editor or file viewer */
2056 if (mc_maybe_editor_or_viewer ())
2057 return;
2059 setup_mc ();
2061 setup_panels_and_run_mc ();
2063 /* Program end */
2064 midnight_shutdown = 1;
2066 /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */
2067 if (print_last_wd) {
2068 if (!vfs_current_is_local ())
2069 last_wd_string = g_strdup (".");
2070 else
2071 last_wd_string = g_strdup (cpanel->cwd);
2073 done_mc ();
2075 destroy_dlg (midnight_dlg);
2076 current_panel = 0;
2077 done_mc_profile ();
2080 #if defined (_OS_NT)
2081 /* Windows NT code */
2082 #define CONTROL_FILE "\\mc.%d.control"
2083 char control_file [sizeof (CONTROL_FILE) + 8];
2085 void
2086 OS_Setup (void)
2088 SetConsoleTitle ("GNU Midnight Commander");
2090 shell = getenv ("COMSPEC");
2091 if (!shell || !*shell)
2092 shell = get_default_shell ();
2094 /* Default opening mode for files is binary, not text (CR/LF translation) */
2095 #ifndef __EMX__
2096 _fmode = O_BINARY;
2097 #endif
2099 mc_home = get_mc_lib_dir ();
2102 static void
2103 sigchld_handler_no_subshell (int sig)
2107 void
2108 init_sigchld (void)
2112 void
2113 init_sigfatals (void)
2115 /* Nothing to be done on the OS/2, Windows/NT */
2119 #else
2121 /* Unix version */
2122 #define CONTROL_FILE "/tmp/mc.%d.control"
2123 char control_file [sizeof (CONTROL_FILE) + 8];
2125 static void
2126 OS_Setup (void)
2128 char *mc_libdir;
2129 shell = getenv ("SHELL");
2130 if (!shell || !*shell)
2131 shell = g_strdup (getpwuid (geteuid ())->pw_shell);
2132 if (!shell || !*shell)
2133 shell = "/bin/sh";
2135 g_snprintf (control_file, sizeof (control_file), CONTROL_FILE, getpid ());
2136 my_putenv ("MC_CONTROL_FILE", control_file);
2138 /* This is the directory, where MC was installed, on Unix this is LIBDIR */
2139 /* and can be overriden by the MC_LIBDIR environment variable */
2140 if ((mc_libdir = getenv ("MC_LIBDIR")) != NULL) {
2141 mc_home = g_strdup (mc_libdir);
2142 } else {
2143 mc_home = g_strdup (LIBDIR);
2147 static void
2148 sigchld_handler_no_subshell (int sig)
2150 #if defined(linux) || defined(__linux__)
2151 int pid, status;
2153 if (!console_flag)
2154 return;
2156 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2157 the value of console_flag never changed, we could simply not install
2158 this handler at all if (!console_flag && !use_subshell). */
2160 /* That comment is no longer true. We need to wait() on a sigchld
2161 handler (that's at least what the tarfs code expects currently). */
2163 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2165 if (pid == cons_saver_pid){
2167 #ifdef SIGTSTP
2168 if (WIFSTOPPED (status))
2169 /* Someone has stopped cons.saver - restart it */
2170 kill (pid, SIGCONT);
2171 else
2172 #endif /* SIGTSTP */
2174 /* cons.saver has died - disable console saving */
2175 handle_console (CONSOLE_DONE);
2176 console_flag = 0;
2179 #endif /* linux || __linux__ */
2181 /* If we get here, some other child exited; ignore it */
2184 void
2185 init_sigchld (void)
2187 struct sigaction sigchld_action;
2189 sigchld_action.sa_handler =
2190 #ifdef HAVE_SUBSHELL_SUPPORT
2191 use_subshell ? sigchld_handler :
2192 #endif /* HAVE_SUBSHELL_SUPPORT */
2193 sigchld_handler_no_subshell;
2195 sigemptyset (&sigchld_action.sa_mask);
2197 #ifdef SA_RESTART
2198 sigchld_action.sa_flags = SA_RESTART;
2199 #else
2200 sigchld_action.sa_flags = 0;
2201 #endif /* !SA_RESTART */
2203 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) {
2204 #ifdef HAVE_SUBSHELL_SUPPORT
2206 * This may happen on QNX Neutrino 6, where SA_RESTART
2207 * is defined but not implemented. Fallback to no subshell.
2209 use_subshell = 0;
2210 #endif /* HAVE_SUBSHELL_SUPPORT */
2214 #endif /* _OS_NT, UNIX */
2216 static void
2217 print_mc_usage (FILE *stream)
2219 const char * const * ptr;
2220 const char * const usage [] = {
2222 N_("Usage is:\n\n"
2223 "mc [flags] [this_dir] [other_panel_dir]\n\n"),
2224 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2225 N_("-a, --stickchars Force use of +, -, | for line drawing.\n"),
2226 #endif
2227 N_("-b, --nocolor Force black and white display.\n"),
2228 #ifdef WITH_BACKGROUND
2229 N_("-B, --background [DEVEL-ONLY: Debug the background code]\n"),
2230 #endif
2231 N_("-c, --color Force color mode.\n"
2232 "-C, --colors Specify colors (use --help-colors to get a list).\n"
2233 "-d, --nomouse Disable mouse support.\n"),
2234 #ifdef USE_INTERNAL_EDIT
2235 N_("-e, --edit Startup the internal editor.\n"),
2236 #endif
2237 N_("-f, --libdir Print configured paths.\n"
2238 "-h, --help Shows this help message.\n"
2239 "-k, --resetsoft Reset softkeys (HP terminals only) to their terminfo/termcap\n"
2240 " default.\n"),
2241 #ifdef USE_NETCODE
2242 N_("-l, --ftplog file Log ftpfs commands to the file.\n"),
2243 #endif
2244 #ifdef HAVE_MAD
2245 N_("-M, --memory file [DEVEL-ONLY: Log MAD messages to the file.]\n"),
2246 #endif
2247 N_("-P, --printwd At exit, print the last working directory.\n"
2248 "-s, --slow Disables verbose operation (for slow terminals).\n"),
2249 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2250 N_("-t, --termcap Activate support for the TERMCAP variable.\n"),
2251 #endif
2252 #if defined(HAVE_SLANG) && defined(OS2_NT)
2253 N_("-S, --createcmdile Create command file to set default directory upon exit.\n"),
2254 #endif
2255 #ifdef HAVE_SUBSHELL_SUPPORT
2256 N_("-u, --nosubshell Disable the concurrent subshell mode.\n"
2257 "-U, --subshell Force the concurrent subshell mode.\n"
2258 "-r, --forceexec Force subshell execution.\n"),
2259 #endif
2260 N_("-v, --view fname Start up into the viewer mode.\n"
2261 "-V, --version Report version and configuration options.\n"
2262 "-x, --xterm Force xterm mouse support and screen save/restore.\n"
2263 "+number number it is the start line number of file for `mcedit'.\n"),
2264 N_("\n"
2265 "Please send any bug reports (including the output of `mc -V')\n"
2266 "to mc-devel@gnome.org\n"),
2267 NULL
2270 version (0);
2272 for (ptr = usage; *ptr; ptr++)
2273 fputs (_(*ptr), stream);
2276 static void
2277 print_color_usage (void)
2279 fputs (_("--colors KEYWORD={FORE},{BACK}\n\n"
2280 "{FORE} and {BACK} can be ommited, and the default will be used\n"
2281 "\n"
2282 "Keywords:\n"
2283 " Global: errors, reverse, gauge, input\n"
2284 " File display: normal, selected, marked, markselect\n"
2285 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus\n"
2286 " Menus: menu, menuhot, menusel, menuhotsel\n"
2287 " Help: helpnormal, helpitalic, helplink, helpslink\n"
2288 " File types: directory, execute, link, device, special, core\n"
2289 "\n"
2290 "Colors:\n"
2291 " black, gray, red, brightred, green, brightgreen, brown,\n"
2292 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
2293 " brightcyan, lightgray and white\n\n"), stdout);
2296 static void
2297 probably_finish_program (void)
2299 if (finish_program){
2300 if (print_last_wd)
2301 printf (".");
2302 exit (1);
2306 enum {
2307 GEOMETRY_KEY = -1,
2308 NOWIN_KEY = -2
2311 static void
2312 process_args (int c, const char *option_arg)
2314 switch (c) {
2315 case 'V':
2316 version (1);
2317 finish_program = 1;
2318 break;
2320 case 'c':
2321 disable_colors = 0;
2322 #ifdef HAVE_SLANG
2323 force_colors = 1;
2324 #endif
2325 break;
2327 case 'f':
2328 printf (_("Library directory for the Midnight Commander: %s\n"), mc_home);
2329 finish_program = 1;
2330 break;
2332 #ifdef USE_NETCODE
2333 case 'l':
2334 ftpfs_set_debug (option_arg);
2335 #ifdef WITH_SMBFS
2336 /* smbfs_set_debug (option_arg); */
2337 #endif
2338 break;
2339 #endif
2341 case 'm':
2342 fputs (_("Option -m is obsolete. Please look at Display Bits... in the Option's menu\n"), stderr);
2343 finish_program = 1;
2344 break;
2346 #ifdef HAVE_MAD
2347 case 'M':
2348 mad_set_debug (option_arg);
2349 break;
2350 #endif
2352 #ifdef OS2_NT
2353 case 'S':
2354 print_last_wd = 2;
2355 batch_file_name = option_arg;
2356 break;
2357 #endif
2359 case 'd':
2360 use_mouse_p = MOUSE_DISABLED;
2361 break;
2363 #ifdef HAVE_SUBSHELL_SUPPORT
2364 case 'u':
2365 use_subshell = 0;
2366 break;
2367 #endif /* HAVE_SUBSHELL_SUPPORT */
2369 case 'H':
2370 print_color_usage ();
2371 finish_program = 1;
2372 break;
2374 case 'h':
2375 print_mc_usage (stdout);
2376 finish_program = 1;
2380 static const struct poptOption argument_table [] = {
2381 #ifdef WITH_BACKGROUND
2382 { "background", 'B', POPT_ARG_NONE, &background_wait, 0,
2383 N_("Use to debug the background code") },
2384 #endif
2385 #if defined(HAVE_SLANG) && defined(OS2_NT)
2386 { "createcmdfile", 'S', POPT_ARG_STRING, NULL, 'S' },
2387 #endif
2388 { "color", 'c', POPT_ARG_NONE, NULL, 'c',
2389 N_("Request to run in color mode") },
2390 { "colors", 'C', POPT_ARG_STRING, &command_line_colors, 0,
2391 N_("Specifies a color configuration") },
2393 #ifdef USE_INTERNAL_EDIT
2394 { "edit", 'e', POPT_ARG_STRING, &edit_one_file, 0,
2395 N_("Edits one file") },
2396 #endif
2398 { "help", 'h', POPT_ARG_NONE, NULL, 'h',
2399 N_("Displays this help message") },
2400 { "help-colors", 'H', POPT_ARG_NONE, NULL, 'H',
2401 N_("Displays a help screen on how to change the color scheme") },
2402 #ifdef USE_NETCODE
2403 { "ftplog", 'l', POPT_ARG_STRING, NULL, 'l',
2404 N_("Log ftp dialog to specified file") },
2405 #endif
2406 #ifdef HAVE_MAD
2407 { "memory", 'M', POPT_ARG_STRING, NULL, 'M', },
2408 #endif
2409 { "libdir", 'f', POPT_ARG_NONE, NULL, 'f' },
2410 { NULL, 'm', POPT_ARG_NONE, NULL, 'm',
2411 N_("Obsolete") },
2412 { "nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0,
2413 N_("Requests to run in black and white") },
2414 { "nomouse", 'd', POPT_ARG_NONE, NULL, 'd',
2415 N_("Disable mouse support in text version") },
2416 #ifdef HAVE_SUBSHELL_SUPPORT
2417 { "nosubshell", 'u', POPT_ARG_NONE, NULL, 'u',
2418 N_("Disables subshell support") },
2419 { "forceexec", 'r', POPT_ARG_NONE, &force_subshell_execution,0 },
2420 #endif
2421 { "printwd", 'P', POPT_ARG_NONE, &print_last_wd, 0,
2422 N_("Prints working directory at program exit") },
2423 { "resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0,
2424 N_("Resets soft keys on HP terminals") },
2425 { "slow", 's', POPT_ARG_NONE, &slow_terminal, 0,
2426 N_("To run on slow terminals") },
2427 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2428 { "stickchars", 'a', 0, &force_ugly_line_drawing, 0,
2429 N_("Use stickchars to draw") },
2430 #endif
2431 #ifdef HAVE_SUBSHELL_SUPPORT
2432 { "subshell", 'U', POPT_ARG_NONE, &use_subshell, 0,
2433 N_("Enables subshell support (default)")},
2434 #endif
2435 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2436 { "termcap", 't', 0, &SLtt_Try_Termcap, 0,
2437 N_("Tries to use termcap instead of terminfo") },
2438 #endif
2439 { "version", 'V', POPT_ARG_NONE, NULL, 'V',
2440 N_("Displays the current version") },
2441 { "view", 'v', POPT_ARG_STRING, &view_one_file, 0,
2442 N_("Launches the file viewer on a file") },
2443 { "xterm", 'x', POPT_ARG_NONE, &force_xterm, 0,
2444 N_("Forces xterm features") },
2446 { NULL, 0, 0, NULL, 0 }
2449 static void
2450 handle_args (int argc, char *argv [])
2452 char *tmp;
2453 poptContext ctx;
2454 char *option_arg, *base;
2455 int c;
2457 ctx = poptGetContext ("mc", argc, argv, argument_table, 0);
2459 #ifdef USE_TERMCAP
2460 SLtt_Try_Termcap = 1;
2461 #endif
2463 while ((c = poptGetNextOpt (ctx)) > 0){
2464 option_arg = poptGetOptArg (ctx);
2466 process_args (c, option_arg);
2469 if (c < -1){
2470 print_mc_usage (stderr);
2471 fprintf(stderr, "%s: %s\n",
2472 poptBadOption (ctx, POPT_BADOPTION_NOALIAS),
2473 poptStrerror (c));
2474 finish_program = 1;
2476 probably_finish_program ();
2478 tmp = poptGetArg (ctx);
2481 * Check for special invocation names mcedit and mcview,
2482 * if none apply then set the current directory and the other
2483 * directory from the command line arguments
2485 base = x_basename (argv[0]);
2486 if (!STRNCOMP (base, "mce", 3) || !STRCOMP(base, "vi")) {
2487 edit_one_file = "";
2488 edit_one_file_start_line = 1;
2489 if (tmp) {
2490 if (*tmp == '+' && isdigit (tmp[1])){
2491 int start_line = atoi (tmp);
2492 if (start_line > 0) {
2493 char *file = poptGetArg (ctx);
2494 if (file) {
2495 tmp = file;
2496 edit_one_file_start_line = start_line;
2500 edit_one_file = g_strdup (tmp);
2502 } else
2503 if (!STRNCOMP (base, "mcv", 3) || !STRCOMP(base, "view")) {
2504 if (tmp)
2505 view_one_file = g_strdup (tmp);
2506 else {
2507 fprintf (stderr, "No arguments given to the viewer\n");
2508 finish_program = 1;
2509 probably_finish_program ();
2511 } else {
2512 /* sets the current dir and the other dir */
2513 if (tmp) {
2514 char buffer[MC_MAXPATHLEN + 2];
2515 this_dir = g_strdup (tmp);
2516 mc_get_current_wd (buffer, sizeof (buffer) - 2);
2517 if ((tmp = poptGetArg (ctx)))
2518 other_dir = g_strdup (tmp);
2522 poptFreeContext (ctx);
2526 * The compatibility_move_mc_files routine is intended to
2527 * move all of the hidden .mc files into a private ~/.mc
2528 * directory in the home directory, to avoid cluttering the users.
2530 * Previous versions of the program had all of their files in
2531 * the $HOME, we are now putting them in $HOME/.mc
2533 #ifdef OS2_NT
2534 # define compatibility_move_mc_files()
2535 #else
2537 static int
2538 do_mc_filename_rename (char *mc_dir, char *o_name, char *n_name)
2540 char *full_o_name = concat_dir_and_file (home_dir, o_name);
2541 char *full_n_name = g_strconcat (home_dir, MC_BASE, n_name, NULL);
2542 int move;
2544 move = 0 == rename (full_o_name, full_n_name);
2545 g_free (full_o_name);
2546 g_free (full_n_name);
2547 return move;
2550 static void
2551 do_compatibility_move (char *mc_dir)
2553 struct stat s;
2554 int move;
2556 if (stat (mc_dir, &s) == 0)
2557 return;
2558 if (errno != ENOENT)
2559 return;
2561 if (mkdir (mc_dir, 0777) == -1)
2562 return;
2564 move = do_mc_filename_rename (mc_dir, ".mc.ini", "ini");
2565 move += do_mc_filename_rename (mc_dir, ".mc.hot", "hotlist");
2566 move += do_mc_filename_rename (mc_dir, ".mc.ext", "ext");
2567 move += do_mc_filename_rename (mc_dir, ".mc.menu", "menu");
2568 move += do_mc_filename_rename (mc_dir, ".mc.bashrc", "bashrc");
2569 move += do_mc_filename_rename (mc_dir, ".mc.inputrc", "inputrc");
2570 move += do_mc_filename_rename (mc_dir, ".mc.tcshrc", "tcshrc");
2571 move += do_mc_filename_rename (mc_dir, ".mc.tree", "tree");
2573 if (!move)
2574 return;
2576 show_change_notice = 1;
2579 static void
2580 compatibility_move_mc_files (void)
2582 char *mc_dir = concat_dir_and_file (home_dir, ".mc");
2584 do_compatibility_move (mc_dir);
2585 g_free (mc_dir);
2587 #endif /* OS2_NT */
2590 main (int argc, char *argv [])
2592 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2593 setlocale (LC_ALL, "");
2594 bindtextdomain ("mc", LOCALEDIR);
2595 textdomain ("mc");
2596 mad_init ();
2598 /* Initialize list of all user group for timur_clr_mode */
2599 init_groups ();
2601 OS_Setup ();
2603 /* This variable is used by the subshell */
2604 home_dir = getenv ("HOME");
2605 if (!home_dir) {
2606 /* mc_home was computed by OS_Setup */
2607 home_dir = mc_home;
2610 vfs_init ();
2612 #ifdef HAVE_SLANG
2613 SLtt_Ignore_Beep = 1;
2614 #endif
2616 /* NOTE: This has to be called before slang_init or whatever routine
2617 calls any define_sequence */
2618 init_key ();
2620 handle_args (argc, argv);
2622 /* Used to report the last working directory at program end */
2623 if (print_last_wd){
2624 #ifndef OS2_NT
2625 stdout_fd = dup (1);
2626 close (1);
2627 if (open (ttyname (0), O_RDWR) < 0)
2628 if (open ("/dev/tty", O_RDWR) < 0) {
2629 /* Try if stderr is not redirected as the last chance */
2630 char *p = g_strdup (ttyname (0));
2632 if (!strcmp (p, ttyname (2)))
2633 dup2 (2, 1);
2634 else {
2635 fprintf (stderr,
2636 _("Couldn't open tty line. You have to run mc without the -P flag.\n"
2637 "On some systems you may want to run # `which mc`\n"));
2638 exit (1);
2640 g_free (p);
2642 #endif
2645 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
2646 handle_console (CONSOLE_INIT);
2648 # ifdef HAVE_SUBSHELL_SUPPORT
2649 subshell_get_console_attributes ();
2650 # endif
2652 /* Install the SIGCHLD handler; must be done before init_subshell() */
2653 init_sigchld ();
2655 compatibility_move_mc_files ();
2657 /* We need this, since ncurses endwin () doesn't restore the signals */
2658 save_stop_handler ();
2660 /* Must be done before init_subshell, to set up the terminal size: */
2661 /* FIXME: Should be removed and LINES and COLS computed on subshell */
2662 #ifdef HAVE_SLANG
2663 slang_init ();
2664 #endif
2665 /* NOTE: This call has to be after slang_init. It's the small part from
2666 the previous init_key which had to be moved after the call of slang_init */
2667 init_key_input_fd ();
2669 load_setup ();
2671 init_curses ();
2673 init_xterm_support ();
2675 #ifdef HAVE_SUBSHELL_SUPPORT
2677 /* Don't use subshell when invoked as a viewer or editor */
2678 if (edit_one_file || view_one_file)
2679 use_subshell = 0;
2681 /* Done here to ensure that the subshell doesn't */
2682 /* inherit the file descriptors opened below, etc */
2683 if (use_subshell)
2684 init_subshell ();
2686 #endif /* HAVE_SUBSHELL_SUPPORT */
2688 /* Removing this from the X code let's us type C-c */
2689 load_key_defs ();
2691 /* Also done after init_subshell, to save any shell init file messages */
2692 if (console_flag)
2693 handle_console (CONSOLE_SAVE);
2695 if (alternate_plus_minus)
2696 application_keypad_mode ();
2698 if (show_change_notice){
2699 message (1, _(" Notice "),
2700 _(" The Midnight Commander configuration files \n"
2701 " are now stored in the ~/.mc directory, the \n"
2702 " files have been moved now\n"));
2705 #ifdef HAVE_SUBSHELL_SUPPORT
2706 if (use_subshell){
2707 prompt = strip_ctrl_codes (subshell_prompt);
2708 if (!prompt)
2709 prompt = "";
2710 } else
2711 #endif /* HAVE_SUBSHELL_SUPPORT */
2712 prompt = (geteuid () == 0) ? "# " : "$ ";
2714 /* Program main loop */
2715 do_nc ();
2717 /* Save the tree store */
2718 tree_store_save ();
2720 /* Virtual File System shutdown */
2721 vfs_shut ();
2723 /* Delete list of all user groups */
2724 destroy_groups ();
2726 flush_extension_file (); /* does only free memory */
2728 /* Miguel, maybe the fix in slang is not required and
2729 * it could be done by removing the slang_done_screen.
2730 * Do I need to call slang_reset_tty then?
2732 endwin ();
2733 slang_shutdown ();
2735 if (console_flag && !(quit & SUBSHELL_EXIT))
2736 restore_console ();
2737 if (alternate_plus_minus)
2738 numeric_keypad_mode ();
2740 #ifndef OS2_NT
2741 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
2742 #endif
2744 if (console_flag)
2745 handle_console (CONSOLE_DONE);
2746 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
2748 #ifdef _OS_NT
2749 /* On NT, home_dir is malloced */
2750 g_free (home_dir);
2751 #endif
2752 #if defined(OS2_NT)
2753 if (print_last_wd == 2){
2754 FILE *bat_file;
2756 print_last_wd = 0;
2757 bat_file = fopen(batch_file_name, "w");
2758 if (bat_file != NULL){
2759 /* .ado: \r\n for Win95 */
2760 fprintf(bat_file, "@echo off\r\n");
2761 if (isalpha(last_wd_string[0]))
2762 fprintf(bat_file, "%c:\r\n", last_wd_string[0]);
2763 fprintf(bat_file, "cd %s\r\n", last_wd_string);
2764 fclose(bat_file);
2767 #endif
2768 if (print_last_wd) {
2769 if (print_last_revert || edit_one_file || view_one_file)
2770 write (stdout_fd, ".", 1);
2771 else
2772 write (stdout_fd, last_wd_string, strlen (last_wd_string));
2773 g_free (last_wd_string);
2776 #ifdef HAVE_MAD
2777 done_key ();
2778 #ifdef HAVE_CHARSET
2779 free_codepages_list ();
2780 #endif
2781 #endif
2783 mad_finalize (__FILE__, __LINE__);
2784 return 0;