Blame
Date:
Sun Apr 24 12:14:54 2022 UTC
Message:
move load session stuff to session.c
001
2021-03-14
op
/*
002
2021-03-14
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
003
2021-03-14
op
*
004
2021-03-14
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-03-14
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-03-14
op
* copyright notice and this permission notice appear in all copies.
007
2021-03-14
op
*
008
2021-03-14
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-03-14
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-03-14
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-03-14
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-03-14
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-03-14
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-03-14
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015
2021-03-14
op
*/
016
2021-03-14
op
017
2021-03-15
op
/*
018
2021-10-07
op
* Handles config and runtime files
019
2021-03-15
op
*/
020
2021-07-21
op
021
2021-07-21
op
#include "compat.h"
022
2021-03-14
op
023
2021-03-20
op
#include <sys/stat.h>
024
2021-08-13
op
#include <sys/types.h>
025
2021-03-20
op
026
2021-08-13
op
#include <dirent.h>
027
2021-03-14
op
#include <errno.h>
028
2021-03-14
op
#include <limits.h>
029
2021-10-07
op
#include <libgen.h>
030
2021-03-14
op
#include <stdio.h>
031
2021-03-14
op
#include <stdlib.h>
032
2021-03-14
op
#include <string.h>
033
2021-03-14
op
#include <unistd.h>
034
2021-07-12
op
035
2021-07-13
op
#include "pages.h"
036
2022-04-24
op
#include "parser.h"
037
2022-01-03
op
#include "session.h"
038
2022-04-24
op
#include "telescope.h"
039
2022-01-11
op
#include "utils.h"
040
2021-03-14
op
041
2022-04-24
op
#include "fs.h"
042
2022-04-24
op
043
2022-04-24
op
#ifndef nitems
044
2022-04-24
op
#define nitems(x) (sizeof(x) / sizeof(x[0]))
045
2022-04-24
op
#endif
046
2022-04-24
op
047
2021-08-13
op
static int select_non_dot(const struct dirent *);
048
2021-08-13
op
static int select_non_dotdot(const struct dirent *);
049
2021-10-07
op
static size_t join_path(char*, const char*, const char*, size_t);
050
2021-10-07
op
static void getenv_default(char*, const char*, const char*, size_t);
051
2021-10-07
op
static void mkdirs(const char*, mode_t);
052
2022-01-03
op
static void init_paths(void);
053
2021-03-14
op
054
2021-11-27
op
/*
055
2021-11-27
op
* Where to store user data. These are all equal to ~/.telescope if
056
2021-11-27
op
* it exists.
057
2021-11-27
op
*/
058
2021-10-07
op
char config_path_base[PATH_MAX];
059
2021-10-07
op
char data_path_base[PATH_MAX];
060
2021-10-07
op
char cache_path_base[PATH_MAX];
061
2021-10-07
op
062
2022-02-11
op
char ctlsock_path[PATH_MAX];
063
2021-10-07
op
char config_path[PATH_MAX];
064
2021-10-07
op
char lockfile_path[PATH_MAX];
065
2021-10-07
op
char bookmark_file[PATH_MAX];
066
2021-10-07
op
char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
067
2021-10-07
op
char crashed_file[PATH_MAX];
068
2022-04-24
op
char session_file[PATH_MAX], session_file_tmp[PATH_MAX];
069
2022-04-24
op
char history_file[PATH_MAX], history_file_tmp[PATH_MAX];
070
2021-03-15
op
071
2022-04-24
op
static int
072
2022-04-24
op
select_non_dot(const struct dirent *d)
073
2021-08-13
op
{
074
2022-04-24
op
return strcmp(d->d_name, ".");
075
2022-04-24
op
}
076
2021-08-13
op
077
2022-04-24
op
static int
078
2022-04-24
op
select_non_dotdot(const struct dirent *d)
079
2022-04-24
op
{
080
2022-04-24
op
return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
081
2021-08-13
op
}
082
2021-08-13
op
083
2021-08-13
op
static void
084
2022-04-24
op
send_dir(struct tab *tab, const char *path)
085
2021-03-14
op
{
086
2022-04-24
op
struct dirent **names;
087
2022-04-24
op
int (*selector)(const struct dirent *) = select_non_dot;
088
2022-04-24
op
int i, len;
089
2021-03-14
op
090
2022-04-24
op
#if notyet
091
2022-04-24
op
/*
092
2022-04-24
op
* need something to fake a redirect
093
2022-04-24
op
*/
094
2021-03-14
op
095
2022-04-24
op
if (!has_suffix(path, "/")) {
096
2022-04-24
op
if (asprintf(&s, "%s/", path) == -1)
097
2022-04-24
op
die();
098
2022-04-24
op
send_hdr(peerid, 30, s);
099
2022-04-24
op
free(s);
100
2022-04-24
op
return;
101
2021-07-21
op
}
102
2022-04-24
op
#endif
103
2021-07-21
op
104
2022-04-24
op
if (!strcmp(path, "/"))
105
2022-04-24
op
selector = select_non_dotdot;
106
2022-04-24
op
107
2022-04-24
op
if ((len = scandir(path, &names, selector, alphasort)) == -1) {
108
2022-04-24
op
load_page_from_str(tab, "# failure reading the directory\n");
109
2021-07-18
op
return;
110
2021-03-14
op
}
111
2021-07-18
op
112
2022-04-24
op
parser_init(tab, gemtext_initparser);
113
2022-04-24
op
parser_parsef(tab, "# Index of %s\n\n", path);
114
2021-07-21
op
115
2022-04-24
op
for (i = 0; i < len; ++i) {
116
2022-04-24
op
const char *sufx = "";
117
2021-08-13
op
118
2022-04-24
op
if (names[i]->d_type == DT_DIR)
119
2022-04-24
op
sufx = "/";
120
2022-04-24
op
121
2022-04-24
op
parser_parsef(tab, "=> %s%s\n", names[i]->d_name, sufx);
122
2022-04-24
op
}
123
2022-04-24
op
124
2022-04-24
op
parser_free(tab);
125
2022-04-24
op
free(names);
126
2021-08-13
op
}
127
2021-08-13
op
128
2022-04-24
op
static int
129
2022-04-24
op
is_dir(FILE *fp)
130
2021-08-13
op
{
131
2022-04-24
op
struct stat sb;
132
2021-08-13
op
133
2022-04-24
op
if (fstat(fileno(fp), &sb) == -1)
134
2022-04-24
op
return 0;
135
2021-08-13
op
136
2022-04-24
op
return S_ISDIR(sb.st_mode);
137
2021-08-13
op
}
138
2021-08-13
op
139
2022-04-24
op
static parserinit
140
2021-08-13
op
file_type(const char *path)
141
2021-08-13
op
{
142
2022-04-24
op
const struct mapping {
143
2021-08-13
op
const char *ext;
144
2022-04-24
op
parserinit fn;
145
2021-08-13
op
} ms[] = {
146
2022-04-24
op
{"diff", textpatch_initparser},
147
2022-04-24
op
{"gemini", gemtext_initparser},
148
2022-04-24
op
{"gmi", gemtext_initparser},
149
2022-04-24
op
{"markdown", textplain_initparser},
150
2022-04-24
op
{"md", textplain_initparser},
151
2022-04-24
op
{"patch", gemtext_initparser},
152
2021-08-13
op
{NULL, NULL},
153
2021-08-13
op
}, *m;
154
2022-04-24
op
const char *dot;
155
2021-08-13
op
156
2021-08-13
op
if ((dot = strrchr(path, '.')) == NULL)
157
2022-04-24
op
return textplain_initparser;
158
2021-08-13
op
159
2021-08-13
op
dot++;
160
2021-08-13
op
161
2021-08-13
op
for (m = ms; m->ext != NULL; ++m)
162
2021-08-13
op
if (!strcmp(m->ext, dot))
163
2022-04-24
op
return m->fn;
164
2021-08-13
op
165
2022-04-24
op
return textplain_initparser;
166
2021-08-13
op
}
167
2021-08-13
op
168
2022-04-24
op
void
169
2022-04-24
op
fs_load_url(struct tab *tab, const char *url)
170
2021-08-13
op
{
171
2022-04-24
op
const char *bpath = "bookmarks.gmi", *fallback = "# Not found\n";
172
2022-04-24
op
parserinit initfn = gemtext_initparser;
173
2022-04-24
op
char path[PATH_MAX];
174
2022-04-24
op
FILE *fp = NULL;
175
2022-04-24
op
size_t i;
176
2022-04-24
op
char buf[BUFSIZ];
177
2022-04-24
op
struct page {
178
2022-04-24
op
const char *name;
179
2022-04-24
op
const char *path;
180
2022-04-24
op
const uint8_t *data;
181
2022-04-24
op
size_t len;
182
2022-04-24
op
} pages[] = {
183
2022-04-24
op
{"about", NULL, about_about, about_about_len},
184
2022-04-24
op
{"blank", NULL, about_blank, about_blank_len},
185
2022-04-24
op
{"bookmarks", bpath, bookmarks, bookmarks_len},
186
2022-04-24
op
{"crash", NULL, about_crash, about_crash_len},
187
2022-04-24
op
{"help", NULL, about_help, about_help_len},
188
2022-04-24
op
{"license", NULL, about_license, about_license_len},
189
2022-04-24
op
{"new", NULL, about_new, about_new_len},
190
2022-04-24
op
}, *page = NULL;
191
2021-08-13
op
192
2022-04-24
op
if (!strncmp(url, "about:", 6)) {
193
2022-04-24
op
url += 6;
194
2021-08-13
op
195
2022-04-24
op
for (i = 0; page == NULL && i < nitems(pages); ++i) {
196
2022-04-24
op
if (!strcmp(url, pages[i].name))
197
2022-04-24
op
page = &pages[i];
198
2022-04-24
op
}
199
2021-08-13
op
200
2022-04-24
op
if (page == NULL)
201
2022-04-24
op
goto done;
202
2021-08-13
op
203
2022-04-24
op
strlcpy(path, data_path_base, sizeof(path));
204
2022-04-24
op
strlcat(path, "/", sizeof(path));
205
2022-04-24
op
if (page->path != NULL)
206
2022-04-24
op
strlcat(path, page->path, sizeof(path));
207
2022-04-24
op
else {
208
2022-04-24
op
strlcat(path, "page/about_", sizeof(path));
209
2022-04-24
op
strlcat(path, page->name, sizeof(path));
210
2022-04-24
op
strlcat(path, ".gmi", sizeof(path));
211
2022-04-24
op
}
212
2021-08-13
op
213
2022-04-24
op
fallback = page->data;
214
2022-04-24
op
} else if (!strncmp(url, "file://", 7)) {
215
2022-04-24
op
url += 7;
216
2022-04-24
op
strlcpy(path, url, sizeof(path));
217
2022-04-24
op
initfn = file_type(url);
218
2022-04-24
op
} else
219
2022-04-24
op
goto done;
220
2021-08-13
op
221
2022-04-24
op
if ((fp = fopen(path, "r")) == NULL)
222
2022-04-24
op
goto done;
223
2021-08-13
op
224
2022-04-24
op
if (is_dir(fp)) {
225
2022-04-24
op
fclose(fp);
226
2022-04-24
op
send_dir(tab, path);
227
2022-04-24
op
goto done;
228
2021-08-13
op
}
229
2021-08-13
op
230
2022-04-24
op
parser_init(tab, initfn);
231
2022-04-24
op
for (;;) {
232
2022-04-24
op
size_t r;
233
2021-08-13
op
234
2022-04-24
op
r = fread(buf, 1, sizeof(buf), fp);
235
2022-04-24
op
if (!parser_parse(tab, buf, r))
236
2022-04-24
op
break;
237
2022-04-24
op
if (r != sizeof(buf))
238
2022-04-24
op
break;
239
2021-08-13
op
}
240
2022-04-24
op
parser_free(tab);
241
2021-08-13
op
242
2022-04-24
op
done:
243
2022-04-24
op
if (fp != NULL)
244
2022-04-24
op
fclose(fp);
245
2022-04-24
op
else
246
2022-04-24
op
load_page_from_str(tab, fallback);
247
2021-03-15
op
}
248
2021-04-25
op
249
2021-10-07
op
static size_t
250
2021-10-07
op
join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
251
2021-03-14
op
{
252
2021-10-07
op
strlcpy(buf, lhs, buflen);
253
2021-10-07
op
return strlcat(buf, rhs, buflen);
254
2021-10-07
op
}
255
2021-07-15
op
256
2021-10-07
op
static void
257
2021-10-07
op
getenv_default(char *buf, const char *name, const char *def, size_t buflen)
258
2021-10-07
op
{
259
2021-10-07
op
size_t ret;
260
2021-10-07
op
char *home, *env;
261
2021-03-15
op
262
2021-10-07
op
if ((home = getenv("HOME")) == NULL)
263
2021-10-07
op
errx(1, "HOME is not defined");
264
2021-03-18
op
265
2021-10-07
op
if ((env = getenv(name)) != NULL)
266
2021-10-07
op
ret = strlcpy(buf, env, buflen);
267
2021-10-07
op
else
268
2021-10-07
op
ret = join_path(buf, home, def, buflen);
269
2021-04-25
op
270
2021-10-07
op
if (ret >= buflen)
271
2021-10-07
op
errx(1, "buffer too small for %s", name);
272
2021-10-07
op
}
273
2021-04-01
op
274
2021-10-07
op
static void
275
2021-10-07
op
mkdirs(const char *path, mode_t mode)
276
2021-10-07
op
{
277
2021-10-07
op
char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
278
2021-07-20
op
279
2021-10-07
op
strlcpy(copy, path, sizeof(copy));
280
2021-10-07
op
strlcpy(orig, path, sizeof(orig));
281
2021-10-07
op
parent = dirname(copy);
282
2021-10-07
op
if (!strcmp(parent, "/"))
283
2021-10-07
op
return;
284
2021-10-07
op
mkdirs(parent, mode);
285
2021-10-07
op
286
2021-10-07
op
if (mkdir(orig, mode) != 0) {
287
2021-10-07
op
if (errno == EEXIST)
288
2021-10-07
op
return;
289
2021-10-07
op
err(1, "can't mkdir %s", orig);
290
2021-10-07
op
}
291
2021-10-07
op
}
292
2021-10-07
op
293
2021-10-07
op
static void
294
2022-01-03
op
init_paths(void)
295
2021-10-07
op
{
296
2022-01-03
op
char xdg_config_base[PATH_MAX];
297
2022-01-03
op
char xdg_data_base[PATH_MAX];
298
2022-01-03
op
char xdg_cache_base[PATH_MAX];
299
2022-01-03
op
char old_path[PATH_MAX];
300
2022-01-03
op
char *home;
301
2022-01-03
op
struct stat info;
302
2021-10-07
op
303
2021-10-07
op
/* old path */
304
2021-10-07
op
if ((home = getenv("HOME")) == NULL)
305
2021-10-07
op
errx(1, "HOME is not defined");
306
2021-10-07
op
join_path(old_path, home, "/.telescope", sizeof(old_path));
307
2021-10-07
op
308
2021-10-07
op
/* if ~/.telescope exists, use that instead of xdg dirs */
309
2021-10-07
op
if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
310
2021-10-07
op
join_path(config_path_base, home, "/.telescope",
311
2021-10-07
op
sizeof(config_path_base));
312
2021-10-07
op
join_path(data_path_base, home, "/.telescope",
313
2021-10-07
op
sizeof(data_path_base));
314
2021-10-07
op
join_path(cache_path_base, home, "/.telescope",
315
2021-10-07
op
sizeof(cache_path_base));
316
2021-10-07
op
return;
317
2021-10-07
op
}
318
2021-10-07
op
319
2021-10-07
op
/* xdg paths */
320
2021-10-07
op
getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
321
2021-10-07
op
sizeof(xdg_config_base));
322
2021-10-07
op
getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
323
2021-10-07
op
sizeof(xdg_data_base));
324
2021-10-07
op
getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
325
2021-10-07
op
sizeof(xdg_cache_base));
326
2021-10-07
op
327
2021-10-07
op
join_path(config_path_base, xdg_config_base, "/telescope",
328
2021-10-07
op
sizeof(config_path_base));
329
2021-10-07
op
join_path(data_path_base, xdg_data_base, "/telescope",
330
2021-10-07
op
sizeof(data_path_base));
331
2021-10-07
op
join_path(cache_path_base, xdg_cache_base, "/telescope",
332
2021-10-07
op
sizeof(cache_path_base));
333
2021-10-07
op
334
2021-10-07
op
mkdirs(xdg_config_base, S_IRWXU);
335
2021-10-07
op
mkdirs(xdg_data_base, S_IRWXU);
336
2021-10-07
op
mkdirs(xdg_cache_base, S_IRWXU);
337
2021-10-07
op
338
2021-10-07
op
mkdirs(config_path_base, S_IRWXU);
339
2021-10-07
op
mkdirs(data_path_base, S_IRWXU);
340
2021-10-07
op
mkdirs(cache_path_base, S_IRWXU);
341
2021-10-07
op
}
342
2021-10-07
op
343
2021-10-07
op
int
344
2021-10-07
op
fs_init(void)
345
2021-10-07
op
{
346
2022-01-03
op
init_paths();
347
2021-10-07
op
348
2022-02-11
op
join_path(ctlsock_path, cache_path_base, "/ctl",
349
2022-02-11
op
sizeof(ctlsock_path));
350
2021-10-07
op
join_path(config_path, config_path_base, "/config",
351
2021-10-07
op
sizeof(config_path));
352
2021-10-07
op
join_path(lockfile_path, cache_path_base, "/lock",
353
2021-10-07
op
sizeof(lockfile_path));
354
2021-10-07
op
join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
355
2021-10-07
op
sizeof(bookmark_file));
356
2021-10-07
op
join_path(known_hosts_file, data_path_base, "/known_hosts",
357
2021-10-07
op
sizeof(known_hosts_file));
358
2021-10-07
op
join_path(known_hosts_tmp, cache_path_base,
359
2021-10-07
op
"/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
360
2021-10-07
op
join_path(session_file, cache_path_base, "/session",
361
2022-04-24
op
sizeof(session_file));
362
2022-04-24
op
join_path(session_file_tmp, cache_path_base, "/session.XXXXXXXXXX",
363
2021-10-07
op
sizeof(session_file));
364
2022-02-26
op
join_path(history_file, cache_path_base, "/history",
365
2022-04-24
op
sizeof(history_file));
366
2022-04-24
op
join_path(history_file_tmp, cache_path_base, "/history.XXXXXXXXXX",
367
2022-02-26
op
sizeof(history_file));
368
2021-10-07
op
join_path(crashed_file, cache_path_base, "/crashed",
369
2021-10-07
op
sizeof(crashed_file));
370
2021-10-07
op
371
2021-03-18
op
return 1;
372
2022-02-26
op
}
Omar Polo