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 ui_state;
89 extern TAILQ_HEAD(tabshead, tab) tabshead;
90 struct tab {
91 struct parser page;
92 TAILQ_ENTRY(tab) tabs;
93 uint32_t id;
94 uint32_t flags;
96 char urlstr[GEMINI_URL_LEN];
97 struct url url;
99 int code;
100 char meta[GEMINI_URL_LEN];
101 int redirect_count;
103 struct ui_state *s;
104 };
106 struct proto {
107 const char *schema;
109 /* should load the given url in the tab. Optionally, it may
110 * consider the given url as relative to the one already
111 * present in tab. It must set tab->urlstr to a serialized
112 * human-friendly URL. */
113 void (*loadfn)(struct tab*, const char*);
114 };
116 struct kmap {
117 TAILQ_HEAD(map, keymap) m;
118 void (*unhandled_input)(void);
119 };
121 struct keymap {
122 int meta;
123 int key;
124 struct kmap map;
125 void (*fn)(struct tab*);
127 TAILQ_ENTRY(keymap) keymaps;
128 };
130 extern struct event imsgev;
132 /* gemini.c */
133 int client_main(struct imsgbuf *b);
135 /* gemtext.c */
136 void gemtext_initparser(struct parser*);
138 /* keymap.c */
139 int kbd(const char*);
140 const char *unkbd(int);
141 int kmap_define_key(struct kmap*, const char*, void(*)(struct tab*));
143 /* mime.c */
144 int setup_parser_for(struct tab*);
146 /* pages.c */
147 extern const char *about_new;
149 #define CANNOT_FETCH 0
150 #define TOO_MUCH_REDIRECTS 1
151 #define MALFORMED_RESPONSE 2
152 #define UNKNOWN_TYPE_OR_CSET 3
153 extern const char *err_pages[70];
155 /* parser.c */
156 int parser_append(struct parser*, const char*, size_t);
157 int parser_set_buf(struct parser*, const char*, size_t);
159 /* sandbox.c */
160 void sandbox_network_process(void);
162 /* telescope.c */
163 void load_about_url(struct tab*, const char*);
164 void load_gemini_url(struct tab*, const char*);
165 void load_url(struct tab*, const char*);
166 void stop_tab(struct tab*);
168 /* textplain.c */
169 void textplain_initparser(struct parser*);
171 /* ui.c */
172 int ui_init(void);
173 void ui_on_tab_loaded(struct tab*);
174 void ui_on_tab_refresh(struct tab*);
175 void ui_require_input(struct tab*, int);
176 void ui_end(void);
178 /* util.c */
179 int mark_nonblock(int);
180 char *telescope_strnchr(char*, char, size_t);
181 int has_prefix(const char*, const char*);
183 #endif /* TELESCOPE_H */