void ExecutableInfo::dumpParams() { CRLog::info("%s", toUtf8(VERSION_STRING).c_str()); if (!exename.empty()) { CRLog::info("Executable file: %s\n", toUtf8(exename).c_str()); if (argCount()) { CRLog::info("Inferior arguments:"); for (int i = 0; i < argCount(); i++) { CRLog::info("[%d] %s", i, toUtf8(args[i]).c_str()); } } } if (!dir.empty()) CRLog::info("Directory: %s\n", toUtf8(dir).c_str()); }
Value FunSubstring::evaluate() const { String s = arg(0)->evaluate().toString(); double doublePos = arg(1)->evaluate().toNumber(); if (isnan(doublePos)) return ""; long pos = static_cast<long>(FunRound::round(doublePos)); bool haveLength = argCount() == 3; long len = -1; if (haveLength) { double doubleLen = arg(2)->evaluate().toNumber(); if (isnan(doubleLen)) return ""; len = static_cast<long>(FunRound::round(doubleLen)); } if (pos > long(s.length())) return ""; if (pos < 1) { if (haveLength) { len -= 1 - pos; if (len < 1) return ""; } pos = 1; } return s.substring(pos - 1, len); }
Value FunNormalizeSpace::evaluate(EvaluationContext& context) const { if (!argCount()) { String s = Value(context.node.get()).toString(); return s.simplifyWhiteSpace(); } String s = arg(0)->evaluate(context).toString(); return s.simplifyWhiteSpace(); }
Value FunConcat::doEvaluate() const { String str = ""; for (unsigned i = 0; i < argCount(); ++i) str += arg(i)->evaluate().toString(); return str; }
Value FunNormalizeSpace::doEvaluate() const { if (argCount() == 0) { String s = Value(Expression::evaluationContext().node).toString(); return Value(s.deprecatedString().simplifyWhiteSpace()); } String s = arg(0)->evaluate().toString(); return Value(s.deprecatedString().simplifyWhiteSpace()); }
// substring(string, number pos, number? len) // // Characters in string are indexed from 1. Numbers are doubles and // substring is specified to work with IEEE-754 infinity, NaN, and // XPath's bespoke rounding function, round. // // <https://www.w3.org/TR/xpath/#function-substring> Value FunSubstring::evaluate(EvaluationContext& context) const { String sourceString = arg(0)->evaluate(context).toString(); const double pos = FunRound::round(arg(1)->evaluate(context).toNumber()); const double len = argCount() == 3 ? FunRound::round(arg(2)->evaluate(context).toNumber()) : std::numeric_limits<double>::infinity(); const auto bounds = computeSubstringStartEnd(pos, len, sourceString.length()); if (bounds.second <= bounds.first) return ""; return sourceString.substring(bounds.first - 1, bounds.second - bounds.first); }
Value FunConcat::evaluate(EvaluationContext& context) const { StringBuilder result; result.reserveCapacity(1024); unsigned count = argCount(); for (unsigned i = 0; i < count; ++i) { String str(arg(i)->evaluate(context).toString()); result.append(str); } return result.toString(); }
Value FunName::evaluate(EvaluationContext& context) const { if (argCount() > 0) { Value a = arg(0)->evaluate(context); if (!a.isNodeSet()) return ""; Node* node = a.toNodeSet(&context).firstNode(); return node ? expandedName(node) : ""; } return expandedName(context.node.get()); }
Value FunConcat::evaluate() const { Vector<UChar, 1024> result; unsigned count = argCount(); for (unsigned i = 0; i < count; ++i) { String str(arg(i)->evaluate().toString()); result.append(str.characters(), str.length()); } return String(result.data(), result.size()); }
bool XPathFunctionIn::doEvaluate(const Node<std::string>& context, const ExecutionContext<std::string>& executionContext) const { for (size_t i = 0; i < argCount(); i++) { XPathValue<std::string> stateName = arg(i, context, executionContext); if (stateName.type() == STRING) { if (_interpreter->isInState(stateName.asString())) { continue; } } return false; } return true; }
Value FunLocalName::evaluate() const { if (argCount() > 0) { Value a = arg(0)->evaluate(); if (!a.isNodeSet()) return ""; Node* node = a.toNodeSet().firstNode(); return node ? expandedNameLocalPart(node) : ""; } return expandedNameLocalPart(evaluationContext().node.get()); }
Value FunNamespaceURI::evaluate() const { if (argCount() > 0) { Value a = arg(0)->evaluate(); if (!a.isNodeSet()) return ""; Node* node = a.toNodeSet().firstNode(); return node ? node->namespaceURI().string() : ""; } return evaluationContext().node->namespaceURI().string(); }
virtual Arabica::XPath::NodeSet<std::string> doEvaluate(const DOM::Node<std::string>& context, const Arabica::XPath::ExecutionContext<std::string>& executionContext) const { Arabica::XPath::NodeSet<std::string> nodes; Arabica::XPath::XPathValue<std::string> a0 = arg(0, context, executionContext); if(argCount() != 1) throw Arabica::XPath::UnsupportedException("two arg version of document()"); if(a0.type() != Arabica::XPath::STRING) throw Arabica::XPath::UnsupportedException("node-set arg version of document()"); load_document(a0.asString(), nodes); return nodes; } // doEvaluate
bool argAppend(const char **aArgs, const char *aNewArg, int *aArgCount, int aArraySize) { bool success = true; if ((*aArgCount) < 0) { *aArgCount = argCount(aArgs); } if ((*aArgCount + 2) > aArraySize) { /* Cannot copy next argument */ success = false; } else { aArgs[*aArgCount] = aNewArg; (*aArgCount)++; aArgs[*aArgCount] = NULL; } return success; }
Value FunNamespaceURI::doEvaluate() const { Node* node = 0; if (argCount() > 0) { Value a = arg(0)->evaluate(); if (!a.isNodeVector() || a.toNodeVector().size() == 0) return ""; node = a.toNodeVector()[0].get(); } if (!node) node = evaluationContext().node.get(); return Value(node->namespaceURI()); }
Value FunName::doEvaluate() const { Node* node = 0; if (argCount() > 0) { Value a = arg(0)->evaluate(); if (!a.isNodeVector() || a.toNodeVector().size() == 0) return ""; node = a.toNodeVector()[0].get(); } if (!node) node = evaluationContext().node.get(); return node->prefix() + ":" + node->localName(); }
static DebugWaitFor dbgShowArg(char *line, processPo p, termPo loc, insWord ins, void *cl) { integer argNo = cmdCount(line, 0); methodPo mtd = p->prog; framePo fp = p->fp; ptrPo sp = p->sp; if (argNo == 0) showAllArgs(debugOutChnnl, p, mtd, fp, sp); else if (argNo > 0 && argNo < argCount(mtd)) showArg(debugOutChnnl, argNo, mtd, fp, sp); else outMsg(debugOutChnnl, "invalid argument number: %d", argNo); resetDeflt("n"); return moreDebug; }
void showStackCall(ioPo out, integer frameNo, methodPo mtd, insPo pc, framePo fp, integer displayDepth) { integer pcOffset = (integer) (pc - mtd->code); termPo locn = findPcLocation(mtd, pcOffset); if (locn != Null) outMsg(out, "[%d] %#L: %T(", frameNo, locn, mtd); else outMsg(out, "[%d] (unknown loc): %T[%d](", frameNo, mtd, pcOffset); integer count = argCount(mtd); char *sep = ""; for (integer ix = 0; ix < count; ix++) { outMsg(out, "%s%,*T", sep, displayDepth, fp->args[ix]); sep = ", "; } outMsg(out, ")\n"); }
Value FunLocalName::evaluate() const { Node* node = 0; if (argCount() > 0) { Value a = arg(0)->evaluate(); if (!a.isNodeSet()) return ""; node = a.toNodeSet().firstNode(); if (!node) return ""; } if (!node) node = evaluationContext().node.get(); return node->localName().string(); }
std::ostream& InstructionSet::Instruction::printAnswer( std::ostream& out, const char* answer ) const { // assumed when param ist 16bit than answer too if( returnLength() == 1 ) { // single bytes out << int(*answer); } else if( argCount() == 2 && returnLength()==2 ) { out << unsigned(answer[0]) << ", " << unsigned(answer[1]); } else if( returnLength()==2 ) { Int16 arg( answer, Endianess::BIG, 2 ); out << arg; } return out; }
Value FunName::evaluate() const { Node* node = 0; if (argCount() > 0) { Value a = arg(0)->evaluate(); if (!a.isNodeSet()) return ""; node = a.toNodeSet().firstNode(); if (!node) return ""; } if (!node) node = evaluationContext().node.get(); const AtomicString& prefix = node->prefix(); return prefix.isEmpty() ? node->localName().string() : prefix + ":" + node->localName(); }
Value FunSubstring::doEvaluate() const { String s = arg(0)->evaluate().toString(); long pos = lround(arg(1)->evaluate().toNumber()); bool haveLength = argCount() == 3; long len = -1; if (haveLength) len = lround(arg(2)->evaluate().toNumber()); if (pos > long(s.length())) return ""; if (haveLength && pos < 1) { len -= 1 - pos; pos = 1; if (len < 1) return ""; } return s.substring(pos - 1, len); }
bool argAppendParse(const char **aArgs, char *aNewArgs, int *aArgCount, int aArraySize) { bool success = true; char *savePtr = NULL; char *nextToken; if ((*aArgCount) < 0) { *aArgCount = argCount(aArgs); } while ((nextToken = strtok_r(aNewArgs, ",", &savePtr))) { if ((*aArgCount + 2) > aArraySize) { /* Cannot copy next argument */ success = false; break; } else { aArgs[*aArgCount] = nextToken; (*aArgCount)++; } aNewArgs = NULL; } aArgs[*aArgCount] = NULL; return success; }
Value FunNumber::evaluate() const { if (!argCount()) return Value(Expression::evaluationContext().node.get()).toNumber(); return arg(0)->evaluate().toNumber(); }
Value FunStringLength::evaluate() const { if (!argCount()) return Value(Expression::evaluationContext().node.get()).toString().length(); return arg(0)->evaluate().toString().length(); }
Value FunStringLength::evaluate(EvaluationContext& context) const { if (!argCount()) return Value(context.node.get()).toString().length(); return arg(0)->evaluate(context).toString().length(); }
int main(int argc ,char *argv[]) { // Check for valid arguments if (argc != 1) { ErrorMsg(); exit(1); } int pathNum = 1; char *mypath[MAX_LINE_LENGTH]; mypath[0] = "/bin"; while(1) { char input[10 * MAX_LINE_LENGTH]; char buff[PATH_MAX + 1]; char *dir; char *cwd; char *redirPath = NULL; printf("whoosh> "); fflush(stdout); if (fgets(input, sizeof(input), stdin) == NULL) { ErrorMsg(); continue; } else { strtok(input, "\n"); if (strlen(input) > MAX_LINE_LENGTH) { ErrorMsg(); continue; } // Get number of arguments int numArgs = argCount(input); if (numArgs != 0) { int i = 0; char *userInput[numArgs + 1]; char *token = strtok(input, " "); // Put each argument in userInput array while (token != NULL) { if (i < numArgs) { if ((i + 1) == numArgs) { userInput[i] = token; } else { userInput[i] = strdup(token); } i++; } token = strtok(NULL, " "); } int redir = 0; int lastRedir = 0; // Check for any redirects for(i = 1; i < numArgs; i++) { if(strcmp(userInput[i], ">") == 0) { redir++; lastRedir = i; } } // Redirect error handling if(redir > 1) { ErrorMsg(); continue; } else if (redir == 1) { // Redirect error handling if(lastRedir != numArgs - 2) { ErrorMsg(); continue; } if (userInput[numArgs - 1][0] == '/') { if(chdir(userInput[numArgs - 1]) == 0) { // Save redirect path redirPath = strdup(userInput[numArgs - 1]); } else { // Redirect error handling ErrorMsg(); continue; } } // Delete the path numArgs = numArgs - 2; } userInput[numArgs] = NULL; // Exit on command if (strcmp(userInput[0], "exit") == 0) { exit(0); } else if (strcmp(userInput[0], "cd") == 0) { // Change directory to home if (numArgs == 1) { dir = getenv("HOME"); if (chdir(dir) != 0) { ErrorMsg(); } } else { // Change directory to the given path dir = userInput[1]; if (chdir(dir) != 0) { ErrorMsg(); } } } else if (strcmp(userInput[0], "pwd") == 0) { // Print working directory cwd = getcwd(buff, PATH_MAX + 1); if (cwd != NULL) { printf("%s\n", cwd); } else { ErrorMsg(); } } else if (strcmp(userInput[0], "path") == 0) { if(numArgs > 1) { // Change path to given path pathNum = 0; for(i = 1 ; i < numArgs; i++) { if(userInput[i][0] == '/') { mypath[i - 1] = strdup(userInput[i]); pathNum++; } else { ErrorMsg(); } } } else { // Remove path mypath[0] = NULL; pathNum = 0; } } else { // Execute other commands int fileExists = 0; // Save current working directory cwd = getcwd(buff, PATH_MAX + 1); // Check every path to find executable command for(i = 0; i < pathNum; i++) { chdir(mypath[i]); struct stat buffer; if(stat(userInput[0], &buffer) == 0) { // File exists fileExists = 1; char *string = malloc(strlen(mypath[i]) + strlen("/") + strlen(userInput[0]) + 1); strcpy(string, mypath[i]); strcat(string, "/"); strcat(string, userInput[0]); userInput[0] = string; // Return to current directory chdir(cwd); break; } } if (fileExists == 0) { ErrorMsg(); continue; } else { // Create new process int rc = fork(); if (rc == 0) { // Child will execute file if (redir == 1) { if (redirPath != NULL) { chdir(redirPath); } close(STDOUT_FILENO); open("output.out", O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); close(STDERR_FILENO); open("output.err", O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); } if (execv(userInput[0], userInput) == -1) { ErrorMsg(); exit(1); } } // Parent waits for child else if(rc > 0) { wait(NULL); } } } } } } return 0; }
Value FunNumber::evaluate(EvaluationContext& context) const { if (!argCount()) return Value(context.node.get()).toNumber(); return arg(0)->evaluate(context).toNumber(); }
size_t Property::parameterCount() const /* override */ { PyScript::ScriptObject attribute = impl_->pythonObject_.getAttribute(impl_->key_.c_str(), PyScript::ScriptErrorPrint()); assert(attribute.exists()); if (!attribute.exists()) { return 0; } if (!attribute.isCallable()) { return 0; } // -- Old-style class instance.__call__(self) if (PyScript::ScriptInstance::check(attribute)) { auto callObject = attribute.getAttribute("__call__", PyScript::ScriptErrorClear()); if (!callObject.exists()) { return 0; } // Convert __call__(self) method object to a function() auto methodObject = PyScript::ScriptMethod::create(callObject); assert(methodObject.exists()); auto functionObject = methodObject.function(); assert(functionObject.exists()); // Convert function to code and get arg count auto codeObject = functionObject.code(); assert(codeObject.exists()); const auto argCount = codeObject.argCount(); // Methods subtract 1 argument for "self". const int selfArg = 1; assert(argCount > 0); return (argCount - selfArg); } // -- Old-style class constructor instance(self) if (PyScript::ScriptClass::check(attribute)) { auto initObject = attribute.getAttribute("__init__", PyScript::ScriptErrorClear()); if (!initObject.exists()) { // Default __init__(self) return 0; } // Convert __init__(self) method object to a function() auto methodObject = PyScript::ScriptMethod::create(initObject); assert(methodObject.exists()); auto functionObject = methodObject.function(); assert(functionObject.exists()); // Convert function to code and get arg count auto codeObject = functionObject.code(); assert(codeObject.exists()); const auto argCount = codeObject.argCount(); // Methods subtract 1 argument for "self". const int selfArg = 1; assert(argCount > 0); return (argCount - selfArg); } // -- Method like self.function(self) auto methodObject = PyScript::ScriptMethod::create(attribute); if (methodObject.exists()) { // Convert self.function() method object to a function() auto functionObject = methodObject.function(); assert(functionObject.exists()); // Convert function to code and get arg count auto codeObject = functionObject.code(); assert(codeObject.exists()); const auto argCount = codeObject.argCount(); // Methods subtract 1 argument for "self". const int selfArg = 1; assert(argCount > 0); return (argCount - selfArg); } // -- Plain function or lambda type auto functionObject = PyScript::ScriptFunction::create(attribute); if (functionObject.exists()) { auto codeObject = functionObject.code(); assert(codeObject.exists()); return codeObject.argCount(); } // -- New-style class instance.__call__(self) auto callObject = attribute.getAttribute("__call__", PyScript::ScriptErrorClear()); // Convert __call__(self) method object to a function() methodObject = PyScript::ScriptMethod::create(callObject); if (methodObject.exists()) { // Convert function to code and get arg count functionObject = methodObject.function(); assert(functionObject.exists()); auto codeObject = functionObject.code(); assert(codeObject.exists()); const auto argCount = codeObject.argCount(); // Methods subtract 1 argument for "self". const int selfArg = 1; assert(argCount > 0); return (argCount - selfArg); } // -- New-style class constructor instance.__init__(self) auto initObject = attribute.getAttribute("__init__", PyScript::ScriptErrorClear()); // Convert __init__(self) method object to a function() methodObject = PyScript::ScriptMethod::create(initObject); if (methodObject.exists()) { // Convert function to code and get arg count functionObject = methodObject.function(); assert(functionObject.exists()); auto codeObject = functionObject.code(); assert(codeObject.exists()); const auto argCount = codeObject.argCount(); // Methods subtract 1 argument for "self". const int selfArg = 1; assert(argCount > 0); return (argCount - selfArg); } // Default __init__(self) return 0; }
Value FunString::doEvaluate() const { if (argCount() == 0) return Value(Expression::evaluationContext().node).toString(); return arg(0)->evaluate().toString(); }