1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/frontend.c Wed Aug 31 14:41:56 2016 +0200 1.3 @@ -0,0 +1,185 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2016 Mike Becker. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + * 1.31 + */ 1.32 + 1.33 +#include <stdio.h> 1.34 +#include <stdlib.h> 1.35 +#include <unistd.h> 1.36 +#include <string.h> 1.37 + 1.38 +#include "c2html.h" 1.39 +#include "ucx/utils.h" 1.40 + 1.41 +#define FILEBUF_SIZE 4096 1.42 + 1.43 +typedef struct { 1.44 + char* outfilename; 1.45 + char* headerfile; 1.46 + char* footerfile; 1.47 + char* infilename; 1.48 + int showlinenumbers; 1.49 +} Settings; 1.50 + 1.51 +static int appendfile(const char *filename, FILE *fout, 1.52 + char *copybuf, size_t copybuflen, const char *errmsg) { 1.53 + FILE *headerfile = fopen(filename, "r"); 1.54 + if (!headerfile) { 1.55 + perror(errmsg); 1.56 + if (fout != stdout) { 1.57 + fclose(fout); 1.58 + } 1.59 + return 1; 1.60 + } 1.61 + ucx_stream_copy(headerfile, fout, 1.62 + (read_func) fread, (write_func) fwrite, 1.63 + copybuf, copybuflen, (size_t)-1); 1.64 + fclose(headerfile); 1.65 + return 0; 1.66 +} 1.67 + 1.68 +static void printhelp() { 1.69 + printf("Formats source code using HTML.\n\nUsage:\n" 1.70 + " c2html [Options] FILE\n\n" 1.71 + " Options:\n" 1.72 + " -h Prints this help message\n" 1.73 + " -j Highlight Java instead of C source code\n" 1.74 + " -o <output> Output file (stdout, if not specified)\n" 1.75 + " -H <header> Prepend header file\n" 1.76 + " -F <footer> Append footer file\n" 1.77 + " -p Disable highlighting (plain text)\n" 1.78 + " -l Disable line numbers\n" 1.79 + " -V, -v Prints version and exits\n" 1.80 + "\n"); 1.81 +} 1.82 + 1.83 +int main(int argc, char** argv) { 1.84 + 1.85 + /* Default settings */ 1.86 + Settings settings; 1.87 + memset(&settings, 0, sizeof(settings)); 1.88 + settings.showlinenumbers = 1; 1.89 + c2html_highlighter_func hltr = c2html_c_highlighter; 1.90 + 1.91 + /* Parse command line */ 1.92 + char optc; 1.93 + while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { 1.94 + switch (optc) { 1.95 + case 'o': 1.96 + if (!(optarg[0] == '-' && optarg[1] == 0)) { 1.97 + settings.outfilename = optarg; 1.98 + } 1.99 + break; 1.100 + case 'F': 1.101 + settings.footerfile = optarg; 1.102 + break; 1.103 + case 'H': 1.104 + settings.headerfile = optarg; 1.105 + break; 1.106 + case 'j': 1.107 + hltr = c2html_java_highlighter; 1.108 + break; 1.109 + case 'p': 1.110 + hltr = c2html_plain_highlighter; 1.111 + break; 1.112 + case 'l': 1.113 + settings.showlinenumbers = 0; 1.114 + break; 1.115 + case 'h': 1.116 + printhelp(); 1.117 + return EXIT_SUCCESS; 1.118 + case 'v': 1.119 + case 'V': 1.120 +#ifdef VERSION_DEVELOP 1.121 + printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); 1.122 +#else 1.123 + printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); 1.124 +#endif 1.125 + return EXIT_SUCCESS; 1.126 + default: 1.127 + return EXIT_FAILURE; 1.128 + } 1.129 + } 1.130 + 1.131 + if (optind != argc-1) { 1.132 + printhelp(); 1.133 + return EXIT_FAILURE; 1.134 + } else { 1.135 + /* Open output file */ 1.136 + settings.infilename = argv[optind]; 1.137 + FILE *fout; 1.138 + if (settings.outfilename) { 1.139 + fout = fopen(settings.outfilename, "w"); 1.140 + if (!fout) { 1.141 + perror("Error opening output file"); 1.142 + return EXIT_FAILURE; 1.143 + } 1.144 + } else { 1.145 + fout = stdout; 1.146 + } 1.147 + 1.148 + /* Allocate file buffer */ 1.149 + char *filebuf = malloc(FILEBUF_SIZE); 1.150 + if (!filebuf) { 1.151 + perror("Error allocating file buffer"); 1.152 + return EXIT_FAILURE; 1.153 + } 1.154 + 1.155 + /* Prepend header file */ 1.156 + if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, 1.157 + "Error opening header file")) { 1.158 + return EXIT_FAILURE; 1.159 + } 1.160 + 1.161 + /* Process input file */ 1.162 + FILE *inputfile = fopen(settings.infilename, "r"); 1.163 + if (inputfile) { 1.164 + c2html_fformat_file( 1.165 + inputfile, filebuf, FILEBUF_SIZE, 1.166 + fout, hltr, settings.showlinenumbers 1.167 + ); 1.168 + fclose(inputfile); 1.169 + } else { 1.170 + perror("Error opening input file"); 1.171 + if (fout != stdout) { 1.172 + fclose(fout); 1.173 + } 1.174 + return EXIT_FAILURE; 1.175 + } 1.176 + 1.177 + /* Append footer file */ 1.178 + if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, 1.179 + "Error opening footer file")) { 1.180 + return EXIT_FAILURE; 1.181 + } 1.182 + 1.183 + free(filebuf); 1.184 + 1.185 + return EXIT_SUCCESS; 1.186 + } 1.187 +} 1.188 +