xref: /freebsd/contrib/bc/gen/strgen.c (revision 7cc42f6d)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2020 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 #include <libgen.h>
44 
45 static const char* const bc_gen_header =
46 	"// Copyright (c) 2018-2020 Gavin D. Howard and contributors.\n"
47 	"// Licensed under the 2-clause BSD license.\n"
48 	"// *** AUTOMATICALLY GENERATED FROM %s. DO NOT MODIFY. ***\n";
49 
50 static const char* const bc_gen_include = "#include <%s>\n\n";
51 static const char* const bc_gen_label = "const char *%s = \"%s\";\n\n";
52 static const char* const bc_gen_ifdef = "#if %s\n";
53 static const char* const bc_gen_endif = "#endif // %s\n";
54 static const char* const bc_gen_name = "const char %s[] = {\n";
55 
56 #define IO_ERR (1)
57 #define INVALID_INPUT_FILE (2)
58 #define INVALID_PARAMS (3)
59 
60 #define MAX_WIDTH (74)
61 
62 int main(int argc, char *argv[]) {
63 
64 	FILE *in, *out;
65 	char *label, *define, *name, *include;
66 	int c, count, slashes, err = IO_ERR;
67 	bool has_label, has_define, remove_tabs;
68 
69 	if (argc < 5) {
70 		printf("usage: %s input output name header [label [define [remove_tabs]]]\n", argv[0]);
71 		return INVALID_PARAMS;
72 	}
73 
74 	name = argv[3];
75 	include = argv[4];
76 
77 	has_label = (argc > 5 && strcmp("", argv[5]) != 0);
78 	label = has_label ? argv[5] : "";
79 
80 	has_define = (argc > 6 && strcmp("", argv[6]) != 0);
81 	define = has_define ? argv[6] : "";
82 
83 	remove_tabs = (argc > 7);
84 
85 	in = fopen(argv[1], "r");
86 	if (!in) return INVALID_INPUT_FILE;
87 
88 	out = fopen(argv[2], "w");
89 	if (!out) goto out_err;
90 
91 	if (fprintf(out, bc_gen_header, argv[1]) < 0) goto err;
92 	if (has_define && fprintf(out, bc_gen_ifdef, define) < 0) goto err;
93 	if (fprintf(out, bc_gen_include, include) < 0) goto err;
94 	if (has_label && fprintf(out, bc_gen_label, label, argv[1]) < 0) goto err;
95 	if (fprintf(out, bc_gen_name, name) < 0) goto err;
96 
97 	c = count = slashes = 0;
98 
99 	while (slashes < 2 && (c = fgetc(in)) >= 0) {
100 		slashes += (slashes == 1 && c == '/' && fgetc(in) == '\n');
101 		slashes += (!slashes && c == '/' && fgetc(in) == '*');
102 	}
103 
104 	if (c < 0) {
105 		err = INVALID_INPUT_FILE;
106 		goto err;
107 	}
108 
109 	while ((c = fgetc(in)) == '\n');
110 
111 	while (c >= 0) {
112 
113 		int val;
114 
115 		if (!remove_tabs || c != '\t') {
116 
117 			if (!count && fputc('\t', out) == EOF) goto err;
118 
119 			val = fprintf(out, "%d,", c);
120 			if (val < 0) goto err;
121 
122 			count += val;
123 
124 			if (count > MAX_WIDTH) {
125 				count = 0;
126 				if (fputc('\n', out) == EOF) goto err;
127 			}
128 		}
129 
130 		c = fgetc(in);
131 	}
132 
133 	if (!count && (fputc(' ', out) == EOF || fputc(' ', out) == EOF)) goto err;
134 	if (fprintf(out, "0\n};\n") < 0) goto err;
135 
136 	err = (has_define && fprintf(out, bc_gen_endif, define) < 0);
137 
138 err:
139 	fclose(out);
140 out_err:
141 	fclose(in);
142 	return err;
143 }
144