|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2023 Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "addoninfo.h" |
| 20 | + |
| 21 | +#include "path.h" |
| 22 | +#include "utils.h" |
| 23 | + |
| 24 | +#include <fstream> |
| 25 | +#include <sstream> |
| 26 | +#include <string> |
| 27 | + |
| 28 | +#include "json.h" |
| 29 | + |
| 30 | +static std::string getFullPath(const std::string &fileName, const std::string &exename) { |
| 31 | + if (Path::isFile(fileName)) |
| 32 | + return fileName; |
| 33 | + |
| 34 | + const std::string exepath = Path::getPathFromFilename(exename); |
| 35 | + if (Path::isFile(exepath + fileName)) |
| 36 | + return exepath + fileName; |
| 37 | + if (Path::isFile(exepath + "addons/" + fileName)) |
| 38 | + return exepath + "addons/" + fileName; |
| 39 | + |
| 40 | +#ifdef FILESDIR |
| 41 | + if (Path::isFile(FILESDIR + ("/" + fileName))) |
| 42 | + return FILESDIR + ("/" + fileName); |
| 43 | + if (Path::isFile(FILESDIR + ("/addons/" + fileName))) |
| 44 | + return FILESDIR + ("/addons/" + fileName); |
| 45 | +#endif |
| 46 | + return ""; |
| 47 | +} |
| 48 | + |
| 49 | +static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &json, const std::string &fileName, const std::string &exename) { |
| 50 | + const std::string& json_error = picojson::get_last_error(); |
| 51 | + if (!json_error.empty()) { |
| 52 | + return "Loading " + fileName + " failed. " + json_error; |
| 53 | + } |
| 54 | + if (!json.is<picojson::object>()) |
| 55 | + return "Loading " + fileName + " failed. Bad json."; |
| 56 | + picojson::object obj = json.get<picojson::object>(); |
| 57 | + if (obj.count("args")) { |
| 58 | + if (!obj["args"].is<picojson::array>()) |
| 59 | + return "Loading " + fileName + " failed. args must be array."; |
| 60 | + for (const picojson::value &v : obj["args"].get<picojson::array>()) |
| 61 | + addoninfo.args += " " + v.get<std::string>(); |
| 62 | + } |
| 63 | + |
| 64 | + if (obj.count("ctu")) { |
| 65 | + // ctu is specified in the config file |
| 66 | + if (!obj["ctu"].is<bool>()) |
| 67 | + return "Loading " + fileName + " failed. ctu must be boolean."; |
| 68 | + addoninfo.ctu = obj["ctu"].get<bool>(); |
| 69 | + } else { |
| 70 | + addoninfo.ctu = false; |
| 71 | + } |
| 72 | + |
| 73 | + if (obj.count("python")) { |
| 74 | + // Python was defined in the config file |
| 75 | + if (obj["python"].is<picojson::array>()) { |
| 76 | + return "Loading " + fileName +" failed. python must not be an array."; |
| 77 | + } |
| 78 | + addoninfo.python = obj["python"].get<std::string>(); |
| 79 | + } else { |
| 80 | + addoninfo.python = ""; |
| 81 | + } |
| 82 | + |
| 83 | + if (obj.count("executable")) { |
| 84 | + if (!obj["executable"].is<std::string>()) |
| 85 | + return "Loading " + fileName + " failed. executable must be a string."; |
| 86 | + addoninfo.executable = getFullPath(obj["executable"].get<std::string>(), fileName); |
| 87 | + return ""; |
| 88 | + } |
| 89 | + |
| 90 | + return addoninfo.getAddonInfo(obj["script"].get<std::string>(), exename); |
| 91 | +} |
| 92 | + |
| 93 | +std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::string &exename) { |
| 94 | + if (fileName[0] == '{') { |
| 95 | + picojson::value json; |
| 96 | + const std::string err = picojson::parse(json, fileName); |
| 97 | + (void)err; // TODO: report |
| 98 | + return parseAddonInfo(*this, json, fileName, exename); |
| 99 | + } |
| 100 | + if (fileName.find('.') == std::string::npos) |
| 101 | + return getAddonInfo(fileName + ".py", exename); |
| 102 | + |
| 103 | + if (endsWith(fileName, ".py")) { |
| 104 | + scriptFile = Path::fromNativeSeparators(getFullPath(fileName, exename)); |
| 105 | + if (scriptFile.empty()) |
| 106 | + return "Did not find addon " + fileName; |
| 107 | + |
| 108 | + std::string::size_type pos1 = scriptFile.rfind('/'); |
| 109 | + if (pos1 == std::string::npos) |
| 110 | + pos1 = 0; |
| 111 | + else |
| 112 | + pos1++; |
| 113 | + std::string::size_type pos2 = scriptFile.rfind('.'); |
| 114 | + if (pos2 < pos1) |
| 115 | + pos2 = std::string::npos; |
| 116 | + name = scriptFile.substr(pos1, pos2 - pos1); |
| 117 | + |
| 118 | + runScript = getFullPath("runaddon.py", exename); |
| 119 | + |
| 120 | + return ""; |
| 121 | + } |
| 122 | + |
| 123 | + if (!endsWith(fileName, ".json")) |
| 124 | + return "Failed to open addon " + fileName; |
| 125 | + |
| 126 | + std::ifstream fin(fileName); |
| 127 | + if (!fin.is_open()) |
| 128 | + return "Failed to open " + fileName; |
| 129 | + picojson::value json; |
| 130 | + fin >> json; |
| 131 | + return parseAddonInfo(*this, json, fileName, exename); |
| 132 | +} |
0 commit comments