2 * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #define _XOPEN_SOURCE_EXTENDED
35 static FILE *debug_fd = NULL;
37 /* Helper functions */
38 static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap)
42 /* Log to standard out */
45 fprintf(output_fd, "%s: ", prefix);
46 vfprintf(output_fd, fmt, tmp);
47 fprintf(output_fd, "\n");
51 /* Log to debug file */
54 fprintf(debug_fd, "%s: ", prefix);
55 vfprintf(debug_fd, fmt, tmp);
56 fprintf(debug_fd, "\n");
60 /* Log to status bar */
61 if (&COMPACT && stdscr) {
62 int rev = COMPACT ? A_BOLD : 0;
65 mvhline(LINES-2, 0, ACS_HLINE, COLS);
67 attron(COLOR_PAIR(COLOR_ERROR) | rev);
68 vwprintw(stdscr, fmt, tmp);
69 attroff(COLOR_PAIR(COLOR_ERROR) | rev);
78 debug_fd = fopen("/tmp/lackey.log", "w+");
81 /* String functions */
82 void strsub(char *str, char find, char repl)
84 for (char *cur = str; *cur; cur++)
89 char *strcopy(const char *str)
96 int match(const char *a, const char *b)
102 return !strcmp(a, b);
105 /* Memory functions */
106 void *alloc0(int size)
108 void *data = calloc(1, size);
110 error("memory allocation failed");
115 char *read_file(const char *path, int *len)
117 /* we could use stat, but we'll try to be portable */
118 FILE *fd = fopen(path, "rt+");
122 int block = 512; // read size
123 int size = 512; // buffer size
124 int slen = 0; // string length
125 char *buf = malloc(size);
130 if (slen + block + 1 > size) {
132 buf = realloc(buf, size);
136 slen += fread(&buf[slen], 1, block, fd);
147 /* Debugging functions */
148 void debug(char *fmt, ...)
152 message(NULL, "debug", fmt, ap);
156 void error(char *fmt, ...)
162 message(stderr, "error", fmt, ap);