bool _ScanDir(CStr path, int mask, CStr body, qCtx *ctx, qStr *out, DIRSTATE &st) { BOOL bMore; HANDLE hFind; BOOL showdot = false; // truncate trailing slashes char *b = path.GetBuffer(); if (!b || !*b) return false; char *p = path+path.Length() - 1; while (p >= b && ISDIRSEP(*p)) --p; if (p-b+1 > 0) { if (*p == ':') { showdot = true; if (!ISDIRSEP(p[1])) { path << '.'; b = path.GetBuffer(); p = path+path.Length() - 1; } else ++p; } st.path = path; // truncate path to parent while (p >= b && !ISPATHSEP(*p)) --p; if (p >= b) { st.path.Grow(p-b+1); } else { st.path.Grow(0); } } else { st.path = path; } // read all entries in the directory WIN32_FIND_DATA *r = &st.data; hFind = FindFirstFile(path, r); bMore = (hFind != (HANDLE) -1); while (bMore &&!st.bquit) { if ((mask & r->dwFileAttributes) && !(r->cFileName[0]=='.'&&r->cFileName[1]=='\0') ) { ctx->Parse(body, out); } else if (showdot && r->cFileName[0]=='.'&&r->cFileName[1]=='\0') { ctx->Parse(body, out); } bMore = FindNextFile(hFind, r); } FindClose(hFind); return true; } // dir_scan
bool SetError(int nResult, bool bFree = true) { if (!myErrRes && !SQL_SUCCEEDED(nResult)) { myErrBuf.Grow(1024); myErrState.Grow(6); SQLSMALLINT tlen = 0; if (!SQLGetDiagRec(myType, myHandle, 1, (SQLCHAR*) myErrState.GetBuffer(), &myErrCode, (SQLCHAR*) (myErrBuf.GetBuffer())+8, myErrBuf.Length()-8, &tlen)) { myErrBuf[0] = '('; memcpy(myErrBuf+1, (const char *)myErrState, 5); myErrBuf[6] = ')'; myErrBuf[7] = ' '; myErrBuf.Grow(min(tlen+8,myErrBuf.Length()-8)); myErrRes = nResult; } else { myErrState = "00000"; myErrBuf = "(00000) No error."; myErrRes = nResult; } if (bFree) { SQLFreeHandle(myType, myHandle); myHandle = 0; } return false; } return true; }
void EvalLcase(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 0) { CStr tmp = (*args)[0]; if (tmp) out->PutS(strlwr(tmp.GetBuffer()), tmp.Length()); } }
void ProtoParseHosts(CStrAry &hosts, CStr &hostStr) { char *p = hostStr.GetBuffer(); p = strtok(p, ";\n"); while (p) { if (*p && strchr(p, '.')) { hosts.Add(p); } p = strtok(NULL, ";\n"); } }
CStr EVP_decrypt(CStr passw, CStr strin, const char *cipher) { int len = strin.Length(); if (len > 0 && passw.Length() > 0) { strin.Grow(len + EVP_MAX_IV_LENGTH); return strin.Grow( EVP_decrypt(passw.SafeP(), passw.Length(), strin.GetBuffer(), len, cipher) ); } else return strin; }
CDbConn CDbPool::Connect(char *dsn) { CDbConn conn; if (!myConnMap.Find(dsn,conn)) { CStr tmp = dsn; conn = myLib->Connect(tmp.GetBuffer()); conn.BeginTrans(); myConnMap.Set(dsn,conn); } return conn; }
void EvalDirRemove(const void *mode, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("rmdir", 1, 1); CStr path = (*args)[0]; if (path) { bslash(path.GetBuffer()); #ifdef WIN32 _rmdir(path); #else rmdir(path); #endif } }
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'); }
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 qObjHCtx::HEnum(qCtx *ctx, qStr *out, qArgAry *args) { // require body argument if (args->Count() < 1) return; // get context CStr var = (*args)[0]; // read filter int filter = 0; if (args->Count() > 2) { CStr tmp = (*args)[2]; strlwr(tmp.GetBuffer()); if (strchr((const char*)tmp, 'v')) filter |= HENUM_VALUES; if (strchr((const char*)tmp, 'k')) filter |= HENUM_KEYS; if (strchr((const char*)tmp, 't')) filter |= HENUM_TREE; } else filter = HENUM_KEYS | HENUM_VALUES; // loop through objects in my map qCtxTmp tmpCtx(ctx); LOOPCTX loop; loop.body = args->GetAt(1); loop.n = 0; loop.ctx = &tmpCtx; loop.out = out; loop.ctx->MapObj(&loop.key, "key"); loop.ctx->MapObj(&loop.val, "value"); ctx->MapObj(&loop.n, "num"); myHash.Enum(&loop, var, filter, HEnumLoop); return; }
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 EvalSqlQ(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { CStr val = (*args)[0]; char *p = val.GetBuffer(); CStr quo(val.Length() * 2 + 2); char *v = quo.GetBuffer(); *v++ = '\''; if (p) { while (*p) { if (*p == '\'') *v++ = '\''; *v++ = *p; ++p; } } *v++ = '\''; quo.Grow(v-(const char *)quo); out->PutS(quo); }
void EvalFilePath(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("filepath", 1, 1); CStr path = (*args)[0]; char *b = path.GetBuffer(); if (b) { char *r = b + path.Length() - 1; while (r >= b && !(ISPATHSEP(*r))) { --r; } if (r >= b) { *r = DIRSEP; ++r; *r = '\0'; path.Grow(r - b); out->PutS(path); } } }
void qCtxComp::OutFunc(qStr *out, CStr &name, qArgAry *ary) { int i, j; char *p; C_BASE b; b.bc = BC_FUNC; b.rn = myRn ? rand()%256 : 0; b.ln = name.Length(); out->PutS((char *)&b, sizeof(b)); p = name.GetBuffer(); if (b.rn) { for (j = 0; j < b.ln; ++j) p[j] = p[j] ^ b.rn; } out->PutS(name); C_ARGS v; v.cnt = ary->Count(); out->PutS((char *)&v, sizeof(v)); C_ARG a; for (i = 0; i < ary->Count(); ++i){ a.ln = ary->GetAt(i).Length(); a.at = ary->GetQuot(i); out->PutS((char *)&a, sizeof(a)); p = ary->GetAt(i).GetBuffer(); if (b.rn) { for (j = 0; j < (int) a.ln; ++j) p[j] = p[j] ^ b.rn; } out->PutS(ary->GetAt(i)); } }
void qObjProto::EvalSmtpMail(qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() >= 4) { qMailOpts qmop; qmop.host = (*args)[0]; qmop.smtp = qmop.host; qmop.user = (*args)[1].GetBuffer(); qmop.from = (*args)[2]; qmop.subj = (*args)[4]; qmop.subj = ReplaceStr(qmop.subj, "\n", ""); qmop.subj = ReplaceStr(qmop.subj, "\r", ""); qmop.from = ReplaceStr(qmop.from, "\n", ""); qmop.from = ReplaceStr(qmop.from, "\r", ""); qmop.host = ReplaceStr(qmop.host, "\n", ""); qmop.host = ReplaceStr(qmop.host, "\r", ""); qmop.body.Add("'" << (*args)[5]); if (qsUnreg) qmop.unreg = true; int i = 6; while (args->GetAt(i)) { qmop.body.Add((*args)[i++]); } // FIX QMAIL LACK OF MULTIPLE -TO- HEADERS! CStr rcpt = (*args)[3]; ReplaceStr(rcpt, "\n", ""); ReplaceStr(rcpt, "\r", ""); char * rx = rcpt.GetBuffer(); char * tok = rx; char * p = strchr(tok, ';'); if (p) *p = '\0'; while (tok) { while (isspace(*tok)) ++tok; if (*tok) { qmop.rcpt = tok; qmop.to = tok; try { int errVal = qsmtp(&qmop); if (errVal) ctx->ThrowF(out, errVal+600, "Mail error #%d.", errVal); } catch (CEx ex) { ctx->Throw(out, ex.id+600, ex.msg); } } if (p) { tok = p + 1; p = strchr(tok, ';'); if (p) *p = '\0'; } else tok = NULL; } } else { ctx->Throw(out, 655, "USAGE: %smtp-mail(host, user, from, to, subj, body...)"); } return; }
void RunCompiled(qCtx *ctx, qStr *in, qStr *out) { int i, j; char *p; C_BASE b; while (in->GetS((char *) &b, sizeof(b)) == sizeof(b) && b.ln > 0) { CStr dt(b.ln); if (in->GetS(dt.GetBuffer(), b.ln) == b.ln) { if (b.bc == BC_FUNC) { p = dt.GetBuffer(); if (b.rn) { for (j = 0; j < b.ln; ++j) p[j] = p[j] ^ b.rn; } qObj *obj; if (ctx->Find(&obj, (const CStr &) dt)) { C_ARGS v; C_ARG a; CStr cur; qArgAry ary; char qmode; if (in->GetS((char *) &v, sizeof(v)) == sizeof(v)) { char *map = obj->GetQmap(); if (!map || *map == 'A') { qmode = !map ? '0' : '1'; for (i = 0; i < v.cnt; ++i) { { if (in->GetS((char *) &a, sizeof(a)) == sizeof(a)) { cur.Grow(a.ln); if (in->GetS(cur.GetBuffer(), a.ln) == (int) a.ln) { p = cur.GetBuffer(); if (b.rn) { for (j = 0; j < (int) a.ln; ++j) p[j] = p[j] ^ b.rn; } ary.Add(cur); ary.SetQuot(i, a.at == ARG_QSTR); if (!ary.GetQuot(i) && qmode == '0') { qStrBuf tmp; if (a.at == ARG_CMP) { qStrReadBuf rTmp(ary[i]); RunCompiled(ctx, &rTmp, &tmp); } else { qStrReadBuf rTmp(ary[i]); ctx->Parse(&rTmp, &tmp); } ary[i] = tmp; } else { if (a.at == ARG_CMP) { if (qmode != '2') { qStrBuf tmp; qStrReadBuf rTmp(ary[i]); Decompile(&rTmp, &tmp); ary[i] = tmp; } else { ary.SetQuot(i, ARG_CMP); } } } } } } } } else { qmode = (*map == '1' ? '1' : '0'); ++map; for (i = 0; i < v.cnt; ++i) { { if (in->GetS((char *) &a, sizeof(a)) == sizeof(a)) { cur.Grow(a.ln); if (in->GetS(cur.GetBuffer(), a.ln) == (int) a.ln) { p = cur.GetBuffer(); if (b.rn) { for (j = 0; j < (int) a.ln; ++j) p[j] = p[j] ^ b.rn; } ary.Add(cur); ary.SetQuot(i, a.at == ARG_QSTR); if (!ary.GetQuot(i) && qmode == '0') { qStrBuf tmp; if (a.at == ARG_CMP) { qStrReadBuf rTmp(ary[i]); RunCompiled(ctx, &rTmp, &tmp); } else { qStrReadBuf rTmp(ary[i]); ctx->Parse(&rTmp, &tmp); } ary[i] = tmp; } else { if (a.at == ARG_CMP) { if (qmode != '2') { qStrBuf tmp; qStrReadBuf rTmp(ary[i]); Decompile(&rTmp, &tmp); ary[i] = tmp; } else { ary.SetQuot(i, ARG_CMP); } } } } } } if (*map) { if (*map != 'A') { qmode = *map; ++map; } } else { qmode = '0'; } } } obj->Eval(ctx, out, v.cnt ? &ary : NULL); } } else { if (ctx->GetStrict()) { ctx->ThrowF(out, 98, "Function '%s' was not found.", (const char *) dt); } else { C_ARGS v; C_ARG a; CStr cur; if (in->GetS((char *) &v, sizeof(v)) == sizeof(v)) { for (i = 0; i < v.cnt; ++i) { if (in->GetS((char *) &a, sizeof(a)) == sizeof(a)) { cur.Grow(a.ln); if (in->GetS(cur.GetBuffer(), a.ln) != (int) a.ln) { break; } } } } out->PutC('%'); out->PutS(dt); if (v.cnt > 0) { out->PutC(T_LP); out->PutS("..."); out->PutC(T_RP); } else { out->PutC('%'); } } } } else if (b.bc == BC_OUT) { out->PutS(dt, b.ln); } } } }
void Decompile(qStr *in, qStr *out) { int i, j; char *p; C_BASE b; while (in->GetS((char *) &b, sizeof(b)) == sizeof(b)) { CStr dt(b.ln); if (in->GetS(dt.GetBuffer(), b.ln) == b.ln) { if (b.bc == BC_FUNC) { C_ARGS v; C_ARG a; CStr cur; p = dt.GetBuffer(); if (b.rn) { for (j = 0; j < b.ln; ++j) p[j] = p[j] ^ b.rn; } out->PutC('%'); out->PutS(dt); if (in->GetS((char *) &v, sizeof(v)) == sizeof(v)) { if (v.cnt > 0) out->PutC('('); else out->PutC('%'); for (i = 0; i < v.cnt; ++i) { if (in->GetS((char *) &a, sizeof(a)) == sizeof(a)) { cur.Grow(a.ln); if (in->GetS(cur.GetBuffer(), a.ln) == (int) a.ln) { p = cur.GetBuffer(); if (b.rn) { for (j = 0; j < (int) a.ln; ++j) p[j] = p[j] ^ b.rn; } if (a.at == ARG_CMP) { qStrReadBuf rTmp(cur); Decompile(&rTmp, out); } else { if (a.at == ARG_QSTR) out->PutC('\''); out->PutS(cur); } if (i < (v.cnt-1)) { out->PutC(','); } } } } if (v.cnt > 0) out->PutC(')'); } } else if (b.bc == BC_OUT) { out->PutS(dt, b.ln); } } } }
void EvalEnumSort(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { if (args->Count() > 2) { CStr datax = (*args)[0];; CStr body = args->GetAt(2).GetBuffer(); if (!body) return; char * data = datax.GetBuffer(); CStr delim = "\n\r|,;"; CStr alg; if (args->Count() > 1 && args->GetAt(1)) delim = (*args)[1];; if (args->Count() > 3) alg = args->GetAt(3); int num = 0; WNAryX *ary = NULL; char *p; WNCompX compx; if ((p = strtok(data, delim))) { int len; do { len = strlen(p); ary = (WNAryX*) realloc(ary, ++num * sizeof(WNAryX)); memset(&(ary[num-1]), 0, sizeof(WNAryX)); new(&ary[num-1]) WNAryX; ary[num-1].v = p; ary[num-1].c=&compx; } while ((p = strtok(NULL, delim))); } if (!alg.IsEmpty()) { qCtxTmp tmpCtx(ctx); qStrBuf tmpOut; compx.alg=alg; compx.out=&tmpOut; compx.ctx=&tmpCtx; tmpCtx.MapObj(&compx.a, "a"); tmpCtx.MapObj(&compx.b, "b"); qsort(ary, num, sizeof(WNAryX), EvalWNCom); } else qsort(ary, num, sizeof(WNAryX), EvalWNComSimple); const char *curw; qCtxTmp tmpCtx(ctx); tmpCtx.MapObj(&curw, "token"); bool ok = true; tmpCtx.MapObj(&ok, (QOBJFUNC) EvalBreak, "break"); int i; for (i = 0; ok && i < num; ++i) { curw = ary[i].v; tmpCtx.Parse(body, out); ary[i].~WNAryX(); } free(ary); } }