src/c2html.c

Tue, 23 Aug 2016 12:06:46 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 12:06:46 +0200
changeset 38
77c158821738
parent 37
1a67185e5496
child 39
ac35daceb24c
permissions
-rw-r--r--

use macros for exit codes

universe@23 1 /*
universe@23 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@23 3 *
universe@35 4 * Copyright 2016 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@37 114 " -V, -v Prints version and exits\n"
universe@19 115 "\n");
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 if (highlighter) {
olaf@24 161 highlighter->parser(in->lines[i], line, highlighter);
olaf@24 162 } else {
universe@30 163 char *c = in->lines[i];
universe@30 164 size_t dp = 0;
universe@30 165 while (*c) {
universe@30 166 dp = writeescapedchar(line, dp, *c);
universe@30 167 c++;
universe@30 168 }
universe@30 169 line[dp] = '\0';
olaf@24 170 }
universe@30 171
olaf@24 172 // write line number
olaf@24 173 if (showln) {
olaf@24 174 WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
universe@27 175 char lnbuf[128];
universe@27 176 int len;
universe@27 177 // line number link
universe@27 178 len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">",
universe@27 179 i+1, i+1);
olaf@24 180 out(lnbuf, 1, len, stream);
universe@27 181 // justified line number
universe@27 182 len = snprintf(lnbuf, 128, "%*d ", lnw, i+1);
universe@27 183 out(lnbuf, 1, len, stream);
universe@27 184 WRITECONST(stream, out, "</a></span> ");
olaf@24 185 }
olaf@24 186
olaf@24 187 // write formated (or plain) code line
universe@30 188 out(line, 1, strlen(line), stream);
olaf@24 189 }
olaf@24 190
olaf@24 191 WRITECONST(stream, out, "</pre>\n");
olaf@24 192 free(line);
olaf@24 193 return 0;
olaf@24 194 }
olaf@24 195
olaf@24 196 void init_c_highlighter(highlighter_t *highlighter) {
olaf@24 197 memset(highlighter, 0, sizeof(highlighter_t));
universe@36 198 highlighter->isdirective = check_cdirective;
universe@36 199 highlighter->istype = check_ctype;
olaf@24 200 highlighter->keywords = ckeywords;
olaf@24 201 highlighter->parser = cparseline;
olaf@24 202 }
olaf@24 203
olaf@24 204 void init_java_highlighter(highlighter_t *highlighter) {
olaf@24 205 memset(highlighter, 0, sizeof(highlighter_t));
universe@36 206 highlighter->isdirective = check_jdirective;
universe@36 207 highlighter->istype = check_jtype;
olaf@24 208 highlighter->keywords = jkeywords;
olaf@24 209 highlighter->parser = jparseline;
olaf@24 210 }
olaf@24 211
universe@1 212 int main(int argc, char** argv) {
universe@22 213 int retcode = EXIT_SUCCESS;
universe@22 214
universe@19 215 settings_t settings;
universe@22 216 memset(&settings, 0, sizeof(settings));
universe@19 217 settings.highlight = 1;
olaf@24 218 settings.showlinenumbers = 1;
universe@22 219
olaf@24 220 int lang = C2HTML_C;
universe@19 221
universe@19 222 char optc;
universe@37 223 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
universe@19 224 switch (optc) {
universe@19 225 case 'o':
universe@19 226 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@19 227 settings.outfilename = optarg;
universe@19 228 }
universe@19 229 break;
universe@22 230 case 'F':
universe@22 231 settings.footerfile = optarg;
universe@22 232 break;
universe@22 233 case 'H':
universe@22 234 settings.headerfile = optarg;
universe@22 235 break;
universe@19 236 case 'j':
olaf@24 237 lang = C2HTML_JAVA;
universe@19 238 break;
universe@19 239 case 'p':
universe@19 240 settings.highlight = 0;
universe@19 241 break;
olaf@24 242 case 'l':
olaf@24 243 settings.showlinenumbers = 0;
olaf@24 244 break;
universe@19 245 case 'h':
universe@19 246 printhelp();
universe@38 247 return EXIT_SUCCESS;
universe@37 248 case 'v':
universe@37 249 case 'V':
universe@37 250 #ifdef VERSION_DEVELOP
universe@37 251 printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR);
universe@37 252 #else
universe@37 253 printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
universe@37 254 #endif
universe@38 255 return EXIT_SUCCESS;
universe@19 256 default:
universe@38 257 return EXIT_FAILURE;
universe@11 258 }
universe@19 259 }
universe@19 260
universe@19 261 if (optind != argc-1) {
universe@11 262 printhelp();
universe@19 263 return 1;
universe@19 264 } else {
universe@19 265 settings.infilename = argv[optind];
universe@22 266 FILE *fout;
universe@22 267 if (settings.outfilename) {
universe@22 268 fout = fopen(settings.outfilename, "w");
universe@22 269 if (!fout) {
universe@22 270 perror("Error opening output file");
universe@38 271 return EXIT_FAILURE;
universe@22 272 }
universe@22 273 } else {
universe@22 274 fout = stdout;
universe@22 275 }
universe@22 276
universe@22 277 if (copyfile(settings.headerfile, fout)) {
universe@22 278 perror("Error opening header file");
universe@38 279 retcode = EXIT_FAILURE;
universe@22 280 goto prog_end;
universe@22 281 }
olaf@24 282
olaf@24 283 highlighter_t highlighter;
olaf@24 284 highlighter_t *hptr = &highlighter;
olaf@24 285 switch (lang) {
olaf@24 286 case C2HTML_C:
olaf@24 287 init_c_highlighter(&highlighter);
olaf@24 288 break;
olaf@24 289 case C2HTML_JAVA:
olaf@24 290 init_java_highlighter(&highlighter);
olaf@24 291 break;
olaf@24 292 default:
olaf@24 293 hptr = NULL;
olaf@24 294 break;
olaf@24 295 }
olaf@24 296 if (!settings.highlight) {
olaf@24 297 hptr = NULL;
olaf@24 298 }
universe@19 299
universe@19 300 inputfile_t *inputfile = readinput(settings.infilename);
universe@19 301 if (inputfile) {
olaf@24 302 formatfile(
olaf@24 303 hptr,
olaf@24 304 inputfile,
olaf@24 305 (fmt_write_func)fwrite,
olaf@24 306 fout,
olaf@24 307 settings.showlinenumbers);
universe@25 308 freeinputfilebuffer(inputfile);
universe@22 309 } else {
universe@22 310 perror("Error opening input file");
universe@38 311 retcode = EXIT_FAILURE;
olaf@24 312 }
olaf@24 313
olaf@24 314 if (copyfile(settings.footerfile, fout)) {
olaf@24 315 perror("Error opening footer file");
universe@38 316 retcode = EXIT_FAILURE;
universe@22 317 }
universe@22 318
universe@22 319 prog_end:
universe@22 320 if (fout != stdout) {
universe@22 321 fclose(fout);
universe@19 322 }
universe@19 323
universe@22 324 return retcode;
universe@11 325 }
universe@1 326 }
universe@1 327

mercurial