navit  0.5.3-trunk
command.c File Reference
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <glib.h>
#include "item.h"
#include "xmlconfig.h"
#include "main.h"
#include "navit.h"
#include "vehicle.h"
#include "speech.h"
#include "gui.h"
#include "debug.h"
#include "callback.h"
#include "command.h"
#include "event.h"
#include "navit_nls.h"

Data Structures

struct  result
 
struct  result_list
 
struct  context
 
struct  command_saved_cb
 
struct  command_saved
 

Enumerations

enum  error {
  no_error =0 , missing_double_quote , missing_opening_parenthesis , missing_closing_parenthesis ,
  missing_closing_brace , missing_colon , missing_semicolon , wrong_type ,
  illegal_number_format , illegal_character , missing_closing_bracket , invalid_type ,
  not_ready , internal , eof_reached
}
 
enum  op_type { op_type_binary , op_type_prefix , op_type_suffix }
 
enum  set_type { set_type_symbol , set_type_string , set_type_integer , set_type_float }
 

Functions

static void eval_comma (struct context *ctx, struct result *res)
 
static struct attr ** eval_list (struct context *ctx)
 
char * command_error_to_text (int err)
 Converts an error to human-readable text. More...
 
static void result_free (struct result *res)
 
static int command_register_callbacks (struct command_saved *cs)
 Registers callbacks for a saved command. More...
 
static const char * get_op (struct context *ctx, int test,...)
 
static int is_double (struct result *res)
 
static void dump (struct result *res)
 
static enum attr_type command_attr_type (struct result *res)
 
static int command_object_get_attr (struct context *ctx, struct attr *object, enum attr_type attr_type, struct attr *ret)
 Retrieves an attribute from an object. More...
 
static int command_object_add_attr (struct context *ctx, struct attr *object, struct attr *attr)
 
static int command_object_remove_attr (struct context *ctx, struct attr *object, struct attr *attr)
 
static void command_get_attr (struct context *ctx, struct result *res)
 Retrieves the current value of an attribute and stores it in. More...
 
static void command_set_attr (struct context *ctx, struct result *res, struct result *newres)
 
static void resolve_object (struct context *ctx, struct result *res)
 Resolves an object reference. More...
 
static void resolve (struct context *ctx, struct result *res)
 Resolves and retrieves an object and stores it in. More...
 
static double get_double (struct context *ctx, struct result *res)
 
static int get_int_bool (struct context *ctx, int is_bool, struct result *res)
 Returns an integer or bool representation of the result of an expression. More...
 
static int get_int (struct context *ctx, struct result *res)
 Returns an integer representation of the result of an expression. More...
 
static int get_bool (struct context *ctx, struct result *res)
 Returns a boolean representation of the result of an expression. More...
 
static char * get_string (struct context *ctx, struct result *res)
 
static void set_double (struct result *res, double val)
 
static void set_int (struct result *res, int val)
 
static void result_op (struct context *ctx, enum op_type op_type, const char *op, struct result *inout, struct result *in)
 
static void result_set (struct context *ctx, enum set_type set_type, const char *op, int len, struct result *out)
 
static void eval_value (struct context *ctx, struct result *res)
 Evaluates a value and stores its result. More...
 
static int get_next_object (struct context *ctx, struct result *res)
 Retrieves the next object reference from an expression. More...
 
static void eval_brace (struct context *ctx, struct result *res)
 
static void command_call_function (struct context *ctx, struct result *res)
 
static void eval_postfix (struct context *ctx, struct result *res)
 
static void eval_unary (struct context *ctx, struct result *res)
 
static void eval_multiplicative (struct context *ctx, struct result *res)
 
static void eval_additive (struct context *ctx, struct result *res)
 
static void eval_equality (struct context *ctx, struct result *res)
 
static void eval_bitwise_and (struct context *ctx, struct result *res)
 
static void eval_bitwise_xor (struct context *ctx, struct result *res)
 
static void eval_bitwise_or (struct context *ctx, struct result *res)
 
