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;
65 struct lineprefix {
66 const char *prfx1;
67 const char *prfx2;
68 };
69 extern struct lineprefix line_prefixes[];
71 struct line_face {
72 int prefix_prop;
73 int text_prop;
74 };
75 extern struct line_face line_faces[];
77 struct tab_face {
78 int background, tab, current_tab;
79 };
80 extern struct tab_face tab_face;
82 struct modeline_face {
83 int background;
84 };
85 extern struct modeline_face modeline_face;
87 struct minibuffer_face {
88 int background;
89 };
90 extern struct minibuffer_face minibuffer_face;
92 enum line_type {
93 LINE_TEXT,
94 LINE_LINK,
95 LINE_TITLE_1,
96 LINE_TITLE_2,
97 LINE_TITLE_3,
98 LINE_ITEM,
99 LINE_QUOTE,
100 LINE_PRE_START,
101 LINE_PRE_CONTENT,
102 LINE_PRE_END,
103 };
105 struct line {
106 enum line_type type;
107 char *line;
108 char *alt;
109 int flags;
110 TAILQ_ENTRY(line) lines;
111 };
113 struct vline {
114 const struct line *parent;
115 char *line;
116 int flags;
117 TAILQ_ENTRY(vline) vlines;
118 };
120 struct parser;
121 struct page;
123 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
124 typedef int (*parserfreefn)(struct parser*);
126 typedef void (imsg_handlerfn)(struct imsg*, size_t);
128 struct parser {
129 const char *name;
130 char title[32+1];
131 char *buf;
132 size_t len;
133 size_t cap;
134 int flags;
135 parsechunkfn parse;
136 parserfreefn free;
138 TAILQ_HEAD(, line) head;
139 };
141 /*
142 * differnt types of trust for a certificate. Following
143 * gemini://thfr.info/gemini/modified-trust-verify.gmi
144 */
145 enum trust_state {
146 TS_UNKNOWN,
147 TS_UNTRUSTED,
148 TS_TRUSTED,
149 TS_VERIFIED,
150 };
152 struct tofu_entry {
153 char domain[GEMINI_URL_LEN];
155 /*
156 * enough space for ``PROTO:HASH''. probably isn't a good
157 * idea tho.
158 */
159 char hash[128+1];
160 int verified;
161 };
163 struct histhead {
164 TAILQ_HEAD(mhisthead, hist) head;
165 size_t len;
166 };
167 struct hist {
168 char h[1025];
169 TAILQ_ENTRY(hist) entries;
170 };
172 struct buffer {
173 struct parser page;
175 size_t last_line_off;
176 int force_redraw;
178 int curs_x;
179 int curs_y;
180 size_t line_off;
181 size_t line_max;
182 struct vline *current_line;
183 size_t cpoff;
184 TAILQ_HEAD(vhead, vline) head;
185 };
187 extern TAILQ_HEAD(tabshead, tab) tabshead;
188 struct tab {
189 TAILQ_ENTRY(tab) tabs;
190 uint32_t id;
191 uint32_t flags;
193 char *cert;
194 enum trust_state trust;
195 struct phos_uri uri;
196 struct histhead hist;
197 struct hist *hist_cur;
198 size_t hist_off;
200 int code;
201 char meta[GEMINI_URL_LEN];
202 int redirect_count;
204 struct buffer buffer;
206 short loading_anim;
207 short loading_anim_step;
208 struct event loadingev;
210 int fd;
211 size_t bytes;
212 char *path;
213 };
215 struct proto {
216 const char *schema;
218 /*
219 * should load the given url in the tab. Optionally, it may
220 * consider the given url as relative to the one already
221 * present in tab. It must set tab->urlstr to a serialized
222 * human-friendly URL.
223 */
224 void (*loadfn)(struct tab*, const char*);
225 };
227 struct kmap {
228 TAILQ_HEAD(map, keymap) m;
229 void (*unhandled_input)(void);
230 };
232 struct keymap {
233 int meta;
234 int key;
235 struct kmap map;
236 void (*fn)(struct buffer*);
238 TAILQ_ENTRY(keymap) keymaps;
239 };
241 /* defaults.c */
242 int config_setprfx(const char *, int, const char *);
243 int config_setvari(const char *, int);
244 int config_setvars(const char *, char *);
246 /* fs.c */
247 int fs_init(void);
248 int fs_main(struct imsgbuf*);
249 int load_certs(struct ohash*);
250 int load_last_session(void(*)(const char*));
252 /* gemini.c */
253 int client_main(struct imsgbuf*);
255 /* gemtext.c */
256 void gemtext_initparser(struct parser*);
258 /* hist.c */
259 void hist_clear_forward(struct histhead*, struct hist*);
260 void hist_push(struct histhead*, struct hist*);
262 /* keymap.c */
263 int kbd(const char*);
264 const char *unkbd(int);
265 int kmap_define_key(struct kmap*, const char*, void(*)(struct buffer*));
267 /* mime.c */
268 int setup_parser_for(struct tab*);
270 /* pages.c */
271 extern const char *about_about;
272 extern const char *about_blank;
273 extern const char *about_help;
274 extern const char *about_new;
276 #define CANNOT_FETCH 0
277 #define TOO_MUCH_REDIRECTS 1
278 #define MALFORMED_RESPONSE 2
279 #define UNKNOWN_TYPE_OR_CSET 3
280 extern const char *err_pages[70];
282 /* parse.y */
283 void parseconfig(const char *, int);
285 /* parser.c */
286 int parser_append(struct parser*, const char*, size_t);
287 int parser_set_buf(struct parser*, const char*, size_t);
288 int parser_foreach_line(struct parser*, const char*, size_t, parsechunkfn);
290 /* sandbox.c */
291 void sandbox_network_process(void);
292 void sandbox_ui_process(void);
293 void sandbox_fs_process(void);
295 /* telescope.c */
296 void load_about_url(struct tab*, const char*);
297 void load_gemini_url(struct tab*, const char*);
298 void load_url(struct tab*, const char*);
299 int load_previous_page(struct tab*);
300 int load_next_page(struct tab*);
301 void stop_tab(struct tab*);
302 void add_to_bookmarks(const char*);
303 void save_session(void);
305 /* textplain.c */
306 void textplain_initparser(struct parser*);
308 /* tofu.c */
309 void tofu_init(struct ohash*, unsigned int, ptrdiff_t);
310 struct tofu_entry *tofu_lookup(struct ohash*, const char*, const char*);
311 void tofu_add(struct ohash*, struct tofu_entry*);
312 void tofu_update(struct ohash*, struct tofu_entry*);
314 /* ui.c */
315 unsigned int tab_new_id(void);
316 int ui_init(int, char * const*);
317 void ui_on_tab_loaded(struct tab*);
318 void ui_on_tab_refresh(struct tab*);
319 void ui_require_input(struct tab*, int);
320 void ui_read(const char*, void(*)(const char*, unsigned int), unsigned int);
321 void ui_yornp(const char*, void (*)(int, unsigned int), unsigned int);
322 void ui_notify(const char*, ...) __attribute__((format(printf, 1, 2)));
323 void ui_end(void);
325 /* utf.8 */
326 uint32_t utf8_decode(uint32_t*restrict, uint32_t*restrict, uint8_t);
327 size_t utf8_encode(uint32_t, char*);
328 char *utf8_nth(char*, size_t);
329 size_t utf8_cplen(char*);
330 size_t utf8_chwidth(uint32_t);
331 size_t utf8_snwidth(const char*, size_t);
332 size_t utf8_swidth(const char*);
333 size_t utf8_swidth_between(const char*, const char*);
334 char *utf8_next_cp(const char*);
335 char *utf8_prev_cp(const char*, const char*);
337 /* util.c */
338 int mark_nonblock(int);
339 int has_prefix(const char*, const char*);
340 int unicode_isspace(uint32_t);
341 int unicode_isgraph(uint32_t);
342 void dispatch_imsg(struct imsgbuf*, imsg_handlerfn**, size_t);
344 /* wrap.c */
345 void empty_linelist(struct buffer*);
346 void empty_vlist(struct buffer*);
347 int wrap_text(struct buffer*, const char*, struct line*, size_t);
348 int hardwrap_text(struct buffer*, struct line*, size_t);
350 #endif /* TELESCOPE_H */