9 #include <openssl/applink.c>
24 using std::make_unique;
27 using std::unique_ptr;
38 int main(
int argc,
char** argv)
41 auto printVersion = [&]() ->
void {
42 Console::printLine(
string(
"minitscript ") + Version::getVersion());
43 Console::printLine(Version::getCopyright());
46 auto printUsage = [&]() ->
void {
47 Console::printLine(
"Usage: minitscript [--version] [--verbose] [path_to_script | < path_to_script] --arguments [script command line arguments...]");
50 auto printInformation = [&]() ->
void {
55 Console::printLine(
"If you do not provide a path to the script or do not pipe a script into the standard input stream,");
56 Console::printLine(
"you get a prompt to enter your script. You can finish inputting by hitting Ctrl-D on Unix or Ctrl-Z on Windows.");
63 for (
auto i = 1; i < argc; i++) {
64 string argument = argv[i];
65 if (argument ==
"--help") {
69 if (argument ==
"--version") {
72 if (argument ==
"--verbose") {
75 if (argument ==
"--arguments") {
78 if (pathToScript.empty() ==
false) {
79 Console::printLine(
"Invalid command line arguments!");
84 pathToScript = argument;
90 if (version ==
true) {
96 MinitScript::initialize();
98 unique_ptr<Context> context;
99 unique_ptr<MinitScript> script;
104 if (pathToScript.empty() ==
true) {
106 auto statementsOnly =
true;
109 while (cin.eof() ==
false && getline(cin, line)) {
110 if (StringTools::startsWith(line,
"on:") ==
true ||
111 StringTools::startsWith(line,
"on-enabled:") ==
true ||
112 StringTools::startsWith(line,
"function:") ==
true ||
113 StringTools::startsWith(line,
"callable:") ==
true) statementsOnly =
false;
114 scriptCode+= line +
"\n";
116 pathToScript = tmpnam(
nullptr);
119 if (statementsOnly ==
true) {
120 scriptCode = StringTools::replace(
121 FileSystem::getContentAsString(
MINITSCRIPT_DATA +
"/resources/minitscript/templates/cli",
"statements.tscript"),
127 FileSystem::setContentFromString(
128 FileSystem::getPathName(pathToScript),
129 FileSystem::getFileName(pathToScript),
133 tmpFile = pathToScript;
135 pathToScript.clear();
136 Console::printLine(
"An error occurred: " +
string(exception.what()));
140 if (pathToScript.empty() ==
false) {
142 context = make_unique<Context>();
146 vector<string> argumentValues;
147 argumentValues.push_back(
string(argv[0]));
148 bool haveArguments =
false;
149 for (
auto i = 0; i < argc; i++) {
150 string argumentValue(argv[i]);
151 if (haveArguments ==
false &&
152 argumentValue ==
"--arguments") {
153 haveArguments =
true;
157 if (haveArguments ==
false)
continue;
159 argumentValues.push_back(argumentValue);
162 context->setArgumentValues(argumentValues);
165 script = make_unique<MinitScript>();
166 script->setContext(context.get());
168 FileSystem::getPathName(pathToScript),
169 FileSystem::getFileName(pathToScript)
172 if (script !=
nullptr) {
174 if (verbose ==
true) Console::printLine(script->getInformation());
176 if (script->isValid() ==
false) {
177 Console::printLine(pathToScript +
": Script not valid. Exiting!");
181 Network::initialize();
183 #if defined(_MSC_VER)
187 auto scriptPtr = script.get();
188 context->push(scriptPtr);
189 context->addScript(
"application", script.release());
191 while (scriptPtr->isRunning() ==
true) {
192 scriptPtr->execute();
200 if (version ==
false) {
206 if (tmpFile.empty() ==
false) {
208 FileSystem::removeFile(
209 FileSystem::getPathName(tmpFile),
210 FileSystem::getFileName(tmpFile)
213 pathToScript.clear();
214 Console::printLine(
"An error occurred: " +
string(exception.what()));
219 return context->getExitCode();
int main(int argc, char **argv)
std::exception Exception
Exception base class.