diff --git a/ase/awk/Awk.java b/ase/awk/Awk.java index 3f526184..28209ad4 100644 --- a/ase/awk/Awk.java +++ b/ase/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.8 2006-11-24 13:20:48 bacon Exp $ + * $Id: Awk.java,v 1.9 2006-11-24 15:04:23 bacon Exp $ */ package ase.awk; @@ -58,8 +58,8 @@ public abstract class Awk 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);*/ + if (type == Extio.TYPE_PIPE) return open_pipe (extio); + //if (type == Extio.TYPE_COPROC) return open_coproc (extio); return -1; } @@ -68,8 +68,8 @@ public abstract class Awk 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);*/ + if (type == Extio.TYPE_PIPE) return close_pipe (extio); + //if (type == Extio.TYPE_COPROC) return close_coproc (extio); return -1; } @@ -86,10 +86,10 @@ public abstract class Awk 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);*/ + if (type == Extio.TYPE_PIPE) + return read_pipe (extio, buf, len); + //if (type == Extio.TYPE_COPROC) + // return read_coproc (extio, buf, len); return -1; } @@ -102,10 +102,20 @@ public abstract class Awk 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);*/ + 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 int flush_extio (Extio extio) + { + int type = extio.getType (); + if (type == Extio.TYPE_CONSOLE) return flush_console (extio); + if (type == Extio.TYPE_FILE) return flush_file (extio); + if (type == Extio.TYPE_PIPE) return flush_pipe (extio); + //if (type == Extio.TYPE_COPROC) return flush_coproc (extio); return -1; } @@ -120,10 +130,18 @@ public abstract class Awk 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 flush_console (Extio extio); protected abstract int next_console (Extio extio); protected abstract int open_file (Extio extio); - protected abstract int close_file (Extio name); + protected abstract int close_file (Extio extio); protected abstract int read_file (Extio extio, char[] buf, int len); protected abstract int write_file (Extio extio, char[] buf, int len); + protected abstract int flush_file (Extio extio); + + protected abstract int open_pipe (Extio extio); + protected abstract int close_pipe (Extio extio); + protected abstract int read_pipe (Extio extio, char[] buf, int len); + protected abstract int write_pipe (Extio extio, char[] buf, int len); + protected abstract int flush_pipe (Extio extio); } diff --git a/ase/awk/StdAwk.java b/ase/awk/StdAwk.java index fa833c97..005f1746 100644 --- a/ase/awk/StdAwk.java +++ b/ase/awk/StdAwk.java @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.java,v 1.1 2006-11-24 13:20:48 bacon Exp $ + * $Id: StdAwk.java,v 1.2 2006-11-24 15:04:23 bacon Exp $ */ package ase.awk; @@ -135,7 +135,8 @@ public abstract class StdAwk extends Awk if (mode == Extio.MODE_CONSOLE_WRITE) { - OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle (); + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle (); // as the write operation below doesn't indicate // if it has reached the end, console can't be // switched here unlike read_console. @@ -148,6 +149,22 @@ public abstract class StdAwk extends Awk return -1; } + protected int flush_console (Extio extio) + { + int mode = extio.getMode (); + + if (mode == Extio.MODE_CONSOLE_WRITE) + { + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle (); + try { osw.flush (); } + catch (IOException e) { return -1; } + return 0; + } + + return -1; + } + protected int next_console (Extio extio) { int mode = extio.getMode (); @@ -348,4 +365,131 @@ public abstract class StdAwk extends Awk return -1; } + + protected int flush_file (Extio extio) + { + int mode = extio.getMode (); + + if (mode == Extio.MODE_FILE_WRITE || + mode == Extio.MODE_FILE_APPEND) + { + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle (); + try { osw.flush (); } + catch (IOException e) { return -1; } + return 0; + } + + return -1; + } + + /* ===== pipe ===== */ + public int open_pipe (Extio extio) + { + int mode = extio.getMode(); + + if (mode == Extio.MODE_PIPE_READ) + { + + Process proc; + InputStreamReader isr; + + try { proc = Runtime.getRuntime().exec (extio.getName()); } + catch (IOException e) { return -1; } + isr = new InputStreamReader (proc.getInputStream()); + extio.setHandle (isr); + return 1; + } + else if (mode == Extio.MODE_PIPE_WRITE) + { + Process proc; + OutputStreamWriter osw; + + try { proc = Runtime.getRuntime().exec (extio.getName()); } + catch (IOException e) { return -1; } + osw = new OutputStreamWriter (proc.getOutputStream()); + extio.setHandle (osw); + return 1; + } + + return -1; + } + + public int close_pipe (Extio extio) + { + int mode = extio.getMode(); + + if (mode == Extio.MODE_PIPE_READ) + { + InputStreamReader isr; + isr = (InputStreamReader)extio.getHandle(); + try { isr.close (); } + catch (IOException e) { return -1; } + return 0; + } + else if (mode == Extio.MODE_PIPE_WRITE) + { + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle(); + try { osw.close (); } + catch (IOException e) { return -1; } + return 0; + } + + return -1; + } + + protected int read_pipe (Extio extio, char[] buf, int len) + { + int mode = extio.getMode(); + + if (mode == Extio.MODE_PIPE_READ) + { + InputStreamReader isr; + isr = (InputStreamReader)extio.getHandle(); + + try + { + len = isr.read (buf, 0, len); + if (len == -1) len = 0; + } + catch (IOException e) { len = -1; } + return len; + } + + return -1; + } + + protected int write_pipe (Extio extio, char[] buf, int len) + { + int mode = extio.getMode(); + + if (mode == Extio.MODE_PIPE_WRITE) + { + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle(); + try { osw.write (buf, 0, len); } + catch (IOException e) { len = -1; } + return len; + } + + return -1; + } + + protected int flush_pipe (Extio extio) + { + int mode = extio.getMode (); + + if (mode == Extio.MODE_PIPE_WRITE) + { + OutputStreamWriter osw; + osw = (OutputStreamWriter)extio.getHandle (); + try { osw.flush (); } + catch (IOException e) { return -1; } + return 0; + } + + return -1; + } + } diff --git a/ase/awk/jni.c b/ase/awk/jni.c index f6f4bab2..3d286947 100644 --- a/ase/awk/jni.c +++ b/ase/awk/jni.c @@ -1,5 +1,5 @@ /* - * $Id: jni.c,v 1.23 2006-11-24 13:20:49 bacon Exp $ + * $Id: jni.c,v 1.24 2006-11-24 15:04:23 bacon Exp $ */ #include @@ -274,7 +274,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj) runio_data.env = env; runio_data.obj = obj; - runios.pipe = ASE_NULL; + runios.pipe = __process_extio; runios.coproc = ASE_NULL; runios.file = __process_extio; runios.console = __process_extio; @@ -656,6 +656,38 @@ static ase_ssize_t __call_java_write_extio ( return ret; } + +static ase_ssize_t __call_java_flush_extio ( + JNIEnv* env, jobject obj, char* meth, ase_awk_extio_t* extio) +{ + jclass class; + jmethodID mid; + jthrowable thrown; + jint ret; + + class = (*env)->GetObjectClass(env, obj); + + mid = (*env)->GetMethodID ( + env, class, meth, "(Lase/awk/Extio;)I"); + if (mid == NULL) + { + (*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; + } + + (*env)->DeleteLocalRef (env, class); + return ret; +} + static ase_ssize_t __call_java_next_extio ( JNIEnv* env, jobject obj, char* meth, ase_awk_extio_t* extio) { @@ -765,20 +797,18 @@ static ase_ssize_t __process_extio ( runio_data->env, runio_data->obj, "write_extio", epa, data, size); } + else if (cmd == ASE_AWK_IO_FLUSH) + { + return __call_java_flush_extio ( + runio_data->env, runio_data->obj, + "flush_console", epa); + } else if (cmd == ASE_AWK_IO_NEXT) { return __call_java_next_extio ( runio_data->env, runio_data->obj, "next_console", epa); } -#if 0 - else if (cmd == ASE_AWK_IO_FLUSH) - { - return __call_java_flush_extio ( - runio_data->env, runio_data->obj, "flush_console", - data, size); - } -#endif return -1; } diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 0b75ad85..11f07d92 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.207 2006-11-24 13:20:49 bacon Exp $ + * $Id: parse.c,v 1.208 2006-11-24 15:07:18 bacon Exp $ */ #include @@ -3385,7 +3385,6 @@ static ase_awk_nde_t* __parse_next (ase_awk_t* awk) static ase_awk_nde_t* __parse_nextfile (ase_awk_t* awk, int out) { ase_awk_nde_nextfile_t* nde; - int stream = 1; if (awk->parse.id.block == PARSE_BEGIN_BLOCK || awk->parse.id.block == PARSE_END_BLOCK)