src/c2html.c

Tue, 23 Aug 2016 14:31:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 14:31:02 +0200
changeset 43
a8cee98c8832
parent 42
7f2403c637a7
child 44
2b4ac35d061d
permissions
-rw-r--r--

cleans up main function

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2016 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include "c2html.h"

#include "ucx/buffer.h"
#include "ucx/list.h"
#include "ucx/utils.h"

void printhelp() {
    printf("Formats source code using HTML.\n\nUsage:\n"
        "  c2html [Options] FILE\n\n"
        " Options:\n"
        "  -h                    Prints this help message\n"
        "  -j                    Highlight Java instead of C source code\n"
        "  -o <output>           Output file (stdout, if not specified)\n"
        "  -H <header>           Prepend header file\n"
        "  -F <footer>           Append footer file\n"
        "  -p                    Disable highlighting (plain text)\n"
        "  -l                    Disable line numbers\n"
        "  -V, -v                Prints version and exits\n"
        "\n");
}

#define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
int formatfile(
        highlighter_t *highlighter,
        UcxList *in,
        write_func out,
        void *stream,
        int showlineno) {
    size_t lines = ucx_list_size(in);
    
    UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
    if(!line) {
        return 1;
    }
    WRITECONST(stream, out, "<pre>\n");
    
    int lnw;
    {
        lnw = 1;
        int p = 1;
        while ((p*=10) < lines) lnw++;
    }

    size_t lineno = 0;
    UCX_FOREACH(sourceline, in) {
        lineno++;
        /* TODO: backwards compatibility: replace line->space in all occasions
         * and use UcxBuffer functions
         */
        if (highlighter) {
            highlighter->parser(sourceline->data, line->space, highlighter);
        } else {
            char *c = sourceline->data;
            size_t dp = 0;
            while (*c && *c != '\n') {
                dp = writeescapedchar(line->space, dp, *c);
                c++;
            }
            line->space[dp++] = '\n';
            line->space[dp] = '\0';
        }

        // write line number
        if (showlineno) {
            WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
            char lnbuf[128];
            int len;
            // line number link
            len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">",
                lineno, lineno);
            out(lnbuf, 1, len, stream);
            // justified line number
            len = snprintf(lnbuf, 128, "%*d ", lnw, lineno);
            out(lnbuf, 1, len, stream);
            WRITECONST(stream, out, "</a></span> ");
        }
        
        // write formated (or plain) code line
        out(line->space, 1, strlen(line->space), stream);
    }
    
    WRITECONST(stream, out, "</pre>\n");
    ucx_buffer_free(line);
    return 0;
}

void init_c_highlighter(highlighter_t *highlighter) {
    memset(highlighter, 0, sizeof(highlighter_t));
    highlighter->isdirective = check_cdirective;
    highlighter->istype = check_ctype;
    highlighter->keywords = ckeywords;
    highlighter->parser = cparseline;
}

void init_java_highlighter(highlighter_t *highlighter) {
    memset(highlighter, 0, sizeof(highlighter_t));
    highlighter->isdirective = check_jdirective;
    highlighter->istype = check_jtype;
    highlighter->keywords = jkeywords;
    highlighter->parser = jparseline;
}

#define FILEBUF_SIZE 4096

enum source_type {
    SOURCE_C,
    SOURCE_JAVA,
    SOURCE_PLAIN
};

