/*
 * $Id: awk.c,v 1.26 2006-04-22 13:54:53 bacon Exp $
 */

#include <xp/awk/awk.h>
#include <wchar.h>
#include <stdio.h>
#include <string.h>

#ifdef __STAND_ALONE
#define xp_printf xp_awk_printf
extern int xp_awk_printf (const xp_char_t* fmt, ...); 
#else
#ifdef XP_CHAR_IS_MCHAR
#define xp_printf printf
#else
#define xp_printf wprintf
#endif
#endif

#ifdef __STAND_ALONE
#define xp_strcmp xp_awk_strcmp
extern int xp_awk_strcmp (const xp_char_t* s1, const xp_char_t* s2);
#else
#ifdef XP_CHAR_IS_MCHAR
#define xp_strcmp strcmp
#else
#define xp_strcmp wcscmp
#endif
#endif

static xp_ssize_t process_source (
	int cmd, void* arg, xp_char_t* data, xp_size_t size)
{
	xp_char_t c;

	switch (cmd) {
	case XP_AWK_INPUT_OPEN:
	case XP_AWK_INPUT_CLOSE:
	case XP_AWK_INPUT_NEXT:
		return 0;

	case XP_AWK_INPUT_DATA:
		if (size <= 0) return -1;
#ifdef XP_CHAR_IS_MCHAR
		c = fgetc (stdin);
#else
		c = fgetwc (stdin);
#endif
		if (c == XP_CHAR_EOF) return 0;
		*data = c;
		return 1;

	case XP_AWK_OUTPUT_OPEN:
	case XP_AWK_OUTPUT_CLOSE:
	case XP_AWK_OUTPUT_NEXT:
	case XP_AWK_OUTPUT_DATA:
		return 0;
	}

	return -1;
}

struct data_io
{
	const char* input_file;
	FILE* input_handle;
};

static xp_ssize_t process_data (
	int cmd, void* arg, xp_char_t* data, xp_size_t size)
{
	struct data_io* io = (struct data_io*)arg;
	xp_char_t c;

	switch (cmd) {
	case XP_AWK_INPUT_OPEN:
		io->input_handle = fopen (io->input_file, "r");
		if (io->input_handle == NULL) return -1;
		return 0;

	case XP_AWK_INPUT_CLOSE:
		fclose (io->input_handle);
		io->input_handle = NULL;
		return 0;

	case XP_AWK_INPUT_NEXT:
		/* input switching not supported for the time being... */
		return -1;

	case XP_AWK_INPUT_DATA:
		if (size <= 0) return -1;
#ifdef XP_CHAR_IS_MCHAR
		c = fgetc (io->input_handle);
#else
		c = fgetwc (io->input_handle);
#endif
		if (c == XP_CHAR_EOF) return 0;
		*data = c;
		return 1;

	case XP_AWK_OUTPUT_OPEN:
	case XP_AWK_OUTPUT_CLOSE:
	case XP_AWK_OUTPUT_NEXT:
	case XP_AWK_OUTPUT_DATA:
		return -1;
	}

	return -1;

}

#ifdef __linux
#include <mcheck.h>
#endif

#if defined(__STAND_ALONE) && !defined(_WIN32)
int main (int argc, char* argv[])
#else
int xp_main (int argc, xp_char_t* argv[])
#endif
{
	xp_awk_t* awk;
	struct data_io data_io = { "awk.in", NULL };

#ifdef __linux
	mtrace ();
#endif

	if ((awk = xp_awk_open()) == XP_NULL) 
	{
		xp_printf (XP_TEXT("Error: cannot open awk\n"));
		return -1;
	}

	if (xp_awk_attsrc(awk, process_source, XP_NULL) == -1) 
	{
		xp_awk_close (awk);
		xp_printf (XP_TEXT("Error: cannot attach source\n"));
		return -1;
	}

	xp_awk_setparseopt (awk, 
		XP_AWK_EXPLICIT | XP_AWK_UNIQUE | 
		XP_AWK_SHADING | XP_AWK_IMPLICIT | XP_AWK_SHIFT);

	if (argc == 2) 
	{
#if defined(__STAND_ALONE) && !defined(_WIN32)
		if (strcmp(argv[1], "-m") == 0)
#else
		if (xp_strcmp(argv[1], XP_TEXT("-m")) == 0)
#endif
		{
			xp_awk_setrunopt (awk, XP_AWK_RUNMAIN);
		}
	}

	if (xp_awk_parse(awk) == -1) 
	{
#if defined(__STAND_ALONE) && !defined(_WIN32) && defined(XP_CHAR_IS_WCHAR)
		xp_printf (
			XP_TEXT("error: cannot parse program - [%d] %ls\n"), 
			xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
#else
		xp_printf (
			XP_TEXT("error: cannot parse program - [%d] %s\n"), 
			xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
#endif
		xp_awk_close (awk);
		return -1;
	}

	if (xp_awk_run (awk, process_data, (void*)&data_io) == -1) 
	{
#if defined(__STAND_ALONE) && !defined(_WIN32) && defined(XP_CHAR_IS_WCHAR)
		xp_printf (
			XP_TEXT("error: cannot run program - [%d] %ls\n"), 
			xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
#else
		xp_printf (
			XP_TEXT("error: cannot run program - [%d] %s\n"), 
			xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
#endif
		xp_awk_close (awk);
		return -1;
	}

	xp_awk_close (awk);

#ifdef __linux
	muntrace ();
#endif
	return 0;
}