2 c553191e 2022-01-11 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 c553191e 2022-01-11 op * Permission to use, copy, modify, and distribute this software for any
5 c553191e 2022-01-11 op * purpose with or without fee is hereby granted, provided that the above
6 c553191e 2022-01-11 op * copyright notice and this permission notice appear in all copies.
8 c553191e 2022-01-11 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c553191e 2022-01-11 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c553191e 2022-01-11 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c553191e 2022-01-11 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c553191e 2022-01-11 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c553191e 2022-01-11 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c553191e 2022-01-11 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 c553191e 2022-01-11 op #include "compat.h"
19 c553191e 2022-01-11 op #include <ctype.h>
20 c553191e 2022-01-11 op #include <errno.h>
21 c553191e 2022-01-11 op #include <fcntl.h>
22 c553191e 2022-01-11 op #include <stdlib.h>
23 c553191e 2022-01-11 op #include <string.h>
24 c553191e 2022-01-11 op #include <unistd.h>
26 c553191e 2022-01-11 op #include "telescope.h"
27 c553191e 2022-01-11 op #include "utils.h"
29 c553191e 2022-01-11 op static void imsg_event_add(struct imsgev *);
32 c553191e 2022-01-11 op mark_nonblock(int fd)
36 c553191e 2022-01-11 op if ((flags = fcntl(fd, F_GETFL)) == -1)
38 c553191e 2022-01-11 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
44 c553191e 2022-01-11 op has_prefix(const char *str, const char *prfx)
48 c553191e 2022-01-11 op for (i = 0; str[i] != '\0' && prfx[i] != '\0'; ++i)
49 c553191e 2022-01-11 op if (str[i] != prfx[i])
51 c553191e 2022-01-11 op return prfx[i] == '\0';
55 c553191e 2022-01-11 op has_suffix(const char *str, const char *sufx)
59 c553191e 2022-01-11 op l = strlen(str);
60 c553191e 2022-01-11 op s = strlen(sufx);
65 c553191e 2022-01-11 op return !strcmp(str + (l - s), sufx);
69 c553191e 2022-01-11 op unicode_isspace(uint32_t cp)
71 c553191e 2022-01-11 op if (cp < INT8_MAX)
72 c553191e 2022-01-11 op return isspace(cp);
77 c553191e 2022-01-11 op unicode_isgraph(uint32_t cp)
79 c553191e 2022-01-11 op if (cp < INT8_MAX)
80 c553191e 2022-01-11 op return isgraph(cp);
85 c553191e 2022-01-11 op imsg_event_add(struct imsgev *iev)
87 c553191e 2022-01-11 op iev->events = EV_READ;
88 c553191e 2022-01-11 op if (iev->ibuf.w.queued)
89 c553191e 2022-01-11 op iev->events |= EV_WRITE;
91 c553191e 2022-01-11 op event_del(&iev->ev);
92 c553191e 2022-01-11 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
93 c553191e 2022-01-11 op event_add(&iev->ev, NULL);
97 c553191e 2022-01-11 op dispatch_imsg(struct imsgev *iev, short event, imsg_handlerfn **handlers,
100 c553191e 2022-01-11 op struct imsgbuf *ibuf;
101 c553191e 2022-01-11 op struct imsg imsg;
102 c553191e 2022-01-11 op size_t datalen, i;
105 c553191e 2022-01-11 op ibuf = &iev->ibuf;
107 c553191e 2022-01-11 op if (event & EV_READ) {
108 c553191e 2022-01-11 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
109 c553191e 2022-01-11 op err(1, "imsg_read error");
113 c553191e 2022-01-11 op if (event & EV_WRITE) {
114 c553191e 2022-01-11 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
115 c553191e 2022-01-11 op err(1, "msgbuf_write");
121 c553191e 2022-01-11 op if ((n = imsg_get(ibuf, &imsg)) == -1)
125 c553191e 2022-01-11 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
126 c553191e 2022-01-11 op i = imsg.hdr.type;
127 c553191e 2022-01-11 op if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
129 c553191e 2022-01-11 op handlers[i](&imsg, datalen);
130 c553191e 2022-01-11 op imsg_free(&imsg);
133 c553191e 2022-01-11 op imsg_event_add(iev);
138 c553191e 2022-01-11 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
139 c553191e 2022-01-11 op pid_t pid, int fd, const void *data, uint16_t datalen)
143 c553191e 2022-01-11 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
144 c553191e 2022-01-11 op datalen) != -1))
145 c553191e 2022-01-11 op imsg_event_add(iev);
151 c553191e 2022-01-11 op hash_alloc(size_t len, void *d)
153 c553191e 2022-01-11 op if ((d = malloc(len)) == NULL)
159 c553191e 2022-01-11 op hash_calloc(size_t nmemb, size_t size, void *d)
161 c553191e 2022-01-11 op if ((d = calloc(nmemb, size)) == NULL)
167 c553191e 2022-01-11 op hash_free(void *ptr, void *d)