Repository navigation
Expand file tree
/
Copy pathbbcode.c
More file actions
496 lines (435 loc) · 10.9 KB
/
Copy pathbbcode.c
File metadata and controls
496 lines (435 loc) · 10.9 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/**
* bbcode.c
* Compiles BBCode into an intermediate language that can later be used by other
* applications to convert it into other markup languages.
*
* @author Nathan Campos <nathan@innoveworkshop.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "atom.h"
#include "utils.h"
#include "shims.h"
/**
* The maximum number of characters, inclusing the NUL terminator, that a tag
* name may contain.
*/
#define MAX_TAG_NAME_LEN 15
/**
* Lexing stages.
*/
typedef enum lexer_stage_e {
LEXER_START = -1,
LEXER_STRING = 0,
LEXER_INTAG,
LEXER_INARG,
LEXER_OUTTAG
} lexer_stage_t;
/* Function prototypes. */
int lex(const char *str, const char **start, const char **end);
char** lexer(const char *code);
atom_t* parse_atom(char **tokens, char ***next);
char** parse_tag(atom_t *atom, char **tokens);
char** parse_prefmt(atom_t *atom, char **tokens);
/**
* Application's main entry point.
*
* @param argc Number of command line arguments passed.
* @param argv Array of command line arguments.
*
* @return Return code.
*/
int main(int argc, char **argv) {
char *code;
char *line;
size_t len;
char **tokens;
char **cur;
FILE *stream;
/* Take a memory snapshot for memory leak detection. */
mem_snapshot_start();
/* Check if we should read from a file. */
stream = stdin;
if (argc > 1) {
stream = fopen(argv[1], "r");
if (stream == NULL) {
fprintf(stderr, "error: failed to open file '%s' for reading.\n",
argv[1]);
return 1;
}
}
/* Read entire file into memory so that it can be tokenized later. */
line = malloc(LINE_MAX * sizeof(char));
code = malloc(LINE_MAX * sizeof(char));
*code = '\0';
len = 0;
while (fgets(line, sizeof(line), stream)) {
size_t linelen = strlen(line);
code = realloc(code, (len + linelen + 1) * sizeof(char));
strcat(code + len, line);
len += linelen;
}
/* Close the file stream if needed. */
if (stream != stdin) {
fclose(stream);
stream = NULL;
}
/* Pass BBCode through the lexer to extract tokens, then free resources. */
free(line);
tokens = lexer(code);
free(code);
line = NULL;
code = NULL;
/* Parse atoms and print them out. */
cur = tokens;
while (*cur != NULL) {
atom_t *atom = parse_atom(cur, &cur);
if (!atom)
continue;
atom_print(atom);
atom_free(atom);
atom = NULL;
}
/* Free the tokens list. */
free(tokens);
/* Check for memory leaks. */
mem_snapshot_report();
return 0;
}
/**
* Lexes the entire code provided.
*
* @param code BBCode string to be lexed.
*
* @return List of tokens lexed from the provided string.
*/
char** lexer(const char *code) {
lexer_stage_t stage;
const char *start;
const char *end;
char **tokens;
int count;
int ret;
/* Allocate our initial token list. */
count = 1;
tokens = malloc(sizeof(char*));
*tokens = NULL;
stage = LEXER_START;
start = code;
while ((ret = lex(start, &start, &end)) > 0) {
switch (stage) {
case LEXER_INARG:
/* Opening tags always generate a new token. For code blocks. */
if (*start == '[')
goto new_token;
/* Treat all tokens that are not ']' as a single token. */
if ((*tokens[count - 2] != '=') && (*start != ']')) {
strcatdelim_alloc(&tokens[count - 2], start, end);
count--;
break;
}
goto new_token;
case LEXER_STRING:
/* Newlines are always new tokens. */
if (*start == '\n')
goto new_token;
/* Join all tokens until we reach a tag opening. */
if (*start != '[') {
strcatdelim_alloc(&tokens[count - 2], start, end);
count--;
break;
}
goto new_token;
case LEXER_START:
/* Start of a brand new string. */
stage = LEXER_STRING;
goto new_token;
case LEXER_INTAG:
case LEXER_OUTTAG:
new_token:
strcatdelim_alloc(&tokens[count - 1], start, end);
break;
}
#if defined(_DEBUG)
fprintf(stderr, "LEXER %d \"%s\" %d\n", stage, tokens[count - 1],
count);
#endif /* _DEBUG */
/* Change lexer stage if needed. */
if (*start == '\n') {
stage = LEXER_START;
} else if (*start == '[') {
stage = LEXER_INTAG;
} else if ((*start == '=') && (stage == LEXER_INTAG)) {
stage = LEXER_INARG;
} else if ((stage >= LEXER_INTAG) && (*start == ']')) {
stage = LEXER_OUTTAG;
} else if (stage == LEXER_OUTTAG) {
stage = LEXER_STRING;
}
/* Move to the next token in the string. */
start = end;
count++;
/* Reset all buffers. */
tokens = realloc(tokens, count * sizeof(char*));
tokens[count - 1] = NULL;
}
return tokens;
}
/**
* A very simple lexer to find the beginning and the end of tokens in a string.
*
* @param str String to be scanned for tokens.
* @param start Pointer to the start of a token.
* @param end Pointer to the end of a token.
*
* @return The size of our token if successful. 0 if we've reached the end of
* the string. Less than 0 if an error occurred.
*/
int lex(const char *str, const char **start, const char **end) {
const char *delim = "[]/=\n";
/* Are we wasting our time? */
if (str == NULL)
return -1;
/* Ignore CR characters. */
str += strspn(str, "\r");
/* Check if we've reached the end of the string. */
if (*str == '\0') {
*start = *end = NULL;
return 0;
}
/* Read the delimiter or a string until we reach the next delimiter. */
*start = str;
if (chrchr(*str, delim)) {
*end = str + 1;
} else {
*end = str + strcspn(str, delim);
}
return (int)(*end - *start);
}
/**
* Parses the next atom from the tokens list.
*
* @warning This function will free unused tokens from the tokens list.
*
* @param tokens Tokens list.
* @param next Pointer to where the next token to be parsed.
*
* @return Parsed a dynamically allocated atom object. NULL if a token to be
* ignored was parsed.
*/
atom_t* parse_atom(char **tokens, char ***next) {
atom_t *atom;
static atom_type_t last_type;
static int prefmt_stat;
static char last_open_tag[MAX_TAG_NAME_LEN];
/* Create atom object. */
atom = atom_new(-1);
/* Deal with code/preformatted sections. */
if (prefmt_stat == 2) {
/* Parse the preformatted section atom. */
atom->type = ATOM_PREFMT;
*next = parse_prefmt(atom, tokens);
prefmt_stat = -1;
goto store_atom;
} else if (prefmt_stat == -1) {
/* Close the code tag that was gobbled while parsing the section. */
atom->type = ATOM_TAG_CLOSE;
atom->value = strdup(last_open_tag);
prefmt_stat = 0;
goto store_atom;
}
/* Parse atom based on the first character of the current token. */
switch (**tokens) {
case '[':
/* Tag */
free(*tokens);
*tokens = NULL;
*next = parse_tag(atom, tokens + 1);
break;
case ']':
/* Closing brace of tag. */
if ((last_type >= ATOM_TAG_OPEN) || (last_type == ATOM_ESCAPED)) {
/* Ignore this token. */
free(*tokens);
*tokens = NULL;
*next = tokens + 1;
free(atom);
atom = NULL;
return NULL;
}
/* Regular text atom that starts with an closing brace. */
goto text_atom;
case '=':
/* Tag argument if last token was a tag open. */
if (last_type == ATOM_TAG_OPEN) {
/* Change atom type and ignore token. */
atom->type = ATOM_TAG_ARG;
free(*tokens);
*tokens = NULL;
/* Use next token as the atom's value. */
atom->value = *(tokens + 1);
/* Jump ahead of the value token. */
*next = tokens + 2;
break;
}
/* Regular text atom that starts with an equal sign. */
goto text_atom;
case '\n':
/* Linebreak */
atom->type = ATOM_LINEBREAK;
atom->value = NULL;
/* Ignore the current token and jump to the next one. */
free(*tokens);
*tokens = NULL;
*next = tokens + 1;
break;
default:
/* Text */
text_atom:
atom->type = ATOM_RAW;
atom->value = *tokens;
/* Jump to the next token. */
*next = tokens + 1;
break;
}
/* Determine if we are about to parse a preformatted section. */
if ((atom->type == ATOM_TAG_OPEN) && (!strcmp(atom->value, "CODE") ||
!strcmp(atom->value, "PRE"))) {
/* Ignore closing brace of code tag. */
if (***next == ']') {
free(**next);
**next = NULL;
*next = *next + 1;
/* Mark the beginning of a preformatted section. */
prefmt_stat = 2;
} else {
/* Mark the beginning of an argument for a preformatted section. */
prefmt_stat = 1;
}
} else if (prefmt_stat == 1) {
/* Guess we've just finished parsing the argument of a code section. */
free(**next);
**next = NULL;
*next = *next + 1;
prefmt_stat = 2;
} else {
prefmt_stat = 0;
}
store_atom:
/* Store the last atom type and tag open. */
last_type = atom->type;
if (atom->type == ATOM_TAG_OPEN)
strncpy(last_open_tag, atom->value, MAX_TAG_NAME_LEN);
return atom;
}
/**
* Parses an opening or closing of a tag.
*
* @param atom Atom object to be populated.
* @param tokens Tokens list to be used.
*
* @return Position of the next token after the atom.
*/
char** parse_tag(atom_t *atom, char **tokens) {
char *tmp;
char **next;
/* Check if we are closing or opening a tag. */
next = tokens;
if (**next == '/') {
atom->type = ATOM_TAG_CLOSE;
free(*next);
*next = NULL;
next++;
} else {
atom->type = ATOM_TAG_OPEN;
}
/* Extract value and normalize tag value. */
atom->value = *next;
next++;
tmp = atom->value;
while (*tmp) {
*tmp = toupper((unsigned char)*tmp);
tmp++;
}
/* Ignore cases where the user wants to print a bracket. */
if (*atom->value == '[')
atom->type = ATOM_ESCAPED;
return next;
}
/**
* Parses an entire preformatted section of tokens.
*
* @param atom Atom object to be populated.
* @param tokens Tokens list to be used.
*
* @return Position of the next token after the atom.
*/
char** parse_prefmt(atom_t *atom, char **tokens) {
int close_seq;
char **next;
size_t len;
/* Prepare the atom's value to be populated. */
atom->value = malloc(sizeof(char));
*atom->value = '\0';
len = 1;
/* Join all tokens until we reach the '[', '/', "code", ']' sequence. */
close_seq = 0;
next = tokens;
while (close_seq < 4) {
/* Store place of NUL terminator to shortcircuit concatenation. */
size_t lenv = len - 1;
switch (close_seq) {
case 0:
/* Looking for '['. */
if (**next == '[') {
close_seq++;
} else {
close_seq = 0;
}
break;
case 1:
/* Looking for '/'. */
if (**next == '/') {
close_seq++;
} else {
close_seq = 0;
}
break;
case 2:
/* Looking for "code". */
if (!strcmpci(*next, "code") || !strcmpci(*next, "pre")) {
close_seq++;
} else {
close_seq = 0;
}
break;
case 3:
/* Looking for ']'. */
if (**next == ']') {
close_seq++;
} else {
close_seq = 0;
}
break;
}
/* Append token to value. */
len += strlen(*next);
atom->value = realloc(atom->value, len * sizeof(char));
strcat(atom->value + lenv, *next);
/* Discard token and move to the next one. */
free(*next);
*next = NULL;
next++;
}
/* Remove the "[/code]" from the end of the preformatted section */
if (!strcmpci(atom->value + len - 6, "code]")) {
len -= 7;
} else if (!strcmpci(atom->value + len - 5, "pre]")) {
len -= 6;
}
atom->value[len - 1] = '\0';
atom->value = realloc(atom->value, len * sizeof(char));
return next;
}