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;
67 struct lineprefix {
68 const char *prfx1;
69 const char *prfx2;
70 };
71 extern struct lineprefix line_prefixes[];
73 struct line_face {
74 int prfx_pair, pair, trail_pair;
75 int prfx_bg, bg, trail_bg;
76 int prfx_fg, fg, trail_fg;
77 int prfx_attr, attr, trail_attr;
79 int prefix, text, trail;
80 };
81 extern struct line_face line_faces[];
83 struct tab_face {
84 int bg_attr, bg_bg, bg_fg;
85 int t_attr, t_bg, t_fg;
86 int c_attr, c_bg, c_fg;
88 int background, tab, current;
89 };
90 extern struct tab_face tab_face;
92 struct body_face {
93 int lbg, lfg;
94 int bg, fg;
95 int rbg, rfg;
97 int left, body, right;
98 };
99 extern struct body_face body_face;
101 struct modeline_face {
102 int bg, fg, attr;
103 int background;
104 };
105 extern struct modeline_face modeline_face;
107 struct minibuffer_face {
108 int bg, fg, attr;
109 int background;
110 };
111 extern struct minibuffer_face minibuffer_face;
113 enum line_type {
114 LINE_TEXT,
115 LINE_LINK,
116 LINE_TITLE_1,
117 LINE_TITLE_2,
118 LINE_TITLE_3,
119 LINE_ITEM,
120 LINE_QUOTE,
121 LINE_PRE_START,
122 LINE_PRE_CONTENT,
123 LINE_PRE_END,
124 };
126 /* for lines: mark as hidden */
127 #define L_HIDDEN 1
129 /* for vlines: mark as continuation */
130 #define L_CONTINUATION 2
132 struct line {
133 enum line_type type;
134 char *line;
135 char *alt;
136 int flags;
137 TAILQ_ENTRY(line) lines;
138 };
140 struct vline {
141 const struct line *parent;
142 char *line;
143 int flags;
144 TAILQ_ENTRY(vline) vlines;
145 };
147 struct parser;
148 struct page;
150 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
151 typedef int (*parserfreefn)(struct parser*);
153 typedef void (imsg_handlerfn)(struct imsg*, size_t);
155 struct parser {
156 const char *name;
157 char title[32+1];
158 char *buf;
159 size_t len;
160 size_t cap;
161 int flags;
162 parsechunkfn parse;
163 parserfreefn free;
165 TAILQ_HEAD(, line) head;
166 };
168 /*
169 * differnt types of trust for a certificate. Following
170 * gemini://thfr.info/gemini/modified-trust-verify.gmi
171 */
172 enum trust_state {
173 TS_UNKNOWN,
174 TS_UNTRUSTED,
175 TS_TRUSTED,
176 TS_VERIFIED,
177 };
179 struct tofu_entry {
180 char domain[GEMINI_URL_LEN];
182 /*
183 * enough space for ``PROTO:HASH''. probably isn't a good
184 * idea tho.
185 */
186 char hash[128+1];
187 int verified;
188 };
190 struct histhead {
191 TAILQ_HEAD(mhisthead, hist) head;
192 size_t len;
193 };
194 struct hist {
195 char h[1025];
196 TAILQ_ENTRY(hist) entries;
197 };
199 struct buffer {
200 struct parser page;
202 size_t last_line_off;
203 int force_redraw;
205 int curs_x;
206 int curs_y;
207 size_t line_off;
208 size_t line_max;
209 struct vline *top_line;
210 struct vline *current_line;
211 size_t cpoff;
212 TAILQ_HEAD(vhead, vline) head;
213 };
215 #define TAB_CURRENT 0x1
216 #define TAB_URGENT 0x2
218 #define NEW_TAB_URL "about:new"
220 extern TAILQ_HEAD(tabshead, tab) tabshead;
221 struct tab {
222 TAILQ_ENTRY(tab) tabs;
223 uint32_t id;
224 uint32_t flags;
226 char *cert;
227 enum trust_state trust;
228 struct proxy *proxy;
229 struct phos_uri uri;
230 struct histhead hist;
231 struct hist *hist_cur;
232 size_t hist_off;
234 int code;
235 char meta[GEMINI_URL_LEN];
236 int redirect_count;
238 struct buffer buffer;
240 short loading_anim;
241 short loading_anim_step;
242 struct event loadingev;
244 int fd;
245 size_t bytes;
246 char *path;
247 };
249 struct proto {
250 const char *schema;
252 /*
253 * should load the given url in the tab. Optionally, it may
254 * consider the given url as relative to the one already
255 * present in tab. It must set tab->urlstr to a serialized
256 * human-friendly URL.
257 */
258 void (*loadfn)(struct tab*, const char*);
259 };
261 extern TAILQ_HEAD(proxylist, proxy) proxies;
262 struct proxy {
263 char *match_proto;
265 char *host;
266 char *port;
267 int proto;
269 TAILQ_ENTRY(proxy) proxies;
270 };
272 enum {
273 PROTO_GEMINI,
274 /* ... */
275 };
277 struct get_req {
278 int proto;
279 char host[254];
280 char port[16];
281 char req[1027];
282 };
284 struct kmap {
285 TAILQ_HEAD(map, keymap) m;
286 void (*unhandled_input)(void);
287 };
288 extern struct kmap global_map, minibuffer_map;
290 typedef void(interactivefn)(struct buffer *);
292 struct keymap {
293 int meta;
294 int key;
295 struct kmap map;
296 interactivefn *fn;
298 TAILQ_ENTRY(keymap) keymaps;
299 };
301 struct cmd {
302 const char *cmd;
303 void (*fn)(struct buffer *);
304 };
305 extern struct cmd cmds[];
307 /* defaults.c */
308 void config_init(void);
309 int config_setprfx(const char *, const char *, const char *);
310 int config_setvari(const char *, int);
311 int config_setvars(const char *, char *);
312 int config_setcolor(int, const char *, int, int, int);
313 int config_setattr(const char *, int, int, int);
314 void config_apply_style(void);
316 /* fs.c */
317 int fs_init(void);
318 int fs_main(struct imsgbuf*);
319 int load_certs(struct ohash*);
320 int load_last_session(void(*)(const char*));
322 /* gemini.c */
323 int client_main(struct imsgbuf*);
325 /* gemtext.c */
326 void gemtext_initparser(struct parser*);
328 /* hist.c */
329 void hist_clear_forward(struct histhead*, struct hist*);
330 void hist_push(struct histhead*, struct hist*);
332 /* keymap.c */
333 int kbd(const char*);
334 const char *unkbd(int);
335 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
337 /* mime.c */
338 int setup_parser_for(struct tab*);
340 /* pages.c */
341 extern const char *about_about;
342 extern const char *about_blank;
343 extern const char *about_help;
344 extern const char *about_new;
346 #define CANNOT_FETCH 0
347 #define TOO_MUCH_REDIRECTS 1
348 #define MALFORMED_RESPONSE 2
349 #define UNKNOWN_TYPE_OR_CSET 3
350 extern const char *err_pages[70];
352 /* parse.y */
353 void parseconfig(const char *, int);
355 /* parser.c */
356 int parser_append(struct parser*, const char*, size_t);
357 int parser_set_buf(struct parser*, const char*, size_t);
358 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
360 /* sandbox.c */
361 void sandbox_network_process(void);
362 void sandbox_ui_process(void);
363 void sandbox_fs_process(void);
365 /* telescope.c */
366 void load_about_url(struct tab*, const char*);
367 void load_gemini_url(struct tab*, const char*);
368 void load_via_proxy(struct tab *, const char *, struct proxy *);
369 void load_url(struct tab*, const char*);
370 int load_previous_page(struct tab*);
371 int load_next_page(struct tab*);
372 void stop_tab(struct tab*);
373 void add_to_bookmarks(const char*);
374 void save_session(void);
376 /* textplain.c */
377 void textplain_initparser(struct parser*);
379 /* tofu.c */
380 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
381 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
382 void tofu_add(struct ohash*, struct tofu_entry*);
383 void tofu_update(struct ohash*, struct tofu_entry*);
385 /* ui.c */
386 extern int body_lines;
387 extern int body_cols;
388 extern int in_minibuffer;
390 struct excursion {
391 int curs_x, curs_y;
392 size_t line_off;
393 struct vline *current_line;
394 size_t cpoff;
395 };
397 enum pairs {
398 PTL_BG = 1,
399 PTL_TAB,
400 PTL_CURR,
402 PBODY,
403 PBLEFT,
404 PBRIGHT,
406 PT,
407 PT_PRFX,
408 PT_TRAIL,
409 PL,
410 PL_PRFX,
411 PL_TRAIL,
412 PT1,
413 PT1_PRFX,
414 PT1_TRAIL,
415 PT2,
416 PT2_PRFX,
417 PT2_TRAIL,
418 PT3,
419 PT3_PRFX,
420 PT3_TRAIL,
421 PI,
422 PI_PRFX,
423 PI_TRAIL,
424 PQ,
425 PQ_PRFX,
426 PQ_TRAIL,
427 PPSTART,
428 PPSTART_PRFX,
429 PPSTART_TRAIL,
430 PP,
431 PP_PRFX,
432 PP_TRAIL,
433 PPEND,
434 PPEND_PRFX,
435 PPEND_TRAIL,
437 PMODELINE,
439 PMINIBUF,
440 };
442 struct thiskey {
443 short meta;
444 int key;
445 uint32_t cp;
446 };
447 extern struct thiskey thiskey;
449 extern struct histhead eecmd_history,
450 ir_history,
451 lu_history,
452 read_history;
454 struct ministate {
455 char *curmesg;
457 char prompt[64];
458 void (*donefn)(void);
459 void (*abortfn)(void);
461 char buf[1025];
462 struct line line;
463 struct vline vline;
464 struct buffer buffer;
466 struct histhead *history;
467 struct hist *hist_cur;
468 size_t hist_off;
469 };
470 extern struct ministate ministate;
472 void save_excursion(struct excursion *, struct buffer *);
473 void restore_excursion(struct excursion *, struct buffer *);
474 void minibuffer_taint_hist(void);
475 void eecmd_self_insert(void);
476 void eecmd_select(void);
477 void ir_self_insert(void);
478 void ir_select(void);
479 void lu_self_insert(void);
480 void lu_select(void);
481 void bp_select(void);
482 void vmessage(const char*, va_list);
483 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
484 void start_loading_anim(struct tab *tab);
485 void load_url_in_tab(struct tab *, const char *);
486 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
487 void exit_minibuffer(void);
488 void switch_to_tab(struct tab *);
489 struct tab *current_tab(void);
490 struct tab *new_tab(const char *);
491 unsigned int tab_new_id(void);
492 int ui_init(int, char * const*);
493 void ui_on_tab_loaded(struct tab*);
494 void ui_on_tab_refresh(struct tab*);
495 const char *ui_keyname(int);
496 void ui_toggle_side_window(void);
497 void ui_schedule_redraw(void);
498 void ui_require_input(struct tab*, int);
499 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
500 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
501 void ui_end(void);
503 /* utf.8 */
504 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
505 size_t utf8_encode(uint32_t, char*);
506 char *utf8_nth(char*, size_t);
507 size_t utf8_cplen(char*);
508 size_t utf8_chwidth(uint32_t);
509 size_t utf8_snwidth(const char*, size_t);
510 size_t utf8_swidth(const char*);
511 size_t utf8_swidth_between(const char*, const char*);
512 char *utf8_next_cp(const char*);
513 char *utf8_prev_cp(const char*, const char*);
515 /* util.c */
516 int mark_nonblock(int);
517 int has_prefix(const char*, const char*);
518 int unicode_isspace(uint32_t);
519 int unicode_isgraph(uint32_t);
520 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
522 /* wrap.c */
523 void erase_buffer(struct buffer *);
524 void empty_linelist(struct buffer*);
525 void empty_vlist(struct buffer*);
526 int wrap_text(struct buffer*, const char*, struct line*, size_t);
527 int hardwrap_text(struct buffer*, struct line*, size_t);
529 #endif /* TELESCOPE_H */