static void eval_logical_and (struct context *ctx, struct result *res)
 
static void eval_logical_or (struct context *ctx, struct result *res)
 
static void eval_conditional (struct context *ctx, struct result *res)
 
static void eval_assignment (struct context *ctx, struct result *res)
 
void command (struct attr *attr, char *expr)
 
static void command_evaluate_to (struct attr *attr, const char *expr, struct context *ctx, struct result *res)
 
enum attr_type command_evaluate_to_attr (struct attr *attr, char *expr, int *error, struct attr *ret)
 
void command_evaluate_to_void (struct attr *attr, char *expr, int *error)
 
char * command_evaluate_to_string (struct attr *attr, char *expr, int *error)
 
int command_evaluate_to_int (struct attr *attr, char *expr, int *error)
 
int command_evaluate_to_boolean (struct attr *attr, const char *expr, int *error)
 
int command_evaluate_to_length (const char *expr, int *error)
 
static int command_evaluate_single (struct context *ctx)
 
void command_evaluate (struct attr *attr, const char *expr)
 
void command_interpreter (struct attr *attr)
 
static void command_table_call (struct command_table *table, int count, void *data, char *command, struct attr **in, struct attr ***out, int *valid)
 
void command_add_table_attr (struct command_table *table, int count, void *data, struct attr *attr)
 
void command_add_table (struct callback_list *cbl, struct command_table *table, int count, void *data)
 
void command_saved_set_cb (struct command_saved *cs, struct callback *cb)
 
int command_saved_get_int (struct command_saved *cs)
 Returns an integer representation of the evaluation result of a saved command. More...
 
int command_saved_error (struct command_saved *cs)
 
static void command_saved_evaluate_idle (struct command_saved *cs)
 Idle function to evaluate a command. More...
 
static void command_saved_evaluate (struct command_saved *cs)
 Evaluates a command. More...
 
static void command_saved_callbacks_changed (struct command_saved *cs)
 Recreates all callbacks for a saved command. More...
 
struct command_savedcommand_saved_attr_new (char *command, struct attr *attr, struct callback *cb, int async)
 Creates a new saved command. More...
 
struct command_savedcommand_saved_new (char *command, struct navit *navit, struct callback *cb, int async)
 
void command_saved_destroy (struct command_saved *cs)
 

Enumeration Type Documentation

◆ error

enum error
Enumerator
no_error 
missing_double_quote 
missing_opening_parenthesis 
missing_closing_parenthesis 
missing_closing_brace 
missing_colon 
missing_semicolon 
wrong_type 
illegal_number_format 
illegal_character 
missing_closing_bracket 
invalid_type 
not_ready 
internal 
eof_reached 

◆ op_type

enum op_type
Enumerator
op_type_binary 
op_type_prefix 
op_type_suffix 

◆ set_type

enum set_type
Enumerator
set_type_symbol 
set_type_string 
set_type_integer 
set_type_float 

Function Documentation

◆ command()

◆ command_add_table()

void command_add_table ( struct callback_list cbl,
struct command_table table,
int  count,
void *  data 
)

◆ command_add_table_attr()

void command_add_table_attr ( struct command_table table,
int  count,
void *  data,
struct attr attr 
)

◆ command_attr_type()

◆ command_call_function()

◆ command_error_to_text()

char* command_error_to_text ( int  err)

Converts an error to human-readable text.

Parameters
errThe error code
Returns
A string containing the error description. The caller is responsible for freeing up the string by calling
g_free()
when it is no longer needed.

References eof_reached, err(), illegal_character, illegal_number_format, invalid_type, missing_closing_brace, missing_closing_bracket, missing_closing_parenthesis, missing_colon, missing_double_quote, missing_opening_parenthesis, missing_semicolon, no_error, not_ready, and wrong_type.

Referenced by command_evaluate(), osd_std_config(), and osd_std_reconfigure().

◆ command_evaluate()

◆ command_evaluate_single()

◆ command_evaluate_to()

static void command_evaluate_to ( struct attr attr,
const char *  expr,
struct context ctx,
struct result res 
)
static

