qse/ase/test/awk/awk.c

121 lines
2.0 KiB
C
Raw Normal View History

2006-03-31 16:35:37 +00:00
/*
* $Id: awk.c,v 1.14 2006-03-31 16:35:37 bacon Exp $
*/
2005-11-14 15:23:54 +00:00
#include <xp/awk/awk.h>
2006-03-31 16:35:37 +00:00
#include <xp/bas/stdio.h>
#include <wchar.h>
2006-01-18 16:12:59 +00:00
#ifdef __STAND_ALONE
static xp_ssize_t process_source (int cmd, void* arg, xp_char_t* data, xp_size_t size)
{
2006-03-29 16:39:04 +00:00
xp_char_t c;
2006-01-18 16:12:59 +00:00
switch (cmd) {
case XP_AWK_IO_OPEN:
case XP_AWK_IO_CLOSE:
return 0;
case XP_AWK_IO_DATA:
2006-03-15 15:34:59 +00:00
if (size <= 0) return -1;
2006-03-29 16:39:04 +00:00
#ifdef XP_CHAR_IS_MCHAR
c = fgetc (stdin);
#else
2006-01-18 16:12:59 +00:00
c = fgetwc (stdin);
2006-03-29 16:39:04 +00:00
#endif
2006-01-18 16:12:59 +00:00
if (c == XP_CHAR_EOF) return 0;
*data = c;
return 1;
}
return -1;
}
#else
2005-11-14 15:23:54 +00:00
#include <xp/bas/stdio.h>
#include <xp/bas/sio.h>
static xp_ssize_t process_source (int cmd, void* arg, xp_char_t* data, xp_size_t size)
{
xp_ssize_t n;
switch (cmd) {
case XP_AWK_IO_OPEN:
case XP_AWK_IO_CLOSE:
return 0;
case XP_AWK_IO_DATA:
if (size < 0) return -1;
n = xp_sio_getc (xp_sio_in, data);
if (n == 0) return 0;
if (n != 1) return -1;
return n;
}
return -1;
}
2006-01-18 16:12:59 +00:00
#endif
2005-11-14 15:23:54 +00:00
2006-01-30 17:50:38 +00:00
#ifdef __linux
#include <mcheck.h>
#endif
2006-03-29 16:39:04 +00:00
#ifdef _WIN32
2005-11-14 15:23:54 +00:00
int xp_main (int argc, xp_char_t* argv[])
2006-03-29 16:39:04 +00:00
#else
int xp_main (int argc, char* argv[])
#endif
2005-11-14 15:23:54 +00:00
{
2006-03-31 16:35:37 +00:00
xp_awk_t* awk;
2005-11-14 15:23:54 +00:00
2006-01-30 17:50:38 +00:00
#ifdef __linux
mtrace ();
#endif
2005-11-14 15:23:54 +00:00
#if 0
if (argc != 2) {
2006-01-18 16:12:59 +00:00
xp_printf (XP_TEXT("Usage: %s file\n"), argv[0]);
2005-11-14 15:23:54 +00:00
return -1;
}
#endif
2006-03-31 16:35:37 +00:00
if ((awk = xp_awk_open()) == XP_NULL) {
2006-01-18 16:12:59 +00:00
xp_printf (XP_TEXT("Error: cannot open awk\n"));
2005-11-14 15:23:54 +00:00
return -1;
}
2006-03-31 16:35:37 +00:00
if (xp_awk_attsrc(awk, process_source, XP_NULL) == -1) {
xp_awk_close (awk);
2006-01-18 16:12:59 +00:00
xp_printf (XP_TEXT("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 |
XP_AWK_SHADING | XP_AWK_IMPLICIT);
2006-03-05 17:07:33 +00:00
2006-03-31 16:35:37 +00:00
if (xp_awk_parse(awk) == -1) {
2006-01-31 16:57:45 +00:00
xp_printf (
XP_TEXT("error: cannot parse program - [%d] %s\n"),
2006-03-31 16:35:37 +00:00
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
xp_awk_close (awk);
2005-11-14 15:23:54 +00:00
return -1;
}
2006-03-31 16:35:37 +00:00
if (xp_awk_run(awk) == -1) {
2006-03-05 17:07:33 +00:00
xp_printf (
XP_TEXT("error: cannot run program - [%d] %s\n"),
2006-03-31 16:35:37 +00:00
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
xp_awk_close (awk);
2006-03-05 17:07:33 +00:00
}
2006-03-31 16:35:37 +00:00
xp_awk_close (awk);
2006-01-30 17:50:38 +00:00
#ifdef __linux
muntrace ();
#endif
2005-11-14 15:23:54 +00:00
return 0;
}