A programming language for manipulation of streams.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 

90 рядки
1.7 KiB

#include "parse_tool.h"
#include <cassert>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <chrono>
#include <filesystem>
#include <thread>
using namespace std::chrono_literals;
struct parameters_t{
tool_env default_env{};
std::string exec;
std::chrono::microseconds delay = 0us;
};
parameters_t parse_cmd(const std::vector<std::string>& args)
{
parameters_t ret;
auto it = args.begin();
for(;it!=args.end();it++)
{
if((*it)[0]!='-')
{
ret.exec = *it;
}
else
{
switch((*it)[1])
{
case 'c':
++it;
if(it->size()>=2)
ret.default_env = parse_toolenv(ret.default_env, it->begin(), --(it->end()));
case 'd':
ret.delay = std::chrono::microseconds{std::strtoll(it->c_str()+2, nullptr, 10)};
break;
}
}
}
return ret;
}
int MAIN(std::vector<std::string> args)
{
bool used_tmp = false;
std::string code;
auto parameters = parse_cmd(args);
if(parameters.exec.empty())
{
used_tmp = true;
std::stringstream command{};
auto v = std::getenv("EDITOR");
command<< (v!=NULL ? v : "vi");
std::array<char, L_tmpnam> buff;
parameters.exec = std::tmpnam(&buff.front());
command<<" "<<parameters.exec;
std::system(command.str().c_str());
}
std::ifstream input(parameters.exec);
std::stringstream sstr;
while(input >> sstr.rdbuf());
if(used_tmp)
{
std::filesystem::remove(parameters.exec);
}
code = sstr.str();
tool_env glob = parameters.default_env;
parse_tool p{code, glob};
while(p.has_queue())
{
std::this_thread::sleep_for(parameters.delay);
p.execute();
}
return 0;
}
int main(int argc, char** argv)
{
std::vector<std::string> args;
for(int v=1;v<argc;v++)
args.emplace_back(argv[v]);
return MAIN(args);
}