X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Futil.c;h=420dbd56dd99b7279bcd0f21639859d2981c30e4;hb=335d816efb8dd7c003e285a19e9434c4119ec13f;hp=582b65deff8b70adc0c2f06dd248ac7fd92f6250;hpb=daded0f3fb930e7758e1009683c0cdb46eada3f6;p=lackey diff --git a/src/util.c b/src/util.c index 582b65d..420dbd5 100644 --- a/src/util.c +++ b/src/util.c @@ -23,10 +23,50 @@ #include "date.h" #include "cal.h" #include "view.h" +#include "util.h" + +/* For testing */ +#pragma weak COMPACT /* Static data */ static FILE *debug_fd = NULL; +/* Helper functions */ +static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap) +{ + va_list tmp; + + /* Log to standard out */ + if (output_fd) { + va_copy(tmp, ap); + fprintf(output_fd, "%s: ", prefix); + vfprintf(output_fd, fmt, tmp); + fprintf(output_fd, "\n"); + } + + /* Log to debug file */ + if (debug_fd) { + va_copy(tmp, ap); + fprintf(debug_fd, "%s: ", prefix); + vfprintf(debug_fd, fmt, tmp); + fprintf(debug_fd, "\n"); + } + + /* Log to status bar */ + if (&COMPACT && stdscr) { + int rev = COMPACT ? A_BOLD : 0; + va_copy(tmp, ap); + if (!COMPACT) + mvhline(LINES-2, 0, ACS_HLINE, COLS); + move(LINES-1, 0); + attron(COLOR_PAIR(COLOR_ERROR) | rev); + vwprintw(stdscr, fmt, tmp); + attroff(COLOR_PAIR(COLOR_ERROR) | rev); + if (!COMPACT) + clrtoeol(); + } +} + /* Initialize */ void util_init(void) { @@ -41,30 +81,35 @@ void strsub(char *str, char find, char repl) *cur = repl; } +/* Memory functions */ +void *alloc0(int size) +{ + void *data = calloc(1, size); + if (!data) + error("memory allocation failed"); + return data; +} + /* Debugging functions */ -int debug(char *fmt, ...) +void debug(char *fmt, ...) { - int rval; va_list ap; + va_start(ap, fmt); + message(NULL, "debug", fmt, ap); + va_end(ap); +} - /* Log to debug file */ - if (debug_fd) { - va_start(ap, fmt); - vfprintf(debug_fd, "debug: ", ap); - rval = vfprintf(debug_fd, fmt, ap); - } - - /* Log to status bar */ +void error(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fflush(stdout); + fflush(stderr); + message(stderr, "error", fmt, ap); + va_end(ap); if (stdscr) { - va_start(ap, fmt); - mvhline(LINES-2, 0, ACS_HLINE, COLS); - move(LINES-1, 0); - attron(COLOR_PAIR(COLOR_ERROR)); - vwprintw(stdscr, fmt, ap); - attroff(COLOR_PAIR(COLOR_ERROR)); - clrtoeol(); + getch(); + endwin(); } - - va_end(ap); - return rval; + exit(-1); }