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
31 #pragma weak view_debug
34 static FILE *debug_fd = NULL;
37 extern void view_debug(const char *fmt, va_list ap);
39 /* Helper functions */
40 static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap)
44 /* Log to standard out */
47 fprintf(output_fd, "%s: ", prefix);
48 vfprintf(output_fd, fmt, tmp);
49 fprintf(output_fd, "\n");
53 /* Log to debug file */
56 fprintf(debug_fd, "%s: ", prefix);
57 vfprintf(debug_fd, fmt, tmp);
58 fprintf(debug_fd, "\n");
62 /* Log to status bar */
72 debug_fd = fopen("/tmp/lackey.log", "w+");
75 /* String functions */
76 void strsub(char *str, char find, char repl)
78 for (char *cur = str; *cur; cur++)
83 char *strcopy(const char *str)
90 int match(const char *a, const char *b)
99 /* Memory functions */
100 void *alloc0(int size)
102 void *data = calloc(1, size);
104 error("memory allocation failed");
109 char *read_file(const char *path, int *len)
111 /* we could use stat, but we'll try to be portable */
112 FILE *fd = fopen(path, "rt+");
116 int block = 512; // read size
117 int size = 512; // buffer size
118 int slen = 0; // string length
119 char *buf = malloc(size);
124 if (slen + block + 1 > size) {
126 buf = realloc(buf, size);
130 slen += fread(&buf[slen], 1, block, fd);
141 /* Debugging functions */
142 void debug(char *fmt, ...)
146 message(NULL, "debug", fmt, ap);
150 void error(char *fmt, ...)
156 message(stderr, "error", fmt, ap);