/* Ported to GNU C openVMS/Alpha by Klaus Kämpf, kkaempf@progis.de  */
/* <stdio.h>
 *
 *	This defines a DEC C compatible standard I/O interface.
 */
#ifndef _STDIO_H
# define _STDIO_H
# ifdef __cplusplus
extern "C" {
# endif

#if defined(__STDC__) && !defined(_STDDEF_H)
# include <stddef.h>	/* get size_t, NULL */
#endif

# define ___fdecl(_func,_arglist) _func _arglist __asm("decc$" #_func)
#if __IEEE_FLOAT
# define ___gdecl(_func, _arglist) _func _arglist __asm("decc$tx" #_func)
# define ___xdecl(_func, _arglist) _func _arglist __asm("gnuc$tx" #_func)
#elif __G_FLOAT
# define ___gdecl(_func, _arglist) _func _arglist __asm("decc$gx" #_func)
# define ___xdecl(_func, _arglist) _func _arglist __asm("gnuc$gx" #_func)
#else
# error "No floating format defined"
#endif

/*
 *	The maximum number of files we can have open at a time
 *	(not really; blocks of this many are chained together by vaxcrtl)
 */
#define _NFILE 20

/*
 *	STDIO buffer size
 */

#define BUFSIZ 32768

/*
 *	Arguments to setvbuf()
 */
#define _IOLBF 1	/* line buffered */
#define _IOFBF 2	/* fully buffered */
/* #def _IONBF 4 */	/* unbuffered (really defined below) */

/*
 *	This is what the DEC C stdio FILE structure looks like
 */
struct	_iobuf	{
	int	_cnt;			/* # of characters in the buffer */
	char	*_ptr;			/* Pointer into the buffer	 */
	char	*_base;			/* Pointer to start of buffer	 */
	unsigned char	_flag;			/* STDIO flags			 */
#define _IOREAD		0001			/* Open for reading	 */
#define _IOWRT		0002			/* Open for writing	 */
#define _IONBF		0004			/* No buffer		 */
#define _IOMYBUF	0010			/* Using "my" buffer	 */
#define _IOEOF		0020			/* At End Of File	 */
#define _IOERR		0040			/* I/O error has occured */
#define _IOSTRG		0100			/* Doing I/O to a string */
#define _IORW		0200			/* Open for read/write	 */
	unsigned char	_file;			/* File descriptor		 */
	unsigned char	_pad1;
	unsigned char	_pad2;
};

/*
 *	Instead of passing around pointers to _iobuf structures, VAXCRTL
 *	passes around pointers to pointers.
 */
typedef struct _iobuf *FILE;

/*
 *	Cookie used by fgetpos() and fsetpos() [alternatives to ftell/fseek]
 *	Internally, it's a 6-byte RFA plus a 2-byte offset within record.
 */
typedef struct _FPOS_T { unsigned : 32, : 32; } fpos_t __attribute__ ((packed));

/*
 *	Also, stdin/stdout/stderr need to be defined
 */

extern FILE *stdin __asm ("DECC$GA_STDIN");
extern FILE *stdout __asm ("DECC$GA_STDOUT");
extern FILE *stderr __asm ("DECC$GA_STDERR");

/*
 *	Define NULL and EOF
 */
#ifndef __STDC__	/* got it from <stddef.h> if __STDC__ */
# define NULL 0
#endif
#define EOF	(-1)

#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_EOF SEEK_END	/* VAX C compatability */

#define FOPEN_MAX 8	/* minimum number simultaneously open files */
#define OPEN_MAX  8	/* ditto (note: minimum _guaranteed_; more possible) */
#define TMP_MAX  32	/* minimum number of unique names tmpnam() gives */
#define FILENAME_MAX 256 /* longest filename */

/*
 *	Buffer sizes for various return values
 */
#define L_ctermid	64	/* size of device name */
#define L_cuserid	16	/* size of user name */
#define L_tmpnam	256	/* size of file name */
#define L_lcltmpnam	256	/* size of file local name */
#define L_nettmpnam	256	/* size of file network name */

/*
 *	Define the stdio macros; note that getc and putc are function calls.
 */

int	___fdecl (getchar, (void));
int	___fdecl (putchar, (int c));
int	___fdecl (getc, (FILE *fp));


#define getchar() \
	((*stdin) ? \
	(((((*stdin)->_cnt > 0) & !((*stdin)->_flag & 0x40)) ? \
	( (*stdin)->_cnt--, \
	((int) ((unsigned char) *((*stdin)->_ptr++)))) : \
	DECC$GETCHAR())) : \
	DECC$GETCHAR())   

#define putc(x, fp)	fputc((x), fp)
#define putchar(x)	fputc((x), stdout)

#define feof(fp)	(((*fp)->_flag & _IOEOF)!=0)
#define ferror(fp)	(((*fp)->_flag & _IOERR)!=0)
#define fileno(fp)	((*fp)->_file)
#define clearerr(fp)	((*fp)->_flag &= ~(_IOERR|_IOEOF))

/* DEC C compatibility, prototypes in unixio.h */
int ___fdecl (open, (const char *path, int mode, ...));
int ___fdecl (close, (int fd));
int ___fdecl (read, (int fd, void *buf, unsigned int nbyte));
int ___fdecl (write, (int fd, const void *buf, unsigned int nbyte));
#define record_read(fp, buf, max_size) read (fileno(fp), (buf), (max_size))
#define record_write(fp, buf, size) fwrite((buf), (size), 1, (fp))

/*
 *	Declare stdio routines
 */
/* access */
FILE *	___fdecl (fopen, (const char *name, const char *mode, ...));
FILE *	___fdecl (fdopen, (int file_desc, const char *mode));
FILE *	___fdecl (freopen, (const char *name, const char *mode, FILE *stream, ...));
int	___fdecl (fflush, (FILE *stream));
int	___fdecl (fclose, (FILE *stream));

/* unformatted input/output */
int ___fdecl (fgetc, (FILE *__stream));
#define fgetc(fp) \
	((*fp) ? \
	(((((*fp)->_cnt > 0) & !((*fp)->_flag & 0x40)) ? \
	( (*fp)->_cnt--, \
	((int) ((unsigned char) *((*fp)->_ptr++)))) : \
	DECC$FGETC(fp))) : \
	DECC$FGETC(fp))

int	___fdecl (ungetc, (int c, FILE *stream));
int	___fdecl (fputc, (int, FILE *stream));
int	___fdecl (getw, (FILE *streamfile_ptr));
int	___fdecl (putw, (int, FILE *stream));
char *	___fdecl (gets, (char *));
int	___fdecl (puts, (const char *));
char *	___fdecl (fgets, (char *, int, FILE *stream));
int	___fdecl (fputs, (const char *, FILE *stream));
size_t	___fdecl (fread, (void *, size_t, size_t, FILE *stream));
size_t	___fdecl (fwrite, (const void *, size_t, size_t, FILE *stream));

/* formatted input/output  */
#include <va-alpha.h>
#define ___va_list_t __gnuc_va_list
int  ___gdecl (scanf, (const char *, ...));
int  ___gdecl (printf, (const char *, ...));
int  ___gdecl (vprintf, (const char *, ___va_list_t));
int  ___gdecl (fscanf, (FILE *stream, const char *, ...));
int  ___gdecl (fprintf, (FILE *stream, const char *, ...));
int  ___gdecl (vfprintf, (FILE *stream, const char *, ___va_list_t));
int  ___gdecl (sscanf, (const char *, const char *, ...));
int  ___gdecl (sprintf, (char *, const char *, ...));
int  ___gdecl (vsprintf, (char *, const char *, ___va_list_t));
# undef ___va_list_t

/* positioning */
int	___fdecl (fseek, (FILE *stream, long, int));
long	___fdecl (ftell, (FILE *stream));
int	___fdecl (fsetpos, (FILE *stream, const fpos_t *));
int	___fdecl (fgetpos, (FILE *stream, fpos_t *));
int	___fdecl (rewind, (FILE *stream));

/* miscellaneous */

void	___fdecl (perror, (const char *));
int unlink (const char *file) __asm("decc$remove");
int	___fdecl (remove, (const char *));
void	___fdecl (setbuf, (FILE *stream, char *));
int	___fdecl (setvbuf, (FILE *stream, char *, int, size_t));
char *	___fdecl (fgetname, (FILE *stream, char *, ...)); /* optional arg is VMS vs shell name format */
char *	___fdecl (tmpnam, (char *));
FILE *	___fdecl (tmpfile, (void));

#undef ___gdecl
#undef ___fdecl

# ifdef __cplusplus
}
# endif

#endif	/*_STDIO_H*/
