+ 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);