+void
+die(const char *errstr, ...)
+{
+ va_list ap;
+
+ va_start(ap, errstr);
+ vfprintf(stderr, errstr, ap);
+ va_end(ap);
+ exit(1);
+}
+
+void
+setup(void)
+{
+ GdkDisplay *gdpy;
+ int i, j;
+
+ /* clean up any zombies immediately */
+ sigchld(0);
+ if (signal(SIGHUP, sighup) == SIG_ERR)
+ die("Can't install SIGHUP handler");
+
+ if (!(dpy = XOpenDisplay(NULL)))
+ die("Can't open default display");
+
+ /* atoms */
+ atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
+ atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
+ atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
+
+ gtk_init(NULL, NULL);
+
+ gdpy = gdk_display_get_default();
+
+ curconfig = defconfig;
+
+ /* dirs and files */
+ cookiefile = buildfile(cookiefile);
+ scriptfile = buildfile(scriptfile);
+ cachedir = buildpath(cachedir);
+ certdir = buildpath(certdir);
+
+ gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
+
+ for (i = 0; i < LENGTH(certs); ++i) {
+ if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
+ certs[i].file = g_strconcat(certdir, "/", certs[i].file,
+ NULL);
+ } else {
+ fprintf(stderr, "Could not compile regex: %s\n",
+ certs[i].regex);
+ certs[i].regex = NULL;
+ }
+ }
+
+ if (!stylefile) {
+ styledir = buildpath(styledir);
+ for (i = 0; i < LENGTH(styles); ++i) {
+ if (!regcomp(&(styles[i].re), styles[i].regex,
+ REG_EXTENDED)) {
+ styles[i].file = g_strconcat(styledir, "/",
+ styles[i].file, NULL);
+ } else {
+ fprintf(stderr, "Could not compile regex: %s\n",
+ styles[i].regex);
+ styles[i].regex = NULL;
+ }
+ }
+ g_free(styledir);
+ } else {
+ stylefile = buildfile(stylefile);
+ }
+
+ for (i = 0; i < LENGTH(uriparams); ++i) {
+ if (!regcomp(&(uriparams[i].re), uriparams[i].uri,
+ REG_EXTENDED)) {
+ /* copy default parameters if they are not already set
+ * or if they are forced */
+ for (j = 0; j < ParameterLast; ++j) {
+ if (!uriparams[i].config[j].force ||
+ defconfig[j].force)
+ uriparams[i].config[j] = defconfig[j];
+ }
+ } else {
+ fprintf(stderr, "Could not compile regex: %s\n",
+ uriparams[i].uri);
+ uriparams[i].uri = NULL;
+ }
+ }
+}
+
+void
+sigchld(int unused)
+{
+ if (signal(SIGCHLD, sigchld) == SIG_ERR)
+ die("Can't install SIGCHLD handler");
+ while (waitpid(-1, NULL, WNOHANG) > 0)
+ ;
+}
+
+void
+sighup(int unused)
+{
+ Arg a = { .b = 0 };
+ Client *c;
+
+ for (c = clients; c; c = c->next)
+ reload(c, &a);
+}
+
+char *
+buildfile(const char *path)
+{
+ char *dname, *bname, *bpath, *fpath;
+ FILE *f;
+
+ dname = g_path_get_dirname(path);
+ bname = g_path_get_basename(path);
+
+ bpath = buildpath(dname);
+ g_free(dname);
+
+ fpath = g_build_filename(bpath, bname, NULL);
+ g_free(bpath);
+ g_free(bname);
+
+ if (!(f = fopen(fpath, "a")))
+ die("Could not open file: %s\n", fpath);
+
+ g_chmod(fpath, 0600); /* always */
+ fclose(f);
+
+ return fpath;
+}
+
+static const char*
+getuserhomedir(const char *user)
+{
+ struct passwd *pw = getpwnam(user);
+
+ if (!pw)
+ die("Can't get user %s login information.\n", user);
+
+ return pw->pw_dir;
+}
+
+static const char*
+getcurrentuserhomedir(void)
+{
+ const char *homedir;
+ const char *user;
+ struct passwd *pw;
+
+ homedir = getenv("HOME");
+ if (homedir)
+ return homedir;
+
+ user = getenv("USER");
+ if (user)
+ return getuserhomedir(user);
+
+ pw = getpwuid(getuid());
+ if (!pw)
+ die("Can't get current user home directory\n");
+
+ return pw->pw_dir;
+}
+
+char *
+buildpath(const char *path)
+{
+ char *apath, *name, *p, *fpath;
+ const char *homedir;
+
+ if (path[0] == '~') {
+ if (path[1] == '/' || path[1] == '\0') {
+ p = (char *)&path[1];
+ homedir = getcurrentuserhomedir();
+ } else {
+ if ((p = strchr(path, '/')))
+ name = g_strndup(&path[1], --p - path);
+ else
+ name = g_strdup(&path[1]);
+
+ homedir = getuserhomedir(name);
+ g_free(name);
+ }
+ apath = g_build_filename(homedir, p, NULL);
+ } else {
+ apath = g_strdup(path);
+ }
+
+ /* creating directory */
+ if (g_mkdir_with_parents(apath, 0700) < 0)
+ die("Could not access directory: %s\n", apath);
+
+ fpath = realpath(apath, NULL);
+ g_free(apath);
+
+ return fpath;
+}
+
+Client *
+newclient(Client *rc)
+{
+ Client *c;
+
+ if (!(c = calloc(1, sizeof(Client))))
+ die("Cannot malloc!\n");
+
+ c->next = clients;
+ clients = c;
+
+ c->progress = 100;
+ c->view = newview(c, rc ? rc->view : NULL);
+
+ return c;
+}
+
+void
+loaduri(Client *c, const Arg *a)
+{
+ struct stat st;
+ char *url, *path;
+ const char *uri = a->v;
+
+ if (g_strcmp0(uri, "") == 0)
+ return;
+
+ if (g_str_has_prefix(uri, "http://") ||
+ g_str_has_prefix(uri, "https://") ||
+ g_str_has_prefix(uri, "file://") ||
+ g_str_has_prefix(uri, "about:")) {
+ url = g_strdup(uri);
+ } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
+ url = g_strdup_printf("file://%s", path);
+ free(path);
+ } else {
+ url = g_strdup_printf("http://%s", uri);
+ }
+
+ setatom(c, AtomUri, url);
+
+ if (strcmp(url, geturi(c)) == 0) {
+ reload(c, a);
+ } else {
+ webkit_web_view_load_uri(c->view, url);
+ updatetitle(c);
+ }
+
+ g_free(url);
+}
+
+const char *
+geturi(Client *c)
+{
+ const char *uri;
+
+ if (!(uri = webkit_web_view_get_uri(c->view)))
+ uri = "about:blank";
+ return uri;
+}
+
+void
+setatom(Client *c, int a, const char *v)
+{
+ XSync(dpy, False);
+ XChangeProperty(dpy, c->xid,
+ atoms[a], XA_STRING, 8, PropModeReplace,
+ (unsigned char *)v, strlen(v) + 1);
+}
+
+const char *
+getatom(Client *c, int a)
+{
+ static char buf[BUFSIZ];
+ Atom adummy;
+ int idummy;
+ unsigned long ldummy;
+ unsigned char *p = NULL;
+
+ XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
+ &adummy, &idummy, &ldummy, &ldummy, &p);
+ if (p)
+ strncpy(buf, (char *)p, LENGTH(buf) - 1);
+ else
+ buf[0] = '\0';
+ XFree(p);
+
+ return buf;
+}
+
+void
+updatetitle(Client *c)
+{
+ char *title;
+ const char *name = c->overtitle ? c->overtitle :
+ c->title ? c->title : "";
+
+ if (curconfig[ShowIndicators].val.b) {
+ gettogglestats(c);
+ getpagestats(c);
+
+ if (c->progress != 100)
+ title = g_strdup_printf("[%i%%] %s:%s | %s",
+ c->progress, togglestats, pagestats, name);
+ else
+ title = g_strdup_printf("%s:%s | %s",
+ togglestats, pagestats, name);
+
+ gtk_window_set_title(GTK_WINDOW(c->win), title);
+ g_free(title);
+ } else {
+ gtk_window_set_title(GTK_WINDOW(c->win), name);
+ }
+}
+
+void
+gettogglestats(Client *c)
+{
+ togglestats[0] = cookiepolicy_set(cookiepolicy_get());
+ togglestats[1] = curconfig[CaretBrowsing].val.b ? 'C' : 'c';
+ togglestats[2] = curconfig[Geolocation].val.b ? 'G' : 'g';
+ togglestats[3] = curconfig[DiskCache].val.b ? 'D' : 'd';
+ togglestats[4] = curconfig[LoadImages].val.b ? 'I' : 'i';
+ togglestats[5] = curconfig[JavaScript].val.b ? 'S' : 's';
+ togglestats[6] = curconfig[Plugins].val.b ? 'V' : 'v';
+ togglestats[7] = curconfig[Style].val.b ? 'M' : 'm';
+ togglestats[8] = curconfig[FrameFlattening].val.b ? 'F' : 'f';
+ togglestats[9] = curconfig[Certificate].val.b ? 'X' : 'x';
+ togglestats[10] = curconfig[StrictTLS].val.b ? 'T' : 't';
+ togglestats[11] = '\0';
+}
+
+void
+getpagestats(Client *c)
+{
+ if (c->https)
+ pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
+ else
+ pagestats[0] = '-';
+ pagestats[1] = '\0';
+}
+
+WebKitCookieAcceptPolicy
+cookiepolicy_get(void)
+{
+ switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
+ case 'a':
+ return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
+ case '@':
+ return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
+ default: /* fallthrough */
+ case 'A':
+ return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
+ }
+}
+
+char
+cookiepolicy_set(const WebKitCookieAcceptPolicy p)
+{
+ switch (p) {
+ case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
+ return 'a';
+ case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
+ return '@';
+ default: /* fallthrough */
+ case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
+ return 'A';
+ }
+}
+
+void
+seturiparameters(Client *c, const char *uri)
+{
+ int i;
+
+ for (i = 0; i < LENGTH(uriparams); ++i) {
+ if (uriparams[i].uri &&
+ !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
+ curconfig = uriparams[i].config;
+ break;
+ }
+ }
+
+ for (i = 0; i < ParameterLast; ++i)
+ setparameter(c, 0, i, &curconfig[i].val);
+}
+
+void
+setparameter(Client *c, int refresh, ParamName p, const Arg *a)
+{
+ GdkRGBA bgcolor = { 0 };
+ WebKitSettings *s = webkit_web_view_get_settings(c->view);
+
+ switch (p) {
+ case AcceleratedCanvas:
+ webkit_settings_set_enable_accelerated_2d_canvas(s, a->b);
+ break;
+ case CaretBrowsing:
+ webkit_settings_set_enable_caret_browsing(s, a->b);
+ refresh = 0;
+ break;
+ case Certificate:
+ if (a->b)
+ setcert(c, geturi(c));
+ return; /* do not update */
+ case CookiePolicies:
+ webkit_cookie_manager_set_accept_policy(
+ webkit_web_context_get_cookie_manager(
+ webkit_web_view_get_context(c->view)),
+ cookiepolicy_get());
+ refresh = 0;
+ break;
+ case DiskCache:
+ webkit_web_context_set_cache_model(
+ webkit_web_view_get_context(c->view), a->b ?
+ WEBKIT_CACHE_MODEL_WEB_BROWSER :
+ WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
+ return; /* do not update */
+ case DNSPrefetch:
+ webkit_settings_set_enable_dns_prefetching(s, a->b);
+ return; /* do not update */
+ case FontSize:
+ webkit_settings_set_default_font_size(s, a->i);
+ return; /* do not update */
+ case FrameFlattening:
+ webkit_settings_set_enable_frame_flattening(s, a->b);
+ break;
+ case Geolocation:
+ refresh = 0;
+ break;
+ case HideBackground:
+ if (a->b)
+ webkit_web_view_set_background_color(c->view, &bgcolor);
+ return; /* do not update */
+ case Inspector:
+ webkit_settings_set_enable_developer_extras(s, a->b);
+ return; /* do not update */
+ case JavaScript:
+ webkit_settings_set_enable_javascript(s, a->b);
+ break;
+ case KioskMode:
+ return; /* do nothing */
+ case LoadImages:
+ webkit_settings_set_auto_load_images(s, a->b);
+ break;
+ case MediaManualPlay:
+ webkit_settings_set_media_playback_requires_user_gesture(s, a->b);
+ break;
+ case Plugins:
+ webkit_settings_set_enable_plugins(s, a->b);
+ break;
+ case PreferredLanguages:
+ return; /* do nothing */
+ case RunInFullscreen:
+ return; /* do nothing */
+ case ScrollBars:
+ /* Disabled until we write some WebKitWebExtension for
+ * manipulating the DOM directly.
+ enablescrollbars = !enablescrollbars;
+ evalscript(c, "document.documentElement.style.overflow = '%s'",
+ enablescrollbars ? "auto" : "hidden");
+ */
+ return; /* do not update */
+ case ShowIndicators:
+ break;
+ case SiteQuirks:
+ webkit_settings_set_enable_site_specific_quirks(s, a->b);
+ break;
+ case SpellChecking:
+ webkit_web_context_set_spell_checking_enabled(
+ webkit_web_view_get_context(c->view), a->b);
+ return; /* do not update */
+ case SpellLanguages:
+ return; /* do nothing */
+ case StrictTLS:
+ webkit_web_context_set_tls_errors_policy(
+ webkit_web_view_get_context(c->view), a->b ?
+ WEBKIT_TLS_ERRORS_POLICY_FAIL :
+ WEBKIT_TLS_ERRORS_POLICY_IGNORE);
+ break;
+ case Style:
+ if (a->b)
+ setstyle(c, getstyle(geturi(c)));
+ else
+ webkit_user_content_manager_remove_all_style_sheets(
+ webkit_web_view_get_user_content_manager(c->view));
+ refresh = 0;
+ break;
+ case ZoomLevel:
+ webkit_web_view_set_zoom_level(c->view, a->f);
+ return; /* do not update */
+ default:
+ return; /* do nothing */
+ }
+
+ updatetitle(c);
+ if (refresh)
+ reload(c, a);
+}
+
+const char *
+getcert(const char *uri)
+{
+ int i;
+
+ for (i = 0; i < LENGTH(certs); ++i) {
+ if (certs[i].regex &&
+ !regexec(&(certs[i].re), uri, 0, NULL, 0))
+ return certs[i].file;
+ }
+
+ return NULL;