Merge pull request #16539 from gemu2015/scripter_update

nested loops etc
This commit is contained in:
Jason2866 2022-09-16 13:13:08 +02:00 committed by GitHub
commit 5eb43d21b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -80,7 +80,7 @@ uint32_t EncodeLightId(uint8_t relay_id);
uint32_t DecodeLightId(uint32_t hue_id); uint32_t DecodeLightId(uint32_t hue_id);
char *web_send_line(char mc, char *lp); char *web_send_line(char mc, char *lp);
int32_t web_send_file(char mc, char *file); int32_t web_send_file(char mc, char *file);
char *Get_esc_char(char *cp, char *esc_chr);
#define SPECIAL_EEPMODE_SIZE 6200 #define SPECIAL_EEPMODE_SIZE 6200
#ifndef STASK_STACK #ifndef STASK_STACK
@ -438,6 +438,7 @@ struct SCRIPT_MEM {
void *script_mem; void *script_mem;
uint16_t script_mem_size; uint16_t script_mem_size;
uint8_t script_dprec; uint8_t script_dprec;
char script_sepc;
uint8_t script_lzero; uint8_t script_lzero;
uint8_t var_not_found; uint8_t var_not_found;
uint8_t glob_error; uint8_t glob_error;
@ -499,7 +500,7 @@ void flt2char(float num, char *nbuff) {
dtostrfd(num, glob_script_mem.script_dprec, nbuff); dtostrfd(num, glob_script_mem.script_dprec, nbuff);
} }
// convert float to char with leading zeros // convert float to char with leading zeros
void f2char(float num, uint32_t dprec, uint32_t lzeros, char *nbuff) { void f2char(float num, uint32_t dprec, uint32_t lzeros, char *nbuff, char dsep) {
dtostrfd(num, dprec, nbuff); dtostrfd(num, dprec, nbuff);
if (lzeros > 1) { if (lzeros > 1) {
// check leading zeros // check leading zeros
@ -522,6 +523,13 @@ void f2char(float num, uint32_t dprec, uint32_t lzeros, char *nbuff) {
strcpy(nbuff,cpbuf); strcpy(nbuff,cpbuf);
} }
} }
if (dsep != '.') {
for (uint16_t cnt = 0; cnt < strlen(nbuff); cnt++) {
if (nbuff[cnt] == '.') {
nbuff[cnt] = dsep;
}
}
}
} }
@ -789,9 +797,12 @@ char *script;
// string vars // string vars
op++; op++;
*snp_p ++= strings_p; *snp_p ++= strings_p;
while (*op!='\"') { while (*op != '\"') {
if (*op==SCRIPT_EOL) break; if (*op == SCRIPT_EOL) break;
*strings_p++ = *op++; char iob;
op = Get_esc_char(op, &iob);
//*strings_p++ = *op++;
*strings_p++ = iob;
} }
*strings_p++ = 0; *strings_p++ = 0;
vtypes[vars].bits.is_string = 1; vtypes[vars].bits.is_string = 1;
@ -970,6 +981,7 @@ char *script;
glob_script_mem.numvars = vars; glob_script_mem.numvars = vars;
glob_script_mem.script_dprec = SCRIPT_FLOAT_PRECISION; glob_script_mem.script_dprec = SCRIPT_FLOAT_PRECISION;
glob_script_mem.script_lzero = 0; glob_script_mem.script_lzero = 0;
glob_script_mem.script_sepc = '.';
glob_script_mem.script_loglevel = LOG_LEVEL_INFO; glob_script_mem.script_loglevel = LOG_LEVEL_INFO;
@ -980,7 +992,7 @@ char *script;
} else { } else {
char string[32]; char string[32];
f2char(glob_script_mem.fvars[dvtp[count].index], glob_script_mem.script_dprec, glob_script_mem.script_lzero, string); f2char(glob_script_mem.fvars[dvtp[count].index], glob_script_mem.script_dprec, glob_script_mem.script_lzero, string, '.');
toLog(string); toLog(string);
} }
} }
@ -1997,6 +2009,33 @@ char *isargs(char *lp, uint32_t isind) {
return lp; return lp;
} }
char *Get_esc_char(char *cp, char *esc_chr) {
char iob = *cp;
if (iob == '\\') {
cp++;
if (*cp == 't') {
iob = '\t';
} else if (*cp == 'n') {
iob = '\n';
} else if (*cp == 'r') {
iob = '\r';
} else if (*cp == '0' && *(cp + 1) == 'x') {
cp += 2;
iob = strtol(cp, 0, 16);
cp++;
} else if (*cp == '\\') {
iob = '\\';
}
else {
cp--;
}
}
*esc_chr = iob;
cp++;
return cp;
}
char *isget(char *lp, char *sp, uint32_t isind, struct GVARS *gv) { char *isget(char *lp, char *sp, uint32_t isind, struct GVARS *gv) {
float fvar; float fvar;
lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0);
@ -2061,29 +2100,9 @@ char *isvar(char *lp, uint8_t *vtype, struct T_INDEX *tind, float *fp, char *sp,
lp++; lp++;
while (*lp != '"') { while (*lp != '"') {
if (*lp == 0 || *lp == SCRIPT_EOL) break; if (*lp == 0 || *lp == SCRIPT_EOL) break;
uint8_t iob = *lp; char iob;
if (iob == '\\') { lp = Get_esc_char(lp, &iob);
lp++; if (sp) *sp++ = iob;
if (*lp == 't') {
iob = '\t';
} else if (*lp == 'n') {
iob = '\n';
} else if (*lp == 'r') {
iob = '\r';
} else if (*lp == '0' && *(lp+1) == 'x') {
lp += 2;
iob = strtol(lp, 0, 16);
lp++;
} else if (*lp == '\\') {
iob = '\\';
} else {
lp--;
}
if (sp) *sp++ = iob;
} else {
if (sp) *sp++ = iob;
}
lp++;
} }
if (sp) *sp = 0; if (sp) *sp = 0;
*vtype = STR_RES; *vtype = STR_RES;
@ -2569,6 +2588,10 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, gv); lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, gv);
while (*lp==' ') lp++; while (*lp==' ') lp++;
glob_script_mem.script_lzero = fvar; glob_script_mem.script_lzero = fvar;
if (*lp == ',' || *lp == '.') {
glob_script_mem.script_sepc = *lp;
lp++;
}
lp = GetNumericArgument(lp , OPER_EQU, &fvar, gv); lp = GetNumericArgument(lp , OPER_EQU, &fvar, gv);
while (*lp==' ') lp++; while (*lp==' ') lp++;
glob_script_mem.script_dprec = fvar; glob_script_mem.script_dprec = fvar;
@ -3979,7 +4002,13 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
lp = GetStringArgument(lp + 3, OPER_EQU, str, 0); lp = GetStringArgument(lp + 3, OPER_EQU, str, 0);
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
char token[2]; char token[2];
token[0] = *lp++; if (*lp == '\'') {
lp++;
lp = Get_esc_char(lp, token);
lp++;
} else {
token[0] = *lp++;
}
token[1] = 0; token[1] = 0;
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
lp = GetNumericArgument(lp, OPER_EQU, &fvar, gv); lp = GetNumericArgument(lp, OPER_EQU, &fvar, gv);
@ -4033,8 +4062,10 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
lp += 2; lp += 2;
uint8_t dprec = glob_script_mem.script_dprec; uint8_t dprec = glob_script_mem.script_dprec;
uint8_t lzero = glob_script_mem.script_lzero; uint8_t lzero = glob_script_mem.script_lzero;
char dsep = glob_script_mem.script_sepc;
if (isdigit(*lp)) { if (isdigit(*lp)) {
if (*(lp + 1) == '.') { if (*(lp + 1) == '.' || *(lp + 1) == ',') {
dsep = *(lp + 1);
lzero = *lp & 0xf; lzero = *lp & 0xf;
lp += 2; lp += 2;
dprec = *lp & 0xf; dprec = *lp & 0xf;
@ -4046,7 +4077,7 @@ extern void W8960_SetGain(uint8_t sel, uint16_t value);
} }
lp = GetNumericArgument(lp, OPER_EQU, &fvar, gv); lp = GetNumericArgument(lp, OPER_EQU, &fvar, gv);
char str[SCRIPT_MAXSSIZE]; char str[SCRIPT_MAXSSIZE];
f2char(fvar, dprec, lzero, str); f2char(fvar, dprec, lzero, str, dsep);
if (sp) strlcpy(sp, str, glob_script_mem.max_ssize); if (sp) strlcpy(sp, str, glob_script_mem.max_ssize);
lp++; lp++;
len = 0; len = 0;
@ -4853,6 +4884,16 @@ extern char *SML_GetSVal(uint32_t index);
goto nfuncexit; goto nfuncexit;
} }
#endif #endif
if (!strncmp(lp, "tc(", 3)) {
lp = GetNumericArgument(lp + 3, OPER_EQU, &fvar, gv);
lp++;
if (sp) {
sp[0] = fvar;
sp[1] = 0;
}
len = 0;
goto strexit;
}
break; break;
case 'u': case 'u':
if (!strncmp(vname, "uptime", 6)) { if (!strncmp(vname, "uptime", 6)) {
@ -5446,6 +5487,7 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
uint16_t count; uint16_t count;
uint8_t vtype; uint8_t vtype;
uint8_t dprec = glob_script_mem.script_dprec; uint8_t dprec = glob_script_mem.script_dprec;
char dsep = glob_script_mem.script_sepc;
uint8_t lzero = glob_script_mem.script_lzero; uint8_t lzero = glob_script_mem.script_lzero;
float fvar; float fvar;
cp = srcbuf; cp = srcbuf;
@ -5460,7 +5502,8 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
dstbuf[count] = *cp++; dstbuf[count] = *cp++;
} else { } else {
if (isdigit(*cp)) { if (isdigit(*cp)) {
if (*(cp+1) == '.') { if (*(cp + 1) == '.' || *(cp + 1) == ',') {
dsep = *(cp + 1);
lzero = *cp & 0xf; lzero = *cp & 0xf;
cp+=2; cp+=2;
} }
@ -5469,6 +5512,7 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
} else { } else {
dprec = glob_script_mem.script_dprec; dprec = glob_script_mem.script_dprec;
lzero = glob_script_mem.script_lzero; lzero = glob_script_mem.script_lzero;
dsep = glob_script_mem.script_sepc;
} }
if (*cp=='(') { if (*cp=='(') {
// math expression // math expression
@ -5480,7 +5524,7 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
glob_script_mem.glob_error = 0; glob_script_mem.glob_error = 0;
cp = GetStringArgument(slp, OPER_EQU, string, 0); cp = GetStringArgument(slp, OPER_EQU, string, 0);
} else { } else {
f2char(fvar, dprec, lzero, string); f2char(fvar, dprec, lzero, string, dsep);
} }
uint8_t slen = strlen(string); uint8_t slen = strlen(string);
if (count + slen < dstsize - 1) { if (count + slen < dstsize - 1) {
@ -5494,7 +5538,7 @@ void Replace_Cmd_Vars(char *srcbuf, uint32_t srcsize, char *dstbuf, uint32_t dst
// found variable as result // found variable as result
if (vtype==NUM_RES || (vtype&STYPE)==0) { if (vtype==NUM_RES || (vtype&STYPE)==0) {
// numeric result // numeric result
f2char(fvar, dprec, lzero, string); f2char(fvar, dprec, lzero, string, dsep);
} else { } else {
// string result // string result
} }
@ -5871,9 +5915,15 @@ int16_t retval;
return retval; return retval;
} }
#define SCRIPT_LOOP_NEST 3
int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) { int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
uint8_t vtype = 0, sindex, xflg, floop = 0, globvindex, fromscriptcmd = 0; uint8_t vtype = 0, sindex, xflg, globvindex, fromscriptcmd = 0;
char *lp_next; // 22 bytes per nested loop
uint8_t floop[SCRIPT_LOOP_NEST] = {0, 0, 0};
int8_t loopdepth = -1;
char *lp_next[SCRIPT_LOOP_NEST];
char *cv_ptr[SCRIPT_LOOP_NEST];
float *cv_count[SCRIPT_LOOP_NEST], cv_max[SCRIPT_LOOP_NEST], cv_inc[SCRIPT_LOOP_NEST];
int16_t globaindex, saindex; int16_t globaindex, saindex;
struct T_INDEX ind; struct T_INDEX ind;
uint8_t operand, lastop, numeric = 1, if_state[IF_NEST], if_exe[IF_NEST], if_result[IF_NEST], and_or, ifstck = 0; uint8_t operand, lastop, numeric = 1, if_state[IF_NEST], if_exe[IF_NEST], if_result[IF_NEST], and_or, ifstck = 0;
@ -5881,8 +5931,8 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
if_result[ifstck] = 0; if_result[ifstck] = 0;
if_exe[ifstck] = 1; if_exe[ifstck] = 1;
char cmpstr[SCRIPT_MAXSSIZE]; char cmpstr[SCRIPT_MAXSSIZE];
float *dfvar, *cv_count, cv_max, cv_inc; float *dfvar;
char *cv_ptr;
float fvar = 0, fvar1, sysvar, swvar; float fvar = 0, fvar1, sysvar, swvar;
uint8_t section = 0, sysv_type = 0, swflg = 0; uint8_t section = 0, sysv_type = 0, swflg = 0;
@ -5895,7 +5945,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
} }
uint8_t check = 0; uint8_t check = 0;
if (tlen<0) { if (tlen < 0) {
tlen = abs(tlen); tlen = abs(tlen);
check = 1; check = 1;
} }
@ -5903,22 +5953,23 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
while (1) { while (1) {
// check line // check line
// skip leading spaces // skip leading spaces and tabs
startline: startline:
SCRIPT_SKIP_SPACES while (*lp == '\t' || *lp == ' ') {
while (*lp == '\t') {lp++;} lp++;
}
// skip empty line // skip empty line
SCRIPT_SKIP_EOL SCRIPT_SKIP_EOL
// skip comment // skip comment
if (*lp==';') goto next_line; if (*lp == ';') goto next_line;
if (!*lp) break; if (!*lp) break;
if (section) { if (section) {
// we are in section // we are in section
if (*lp=='>') { if (*lp == '>') {
return 0; return 0;
} }
if (*lp=='#') { if (*lp == '#') {
return 0; return 0;
} }
glob_script_mem.var_not_found = 0; glob_script_mem.var_not_found = 0;
@ -5935,59 +5986,59 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
if (!strncmp(lp, "if", 2)) { if (!strncmp(lp, "if", 2)) {
lp += 2; lp += 2;
if (ifstck<IF_NEST-1) ifstck++; if (ifstck < IF_NEST - 1) ifstck++;
if_state[ifstck] = 1; if_state[ifstck] = 1;
if_result[ifstck] = 0; if_result[ifstck] = 0;
if (ifstck==1) if_exe[ifstck] = 1; if (ifstck == 1) if_exe[ifstck] = 1;
else if_exe[ifstck] = if_exe[ifstck - 1]; else if_exe[ifstck] = if_exe[ifstck - 1];
and_or = 0; and_or = 0;
} else if (!strncmp(lp, "then", 4) && if_state[ifstck]==1) { } else if (!strncmp(lp, "then", 4) && if_state[ifstck] == 1) {
lp += 4; lp += 4;
if_state[ifstck] = 2; if_state[ifstck] = 2;
if (if_exe[ifstck - 1]) if_exe[ifstck] = if_result[ifstck]; if (if_exe[ifstck - 1]) if_exe[ifstck] = if_result[ifstck];
} else if (!strncmp(lp, "else", 4) && if_state[ifstck]==2) { } else if (!strncmp(lp, "else", 4) && if_state[ifstck] == 2) {
lp += 4; lp += 4;
if_state[ifstck] = 3; if_state[ifstck] = 3;
if (if_exe[ifstck - 1]) if_exe[ifstck] = !if_result[ifstck]; if (if_exe[ifstck - 1]) if_exe[ifstck] = !if_result[ifstck];
} else if (!strncmp(lp, "endif", 5) && if_state[ifstck]>=2) { } else if (!strncmp(lp, "endif", 5) && if_state[ifstck] >= 2) {
lp += 5; lp += 5;
if (ifstck>0) { if (ifstck>0) {
if_state[ifstck] = 0; if_state[ifstck] = 0;
ifstck--; ifstck--;
} }
goto next_line; goto next_line;
} else if (!strncmp(lp, "or", 2) && if_state[ifstck]==1) { } else if (!strncmp(lp, "or", 2) && if_state[ifstck] == 1) {
lp += 2; lp += 2;
and_or = 1; and_or = 1;
} else if (!strncmp(lp, "and", 3) && if_state[ifstck]==1) { } else if (!strncmp(lp, "and", 3) && if_state[ifstck] == 1) {
lp += 3; lp += 3;
and_or = 2; and_or = 2;
} }
if (*lp=='{' && if_state[ifstck]==1) { if (*lp == '{' && if_state[ifstck] == 1) {
lp += 1; // then lp += 1; // then
if_state[ifstck] = 2; if_state[ifstck] = 2;
if (if_exe[ifstck - 1]) if_exe[ifstck]=if_result[ifstck]; if (if_exe[ifstck - 1]) if_exe[ifstck] = if_result[ifstck];
} else if (*lp=='{' && if_state[ifstck]==3) { } else if (*lp == '{' && if_state[ifstck] == 3) {
lp += 1; // after else lp += 1; // after else
//if_state[ifstck]=3; //if_state[ifstck]=3;
} else if (*lp=='}' && if_state[ifstck]>=2) { } else if (*lp == '}' && if_state[ifstck] >= 2) {
lp++; // must check for else lp++; // must check for else
char *slp = lp; char *slp = lp;
uint8_t iselse = 0; uint8_t iselse = 0;
for (uint8_t count = 0; count<8;count++) { for (uint8_t count = 0; count < 8; count++) {
if (*lp=='}') { if (*lp == '}') {
// must be endif // must be endif
break; break;
} }
if (!strncmp(lp, "else", 4)) { if (!strncmp(lp, "else", 4)) {
// is before else, no endif // is before else, no endif
if_state[ifstck] = 3; if_state[ifstck] = 3;
if (if_exe[ifstck-1]) if_exe[ifstck]=!if_result[ifstck]; if (if_exe[ifstck - 1]) if_exe[ifstck] =! if_result[ifstck];
lp += 4; lp += 4;
iselse = 1; iselse = 1;
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
if (*lp=='{') lp++; if (*lp == '{') lp++;
break; break;
} }
lp++; lp++;
@ -5995,7 +6046,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
if (!iselse) { if (!iselse) {
lp = slp; lp = slp;
// endif // endif
if (ifstck>0) { if (ifstck > 0) {
if_state[ifstck] = 0; if_state[ifstck] = 0;
ifstck--; ifstck--;
} }
@ -6008,28 +6059,29 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
// simple implementation, zero loop count not supported // simple implementation, zero loop count not supported
lp += 3; lp += 3;
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
lp_next = 0; loopdepth++;
if (loopdepth >= SCRIPT_LOOP_NEST) {
loopdepth = SCRIPT_LOOP_NEST - 1;
}
lp_next[loopdepth] = 0;
lp = isvar(lp, &vtype, &ind, 0, 0, gv); lp = isvar(lp, &vtype, &ind, 0, 0, gv);
if ((vtype!=VAR_NV) && (vtype&STYPE)==0) { if ((vtype != VAR_NV) && (vtype & STYPE) == 0) {
// numeric var // numeric var
uint8_t index = glob_script_mem.type[ind.index].index; uint8_t index = glob_script_mem.type[ind.index].index;
cv_count = &glob_script_mem.fvars[index]; cv_count[loopdepth] = &glob_script_mem.fvars[index];
SCRIPT_SKIP_SPACES lp = GetNumericArgument(lp, OPER_EQU, cv_count[loopdepth], 0);
lp = GetNumericArgument(lp, OPER_EQU, cv_count, 0); lp = GetNumericArgument(lp, OPER_EQU, &cv_max[loopdepth], 0);
SCRIPT_SKIP_SPACES lp = GetNumericArgument(lp, OPER_EQU, &cv_inc[loopdepth], 0);
lp = GetNumericArgument(lp, OPER_EQU, &cv_max, 0);
SCRIPT_SKIP_SPACES
lp = GetNumericArgument(lp, OPER_EQU, &cv_inc, 0);
//SCRIPT_SKIP_EOL //SCRIPT_SKIP_EOL
cv_ptr = lp; cv_ptr[loopdepth] = lp;
if (*cv_count<=cv_max && cv_inc>0) { if (*cv_count[loopdepth] <= cv_max[loopdepth] && cv_inc[loopdepth] > 0) {
// inc loop // inc loop
floop = 1; floop[loopdepth] = 1;
} else { } else {
// dec loop // dec loop
floop = 2; floop[loopdepth] = 2;
if (cv_inc>0) { if (cv_inc[loopdepth] > 0) {
floop = 1; floop[loopdepth] = 1;
} }
} }
} else { } else {
@ -6037,23 +6089,40 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
toLogEOL("for error", lp); toLogEOL("for error", lp);
} }
} else if (!strncmp(lp, "next", 4)) { } else if (!strncmp(lp, "next", 4)) {
lp_next = lp; getnext:
if (floop>0) { if (loopdepth >= 0) {
// for next loop lp_next[loopdepth] = lp;
*cv_count += cv_inc; if (floop[loopdepth] > 0) {
if (floop==1) { // for next loop
if (*cv_count<=cv_max) { *cv_count[loopdepth] += cv_inc[loopdepth];
lp = cv_ptr; if (floop[loopdepth] == 1) {
if (*cv_count[loopdepth] <= cv_max[loopdepth]) {
lp = cv_ptr[loopdepth];
} else {
lp += 4;
floop[loopdepth] = 0;
loopdepth--;
if (loopdepth < -1) {
loopdepth = -1;
}
}
} else { } else {
lp += 4; if (*cv_count[loopdepth] >= cv_max[loopdepth]) {
floop = 0; lp = cv_ptr[loopdepth];
} else {
lp += 4;
floop[loopdepth] = 0;
loopdepth--;
if (loopdepth < -1) {
loopdepth = -1;
}
}
} }
} else { } else {
if (*cv_count>=cv_max) { lp += 4;
lp = cv_ptr; loopdepth--;
} else { if (loopdepth < -1) {
lp += 4; loopdepth = -1;
floop = 0;
} }
} }
} }
@ -6064,7 +6133,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
char *slp = lp; char *slp = lp;
lp = GetNumericArgument(lp, OPER_EQU, &swvar, 0); lp = GetNumericArgument(lp, OPER_EQU, &swvar, 0);
if (glob_script_mem.glob_error==1) { if (glob_script_mem.glob_error == 1) {
// was string, not number // was string, not number
lp = slp; lp = slp;
// get the string // get the string
@ -6079,7 +6148,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
float cvar; float cvar;
if (!(swflg & 0x80)) { if (!(swflg & 0x80)) {
lp = GetNumericArgument(lp, OPER_EQU, &cvar, 0); lp = GetNumericArgument(lp, OPER_EQU, &cvar, 0);
if (swvar!=cvar) { if (swvar != cvar) {
swflg = 2; swflg = 2;
} else { } else {
swflg = 1; swflg = 1;
@ -6093,20 +6162,20 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
swflg = 0x82; swflg = 0x82;
} }
} }
} else if (!strncmp(lp, "ends", 4) && swflg>0) { } else if (!strncmp(lp, "ends", 4) && swflg > 0) {
lp += 4; lp += 4;
swflg = 0; swflg = 0;
} }
if ((swflg & 3)==2) goto next_line; if ((swflg & 3) == 2) goto next_line;
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
//SCRIPT_SKIP_EOL //SCRIPT_SKIP_EOL
if (*lp==SCRIPT_EOL) { if (*lp == SCRIPT_EOL) {
goto next_line; goto next_line;
} }
//toLogN(lp,16); //toLogN(lp,16);
if (!if_exe[ifstck] && if_state[ifstck]!=1) goto next_line; if (!if_exe[ifstck] && if_state[ifstck] != 1) goto next_line;
#ifdef IFTHEN_DEBUG #ifdef IFTHEN_DEBUG
sprintf(tbuff, "stack=%d,exe=%d,state=%d,cmpres=%d execute line: ", ifstck, if_exe[ifstck], if_state[ifstck], if_result[ifstck]); sprintf(tbuff, "stack=%d,exe=%d,state=%d,cmpres=%d execute line: ", ifstck, if_exe[ifstck], if_state[ifstck], if_result[ifstck]);
@ -6137,23 +6206,28 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
goto next_line; goto next_line;
} else if (!strncmp(lp, "break", 5)) { } else if (!strncmp(lp, "break", 5)) {
lp += 5; lp += 5;
if (floop) { if (loopdepth >= 0) {
// should break loop if (floop[loopdepth] ) {
if (lp_next) { // should break loop
lp = lp_next; if (lp_next[loopdepth]) {
lp = lp_next[loopdepth];
}
floop[loopdepth] = 0;
goto getnext;
} }
floop = 0;
} else { } else {
section = 99; section = 99;
// leave immediately // leave immediately
goto next_line;
} }
goto next_line;
} else if (!strncmp(lp, "dp", 2) && isdigit(*(lp + 2))) { } else if (!strncmp(lp, "dp", 2) && isdigit(*(lp + 2))) {
lp += 2; lp += 2;
// number precision // number precision
if (*(lp + 1)== '.') { if (*(lp + 1) == '.' || *(lp + 1) == ',' ) {
glob_script_mem.script_sepc = *(lp + 1);
glob_script_mem.script_lzero = atoi(lp); glob_script_mem.script_lzero = atoi(lp);
lp+=2; lp += 2;
} }
glob_script_mem.script_dprec = atoi(lp); glob_script_mem.script_dprec = atoi(lp);
goto next_line; goto next_line;
@ -6190,7 +6264,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
} }
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
uint8_t mode = 0; uint8_t mode = 0;
if ((*lp=='I') || (*lp=='O') || (*lp=='P')) { if ((*lp == 'I') || (*lp == 'O') || (*lp == 'P')) {
switch (*lp) { switch (*lp) {
case 'I': case 'I':
mode = 0; mode = 0;
@ -6207,10 +6281,10 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0);
mode = fvar; mode = fvar;
} }
uint8_t pm=0; uint8_t pm = 0;
if (mode==0) pm = INPUT; if (mode == 0) pm = INPUT;
if (mode==1) pm = OUTPUT; if (mode == 1) pm = OUTPUT;
if (mode==2) pm = INPUT_PULLUP; if (mode == 2) pm = INPUT_PULLUP;
pinMode(pinnr, pm); pinMode(pinnr, pm);
goto next_line; goto next_line;
} else if (!strncmp(lp, "spin(", 5)) { } else if (!strncmp(lp, "spin(", 5)) {
@ -6240,16 +6314,16 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
#ifdef USE_WS2812 #ifdef USE_WS2812
else if (!strncmp(lp, "ws2812(", 7)) { else if (!strncmp(lp, "ws2812(", 7)) {
lp = isvar(lp + 7, &vtype, &ind, 0, 0, gv); lp = isvar(lp + 7, &vtype, &ind, 0, 0, gv);
if (vtype!=VAR_NV) { if (vtype != VAR_NV) {
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
if (*lp!=')') { if (*lp != ')') {
lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0);
} else { } else {
fvar = 0; fvar = 0;
} }
// found variable as result // found variable as result
uint8_t index = glob_script_mem.type[ind.index].index; uint8_t index = glob_script_mem.type[ind.index].index;
if ((vtype&STYPE)==0) { if ((vtype & STYPE) == 0) {
// numeric result // numeric result
if (glob_script_mem.type[ind.index].bits.is_filter) { if (glob_script_mem.type[ind.index].bits.is_filter) {
uint16_t len = 0; uint16_t len = 0;
@ -6278,7 +6352,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
else if (!strncmp(lp, "pwm", 3)) { else if (!strncmp(lp, "pwm", 3)) {
lp += 3; lp += 3;
uint8_t channel = 1; uint8_t channel = 1;
if (*(lp+1)=='(') { if (*(lp+1) == '(') {
channel = *lp & 0x0f; channel = *lp & 0x0f;
#ifdef ESP8266 #ifdef ESP8266
if (channel > 5) {channel = 5;} if (channel > 5) {channel = 5;}
@ -6289,7 +6363,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
if (channel < 1) {channel = 1;} if (channel < 1) {channel = 1;}
lp += 2; lp += 2;
} else { } else {
if (*lp=='(') { if (*lp == '(') {
lp++; lp++;
} else { } else {
goto next_line; goto next_line;
@ -6298,7 +6372,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0); lp = GetNumericArgument(lp, OPER_EQU, &fvar, 0);
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
float fvar1=4000; float fvar1=4000;
if (*lp!=')') { if (*lp != ')') {
lp = GetNumericArgument(lp, OPER_EQU, &fvar1, 0); lp = GetNumericArgument(lp, OPER_EQU, &fvar1, 0);
} }
esp_pwm(fvar, fvar1, channel); esp_pwm(fvar, fvar1, channel);
@ -6340,7 +6414,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
#if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL) #if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL)
else if (!strncmp(lp, "mail", 4)) { else if (!strncmp(lp, "mail", 4)) {
lp+=5; lp += 5;
//char tmp[256]; //char tmp[256];
char *tmp = (char*)malloc(256); char *tmp = (char*)malloc(256);
if (tmp) { if (tmp) {
@ -6351,16 +6425,16 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
goto next_line; goto next_line;
} }
#endif #endif
else if (!strncmp(lp,"=>",2) || !strncmp(lp,"->",2) || !strncmp(lp,"+>",2) || !strncmp(lp,"print",5)) { else if (!strncmp(lp, "=>", 2) || !strncmp(lp, "->", 2) || !strncmp(lp, "+>", 2) || !strncmp(lp, "print", 5)) {
// execute cmd // execute cmd
uint8_t sflag = 0,pflg = 0,svmqtt,swll; uint8_t sflag = 0,pflg = 0,svmqtt,swll;
if (*lp=='p') { if (*lp == 'p') {
pflg = 1; pflg = 1;
lp += 5; lp += 5;
} }
else { else {
if (*lp=='-') sflag = 1; if (*lp == '-') sflag = 1;
if (*lp=='+') sflag = 2; if (*lp == '+') sflag = 2;
lp += 2; lp += 2;
} }
char *slp = lp; char *slp = lp;
@ -6371,7 +6445,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
uint16_t count; uint16_t count;
for (count = 0; count < glob_script_mem.cmdbuffer_size-2; count++) { for (count = 0; count < glob_script_mem.cmdbuffer_size-2; count++) {
//if (*lp=='\r' || *lp=='\n' || *lp=='}') { //if (*lp=='\r' || *lp=='\n' || *lp=='}') {
if (!*lp || *lp=='\r' || *lp=='\n') { if (!*lp || *lp == '\r' || *lp == '\n') {
cmd[count] = 0; cmd[count] = 0;
break; break;
} }
@ -6455,7 +6529,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
if (gv) globaindex = gv->numind; if (gv) globaindex = gv->numind;
else globaindex = -1; else globaindex = -1;
uint8_t index = glob_script_mem.type[ind.index].index; uint8_t index = glob_script_mem.type[ind.index].index;
if ((vtype&STYPE)==0) { if ((vtype & STYPE) == 0) {
// numeric result // numeric result
if (ind.bits.settable || ind.bits.is_filter) { if (ind.bits.settable || ind.bits.is_filter) {
dfvar = &sysvar; dfvar = &sysvar;
@ -6472,7 +6546,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
lp = getop(lp, &lastop); lp = getop(lp, &lastop);
#ifdef SCRIPT_LM_SUB #ifdef SCRIPT_LM_SUB
if (*lp=='#') { if (*lp == '#') {
// subroutine // subroutine
lp = eval_sub(lp, &fvar, 0); lp = eval_sub(lp, &fvar, 0);
} else { } else {
@ -6571,8 +6645,8 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
glob_script_mem.script_loglevel = *dfvar; glob_script_mem.script_loglevel = *dfvar;
break; break;
case SCRIPT_TELEPERIOD: case SCRIPT_TELEPERIOD:
if (*dfvar<10) *dfvar = 10; if (*dfvar < 10) *dfvar = 10;
if (*dfvar>300) *dfvar = 300; if (*dfvar > 300) *dfvar = 300;
Settings->tele_period = *dfvar; Settings->tele_period = *dfvar;
break; break;
case SCRIPT_EVENT_HANDLED: case SCRIPT_EVENT_HANDLED:
@ -6605,7 +6679,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
lp = getop(lp, &lastop); lp = getop(lp, &lastop);
#ifdef SCRIPT_LM_SUB #ifdef SCRIPT_LM_SUB
if (*lp=='#') { if (*lp == '#') {
// subroutine // subroutine
lp = eval_sub(lp, 0, str); lp = eval_sub(lp, 0, str);
} else { } else {
@ -6630,17 +6704,17 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
script_udp_sendvar(varname, 0, str); script_udp_sendvar(varname, 0, str);
} }
#endif //USE_SCRIPT_GLOBVARS #endif //USE_SCRIPT_GLOBVARS
if (saindex>=0) { if (saindex >= 0) {
if (lastop==OPER_EQU) { if (lastop == OPER_EQU) {
strlcpy(glob_script_mem.last_index_string[glob_script_mem.sind_num] + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); strlcpy(glob_script_mem.last_index_string[glob_script_mem.sind_num] + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
} else if (lastop==OPER_PLSEQU) { } else if (lastop == OPER_PLSEQU) {
strncat(glob_script_mem.last_index_string[glob_script_mem.sind_num] + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); strncat(glob_script_mem.last_index_string[glob_script_mem.sind_num] + (saindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
} }
gv->strind = -1; gv->strind = -1;
} else { } else {
if (lastop==OPER_EQU) { if (lastop == OPER_EQU) {
strlcpy(glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); strlcpy(glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
} else if (lastop==OPER_PLSEQU) { } else if (lastop == OPER_PLSEQU) {
strncat(glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize); strncat(glob_script_mem.glob_snp + (sindex * glob_script_mem.max_ssize), str, glob_script_mem.max_ssize);
} }
} }
@ -6649,7 +6723,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
} }
SCRIPT_SKIP_SPACES SCRIPT_SKIP_SPACES
if (*lp=='{' && if_state[ifstck]==3) { if (*lp == '{' && if_state[ifstck] == 3) {
lp += 1; // else lp += 1; // else
//if_state[ifstck]=3; //if_state[ifstck]=3;
} }
@ -6658,7 +6732,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
} else { } else {
//Serial.printf(">> decode %s\n",lp ); //Serial.printf(">> decode %s\n",lp );
// decode line // decode line
if (*lp=='>' && tlen==1) { if (*lp == '>' && tlen == 1) {
// called from cmdline // called from cmdline
lp++; lp++;
section = 1; section = 1;
@ -6686,7 +6760,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
numeric = 1; numeric = 1;
glob_script_mem.glob_error = 0; glob_script_mem.glob_error = 0;
argptr = GetNumericArgument((char*)ctype + 1, OPER_EQU, &fparam, 0); argptr = GetNumericArgument((char*)ctype + 1, OPER_EQU, &fparam, 0);
if (glob_script_mem.glob_error==1) { if (glob_script_mem.glob_error == 1) {
// was string, not number // was string, not number
numeric = 0; numeric = 0;
// get the string // get the string
@ -6735,7 +6809,7 @@ int16_t Run_script_sub(const char *type, int8_t tlen, struct GVARS *gv) {
// next line // next line
next_line: next_line:
if (section == 99) return 0; if (section == 99) return 0;
if (*lp==SCRIPT_EOL) { if (*lp == SCRIPT_EOL) {
lp++; lp++;
} else { } else {
lp = strchr(lp, SCRIPT_EOL); lp = strchr(lp, SCRIPT_EOL);
@ -9943,7 +10017,7 @@ exgc:
} else { } else {
fval = fp[cnt]; fval = fp[cnt];
} }
f2char(fval, glob_script_mem.script_dprec, glob_script_mem.script_lzero, acbuff); f2char(fval, glob_script_mem.script_dprec, glob_script_mem.script_lzero, acbuff, '.');
WSContentSend_P("%s", acbuff); WSContentSend_P("%s", acbuff);
if (ind < anum - 1) { WSContentSend_P(","); } if (ind < anum - 1) { WSContentSend_P(","); }
} }