#ifdef-#endifdef, #define, #setdef

Akin to #if, a second class of preprocessor directives consists of:  #ifdef, #ifndef, #elifdef, #elifndef, #elsedef, #endifdef, #define, #undefine, and #setdef.  Use the #ifdef family of directives to customize configurations according to conditions you specify or based on the current system or network state.

The format is

#ifdef [!] <define>                            [also: #ifndef]
        <lines>                [optional]
#elifdef <define>              [optional]      [or: #elsifdef,
                                                        #elseifdef]
                                               [also: #elifndef,
                                                      #elsifndef,
                                                      #elseifndef]
        <lines>                [optional]
#elsedef                       [optional]
        <lines>                [optional]
#endifdef                                      [or: #fidef]

#define <define>                               [or: #def]

#undefine <define>                             [or: #undef]

#setdef <define> = TRUE|true|YES|yes|ON|on|1|
                   FALSE|false|NO|no|OFF|off|0|
                   [!] <define>|
                   [!] [<proc>]
where <define> is an identifier representing a form of logical switch that is either defined (true) or undefined (false).

#ifdef means "if <define> is true".  #ifndef means "#ifdef [not] <define>", or in other words, "if <define> is not true".

#ifdef ! <define> is equivalent to #ifndef <define>.  Similarly, #ifndef ! <define> is equivalent to #ifdef <define>.

#setdef <define> = TRUE is equivalent to #define <define>.  Similarly, #setdef <define> = FALSE is equivalent to #undefine <define>.

#setdef <define> = ! TRUE is equivalent to #setdef <define> = FALSE.  Similarly, #setdef <define> = ! FALSE is equivalent to #setdef <define> = TRUE.

You may #setdef a <define> equal to the exit status (TRUE or FALSE) of any process, for example

#setdef paranoid = [test `hostname` = "kerberos"]
Logical defines are set (to TRUE) or unset (to FALSE) in any of three ways: (a) in the file defines.cfg; (b) in any config file, except systems.cfg or defines.cfg, by means of the #define, #undefine, or #setdef directives; or (c) at the command line, by means of either the +D (sets to TRUE) or -D (unsets to FALSE) switches.  (See defines.cfg and piktc for further explanation.)

#def(ine), #undef(ine), and #setdef statements in the config files have the highest precedence, and command-line defines/undefines override any settings in the defines.cfg file.

If you #def(ine), #undef(ine), or #setdef an identifier in a config file, else use -/+D <identifier> at the command line, without first declaring that identifier in defines.cfg, that will generate an error.

You can set and unset a define throughout the config files by a succession #def(ine), #undef(ine), and #setdef preprocessor statements.  #def(ine), #undef(ine), and #setdef statements have global effect.  That is, if you set (or unset) a define in one file, it remains set (or unset) through the remainder of that file and on into the next (unless or until you change its setting by a new #def(ine), #undef(ine), or #setdef directive).

If you want to turn on debugging temporarily, then switch it back to its former state, you could do this

#ifdef debug
#  define deftmp        // deftmp must be registered in defines.cfg
#elsedef
#  undefine deftmp
#endifdef

#define debug

...

#ifdef deftmp
#  define debug
#elsedef
#  undefine debug
#endifdef
Or, you can achieve the same effect in this shorthand form:
#setdef deftmp = debug

#define debug                        [or: #setdef debug = TRUE]

...

#setdef debug = deftmp
In other words, you can assign one switch's logical value to another's by using #setdef <define1> = <define2>.  (In the above example, it is irrelevant what you set deftmp to in defines.cfg.  If you fail to register deftmp--or any other define--in defines.cfg, and simply reference it later on in your config files, the preprocessor will complain about an "unknown define".)

#elsifdef and #elseifdef are alternate forms of #elifdef, #elsifndef and #elseifndef are alternate forms of #elifndef, #define is synonymous with #def, #undefine is synonymous with #undef, and #fidef is interchangeable with #endifdef.

You can nest #ifdef-#endifdef's up to sixty four levels deep.

prev page 1st page next page
Home | FAQ | News | Intro | Samples | Tutorial | Reference | Software | DevNotes | Licensing | Authors | Pikt-Users | Pikt-Workers | Links | SiteIndex | ContactUs
Page best viewed at 1024x768.   Page last updated 2006-09-10.   This site is PIKT® powered.
PIKT® is a registered trademark of the University of Chicago.   Copyright © 1998-2006 Robert Osterlund.  All rights reserved.
Home FAQ News Intro Samples Tutorial Reference Software
PIKT Logo
PIKT Page Title