Recovered from cvs revision 2007-05-26 10:23:00

This commit is contained in:
2007-05-26 21:41:00 +00:00
parent 9c128e5a92
commit 38e1af83e6
12 changed files with 814 additions and 570 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.cpp,v 1.37 2007/05/23 11:43:24 bacon Exp $
* $Id: Awk.cpp,v 1.38 2007/05/25 11:49:42 bacon Exp $
*/
#include <ase/awk/Awk.hpp>
@ -560,13 +560,13 @@ namespace ASE
return ase_awk_getoption (awk);
}
void Awk::setDepth (int id, size_t depth)
void Awk::setMaxDepth (int ids, size_t depth)
{
ASE_ASSERT (awk != ASE_NULL);
ase_awk_setmaxdepth (awk, id, depth);
ase_awk_setmaxdepth (awk, ids, depth);
}
int Awk::getDepth (int id)
int Awk::getMaxDepth (int id)
{
ASE_ASSERT (awk != ASE_NULL);
return ase_awk_getmaxdepth (awk, id);

View File

@ -1,5 +1,5 @@
/*
* $Id: Awk.hpp,v 1.35 2007/05/23 11:43:24 bacon Exp $
* $Id: Awk.hpp,v 1.36 2007/05/25 11:49:42 bacon Exp $
*/
#ifndef _ASE_AWK_AWK_HPP_
@ -406,8 +406,8 @@ namespace ASE
DEPTH_REX_MATCH = ASE_AWK_DEPTH_REX_MATCH
};
virtual void setDepth (int id, size_t depth);
virtual int getDepth (int id);
virtual void setMaxDepth (int ids, size_t depth);
virtual int getMaxDepth (int id);
virtual int parse ();
virtual int run (const char_t* main = ASE_NULL,

View File

@ -1,11 +1,13 @@
/*
* $Id: Awk.java,v 1.6 2007/05/24 06:53:21 bacon Exp $
* $Id: Awk.java,v 1.8 2007/05/25 14:41:48 bacon Exp $
*
* {License}
*/
package ase.awk;
import java.io.*;
public abstract class Awk
{
// mode for open_source & close_source
@ -37,6 +39,12 @@ public abstract class Awk
public static final int OPTION_CRLF = (1 << 13);
public static final int OPTION_ARGSTOMAIN = (1 << 14);
protected final static Reader stdin =
new BufferedReader (new InputStreamReader (System.in));
protected final static Writer stdout =
new BufferedWriter (new OutputStreamWriter (System.out));
private long handle;
public Awk () throws Exception
@ -84,6 +92,7 @@ public abstract class Awk
protected native String strftime (String fmt, long sec);
protected native String strfgmtime (String fmt, long sec);
protected native int system (String cmd);
/* == simpler run methods == */
public void run (String main) throws Exception

View File

@ -1,5 +1,5 @@
/*
* $Id: StdAwk.java,v 1.7 2007/05/24 06:53:21 bacon Exp $
* $Id: StdAwk.java,v 1.9 2007/05/25 14:41:48 bacon Exp $
*
* {License}
*/
@ -10,39 +10,9 @@ import java.io.*;
public abstract class StdAwk extends Awk
{
private Reader src_in = null;
private Writer src_out = null;
private String[] sin = null;
private int sin_no = 0;
private String[] sout = null;
private int sout_no = 0;
private String[] cin = null;
private int cin_no = 0;
private String[] cout = null;
private int cout_no = 0;
private long seed;
private java.util.Random random;
private final static Reader stdin =
new BufferedReader (new InputStreamReader (System.in));
private final static Writer stdout =
new BufferedWriter (new OutputStreamWriter (System.out));
/*
static
{
stdin = new BufferedReader (
new InputStreamReader (System.in));
stdout = new BufferedWriter (
new OutputStreamWriter (System.out));
}
*/
public StdAwk () throws Exception
{
super ();
@ -69,405 +39,6 @@ public abstract class StdAwk extends Awk
addFunction ("system", 1, 1);
}
/* == major methods == */
public void parse () throws Exception
{
sin = sourceInputNames (); sin_no = 0;
sout = sourceOutputNames (); sout_no = 0;
super.parse ();
}
public void run (String main, String[] args) throws Exception
{
cin = consoleInputNames (); cin_no = 0;
cout = consoleOutputNames (); cout_no = 0;
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, null);
}
/* == source code names == */
protected String[] sourceInputNames () { return null; }
protected String[] sourceOutputNames () { return null; }
/* == console names == */
protected String[] consoleInputNames () { return null; }
protected String[] consoleOutputNames () { return null; }
/* == source code == */
protected int openSource (int mode)
{
if (mode == SOURCE_READ)
{
Reader isr;
sin_no = 0;
if (sin == null || sin_no >= sin.length) return 0;
isr = get_stream_reader (sin[sin_no]);
if (isr == null) return -1;
src_in = isr;
sin_no++;
return 1;
}
else if (mode == SOURCE_WRITE)
{
Writer osw;
sout_no = 0;
// when sout is null, the source output is treated
// as if the operation has been successful with
// the writeSource and closeSource method.
if (sout == null) return 1;
if (sout_no >= sin.length) return 0;
osw = get_stream_writer (sout[sout_no]);
if (osw == null) return -1;
src_out = osw;
sout_no++;
return 1;
}
return -1;
}
protected int closeSource (int mode)
{
if (mode == SOURCE_READ)
{
if (src_in != StdAwk.stdin)
{
try { src_in.close (); }
catch (IOException e) { return -1; }
}
return 0;
}
else if (mode == SOURCE_WRITE)
{
if (src_out == null) return 0;
if (src_out != StdAwk.stdout)
{
try { src_out.close (); }
catch (IOException e) { return -1; }
}
return 0;
}
return -1;
}
protected int readSource (char[] buf, int len)
{
try {
int n = src_in.read (buf, 0, len);
while (n == -1)
{
Reader isr;
if (sin == null || sin_no >= sin.length) return 0;
isr = get_stream_reader (sin[sin_no]);
if (isr == null) return -1;
if (src_in != StdAwk.stdin)
{
try { src_in.close (); }
catch (IOException ec) { /* ignore */ }
}
src_in = isr;
sin_no++;
n = src_in.read (buf, 0, len);
}
return n;
}
catch (IOException e)
{
return -1;
}
}
protected int writeSource (char[] buf, int len)
{
if (src_out == null) return len;
// only the first file is used at the moment.
// this is because the write message doesn't indicate
// the end of the output stream.
try { src_out.write (buf, 0, len); }
catch (IOException e) { return -1; }
return len;
}
/* == console interface == */
protected int openConsole (Extio extio)
{
//System.err.println ("[openConsole called.... name: " + extio.getName() + " mode: " + extio.getMode());
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_READ)
{
/*InputStream*/Reader isr;
cin_no = 0;
if (cin == null || cin_no >= cin.length) return 0;
isr = get_stream_reader (cin[cin_no]);
if (isr == null) return -1;
extio.setHandle (isr);
try { setConsoleInputName (extio, cin[cin_no]); }
catch (Exception e) { return -1; }
cin_no++;
return 1;
}
else if (mode == Extio.MODE_CONSOLE_WRITE)
{
Writer osw;
cout_no = 0;
if (cout == null || cout_no >= cout.length) return 0;
osw = get_stream_writer (cout[cout_no]);
if (osw == null) return -1;
extio.setHandle (osw);
try { setConsoleOutputName (extio, cout[cout_no]); }
catch (Exception e) { return -1; }
cout_no++;
return 1;
}
return -1;
}
protected int closeConsole (Extio extio)
{
//System.err.println ("[closeConsole called.... name: " + extio.getName() + " mode: " + extio.getMode());
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_READ)
{
Reader isr = (Reader)extio.getHandle ();
if (isr != StdAwk.stdin)
{
try { isr.close (); }
catch (IOException e) { return -1; }
}
return 0;
}
else if (mode == Extio.MODE_CONSOLE_WRITE)
{
Writer osw = (Writer)extio.getHandle ();
/* TODO: selective close the stream...
* system.out should not be closed??? */
if (osw != StdAwk.stdout)
{
try { osw.close (); }
catch (IOException e) { return -1; }
}
return 0;
}
return -1;
}
protected int readConsole (Extio extio, char[] buf, int len)
{
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_READ)
{
Reader isr, tmp;
int n;
isr = (Reader)extio.getHandle ();
try { n = isr.read (buf, 0, len); }
catch (IOException e) { return -1; }
while (n == -1)
{
if (cin == null || cin_no >= cin.length) return 0;
tmp = get_stream_reader (cin[cin_no]);
if (tmp == null) return -1;
if (isr != StdAwk.stdin)
{
try { isr.close (); }
catch (IOException e) { /* ignore */ }
}
extio.setHandle (tmp);
try { setConsoleInputName (extio, cin[cin_no]); }
catch (Exception e) { return -1; }
isr = (Reader)extio.getHandle ();
cin_no++;
try { n = isr.read (buf, 0, len); }
catch (IOException e) { return -1; }
}
return n;
}
return -1;
}
protected int writeConsole (Extio extio, char[] buf, int len)
{
int mode = extio.getMode ();
//System.err.println ("[writeConsole called name: " + extio.getName() + " mode: " + extio.getMode());
if (mode == Extio.MODE_CONSOLE_WRITE)
{
Writer osw;
osw = (Writer)extio.getHandle ();
// as the write operation below doesn't
// indicate if it has reached the end, console
// can't be switched here unlike in read_console
try { osw.write (buf, 0, len); osw.flush (); }
catch (IOException e) { return -1; }
return len;
}
return -1;
}
protected int flushConsole (Extio extio)
{
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_WRITE)
{
Writer osw;
osw = (Writer)extio.getHandle ();
try { osw.flush (); }
catch (IOException e) { return -1; }
return 0;
}
return -1;
}
protected int nextConsole (Extio extio)
{
int mode = extio.getMode ();
if (mode == Extio.MODE_CONSOLE_READ)
{
Reader isr, tmp;
isr = (Reader)extio.getHandle ();
if (cin == null || cin_no >= cin.length) return 0;
tmp = get_stream_reader (cin[cin_no]);
if (tmp == null) return -1;
if (isr != StdAwk.stdin)
{
try { isr.close (); }
catch (IOException e) { /* ignore */ }
}
extio.setHandle (tmp);
try { setConsoleInputName (extio, cin[cin_no]); }
catch (Exception e) { return -1; }
cin_no++;
return 1;
}
else if (mode == Extio.MODE_CONSOLE_WRITE)
{
Writer osw, tmp;
osw = (Writer)extio.getHandle ();
if (cout == null || cout_no >= cout.length) return 0;
tmp = get_stream_writer (cout[cout_no]);
if (tmp == null) return -1;
if (osw != StdAwk.stdout)
{
try { osw.close (); }
catch (IOException e) { /* ignore */ }
}
extio.setHandle (tmp);
try { setConsoleOutputName (extio, cout[cout_no]); }
catch (Exception e) { return -1;}
cout_no++;
return 1;
}
return -1;
}
private Reader get_stream_reader (String name)
{
Reader isr;
if (name == null || name.length() == 0) isr = StdAwk.stdin;
else
{
FileInputStream fis;
try { fis = new FileInputStream (name); }
catch (IOException e) { return null; }
isr = new BufferedReader(new InputStreamReader (fis));
}
return isr;
}
private Writer get_stream_writer (String name)
{
Writer osw;
if (name == null || name.length() == 0) osw = StdAwk.stdout;
else
{
FileOutputStream fos;
try { fos = new FileOutputStream (name); }
catch (IOException e) { return null; }
osw = new BufferedWriter (new OutputStreamWriter (fos));
}
return osw;
}
/* == file interface == */
protected int openFile (Extio extio)
{
@ -521,7 +92,7 @@ public abstract class StdAwk extends Awk
{
Reader isr;
isr = (Reader)extio.getHandle();
if (isr != StdAwk.stdin)
if (isr != null)
{
try { isr.close (); }
catch (IOException e) { return -1; }
@ -532,7 +103,7 @@ public abstract class StdAwk extends Awk
{
Writer osw;
osw = (Writer)extio.getHandle();
if (osw != StdAwk.stdout)
if (osw != null)
{
try { osw.close (); }
catch (IOException e) { return -1; }
@ -543,7 +114,7 @@ public abstract class StdAwk extends Awk
{
Writer osw;
osw = (Writer)extio.getHandle();
if (osw != StdAwk.stdout)
if (osw != null)
{
try { osw.close (); }
catch (IOException e) { return -1; }
@ -651,7 +222,7 @@ public abstract class StdAwk extends Awk
{
Reader isr;
isr = (Reader)extio.getHandle();
if (isr != StdAwk.stdin)
if (isr != null)
{
try { isr.close (); }
catch (IOException e) { return -1; }
@ -662,7 +233,7 @@ public abstract class StdAwk extends Awk
{
Writer osw;
osw = (Writer)extio.getHandle();
if (osw != StdAwk.stdout)
if (osw != null)
{
try { osw.close (); }
catch (IOException e) { return -1; }
@ -817,6 +388,10 @@ public abstract class StdAwk extends Awk
/* miscellaneous built-in functions */
public Object bfn_system (long runid, Object[] args) throws Exception
{
String str = builtinFunctionArgumentToString (runid, args[0]);
return system (str);
/*
String str = builtinFunctionArgumentToString (runid, args[0]);
Process proc = null;
int n = 0;
@ -833,7 +408,7 @@ public abstract class StdAwk extends Awk
is = proc.getInputStream();
// TODO; better error handling... program execution.. io redirection???
// TODO; better error handling... program execution.. io redirection???
try { while (is.read (buf) != -1) ; }
catch (IOException e) { n = -1; };
@ -846,6 +421,7 @@ public abstract class StdAwk extends Awk
}
return new Long (n);
*/
}
/* == utility functions == */

View File

@ -1,5 +1,5 @@
/*
* $Id: jni.c,v 1.8 2007/05/24 06:53:21 bacon Exp $
* $Id: jni.c,v 1.10 2007/05/25 14:41:48 bacon Exp $
*
* {License}
*/
@ -380,9 +380,19 @@ JNIEXPORT void JNICALL Java_ase_awk_Awk_open (JNIEnv* env, jobject obj)
(*env)->SetLongField (env, obj, handle, (jlong)awk);
opt = ASE_AWK_EXPLICIT | ASE_AWK_UNIQUEFN | ASE_AWK_SHADING |
ASE_AWK_IMPLICIT | ASE_AWK_SHIFT | ASE_AWK_IDIV |
ASE_AWK_EXTIO | ASE_AWK_BLOCKLESS | ASE_AWK_NEXTOFILE;
opt = ASE_AWK_IMPLICIT |
ASE_AWK_EXPLICIT |
ASE_AWK_UNIQUEFN |
ASE_AWK_IDIV |
ASE_AWK_SHADING |
ASE_AWK_SHIFT |
ASE_AWK_EXTIO |
ASE_AWK_BLOCKLESS |
ASE_AWK_STRBASEONE |
ASE_AWK_STRIPSPACES |
ASE_AWK_NEXTOFILE |
ASE_AWK_ARGSTOMAIN;
ase_awk_setoption (awk, opt);
}
@ -2356,6 +2366,7 @@ static jstring JNICALL call_strftime (
ase_size_t len, i;
const jchar* ptr;
ase_char_t* tmp;
jchar* tmp2;
jstring ret;
len = (*env)->GetStringLength (env, fmt);
@ -2371,7 +2382,7 @@ static jstring JNICALL call_strftime (
return NULL;
}
tmp = (jchar*) malloc (ASE_SIZEOF(ase_char_t)*(len+1));
tmp = (ase_char_t*) malloc (ASE_SIZEOF(ase_char_t)*(len+1));
if (tmp == NULL)
{
(*env)->ReleaseStringChars (env, fmt, ptr);
@ -2393,8 +2404,8 @@ static jstring JNICALL call_strftime (
if (len > 0 && ASE_SIZEOF(jchar) != ASE_SIZEOF(ase_char_t))
{
tmp = (jchar*) malloc (ASE_SIZEOF(jchar)*len);
if (tmp == NULL)
tmp2 = (jchar*) malloc (ASE_SIZEOF(jchar)*len);
if (tmp2 == NULL)
{
throw_exception (
env,
@ -2403,9 +2414,9 @@ static jstring JNICALL call_strftime (
0);
return NULL;
}
for (i = 0; i < len; i++) tmp[i] = (jchar)buf[i];
ret = (*env)->NewString (env, tmp, len);
free (tmp);
for (i = 0; i < len; i++) tmp2[i] = (jchar)buf[i];
ret = (*env)->NewString (env, tmp2, len);
free (tmp2);
}
else
{
@ -2459,7 +2470,6 @@ JNIEXPORT jstring JNICALL Java_ase_awk_Awk_strfgmtime (
return call_strftime (env, obj, fmt, tm);
}
/*
JNIEXPORT jint JNICALL Java_ase_awk_Awk_system (
JNIEnv* env, jobject obj, jstring cmd)
{
@ -2468,8 +2478,8 @@ JNIEXPORT jint JNICALL Java_ase_awk_Awk_system (
ase_char_t* tmp;
jint ret;
len = (*env)->GetStringLength (env, fmt);
ptr = (*env)->GetStringChars (env, fmt, JNI_FALSE);
len = (*env)->GetStringLength (env, cmd);
ptr = (*env)->GetStringChars (env, cmd, JNI_FALSE);
if (ptr == NULL)
{
(*env)->ExceptionClear (env);
@ -2478,37 +2488,36 @@ JNIEXPORT jint JNICALL Java_ase_awk_Awk_system (
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
ASE_AWK_ENOMEM,
0);
return NULL;
return -1;
}
tmp = (jchar*) malloc (ASE_SIZEOF(ase_char_t)*(len+1));
tmp = (ase_char_t*) malloc (ASE_SIZEOF(ase_char_t)*(len+1));
if (tmp == NULL)
{
(*env)->ReleaseStringChars (env, fmt, ptr);
(*env)->ReleaseStringChars (env, cmd, ptr);
throw_exception (
env,
ase_awk_geterrstr(ASE_NULL, ASE_AWK_ENOMEM),
ASE_AWK_ENOMEM,
0);
return NULL;
return -1;
}
for (i = 0; i < len; i++) tmp[i] = (ase_char_t)ptr[i];
tmp[i] = ASE_T('\0');
#ifdef _WIN32
ret = _tsystem(ptr);
ret = _tsystem(tmp);
#else
char* mbs = (char*)malloc (awk, len*5+1);
char* mbs = (char*)malloc (len*5+1);
if (mbs == ASE_NULL) return -1;
size_t mbl = wcstombs (mbs, ptr, len*5);
size_t mbl = wcstombs (mbs, tmp, len*5);
if (mbl == (size_t)-1) return -1;
mbs[mbl] = '\0';
ret = system(mbs);
free (awk, mbs);
free (mbs);
#endif
return ret;
}
*/

View File

@ -1,5 +1,5 @@
#
# $Id: makefile.in,v 1.5 2007/05/06 04:23:44 bacon Exp $
# $Id: makefile.in,v 1.6 2007/05/25 11:49:42 bacon Exp $
#
NAME = aseawk
@ -123,16 +123,16 @@ $(TMP_DIR)/rex.o: rex.c
$(TMP_DIR)/jni.o: jni.c
$(LIBTOOL_COMPILE) $(CC) $(CFLAGS) $(CFLAGS_JNI) -o $@ -c jni.c
$(TMP_DIR)/ase/awk/Awk.class:
$(TMP_DIR)/ase/awk/Awk.class: Awk.java
$(JAVAC) -classpath ../.. -d $(TMP_DIR) Awk.java
$(TMP_DIR)/ase/awk/StdAwk.class:
$(TMP_DIR)/ase/awk/StdAwk.class: StdAwk.java
$(JAVAC) -classpath ../.. -d $(TMP_DIR) StdAwk.java
$(TMP_DIR)/ase/awk/Extio.class:
$(TMP_DIR)/ase/awk/Extio.class: Extio.java
$(JAVAC) -classpath ../.. -d $(TMP_DIR) Extio.java
$(TMP_DIR)/ase/awk/Exception.class:
$(TMP_DIR)/ase/awk/Exception.class: Exception.java
$(JAVAC) -classpath ../.. -d $(TMP_DIR) Exception.java
$(TMP_DIR)/cxx/Awk.o: Awk.cpp Awk.hpp