~ashn/cdoc

ref: 389b94d9fc44b48e0e8d073ec956b8b2ff2769bc cdoc/cdoc.c -rw-r--r-- 16.4 KiB
389b94d9ashn Make xalloc less "clever" 9 months ago
                                                                                
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! @file cdoc.c
//! @license 0BSD

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VERSION "0.2"

//! @function usage
//!     Print usage information and exit.
static void
usage(void);
//! @function version
//!     Print version information and exit.
static void
version(void);

//! @macro LINENO
//!     Calculates a 1-indexed line number from the pointer off set of two
//!     pointers to lines in the file.
//! @param start
//!     Starting line of the file.
//! @param cur
//!     Some line in the file.
#define LINENO(/* char** */ start, /* char** */ cur) ((int)(cur - start + 1))

//! @function errorf
//!     Write a formatted error message and exit with EXIT_FAILURE status.
//! @param fmt
//!     Printf-style format string.
//! @param ...
//!     Format arguments.
//! @note
//!     A newline character is automatically written after the formatted error
//!     message.
static void
errorf(char const* fmt, ...);
//! @function xalloc
//!     General purpose allocation & reallocation function that will never
//!     return an invalid pointer.
//!     Behavior is identical to realloc with the following exceptions:
//!     (1) Allocation failure will print an error message and terminate the
//!         program with EXIT_FAILURE status.
//!     (2) The call xalloc(p, 0) is guaranteed to free the memory backing p.
//!     A pointer p returned by xalloc may be freed with xalloc(p, 0) or
//!     free(p).
static void*
xalloc(void* ptr, size_t size);
//! @function read_text_file
//!     Returns the contents of stream as a NUL-terminated heap-allocated
//!     cstring.
//!     Encountering a NUL-byte or read failure will print an error message and
//!     terminate the program with EXIT_FAILURE status.
static char*
read_text_file(FILE* stream);
//! @function text_to_lines
//!     Transform provided NUL-terminated text string into a heap-allocated
//!     NULL-terminated list of NUL-terminated cstrings by replacing newline
//!     characters with NUL bytes.
//! @note
//!     This function modifies text in place.
static char**
text_to_lines(char* text);
//! @function is_hspace
//!     Returns true if c is a horizontal whitespace character.
//!     The characters '\t', '\r', and ' ' are considered horizontal whitespace.
static bool
is_hspace(int c);
//! @function is_doc_line
//!     Returns true the first non-whitespace characters of line are "//!", the
//!     start of a doc-line in cdoc.
//! @param line
//!     NUL-terminated character string.
static bool
is_doc_line(char const* line);
//! @function doc_content_start
//!     Returns a pointer to the non whitespace character after the initial
//!     "//!" in a doc-line.
//!     Terminates the program with EXIT_FAILURE status if line does not
//!     satisfy the definition of a doc-line as defined by is_doc_line.
static char const*
doc_content_start(char const* line);

//! @function do_file
//!     Generate documentation for the provided file.
//! @param fp
//!     File pointer returned from fopen.
//!     The position fp's file offset should be at the beginning of the file.
static void
do_file(FILE* fp);

//! @struct doc
//!     Representation of a document, aka a list of doc-sections documenting the
//!     a single portion of a C source file.
//!
//!     The cdoc documentation:
//!     <pre><code>
//!     //! @struct foo
//!     //!     The struct used for fooing.
//!     //! @note
//!     //!     Be careful not to bar this foo.
//!     struct foo
//!     {
//!         int some_member;
//!     };
//!     </code></pre>
//!     is comprised of two sections (beginning with @variable and @note) and
//!     four lines of source.
struct doc
{
    //! @member sections
    //!     Dynamically allocated list of sections in this doc.
    struct section* sections;
    //! @member section_count
    //!     Number of sections in this doc.
    size_t section_count;
    //! @member source
    //!     Dynamically allocated NULL-terminated list of source code associated
    //!     with this doc.
    //!     A value of NULL implies that this doc has no associated source code.
    char** source;
};
//! @struct section
//!     Representation of a portion of a document, specifically one facet of the
//!     subject of the doc.
//!     Each section has a mandatory tag, an optional name, and optional body
//!     of description text.
//!     The doc-lines:
//!     <pre><code>
//!     //! @struct foo
//!     //!     The struct used for fooing.
//!     </code></pre>
//!     form one section with the tag "struct", the name "foo" and a text body
//!     containing the single line of text "The struct used for fooing.".
struct section
{
    //! @member tag_start
    //!     Location of the first character in the tag slice.
    char const* tag_start;
    //! @member tag_len
    //!     Length of the tag slice.
    int tag_len;

