universe@23: /* universe@23: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@23: * universe@35: * Copyright 2016 Mike Becker. All rights reserved. universe@23: * universe@23: * Redistribution and use in source and binary forms, with or without universe@23: * modification, are permitted provided that the following conditions are met: universe@23: * universe@23: * 1. Redistributions of source code must retain the above copyright universe@23: * notice, this list of conditions and the following disclaimer. universe@23: * universe@23: * 2. Redistributions in binary form must reproduce the above copyright universe@23: * notice, this list of conditions and the following disclaimer in the universe@23: * documentation and/or other materials provided with the distribution. universe@23: * universe@23: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@23: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@23: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@23: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@23: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@23: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@23: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@23: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@23: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@23: * POSSIBILITY OF SUCH DAMAGE. universe@23: * universe@23: */ universe@22: #include "c2html.h" universe@1: universe@1: inputfile_t *inputfilebuffer(size_t capacity) { universe@19: inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t)); universe@19: inputfile->lines = (char**) malloc(capacity * sizeof(char*)); universe@19: inputfile->capacity = capacity; universe@19: inputfile->count = 0; universe@19: inputfile->maxlinewidth = 0; universe@19: universe@19: return inputfile; universe@0: } universe@0: universe@1: void addline(inputfile_t *inputfile, char* line, size_t width) { universe@19: char *l = (char*) malloc(width+1); universe@19: memcpy(l, line, width); universe@19: l[width] = 0; universe@19: if (inputfile->count >= inputfile->capacity) { universe@19: inputfile->capacity <<= 1; universe@25: inputfile->lines = realloc(inputfile->lines, universe@25: sizeof(char*)*inputfile->capacity); universe@19: } universe@19: inputfile->lines[inputfile->count] = l; universe@19: inputfile->maxlinewidth = universe@19: width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth; universe@19: inputfile->count++; universe@1: } universe@1: universe@1: void freeinputfilebuffer(inputfile_t *inputfile) { universe@19: for (int i = 0 ; i < inputfile->count ; i++) { universe@19: free(inputfile->lines[i]); universe@19: } universe@19: free(inputfile->lines); universe@19: free(inputfile); universe@1: } universe@1: universe@1: inputfile_t *readinput(char *filename) { universe@1: universe@19: int fd = open(filename, O_RDONLY); universe@19: if (fd == -1) return NULL; universe@1: universe@19: inputfile_t *inputfile = inputfilebuffer(512); universe@19: universe@19: char buf[INPUTBUF_SIZE]; universe@19: ssize_t r; universe@19: universe@19: size_t maxlinewidth = 256; universe@19: char *line = (char*) malloc(maxlinewidth); universe@19: size_t col = 0; universe@19: universe@19: while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) { universe@19: for (size_t i = 0 ; i < r ; i++) { universe@19: if (col >= maxlinewidth-4) { universe@19: maxlinewidth <<= 1; universe@19: line = realloc(line, maxlinewidth); universe@19: } universe@19: universe@19: if (buf[i] == '\n') { universe@19: line[col++] = '\n'; universe@19: line[col] = 0; universe@19: addline(inputfile, line, col); universe@19: col = 0; universe@19: } else { universe@19: line[col++] = buf[i]; universe@19: } universe@19: } universe@1: } universe@19: universe@19: free(line); universe@19: universe@19: close(fd); universe@19: universe@19: return inputfile; universe@1: } universe@1: universe@1: void printhelp() { universe@19: printf("Formats source code using HTML.\n\nUsage:\n" universe@19: " c2html [Options] FILE\n\n" universe@19: " Options:\n" universe@19: " -h Prints this help message\n" universe@19: " -j Highlight Java instead of C source code\n" universe@19: " -o Output file (stdout, if not specified)\n" universe@22: " -H
Prepend header file\n" universe@22: " -F