-
Notifications
You must be signed in to change notification settings - Fork 0
/
writebuf.h
179 lines (177 loc) · 7.56 KB
/
writebuf.h
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
#define WRITEBUFMAXCHARLEN 128
#define WRITEBUFMAXBASELEN 256
void writebufclean(char *buf, int basecode) {
int bfi = -1; while (buf[++bfi] != '\0') {
if (buf[bfi] == '"')
{ fprintf(stdout, """); }
else if (buf[bfi] == '&')
{ fprintf(stdout, "&"); }
else if (buf[bfi] == '<')
{ fprintf(stdout, "<"); }
else if (buf[bfi] == '>')
{ fprintf(stdout, ">"); }
else if (buf[bfi] == '\\')
{ fprintf(stdout, "\"); }
else { fprintf(stdout, "%c", buf[bfi]); }
// if (buf[bfi] == '"')
// { fprintf(stdout, "%c%c", '\\', '"'); }
// else { fprintf(stdout, "%c", buf[bfi]); }
// fprintf(stdout, "%c", buf[bfi]);
}
}
void writebufhighlight(char *buf, int basecode) {
char charbuf[WRITEBUFMAXCHARLEN];
char basebuf[WRITEBUFMAXBASELEN];
char *cfmt = "<b class=\"%s\">%c</b>";
char *sfmt = "<b class=\"%s\">%s</b>";
char *cescfmt = "<b class=\\\"%s\\\">%c</b>";
char *sescfmt = "<b class=\\\"%s\\\">%s</b>";
#define WRITEBUFMATCHLISTLEN 8
char *clr[WRITEBUFMATCHLISTLEN];
char *mch[WRITEBUFMATCHLISTLEN];
clr[0] = "clr0"; mch[0] = "abcdefghijklmnopqrstuvwxyz";
clr[1] = "clr1"; mch[1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
clr[2] = "clr2"; mch[2] = "{};";
clr[3] = "clr3"; mch[3] = "()[]:,.=~#$%@";
clr[4] = "clr4"; mch[4] = "'\"";
clr[5] = "clr5"; mch[5] = "0123456789";
clr[6] = "clr6"; mch[6] = "\\<>+-*/^_`&|!?";
clr[7] = "clr9"; mch[7] = " \t";
#define WRITEBUFBLACKLISTLEN 8
char *blxx[WRITEBUFBLACKLISTLEN];
char *blx = "<>\" \t\\\r~";
blxx[0] = "<";
blxx[1] = ">";
blxx[2] = """;
blxx[3] = " ";
blxx[4] = " ";
blxx[5] = "\";
blxx[6] = "</div><div class=\"subline\">";
blxx[7] = "</div><div class=\"subline\">";
char trmode = '\0';
char submode = '-';
char *trclr = "clr7";
char *limclr = "clr6";
char *subclr = "clr8";
char *rightsquareclr = "rsqb";
char *dashclr = "dash";
char *scoreclr = "score";
char *equalsclr = "equals";
char *actionclr = "action";
char *actendclr = "actend";
char *spaceclr = clr[7];
char *wordclr = clr[0];
char *commandclr = clr[1];
char *datasrcclr = clr[5];
char *tagclr = "tag";
char *tagnameclr = "clr5";
char activeline = 'n';
char commandname = 'n';
char datasrcname = 'n';
char tagname = 'n';
char actionlist = 'n';
char actionname = 'n';
char lschar = '\0';
char tschar = '\n'; // for sol matches
char trchar = '%';
char tbchar = ']';
char *selclr = "";
char *lsclr = selclr;
char subline = 'n';
char *subclass = NULL;
int charsum = -1;
int bfi = -1;
char *linestart = "<div class=\"line\">";
char *linestop = "</div>";
if (basecode == 1) {
charsum = stringbasecodechecksum(linestart, basebuf, -1);
fprintf(stdout, "%s", basebuf);
} else { fprintf(stdout, "%s", linestart); }
if (basecode == 1) // SOL wisp
{ fprintf(stdout, "AA"); }
while (buf[++bfi] != '\0') {
int bfsfmt = 0;
int bfn = -1; while (++bfn < WRITEBUFMATCHLISTLEN) {
int bfa = -1; while (mch[bfn][++bfa] != '\0') {
if (mch[bfn][bfa] == buf[bfi]) {
lschar = tschar;
lsclr = selclr;
tschar = buf[bfi];
selclr = (trmode == '\0' || tschar == '*' || tschar == '/') ? clr[bfn] : trclr;
if (trmode == '\n' && tschar == '\n') { trmode = '\0'; }
if (trmode == '\t' && lschar == '*' && tschar == '/') { trmode = '\0'; }
if (lschar == '/' || lschar == '#' || lschar == '$' || lschar == '%')
{ if (trmode == '\0' && tschar == '/') { trmode = '\n'; trchar = lschar; } }
if (submode == '[' && lschar == '[' && tschar == '.') { submode = '.'; }
else if (submode == '[') { submode = '-'; }
if (trmode == '\0' && lschar == '\n' && tschar == '[') { submode = '['; }
if (submode == '.' && tschar == ']') { submode = '-'; }
else if (submode == '.') { selclr = subclr; }
if ((trmode == '\0' || trmode == '\n') && lschar == '/' && tschar == '*') { trmode = '\t'; }
if (selclr == trclr && tschar == '[') { selclr = limclr; tbchar = '['; }
if (selclr == trclr && tschar == ']') { selclr = limclr; tbchar = ']'; }
if (selclr == trclr && tschar == '.') { selclr = limclr; } // clr[bfn]; }
if (tbchar == ']' && trchar != '/' && tschar == ' ') { trmode = '\0'; }
if (lschar == '\n' && tschar == ']') { selclr = rightsquareclr; activeline = 'y'; }
if (lsclr == rightsquareclr && tschar == '-') { selclr = dashclr; activeline = 'n'; }
if (lsclr == rightsquareclr && tschar == ':') { selclr = scoreclr; commandname = 'y'; }
if (lsclr == rightsquareclr && tschar == '=') { selclr = equalsclr; datasrcname = 'y'; }
// if (lsclr == rightsquareclr && tschar == '[') { actionlist = 'y'; }
if (activeline == 'n' && tschar == '[') { actionname = 'y'; selclr = actionclr; }
else if (actionname == 'y' && tschar == ']') { actionname = 'n'; selclr = actendclr; }
else if (actionname == 'y') { selclr = wordclr; }
if (activeline == 'y' && tschar == '.') { tagname = 'y'; selclr = tagclr; }
else if (tagname == 'y' && (tschar < 'a' || tschar > 'z')) { tagname = 'n'; }
else if (tagname == 'y') { selclr = tagnameclr; }
if (commandname == 'y' && selclr == wordclr) { selclr = commandclr; }
if (commandname == 'y' && selclr == spaceclr) { commandname = 'n'; }
if (datasrcname == 'y' && selclr == wordclr) { selclr = datasrcclr; }
if (datasrcname == 'y' && selclr == spaceclr) { datasrcname = 'n'; }
if (datasrcname == 'y' && selclr == wordclr) { selclr = datasrcclr; }
if (datasrcname == 'y' && selclr == spaceclr) { datasrcname = 'n'; }
int bfxs = 0, bfx = -1;
if (subline == 'h') {
if (buf[bfi] == '>') { subline = '>'; subclass = "output"; }
else if (buf[bfi] == '<') { subline = '<'; subclass = "input"; }
else if (buf[bfi] == ':') { subline = ':'; subclass = "label"; }
else if (buf[bfi] == '|') { subline = '|'; subclass = "command"; }
else if (buf[bfi] == '@') { subline = '@'; subclass = "context"; }
else { subline = 'y'; }
if (subclass != NULL) { selclr = subclass; }
} else if (buf[bfi] == '~') { subline = 'h'; subclass = NULL; }
while (subclass == NULL && blx[++bfx] != '\0') {
if (blx[bfx] == buf[bfi]) { bfxs = 1; break; }
}
if (basecode == 1) {
if (bfxs == 0) { sprintf(charbuf, cfmt, selclr, buf[bfi]); }
if (bfxs == 1) { sprintf(charbuf, sfmt, selclr, blxx[bfx]); }
charsum = stringbasecodechecksum(charbuf, basebuf, -1);
fprintf(stdout, "%s", basebuf);
} else {
if (bfxs == 0) { sprintf(charbuf, cescfmt, selclr, buf[bfi]); }
if (bfxs == 1) { sprintf(charbuf, sescfmt, selclr, blxx[bfx]); }
fprintf(stdout, "%s", charbuf);
}
bfsfmt = 1;
}
}
}
if (bfsfmt == 0) {
if (basecode == 1) {
sprintf(charbuf, "%c", buf[bfi]);
charsum = stringbasecodechecksum(charbuf, basebuf, -1);
fprintf(stdout, "%s", basebuf);
} else {
sprintf(charbuf, "%c", buf[bfi]);
fprintf(stdout, "%s", charbuf);
}
bfsfmt = 0; // reset after each char (changed)
}
}
if (basecode == 1) {
charsum = stringbasecodechecksum(linestop, basebuf, -1);
fprintf(stdout, "%s", basebuf);
} else { fprintf(stdout, "%s", linestop); }
// if (basecode == 1) // EOL wisp
// { fprintf(stdout, "AA"); }
}