src/c2html.c

Tue, 23 Aug 2016 13:49:38 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 13:49:38 +0200
changeset 39
ac35daceb24c
parent 38
77c158821738
child 40
903b46fc4214
permissions
-rw-r--r--

adds UCX + changes how the input file is read (uses an consecutive memory area now)

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@39 31 #include "ucx/buffer.h"
universe@39 32 #include "ucx/list.h"
universe@39 33 #include "ucx/utils.h"
universe@1 34
universe@1 35 void printhelp() {
universe@19 36 printf("Formats source code using HTML.\n\nUsage:\n"
universe@19 37 " c2html [Options] FILE\n\n"
universe@19 38 " Options:\n"
universe@19 39 " -h Prints this help message\n"
universe@19 40 " -j Highlight Java instead of C source code\n"
universe@19 41 " -o <output> Output file (stdout, if not specified)\n"
universe@22 42 " -H <header> Prepend header file\n"
universe@22 43 " -F <footer> Append footer file\n"
universe@19 44 " -p Disable highlighting (plain text)\n"
olaf@24 45 " -l Disable line numbers\n"
universe@37 46 " -V, -v Prints version and exits\n"
universe@19 47 "\n");
universe@1 48 }
universe@1 49
universe@22 50 int copyfile(char *filename, FILE *dest) {
universe@22 51 if (!filename) {
universe@22 52 return 0;
universe@22 53 }
universe@22 54
universe@22 55 FILE *src = fopen(filename, "r");
universe@22 56 if (src) {
universe@22 57 char buf[4096];
universe@22 58 int r;
universe@22 59 while ((r = fread(buf, 1, 4096, src)) > 0) {
universe@22 60 fwrite(buf, 1, r, dest);
universe@22 61 }
universe@22 62 fclose(src);
universe@22 63 return 0;
universe@22 64 } else {
universe@39 65 return 1;
universe@22 66 }
universe@22 67 }
universe@22 68
olaf@24 69 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream)
olaf@24 70 int formatfile(
olaf@24 71 highlighter_t *highlighter,
universe@39 72 UcxList *in,
universe@39 73 write_func out,
olaf@24 74 void *stream,
universe@39 75 int showlineno) {
universe@39 76 size_t lines = ucx_list_size(in);
olaf@24 77
universe@39 78 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
olaf@24 79 if(!line) {
olaf@24 80 return 1;
olaf@24 81 }
olaf@24 82 WRITECONST(stream, out, "<pre>\n");
olaf@24 83
universe@39 84 int lnw;
universe@39 85 {
universe@39 86 lnw = 1;
universe@39 87 int p = 1;
universe@39 88 while ((p*=10) < lines) lnw++;
universe@39 89 }
universe@39 90
universe@39 91 size_t lineno = 0;
universe@39 92 UCX_FOREACH(sourceline, in) {
universe@39 93 lineno++;
universe@39 94 /* TODO: backwards compatibility: replace line->space in all occasions
universe@39 95 * and use UcxBuffer functions
universe@39 96 */
olaf@24 97 if (highlighter) {
universe@39 98 highlighter->parser(sourceline->data, line->space, highlighter);
olaf@24 99 } else {
universe@39 100 char *c = sourceline->data;
universe@30 101 size_t dp = 0;
universe@30 102 while (*c) {
universe@39 103 dp = writeescapedchar(line->space, dp, *c);
universe@30 104 c++;
universe@30 105 }
universe@39 106 line->space[dp] = '\0';
olaf@24 107 }
universe@30 108
olaf@24 109 // write line number
universe@39 110 if (showlineno) {
olaf@24 111 WRITECONST(stream, out, "<span class=\"c2html-lineno\">");
universe@27 112 char lnbuf[128];
universe@27 113 int len;
universe@27 114 // line number link
universe@27 115 len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">",
universe@39 116 lineno, lineno);
olaf@24 117 out(lnbuf, 1, len, stream);
universe@27 118 // justified line number
universe@39 119 len = snprintf(lnbuf, 128, "%*d ", lnw, lineno);
universe@27 120 out(lnbuf, 1, len, stream);
universe@27 121 WRITECONST(stream, out, "</a></span> ");
olaf@24 122 }
olaf@24 123
olaf@24 124 // write formated (or plain) code line
universe@39 125 out(line->space, 1, strlen(line->space), stream);
olaf@24 126 }
olaf@24 127
olaf@24 128 WRITECONST(stream, out, "</pre>\n");
universe@39 129 ucx_buffer_free(line);
olaf@24 130 return 0;
olaf@24 131 }
olaf@24 132
olaf@24 133 void init_c_highlighter(highlighter_t *highlighter) {
olaf@24 134 memset(highlighter, 0, sizeof(highlighter_t));
universe@36 135 highlighter->isdirective = check_cdirective;
universe@36 136 highlighter->istype = check_ctype;
olaf@24 137 highlighter->keywords = ckeywords;
olaf@24 138 highlighter->parser = cparseline;
olaf@24 139 }
olaf@24 140
olaf@24 141 void init_java_highlighter(highlighter_t *highlighter) {
olaf@24 142 memset(highlighter, 0, sizeof(highlighter_t));
universe@36 143 highlighter->isdirective = check_jdirective;
universe@36 144 highlighter->istype = check_jtype;
olaf@24 145 highlighter->keywords = jkeywords;
olaf@24 146 highlighter->parser = jparseline;
olaf@24 147 }
olaf@24 148
universe@39 149 enum source_type {
universe@39 150 SOURCE_C,
universe@39 151 SOURCE_JAVA,
universe@39 152 SOURCE_PLAIN
universe@39 153 };
universe@39 154
universe@1 155 int main(int argc, char** argv) {
universe@22 156 int retcode = EXIT_SUCCESS;
universe@22 157
universe@39 158 Settings settings;
universe@22 159 memset(&settings, 0, sizeof(settings));
olaf@24 160 settings.showlinenumbers = 1;
universe@22 161
universe@39 162 enum source_type sourcetype = SOURCE_C;
universe@19 163
universe@19 164 char optc;
universe@37 165 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
universe@19 166 switch (optc) {
universe@19 167 case 'o':
universe@19 168 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@19 169 settings.outfilename = optarg;
universe@19 170 }
universe@19 171 break;
universe@22 172 case 'F':
universe@22 173 settings.footerfile = optarg;
universe@22 174 break;
universe@22 175 case 'H':
universe@22 176 settings.headerfile = optarg;
universe@22 177 break;
universe@19 178 case 'j':
universe@39 179 sourcetype = SOURCE_JAVA;
universe@19 180 break;
universe@19 181 case 'p':
universe@39 182 sourcetype = SOURCE_PLAIN;
universe@19 183 break;
olaf@24 184 case 'l':
olaf@24 185 settings.showlinenumbers = 0;
olaf@24 186 break;
universe@19 187 case 'h':
universe@19 188 printhelp();
universe@38 189 return EXIT_SUCCESS;
universe@37 190 case 'v':
universe@37 191 case 'V':
universe@37 192 #ifdef VERSION_DEVELOP
universe@37 193 printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR);
universe@37 194 #else
universe@37 195 printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
universe@37 196 #endif
universe@38 197 return EXIT_SUCCESS;
universe@19 198 default:
universe@38 199 return EXIT_FAILURE;
universe@11 200 }
universe@19 201 }
universe@19 202
universe@19 203 if (optind != argc-1) {
universe@11 204 printhelp();
universe@19 205 return 1;
universe@19 206 } else {
universe@19 207 settings.infilename = argv[optind];
universe@22 208 FILE *fout;
universe@22 209 if (settings.outfilename) {
universe@22 210 fout = fopen(settings.outfilename, "w");
universe@22 211 if (!fout) {
universe@22 212 perror("Error opening output file");
universe@38 213 return EXIT_FAILURE;
universe@22 214 }
universe@22 215 } else {
universe@22 216 fout = stdout;
universe@22 217 }
universe@22 218
universe@22 219 if (copyfile(settings.headerfile, fout)) {
universe@22 220 perror("Error opening header file");
universe@38 221 retcode = EXIT_FAILURE;
universe@22 222 goto prog_end;
universe@22 223 }
olaf@24 224
olaf@24 225 highlighter_t highlighter;
olaf@24 226 highlighter_t *hptr = &highlighter;
universe@39 227 switch (sourcetype) {
universe@39 228 case SOURCE_C:
olaf@24 229 init_c_highlighter(&highlighter);
olaf@24 230 break;
universe@39 231 case SOURCE_JAVA:
olaf@24 232 init_java_highlighter(&highlighter);
olaf@24 233 break;
universe@39 234 case SOURCE_PLAIN:
olaf@24 235 hptr = NULL;
olaf@24 236 break;
universe@39 237 default: /* should be unreachable */
universe@39 238 fprintf(stderr, "error in enum source_type\n");
universe@39 239 retcode = EXIT_FAILURE;
universe@39 240 goto prog_end;
olaf@24 241 }
universe@19 242
universe@39 243 FILE *inputfile = fopen(settings.infilename, "r");
universe@19 244 if (inputfile) {
universe@39 245 UcxBuffer *filebuf = ucx_buffer_new(NULL,
universe@39 246 2048, UCX_BUFFER_AUTOEXTEND);
universe@39 247 {
universe@39 248 const size_t tmpbufsize = 512;
universe@39 249 char *tmpbuf = malloc(tmpbufsize);
universe@39 250 ucx_stream_copy(inputfile, filebuf, (read_func) fread,
universe@39 251 (write_func) ucx_buffer_write,
universe@39 252 tmpbuf, tmpbufsize, (size_t)-1);
universe@39 253 free(tmpbuf);
universe@39 254 }
universe@39 255 fclose(inputfile);
universe@39 256
universe@39 257 UcxList *inputlines = ucx_list_append(NULL, filebuf->space);
universe@39 258 for (size_t i = 1 ; i < filebuf->size ; i++) {
universe@39 259 if (filebuf->space[i] == '\r') {
universe@39 260 filebuf->space[i] = '\n'; i++;
universe@39 261 }
universe@39 262 if (filebuf->space[i] == '\n' && i+1 < filebuf->size) {
universe@39 263 ucx_list_append(inputlines, filebuf->space+i+1);
universe@39 264 }
universe@39 265 }
universe@39 266
olaf@24 267 formatfile(
olaf@24 268 hptr,
universe@39 269 inputlines,
universe@39 270 (write_func) fwrite,
olaf@24 271 fout,
olaf@24 272 settings.showlinenumbers);
universe@39 273 ucx_buffer_free(filebuf);
universe@22 274 } else {
universe@22 275 perror("Error opening input file");
universe@38 276 retcode = EXIT_FAILURE;
olaf@24 277 }
olaf@24 278
olaf@24 279 if (copyfile(settings.footerfile, fout)) {
olaf@24 280 perror("Error opening footer file");
universe@38 281 retcode = EXIT_FAILURE;
universe@22 282 }
universe@22 283
universe@22 284 prog_end:
universe@22 285 if (fout != stdout) {
universe@22 286 fclose(fout);
universe@19 287 }
universe@19 288
universe@22 289 return retcode;
universe@11 290 }
universe@1 291 }
universe@1 292

mercurial