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 5e11c00c 2021-03-02 op #include "telescope.h"
18 5e11c00c 2021-03-02 op
19 1304bbdd 2021-03-15 op #include <errno.h>
20 5e11c00c 2021-03-02 op #include <fcntl.h>
21 b0003f58 2021-03-08 op #include <stdlib.h>
22 1304bbdd 2021-03-15 op #include <unistd.h>
23 5e11c00c 2021-03-02 op
24 5e11c00c 2021-03-02 op int
25 5e11c00c 2021-03-02 op mark_nonblock(int fd)
26 5e11c00c 2021-03-02 op {
27 5e11c00c 2021-03-02 op int flags;
28 5e11c00c 2021-03-02 op
29 5e11c00c 2021-03-02 op if ((flags = fcntl(fd, F_GETFL)) == -1)
30 5e11c00c 2021-03-02 op return 0;
31 5e11c00c 2021-03-02 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
32 5e11c00c 2021-03-02 op return 0;
33 5e11c00c 2021-03-02 op return 1;
34 5e11c00c 2021-03-02 op }
35 5e11c00c 2021-03-02 op
36 5e11c00c 2021-03-02 op char *
37 5e11c00c 2021-03-02 op telescope_strnchr(char *b, char d, size_t len)
38 5e11c00c 2021-03-02 op {
39 5e11c00c 2021-03-02 op size_t i;
40 5e11c00c 2021-03-02 op
41 5e11c00c 2021-03-02 op for (i = 0; i < len; ++i) {
42 5e11c00c 2021-03-02 op if (b[i] == d)
43 5e11c00c 2021-03-02 op return &b[i];
44 5e11c00c 2021-03-02 op }
45 5e11c00c 2021-03-02 op
46 5e11c00c 2021-03-02 op return NULL;
47 5e11c00c 2021-03-02 op }
48 ee130316 2021-03-09 op
49 ee130316 2021-03-09 op int
50 ee130316 2021-03-09 op has_prefix(const char *str, const char *prfx)
51 ee130316 2021-03-09 op {
52 ee130316 2021-03-09 op size_t i;
53 ee130316 2021-03-09 op
54 ee130316 2021-03-09 op for (i = 0; str[i] != '\0' && prfx[i] != '\0'; ++i)
55 ee130316 2021-03-09 op if (str[i] != prfx[i])
56 ee130316 2021-03-09 op return 0;
57 ee130316 2021-03-09 op return prfx[i] == '\0';
58 ee130316 2021-03-09 op }
59 1304bbdd 2021-03-15 op
60 1304bbdd 2021-03-15 op void
61 1304bbdd 2021-03-15 op dispatch_imsg(struct imsgbuf *ibuf, imsg_handlerfn **handlers, size_t size)
62 1304bbdd 2021-03-15 op {
63 1304bbdd 2021-03-15 op struct imsg imsg;
64 1304bbdd 2021-03-15 op size_t datalen, i;
65 1304bbdd 2021-03-15 op ssize_t n;
66 1304bbdd 2021-03-15 op
67 1304bbdd 2021-03-15 op if ((n = imsg_read(ibuf)) == -1) {
68 1304bbdd 2021-03-15 op if (errno == EAGAIN || errno == EWOULDBLOCK)
69 1304bbdd 2021-03-15 op return;
70 1304bbdd 2021-03-15 op _exit(1);
71 1304bbdd 2021-03-15 op }
72 1304bbdd 2021-03-15 op
73 1304bbdd 2021-03-15 op if (n == 0)
74 1304bbdd 2021-03-15 op _exit(1);
75 1304bbdd 2021-03-15 op
76 1304bbdd 2021-03-15 op for (;;) {
77 1304bbdd 2021-03-15 op if ((n = imsg_get(ibuf, &imsg)) == -1)
78 1304bbdd 2021-03-15 op _exit(1);
79 1304bbdd 2021-03-15 op if (n == 0)
80 1304bbdd 2021-03-15 op return;
81 1304bbdd 2021-03-15 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
82 1304bbdd 2021-03-15 op i = imsg.hdr.type;
83 1304bbdd 2021-03-15 op if (i >= (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
84 1304bbdd 2021-03-15 op abort();
85 1304bbdd 2021-03-15 op handlers[i](&imsg, datalen);
86 1304bbdd 2021-03-15 op imsg_free(&imsg);
87 1304bbdd 2021-03-15 op }
88 1304bbdd 2021-03-15 op }