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