002
2022-01-11
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
004
2022-01-11
op
* Permission to use, copy, modify, and distribute this software for any
005
2022-01-11
op
* purpose with or without fee is hereby granted, provided that the above
006
2022-01-11
op
* copyright notice and this permission notice appear in all copies.
008
2022-01-11
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2022-01-11
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2022-01-11
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2022-01-11
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2022-01-11
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2022-01-11
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2022-01-11
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2022-01-11
op
#include "compat.h"
019
2022-01-11
op
#include <ctype.h>
020
2022-01-11
op
#include <errno.h>
021
2022-01-11
op
#include <fcntl.h>
022
2022-01-11
op
#include <stdlib.h>
023
2022-01-11
op
#include <string.h>
024
2022-01-11
op
#include <unistd.h>
026
2022-01-11
op
#include "telescope.h"
027
2022-01-11
op
#include "utils.h"
030
2022-01-11
op
mark_nonblock(int fd)
032
2022-01-11
op
int flags;
034
2022-01-11
op
if ((flags = fcntl(fd, F_GETFL)) == -1)
035
2022-01-11
op
return 0;
036
2022-01-11
op
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
037
2022-01-11
op
return 0;
038
2022-01-11
op
return 1;
042
2022-01-11
op
has_suffix(const char *str, const char *sufx)
044
2022-01-11
op
size_t l, s;
046
2022-01-11
op
l = strlen(str);
047
2022-01-11
op
s = strlen(sufx);
049
2022-01-11
op
if (l < s)
050
2022-01-11
op
return 0;
052
2022-01-11
op
return !strcmp(str + (l - s), sufx);
056
2022-01-11
op
unicode_isspace(uint32_t cp)
058
2022-01-11
op
if (cp < INT8_MAX)
059
2022-01-11
op
return isspace(cp);
060
2022-01-11
op
return 0;
064
2022-01-11
op
unicode_isgraph(uint32_t cp)
066
2022-01-11
op
if (cp < INT8_MAX)
067
2022-01-11
op
return isgraph(cp);
068
2022-01-11
op
return 1;
072
2022-01-11
op
imsg_event_add(struct imsgev *iev)
074
2022-01-11
op
iev->events = EV_READ;
075
2022-01-11
op
if (iev->ibuf.w.queued)
076
2022-01-11
op
iev->events |= EV_WRITE;
078
2022-01-11
op
event_del(&iev->ev);
079
2022-01-11
op
event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
080
2022-01-11
op
event_add(&iev->ev, NULL);
084
2022-01-11
op
dispatch_imsg(struct imsgev *iev, short event, imsg_handlerfn **handlers,
085
2022-01-11
op
size_t size)
087
2022-01-11
op
struct imsgbuf *ibuf;
088
2022-01-11
op
struct imsg imsg;
089
2022-01-11
op
size_t datalen, i;
090
2022-01-11
op
ssize_t n;
092
2022-01-11
op
ibuf = &iev->ibuf;
094
2022-01-11
op
if (event & EV_READ) {
095
2022-01-11
op
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
096
2022-01-11
op
err(1, "imsg_read error");
097
2022-01-11
op
if (n == 0)
098
2022-01-11
op
return -1;
100
2022-01-11
op
if (event & EV_WRITE) {
101
2022-01-11
op
if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
102
2022-01-11
op
err(1, "msgbuf_write");
103
2022-01-11
op
if (n == 0)
104
2022-01-11
op
return -1;
107
2022-01-11
op
for (;;) {
108
2022-01-11
op
if ((n = imsg_get(ibuf, &imsg)) == -1)
109
2022-01-11
op
_exit(1);
110
2022-01-11
op
if (n == 0)
112
2022-02-11
op
datalen = IMSG_DATA_SIZE(imsg);
113
2022-01-11
op
i = imsg.hdr.type;
114
2022-01-11
op
if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL)
116
2022-01-11
op
handlers[i](&imsg, datalen);
117
2022-01-11
op
imsg_free(&imsg);
120
2022-01-11
op
imsg_event_add(iev);
121
2022-01-11
op
return 0;
125
2022-01-11
op
imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
126
2022-01-11
op
pid_t pid, int fd, const void *data, uint16_t datalen)
130
2022-01-11
op
if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
131
2022-01-11
op
datalen) != -1))
132
2022-01-11
op
imsg_event_add(iev);
134
2022-01-11
op
return ret;
138
2022-01-11
op
hash_alloc(size_t len, void *d)
140
2022-01-11
op
if ((d = malloc(len)) == NULL)
142
2022-01-11
op
return d;
146
2022-01-11
op
hash_calloc(size_t nmemb, size_t size, void *d)
148
2022-01-11
op
if ((d = calloc(nmemb, size)) == NULL)
150
2022-01-11
op
return d;
154
2022-01-11
op
hash_free(void *ptr, void *d)
156
2022-01-11
op
free(ptr);