src/c2html.c

Tue, 23 Aug 2016 15:28:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 15:28:56 +0200
changeset 45
1f3835182aeb
parent 44
2b4ac35d061d
child 46
534a4ef4143d
permissions
-rw-r--r--

changes signature of parser functions to use a UcxBuffer - the functions itself don't use the API yet

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

mercurial