Commit af221bd9 authored by Jonathan Wilkes's avatar Jonathan Wilkes
Browse files

simplify GUI-side parser, and send raw (i.e., old) sys_vgui and sys_gui calls...

simplify GUI-side parser, and send raw (i.e., old) sys_vgui and sys_gui calls to just post to the console
parent 50cf48fe
......@@ -989,73 +989,46 @@ exports.connect = connect;
// Add a 'data' event handler for the client socket
// data parameter is what the server sent to this socket
// Pd can send us different types of data:
// 1) The old style tcl commands with "\n" at end (or "\\\n" for continuation)
// 2) new style commands: selector "stringarg",42,"stringarg3",etc.
// Below we separate the wheat from chaff, eval'ing the new commands and just
// printing the old ones in blue to the console
// To facilitate this, the new style commands are always preceded with an
// alarm bell '\a', and end with a vertical tab '\v'. These should produce
// a decent stop-gap since they are rarely used in Pd patch text.
// We're not receiving FUDI (i.e., Pd) messages. Right now we're just using
// an alarm bell '\a' to signal the end of a message. This is easier than
// checking for unescaped semicolons, since it only requires a check for a
// single byte. Of course this makes it more brittle, so it can be changed
// later if needed.
function init_socket_events () {
var next_command = ""; // A not-quite-FUDI command: selector arg1,arg2,etc.
// These are formatted on the C side to be easy
// to parse here in javascript
var old_command = ""; // Old-style sys_vgui cmds (printed to console)
var cmdHeader = false;
var perfect_parser = function(data) {
var i, len, selector, args;
len = data.length;
for (i = 0; i < len; i++) {
if (cmdHeader) {
// check for end of command:
if (data[i] === 11) { // vertical tab "\v"
// decode next_command
try {
// This should work for all utf-8 content
next_command = decodeURIComponent(next_command);
}
catch(err) {
// This should work for ISO-8859-1
next_command = unescape(next_command);
}
// Turn newlines into backslash + "n" so
// eval will do the right thing
next_command = next_command.replace(/\n/g, "\\n");
selector = next_command.slice(0, next_command.indexOf(" "));
args = next_command.slice(selector.length + 1);
cmdHeader = false;
next_command = "";
// Now evaluate it
//post("Evaling: " + selector + "(" + args + ");");
eval(selector + "(" + args + ");");
} else {
next_command += "%" +
("0" // leading zero (for rare case of single digit)
+ data[i].toString(16)) // to hex
.slice(-2); // remove extra leading zero
// check for end of command:
if (data[i] === 7) { // alarm bell
// decode next_command
try {
// This should work for all utf-8 content
next_command = decodeURIComponent(next_command);
}
} else if (data[i] === 7) { // ASCII alarm bell "\a"
// if we have an old-style message, print it out
if (old_command !== "") {
var old_command_output;
try {
old_command_output = decodeURIComponent(old_command);
}
catch(err) {
old_command_output = unescape(old_command);
}
old_command= "";
//post("warning: old command: " + old_command_output,
// "blue");
catch(err) {
// This should work for ISO-8859-1
next_command = unescape(next_command);
}
cmdHeader = true;
// Turn newlines into backslash + "n" so
// eval will do the right thing with them
next_command = next_command.replace(/\n/g, "\\n");
selector = next_command.slice(0, next_command.indexOf(" "));
args = next_command.slice(selector.length + 1);
next_command = "";
// Now evaluate it
//post("Evaling: " + selector + "(" + args + ");");
eval(selector + "(" + args + ");");
} else {
// this is an old-style sys_vgui
old_command += "%" + ("0" + data[i].toString(16)).slice(-2);
next_command += "%" +
("0" // leading zero (for rare case of single digit)
+ data[i].toString(16)) // to hex
.slice(-2); // remove extra leading zero
}
}
};
......@@ -1064,36 +1037,21 @@ function init_socket_events () {
var i, len, selector, args;
len = data.length;
for (i = 0; i < len; i++) {
if (cmdHeader) {
// check for end of command:
if (data[i] === "\v") { // vertical tab
// we have the next_command...
// Turn newlines into backslash + "n" so
// eval will do the right thing
next_command = next_command.replace(/\n/g, "\\n");
selector = next_command.slice(0, next_command.indexOf(" "));
args = next_command.slice(selector.length + 1);
cmdHeader = false;
next_command = "";
// Now evaluate it-- unfortunately the V8 engine can't
// optimize this eval call.
//post("Evaling: " + selector + "(" + args + ");");
eval(selector + "(" + args + ");");
} else {
next_command += data[i];
}
} else if (data[i] === String.fromCharCode(7)) {
// ASCII alarm bell "\a"
// if we have an old-style message, print it out
if (old_command !== "") {
//post("warning: old command: " + old_command_output,
// "blue");
old_command= "";
}
cmdHeader = true;
// check for end of command:
if (data[i] === String.fromCharCode(7)) { // vertical tab
// we have the next_command...
// Turn newlines into backslash + "n" so
// eval will do the right thing
next_command = next_command.replace(/\n/g, "\\n");
selector = next_command.slice(0, next_command.indexOf(" "));
args = next_command.slice(selector.length + 1);
next_command = "";
// Now evaluate it-- unfortunately the V8 engine can't
// optimize this eval call.
//post("Evaling: " + selector + "(" + args + ");");
eval(selector + "(" + args + ");");
} else {
// this is an old-style sys_vgui
old_command += data[i];
next_command += data[i];
}
}
};
......
......@@ -458,9 +458,9 @@ static int socketreceiver_doread(t_socketreceiver *x)
binbuf_text(inbinbuf, messbuf, bp - messbuf);
if (sys_debuglevel & DEBUG_MESSDOWN) {
if (stderr_isatty)
fprintf(stderr,"<- \e[0;1;36m%.*s\e[0m\n", bp - messbuf, messbuf);
fprintf(stderr,"\n<- \e[0;1;36m%.*s\e[0m", bp - messbuf, messbuf);
else
fprintf(stderr,"<- %.*s\n", bp - messbuf, messbuf);
fprintf(stderr,"\n<- %.*s", bp - messbuf, messbuf);
}
x->sr_inhead = inhead;
x->sr_intail = intail;
......@@ -715,13 +715,14 @@ void sys_vvgui(const char *fmt, va_list ap) {
}
if (sys_debuglevel & DEBUG_MESSUP) {
//blargh();
int begin = lastend=='\n' || lastend=='\r' || lastend==-1;
//int begin = lastend=='\n' || lastend=='\r' || lastend==-1;
int begin = lastend=='\a' || lastend==-1;
if (stderr_isatty)
fprintf(stderr, "%s\e[0;1;35m%s\e[0m",
begin ? "-> " : "", sys_guibuf + sys_guibufhead);
begin ? "\n-> " : "", sys_guibuf + sys_guibufhead);
else
fprintf(stderr, "%s%s",
begin ? "-> " : "", sys_guibuf + sys_guibufhead);
begin ? "\n-> " : "", sys_guibuf + sys_guibufhead);
}
sys_guibufhead += msglen;
sys_bytessincelastping += msglen;
......@@ -736,16 +737,25 @@ void sys_vgui(const char *fmt, ...) {
va_start(ap, fmt);
sys_vvgui(fmt,ap);
}
/* This was used by the old command interface, but is no longer used. */
void sys_vvguid(const char *file, int line, const char *fmt, va_list ap) {
if ((sys_debuglevel&4) && /*line>=0 &&*/ (lastend=='\n' || lastend=='\r' || lastend==-1)) {
sys_vgui("AT %s:%d; ",file,line);
}
sys_vvgui(fmt,ap);
}
/* Old GUI command interface... */
void sys_vguid(const char *file, int line, const char *fmt, ...) {
va_list ap;
va_list ap;
char buf[MAXPDSTRING];
va_start(ap, fmt);
sys_vvguid(file,line,fmt,ap);
vsnprintf(buf, MAXPDSTRING-1, fmt, ap);
va_end(ap);
/* For old commands, we're just posting them to the pd console */
post("Old command at %s:%d:%s", file, line, buf);
//sys_vvguid(file,line,fmt,ap);
}
void sys_gui(const char *s)
{
......@@ -773,7 +783,7 @@ char *escape_double_quotes(const char *src) {
void gui_end_vmess(void)
{
sys_gui("\v");
sys_gui("\a");
}
......@@ -785,7 +795,7 @@ void gui_do_vmess(const char *sel, char *fmt, int end, va_list ap)
int nargs = 0;
char *fp = fmt;
//va_start(ap, end);
sys_vgui("\a%s ", sel); /* use a bell to signal the beginning of msg
sys_vgui("%s ", sel); /* use a bell to signal the beginning of msg
(this can be replaced with any other obscure
ascii escape) */
while (*fp)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment