cpp compatiblity Unix/VMS

Richard A. O'Keefe ok at quintus.uucp
Sat Aug 13 15:29:32 AEST 1988


In article <229 at gsg.UUCP> lew at 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="<strings.h>"	# 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.



More information about the Comp.lang.c mailing list