src/c2html.c

Tue, 21 Apr 2015 09:47:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 21 Apr 2015 09:47:52 +0200
changeset 25
f82aa7afe872
parent 24
e43dee5892f4
child 27
53fd8595378c
permissions
-rw-r--r--

more and better test cases + fixed memory leak introduced by changeset e43dee5892f4

universe@23 1 /*
universe@23 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@23 3 *
olaf@24 4 * Copyright 2015 Mike Becker. All rights reserved.
universe@23 5 *
universe@23 6 * Redistribution and use in source and binary forms, with or without
universe@23 7 * modification, are permitted provided that the following conditions are met:
universe@23 8 *
universe@23 9 * 1. Redistributions of source code must retain the above copyright
universe@23 10 * notice, this list of conditions and the following disclaimer.
universe@23 11 *
universe@23 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@23 13 * notice, this list of conditions and the following disclaimer in the
universe@23 14 * documentation and/or other materials provided with the distribution.
universe@23 15 *
universe@23 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@23 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@23 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@23 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@23 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@23 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@23 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@23 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@23 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@23 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@23 26 * POSSIBILITY OF SUCH DAMAGE.
universe@23 27 *
universe@23 28 */
universe@22 29 #include "c2html.h"
universe@1 30
universe@1 31 inputfile_t *inputfilebuffer(size_t capacity) {
universe@19 32 inputfile_t *inputfile = (inputfile_t*) malloc(sizeof(inputfile_t));
universe@19 33 inputfile->lines = (char**) malloc(capacity * sizeof(char*));
universe@19 34 inputfile->capacity = capacity;
universe@19 35 inputfile->count = 0;
universe@19 36 inputfile->maxlinewidth = 0;
universe@19 37
universe@19 38 return inputfile;
universe@0 39 }
universe@0 40
universe@1 41 void addline(inputfile_t *inputfile, char* line, size_t width) {
universe@19 42 char *l = (char*) malloc(width+1);
universe@19 43 memcpy(l, line, width);
universe@19 44 l[width] = 0;
universe@19 45 if (inputfile->count >= inputfile->capacity) {
universe@19 46 inputfile->capacity <<= 1;
universe@25 47 inputfile->lines = realloc(inputfile->lines,
universe@25 48 sizeof(char*)*inputfile->capacity);
universe@19 49 }
universe@19 50 inputfile->lines[inputfile->count] = l;
universe@19 51 inputfile->maxlinewidth =
universe@19 52 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
universe@19 53 inputfile->count++;
universe@1 54 }
universe@1 55
universe@1 56 void freeinputfilebuffer(inputfile_t *inputfile) {
universe@19 57 for (int i = 0 ; i < inputfile->count ; i++) {
universe@19 58 free(inputfile->lines[i]);
universe@19 59 }
universe@19 60 free(inputfile->lines);
universe@19 61 free(inputfile);
universe@1 62 }
universe@1 63
universe@1 64 inputfile_t *readinput(char *filename) {
universe@1 65
universe@19 66 int fd = open(filename, O_RDONLY);
universe@19 67 if (fd == -1) return NULL;
universe@1 68
universe@19 69 inputfile_t *inputfile = inputfilebuffer(512);
universe@19 70
universe@19 71 char buf[INPUTBUF_SIZE];
universe@19 72 ssize_t r;
universe@19 73
universe@19 74 size_t maxlinewidth = 256;
universe@19 75 char *line = (char*) malloc(maxlinewidth);
universe@19 76 size_t col = 0;
universe@19 77
universe@19 78 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
universe@19 79 for (size_t i = 0 ; i < r ; i++) {
universe@19 80 if (col >= maxlinewidth-4) {
universe@19 81 maxlinewidth <<= 1;
universe@19 82 line = realloc(line, maxlinewidth);
universe@19 83 }
universe@19 84
universe@19 85 if (buf[i] == '\n') {
universe@19 86 line[col++] = '\n';
universe@19 87 line[col] = 0;
universe@19 88 addline(inputfile, line, col);
universe@19 89 col = 0;
universe@19 90 } else {
universe@19 91 line[col++] = buf[i];
universe@19 92 }
universe@19 93 }
universe@1 94 }
universe@19 95
universe@19 96 free(line);
universe@19 97
universe@19 98 close(fd);
universe@19 99
universe@19 100 return inputfile;
universe@1 101 }
universe@1 102
universe@1 103 void printhelp() {
universe@19 104 printf("Formats source code using HTML.\n\nUsage:\n"
universe@19 105 " c2html [Options] FILE\n\n"
universe@19 106 " Options:\n"
universe@19 107 " -h Prints this help message\n"
universe@19 108 " -j Highlight Java instead of C source code\n"
universe@19 109 " -o <output> Output file (stdout, if not specified)\n"
universe@22 110 " -H <header> Prepend header file\n"
universe@22 111 " -F <footer> Append footer file\n"
universe@19 112 " -p Disable highlighting (plain text)\n"
olaf@24 113 " -l Disable line numbers\n"
universe@19 114 "\n");
universe@19 115
universe@19 116
universe@1 117 }
universe@1 118
universe@4 119 int lnint(size_t lnc) {
universe@19 120 int w = 1, p = 1;
universe@19 121 while ((p*=10) < lnc) w++;
universe@19 122 return w;
universe@1 123 }
universe@1 124
universe@22 125 int copyfile(char *filename, FILE *dest) {
universe@22 126 if (!filename) {
universe@22 127 return 0;
universe@22 128 }
universe@22 129
universe@22 130 FILE *src = fopen(filename, "r");
universe@22 131 if (src) {
universe@22 132 char buf[4096];
universe@22 133 int r;
universe@22 134 while ((r = fread(buf, 1, 4096, src)) > 0) {
universe@22 135 fwrite(buf, 1, r, dest);
universe@22 136 }
universe@22 137 fclose(src);
universe@22 138 return 0;
universe@22 139 } else {
olaf@24 140 return -1;
universe@22 141 }
universe@22 142 }
universe@22 143
olaf@24 144 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
olaf@24 145 int formatfile(
olaf@24 146 highlighter_t *highlighter,
olaf@24 147 inputfile_t *in,
olaf@24 148 fmt_write_func out,
olaf@24 149 void *stream,
olaf@24 150 _Bool showln) {
olaf@24 151 // formats an input file and writes the result to out
olaf@24 152
olaf@24 153 char *line = malloc(in->maxlinewidth*64);
olaf@24 154 if(!line) {
olaf@24 155 return 1;
olaf@24 156 }
olaf@24 157 WRITECONST(stream, out, "<pre>\n");
olaf@24 158
olaf@24 159 int lnw = lnint(in->count);
olaf@24 160 for (int i = 0 ; i < in->count ; i++) {
olaf@24 161 char *ln = line;
olaf@24 162 if (highlighter) {
olaf@24 163 highlighter->parser(in->lines[i], line, highlighter);
olaf@24 164 } else {
olaf@24 165 ln = in->lines[i];
olaf@24 166 }
olaf@24 167
olaf@24 168 // write line number
olaf@24 169 if (showln) {
olaf@24 170 WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
olaf@24 171 char lnbuf[16];
olaf@24 172 int len = snprintf(lnbuf, 16, "%*d ", lnw, i+1);
olaf@24 173 out(lnbuf, 1, len, stream);
olaf@24 174 WRITECONST(stream, out, "</span> ");
olaf@24 175 }
olaf@24 176
olaf@24 177 // write formated (or plain) code line
olaf@24 178 out(ln, 1, strlen(ln), stream);
olaf@24 179 }
olaf@24 180
olaf@24 181 WRITECONST(stream, out, "</pre>\n");
olaf@24 182 free(line);
olaf@24 183 return 0;
olaf@24 184 }
olaf@24 185
olaf@24 186 void init_c_highlighter(highlighter_t *highlighter) {
olaf@24 187 memset(highlighter, 0, sizeof(highlighter_t));
olaf@24 188 highlighter->isdirective = iscdirective;
olaf@24 189 highlighter->istype = isctype;
olaf@24 190 highlighter->keywords = ckeywords;
olaf@24 191 highlighter->parser = cparseline;
olaf@24 192 }
olaf@24 193
olaf@24 194 void init_java_highlighter(highlighter_t *highlighter) {
olaf@24 195 memset(highlighter, 0, sizeof(highlighter_t));
olaf@24 196 highlighter->isdirective = isjdirective;
olaf@24 197 highlighter->istype = isjtype;
olaf@24 198 highlighter->keywords = jkeywords;
olaf@24 199 highlighter->parser = jparseline;
olaf@24 200 }
olaf@24 201
universe@1 202 int main(int argc, char** argv) {
universe@22 203 int retcode = EXIT_SUCCESS;
universe@22 204
universe@19 205 settings_t settings;
universe@22 206 memset(&settings, 0, sizeof(settings));
universe@19 207 settings.highlight = 1;
olaf@24 208 settings.showlinenumbers = 1;
universe@22 209
olaf@24 210 int lang = C2HTML_C;
universe@19 211
universe@19 212 char optc;
olaf@24 213 while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) {
universe@19 214 switch (optc) {
universe@19 215 case 'o':
universe@19 216 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@19 217 settings.outfilename = optarg;
universe@19 218 }
universe@19 219 break;
universe@22 220 case 'F':
universe@22 221 settings.footerfile = optarg;
universe@22 222 break;
universe@22 223 case 'H':
universe@22 224 settings.headerfile = optarg;
universe@22 225 break;
universe@19 226 case 'j':
olaf@24 227 lang = C2HTML_JAVA;
universe@19 228 break;
universe@19 229 case 'p':
universe@19 230 settings.highlight = 0;
universe@19 231 break;
olaf@24 232 case 'l':
olaf@24 233 settings.showlinenumbers = 0;
olaf@24 234 break;
universe@19 235 case 'h':
universe@19 236 printhelp();
universe@19 237 return 0;
universe@19 238 default:
universe@19 239 return 1;
universe@11 240 }
universe@19 241 }
universe@19 242
universe@19 243 if (optind != argc-1) {
universe@11 244 printhelp();
universe@19 245 return 1;
universe@19 246 } else {
universe@19 247 settings.infilename = argv[optind];
universe@22 248 FILE *fout;
universe@22 249 if (settings.outfilename) {
universe@22 250 fout = fopen(settings.outfilename, "w");
universe@22 251 if (!fout) {
universe@22 252 perror("Error opening output file");
olaf@24 253 return -1;
universe@22 254 }
universe@22 255 } else {
universe@22 256 fout = stdout;
universe@22 257 }
universe@22 258
universe@22 259 if (copyfile(settings.headerfile, fout)) {
universe@22 260 perror("Error opening header file");
olaf@24 261 retcode = -1;
universe@22 262 goto prog_end;
universe@22 263 }
olaf@24 264
olaf@24 265 highlighter_t highlighter;
olaf@24 266 highlighter_t *hptr = &highlighter;
olaf@24 267 switch (lang) {
olaf@24 268 case C2HTML_C:
olaf@24 269 init_c_highlighter(&highlighter);
olaf@24 270 break;
olaf@24 271 case C2HTML_JAVA:
olaf@24 272 init_java_highlighter(&highlighter);
olaf@24 273 break;
olaf@24 274 default:
olaf@24 275 hptr = NULL;
olaf@24 276 break;
olaf@24 277 }
olaf@24 278 if (!settings.highlight) {
olaf@24 279 hptr = NULL;
olaf@24 280 }
universe@19 281
universe@19 282 inputfile_t *inputfile = readinput(settings.infilename);
universe@19 283 if (inputfile) {
olaf@24 284 formatfile(
olaf@24 285 hptr,
olaf@24 286 inputfile,
olaf@24 287 (fmt_write_func)fwrite,
olaf@24 288 fout,
olaf@24 289 settings.showlinenumbers);
universe@25 290 freeinputfilebuffer(inputfile);
universe@22 291 } else {
universe@22 292 perror("Error opening input file");
olaf@24 293 retcode = -1;
olaf@24 294 }
olaf@24 295
olaf@24 296 if (copyfile(settings.footerfile, fout)) {
olaf@24 297 perror("Error opening footer file");
olaf@24 298 retcode = -1;
universe@22 299 }
universe@22 300
universe@22 301 prog_end:
universe@22 302 if (fout != stdout) {
universe@22 303 fclose(fout);
universe@19 304 }
universe@19 305
universe@22 306 return retcode;
universe@11 307 }
universe@1 308 }
universe@1 309

mercurial