1 5e11c00c 2021-03-02 op #include "telescope.h"
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
5 5e11c00c 2021-03-02 op #include <err.h>
6 5e11c00c 2021-03-02 op #include <errno.h>
7 5e11c00c 2021-03-02 op #include <event.h>
8 5e11c00c 2021-03-02 op #include <signal.h>
9 5e11c00c 2021-03-02 op #include <stdio.h>
10 5e11c00c 2021-03-02 op #include <stdlib.h>
11 5e11c00c 2021-03-02 op #include <string.h>
12 5e11c00c 2021-03-02 op #include <unistd.h>
14 5e11c00c 2021-03-02 op struct event imsgev;
15 5e11c00c 2021-03-02 op struct tabshead tabshead;
17 5e11c00c 2021-03-02 op static struct imsgbuf *ibuf;
18 5e11c00c 2021-03-02 op static uint32_t tab_counter;
20 5e11c00c 2021-03-02 op static void handle_imsg_err(struct imsg*, size_t);
21 5e11c00c 2021-03-02 op static void handle_imsg_check_cert(struct imsg*, size_t);
22 5e11c00c 2021-03-02 op static void handle_imsg_got_code(struct imsg*, size_t);
23 5e11c00c 2021-03-02 op static void handle_imsg_got_meta(struct imsg*, size_t);
24 5e11c00c 2021-03-02 op static void handle_imsg_buf(struct imsg*, size_t);
25 5e11c00c 2021-03-02 op static void handle_imsg_eof(struct imsg*, size_t);
27 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
28 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
29 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
30 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
31 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
32 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
33 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
36 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
39 5e11c00c 2021-03-02 op abort(); /* TODO */
42 5e11c00c 2021-03-02 op static struct tab *
43 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
45 5e11c00c 2021-03-02 op struct tab *t;
47 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
48 5e11c00c 2021-03-02 op if (t->id == id)
56 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
58 5e11c00c 2021-03-02 op /* write(2, imsg->data, datalen); */
59 5e11c00c 2021-03-02 op /* fprintf(stderr, "\nEOF\n"); */
60 5e11c00c 2021-03-02 op /* event_loopbreak(); */
64 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
66 5e11c00c 2021-03-02 op int tofu_res = 1;
68 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
69 5e11c00c 2021-03-02 op imsg_flush(ibuf);
73 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
77 5e11c00c 2021-03-02 op if (sizeof(code) != datalen)
80 5e11c00c 2021-03-02 op memcpy(&code, imsg->data, sizeof(code));
82 5e11c00c 2021-03-02 op /* fprintf(stderr, "got status code: %d\n", code); */
86 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
88 5e11c00c 2021-03-02 op /* fprintf(stderr, "got meta: "); */
89 5e11c00c 2021-03-02 op /* fflush(stderr); */
90 5e11c00c 2021-03-02 op /* write(2, imsg->data, datalen); */
91 5e11c00c 2021-03-02 op /* fprintf(stderr, "\n"); */
95 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
97 5e11c00c 2021-03-02 op struct tab *t;
98 5e11c00c 2021-03-02 op struct line *l;
100 5e11c00c 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
102 5e11c00c 2021-03-02 op if (!t->page.parse(&t->page, imsg->data, datalen))
105 5e11c00c 2021-03-02 op ui_on_tab_refresh(t);
109 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
111 5e11c00c 2021-03-02 op /* printf("===== EOF\n"); */
112 5e11c00c 2021-03-02 op /* event_loopbreak(); */
116 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
118 5e11c00c 2021-03-02 op struct imsg imsg;
119 5e11c00c 2021-03-02 op size_t datalen;
122 5e11c00c 2021-03-02 op if ((n = imsg_read(ibuf)) == -1) {
123 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
128 5e11c00c 2021-03-02 op if (n == 0) {
129 5e11c00c 2021-03-02 op fprintf(stderr, "other side is dead\n");
134 5e11c00c 2021-03-02 op if ((n = imsg_get(ibuf, &imsg)) == -1)
138 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
139 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
140 5e11c00c 2021-03-02 op imsg_free(&imsg);
145 5e11c00c 2021-03-02 op new_tab(void)
147 5e11c00c 2021-03-02 op struct tab *tab;
148 5e11c00c 2021-03-02 op const char *url = "about:new";
149 5e11c00c 2021-03-02 op /* const char *url = "gemini://localhost/cgi/slow-out"; */
151 5e11c00c 2021-03-02 op if ((tab = calloc(1, sizeof(*tab))) == NULL)
154 5e11c00c 2021-03-02 op TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
156 5e11c00c 2021-03-02 op tab->id = tab_counter++;
157 5e11c00c 2021-03-02 op TAILQ_INIT(&tab->page.head);
158 5e11c00c 2021-03-02 op gemtext_initparser(&tab->page);
160 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_GET, tab->id, 0, -1, url, strlen(url)+1);
161 5e11c00c 2021-03-02 op imsg_flush(ibuf);
163 5e11c00c 2021-03-02 op ui_on_new_tab(tab);
169 5e11c00c 2021-03-02 op struct imsgbuf main_ibuf, network_ibuf;
170 5e11c00c 2021-03-02 op int imsg_fds[2];
172 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
173 5e11c00c 2021-03-02 op signal(SIGINT, SIG_IGN);
175 5e11c00c 2021-03-02 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
176 5e11c00c 2021-03-02 op err(1, "socketpair");
178 5e11c00c 2021-03-02 op switch (fork()) {
180 5e11c00c 2021-03-02 op err(1, "fork");
183 5e11c00c 2021-03-02 op setproctitle("client");
184 5e11c00c 2021-03-02 op close(imsg_fds[0]);
185 5e11c00c 2021-03-02 op imsg_init(&network_ibuf, imsg_fds[1]);
186 5e11c00c 2021-03-02 op exit(client_main(&network_ibuf));
189 5e11c00c 2021-03-02 op close(imsg_fds[1]);
190 5e11c00c 2021-03-02 op imsg_init(&main_ibuf, imsg_fds[0]);
191 5e11c00c 2021-03-02 op ibuf = &main_ibuf;
193 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
195 5e11c00c 2021-03-02 op event_init();
197 5e11c00c 2021-03-02 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
198 5e11c00c 2021-03-02 op event_add(&imsgev, NULL);
204 5e11c00c 2021-03-02 op event_dispatch();
206 5e11c00c 2021-03-02 op imsg_compose(ibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
207 5e11c00c 2021-03-02 op imsg_flush(ibuf);