17 #include "navit_nls.h"
18 #include "obj_filter.h"
21 gui.fullscreen=!gui.fullscreen
26 speech.active=!speech.active
29 osd[type=="xxx"].active=0;osd[type=="yyy"].active=0
50 struct command_saved_cb {
55 struct command_saved {
58 char *command; // The command string itself
59 struct event_idle *idle_ev; // Event to update this command
60 struct callback *idle_cb;
61 struct callback *register_cb; // Callback to register all the callbacks
62 struct event_idle *register_ev; // Idle event to register all the callbacks
65 struct command_saved_cb *cbs; // List of callbacks for this saved command
66 struct callback *cb; // Callback that should be called when we re-evaluate
72 no_error=0,missing_closing_brace, missing_colon, wrong_type, illegal_number_format, illegal_character, missing_closing_bracket, invalid_type, not_ready
75 static void eval_comma(struct context *ctx, struct result *res);
76 static struct attr ** eval_list(struct context *ctx);
79 result_free(struct result *res)
83 static int command_register_callbacks(struct command_saved *cs);
86 get_op(struct context *ctx, int test, ...)
92 while (g_ascii_isspace(*ctx->expr)) {
97 while ((op = va_arg(ap, char *))) {
98 if (!strncmp(ctx->expr, op, strlen(op))) {
101 ctx->expr+=strlen(op);
110 is_int(struct result *res)
116 is_double(struct result *res)
122 dump(struct result *res)
125 char object[res->varlen+1];
126 char attribute[res->attrnlen+1];
128 strncpy(object, res->var, res->varlen);
129 object[res->varlen]='\0';
131 strncpy(attribute, res->attrn, res->attrnlen);
132 attribute[res->attrnlen]='\0';
133 dbg(0,"type:%s\n", attr_to_name(res->attr.type));
134 dbg(0,"attribute '%s' from '%s'\n", attribute, object);
138 static enum attr_type
139 command_attr_type(struct result *res)
141 char *attrn=g_alloca(sizeof(char)*(res->attrnlen+1));
143 strncpy(attrn, res->attrn, res->attrnlen);
144 attrn[res->attrnlen]='\0';
145 return attr_from_name(attrn);
149 command_object_get_attr(struct context *ctx, struct attr *object, enum attr_type attr_type, struct attr *ret)
151 struct object_func *func=object_func_lookup(object->type);
152 if (!func || !func->get_attr)
154 return func->get_attr(object->u.data, attr_type, ret, NULL);
158 command_object_add_attr(struct context *ctx, struct attr *object, struct attr *attr)
160 struct object_func *func=object_func_lookup(object->type);
161 if (!func || !func->add_attr)
163 return func->add_attr(object->u.data, attr);
167 command_object_remove_attr(struct context *ctx, struct attr *object, struct attr *attr)
169 struct object_func *func=object_func_lookup(object->type);
170 if (!func || !func->remove_attr)
172 return func->remove_attr(object->u.data, attr);
177 command_get_attr(struct context *ctx, struct result *res)
180 enum attr_type attr_type=command_attr_type(res);
181 result=command_object_get_attr(ctx, &res->attr, attr_type, &res->attr);
184 res->varlen=res->attrnlen;
186 res->attr.type=attr_none;
196 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
199 enum attr_type attr_type=command_attr_type(res);
200 struct object_func *func=object_func_lookup(res->attr.type);
201 if (!func || !func->set_attr)
203 if (attr_type == attr_attr_types) {
204 char *attrn=g_alloca(sizeof(char)*(res->attrnlen+1));
206 strncpy(attrn, res->attrn, res->attrnlen);
207 attrn[res->attrnlen]='\0';
208 tmp=attr_new_from_text(attrn, newres->attr.u.str);
209 newres->attr.u.data=tmp->u.data;
212 newres->attr.type=attr_type;
213 result=func->set_attr(res->attr.u.data, &newres->attr);
218 resolve_object(struct context *ctx, struct result *res)
220 if (res->attr.type == attr_none && res->varlen) {
221 res->attr=*ctx->attr;
223 res->attrnlen=res->varlen;
230 resolve(struct context *ctx, struct result *res, struct attr *parent) //FIXME What is that parent for?
232 resolve_object(ctx, res);
234 command_get_attr(ctx, res);
238 get_double(struct context *ctx, struct result *res)
240 resolve(ctx, res, NULL);
247 get_int(struct context *ctx, struct result *res)
249 resolve(ctx, res, NULL);
250 if (res->attr.type == attr_none)
252 if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
253 return res->attr.u.num;
255 if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
256 return (int) (*res->attr.u.numd);
258 ctx->error=wrong_type;
264 get_string(struct context *ctx, struct result *res)
266 resolve(ctx, res, NULL);
267 return attr_to_text(&res->attr, NULL, 0);
271 set_double(struct context *ctx, struct result *res, double val)
278 set_int(struct context *ctx, struct result *res, int val)
281 res->attr.type=attr_type_int_begin;
287 eval_value(struct context *ctx, struct result *res) {
290 struct obj_filter_t out;
299 while (g_ascii_isspace(*op)) {
303 parsed_chars = parse_obj_filter(op, &out);
305 struct attr* res_attr = filter_object(ctx->attr, out.iterator_type, out.filter_expr, out.idx);
307 res->attr = *res_attr;
309 ctx->expr = op+parsed_chars;
314 if ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
315 res->attr.type=attr_none;
318 while ((op[0] >= 'a' && op[0] <= 'z') || (op[0] >= '0' && op[0] <= '9') || op[0] == '_') {
322 if (res->varlen == 3 && !strncmp(res->var,"new",3) && op[0] == ' ') {
331 if ((op[0] >= '0' && op[0] <= '9') ||
332 (op[0] == '.' && op[1] >= '0' && op[1] <= '9') ||
333 (op[0] == '-' && op[1] >= '0' && op[1] <= '9') ||
334 (op[0] == '-' && op[1] == '.' && op[2] >= '0' && op[2] <= '9')) {
335 while ((op[0] >= '0' && op[0] <= '9') || op[0] == '.' || (res->varlen == 0 && op[0] == '-')) {
339 ctx->error=illegal_number_format;
346 res->val = strtod(ctx->expr, NULL);
347 res->attr.type=attr_type_double_begin;
348 res->attr.u.numd=&res->val;
350 res->attr.type=attr_type_int_begin;
351 res->attr.u.num=atoi(ctx->expr);
359 } while (op[0] != '"');
360 res->attr.type=attr_type_string_begin;
362 res->attr.u.str=g_malloc(len+1);
363 strncpy(res->attr.u.str, ctx->expr+1, len);
364 res->attr.u.str[len]='\0';
369 ctx->error=illegal_character;
373 get_next_object(struct context *ctx, struct result *res) {
381 if ((res->attr.type == attr_none) && (res->varlen > 0)) {
382 if (ctx->expr[0] != '.') {
383 return 1; // 1 means "this is the final object name"
385 return 2; // 2 means "there are more object names following" (e.g. we just hit 'vehicle' in 'vehicle.position_speed'
390 // Probably hit an operator
399 eval_brace(struct context *ctx, struct result *res)
401 if (get_op(ctx,0,"(",NULL)) {
402 eval_comma(ctx, res);
403 if (ctx->error) return;
404 if (!get_op(ctx,0,")",NULL))
405 ctx->error=missing_closing_brace;
408 eval_value(ctx, res);
412 command_call_function(struct context *ctx, struct result *res)
414 struct attr cbl,**list=NULL;
415 char *function=g_alloca(sizeof(char)*(res->attrnlen+1));
417 strncpy(function, res->attrn, res->attrnlen);
418 function[res->attrnlen]='\0';
419 dbg(1,"function=%s\n", function);
420 if (ctx->expr[0] != ')') {
422 if (ctx->error) return;
424 if (!get_op(ctx,0,")",NULL)) {
425 ctx->error=missing_closing_brace;
428 if (!strcmp(function,"_") && list && list[0] && list[0]->type >= attr_type_string_begin && list[0]->type <= attr_type_string_end) {
429 res->attr.type=list[0]->type;
430 res->attr.u.str=g_strdup(gettext(list[0]->u.str));
432 } else if (!strncmp(function,"new ",4)) {
433 enum attr_type attr_type=attr_from_name(function+4);
434 if (attr_type != attr_none) {
435 struct object_func *func=object_func_lookup(attr_type);
436 if (func && func->create) {
437 res->attr.type=attr_type;
438 res->attr.u.data=func->create(list[0], list+1);
441 } else if (!strcmp(function,"add_attr")) {
442 command_object_add_attr(ctx, &res->attr, list[0]);
443 } else if (!strcmp(function,"remove_attr")) {
444 command_object_remove_attr(ctx, &res->attr, list[0]);
446 if (command_object_get_attr(ctx, &res->attr, attr_callback_list, &cbl)) {
448 struct attr **out=NULL;
449 dbg(1,"function call %s from %s\n",function, attr_to_name(res->attr.type));
450 callback_list_call_attr_4(cbl.u.callback_list, attr_command, function, list, &out, &valid);
452 dbg(0, "invalid command ignored: \"%s\"; see http://wiki.navit-project.org/index.php/"
453 "The_Navit_Command_Interface for valid commands.\n", function);
456 attr_dup_content(out[0], &res->attr);
459 res->attr.type=attr_none;
461 res->attr.type=attr_none;
470 eval_postfix(struct context *ctx, struct result *res)
475 eval_brace(ctx, res);
476 if (ctx->error) return;
478 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
481 eval_brace(ctx, &tmp);
482 if (ctx->error) return;
483 resolve(ctx, res,NULL);
484 if (ctx->error) return;
486 res->attrnlen=tmp.varlen;
488 } else if (op[0] == '[') {
489 if (!get_op(ctx,0,"]",NULL)) {
490 ctx->error=missing_closing_bracket;
493 } else if (op[0] == '(') {
494 dbg(1,"function call\n");
495 resolve_object(ctx, res);
496 command_call_function(ctx, res);
502 eval_unary(struct context *ctx, struct result *res)
505 op=get_op(ctx,0,"~","!",NULL);
507 eval_unary(ctx, res);
508 if (ctx->error) return;
510 set_int(ctx, res, ~get_int(ctx, res));
512 set_int(ctx, res, !get_int(ctx, res));
514 eval_postfix(ctx, res);
518 eval_multiplicative(struct context *ctx, struct result *res)
523 eval_unary(ctx, res);
524 if (ctx->error) return;
526 if (!(op=get_op(ctx,0,"*","/","%",NULL))) return;
527 eval_unary(ctx, &tmp);
528 if (ctx->error) return;
529 if (is_double(res) || is_double(&tmp)) {
531 set_double(ctx, res, get_double(ctx, res) * get_double(ctx, &tmp));
532 else if (op[0] == '/')
533 set_double(ctx, res, get_double(ctx, res) / get_double(ctx, &tmp));
535 ctx->error=invalid_type;
540 set_int(ctx, res, get_int(ctx, res) * get_int(ctx, &tmp));
541 else if (op[0] == '/')
542 set_int(ctx, res, get_int(ctx, res) / get_int(ctx, &tmp));
544 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
546 if (ctx->error) return;
551 eval_additive(struct context *ctx, struct result *res)
556 eval_multiplicative(ctx, res);
557 if (ctx->error) return;
559 if (!(op=get_op(ctx,0,"-","+",NULL))) return;
560 eval_multiplicative(ctx, &tmp);
561 if (ctx->error) return;
562 if (is_double(res) || is_double(&tmp)) {
564 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
566 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
569 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
571 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
573 if (ctx->error) return;
578 eval_equality(struct context *ctx, struct result *res)
583 eval_additive(ctx, res);
584 if (ctx->error) return;
586 if (!(op=get_op(ctx,0,"==","!=","<=",">=","<",">",NULL))) return;
587 eval_additive(ctx, &tmp);
588 if (ctx->error) return;
592 set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
595 set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
599 set_int(ctx, res, (get_int(ctx, res) <= get_int(ctx, &tmp)));
601 set_int(ctx, res, (get_int(ctx, res) < get_int(ctx, &tmp)));
606 set_int(ctx, res, (get_int(ctx, res) >= get_int(ctx, &tmp)));
608 set_int(ctx, res, (get_int(ctx, res) > get_int(ctx, &tmp)));
620 eval_bitwise_and(struct context *ctx, struct result *res)
624 eval_equality(ctx, res);
625 if (ctx->error) return;
627 if (get_op(ctx,1,"&&",NULL)) return;
628 if (!get_op(ctx,0,"&",NULL)) return;
629 eval_equality(ctx, &tmp);
630 if (ctx->error) return;
631 set_int(ctx, res, get_int(ctx, res) & get_int(ctx, &tmp));
632 if (ctx->error) return;
637 eval_bitwise_xor(struct context *ctx, struct result *res)
641 eval_bitwise_and(ctx, res);
642 if (ctx->error) return;
644 if (!get_op(ctx,0,"^",NULL)) return;
645 eval_bitwise_and(ctx, &tmp);
646 if (ctx->error) return;
647 set_int(ctx, res, get_int(ctx, res) ^ get_int(ctx, &tmp));
648 if (ctx->error) return;
653 eval_bitwise_or(struct context *ctx, struct result *res)
657 eval_bitwise_xor(ctx, res);
658 if (ctx->error) return;
660 if (get_op(ctx,1,"||",NULL)) return;
661 if (!get_op(ctx,0,"|",NULL)) return;
662 eval_bitwise_xor(ctx, &tmp);
663 if (ctx->error) return;
664 set_int(ctx, res, get_int(ctx, res) | get_int(ctx, &tmp));
665 if (ctx->error) return;
670 eval_logical_and(struct context *ctx, struct result *res)
674 eval_bitwise_or(ctx, res);
675 if (ctx->error) return;
677 if (!get_op(ctx,0,"&&",NULL)) return;
678 eval_bitwise_or(ctx, &tmp);
679 if (ctx->error) return;
680 set_int(ctx, res, get_int(ctx, res) && get_int(ctx, &tmp));
681 if (ctx->error) return;
686 eval_logical_or(struct context *ctx, struct result *res)
690 eval_logical_and(ctx, res);
691 if (ctx->error) return;
693 if (!get_op(ctx,0,"||",NULL)) return;
694 eval_logical_and(ctx, &tmp);
695 if (ctx->error) return;
696 set_int(ctx, res, get_int(ctx, res) || get_int(ctx, &tmp));
697 if (ctx->error) return;
702 eval_conditional(struct context *ctx, struct result *res)
707 eval_logical_or(ctx, res);
708 if (ctx->error) return;
709 if (!get_op(ctx,0,"?",NULL)) return;
710 cond=!!get_int(ctx, res);
711 if (ctx->error) return;
712 eval_logical_or(ctx, &tmp);
713 if (ctx->error) return;
716 if (!get_op(ctx,0,":",NULL)) {
717 ctx->error=missing_colon;
720 eval_logical_or(ctx, &tmp);
721 if (ctx->error) return;
726 /* = *= /= %= += -= >>= <<= &= ^= |= */
729 eval_assignment(struct context *ctx, struct result *res)
732 eval_conditional(ctx, res);
733 if (ctx->error) return;
734 if (!get_op(ctx,0,"=",NULL)) return;
735 eval_conditional(ctx, &tmp);
736 if (ctx->error) return;
737 resolve(ctx, &tmp, NULL);
738 if (ctx->error) return;
739 resolve_object(ctx, res);
740 command_set_attr(ctx, res, &tmp);
745 eval_comma(struct context *ctx, struct result *res)
749 eval_assignment(ctx, res);
750 if (ctx->error) return;
752 if (!get_op(ctx,0,",",NULL)) return;
753 eval_assignment(ctx, &tmp);
754 if (ctx->error) return;
759 static struct attr **
760 eval_list(struct context *ctx)
764 struct attr **ret=NULL;
766 eval_assignment(ctx, &tmp);
771 resolve(ctx, &tmp, NULL);
772 ret=attr_generic_add_attr(ret, &tmp.attr);
773 if (!get_op(ctx,0,",",NULL)) return ret;
779 void command(struct attr *attr, char *expr)
783 memset(&res, 0, sizeof(res));
784 memset(&ctx, 0, sizeof(ctx));
788 printf("command='%s'\n", expr);
789 eval_comma(&ctx,&res);
790 printf("err=%d %s\n", ctx.error, ctx.expr);
793 resolve(&ctx, &res, NULL);
795 printf("%s\n", get_string(&ctx, &res));
800 command_evaluate_to(struct attr *attr, const char *expr, struct context *ctx, struct result *res)
802 memset(res, 0, sizeof(*res));
803 memset(ctx, 0, sizeof(*ctx));
810 command_evaluate_to_attr(struct attr *attr, char *expr, int *error, struct attr *ret)
814 command_evaluate_to(attr, expr, &ctx, &res);
817 resolve_object(&ctx, &res);
819 dbg(0,"type %s\n",attr_to_name(command_attr_type(&res)));
820 return command_attr_type(&res);
824 command_evaluate_to_void(struct attr *attr, char *expr, int *error)
828 command_evaluate_to(attr, expr, &ctx, &res);
830 resolve(&ctx, &res, NULL);
837 command_evaluate_to_string(struct attr *attr, char *expr, int *error)
843 command_evaluate_to(attr, expr, &ctx, &res);
845 resolve(&ctx, &res, NULL);
847 ret=get_string(&ctx, &res);
857 command_evaluate_to_int(struct attr *attr, char *expr, int *error)
863 command_evaluate_to(attr, expr, &ctx, &res);
865 resolve(&ctx, &res, NULL);
867 ret=get_int(&ctx, &res);
877 command_evaluate_to_boolean(struct attr *attr, const char *expr, int *error)
883 command_evaluate_to(attr, expr, &ctx, &res);
885 resolve(&ctx, &res, NULL);
887 if (res.attr.type == attr_none)
889 else if ((res.attr.type >= attr_type_int_begin && res.attr.type <= attr_type_int_end) ||
890 (res.attr.type >= attr_type_double_begin && res.attr.type <= attr_type_double_end))
891 ret=get_int(&ctx, &res);
893 ret=res.attr.u.data != NULL;
904 command_evaluate(struct attr *attr, const char *expr)
906 /* Once the eval has started we can't rely anymore on the content of
907 * expr which may be freed when the calling widget is destroyed by a
908 * subsequent command call. Hence the g_strdup. */
913 memset(&res, 0, sizeof(res));
914 memset(&ctx, 0, sizeof(ctx));
917 ctx.expr=expr_dup=g_strdup(expr);
919 eval_comma(&ctx,&res);
922 resolve(&ctx, &res, NULL);
925 if (!get_op(&ctx,0,";",NULL))
933 command_interpreter(struct attr *attr)
938 size=read(0, buffer, 4095);
943 command(attr, buffer);
949 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
952 for (i = 0 ; i < count ; i++) {
953 if (!strcmp(command,table->command)) {
956 table->func(data, command, in, out);
963 command_add_table_attr(struct command_table *table, int count, void *data, struct attr *attr)
965 attr->type=attr_callback;
966 attr->u.callback=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
970 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
973 command_add_table_attr(table, count, data, &attr);
974 callback_list_add(cbl, attr.u.callback);
978 command_saved_set_cb (struct command_saved *cs, struct callback *cb)
984 command_saved_get_int (struct command_saved *cs)
986 return get_int(&cs->ctx, &cs->res);
990 command_saved_error (struct command_saved *cs)
996 command_saved_evaluate_idle (struct command_saved *cs)
998 // Only run once at a time
1000 event_remove_idle(cs->idle_ev);
1004 command_evaluate_to(&cs->navit, cs->command, &cs->ctx, &cs->res);
1006 if (!cs->ctx.error) {
1010 callback_call_1(cs->cb, cs);
1013 cs->error = cs->ctx.error;
1018 command_saved_evaluate(struct command_saved *cs)
1021 command_saved_evaluate_idle(cs);
1025 // We're already scheduled for reevaluation
1030 cs->idle_cb = callback_new_1(callback_cast(command_saved_evaluate_idle), cs);
1033 cs->idle_ev = event_add_idle(100, cs->idle_cb);
1037 command_saved_callbacks_changed(struct command_saved *cs)
1039 // For now, we delete each and every callback and then re-create them
1041 struct object_func *func;
1044 if (cs->register_ev) {
1045 event_remove_idle(cs->register_ev);
1046 cs->register_ev = NULL;
1049 attr.type = attr_callback;
1051 for (i = 0; i < cs->num_cbs; i++) {
1052 func = object_func_lookup(cs->cbs[i].attr.type);
1054 if (!func->remove_attr) {
1055 dbg(0, "Could not remove command-evaluation callback because remove_attr is missing for type %i!\n", cs->cbs[i].attr.type);
1059 attr.u.callback = cs->cbs[i].cb;
1061 func->remove_attr(cs->cbs[i].attr.u.data, &attr);
1062 callback_destroy(cs->cbs[i].cb);
1069 // Now, re-create all the callbacks
1070 command_register_callbacks(cs);
1074 command_register_callbacks(struct command_saved *cs)
1076 struct attr prev,cb_attr,attr;
1078 struct object_func *func;
1079 struct callback *cb;
1082 cs->ctx.expr = cs->command;
1083 cs->ctx.attr = &attr;
1086 while ((status = get_next_object(&cs->ctx, &cs->res)) != 0) {
1087 resolve(&cs->ctx, &cs->res, NULL);
1089 if (cs->ctx.error || (cs->res.attr.type == attr_none)) {
1090 // We could not resolve an object, perhaps because it has not been created
1094 if (prev.type != attr_none) {
1095 func = object_func_lookup(prev.type);
1097 if (func->add_attr) {
1098 if (status == 2) { // This is not the final attribute name
1099 cb = callback_new_attr_1(callback_cast(command_saved_callbacks_changed), cs->res.attr.type, (void*)cs);
1100 attr = cs->res.attr;
1101 } else if (status == 1) { // This is the final attribute name
1102 cb = callback_new_attr_1(callback_cast(command_saved_evaluate), cs->res.attr.type, (void*)cs);
1103 cs->ctx.attr = &cs->navit;
1105 dbg(0, "Error: Strange status returned from get_next_object()\n");
1109 cs->cbs = g_realloc(cs->cbs, (sizeof(struct command_saved_cb) * cs->num_cbs));
1110 cs->cbs[cs->num_cbs-1].cb = cb;
1111 cs->cbs[cs->num_cbs-1].attr = prev;
1113 cb_attr.u.callback = cb;
1114 cb_attr.type = attr_callback;
1116 func->add_attr(prev.u.data, &cb_attr);
1119 dbg(0, "Could not add callback because add_attr is missing for type %i}n", prev.type);
1124 prev = cs->res.attr;
1130 command_saved_evaluate_idle(cs);
1135 struct command_saved *
1136 command_saved_new(char *command, struct navit *navit, struct callback *cb, int async)
1138 struct command_saved *ret;
1140 ret = g_new0(struct command_saved, 1);
1141 ret->command = g_strdup(command);
1142 ret->navit.u.navit = navit;
1143 ret->navit.type = attr_navit;
1145 ret->error = not_ready;
1148 if (!command_register_callbacks(ret)) {
1149 // We try this as an idle call again
1150 ret->register_cb = callback_new_1(callback_cast(command_saved_callbacks_changed), ret);
1151 ret->register_ev = event_add_idle(300, ret->register_cb);
1158 command_saved_destroy(struct command_saved *cs)
1160 g_free(cs->command);