예제 #1
0
BOOL PluginOILFilter::DoExport(CCLexFile* pXarFile, PathName* pPath)
{
	// Here we should just need to wait for the process started in GetExportFile 
	// to finish
	// Check stderr for errors and progress

	// However for now we will instead
	// Run the plugin with the following options
	// -e -g -f <filename> -x <xmlfilename>

	// The xmlfilename is a path to a user and filter specific file
	// e.g. ~/.XaraLX/filtername.xml

	// Check stderr for errors

	wxString sCommand(m_DoExport);
	sCommand.Replace(_T("%OUT%"), (LPCTSTR)pPath->GetPath());
	sCommand.Replace(_T("%XML%"), m_XMLFile.GetFullPath());

	TRACEUSER("Gerry", _T("Running '%s'"), sCommand.c_str());

	CCDiskFile TempFile(CCFILE_DEFAULTSIZE, FALSE, FALSE);
	if (!TempFile.open(m_TempXarFile, ios::in | ios::binary))
	{
		// report an error here
		return FALSE;
	}

	// Create a process with the TempFile as the stdin
	PluginFilterProcess* pTheProc = new PluginFilterProcess((PluginNativeFilter*)Parent, &TempFile, NULL);

	INT32 code = pTheProc->Execute(sCommand);
	TRACEUSER("Gerry", _T("Execute returned %d"), code);
	TempFile.close();
	if (code != 0)
	{
		TRACEUSER("Gerry", _T("Execution of '%s' failed (%d)"), sCommand.c_str(), code);
		// Extract error and report it
		pTheProc->ReportError();
		delete pTheProc;
		return(FALSE);
	}

	pTheProc->ReportWarning();
	delete pTheProc;

	return(TRUE);
}
예제 #2
0
INT32 PluginOILFilter::HowCompatible(PathName& FileName)
{
	INT32 HowCompatible = 0;

	// Here we need to run the plugin synchronously with the following options
	// -c -f <filename>

	// Check stderr for errors
	// Get HowCompatible from stdout
	if (!m_CanImport.IsEmpty())
	{
		wxString sCommand(m_CanImport);
		sCommand.Replace(_T("%IN%"), (LPCTSTR)FileName.GetPath());

		TRACEUSER("Gerry", _T("Running '%s'"), sCommand.c_str());

		wxArrayString saOutput;
		wxArrayString saErrors;
		INT32 code = wxExecute(sCommand, saOutput, saErrors, wxEXEC_NODISABLE);
		TRACEUSER("Gerry", _T("wxExecute returned %d"), code);
		if (code == 0)
		{
			// Extract the value from saOutput
			if (saOutput.Count() > 0)
			{
				INT32 Val = wxAtoi(saOutput[0]);
				TRACEUSER("Gerry", _T("Command '%s' returned string '%s'"), sCommand.c_str(), saOutput[0].c_str());
				TRACEUSER("Gerry", _T("Value = %d"), Val);
				if (Val >= 0 && Val <= 10)
				{
					HowCompatible = Val;
				}
			}
			else
			{
				TRACEUSER("Gerry", _T("Command '%s' returned no output value"), sCommand.c_str());
			}
		}
		else
		{
			TRACEUSER("Gerry", _T("Command '%s' exited with code %d"), sCommand.c_str(), code);
		}
	}

	return(HowCompatible);
}
예제 #3
0
BOOL PluginOILFilter::GetCapabilities(CCLexFile* pFile, PathName* pPath, CapabilityTree* pCapTree)
{
	// Here we need to run the plugin synchronously with the following options
	// -p -f <filename> -x <xmlfilename>

	// The xmlfilename is a path to a user and filter specific file
	// e.g. ~/.XaraLX/filtername.xml

	// The XML is returned via the file

	// Does this need double quotes to cope with spaces in filenames?
	wxString sCommand(m_PrepareExport);
	sCommand.Replace(_T("%OUT%"), (LPCTSTR)pPath->GetPath());
	sCommand.Replace(_T("%XML%"), m_XMLFile.GetFullPath());

	TRACEUSER("Gerry", _T("Running '%s'"), sCommand.c_str());

	wxArrayString saOutput;
	wxArrayString saErrors;
	INT32 code = wxExecute(sCommand, saOutput, saErrors, wxEXEC_NODISABLE);

#ifdef _DEBUG
	for (UINT32 i = 0; i < saErrors.GetCount(); i++)
	{
		TRACEUSER("Gerry", _T("stderr: %s"), saErrors[i].c_str());
	}
#endif

	if (code == 0)
	{
		BOOL bOK = BuildCapabilityTree(m_XMLFile.GetFullPath(), pCapTree);
		if (!bOK)
		{
			// Assume BuildCapabilityTree has already set an error
			return FALSE;
		}
	}
	else
	{
		TRACEUSER("Gerry", _T("Command '%s' exited with code %d"), sCommand.c_str(), code);

		// Get error message from saErrors

		// Look for the first entry that starts "ERROR:" and set the remainder of 
		// the line as the error message
		wxString line;
		size_t index = 0;
		while (index < saErrors.GetCount())
		{
			wxString rest;
			if (saErrors[index].StartsWith(_T("ERROR:"), &rest))
			{
				Error::SetError(0, rest.c_str(), 0);
				break;
			}
			index++;
		}

		if (index == saErrors.GetCount())
		{
			ERROR1(FALSE, _R(IDS_XPF_NO_ERROR_SET));
		}

		return(FALSE);
	}

	return(TRUE);
}
예제 #4
0
BOOL PluginOILFilter::GetImportFile(CCLexFile* pFile, CCLexFile** ppNewFile)
{
	ERROR2IF(pFile == NULL, FALSE,"PluginOILFilter::GetImportFile no file to import from");
	ERROR2IF(ppNewFile == NULL, FALSE,"PluginOILFilter::GetImportFile no newfile pointer");

	*ppNewFile = NULL;

	// Here we should really run the plugin asynchronously with the following options
	// -i -g -f <filename>

	// Redirect stdout to a CCLexFile
	// Check stderr during the Xar import and abort if an error is reported

	m_TempXarFile.SetPathName(_T("/tmp/xpftemp.xar"));

	CCDiskFile TempFile;
	if (!TempFile.open(m_TempXarFile, ios::out | ios::trunc | ios::binary))
	{
		// report an error here
		return FALSE;
	}

	PathName FileName = pFile->GetPathName();

	wxString sCommand(m_DoImport);
	sCommand.Replace(_T("%IN%"), (LPCTSTR)FileName.GetPath());

	TRACEUSER("Gerry", _T("Running '%s'"), sCommand.c_str());

	// Create a process with the TempFile as the stdout
	PluginFilterProcess* pTheProc = new PluginFilterProcess((PluginNativeFilter*)Parent, NULL, &TempFile);

	INT32 code = pTheProc->Execute(sCommand);
	TempFile.close();
	if (code != 0)
	{
		TRACEUSER("Gerry", _T("Execution of '%s' failed."), sCommand.c_str());
		// Extract error from a derived CamProcess class and report it
		pTheProc->ReportError();
		delete pTheProc;
		return(FALSE);
	}

	pTheProc->ReportWarning();
	delete pTheProc;
	pTheProc = NULL;

	CCDiskFile* pTempFile = new CCDiskFile();
	if (pTempFile)
	{
		if (pTempFile->open(m_TempXarFile, ios::in | ios::binary))
		{
			*ppNewFile = pTempFile;
			return(TRUE);
		}

		delete pTempFile;
	}

	return(FALSE);
}
예제 #5
0
int main(int argc, char *argv[])
{
	bool isBTSDo = false;	// If set, execute one command without prompting, then exit.
	std::string sCommand("");
	progname = argv[0];
	argc--; argv++;			// Skip program name.
	while(argc > 0) {
		if (argv[0][0] == '-') {
			if (strlen(argv[0]) > 2) {
				oops("Invalid option '%s'\n", argv[0]);
				exit(1);
			}
			switch(argv[0][1]) {
				case 'd': // OpenBTSDo interface
					isBTSDo = true;
					break;
				case 'c': // Run command on command line then exit.
					isBTSDo = true;
					if (argc == 1) {
						oops("Missing argument to -c\n");
					}
					{
						// Gather up the command line.
						for (int j = 1; j < argc; j++) {
							sCommand += argv[j];
							sCommand += " ";
						}
					}
					argc = 1;	// terminates while loop.
					break;
				case 'p': // TCP Port number
					argc--, argv++;
					port = atoi(argv[0]);
					printf("TCP %d\n", port);
					break;
				case 't': // target
					argc--, argv++;
					snprintf(target, sizeof(target)-1, "%s", argv[0]);
					break;
				default:
					oops("Invalid option '%s'\n", argv[0]);
					exit(1);	// NOTREACHED but makes the compiler happy.
			}
			argc--;
			argv++;
		} else {
			oops("Invalid argument '%s'\n", argv[0]);
			exit(1);	// NOTREACHED but makes the compiler happy.
		}
	}

    // Note that this only works if we are communicating across localhost.
    // TODO: Fix this so we can push the file across the socket if done
    // remotely.
    // Don't do this if running the single line configuration methods, we
    // will assume running from an external script is mostly a testing
    // thing, not a deployment thing for an actual base station.

	if (sCommand.c_str()[0] == '\0') {
		banner();
		printf("Connecting to %s:%d...\n", target, port);
	}

	int sock = -1;
	char prompt[16] = "OpenBTS> ";

	// Define this stuff "globally" as it's needed in various places
	memset(&sa, 0, sizeof(sa));

	// the socket
	sock = socket(AF_INET,SOCK_STREAM,0);
	if (sock<0) {
		perror("opening stream socket");
		exit(1);
	}

	// destination address
	sa.sin_family = AF_INET;
	sa.sin_port = htons(port);
	if (inet_pton(AF_INET, target, &sa.sin_addr) <= 0) {
		oops("unable to convert target to an IP address\n");
	}

	if (0) {
		// (pat) I used this code for testing.
		// If you wanted to specify the port you were binding from, this is how you would do it...
		// We dont use this code - we let the connect system call pick the port.
		struct sockaddr_in sockAddrBuf;
		memset(&sockAddrBuf,0,sizeof(sockAddrBuf));		// overkill.
		sockAddrBuf.sin_family = AF_INET;
		sockAddrBuf.sin_addr.s_addr = INADDR_ANY;
		sockAddrBuf.sin_port = htons(13011);
		if (bind(sock, (struct sockaddr *) &sockAddrBuf, sizeof(struct sockaddr_in))) {	// Bind the socket to our assigned port.
			printf("bind call failed: %s",strerror(errno));
			exit(2);
		}
	}

	if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
		perror("connect stream socket");
		fprintf(stderr, "Is OpenBTS running?\n");
		exit(1);
	}

