17 #include "navit_nls.h"
20 gui.fullscreen=!gui.fullscreen
25 speech.active=!speech.active
28 osd[type=="xxx"].active=0;osd[type=="yyy"].active=0
49 struct command_saved_cb {
54 struct command_saved {
57 char *command; // The command string itself
58 struct event_idle *idle_ev; // Event to update this command
59 struct callback *idle_cb;
60 struct callback *register_cb; // Callback to register all the callbacks
61 struct event_idle *register_ev; // Idle event to register all the callbacks
64 struct command_saved_cb *cbs; // List of callbacks for this saved command
65 struct callback *cb; // Callback that should be called when we re-evaluate
70 no_error=0,missing_closing_brace, missing_colon, wrong_type, illegal_number_format, illegal_character, missing_closing_bracket, invalid_type, not_ready
73 static void eval_comma(struct context *ctx, struct result *res);
74 static struct attr ** eval_list(struct context *ctx);
77 result_free(struct result *res)
81 static int command_register_callbacks(struct command_saved *cs);
84 get_op(struct context *ctx, int test, ...)
89 while (g_ascii_isspace(*ctx->expr)) {
94 while ((op = va_arg(ap, char *))) {
95 if (!strncmp(ctx->expr, op, strlen(op))) {
98 ctx->expr+=strlen(op);
107 is_int(struct result *res)
113 is_double(struct result *res)
119 dump(struct result *res)
122 char object[res->varlen+1];
123 char attribute[res->attrnlen+1];
125 strncpy(object, res->var, res->varlen);
126 object[res->varlen]='\0';
128 strncpy(attribute, res->attrn, res->attrnlen);
129 attribute[res->attrnlen]='\0';
130 dbg(0,"type:%s\n", attr_to_name(res->attr.type));
131 dbg(0,"attribute '%s' from '%s'\n", attribute, object);
135 static enum attr_type
136 command_attr_type(struct result *res)
138 char attrn[res->attrnlen+1];
140 strncpy(attrn, res->attrn, res->attrnlen);
141 attrn[res->attrnlen]='\0';
142 return attr_from_name(attrn);
146 command_object_get_attr(struct context *ctx, struct attr *object, enum attr_type attr_type, struct attr *ret)
148 struct object_func *func=object_func_lookup(object->type);
149 if (!func || !func->get_attr)
151 return func->get_attr(object->u.data, attr_type, ret, NULL);
155 command_get_attr(struct context *ctx, struct result *res)
158 enum attr_type attr_type=command_attr_type(res);
159 result=command_object_get_attr(ctx, &res->attr, attr_type, &res->attr);
162 res->varlen=res->attrnlen;
164 res->attr.type=attr_none;
174 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
177 enum attr_type attr_type=command_attr_type(res);
178 struct object_func *func=object_func_lookup(res->attr.type);
179 if (!func || !func->set_attr)
181 newres->attr.type=attr_type;
182 result=func->set_attr(res->attr.u.data, &newres->attr);
187 resolve_object(struct context *ctx, struct result *res)
189 if (res->attr.type == attr_none && res->varlen) {
190 res->attr=*ctx->attr;
192 res->attrnlen=res->varlen;
199 resolve(struct context *ctx, struct result *res, struct attr *parent) //FIXME What is that parent for?
201 resolve_object(ctx, res);
203 command_get_attr(ctx, res);
207 get_double(struct context *ctx, struct result *res)
209 resolve(ctx, res, NULL);
216 get_int(struct context *ctx, struct result *res)
218 resolve(ctx, res, NULL);
219 if (res->attr.type == attr_none)
221 if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
222 return res->attr.u.num;
224 if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
225 return (int) (*res->attr.u.numd);
227 ctx->error=wrong_type;
233 get_string(struct context *ctx, struct result *res)
235 resolve(ctx, res, NULL);
236 return attr_to_text(&res->attr, NULL, 0);
240 set_double(struct context *ctx, struct result *res, double val)
247 set_int(struct context *ctx, struct result *res, int val)
250 res->attr.type=attr_type_int_begin;
256 eval_value(struct context *ctx, struct result *res) {
265 while (g_ascii_isspace(*op)) {
269 if ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
270 res->attr.type=attr_none;
273 while ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
277 if (res->varlen == 3 && !strncmp(res->var,"new",3) && op[0] == ' ') {
286 if ((op[0] >= '0' && op[0] <= '9') ||
287 (op[0] == '.' && op[1] >= '0' && op[1] <= '9') ||
288 (op[0] == '-' && op[1] >= '0' && op[1] <= '9') ||
289 (op[0] == '-' && op[1] == '.' && op[2] >= '0' && op[2] <= '9')) {
290 while ((op[0] >= '0' && op[0] <= '9') || op[0] == '.' || (res->varlen == 0 && op[0] == '-')) {
294 ctx->error=illegal_number_format;
301 res->val = strtod(ctx->expr, NULL);
302 res->attr.type=attr_type_double_begin;
303 res->attr.u.numd=&res->val;
305 res->attr.type=attr_type_int_begin;
306 res->attr.u.num=atoi(ctx->expr);
314 } while (op[0] != '"');
315 res->attr.type=attr_type_string_begin;
317 res->attr.u.str=g_malloc(len+1);
318 strncpy(res->attr.u.str, ctx->expr+1, len);
319 res->attr.u.str[len]='\0';
324 ctx->error=illegal_character;
328 get_next_object(struct context *ctx, struct result *res) {
336 if ((res->attr.type == attr_none) && (res->varlen > 0)) {
337 if (ctx->expr[0] != '.') {
338 return 1; // 1 means "this is the final object name"
340 return 2; // 2 means "there are more object names following" (e.g. we just hit 'vehicle' in 'vehicle.position_speed'
345 // Probably hit an operator
354 eval_brace(struct context *ctx, struct result *res)
356 if (get_op(ctx,0,"(",NULL)) {
357 eval_comma(ctx, res);
358 if (ctx->error) return;
359 if (!get_op(ctx,0,")",NULL))
360 ctx->error=missing_closing_brace;
363 eval_value(ctx, res);
367 command_call_function(struct context *ctx, struct result *res)
369 struct attr cbl,**list=NULL;
370 char function[res->attrnlen+1];
372 strncpy(function, res->attrn, res->attrnlen);
373 function[res->attrnlen]='\0';
374 dbg(1,"function=%s\n", function);
375 if (ctx->expr[0] != ')') {
377 if (ctx->error) return;
379 if (!get_op(ctx,0,")",NULL)) {
380 ctx->error=missing_closing_brace;
383 if (!strcmp(function,"_") && list && list[0] && list[0]->type >= attr_type_string_begin && list[0]->type <= attr_type_string_end) {
384 res->attr.type=list[0]->type;
385 res->attr.u.str=g_strdup(gettext(list[0]->u.str));
387 } if (!strncmp(function,"new ",4)) {
388 enum attr_type attr_type=attr_from_name(function+4);
389 if (attr_type != attr_none) {
390 struct object_func *func=object_func_lookup(attr_type);
391 if (func && func->new) {
392 res->attr.type=attr_type;
393 res->attr.u.data=func->new(NULL, list);
397 if (command_object_get_attr(ctx, &res->attr, attr_callback_list, &cbl)) {
399 dbg(1,"function call %s from %s\n",function, attr_to_name(res->attr.type));
400 callback_list_call_attr_4(cbl.u.callback_list, attr_command, function, list, NULL, &valid);
402 res->attr.type=attr_none;
411 eval_postfix(struct context *ctx, struct result *res)
416 eval_brace(ctx, res);
417 if (ctx->error) return;
419 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
422 eval_brace(ctx, &tmp);
423 if (ctx->error) return;
424 resolve(ctx, res,NULL);
425 if (ctx->error) return;
427 res->attrnlen=tmp.varlen;
429 } else if (op[0] == '[') {
430 if (!get_op(ctx,0,"]",NULL)) {
431 ctx->error=missing_closing_bracket;
434 } else if (op[0] == '(') {
435 dbg(1,"function call\n");
436 resolve_object(ctx, res);
437 command_call_function(ctx, res);
443 eval_unary(struct context *ctx, struct result *res)
446 op=get_op(ctx,0,"~","!",NULL);
448 eval_unary(ctx, res);
449 if (ctx->error) return;
451 set_int(ctx, res, ~get_int(ctx, res));
453 set_int(ctx, res, !get_int(ctx, res));
455 eval_postfix(ctx, res);
459 eval_multiplicative(struct context *ctx, struct result *res)
464 eval_unary(ctx, res);
465 if (ctx->error) return;
467 if (!(op=get_op(ctx,0,"*","/","%",NULL))) return;
468 eval_unary(ctx, &tmp);
469 if (ctx->error) return;
470 if (is_double(res) || is_double(&tmp)) {
472 set_double(ctx, res, get_double(ctx, res) * get_double(ctx, &tmp));
473 else if (op[0] == '/')
474 set_double(ctx, res, get_double(ctx, res) / get_double(ctx, &tmp));
476 ctx->error=invalid_type;
481 set_int(ctx, res, get_int(ctx, res) * get_int(ctx, &tmp));
482 else if (op[0] == '/')
483 set_int(ctx, res, get_int(ctx, res) / get_int(ctx, &tmp));
485 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
487 if (ctx->error) return;
492 eval_additive(struct context *ctx, struct result *res)
497 eval_multiplicative(ctx, res);
498 if (ctx->error) return;
500 if (!(op=get_op(ctx,0,"-","+",NULL))) return;
501 eval_multiplicative(ctx, &tmp);
502 if (ctx->error) return;
503 if (is_double(res) || is_double(&tmp)) {
505 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
507 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
510 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
512 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
514 if (ctx->error) return;
519 eval_equality(struct context *ctx, struct result *res)
524 eval_additive(ctx, res);
525 if (ctx->error) return;
527 if (!(op=get_op(ctx,0,"==","!=","<=",">=","<",">",NULL))) return;
528 eval_additive(ctx, &tmp);
529 if (ctx->error) return;
533 set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
536 set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
540 set_int(ctx, res, (get_int(ctx, res) <= get_int(ctx, &tmp)));
542 set_int(ctx, res, (get_int(ctx, res) < get_int(ctx, &tmp)));
547 set_int(ctx, res, (get_int(ctx, res) >= get_int(ctx, &tmp)));
549 set_int(ctx, res, (get_int(ctx, res) > get_int(ctx, &tmp)));
561 eval_bitwise_and(struct context *ctx, struct result *res)
565 eval_equality(ctx, res);
566 if (ctx->error) return;
568 if (get_op(ctx,1,"&&",NULL)) return;
569 if (!get_op(ctx,0,"&",NULL)) return;
570 eval_equality(ctx, &tmp);
571 if (ctx->error) return;
572 set_int(ctx, res, get_int(ctx, res) & get_int(ctx, &tmp));
573 if (ctx->error) return;
578 eval_bitwise_xor(struct context *ctx, struct result *res)
582 eval_bitwise_and(ctx, res);
583 if (ctx->error) return;
585 if (!get_op(ctx,0,"^",NULL)) return;
586 eval_bitwise_and(ctx, &tmp);
587 if (ctx->error) return;
588 set_int(ctx, res, get_int(ctx, res) ^ get_int(ctx, &tmp));
589 if (ctx->error) return;
594 eval_bitwise_or(struct context *ctx, struct result *res)
598 eval_bitwise_xor(ctx, res);
599 if (ctx->error) return;
601 if (get_op(ctx,1,"||",NULL)) return;
602 if (!get_op(ctx,0,"|",NULL)) return;
603 eval_bitwise_xor(ctx, &tmp);
604 if (ctx->error) return;
605 set_int(ctx, res, get_int(ctx, res) | get_int(ctx, &tmp));
606 if (ctx->error) return;
611 eval_logical_and(struct context *ctx, struct result *res)
615 eval_bitwise_or(ctx, res);
616 if (ctx->error) return;
618 if (!get_op(ctx,0,"&&",NULL)) return;
619 eval_bitwise_or(ctx, &tmp);
620 if (ctx->error) return;
621 set_int(ctx, res, get_int(ctx, res) && get_int(ctx, &tmp));
622 if (ctx->error) return;
627 eval_logical_or(struct context *ctx, struct result *res)
631 eval_logical_and(ctx, res);
632 if (ctx->error) return;
634 if (!get_op(ctx,0,"||",NULL)) return;
635 eval_logical_and(ctx, &tmp);
636 if (ctx->error) return;
637 set_int(ctx, res, get_int(ctx, res) || get_int(ctx, &tmp));
638 if (ctx->error) return;
643 eval_conditional(struct context *ctx, struct result *res)
648 eval_logical_or(ctx, res);
649 if (ctx->error) return;
650 if (!get_op(ctx,0,"?",NULL)) return;
651 cond=!!get_int(ctx, res);
652 if (ctx->error) return;
653 eval_logical_or(ctx, &tmp);
654 if (ctx->error) return;
657 if (!get_op(ctx,0,":",NULL)) {
658 ctx->error=missing_colon;
661 eval_logical_or(ctx, &tmp);
662 if (ctx->error) return;
667 /* = *= /= %= += -= >>= <<= &= ^= |= */
670 eval_assignment(struct context *ctx, struct result *res)
673 eval_conditional(ctx, res);
674 if (ctx->error) return;
675 if (!get_op(ctx,0,"=",NULL)) return;
676 eval_conditional(ctx, &tmp);
677 if (ctx->error) return;
678 resolve(ctx, &tmp, NULL);
679 if (ctx->error) return;
680 resolve_object(ctx, res);
681 command_set_attr(ctx, res, &tmp);
686 eval_comma(struct context *ctx, struct result *res)
690 eval_assignment(ctx, res);
691 if (ctx->error) return;
693 if (!get_op(ctx,0,",",NULL)) return;
694 eval_assignment(ctx, &tmp);
695 if (ctx->error) return;
700 static struct attr **
701 eval_list(struct context *ctx)
705 struct attr **ret=NULL;
707 eval_assignment(ctx, &tmp);
712 resolve(ctx, &tmp, NULL);
713 ret=attr_generic_add_attr(ret, &tmp.attr);
714 if (!get_op(ctx,0,",",NULL)) return ret;
720 void command(struct attr *attr, char *expr)
724 memset(&res, 0, sizeof(res));
725 memset(&ctx, 0, sizeof(ctx));
729 printf("command='%s'\n", expr);
730 eval_comma(&ctx,&res);
731 printf("err=%d %s\n", ctx.error, ctx.expr);
734 resolve(&ctx, &res, NULL);
736 printf("%s\n", get_string(&ctx, &res));
741 command_evaluate_to(struct attr *attr, char *expr, struct context *ctx, struct result *res)
743 memset(res, 0, sizeof(*res));
744 memset(ctx, 0, sizeof(*ctx));
751 command_evaluate_to_void(struct attr *attr, char *expr, int **error)
755 command_evaluate_to(attr, expr, &ctx, &res);
757 resolve(&ctx, &res, NULL);
764 command_evaluate_to_string(struct attr *attr, char *expr, int **error)
770 command_evaluate_to(attr, expr, &ctx, &res);
772 resolve(&ctx, &res, NULL);
774 ret=get_string(&ctx, &res);
784 command_evaluate_to_int(struct attr *attr, char *expr, int **error)
790 command_evaluate_to(attr, expr, &ctx, &res);
792 resolve(&ctx, &res, NULL);
794 ret=get_int(&ctx, &res);
804 command_evaluate_to_boolean(struct attr *attr, char *expr, int **error)
810 command_evaluate_to(attr, expr, &ctx, &res);
812 resolve(&ctx, &res, NULL);
814 if (res.attr.type == attr_none)
816 else if ((res.attr.type >= attr_type_int_begin && res.attr.type <= attr_type_int_end) ||
817 (res.attr.type >= attr_type_double_begin && res.attr.type <= attr_type_double_end))
818 ret=get_int(&ctx, &res);
820 ret=res.attr.u.data != NULL;
831 command_evaluate(struct attr *attr, char *expr)
835 memset(&res, 0, sizeof(res));
836 memset(&ctx, 0, sizeof(ctx));
841 eval_comma(&ctx,&res);
844 resolve(&ctx, &res, NULL);
847 if (!get_op(&ctx,0,";",NULL)) return;
853 command_interpreter(struct attr *attr)
858 size=read(0, buffer, 4095);
863 command(attr, buffer);
869 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
872 for (i = 0 ; i < count ; i++) {
873 if (!strcmp(command,table->command)) {
876 table->func(data, command, in, out);
883 command_add_table_attr(struct command_table *table, int count, void *data, struct attr *attr)
885 attr->type=attr_callback;
886 attr->u.callback=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
890 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
893 command_add_table_attr(table, count, data, &attr);
894 callback_list_add(cbl, attr.u.callback);
898 command_saved_set_cb (struct command_saved *cs, struct callback *cb)
904 command_saved_get_int (struct command_saved *cs)
906 return get_int(&cs->ctx, &cs->res);
910 command_saved_error (struct command_saved *cs)
916 command_saved_evaluate_idle (struct command_saved *cs)
918 // Only run once at a time
920 event_remove_idle(cs->idle_ev);
924 command_evaluate_to(&cs->navit, cs->command, &cs->ctx, &cs->res);
926 if (!cs->ctx.error) {
930 callback_call_1(cs->cb, cs);
933 cs->error = cs->ctx.error;
938 command_saved_evaluate(struct command_saved *cs)
941 // We're already scheduled for reevaluation
946 cs->idle_cb = callback_new_1(callback_cast(command_saved_evaluate_idle), cs);
949 cs->idle_ev = event_add_idle(100, cs->idle_cb);
953 command_saved_callbacks_changed(struct command_saved *cs)
955 // For now, we delete each and every callback and then re-create them
957 struct object_func *func;
960 if (cs->register_ev) {
961 event_remove_idle(cs->register_ev);
962 cs->register_ev = NULL;
965 attr.type = attr_callback;
967 for (i = 0; i < cs->num_cbs; i++) {
968 func = object_func_lookup(cs->cbs[i].attr.type);
970 if (!func->remove_attr) {
971 dbg(0, "Could not remove command-evaluation callback because remove_attr is missing for type %i!\n", cs->cbs[i].attr.type);
975 attr.u.callback = cs->cbs[i].cb;
977 func->remove_attr(cs->cbs[i].attr.u.data, &attr);
978 callback_destroy(cs->cbs[i].cb);
985 // Now, re-create all the callbacks
986 command_register_callbacks(cs);
990 command_register_callbacks(struct command_saved *cs)
992 struct attr prev,cb_attr,attr;
994 struct object_func *func;
998 cs->ctx.expr = cs->command;
999 cs->ctx.attr = &attr;
1002 while ((status = get_next_object(&cs->ctx, &cs->res)) != 0) {
1003 resolve(&cs->ctx, &cs->res, NULL);
1005 if (cs->ctx.error || (cs->res.attr.type == attr_none)) {
1006 // We could not resolve an object, perhaps because it has not been created
1010 if (prev.type != attr_none) {
1011 func = object_func_lookup(prev.type);
1013 if (func->add_attr) {
1014 if (status == 2) { // This is not the final attribute name
1015 cb = callback_new_attr_1(callback_cast(command_saved_callbacks_changed), cs->res.attr.type, (void*)cs);
1016 attr = cs->res.attr;
1017 } else if (status == 1) { // This is the final attribute name
1018 cb = callback_new_attr_1(callback_cast(command_saved_evaluate), cs->res.attr.type, (void*)cs);
1019 cs->ctx.attr = &cs->navit;
1021 dbg(0, "Error: Strange status returned from get_next_object()\n");
1025 cs->cbs = g_realloc(cs->cbs, (sizeof(struct command_saved_cb) * cs->num_cbs));
1026 cs->cbs[cs->num_cbs-1].cb = cb;
1027 cs->cbs[cs->num_cbs-1].attr = prev;
1029 cb_attr.u.callback = cb;
1030 cb_attr.type = attr_callback;
1032 func->add_attr(prev.u.data, &cb_attr);
1035 dbg(0, "Could not add callback because add_attr is missing for type %i}n", prev.type);
1040 prev = cs->res.attr;
1046 command_saved_evaluate_idle(cs);
1051 struct command_saved
1052 *command_saved_new(char *command, struct navit *navit, struct callback *cb)
1054 struct command_saved *ret;
1056 ret = g_new0(struct command_saved, 1);
1057 ret->command = g_strdup(command);
1058 ret->navit.u.navit = navit;
1059 ret->navit.type = attr_navit;
1061 ret->error = not_ready;
1063 if (!command_register_callbacks(ret)) {
1064 // We try this as an idle call again
1065 ret->register_cb = callback_new_1(callback_cast(command_saved_callbacks_changed), ret);
1066 ret->register_ev = event_add_idle(300, ret->register_cb);
1073 command_saved_destroy(struct command_saved *cs)
1075 g_free(cs->command);