Mon, 13 Nov 2017 14:17:46 +0100
removes build/ucx from makefile
universe@55 | 1 | /* |
universe@55 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@55 | 3 | * |
universe@55 | 4 | * Copyright 2016 Mike Becker. All rights reserved. |
universe@55 | 5 | * |
universe@55 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@55 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@55 | 8 | * |
universe@55 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@55 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@55 | 11 | * |
universe@55 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@55 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@55 | 14 | * documentation and/or other materials provided with the distribution. |
universe@55 | 15 | * |
universe@55 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@55 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@55 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@55 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@55 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@55 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@55 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@55 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@55 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@55 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@55 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@55 | 27 | * |
universe@55 | 28 | */ |
universe@55 | 29 | |
universe@55 | 30 | #include <stdio.h> |
universe@55 | 31 | #include <stdlib.h> |
universe@55 | 32 | #include <unistd.h> |
universe@55 | 33 | #include <string.h> |
universe@55 | 34 | |
universe@55 | 35 | #include "c2html.h" |
universe@61 | 36 | #include <ucx/utils.h> |
universe@55 | 37 | |
universe@55 | 38 | #define FILEBUF_SIZE 4096 |
universe@55 | 39 | |
universe@55 | 40 | typedef struct { |
universe@55 | 41 | char* outfilename; |
universe@55 | 42 | char* headerfile; |
universe@55 | 43 | char* footerfile; |
universe@55 | 44 | char* infilename; |
universe@55 | 45 | int showlinenumbers; |
universe@55 | 46 | } Settings; |
universe@55 | 47 | |
universe@55 | 48 | static int appendfile(const char *filename, FILE *fout, |
universe@55 | 49 | char *copybuf, size_t copybuflen, const char *errmsg) { |
universe@55 | 50 | FILE *headerfile = fopen(filename, "r"); |
universe@55 | 51 | if (!headerfile) { |
universe@55 | 52 | perror(errmsg); |
universe@55 | 53 | if (fout != stdout) { |
universe@55 | 54 | fclose(fout); |
universe@55 | 55 | } |
universe@55 | 56 | return 1; |
universe@55 | 57 | } |
universe@61 | 58 | ucx_stream_bncopy(headerfile, fout, |
universe@55 | 59 | (read_func) fread, (write_func) fwrite, |
universe@55 | 60 | copybuf, copybuflen, (size_t)-1); |
universe@55 | 61 | fclose(headerfile); |
universe@55 | 62 | return 0; |
universe@55 | 63 | } |
universe@55 | 64 | |
universe@55 | 65 | static void printhelp() { |
universe@55 | 66 | printf("Formats source code using HTML.\n\nUsage:\n" |
universe@55 | 67 | " c2html [Options] FILE\n\n" |
universe@55 | 68 | " Options:\n" |
universe@55 | 69 | " -h Prints this help message\n" |
universe@55 | 70 | " -j Highlight Java instead of C source code\n" |
universe@55 | 71 | " -o <output> Output file (stdout, if not specified)\n" |
universe@55 | 72 | " -H <header> Prepend header file\n" |
universe@55 | 73 | " -F <footer> Append footer file\n" |
universe@55 | 74 | " -p Disable highlighting (plain text)\n" |
universe@55 | 75 | " -l Disable line numbers\n" |
universe@55 | 76 | " -V, -v Prints version and exits\n" |
universe@55 | 77 | "\n"); |
universe@55 | 78 | } |
universe@55 | 79 | |
universe@55 | 80 | int main(int argc, char** argv) { |
universe@55 | 81 | |
universe@55 | 82 | /* Default settings */ |
universe@55 | 83 | Settings settings; |
universe@55 | 84 | memset(&settings, 0, sizeof(settings)); |
universe@55 | 85 | settings.showlinenumbers = 1; |
universe@55 | 86 | c2html_highlighter_func hltr = c2html_c_highlighter; |
universe@55 | 87 | |
universe@55 | 88 | /* Parse command line */ |
universe@55 | 89 | char optc; |
universe@55 | 90 | while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) { |
universe@55 | 91 | switch (optc) { |
universe@55 | 92 | case 'o': |
universe@55 | 93 | if (!(optarg[0] == '-' && optarg[1] == 0)) { |
universe@55 | 94 | settings.outfilename = optarg; |
universe@55 | 95 | } |
universe@55 | 96 | break; |
universe@55 | 97 | case 'F': |
universe@55 | 98 | settings.footerfile = optarg; |
universe@55 | 99 | break; |
universe@55 | 100 | case 'H': |
universe@55 | 101 | settings.headerfile = optarg; |
universe@55 | 102 | break; |
universe@55 | 103 | case 'j': |
universe@55 | 104 | hltr = c2html_java_highlighter; |
universe@55 | 105 | break; |
universe@55 | 106 | case 'p': |
universe@55 | 107 | hltr = c2html_plain_highlighter; |
universe@55 | 108 | break; |
universe@55 | 109 | case 'l': |
universe@55 | 110 | settings.showlinenumbers = 0; |
universe@55 | 111 | break; |
universe@55 | 112 | case 'h': |
universe@55 | 113 | printhelp(); |
universe@55 | 114 | return EXIT_SUCCESS; |
universe@55 | 115 | case 'v': |
universe@55 | 116 | case 'V': |
universe@55 | 117 | #ifdef VERSION_DEVELOP |
universe@55 | 118 | printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR); |
universe@55 | 119 | #else |
universe@55 | 120 | printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR); |
universe@55 | 121 | #endif |
universe@55 | 122 | return EXIT_SUCCESS; |
universe@55 | 123 | default: |
universe@55 | 124 | return EXIT_FAILURE; |
universe@55 | 125 | } |
universe@55 | 126 | } |
universe@55 | 127 | |
universe@55 | 128 | if (optind != argc-1) { |
universe@55 | 129 | printhelp(); |
universe@55 | 130 | return EXIT_FAILURE; |
universe@55 | 131 | } else { |
universe@55 | 132 | /* Open output file */ |
universe@55 | 133 | settings.infilename = argv[optind]; |
universe@55 | 134 | FILE *fout; |
universe@55 | 135 | if (settings.outfilename) { |
universe@55 | 136 | fout = fopen(settings.outfilename, "w"); |
universe@55 | 137 | if (!fout) { |
universe@55 | 138 | perror("Error opening output file"); |
universe@55 | 139 | return EXIT_FAILURE; |
universe@55 | 140 | } |
universe@55 | 141 | } else { |
universe@55 | 142 | fout = stdout; |
universe@55 | 143 | } |
universe@55 | 144 | |
universe@55 | 145 | /* Allocate file buffer */ |
universe@55 | 146 | char *filebuf = malloc(FILEBUF_SIZE); |
universe@55 | 147 | if (!filebuf) { |
universe@55 | 148 | perror("Error allocating file buffer"); |
universe@55 | 149 | return EXIT_FAILURE; |
universe@55 | 150 | } |
universe@55 | 151 | |
universe@55 | 152 | /* Prepend header file */ |
universe@55 | 153 | if (appendfile(settings.headerfile, fout, filebuf, FILEBUF_SIZE, |
universe@55 | 154 | "Error opening header file")) { |
universe@55 | 155 | return EXIT_FAILURE; |
universe@55 | 156 | } |
universe@55 | 157 | |
universe@55 | 158 | /* Process input file */ |
universe@55 | 159 | FILE *inputfile = fopen(settings.infilename, "r"); |
universe@55 | 160 | if (inputfile) { |
universe@57 | 161 | c2html_fformatf( |
universe@55 | 162 | inputfile, filebuf, FILEBUF_SIZE, |
universe@55 | 163 | fout, hltr, settings.showlinenumbers |
universe@55 | 164 | ); |
universe@55 | 165 | fclose(inputfile); |
universe@55 | 166 | } else { |
universe@55 | 167 | perror("Error opening input file"); |
universe@55 | 168 | if (fout != stdout) { |
universe@55 | 169 | fclose(fout); |
universe@55 | 170 | } |
universe@55 | 171 | return EXIT_FAILURE; |
universe@55 | 172 | } |
universe@55 | 173 | |
universe@55 | 174 | /* Append footer file */ |
universe@55 | 175 | if (appendfile(settings.footerfile, fout, filebuf, FILEBUF_SIZE, |
universe@55 | 176 | "Error opening footer file")) { |
universe@55 | 177 | return EXIT_FAILURE; |
universe@55 | 178 | } |
universe@55 | 179 | |
universe@55 | 180 | free(filebuf); |
universe@55 | 181 | |
universe@55 | 182 | return EXIT_SUCCESS; |
universe@55 | 183 | } |
universe@55 | 184 | } |
universe@55 | 185 |