Fix:core:Fixed various compiler warnings
[navit-package] / 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
18 /*
19 gui.fullscreen()
20 gui.menu()
21 gui.get_data() 
22 zoom_in() 
23 zoom_out()
24 speech.active=!speech.active
25 osd_configuration=1
26 Not yet:
27 osd[type=="xxx"].active=0;osd[type=="yyy"].active=0
28 */
29
30
31 struct result {
32         struct attr attr;
33         double val;
34         char *var;
35         int varlen;
36         char *attrn;
37         int attrnlen;
38         int allocated;
39 };
40
41 struct context {
42         struct attr *attr;
43         int error;
44         char *expr;
45         struct result res;
46 };
47
48 struct command_saved_cb {
49         struct callback *cb;
50         struct attr attr;
51 };
52
53 struct command_saved {
54         struct context ctx;
55         struct result res;
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
61         struct attr navit;
62         int num_cbs;
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
65         int error;
66 };
67
68 enum error {
69         no_error=0,missing_closing_brace, missing_colon, wrong_type, illegal_number_format, illegal_character, missing_closing_bracket, invalid_type, not_ready
70 };
71
72 static void eval_comma(struct context *ctx, struct result *res);
73 static struct attr ** eval_list(struct context *ctx);
74
75 static void
76 result_free(struct result *res)
77 {
78 }
79
80 static int command_register_callbacks(struct command_saved *cs);
81
82 static char *
83 get_op(struct context *ctx, int test, ...)
84 {
85         char *op,*ret=NULL;
86         va_list ap;
87
88         while (g_ascii_isspace(*ctx->expr)) {
89                 ctx->expr++;
90         }
91
92         va_start(ap, test);
93         while ((op = va_arg(ap, char *))) {
94                 if (!strncmp(ctx->expr, op, strlen(op))) {
95                         ret=ctx->expr;
96                         if (! test)
97                                 ctx->expr+=strlen(op);
98                         break;
99                 }
100         }
101         va_end(ap);
102         return ret;
103 }
104
105 /*static int
106 is_int(struct result *res)
107 {
108         return 1;
109 }*/
110
111 static int
112 is_double(struct result *res)
113 {
114         return 0;
115 }
116
117 static void
118 dump(struct result *res)
119 {
120         char object[res->varlen+1];
121         char attribute[res->attrnlen+1];
122         if (res->var)
123                 strncpy(object, res->var, res->varlen);
124         object[res->varlen]='\0';
125         if (res->attrn)
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);
130 }
131
132 static enum attr_type
133 command_attr_type(struct result *res)
134 {
135         char attrn[res->attrnlen+1];
136
137         strncpy(attrn, res->attrn, res->attrnlen);
138         attrn[res->attrnlen]='\0';
139         return attr_from_name(attrn);
140 }
141
142 static int
143 command_object_get_attr(struct context *ctx, struct attr *object, enum attr_type attr_type, struct attr *ret)
144 {
145         struct object_func *func=object_func_lookup(object->type);
146         if (!func || !func->get_attr)
147                 return 0;
148         return func->get_attr(object->u.data, attr_type, ret, NULL);
149 }
150
151 static void
152 command_get_attr(struct context *ctx, struct result *res)
153 {
154         int result;
155         enum attr_type attr_type=command_attr_type(res);
156         result=command_object_get_attr(ctx, &res->attr, attr_type, &res->attr);
157         if (result) {
158                 res->var=res->attrn;
159                 res->varlen=res->attrnlen;
160         } else {
161                 res->attr.type=attr_none;
162                 res->var=NULL;
163                 res->varlen=0;
164         }
165         res->attrn=NULL;
166         res->attrnlen=0;
167         dump(res);
168 }
169
170 static void
171 command_set_attr(struct context *ctx, struct result *res, struct result *newres)
172 {
173         int result=0;
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)
177                 return;
178         newres->attr.type=attr_type;
179         result=func->set_attr(res->attr.u.data, &newres->attr);
180         *res=*newres;
181 }
182
183 static void
184 resolve_object(struct context *ctx, struct result *res)
185 {
186         if (res->attr.type == attr_none && res->varlen) {
187                 res->attr=*ctx->attr;
188                 res->attrn=res->var;
189                 res->attrnlen=res->varlen;
190                 res->var=NULL;
191                 res->varlen=0;
192         }
193 }
194
195 static void
196 resolve(struct context *ctx, struct result *res, struct attr *parent) //FIXME What is that parent for?
197 {
198         resolve_object(ctx, res);
199         if (res->attr.type >= attr_type_object_begin && res->attr.type <= attr_type_object_end) {
200                 if (res->attrn)
201                         command_get_attr(ctx, res);
202                 return;
203         }
204 }
205
206 static double
207 get_double(struct context *ctx, struct result *res)
208 {
209         resolve(ctx, res, NULL);
210         return res->val;
211 }
212
213
214
215 static int
216 get_int(struct context *ctx, struct result *res)
217 {
218         resolve(ctx, res, NULL);
219         if (res->attr.type == attr_none)
220                 return 0;
221         if (res->attr.type >= attr_type_int_begin && res->attr.type <= attr_type_int_end) {
222                 return res->attr.u.num;
223         }
224         if (res->attr.type >= attr_type_double_begin && res->attr.type <= attr_type_double_end) {
225                 return (int) (*res->attr.u.numd);
226         }
227         ctx->error=wrong_type;
228         return 0;
229 }
230
231
232 static char *
233 get_string(struct context *ctx, struct result *res)
234 {
235         resolve(ctx, res, NULL);
236         return attr_to_text(&res->attr, NULL, 0);
237 }
238
239 static void
240 set_double(struct context *ctx, struct result *res, double val)
241 {
242         result_free(res);
243         res->val=val;
244 }
245
246 static void
247 set_int(struct context *ctx, struct result *res, int val)
248 {
249         result_free(res);
250         res->attr.type=attr_type_int_begin;
251         res->attr.u.num=val;
252 }
253
254
255 static void
256 eval_value(struct context *ctx, struct result *res) {
257         char *op;
258         int len,dots=0;
259         op=ctx->expr;
260         res->varlen=0;
261         res->var=NULL;
262         res->attrnlen=0;
263         res->attrn=NULL;
264
265         while (g_ascii_isspace(*op)) {
266                 op++;
267         }
268
269         if (op[0] >= 'a' && op[0] <= 'z') {
270                 res->attr.type=attr_none;
271                 res->var=op;
272                 while ((op[0] >= 'a' && op[0] <= 'z') || op[0] == '_') {
273                         res->varlen++;
274                         op++;
275                 }
276                 ctx->expr=op;
277                 return;
278         }
279         if ((op[0] >= '0' && op[0] <= '9') ||
280             (op[0] == '.' && op[1] >= '0' && op[1] <= '9') ||
281             (op[0] == '-' && op[1] >= '0' && op[1] <= '9') ||
282             (op[0] == '-' && op[1] == '.' && op[2] >= '0' && op[2] <= '9')) {
283                 while ((op[0] >= '0' && op[0] <= '9') || op[0] == '.' || (res->varlen == 0 && op[0] == '-')) {
284                         if (op[0] == '.')
285                                 dots++;
286                         if (dots > 1) {
287                                 ctx->error=illegal_number_format;
288                                 return;
289                         }
290                         res->varlen++;
291                         op++;
292                 }
293                 if (dots) {
294                         res->val = strtod(ctx->expr, NULL);
295                         res->attr.type=attr_type_double_begin;
296                         res->attr.u.numd=&res->val;
297                 } else {
298                         res->attr.type=attr_type_int_begin;
299                         res->attr.u.num=atoi(ctx->expr);
300                 }
301                 ctx->expr=op;
302                 return;
303         }
304         if (op[0] == '"') {
305                 do {
306                         op++;
307                 } while (op[0] != '"');
308                 res->attr.type=attr_type_string_begin;
309                 len=op-ctx->expr-1;
310                 res->attr.u.str=g_malloc(len+1);
311                 strncpy(res->attr.u.str, ctx->expr+1, len);
312                 res->attr.u.str[len]='\0';
313                 op++;
314                 ctx->expr=op;
315                 return;
316         }
317         ctx->error=illegal_character;
318 }
319
320 static int
321 get_next_object(struct context *ctx, struct result *res) {
322         
323         while (*ctx->expr) {
324                 res->varlen = 0;
325                 ctx->error = 0;
326
327                 eval_value(ctx,res);
328                 
329                 if ((res->attr.type == attr_none) && (res->varlen > 0)) {
330                         if (ctx->expr[0] != '.') {
331                                 return 1;               // 1 means "this is the final object name"
332                         } else {
333                                 return 2;               // 2 means "there are more object names following" (e.g. we just hit 'vehicle' in 'vehicle.position_speed'
334                         }
335                 }
336
337                 if (ctx->error) {
338                         // Probably hit an operator
339                         ctx->expr++;
340                 }
341         }
342
343         return 0;
344 }
345
346 static void
347 eval_brace(struct context *ctx, struct result *res)
348 {
349         if (get_op(ctx,0,"(",NULL)) {
350                 eval_comma(ctx, res);
351                 if (ctx->error) return;
352                 if (!get_op(ctx,0,")",NULL)) 
353                         ctx->error=missing_closing_brace;
354                 return;
355         }
356         eval_value(ctx, res);
357 }
358
359 static void
360 command_call_function(struct context *ctx, struct result *res)
361 {
362         struct attr cbl,**list=NULL;
363         char function[res->attrnlen+1];
364         if (res->attrn)
365                 strncpy(function, res->attrn, res->attrnlen);
366         function[res->attrnlen]='\0';
367         dbg(0,"function=%s\n", function);
368         if (ctx->expr[0] != ')') {
369                 list=eval_list(ctx);    
370                 if (ctx->error) return;
371         }
372         if (!get_op(ctx,0,")",NULL)) {
373                 ctx->error=missing_closing_brace;
374                 return;
375         }
376         if (command_object_get_attr(ctx, &res->attr, attr_callback_list, &cbl)) {
377                 int valid;
378                 dbg(0,"function call %s from %s\n",function, attr_to_name(res->attr.type));
379                 callback_list_call_attr_4(cbl.u.callback_list, attr_command, function, list, NULL, &valid);
380         }
381         res->var=NULL;
382         res->varlen=0;
383         res->attrn=NULL;
384         res->attrnlen=0;
385         res->attr.type=attr_none;
386 }
387
388 static void
389 eval_postfix(struct context *ctx, struct result *res)
390 {
391         struct result tmp;
392         char *op;
393
394         eval_brace(ctx, res);
395         if (ctx->error) return;
396         for (;;) {
397                 if (!(op=get_op(ctx,0,"[","(",".",NULL)))
398                         return;
399                 if (op[0] == '.') {
400                         eval_brace(ctx, &tmp);
401                         if (ctx->error) return;
402                         resolve(ctx, res,NULL);
403                         if (ctx->error) return;
404                         res->attrn=tmp.var;
405                         res->attrnlen=tmp.varlen;
406                         dump(res);
407                 } else if (op[0] == '[') {
408                         if (!get_op(ctx,0,"]",NULL)) {
409                                 ctx->error=missing_closing_bracket;
410                                 return;
411                         }
412                 } else if (op[0] == '(') {
413                         dbg(0,"function call\n");
414                         resolve_object(ctx, res);
415                         command_call_function(ctx, res);
416                 }
417         }
418 }
419
420 static void
421 eval_unary(struct context *ctx, struct result *res) 
422 {
423         char *op;
424         op=get_op(ctx,0,"~","!",NULL);
425         if (op) {
426                 eval_unary(ctx, res);
427                 if (ctx->error) return;
428                 if (op[0] == '~')
429                         set_int(ctx, res, ~get_int(ctx, res));
430                 else
431                         set_int(ctx, res, !get_int(ctx, res));
432         } else
433                 eval_postfix(ctx, res);
434 }
435
436 static void
437 eval_multiplicative(struct context *ctx, struct result *res) 
438 {
439         struct result tmp;
440         char *op;
441
442         eval_unary(ctx, res);
443         if (ctx->error) return;
444         for (;;) {
445                 if (!(op=get_op(ctx,0,"*","/","%",NULL))) return;
446                 eval_unary(ctx, &tmp);
447                 if (ctx->error) return;
448                 if (is_double(res) || is_double(&tmp)) {
449                         if (op[0] == '*')
450                                 set_double(ctx, res, get_double(ctx, res) * get_double(ctx, &tmp));
451                         else if (op[0] == '/')
452                                 set_double(ctx, res, get_double(ctx, res) / get_double(ctx, &tmp));
453                         else {
454                                 ctx->error=invalid_type;
455                                 return;
456                         }
457                 } else {
458                         if (op[0] == '*')
459                                 set_int(ctx, res, get_int(ctx, res) * get_int(ctx, &tmp));
460                         else if (op[0] == '/')
461                                 set_int(ctx, res, get_int(ctx, res) / get_int(ctx, &tmp));
462                         else
463                                 set_int(ctx, res, get_int(ctx, res) % get_int(ctx, &tmp));
464                 }
465                 if (ctx->error) return;
466         }
467 }
468
469 static void
470 eval_additive(struct context *ctx, struct result *res) 
471 {
472         struct result tmp;
473         char *op;
474
475         eval_multiplicative(ctx, res);
476         if (ctx->error) return;
477         for (;;) {
478                 if (!(op=get_op(ctx,0,"-","+",NULL))) return;
479                 eval_multiplicative(ctx, &tmp);
480                 if (ctx->error) return;
481                 if (is_double(res) || is_double(&tmp)) {
482                         if (op[0] == '+')
483                                 set_double(ctx, res, get_double(ctx, res) + get_double(ctx, &tmp));
484                         else
485                                 set_double(ctx, res, get_double(ctx, res) - get_double(ctx, &tmp));
486                 } else {
487                         if (op[0] == '+')
488                                 set_int(ctx, res, get_int(ctx, res) + get_int(ctx, &tmp));
489                         else
490                                 set_int(ctx, res, get_int(ctx, res) - get_int(ctx, &tmp));
491                 }
492                 if (ctx->error) return;
493         }
494 }
495
496 static void
497 eval_equality(struct context *ctx, struct result *res) 
498 {
499         struct result tmp;
500         char *op;
501
502         eval_additive(ctx, res);
503         if (ctx->error) return;
504         for (;;) {
505                 if (!(op=get_op(ctx,0,"==","!=","<=",">=","<",">",NULL))) return;
506                 eval_additive(ctx, &tmp);
507                 if (ctx->error) return;
508
509                 switch (op[0]) {
510                 case '=':
511                         set_int(ctx, res, (get_int(ctx, res) == get_int(ctx, &tmp)));
512                         break;
513                 case '!':
514                         set_int(ctx, res, (get_int(ctx, res) != get_int(ctx, &tmp)));
515                         break;
516                 case '<':
517                         if (op[1] == '=') {
518                                 set_int(ctx, res, (get_int(ctx, res) <= get_int(ctx, &tmp)));
519                         } else {
520                                 set_int(ctx, res, (get_int(ctx, res) < get_int(ctx, &tmp)));
521                         }
522                         break;
523                 case '>':
524                         if (op[1] == '=') {
525                                 set_int(ctx, res, (get_int(ctx, res) >= get_int(ctx, &tmp)));
526                         } else {
527                                 set_int(ctx, res, (get_int(ctx, res) > get_int(ctx, &tmp)));
528                         }
529                         break;
530                 default:
531                         break;
532                 }
533                 result_free(&tmp);
534         }
535 }
536
537
538 static void
539 eval_bitwise_and(struct context *ctx, struct result *res) 
540 {
541         struct result tmp;
542
543         eval_equality(ctx, res);
544         if (ctx->error) return;
545         for (;;) {
546                 if (get_op(ctx,1,"&&",NULL)) return;
547                 if (!get_op(ctx,0,"&",NULL)) return;
548                 eval_equality(ctx, &tmp);
549                 if (ctx->error) return;
550                 set_int(ctx, res, get_int(ctx, res) & get_int(ctx, &tmp));
551                 if (ctx->error) return;
552         }
553 }
554
555 static void
556 eval_bitwise_xor(struct context *ctx, struct result *res) 
557 {
558         struct result tmp;
559
560         eval_bitwise_and(ctx, res);
561         if (ctx->error) return;
562         for (;;) {
563                 if (!get_op(ctx,0,"^",NULL)) return;
564                 eval_bitwise_and(ctx, &tmp);
565                 if (ctx->error) return;
566                 set_int(ctx, res, get_int(ctx, res) ^ get_int(ctx, &tmp));
567                 if (ctx->error) return;
568         }
569 }
570
571 static void
572 eval_bitwise_or(struct context *ctx, struct result *res) 
573 {
574         struct result tmp;
575
576         eval_bitwise_xor(ctx, res);
577         if (ctx->error) return;
578         for (;;) {
579                 if (get_op(ctx,1,"||",NULL)) return;
580                 if (!get_op(ctx,0,"|",NULL)) return;
581                 eval_bitwise_xor(ctx, &tmp);
582                 if (ctx->error) return;
583                 set_int(ctx, res, get_int(ctx, res) | get_int(ctx, &tmp));
584                 if (ctx->error) return;
585         }
586 }
587
588 static void
589 eval_logical_and(struct context *ctx, struct result *res) 
590 {
591         struct result tmp;
592
593         eval_bitwise_or(ctx, res);
594         if (ctx->error) return;
595         for (;;) {
596                 if (!get_op(ctx,0,"&&",NULL)) return;
597                 eval_bitwise_or(ctx, &tmp);
598                 if (ctx->error) return;
599                 set_int(ctx, res, get_int(ctx, res) && get_int(ctx, &tmp));
600                 if (ctx->error) return;
601         }
602 }
603
604 static void
605 eval_logical_or(struct context *ctx, struct result *res) 
606 {
607         struct result tmp;
608
609         eval_logical_and(ctx, res);
610         if (ctx->error) return;
611         for (;;) {
612                 if (!get_op(ctx,0,"||",NULL)) return;
613                 eval_logical_and(ctx, &tmp);
614                 if (ctx->error) return;
615                 set_int(ctx, res, get_int(ctx, res) || get_int(ctx, &tmp));
616                 if (ctx->error) return;
617         }
618 }
619
620 static void
621 eval_conditional(struct context *ctx, struct result *res)
622 {
623         struct result tmp;
624         int cond;
625
626         eval_logical_or(ctx, res);
627         if (ctx->error) return;
628         if (!get_op(ctx,0,"?",NULL)) return;
629         cond=!!get_int(ctx, res);
630         if (ctx->error) return;
631         eval_logical_or(ctx, &tmp);
632         if (ctx->error) return;
633         if (cond)
634                 *res=tmp;
635         if (!get_op(ctx,0,":",NULL)) {
636                 ctx->error=missing_colon;
637                 return;
638         }
639         eval_logical_or(ctx, &tmp);
640         if (ctx->error) return;
641         if (!cond)
642                 *res=tmp;
643 }
644
645 /* = *= /= %= += -= >>= <<= &= ^= |= */
646
647 static void
648 eval_assignment(struct context *ctx, struct result *res)
649 {
650         struct result tmp;
651         eval_conditional(ctx, res);
652         if (ctx->error) return;
653         if (!get_op(ctx,0,"=",NULL)) return;
654         eval_conditional(ctx, &tmp);
655         if (ctx->error) return;
656         resolve_object(ctx, res);
657         command_set_attr(ctx, res, &tmp);
658 }
659
660 /* , */
661 static void
662 eval_comma(struct context *ctx, struct result *res)
663 {
664         struct result tmp;
665
666         eval_assignment(ctx, res);
667         if (ctx->error) return;
668         for (;;) {
669                 if (!get_op(ctx,0,",",NULL)) return;
670                 eval_assignment(ctx, &tmp);
671                 if (ctx->error) return;
672                 *res=tmp;
673         }
674 }
675
676 static struct attr **
677 eval_list(struct context *ctx)
678 {
679         struct result tmp;
680
681         struct attr **ret=NULL;
682         for (;;) {
683                 eval_assignment(ctx, &tmp);
684                 if (ctx->error) {
685                         attr_list_free(ret);
686                         return NULL;
687                 }
688                 ret=attr_generic_add_attr(ret, &tmp.attr);
689                 if (!get_op(ctx,0,",",NULL)) return ret;
690         }
691 }
692
693 #if 0
694
695 void command(struct attr *attr, char *expr)
696 {
697         struct result res;
698         struct context ctx;
699         memset(&res, 0, sizeof(res));
700         memset(&ctx, 0, sizeof(ctx));
701         ctx.attr=attr;
702         ctx.error=0;
703         ctx.expr=expr;
704         printf("command='%s'\n", expr);
705         eval_comma(&ctx,&res);
706         printf("err=%d %s\n", ctx.error, ctx.expr);
707         dump(&res);
708         printf("***\n");
709         resolve(&ctx, &res, NULL);
710         dump(&res);
711         printf("%s\n", get_string(&ctx, &res));
712 }
713 #endif
714
715 static void
716 command_evaluate_to(struct attr *attr, char *expr, struct context *ctx, struct result *res)
717 {
718         memset(res, 0, sizeof(*res));
719         memset(ctx, 0, sizeof(*ctx));
720         ctx->attr=attr;
721         ctx->expr=expr;
722         eval_comma(ctx,res);
723 }
724
725 void
726 command_evaluate_to_void(struct attr *attr, char *expr, int **error)
727 {
728         struct result res;
729         struct context ctx;
730         command_evaluate_to(attr, expr, &ctx, &res);
731         if (!ctx.error)
732                 resolve(&ctx, &res, NULL);
733         if (error)
734                 *error=&ctx.error;
735
736 }
737
738 char *
739 command_evaluate_to_string(struct attr *attr, char *expr, int **error)
740 {
741         struct result res;
742         struct context ctx;
743         char *ret=NULL;
744
745         command_evaluate_to(attr, expr, &ctx, &res);
746         if (!ctx.error)
747                 ret=get_string(&ctx, &res);
748         if (error)
749                 *error=&ctx.error;
750         if (ctx.error)
751                 return NULL;
752         else
753                 return ret;
754 }
755
756 int
757 command_evaluate_to_int(struct attr *attr, char *expr, int **error)
758 {
759         struct result res;
760         struct context ctx;
761         int ret=0;
762
763         command_evaluate_to(attr, expr, &ctx, &res);
764         if (!ctx.error)
765                 ret=get_int(&ctx, &res);
766         if (error)
767                 *error=&ctx.error;
768         if (ctx.error)
769                 return 0;
770         else
771                 return ret;
772 }
773
774 void
775 command_evaluate(struct attr *attr, char *expr)
776 {
777         struct result res;
778         struct context ctx;
779         memset(&res, 0, sizeof(res));
780         memset(&ctx, 0, sizeof(ctx));
781         ctx.attr=attr;
782         ctx.error=0;
783         ctx.expr=expr;
784         for (;;) {
785                 eval_comma(&ctx,&res);
786                 if (ctx.error)
787                         return;
788                 resolve(&ctx, &res, NULL);
789                 if (ctx.error)
790                         return;
791                 if (!get_op(&ctx,0,";",NULL)) return;
792         }
793 }
794
795 #if 0
796 void
797 command_interpreter(struct attr *attr)
798 {
799                 char buffer[4096];
800                 int size;
801                 for (;;) {
802                 size=read(0, buffer, 4095);
803                 buffer[size]='\0';
804                 if (size) {
805                         buffer[size-1]='\0';
806                 }
807                 command(attr, buffer);
808                 }
809 }
810 #endif
811
812 static void
813 command_table_call(struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
814 {
815         int i;
816         for (i = 0 ; i < count ; i++) {
817                 if (!strcmp(command,table->command)) {
818                         if (valid)
819                                 *valid=1;
820                         table->func(data, command, in, out);
821                 }
822                 table++;
823         }
824 }
825
826 void
827 command_add_table(struct callback_list *cbl, struct command_table *table, int count, void *data)
828 {
829         struct callback *cb=callback_new_attr_3(callback_cast(command_table_call),attr_command, table, count, data);
830         callback_list_add(cbl, cb);
831 }
832
833 void
834 command_saved_set_cb (struct command_saved *cs, struct callback *cb)
835 {
836         cs->cb = cb;
837 }
838
839 int
840 command_saved_get_int (struct command_saved *cs)
841 {
842         return get_int(&cs->ctx, &cs->res);
843 }
844
845 int 
846 command_saved_error (struct command_saved *cs)
847 {
848         return cs->error;
849 }
850
851 static void
852 command_saved_evaluate_idle (struct command_saved *cs) 
853 {
854         // Only run once at a time
855         if (cs->idle_ev) {
856                 event_remove_idle(cs->idle_ev);
857                 cs->idle_ev = NULL;
858         }
859
860         command_evaluate_to(&cs->navit, cs->command, &cs->ctx, &cs->res);
861
862         if (!cs->ctx.error) {
863                 cs->error = 0;
864
865                 if (cs->cb) {
866                         callback_call_1(cs->cb, cs);
867                 }
868         } else {
869                 cs->error = cs->ctx.error;
870         }
871 }
872
873 static void
874 command_saved_evaluate(struct command_saved *cs)
875 {       
876         if (cs->idle_ev) {
877                 // We're already scheduled for reevaluation
878                 return;
879         }
880
881         if (!cs->idle_cb) {
882                 cs->idle_cb = callback_new_1(callback_cast(command_saved_evaluate_idle), cs);
883         }
884
885         cs->idle_ev = event_add_idle(100, cs->idle_cb);
886 }
887
888 static void
889 command_saved_callbacks_changed(struct command_saved *cs)
890 {
891         // For now, we delete each and every callback and then re-create them
892         int i;
893         struct object_func *func;
894         struct attr attr;
895
896         if (cs->register_ev) {
897                 event_remove_idle(cs->register_ev);
898                 cs->register_ev = NULL;
899         }
900
901         attr.type = attr_callback;
902
903         for (i = 0; i < cs->num_cbs; i++) {
904                 func = object_func_lookup(cs->cbs[i].attr.type);
905                 
906                 if (!func->remove_attr) {
907                         dbg(0, "Could not remove command-evaluation callback because remove_attr is missing for type %i!\n", cs->cbs[i].attr.type);
908                         continue;
909                 }
910
911                 attr.u.callback = cs->cbs[i].cb;
912
913                 func->remove_attr(cs->cbs[i].attr.u.data, &attr);
914                 callback_destroy(cs->cbs[i].cb);
915         }
916
917         g_free(cs->cbs);
918         cs->cbs = NULL;
919         cs->num_cbs = 0;
920
921         // Now, re-create all the callbacks
922         command_register_callbacks(cs);
923 }
924
925 static int
926 command_register_callbacks(struct command_saved *cs)
927 {
928         struct attr prev,cb_attr,attr;
929         int status;
930         struct object_func *func;
931         struct callback *cb;
932         
933         attr = cs->navit;
934         cs->ctx.expr = cs->command;
935         cs->ctx.attr = &attr;
936         prev = cs->navit;       
937
938         while ((status = get_next_object(&cs->ctx, &cs->res)) != 0) {
939                 resolve(&cs->ctx, &cs->res, NULL);
940
941                 if (cs->ctx.error || (cs->res.attr.type == attr_none)) {
942                         // We could not resolve an object, perhaps because it has not been created
943                         return 0;
944                 }
945
946                 if (prev.type != attr_none) {
947                         func = object_func_lookup(prev.type);
948
949                         if (func->add_attr) {
950                                 if (status == 2) { // This is not the final attribute name
951                                         cb = callback_new_attr_1(callback_cast(command_saved_callbacks_changed), cs->res.attr.type, (void*)cs);
952                                         attr = cs->res.attr;
953                                 } else if (status == 1) { // This is the final attribute name
954                                         cb = callback_new_attr_1(callback_cast(command_saved_evaluate), cs->res.attr.type, (void*)cs);
955                                         cs->ctx.attr = &cs->navit;
956                                 } else {
957                                         dbg(0, "Error: Strange status returned from get_next_object()\n");
958                                 }
959
960                                 cs->num_cbs++;
961                                 cs->cbs = g_realloc(cs->cbs, (sizeof(struct command_saved_cb) * cs->num_cbs));
962                                 cs->cbs[cs->num_cbs-1].cb = cb;
963                                 cs->cbs[cs->num_cbs-1].attr = prev;
964                                         
965                                 cb_attr.u.callback = cb;
966                                 cb_attr.type = attr_callback;
967
968                                 func->add_attr(prev.u.data, &cb_attr);
969
970                         } else {
971                                 dbg(0, "Could not add callback because add_attr is missing for type %i}n", prev.type);
972                         }
973                 }
974
975                 if (status == 2) {
976                         prev = cs->res.attr;
977                 } else {
978                         prev = cs->navit;
979                 }
980         }
981
982         command_saved_evaluate_idle(cs);
983
984         return 1;
985 }
986
987 struct command_saved
988 *command_saved_new(char *command, struct navit *navit, struct callback *cb)
989 {
990         struct command_saved *ret;
991
992         ret = g_new0(struct command_saved, 1);
993         ret->command = g_strdup(command);
994         ret->navit.u.navit = navit;
995         ret->navit.type = attr_navit;
996         ret->cb = cb;
997         ret->error = not_ready;
998
999         if (!command_register_callbacks(ret)) {
1000                 // We try this as an idle call again
1001                 ret->register_cb = callback_new_1(callback_cast(command_saved_callbacks_changed), ret);
1002                 ret->register_ev = event_add_idle(300, ret->register_cb);
1003         }               
1004
1005         return ret;
1006 }
1007
1008 void 
1009 command_saved_destroy(struct command_saved *cs)
1010 {
1011         g_free(cs->command);
1012         g_free(cs);
1013 }