]> Pileus Git - ~andy/lamechat/blobdiff - util.c
Strip HTML tags.
[~andy/lamechat] / util.c
diff --git a/util.c b/util.c
index a1ce4b5dc03a08bd7c0c43d43b112f07f4b202bc..b5719244721ecb936743adc4f642d1b15b80d36c 100644 (file)
--- a/util.c
+++ b/util.c
@@ -15,6 +15,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define _GNU_SOURCE
 #define _XOPEN_SOURCE
 #define _XOPEN_SOURCE_EXTENDED
 #define _POSIX_C_SOURCE 200100
@@ -23,6 +24,7 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include <ctype.h>
 #include <errno.h>
 #include <time.h>
 
@@ -102,6 +104,16 @@ char *strcopy(const char *str)
        return strdup(str);
 }
 
+void strset(char **old, const char *str)
+{
+       if (*old)
+               free(*old);
+       if (str)
+               *old = strdup(str);
+       else
+               *old = NULL;
+}
+
 int match(const char *a, const char *b)
 {
        if (a == b)
@@ -111,6 +123,46 @@ int match(const char *a, const char *b)
        return !strcmp(a, b);
 }
 
+int prefix(const char *str, const char *prefix, const char **suffix)
+{
+       while (*str && *prefix) {
+               if (*str != *prefix)
+                       return 0;
+               str++;
+               prefix++;
+       }
+       if (*prefix)
+               return 0;
+       if (*str && *str != ' ')
+               return 0;
+       while (*str && *str == ' ')
+               str++;
+       if (suffix)
+               *suffix = *str ? str : NULL;
+       return 1;
+}
+
+char *despace(char *str)
+{
+       int chr, spaces = 1;
+       char *src = str;
+       char *dst = str;
+       while ((chr = *src++)) {
+               if (isspace(chr))
+                       spaces++;
+               else
+                       spaces=0;
+               if (spaces == 0)
+                       *dst++ = chr;
+               if (spaces == 1)
+                       *dst++ = ' ';
+       }
+       if (dst > str && spaces)
+               dst--;
+       *dst = '\0';
+       return str;
+}
+
 /* Memory functions */
 void *alloc0(int size)
 {
@@ -142,6 +194,14 @@ void release(buf_t *buf)
        buf->max  = 0;
 }
 
+const char *reset(buf_t *buf)
+{
+       if (!buf->len)
+               return NULL;
+       buf->len = 0;
+       return buf->data;
+}
+
 /* Data functions */
 int base64(const void *_in, int ilen, void *_out, int olen)
 {
@@ -227,7 +287,7 @@ err:
 int poll_add(poll_t *poll, int fd, cb_t cb, void *data)
 {
        struct epoll_event ctl = {
-               .events = EPOLLIN | EPOLLOUT | EPOLLERR,
+               .events = EPOLLET | EPOLLIN | EPOLLOUT | EPOLLERR,
                .data.ptr = poll,
        };
        poll->fd   = fd;
@@ -239,7 +299,8 @@ int poll_add(poll_t *poll, int fd, cb_t cb, void *data)
 int poll_ctl(poll_t *poll, int in, int out, int err)
 {
        struct epoll_event ctl = {
-               .events = (in  ? EPOLLIN  : 0)
+               .events = EPOLLET
+                       | (in  ? EPOLLIN  : 0)
                        | (out ? EPOLLOUT : 0)
                        | (err ? EPOLLERR : 0),
                .data.ptr = poll,
@@ -266,7 +327,7 @@ int poll_run(int timeout)
                if (count < 0)
                        continue;
                if (count == 0)
-                       continue;
+                       return running;
                if (!(poll = event.data.ptr))
                        continue;
                if (!(poll->cb))