-
Notifications
You must be signed in to change notification settings - Fork 50
/
libc-doc.c
146 lines (134 loc) · 3.62 KB
/
libc-doc.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_FILE_SIZE (4 * 1024 * 1024)
char buff[MAX_FILE_SIZE];
char token[MAX_FILE_SIZE];
void parse_token(char** s, char* t)
{
int verbatim_mode = 0;
while (**s == ' ' || **s == '\t' || **s == '\n' || **s == '\r')
(*s)++;
if (**s != '{') {
(*s)[32] = 0;
fprintf(stderr, "ERROR: Missing { at '%s...'\n", *s);
exit(-1);
}
(*s)++;
int tl = 0;
while (**s) {
// %< .... %> delineates a verbatim latex section, where no escaping of any sort happens.
if (verbatim_mode) {
if ((*s)[0] == '%' && (*s)[1] == '>') {
(*s) += 2;
verbatim_mode = 0;
}
else {
t[tl++] = **s;
(*s)++;
}
}
else {
if ((*s)[0] == '%' && (*s)[1] == '<') {
(*s) += 2;
verbatim_mode = 1;
continue;
}
if ((**s) == '}') {
(*s)++;
t[tl] = 0;
return;
}
else {
switch (**s) {
case '$':
case '_':
t[tl++] = '\\';
t[tl++] = **s;
break;
default:
t[tl++] = **s;
}
(*s)++;
}
}
}
}
int main(int argc, char** argv)
{
FILE* f = fopen(argv[1], "r");
int len = fread(buff, 1, MAX_FILE_SIZE, f);
fprintf(stderr, "Read %d bytes.\n", len);
buff[len] = 0;
char* s = strstr(buff, "\\m65libsummary");
while (s) {
s += strlen("\\m65libsummary");
// Skip spaces
while (s[0] == ' ')
s++;
parse_token(&s, token);
printf("\n\\subsection{%s}\n", token);
printf("\\index{%s}\n", token);
parse_token(&s, token);
printf("\\begin{description}[leftmargin=2.4cm,style=nextline]\n"
"\\item [Description:] {%s}\n",
token);
int last_was_param = 0;
while (strncmp(s, "\\m65libsummary", strlen("\\m65libsummary"))) {
int is_param = 0;
s = strstr(s, "\\m65lib");
if (!s)
break;
if (!strncmp(s, "\\m65libparam", strlen("\\m65libparam"))) {
s += strlen("\\m65libparam");
parse_token(&s, token);
if (!last_was_param) {
last_was_param = 1;
printf("\\item [Parameters:]\n\\begin{description}\n");
}
printf("\\item [{\\em %s}:]", token);
is_param = 1;
parse_token(&s, token);
printf(" {%s}\n", token);
}
else if (!strncmp(s, "\\m65libretval", strlen("\\m65libretval"))) {
s += strlen("\\m65libretval");
if (last_was_param)
printf("\\end{description}\n");
parse_token(&s, token);
printf("\\item [Return Value:] {%s}\n", token);
}
else if (!strncmp(s, "\\m65libsyntax", strlen("\\m65libsyntax"))) {
s += strlen("\\m65libsyntax");
if (last_was_param)
printf("\\end{description}\n");
parse_token(&s, token);
printf("\\item [Syntax:] \\stw{%s}\n", token);
}
else if (!strncmp(s, "\\m65libremarks", strlen("\\m65libremarks"))) {
s += strlen("\\m65libremarks");
if (last_was_param)
printf("\\end{description}\n");
parse_token(&s, token);
printf("\\item [Notes:] {%s}\n", token);
}
else if (!strncmp(s, "\\m65libsummary", strlen("\\m65libsummary"))) {
// Just catch so we don't report an error.
if (last_was_param)
printf("\\end{description}\n");
}
else {
s[32] = 0;
fprintf(stderr, "Unknown block type '%s...'\n", s);
exit(-1);
}
last_was_param = is_param;
}
printf("\\end{description}\n");
// Find next instance
if (s)
s = strstr(s, "\\m65libsummary");
else
break;
}
}