X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Futil.c;h=131e9dea2e6261ccf4823030e4bde638a2bcff1d;hb=6e84acb58056ce793a08041d202bb96861874025;hp=86bc3730701a21d2f5d1d4abc96a6929db769325;hpb=19a02d772c5753208c667b37a57527796ac6dc75;p=lackey diff --git a/src/util.c b/src/util.c index 86bc373..131e9de 100644 --- a/src/util.c +++ b/src/util.c @@ -1,17 +1,16 @@ /* -#include * Copyright (C) 2012 Andy Spencer - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -22,42 +21,89 @@ #include #include "date.h" -#include "event.h" -#include "screen.h" +#include "cal.h" +#include "view.h" +#include "util.h" /* Static data */ static FILE *debug_fd = NULL; -/* Initialize */ -void util_init(void) +/* Helper functions */ +static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap) { - debug_fd = fopen("/tmp/lackey.log", "w+"); -} + va_list tmp; -/* Debugging functions */ -int debug(char *fmt, ...) -{ - int rval; - va_list ap; + /* 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_start(ap, fmt); - vfprintf(debug_fd, "debug: ", ap); - rval = vfprintf(debug_fd, fmt, ap); + va_copy(tmp, ap); + fprintf(debug_fd, "%s: ", prefix); + vfprintf(debug_fd, fmt, tmp); + fprintf(debug_fd, "\n"); } /* Log to status bar */ if (stdscr) { - va_start(ap, fmt); + va_copy(tmp, ap); mvhline(LINES-2, 0, ACS_HLINE, COLS); move(LINES-1, 0); attron(COLOR_PAIR(COLOR_ERROR)); - vwprintw(stdscr, fmt, ap); + vwprintw(stdscr, fmt, tmp); attroff(COLOR_PAIR(COLOR_ERROR)); clrtoeol(); } +} + +/* Initialize */ +void util_init(void) +{ + debug_fd = fopen("/tmp/lackey.log", "w+"); +} +/* String functions */ +void strsub(char *str, char find, char repl) +{ + for (char *cur = str; *cur; cur++) + if (*cur == find) + *cur = repl; +} + +/* Memory functions */ +void *alloc0(int size) +{ + void *data = calloc(1, size); + if (!data) + error("memory allocation failed"); + return data; +} + +/* Debugging functions */ +void debug(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + message(NULL, "debug", fmt, ap); + va_end(ap); +} + +void error(char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + fflush(stdout); + fflush(stderr); + message(stderr, "error", fmt, ap); va_end(ap); - return rval; + if (stdscr) { + getch(); + endwin(); + } + exit(-1); }