Direct commit: fixed visibility scope for global variable GArray *macros_list
[pantumic.git] / src / main.c
blobba10e1780c2c2f7530fb8de2f814bfaab755cd3b
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_sysconfig_dir: Area for default settings from maintainers of distributuves
123 default is /etc/mc or may be defined by MC_DATADIR
125 char *mc_sysconfig_dir = NULL;
127 /* mc_share_data_dir: Area for default settings from developers */
128 char *mc_share_data_dir = NULL;
130 /* Set to TRUE to suppress printing the last directory */
131 int print_last_revert = FALSE;
133 /* If set, then print to the given file the last directory we were at */
134 char *last_wd_string = NULL;
136 /* index to record_macro_buf[], -1 if not recording a macro */
137 int macro_index = -1;
139 GArray *macros_list;
141 /*** file scope macro definitions ****************************************************************/
143 /*** file scope type declarations ****************************************************************/
145 /*** file scope variables ************************************************************************/
147 /*** file scope functions ************************************************************************/
148 /* --------------------------------------------------------------------------------------------- */
150 static void
151 check_codeset (void)
153 const char *current_system_codepage = NULL;
155 current_system_codepage = str_detect_termencoding ();
157 #ifdef HAVE_CHARSET
159 const char *_display_codepage;
161 _display_codepage = get_codepage_id (display_codepage);
163 if (strcmp (_display_codepage, current_system_codepage) != 0)
165 display_codepage = get_codepage_index (current_system_codepage);
166 if (display_codepage == -1)
167 display_codepage = 0;
169 mc_config_set_string (mc_main_config, "Misc", "display_codepage", cp_display);
172 #endif
174 utf8_display = str_isutf8 (current_system_codepage);
177 /* --------------------------------------------------------------------------------------------- */
179 /** POSIX version. The only version we support. */
180 static void
181 OS_Setup (void)
183 const char *shell_env = getenv ("SHELL");
184 const char *mc_libdir;
186 if ((shell_env == NULL) || (shell_env[0] == '\0'))
188 struct passwd *pwd;
189 pwd = getpwuid (geteuid ());
190 if (pwd != NULL)
191 shell = g_strdup (pwd->pw_shell);
193 else
194 shell = g_strdup (shell_env);
196 if ((shell == NULL) || (shell[0] == '\0'))
198 g_free (shell);
199 shell = g_strdup ("/bin/sh");
202 /* This is the directory, where MC was installed, on Unix this is DATADIR */
203 /* and can be overriden by the MC_DATADIR environment variable */
204 mc_libdir = getenv ("MC_DATADIR");
205 if (mc_libdir != NULL)
207 mc_sysconfig_dir = g_strdup (mc_libdir);
208 mc_share_data_dir = g_strdup (SYSCONFDIR);
210 else
212 mc_sysconfig_dir = g_strdup (SYSCONFDIR);
213 mc_share_data_dir = g_strdup (DATADIR);
218 /* --------------------------------------------------------------------------------------------- */
220 static void
221 sigchld_handler_no_subshell (int sig)
223 #ifdef __linux__
224 int pid, status;
226 if (!console_flag)
227 return;
229 /* COMMENT: if it were true that after the call to handle_console(..INIT)
230 the value of console_flag never changed, we could simply not install
231 this handler at all if (!console_flag && !use_subshell). */
233 /* That comment is no longer true. We need to wait() on a sigchld
234 handler (that's at least what the tarfs code expects currently). */
236 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
238 if (pid == cons_saver_pid)
241 if (WIFSTOPPED (status))
243 /* Someone has stopped cons.saver - restart it */
244 kill (pid, SIGCONT);
246 else
248 /* cons.saver has died - disable console saving */
249 handle_console (CONSOLE_DONE);
250 console_flag = 0;
253 /* If we got here, some other child exited; ignore it */
254 #endif /* __linux__ */
256 (void) sig;
259 /* --------------------------------------------------------------------------------------------- */
261 static void
262 init_sigchld (void)
264 struct sigaction sigchld_action;
266 sigchld_action.sa_handler =
267 #ifdef HAVE_SUBSHELL_SUPPORT
268 use_subshell ? sigchld_handler :
269 #endif /* HAVE_SUBSHELL_SUPPORT */
270 sigchld_handler_no_subshell;
272 sigemptyset (&sigchld_action.sa_mask);
274 #ifdef SA_RESTART
275 sigchld_action.sa_flags = SA_RESTART;
276 #else
277 sigchld_action.sa_flags = 0;
278 #endif /* !SA_RESTART */
280 if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
282 #ifdef HAVE_SUBSHELL_SUPPORT
284 * This may happen on QNX Neutrino 6, where SA_RESTART
285 * is defined but not implemented. Fallback to no subshell.
287 use_subshell = 0;
288 #endif /* HAVE_SUBSHELL_SUPPORT */
292 /* --------------------------------------------------------------------------------------------- */
293 /*** public functions ****************************************************************************/
294 /* --------------------------------------------------------------------------------------------- */
296 /** Define this function for glib-style error handling */
297 GQuark
298 mc_main_error_quark (void)
300 return g_quark_from_static_string (PACKAGE);
303 /* --------------------------------------------------------------------------------------------- */
306 do_cd (const char *new_dir, enum cd_enum exact)
308 gboolean res;
310 res = do_panel_cd (current_panel, new_dir, exact);
312 #if HAVE_CHARSET
313 if (res)
315 const char *enc_name;
317 enc_name = vfs_get_encoding (current_panel->cwd);
318 if (enc_name != NULL)
319 current_panel->codepage = get_codepage_index (enc_name);
320 else
321 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
323 #endif /* HAVE_CHARSET */
325 return res ? 1 : 0;
328 /* --------------------------------------------------------------------------------------------- */
330 #ifdef HAVE_SUBSHELL_SUPPORT
332 load_prompt (int fd, void *unused)
334 (void) fd;
335 (void) unused;
337 if (!read_subshell_prompt ())
338 return 0;
340 /* Don't actually change the prompt if it's invisible */
341 if (((Dlg_head *) top_dlg->data == midnight_dlg) && command_prompt)
343 char *tmp_prompt;
344 int prompt_len;
346 tmp_prompt = strip_ctrl_codes (subshell_prompt);
347 prompt_len = str_term_width1 (tmp_prompt);
349 /* Check for prompts too big */
350 if (COLS > 8 && prompt_len > COLS - 8)
352 prompt_len = COLS - 8;
353 tmp_prompt[prompt_len] = '\0';
355 mc_prompt = tmp_prompt;
356 label_set_text (the_prompt, mc_prompt);
357 input_set_origin ((WInput *) cmdline, prompt_len, COLS - prompt_len);
359 /* since the prompt has changed, and we are called from one of the
360 * tty_get_event channels, the prompt updating does not take place
361 * automatically: force a cursor update and a screen refresh
363 update_cursor (midnight_dlg);
364 mc_refresh ();
366 update_subshell_prompt = TRUE;
367 return 0;
369 #endif /* HAVE_SUBSHELL_SUPPORT */
371 /* --------------------------------------------------------------------------------------------- */
373 /** Show current directory in the xterm title */
374 void
375 update_xterm_title_path (void)
377 /* TODO: share code with midnight_get_title () */
379 const char *path;
380 char host[BUF_TINY];
381 char *p;
382 struct passwd *pw = NULL;
383 char *login = NULL;
384 int res = 0;
386 if (xterm_flag && xterm_title)
388 path = strip_home_and_password (current_panel->cwd);
389 res = gethostname (host, sizeof (host));
390 if (res)
391 { /* On success, res = 0 */
392 host[0] = '\0';
394 else
396 host[sizeof (host) - 1] = '\0';
398 pw = getpwuid (getuid ());
399 if (pw)
401 login = g_strdup_printf ("%s@%s", pw->pw_name, host);
403 else
405 login = g_strdup (host);
407 p = g_strdup_printf ("mc [%s]:%s", login, path);
408 fprintf (stdout, "\33]0;%s\7", str_term_form (p));
409 g_free (login);
410 g_free (p);
411 if (!alternate_plus_minus)
412 numeric_keypad_mode ();
413 fflush (stdout);
417 /* --------------------------------------------------------------------------------------------- */
420 main (int argc, char *argv[])
422 GError *error = NULL;
423 gboolean isInitialized;
425 /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
426 setlocale (LC_ALL, "");
427 bindtextdomain ("mc", LOCALEDIR);
428 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 /* Initialize and create home directories */
466 /* do it after the screen library initialization to show the error message */
467 mc_config_init_config_paths (&error);
468 if (error == NULL)
470 if (mc_config_deprecated_dir_present ())
472 mc_config_migrate_from_old_place (&error);
476 /* Must be done before init_subshell, to set up the terminal size: */
477 /* FIXME: Should be removed and LINES and COLS computed on subshell */
478 tty_init ((gboolean) mc_args__slow_terminal, (gboolean) mc_args__ugly_line_drawing);
480 load_setup ();
482 /* start check display_codepage and source_codepage */
483 check_codeset ();
485 /* Removing this from the X code let's us type C-c */
486 load_key_defs ();
488 load_keymap_defs ();
490 macros_list = g_array_new (TRUE, FALSE, sizeof (macros_t));
492 tty_init_colors (mc_args__disable_colors, mc_args__force_colors);
495 GError *error2 = NULL;
496 isInitialized = mc_skin_init (&error2);
497 mc_filehighlight = mc_fhl_new (TRUE);
498 dlg_set_default_colors ();
500 if (!isInitialized)
502 message (D_ERROR, _("Warning"), "%s", error2->message);
503 g_error_free (error2);
504 error2 = NULL;
508 if (error != NULL)
510 message (D_ERROR, _("Warning"), "%s", error->message);
511 g_error_free (error);
512 error = NULL;
516 #ifdef HAVE_SUBSHELL_SUPPORT
517 /* Done here to ensure that the subshell doesn't */
518 /* inherit the file descriptors opened below, etc */
519 if (use_subshell)
520 init_subshell ();
522 #endif /* HAVE_SUBSHELL_SUPPORT */
524 /* Also done after init_subshell, to save any shell init file messages */
525 if (console_flag)
526 handle_console (CONSOLE_SAVE);
528 if (alternate_plus_minus)
529 application_keypad_mode ();
531 #ifdef HAVE_SUBSHELL_SUPPORT
532 if (use_subshell)
534 mc_prompt = strip_ctrl_codes (subshell_prompt);
535 if (mc_prompt == NULL)
536 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
538 else
539 #endif /* HAVE_SUBSHELL_SUPPORT */
540 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
542 /* Program main loop */
543 if (!midnight_shutdown)
544 do_nc ();
546 /* Save the tree store */
547 tree_store_save ();
549 free_keymap_defs ();
551 /* Virtual File System shutdown */
552 vfs_shut ();
554 flush_extension_file (); /* does only free memory */
556 mc_fhl_free (&mc_filehighlight);
557 mc_skin_deinit ();
558 tty_colors_done ();
560 tty_shutdown ();
562 done_setup ();
564 if (console_flag && (quit & SUBSHELL_EXIT) == 0)
565 handle_console (CONSOLE_RESTORE);
566 if (alternate_plus_minus)
567 numeric_keypad_mode ();
569 signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */
571 if (console_flag)
572 handle_console (CONSOLE_DONE);
574 if (mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file != NULL
575 && last_wd_string != NULL && !print_last_revert)
577 int last_wd_fd;
579 last_wd_fd = open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
580 S_IRUSR | S_IWUSR);
581 if (last_wd_fd != -1)
583 ssize_t ret1;
584 int ret2;
585 ret1 = write (last_wd_fd, last_wd_string, strlen (last_wd_string));
586 ret2 = close (last_wd_fd);
589 g_free (last_wd_string);
591 g_free (mc_share_data_dir);
592 g_free (mc_sysconfig_dir);
593 g_free (shell);
595 done_key ();
597 if (macros_list != NULL)
599 guint i;
600 macros_t *macros;
601 for (i = 0; i < macros_list->len; i++)
603 macros = &g_array_index (macros_list, struct macros_t, i);
604 if (macros != NULL && macros->macro != NULL)
605 g_array_free (macros->macro, FALSE);
607 g_array_free (macros_list, TRUE);
610 str_uninit_strings ();
612 g_free (mc_run_param0);
613 g_free (mc_run_param1);
615 mc_config_deinit_config_paths ();
617 putchar ('\n'); /* Hack to make shell's prompt start at left of screen */
619 return 0;
622 /* --------------------------------------------------------------------------------------------- */