Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Remove useless commented lines of code.
[simgrid.git] / src / xbt / automaton / automatonparse_promela.c
1 /* methods for implementation of automaton from promela description */
2
3 /* Copyright (c) 2011-2019. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "src/internal_config.h"
9 #include "xbt/automaton.h"
10 #include <errno.h>
11 #include <string.h>   /* strerror */
12 #if HAVE_UNISTD_H
13 # include <unistd.h>   /* isatty */
14 #endif
15 #include <xbt/log.h>
16 #include <xbt/sysdep.h>
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_automaton);
19
20 static xbt_automaton_t parsed_automaton;
21 char* state_id_src;
22
23 static void new_state(char* id, int src){
24   char* saveptr = NULL; // for strtok_r()
25   char* id_copy = xbt_strdup(id);
26   char* first_part = strtok_r(id_copy, "_", &saveptr);
27   int type = 0 ; // -1=initial state; 0=intermediate state; 1=final state
28
29   if(strcmp(first_part,"accept")==0){
30     type = 1;
31   }else{
32     char* second_part = strtok_r(NULL, "_", &saveptr);
33     if(strcmp(second_part,"init")==0){
34       type = -1;
35     }
36   }
37   xbt_free(id_copy);
38
39   xbt_automaton_state_t state = xbt_automaton_state_exists(parsed_automaton, id);
40   if(state == NULL){
41     state = xbt_automaton_state_new(parsed_automaton, type, id);
42   }
43
44   if(type==-1)
45     parsed_automaton->current_state = state;
46
47   if(src) {
48     xbt_free(state_id_src);
49     state_id_src = xbt_strdup(id);
50   }
51 }
52
53 static void new_transition(char* id, xbt_automaton_exp_label_t label)
54 {
55   new_state(id, 0);
56   xbt_automaton_state_t state_dst = xbt_automaton_state_exists(parsed_automaton, id);
57   xbt_automaton_state_t state_src = xbt_automaton_state_exists(parsed_automaton, state_id_src);
58
59   xbt_automaton_transition_new(parsed_automaton, state_src, state_dst, label);
60
61 }
62
63 #include "parserPromela.tab.cacc"
64
65 void xbt_automaton_load(xbt_automaton_t a, const char *file)
66 {
67   parsed_automaton = a;
68   yyin = fopen(file, "r");
69   if (yyin == NULL)
70     xbt_die("Failed to open automaton file `%s': %s", file, strerror(errno));
71   yyparse();
72 }