Allow ~/... and $HOME/... paths for more stuff.
authorBrenden Matthews <brenden@rty.ca>
Mon, 25 May 2009 04:33:47 +0000 (22:33 -0600)
committerBrenden Matthews <brenden@rty.ca>
Mon, 25 May 2009 04:33:47 +0000 (22:33 -0600)
ChangeLog
src/common.c
src/common.h
src/conky.c
src/conky.h
src/imlib2.c
src/llua.c
src/tailhead.c

index 47e2ebc..d9a3468 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2009-05-24
        * Added fancy new 'temperature gradients' feature for graphs, via the -t
        switch at the end of graph arguments.
+       * Allow ~/... and $HOME/... paths for more stuff.
 
 2009-05-19
        * Added inotify support to reload the config when modified automatically
index 679d8ca..2262b98 100644 (file)
@@ -79,9 +79,32 @@ double get_time(void)
        return tv.tv_sec + (tv.tv_usec / 1000000.0);
 }
 
+/* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
+ * DEFAULT_TEXT_BUFFER_SIZE.  It's similar to variable_substitute, except only
+ * cheques for $HOME and ~/ in path */
+void to_real_path(char *dest, const char *source)
+{
+       char tmp[DEFAULT_TEXT_BUFFER_SIZE];
+       if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
+               char *homedir = getenv("HOME");
+               if (homedir) {
+                       snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
+               } else {
+                       ERR("$HOME environment variable doesn't exist");
+                       strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
+               }
+       } else {
+               strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
+       }
+}
+
 FILE *open_file(const char *file, int *reported)
 {
-       FILE *fp = fopen(file, "r");
+       char path[DEFAULT_TEXT_BUFFER_SIZE];
+       FILE *fp = 0;
+
+       to_real_path(path, file);
+       fp = fopen(file, "r");
 
        if (!fp) {
                if (!reported || *reported == 0) {
index b025c92..1c4bd40 100644 (file)
@@ -27,6 +27,10 @@ struct process *get_first_process(void);
 void get_cpu_count(void);
 double get_time(void);
 
+/* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
+ * DEFAULT_TEXT_BUFFER_SIZE.  It's similar to variable_substitute, except only
+ * cheques for $HOME and ~/ in path */
+void to_real_path(char *dest, const char *source);
 FILE *open_file(const char *, int *);
 void variable_substitute(const char *s, char *dest, unsigned int n);
 
index d7d6f43..f9f5e9b 100644 (file)
@@ -405,7 +405,7 @@ static int updatereset;
 int check_contains(char *f, char *s)
 {
        int ret = 0;
-       FILE *where = fopen(f, "r");
+       FILE *where = open_file(f, 0);
 
        if (where) {
                char buf1[256], buf2[256];
@@ -5138,7 +5138,7 @@ static void generate_text_internal(char *p, int p_max_size,
                        OBJ(head)
                                print_head_object(obj, p, p_max_size);
                        OBJ(lines) {
-                               FILE *fp = fopen(obj->data.s,"r");
+                               FILE *fp = open_file(obj->data.s, &obj->a);
 
                                if(fp != NULL) {
 /* FIXME: use something more general (see also tail.c, head.c */
@@ -5162,7 +5162,7 @@ static void generate_text_internal(char *p, int p_max_size,
                        }
 
                        OBJ(words) {
-                               FILE *fp = fopen(obj->data.s,"r");
+                               FILE *fp = open_file(obj->data.s, &obj->a);
 
                                if(fp != NULL) {
                                        char buf[BUFSZ];
@@ -6361,12 +6361,12 @@ static void draw_text(void)
 
 static void draw_stuff(void)
 {
-       if(overwrite_file) {
+       if (overwrite_file) {
                overwrite_fpointer = fopen(overwrite_file, "w");
                if(!overwrite_fpointer)
                        ERR("Can't overwrite '%s' anymore", overwrite_file);
        }
-       if(append_file) {
+       if (append_file) {
                append_fpointer = fopen(append_file, "a");
                if(!append_fpointer)
                        ERR("Can't append '%s' anymore", append_file);
@@ -8436,11 +8436,11 @@ int main(int argc, char **argv)
 
        if (!current_config) {
                /* load default config file */
-               char buf[256];
+               char buf[DEFAULT_TEXT_BUFFER_SIZE];
                FILE *fp;
 
                /* Try to use personal config file first */
-               variable_substitute(CONFIG_FILE, buf, sizeof(buf));
+               to_real_path(buf, CONFIG_FILE);
                if (buf[0] && (fp = fopen(buf, "r"))) {
                        current_config = strndup(buf, max_user_text);
                        fclose(fp);
index fde9ce4..78f6e1f 100644 (file)
@@ -330,4 +330,6 @@ enum x_initialiser_state {
 extern int output_methods;
 extern enum x_initialiser_state x_initialised;
 
+#define DEFAULT_TEXT_BUFFER_SIZE_S "##DEFAULT_TEXT_BUFFER_SIZE"
+
 #endif /* _conky_h_ */
index 3ea39d4..8f2ef5f 100644 (file)
@@ -22,6 +22,7 @@
 #include "imlib2.h"
 #include "config.h"
 #include "logging.h"
+#include "common.h"
 
 #include <Imlib2.h>
 #include <stdio.h>
@@ -91,6 +92,7 @@ void cimlib_add_image(const char *args)
        if (!sscanf(args, "%1024s", cur->name)) {
                ERR("Invalid args for $image.  Format is: '<path to image> (-p x,y) (-s WxH)' (got '%s')", args);
        }
+       to_real_path(cur->name, cur->name);
        // now we check for optional args
        tmp = strstr(args, "-p ");
        if (tmp) {
index 8b941e5..4a0953f 100644 (file)
@@ -42,8 +42,12 @@ void llua_init(void)
 void llua_load(const char *script)
 {
        int error;
+       char path[DEFAULT_TEXT_BUFFER_SIZE];
+
        if(!lua_L) return;
-       error = luaL_dofile(lua_L, script);
+
+       to_real_path(path, script);
+       error = luaL_dofile(lua_L, path);
        if (error) {
                ERR("llua_load: %s", lua_tostring(lua_L, -1));
                lua_pop(lua_L, 1);
index 68f9c53..5fbe254 100644 (file)
@@ -53,7 +53,7 @@ static void *memrchr(const void *buffer, char c, size_t n)
 int init_tailhead_object(enum tailhead_type type,
                struct text_object *obj, const char *arg)
 {
-       char buf[64];
+       char buf[128];
        int n1, n2;
        struct stat st;
        FILE *fp = NULL;
@@ -69,7 +69,7 @@ int init_tailhead_object(enum tailhead_type type,
                return 1;
        }
 
-       numargs = sscanf(arg, "%63s %i %i", buf, &n1, &n2);
+       numargs = sscanf(arg, "%127s %i %i", buf, &n1, &n2);
 
        if (numargs < 2 || numargs > 3) {
                ERR("incorrect number of arguments given to %s object", me);
@@ -87,6 +87,7 @@ int init_tailhead_object(enum tailhead_type type,
        if (type == HEAD) {
                goto NO_FIFO;
        }
+       to_real_path(buf, buf);
        if (stat(buf, &st) == 0) {
                if (S_ISFIFO(st.st_mode)) {
                        fd = open(buf, O_RDONLY | O_NONBLOCK);