#ifdef HAVE_LIBREADLINE
	char *history_name = 0;
	if (!isBTSDo)
	{
	    // start console
	    using_history();

	    static const char * const history_file_name = "/.openbts_history";
	    char *home_dir = getenv("HOME");

	    if(home_dir) {
		    size_t home_dir_len = strlen(home_dir);
		    size_t history_file_len = strlen(history_file_name);
		    size_t history_len = home_dir_len + history_file_len + 1;
		    if(history_len > home_dir_len) {
			    if(!(history_name = (char *)malloc(history_len))) {
				    perror("malloc failed");
				    exit(2);
			    }
			    memcpy(history_name, home_dir, home_dir_len);
			    memcpy(history_name + home_dir_len, history_file_name,
			       history_file_len + 1);
			    read_history(history_name);
		    }
	    }
	}
#endif


	if (!isBTSDo)
	    printf("Remote Interface Ready.\nType:\n \"help\" to see commands,\n \"version\" for version information,\n \"notices\" for licensing information,\n \"quit\" to exit console interface.\n");


        if (sCommand.c_str()[0] != '\0') {
                doCmd(sock, (char *)sCommand.c_str());
        } else
            while (1)
            {
#ifdef HAVE_LIBREADLINE
                char *cmd = readline(isBTSDo ? NULL : prompt);
                if (!cmd) continue;
                        if (cmd[0] == '\0') continue;
                if (!isBTSDo)
                    if (*cmd) add_history(cmd);
#else // HAVE_LIBREADLINE
                if (!isBTSDo)
                {
                    printf("%s",prompt);
                    fflush(stdout);
                }
                char *inbuf = (char*)malloc(BUFSIZ);
                char *cmd = fgets(inbuf,BUFSIZ-1,stdin);
                if (!cmd)
                {
                    if (isBTSDo)
                        break;
                    continue;
                }
                        if (cmd[0] == '\0') continue;
                // strip trailing CR
                cmd[strlen(cmd)-1] = '\0';
#endif
                if (!isBTSDo)
                {
                    // local quit?
                    if (strcmp(cmd,"quit")==0) {
                        printf("closing remote console\n");
                        break;
                    }
                            // shutdown via upstart
                    if (strcmp(cmd,"shutdown")==0) {
                        printf("terminating openbts\n");
                        if (getuid() == 0)
                            system("stop openbts");
                        else
                        {
                            printf("If prompted, enter the password you use for sudo\n");
                            system("sudo stop openbts");
                        }
                        break;
                    }
                    // shell escape?
                    if (cmd[0]=='!') {
                        int i = system(cmd+1);
                        if (i < 0)
                        {
                            perror("system");
                        }
                        continue;
                    }
                }
                char *pCmd = cmd;
                while(isspace(*pCmd)) pCmd++; // skip leading whitespace
                if (*pCmd)
                {
                    if (doCmd(sock, cmd) == false)
                    {
                        bool sd = false;
                        if (strcmp(cmd,"shutdown")==0)
                            sd = true;
                        else if (strcmp(cmd,"restart")==0)
                            sd = true;
                        free(cmd);
                        //{
                            if (isBTSDo)
                                break;
                            if (sd)
                                break;
                            continue;
                        //}
                    }
                }
                free(cmd);
                if (isBTSDo)
                    break;
            }

