/* * Copyright (c) 2021 Omar Polo * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "telescope.h" #include #include #include #include #include int mark_nonblock(int fd) { int flags; if ((flags = fcntl(fd, F_GETFL)) == -1) return 0; if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) return 0; return 1; } int has_prefix(const char *str, const char *prfx) { size_t i; for (i = 0; str[i] != '\0' && prfx[i] != '\0'; ++i) if (str[i] != prfx[i]) return 0; return prfx[i] == '\0'; } int unicode_isspace(uint32_t cp) { if (cp < INT8_MAX) return isspace(cp); return 0; } int unicode_isgraph(uint32_t cp) { if (cp < INT8_MAX) return isgraph(cp); return 1; } void dispatch_imsg(struct imsgbuf *ibuf, imsg_handlerfn **handlers, size_t size) { struct imsg imsg; size_t datalen, i; ssize_t n; if ((n = imsg_read(ibuf)) == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) return; _exit(1); } if (n == 0) _exit(1); for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) _exit(1); if (n == 0) return; datalen = imsg.hdr.len - IMSG_HEADER_SIZE; i = imsg.hdr.type; if (i > (size / sizeof(imsg_handlerfn*)) || handlers[i] == NULL) abort(); handlers[i](&imsg, datalen); imsg_free(&imsg); } }