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");
50 /* Log to debug file */
53 fprintf(debug_fd, "%s: ", prefix);
54 vfprintf(debug_fd, fmt, tmp);
55 fprintf(debug_fd, "\n");
58 /* Log to status bar */
59 if (&COMPACT && stdscr) {
60 int rev = COMPACT ? A_BOLD : 0;
63 mvhline(LINES-2, 0, ACS_HLINE, COLS);
65 attron(COLOR_PAIR(COLOR_ERROR) | rev);
66 vwprintw(stdscr, fmt, tmp);
67 attroff(COLOR_PAIR(COLOR_ERROR) | rev);
76 debug_fd = fopen("/tmp/lackey.log", "w+");
79 /* String functions */
80 void strsub(char *str, char find, char repl)
82 for (char *cur = str; *cur; cur++)
87 char *strcopy(const char *str)
94 int match(const char *a, const char *b)
100 return !strcmp(a, b);
103 /* Memory functions */
104 void *alloc0(int size)
106 void *data = calloc(1, size);
108 error("memory allocation failed");
113 char *read_file(const char *path, int *len)
115 /* we could use stat, but we'll try to be portable */
116 FILE *fd = fopen(path, "rt+");
120 int block = 512; // read size
121 int size = 512; // buffer size
122 int slen = 0; // string length
123 char *buf = malloc(size);
128 if (slen + block + 1 > size) {
130 buf = realloc(buf, size);
134 slen += fread(&buf[slen], 1, block, fd);
145 /* Debugging functions */
146 void debug(char *fmt, ...)
150 message(NULL, "debug", fmt, ap);
154 void error(char *fmt, ...)
160 message(stderr, "error", fmt, ap);