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_ERR,
35 IMSG_CHECK_CERT,
36 IMSG_CERT_STATUS,
37 IMSG_GOT_CODE,
38 IMSG_GOT_META,
39 IMSG_PROCEED,
40 IMSG_STOP,
41 IMSG_BUF,
42 IMSG_EOF,
43 IMSG_QUIT,
45 /* ui <-> fs */
46 IMSG_BOOKMARK_PAGE,
47 IMSG_BOOKMARK_OK,
48 IMSG_SAVE_CERT,
49 IMSG_SAVE_CERT_OK,
50 IMSG_UPDATE_CERT,
51 IMSG_UPDATE_CERT_OK,
53 IMSG_FILE_OPEN,
54 IMSG_FILE_OPENED,
56 IMSG_SESSION_START,
57 IMSG_SESSION_TAB,
58 IMSG_SESSION_END,
59 };
61 extern char *new_tab_url;
62 extern int fill_column;
63 extern int olivetti_mode;
64 extern int enable_colors;
66 struct lineprefix {
67 const char *prfx1;
68 const char *prfx2;
69 };
70 extern struct lineprefix line_prefixes[];
72 struct line_face {
73 int prefix_prop;
74 int text_prop;
75 int trail_prop;
76 };
77 extern struct line_face line_faces[];
79 struct tab_face {
80 int background, tab, current_tab;
81 };
82 extern struct tab_face tab_face;
84 struct modeline_face {
85 int background;
86 };
87 extern struct modeline_face modeline_face;
89 struct minibuffer_face {
90 int background;
91 };
92 extern struct minibuffer_face minibuffer_face;
94 enum line_type {
95 LINE_TEXT,
96 LINE_LINK,
97 LINE_TITLE_1,
98 LINE_TITLE_2,
99 LINE_TITLE_3,
100 LINE_ITEM,
101 LINE_QUOTE,
102 LINE_PRE_START,
103 LINE_PRE_CONTENT,
104 LINE_PRE_END,
105 };
107 struct line {
108 enum line_type type;
109 char *line;
110 char *alt;
111 int flags;
112 TAILQ_ENTRY(line) lines;
113 };
115 struct vline {
116 const struct line *parent;
117 char *line;
118 int flags;
119 TAILQ_ENTRY(vline) vlines;
120 };
122 struct parser;
123 struct page;
125 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
126 typedef int (*parserfreefn)(struct parser*);
128 typedef void (imsg_handlerfn)(struct imsg*, size_t);
130 struct parser {
131 const char *name;
132 char title[32+1];
133 char *buf;
134 size_t len;
135 size_t cap;
136 int flags;
137 parsechunkfn parse;
138 parserfreefn free;
140 TAILQ_HEAD(, line) head;
141 };
143 /*
144 * differnt types of trust for a certificate. Following
145 * gemini://thfr.info/gemini/modified-trust-verify.gmi
146 */
147 enum trust_state {
148 TS_UNKNOWN,
149 TS_UNTRUSTED,
150 TS_TRUSTED,
151 TS_VERIFIED,
152 };
154 struct tofu_entry {
155 char domain[GEMINI_URL_LEN];
157 /*
158 * enough space for ``PROTO:HASH''. probably isn't a good
159 * idea tho.
160 */
161 char hash[128+1];
162 int verified;
163 };
165 struct histhead {
166 TAILQ_HEAD(mhisthead, hist) head;
167 size_t len;
168 };
169 struct hist {
170 char h[1025];
171 TAILQ_ENTRY(hist) entries;
172 };
174 struct buffer {
175 struct parser page;
177 size_t last_line_off;
178 int force_redraw;
180 int curs_x;
181 int curs_y;
182 size_t line_off;
183 size_t line_max;
184 struct vline *current_line;
185 size_t cpoff;
186 TAILQ_HEAD(vhead, vline) head;
187 };
189 #define TAB_CURRENT 0x1
190 #define TAB_URGENT 0x2
192 #define NEW_TAB_URL "about:new"
194 extern TAILQ_HEAD(tabshead, tab) tabshead;
195 struct tab {
196 TAILQ_ENTRY(tab) tabs;
197 uint32_t id;
198 uint32_t flags;
200 char *cert;
201 enum trust_state trust;
202 struct phos_uri uri;
203 struct histhead hist;
204 struct hist *hist_cur;
205 size_t hist_off;
207 int code;
208 char meta[GEMINI_URL_LEN];
209 int redirect_count;
211 struct buffer buffer;
213 short loading_anim;
214 short loading_anim_step;
215 struct event loadingev;
217 int fd;
218 size_t bytes;
219 char *path;
220 };
222 struct proto {
223 const char *schema;
225 /*
226 * should load the given url in the tab. Optionally, it may
227 * consider the given url as relative to the one already
228 * present in tab. It must set tab->urlstr to a serialized
229 * human-friendly URL.
230 */
231 void (*loadfn)(struct tab*, const char*);
232 };
234 struct kmap {
235 TAILQ_HEAD(map, keymap) m;
236 void (*unhandled_input)(void);
237 };
239 struct keymap {
240 int meta;
241 int key;
242 struct kmap map;
243 void (*fn)(struct buffer*);
245 TAILQ_ENTRY(keymap) keymaps;
246 };
248 /* defaults.c */
249 int config_setprfx(const char *, const char *, const char *);
250 int config_setvari(const char *, int);
251 int config_setvars(const char *, char *);
252 int config_setcolor(int, const char *, int, int, int);
253 void config_apply_colors(void);
255 /* fs.c */
256 int fs_init(void);
257 int fs_main(struct imsgbuf*);
258 int load_certs(struct ohash*);
259 int load_last_session(void(*)(const char*));
261 /* gemini.c */
262 int client_main(struct imsgbuf*);
264 /* gemtext.c */
265 void gemtext_initparser(struct parser*);
267 /* hist.c */
268 void hist_clear_forward(struct histhead*, struct hist*);
269 void hist_push(struct histhead*, struct hist*);
271 /* keymap.c */
272 int kbd(const char*);
273 const char *unkbd(int);
274 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
276 /* mime.c */
277 int setup_parser_for(struct tab*);
279 /* pages.c */
280 extern const char *about_about;
281 extern const char *about_blank;
282 extern const char *about_help;
283 extern const char *about_new;
285 #define CANNOT_FETCH 0
286 #define TOO_MUCH_REDIRECTS 1
287 #define MALFORMED_RESPONSE 2
288 #define UNKNOWN_TYPE_OR_CSET 3
289 extern const char *err_pages[70];
291 /* parse.y */
292 void parseconfig(const char *, int);
294 /* parser.c */
295 int parser_append(struct parser*, const char*, size_t);
296 int parser_set_buf(struct parser*, const char*, size_t);
297 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
299 /* sandbox.c */
300 void sandbox_network_process(void);
301 void sandbox_ui_process(void);
302 void sandbox_fs_process(void);
304 /* telescope.c */
305 void load_about_url(struct tab*, const char*);
306 void load_gemini_url(struct tab*, const char*);
307 void load_url(struct tab*, const char*);
308 int load_previous_page(struct tab*);
309 int load_next_page(struct tab*);
310 void stop_tab(struct tab*);
311 void add_to_bookmarks(const char*);
312 void save_session(void);
314 /* textplain.c */
315 void textplain_initparser(struct parser*);
317 /* tofu.c */
318 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
319 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
320 void tofu_add(struct ohash*, struct tofu_entry*);
321 void tofu_update(struct ohash*, struct tofu_entry*);
323 /* ui.c */
324 extern int body_lines;
325 extern int body_cols;
326 extern int in_minibuffer;
328 enum pairs {
329 PT = 1,
330 PT_PRFX,
331 PT_TRAIL,
332 PL,
333 PL_PRFX,
334 PL_TRAIL,
335 PT1,
336 PT1_PRFX,
337 PT1_TRAIL,
338 PT2,
339 PT2_PRFX,
340 PT2_TRAIL,
341 PT3,
342 PT3_PRFX,
343 PT3_TRAIL,
344 PI,
345 PI_PRFX,
346 PI_TRAIL,
347 PQ,
348 PQ_PRFX,
349 PQ_TRAIL,
350 PPSTART,
351 PPSTART_PRFX,
352 PPSTART_TRAIL,
353 PP,
354 PP_PRFX,
355 PP_TRAIL,
356 PPEND,
357 PPEND_PRFX,
358 PPEND_TRAIL,
359 };
361 struct thiskey {
362 short meta;
363 int key;
364 uint32_t cp;
365 };
366 extern struct thiskey thiskey;
368 extern struct histhead eecmd_history,
369 ir_history,
370 lu_history,
371 read_history;
373 struct ministate {
374 char *curmesg;
376 char prompt[64];
377 void (*donefn)(void);
378 void (*abortfn)(void);
380 char buf[1025];
381 struct line line;
382 struct vline vline;
383 struct buffer buffer;
385 struct histhead *history;
386 struct hist *hist_cur;
387 size_t hist_off;
388 };
389 extern struct ministate ministate;
391 void restore_cursor(struct buffer *);
392 void minibuffer_taint_hist(void);
393 void eecmd_self_insert(void);
394 void eecmd_select(void);
395 void ir_self_insert(void);
396 void ir_select(void);
397 void lu_self_insert(void);
398 void lu_select(void);
399 void bp_select(void);
400 void vmessage(const char*, va_list);
401 void message(const char*, ...) __attribute__((format(printf, 1, 2)));
402 void start_loading_anim(struct tab *tab);
403 void load_url_in_tab(struct tab *, const char *);
404 void enter_minibuffer(void(*)(void), void(*)(void), void(*)(void), struct histhead *);
405 void exit_minibuffer(void);
406 void switch_to_tab(struct tab *);
407 struct tab *current_tab(void);
408 struct tab *new_tab(const char *);
409 unsigned int tab_new_id(void);
410 int ui_init(int, char * const*);
411 void ui_on_tab_loaded(struct tab*);
412 void ui_on_tab_refresh(struct tab*);
413 const char *ui_keyname(int);
414 void ui_toggle_side_window(void);
415 void ui_schedule_redraw(void);
416 void ui_require_input(struct tab*, int);
417 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
418 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
419 void ui_end(void);
421 /* utf.8 */
422 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
423 size_t utf8_encode(uint32_t, char*);
424 char *utf8_nth(char*, size_t);
425 size_t utf8_cplen(char*);
426 size_t utf8_chwidth(uint32_t);
427 size_t utf8_snwidth(const char*, size_t);
428 size_t utf8_swidth(const char*);
429 size_t utf8_swidth_between(const char*, const char*);
430 char *utf8_next_cp(const char*);
431 char *utf8_prev_cp(const char*, const char*);
433 /* util.c */
434 int mark_nonblock(int);
435 int has_prefix(const char*, const char*);
436 int unicode_isspace(uint32_t);
437 int unicode_isgraph(uint32_t);
438 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
440 /* wrap.c */
441 void empty_linelist(struct buffer*);
442 void empty_vlist(struct buffer*);
443 int wrap_text(struct buffer*, const char*, struct line*, size_t);
444 int hardwrap_text(struct buffer*, struct line*, size_t);
446 #endif /* TELESCOPE_H */