Broken everything trying to get variable stuff situated.

This commit is contained in:
Brad Arant 2024-10-17 14:02:02 -07:00
parent 4972799e7a
commit 062a023683
8 changed files with 51 additions and 38 deletions

View File

@ -37,24 +37,22 @@ namespace jet {
sessions.erase(sessionId); sessions.erase(sessionId);
} }
coreutils::ZString Global::getVariable(coreutils::ZString &variable) { coreutils::ZString Global::getVariable(coreutils::ZString &variable, std::map<coreutils::ZString, coreutils::MString> &variables) {
if(variable.ifNext("$[")) { if(variable.ifNext("$[")) {
coreutils::MString name; coreutils::MString name;
coreutils::MString modifier; coreutils::MString modifier;
if(variable.ifNext("!")) { if(variable.ifNext("!")) {
return variables[renderVariableName(variable, name, modifier)]; renderVariableName(variable, name, modifier, variables);
return variables[name];
} if(variable.ifNext(":")) { } if(variable.ifNext(":")) {
// TODO: should only allow CGI variable name. Allow variable substitution. // TODO: should only allow CGI variable name. Allow variable substitution.
} if(variable.ifNext("@")) { } if(variable.ifNext("@")) {
// TODO: should only allow environment variables. Allow substitution. // TODO: should only allow session variables. Allow substitution.
} if(variable.ifNext("$")) { } if(variable.ifNext("%")) {
return getenv(renderVariableName(variable, name, modifier).c_str()); renderVariableName(variable, name, modifier, variables);
return getenv(name.c_str());
} else { } else {
renderVariableName(variable, name, modifier); renderVariableName(variable, name, modifier, variables);
if(!variable.ifNext("]")) {
std::cout << "unparsed: " << variable.unparsed() << std::endl;
throw coreutils::Exception("expecting ] to close variable name.");
}
name.split("."); name.split(".");
if(name.getList().size() == 1) if(name.getList().size() == 1)
return variables[name[0]]; return variables[name[0]];
@ -69,16 +67,21 @@ namespace jet {
throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[')."); throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[').");
} }
coreutils::MString Global::renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier) { void Global::renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier, std::map<coreutils::ZString, coreutils::MString> &variables) {
while(!variable.ifNext("]")) {
name << variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-"); name << variable.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
if(variable.ifNext("]")); if(variable.ifNext(";")) {
else if(variable.ifNext(";")) { renderVariableName(variable, modifier, modifier, variables);
modifier = renderVariableName(variable, modifier, modifier); return;
} }
else if(variable.startsWith("$[")) { else if(variable.startsWith("$[")) {
name << getVariable(variable); name << getVariable(variable, variables);
} }
return name; else if(variable.startsWith("#[")) {
name << getVariable(variable, variables);
}
}
return;
} }
__mysql * Global::getSession(coreutils::MString sessionId) { __mysql * Global::getSession(coreutils::MString sessionId) {

View File

@ -18,8 +18,8 @@ namespace jet {
bool sessionExists(coreutils::MString sessionId); bool sessionExists(coreutils::MString sessionId);
void addSession(coreutils::MString sessionId, __mysql *mysql); void addSession(coreutils::MString sessionId, __mysql *mysql);
void removeSession(coreutils::MString sessionId); void removeSession(coreutils::MString sessionId);
coreutils::ZString getVariable(coreutils::ZString &variable); coreutils::ZString getVariable(coreutils::ZString &variable, std::map<coreutils::ZString, coreutils::MString> &variables);
coreutils::MString renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier); void renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier, std::map<coreutils::ZString, coreutils::MString> &variables);
__mysql * getSession(coreutils::MString sessionId); __mysql * getSession(coreutils::MString sessionId);
coreutils::ZString getSessionVariable(coreutils::MString &splitName); coreutils::ZString getSessionVariable(coreutils::MString &splitName);
void outputHeaders(); void outputHeaders();

View File

@ -3,10 +3,10 @@
namespace jet { namespace jet {
KeywordValue::KeywordValue(coreutils::ZString data, Global &global) : MString() { KeywordValue::KeywordValue(coreutils::ZString data, Global &global, std::map<coreutils::ZString, coreutils::MString> &variables) : MString() {
while(!data.eod()) { while(!data.eod()) {
if(data.startsWith("$[")) { if(data.startsWith("$[") || data.startsWith("#[")) {
write(global.getVariable(data)); write(global.getVariable(data, variables));
} else { } else {
write(data.charAt(0)); write(data.charAt(0));
data.nextChar(); data.nextChar();

View File

@ -13,7 +13,7 @@ namespace jet {
class KeywordValue : public coreutils::MString { class KeywordValue : public coreutils::MString {
public: public:
KeywordValue(coreutils::ZString data, Global &global); KeywordValue(coreutils::ZString data, Global &global, std::map<coreutils::ZString, coreutils::MString> &variables);
virtual ~KeywordValue(); virtual ~KeywordValue();
}; };

12
Tag.cpp
View File

@ -46,7 +46,7 @@ namespace jet {
if(!finished) { if(!finished) {
coreutils::ZString keywordName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); coreutils::ZString keywordName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_");
if(in.ifNext("=\"")) { if(in.ifNext("=\"")) {
variables[keywordName] = KeywordValue(in.getTokenExclude("\""), global); variables[keywordName] = KeywordValue(in.getTokenExclude("\""), global, variables);
} }
if(!in.ifNext("\"")) {} if(!in.ifNext("\"")) {}
} }
@ -163,14 +163,8 @@ namespace jet {
out.write(in.charAt(0)); out.write(in.charAt(0));
in.nextChar(); in.nextChar();
} }
} else if(in.ifNext("#[")) { } else if(in.startsWith("$[") || in.startsWith("#[")) {
coreutils::MString varName = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); out.write(global.getVariable(in, variables));
if(in.ifNext("]"))
out.write(variables[varName]);
else
throw coreutils::Exception("Local variable name has a syntax error.");
} else if(in.startsWith("$[")) {
out.write(global.getVariable(in));
} else { } else {
out.write(in.charAt(0)); out.write(in.charAt(0));
in.nextChar(); in.nextChar();

BIN
jet-2.0

Binary file not shown.

View File

@ -5,9 +5,9 @@
<html> <html>
--- ---
<set name="modified1" value="ABCD" /> <set name="modified1" value="ABCD" />
$[modified1;TOHEX] $[modified1]
<set name="tohex" value="TOHEX" /> <set name="tohex" value="TOHEX" />
$[modified1;$[tohex]] $[modified1]
--- ---
$[nonexistant] $[nonexistant]
$[$HOME] $[$HOME]

16
testvar.jet Executable file
View File

@ -0,0 +1,16 @@
#!./jet-2.0
<jet filterblanklines="true" trimlines="true">
<set name="ix" value="1" />
<set name="letterx" value="x" />
<set name="var1" value="this is a test" />
<set name="1var1" value="This is another test" />
<set name="var1var" value="Yet another test" />
<set name="var11" value="it seems to work." />
$[$[ix]var$[ix]]
$[var$[ix]]
$[var$[ix]var]
$[var$[i$[letterx]]var]
$[letterx;TOHEX]
$[var$[i$[letterx]]$[ix]]
$[var$[i$[letterx]]$[i$[letterx]]]
</jet>