2011-03-12 18:08:07 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Daniel Silverstone <dsilvers@digital-scurf.org>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-02-08 01:19:47 +03:00
|
|
|
#include <limits.h>
|
2016-02-12 16:36:12 +03:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2011-03-12 18:08:07 +03:00
|
|
|
|
2016-02-05 18:36:18 +03:00
|
|
|
#include "utils/config.h"
|
2014-10-16 12:52:43 +04:00
|
|
|
#include "utils/log.h"
|
2015-06-22 12:42:01 +03:00
|
|
|
#include "utils/messages.h"
|
2014-10-16 12:52:43 +04:00
|
|
|
#include "utils/filepath.h"
|
2013-05-26 01:46:27 +04:00
|
|
|
#include "utils/nsoption.h"
|
2011-03-12 18:08:07 +03:00
|
|
|
#include "content/urldb.h"
|
2016-02-12 16:36:12 +03:00
|
|
|
#include "content/fetchers.h"
|
2011-03-12 18:08:07 +03:00
|
|
|
#include "content/fetchers/resource.h"
|
2014-10-17 12:27:58 +04:00
|
|
|
#include "content/hlcache.h"
|
2014-10-16 12:52:43 +04:00
|
|
|
#include "desktop/gui_misc.h"
|
2011-03-12 18:08:07 +03:00
|
|
|
#include "desktop/netsurf.h"
|
|
|
|
|
2014-03-08 18:13:27 +04:00
|
|
|
#include "monkey/dispatch.h"
|
|
|
|
#include "monkey/browser.h"
|
|
|
|
#include "monkey/cert.h"
|
|
|
|
#include "monkey/401login.h"
|
|
|
|
#include "monkey/filetype.h"
|
|
|
|
#include "monkey/fetch.h"
|
|
|
|
#include "monkey/schedule.h"
|
2015-04-16 00:03:34 +03:00
|
|
|
#include "monkey/bitmap.h"
|
2014-03-08 18:13:27 +04:00
|
|
|
|
2016-02-05 18:36:18 +03:00
|
|
|
/** maximum number of languages in language vector */
|
|
|
|
#define LANGV_SIZE 32
|
|
|
|
/** maximum length of all strings in language vector */
|
|
|
|
#define LANGS_SIZE 4096
|
|
|
|
|
|
|
|
/** resource search path vector */
|
|
|
|
char **respaths;
|
2011-03-12 18:08:07 +03:00
|
|
|
|
2014-10-13 00:18:17 +04:00
|
|
|
static bool monkey_done = false;
|
|
|
|
|
2014-10-26 15:42:53 +03:00
|
|
|
/**
|
|
|
|
* Cause an abnormal program termination.
|
|
|
|
*
|
|
|
|
* \note This never returns and is intended to terminate without any cleanup.
|
|
|
|
*
|
|
|
|
* \param error The message to display to the user.
|
|
|
|
*/
|
|
|
|
static void die(const char * const error)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "DIE %s\n", error);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-02-05 18:36:18 +03:00
|
|
|
/** obtain language from environment
|
|
|
|
*
|
|
|
|
* start with GNU extension LANGUAGE environment variable and then try
|
|
|
|
* POSIX variables LC_ALL, LC_MESSAGES and LANG
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static const char *get_language(void)
|
|
|
|
{
|
|
|
|
const char *lang;
|
|
|
|
|
|
|
|
lang = getenv("LANGUAGE");
|
|
|
|
if ((lang != NULL) && (lang[0] != '\0')) {
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
lang = getenv("LC_ALL");
|
|
|
|
if ((lang != NULL) && (lang[0] != '\0')) {
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
lang = getenv("LC_MESSAGES");
|
|
|
|
if ((lang != NULL) && (lang[0] != '\0')) {
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
lang = getenv("LANG");
|
|
|
|
if ((lang != NULL) && (lang[0] != '\0')) {
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
* vector will always have the C language as its last entry.
|
|
|
|
*
|
|
|
|
* This implementation creates an internal static representation of
|
|
|
|
* the vector when first called and returns that for all subsequent
|
|
|
|
* calls. i.e. changing the environment does not change the returned
|
|
|
|
* vector on repeated calls.
|
|
|
|
*
|
|
|
|
* If the environment variables have more than LANGV_SIZE languages or
|
|
|
|
* LANGS_SIZE bytes of data the results list will be curtailed.
|
|
|
|
*/
|
|
|
|
static const char * const *get_languagev(void)
|
|
|
|
{
|
|
|
|
static const char *langv[LANGV_SIZE];
|
|
|
|
int langidx = 0; /* index of next entry in vector */
|
|
|
|
static char langs[LANGS_SIZE];
|
|
|
|
char *curp; /* next language parameter in langs string */
|
|
|
|
const char *lange; /* language from environment variable */
|
|
|
|
int lang_len;
|
|
|
|
char *cln; /* colon in lange */
|
|
|
|
|
|
|
|
/* return cached vector */
|
|
|
|
if (langv[0] != NULL) {
|
|
|
|
return &langv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
curp = &langs[0];
|
|
|
|
|
|
|
|
lange = get_language();
|
|
|
|
|
|
|
|
if (lange != NULL) {
|
|
|
|
lang_len = strlen(lange) + 1;
|
|
|
|
if (lang_len < (LANGS_SIZE - 2)) {
|
|
|
|
memcpy(curp, lange, lang_len);
|
|
|
|
while ((curp[0] != 0) &&
|
|
|
|
(langidx < (LANGV_SIZE - 2))) {
|
|
|
|
/* avoid using strchrnul as it is not portable */
|
|
|
|
cln = strchr(curp, ':');
|
|
|
|
if (cln == NULL) {
|
|
|
|
langv[langidx++] = curp;
|
|
|
|
curp += lang_len;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if ((cln - curp) > 1) {
|
|
|
|
/* only place non empty entries in vector */
|
|
|
|
langv[langidx++] = curp;
|
|
|
|
}
|
|
|
|
*cln++ = 0; /* null terminate */
|
|
|
|
lang_len -= (cln - curp);
|
|
|
|
curp = cln;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure C language is present */
|
|
|
|
langv[langidx++] = curp;
|
|
|
|
*curp++ = 'C';
|
|
|
|
*curp++ = 0;
|
|
|
|
langv[langidx] = NULL;
|
|
|
|
|
|
|
|
return &langv[0];
|
|
|
|
}
|
|
|
|
|
2011-03-12 18:08:07 +03:00
|
|
|
/* Stolen from gtk/gui.c */
|
|
|
|
static char **
|
|
|
|
nsmonkey_init_resource(const char *resource_path)
|
|
|
|
{
|
2016-02-05 18:36:18 +03:00
|
|
|
const char * const *langv;
|
2014-01-26 03:00:22 +04:00
|
|
|
char **pathv; /* resource path string vector */
|
|
|
|
char **respath; /* resource paths vector */
|
2011-03-12 18:08:07 +03:00
|
|
|
|
2014-01-26 03:00:22 +04:00
|
|
|
pathv = filepath_path_to_strvec(resource_path);
|
2011-03-12 18:08:07 +03:00
|
|
|
|
2016-02-05 18:36:18 +03:00
|
|
|
langv = get_languagev();
|
2011-03-12 18:08:07 +03:00
|
|
|
|
2014-01-26 03:00:22 +04:00
|
|
|
respath = filepath_generate(pathv, langv);
|
2011-03-12 18:08:07 +03:00
|
|
|
|
2014-01-26 03:00:22 +04:00
|
|
|
filepath_free_strvec(pathv);
|
2011-03-12 18:08:07 +03:00
|
|
|
|
2014-01-26 03:00:22 +04:00
|
|
|
return respath;
|
2011-03-12 18:08:07 +03:00
|
|
|
}
|
|
|
|
|
2014-01-12 02:34:12 +04:00
|
|
|
static void monkey_quit(void)
|
2011-03-12 18:08:07 +03:00
|
|
|
{
|
2014-01-26 03:00:22 +04:00
|
|
|
urldb_save_cookies(nsoption_charp(cookie_jar));
|
|
|
|
urldb_save(nsoption_charp(url_file));
|
|
|
|
monkey_fetch_filetype_fin();
|
2011-03-12 18:08:07 +03:00
|
|
|
}
|
|
|
|
|
2014-05-28 02:52:35 +04:00
|
|
|
static nserror gui_launch_url(struct nsurl *url)
|
2011-03-12 20:27:18 +03:00
|
|
|
{
|
2014-05-28 02:52:35 +04:00
|
|
|
fprintf(stdout, "GENERIC LAUNCH URL %s\n", nsurl_access(url));
|
|
|
|
return NSERROR_OK;
|
2011-03-12 20:27:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void quit_handler(int argc, char **argv)
|
|
|
|
{
|
2014-10-13 00:18:17 +04:00
|
|
|
monkey_done = true;
|
2011-03-12 20:27:18 +03:00
|
|
|
}
|
|
|
|
|
2013-05-27 20:27:56 +04:00
|
|
|
/**
|
|
|
|
* Set option defaults for monkey frontend
|
|
|
|
*
|
|
|
|
* @param defaults The option table to update.
|
|
|
|
* @return error status.
|
|
|
|
*/
|
|
|
|
static nserror set_defaults(struct nsoption_s *defaults)
|
2012-10-17 17:20:18 +04:00
|
|
|
{
|
2014-10-12 23:40:40 +04:00
|
|
|
/* Set defaults for absent option strings */
|
|
|
|
nsoption_setnull_charp(cookie_file, strdup("~/.netsurf/Cookies"));
|
|
|
|
nsoption_setnull_charp(cookie_jar, strdup("~/.netsurf/Cookies"));
|
|
|
|
nsoption_setnull_charp(url_file, strdup("~/.netsurf/URLs"));
|
|
|
|
|
2013-05-27 20:27:56 +04:00
|
|
|
return NSERROR_OK;
|
2012-10-17 17:20:18 +04:00
|
|
|
}
|
|
|
|
|
2013-05-23 14:50:57 +04:00
|
|
|
/**
|
|
|
|
* Ensures output logging stream is correctly configured
|
|
|
|
*/
|
|
|
|
static bool nslog_stream_configure(FILE *fptr)
|
|
|
|
{
|
|
|
|
/* set log stream to be non-buffering */
|
|
|
|
setbuf(fptr, NULL);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-16 00:50:28 +03:00
|
|
|
static struct gui_misc_table monkey_misc_table = {
|
2014-03-08 18:13:27 +04:00
|
|
|
.schedule = monkey_schedule,
|
2014-01-23 03:19:57 +04:00
|
|
|
|
2014-01-12 02:34:12 +04:00
|
|
|
.quit = monkey_quit,
|
2014-01-15 16:28:55 +04:00
|
|
|
.launch_url = gui_launch_url,
|
|
|
|
.cert_verify = gui_cert_verify,
|
2014-01-16 04:52:17 +04:00
|
|
|
.login = gui_401login_open,
|
2014-01-11 18:20:30 +04:00
|
|
|
};
|
|
|
|
|
2016-02-12 16:36:12 +03:00
|
|
|
static void monkey_run(void)
|
|
|
|
{
|
|
|
|
fd_set read_fd_set, write_fd_set, exc_fd_set;
|
|
|
|
int max_fd;
|
|
|
|
int rdy_fd;
|
|
|
|
int schedtm;
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval* timeout;
|
|
|
|
|
|
|
|
while (!monkey_done) {
|
|
|
|
|
|
|
|
/* clears fdset */
|
|
|
|
fetcher_fdset(&read_fd_set, &write_fd_set, &exc_fd_set, &max_fd);
|
|
|
|
|
|
|
|
/* add stdin to the set */
|
|
|
|
if (max_fd < 0) {
|
|
|
|
max_fd = 0;
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
LOG("Iterate blocking");
|
|
|
|
fprintf(stdout, "GENERIC POLL BLOCKING\n");
|
|
|
|
timeout = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
LOG("Iterate immediate");
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
timeout = &tv;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
LOG("Iterate non-blocking");
|
|
|
|
fprintf(stdout, "GENERIC POLL TIMED\n");
|
|
|
|
tv.tv_sec = schedtm / 1000; /* miliseconds to seconds */
|
|
|
|
tv.tv_usec = (schedtm % 1000) * 1000; /* remainder to microseconds */
|
|
|
|
timeout = &tv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdy_fd = select(max_fd + 1,
|
|
|
|
&read_fd_set,
|
|
|
|
&write_fd_set,
|
|
|
|
&exc_fd_set,
|
|
|
|
timeout);
|
|
|
|
if (rdy_fd < 0) {
|
|
|
|
monkey_done = true;
|
|
|
|
} else if (rdy_fd > 0) {
|
|
|
|
if (FD_ISSET(0, &read_fd_set)) {
|
|
|
|
monkey_process_command();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-12 18:08:07 +03:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *messages;
|
|
|
|
char *options;
|
|
|
|
char buf[PATH_MAX];
|
2013-05-27 20:27:56 +04:00
|
|
|
nserror ret;
|
2014-05-10 13:22:10 +04:00
|
|
|
struct netsurf_table monkey_table = {
|
2016-04-16 00:50:28 +03:00
|
|
|
.misc = &monkey_misc_table,
|
2014-01-15 23:37:05 +04:00
|
|
|
.window = monkey_window_table,
|
|
|
|
.download = monkey_download_table,
|
2014-01-26 03:00:22 +04:00
|
|
|
.fetch = monkey_fetch_table,
|
2015-04-16 00:03:34 +03:00
|
|
|
.bitmap = monkey_bitmap_table,
|
2014-01-15 23:37:05 +04:00
|
|
|
};
|
2013-05-27 20:27:56 +04:00
|
|
|
|
2014-05-10 13:22:10 +04:00
|
|
|
ret = netsurf_register(&monkey_table);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
die("NetSurf operation table failed registration");
|
|
|
|
}
|
|
|
|
|
2011-03-12 18:08:07 +03:00
|
|
|
/* Unbuffer stdin/out/err */
|
|
|
|
setbuf(stdin, NULL);
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
setbuf(stderr, NULL);
|
2014-01-26 03:00:22 +04:00
|
|
|
|
2011-03-12 18:08:07 +03:00
|
|
|
/* Prep the search paths */
|
|
|
|
respaths = nsmonkey_init_resource("${HOME}/.netsurf/:${NETSURFRES}:"MONKEY_RESPATH":./monkey/res");
|
|
|
|
|
2013-05-23 14:50:57 +04:00
|
|
|
/* initialise logging. Not fatal if it fails but not much we can do
|
|
|
|
* about it either.
|
|
|
|
*/
|
|
|
|
nslog_init(nslog_stream_configure, &argc, argv);
|
|
|
|
|
2013-05-27 20:27:56 +04:00
|
|
|
/* user options setup */
|
|
|
|
ret = nsoption_init(set_defaults, &nsoptions, &nsoptions_default);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
die("Options failed to initialise");
|
|
|
|
}
|
|
|
|
options = filepath_find(respaths, "Choices");
|
2013-06-01 18:49:17 +04:00
|
|
|
nsoption_read(options, nsoptions);
|
2011-03-12 18:08:07 +03:00
|
|
|
free(options);
|
2013-06-01 18:49:17 +04:00
|
|
|
nsoption_commandline(&argc, argv, nsoptions);
|
2013-05-27 20:27:56 +04:00
|
|
|
|
|
|
|
messages = filepath_find(respaths, "Messages");
|
2015-06-19 18:29:42 +03:00
|
|
|
ret = messages_add_from_file(messages);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
LOG("Messages failed to load");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* common initialisation */
|
|
|
|
ret = netsurf_init(NULL);
|
2013-05-27 20:27:56 +04:00
|
|
|
free(messages);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
die("NetSurf failed to initialise");
|
|
|
|
}
|
2014-01-26 03:00:22 +04:00
|
|
|
|
2011-03-17 14:26:30 +03:00
|
|
|
filepath_sfinddef(respaths, buf, "mime.types", "/etc/");
|
2014-01-23 03:19:57 +04:00
|
|
|
monkey_fetch_filetype_init(buf);
|
2014-01-26 03:00:22 +04:00
|
|
|
|
2012-03-22 13:34:34 +04:00
|
|
|
urldb_load(nsoption_charp(url_file));
|
|
|
|
urldb_load_cookies(nsoption_charp(cookie_file));
|
2014-01-26 03:00:22 +04:00
|
|
|
|
2014-10-26 15:42:53 +03:00
|
|
|
ret = monkey_register_handler("QUIT", quit_handler);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
die("quit handler failed to register");
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = monkey_register_handler("WINDOW", monkey_window_handle_command);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
die("window handler fialed to register");
|
|
|
|
}
|
2014-01-26 03:00:22 +04:00
|
|
|
|
2011-03-12 20:27:18 +03:00
|
|
|
fprintf(stdout, "GENERIC STARTED\n");
|
2016-02-12 16:36:12 +03:00
|
|
|
monkey_run();
|
2014-10-13 00:18:17 +04:00
|
|
|
|
2011-03-12 20:27:18 +03:00
|
|
|
fprintf(stdout, "GENERIC CLOSING_DOWN\n");
|
2011-03-12 20:54:02 +03:00
|
|
|
monkey_kill_browser_windows();
|
2014-01-26 03:00:22 +04:00
|
|
|
|
2011-03-12 18:08:07 +03:00
|
|
|
netsurf_exit();
|
2011-03-12 20:27:18 +03:00
|
|
|
fprintf(stdout, "GENERIC FINISHED\n");
|
2013-06-01 18:49:17 +04:00
|
|
|
|
|
|
|
/* finalise options */
|
|
|
|
nsoption_finalise(nsoptions, nsoptions_default);
|
|
|
|
|
2011-03-12 18:08:07 +03:00
|
|
|
return 0;
|
|
|
|
}
|