This source file includes following definitions.
- OS_Setup
- sigchld_handler_no_subshell
- init_sigchld
- check_sid
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 #include <config.h>
33
34 #include <ctype.h>
35 #include <errno.h>
36 #include <locale.h>
37 #include <pwd.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <signal.h>
44 #include <unistd.h>
45
46 #include "lib/global.h"
47
48 #include "lib/event.h"
49 #include "lib/tty/tty.h"
50 #include "lib/tty/key.h"
51 #include "lib/tty/mouse.h"
52 #include "lib/skin.h"
53 #include "lib/filehighlight.h"
54 #include "lib/fileloc.h"
55 #include "lib/strutil.h"
56 #include "lib/util.h"
57 #include "lib/vfs/vfs.h"
58
59 #include "filemanager/filemanager.h"
60 #include "filemanager/treestore.h"
61 #include "filemanager/layout.h"
62 #include "filemanager/ext.h"
63 #include "filemanager/command.h"
64 #include "filemanager/panel.h"
65 #include "filemanager/filenot.h"
66
67 #ifdef USE_INTERNAL_EDIT
68 #include "editor/edit.h"
69 #endif
70
71 #include "vfs/plugins_init.h"
72
73 #include "events_init.h"
74 #include "args.h"
75 #ifdef ENABLE_SUBSHELL
76 #include "subshell/subshell.h"
77 #endif
78 #include "keymap.h"
79 #include "setup.h"
80
81 #include "lib/charsets.h"
82 #include "selcodepage.h"
83
84 #include "consaver/cons.saver.h"
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 static void
102 OS_Setup (void)
103 {
104 mc_shell_init ();
105
106
107
108 const char *datadir_env = g_getenv ("MC_DATADIR");
109
110 if (datadir_env != NULL)
111 mc_global.sysconfig_dir = g_strdup (datadir_env);
112 else
113 mc_global.sysconfig_dir = g_strdup (SYSCONFDIR);
114
115 mc_global.share_data_dir = g_strdup (DATADIR);
116 }
117
118
119
120 static void
121 sigchld_handler_no_subshell (int sig)
122 {
123 #ifdef __linux__
124 int pid, status;
125
126 if (mc_global.tty.console_flag == '\0')
127 return;
128
129
130
131
132
133
134
135
136 pid = waitpid (cons_saver_pid, &status, WUNTRACED | WNOHANG);
137
138 if (pid == cons_saver_pid)
139 {
140 if (WIFSTOPPED (status))
141 {
142
143 kill (pid, SIGCONT);
144 }
145 else
146 {
147
148 handle_console (CONSOLE_DONE);
149 mc_global.tty.console_flag = '\0';
150 }
151 }
152
153 #endif
154
155 (void) sig;
156 }
157
158
159
160 static void
161 init_sigchld (void)
162 {
163 struct sigaction sigchld_action;
164
165 memset (&sigchld_action, 0, sizeof (sigchld_action));
166 sigchld_action.sa_handler =
167 #ifdef ENABLE_SUBSHELL
168 mc_global.tty.use_subshell ? sigchld_handler :
169 #endif
170 sigchld_handler_no_subshell;
171
172 sigemptyset (&sigchld_action.sa_mask);
173
174 #ifdef SA_RESTART
175 sigchld_action.sa_flags = SA_RESTART;
176 #endif
177
178 if (my_sigaction (SIGCHLD, &sigchld_action, NULL) == -1)
179 {
180 #ifdef ENABLE_SUBSHELL
181
182
183
184
185 mc_global.tty.use_subshell = FALSE;
186 #endif
187 }
188 }
189
190
191
192
193
194
195
196
197 static gboolean
198 check_sid (void)
199 {
200 pid_t my_sid, old_sid;
201 const char *sid_str;
202
203 sid_str = getenv ("MC_SID");
204 if (sid_str == NULL)
205 return TRUE;
206
207 old_sid = (pid_t) strtol (sid_str, NULL, 0);
208 if (old_sid == 0)
209 return TRUE;
210
211 my_sid = getsid (0);
212 if (my_sid == -1)
213 return TRUE;
214
215
216 return (old_sid != my_sid);
217 }
218
219
220
221
222
223 int
224 main (int argc, char *argv[])
225 {
226 GError *mcerror = NULL;
227 int exit_code = EXIT_FAILURE;
228 const char *tmpdir = NULL;
229
230 mc_global.run_from_parent_mc = !check_sid ();
231
232
233 #ifdef HAVE_SETLOCALE
234 (void) setlocale (LC_ALL, "");
235 #endif
236 (void) bindtextdomain (PACKAGE, LOCALEDIR);
237 (void) textdomain (PACKAGE);
238
239
240 str_init_strings (NULL);
241
242 mc_setup_run_mode (argv);
243
244 if (!mc_args_parse (&argc, &argv, "mc", &mcerror))
245 {
246 startup_exit_falure:
247 fprintf (stderr, _ ("Failed to run:\n%s\n"), mcerror->message);
248 g_error_free (mcerror);
249 startup_exit_ok:
250 mc_shell_deinit ();
251 str_uninit_strings ();
252 return exit_code;
253 }
254
255
256
257
258
259
260 mc_global.tty.xterm_flag = tty_check_xterm_compat (mc_args__force_xterm);
261
262
263 OS_Setup ();
264
265 if (!g_path_is_absolute (mc_config_get_home_dir ()))
266 {
267 mc_propagate_error (&mcerror, 0, "%s: %s", _ ("Home directory path is not absolute"),
268 mc_config_get_home_dir ());
269 mc_event_deinit (NULL);
270 goto startup_exit_falure;
271 }
272
273 if (!mc_args_show_info ())
274 {
275 exit_code = EXIT_SUCCESS;
276 goto startup_exit_ok;
277 }
278
279 if (!events_init (&mcerror))
280 goto startup_exit_falure;
281
282 mc_config_init_config_paths (&mcerror);
283 if (mcerror != NULL)
284 {
285 mc_event_deinit (NULL);
286 goto startup_exit_falure;
287 }
288
289 vfs_init ();
290 vfs_plugins_init ();
291
292 load_setup ();
293
294
295 vfs_setup_work_dir ();
296
297
298 tmpdir = mc_tmpdir ();
299
300
301
302 if (!mc_setup_by_args (argc, argv, &mcerror))
303 {
304
305
306 vfs_expire (TRUE);
307 (void) my_rmdir (tmpdir);
308
309 vfs_shut ();
310 done_setup ();
311 g_free (saved_other_dir);
312 mc_event_deinit (NULL);
313 goto startup_exit_falure;
314 }
315
316
317
318
319
320 if (mc_global.mc_run_mode == MC_RUN_FULL)
321 {
322 char *buffer;
323 vfs_path_t *vpath;
324
325 buffer = mc_config_get_string (mc_global.panels_config, "Dirs", "other_dir", ".");
326 vpath = vfs_path_from_str (buffer);
327 if (vfs_file_is_local (vpath))
328 saved_other_dir = buffer;
329 else
330 g_free (buffer);
331 vfs_path_free (vpath, TRUE);
332 }
333
334
335
336 init_key ();
337
338
339 handle_console (CONSOLE_INIT);
340
341 #ifdef ENABLE_SUBSHELL
342
343 if (mc_global.mc_run_mode != MC_RUN_FULL && mc_global.run_from_parent_mc)
344 mc_global.tty.use_subshell = FALSE;
345
346 if (mc_global.tty.use_subshell)
347 subshell_get_console_attributes ();
348 #endif
349
350
351 init_sigchld ();
352
353
354 save_stop_handler ();
355
356
357
358 tty_init (!mc_args__nomouse, mc_global.tty.xterm_flag);
359
360
361 load_key_defs ();
362
363 keymap_load (!mc_args__nokeymap);
364
365 #ifdef USE_INTERNAL_EDIT
366 macros_list = g_array_new (TRUE, FALSE, sizeof (macros_t));
367 #endif
368
369 tty_init_colors (mc_global.tty.disable_colors, mc_args__force_colors);
370
371 mc_skin_init (NULL, &mcerror);
372 dlg_set_default_colors ();
373 input_set_default_colors ();
374 if (mc_global.mc_run_mode == MC_RUN_FULL)
375 command_set_default_colors ();
376
377 mc_error_message (&mcerror, NULL);
378
379 #ifdef ENABLE_SUBSHELL
380
381
382 if (mc_global.tty.use_subshell && mc_global.run_from_parent_mc)
383 {
384 const int quit_mc =
385 query_dialog (_ ("Warning"),
386 _ ("GNU Midnight Commander\nis already running on this terminal.\n"
387 "Subshell support will be disabled."),
388 D_ERROR, 2, _ ("&OK"), _ ("&Quit"));
389
390 if (quit_mc != 0)
391 {
392
393 mc_global.midnight_shutdown = TRUE;
394 }
395
396
397 mc_global.tty.use_subshell = FALSE;
398 }
399
400 if (mc_global.tty.use_subshell)
401 init_subshell ();
402 #endif
403
404 if (!mc_global.midnight_shutdown)
405 {
406
407 if (mc_global.tty.console_flag != '\0')
408 handle_console (CONSOLE_SAVE);
409
410 if (mc_global.tty.alternate_plus_minus)
411 application_keypad_mode ();
412
413
414
415 init_mouse ();
416
417
418
419 enable_bracketed_paste ();
420
421
422 mc_prompt = (geteuid () == 0) ? "# " : "$ ";
423 }
424
425
426 if (mc_global.midnight_shutdown)
427 exit_code = EXIT_SUCCESS;
428 else
429 exit_code = do_nc () ? EXIT_SUCCESS : EXIT_FAILURE;
430
431 disable_bracketed_paste ();
432
433 disable_mouse ();
434
435
436 (void) tree_store_save ();
437
438 keymap_free ();
439
440
441
442 vfs_expire (TRUE);
443 (void) my_rmdir (tmpdir);
444
445
446 vfs_shut ();
447
448 flush_extension_file ();
449
450 mc_skin_deinit ();
451 tty_colors_done ();
452
453 tty_shutdown ();
454
455 done_setup ();
456
457 if (mc_global.tty.console_flag != '\0' && (quit & SUBSHELL_EXIT) == 0)
458 handle_console (CONSOLE_RESTORE);
459 if (mc_global.tty.alternate_plus_minus)
460 numeric_keypad_mode ();
461
462 (void) my_signal (SIGCHLD, SIG_DFL);
463
464 if (mc_global.tty.console_flag != '\0')
465 handle_console (CONSOLE_DONE);
466
467 if (mc_global.mc_run_mode == MC_RUN_FULL && mc_args__last_wd_file != NULL && last_wd_str != NULL
468 && !print_last_revert)
469 {
470 const int last_wd_fd =
471 open (mc_args__last_wd_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
472 if (last_wd_fd != -1)
473 {
474 MC_UNUSED const ssize_t ret1 = write (last_wd_fd, last_wd_str, strlen (last_wd_str));
475 MC_UNUSED const int ret2 = close (last_wd_fd);
476 }
477 }
478
479 g_free (last_wd_str);
480
481 mc_shell_deinit ();
482
483 done_key ();
484
485 #ifdef USE_INTERNAL_EDIT
486 if (macros_list != NULL)
487 {
488 guint i;
489
490 for (i = 0; i < macros_list->len; i++)
491 {
492 macros_t *macros;
493
494 macros = &g_array_index (macros_list, struct macros_t, i);
495 if (macros != NULL && macros->macro != NULL)
496 (void) g_array_free (macros->macro, TRUE);
497 }
498 (void) g_array_free (macros_list, TRUE);
499 }
500 #endif
501
502 str_uninit_strings ();
503
504 if (mc_global.mc_run_mode != MC_RUN_EDITOR)
505 g_free (mc_run_param0);
506 #ifdef USE_INTERNAL_EDIT
507 else
508 g_list_free_full ((GList *) mc_run_param0, (GDestroyNotify) edit_arg_free);
509 #endif
510
511 g_free (mc_run_param1);
512 g_free (saved_other_dir);
513
514 mc_config_deinit_config_paths ();
515
516 (void) mc_event_deinit (&mcerror);
517 if (mcerror != NULL)
518 {
519 fprintf (stderr, _ ("\nFailed while close:\n%s\n"), mcerror->message);
520 g_error_free (mcerror);
521 exit_code = EXIT_FAILURE;
522 }
523
524 (void) putchar ('\n');
525
526 return exit_code;
527 }
528
529