Example #1
0
extern "C" LyXFunctionResult applescript_execute_command(const char *cmd, const char *arg) {
    LYXERR(Debug::ACTION, "Running command [" << cmd << "] with arguments [" << arg << "]");
	FuncRequest fr(lyxaction.lookupFunc(cmd), arg);
	fr.setOrigin(FuncRequest::LYXSERVER);
	DispatchResult dr;
	theApp()->dispatch(fr, dr);
	
    string const rval = to_utf8(dr.message());
    char *cstr =(char*)  malloc((rval.size()+1)*sizeof(rval[0]));
    strcpy (cstr, rval.c_str());
    
    // Returns the result
    LyXFunctionResult result;
    result.code = dr.error() ? -1 : 0;
    result.message = cstr;
    
    return result;
}
Example #2
0
// Reads and processes input from client and check
// if the connection has been closed
void ServerSocket::dataCallback(int fd)
{
	map<int, shared_ptr<LyXDataSocket> >::const_iterator it = clients.find(fd);
	if (it == clients.end())
		return;
	shared_ptr<LyXDataSocket> client = it->second;
	string line;
	size_t pos;
	bool saidbye = false;
	while (!saidbye && client->readln(line)) {
		// The protocol must be programmed here
		// Split the key and the data
		if ((pos = line.find(':')) == string::npos) {
			client->writeln("ERROR:" + line + ":malformed message");
			continue;
		}

		string const key = line.substr(0, pos);
		if (key == "LYXCMD") {
			string const cmd = line.substr(pos + 1);
			FuncRequest fr(lyxaction.lookupFunc(cmd));
			fr.setOrigin(FuncRequest::LYXSERVER);
			DispatchResult dr;
			theApp()->dispatch(fr, dr);
			string const rval = to_utf8(dr.message());
			if (dr.error())
				client->writeln("ERROR:" + cmd + ':' + rval);
			else
				client->writeln("INFO:" + cmd + ':' + rval);
		} else if (key == "HELLO") {
			// no use for client name!
			client->writeln("HELLO:");
		} else if (key == "BYE") {
			saidbye = true;
		} else {
			client->writeln("ERROR:unknown key " + key);
		}
	}

	if (saidbye || !client->connected()) {
		clients.erase(fd);
	}
}