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;
164 #define PARSER_IN_BODY 1
165 #define PARSER_IN_PRE 2
166 int flags;
167 parsechunkfn parse;
168 parserfreefn free;
170 TAILQ_HEAD(, line) head;
171 };
173 /*
174 * differnt types of trust for a certificate. Following
175 * gemini://thfr.info/gemini/modified-trust-verify.gmi
176 */
177 enum trust_state {
178 TS_UNKNOWN,
179 TS_UNTRUSTED,
180 TS_TEMP_TRUSTED,
181 TS_TRUSTED,
182 TS_VERIFIED,
183 };
185 struct tofu_entry {
186 char domain[GEMINI_URL_LEN];
188 /*
189 * enough space for ``PROTO:HASH''. probably isn't a good
190 * idea tho.
191 */
192 char hash[128+1];
193 int verified;
194 };
196 struct histhead {
197 TAILQ_HEAD(mhisthead, hist) head;
198 size_t len;
199 };
200 struct hist {
201 char h[1025];
202 TAILQ_ENTRY(hist) entries;
203 };
205 struct buffer {
206 struct parser page;
208 size_t last_line_off;
209 int force_redraw;
211 int curs_x;
212 int curs_y;
213 size_t line_off;
214 size_t line_max;
215 struct vline *top_line;
216 struct vline *current_line;
217 size_t cpoff;
218 TAILQ_HEAD(vhead, vline) head;
219 };
221 #define TAB_CURRENT 0x1
222 #define TAB_URGENT 0x2
224 #define NEW_TAB_URL "about:new"
226 extern TAILQ_HEAD(tabshead, tab) tabshead;
227 struct tab {
228 TAILQ_ENTRY(tab) tabs;
229 uint32_t id;
230 uint32_t flags;
232 char *cert;
233 enum trust_state trust;
234 struct proxy *proxy;
235 struct phos_uri uri;
236 struct histhead hist;
237 struct hist *hist_cur;
238 size_t hist_off;
240 int code;
241 char meta[GEMINI_URL_LEN];
242 int redirect_count;
244 struct buffer buffer;
246 short loading_anim;
247 short loading_anim_step;
248 struct event loadingev;
250 int fd;
251 size_t bytes;
252 char *path;
253 };
255 struct proto {
256 const char *schema;
258 /*
259 * should load the given url in the tab. Optionally, it may
260 * consider the given url as relative to the one already
261 * present in tab. It must set tab->urlstr to a serialized
262 * human-friendly URL.
263 */
264 void (*loadfn)(struct tab*, const char*);
265 };
267 extern TAILQ_HEAD(proxylist, proxy) proxies;
268 struct proxy {
269 char *match_proto;
271 char *host;
272 char *port;
273 int proto;
275 TAILQ_ENTRY(proxy) proxies;
276 };
278 enum {
279 PROTO_GEMINI,
280 /* ... */
281 };
283 struct get_req {
284 int proto;
285 char host[254];
286 char port[16];
287 char req[1027];
288 };
290 struct kmap {
291 TAILQ_HEAD(map, keymap) m;
292 void (*unhandled_input)(void);
293 };
294 extern struct kmap global_map, minibuffer_map;
296 typedef void(interactivefn)(struct buffer *);
298 struct keymap {
299 int meta;
300 int key;
301 struct kmap map;
302 interactivefn *fn;
304 TAILQ_ENTRY(keymap) keymaps;
305 };
307 struct cmd {
308 const char *cmd;
309 void (*fn)(struct buffer *);
310 };
311 extern struct cmd cmds[];
313 /* defaults.c */
314 void config_init(void);
315 int config_setprfx(const char *, const char *, const char *);
316 int config_setvari(const char *, int);
317 int config_setvars(const char *, char *);
318 int config_setcolor(int, const char *, int, int, int);
319 int config_setattr(const char *, int, int, int);
320 void config_apply_style(void);
322 /* fs.c */
323 int fs_init(void);
324 int fs_main(void);
325 int load_certs(struct ohash*);
326 int load_last_session(void(*)(const char*));
328 /* gemini.c */
329 int client_main(void);
331 /* hist.c */
332 void hist_clear_forward(struct histhead*, struct hist*);
333 void hist_push(struct histhead*, struct hist*);
335 /* keymap.c */
336 int kbd(const char*);
337 const char *unkbd(int);
338 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
340 /* mime.c */
341 int setup_parser_for(struct tab*);
343 /* pages.c */
344 extern const char *about_about;
345 extern const char *about_blank;
346 extern const char *about_help;
347 extern const char *about_new;
349 #define CANNOT_FETCH 0
350 #define TOO_MUCH_REDIRECTS 1
351 #define MALFORMED_RESPONSE 2
352 #define UNKNOWN_TYPE_OR_CSET 3
353 extern const char *err_pages[70];
355 /* parse.y */
356 void parseconfig(const char *, int);
358 /* sandbox.c */
359 void sandbox_network_process(void);
360 void sandbox_ui_process(void);
361 void sandbox_fs_process(void);
363 /* telescope.c */
364 void load_about_url(struct tab*, const char*);
365 void load_gemini_url(struct tab*, const char*);
366 void load_via_proxy(struct tab *, const char *, struct proxy *);
367 void load_url(struct tab*, const char*);
368 int load_previous_page(struct tab*);
369 int load_next_page(struct tab*);
370 void stop_tab(struct tab*);
371 void add_to_bookmarks(const char*);
372 void save_session(void);
374 /* tofu.c */
375 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
376 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
377 void tofu_add(struct ohash*, struct tofu_entry*);
378 void tofu_update(struct ohash*, struct tofu_entry*);
379 void tofu_temp_trust(struct ohash *, const char *, const char *, const char *);
381 /* ui.c */
382 extern int body_lines;
383 extern int body_cols;
384 extern int in_minibuffer;
386 extern struct kmap global_map, minibuffer_map, *current_map, *base_map;
388 struct excursion {
389 int curs_x, curs_y;
390 size_t line_off;
391 struct vline *current_line;
392 size_t cpoff;
393 };
395 enum pairs {
396 PTL_BG = 1,
397 PTL_TAB,
398 PTL_CURR,
400 PBODY,
401 PBLEFT,
402 PBRIGHT,
404 PT,
405 PT_PRFX,
406 PT_TRAIL,
407 PL,
408 PL_PRFX,
409 PL_TRAIL,
410 PT1,
411 PT1_PRFX,
412 PT1_TRAIL,
413 PT2,
414 PT2_PRFX,
415 PT2_TRAIL,
416 PT3,
417 PT3_PRFX,
418 PT3_TRAIL,
419 PI,
420 PI_PRFX,
421 PI_TRAIL,
422 PQ,
423 PQ_PRFX,
424 PQ_TRAIL,
425 PPSTART,
426 PPSTART_PRFX,
427 PPSTART_TRAIL,
428 PP,
429 PP_PRFX,
430 PP_TRAIL,
431 PPEND,
432 PPEND_PRFX,
433 PPEND_TRAIL,
435 PMODELINE,
437 PMINIBUF,
438 };
440 struct thiskey {
441 short meta;
442 int key;
443 uint32_t cp;
444 };
445 extern struct thiskey thiskey;
447 extern struct histhead eecmd_history,
448 ir_history,
449 lu_history,
450 read_history;
452 struct ministate {
453 char *curmesg;
455 char prompt[64];
456 void (*donefn)(void);
457 void (*abortfn)(void);
459 char buf[1025];
460 struct line line;
461 struct vline vline;
462 struct buffer buffer;
464 struct histhead *history;
465 struct hist *hist_cur;
466 size_t hist_off;
467 };
468 extern struct ministate ministate;
470 void save_excursion(struct excursion *, struct buffer *);
471 void restore_excursion(struct excursion *, struct buffer *);
472 void global_key_unbound(void);
473 void minibuffer_taint_hist(void);
474 void eecmd_self_insert(void);
475 void eecmd_select(void);
476 void ir_self_insert(void);
477 void ir_select(void);
478 void lu_self_insert(void);
479 void lu_select(void);
480 void bp_select(void);
481 void vmessage(const char*, va_list);
482 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
483 void start_loading_anim(struct tab *tab);
484 void load_url_in_tab(struct tab *, const char *);
485 void switch_to_tab(struct tab *);
486 struct tab *current_tab(void);
487 struct buffer *current_buffer(void);
488 struct tab *new_tab(const char *);
489 unsigned int tab_new_id(void);
490 int ui_init(void);
491 void ui_on_tab_loaded(struct tab*);
492 void ui_on_tab_refresh(struct tab*);
493 const char *ui_keyname(int);
494 void ui_toggle_side_window(void);
495 void ui_schedule_redraw(void);
496 void ui_require_input(struct tab*, int);
497 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
498 void ui_yornp(const char*, void (*)(int, struct tab *), struct tab *);
499 void ui_end(void);
501 /* utf.8 */
502 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
503 size_t utf8_encode(uint32_t, char*);
504 char *utf8_nth(char*, size_t);
505 size_t utf8_cplen(char*);
506 size_t utf8_chwidth(uint32_t);
507 size_t utf8_snwidth(const char*, size_t);
508 size_t utf8_swidth(const char*);
509 size_t utf8_swidth_between(const char*, const char*);
510 char *utf8_next_cp(const char*);
511 char *utf8_prev_cp(const char*, const char*);
513 /* util.c */
514 int mark_nonblock(int);
515 int has_prefix(const char*, const char*);
516 int unicode_isspace(uint32_t);
517 int unicode_isgraph(uint32_t);
518 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
520 /* wrap.c */
521 void erase_buffer(struct buffer *);
522 void empty_linelist(struct buffer*);
523 void empty_vlist(struct buffer*);
524 int wrap_text(struct buffer*, const char*, struct line*, size_t);
525 int hardwrap_text(struct buffer*, struct line*, size_t);
527 #endif /* TELESCOPE_H */