#ifdef HAVE_LIBREADLINE
	if (!isBTSDo)
	{
	    if(history_name)
	    {
		    int e = write_history(history_name);
		    if(e) {
			    fprintf(stderr, "error: history: %s\n", strerror(e));
		    }
		    free(history_name);
		    history_name = 0;
	    }
	}
#endif


	close(sock);
}
예제 #6
0
bool ConsoleAdapter::consoleInputBox_KeyUp(const CEGUI::EventArgs& args)
{
    const CEGUI::KeyEventArgs& keyargs = static_cast<const CEGUI::KeyEventArgs&>(args);

    if(keyargs.scancode != CEGUI::Key::Tab)
    {
        mTabPressed = false;
    }
    switch(keyargs.scancode)
    {
    case CEGUI::Key::ArrowUp:
    {
        if(mBackend->getHistory().getHistoryPosition() == 0)
        {
            mCommandLine = mInputBox->getText().c_str();
        }
        else
        {
            // we are not at the command line but in the history
            // => write back the editing
            mBackend->getHistory().changeHistory(mBackend->getHistory().getHistoryPosition(), mInputBox->getText().c_str());
        }
        mBackend->getHistory().moveBackwards();
        if(mBackend->getHistory().getHistoryPosition() != 0)
        {
            mInputBox->setText(mBackend->getHistory().getHistoryString());
        }

        return true;
    }
    case CEGUI::Key::ArrowDown:
    {
        if(mBackend->getHistory().getHistoryPosition() > 0)
        {
            mBackend->getHistory().changeHistory(mBackend->getHistory().getHistoryPosition(), mInputBox->getText().c_str());
            mBackend->getHistory().moveForwards();
            if(mBackend->getHistory().getHistoryPosition() == 0)
            {
                mInputBox->setText(mCommandLine);
            }
            else
            {
                mInputBox->setText(mBackend->getHistory().getHistoryString());
            }
        }

        return true;
    }
    case CEGUI::Key::Tab:
    {
        std::string sCommand(mInputBox->getText().c_str());

        // only process commands
        if(sCommand[0] != '/')
        {
            return true;
        }
        sCommand = sCommand.substr(1, mInputBox->getCaretIndex() - 1);
        if(mTabPressed == true)
        {
            const std::set< std::string > commands(mBackend->getPrefixes(sCommand));

            if(commands.size() > 0)
            {
                std::set< std::string >::const_iterator iCommand(commands.begin());
                std::string sMessage("");

                mSelected = (mSelected + 1) % commands.size();

                int select(0);

                while(iCommand != commands.end())
                {
                    if(select == mSelected)
                    {
                        std::string sCommandLine(mInputBox->getText().c_str());

                        // compose the new command line: old text before the caret + selected command
                        mInputBox->setText(sCommandLine.substr(0, mInputBox->getCaretIndex()) + iCommand->substr(mInputBox->getCaretIndex() - 1));
                        mInputBox->setSelection(mInputBox->getCaretIndex(), 0xFFFFFFFF);
                    }
                    sMessage += *iCommand + ' ';
                    ++iCommand;
                    ++select;
                }
                mBackend->pushMessage(sMessage);
            }
        }
        else
        {
            mTabPressed = true;
            mSelected = 0;

            const std::set< std::string > commands(mBackend->getPrefixes(sCommand));

            if(commands.size() == 0)
            {
                // TODO: Error reporting?
            }
            else
            {
                // if any command starts with the current prefix
                if(commands.size() == 1)
                {
                    mInputBox->setText(std::string("/") + *(commands.begin()) + ' ');
                    // this will be at the end of the text
                    mInputBox->setCaretIndex(0xFFFFFFFF);
                }
                else
                {
                    //If there are multiple matches we need to find the lowest common denominator. We'll do this by iterating through all characters and then checking with all the possible commands if they match that prefix, until we get a false.
                    std::set< std::string >::const_iterator iSelected(commands.begin());
                    std::set< std::string >::const_iterator iCommand(commands.begin());
                    std::string sCommonPrefix(*iCommand);
                    int select = 1;

                    ++iCommand;
                    while(iCommand != commands.end())
                    {
                        if(select == mSelected)
                        {
                            iSelected = iCommand;
                        }

                        std::string::size_type i(0);

                        while((i < sCommonPrefix.length()) && (i < (*iCommand).length()))
                        {
                            if(sCommonPrefix[i] != (*iCommand)[i])
                            {
                                break;
                            }
                            ++i;
                        }
                        if(i < sCommonPrefix.length())
                        {
                            sCommonPrefix = sCommonPrefix.substr(0, i);
                        }
                        ++select;
                        ++iCommand;
                    }
                    mInputBox->setText(std::string("/") + sCommonPrefix + iSelected->substr(sCommonPrefix.length()));
                    mInputBox->setCaretIndex(sCommonPrefix.length() + 1);
                    mInputBox->setSelection(sCommonPrefix.length() + 1, 0xFFFFFFFF);
                }
            }
        }

        return true;
    }
    case CEGUI::Key::Return:
    case CEGUI::Key::NumpadEnter:
    {
        if (mReturnKeyDown) {
            mReturnKeyDown = false;
            if(mInputBox->getSelectionLength() > 0)
            {
                unsigned long ulSelectionEnd(mInputBox->getSelectionEndIndex());

                mInputBox->setText(mInputBox->getText() + ' ');
                mInputBox->setCaretIndex(ulSelectionEnd + 1);
                mInputBox->setSelection(mInputBox->getCaretIndex(), mInputBox->getCaretIndex());
            }
            else
            {
                const CEGUI::String consoleText(mInputBox->getText());

                mInputBox->setText(CEGUI::String(""));
                mBackend->pushMessage(("> " + consoleText).c_str());
                // run the command
                mBackend->runCommand(consoleText.c_str());
                EventCommandExecuted.emit(consoleText.c_str());
            }
        }

        return true;
    }
    default:
    {
        break;
    }
    }

    return false;
}