src/c2html.c

Sun, 19 Apr 2015 10:48:00 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 19 Apr 2015 10:48:00 +0200
changeset 24
e43dee5892f4
parent 23
f44a185b678b
child 25
f82aa7afe872
permissions
-rw-r--r--

improved code structure and added option for disabling line numbers

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@19 47 inputfile->lines = realloc(inputfile->lines, inputfile->capacity);
universe@19 48 }
universe@19 49 inputfile->lines[inputfile->count] = l;
universe@19 50 inputfile->maxlinewidth =
universe@19 51 width > inputfile->maxlinewidth ? width : inputfile->maxlinewidth;
universe@19 52 inputfile->count++;
universe@1 53 }
universe@1 54
universe@1 55 void freeinputfilebuffer(inputfile_t *inputfile) {
universe@19 56 for (int i = 0 ; i < inputfile->count ; i++) {
universe@19 57 free(inputfile->lines[i]);
universe@19 58 }
universe@19 59 free(inputfile->lines);
universe@19 60 free(inputfile);
universe@1 61 }
universe@1 62
universe@1 63 inputfile_t *readinput(char *filename) {
universe@1 64
universe@19 65 int fd = open(filename, O_RDONLY);
universe@19 66 if (fd == -1) return NULL;
universe@1 67
universe@19 68 inputfile_t *inputfile = inputfilebuffer(512);
universe@19 69
universe@19 70 char buf[INPUTBUF_SIZE];
universe@19 71 ssize_t r;
universe@19 72
universe@19 73 size_t maxlinewidth = 256;
universe@19 74 char *line = (char*) malloc(maxlinewidth);
universe@19 75 size_t col = 0;
universe@19 76
universe@19 77 while ((r = read(fd, buf, INPUTBUF_SIZE)) > 0) {
universe@19 78 for (size_t i = 0 ; i < r ; i++) {
universe@19 79 if (col >= maxlinewidth-4) {
universe@19 80 maxlinewidth <<= 1;
universe@19 81 line = realloc(line, maxlinewidth);
universe@19 82 }
universe@19 83
universe@19 84 if (buf[i] == '\n') {
universe@19 85 line[col++] = '\n';
universe@19 86 line[col] = 0;
universe@19 87 addline(inputfile, line, col);
universe@19 88 col = 0;
universe@19 89 } else {
universe@19 90 line[col++] = buf[i];
universe@19 91 }
universe@19 92 }
universe@1 93 }
universe@19 94
universe@19 95 free(line);
universe@19 96
universe@19 97 close(fd);
universe@19 98
universe@19 99 return inputfile;
universe@1 100 }
universe@1 101
universe@1 102 void printhelp() {
universe@19 103 printf("Formats source code using HTML.\n\nUsage:\n"
universe@19 104 " c2html [Options] FILE\n\n"
universe@19 105 " Options:\n"
universe@19 106 " -h Prints this help message\n"
universe@19 107 " -j Highlight Java instead of C source code\n"
universe@19 108 " -o <output> Output file (stdout, if not specified)\n"
universe@22 109 " -H <header> Prepend header file\n"
universe@22 110 " -F <footer> Append footer file\n"
universe@19 111 " -p Disable highlighting (plain text)\n"
olaf@24 112 " -l Disable line numbers\n"
universe@19 113 "\n");
universe@19 114
universe@19 115
universe@1 116 }
universe@1 117
universe@4 118 int lnint(size_t lnc) {
universe@19 119 int w = 1, p = 1;
universe@19 120 while ((p*=10) < lnc) w++;
universe@19 121 return w;
universe@1 122 }
universe@1 123
universe@22 124 int copyfile(char *filename, FILE *dest) {
universe@22 125 if (!filename) {
universe@22 126 return 0;
universe@22 127 }
universe@22 128
universe@22 129 FILE *src = fopen(filename, "r");
universe@22 130 if (src) {
universe@22 131 char buf[4096];
universe@22 132 int r;
universe@22 133 while ((r = fread(buf, 1, 4096, src)) > 0) {
universe@22 134 fwrite(buf, 1, r, dest);
universe@22 135 }
universe@22 136 fclose(src);
universe@22 137 return 0;
universe@22 138 } else {
olaf@24 139 return -1;
universe@22 140 }
universe@22 141 }
universe@22 142
olaf@24 143 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
olaf@24 144 int formatfile(
olaf@24 145 highlighter_t *highlighter,
olaf@24 146 inputfile_t *in,
olaf@24 147 fmt_write_func out,
olaf@24 148 void *stream,
olaf@24 149 _Bool showln) {
olaf@24 150 // formats an input file and writes the result to out
olaf@24 151
olaf@24 152 char *line = malloc(in->maxlinewidth*64);
olaf@24 153 if(!line) {
olaf@24 154 return 1;
olaf@24 155 }
olaf@24 156 WRITECONST(stream, out, "<pre>\n");
olaf@24 157
olaf@24 158 int lnw = lnint(in->count);
olaf@24 159 for (int i = 0 ; i < in->count ; i++) {
olaf@24 160 char *ln = line;
olaf@24 161 if (highlighter) {
olaf@24 162 highlighter->parser(in->lines[i], line, highlighter);
olaf@24 163 } else {
olaf@24 164 ln = in->lines[i];
olaf@24 165 }
olaf@24 166
olaf@24 167 // write line number
olaf@24 168 if (showln) {
olaf@24 169 WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
olaf@24 170 char lnbuf[16];
olaf@24 171 int len = snprintf(lnbuf, 16, "%*d ", lnw, i+1);
olaf@24 172 out(lnbuf, 1, len, stream);
olaf@24 173 WRITECONST(stream, out, "</span> ");
olaf@24 174 }
olaf@24 175
olaf@24 176 // write formated (or plain) code line
olaf@24 177 out(ln, 1, strlen(ln), stream);
olaf@24 178 }
olaf@24 179
olaf@24 180 WRITECONST(stream, out, "</pre>\n");
olaf@24 181 free(line);
olaf@24 182 return 0;
olaf@24 183 }
olaf@24 184
olaf@24 185 void init_c_highlighter(highlighter_t *highlighter) {
olaf@24 186 memset(highlighter, 0, sizeof(highlighter_t));
olaf@24 187 highlighter->isdirective = iscdirective;
olaf@24 188 highlighter->istype = isctype;
olaf@24 189 highlighter->keywords = ckeywords;
olaf@24 190 highlighter->parser = cparseline;
olaf@24 191 }
olaf@24 192
olaf@24 193 void init_java_highlighter(highlighter_t *highlighter) {
olaf@24 194 memset(highlighter, 0, sizeof(highlighter_t));
olaf@24 195 highlighter->isdirective = isjdirective;
olaf@24 196 highlighter->istype = isjtype;
olaf@24 197 highlighter->keywords = jkeywords;
olaf@24 198 highlighter->parser = jparseline;
olaf@24 199 }
olaf@24 200
universe@1 201 int main(int argc, char** argv) {
universe@22 202 int retcode = EXIT_SUCCESS;
universe@22 203
universe@19 204 settings_t settings;
universe@22 205 memset(&settings, 0, sizeof(settings));
universe@19 206 settings.highlight = 1;
olaf@24 207 settings.showlinenumbers = 1;
universe@22 208
olaf@24 209 int lang = C2HTML_C;
universe@19 210
universe@19 211 char optc;
olaf@24 212 while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) {
universe@19 213 switch (optc) {
universe@19 214 case 'o':
universe@19 215 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@19 216 settings.outfilename = optarg;
universe@19 217 }
universe@19 218 break;
universe@22 219 case 'F':
universe@22 220 settings.footerfile = optarg;
universe@22 221 break;
universe@22 222 case 'H':
universe@22 223 settings.headerfile = optarg;
universe@22 224 break;
universe@19 225 case 'j':
olaf@24 226 lang = C2HTML_JAVA;
universe@19 227 break;
universe@19 228 case 'p':
universe@19 229 settings.highlight = 0;
universe@19 230 break;
olaf@24 231 case 'l':
olaf@24 232 settings.showlinenumbers = 0;
olaf@24 233 break;
universe@19 234 case 'h':
universe@19 235 printhelp();
universe@19 236 return 0;
universe@19 237 default:
universe@19 238 return 1;
universe@11 239 }
universe@19 240 }
universe@19 241
universe@19 242 if (optind != argc-1) {
universe@11 243 printhelp();
universe@19 244 return 1;
universe@19 245 } else {
universe@19 246 settings.infilename = argv[optind];
universe@22 247 FILE *fout;
universe@22 248 if (settings.outfilename) {
universe@22 249 fout = fopen(settings.outfilename, "w");
universe@22 250 if (!fout) {
universe@22 251 perror("Error opening output file");
olaf@24 252 return -1;
universe@22 253 }
universe@22 254 } else {
universe@22 255 fout = stdout;
universe@22 256 }
universe@22 257
universe@22 258 if (copyfile(settings.headerfile, fout)) {
universe@22 259 perror("Error opening header file");
olaf@24 260 retcode = -1;
universe@22 261 goto prog_end;
universe@22 262 }
olaf@24 263
olaf@24 264 highlighter_t highlighter;
olaf@24 265 highlighter_t *hptr = &highlighter;
olaf@24 266 switch (lang) {
olaf@24 267 case C2HTML_C:
olaf@24 268 init_c_highlighter(&highlighter);
olaf@24 269 break;
olaf@24 270 case C2HTML_JAVA:
olaf@24 271 init_java_highlighter(&highlighter);
olaf@24 272 break;
olaf@24 273 default:
olaf@24 274 hptr = NULL;
olaf@24 275 break;
olaf@24 276 }
olaf@24 277 if (!settings.highlight) {
olaf@24 278 hptr = NULL;
olaf@24 279 }
universe@19 280
universe@19 281 inputfile_t *inputfile = readinput(settings.infilename);
universe@19 282 if (inputfile) {
olaf@24 283 formatfile(
olaf@24 284 hptr,
olaf@24 285 inputfile,
olaf@24 286 (fmt_write_func)fwrite,
olaf@24 287 fout,
olaf@24 288 settings.showlinenumbers);
universe@22 289 } else {
universe@22 290 perror("Error opening input file");
olaf@24 291 retcode = -1;
olaf@24 292 }
olaf@24 293
olaf@24 294 if (copyfile(settings.footerfile, fout)) {
olaf@24 295 perror("Error opening footer file");
olaf@24 296 retcode = -1;
universe@22 297 }
universe@22 298
universe@22 299 prog_end:
universe@22 300 if (fout != stdout) {
universe@22 301 fclose(fout);
universe@19 302 }
universe@19 303
universe@22 304 return retcode;
universe@11 305 }
universe@1 306 }
universe@1 307

mercurial