-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgophermap.c
More file actions
151 lines (134 loc) · 3.45 KB
/
gophermap.c
File metadata and controls
151 lines (134 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*-
* "THE BEER-WARE LICENSE" (Revision 42):
* <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return.
*/
#define _POSIX_C_SOURCE 200809
#include <assert.h>
#include <syslog.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <errno.h>
#include "gophermap.h"
#include "tools.h"
bool
gophermap_parse_item(struct opt_options *options, struct item *item,
const char *selector, const char *line, FILE *out)
{
assert(item != NULL);
assert(line != NULL);
assert(out != NULL);
const char *p = line;
if (*p == '\t') {
syslog(LOG_NOTICE, "malformed gophermap line: \"%s\"", line);
send_error(out, "E: Malformed line", line);
return (false);
}
char type = *p++;
size_t l = strcspn(p, "\t");
if (l == 0) {
syslog(LOG_NOTICE, "malformed gophermap line: \"%s\"", line);
send_error(out, "E: Malformed line", line);
return (false);
}
char *display = malloc(l+1);
if (display == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(out, "E: malloc", strerror(errno));
send_info(out, "I: I could not allocate memory.", NULL);
exit(EXIT_FAILURE);
}
strncpy(display, p, l);
display[l] = '\0';
p += l;
if (*p == '\0') {
syslog(LOG_NOTICE, "malformed gophermap line: \"%s\"", line);
send_error(out, "E: Malformed line", line);
free(display);
return (false);
}
p++;
l = strcspn(p, "\t");
bool relative = (l > 0 && *p != '/' && strncasecmp(p, "GET ", 4) != 0);
char *sel;
if (relative) {
char *rel = malloc(l+1);
if (rel == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(out, "E: malloc", strerror(errno));
send_info(out, "I: I could not allocate memory.", NULL);
exit(EXIT_FAILURE);
}
strncpy(rel, p, l);
rel[l] = '\0';
sel = tool_join_path(selector, rel, out);
free(rel);
} else {
sel = malloc(l+1);
if (sel == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(out, "E: malloc", strerror(errno));
send_info(out, "I: I could not allocate memory.", NULL);
exit(EXIT_FAILURE);
}
strncpy(sel, p, l);
sel[l] = '\0';
}
p += l;
if (*p != '\0')
p++;
size_t ll = l = strcspn(p, "\t");
bool use_default = (l == 0 || (l == 1 && *p == '+'));
if (use_default)
ll = strlen(opt_get_host(options));
char *host = malloc(ll+1);
if (host == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(out, "E: malloc", strerror(errno));
send_info(out, "I: I could not allocate memory.", NULL);
exit(EXIT_FAILURE);
}
if (use_default)
strncpy(host, opt_get_host(options), ll);
else
strncpy(host, p, ll);
host[ll] = '\0';
p += l;
if (*p != '\0')
p++;
ll = l = strcspn(p, "\t");
use_default = (l == 0 || (l == 1 && *p == '+'));
if (use_default)
ll = strlen(opt_get_port(options));
char *port = malloc(ll+1);
if (host == NULL) {
syslog(LOG_ERR, "malloc error: %m");
send_error(out, "E: malloc", strerror(errno));
send_info(out, "I: I could not allocate "
"memory.", NULL);
exit(EXIT_FAILURE);
}
if (use_default)
strncpy(port, opt_get_port(options), ll);
else
strncpy(port, p, ll);
port[ll] = '\0';
item->type = type;
item->display = display;
item->selector = sel;
item->host = host;
item->port = port;
return (true);
}
void
gophermap_free_item(struct item *item)
{
assert(item != NULL);
free(item->display);
free(item->selector);
free(item->host);
free(item->port);
}