-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgibase.c
227 lines (199 loc) · 4.76 KB
/
cgibase.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Here are the routines of man2html that output a HREF string.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h> /* tolower() */
#include <string.h> /* strlen() */
#include "defs.h"
typedef void (*link_print_func_t)(const char *section, const char *page);
typedef link_print_func_t (*link_lookup_func_t)(const char *what);
struct link_part {
link_print_func_t print;
char *string;
};
static struct link_part *link_parts_page;
static struct link_part *link_parts_home;
static const char *
html_char_escape(int ch) {
switch (ch) {
case '"': return """;
case '&': return "&";
case '<': return "<";
case '>': return ">";
}
return NULL;
}
void
print_html_char(int ch) {
const char *esc;
if ((esc = html_char_escape(ch))) {
printf("%s", esc);
} else {
printf("%c", ch);
}
}
void
print_html_string(const char *str) {
for (; *str; str++) {
print_html_char(*str);
}
}
static void
link_print_section(const char *section, const char *page) {
(void)page;
if (section[0]) {
print_html_char(section[0]);
}
}
static void
link_print_subsection(const char *section, const char *page) {
(void)page;
if (section[0]) {
print_html_string(§ion[1]);
}
}
static void
link_print_page(const char *section, const char *page) {
(void)section;
print_html_string(page);
}
static link_print_func_t
link_lookup_section(const char *what) {
if (!strcmp(what, "{section}")) return link_print_section;
if (!strcmp(what, "{subsection}")) return link_print_subsection;
return NULL;
}
static link_print_func_t
link_lookup_page(const char *what) {
if (!strcmp(what, "{page}")) return link_print_page;
return link_lookup_section(what);
}
static link_print_func_t
link_lookup_none(const char *what) {
(void)what;
return NULL;
}
static const char *
link_parse(const char *template, link_lookup_func_t lookup,
struct link_part **out) {
struct link_part *parts;
struct link_part *part;
const size_t maxparts = 15;
const size_t parts_size = (maxparts + 1) * sizeof(*parts);
const char *a;
const char *b;
char *str;
size_t len;
*out = NULL;
part = parts = xmalloc(parts_size);
memset(parts, 0, parts_size);
for (a = template; *a; a = b) {
b = a;
if (*a == '{') {
b++;
while (*b != '}') {
if (!isalnum(*b)) {
return "syntax error";
}
b++;
}
b++;
} else if (*a == '}') {
return "syntax error";
} else {
while (*b && (*b != '{') && (*b != '}')) {
b++;
}
}
len = b - a;
str = xmalloc(len + 1);
memcpy(str, a, len);
str[len] = '\0';
//
if (part >= parts + maxparts) {
return "too many link parts";
}
if (*str == '{') {
if ((part->print = lookup(str)) == NULL) {
return "unknown directive";
}
free(str);
} else {
part->string = str;
}
part++;
}
*out = parts;
return NULL;
}
const char *
link_parse_page(const char *template) {
return link_parse(template,
link_lookup_page,
&link_parts_page);
}
const char *
link_parse_home(const char *template) {
return link_parse(template,
link_lookup_none,
&link_parts_home);
}
static void
print_link(struct link_part *parts, const char *section, const char *page) {
struct link_part *part;
if (!parts) {
return;
}
for (part = parts; ; part++) {
if (part->print) {
part->print(section, page);
} else if (part->string) {
printf("%s", part->string);
} else {
break;
}
}
}
void
print_sig(void) {
}
void
include_file_html(char *g) {
printf("<A HREF=\"file:/usr/include/%s\">%s</A>>", g,g);
}
void
print_link_home(void)
{
printf("<A HREF=\"");
print_link(link_parts_home, "", "");
printf("\">");
print_html_string("Return to Main Contents");
printf("</A>");
}
void
print_link_page(const char *section, const char *page)
{
printf("<A HREF=\"");
print_link(link_parts_page, section, page);
printf("\">");
print_html_string(page);
printf("</A>");
}
void
ftp_html(char *f) {
printf("<A HREF=\"ftp://%s\">%s</A>", f, f);
}
void
www_html(char *f) {
printf("<A HREF=\"http://%s\">%s</A>", f, f);
}
void
mailto_html(char *g) {
printf("<A HREF=\"mailto:%s\">%s</A>", g, g);
}
void
url_html(char *g) {
printf("<A HREF=\"%s\">%s</A>", g, g);
}