*** empty log message ***
This commit is contained in:
parent
85a2b9c33b
commit
0d671f78a7
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk.h,v 1.126 2006-10-12 04:17:30 bacon Exp $
|
||||
* $Id: awk.h,v 1.127 2006-10-13 10:18:10 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_AWK_H_
|
||||
@ -25,10 +25,11 @@ typedef xp_ssize_t (*xp_awk_io_t) (
|
||||
|
||||
struct xp_awk_extio_t
|
||||
{
|
||||
int type; /* [IN] console, file, coproc, pipe */
|
||||
int mode; /* [IN] read, write, etc */
|
||||
xp_char_t* name; /* [IN] */
|
||||
void* handle; /* [OUT] */
|
||||
int type; /* [IN] console, file, coproc, pipe */
|
||||
int mode; /* [IN] read, write, etc */
|
||||
xp_char_t* name; /* [IN] */
|
||||
void* custom_data; /* [IN] */
|
||||
void* handle; /* [OUT] */
|
||||
|
||||
/* input buffer */
|
||||
struct
|
||||
@ -92,6 +93,7 @@ struct xp_awk_runios_t
|
||||
xp_awk_io_t coproc;
|
||||
xp_awk_io_t file;
|
||||
xp_awk_io_t console;
|
||||
void* custom_data;
|
||||
};
|
||||
|
||||
struct xp_awk_runcbs_t
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk_i.h,v 1.65 2006-10-12 14:36:25 bacon Exp $
|
||||
* $Id: awk_i.h,v 1.66 2006-10-13 10:18:10 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_AWKI_H_
|
||||
@ -260,6 +260,7 @@ struct xp_awk_run_t
|
||||
struct
|
||||
{
|
||||
xp_awk_io_t handler[XP_AWK_EXTIO_NUM];
|
||||
void* custom_data;
|
||||
xp_awk_extio_t* chain;
|
||||
} extio;
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: extio.c,v 1.52 2006-10-12 04:17:30 bacon Exp $
|
||||
* $Id: extio.c,v 1.53 2006-10-13 10:18:10 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -132,14 +132,14 @@ int xp_awk_readextio (
|
||||
p->type = (extio_type | extio_mask);
|
||||
p->mode = extio_mode;
|
||||
p->handle = XP_NULL;
|
||||
p->next = XP_NULL;
|
||||
p->custom_data = run->extio.custom_data;
|
||||
|
||||
p->in.buf[0] = XP_T('\0');
|
||||
p->in.pos = 0;
|
||||
p->in.len = 0;
|
||||
p->in.eof = xp_false;
|
||||
|
||||
p->next = XP_NULL;
|
||||
|
||||
n = handler (XP_AWK_IO_OPEN, p, XP_NULL, 0);
|
||||
if (n == -1)
|
||||
{
|
||||
@ -159,11 +159,11 @@ int xp_awk_readextio (
|
||||
p->next = run->extio.chain;
|
||||
run->extio.chain = p;
|
||||
|
||||
/* n == 0 indicates that it has reached the end of input.
|
||||
* the user io handler can return 0 for the open request
|
||||
* if it doesn't have any files to open. One advantage
|
||||
* of doing this would be that you can skip the entire
|
||||
* pattern-block matching and exeuction. */
|
||||
/* usually, n == 0 indicates that it has reached the end
|
||||
* of the input. the user io handler can return 0 for the
|
||||
* open request if it doesn't have any files to open. One
|
||||
* advantage of doing this would be that you can skip the
|
||||
* entire pattern-block matching and exeuction. */
|
||||
if (n == 0) return 0;
|
||||
}
|
||||
|
||||
@ -457,6 +457,7 @@ int xp_awk_writeextio_str (
|
||||
p->mode = extio_mode;
|
||||
p->handle = XP_NULL;
|
||||
p->next = XP_NULL;
|
||||
p->custom_data = run->extio.custom_data;
|
||||
|
||||
n = handler (XP_AWK_IO_OPEN, p, XP_NULL, 0);
|
||||
if (n == -1)
|
||||
@ -477,7 +478,11 @@ int xp_awk_writeextio_str (
|
||||
p->next = run->extio.chain;
|
||||
run->extio.chain = p;
|
||||
|
||||
/* read the comment in xp_awk_readextio */
|
||||
/* usually, n == 0 indicates that it has reached the end
|
||||
* of the input. the user io handler can return 0 for the
|
||||
* open request if it doesn't have any files to open. One
|
||||
* advantage of doing this would be that you can skip the
|
||||
* entire pattern-block matching and exeuction. */
|
||||
if (n == 0) return 0;
|
||||
}
|
||||
|
||||
|
352
ase/awk/jni.c
352
ase/awk/jni.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: jni.c,v 1.5 2006-10-12 14:36:25 bacon Exp $
|
||||
* $Id: jni.c,v 1.6 2006-10-13 10:18:10 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/jni.h>
|
||||
@ -13,17 +13,23 @@
|
||||
#define EXCEPTION_AWK "xpkit/xpj/awk/AwkException"
|
||||
#define FIELD_AWK "__awk"
|
||||
|
||||
enum
|
||||
{
|
||||
SOURCE_READ = 1,
|
||||
SOURCE_WRITE = 2
|
||||
};
|
||||
|
||||
/* TODO: what if xp_char_t is xp_mchar_t??? */
|
||||
|
||||
static xp_ssize_t __read_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count);
|
||||
static xp_ssize_t __write_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count);
|
||||
|
||||
static xp_ssize_t __call_java_read_source (
|
||||
JNIEnv* env, jobject obj, xp_char_t* buf, xp_size_t size);
|
||||
static xp_ssize_t __call_java_write_source (
|
||||
JNIEnv* env, jobject obj, xp_char_t* buf, xp_size_t size);
|
||||
static xp_ssize_t __process_extio_console (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count);
|
||||
|
||||
typedef struct srcio_data_t srcio_data_t;
|
||||
typedef struct runio_data_t runio_data_t;
|
||||
|
||||
struct srcio_data_t
|
||||
{
|
||||
@ -31,6 +37,12 @@ struct srcio_data_t
|
||||
jobject obj;
|
||||
};
|
||||
|
||||
struct runio_data_t
|
||||
{
|
||||
JNIEnv* env;
|
||||
jobject obj;
|
||||
};
|
||||
|
||||
static void* __awk_malloc (xp_size_t n, void* custom_data)
|
||||
{
|
||||
return malloc (n);
|
||||
@ -78,10 +90,16 @@ JNIEXPORT void JNICALL Java_xpkit_xpj_awk_Awk_open (JNIEnv* env, jobject obj)
|
||||
|
||||
syscas.memcpy = memcpy;
|
||||
syscas.memset = memset;
|
||||
/* TODO: */
|
||||
#ifdef _WIN32
|
||||
syscas.sprintf = _snwprintf;
|
||||
syscas.dprintf = wprintf;
|
||||
syscas.abort = abort;
|
||||
#else
|
||||
/* TODO: */
|
||||
syscas.sprintf = XXXXX;
|
||||
syscas.dprintf = XXXXX;
|
||||
syscas.abort = abort;
|
||||
#endif
|
||||
|
||||
awk = xp_awk_open (&syscas);
|
||||
if (awk == NULL)
|
||||
@ -145,13 +163,12 @@ JNIEXPORT void JNICALL Java_xpkit_xpj_awk_Awk_parse (JNIEnv* env, jobject obj)
|
||||
srcios.out = __write_source;
|
||||
srcios.custom_data = &srcio_data;
|
||||
|
||||
printf ("OK.......\n");
|
||||
if (xp_awk_parse (awk, &srcios) == -1)
|
||||
{
|
||||
printf ("parse error.......\n");
|
||||
except = (*env)->FindClass (env, EXCEPTION_AWK);
|
||||
if (except == 0) return;
|
||||
(*env)->ThrowNew (env, except, "ERROR ....");
|
||||
(*env)->ThrowNew (env, except, "Parse Error ...");
|
||||
printf ("parse error -> line [%d] %S\n", xp_awk_getsrcline(awk), xp_awk_geterrstr(xp_awk_geterrnum(awk)));
|
||||
return;
|
||||
}
|
||||
@ -159,41 +176,83 @@ printf ("parse error -> line [%d] %S\n", xp_awk_getsrcline(awk), xp_awk_geterrst
|
||||
|
||||
JNIEXPORT void JNICALL Java_xpkit_xpj_awk_Awk_run (JNIEnv* env, jobject obj)
|
||||
{
|
||||
jclass class;
|
||||
jfieldID fid;
|
||||
jthrowable except;
|
||||
|
||||
xp_awk_t* awk;
|
||||
xp_awk_runios_t runios;
|
||||
runio_data_t runio_data;
|
||||
|
||||
class = (*env)->GetObjectClass (env, obj);
|
||||
|
||||
fid = (*env)->GetFieldID (env, class, FIELD_AWK, "J");
|
||||
if (fid == 0) return;
|
||||
|
||||
awk = (xp_awk_t*) (*env)->GetLongField (env, obj, fid);
|
||||
|
||||
runio_data.env = env;
|
||||
runio_data.obj = obj;
|
||||
|
||||
runios.pipe = XP_NULL;
|
||||
runios.coproc = XP_NULL;
|
||||
runios.file = XP_NULL;
|
||||
runios.console = __process_extio_console;
|
||||
runios.custom_data = &runio_data;
|
||||
|
||||
if (xp_awk_run (awk, &runios, XP_NULL, XP_NULL) == -1)
|
||||
{
|
||||
except = (*env)->FindClass (env, EXCEPTION_AWK);
|
||||
if (except == 0) return;
|
||||
(*env)->ThrowNew (env, except, "Run Error ...");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static xp_ssize_t __read_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count)
|
||||
static xp_ssize_t __call_java_open_source (JNIEnv* env, jobject obj, int mode)
|
||||
{
|
||||
srcio_data_t* srcio_data;
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jthrowable thrown;
|
||||
jint ret;
|
||||
|
||||
srcio_data = (srcio_data_t*)arg;
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
if (cmd == XP_AWK_IO_OPEN || cmd == XP_AWK_IO_CLOSE) return 0;
|
||||
mid = (*env)->GetMethodID (env, class, "open_source", "(I)I");
|
||||
if (mid == 0) return -1;
|
||||
|
||||
if (cmd == XP_AWK_IO_READ)
|
||||
ret = (*env)->CallIntMethod (env, obj, mid, mode);
|
||||
thrown = (*env)->ExceptionOccurred (env);
|
||||
if (thrown)
|
||||
{
|
||||
return __call_java_read_source (
|
||||
srcio_data->env, srcio_data->obj, data, count);
|
||||
(*env)->ExceptionClear (env);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static xp_ssize_t __write_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count)
|
||||
static xp_ssize_t __call_java_close_source (JNIEnv* env, jobject obj, int mode)
|
||||
{
|
||||
srcio_data_t* srcio_data;
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jthrowable thrown;
|
||||
jint ret;
|
||||
|
||||
srcio_data = (srcio_data_t*)arg;
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
if (cmd == XP_AWK_IO_OPEN || cmd == XP_AWK_IO_CLOSE) return 0;
|
||||
if (cmd == XP_AWK_IO_WRITE)
|
||||
mid = (*env)->GetMethodID (env, class, "close_source", "(I)I");
|
||||
if (mid == 0) return -1;
|
||||
|
||||
ret = (*env)->CallIntMethod (env, obj, mid, mode);
|
||||
thrown = (*env)->ExceptionOccurred (env);
|
||||
if (thrown)
|
||||
{
|
||||
return __call_java_write_source (
|
||||
srcio_data->env, srcio_data->obj, data, count);
|
||||
(*env)->ExceptionClear (env);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static xp_ssize_t __call_java_read_source (
|
||||
@ -202,23 +261,28 @@ static xp_ssize_t __call_java_read_source (
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jcharArray array;
|
||||
xp_ssize_t i, n;
|
||||
jchar* tmp;
|
||||
jint ret, i;
|
||||
jthrowable thrown;
|
||||
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
mid = (*env)->GetMethodID (env, class, "read_source", "([CI)I");
|
||||
if (mid == 0) return -1;
|
||||
|
||||
array = (*env)->NewCharArray (env, 1024);
|
||||
array = (*env)->NewCharArray (env, size);
|
||||
if (array == NULL) return -1;
|
||||
|
||||
n = (*env)->CallIntMethod (env, obj, mid, array, 1024);
|
||||
ret = (*env)->CallIntMethod (env, obj, mid, array, size);
|
||||
thrown = (*env)->ExceptionOccurred (env);
|
||||
if (thrown)
|
||||
{
|
||||
(*env)->ExceptionClear (env);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
// TODO: how to handle error..
|
||||
// TODO: what is xp_char_t is xp_mchar_t? use UTF8 ???
|
||||
tmp = (*env)->GetCharArrayElements (env, array, 0);
|
||||
for (i = 0; i < n && i < size; i++) buf[i] = (xp_char_t)tmp[i];
|
||||
for (i = 0; i < ret; i++) buf[i] = (xp_char_t)tmp[i];
|
||||
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
|
||||
|
||||
return i;
|
||||
@ -230,8 +294,9 @@ static xp_ssize_t __call_java_write_source (
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jcharArray array;
|
||||
xp_ssize_t i;
|
||||
jchar* tmp;
|
||||
jint ret, i;
|
||||
jthrowable thrown;
|
||||
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
@ -241,11 +306,224 @@ static xp_ssize_t __call_java_write_source (
|
||||
array = (*env)->NewCharArray (env, size);
|
||||
if (array == NULL) return -1;
|
||||
|
||||
// TODO: how to handle error..
|
||||
// TODO: what is xp_char_t is xp_mchar_t? use UTF8 ???
|
||||
tmp = (*env)->GetCharArrayElements (env, array, 0);
|
||||
for (i = 0; i < size; i++) tmp[i] = (jchar)buf[i];
|
||||
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
|
||||
|
||||
return (*env)->CallIntMethod (env, obj, mid, array, size);
|
||||
ret = (*env)->CallIntMethod (env, obj, mid, array, size);
|
||||
thrown = (*env)->ExceptionOccurred (env);
|
||||
if (thrown)
|
||||
{
|
||||
(*env)->ExceptionClear (env);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static xp_ssize_t __call_java_open_extio (JNIEnv* env, jobject obj, char* name)
|
||||
{
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jthrowable thrown;
|
||||
jint ret;
|
||||
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
mid = (*env)->GetMethodID (env, class, name, "()I");
|
||||
if (mid == 0) return -1;
|
||||
|
||||
ret = (*env)->CallIntMethod (env, obj, mid);
|
||||
thrown = (*env)->ExceptionOccurred (env);
|
||||
if (thrown)
|
||||
{
|
||||
(*env)->ExceptionClear (env);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static xp_ssize_t __call_java_close_extio (JNIEnv* env, jobject obj, char* name)
|
||||
{
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jthrowable thrown;
|
||||
jint ret;
|
||||
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
mid = (*env)->GetMethodID (env, class, name, "()I");
|
||||
if (mid == 0) return -1;
|
||||
|
||||
ret = (*env)->CallIntMethod (env, obj, mid);
|
||||
thrown = (*env)->ExceptionOccurred (env);
|
||||
if (thrown)
|
||||
{
|
||||
(*env)->ExceptionClear (env);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static xp_ssize_t __call_java_read_extio (
|
||||
JNIEnv* env, jobject obj, char* name, xp_char_t* buf, xp_size_t size)
|
||||
{
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jcharArray array;
|
||||
jchar* tmp;
|
||||
jint ret, i;
|
||||
jthrowable thrown;
|
||||
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
mid = (*env)->GetMethodID (env, class, name, "([CI)I");
|
||||
if (mid == 0) return -1;
|
||||
|
||||
array = (*env)->NewCharArray (env, size);
|
||||
if (array == NULL) return -1;
|
||||
|
||||
ret = (*env)->CallIntMethod (env, obj, mid, array, size);
|
||||
thrown = (*env)->ExceptionOccurred (env);
|
||||
if (thrown)
|
||||
{
|
||||
(*env)->ExceptionClear (env);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
tmp = (*env)->GetCharArrayElements (env, array, 0);
|
||||
for (i = 0; i < ret; i++) buf[i] = (xp_char_t)tmp[i];
|
||||
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static xp_ssize_t __call_java_write_extio (
|
||||
JNIEnv* env, jobject obj, char* name, xp_char_t* data, xp_size_t size)
|
||||
{
|
||||
jclass class;
|
||||
jmethodID mid;
|
||||
jcharArray array;
|
||||
xp_ssize_t i;
|
||||
jchar* tmp;
|
||||
jint ret;
|
||||
jthrowable thrown;
|
||||
|
||||
class = (*env)->GetObjectClass(env, obj);
|
||||
|
||||
mid = (*env)->GetMethodID (env, class, name, "([CI)I");
|
||||
if (mid == 0) return -1;
|
||||
|
||||
array = (*env)->NewCharArray (env, size);
|
||||
if (array == NULL) return -1;
|
||||
|
||||
tmp = (*env)->GetCharArrayElements (env, array, 0);
|
||||
for (i = 0; i < size; i++) tmp[i] = (jchar)data[i];
|
||||
(*env)->ReleaseCharArrayElements (env, array, tmp, 0);
|
||||
|
||||
ret = (*env)->CallIntMethod (env, obj, mid, array, size);
|
||||
thrown = (*env)->ExceptionOccurred (env);
|
||||
if (thrown)
|
||||
{
|
||||
(*env)->ExceptionClear (env);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static xp_ssize_t __read_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count)
|
||||
{
|
||||
srcio_data_t* srcio_data = (srcio_data_t*)arg;
|
||||
|
||||
if (cmd == XP_AWK_IO_OPEN)
|
||||
{
|
||||
return __call_java_open_source (
|
||||
srcio_data->env, srcio_data->obj, SOURCE_READ);
|
||||
}
|
||||
else if (cmd == XP_AWK_IO_CLOSE)
|
||||
{
|
||||
return __call_java_close_source (
|
||||
srcio_data->env, srcio_data->obj, SOURCE_READ);
|
||||
}
|
||||
else if (cmd == XP_AWK_IO_READ)
|
||||
{
|
||||
return __call_java_read_source (
|
||||
srcio_data->env, srcio_data->obj, data, count);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static xp_ssize_t __write_source (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count)
|
||||
{
|
||||
srcio_data_t* srcio_data = (srcio_data_t*)arg;
|
||||
|
||||
if (cmd == XP_AWK_IO_OPEN)
|
||||
{
|
||||
return __call_java_open_source (
|
||||
srcio_data->env, srcio_data->obj, SOURCE_WRITE);
|
||||
}
|
||||
else if (cmd == XP_AWK_IO_CLOSE)
|
||||
{
|
||||
return __call_java_close_source (
|
||||
srcio_data->env, srcio_data->obj, SOURCE_WRITE);
|
||||
}
|
||||
else if (cmd == XP_AWK_IO_WRITE)
|
||||
{
|
||||
return __call_java_write_source (
|
||||
srcio_data->env, srcio_data->obj, data, count);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static xp_ssize_t __process_extio_console (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t size)
|
||||
{
|
||||
xp_awk_extio_t* epa = (xp_awk_extio_t*)arg;
|
||||
runio_data_t* runio_data = (runio_data_t*)epa->custom_data;
|
||||
|
||||
if (cmd == XP_AWK_IO_OPEN)
|
||||
{
|
||||
return __call_java_open_extio (
|
||||
runio_data->env, runio_data->obj, "open_console");
|
||||
}
|
||||
else if (cmd == XP_AWK_IO_CLOSE)
|
||||
{
|
||||
return __call_java_close_extio (
|
||||
runio_data->env, runio_data->obj, "close_console");
|
||||
}
|
||||
|
||||
else if (cmd == XP_AWK_IO_READ)
|
||||
{
|
||||
return __call_java_read_extio (
|
||||
runio_data->env, runio_data->obj, "read_console",
|
||||
data, size);
|
||||
}
|
||||
else if (cmd == XP_AWK_IO_WRITE)
|
||||
{
|
||||
/* epa->handle not used at all */
|
||||
/* TODO: error handling */
|
||||
return __call_java_write_extio (
|
||||
runio_data->env, runio_data->obj, "write_console",
|
||||
data, size);
|
||||
}
|
||||
#if 0
|
||||
else if (cmd == XP_AWK_IO_FLUSH)
|
||||
{
|
||||
if (fflush ((FILE*)epa->handle) == EOF) return -1;
|
||||
return 0;
|
||||
}
|
||||
else if (cmd == XP_AWK_IO_NEXT)
|
||||
{
|
||||
return next_extio_console (epa);
|
||||
}
|
||||
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: parse.c,v 1.188 2006-10-12 04:17:31 bacon Exp $
|
||||
* $Id: parse.c,v 1.189 2006-10-13 10:18:10 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -318,16 +318,19 @@ static struct __bvent __bvtab[] =
|
||||
|
||||
int xp_awk_parse (xp_awk_t* awk, xp_awk_srcios_t* srcios)
|
||||
{
|
||||
int n = 0;
|
||||
int n = 0, op;
|
||||
|
||||
xp_awk_assert (awk, srcios != XP_NULL && srcios->in != XP_NULL);
|
||||
|
||||
xp_awk_clear (awk);
|
||||
awk->src.ios = srcios;
|
||||
|
||||
if (awk->src.ios->in (
|
||||
XP_AWK_IO_OPEN, awk->src.ios->custom_data, XP_NULL, 0) == -1)
|
||||
op = awk->src.ios->in (
|
||||
XP_AWK_IO_OPEN, awk->src.ios->custom_data, XP_NULL, 0);
|
||||
if (op == -1)
|
||||
{
|
||||
/* cannot open the source file.
|
||||
* it doesn't even have to call CLOSE */
|
||||
awk->errnum = XP_AWK_ESRCINOPEN;
|
||||
return -1;
|
||||
}
|
||||
@ -338,30 +341,37 @@ int xp_awk_parse (xp_awk_t* awk, xp_awk_srcios_t* srcios)
|
||||
goto exit_parse;
|
||||
}
|
||||
|
||||
/* get the first character */
|
||||
if (__get_char(awk) == -1)
|
||||
/* the user io handler for the source code input returns 0 when
|
||||
* it doesn't have any files to open. this is the same condition
|
||||
* as the source code file is empty. so it will perform the parsing
|
||||
* when op is positive, which means there are something to parse */
|
||||
if (op > 0)
|
||||
{
|
||||
n = -1;
|
||||
goto exit_parse;
|
||||
}
|
||||
|
||||
/* get the first token */
|
||||
if (__get_token(awk) == -1)
|
||||
{
|
||||
n = -1;
|
||||
goto exit_parse;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (MATCH(awk,TOKEN_EOF)) break;
|
||||
if (MATCH(awk,TOKEN_NEWLINE)) continue;
|
||||
|
||||
if (__parse_progunit (awk) == XP_NULL)
|
||||
/* get the first character */
|
||||
if (__get_char(awk) == -1)
|
||||
{
|
||||
n = -1;
|
||||
goto exit_parse;
|
||||
}
|
||||
|
||||
/* get the first token */
|
||||
if (__get_token(awk) == -1)
|
||||
{
|
||||
n = -1;
|
||||
goto exit_parse;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (MATCH(awk,TOKEN_EOF)) break;
|
||||
if (MATCH(awk,TOKEN_NEWLINE)) continue;
|
||||
|
||||
if (__parse_progunit (awk) == XP_NULL)
|
||||
{
|
||||
n = -1;
|
||||
goto exit_parse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
awk->tree.nglobals = xp_awk_tab_getsize(&awk->parse.globals);
|
||||
@ -379,7 +389,7 @@ exit_parse:
|
||||
if (awk->src.ios->in (
|
||||
XP_AWK_IO_CLOSE, awk->src.ios->custom_data, XP_NULL, 0) == -1)
|
||||
{
|
||||
if (n == 0)
|
||||
if (n != -1)
|
||||
{
|
||||
/* this is to keep the earlier error above
|
||||
* that might be more critical than this */
|
||||
@ -4121,27 +4131,44 @@ static int __deparse (xp_awk_t* awk)
|
||||
xp_awk_chain_t* chain;
|
||||
xp_char_t tmp[xp_sizeof(xp_size_t)*8 + 32];
|
||||
struct __deparse_func_t df;
|
||||
int n;
|
||||
int n = 0, op;
|
||||
|
||||
xp_awk_assert (awk, awk->src.ios->out != XP_NULL);
|
||||
|
||||
awk->src.shared.buf_len = 0;
|
||||
awk->src.shared.buf_pos = 0;
|
||||
|
||||
/* TODO: more error handling */
|
||||
if (awk->src.ios->out (
|
||||
XP_AWK_IO_OPEN, awk->src.ios->custom_data, XP_NULL, 0) == -1)
|
||||
op = awk->src.ios->out (
|
||||
XP_AWK_IO_OPEN, awk->src.ios->custom_data, XP_NULL, 0);
|
||||
if (op == -1)
|
||||
{
|
||||
awk->errnum = XP_AWK_ESRCOUTOPEN;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (op == 0)
|
||||
{
|
||||
/* the result of the open operation indicates that the
|
||||
* file has been open but reached the end. so it has to
|
||||
* skip the entire deparsing procedure as it can't write
|
||||
* any single characters on such an io handler. but note
|
||||
* that this is not really an error for the parse and deparser.
|
||||
*
|
||||
* in fact, there are two ways to skip deparsing.
|
||||
* 1. set awk->src.ios.out to NULL.
|
||||
* 2. set awk->src.ios.out to a normal handler but
|
||||
* make it return 0 on the OPEN request.
|
||||
*/
|
||||
n = 0;
|
||||
goto exit_deparse;
|
||||
}
|
||||
|
||||
#define EXIT_DEPARSE(num) \
|
||||
do { n = -1; awk->errnum = num ; goto exit_deparse; } while(0)
|
||||
|
||||
if (awk->tree.nglobals > awk->tree.nbglobals)
|
||||
{
|
||||
xp_size_t i, n;
|
||||
xp_size_t i, len;
|
||||
|
||||
xp_awk_assert (awk, awk->tree.nglobals > 0);
|
||||
if (xp_awk_putsrcstr (awk, XP_T("global ")) == -1)
|
||||
@ -4149,19 +4176,19 @@ static int __deparse (xp_awk_t* awk)
|
||||
|
||||
for (i = awk->tree.nbglobals; i < awk->tree.nglobals - 1; i++)
|
||||
{
|
||||
n = xp_awk_longtostr ((xp_long_t)i,
|
||||
len = xp_awk_longtostr ((xp_long_t)i,
|
||||
10, XP_T("__global"), tmp, xp_countof(tmp));
|
||||
xp_awk_assert (awk, n != (xp_size_t)-1);
|
||||
if (xp_awk_putsrcstrx (awk, tmp, n) == -1)
|
||||
xp_awk_assert (awk, len != (xp_size_t)-1);
|
||||
if (xp_awk_putsrcstrx (awk, tmp, len) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
if (xp_awk_putsrcstr (awk, XP_T(", ")) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
}
|
||||
|
||||
n = xp_awk_longtostr ((xp_long_t)i,
|
||||
len = xp_awk_longtostr ((xp_long_t)i,
|
||||
10, XP_T("__global"), tmp, xp_countof(tmp));
|
||||
xp_awk_assert (awk, n != (xp_size_t)-1);
|
||||
if (xp_awk_putsrcstrx (awk, tmp, n) == -1)
|
||||
xp_awk_assert (awk, len != (xp_size_t)-1);
|
||||
if (xp_awk_putsrcstrx (awk, tmp, len) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
if (xp_awk_putsrcstr (awk, XP_T(";\n\n")) == -1)
|
||||
EXIT_DEPARSE (XP_AWK_ESRCOUTWRITE);
|
||||
@ -4231,7 +4258,7 @@ exit_deparse:
|
||||
{
|
||||
if (n != -1)
|
||||
{
|
||||
awk->errnum = XP_AWK_ESRCOUTOPEN;
|
||||
awk->errnum = XP_AWK_ESRCOUTCLOSE;
|
||||
n = -1;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: run.c,v 1.234 2006-10-12 14:36:25 bacon Exp $
|
||||
* $Id: run.c,v 1.235 2006-10-13 10:18:10 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -645,6 +645,7 @@ static int __init_run (xp_awk_run_t* run, xp_awk_runios_t* runios, int* errnum)
|
||||
run->extio.handler[XP_AWK_EXTIO_COPROC] = runios->coproc;
|
||||
run->extio.handler[XP_AWK_EXTIO_FILE] = runios->file;
|
||||
run->extio.handler[XP_AWK_EXTIO_CONSOLE] = runios->console;
|
||||
run->extio.custom_data = runios->custom_data;
|
||||
run->extio.chain = XP_NULL;
|
||||
|
||||
run->global.rs = XP_NULL;
|
||||
@ -789,38 +790,42 @@ static int __build_runarg (xp_awk_run_t* run, xp_awk_runarg_t* runarg)
|
||||
}
|
||||
xp_awk_refupval (v_argv);
|
||||
|
||||
for (argc = 0, p = runarg; p->ptr != XP_NULL; argc++, p++)
|
||||
if (runarg == XP_NULL) argc = 0;
|
||||
else
|
||||
{
|
||||
v_tmp = xp_awk_makestrval (run, p->ptr, p->len);
|
||||
if (v_tmp == XP_NULL)
|
||||
for (argc = 0, p = runarg; p->ptr != XP_NULL; argc++, p++)
|
||||
{
|
||||
xp_awk_refdownval (run, v_argv);
|
||||
run->errnum = XP_AWK_ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
v_tmp = xp_awk_makestrval (run, p->ptr, p->len);
|
||||
if (v_tmp == XP_NULL)
|
||||
{
|
||||
xp_awk_refdownval (run, v_argv);
|
||||
run->errnum = XP_AWK_ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
key_len = xp_awk_longtostr (
|
||||
argc, 10, XP_NULL, key, xp_countof(key));
|
||||
xp_awk_assert (run->awk, key_len != (xp_size_t)-1);
|
||||
key_len = xp_awk_longtostr (
|
||||
argc, 10, XP_NULL, key, xp_countof(key));
|
||||
xp_awk_assert (run->awk, key_len != (xp_size_t)-1);
|
||||
|
||||
/* increment reference count of v_tmp in advance as if
|
||||
* it has successfully been assigned into the ARGV map */
|
||||
xp_awk_refupval (v_tmp);
|
||||
/* increment reference count of v_tmp in advance as if
|
||||
* it has successfully been assigned into ARGV. */
|
||||
xp_awk_refupval (v_tmp);
|
||||
|
||||
if (xp_awk_map_putx (
|
||||
((xp_awk_val_map_t*)v_argv)->map,
|
||||
key, key_len, v_tmp, XP_NULL) == -1)
|
||||
{
|
||||
/* if the assignment operation fails, decrements
|
||||
* the reference of v_tmp to free it */
|
||||
xp_awk_refdownval (run, v_tmp);
|
||||
if (xp_awk_map_putx (
|
||||
((xp_awk_val_map_t*)v_argv)->map,
|
||||
key, key_len, v_tmp, XP_NULL) == -1)
|
||||
{
|
||||
/* if the assignment operation fails, decrements
|
||||
* the reference of v_tmp to free it */
|
||||
xp_awk_refdownval (run, v_tmp);
|
||||
|
||||
/* the other values previously assigned into the map
|
||||
* will be freeed when v_argv is freed */
|
||||
xp_awk_refdownval (run, v_argv);
|
||||
/* the values previously assigned into the
|
||||
* map will be freeed when v_argv is freed */
|
||||
xp_awk_refdownval (run, v_argv);
|
||||
|
||||
run->errnum = XP_AWK_ENOMEM;
|
||||
return -1;
|
||||
run->errnum = XP_AWK_ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2105,6 +2110,7 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde)
|
||||
xp_awk_refdownval (run, v);
|
||||
return -1;
|
||||
}
|
||||
|
||||
xp_awk_refdownval (run, v);
|
||||
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk.c,v 1.95 2006-10-12 04:17:58 bacon Exp $
|
||||
* $Id: awk.c,v 1.96 2006-10-13 10:18:39 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk.h>
|
||||
@ -187,7 +187,8 @@ static xp_ssize_t dump_source (
|
||||
{
|
||||
/*struct src_io* src_io = (struct src_io*)arg;*/
|
||||
|
||||
if (cmd == XP_AWK_IO_OPEN || cmd == XP_AWK_IO_CLOSE) return 0;
|
||||
if (cmd == XP_AWK_IO_OPEN) return 1;
|
||||
else if (cmd == XP_AWK_IO_CLOSE) return 0;
|
||||
else if (cmd == XP_AWK_IO_WRITE)
|
||||
{
|
||||
xp_size_t i;
|
||||
|
Loading…
Reference in New Issue
Block a user