2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 * Handles config and runtime files
24 #include <sys/types.h>
38 #include "telescope.h"
44 #define nitems(x) (sizeof(x) / sizeof(x[0]))
47 static int select_non_dot(const struct dirent *);
48 static int select_non_dotdot(const struct dirent *);
49 static size_t join_path(char*, const char*, const char*, size_t);
50 static void getenv_default(char*, const char*, const char*, size_t);
51 static void mkdirs(const char*, mode_t);
52 static void init_paths(void);
55 * Where to store user data. These are all equal to ~/.telescope if
58 char config_path_base[PATH_MAX];
59 char data_path_base[PATH_MAX];
60 char cache_path_base[PATH_MAX];
62 char ctlsock_path[PATH_MAX];
63 char config_path[PATH_MAX];
64 char lockfile_path[PATH_MAX];
65 char bookmark_file[PATH_MAX];
66 char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
67 char crashed_file[PATH_MAX];
68 char session_file[PATH_MAX], session_file_tmp[PATH_MAX];
69 char history_file[PATH_MAX], history_file_tmp[PATH_MAX];
72 select_non_dot(const struct dirent *d)
74 return strcmp(d->d_name, ".");
78 select_non_dotdot(const struct dirent *d)
80 return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
84 send_dir(struct tab *tab, const char *path)
86 struct dirent **names;
87 int (*selector)(const struct dirent *) = select_non_dot;
92 * need something to fake a redirect
95 if (!has_suffix(path, "/")) {
96 if (asprintf(&s, "%s/", path) == -1)
98 send_hdr(peerid, 30, s);
104 if (!strcmp(path, "/"))
105 selector = select_non_dotdot;
107 if ((len = scandir(path, &names, selector, alphasort)) == -1) {
108 load_page_from_str(tab, "# failure reading the directory\n");
112 parser_init(tab, gemtext_initparser);
113 parser_parsef(tab, "# Index of %s\n\n", path);
115 for (i = 0; i < len; ++i) {
116 const char *sufx = "";
118 if (names[i]->d_type == DT_DIR)
121 parser_parsef(tab, "=> %s%s\n", names[i]->d_name, sufx);
133 if (fstat(fileno(fp), &sb) == -1)
136 return S_ISDIR(sb.st_mode);
140 file_type(const char *path)
142 const struct mapping {
146 {"diff", textpatch_initparser},
147 {"gemini", gemtext_initparser},
148 {"gmi", gemtext_initparser},
149 {"markdown", textplain_initparser},
150 {"md", textplain_initparser},
151 {"patch", gemtext_initparser},
156 if ((dot = strrchr(path, '.')) == NULL)
157 return textplain_initparser;
161 for (m = ms; m->ext != NULL; ++m)
162 if (!strcmp(m->ext, dot))
165 return textplain_initparser;
169 fs_load_url(struct tab *tab, const char *url)
171 const char *bpath = "bookmarks.gmi", *fallback = "# Not found\n";
172 parserinit initfn = gemtext_initparser;
183 {"about", NULL, about_about, about_about_len},
184 {"blank", NULL, about_blank, about_blank_len},
185 {"bookmarks", bpath, bookmarks, bookmarks_len},
186 {"crash", NULL, about_crash, about_crash_len},
187 {"help", NULL, about_help, about_help_len},
188 {"license", NULL, about_license, about_license_len},
189 {"new", NULL, about_new, about_new_len},
192 if (!strncmp(url, "about:", 6)) {
195 for (i = 0; page == NULL && i < nitems(pages); ++i) {
196 if (!strcmp(url, pages[i].name))
203 strlcpy(path, data_path_base, sizeof(path));
204 strlcat(path, "/", sizeof(path));
205 if (page->path != NULL)
206 strlcat(path, page->path, sizeof(path));
208 strlcat(path, "page/about_", sizeof(path));
209 strlcat(path, page->name, sizeof(path));
210 strlcat(path, ".gmi", sizeof(path));
213 fallback = page->data;
214 } else if (!strncmp(url, "file://", 7)) {
216 strlcpy(path, url, sizeof(path));
217 initfn = file_type(url);
221 if ((fp = fopen(path, "r")) == NULL)
230 parser_init(tab, initfn);
234 r = fread(buf, 1, sizeof(buf), fp);
235 if (!parser_parse(tab, buf, r))
237 if (r != sizeof(buf))
246 load_page_from_str(tab, fallback);
250 join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
252 strlcpy(buf, lhs, buflen);
253 return strlcat(buf, rhs, buflen);
257 getenv_default(char *buf, const char *name, const char *def, size_t buflen)
262 if ((home = getenv("HOME")) == NULL)
263 errx(1, "HOME is not defined");
265 if ((env = getenv(name)) != NULL)
266 ret = strlcpy(buf, env, buflen);
268 ret = join_path(buf, home, def, buflen);
271 errx(1, "buffer too small for %s", name);
275 mkdirs(const char *path, mode_t mode)
277 char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
279 strlcpy(copy, path, sizeof(copy));
280 strlcpy(orig, path, sizeof(orig));
281 parent = dirname(copy);
282 if (!strcmp(parent, "/"))
284 mkdirs(parent, mode);
286 if (mkdir(orig, mode) != 0) {
289 err(1, "can't mkdir %s", orig);
296 char xdg_config_base[PATH_MAX];
297 char xdg_data_base[PATH_MAX];
298 char xdg_cache_base[PATH_MAX];
299 char old_path[PATH_MAX];
304 if ((home = getenv("HOME")) == NULL)
305 errx(1, "HOME is not defined");
306 join_path(old_path, home, "/.telescope", sizeof(old_path));
308 /* if ~/.telescope exists, use that instead of xdg dirs */
309 if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
310 join_path(config_path_base, home, "/.telescope",
311 sizeof(config_path_base));
312 join_path(data_path_base, home, "/.telescope",
313 sizeof(data_path_base));
314 join_path(cache_path_base, home, "/.telescope",
315 sizeof(cache_path_base));
320 getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
321 sizeof(xdg_config_base));
322 getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
323 sizeof(xdg_data_base));
324 getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
325 sizeof(xdg_cache_base));
327 join_path(config_path_base, xdg_config_base, "/telescope",
328 sizeof(config_path_base));
329 join_path(data_path_base, xdg_data_base, "/telescope",
330 sizeof(data_path_base));
331 join_path(cache_path_base, xdg_cache_base, "/telescope",
332 sizeof(cache_path_base));
334 mkdirs(xdg_config_base, S_IRWXU);
335 mkdirs(xdg_data_base, S_IRWXU);
336 mkdirs(xdg_cache_base, S_IRWXU);
338 mkdirs(config_path_base, S_IRWXU);
339 mkdirs(data_path_base, S_IRWXU);
340 mkdirs(cache_path_base, S_IRWXU);
348 join_path(ctlsock_path, cache_path_base, "/ctl",
349 sizeof(ctlsock_path));
350 join_path(config_path, config_path_base, "/config",
351 sizeof(config_path));
352 join_path(lockfile_path, cache_path_base, "/lock",
353 sizeof(lockfile_path));
354 join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
355 sizeof(bookmark_file));
356 join_path(known_hosts_file, data_path_base, "/known_hosts",
357 sizeof(known_hosts_file));
358 join_path(known_hosts_tmp, cache_path_base,
359 "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
360 join_path(session_file, cache_path_base, "/session",
361 sizeof(session_file));
362 join_path(session_file_tmp, cache_path_base, "/session.XXXXXXXXXX",
363 sizeof(session_file));
364 join_path(history_file, cache_path_base, "/history",
365 sizeof(history_file));
366 join_path(history_file_tmp, cache_path_base, "/history.XXXXXXXXXX",
367 sizeof(history_file));
368 join_path(crashed_file, cache_path_base, "/crashed",
369 sizeof(crashed_file));