22 speech.active=!speech.active
25 osd[type=="xxx"].active=0;osd[type=="yyy"].active=0
46 no_error=0,missing_closing_brace, missing_colon, wrong_type, illegal_number_format, illegal_character, missing_closing_bracket, invalid_type
49 static void eval_comma(struct context *ctx, struct result *res);
50 static struct attr ** eval_list(struct context *ctx);
53 get_op(struct context *ctx, int test, ...)
58 while ((op = va_arg(ap, char *))) {
59 if (!strncmp(ctx->expr, op, strlen(op))) {
62 ctx->expr+=strlen(op);
71 is_int(struct result *res)
77 is_double(struct result *res)
83 dump(struct result *res)
85 char object[res->varlen+1];
86 char attribute[res->attrnlen+1];
88 strncpy(object, res->var, res->varlen);
89 object[res->varlen]='\0';
91 strncpy(attribute, res->attrn, res->attrnlen);
92 attribute[res->attrnlen]='\0';
93 dbg(0,"type:%s\n", attr_to_name(res->attr.type));
94 dbg(0,"attribute '%s' from '%s'\n", attribute, object);
98 command_attr_type(struct result *res)
100 char attrn[res->attrnlen+1];
102 strncpy(attrn, res->attrn, res->attrnlen);
103 attrn[res->attrnlen]='\0';
104 return attr_from_name(attrn);
108 command_object_get_attr(struct attr *object, enum attr_type attr_type, struct attr *ret)
110 switch (object->type) {
112 return gui_get_attr(object->u.gui, attr_type, ret, NULL);
114 return navit_get_attr(object->u.navit, attr_type, ret, NULL);
116 return speech_get_attr(object->u.speech, attr_type, ret, NULL);
118 return vehicle_get_attr(object->u.vehicle, attr_type, ret, NULL);
125 command_get_attr(struct context *ctx, struct result *res)
128 enum attr_type attr_type=command_attr_type(res);
129 result=command_object_get_attr(&res->attr, attr_type, &res->attr);
132 res->varlen=res->attrnlen;
134 res->attr.type=attr_none;
144 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
147 enum attr_type attr_type=command_attr_type(res);
148 newres->attr.type=attr_type;
149 switch (res->attr.type) {
151 result=navit_set_attr(res->attr.u.navit, &newres->attr);
154 result=speech_set_attr(res->attr.u.speech, &newres->attr);
163 resolve_object(struct context *ctx, struct result *res)
165 if (res->attr.type == attr_none && res->varlen) {
166 res->attr=*ctx->attr;
168 res->attrnlen=res->varlen;
175 resolve(struct context *ctx, struct result *res, struct attr *parent)
177 resolve_object(ctx, res);
178 if (res->attr.type >= attr_type_object_begin && res->attr.type <= attr_type_object_end) {
180 command_get_attr(ctx, res);
186 get_double(struct context *ctx, struct result *res)
188 resolve(ctx, res, NULL);
195 get_int(struct context *ctx, struct result *res)
197 resolve(ctx, res, NULL);
198 if (res->attr.type == attr_none)
200 if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
201 return res->attr.u.num;
203 if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
204 return (int) (*res->attr.u.numd);
206 ctx->error=wrong_type;
212 get_string(struct context *ctx, struct result *res)
214 resolve(ctx, res, NULL);
215 return attr_to_text(&res->attr, NULL, 0);
219 set_double(struct context *ctx, struct result *res, double val)
225 set_int(struct context *ctx, struct result *res, int val)
227 res->attr.type=attr_type_int_begin;
233 eval_value(struct context *ctx, struct result *res) {
241 if (op[0] >= 'a' && op[0] <= 'z') {
242 res->attr.type=attr_none;
244 while ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
251 if ((op[0] >= '0' && op[0] <= '9') ||
252 (op[0] == '.' && op[1] >= '0' && op[1] <= '9')) {
253 while ((op[0] >= '0' && op[0] <= '9') || op[0] == '.') {
257 ctx->error=illegal_number_format;
264 res->val = strtod(ctx->expr, NULL);
265 res->attr.type=attr_type_double_begin;
266 res->attr.u.numd=&res->val;
268 res->attr.type=attr_type_int_begin;
269 res->attr.u.num=atoi(ctx->expr);
274 ctx->error=illegal_character;
278 eval_brace(struct context *ctx, struct result *res)
280 if (get_op(ctx,0,"(",NULL)) {
281 eval_comma(ctx, res);
282 if (ctx->error) return;
283 if (!get_op(ctx,0,")",NULL))
284 ctx->error=missing_closing_brace;
287 eval_value(ctx, res);
291 command_call_function(struct context *ctx, struct result *res)
293 struct attr cbl,**list=NULL;
294 char function[res->attrnlen+1];
296 strncpy(function, res->attrn, res->attrnlen);
297 function[res->attrnlen]='\0';
298 dbg(0,"function=%s\n", function);
299 if (ctx->expr[0] != ')') {
301 if (ctx->error) return;
303 if (!get_op(ctx,0,")",NULL)) {
304 ctx->error=missing_closing_brace;
307 if (command_object_get_attr(&res->attr, attr_callback_list, &cbl)) {
309 dbg(0,"function call %s from %s\n",function, attr_to_name(res->attr.type));
310 callback_list_call_attr_4(cbl.u.callback_list, attr_command, function, list, NULL, &valid);
316 res->attr.type=attr_none;
320 eval_postfix(struct context *ctx, struct result *res)
325 eval_brace(ctx, res);
326 if (ctx->error) return;
328 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
331 eval_brace(ctx, &tmp);
332 if (ctx->error) return;
333 resolve(ctx, res,NULL);
334 if (ctx->error) return;
336 res->attrnlen=tmp.varlen;
338 } else if (op[0] == '[') {
339 if (!get_op(ctx,0,"]",NULL)) {
340 ctx->error=missing_closing_bracket;
343 } else if (op[0] == '(') {
344 dbg(0,"function call\n");
345 resolve_object(ctx, res);
346 command_call_function(ctx, res);
352 eval_unary(struct context *ctx, struct result *res)
355 op=get_op(ctx,0,"~","!",NULL);
357 eval_unary(ctx, res);
358 if (ctx->error) return;
360 set_int(ctx, res, ~get_int(ctx, res));
362 set_int(ctx, res, !get_int(ctx, res));
364 eval_postfix(ctx, res);
368 eval_multiplicative(struct context *ctx, struct result *res)
373 eval_unary(ctx, res);
374 if (ctx->error) return;
376 if (!(op=get_op(ctx,0,"*","/","%",NULL))) return;
377 eval_unary(ctx, &tmp);
378 if (ctx->error) return;
379 if (is_double(res) || is_double(&tmp)) {
381 set_double(ctx, res, get_double(ctx, res) * get_double(ctx, &tmp));
382 else if (op[0] == '/')
383 set_double(ctx, res, get_double(ctx, res) / get_double(ctx, &tmp));
385 ctx->error=invalid_type;
390 set_int(ctx, res, get_int(ctx, res) * get_int(ctx, &tmp));
391 else if (op[0] == '/')
392 set_int(ctx, res, get_int(ctx, res) / get_int(ctx, &tmp));
394 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
396 if (ctx->error) return;
401 eval_additive(struct context *ctx, struct result *res)
406 eval_multiplicative(ctx, res);
407 if (ctx->error) return;
409 if (!(op=get_op(ctx,0,"-","+",NULL))) return;
410 eval_multiplicative(ctx, &tmp);
411 if (ctx->error) return;
412 if (is_double(res) || is_double(&tmp)) {
414 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
416 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
419 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
421 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
423 if (ctx->error) return;
428 eval_equality(struct context *ctx, struct result *res)
433 eval_additive(ctx, res);
434 if (ctx->error) return;
436 if (!(op=get_op(ctx,0,"==","!=",NULL))) return;
437 eval_additive(ctx, &tmp);
438 if (ctx->error) return;
440 set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
442 set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
448 eval_bitwise_and(struct context *ctx, struct result *res)
452 eval_equality(ctx, res);
453 if (ctx->error) return;
455 if (get_op(ctx,1,"&&",NULL)) return;
456 if (!get_op(ctx,0,"&",NULL)) return;
457 eval_equality(ctx, &tmp);
458 if (ctx->error) return;
459 set_int(ctx, res, get_int(ctx, res) & get_int(ctx, &tmp));
460 if (ctx->error) return;
465 eval_bitwise_xor(struct context *ctx, struct result *res)
469 eval_bitwise_and(ctx, res);
470 if (ctx->error) return;
472 if (!get_op(ctx,0,"^",NULL)) return;
473 eval_bitwise_and(ctx, &tmp);
474 if (ctx->error) return;
475 set_int(ctx, res, get_int(ctx, res) ^ get_int(ctx, &tmp));
476 if (ctx->error) return;
481 eval_bitwise_or(struct context *ctx, struct result *res)
485 eval_bitwise_xor(ctx, res);
486 if (ctx->error) return;
488 if (get_op(ctx,1,"||",NULL)) return;
489 if (!get_op(ctx,0,"|",NULL)) return;
490 eval_bitwise_xor(ctx, &tmp);
491 if (ctx->error) return;
492 set_int(ctx, res, get_int(ctx, res) | get_int(ctx, &tmp));
493 if (ctx->error) return;
498 eval_logical_and(struct context *ctx, struct result *res)
502 eval_bitwise_or(ctx, res);
503 if (ctx->error) return;
505 if (!get_op(ctx,0,"&&",NULL)) return;
506 eval_bitwise_or(ctx, &tmp);
507 if (ctx->error) return;
508 set_int(ctx, res, get_int(ctx, res) && get_int(ctx, &tmp));
509 if (ctx->error) return;
514 eval_logical_or(struct context *ctx, struct result *res)
518 eval_logical_and(ctx, res);
519 if (ctx->error) return;
521 if (!get_op(ctx,0,"||",NULL)) return;
522 eval_logical_and(ctx, &tmp);
523 if (ctx->error) return;
524 set_int(ctx, res, get_int(ctx, res) || get_int(ctx, &tmp));
525 if (ctx->error) return;
530 eval_conditional(struct context *ctx, struct result *res)
535 eval_logical_or(ctx, res);
536 if (ctx->error) return;
537 if (!get_op(ctx,0,"?",NULL)) return;
538 cond=!!get_int(ctx, res);
539 if (ctx->error) return;
540 eval_logical_or(ctx, &tmp);
541 if (ctx->error) return;
544 if (!get_op(ctx,0,":",NULL)) {
545 ctx->error=missing_colon;
548 eval_logical_or(ctx, &tmp);
549 if (ctx->error) return;
554 /* = *= /= %= += -= >>= <<= &= ^= |= */
557 eval_assignment(struct context *ctx, struct result *res)
560 eval_conditional(ctx, res);
561 if (ctx->error) return;
562 if (!get_op(ctx,0,"=",NULL)) return;
563 eval_conditional(ctx, &tmp);
564 if (ctx->error) return;
565 resolve_object(ctx, res);
566 command_set_attr(ctx, res, &tmp);
571 eval_comma(struct context *ctx, struct result *res)
575 eval_assignment(ctx, res);
576 if (ctx->error) return;
578 if (!get_op(ctx,0,",",NULL)) return;
579 eval_assignment(ctx, &tmp);
580 if (ctx->error) return;
585 static struct attr **
586 eval_list(struct context *ctx)
590 struct attr **ret=NULL;
592 eval_assignment(ctx, &tmp);
597 ret=attr_generic_add_attr(ret, &tmp.attr);
598 if (!get_op(ctx,0,",",NULL)) return ret;
604 void command(struct attr *attr, char *expr)
608 memset(&res, 0, sizeof(res));
609 memset(&ctx, 0, sizeof(ctx));
613 printf("command='%s'\n", expr);
614 eval_comma(&ctx,&res);
615 printf("err=%d %s\n", ctx.error, ctx.expr);
618 resolve(&ctx, &res, NULL);
620 printf("%s\n", get_string(&ctx, &res));
625 command_evaluate_to_void(struct attr *attr, char *expr)
629 memset(&res, 0, sizeof(res));
630 memset(&ctx, 0, sizeof(ctx));
634 eval_comma(&ctx,&res);
635 resolve(&ctx, &res, NULL);
639 command_evaluate(struct attr *attr, char *expr)
643 memset(&res, 0, sizeof(res));
644 memset(&ctx, 0, sizeof(ctx));
649 eval_comma(&ctx,&res);
652 resolve(&ctx, &res, NULL);
655 if (!get_op(&ctx,0,";",NULL)) return;
661 command_interpreter(struct attr *attr)
666 size=read(0, buffer, 4095);
671 command(attr, buffer);
677 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
680 for (i = 0 ; i < count ; i++) {
681 if (!strcmp(command,table->command)) {
684 table->func(data, command, in, out);
691 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
693 struct callback *cb=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
694 callback_list_add(cbl, cb);