Work on if, call and system tags.

This commit is contained in:
Brad Arant 2024-07-25 02:18:17 +00:00
parent 8475ef01d1
commit 071151d8a1
6 changed files with 123 additions and 20 deletions

View File

@ -1,4 +1,4 @@
#include "__call.h" #include "__system.h"
#include "Exception.h" #include "Exception.h"
#include "MString.h" #include "MString.h"
#include <stdlib.h> #include <stdlib.h>
@ -8,7 +8,7 @@
namespace jet { 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) if(hasContainer)
throw coreutils::Exception("call cannot have a container."); throw coreutils::Exception("call cannot have a container.");
if(!variableDefined(coreutils::ZString("pgm"))) if(!variableDefined(coreutils::ZString("pgm")))

View File

@ -1,14 +1,14 @@
#ifndef ____call_h__ #ifndef ____system_h__
#define ____call_h__ #define ____system_h__
#include "Tag.h" #include "Tag.h"
namespace jet { namespace jet {
class __call : public Tag { class __system : public Tag {
public: public:
__call(coreutils::ZString &in, coreutils::MString &out, Global &global); __system(coreutils::ZString &in, coreutils::MString &out, Global &global);
private: private:
int pid; int pid;

View File

@ -8,6 +8,7 @@ namespace jet {
__if::__if(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global, "else") { __if::__if(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global, "else") {
coreutils::MString result; coreutils::MString result;
bool booleanResult = false;
if(variableDefined("value1")) { if(variableDefined("value1")) {
if(variableDefined("expr")) if(variableDefined("expr"))
@ -19,15 +20,27 @@ namespace jet {
throw coreutils::Exception("type expected if value1 and value2 specified."); throw coreutils::Exception("type expected if value1 and value2 specified.");
} else } else
throw coreutils::Exception("value2 required if value1 specified."); 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")) { } else if(variableDefined("expr")) {
if(variableDefined("value2")) if(variableDefined("value2"))
throw coreutils::Exception("value2 should not be specified with expr."); throw coreutils::Exception("value2 should not be specified with expr.");
if(variableDefined("type")) if(variableDefined("type"))
throw coreutils::Exception("type should not be specified with expr."); throw coreutils::Exception("type should not be specified with expr.");
result = Expression(variables["expr"]); result = Expression(variables["expr"]);
booleanResult = true;
} }
if(result.boolValue()) if(booleanResult)
processContainer(container); processContainer(container);
else else
if(hasContainer2) if(hasContainer2)

57
__system.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "__call.h"
#include "Exception.h"
#include "MString.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
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;
}
}

26
__system.h Normal file
View File

@ -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

View File

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "Global.h" #include "Global.h"
#include "Exception.h"
#include "__jet.h" #include "__jet.h"
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -49,10 +50,16 @@ int main(int argc, char **argv) {
std::cout << "---------\n" << data << "----------\n" << std::endl; std::cout << "---------\n" << data << "----------\n" << std::endl;
jet::Global global; try {
coreutils::MString out; jet::Global global;
jet::__jet *jet = new jet::__jet(data, out, global); coreutils::MString out;
delete jet; jet::__jet *jet = new jet::__jet(data, out, global);
std::cout << ">>-------" << std::endl << out << std::endl << "<<------" << std::endl; delete jet;
// global.dump(); std::cout << ">>-------" << std::endl << out << std::endl << "<<------" << std::endl;
// global.dump();
}
catch(coreutils::Exception e) {
std::cout << "Error caught: " << e.text << std::endl;
}
} }