src/c2html.c

Tue, 23 Aug 2016 14:31:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 14:31:02 +0200
changeset 43
a8cee98c8832
parent 42
7f2403c637a7
child 44
2b4ac35d061d
permissions
-rw-r--r--

cleans up main function

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

mercurial