]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/gen/strgen.c
Merge commit '7a590c074ceede12b2b6e794f8703d6fa5749918'
[FreeBSD/FreeBSD.git] / contrib / bc / gen / strgen.c
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
7  *
8  * 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.
29  *
30  * *****************************************************************************
31  *
32  * Generates a const array from a bc script.
33  *
34  */
35
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <errno.h>
42
43 #ifndef _WIN32
44 #include <libgen.h>
45 #endif // _WIN32
46
47 static const char* const bc_gen_header =
48         "// Copyright (c) 2018-2021 Gavin D. Howard and contributors.\n"
49         "// Licensed under the 2-clause BSD license.\n"
50         "// *** AUTOMATICALLY GENERATED FROM %s. DO NOT MODIFY. ***\n\n";
51
52 static const char* const bc_gen_label = "const char *%s = \"%s\";\n\n";
53 static const char* const bc_gen_label_extern = "extern const char *%s;\n\n";
54 static const char* const bc_gen_ifdef = "#if %s\n";
55 static const char* const bc_gen_endif = "#endif // %s\n";
56 static const char* const bc_gen_name = "const char %s[] = {\n";
57 static const char* const bc_gen_name_extern = "extern const char %s[];\n\n";
58
59 #define IO_ERR (1)
60 #define INVALID_INPUT_FILE (2)
61 #define INVALID_PARAMS (3)
62
63 #define MAX_WIDTH (74)
64
65 static void open_file(FILE** f, const char* filename, const char* mode) {
66
67 #ifndef _WIN32
68         *f = fopen(filename, mode);
69 #else // _WIN32
70         *f = NULL;
71         fopen_s(f, filename, mode);
72 #endif // _WIN32
73 }
74
75 static int output_label(FILE* out, const char* label, const char* name) {
76
77 #ifndef _WIN32
78         return fprintf(out, bc_gen_label, label, name);
79 #else // _WIN32
80
81         size_t i, count = 0, len = strlen(name);
82         char* buf;
83         int ret;
84
85         for (i = 0; i < len; ++i) {
86                 count += (name[i] == '\\');
87         }
88
89         buf = (char*) malloc(len + 1 + count);
90         if (buf == NULL) return -1;
91
92         count = 0;
93
94         for (i = 0; i < len; ++i) {
95                 buf[i + count] = name[i];
96                 if (name[i] == '\\') {
97                         count += 1;
98                         buf[i + count] = name[i];
99                 }
100         }
101
102         buf[i + count] = '\0';
103
104         ret = fprintf(out, bc_gen_label, label, buf);
105
106         free(buf);
107
108         return ret;
109
110 #endif // _WIN32
111 }
112
113 int main(int argc, char *argv[]) {
114
115         FILE *in, *out;
116         char *label, *define, *name;
117         int c, count, slashes, err = IO_ERR;
118         bool has_label, has_define, remove_tabs;
119
120         if (argc < 5) {
121                 printf("usage: %s input output name header [label [define [remove_tabs]]]\n", argv[0]);
122                 return INVALID_PARAMS;
123         }
124
125         name = argv[3];
126
127         has_label = (argc > 4 && strcmp("", argv[4]) != 0);
128         label = has_label ? argv[4] : "";
129
130         has_define = (argc > 5 && strcmp("", argv[5]) != 0);
131         define = has_define ? argv[5] : "";
132
133         remove_tabs = (argc > 6);
134
135         open_file(&in, argv[1], "r");
136         if (!in) return INVALID_INPUT_FILE;
137
138         open_file(&out, argv[2], "w");
139         if (!out) goto out_err;
140
141         if (fprintf(out, bc_gen_header, argv[1]) < 0) goto err;
142         if (has_label && fprintf(out, bc_gen_label_extern, label) < 0) goto err;
143         if (fprintf(out, bc_gen_name_extern, name) < 0) goto err;
144         if (has_define && fprintf(out, bc_gen_ifdef, define) < 0) goto err;
145         if (has_label && output_label(out, label, argv[1]) < 0) goto err;
146         if (fprintf(out, bc_gen_name, name) < 0) goto err;
147
148         c = count = slashes = 0;
149
150         while (slashes < 2 && (c = fgetc(in)) >= 0) {
151                 slashes += (slashes == 1 && c == '/' && fgetc(in) == '\n');
152                 slashes += (!slashes && c == '/' && fgetc(in) == '*');
153         }
154
155         if (c < 0) {
156                 err = INVALID_INPUT_FILE;
157                 goto err;
158         }
159
160         while ((c = fgetc(in)) == '\n');
161
162         while (c >= 0) {
163
164                 int val;
165
166                 if (!remove_tabs || c != '\t') {
167
168                         if (!count && fputc('\t', out) == EOF) goto err;
169
170                         val = fprintf(out, "%d,", c);
171                         if (val < 0) goto err;
172
173                         count += val;
174
175                         if (count > MAX_WIDTH) {
176                                 count = 0;
177                                 if (fputc('\n', out) == EOF) goto err;
178                         }
179                 }
180
181                 c = fgetc(in);
182         }
183
184         if (!count && (fputc(' ', out) == EOF || fputc(' ', out) == EOF)) goto err;
185         if (fprintf(out, "0\n};\n") < 0) goto err;
186
187         err = (has_define && fprintf(out, bc_gen_endif, define) < 0);
188
189 err:
190         fclose(out);
191 out_err:
192         fclose(in);
193         return err;
194 }