qse/ase/test/awk/awk.c

233 lines
4.5 KiB
C
Raw Normal View History

2006-03-31 16:35:37 +00:00
/*
2006-06-13 04:26:24 +00:00
* $Id: awk.c,v 1.35 2006-06-13 04:26:24 bacon Exp $
2006-03-31 16:35:37 +00:00
*/
2005-11-14 15:23:54 +00:00
#include <xp/awk/awk.h>
2006-03-31 18:13:31 +00:00
#include <stdio.h>
2006-04-10 09:26:17 +00:00
#include <string.h>
2006-04-30 18:05:07 +00:00
#ifdef XP_CHAR_IS_CHAR
#include <wchar.h>
#endif
2006-01-18 16:12:59 +00:00
2006-05-06 16:05:12 +00:00
#ifndef __STAND_ALONE
#include <xp/bas/stdio.h>
#include <xp/bas/string.h>
2006-04-16 04:31:38 +00:00
#endif
2006-01-18 16:12:59 +00:00
2006-04-16 04:31:38 +00:00
#ifdef __STAND_ALONE
2006-05-06 16:05:12 +00:00
#define xp_printf xp_awk_printf
extern int xp_awk_printf (const xp_char_t* fmt, ...);
#define xp_strcmp xp_awk_strcmp
extern int xp_awk_strcmp (const xp_char_t* s1, const xp_char_t* s2);
2006-04-16 04:31:38 +00:00
#endif
2006-04-10 09:22:05 +00:00
2006-05-12 09:39:20 +00:00
#if defined(_WIN32) && defined(__STAND_ALONE) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif
2006-05-12 11:10:26 +00:00
#if defined(__linux) && defined(_DEBUG)
2006-05-12 09:39:20 +00:00
#include <mcheck.h>
#endif
2006-04-16 04:31:38 +00:00
static xp_ssize_t process_source (
int cmd, void* arg, xp_char_t* data, xp_size_t size)
2006-01-18 16:12:59 +00:00
{
2006-03-29 16:39:04 +00:00
xp_char_t c;
2006-01-18 16:12:59 +00:00
2006-06-13 04:26:24 +00:00
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;
}
2006-04-22 13:54:53 +00:00
}
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;
2006-06-13 04:26:24 +00:00
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;
}
2006-01-18 16:12:59 +00:00
}
return -1;
}
2006-04-18 16:27:29 +00:00
#if defined(__STAND_ALONE) && !defined(_WIN32)
2006-05-12 09:39:20 +00:00
static int __main (int argc, char* argv[])
2006-04-14 11:13:06 +00:00
#else
2006-05-12 09:39:20 +00:00
static int __main (int argc, xp_char_t* argv[])
2006-04-14 11:13:06 +00:00
#endif
2005-11-14 15:23:54 +00:00
{
2006-03-31 16:35:37 +00:00
xp_awk_t* awk;
2006-04-22 13:54:53 +00:00
struct data_io data_io = { "awk.in", NULL };
2005-11-14 15:23:54 +00:00
2006-04-10 09:22:05 +00:00
if ((awk = xp_awk_open()) == XP_NULL)
{
2006-05-06 16:05:12 +00:00
xp_printf (XP_T("Error: cannot open awk\n"));
2005-11-14 15:23:54 +00:00
return -1;
}
2006-04-10 09:22:05 +00:00
if (xp_awk_attsrc(awk, process_source, XP_NULL) == -1)
{
2006-03-31 16:35:37 +00:00
xp_awk_close (awk);
2006-05-06 16:05:12 +00:00
xp_printf (XP_T("Error: cannot attach source\n"));
2005-11-14 15:23:54 +00:00
return -1;
}
2006-03-31 16:35:37 +00:00
xp_awk_setparseopt (awk,
XP_AWK_EXPLICIT | XP_AWK_UNIQUE |
2006-04-06 16:25:37 +00:00
XP_AWK_SHADING | XP_AWK_IMPLICIT | XP_AWK_SHIFT);
2006-03-05 17:07:33 +00:00
2006-04-10 09:22:05 +00:00
if (argc == 2)
{
2006-04-18 16:27:29 +00:00
#if defined(__STAND_ALONE) && !defined(_WIN32)
2006-04-14 11:13:06 +00:00
if (strcmp(argv[1], "-m") == 0)
#else
2006-05-06 16:05:12 +00:00
if (xp_strcmp(argv[1], XP_T("-m")) == 0)
2006-04-14 11:13:06 +00:00
#endif
2006-04-10 09:22:05 +00:00
{
xp_awk_setrunopt (awk, XP_AWK_RUNMAIN);
}
}
2006-04-09 15:34:38 +00:00
2006-04-10 09:22:05 +00:00
if (xp_awk_parse(awk) == -1)
{
2006-04-18 16:27:29 +00:00
#if defined(__STAND_ALONE) && !defined(_WIN32) && defined(XP_CHAR_IS_WCHAR)
2006-04-14 16:26:00 +00:00
xp_printf (
2006-05-13 16:33:07 +00:00
XP_T("error: cannot parse program - line %u [%d] %ls\n"),
(unsigned int)xp_awk_getsrcline(awk),
2006-04-14 16:26:00 +00:00
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
#else
2006-01-31 16:57:45 +00:00
xp_printf (
2006-05-13 16:33:07 +00:00
XP_T("error: cannot parse program - line %u [%d] %s\n"),
(unsigned int)xp_awk_getsrcline(awk),
2006-03-31 16:35:37 +00:00
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
2006-04-14 16:26:00 +00:00
#endif
2006-03-31 16:35:37 +00:00
xp_awk_close (awk);
2005-11-14 15:23:54 +00:00
return -1;
}
2006-04-22 13:54:53 +00:00
if (xp_awk_run (awk, process_data, (void*)&data_io) == -1)
2006-04-10 09:22:05 +00:00
{
2006-04-18 16:27:29 +00:00
#if defined(__STAND_ALONE) && !defined(_WIN32) && defined(XP_CHAR_IS_WCHAR)
2006-04-14 16:26:00 +00:00
xp_printf (
2006-05-06 16:05:12 +00:00
XP_T("error: cannot run program - [%d] %ls\n"),
2006-04-14 16:26:00 +00:00
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
#else
2006-03-05 17:07:33 +00:00
xp_printf (
2006-05-06 16:05:12 +00:00
XP_T("error: cannot run program - [%d] %s\n"),
2006-03-31 16:35:37 +00:00
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
2006-04-14 16:26:00 +00:00
#endif
2006-03-31 16:35:37 +00:00
xp_awk_close (awk);
2006-04-06 16:25:37 +00:00
return -1;
2006-03-05 17:07:33 +00:00
}
2006-03-31 16:35:37 +00:00
xp_awk_close (awk);
2006-05-12 09:39:20 +00:00
return 0;
}
2006-01-30 17:50:38 +00:00
2006-05-12 09:39:20 +00:00
#if defined(__STAND_ALONE) && !defined(_WIN32)
int main (int argc, char* argv[])
#else
int xp_main (int argc, xp_char_t* argv[])
#endif
{
int n;
2006-05-12 11:10:26 +00:00
#if defined(__linux) && defined(_DEBUG)
2006-05-12 09:39:20 +00:00
mtrace ();
#endif
2006-05-12 10:56:18 +00:00
/*#if defined(_WIN32) && defined(__STAND_ALONE) && defined(_DEBUG)
_CrtSetDbgFlag (_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF);
#endif*/
2006-05-12 09:39:20 +00:00
n = __main (argc, argv);
2006-05-12 11:10:26 +00:00
#if defined(__linux) && defined(_DEBUG)
2006-01-30 17:50:38 +00:00
muntrace ();
#endif
2006-05-12 09:39:20 +00:00
#if defined(_WIN32) && defined(__STAND_ALONE) && defined(_DEBUG)
_CrtDumpMemoryLeaks ();
2006-05-13 15:51:43 +00:00
wprintf (L"Press ENTER to quit\n");
getchar ();
2006-05-12 09:39:20 +00:00
#endif
2006-05-13 15:51:43 +00:00
2006-05-12 09:39:20 +00:00
return n;
2005-11-14 15:23:54 +00:00
}
2006-05-12 09:39:20 +00:00