diff --git a/Makefile b/Makefile index d67683c..8e53c40 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,9 @@ all : $(PROGRAMS) install : $(PROGRAMS) cp $^ $(HOME)/bin/ -dlight : dlight.o env.o http.o rss.o lockfile.o filter.o cconf.o dlhist.o -dlight-compile : compile.o env.o lockfile.o filter.o cconf.o -dlight-read-config : read-config.o env.o cconf.o +dlight : dlight.o env.o http.o rss.o lockfile.o filter.o cconf.o dlhist.o error.o +dlight-compile : compile.o env.o lockfile.o filter.o cconf.o error.o +dlight-read-config : read-config.o env.o cconf.o error.o dlight-% : %.o $(CC) $(LDFLAGS) -o $@ $^ diff --git a/compile.c b/compile.c index f1ac3d9..077b26a 100644 --- a/compile.c +++ b/compile.c @@ -27,12 +27,11 @@ #include #include #include "env.h" +#include "error.h" #include "cconf.h" #include "lockfile.h" #include "filter.h" -#define error(...) fprintf(stderr, "error: " __VA_ARGS__) - #define isalias(x) (isalnum(x) || (x) == '-') #define MAXNAME 1024 @@ -252,8 +251,7 @@ static int parse_target(struct target *target) { if (!alias) return -1; if (!alias[0] && !dest_table_nr) { - error("No destination found for target '%s'\n", src); - return -1; + return error("No destination found for target '%s'\n", src); } target->src = strdup(src); diff --git a/dlight.c b/dlight.c index 790abbd..f84536f 100644 --- a/dlight.c +++ b/dlight.c @@ -3,14 +3,13 @@ #include #include #include "env.h" +#include "error.h" #include "cconf.h" #include "dlhist.h" #include "filter.h" #include "http.h" #include "rss.h" -#define error(...) fprintf(stderr, "error: " __VA_ARGS__) - static void process_items(rss_t rss, struct target *t) { int i; @@ -29,8 +28,7 @@ static void process_items(rss_t rss, struct target *t) { if (http_download_file(item.link, filter->dest) < 0 && errno != EEXIST) { - printf("download failed: %s\n", - strerror(errno)); + error("download failed: %s\n", strerror(errno)); continue; } diff --git a/filter.c b/filter.c index 35f3427..bf15326 100644 --- a/filter.c +++ b/filter.c @@ -21,17 +21,18 @@ #include #include #include +#include "error.h" #include "filter.h" static inline pcre* compile(const char *pattern) { - const char *error; + const char *err; int eoffset; pcre *regex; - regex = pcre_compile(pattern, 0, &error, &eoffset, NULL); + regex = pcre_compile(pattern, 0, &err, &eoffset, NULL); if (!regex) { - fprintf(stderr, "Error compiling expression\n"); + error("Error compiling expression\n"); return NULL; } @@ -48,14 +49,14 @@ static inline int match(pcre *pcre, const char *subject) { int filter_check_syntax(const char *pattern) { - const char *error; + const char *err; int eoffset; pcre *regex; - regex = pcre_compile(pattern, 0, &error, &eoffset, NULL); + regex = pcre_compile(pattern, 0, &err, &eoffset, NULL); if (!regex) { - fprintf(stderr, "filter: error in expression '%s': %s\n", - pattern, error); + error("filter: error in expression '%s': %s\n", + pattern, err); return 0; } return 1; diff --git a/http.c b/http.c index fcbb885..dc5ecf4 100644 --- a/http.c +++ b/http.c @@ -25,6 +25,7 @@ #include #include #include +#include "error.h" #include "http.h" static char* strnstrr(const char *str, const char *needle, size_t size) { @@ -109,7 +110,7 @@ static size_t write_cb(void *src, size_t smemb, size_t nmemb, void *data) { dest->block = realloc(dest->block, dest->len + size); if (dest->block == NULL) { - printf("out of memory\n"); + error("out of memory\n"); return 0; } memcpy(dest->block + dest->len, src, size); @@ -148,7 +149,7 @@ struct http_data* http_fetch_page(const char *url) { res = curl_easy_perform(handle); if (res != CURLE_OK) { - printf("curl: (%s) %s\n", url, curl_easy_strerror(res)); + error("curl: (%s) %s\n", url, curl_easy_strerror(res)); goto error; } @@ -184,7 +185,7 @@ int http_download_file(const char *url, const char *dir) { res = curl_easy_perform(handle); if (res != CURLE_OK) { - printf("curl: (%s) %s\n", url, curl_easy_strerror(res)); + error("curl: (%s) %s\n", url, curl_easy_strerror(res)); goto error; } diff --git a/lockfile.c b/lockfile.c index 09cda4c..a7b6654 100644 --- a/lockfile.c +++ b/lockfile.c @@ -26,7 +26,7 @@ #include #include #include -#include +#include "error.h" #include "lockfile.h" #define locked(x) ((x)->fd >= 0) @@ -95,13 +95,9 @@ int hold_lock(struct lockfile *lock, const char *filename, int force) { lock->fd = open(lock->name, mask, 0600); if (lock->fd < 0) { - if (errno == EEXIST) { - fprintf(stderr, "'%s' is locked\n", lock->name); - } else { - fprintf(stderr, "unable to create lockfile '%s'", - lock->name); - } - return -1; + return error(errno == EEXIST ? + "'%s' is locked\n" : "unable to create lockfile '%s'", + lock->name); } lock->next = active_locks; active_locks = lock; diff --git a/read-config.c b/read-config.c index 16ee788..3a9b16c 100644 --- a/read-config.c +++ b/read-config.c @@ -22,8 +22,9 @@ #include #include "cconf.h" #include "env.h" +#include "error.h" -static char *usage = "dlight-read-config [ | -h ]\n"; +static char *usagestr = "dlight-read-config [ | -h ]"; int main(int argc, char **argv) { @@ -33,8 +34,7 @@ int main(int argc, char **argv) { if (argc > 1) { if (!strcmp(argv[1], "-h")) { - fprintf(stderr, usage); - return 1; + usage(usagestr); } strncpy(file, argv[1], sizeof(file)); } else {