Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ Jawk fully implements POSIX AWK, and adds support for the most commonly used gaw

- Builtins, available by default through the built-in GNU Awk compatibility extension: `asort()`, `asorti()`, `typeof()`, `isarray()`, `mkbool()`, `gensub()`, `patsplit()`, `strtonum()`, `systime()`, `mktime()`, `strftime()`, `bindtextdomain()`, `dcgettext()`, `dcngettext()`, and `PROCINFO["sorted_in"]`-controlled array traversal
- Arrays of arrays (`a[i][j]`) and typed regexp literals (`@/re/`)
- Source inclusion with `@include`, namespaces with `@namespace` and `ns::name`, and indirect function calls such as `@functionName(args)`
- `BEGINFILE` / `ENDFILE` special patterns, with the `ERRNO` and `ARGIND` special variables, so a script can hook into the command-line file processing loop and skip unreadable files without a fatal error
- The `nextfile` statement
- The `IGNORECASE`, `SYMTAB`, and `FUNCTAB` special variables

As in gawk, the gawk-specific syntax is not special in `--posix` mode.
The gawk-specific `@` syntax and arrays-of-arrays syntax are rejected in `--posix` mode.

## CLI Example

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/io/jawk/Awk.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,10 @@ protected final <T extends AwkProgram> T compileProgram(
lastAst = null;
if (!scripts.isEmpty()) {
// Parse all script sources into a single AST
AwkParser parser = new AwkParser(this.extensionFunctions, settings.isPosix());
AwkParser parser = new AwkParser(
this.extensionFunctions,
settings.isPosix(),
isSourceIncludeAllowed());
AstNode ast = parser.parse(scripts);
lastAst = ast;
if (ast != null) {
Expand All @@ -521,6 +524,15 @@ protected final <T extends AwkProgram> T compileProgram(
return tuples;
}

/**
* Returns whether scripts compiled by this engine may use {@code @include}.
*
* @return {@code true} for the standard engine
*/
protected boolean isSourceIncludeAllowed() {
return true;
}

/**
* Compile an expression to evaluate (not a full script).
*
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/jawk/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ private static void checkParameterHasArgument(String[] args, int argIdx) {
}
}

private static final Pattern INITIAL_VAR_PATTERN = Pattern.compile("([_a-zA-Z][_0-9a-zA-Z]*)=(.*)");
private static final Pattern INITIAL_VAR_PATTERN = Pattern
.compile(
"((?:[_a-zA-Z][_0-9a-zA-Z]*::)?[_a-zA-Z][_0-9a-zA-Z]*)=(.*)");

/**
* Parses a variable assignment passed via <code>-v</code> and stores it in the
Expand All @@ -392,6 +394,9 @@ private static void addVariable(AwkSettings settings, String keyValue) {
"keyValue \"" + keyValue + "\" must be of the form \"name=value\"");
}
String name = m.group(1);
if (name.startsWith("awk::")) {
name = name.substring("awk::".length());
}
Comment thread
bertysentry marked this conversation as resolved.
String valueString = m.group(2);
settings.putVariable(name, valueString);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/jawk/SandboxedAwk.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public AwkExpression compileExpression(String expression, boolean disableOptimiz
return compileExpression(expression, disableOptimizeParam, new SandboxedCompiledAwkExpression());
}

@Override
protected boolean isSourceIncludeAllowed() {
return false;
}

@Override
public AVM createAvm() {
return createAvm(getSettings());
Expand Down
Loading
Loading