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) {
26 xbt_dict_set(action_funs,action_name,function,NULL);
28 /** \ingroup msg_actions
29 * \brief Unregisters a function, which handled a kind of action
31 * \param name the reference name of the action.
33 void MSG_action_unregister(const char*action_name) {
34 xbt_dict_remove(action_funs,action_name);
37 static int MSG_action_runner(int argc, char* argv[]) {
41 xbt_dynar_foreach(action_list,cursor,evt) {
42 if (!strcmp(xbt_dynar_get_as(evt,0,char*),argv[0])) {
43 msg_action_fun function = xbt_dict_get(action_funs,xbt_dynar_get_as(evt,1,char*));
51 void _MSG_action_init() {
52 action_funs = xbt_dict_new();
53 action_list = xbt_dynar_new(sizeof(xbt_dynar_t),xbt_dynar_free_voidp);
54 MSG_function_register_default(MSG_action_runner);
57 /** \ingroup msg_actions
58 * \brief A trace loader
60 * Load a trace file containing actions, and execute them.
62 MSG_error_t MSG_action_trace_run(char*path) {
72 fp = fopen(path, "r");
73 xbt_assert2(fp != NULL,"Cannot open %s: %s",path,strerror(errno));
75 while ((read = getline(&line, &len, fp)) != -1) {
76 char *comment = strchr(line,'#');
79 xbt_str_trim(line,NULL);
82 evt = xbt_str_split_quoted(line);
83 xbt_dynar_push(action_list,&evt);
91 xbt_dynar_free(&action_list);
92 action_list = xbt_dynar_new(sizeof(xbt_dynar_t),xbt_dynar_free_voidp);