Path: utzoo!yunexus!geac!syntron!jtsv16!uunet!peregrine!elroy!ames!mailrus!cornell!uw-beaver!teknowledge-vaxc!sri-unix!quintus!ok From: ok@quintus.uucp (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: cpp compatiblity Unix/VMS Message-ID: <286@quintus.UUCP> Date: 13 Aug 88 05:29:32 GMT Article-I.D.: quintus.286 References: <792@cernvax.UUCP> <229@gsg.UUCP> Sender: news@quintus.UUCP Reply-To: ok@quintus.UUCP (Richard A. O'Keefe) Organization: Quintus Computer Systems, Inc. Lines: 54 In article <229@gsg.UUCP> lew@gsg.UUCP (Paul Lew) writes: >Is there a version of cpp that will use Unix environment variables for >include file names? This is something I like to have for a long time. Suggestion: you don't _have_ to do everything with cpp. Write a program that extracts the variables of interest from the environment and writes a header file. For example, I just knocked together the following Bourne shell script to do this (10 minutes, 5 minutes needed to check the manual because I normally use csh): #!/bin/sh # FILE : zabbo # USAGE : zabbo var.... >foobaz.h # EFFECT: # For each var in turn, # if var is defined, then #define var "$var" is written. # otherwise, #undef var is written. # EXAMPLE: zabbo TERM HOME >env.h d='$' q='"' for var do eval "def=is_$d{$var+defined} val=$d{$var-}" if [ $def = is_defined ] then echo "#define $var $q$val$q" else echo "#undef $var" fi done Here's an example: % zabbo TERM HOME NONESUCH >env.h % cat env.h #define TERM "sun" #define HOME "/goedel/ok" #undef NONESUCH How can you use this to select the appropriate header file? Suppose you have an environment variable STRINGS_HDR="" # or whatever then you create a header file with % zabbo STRINGS_HDR ...others... >env.h and in your C program do #include "env.h" #include STRINGS_HDR This works in 4.2, V.2, and V.3. There is no end to the places you might want to get definitions for C macros. This technique of writing a program to extract the information and repackage it for CPP can be used for other things than looking up the environment, and keeps CPP simple.