Blame


1 5e11c00c 2021-03-02 op /*
2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 5e11c00c 2021-03-02 op *
4 5e11c00c 2021-03-02 op * Permission to use, copy, modify, and distribute this software for any
5 5e11c00c 2021-03-02 op * purpose with or without fee is hereby granted, provided that the above
6 5e11c00c 2021-03-02 op * copyright notice and this permission notice appear in all copies.
7 5e11c00c 2021-03-02 op *
8 5e11c00c 2021-03-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5e11c00c 2021-03-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5e11c00c 2021-03-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5e11c00c 2021-03-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5e11c00c 2021-03-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5e11c00c 2021-03-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5e11c00c 2021-03-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5e11c00c 2021-03-02 op */
16 5e11c00c 2021-03-02 op
17 786e6deb 2021-07-21 op #include "compat.h"
18 5e11c00c 2021-03-02 op
19 17e5106b 2021-03-21 op #include <ctype.h>
20 1304bbdd 2021-03-15 op #include <errno.h>
21 5e11c00c 2021-03-02 op #include <fcntl.h>
22 b0003f58 2021-03-08 op #include <stdlib.h>
23 cd18973c 2021-08-13 op #include <string.h>
24 1304bbdd 2021-03-15 op #include <unistd.h>
25 5e11c00c 2021-03-02 op
26 786e6deb 2021-07-21 op #include "telescope.h"
27 9d65b1d9 2022-01-11 op #include "utils.h"
28 786e6deb 2021-07-21 op
29 bc10f6a5 2021-07-12 op static void imsg_event_add(struct imsgev *);
30 bc10f6a5 2021-07-12 op
31 5e11c00c 2021-03-02 op int
32 5e11c00c 2021-03-02 op mark_nonblock(int fd)
33 5e11c00c 2021-03-02 op {
34 5e11c00c 2021-03-02 op int flags;
35 5e11c00c 2021-03-02 op
36 5e11c00c 2021-03-02 op if ((flags = fcntl(fd, F_GETFL)) == -1)
37 95a8c791 2021-08-26 op return 0;
38 5e11c00c 2021-03-02 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
39 95a8c791 2021-08-26 op return 0;
40 5e11c00c 2021-03-02 op return 1;
41 5e11c00c 2021-03-02 op }
42 5e11c00c 2021-03-02 op
43 ee130316 2021-03-09 op int
44 ee130316 2021-03-09 op has_prefix(const char *str, const char *prfx)
45 ee130316 2021-03-09 op {
46 ee130316 2021-03-09 op size_t i;
47 ee130316 2021-03-09 op
48 ee130316 2021-03-09 op for (i = 0; str[i] != '\0' && prfx[i] != '\0'; ++i)
49 ee130316 2021-03-09 op if (str[i] != prfx[i])
50 ee130316 2021-03-09 op return 0;
51 ee130316 2021-03-09 op return prfx[i] == '\0';
52 ee130316 2021-03-09 op }
53 1304bbdd 2021-03-15 op
54 17e5106b 2021-03-21 op int
55 cd18973c 2021-08-13 op has_suffix(const char *str, const char *sufx)
56 cd18973c 2021-08-13 op {
57 cd18973c 2021-08-13 op size_t l, s;
58 cd18973c 2021-08-13 op
59 cd18973c 2021-08-13 op l = strlen(str);
60 cd18973c 2021-08-13 op s = strlen(sufx);
61 cd18973c 2021-08-13 op
62 cd18973c 2021-08-13 op if (l < s)
63 cd18973c 2021-08-13 op return 0;
64 cd18973c 2021-08-13 op
65 cd18973c 2021-08-13 op return !strcmp(str + (l - s), sufx);
66 cd18973c 2021-08-13 op }
67 cd18973c 2021-08-13 op
68 cd18973c 2021-08-13 op int
69 17e5106b 2021-03-21 op unicode_isspace(uint32_t cp)
70 17e5106b 2021-03-21 op {
71 17e5106b 2021-03-21 op if (cp < INT8_MAX)
72 17e5106b 2021-03-21 op return isspace(cp);
73 17e5106b 2021-03-21 op return 0;
74 17e5106b 2021-03-21 op }
75 17e5106b 2021-03-21 op
76 17e5106b 2021-03-21 op int
77 17e5106b 2021-03-21 op unicode_isgraph(uint32_t cp)
78 17e5106b 2021-03-21 op {
79 17e5106b 2021-03-21 op if (cp < INT8_MAX)
80 17e5106b 2021-03-21 op return isgraph(cp);
81 17e5106b 2021-03-21 op return 1;
82 17e5106b 2021-03-21 op }
83 17e5106b 2021-03-21 op
84 bc10f6a5 2021-07-12 op static void
85 bc10f6a5 2021-07-12 op imsg_event_add(struct imsgev *iev)
86 bc10f6a5 2021-07-12 op {
87 bc10f6a5 2021-07-12 op iev->events = EV_READ;
88 bc10f6a5 2021-07-12 op if (iev->ibuf.w.queued)
89 bc10f6a5 2021-07-12 op iev->events |= EV_WRITE;
90 bc10f6a5 2021-07-12 op
91 bc10f6a5 2021-07-12 op event_del(&iev->ev);
92 bc10f6a5 2021-07-12 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
93 bc10f6a5 2021-07-12 op event_add(&iev->ev, NULL);
94 bc10f6a5 2021-07-12 op }
95 bc10f6a5 2021-07-12 op
96 ea080950 2021-07-20 op int
97 bc10f6a5 2021-07-12 op dispatch_imsg(struct imsgev *iev, short event, imsg_handlerfn **handlers,
98 bc10f6a5 2021-07-12 op size_t size)
99 1304bbdd 2021-03-15 op {
100 bc10f6a5 2021-07-12 op struct imsgbuf *ibuf;
101 bc10f6a5 2021-07-12 op struct imsg imsg;
102 bc10f6a5 2021-07-12 op size_t datalen, i;
103 bc10f6a5 2021-07-12 op ssize_t n;
104 1304bbdd 2021-03-15 op
105 bc10f6a5 2021-07-12 op ibuf = &iev->ibuf;
106 bc10f6a5 2021-07-12 op
107 bc10f6a5 2021-07-12 op if (event & EV_READ) {
108 bc10f6a5 2021-07-12 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
109 bc10f6a5 2021-07-12 op err(1, "imsg_read error");
110 bc10f6a5 2021-07-12 op if (n == 0)
111 ea080950 2021-07-20 op return -1;
112 1304bbdd 2021-03-15 op }
113 bc10f6a5 2021-07-12 op if (event & EV_WRITE) {
114 bc10f6a5 2021-07-12 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
115 bc10f6a5 2021-07-12 op err(1, "msgbuf_write");
116 bc10f6a5 2021-07-12 op if (n == 0)
117 ea080950 2021-07-20 op return -1;
118 bc10f6a5 2021-07-12 op }
119 1304bbdd 2021-03-15 op
120 1304bbdd 2021-03-15 op for (;;) {
121 1304bbdd 2021-03-15 op if ((n = imsg_get(ibuf, &imsg)) == -1)
122 1304bbdd 2021-03-15 op _exit(1);
123 1304bbdd 2021-03-15 op if (n == 0)
124 bc10f6a5 2021-07-12 op break;
125 1304bbdd 2021-03-15 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
126 1304bbdd 2021-03-15 op i = imsg.hdr.type;
127 3a227e9a 2021-03-18 op if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
128 1304bbdd 2021-03-15 op abort();
129 1304bbdd 2021-03-15 op handlers[i](&imsg, datalen);
130 1304bbdd 2021-03-15 op imsg_free(&imsg);
131 1304bbdd 2021-03-15 op }
132 bc10f6a5 2021-07-12 op
133 bc10f6a5 2021-07-12 op imsg_event_add(iev);
134 ea080950 2021-07-20 op return 0;
135 1304bbdd 2021-03-15 op }
136 bc10f6a5 2021-07-12 op
137 bc10f6a5 2021-07-12 op int
138 bc10f6a5 2021-07-12 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
139 bc10f6a5 2021-07-12 op pid_t pid, int fd, const void *data, uint16_t datalen)
140 bc10f6a5 2021-07-12 op {
141 bc10f6a5 2021-07-12 op int ret;
142 bc10f6a5 2021-07-12 op
143 bc10f6a5 2021-07-12 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
144 bc10f6a5 2021-07-12 op datalen) != -1))
145 bc10f6a5 2021-07-12 op imsg_event_add(iev);
146 bc10f6a5 2021-07-12 op
147 bc10f6a5 2021-07-12 op return ret;
148 bc10f6a5 2021-07-12 op }
149 036c104e 2022-01-11 op
150 036c104e 2022-01-11 op void *
151 036c104e 2022-01-11 op hash_alloc(size_t len, void *d)
152 036c104e 2022-01-11 op {
153 036c104e 2022-01-11 op if ((d = malloc(len)) == NULL)
154 036c104e 2022-01-11 op abort();
155 036c104e 2022-01-11 op return d;
156 036c104e 2022-01-11 op }
157 036c104e 2022-01-11 op
158 036c104e 2022-01-11 op void *
159 036c104e 2022-01-11 op hash_calloc(size_t nmemb, size_t size, void *d)
160 036c104e 2022-01-11 op {
161 036c104e 2022-01-11 op if ((d = calloc(nmemb, size)) == NULL)
162 036c104e 2022-01-11 op abort();
163 036c104e 2022-01-11 op return d;
164 036c104e 2022-01-11 op }
165 036c104e 2022-01-11 op
166 036c104e 2022-01-11 op void
167 036c104e 2022-01-11 op hash_free(void *ptr, void *d)
168 036c104e 2022-01-11 op {
169 036c104e 2022-01-11 op free(ptr);
170 036c104e 2022-01-11 op }