◆ command_evaluate_to_attr()

enum attr_type command_evaluate_to_attr ( struct attr attr,
char *  expr,
int *  error,
struct attr ret 
)

◆ command_evaluate_to_boolean()

int command_evaluate_to_boolean ( struct attr attr,
const char *  expr,
int *  error 
)

◆ command_evaluate_to_int()

int command_evaluate_to_int ( struct attr attr,
char *  expr,
int *  error 
)

◆ command_evaluate_to_length()

int command_evaluate_to_length ( const char *  expr,
int *  error 
)

◆ command_evaluate_to_string()

char* command_evaluate_to_string ( struct attr attr,
char *  expr,
int *  error 
)

◆ command_evaluate_to_void()

void command_evaluate_to_void ( struct attr attr,
char *  expr,
int *  error 
)

◆ command_get_attr()

static void command_get_attr ( struct context ctx,
struct result res 
)
static

Retrieves the current value of an attribute and stores it in.

res

.

If

ctx->skip

is true, the function aborts and no action is taken.

Before calling this function, object references in

res

must be resolved. That is,

res->attr

holds a copy of

ctx->attr

and the first

res->attrnlen

characters of

res->attrn

correspond to the object name.

After this function completes,

res->allocated

is true, and

res->attrn

and

res->attrnlen

are reset.

If the attribute was successfully retrieved, the first

res->varlen

characters of

res->var

correspond to an object name and

res->attr

holds the attribute.

If the attribute could not be retrieved,

res->attr.type

is set to

attr_none

, and

res->var

and

res->varlen

are reset.

Parameters
ctxThe context
resThe result

References result::allocated, result::attr, attr_to_name(), result::attrn, result::attrnlen, command_attr_type(), command_object_get_attr(), dbg, dump(), lvl_debug, lvl_warning, result_free(), context::skip, attr::type, result::var, and result::varlen.

Referenced by resolve().

◆ command_interpreter()

void command_interpreter ( struct attr attr)

References command(), and size.

◆ command_object_add_attr()

static int command_object_add_attr ( struct context ctx,
struct attr object,
struct attr attr 
)
static

◆ command_object_get_attr()

static int command_object_get_attr ( struct context ctx,
struct attr object,
enum attr_type  attr_type,
struct attr ret 
)
static

Retrieves an attribute from an object.

This function will retrieve the first matching attribute by calling the

get_attr

method for the object type. If

object

does not refer to a valid object, or the

get_attr

method for the object type could not be defined, the function fails and zero is returned.

Parameters
ctxThe context (ignored)
objectThe object for which the attribute is to be retrieved.
attr_typeThe type of attribute to retrieve
retPoints to a
struct attr
Definition: attr.h:157
to which the attribute will be copied
Returns
True if a matching attribute was found, false if no matching attribute was found or an error occurred

References attr_dup_content(), attr_to_name(), attr::data, dbg, object_func::dup, object_func::get_attr, lvl_warning, object_func_lookup(), attr::type, and attr::u.

Referenced by command_call_function(), and command_get_attr().

◆ command_object_remove_attr()

static int command_object_remove_attr ( struct context ctx,
struct attr object,
struct attr attr 
)
static

◆ command_register_callbacks()

static int command_register_callbacks ( struct command_saved cs)
static

Registers callbacks for a saved command.

This function registers callbacks for each attribute used in a saved command, causing the command to be re-evaluated whenever its value might change.

This function will fail if an object used in the expression could not be resolved. This may happen during startup if this function is called before all objects have been created. In this case, the caller should schedule the function to be called again at a later time.

It will also fail if an error is encountered. This can be determined by examining

cs->ctx.error

after the function returns.

Parameters
csThe command
Returns
True if all callbacks were successfully registered, false if the function failed

