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 struct tab *t;
113 t = tab_by_id(imsg->hdr.peerid);
114 if (!t->page.free(&t->page))
115 die();
117 ui_on_tab_refresh(t);
120 static void
121 dispatch_imsg(int fd, short ev, void *d)
123 struct imsg imsg;
124 size_t datalen;
125 ssize_t n;
127 if ((n = imsg_read(ibuf)) == -1) {
128 if (errno == EAGAIN || errno == EWOULDBLOCK)
129 return;
130 die();
133 if (n == 0) {
134 fprintf(stderr, "other side is dead\n");
135 exit(0);
138 for (;;) {
139 if ((n = imsg_get(ibuf, &imsg)) == -1)
140 die();
141 if (n == 0)
142 return;
143 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
144 handlers[imsg.hdr.type](&imsg, datalen);
145 imsg_free(&imsg);
149 void
150 new_tab(void)
152 struct tab *tab;
153 const char *url = "about:new";
154 /* const char *url = "gemini://localhost/cgi/slow-out"; */
156 if ((tab = calloc(1, sizeof(*tab))) == NULL)
157 die();
159 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
161 tab->id = tab_counter++;
162 TAILQ_INIT(&tab->page.head);
163 gemtext_initparser(&tab->page);
165 imsg_compose(ibuf, IMSG_GET, tab->id, 0, -1, url, strlen(url)+1);
166 imsg_flush(ibuf);
168 ui_on_new_tab(tab);
171 int
172 main(void)
174 struct imsgbuf main_ibuf, network_ibuf;
175 int imsg_fds[2];
177 signal(SIGCHLD, SIG_IGN);
178 signal(SIGINT, SIG_IGN);
180 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
181 err(1, "socketpair");
183 switch (fork()) {
184 case -1:
185 err(1, "fork");
186 case 0:
187 /* child */
188 setproctitle("client");
189 close(imsg_fds[0]);
190 imsg_init(&network_ibuf, imsg_fds[1]);
191 exit(client_main(&network_ibuf));
194 close(imsg_fds[1]);
195 imsg_init(&main_ibuf, imsg_fds[0]);
196 ibuf = &main_ibuf;
198 TAILQ_INIT(&tabshead);
200 event_init();
202 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, ibuf);
203 event_add(&imsgev, NULL);
205 ui_init();
207 new_tab();
209 event_dispatch();
211 imsg_compose(ibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
212 imsg_flush(ibuf);
214 ui_end();
216 return 0;