Recovered from cvs revision 2007-05-27 05:11:00
This commit is contained in:
187
ase/awk/Awk.java
187
ase/awk/Awk.java
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: Awk.java,v 1.8 2007/05/25 14:41:48 bacon Exp $
|
||||
* $Id: Awk.java,v 1.10 2007/05/26 10:52:48 bacon Exp $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
@ -50,15 +50,14 @@ public abstract class Awk
|
||||
public Awk () throws Exception
|
||||
{
|
||||
this.handle = 0;
|
||||
|
||||
open ();
|
||||
}
|
||||
|
||||
/* == just in case == */
|
||||
protected void finalize () throws Throwable
|
||||
{
|
||||
super.finalize ();
|
||||
if (handle != 0) close ();
|
||||
super.finalize ();
|
||||
}
|
||||
|
||||
/* == native methods == */
|
||||
@ -80,9 +79,9 @@ public abstract class Awk
|
||||
String name, int min_args, int max_args) throws Exception;
|
||||
private native void delbfn (String name) throws Exception;
|
||||
|
||||
private native void setfilename (
|
||||
native void setfilename (
|
||||
long runid, String name) throws Exception;
|
||||
private native void setofilename (
|
||||
native void setofilename (
|
||||
long runid, String name) throws Exception;
|
||||
|
||||
private native Object strtonum (
|
||||
@ -266,21 +265,51 @@ public abstract class Awk
|
||||
/* == external io interface == */
|
||||
protected int openExtio (Extio extio)
|
||||
{
|
||||
int type = extio.getType ();
|
||||
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);
|
||||
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)
|
||||
{
|
||||
int type = extio.getType ();
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
@ -292,15 +321,28 @@ public abstract class Awk
|
||||
// the end of the stream.
|
||||
if (len <= 0) return -1;
|
||||
|
||||
int type = extio.getType ();
|
||||
if (type == Extio.TYPE_CONSOLE)
|
||||
return readConsole (extio, buf, len);
|
||||
if (type == Extio.TYPE_FILE)
|
||||
return readFile (extio, buf, len);
|
||||
if (type == Extio.TYPE_PIPE)
|
||||
return readPipe (extio, buf, len);
|
||||
//if (type == Extio.TYPE_COPROC)
|
||||
// return readCoproc (extio, buf, len);
|
||||
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;
|
||||
}
|
||||
|
||||
@ -308,51 +350,86 @@ public abstract class Awk
|
||||
{
|
||||
if (len <= 0) return -1;
|
||||
|
||||
int type = extio.getType ();
|
||||
if (type == Extio.TYPE_CONSOLE)
|
||||
return writeConsole (extio, buf, len);
|
||||
if (type == Extio.TYPE_FILE)
|
||||
return writeFile (extio, buf, len);
|
||||
if (type == Extio.TYPE_PIPE)
|
||||
return writePipe (extio, buf, len);
|
||||
//if (type == Extio.TYPE_COPROC)
|
||||
// return writeCoproc (extio, buf, len);
|
||||
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)
|
||||
{
|
||||
int type = extio.getType ();
|
||||
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);
|
||||
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 ();
|
||||
if (type == Extio.TYPE_CONSOLE) return nextConsole (extio);
|
||||
|
||||
switch (extio.getType())
|
||||
{
|
||||
case Extio.TYPE_CONSOLE:
|
||||
{
|
||||
return nextConsole ((Console)extio.getHandle());
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
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 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 (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 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 (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);
|
||||
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);
|
||||
}
|
||||
|
28
ase/awk/Console.java
Normal file
28
ase/awk/Console.java
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* $Id: Console.java,v 1.2 2007/05/26 10:52:48 bacon Exp $
|
||||
*/
|
||||
|
||||
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,27 +1,27 @@
|
||||
/*
|
||||
* $Id: Extio.java,v 1.3 2007/04/30 05:47:33 bacon Exp $
|
||||
* $Id: Extio.java,v 1.5 2007/05/26 10:52:48 bacon Exp $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
|
||||
package ase.awk;
|
||||
|
||||
public class Extio
|
||||
class Extio
|
||||
{
|
||||
public static final int TYPE_PIPE = 0;
|
||||
public static final int TYPE_COPROC = 1;
|
||||
public static final int TYPE_FILE = 2;
|
||||
public static final int TYPE_CONSOLE = 3;
|
||||
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;
|
||||
|
||||
public static final int MODE_PIPE_READ = 0;
|
||||
public static final int MODE_PIPE_WRITE = 1;
|
||||
protected static final int MODE_PIPE_READ = 0;
|
||||
protected 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;
|
||||
protected static final int MODE_FILE_READ = 0;
|
||||
protected static final int MODE_FILE_WRITE = 1;
|
||||
protected static final int MODE_FILE_APPEND = 2;
|
||||
|
||||
public static final int MODE_CONSOLE_READ = 0;
|
||||
public static final int MODE_CONSOLE_WRITE = 1;
|
||||
protected static final int MODE_CONSOLE_READ = 0;
|
||||
protected static final int MODE_CONSOLE_WRITE = 1;
|
||||
|
||||
private String name;
|
||||
private int type;
|
||||
@ -67,9 +67,4 @@ public class Extio
|
||||
{
|
||||
return this.handle;
|
||||
}
|
||||
|
||||
protected void finalize () throws Throwable
|
||||
{
|
||||
super.finalize ();
|
||||
}
|
||||
};
|
||||
|
18
ase/awk/File.java
Normal file
18
ase/awk/File.java
Normal file
@ -0,0 +1,18 @@
|
||||
/*
|
||||
* $Id: File.java,v 1.2 2007/05/26 10:52:48 bacon Exp $
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
44
ase/awk/IO.java
Normal file
44
ase/awk/IO.java
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* $Id: IO.java,v 1.1 2007/05/26 10:52:48 bacon Exp $
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
16
ase/awk/Pipe.java
Normal file
16
ase/awk/Pipe.java
Normal file
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* $Id: Pipe.java,v 1.2 2007/05/26 10:52:48 bacon Exp $
|
||||
*/
|
||||
|
||||
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,5 +1,5 @@
|
||||
/*
|
||||
* $Id: StdAwk.java,v 1.9 2007/05/25 14:41:48 bacon Exp $
|
||||
* $Id: StdAwk.java,v 1.10 2007/05/26 10:23:52 bacon Exp $
|
||||
*
|
||||
* {License}
|
||||
*/
|
||||
@ -40,139 +40,124 @@ public abstract class StdAwk extends Awk
|
||||
}
|
||||
|
||||
/* == file interface == */
|
||||
protected int openFile (Extio extio)
|
||||
protected int openFile (File file)
|
||||
{
|
||||
int mode = extio.getMode();
|
||||
int mode = file.getMode();
|
||||
|
||||
if (mode == Extio.MODE_FILE_READ)
|
||||
if (mode == File.MODE_READ)
|
||||
{
|
||||
FileInputStream fis;
|
||||
Reader isr;
|
||||
|
||||
try { fis = new FileInputStream (extio.getName()); }
|
||||
try { fis = new FileInputStream (file.getName()); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
isr = new BufferedReader (new InputStreamReader (fis));
|
||||
extio.setHandle (isr);
|
||||
Reader rd = new BufferedReader (
|
||||
new InputStreamReader (fis));
|
||||
file.setHandle (rd);
|
||||
return 1;
|
||||
}
|
||||
else if (mode == Extio.MODE_FILE_WRITE)
|
||||
else if (mode == File.MODE_WRITE)
|
||||
{
|
||||
FileOutputStream fos;
|
||||
Writer osw;
|
||||
|
||||
try { fos = new FileOutputStream (extio.getName()); }
|
||||
try { fos = new FileOutputStream (file.getName()); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
osw = new BufferedWriter (new OutputStreamWriter (fos));
|
||||
extio.setHandle (osw);
|
||||
Writer wr = new BufferedWriter (
|
||||
new OutputStreamWriter (fos));
|
||||
file.setHandle (wr);
|
||||
return 1;
|
||||
}
|
||||
else if (mode == Extio.MODE_FILE_APPEND)
|
||||
else if (mode == File.MODE_APPEND)
|
||||
{
|
||||
FileOutputStream fos;
|
||||
Writer osw;
|
||||
|
||||
try { fos = new FileOutputStream (extio.getName(), true); }
|
||||
try { fos = new FileOutputStream (file.getName(), true); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
osw = new BufferedWriter (new OutputStreamWriter (fos));
|
||||
extio.setHandle (osw);
|
||||
Writer wr = new BufferedWriter (
|
||||
new OutputStreamWriter (fos));
|
||||
file.setHandle (wr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int closeFile (Extio extio)
|
||||
protected int closeFile (File file)
|
||||
{
|
||||
int mode = extio.getMode();
|
||||
int mode = file.getMode();
|
||||
|
||||
if (mode == Extio.MODE_FILE_READ)
|
||||
if (mode == File.MODE_READ)
|
||||
{
|
||||
Reader isr;
|
||||
isr = (Reader)extio.getHandle();
|
||||
if (isr != null)
|
||||
{
|
||||
try { isr.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
}
|
||||
Reader isr = (Reader)file.getHandle();
|
||||
try { isr.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
else if (mode == Extio.MODE_FILE_WRITE)
|
||||
else if (mode == File.MODE_WRITE)
|
||||
{
|
||||
Writer osw;
|
||||
osw = (Writer)extio.getHandle();
|
||||
if (osw != null)
|
||||
{
|
||||
try { osw.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
}
|
||||
Writer osw = (Writer)file.getHandle();
|
||||
try { osw.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
else if (mode == Extio.MODE_FILE_APPEND)
|
||||
else if (mode == File.MODE_APPEND)
|
||||
{
|
||||
Writer osw;
|
||||
osw = (Writer)extio.getHandle();
|
||||
if (osw != null)
|
||||
{
|
||||
try { osw.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
}
|
||||
Writer osw = (Writer)file.getHandle();
|
||||
try { osw.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int readFile (Extio extio, char[] buf, int len)
|
||||
protected int readFile (File file, char[] buf, int len)
|
||||
{
|
||||
int mode = extio.getMode();
|
||||
int mode = file.getMode();
|
||||
|
||||
if (mode == Extio.MODE_FILE_READ)
|
||||
if (mode == File.MODE_READ)
|
||||
{
|
||||
Reader isr;
|
||||
isr = (Reader)extio.getHandle();
|
||||
Reader rd = (Reader)file.getHandle();
|
||||
|
||||
try
|
||||
{
|
||||
len = isr.read (buf, 0, len);
|
||||
if (len == -1) len = 0;
|
||||
len = rd.read (buf, 0, len);
|
||||
if (len == -1) return 0;
|
||||
}
|
||||
catch (IOException e) { len = -1; }
|
||||
catch (IOException e) { return -1; }
|
||||
return len;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int writeFile (Extio extio, char[] buf, int len)
|
||||
protected int writeFile (File file, char[] buf, int len)
|
||||
{
|
||||
int mode = extio.getMode();
|
||||
int mode = file.getMode();
|
||||
|
||||
if (mode == Extio.MODE_FILE_WRITE ||
|
||||
mode == Extio.MODE_FILE_APPEND)
|
||||
if (mode == File.MODE_WRITE ||
|
||||
mode == File.MODE_APPEND)
|
||||
{
|
||||
Writer osw;
|
||||
osw = (Writer)extio.getHandle();
|
||||
try { osw.write (buf, 0, len); }
|
||||
catch (IOException e) { len = -1; }
|
||||
Writer wr = (Writer)file.getHandle();
|
||||
try { wr.write (buf, 0, len); }
|
||||
catch (IOException e) { return -1; }
|
||||
return len;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int flushFile (Extio extio)
|
||||
protected int flushFile (File file)
|
||||
{
|
||||
int mode = extio.getMode ();
|
||||
int mode = file.getMode ();
|
||||
|
||||
if (mode == Extio.MODE_FILE_WRITE ||
|
||||
mode == Extio.MODE_FILE_APPEND)
|
||||
if (mode == File.MODE_WRITE ||
|
||||
mode == File.MODE_APPEND)
|
||||
{
|
||||
Writer osw;
|
||||
osw = (Writer)extio.getHandle ();
|
||||
try { osw.flush (); }
|
||||
Writer wr = (Writer)file.getHandle ();
|
||||
try { wr.flush (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
@ -180,82 +165,126 @@ public abstract class StdAwk extends Awk
|
||||
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 (Extio extio)
|
||||
protected int openPipe (Pipe pipe)
|
||||
{
|
||||
int mode = extio.getMode();
|
||||
int mode = pipe.getMode();
|
||||
|
||||
if (mode == Extio.MODE_PIPE_READ)
|
||||
if (mode == Pipe.MODE_READ)
|
||||
{
|
||||
|
||||
Process proc;
|
||||
Reader isr;
|
||||
|
||||
try { proc = popen (extio.getName()); }
|
||||
try { proc = popen (pipe.getName()); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
isr = new BufferedReader (new InputStreamReader (proc.getInputStream()));
|
||||
extio.setHandle (isr);
|
||||
Reader rd = new BufferedReader (
|
||||
new InputStreamReader (proc.getInputStream()));
|
||||
|
||||
pipe.setHandle (rd);
|
||||
return 1;
|
||||
}
|
||||
else if (mode == Extio.MODE_PIPE_WRITE)
|
||||
else if (mode == Pipe.MODE_WRITE)
|
||||
{
|
||||
Process proc;
|
||||
Writer osw;
|
||||
|
||||
try { proc = popen (extio.getName()); }
|
||||
try { proc = popen (pipe.getName()); }
|
||||
catch (IOException e) { return -1; }
|
||||
|
||||
osw = new BufferedWriter (new OutputStreamWriter (proc.getOutputStream()));
|
||||
extio.setHandle (osw);
|
||||
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 (Extio extio)
|
||||
protected int closePipe (Pipe pipe)
|
||||
{
|
||||
int mode = extio.getMode();
|
||||
int mode = pipe.getMode();
|
||||
|
||||
if (mode == Extio.MODE_PIPE_READ)
|
||||
if (mode == Pipe.MODE_READ)
|
||||
{
|
||||
Reader isr;
|
||||
isr = (Reader)extio.getHandle();
|
||||
if (isr != null)
|
||||
{
|
||||
try { isr.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
}
|
||||
Reader rd = (Reader)pipe.getHandle();
|
||||
try { rd.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
else if (mode == Extio.MODE_PIPE_WRITE)
|
||||
else if (mode == Pipe.MODE_WRITE)
|
||||
{
|
||||
Writer osw;
|
||||
osw = (Writer)extio.getHandle();
|
||||
if (osw != null)
|
||||
//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
|
||||
{
|
||||
try { osw.close (); }
|
||||
catch (IOException e) { return -1; }
|
||||
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 (Extio extio, char[] buf, int len)
|
||||
protected int readPipe (Pipe pipe, char[] buf, int len)
|
||||
{
|
||||
int mode = extio.getMode();
|
||||
int mode = pipe.getMode();
|
||||
|
||||
if (mode == Extio.MODE_PIPE_READ)
|
||||
if (mode == Pipe.MODE_READ)
|
||||
{
|
||||
Reader isr;
|
||||
isr = (Reader)extio.getHandle();
|
||||
|
||||
Reader rd = (Reader)pipe.getHandle();
|
||||
try
|
||||
{
|
||||
len = isr.read (buf, 0, len);
|
||||
len = rd.read (buf, 0, len);
|
||||
if (len == -1) len = 0;
|
||||
}
|
||||
catch (IOException e) { len = -1; }
|
||||
@ -265,31 +294,34 @@ public abstract class StdAwk extends Awk
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected int writePipe (Extio extio, char[] buf, int len)
|
||||
protected int writePipe (Pipe pipe, char[] buf, int len)
|
||||
{
|
||||
int mode = extio.getMode();
|
||||
int mode = pipe.getMode();
|
||||
|
||||
if (mode == Extio.MODE_PIPE_WRITE)
|
||||
if (mode == Pipe.MODE_WRITE)
|
||||
{
|
||||
Writer osw;
|
||||
osw = (Writer)extio.getHandle();
|
||||
try { osw.write (buf, 0, len); }
|
||||
catch (IOException e) { len = -1; }
|
||||
//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 (Extio extio)
|
||||
protected int flushPipe (Pipe pipe)
|
||||
{
|
||||
int mode = extio.getMode ();
|
||||
int mode = pipe.getMode ();
|
||||
|
||||
if (mode == Extio.MODE_PIPE_WRITE)
|
||||
if (mode == Pipe.MODE_WRITE)
|
||||
{
|
||||
Writer osw;
|
||||
osw = (Writer)extio.getHandle ();
|
||||
try { osw.flush (); }
|
||||
Writer wr = (Writer)pipe.getHandle ();
|
||||
try { wr.flush (); }
|
||||
catch (IOException e) { return -1; }
|
||||
return 0;
|
||||
}
|
||||
@ -390,38 +422,6 @@ public abstract class StdAwk extends Awk
|
||||
{
|
||||
String str = builtinFunctionArgumentToString (runid, args[0]);
|
||||
return system (str);
|
||||
|
||||
/*
|
||||
String str = builtinFunctionArgumentToString (runid, args[0]);
|
||||
Process proc = null;
|
||||
int n = 0;
|
||||
|
||||
str = builtinFunctionArgumentToString (runid, args[0]);
|
||||
|
||||
try { proc = popen (str); }
|
||||
catch (IOException e) { n = -1; }
|
||||
|
||||
if (proc != null)
|
||||
{
|
||||
InputStream is;
|
||||
byte[] buf = new byte[1024];
|
||||
|
||||
is = proc.getInputStream();
|
||||
|
||||
// TODO; better error handling... program execution.. io redirection???
|
||||
try { while (is.read (buf) != -1) ; }
|
||||
catch (IOException e) { n = -1; };
|
||||
|
||||
try { n = proc.waitFor (); }
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
proc.destroy ();
|
||||
n = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return new Long (n);
|
||||
*/
|
||||
}
|
||||
|
||||
/* == utility functions == */
|
||||
|
@ -1,5 +1,5 @@
|
||||
#
|
||||
# $Id: makefile.in,v 1.6 2007/05/25 11:49:42 bacon Exp $
|
||||
# $Id: makefile.in,v 1.7 2007/05/26 10:58:38 bacon Exp $
|
||||
#
|
||||
|
||||
NAME = aseawk
|
||||
@ -132,6 +132,18 @@ $(TMP_DIR)/ase/awk/StdAwk.class: StdAwk.java
|
||||
$(TMP_DIR)/ase/awk/Extio.class: Extio.java
|
||||
$(JAVAC) -classpath ../.. -d $(TMP_DIR) Extio.java
|
||||
|
||||
$(TMP_DIR)/ase/awk/IO.class: IO.java
|
||||
$(JAVAC) -classpath ../.. -d $(TMP_DIR) IO.java
|
||||
|
||||
$(TMP_DIR)/ase/awk/Console.class: Console.java
|
||||
$(JAVAC) -classpath ../.. -d $(TMP_DIR) Console.java
|
||||
|
||||
$(TMP_DIR)/ase/awk/File.class: File.java
|
||||
$(JAVAC) -classpath ../.. -d $(TMP_DIR) File.java
|
||||
|
||||
$(TMP_DIR)/ase/awk/Pipe.class: Pipe.java
|
||||
$(JAVAC) -classpath ../.. -d $(TMP_DIR) Pipe.java
|
||||
|
||||
$(TMP_DIR)/ase/awk/Exception.class: Exception.java
|
||||
$(JAVAC) -classpath ../.. -d $(TMP_DIR) Exception.java
|
||||
|
||||
|
Reference in New Issue
Block a user