References command_saved::async, context::attr, command_saved_cb::attr, result::attr, attr::callback, callback_cast, callback_new_attr_1, command_saved_cb::cb, command_saved::cbs, command_saved::command, command_saved_callbacks_changed(), command_saved_evaluate(), command_saved_evaluate_idle(), command_saved::context_attr, command_saved::ctx, attr::data, dbg, context::error, context::expr, callback::func, get_next_object(), lvl_debug, lvl_error, command_saved::num_cbs, object_func_lookup(), command_saved::res, resolve(), attr::type, attr::u, and result::var.

Referenced by command_saved_attr_new(), and command_saved_callbacks_changed().

◆ command_saved_attr_new()

struct command_saved* command_saved_attr_new ( char *  command,
struct attr attr,
struct callback cb,
int  async 
)

Creates a new saved command.

Parameters
commandThe command string
attrThe context attribute for the saved command
cbThe callback to call whenver the command is re-evaluated
asyncWhether the saved command should be flagged as asynchronous, causing it to be evaluated in an idle callback

References command_saved::async, callback_cast, callback_new_1, command_saved::cb, command_saved::command, command(), command_register_callbacks(), command_saved_callbacks_changed(), command_saved::context_attr, dbg, command_saved::error, event_add_idle(), lvl_debug, not_ready, command_saved::register_cb, and command_saved::register_ev.

Referenced by command_saved_new(), and script_set_attr_int().

◆ command_saved_callbacks_changed()

◆ command_saved_destroy()

void command_saved_destroy ( struct command_saved cs)

◆ command_saved_error()

int command_saved_error ( struct command_saved cs)

◆ command_saved_evaluate()

static void command_saved_evaluate ( struct command_saved cs)
static

Evaluates a command.

This function examines

cs->async

to determine if the command should be evaluated immediately. If

cs->async

is true, an idle event is registered to register the command. Else the command is evaluated immediately and the result can be retrieved immediately after this function returns.

See command_saved_evaluate_idle(struct command_saved *) for details.

Parameters
csThe command to evaluate

References command_saved::async, callback_cast, callback_new_1, command_saved::command, command_saved_evaluate_idle(), dbg, event_add_idle(), command_saved::idle_cb, command_saved::idle_ev, and lvl_debug.

Referenced by command_register_callbacks().

◆ command_saved_evaluate_idle()

static void command_saved_evaluate_idle ( struct command_saved cs)
static

Idle function to evaluate a command.

This function is called from an idle loop for asynchronous evaluation but may also be called in-line.

The result of the evaluation can be retrieved from

cs->res

after this function returns. If an error occurred, it will be stored in

cs->error

.

Parameters
csThe command to evaluate

References callback_call_1, command_saved::cb, command_saved::command, command_evaluate_to(), command_saved::context_attr, command_saved::ctx, dbg, context::error, command_saved::error, event_remove_idle(), command_saved::idle_ev, lvl_debug, and command_saved::res.

Referenced by command_register_callbacks(), and command_saved_evaluate().

◆ command_saved_get_int()

int command_saved_get_int ( struct command_saved cs)

Returns an integer representation of the evaluation result of a saved command.

This function is a wrapper around

static int get_int(struct context *ctx, struct result *res)
Returns an integer representation of the result of an expression.
Definition: command.c:442

. It is equivalent to

get_int(&cs->ctx, &cs->res)

. See

for a description.

References command_saved::ctx, get_int(), and command_saved::res.

Referenced by osd_std_config(), and osd_std_reconfigure().

◆ command_saved_new()

struct command_saved* command_saved_new ( char *  command,
struct navit navit,
struct callback cb,
int  async 
)

◆ command_saved_set_cb()

void command_saved_set_cb ( struct command_saved cs,
struct callback cb 
)

References command_saved::cb.

Referenced by osd_std_config().

◆ command_set_attr()

◆ command_table_call()

static void command_table_call ( struct command_table table,
int  count,
void *  data,
char *  command,
struct attr **  in,
struct attr ***  out,
int *  valid 
)
static

◆ dump()

static void dump ( struct result res)
static

◆ eval_additive()

static void eval_additive ( struct context ctx,
struct result res 
)
static

◆ eval_assignment()

