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 "compat.h"
22 #include "url.h"
24 #define GEMINI_URL_LEN 1024
26 enum imsg_type {
27 IMSG_GET, /* data is URL, peerid the tab id */
28 IMSG_ERR,
29 IMSG_CHECK_CERT,
30 IMSG_CERT_STATUS,
31 IMSG_GOT_CODE,
32 IMSG_GOT_META,
33 IMSG_PROCEED,
34 IMSG_STOP,
35 IMSG_BUF,
36 IMSG_EOF,
37 IMSG_QUIT,
38 };
40 enum line_type {
41 LINE_TEXT,
42 LINE_LINK,
43 LINE_TITLE_1,
44 LINE_TITLE_2,
45 LINE_TITLE_3,
46 LINE_ITEM,
47 LINE_QUOTE,
48 LINE_PRE_START,
49 LINE_PRE_CONTENT,
50 LINE_PRE_END,
51 };
53 struct line {
54 enum line_type type;
55 char *line;
56 char *alt;
57 int flags;
58 TAILQ_ENTRY(line) lines;
59 };
61 struct parser;
62 struct page;
64 /* typedef void (*initparserfn)(struct parser*); */
66 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
67 typedef int (*parserfreefn)(struct parser*);
69 typedef void (imsg_handlerfn)(struct imsg*, size_t);
71 struct parser {
72 const char *name;
73 char *buf;
74 size_t len;
75 size_t cap;
76 int flags;
77 parsechunkfn parse;
78 parserfreefn free;
80 TAILQ_HEAD(, line) head;
81 };
83 struct ui_state;
85 extern TAILQ_HEAD(tabshead, tab) tabshead;
86 struct tab {
87 struct parser page;
88 TAILQ_ENTRY(tab) tabs;
89 uint32_t id;
90 uint32_t flags;
92 char urlstr[GEMINI_URL_LEN];
93 struct url url;
95 int code;
96 char meta[GEMINI_URL_LEN];
97 int redirect_count;
99 struct ui_state *s;
100 };
102 struct proto {
103 const char *schema;
105 /* should load the given url in the tab. Optionally, it may
106 * consider the given url as relative to the one already
107 * present in tab. It must set tab->urlstr to a serialized
108 * human-friendly URL. */
109 void (*loadfn)(struct tab*, const char*);
110 };
112 struct kmap {
113 TAILQ_HEAD(map, keymap) m;
114 void (*unhandled_input)(void);
115 };
117 struct keymap {
118 int meta;
119 int key;
120 struct kmap map;
121 void (*fn)(struct tab*);
123 TAILQ_ENTRY(keymap) keymaps;
124 };
126 extern struct event imsgev;
128 /* gemini.c */
129 int client_main(struct imsgbuf *b);
131 /* gemtext.c */
132 void gemtext_initparser(struct parser*);
134 /* keymap.c */
135 int kbd(const char*);
136 const char *unkbd(int);
137 int kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
139 /* mime.c */
140 int setup_parser_for(struct tab*);
142 /* pages.c */
143 extern const char *about_new;
145 #define CANNOT_FETCH 0
146 #define TOO_MUCH_REDIRECTS 1
147 #define MALFORMED_RESPONSE 2
148 #define UNKNOWN_TYPE_OR_CSET 3
149 extern const char *err_pages[70];
151 /* parser.c */
152 int parser_append(struct parser*, const char*, size_t);
153 int parser_set_buf(struct parser*, const char*, size_t);
155 /* sandbox.c */
156 void sandbox_network_process(void);
158 /* telescope.c */
159 void load_about_url(struct tab*, const char*);
160 void load_gemini_url(struct tab*, const char*);
161 void load_url(struct tab*, const char*);
162 void stop_tab(struct tab*);
164 /* textplain.c */
165 void textplain_initparser(struct parser*);
167 /* ui.c */
168 int ui_init(void);
169 void ui_on_tab_loaded(struct tab*);
170 void ui_on_tab_refresh(struct tab*);
171 void ui_require_input(struct tab*, int);
172 void ui_end(void);
174 /* util.c */
175 int mark_nonblock(int);
176 char *telescope_strnchr(char*, char, size_t);
177 int has_prefix(const char*, const char*);
179 #endif /* TELESCOPE_H */