C-Minus Preprocessor

C-Minus Preprocessor
Login

C-Minus Preprocessor

The C-minus Preprocessor (a.k.a. c-pp) is a truly minimal C-like preprocessor application. Why? Because C preprocessors can process non-C code but generally make quite a mess of it1. The purpose of this application is an extremely minimal preprocessor with only the most basic functionality of a C preprocessor (see below). It was conceived for use with JavaScript code but is generic enough to be used with essentially arbitrary UTF-8 text (including C code).

Design note: this tool makes use of SQLite. Though not strictly needed in order to implement it, this tool was specifically created for use with the sqlite3 project's own JavaScript code in order to facilitate creation of different builds, so there's no reason not to make use of sqlite3 to do some of the heavy lifting. It does not require any cutting-edge sqlite3 features and should be usable with any version which supports features as old as WITHOUT ROWID.

Formalities

Project home: https://fossil.wanderinghorse.net/r/c-pp

License: same as sqlite3 (see c-pp.c for the full details)

Author: Stephan Beal https://wanderinghorse.net/home/stephan/

Supported Markup

Like a C preprocessor, this tool reads input text files and conditionally filters out parts. Unlike CPP, c-pp does no inline expansion of content. It reads only lines which start with its keyword delimiter in column zero and passes all other input through as-is unless it is elided due to an #if. Like CPP, it accepts spaces between the keyword delimiter and the keyword, plus it accepts backslash-escaped newlines (but doesn't have terribly much use for them).

Note that "#" above is symbolic. The keyword delimiter is configurable and defaults to ##. Define CMPP_DEFAULT_DELIM to a string when compiling to define the default at build-time. The delimiter may be modifed via a command-line flag.

Examples

$ cat my.txt
##if foo
foo
##elif bar
bar
##else
baz
##endif
$ c-pp my.txt -Dfoo
foo
$ c-pp my.txt -Dbar
bar
$ c-pp my.txt
baz
$ cat hosts.txt
%include /etc/hosts
$ c-pp -d % hosts.txt
127.0.0.1	localhost
127.0.1.1	my-computer
...

$ cat hosts.txt
%include hosts
$ c-pp -d % -I/etc hosts.txt
127.0.0.1	localhost
127.0.1.1	my-computer
...

  1. ^ C preprocessors, when running in comment-retention mode, tend to inject # characters all over the place and may do silly things like automatically include compiler-specific headers and emit the comments from those. e.g. using gcc -E -CC will include a gcc-internal header and emit a GPL license header in the output. e.g. try:
    $ echo 'extern int x;' > y.c; gcc -E -CC y.c