src/c2html.c

Thu, 23 Jan 2014 09:19:37 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Jan 2014 09:19:37 +0100
changeset 21
537aec525835
parent 20
ebbf0776c1bc
child 22
f463693b5eeb
permissions
-rw-r--r--

structured source code

universe@1 1 /*
universe@1 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@1 3 *
universe@21 4 * Copyright 2014 Mike Becker. All rights reserved.
universe@1 5 *
universe@1 6 * Redistribution and use in source and binary forms, with or without
universe@1 7 * modification, are permitted provided that the following conditions are met:
universe@1 8 *
universe@1 9 * 1. Redistributions of source code must retain the above copyright
universe@1 10 * notice, this list of conditions and the following disclaimer.
universe@1 11 *
universe@1 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@1 13 * notice, this list of conditions and the following disclaimer in the
universe@1 14 * documentation and/or other materials provided with the distribution.
universe@1 15 *
universe@1 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@1 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@1 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@1 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@1 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@1 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@1 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@1 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@1 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@1 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@1 26 * POSSIBILITY OF SUCH DAMAGE.
universe@1 27 *
universe@1 28 */
universe@1 29
universe@1 30 #include <stdio.h>
universe@1 31 #include <stdlib.h>
universe@1 32 #include <string.h>
universe@1 33 #include <fcntl.h>
universe@1 34 #include <unistd.h>
universe@4 35 #include <ctype.h>
universe@4 36
universe@21 37 #include "javacodegen.h"
universe@21 38 #include "ccodegen.h"
universe@21 39
universe@4 40 #define INPUTBUF_SIZE 2048
universe@16 41
universe@11 42 typedef struct {
universe@19 43 char* outfilename;
universe@19 44 char* infilename;
universe@19 45 int highlight;
universe@11 46 } settings_t;
universe@4 47
universe@4 48 typedef struct {
universe@19 49 size_t count;
universe@19 50 size_t capacity;
universe@19 51 size_t maxlinewidth;
universe@19 52 char** lines;
universe@4 53 } inputfile_t;
universe@1 54
universe@1 55 inputfile_t *inputfilebuffer(size_t capacity) {
universe@19 56 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
universe@19 57 inputfile->lines = (char**) malloc(capacity * sizeof(char*));
universe@19 58 inputfile->capacity = capacity;
universe@19 59 inputfile->count = 0;
universe@19 60 inputfile->maxlinewidth = 0;
universe@19 61
universe@19 62 return inputfile;
universe@0 63 }
universe@0 64
universe@1 65 void addline(inputfile_t *inputfile, char* line, size_t width) {
universe@19 66 char *l = (char*) malloc(width+1);
universe@19 67 memcpy(l, line, width);
universe@19 68 l[width] = 0;
universe@19 69 if (inputfile->count >= inputfile->capacity) {
universe@19 70 inputfile->capacity <<= 1;
universe@19 71 inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
universe@19 72 }
universe@19 73 inputfile->lines[inputfile->count] = l;
universe@19 74 inputfile->maxlinewidth =
universe@19 75 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
universe@19 76 inputfile->count++;
universe@1 77 }
universe@1 78
universe@1 79 void freeinputfilebuffer(inputfile_t *inputfile) {
universe@19 80 for (int i = 0 ; i < inputfile->count ; i++) {
universe@19 81 free(inputfile->lines[i]);
universe@19 82 }
universe@19 83 free(inputfile->lines);
universe@19 84 free(inputfile);
universe@1 85 }
universe@1 86
universe@1 87 inputfile_t *readinput(char *filename) {
universe@1 88
universe@19 89 int fd = open(filename, O_RDONLY);
universe@19 90 if (fd == -1) return NULL;
universe@1 91
universe@19 92 inputfile_t *inputfile = inputfilebuffer(512);
universe@19 93
universe@19 94 char buf[INPUTBUF_SIZE];
universe@19 95 ssize_t r;
universe@19 96
universe@19 97 size_t maxlinewidth = 256;
universe@19 98 char *line = (char*) malloc(maxlinewidth);
universe@19 99 size_t col = 0;
universe@19 100
universe@19 101 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
universe@19 102 for (size_t i = 0 ; i < r ; i++) {
universe@19 103 if (col >= maxlinewidth-4) {
universe@19 104 maxlinewidth <<= 1;
universe@19 105 line = realloc(line, maxlinewidth);
universe@19 106 }
universe@19 107
universe@19 108 if (buf[i] == '\n') {
universe@19 109 line[col++] = '\n';
universe@19 110 line[col] = 0;
universe@19 111 addline(inputfile, line, col);
universe@19 112 col = 0;
universe@19 113 } else {
universe@19 114 line[col++] = buf[i];
universe@19 115 }
universe@19 116 }
universe@1 117 }
universe@19 118
universe@19 119 free(line);
universe@19 120
universe@19 121 close(fd);
universe@19 122
universe@19 123 return inputfile;
universe@1 124 }
universe@1 125
universe@1 126 void printhelp() {
universe@19 127 printf("Formats source code using HTML.\n\nUsage:\n"
universe@19 128 " c2html [Options] FILE\n\n"
universe@19 129 " Options:\n"
universe@19 130 " -h Prints this help message\n"
universe@19 131 " -j Highlight Java instead of C source code\n"
universe@19 132 " -o <output> Output file (stdout, if not specified)\n"
universe@19 133 " -p Disable highlighting (plain text)\n"
universe@19 134 "\n");
universe@19 135
universe@19 136
universe@1 137 }
universe@1 138
universe@4 139 int lnint(size_t lnc) {
universe@19 140 int w = 1, p = 1;
universe@19 141 while ((p*=10) < lnc) w++;
universe@19 142 return w;
universe@1 143 }
universe@1 144
universe@1 145 int main(int argc, char** argv) {
universe@19 146 settings_t settings;
universe@19 147 settings.outfilename = NULL;
universe@19 148 settings.highlight = 1;
universe@19 149
universe@19 150 highlighter_t highlighter;
universe@20 151 memset(&highlighter, 0, sizeof(highlighter));
universe@19 152 highlighter.isdirective = iscdirective;
universe@19 153 highlighter.istype = isctype;
universe@19 154 highlighter.keywords = ckeywords;
universe@21 155 highlighter.parser = cparseline;
universe@19 156
universe@19 157 char optc;
universe@19 158 while ((optc = getopt(argc, argv, "hjo:p")) != -1) {
universe@19 159 switch (optc) {
universe@19 160 case 'o':
universe@19 161 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@19 162 settings.outfilename = optarg;
universe@19 163 }
universe@19 164 break;
universe@19 165 case 'j':
universe@19 166 highlighter.isdirective = isjdirective;
universe@19 167 highlighter.istype = isjtype;
universe@19 168 highlighter.keywords = jkeywords;
universe@21 169 highlighter.parser = jparseline;
universe@19 170 break;
universe@19 171 case 'p':
universe@19 172 settings.highlight = 0;
universe@19 173 break;
universe@19 174 case 'h':
universe@19 175 printhelp();
universe@19 176 return 0;
universe@19 177 default:
universe@19 178 return 1;
universe@11 179 }
universe@19 180 }
universe@19 181
universe@19 182 if (optind != argc-1) {
universe@11 183 printhelp();
universe@19 184 return 1;
universe@19 185 } else {
universe@19 186 settings.infilename = argv[optind];
universe@19 187
universe@19 188 inputfile_t *inputfile = readinput(settings.infilename);
universe@19 189 if (inputfile) {
universe@19 190 FILE *fout;
universe@19 191 char *line;
universe@19 192 if (settings.highlight) {
universe@19 193 line = (char*) malloc(inputfile->maxlinewidth*64);
universe@19 194 } else {
universe@19 195 line = NULL;
universe@19 196 }
universe@19 197 if (settings.outfilename) {
universe@19 198 fout = fopen(settings.outfilename, "w");
universe@19 199 } else {
universe@19 200 fout = stdout;
universe@19 201 }
universe@19 202 fprintf(fout, "<pre>\n");
universe@19 203 int lnw = lnint(inputfile->count);
universe@19 204 for (int i = 0 ; i < inputfile->count ; i++) {
universe@19 205 if (settings.highlight) {
universe@21 206 highlighter.parser(inputfile->lines[i], line, &highlighter);
universe@19 207 } else {
universe@19 208 line = inputfile->lines[i];
universe@19 209 }
universe@19 210 fprintf(fout, "<span class=\"c2html-lineno\">%*d:</span> %s",
universe@19 211 lnw, i+1, line);
universe@19 212 }
universe@19 213 if (settings.highlight) {
universe@19 214 free(line);
universe@19 215 }
universe@19 216 fprintf(fout, "</pre>\n");
universe@19 217
universe@19 218 if (fout != stdout) {
universe@19 219 fclose(fout);
universe@19 220 }
universe@19 221
universe@19 222 freeinputfilebuffer(inputfile);
universe@19 223 }
universe@19 224
universe@11 225 return 0;
universe@11 226 }
universe@1 227 }
universe@1 228

mercurial