static void eval_assignment ( struct context ctx,
struct result res 
)
static

◆ eval_bitwise_and()

static void eval_bitwise_and ( struct context ctx,
struct result res 
)
static

◆ eval_bitwise_or()

static void eval_bitwise_or ( struct context ctx,
struct result res 
)
static

◆ eval_bitwise_xor()

static void eval_bitwise_xor ( struct context ctx,
struct result res 
)
static

◆ eval_brace()

static void eval_brace ( struct context ctx,
struct result res 
)
static

◆ eval_comma()

static void eval_comma ( struct context ctx,
struct result res 
)
static

◆ eval_conditional()

static void eval_conditional ( struct context ctx,
struct result res 
)
static

◆ eval_equality()

static void eval_equality ( struct context ctx,
struct result res 
)
static

◆ eval_list()

static struct attr ** eval_list ( struct context ctx)
static

◆ eval_logical_and()

static void eval_logical_and ( struct context ctx,
struct result res 
)
static

◆ eval_logical_or()

static void eval_logical_or ( struct context ctx,
struct result res 
)
static

◆ eval_multiplicative()

static void eval_multiplicative ( struct context ctx,
struct result res 
)
static

◆ eval_postfix()

◆ eval_unary()

static void eval_unary ( struct context ctx,
struct result res 
)
static

◆ eval_value()

static void eval_value ( struct context ctx,
struct result res 
)
static

Evaluates a value and stores its result.

This function evaluates the first value in

ctx->expr

. A value can be either an object name (such as

vehicle.position_speed
Definition: vehicle.c:59

) or a literal value.

If evaluation is successful, the result is stored in

res->attr

.

If an object name is encountered, the result has an attribute type of

attr_none

and the first

res->varlen

characters of

res->var

will point to the object name.

If a literal value is encountered, the result's attribute type is set to the corresponding generic data type and its value is stored with the attribute.

After this function returns,

ctx->expr

contains the rest of the expression string, which was not evaluated. Leading spaces before the value will be discarded with the value.

If

ctx->expr

, after eliminating any leading whitespace, does not begin with a valid value, one of the following errors is stored in

ctx->error

:

Parameters
ctxThe context to evaluate
resPoints to a
struct res
in which the result will be stored

References result::attrn, result::attrnlen, dbg, eof_reached, context::error, context::expr, illegal_character, illegal_number_format, lvl_debug, missing_double_quote, result_free(), result_set(), set_type_float, set_type_integer, set_type_string, set_type_symbol, result::var, and result::varlen.

Referenced by eval_brace(), and get_next_object().

◆ get_bool()

static int get_bool ( struct context ctx,
struct result res 
)
static

Returns a boolean representation of the result of an expression.

This function is a wrapper around

static int get_int_bool(struct context *ctx, int is_bool, struct result *res)
Returns an integer or bool representation of the result of an expression.
Definition: command.c:417

. It is equivalent to

get_int_bool(ctx, 1, res)

. See

for a description.

References get_int_bool().

Referenced by command_evaluate_single(), eval_conditional(), and result_op().

◆ get_double()

static double get_double ( struct context ctx,
struct result res 
)
static

References resolve(), and result::val.

Referenced by result_op().

◆ get_int()

static int get_int ( struct context ctx,
struct result res 
)
static

Returns an integer representation of the result of an expression.

This function is a wrapper around

. It is equivalent to

get_int_bool(ctx, 0, res)

. See

for a description.

References get_int_bool().

Referenced by command_evaluate_to_boolean(), command_evaluate_to_int(), command_saved_get_int(), and result_op().

◆ get_int_bool()

static int get_int_bool ( struct context ctx,
int  is_bool,
struct result res 
)
static

Returns an integer or bool representation of the result of an expression.

This function evaluates the result of an expression (

res->attr

).

If

res->attr

is of a numeric type, its integer part is returned.

If

is_bool

is false and

res->attr

is not of a numeric type, 0 is returned.

If

is_bool

is true and

res->attr

is of an object or string type, true is returned for non-null, false otherwise.

