org.jawk.jrt
Class JRT

java.lang.Object
  extended by org.jawk.jrt.JRT

public class JRT
extends java.lang.Object

The Jawk runtime coordinator. The JRT services interpreted and compiled Jawk scripts, mainly for IO and other non-CPU bound tasks. The goal is to house service functions into a Java-compiled class rather than to hand-craft service functions in bytecode, or cut-paste compiled JVM code into the compiled AWK script. Also, since these functions are non-CPU bound, the need for inlining is reduced.

Variable access is achieved through the VariableManager interface. The constructor requires a VariableManager instance (which, in this case, is the compiled Jawk class itself).

Main services include:

All static and non-static service methods should be package-private to the resultant AWK script class rather than public. However, the resultant script class is not in the org.jawk.jrt package by default, and the user may reassign the resultant script class to another package. Therefore, all accessed methods are public.

See Also:
VariableManager

Field Summary
static java.lang.String DEFAULT_RS_REGEX
          The default regular expression for RS.
 java.lang.String input_line
           
 java.util.Map<java.lang.String,java.io.PrintStream> output_files
           
 PartitioningReader pr
           
 
Constructor Summary
JRT(VariableManager vm)
          Create a JRT with a VariableManager
 
Method Summary
 void applyRS(java.lang.Object rs_obj)
           
static void assignEnvironmentVariables(AssocArray aa)
          Called by AVM/compiled modules to assign local environment variables to an associative array (in this case, to ENVIRON).
 void assignInitialVariables(java.util.Map<java.lang.String,java.lang.Object> initial_var_map)
          Assign all -v variables.
static boolean compare2(java.lang.Object o1, java.lang.Object o2, int mode)
          Compares two objects.
static java.lang.Object dec(java.lang.Object o)
          Return an object which is numerically equivalent to one minus a given object.
static java.lang.Object inc(java.lang.Object o)
          Return an object which is numerically equivalent to one plus a given object.
 java.lang.Integer jrtClose(java.lang.String filename)
          Attempt to close an open stream, whether it is an input file, output file, input process, or output process.
 void jrtCloseAll()
           
 boolean jrtConsumeCommandInput(java.lang.String cmd)
           
 java.lang.Integer jrtConsumeCommandInputForGetline(java.lang.String cmd_string)
          Retrieve the next line of output from a command, executing the command if necessary and store it to $0.
 boolean jrtConsumeFileInput(java.lang.String filename)
           
 java.lang.Integer jrtConsumeFileInputForGetline(java.lang.String filename)
           
 boolean jrtConsumeInput(boolean for_getline)
          Attempt to consume one line of input, either from stdin or from filenames passed in to ARGC/ARGV via the command-line.
 java.lang.Object jrtGetInputField(int fieldnum)
           
 java.lang.Object jrtGetInputField(java.lang.Object fieldnum_obj)
          Retrieve the contents of a particular input field.
 java.lang.String jrtGetInputString()
          Retrieve $0.
 java.io.PrintStream jrtGetPrintStream(java.lang.String filename, boolean append)
          Retrieve the PrintStream which writes to a particular file, creating the PrintStream if necessary.
 void jrtParseFields()
          Splits $0 into $1, $2, etc.
 java.lang.String jrtSetInputField(java.lang.Object value_obj, int field_num)
          Stores value_obj into an input field.
 java.io.PrintStream jrtSpawnForOutput(java.lang.String cmd)
          Retrieve the PrintStream which shuttles data to stdin for a process, executing the process if necessary.
static java.lang.Integer jrtSystem(java.lang.String cmd)
          Executes the command specified by cmd and waits for termination, returning an Integer object containing the return code.
static java.util.Random newRandom(int seed)
           
static void printfFunction(java.lang.Object[] arr, java.lang.String fmt_arg)
          Applies a format string to a set of parameters and prints the result to stdout.
static void printfFunction(java.io.PrintStream ps, java.lang.Object[] arr, java.lang.String fmt_arg)
          Applies a format string to a set of parameters and prints the result to a PrintStream.
static void printfFunctionNoCatch(java.lang.Object[] arr, java.lang.String fmt_arg)
           
static void printfFunctionNoCatch(java.io.PrintStream ps, java.lang.Object[] arr, java.lang.String fmt_arg)
           
static java.lang.Integer replaceAll(java.lang.Object orig_value_obj, java.lang.Object repl_obj, java.lang.Object ere_obj, java.lang.StringBuffer sb, java.lang.String convfmt)
           
