/*
** CVS: $Id: function.h,v 1.6 2001/02/08 10:53:33 zhangsz Exp $
**
** function.h    
** Created at 2000/07/23            
** public domain code by Zhang Shuzhong       
**
** Description: Header file of function definations.    
*/

#ifndef FUNCTION__H
#define FUNCTION__H

#define DisplayContent(buf, bcd_buf, nbytes) \
    BcdToAscii(buf, bcd_buf, 2 * nbytes); \
	FormatOutput(buf, 2*nbytes, 2, ' '); \
    log_debug("packets contains (%s):\n%s", FUNCTION_MACRO, buf);
	
#define BzeroContent(content, length) \
	bzero(content+length, MAX_CONTENT_LEN-length);

#define WriteOutBuffer WriteToOutBuffer

#define WriteToOutBuffer(PROG_NAME, ip_semid, poutbuf, pbuf) \
	SemLock(PROG_NAME, ip_semid, PORT_NUM, 1, IPC_NOWAIT); \
	WriteMsg((in_buffer *) poutbuf, pbuf, OUT_LIST_NUM); \
	SemUnlock(PROG_NAME, ip_semid, PORT_NUM, 1, IPC_NOWAIT); \
	SemUnlock(PROG_NAME, ip_semid, SEM_NUM-1, 1, IPC_NOWAIT); 

#define ReadInBuffer ReadFromInBuffer

#define ReadFromInBuffer(PROG_NAME, ip_semid, port_id, pinbuf, pbuf) \
	SemLock(PROG_NAME, ip_semid, port_id, 1, IPC_NOWAIT); \
	ReadMsg((in_buffer *) pinbuf, pbuf, IN_LIST_NUM); \
	SemUnlock(PROG_NAME, ip_semid, port_id, 1, IPC_NOWAIT); \

int InitDaemon(void);         /* Initialize a daemon process */

void 
SetTermSignal(struct termio *ptermio, void (*handler)(int));

/* Initialize a daemon process */
void DaemonInit(const char *pname, int); 

/* passive a socket handler, from comfunc.c */
int passivesock( const char * transport, u_short portbase, int qlen);

/* passive a UDP socket handler, from comfunc.c */
int passiveUDP(u_short portbase);

/* Fill string 'c' into last area of 'str'.  */
void StringCat(char *str, char *c, int slen);

int ByteLength(BYTE *bcd, int c);

/* Cut string while match character c.  */
void StringCut(char *str, int c);

void Bstr2Hstr(char *cptr);

void bitstring(char *str, long byze, int biz, int strwid);

/* Binary string convert to int.  */
unsigned int Bstr2Int(char *cptr);

/*  Hex string convert to int.  */
unsigned int Hstr2Int(char *cptr);

/* Get current hour */
int gethour(char * hour_buf);
int getweek(char * week_buf);

/* Get current hour */
int getmin(char * min_buf);

/* Convert data from ASCII form to left-aligned compact BCD form. */
void  AsciiToBcd (BYTE *bcd_buf, const char *ascii_buf, int len);

/* Convert data from left-aligned compact BCD form to ASCII form. */
void  BcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len);

/* Convert data from ASCII form to right-aligned compact BCD form. */
void  AsciiToRbcd (BYTE *bcd_buf, const char *ascii_buf, int len);

const char *
getdaytime(char *daytime_buf);

char *GetASCTime(char *timep);

void *
ShmInit(int shm_id, char *prog_name, key_t key, int size, int shmflg);

int
SemInit(char *prog_name, key_t key, int sem_num, int sem_flg);

BOOL
SemLock(char *prog_name, int sem_id, int sem_num, 
        unsigned nsops, int sem_flg);

BOOL 
SemUnlock(char *prog_name, int sem_id, int sem_num, 
        unsigned nsops, int sem_flg);

BOOL
ReadMsg(void *pbuf, void *pmsg, int max_num);

BOOL 
WriteMsg(void *pbuf, void *pmsg, int max_num);

const char *
Inet_ntoa(const long addr);

const long
Inet_pton(const char *sp);

char *
FormatOutput(char *sp, size_t, int, int);
	
void
ByteReverse(BYTE *p1, BYTE *p2, size_t size);
void 
SafeFree(void *pmem);

void
GetAsciiField(char *dest, BYTE *src, size_t n);

#endif
