Merge with git+ssh://pasky.or.cz/srv/git/elinks.git
[elinks.git] / src / main / main.c
blob5b2bec8eff9bcb4a74ebcb5c30fb0dcc11260682
1 /* The main program - startup */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #ifdef HAVE_FCNTL_H
13 #include <fcntl.h> /* OS/2 needs this after sys/types.h */
14 #endif
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
19 #include "elinks.h"
21 #include "bfu/dialog.h"
22 #include "cache/cache.h"
23 #include "config/cmdline.h"
24 #include "config/conf.h"
25 #include "config/home.h"
26 #include "config/kbdbind.h"
27 #include "config/options.h"
28 #include "dialogs/menu.h"
29 #include "document/document.h"
30 #include "intl/charsets.h"
31 #include "intl/gettext/libintl.h"
32 #include "main/event.h"
33 #include "main/interlink.h"
34 #include "main/main.h"
35 #include "main/module.h"
36 #include "main/select.h"
37 #include "main/version.h"
38 #include "network/connection.h"
39 #include "network/dns.h"
40 #include "network/state.h"
41 #include "osdep/osdep.h"
42 #include "osdep/signals.h"
43 #include "osdep/sysname.h"
44 #include "protocol/auth/auth.h"
45 #include "session/download.h"
46 #include "session/session.h"
47 #include "terminal/kbd.h"
48 #include "terminal/screen.h"
49 #include "terminal/terminal.h"
50 #include "util/color.h"
51 #include "util/error.h"
52 #include "util/file.h"
53 #include "util/memdebug.h"
54 #include "util/memory.h"
55 #include "viewer/dump/dump.h"
56 #include "viewer/text/marks.h"
58 struct program program;
60 static int ac;
61 static unsigned char **av;
62 static int init_b = 0;
64 /* Check if either stdin or stdout are pipes */
65 static void
66 check_stdio(struct list_head *url_list)
68 assert(!remote_session_flags);
70 /* Should the document be read from stdin? */
71 if (!isatty(STDIN_FILENO)) {
72 /* Only start reading from stdin if no URL was given on the
73 * command line. */
74 if (url_list && list_empty(*url_list)) {
75 get_opt_bool("protocol.file.allow_special_files") = 1;
76 add_to_string_list(url_list, "file:///dev/stdin", 17);
78 get_cmd_opt_bool("no-connect") = 1;
81 /* If called for outputting to a pipe without -dump or -source
82 * specified default to using dump viewer. */
83 if (!isatty(STDOUT_FILENO)) {
84 int *dump = &get_cmd_opt_bool("dump");
86 if (!*dump && !get_cmd_opt_bool("source"))
87 *dump = 1;
91 static void
92 check_cwd(void)
94 unsigned char *cwd = get_cwd();
96 if (!cwd || !file_is_dir(cwd)) {
97 unsigned char *home = getenv("HOME");
99 if (home && file_is_dir(home))
100 chdir(home);
103 mem_free_if(cwd);
106 static void
107 init(void)
109 INIT_LIST_HEAD(url_list);
110 int fd = -1;
111 enum retval ret;
113 init_osdep();
114 init_static_version();
115 check_cwd();
117 #ifdef CONFIG_NLS
118 bindtextdomain(PACKAGE, LOCALEDIR);
119 textdomain(PACKAGE);
120 set_language(0);
121 #endif
123 init_event();
125 init_charsets_lookup();
126 init_colors_lookup();
127 init_modules(main_modules);
129 init_options();
130 register_modules_options(main_modules);
131 register_modules_options(builtin_modules);
132 set_sigcld();
133 get_system_name();
134 init_keymaps();
136 /* XXX: OS/2 has some stupid bug and the pipe must be created before
137 * socket :-/. -- Mikulas */
138 if (check_terminal_pipes()) {
139 ERROR(gettext("Cannot create a pipe for internal communication."));
140 program.retval = RET_FATAL;
141 program.terminate = 1;
142 return;
145 /* Parsing command line options */
146 ret = parse_options(ac - 1, av + 1, &url_list);
147 if (ret != RET_OK) {
148 /* Command line handlers return RET_COMMAND to signal that they
149 * completed successfully and that the program should stop. */
150 if (ret != RET_COMMAND)
151 program.retval = ret;
152 program.terminate = 1;
153 free_string_list(&url_list);
154 return;
157 if (!remote_session_flags) {
158 check_stdio(&url_list);
161 if (!get_cmd_opt_bool("no-home")) {
162 init_home();
165 /* If there's no -no-connect, -dump or -source option, check if there's
166 * no other ELinks running. If we found any, by-pass initialization of
167 * non critical subsystems, open socket and act as a slave for it. */
168 if (get_cmd_opt_bool("no-connect")
169 || get_cmd_opt_bool("dump")
170 || get_cmd_opt_bool("source")
171 || (fd = init_interlink()) == -1) {
173 load_config();
174 update_options_visibility();
175 /* Parse commandline options again, in order to override any
176 * config file options. */
177 parse_options(ac - 1, av + 1, NULL);
178 /* ... and re-check stdio, in order to override any command
179 * line options! >;) */
180 if (!remote_session_flags) {
181 check_stdio(NULL);
184 init_b = 1;
185 init_modules(builtin_modules);
188 if (get_cmd_opt_bool("dump")
189 || get_cmd_opt_bool("source")) {
190 /* Dump the URL list */
191 #ifdef CONFIG_ECMASCRIPT
192 /* The ECMAScript code is not good at coping with this. And it
193 * makes currently no sense to evaluate ECMAScript in this
194 * context anyway. */
195 get_opt_bool("ecmascript.enable") = 0;
196 #endif
197 if (!list_empty(url_list)) {
198 dump_next(&url_list);
199 } else {
200 unsigned char *arg = get_cmd_opt_bool("dump")
201 ? "dump" : "source";
203 usrerror(gettext("URL expected after -%s"), arg);
204 program.retval = RET_SYNTAX;
205 program.terminate = 1;
208 } else if (remote_session_flags & SES_REMOTE_PING) {
209 /* If no instance was running return ping failure */
210 if (fd == -1) {
211 usrerror(gettext("No running ELinks found."));
212 program.retval = RET_PING;
214 program.terminate = 1;
216 } else if (remote_session_flags && fd == -1) {
217 /* The remote session(s) can not be created */
218 usrerror(gettext("No remote session to connect to."));
219 program.retval = RET_REMOTE;
220 program.terminate = 1;
222 } else {
223 struct string info;
224 struct terminal *term = NULL;
226 if (!encode_session_info(&info, &url_list)) {
227 ERROR(gettext("Unable to encode session info."));
228 program.retval = RET_FATAL;
229 program.terminate = 1;
231 } else if (fd != -1) {
232 /* Attach to already running ELinks and act as a slave
233 * for it. */
234 close_terminal_pipes();
236 handle_trm(get_input_handle(), get_output_handle(),
237 fd, fd, get_ctl_handle(), info.source, info.length,
238 remote_session_flags);
239 } else {
240 /* Setup a master terminal */
241 term = attach_terminal(get_input_handle(), get_output_handle(),
242 get_ctl_handle(), info.source, info.length);
243 if (!term) {
244 ERROR(gettext("Unable to attach_terminal()."));
245 program.retval = RET_FATAL;
246 program.terminate = 1;
250 /* OK, this is race condition, but it must be so; GPM installs
251 * it's own buggy TSTP handler. */
252 if (!program.terminate) handle_basic_signals(term);
253 done_string(&info);
256 if (program.terminate) close_terminal_pipes();
257 free_string_list(&url_list);
261 static void
262 terminate_all_subsystems(void)
264 done_interlink();
265 check_bottom_halves();
266 abort_all_downloads();
267 check_bottom_halves();
268 destroy_all_terminals();
269 check_bottom_halves();
270 free_all_itrms();
272 /* When aborting all connections also keep-alive connections are
273 * aborted. A (normal) connection will be started for any keep-alive
274 * connection that needs to send a command to the server before
275 * aborting. This means we need to abort_all_connections() twice.
277 * It forces a some what unclean connection tear-down since at most the
278 * shutdown routine will be able to send one command. But else it would
279 * take too long time to terminate. */
280 abort_all_connections();
281 check_bottom_halves();
282 abort_all_connections();
284 if (init_b) {
285 #ifdef CONFIG_SCRIPTING
286 trigger_event_name("quit");
287 #endif
288 #ifdef CONFIG_MARKS
289 free_marks();
290 #endif
291 free_history_lists();
292 free_auth();
293 done_modules(builtin_modules);
294 done_screen_drivers();
295 done_saved_session_info();
298 shrink_memory(1);
299 free_charsets_lookup();
300 free_colors_lookup();
301 done_modules(main_modules);
302 free_keymaps();
303 free_conv_table();
304 check_bottom_halves();
305 done_home();
306 done_state_message();
307 done_bfu_colors();
308 unregister_modules_options(builtin_modules);
309 unregister_modules_options(main_modules);
310 done_options();
311 done_event();
312 terminate_osdep();
315 void
316 shrink_memory(int whole)
318 shrink_dns_cache(whole);
319 shrink_format_cache(whole);
320 garbage_collection(whole);
323 #ifdef CONFIG_NO_ROOT_EXEC
324 static void
325 check_if_root(void)
327 if (!getuid() || !geteuid()) {
328 fprintf(stderr, "%s\n\n"
329 "Permission to run this program as root "
330 "user was disabled at compile time.\n\n",
331 full_static_version);
332 exit(-1);
335 #else
336 #define check_if_root()
337 #endif
340 main(int argc, char *argv[])
342 check_if_root();
344 program.terminate = 0;
345 program.retval = RET_OK;
346 program.path = argv[0];
347 ac = argc;
348 av = (unsigned char **) argv;
350 select_loop(init);
351 terminate_all_subsystems();
353 #ifdef DEBUG_MEMLEAK
354 check_memory_leaks();
355 #endif
356 return program.retval;