1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/* ___ ___ ___ ___
** / __|/ _ \/ __/ __| secs
** \__ \ __/ (__\__ \ Copyright 1999-2004 Daniel Reed <n@ml.org>
** |___/\___|\___|___/ Simple Embedded Client Scripting
*/
#include <naim/secs.h>
void set_echof(const char *, ...);
void secs_script_init(void) {
}
char *secs_script_expand(secs_block_t *block, const char *instr) {
static char *workstr = NULL;
static signed int worklen = 0;
register const char
*str;
signed int slen;
if (instr == NULL)
return(NULL);
str = instr;
slen = 0;
if (block == NULL)
block = secs_block_getroot();
if (workstr == NULL) {
assert(worklen == 0);
workstr = secs_mem_alloc(256);
worklen = 256;
} else
assert(worklen != 0);
while (*str) {
if (*str == '$') {
secs_var_t *var = NULL;
if ((var = secs_var_find_n(NULL, str+1)) != NULL) {
char *val = *(var->val_str);
signed int vallen = strlen(val);
str += 1+strlen(var->name);
assert((worklen - slen - vallen) <= worklen);
while ((worklen - slen - vallen) < 2) {
worklen += 256;
workstr = secs_mem_realloc(workstr, worklen);
}
strncpy(workstr+slen, val, vallen);
slen += vallen;
continue;
}
}
if ((worklen - slen) < 2) {
worklen += 256;
workstr = secs_mem_realloc(workstr, worklen);
}
workstr[slen++] = *str++;
}
assert(slen < worklen);
assert((worklen - slen - 256) <= worklen);
workstr[slen] = 0;
if ((worklen - slen - 256) > 2) {
worklen -= 256;
workstr = secs_mem_realloc(workstr, worklen);
}
return(workstr);
}
typedef enum {
COMP_UNKNOWN = 0,
COMP_EQUAL,
COMP_NEQUAL,
COMP_LESS,
COMP_LEQUAL,
COMP_GREAT,
COMP_GEQUAL,
COMP_AND,
COMP_OR
} comp_t;
int secs_script_eval(char **line) {
comp_t comp = COMP_UNKNOWN;
int which = 0,
val[3] = { 0, 0, 0 };
while (**line != 0) {
while (isspace(**line))
(*line)++;
switch (**line) {
case '(':
(*line)++;
val[which++] = secs_script_eval(line);
break;
case ')':
(*line)++;
while (isspace(**line))
(*line)++;
if (which != 2) {
set_echof("secs_script_eval: Hit end of expression before done finding values to compare.\n");
return(0);
}
switch (comp) {
case COMP_EQUAL:
return(val[0] == val[1]);
case COMP_NEQUAL:
return(val[0] != val[1]);
case COMP_LESS:
return(val[0] < val[1]);
case COMP_LEQUAL:
return(val[0] <= val[1]);
case COMP_GREAT:
return(val[0] > val[1]);
case COMP_GEQUAL:
return(val[0] >= val[1]);
case COMP_AND:
return(val[0] && val[1]);
case COMP_OR:
return(val[0] || val[1]);
case COMP_UNKNOWN:
set_echof("secs_script_eval: Hit end of expression without finding operator.\n");
return(0);
}
case '$': {
secs_var_t *var = NULL;
if ((var = secs_var_find_n(NULL, *line+1)) != NULL) {
(*line) += strlen(var->name)+1;
val[which++] = *(var->val_num);
} else {
set_echof("secs_script_eval: Unknown variable: %s\n", *line);
(*line)++;
return(0);
}
break;
}
case '=':
(*line)++;
if (**line == '=') {
(*line)++;
comp = COMP_EQUAL;
} else {
set_echof("secs_script_eval: Unable to set variable values in a control block (maybe you meant ==).\n");
return(0);
}
break;
case '!':
(*line)++;
if (**line == '=') {
(*line)++;
comp = COMP_NEQUAL;
} else if (**line == '<') {
(*line)++;
comp = COMP_GEQUAL;
} else if (**line == '>') {
(*line)++;
comp = COMP_LEQUAL;
} else {
set_echof("secs_script_eval: ! must be followed by =, <, or >.\n");
return(0);
}
break;
case '<':
(*line)++;
if (**line == '=') {
(*line)++;
comp = COMP_LEQUAL;
} else
comp = COMP_LESS;
break;
case '>':
(*line)++;
if (**line == '=') {
(*line)++;
comp = COMP_GEQUAL;
} else
comp = COMP_GREAT;
break;
case '&':
(*line)++;
if (**line == '&') {
(*line)++;
comp = COMP_AND;
} else {
set_echof("secs_script_eval: Unable to perform boolean arithmetic in a control block (maybe you meant &&).\n");
return(0);
}
break;
case '|':
(*line)++;
if (**line == '|') {
(*line)++;
comp = COMP_OR;
} else {
set_echof("secs_script_eval: Unable to perform boolean arithmetic in a control block (maybe you meant ||).\n");
return(0);
}
break;
default: {
char *tmp;
val[which] = strtol(*line, &tmp, 0);
if (*line != tmp) {
*line = tmp;
which++;
} else {
set_echof("secs_script_eval: Unknown input: %s\n", *line);
return(0);
}
}
}
if (which == 3) {
set_echof("secs_script_eval: Too many values.\n");
return(0);
}
}
set_echof("secs_script_eval: error parsing line, end of string found before close parenthesis\n");
return(0);
}
int secs_script_parse(const char *line) {
char *strbuf = NULL, *str = NULL;
size_t slen = 0;
if ((line == NULL) || ((slen = strlen(line)) == 0))
return(0);
strbuf = secs_mem_alloc(slen+2);
if (strbuf == NULL)
return(0);
strncpy(strbuf, line, slen+2);
while ((str = strchr(strbuf, '\n')) != NULL)
*str = ' ';
while ((str = strrchr(strbuf, ';')) != NULL)
*str = 0;
str = strbuf;
while ((str != NULL) && (*str != 0)) {
char *workstr = str,
*word = NULL;
str += strlen(str)+1; // firstwhite() munges str, so we
// have to do this before processing
if (*workstr == '/') {
while ((word = strchr(workstr, '\001')) != NULL)
*word = ';';
secs_handle(secs_script_expand(NULL, workstr+1));
continue;
}
word = atom(workstr);
workstr = firstwhite(workstr);
if (strcasecmp(word, "ECHO") == 0)
set_echof("%s\n", secs_script_expand(NULL, workstr));
else if (strcasecmp(word, "SET") == 0) {
word = atom(workstr);
workstr = firstwhite(workstr);
secs_setvar(word, workstr);
} else if (strcasecmp(word, "IF") == 0) {
if (*workstr == '(') {
workstr++;
if (secs_script_eval(&workstr) != 0)
secs_script_parse(workstr);
} else
set_echof("secs_script_parse: You need to"
" include parenthesis around"
" control blocks for if (%s)\n",
workstr);
}
}
secs_mem_free(strbuf);
return(1);
}
|