Blob


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