int main(int argc, char** argv) {

    /* Default settings */
    Settings settings;
    memset(&settings, 0, sizeof(settings));
    settings.showlinenumbers = 1;
    enum source_type sourcetype = SOURCE_C;

    /* Parse command line */
    char optc;
    while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
        switch (optc) {
            case 'o':
                if (!(optarg[0] == '-' && optarg[1] == 0)) {
                    settings.outfilename = optarg;
                }
                break;
            case 'F':
                settings.footerfile = optarg;
                break;
            case 'H':
                settings.headerfile = optarg;
                break;
            case 'j':
                sourcetype = SOURCE_JAVA;
                break;
            case 'p':
                sourcetype = SOURCE_PLAIN;
                break;
            case 'l':
                settings.showlinenumbers = 0;
                break;
            case 'h':
                printhelp();
                return EXIT_SUCCESS;
            case 'v':
            case 'V':
#ifdef VERSION_DEVELOP
                printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR);
#else
                printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
#endif
                return EXIT_SUCCESS;
            default:
                return EXIT_FAILURE;
        }
    }

    if (optind != argc-1) {
        printhelp();
        return EXIT_FAILURE;
    } else {
        /* Configure highlighter */
        highlighter_t highlighter;
        highlighter_t *hptr = &highlighter;
        switch (sourcetype) {
            case SOURCE_C:
                init_c_highlighter(&highlighter);
                break;
            case SOURCE_JAVA:
                init_java_highlighter(&highlighter);
                break;
            case SOURCE_PLAIN:
                hptr = NULL;
                break;
            default: /* should be unreachable */
                fprintf(stderr, "error in enum source_type\n");
                return EXIT_FAILURE;
        }
        
        /* Open output file */
        settings.infilename = argv[optind];
        FILE *fout;
        if (settings.outfilename) {
            fout = fopen(settings.outfilename, "w");
            if (!fout) {
                perror("Error opening output file");
                return EXIT_FAILURE;
            }
        } else {
            fout = stdout;
        }
        
        /* Allocate file buffer  */
        char *filebuf = malloc(FILEBUF_SIZE);
        if (!filebuf) {
            perror("Error allocating file buffer");
            return EXIT_FAILURE;
        }
        
        /* Prepend header file */
        {
            FILE *headerfile = fopen(settings.headerfile, "r");
            if (!headerfile) {
                perror("Error opening header file");
                if (fout != stdout) {
                    fclose(fout);
                }
                return EXIT_FAILURE;
            }
            ucx_stream_copy(headerfile, fout,
                    (read_func) fread, (write_func) fwrite,
                    filebuf, FILEBUF_SIZE, (size_t)-1);
            fclose(headerfile);
        }

        /* Process input file */
        FILE *inputfile = fopen(settings.infilename, "r");
        if (inputfile) {
            UcxBuffer *content = ucx_buffer_new(NULL,
                    FILEBUF_SIZE*2, UCX_BUFFER_AUTOEXTEND);
            {
                ucx_stream_copy(inputfile, content, (read_func) fread,
                        (write_func) ucx_buffer_write,
                        filebuf, FILEBUF_SIZE, (size_t)-1);
            }
            fclose(inputfile);
            
            UcxList *inputlines = ucx_list_append(NULL, content->space);
            for (size_t i = 1 ; i < content->size ; i++) {
                if (content->space[i] == '\r') {
                    content->space[i] = '\n'; i++;
                }
                if (content->space[i] == '\n' && i+1 < content->size) {
                    ucx_list_append(inputlines, content->space+i+1);
                }
            }
            
            formatfile(
                    hptr,
                    inputlines,
                    (write_func) fwrite,
                    fout,
                    settings.showlinenumbers);
            ucx_buffer_free(content);
        } else {
            perror("Error opening input file");
            if (fout != stdout) {
                fclose(fout);
            }
            return EXIT_FAILURE;
        }
        
        /* Append footer file */
        {
            FILE *footerfile = fopen(settings.footerfile, "r");
            if (!footerfile) {
                perror("Error opening footer file");
                if (fout != stdout) {
                    fclose(fout);
                }
                return EXIT_FAILURE;
            }
            ucx_stream_copy(footerfile, fout,
                    (read_func) fread, (write_func) fwrite,
                    filebuf, FILEBUF_SIZE, (size_t)-1);
            fclose(footerfile);
        }

        
        free(filebuf);

        return EXIT_SUCCESS;
    }
}

mercurial