src/frontend.c

Mon, 24 Apr 2023 21:01:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 24 Apr 2023 21:01:41 +0200
changeset 67
5da2cb5aea6b
parent 66
1b12cf799fee
parent 61
47a5fc33590a
child 70
60cecca5e484
permissions
-rw-r--r--

merge upstream changes

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@66 36 #include <cx/utils.h>
universe@55 37
universe@55 38 typedef struct {
universe@55 39 char* outfilename;
universe@55 40 char* headerfile;
universe@55 41 char* footerfile;
universe@55 42 char* infilename;
universe@55 43 int showlinenumbers;
universe@55 44 } Settings;
universe@55 45
universe@66 46 static int appendfile(const char *filename, FILE *fout, const char *errmsg) {
universe@66 47 FILE *fin = fopen(filename, "r");
universe@66 48 if (!fin) {
universe@55 49 perror(errmsg);
universe@55 50 if (fout != stdout) {
universe@55 51 fclose(fout);
universe@55 52 }
universe@55 53 return 1;
universe@55 54 }
universe@66 55 cx_stream_copy(fin, fout, (cx_read_func) fread, (cx_write_func) fwrite);
universe@66 56 fclose(fin);
universe@55 57 return 0;
universe@55 58 }
universe@55 59
universe@55 60 static void printhelp() {
universe@55 61 printf("Formats source code using HTML.\n\nUsage:\n"
universe@55 62 " c2html [Options] FILE\n\n"
universe@55 63 " Options:\n"
universe@55 64 " -h Prints this help message\n"
universe@55 65 " -j Highlight Java instead of C source code\n"
universe@55 66 " -o <output> Output file (stdout, if not specified)\n"
universe@55 67 " -H <header> Prepend header file\n"
universe@55 68 " -F <footer> Append footer file\n"
universe@55 69 " -p Disable highlighting (plain text)\n"
universe@55 70 " -l Disable line numbers\n"
universe@55 71 " -V, -v Prints version and exits\n"
universe@55 72 "\n");
universe@55 73 }
universe@55 74
universe@55 75 int main(int argc, char** argv) {
universe@55 76
universe@55 77 /* Default settings */
universe@55 78 Settings settings;
universe@55 79 memset(&settings, 0, sizeof(settings));
universe@55 80 settings.showlinenumbers = 1;
universe@55 81 c2html_highlighter_func hltr = c2html_c_highlighter;
universe@55 82
universe@55 83 /* Parse command line */
universe@66 84 int optc;
universe@55 85 while ((optc = getopt(argc, argv, "hljo:pH:F:vV")) != -1) {
universe@55 86 switch (optc) {
universe@55 87 case 'o':
universe@55 88 if (!(optarg[0] == '-' && optarg[1] == 0)) {
universe@55 89 settings.outfilename = optarg;
universe@55 90 }
universe@55 91 break;
universe@55 92 case 'F':
universe@55 93 settings.footerfile = optarg;
universe@55 94 break;
universe@55 95 case 'H':
universe@55 96 settings.headerfile = optarg;
universe@55 97 break;
universe@55 98 case 'j':
universe@55 99 hltr = c2html_java_highlighter;
universe@55 100 break;
universe@55 101 case 'p':
universe@55 102 hltr = c2html_plain_highlighter;
universe@55 103 break;
universe@55 104 case 'l':
universe@55 105 settings.showlinenumbers = 0;
universe@55 106 break;
universe@55 107 case 'h':
universe@55 108 printhelp();
universe@55 109 return EXIT_SUCCESS;
universe@55 110 case 'v':
universe@55 111 case 'V':
universe@55 112 #ifdef VERSION_DEVELOP
universe@55 113 printf("%d.%d (unstable)\n", VERSION_MAJOR, VERSION_MINOR);
universe@55 114 #else
universe@55 115 printf("%d.%d\n", VERSION_MAJOR, VERSION_MINOR);
universe@55 116 #endif
universe@55 117 return EXIT_SUCCESS;
universe@55 118 default:
universe@55 119 return EXIT_FAILURE;
universe@55 120 }
universe@55 121 }
universe@55 122
universe@55 123 if (optind != argc-1) {
universe@55 124 printhelp();
universe@55 125 return EXIT_FAILURE;
universe@55 126 } else {
universe@55 127 /* Open output file */
universe@55 128 settings.infilename = argv[optind];
universe@55 129 FILE *fout;
universe@55 130 if (settings.outfilename) {
universe@55 131 fout = fopen(settings.outfilename, "w");
universe@55 132 if (!fout) {
universe@55 133 perror("Error opening output file");
universe@55 134 return EXIT_FAILURE;
universe@55 135 }
universe@55 136 } else {
universe@55 137 fout = stdout;
universe@55 138 }
universe@55 139
universe@55 140 /* Prepend header file */
universe@66 141 if (appendfile(settings.headerfile, fout,
universe@55 142 "Error opening header file")) {
universe@55 143 return EXIT_FAILURE;
universe@55 144 }
universe@55 145
universe@55 146 /* Process input file */
universe@55 147 FILE *inputfile = fopen(settings.infilename, "r");
universe@55 148 if (inputfile) {
universe@66 149 CxBuffer fbuf;
universe@66 150 cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND);
universe@66 151 cx_stream_copy(inputfile, &fbuf, (cx_read_func) fread,
universe@66 152 (cx_write_func) cxBufferWrite);
universe@66 153 fclose(inputfile);
universe@66 154 c2html_bformat(
universe@66 155 fbuf.space, fbuf.size,
universe@66 156 fout, (cx_write_func ) fwrite, hltr,
universe@66 157 settings.showlinenumbers
universe@55 158 );
universe@66 159 cxBufferDestroy(&fbuf);
universe@55 160 } else {
universe@55 161 perror("Error opening input file");
universe@55 162 if (fout != stdout) {
universe@55 163 fclose(fout);
universe@55 164 }
universe@55 165 return EXIT_FAILURE;
universe@55 166 }
universe@55 167
universe@55 168 /* Append footer file */
universe@66 169 if (appendfile(settings.footerfile, fout,
universe@55 170 "Error opening footer file")) {
universe@55 171 return EXIT_FAILURE;
universe@55 172 }
universe@55 173
universe@55 174 return EXIT_SUCCESS;
universe@55 175 }
universe@55 176 }
universe@55 177

mercurial