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
71 no_error=0,missing_closing_brace, missing_colon, wrong_type, illegal_number_format, illegal_character, missing_closing_bracket, invalid_type, not_ready
74 static void eval_comma(struct context *ctx, struct result *res);
75 static struct attr ** eval_list(struct context *ctx);
78 result_free(struct result *res)
82 static int command_register_callbacks(struct command_saved *cs);
85 get_op(struct context *ctx, int test, ...)
91 while (g_ascii_isspace(*ctx->expr)) {
96 while ((op = va_arg(ap, char *))) {
97 if (!strncmp(ctx->expr, op, strlen(op))) {
100 ctx->expr+=strlen(op);
109 is_int(struct result *res)
115 is_double(struct result *res)
121 dump(struct result *res)
124 char object[res->varlen+1];
125 char attribute[res->attrnlen+1];
127 strncpy(object, res->var, res->varlen);
128 object[res->varlen]='\0';
130 strncpy(attribute, res->attrn, res->attrnlen);
131 attribute[res->attrnlen]='\0';
132 dbg(0,"type:%s\n", attr_to_name(res->attr.type));
133 dbg(0,"attribute '%s' from '%s'\n", attribute, object);
137 static enum attr_type
138 command_attr_type(struct result *res)
140 char *attrn=g_alloca(sizeof(char)*(res->attrnlen+1));
142 strncpy(attrn, res->attrn, res->attrnlen);
143 attrn[res->attrnlen]='\0';
144 return attr_from_name(attrn);
148 command_object_get_attr(struct context *ctx, struct attr *object, enum attr_type attr_type, struct attr *ret)
150 struct object_func *func=object_func_lookup(object->type);
151 if (!func || !func->get_attr)
153 return func->get_attr(object->u.data, attr_type, ret, NULL);
157 command_object_add_attr(struct context *ctx, struct attr *object, struct attr *attr)
159 struct object_func *func=object_func_lookup(object->type);
160 if (!func || !func->add_attr)
162 return func->add_attr(object->u.data, attr);
166 command_object_remove_attr(struct context *ctx, struct attr *object, struct attr *attr)
168 struct object_func *func=object_func_lookup(object->type);
169 if (!func || !func->remove_attr)
171 return func->remove_attr(object->u.data, attr);
176 command_get_attr(struct context *ctx, struct result *res)
179 enum attr_type attr_type=command_attr_type(res);
180 result=command_object_get_attr(ctx, &res->attr, attr_type, &res->attr);
183 res->varlen=res->attrnlen;
185 res->attr.type=attr_none;
195 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
198 enum attr_type attr_type=command_attr_type(res);
199 struct object_func *func=object_func_lookup(res->attr.type);
200 if (!func || !func->set_attr)
202 if (attr_type == attr_attr_types) {
203 char *attrn=g_alloca(sizeof(char)*(res->attrnlen+1));
205 strncpy(attrn, res->attrn, res->attrnlen);
206 attrn[res->attrnlen]='\0';
207 tmp=attr_new_from_text(attrn, newres->attr.u.str);
208 newres->attr.u.data=tmp->u.data;
211 newres->attr.type=attr_type;
212 result=func->set_attr(res->attr.u.data, &newres->attr);
217 resolve_object(struct context *ctx, struct result *res)
219 if (res->attr.type == attr_none && res->varlen) {
220 res->attr=*ctx->attr;
222 res->attrnlen=res->varlen;
229 resolve(struct context *ctx, struct result *res, struct attr *parent) //FIXME What is that parent for?
231 resolve_object(ctx, res);
233 command_get_attr(ctx, res);
237 get_double(struct context *ctx, struct result *res)
239 resolve(ctx, res, NULL);
246 get_int(struct context *ctx, struct result *res)
248 resolve(ctx, res, NULL);
249 if (res->attr.type == attr_none)
251 if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
252 return res->attr.u.num;
254 if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
255 return (int) (*res->attr.u.numd);
257 ctx->error=wrong_type;
263 get_string(struct context *ctx, struct result *res)
265 resolve(ctx, res, NULL);
266 return attr_to_text(&res->attr, NULL, 0);
270 set_double(struct context *ctx, struct result *res, double val)
277 set_int(struct context *ctx, struct result *res, int val)
280 res->attr.type=attr_type_int_begin;
286 eval_value(struct context *ctx, struct result *res) {
289 struct obj_filter_t out;
298 while (g_ascii_isspace(*op)) {
302 parsed_chars = parse_obj_filter(op, &out);
304 struct attr* res_attr = filter_object(ctx->attr, out.iterator_type, out.filter_expr, out.idx);
306 res->attr = *res_attr;
308 ctx->expr = op+parsed_chars;
313 if ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
314 res->attr.type=attr_none;
317 while ((op[0] >= 'a' && op[0] <= 'z') || (op[0] >= '0' && op[0] <= '9') || op[0] == '_') {
321 if (res->varlen == 3 && !strncmp(res->var,"new",3) && op[0] == ' ') {
330 if ((op[0] >= '0' && op[0] <= '9') ||
331 (op[0] == '.' && op[1] >= '0' && op[1] <= '9') ||
332 (op[0] == '-' && op[1] >= '0' && op[1] <= '9') ||
333 (op[0] == '-' && op[1] == '.' && op[2] >= '0' && op[2] <= '9')) {
334 while ((op[0] >= '0' && op[0] <= '9') || op[0] == '.' || (res->varlen == 0 && op[0] == '-')) {
338 ctx->error=illegal_number_format;
345 res->val = strtod(ctx->expr, NULL);
346 res->attr.type=attr_type_double_begin;
347 res->attr.u.numd=&res->val;
349 res->attr.type=attr_type_int_begin;
350 res->attr.u.num=atoi(ctx->expr);
358 } while (op[0] != '"');
359 res->attr.type=attr_type_string_begin;
361 res->attr.u.str=g_malloc(len+1);
362 strncpy(res->attr.u.str, ctx->expr+1, len);
363 res->attr.u.str[len]='\0';
368 ctx->error=illegal_character;
372 get_next_object(struct context *ctx, struct result *res) {
380 if ((res->attr.type == attr_none) && (res->varlen > 0)) {
381 if (ctx->expr[0] != '.') {
382 return 1; // 1 means "this is the final object name"
384 return 2; // 2 means "there are more object names following" (e.g. we just hit 'vehicle' in 'vehicle.position_speed'
389 // Probably hit an operator
398 eval_brace(struct context *ctx, struct result *res)
400 if (get_op(ctx,0,"(",NULL)) {
401 eval_comma(ctx, res);
402 if (ctx->error) return;
403 if (!get_op(ctx,0,")",NULL))
404 ctx->error=missing_closing_brace;
407 eval_value(ctx, res);
411 command_call_function(struct context *ctx, struct result *res)
413 struct attr cbl,**list=NULL;
414 char *function=g_alloca(sizeof(char)*(res->attrnlen+1));
416 strncpy(function, res->attrn, res->attrnlen);
417 function[res->attrnlen]='\0';
418 dbg(1,"function=%s\n", function);
419 if (ctx->expr[0] != ')') {
421 if (ctx->error) return;
423 if (!get_op(ctx,0,")",NULL)) {
424 ctx->error=missing_closing_brace;
427 if (!strcmp(function,"_") && list && list[0] && list[0]->type >= attr_type_string_begin && list[0]->type <= attr_type_string_end) {
428 res->attr.type=list[0]->type;
429 res->attr.u.str=g_strdup(gettext(list[0]->u.str));
431 } else if (!strncmp(function,"new ",4)) {
432 enum attr_type attr_type=attr_from_name(function+4);
433 if (attr_type != attr_none) {
434 struct object_func *func=object_func_lookup(attr_type);
435 if (func && func->create) {
436 res->attr.type=attr_type;
437 res->attr.u.data=func->create(list[0], list+1);
440 } else if (!strcmp(function,"add_attr")) {
441 command_object_add_attr(ctx, &res->attr, list[0]);
442 } else if (!strcmp(function,"remove_attr")) {
443 command_object_remove_attr(ctx, &res->attr, list[0]);
445 if (command_object_get_attr(ctx, &res->attr, attr_callback_list, &cbl)) {
447 struct attr **out=NULL;
448 dbg(1,"function call %s from %s\n",function, attr_to_name(res->attr.type));
449 callback_list_call_attr_4(cbl.u.callback_list, attr_command, function, list, &out, &valid);
451 dbg(0, "invalid command ignored: \"%s\"; see http://wiki.navit-project.org/index.php/"
452 "The_Navit_Command_Interface for valid commands.\n", function);
455 attr_dup_content(out[0], &res->attr);
458 res->attr.type=attr_none;
460 res->attr.type=attr_none;
469 eval_postfix(struct context *ctx, struct result *res)
474 eval_brace(ctx, res);
475 if (ctx->error) return;
477 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
480 eval_brace(ctx, &tmp);
481 if (ctx->error) return;
482 resolve(ctx, res,NULL);
483 if (ctx->error) return;
485 res->attrnlen=tmp.varlen;
487 } else if (op[0] == '[') {
488 if (!get_op(ctx,0,"]",NULL)) {
489 ctx->error=missing_closing_bracket;
492 } else if (op[0] == '(') {
493 dbg(1,"function call\n");
494 resolve_object(ctx, res);
495 command_call_function(ctx, res);
501 eval_unary(struct context *ctx, struct result *res)
504 op=get_op(ctx,0,"~","!",NULL);
506 eval_unary(ctx, res);
507 if (ctx->error) return;
509 set_int(ctx, res, ~get_int(ctx, res));
511 set_int(ctx, res, !get_int(ctx, res));
513 eval_postfix(ctx, res);
517 eval_multiplicative(struct context *ctx, struct result *res)
522 eval_unary(ctx, res);
523 if (ctx->error) return;
525 if (!(op=get_op(ctx,0,"*","/","%",NULL))) return;
526 eval_unary(ctx, &tmp);
527 if (ctx->error) return;
528 if (is_double(res) || is_double(&tmp)) {
530 set_double(ctx, res, get_double(ctx, res) * get_double(ctx, &tmp));
531 else if (op[0] == '/')
532 set_double(ctx, res, get_double(ctx, res) / get_double(ctx, &tmp));
534 ctx->error=invalid_type;
539 set_int(ctx, res, get_int(ctx, res) * get_int(ctx, &tmp));
540 else if (op[0] == '/')
541 set_int(ctx, res, get_int(ctx, res) / get_int(ctx, &tmp));
543 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
545 if (ctx->error) return;
550 eval_additive(struct context *ctx, struct result *res)
555 eval_multiplicative(ctx, res);
556 if (ctx->error) return;
558 if (!(op=get_op(ctx,0,"-","+",NULL))) return;
559 eval_multiplicative(ctx, &tmp);
560 if (ctx->error) return;
561 if (is_double(res) || is_double(&tmp)) {
563 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
565 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
568 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
570 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
572 if (ctx->error) return;
577 eval_equality(struct context *ctx, struct result *res)
582 eval_additive(ctx, res);
583 if (ctx->error) return;
585 if (!(op=get_op(ctx,0,"==","!=","<=",">=","<",">",NULL))) return;
586 eval_additive(ctx, &tmp);
587 if (ctx->error) return;
591 set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
594 set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
598 set_int(ctx, res, (get_int(ctx, res) <= get_int(ctx, &tmp)));
600 set_int(ctx, res, (get_int(ctx, res) < get_int(ctx, &tmp)));
605 set_int(ctx, res, (get_int(ctx, res) >= get_int(ctx, &tmp)));
607 set_int(ctx, res, (get_int(ctx, res) > get_int(ctx, &tmp)));
619 eval_bitwise_and(struct context *ctx, struct result *res)
623 eval_equality(ctx, res);
624 if (ctx->error) return;
626 if (get_op(ctx,1,"&&",NULL)) return;
627 if (!get_op(ctx,0,"&",NULL)) return;
628 eval_equality(ctx, &tmp);
629 if (ctx->error) return;
630 set_int(ctx, res, get_int(ctx, res) & get_int(ctx, &tmp));
631 if (ctx->error) return;
636 eval_bitwise_xor(struct context *ctx, struct result *res)
640 eval_bitwise_and(ctx, res);
641 if (ctx->error) return;
643 if (!get_op(ctx,0,"^",NULL)) return;
644 eval_bitwise_and(ctx, &tmp);
645 if (ctx->error) return;
646 set_int(ctx, res, get_int(ctx, res) ^ get_int(ctx, &tmp));
647 if (ctx->error) return;
652 eval_bitwise_or(struct context *ctx, struct result *res)
656 eval_bitwise_xor(ctx, res);
657 if (ctx->error) return;
659 if (get_op(ctx,1,"||",NULL)) return;
660 if (!get_op(ctx,0,"|",NULL)) return;
661 eval_bitwise_xor(ctx, &tmp);
662 if (ctx->error) return;
663 set_int(ctx, res, get_int(ctx, res) | get_int(ctx, &tmp));
664 if (ctx->error) return;
669 eval_logical_and(struct context *ctx, struct result *res)
673 eval_bitwise_or(ctx, res);
674 if (ctx->error) return;
676 if (!get_op(ctx,0,"&&",NULL)) return;
677 eval_bitwise_or(ctx, &tmp);
678 if (ctx->error) return;
679 set_int(ctx, res, get_int(ctx, res) && get_int(ctx, &tmp));
680 if (ctx->error) return;
685 eval_logical_or(struct context *ctx, struct result *res)
689 eval_logical_and(ctx, res);
690 if (ctx->error) return;
692 if (!get_op(ctx,0,"||",NULL)) return;
693 eval_logical_and(ctx, &tmp);
694 if (ctx->error) return;
695 set_int(ctx, res, get_int(ctx, res) || get_int(ctx, &tmp));
696 if (ctx->error) return;
701 eval_conditional(struct context *ctx, struct result *res)
706 eval_logical_or(ctx, res);
707 if (ctx->error) return;
708 if (!get_op(ctx,0,"?",NULL)) return;
709 cond=!!get_int(ctx, res);
710 if (ctx->error) return;
711 eval_logical_or(ctx, &tmp);
712 if (ctx->error) return;
715 if (!get_op(ctx,0,":",NULL)) {
716 ctx->error=missing_colon;
719 eval_logical_or(ctx, &tmp);
720 if (ctx->error) return;
725 /* = *= /= %= += -= >>= <<= &= ^= |= */
728 eval_assignment(struct context *ctx, struct result *res)
731 eval_conditional(ctx, res);
732 if (ctx->error) return;
733 if (!get_op(ctx,0,"=",NULL)) return;
734 eval_conditional(ctx, &tmp);
735 if (ctx->error) return;
736 resolve(ctx, &tmp, NULL);
737 if (ctx->error) return;
738 resolve_object(ctx, res);
739 command_set_attr(ctx, res, &tmp);
744 eval_comma(struct context *ctx, struct result *res)
748 eval_assignment(ctx, res);
749 if (ctx->error) return;
751 if (!get_op(ctx,0,",",NULL)) return;
752 eval_assignment(ctx, &tmp);
753 if (ctx->error) return;
758 static struct attr **
759 eval_list(struct context *ctx)
763 struct attr **ret=NULL;
765 eval_assignment(ctx, &tmp);
770 resolve(ctx, &tmp, NULL);
771 ret=attr_generic_add_attr(ret, &tmp.attr);
772 if (!get_op(ctx,0,",",NULL)) return ret;
778 void command(struct attr *attr, char *expr)
782 memset(&res, 0, sizeof(res));
783 memset(&ctx, 0, sizeof(ctx));
787 printf("command='%s'\n", expr);
788 eval_comma(&ctx,&res);
789 printf("err=%d %s\n", ctx.error, ctx.expr);
792 resolve(&ctx, &res, NULL);
794 printf("%s\n", get_string(&ctx, &res));
799 command_evaluate_to(struct attr *attr, const char *expr, struct context *ctx, struct result *res)
801 memset(res, 0, sizeof(*res));
802 memset(ctx, 0, sizeof(*ctx));
809 command_evaluate_to_attr(struct attr *attr, char *expr, int *error, struct attr *ret)
813 command_evaluate_to(attr, expr, &ctx, &res);
816 resolve_object(&ctx, &res);
818 dbg(0,"type %s\n",attr_to_name(command_attr_type(&res)));
819 return command_attr_type(&res);
823 command_evaluate_to_void(struct attr *attr, char *expr, int *error)
827 command_evaluate_to(attr, expr, &ctx, &res);
829 resolve(&ctx, &res, NULL);
836 command_evaluate_to_string(struct attr *attr, char *expr, int *error)
842 command_evaluate_to(attr, expr, &ctx, &res);
844 resolve(&ctx, &res, NULL);
846 ret=get_string(&ctx, &res);
856 command_evaluate_to_int(struct attr *attr, char *expr, int *error)
862 command_evaluate_to(attr, expr, &ctx, &res);
864 resolve(&ctx, &res, NULL);
866 ret=get_int(&ctx, &res);
876 command_evaluate_to_boolean(struct attr *attr, const char *expr, int *error)
882 command_evaluate_to(attr, expr, &ctx, &res);
884 resolve(&ctx, &res, NULL);
886 if (res.attr.type == attr_none)
888 else if ((res.attr.type >= attr_type_int_begin && res.attr.type <= attr_type_int_end) ||
889 (res.attr.type >= attr_type_double_begin && res.attr.type <= attr_type_double_end))
890 ret=get_int(&ctx, &res);
892 ret=res.attr.u.data != NULL;
903 command_evaluate(struct attr *attr, const char *expr)
905 /* Once the eval has started we can't rely anymore on the content of
906 * expr which may be freed when the calling widget is destroyed by a
907 * subsequent command call. Hence the g_strdup. */
912 memset(&res, 0, sizeof(res));
913 memset(&ctx, 0, sizeof(ctx));
916 ctx.expr=expr_dup=g_strdup(expr);
918 eval_comma(&ctx,&res);
921 resolve(&ctx, &res, NULL);
924 if (!get_op(&ctx,0,";",NULL))
932 command_interpreter(struct attr *attr)
937 size=read(0, buffer, 4095);
942 command(attr, buffer);
948 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
951 for (i = 0 ; i < count ; i++) {
952 if (!strcmp(command,table->command)) {
955 table->func(data, command, in, out);
962 command_add_table_attr(struct command_table *table, int count, void *data, struct attr *attr)
964 attr->type=attr_callback;
965 attr->u.callback=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
969 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
972 command_add_table_attr(table, count, data, &attr);
973 callback_list_add(cbl, attr.u.callback);
977 command_saved_set_cb (struct command_saved *cs, struct callback *cb)
983 command_saved_get_int (struct command_saved *cs)
985 return get_int(&cs->ctx, &cs->res);
989 command_saved_error (struct command_saved *cs)
995 command_saved_evaluate_idle (struct command_saved *cs)
997 // Only run once at a time
999 event_remove_idle(cs->idle_ev);
1003 command_evaluate_to(&cs->navit, cs->command, &cs->ctx, &cs->res);
1005 if (!cs->ctx.error) {
1009 callback_call_1(cs->cb, cs);
1012 cs->error = cs->ctx.error;
1017 command_saved_evaluate(struct command_saved *cs)
1020 // We're already scheduled for reevaluation
1025 cs->idle_cb = callback_new_1(callback_cast(command_saved_evaluate_idle), cs);
1028 cs->idle_ev = event_add_idle(100, cs->idle_cb);
1032 command_saved_callbacks_changed(struct command_saved *cs)
1034 // For now, we delete each and every callback and then re-create them
1036 struct object_func *func;
1039 if (cs->register_ev) {
1040 event_remove_idle(cs->register_ev);
1041 cs->register_ev = NULL;
1044 attr.type = attr_callback;
1046 for (i = 0; i < cs->num_cbs; i++) {
1047 func = object_func_lookup(cs->cbs[i].attr.type);
1049 if (!func->remove_attr) {
1050 dbg(0, "Could not remove command-evaluation callback because remove_attr is missing for type %i!\n", cs->cbs[i].attr.type);
1054 attr.u.callback = cs->cbs[i].cb;
1056 func->remove_attr(cs->cbs[i].attr.u.data, &attr);
1057 callback_destroy(cs->cbs[i].cb);
1064 // Now, re-create all the callbacks
1065 command_register_callbacks(cs);
1069 command_register_callbacks(struct command_saved *cs)
1071 struct attr prev,cb_attr,attr;
1073 struct object_func *func;
1074 struct callback *cb;
1077 cs->ctx.expr = cs->command;
1078 cs->ctx.attr = &attr;
1081 while ((status = get_next_object(&cs->ctx, &cs->res)) != 0) {
1082 resolve(&cs->ctx, &cs->res, NULL);
1084 if (cs->ctx.error || (cs->res.attr.type == attr_none)) {
1085 // We could not resolve an object, perhaps because it has not been created
1089 if (prev.type != attr_none) {
1090 func = object_func_lookup(prev.type);
1092 if (func->add_attr) {
1093 if (status == 2) { // This is not the final attribute name
1094 cb = callback_new_attr_1(callback_cast(command_saved_callbacks_changed), cs->res.attr.type, (void*)cs);
1095 attr = cs->res.attr;
1096 } else if (status == 1) { // This is the final attribute name
1097 cb = callback_new_attr_1(callback_cast(command_saved_evaluate), cs->res.attr.type, (void*)cs);
1098 cs->ctx.attr = &cs->navit;
1100 dbg(0, "Error: Strange status returned from get_next_object()\n");
1104 cs->cbs = g_realloc(cs->cbs, (sizeof(struct command_saved_cb) * cs->num_cbs));
1105 cs->cbs[cs->num_cbs-1].cb = cb;
1106 cs->cbs[cs->num_cbs-1].attr = prev;
1108 cb_attr.u.callback = cb;
1109 cb_attr.type = attr_callback;
1111 func->add_attr(prev.u.data, &cb_attr);
1114 dbg(0, "Could not add callback because add_attr is missing for type %i}n", prev.type);
1119 prev = cs->res.attr;
1125 command_saved_evaluate_idle(cs);
1130 struct command_saved
1131 *command_saved_new(char *command, struct navit *navit, struct callback *cb)
1133 struct command_saved *ret;
1135 ret = g_new0(struct command_saved, 1);
1136 ret->command = g_strdup(command);
1137 ret->navit.u.navit = navit;
1138 ret->navit.type = attr_navit;
1140 ret->error = not_ready;
1142 if (!command_register_callbacks(ret)) {
1143 // We try this as an idle call again
1144 ret->register_cb = callback_new_1(callback_cast(command_saved_callbacks_changed), ret);
1145 ret->register_ev = event_add_idle(300, ret->register_cb);
1152 command_saved_destroy(struct command_saved *cs)
1154 g_free(cs->command);