    //! @member name_start
    //!     Location of the first character in the name slice.
    char const* name_start;
    //! @member name_len
    //!     Length of the name slice.
    //!     A length of zero implies that this tag has no name.
    int name_len;

    //! @member text_start
    //!     Location of the first line in the text body.
    char** text_start;
    //! @member text_len
    //!     Length of the text body.
    //!     A length of zero implies that this tag has no body.
    int text_len;
};

//! @function parse_struct_source
//!     Parse the lines of a struct forward declaration or definition using the
//!     provided parse state.
//! @note
//!     The method used for parsing in this function happens to works for
//!     struct, union, enum, typedef, and variable declarations.
//! @todo
//!     Currently this function does not handle comments (C or C++ style)
//!     correctly which may lead to erroneous parsing.
//!     This function should be modified to correctly handle comments.
static char**
parse_struct_source(char*** linepp);
//! @function parse_function_source
//!     Parse the lines of a function prototype or definition using the provided
//!     parse state.
static char**
parse_function_source(char*** linepp);
//! @function parse_macro_source
//!     Parse the lines of a preprocessor macro using the provided parse state.
static char**
parse_macro_source(char*** linepp);
//! @function parse_doc
//!     Construct and return a doc from the provided parse state parameters.
static struct doc
parse_doc(char* const* lines_begin, char*** linepp);
//! @function print_doc
//!     Print a formatted representation of this doc to stdout.
static void
print_doc(struct doc const d);
//! @function parse_section
//!     Construct and return a section from the provided parse state parameters.
static struct section
parse_section(char* const* lines_begin, char*** linepp);
//! @function print_section
//!     Print a formatted representation of this section stdout.
static void
print_section(struct section const s);

int
main(int argc, char** argv)
{
    bool parse_options = true;
    for (int i = 1; i < argc; ++i) {
        char const* const arg = argv[i];
        if (parse_options && strcmp(arg, "--help") == 0) {
            usage();
        }
        if (parse_options && strcmp(arg, "--version") == 0) {
            version();
        }
        if (parse_options && strcmp(arg, "--") == 0) {
            parse_options = false;
            continue;
        }

        bool const use_stdin = strcmp(arg, "-") == 0 && !parse_options;
        FILE* const fp = use_stdin ? stdin : fopen(arg, "rb");
        if (fp == NULL) {
            perror(arg);
            exit(EXIT_FAILURE);
        }
        do_file(fp);
        fclose(fp);
    }

    return EXIT_SUCCESS;
}

static void
usage(void)
{
    // clang-format off
    puts(
        "Usage: cdoc [OPTION]... [--] [FILE]..."                "\n"
                                                                "\n"
        "With no FILE, or when FILE is -, read standard input." "\n"
                                                                "\n"
        "Options:"                                              "\n"
        "  --help      Display usage information and exit."     "\n"
        "  --version   Display version information and exit."   "\n"
    );
    // clang-format on
    exit(EXIT_SUCCESS);
}

static void
version(void)
{
    puts(VERSION);
    exit(EXIT_SUCCESS);
}

static void
errorf(char const* fmt, ...)
{
    if (fmt == NULL) {
        fputs("error: errorf called with NULL format string\n", stderr);
        goto terminate;
    }

    va_list args;
    va_start(args, fmt);
    fputs("error: ", stderr);
    vfprintf(stderr, fmt, args);
    fputs("\n", stderr);
    va_end(args);

terminate:
    // Any error should immediately terminate the program.
    exit(EXIT_FAILURE);
}

static void*
xalloc(void* ptr, size_t size)
{
    if (size == 0) {
        free(ptr);
        return NULL;
    }
    if ((ptr = realloc(ptr, size)) == NULL) {
        errorf("[%s] Out of memory", __func__);
    }
    return ptr;
}

