diff --git a/mst.cxx b/mst.cxx index f9bebf3..60e946f 100644 --- a/mst.cxx +++ b/mst.cxx @@ -1,4 +1,4 @@ -// vim: sw=4 ts=4 expandtab +// vim: sw=4 ts=4 expandtab cc=120 // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -16,52 +16,41 @@ #include #include -int main(int argc, char *argv[]) { - try { - mstch::config::escape = [](const std::string& str) -> std::string { - return str; - }; - std::ifstream tpl_fstream{ argv[1] }; - std::string tpl{ std::istreambuf_iterator{ tpl_fstream }, - std::istreambuf_iterator{} }; - mstch::map ctx{ - {"include", mstch::lambda{ [](const std::string &filename) -> mstch::node { - std::ifstream file_fstream{ filename }; - if((file_fstream.rdstate() & std::ifstream::failbit) != 0) - throw std::runtime_error("Can't open file: " + filename); - return std::string { std::istreambuf_iterator{ file_fstream }, - std::istreambuf_iterator{} }; - }}}, - {"env", mstch::lambda{ [](const std::string &env_var_name) -> mstch::node { - if(char *env_var_value = std::getenv(env_var_name.c_str())) - return std::string{env_var_value}; - throw std::runtime_error("Env var doesn't exist: " + env_var_name); - }}}, - {"cmd", mstch::lambda{ [](const std::string &cmd) -> mstch::node { - // https://stackoverflow.com/a/478960/547223 - std::array buffer; // 16 KiB - buffer.fill('\0'); - std::string stdout; - std::unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); - if(!pipe) - throw std::runtime_error("Command failed: " + cmd); - while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) - stdout += buffer.data(); - return stdout; - }}} - }; +int main(int argc, char* argv[]) { + using namespace std; + try { + mstch::config::escape = [](const string& str) -> string { return str; }; + ifstream tpl_fstream{argv[1]}; + string tpl{istreambuf_iterator{tpl_fstream}, istreambuf_iterator{}}; + mstch::map ctx{{"include", mstch::lambda{[](const string& filename) -> mstch::node { + ifstream file_fstream{filename}; + if ((file_fstream.rdstate() & ifstream::failbit) != 0) + throw runtime_error("Can't open file: " + filename); + return string{istreambuf_iterator{file_fstream}, istreambuf_iterator{}}; + }}}, + {"env", mstch::lambda{[](const string& env_var_name) -> mstch::node { + if (char* env_var_value = getenv(env_var_name.c_str())) return string{env_var_value}; + throw runtime_error("Env var doesn't exist: " + env_var_name); + }}}, + {"cmd", mstch::lambda{[](const string& cmd) -> mstch::node { + // https://stackoverflow.com/a/478960/547223 + array buffer; // 16 KiB + buffer.fill('\0'); + string stdout; + unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); + if (!pipe) throw runtime_error("Command failed: " + cmd); + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) stdout += buffer.data(); + return stdout; + }}}}; - for(int i=2; i+1