2018-12-29 05:13:58 +01:00
|
|
|
/* Public Domain / CC-0
|
|
|
|
* Author: tastytea <tastytea@tastytea.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
#include <filesystem>
|
|
|
|
#else
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
#endif
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <libconfig.h++>
|
|
|
|
#include <basedir.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "xdgcfg.hpp"
|
2018-12-28 13:56:34 +01:00
|
|
|
#include "version.hpp"
|
|
|
|
|
2018-12-29 05:13:58 +01:00
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
#else
|
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
#endif
|
|
|
|
using std::cout;
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
string compiler = "g++";
|
|
|
|
fs::path cache_dir;
|
|
|
|
|
|
|
|
void read_settings()
|
|
|
|
{
|
2018-12-29 05:56:26 +01:00
|
|
|
bool need_save = false;
|
2018-12-29 05:13:58 +01:00
|
|
|
xdgcfg config("cppscript.cfg");
|
|
|
|
if (config.read() != 0)
|
|
|
|
{
|
|
|
|
config.write();
|
|
|
|
}
|
|
|
|
libconfig::Setting &cfg = config.get_cfg().getRoot();
|
|
|
|
|
|
|
|
if (cfg.exists("compiler"))
|
|
|
|
{
|
|
|
|
compiler = cfg["compiler"].c_str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cfg.add("compiler", libconfig::Setting::TypeString) = compiler;
|
|
|
|
need_save = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.exists("cache_dir"))
|
|
|
|
{
|
|
|
|
cache_dir = cfg["cache_dir"].c_str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xdgHandle xdg;
|
|
|
|
xdgInitHandle(&xdg);
|
|
|
|
cache_dir = xdgCacheHome(&xdg);
|
|
|
|
cache_dir /= "cppscript";
|
|
|
|
xdgWipeHandle(&xdg);
|
|
|
|
}
|
|
|
|
if (!fs::is_directory(cache_dir))
|
|
|
|
{
|
|
|
|
fs::create_directories(cache_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_save)
|
|
|
|
{
|
|
|
|
config.write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 13:56:34 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-12-29 05:13:58 +01:00
|
|
|
read_settings();
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
const fs::path path(argv[1]);
|
|
|
|
const fs::path binary = cache_dir / path.stem();
|
|
|
|
const fs::path source = binary.string() + ".cpp";
|
2018-12-29 05:33:58 +01:00
|
|
|
string compiler_arguments;
|
2018-12-29 05:13:58 +01:00
|
|
|
|
|
|
|
std::ifstream in(path);
|
|
|
|
if (in.is_open())
|
|
|
|
{
|
|
|
|
std::ofstream out(source);
|
|
|
|
if (out.is_open())
|
|
|
|
{
|
|
|
|
string buf;
|
|
|
|
std::getline(in, buf);
|
2018-12-29 05:33:58 +01:00
|
|
|
std::getline(in, buf);
|
|
|
|
if (buf.substr(0, 12).compare("//cppscript:") == 0)
|
|
|
|
{
|
|
|
|
compiler_arguments = buf.substr(12);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out << buf << endl;
|
|
|
|
}
|
2018-12-29 05:13:58 +01:00
|
|
|
while (!in.eof())
|
|
|
|
{
|
|
|
|
std::getline(in, buf);
|
|
|
|
out << buf << endl;
|
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "ERROR: Could not open file: " << source << endl;
|
2018-12-29 05:56:26 +01:00
|
|
|
std::exit(1);
|
2018-12-29 05:13:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "ERROR: Could not open file: " << path << endl;
|
2018-12-29 05:56:26 +01:00
|
|
|
std::exit(1);
|
2018-12-29 05:13:58 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 05:33:58 +01:00
|
|
|
std::system((compiler + " " + source.string() + " " + compiler_arguments
|
2018-12-29 05:13:58 +01:00
|
|
|
+ " -o " + binary.string()).c_str());
|
2018-12-29 05:33:58 +01:00
|
|
|
execv(binary.c_str(), &argv[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "usage: " << argv[0] << " file [arguments]\n";
|
2018-12-29 05:56:26 +01:00
|
|
|
std::exit(1);
|
2018-12-29 05:13:58 +01:00
|
|
|
}
|
2018-12-28 13:56:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|