Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless check for null before free.
[simgrid.git] / src / xbt / automaton / automatonparse_promela.c
1 /* methods for implementation of automaton from promela description */
2
3 /* Copyright (c) 2011-2018. 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
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_automaton);
18
19 static xbt_automaton_t parsed_automaton;
20 char* state_id_src;
21
22 static void new_state(char* id, int src){
23   char* saveptr = NULL; // for strtok_r()
24   char* id_copy = xbt_strdup(id);
25   char* first_part = strtok_r(id_copy, "_", &saveptr);
26   int type = 0 ; // -1=initial state; 0=intermediate state; 1=final state
27
28   if(strcmp(first_part,"accept")==0){
29     type = 1;
30   }else{
31     char* second_part = strtok_r(NULL, "_", &saveptr);
32     if(strcmp(second_part,"init")==0){
33       type = -1;
34     }
35   }
36   xbt_free(id_copy);
37
38   xbt_automaton_state_t state = xbt_automaton_state_exists(parsed_automaton, id);
39   if(state == NULL){
40     state = xbt_automaton_state_new(parsed_automaton, type, id);
41   }
42
43   if(type==-1)
44     parsed_automaton->current_state = state;
45
46   if(src) {
47     xbt_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   new_state(id, 0);
55   xbt_automaton_state_t state_dst = xbt_automaton_state_exists(parsed_automaton, id);
56   xbt_automaton_state_t state_src = xbt_automaton_state_exists(parsed_automaton, state_id_src);
57
58   //xbt_transition_t trans = NULL;
59   xbt_automaton_transition_new(parsed_automaton, state_src, state_dst, label);
60
61 }
62
63 static xbt_automaton_exp_label_t new_label(int type, ...){
64   xbt_automaton_exp_label_t label = NULL;
65   xbt_automaton_exp_label_t left;
66   xbt_automaton_exp_label_t right;
67   xbt_automaton_exp_label_t exp_not;
68   char *p;
69
70   va_list ap;
71   va_start(ap,type);
72   switch(type){
73   case 0 :
74     left = va_arg(ap, xbt_automaton_exp_label_t);
75     right = va_arg(ap, xbt_automaton_exp_label_t);
76     label = xbt_automaton_exp_label_new(type, left, right);
77     break;
78   case 1 :
79     left = va_arg(ap, xbt_automaton_exp_label_t);
80     right = va_arg(ap, xbt_automaton_exp_label_t);
81     label = xbt_automaton_exp_label_new(type, left, right);
82     break;
83   case 2 :
84     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
85     label = xbt_automaton_exp_label_new(type, exp_not);
86     break;
87   case 3 :
88     p = va_arg(ap, char*);
89     label = xbt_automaton_exp_label_new(type, p);
90     break;
91   case 4 :
92     label = xbt_automaton_exp_label_new(type);
93     break;
94   default:
95     XBT_DEBUG("Invalid type: %d", type);
96     break;
97   }
98   va_end(ap);
99   return label;
100 }
101
102 #include "parserPromela.tab.cacc"
103
104 void xbt_automaton_load(xbt_automaton_t a, const char *file)
105 {
106   parsed_automaton = a;
107   yyin = fopen(file, "r");
108   if (yyin == NULL)
109     xbt_die("Failed to open automaton file `%s': %s", file, strerror(errno));
110   yyparse();
111 }