+void
+die(const char *errstr, ...)
+{
+ va_list ap;
+
+ va_start(ap, errstr);
+ vfprintf(stderr, errstr, ap);
+ va_end(ap);
+ exit(1);
+}
+
+void
+setup(void)
+{
+ int i;
+
+ /* clean up any zombies immediately */
+ sigchld(0);
+ gtk_init(NULL, NULL);
+
+ dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
+
+ /* atoms */
+ atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
+ atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
+ atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
+
+ /* dirs and files */
+ cookiefile = buildfile(cookiefile);
+ scriptfile = buildfile(scriptfile);
+ cachedir = buildpath(cachedir);
+
+ if (!stylefile) {
+ styledir = buildpath(styledir);
+ for (i = 0; i < LENGTH(styles); ++i) {
+ if (regcomp(&(styles[i].re), styles[i].regex,
+ REG_EXTENDED)) {
+ fprintf(stderr,
+ "Could not compile regex: %s\n",
+ styles[i].regex);
+ styles[i].regex = NULL;
+ }
+ styles[i].style = g_strconcat(styledir, "/",
+ styles[i].style, NULL);
+ }
+ g_free(styledir);
+ } else {
+ stylefile = buildfile(stylefile);
+ }
+}
+
+void
+sigchld(int unused)
+{
+ if (signal(SIGCHLD, sigchld) == SIG_ERR)
+ die("Can't install SIGCHLD handler");
+ while (waitpid(-1, NULL, WNOHANG) > 0)
+ ;
+}
+
+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;
+}
+
+char *
+buildpath(const char *path)
+{
+ struct passwd *pw;
+ char *apath, *name, *p, *fpath;
+
+ if (path[0] == '~') {
+ if (path[1] == '/' || path[1] == '\0') {
+ p = (char *)&path[1];
+ pw = getpwuid(getuid());
+ } else {
+ if ((p = strchr(path, '/')))
+ name = g_strndup(&path[1], --p - path);
+ else
+ name = g_strdup(&path[1]);
+
+ if (!(pw = getpwnam(name))) {
+ die("Can't get user %s home directory: %s.\n",
+ name, path);
+ }
+ g_free(name);
+ }
+ apath = g_build_filename(pw->pw_dir, 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->title = NULL;
+ c->progress = 100;
+
+ c->next = clients;
+ clients = c;
+
+ c->view = newview(c, rc ? rc->view : NULL);
+ c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
+
+ return c;
+}
+
+void
+addaccelgroup(Client *c)
+{
+ int i;
+ GtkAccelGroup *group = gtk_accel_group_new();
+ GClosure *closure;
+
+ for (i = 0; i < LENGTH(keys); i++) {
+ closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
+ gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
+ closure);
+ }
+ gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
+}
+
+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_strrstr(uri, "://") || 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);
+ c->title = geturi(c);
+ 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;
+
+ if (showindicators) {
+ gettogglestats(c);
+ getpagestats(c);
+
+ if (c->progress != 100) {
+ title = g_strdup_printf("[%i%%] %s:%s | %s",
+ c->progress, togglestats, pagestats,
+ c->targeturi ? c->targeturi : c->title);
+ } else {
+ title = g_strdup_printf("%s:%s | %s",
+ togglestats, pagestats,
+ c->targeturi ? c->targeturi : c->title);
+ }
+
+ gtk_window_set_title(GTK_WINDOW(c->win), title);
+ g_free(title);
+ } else {
+ gtk_window_set_title(GTK_WINDOW(c->win), c->title ?
+ c->title : "");
+ }
+}
+
+void
+gettogglestats(Client *c)
+{
+ togglestats[0] = cookiepolicy_set(cookiepolicy_get());
+ togglestats[1] = enablecaretbrowsing ? 'C' : 'c';
+ togglestats[2] = allowgeolocation ? 'G' : 'g';
+ togglestats[3] = enablecache ? 'D' : 'd';
+ togglestats[4] = loadimages ? 'I' : 'i';
+ togglestats[5] = enablescripts ? 'S' : 's';
+ togglestats[6] = enableplugins ? 'V' : 'v';
+ togglestats[7] = enablestyle ? 'M' : 'm';
+ togglestats[8] = enableframeflattening ? 'F' : 'f';
+ togglestats[9] = '\0';
+}
+
+void
+getpagestats(Client *c)
+{
+ pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
+ c->tlsflags > 0 ? 'U' : 'T';
+ pagestats[1] = '\0';
+}
+
+WebKitCookieAcceptPolicy
+cookiepolicy_get(void)
+{
+ switch (cookiepolicies[cookiepolicy]) {
+ case 'a':
+ return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
+ case '@':
+ return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
+ case 'A':
+ default:
+ break;
+ }
+
+ 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 '@';
+ case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
+ default:
+ break;
+ }
+
+ return 'A';
+}
+
+const char *
+getstyle(const char *uri)
+{
+ int i;
+
+ if (stylefile)
+ return stylefile;
+
+ for (i = 0; i < LENGTH(styles); ++i) {
+ if (styles[i].regex &&
+ !regexec(&(styles[i].re), uri, 0, NULL, 0))
+ return styles[i].style;
+ }
+
+ return "";
+}
+
+void
+setstyle(Client *c, const char *stylefile)
+{
+ gchar *style;
+
+ if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
+ fprintf(stderr, "Could not read style file: %s\n", stylefile);
+ return;
+ }
+
+ webkit_user_content_manager_add_style_sheet(
+ webkit_web_view_get_user_content_manager(c->view),
+ webkit_user_style_sheet_new(style,
+ WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+ WEBKIT_USER_STYLE_LEVEL_USER,
+ NULL, NULL));
+
+ g_free(style);
+}
+
+void
+runscript(Client *c)
+{
+ gchar *script;
+ gsize l;
+
+ if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
+ evalscript(c, script);
+ g_free(script);
+}
+
+void
+evalscript(Client *c, const char *jsstr, ...)
+{
+ va_list ap;
+ gchar *script;
+
+ va_start(ap, jsstr);
+ script = g_strdup_vprintf(jsstr, ap);
+ va_end(ap);
+
+ webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
+ g_free(script);
+}
+
+void
+updatewinid(Client *c)
+{
+ snprintf(winid, LENGTH(winid), "%lu", c->xid);
+}
+
+void
+handleplumb(Client *c, const char *uri)
+{
+ Arg a = (Arg)PLUMB(uri);
+ spawn(c, &a);
+}
+
+void
+newwindow(Client *c, const Arg *a, int noembed)
+{
+ int i = 0;
+ char tmp[64];
+ const char *cmd[26], *uri;
+ const Arg arg = { .v = cmd };
+
+ cmd[i++] = argv0;
+ cmd[i++] = "-a";
+ cmd[i++] = cookiepolicies;
+ cmd[i++] = enablescrollbars ? "-B" : "-b";
+ if (cookiefile && g_strcmp0(cookiefile, "")) {
+ cmd[i++] = "-c";
+ cmd[i++] = cookiefile;
+ }
+ cmd[i++] = enablecache ? "-D" : "-d";
+ if (embed && !noembed) {
+ cmd[i++] = "-e";
+ snprintf(tmp, LENGTH(tmp), "%lu", embed);
+ cmd[i++] = tmp;
+ }
+ cmd[i++] = runinfullscreen ? "-F" : "-f";
+ cmd[i++] = allowgeolocation ? "-G" : "-g";
+ cmd[i++] = loadimages ? "-I" : "-i";
+ cmd[i++] = kioskmode ? "-K" : "-k";
+ cmd[i++] = enablestyle ? "-M" : "-m";
+ cmd[i++] = enableinspector ? "-N" : "-n";
+ cmd[i++] = enableplugins ? "-P" : "-p";
+ if (scriptfile && g_strcmp0(scriptfile, "")) {
+ cmd[i++] = "-r";
+ cmd[i++] = scriptfile;
+ }
+ cmd[i++] = enablescripts ? "-S" : "-s";
+ if (stylefile && g_strcmp0(stylefile, "")) {
+ cmd[i++] = "-t";
+ cmd[i++] = stylefile;
+ }
+ if (fulluseragent && g_strcmp0(fulluseragent, "")) {
+ cmd[i++] = "-u";
+ cmd[i++] = fulluseragent;
+ }
+ if (showxid)
+ cmd[i++] = "-x";
+ /* do not keep zoom level */
+ cmd[i++] = "--";
+ if ((uri = a->v))
+ cmd[i++] = uri;
+ cmd[i] = NULL;
+
+ spawn(c, &arg);