diff --git a/Global.cpp b/Global.cpp index 012d763..6564285 100644 --- a/Global.cpp +++ b/Global.cpp @@ -58,73 +58,7 @@ namespace jet { return value.fromCGI(); throw coreutils::Exception("modifier not valid."); } - - coreutils::MString Global::getVariable(coreutils::ZString &variable, - std::map &lvariables, - std::map &keywords) { - if(variable.ifNext("$[")) { - coreutils::MString name; - coreutils::MString modifier; - if(variable.ifNext("!")) { - renderVariableName(variable, name, modifier, lvariables, keywords); - return variables[name]; - } else if(variable.ifNext("%")) { - renderVariableName(variable, name, modifier, lvariables, keywords); - return keywords[name]; - } else if(variable.ifNext(":")) { - renderVariableName(variable, name, modifier, lvariables, keywords); - if(name.find(":") == -1) { - name << ":0"; - } - return processModifier(cgiVariables[name], modifier); - } if(variable.ifNext("@")) { - // TODO: should only allow session variables. Allow substitution. - } if(variable.ifNext("%")) { - renderVariableName(variable, name, modifier, lvariables, keywords); - return getenv(name.c_str()); - } else { - renderVariableName(variable, name, modifier, lvariables, keywords); - name.split("."); - if(name.getList().size() == 1) { - if(variables.find(name[0]) == variables.end()) - throw coreutils::Exception("global variable is not initialized."); - return processModifier(variables[name[0]], modifier); - } - return getSessionVariable(name); - } - throw coreutils::Exception("expected variable name or type designator."); - } if(variable.ifNext("#[")) { - coreutils::MString name; - coreutils::MString modifier; - renderVariableName(variable, name, modifier, lvariables, keywords); - if(lvariables.find(name) == lvariables.end()) - throw coreutils::Exception("local variable is not initialized."); - return lvariables[name]; - } - throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[')."); - } - - void Global::renderVariableName(coreutils::ZString &variable, - coreutils::MString &name, coreutils::MString &modifier, - std::map &lvariables, - std::map &keywords) { - while(!variable.ifNext("]")) { - name << variable.getTokenInclude("?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-"); - if(variable.ifNext(";")) { - renderVariableName(variable, modifier, modifier, lvariables, keywords); - return; - } else if(variable.ifNext(":")) { - name << ":"; - } else if(variable.startsWith("$[") || variable.startsWith("#[")) { - name << getVariable(variable, lvariables, keywords); - } else if(variable.ifNext("]")) - return; - else if(!variable.ifNextInclude("?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-")) - throw coreutils::Exception("invalid variable name."); - } - return; - } - + __mysql * Global::getSession(coreutils::MString sessionId) { if(sessions.find(sessionId) == sessions.end()) throw coreutils::Exception("requested session is not available."); @@ -198,5 +132,5 @@ namespace jet { throw coreutils::Exception("expecting = after name in received CGI data."); } } - + } diff --git a/Global.h b/Global.h index 316e57a..034bda9 100644 --- a/Global.h +++ b/Global.h @@ -19,14 +19,6 @@ namespace jet { void addSession(coreutils::MString sessionId, __mysql *mysql); void removeSession(coreutils::MString sessionId); coreutils::MString processModifier(coreutils::MString &value, coreutils::MString &modifier); - coreutils::MString getVariable(coreutils::ZString &variable, - std::map &lvariables, - std::map &keywords); - void renderVariableName(coreutils::ZString &variable, - coreutils::MString &name, - coreutils::MString &modifier, - std::map &lvariables, - std::map &keywords); __mysql * getSession(coreutils::MString sessionId); coreutils::ZString getSessionVariable(coreutils::MString &splitName); void outputHeaders(); @@ -40,7 +32,7 @@ namespace jet { std::map headers; std::map tags; char **envp; - + }; } diff --git a/TODO.txt b/TODO.txt index 2ad738f..7dec809 100644 --- a/TODO.txt +++ b/TODO.txt @@ -3,3 +3,4 @@ 2) Only allow stream tag in CGI mode. 3) Create a method to upload a file directly to a file name to bypass buffering on large files. +4) Allow the cookie tag only if CGI mode selected. diff --git a/Tag.cpp b/Tag.cpp index a2c3578..12eb0cd 100644 --- a/Tag.cpp +++ b/Tag.cpp @@ -25,6 +25,7 @@ #include "__dotag.h" #include "__stream.h" #include "__dump.h" +#include "Operand.h" #include namespace jet { @@ -206,7 +207,7 @@ namespace jet { } } else if(in.startsWith("$[") || in.startsWith("#[")) { global.errorCursor = in.getCursor(); - out.write(global.getVariable(in, local->variables, keywords)); + out.write(getVariable(in)); } else { out.write(in.nextChar()); } @@ -366,4 +367,102 @@ namespace jet { } return false; } + + coreutils::MString Tag::getVariable(coreutils::ZString &variable) { + if(variable.ifNext("$[")) { + coreutils::MString name; + coreutils::MString modifier; + if(variable.ifNext("!")) { + renderVariableName(variable, name, modifier); + return global.variables[name]; + } else if(variable.ifNext("%")) { + renderVariableName(variable, name, modifier); + return keywords[name]; + } else if(variable.ifNext(":")) { + renderVariableName(variable, name, modifier); + if(name.find(":") == -1) { + name << ":0"; + } + return processModifier(global.cgiVariables[name], modifier); + } else if(variable.ifNext("@")) { + // TODO: should only allow session variables. Allow substitution. + } else if(variable.ifNext("%")) { + renderVariableName(variable, name, modifier); + return getenv(name.c_str()); + } else { + renderVariableName(variable, name, modifier); + name.split("."); + if(name.getList().size() == 1) { + if(global.variables.find(name[0]) == globals.variables.end()) + throw coreutils::Exception("global variable is not initialized."); + return processModifier(global.variables[name[0]], modifier); + } + return global.getSessionVariable(name); + } + throw coreutils::Exception("expected variable name or type designator."); + } else if(variable.ifNext("#[")) { + coreutils::MString name; + coreutils::MString modifier; + renderVariableName(variable, name, modifier); + if(local->variables.find(name) == local->variables.end()) + throw coreutils::Exception("local variable is not initialized."); + return local->variables[name]; + } + throw coreutils::Exception("Expecting a variable initializer ('$[' or '#[')."); + } + + void Tag::renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier) { + while(!variable.ifNext("]")) { + name << variable.getTokenInclude("?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-"); + if(variable.ifNext(";")) { + renderVariableName(variable, modifier, modifier); + return; + } else if(variable.ifNext(":")) { + name << ":"; + } else if(variable.startsWith("$[") || variable.startsWith("#[")) { + name << getVariable(variable); + } else if(variable.ifNext("]")) + return; + else if(!variable.ifNextInclude("?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-")) + throw coreutils::Exception("invalid variable name."); + } + return; + } + + void Tag::storeVariable(coreutils::ZString variable) { + if(keywordDefined("expr")) { + if(!keywordDefined("scope") || (keywords["scope"] == "global")) + global.variables[keywords[variable]] = Operand(keywords["expr"], global, parent->variables, keywords).string; + else if(keywords["scope"] == "local") + local->variables[keywords[variable]] = Operand(keywords["expr"], global, parent->variables, keywords).string; + else if(keywords["scope"] == "parent") + local->parent->variables[keywords[variable]] = Operand(keywords["expr"], global, parent->variables, keywords).string; + } else if(hasContainer) { + processContainer(container); + if(evaluate) { + if(!keywordDefined("scope") || (keywords["scope"] == "global")) + global.variables[keywords[variable]] = out; + else if(keywords["scope"] == "local") + local->variables[keywords[variable]] = out; + else if(keywords["scope"] == "parent") + local->parent->variables[keywords[variable]] = out; + } else { + if(!keywordDefined("scope") || (keywords["scope"] == "global")) + global.variables[keywords[variable]] = container; + else if(keywords["scope"] == "local") + local->variables[keywords[variable]] = container; + else if(keywords["scope"] == "parent") + local->parent->variables[keywords[variable]] = container; + } + } else { + resolveKeyword("value"); + if(!keywordDefined("scope") || (keywords["scope"] == "global")) + global.variables[keywords[variable]] = keywords["value"]; + else if(keywords["scope"] == "local") + local->variables[keywords[variable]] = keywords["value"]; + else if(keywords["scope"] == "parent") + local->parent->variables[keywords[variable]] = keywords["value"]; + } + } + } diff --git a/Tag.h b/Tag.h index a546c79..1f39aa7 100644 --- a/Tag.h +++ b/Tag.h @@ -20,8 +20,6 @@ namespace jet { coreutils::ZString name; coreutils::ZString container; coreutils::ZString container2; - Tag *parent; - Tag *local; protected: bool hasContainer = false; @@ -32,6 +30,8 @@ namespace jet { void copyContainer(coreutils::ZString &in, coreutils::MString &out); Global &global; + Tag *parent; + Tag *local; coreutils::MString &parentOut; coreutils::MString out; @@ -43,6 +43,10 @@ namespace jet { bool trimLines = false; bool cleanWhitespace = false; + coreutils::MString getVariable(coreutils::ZString &variable); + void renderVariableName(coreutils::ZString &variable, coreutils::MString &name, coreutils::MString &modifier); + void storeVariable(coreutils::ZString variable); + private: bool containerOnly = false; coreutils::ZString splitTagName; diff --git a/__set.cpp b/__set.cpp index c9d1f1f..2930268 100644 --- a/__set.cpp +++ b/__set.cpp @@ -22,40 +22,41 @@ namespace jet { throw coreutils::Exception("Cannot use eval with expr."); resolveKeyword("name"); + storeVariable(keywords["name"]); - if(keywordDefined("expr")) { - if(!keywordDefined("scope") || (keywords["scope"] == "global")) - global.variables[keywords["name"]] = Operand(keywords["expr"], global, parent->variables, keywords).string; - else if(keywords["scope"] == "local") - local->variables[keywords["name"]] = Operand(keywords["expr"], global, parent->variables, keywords).string; - else if(keywords["scope"] == "parent") - local->parent->variables[keywords["name"]] = Operand(keywords["expr"], global, parent->variables, keywords).string; - } else if(hasContainer) { - processContainer(container); - if(evaluate) { - if(!keywordDefined("scope") || (keywords["scope"] == "global")) - global.variables[keywords["name"]] = out; - else if(keywords["scope"] == "local") - local->variables[keywords["name"]] = out; - else if(keywords["scope"] == "parent") - local->parent->variables[keywords["name"]] = out; - } else { - if(!keywordDefined("scope") || (keywords["scope"] == "global")) - global.variables[keywords["name"]] = container; - else if(keywords["scope"] == "local") - local->variables[keywords["name"]] = container; - else if(keywords["scope"] == "parent") - local->parent->variables[keywords["name"]] = container; - } - } else { - resolveKeyword("value"); - if(!keywordDefined("scope") || (keywords["scope"] == "global")) - global.variables[keywords["name"]] = keywords["value"]; - else if(keywords["scope"] == "local") - local->variables[keywords["name"]] = keywords["value"]; - else if(keywords["scope"] == "parent") - local->parent->variables[keywords["name"]] = keywords["value"]; - } - +// if(keywordDefined("expr")) { +// if(!keywordDefined("scope") || (keywords["scope"] == "global")) +// global.variables[keywords["name"]] = Operand(keywords["expr"], global, parent->variables, keywords).string; +// else if(keywords["scope"] == "local") +// local->variables[keywords["name"]] = Operand(keywords["expr"], global, parent->variables, keywords).string; +// else if(keywords["scope"] == "parent") +// local->parent->variables[keywords["name"]] = Operand(keywords["expr"], global, parent->variables, keywords).string; + // } else if(hasContainer) { +// processContainer(container); +// if(evaluate) { +// if(!keywordDefined("scope") || (keywords["scope"] == "global")) +// global.variables[keywords["name"]] = out; +// else if(keywords["scope"] == "local") +// local->variables[keywords["name"]] = out; +// else if(keywords["scope"] == "parent") +// local->parent->variables[keywords["name"]] = out; +// } else { +// if(!keywordDefined("scope") || (keywords["scope"] == "global")) +// global.variables[keywords["name"]] = container; +// else if(keywords["scope"] == "local") +// local->variables[keywords["name"]] = container; +// else if(keywords["scope"] == "parent") +// local->parent->variables[keywords["name"]] = container; +// } + // } else { +// resolveKeyword("value"); +// if(!keywordDefined("scope") || (keywords["scope"] == "global")) +// global.variables[keywords["name"]] = keywords["value"]; +// else if(keywords["scope"] == "local") +// local->variables[keywords["name"]] = keywords["value"]; +// else if(keywords["scope"] == "parent") +// local->parent->variables[keywords["name"]] = keywords["value"]; + // } + } }