[net.sources] #defines for portable code

day@kovacs.UUCP (David Yost) (08/02/84)

I have been developing and porting the Rand Editor to
different machines and systems for five years now.
(If you are interested in the significant recent
developments, mail or call 213-650-1089.)

#defined names that tell you the version of the
system, such as SYS5_2 or V7 are not the solution.
Among other problems, there are too many hacked
kernels out there, and many of the hacks are needed,
so you want your program to know they are there.

A large, sophisticated piece of software needs to
know things about the machine, the compiler, the
library functions, and the system calls.  My solution
to the porting issues has been to use two include
files: "c_env.h" for the machine and the compiler,
and "localenv.h" for the library and the kernel.

Most of what follows should be pretty obvious, but
the Regx stuff needs explanation.  Briefly, since the
compiler assigns registers in the order that it
encounters the declarations, if you just declare lots
of registers in case the machine has enough, you can
lose big on machines with fewer registers than the
number you declared.  Instead, I declare them Reg1,
Reg2, ... in the order of importance, and the c_env
#define's take care of the rest.

=== c_env.h
/*
 *   C Environment.
 *   Information about the machine and the compiler.
 *
 *   VAX 4.1bsd & 4.2bsd
 */

/*
 *  In the #define's that follow, define the first n to 'register'
 *  and the rest to nothing, where 'n' is the number of registers
 *  supported by your compiler.
 */
#define Reg1  register
#define Reg2  register
#define Reg3  register
#define Reg4  register
#define Reg5  register
#define Reg6  register
#define Reg7
#define Reg8
#define Reg9
#define Reg10
#define Reg11
#define Reg12

#define CHARMASK   0xFF
#define CHARNBITS  8
#define MAXCHAR	   0x7F

#define SHORTMASK  0xFFFF
#define SHORTNBITS 16
#define MAXSHORT   0x7FFF

#define LONGMASK  0xFFFFFFFF
#define LONGNBITS 32
#define MAXLONG	  0x7FFFFFFF

#define INTMASK	 0xFFFFFFFF
#define INTNBITS 32
#define MAXINT	 0x7FFFFFFF

#define BIGADDR		/* text address space > 64K */
/* fine ADDR64K		/* text and data share 64K of memory (no split I&D */

#define INT4		/* sizeof (int) == 4 */
/* fine INT2		/* sizeof (int) == 2 */

#define PTR4		/* sizeof (char *) == 4 */
/* fine PTR2		/* sizeof (char *) == 2 */

			/* unsigned types supported by the compiler: */
#define UNSCHAR		/* unsigned char  */
#define UNSSHORT	/* unsigned short */
#define UNSLONG		/* unsigned long  */

/* fine NOSIGNEDCHAR	/* there is no signed char type */

#define STRUCTASSIGN	/* Compiler does struct assignments */

#define UNIONS_IN_REGISTERS	/* compiler allows unions in registers */

/* fine void int	/* Fake the new 'void' type to an int */

/* Byte order: */
#define SHORT_10	/* pdp11, vax, 16000, 8086, 8080, ... */
#define LONG_3210	/* vax, 16000, 8086, 8080, ... */
/* fine LONG_0123	/* ibm, perkin-elmer, 68000, pyramid, ... */
/* fine LONG_1032	/* pdp11 */
=== localenv.h
/*
 *   System Environment
 *   Information about the system call interface and the C library.
 *
 *   4.2bsd - vax & pyramid so far
 */

/* fine index strchr		/* */
/* fine rindex strrchr		/* */
/* fine NOIOCTL_H		/* there is no ioctl.h */
/* fine RDNDELAY		/* read call has NDELAY capability */
#define EMPTY			/* can implement empty(fd) subroutine call */
#define LINKS			/* file system has links */
#define CANFORK			/* system has fork() */
#define VFORK			/* system has vfork() */
#define ABORT_SIG SIGILL	/* which signal does abort() use */
#define SIGARG			/* signal catch routine has sig num as arg */
#define SIG_INCL <signal.h>
#define SGTT_INCL <sgtty.h>
#define TTYNAME			/* use ttyname function */
/* define TTYN			/* use ttyn function */
#define SHORTUID		/* uid is a short, not a char (v7+) */
#define ENVIRON			/* getenv() is implemented */
#define VAX_MONITOR		/* use monitor() routine for vax */
#define SIGNALS			/* system has signals */
#define SYMLINKS		/* 4.2 symbolic links */
#define NDIR			/* 4.2 dirrectory routines */
#define SYSMKDIR		/* use mkdir(name, mode) system call */
#define SYSRMDIR		/* use rmdir(name) system call */
#define SYSFCHOWN		/* use fchown(fd, ...) system call */
#define SYSFCHMOD		/* use fchmod(fd, ...) system call */
#define CLR_SUID_ON_WRITE	/* modifying a file clrs suid and sgid bits */
#define SYSSELECT		/* system has berkeley select system call */
#define WAIT_UNION		/* include <sys/wait.h> */
#ifdef	WAIT_UNION
#define WAITARG union wait
#else	WAIT_UNION
#define WAITARG int
#endif	WAIT_UNION
/* fine MALLOC0OK		/* has NULL return arena corruption fix */
=== end