Mercurial > molko
changeset 223:560303066120
tools: sync latest bcc
While here rename to mlk-bcc like other tools.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 18 Nov 2020 19:09:23 +0100 |
parents | bf7169bb054d |
children | 6f6c49b95f77 |
files | cmake/MolkoBuildAssets.cmake tools/bcc/CMakeLists.txt tools/bcc/bcc.c tools/bcc/main.c |
diffstat | 4 files changed, 179 insertions(+), 140 deletions(-) [+] |
line wrap: on
line diff
--- a/cmake/MolkoBuildAssets.cmake Wed Nov 18 19:03:47 2020 +0100 +++ b/cmake/MolkoBuildAssets.cmake Wed Nov 18 19:09:23 2020 +0100 @@ -38,10 +38,10 @@ add_custom_command( OUTPUT ${output} - COMMAND $<TARGET_FILE:bcc> ${arg0} -s ${a} ${category}-${name} > ${output} + COMMAND $<TARGET_FILE:mlk-bcc> ${arg0} -csu ${a} ${category}-${name} > ${output} DEPENDS ${a} - $<TARGET_FILE:bcc> + $<TARGET_FILE:mlk-bcc> COMMENT "Generate header file from ${basename}" )
--- a/tools/bcc/CMakeLists.txt Wed Nov 18 19:03:47 2020 +0100 +++ b/tools/bcc/CMakeLists.txt Wed Nov 18 19:09:23 2020 +0100 @@ -17,4 +17,4 @@ # project(molko-bcc) -molko_define_executable(TARGET bcc SOURCES bcc.c FOLDER tools) +molko_define_executable(TARGET mlk-bcc SOURCES main.c FOLDER tools)
--- a/tools/bcc/bcc.c Wed Nov 18 19:03:47 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,137 +0,0 @@ -/* - * molko-bcc.c -- simple binary compiler - * - * Copyright (c) 2020 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#define _XOPEN_SOURCE 700 -#include <errno.h> -#include <stdarg.h> -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> -#include <stdnoreturn.h> -#include <string.h> -#include <unistd.h> - -static const char *charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; -static bool fstatic; -static bool nullterm; - -noreturn static void -usage(void) -{ - fprintf(stderr, "usage: molko-bcc [-s] input varname\n"); - exit(1); -} - -noreturn static void -die(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - fputs("abort: ", stderr); - vfprintf(stderr, fmt, ap); - va_end(ap); - exit(1); -} - -static char * -mangle(char *variable) -{ - char *p; - size_t pos; - - /* Remove extension. */ - if ((p = strrchr(variable, '.'))) - *p = '\0'; - - /* Remove disallowed characters. */ - while ((pos = strspn(variable, charset)) != strlen(variable)) - variable[pos] = '_'; - - return variable; -} - -static void -process(const char *input, const char *variable) -{ - FILE *fp; - int ch, idx = 0; - - if (strcmp(input, "-") == 0) - fp = stdin; - else if (!(fp = fopen(input, "rb"))) - die("%s: %s\n", input, strerror(errno)); - - if (fstatic) - printf("static "); - - printf("const unsigned char %s[] = {\n", variable); - - while ((ch = fgetc(fp)) != EOF) { - if (idx == 0) - putchar('\t'); - - printf("0x%02x, ", (unsigned char)ch); - - if (++idx == 4) { - idx = 0; - putchar('\n'); - } - } - - /* Add final '\0' if requested. */ - if (nullterm) { - if (idx++ == 0) - putchar('\t'); - - printf("0x00"); - } - - if (idx != 0) - printf("\n"); - - puts("};"); - fclose(fp); -} - -int -main(int argc, char **argv) -{ - int ch; - - while ((ch = getopt(argc, argv, "0s")) != -1) { - switch (ch) { - case '0': - nullterm = true; - break; - case 's': - fstatic = true; - break; - default: - break; - } - } - - argc -= optind; - argv += optind; - - if (argc < 2) - usage(); - - process(argv[0], mangle(argv[1])); -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/bcc/main.c Wed Nov 18 19:09:23 2020 +0100 @@ -0,0 +1,176 @@ +/* + * main.c -- binary to C/C++ arrays converter + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <errno.h> +#include <stdarg.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdnoreturn.h> +#include <string.h> +#include <unistd.h> + +static const char *charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; +static char findentchar = '\t'; +static int findent = 1; +static bool fconst; +static bool fnull; +static bool fstatic; +static bool funsigned; + +noreturn static void +usage(void) +{ + fprintf(stderr, "usage: bcc [-0csu] [-I tab-indent] [-i space-indent] input variable\n"); + exit(1); +} + +noreturn static void +die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fputs("abort: ", stderr); + vfprintf(stderr, fmt, ap); + va_end(ap); + exit(1); +} + +static char * +mangle(char *variable) +{ + char *p; + size_t pos; + + /* Remove extension. */ + if ((p = strrchr(variable, '.'))) + *p = '\0'; + + /* Remove disallowed characters. */ + while ((pos = strspn(variable, charset)) != strlen(variable)) + variable[pos] = '_'; + + return variable; +} + +static void +indent(void) +{ + for (int i = 0; i < findent; ++i) + putchar(findentchar); +} + +static void +put(int ch) +{ + if (funsigned) + printf("0x%02hhx", (unsigned char)ch); + else + printf("%hhd", (signed char)ch); +} + +static void +process(const char *input, const char *variable) +{ + FILE *fp; + int ch, col = 0; + + if (strcmp(input, "-") == 0) + fp = stdin; + else if (!(fp = fopen(input, "rb"))) + die("%s: %s\n", input, strerror(errno)); + + if (fstatic) + printf("static "); + if (fconst) + printf("const "); + + printf(funsigned ? "unsigned " : "signed "); + printf("char %s[] = {\n", variable); + + for (ch = fgetc(fp); ch != EOF; ) { + if (col == 0) + indent(); + + put(ch); + + if ((ch = fgetc(fp)) != EOF || fnull) + printf(",%s", col < 3 ? " " : ""); + + if (++col == 4) { + col = 0; + putchar('\n'); + } + + /* Add final '\0' if required. */ + if (ch == EOF && fnull) { + if (col++ == 0) + indent(); + + put(0); + } + } + + if (col != 0) + printf("\n"); + + puts("};"); + fclose(fp); +} + +int +main(int argc, char **argv) +{ + int ch; + + while ((ch = getopt(argc, argv, "0cI:i:su")) != -1) { + switch (ch) { + case '0': + fnull = true; + break; + case 'c': + fconst = true; + break; + case 'I': + findentchar = '\t'; + findent = atoi(optarg); + break; + case 'i': + findentchar = ' '; + findent = atoi(optarg); + break; + case 's': + fstatic = true; + break; + case 'u': + funsigned = true; + break; + default: + break; + } + } + + argc -= optind; + argv += optind; + + if (argc < 2) + usage(); + + process(argv[0], mangle(argv[1])); +}