2006-10-24 04:57:29 +00:00
|
|
|
/*
|
2006-12-02 16:26:29 +00:00
|
|
|
* $Id: Awk.java,v 1.15 2006-12-02 16:26:03 bacon Exp $
|
2006-10-24 04:57:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package ase.awk;
|
|
|
|
|
2006-11-26 16:17:51 +00:00
|
|
|
import java.security.AccessController;
|
|
|
|
import java.security.PrivilegedAction;
|
2006-10-24 04:57:29 +00:00
|
|
|
|
|
|
|
public abstract class Awk
|
|
|
|
{
|
|
|
|
// mode for open_source & close_source
|
|
|
|
public static final int SOURCE_READ = 1;
|
|
|
|
public static final int SOURCE_WRITE = 2;
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
2006-11-26 16:17:51 +00:00
|
|
|
//System.load ("c://projects//ase/awk/aseawk.dll");
|
|
|
|
AccessController.doPrivileged (new PrivilegedAction ()
|
|
|
|
{
|
|
|
|
public Object run ()
|
|
|
|
{
|
2006-12-02 16:26:29 +00:00
|
|
|
String dll = ase.awk.Awk.class.getResource("aseawk.dll").getFile();
|
|
|
|
System.load (dll);
|
|
|
|
//System.load ("c://projects//ase/awk/aseawk.dll");
|
|
|
|
//System.loadLibrary ("aseawk");
|
2006-11-26 16:17:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
2006-10-24 04:57:29 +00:00
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
private long handle;
|
2006-10-24 04:57:29 +00:00
|
|
|
|
2006-10-24 06:06:16 +00:00
|
|
|
public Awk () throws Exception
|
2006-10-24 04:57:29 +00:00
|
|
|
{
|
|
|
|
open ();
|
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
/* == just in case == */
|
|
|
|
protected void finalize ()
|
|
|
|
{
|
|
|
|
if (handle != 0) close ();
|
|
|
|
}
|
|
|
|
|
2006-11-27 15:11:14 +00:00
|
|
|
/* == native methods == */
|
2006-10-24 06:06:16 +00:00
|
|
|
private native void open () throws Exception;
|
2006-11-27 15:11:14 +00:00
|
|
|
public native void close ();
|
|
|
|
public native void parse () throws Exception;
|
|
|
|
public native void run () throws Exception;
|
|
|
|
|
2006-11-29 14:52:36 +00:00
|
|
|
private native int addbfn (String name, int min_args, int max_args);
|
|
|
|
private native int delbfn (String name);
|
2006-11-27 15:11:14 +00:00
|
|
|
|
2006-12-02 16:26:29 +00:00
|
|
|
private native int setfilename (long runid, String name);
|
|
|
|
private native int setofilename (long runid, String name);
|
|
|
|
|
|
|
|
private native Object strtonum (long runid, String str);
|
|
|
|
private native String valtostr (long runid, Object obj);
|
2006-11-22 15:12:04 +00:00
|
|
|
|
2006-11-27 15:11:14 +00:00
|
|
|
/* == builtin functions == */
|
|
|
|
public void addBuiltinFunction (
|
|
|
|
String name, int min_args, int max_args) throws Exception
|
|
|
|
{
|
2006-11-29 14:52:36 +00:00
|
|
|
if (addbfn (name, min_args, max_args) == -1)
|
|
|
|
{
|
|
|
|
throw new Exception (
|
|
|
|
"cannot add the built-in function - " + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteBuiltinFunction (String name) throws Exception
|
|
|
|
{
|
|
|
|
if (delbfn (name) == -1)
|
|
|
|
{
|
|
|
|
throw new Exception (
|
|
|
|
"cannot delete the built-in function - " + name);
|
|
|
|
}
|
2006-11-27 15:11:14 +00:00
|
|
|
}
|
|
|
|
|
2006-12-02 16:26:29 +00:00
|
|
|
public long builtinFunctionArgumentToLong (long runid, Object obj)
|
|
|
|
{
|
|
|
|
long n;
|
|
|
|
|
|
|
|
if (obj == null) n = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (obj instanceof String)
|
|
|
|
obj = strtonum (runid, (String)obj);
|
|
|
|
|
|
|
|
if (obj instanceof Long)
|
|
|
|
{
|
|
|
|
n = ((Long)obj).longValue ();
|
|
|
|
}
|
|
|
|
else if (obj instanceof Double)
|
|
|
|
{
|
|
|
|
n = ((Double)obj).longValue ();
|
|
|
|
}
|
|
|
|
else if (obj instanceof Integer)
|
|
|
|
{
|
|
|
|
n = ((Integer)obj).longValue ();
|
|
|
|
}
|
|
|
|
else if (obj instanceof Short)
|
|
|
|
{
|
|
|
|
n = ((Short)obj).longValue ();
|
|
|
|
}
|
|
|
|
else if (obj instanceof Float)
|
|
|
|
{
|
|
|
|
n = ((Float)obj).longValue ();
|
|
|
|
}
|
|
|
|
else n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double builtinFunctionArgumentToDouble (long runid, Object obj)
|
|
|
|
{
|
|
|
|
double n;
|
|
|
|
|
|
|
|
if (obj == null) n = 0.0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (obj instanceof String)
|
|
|
|
obj = strtonum (runid, (String)obj);
|
|
|
|
|
|
|
|
if (obj instanceof Long)
|
|
|
|
{
|
|
|
|
n = ((Long)obj).doubleValue ();
|
|
|
|
}
|
|
|
|
else if (obj instanceof Double)
|
|
|
|
{
|
|
|
|
n = ((Double)obj).doubleValue ();
|
|
|
|
}
|
|
|
|
else if (obj instanceof Integer)
|
|
|
|
{
|
|
|
|
n = ((Integer)obj).doubleValue ();
|
|
|
|
}
|
|
|
|
else if (obj instanceof Short)
|
|
|
|
{
|
|
|
|
n = ((Short)obj).doubleValue ();
|
|
|
|
}
|
|
|
|
else if (obj instanceof Float)
|
|
|
|
{
|
|
|
|
n = ((Float)obj).doubleValue ();
|
|
|
|
}
|
|
|
|
else n = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String builtinFunctionArgumentToString (long runid, Object obj)
|
|
|
|
{
|
|
|
|
String str;
|
|
|
|
|
|
|
|
if (obj == null) str = "";
|
|
|
|
else if (obj instanceof String) str = (String)obj;
|
|
|
|
else str = valtostr (runid, obj);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
/* == console name setters == */
|
2006-11-24 13:25:12 +00:00
|
|
|
public void setInputConsoleName (Extio extio, String name) //throws Exception
|
2006-11-22 15:12:04 +00:00
|
|
|
{
|
|
|
|
/* TODO: setconsolename is not safe. for example, it can
|
2006-12-02 16:26:29 +00:00
|
|
|
* crash the program if runid is invalid. so this wrapper
|
2006-11-22 15:12:04 +00:00
|
|
|
* needs to do some sanity check. */
|
2006-12-02 16:26:29 +00:00
|
|
|
//if (setconsolename (runid, name) == -1)
|
2006-11-22 15:12:04 +00:00
|
|
|
// throw new Exception ("cannot set the consle name");
|
2006-11-24 13:25:12 +00:00
|
|
|
setfilename (extio.getRunId(), name);
|
2006-11-23 03:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setOutputConsoleName (Extio extio, String name)
|
|
|
|
{
|
|
|
|
// TODO:
|
2006-11-24 13:25:12 +00:00
|
|
|
setofilename (extio.getRunId(), name);
|
2006-11-22 15:12:04 +00:00
|
|
|
}
|
2006-10-24 04:57:29 +00:00
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
/* == recursion depth limiting == */
|
|
|
|
protected int getMaxParseDepth ()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
protected int getMaxRunDepth ()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* == source code management == */
|
|
|
|
protected abstract int openSource (int mode);
|
|
|
|
protected abstract int closeSource (int mode);
|
|
|
|
protected abstract int readSource (char[] buf, int len);
|
|
|
|
protected abstract int writeSource (char[] buf, int len);
|
2006-10-24 04:57:29 +00:00
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
/* == external io interface == */
|
|
|
|
protected int openExtio (Extio extio)
|
2006-11-21 15:06:51 +00:00
|
|
|
{
|
|
|
|
int type = extio.getType ();
|
2006-11-26 15:55:44 +00:00
|
|
|
if (type == Extio.TYPE_CONSOLE) return openConsole (extio);
|
|
|
|
if (type == Extio.TYPE_FILE) return openFile (extio);
|
|
|
|
if (type == Extio.TYPE_PIPE) return openPipe (extio);
|
|
|
|
//if (type == Extio.TYPE_COPROC) return openCoproc (extio);
|
2006-11-21 15:06:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
protected int closeExtio (Extio extio)
|
2006-11-21 15:06:51 +00:00
|
|
|
{
|
|
|
|
int type = extio.getType ();
|
2006-11-26 15:55:44 +00:00
|
|
|
if (type == Extio.TYPE_CONSOLE) return closeConsole (extio);
|
|
|
|
if (type == Extio.TYPE_FILE) return closeFile (extio);
|
|
|
|
if (type == Extio.TYPE_PIPE) return closePipe (extio);
|
|
|
|
//if (type == Extio.TYPE_COPROC) return closeCoproc (extio);
|
2006-11-21 15:06:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
protected int readExtio (Extio extio, char[] buf, int len)
|
2006-11-21 15:06:51 +00:00
|
|
|
{
|
2006-11-23 03:31:58 +00:00
|
|
|
// this check is needed because 0 is used to indicate
|
|
|
|
// the end of the stream. java streams can return 0
|
|
|
|
// if the data given is 0 bytes and it didn't reach
|
|
|
|
// the end of the stream.
|
|
|
|
if (len <= 0) return -1;
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
int type = extio.getType ();
|
|
|
|
if (type == Extio.TYPE_CONSOLE)
|
2006-11-26 15:55:44 +00:00
|
|
|
return readConsole (extio, buf, len);
|
2006-11-21 15:06:51 +00:00
|
|
|
if (type == Extio.TYPE_FILE)
|
2006-11-26 15:55:44 +00:00
|
|
|
return readFile (extio, buf, len);
|
2006-11-24 15:07:18 +00:00
|
|
|
if (type == Extio.TYPE_PIPE)
|
2006-11-26 15:55:44 +00:00
|
|
|
return readPipe (extio, buf, len);
|
2006-11-24 15:07:18 +00:00
|
|
|
//if (type == Extio.TYPE_COPROC)
|
2006-11-26 15:55:44 +00:00
|
|
|
// return readCoproc (extio, buf, len);
|
2006-11-21 15:06:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
protected int writeExtio (Extio extio, char[] buf, int len)
|
2006-11-21 15:06:51 +00:00
|
|
|
{
|
2006-11-23 03:31:58 +00:00
|
|
|
if (len <= 0) return -1;
|
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
int type = extio.getType ();
|
|
|
|
if (type == Extio.TYPE_CONSOLE)
|
2006-11-26 15:55:44 +00:00
|
|
|
return writeConsole (extio, buf, len);
|
2006-11-21 15:06:51 +00:00
|
|
|
if (type == Extio.TYPE_FILE)
|
2006-11-26 15:55:44 +00:00
|
|
|
return writeFile (extio, buf, len);
|
2006-11-24 15:07:18 +00:00
|
|
|
if (type == Extio.TYPE_PIPE)
|
2006-11-26 15:55:44 +00:00
|
|
|
return writePipe (extio, buf, len);
|
2006-11-24 15:07:18 +00:00
|
|
|
//if (type == Extio.TYPE_COPROC)
|
2006-11-26 15:55:44 +00:00
|
|
|
// return writeCoproc (extio, buf, len);
|
2006-11-24 15:07:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
protected int flushExtio (Extio extio)
|
2006-11-24 15:07:18 +00:00
|
|
|
{
|
|
|
|
int type = extio.getType ();
|
2006-11-26 15:55:44 +00:00
|
|
|
if (type == Extio.TYPE_CONSOLE) return flushConsole (extio);
|
|
|
|
if (type == Extio.TYPE_FILE) return flushFile (extio);
|
|
|
|
if (type == Extio.TYPE_PIPE) return flushPipe (extio);
|
|
|
|
//if (type == Extio.TYPE_COPROC) return flushCoproc (extio);
|
2006-11-21 15:06:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
protected int nextExtio (Extio extio)
|
2006-11-22 05:58:26 +00:00
|
|
|
{
|
|
|
|
int type = extio.getType ();
|
2006-11-26 15:55:44 +00:00
|
|
|
if (type == Extio.TYPE_CONSOLE) return nextConsole (extio);
|
2006-11-22 05:58:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-26 15:55:44 +00:00
|
|
|
protected abstract int openConsole (Extio extio);
|
|
|
|
protected abstract int closeConsole (Extio extio);
|
|
|
|
protected abstract int readConsole (Extio extio, char[] buf, int len);
|
|
|
|
protected abstract int writeConsole (Extio extio, char[] buf, int len);
|
|
|
|
protected abstract int flushConsole (Extio extio);
|
|
|
|
protected abstract int nextConsole (Extio extio);
|
|
|
|
|
|
|
|
protected abstract int openFile (Extio extio);
|
|
|
|
protected abstract int closeFile (Extio extio);
|
|
|
|
protected abstract int readFile (Extio extio, char[] buf, int len);
|
|
|
|
protected abstract int writeFile (Extio extio, char[] buf, int len);
|
|
|
|
protected abstract int flushFile (Extio extio);
|
|
|
|
|
|
|
|
protected abstract int openPipe (Extio extio);
|
|
|
|
protected abstract int closePipe (Extio extio);
|
|
|
|
protected abstract int readPipe (Extio extio, char[] buf, int len);
|
|
|
|
protected abstract int writePipe (Extio extio, char[] buf, int len);
|
|
|
|
protected abstract int flushPipe (Extio extio);
|
2006-10-24 04:57:29 +00:00
|
|
|
}
|