// SPDX-License-Identifier: GPL-3.0-or-later // Adapt CMOC's GCC-style preprocessor invocation to bundled mcpp. Ordinary C // keeps mcpp's standard token separation. Only explicit asm { ... } bodies use // mcpp's documented assembler expansion, which preserves #MACRO and -MACRO,x. #include #include #include #include #include #include #include #include #include typedef struct { size_t start, end; } Block; typedef struct { char *text; size_t size; Block *blocks; size_t count; } Output; static int preprocess(const char *executable, char **args, Output *out) { FILE *capture = tmpfile(); if (!capture) { perror("preprocessor capture"); return 1; } pid_t child = fork(); if (child < 0) { perror("preprocessor fork"); fclose(capture); return 1; } if (!child) { if (dup2(fileno(capture), STDOUT_FILENO) < 0) _exit(126); execv(executable, args); perror("bundled mcpp"); _exit(127); } int status; while (waitpid(child, &status, 0) < 0) { if (errno != EINTR) { fclose(capture); return 1; } } off_t length = ftello(capture); // Guard malformed tools/unbounded inputs; no truncated source is compiled. if (length < 0 || length > 64 * 1024 * 1024 || fseeko(capture, 0, SEEK_SET)) { fputs("Preprocessor output is too large or unreadable\n", stderr); fclose(capture); return 1; } out->size = (size_t)length; out->text = malloc(out->size + 1); if (!out->text || fread(out->text, 1, out->size, capture) != out->size) { fclose(capture); return 1; } out->text[out->size] = 0; fclose(capture); return WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status); } static int identifier(char c) { return isalnum((unsigned char)c) || c == '_'; } static size_t quote_end(const char *s, size_t i, size_t length) { char quote = s[i++]; while (i < length) { if (s[i] == '\\' && i + 1 < length) i += 2; else if (s[i++] == quote) break; } return i; } static int find_blocks(Output *out) { const char *s = out->text; size_t i = 0; while (i < out->size) { // Line markers contain filenames and must never count as source tokens. if ((i == 0 || s[i - 1] == '\n') && s[i] == '#') { while (i < out->size && s[i] != '\n') ++i; } else if (s[i] == '"' || s[i] == '\'') i = quote_end(s, i, out->size); else if (s[i] == '/' && i + 1 < out->size && s[i + 1] == '/') { while (i < out->size && s[i] != '\n') ++i; } else if (s[i] == '/' && i + 1 < out->size && s[i + 1] == '*') { i += 2; while (i + 1 < out->size && !(s[i] == '*' && s[i + 1] == '/')) ++i; i = i + 2 < out->size ? i + 2 : out->size; } else if (i + 3 <= out->size && !memcmp(s + i, "asm", 3) && (i == 0 || !identifier(s[i - 1])) && (i + 3 == out->size || !identifier(s[i + 3]))) { i += 3; while (i < out->size && isspace((unsigned char)s[i])) ++i; if (i == out->size || s[i] != '{') continue; size_t start = ++i; unsigned depth = 1; while (i < out->size && depth) { // Match CMOC's balanced-brace inline-assembly token, retaining // line markers and assembler comments byte for byte. if (s[i] == '{') ++depth; else if (s[i] == '}') --depth; ++i; } if (depth) return 0; Block *grown = realloc(out->blocks, (out->count + 1) * sizeof(Block)); if (!grown) return 0; out->blocks = grown; out->blocks[out->count++] = (Block){ start, i - 1 }; } else ++i; } return 1; } static void emit(const char *text, size_t length) { size_t start = 0; while (start < length) { size_t end = start; while (end < length && text[end] != '\n') ++end; if (end < length) ++end; // mcpp's cur_file() prints raw path bytes inside quotes. Escape them // for CMOC's C-style marker parser; paths may themselves contain quotes. // CMOC accepts GCC markers (# NUMBER), not mcpp's #line NUMBER. size_t number = start; if (end - start >= 6 && !memcmp(text + start, "#line ", 6)) number += 6; else if (end - start >= 2 && text[start] == '#' && text[start + 1] == ' ') number += 2; size_t p = number; while (p < end && isdigit((unsigned char)text[p])) ++p; while (p < end && text[p] == ' ') ++p; size_t last = end; while (last > p && text[last - 1] != '"') --last; if (number != start && p < end && text[p] == '"' && last > p + 1) { fputs("# ", stdout); fwrite(text + number, 1, p + 1 - number, stdout); for (size_t i = p + 1; i < last - 1; ++i) { if (text[i] == '"' || text[i] == '\\') fputc('\\', stdout); fputc(text[i], stdout); } fwrite(text + last - 1, 1, end - last + 1, stdout); } else fwrite(text + start, 1, end - start, stdout); start = end; } } int main(int argc, char **argv) { char executable[PATH_MAX], resolved[PATH_MAX]; uint32_t size = sizeof executable; if (_NSGetExecutablePath(executable, &size) || !realpath(executable, resolved)) { perror("Cannot locate bundled preprocessor"); return 1; } char *last = strrchr(resolved, '/'); if (!last || (size_t)(last - resolved) + sizeof "/mcpp" > sizeof resolved) return 1; strcpy(last + 1, "mcpp"); char **args = calloc((size_t)argc + 9, sizeof *args); if (!args) return 1; int count = 0; args[count++] = resolved; args[count++] = "-e"; args[count++] = "utf8"; args[count++] = "-W0"; for (int i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-xc++")) args[count++] = "-+"; else if (!strcmp(argv[i], "-nostdinc")) args[count++] = "-I-"; else if (!strcmp(argv[i], "-undef")) args[count++] = "-N"; else if (!strcmp(argv[i], "-Wno-builtin-macro-redefined")) continue; else args[count++] = argv[i]; } Output standard = {0}, assembly = {0}; int result = preprocess(resolved, args, &standard); if (!result && !find_blocks(&standard)) { fputs("Cannot identify CMOC inline assembly blocks\n", stderr); result = 1; } if (!result && standard.count) { args[count++] = "-a"; result = preprocess(resolved, args, &assembly); if (!result && (!find_blocks(&assembly) || assembly.count != standard.count)) { fputs("CMOC inline assembly differs between preprocessing modes\n", stderr); result = 1; } if (!result) { size_t offset = 0; for (size_t i = 0; i < standard.count; ++i) { Block a = standard.blocks[i], b = assembly.blocks[i]; emit(standard.text + offset, a.start - offset); emit(assembly.text + b.start, b.end - b.start); offset = a.end; } emit(standard.text + offset, standard.size - offset); } } else if (standard.text) emit(standard.text, standard.size); free(standard.text); free(standard.blocks); free(assembly.text); free(assembly.blocks); free(args); if (ferror(stdout) || fflush(stdout)) return 1; return result; }