💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Vgmi › files › 68e7184026edf419dfbc89977cb96… captured on 2023-12-28 at 15:46:15. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 /*
1 * ISC License
2 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>
3 */
4 #ifndef DISABLE_XDG
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include "macro.h"
10 #include "strnstr.h"
11 #include "proc.h"
12 #include "sandbox.h"
13 #include "error.h"
14 #include "config.h"
15
16 const char *allowed_protocols[] = {
17 "http://",
18 "https://",
19 "gopher://",
20 "mailto:",
21 };
22
23 int has_xdg = -1;
24 int xdg_available() {
25 if (has_xdg == -1)
26 has_xdg = !system("xdg-open --help >/dev/null 2>&1");
27 return has_xdg;
28 }
29
30 int xdg_exec(char *line, size_t len) {
31 char cmd[MAX_URL + 128];
32 size_t i;
33 for (i = 0; i < LENGTH(allowed_protocols); i++) {
34 if (strnstr(line, allowed_protocols[i], len) == line)
35 break;
36 }
37 if (i >= LENGTH(allowed_protocols)) return -1;
38 snprintf(V(cmd), "xdg-open %s >/dev/null 2>&1", line);
39 system(cmd);
40 return 0;
41 }
42
43 int xdg_proc(int in, int out) {
44
45 char buf[MAX_URL];
46 size_t i;
47 unsigned char byte;
48
49 sandbox_set_name("vgmi [xdg]");
50 i = byte = 0;
51 write(out, P(byte));
52 while (1) {
53 if (i >= sizeof(buf)) i = 0;
54 if (read(in, P(byte)) != 1) break;
55 if (byte != '\n') {
56 buf[i++] = byte;
57 continue;
58 }
59 buf[i] = 0;
60 byte = xdg_exec(V(buf));
61 write(out, P(byte));
62 i = 0;
63
64 }
65 return 0;
66 }
67
68 int xdg_in = -1, xdg_out = -1;
69 int xdg_request(const char *request) {
70 unsigned char byte;
71 write(xdg_out, request, strlen(request));
72 byte = '\n';
73 write(xdg_out, P(byte));
74 read(xdg_in, &byte, 1);
75 if (byte) return ERROR_XDG;
76 return 0;
77 }
78
79 int xdg_init() {
80 if (!config.enableXdg) {
81 has_xdg = 0;
82 return 0;
83 }
84 return proc_fork("--xdg", &xdg_in, &xdg_out);
85 }
86 #else
87 typedef int hide_warning;
88 #endif
89