static java.lang.Integer replaceFirst(java.lang.Object orig_value_obj, java.lang.Object repl_obj, java.lang.Object ere_obj, java.lang.StringBuffer sb, java.lang.String convfmt)
           
static int split(java.lang.Object fs, java.lang.Object array, java.lang.Object string, java.lang.String convfmt)
          Splits the string into parts separated the regular expression fs.
static int split(java.lang.Object array, java.lang.Object string, java.lang.String convfmt)
          Splits the string into parts separated by one or more spaces; blank first and last fields are eliminated.
static java.lang.String sprintfFunction(java.lang.Object[] arr, java.lang.String fmt_arg)
          Applies a format string to a set of parameters and returns the formatted result.
static java.lang.String sprintfFunctionNoCatch(java.lang.Object[] arr, java.lang.String fmt_arg)
           
static java.lang.String substr(java.lang.Object size_obj, java.lang.Object startpos_obj, java.lang.String str)
           
static java.lang.String substr(java.lang.Object startpos_obj, java.lang.String str)
           
static int timeSeed()
           
static java.lang.String toAwkString(java.lang.Object o, java.lang.String convfmt)
          Convert Strings, Integers, and Doubles to Strings based on the CONVFMT variable contents.
static java.lang.String toAwkStringForOutput(java.lang.Object o, java.lang.String ofmt)
          Convert a String, Integer, or Double to String based on the OFMT variable contents.
 boolean toBoolean(java.lang.Object o)
          Converts an Integer, Double, String, Pattern, or PatternPair to a boolean.
static double toDouble(java.lang.Object o)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_RS_REGEX

public static final java.lang.String DEFAULT_RS_REGEX
The default regular expression for RS. The AVM refers to this field, so that the field exists in one place.


pr

public PartitioningReader pr

input_line

public java.lang.String input_line

output_files

public java.util.Map<java.lang.String,java.io.PrintStream> output_files
Constructor Detail

JRT

public JRT(VariableManager vm)
Create a JRT with a VariableManager

Parameters:
vm - The VariableManager to use with this JRT.
Method Detail

assignInitialVariables

public final void assignInitialVariables(java.util.Map<java.lang.String,java.lang.Object> initial_var_map)
Assign all -v variables.

Parameters:
initial_var_map - A map containing all initial variable names and their values.

assignEnvironmentVariables

public static final void assignEnvironmentVariables(AssocArray aa)
Called by AVM/compiled modules to assign local environment variables to an associative array (in this case, to ENVIRON).

Parameters:
aa - The associatve array to populate with environment variables. The module asserts that the associative array is empty prior to population.

toAwkString

public static final java.lang.String toAwkString(java.lang.Object o,
                                                 java.lang.String convfmt)
Convert Strings, Integers, and Doubles to Strings based on the CONVFMT variable contents.

Parameters:
o - Object to convert.
convfmt - The contents of the CONVFMT variable.
Returns:
A String representation of o.

toAwkStringForOutput

public static final java.lang.String toAwkStringForOutput(java.lang.Object o,
                                                          java.lang.String ofmt)
Convert a String, Integer, or Double to String based on the OFMT variable contents. Jawk will subsequently use this String for output via print().

Parameters:
o - Object to convert.
ofmt - The contents of the OFMT variable.
Returns:
A String representation of o.

toDouble

public static final double toDouble(java.lang.Object o)

compare2

public static final boolean compare2(java.lang.Object o1,
                                     java.lang.Object o2,
                                     int mode)
Compares two objects. Whether to employ less-than, equals, or greater-than checks depends on the mode chosen by the callee. It handles Awk variable rules and type conversion semantics.

Parameters:
o1 - The 1st object.
o2 - the 2nd object.
mode -
  • < 0 - Return true if o1 < o2.
  • 0 - Return true if o1 == o2.
  • > 0 - Return true if o1 > o2.

inc

public static java.lang.Object inc(java.lang.Object o)
Return an object which is numerically equivalent to one plus a given object. For Integers and Doubles, this is similar to o+1. For Strings, attempts are made to convert it to a double first. If the String does not represent a valid Double, 1 is returned.

Parameters:
o - The object to increase.
Returns:
o+1 if o is an Integer or Double object, or if o is a String object and represents a double. Otherwise, 1 is returned. If the return value is an integer, an Integer object is returned. Otherwise, a Double object is returned.

