- converted 1st stage setup stub from message box style to property sheet style
[reactos.git] / rosapps / applications / mc / src / main.c
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997 The Free Software Foundation
3
4 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
5 1994, 1995 Janne Kukonlehto
6 1997 Norbert Warmuth
7
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.
12
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.
17
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include <config.h>
23 #include <locale.h>
24
25 #ifdef _OS_NT
26 # include <windows.h>
27 #endif
28
29 #ifdef __os2__
30 # define INCL_DOS
31 # define INCL_DOSFILEMGR
32 # define INCL_DOSERRORS
33 # include <os2.h>
34 #endif
35
36 #include "tty.h"
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/param.h>
42
43 #include <sys/stat.h>
44
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48
49 /* unistd.h defines _POSIX_VERSION on POSIX.1 systems. */
50 #if defined(HAVE_DIRENT_H) || defined(_POSIX_VERSION)
51 # include <dirent.h>
52 # define NLENGTH(dirent) (strlen ((dirent)->d_name))
53 #else
54 # define dirent direct
55 # define NLENGTH(dirent) ((dirent)->d_namlen)
56
57 # ifdef HAVE_SYS_NDIR_H
58 # include <sys/ndir.h>
59 # endif /* HAVE_SYS_NDIR_H */
60
61 # ifdef HAVE_SYS_DIR_H
62 # include <sys/dir.h>
63 # endif /* HAVE_SYS_DIR_H */
64
65 # ifdef HAVE_NDIR_H
66 # include <ndir.h>
67 # endif /* HAVE_NDIR_H */
68 #endif /* not (HAVE_DIRENT_H or _POSIX_VERSION) */
69
70 #if HAVE_SYS_WAIT_H
71 # include <sys/wait.h> /* For waitpid() */
72 #endif
73
74 #include <errno.h>
75 #ifndef OS2_NT
76 # include <pwd.h>
77 #endif
78 #include <ctype.h>
79 #include <fcntl.h> /* For O_RDWR */
80 #include <signal.h>
81
82 /* Program include files */
83 #include "x.h"
84 #include "mad.h"
85 #include "dir.h"
86 #include "color.h"
87 #include "global.h"
88 #include "util.h"
89 #include "dialog.h"
90 #include "menu.h"
91 #include "file.h"
92 #include "panel.h"
93 #include "main.h"
94 #include "win.h"
95 #include "user.h"
96 #include "mem.h"
97 #include "mouse.h"
98 #include "option.h"
99 #include "tree.h"
100 #include "cons.saver.h"
101 #include "subshell.h"
102 #include "key.h" /* For init_key() and mi_getch() */
103 #include "setup.h" /* save_setup() */
104 #include "profile.h" /* free_profiles() */
105 #include "boxes.h"
106 #include "layout.h"
107 #include "cmd.h" /* Normal commands */
108 #include "hotlist.h"
109 #include "panelize.h"
110 #ifndef __os2__
111 # include "learn.h"
112 #endif
113 #include "listmode.h"
114 #include "background.h"
115 #include "ext.h" /* For flush_extension_file() */
116
117 /* Listbox for the command history feature */
118 #include "widget.h"
119 #include "command.h"
120 #include "wtools.h"
121 #include "complete.h" /* For the free_completion */
122
123 #include "chmod.h"
124 #include "chown.h"
125
126 #ifdef OS2_NT
127 # include <io.h>
128 # include <drive.h>
129 #endif
130
131 #include "../vfs/vfs.h"
132 #include "../vfs/extfs.h"
133
134
135 #include "popt.h"
136
137 /* "$Id$" */
138
139 /* When the modes are active, left_panel, right_panel and tree_panel */
140 /* Point to a proper data structure. You should check with the functions */
141 /* get_current_type and get_other_type the types of the panels before using */
142 /* This pointer variables */
143
144 /* The structures for the panels */
145 WPanel *left_panel;
146 WPanel *right_panel;
147
148 /* The pointer to the tree */
149 WTree *the_tree;
150
151 /* The Menubar */
152 WMenu *the_menubar;
153
154 /* Pointers to the selected and unselected panel */
155 WPanel *current_panel = NULL;
156
157 /* Set when we want use advanced chmod command instead of chmod and/or chown */
158 int advanced_chfns = 0;
159
160 /* Set when main loop should be terminated */
161 volatile int quit = 0;
162
163 /* Set if you want the possible completions dialog for the first time */
164 int show_all_if_ambiguous = 0;
165
166 /* Set when cd symlink following is desirable (bash mode) */
167 int cd_symlinks = 1;
168
169 /* If set then dialogs just clean the screen when refreshing, else */
170 /* they do a complete refresh, refreshing all the parts of the program */
171 int fast_refresh = 0;
172
173 /* If true, marking a files moves the cursor down */
174 int mark_moves_down = 1;
175
176 /* If true, at startup the user-menu is invoked */
177 int auto_menu = 0;
178
179 /* If true, use + and \ keys normally and select/unselect do if M-+ / M-\ and M--
180 and keypad + / - */
181 int alternate_plus_minus = 0;
182
183 /* If true, then the +, - and \ keys have their special meaning only if the
184 * command line is emtpy, otherwise they behave like regular letters
185 */
186 int only_leading_plus_minus = 1;
187
188 /* If true, after executing a command, wait for a keystroke */
189 enum { pause_never, pause_on_dumb_terminals, pause_always };
190
191 int pause_after_run = pause_on_dumb_terminals;
192
193 /* It true saves the setup when quitting */
194 int auto_save_setup = 1;
195
196 /* If true, be eight bit clean */
197 int eight_bit_clean = 0;
198
199 /* If true, then display chars 0-255, else iso-8859-1,
200 requires eight_bit_clean */
201 int full_eight_bits = 0;
202
203 /* If true use the internal viewer */
204 int use_internal_view = 1;
205
206 /* Have we shown the fast-reload warning in the past? */
207 int fast_reload_w = 0;
208
209 /* Move page/item? When clicking on the top or bottom of a panel */
210 int mouse_move_pages = 1;
211
212 /* If true: l&r arrows are used to chdir if the input line is empty */
213 int navigate_with_arrows = 0;
214
215 /* If it is set, the commander will iconify itself when executing a program */
216 int iconify_on_exec = 1;
217
218 /* If true use +, -, | for line drawing */
219 int force_ugly_line_drawing = 0;
220
221 /* If true message "The shell is already running a command" never */
222 int force_subshell_execution = 0;
223
224 /* If true program softkeys (HP terminals only) on startup and after every
225 command ran in the subshell to the description found in the termcap/terminfo
226 database */
227 int reset_hp_softkeys = 0;
228
229 /* The prompt */
230 char *prompt = 0;
231
232 /* The widget where we draw the prompt */
233 WLabel *the_prompt;
234
235 /* The hint bar */
236 WLabel *the_hint;
237
238 /* The button bar */
239 WButtonBar *the_bar;
240
241 #ifdef HAVE_X
242 WButtonBar *the_bar2;
243 #endif
244
245 /* For slow terminals */
246 int slow_terminal = 0;
247
248 /* use mouse? */
249 int use_mouse_p = GPM_MOUSE;
250
251 /* If true, assume we are running on an xterm terminal */
252 static int force_xterm = 0;
253
254 /* Controls screen clearing before an exec */
255 int clear_before_exec = 1;
256
257 /* Asks for confirmation before deleting a file */
258 int confirm_delete = 1;
259
260 /* Asks for confirmation before overwriting a file */
261 int confirm_overwrite = 1;
262
263 /* Asks for confirmation before executing a program by pressing enter */
264 int confirm_execute = 0;
265
266 /* Asks for confirmation before leaving the program */
267 int confirm_exit = 1;
268
269 /* Asks for confirmation when using F3 to view a directory and there
270 are tagged files */
271 int confirm_view_dir = 0;
272
273 /* This flag indicates if the pull down menus by default drop down */
274 int drop_menus = 0;
275
276 /* The dialog handle for the main program */
277 Dlg_head *midnight_dlg;
278
279 /* Subshell: if set, then the prompt was not saved on CONSOLE_SAVE */
280 /* We need to paint it after CONSOLE_RESTORE, see: load_prompt */
281 int update_prompt = 0;
282
283 /* The name which was used to invoke mc */
284 char *program_name;
285
286 /* The home directory */
287 char *home_dir;
288
289 /* The value of the other directory, only used when loading the setup */
290 char *other_dir = 0;
291 char *this_dir = 0;
292
293 /* If true, then print on stdout the last directory we were at */
294 static int print_last_wd = 0;
295 static char *last_wd_string;
296 static int print_last_revert = 0;
297
298 /* On OS/2 and on Windows NT, we need a batch file to do the -P magic */
299 #ifdef OS2_NT
300 static char *batch_file_name = 0;
301 #endif
302
303 /* widget colors for the midnight commander */
304 int midnight_colors [4];
305
306 /* Force colors, only used by Slang */
307 int force_colors = 0;
308
309 /* colors specified on the command line: they override any other setting */
310 char *command_line_colors;
311
312 /* File name to view if argument was supplied */
313 char *view_one_file = 0;
314
315 /* File name to view if argument was supplied */
316 char *edit_one_file = 0;
317
318 /* Used so that widgets know if they are being destroyed or
319 shut down */
320 int midnight_shutdown = 0;
321
322 /* to show nice prompts */
323 static int last_paused = 0;
324
325 /* Only used at program boot */
326 int boot_current_is_left = 1;
327
328 /* Used for keeping track of the original stdout */
329 int stdout_fd = 0;
330
331 /* The user's shell */
332 char *shell;
333
334 /* mc_home: The home of MC */
335 char *mc_home;
336
337 /* if on, it displays the information that files have been moved to ~/.mc */
338 int show_change_notice = 0;
339
340 char cmd_buf [512];
341
342 /* Used during argument processing */
343 int finish_program = 0;
344
345 /* Forward declarations */
346 char *get_mc_lib_dir ();
347 int panel_event (Gpm_Event *event, WPanel *panel);
348 int menu_bar_event (Gpm_Event *event, void *);
349 static void menu_cmd (void);
350
351 #ifndef HAVE_GNOME
352 WPanel *
353 get_current_panel ()
354 {
355 return current_panel;
356 }
357
358 WPanel *
359 get_other_panel ()
360 {
361 return (WPanel *) get_panel_widget (get_other_index ());
362 }
363 #endif
364
365 void
366 try_to_select (WPanel *panel, char *name)
367 {
368 Xtry_to_select (panel, name);
369 select_item (panel);
370 display_mini_info (panel);
371 }
372
373 /*
374 * cd_try_to_select:
375 *
376 * If we moved to the parent directory move the selection pointer to
377 * the old directory name
378 */
379 void
380 cd_try_to_select (WPanel *panel)
381 {
382 #ifdef USE_VFS
383 char *p, *q;
384 #endif
385
386 int i, j = 4;
387
388 if (strlen (panel->lwd) > strlen (panel->cwd)
389 && strncmp (panel->cwd, panel->lwd, strlen (panel->cwd)) == 0
390 && strchr (panel->lwd + strlen (panel->cwd) + 1, PATH_SEP) == 0)
391 try_to_select (panel, panel->lwd);
392 else
393 #ifdef USE_VFS
394 if ((!strncmp (panel->lwd, "tar:", 4) &&
395 !strncmp (panel->lwd + 4, panel->cwd, strlen (panel->cwd))) ||
396 ((i = extfs_prefix_to_type (panel->lwd)) != -1 &&
397 !strncmp (panel->lwd + (j = strlen (extfs_get_prefix (i)) + 1),
398 panel->cwd, strlen (panel->cwd)))) {
399 p = strdup (panel->lwd + j + strlen (panel->cwd));
400 q = strchr (p, PATH_SEP);
401 if (q != NULL && (q != p || (q = strchr (q + 1, PATH_SEP)) != NULL))
402 *q = 0;
403 try_to_select (panel, p);
404 free (p);
405 } else
406 #endif
407 try_to_select (panel, NULL);
408 }
409
410 void
411 reload_panelized (WPanel *panel)
412 {
413 int i, j;
414 dir_list *list = &panel->dir;
415
416 if (panel != cpanel)
417 mc_chdir (panel->cwd);
418
419 for (i = 0, j = 0; i < panel->count; i++){
420 if (list->list [i].f.marked) {
421 /* Unmark the file in advance. In case the following mc_lstat
422 * fails we are done, else we have to mark the file again
423 * (Note: do_file_mark depends on a valid "list->list [i].buf").
424 * IMO that's the best way to update the panel's summary status
425 * -- Norbert
426 */
427 do_file_mark (panel, i, 0);
428 }
429 if (mc_lstat (list->list [i].fname, &list->list [i].buf)){
430 free (list->list [i].fname);
431 continue;
432 }
433 if (list->list [i].f.marked)
434 do_file_mark (panel, i, 1);
435 if (j != i)
436 list->list [j] = list->list [i];
437 j++;
438 }
439 if (j == 0)
440 panel->count = set_zero_dir (list);
441 else
442 panel->count = j;
443
444 if (panel != cpanel)
445 mc_chdir (cpanel->cwd);
446 }
447
448 void
449 update_one_panel_widget (WPanel *panel, int force_update, char *current_file)
450 {
451 int free_pointer;
452
453 if (force_update & UP_RELOAD){
454 panel->is_panelized = 0;
455
456 ftpfs_flushdir ();
457 bzero (&(panel->dir_stat), sizeof (panel->dir_stat));
458 }
459
460 /* If current_file == -1 (an invalid pointer) then preserve selection */
461 if (current_file == UP_KEEPSEL){
462 free_pointer = 1;
463 current_file = strdup (panel->dir.list [panel->selected].fname);
464 } else
465 free_pointer = 0;
466
467 if (panel->is_panelized)
468 reload_panelized (panel);
469 else
470 panel_reload (panel);
471
472 try_to_select (panel, current_file);
473 panel->dirty = 1;
474
475 if (free_pointer)
476 free (current_file);
477 }
478
479 #ifndef PORT_HAS_UPDATE_PANELS
480 void
481 update_one_panel (int which, int force_update, char *current_file)
482 {
483 WPanel *panel;
484
485 if (get_display_type (which) != view_listing)
486 return;
487
488 panel = (WPanel *) get_panel_widget (which);
489 update_one_panel_widget (panel, force_update, current_file);
490 }
491
492 /* This routine reloads the directory in both panels. It tries to
493 * select current_file in current_panel and other_file in other_panel.
494 * If current_file == -1 then it automatically sets current_file and
495 * other_file to the currently selected files in the panels.
496 *
497 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
498 * will not reload the other panel.
499 */
500 void
501 update_panels (int force_update, char *current_file)
502 {
503 int reload_other = !(force_update & UP_ONLY_CURRENT);
504 WPanel *panel;
505
506 update_one_panel (get_current_index (), force_update, current_file);
507 if (reload_other)
508 update_one_panel (get_other_index (), force_update, UP_KEEPSEL);
509
510 if (get_current_type () == view_listing)
511 panel = (WPanel *) get_panel_widget (get_current_index ());
512 else
513 panel = (WPanel *) get_panel_widget (get_other_index ());
514
515 mc_chdir (panel->cwd);
516 }
517 #endif
518
519 #ifdef WANT_PARSE
520 static void select_by_index (WPanel *panel, int i);
521
522 /* Called by parse_control_file */
523 static int index_by_name (file_entry *list, int count)
524 {
525 char *name;
526 int i;
527
528 name = strtok (NULL, " \t\n");
529 if (!name || !*name)
530 return -1;
531 for (i = 0; i < count; i++){
532 if (strcmp (name, list[i].fname) == 0)
533 return i;
534 }
535 return -1;
536 }
537
538 /* Called by parse_control_file */
539 static void select_by_index (WPanel *panel, int i)
540 {
541 if (i >= panel->count)
542 return;
543
544 unselect_item (panel);
545 panel->selected = i;
546
547 #ifndef HAVE_X
548 while (panel->selected - panel->top_file >= ITEMS (panel)){
549 /* Scroll window half screen */
550 panel->top_file += ITEMS (panel)/2;
551 paint_dir (panel);
552 select_item (panel);
553 }
554 while (panel->selected < panel->top_file){
555 /* Scroll window half screen */
556 panel->top_file -= ITEMS (panel)/2;
557 if (panel->top_file < 0) panel->top_file = 0;
558 paint_dir (panel);
559 }
560 #endif
561 select_item (panel);
562 }
563
564 /* Called by my_system
565 No error reporting, just exits on the first sign of trouble */
566 static void parse_control_file (void)
567 {
568 char *data, *current;
569 WPanel *panel;
570 file_entry *list;
571 int i;
572 FILE *file;
573 struct stat s;
574
575 if ((file = fopen (control_file, "r")) == NULL){
576 return;
577 }
578 /* Use of fstat prevents race conditions */
579 if (fstat (fileno (file), &s) != 0){
580 fclose (file);
581 return;
582 }
583 #ifndef OS2_NT
584 /* Security: Check that the user owns the control file to prevent
585 other users from playing tricks on him/her. */
586 if (s.st_uid != getuid ()){
587 fclose (file);
588 return;
589 }
590 #endif
591 data = (char *) xmalloc (s.st_size+1, "main, parse_control_file");
592 if (!data){
593 fclose (file);
594 return;
595 }
596 if (s.st_size != fread (data, 1, s.st_size, file)){
597 free (data);
598 fclose (file);
599 return;
600 }
601 data [s.st_size] = 0;
602 fclose (file);
603
604 /* The Control file has now been loaded to memory -> start parsing. */
605 current = strtok (data, " \t\n");
606 while (current && *current){
607 if (isupper (*current)){
608 if (get_other_type () != view_listing)
609 break;
610 else
611 panel = other_panel;
612 } else
613 panel = cpanel;
614
615 list = panel->dir.list;
616 *current = tolower (*current);
617
618 if (strcmp (current, "clear_tags") == 0){
619 unmark_files (panel);
620 } else if (strcmp (current, "tag") == 0){
621 i = index_by_name (list, panel->count);
622 if (i >= 0) {
623 do_file_mark (panel, i, 1);
624 }
625 } else if (strcmp (current, "untag") == 0){
626 i = index_by_name (list, panel->count);
627 if (i >= 0){
628 do_file_mark (panel, i, 0);
629 }
630 } else if (strcmp (current, "select") == 0){
631 i = index_by_name (list, panel->count);
632 if (i >= 0){
633 select_by_index (panel, i);
634 }
635 } else if (strcmp (current, "change_panel") == 0){
636 change_panel ();
637 } else if (strcmp (current, "cd") == 0){
638 int change = 0;
639 current = strtok (NULL, " \t\n");
640 if (!current) break;
641 if (cpanel != panel){
642 change_panel ();
643 change = 1;
644 }
645 do_cd (current, cd_parse_command);
646 if (change)
647 change_panel ();
648 } else {
649 /* Unknown command -> let's give up */
650 break;
651 }
652 current = strtok (NULL, " \t\n");
653 }
654
655 free (data);
656 paint_panel (cpanel);
657 paint_panel (opanel);
658 }
659 #else
660 #define parse_control_file()
661 #endif /* WANT_PARSE */
662
663 /* Sets up the terminal before executing a program */
664 static void
665 pre_exec (void)
666 {
667 use_dash (0);
668 edition_pre_exec ();
669 }
670
671 /* Save current stat of directories to avoid reloading the panels */
672 /* when no modifications have taken place */
673 void
674 save_cwds_stat (void)
675 {
676 if (fast_reload){
677 mc_stat (cpanel->cwd, &(cpanel->dir_stat));
678 if (get_other_type () == view_listing)
679 mc_stat (opanel->cwd, &(opanel->dir_stat));
680 }
681 }
682
683 #ifdef HAVE_SUBSHELL_SUPPORT
684 void
685 do_possible_cd (char *new_dir)
686 {
687 if (!do_cd (new_dir, cd_exact))
688 message (1, _(" Warning "),
689 _(" The Commander can't change to the directory that \n"
690 " the subshell claims you are in. Perhaps you have \n"
691 " deleted your working directory, or given yourself \n"
692 " extra access permissions with the \"su\" command? "));
693 }
694
695 void
696 do_update_prompt ()
697 {
698 if (update_prompt){
699 printf ("%s", subshell_prompt);
700 fflush (stdout);
701 update_prompt = 0;
702 }
703 }
704 #endif
705
706 void
707 restore_console (void)
708 {
709 handle_console (CONSOLE_RESTORE);
710 }
711
712 void
713 exec_shell ()
714 {
715 do_execute (shell, 0, 0);
716 }
717
718 void
719 do_execute (const char *shell, const char *command, int flags)
720 {
721 #ifdef HAVE_SUBSHELL_SUPPORT
722 char *new_dir = NULL;
723 #endif
724
725 #ifdef USE_VFS
726 char *old_vfs_dir = 0;
727
728 if (!vfs_current_is_local ())
729 old_vfs_dir = strdup (vfs_get_current_dir ());
730 #endif
731
732 save_cwds_stat ();
733 pre_exec ();
734 if (console_flag)
735 restore_console ();
736
737 #ifndef __os2__
738 unlink (control_file);
739 #endif
740 if (!use_subshell && !(flags & EXECUTE_INTERNAL)){
741 printf ("%s%s%s\n", last_paused ? "\r\n":"", prompt, command);
742 last_paused = 0;
743 }
744
745 #ifdef HAVE_SUBSHELL_SUPPORT
746 if (use_subshell && !(flags & EXECUTE_INTERNAL)){
747 do_update_prompt ();
748
749 /* We don't care if it died, higher level takes care of this */
750 #ifdef USE_VFS
751 invoke_subshell (command, VISIBLY, old_vfs_dir ? 0 : &new_dir);
752 #else
753 invoke_subshell (command, VISIBLY, &new_dir);
754 #endif
755 } else
756 #endif
757 my_system (flags, shell, command);
758
759 #ifndef HAVE_GNOME
760 if (!(flags & EXECUTE_INTERNAL)){
761 if ((pause_after_run == pause_always ||
762 (pause_after_run == pause_on_dumb_terminals &&
763 !xterm_flag && !console_flag)) && !quit){
764 printf (_("Press any key to continue..."));
765 last_paused = 1;
766 fflush (stdout);
767 mc_raw_mode ();
768 xgetch ();
769 }
770 if (console_flag){
771 if (output_lines && keybar_visible) {
772 putchar('\n');
773 fflush(stdout);
774 }
775 }
776 }
777 #endif
778
779 if (console_flag)
780 handle_console (CONSOLE_SAVE);
781 edition_post_exec ();
782
783 #ifdef HAVE_SUBSHELL_SUPPORT
784 if (new_dir)
785 do_possible_cd (new_dir);
786
787 #endif
788 #ifdef USE_VFS
789 if (old_vfs_dir){
790 mc_chdir (old_vfs_dir);
791 free (old_vfs_dir);
792 }
793 #endif
794
795 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
796
797 parse_control_file ();
798 #ifndef __os2__
799 unlink (control_file);
800 #endif
801 do_refresh ();
802 use_dash (TRUE);
803 }
804
805 /* Executes a command */
806 void
807 shell_execute (char *command, int flags)
808 {
809 #ifdef HAVE_SUBSHELL_SUPPORT
810 if (use_subshell)
811 if (subshell_state == INACTIVE || force_subshell_execution)
812 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
813 else
814 message (1, MSG_ERROR, _(" The shell is already running a command "));
815 else
816 #endif
817 do_execute (shell, command, flags | EXECUTE_AS_SHELL);
818 }
819
820 void
821 execute (char *command)
822 {
823 shell_execute (command, 0);
824 }
825
826 void
827 change_panel (void)
828 {
829 free_completions (input_w (cmdline));
830 dlg_one_down (midnight_dlg);
831 }
832
833 static int
834 quit_cmd_internal (int quiet)
835 {
836 int q = quit;
837
838 if (quiet || !confirm_exit){
839 q = 1;
840 } else {
841 if (query_dialog (_(" The Midnight Commander "),
842 _(" Do you really want to quit the Midnight Commander? "),
843 0, 2, _("&Yes"), _("&No")) == 0)
844 q = 1;
845 }
846 if (q){
847 #ifdef HAVE_SUBSHELL_SUPPORT
848 if (!use_subshell)
849 midnight_dlg->running = 0;
850 else
851 if ((q = exit_subshell ()))
852 #endif
853 midnight_dlg->running = 0;
854 }
855 if (q)
856 quit |= 1;
857 return quit;
858 }
859
860 int quit_cmd (void)
861 {
862 quit_cmd_internal (0);
863 return quit;
864 }
865
866 int quiet_quit_cmd (void)
867 {
868 print_last_revert = 1;
869 quit_cmd_internal (1);
870 return quit;
871 }
872
873 /*
874 * Touch window and refresh window functions
875 */
876
877 /* This routine untouches the first line on both panels in order */
878 /* to avoid the refreshing the menu bar */
879
880 #ifdef HAVE_X
881 void
882 untouch_bar (void)
883 {
884 }
885
886 void
887 repaint_screen (void)
888 {
889 do_refresh ();
890 }
891
892 #else /* HAVE_X */
893 void
894 untouch_bar (void)
895 {
896 do_refresh ();
897 }
898
899 void
900 repaint_screen (void)
901 {
902 do_refresh ();
903 mc_refresh ();
904 }
905 #endif /* HAVE_X */
906
907 /* Wrapper for do_subshell_chdir, check for availability of subshell */
908 void
909 subshell_chdir (char *directory)
910 {
911 #ifdef HAVE_SUBSHELL_SUPPORT
912 if (use_subshell){
913 if (vfs_current_is_local ())
914 do_subshell_chdir (directory, 0, 1);
915 }
916 #endif
917 }
918
919 void
920 directory_history_add (WPanel * panel, char *s)
921 {
922 if (!panel->dir_history) {
923 panel->dir_history = malloc (sizeof (Hist));
924 memset (panel->dir_history, 0, sizeof (Hist));
925 panel->dir_history->text = strdup (s);
926 return;
927 }
928 if (!strcmp (panel->dir_history->text, s))
929 return;
930 if (panel->dir_history->next) {
931 if (panel->dir_history->next->text) {
932 free (panel->dir_history->next->text);
933 panel->dir_history->next->text = 0;
934 }
935 } else {
936 panel->dir_history->next = malloc (sizeof (Hist));
937 memset (panel->dir_history->next, 0, sizeof (Hist));
938 panel->dir_history->next->prev = panel->dir_history;
939 }
940 panel->dir_history = panel->dir_history->next;
941 panel->dir_history->text = strdup (s);
942
943 panel_update_marks (panel);
944 }
945
946 /* Changes the current panel directory */
947 int
948 _do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
949 {
950 char *directory, *olddir;
951 char temp [MC_MAXPATHLEN];
952 #ifdef USE_VFS
953 vfs *oldvfs;
954 vfsid oldvfsid;
955 struct vfs_stamping *parent;
956 #endif
957 olddir = strdup (panel->cwd);
958
959 /* Convert *new_path to a suitable pathname, handle ~user */
960
961 if (cd_type == cd_parse_command){
962 while (*new_dir == ' ')
963 new_dir++;
964
965 if (!strcmp (new_dir, "-")){
966 strcpy (temp, panel->lwd);
967 new_dir = temp;
968 }
969 }
970 directory = *new_dir ? new_dir : home_dir;
971
972 if (mc_chdir (directory) == -1){
973 strcpy (panel->cwd, olddir);
974 free (olddir);
975 return 0;
976 }
977
978 /* Success: save previous directory, shutdown status of previous dir */
979 strcpy (panel->lwd, olddir);
980 free_completions (input_w (cmdline));
981
982 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
983
984 #ifdef USE_VFS
985 oldvfs = vfs_type (olddir);
986 oldvfsid = vfs_ncs_getid (oldvfs, olddir, &parent);
987 vfs_add_noncurrent_stamps (oldvfs, oldvfsid, parent);
988 vfs_rm_parents (parent);
989 #endif
990 free (olddir);
991
992 subshell_chdir (panel->cwd);
993
994 /* Reload current panel */
995 clean_dir (&panel->dir, panel->count);
996 panel->count = do_load_dir (&panel->dir, panel->sort_type,
997 panel->reverse, panel->case_sensitive, panel->filter);
998 panel->top_file = 0;
999 panel->selected = 0;
1000 panel->marked = 0;
1001 panel->dirs_marked = 0;
1002 panel->total = 0;
1003 panel->searching = 0;
1004 cd_try_to_select (panel);
1005 load_hint ();
1006 panel_update_contents (panel);
1007 return 1;
1008 }
1009
1010 int
1011 do_panel_cd (WPanel *panel, char *new_dir, enum cd_enum cd_type)
1012 {
1013 int r;
1014
1015 r = _do_panel_cd (panel, new_dir, cd_type);
1016 if (r)
1017 directory_history_add (cpanel, cpanel->cwd);
1018 return r;
1019 }
1020
1021 int
1022 do_cd (char *new_dir, enum cd_enum exact)
1023 {
1024 return (do_panel_cd (cpanel, new_dir, exact));
1025 }
1026
1027 void
1028 directory_history_next (WPanel * panel)
1029 {
1030 if (!panel->dir_history->next)
1031 return;
1032 if (_do_panel_cd (panel, panel->dir_history->next->text, cd_exact))
1033 panel->dir_history = panel->dir_history->next;
1034 panel_update_marks (panel);
1035 }
1036
1037 void
1038 directory_history_prev (WPanel * panel)
1039 {
1040 if (!panel->dir_history->prev)
1041 return;
1042 if (_do_panel_cd (panel, panel->dir_history->prev->text, cd_exact))
1043 panel->dir_history = panel->dir_history->prev;
1044 panel_update_marks (panel);
1045 }
1046
1047 void
1048 directory_history_list (WPanel * panel)
1049 {
1050 char *s;
1051 /* must be at least two to show a history */
1052 if (panel->dir_history) {
1053 if (panel->dir_history->prev || panel->dir_history->next) {
1054 s = show_hist (panel->dir_history, panel->widget.x, panel->widget.y);
1055 if (s) {
1056 int r;
1057 r = _do_panel_cd (panel, s, cd_exact);
1058 if (r)
1059 directory_history_add (panel, panel->cwd);
1060 free (s);
1061 }
1062 }
1063 }
1064 }
1065
1066 #ifdef HAVE_SUBSHELL_SUPPORT
1067 int
1068 load_prompt (int fd, void *unused)
1069 {
1070 if (!read_subshell_prompt (QUIETLY))
1071 return 0;
1072
1073 if (command_prompt){
1074 int prompt_len;
1075
1076 prompt = strip_ctrl_codes (subshell_prompt);
1077 prompt_len = strlen (prompt);
1078
1079 /* Check for prompts too big */
1080 if (prompt_len > COLS - 8) {
1081 prompt [COLS - 8 ] = 0;
1082 prompt_len = COLS - 8;
1083 }
1084 label_set_text (the_prompt, prompt);
1085 winput_set_origin ((WInput *)cmdline, prompt_len, COLS-prompt_len);
1086
1087 /* since the prompt has changed, and we are called from one of the
1088 * get_event channels, the prompt updating does not take place
1089 * automatically: force a cursor update and a screen refresh
1090 */
1091 if (current_dlg == midnight_dlg){
1092 update_cursor (midnight_dlg);
1093 mc_refresh ();
1094 }
1095 }
1096 update_prompt = 1;
1097 return 0;
1098 }
1099 #endif
1100
1101 /* The user pressed the enter key */
1102 int
1103 menu_bar_event (Gpm_Event *event, void *x)
1104 {
1105 if (event->type != GPM_DOWN)
1106 return MOU_NORMAL;
1107
1108 return MOU_ENDLOOP;
1109 }
1110
1111 /* Used to emulate Lynx's entering leaving a directory with the arrow keys */
1112 int
1113 maybe_cd (int char_code, int move_up_dir)
1114 {
1115 if (navigate_with_arrows){
1116 if (!input_w (cmdline)->buffer [0]){
1117 if (!move_up_dir){
1118 do_cd ("..", cd_exact);
1119 return 1;
1120 }
1121 if (S_ISDIR (selection (cpanel)->buf.st_mode)
1122 || link_isdir (selection (cpanel))){
1123 do_cd (selection (cpanel)->fname, cd_exact);
1124 return 1;
1125 }
1126 }
1127 }
1128 return 0;
1129 }
1130
1131 void
1132 set_sort_to (WPanel *p, sortfn *sort_order)
1133 {
1134 p->sort_type = sort_order;
1135
1136 /* The directory is already sorted, we have to load the unsorted stuff */
1137 if (sort_order == (sortfn *) unsorted){
1138 char *current_file;
1139
1140 current_file = strdup (cpanel->dir.list [cpanel->selected].fname);
1141 panel_reload (cpanel);
1142 try_to_select (cpanel, current_file);
1143 free (current_file);
1144 }
1145 do_re_sort (p);
1146 }
1147
1148 void
1149 sort_cmd (void)
1150 {
1151 WPanel *p;
1152 sortfn *sort_order;
1153
1154 if (!SELECTED_IS_PANEL)
1155 return;
1156
1157 p = MENU_PANEL;
1158 sort_order = sort_box (p->sort_type, &p->reverse, &p->case_sensitive);
1159
1160 if (sort_order == 0)
1161 return;
1162
1163 p->sort_type = sort_order;
1164
1165 /* The directory is already sorted, we have to load the unsorted stuff */
1166 if (sort_order == (sortfn *) unsorted){
1167 char *current_file;
1168
1169 current_file = strdup (cpanel->dir.list [cpanel->selected].fname);
1170 panel_reload (cpanel);
1171 try_to_select (cpanel, current_file);
1172 free (current_file);
1173 }
1174 do_re_sort (p);
1175 }
1176
1177 static void
1178 tree_box (void)
1179 {
1180 char *sel_dir;
1181
1182 sel_dir = tree (selection (cpanel)->fname);
1183 if (sel_dir){
1184 do_cd(sel_dir, cd_exact);
1185 free (sel_dir);
1186 }
1187 }
1188
1189 #if SOMEDAY_WE_WILL_FINISH_THIS_CODE
1190 static void
1191 listmode_cmd (void)
1192 {
1193 char *newmode;
1194 newmode = listmode_edit ("half <type,>name,|,size:8,|,perm:4+");
1195 message (0, " Listing format edit ", " New mode is \"%s\" ", newmode);
1196 free (newmode);
1197 }
1198 #endif
1199
1200 #ifdef HAVE_GNOME
1201 void init_menu () {};
1202 void done_menu () {};
1203 #else
1204 /* NOTICE: hotkeys specified here are overriden in menubar_paint_idx (alex) */
1205 static menu_entry PanelMenu [] = {
1206 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
1207 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
1208 { ' ', N_("&Info C-x i"), 'I', info_cmd },
1209 { ' ', N_("&Tree"), 'T', tree_cmd },
1210 { ' ', "", ' ', 0 },
1211 { ' ', N_("&Sort order..."), 'S', sort_cmd },
1212 { ' ', "", ' ', 0 },
1213 { ' ', N_("&Filter..."), 'F', filter_cmd },
1214 #ifdef USE_NETCODE
1215 { ' ', "", ' ', 0 },
1216 { ' ', N_("&Network link..."), 'N', netlink_cmd },
1217 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
1218 #endif
1219 { ' ', "", ' ', 0 },
1220 #ifdef OS2_NT
1221 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_a },
1222 #endif
1223 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
1224 };
1225
1226 static menu_entry RightMenu [] = {
1227 { ' ', N_("&Listing mode..."), 'L', listing_cmd },
1228 { ' ', N_("&Quick view C-x q"), 'Q', quick_view_cmd },
1229 { ' ', N_("&Info C-x i"), 'I', info_cmd },
1230 { ' ', N_("&Tree"), 'T', tree_cmd },
1231 { ' ', "", ' ', 0 },
1232 { ' ', N_("&Sort order..."), 'S', sort_cmd },
1233 { ' ', "", ' ', 0 },
1234 { ' ', N_("&Filter..."), 'F', filter_cmd },
1235 #ifdef USE_NETCODE
1236 { ' ', "", ' ', 0 },
1237 { ' ', N_("&Network link..."), 'N', netlink_cmd },
1238 { ' ', N_("FT&P link..."), 'P', ftplink_cmd },
1239 #endif
1240 { ' ', "", ' ', 0 },
1241 #ifdef OS2_NT
1242 { ' ', N_("&Drive... M-d"), 'D', drive_cmd_b },
1243 #endif
1244 { ' ', N_("&Rescan C-r"), 'R', reread_cmd }
1245 };
1246
1247 static menu_entry FileMenu [] = {
1248 { ' ', N_("&User menu F2"), 'U', user_menu_cmd },
1249 { ' ', N_("&View F3"), 'V', view_cmd },
1250 { ' ', N_("&Filtered view M-!"), 'F', filtered_view_cmd },
1251 { ' ', N_("&Edit F4"), 'E', edit_cmd },
1252 { ' ', N_("&Copy F5"), 'C', copy_cmd },
1253 { ' ', N_("c&Hmod C-x c"), 'H', chmod_cmd },
1254 #ifndef OS2_NT
1255 { ' ', N_("&Link C-x l"), 'L', link_cmd },
1256 { ' ', N_("&SymLink C-x s"), 'S', symlink_cmd },
1257 { ' ', N_("edit s&Ymlink C-x C-s"), 'Y', edit_symlink_cmd },
1258 { ' ', N_("ch&Own C-x o"), 'O', chown_cmd },
1259 { ' ', N_("&Advanced chown "), 'A', chown_advanced_cmd },
1260 #endif
1261 { ' ', N_("&Rename/Move F6"), 'R', ren_cmd },
1262 { ' ', N_("&Mkdir F7"), 'M', mkdir_cmd },
1263 { ' ', N_("&Delete F8"), 'D', delete_cmd },
1264 { ' ', N_("&Quick cd M-c"), 'Q', quick_cd_cmd },
1265 { ' ', "", ' ', 0 },
1266 { ' ', N_("select &Group M-+"), 'G', select_cmd },
1267 { ' ', N_("u&Nselect group M-\\"),'N', unselect_cmd },
1268 { ' ', N_("reverse selec&Tion M-*"), 'T', reverse_selection_cmd },
1269 { ' ', "", ' ', 0 },
1270 { ' ', N_("e&Xit F10"), 'X', (callfn) quit_cmd }
1271 };
1272
1273 void external_panelize (void);
1274 static menu_entry CmdMenu [] = {
1275 /* I know, I'm lazy, but the tree widget when it's not running
1276 * as a panel still has some problems, I have not yet finished
1277 * the WTree widget port, sorry.
1278 */
1279 { ' ', N_("&Directory tree"), 'D', tree_box },
1280 { ' ', N_("&Find file M-?"), 'F', find_cmd },
1281 #ifndef HAVE_XVIEW
1282 { ' ', N_("s&Wap panels C-u"), 'W', swap_cmd },
1283 { ' ', N_("switch &Panels on/off C-o"), 'P', view_other_cmd },
1284 #endif
1285 { ' ', N_("&Compare directories C-x d"), 'C', compare_dirs_cmd },
1286 { ' ', N_("e&Xternal panelize C-x !"), 'X', external_panelize },
1287 #ifdef HAVE_DUSUM
1288 { ' ', N_("show directory s&Izes"), 'I', dirsizes_cmd },
1289 #endif
1290 { ' ', "", ' ', 0 },
1291 { ' ', N_("command &History"), 'H', history_cmd },
1292 { ' ', N_("di&Rectory hotlist C-\\"), 'R', quick_chdir_cmd },
1293 #ifdef USE_VFS
1294 { ' ', N_("&Active VFS list C-x a"), 'A', reselect_vfs },
1295 #endif
1296 #ifdef WITH_BACKGROUND
1297 { ' ', N_("&Background jobs C-x j"), 'B', jobs_cmd },
1298 #endif
1299 { ' ', "", ' ', 0 },
1300 #ifdef USE_EXT2FSLIB
1301 { ' ', N_("&Undelete files (ext2fs only)"), 'U', undelete_cmd },
1302 #endif
1303 #ifdef VERSION_4
1304 { ' ', N_("&Listing format edit"), 'L', listmode_cmd},
1305 #endif
1306 { ' ', N_("&Extension file edit"), 'E', ext_cmd },
1307 { ' ', N_("&Menu file edit"), 'M', menu_edit_cmd }
1308 };
1309
1310 /* Must keep in sync with the constants in menu_cmd */
1311 static menu_entry OptMenu [] = {
1312 { ' ', N_("&Configuration..."), 'C', configure_box },
1313 { ' ', N_("&Layout..."), 'L', layout_cmd },
1314 { ' ', N_("c&Onfirmation..."), 'O', confirm_box },
1315 { ' ', N_("&Display bits..."), 'D', display_bits_box },
1316 #if !defined(HAVE_X) && !defined(OS2_NT)
1317 { ' ', N_("learn &Keys..."), 'K', learn_keys },
1318 #endif
1319 #ifdef USE_VFS
1320 { ' ', N_("&Virtual FS..."), 'V', configure_vfs },
1321 #endif
1322 { ' ', "", ' ', 0 },
1323 { ' ', N_("&Save setup"), 'S', save_setup_cmd }
1324 };
1325
1326 #define menu_entries(x) sizeof(x)/sizeof(menu_entry)
1327
1328 Menu MenuBar [5];
1329 #ifndef HAVE_X
1330 static Menu MenuBarEmpty [5];
1331 #endif
1332
1333 void
1334 init_menu (void)
1335 {
1336 int i;
1337
1338 #ifdef HAVE_X
1339 MenuBar [0] = create_menu (_(" Left "), PanelMenu, menu_entries (PanelMenu));
1340 #else
1341 MenuBar [0] = create_menu ( horizontal_split ? _(" Above ") : _(" Left "),
1342 PanelMenu, menu_entries (PanelMenu));
1343 #endif
1344 MenuBar [1] = create_menu (_(" File "), FileMenu, menu_entries (FileMenu));
1345 MenuBar [2] = create_menu (_(" Command "), CmdMenu, menu_entries (CmdMenu));
1346 MenuBar [3] = create_menu (_(" Options "), OptMenu, menu_entries (OptMenu));
1347 #ifndef HAVE_XVIEW
1348 #ifdef HAVE_X
1349 MenuBar [4] = create_menu (_(" Right "), RightMenu, menu_entries (PanelMenu));
1350 #else
1351 MenuBar [4] = create_menu (horizontal_split ? _(" Below ") : _(" Right "),
1352 RightMenu, menu_entries (PanelMenu));
1353 for (i = 0; i < 5; i++)
1354 MenuBarEmpty [i] = create_menu (MenuBar [i]->name, 0, 0);
1355 #endif /* HAVE_X */
1356 #endif /* ! HAVE_XVIEW */
1357 }
1358
1359 void
1360 done_menu (void)
1361 {
1362 int i;
1363
1364 #ifndef HAVE_XVIEW
1365 for (i = 0; i < 5; i++){
1366 destroy_menu (MenuBar [i]);
1367 #ifndef HAVE_X
1368 destroy_menu (MenuBarEmpty [i]);
1369 #endif
1370 #else
1371 for (i = 0; i < 4; i++){
1372 destroy_menu (MenuBar [i]);
1373 #endif
1374 }
1375 }
1376 #endif
1377
1378 static void
1379 menu_last_selected_cmd (void)
1380 {
1381 the_menubar->active = 1;
1382 the_menubar->dropped = drop_menus;
1383 the_menubar->previous_selection = dlg_item_number (midnight_dlg);
1384 dlg_select_widget (midnight_dlg, the_menubar);
1385 }
1386
1387 static void
1388 menu_cmd (void)
1389 {
1390 if (the_menubar->active)
1391 return;
1392
1393 if (get_current_index () == 0)
1394 the_menubar->selected = 0;
1395 else
1396 the_menubar->selected = 4;
1397 menu_last_selected_cmd ();
1398 }
1399
1400 /* Flag toggling functions */
1401 void
1402 toggle_confirm_delete (void)
1403 {
1404 confirm_delete = !confirm_delete;
1405 }
1406
1407 void
1408 toggle_fast_reload (void)
1409 {
1410 fast_reload = !fast_reload;
1411 if (fast_reload_w == 0 && fast_reload){
1412 message (0, _(" Information "),
1413 _(" Using the fast reload option may not reflect the exact \n"
1414 " directory contents. In this cases you'll need to do a \n"
1415 " manual reload of the directory. See the man page for \n"
1416 " the details. "));
1417 fast_reload_w = 1;
1418 }
1419 }
1420
1421 void
1422 toggle_mix_all_files (void)
1423 {
1424 mix_all_files = !mix_all_files;
1425 update_panels (UP_RELOAD, UP_KEEPSEL);
1426 }
1427
1428 void
1429 toggle_show_backup (void)
1430 {
1431 show_backups = !show_backups;
1432 update_panels (UP_RELOAD, UP_KEEPSEL);
1433 }
1434
1435 void
1436 toggle_show_hidden (void)
1437 {
1438 show_dot_files = !show_dot_files;
1439 update_panels (UP_RELOAD, UP_KEEPSEL);
1440 }
1441
1442 void
1443 toggle_show_mini_status (void)
1444 {
1445 show_mini_info = !show_mini_info;
1446 paint_panel (cpanel);
1447 if (get_other_type () == view_listing)
1448 paint_panel (opanel);
1449 }
1450
1451 void
1452 toggle_align_extensions (void)
1453 {
1454 align_extensions = !align_extensions;
1455 }
1456
1457 #ifndef PORT_HAS_CREATE_PANELS
1458 void
1459 create_panels (void)
1460 {
1461 int current_index;
1462 int other_index;
1463 int current_mode;
1464 int other_mode;
1465 char original_dir [1024];
1466
1467 original_dir [0] = 0;
1468
1469 if (boot_current_is_left){
1470 current_index = 0;
1471 other_index = 1;
1472 current_mode = startup_left_mode;
1473 other_mode = startup_right_mode;
1474 } else {
1475 current_index = 1;
1476 other_index = 0;
1477 current_mode = startup_right_mode;
1478 other_mode = startup_left_mode;
1479 }
1480 /* Creates the left panel */
1481 if (this_dir){
1482 if (other_dir){
1483 /* Ok, user has specified two dirs, save the original one,
1484 * since we may not be able to chdir to the proper
1485 * second directory later
1486 */
1487 mc_get_current_wd (original_dir, sizeof (original_dir)-2);
1488 }
1489 mc_chdir (this_dir);
1490 }
1491 set_display_type (current_index, current_mode);
1492
1493 /* The other panel */
1494 if (other_dir){
1495 if (original_dir [0])
1496 mc_chdir (original_dir);
1497 mc_chdir (other_dir);
1498 }
1499 set_display_type (other_index, other_mode);
1500
1501 if (startup_left_mode == view_listing){
1502 current_panel = left_panel;
1503 } else {
1504 if (right_panel)
1505 current_panel = right_panel;
1506 else
1507 current_panel = left_panel;
1508 }
1509
1510 /* Create the nice widgets */
1511 cmdline = command_new (0, 0, 0);
1512 the_prompt = label_new (0, 0, prompt, NULL);
1513 the_prompt->transparent = 1;
1514 the_bar = buttonbar_new (keybar_visible);
1515
1516 #ifndef HAVE_GNOME
1517 the_hint = label_new (0, 0, 0, NULL);
1518 the_hint->transparent = 1;
1519 the_hint->auto_adjust_cols = 0;
1520 the_hint->widget.cols = COLS;
1521 #endif
1522
1523 #ifndef HAVE_XVIEW
1524 the_menubar = menubar_new (0, 0, COLS, MenuBar, 5);
1525 #else
1526 the_menubar = menubar_new (0, 0, COLS, MenuBar + 1, 3);
1527 the_bar2 = buttonbar_new (keybar_visible);
1528 #endif
1529 }
1530 #endif
1531
1532 static void copy_current_pathname (void)
1533 {
1534 if (!command_prompt)
1535 return;
1536
1537 stuff (input_w (cmdline), cpanel->cwd, 0);
1538 if (cpanel->cwd [strlen (cpanel->cwd) - 1] != PATH_SEP)
1539 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1540 }
1541
1542 static void copy_other_pathname (void)
1543 {
1544 if (get_other_type () != view_listing)
1545 return;
1546
1547 if (!command_prompt)
1548 return;
1549
1550 stuff (input_w (cmdline), opanel->cwd, 0);
1551 if (cpanel->cwd [strlen (opanel->cwd) - 1] != PATH_SEP)
1552 stuff (input_w (cmdline), PATH_SEP_STR, 0);
1553 }
1554
1555 static void copy_readlink (WPanel *panel)
1556 {
1557 if (!command_prompt)
1558 return;
1559 if (S_ISLNK (selection (panel)->buf.st_mode)) {
1560 char buffer [MC_MAXPATHLEN];
1561 char *p = concat_dir_and_file (panel->cwd, selection (panel)->fname);
1562 int i;
1563
1564 i = mc_readlink (p, buffer, MC_MAXPATHLEN);
1565 free (p);
1566 if (i > 0) {
1567 buffer [i] = 0;
1568 stuff (input_w (cmdline), buffer, 0);
1569 }
1570 }
1571 }
1572
1573 static void copy_current_readlink (void)
1574 {
1575 copy_readlink (cpanel);
1576 }
1577
1578 static void copy_other_readlink (void)
1579 {
1580 if (get_other_type () != view_listing)
1581 return;
1582 copy_readlink (opanel);
1583 }
1584
1585 /* Inserts the selected file name into the input line */
1586 /* Exported so that the command modules uses it */
1587 void copy_prog_name (void)
1588 {
1589 char *tmp;
1590 if (!command_prompt)
1591 return;
1592
1593 if (get_current_type () == view_tree){
1594 WTree *tree = (WTree *) get_panel_widget (get_current_index ());
1595 tmp = name_quote (tree->selected_ptr->name, 1);
1596 } else
1597 tmp = name_quote (selection (cpanel)->fname, 1);
1598 stuff (input_w (cmdline), tmp, 1);
1599 free (tmp);
1600 }
1601
1602 static void copy_tagged (WPanel *panel)
1603 {
1604 int i;
1605
1606 if (!command_prompt)
1607 return;
1608 input_disable_update (input_w (cmdline));
1609 if (panel->marked){
1610 for (i = 0; i < panel->count; i++)
1611 if (panel->dir.list [i].f.marked) {
1612 char *tmp = name_quote (panel->dir.list [i].fname, 1);
1613 stuff (input_w (cmdline), tmp, 1);
1614 free (tmp);
1615 }
1616 } else {
1617 char *tmp = name_quote (panel->dir.list [panel->selected].fname, 1);
1618 stuff (input_w (cmdline), tmp, 1);
1619 free (tmp);
1620 }
1621 input_enable_update (input_w (cmdline));
1622 }
1623
1624 static void copy_current_tagged (void)
1625 {
1626 copy_tagged (cpanel);
1627 }
1628
1629 static void copy_other_tagged (void)
1630 {
1631 if (get_other_type () != view_listing)
1632 return;
1633 copy_tagged (opanel);
1634 }
1635
1636 static void do_suspend_cmd (void)
1637 {
1638 pre_exec ();
1639
1640 if (console_flag && !use_subshell)
1641 restore_console ();
1642
1643 #ifndef OS2_NT
1644 {
1645 struct sigaction sigtstp_action;
1646
1647 /* Make sure that the SIGTSTP below will suspend us directly,
1648 without calling ncurses' SIGTSTP handler; we *don't* want
1649 ncurses to redraw the screen immediately after the SIGCONT */
1650 sigaction (SIGTSTP, &startup_handler, &sigtstp_action);
1651
1652 kill (getpid (), SIGTSTP);
1653
1654 /* Restore previous SIGTSTP action */
1655 sigaction (SIGTSTP, &sigtstp_action, NULL);
1656 }
1657 #endif
1658
1659 if (console_flag && !use_subshell)
1660 handle_console (CONSOLE_SAVE);
1661
1662 edition_post_exec ();
1663 }
1664
1665 void suspend_cmd (void)
1666 {
1667 save_cwds_stat ();
1668 do_suspend_cmd ();
1669 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1670 do_refresh ();
1671 }
1672
1673 void init_labels (Widget *paneletc)
1674 {
1675 define_label (midnight_dlg, paneletc, 1, _("Help"), help_cmd);
1676 define_label (midnight_dlg, paneletc, 2, _("Menu"), user_menu_cmd);
1677 define_label (midnight_dlg, paneletc, 9, _("PullDn"), menu_cmd);
1678 define_label (midnight_dlg, paneletc, 10, _("Quit"), (voidfn) quit_cmd);
1679 }
1680
1681 #ifndef HAVE_XVIEW
1682 static key_map ctl_x_map [] = {
1683 { XCTRL('c'), (callfn) quit_cmd },
1684 #ifdef USE_VFS
1685 { 'a', reselect_vfs },
1686 #endif
1687 { 'd', compare_dirs_cmd },
1688 #ifndef HAVE_GNOME
1689 { 'p', copy_current_pathname },
1690 { XCTRL('p'), copy_other_pathname },
1691 { 't', copy_current_tagged },
1692 { XCTRL('t'), copy_other_tagged },
1693 #endif
1694 { 'c', chmod_cmd },
1695 #ifndef OS2_NT
1696 { 'o', chown_cmd },
1697 { 'l', link_cmd },
1698 { XCTRL('l'), other_symlink_cmd },
1699 { 's', symlink_cmd },
1700 { XCTRL('s'), edit_symlink_cmd },
1701 { 'r', copy_current_readlink },
1702 { XCTRL('r'), copy_other_readlink },
1703 #endif
1704 #ifndef HAVE_GNOME
1705 { 'i', info_cmd_no_menu },
1706 { 'q', quick_cmd_no_menu },
1707 #endif
1708 { 'h', add2hotlist_cmd },
1709 { '!', external_panelize },
1710 #ifdef WITH_BACKGROUND
1711 { 'j', jobs_cmd },
1712 #endif
1713 #ifdef HAVE_SETSOCKOPT
1714 { '%', source_routing },
1715 #endif
1716 { 0, 0 }
1717 };
1718
1719 static int ctl_x_map_enabled = 0;
1720
1721 static void ctl_x_cmd (int ignore)
1722 {
1723 ctl_x_map_enabled = 1;
1724 }
1725
1726 static void nothing ()
1727 {
1728 }
1729
1730 static key_map default_map [] = {
1731 #ifndef HAVE_GNOME
1732 { KEY_F(19), menu_last_selected_cmd },
1733 { KEY_F(20), (key_callback) quiet_quit_cmd },
1734
1735 /* Copy useful information to the command line */
1736 { ALT('\n'), copy_prog_name },
1737 { ALT('\r'), copy_prog_name },
1738 { ALT('a'), copy_current_pathname },
1739 { ALT('A'), copy_other_pathname },
1740
1741 { ALT('c'), quick_cd_cmd },
1742
1743 /* To access the directory hotlist */
1744 { XCTRL('\\'), quick_chdir_cmd },
1745
1746 /* Suspend */
1747 { XCTRL('z'), suspend_cmd },
1748 #endif
1749 /* The filtered view command */
1750 { ALT('!'), filtered_view_cmd_cpanel },
1751
1752 /* Find file */
1753 { ALT('?'), find_cmd },
1754
1755 /* Panel refresh */
1756 { XCTRL('r'), reread_cmd },
1757
1758 { ALT('t'), toggle_listing_cmd },
1759
1760 #ifndef HAVE_X
1761 /* Swap panels */
1762 { XCTRL('u'), swap_cmd },
1763
1764 /* View output */
1765 { XCTRL('o'), view_other_cmd },
1766 #endif
1767
1768 /* Control-X keybindings */
1769 { XCTRL('x'), ctl_x_cmd },
1770
1771 /* Trap dlg's exit commands */
1772 { ESC_CHAR, nothing },
1773 { XCTRL('c'), nothing },
1774 { XCTRL('g'), nothing },
1775 { 0, 0 },
1776 };
1777 #endif
1778
1779 #ifndef HAVE_X
1780 static void setup_sigwinch ()
1781 {
1782 #ifndef OS2_NT
1783 struct sigaction act, oact;
1784
1785 # if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
1786 # ifdef SIGWINCH
1787 act.sa_handler = flag_winch;
1788 sigemptyset (&act.sa_mask);
1789 act.sa_flags = 0;
1790 # ifdef SA_RESTART
1791 act.sa_flags |= SA_RESTART;
1792 # endif
1793 sigaction (SIGWINCH, &act, &oact);
1794 # endif
1795 # endif
1796 #endif
1797 }
1798
1799 static void
1800 setup_pre ()
1801 {
1802 /* Call all the inits */
1803 #ifndef HAVE_SLANG
1804 meta (stdscr, eight_bit_clean);
1805 #else
1806 SLsmg_Display_Eight_Bit = full_eight_bits ? 128 : 160;
1807 #endif
1808 }
1809 #else
1810 #define setup_pre()
1811 #define setup_sigwinch()
1812 #endif
1813
1814 static void
1815 setup_post ()
1816 {
1817 setup_sigwinch ();
1818
1819 init_uid_gid_cache ();
1820
1821 #ifndef HAVE_X
1822 if (baudrate () < 9600 || slow_terminal){
1823 verbose = 0;
1824 }
1825 if (use_mouse_p)
1826 init_mouse ();
1827 #endif
1828
1829 midnight_colors [0] = 0;
1830 midnight_colors [1] = REVERSE_COLOR; /* FOCUSC */
1831 midnight_colors [2] = INPUT_COLOR; /* HOT_NORMALC */
1832 midnight_colors [3] = NORMAL_COLOR; /* HOT_FOCUSC */
1833 }
1834
1835 static void setup_mc (void)
1836 {
1837 setup_pre ();
1838 init_menu ();
1839 create_panels ();
1840
1841 #ifdef HAVE_GNOME
1842 return;
1843 #endif
1844 setup_panels ();
1845
1846 #ifdef HAVE_SUBSHELL_SUPPORT
1847 if (use_subshell)
1848 add_select_channel (subshell_pty, load_prompt, 0);
1849 #endif
1850
1851 setup_post ();
1852 }
1853
1854 static void setup_dummy_mc (const char *file)
1855 {
1856 char d[MC_MAXPATHLEN];
1857
1858 mc_get_current_wd (d, MC_MAXPATHLEN);
1859 setup_mc ();
1860 mc_chdir (d);
1861
1862 /* Create a fake current_panel, this is needed because the
1863 * expand_format routine will use current panel.
1864 */
1865 strcpy (cpanel->cwd, d);
1866 cpanel->selected = 0;
1867 cpanel->count = 1;
1868 cpanel->dir.list[0].fname = (char *) file;
1869 }
1870
1871 static void done_mc ()
1872 {
1873 done_menu ();
1874
1875 /* Setup shutdown
1876 *
1877 * We sync the profiles since the hotlist may have changed, while
1878 * we only change the setup data if we have the auto save feature set
1879 */
1880 if (auto_save_setup)
1881 save_setup (); /* does also call save_hotlist */
1882 else
1883 save_hotlist();
1884 done_screen ();
1885 vfs_add_current_stamps ();
1886 if (xterm_flag && xterm_hintbar)
1887 set_hintbar(_("Thank you for using GNU Midnight Commander"));
1888 }
1889
1890 /* This should be called after destroy_dlg since panel widgets
1891 * save their state on the profiles
1892 */
1893 static void done_mc_profile ()
1894 {
1895 if (!auto_save_setup)
1896 profile_forget_profile (profile_name);
1897 sync_profiles ();
1898 done_setup ();
1899 free_profiles ();
1900 }
1901
1902 /* This routine only handles cpanel, and opanel, it is easy to
1903 * change to use npanels, just loop over the number of panels
1904 * and use get_panel_widget (i) and make the test done below
1905 */
1906 void make_panels_dirty ()
1907 {
1908 if (cpanel->dirty)
1909 panel_update_contents (cpanel);
1910
1911 if ((get_other_type () == view_listing) && opanel->dirty)
1912 panel_update_contents (opanel);
1913 }
1914
1915 /* In OS/2 and Windows NT people want to actually type the '\' key frequently */
1916 #ifdef OS2_NT
1917 # define check_key_backslash(x) 0
1918 #else
1919 # define check_key_backslash(x) ((x) == '\\')
1920 #endif
1921
1922 int midnight_callback (struct Dlg_head *h, int id, int msg)
1923 {
1924 int i;
1925
1926 switch (msg){
1927 #ifndef HAVE_XVIEW
1928
1929 /* Speed up routine: now, we just set the */
1930 case DLG_PRE_EVENT:
1931 make_panels_dirty ();
1932 return MSG_HANDLED;
1933
1934 case DLG_KEY:
1935 if (ctl_x_map_enabled){
1936 ctl_x_map_enabled = 0;
1937 for (i = 0; ctl_x_map [i].key_code; i++)
1938 if (id == ctl_x_map [i].key_code){
1939 (*ctl_x_map [i].fn)(id);
1940 return MSG_HANDLED;
1941 }
1942 }
1943
1944 if (id == KEY_F(10) && !the_menubar->active){
1945 quit_cmd ();
1946 return MSG_HANDLED;
1947 }
1948
1949 if (id == '\t')
1950 free_completions (input_w (cmdline));
1951
1952 /* On Linux, we can tell the difference */
1953 if (id == '\n' && ctrl_pressed ()){
1954 copy_prog_name ();
1955 return MSG_HANDLED;
1956 }
1957
1958 if (id == '\n' && input_w (cmdline)->buffer [0]){
1959 send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
1960 return MSG_HANDLED;
1961 }
1962
1963 if ((!alternate_plus_minus || !(console_flag || xterm_flag)) &&
1964 !quote && !cpanel->searching) {
1965 if(!only_leading_plus_minus) {
1966 /* Special treatement, since the input line will eat them */
1967 if (id == '+' ) {
1968 select_cmd ();
1969 return MSG_HANDLED;
1970 }
1971
1972 if (check_key_backslash (id) || id == '-'){
1973 unselect_cmd ();
1974 return MSG_HANDLED;
1975 }
1976
1977 if (id == '*') {
1978 reverse_selection_cmd ();
1979 return MSG_HANDLED;
1980 }
1981 } else if (command_prompt && !strlen (input_w (cmdline)->buffer)) {
1982 /* Special treatement '+', '-', '\', '*' only when this is
1983 * first char on input line
1984 */
1985
1986 if (id == '+') {
1987 select_cmd ();
1988 return MSG_HANDLED;
1989 }
1990
1991 if (check_key_backslash (id) || id == '-') {
1992 unselect_cmd ();
1993 return MSG_HANDLED;
1994 }
1995
1996 if (id == '*') {
1997 reverse_selection_cmd ();
1998 return MSG_HANDLED;
1999 }
2000 }
2001 }
2002 break;
2003
2004 case DLG_HOTKEY_HANDLED:
2005 if (get_current_type () == view_listing)
2006 cpanel->searching = 0;
2007 break;
2008
2009 case DLG_UNHANDLED_KEY:
2010 if (command_prompt){
2011 int v;
2012
2013 v = send_message_to (h, (Widget *) cmdline, WIDGET_KEY, id);
2014 if (v)
2015 return v;
2016 }
2017 if (ctl_x_map_enabled){
2018 ctl_x_map_enabled = 0;
2019 for (i = 0; ctl_x_map [i].key_code; i++)
2020 if (id == ctl_x_map [i].key_code){
2021 (*ctl_x_map [i].fn)(id);
2022 return MSG_HANDLED;
2023 }
2024 } else {
2025 for (i = 0; default_map [i].key_code; i++){
2026 if (id == default_map [i].key_code){
2027 (*default_map [i].fn)(id);
2028 return MSG_HANDLED;
2029 }
2030 }
2031 }
2032 return MSG_NOT_HANDLED;
2033
2034 #endif
2035 #ifndef HAVE_X
2036 /* We handle the special case of the output lines */
2037 case DLG_DRAW:
2038 attrset (SELECTED_COLOR);
2039 if (console_flag && output_lines)
2040 show_console_contents (output_start_y,
2041 LINES-output_lines-keybar_visible-1,
2042 LINES-keybar_visible-1);
2043 break;
2044 #endif
2045
2046 }
2047 return default_dlg_callback (h, id, msg);
2048 }
2049
2050 #ifdef HAVE_X
2051 /* This should be rewritten in order to support as many panel containers as
2052 the user wants */
2053
2054 #ifndef HAVE_GNOME
2055 widget_data containers [2];
2056 int containers_no = 2;
2057
2058 void
2059 xtoolkit_panel_setup ()
2060 {
2061 containers [0] = x_create_panel_container (0);
2062 containers [1] = x_create_panel_container (1);
2063 input_w (cmdline)->widget.wcontainer = containers [0];
2064 input_w (cmdline)->widget.area = AREA_BOTTOM;
2065 the_prompt->widget.wcontainer = containers [0];
2066 the_prompt->widget.area = AREA_BOTTOM;
2067 the_bar->widget.wcontainer = containers [0];
2068 the_bar->widget.area = AREA_TOP;
2069 #ifdef HAVE_XVIEW
2070 the_bar2->widget.wcontainer = containers [1];
2071 the_bar2->widget.area = AREA_TOP;
2072 #endif
2073 get_panel_widget (0)->wcontainer = containers [0];
2074 get_panel_widget (0)->area = AREA_RIGHT;
2075 get_panel_widget (1)->wcontainer = containers [1];
2076 get_panel_widget (1)->area = AREA_RIGHT;
2077 the_menubar->widget.wcontainer = (widget_data) NULL;
2078 }
2079 #else
2080 # define xtoolkit_panel_setup()
2081 #endif
2082
2083 #else
2084 # define xtoolkit_panel_setup()
2085 #endif
2086
2087 #ifndef PORT_HAS_LOAD_HINT
2088 void load_hint ()
2089 {
2090 char *hint;
2091
2092 if (!the_hint->widget.parent)
2093 return;
2094
2095 if (!message_visible && (!xterm_flag || !xterm_hintbar)){
2096 label_set_text (the_hint, 0);
2097 return;
2098 }
2099
2100 if ((hint = get_random_hint ())){
2101 if (*hint)
2102 set_hintbar (hint);
2103 free (hint);
2104 } else {
2105 set_hintbar ("The Midnight Commander " VERSION
2106 " (C) 1995-1997 the Free Software Foundation");
2107 }
2108 }
2109 #endif
2110
2111 static void
2112 setup_panels_and_run_mc ()
2113 {
2114 int first, second;
2115
2116 xtoolkit_panel_setup ();
2117 tk_new_frame (midnight_dlg, "p.");
2118 #ifndef HAVE_X
2119 add_widget (midnight_dlg, the_hint);
2120 #endif /* HAVE_X */
2121 load_hint ();
2122 add_widgetl (midnight_dlg, cmdline, XV_WLAY_RIGHTOF);
2123 add_widgetl (midnight_dlg, the_prompt, XV_WLAY_DONTCARE);
2124 tk_end_frame ();
2125 add_widget (midnight_dlg, the_bar);
2126 #ifdef HAVE_XVIEW
2127 add_widget (midnight_dlg, the_bar2);
2128 #endif
2129 if (boot_current_is_left){
2130 first = 1;
2131 second = 0;
2132 } else {
2133 first = 0;
2134 second = 1;
2135 }
2136 add_widget (midnight_dlg, get_panel_widget (first));
2137 add_widget (midnight_dlg, get_panel_widget (second));
2138 add_widget (midnight_dlg, the_menubar);
2139
2140 init_labels (get_panel_widget (0));
2141 init_labels (get_panel_widget (1));
2142
2143 /* Run the Midnight Commander if no file was specified in the command line */
2144 run_dlg (midnight_dlg);
2145 }
2146
2147 /* result must be free'd (I think this should go in util.c) */
2148 char *
2149 prepend_cwd_on_local (char *filename)
2150 {
2151 char *d;
2152 int l;
2153
2154 if (vfs_file_is_local (filename)){
2155 if (*filename == PATH_SEP) /* an absolute pathname */
2156 return strdup (filename);
2157 d = malloc (MC_MAXPATHLEN + strlen (filename) + 2);
2158 mc_get_current_wd (d, MC_MAXPATHLEN);
2159 l = strlen(d);
2160 d[l++] = PATH_SEP;
2161 strcpy (d + l, filename);
2162 return canonicalize_pathname (d);
2163 } else
2164 return strdup (filename);
2165 }
2166
2167 #ifdef USE_INTERNAL_EDIT
2168 void edit (const char *file_name, int startline);
2169
2170 int
2171 mc_maybe_editor_or_viewer (void)
2172 {
2173 char *path;
2174
2175 if (!(view_one_file || edit_one_file))
2176 return 0;
2177
2178 /* Invoke the internal view/edit routine with:
2179 * the default processing and forcing the internal viewer/editor
2180 */
2181 if (view_one_file) {
2182 path = prepend_cwd_on_local (view_one_file);
2183 setup_dummy_mc (path);
2184 view_file (path, 0, 1);
2185 }
2186 #ifdef USE_INTERNAL_EDIT
2187 else {
2188 path = prepend_cwd_on_local ("");
2189 setup_dummy_mc (path);
2190 edit (edit_one_file, 1);
2191 }
2192 #endif
2193 free (path);
2194 midnight_shutdown = 1;
2195 done_mc ();
2196 return 1;
2197 }
2198
2199 #endif
2200
2201 static void
2202 do_nc (void)
2203 {
2204 midnight_dlg = create_dlg (0, 0, LINES, COLS, midnight_colors, midnight_callback, "[main]", "midnight", 0);
2205 midnight_dlg->has_menubar = 1;
2206
2207 #ifdef USE_INTERNAL_EDIT
2208 /* Check if we were invoked as an editor or file viewer */
2209 if (mc_maybe_editor_or_viewer ())
2210 return;
2211 #endif
2212
2213 setup_mc ();
2214
2215 #ifndef HAVE_GNOME
2216 setup_panels_and_run_mc ();
2217 #endif
2218
2219 /* Program end */
2220 midnight_shutdown = 1;
2221
2222 /* destroy_dlg destroys even cpanel->cwd, so we have to save a copy :) */
2223 if (print_last_wd) {
2224 if (!vfs_current_is_local ())
2225 last_wd_string = strdup (".");
2226 else
2227 last_wd_string = strdup (cpanel->cwd);
2228 }
2229 done_mc ();
2230
2231 #ifndef HAVE_GNOME
2232 destroy_dlg (midnight_dlg);
2233 current_panel = 0;
2234 #endif
2235 done_mc_profile ();
2236 }
2237
2238 #include "features.inc"
2239
2240 static void
2241 version (int verbose)
2242 {
2243 fprintf (stderr, "The Midnight Commander %s\n", VERSION);
2244 if (!verbose)
2245 return;
2246
2247 #ifndef HAVE_X
2248 fprintf (stderr,
2249 _("with mouse support on xterm%s.\n"),
2250 status_mouse_support ? _(" and the Linux console") : "");
2251 #endif /* HAVE_X */
2252
2253 fprintf (stderr, features);
2254 if (print_last_wd)
2255 write (stdout_fd, ".", 1);
2256 }
2257
2258 #if defined (_OS_NT)
2259 /* Windows NT code */
2260 #define CONTROL_FILE "\\mc.%d.control"
2261 char control_file [sizeof (CONTROL_FILE) + 8];
2262
2263 void
2264 OS_Setup ()
2265 {
2266 SetConsoleTitle ("GNU Midnight Commander");
2267
2268 shell = getenv ("COMSPEC");
2269 if (!shell || !*shell)
2270 shell = get_default_shell ();
2271
2272 /* Default opening mode for files is binary, not text (CR/LF translation) */
2273 #ifndef __EMX__
2274 _fmode = O_BINARY;
2275 #endif
2276
2277 mc_home = get_mc_lib_dir ();
2278 }
2279
2280 static void
2281 sigchld_handler_no_subshell (int sig)
2282 {
2283 }
2284
2285 void
2286 init_sigchld (void)
2287 {
2288 }
2289
2290 void
2291 init_sigfatals (void)
2292 {
2293 /* Nothing to be done on the OS/2, Windows/NT */
2294 }
2295
2296
2297 #elif defined (__os2__)
2298 /* OS/2 Version */
2299 # define CONTROL_FILE "\\mc.%d.control"
2300 char control_file [sizeof (CONTROL_FILE) + 8];
2301
2302 void
2303 OS_Setup (void)
2304 {
2305 /* .ado: This does not work: */
2306 /* DosSMSetTitle ((PSZ) "This is my app"); */
2307 /* In DEF: IMPORTS
2308 DosSMSetTitle = SESMGR.DOSSMSETTITLE */
2309 shell = getenv ("COMSPEC");
2310 if (!shell || !*shell)
2311 shell = get_default_shell ();
2312
2313 mc_home = get_mc_lib_dir ();
2314 }
2315
2316 static void
2317 sigchld_handler_no_subshell (int sig)
2318 {
2319 }
2320
2321 void
2322 init_sigchld (void)
2323 {
2324 }
2325
2326 void
2327 init_sigfatals (void)
2328 {
2329 /* Nothing to be done on the OS/2, Windows/NT */
2330 }
2331 #else
2332
2333 /* Unix version */
2334 #define CONTROL_FILE "/tmp/mc.%d.control"
2335 char control_file [sizeof (CONTROL_FILE) + 8];
2336
2337 void
2338 OS_Setup ()
2339 {
2340 char *termvalue;
2341 char *mc_libdir;
2342
2343 termvalue = getenv ("TERM");
2344 if (!termvalue){
2345 fprintf (stderr, _("The TERM environment variable is unset!\n"));
2346 termvalue = "";
2347 }
2348 #ifndef HAVE_X
2349 if (force_xterm || (strncmp (termvalue, "xterm", 5) == 0 || strcmp (termvalue, "dtterm") == 0)){
2350 use_mouse_p = XTERM_MOUSE;
2351 xterm_flag = 1;
2352 # ifdef SET_TITLE
2353 printf ("\33]0;GNU Midnight Commander\7");
2354 # endif
2355 }
2356 #endif /* ! HAVE_X */
2357 shell = getenv ("SHELL");
2358 if (!shell || !*shell)
2359 shell = strdup (getpwuid (geteuid ())->pw_shell);
2360 if (!shell || !*shell)
2361 shell = "/bin/sh";
2362
2363 sprintf (control_file, CONTROL_FILE, getpid ());
2364 my_putenv ("MC_CONTROL_FILE", control_file);
2365
2366 /* This is the directory, where MC was installed, on Unix this is LIBDIR */
2367 /* and can be overriden by the MCHOME environment variable */
2368 if ((mc_libdir = getenv ("MCHOME")) != NULL) {
2369 mc_home = strdup (mc_libdir);
2370 } else {
2371 mc_home = strdup (LIBDIR);
2372 }
2373 }
2374
2375 static void
2376 sigchld_handler_no_subshell (int sig)
2377 {
2378 #ifndef HAVE_X
2379 int pid, status;
2380
2381 if (!console_flag)
2382 return;
2383
2384 /* COMMENT: if it were true that after the call to handle_console(..INIT)
2385 the value of console_flag never changed, we could simply not install
2386 this handler at all if (!console_flag && !use_subshell). */
2387
2388 /* That comment is no longer true. We need to wait() on a sigchld
2389 handler (that's at least what the tarfs code expects currently). */
2390
2391 #ifndef SCO_FLAVOR
2392 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
2393
2394 if (pid == cons_saver_pid){
2395 /* {{{ Someone has stopped or killed cons.saver; restart it */
2396
2397 if (WIFSTOPPED (status))
2398 kill (pid, SIGCONT);
2399 else
2400 {
2401 handle_console (CONSOLE_DONE);
2402 handle_console (CONSOLE_INIT);
2403 }
2404 /* }}} */
2405 }
2406 #endif /* ! SCO_FLAVOR */
2407
2408 /* If we get here, some other child exited; ignore it */
2409 #endif /* ! HAVE_X */
2410 }
2411
2412 #if 0
2413 void
2414 mc_fatal_signal (int signum)
2415 {
2416 volatile int x;
2417 char cmd;
2418 pid_t pid;
2419 char *args [4] = { "gdb", NULL, NULL, NULL };
2420 char pid [20];
2421
2422 sprintf (buf, "%d", getpid ());
2423 fprintf (stderr,
2424 "Midnight Commander fatal error, PID=%d\n"
2425 "What to do: [e]xit, [s]tack trace, [a]ttach to process\n", getpid ());
2426
2427 read (1, &cmd, 1);
2428 if (cmd == 'a'){
2429 for (x = 1; x;)
2430 ;
2431 }
2432 if (cmd == 's'){
2433 args [1] = program_name;
2434 args [2] = buf;
2435 pid = fork ();
2436 if (pid == -1){
2437 fprintf (stderr, "Could not fork, exiting\n");
2438 exit (0);
2439 }
2440 if (pid == 0){
2441 stack_trace (args);
2442 } else {
2443 while (1)
2444 ;
2445 }
2446 }
2447 exit (0);
2448 }
2449
2450 void
2451 init_sigfatals (void)
2452 {
2453 struct sigaction sa;
2454
2455 sa.sa_hanlder = mc_fatal_signal;
2456 sa.sa_mask = 0;
2457 sa.sa_flags = 0;
2458
2459 sigaction (SIGSEGV, &sa, NULL);
2460 sigaction (SIGBUS, &sa, NULL);
2461 sigaction (SIGFPE, &sa, NULL);
2462 }
2463 #else
2464 #define init_sigfatals()
2465 #endif
2466
2467 void
2468 init_sigchld (void)
2469 {
2470 struct sigaction sigchld_action;
2471
2472 sigchld_action.sa_handler =
2473 #ifdef HAVE_SUBSHELL_SUPPORT
2474 use_subshell ? sigchld_handler :
2475 #endif
2476 sigchld_handler_no_subshell;
2477
2478 sigemptyset (&sigchld_action.sa_mask);
2479
2480 #ifdef SA_RESTART
2481 sigchld_action.sa_flags = SA_RESTART;
2482 #else
2483 sigchld_action.sa_flags = 0;
2484 #endif
2485
2486 sigaction (SIGCHLD, &sigchld_action, NULL);
2487 }
2488
2489 #endif /* _OS_NT, __os2__, UNIX */
2490
2491 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2492 extern int SLtt_Try_Termcap;
2493 #endif
2494
2495 #ifndef PORT_WANTS_ARGP
2496 static void
2497 print_mc_usage (void)
2498 {
2499 version (0);
2500 fprintf (stderr,
2501 "Usage is:\n\n"
2502 "mc [flags] [this_dir] [other_panel_dir]\n\n"
2503 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2504 "-a, --stickchars Force use of +, -, | for line drawing.\n"
2505 #endif
2506 "-b, --nocolor Force black and white display.\n"
2507 #ifdef WITH_BACKGROUND
2508 "-B, --background [DEVEL-ONLY: Debug the background code]\n"
2509 #endif
2510 "-c, --color Force color mode.\n"
2511 "-C, --colors Specify colors (use --help-colors to get a list).\n"
2512 #ifdef USE_INTERNAL_EDIT
2513 "-e, --edit Startup the internal editor.\n"
2514 #endif
2515 "-d, --nomouse Disable mouse support.\n"
2516 "-f, --libdir Print configured paths.\n"
2517 "-h, --help Shows this help message.\n"
2518 "-k, --resetsoft Reset softkeys (HP terminals only) to their terminfo/termcap\n"
2519 " default.\n"
2520 "-P, --printwd At exit, print the last working directory.\n"
2521 "-s, --slow Disables verbose operation (for slow terminals).\n"
2522 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2523 "-t, --termcap Activate support for the TERMCAP variable.\n"
2524 #endif
2525 #ifdef USE_NETCODE
2526 "-l, --ftplog file Log ftpfs commands to the file.\n"
2527 #endif
2528 #if defined(HAVE_SLANG) && defined(OS2_NT)
2529 "-S, --createcmdile Create command file to set default directory upon exit.\n"
2530 #endif
2531
2532 #ifdef HAVE_SUBSHELL_SUPPORT
2533 "-u, --nosubshell Disable the concurrent subshell mode.\n"
2534 "-U, --subshell Force the concurrent subshell mode.\n"
2535 "-r, --forceexec Force subshell execution.\n"
2536 #endif
2537 "-v, --view fname Start up into the viewer mode.\n"
2538 "-V, --version Report version and configuration options.\n"
2539 "-x, --xterm Force xterm mouse support and screen save/restore.\n"
2540 #ifdef HAVE_SUBSHELL_SUPPORT
2541 "-X, --dbgsubshell [DEVEL-ONLY: Debug the subshell].\n"
2542 #endif
2543 );
2544 }
2545 #endif /* PORT_WANTS_ARGP */
2546
2547 static void
2548 print_color_usage (void)
2549 {
2550 fprintf (stderr,
2551 "--colors KEYWORD={FORE},{BACK}\n\n"
2552 "{FORE} and {BACK} can be ommited, and the default will be used\n"
2553 "\n"
2554 "Keywords:\n"
2555 " Global: errors, reverse, gauge, input\n"
2556 " File display: normal, selected, marked, markselect\n"
2557 " Dialog boxes: dnormal, dfocus, dhotnormal, dhotfocus\n"
2558 " Menus: menu, menuhot, menusel, menuhotsel\n"
2559 " Help: helpnormal, helpitalic, helplink, helpslink\n"
2560 " File types: directory, execute, link, device, special, core\n"
2561 "\n"
2562 "Colors:\n"
2563 " black, gray, red, brightred, green, brightgreen, brown,\n"
2564 " yellow, blue, brightblue, magenta, brightmagenta, cyan,\n"
2565 " brightcyan, lightgray and white\n\n");
2566
2567 }
2568
2569
2570 static void
2571 probably_finish_program (void)
2572 {
2573 if (finish_program){
2574 if (print_last_wd)
2575 printf (".");
2576 exit (1);
2577 }
2578 }
2579
2580 enum {
2581 GEOMETRY_KEY = -1,
2582 NOWIN_KEY = -2
2583 };
2584
2585 static void
2586 process_args (int c, char *option_arg)
2587 {
2588 switch (c) {
2589 case 'V':
2590 version (1);
2591 finish_program = 1;
2592 break;
2593
2594 case 'c':
2595 disable_colors = 0;
2596 #ifdef HAVE_SLANG
2597 force_colors = 1;
2598 #endif
2599 break;
2600
2601 case 'f':
2602 fprintf (stderr, _("Library directory for the Midnight Commander: %s\n"), mc_home);
2603 finish_program = 1;
2604 break;
2605
2606 case 'm':
2607 fprintf (stderr, _("Option -m is obsolete. Please look at Display Bits... in the Option's menu\n"));
2608 finish_program = 1;
2609 break;
2610
2611 #ifdef USE_NETCODE
2612 case 'l':
2613 ftpfs_set_debug (option_arg);
2614 break;
2615 #endif
2616
2617 #ifdef OS2_NT
2618 case 'S':
2619 print_last_wd = 2;
2620 batch_file_name = option_arg;
2621 break;
2622 #endif
2623
2624 case 'd':
2625 use_mouse_p = NO_MOUSE;
2626 break;
2627
2628 case 'X':
2629 #ifdef HAVE_SUBSHELL_SUPPORT
2630 debug_subshell = 1;
2631 #endif
2632 break;
2633
2634 case 'U':
2635 #ifdef HAVE_SUBSHELL_SUPPORT
2636 use_subshell = 1;
2637 #endif
2638 break;
2639
2640 case 'u':
2641 #ifdef HAVE_SUBSHELL_SUPPORT
2642 use_subshell = 0;
2643 #endif
2644 break;
2645
2646 case 'r':
2647 #ifdef HAVE_SUBSHELL_SUPPORT
2648 force_subshell_execution = 1;
2649 #endif
2650 break;
2651
2652 case 'H':
2653 print_color_usage ();
2654 finish_program = 1;
2655 break;
2656
2657 case 'h':
2658 #ifndef PORT_WANTS_ARGP
2659 print_mc_usage ();
2660 finish_program = 1;
2661 #endif
2662 }
2663 }
2664
2665 #ifdef PORT_WANTS_ARGP
2666 static struct argp_option argp_options [] = {
2667 #ifdef WITH_BACKGROUND
2668 { "background", 'B', NULL, 0, N_("[DEVEL-ONLY: Debug the background code]"), 0 },
2669 #endif
2670 #if defined(HAVE_SLANG) && defined(OS2_NT)
2671 { "createcmdfile", 'S', "CMDFILE", , 0, N_("Create command file to set default directory upon exit."), 1 },
2672 #endif
2673 { "color", 'c', NULL, 0, N_("Force color mode."), 0 },
2674 { "colors", 'C', "COLORS", 0, N_("Specify colors (use --help-colors to get a list)."), 1 },
2675 #ifdef HAVE_SUBSHELL_SUPPORT
2676 { "dbgsubshell", 'X', NULL, 0, N_("[DEVEL-ONLY: Debug the subshell."), 0 },
2677 #endif
2678 { "edit", 'e', "EDIT", 0, N_("Startup the internal editor."), 1 },
2679 { "help", 'h', NULL, 0, N_("Shows this help message."), 0 },
2680 { "help-colors", 'H', NULL, 0, N_("Help on how to specify colors."), 0 },
2681 #ifdef USE_NETCODE
2682 { "ftplog", 'l', "FTPLOG", 0, N_("Log ftpfs commands to the file."), 1 },
2683 #endif
2684 { "libdir", 'f', NULL, 0, N_("Prints out the configured paths."), 0 },
2685 { NULL, 'm', NULL, OPTION_HIDDEN, NULL, 0 },
2686 { "nocolor", 'b', NULL, 0, N_("Force black and white display."), 0 },
2687 { "nomouse", 'd', NULL, 0, N_("Disable mouse support."), 0 },
2688 #ifdef HAVE_SUBSHELL_SUPPORT
2689 { "subshell", 'U', NULL, 0, N_("Force the concurrent subshell mode"), 0 },
2690 { "nosubshell", 'u', NULL, 0, N_("Disable the concurrent subshell mode."), 0 },
2691 { "forceexec", 'r', NULL, 0, N_("Force subshell execution."), 0 },
2692 #endif
2693 { "printwd", 'P', NULL, 0, N_("At exit, print the last working directory."), 0 },
2694 { "resetsoft", 'k', NULL, 0, N_("Reset softkeys (HP terminals only) to their terminfo/termcap default."), 0},
2695 { "slow", 's', NULL, 0, N_("Disables verbose operation (for slow terminals)."), 0 },
2696 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2697 { "stickchars", 'a', NULL, 0, N_("Use simple symbols for line drawing."), 0 },
2698 #endif
2699 #ifdef HAVE_SUBSHELL_SUPPORT
2700 #endif
2701 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2702 { "termcap", 't', NULL, 0, N_("Activate support for the TERMCAP variable."), 0 },
2703 #endif
2704 { "version", 'V', NULL, 0, N_("Report versionand configuration options."), 0 },
2705 { "view", 'v', NULL, 0, N_("Start up into the viewer mode."), 0 },
2706 { "xterm", 'x', NULL, 0, N_("Force xterm mouse support and screen save/restore"), 0 },
2707 { "geometry", GEOMETRY_KEY, "GEOMETRY", 0, N_("Geometry for the window"), 0 },
2708 { "nowindows", NOWIN_KEY, NULL, 0, N_("No windows opened at startup"), 0 },
2709 { NULL, 0, NULL, 0, NULL },
2710 };
2711
2712 GList *directory_list = 0;
2713 GList *geometry_list = 0;
2714 int nowindows;
2715
2716 static error_t
2717 parse_an_arg (int key, char *arg, struct argp_state *state)
2718 {
2719 switch (key){
2720 #ifdef WITH_BACKGROUND
2721 case 'B':
2722 background_wait = 1;
2723 return 0;
2724 #endif
2725 case 'b':
2726 disable_colors = 1;
2727 return 0;
2728
2729 case 'P':
2730 print_last_wd = 1;
2731 return 0;
2732
2733 case 'k':
2734 reset_hp_softkeys = 1;
2735 return 0;
2736
2737 case 's':
2738 slow_terminal = 1;
2739 return 0;
2740
2741 case 'a':
2742 force_ugly_line_drawing = 1;
2743 return 0;
2744
2745 case 'x':
2746 force_xterm = 1;
2747 return 0;
2748
2749 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2750 case 't':
2751 SLtt_Try_Termcap = 1;
2752 return 0;
2753 #endif
2754
2755 case GEOMETRY_KEY:
2756 geometry_list = g_list_append (geometry_list, arg);
2757 return 0;
2758
2759 case NOWIN_KEY:
2760 nowindows = 1;
2761
2762 case ARGP_KEY_ARG:
2763 break;
2764
2765 case ARGP_KEY_INIT:
2766 case ARGP_KEY_FINI:
2767 return 0;
2768
2769 default:
2770 process_args (key, arg);
2771 }
2772
2773 if (arg){
2774 if (edit_one_file)
2775 edit_one_file = strdup (arg);
2776 else if (view_one_file)
2777 view_one_file = strdup (arg);
2778 else
2779 directory_list = g_list_append (directory_list, arg);
2780 }
2781 return 0;
2782 }
2783
2784 static struct argp mc_argp_parser = {
2785 argp_options, parse_an_arg, N_("[this dir] [other dir]"), NULL, NULL, NULL, NULL
2786 };
2787
2788 #else
2789
2790 static struct poptOption argumentTable[] = {
2791 #ifdef WITH_BACKGROUND
2792 { "background", 'B', POPT_ARG_NONE, &background_wait, 0 },
2793 #endif
2794 #if defined(HAVE_SLANG) && defined(OS2_NT)
2795 { "createcmdfile", 'S', POPT_ARG_STRING, NULL, 'S' },
2796 #endif
2797 { "color", 'c', POPT_ARG_NONE, NULL, 'c' },
2798 { "colors", 'C', POPT_ARG_STRING, &command_line_colors, 0 },
2799 #ifdef HAVE_SUBSHELL_SUPPORT
2800 { "dbgsubshell", 'X', POPT_ARG_NONE, &debug_subshell, 0 },
2801 #endif
2802 { "edit", 'e', POPT_ARG_STRING, &edit_one_file, 0 },
2803
2804 { "help", 'h', POPT_ARG_NONE, NULL, 'h' },
2805 { "help-colors", 'H', POPT_ARG_NONE, NULL, 'H' },
2806 #ifdef USE_NETCODE
2807 { "ftplog", 'l', POPT_ARG_STRING, NULL, 'l' },
2808 #endif
2809 { "libdir", 'f', POPT_ARG_NONE, NULL, 'f' },
2810 { NULL, 'm', POPT_ARG_NONE, NULL, 'm' },
2811 { "nocolor", 'b', POPT_ARG_NONE, &disable_colors, 0 },
2812 { "nomouse", 'd', POPT_ARG_NONE, NULL, 'd' },
2813 #ifdef HAVE_SUBSHELL_SUPPORT
2814 { "nosubshell", 'u', POPT_ARG_NONE, NULL, 'u' },
2815 { "forceexec", 'r', POPT_ARG_NONE, NULL, 'r' },
2816 #endif
2817 { "printwd", 'P', POPT_ARG_NONE, &print_last_wd, 0 },
2818 { "resetsoft", 'k', POPT_ARG_NONE, &reset_hp_softkeys, 0 },
2819 { "slow", 's', POPT_ARG_NONE, &slow_terminal, 0 },
2820 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2821 { "stickchars", 'a', 0, &force_ugly_line_drawing, 0 },
2822 #endif
2823 #ifdef HAVE_SUBSHELL_SUPPORT
2824 { "subshell", 'U', POPT_ARG_NONE, NULL, 'U' },
2825 #endif
2826 #if defined(HAVE_SLANG) && !defined(OS2_NT)
2827 { "termcap", 't', 0, &SLtt_Try_Termcap, 0 },
2828 #endif
2829 { "version", 'V', POPT_ARG_NONE, NULL, 'V'},
2830 { "view", 'v', POPT_ARG_STRING, &view_one_file, 0 },
2831 { "xterm", 'x', POPT_ARG_NONE, &force_xterm, 0},
2832
2833 { NULL, 0, 0, NULL, 0 }
2834 };
2835
2836 static void
2837 handle_args (int argc, char *argv [])
2838 {
2839 char *tmp, *option_arg, *base;
2840 int c;
2841 poptContext optCon;
2842
2843 optCon = poptGetContext ("mc", argc, argv, argumentTable, 0);
2844
2845 #ifdef USE_TERMCAP
2846 SLtt_Try_Termcap = 1;
2847 #endif
2848
2849 while ((c = poptGetNextOpt (optCon)) > 0) {
2850 option_arg = poptGetOptArg(optCon);
2851
2852 process_args (c, option_arg);
2853 }
2854
2855 if (c < -1){
2856 print_mc_usage ();
2857 fprintf(stderr, "%s: %s\n",
2858 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
2859 poptStrerror(c));
2860 finish_program = 1;
2861 }
2862
2863 probably_finish_program ();
2864
2865 /* Check for special invocation names mcedit and mcview,
2866 * if none apply then set the current directory and the other
2867 * directory from the command line arguments
2868 */
2869 tmp = poptGetArg (optCon);
2870 base = x_basename (argv[0]);
2871 if (!STRNCOMP (base, "mce", 3) || !STRCOMP(base, "vi")) {
2872 edit_one_file = "";
2873 if (tmp)
2874 edit_one_file = strdup (tmp);
2875 } else
2876 if (!STRNCOMP (base, "mcv", 3) || !STRCOMP(base, "view")) {
2877 if (tmp)
2878 view_one_file = strdup (tmp);
2879 } else {
2880 /* sets the current dir and the other dir */
2881 if (tmp) {
2882 char buffer[MC_MAXPATHLEN + 2];
2883 this_dir = strdup (tmp);
2884 mc_get_current_wd (buffer, sizeof (buffer) - 2);
2885 if ((tmp = poptGetArg (optCon)))
2886 other_dir = strdup (tmp);
2887 }
2888 }
2889 poptFreeContext(optCon);
2890 }
2891 #endif
2892
2893 /*
2894 * The compatibility_move_mc_files routine is intended to
2895 * move all of the hidden .mc files into a private ~/.mc
2896 * directory in the home directory, to avoid cluttering the users.
2897 *
2898 * Previous versions of the program had all of their files in
2899 * the $HOME, we are now putting them in $HOME/.mc
2900 */
2901 #ifdef OS2_NT
2902 # define compatibility_move_mc_files()
2903 #else
2904
2905 int
2906 do_mc_filename_rename (char *mc_dir, char *o_name, char *n_name)
2907 {
2908 char *full_o_name = concat_dir_and_file (home_dir, o_name);
2909 char *full_n_name = copy_strings (home_dir, MC_BASE, n_name, NULL);
2910 int move;
2911
2912 move = 0 == rename (full_o_name, full_n_name);
2913 free (full_o_name);
2914 free (full_n_name);
2915 return move;
2916 }
2917
2918 void
2919 do_compatibility_move (char *mc_dir)
2920 {
2921 struct stat s;
2922 int move;
2923
2924 if (stat (mc_dir, &s) == 0)
2925 return;
2926 if (errno != ENOENT)
2927 return;
2928
2929 if (mkdir (mc_dir, 0777) == -1)
2930 return;
2931
2932 move = do_mc_filename_rename (mc_dir, ".mc.ini", "ini");
2933 move += do_mc_filename_rename (mc_dir, ".mc.hot", "hotlist");
2934 move += do_mc_filename_rename (mc_dir, ".mc.ext", "ext");
2935 move += do_mc_filename_rename (mc_dir, ".mc.menu", "menu");
2936 move += do_mc_filename_rename (mc_dir, ".mc.bashrc", "bashrc");
2937 move += do_mc_filename_rename (mc_dir, ".mc.inputrc", "inputrc");
2938 move += do_mc_filename_rename (mc_dir, ".mc.tcshrc", "tcshrc");
2939 move += do_mc_filename_rename (mc_dir, ".mc.tree", "tree");
2940
2941 if (!move)
2942 return;
2943
2944 show_change_notice = 1;
2945 }
2946
2947 void
2948 compatibility_move_mc_files (void)
2949 {
2950 char *mc_dir = concat_dir_and_file (home_dir, ".mc");
2951
2952 do_compatibility_move (mc_dir);
2953 free (mc_dir);
2954 }
2955 #endif
2956
2957 int main (int argc, char *argv [])
2958 {
2959 #ifndef OS2_NT
2960 /* Backward compatibility: Gives up privileges in case someone
2961 installed the mc as setuid */
2962 setuid (getuid ());
2963 #else
2964 # define LOCALEDIR get_mc_lib_dir ()
2965 #endif
2966 #ifdef _OS_NT
2967 signal(SIGINT,SIG_IGN); /* Ignore CTRL-C */
2968 #endif
2969 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
2970 setlocale (LC_ALL, "");
2971 bindtextdomain ("mc", LOCALEDIR);
2972 textdomain ("mc");
2973
2974 /* Initialize list of all user group for timur_clr_mode */
2975 init_groups ();
2976
2977 OS_Setup ();
2978
2979 vfs_init ();
2980
2981 #ifdef HAVE_X
2982 /* NOTE: This call has to be before any our argument handling :) */
2983
2984 #ifdef HAVE_GNOME
2985 session_management_setup (argv [0]);
2986 {
2987 char *base = x_basename (argv [0]);
2988
2989 if (base){
2990 if (strcmp (base, "mcedit") == 0)
2991 edit_one_file = "";
2992
2993 if (strcmp (base, "mcview") == 0)
2994 view_one_file = "";
2995 }
2996 }
2997 gnome_init ("gmc", &mc_argp_parser, argc, argv, 0, NULL);
2998 probably_finish_program ();
2999 #endif
3000
3001 if (xtoolkit_init (&argc, argv) == -1)
3002 exit (1);
3003 #endif /* HAVE_X */
3004
3005
3006 #ifdef HAVE_SLANG
3007 SLtt_Ignore_Beep = 1;
3008 #endif
3009
3010 /* NOTE: This has to be called before slang_init or whatever routine
3011 calls any define_sequence */
3012 init_key ();
3013
3014 #ifndef PORT_WANTS_ARGP
3015 handle_args (argc, argv);
3016 #endif
3017
3018 /* Used to report the last working directory at program end */
3019 if (print_last_wd){
3020 #ifndef OS2_NT
3021 stdout_fd = dup (1);
3022 close (1);
3023 if (open (ttyname (0), O_RDWR) < 0)
3024 if (open ("/dev/tty", O_RDWR) < 0) {
3025 /* Try if stderr is not redirected as the last chance */
3026 char *p = strdup (ttyname (0));
3027
3028 if (!strcmp (p, ttyname (2)))
3029 dup2 (2, 1);
3030 else {
3031 fprintf (stderr,
3032 _("Couldn't open tty line. You have to run mc without the -P flag.\n"
3033 "On some systems you may want to run # `which mc`\n"));
3034 exit (1);
3035 }
3036 free (p);
3037 }
3038 #endif
3039 }
3040
3041 # ifdef HAVE_X
3042 /* This is to avoid subshell trying to restard any child pid
3043 * that happends to have cons_saver_pid (a random startup value).
3044 * and PID 1 is init, unlikely we could be the parent of it.
3045 */
3046 /* cons_saver_pid = 1; */
3047 # else
3048 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
3049 handle_console (CONSOLE_INIT);
3050 # endif
3051
3052 # ifdef HAVE_SUBSHELL_SUPPORT
3053 subshell_get_console_attributes ();
3054 # endif
3055
3056 /* Install the SIGCHLD handler; must be done before init_subshell() */
3057 init_sigchld ();
3058 init_sigfatals ();
3059
3060 /* This variable is used by the subshell */
3061 home_dir = getenv ("HOME");
3062 if (!home_dir) {
3063 #ifndef OS2_NT
3064 home_dir = PATH_SEP_STR;
3065 #else
3066 home_dir = mc_home; /* LIBDIR, calculated in OS_Setup() */
3067 #endif
3068 }
3069
3070 compatibility_move_mc_files ();
3071
3072 # ifdef HAVE_X
3073 /* We need this, since ncurses endwin () doesn't restore the signals */
3074 save_stop_handler ();
3075 # endif
3076
3077 /* Must be done before init_subshell, to set up the terminal size: */
3078 /* FIXME: Should be removed and LINES and COLS computed on subshell */
3079 slang_init ();
3080 /* NOTE: This call has to be after slang_init. It's the small part from
3081 the previous init_key which had to be moved after the call of slang_init */
3082 init_key_input_fd ();
3083
3084 load_setup ();
3085
3086 #ifdef HAVE_GNOME
3087 init_colors ();
3088 #else
3089 init_curses ();
3090 #endif
3091
3092 #ifdef HAVE_GNOME
3093 use_subshell = 0;
3094 #endif
3095
3096 # ifdef HAVE_SUBSHELL_SUPPORT
3097 /* Done here to ensure that the subshell doesn't */
3098 /* inherit the file descriptors opened below, etc */
3099
3100 if (use_subshell)
3101 init_subshell ();
3102 # endif
3103
3104 # ifndef HAVE_X
3105 /* Removing this from the X code let's us type C-c */
3106 load_key_defs ();
3107
3108 /* Also done after init_subshell, to save any shell init file messages */
3109 if (console_flag)
3110 handle_console (CONSOLE_SAVE);
3111
3112 if (alternate_plus_minus)
3113 application_keypad_mode ();
3114 # endif
3115
3116 /* The directory hot list */
3117 load_hotlist ();
3118
3119 if (show_change_notice){
3120 message (1, _(" Notice "),
3121 _(" The Midnight Commander configuration files \n"
3122 " are now stored in the ~/.mc directory, the \n"
3123 " files have been moved now\n"));
3124 }
3125
3126 # ifdef HAVE_SUBSHELL_SUPPORT
3127 if (use_subshell){
3128 prompt = strip_ctrl_codes (subshell_prompt);
3129 if (!prompt)
3130 prompt = "";
3131 } else
3132 # endif
3133 prompt = (geteuid () == 0) ? "# " : "$ ";
3134
3135 /* Program main loop */
3136 do_nc ();
3137
3138 /* Virtual File System shutdown */
3139 vfs_shut ();
3140
3141 /* Delete list of all user groups*/
3142 delete_groups ();
3143
3144 flush_extension_file (); /* does only free memory */
3145
3146 # ifndef HAVE_X
3147 /* Miguel, maybe the fix in slang is not required and
3148 * it could be done by removing the slang_done_screen.
3149 * Do I need to call slang_reset_tty then?
3150 */
3151 endwin ();
3152 slang_shutdown ();
3153
3154 if (console_flag && !(quit & SUBSHELL_EXIT))
3155 restore_console ();
3156 if (alternate_plus_minus)
3157 numeric_keypad_mode ();
3158 # endif
3159
3160 #ifndef OS2_NT
3161 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
3162 #endif
3163
3164 # ifndef HAVE_X
3165 if (console_flag)
3166 handle_console (CONSOLE_DONE);
3167 putchar ('\r'); /* Hack to make shell's prompt start at left of screen */
3168 # endif
3169
3170 #ifdef _OS_NT
3171 /* On NT, home_dir is malloced */
3172 // free (home_dir);
3173 #endif
3174 #if defined(OS2_NT)
3175 if (print_last_wd == 2){
3176 FILE *bat_file;
3177
3178 print_last_wd = 0;
3179 bat_file = fopen(batch_file_name, "w");
3180 if (bat_file != NULL){
3181 /* .ado: \r\n for Win95 */
3182 fprintf(bat_file, "@echo off\r\n");
3183 if (isalpha(last_wd_string[0]))
3184 fprintf(bat_file, "%c:\r\n", last_wd_string[0]);
3185 fprintf(bat_file, "cd %s\r\n", last_wd_string);
3186 fclose(bat_file);
3187 }
3188 }
3189 #endif
3190 if (print_last_wd) {
3191 if (print_last_revert || edit_one_file || view_one_file)
3192 write (stdout_fd, ".", 1);
3193 else
3194 write (stdout_fd, last_wd_string, strlen (last_wd_string));
3195 free (last_wd_string);
3196 }
3197
3198 #ifdef HAVE_MAD
3199 done_key ();
3200 #endif
3201 mad_finalize (__FILE__, __LINE__);
3202 #ifdef HAVE_X
3203 xtoolkit_end ();
3204 #endif
3205 return 0;
3206 }