Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #ifndef TELESCOPE_H
18 #define TELESCOPE_H
20 #include "cmd.h"
21 #include "compat.h"
22 #include "phos/phos.h"
24 #include <event.h>
26 #define MIN(a, b) ((a) < (b) ? (a) : (b))
27 #define MAX(a, b) ((a) > (b) ? (a) : (b))
29 #define GEMINI_URL_LEN 1024
31 enum imsg_type {
32 /* ui <-> client/fs */
33 IMSG_GET, /* data is URL, peerid the tab id */
34 IMSG_ERR,
35 IMSG_CHECK_CERT,
36 IMSG_CERT_STATUS,
37 IMSG_GOT_CODE,
38 IMSG_GOT_META,
39 IMSG_PROCEED,
40 IMSG_STOP,
41 IMSG_BUF,
42 IMSG_EOF,
43 IMSG_QUIT,
45 /* ui <-> fs */
46 IMSG_BOOKMARK_PAGE,
47 IMSG_BOOKMARK_OK,
48 IMSG_SAVE_CERT,
49 IMSG_SAVE_CERT_OK,
50 IMSG_UPDATE_CERT,
51 IMSG_UPDATE_CERT_OK,
53 IMSG_FILE_OPEN,
54 IMSG_FILE_OPENED,
56 IMSG_SESSION_START,
57 IMSG_SESSION_TAB,
58 IMSG_SESSION_END,
59 };
61 extern char *new_tab_url;
62 extern int fill_column;
63 extern int olivetti_mode;
64 extern int enable_colors;
66 struct lineprefix {
67 const char *prfx1;
68 const char *prfx2;
69 };
70 extern struct lineprefix line_prefixes[];
72 struct line_face {
73 int prefix_prop;
74 int text_prop;
75 };
76 extern struct line_face line_faces[];
78 struct tab_face {
79 int background, tab, current_tab;
80 };
81 extern struct tab_face tab_face;
83 struct modeline_face {
84 int background;
85 };
86 extern struct modeline_face modeline_face;
88 struct minibuffer_face {
89 int background;
90 };
91 extern struct minibuffer_face minibuffer_face;
93 enum line_type {
94 LINE_TEXT,
95 LINE_LINK,
96 LINE_TITLE_1,
97 LINE_TITLE_2,
98 LINE_TITLE_3,
99 LINE_ITEM,
100 LINE_QUOTE,
101 LINE_PRE_START,
102 LINE_PRE_CONTENT,
103 LINE_PRE_END,
104 };
106 struct line {
107 enum line_type type;
108 char *line;
109 char *alt;
110 int flags;
111 TAILQ_ENTRY(line) lines;
112 };
114 struct vline {
115 const struct line *parent;
116 char *line;
117 int flags;
118 TAILQ_ENTRY(vline) vlines;
119 };
121 struct parser;
122 struct page;
124 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
125 typedef int (*parserfreefn)(struct parser*);
127 typedef void (imsg_handlerfn)(struct imsg*, size_t);
129 struct parser {
130 const char *name;
131 char title[32+1];
132 char *buf;
133 size_t len;
134 size_t cap;
135 int flags;
136 parsechunkfn parse;
137 parserfreefn free;
139 TAILQ_HEAD(, line) head;
140 };
142 /*
143 * differnt types of trust for a certificate. Following
144 * gemini://thfr.info/gemini/modified-trust-verify.gmi
145 */
146 enum trust_state {
147 TS_UNKNOWN,
148 TS_UNTRUSTED,
149 TS_TRUSTED,
150 TS_VERIFIED,
151 };
153 struct tofu_entry {
154 char domain[GEMINI_URL_LEN];
156 /*
157 * enough space for ``PROTO:HASH''. probably isn't a good
158 * idea tho.
159 */
160 char hash[128+1];
161 int verified;
162 };
164 struct histhead {
165 TAILQ_HEAD(mhisthead, hist) head;
166 size_t len;
167 };
168 struct hist {
169 char h[1025];
170 TAILQ_ENTRY(hist) entries;
171 };
173 struct buffer {
174 struct parser page;
176 size_t last_line_off;
177 int force_redraw;
179 int curs_x;
180 int curs_y;
181 size_t line_off;
182 size_t line_max;
183 struct vline *current_line;
184 size_t cpoff;
185 TAILQ_HEAD(vhead, vline) head;
186 };
188 #define TAB_CURRENT 0x1
189 #define TAB_URGENT 0x2
191 #define NEW_TAB_URL "about:new"
193 extern TAILQ_HEAD(tabshead, tab) tabshead;
194 struct tab {
195 TAILQ_ENTRY(tab) tabs;
196 uint32_t id;
197 uint32_t flags;
199 char *cert;
200 enum trust_state trust;
201 struct phos_uri uri;
202 struct histhead hist;
203 struct hist *hist_cur;
204 size_t hist_off;
206 int code;
207 char meta[GEMINI_URL_LEN];
208 int redirect_count;
210 struct buffer buffer;
212 short loading_anim;
213 short loading_anim_step;
214 struct event loadingev;
216 int fd;
217 size_t bytes;
218 char *path;
219 };
221 struct proto {
222 const char *schema;
224 /*
225 * should load the given url in the tab. Optionally, it may
226 * consider the given url as relative to the one already
227 * present in tab. It must set tab->urlstr to a serialized
228 * human-friendly URL.
229 */
230 void (*loadfn)(struct tab*, const char*);
231 };
233 struct kmap {
234 TAILQ_HEAD(map, keymap) m;
235 void (*unhandled_input)(void);
236 };
238 struct keymap {
239 int meta;
240 int key;
241 struct kmap map;
242 void (*fn)(struct buffer*);
244 TAILQ_ENTRY(keymap) keymaps;
245 };
247 /* defaults.c */
248 int config_setprfx(const char *, int, const char *);
249 int config_setvari(const char *, int);
250 int config_setvars(const char *, char *);
251 int config_setcolor(const char *, int, int, int);
252 void config_apply_colors(void);
254 /* fs.c */
255 int fs_init(void);
256 int fs_main(struct imsgbuf*);
257 int load_certs(struct ohash*);
258 int load_last_session(void(*)(const char*));
260 /* gemini.c */
261 int client_main(struct imsgbuf*);
263 /* gemtext.c */
264 void gemtext_initparser(struct parser*);
266 /* hist.c */
267 void hist_clear_forward(struct histhead*, struct hist*);
268 void hist_push(struct histhead*, struct hist*);
270 /* keymap.c */
271 int kbd(const char*);
272 const char *unkbd(int);
273 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
275 /* mime.c */
276 int setup_parser_for(struct tab*);
278 /* pages.c */
279 extern const char *about_about;
280 extern const char *about_blank;
281 extern const char *about_help;
282 extern const char *about_new;
284 #define CANNOT_FETCH 0
285 #define TOO_MUCH_REDIRECTS 1
286 #define MALFORMED_RESPONSE 2
287 #define UNKNOWN_TYPE_OR_CSET 3
288 extern const char *err_pages[70];
290 /* parse.y */
291 void parseconfig(const char *, int);
293 /* parser.c */
294 int parser_append(struct parser*, const char*, size_t);
295 int parser_set_buf(struct parser*, const char*, size_t);
296 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
298 /* sandbox.c */
299 void sandbox_network_process(void);
300 void sandbox_ui_process(void);
301 void sandbox_fs_process(void);
303 /* telescope.c */
304 void load_about_url(struct tab*, const char*);
305 void load_gemini_url(struct tab*, const char*);
306 void load_url(struct tab*, const char*);
307 int load_previous_page(struct tab*);
308 int load_next_page(struct tab*);
309 void stop_tab(struct tab*);
310 void add_to_bookmarks(const char*);
311 void save_session(void);
313 /* textplain.c */
314 void textplain_initparser(struct parser*);
316 /* tofu.c */
317 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
318 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
319 void tofu_add(struct ohash*, struct tofu_entry*);
320 void tofu_update(struct ohash*, struct tofu_entry*);
322 /* ui.c */
323 extern int body_lines;
324 extern int body_cols;
325 extern int in_minibuffer;
327 enum {
328 PAIR_TEXT = 1,
329 PAIR_TEXT_PRFX,
330 PAIR_LINK,
331 PAIR_LINK_PRFX,
332 PAIR_TITLE_1,
333 PAIR_TITLE_1_PRFX,
334 PAIR_TITLE_2,
335 PAIR_TITLE_2_PRFX,
336 PAIR_TITLE_3,
337 PAIR_TITLE_3_PRFX,
338 PAIR_ITEM,
339 PAIR_ITEM_PRFX,
340 PAIR_QUOTE,
341 PAIR_QUOTE_PRFX,
342 PAIR_PRE_START,
343 PAIR_PRE_START_PRFX,
344 PAIR_PRE,
345 PAIR_PRE_PRFX,
346 PAIR_PRE_END,
347 PAIR_PRE_END_PRFX,
348 };
350 struct thiskey {
351 short meta;
352 int key;
353 uint32_t cp;
354 };
355 extern struct thiskey thiskey;
357 extern struct histhead eecmd_history,
358 ir_history,
359 lu_history,
360 read_history;
362 struct ministate {
363 char *curmesg;
365 char prompt[64];
366 void (*donefn)(void);
367 void (*abortfn)(void);
369 char buf[1025];
370 struct line line;
371 struct vline vline;
372 struct buffer buffer;
374 struct histhead *history;
375 struct hist *hist_cur;
376 size_t hist_off;
377 };
378 extern struct ministate ministate;
380 void restore_cursor(struct buffer *);
381 void minibuffer_taint_hist(void);
382 void eecmd_self_insert(void);
383 void eecmd_select(void);
384 void ir_self_insert(void);
385 void ir_select(void);
386 void lu_self_insert(void);
387 void lu_select(void);
388 void bp_select(void);
389 void vmessage(const char*, va_list);
390 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
391 void start_loading_anim(struct tab *tab);
392 void load_url_in_tab(struct tab *, const char *);
393 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
394 void exit_minibuffer(void);
395 void switch_to_tab(struct tab *);
396 struct tab *current_tab(void);
397 struct tab *new_tab(const char *);
398 unsigned int tab_new_id(void);
399 int ui_init(int, char * const*);
400 void ui_on_tab_loaded(struct tab*);
401 void ui_on_tab_refresh(struct tab*);
402 const char *ui_keyname(int);
403 void ui_toggle_side_window(void);
404 void ui_schedule_redraw(void);
405 void ui_require_input(struct tab*, int);
406 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
407 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
408 void ui_end(void);
410 /* utf.8 */
411 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
412 size_t utf8_encode(uint32_t, char*);
413 char *utf8_nth(char*, size_t);
414 size_t utf8_cplen(char*);
415 size_t utf8_chwidth(uint32_t);
416 size_t utf8_snwidth(const char*, size_t);
417 size_t utf8_swidth(const char*);
418 size_t utf8_swidth_between(const char*, const char*);
419 char *utf8_next_cp(const char*);
420 char *utf8_prev_cp(const char*, const char*);
422 /* util.c */
423 int mark_nonblock(int);
424 int has_prefix(const char*, const char*);
425 int unicode_isspace(uint32_t);
426 int unicode_isgraph(uint32_t);
427 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
429 /* wrap.c */
430 void empty_linelist(struct buffer*);
431 void empty_vlist(struct buffer*);
432 int wrap_text(struct buffer*, const char*, struct line*, size_t);
433 int hardwrap_text(struct buffer*, struct line*, size_t);
435 #endif /* TELESCOPE_H */