dec

public static java.lang.Object dec(java.lang.Object o)
Return an object which is numerically equivalent to one minus a given object. For Integers and Doubles, this is similar to o-1. For Strings, attempts are made to convert it to a double first. If the String does not represent a valid Double, -1 is returned.

Parameters:
o - The object to increase.
Returns:
o-1 if o is an Integer or Double object, or if o is a String object and represents a double. Otherwise, -1 is returned. If the return value is an integer, an Integer object is returned. Otherwise, a Double object is returned.

toBoolean

public final boolean toBoolean(java.lang.Object o)
Converts an Integer, Double, String, Pattern, or PatternPair to a boolean.

Parameters:
o - The object to convert to a boolean.
Returns:
For the following class types for o:
  • Integer - o.intValue() != 0
  • Double - o.doubleValue() != 0
  • String - o.length() > 0
  • Pattern - $0 ~ o
  • PatternPair - $0 ~ o (with some state information)
If o is none of these types, an error is thrown.
See Also:
PatternPair

split

public static final int split(java.lang.Object array,
                              java.lang.Object string,
                              java.lang.String convfmt)
Splits the string into parts separated by one or more spaces; blank first and last fields are eliminated. This conforms to the 2-argument version of AWK's split function.

Parameters:
array - The array to populate.
string - The string to split.
convfmt - Contents of the CONVFMT variable.
Returns:
The number of parts resulting from this split operation.

split

public static final int split(java.lang.Object fs,
                              java.lang.Object array,
                              java.lang.Object string,
                              java.lang.String convfmt)
Splits the string into parts separated the regular expression fs. This conforms to the 3-argument version of AWK's split function.

If fs is blank, it behaves similar to the 2-arg version of AWK's split function.

Parameters:
fs - Field separator regular expression.
array - The array to populate.
string - The string to split.
convfmt - Contents of the CONVFMT variable.
Returns:
The number of parts resulting from this split operation.

jrtConsumeInput

public boolean jrtConsumeInput(boolean for_getline)
                        throws java.io.IOException
Attempt to consume one line of input, either from stdin or from filenames passed in to ARGC/ARGV via the command-line.

Parameters:
for_getline - true if call is for getline, false otherwise.
Returns:
true if line is consumed, false otherwise.
Throws:
java.io.IOException - upon an IO error.

jrtParseFields

public void jrtParseFields()
Splits $0 into $1, $2, etc. Called when an update to $0 has occurred.


jrtGetInputField

public java.lang.Object jrtGetInputField(java.lang.Object fieldnum_obj)
Retrieve the contents of a particular input field.

Parameters:
fieldnum_obj - Object refering to the field number.
Returns:
Contents of the field.

jrtGetInputField

public java.lang.Object jrtGetInputField(int fieldnum)

jrtSetInputField

public java.lang.String jrtSetInputField(java.lang.Object value_obj,
                                         int field_num)
Stores value_obj into an input field.

Parameters:
value_obj - The RHS of the assignment.
field_num - Object refering to the field number.
Returns:
A string representation of value_obj.

jrtConsumeFileInputForGetline

public java.lang.Integer jrtConsumeFileInputForGetline(java.lang.String filename)

jrtConsumeCommandInputForGetline

public java.lang.Integer jrtConsumeCommandInputForGetline(java.lang.String cmd_string)
Retrieve the next line of output from a command, executing the command if necessary and store it to $0.

Parameters:
cmd_string - The command to execute.
Returns:
Integer(1) if successful, Integer(0) if no more input is available, Integer(-1) upon an IO error.

jrtGetInputString

public java.lang.String jrtGetInputString()
Retrieve $0.

Returns:
The contents of the $0 input field.

jrtGetPrintStream

public final java.io.PrintStream jrtGetPrintStream(java.lang.String filename,
                                                   boolean append)
Retrieve the PrintStream which writes to a particular file, creating the PrintStream if necessary.

Parameters:
filename - The file which to write the contents of the PrintStream.
append - true to append to the file, false to overwrite the file.

jrtConsumeFileInput

public boolean jrtConsumeFileInput(java.lang.String filename)
                            throws java.io.IOException
Throws:
java.io.IOException

jrtConsumeCommandInput

public boolean jrtConsumeCommandInput(java.lang.String cmd)
                               throws java.io.IOException
Throws:
java.io.IOException

jrtSpawnForOutput

