Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrates to use JepConfig stdout, stderr from setStream method #36

Merged
merged 7 commits into from
Apr 17, 2023
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
44 changes: 0 additions & 44 deletions data/python/jepstream.py

This file was deleted.

4 changes: 1 addition & 3 deletions src/main/java/ghidrathon/GhidrathonConsoleInputThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ public void run() {

try {

python = GhidrathonInterpreter.get();
python = GhidrathonInterpreter.get(out, err);

// set Python stdout and stderr to reference our console window
python.setStreams(out, err);
python.printWelcome();

} catch (RuntimeException e) {
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/ghidrathon/GhidrathonScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ protected void run() {

try {

python = GhidrathonInterpreter.get();

// redirect Python stdout and stderr to console window
python.setStreams(out, err);
python = GhidrathonInterpreter.get(out, err);

// run Python script from Python interpreter
python.runScript(getSourceFile(), this);
Expand Down Expand Up @@ -77,10 +74,7 @@ public void runScript(String name, GhidraState scriptState) {

try {

python = GhidrathonInterpreter.get();

// redirect Python stdout and stderr to console window
python.setStreams(out, err);
python = GhidrathonInterpreter.get(out, err);

ResourceFile source = GhidraScriptUtil.findScriptByName(name);
if (source == null) {
Expand Down
56 changes: 21 additions & 35 deletions src/main/java/ghidrathon/interpreter/GhidrathonInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.io.IOException;
import java.io.FileNotFoundException;

import org.apache.commons.io.output.WriterOutputStream;

import generic.jar.ResourceFile;

import ghidra.framework.Application;
Expand Down Expand Up @@ -48,7 +50,7 @@ public class GhidrathonInterpreter {
* @throws JepException
* @throws IOException
*/
private GhidrathonInterpreter() throws JepException, IOException{
private GhidrathonInterpreter(PrintWriter out, PrintWriter err) throws JepException, IOException{

// configure the Python includes path with the user's Ghdira script directory
String paths = "";
Expand All @@ -66,6 +68,22 @@ private GhidrathonInterpreter() throws JepException, IOException{

// configure Python includes Path
config.addIncludePaths(paths);

// configure Jep stdout and stderr
config.redirectStdout(new WriterOutputStream(out, System.getProperty("file.encoding")) {
@Override
public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
flush(); // flush the output to ensure it is displayed in real-time
}
});
config.redirectStdErr(new WriterOutputStream(err, System.getProperty("file.encoding")) {
@Override
public void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
flush(); // flush the error to ensure it is displayed in real-time
}
});

// we must set the native Jep library before creating a Jep instance
setJepNativeBinaryPath();
Expand Down Expand Up @@ -206,11 +224,11 @@ private void injectScriptHierarchy(GhidraScript script) throws JepException, Fil
* @return GhidrathonInterpreter
* @throws RuntimeException
*/
public static GhidrathonInterpreter get() throws RuntimeException {
public static GhidrathonInterpreter get(PrintWriter out, PrintWriter err) throws RuntimeException {

try {

return new GhidrathonInterpreter();
return new GhidrathonInterpreter(out, err);

} catch (Exception e) {

Expand Down Expand Up @@ -380,38 +398,6 @@ public void runScript(ResourceFile file, GhidraScript script) {

}

/**
* Set output and error streams for Jep instance.
*
* Output and error streams from Python interpreter are redirected to the specified streams. If these are not set, this data is lost.
*
* @param out output stream
* @param err error stream
*/
public void setStreams(PrintWriter out, PrintWriter err) {

try {

ResourceFile file = Application.getModuleDataFile(extname, "python/jepstream.py");
ooprathamm marked this conversation as resolved.
Show resolved Hide resolved

jep.set("GhidraPluginToolConsoleOutWriter", out);
jep.set("GhidraPluginToolConsoleErrWriter", err);

jep.runScript(file.getAbsolutePath());

this.out = out;
this.err = err;

} catch (JepException | FileNotFoundException e) {

// ensure stack trace prints to err stream for user
e.printStackTrace();
throw new RuntimeException(e);

}

}

public void printWelcome() {

try {
Expand Down