qse/ase/test/awk/awk.c

196 lines
3.7 KiB
C
Raw Normal View History

2006-03-31 16:35:37 +00:00
/*
2006-04-22 13:54:53 +00:00
* $Id: awk.c,v 1.26 2006-04-22 13:54:53 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 16:35:37 +00:00
#include <wchar.h>
2006-03-31 18:13:31 +00:00
#include <stdio.h>
2006-04-10 09:26:17 +00:00
#include <string.h>
2006-01-18 16:12:59 +00:00
2006-04-16 04:31:38 +00:00
#ifdef __STAND_ALONE
#define xp_printf xp_awk_printf
extern int xp_awk_printf (const xp_char_t* fmt, ...);
#else
2006-03-31 18:13:31 +00:00
#ifdef XP_CHAR_IS_MCHAR
#define xp_printf printf
#else
#define xp_printf wprintf
#endif
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
#define xp_strcmp xp_awk_strcmp
extern int xp_awk_strcmp (const xp_char_t* s1, const xp_char_t* s2);
#else
2006-04-10 09:22:05 +00:00
#ifdef XP_CHAR_IS_MCHAR
#define xp_strcmp strcmp
#else
#define xp_strcmp wcscmp
#endif
2006-04-16 04:31:38 +00:00
#endif
2006-04-10 09:22:05 +00:00
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
switch (cmd) {
2006-04-22 13:54:53 +00:00
case XP_AWK_INPUT_OPEN:
case XP_AWK_INPUT_CLOSE:
case XP_AWK_INPUT_NEXT:
2006-01-18 16:12:59 +00:00
return 0;
2006-04-22 13:54:53 +00:00
case XP_AWK_INPUT_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;
2006-04-22 13:54:53 +00:00
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;
2006-01-18 16:12:59 +00:00
}
return -1;
2006-04-22 13:54:53 +00:00
2006-01-18 16:12:59 +00:00
}
2006-01-30 17:50:38 +00:00
#ifdef __linux
#include <mcheck.h>
#endif
2006-04-18 16:27:29 +00:00
#if defined(__STAND_ALONE) && !defined(_WIN32)
2006-04-14 11:13:06 +00:00
int main (int argc, char* argv[])
#else
2005-11-14 15:23:54 +00:00
int xp_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-01-30 17:50:38 +00:00
#ifdef __linux
mtrace ();
#endif
2006-04-10 09:22:05 +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-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-04-19 02:52:53 +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 |
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-04-10 09:22:05 +00:00
if (xp_strcmp(argv[1], XP_TEXT("-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 (
XP_TEXT("error: cannot parse program - [%d] %ls\n"),
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
#else
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));
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 (
XP_TEXT("error: cannot run program - [%d] %ls\n"),
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
#else
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));
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-01-30 17:50:38 +00:00
#ifdef __linux
muntrace ();
#endif
2005-11-14 15:23:54 +00:00
return 0;
}