Blame


1 c553191e 2022-01-11 op /*
2 c553191e 2022-01-11 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 c553191e 2022-01-11 op *
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.
7 c553191e 2022-01-11 op *
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.
15 c553191e 2022-01-11 op */
16 c553191e 2022-01-11 op
17 c553191e 2022-01-11 op #include "compat.h"
18 c553191e 2022-01-11 op
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>
25 c553191e 2022-01-11 op
26 c553191e 2022-01-11 op #include "telescope.h"
27 c553191e 2022-01-11 op #include "utils.h"
28 c553191e 2022-01-11 op
29 c553191e 2022-01-11 op int
30 c553191e 2022-01-11 op mark_nonblock(int fd)
31 c553191e 2022-01-11 op {
32 c553191e 2022-01-11 op int flags;
33 c553191e 2022-01-11 op
34 c553191e 2022-01-11 op if ((flags = fcntl(fd, F_GETFL)) == -1)
35 c553191e 2022-01-11 op return 0;
36 c553191e 2022-01-11 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
37 c553191e 2022-01-11 op return 0;
38 c553191e 2022-01-11 op return 1;
39 c553191e 2022-01-11 op }
40 c553191e 2022-01-11 op
41 c553191e 2022-01-11 op int
42 c553191e 2022-01-11 op has_suffix(const char *str, const char *sufx)
43 c553191e 2022-01-11 op {
44 c553191e 2022-01-11 op size_t l, s;
45 c553191e 2022-01-11 op
46 c553191e 2022-01-11 op l = strlen(str);
47 c553191e 2022-01-11 op s = strlen(sufx);
48 c553191e 2022-01-11 op
49 c553191e 2022-01-11 op if (l < s)
50 c553191e 2022-01-11 op return 0;
51 c553191e 2022-01-11 op
52 c553191e 2022-01-11 op return !strcmp(str + (l - s), sufx);
53 c553191e 2022-01-11 op }
54 c553191e 2022-01-11 op
55 c553191e 2022-01-11 op int
56 c553191e 2022-01-11 op unicode_isspace(uint32_t cp)
57 c553191e 2022-01-11 op {
58 c553191e 2022-01-11 op if (cp < INT8_MAX)
59 c553191e 2022-01-11 op return isspace(cp);
60 c553191e 2022-01-11 op return 0;
61 c553191e 2022-01-11 op }
62 c553191e 2022-01-11 op
63 c553191e 2022-01-11 op int
64 c553191e 2022-01-11 op unicode_isgraph(uint32_t cp)
65 c553191e 2022-01-11 op {
66 c553191e 2022-01-11 op if (cp < INT8_MAX)
67 c553191e 2022-01-11 op return isgraph(cp);
68 c553191e 2022-01-11 op return 1;
69 c553191e 2022-01-11 op }
70 c553191e 2022-01-11 op
71 0f559c55 2022-02-11 op void
72 c553191e 2022-01-11 op imsg_event_add(struct imsgev *iev)
73 c553191e 2022-01-11 op {
74 c553191e 2022-01-11 op iev->events = EV_READ;
75 c553191e 2022-01-11 op if (iev->ibuf.w.queued)
76 c553191e 2022-01-11 op iev->events |= EV_WRITE;
77 c553191e 2022-01-11 op
78 c553191e 2022-01-11 op event_del(&iev->ev);
79 c553191e 2022-01-11 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
80 c553191e 2022-01-11 op event_add(&iev->ev, NULL);
81 c553191e 2022-01-11 op }
82 c553191e 2022-01-11 op
83 c553191e 2022-01-11 op int
84 c553191e 2022-01-11 op dispatch_imsg(struct imsgev *iev, short event, imsg_handlerfn **handlers,
85 c553191e 2022-01-11 op size_t size)
86 c553191e 2022-01-11 op {
87 c553191e 2022-01-11 op struct imsgbuf *ibuf;
88 c553191e 2022-01-11 op struct imsg imsg;
89 c553191e 2022-01-11 op size_t datalen, i;
90 c553191e 2022-01-11 op ssize_t n;
91 c553191e 2022-01-11 op
92 c553191e 2022-01-11 op ibuf = &iev->ibuf;
93 c553191e 2022-01-11 op
94 c553191e 2022-01-11 op if (event & EV_READ) {
95 c553191e 2022-01-11 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
96 c553191e 2022-01-11 op err(1, "imsg_read error");
97 c553191e 2022-01-11 op if (n == 0)
98 c553191e 2022-01-11 op return -1;
99 c553191e 2022-01-11 op }
100 c553191e 2022-01-11 op if (event & EV_WRITE) {
101 c553191e 2022-01-11 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
102 c553191e 2022-01-11 op err(1, "msgbuf_write");
103 c553191e 2022-01-11 op if (n == 0)
104 c553191e 2022-01-11 op return -1;
105 c553191e 2022-01-11 op }
106 c553191e 2022-01-11 op
107 c553191e 2022-01-11 op for (;;) {
108 c553191e 2022-01-11 op if ((n = imsg_get(ibuf, &imsg)) == -1)
109 c553191e 2022-01-11 op _exit(1);
110 c553191e 2022-01-11 op if (n == 0)
111 c553191e 2022-01-11 op break;
112 7018345e 2022-02-11 op datalen = IMSG_DATA_SIZE(imsg);
113 c553191e 2022-01-11 op i = imsg.hdr.type;
114 c553191e 2022-01-11 op if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
115 c553191e 2022-01-11 op abort();
116 c553191e 2022-01-11 op handlers[i](&imsg, datalen);
117 c553191e 2022-01-11 op imsg_free(&imsg);
118 c553191e 2022-01-11 op }
119 c553191e 2022-01-11 op
120 c553191e 2022-01-11 op imsg_event_add(iev);
121 c553191e 2022-01-11 op return 0;
122 c553191e 2022-01-11 op }
123 c553191e 2022-01-11 op
124 c553191e 2022-01-11 op int
125 c553191e 2022-01-11 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
126 c553191e 2022-01-11 op pid_t pid, int fd, const void *data, uint16_t datalen)
127 c553191e 2022-01-11 op {
128 c553191e 2022-01-11 op int ret;
129 c553191e 2022-01-11 op
130 c553191e 2022-01-11 op if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
131 c553191e 2022-01-11 op datalen) != -1))
132 c553191e 2022-01-11 op imsg_event_add(iev);
133 c553191e 2022-01-11 op
134 c553191e 2022-01-11 op return ret;
135 c553191e 2022-01-11 op }
136 c553191e 2022-01-11 op
137 c553191e 2022-01-11 op void *
138 c553191e 2022-01-11 op hash_alloc(size_t len, void *d)
139 c553191e 2022-01-11 op {
140 c553191e 2022-01-11 op if ((d = malloc(len)) == NULL)
141 c553191e 2022-01-11 op abort();
142 c553191e 2022-01-11 op return d;
143 c553191e 2022-01-11 op }
144 c553191e 2022-01-11 op
145 c553191e 2022-01-11 op void *
146 c553191e 2022-01-11 op hash_calloc(size_t nmemb, size_t size, void *d)
147 c553191e 2022-01-11 op {
148 c553191e 2022-01-11 op if ((d = calloc(nmemb, size)) == NULL)
149 c553191e 2022-01-11 op abort();
150 c553191e 2022-01-11 op return d;
151 c553191e 2022-01-11 op }
152 c553191e 2022-01-11 op
153 c553191e 2022-01-11 op void
154 c553191e 2022-01-11 op hash_free(void *ptr, void *d)
155 c553191e 2022-01-11 op {
156 c553191e 2022-01-11 op free(ptr);
157 c553191e 2022-01-11 op }