mirror of https://github.com/contiki-ng/mspsim
Changed file redirection in CLI to be more similar to normal shell. ">file" will overwrite file and ">>file" will append to file. Closing a file will stop all attached commands
git-svn-id: https://mspsim.svn.sourceforge.net/svnroot/mspsim/mspsim@638 23d1a52b-0c3c-0410-b72d-8f29ab48fe35
This commit is contained in:
parent
887e889f05
commit
8b4d057a7e
|
|
@ -1,8 +1,7 @@
|
|||
# autorun script for MSPSim
|
||||
# - all commands will run after loaded firmware into MSPSim
|
||||
#exec rm log.txt
|
||||
#log CC2420 >log.txt
|
||||
#printcalls >log.txt
|
||||
#printcalls >>log.txt
|
||||
|
||||
# Install and activate the plugin 'ContikiChecker'
|
||||
# install ContikiChecker
|
||||
|
|
@ -12,5 +11,5 @@
|
|||
service controlgui start
|
||||
service nodegui start
|
||||
#service stackchart start
|
||||
rflistener output CC2420 > rfdata.txt
|
||||
#rflistener output CC2420 >> rfdata.txt
|
||||
start
|
||||
|
|
|
|||
|
|
@ -1,21 +1,24 @@
|
|||
package se.sics.mspsim.cli;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
|
||||
import se.sics.mspsim.util.ComponentRegistry;
|
||||
|
||||
public class FileCommands implements CommandBundle {
|
||||
|
||||
private Hashtable <String, FileTarget> fileTargets = new Hashtable<String, FileTarget>();
|
||||
private final Hashtable <String, FileTarget> fileTargets = new Hashtable<String, FileTarget>();
|
||||
|
||||
public void setupCommands(final ComponentRegistry registry, CommandHandler handler) {
|
||||
// TODO: this should also be "registered" as a "sink".
|
||||
// probably this should be handled using ">" instead!
|
||||
handler.registerCommand(">", new FileTargetCommand(fileTargets,
|
||||
null, "<filename>", false));
|
||||
null, "<filename>", false, false));
|
||||
|
||||
handler.registerCommand(">>", new FileTargetCommand(fileTargets,
|
||||
null, "<filename>", false, true));
|
||||
|
||||
handler.registerCommand("tee", new FileTargetCommand(fileTargets,
|
||||
"redirect to file and std-out", "<filename>", true));
|
||||
"redirect to file and standard out", "<filename>", true, true));
|
||||
|
||||
handler.registerCommand("fclose", new BasicCommand("close the specified file", "<filename>") {
|
||||
public int executeCommand(CommandContext context) {
|
||||
|
|
@ -23,21 +26,28 @@ public class FileCommands implements CommandBundle {
|
|||
FileTarget ft = fileTargets.get(name);
|
||||
if (ft != null) {
|
||||
context.out.println("Closing file " + name);
|
||||
fileTargets.remove(name);
|
||||
ft.close();
|
||||
return 0;
|
||||
} else {
|
||||
context.err.println("Could not find the open file " + name);
|
||||
return 1;
|
||||
}
|
||||
context.err.println("Could not find the open file " + name);
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
|
||||
handler.registerCommand("files", new BasicCommand("list open files", "") {
|
||||
public int executeCommand(CommandContext context) {
|
||||
for (Iterator<FileTarget> iterator = fileTargets.values().iterator(); iterator.hasNext();) {
|
||||
FileTarget type = iterator.next();
|
||||
context.out.println(type.getName());
|
||||
FileTarget[] files = null;
|
||||
synchronized (fileTargets) {
|
||||
if (fileTargets.size() > 0) {
|
||||
files = fileTargets.values().toArray(new FileTarget[fileTargets.size()]);
|
||||
}
|
||||
}
|
||||
if (files == null) {
|
||||
context.out.println("There are no open files.");
|
||||
} else {
|
||||
for (FileTarget type : files) {
|
||||
context.out.println(type.getStatus());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,48 +42,141 @@ package se.sics.mspsim.cli;
|
|||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* @author joakim
|
||||
*
|
||||
*/
|
||||
public class FileTarget implements LineListener {
|
||||
public class FileTarget {
|
||||
|
||||
private final FileWriter out;
|
||||
private final String name;
|
||||
private static final boolean DEBUG = false;
|
||||
|
||||
public FileTarget(String name) throws IOException {
|
||||
this.out = new FileWriter(name);
|
||||
this.name = name;
|
||||
}
|
||||
private final Hashtable<String,FileTarget> fileTargets;
|
||||
private final String name;
|
||||
private final FileWriter out;
|
||||
private ArrayList<CommandContext> contexts = new ArrayList<CommandContext>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see se.sics.mspsim.cli.LineListener#lineRead(java.lang.String)
|
||||
*/
|
||||
public void lineRead(String line) {
|
||||
if (line == null) {
|
||||
close();
|
||||
} else {
|
||||
try {
|
||||
out.write(line);
|
||||
out.write('\n');
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
};
|
||||
public FileTarget(Hashtable<String,FileTarget> fileTargets, String name,
|
||||
boolean append) throws IOException {
|
||||
this.fileTargets = fileTargets;
|
||||
this.out = new FileWriter(name, append);
|
||||
this.name = name;
|
||||
fileTargets.put(name, this);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(name);
|
||||
synchronized (fileTargets) {
|
||||
if (contexts != null) {
|
||||
sb.append(" \tPIDs: [");
|
||||
for (int i = 0, n = contexts.size(); i < n; i++) {
|
||||
int pid = contexts.get(i).getPID();
|
||||
if (i > 0) {
|
||||
sb.append(',');
|
||||
}
|
||||
if (pid < 0) {
|
||||
sb.append('?');
|
||||
} else {
|
||||
sb.append(pid);
|
||||
}
|
||||
}
|
||||
sb.append(']');
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void lineRead(CommandContext context, String line) {
|
||||
if (line == null) {
|
||||
removeContext(context);
|
||||
} else {
|
||||
try {
|
||||
out.write(line);
|
||||
out.write('\n');
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(context.err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addContext(CommandContext context) {
|
||||
boolean added = false;
|
||||
synchronized (fileTargets) {
|
||||
if (contexts != null) {
|
||||
contexts.add(context);
|
||||
added = true;
|
||||
if (DEBUG) {
|
||||
System.out.println("FileTarget: new writer to " + name
|
||||
+ " (" + contexts.size() + ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!added) {
|
||||
context.kill();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeContext(CommandContext context) {
|
||||
boolean close = false;
|
||||
synchronized (fileTargets) {
|
||||
if (contexts != null && contexts.remove(context)) {
|
||||
if (DEBUG) {
|
||||
System.out.println("FileTarget: removed writer from "
|
||||
+ name + " (" + contexts.size() + ')');
|
||||
}
|
||||
if (contexts.size() == 0) {
|
||||
close = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (close) {
|
||||
close(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
close(true);
|
||||
}
|
||||
|
||||
private void close(boolean forceClose) {
|
||||
ArrayList<CommandContext> list;
|
||||
synchronized (fileTargets) {
|
||||
if (contexts == null) {
|
||||
// Already closed
|
||||
return;
|
||||
}
|
||||
if (contexts.size() > 0 && !forceClose) {
|
||||
// File still has connected writers.
|
||||
return;
|
||||
}
|
||||
list = contexts;
|
||||
contexts = null;
|
||||
if (fileTargets.get(name) == this) {
|
||||
fileTargets.remove(name);
|
||||
if (DEBUG) {
|
||||
System.out.println("FileTarget: closed file " + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (list != null) {
|
||||
// Close any connected writers
|
||||
for (CommandContext context : list) {
|
||||
context.kill();
|
||||
}
|
||||
}
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// Ignore close errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,39 +4,50 @@ import java.io.IOException;
|
|||
import java.util.Hashtable;
|
||||
|
||||
public class FileTargetCommand extends BasicLineCommand {
|
||||
FileTarget ft;
|
||||
Hashtable<String, FileTarget> fileTargets;
|
||||
private boolean print;
|
||||
private final Hashtable<String, FileTarget> fileTargets;
|
||||
private final boolean print;
|
||||
private final boolean append;
|
||||
|
||||
private FileTarget ft;
|
||||
private CommandContext context;
|
||||
|
||||
public FileTargetCommand(Hashtable<String,FileTarget> fileTargets,
|
||||
String name, String desc, boolean print) {
|
||||
String name, String desc, boolean print, boolean append) {
|
||||
super(name, desc);
|
||||
this.fileTargets = fileTargets;
|
||||
this.print = print;
|
||||
this.append = append;
|
||||
}
|
||||
|
||||
public int executeCommand(CommandContext context) {
|
||||
this.context = context;
|
||||
String fileName = context.getArgument(0);
|
||||
ft = fileTargets.get(fileName);
|
||||
if (ft == null) {
|
||||
try {
|
||||
ft = new FileTarget(fileName);
|
||||
fileTargets.put(fileName, ft);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
synchronized (fileTargets) {
|
||||
ft = fileTargets.get(fileName);
|
||||
if (ft == null) {
|
||||
try {
|
||||
ft = new FileTarget(fileTargets, fileName, append);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(context.err);
|
||||
return -1;
|
||||
}
|
||||
} else if (!append) {
|
||||
context.err.println("File already opened: can not overwrite");
|
||||
return -1;
|
||||
}
|
||||
ft.addContext(context);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public void lineRead(String line) {
|
||||
ft.lineRead(line);
|
||||
if (print) context.out.println(line);
|
||||
ft.lineRead(context, line);
|
||||
}
|
||||
|
||||
|
||||
public void stopCommand(CommandContext context) {
|
||||
// Should this do anything?
|
||||
// Probably depending on the ft's config
|
||||
if (ft != null) {
|
||||
ft.removeContext(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue