Package io.jawk.ext

Class GawkExtension

All Implemented Interfaces:
JawkExtension

public class GawkExtension extends AbstractExtension implements JawkExtension
GNU awk compatibility extension for array sorting and type introspection.
  • Constructor Details

    • GawkExtension

      public GawkExtension()
  • Method Details

    • getExtensionName

      public String getExtensionName()

      getExtensionName.

      Specified by:
      getExtensionName in interface JawkExtension
      Overrides:
      getExtensionName in class AbstractExtension
      Returns:
      name of the extension package.
    • initializeGawkVariables

      public void initializeGawkVariables(AVM avmParam, JRT jrt)
      Installs the PROCINFO["sorted_in"] traversal order for for-in loops and binds this per-engine extension instance to its interpreter. SYMTAB and FUNCTAB are populated by the interpreter itself.
      Parameters:
      avmParam - interpreter about to execute
      jrt - runtime associated with avmParam
    • asort

      public Long asort(Map<Object,Object> source, Map<Object,Object> dest, Object how)
      Sorts an array by value, optionally writing the result to another array.
      Parameters:
      source - source array
      dest - destination array, or null to sort in place
      how - predefined sorting mode, or null for the default
      Returns:
      number of sorted elements
    • asorti

      public Long asorti(Map<Object,Object> source, Map<Object,Object> dest, Object how)
      Sorts an array by index, optionally writing the result to another array.
      Parameters:
      source - source array
      dest - destination array, or null to sort in place
      how - predefined sorting mode, or null for the default
      Returns:
      number of sorted elements
    • typeof

      public String typeof(Object value, Map<Object,Object> meta)
      Returns the gawk type category for a value.
      Parameters:
      value - value to inspect
      meta - optional metadata destination array
      Returns:
      gawk type name
    • isarray

      public Long isarray(Object value)
      Returns whether the supplied value is an array.
      Parameters:
      value - value to inspect
      Returns:
      1 for arrays, 0 otherwise
    • mkbool

      public GawkBool mkbool(Object value)
      Creates a boolean-typed numeric value used by gawk's test suite.
      Parameters:
      value - truth value
      Returns:
      boolean numeric value
    • gensub

      public String gensub(Object regexp, Object replacement, Object how, Object target)
      Performs a small gawk-compatible gensub() substitution.
      Parameters:
      regexp - regular expression
      replacement - replacement text
      how - occurrence selector or g
      target - target text, or null to default to $0
      Returns:
      substituted text
    • systime

      public Long systime()
      Returns the current time in seconds since the epoch.
      Returns:
      seconds since 1970-01-01 00:00:00 UTC
    • mktime

      public Long mktime(Object datespec, Object utcFlag)
      Converts a gawk "YYYY MM DD HH MM SS [DST]" date specification into seconds since the epoch, normalizing out-of-range values.

      The conversion follows Java's calendar rules: an ambiguous wall time during a DST fall-back resolves to its standard-time occurrence, and a positive DST hint applies the zone's current savings (zones without DST ignore the hint). See the documented differences with gawk, whose behavior in these edge cases follows the C library.

      Parameters:
      datespec - date specification with six or seven numeric fields
      utcFlag - when truthy, interpret the specification as UTC
      Returns:
      seconds since the epoch, or -1 when the specification is invalid
    • strftime

      public String strftime(Object format, Object timestamp, Object utcFlag)
      Formats a timestamp with C strftime(3) conversion specifiers.
      Parameters:
      format - format string; defaults to PROCINFO["strftime"] or gawk's "%a %b %e %H:%M:%S %Z %Y"
      timestamp - seconds since the epoch; defaults to the current time
      utcFlag - when truthy, format in UTC instead of the local time zone
      Returns:
      formatted timestamp
    • strtonum

      public Number strtonum(Object value)
      Converts a string to a number, recognizing gawk's non-decimal notation: a 0x prefix selects hexadecimal and a leading 0 over octal digits selects octal.
      Parameters:
      value - value to convert
      Returns:
      numeric value
    • patsplit

      public Long patsplit(Object source, Map<Object,Object> array, Object fieldpat, Map<Object,Object> seps)
      Splits a string by content: pieces matching fieldpat become fields, the text between them becomes separators. This is gawk's patsplit(), the function form of FPAT field splitting.
      Parameters:
      source - text to split
      array - destination array for the fields
      fieldpat - field pattern, or null to use the FPAT global variable (default "[^[:space:]]+")
      seps - optional destination array for the separators; entry 0 holds the text before the first field
      Returns:
      number of fields
    • dcgettext

      public String dcgettext(Object string, Object domain, Object category)
      Returns the translation of a string in the given text domain and locale category. Jawk ships no message catalogs, so the text is returned untranslated, exactly like gawk without a matching .mo file.
      Parameters:
      string - text to translate
      domain - text domain; defaults to TEXTDOMAIN
      category - locale category; validated, then ignored (no catalogs)
      Returns:
      the untranslated text
    • dcngettext

      public String dcngettext(Object singular, Object plural, Object number, Object domain, Object category)
      Returns the singular or plural form of a message according to a number. Without message catalogs this applies the English plural rule, exactly like gawk without a matching .mo file.
      Parameters:
      singular - singular form
      plural - plural form
      number - quantity deciding the form
      domain - text domain; defaults to TEXTDOMAIN
      category - locale category; validated, then ignored (no catalogs)
      Returns:
      singular when the number is 1, plural otherwise
    • bindtextdomain

      public String bindtextdomain(Object directory, Object domain)
      Binds a text domain to a message catalog directory and returns the binding, mirroring gawk's bindtextdomain().
      Parameters:
      directory - directory to bind; the AWK empty string queries the current binding without changing it
      domain - text domain; defaults to TEXTDOMAIN
      Returns:
      the directory now bound to the domain