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_exit
32 #pragma weak view_debug
35 static FILE *debug_fd = NULL;
38 extern void view_debug(const char *fmt, va_list ap);
40 /* Helper functions */
41 static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap)
45 /* Log to standard out */
48 fprintf(output_fd, "%s: ", prefix);
49 vfprintf(output_fd, fmt, tmp);
50 fprintf(output_fd, "\n");
54 /* Log to debug file */
57 fprintf(debug_fd, "%s: ", prefix);
58 vfprintf(debug_fd, fmt, tmp);
59 fprintf(debug_fd, "\n");
63 /* Log to status bar */
73 debug_fd = fopen("/tmp/lackey.log", "w+");
76 /* String functions */
77 void strsub(char *str, char find, char repl)
79 for (char *cur = str; *cur; cur++)
84 char *strcopy(const char *str)
91 int match(const char *a, const char *b)
100 /* Memory functions */
101 void *alloc0(int size)
103 void *data = calloc(1, size);
105 error("memory allocation failed");
110 char *read_file(const char *path, int *len)
112 /* we could use stat, but we'll try to be portable */
113 FILE *fd = fopen(path, "rt+");
117 int block = 512; // read size
118 int size = 512; // buffer size
119 int slen = 0; // string length
120 char *buf = malloc(size);
125 if (slen + block + 1 > size) {
127 buf = realloc(buf, size);
131 slen += fread(&buf[slen], 1, block, fd);
142 /* Debugging functions */
143 void debug(char *fmt, ...)
147 message(NULL, "debug", fmt, ap);
151 void error(char *fmt, ...)
157 message(stderr, "error", fmt, ap);