Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
maxmin: be more explicit about the leaked variables
[simgrid.git] / src / xbt / automaton / automatonparse_promela.c
1 /* methods for implementation of automaton from promela description */
2
3 /* Copyright (c) 2011-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "src/internal_config.h"
10 #include "xbt/automaton.h"
11 #include <errno.h>
12 #include <string.h>   /* strerror */
13 #if HAVE_UNISTD_H
14 # include <unistd.h>   /* isatty */
15 #endif
16
17 static xbt_automaton_t parsed_automaton;
18 char* state_id_src;
19
20 static void new_state(char* id, int src){
21
22   char* id_copy = xbt_strdup(id);
23   char* first_part = strtok(id_copy,"_");
24   int type = 0 ; // -1=initial state; 0=intermediate state; 1=final state
25
26   if(strcmp(first_part,"accept")==0){
27     type = 1;
28   }else{
29     char* second_part = strtok(NULL,"_");
30     if(strcmp(second_part,"init")==0){
31       type = -1;
32     }
33   }
34   free(id_copy);
35
36   xbt_automaton_state_t state = NULL;
37   state = xbt_automaton_state_exists(parsed_automaton, id);
38   if(state == NULL){
39     state = xbt_automaton_state_new(parsed_automaton, type, id);
40   }
41
42   if(type==-1)
43     parsed_automaton->current_state = state;
44
45   if(src) {
46     if (state_id_src)
47       free(state_id_src);
48     state_id_src = xbt_strdup(id);
49   }
50 }
51
52 static void new_transition(char* id, xbt_automaton_exp_label_t label)
53 {
54   xbt_automaton_state_t state_dst = NULL;
55   new_state(id, 0);
56   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_transition_t trans = NULL;
60   xbt_automaton_transition_new(parsed_automaton, state_src, state_dst, label);
61
62 }
63
64 static xbt_automaton_exp_label_t new_label(int type, ...){
65   xbt_automaton_exp_label_t label = NULL;
66   xbt_automaton_exp_label_t left;
67   xbt_automaton_exp_label_t right;
68   xbt_automaton_exp_label_t exp_not;
69   char *p;
70
71   va_list ap;
72   va_start(ap,type);
73   switch(type){
74   case 0 :
75     left = va_arg(ap, xbt_automaton_exp_label_t);
76     right = va_arg(ap, xbt_automaton_exp_label_t);
77     label = xbt_automaton_exp_label_new(type, left, right);
78     break;
79   case 1 :
80     left = va_arg(ap, xbt_automaton_exp_label_t);
81     right = va_arg(ap, xbt_automaton_exp_label_t);
82     label = xbt_automaton_exp_label_new(type, left, right);
83     break;
84   case 2 :
85     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
86     label = xbt_automaton_exp_label_new(type, exp_not);
87     break;
88   case 3 :
89     p = va_arg(ap, char*);
90     label = xbt_automaton_exp_label_new(type, p);
91     break;
92   case 4 :
93     label = xbt_automaton_exp_label_new(type);
94     break;
95   }
96   va_end(ap);
97   return label;
98 }
99
100 #include "parserPromela.tab.cacc"
101
102 void xbt_automaton_load(xbt_automaton_t a, const char *file)
103 {
104   parsed_automaton = a;
105   yyin = fopen(file, "r");
106   if (yyin == NULL)
107     xbt_die("Failed to open automaton file `%s': %s", file, strerror(errno));
108   yyparse();
109 }