From 81fcbf1c693216ecc42a4179fccd895332373a8c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 21 Nov 2006 15:06:51 +0000 Subject: [PATCH] *** empty log message *** --- ase/awk/Awk.java | 69 +++++++-- ase/awk/Extio.java | 56 ++++++- ase/awk/awk.h | 43 ++++-- ase/awk/extio.c | 87 +++++++++-- ase/awk/jni.c | 305 +++++++++++++++++++++++--------------- ase/awk/run.c | 16 +- ase/awk/tree.c | 7 +- ase/test/awk/Awk.java | 133 +++++++++++++---- ase/test/awk/awk.c | 18 +-- ase/test/awk/makefile.bcc | 8 +- 10 files changed, 516 insertions(+), 226 deletions(-) diff --git a/ase/awk/Awk.java b/ase/awk/Awk.java index dce8927a..2bb0fef5 100644 --- a/ase/awk/Awk.java +++ b/ase/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.3 2006-10-24 06:05:01 bacon Exp $ + * $Id: Awk.java,v 1.4 2006-11-21 15:06:14 bacon Exp $ */ package ase.awk; @@ -29,23 +29,68 @@ public abstract class Awk public native void run () throws Exception; private native void open () throws Exception; - /* - protected native void set_extio (long extio, Object obj); - protected native Object get_extio (long extio); - */ - /* abstrace methods */ protected abstract int open_source (int mode); protected abstract int close_source (int mode); protected abstract int read_source (char[] buf, int len); protected abstract int write_source (char[] buf, int len); - protected abstract int open_console (); - protected abstract int close_console (); - protected abstract int read_console (char[] buf, int len); - protected abstract int write_console (char[] buf, int len); - protected abstract int next_console (char[] buf, int len); + protected int open_extio (Extio extio) + { + int type = extio.getType (); + if (type == Extio.TYPE_CONSOLE) return open_console (extio); + if (type == Extio.TYPE_FILE) return open_file (extio); + /*if (type == Extio.TYPE_PIPE) return open_pipe (extio); + if (type == Extio.TYPE_COPROC) return open_coproc (extio);*/ + return -1; + } + + protected int close_extio (Extio extio) + { + int type = extio.getType (); + if (type == Extio.TYPE_CONSOLE) return close_console (extio); + if (type == Extio.TYPE_FILE) return close_file (extio); + /*if (type == Extio.TYPE_PIPE) return close_pipe (extio); + if (type == Extio.TYPE_COPROC) return close_coproc (extio);*/ + return -1; + } + + protected int read_extio (Extio extio, char[] buf, int len) + { + int type = extio.getType (); + if (type == Extio.TYPE_CONSOLE) + return read_console (extio, buf, len); + if (type == Extio.TYPE_FILE) + return read_file (extio, buf, len); + /*if (type == Extio.TYPE_PIPE) + * return read_pipe (extio, buf, len); + if (type == Extio.TYPE_COPROC) + return read_coproc (extio, buf, len);*/ + return -1; + } + + protected int write_extio (Extio extio, char[] buf, int len) + { + int type = extio.getType (); + if (type == Extio.TYPE_CONSOLE) + return write_console (extio, buf, len); + if (type == Extio.TYPE_FILE) + return write_file (extio, buf, len); + /*if (type == Extio.TYPE_PIPE) + * return write_pipe (extio, buf, len); + if (type == Extio.TYPE_COPROC) + return write_coproc (extio, buf, len);*/ + return -1; + } + + protected abstract int open_console (Extio extio); + protected abstract int close_console (Extio extio); + protected abstract int read_console (Extio extio, char[] buf, int len); + protected abstract int write_console (Extio extio, char[] buf, int len); + protected abstract int next_console (Extio extio, char[] buf, int len); protected abstract int open_file (Extio extio); - protected abstract int close_file (String name); + protected abstract int close_file (Extio name); + protected abstract int read_file (Extio extio, char[] buf, int len); + protected abstract int write_file (Extio extio, char[] buf, int len); } diff --git a/ase/awk/Extio.java b/ase/awk/Extio.java index 8c13b77d..6733b48f 100644 --- a/ase/awk/Extio.java +++ b/ase/awk/Extio.java @@ -2,21 +2,61 @@ package ase.awk; public class Extio { - private long extio; + public static final int TYPE_PIPE = 0; + public static final int TYPE_COPROC = 1; + public static final int TYPE_FILE = 2; + public static final int TYPE_CONSOLE = 3; - protected Extio (long extio) + public static final int MODE_PIPE_READ = 0; + public static final int MODE_PIPE_WRITE = 1; + + public static final int MODE_FILE_READ = 0; + public static final int MODE_FILE_WRITE = 1; + public static final int MODE_FILE_APPEND = 2; + + public static final int MODE_CONSOLE_READ = 0; + public static final int MODE_CONSOLE_WRITE = 1; + + private String name; + private int type; + private int mode; + private Object handle; + + protected Extio (String name, int type, int mode) { + this.name = name; + this.type = type; + this.mode = mode; + this.handle = null; } - public native String name (); - - /* - void setHandle (Object obj) + public String getName () { + return this.name; } - Object getHandle () + public int getType () { + return this.type; + } + + public int getMode () + { + return this.mode; + } + + public void setHandle (Object handle) + { + this.handle = handle; + } + + public Object getHandle () + { + return this.handle; + } + + protected void finalize () + { + System.out.println ("Extio being finalized...."); } - */ }; diff --git a/ase/awk/awk.h b/ase/awk/awk.h index 27e1431a..da5ac32a 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.147 2006-11-19 15:24:20 bacon Exp $ + * $Id: awk.h,v 1.148 2006-11-21 15:06:14 bacon Exp $ */ #ifndef _ASE_AWK_AWK_H_ @@ -26,22 +26,29 @@ typedef ase_ssize_t (*ase_awk_io_t) ( struct ase_awk_extio_t { ase_awk_run_t* run; /* [IN] */ - int type; /* [IN] console, file, coproc, pipe */ - int mode; /* [IN] read, write, etc */ + int type; /* [IN] console, file, coproc, pipe */ + int mode; /* [IN] read, write, etc */ ase_char_t* name; /* [IN] */ - void* custom_data; /* [IN] */ + void* custom_data; /* [IN] */ + void* handle; /* [OUT] */ - void* handle; /* [OUT] */ - - /* input buffer */ + /* input */ struct { ase_char_t buf[2048]; ase_size_t pos; ase_size_t len; ase_bool_t eof; + ase_bool_t eos; } in; + /* output */ + struct + { + ase_bool_t eof; + ase_bool_t eos; + } out; + ase_awk_extio_t* next; }; @@ -126,15 +133,21 @@ enum enum { - ASE_AWK_IO_PIPE_READ = 0, - ASE_AWK_IO_PIPE_WRITE = 1, + ASE_AWK_EXTIO_PIPE_READ = 0, + ASE_AWK_EXTIO_PIPE_WRITE = 1, - ASE_AWK_IO_FILE_READ = 0, - ASE_AWK_IO_FILE_WRITE = 1, - ASE_AWK_IO_FILE_APPEND = 2, + /* + ASE_AWK_EXTIO_COPROC_READ = 0, + ASE_AWK_EXTIO_COPROC_WRITE = 1, + ASE_AWK_EXTIO_COPROC_RDWR = 2, + */ - ASE_AWK_IO_CONSOLE_READ = 0, - ASE_AWK_IO_CONSOLE_WRITE = 1 + ASE_AWK_EXTIO_FILE_READ = 0, + ASE_AWK_EXTIO_FILE_WRITE = 1, + ASE_AWK_EXTIO_FILE_APPEND = 2, + + ASE_AWK_EXTIO_CONSOLE_READ = 0, + ASE_AWK_EXTIO_CONSOLE_WRITE = 1 }; /* various options */ @@ -311,7 +324,7 @@ enum }; /* extio types */ -enum +enum ase_awk_extio_type_t { /* extio types available */ ASE_AWK_EXTIO_PIPE, diff --git a/ase/awk/extio.c b/ase/awk/extio.c index 2cd95f4a..79236ed9 100644 --- a/ase/awk/extio.c +++ b/ase/awk/extio.c @@ -1,5 +1,5 @@ /* - * $Id: extio.c,v 1.60 2006-11-17 07:26:15 bacon Exp $ + * $Id: extio.c,v 1.61 2006-11-21 15:06:14 bacon Exp $ */ #include @@ -27,10 +27,10 @@ static int __in_mode_map[] = { /* the order should match the order of the * ASE_AWK_IN_XXX values in tree.h */ - ASE_AWK_IO_PIPE_READ, + ASE_AWK_EXTIO_PIPE_READ, 0, - ASE_AWK_IO_FILE_READ, - ASE_AWK_IO_CONSOLE_READ + ASE_AWK_EXTIO_FILE_READ, + ASE_AWK_EXTIO_CONSOLE_READ }; static int __in_mask_map[] = @@ -56,11 +56,11 @@ static int __out_mode_map[] = { /* the order should match the order of the * ASE_AWK_OUT_XXX values in tree.h */ - ASE_AWK_IO_PIPE_WRITE, + ASE_AWK_EXTIO_PIPE_WRITE, 0, - ASE_AWK_IO_FILE_WRITE, - ASE_AWK_IO_FILE_APPEND, - ASE_AWK_IO_CONSOLE_WRITE + ASE_AWK_EXTIO_FILE_WRITE, + ASE_AWK_EXTIO_FILE_APPEND, + ASE_AWK_EXTIO_CONSOLE_WRITE }; static int __out_mask_map[] = @@ -140,6 +140,7 @@ int ase_awk_readextio ( p->in.pos = 0; p->in.len = 0; p->in.eof = ase_false; + p->in.eos = ase_false; n = handler (ASE_AWK_IO_OPEN, p, ASE_NULL, 0); if (n == -1) @@ -165,7 +166,17 @@ int ase_awk_readextio ( * 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; + if (n == 0) + { + p->in.eos = ase_true; + return 0; + } + } + + if (p->in.eos) + { + /* no more streams. */ + return 0; } /* ready to read a line */ @@ -323,7 +334,8 @@ int ase_awk_readextio ( else line_len = line_len + 1; } - if (rs_ptr != ASE_NULL && rs->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, rs_ptr); + if (rs_ptr != ASE_NULL && + rs->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, rs_ptr); ase_awk_refdownval (run, rs); /* increment NR */ @@ -460,6 +472,9 @@ int ase_awk_writeextio_str ( p->next = ASE_NULL; p->custom_data = run->extio.custom_data; + p->out.eof = ase_false; + p->out.eos = ase_false; + n = handler (ASE_AWK_IO_OPEN, p, ASE_NULL, 0); if (n == -1) { @@ -484,11 +499,27 @@ int ase_awk_writeextio_str ( * 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; + if (n == 0) + { + p->out.eos = ase_true; + return 0; + } } -/* TODO: if write handler returns less than the request, loop */ - if (len > 0) + if (p->out.eos) + { + /* no more streams */ + return 0; + } + + if (p->out.eof) + { + /* it has reached the end of the stream but this function + * has been recalled */ + return 0; + } + + while (len > 0) { n = handler (ASE_AWK_IO_WRITE, p, str, len); @@ -503,13 +534,19 @@ int ase_awk_writeextio_str ( return -1; } - if (n == 0) return 0; + if (n == 0) + { + p->out.eof = ase_true; + return 0; + } + + len -= n; + str += n; } return 1; } - int ase_awk_flushextio ( ase_awk_run_t* run, int out_type, const ase_char_t* name) { @@ -615,6 +652,12 @@ int ase_awk_nextextio_read ( return -1; } + if (p->in.eos) + { + /* no more streams. */ + return 0; + } + n = handler (ASE_AWK_IO_NEXT, p, ASE_NULL, 0); if (n == -1) { @@ -623,6 +666,20 @@ int ase_awk_nextextio_read ( return -1; } + if (n == 0) + { + /* the next stream cannot be opened. + * set the eos flags so that the next call to nextextio_read + * will return 0 without executing the handler */ + p->in.eos = ase_true; + } + else + { + /* as the next stream has been opened successfully, + * the eof flag should be cleared if set */ + p->in.eof = ase_false; + } + return n; } diff --git a/ase/awk/jni.c b/ase/awk/jni.c index 39b3c61a..3b718dff 100644 --- a/ase/awk/jni.c +++ b/ase/awk/jni.c @@ -1,5 +1,5 @@ /* - * $Id: jni.c,v 1.17 2006-11-19 15:33:39 bacon Exp $ + * $Id: jni.c,v 1.18 2006-11-21 15:06:15 bacon Exp $ */ #include @@ -10,13 +10,18 @@ #include #include #include +#include #ifdef _WIN32 #include #include #endif -#define EXCEPTION_AWK "ase/awk/AwkException" +#ifndef ASE_CHAR_IS_WCHAR + #error this module supports ASE_CHAR_IS_WCHAR only +#endif + +#define EXCEPTION_AWK "ase/awk/Exception" #define FIELD_AWK "__awk" enum @@ -31,9 +36,7 @@ static ase_ssize_t __read_source ( int cmd, void* arg, ase_char_t* data, ase_size_t count); static ase_ssize_t __write_source ( int cmd, void* arg, ase_char_t* data, ase_size_t count); -static ase_ssize_t __process_extio_console ( - int cmd, void* arg, ase_char_t* data, ase_size_t count); -static ase_ssize_t __process_extio_file ( +static ase_ssize_t __process_extio ( int cmd, void* arg, ase_char_t* data, ase_size_t count); typedef struct srcio_data_t srcio_data_t; @@ -66,6 +69,11 @@ static void __awk_free (void* ptr, void* custom_data) free (ptr); } +static ase_real_t __awk_pow (ase_real_t x, ase_real_t y) +{ + return pow (x, y); +} + static int __awk_sprintf ( ase_char_t* buf, ase_size_t len, const ase_char_t* fmt, ...) { @@ -166,6 +174,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv* env, jobject obj) syscas.memcpy = memcpy; syscas.memset = memset; + syscas.pow = __awk_pow; syscas.sprintf = __awk_sprintf; syscas.aprintf = __awk_aprintf; syscas.dprintf = __awk_dprintf; @@ -187,7 +196,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv* env, jobject obj) opt = ASE_AWK_EXPLICIT | ASE_AWK_UNIQUE | ASE_AWK_DBLSLASHES | ASE_AWK_SHADING | ASE_AWK_IMPLICIT | ASE_AWK_SHIFT | - ASE_AWK_EXTIO | ASE_AWK_BLOCKLESS; + ASE_AWK_EXTIO | ASE_AWK_BLOCKLESS | ASE_AWK_HASHSIGN; ase_awk_setopt (awk, opt); printf ("__awk(native) done => %u, 0x%X\n", awk, awk); @@ -266,15 +275,22 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj) runios.pipe = ASE_NULL; runios.coproc = ASE_NULL; - runios.file = __process_extio_file; - runios.console = __process_extio_console; + runios.file = __process_extio; + runios.console = __process_extio; runios.custom_data = &runio_data; if (ase_awk_run (awk, ASE_NULL, &runios, ASE_NULL, ASE_NULL) == -1) { + char msg[256]; + int n; + except = (*env)->FindClass (env, EXCEPTION_AWK); if (except == 0) return; - (*env)->ThrowNew (env, except, "Run Error ..."); + + snprintf (msg, sizeof(msg), "%S", + ase_awk_geterrstr(ase_awk_geterrnum(awk))); + if (n < 0 || n >= sizeof(msg)) msg[sizeof(msg)-1] = '\0'; + (*env)->ThrowNew (env, except, msg); return; } } @@ -303,7 +319,11 @@ static ase_ssize_t __call_java_open_source (JNIEnv* env, jobject obj, int mode) class = (*env)->GetObjectClass(env, obj); mid = (*env)->GetMethodID (env, class, "open_source", "(I)I"); - if (mid == 0) return -1; + if (mid == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } ret = (*env)->CallIntMethod (env, obj, mid, mode); thrown = (*env)->ExceptionOccurred (env); @@ -313,6 +333,7 @@ static ase_ssize_t __call_java_open_source (JNIEnv* env, jobject obj, int mode) ret = -1; } + (*env)->DeleteLocalRef (env, class); return ret; } @@ -326,7 +347,11 @@ static ase_ssize_t __call_java_close_source (JNIEnv* env, jobject obj, int mode) class = (*env)->GetObjectClass(env, obj); mid = (*env)->GetMethodID (env, class, "close_source", "(I)I"); - if (mid == 0) return -1; + if (mid == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } ret = (*env)->CallIntMethod (env, obj, mid, mode); thrown = (*env)->ExceptionOccurred (env); @@ -336,6 +361,7 @@ static ase_ssize_t __call_java_close_source (JNIEnv* env, jobject obj, int mode) ret = -1; } + (*env)->DeleteLocalRef (env, class); return ret; } @@ -352,10 +378,18 @@ static ase_ssize_t __call_java_read_source ( class = (*env)->GetObjectClass(env, obj); mid = (*env)->GetMethodID (env, class, "read_source", "([CI)I"); - if (mid == 0) return -1; + if (mid == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } array = (*env)->NewCharArray (env, size); - if (array == NULL) return -1; + if (array == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } ret = (*env)->CallIntMethod (env, obj, mid, array, size); thrown = (*env)->ExceptionOccurred (env); @@ -370,6 +404,7 @@ static ase_ssize_t __call_java_read_source ( (*env)->ReleaseCharArrayElements (env, array, tmp, 0); (*env)->DeleteLocalRef (env, array); + (*env)->DeleteLocalRef (env, class); return i; } @@ -386,10 +421,18 @@ static ase_ssize_t __call_java_write_source ( class = (*env)->GetObjectClass(env, obj); mid = (*env)->GetMethodID (env, class, "write_source", "([CI)I"); - if (mid == 0) return -1; + if (mid == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } array = (*env)->NewCharArray (env, size); - if (array == NULL) return -1; + if (array == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } tmp = (*env)->GetCharArrayElements (env, array, 0); for (i = 0; i < size; i++) tmp[i] = (jchar)buf[i]; @@ -404,6 +447,7 @@ static ase_ssize_t __call_java_write_source ( } (*env)->DeleteLocalRef (env, array); + (*env)->DeleteLocalRef (env, class); return ret; } @@ -413,63 +457,84 @@ static ase_ssize_t __call_java_open_extio ( jclass class; jmethodID mid; jthrowable thrown; + jclass extio_class; + jmethodID extio_cons; + jobject extio_object; + jstring extio_name; jint ret; class = (*env)->GetObjectClass(env, obj); - if (extio == ASE_NULL) + extio_class = (*env)->FindClass (env, "ase/awk/Extio"); + if (extio_class == NULL) { - mid = (*env)->GetMethodID (env, class, meth, "()I"); - if (mid == 0) return -1; - - ret = (*env)->CallIntMethod (env, obj, mid); - } - else - { - /* - jstring name_str; - - mid = (*env)->GetMethodID ( - env, class, meth, "(Ljava/lang/String;)I"); - if (mid == 0) return -1; - - name_str = (*env)->NewString ( - env, extio->name, ase_awk_strlen(extio->name)); - if (name_str == 0) return -1; - - ret = (*env)->CallIntMethod (env, obj, mid, name_str); - - (*env)->DeleteLocalRef (env, name_str); - */ - jclass extio_class; - jmethodID extio_cons; - jobject extio_object; - - extio_class = (*env)->FindClass (env, "ase/awk/Extio"); - if (extio_class == NULL) return -1; - - extio_cons = (*env)->GetMethodID ( - env, extio_class, "", "(J)V"); - if (extio_cons == NULL) return -1; - - mid = (*env)->GetMethodID ( - env, class, meth, "(Lase/awk/Extio;)I"); - if (mid == NULL) return -1; - - extio_object = (*env)->NewObject ( - env, extio_class, extio_cons, (jlong)extio); - if (extio_object == NULL) return -1; - - ret = (*env)->CallIntMethod (env, obj, mid, extio_object); + (*env)->DeleteLocalRef (env, class); + return -1; } + /* get the constructor */ + extio_cons = (*env)->GetMethodID ( + env, extio_class, "", "(Ljava/lang/String;II)V"); + if (extio_cons == NULL) + { + (*env)->DeleteLocalRef (env, extio_class); + (*env)->DeleteLocalRef (env, class); + return -1; + } + + /* get the method - meth */ + mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;)I"); + if (mid == NULL) + { + (*env)->DeleteLocalRef (env, extio_class); + (*env)->DeleteLocalRef (env, class); + return -1; + } + + /* construct the name */ + extio_name = (*env)->NewString ( + env, extio->name, ase_awk_strlen(extio->name)); + if (extio_name == NULL) + { + (*env)->DeleteLocalRef (env, extio_class); + (*env)->DeleteLocalRef (env, class); + return -1; + } + + /* construct the extio object */ + extio_object = (*env)->NewObject ( + env, extio_class, extio_cons, + extio_name, extio->type & 0xFF, extio->mode); + if (extio_object == NULL) + { + (*env)->DeleteLocalRef (env, extio_name); + (*env)->DeleteLocalRef (env, extio_class); + (*env)->DeleteLocalRef (env, class); + return -1; + } + + /* execute the method */ + ret = (*env)->CallIntMethod (env, obj, mid, extio_object); thrown = (*env)->ExceptionOccurred (env); if (thrown) { +(*env)->ExceptionDescribe (env); (*env)->ExceptionClear (env); - ret = -1; + return -1; } + if (ret != -1) + { + /* ret == -1 failed to open the stream + * ret == 0 opened the stream and reached its end + * ret == 1 opened the stream. */ + extio->handle = (*env)->NewGlobalRef (env, extio_object); + } + + (*env)->DeleteLocalRef (env, extio_object); + (*env)->DeleteLocalRef (env, extio_name); + (*env)->DeleteLocalRef (env, extio_class); + (*env)->DeleteLocalRef (env, class); return ret; } @@ -483,41 +548,38 @@ static ase_ssize_t __call_java_close_extio ( class = (*env)->GetObjectClass(env, obj); - if (extio == ASE_NULL) + mid = (*env)->GetMethodID ( + env, class, meth, "(Lase/awk/Extio;)I"); + if (mid == NULL) { - mid = (*env)->GetMethodID (env, class, meth, "()I"); - if (mid == 0) return -1; - - ret = (*env)->CallIntMethod (env, obj, mid); - } - else - { - jstring name_str; - - mid = (*env)->GetMethodID ( - env, class, meth, "(Ljava/lang/String;)I"); - if (mid == 0) return -1; - - name_str = (*env)->NewString ( - env, extio->name, ase_awk_strlen(extio->name)); - if (name_str == 0) return -1; - - ret = (*env)->CallIntMethod (env, obj, mid, name_str); - (*env)->DeleteLocalRef (env, name_str); + (*env)->DeleteLocalRef (env, class); + return -1; } + ret = (*env)->CallIntMethod (env, obj, mid, extio->handle); thrown = (*env)->ExceptionOccurred (env); if (thrown) { +(*env)->ExceptionDescribe (env); (*env)->ExceptionClear (env); ret = -1; } + if (ret != -1) + { + /* ret == -1 failed to close the stream + * ret == 0 closed the stream */ + (*env)->DeleteGlobalRef (env, extio->handle); + extio->handle = NULL; + } + + (*env)->DeleteLocalRef (env, class); return ret; } static ase_ssize_t __call_java_read_extio ( - JNIEnv* env, jobject obj, char* meth, ase_char_t* buf, ase_size_t size) + JNIEnv* env, jobject obj, char* meth, + ase_awk_extio_t* extio, ase_char_t* buf, ase_size_t size) { jclass class; jmethodID mid; @@ -528,13 +590,21 @@ static ase_ssize_t __call_java_read_extio ( class = (*env)->GetObjectClass(env, obj); - mid = (*env)->GetMethodID (env, class, meth, "([CI)I"); - if (mid == 0) return -1; + mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;[CI)I"); + if (mid == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } array = (*env)->NewCharArray (env, size); - if (array == NULL) return -1; + if (array == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } - ret = (*env)->CallIntMethod (env, obj, mid, array, size); + ret = (*env)->CallIntMethod (env, obj, mid, extio->handle, array, size); thrown = (*env)->ExceptionOccurred (env); if (thrown) { @@ -542,16 +612,21 @@ static ase_ssize_t __call_java_read_extio ( ret = -1; } - tmp = (*env)->GetCharArrayElements (env, array, 0); - for (i = 0; i < ret; i++) buf[i] = (ase_char_t)tmp[i]; - (*env)->ReleaseCharArrayElements (env, array, tmp, 0); + if (ret > 0) + { + tmp = (*env)->GetCharArrayElements (env, array, 0); + for (i = 0; i < ret; i++) buf[i] = (ase_char_t)tmp[i]; + (*env)->ReleaseCharArrayElements (env, array, tmp, 0); + } (*env)->DeleteLocalRef (env, array); + (*env)->DeleteLocalRef (env, class); return ret; } static ase_ssize_t __call_java_write_extio ( - JNIEnv* env, jobject obj, char* meth, ase_char_t* data, ase_size_t size) + JNIEnv* env, jobject obj, char* meth, + ase_awk_extio_t* extio, ase_char_t* data, ase_size_t size) { jclass class; jmethodID mid; @@ -563,17 +638,25 @@ static ase_ssize_t __call_java_write_extio ( class = (*env)->GetObjectClass(env, obj); - mid = (*env)->GetMethodID (env, class, meth, "([CI)I"); - if (mid == 0) return -1; + mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;[CI)I"); + if (mid == NULL) + { + (*env)->DeleteLocalRef (env, class); + return -1; + } array = (*env)->NewCharArray (env, size); - if (array == NULL) return -1; + if (array == NULL) + { + (*env)->DeleteLocalRef (env, class); + 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); + ret = (*env)->CallIntMethod (env, obj, mid, extio->handle, array, size); thrown = (*env)->ExceptionOccurred (env); if (thrown) { @@ -582,6 +665,7 @@ static ase_ssize_t __call_java_write_extio ( } (*env)->DeleteLocalRef (env, array); + (*env)->DeleteLocalRef (env, class); return ret; } @@ -633,7 +717,7 @@ static ase_ssize_t __write_source ( return -1; } -static ase_ssize_t __process_extio_console ( +static ase_ssize_t __process_extio ( int cmd, void* arg, ase_char_t* data, ase_size_t size) { ase_awk_extio_t* epa = (ase_awk_extio_t*)arg; @@ -643,26 +727,25 @@ static ase_ssize_t __process_extio_console ( { return __call_java_open_extio ( runio_data->env, runio_data->obj, - "open_console", ASE_NULL); + "open_extio", epa); } else if (cmd == ASE_AWK_IO_CLOSE) { return __call_java_close_extio ( runio_data->env, runio_data->obj, - "close_console", ASE_NULL); + "close_extio", epa); } - else if (cmd == ASE_AWK_IO_READ) { return __call_java_read_extio ( - runio_data->env, runio_data->obj, "read_console", - data, size); + runio_data->env, runio_data->obj, + "read_extio", epa, data, size); } else if (cmd == ASE_AWK_IO_WRITE) { return __call_java_write_extio ( - runio_data->env, runio_data->obj, "write_console", - data, size); + runio_data->env, runio_data->obj, + "write_extio", epa, data, size); } #if 0 else if (cmd == ASE_AWK_IO_FLUSH) @@ -681,25 +764,3 @@ static ase_ssize_t __process_extio_console ( return -1; } - -static ase_ssize_t __process_extio_file ( - int cmd, void* arg, ase_char_t* data, ase_size_t size) -{ - ase_awk_extio_t* epa = (ase_awk_extio_t*)arg; - runio_data_t* runio_data = (runio_data_t*)epa->custom_data; - - if (cmd == ASE_AWK_IO_OPEN) - { - return __call_java_open_extio ( - runio_data->env, runio_data->obj, - "open_file", epa); - } - else if (cmd == ASE_AWK_IO_CLOSE) - { - return __call_java_close_extio ( - runio_data->env, runio_data->obj, - "close_file", epa); - } - - return -1; -} diff --git a/ase/awk/run.c b/ase/awk/run.c index 02553897..a4329390 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.279 2006-11-20 14:29:33 bacon Exp $ + * $Id: run.c,v 1.280 2006-11-21 15:06:15 bacon Exp $ */ #include @@ -562,6 +562,7 @@ int ase_awk_run (ase_awk_t* awk, n = 0; } + __deinit_run (run); __del_run (awk, run); ASE_AWK_FREE (awk, run); @@ -758,7 +759,6 @@ static int __init_run (ase_awk_run_t* run, ase_awk_runios_t* runios, int* errnum static void __deinit_run (ase_awk_run_t* run) { - if (run->pattern_range_state != ASE_NULL) ASE_AWK_FREE (run->awk, run->pattern_range_state); @@ -1281,8 +1281,8 @@ static int __run_pattern_blocks (ase_awk_run_t* run) int saved = run->errnum; /* don't care about the result of input close */ - ase_awk_closeextio_read ( - run, ASE_AWK_IN_CONSOLE, ASE_T("")); +// ase_awk_closeextio_read ( +// run, ASE_AWK_IN_CONSOLE, ASE_T("")); run->errnum = saved; return -1; @@ -1300,8 +1300,8 @@ static int __run_pattern_blocks (ase_awk_run_t* run) { int saved = run->errnum; - ase_awk_closeextio_read ( - run, ASE_AWK_IN_CONSOLE, ASE_T("")); +// ase_awk_closeextio_read ( +// run, ASE_AWK_IN_CONSOLE, ASE_T("")); run->errnum = saved; return -1; @@ -1318,6 +1318,7 @@ static int __run_pattern_blocks (ase_awk_run_t* run) * -1 regardless of the value of errnum. */ if (need_to_close) { +/* TODO: do i have to close exito here... n = ase_awk_closeextio_read ( run, ASE_AWK_IN_CONSOLE, ASE_T("")); if (n == -1) @@ -1326,6 +1327,7 @@ static int __run_pattern_blocks (ase_awk_run_t* run) PANIC_I (run, ASE_AWK_ECONINCLOSE); else return -1; } +*/ } return 0; @@ -6087,7 +6089,7 @@ ase_char_t* ase_awk_format ( } - if (prec == -1 || prec == 0 || prec > ch_len ) prec = ch_len; + if (prec == -1 || prec == 0 || prec > ch_len) prec = ch_len; if (prec > width) width = prec; if (!minus) diff --git a/ase/awk/tree.c b/ase/awk/tree.c index 9e83053e..50d902cf 100644 --- a/ase/awk/tree.c +++ b/ase/awk/tree.c @@ -1,5 +1,5 @@ /* - * $Id: tree.c,v 1.89 2006-11-19 11:21:06 bacon Exp $ + * $Id: tree.c,v 1.90 2006-11-21 15:06:15 bacon Exp $ */ #include @@ -282,7 +282,7 @@ static int __print_expression (ase_awk_t* awk, ase_awk_nde_t* nde) len = ((ase_awk_nde_str_t*)nde)->len; for (i = 0; i < len; i++) { - /* TODO: more deescaping */ + /* TODO: maybe more de-escaping?? */ if (ptr[i] == ASE_T('\n')) PUT_SRCSTR (awk, ASE_T("\\n")); else if (ptr[i] == ASE_T('\r')) @@ -304,7 +304,6 @@ static int __print_expression (ase_awk_t* awk, ase_awk_nde_t* nde) case ASE_AWK_NDE_REX: { - /* TODO: buf, len */ PUT_SRCSTR (awk, ASE_T("/")); PUT_SRCSTRX (awk, ((ase_awk_nde_rex_t*)nde)->buf, @@ -489,7 +488,6 @@ static int __print_expression (ase_awk_t* awk, ase_awk_nde_t* nde) case ASE_AWK_NDE_AFN: { - /* TODO: use px->what.afn.name_len */ ase_awk_nde_call_t* px = (ase_awk_nde_call_t*)nde; PUT_SRCSTRX (awk, px->what.afn.name, px->what.afn.name_len); @@ -795,7 +793,6 @@ static int __print_statements (ase_awk_t* awk, ase_awk_nde_t* tree, int depth) { PRINT_TABS (awk, depth); PUT_SRCSTR (awk, ASE_T("delete ")); - /* TODO: can't use __print_expression??? */ ase_awk_prnpt (awk, ((ase_awk_nde_delete_t*)p)->var); break; } diff --git a/ase/test/awk/Awk.java b/ase/test/awk/Awk.java index a7e18005..8ccec86a 100644 --- a/ase/test/awk/Awk.java +++ b/ase/test/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.1 2006-10-24 06:03:14 bacon Exp $ + * $Id: Awk.java,v 1.2 2006-11-21 15:06:50 bacon Exp $ */ package ase.test.awk; @@ -11,6 +11,8 @@ public class Awk extends ase.awk.Awk private FileReader insrc; private FileWriter outsrc; + private InputStreamReader console_in = null; + public Awk () throws ase.awk.Exception { super (); @@ -20,13 +22,13 @@ public class Awk extends ase.awk.Awk { if (mode == SOURCE_READ) { - try { insrc = new FileReader ("test.awk"); } + try { insrc = new FileReader ("t.awk"); } catch (IOException e) { return -1; } return 1; } else if (mode == SOURCE_WRITE) { - try { outsrc = new FileWriter ("test.out"); } + try { outsrc = new FileWriter ("t.out"); } catch (IOException e) { return -1; } return 1; } @@ -65,39 +67,107 @@ public class Awk extends ase.awk.Awk return len; } - protected int open_console () + protected int open_console (ase.awk.Extio extio) { - System.err.println ("[open_console called....]"); - return 1; + System.err.println ("[open_console called.... name: " + extio.getName() + " mode: " + extio.getMode()); + + int mode = extio.getMode (); + + if (mode == ase.awk.Extio.MODE_CONSOLE_READ) + { + InputStreamReader isr = + new InputStreamReader (System.in); + extio.setHandle (isr); + return 1; + } + else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) + { + OutputStreamWriter osw = + new OutputStreamWriter (System.out); + extio.setHandle (osw); + return 1; + } + + return -1; } - protected int close_console () + protected int close_console (ase.awk.Extio extio) { - System.err.println ("[close_console called....]"); - return 1; + System.err.println ("[close_console called.... name: " + extio.getName() + " mode: " + extio.getMode()); + + int mode = extio.getMode (); + + if (mode == ase.awk.Extio.MODE_CONSOLE_READ) + { + InputStreamReader isr = (InputStreamReader)extio.getHandle (); + try { isr.close (); } + catch (IOException e) { return -1; } + return 0; + } + else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) + { + OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle (); + //try { osw.close (); } + //catch (IOException e) { return -1; } + return 0; + } + + return -1; } - protected int read_console (char[] buf, int len) + protected int read_console (ase.awk.Extio extio, char[] buf, int len) { - return 0; + int mode = extio.getMode (); + + if (mode == ase.awk.Extio.MODE_CONSOLE_READ) + { + InputStreamReader isr = (InputStreamReader)extio.getHandle (); + try + { + len = isr.read (buf, 0, len); + if (len == -1) len = 0; + } + catch (IOException e) { System.out.println ("EXCEPTIN---"+e.getMessage());return -1; } + + return len; + } + else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) + { + return -1; + } + + return -1; } - protected int write_console (char[] buf, int len) + protected int write_console (ase.awk.Extio extio, char[] buf, int len) { - System.out.print (new String (buf, 0, len)); - return len; + int mode = extio.getMode (); + + if (mode == ase.awk.Extio.MODE_CONSOLE_READ) + { + return -1; + } + else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) + { + OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle (); + try { osw.write (buf, 0, len); osw.flush (); } + catch (IOException e) { return -1; } + + return len; + } + return -1; } - protected int next_console (char[] buf, int len) + protected int next_console (ase.awk.Extio extio, char[] buf, int len) { return 0; } public int open_file (ase.awk.Extio extio) { - System.out.print ("opening file ["); - //System.out.print (extio.name()); - System.out.println ("]"); + /*System.out.print ("opening file ["); + System.out.print (extio.getName()); + System.out.println ("]");*/ /* FileInputStream f = new FileInputStream (extio.name()); @@ -106,24 +176,23 @@ public class Awk extends ase.awk.Awk return 1; } - /* - public int open_file (String name) + public int close_file (ase.awk.Extio extio) { - System.out.print ("opening file ["); - System.out.print (name); - System.out.println ("]"); - return 1; - } - */ - - public int close_file (String name) - { - System.out.print ("closing file ["); - System.out.print (name); - System.out.println ("]"); + /*System.out.print ("closing file ["); + System.out.print (extio.getName()); + System.out.println ("]");*/ return 0; } + protected int read_file (ase.awk.Extio extio, char[] buf, int len) + { + return -1; + } + protected int write_file (ase.awk.Extio extio, char[] buf, int len) + { + return -1; + } + public static void main (String[] args) { Awk awk = null; diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index ea684ded..0849e676 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.120 2006-11-19 15:33:40 bacon Exp $ + * $Id: awk.c,v 1.121 2006-11-21 15:06:51 bacon Exp $ */ #include @@ -271,9 +271,9 @@ static ase_ssize_t process_extio_pipe ( FILE* handle; const ase_char_t* mode; - if (epa->mode == ASE_AWK_IO_PIPE_READ) + if (epa->mode == ASE_AWK_EXTIO_PIPE_READ) mode = ASE_T("r"); - else if (epa->mode == ASE_AWK_IO_PIPE_WRITE) + else if (epa->mode == ASE_AWK_EXTIO_PIPE_WRITE) mode = ASE_T("w"); else return -1; /* TODO: any way to set the error number? */ __awk_dprintf (ASE_T("opending %s of type %d (pipe)\n"), epa->name, epa->type); @@ -311,7 +311,7 @@ static ase_ssize_t process_extio_pipe ( case ASE_AWK_IO_FLUSH: { - if (epa->mode == ASE_AWK_IO_PIPE_READ) return -1; + if (epa->mode == ASE_AWK_EXTIO_PIPE_READ) return -1; else return 0; } @@ -336,11 +336,11 @@ static ase_ssize_t process_extio_file ( FILE* handle; const ase_char_t* mode; - if (epa->mode == ASE_AWK_IO_FILE_READ) + if (epa->mode == ASE_AWK_EXTIO_FILE_READ) mode = ASE_T("r"); - else if (epa->mode == ASE_AWK_IO_FILE_WRITE) + else if (epa->mode == ASE_AWK_EXTIO_FILE_WRITE) mode = ASE_T("w"); - else if (epa->mode == ASE_AWK_IO_FILE_APPEND) + else if (epa->mode == ASE_AWK_EXTIO_FILE_APPEND) mode = ASE_T("a"); else return -1; /* TODO: any way to set the error number? */ @@ -506,7 +506,7 @@ static int open_extio_console (ase_awk_extio_t* epa) __awk_dprintf (ASE_T("opening console[%s] of type %x\n"), epa->name, epa->type); - if (epa->mode == ASE_AWK_IO_CONSOLE_READ) + if (epa->mode == ASE_AWK_EXTIO_CONSOLE_READ) { if (infiles[infile_no] == ASE_NULL) { @@ -546,7 +546,7 @@ static int open_extio_console (ase_awk_extio_t* epa) infile_no++; return 1; } - else if (epa->mode == ASE_AWK_IO_CONSOLE_WRITE) + else if (epa->mode == ASE_AWK_EXTIO_CONSOLE_WRITE) { __awk_dprintf (ASE_T(" console(w) - \n")); /* TODO: does output console has a name??? */ diff --git a/ase/test/awk/makefile.bcc b/ase/test/awk/makefile.bcc index 80a9d135..80ec51ab 100644 --- a/ase/test/awk/makefile.bcc +++ b/ase/test/awk/makefile.bcc @@ -19,8 +19,14 @@ all: awk awk: awk.obj $(LD) $(LDFLAGS) $(STARTUP) awk.obj,$@.exe,,$(LIBS),, +java: + javac -classpath ../../.. Awk.java + +jrun: + java -Xms1m -Xmx2m -classpath ../../.. ase.test.awk.Awk + clean: - del $(OBJS) *.obj $(OUT) + del $(OBJS) *.obj *.class awk.exe .SUFFIXES: .c .obj .c.obj: