Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <ctype.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
26 #include "telescope.h"
28 static void imsg_event_add(struct imsgev *);
30 int
31 mark_nonblock(int fd)
32 {
33 int flags;
35 if ((flags = fcntl(fd, F_GETFL)) == -1)
36 return 0;
37 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
38 return 0;
39 return 1;
40 }
42 int
43 has_prefix(const char *str, const char *prfx)
44 {
45 size_t i;
47 for (i = 0; str[i] != '\0' && prfx[i] != '\0'; ++i)
48 if (str[i] != prfx[i])
49 return 0;
50 return prfx[i] == '\0';
51 }
53 int
54 has_suffix(const char *str, const char *sufx)
55 {
56 size_t l, s;
58 l = strlen(str);
59 s = strlen(sufx);
61 if (l < s)
62 return 0;
64 return !strcmp(str + (l - s), sufx);
65 }
67 int
68 unicode_isspace(uint32_t cp)
69 {
70 if (cp < INT8_MAX)
71 return isspace(cp);
72 return 0;
73 }
75 int
76 unicode_isgraph(uint32_t cp)
77 {
78 if (cp < INT8_MAX)
79 return isgraph(cp);
80 return 1;
81 }
83 static void
84 imsg_event_add(struct imsgev *iev)
85 {
86 iev->events = EV_READ;
87 if (iev->ibuf.w.queued)
88 iev->events |= EV_WRITE;
90 event_del(&iev->ev);
91 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
92 event_add(&iev->ev, NULL);
93 }
95 int
96 dispatch_imsg(struct imsgev *iev, short event, imsg_handlerfn **handlers,
97 size_t size)
98 {
99 struct imsgbuf *ibuf;
100 struct imsg imsg;
101 size_t datalen, i;
102 ssize_t n;
104 ibuf = &iev->ibuf;
106 if (event & EV_READ) {
107 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
108 err(1, "imsg_read error");
109 if (n == 0)
110 return -1;
112 if (event & EV_WRITE) {
113 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
114 err(1, "msgbuf_write");
115 if (n == 0)
116 return -1;
119 for (;;) {
120 if ((n = imsg_get(ibuf, &imsg)) == -1)
121 _exit(1);
122 if (n == 0)
123 break;
124 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
125 i = imsg.hdr.type;
126 if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
127 abort();
128 handlers[i](&imsg, datalen);
129 imsg_free(&imsg);
132 imsg_event_add(iev);
133 return 0;
136 int
137 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
138 pid_t pid, int fd, const void *data, uint16_t datalen)
140 int ret;
142 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
143 datalen) != -1))
144 imsg_event_add(iev);
146 return ret;