2 5e11c00c 2021-03-02 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 5e11c00c 2021-03-02 op #include "telescope.h"
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>
26 5e11c00c 2021-03-02 op mark_nonblock(int fd)
30 5e11c00c 2021-03-02 op if ((flags = fcntl(fd, F_GETFL)) == -1)
32 5e11c00c 2021-03-02 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
38 ee130316 2021-03-09 op has_prefix(const char *str, const char *prfx)
42 ee130316 2021-03-09 op for (i = 0; str[i] != '\0' && prfx[i] != '\0'; ++i)
43 ee130316 2021-03-09 op if (str[i] != prfx[i])
45 ee130316 2021-03-09 op return prfx[i] == '\0';
49 17e5106b 2021-03-21 op unicode_isspace(uint32_t cp)
51 17e5106b 2021-03-21 op if (cp < INT8_MAX)
52 17e5106b 2021-03-21 op return isspace(cp);
57 17e5106b 2021-03-21 op unicode_isgraph(uint32_t cp)
59 17e5106b 2021-03-21 op if (cp < INT8_MAX)
60 17e5106b 2021-03-21 op return isgraph(cp);
65 1304bbdd 2021-03-15 op dispatch_imsg(struct imsgbuf *ibuf, imsg_handlerfn **handlers, size_t size)
67 1304bbdd 2021-03-15 op struct imsg imsg;
68 1304bbdd 2021-03-15 op size_t datalen, i;
71 1304bbdd 2021-03-15 op if ((n = imsg_read(ibuf)) == -1) {
72 1304bbdd 2021-03-15 op if (errno == EAGAIN || errno == EWOULDBLOCK)
81 1304bbdd 2021-03-15 op if ((n = imsg_get(ibuf, &imsg)) == -1)
85 1304bbdd 2021-03-15 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
86 1304bbdd 2021-03-15 op i = imsg.hdr.type;
87 3a227e9a 2021-03-18 op if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
89 1304bbdd 2021-03-15 op handlers[i](&imsg, datalen);
90 1304bbdd 2021-03-15 op imsg_free(&imsg);