/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * The contents of this file are subject to the Netscape Public License
 * Version 1.0 (the "NPL"); you may not use this file except in
 * compliance with the NPL.  You may obtain a copy of the NPL at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the NPL is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
 * for the specific language governing rights and limitations under the
 * NPL.
 *
 * The Initial Developer of this code under the NPL is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
 * Reserved.
 */

#ifndef prriscos_h___
#define prriscos_h___

/*
 * Internal configuration macros
 */

#define PR_LINKER_ARCH	"riscos"
#define _PR_SI_SYSNAME	"RISCOS"
#define _PR_SI_ARCHITECTURE "arm"
/* undefined so we can find where it causes the errors :-) */
/* #define PR_DLL_SUFFIX		".so.1.0" */

/*
** For sunos type machines, don't specify an address because the
** NetBSD/SPARC O.S. does the wrong thing.
*/
/* #define _PR_VMBASE		0x30000000 */
/* #define _PR_STACK_VMBASE	0x50000000 */
#define _MD_DEFAULT_STACK_SIZE	4096L /* RISC OS is 4k */
/* #define _MD_MMAP_FLAGS          MAP_PRIVATE */


/* #include <unistd.h> */
#include <stddef.h>
/* #include <string.h> /-* for memset! -*/
#include <time.h>
/* #include <socklib.h> */
/* #include <sys/select.h> */
/* #include <sys/dir.h> */
/* #include <sys/stat.h> */
#include <errno.h>

#include "prio.h"
#include "prmem.h"
#include "prclist.h"

#define PR_DIRECTORY_SEPARATOR		'.'
#define PR_DIRECTORY_SEPARATOR_STR	"."
#define PR_PATH_SEPARATOR		','
#define PR_PATH_SEPARATOR_STR		","
#define GCPTR
typedef int (*FARPROC)();

/*
 * intervals at which GLOBAL threads wakeup to check for pending interrupt
 */
#define _PR_INTERRUPT_CHECK_INTERVAL_SECS 5
extern PRIntervalTime intr_timeout_ticks;

typedef struct _PRRISCOSPollDesc {
	PRInt32 osfd;
	PRInt16 in_flags;
	PRInt16 out_flags;
} _PRRISCOSPollDesc;

typedef struct PRPollQueue {
    PRCList links;        /* for linking PRPollQueue's together */
    _PRRISCOSPollDesc *pds;        /* array of poll descriptors */
    PRUintn npds;            /* length of the array */
    PRPackedBool on_ioq;    /* is this on the async i/o work q? */
    PRIntervalTime timeout;        /* timeout, in ticks */
    struct PRThread *thr;
} PRPollQueue;

#define _PR_POLLQUEUE_PTR(_qp) \
    ((PRPollQueue*) ((char*) (_qp) - offsetof(PRPollQueue,links)))


extern PRInt32 _PR_WaitForFD(PRInt32 osfd, PRUintn how,
					PRIntervalTime timeout);
extern void _PR_Unblock_IO_Wait(struct PRThread *thr);

#if defined(_PR_LOCAL_THREADS_ONLY) || defined(_PR_GLOBAL_THREADS_ONLY)
#define _MD_CHECK_FOR_EXIT()
#endif

/* extern fd_set _pr_md_read_set, _pr_md_write_set, _pr_md_exception_set; */
extern PRInt16 _pr_md_read_cnt[], _pr_md_write_cnt[], _pr_md_exception_cnt[];
extern PRInt32 _pr_md_ioq_max_osfd;
extern PRUint32 _pr_md_ioq_timeout;

struct _MDFileDesc {
    int osfd;
};

/* struct _MDDir { */
	/* DIR *d; */
/* }; */

/*
 * md-specific cpu structure field, common to all RISCOS platforms
 */
#define _PR_MD_MAX_OSFD FD_SETSIZE

/* struct _MDCPU_RISCOS { */
    /* PRCList ioQ; */
    /* fd_set fd_read_set, fd_write_set, fd_exception_set; */
    /* PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD], */
				/* fd_exception_cnt[_PR_MD_MAX_OSFD]; */
    /* PRUint32 ioq_timeout; */
    /* PRInt32 ioq_max_osfd; */
/* }; */
struct _PRCPU;
/* extern void _MD_riscos_init_running_cpu(struct _PRCPU *cpu); */

