Add:Core:Make asynchonicity for saved commands optional
[profile/ivi/navit.git] / navit / navit / command.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <glib.h>
6 #include "item.h"
7 #include "xmlconfig.h"
8 #include "main.h"
9 #include "navit.h"
10 #include "vehicle.h"
11 #include "speech.h"
12 #include "gui.h"
13 #include "debug.h"
14 #include "callback.h"
15 #include "command.h"
16 #include "event.h"
17 #include "navit_nls.h"
18 #include "obj_filter.h"
19
20 /*
21 gui.fullscreen=!gui.fullscreen
22 gui.menu()
23 gui.get_data() 
24 zoom_in() 
25 zoom_out()
26 speech.active=!speech.active
27 osd_configuration=1
28 Not yet:
29 osd[type=="xxx"].active=0;osd[type=="yyy"].active=0
30 */
31
32
33 struct result {
34         struct attr attr;
35         double val;
36         const char *var;
37         int varlen;
38         const char *attrn;
39         int attrnlen;
40         int allocated;
41 };
42
43 struct context {
44         struct attr *attr;
45         int error;
46         const char *expr;
47         struct result res;
48 };
49
50 struct command_saved_cb {
51         struct callback *cb;
52         struct attr attr;
53 };
54
55 struct command_saved {
56         struct context ctx;
57         struct result res;
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
63         struct attr navit;
64         int num_cbs;
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
67         int error;
68         int async;
69 };
70
71 enum error {
72         no_error=0,missing_closing_brace, missing_colon, wrong_type, illegal_number_format, illegal_character, missing_closing_bracket, invalid_type, not_ready
73 };
74
75 static void eval_comma(struct context *ctx, struct result *res);
76 static struct attr ** eval_list(struct context *ctx);
77
78 static void
79 result_free(struct result *res)
80 {
81 }
82
83 static int command_register_callbacks(struct command_saved *cs);
84
85 static const char *
86 get_op(struct context *ctx, int test, ...)
87 {
88         char *op;
89         const char *ret=NULL;
90         va_list ap;
91
92         while (g_ascii_isspace(*ctx->expr)) {
93                 ctx->expr++;
94         }
95
96         va_start(ap, test);
97         while ((op = va_arg(ap, char *))) {
98                 if (!strncmp(ctx->expr, op, strlen(op))) {
99                         ret=ctx->expr;
100                         if (! test)
101                                 ctx->expr+=strlen(op);
102                         break;
103                 }
104         }
105         va_end(ap);
106         return ret;
107 }
108
109 /*static int
110 is_int(struct result *res)
111 {
112         return 1;
113 }*/
114
115 static int
116 is_double(struct result *res)
117 {
118         return 0;
119 }
120
121 static void
122 dump(struct result *res)
123 {
124 #if 0
125         char object[res->varlen+1];
126         char attribute[res->attrnlen+1];
127         if (res->var)
128                 strncpy(object, res->var, res->varlen);
129         object[res->varlen]='\0';
130         if (res->attrn)
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);
135 #endif
136 }
137
138 static enum attr_type
139 command_attr_type(struct result *res)
140 {
141         char *attrn=g_alloca(sizeof(char)*(res->attrnlen+1));
142
143         strncpy(attrn, res->attrn, res->attrnlen);
144         attrn[res->attrnlen]='\0';
145         return attr_from_name(attrn);
146 }
147
148 static int
149 command_object_get_attr(struct context *ctx, struct attr *object, enum attr_type attr_type, struct attr *ret)
150 {
151         struct object_func *func=object_func_lookup(object->type);
152         if (!func || !func->get_attr)
153                 return 0;
154         return func->get_attr(object->u.data, attr_type, ret, NULL);
155 }
156
157 static int
158 command_object_add_attr(struct context *ctx, struct attr *object, struct attr *attr)
159 {
160         struct object_func *func=object_func_lookup(object->type);
161         if (!func || !func->add_attr)
162                 return 0;
163         return func->add_attr(object->u.data, attr);
164 }
165
166 static int
167 command_object_remove_attr(struct context *ctx, struct attr *object, struct attr *attr)
168 {
169         struct object_func *func=object_func_lookup(object->type);
170         if (!func || !func->remove_attr)
171                 return 0;
172         return func->remove_attr(object->u.data, attr);
173 }
174
175
176 static void
177 command_get_attr(struct context *ctx, struct result *res)
178 {
179         int result;
180         enum attr_type attr_type=command_attr_type(res);
181         result=command_object_get_attr(ctx, &res->attr, attr_type, &res->attr);
182         if (result) {
183                 res->var=res->attrn;
184                 res->varlen=res->attrnlen;
185         } else {
186                 res->attr.type=attr_none;
187                 res->var=NULL;
188                 res->varlen=0;
189         }
190         res->attrn=NULL;
191         res->attrnlen=0;
192         dump(res);
193 }
194
195 static void
196 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
197 {
198         int result=0;
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)
202                 return;
203         if (attr_type == attr_attr_types) {
204                 char *attrn=g_alloca(sizeof(char)*(res->attrnlen+1));
205                 struct attr *tmp;
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;
210                 g_free(tmp);
211         }
212         newres->attr.type=attr_type;
213         result=func->set_attr(res->attr.u.data, &newres->attr);
214         *res=*newres;
215 }
216
217 static void
218 resolve_object(struct context *ctx, struct result *res)
219 {
220         if (res->attr.type == attr_none && res->varlen) {
221                 res->attr=*ctx->attr;
222                 res->attrn=res->var;
223                 res->attrnlen=res->varlen;
224                 res->var=NULL;
225                 res->varlen=0;
226         }
227 }
228
229 static void
230 resolve(struct context *ctx, struct result *res, struct attr *parent) //FIXME What is that parent for?
231 {
232         resolve_object(ctx, res);
233         if (res->attrn)
234                 command_get_attr(ctx, res);
235 }
236
237 static double
238 get_double(struct context *ctx, struct result *res)
239 {
240         resolve(ctx, res, NULL);
241         return res->val;
242 }
243
244
245
246 static int
247 get_int(struct context *ctx, struct result *res)
248 {
249         resolve(ctx, res, NULL);
250         if (res->attr.type == attr_none)
251                 return 0;
252         if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
253                 return res->attr.u.num;
254         }
255         if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
256                 return (int) (*res->attr.u.numd);
257         }
258         ctx->error=wrong_type;
259         return 0;
260 }
261
262
263 static char *
264 get_string(struct context *ctx, struct result *res)
265 {
266         resolve(ctx, res, NULL);
267         return attr_to_text(&res->attr, NULL, 0);
268 }
269
270 static void
271 set_double(struct context *ctx, struct result *res, double val)
272 {
273         result_free(res);
274         res->val=val;
275 }
276
277 static void
278 set_int(struct context *ctx, struct result *res, int val)
279 {
280         result_free(res);
281         res->attr.type=attr_type_int_begin;
282         res->attr.u.num=val;
283 }
284
285
286 static void
287 eval_value(struct context *ctx, struct result *res) {
288         const char *op;
289         int len,dots=0;
290         struct obj_filter_t out;
291         int parsed_chars;
292
293         op=ctx->expr;
294         res->varlen=0;
295         res->var=NULL;
296         res->attrnlen=0;
297         res->attrn=NULL;
298
299         while (g_ascii_isspace(*op)) {
300                 op++;
301         }
302
303         parsed_chars = parse_obj_filter(op, &out);
304         if (parsed_chars) {
305                 struct attr* res_attr = filter_object(ctx->attr, out.iterator_type, out.filter_expr, out.idx);
306                 if (res_attr) {
307                         res->attr = *res_attr;
308                         g_free(res_attr);
309                         ctx->expr = op+parsed_chars;
310                         return;
311                 }
312         } 
313
314         if ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
315                 res->attr.type=attr_none;
316                 res->var=op;
317                 for (;;) {
318                         while ((op[0] >= 'a' && op[0] <= 'z') || (op[0] >= '0' && op[0] <= '9') || op[0] == '_') {
319                                 res->varlen++;
320                                 op++;
321                         }
322                         if (res->varlen == 3 && !strncmp(res->var,"new",3) && op[0] == ' ') {
323                                 res->varlen++;
324                                 op++;
325                         } else
326                                 break;
327                 }
328                 ctx->expr=op;
329                 return;
330         }
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] == '-')) {
336                         if (op[0] == '.')
337                                 dots++;
338                         if (dots > 1) {
339                                 ctx->error=illegal_number_format;
340                                 return;
341                         }
342                         res->varlen++;
343                         op++;
344                 }
345                 if (dots) {
346                         res->val = strtod(ctx->expr, NULL);
347                         res->attr.type=attr_type_double_begin;
348                         res->attr.u.numd=&res->val;
349                 } else {
350                         res->attr.type=attr_type_int_begin;
351                         res->attr.u.num=atoi(ctx->expr);
352                 }
353                 ctx->expr=op;
354                 return;
355         }
356         if (op[0] == '"') {
357                 do {
358                         op++;
359                 } while (op[0] != '"');
360                 res->attr.type=attr_type_string_begin;
361                 len=op-ctx->expr-1;
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';
365                 op++;
366                 ctx->expr=op;
367                 return;
368         }
369         ctx->error=illegal_character;
370 }
371
372 static int
373 get_next_object(struct context *ctx, struct result *res) {
374         
375         while (*ctx->expr) {
376                 res->varlen = 0;
377                 ctx->error = 0;
378
379                 eval_value(ctx,res);
380                 
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"
384                         } else {
385                                 return 2;               // 2 means "there are more object names following" (e.g. we just hit 'vehicle' in 'vehicle.position_speed'
386                         }
387                 }
388
389                 if (ctx->error) {
390                         // Probably hit an operator
391                         ctx->expr++;
392                 }
393         }
394
395         return 0;
396 }
397
398 static void
399 eval_brace(struct context *ctx, struct result *res)
400 {
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;
406                 return;
407         }
408         eval_value(ctx, res);
409 }
410
411 static void
412 command_call_function(struct context *ctx, struct result *res)
413 {
414         struct attr cbl,**list=NULL;
415         char *function=g_alloca(sizeof(char)*(res->attrnlen+1));
416         if (res->attrn)
417                 strncpy(function, res->attrn, res->attrnlen);
418         function[res->attrnlen]='\0';
419         dbg(1,"function=%s\n", function);
420         if (ctx->expr[0] != ')') {
421                 list=eval_list(ctx);    
422                 if (ctx->error) return;
423         }
424         if (!get_op(ctx,0,")",NULL)) {
425                 ctx->error=missing_closing_brace;
426                 return;
427         }
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));      
431                 
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);
439                         }
440                 }
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]);
445         } else {
446                 if (command_object_get_attr(ctx, &res->attr, attr_callback_list, &cbl)) {
447                         int valid =0;
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);
451                         if (valid!=1){
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);
454                         }
455                         if (out && out[0]) {
456                                 attr_dup_content(out[0], &res->attr);
457                                 attr_list_free(out);
458                         } else
459                                 res->attr.type=attr_none;
460                 } else
461                         res->attr.type=attr_none;
462         }
463         res->var=NULL;
464         res->varlen=0;
465         res->attrn=NULL;
466         res->attrnlen=0;
467 }
468
469 static void
470 eval_postfix(struct context *ctx, struct result *res)
471 {
472         struct result tmp;
473         const char *op;
474
475         eval_brace(ctx, res);
476         if (ctx->error) return;
477         for (;;) {
478                 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
479                         return;
480                 if (op[0] == '.') {
481                         eval_brace(ctx, &tmp);
482                         if (ctx->error) return;
483                         resolve(ctx, res,NULL);
484                         if (ctx->error) return;
485                         res->attrn=tmp.var;
486                         res->attrnlen=tmp.varlen;
487                         dump(res);
488                 } else if (op[0] == '[') {
489                         if (!get_op(ctx,0,"]",NULL)) {
490                                 ctx->error=missing_closing_bracket;
491                                 return;
492                         }
493                 } else if (op[0] == '(') {
494                         dbg(1,"function call\n");
495                         resolve_object(ctx, res);
496                         command_call_function(ctx, res);
497                 }
498         }
499 }
500
501 static void
502 eval_unary(struct context *ctx, struct result *res) 
503 {
504         const char *op;
505         op=get_op(ctx,0,"~","!",NULL);
506         if (op) {
507                 eval_unary(ctx, res);
508                 if (ctx->error) return;
509                 if (op[0] == '~')
510                         set_int(ctx, res, ~get_int(ctx, res));
511                 else
512                         set_int(ctx, res, !get_int(ctx, res));
513         } else
514                 eval_postfix(ctx, res);
515 }
516
517 static void
518 eval_multiplicative(struct context *ctx, struct result *res) 
519 {
520         struct result tmp;
521         const char *op;
522
523         eval_unary(ctx, res);
524         if (ctx->error) return;
525         for (;;) {
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)) {
530                         if (op[0] == '*')
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));
534                         else {
535                                 ctx->error=invalid_type;
536                                 return;
537                         }
538                 } else {
539                         if (op[0] == '*')
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));
543                         else
544                                 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
545                 }
546                 if (ctx->error) return;
547         }
548 }
549
550 static void
551 eval_additive(struct context *ctx, struct result *res) 
552 {
553         struct result tmp;
554         const char *op;
555
556         eval_multiplicative(ctx, res);
557         if (ctx->error) return;
558         for (;;) {
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)) {
563                         if (op[0] == '+')
564                                 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
565                         else
566                                 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
567                 } else {
568                         if (op[0] == '+')
569                                 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
570                         else
571                                 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
572                 }
573                 if (ctx->error) return;
574         }
575 }
576
577 static void
578 eval_equality(struct context *ctx, struct result *res) 
579 {
580         struct result tmp;
581         const char *op;
582
583         eval_additive(ctx, res);
584         if (ctx->error) return;
585         for (;;) {
586                 if (!(op=get_op(ctx,0,"==","!=","<=",">=","<",">",NULL))) return;
587                 eval_additive(ctx, &tmp);
588                 if (ctx->error) return;
589
590                 switch (op[0]) {
591                 case '=':
592                         set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
593                         break;
594                 case '!':
595                         set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
596                         break;
597                 case '<':
598                         if (op[1] == '=') {
599                                 set_int(ctx, res, (get_int(ctx, res) <= get_int(ctx, &tmp)));
600                         } else {
601                                 set_int(ctx, res, (get_int(ctx, res) < get_int(ctx, &tmp)));
602                         }
603                         break;
604                 case '>':
605                         if (op[1] == '=') {
606                                 set_int(ctx, res, (get_int(ctx, res) >= get_int(ctx, &tmp)));
607                         } else {
608                                 set_int(ctx, res, (get_int(ctx, res) > get_int(ctx, &tmp)));
609                         }
610                         break;
611                 default:
612                         break;
613                 }
614                 result_free(&tmp);
615         }
616 }
617
618
619 static void
620 eval_bitwise_and(struct context *ctx, struct result *res) 
621 {
622         struct result tmp;
623
624         eval_equality(ctx, res);
625         if (ctx->error) return;
626         for (;;) {
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;
633         }
634 }
635
636 static void
637 eval_bitwise_xor(struct context *ctx, struct result *res) 
638 {
639         struct result tmp;
640
641         eval_bitwise_and(ctx, res);
642         if (ctx->error) return;
643         for (;;) {
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;
649         }
650 }
651
652 static void
653 eval_bitwise_or(struct context *ctx, struct result *res) 
654 {
655         struct result tmp;
656
657         eval_bitwise_xor(ctx, res);
658         if (ctx->error) return;
659         for (;;) {
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;
666         }
667 }
668
669 static void
670 eval_logical_and(struct context *ctx, struct result *res) 
671 {
672         struct result tmp;
673
674         eval_bitwise_or(ctx, res);
675         if (ctx->error) return;
676         for (;;) {
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;
682         }
683 }
684
685 static void
686 eval_logical_or(struct context *ctx, struct result *res) 
687 {
688         struct result tmp;
689
690         eval_logical_and(ctx, res);
691         if (ctx->error) return;
692         for (;;) {
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;
698         }
699 }
700
701 static void
702 eval_conditional(struct context *ctx, struct result *res)
703 {
704         struct result tmp;
705         int cond;
706
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;
714         if (cond)
715                 *res=tmp;
716         if (!get_op(ctx,0,":",NULL)) {
717                 ctx->error=missing_colon;
718                 return;
719         }
720         eval_logical_or(ctx, &tmp);
721         if (ctx->error) return;
722         if (!cond)
723                 *res=tmp;
724 }
725
726 /* = *= /= %= += -= >>= <<= &= ^= |= */
727
728 static void
729 eval_assignment(struct context *ctx, struct result *res)
730 {
731         struct result tmp;
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);
741 }
742
743 /* , */
744 static void
745 eval_comma(struct context *ctx, struct result *res)
746 {
747         struct result tmp;
748
749         eval_assignment(ctx, res);
750         if (ctx->error) return;
751         for (;;) {
752                 if (!get_op(ctx,0,",",NULL)) return;
753                 eval_assignment(ctx, &tmp);
754                 if (ctx->error) return;
755                 *res=tmp;
756         }
757 }
758
759 static struct attr **
760 eval_list(struct context *ctx)
761 {
762         struct result tmp;
763
764         struct attr **ret=NULL;
765         for (;;) {
766                 eval_assignment(ctx, &tmp);
767                 if (ctx->error) {
768                         attr_list_free(ret);
769                         return NULL;
770                 }
771                 resolve(ctx, &tmp, NULL);
772                 ret=attr_generic_add_attr(ret, &tmp.attr);
773                 if (!get_op(ctx,0,",",NULL)) return ret;
774         }
775 }
776
777 #if 0
778
779 void command(struct attr *attr, char *expr)
780 {
781         struct result res;
782         struct context ctx;
783         memset(&res, 0, sizeof(res));
784         memset(&ctx, 0, sizeof(ctx));
785         ctx.attr=attr;
786         ctx.error=0;
787         ctx.expr=expr;
788         printf("command='%s'\n", expr);
789         eval_comma(&ctx,&res);
790         printf("err=%d %s\n", ctx.error, ctx.expr);
791         dump(&res);
792         printf("***\n");
793         resolve(&ctx, &res, NULL);
794         dump(&res);
795         printf("%s\n", get_string(&ctx, &res));
796 }
797 #endif
798
799 static void
800 command_evaluate_to(struct attr *attr, const char *expr, struct context *ctx, struct result *res)
801 {
802         memset(res, 0, sizeof(*res));
803         memset(ctx, 0, sizeof(*ctx));
804         ctx->attr=attr;
805         ctx->expr=expr;
806         eval_comma(ctx,res);
807 }
808
809 enum attr_type
810 command_evaluate_to_attr(struct attr *attr, char *expr, int *error, struct attr *ret)
811 {
812         struct result res;
813         struct context ctx;
814         command_evaluate_to(attr, expr, &ctx, &res);
815         if (ctx.error)
816                 return attr_none;
817         resolve_object(&ctx, &res);
818         *ret=res.attr;
819         dbg(0,"type %s\n",attr_to_name(command_attr_type(&res)));
820         return command_attr_type(&res);
821 }
822
823 void
824 command_evaluate_to_void(struct attr *attr, char *expr, int *error)
825 {
826         struct result res;
827         struct context ctx;
828         command_evaluate_to(attr, expr, &ctx, &res);
829         if (!ctx.error)
830                 resolve(&ctx, &res, NULL);
831         if (error)
832                 *error=ctx.error;
833
834 }
835
836 char *
837 command_evaluate_to_string(struct attr *attr, char *expr, int *error)
838 {
839         struct result res;
840         struct context ctx;
841         char *ret=NULL;
842
843         command_evaluate_to(attr, expr, &ctx, &res);
844         if (!ctx.error)
845                 resolve(&ctx, &res, NULL);
846         if (!ctx.error)
847                 ret=get_string(&ctx, &res);
848         if (error)
849                 *error=ctx.error;
850         if (ctx.error)
851                 return NULL;
852         else
853                 return ret;
854 }
855
856 int
857 command_evaluate_to_int(struct attr *attr, char *expr, int *error)
858 {
859         struct result res;
860         struct context ctx;
861         int ret=0;
862
863         command_evaluate_to(attr, expr, &ctx, &res);
864         if (!ctx.error)
865                 resolve(&ctx, &res, NULL);
866         if (!ctx.error)
867                 ret=get_int(&ctx, &res);
868         if (error)
869                 *error=ctx.error;
870         if (ctx.error)
871                 return 0;
872         else
873                 return ret;
874 }
875
876 int
877 command_evaluate_to_boolean(struct attr *attr, const char *expr, int *error)
878 {
879         struct result res;
880         struct context ctx;
881         int ret=0;
882
883         command_evaluate_to(attr, expr, &ctx, &res);
884         if (!ctx.error)
885                 resolve(&ctx, &res, NULL);
886         if (!ctx.error) {
887                 if (res.attr.type == attr_none)
888                         ret=0;
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);
892                 else 
893                         ret=res.attr.u.data != NULL;
894         }
895         if (error)
896                 *error=ctx.error;
897         if (ctx.error)
898                 return 0;
899         else    
900                 return ret;
901 }
902
903 void
904 command_evaluate(struct attr *attr, const char *expr)
905 {
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. */
909
910         char *expr_dup;
911         struct result res;
912         struct context ctx;
913         memset(&res, 0, sizeof(res));
914         memset(&ctx, 0, sizeof(ctx));
915         ctx.attr=attr;
916         ctx.error=0;
917         ctx.expr=expr_dup=g_strdup(expr);
918         for (;;) {
919                 eval_comma(&ctx,&res);
920                 if (ctx.error)
921                         break;
922                 resolve(&ctx, &res, NULL);
923                 if (ctx.error)
924                         break;
925                 if (!get_op(&ctx,0,";",NULL))
926                         break;
927         }
928         g_free(expr_dup);
929 }
930
931 #if 0
932 void
933 command_interpreter(struct attr *attr)
934 {
935                 char buffer[4096];
936                 int size;
937                 for (;;) {
938                 size=read(0, buffer, 4095);
939                 buffer[size]='\0';
940                 if (size) {
941                         buffer[size-1]='\0';
942                 }
943                 command(attr, buffer);
944                 }
945 }
946 #endif
947
948 static void
949 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
950 {
951         int i;
952         for (i = 0 ; i < count ; i++) {
953                 if (!strcmp(command,table->command)) {
954                         if (valid)
955                                 *valid=1;
956                         table->func(data, command, in, out);
957                 }
958                 table++;
959         }
960 }
961
962 void
963 command_add_table_attr(struct command_table *table, int count, void *data, struct attr *attr)
964 {
965         attr->type=attr_callback;
966         attr->u.callback=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
967 }
968
969 void
970 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
971 {
972         struct attr attr;
973         command_add_table_attr(table, count, data, &attr);
974         callback_list_add(cbl, attr.u.callback);
975 }
976
977 void
978 command_saved_set_cb (struct command_saved *cs, struct callback *cb)
979 {
980         cs->cb = cb;
981 }
982
983 int
984 command_saved_get_int (struct command_saved *cs)
985 {
986         return get_int(&cs->ctx, &cs->res);
987 }
988
989 int 
990 command_saved_error (struct command_saved *cs)
991 {
992         return cs->error;
993 }
994
995 static void
996 command_saved_evaluate_idle (struct command_saved *cs) 
997 {
998         // Only run once at a time
999         if (cs->idle_ev) {
1000                 event_remove_idle(cs->idle_ev);
1001                 cs->idle_ev = NULL;
1002         }
1003
1004         command_evaluate_to(&cs->navit, cs->command, &cs->ctx, &cs->res);
1005
1006         if (!cs->ctx.error) {
1007                 cs->error = 0;
1008
1009                 if (cs->cb) {
1010                         callback_call_1(cs->cb, cs);
1011                 }
1012         } else {
1013                 cs->error = cs->ctx.error;
1014         }
1015 }
1016
1017 static void
1018 command_saved_evaluate(struct command_saved *cs)
1019 {       
1020         if (!cs->async) {
1021                 command_saved_evaluate_idle(cs);
1022                 return;
1023         }
1024         if (cs->idle_ev) {
1025                 // We're already scheduled for reevaluation
1026                 return;
1027         }
1028
1029         if (!cs->idle_cb) {
1030                 cs->idle_cb = callback_new_1(callback_cast(command_saved_evaluate_idle), cs);
1031         }
1032
1033         cs->idle_ev = event_add_idle(100, cs->idle_cb);
1034 }
1035
1036 static void
1037 command_saved_callbacks_changed(struct command_saved *cs)
1038 {
1039         // For now, we delete each and every callback and then re-create them
1040         int i;
1041         struct object_func *func;
1042         struct attr attr;
1043
1044         if (cs->register_ev) {
1045                 event_remove_idle(cs->register_ev);
1046                 cs->register_ev = NULL;
1047         }
1048
1049         attr.type = attr_callback;
1050
1051         for (i = 0; i < cs->num_cbs; i++) {
1052                 func = object_func_lookup(cs->cbs[i].attr.type);
1053                 
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);
1056                         continue;
1057                 }
1058
1059                 attr.u.callback = cs->cbs[i].cb;
1060
1061                 func->remove_attr(cs->cbs[i].attr.u.data, &attr);
1062                 callback_destroy(cs->cbs[i].cb);
1063         }
1064
1065         g_free(cs->cbs);
1066         cs->cbs = NULL;
1067         cs->num_cbs = 0;
1068
1069         // Now, re-create all the callbacks
1070         command_register_callbacks(cs);
1071 }
1072
1073 static int
1074 command_register_callbacks(struct command_saved *cs)
1075 {
1076         struct attr prev,cb_attr,attr;
1077         int status;
1078         struct object_func *func;
1079         struct callback *cb;
1080         
1081         attr = cs->navit;
1082         cs->ctx.expr = cs->command;
1083         cs->ctx.attr = &attr;
1084         prev = cs->navit;       
1085
1086         while ((status = get_next_object(&cs->ctx, &cs->res)) != 0) {
1087                 resolve(&cs->ctx, &cs->res, NULL);
1088
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
1091                         return 0;
1092                 }
1093
1094                 if (prev.type != attr_none) {
1095                         func = object_func_lookup(prev.type);
1096
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;
1104                                 } else {
1105                                         dbg(0, "Error: Strange status returned from get_next_object()\n");
1106                                 }
1107
1108                                 cs->num_cbs++;
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;
1112                                         
1113                                 cb_attr.u.callback = cb;
1114                                 cb_attr.type = attr_callback;
1115
1116                                 func->add_attr(prev.u.data, &cb_attr);
1117
1118                         } else {
1119                                 dbg(0, "Could not add callback because add_attr is missing for type %i}n", prev.type);
1120                         }
1121                 }
1122
1123                 if (status == 2) {
1124                         prev = cs->res.attr;
1125                 } else {
1126                         prev = cs->navit;
1127                 }
1128         }
1129
1130         command_saved_evaluate_idle(cs);
1131
1132         return 1;
1133 }
1134
1135 struct command_saved *
1136 command_saved_new(char *command, struct navit *navit, struct callback *cb, int async)
1137 {
1138         struct command_saved *ret;
1139
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;
1144         ret->cb = cb;
1145         ret->error = not_ready;
1146         ret->async = async;
1147
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);
1152         }               
1153
1154         return ret;
1155 }
1156
1157 void 
1158 command_saved_destroy(struct command_saved *cs)
1159 {
1160         g_free(cs->command);
1161         g_free(cs);
1162 }