diff --git a/src/API.cpp b/src/API.cpp index 1547364ab..11279b369 100644 --- a/src/API.cpp +++ b/src/API.cpp @@ -23,6 +23,28 @@ // 02110-1301 // USA // +// ------------- +// +// Copyright © 1994–2008 Lua.org, PUC-Rio. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// //////////////////////////////////////////////////////////////////////////////// #include "API.h" @@ -389,7 +411,7 @@ void API::initialize () { // Initialize Lua. L = lua_open (); - luaL_openlibs (L); + luaL_openlibs (L); // TODO Error handling // Register all the API functions in Lua global space. lua_pushcfunction (L, api_task_version); lua_setglobal (L, "task_version"); diff --git a/src/Context.cpp b/src/Context.cpp index a4a9485ad..aaca35086 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -83,6 +83,11 @@ void Context::initialize (int argc, char** argv) } initialize (); + + // Hook system init, plus post-start event occurring at the first possible + // moment after hook initialization. + hooks.initialize (); + hooks.trigger ("post-start"); } //////////////////////////////////////////////////////////////////////////////// @@ -180,6 +185,7 @@ int Context::run () else std::cout << *f << std::endl; + hooks.trigger ("pre-exit"); return rc; } diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 46bd1ea69..1cf399747 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -25,6 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// +#include #include "Hooks.h" //////////////////////////////////////////////////////////////////////////////// @@ -38,3 +39,90 @@ Hooks::~Hooks () } //////////////////////////////////////////////////////////////////////////////// +void Hooks::initialize () +{ + api.initialize (); +} + +//////////////////////////////////////////////////////////////////////////////// +bool Hooks::trigger (const std::string& event) +{ + // TODO Look up scripts/functions hooking this event. + // TODO Load the scripts if necessary. + + // TODO Call each function. + std::string type; + if (eventType (event, type)) + { + if (type == "program") return triggerProgramEvent (event); + else if (type == "list") return triggerListEvent (event); + else if (type == "task") return triggerTaskEvent (event); + else if (type == "field") return triggerFieldEvent (event); + } + else + throw std::string ("Unrecognized hook event '") + event + "'"; + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Hooks::eventType (const std::string& event, std::string& type) +{ + if (event == "post-start" || + event == "pre-exit") + { + type = "program"; + return true; + } + else if (event == "?") + { + type = "list"; + return true; + } + else if (event == "?") + { + type = "task"; + return true; + } + else if (event == "?") + { + type = "field"; + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Hooks::triggerProgramEvent (const std::string& event) +{ + std::cout << "Hooks::triggerProgramEvent " << event << std::endl; + + // TODO Is this event hooked? + // TODO Is the associated script loaded? + // TODO Call the function + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Hooks::triggerListEvent (const std::string& event) +{ + std::cout << "Hooks::triggerListEvent " << event << std::endl; + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Hooks::triggerTaskEvent (const std::string& event) +{ + std::cout << "Hooks::triggerTaskEvent " << event << std::endl; + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Hooks::triggerFieldEvent (const std::string& event) +{ + std::cout << "Hooks::triggerFieldEvent " << event << std::endl; + return false; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Hooks.h b/src/Hooks.h index 08a26fe12..ad596637e 100644 --- a/src/Hooks.h +++ b/src/Hooks.h @@ -27,16 +27,31 @@ #ifndef INCLUDED_HOOKS #define INCLUDED_HOOKS +#include +#include +#include "API.h" + class Hooks { public: - Hooks (); // Default constructor - ~Hooks (); // Destructor + Hooks (); // Default constructor + ~Hooks (); // Destructor + Hooks (const Hooks&); // Deliberately unimplemented + Hooks& operator= (const Hooks&); // Deliberately unimplemented - Hooks (const Hooks&); - Hooks& operator= (const Hooks&); + void initialize (); + bool trigger (const std::string&); + bool eventType (const std::string&, std::string&); private: + bool triggerProgramEvent (const std::string&); + bool triggerListEvent (const std::string&); + bool triggerTaskEvent (const std::string&); + bool triggerFieldEvent (const std::string&); + +private: + API api; + std::vector scripts; }; #endif diff --git a/src/command.cpp b/src/command.cpp index cfadb7a36..2d7c2b733 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -490,8 +490,12 @@ int handleVersion (std::string &outs) #endif << std::endl - << "Copyright (C) 2006 - 2010, P. Beckingham, F. Hernandez." + << "Copyright (C) 2006 - 2010 P. Beckingham, F. Hernandez." << std::endl +#ifdef HAVE_LIBLUA + << "Portions of this software Copyright (C) 1994 – 2008 Lua.org, PUC-Rio." + << std::endl +#endif << disclaimer.render () << link.render () << std::endl;