Class JRT

java.lang.Object
org.sentrysoftware.jawk.jrt.JRT

public class JRT extends 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 byte-code, 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:

  • File and command output redirection via print(f).
  • File and command input redirection via getline.
  • Most built-in AWK functions, such as system(), sprintf(), etc.
  • Automatic AWK type conversion routines.
  • IO management for input rule processing.
  • Random number engine management.
  • Input field ($0, $1, ...) management.

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.sentrysoftware.jawk.jrt package by default, and the user may reassign the resultant script class to another package. Therefore, all accessed methods are public.

Author:
Danny Daglas
See Also:
  • Constructor Details

    • JRT

      public JRT(VariableManager vm)
      Create a JRT with a VariableManager
      Parameters:
      vm - The VariableManager to use with this JRT.
  • Method Details

    • assignInitialVariables

      public final void assignInitialVariables(Map<String,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 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 associative array to populate with environment variables. The module asserts that the associative array is empty prior to population.
    • toAwkString

      public static String toAwkString(Object o, String convfmt, Locale locale)
      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.
      locale - a Locale object
      Returns:
      A String representation of o.
    • toAwkStringForOutput

      public static String toAwkStringForOutput(Object o, String ofmt, Locale locale)
      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.
      locale - a Locale object
      Returns:
      A String representation of o.
    • toDouble

      public static double toDouble(Object o)
      Convert a String, Integer, or Double to Double.
      Parameters:
      o - Object to convert.
      Returns:
      the "double" value of o, or 0 if invalid
    • toLong

      public static long toLong(Object o)
      Convert a String, Long, or Double to Long.
      Parameters:
      o - Object to convert.
      Returns:
      the "long" value of o, or 0 if invalid
    • compare2

      public static boolean compare2(Object o1, 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.
      Returns:
      a boolean
    • inc

      public static Object inc(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 Object dec(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(Object o)
      Converts an Integer, Double, String, Pattern, or ConditionPair to a boolean.
      Parameters:
      o - The object to convert to a boolean.
      Returns:
      For the following class types for o:
      • Integer - o.intValue() != 0
      • Long - o.longValue() != 0
      • Double - o.doubleValue() != 0
      • String - o.length() > 0
      • UninitializedObject - false
      • Pattern - $0 ~ o
      If o is none of these types, an error is thrown.
    • split

      public static int split(Object array, Object string, String convfmt, Locale locale)
      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.
      locale - a Locale object
      Returns:
      The number of parts resulting from this split operation.
    • split

      public static int split(Object fs, Object array, Object string, String convfmt, Locale locale)
      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.
      locale - a Locale object
      Returns:
      The number of parts resulting from this split operation.
    • getPartitioningReader

      public PartitioningReader getPartitioningReader()

      Getter for the field partitioningReader.

      Returns:
      a PartitioningReader object
    • getInputLine

      public String getInputLine()

      Getter for the field inputLine.

      Returns:
      a String object
    • setInputLine

      public void setInputLine(String inputLine)

      Setter for the field inputLine.

      Parameters:
      inputLine - a String object
    • jrtConsumeInput

      public boolean jrtConsumeInput(InputStream input, boolean for_getline, Locale locale) throws 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.
      input - a InputStream object
      locale - a Locale object
      Returns:
      true if line is consumed, false otherwise.
      Throws:
      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 Object jrtGetInputField(Object fieldnum_obj)
      Retrieve the contents of a particular input field.
      Parameters:
      fieldnum_obj - Object referring to the field number.
      Returns:
      Contents of the field.
    • jrtGetInputField

      public Object jrtGetInputField(int fieldnum)

      jrtGetInputField.

      Parameters:
      fieldnum - a int
      Returns:
      a Object object
    • jrtSetInputField

      public String jrtSetInputField(Object value_obj, int field_num)
      Stores value_obj into an input field.
      Parameters:
      value_obj - The RHS of the assignment.
      field_num - Object referring to the field number.
      Returns:
      A string representation of value_obj.
    • jrtConsumeFileInputForGetline

      public Integer jrtConsumeFileInputForGetline(String filename)

      jrtConsumeFileInputForGetline.

      Parameters:
      filename - a String object
      Returns:
      a Integer object
    • jrtConsumeCommandInputForGetline

      public Integer jrtConsumeCommandInputForGetline(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 String jrtGetInputString()
      Retrieve $0.
      Returns:
      The contents of the $0 input field.
    • getOutputFiles

      public Map<String,PrintStream> getOutputFiles()

      Getter for the field outputFiles.

      Returns:
      a Map object
    • jrtGetPrintStream

      public final PrintStream jrtGetPrintStream(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.
      Returns:
      a PrintStream object
    • jrtConsumeFileInput

      public boolean jrtConsumeFileInput(String filename) throws IOException

      jrtConsumeFileInput.

      Parameters:
      filename - a String object
      Returns:
      a boolean
      Throws:
      IOException - if any.
    • jrtConsumeCommandInput

      public boolean jrtConsumeCommandInput(String cmd) throws IOException

      jrtConsumeCommandInput.

      Parameters:
      cmd - a String object
      Returns:
      a boolean
      Throws:
      IOException - if any.
    • jrtSpawnForOutput

      public PrintStream jrtSpawnForOutput(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 Integer jrtClose(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()

      jrtCloseAll.

    • jrtSystem

      public static Integer jrtSystem(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.
    • sprintfNoCatch

      public static String sprintfNoCatch(Locale locale, String fmt_arg, Object... arr) throws IllegalFormatException

      sprintfFunctionNoCatch.

      Parameters:
      locale - a Locale object
      fmt_arg - a String object
      arr - an array of Object objects
      Returns:
      a String object
      Throws:
      IllegalFormatException - if any.
    • printfNoCatch

      public static void printfNoCatch(Locale locale, String fmt_arg, Object... arr)

      printfFunctionNoCatch.

      Parameters:
      locale - a Locale object
      fmt_arg - a String object
      arr - an array of Object objects
    • printfNoCatch

      public static void printfNoCatch(PrintStream ps, Locale locale, String fmt_arg, Object... arr)

      printfFunctionNoCatch.

      Parameters:
      ps - a PrintStream object
      locale - a Locale object
      fmt_arg - a String object
      arr - an array of Object objects
    • replaceFirst

      public static Integer replaceFirst(Object orig_value_obj, Object repl_obj, Object ere_obj, StringBuffer sb, String convfmt, Locale locale)

      replaceFirst.

      Parameters:
      orig_value_obj - a Object object
      repl_obj - a Object object
      ere_obj - a Object object
      sb - a StringBuffer object
      convfmt - a String object
      locale - a Locale object
      Returns:
      a Integer object
    • replaceAll

      public static Integer replaceAll(Object orig_value_obj, Object repl_obj, Object ere_obj, StringBuffer sb, String convfmt, Locale locale)

      replaceAll.

      Parameters:
      orig_value_obj - a Object object
      repl_obj - a Object object
      ere_obj - a Object object
      sb - a StringBuffer object
      convfmt - a String object
      locale - a Locale object
      Returns:
      a Integer object
    • substr

      public static String substr(Object startpos_obj, String str)

      substr.

      Parameters:
      startpos_obj - a Object object
      str - a String object
      Returns:
      a String object
    • substr

      public static String substr(Object size_obj, Object startpos_obj, String str)

      substr.

      Parameters:
      size_obj - a Object object
      startpos_obj - a Object object
      str - a String object
      Returns:
      a String object
    • timeSeed

      public static int timeSeed()

      timeSeed.

      Returns:
      a int
    • newRandom

      public static Random newRandom(int seed)

      newRandom.

      Parameters:
      seed - a int
      Returns:
      a Random object
    • applyRS

      public void applyRS(Object rs_obj)

      applyRS.

      Parameters:
      rs_obj - a Object object