blob: 63faf1ec34727883f4e3a5a56efa1c0e03337876 [file] [log] [blame]
Gavin Howardf8333992018-02-19 13:55:25 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howardf8333992018-02-19 13:55:25 -07003 *
Gavin Howard29e00ba2020-06-30 09:25:21 -06004 * SPDX-License-Identifier: BSD-2-Clause
5 *
Gavin Howard4feb7082021-01-26 01:19:25 -07006 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
Gavin Howardf8333992018-02-19 13:55:25 -07007 *
Gavin Howard7345cb92019-04-08 14:13:43 -06008 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
Gavin Howardf8333992018-02-19 13:55:25 -070029 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070030 * *****************************************************************************
Gavin Howardf8333992018-02-19 13:55:25 -070031 *
32 * Generates a const array from a bc script.
33 *
34 */
35
Gavin Howardbe3a3422018-09-07 13:29:10 -060036#include <stdbool.h>
Gavin Howardf8333992018-02-19 13:55:25 -070037#include <stdio.h>
Gavin Howard207053c2018-02-20 11:12:43 -070038#include <stdlib.h>
39#include <string.h>
40
Gavin Howardbe3a3422018-09-07 13:29:10 -060041#include <errno.h>
42
Gavin Howard2968e992021-06-18 23:40:15 -060043// For some reason, Windows needs this header.
Gavin Howardf7a64302021-04-04 19:13:24 -060044#ifndef _WIN32
Gavin Howard207053c2018-02-20 11:12:43 -070045#include <libgen.h>
Gavin Howardf7a64302021-04-04 19:13:24 -060046#endif // _WIN32
Gavin Howardf8333992018-02-19 13:55:25 -070047
Gavin Howard2968e992021-06-18 23:40:15 -060048// This is exactly what it looks like. It just slaps a simple license header on
49// the generated C source file.
Gavin Howardbe3a3422018-09-07 13:29:10 -060050static const char* const bc_gen_header =
Gavin Howard4feb7082021-01-26 01:19:25 -070051 "// Copyright (c) 2018-2021 Gavin D. Howard and contributors.\n"
Gavin Howardb64e6522019-02-06 15:22:03 -070052 "// Licensed under the 2-clause BSD license.\n"
Gavin Howard9f9bef42020-10-22 21:37:46 -060053 "// *** AUTOMATICALLY GENERATED FROM %s. DO NOT MODIFY. ***\n\n";
Gavin Howardbe3a3422018-09-07 13:29:10 -060054
Gavin Howard2968e992021-06-18 23:40:15 -060055// These are just format strings used to generate the C source.
Gavin Howardbe3a3422018-09-07 13:29:10 -060056static const char* const bc_gen_label = "const char *%s = \"%s\";\n\n";
Gavin Howard9f9bef42020-10-22 21:37:46 -060057static const char* const bc_gen_label_extern = "extern const char *%s;\n\n";
Gavin Howard64ae6492018-12-26 17:47:34 -070058static const char* const bc_gen_ifdef = "#if %s\n";
Gavin Howardeb5c6ab2018-10-03 11:47:36 -060059static const char* const bc_gen_endif = "#endif // %s\n";
Gavin Howardbe3a3422018-09-07 13:29:10 -060060static const char* const bc_gen_name = "const char %s[] = {\n";
Gavin Howard9f9bef42020-10-22 21:37:46 -060061static const char* const bc_gen_name_extern = "extern const char %s[];\n\n";
Gavin Howardfe2351d2018-02-19 14:10:15 -070062
Gavin Howard2968e992021-06-18 23:40:15 -060063// Error codes. We can't use 0 because these are used as exit statuses, and 0
64// as an exit status is not an error.
Gavin Howardfb442412019-02-06 15:28:53 -070065#define IO_ERR (1)
Gavin Howard1cbfe242019-01-09 17:13:11 -070066#define INVALID_INPUT_FILE (2)
Gavin Howardfb442412019-02-06 15:28:53 -070067#define INVALID_PARAMS (3)
Gavin Howardf8333992018-02-19 13:55:25 -070068
Gavin Howard2968e992021-06-18 23:40:15 -060069// This is the max width to print characters to the screen. This is to ensure
Gavin Howardd72062c2021-06-19 15:45:31 -060070// that lines don't go much over 80 characters.
71#define MAX_WIDTH (72)
Gavin Howard7edf8292018-03-09 02:07:59 -070072
Gavin Howardd72062c2021-06-19 15:45:31 -060073/**
74 * Open a file. This function is to smooth over differences between POSIX and
75 * Windows.
76 * @param f A pointer to the FILE pointer that will be initialized.
77 * @param filename The name of the file.
78 * @param mode The mode to open the file in.
79 */
Gavin Howardbcd5ac12021-04-04 19:56:27 -060080static void open_file(FILE** f, const char* filename, const char* mode) {
Gavin Howardf7a64302021-04-04 19:13:24 -060081
82#ifndef _WIN32
Gavin Howardd72062c2021-06-19 15:45:31 -060083
Gavin Howardf7a64302021-04-04 19:13:24 -060084 *f = fopen(filename, mode);
Gavin Howardd72062c2021-06-19 15:45:31 -060085
Gavin Howardf7a64302021-04-04 19:13:24 -060086#else // _WIN32
Gavin Howardd72062c2021-06-19 15:45:31 -060087
88 // We want the file pointer to be NULL on failure, but fopen_s() is not
89 // guaranteed to set it.
Gavin Howardf7a64302021-04-04 19:13:24 -060090 *f = NULL;
91 fopen_s(f, filename, mode);
Gavin Howardd72062c2021-06-19 15:45:31 -060092
Gavin Howardf7a64302021-04-04 19:13:24 -060093#endif // _WIN32
94}
95
Gavin Howardd72062c2021-06-19 15:45:31 -060096/**
97 * Outputs a label, which is a string literal that the code can use as a name
98 * for the file that is being turned into a string. This is important for the
99 * math libraries because the parse and lex code expects a filename. The label
100 * becomes the filename for the purposes of lexing and parsing.
101 *
102 * The label is generated from bc_gen_label (above). It has the form:
103 *
104 * const char *<label_name> = <label>;
105 *
106 * This function is also needed to smooth out differences between POSIX and
107 * Windows, specifically, the fact that Windows uses backslashes for filenames
108 * and that backslashes have to be escaped in a string literal.
109 *
110 * @param out The file to output to.
111 * @param label The label name.
112 * @param name The actual label text, which is a filename.
113 * @return Positive if no error, negative on error, just like *printf().
114 */
Gavin Howardbcd5ac12021-04-04 19:56:27 -0600115static int output_label(FILE* out, const char* label, const char* name) {
Gavin Howardf7a64302021-04-04 19:13:24 -0600116
117#ifndef _WIN32
Gavin Howardd72062c2021-06-19 15:45:31 -0600118
Gavin Howardf7a64302021-04-04 19:13:24 -0600119 return fprintf(out, bc_gen_label, label, name);
Gavin Howardd72062c2021-06-19 15:45:31 -0600120
Gavin Howardf7a64302021-04-04 19:13:24 -0600121#else // _WIN32
122
123 size_t i, count = 0, len = strlen(name);
124 char* buf;
125 int ret;
126
Gavin Howardd72062c2021-06-19 15:45:31 -0600127 // This loop counts how many backslashes there are in the label.
128 for (i = 0; i < len; ++i) count += (name[i] == '\\');
Gavin Howardf7a64302021-04-04 19:13:24 -0600129
130 buf = (char*) malloc(len + 1 + count);
131 if (buf == NULL) return -1;
132
133 count = 0;
134
Gavin Howardd72062c2021-06-19 15:45:31 -0600135 // This loop is the meat of the Windows version. What it does is copy the
136 // label byte-for-byte, unless it encounters a backslash, in which case, it
137 // copies the backslash twice to have it escaped properly in the string
138 // literal.
Gavin Howardf7a64302021-04-04 19:13:24 -0600139 for (i = 0; i < len; ++i) {
Gavin Howardd72062c2021-06-19 15:45:31 -0600140
Gavin Howardf7a64302021-04-04 19:13:24 -0600141 buf[i + count] = name[i];
Gavin Howardd72062c2021-06-19 15:45:31 -0600142
Gavin Howardf7a64302021-04-04 19:13:24 -0600143 if (name[i] == '\\') {
144 count += 1;
145 buf[i + count] = name[i];
146 }
147 }
148
149 buf[i + count] = '\0';
150
151 ret = fprintf(out, bc_gen_label, label, buf);
152
153 free(buf);
154
155 return ret;
156
157#endif // _WIN32
158}
159
Gavin Howardd72062c2021-06-19 15:45:31 -0600160/**
161 * This program generates C strings (well, actually, C char arrays) from text
162 * files. It generates 1 C source file. The resulting file has this structure:
163 *
164 * <Copyright Header>
165 *
166 * [<Label Extern>]
167 *
168 * <Char Array Extern>
169 *
170 * [<Preprocessor Guard Begin>]
171 * [<Label Definition>]
172 *
173 * <Char Array Definition>
174 * [<Preprocessor Guard End>]
175 *
176 * Anything surrounded by square brackets may not be in the final generated
177 * source file.
178 *
Gavin Howarddbe68542021-06-19 17:20:08 -0600179 * The required command-line parameters are:
Gavin Howardd72062c2021-06-19 15:45:31 -0600180 *
Gavin Howarddbe68542021-06-19 17:20:08 -0600181 * input Input filename.
182 * output Output filename.
183 * name The name of the char array.
Gavin Howardd72062c2021-06-19 15:45:31 -0600184 *
185 * The optional parameters are:
186 *
Gavin Howarddbe68542021-06-19 17:20:08 -0600187 * label If given, a label for the char array. See the comment for the
188 * output_label() function. It is meant as a "filename" for the
189 * text when processed by bc and dc. If label is given, then the
190 * <Label Extern> and <Label Definition> will exist in the
191 * generated source file.
192 * define If given, a preprocessor macro that should be used as a guard
193 * for the char array and its label. If define is given, then
194 * <Preprocessor Guard Begin> will exist in the form
195 * "#if <define>" as part of the generated source file, and
196 * <Preprocessor Guard End> will exist in the form
197 * "endif // <define>".
198 * remove_tabs If this parameter exists, it must be an integer. If it is
199 * non-zero, then tabs are removed from the input file text before
200 * outputting to the output char array.
Gavin Howardd72062c2021-06-19 15:45:31 -0600201 *
202 * All text files that are transformed have license comments. This program finds
203 * the end of that comment and strips it out as well.
204 */
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700205int main(int argc, char *argv[]) {
Gavin Howardf8333992018-02-19 13:55:25 -0700206
Gavin Howard2b6631f2018-12-13 17:35:08 -0700207 FILE *in, *out;
Gavin Howard82bb8702020-11-25 22:17:02 -0700208 char *label, *define, *name;
Gavin Howard31065f52019-02-06 15:22:45 -0700209 int c, count, slashes, err = IO_ERR;
Gavin Howard2b6631f2018-12-13 17:35:08 -0700210 bool has_label, has_define, remove_tabs;
Gavin Howardd571acf2018-02-20 17:13:07 -0700211
Gavin Howardd72062c2021-06-19 15:45:31 -0600212 if (argc < 4) {
Gavin Howarddbe68542021-06-19 17:20:08 -0600213 printf("usage: %s input output name [label [define [remove_tabs]]]\n",
Gavin Howardd72062c2021-06-19 15:45:31 -0600214 argv[0]);
Gavin Howard2b6631f2018-12-13 17:35:08 -0700215 return INVALID_PARAMS;
216 }
Gavin Howardf8333992018-02-19 13:55:25 -0700217
Gavin Howard2b6631f2018-12-13 17:35:08 -0700218 name = argv[3];
Gavin Howardbe3a3422018-09-07 13:29:10 -0600219
Gavin Howard82bb8702020-11-25 22:17:02 -0700220 has_label = (argc > 4 && strcmp("", argv[4]) != 0);
221 label = has_label ? argv[4] : "";
Gavin Howardeb5c6ab2018-10-03 11:47:36 -0600222
Gavin Howard82bb8702020-11-25 22:17:02 -0700223 has_define = (argc > 5 && strcmp("", argv[5]) != 0);
224 define = has_define ? argv[5] : "";
Gavin Howard1cbfe242019-01-09 17:13:11 -0700225
Gavin Howard82bb8702020-11-25 22:17:02 -0700226 remove_tabs = (argc > 6);
Gavin Howard3f9b5742018-12-13 17:31:00 -0700227
Gavin Howardf7a64302021-04-04 19:13:24 -0600228 open_file(&in, argv[1], "r");
Gavin Howard2b6631f2018-12-13 17:35:08 -0700229 if (!in) return INVALID_INPUT_FILE;
Gavin Howardf8333992018-02-19 13:55:25 -0700230
Gavin Howardf7a64302021-04-04 19:13:24 -0600231 open_file(&out, argv[2], "w");
Gavin Howard31065f52019-02-06 15:22:45 -0700232 if (!out) goto out_err;
Gavin Howardf8333992018-02-19 13:55:25 -0700233
Gavin Howard31065f52019-02-06 15:22:45 -0700234 if (fprintf(out, bc_gen_header, argv[1]) < 0) goto err;
Gavin Howard9f9bef42020-10-22 21:37:46 -0600235 if (has_label && fprintf(out, bc_gen_label_extern, label) < 0) goto err;
236 if (fprintf(out, bc_gen_name_extern, name) < 0) goto err;
Gavin Howard31065f52019-02-06 15:22:45 -0700237 if (has_define && fprintf(out, bc_gen_ifdef, define) < 0) goto err;
Gavin Howardf7a64302021-04-04 19:13:24 -0600238 if (has_label && output_label(out, label, argv[1]) < 0) goto err;
Gavin Howard31065f52019-02-06 15:22:45 -0700239 if (fprintf(out, bc_gen_name, name) < 0) goto err;
Gavin Howardf8333992018-02-19 13:55:25 -0700240
Gavin Howard2b6631f2018-12-13 17:35:08 -0700241 c = count = slashes = 0;
Gavin Howard7edf8292018-03-09 02:07:59 -0700242
Gavin Howardd72062c2021-06-19 15:45:31 -0600243 // This is where the end of the license comment is found.
Gavin Howard2b6631f2018-12-13 17:35:08 -0700244 while (slashes < 2 && (c = fgetc(in)) >= 0) {
Gavin Howard31065f52019-02-06 15:22:45 -0700245 slashes += (slashes == 1 && c == '/' && fgetc(in) == '\n');
246 slashes += (!slashes && c == '/' && fgetc(in) == '*');
Gavin Howard2b6631f2018-12-13 17:35:08 -0700247 }
Gavin Howard7edf8292018-03-09 02:07:59 -0700248
Gavin Howardd72062c2021-06-19 15:45:31 -0600249 // The file is invalid if the end of the license comment could not be found.
Gavin Howard2b6631f2018-12-13 17:35:08 -0700250 if (c < 0) {
251 err = INVALID_INPUT_FILE;
Gavin Howard31065f52019-02-06 15:22:45 -0700252 goto err;
Gavin Howard2b6631f2018-12-13 17:35:08 -0700253 }
Gavin Howard7edf8292018-03-09 02:07:59 -0700254
Gavin Howardd72062c2021-06-19 15:45:31 -0600255 // Do not put extra newlines at the beginning of the char array.
Gavin Howard31065f52019-02-06 15:22:45 -0700256 while ((c = fgetc(in)) == '\n');
Gavin Howarddb28f432018-09-25 15:55:35 -0600257
Gavin Howardd72062c2021-06-19 15:45:31 -0600258 // This loop is what generates the actual char array. It counts how many
259 // chars it has printed per line in order to insert newlines at appropriate
260 // places. It also skips tabs if they should be removed.
Gavin Howard2b6631f2018-12-13 17:35:08 -0700261 while (c >= 0) {
Gavin Howardf8333992018-02-19 13:55:25 -0700262
Gavin Howard2b6631f2018-12-13 17:35:08 -0700263 int val;
Gavin Howardf8333992018-02-19 13:55:25 -0700264
Gavin Howard2b6631f2018-12-13 17:35:08 -0700265 if (!remove_tabs || c != '\t') {
Gavin Howard3f9b5742018-12-13 17:31:00 -0700266
Gavin Howard31065f52019-02-06 15:22:45 -0700267 if (!count && fputc('\t', out) == EOF) goto err;
Gavin Howard3f9b5742018-12-13 17:31:00 -0700268
Gavin Howard2b6631f2018-12-13 17:35:08 -0700269 val = fprintf(out, "%d,", c);
Gavin Howard31065f52019-02-06 15:22:45 -0700270 if (val < 0) goto err;
Gavin Howardfe2351d2018-02-19 14:10:15 -0700271
Gavin Howard2b6631f2018-12-13 17:35:08 -0700272 count += val;
Gavin Howardfe2351d2018-02-19 14:10:15 -0700273
Gavin Howard2b6631f2018-12-13 17:35:08 -0700274 if (count > MAX_WIDTH) {
Gavin Howard2b6631f2018-12-13 17:35:08 -0700275 count = 0;
Gavin Howard31065f52019-02-06 15:22:45 -0700276 if (fputc('\n', out) == EOF) goto err;
Gavin Howard2b6631f2018-12-13 17:35:08 -0700277 }
278 }
Gavin Howarddb28f432018-09-25 15:55:35 -0600279
Gavin Howard2b6631f2018-12-13 17:35:08 -0700280 c = fgetc(in);
281 }
Gavin Howardf8333992018-02-19 13:55:25 -0700282
Gavin Howardd72062c2021-06-19 15:45:31 -0600283 // Make sure the end looks nice and insert the NUL byte at the end.
Gavin Howard31065f52019-02-06 15:22:45 -0700284 if (!count && (fputc(' ', out) == EOF || fputc(' ', out) == EOF)) goto err;
285 if (fprintf(out, "0\n};\n") < 0) goto err;
Gavin Howardeb5c6ab2018-10-03 11:47:36 -0600286
Gavin Howardfb442412019-02-06 15:28:53 -0700287 err = (has_define && fprintf(out, bc_gen_endif, define) < 0);
Gavin Howardf8333992018-02-19 13:55:25 -0700288
Gavin Howard31065f52019-02-06 15:22:45 -0700289err:
Gavin Howard2b6631f2018-12-13 17:35:08 -0700290 fclose(out);
Gavin Howardd571acf2018-02-20 17:13:07 -0700291out_err:
Gavin Howard2b6631f2018-12-13 17:35:08 -0700292 fclose(in);
Gavin Howard2b6631f2018-12-13 17:35:08 -0700293 return err;
Gavin Howardf8333992018-02-19 13:55:25 -0700294}