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 "simgrid_config.h" /* getline */
7 #include "msg/private.h"
11 static xbt_dict_t action_funs;
12 static xbt_dynar_t action_list;
15 /** \ingroup msg_actions
16 * \brief Registers a function to handle a kind of action
18 * Registers a function to handle a kind of action
19 * This table is then used by #MSG_action_trace_run
21 * The argument of the function is the line describing the action, splitted on spaces with xbt_str_split_quoted()
23 * \param name the reference name of the action.
24 * \param code the function; prototype given by the type: void...(xbt_dynar_t action)
26 void MSG_action_register(const char *action_name, msg_action_fun function)
28 xbt_dict_set(action_funs, action_name, function, NULL);
31 /** \ingroup msg_actions
32 * \brief Unregisters a function, which handled a kind of action
34 * \param name the reference name of the action.
36 void MSG_action_unregister(const char *action_name)
38 xbt_dict_remove(action_funs, action_name);
41 static int MSG_action_runner(int argc, char *argv[])
46 xbt_dynar_foreach(action_list, cursor, evt) {
47 if (!strcmp(xbt_dynar_get_as(evt, 0, char *), argv[0])) {
48 msg_action_fun function =
49 xbt_dict_get(action_funs, xbt_dynar_get_as(evt, 1, char *));
57 void _MSG_action_init()
59 action_funs = xbt_dict_new();
60 action_list = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);
61 MSG_function_register_default(MSG_action_runner);
64 /** \ingroup msg_actions
65 * \brief A trace loader
67 * Load a trace file containing actions, and execute them.
69 MSG_error_t MSG_action_trace_run(char *path)
80 fp = fopen(path, "r");
81 xbt_assert2(fp != NULL, "Cannot open %s: %s", path, strerror(errno));
83 while ((read = getline(&line, &len, fp)) != -1) {
84 char *comment = strchr(line, '#');
87 xbt_str_trim(line, NULL);
90 evt = xbt_str_split_quoted(line);
91 xbt_dynar_push(action_list, &evt);
99 xbt_dynar_free(&action_list);
100 action_list = xbt_dynar_new(sizeof(xbt_dynar_t), xbt_dynar_free_voidp);