/* #define _PR_IOQ(_cpu)			((_cpu)->md.md_RISCOS.ioQ) */
/* #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu)) */
/* #define _PR_FD_READ_SET(_cpu)		((_cpu)->md.md_RISCOS.fd_read_set) */
/* #define _PR_FD_READ_CNT(_cpu)		((_cpu)->md.md_RISCOS.fd_read_cnt) */
/* #define _PR_FD_WRITE_SET(_cpu)		((_cpu)->md.md_RISCOS.fd_write_set) */
/* #define _PR_FD_WRITE_CNT(_cpu)		((_cpu)->md.md_RISCOS.fd_write_cnt) */
/* #define _PR_FD_EXCEPTION_SET(_cpu)	((_cpu)->md.md_RISCOS.fd_exception_set) */
/* #define _PR_FD_EXCEPTION_CNT(_cpu)	((_cpu)->md.md_RISCOS.fd_exception_cnt) */
/* #define _PR_IOQ_TIMEOUT(_cpu)		((_cpu)->md.md_RISCOS.ioq_timeout) */
/* #define _PR_IOQ_MAX_OSFD(_cpu)		((_cpu)->md.md_RISCOS.ioq_max_osfd) */


/* --- Stack stuff --- */
#define _MD_INIT_STACK(stack, redzone)
#define _MD_CLEAR_STACK(stack)

#define PR_SET_INTSOFF(newval)

/************************************************************************/

extern void _PR_RISCOSInit(void);

/************************************************************************/

struct _MDProcess {
    /* pid_t pid; */
  int  dummy;
};

struct PRProcess;
struct PRProcessAttr;

/* Create a new process (fork() + exec()) */
#define _MD_CREATE_PROCESS _MD_CreateRISCOSProcess
extern struct PRProcess * _MD_CreateRISCOSProcess(
    const char *path,
    char *const *argv,
    char *const *envp,
    const struct PRProcessAttr *attr
);

#define _MD_DETACH_PROCESS _MD_DetachRISCOSProcess
extern PRStatus _MD_DetachRISCOSProcess(struct PRProcess *process);

/* Wait for a child process to terminate */
#define _MD_WAIT_PROCESS _MD_WaitRISCOSProcess
extern PRStatus _MD_WaitRISCOSProcess(struct PRProcess *process,
    PRInt32 *exitCode);

#define _MD_KILL_PROCESS _MD_KillRISCOSProcess
extern PRStatus _MD_KillRISCOSProcess(struct PRProcess *process);

/************************************************************************/

#define _MD_START_INTERRUPTS			_MD_StartInterrupts
#define _MD_STOP_INTERRUPTS				_MD_StopInterrupts
#define _MD_DISABLE_CLOCK_INTERRUPTS	_MD_DisableClockInterrupts
#define _MD_BLOCK_CLOCK_INTERRUPTS		_MD_BlockClockInterrupts
#define _MD_UNBLOCK_CLOCK_INTERRUPTS	_MD_UnblockClockInterrupts

/************************************************************************/

extern void		_MD_InitCPUS(void);
#define _MD_INIT_CPUS           _MD_InitCPUS

extern void		_MD_Wakeup_CPUs(void);
#define _MD_WAKEUP_CPUS _MD_Wakeup_CPUs

#define _MD_PAUSE_CPU			_MD_PauseCPU

#if defined(_PR_LOCAL_THREADS_ONLY) || defined(_PR_GLOBAL_THREADS_ONLY)
#define _MD_CLEANUP_BEFORE_EXIT()
#endif

#ifndef IRIX
#define _MD_EXIT(status)		_exit(status)
#endif

/************************************************************************/

#define _MD_GET_ENV				getenv
#define _MD_PUT_ENV				putenv

/************************************************************************/

extern void		_MD_MakeNonblock(PRFileDesc *fd);
#define _MD_MAKE_NONBLOCK			_MD_MakeNonblock

/************************************************************************/

extern PRStatus	_MD_AllocSegment(PRSegment *seg, PRUint32 size,
				void *vaddr);
extern void		_MD_FreeSegment(PRSegment *seg);

#define _MD_INIT_SEGS			_MD_InitSegs
#define _MD_ALLOC_SEGMENT		_MD_AllocSegment
#define _MD_FREE_SEGMENT		_MD_FreeSegment

/************************************************************************/

#define _MD_INTERVAL_INIT()
#define _MD_INTERVAL_PER_MILLISEC()	(_PR_MD_INTERVAL_PER_SEC() / 1000)
#define _MD_INTERVAL_PER_MICROSEC()	(_PR_MD_INTERVAL_PER_SEC() / 1000000)

/************************************************************************/

#define _MD_ERRNO()             	(errno)
#define _MD_GET_SOCKET_ERROR()		(errno)

