aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-04-20 21:26:30 -0400
committerJesse Luehrs <doy@tozt.net>2014-04-20 21:26:30 -0400
commit555797e76f787fbc04466a3281d5bafb840fe6a7 (patch)
tree7aa03bb663bcfd39fa1a75e23f4e905fa39cf5c3
parentfb34f00c2fdc35c674f9c0ba0c9db6028605a6e9 (diff)
downloadrunes-555797e76f787fbc04466a3281d5bafb840fe6a7.tar.gz
runes-555797e76f787fbc04466a3281d5bafb840fe6a7.zip
start adding configuration
-rw-r--r--Makefile2
-rw-r--r--config.c120
-rw-r--r--config.h6
-rw-r--r--display.c1
-rw-r--r--runes.h1
-rw-r--r--term.c5
6 files changed, 129 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index bdf6871..141e7dd 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
OUT = runes
-OBJ = runes.o display.o term.o parser.o window-xlib.o pty-unix.o
+OBJ = runes.o display.o term.o parser.o config.o window-xlib.o pty-unix.o
LIBS = cairo cairo-xlib libuv pangocairo
CFLAGS ?= -g -Wall -Wextra -Werror
LDFLAGS ?= -g -Wall -Wextra -Werror
diff --git a/config.c b/config.c
new file mode 100644
index 0000000..1664e4b
--- /dev/null
+++ b/config.c
@@ -0,0 +1,120 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "runes.h"
+
+static void runes_config_set_defaults(RunesTerm *t);
+static FILE *runes_config_get_config_file();
+static void runes_config_process_config_file(RunesTerm *t, FILE *config_file);
+static void runes_config_set(RunesTerm *t, char *key, char *value);
+
+void runes_config_init(RunesTerm *t, int argc, char *argv[])
+{
+ UNUSED(argc);
+ UNUSED(argv);
+
+ memset((void *)t, 0, sizeof(*t));
+
+ runes_config_set_defaults(t);
+ runes_config_process_config_file(t, runes_config_get_config_file());
+}
+
+static void runes_config_set_defaults(RunesTerm *t)
+{
+ t->font_name = "monospace 10";
+}
+
+static FILE *runes_config_get_config_file()
+{
+ char *home, *config_dir, *path;
+ size_t home_len, config_dir_len;
+ FILE *file;
+
+ home = getenv("HOME");
+ home_len = strlen(home);
+
+ config_dir = getenv("XDG_CONFIG_HOME");
+ if (config_dir) {
+ config_dir = strdup(config_dir);
+ }
+ else {
+ config_dir = malloc(home_len + sizeof("/.config") + 1);
+ strcpy(config_dir, home);
+ strcpy(config_dir + home_len, "/.config");
+ }
+ config_dir_len = strlen(config_dir);
+
+ path = malloc(config_dir_len + sizeof("/runes/runes.conf") + 1);
+ strcpy(path, config_dir);
+ strcpy(path + config_dir_len, "/runes/runes.conf");
+ free(config_dir);
+
+ if ((file = fopen(path, "r"))) {
+ free(path);
+ return file;
+ }
+
+ free(path);
+ path = malloc(home_len + sizeof("/.runesrc") + 1);
+ strcpy(path, home);
+ strcpy(path + home_len, "/.runesrc");
+
+ if ((file = fopen(path, "r"))) {
+ free(path);
+ return file;
+ }
+
+ free(path);
+
+ if ((file = fopen("/etc/runesrc", "r"))) {
+ return file;
+ }
+
+ return NULL;
+}
+
+static void runes_config_process_config_file(RunesTerm *t, FILE *config_file)
+{
+ char line[1024];
+
+ if (!config_file) {
+ return;
+ }
+
+ while (fgets(line, 1024, config_file)) {
+ char *kbegin, *kend, *vbegin, *vend;
+ size_t len;
+
+ len = strlen(line);
+ if (line[len - 1] == '\n') {
+ line[len - 1] = '\0';
+ len--;
+ }
+
+ kbegin = line + strspn(line, " \t");
+ kend = kbegin + strcspn(kbegin, " \t=");
+ vbegin = kend + strspn(kend, " \t");
+ if (*vbegin != '=') {
+ fprintf(stderr, "couldn't parse line: '%s'\n", line);
+ }
+ vbegin++;
+ vbegin = vbegin + strspn(vbegin, " \t");
+ vend = line + len;
+
+ *kend = '\0';
+ *vend = '\0';
+
+ runes_config_set(t, kbegin, vbegin);
+ }
+}
+
+static void runes_config_set(RunesTerm *t, char *key, char *val)
+{
+ if (!strcmp(key, "font")) {
+ t->font_name = strdup(val);
+ }
+ else {
+ fprintf(stderr, "unknown option: '%s'\n", key);
+ }
+}
diff --git a/config.h b/config.h
new file mode 100644
index 0000000..980cbab
--- /dev/null
+++ b/config.h
@@ -0,0 +1,6 @@
+#ifndef _RUNES_CONFIG_H
+#define _RUNES_CONFIG_H
+
+void runes_config_init(RunesTerm *t, int argc, char *argv[]);
+
+#endif
diff --git a/display.c b/display.c
index 0fdf7d8..98f09d7 100644
--- a/display.c
+++ b/display.c
@@ -15,7 +15,6 @@ static void runes_display_scroll_down(RunesTerm *t, int rows);
void runes_display_init(RunesTerm *t)
{
- t->font_name = "Fixed 10.5";
runes_display_recalculate_font_metrics(t);
t->fgdefault = cairo_pattern_create_rgb(0.827, 0.827, 0.827);
diff --git a/runes.h b/runes.h
index 7287fd8..8c2861b 100644
--- a/runes.h
+++ b/runes.h
@@ -28,6 +28,7 @@ struct runes_loop_data {
#include "term.h"
#include "display.h"
#include "parser.h"
+#include "config.h"
#define UNUSED(x) ((void)x)
diff --git a/term.c b/term.c
index 6a18142..fe21752 100644
--- a/term.c
+++ b/term.c
@@ -1,11 +1,8 @@
-#include <stdlib.h>
-#include <string.h>
-
#include "runes.h"
void runes_term_init(RunesTerm *t, int argc, char *argv[])
{
- memset((void *)t, 0, sizeof(*t));
+ runes_config_init(t, argc, argv);
/* doing most of the pty initialization right at the beginning, because
* libuv will set up a bunch of state (including potentially things like