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 struct line {
127 enum line_type type;
128 char *line;
129 char *alt;
130 int flags;
131 TAILQ_ENTRY(line) lines;
132 };
134 struct vline {
135 const struct line *parent;
136 char *line;
137 int flags;
138 TAILQ_ENTRY(vline) vlines;
139 };
141 struct parser;
142 struct page;
144 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
145 typedef int (*parserfreefn)(struct parser*);
147 typedef void (imsg_handlerfn)(struct imsg*, size_t);
149 struct parser {
150 const char *name;
151 char title[32+1];
152 char *buf;
153 size_t len;
154 size_t cap;
155 int flags;
156 parsechunkfn parse;
157 parserfreefn free;
159 TAILQ_HEAD(, line) head;
160 };
162 /*
163 * differnt types of trust for a certificate. Following
164 * gemini://thfr.info/gemini/modified-trust-verify.gmi
165 */
166 enum trust_state {
167 TS_UNKNOWN,
168 TS_UNTRUSTED,
169 TS_TRUSTED,
170 TS_VERIFIED,
171 };
173 struct tofu_entry {
174 char domain[GEMINI_URL_LEN];
176 /*
177 * enough space for ``PROTO:HASH''. probably isn't a good
178 * idea tho.
179 */
180 char hash[128+1];
181 int verified;
182 };
184 struct histhead {
185 TAILQ_HEAD(mhisthead, hist) head;
186 size_t len;
187 };
188 struct hist {
189 char h[1025];
190 TAILQ_ENTRY(hist) entries;
191 };
193 struct buffer {
194 struct parser page;
196 size_t last_line_off;
197 int force_redraw;
199 int curs_x;
200 int curs_y;
201 size_t line_off;
202 size_t line_max;
203 struct vline *current_line;
204 size_t cpoff;
205 TAILQ_HEAD(vhead, vline) head;
206 };
208 #define TAB_CURRENT 0x1
209 #define TAB_URGENT 0x2
211 #define NEW_TAB_URL "about:new"
213 extern TAILQ_HEAD(tabshead, tab) tabshead;
214 struct tab {
215 TAILQ_ENTRY(tab) tabs;
216 uint32_t id;
217 uint32_t flags;
219 char *cert;
220 enum trust_state trust;
221 struct proxy *proxy;
222 struct phos_uri uri;
223 struct histhead hist;
224 struct hist *hist_cur;
225 size_t hist_off;
227 int code;
228 char meta[GEMINI_URL_LEN];
229 int redirect_count;
231 struct buffer buffer;
233 short loading_anim;
234 short loading_anim_step;
235 struct event loadingev;
237 int fd;
238 size_t bytes;
239 char *path;
240 };
242 struct proto {
243 const char *schema;
245 /*
246 * should load the given url in the tab. Optionally, it may
247 * consider the given url as relative to the one already
248 * present in tab. It must set tab->urlstr to a serialized
249 * human-friendly URL.
250 */
251 void (*loadfn)(struct tab*, const char*);
252 };
254 extern TAILQ_HEAD(proxylist, proxy) proxies;
255 struct proxy {
256 char *match_proto;
258 char *host;
259 char *port;
260 int proto;
262 TAILQ_ENTRY(proxy) proxies;
263 };
265 enum {
266 PROTO_GEMINI,
267 /* ... */
268 };
270 struct get_req {
271 int proto;
272 char host[254];
273 char port[16];
274 char req[1027];
275 };
277 struct kmap {
278 TAILQ_HEAD(map, keymap) m;
279 void (*unhandled_input)(void);
280 };
281 extern struct kmap global_map, minibuffer_map;
283 typedef void(interactivefn)(struct buffer *);
285 struct keymap {
286 int meta;
287 int key;
288 struct kmap map;
289 interactivefn *fn;
291 TAILQ_ENTRY(keymap) keymaps;
292 };
294 struct cmd {
295 const char *cmd;
296 void (*fn)(struct buffer *);
297 };
298 extern struct cmd cmds[];
300 /* defaults.c */
301 void config_init(void);
302 int config_setprfx(const char *, const char *, const char *);
303 int config_setvari(const char *, int);
304 int config_setvars(const char *, char *);
305 int config_setcolor(int, const char *, int, int, int);
306 int config_setattr(const char *, int, int, int);
307 void config_apply_style(void);
309 /* fs.c */
310 int fs_init(void);
311 int fs_main(struct imsgbuf*);
312 int load_certs(struct ohash*);
313 int load_last_session(void(*)(const char*));
315 /* gemini.c */
316 int client_main(struct imsgbuf*);
318 /* gemtext.c */
319 void gemtext_initparser(struct parser*);
321 /* hist.c */
322 void hist_clear_forward(struct histhead*, struct hist*);
323 void hist_push(struct histhead*, struct hist*);
325 /* keymap.c */
326 int kbd(const char*);
327 const char *unkbd(int);
328 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
330 /* mime.c */
331 int setup_parser_for(struct tab*);
333 /* pages.c */
334 extern const char *about_about;
335 extern const char *about_blank;
336 extern const char *about_help;
337 extern const char *about_new;
339 #define CANNOT_FETCH 0
340 #define TOO_MUCH_REDIRECTS 1
341 #define MALFORMED_RESPONSE 2
342 #define UNKNOWN_TYPE_OR_CSET 3
343 extern const char *err_pages[70];
345 /* parse.y */
346 void parseconfig(const char *, int);
348 /* parser.c */
349 int parser_append(struct parser*, const char*, size_t);
350 int parser_set_buf(struct parser*, const char*, size_t);
351 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
353 /* sandbox.c */
354 void sandbox_network_process(void);
355 void sandbox_ui_process(void);
356 void sandbox_fs_process(void);
358 /* telescope.c */
359 void load_about_url(struct tab*, const char*);
360 void load_gemini_url(struct tab*, const char*);
361 void load_via_proxy(struct tab *, const char *, struct proxy *);
362 void load_url(struct tab*, const char*);
363 int load_previous_page(struct tab*);
364 int load_next_page(struct tab*);
365 void stop_tab(struct tab*);
366 void add_to_bookmarks(const char*);
367 void save_session(void);
369 /* textplain.c */
370 void textplain_initparser(struct parser*);
372 /* tofu.c */
373 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
374 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
375 void tofu_add(struct ohash*, struct tofu_entry*);
376 void tofu_update(struct ohash*, struct tofu_entry*);
378 /* ui.c */
379 extern int body_lines;
380 extern int body_cols;
381 extern int in_minibuffer;
383 struct excursion {
384 int curs_x, curs_y;
385 size_t line_off;
386 struct vline *current_line;
387 size_t cpoff;
388 };
390 enum pairs {
391 PTL_BG = 1,
392 PTL_TAB,
393 PTL_CURR,
395 PBODY,
396 PBLEFT,
397 PBRIGHT,
399 PT,
400 PT_PRFX,
401 PT_TRAIL,
402 PL,
403 PL_PRFX,
404 PL_TRAIL,
405 PT1,
406 PT1_PRFX,
407 PT1_TRAIL,
408 PT2,
409 PT2_PRFX,
410 PT2_TRAIL,
411 PT3,
412 PT3_PRFX,
413 PT3_TRAIL,
414 PI,
415 PI_PRFX,
416 PI_TRAIL,
417 PQ,
418 PQ_PRFX,
419 PQ_TRAIL,
420 PPSTART,
421 PPSTART_PRFX,
422 PPSTART_TRAIL,
423 PP,
424 PP_PRFX,
425 PP_TRAIL,
426 PPEND,
427 PPEND_PRFX,
428 PPEND_TRAIL,
430 PMODELINE,
432 PMINIBUF,
433 };
435 struct thiskey {
436 short meta;
437 int key;
438 uint32_t cp;
439 };
440 extern struct thiskey thiskey;
442 extern struct histhead eecmd_history,
443 ir_history,
444 lu_history,
445 read_history;
447 struct ministate {
448 char *curmesg;
450 char prompt[64];
451 void (*donefn)(void);
452 void (*abortfn)(void);
454 char buf[1025];
455 struct line line;
456 struct vline vline;
457 struct buffer buffer;
459 struct histhead *history;
460 struct hist *hist_cur;
461 size_t hist_off;
462 };
463 extern struct ministate ministate;
465 void save_excursion(struct excursion *, struct buffer *);
466 void restore_excursion(struct excursion *, struct buffer *);
467 void restore_cursor(struct buffer *);
468 void minibuffer_taint_hist(void);
469 void eecmd_self_insert(void);
470 void eecmd_select(void);
471 void ir_self_insert(void);
472 void ir_select(void);
473 void lu_self_insert(void);
474 void lu_select(void);
475 void bp_select(void);
476 void vmessage(const char*, va_list);
477 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
478 void start_loading_anim(struct tab *tab);
479 void load_url_in_tab(struct tab *, const char *);
480 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
481 void exit_minibuffer(void);
482 void switch_to_tab(struct tab *);
483 struct tab *current_tab(void);
484 struct tab *new_tab(const char *);
485 unsigned int tab_new_id(void);
486 int ui_init(int, char * const*);
487 void ui_on_tab_loaded(struct tab*);
488 void ui_on_tab_refresh(struct tab*);
489 const char *ui_keyname(int);
490 void ui_toggle_side_window(void);
491 void ui_schedule_redraw(void);
492 void ui_require_input(struct tab*, int);
493 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
494 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
495 void ui_end(void);
497 /* utf.8 */
498 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
499 size_t utf8_encode(uint32_t, char*);
500 char *utf8_nth(char*, size_t);
501 size_t utf8_cplen(char*);
502 size_t utf8_chwidth(uint32_t);
503 size_t utf8_snwidth(const char*, size_t);
504 size_t utf8_swidth(const char*);
505 size_t utf8_swidth_between(const char*, const char*);
506 char *utf8_next_cp(const char*);
507 char *utf8_prev_cp(const char*, const char*);
509 /* util.c */
510 int mark_nonblock(int);
511 int has_prefix(const char*, const char*);
512 int unicode_isspace(uint32_t);
513 int unicode_isgraph(uint32_t);
514 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
516 /* wrap.c */
517 void empty_linelist(struct buffer*);
518 void empty_vlist(struct buffer*);
519 int wrap_text(struct buffer*, const char*, struct line*, size_t);
520 int hardwrap_text(struct buffer*, struct line*, size_t);
522 #endif /* TELESCOPE_H */