This commit is contained in:
@ -1,80 +0,0 @@
|
||||
/*
|
||||
* $Id: Argument.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public class Argument implements Clearable
|
||||
{
|
||||
protected long runid;
|
||||
//protected long valid;
|
||||
public long valid;
|
||||
|
||||
/* An instance of the Argument class should not be used
|
||||
* outside the context where it is availble. When it is
|
||||
* referenced that way, the getXXX methods may cause
|
||||
* JVM to crash */
|
||||
|
||||
Argument (Context ctx, long runid, long valid)
|
||||
{
|
||||
if (ctx != null) ctx.pushClearable (this);
|
||||
this.runid = runid;
|
||||
this.valid = valid;
|
||||
}
|
||||
|
||||
Argument (long runid, long valid)
|
||||
{
|
||||
this (null, runid, valid);
|
||||
}
|
||||
|
||||
public void clear ()
|
||||
{
|
||||
clearval (this.runid, this.valid);
|
||||
}
|
||||
|
||||
public long getIntValue ()
|
||||
{
|
||||
if (this.valid == 0) return 0;
|
||||
return getintval (this.runid, this.valid);
|
||||
}
|
||||
|
||||
public double getRealValue ()
|
||||
{
|
||||
if (this.valid == 0) return 0.0;
|
||||
return getrealval (this.runid, this.valid);
|
||||
}
|
||||
|
||||
public String getStringValue () throws Exception
|
||||
{
|
||||
if (this.valid == 0) return "";
|
||||
return getstrval (this.runid, this.valid);
|
||||
}
|
||||
|
||||
public boolean isIndexed ()
|
||||
{
|
||||
if (this.valid == 0) return false;
|
||||
return isindexed (this.runid, this.valid);
|
||||
}
|
||||
|
||||
public Argument getIndexed (String idx) throws Exception
|
||||
{
|
||||
if (this.valid == 0) return null;
|
||||
return getindexed (this.runid, this.valid, idx);
|
||||
}
|
||||
|
||||
public Argument getIndexed (long idx) throws Exception
|
||||
{
|
||||
if (this.valid == 0) return null;
|
||||
return getIndexed (Long.toString(idx));
|
||||
}
|
||||
|
||||
protected native long getintval (long runid, long valid);
|
||||
protected native double getrealval (long runid, long valid);
|
||||
protected native String getstrval (long runid, long valid) throws Exception;
|
||||
protected native boolean isindexed (long runid, long valid);
|
||||
protected native Argument getindexed (long runid, long valid, String idx) throws Exception;
|
||||
|
||||
protected native void clearval (long runid, long valid);
|
||||
}
|
@ -1,459 +0,0 @@
|
||||
/*
|
||||
* $Id: Awk.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Represents the AWK interpreter engine
|
||||
*/
|
||||
public abstract class Awk
|
||||
{
|
||||
private HashMap functionTable;
|
||||
|
||||
// mode for open_source & close_source
|
||||
public static final int SOURCE_READ = 1;
|
||||
public static final int SOURCE_WRITE = 2;
|
||||
|
||||
// depth id
|
||||
public static final int DEPTH_BLOCK_PARSE = (1 << 0);
|
||||
public static final int DEPTH_BLOCK_RUN = (1 << 1);
|
||||
public static final int DEPTH_EXPR_PARSE = (1 << 2);
|
||||
public static final int DEPTH_EXPR_RUN = (1 << 3);
|
||||
public static final int DEPTH_REX_BUILD = (1 << 4);
|
||||
public static final int DEPTH_REX_MATCH = (1 << 5);
|
||||
|
||||
// options
|
||||
public static final int OPTION_IMPLICIT = (1 << 0);
|
||||
public static final int OPTION_EXPLICIT = (1 << 1);
|
||||
public static final int OPTION_SHIFT = (1 << 4);
|
||||
public static final int OPTION_IDIV = (1 << 5);
|
||||
public static final int OPTION_STRCONCAT = (1 << 6);
|
||||
public static final int OPTION_EXTIO = (1 << 7);
|
||||
public static final int OPTION_COPROC = (1 << 8);
|
||||
public static final int OPTION_BLOCKLESS = (1 << 9);
|
||||
public static final int OPTION_BASEONE = (1 << 10);
|
||||
public static final int OPTION_STRIPSPACES = (1 << 11);
|
||||
public static final int OPTION_NEXTOFILE = (1 << 12);
|
||||
public static final int OPTION_CRLF = (1 << 13);
|
||||
public static final int OPTION_ARGSTOMAIN = (1 << 14);
|
||||
public static final int OPTION_RESET = (1 << 15);
|
||||
public static final int OPTION_MAPTOVAR = (1 << 16);
|
||||
public static final int OPTION_PABLOCK = (1 << 17);
|
||||
|
||||
protected final static Reader stdin = new BufferedReader (new InputStreamReader (System.in));
|
||||
protected final static Writer stdout = new BufferedWriter (new OutputStreamWriter (System.out));
|
||||
|
||||
private long awkid;
|
||||
|
||||
public Awk () throws Exception
|
||||
{
|
||||
this.awkid = 0;
|
||||
this.functionTable = new HashMap ();
|
||||
open ();
|
||||
}
|
||||
|
||||
/* == just in case == */
|
||||
protected void finalize () throws Throwable
|
||||
{
|
||||
close ();
|
||||
super.finalize ();
|
||||
}
|
||||
|
||||
public void close ()
|
||||
{
|
||||
if (this.awkid != 0)
|
||||
{
|
||||
close (this.awkid);
|
||||
this.awkid = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a source program
|
||||
*/
|
||||
public void parse () throws Exception
|
||||
{
|
||||
parse (this.awkid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a parsed program
|
||||
*/
|
||||
public void run (String main, String[] args) throws Exception
|
||||
{
|
||||
run (this.awkid, main, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a parsed program
|
||||
*/
|
||||
public void run (String main) throws Exception
|
||||
{
|
||||
run (this.awkid, main, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a parsed program
|
||||
*/
|
||||
public void run (String[] args) throws Exception
|
||||
{
|
||||
run (this.awkid, null, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a parsed program
|
||||
*/
|
||||
public void run () throws Exception
|
||||
{
|
||||
run (this.awkid, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a request to stop a running program
|
||||
*/
|
||||
public void stop () throws Exception
|
||||
{
|
||||
stop (this.awkid);
|
||||
}
|
||||
|
||||
/* == native methods == */
|
||||
private native void open () throws Exception;
|
||||
protected native void close (long awkid);
|
||||
protected native void parse (long awkid) throws Exception;
|
||||
protected native void run (long awkid, String main, String[] args) throws Exception;
|
||||
protected native void stop (long awkid) throws Exception;
|
||||
protected native int getmaxdepth (long awkid, int id) throws Exception;
|
||||
protected native void setmaxdepth (long awkid, int id, int depth) throws Exception;
|
||||
protected native int getoption (long awkid) throws Exception;
|
||||
protected native void setoption (long awkid, int opt) throws Exception;
|
||||
protected native boolean getdebug (long awkid) throws Exception;
|
||||
protected native void setdebug (long awkid, boolean debug) throws Exception;
|
||||
protected native String getword (long awkid, String ow) throws Exception;
|
||||
protected native void setword (long awkid, String ow, String nw) throws Exception;
|
||||
|
||||
|
||||
protected native void addfunc (String name, int min_args, int max_args) throws Exception;
|
||||
protected native void delfunc (String name) throws Exception;
|
||||
native void setfilename (long runid, String name) throws Exception;
|
||||
native void setofilename (long runid, String name) throws Exception;
|
||||
protected native String strftime (String fmt, long sec);
|
||||
protected native String strfgmtime (String fmt, long sec);
|
||||
protected native int system (String cmd);
|
||||
|
||||
|
||||
/* == intrinsic functions == */
|
||||
public void addFunction (String name, int min_args, int max_args) throws Exception
|
||||
{
|
||||
addFunction (name, min_args, max_args, name);
|
||||
}
|
||||
|
||||
public void addFunction (String name, int min_args, int max_args, String method) throws Exception
|
||||
{
|
||||
if (functionTable.containsKey (name))
|
||||
{
|
||||
throw new Exception (
|
||||
"cannot add existing function '" + name + "'",
|
||||
Exception.EXIST);
|
||||
}
|
||||
|
||||
functionTable.put (name, method);
|
||||
try { addfunc (name, min_args, max_args); }
|
||||
catch (Exception e)
|
||||
{
|
||||
functionTable.remove (name);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteFunction (String name) throws Exception
|
||||
{
|
||||
delfunc (name);
|
||||
functionTable.remove (name);
|
||||
}
|
||||
|
||||
protected void handleFunction (
|
||||
Context ctx, String name, Return ret, Argument[] args) throws Exception
|
||||
{
|
||||
String mn = (String)functionTable.get(name);
|
||||
// name should always be found in this table.
|
||||
// otherwise, there is something wrong with this program.
|
||||
|
||||
Class c = this.getClass ();
|
||||
Class[] a = { Context.class, String.class, Return.class, Argument[].class };
|
||||
|
||||
try
|
||||
{
|
||||
Method m = c.getMethod (mn, a);
|
||||
//m.invoke (this, ctx, name, ret, args) ;
|
||||
m.invoke (this, new Object[] {ctx, name, ret, args});
|
||||
|
||||
}
|
||||
catch (java.lang.reflect.InvocationTargetException e)
|
||||
{
|
||||
/* the underlying method has throw an exception */
|
||||
Throwable t = e.getCause();
|
||||
if (t == null)
|
||||
{
|
||||
throw new Exception (null, Exception.BFNIMPL);
|
||||
}
|
||||
else if (t instanceof Exception)
|
||||
{
|
||||
throw (Exception)t;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception (
|
||||
t.getMessage(), Exception.BFNIMPL);
|
||||
}
|
||||
}
|
||||
catch (java.lang.Exception e)
|
||||
{
|
||||
throw new Exception (e.getMessage(), Exception.BFNIMPL);
|
||||
}
|
||||
}
|
||||
|
||||
/* == depth limiting == */
|
||||
public int getMaxDepth (int id) throws Exception
|
||||
{
|
||||
return getmaxdepth (this.awkid, id);
|
||||
}
|
||||
|
||||
public void setMaxDepth (int ids, int depth) throws Exception
|
||||
{
|
||||
setmaxdepth (this.awkid, ids, depth);
|
||||
}
|
||||
|
||||
/* == option == */
|
||||
public int getOption () throws Exception
|
||||
{
|
||||
return getoption (this.awkid);
|
||||
}
|
||||
|
||||
public void setOption (int opt) throws Exception
|
||||
{
|
||||
setoption (this.awkid, opt);
|
||||
}
|
||||
|
||||
/* == debug == */
|
||||
public boolean getDebug () throws Exception
|
||||
{
|
||||
return getdebug (this.awkid);
|
||||
}
|
||||
|
||||
public void setDebug (boolean debug) throws Exception
|
||||
{
|
||||
setdebug (this.awkid, debug);
|
||||
}
|
||||
|
||||
/* == word replacement == */
|
||||
public String getWord (String ow) throws Exception
|
||||
{
|
||||
return getword (this.awkid, ow);
|
||||
}
|
||||
|
||||
public void setWord (String ow, String nw) throws Exception
|
||||
{
|
||||
setword (this.awkid, ow, nw);
|
||||
}
|
||||
|
||||
public void unsetWord (String ow) throws Exception
|
||||
{
|
||||
setword (this.awkid, ow, null);
|
||||
}
|
||||
|
||||
public void unsetAllWords () throws Exception
|
||||
{
|
||||
setword (this.awkid, null, null);
|
||||
}
|
||||
|
||||
/* == 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);
|
||||
|
||||
/* == external io interface == */
|
||||
protected int openExtio (Extio extio)
|
||||
{
|
||||
switch (extio.getType())
|
||||
{
|
||||
case Extio.TYPE_CONSOLE:
|
||||
{
|
||||
Console con = new Console (this, extio);
|
||||
int n = openConsole (con);
|
||||
extio.setHandle (con);
|
||||
return n;
|
||||
}
|
||||
|
||||
case Extio.TYPE_FILE:
|
||||
{
|
||||
File file = new File (this, extio);
|
||||
int n = openFile (file);
|
||||
extio.setHandle (file);
|
||||
return n;
|
||||
}
|
||||
|
||||
case Extio.TYPE_PIPE:
|
||||
{
|
||||
Pipe pipe = new Pipe (this, extio);
|
||||
int n = openPipe (pipe);
|
||||
extio.setHandle (pipe);
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int closeExtio (Extio extio)
|
||||
{
|
||||
switch (extio.getType())
|
||||
{
|
||||
case Extio.TYPE_CONSOLE:
|
||||
return closeConsole (
|
||||
(Console)extio.getHandle());
|
||||
|
||||
case Extio.TYPE_FILE:
|
||||
return closeFile ((File)extio.getHandle());
|
||||
|
||||
case Extio.TYPE_PIPE:
|
||||
return closePipe ((Pipe)extio.getHandle());
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int readExtio (Extio extio, char[] buf, int len)
|
||||
{
|
||||
// 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;
|
||||
|
||||
switch (extio.getType())
|
||||
{
|
||||
|
||||
case Extio.TYPE_CONSOLE:
|
||||
{
|
||||
return readConsole (
|
||||
(Console)extio.getHandle(), buf, len);
|
||||
}
|
||||
|
||||
case Extio.TYPE_FILE:
|
||||
{
|
||||
return readFile (
|
||||
(File)extio.getHandle(), buf, len);
|
||||
}
|
||||
|
||||
case Extio.TYPE_PIPE:
|
||||
{
|
||||
return readPipe (
|
||||
(Pipe)extio.getHandle(), buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int writeExtio (Extio extio, char[] buf, int len)
|
||||
{
|
||||
if (len <= 0) return -1;
|
||||
|
||||
switch (extio.getType())
|
||||
{
|
||||
case Extio.TYPE_CONSOLE:
|
||||
{
|
||||
return writeConsole (
|
||||
(Console)extio.getHandle(), buf, len);
|
||||
}
|
||||
|
||||
case Extio.TYPE_FILE:
|
||||
{
|
||||
return writeFile (
|
||||
(File)extio.getHandle(), buf, len);
|
||||
}
|
||||
|
||||
case Extio.TYPE_PIPE:
|
||||
{
|
||||
return writePipe (
|
||||
(Pipe)extio.getHandle(), buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int flushExtio (Extio extio)
|
||||
{
|
||||
switch (extio.getType())
|
||||
{
|
||||
|
||||
case Extio.TYPE_CONSOLE:
|
||||
{
|
||||
return flushConsole ((Console)extio.getHandle());
|
||||
}
|
||||
|
||||
case Extio.TYPE_FILE:
|
||||
{
|
||||
return flushFile ((File)extio.getHandle());
|
||||
}
|
||||
|
||||
case Extio.TYPE_PIPE:
|
||||
{
|
||||
return flushPipe ((Pipe)extio.getHandle());
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int nextExtio (Extio extio)
|
||||
{
|
||||
int type = extio.getType ();
|
||||
|
||||
switch (extio.getType())
|
||||
{
|
||||
case Extio.TYPE_CONSOLE:
|
||||
{
|
||||
return nextConsole ((Console)extio.getHandle());
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected abstract int openConsole (Console con);
|
||||
protected abstract int closeConsole (Console con);
|
||||
protected abstract int readConsole (Console con, char[] buf, int len);
|
||||
protected abstract int writeConsole (Console con, char[] buf, int len);
|
||||
protected abstract int flushConsole (Console con);
|
||||
protected abstract int nextConsole (Console con);
|
||||
|
||||
protected abstract int openFile (File file);
|
||||
protected abstract int closeFile (File file);
|
||||
protected abstract int readFile (File file, char[] buf, int len);
|
||||
protected abstract int writeFile (File file, char[] buf, int len);
|
||||
protected abstract int flushFile (File file);
|
||||
|
||||
protected abstract int openPipe (Pipe pipe);
|
||||
protected abstract int closePipe (Pipe pipe);
|
||||
protected abstract int readPipe (Pipe pipe, char[] buf, int len);
|
||||
protected abstract int writePipe (Pipe pipe, char[] buf, int len);
|
||||
protected abstract int flushPipe (Pipe pipe);
|
||||
|
||||
/* TODO: ...
|
||||
protected void onRunStart (Context ctx) {}
|
||||
protected void onRunEnd (Context ctx) {}
|
||||
protected void onRunReturn (Context ctx) {}
|
||||
protected void onRunStatement (Context ctx) {}
|
||||
*/
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
/*
|
||||
* $Id: Clearable.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public interface Clearable
|
||||
{
|
||||
public void clear ();
|
||||
}
|
||||
|
@ -1,30 +0,0 @@
|
||||
/*
|
||||
* $Id: Console.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public class Console extends IO
|
||||
{
|
||||
public static final int MODE_READ = Extio.MODE_CONSOLE_READ;
|
||||
public static final int MODE_WRITE = Extio.MODE_CONSOLE_WRITE;
|
||||
|
||||
protected Console (Awk awk, Extio extio)
|
||||
{
|
||||
super (awk, extio);
|
||||
}
|
||||
|
||||
public void setFileName (String name) throws Exception
|
||||
{
|
||||
if (getMode() == MODE_READ)
|
||||
{
|
||||
awk.setfilename (getRunId(), name);
|
||||
}
|
||||
else
|
||||
{
|
||||
awk.setofilename (getRunId(), name);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
/*
|
||||
* $Id: Context.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class Context
|
||||
{
|
||||
public static int GLOBAL_ARGC = 0;
|
||||
public static int GLOBAL_ARGV = 1;
|
||||
public static int GLOBAL_CONVFMT = 2;
|
||||
public static int GLOBAL_FILENAME = 3;
|
||||
public static int GLOBAL_FNR = 4;
|
||||
public static int GLOBAL_FS = 5;
|
||||
public static int GLOBAL_IGNORECASE = 6;
|
||||
public static int GLOBAL_NF = 7;
|
||||
public static int GLOBAL_NR = 8;
|
||||
public static int GLOBAL_OFILENAME = 9;
|
||||
public static int GLOBAL_OFMT = 10;
|
||||
public static int GLOBAL_OFS = 11;
|
||||
public static int GLOBAL_ORS = 12;
|
||||
public static int GLOBAL_RLENGTH = 13;
|
||||
public static int GLOBAL_RS = 14;
|
||||
public static int GLOBAL_RSTART = 15;
|
||||
public static int GLOBAL_SUBSEP = 16;
|
||||
|
||||
protected Awk awk;
|
||||
protected long runid;
|
||||
protected Object custom;
|
||||
protected Stack clearableStack;
|
||||
|
||||
Context (Awk awk)
|
||||
{
|
||||
this.awk = awk;
|
||||
this.runid = 0;
|
||||
this.custom = null;
|
||||
this.clearableStack = new Stack ();
|
||||
}
|
||||
|
||||
void clear ()
|
||||
{
|
||||
Clearable obj;
|
||||
while ((obj = popClearable()) != null) obj.clear ();
|
||||
}
|
||||
|
||||
void pushClearable (Clearable obj)
|
||||
{
|
||||
clearableStack.push (obj);
|
||||
}
|
||||
|
||||
Clearable popClearable ()
|
||||
{
|
||||
if (clearableStack.empty()) return null;
|
||||
return (Clearable)clearableStack.pop ();
|
||||
}
|
||||
|
||||
public Awk getAwk ()
|
||||
{
|
||||
return awk;
|
||||
}
|
||||
|
||||
public long getId ()
|
||||
{
|
||||
return this.runid;
|
||||
}
|
||||
|
||||
public void setCustom (Object custom)
|
||||
{
|
||||
this.custom = custom;
|
||||
}
|
||||
|
||||
public Object getCustom ()
|
||||
{
|
||||
return this.custom;
|
||||
}
|
||||
|
||||
public void setConsoleInputName (String name) throws Exception
|
||||
{
|
||||
awk.setfilename (this.runid, name);
|
||||
}
|
||||
|
||||
public void setConsoleOutputName (String name) throws Exception
|
||||
{
|
||||
awk.setofilename (this.runid, name);
|
||||
}
|
||||
|
||||
public void stop ()
|
||||
{
|
||||
stop (this.runid);
|
||||
}
|
||||
|
||||
public void setGlobal (int id, Return ret) throws Exception
|
||||
{
|
||||
// regardless of the result, the value field
|
||||
// of the return object is reset to 0 by setglobal.
|
||||
setglobal (this.runid, id, ret);
|
||||
}
|
||||
|
||||
public Argument getGlobal (int id) throws Exception
|
||||
{
|
||||
return getglobal (this.runid, id);
|
||||
}
|
||||
|
||||
protected native void stop (long runid);
|
||||
protected native void setglobal (long runid, int id, Return ret);
|
||||
protected native Argument getglobal (long runid, int id);
|
||||
|
||||
// TODO:
|
||||
// setError
|
||||
// getError
|
||||
}
|
@ -1,184 +0,0 @@
|
||||
/*
|
||||
* $Id: Exception.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public class Exception extends java.lang.Exception
|
||||
{
|
||||
private int code;
|
||||
private int line;
|
||||
|
||||
// generated by generrcode-java.awk
|
||||
public static final int NOERR = 0;
|
||||
public static final int CUSTOM = 1;
|
||||
public static final int INVAL = 2;
|
||||
public static final int NOMEM = 3;
|
||||
public static final int NOSUP = 4;
|
||||
public static final int NOPER = 5;
|
||||
public static final int NODEV = 6;
|
||||
public static final int NOSPC = 7;
|
||||
public static final int MFILE = 8;
|
||||
public static final int MLINK = 9;
|
||||
public static final int AGAIN = 10;
|
||||
public static final int NOENT = 11;
|
||||
public static final int EXIST = 12;
|
||||
public static final int FTBIG = 13;
|
||||
public static final int TBUSY = 14;
|
||||
public static final int ISDIR = 15;
|
||||
public static final int IOERR = 16;
|
||||
public static final int OPEN = 17;
|
||||
public static final int READ = 18;
|
||||
public static final int WRITE = 19;
|
||||
public static final int CLOSE = 20;
|
||||
public static final int INTERN = 21;
|
||||
public static final int RUNTIME = 22;
|
||||
public static final int BLKNST = 23;
|
||||
public static final int EXPRNST = 24;
|
||||
public static final int SINOP = 25;
|
||||
public static final int SINCL = 26;
|
||||
public static final int SINRD = 27;
|
||||
public static final int SOUTOP = 28;
|
||||
public static final int SOUTCL = 29;
|
||||
public static final int SOUTWR = 30;
|
||||
public static final int LXCHR = 31;
|
||||
public static final int LXDIG = 32;
|
||||
public static final int LXUNG = 33;
|
||||
public static final int ENDSRC = 34;
|
||||
public static final int ENDCMT = 35;
|
||||
public static final int ENDSTR = 36;
|
||||
public static final int ENDREX = 37;
|
||||
public static final int LBRACE = 38;
|
||||
public static final int LPAREN = 39;
|
||||
public static final int RPAREN = 40;
|
||||
public static final int RBRACK = 41;
|
||||
public static final int COMMA = 42;
|
||||
public static final int SCOLON = 43;
|
||||
public static final int COLON = 44;
|
||||
public static final int STMEND = 45;
|
||||
public static final int IN = 46;
|
||||
public static final int NOTVAR = 47;
|
||||
public static final int EXPRES = 48;
|
||||
public static final int FUNC = 49;
|
||||
public static final int WHILE = 50;
|
||||
public static final int ASSIGN = 51;
|
||||
public static final int IDENT = 52;
|
||||
public static final int FNNAME = 53;
|
||||
public static final int BLKBEG = 54;
|
||||
public static final int BLKEND = 55;
|
||||
public static final int DUPBEG = 56;
|
||||
public static final int DUPEND = 57;
|
||||
public static final int BFNRED = 58;
|
||||
public static final int AFNRED = 59;
|
||||
public static final int GBLRED = 60;
|
||||
public static final int PARRED = 61;
|
||||
public static final int DUPPAR = 62;
|
||||
public static final int DUPGBL = 63;
|
||||
public static final int DUPLCL = 64;
|
||||
public static final int BADPAR = 65;
|
||||
public static final int BADVAR = 66;
|
||||
public static final int UNDEF = 67;
|
||||
public static final int LVALUE = 68;
|
||||
public static final int GBLTM = 69;
|
||||
public static final int LCLTM = 70;
|
||||
public static final int PARTM = 71;
|
||||
public static final int DELETE = 72;
|
||||
public static final int RESET = 73;
|
||||
public static final int BREAK = 74;
|
||||
public static final int CONTINUE = 75;
|
||||
public static final int NEXTBEG = 76;
|
||||
public static final int NEXTEND = 77;
|
||||
public static final int NEXTFBEG = 78;
|
||||
public static final int NEXTFEND = 79;
|
||||
public static final int PRINTFARG = 80;
|
||||
public static final int PREPST = 81;
|
||||
public static final int GLNCPS = 82;
|
||||
public static final int DIVBY0 = 83;
|
||||
public static final int OPERAND = 84;
|
||||
public static final int POSIDX = 85;
|
||||
public static final int ARGTF = 86;
|
||||
public static final int ARGTM = 87;
|
||||
public static final int FNNONE = 88;
|
||||
public static final int NOTIDX = 89;
|
||||
public static final int NOTDEL = 90;
|
||||
public static final int NOTMAP = 91;
|
||||
public static final int NOTMAPIN = 92;
|
||||
public static final int NOTMAPNILIN = 93;
|
||||
public static final int NOTREF = 94;
|
||||
public static final int NOTASS = 95;
|
||||
public static final int IDXVALASSMAP = 96;
|
||||
public static final int POSVALASSMAP = 97;
|
||||
public static final int MAPTOSCALAR = 98;
|
||||
public static final int SCALARTOMAP = 99;
|
||||
public static final int MAPNOTALLOWED = 100;
|
||||
public static final int VALTYPE = 101;
|
||||
public static final int RDELETE = 102;
|
||||
public static final int RRESET = 103;
|
||||
public static final int RNEXTBEG = 104;
|
||||
public static final int RNEXTEND = 105;
|
||||
public static final int RNEXTFBEG = 106;
|
||||
public static final int RNEXTFEND = 107;
|
||||
public static final int BFNUSER = 108;
|
||||
public static final int BFNIMPL = 109;
|
||||
public static final int IOUSER = 110;
|
||||
public static final int IONONE = 111;
|
||||
public static final int IOIMPL = 112;
|
||||
public static final int IONMEM = 113;
|
||||
public static final int IONMNL = 114;
|
||||
public static final int FMTARG = 115;
|
||||
public static final int FMTCNV = 116;
|
||||
public static final int CONVFMTCHR = 117;
|
||||
public static final int OFMTCHR = 118;
|
||||
public static final int REXRECUR = 119;
|
||||
public static final int REXRPAREN = 120;
|
||||
public static final int REXRBRACKET = 121;
|
||||
public static final int REXRBRACE = 122;
|
||||
public static final int REXUNBALPAR = 123;
|
||||
public static final int REXCOLON = 124;
|
||||
public static final int REXCRANGE = 125;
|
||||
public static final int REXCCLASS = 126;
|
||||
public static final int REXBRANGE = 127;
|
||||
public static final int REXEND = 128;
|
||||
public static final int REXGARBAGE = 129;
|
||||
// end of error codes
|
||||
|
||||
public Exception ()
|
||||
{
|
||||
super ();
|
||||
this.code = NOERR;
|
||||
this.line = 0;
|
||||
}
|
||||
|
||||
public Exception (String msg)
|
||||
{
|
||||
super (msg);
|
||||
this.code = CUSTOM;
|
||||
this.line = 0;
|
||||
}
|
||||
|
||||
public Exception (String msg, int code)
|
||||
{
|
||||
super (msg);
|
||||
this.code = code;
|
||||
this.line = 0;
|
||||
}
|
||||
|
||||
public Exception (String msg, int code, int line)
|
||||
{
|
||||
super (msg);
|
||||
this.code = code;
|
||||
this.line = line;
|
||||
}
|
||||
|
||||
public int getCode ()
|
||||
{
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public int getLine ()
|
||||
{
|
||||
return this.line;
|
||||
}
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
/*
|
||||
* $Id: Extio.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public class Extio
|
||||
{
|
||||
protected static final int TYPE_PIPE = 0;
|
||||
protected static final int TYPE_COPROC = 1;
|
||||
protected static final int TYPE_FILE = 2;
|
||||
protected static final int TYPE_CONSOLE = 3;
|
||||
|
||||
/* PROBLEMS WITH GCJ 3.4.6 if these fields are protected.
|
||||
*
|
||||
protected static final int MODE_PIPE_READ = 0;
|
||||
protected static final int MODE_PIPE_WRITE = 1;
|
||||
|
||||
protected static final int MODE_FILE_READ = 0;
|
||||
protected static final int MODE_FILE_WRITE = 1;
|
||||
protected static final int MODE_FILE_APPEND = 2;
|
||||
|
||||
protected static final int MODE_CONSOLE_READ = 0;
|
||||
protected static final int MODE_CONSOLE_WRITE = 1;
|
||||
*/
|
||||
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 long run_id;
|
||||
private Object handle;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getName ()
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public int getType ()
|
||||
{
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public int getMode ()
|
||||
{
|
||||
return this.mode;
|
||||
}
|
||||
|
||||
public long getRunId ()
|
||||
{
|
||||
return this.run_id;
|
||||
}
|
||||
|
||||
public void setHandle (Object handle)
|
||||
{
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
public Object getHandle ()
|
||||
{
|
||||
return this.handle;
|
||||
}
|
||||
};
|
@ -1,20 +0,0 @@
|
||||
/*
|
||||
* $Id: File.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public class File extends IO
|
||||
{
|
||||
public static final int MODE_READ = Extio.MODE_FILE_READ;
|
||||
public static final int MODE_WRITE = Extio.MODE_FILE_WRITE;
|
||||
public static final int MODE_APPEND = Extio.MODE_FILE_APPEND;
|
||||
|
||||
protected File (Awk awk, Extio extio)
|
||||
{
|
||||
super (awk, extio);
|
||||
}
|
||||
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
/*
|
||||
* $Id: IO.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
class IO
|
||||
{
|
||||
protected Awk awk;
|
||||
protected Extio extio;
|
||||
protected Object handle;
|
||||
|
||||
protected IO (Awk awk, Extio extio)
|
||||
{
|
||||
this.awk = awk;
|
||||
this.extio = extio;
|
||||
}
|
||||
|
||||
public String getName ()
|
||||
{
|
||||
return extio.getName();
|
||||
}
|
||||
|
||||
public int getMode ()
|
||||
{
|
||||
return extio.getMode();
|
||||
}
|
||||
|
||||
public long getRunId ()
|
||||
{
|
||||
return extio.getRunId();
|
||||
}
|
||||
|
||||
public void setHandle (Object handle)
|
||||
{
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
public Object getHandle ()
|
||||
{
|
||||
return handle;
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
/*
|
||||
* $Id: Pipe.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public class Pipe extends IO
|
||||
{
|
||||
public static final int MODE_READ = Extio.MODE_PIPE_READ;
|
||||
public static final int MODE_WRITE = Extio.MODE_PIPE_WRITE;
|
||||
|
||||
protected Pipe (Awk awk, Extio extio)
|
||||
{
|
||||
super (awk, extio);
|
||||
}
|
||||
}
|
@ -1,184 +0,0 @@
|
||||
/*
|
||||
* $Id: Return.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* ${License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public class Return implements Clearable
|
||||
{
|
||||
protected long runid;
|
||||
protected long valid;
|
||||
|
||||
/* An instance of the Return class should not be used
|
||||
* outside the context where it is availble. When it is
|
||||
* referenced that way, the setXXX methods may cause
|
||||
* JVM to crash */
|
||||
|
||||
public Return (Context ctx)
|
||||
{
|
||||
ctx.pushClearable (this);
|
||||
this.runid = ctx.getId();
|
||||
this.valid = 0;
|
||||
}
|
||||
|
||||
Return (long runid, long valid)
|
||||
{
|
||||
this.runid = runid;
|
||||
this.valid = valid;
|
||||
}
|
||||
|
||||
public boolean isIndexed ()
|
||||
{
|
||||
if (this.runid == 0) return false;
|
||||
return isindexed (this.runid, this.valid);
|
||||
}
|
||||
|
||||
public void setIntValue (long v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setintval (this.runid, this.valid, v);
|
||||
}
|
||||
|
||||
public void setIntValue (int v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setintval (this.runid, this.valid, (long)v);
|
||||
}
|
||||
|
||||
public void setIntValue (short v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setintval (this.runid, this.valid, (long)v);
|
||||
}
|
||||
|
||||
public void setIntValue (byte v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setintval (this.runid, this.valid, (long)v);
|
||||
}
|
||||
|
||||
public void setRealValue (double v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setrealval (this.runid, this.valid, v);
|
||||
}
|
||||
|
||||
public void setRealValue (float v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setrealval (this.runid, this.valid, (double)v);
|
||||
}
|
||||
|
||||
public void setStringValue (String v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setstrval (this.runid, this.valid, v);
|
||||
}
|
||||
|
||||
public void setIndexedIntValue (String index, long v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedintval (this.runid, this.valid, index, v);
|
||||
}
|
||||
|
||||
public void setIndexedIntValue (String index, int v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedintval (this.runid, this.valid, index, (long)v);
|
||||
}
|
||||
|
||||
public void setIndexedIntValue (String index, short v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedintval (this.runid, this.valid, index, (long)v);
|
||||
}
|
||||
|
||||
public void setIndexedIntValue (String index, byte v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedintval (this.runid, this.valid, index, (long)v);
|
||||
}
|
||||
|
||||
public void setIndexedRealValue (String index, double v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedrealval (this.runid, this.valid, index, v);
|
||||
}
|
||||
|
||||
public void setIndexedRealValue (String index, float v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedrealval (this.runid, this.valid, index, (double)v);
|
||||
}
|
||||
|
||||
public void setIndexedStringValue (String index, String v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedstrval (this.runid, this.valid, index, v);
|
||||
}
|
||||
|
||||
public void setIndexedIntValue (long index, long v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedintval (this.runid, this.valid, Long.toString(index), v);
|
||||
}
|
||||
|
||||
public void setIndexedIntValue (long index, int v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedintval (this.runid, this.valid, Long.toString(index), (long)v);
|
||||
}
|
||||
|
||||
public void setIndexedIntValue (long index, short v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedintval (this.runid, this.valid, Long.toString(index), (long)v);
|
||||
}
|
||||
|
||||
public void setIndexedIntValue (long index, byte v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedintval (this.runid, this.valid, Long.toString(index), (long)v);
|
||||
}
|
||||
|
||||
public void setIndexedRealValue (long index, double v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedrealval (this.runid, this.valid, Long.toString(index), v);
|
||||
}
|
||||
|
||||
public void setIndexedRealValue (long index, float v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedrealval (this.runid, this.valid, Long.toString(index), (double)v);
|
||||
}
|
||||
|
||||
public void setIndexedStringValue (long index, String v)
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
setindexedstrval (this.runid, this.valid, Long.toString(index), v);
|
||||
}
|
||||
|
||||
public void clear ()
|
||||
{
|
||||
if (this.runid == 0) return;
|
||||
clearval (this.runid, this.valid);
|
||||
}
|
||||
|
||||
protected native boolean isindexed (long runid, long valid);
|
||||
|
||||
protected native void setintval (long runid, long valid, long v);
|
||||
protected native void setrealval (long runid, long valid, double v);
|
||||
protected native void setstrval (long runid, long valid, String v);
|
||||
|
||||
protected native void setindexedintval (
|
||||
long runid, long valid, String index, long v);
|
||||
protected native void setindexedrealval (
|
||||
long runid, long valid, String index, double v);
|
||||
protected native void setindexedstrval (
|
||||
long runid, long valid, String index, String v);
|
||||
|
||||
protected native void clearval (long runid, long valid);
|
||||
}
|
@ -1,451 +0,0 @@
|
||||
/*
|
||||
* $Id: StdAwk.java 115 2008-03-03 11:13:15Z baconevi $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Extends the core interpreter engine to implement the language closer to
|
||||
* the standard.
|
||||
*/
|
||||
public abstract class StdAwk extends Awk
|
||||
{
|
||||
private long seed;
|
||||
private java.util.Random random;
|
||||
|
||||
public StdAwk () throws Exception
|
||||
{
|
||||
super ();
|
||||
|
||||
seed = System.currentTimeMillis();
|
||||
random = new java.util.Random (seed);
|
||||
|
||||
addFunction ("sin", 1, 1);
|
||||
addFunction ("cos", 1, 1);
|
||||
addFunction ("tan", 1, 1);
|
||||
addFunction ("atan", 1, 1);
|
||||
addFunction ("atan2", 2, 2);
|
||||
addFunction ("log", 1, 1);
|
||||
addFunction ("exp", 1, 1);
|
||||
addFunction ("sqrt", 1, 1);
|
||||
addFunction ("int", 1, 1, "bfnint");
|
||||
|
||||
addFunction ("srand", 0, 1);
|
||||
addFunction ("rand", 0, 0);
|
||||
|
||||
addFunction ("systime", 0, 0);
|
||||
addFunction ("strftime", 0, 2);
|
||||
addFunction ("strfgmtime", 0, 2);
|
||||
|
||||
addFunction ("system", 1, 1);
|
||||
}
|
||||
|
||||
/* == file interface == */
|
||||
protected int openFile (File file)
|
||||
{
|
||||
int mode = file.getMode();
|
||||
|
||||
if (mode == File.MODE_READ)
|
||||
{
|
||||
FileInputStream fis;
|
||||
|
||||
try { fis = new FileInputStream (file.getName()); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
Reader rd = new BufferedReader (
|
||||
new InputStreamReader (fis));
|
||||
file.setHandle (rd);
|
||||
return 1;
|
||||
}
|
||||
else if (mode == File.MODE_WRITE)
|
||||
{
|
||||
FileOutputStream fos;
|
||||
|
||||
try { fos = new FileOutputStream (file.getName()); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
Writer wr = new BufferedWriter (
|
||||
new OutputStreamWriter (fos));
|
||||
file.setHandle (wr);
|
||||
return 1;
|
||||
}
|
||||
else if (mode == File.MODE_APPEND)
|
||||
{
|
||||
FileOutputStream fos;
|
||||
|
||||
try { fos = new FileOutputStream (file.getName(), true); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
Writer wr = new BufferedWriter (
|
||||
new OutputStreamWriter (fos));
|
||||
file.setHandle (wr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int closeFile (File file)
|
||||
{
|
||||
int mode = file.getMode();
|
||||
|
||||
if (mode == File.MODE_READ)
|
||||
{
|
||||
Reader isr = (Reader)file.getHandle();
|
||||
try { isr.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
else if (mode == File.MODE_WRITE)
|
||||
{
|
||||
Writer osw = (Writer)file.getHandle();
|
||||
try { osw.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
else if (mode == File.MODE_APPEND)
|
||||
{
|
||||
Writer osw = (Writer)file.getHandle();
|
||||
try { osw.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int readFile (File file, char[] buf, int len)
|
||||
{
|
||||
int mode = file.getMode();
|
||||
|
||||
if (mode == File.MODE_READ)
|
||||
{
|
||||
Reader rd = (Reader)file.getHandle();
|
||||
|
||||
try
|
||||
{
|
||||
len = rd.read (buf, 0, len);
|
||||
if (len == -1) return 0;
|
||||
}
|
||||
catch (IOException e) { return -1; }
|
||||
return len;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int writeFile (File file, char[] buf, int len)
|
||||
{
|
||||
int mode = file.getMode();
|
||||
|
||||
if (mode == File.MODE_WRITE ||
|
||||
mode == File.MODE_APPEND)
|
||||
{
|
||||
Writer wr = (Writer)file.getHandle();
|
||||
try { wr.write (buf, 0, len); }
|
||||
catch (IOException e) { return -1; }
|
||||
return len;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int flushFile (File file)
|
||||
{
|
||||
int mode = file.getMode ();
|
||||
|
||||
if (mode == File.MODE_WRITE ||
|
||||
mode == File.MODE_APPEND)
|
||||
{
|
||||
Writer wr = (Writer)file.getHandle ();
|
||||
try { wr.flush (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private class RWE
|
||||
{
|
||||
public Writer wr;
|
||||
public Reader rd;
|
||||
public Reader er;
|
||||
|
||||
public RWE (Writer wr, Reader rd, Reader er)
|
||||
{
|
||||
this.wr = wr;
|
||||
this.rd = rd;
|
||||
this.er = er;
|
||||
}
|
||||
};
|
||||
|
||||
/* == pipe interface == */
|
||||
protected int openPipe (Pipe pipe)
|
||||
{
|
||||
int mode = pipe.getMode();
|
||||
|
||||
if (mode == Pipe.MODE_READ)
|
||||
{
|
||||
Process proc;
|
||||
|
||||
try { proc = popen (pipe.getName()); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
Reader rd = new BufferedReader (
|
||||
new InputStreamReader (proc.getInputStream()));
|
||||
|
||||
pipe.setHandle (rd);
|
||||
return 1;
|
||||
}
|
||||
else if (mode == Pipe.MODE_WRITE)
|
||||
{
|
||||
Process proc;
|
||||
|
||||
try { proc = popen (pipe.getName()); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
Writer wr = new BufferedWriter (
|
||||
new OutputStreamWriter (proc.getOutputStream()));
|
||||
Reader rd = new BufferedReader (
|
||||
new InputStreamReader (proc.getInputStream()));
|
||||
Reader er = new BufferedReader (
|
||||
new InputStreamReader (proc.getErrorStream()));
|
||||
|
||||
pipe.setHandle (new RWE (wr, rd, er));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int closePipe (Pipe pipe)
|
||||
{
|
||||
int mode = pipe.getMode();
|
||||
|
||||
if (mode == Pipe.MODE_READ)
|
||||
{
|
||||
Reader rd = (Reader)pipe.getHandle();
|
||||
try { rd.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
else if (mode == Pipe.MODE_WRITE)
|
||||
{
|
||||
//Writer wr = (Writer)pipe.getHandle();
|
||||
RWE rwe = (RWE)pipe.getHandle();
|
||||
|
||||
try { rwe.wr.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
char[] buf = new char[256];
|
||||
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int len = rwe.rd.read (buf, 0, buf.length);
|
||||
if (len == -1) break;
|
||||
System.out.print (new String (buf, 0, len));
|
||||
}
|
||||
|
||||
System.out.flush ();
|
||||
}
|
||||
catch (IOException e) {}
|
||||
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int len = rwe.er.read (buf, 0, buf.length);
|
||||
if (len == -1) break;
|
||||
System.err.print (new String (buf, 0, len));
|
||||
}
|
||||
|
||||
System.err.flush ();
|
||||
}
|
||||
catch (IOException e) {}
|
||||
|
||||
try { rwe.rd.close (); } catch (IOException e) {}
|
||||
try { rwe.er.close (); } catch (IOException e) {}
|
||||
|
||||
pipe.setHandle (null);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int readPipe (Pipe pipe, char[] buf, int len)
|
||||
{
|
||||
int mode = pipe.getMode();
|
||||
|
||||
if (mode == Pipe.MODE_READ)
|
||||
{
|
||||
Reader rd = (Reader)pipe.getHandle();
|
||||
try
|
||||
{
|
||||
len = rd.read (buf, 0, len);
|
||||
if (len == -1) len = 0;
|
||||
}
|
||||
catch (IOException e) { len = -1; }
|
||||
return len;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int writePipe (Pipe pipe, char[] buf, int len)
|
||||
{
|
||||
int mode = pipe.getMode();
|
||||
|
||||
if (mode == Pipe.MODE_WRITE)
|
||||
{
|
||||
//Writer wr = (Writer)pipe.getHandle ();
|
||||
RWE rw = (RWE)pipe.getHandle();
|
||||
try
|
||||
{
|
||||
rw.wr.write (buf, 0, len);
|
||||
rw.wr.flush ();
|
||||
}
|
||||
catch (IOException e) { return -1; }
|
||||
return len;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int flushPipe (Pipe pipe)
|
||||
{
|
||||
int mode = pipe.getMode ();
|
||||
|
||||
if (mode == Pipe.MODE_WRITE)
|
||||
{
|
||||
Writer wr = (Writer)pipe.getHandle ();
|
||||
try { wr.flush (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* == arithmetic built-in functions */
|
||||
public void sin (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setRealValue (Math.sin(args[0].getRealValue()));
|
||||
}
|
||||
|
||||
public void cos (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setRealValue (Math.cos(args[0].getRealValue()));
|
||||
}
|
||||
|
||||
public void tan (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setRealValue (Math.tan(args[0].getRealValue()));
|
||||
}
|
||||
|
||||
public void atan (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setRealValue (Math.atan(args[0].getRealValue()));
|
||||
}
|
||||
|
||||
public void atan2 (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
double y = args[0].getRealValue();
|
||||
double x = args[1].getRealValue();
|
||||
ret.setRealValue (Math.atan2(y,x));
|
||||
}
|
||||
|
||||
public void log (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setRealValue (Math.log(args[0].getRealValue()));
|
||||
}
|
||||
|
||||
public void exp (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setRealValue (Math.exp(args[0].getRealValue()));
|
||||
}
|
||||
|
||||
public void sqrt (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setRealValue (Math.sqrt(args[0].getRealValue()));
|
||||
}
|
||||
|
||||
public void bfnint (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setIntValue (args[0].getIntValue());
|
||||
}
|
||||
|
||||
public void rand (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
ret.setRealValue (random.nextDouble ());
|
||||
}
|
||||
|
||||
public void srand (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
long prev_seed = seed;
|
||||
|
||||
seed = (args == null || args.length == 0)?
|
||||
System.currentTimeMillis ():
|
||||
args[0].getIntValue();
|
||||
|
||||
random.setSeed (seed);
|
||||
ret.setIntValue (prev_seed);
|
||||
}
|
||||
|
||||
public void systime (Context ctx, String name, Return ret, Argument[] args)
|
||||
{
|
||||
long msec = System.currentTimeMillis ();
|
||||
ret.setIntValue (msec / 1000);
|
||||
}
|
||||
|
||||
public void strftime (Context ctx, String name, Return ret, Argument[] args) throws Exception
|
||||
{
|
||||
String fmt = (args.length<1)? "%c": args[0].getStringValue();
|
||||
long t = (args.length<2)? (System.currentTimeMillis()/1000): args[1].getIntValue();
|
||||
ret.setStringValue (strftime (fmt, t));
|
||||
}
|
||||
|
||||
public void strfgmtime (Context ctx, String name, Return ret, Argument[] args) throws Exception
|
||||
{
|
||||
String fmt = (args.length<1)? "%c": args[0].getStringValue();
|
||||
long t = (args.length<2)? (System.currentTimeMillis()/1000): args[1].getIntValue();
|
||||
ret.setStringValue (strfgmtime (fmt, t));
|
||||
}
|
||||
|
||||
/* miscellaneous built-in functions */
|
||||
public void system (Context ctx, String name, Return ret, Argument[] args) throws Exception
|
||||
{
|
||||
ret.setIntValue (system (args[0].getStringValue()));
|
||||
}
|
||||
|
||||
/* == utility functions == */
|
||||
private Process popen (String command) throws IOException
|
||||
{
|
||||
String full;
|
||||
|
||||
/* TODO: consider OS names and versions */
|
||||
full = System.getenv ("ComSpec");
|
||||
if (full != null)
|
||||
{
|
||||
full = full + " /c " + command;
|
||||
}
|
||||
else
|
||||
{
|
||||
full = System.getenv ("SHELL");
|
||||
if (full != null)
|
||||
{
|
||||
full = "/bin/sh -c \"" + command + "\"";
|
||||
}
|
||||
else full = command;
|
||||
}
|
||||
|
||||
return Runtime.getRuntime().exec (full);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user