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 MIN(a, b) ((a) < (b) ? (a) : (b))
25 #define MAX(a, b) ((a) > (b) ? (a) : (b))
27 #define GEMINI_URL_LEN 1024
29 enum imsg_type {
30 IMSG_GET, /* data is URL, peerid the tab id */
31 IMSG_ERR,
32 IMSG_CHECK_CERT,
33 IMSG_CERT_STATUS,
34 IMSG_GOT_CODE,
35 IMSG_GOT_META,
36 IMSG_PROCEED,
37 IMSG_STOP,
38 IMSG_BUF,
39 IMSG_EOF,
40 IMSG_QUIT,
41 };
43 enum line_type {
44 LINE_TEXT,
45 LINE_LINK,
46 LINE_TITLE_1,
47 LINE_TITLE_2,
48 LINE_TITLE_3,
49 LINE_ITEM,
50 LINE_QUOTE,
51 LINE_PRE_START,
52 LINE_PRE_CONTENT,
53 LINE_PRE_END,
54 };
56 struct line {
57 enum line_type type;
58 char *line;
59 char *alt;
60 int flags;
61 TAILQ_ENTRY(line) lines;
62 };
64 struct parser;
65 struct page;
67 /* typedef void (*initparserfn)(struct parser*); */
69 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
70 typedef int (*parserfreefn)(struct parser*);
72 typedef void (imsg_handlerfn)(struct imsg*, size_t);
74 struct parser {
75 const char *name;
76 char title[32+1];
77 char *buf;
78 size_t len;
79 size_t cap;
80 int flags;
81 parsechunkfn parse;
82 parserfreefn free;
84 TAILQ_HEAD(, line) head;
85 };
87 struct histhead {
88 TAILQ_HEAD(mhisthead, hist) head;
89 size_t len;
90 };
91 struct hist {
92 char h[1025];
93 TAILQ_ENTRY(hist) entries;
94 };
96 struct ui_state;
98 extern TAILQ_HEAD(tabshead, tab) tabshead;
99 struct tab {
100 struct parser page;
101 TAILQ_ENTRY(tab) tabs;
102 uint32_t id;
103 uint32_t flags;
105 struct url url;
106 struct histhead hist;
107 struct hist *hist_cur;
108 size_t hist_off;
110 int code;
111 char meta[GEMINI_URL_LEN];
112 int redirect_count;
114 struct ui_state *s;
115 };
117 struct proto {
118 const char *schema;
120 /* should load the given url in the tab. Optionally, it may
121 * consider the given url as relative to the one already
122 * present in tab. It must set tab->urlstr to a serialized
123 * human-friendly URL. */
124 void (*loadfn)(struct tab*, const char*);
125 };
127 struct kmap {
128 TAILQ_HEAD(map, keymap) m;
129 void (*unhandled_input)(void);
130 };
132 struct keymap {
133 int meta;
134 int key;
135 struct kmap map;
136 void (*fn)(struct tab*);
138 TAILQ_ENTRY(keymap) keymaps;
139 };
141 extern struct event imsgev;
143 /* gemini.c */
144 int client_main(struct imsgbuf *b);
146 /* gemtext.c */
147 void gemtext_initparser(struct parser*);
149 /* hist.c */
150 void hist_clear_forward(struct histhead*, struct hist*);
151 void hist_push(struct histhead*, struct hist*);
153 /* keymap.c */
154 int kbd(const char*);
155 const char *unkbd(int);
156 int kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
158 /* mime.c */
159 int setup_parser_for(struct tab*);
161 /* pages.c */
162 extern const char *about_new;
164 #define CANNOT_FETCH 0
165 #define TOO_MUCH_REDIRECTS 1
166 #define MALFORMED_RESPONSE 2
167 #define UNKNOWN_TYPE_OR_CSET 3
168 extern const char *err_pages[70];
170 /* parser.c */
171 int parser_append(struct parser*, const char*, size_t);
172 int parser_set_buf(struct parser*, const char*, size_t);
174 /* sandbox.c */
175 void sandbox_network_process(void);
177 /* telescope.c */
178 void load_about_url(struct tab*, const char*);
179 void load_gemini_url(struct tab*, const char*);
180 void load_url(struct tab*, const char*);
181 int load_previous_page(struct tab*);
182 int load_next_page(struct tab*);
183 void stop_tab(struct tab*);
185 /* textplain.c */
186 void textplain_initparser(struct parser*);
188 /* ui.c */
189 int ui_init(void);
190 void ui_on_tab_loaded(struct tab*);
191 void ui_on_tab_refresh(struct tab*);
192 void ui_require_input(struct tab*, int);
193 void ui_end(void);
195 /* util.c */
196 int mark_nonblock(int);
197 char *telescope_strnchr(char*, char, size_t);
198 int has_prefix(const char*, const char*);
200 #endif /* TELESCOPE_H */