diff --git a/ase/awk/Awk.java b/ase/awk/Awk.java index 83630b21..2fa0636a 100644 --- a/ase/awk/Awk.java +++ b/ase/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.24 2007-01-31 09:31:02 bacon Exp $ + * $Id: Awk.java,v 1.25 2007-02-01 07:23:59 bacon Exp $ */ package ase.awk; @@ -54,7 +54,7 @@ public abstract class Awk private native void open () throws Exception; public native void close (); public native void parse () throws Exception; - public native void run (String main) throws Exception; + public native void run (String main, String[] args) throws Exception; private native int getmaxdepth (int id); private native void setmaxdepth (int id, int depth); @@ -79,10 +79,20 @@ public abstract class Awk private native String valtostr ( long runid, Object obj) throws Exception; - /* == simpler run method == */ + /* == simpler run methods == */ + public void run (String main) throws Exception + { + run (main, null); + } + + public void run (String[] args) throws Exception + { + run (null, args); + } + public void run () throws Exception { - run (null); + run (null, null); } /* == builtin functions == */ diff --git a/ase/awk/StdAwk.java b/ase/awk/StdAwk.java index ca0bf048..8c9dcc5b 100644 --- a/ase/awk/StdAwk.java +++ b/ase/awk/StdAwk.java @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.java,v 1.16 2007-01-31 09:31:03 bacon Exp $ + * $Id: StdAwk.java,v 1.17 2007-02-01 07:23:59 bacon Exp $ */ package ase.awk; @@ -52,18 +52,29 @@ public abstract class StdAwk extends Awk super.parse (); } - public void run (String main) throws Exception + public void run (String main, String[] args) throws Exception { cin = consoleInputNames (); cin_no = 0; cout = consoleOutputNames (); cout_no = 0; - super.run (main); + super.run (main, args); + } + + public void run (String main) throws Exception + { + run (main, null); + } + + public void run (String[] args) throws Exception + { + run (null, args); } public void run () throws Exception { - run (null); + run (null, null); } + /* == source code names == */ protected abstract String[] sourceInputNames (); protected String[] sourceOutputNames () { return null; } diff --git a/ase/awk/jni.c b/ase/awk/jni.c index 18909168..d9fccce3 100644 --- a/ase/awk/jni.c +++ b/ase/awk/jni.c @@ -1,5 +1,5 @@ /* - * $Id: jni.c,v 1.63 2007-01-31 09:31:03 bacon Exp $ + * $Id: jni.c,v 1.64 2007-02-01 07:23:59 bacon Exp $ */ #include @@ -396,7 +396,34 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj) (*env)->DeleteLocalRef (env, run_data.object_class); \ } while (0) -JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring mfn) +static ase_char_t* java_strxdup (jchar* str, jint len) +{ + if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t)) + { + ase_char_t* tmp; + ase_size_t i; + + tmp = (ase_char_t*) malloc ((len+1) * ASE_SIZEOF(ase_char_t)); + if (tmp == ASE_NULL) return ASE_NULL; + + for (i = 0; i < (ase_size_t)len; i++) tmp[i] = (ase_char_t)str[i]; + tmp[i] = ASE_T('\0'); + + return tmp; + } + else + { + ase_char_t* tmp; + + tmp = (ase_char_t*) malloc ((len+1) * ASE_SIZEOF(ase_char_t)); + if (tmp == ASE_NULL) return ASE_NULL; + + ase_awk_strncpy (tmp, (ase_char_t*)str, (ase_size_t)len); + return tmp; + } +} + +JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring mfn, jobjectArray args) { jclass class; jfieldID handle; @@ -407,9 +434,11 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m run_data_t run_data; ase_char_t* mmm; - ase_size_t len; + ase_size_t len, i; jchar* ptr; + ase_awk_runarg_t* runarg = NULL; + class = (*env)->GetObjectClass (env, obj); handle = (*env)->GetFieldID (env, class, FIELD_HANDLE, "J"); (*env)->DeleteLocalRef (env, class); @@ -498,6 +527,8 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m } else { + /* process the main entry point */ + len = (*env)->GetStringLength (env, mfn); if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t)) @@ -555,9 +586,99 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m } } - if (ase_awk_run (awk, - mmm, &runios, ASE_NULL, ASE_NULL, &run_data) == -1) + + if (args != NULL) { + /* compose arguments */ + + len = (*env)->GetArrayLength (env, args); + + runarg = malloc (sizeof(ase_awk_runarg_t) * (len+1)); + if (runarg == NULL) + { + if (mmm != NULL && mmm != mfn) free (mmm); + if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr); + DELETE_CLASS_REFS (env, run_data); + + throw_exception ( + env, + ase_awk_geterrstr(ASE_AWK_ENOMEM), + ASE_AWK_ENOMEM, + 0); + + return; + } + + for (i = 0; i < len; i++) + { + jchar* tmp; + jstring obj = (jstring)(*env)->GetObjectArrayElement (env, args, i); + + runarg[i].len = (*env)->GetStringLength (env, obj); + tmp = (*env)->GetStringChars (env, obj, JNI_FALSE); + if (tmp == NULL) + { + ase_size_t j; + + for (j = 0; j < i; j++) free (runarg[j].ptr); + free (runarg); + + (*env)->DeleteLocalRef (env, obj); + + if (mmm != NULL && mmm != mfn) free (mmm); + if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr); + DELETE_CLASS_REFS (env, run_data); + + throw_exception ( + env, + ase_awk_geterrstr(ASE_AWK_ENOMEM), + ASE_AWK_ENOMEM, + 0); + + return; + } + + runarg[i].ptr = java_strxdup (tmp, runarg[i].len); + if (runarg[i].ptr == NULL) + { + ase_size_t j; + + for (j = 0; j < i; j++) free (runarg[j].ptr); + free (runarg); + + (*env)->ReleaseStringChars (env, obj, tmp); + (*env)->DeleteLocalRef (env, obj); + + if (mmm != NULL && mmm != mfn) free (mmm); + if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr); + DELETE_CLASS_REFS (env, run_data); + + throw_exception ( + env, + ase_awk_geterrstr(ASE_AWK_ENOMEM), + ASE_AWK_ENOMEM, + 0); + + return; + } + + (*env)->ReleaseStringChars (env, obj, tmp); + (*env)->DeleteLocalRef (env, obj); + } + + runarg[i].ptr = NULL; + runarg[i].len = 0; + } + + if (ase_awk_run (awk, + mmm, &runios, ASE_NULL, runarg, &run_data) == -1) + { + if (runarg != NULL) + { + for (i = 0; i < len; i++) free (runarg[i].ptr); + free (runarg); + } + if (mmm != NULL && mmm != mfn) free (mmm); if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr); DELETE_CLASS_REFS (env, run_data); @@ -570,6 +691,12 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring m return; } + if (runarg != NULL) + { + for (i = 0; i < len; i++) free (runarg[i].ptr); + free (runarg); + } + if (mmm != NULL && mmm != mfn) free (mmm); if (ptr != NULL) (*env)->ReleaseStringChars (env, mfn, ptr); DELETE_CLASS_REFS (env, run_data); diff --git a/ase/awk/jni.h b/ase/awk/jni.h index f9729c24..a6b699ec 100644 --- a/ase/awk/jni.h +++ b/ase/awk/jni.h @@ -1,5 +1,5 @@ /* - * $Id: jni.h,v 1.20 2007-01-31 09:31:03 bacon Exp $ + * $Id: jni.h,v 1.21 2007-02-01 07:23:59 bacon Exp $ */ #ifndef _ASE_AWK_JNI_H_ @@ -18,7 +18,8 @@ extern "C" { JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv* env, jobject obj); JNIEXPORT void JNICALL Java_ase_awk_Awk_close (JNIEnv* env, jobject obj); JNIEXPORT void JNICALL Java_ase_awk_Awk_parse (JNIEnv* env, jobject obj); -JNIEXPORT void JNICALL Java_ase_awk_Awk_run (JNIEnv* env, jobject obj, jstring mfn); +JNIEXPORT void JNICALL Java_ase_awk_Awk_run ( + JNIEnv* env, jobject obj, jstring mfn, jobjectArray args); JNIEXPORT void JNICALL Java_ase_awk_Awk_addbfn ( JNIEnv* env, jobject obj, jstring name, jint min_args, jint max_args); diff --git a/ase/awk/makefile.lnx.gcc b/ase/awk/makefile.lnx.gcc index ad916880..467b6778 100644 --- a/ase/awk/makefile.lnx.gcc +++ b/ase/awk/makefile.lnx.gcc @@ -17,7 +17,7 @@ CC = gcc AR = ar LD = ld RANLIB = ranlib -CFLAGS = -Wall -O2 -D_REENTRANT -D_THREAD_SAFE -fPIC -fno-omit-frame-pointer -fno-strict-aliasing -I../.. $(JNI_INCPATH) +CFLAGS = -Wall -O2 -D_REENTRANT -D_THREAD_SAFE -fPIC -I../.. $(JNI_INCPATH) LDFLAGS = LIBS = diff --git a/ase/test/awk/Awk.java b/ase/test/awk/Awk.java index ccaba9a2..f44265d3 100644 --- a/ase/test/awk/Awk.java +++ b/ase/test/awk/Awk.java @@ -1,5 +1,5 @@ /* - * $Id: Awk.java,v 1.25 2007-01-31 09:31:29 bacon Exp $ + * $Id: Awk.java,v 1.26 2007-02-01 07:23:59 bacon Exp $ */ package ase.test.awk; @@ -101,7 +101,12 @@ public class Awk extends ase.awk.StdAwk System.out.println ("Option: [" + awk.getOption() + "]"); awk.parse (); - awk.run ("main"); + + String[] aaa = new String[3]; + aaa[0] = "abcdefg"; + aaa[1] = "qwerty"; + aaa[2] = "awk is bad"; + awk.run ("main", aaa); } catch (ase.awk.Exception e) { diff --git a/ase/test/awk/makefile.lnx.gcc b/ase/test/awk/makefile.lnx.gcc index 94ef1005..ba044183 100644 --- a/ase/test/awk/makefile.lnx.gcc +++ b/ase/test/awk/makefile.lnx.gcc @@ -2,7 +2,7 @@ SRCS = awk.c OBJS = $(SRCS:.c=.o) CC = gcc -CFLAGS = -O2 -Wall -D_REENTRANT -D_THREAD_SAFE -fPIC -fno-omit-frame-pointer -fno-strict-aliasing -I../../.. +CFLAGS = -O2 -Wall -D_REENTRANT -D_THREAD_SAFE -fPIC -I../../.. LDFLAGS = -L../../awk LIBS = -laseawk -lm