Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | * PERFORMANCE OF THIS SOFTWARE. |
| 16 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 18 | * |
| 19 | * Generates a const array from a bc script. |
| 20 | * |
| 21 | */ |
| 22 | |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 23 | #include <stdbool.h> |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
Gavin Howard | 207053c | 2018-02-20 11:12:43 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | |
Gavin Howard | 207053c | 2018-02-20 11:12:43 -0700 | [diff] [blame] | 30 | #include <libgen.h> |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 31 | |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 32 | static const char* const bc_gen_header = |
Gavin Howard | b52a879 | 2018-09-07 13:37:41 -0600 | [diff] [blame] | 33 | "// Copyright 2018 Gavin D. Howard. Under a 0-clause BSD license.\n" |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 34 | "// *** AUTOMATICALLY GENERATED FROM %s. DO NOT MODIFY. ***\n"; |
| 35 | |
| 36 | static const char* const bc_gen_label = "const char *%s = \"%s\";\n\n"; |
| 37 | static const char* const bc_gen_name = "const char %s[] = {\n"; |
Gavin Howard | fe2351d | 2018-02-19 14:10:15 -0700 | [diff] [blame] | 38 | |
| 39 | #define INVALID_PARAMS (1) |
Gavin Howard | 207053c | 2018-02-20 11:12:43 -0700 | [diff] [blame] | 40 | #define MALLOC_FAIL (2) |
| 41 | #define INVALID_INPUT_FILE (3) |
| 42 | #define INVALID_OUTPUT_FILE (4) |
Gavin Howard | 1ba6bee | 2018-03-08 11:52:45 -0700 | [diff] [blame] | 43 | #define INVALID_HEADER_FILE (5) |
| 44 | #define IO_ERR (6) |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 45 | |
Gavin Howard | 7edf829 | 2018-03-09 02:07:59 -0700 | [diff] [blame] | 46 | #define MAX_WIDTH (74) |
| 47 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 48 | int main(int argc, char *argv[]) { |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 49 | |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 50 | FILE *in, *out; |
Gavin Howard | b52a879 | 2018-09-07 13:37:41 -0600 | [diff] [blame] | 51 | char *label, *name; |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 52 | int c, count, err, slashes; |
| 53 | bool has_label; |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 54 | |
| 55 | err = 0; |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 56 | |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 57 | if (argc < 4) { |
Gavin Howard | 96d9793 | 2018-09-13 14:16:56 -0600 | [diff] [blame] | 58 | printf("usage: gen input output name [label]\n"); |
Gavin Howard | fe2351d | 2018-02-19 14:10:15 -0700 | [diff] [blame] | 59 | return INVALID_PARAMS; |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 62 | name = argv[3]; |
| 63 | |
| 64 | has_label = argc > 4; |
| 65 | label = has_label ? argv[4] : ""; |
| 66 | |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 67 | in = fopen(argv[1], "r"); |
| 68 | |
Gavin Howard | b52a879 | 2018-09-07 13:37:41 -0600 | [diff] [blame] | 69 | if (!in) return INVALID_INPUT_FILE; |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 70 | |
| 71 | out = fopen(argv[2], "w"); |
| 72 | |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 73 | if (!out) { |
| 74 | err = INVALID_OUTPUT_FILE; |
| 75 | goto out_err; |
| 76 | } |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 77 | |
Gavin Howard | b52a879 | 2018-09-07 13:37:41 -0600 | [diff] [blame] | 78 | if (fprintf(out, bc_gen_header, argv[1]) < 0) { |
Gavin Howard | 1ba6bee | 2018-03-08 11:52:45 -0700 | [diff] [blame] | 79 | err = IO_ERR; |
| 80 | goto error; |
| 81 | } |
| 82 | |
Gavin Howard | b52a879 | 2018-09-07 13:37:41 -0600 | [diff] [blame] | 83 | if (has_label && fprintf(out, bc_gen_label, label, argv[1]) < 0) { |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 84 | err = IO_ERR; |
| 85 | goto error; |
| 86 | } |
Gavin Howard | fe2351d | 2018-02-19 14:10:15 -0700 | [diff] [blame] | 87 | |
Gavin Howard | be3a342 | 2018-09-07 13:29:10 -0600 | [diff] [blame] | 88 | if (fprintf(out, bc_gen_name, name) < 0) { |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 89 | err = IO_ERR; |
| 90 | goto error; |
| 91 | } |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 92 | |
Gavin Howard | 7061017 | 2018-09-05 20:30:08 -0600 | [diff] [blame] | 93 | c = count = slashes = 0; |
Gavin Howard | 7edf829 | 2018-03-09 02:07:59 -0700 | [diff] [blame] | 94 | |
| 95 | while (slashes < 2 && (c = fgetc(in)) >= 0) { |
| 96 | if (slashes == 1 && c == '/' && fgetc(in) == '\n') ++slashes; |
| 97 | if (!slashes && c == '/' && fgetc(in) == '*') ++slashes; |
| 98 | } |
| 99 | |
| 100 | if (c < 0) { |
| 101 | err = INVALID_INPUT_FILE; |
| 102 | goto error; |
| 103 | } |
| 104 | |
Gavin Howard | db28f43 | 2018-09-25 15:55:35 -0600 | [diff] [blame] | 105 | c = fgetc(in); |
| 106 | |
| 107 | if (c == '\n') c = fgetc(in); |
| 108 | |
| 109 | while (c >= 0) { |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 110 | |
Gavin Howard | fe2351d | 2018-02-19 14:10:15 -0700 | [diff] [blame] | 111 | int val; |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 112 | |
Gavin Howard | fe2351d | 2018-02-19 14:10:15 -0700 | [diff] [blame] | 113 | if (!count) { |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 114 | if (fprintf(out, " ") < 0) { |
| 115 | err = IO_ERR; |
| 116 | goto error; |
| 117 | } |
Gavin Howard | fe2351d | 2018-02-19 14:10:15 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | val = fprintf(out, "%d,", c); |
| 121 | |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 122 | if (val < 0) { |
| 123 | err = IO_ERR; |
| 124 | goto error; |
| 125 | } |
Gavin Howard | fe2351d | 2018-02-19 14:10:15 -0700 | [diff] [blame] | 126 | |
| 127 | count += val; |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 128 | |
Gavin Howard | 7edf829 | 2018-03-09 02:07:59 -0700 | [diff] [blame] | 129 | if (count > MAX_WIDTH) { |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 130 | |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 131 | count = 0; |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 132 | |
| 133 | if (fputc('\n', out) == EOF) { |
| 134 | err = IO_ERR; |
| 135 | goto error; |
| 136 | } |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 137 | } |
Gavin Howard | db28f43 | 2018-09-25 15:55:35 -0600 | [diff] [blame] | 138 | |
| 139 | c = fgetc(in); |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Gavin Howard | 8bdbb1f | 2018-02-20 16:17:16 -0700 | [diff] [blame] | 142 | if (!count) { |
Gavin Howard | 7edf829 | 2018-03-09 02:07:59 -0700 | [diff] [blame] | 143 | if (fputc(' ', out) == EOF || fputc(' ', out) == EOF) { |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 144 | err = IO_ERR; |
| 145 | goto error; |
| 146 | } |
Gavin Howard | fe2351d | 2018-02-19 14:10:15 -0700 | [diff] [blame] | 147 | } |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 148 | if (fprintf(out, "0\n};\n") < 0) { |
| 149 | err = IO_ERR; |
| 150 | goto error; |
| 151 | } |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 152 | |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 153 | error: |
| 154 | |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 155 | fclose(out); |
| 156 | |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 157 | out_err: |
| 158 | |
| 159 | fclose(in); |
| 160 | |
Gavin Howard | d571acf | 2018-02-20 17:13:07 -0700 | [diff] [blame] | 161 | return err; |
Gavin Howard | f833399 | 2018-02-19 13:55:25 -0700 | [diff] [blame] | 162 | } |