migrating getvariable and setvarable to Tag. Needs alot of work.

This commit is contained in:
barant 2025-01-06 14:57:48 -08:00
parent 229394310d
commit 2549220914
6 changed files with 145 additions and 114 deletions

View File

@ -58,73 +58,7 @@ namespace jet {
return value.fromCGI();
throw coreutils::Exception("modifier not valid.");
}
coreutils::MString Global::getVariable(coreutils::ZString &variable,
std::map<coreutils::MString, coreutils::MString> &lvariables,
std::map<coreutils::MString, coreutils::MString> &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<coreutils::MString, coreutils::MString> &lvariables,
std::map<coreutils::MString, coreutils::MString> &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.");
}
}
}

View File

@ -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<coreutils::MString, coreutils::MString> &lvariables,
std::map<coreutils::MString, coreutils::MString> &keywords);
void renderVariableName(coreutils::ZString &variable,
coreutils::MString &name,
coreutils::MString &modifier,
std::map<coreutils::MString, coreutils::MString> &lvariables,
std::map<coreutils::MString, coreutils::MString> &keywords);
__mysql * getSession(coreutils::MString sessionId);
coreutils::ZString getSessionVariable(coreutils::MString &splitName);
void outputHeaders();
@ -40,7 +32,7 @@ namespace jet {
std::map<coreutils::MString, coreutils::MString> headers;
std::map<coreutils::MString, coreutils::MString> tags;
char **envp;
};
}

View File

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

101
Tag.cpp
View File

@ -25,6 +25,7 @@
#include "__dotag.h"
#include "__stream.h"
#include "__dump.h"
#include "Operand.h"
#include <iostream>
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"];
}
}
}

8
Tag.h
View File

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

View File

@ -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"];
// }
}
}