/*-
 * Copyright (c) 2004 Seth Kingsley.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgment:
 *    This product includes software developed by Seth Kingsley and
 *    contributors.
 * 4. Neither the author's name, nor any of the contributors names may be
 *    used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* $Id: mk.h,v 1.9 2004/06/24 04:40:39 sethk Exp $ */

#ifndef MK_H
#define MK_H

#include    <sys/cdefs.h>
#include    <sys/types.h>
#include    <sys/queue.h>

#include    <stdbool.h>

#ifdef __linux__
#include    <features.h>

#define __RCSID(id) static const char rcsid[] = id

size_t strlcpy(char *_dst, const char *_src, size_t _siz);
#endif /* __linux__ */

#ifdef __GNU_LIBRARY__
static __inline const char *
getprogname(void)
{
    extern const char	*__progname;

    return __progname;
}
#endif /* __GNU_LIBRARY__ */

#ifdef MK_CONFIG
#include    <sys/queue.h>

typedef bool (*mk_config_typefunc_t)(void *_dest, const char *_src);
typedef void (*mk_config_freefunc_t)(void *_val);
typedef void (*mk_config_errfunc_t)(const char *_act,
	const char *_desc,
	void *_udata);

struct mk_config_type
{
    char		    *mct_name;
    mk_config_typefunc_t    mct_typefunc;
    mk_config_freefunc_t    mct_freefunc;
    STAILQ_ENTRY(mk_config_type) mct_link;
};

struct mk_config_var
{
    const char	*mcv_name,
		*mcv_typename;
    void	*mcv_valuep;
    const void	*mcv_default;
};

#define MK_CONF_INT "integer"
#define MK_CONF_STR "string"
#define MK_CONF_IP  "ip address"
#endif /* MK_CONFIG */

void *mk_malloc_or_die(size_t _n);
void *mk_realloc_or_die(void *_p, size_t _n);

#ifdef DMALLOC

#include    "dmalloc.h"

#define mk_malloc   malloc
#define mk_realloc  realloc
#define mk_free	    free
#define mk_strdup   strdup

#else /* !DMALLOC */

extern void *(*mk_malloc)(size_t _n);
extern void *(*mk_realloc)(void *_p, size_t _n);
extern void (*mk_free)(void *_p);

char *mk_strdup(const char *_s);

#endif /* DMALLOC */

#ifdef MK_CONFIG
void mk_config_type_add(const char *_name,
	mk_config_typefunc_t _tfunc,
	mk_config_freefunc_t _ffunc);
bool mk_config_read(int _fd,
	struct mk_config_var *_mcvl,
	u_int _nmcv,
	mk_config_errfunc_t _efunc,
	void *_udata);
void mk_config_free(struct mk_config_var *_mcvl, u_int _nmcv);
#endif /* MK_CONFIG */
pid_t mk_spawn(const char *_dir,
	int *pfds,
	char * const _av[],
	char **_descp);
struct mk_lines *mk_lines_init(void);
void mk_lines_fini(struct mk_lines *_mlp);
ssize_t mk_lines_read(int _fd, struct mk_lines *_mlp);
char *mk_lines_next(struct mk_lines *_mlp);

#endif /* !MK_H */

