Mon, 26 Mar 2007 23:21:55 -0700
changeset 139 8c536b29e8f09b7864e6c9a452d7aabdb7d9b1f2
parent 1 9b2a99adc05e53cd4010de512f50118594756650
permissions -rw-r--r--
get rid of old backbuffer API and related code. b=371392 r/sr=roc


This is a very primitive line based preprocessor, for times when using
a C preprocessor isn't an option.


Any line starting with a hash # and a letter is considered to be a
preprocessor instruction. Other lines starting with a hash are ignored
as comments.

The following preprocessor instructions are recognised.

   #define VARIABLE
   #undef VARIABLE
   #ifdef VARIABLE
   #ifndef VARIABLE
   #if !VARIABLE
   #elifdef VARIABLE
   #elifndef VARIABLE
   #elif VARIABLE
   #elif !VARIABLE
   #error STRING
   #include FILENAME
   #includesubst @VAR@FILENAME
   #expand STRING
   #literal STRING
   #filter FILTER1 FILTER2 ... FILTERn
   #unfilter FILTER1 FILTER2 ... FILTERn

Whitespace is significant -- for instance, '#define TEST foo' is not
the same as '#define TEST foo '. The first defines TEST to be a three
character string, the second defines it to be four characters long.

The conditionals (#ifdef, #ifndef, #if, #else, #elifdef, #elifndef,
#elif, #endif) can be nested to arbitrary depth.

The #elifdef, #elifndef, and #elif instructions are equivalent to
#else instructions combined with the relevant conditional.  For

   #ifdef foo
      block 1
   #elifdef bar
      block 2

...could be written as:

   #ifdef foo
      block 1
   #ifdef bar
      block 2

An #else block is included if all previous conditions were false, and
is equivalent to #elif 1, i.e.:

   #ifdef foo
      foo is defined
      foo is not defined
   #endif equivalent to:
   #ifdef foo
      foo is defined
   #elif 1
      foo is not defined

#else is not required to be the last condition in an if/el*/endif
series.  In particular, along with #else's equivalence to #elif 1
this means that the following holds:

   #if 0
      never included
      always included
      never included
   #elif 1
      never included

The #error instruction stops execution at this point with a fatal
error. The error message will include the given STRING.

The #include instruction causes the specified file FILENAME to be
recursively processed, as if it was inserted at the current position
in the file. This means conditionals can be started in one file and
ended in another, although this practice is strongly discouraged.
There is no predefined limit to the depth of #includes, and there is
no restriction on self-inclusion, so care should be taken to avoid
infinite loops.

The #includesubst instruction behaves like #include, except that any
variables in @ATSIGNS@ are expanded, like the substitution filter.

The #expand instruction will print the given STRING with variable
substitutions. See the substitution section below.

The #literal instruction will print the given STRING with a newline,
with absolutely no other fixups, guaranteed. This can be used to
output lines starting with a #, which would otherwise be stripped out
as comments.

The #filter instruction enables the specified filters. You can turn
off filters using #unfilter. See the Filters section below.


Variables consist of any alphanumeric string. They are defined using
the -D command line argument and the #define instruction.

To define all environment variables, so that you can use __HOME__,
etc, with #expand, use the -E argument. Note that arguments that use
non-word characters (like "!") are not included. (In particular,
cygwin is known to include all kinds of weird characters in its
environment variables.)

Two special variables are predefined, FILE and LINE. They can be
passed to #define and #undef, but FILE is automatically redefined at
the top of each file, and LINE is increased by one at the start of
each line.

The variable '1' is predefined with value 1. The variable '0' is not
defined. This allows constructs such as

   #if 0
   #endif be used to quickly comment out large sections. Note, however,
that these are simply variables, and can be redefined. This is
strongly discouraged.


In any line starting with the instruction #expand, variable names
contained between double underscores, like __THIS__, are expanded to
their string values, or the empty string if they are not defined.

For example to print the current filename:

   #expand <!-- This file is automatically generated from __FILE__ -->

Normal lines are not affected.

See also the substitution filter below.


The following filters are supported:

     Strips blank lines from the output.

     Strips everything from the first two consecutive slash (/)
     characters until the end of the line.

     Collapses sequences of spaces into a single space.

     Replaces occurrences of "@foo@" by the value of the variable
     "foo". If @foo@ is not defined, the preprocessor will terminate
     with a fatal error.

     Replaces occurrences of "@foo@" by the value of the variable
     "foo". If @foo@ is not defined, the empty string is used instead.

Filters are run in alphabetical order, on a per-line basis.

Command Line Arguments

Syntax: [-Dvariable[=value]] [-E] [-Ffilter]
                   [-Ifilename] [-d] [--marker=<c>] [--] filenames...

   Set variable to 1 before processing the files.

   Set variable to value before processing the files.

   Define all environment variables.

   Enables the specified filter.

   Include filename before any other files.

   Run through the files on the command line, listing the files they
   depend on given the specified environment variables and filters.
   Doesn't recurse into those files. The output is given as one
   dependency per line, and filenames are given relative to the
   current directory.

   Set the type of line endings to use. "type" can be either "cr",
   "lf", or "crlf". The default is whatever your platform uses for
   perl's "\n" character.

   Use the character <c> instead of '#' as the marker for preprocessor

   Indicates the end of non-filename arguments.

   Indicates that input should come from standard input.

If no filenames are provided, standard input is used instead. If many
files are provided, they are processed sequentially, as if they were
one big file. -I files are handled before the other files, in the
order specified, but after handling any -D, -E, -F, and -d arguments.

Contact Details

Feel free to e-mail me if you have any questions:
Ian Hickson <>