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