1 // SPDX-License-Identifier: MIT
3 * Copyright © 2023 Intel Corporation
14 "// SPDX-License-Identifier: MIT\n" \
17 " * DO NOT MODIFY.\n" \
19 " * This file was generated from rules: %s\n" \
21 "#ifndef _GENERATED_XE_WA_OOB_\n" \
22 "#define _GENERATED_XE_WA_OOB_\n" \
31 static void print_usage(FILE *f)
33 fprintf(f, "usage: %s <input-rule-file> <generated-c-source-file> <generated-c-header-file>\n",
34 program_invocation_short_name);
37 static void print_parse_error(const char *err_msg, const char *line,
40 fprintf(stderr, "ERROR: %s\nERROR: %u: %.60s\n",
41 err_msg, lineno, line);
44 static char *strip(char *line, size_t linelen)
46 while (isspace(*(line + linelen)))
49 line[linelen - 1] = '\0';
51 return line + strspn(line, " \f\n\r\t\v");
54 #define MAX_LINE_LEN 4096
55 static int parse(FILE *input, FILE *csource, FILE *cheader)
57 char line[MAX_LINE_LEN + 1];
58 char *name, *prev_name = NULL, *rules;
59 unsigned int lineno = 0, idx = 0;
61 while (fgets(line, sizeof(line), input)) {
65 if (line[0] == '\0' || line[0] == '#' || line[0] == '\n') {
70 linelen = strlen(line);
71 if (linelen == MAX_LINE_LEN) {
72 print_parse_error("line too long", line, lineno);
76 is_continuation = isspace(line[0]);
77 name = strip(line, linelen);
79 if (!is_continuation) {
80 name = strtok(name, " \t");
81 rules = strtok(NULL, "");
84 print_parse_error("invalid rule continuation",
93 if (rules[0] == '\0') {
94 print_parse_error("invalid empty rule\n", line, lineno);
99 fprintf(cheader, "\tXE_WA_OOB_%s = %u,\n", name, idx);
100 fprintf(csource, "{ XE_RTP_NAME(\"%s\"), XE_RTP_RULES(%s) },\n",
103 fprintf(csource, "{ XE_RTP_NAME(NULL), XE_RTP_RULES(%s) },\n",
109 if (!is_continuation)
113 fprintf(cheader, "\t_XE_WA_OOB_COUNT = %u\n", idx);
118 int main(int argc, const char *argv[])
131 [ARGS_INPUT] = { .fn = argv[1], .mode = "r" },
132 [ARGS_CSOURCE] = { .fn = argv[2], .mode = "w" },
133 [ARGS_CHEADER] = { .fn = argv[3], .mode = "w" },
138 fprintf(stderr, "ERROR: wrong arguments\n");
143 for (int i = 0; i < _ARGS_COUNT; i++) {
144 args[i].f = fopen(args[i].fn, args[i].mode);
146 fprintf(stderr, "ERROR: Can't open %s: %m\n",
152 fprintf(args[ARGS_CHEADER].f, HEADER, args[ARGS_INPUT].fn);
153 ret = parse(args[ARGS_INPUT].f, args[ARGS_CSOURCE].f,
154 args[ARGS_CHEADER].f);
156 fprintf(args[ARGS_CHEADER].f, FOOTER);
159 for (int i = 0; i < _ARGS_COUNT; i++) {