A programming language for manipulation of streams.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.7 KiB

5 years ago
  1. #include "parse_tool.h"
  2. #include <cassert>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <cstdio>
  6. #include <chrono>
  7. #include <filesystem>
  8. #include <thread>
  9. using namespace std::chrono_literals;
  10. struct parameters_t{
  11. tool_env default_env{};
  12. std::string exec;
  13. std::chrono::microseconds delay = 0us;
  14. };
  15. parameters_t parse_cmd(const std::vector<std::string>& args)
  16. {
  17. parameters_t ret;
  18. auto it = args.begin();
  19. for(;it!=args.end();it++)
  20. {
  21. if((*it)[0]!='-')
  22. {
  23. ret.exec = *it;
  24. }
  25. else
  26. {
  27. switch((*it)[1])
  28. {
  29. case 'c':
  30. ++it;
  31. if(it->size()>=2)
  32. ret.default_env = parse_toolenv(ret.default_env, it->begin(), --(it->end()));
  33. case 'd':
  34. ret.delay = std::chrono::microseconds{std::strtoll(it->c_str()+2, nullptr, 10)};
  35. break;
  36. }
  37. }
  38. }
  39. return ret;
  40. }
  41. int MAIN(std::vector<std::string> args)
  42. {
  43. bool used_tmp = false;
  44. std::string code;
  45. auto parameters = parse_cmd(args);
  46. if(parameters.exec.empty())
  47. {
  48. used_tmp = true;
  49. std::stringstream command{};
  50. auto v = std::getenv("EDITOR");
  51. command<< (v!=NULL ? v : "vi");
  52. std::array<char, L_tmpnam> buff;
  53. parameters.exec = std::tmpnam(&buff.front());
  54. command<<" "<<parameters.exec;
  55. std::system(command.str().c_str());
  56. }
  57. std::ifstream input(parameters.exec);
  58. std::stringstream sstr;
  59. while(input >> sstr.rdbuf());
  60. if(used_tmp)
  61. {
  62. std::filesystem::remove(parameters.exec);
  63. }
  64. code = sstr.str();
  65. tool_env glob = parameters.default_env;
  66. parse_tool p{code, glob};
  67. while(p.has_queue())
  68. {
  69. std::this_thread::sleep_for(parameters.delay);
  70. p.execute();
  71. }
  72. return 0;
  73. }
  74. int main(int argc, char** argv)
  75. {
  76. std::vector<std::string> args;
  77. for(int v=1;v<argc;v++)
  78. args.emplace_back(argv[v]);
  79. return MAIN(args);
  80. }