void qObjODBC::Execute(qCtx *ctx, qStr *out, const char *sql, CStr &body, CStr &head, CStr &foot) { if (!myConn) { ctx->Throw(out, 302, myConn.GetErrorMsg()); return; } myStmt = myConn.Execute(sql); if (!myStmt) { ctx->Throw(out, 302, myStmt.GetErrorMsg()); return; } if ( ! ( body.IsEmpty() && head.IsEmpty() && foot.IsEmpty() ) ) { if (myStmt.Bind()) { bool ok = myStmt.Next(); ctx->MapObj(&ok, (QOBJFUNC) EvalBreak, "break"); if (!head.IsEmpty()) ctx->Parse(head, out); ok = ok && !myStmt.Done(); while (ok) { ctx->Parse(body, out); if (ok) ok = myStmt.Next(); } if (!foot.IsEmpty()) ctx->Parse(foot, out); } } }
void EvalDefctx(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("defctx", 2, 3); if (args->Count() > 1) { CStr name = (*args)[0]; if (name.IsEmpty()) { ctx->Throw(out, 121, "defctx: empty variable name"); return; } CStr bname = (*args)[1]; CStr body; if (args->Count() > 2) body = args->GetAt(2); qObj *base = 0; if (!bname.IsEmpty()) ctx->Find(&base, bname); qObjClass *obj = new qObjClass((qObjClass *)base); // now load in methods and variables obj->GetCtx()->SetParent(ctx); qStrNull tmpOut; obj->GetCtx()->Parse(body, &tmpOut); ctx->MapObj(obj, name); obj->GetCtx()->SetParent(NULL); } }
void EvalFmt(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { CStr val = (*args)[0]; ColFmt col; memset(&col, 0, sizeof(col)); col.dec = args->Count() > 1 ? ParseInt((*args)[1]) : 2; if (args->Count() > 2) { CStr dch = (*args)[2]; col.dch = (dch.IsEmpty()) ? 0 : *dch; } else col.dch = '.'; if (args->Count() > 3) { CStr tho = (*args)[3]; col.tho = (tho.IsEmpty()) ? 0 : *tho; } else col.tho = ','; CStr res(256); double dval; const char *p = CTabFmt::NumFmt(col, val.SafeP(), res.GetBuffer(), 256, &dval); out->PutS(p); }
static CStr set_default_log() { CStr log = getenv("SMX_LOG"); #ifdef unix if (log.IsEmpty()) { const char * varlog = "/var/log/smx_debug.log"; FILE * t = fopen(varlog, "a"); if (t) { log = varlog; fclose(t); } } #endif if (log.IsEmpty()) { const char * tmp = getenv("tmp"); tmp = getenv("temp"); if (tmp) { log = tmp; log += "/smx_debug.log"; } } if (log.IsEmpty()) { const char * tmp = getenv("HOME"); if (tmp) { log += "/smx_debug.log"; } } return log; }
void LoadHSet(qCtx *ctx) { //core CStr tmpName; tmpName = getenv("SMXHOME"); if (tmpName.IsEmpty()) { #ifdef WIN32 tmpName = getenv("TEMP"); if (tmpName.IsEmpty()) { tmpName = getenv("HOMEDRIVE"); if (!tmpName.IsEmpty()) { tmpName += getenv("HOMEPATH"); tmpName.RTrim('/'); tmpName.RTrim('\\'); } else { tmpName = "."; } } tmpName = tmpName + "\\.smx"; CreateDirectory(tmpName,NULL); tmpName = tmpName + "\\hset.db"; #else tmpName = getenv("HOME"); if (tmpName.IsEmpty()) tmpName = "/tmp"; mkdir(tmpName,0750); tmpName = tmpName + "/.smx"; mkdir(tmpName,0750); tmpName = tmpName + "/hset.db"; #endif } else { #ifdef WIN32 tmpName = tmpName + "\\hset.db"; #else tmpName = tmpName + "/hset.db"; #endif } #ifdef WIN32 remove(tmpName); #endif qObjHCtx *hCtx = new qObjHCtx(ctx); hCtx->SetPath(tmpName, true); ctx->MapObj(hCtx, EvalHSet, "hset"); ctx->MapObj(hCtx, EvalHSet, "hdel"); ctx->MapObj(hCtx, EvalHGet, "hget"); ctx->MapObj(hCtx, EvalHExists, "hexists"); ctx->MapObj(hCtx, EvalHFile, "hdbfile"); ctx->MapObj(hCtx, EvalHEnumValues, "henumvalues"); ctx->MapObj(hCtx, EvalHEnumKeys, "henumkeys"); ctx->MapObj(hCtx, EvalHEnumTree, "henumtree"); ctx->MapObj(hCtx, "<hctx>"); }
void qObjProto::EvalXTCP(qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() < 1) { ctx->Throw(out, 655, "USAGE: %connect-tcp(host,body[,timeout]])"); return; } CStrAry hosts; CStr serv = (*args)[0]; CStr bodyStr = args->GetAt(1); double timeout = ParseDbl((*args)[2]); if (serv.IsEmpty()) return; int port = 0; char * p = strchr((const char *)serv, ':'); if (p) { port = atoi(p+1); *p = '\0'; ++p; } if (!port) port = IPPORT_TELNET; int sock_rval; Sock sock; CStr body, dom, ref; qCtxTmp tmpCtx(ctx); if (!bodyStr.IsEmpty()) { tmpCtx.MapObj(&sock, EvalXTCPSend,"send"); tmpCtx.MapObj(&sock, EvalXTCPRecv,"recv"); } PROTO_OPEN_SOCK(); if (timeout > 1) sock.SetTimeout((float) timeout); tmpCtx.Parse(bodyStr, out); sock.Close(); return; }
void qObjProto::EvalTCPConn(qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() < 1) { ctx->Throw(out, 655, "USAGE: %tcp(name,host[,timeout]])"); return; } CStr name = (*args)[0]; CStr serv = (*args)[1]; double timeout = ParseDbl((*args)[2]); if (serv.IsEmpty()) { ctx->Throw(out, 656, "%tcp : host unspecified"); return; } if (name.IsEmpty()) { ctx->Throw(out, 656, "%tcp : connection name unspecified"); return; } int port = 0; char * p = strchr(serv, ':'); if (p) { port = atoi(p+1); *p = '\0'; ++p; } #ifndef IPPORT_TELNET #define IPPORT_TELNET 23 #endif if (!port) port = IPPORT_TELNET; qObjTcp *conn = new qObjTcp(); int sock_rval; PROTO_OPEN_SOCK2(conn->sock); if (timeout > 1) conn->sock.SetTimeout((float) timeout); ctx->MapObjLet(conn, name); return; }
void EvalXReplace(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { int i = 1; CStr in = (*args)[0]; while (i < args->Count()) { CStr from = (*args)[i]; if (!in.IsEmpty() && !from.IsEmpty()) { CStr to = (*args)[i+1]; in = ReplaceStr(in, from, to); } i += 2; } out->PutS(in); }
static int psx_auth_fail(request_rec *r, qEnvApache *renv) { // this is kinda wrong.... but for now we keep it.... if (!r->header_only) { CStr resp = renv->GetCtx()->Eval("http-noauth"); if (!resp.IsEmpty()) { qStrBuf bufin(resp); qStrBuf bufout; renv->GetCtx()->ParseTry(&bufin, &bufout); ap_custom_response(r, HTTP_UNAUTHORIZED, bufout.GetBuffer()); } } if (!ap_auth_type(r)) { ap_table_setn(r->err_headers_out, r->proxyreq ? "Proxy-Authenticate" : "WWW-Authenticate", ap_pstrcat(r->pool, "Basic realm=\"", ap_auth_name(r), "\"", NULL)); } else { ap_note_basic_auth_failure(r); } renv->Free(); ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, AP2STATUS r, "returning unauthorized: %d, status : %d", HTTP_UNAUTHORIZED, r->status); return HTTP_UNAUTHORIZED; }
void EvalRTrim(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("ltrim", 1, 2); if (args->Count() > 1) { CStr str = (*args)[0]; if (str.IsEmpty()) return; CStr toks = (*args)[1]; if (toks.Length() == 0) out->PutS(str); else if (toks.Length() == 1) out->PutS(str.RTrim(*toks)); else { const char *p, *b = str; p = b + str.Length() - 1; while(p >= b && strchr((const char *)toks, *p)) --p; ++p; out->PutS(b, p - b); } } else out->PutS((*args)[0].RTrim()); }
void EvalTrim(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("trim", 1, 2); if (args->Count() > 1) { CStr str = (*args)[0]; if (str.IsEmpty()) return; CStr toks = (*args)[1]; if (toks.Length() == 0) out->PutS(str); else if (toks.Length() == 1) out->PutS(str.Trim(*toks)); else { const char *b, *p = str; size_t i = strspn(p,toks); b = p += i; p += str.Length() - 1 - i; while(p >= b && strchr(toks.Data(), *p)) --p; ++p; out->PutS(b, p - b); } } else out->PutS((*args)[0].Trim()); }
bool Open(qCtx *ctx, qStr *out, CStr port, CStr dcb_def) { DCB dcb; HANDLE tCom; tCom = CreateFile( port, GENERIC_READ | GENERIC_WRITE, 0, // comm devices must be opened w/exclusive-access NULL, // no security attributes OPEN_EXISTING, // comm devices must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); if (tCom == INVALID_HANDLE_VALUE) { ctx->ThrowF(out, 601, "Invalid comm port. %y", GetLastError()); return false; } GetCommState(tCom, &dcb); if (!dcb_def.IsEmpty() && (!BuildCommDCB(dcb_def, &dcb) || !SetCommState(tCom, &dcb)) ) { ctx->ThrowF(out, 602, "Invalid comm definition parameter(s). %y", GetLastError()); return false; } myCom = tCom; return true; }
void EvalTCPRecv(qCtx *ctx, qStr *out, qArgAry *args) { int len; char *line = NULL; CStr lcmd = (*args)[0]; if (!lcmd.IsEmpty()) { if ((lcmd[0] = '*')) { len = sock.Read(SOCK_DEFAULT_BUF_SIZE); while (len > 0) { out->PutS(sock.GetBuf(), len); len = sock.Read(SOCK_DEFAULT_BUF_SIZE); } } else { int max = atoi(lcmd); int sofar = 0; while (len > 0 && sofar < max) { sofar += (len = sock.Read(min(SOCK_DEFAULT_BUF_SIZE,max))); if (len) out->PutS(sock.GetBuf(), len); } } } else { len = sock.ReadLine(&line); if (len > 0) out->PutS(line, len); } if (len < 0 ) { if (len == Sock::ERR_TIMEOUT) { ctx->ThrowF(out, 702, "Timeout while waiting to read data from host %s:%d, %y", sock.GetHost(), sock.GetPort()); } else { ctx->ThrowF(out, 702, "Error while reading data from host %s:%d, %y", sock.GetHost(), sock.GetPort(), GetLastError()); } } }
CStr B64_decode(CStr strin) { if (!strin.IsEmpty()) { CStr strout((int) strin.Length()); int len = B64_decode(strin, strout.GetBuffer(), strin.Length()); if (len < 0) len = 0; return strout.Grow(len); } else return strin; }
void EvalFileDelete(const void *mode, qCtx *ctx, qStr *out, qArgAry *args) { CStr path = (*args)[0]; if (path.IsEmpty()) return; if (!safe_fcheck(ctx, path, 'w')) { ctx->ThrowF(out, 632, "Error deleting file, %y", GetLastError()); return; } remove(path); }
int EvalWNCom(const void *a, const void *b) { WNCompX *c = ((WNAryX *) a)->c; c->a = ((WNAryX *) a)->v; c->b = ((WNAryX *) b)->v; CStr r = c->ctx->ParseStr(c->alg); if (!r.IsEmpty()) { return atoi(r); } else return 0; }
// blowfish decrypt void EvalDecode(const void *data, qCtx *ctx, qStr *pStream, qArgAry *args) { if (args->Count() > 0) { CStr encoding = (*args)[1]; CStr out; if (!encoding.IsEmpty() && !stricmp(encoding, "hex")) { out = HEX_decode((*args)[0]); } else { out = B64_decode((*args)[0]); } pStream->PutS(out); } }
void WriteCSV(const void *mode, bool forceQuoted, qCtx *ctx, qStr *out, qArgAry *args) { CStr path = ctx->ParseStr((*args)[0]); if (path.IsEmpty()) return; FILE *fp = safe_fopen(ctx, path, (const char *) mode); if (!fp) { ctx->ThrowF(out, 601, "Failed to open file for writing. %y", GetLastError()); return; } qStrFileO fo(fp, true); CStr bo; qStrBuf quot; qCtxTmp sub(ctx); sub.MapObj(fp, EvalFileFlush,"flush"); int i; char *p, *b; for (i = 1; i < args->Count(); ++i) { bo = sub.ParseStr((*args)[i]); quot.Clear(); b = bo.GetBuffer(); if ((p = strchr((const char *)b, '"'))) { quot.PutC('"'); do { quot.PutS(b, p - b + 1); quot.PutS('"'); } while ((p = strchr((const char *)b, '"'))); quot.PutC('"'); fo.PutS(quot); } else if (forceQuoted || (p = strchr((const char *)b, ','))) { quot.PutC('"'); quot.PutS(bo); quot.PutC('"'); fo.PutS(quot); } else fo.PutS(bo); if (i < (args->Count()-1) ) { fo.PutC(','); } } fo.PutC('\n'); }
void EvalEncrypt(const void *data, qCtx *ctx, qStr *pStream, qArgAry *args) { if (args->Count() > 1) { CStr cipher = (*args)[2]; CStr encoding = (*args)[3]; CStr out; if (!encoding.IsEmpty() && !stricmp(encoding, "hex")) { out = HEX_encode(EVP_encrypt((*args)[1],(*args)[0],cipher)); } else { out = B64_encode(EVP_encrypt((*args)[1],(*args)[0],cipher)); } pStream->PutS(out); } }
void EvalDecrypt(const void *data, qCtx *ctx, qStr *pStream, qArgAry *args) { if (args->Count() > 1) { CStr cipher = (*args)[2]; CStr encoding = (*args)[3]; CStr out; if (!encoding.IsEmpty() && !stricmp(encoding, "hex")) { out = EVP_decrypt((*args)[1].SafeP(),HEX_decode((*args)[0].SafeP()),cipher); } else { out = EVP_decrypt((*args)[1].SafeP(),B64_decode((*args)[0].SafeP()),cipher); } pStream->PutS(out); } }
void EvalLet(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("let", 1, 2); if (args->Count() >= 1) { CStr var = (*args)[0]; if (var.IsEmpty()) { ctx->Throw(out, 121, "let: empty variable name"); return; } if (args->Count() >= 2) { CStr val = (*args)[1]; ctx->MapObjLet(ctx->CreateObj(val), var); } } }
void EvalFileExists(const void *mode, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("exists", 1, 1); CStr path = (*args)[0]; if (path.IsEmpty()) return; #ifdef WIN32 if (_access(path, 00) == 0) { out->PutC('T'); } #else struct stat s; if (stat(path, &s) == 0) { out->PutC('T'); } #endif }
void EvalFmtgTime(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() >= 1) { time_t tmt = ParseInt((*args)[0]); struct tm * tms = gmtime(&tmt); CStr p; if (args->Count() == 1) p = "m/d/yy hh:nn aa"; else p = (*args)[1]; if (!p.IsEmpty() && tms) { FmtTime(tms, p, out); } } }
void qObjDef::AddArgName(const CStr arg, int quoted) { if (!arg.IsEmpty()) myArgNames.Add(arg); else myArgNames.Add(0); myQuoted.Add(quoted); if (quoted==dQuoted && myQmap.IsEmpty()) { int i; for (i = 0; i < myQuoted.Count(); ++i) { myQmap << ((myQuoted[i] == (int) dQuoted) ? '1' : '0'); } } else { myQmap << ((quoted == (int) dQuoted) ? '1' : '0'); } }
// expand an unexpanded (quoted) macro, exposing only the specified functions void EvalSafeExpand(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { CStr val = (*args)[0]; if (val.Length() > 0) { qCtxTmp tCtx(ctx->GetEnv()); int i; CStr name; qObj *obj; for (i=1; i < args->Count(); ++i) { name = (*args)[i]; if (!name.IsEmpty() && ctx->Find(&obj, name)) { tCtx.MapObj(new qObjSafe(obj, ctx), name); } } tCtx.Parse(val, out); } }
CDbCol *qObjODBC::GetEvalCol(qCtx *ctx, qArgAry *args) { CDbCol *col = 0; if (args->Count() > 0) { CStr index = (*args)[0]; index.Trim(); if (!index.IsEmpty()) { if (isdigit(index[0])) { col = myStmt.Column(atoi(index)-1); } else { strlwr(index.GetBuffer()); col = myStmt.Column((const char *)index); } } } return col; }
void EvalGetAttrValue(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 0) { CStr tmp = (*args)[0]; if (args->Count() > 1) { CStr m = (*args)[1]; if (tmp && !m.IsEmpty()) { char *p = tmp.GetBuffer(); char *n = p; char *v = 0; while (*p) { while (isspace(*p)) ++p; if (*p == '=') { *p++ = '\0'; while (isspace(*p)) ++p; v = p; while (*p) { if (*p == ';') { *p = '\0'; if (!stricmp(m, n)) { out->PutS(v); return; } while (isspace(*++p)); n = p; v = NULL; break; } ++p; } } else if (*p == ';') n = p + 1; ++p; } if (v && *v && n && *n && !stricmp(m, n)) { out->PutS(v); return; } } } } }
void EvalGSet(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("gset", 1, 2); if (args->Count() >= 1) { CStr var = (*args)[0]; if (var.IsEmpty()) { ctx->Throw(out, 121, "gset: empty variable name"); return; } if (args->Count() >= 2) { CStr val = (*args)[1]; if (ctx->GetEnv() && ctx->GetEnv()->GetSessionCtx()) { ctx->MapObjTop(ctx->CreateObj(val), var, ctx->GetEnv()->GetSessionCtx()); } else { ctx->MapObjTop(ctx->CreateObj(val), var, NULL); } } } }
void EvalFileWrite(const void *mode, qCtx *ctx, qStr *out, qArgAry *args) { CStr path = ctx->ParseStr((*args)[0]); FILE *fp; int err = 0; #ifndef WIN32 CStr perm_str = ctx->ParseStr((*args)[2]); int perms = strtol(perm_str.SafeP(),(char **)NULL, 0); mode_t prev_perms; if (perms) { prev_perms = umask((mode_t)~perms); printf("umask: %d (%d)\n", perms, prev_perms); } try { #endif if (path.IsEmpty()) return; fp = safe_fopen(ctx, path, (const char *) mode); if (!fp) err = GetLastError(); #ifndef WIN32 } catch (qCtxEx ex) { if (perms) umask(prev_perms); throw ex; } if (perms) umask(prev_perms); #endif if (!fp) { ctx->ThrowF(out, 601, "Failed to open file for writing. %y", err); return; } qStrFileO fo(fp, true); qCtxTmp sub(ctx); sub.MapObj(fp, EvalFileFlush,"flush"); sub.Parse(args->GetAt(1), &fo); }
void EvalLTrim(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("ltrim", 1, 2); if (args->Count() > 1) { CStr str = (*args)[0]; if (str.IsEmpty()) return; CStr toks = (*args)[1]; if (toks.Length() == 0) out->PutS(str); else if (toks.Length() == 1) out->PutS(str.LTrim(*toks)); else { const char *p = str; size_t i = strspn(p,toks); out->PutS(p+i, str.Length() - i); } } else out->PutS((*args)[0].LTrim()); }