/************************************************************************/

extern PRInt32 _MD_AvailableSocket(PRInt32 osfd);

/* #include <errno.h> */
/* #include <sys/types.h> */
/* #include <sys/time.h> */
/* #include <sys/stat.h> */

extern void _MD_InitSegs(void);
extern void _MD_StartInterrupts(void);
extern void _MD_StopInterrupts(void);
extern void _MD_DisableClockInterrupts(void);
extern void _MD_BlockClockInterrupts(void);
extern void _MD_UnblockClockInterrupts(void);
extern void _MD_PauseCPU(PRIntervalTime timeout);

extern PRStatus _MD_open_dir(struct _MDDir *, const char *);
extern PRInt32  _MD_close_dir(struct _MDDir *);
extern char *   _MD_read_dir(struct _MDDir *, PRIntn);
extern PRInt32  _MD_open(const char *name, PRIntn osflags, PRIntn mode);
extern PRInt32	_MD_delete(const char *name);
extern PRInt32	_MD_getfileinfo(const char *fn, PRFileInfo *info);
extern PRInt32	_MD_getfileinfo64(const char *fn, PRFileInfo64 *info);
extern PRInt32  _MD_getopenfileinfo(const PRFileDesc *fd, PRFileInfo *info);
extern PRInt32  _MD_getopenfileinfo64(const PRFileDesc *fd, PRFileInfo64 *info);
extern PRInt32	_MD_rename(const char *from, const char *to);
extern PRInt32	_MD_access(const char *name, PRIntn how);
extern PRInt32	_MD_mkdir(const char *name, PRIntn mode);
extern PRInt32	_MD_rmdir(const char *name);
extern PRInt32	_MD_accept_read(PRInt32 sock, PRInt32 *newSock,
				PRNetAddr **raddr, void *buf, PRInt32 amount);
extern PRInt32 	_PR_RISCOSTransmitFile(PRFileDesc *sd, PRFileDesc *fd,
			const void *headers, PRInt32 hlen,
			PRTransmitFileFlags flags, PRIntervalTime timeout);

extern PRStatus _MD_LockFile(PRInt32 osfd);
extern PRStatus _MD_TLockFile(PRInt32 osfd);
extern PRStatus _MD_UnlockFile(PRInt32 osfd);

#define _MD_OPEN_DIR(dir, name)		    _MD_open_dir(dir, name)
#define _MD_CLOSE_DIR(dir)		        _MD_close_dir(dir)
#define _MD_READ_DIR(dir, flags)	    _MD_read_dir(dir, flags)
#define _MD_OPEN(name, osflags, mode    )	_MD_open(name, osflags, mode)
extern PRInt32 _MD_read(PRFileDesc *fd, void *buf, PRInt32 amount);
#define _MD_READ(fd,buf,amount)		    _MD_read(fd,buf,amount)
extern PRInt32 _MD_write(PRFileDesc *fd, const void *buf, PRInt32 amount);
#define _MD_WRITE(fd,buf,amount)	    _MD_write(fd,buf,amount)
#define _MD_DELETE(name)		        _MD_delete(name)
#define _MD_GETFILEINFO(fn, info)	    _MD_getfileinfo(fn, info)
#define _MD_GETFILEINFO64(fn, info)	    _MD_getfileinfo64(fn, info)
#define _MD_GETOPENFILEINFO(fd, info)	_MD_getopenfileinfo(fd, info)
#define _MD_GETOPENFILEINFO64(fd, info)	_MD_getopenfileinfo64(fd, info)
#define _MD_RENAME(from, to)		    _MD_rename(from, to)
#define _MD_ACCESS(name, how)		    _MD_access(name, how)
#define _MD_MKDIR(name, mode)		    _MD_mkdir(name, mode)
#define _MD_RMDIR(name)			        _MD_rmdir(name)
#define _MD_ACCEPT_READ(sock, newSock, raddr, buf, amount)	_MD_accept_read(sock, newSock, raddr, buf, amount)

#define _MD_LOCKFILE _MD_LockFile
#define _MD_TLOCKFILE _MD_TLockFile
#define _MD_UNLOCKFILE _MD_UnlockFile


extern PRInt32		_MD_socket(int af, int type, int flags);
#define _MD_SOCKET	_MD_socket
extern PRInt32		_MD_connect(PRFileDesc *fd, const PRNetAddr *addr,
								PRUint32 addrlen, PRIntervalTime timeout);
#define _MD_CONNECT	_MD_connect
extern PRInt32		_MD_accept(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen,
													PRIntervalTime timeout);
