23 speech.active=!speech.active
26 osd[type=="xxx"].active=0;osd[type=="yyy"].active=0
48 no_error=0,missing_closing_brace, missing_colon, wrong_type, illegal_number_format, illegal_character, missing_closing_bracket, invalid_type
51 static void eval_comma(struct context *ctx, struct result *res);
52 static struct attr ** eval_list(struct context *ctx);
55 result_free(struct result *res)
60 get_op(struct context *ctx, int test, ...)
65 while ((op = va_arg(ap, char *))) {
66 if (!strncmp(ctx->expr, op, strlen(op))) {
69 ctx->expr+=strlen(op);
78 is_int(struct result *res)
84 is_double(struct result *res)
90 dump(struct result *res)
92 char object[res->varlen+1];
93 char attribute[res->attrnlen+1];
95 strncpy(object, res->var, res->varlen);
96 object[res->varlen]='\0';
98 strncpy(attribute, res->attrn, res->attrnlen);
99 attribute[res->attrnlen]='\0';
100 dbg(0,"type:%s\n", attr_to_name(res->attr.type));
101 dbg(0,"attribute '%s' from '%s'\n", attribute, object);
104 static enum attr_type
105 command_attr_type(struct result *res)
107 char attrn[res->attrnlen+1];
109 strncpy(attrn, res->attrn, res->attrnlen);
110 attrn[res->attrnlen]='\0';
111 return attr_from_name(attrn);
115 command_object_get_attr(struct attr *object, enum attr_type attr_type, struct attr *ret)
117 struct object_func *func=object_func_lookup(object->type);
118 if (!func || !func->get_attr)
120 return func->get_attr(object->u.data, attr_type, ret, NULL);
124 command_get_attr(struct context *ctx, struct result *res)
127 enum attr_type attr_type=command_attr_type(res);
128 result=command_object_get_attr(&res->attr, attr_type, &res->attr);
131 res->varlen=res->attrnlen;
133 res->attr.type=attr_none;
143 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
146 enum attr_type attr_type=command_attr_type(res);
147 struct object_func *func=object_func_lookup(res->attr.type);
148 if (!func || !func->set_attr)
150 newres->attr.type=attr_type;
151 result=func->set_attr(res->attr.u.data, &newres->attr);
156 resolve_object(struct context *ctx, struct result *res)
158 if (res->attr.type == attr_none && res->varlen) {
159 res->attr=*ctx->attr;
161 res->attrnlen=res->varlen;
168 resolve(struct context *ctx, struct result *res, struct attr *parent)
170 resolve_object(ctx, res);
171 if (res->attr.type >= attr_type_object_begin && res->attr.type <= attr_type_object_end) {
173 command_get_attr(ctx, res);
179 get_double(struct context *ctx, struct result *res)
181 resolve(ctx, res, NULL);
188 get_int(struct context *ctx, struct result *res)
190 resolve(ctx, res, NULL);
191 if (res->attr.type == attr_none)
193 if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
194 return res->attr.u.num;
196 if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
197 return (int) (*res->attr.u.numd);
199 ctx->error=wrong_type;
205 get_string(struct context *ctx, struct result *res)
207 resolve(ctx, res, NULL);
208 return attr_to_text(&res->attr, NULL, 0);
212 set_double(struct context *ctx, struct result *res, double val)
219 set_int(struct context *ctx, struct result *res, int val)
222 res->attr.type=attr_type_int_begin;
228 eval_value(struct context *ctx, struct result *res) {
236 if (op[0] >= 'a' && op[0] <= 'z') {
237 res->attr.type=attr_none;
239 while ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
246 if ((op[0] >= '0' && op[0] <= '9') ||
247 (op[0] == '.' && op[1] >= '0' && op[1] <= '9') ||
248 (op[0] == '-' && op[1] >= '0' && op[1] <= '9') ||
249 (op[0] == '-' && op[1] == '.' && op[2] >= '0' && op[2] <= '9')) {
250 while ((op[0] >= '0' && op[0] <= '9') || op[0] == '.' || (res->varlen == 0 && op[0] == '-')) {
254 ctx->error=illegal_number_format;
261 res->val = strtod(ctx->expr, NULL);
262 res->attr.type=attr_type_double_begin;
263 res->attr.u.numd=&res->val;
265 res->attr.type=attr_type_int_begin;
266 res->attr.u.num=atoi(ctx->expr);
274 } while (op[0] != '"');
275 res->attr.type=attr_type_string_begin;
277 res->attr.u.str=g_malloc(len+1);
278 strncpy(res->attr.u.str, ctx->expr+1, len);
279 res->attr.u.str[len]='\0';
284 ctx->error=illegal_character;
288 eval_brace(struct context *ctx, struct result *res)
290 if (get_op(ctx,0,"(",NULL)) {
291 eval_comma(ctx, res);
292 if (ctx->error) return;
293 if (!get_op(ctx,0,")",NULL))
294 ctx->error=missing_closing_brace;
297 eval_value(ctx, res);
301 command_call_function(struct context *ctx, struct result *res)
303 struct attr cbl,**list=NULL;
304 char function[res->attrnlen+1];
306 strncpy(function, res->attrn, res->attrnlen);
307 function[res->attrnlen]='\0';
308 dbg(0,"function=%s\n", function);
309 if (ctx->expr[0] != ')') {
311 if (ctx->error) return;
313 if (!get_op(ctx,0,")",NULL)) {
314 ctx->error=missing_closing_brace;
317 if (command_object_get_attr(&res->attr, attr_callback_list, &cbl)) {
319 dbg(0,"function call %s from %s\n",function, attr_to_name(res->attr.type));
320 callback_list_call_attr_4(cbl.u.callback_list, attr_command, function, list, NULL, &valid);
326 res->attr.type=attr_none;
330 eval_postfix(struct context *ctx, struct result *res)
335 eval_brace(ctx, res);
336 if (ctx->error) return;
338 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
341 eval_brace(ctx, &tmp);
342 if (ctx->error) return;
343 resolve(ctx, res,NULL);
344 if (ctx->error) return;
346 res->attrnlen=tmp.varlen;
348 } else if (op[0] == '[') {
349 if (!get_op(ctx,0,"]",NULL)) {
350 ctx->error=missing_closing_bracket;
353 } else if (op[0] == '(') {
354 dbg(0,"function call\n");
355 resolve_object(ctx, res);
356 command_call_function(ctx, res);
362 eval_unary(struct context *ctx, struct result *res)
365 op=get_op(ctx,0,"~","!",NULL);
367 eval_unary(ctx, res);
368 if (ctx->error) return;
370 set_int(ctx, res, ~get_int(ctx, res));
372 set_int(ctx, res, !get_int(ctx, res));
374 eval_postfix(ctx, res);
378 eval_multiplicative(struct context *ctx, struct result *res)
383 eval_unary(ctx, res);
384 if (ctx->error) return;
386 if (!(op=get_op(ctx,0,"*","/","%",NULL))) return;
387 eval_unary(ctx, &tmp);
388 if (ctx->error) return;
389 if (is_double(res) || is_double(&tmp)) {
391 set_double(ctx, res, get_double(ctx, res) * get_double(ctx, &tmp));
392 else if (op[0] == '/')
393 set_double(ctx, res, get_double(ctx, res) / get_double(ctx, &tmp));
395 ctx->error=invalid_type;
400 set_int(ctx, res, get_int(ctx, res) * get_int(ctx, &tmp));
401 else if (op[0] == '/')
402 set_int(ctx, res, get_int(ctx, res) / get_int(ctx, &tmp));
404 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
406 if (ctx->error) return;
411 eval_additive(struct context *ctx, struct result *res)
416 eval_multiplicative(ctx, res);
417 if (ctx->error) return;
419 if (!(op=get_op(ctx,0,"-","+",NULL))) return;
420 eval_multiplicative(ctx, &tmp);
421 if (ctx->error) return;
422 if (is_double(res) || is_double(&tmp)) {
424 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
426 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
429 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
431 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
433 if (ctx->error) return;
438 eval_equality(struct context *ctx, struct result *res)
443 eval_additive(ctx, res);
444 if (ctx->error) return;
446 if (!(op=get_op(ctx,0,"==","!=",NULL))) return;
447 eval_additive(ctx, &tmp);
448 if (ctx->error) return;
450 set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
452 set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
459 eval_bitwise_and(struct context *ctx, struct result *res)
463 eval_equality(ctx, res);
464 if (ctx->error) return;
466 if (get_op(ctx,1,"&&",NULL)) return;
467 if (!get_op(ctx,0,"&",NULL)) return;
468 eval_equality(ctx, &tmp);
469 if (ctx->error) return;
470 set_int(ctx, res, get_int(ctx, res) & get_int(ctx, &tmp));
471 if (ctx->error) return;
476 eval_bitwise_xor(struct context *ctx, struct result *res)
480 eval_bitwise_and(ctx, res);
481 if (ctx->error) return;
483 if (!get_op(ctx,0,"^",NULL)) return;
484 eval_bitwise_and(ctx, &tmp);
485 if (ctx->error) return;
486 set_int(ctx, res, get_int(ctx, res) ^ get_int(ctx, &tmp));
487 if (ctx->error) return;
492 eval_bitwise_or(struct context *ctx, struct result *res)
496 eval_bitwise_xor(ctx, res);
497 if (ctx->error) return;
499 if (get_op(ctx,1,"||",NULL)) return;
500 if (!get_op(ctx,0,"|",NULL)) return;
501 eval_bitwise_xor(ctx, &tmp);
502 if (ctx->error) return;
503 set_int(ctx, res, get_int(ctx, res) | get_int(ctx, &tmp));
504 if (ctx->error) return;
509 eval_logical_and(struct context *ctx, struct result *res)
513 eval_bitwise_or(ctx, res);
514 if (ctx->error) return;
516 if (!get_op(ctx,0,"&&",NULL)) return;
517 eval_bitwise_or(ctx, &tmp);
518 if (ctx->error) return;
519 set_int(ctx, res, get_int(ctx, res) && get_int(ctx, &tmp));
520 if (ctx->error) return;
525 eval_logical_or(struct context *ctx, struct result *res)
529 eval_logical_and(ctx, res);
530 if (ctx->error) return;
532 if (!get_op(ctx,0,"||",NULL)) return;
533 eval_logical_and(ctx, &tmp);
534 if (ctx->error) return;
535 set_int(ctx, res, get_int(ctx, res) || get_int(ctx, &tmp));
536 if (ctx->error) return;
541 eval_conditional(struct context *ctx, struct result *res)
546 eval_logical_or(ctx, res);
547 if (ctx->error) return;
548 if (!get_op(ctx,0,"?",NULL)) return;
549 cond=!!get_int(ctx, res);
550 if (ctx->error) return;
551 eval_logical_or(ctx, &tmp);
552 if (ctx->error) return;
555 if (!get_op(ctx,0,":",NULL)) {
556 ctx->error=missing_colon;
559 eval_logical_or(ctx, &tmp);
560 if (ctx->error) return;
565 /* = *= /= %= += -= >>= <<= &= ^= |= */
568 eval_assignment(struct context *ctx, struct result *res)
571 eval_conditional(ctx, res);
572 if (ctx->error) return;
573 if (!get_op(ctx,0,"=",NULL)) return;
574 eval_conditional(ctx, &tmp);
575 if (ctx->error) return;
576 resolve_object(ctx, res);
577 command_set_attr(ctx, res, &tmp);
582 eval_comma(struct context *ctx, struct result *res)
586 eval_assignment(ctx, res);
587 if (ctx->error) return;
589 if (!get_op(ctx,0,",",NULL)) return;
590 eval_assignment(ctx, &tmp);
591 if (ctx->error) return;
596 static struct attr **
597 eval_list(struct context *ctx)
601 struct attr **ret=NULL;
603 eval_assignment(ctx, &tmp);
608 ret=attr_generic_add_attr(ret, &tmp.attr);
609 if (!get_op(ctx,0,",",NULL)) return ret;
615 void command(struct attr *attr, char *expr)
619 memset(&res, 0, sizeof(res));
620 memset(&ctx, 0, sizeof(ctx));
624 printf("command='%s'\n", expr);
625 eval_comma(&ctx,&res);
626 printf("err=%d %s\n", ctx.error, ctx.expr);
629 resolve(&ctx, &res, NULL);
631 printf("%s\n", get_string(&ctx, &res));
636 command_evaluate_to(struct attr *attr, char *expr, struct context *ctx, struct result *res)
638 memset(res, 0, sizeof(*res));
639 memset(ctx, 0, sizeof(*ctx));
646 command_evaluate_to_void(struct attr *attr, char *expr, int **error)
650 command_evaluate_to(attr, expr, &ctx, &res);
652 resolve(&ctx, &res, NULL);
659 command_evaluate_to_string(struct attr *attr, char *expr, int **error)
665 command_evaluate_to(attr, expr, &ctx, &res);
669 ret=get_string(&ctx, &res);
677 command_evaluate(struct attr *attr, char *expr)
681 memset(&res, 0, sizeof(res));
682 memset(&ctx, 0, sizeof(ctx));
687 eval_comma(&ctx,&res);
690 resolve(&ctx, &res, NULL);
693 if (!get_op(&ctx,0,";",NULL)) return;
699 command_interpreter(struct attr *attr)
704 size=read(0, buffer, 4095);
709 command(attr, buffer);
715 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
718 for (i = 0 ; i < count ; i++) {
719 if (!strcmp(command,table->command)) {
722 table->func(data, command, in, out);
729 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
731 struct callback *cb=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
732 callback_list_add(cbl, cb);