This repository was archived by the owner on Jul 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAether.java
More file actions
79 lines (65 loc) · 2.63 KB
/
Copy pathAether.java
File metadata and controls
79 lines (65 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package aether;
import aether.lexer.Lexer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Aether {
private static final Interpreter interpreter = new Interpreter();
// Define your custom language extension here
private static final String FILE_EXTENSION = ".aether";
static void main(String[] args) throws IOException {
if (args.length > 1) {
System.out.println("Usage: java -jar target/java-project-aether-1.0-SNAPSHOT.jar [script" + FILE_EXTENSION + "]");
System.exit(64);
} else if (args.length == 1) {
runFile(args[0]);
} else {
runPrompt();
}
}
private static void runFile(String path) throws IOException {
// Enforce the custom file extension constraint
if (!path.endsWith(FILE_EXTENSION)) {
System.err.println("Error: Invalid file format. Aether source files must end with '" + FILE_EXTENSION + "'.");
System.exit(65); // EX_DATAERR: Input data was incorrect in some way
}
// 2. Define the base directory where your scripts are allowed
var baseDir = Paths.get("scripts").toAbsolutePath().normalize();
// 3. Resolve the requested path against the base directory
var targetPath = baseDir.resolve(path).normalize();
// 4. Vulnerability Check: Ensure the resolved path starts with the base directory
if (!targetPath.startsWith(baseDir)) {
throw new SecurityException("Access denied: File path outside of allowed directory.");
}
// 5. Proceed with execution
var bytes = Files.readAllBytes(targetPath);
run(new String(bytes));
}
private static void runPrompt() throws IOException {
var input = new InputStreamReader(System.in);
var reader = new BufferedReader(input);
for (;;) {
System.out.print("aether> ");
var line = reader.readLine();
if (line == null) break;
run(line);
}
}
private static void run(String source) {
if (source.trim().isEmpty()) return;
try {
var lexer = new Lexer(source);
var tokens = lexer.scanTokens();
var parser = new Parser(tokens);
var statements = parser.parse();
if (statements == null || statements.contains(null)) {
return;
}
interpreter.interpret(statements);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}