Panel update: type accuracy.
[midnight-commander.git] / src / main.c
blob155803d6c76c138fb72e8004b5c131f916644655
1 /* Main program for the Midnight Commander
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1994, 1995, 1996, 1997 Miguel de Icaza
6 1994, 1995 Janne Kukonlehto
7 1997 Norbert Warmuth
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file main.c
24 * \brief Source: this is a main module
27 #include <config.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pwd.h> /* for username in xterm title */
42 #include "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/tty/key.h" /* For init_key() */
46 #include "lib/tty/win.h" /* xterm_flag */
47 #include "lib/skin.h"
48 #include "lib/filehighlight.h"
49 #include "lib/fileloc.h"
50 #include "lib/strutil.h"
51 #include "lib/util.h"
52 #include "lib/vfs/mc-vfs/vfs.h" /* vfs_init(), vfs_shut() */
54 #include "filemanager/midnight.h" /* current_panel */
55 #include "filemanager/treestore.h" /* tree_store_save */
56 #include "filemanager/layout.h" /* command_prompt */
57 #include "filemanager/ext.h" /* flush_extension_file() */
58 #include "filemanager/command.h" /* cmdline */
60 #include "args.h"
61 #include "subshell.h"
62 #include "setup.h" /* load_setup() */
64 #ifdef HAVE_CHARSET
65 #include "lib/charsets.h"
66 #include "selcodepage.h"
67 #endif /* HAVE_CHARSET */
69 #include "consaver/cons.saver.h" /* console_flag */
71 #include "main.h"
73 /*** global variables ****************************************************************************/
75 mc_fhl_t *mc_filehighlight;
77 /* Set when main loop should be terminated */
78 int quit = 0;
80 #ifdef HAVE_CHARSET
81 /* Numbers of (file I/O) and (input/display) codepages. -1 if not selected */
82 int source_codepage = -1;
83 int default_source_codepage = -1;
84 int display_codepage = -1;
85 char *autodetect_codeset = NULL;
86 gboolean is_autodetect_codeset_enabled = FALSE;
87 #else
88 /* If true, allow characters in the range 160-255 */
89 int eight_bit_clean = 1;
91 * If true, also allow characters in the range 128-159.
92 * This is reported to break on many terminals (xterm, qansi-m).
94 int full_eight_bits = 0;
95 #endif /* !HAVE_CHARSET */
98 * If utf-8 terminal utf8_display = 1
99 * Display bits set UTF-8
101 int utf8_display = 0;
103 /* If true use the internal viewer */
104 int use_internal_view = 1;
105 /* If set, use the builtin editor */
106 int use_internal_edit = 1;
108 mc_run_mode_t mc_run_mode = MC_RUN_FULL;
109 char *mc_run_param0 = NULL;
110 char *mc_run_param1 = NULL;
112 /* Used so that widgets know if they are being destroyed or
113 shut down */
114 int midnight_shutdown = 0;
116 /* The user's shell */
117 char *shell = NULL;
119 /* The prompt */
120 const char *mc_prompt = NULL;
122 /* mc_home: The home of MC - /etc/mc or defined by MC_DATADIR */
123 char *mc_home = NULL;
124 /* mc_home_alt: Alternative home of MC - deprecated /usr/share/mc */
125 char *mc_home_alt = NULL;
126 /* The home directory */
127 const char *home_dir = NULL;
129 /* Set to TRUE to suppress printing the last directory */
130 int print_last_revert = FALSE;
132 /* If set, then print to the given file the last directory we were at */
133 char *last_wd_string = NULL;
135 /*** file scope macro definitions ****************************************************************/
137 /*** file scope type declarations ****************************************************************/
139 /*** file scope variables ************************************************************************/
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
144 static void
145 check_codeset (void)
147 const char *current_system_codepage = NULL;
149 current_system_codepage = str_detect_termencoding ();
151 #ifdef HAVE_CHARSET
153 const char *_display_codepage;
155 _display_codepage = get_codepage_id (display_codepage);
157 if (strcmp (_display_codepage, current_system_codepage) != 0)
159 display_codepage = get_codepage_index (current_system_codepage);
160 if (display_codepage == -1)
161 display_codepage = 0;
163 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
166 #endif
168 utf8_display = str_isutf8 (current_system_codepage);
171 /* --------------------------------------------------------------------------------------------- */
173 /** POSIX version. The only version we support. */
174 static void
175 OS_Setup (void)
177 const char *shell_env = getenv ("SHELL");
178 const char *mc_libdir;
180 if ((shell_env == NULL) || (shell_env[0] == '\0'))
182 struct passwd *pwd;
183 pwd = getpwuid (geteuid ());
184 if (pwd != NULL)
185 shell = g_strdup (pwd->pw_shell);
187 else
188 shell = g_strdup (shell_env);
190 if ((shell == NULL) || (shell[0] == '\0'))
192 g_free (shell);
193 shell = g_strdup ("/bin/sh");
196 /* This is the directory, where MC was installed, on Unix this is DATADIR */
197 /* and can be overriden by the MC_DATADIR environment variable */
198 mc_libdir = getenv ("MC_DATADIR");
199 if (mc_libdir != NULL)
201 mc_home = g_strdup (mc_libdir);
202 mc_home_alt = g_strdup (SYSCONFDIR);
204 else
206 mc_home = g_strdup (SYSCONFDIR);
207 mc_home_alt = g_strdup (DATADIR);
210 /* This variable is used by the subshell */
211 home_dir = getenv ("HOME");
213 if (home_dir == NULL)
214 home_dir = mc_home;
217 /* --------------------------------------------------------------------------------------------- */
219 static void
220 sigchld_handler_no_subshell (int sig)
222 #ifdef __linux__
223 int pid, status;
225 if (!console_flag)
226 return;
228 /* COMMENT: if it were true that after the call to handle_console(..INIT)
229 the value of console_flag never changed, we could simply not install
230 this handler at all if (!console_flag && !use_subshell). */
232 /* That comment is no longer true. We need to wait() on a sigchld
233 handler (that's at least what the tarfs code expects currently). */
235 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
237 if (pid == cons_saver_pid)
240 if (WIFSTOPPED (status))
242 /* Someone has stopped cons.saver - restart it */
243 kill (pid, SIGCONT);
245 else
247 /* cons.saver has died - disable console saving */
248 handle_console (CONSOLE_DONE);
249 console_flag = 0;
252 /* If we got here, some other child exited; ignore it */
253 #endif /* __linux__ */
255 (void) sig;
258 /* --------------------------------------------------------------------------------------------- */
260 static void
261 init_sigchld (void)
263 struct sigaction sigchld_action;
265 sigchld_action.sa_handler =
266 #ifdef HAVE_SUBSHELL_SUPPORT
267 use_subshell ? sigchld_handler :
268 #endif /* HAVE_SUBSHELL_SUPPORT */
269 sigchld_handler_no_subshell;
271 sigemptyset (&sigchld_action.sa_mask);
273 #ifdef SA_RESTART
274 sigchld_action.sa_flags = SA_RESTART;
275 #else
276 sigchld_action.sa_flags = 0;
277 #endif /* !SA_RESTART */
279 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
281 #ifdef HAVE_SUBSHELL_SUPPORT
283 * This may happen on QNX Neutrino 6, where SA_RESTART
284 * is defined but not implemented. Fallback to no subshell.
286 use_subshell = 0;
287 #endif /* HAVE_SUBSHELL_SUPPORT */
291 /* --------------------------------------------------------------------------------------------- */
292 /*** public functions ****************************************************************************/
293 /* --------------------------------------------------------------------------------------------- */
295 /** Define this function for glib-style error handling */
296 GQuark
297 mc_main_error_quark (void)
299 return g_quark_from_static_string (PACKAGE);
302 /* --------------------------------------------------------------------------------------------- */
305 do_cd (const char *new_dir, enum cd_enum exact)
307 gboolean res;
309 res = do_panel_cd (current_panel, new_dir, exact);
311 #if HAVE_CHARSET
312 if (res)
314 const char *enc_name;
316 enc_name = vfs_get_encoding (current_panel->cwd);
317 if (enc_name != NULL)
318 current_panel->codepage = get_codepage_index (enc_name);
319 else
320 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
322 #endif /* HAVE_CHARSET */
324 return res ? 1 : 0;
327 /* --------------------------------------------------------------------------------------------- */
329 #ifdef HAVE_SUBSHELL_SUPPORT
331 load_prompt (int fd, void *unused)
333 (void) fd;
334 (void) unused;
336 if (!read_subshell_prompt ())
337 return 0;
339 /* Don't actually change the prompt if it's invisible */
340 if (((Dlg_head *) top_dlg->data == midnight_dlg) && command_prompt)
342 char *tmp_prompt;
343 int prompt_len;
345 tmp_prompt = strip_ctrl_codes (subshell_prompt);
346 prompt_len = str_term_width1 (tmp_prompt);
348 /* Check for prompts too big */
349 if (COLS > 8 && prompt_len > COLS - 8)
351 prompt_len = COLS - 8;
352 tmp_prompt[prompt_len] = '\0';
354 mc_prompt = tmp_prompt;
355 label_set_text (the_prompt, mc_prompt);
356 input_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
358 /* since the prompt has changed, and we are called from one of the
359 * tty_get_event channels, the prompt updating does not take place
360 * automatically: force a cursor update and a screen refresh
362 update_cursor (midnight_dlg);
363 mc_refresh ();
365 update_subshell_prompt = TRUE;
366 return 0;
368 #endif /* HAVE_SUBSHELL_SUPPORT */
370 /* --------------------------------------------------------------------------------------------- */
372 /** Show current directory in the xterm title */
373 void
374 update_xterm_title_path (void)
376 /* TODO: share code with midnight_get_title () */
378 const char *path;
379 char host[BUF_TINY];
380 char *p;
381 struct passwd *pw = NULL;
382 char *login = NULL;
383 int res = 0;
385 if (xterm_flag && xterm_title)
387 path = strip_home_and_password (current_panel->cwd);
388 res = gethostname (host, sizeof (host));
389 if (res)
390 { /* On success, res = 0 */
391 host[0] = '\0';
393 else
395 host[sizeof (host) - 1] = '\0';
397 pw = getpwuid (getuid ());
398 if (pw)
400 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
402 else
404 login = g_strdup (host);
406 p = g_strdup_printf ("mc [%s]:%s", login, path);
407 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
408 g_free (login);
409 g_free (p);
410 if (!alternate_plus_minus)
411 numeric_keypad_mode ();
412 fflush (stdout);
416 /* --------------------------------------------------------------------------------------------- */
419 main (int argc, char *argv[])
421 struct stat s;
422 char *mc_dir;
423 GError *error = NULL;
424 gboolean isInitialized;
426 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
427 setlocale (LC_ALL, "");
428 bindtextdomain ("mc", LOCALEDIR);
429 textdomain ("mc");
431 /* Set up temporary directory */
432 mc_tmpdir ();
434 OS_Setup ();
436 str_init_strings (NULL);
438 vfs_init ();
440 if (!mc_args_handle (argc, argv, "mc"))
441 exit (EXIT_FAILURE);
443 /* NOTE: This has to be called before tty_init or whatever routine
444 calls any define_sequence */
445 init_key ();
447 /* Must be done before installing the SIGCHLD handler [[FIXME]] */
448 handle_console (CONSOLE_INIT);
450 #ifdef HAVE_SUBSHELL_SUPPORT
451 /* Don't use subshell when invoked as viewer or editor */
452 if (mc_run_mode != MC_RUN_FULL)
453 use_subshell = 0;
455 if (use_subshell)
456 subshell_get_console_attributes ();
457 #endif /* HAVE_SUBSHELL_SUPPORT */
459 /* Install the SIGCHLD handler; must be done before init_subshell() */
460 init_sigchld ();
462 /* We need this, since ncurses endwin () doesn't restore the signals */
463 save_stop_handler ();
465 /* Must be done before init_subshell, to set up the terminal size: */
466 /* FIXME: Should be removed and LINES and COLS computed on subshell */
467 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
469 load_setup ();
471 /* start check display_codepage and source_codepage */
472 check_codeset ();
474 /* Removing this from the X code let's us type C-c */
475 load_key_defs ();
477 load_keymap_defs ();
479 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
480 isInitialized = mc_skin_init (&error);
481 mc_filehighlight = mc_fhl_new (TRUE);
482 dlg_set_default_colors ();
484 if (!isInitialized)
486 message (D_ERROR, _("Warning"), "%s", error->message);
487 g_error_free (error);
488 error = NULL;
491 /* create home directory */
492 /* do it after the screen library initialization to show the error message */
493 mc_dir = g_build_filename (home_dir, MC_USERCONF_DIR, (char *) NULL);
494 canonicalize_pathname (mc_dir);
495 if ((stat (mc_dir, &s) != 0) && (errno == ENOENT) && mkdir (mc_dir, 0700) != 0)
496 message (D_ERROR, _("Warning"), _("Cannot create %s directory"), mc_dir);
497 g_free (mc_dir);
499 #ifdef HAVE_SUBSHELL_SUPPORT
500 /* Done here to ensure that the subshell doesn't */
501 /* inherit the file descriptors opened below, etc */
502 if (use_subshell)
503 init_subshell ();
505 #endif /* HAVE_SUBSHELL_SUPPORT */
507 /* Also done after init_subshell, to save any shell init file messages */
508 if (console_flag)
509 handle_console (CONSOLE_SAVE);
511 if (alternate_plus_minus)
512 application_keypad_mode ();
514 #ifdef HAVE_SUBSHELL_SUPPORT
515 if (use_subshell)
517 mc_prompt = strip_ctrl_codes (subshell_prompt);
518 if (mc_prompt == NULL)
519 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
521 else
522 #endif /* HAVE_SUBSHELL_SUPPORT */
523 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
525 /* Program main loop */
526 if (!midnight_shutdown)
527 do_nc ();
529 /* Save the tree store */
530 tree_store_save ();
532 free_keymap_defs ();
534 /* Virtual File System shutdown */
535 vfs_shut ();
537 flush_extension_file (); /* does only free memory */
539 mc_fhl_free (&mc_filehighlight);
540 mc_skin_deinit ();
541 tty_colors_done ();
543 tty_shutdown ();
545 done_setup ();
547 if (console_flag && (quit & SUBSHELL_EXIT) == 0)
548 handle_console (CONSOLE_RESTORE);
549 if (alternate_plus_minus)
550 numeric_keypad_mode ();
552 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
554 if (console_flag)
555 handle_console (CONSOLE_DONE);
557 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file != NULL
558 && last_wd_string != NULL && !print_last_revert)
560 int last_wd_fd;
562 last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
563 S_IRUSR | S_IWUSR);
564 if (last_wd_fd != -1)
566 ssize_t ret1;
567 int ret2;
568 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
569 ret2 = close (last_wd_fd);
572 g_free (last_wd_string);
574 g_free (mc_home_alt);
575 g_free (mc_home);
576 g_free (shell);
578 done_key ();
580 str_uninit_strings ();
582 g_free (mc_run_param0);
583 g_free (mc_run_param1);
585 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
587 return 0;
590 /* --------------------------------------------------------------------------------------------- */