static char*
read_text_file(FILE* stream)
{
    char* buf = NULL;
    size_t len = 0;

    int c;
    while ((c = fgetc(stream)) != EOF) {
        if (c == '\0') {
            errorf("Encountered illegal NUL byte");
        }
        buf = xalloc(buf, len + 1);
        buf[len++] = (char)c;
    }
    if (!feof(stream)) {
        errorf("Failed to read entire text file");
    }

    buf = xalloc(buf, len + 1);
    buf[len++] = '\0';
    return buf;
}

static char**
text_to_lines(char* text)
{
    char** lines = xalloc(NULL, (strlen(text) + 1) * sizeof(char*));
    size_t count = 0;
    lines[count++] = text;

    for (; *text; ++text) {
        if (*text != '\n') {
            continue;
        }
        *text = '\0';
        lines[count++] = text + 1;
    }
    lines[count - 1] = NULL;

    return lines;
}

static bool
is_hspace(int c)
{
    return c == '\t' || c == '\r' || c == ' ';
}

static bool
is_doc_line(char const* line)
{
    if (line == NULL) {
        return false;
    }
    while (is_hspace(*line)) {
        line += 1;
    }
    if (*line++ != '/') {
        return false;
    }
    if (*line++ != '/') {
        return false;
    }
    if (*line++ != '!') {
        return false;
    }
    return true;
}

static char const*
doc_content_start(char const* cp)
{
    while (is_hspace(*cp)) { // Whitespace before '//!'
        cp += 1;
    }
    if (cp[0] != '/' || cp[1] != '/' || cp[2] != '!') {
        errorf("[%s] Received non-doc-line", __func__);
    }
    cp += 3; // Length of "//!"
    while (is_hspace(*cp)) {
        cp += 1; // Whitespace after '//!'
    }
    return cp;
}

static void
do_file(FILE* fp)
{
    char* const text = read_text_file(fp);
    char** const lines = text_to_lines(text);
    char** linep = lines; // current line

    // PARSE
    struct doc* docs = NULL;
    size_t doc_count = 0;
    while (*linep != NULL) {
        if (!is_doc_line(*linep)) {
            linep += 1;
            continue;
        }
        docs = xalloc(docs, (doc_count + 1) * sizeof(*docs));
        docs[doc_count++] = parse_doc(lines, &linep);
    }

    // PRINT
    for (size_t i = 0; i < doc_count; ++i) {
        print_doc(docs[i]);
    }

    // CLEANUP
    for (size_t i = 0; i < doc_count; ++i) {
        free(docs[i].sections);
        free(docs[i].source);
    }
    free(docs);
    free(text);
    free(lines);
}

static char**
parse_struct_source(char*** linepp)
{
    char** lines = NULL;
    size_t count = 0;

    bool parsed = false; // Are we finished parsing the source?
    int brackets = 0; // Number of '{' minus number of '}'.
    for (; !parsed; *linepp += 1) {
        if (*linepp == NULL) {
            errorf("Unexpected end-of-file");
        }
        lines = xalloc(lines, (count + 1) * sizeof(char*));
        lines[count++] = **linepp;

        for (char* cp = **linepp; *cp != '\0'; ++cp) {
            brackets += *cp == '{';
            brackets -= *cp == '}';
            if (brackets == 0 && *cp == ';') {
                parsed = true;
            }
        }
    }

    lines = xalloc(lines, (count + 1) * sizeof(char*));
    lines[count] = NULL;
    return lines;
}

static char**
parse_function_source(char*** linepp)
{
    char** lines = NULL;
    size_t count = 0;

    bool parsed = false;
    for (; !parsed; *linepp += 1) {
        if (*linepp == NULL) {
            errorf("Unexpected end-of-file");
        }
        lines = xalloc(lines, (count + 1) * sizeof(char*));
        lines[count++] = **linepp;

        for (char* cp = **linepp; *cp != '\0'; ++cp) {
            if (*cp == ';') {
                parsed = true;
                break;
            }
            else if (*cp == '{') {
                parsed = true;
                lines = xalloc(lines, (count + 1) * sizeof(char*));
                lines[count++] = "/* function definition... */";
            }
            if (*cp == '{' || *cp == ';') {
                parsed = true;
            }
        }
    }

    lines = xalloc(lines, (count + 1) * sizeof(char*));
    lines[count] = NULL;
    return lines;
}

