diff options
Diffstat (limited to 'frontends/monkey/main.c')
-rw-r--r-- | frontends/monkey/main.c | 128 |
1 files changed, 112 insertions, 16 deletions
diff --git a/frontends/monkey/main.c b/frontends/monkey/main.c index 53cde5a72..463f0bea6 100644 --- a/frontends/monkey/main.c +++ b/frontends/monkey/main.c @@ -23,6 +23,8 @@ #include <sys/types.h> #include <unistd.h> #include <string.h> +#include <errno.h> +#include <signal.h> #include "utils/config.h" #include "utils/sys_time.h" @@ -36,10 +38,11 @@ #include "netsurf/url_db.h" #include "netsurf/cookie_db.h" #include "content/fetch.h" +#include "content/backing_store.h" +#include "monkey/output.h" #include "monkey/dispatch.h" #include "monkey/browser.h" -#include "monkey/cert.h" #include "monkey/401login.h" #include "monkey/filetype.h" #include "monkey/fetch.h" @@ -66,11 +69,12 @@ static bool monkey_done = false; */ static void die(const char * const error) { - fprintf(stderr, "DIE %s\n", error); + moutf(MOUT_DIE, "%s", error); exit(EXIT_FAILURE); } -/** obtain language from environment +/** + * obtain language from environment * * start with GNU extension LANGUAGE environment variable and then try * POSIX variables LC_ALL, LC_MESSAGES and LANG @@ -104,7 +108,8 @@ static const char *get_language(void) } -/** provide a string vector of languages in preference order +/** + * provide a string vector of languages in preference order * * environment variables are processed to aquire a colon separated * list of languages which are converted into a string vector. The @@ -171,7 +176,16 @@ static const char * const *get_languagev(void) return &langv[0]; } -/* Stolen from gtk/gui.c */ +/** + * Create an array of valid paths to search for resources. + * + * The idea is that all the complex path computation to find resources + * is performed here, once, rather than every time a resource is + * searched for. + * + * \param resource_path A shell style colon separated path list + * \return A string vector of valid paths where resources can be found + */ static char ** nsmonkey_init_resource(const char *resource_path) { @@ -199,7 +213,17 @@ static void monkey_quit(void) static nserror gui_launch_url(struct nsurl *url) { - fprintf(stdout, "GENERIC LAUNCH URL %s\n", nsurl_access(url)); + moutf(MOUT_GENERIC, "LAUNCH URL %s", nsurl_access(url)); + return NSERROR_OK; +} + +static nserror gui_present_cookies(const char *search_term) +{ + if (search_term != NULL) { + moutf(MOUT_GENERIC, "PRESENT_COOKIES %s", search_term); + } else { + moutf(MOUT_GENERIC, "PRESENT_COOKIES"); + } return NSERROR_OK; } @@ -243,12 +267,11 @@ static bool nslog_stream_configure(FILE *fptr) static struct gui_misc_table monkey_misc_table = { .schedule = monkey_schedule, - .warning = monkey_warn_user, .quit = monkey_quit, .launch_url = gui_launch_url, - .cert_verify = gui_cert_verify, .login = gui_401login_open, + .present_cookies = gui_present_cookies, }; static void monkey_run(void) @@ -262,6 +285,9 @@ static void monkey_run(void) while (!monkey_done) { + /* discover the next scheduled event time */ + schedtm = monkey_schedule_run(); + /* clears fdset */ fetch_fdset(&read_fd_set, &write_fd_set, &exc_fd_set, &max_fd); @@ -272,14 +298,11 @@ static void monkey_run(void) FD_SET(0, &read_fd_set); FD_SET(0, &exc_fd_set); - /* discover the next scheduled event time */ - schedtm = monkey_schedule_run(); - /* setup timeout */ switch (schedtm) { case -1: NSLOG(netsurf, INFO, "Iterate blocking"); - fprintf(stdout, "GENERIC POLL BLOCKING\n"); + moutf(MOUT_GENERIC, "POLL BLOCKING"); timeout = NULL; break; @@ -292,7 +315,7 @@ static void monkey_run(void) default: NSLOG(netsurf, INFO, "Iterate non-blocking"); - fprintf(stdout, "GENERIC POLL TIMED %d\n", schedtm); + moutf(MOUT_GENERIC, "POLL TIMED %d", schedtm); tv.tv_sec = schedtm / 1000; /* miliseconds to seconds */ tv.tv_usec = (schedtm % 1000) * 1000; /* remainder to microseconds */ timeout = &tv; @@ -305,6 +328,7 @@ static void monkey_run(void) &exc_fd_set, timeout); if (rdy_fd < 0) { + NSLOG(netsurf, CRITICAL, "Unable to select: %s", strerror(errno)); monkey_done = true; } else if (rdy_fd > 0) { if (FD_ISSET(0, &read_fd_set)) { @@ -314,6 +338,53 @@ static void monkey_run(void) } } +#if (!defined(NDEBUG) && defined(HAVE_EXECINFO)) +#include <execinfo.h> +static void *backtrace_buffer[4096]; + +void +__assert_fail(const char *__assertion, const char *__file, + unsigned int __line, const char *__function) +{ + int frames; + fprintf(stderr, + "MONKEY: Assertion failure!\n" + "%s:%d: %s: Assertion `%s` failed.\n", + __file, __line, __function, __assertion); + + frames = backtrace(&backtrace_buffer[0], 4096); + if (frames > 0 && frames < 4096) { + fprintf(stderr, "Backtrace:\n"); + fflush(stderr); + backtrace_symbols_fd(&backtrace_buffer[0], frames, 2); + } + + abort(); +} + +static void +signal_handler(int sig) +{ + int frames; + fprintf(stderr, "Caught signal %s (%d)\n", + ((sig == SIGSEGV) ? "SIGSEGV" : + ((sig == SIGILL) ? "SIGILL" : + ((sig == SIGFPE) ? "SIGFPE" : + ((sig == SIGBUS) ? "SIGBUS" : + "unknown signal")))), + sig); + frames = backtrace(&backtrace_buffer[0], 4096); + if (frames > 0 && frames < 4096) { + fprintf(stderr, "Backtrace:\n"); + fflush(stderr); + backtrace_symbols_fd(&backtrace_buffer[0], frames, 2); + } + + abort(); +} + +#endif + int main(int argc, char **argv) { @@ -328,8 +399,18 @@ main(int argc, char **argv) .fetch = monkey_fetch_table, .bitmap = monkey_bitmap_table, .layout = monkey_layout_table, + .llcache = filesystem_llcache_table, }; +#if (!defined(NDEBUG) && defined(HAVE_EXECINFO)) + /* Catch segfault, illegal instructions and fp exceptions */ + signal(SIGSEGV, signal_handler); + signal(SIGILL, signal_handler); + signal(SIGFPE, signal_handler); + /* It's unlikely, but SIGBUS could happen on some platforms */ + signal(SIGBUS, signal_handler); +#endif + ret = netsurf_register(&monkey_table); if (ret != NSERROR_OK) { die("NetSurf operation table failed registration"); @@ -377,6 +458,12 @@ main(int argc, char **argv) urldb_load(nsoption_charp(url_file)); urldb_load_cookies(nsoption_charp(cookie_file)); + /* Free resource paths now we're done finding resources */ + for (char **s = respaths; *s != NULL; s++) { + free(*s); + } + free(respaths); + ret = monkey_register_handler("QUIT", quit_handler); if (ret != NSERROR_OK) { die("quit handler failed to register"); @@ -392,14 +479,20 @@ main(int argc, char **argv) die("options handler failed to register"); } - fprintf(stdout, "GENERIC STARTED\n"); + ret = monkey_register_handler("LOGIN", monkey_login_handle_command); + if (ret != NSERROR_OK) { + die("login handler failed to register"); + } + + + moutf(MOUT_GENERIC, "STARTED"); monkey_run(); - fprintf(stdout, "GENERIC CLOSING_DOWN\n"); + moutf(MOUT_GENERIC, "CLOSING_DOWN"); monkey_kill_browser_windows(); netsurf_exit(); - fprintf(stdout, "GENERIC FINISHED\n"); + moutf(MOUT_GENERIC, "FINISHED"); /* finalise options */ nsoption_finalise(nsoptions, nsoptions_default); @@ -407,5 +500,8 @@ main(int argc, char **argv) /* finalise logging */ nslog_finalise(); + /* And free any monkey-specific bits */ + monkey_free_handlers(); + return 0; } |