#define _MD_ACCEPT	_MD_accept
extern PRInt32		_MD_bind(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen);
#define _MD_BIND	_MD_bind
extern PRInt32		_MD_listen(PRFileDesc *fd, PRIntn backlog);
#define _MD_LISTEN	_MD_listen
extern PRInt32		_MD_shutdown(PRFileDesc *fd, PRIntn how);
#define _MD_SHUTDOWN	_MD_shutdown

extern PRInt32		_MD_recv(PRFileDesc *fd, void *buf, PRInt32 amount,
                               PRIntn flags, PRIntervalTime timeout);
#define _MD_RECV	_MD_recv
extern PRInt32		_MD_send(PRFileDesc *fd, const void *buf, PRInt32 amount,
									PRIntn flags, PRIntervalTime timeout);
#define _MD_SEND	_MD_send
extern PRInt32		_MD_recvfrom(PRFileDesc *fd, void *buf, PRInt32 amount,
						PRIntn flags, PRNetAddr *addr, PRUint32 *addrlen,
											PRIntervalTime timeout);
#define _MD_RECVFROM	_MD_recvfrom
extern PRInt32 _MD_sendto(PRFileDesc *fd, const void *buf, PRInt32 amount,
							PRIntn flags, const PRNetAddr *addr, PRUint32 addrlen,
												PRIntervalTime timeout);
#define _MD_SENDTO	_MD_sendto
extern PRInt32		_MD_writev(PRFileDesc *fd, struct PRIOVec *iov,
								PRInt32 iov_size, PRIntervalTime timeout);
#define _MD_WRITEV	_MD_writev

extern PRInt32		_MD_socketavailable(PRFileDesc *fd);
#define	_MD_SOCKETAVAILABLE		_MD_socketavailable
extern PRInt64		_MD_socketavailable64(PRFileDesc *fd);
#define	_MD_SOCKETAVAILABLE64		_MD_socketavailable64

extern PRInt32 _MD_pr_poll(PRPollDesc *pds, PRIntn npds,
												PRIntervalTime timeout);
#define _MD_PR_POLL		_MD_pr_poll

extern PRInt32		_MD_close(PRInt32 osfd);
#define _MD_CLOSE_FILE	_MD_close
extern PRInt32		_MD_lseek(PRFileDesc*, PRInt32, PRSeekWhence);
#define _MD_LSEEK	_MD_lseek
extern PRInt64		_MD_lseek64(PRFileDesc*, PRInt64, PRSeekWhence);
#define _MD_LSEEK64	_MD_lseek64
extern PRInt32		_MD_fsync(PRFileDesc *fd);
#define _MD_FSYNC	_MD_fsync

extern PRInt32 _MD_socketpair(int af, int type, int flags, PRInt32 *osfd);
#define _MD_SOCKETPAIR		_MD_socketpair

#define _MD_CLOSE_SOCKET	_MD_close

#ifndef NO_NSPR_10_SUPPORT
#define _MD_STAT	stat
#endif

extern PRStatus _MD_getpeername(PRFileDesc *fd, PRNetAddr *addr,
											PRUint32 *addrlen);
#define _MD_GETPEERNAME _MD_getpeername
extern PRStatus _MD_getsockname(PRFileDesc *fd, PRNetAddr *addr,
											PRUint32 *addrlen);
#define _MD_GETSOCKNAME _MD_getsockname

extern PRStatus _MD_getsockopt(PRFileDesc *fd, PRInt32 level,
						PRInt32 optname, char* optval, PRInt32* optlen);
#define _MD_GETSOCKOPT		_MD_getsockopt
extern PRStatus _MD_setsockopt(PRFileDesc *fd, PRInt32 level,
					PRInt32 optname, const char* optval, PRInt32 optlen);
#define _MD_SETSOCKOPT		_MD_setsockopt

extern PRStatus _MD_gethostname(char *name, PRUint32 namelen);
#define _MD_GETHOSTNAME		_MD_gethostname

extern int _MD_riscos_get_nonblocking_connect_error(int osfd);

#define _MD_SELECT                    select

#define HAVE_SOCKET_REUSEADDR
#define HAVE_SOCKET_KEEPALIVE
#define HAVE_BSD_FLOCK

/* Memory-mapped files */

struct _MDFileMap {
    PRIntn prot;
    PRIntn flags;
};

extern PRStatus _MD_CreateFileMap(struct PRFileMap *fmap, PRInt64 size);
#define _MD_CREATE_FILE_MAP _MD_CreateFileMap

