86 lines
2.7 KiB
C++
86 lines
2.7 KiB
C++
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
#include <stdexcept>
|
|
#include <sstream>
|
|
#include <fstream>
|
|
#include <getopt.h>
|
|
#include <libsclogging.hpp>
|
|
#include <libscrequest.hpp>
|
|
#include "compiler.hpp"
|
|
|
|
sc::logger logger {"curly", sc::loglevel::info};
|
|
|
|
void print_help() {
|
|
std::cout << "usage: curly [-h|--version]\n";
|
|
std::cout << " -h, --help show this help text and exit\n";
|
|
std::cout << " --version show version number and exit\n";
|
|
}
|
|
|
|
void print_version() {
|
|
std::cout << "curly version 1.0\n";
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
try {
|
|
int opt_char, opt_val;
|
|
struct option long_options[] = {
|
|
{"help", no_argument, nullptr, 'h'},
|
|
{"version", no_argument, &opt_val, 1},
|
|
{nullptr, 0, nullptr, 0}
|
|
};
|
|
while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) != -1) {
|
|
std::string arg {optarg ? optarg : ""};
|
|
switch (opt_char) {
|
|
case 0: {
|
|
// handle long-only options here
|
|
switch (opt_val) {
|
|
case 1:
|
|
print_version();
|
|
return EXIT_SUCCESS;
|
|
}
|
|
break;
|
|
}
|
|
case 'h':
|
|
print_help();
|
|
return EXIT_SUCCESS;
|
|
case '?':
|
|
throw std::runtime_error("unrecognized option");
|
|
}
|
|
}
|
|
compiler proc;
|
|
bool done {false};
|
|
if (optind == argc) {
|
|
// here when no file args
|
|
std::string base_url {"https://bobpolis.com/krul/"};
|
|
std::string next_url {"start.txt"};
|
|
sc::requester req;
|
|
req.logger(&logger);
|
|
while (!done) {
|
|
std::istringstream in {req.get(base_url + next_url)};
|
|
next_url = proc.eval(in, done);
|
|
SCInfo(logger, next_url);
|
|
}
|
|
std::cout << next_url << '\n';
|
|
}
|
|
for (int i = optind; i < argc; ++i) {
|
|
try {
|
|
done = false;
|
|
std::ifstream file {argv[i]};
|
|
proc.eval(file, done);
|
|
} catch (const syntax_error& err) {
|
|
std::cerr << "curly: syntax error in " << argv[i];
|
|
std::cerr << ", at line " << err.lineno();
|
|
std::cerr << ": " << err.what() << '\n';
|
|
} catch (const std::runtime_error& ex) {
|
|
std::cerr << "curly: " << ex.what() << '\n';
|
|
}
|
|
}
|
|
|
|
} catch (const std::exception& ex) {
|
|
std::cerr << "curly: " << ex.what() << '\n';
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|