/*
 * common.h  -  Common definitions for all mknbi programs
 *
 * Copyright (C) 1996-2003 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: common.h,v 1.7 2003/03/09 00:43:08 gkminix Exp $
 */

#ifndef _COMMON_H
#define _COMMON_H


/*
 * General definitions for compiling on different systems
 */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef __STDC__
# define voidstar void *
#else
# define voidstar char *
#endif

#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
#else
# ifdef HAVE_MALLOC_H
#  include <malloc.h>
# else
extern voidstar malloc();
extern void free();
# endif
#endif

#ifndef O_BINARY
# define O_BINARY 0
#endif

#ifndef __P
# if PROTOTYPES
#  define __P(args) args
# else
#  if defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus)
#   define __P(args) args
#  else
#   define __P(args) ()
#  endif
# endif
#endif

#include <errno.h>
#ifndef errno
extern int errno;
#endif

#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifndef MAXPATHLEN
# ifdef PATH_MAX
#  define MAXPATHLEN PATH_MAX
# else
#  define MAXPATHLEN 1024
# endif
#endif



/*
 * Define some generally useful macros. These are usually
 * already defined in sys/param.h, but on some systems they
 * might not.
 */
#ifndef roundup
# define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
#endif
#ifndef howmany
# define howmany(x, y)	(((x)+((y)-1))/(y))
#endif
#ifndef MIN
# define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#ifndef MAX
# define MAX(a,b) (((a)>(b))?(a):(b))
#endif



/*
 * sys/stat.h will also contain time_t which is required for nblib.h
 */
#include <sys/stat.h>

#ifdef HAVE_ASSERT_H
# include <assert.h>
#else
# undef assert
# define assert(expr)	((void)0)
#endif

#if defined(M_XENIX) && defined(I_286)
#undef __XENIX__
#define __XENIX__
#endif



/*
 * Check for packed attribute. This is necessary to match some C
 * structures to what the bootrom and the assembler modules expect.
 * Without the packed attribute at least the GNU C compiler aligns
 * byte variables to word boundaries which is not always correct.
 * Other compilers might be able to use the pack or align pragmas.
 */
#ifdef USE_PACKED
# define PACKED __attribute__((packed))
# undef USE_PRAGMA_PACK
# undef USE_PRAGMA_ALIGN
#endif

#ifdef USE_PRAGMA_PACK
# define PACKED
# undef USE_PACKED
# undef USE_PRAGMA_ALIGN
#endif

#ifdef USE_PRAGMA_ALIGN
# define PACKED
# undef USE_PACKED
# undef USE_PRAGMA_PACK
#endif



/*
 * sys/types.h and unistd.h are required to include variable types
 * which define certain bit sized units.
 */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_BITYPES_H
# include <sys/bitypes.h>
#endif



/*
 * Define maximum and minimum values for certain data types.
 */
#ifdef HAVE_LIMITS_H
# include <limits.h>
#else
# include <values.h>
# ifndef SHRT_MIN
#  define SHRT_MIN MINSHORT
# endif
# ifndef INT_MIN
#  define INT_MIN MININT
# endif
# ifndef LONG_MIN
#  define LONG_MIN MINLONG
# endif
# ifndef SHRT_MAX
#  define SHRT_MAX MAXSHORT
# endif
# ifndef INT_MAX
#  define INT_MAX MAXINT
# endif
# ifndef UINT_MAX
#  define UINT_MAX (INT_MAX * 2)
# endif
# ifndef LONG_MAX
#  define LONG_MAX MAXLONG
# endif
# ifndef ULONG_MAX
#  define ULONG_MAX (LONG_MAX * 2)
# endif
#endif



/*
 * Check if we have proper networking support
 */
#if !defined(HAVE_NETDB_H) || !defined(HAVE_NETINET_IN_H) || !defined(HAVE_ARPA_INET_H)
#undef HAVE_INET
#else
#define HAVE_INET
#endif



/*
 * Different variants of UNIX use different ways accessing directories. Make
 * everything so that it looks like in dirent.h.
 */
#ifdef NEED_DIR
# if defined(HAVE_DIRENT_H) || defined(_POSIX_VERSION)
#  include <dirent.h>
#  undef NLENGTH
#  define NLENGTH(dirent) (strlen((dirent)->d_name))
# else
#  undef dirent
#  define dirent direct
#  undef NLENGTH
#  define NLENGTH(dirent) ((dirent)->d_namlen)
#  ifdef HAVE_SYSNDIR_H
#   include <sys/ndir.h>
#  endif
#  ifdef HAVE_SYSDIR_H
#   include <sys/dir.h>
#  endif
#  ifdef HAVE_NDIR_H
#   include <ndir.h>
#  endif
# endif
#endif



/*
 * Check for include files which define struct tm. Support for time_t is
 * included with sys/stat.h above, so for time_t alone we don't need this.
 */
