diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/base64.cpp | 3 | ||||
-rw-r--r-- | src/utils/copynpaste.cpp | 15 | ||||
-rw-r--r-- | src/utils/dtor.h | 2 | ||||
-rw-r--r-- | src/utils/gettext.h | 2 | ||||
-rw-r--r-- | src/utils/mathutils.h | 2 | ||||
-rw-r--r-- | src/utils/mkdir.cpp | 16 | ||||
-rw-r--r-- | src/utils/mkdir.h | 2 | ||||
-rw-r--r-- | src/utils/mutex.h | 2 | ||||
-rw-r--r-- | src/utils/paths.cpp | 4 | ||||
-rw-r--r-- | src/utils/paths.h | 2 | ||||
-rw-r--r-- | src/utils/process.cpp | 172 | ||||
-rw-r--r-- | src/utils/process.h | 29 | ||||
-rw-r--r-- | src/utils/sha256.cpp | 2 | ||||
-rw-r--r-- | src/utils/sha256.h | 2 | ||||
-rw-r--r-- | src/utils/specialfolder.cpp | 2 | ||||
-rw-r--r-- | src/utils/specialfolder.h | 2 | ||||
-rw-r--r-- | src/utils/stringutils.cpp | 12 | ||||
-rw-r--r-- | src/utils/stringutils.h | 4 | ||||
-rw-r--r-- | src/utils/xml.cpp | 38 | ||||
-rw-r--r-- | src/utils/xml.h | 34 |
20 files changed, 306 insertions, 41 deletions
diff --git a/src/utils/base64.cpp b/src/utils/base64.cpp index 843a0b534..24a3a58e3 100644 --- a/src/utils/base64.cpp +++ b/src/utils/base64.cpp @@ -108,7 +108,8 @@ unsigned char *php3_base64_decode(const unsigned char *string, /* run through the whole string, converting as we go */ while ((ch = *current++) != '\0') { - if (ch == base64_pad) break; + if (ch == base64_pad) + break; /* When Base64 gets POSTed, all pluses are interpreted as spaces. This line changes them back. It's not exactly the Base64 spec, diff --git a/src/utils/copynpaste.cpp b/src/utils/copynpaste.cpp index 3e37afd9d..3e9e09a5c 100644 --- a/src/utils/copynpaste.cpp +++ b/src/utils/copynpaste.cpp @@ -407,8 +407,17 @@ bool retrieveBuffer(std::string& text, std::string::size_type& pos) return false; } +bool runxsel(std::string& text, const char *p1, const char *p2 = nullptr); + bool sendBuffer(std::string& text) { + runxsel(text, "-i"); + runxsel(text, "-b", "-i"); + return true; +} + +bool runxsel(std::string& text, const char *p1, const char *p2) +{ pid_t pid; int fd[2]; @@ -432,7 +441,11 @@ bool sendBuffer(std::string& text) } close(fd[0]); } - execl("/usr/bin/xsel", "xsel", "-i", (char *)nullptr); + if (p2) + execl("/usr/bin/xsel", "xsel", p1, p2, (char *)nullptr); + else + execl("/usr/bin/xsel", "xsel", p1, (char *)nullptr); + exit(1); } diff --git a/src/utils/dtor.h b/src/utils/dtor.h index fbe903ced..54fa11549 100644 --- a/src/utils/dtor.h +++ b/src/utils/dtor.h @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2004-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/gettext.h b/src/utils/gettext.h index 696f0d4bf..8bb41a4cb 100644 --- a/src/utils/gettext.h +++ b/src/utils/gettext.h @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2007-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/mathutils.h b/src/utils/mathutils.h index 40a9e5d79..9f6818146 100644 --- a/src/utils/mathutils.h +++ b/src/utils/mathutils.h @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/mkdir.cpp b/src/utils/mkdir.cpp index 84c205b33..582c9c7dc 100644 --- a/src/utils/mkdir.cpp +++ b/src/utils/mkdir.cpp @@ -1,7 +1,7 @@ /* * The ManaPlus Client * Copyright (C) 2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * @@ -43,6 +43,7 @@ int mkdir_r(const char *pathname) { char tmp[PATH_MAX]; + char tmp2[PATH_MAX]; char *p; if (strlen(pathname) >= PATH_MAX - 2) @@ -72,9 +73,13 @@ int mkdir_r(const char *pathname) continue; } + strcpy(tmp2, tmp); + char *p2 = tmp2 + strlen(tmp2) - 1; + if (*p2 == '/' || *p2 == '\\') + *p2 = 0; // check if the name already exists, but not as directory struct stat statbuf; - if (!stat(tmp, &statbuf)) + if (!stat(tmp2, &statbuf)) { if (S_ISDIR(statbuf.st_mode)) { @@ -85,13 +90,12 @@ int mkdir_r(const char *pathname) return -1; } - if (!CreateDirectory(tmp, 0)) + if (!CreateDirectory(tmp2, 0)) { // hack, hack. just assume that x: might be a drive // letter, and try again - if (!(strlen(tmp) == 2 && - !strcmp(tmp + 1, ":"))) - return -1; + if (!(strlen(tmp2) == 2 && !strcmp(tmp2 + 1, ":"))) + return -1; } #ifdef M_MKDIR_TEST_ diff --git a/src/utils/mkdir.h b/src/utils/mkdir.h index deff75475..8c5ab4ca2 100644 --- a/src/utils/mkdir.h +++ b/src/utils/mkdir.h @@ -1,7 +1,7 @@ /* * The ManaPlus Client * Copyright (C) 2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/mutex.h b/src/utils/mutex.h index 03dac2e7a..a97a8d3f8 100644 --- a/src/utils/mutex.h +++ b/src/utils/mutex.h @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2008-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/paths.cpp b/src/utils/paths.cpp index 3cc35cb55..8decb6e95 100644 --- a/src/utils/paths.cpp +++ b/src/utils/paths.cpp @@ -1,6 +1,6 @@ /* * The ManaPlus Client - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * @@ -43,6 +43,8 @@ std::string getRealPath(const std::string &str) #else char *realPath = realpath(str.c_str(), nullptr); #endif + if (!realPath) + return ""; path = realPath; free(realPath); return path; diff --git a/src/utils/paths.h b/src/utils/paths.h index 1723bf6c4..804900587 100644 --- a/src/utils/paths.h +++ b/src/utils/paths.h @@ -1,6 +1,6 @@ /* * The ManaPlus Client - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/process.cpp b/src/utils/process.cpp new file mode 100644 index 000000000..4a2081514 --- /dev/null +++ b/src/utils/process.cpp @@ -0,0 +1,172 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2012 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "utils/process.h" + +#include <string.h> +#include <cstdarg> +#include <cstdio> + +#include <stdlib.h> +#include <unistd.h> + +#include "localconsts.h" + +#include "debug.h" + +const int timeOut = 10; + +#ifdef WIN32 + +#include <windows.h> + +int execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime) +{ + if (!waitTime) + waitTime = timeOut; + + STARTUPINFO siStartupInfo; + PROCESS_INFORMATION piProcessInfo; + memset(&siStartupInfo, 0, sizeof(siStartupInfo)); + memset(&piProcessInfo, 0, sizeof(piProcessInfo)); + siStartupInfo.cb = sizeof(siStartupInfo); + DWORD ret = -1; + std::string args(pathName + " " + arg1); + if (!arg2.empty()) + args += " " + arg2; + + if (CreateProcess(pathName.c_str(), (char*)args.c_str(), 0, 0, false, + CREATE_DEFAULT_ERROR_MODE, 0, 0, &siStartupInfo, + &piProcessInfo) != false) + { + if (!WaitForSingleObject(piProcessInfo.hProcess, timeOut * 1000)) + { + if (GetExitCodeProcess(piProcessInfo.hProcess, &ret)) + { + CloseHandle(piProcessInfo.hProcess); + CloseHandle(piProcessInfo.hThread); + return ret; + } + } + TerminateProcess(piProcessInfo.hProcess, -1); + } + + CloseHandle(piProcessInfo.hProcess); + CloseHandle(piProcessInfo.hThread); + return -1; +} + +#elif defined(__APPLE__) + +int execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime) +{ + return -1; +} + +#elif defined __linux__ || defined __linux + +#include <sys/types.h> +#include <sys/wait.h> + +int execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime) +{ + pid_t mon_pid; + int status; + + if (!waitTime) + waitTime = timeOut; + + if ((mon_pid = fork()) == -1) + { // fork error + return -1; + } + else if (!mon_pid) + { // monitoring child + pid_t pid; + if ((pid = fork()) == -1) + { // fork error + return -1; + } + else if (!pid) + { // work child + if (arg2.empty()) + { + execl(pathName.c_str(), name.c_str(), + arg1.c_str(), (char *)nullptr); + } + else + { + execl(pathName.c_str(), name.c_str(), + arg1.c_str(), arg2.c_str(), (char *)nullptr); + } + exit(-1); + } + + // monitoring process + pid_t sleep_pid; + if ((sleep_pid = fork()) == -1) + { // fork error + return -1; + } + else if (!sleep_pid) + { // sleep pid + sleep (timeOut); +// printf ("time out\n"); + exit(-1); + } + + // monitoring process + pid_t exited_pid = wait(&status); + int ret = -1; + if (exited_pid == pid) + { + kill(sleep_pid, SIGKILL); + if (WIFEXITED(status)) + ret = WEXITSTATUS(status); + } + else + { + kill(pid, SIGKILL); + ret = -1; + } + wait(nullptr); + exit(ret); + } + + // monitoring parent + waitpid(mon_pid, &status, 0); + if (WIFEXITED(status)) + return WEXITSTATUS(status); + + return -1; +} + +#else + +int execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime) +{ + return -1; +} + +#endif diff --git a/src/utils/process.h b/src/utils/process.h new file mode 100644 index 000000000..12421729f --- /dev/null +++ b/src/utils/process.h @@ -0,0 +1,29 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2012 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef UTILS_PROCESS_H +#define UTILS_PROCESS_H + +#include <string> + +int execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime = 0); + +#endif // UTILS_PROCESS_H diff --git a/src/utils/sha256.cpp b/src/utils/sha256.cpp index 523c021a4..ac9c78b2a 100644 --- a/src/utils/sha256.cpp +++ b/src/utils/sha256.cpp @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2008-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file has been slighly modified as part of The ManaPlus Client. * diff --git a/src/utils/sha256.h b/src/utils/sha256.h index aa2468d56..54bb92f3c 100644 --- a/src/utils/sha256.h +++ b/src/utils/sha256.h @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2007-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/specialfolder.cpp b/src/utils/specialfolder.cpp index fae0fbf47..9690a9fb7 100644 --- a/src/utils/specialfolder.cpp +++ b/src/utils/specialfolder.cpp @@ -1,7 +1,7 @@ /* * The ManaPlus Client * Copyright (C) 2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/specialfolder.h b/src/utils/specialfolder.h index 571817370..8eb00a637 100644 --- a/src/utils/specialfolder.h +++ b/src/utils/specialfolder.h @@ -1,7 +1,7 @@ /* * The ManaPlus Client * Copyright (C) 2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 39f14a646..b855e3b04 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2007-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * @@ -612,3 +612,13 @@ std::string &removeProtocol(std::string &url) url = url.substr(i + 3); return url; } + +bool checkPath(std::string path) +{ + if (path.empty()) + return true; + return path.find("../") == std::string::npos + && path.find("..\\") == std::string::npos + && path.find("/..") == std::string::npos + && path.find("\\..") == std::string::npos; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 273fa0c8c..c6eb08a6c 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2007-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * @@ -206,4 +206,6 @@ bool findCutFirst(std::string &str1, std::string str2); std::string &removeProtocol(std::string &url); +bool checkPath(std::string path); + #endif // UTILS_STRINGUTILS_H diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index 10436067f..420915d8f 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2004-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * @@ -32,6 +32,11 @@ #include "debug.h" +static void xmlNullLogger(void *ctx A_UNUSED, const char *msg A_UNUSED, ...) +{ + // Does nothing, that's the whole point of it +} + namespace XML { Document::Document(const std::string &filename, bool useResman): @@ -96,12 +101,12 @@ namespace XML xmlFreeDoc(mDoc); } - xmlNodePtr Document::rootNode() + XmlNodePtr Document::rootNode() { return mDoc ? xmlDocGetRootElement(mDoc) : nullptr; } - int getProperty(xmlNodePtr node, const char* name, int def) + int getProperty(XmlNodePtr node, const char* name, int def) { int &ret = def; @@ -115,7 +120,7 @@ namespace XML return ret; } - double getFloatProperty(xmlNodePtr node, const char* name, double def) + double getFloatProperty(XmlNodePtr node, const char* name, double def) { double &ret = def; @@ -129,7 +134,7 @@ namespace XML return ret; } - std::string getProperty(xmlNodePtr node, const char *name, + std::string getProperty(XmlNodePtr node, const char *name, const std::string &def) { xmlChar *prop = xmlGetProp(node, BAD_CAST name); @@ -143,7 +148,7 @@ namespace XML return def; } - bool getBoolProperty(xmlNodePtr node, const char* name, bool def) + bool getBoolProperty(XmlNodePtr node, const char* name, bool def) { xmlChar *prop = xmlGetProp(node, BAD_CAST name); @@ -154,15 +159,32 @@ namespace XML return def; } - xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name) + XmlNodePtr findFirstChildByName(XmlNodePtr parent, const char *name) { for_each_xml_child_node(child, parent) { - if (xmlStrEqual(child->name, BAD_CAST name)) + if (xmlNameEqual(child, name)) return child; } return nullptr; } + // Initialize libxml2 and check for potential ABI mismatches between + // compiled version and the shared library actually used. + void initXML() + { + xmlInitParser(); + LIBXML_TEST_VERSION; + + // Suppress libxml2 error messages + xmlSetGenericErrorFunc(nullptr, xmlNullLogger); + } + + // Shutdown libxml + void cleanupXML() + { + xmlCleanupParser(); + } + } // namespace XML diff --git a/src/utils/xml.h b/src/utils/xml.h index 7bcec2c83..f623fa618 100644 --- a/src/utils/xml.h +++ b/src/utils/xml.h @@ -2,7 +2,7 @@ * The ManaPlus Client * Copyright (C) 2004-2009 The Mana World Development Team * Copyright (C) 2009-2010 The Mana Developers - * Copyright (C) 2011 The ManaPlus Developers + * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. * @@ -23,10 +23,16 @@ #ifndef XML_H #define XML_H +#include <libxml/encoding.h> +#include <libxml/xmlwriter.h> #include <libxml/tree.h> #include <string> +#define XmlNodePtr xmlNodePtr +#define xmlNameEqual(node, str) xmlStrEqual((node)->name, BAD_CAST (str)) +#define XmlTextWriterPtr xmlTextWriterPtr + /** * XML helper functions. */ @@ -63,40 +69,44 @@ namespace XML * Returns the root node of the document (or NULL if there was a * load error). */ - xmlNodePtr rootNode(); + XmlNodePtr rootNode(); private: xmlDocPtr mDoc; }; /** - * Gets an floating point property from an xmlNodePtr. + * Gets an floating point property from an XmlNodePtr. */ - double getFloatProperty(xmlNodePtr node, const char *name, double def); + double getFloatProperty(XmlNodePtr node, const char *name, double def); /** - * Gets an integer property from an xmlNodePtr. + * Gets an integer property from an XmlNodePtr. */ - int getProperty(xmlNodePtr node, const char *name, int def); + int getProperty(XmlNodePtr node, const char *name, int def); /** - * Gets a string property from an xmlNodePtr. + * Gets a string property from an XmlNodePtr. */ - std::string getProperty(xmlNodePtr node, const char *name, + std::string getProperty(XmlNodePtr node, const char *name, const std::string &def); /** - * Gets a boolean property from an xmlNodePtr. + * Gets a boolean property from an XmlNodePtr. */ - bool getBoolProperty(xmlNodePtr node, const char *name, bool def); + bool getBoolProperty(XmlNodePtr node, const char *name, bool def); /** * Finds the first child node with the given name */ - xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name); + XmlNodePtr findFirstChildByName(XmlNodePtr parent, const char *name); + + void initXML(); + + void cleanupXML(); } #define for_each_xml_child_node(var, parent) \ - for (xmlNodePtr var = parent->xmlChildrenNode; var; var = var->next) + for (XmlNodePtr var = parent->xmlChildrenNode; var; var = var->next) #endif // XML_H |