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