1 /* Copyright (c) 2009 The SimGrid team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "msg/private.h"
10 static xbt_dict_t action_funs;
11 static xbt_dynar_t action_list;
14 /** \ingroup msg_actions
15 * \brief Registers a function to handle a kind of action
17 * Registers a function to handle a kind of action
18 * This table is then used by #MSG_action_trace_run
20 * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
22 * \param name the reference name of the action.
23 * \param code the function; prototype given by the type: void...(xbt_dynar_t action)
25 void MSG_action_register(const char *action_name, msg_action_fun function)
27 xbt_dict_set(action_funs, action_name, function, NULL);
30 /** \ingroup msg_actions
31 * \brief Unregisters a function, which handled a kind of action
33 * \param name the reference name of the action.
35 void MSG_action_unregister(const char *action_name)
37 xbt_dict_remove(action_funs, action_name);
40 static int MSG_action_runner(int argc, char *argv[])
45 xbt_dynar_foreach(action_list, cursor, evt) {
46 if (!strcmp(xbt_dynar_get_as(evt, 0, char *), argv[0])) {
47 msg_action_fun function =
48 xbt_dict_get(action_funs, xbt_dynar_get_as(evt, 1, char *));
56 void _MSG_action_init()
58 action_funs = xbt_dict_new();
59 action_list = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
60 MSG_function_register_default(MSG_action_runner);
63 /** \ingroup msg_actions
64 * \brief A trace loader
66 * Load a trace file containing actions, and execute them.
68 MSG_error_t MSG_action_trace_run(char *path)
79 fp = fopen(path, "r");
80 xbt_assert2(fp != NULL, "Cannot open %s: %s", path, strerror(errno));
82 while ((read = getline(&line, &len, fp)) != -1) {
83 char *comment = strchr(line, '#');
86 xbt_str_trim(line, NULL);
89 evt = xbt_str_split_quoted(line);
90 xbt_dynar_push(action_list, &evt);
98 xbt_dynar_free(&action_list);
99 action_list = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);