Index: /trunk/FACT++/src/Main.h
===================================================================
--- /trunk/FACT++/src/Main.h	(revision 11252)
+++ /trunk/FACT++/src/Main.h	(revision 11252)
@@ -0,0 +1,88 @@
+#ifndef FACT_MAIN
+#define FACT_MAIN
+
+void MainThread(StateMachineImp *io_service, bool dummy)
+{
+    // This is necessary so that the StateMachien Thread can signal the
+    // Readline to exit
+    io_service->Run(dummy);
+    Readline::Stop();
+}
+
+/*
+template<class S>
+int RunDim(Configuration &conf)
+{
+    WindowLog wout;
+
+    ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
+
+    //log.SetWindow(stdscr);
+    if (conf.Has("log"))
+        if (!wout.OpenLogFile(conf.Get<string>("log")))
+            wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
+
+    // Start io_service.Run to use the StateMachineImp::Run() loop
+    // Start io_service.run to only use the commandHandler command detaching
+    AutoScheduler<S> io_service(wout);
+    if (!io_service.SetConfiguration(conf))
+        return -1;
+
+    io_service.Run();
+
+    return 0;
+}
+*/
+
+template<class T, class S>
+int Main(Configuration &conf, bool dummy=false)
+{
+    static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
+
+    WindowLog &win  = shell.GetStreamIn();
+    WindowLog &wout = shell.GetStreamOut();
+
+    if (conf.Has("log"))
+        if (!wout.OpenLogFile(conf.Get<string>("log")))
+            win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
+
+    S io_service(wout);
+    if (!io_service.SetConfiguration(conf))
+        return -1;
+
+    shell.SetReceiver(io_service);
+
+//    boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
+    boost::thread t(boost::bind(MainThread, &io_service, dummy));
+
+    if (conf.Has("cmd"))
+    {
+        const vector<string> v = conf.Get<vector<string>>("cmd");
+        for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
+            shell.ProcessLine(*it);
+    }
+
+    if (conf.Has("exec"))
+    {
+        const vector<string> v = conf.Get<vector<string>>("exec");
+        for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
+            shell.Execute(*it);
+    }
+
+    if (conf.Get<bool>("quit"))
+        shell.Stop();
+
+    shell.Run();                 // Run the shell
+    io_service.Stop();           // Signal Loop-thread to stop
+    // io_service.Close();       // Obsolete, done by the destructor
+    // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
+
+    // Wait until the StateMachine has finished its thread
+    // before returning and destroying the dim objects which might
+    // still be in use.
+    t.join();
+
+    return 0;
+}
+
+#endif