#ifdef NEED_TIME
# ifdef TIME_WITH_SYS_TIME
#  ifdef TM_IN_SYS_TIME
#   include <sys/time.h>
#  endif
#  include <time.h>
# else
#  if defined(HAVE_SYS_TIME_H) && defined(TM_IN_SYS_TIME)
#   include <sys/time.h>
#  else
#   include <time.h>
#  endif
# endif
#endif



/*
 * Do the usual checks for memory and string handling functions.
 */
#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
# include <string.h>
# if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#  include <memory.h>
# endif
#else
# include <strings.h>
# if !defined(HAVE_MEMCPY)
#  define memcpy(D, S, N) bcopy((S), (D), (N))
# endif
# if !defined(HAVE_STRCHR)
#  define strchr(S, C) index ((S), (C))
# endif
# if !defined(HAVE_STRRCHR)
#  define strrchr(S, C) rindex ((S), (C))
# endif
#endif



/*
 * Some glibc versions define strcmp as a macro even if they have it as
 * a routine. Unfortunately, the macro definition breaks YACC files.
 */
#if defined(__GLIBC__)
# ifdef strcmp
#  undef strcmp
# endif
#endif



/*
 * Next check for type definitions for 8, 16 and 32 bit variables.
 * The assembler modules expect most values in a specific size, and
 * we must ensure that the C programs produce the correct size.
 */
#ifdef USE_U_INT
# define __u8 u_int8_t
# define __u16 u_int16_t
# define __u32 u_int32_t
#else
# ifndef USE__UXX
#  ifndef U8_TYPE
#   if SIZEOF_UNSIGNED_CHAR == 1
#    define U8_TYPE unsigned char
#   else
#    error Unable to set 8-bit-type
#   endif
#  endif
#  ifndef U16_TYPE
#   if SIZEOF_UNSIGNED_INT == 2
#    define U16_TYPE unsigned int
#   else
#    if SIZEOF_UNSIGNED_SHORT == 2
#     define U16_TYPE unsigned short
#    else
#     error Unable to set 16-bit-type
#    endif
#   endif
#  endif
#  ifndef U32_TYPE
#   if SIZEOF_UNSIGNED_LONG == 4
#    define U32_TYPE unsigned long
#   else
#    if SIZEOF_UNSIGNED_INT == 4
#     define U32_TYPE unsigned int
#    else
#     error Unable to set 32-bit-type
#    endif
#   endif
#  endif
typedef U8_TYPE  __u8;
typedef U16_TYPE __u16;
typedef U32_TYPE __u32;
# endif
#endif



/*
 * Define some macros to allow a non-intel host system to produce
 * correct byte ordering code for the intel target system.
 */
#ifdef NEED_BINARY  /* No need to include this for utility programs */
# define swapbytes(a)	((((__u16)(a) & 0x00ff) << 8) | (((__u16)(a) & 0xff00) >> 8))
# define low_word(a)	((a) & 0x0000ffff)
# define high_word(a)	(((a) >> 16) & 0x0000ffff)
# define get_long(a)	((unsigned long)((ttoh(getval((a).high)) << 16) | \
						ttoh(getval((a).low))))
# ifndef WORDS_BIGENDIAN
#  define htot(a)		((__u16)(a))
#  define ttoh(a)		((unsigned short)(a))
# else
#  define htot(a)		((__u16)swapbytes(a))
#  define ttoh(a)		((unsigned short)swapbytes(a))
# endif
#endif /* NEED_BINARY */



/*
 * On some systems like SPARCs it is not possible to have integer variables
 * on misaligned addresses, so we have to use memcpy in that case.
 */
#ifdef NEED_BINARY  /* No need to include this for utility programs */
# ifdef USE_COPY

#  define assign(a, b)	mycopy(&(a), (b))
	/*
	 * Copy a value out of a host system variable into a target system
	 * structure
	 */
	static inline void mycopy(a, b)
	  __u16 *a;
	  __u16 b;
	{
	  memcpy(a, &b, sizeof(__u16));
	}

#  define getval(a)	mygetval(&(a))
	/*
	 * Return a value out of a target system structure
	 */
	static inline __u16 mygetval(a)
	  __u16 *a;
	{
	  __u16 b;
	  memcpy(&b, a, sizeof(__u16));
	  return(b);
	}

# else /* USE_COPY */

#  define assign(a, b)	a = b
#  define getval(a)	(a)

# endif /* USE_COPY */
#endif /* NEED_BINARY */



/*
 * Define the layout of Intel addresses and long words in memory.
 */
#ifdef USE_PRAGMA
#pragma pack(1)
#endif
#ifdef USE_PRAGMA_ALIGN
#pragma options align=packed
#endif
struct i_addr {
  __u16 offset	PACKED;
  __u16 segment	PACKED;
};


struct i_long {
  __u16 low	PACKED;
  __u16 high	PACKED;
};
#ifdef USE_PRAGMA
#pragma pack()
#endif
#ifdef USE_PRAGMA_ALIGN
#pragma options align=reset
#endif



/*
 * General definitions
 */
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif



/*
 * Include exit codes
 */
#include <exitcodes.h>



/*
 * Include version information
 */
#include <version.h>


#endif



syntax highlighted by Code2HTML, v. 0.9.1