diff --git a/ase/awk/Awk.java b/ase/awk/Awk.java index 9ebf49a8..870decc4 100644 --- a/ase/awk/Awk.java +++ b/ase/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.5 2006-11-22 05:58:26 bacon Exp $ + * $Id: Awk.java,v 1.6 2006-11-22 15:12:03 bacon Exp $ */ package ase.awk; @@ -28,6 +28,17 @@ public abstract class Awk public native void parse () throws Exception; public native void run () throws Exception; private native void open () throws Exception; + private native int setconsolename (long run_id, String name); + + public void setConsoleName (long run_id, String name) //throws Exception + { + /* TODO: setconsolename is not safe. for example, it can + * crash the program if run_id is invalid. so this wrapper + * needs to do some sanity check. */ + //if (setconsolename (run_id, name) == -1) + // throw new Exception ("cannot set the consle name"); + setconsolename (run_id, name); + } /* abstrace methods */ protected abstract int open_source (int mode); diff --git a/ase/awk/Extio.java b/ase/awk/Extio.java index 6733b48f..f12e7ad7 100644 --- a/ase/awk/Extio.java +++ b/ase/awk/Extio.java @@ -20,13 +20,15 @@ public class Extio private String name; private int type; private int mode; + private long run_id; private Object handle; - protected Extio (String name, int type, int mode) + protected Extio (String name, int type, int mode, long run_id) { this.name = name; this.type = type; this.mode = mode; + this.run_id = run_id; this.handle = null; } @@ -45,6 +47,11 @@ public class Extio return this.mode; } + public long getRunId () + { + return this.run_id; + } + public void setHandle (Object handle) { this.handle = handle; diff --git a/ase/awk/extio.c b/ase/awk/extio.c index 79236ed9..49f794c2 100644 --- a/ase/awk/extio.c +++ b/ase/awk/extio.c @@ -1,5 +1,5 @@ /* - * $Id: extio.c,v 1.61 2006-11-21 15:06:14 bacon Exp $ + * $Id: extio.c,v 1.62 2006-11-22 15:12:04 bacon Exp $ */ #include @@ -678,6 +678,10 @@ int ase_awk_nextextio_read ( /* as the next stream has been opened successfully, * the eof flag should be cleared if set */ p->in.eof = ase_false; + + /* also the previous input buffer must be reset */ + p->in.pos = 0; + p->in.len = 0; } return n; diff --git a/ase/awk/jni.c b/ase/awk/jni.c index a89ed1f6..92b34bd8 100644 --- a/ase/awk/jni.c +++ b/ase/awk/jni.c @@ -1,5 +1,5 @@ /* - * $Id: jni.c,v 1.19 2006-11-22 05:58:26 bacon Exp $ + * $Id: jni.c,v 1.20 2006-11-22 15:12:04 bacon Exp $ */ #include @@ -460,7 +460,7 @@ static ase_ssize_t __call_java_open_extio ( /* get the constructor */ extio_cons = (*env)->GetMethodID ( - env, extio_class, "", "(Ljava/lang/String;II)V"); + env, extio_class, "", "(Ljava/lang/String;IIJ)V"); if (extio_cons == NULL) { (*env)->DeleteLocalRef (env, extio_class); @@ -490,7 +490,7 @@ static ase_ssize_t __call_java_open_extio ( /* construct the extio object */ extio_object = (*env)->NewObject ( env, extio_class, extio_cons, - extio_name, extio->type & 0xFF, extio->mode); + extio_name, extio->type & 0xFF, extio->mode, extio->run); if (extio_object == NULL) { (*env)->DeleteLocalRef (env, extio_name); @@ -574,6 +574,7 @@ static ase_ssize_t __call_java_read_extio ( jint ret, i; jthrowable thrown; +printf ("java_read_extio>>>\n"); class = (*env)->GetObjectClass(env, obj); mid = (*env)->GetMethodID (env, class, meth, "(Lase/awk/Extio;[CI)I"); @@ -655,6 +656,37 @@ static ase_ssize_t __call_java_write_extio ( return ret; } +static ase_ssize_t __call_java_next_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 __read_source ( int cmd, void* arg, ase_char_t* data, ase_size_t count) { @@ -733,6 +765,12 @@ static ase_ssize_t __process_extio ( runio_data->env, runio_data->obj, "write_extio", epa, data, size); } + 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) { @@ -740,13 +778,22 @@ static ase_ssize_t __process_extio ( runio_data->env, runio_data->obj, "flush_console", data, size); } - else if (cmd == ASE_AWK_IO_NEXT) - { - return __call_java_next_extio ( - runio_data->env, runio_data->obj, "flush_console", - data, size); - } #endif return -1; } + +JNIEXPORT int JNICALL Java_ase_awk_Awk_setconsolename (JNIEnv* env, jobject obj, jlong run_id, jstring name) +{ + ase_awk_run_t* run = (ase_awk_run_t*)run_id; + const jchar* str; + int len, n; + + str = (*env)->GetStringChars (env, name, JNI_FALSE); + len = (*env)->GetStringLength (env, name); + n = ase_awk_setconsolename (run, str, len); + (*env)->ReleaseStringChars (env, name, str); + return n; +} + + diff --git a/ase/awk/jni.def b/ase/awk/jni.def index 1f257fd8..efb83b6a 100644 --- a/ase/awk/jni.def +++ b/ase/awk/jni.def @@ -5,6 +5,5 @@ EXPORTS Java_ase_awk_Awk_close Java_ase_awk_Awk_parse Java_ase_awk_Awk_run - Java_ase_awk_Awk_get_1extio - Java_ase_awk_Awk_set_1extio + Java_ase_awk_Awk_setconsolename diff --git a/ase/awk/jni.h b/ase/awk/jni.h index e65644ff..d3c7cb6d 100644 --- a/ase/awk/jni.h +++ b/ase/awk/jni.h @@ -1,5 +1,5 @@ /* - * $Id: jni.h,v 1.7 2006-10-24 04:10:12 bacon Exp $ + * $Id: jni.h,v 1.8 2006-11-22 15:12:04 bacon Exp $ */ #ifndef _ASE_AWK_JNI_H_ @@ -15,6 +15,7 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv*, jobject); JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv*, jobject); JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv*, jobject); JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv*, jobject); +JNIEXPORT int JNICALL Java_ase_awk_Awk_setconsolename (JNIEnv*, jobject, jlong, jstring); #ifdef __cplusplus } diff --git a/ase/test/awk/Awk.java b/ase/test/awk/Awk.java index 9e4247d4..2cbd8fc8 100644 --- a/ase/test/awk/Awk.java +++ b/ase/test/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.4 2006-11-22 05:58:26 bacon Exp $ + * $Id: Awk.java,v 1.5 2006-11-22 15:11:36 bacon Exp $ */ package ase.test.awk; @@ -11,9 +11,25 @@ public class Awk extends ase.awk.Awk private FileReader insrc; private FileWriter outsrc; + private String[] cin; + private int cin_no; + + private String[] cout; + private int cout_no; + public Awk () throws ase.awk.Exception { super (); + + cin = new String[3]; + cin[0] = "c1.txt"; + cin[1] = "c2.txt"; + cin[2] = "c3.txt"; + cin_no = 0; + + cout = new String[1]; + cout[0] = ""; + cout_no = 0; } protected int open_source (int mode) @@ -65,6 +81,7 @@ public class Awk extends ase.awk.Awk return len; } + /* ===== console ===== */ protected int open_console (ase.awk.Extio extio) { System.err.println ("[open_console called.... name: " + extio.getName() + " mode: " + extio.getMode()); @@ -73,15 +90,35 @@ public class Awk extends ase.awk.Awk if (mode == ase.awk.Extio.MODE_CONSOLE_READ) { - InputStreamReader isr = - new InputStreamReader (System.in); + InputStreamReader isr; + if (cin_no >= cin.length) return 0; + isr = get_input_stream (cin[cin_no]); + if (isr == null) return -1; + extio.setHandle (isr); + setConsoleName (extio.getRunId(), cin[cin_no]); + + cin_no++; return 1; } else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) { - OutputStreamWriter osw = - new OutputStreamWriter (System.out); + OutputStreamWriter osw; + + if (cout_no >= cout.length) return 0; + if (cout[cout_no].length() == 0) + { + osw = new OutputStreamWriter (System.out); + } + else + { + FileOutputStream fos; + try { fos = new FileOutputStream (cout[cout_no]); } + catch (IOException e) { return -1; } + osw = new OutputStreamWriter (fos); + } + + cout_no++; extio.setHandle (osw); return 1; } @@ -119,19 +156,33 @@ public class Awk extends ase.awk.Awk 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; } + InputStreamReader isr, tmp; + int n; + + isr = (InputStreamReader)extio.getHandle (); - return len; - } - else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) - { - return -1; + try { n = isr.read (buf, 0, len); } + catch (IOException e) { return -1; } + + while (n == -1) + { + if (cin_no >= cin.length) return 0; + tmp = get_input_stream (cin[cin_no]); + if (tmp == null) return -1; + + try { isr.close (); } + catch (IOException e) { /* ignore */ } + + extio.setHandle (tmp); + setConsoleName (extio.getRunId(), cin[cin_no]); + isr = (InputStreamReader)extio.getHandle (); + cin_no++; + + try { n = isr.read (buf, 0, len); } + catch (IOException e) { return -1; } + } + + return n; } return -1; @@ -141,11 +192,7 @@ public class Awk extends ase.awk.Awk { int mode = extio.getMode (); - if (mode == ase.awk.Extio.MODE_CONSOLE_READ) - { - return -1; - } - else if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) + if (mode == ase.awk.Extio.MODE_CONSOLE_WRITE) { OutputStreamWriter osw = (OutputStreamWriter)extio.getHandle (); try { osw.write (buf, 0, len); osw.flush (); } @@ -153,15 +200,57 @@ public class Awk extends ase.awk.Awk return len; } + return -1; } protected int next_console (ase.awk.Extio extio) { - /* TODO */ - return 0; + int mode = extio.getMode (); + + if (mode == ase.awk.Extio.MODE_CONSOLE_READ) + { + InputStreamReader isr, tmp; + + isr = (InputStreamReader)extio.getHandle (); + + if (cin_no >= cin.length) return 0; + tmp = get_input_stream (cin[cin_no]); + if (tmp == null) return -1; + + try { isr.close (); } + catch (IOException e) { /* ignore */ } + + extio.setHandle (tmp); + setConsoleName (extio.getRunId(), cin[cin_no]); + + cin_no++; + return 1; + } + + return -1; } + private InputStreamReader get_input_stream (String name) + { + InputStreamReader isr; + + if (name == null || name.length() == 0) + { + isr = new InputStreamReader (System.in); + } + else + { + FileInputStream fis; + try { fis = new FileInputStream (name); } + catch (IOException e) { return null; } + isr = new InputStreamReader (fis); + } + + return isr; + } + + /* ===== file ===== */ public int open_file (ase.awk.Extio extio) { int mode = extio.getMode(); diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index 0849e676..e0f19939 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.121 2006-11-21 15:06:51 bacon Exp $ + * $Id: awk.c,v 1.122 2006-11-22 15:11:37 bacon Exp $ */ #include @@ -401,6 +401,11 @@ static int next_extio_console (ase_awk_extio_t* epa); static ase_size_t infile_no = 0; static const ase_char_t* infiles[10000] = { + /* + ASE_T("c1.txt"), + ASE_T("c2.txt"), + ASE_T("c3.txt"), + */ ASE_T(""), ASE_NULL };