For all other types of

res->attr

, 0 (false) is returned.

Parameters
ctxThe context for the expression
is_boolIf true, return boolean representation, else return integer representation. See description.
resThe result of the evaluation
Returns
The result of the expression, see description.

References result::attr, ATTR_IS_OBJECT, ATTR_IS_STRING, attr_to_name(), attr::data, dbg, context::error, lvl_debug, attr::num, attr::numd, resolve(), attr::type, attr::u, and wrong_type.

Referenced by get_bool(), and get_int().

◆ get_next_object()

static int get_next_object ( struct context ctx,
struct result res 
)
static

Retrieves the next object reference from an expression.

This function scans the expression string

ctx->expr

for the next object reference. Anything other than an object reference (whitespace characters, literal values, operators and even illegal characters) is discarded until either the end of the string is reached or an object reference is encountered.

After this function completes successfully,

res->attr.type

is

attr_none

and the first

res->varlen

characters of

res->var

point to the object name.

Object names retrieved by this function are unqualified, i.e.

vehicle.position_speed

will be retrieved as

on the first call (return value 2) and

position_speed

on the second call (return value 1).

Parameters
ctxThe context
resPoints to a
struct result
Definition: command.c:34
where the result will be stored.
Returns
If a complete object name has been retrieved, the return value is 1. If a partial object name has been retrieved (e.g. from
vehicle.position_speed
), the return value is 2. If no object references were found, the return value is 0.

References result::attr, context::error, eval_value(), context::expr, attr::type, and result::varlen.

Referenced by command_register_callbacks().

◆ get_op()

◆ get_string()

◆ is_double()

static int is_double ( struct result res)
static

Referenced by result_op().

◆ resolve()

static void resolve ( struct context ctx,
struct result res 
)
static

Resolves and retrieves an object and stores it in.

res

.

Prior to calling this function,

res

must contain a valid, unresolved object reference:

res->attr.type

must be

attr_none

, and the first

res->varlen

characters of

res->var

must correspond to an object name.

If

ctx->skip

is true, the object reference will be resolved but the object will not be retrieved: the first

res->attrnlen

characters of

res->attrn

correspond to the object name after the function returns, while

res->var

and

res->varlen

are reset.

If

ctx->skip

is false,

res->allocated

is true after this function completes. The object is stored in

res->attr

if it was successfully retrieved, otherwise

res->var

and

res->varlen

are reset.

Parameters
ctxThe context
resThe result

References result::attrn, command_get_attr(), and resolve_object().

Referenced by ch_process(), ch_process_node(), ch_process_nodes(), command(), command_evaluate_single(), command_evaluate_to_boolean(), command_evaluate_to_int(), command_evaluate_to_string(), command_evaluate_to_void(), command_register_callbacks(), eval_assignment(), eval_list(), eval_postfix(), get_double(), get_int_bool(), get_string(), and result_op().

◆ resolve_object()

static void resolve_object ( struct context ctx,
struct result res 
)
static

Resolves an object reference.

Prior to calling this function,

res

must contain a valid, unresolved object reference:

res->attr.type

must be

attr_none

, and the first

res->varlen

characters of

res->var

must correspond to an object name.

After the function returns,

res->attr

holds a copy of

ctx->attr

and the first

res->attrnlen

characters of

res->attrn

correspond to the object name.

res->var

and

res->varlen

are reset.

Parameters
ctxThe context
resThe result

References context::attr, result::attr, result::attrn, result::attrnlen, attr::type, result::var, and result::varlen.

Referenced by command_evaluate_single(), eval_assignment(), eval_postfix(), and resolve().

◆ result_free()

◆ result_op()

◆ result_set()

static void result_set ( struct context ctx,
enum set_type  set_type,
const char *  op,
int  len,
struct result out 
)
static

◆ set_double()

static void set_double ( struct result res,
double  val 
)
static

◆ set_int()

static void set_int ( struct result res,
int  val 
)
static

References result::attr, attr::num, result_free(), attr::type, and attr::u.

Referenced by result_op().