src/c2html.c

Tue, 21 Apr 2015 10:11:37 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 21 Apr 2015 10:11:37 +0200
changeset 27
53fd8595378c
parent 25
f82aa7afe872
child 30
0bfd4d6f086a
permissions
-rw-r--r--

added links for 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@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\">");
universe@27 171 char lnbuf[128];
universe@27 172 int len;
universe@27 173 // line number link
universe@27 174 len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">",
universe@27 175 i+1, i+1);
olaf@24 176 out(lnbuf, 1, len, stream);
universe@27 177 // justified line number
universe@27 178 len = snprintf(lnbuf, 128, "%*d ", lnw, i+1);
universe@27 179 out(lnbuf, 1, len, stream);
universe@27 180 WRITECONST(stream, out, "</a></span> ");
olaf@24 181 }
olaf@24 182
olaf@24 183 // write formated (or plain) code line
olaf@24 184 out(ln, 1, strlen(ln), stream);
olaf@24 185 }
olaf@24 186
olaf@24 187 WRITECONST(stream, out, "</pre>\n");
olaf@24 188 free(line);
olaf@24 189 return 0;
olaf@24 190 }
olaf@24 191
olaf@24 192 void init_c_highlighter(highlighter_t *highlighter) {
olaf@24 193 memset(highlighter, 0, sizeof(highlighter_t));
olaf@24 194 highlighter->isdirective = iscdirective;
olaf@24 195 highlighter->istype = isctype;
olaf@24 196 highlighter->keywords = ckeywords;
olaf@24 197 highlighter->parser = cparseline;
olaf@24 198 }
olaf@24 199
olaf@24 200 void init_java_highlighter(highlighter_t *highlighter) {
olaf@24 201 memset(highlighter, 0, sizeof(highlighter_t));
olaf@24 202 highlighter->isdirective = isjdirective;
olaf@24 203 highlighter->istype = isjtype;
olaf@24 204 highlighter->keywords = jkeywords;
olaf@24 205 highlighter->parser = jparseline;
olaf@24 206 }
olaf@24 207
universe@1 208 int main(int argc, char** argv) {
universe@22 209 int retcode = EXIT_SUCCESS;
universe@22 210
universe@19 211 settings_t settings;
universe@22 212 memset(&settings, 0, sizeof(settings));
universe@19 213 settings.highlight = 1;
olaf@24 214 settings.showlinenumbers = 1;
universe@22 215
olaf@24 216 int lang = C2HTML_C;
universe@19 217
universe@19 218 char optc;
olaf@24 219 while ((optc = getopt(argc, argv, "hljo:pH:F:")) != -1) {
universe@19 220 switch (optc) {
universe@19 221 case 'o':
universe@19 222 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@19 223 settings.outfilename = optarg;
universe@19 224 }
universe@19 225 break;
universe@22 226 case 'F':
universe@22 227 settings.footerfile = optarg;
universe@22 228 break;
universe@22 229 case 'H':
universe@22 230 settings.headerfile = optarg;
universe@22 231 break;
universe@19 232 case 'j':
olaf@24 233 lang = C2HTML_JAVA;
universe@19 234 break;
universe@19 235 case 'p':
universe@19 236 settings.highlight = 0;
universe@19 237 break;
olaf@24 238 case 'l':
olaf@24 239 settings.showlinenumbers = 0;
olaf@24 240 break;
universe@19 241 case 'h':
universe@19 242 printhelp();
universe@19 243 return 0;
universe@19 244 default:
universe@19 245 return 1;
universe@11 246 }
universe@19 247 }
universe@19 248
universe@19 249 if (optind != argc-1) {
universe@11 250 printhelp();
universe@19 251 return 1;
universe@19 252 } else {
universe@19 253 settings.infilename = argv[optind];
universe@22 254 FILE *fout;
universe@22 255 if (settings.outfilename) {
universe@22 256 fout = fopen(settings.outfilename, "w");
universe@22 257 if (!fout) {
universe@22 258 perror("Error opening output file");
olaf@24 259 return -1;
universe@22 260 }
universe@22 261 } else {
universe@22 262 fout = stdout;
universe@22 263 }
universe@22 264
universe@22 265 if (copyfile(settings.headerfile, fout)) {
universe@22 266 perror("Error opening header file");
olaf@24 267 retcode = -1;
universe@22 268 goto prog_end;
universe@22 269 }
olaf@24 270
olaf@24 271 highlighter_t highlighter;
olaf@24 272 highlighter_t *hptr = &highlighter;
olaf@24 273 switch (lang) {
olaf@24 274 case C2HTML_C:
olaf@24 275 init_c_highlighter(&highlighter);
olaf@24 276 break;
olaf@24 277 case C2HTML_JAVA:
olaf@24 278 init_java_highlighter(&highlighter);
olaf@24 279 break;
olaf@24 280 default:
olaf@24 281 hptr = NULL;
olaf@24 282 break;
olaf@24 283 }
olaf@24 284 if (!settings.highlight) {
olaf@24 285 hptr = NULL;
olaf@24 286 }
universe@19 287
universe@19 288 inputfile_t *inputfile = readinput(settings.infilename);
universe@19 289 if (inputfile) {
olaf@24 290 formatfile(
olaf@24 291 hptr,
olaf@24 292 inputfile,
olaf@24 293 (fmt_write_func)fwrite,
olaf@24 294 fout,
olaf@24 295 settings.showlinenumbers);
universe@25 296 freeinputfilebuffer(inputfile);
universe@22 297 } else {
universe@22 298 perror("Error opening input file");
olaf@24 299 retcode = -1;
olaf@24 300 }
olaf@24 301
olaf@24 302 if (copyfile(settings.footerfile, fout)) {
olaf@24 303 perror("Error opening footer file");
olaf@24 304 retcode = -1;
universe@22 305 }
universe@22 306
universe@22 307 prog_end:
universe@22 308 if (fout != stdout) {
universe@22 309 fclose(fout);
universe@19 310 }
universe@19 311
universe@22 312 return retcode;
universe@11 313 }
universe@1 314 }
universe@1 315

mercurial