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_GET_RAW, /* get but with an explicit req str */
35 IMSG_ERR,
36 IMSG_CHECK_CERT,
37 IMSG_CERT_STATUS,
38 IMSG_GOT_CODE,
39 IMSG_GOT_META,
40 IMSG_PROCEED,
41 IMSG_STOP,
42 IMSG_BUF,
43 IMSG_EOF,
44 IMSG_QUIT,
46 /* ui <-> fs */
47 IMSG_BOOKMARK_PAGE,
48 IMSG_BOOKMARK_OK,
49 IMSG_SAVE_CERT,
50 IMSG_SAVE_CERT_OK,
51 IMSG_UPDATE_CERT,
52 IMSG_UPDATE_CERT_OK,
54 IMSG_FILE_OPEN,
55 IMSG_FILE_OPENED,
57 IMSG_SESSION_START,
58 IMSG_SESSION_TAB,
59 IMSG_SESSION_END,
60 };
62 extern char *new_tab_url;
63 extern int fill_column;
64 extern int olivetti_mode;
65 extern int enable_colors;
66 extern int hide_pre_context;
67 extern int hide_pre_blocks;
69 struct lineprefix {
70 const char *prfx1;
71 const char *prfx2;
72 };
73 extern struct lineprefix line_prefixes[];
75 struct line_face {
76 int prfx_pair, pair, trail_pair;
77 int prfx_bg, bg, trail_bg;
78 int prfx_fg, fg, trail_fg;
79 int prfx_attr, attr, trail_attr;
81 int prefix, text, trail;
82 };
83 extern struct line_face line_faces[];
85 struct tab_face {
86 int bg_attr, bg_bg, bg_fg;
87 int t_attr, t_bg, t_fg;
88 int c_attr, c_bg, c_fg;
90 int background, tab, current;
91 };
92 extern struct tab_face tab_face;
94 struct body_face {
95 int lbg, lfg;
96 int bg, fg;
97 int rbg, rfg;
99 int left, body, right;
100 };
101 extern struct body_face body_face;
103 struct modeline_face {
104 int bg, fg, attr;
105 int background;
106 };
107 extern struct modeline_face modeline_face;
109 struct minibuffer_face {
110 int bg, fg, attr;
111 int background;
112 };
113 extern struct minibuffer_face minibuffer_face;
115 enum line_type {
116 LINE_TEXT,
117 LINE_LINK,
118 LINE_TITLE_1,
119 LINE_TITLE_2,
120 LINE_TITLE_3,
121 LINE_ITEM,
122 LINE_QUOTE,
123 LINE_PRE_START,
124 LINE_PRE_CONTENT,
125 LINE_PRE_END,
126 };
128 /* for lines: mark as hidden */
129 #define L_HIDDEN 1
131 /* for vlines: mark as continuation */
132 #define L_CONTINUATION 2
134 struct line {
135 enum line_type type;
136 char *line;
137 char *alt;
138 int flags;
139 TAILQ_ENTRY(line) lines;
140 };
142 struct vline {
143 const struct line *parent;
144 char *line;
145 int flags;
146 TAILQ_ENTRY(vline) vlines;
147 };
149 struct parser;
150 struct page;
152 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
153 typedef int (*parserfreefn)(struct parser*);
155 typedef void (imsg_handlerfn)(struct imsg*, size_t);
157 struct parser {
158 const char *name;
159 char title[32+1];
160 char *buf;
161 size_t len;
162 size_t cap;
163 int flags;
164 parsechunkfn parse;
165 parserfreefn free;
167 TAILQ_HEAD(, line) head;
168 };
170 /*
171 * differnt types of trust for a certificate. Following
172 * gemini://thfr.info/gemini/modified-trust-verify.gmi
173 */
174 enum trust_state {
175 TS_UNKNOWN,
176 TS_UNTRUSTED,
177 TS_TRUSTED,
178 TS_VERIFIED,
179 };
181 struct tofu_entry {
182 char domain[GEMINI_URL_LEN];
184 /*
185 * enough space for ``PROTO:HASH''. probably isn't a good
186 * idea tho.
187 */
188 char hash[128+1];
189 int verified;
190 };
192 struct histhead {
193 TAILQ_HEAD(mhisthead, hist) head;
194 size_t len;
195 };
196 struct hist {
197 char h[1025];
198 TAILQ_ENTRY(hist) entries;
199 };
201 struct buffer {
202 struct parser page;
204 size_t last_line_off;
205 int force_redraw;
207 int curs_x;
208 int curs_y;
209 size_t line_off;
210 size_t line_max;
211 struct vline *top_line;
212 struct vline *current_line;
213 size_t cpoff;
214 TAILQ_HEAD(vhead, vline) head;
215 };
217 #define TAB_CURRENT 0x1
218 #define TAB_URGENT 0x2
220 #define NEW_TAB_URL "about:new"
222 extern TAILQ_HEAD(tabshead, tab) tabshead;
223 struct tab {
224 TAILQ_ENTRY(tab) tabs;
225 uint32_t id;
226 uint32_t flags;
228 char *cert;
229 enum trust_state trust;
230 struct proxy *proxy;
231 struct phos_uri uri;
232 struct histhead hist;
233 struct hist *hist_cur;
234 size_t hist_off;
236 int code;
237 char meta[GEMINI_URL_LEN];
238 int redirect_count;
240 struct buffer buffer;
242 short loading_anim;
243 short loading_anim_step;
244 struct event loadingev;
246 int fd;
247 size_t bytes;
248 char *path;
249 };
251 struct proto {
252 const char *schema;
254 /*
255 * should load the given url in the tab. Optionally, it may
256 * consider the given url as relative to the one already
257 * present in tab. It must set tab->urlstr to a serialized
258 * human-friendly URL.
259 */
260 void (*loadfn)(struct tab*, const char*);
261 };
263 extern TAILQ_HEAD(proxylist, proxy) proxies;
264 struct proxy {
265 char *match_proto;
267 char *host;
268 char *port;
269 int proto;
271 TAILQ_ENTRY(proxy) proxies;
272 };
274 enum {
275 PROTO_GEMINI,
276 /* ... */
277 };
279 struct get_req {
280 int proto;
281 char host[254];
282 char port[16];
283 char req[1027];
284 };
286 struct kmap {
287 TAILQ_HEAD(map, keymap) m;
288 void (*unhandled_input)(void);
289 };
290 extern struct kmap global_map, minibuffer_map;
292 typedef void(interactivefn)(struct buffer *);
294 struct keymap {
295 int meta;
296 int key;
297 struct kmap map;
298 interactivefn *fn;
300 TAILQ_ENTRY(keymap) keymaps;
301 };
303 struct cmd {
304 const char *cmd;
305 void (*fn)(struct buffer *);
306 };
307 extern struct cmd cmds[];
309 /* defaults.c */
310 void config_init(void);
311 int config_setprfx(const char *, const char *, const char *);
312 int config_setvari(const char *, int);
313 int config_setvars(const char *, char *);
314 int config_setcolor(int, const char *, int, int, int);
315 int config_setattr(const char *, int, int, int);
316 void config_apply_style(void);
318 /* fs.c */
319 int fs_init(void);
320 int fs_main(struct imsgbuf*);
321 int load_certs(struct ohash*);
322 int load_last_session(void(*)(const char*));
324 /* gemini.c */
325 int client_main(struct imsgbuf*);
327 /* gemtext.c */
328 void gemtext_initparser(struct parser*);
330 /* hist.c */
331 void hist_clear_forward(struct histhead*, struct hist*);
332 void hist_push(struct histhead*, struct hist*);
334 /* keymap.c */
335 int kbd(const char*);
336 const char *unkbd(int);
337 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
339 /* mime.c */
340 int setup_parser_for(struct tab*);
342 /* pages.c */
343 extern const char *about_about;
344 extern const char *about_blank;
345 extern const char *about_help;
346 extern const char *about_new;
348 #define CANNOT_FETCH 0
349 #define TOO_MUCH_REDIRECTS 1
350 #define MALFORMED_RESPONSE 2
351 #define UNKNOWN_TYPE_OR_CSET 3
352 extern const char *err_pages[70];
354 /* parse.y */
355 void parseconfig(const char *, int);
357 /* parser.c */
358 int parser_append(struct parser*, const char*, size_t);
359 int parser_set_buf(struct parser*, const char*, size_t);
360 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
362 /* sandbox.c */
363 void sandbox_network_process(void);
364 void sandbox_ui_process(void);
365 void sandbox_fs_process(void);
367 /* telescope.c */
368 void load_about_url(struct tab*, const char*);
369 void load_gemini_url(struct tab*, const char*);
370 void load_via_proxy(struct tab *, const char *, struct proxy *);
371 void load_url(struct tab*, const char*);
372 int load_previous_page(struct tab*);
373 int load_next_page(struct tab*);
374 void stop_tab(struct tab*);
375 void add_to_bookmarks(const char*);
376 void save_session(void);
378 /* textplain.c */
379 void textplain_initparser(struct parser*);
381 /* tofu.c */
382 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
383 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
384 void tofu_add(struct ohash*, struct tofu_entry*);
385 void tofu_update(struct ohash*, struct tofu_entry*);
387 /* ui.c */
388 extern int body_lines;
389 extern int body_cols;
390 extern int in_minibuffer;
392 struct excursion {
393 int curs_x, curs_y;
394 size_t line_off;
395 struct vline *current_line;
396 size_t cpoff;
397 };
399 enum pairs {
400 PTL_BG = 1,
401 PTL_TAB,
402 PTL_CURR,
404 PBODY,
405 PBLEFT,
406 PBRIGHT,
408 PT,
409 PT_PRFX,
410 PT_TRAIL,
411 PL,
412 PL_PRFX,
413 PL_TRAIL,
414 PT1,
415 PT1_PRFX,
416 PT1_TRAIL,
417 PT2,
418 PT2_PRFX,
419 PT2_TRAIL,
420 PT3,
421 PT3_PRFX,
422 PT3_TRAIL,
423 PI,
424 PI_PRFX,
425 PI_TRAIL,
426 PQ,
427 PQ_PRFX,
428 PQ_TRAIL,
429 PPSTART,
430 PPSTART_PRFX,
431 PPSTART_TRAIL,
432 PP,
433 PP_PRFX,
434 PP_TRAIL,
435 PPEND,
436 PPEND_PRFX,
437 PPEND_TRAIL,
439 PMODELINE,
441 PMINIBUF,
442 };
444 struct thiskey {
445 short meta;
446 int key;
447 uint32_t cp;
448 };
449 extern struct thiskey thiskey;
451 extern struct histhead eecmd_history,
452 ir_history,
453 lu_history,
454 read_history;
456 struct ministate {
457 char *curmesg;
459 char prompt[64];
460 void (*donefn)(void);
461 void (*abortfn)(void);
463 char buf[1025];
464 struct line line;
465 struct vline vline;
466 struct buffer buffer;
468 struct histhead *history;
469 struct hist *hist_cur;
470 size_t hist_off;
471 };
472 extern struct ministate ministate;
474 void save_excursion(struct excursion *, struct buffer *);
475 void restore_excursion(struct excursion *, struct buffer *);
476 void minibuffer_taint_hist(void);
477 void eecmd_self_insert(void);
478 void eecmd_select(void);
479 void ir_self_insert(void);
480 void ir_select(void);
481 void lu_self_insert(void);
482 void lu_select(void);
483 void bp_select(void);
484 void vmessage(const char*, va_list);
485 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
486 void start_loading_anim(struct tab *tab);
487 void load_url_in_tab(struct tab *, const char *);
488 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
489 void exit_minibuffer(void);
490 void switch_to_tab(struct tab *);
491 struct tab *current_tab(void);
492 struct tab *new_tab(const char *);
493 unsigned int tab_new_id(void);
494 int ui_init(int, char * const*);
495 void ui_on_tab_loaded(struct tab*);
496 void ui_on_tab_refresh(struct tab*);
497 const char *ui_keyname(int);
498 void ui_toggle_side_window(void);
499 void ui_schedule_redraw(void);
500 void ui_require_input(struct tab*, int);
501 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
502 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
503 void ui_end(void);
505 /* utf.8 */
506 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
507 size_t utf8_encode(uint32_t, char*);
508 char *utf8_nth(char*, size_t);
509 size_t utf8_cplen(char*);
510 size_t utf8_chwidth(uint32_t);
511 size_t utf8_snwidth(const char*, size_t);
512 size_t utf8_swidth(const char*);
513 size_t utf8_swidth_between(const char*, const char*);
514 char *utf8_next_cp(const char*);
515 char *utf8_prev_cp(const char*, const char*);
517 /* util.c */
518 int mark_nonblock(int);
519 int has_prefix(const char*, const char*);
520 int unicode_isspace(uint32_t);
521 int unicode_isgraph(uint32_t);
522 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
524 /* wrap.c */
525 void erase_buffer(struct buffer *);
526 void empty_linelist(struct buffer*);
527 void empty_vlist(struct buffer*);
528 int wrap_text(struct buffer*, const char*, struct line*, size_t);
529 int hardwrap_text(struct buffer*, struct line*, size_t);
531 #endif /* TELESCOPE_H */