Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tesh version 2
[simgrid.git] / tools / tesh2 / src / context.c
1 #include <context.h>
2
3
4 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
5
6 #define INDEFINITE_SIGNAL       NULL
7
8 context_t
9 context_new(void)
10 {
11         context_t context = xbt_new0(s_context_t, 1);
12         
13         context->line = NULL;
14         context->command_line = NULL;
15         context->exit_code = 0;
16         context->timeout = INDEFINITE;
17         context->input = xbt_strbuff_new();
18         context->output = xbt_strbuff_new();
19         context->signal = INDEFINITE_SIGNAL;
20         context->output_handling = oh_check;
21         context->async = 0;
22         
23         return context;
24 }
25
26 context_t
27 context_dup(context_t context)
28 {
29         
30         context_t dup = xbt_new0(s_context_t, 1);
31         
32         dup->line = context->line;
33         dup->command_line = context->command_line;
34         dup->exit_code = context->exit_code;
35         dup->timeout = context->timeout;
36         dup->output = NULL;
37         dup->input = NULL;
38         dup->signal = NULL;
39         
40         if(context->input->used)
41         {
42                 dup->input = xbt_strbuff_new();
43                 xbt_strbuff_append(dup->input,context->input->data);
44         }
45
46         if(context->output->used)
47         {
48                 dup->output = xbt_strbuff_new();
49                 xbt_strbuff_append(dup->output,context->output->data);
50         }
51
52         if(context->signal)
53                 dup->signal = strdup(context->signal);
54
55         dup->output_handling = context->output_handling;
56         
57         dup->async = context->async;
58         
59         return dup;
60 }
61
62 void
63 context_clear(context_t context)
64 {
65         context->line = NULL;
66         context->command_line = NULL;
67         context->exit_code = 0;
68         context->timeout = INDEFINITE;
69         
70         if(context->input)
71                 xbt_strbuff_empty(context->input);
72
73         if(context->output)
74                 xbt_strbuff_empty(context->output);
75         
76         if(context->signal)
77         {
78                 free(context->signal);
79                 context->signal = INDEFINITE_SIGNAL;
80         }
81
82         context->output_handling = oh_check;
83         context->async = 0;
84
85 }
86
87 void
88 context_reset(context_t context)
89 {
90         context->line = NULL;
91         context->command_line = NULL;
92
93         if(context->input)
94                 xbt_strbuff_empty(context->input);
95
96         if(context->output)
97                 xbt_strbuff_empty(context->output);
98         
99         if(context->signal)
100         {
101                 free(context->signal);
102                 context->signal = NULL;
103         }
104         
105         /* default expected exit code */
106         context->exit_code = 0;
107
108         context->output_handling = oh_check;
109         context->async = 0;
110
111 }
112
113 void
114 context_input_write(context_t context, const char* buffer)
115 {
116         xbt_strbuff_append(context->input, buffer);
117 }
118
119 void
120 context_ouput_read(context_t context, const char* buffer)
121 {
122         xbt_strbuff_append(context->output, buffer);
123 }
124
125 void
126 context_free(context_t* context)
127 {
128         if(((*context)->input))
129                 xbt_strbuff_free(((*context)->input));
130
131         if(((*context)->output))
132                 xbt_strbuff_free(((*context)->output));
133         
134         if((*context)->signal)
135                 free((*context)->signal);
136
137         *context = NULL;
138 }
139