Blob


1 /*
2 * Copyright (c) 2021, 2024 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 <limits.h>
21 #include <stdio.h> /* XXX: for parsers.h */
23 #include "iri.h"
25 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26 #define MAX(a, b) ((a) > (b) ? (a) : (b))
28 #define GEMINI_URL_LEN 1024
29 #define TITLE_MAX 128+1 /* account for NUL too */
31 #define SIDE_WINDOW_LEFT 0x1
32 #define SIDE_WINDOW_BOTTOM 0x2
34 enum line_type {
35 /* text/gemini */
36 LINE_TEXT,
37 LINE_LINK,
38 LINE_TITLE_1,
39 LINE_TITLE_2,
40 LINE_TITLE_3,
41 LINE_ITEM,
42 LINE_QUOTE,
43 LINE_PRE_START,
44 LINE_PRE_CONTENT,
45 LINE_PRE_END,
47 /* text/x-patch */
48 LINE_PATCH,
49 LINE_PATCH_HDR,
50 LINE_PATCH_HUNK_HDR,
51 LINE_PATCH_ADD,
52 LINE_PATCH_DEL,
54 /* minibuffer */
55 LINE_COMPL,
56 LINE_COMPL_CURRENT,
58 /* help */
59 LINE_HELP,
61 /* download */
62 LINE_DOWNLOAD,
63 LINE_DOWNLOAD_DONE,
64 LINE_DOWNLOAD_INFO,
66 /* misc ui */
67 LINE_FRINGE,
68 };
70 struct line {
71 enum line_type type;
72 char *line;
73 char *alt;
74 void *data;
76 #define L_HIDDEN 0x1
77 int flags;
78 TAILQ_ENTRY(line) lines;
79 };
81 struct vline {
82 struct line *parent;
83 size_t from;
84 size_t len;
85 size_t cplen;
87 #define L_CONTINUATION 0x2
88 int flags;
89 TAILQ_ENTRY(vline) vlines;
90 };
92 struct parser;
94 typedef int (*printfn)(void *, const char *, ...);
96 typedef void (*parserinit)(struct parser *);
98 typedef int (*parsechunkfn)(struct parser *, const char *, size_t);
99 typedef int (*parserfreefn)(struct parser *);
100 typedef int (*parserserial)(struct parser *, FILE *);
102 struct parser {
103 const char *name;
104 char title[128+1];
105 char *buf;
106 size_t len;
107 size_t cap;
109 #define PARSER_IN_BODY 1
110 #define PARSER_IN_PRE 2
111 #define PARSER_IN_PATCH_HDR 4
112 int flags;
113 parserinit init;
114 parsechunkfn parse;
115 parserfreefn free;
116 parserserial serialize;
118 TAILQ_HEAD(, line) head;
119 };
121 /*
122 * differnt types of trust for a certificate. Following
123 * gemini://thfr.info/gemini/modified-trust-verify.gmi
124 */
125 enum trust_state {
126 TS_UNKNOWN,
127 TS_UNTRUSTED,
128 TS_TEMP_TRUSTED,
129 TS_TRUSTED,
130 TS_VERIFIED,
131 };
133 struct buffer {
134 struct parser page;
136 size_t last_line_off;
137 int force_redraw;
139 int curs_x;
140 int curs_y;
141 size_t line_off;
142 size_t line_max;
143 struct vline *top_line;
144 struct vline *current_line;
145 size_t cpoff;
146 TAILQ_HEAD(vhead, vline) head;
147 };
149 #define TAB_CURRENT 0x1 /* only for save_session */
150 #define TAB_KILLED 0x2 /* only for save_session */
151 #define TAB_URGENT 0x4
152 #define TAB_LAZY 0x8 /* to lazy load tabs */
154 #define NEW_TAB_URL "about:new"
156 struct hist;
158 TAILQ_HEAD(tabshead, tab);
159 extern struct tabshead tabshead;
160 extern struct tabshead ktabshead;
161 struct tab {
162 TAILQ_ENTRY(tab) tabs;
163 uint32_t id;
164 uint32_t flags;
166 char *cert;
167 enum trust_state trust;
168 const char *client_cert;
169 int client_cert_temp;
170 struct proxy *proxy;
171 struct iri iri;
172 struct hist *hist;
173 char *last_input_url;
175 int code;
176 char meta[GEMINI_URL_LEN];
177 int redirect_count;
179 struct buffer buffer;
181 short loading_anim;
182 short loading_anim_step;
183 unsigned long loading_timer;
184 };
186 extern TAILQ_HEAD(proxylist, proxy) proxies;
187 struct proxy {
188 char *match_proto;
190 char *host;
191 char *port;
192 int proto;
194 TAILQ_ENTRY(proxy) proxies;
195 };
197 enum {
198 PROTO_FINGER,
199 PROTO_GEMINI,
200 PROTO_GOPHER,
201 /* ... */
202 };
204 struct get_req {
205 int proto;
206 char host[254];
207 char port[16];
208 char req[1027];
209 };
211 /* downloads.c */
212 extern STAILQ_HEAD(downloads, download) downloads;
213 struct download {
214 uint32_t id;
215 int fd;
216 size_t bytes;
217 char *path;
218 STAILQ_ENTRY(download) entries;
219 };
221 void recompute_downloads(void);
222 struct download *enqueue_download(uint32_t, const char *);
223 struct download *download_by_id(uint32_t);
225 /* help.c */
226 void recompute_help(void);
228 /* mime.c */
229 int setup_parser_for(struct tab*);
231 /* net.c */
232 int net_main(void);
234 /* parse.y */
235 void parseconfig(const char *, int);
237 /* sandbox.c */
238 void sandbox_net_process(void);
239 void sandbox_ui_process(void);
241 /* telescope.c */
242 extern int operating;
243 extern int safe_mode;
245 #define LU_MODE_NONE 0x0
246 #define LU_MODE_NOHIST 0x1
247 #define LU_MODE_NOCACHE 0x2
249 void gopher_send_search_req(struct tab *, const char *);
250 void load_page_from_str(struct tab *, const char *);
251 void load_url(struct tab *, const char *, const char *, int);
252 void load_url_in_tab(struct tab *, const char *, const char *, int);
253 int load_previous_page(struct tab*);
254 int load_next_page(struct tab*);
255 void write_buffer(const char *, struct tab *);
256 void humanify_url(const char *, const char *, char *, size_t);
257 int bookmark_page(const char *);
258 int ui_send_net(int, uint32_t, int, const void *, uint16_t);
260 /* wrap.c */
261 void erase_buffer(struct buffer *);
262 void empty_linelist(struct buffer*);
263 void empty_vlist(struct buffer*);
264 int wrap_text(struct buffer*, const char*, struct line*, size_t, int);
265 int wrap_page(struct buffer *, int width);
267 #endif /* TELESCOPE_H */