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/>.
20 #define _XOPEN_SOURCE_EXTENDED
21 #define _POSIX_C_SOURCE 200100
30 #include <sys/epoll.h>
35 #pragma weak view_exit
36 #pragma weak view_debug
40 static FILE *debug_fd = NULL;
41 static int running = 0;
44 extern void view_debug(const char *fmt, va_list ap);
46 /* Helper functions */
47 static void message(FILE *output_fd, const char *prefix, const char *fmt, va_list ap)
52 clock_gettime(CLOCK_REALTIME, &ts);
53 unsigned int sec = ts.tv_sec;
54 unsigned int msec = ts.tv_nsec / 1000000;
56 /* Log to standard out */
59 fprintf(output_fd, "%u.%03u: ", sec, msec);
60 fprintf(output_fd, "%s: ", prefix);
61 vfprintf(output_fd, fmt, tmp);
62 fprintf(output_fd, "\n");
66 /* Log to debug file */
69 fprintf(debug_fd, "%u.%03u: ", sec, msec);
70 fprintf(debug_fd, "%s: ", prefix);
71 vfprintf(debug_fd, fmt, tmp);
72 fprintf(debug_fd, "\n");
76 /* Log to status bar */
86 epoll = epoll_create(1);
87 debug_fd = fopen("/tmp/lamechat.log", "w+");
91 /* String functions */
92 void strsub(char *str, char find, char repl)
94 for (char *cur = str; *cur; cur++)
99 char *strcopy(const char *str)
106 int match(const char *a, const char *b)
112 return !strcmp(a, b);
115 int prefix(const char *str, const char *prefix, const char **suffix)
117 while (*str && *prefix) {
125 if (*str && *str != ' ')
127 while (*str && *str == ' ')
130 *suffix = *str ? str : NULL;
134 /* Memory functions */
135 void *alloc0(int size)
137 void *data = calloc(1, size);
139 error("memory allocation failed");
143 void append(buf_t *buf, const char *data, int len)
145 if (buf->len + len + 1 > buf->max) {
146 buf->max += (((buf->len+len)/4096)+1)*4096;
147 buf->data = realloc(buf->data, buf->max);
149 error("buffer reallocation allocation failed");
152 memcpy(buf->data + buf->len, data, len);
154 ((char*)buf->data)[buf->len] = '\0';
157 void release(buf_t *buf)
166 int base64(const void *_in, int ilen, void *_out, int olen)
168 static const char table[] =
169 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
170 "abcdefghijklmnopqrstuvwxyz"
173 const unsigned char *in = _in;
174 unsigned char *out = _out;
177 int len = ((ilen-1)/3+1)*4;
182 for (int i=0; i<ilen/3; i++) {
183 val = *(in++) << 020;
184 val |= *(in++) << 010;
185 val |= *(in++) << 000;
186 *(out++) = table[(val>>6*3)&077];
187 *(out++) = table[(val>>6*2)&077];
188 *(out++) = table[(val>>6*1)&077];
189 *(out++) = table[(val>>6*0)&077];
196 *out++ = table[(val>>6*3)&077];
197 *out++ = table[(val>>6*2)&077];
198 *out++ = table[(val>>6*1)&077];
203 *out++ = table[(val>>6*3)&077];
204 *out++ = table[(val>>6*2)&077];
214 char *read_file(const char *path, int *len)
216 /* we could use stat, but we'll try to be portable */
217 FILE *fd = fopen(path, "rt+");
221 int block = 512; // read size
222 int size = 512; // buffer size
223 int slen = 0; // string length
224 char *buf = malloc(size);
229 if (slen + block + 1 > size) {
231 buf = realloc(buf, size);
235 slen += fread(&buf[slen], 1, block, fd);
246 /* Polling functions */
247 int poll_add(poll_t *poll, int fd, cb_t cb, void *data)
249 struct epoll_event ctl = {
250 .events = EPOLLIN | EPOLLOUT | EPOLLERR,
256 return epoll_ctl(epoll, EPOLL_CTL_ADD, fd, &ctl);
259 int poll_ctl(poll_t *poll, int in, int out, int err)
261 struct epoll_event ctl = {
262 .events = (in ? EPOLLIN : 0)
263 | (out ? EPOLLOUT : 0)
264 | (err ? EPOLLERR : 0),
267 return epoll_ctl(epoll, EPOLL_CTL_MOD, poll->fd, &ctl);
270 int poll_del(poll_t *poll)
272 return epoll_ctl(epoll, EPOLL_CTL_DEL, poll->fd, NULL);
275 int poll_run(int timeout)
279 struct epoll_event event;
283 count = epoll_wait(epoll, &event, 1, timeout);
290 if (!(poll = event.data.ptr))
294 //debug("poll fd=%d count=%d,%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
296 // event.events & EPOLLIN ? " in" : "",
297 // event.events & EPOLLPRI ? " pri" : "",
298 // event.events & EPOLLOUT ? " out" : "",
299 // event.events & EPOLLRDNORM ? " rdnorm" : "",
300 // event.events & EPOLLRDBAND ? " rdband" : "",
301 // event.events & EPOLLWRNORM ? " wrnorm" : "",
302 // event.events & EPOLLWRBAND ? " wrband" : "",
303 // event.events & EPOLLMSG ? " msg" : "",
304 // event.events & EPOLLERR ? " err" : "",
305 // event.events & EPOLLHUP ? " hup" : "",
306 // event.events & EPOLLRDHUP ? " rdhup" : "",
307 // event.events & EPOLLWAKEUP ? " wakeup" : "",
308 // event.events & EPOLLONESHOT ? " oneshot" : "",
309 // event.events & EPOLLET ? " et" : "");
310 poll->cb(poll->data);
321 /* Debugging functions */
322 void debug(char *fmt, ...)
326 message(NULL, "debug", fmt, ap);
330 void error(char *fmt, ...)
336 message(stderr, "error", fmt, ap);