aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Brendan Taylor <whateley@gmail.com>2010-11-13 10:29:01 -0700
committerGravatar Brendan Taylor <whateley@gmail.com>2010-11-13 10:29:01 -0700
commit351428bd782a625f4534fac417b75d582c1d206b (patch)
tree696bcfe1aa1b677ec149038c25ee082fc7c63675 /src
parentf6beec78c65c51938e1d01627564a6d1f8021673 (diff)
parent94a7ef21e4036407fa800567766e20957e8d55ae (diff)
Merge branch 'dev/soup-cookied'
Conflicts: Makefile
Diffstat (limited to 'src')
-rw-r--r--src/callbacks.c2
-rw-r--r--src/util.c110
-rw-r--r--src/util.h18
-rw-r--r--src/uzbl-core.c179
-rw-r--r--src/uzbl-core.h21
5 files changed, 157 insertions, 173 deletions
diff --git a/src/callbacks.c b/src/callbacks.c
index 0cfa7f4..0cf713b 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -6,7 +6,7 @@
#include "uzbl-core.h"
#include "callbacks.h"
#include "events.h"
-
+#include "util.h"
void
set_proxy_url() {
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..c9c728e
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,110 @@
+#define _POSIX_SOURCE
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "util.h"
+
+const XDG_Var XDG[] =
+{
+ { "XDG_CONFIG_HOME", "~/.config" },
+ { "XDG_DATA_HOME", "~/.local/share" },
+ { "XDG_CACHE_HOME", "~/.cache" },
+ { "XDG_CONFIG_DIRS", "/etc/xdg" },
+ { "XDG_DATA_DIRS", "/usr/local/share/:/usr/share/" },
+};
+
+/*@null@*/ gchar*
+get_xdg_var (XDG_Var xdg) {
+ const gchar* actual_value = getenv (xdg.environmental);
+ const gchar* home = getenv ("HOME");
+ gchar* return_value;
+
+ if (! actual_value || strcmp (actual_value, "") == 0) {
+ if (xdg.default_value) {
+ return_value = str_replace ("~", home, xdg.default_value);
+ } else {
+ return_value = NULL;
+ }
+ } else {
+ return_value = str_replace("~", home, actual_value);
+ }
+
+ return return_value;
+}
+
+/*@null@*/ gchar*
+find_xdg_file (int xdg_type, const char* filename) {
+ /* xdg_type = 0 => config
+ xdg_type = 1 => data
+ xdg_type = 2 => cache*/
+
+ gchar* xdgv = get_xdg_var (XDG[xdg_type]);
+ gchar* temporary_file = g_strconcat (xdgv, filename, NULL);
+ g_free (xdgv);
+
+ gchar* temporary_string;
+ char* saveptr;
+ char* buf;
+
+ if (! file_exists (temporary_file) && xdg_type != 2) {
+ buf = get_xdg_var (XDG[3 + xdg_type]);
+ temporary_string = (char *) strtok_r (buf, ":", &saveptr);
+ g_free(buf);
+
+ while ((temporary_string = (char * ) strtok_r (NULL, ":", &saveptr)) && ! file_exists (temporary_file)) {
+ g_free (temporary_file);
+ temporary_file = g_strconcat (temporary_string, filename, NULL);
+ }
+ }
+
+ //g_free (temporary_string); - segfaults.
+
+ if (file_exists (temporary_file)) {
+ return temporary_file;
+ } else {
+ g_free(temporary_file);
+ return NULL;
+ }
+}
+
+gboolean
+file_exists (const char * filename) {
+ return (access(filename, F_OK) == 0);
+}
+
+char *
+str_replace (const char* search, const char* replace, const char* string) {
+ gchar **buf;
+ char *ret;
+
+ if(!string)
+ return NULL;
+
+ buf = g_strsplit (string, search, -1);
+ ret = g_strjoinv (replace, buf);
+ g_strfreev(buf);
+
+ return ret;
+}
+
+gboolean
+for_each_line_in_file(const gchar *path, void (*callback)(const gchar *l, void *c), void *user_data) {
+ gchar *line = NULL;
+ gsize len;
+
+ GIOChannel *chan = g_io_channel_new_file(path, "r", NULL);
+
+ if (chan) {
+ while (g_io_channel_read_line(chan, &line, &len, NULL, NULL) == G_IO_STATUS_NORMAL) {
+ callback(line, user_data);
+ g_free(line);
+ }
+ g_io_channel_unref (chan);
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..f03f13e
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,18 @@
+#include <glib.h>
+
+typedef struct {
+ gchar* environmental;
+ gchar* default_value;
+} XDG_Var;
+
+gchar* get_xdg_var (XDG_Var xdg);
+
+gchar* find_xdg_file (int xdg_type, const char* filename);
+
+gboolean file_exists(const char* filename);
+
+char *
+str_replace (const char* search, const char* replace, const char* string);
+
+gboolean
+for_each_line_in_file(const gchar *path, void (*callback)(const gchar *l, void *c), void *user_data);
diff --git a/src/uzbl-core.c b/src/uzbl-core.c
index 6b120d0..777c9b4 100644
--- a/src/uzbl-core.c
+++ b/src/uzbl-core.c
@@ -34,6 +34,7 @@
#include "events.h"
#include "inspector.h"
#include "config.h"
+#include "util.h"
UzblCore uzbl;
@@ -62,15 +63,6 @@ GOptionEntry entries[] =
{ NULL, 0, 0, 0, NULL, NULL, NULL }
};
-XDG_Var XDG[] =
-{
- { "XDG_CONFIG_HOME", "~/.config" },
- { "XDG_DATA_HOME", "~/.local/share" },
- { "XDG_CACHE_HOME", "~/.cache" },
- { "XDG_CONFIG_DIRS", "/etc/xdg" },
- { "XDG_DATA_DIRS", "/usr/local/share/:/usr/share/" },
-};
-
/* abbreviations to help keep the table's width humane */
#define PTR_V_STR(var, d, fun) { .ptr.s = &(var), .type = TYPE_STR, .dump = d, .writeable = 1, .func = fun }
#define PTR_V_INT(var, d, fun) { .ptr.i = (int*)&(var), .type = TYPE_INT, .dump = d, .writeable = 1, .func = fun }
@@ -372,48 +364,6 @@ strfree(gchar *str) {
gchar*
argv_idx(const GArray *a, const guint idx) { return g_array_index(a, gchar*, idx); }
-char *
-str_replace (const char* search, const char* replace, const char* string) {
- gchar **buf;
- char *ret;
-
- if(!string)
- return NULL;
-
- buf = g_strsplit (string, search, -1);
- ret = g_strjoinv (replace, buf);
- g_strfreev(buf);
-
- return ret;
-}
-
-GArray*
-read_file_by_line (const gchar *path) {
- GIOChannel *chan = NULL;
- gchar *readbuf = NULL;
- gsize len;
- GArray *lines = g_array_new(TRUE, FALSE, sizeof(gchar*));
- int i = 0;
-
- chan = g_io_channel_new_file(path, "r", NULL);
- if (chan) {
- while (g_io_channel_read_line(chan, &readbuf, &len, NULL, NULL) == G_IO_STATUS_NORMAL) {
- const gchar* val = g_strdup (readbuf);
- g_array_append_val (lines, val);
- g_free (readbuf);
- i ++;
- }
-
- g_io_channel_unref (chan);
- } else {
- gchar *tmp = g_strdup_printf("File %s can not be read.", path);
- send_event(COMMAND_ERROR, tmp, NULL);
- g_free(tmp);
- }
-
- return lines;
-}
-
/* search a PATH style string for an existing file+path combination */
gchar*
find_existing_file(gchar* path_list) {
@@ -735,11 +685,6 @@ builtins() {
/* -- CORE FUNCTIONS -- */
-bool
-file_exists (const char * filename) {
- return (access(filename, F_OK) == 0);
-}
-
void
set_var(WebKitWebView *page, GArray *argv, GString *result) {
(void) page; (void) result;
@@ -970,28 +915,30 @@ hardcopy(WebKitWebView *page, GArray *argv, GString *result) {
webkit_web_frame_print(webkit_web_view_get_main_frame(page));
}
+/* just a wrapper so parse_cmd_line can be used with for_each_line_in_file */
+static void
+parse_cmd_line_cb(const char *line, void *user_data) {
+ (void) user_data;
+ parse_cmd_line(line, NULL);
+}
+
void
include(WebKitWebView *page, GArray *argv, GString *result) {
(void) page;
(void) result;
gchar *pe = NULL,
- *path = NULL,
- *line;
- int i=0;
+ *path = NULL;
if(!argv_idx(argv, 0))
return;
pe = parseenv(argv_idx(argv, 0));
if((path = find_existing_file(pe))) {
- GArray* lines = read_file_by_line(path);
-
- while ((line = g_array_index(lines, gchar*, i))) {
- parse_cmd_line (line, NULL);
- i++;
- g_free (line);
+ if(!for_each_line_in_file(path, parse_cmd_line_cb, NULL)) {
+ gchar *tmp = g_strdup_printf("File %s can not be read.", path);
+ send_event(COMMAND_ERROR, tmp, NULL);
+ g_free(tmp);
}
- g_array_free (lines, TRUE);
send_event(FILE_INCLUDED, path, NULL);
g_free(path);
@@ -1163,32 +1110,23 @@ run_external_js (WebKitWebView * web_view, GArray *argv, GString *result) {
if (argv_idx(argv, 0) &&
((path = find_existing_file(argv_idx(argv, 0)))) ) {
- GArray* lines = read_file_by_line (path);
- gchar* js = NULL;
- int i = 0;
- gchar* line;
-
- while ((line = g_array_index(lines, gchar*, i))) {
- if (js == NULL) {
- js = g_strdup (line);
- } else {
- gchar* newjs = g_strconcat (js, line, NULL);
- js = newjs;
- }
- i ++;
- g_free (line);
+ gchar *file_contents = NULL;
+
+ GIOChannel *chan = g_io_channel_new_file(path, "r", NULL);
+ if (chan) {
+ gsize len;
+ g_io_channel_read_to_end(chan, &file_contents, &len, NULL);
+ g_io_channel_unref (chan);
}
if (uzbl.state.verbose)
printf ("External JavaScript file %s loaded\n", argv_idx(argv, 0));
- gchar* newjs = str_replace("%s", argv_idx (argv, 1)?argv_idx (argv, 1):"", js);
- g_free (js);
- js = newjs;
+ gchar *js = str_replace("%s", argv_idx (argv, 1) ? argv_idx (argv, 1) : "", file_contents);
+ g_free (file_contents);
eval_js (web_view, js, result, path);
g_free (js);
- g_array_free (lines, TRUE);
g_free(path);
}
}
@@ -1639,7 +1577,6 @@ parse_cmd_line(const char *ctl_line, GString *result) {
g_free(ctlstrip);
}
-
/*@null@*/ gchar*
build_stream_name(int type, const gchar* dir) {
State *s = &uzbl.state;
@@ -2138,59 +2075,6 @@ run_handler (const gchar *act, const gchar *args) {
g_strfreev(parts);
}
-/*@null@*/ gchar*
-get_xdg_var (XDG_Var xdg) {
- const gchar* actual_value = getenv (xdg.environmental);
- const gchar* home = getenv ("HOME");
- gchar* return_value;
-
- if (! actual_value || strcmp (actual_value, "") == 0) {
- if (xdg.default_value) {
- return_value = str_replace ("~", home, xdg.default_value);
- } else {
- return_value = NULL;
- }
- } else {
- return_value = str_replace("~", home, actual_value);
- }
-
- return return_value;
-}
-
-/*@null@*/ gchar*
-find_xdg_file (int xdg_type, const char* filename) {
- /* xdg_type = 0 => config
- xdg_type = 1 => data
- xdg_type = 2 => cache*/
-
- gchar* xdgv = get_xdg_var (XDG[xdg_type]);
- gchar* temporary_file = g_strconcat (xdgv, filename, NULL);
- g_free (xdgv);
-
- gchar* temporary_string;
- char* saveptr;
- char* buf;
-
- if (! file_exists (temporary_file) && xdg_type != 2) {
- buf = get_xdg_var (XDG[3 + xdg_type]);
- temporary_string = (char *) strtok_r (buf, ":", &saveptr);
- g_free(buf);
-
- while ((temporary_string = (char * ) strtok_r (NULL, ":", &saveptr)) && ! file_exists (temporary_file)) {
- g_free (temporary_file);
- temporary_file = g_strconcat (temporary_string, filename, NULL);
- }
- }
-
- //g_free (temporary_string); - segfaults.
-
- if (file_exists (temporary_file)) {
- return temporary_file;
- } else {
- g_free(temporary_file);
- return NULL;
- }
-}
void
settings_init () {
State *s = &uzbl.state;
@@ -2211,20 +2095,13 @@ settings_init () {
}
if (s->config_file) {
- GArray* lines = read_file_by_line (s->config_file);
- int i = 0;
- gchar* line;
-
- while ((line = g_array_index(lines, gchar*, i))) {
- parse_cmd_line (line, NULL);
- i ++;
- g_free (line);
+ if(!for_each_line_in_file(s->config_file, parse_cmd_line_cb, NULL)) {
+ gchar *tmp = g_strdup_printf("File %s can not be read.", s->config_file);
+ send_event(COMMAND_ERROR, tmp, NULL);
+ g_free(tmp);
}
- g_array_free (lines, TRUE);
- } else {
- if (uzbl.state.verbose)
- printf ("No configuration file loaded.\n");
- }
+ } else if (uzbl.state.verbose)
+ printf ("No configuration file loaded.\n");
if(s->connect_socket_names)
init_connect_socket();
diff --git a/src/uzbl-core.h b/src/uzbl-core.h
index 19a5887..a572d72 100644
--- a/src/uzbl-core.h
+++ b/src/uzbl-core.h
@@ -206,12 +206,6 @@ extern UzblCore uzbl;
typedef void sigfunc(int);
-/* XDG Stuff */
-typedef struct {
- gchar* environmental;
- gchar* default_value;
-} XDG_Var;
-
/* uzbl variables */
enum ptr_type {TYPE_INT, TYPE_STR, TYPE_FLOAT};
typedef struct {
@@ -230,15 +224,9 @@ typedef struct {
char *
itos(int val);
-char *
-str_replace (const char* search, const char* replace, const char* string);
-
gchar*
strfree(gchar *str);
-GArray*
-read_file_by_line (const gchar *path);
-
gchar*
parseenv (gchar* string);
@@ -263,9 +251,6 @@ print(WebKitWebView *page, GArray *argv, GString *result);
void
commands_hash(void);
-bool
-file_exists (const char * filename);
-
void
load_uri (WebKitWebView * web_view, GArray *argv, GString *result);
@@ -351,12 +336,6 @@ create_plug ();
void
run_handler (const gchar *act, const gchar *args);
-/*@null@*/ gchar*
-get_xdg_var (XDG_Var xdg);
-
-/*@null@*/ gchar*
-find_xdg_file (int xdg_type, const char* filename);
-
void
settings_init ();