src/c2html.c

Tue, 23 Aug 2016 15:07:29 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 15:07:29 +0200
changeset 44
2b4ac35d061d
parent 43
a8cee98c8832
child 45
1f3835182aeb
permissions
-rw-r--r--

cleans up formatfile function up to the parser call

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

mercurial