24 speech.active=!speech.active
27 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 get_op(struct context *ctx, int test, ...)
60 while ((op = va_arg(ap, char *))) {
61 if (!strncmp(ctx->expr, op, strlen(op))) {
64 ctx->expr+=strlen(op);
73 is_int(struct result *res)
79 is_double(struct result *res)
85 dump(struct result *res)
87 char object[res->varlen+1];
88 char attribute[res->attrnlen+1];
90 strncpy(object, res->var, res->varlen);
91 object[res->varlen]='\0';
93 strncpy(attribute, res->attrn, res->attrnlen);
94 attribute[res->attrnlen]='\0';
95 dbg(0,"type:%s\n", attr_to_name(res->attr.type));
96 dbg(0,"attribute '%s' from '%s'\n", attribute, object);
100 command_attr_type(struct result *res)
102 char attrn[res->attrnlen+1];
104 strncpy(attrn, res->attrn, res->attrnlen);
105 attrn[res->attrnlen]='\0';
106 return attr_from_name(attrn);
110 command_object_get_attr(struct attr *object, enum attr_type attr_type, struct attr *ret)
112 struct object_func *func=object_func_lookup(object->type);
113 if (!func || !func->get_attr)
115 return func->get_attr(object->u.data, attr_type, ret, NULL);
119 command_get_attr(struct context *ctx, struct result *res)
122 enum attr_type attr_type=command_attr_type(res);
123 result=command_object_get_attr(&res->attr, attr_type, &res->attr);
126 res->varlen=res->attrnlen;
128 res->attr.type=attr_none;
138 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
141 enum attr_type attr_type=command_attr_type(res);
142 struct object_func *func=object_func_lookup(res->attr.type);
143 if (!func || !func->set_attr)
145 newres->attr.type=attr_type;
146 result=func->set_attr(res->attr.u.data, &newres->attr);
151 resolve_object(struct context *ctx, struct result *res)
153 if (res->attr.type == attr_none && res->varlen) {
154 res->attr=*ctx->attr;
156 res->attrnlen=res->varlen;
163 resolve(struct context *ctx, struct result *res, struct attr *parent)
165 resolve_object(ctx, res);
166 if (res->attr.type >= attr_type_object_begin && res->attr.type <= attr_type_object_end) {
168 command_get_attr(ctx, res);
174 get_double(struct context *ctx, struct result *res)
176 resolve(ctx, res, NULL);
183 get_int(struct context *ctx, struct result *res)
185 resolve(ctx, res, NULL);
186 if (res->attr.type == attr_none)
188 if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
189 return res->attr.u.num;
191 if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
192 return (int) (*res->attr.u.numd);
194 ctx->error=wrong_type;
200 get_string(struct context *ctx, struct result *res)
202 resolve(ctx, res, NULL);
203 return attr_to_text(&res->attr, NULL, 0);
207 set_double(struct context *ctx, struct result *res, double val)
213 set_int(struct context *ctx, struct result *res, int val)
215 res->attr.type=attr_type_int_begin;
221 eval_value(struct context *ctx, struct result *res) {
229 if (op[0] >= 'a' && op[0] <= 'z') {
230 res->attr.type=attr_none;
232 while ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
239 if ((op[0] >= '0' && op[0] <= '9') ||
240 (op[0] == '.' && op[1] >= '0' && op[1] <= '9')) {
241 while ((op[0] >= '0' && op[0] <= '9') || op[0] == '.') {
245 ctx->error=illegal_number_format;
252 res->val = strtod(ctx->expr, NULL);
253 res->attr.type=attr_type_double_begin;
254 res->attr.u.numd=&res->val;
256 res->attr.type=attr_type_int_begin;
257 res->attr.u.num=atoi(ctx->expr);
262 ctx->error=illegal_character;
266 eval_brace(struct context *ctx, struct result *res)
268 if (get_op(ctx,0,"(",NULL)) {
269 eval_comma(ctx, res);
270 if (ctx->error) return;
271 if (!get_op(ctx,0,")",NULL))
272 ctx->error=missing_closing_brace;
275 eval_value(ctx, res);
279 command_call_function(struct context *ctx, struct result *res)
281 struct attr cbl,**list=NULL;
282 char function[res->attrnlen+1];
284 strncpy(function, res->attrn, res->attrnlen);
285 function[res->attrnlen]='\0';
286 dbg(0,"function=%s\n", function);
287 if (ctx->expr[0] != ')') {
289 if (ctx->error) return;
291 if (!get_op(ctx,0,")",NULL)) {
292 ctx->error=missing_closing_brace;
295 if (command_object_get_attr(&res->attr, attr_callback_list, &cbl)) {
297 dbg(0,"function call %s from %s\n",function, attr_to_name(res->attr.type));
298 callback_list_call_attr_4(cbl.u.callback_list, attr_command, function, list, NULL, &valid);
304 res->attr.type=attr_none;
308 eval_postfix(struct context *ctx, struct result *res)
313 eval_brace(ctx, res);
314 if (ctx->error) return;
316 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
319 eval_brace(ctx, &tmp);
320 if (ctx->error) return;
321 resolve(ctx, res,NULL);
322 if (ctx->error) return;
324 res->attrnlen=tmp.varlen;
326 } else if (op[0] == '[') {
327 if (!get_op(ctx,0,"]",NULL)) {
328 ctx->error=missing_closing_bracket;
331 } else if (op[0] == '(') {
332 dbg(0,"function call\n");
333 resolve_object(ctx, res);
334 command_call_function(ctx, res);
340 eval_unary(struct context *ctx, struct result *res)
343 op=get_op(ctx,0,"~","!",NULL);
345 eval_unary(ctx, res);
346 if (ctx->error) return;
348 set_int(ctx, res, ~get_int(ctx, res));
350 set_int(ctx, res, !get_int(ctx, res));
352 eval_postfix(ctx, res);
356 eval_multiplicative(struct context *ctx, struct result *res)
361 eval_unary(ctx, res);
362 if (ctx->error) return;
364 if (!(op=get_op(ctx,0,"*","/","%",NULL))) return;
365 eval_unary(ctx, &tmp);
366 if (ctx->error) return;
367 if (is_double(res) || is_double(&tmp)) {
369 set_double(ctx, res, get_double(ctx, res) * get_double(ctx, &tmp));
370 else if (op[0] == '/')
371 set_double(ctx, res, get_double(ctx, res) / get_double(ctx, &tmp));
373 ctx->error=invalid_type;
378 set_int(ctx, res, get_int(ctx, res) * get_int(ctx, &tmp));
379 else if (op[0] == '/')
380 set_int(ctx, res, get_int(ctx, res) / get_int(ctx, &tmp));
382 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
384 if (ctx->error) return;
389 eval_additive(struct context *ctx, struct result *res)
394 eval_multiplicative(ctx, res);
395 if (ctx->error) return;
397 if (!(op=get_op(ctx,0,"-","+",NULL))) return;
398 eval_multiplicative(ctx, &tmp);
399 if (ctx->error) return;
400 if (is_double(res) || is_double(&tmp)) {
402 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
404 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
407 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
409 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
411 if (ctx->error) return;
416 eval_equality(struct context *ctx, struct result *res)
421 eval_additive(ctx, res);
422 if (ctx->error) return;
424 if (!(op=get_op(ctx,0,"==","!=",NULL))) return;
425 eval_additive(ctx, &tmp);
426 if (ctx->error) return;
428 set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
430 set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
436 eval_bitwise_and(struct context *ctx, struct result *res)
440 eval_equality(ctx, res);
441 if (ctx->error) return;
443 if (get_op(ctx,1,"&&",NULL)) return;
444 if (!get_op(ctx,0,"&",NULL)) return;
445 eval_equality(ctx, &tmp);
446 if (ctx->error) return;
447 set_int(ctx, res, get_int(ctx, res) & get_int(ctx, &tmp));
448 if (ctx->error) return;
453 eval_bitwise_xor(struct context *ctx, struct result *res)
457 eval_bitwise_and(ctx, res);
458 if (ctx->error) return;
460 if (!get_op(ctx,0,"^",NULL)) return;
461 eval_bitwise_and(ctx, &tmp);
462 if (ctx->error) return;
463 set_int(ctx, res, get_int(ctx, res) ^ get_int(ctx, &tmp));
464 if (ctx->error) return;
469 eval_bitwise_or(struct context *ctx, struct result *res)
473 eval_bitwise_xor(ctx, res);
474 if (ctx->error) return;
476 if (get_op(ctx,1,"||",NULL)) return;
477 if (!get_op(ctx,0,"|",NULL)) return;
478 eval_bitwise_xor(ctx, &tmp);
479 if (ctx->error) return;
480 set_int(ctx, res, get_int(ctx, res) | get_int(ctx, &tmp));
481 if (ctx->error) return;
486 eval_logical_and(struct context *ctx, struct result *res)
490 eval_bitwise_or(ctx, res);
491 if (ctx->error) return;
493 if (!get_op(ctx,0,"&&",NULL)) return;
494 eval_bitwise_or(ctx, &tmp);
495 if (ctx->error) return;
496 set_int(ctx, res, get_int(ctx, res) && get_int(ctx, &tmp));
497 if (ctx->error) return;
502 eval_logical_or(struct context *ctx, struct result *res)
506 eval_logical_and(ctx, res);
507 if (ctx->error) return;
509 if (!get_op(ctx,0,"||",NULL)) return;
510 eval_logical_and(ctx, &tmp);
511 if (ctx->error) return;
512 set_int(ctx, res, get_int(ctx, res) || get_int(ctx, &tmp));
513 if (ctx->error) return;
518 eval_conditional(struct context *ctx, struct result *res)
523 eval_logical_or(ctx, res);
524 if (ctx->error) return;
525 if (!get_op(ctx,0,"?",NULL)) return;
526 cond=!!get_int(ctx, res);
527 if (ctx->error) return;
528 eval_logical_or(ctx, &tmp);
529 if (ctx->error) return;
532 if (!get_op(ctx,0,":",NULL)) {
533 ctx->error=missing_colon;
536 eval_logical_or(ctx, &tmp);
537 if (ctx->error) return;
542 /* = *= /= %= += -= >>= <<= &= ^= |= */
545 eval_assignment(struct context *ctx, struct result *res)
548 eval_conditional(ctx, res);
549 if (ctx->error) return;
550 if (!get_op(ctx,0,"=",NULL)) return;
551 eval_conditional(ctx, &tmp);
552 if (ctx->error) return;
553 resolve_object(ctx, res);
554 command_set_attr(ctx, res, &tmp);
559 eval_comma(struct context *ctx, struct result *res)
563 eval_assignment(ctx, res);
564 if (ctx->error) return;
566 if (!get_op(ctx,0,",",NULL)) return;
567 eval_assignment(ctx, &tmp);
568 if (ctx->error) return;
573 static struct attr **
574 eval_list(struct context *ctx)
578 struct attr **ret=NULL;
580 eval_assignment(ctx, &tmp);
585 ret=attr_generic_add_attr(ret, &tmp.attr);
586 if (!get_op(ctx,0,",",NULL)) return ret;
592 void command(struct attr *attr, char *expr)
596 memset(&res, 0, sizeof(res));
597 memset(&ctx, 0, sizeof(ctx));
601 printf("command='%s'\n", expr);
602 eval_comma(&ctx,&res);
603 printf("err=%d %s\n", ctx.error, ctx.expr);
606 resolve(&ctx, &res, NULL);
608 printf("%s\n", get_string(&ctx, &res));
613 command_evaluate_to_void(struct attr *attr, char *expr)
617 memset(&res, 0, sizeof(res));
618 memset(&ctx, 0, sizeof(ctx));
622 eval_comma(&ctx,&res);
623 resolve(&ctx, &res, NULL);
627 command_evaluate(struct attr *attr, char *expr)
631 memset(&res, 0, sizeof(res));
632 memset(&ctx, 0, sizeof(ctx));
637 eval_comma(&ctx,&res);
640 resolve(&ctx, &res, NULL);
643 if (!get_op(&ctx,0,";",NULL)) return;
649 command_interpreter(struct attr *attr)
654 size=read(0, buffer, 4095);
659 command(attr, buffer);
665 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
668 for (i = 0 ; i < count ; i++) {
669 if (!strcmp(command,table->command)) {
672 table->func(data, command, in, out);
679 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
681 struct callback *cb=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
682 callback_list_add(cbl, cb);