extern void * _MD_MemMap(struct PRFileMap *fmap, PRInt64 offset,
        PRUint32 len);
#define _MD_MEM_MAP _MD_MemMap

extern PRStatus _MD_MemUnmap(void *addr, PRUint32 size);
#define _MD_MEM_UNMAP _MD_MemUnmap

extern PRStatus _MD_CloseFileMap(struct PRFileMap *fmap);
#define _MD_CLOSE_FILE_MAP _MD_CloseFileMap

/* Machine-dependent (MD) data structures.  RISC OS has no native threads. */

#define USE_SETJMP

#include <setjmp.h>

struct _MDThread {
    jmp_buf context;
    int id;
    int errcode;
};

struct _MDThreadStack {
    PRInt8 notused;
};

struct _MDLock {
    PRInt8 notused;
};

struct _MDSemaphore {
    PRInt8 notused;
};

struct _MDCVar {
    PRInt8 notused;
};

struct _MDSegment {
    PRInt8 notused;
};

/* struct _MDCPU { */
	/* struct _MDCPU_RISCOS md_RISCOS; */
/* }; */

#define _MD_INIT_LOCKS()
#define _MD_NEW_LOCK(lock) PR_SUCCESS
#define _MD_FREE_LOCK(lock)
#define _MD_LOCK(lock)
#define _MD_UNLOCK(lock)
#define _MD_INIT_IO()
#define _MD_IOQ_LOCK()
#define _MD_IOQ_UNLOCK()

/* These are copied from _sunos4.h (from solaris) */

#define _MD_EARLY_INIT          _MD_EarlyInit
#define _MD_FINAL_INIT		_PR_RISCOSInit
#define _MD_INIT_RUNNING_CPU(cpu) _MD_riscos_init_running_cpu(cpu)
#define _MD_INIT_THREAD         InitThreads()
#define _MD_EXIT_THREAD(thread)
#define	_MD_SUSPEND_THREAD(thread)
#define	_MD_RESUME_THREAD(thread)
#define _MD_CLEAN_THREAD(_thread)


/* from sunos4 */
#define _MD_GET_INTERVAL                  _PR_RISCOS_GetInterval
#define _MD_INTERVAL_PER_SEC              _PR_RISCOS_TicksPerSecond

PRStatus _MD_WAIT(struct PRThread *, PRIntervalTime timeout);
PRStatus _MD_WAKEUP_WAITER(struct PRThread *);
PRStatus _MD_InitializeThread(struct PRThread *thread);
/* void     _MD_SET_PRIORITY(struct _MDThread *thread, PRUintn newPri); */
/* PRStatus _MD_CREATE_THREAD(struct PRThread *thread, void (*start) (void *), */
	/* PRThreadPriority priority, PRThreadScope scope, PRThreadState state, */
        /* PRUint32 stackSize); */

/* PR_EXTERN(PRIntervalTime)	_MD_RISCOS_GetInterval(void); */
/* PR_EXTERN(PRIntervalTime)	_MD_RISCOS_TicksPerSecond(void); */
/* #define _MD_INTERVAL_PER_SEC	_MD_RISCOS_TicksPerSecond */

/* This is really all complete cack for RISC OS */

/* #define _MD_GET_SP(_t)    (_t)->md.context[2] */

#define CONTEXT(_th) ((_th)->md.context)

/*
** Initialize the thread context preparing it to execute _main.
*/
#define _MD_INIT_CONTEXT(_thread, _sp, _main, status)	      \
    PR_BEGIN_MACRO				      \
      *status = PR_TRUE; \
      if (setjmp(CONTEXT(_thread))) {				  \
  	_main();					  \
      }								  \
     /* _MD_GET_SP(_thread) = (int) (_sp - 64); */ \
    PR_END_MACRO

#define _MD_SWITCH_CONTEXT(_thread)  \
    if (!setjmp(CONTEXT(_thread))) { \
	(_thread)->md.errcode = errno;  \
	_PR_Schedule();		     \
    }

/*
** Restore a thread context, saved by _MD_SWITCH_CONTEXT
*/
#define _MD_RESTORE_CONTEXT(_thread) \
{				     \
    errno = (_thread)->md.errcode;	     \
    _MD_SET_CURRENT_THREAD(_thread);	 \
    longjmp(CONTEXT(_thread), 1);    \
}

/* we return you to your normal programming */

#define _MD_CURRENT_THREAD() (0) /* BUG! */
#define _MD_CURRENT_CPU() (0) /* BUG! */

/* strdup is missing */
extern char *strdup(const char *);

#endif /* prriscos_h___ */

