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 TAILQ_ENTRY(line) lines;
58 };
60 struct parser;
61 struct page;
63 /* typedef void (*initparserfn)(struct parser*); */
65 typedef int (*parsechunkfn)(struct parser*, const char*, size_t);
66 typedef int (*parserfreefn)(struct parser*);
68 typedef void (imsg_handlerfn)(struct imsg*, size_t);
70 struct parser {
71 const char *name;
72 char *buf;
73 size_t len;
74 size_t cap;
75 int flags;
76 parsechunkfn parse;
77 parserfreefn free;
79 TAILQ_HEAD(, line) head;
80 };
82 struct ui_state;
84 extern TAILQ_HEAD(tabshead, tab) tabshead;
85 struct tab {
86 struct parser page;
87 TAILQ_ENTRY(tab) tabs;
88 uint32_t id;
89 uint32_t flags;
91 char urlstr[GEMINI_URL_LEN];
92 struct url url;
94 int code;
95 char meta[GEMINI_URL_LEN];
96 int redirect_count;
98 struct ui_state *s;
99 };
101 struct proto {
102 const char *schema;
104 /* should load the given url in the tab. Optionally, it may
105 * consider the given url as relative to the one already
106 * present in tab. It must set tab->urlstr to a serialized
107 * human-friendly URL. */
108 void (*loadfn)(struct tab*, const char*);
109 };
111 extern struct event imsgev;
113 /* gemini.c */
114 int client_main(struct imsgbuf *b);
116 /* gemtext.c */
117 void gemtext_initparser(struct parser*);
119 /* mime.c */
120 int setup_parser_for(struct tab*);
122 /* pages.c */
123 extern const char *about_new;
125 #define CANNOT_FETCH 0
126 #define TOO_MUCH_REDIRECTS 1
127 #define MALFORMED_RESPONSE 2
128 #define UNKNOWN_TYPE_OR_CSET 3
129 extern const char *err_pages[70];
131 /* parser.c */
132 int parser_append(struct parser*, const char*, size_t);
133 int parser_set_buf(struct parser*, const char*, size_t);
135 /* telescope.c */
136 void load_about_url(struct tab*, const char*);
137 void load_gemini_url(struct tab*, const char*);
138 void load_url(struct tab*, const char*);
139 void stop_tab(struct tab*);
141 /* textplain.c */
142 void textplain_initparser(struct parser*);
144 /* ui.c */
145 int ui_init(void);
146 void ui_on_tab_loaded(struct tab*);
147 void ui_on_tab_refresh(struct tab*);
148 void ui_require_input(struct tab*, int);
149 void ui_end(void);
151 /* util.c */
152 int mark_nonblock(int);
153 char *telescope_strnchr(char*, char, size_t);
154 int has_prefix(const char*, const char*);
156 #endif /* TELESCOPE_H */