Package io.jawk.jrt

Class JRT

java.lang.Object
io.jawk.jrt.JRT
Direct Known Subclasses:
SandboxedJRT

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 io.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, Locale locale, AwkSink awkSink, PrintStream error)
      Create a JRT with explicit default output and error streams.
      Parameters:
      vm - The VariableManager to use with this JRT.
      locale - The Locale to use for number formatting.
      awkSink - default output sink used by plain AWK print operations
      error - default error stream used for process stderr
  • Method Details

    • setAwkSink

      public void setAwkSink(AwkSink sink)
      Sets the sink used by default print and printf operations.
      Parameters:
      sink - output sink to use
    • setErrorStream

      public void setErrorStream(PrintStream errorStream)
      Sets the stream used for the stderr output of spawned processes (e.g. system("...")).
      Parameters:
      errorStream - stream to receive process stderr
    • getAwkSink

      public AwkSink getAwkSink()
      Returns the default output sink used by print and printf.
      Returns:
      the current AWK sink
    • getLocale

      public Locale getLocale()
      Returns the locale used for number formatting in this runtime.
      Returns:
      the runtime locale
    • isJrtManagedSpecialVariable

      public static boolean isJrtManagedSpecialVariable(String name)
      Returns whether the supplied variable name is managed directly by JRT rather than through the AVM runtime stack.
      Parameters:
      name - variable name to inspect
      Returns:
      true when the variable is a JRT-managed special variable
    • copySpecialVariables

      public static Map<String,Object> copySpecialVariables(Map<String,Object> variableMap)
      Copies only the JRT-managed special variables from the supplied map.
      Parameters:
      variableMap - source variable map
      Returns:
      a new map containing only JRT-managed special variables
    • prepareForExecution

      public void prepareForExecution(String defaultFs, String defaultRs)
      Resets per-execution JRT state and re-applies the default runtime special variables for a new script or expression execution.

      The defaultFs and defaultRs parameters allow the caller to configure the initial field and record separators. Other special variables (OFS, ORS, CONVFMT, OFMT, SUBSEP) use their POSIX-mandated defaults (see Awk constants) which are platform-independent and therefore not parameterized. Platform-specific end-of-line handling is the responsibility of the AwkSink.

      Parameters:
      defaultFs - default field separator, or null for Awk.DEFAULT_FS
      defaultRs - default record separator
    • assignInitialVariables

      public final void assignInitialVariables(Map<String,Object> initialVarMap)
      Assign all -v variables.
      Parameters:
      initialVarMap - A map containing all initial variable names and their values.
    • applySpecialVariables

      public final void applySpecialVariables(Map<String,Object> variableMap)
      Applies only the JRT-managed special variable assignments from the supplied map (FS, RS, OFS, ORS, CONVFMT, OFMT, SUBSEP, FILENAME, NF, NR, FNR, ARGC). Non-special variables are silently skipped because they require the runtime stack to be fully initialized (which happens during tuple execution).
      Parameters:
      variableMap - a map of variable names to 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.
    • createAwkMap

      public static Map<Object,Object> createAwkMap(boolean sortedArrayKeys)
      Creates an AWK-managed associative array and exposes it as a plain Map for callers that do not need the concrete runtime type.
      Parameters:
      sortedArrayKeys - true to keep keys sorted
      Returns:
      a new AWK associative array
    • containsAwkKey

      public static boolean containsAwkKey(Map<Object,Object> map, Object key)
      Checks key existence using AWK semantics when the supplied map is backed by an AssocArray, otherwise falling back to regular Map semantics.
      Parameters:
      map - map to inspect
      key - key to look up
      Returns:
      true when the key exists
    • getAwkValue

      public static Object getAwkValue(Map<Object,Object> map, Object key)
      Reads a map element using AWK semantics when the supplied map is backed by an AssocArray. For plain Map instances, missing or null-valued entries are exposed as the AWK blank value so later expression evaluation never receives a raw null.
      Parameters:
      map - map to inspect
      key - key to look up
      Returns:
      the stored value, or the AWK blank value when no concrete value is present
    • toAwkString

      public String toAwkString(Object o)
      Convert Strings, Integers, and Doubles to Strings based on the CONVFMT variable contents and the stored Locale.
      Parameters:
      o - Object to convert.
      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
    • isActuallyLong

      public static boolean isActuallyLong(double d)
      Determines whether a double value actually represents a long integer within the limits of floating point precision.
      Parameters:
      d - the double value to examine
      Returns:
      true if d is effectively an integer
    • 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
    • parseFieldNumber

      public static long parseFieldNumber(Object obj)
      Convert a field designator to a non-negative long, raising an AWK runtime exception when the value is invalid.
      Parameters:
      obj - the object identifying the field (for example, the result of a numeric expression)
      Returns:
      the parsed field number as a long
    • 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 int split(Object array, Object string)
      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.
      Returns:
      The number of parts resulting from this split operation.
    • split

      public int split(Object fieldSeparator, Object array, Object string)
      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:
      fieldSeparator - Field separator regular expression.
      array - The array to populate.
      string - The string to split.
      Returns:
      The number of parts resulting from this split operation.
    • getPartitioningReader

      public PartitioningReader getPartitioningReader()
      Returns the underlying PartitioningReader currently in use by the active InputSource, or null if the source is not stream-based.
      Returns:
      the active reader, or null
    • getInputLine

      public String getInputLine()

      Getter for the field inputLine.

      Returns:
      a String object
    • getNF

      public Integer getNF()
      Retrieve the current value of NF. When fields are initialized this returns the number of fields in $0; otherwise 0.
      Returns:
      current NF value
    • setNF

      public void setNF(Object nfObject)
      Set NF to the specified value and update $0 and fields accordingly.
      Parameters:
      nfObject - value to assign to NF
    • getNR

      public Long getNR()
      Get the current NR value as tracked by JRT.
      Returns:
      current NR
    • setNR

      public void setNR(Object value)
      Assign NR to a specific value; also updates the VariableManager copy.
      Parameters:
      value - value to assign
    • getFNR

      public Long getFNR()
      Get the current FNR value as tracked by JRT.
      Returns:
      current FNR
    • setFNR

      public void setFNR(Object value)
      Assign FNR to a specific value; also updates the VariableManager copy.
      Parameters:
      value - value to assign
    • getFSVar

      public Object getFSVar()
      Get FS from the VariableManager.
      Returns:
      FS value
    • getFSString

      public String getFSString()
      Returns the current FS value as a string.
      Returns:
      current field separator
    • setFS

      public void setFS(Object value)
      Set FS via the VariableManager.
      Parameters:
      value - new FS value
    • getRSVar

      public Object getRSVar()
      Get RS from the VariableManager.
      Returns:
      RS value
    • getRSString

      public String getRSString()
      Returns the current RS value as a string.
      Returns:
      current record separator
    • setRS

      public void setRS(Object value)
      Set RS via the VariableManager and apply it to the current reader if any.
      Parameters:
      value - new RS value
    • getOFSVar

      public Object getOFSVar()
      Get OFS from the VariableManager.
      Returns:
      OFS value
    • getOFSString

      public String getOFSString()
      Returns the current OFS value as a string.
      Returns:
      current output field separator
    • setOFS

      public void setOFS(Object value)
      Set OFS via the VariableManager.
      Parameters:
      value - new OFS value
    • getORSVar

      public Object getORSVar()
      Get ORS from the VariableManager.
      Returns:
      ORS value
    • getORSString

      public String getORSString()
      Returns the current ORS value as a string.
      Returns:
      current output record separator
    • setORS

      public void setORS(Object value)
      Set ORS via the VariableManager.
      Parameters:
      value - new ORS value
    • getRSTART

      public Integer getRSTART()
      Get RSTART tracked by JRT (1-based).
      Returns:
      current RSTART
    • setRSTART

      public void setRSTART(Object value)
      Set RSTART tracked by JRT (1-based) and mirror to VariableManager.
      Parameters:
      value - new RSTART
    • getRLENGTH

      public Integer getRLENGTH()
      Get RLENGTH tracked by JRT.
      Returns:
      current RLENGTH
    • setRLENGTH

      public void setRLENGTH(Object value)
      Set RLENGTH tracked by JRT and mirror to VariableManager.
      Parameters:
      value - new RLENGTH
    • getFILENAME

      public String getFILENAME()
      Get FILENAME as tracked by JRT.
      Returns:
      current FILENAME (empty string for stdin/pipe)
    • setFILENAMEViaJrt

      public void setFILENAMEViaJrt(String name)
      Set FILENAME through VariableManager and update JRT mirror.
      Parameters:
      name - file name to set
    • getSUBSEPVar

      public Object getSUBSEPVar()
      Get SUBSEP from the VariableManager.
      Returns:
      SUBSEP value
    • getSUBSEPString

      public String getSUBSEPString()
      Returns the current SUBSEP value as a string.
      Returns:
      current multidimensional-array subscript separator
    • setSUBSEP

      public void setSUBSEP(Object value)
      Set SUBSEP via the VariableManager.
      Parameters:
      value - new SUBSEP value
    • getCONVFMTVar

      public Object getCONVFMTVar()
      Get CONVFMT from the VariableManager.
      Returns:
      CONVFMT value
    • getCONVFMTString

      public String getCONVFMTString()
      Returns the current CONVFMT value as a string.
      Returns:
      current numeric conversion format
    • setCONVFMT

      public void setCONVFMT(Object value)
      Set CONVFMT via the VariableManager.
      Parameters:
      value - new CONVFMT value
    • getOFMTString

      public String getOFMTString()
      Get OFMT from the VariableManager.
      Returns:
      OFMT value
    • setOFMT

      public void setOFMT(Object value)
      Set OFMT via the VariableManager.
      Parameters:
      value - new OFMT value
    • getARGCVar

      public Object getARGCVar()
      Get ARGC from the VariableManager.
      Returns:
      ARGC value
    • setARGC

      public void setARGC(Object value)
      Set ARGC via the VariableManager.
      Parameters:
      value - new ARGC value
    • setInputLine

      public void setInputLine(String inputLine)

      Setter for the field inputLine.

      Parameters:
      inputLine - a String object
    • assignInputLineFromGetline

      public void assignInputLineFromGetline(Object value)
      Assigns $0 from a getline result and initializes $1..$NF.
      Parameters:
      value - getline result assigned to $0
    • consumeInput

      public boolean consumeInput(InputSource source) throws IOException
      Attempt to consume one record from a structured input source and expose it as the current input record.
      Parameters:
      source - source strategy that provides records and optional pre-split fields
      Returns:
      true if a record was consumed; false when the source is exhausted
      Throws:
      IOException - if the source raises an I/O error
    • consumeInputToTarget

      public String consumeInputToTarget(InputSource source) throws IOException
      Attempt to consume one record from a structured input source for getline target, returning only the input text and leaving the current input record state untouched.
      Parameters:
      source - source strategy that provides records and optional pre-split fields
      Returns:
      the consumed input text, or null when the source is exhausted
      Throws:
      IOException - if the source raises an I/O error
    • consumeInputForEval

      public boolean consumeInputForEval(InputSource source) throws IOException
      Consume at most one record from a structured source for expression evaluation.
      Parameters:
      source - source strategy that provides records and optional pre-split fields
      Returns:
      true if a record was consumed, false otherwise
      Throws:
      IOException - if the source raises an I/O error
    • jrtParseFields

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

      public boolean hasInputFields()
      Returns:
      true if at least one input field has been initialized.
    • jrtSetNF

      public void jrtSetNF(Object nfObj)
      Adjust the current input field list and $0 when NF is updated by the AWK script. Fields are either truncated or extended with empty values so that NF truly reflects the number of fields.
      Parameters:
      nfObj - New value for NF
    • jrtGetInputField

      public Object jrtGetInputField(Object fieldnumObj)
      Retrieve the contents of a particular input field.
      Parameters:
      fieldnumObj - Object referring to the field number.
      Returns:
      Contents of the field.
    • jrtGetInputField

      public Object jrtGetInputField(long fieldnum)

      jrtGetInputField.

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

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

      public Integer jrtConsumeFileInputForGetline(String fileNameParam)

      jrtConsumeFileInputForGetline.

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

      public Integer jrtConsumeCommandInputForGetline(String cmdString)
      Retrieve the next line of output from a command, executing the command if necessary and store it to $0.
      Parameters:
      cmdString - 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
    • printDefault

      public void printDefault(Object[] values) throws IOException
      Writes a standard AWK print operation to the default output.
      Parameters:
      values - values to print
      Throws:
      IOException - if the sink cannot be written to
    • printToFile

      public void printToFile(String fileNameParam, boolean append, Object[] values) throws IOException
      Writes a standard AWK print operation to a redirected file.
      Parameters:
      fileNameParam - target file name
      append - whether output should be appended
      values - values to print; an empty array prints $0
      Throws:
      IOException - if the sink cannot be written to
    • printToProcess

      public void printToProcess(String cmd, Object[] values) throws IOException
      Writes a standard AWK print operation to a redirected process.
      Parameters:
      cmd - command to execute
      values - values to print; an empty array prints $0
      Throws:
      IOException - if the sink cannot be written to
    • printfDefault

      public void printfDefault(String format, Object[] values) throws IOException
      Writes a formatted AWK output string to the specified sink.
      Parameters:
      format - format string passed to printf
      values - values supplied after the format string
      Throws:
      IOException - if the sink cannot be written to
    • printfToFile

      public void printfToFile(String fileNameParam, boolean append, String format, Object[] values) throws IOException
      Writes formatted AWK output to a redirected file.
      Parameters:
      fileNameParam - target file name
      append - whether output should be appended
      format - format string passed to printf
      values - values supplied after the format string
      Throws:
      IOException - if the sink cannot be written to
    • printfToProcess

      public void printfToProcess(String cmd, String format, Object[] values) throws IOException
      Writes formatted AWK output to a redirected process.
      Parameters:
      cmd - command to execute
      format - format string passed to printf
      values - values supplied after the format string
      Throws:
      IOException - if the sink cannot be written to
    • jrtGetPrintStream

      public PrintStream jrtGetPrintStream(String fileNameParam, boolean append)
      Retrieve the PrintStream which writes to a particular file, creating the PrintStream if necessary.
      Parameters:
      fileNameParam - 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 fileNameParam) throws IOException

      jrtConsumeFileInput.

      Parameters:
      fileNameParam - 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 fileNameParam)
      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:
      fileNameParam - The filename/command process to close.
      Returns:
      Integer(0) upon a successful close, Integer(-1) otherwise.
    • jrtCloseAll

      public void jrtCloseAll()

      jrtCloseAll.

    • jrtSystem

      public 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 fmtArg, Object... arr) throws IllegalFormatException

      sprintfFunctionNoCatch.

      Parameters:
      locale - a Locale object
      fmtArg - 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 fmtArg, Object... arr)

      printfFunctionNoCatch.

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

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

      printfFunctionNoCatch.

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

      public static String substr(Object startposObj, String str)

      substr.

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

      public static String substr(Object sizeObj, Object startposObj, String str)

      substr.

      Parameters:
      sizeObj - a Object object
      startposObj - a Object object
      str - a String object
      Returns:
      a String object
    • timeSeed

      public static int timeSeed()

      timeSeed.

      Returns:
      a int
    • newRandom

      public static BSDRandom newRandom(int seed)

      newRandom.

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

      public void applyRS(Object rsObj)

      applyRS.

      Parameters:
      rsObj - a Object object