genode/repos/libports/src/test/python/main.cc

78 lines
1.6 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Test for using Python on Genode
* \author Sebastian Sumpf
* \date 2010-02-17
*/
/*
2013-01-10 21:44:47 +01:00
* Copyright (C) 2010-2013 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Python includes */
#include <python2.6/Python.h>
/* Genode includes */
#include <base/log.h>
2011-12-22 16:19:25 +01:00
#include <os/config.h>
/* libc includes */
#include <fcntl.h>
extern "C" int __sread(void *, char *, int);
static bool process_config(char **file)
{
using namespace Genode;
static char file_name[64];
try {
Xml_node config_node = config()->xml_node();
Xml_node script_node = config_node.sub_node("script");
script_node.attribute("name").value(file_name, sizeof(file_name));
*file = file_name;
return true;
}
catch (Xml_node::Nonexistent_sub_node) {
Genode::error("no 'config/script' sub node in config found"); }
2011-12-22 16:19:25 +01:00
catch (Xml_node::Nonexistent_attribute) {
Genode::error("no 'name' attribute in 'script' node found"); }
2011-12-22 16:19:25 +01:00
return false;
}
int main()
{
using namespace Genode;
FILE fp;
::memset(&fp, 0x0, sizeof(fp));
char *name;
if (!process_config(&name)) {
Genode::error("no script found");
2011-12-22 16:19:25 +01:00
return 1;
}
Genode::log("Found script: ", Genode::Cstring(name));
2011-12-22 16:19:25 +01:00
fp._file = open(name, 0, 0);
fp._read = __sread;
fp._cookie = &fp;
fp._flags = __SRD;
Py_SetProgramName(name);
//don't need the 'site' module
Py_NoSiteFlag = 1;
//don't support interactive mode, yet
Py_InteractiveFlag = 0;
Py_Initialize();
Genode::log("Starting python ...");
2011-12-22 16:19:25 +01:00
PyRun_SimpleFile(&fp, name);
return 0;
}