24 speech.active=!speech.active
27 osd[type=="xxx"].active=0;osd[type=="yyy"].active=0
48 struct command_saved_cb {
53 struct command_saved {
56 char *command; // The command string itself
57 struct event_idle *idle_ev; // Event to update this command
58 struct callback *idle_cb;
59 struct callback *register_cb; // Callback to register all the callbacks
60 struct event_idle *register_ev; // Idle event to register all the callbacks
63 struct command_saved_cb *cbs; // List of callbacks for this saved command
64 struct callback *cb; // Callback that should be called when we re-evaluate
69 no_error=0,missing_closing_brace, missing_colon, wrong_type, illegal_number_format, illegal_character, missing_closing_bracket, invalid_type, not_ready
72 static void eval_comma(struct context *ctx, struct result *res);
73 static struct attr ** eval_list(struct context *ctx);
76 result_free(struct result *res)
80 static int command_register_callbacks(struct command_saved *cs);
83 get_op(struct context *ctx, int test, ...)
88 while (g_ascii_isspace(*ctx->expr)) {
93 while ((op = va_arg(ap, char *))) {
94 if (!strncmp(ctx->expr, op, strlen(op))) {
97 ctx->expr+=strlen(op);
106 is_int(struct result *res)
112 is_double(struct result *res)
118 dump(struct result *res)
120 char object[res->varlen+1];
121 char attribute[res->attrnlen+1];
123 strncpy(object, res->var, res->varlen);
124 object[res->varlen]='\0';
126 strncpy(attribute, res->attrn, res->attrnlen);
127 attribute[res->attrnlen]='\0';
128 dbg(0,"type:%s\n", attr_to_name(res->attr.type));
129 dbg(0,"attribute '%s' from '%s'\n", attribute, object);
132 static enum attr_type
133 command_attr_type(struct result *res)
135 char attrn[res->attrnlen+1];
137 strncpy(attrn, res->attrn, res->attrnlen);
138 attrn[res->attrnlen]='\0';
139 return attr_from_name(attrn);
143 command_object_get_attr(struct context *ctx, struct attr *object, enum attr_type attr_type, struct attr *ret)
145 struct object_func *func=object_func_lookup(object->type);
146 if (!func || !func->get_attr)
148 return func->get_attr(object->u.data, attr_type, ret, NULL);
152 command_get_attr(struct context *ctx, struct result *res)
155 enum attr_type attr_type=command_attr_type(res);
156 result=command_object_get_attr(ctx, &res->attr, attr_type, &res->attr);
159 res->varlen=res->attrnlen;
161 res->attr.type=attr_none;
171 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
174 enum attr_type attr_type=command_attr_type(res);
175 struct object_func *func=object_func_lookup(res->attr.type);
176 if (!func || !func->set_attr)
178 newres->attr.type=attr_type;
179 result=func->set_attr(res->attr.u.data, &newres->attr);
184 resolve_object(struct context *ctx, struct result *res)
186 if (res->attr.type == attr_none && res->varlen) {
187 res->attr=*ctx->attr;
189 res->attrnlen=res->varlen;
196 resolve(struct context *ctx, struct result *res, struct attr *parent) //FIXME What is that parent for?
198 resolve_object(ctx, res);
200 command_get_attr(ctx, res);
204 get_double(struct context *ctx, struct result *res)
206 resolve(ctx, res, NULL);
213 get_int(struct context *ctx, struct result *res)
215 resolve(ctx, res, NULL);
216 if (res->attr.type == attr_none)
218 if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
219 return res->attr.u.num;
221 if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
222 return (int) (*res->attr.u.numd);
224 ctx->error=wrong_type;
230 get_string(struct context *ctx, struct result *res)
232 resolve(ctx, res, NULL);
233 return attr_to_text(&res->attr, NULL, 0);
237 set_double(struct context *ctx, struct result *res, double val)
244 set_int(struct context *ctx, struct result *res, int val)
247 res->attr.type=attr_type_int_begin;
253 eval_value(struct context *ctx, struct result *res) {
262 while (g_ascii_isspace(*op)) {
266 if (op[0] >= 'a' && op[0] <= 'z') {
267 res->attr.type=attr_none;
269 while ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
276 if ((op[0] >= '0' && op[0] <= '9') ||
277 (op[0] == '.' && op[1] >= '0' && op[1] <= '9') ||
278 (op[0] == '-' && op[1] >= '0' && op[1] <= '9') ||
279 (op[0] == '-' && op[1] == '.' && op[2] >= '0' && op[2] <= '9')) {
280 while ((op[0] >= '0' && op[0] <= '9') || op[0] == '.' || (res->varlen == 0 && op[0] == '-')) {
284 ctx->error=illegal_number_format;
291 res->val = strtod(ctx->expr, NULL);
292 res->attr.type=attr_type_double_begin;
293 res->attr.u.numd=&res->val;
295 res->attr.type=attr_type_int_begin;
296 res->attr.u.num=atoi(ctx->expr);
304 } while (op[0] != '"');
305 res->attr.type=attr_type_string_begin;
307 res->attr.u.str=g_malloc(len+1);
308 strncpy(res->attr.u.str, ctx->expr+1, len);
309 res->attr.u.str[len]='\0';
314 ctx->error=illegal_character;
318 get_next_object(struct context *ctx, struct result *res) {
326 if ((res->attr.type == attr_none) && (res->varlen > 0)) {
327 if (ctx->expr[0] != '.') {
328 return 1; // 1 means "this is the final object name"
330 return 2; // 2 means "there are more object names following" (e.g. we just hit 'vehicle' in 'vehicle.position_speed'
335 // Probably hit an operator
344 eval_brace(struct context *ctx, struct result *res)
346 if (get_op(ctx,0,"(",NULL)) {
347 eval_comma(ctx, res);
348 if (ctx->error) return;
349 if (!get_op(ctx,0,")",NULL))
350 ctx->error=missing_closing_brace;
353 eval_value(ctx, res);
357 command_call_function(struct context *ctx, struct result *res)
359 struct attr cbl,**list=NULL;
360 char function[res->attrnlen+1];
362 strncpy(function, res->attrn, res->attrnlen);
363 function[res->attrnlen]='\0';
364 dbg(0,"function=%s\n", function);
365 if (ctx->expr[0] != ')') {
367 if (ctx->error) return;
369 if (!get_op(ctx,0,")",NULL)) {
370 ctx->error=missing_closing_brace;
373 if (command_object_get_attr(ctx, &res->attr, attr_callback_list, &cbl)) {
375 dbg(0,"function call %s from %s\n",function, attr_to_name(res->attr.type));
376 callback_list_call_attr_4(cbl.u.callback_list, attr_command, function, list, NULL, &valid);
382 res->attr.type=attr_none;
386 eval_postfix(struct context *ctx, struct result *res)
391 eval_brace(ctx, res);
392 if (ctx->error) return;
394 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
397 eval_brace(ctx, &tmp);
398 if (ctx->error) return;
399 resolve(ctx, res,NULL);
400 if (ctx->error) return;
402 res->attrnlen=tmp.varlen;
404 } else if (op[0] == '[') {
405 if (!get_op(ctx,0,"]",NULL)) {
406 ctx->error=missing_closing_bracket;
409 } else if (op[0] == '(') {
410 dbg(0,"function call\n");
411 resolve_object(ctx, res);
412 command_call_function(ctx, res);
418 eval_unary(struct context *ctx, struct result *res)
421 op=get_op(ctx,0,"~","!",NULL);
423 eval_unary(ctx, res);
424 if (ctx->error) return;
426 set_int(ctx, res, ~get_int(ctx, res));
428 set_int(ctx, res, !get_int(ctx, res));
430 eval_postfix(ctx, res);
434 eval_multiplicative(struct context *ctx, struct result *res)
439 eval_unary(ctx, res);
440 if (ctx->error) return;
442 if (!(op=get_op(ctx,0,"*","/","%",NULL))) return;
443 eval_unary(ctx, &tmp);
444 if (ctx->error) return;
445 if (is_double(res) || is_double(&tmp)) {
447 set_double(ctx, res, get_double(ctx, res) * get_double(ctx, &tmp));
448 else if (op[0] == '/')
449 set_double(ctx, res, get_double(ctx, res) / get_double(ctx, &tmp));
451 ctx->error=invalid_type;
456 set_int(ctx, res, get_int(ctx, res) * get_int(ctx, &tmp));
457 else if (op[0] == '/')
458 set_int(ctx, res, get_int(ctx, res) / get_int(ctx, &tmp));
460 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
462 if (ctx->error) return;
467 eval_additive(struct context *ctx, struct result *res)
472 eval_multiplicative(ctx, res);
473 if (ctx->error) return;
475 if (!(op=get_op(ctx,0,"-","+",NULL))) return;
476 eval_multiplicative(ctx, &tmp);
477 if (ctx->error) return;
478 if (is_double(res) || is_double(&tmp)) {
480 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
482 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
485 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
487 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
489 if (ctx->error) return;
494 eval_equality(struct context *ctx, struct result *res)
499 eval_additive(ctx, res);
500 if (ctx->error) return;
502 if (!(op=get_op(ctx,0,"==","!=","<=",">=","<",">",NULL))) return;
503 eval_additive(ctx, &tmp);
504 if (ctx->error) return;
508 set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
511 set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
515 set_int(ctx, res, (get_int(ctx, res) <= get_int(ctx, &tmp)));
517 set_int(ctx, res, (get_int(ctx, res) < get_int(ctx, &tmp)));
522 set_int(ctx, res, (get_int(ctx, res) >= get_int(ctx, &tmp)));
524 set_int(ctx, res, (get_int(ctx, res) > get_int(ctx, &tmp)));
536 eval_bitwise_and(struct context *ctx, struct result *res)
540 eval_equality(ctx, res);
541 if (ctx->error) return;
543 if (get_op(ctx,1,"&&",NULL)) return;
544 if (!get_op(ctx,0,"&",NULL)) return;
545 eval_equality(ctx, &tmp);
546 if (ctx->error) return;
547 set_int(ctx, res, get_int(ctx, res) & get_int(ctx, &tmp));
548 if (ctx->error) return;
553 eval_bitwise_xor(struct context *ctx, struct result *res)
557 eval_bitwise_and(ctx, res);
558 if (ctx->error) return;
560 if (!get_op(ctx,0,"^",NULL)) return;
561 eval_bitwise_and(ctx, &tmp);
562 if (ctx->error) return;
563 set_int(ctx, res, get_int(ctx, res) ^ get_int(ctx, &tmp));
564 if (ctx->error) return;
569 eval_bitwise_or(struct context *ctx, struct result *res)
573 eval_bitwise_xor(ctx, res);
574 if (ctx->error) return;
576 if (get_op(ctx,1,"||",NULL)) return;
577 if (!get_op(ctx,0,"|",NULL)) return;
578 eval_bitwise_xor(ctx, &tmp);
579 if (ctx->error) return;
580 set_int(ctx, res, get_int(ctx, res) | get_int(ctx, &tmp));
581 if (ctx->error) return;
586 eval_logical_and(struct context *ctx, struct result *res)
590 eval_bitwise_or(ctx, res);
591 if (ctx->error) return;
593 if (!get_op(ctx,0,"&&",NULL)) return;
594 eval_bitwise_or(ctx, &tmp);
595 if (ctx->error) return;
596 set_int(ctx, res, get_int(ctx, res) && get_int(ctx, &tmp));
597 if (ctx->error) return;
602 eval_logical_or(struct context *ctx, struct result *res)
606 eval_logical_and(ctx, res);
607 if (ctx->error) return;
609 if (!get_op(ctx,0,"||",NULL)) return;
610 eval_logical_and(ctx, &tmp);
611 if (ctx->error) return;
612 set_int(ctx, res, get_int(ctx, res) || get_int(ctx, &tmp));
613 if (ctx->error) return;
618 eval_conditional(struct context *ctx, struct result *res)
623 eval_logical_or(ctx, res);
624 if (ctx->error) return;
625 if (!get_op(ctx,0,"?",NULL)) return;
626 cond=!!get_int(ctx, res);
627 if (ctx->error) return;
628 eval_logical_or(ctx, &tmp);
629 if (ctx->error) return;
632 if (!get_op(ctx,0,":",NULL)) {
633 ctx->error=missing_colon;
636 eval_logical_or(ctx, &tmp);
637 if (ctx->error) return;
642 /* = *= /= %= += -= >>= <<= &= ^= |= */
645 eval_assignment(struct context *ctx, struct result *res)
648 eval_conditional(ctx, res);
649 if (ctx->error) return;
650 if (!get_op(ctx,0,"=",NULL)) return;
651 eval_conditional(ctx, &tmp);
652 if (ctx->error) return;
653 resolve_object(ctx, res);
654 command_set_attr(ctx, res, &tmp);
659 eval_comma(struct context *ctx, struct result *res)
663 eval_assignment(ctx, res);
664 if (ctx->error) return;
666 if (!get_op(ctx,0,",",NULL)) return;
667 eval_assignment(ctx, &tmp);
668 if (ctx->error) return;
673 static struct attr **
674 eval_list(struct context *ctx)
678 struct attr **ret=NULL;
680 eval_assignment(ctx, &tmp);
685 resolve(ctx, &tmp, NULL);
686 ret=attr_generic_add_attr(ret, &tmp.attr);
687 if (!get_op(ctx,0,",",NULL)) return ret;
693 void command(struct attr *attr, char *expr)
697 memset(&res, 0, sizeof(res));
698 memset(&ctx, 0, sizeof(ctx));
702 printf("command='%s'\n", expr);
703 eval_comma(&ctx,&res);
704 printf("err=%d %s\n", ctx.error, ctx.expr);
707 resolve(&ctx, &res, NULL);
709 printf("%s\n", get_string(&ctx, &res));
714 command_evaluate_to(struct attr *attr, char *expr, struct context *ctx, struct result *res)
716 memset(res, 0, sizeof(*res));
717 memset(ctx, 0, sizeof(*ctx));
724 command_evaluate_to_void(struct attr *attr, char *expr, int **error)
728 command_evaluate_to(attr, expr, &ctx, &res);
730 resolve(&ctx, &res, NULL);
737 command_evaluate_to_string(struct attr *attr, char *expr, int **error)
743 command_evaluate_to(attr, expr, &ctx, &res);
745 ret=get_string(&ctx, &res);
755 command_evaluate_to_int(struct attr *attr, char *expr, int **error)
761 command_evaluate_to(attr, expr, &ctx, &res);
763 ret=get_int(&ctx, &res);
773 command_evaluate(struct attr *attr, char *expr)
777 memset(&res, 0, sizeof(res));
778 memset(&ctx, 0, sizeof(ctx));
783 eval_comma(&ctx,&res);
786 resolve(&ctx, &res, NULL);
789 if (!get_op(&ctx,0,";",NULL)) return;
795 command_interpreter(struct attr *attr)
800 size=read(0, buffer, 4095);
805 command(attr, buffer);
811 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
814 for (i = 0 ; i < count ; i++) {
815 if (!strcmp(command,table->command)) {
818 table->func(data, command, in, out);
825 command_add_table_attr(struct command_table *table, int count, void *data, struct attr *attr)
827 attr->type=attr_callback;
828 attr->u.callback=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
832 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
835 command_add_table_attr(table, count, data, &attr);
836 callback_list_add(cbl, attr.u.callback);
840 command_saved_set_cb (struct command_saved *cs, struct callback *cb)
846 command_saved_get_int (struct command_saved *cs)
848 return get_int(&cs->ctx, &cs->res);
852 command_saved_error (struct command_saved *cs)
858 command_saved_evaluate_idle (struct command_saved *cs)
860 // Only run once at a time
862 event_remove_idle(cs->idle_ev);
866 command_evaluate_to(&cs->navit, cs->command, &cs->ctx, &cs->res);
868 if (!cs->ctx.error) {
872 callback_call_1(cs->cb, cs);
875 cs->error = cs->ctx.error;
880 command_saved_evaluate(struct command_saved *cs)
883 // We're already scheduled for reevaluation
888 cs->idle_cb = callback_new_1(callback_cast(command_saved_evaluate_idle), cs);
891 cs->idle_ev = event_add_idle(100, cs->idle_cb);
895 command_saved_callbacks_changed(struct command_saved *cs)
897 // For now, we delete each and every callback and then re-create them
899 struct object_func *func;
902 if (cs->register_ev) {
903 event_remove_idle(cs->register_ev);
904 cs->register_ev = NULL;
907 attr.type = attr_callback;
909 for (i = 0; i < cs->num_cbs; i++) {
910 func = object_func_lookup(cs->cbs[i].attr.type);
912 if (!func->remove_attr) {
913 dbg(0, "Could not remove command-evaluation callback because remove_attr is missing for type %i!\n", cs->cbs[i].attr.type);
917 attr.u.callback = cs->cbs[i].cb;
919 func->remove_attr(cs->cbs[i].attr.u.data, &attr);
920 callback_destroy(cs->cbs[i].cb);
927 // Now, re-create all the callbacks
928 command_register_callbacks(cs);
932 command_register_callbacks(struct command_saved *cs)
934 struct attr prev,cb_attr,attr;
936 struct object_func *func;
940 cs->ctx.expr = cs->command;
941 cs->ctx.attr = &attr;
944 while ((status = get_next_object(&cs->ctx, &cs->res)) != 0) {
945 resolve(&cs->ctx, &cs->res, NULL);
947 if (cs->ctx.error || (cs->res.attr.type == attr_none)) {
948 // We could not resolve an object, perhaps because it has not been created
952 if (prev.type != attr_none) {
953 func = object_func_lookup(prev.type);
955 if (func->add_attr) {
956 if (status == 2) { // This is not the final attribute name
957 cb = callback_new_attr_1(callback_cast(command_saved_callbacks_changed), cs->res.attr.type, (void*)cs);
959 } else if (status == 1) { // This is the final attribute name
960 cb = callback_new_attr_1(callback_cast(command_saved_evaluate), cs->res.attr.type, (void*)cs);
961 cs->ctx.attr = &cs->navit;
963 dbg(0, "Error: Strange status returned from get_next_object()\n");
967 cs->cbs = g_realloc(cs->cbs, (sizeof(struct command_saved_cb) * cs->num_cbs));
968 cs->cbs[cs->num_cbs-1].cb = cb;
969 cs->cbs[cs->num_cbs-1].attr = prev;
971 cb_attr.u.callback = cb;
972 cb_attr.type = attr_callback;
974 func->add_attr(prev.u.data, &cb_attr);
977 dbg(0, "Could not add callback because add_attr is missing for type %i}n", prev.type);
988 command_saved_evaluate_idle(cs);
994 *command_saved_new(char *command, struct navit *navit, struct callback *cb)
996 struct command_saved *ret;
998 ret = g_new0(struct command_saved, 1);
999 ret->command = g_strdup(command);
1000 ret->navit.u.navit = navit;
1001 ret->navit.type = attr_navit;
1003 ret->error = not_ready;
1005 if (!command_register_callbacks(ret)) {
1006 // We try this as an idle call again
1007 ret->register_cb = callback_new_1(callback_cast(command_saved_callbacks_changed), ret);
1008 ret->register_ev = event_add_idle(300, ret->register_cb);
1015 command_saved_destroy(struct command_saved *cs)
1017 g_free(cs->command);