public java.io.PrintStream jrtSpawnForOutput(java.lang.String cmd)
Retrieve the PrintStream which shuttles data to stdin for a process, executing the process if necessary. Threads are created to shuttle the data to/from the process.

Parameters:
cmd - The command to execute.
Returns:
The PrintStream which to write to provide input data to the process.

jrtClose

public java.lang.Integer jrtClose(java.lang.String filename)
Attempt to close an open stream, whether it is an input file, output file, input process, or output process.

The specification did not describe AWK behavior when attempting to close streams/processes with the same file/command name. In this case, all open streams with this name are closed.

Parameters:
filename - The filename/command process to close.
Returns:
Integer(0) upon a successful close, Integer(-1) otherwise.

jrtCloseAll

public void jrtCloseAll()

jrtSystem

public static java.lang.Integer jrtSystem(java.lang.String cmd)
Executes the command specified by cmd and waits for termination, returning an Integer object containing the return code. stdin to this process is closed while threads are created to shuttle stdout and stderr of the command to stdout/stderr of the calling process.

Parameters:
cmd - The command to execute.
Returns:
Integer(return_code) of the created process. Integer(-1) is returned on an IO error.

sprintfFunction

public static java.lang.String sprintfFunction(java.lang.Object[] arr,
                                               java.lang.String fmt_arg)
Applies a format string to a set of parameters and returns the formatted result. String.format() is used to perform the formatting. Thus, an IllegalFormatException can be thrown. If so, a blank string ("") is returned.

Parameters:
arr - Arguments to format.
fmt_arg - The format string to apply.
Returns:
The formatted string; a blank string if the format argument is invalid.
See Also:
sprintfFunctionNoCatch(Object[],String)

printfFunction

public static void printfFunction(java.lang.Object[] arr,
                                  java.lang.String fmt_arg)
Applies a format string to a set of parameters and prints the result to stdout. The implementation is a simple call to sprintfFunction:
 System.out.print(sprintfFunction(arr, fmt_arg));
 
String.format() is used to perform the formatting. Thus, an IllegalFormatException can be thrown. If so, a blank string ("") is printed.

Parameters:
arr - Arguments to format.
fmt_arg - The format string to apply.
See Also:
printfFunctionNoCatch(Object[],String)

printfFunction

public static void printfFunction(java.io.PrintStream ps,
                                  java.lang.Object[] arr,
                                  java.lang.String fmt_arg)
Applies a format string to a set of parameters and prints the result to a PrintStream. The implementation is a simple call to sprintfFunction:
 ps.print(sprintfFunction(arr, fmt_arg));
 
String.format() is used to perform the formatting. Thus, an IllegalFormatException can be thrown. If so, a blank string ("") is printed.

Parameters:
ps - The PrintStream to use for printing.
arr - Arguments to format.
fmt_arg - The format string to apply.
See Also:
printfFunctionNoCatch(PrintStream,Object[],String)

sprintfFunctionNoCatch

public static java.lang.String sprintfFunctionNoCatch(java.lang.Object[] arr,
                                                      java.lang.String fmt_arg)
                                               throws java.util.IllegalFormatException
Throws:
java.util.IllegalFormatException

printfFunctionNoCatch

public static void printfFunctionNoCatch(java.lang.Object[] arr,
                                         java.lang.String fmt_arg)

printfFunctionNoCatch

public static void printfFunctionNoCatch(java.io.PrintStream ps,
                                         java.lang.Object[] arr,
                                         java.lang.String fmt_arg)

replaceFirst

public static java.lang.Integer replaceFirst(java.lang.Object orig_value_obj,
                                             java.lang.Object repl_obj,
                                             java.lang.Object ere_obj,
                                             java.lang.StringBuffer sb,
                                             java.lang.String convfmt)

replaceAll

public static java.lang.Integer replaceAll(java.lang.Object orig_value_obj,
                                           java.lang.Object repl_obj,
                                           java.lang.Object ere_obj,
                                           java.lang.StringBuffer sb,
                                           java.lang.String convfmt)

substr

public static java.lang.String substr(java.lang.Object startpos_obj,
                                      java.lang.String str)

substr

public static java.lang.String substr(java.lang.Object size_obj,
                                      java.lang.Object startpos_obj,
                                      java.lang.String str)

timeSeed

public static final int timeSeed()

newRandom

public static java.util.Random newRandom(int seed)

applyRS

public void applyRS(java.lang.Object rs_obj)