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"
27 #include "utils.h"
29 int
30 mark_nonblock_cloexec(int fd)
31 {
32 int flags;
34 if ((flags = fcntl(fd, F_GETFL)) == -1)
35 return 0;
36 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
37 return 0;
38 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
39 return 0;
40 return 1;
41 }
43 int
44 has_suffix(const char *str, const char *sufx)
45 {
46 size_t l, s;
48 l = strlen(str);
49 s = strlen(sufx);
51 if (l < s)
52 return 0;
54 return !strcmp(str + (l - s), sufx);
55 }
57 int
58 unicode_isspace(uint32_t cp)
59 {
60 if (cp < INT8_MAX)
61 return isspace(cp);
62 return 0;
63 }
65 int
66 unicode_isgraph(uint32_t cp)
67 {
68 if (cp < INT8_MAX)
69 return isgraph(cp);
70 return 1;
71 }
73 void
74 imsg_event_add(struct imsgev *iev)
75 {
76 iev->events = EV_READ;
77 if (iev->ibuf.w.queued)
78 iev->events |= EV_WRITE;
80 event_del(&iev->ev);
81 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
82 event_add(&iev->ev, NULL);
83 }
85 int
86 dispatch_imsg(struct imsgev *iev, short event, imsg_handlerfn **handlers,
87 size_t size)
88 {
89 struct imsgbuf *ibuf;
90 struct imsg imsg;
91 size_t datalen, i;
92 ssize_t n;
94 ibuf = &iev->ibuf;
96 if (event & EV_READ) {
97 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
98 err(1, "imsg_read error");
99 if (n == 0)
100 return -1;
102 if (event & EV_WRITE) {
103 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
104 err(1, "msgbuf_write");
105 if (n == 0)
106 return -1;
109 for (;;) {
110 if ((n = imsg_get(ibuf, &imsg)) == -1)
111 _exit(1);
112 if (n == 0)
113 break;
114 datalen = IMSG_DATA_SIZE(imsg);
115 i = imsg.hdr.type;
116 if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
117 abort();
118 handlers[i](&imsg, datalen);
119 imsg_free(&imsg);
122 imsg_event_add(iev);
123 return 0;
126 int
127 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
128 pid_t pid, int fd, const void *data, uint16_t datalen)
130 int ret;
132 if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
133 datalen) != -1))
134 imsg_event_add(iev);
136 return ret;
139 void *
140 hash_alloc(size_t len, void *d)
142 if ((d = malloc(len)) == NULL)
143 abort();
144 return d;
147 void *
148 hash_calloc(size_t nmemb, size_t size, void *d)
150 if ((d = calloc(nmemb, size)) == NULL)
151 abort();
152 return d;
155 void
156 hash_free(void *ptr, void *d)
158 free(ptr);