From 071151d8a1ae150a8632b4021fcec5e9d410956d Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Thu, 25 Jul 2024 02:18:17 +0000 Subject: [PATCH] Work on if, call and system tags. --- __call.cpp | 4 ++-- __call.h | 8 ++++---- __if.cpp | 29 ++++++++++++++++++-------- __system.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ __system.h | 26 ++++++++++++++++++++++++ testjet.cpp | 19 ++++++++++++------ 6 files changed, 123 insertions(+), 20 deletions(-) create mode 100644 __system.cpp create mode 100644 __system.h diff --git a/__call.cpp b/__call.cpp index 89b2650..d2157fc 100644 --- a/__call.cpp +++ b/__call.cpp @@ -1,4 +1,4 @@ -#include "__call.h" +#include "__system.h" #include "Exception.h" #include "MString.h" #include @@ -8,7 +8,7 @@ namespace jet { - __call::__call(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { + __system::__system(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { if(hasContainer) throw coreutils::Exception("call cannot have a container."); if(!variableDefined(coreutils::ZString("pgm"))) diff --git a/__call.h b/__call.h index 09beb14..bea3b18 100644 --- a/__call.h +++ b/__call.h @@ -1,14 +1,14 @@ -#ifndef ____call_h__ -#define ____call_h__ +#ifndef ____system_h__ +#define ____system_h__ #include "Tag.h" namespace jet { - class __call : public Tag { + class __system : public Tag { public: - __call(coreutils::ZString &in, coreutils::MString &out, Global &global); + __system(coreutils::ZString &in, coreutils::MString &out, Global &global); private: int pid; diff --git a/__if.cpp b/__if.cpp index e7ed345..9453c85 100644 --- a/__if.cpp +++ b/__if.cpp @@ -8,7 +8,8 @@ namespace jet { __if::__if(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global, "else") { coreutils::MString result; - + bool booleanResult = false; + if(variableDefined("value1")) { if(variableDefined("expr")) throw coreutils::Exception("Either value1 or expr can be specified but not both."); @@ -19,15 +20,27 @@ namespace jet { throw coreutils::Exception("type expected if value1 and value2 specified."); } else throw coreutils::Exception("value2 required if value1 specified."); - std::cout << variables["value1"].str() << "' and value2 '" << variables["value2"].str() << "' comparison" << std::endl; + if(!variableDefined("type")) + throw coreutils::Exception("type required for a value1 and value2 comparison."); + int rc = variables["value1"].compare(variables["value2"]); + if(((variables["type"] == "eq") && (rc == 0)) || + ((variables["type"] == "ne") && (rc != 0)) || + ((variables["type"] == "lt") && (rc == -1)) || + ((variables["type"] == "le") && (rc != 1)) || + ((variables["type"] == "gt") && (rc == 1)) || + ((variables["type"] == "ge") && (rc != -1))) + booleanResult = true; + else + throw coreutils::Exception("type value must be 'eq','ne','lt','le','gt','ge'."); } else if(variableDefined("expr")) { - if(variableDefined("value2")) - throw coreutils::Exception("value2 should not be specified with expr."); - if(variableDefined("type")) - throw coreutils::Exception("type should not be specified with expr."); - result = Expression(variables["expr"]); + if(variableDefined("value2")) + throw coreutils::Exception("value2 should not be specified with expr."); + if(variableDefined("type")) + throw coreutils::Exception("type should not be specified with expr."); + result = Expression(variables["expr"]); + booleanResult = true; } - if(result.boolValue()) + if(booleanResult) processContainer(container); else if(hasContainer2) diff --git a/__system.cpp b/__system.cpp new file mode 100644 index 0000000..89b2650 --- /dev/null +++ b/__system.cpp @@ -0,0 +1,57 @@ +#include "__call.h" +#include "Exception.h" +#include "MString.h" +#include +#include +#include +#include + +namespace jet { + + __call::__call(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { + if(hasContainer) + throw coreutils::Exception("call cannot have a container."); + if(!variableDefined(coreutils::ZString("pgm"))) + throw coreutils::Exception("pgm keyword must be specified."); + argv[0] = variables["pgm"].c_str(); // TODO: Need to peel off the program name only and pass as argv[0]. + for(ix = 1; ix <= 50; ++ix) { + coreutils::MString arg("arg"); + arg << ix; + if(variableDefined(arg)) + argv[ix] = variables[arg].c_str(); + else + break; + } + argv[ix] == NULL; + pipe(fdo); + pid = fork(); + if(pid == 0) { + close(fdo[0]); + dup2(fdo[1], 1); + if(variableDefined("input")) { + coreutils::ZString input(variables[variables["input"]]); + pipe(fdi); + if(fork() == 0) { + close(fdi[0]); + write(fdi[1], input.getData(), input.getLength()); + close(fdi[1]); + exit(0); + } + close(fdi[0]); + dup2(fdi[0], 0); + } + rc = execve(variables["pgm"].c_str(), argv, NULL); + close(fdo[1]); + std::cout << "rc: " << rc << std::endl; + exit(rc); + } + close(fdo[1]); + if(variableDefined("name")) + global.variables[variables["name"]].read(fdo[0]); + else + out.read(fdo[0]); + waitpid(pid, &status, 0); + std::cout << "status: " << status << std::endl; + } + +} diff --git a/__system.h b/__system.h new file mode 100644 index 0000000..09beb14 --- /dev/null +++ b/__system.h @@ -0,0 +1,26 @@ +#ifndef ____call_h__ +#define ____call_h__ + +#include "Tag.h" + +namespace jet { + + class __call : public Tag { + + public: + __call(coreutils::ZString &in, coreutils::MString &out, Global &global); + + private: + int pid; + int status; + int ix; + int fdi[2]; + int fdo[2]; + int rc; + char *argv[50]; + + }; + +} + +#endif diff --git a/testjet.cpp b/testjet.cpp index 5151c79..a14d3da 100644 --- a/testjet.cpp +++ b/testjet.cpp @@ -1,6 +1,7 @@ #include #include #include "Global.h" +#include "Exception.h" #include "__jet.h" int main(int argc, char **argv) { @@ -49,10 +50,16 @@ int main(int argc, char **argv) { std::cout << "---------\n" << data << "----------\n" << std::endl; - jet::Global global; - coreutils::MString out; - jet::__jet *jet = new jet::__jet(data, out, global); - delete jet; - std::cout << ">>-------" << std::endl << out << std::endl << "<<------" << std::endl; -// global.dump(); + try { + jet::Global global; + coreutils::MString out; + jet::__jet *jet = new jet::__jet(data, out, global); + delete jet; + std::cout << ">>-------" << std::endl << out << std::endl << "<<------" << std::endl; + // global.dump(); + } + catch(coreutils::Exception e) { + std::cout << "Error caught: " << e.text << std::endl; + } + }