static char**
parse_macro_source(char*** linepp)
{
    char** lines = NULL;
    size_t count = 0;

    bool parsed = false;
    for (; !parsed; *linepp += 1) {
        if (*linepp == NULL) {
            errorf("Unexpected end-of-file");
        }
        lines = xalloc(lines, (count + 1) * sizeof(char*));
        lines[count++] = **linepp;
        parsed = (**linepp)[strlen(**linepp) - 1] != '\\';
    }

    lines = xalloc(lines, (count + 1) * sizeof(char*));
    lines[count] = NULL;
    return lines;
}

static struct doc
parse_doc(char* const* lines_begin, char*** linepp)
{
    struct doc d = {0};
    while (is_doc_line(**linepp)) {
        d.sections =
            xalloc(d.sections, (d.section_count + 1) * sizeof(*d.sections));
        d.sections[d.section_count++] = parse_section(lines_begin, linepp);
    }

    char const* const start = d.sections[0].tag_start;
    size_t const len = (size_t)d.sections[0].tag_len;
#define DOC_IS(tag) (len == strlen(tag) && strncmp(start, tag, len) == 0)
    if (DOC_IS("struct") || DOC_IS("union") || DOC_IS("enum")
        || DOC_IS("typedef") || DOC_IS("variable")) {
        d.source = parse_struct_source(linepp);
        char** start = d.source;
        char*** cur = &start;
        while (**cur) {
            if (!is_doc_line(**cur)) {
                *cur += 1;
                continue;
            }
            d.sections =
                xalloc(d.sections, (d.section_count + 1) * sizeof(*d.sections));
            d.sections[d.section_count++] = parse_section(lines_begin, cur);
        }
    }
    else if (DOC_IS("function")) {
        d.source = parse_function_source(linepp);
    }
    else if (DOC_IS("macro")) {
        d.source = parse_macro_source(linepp);
    }
#undef DOC_IS

    return d;
}

static void
print_doc(struct doc const d)
{
    for (size_t i = 0; i < d.section_count; ++i) {
        print_section(d.sections[i]);
    }
    if (d.source != NULL) {
        puts("<pre><code>");
        char** ln = d.source;
        while (*ln != NULL) {
            puts(*ln++);
        }
        puts("</code></pre>");
    }
    puts("<hr>");
}

static struct section
parse_section(char* const* lines_begin, char*** linepp)
{
    struct section s = {0};
    char const* cp = doc_content_start(**linepp);
    if (*cp++ != '@') {
        errorf(
            "[line %d] Doc-section must begin with @<TAG>",
            LINENO(lines_begin, *linepp));
    }
    if (is_hspace(*cp) || *cp == '\0') {
        errorf("[line %d] Empty doc-comment tag", LINENO(lines_begin, *linepp));
    }

    // TAG
    s.tag_start = cp;
    while (!is_hspace(*cp) && (*cp != '\0')) {
        cp += 1;
    }
    s.tag_len = (int)(cp - s.tag_start);

    while (is_hspace(*cp)) {
        cp += 1;
    }

    // NAME
    s.name_start = cp;
    while (!is_hspace(*cp) && (*cp != '\0')) {
        cp += 1;
    }
    s.name_len = (int)(cp - s.name_start);

    while (is_hspace(*cp)) {
        cp += 1;
    }
    if (*cp != '\0') {
        errorf(
            "[line %d] Extra character(s) after tag line <NAME>",
            LINENO(lines_begin, *linepp));
    }

    // TEXT
    *linepp += 1;
    s.text_start = *linepp;
    while (is_doc_line(**linepp) && *doc_content_start(**linepp) != '@') {
        *linepp += 1;
    }
    s.text_len = (int)(*linepp - s.text_start);

    return s;
}

static void
print_section(struct section const s)
{
    printf(
        "<h3>%.*s: %.*s</h3>\n",
        s.tag_len,
        s.tag_start,
        s.name_len,
        s.name_start);
    for (int i = 0; i < s.text_len; ++i) {
        char const* const start = doc_content_start(s.text_start[i]);
        puts(start);
    }
}