#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.
For example, in macros.cfg, you might define the macros exec, execwait, and outputmail differently depending on whether the doexec and/or verbose defines are set to TRUE or FALSE:
#ifdef doexec exec exec execwait exec wait #elsedef exec output mail execwait output mail #endifdef ... #ifdef verbose outputmail output mail #elsedef outputmail output log "/dev/null" #endifdefAs another example, in the SysAdminsCritical & SysAdminsCriticalTest alerts, you might set different timings (for non-missioncritical systems) whether or not it's a holiday; and whether the pikttest define is set to TRUE or FALSE (by means of adding or not the '-T' option to a piktc command), set a different status and set different alarm script lists:
SysAdminsCritical=pikttest #if missioncritical timing 30 * * * * 5 #else # ifndef holiday timing 30 */2 * * * 5 # elsedef timing =piktnever # endifdef #endif nicecmd "=nice -n 19" mailcmd "=mailx -a 'From: piktadmin' -s 'PIKT Alert on =pikthostname: Critical' =sysadmins-critical" #ifndef pikttest status active #elsedef status testing #endifdef level critical alarms #ifndef pikttest SysReboot DmesgScan #elsedef // pikttest # if ! nometalog SyslogCriticalScan SyslogKernelScan # endif RunawayMEMProcs #endifdef // pikttestAs a final example, in a Pikt script, and if the debug define were set to TRUE, you might have a rule to output debugging info:
#ifdef debug rule output mail "$text(#cpu,1) $text(#mem,1) $basename($proc)" #endifdefIn any of the config files (except systems.cfg, where #ifdef, #endifdef, etc. are now allowed), 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 #endifdefOr, you can achieve the same effect in this shorthand form:
#setdef deftmp = debug #define debug [or: #setdef debug = TRUE] ... #setdef debug = deftmpIn 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.
Refer to the Samples section for many more examples.
prev page | 1st page | next page |