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 1304bbdd 2021-03-15 op #include <unistd.h>
24 5e11c00c 2021-03-02 op
25 786e6deb 2021-07-21 op #include "telescope.h"
26 786e6deb 2021-07-21 op
27 bc10f6a5 2021-07-12 op static void imsg_event_add(struct imsgev *);
28 bc10f6a5 2021-07-12 op
29 5e11c00c 2021-03-02 op int
30 5e11c00c 2021-03-02 op mark_nonblock(int fd)
31 5e11c00c 2021-03-02 op {
32 5e11c00c 2021-03-02 op int flags;
33 5e11c00c 2021-03-02 op
34 5e11c00c 2021-03-02 op if ((flags = fcntl(fd, F_GETFL)) == -1)
35 5e11c00c 2021-03-02 op return 0;
36 5e11c00c 2021-03-02 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
37 5e11c00c 2021-03-02 op return 0;
38 5e11c00c 2021-03-02 op return 1;
39 5e11c00c 2021-03-02 op }
40 5e11c00c 2021-03-02 op
41 ee130316 2021-03-09 op int
42 ee130316 2021-03-09 op has_prefix(const char *str, const char *prfx)
43 ee130316 2021-03-09 op {
44 ee130316 2021-03-09 op size_t i;
45 ee130316 2021-03-09 op
46 ee130316 2021-03-09 op for (i = 0; str[i] != '\0' && prfx[i] != '\0'; ++i)
47 ee130316 2021-03-09 op if (str[i] != prfx[i])
48 ee130316 2021-03-09 op return 0;
49 ee130316 2021-03-09 op return prfx[i] == '\0';
50 ee130316 2021-03-09 op }
51 1304bbdd 2021-03-15 op
52 17e5106b 2021-03-21 op int
53 17e5106b 2021-03-21 op unicode_isspace(uint32_t cp)
54 17e5106b 2021-03-21 op {
55 17e5106b 2021-03-21 op if (cp < INT8_MAX)
56 17e5106b 2021-03-21 op return isspace(cp);
57 17e5106b 2021-03-21 op return 0;
58 17e5106b 2021-03-21 op }
59 17e5106b 2021-03-21 op
60 17e5106b 2021-03-21 op int
61 17e5106b 2021-03-21 op unicode_isgraph(uint32_t cp)
62 17e5106b 2021-03-21 op {
63 17e5106b 2021-03-21 op if (cp < INT8_MAX)
64 17e5106b 2021-03-21 op return isgraph(cp);
65 17e5106b 2021-03-21 op return 1;
66 17e5106b 2021-03-21 op }
67 17e5106b 2021-03-21 op
68 bc10f6a5 2021-07-12 op static void
69 bc10f6a5 2021-07-12 op imsg_event_add(struct imsgev *iev)
70 bc10f6a5 2021-07-12 op {
71 bc10f6a5 2021-07-12 op iev->events = EV_READ;
72 bc10f6a5 2021-07-12 op if (iev->ibuf.w.queued)
73 bc10f6a5 2021-07-12 op iev->events |= EV_WRITE;
74 bc10f6a5 2021-07-12 op
75 bc10f6a5 2021-07-12 op event_del(&iev->ev);
76 bc10f6a5 2021-07-12 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
77 bc10f6a5 2021-07-12 op event_add(&iev->ev, NULL);
78 bc10f6a5 2021-07-12 op }
79 bc10f6a5 2021-07-12 op
80 ea080950 2021-07-20 op int
81 bc10f6a5 2021-07-12 op dispatch_imsg(struct imsgev *iev, short event, imsg_handlerfn **handlers,
82 bc10f6a5 2021-07-12 op size_t size)
83 1304bbdd 2021-03-15 op {
84 bc10f6a5 2021-07-12 op struct imsgbuf *ibuf;
85 bc10f6a5 2021-07-12 op struct imsg imsg;
86 bc10f6a5 2021-07-12 op size_t datalen, i;
87 bc10f6a5 2021-07-12 op ssize_t n;
88 1304bbdd 2021-03-15 op
89 bc10f6a5 2021-07-12 op ibuf = &iev->ibuf;
90 bc10f6a5 2021-07-12 op
91 bc10f6a5 2021-07-12 op if (event & EV_READ) {
92 bc10f6a5 2021-07-12 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
93 bc10f6a5 2021-07-12 op err(1, "imsg_read error");
94 bc10f6a5 2021-07-12 op if (n == 0)
95 ea080950 2021-07-20 op return -1;
96 1304bbdd 2021-03-15 op }
97 bc10f6a5 2021-07-12 op if (event & EV_WRITE) {
98 bc10f6a5 2021-07-12 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
99 bc10f6a5 2021-07-12 op err(1, "msgbuf_write");
100 bc10f6a5 2021-07-12 op if (n == 0)
101 ea080950 2021-07-20 op return -1;
102 bc10f6a5 2021-07-12 op }
103 1304bbdd 2021-03-15 op
104 1304bbdd 2021-03-15 op for (;;) {
105 1304bbdd 2021-03-15 op if ((n = imsg_get(ibuf, &imsg)) == -1)
106 1304bbdd 2021-03-15 op _exit(1);
107 1304bbdd 2021-03-15 op if (n == 0)
108 bc10f6a5 2021-07-12 op break;
109 1304bbdd 2021-03-15 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
110 1304bbdd 2021-03-15 op i = imsg.hdr.type;
111 3a227e9a 2021-03-18 op if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
112 1304bbdd 2021-03-15 op abort();
113 1304bbdd 2021-03-15 op handlers[i](&imsg, datalen);
114 1304bbdd 2021-03-15 op imsg_free(&imsg);
115 1304bbdd 2021-03-15 op }
116 bc10f6a5 2021-07-12 op
117 bc10f6a5 2021-07-12 op imsg_event_add(iev);
118 ea080950 2021-07-20 op return 0;
119 1304bbdd 2021-03-15 op }
120 bc10f6a5 2021-07-12 op
121 bc10f6a5 2021-07-12 op int
122 bc10f6a5 2021-07-12 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
123 bc10f6a5 2021-07-12 op pid_t pid, int fd, const void *data, uint16_t datalen)
124 bc10f6a5 2021-07-12 op {
125 bc10f6a5 2021-07-12 op int ret;
126 bc10f6a5 2021-07-12 op
127 bc10f6a5 2021-07-12 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
128 bc10f6a5 2021-07-12 op datalen) != -1))
129 bc10f6a5 2021-07-12 op imsg_event_add(iev);
130 bc10f6a5 2021-07-12 op
131 bc10f6a5 2021-07-12 op return ret;
132 bc10f6a5 2021-07-12 op }