bcc.c

Wed, 21 Oct 2020 15:27:50 +0200

author
David Demelier <markand@malikania.fr>
date
Wed, 21 Oct 2020 15:27:50 +0200
changeset 0
e5fc6565dce0
child 3
b91e030a2040
permissions
-rw-r--r--

misc: initial import

0
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * bcc.c -- binary to C/C++ arrays converter
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <errno.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <stdarg.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include <stdbool.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 #include <stdio.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 #include <stdlib.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 #include <stdnoreturn.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include <string.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
26 #include <unistd.h>
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
27
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 static const char *charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
29 static char findentchar = '\t';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 static int findent = 1;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 static bool fconst;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 static bool fnull;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 static bool fstatic;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 static bool funsigned;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
35
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 noreturn static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 usage(void)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 fprintf(stderr, "usage: bcc [-0csu] [-I tab-indent] [-i space-indent] input variable\n");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
40 exit(1);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
42
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
43 noreturn static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 die(const char *fmt, ...)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
45 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 va_list ap;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
47
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 va_start(ap, fmt);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 fputs("abort: ", stderr);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 vfprintf(stderr, fmt, ap);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 va_end(ap);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 exit(1);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
54
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
55 static char *
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 mangle(char *variable)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 char *p;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 size_t pos;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
60
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
61 /* Remove extension. */
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 if ((p = strrchr(variable, '.')))
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 *p = '\0';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
64
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 /* Remove disallowed characters. */
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
66 while ((pos = strspn(variable, charset)) != strlen(variable))
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 variable[pos] = '_';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
68
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
69 return variable;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
71
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
72 static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
73 indent(void)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
74 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 for (int i = 0; i < findent; ++i)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 putchar(findentchar);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
77 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
78
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
80 put(int ch)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 if (funsigned)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 printf("0x%02hhx", (unsigned char)ch);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 else
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
85 printf("%hhd", (signed char)ch);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
86 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
87
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 static void
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 process(const char *input, const char *variable)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
90 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 FILE *fp;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 int ch, col = 0;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
93
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 if (strcmp(input, "-") == 0)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
95 fp = stdin;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
96 else if (!(fp = fopen(input, "rb")))
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 die("%s: %s\n", input, strerror(errno));
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
98
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
99 if (fstatic)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 printf("static ");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 if (fconst)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 printf("const ");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
103
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
104 printf(funsigned ? "unsigned " : "signed ");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
105 printf("char %s[] = {\n", variable);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
106
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
107 for (ch = fgetc(fp); ch != EOF; ) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
108 if (col == 0)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 indent();
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
110
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
111 put(ch);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
112
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
113 if ((ch = fgetc(fp)) != EOF || fnull)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
114 printf(",%s", col < 3 ? " " : "");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
115
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
116 if (++col == 4) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
117 col = 0;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
118 putchar('\n');
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
119 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
120
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
121 /* Add final '\0' if required. */
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
122 if (ch == EOF && fnull) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
123 if (col++ == 0)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
124 indent();
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
125
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
126 put(0);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
127 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
128 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
129
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
130 if (col != 0)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
131 printf("\n");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
132
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
133 puts("};");
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
134 fclose(fp);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
135 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
136
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
137 int
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
138 main(int argc, char **argv)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
139 {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
140 int ch;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
141
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
142 while ((ch = getopt(argc, argv, "0cI:i:su")) != -1) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
143 switch (ch) {
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
144 case '0':
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
145 fnull = true;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
146 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
147 case 'c':
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
148 fconst = true;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
149 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
150 case 'I':
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
151 findentchar = '\t';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
152 findent = atoi(optarg);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
153 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
154 case 'i':
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
155 findentchar = ' ';
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
156 findent = atoi(optarg);
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
157 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
158 case 's':
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
159 fstatic = true;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
160 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
161 case 'u':
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
162 funsigned = true;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
163 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
164 default:
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
165 break;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
166 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
167 }
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
168
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
169 argc -= optind;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
170 argv += optind;
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
171
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
172 if (argc < 2)
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
173 usage();
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
174
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
175 process(argv[0], mangle(argv[1]));
e5fc6565dce0 misc: initial import
David Demelier <markand@malikania.fr>
parents:
diff changeset
176 }

mercurial