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