/*
 * common.h
 * Common includes/structures/enums etc.
 *
 * Copyright (C) Leni Mayo 2001-2002
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License, version 2, as
 *	published by the Free Software Foundation.
 *	
 *	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.
 */

#ifndef _COMMON_H_
#define _COMMON_H_

/*
 * enum values are explicit in order to guarantee that the symbolic
 * names map to the same values in both the client and the server
 */
#include <stdio.h>
#include <sys/types.h> /* for pid_t */
#include <assert.h>    /* used everywhere */
#include <glib.h>

#ifdef SEREL_DEBUG_MEMORY_DMALLOC
/* compile with -DDMALLOC_FUNC_CHECK to turn on frequent checking */
#include "dmalloc.h"
#endif

#ifndef TRUE
#define TRUE  1
#endif
 
#ifndef FALSE
#define FALSE 0
#endif

#define NOTHERE 0  /* commonly used as assert(NOTHERE) */

#define SERVICE_NAME_SIZE 64   /* maximum length of a service name */
#define PATH_NAME_SIZE   256   /* maximum length of a path    name */

typedef pid_t Pid;
typedef char ServiceName[SERVICE_NAME_SIZE];
typedef char PathName[PATH_NAME_SIZE];

/*
 * Strings for things that appear on the command line
 * so as to avoid them being spread out all over the code
 */
typedef enum
{
	eArgvCodeSereld,
	eArgvSerelhistory,
	eArgvCodeLast
} ArgvCode;

const char *argvCodeAsString(ArgvCode code);

/*
 * There's going to need to be a "manager" command, to set status flags
 * in the server (eg logging styles), and also
 * a "printing" command, to print trees of whats going on
 */

typedef enum
{	
	eCommandTypeRegisterNeed    = 0,
	eCommandTypeRegisterProvide = 1,
	eCommandTypeDeregister      = 2,
	eCommandTypeStatusChange    = 3,
	eCommandTypeServerControl   = 4,
	eCommandTypeQuery           = 5,
	eCommandTypeViaFile         = 6,
	eCommandTypeMax             = 7  /* not a real command, just an "end of" marker */
} CommandType;

typedef enum
{
	ePidStatusSuccess = 0,
	ePidStatusFailure = 1,
} PidStatus;

typedef enum
{
	eServerControlCodeStop = 0  /* ask the server to stop               */
	                            /* probably more later, like restart... */
} ServerControlCode;

typedef enum
{
	eQueryCodeStatus = 0,       /* get the fsm status of a service name        */
	eQueryCodeGraph  = 1        /* get the nodes and edges as an xml/rdf Graph */
} QueryCode;

typedef enum
{
	eViaFileCodeRosterApply    = 0,    /* apply the provide/need/unprovide assoc with the path */
	eViaFileCodeRosterApplyStarted = 1, /* register any already-started service names */
	eViaFileCodeRosterPopulate = 2,    /* populate the roster                      */
	eViaFileCodeRosterQuery    = 3,    /* query the roster                         */
	eViaFileCodeMax            = 4     /* not a real code, just an "end of" marker */
} ViaFileCode;

typedef enum
{
	eRosterQueryCodeVerify       = 0,
	eRosterQueryCodePriorHistory = 1,
	eRosterQueryCodeApply        = 2,
	eRosterQueryCodeMax          = 3
} RosterQueryCode;

typedef enum
{
	eReturnSuccess,  /* send a signal indicating success to the caller   */
	eReturnFailure,  /* send a signal indicating failure to the caller   */
	eReturnNone,     /* don't send anything to the caller just yet       */
	eReturnUndefined /* internal error - some condition has slipped thru */
} CallbackReturnCode;

/* This structure used by the various AsString() functions.
*/
typedef struct
{
	char *buf;
	int  bufsiz;
} SafeBuf;

int sprintfTo(SafeBuf *p, char *fmt, ...);

void parseEnvironment(int *pDebugLevel, const char **ppszLogName);

const char * getClientServerPipePath(char *buf, int bufsiz);

const char * getArgv0(char *argv[], const char *pszDefault);

void printVersion(FILE *fp, const char *pszPackage, const char *pszVersion);

/* used in g_hash_table_foreach() to free the values in a hash
*/
void callbackFreeValue(void * key, void * value, void * pUserData);

void copyFpToFp(FILE *fpDst, FILE *fpSrc);

int createAndOpenTmpFile(char filename[], int len);
FILE * createAndOpenTmpFp(char filename[], int len, const char *mode);

typedef int (*ISDELIM)(char ch);
int isWhitespace(char ch);
GSList * splitStringIntoList(char *psz, ISDELIM fn);
char * stringFromFp(FILE *fp);

void printCommonCmdlineOptions(FILE *fp);

#endif /* _COMMON_H_ */
