Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define correctly variables for windows.
[simgrid.git] / tools / tesh2 / src / context.c
1 /*\r
2  * src/context.c - type representing the tesh file stream.\r
3  *\r
4  * Copyright 2008,2009 Martin Quinson, Malek Cherier All right reserved. \r
5  *\r
6  * This program is free software; you can redistribute it and/or modify it \r
7  * under the terms of the license (GNU LGPL) which comes with this package.\r
8  *\r
9  * Purpose:\r
10  *              This file contains all the definitions of the functions related with\r
11  *              the tesh context type.\r
12  *\r
13  */\r
14  \r
15 #include <context.h>\r
16 \r
17 \r
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);\r
19 \r
20 #define INDEFINITE_SIGNAL       NULL\r
21 \r
22 context_t\r
23 context_new(void)\r
24 {\r
25         context_t context = xbt_new0(s_context_t,1);\r
26         \r
27         context->line = NULL;\r
28         context->pos = NULL;\r
29         context->command_line = NULL;\r
30         context->exit_code = 0;\r
31         context->timeout = INDEFINITE;\r
32         context->input = xbt_strbuff_new();\r
33         context->output = xbt_strbuff_new();\r
34         context->signal = INDEFINITE_SIGNAL;\r
35         context->output_handling = oh_check;\r
36         context->async = 0;\r
37 \r
38         #ifdef _XBT_WIN32\r
39         context->t_command_line = NULL;\r
40         context->is_not_found = 0;\r
41         #endif\r
42         \r
43         return context;\r
44 }\r
45 \r
46 int\r
47 context_free(context_t* ptr)\r
48 {\r
49         if(((*ptr)->input))\r
50                 xbt_strbuff_free(((*ptr)->input));\r
51 \r
52         if(((*ptr)->output))\r
53                 xbt_strbuff_free(((*ptr)->output));\r
54         \r
55         if((*ptr)->command_line)\r
56                 free((*ptr)->command_line);\r
57 \r
58         if((*ptr)->pos)\r
59                 free((*ptr)->pos);\r
60 \r
61         if((*ptr)->signal)\r
62                 free((*ptr)->signal);\r
63 \r
64         #ifdef _XBT_WIN32\r
65         if((*ptr)->t_command_line)\r
66                 free((*ptr)->t_command_line);\r
67         #endif\r
68 \r
69         *ptr = NULL;\r
70         \r
71         return 0;\r
72 }\r
73 \r
74 int\r
75 context_reset(context_t context)\r
76 {\r
77         context->line = NULL;\r
78         context->pos = NULL;\r
79 \r
80         if(context->command_line)\r
81         {\r
82                 free(context->command_line);\r
83                 context->command_line = NULL;\r
84         }\r
85 \r
86         #ifdef _XBT_WIN32\r
87         if(context->t_command_line)\r
88         {\r
89                 free(context->t_command_line);\r
90                 context->t_command_line = NULL;\r
91         }\r
92 \r
93         context->is_not_found = 0;\r
94 \r
95         #endif\r
96 \r
97         if(context->pos)\r
98         {\r
99                 free(context->pos);\r
100                 context->pos = NULL;\r
101         }\r
102 \r
103         if(context->input)\r
104                 xbt_strbuff_empty(context->input);\r
105 \r
106         if(context->output)\r
107                 xbt_strbuff_empty(context->output);\r
108         \r
109         if(context->signal)\r
110         {\r
111                 free(context->signal);\r
112                 context->signal = NULL;\r
113         }\r
114         \r
115         /* default expected exit code */\r
116         context->exit_code = 0;\r
117 \r
118         context->output_handling = oh_check;\r
119         context->async = 0;\r
120         \r
121         return 0;\r
122 \r
123 }\r
124 \r
125 context_t\r
126 context_dup(context_t context)\r
127 {\r
128         context_t dup;\r
129         \r
130         dup = xbt_new0(s_context_t, 1);\r
131         \r
132         dup->line = context->line;\r
133         dup->pos = strdup(context->pos);\r
134         dup->command_line = strdup(context->command_line);\r
135 \r
136         \r
137         #ifdef _XBT_WIN32\r
138         dup->t_command_line = strdup(context->t_command_line);\r
139         dup->is_not_found = context->is_not_found;\r
140         #endif\r
141 \r
142         dup->exit_code = context->exit_code;\r
143         dup->timeout = context->timeout;\r
144         dup->output = NULL;\r
145         dup->input = NULL;\r
146         dup->signal = NULL;\r
147         \r
148         if(context->input->used)\r
149         {\r
150                 dup->input = xbt_strbuff_new();\r
151                 xbt_strbuff_append(dup->input,context->input->data);\r
152         }\r
153         \r
154         dup->output = xbt_strbuff_new();\r
155 \r
156         if(context->output->used)\r
157         {\r
158                 xbt_strbuff_append(dup->output,context->output->data);\r
159         }\r
160 \r
161         if(context->signal)\r
162         {\r
163                 if(!(dup->signal = strdup(context->signal)))\r
164                 {\r
165                         free(dup);\r
166                         return NULL;\r
167                 }\r
168         }\r
169 \r
170         dup->output_handling = context->output_handling;\r
171         \r
172         dup->async = context->async;\r
173         \r
174         return dup;\r
175 }\r
176 \r
177 void\r
178 context_clear(context_t context)\r
179 {\r
180         context->line = NULL;\r
181         context->pos = NULL;\r
182         \r
183         if(context->command_line)\r
184         {\r
185                 free(context->command_line);\r
186                 context->command_line = NULL;\r
187         }\r
188 \r
189         #ifdef _XBT_WIN32\r
190         if(context->t_command_line)\r
191         {\r
192                 free(context->t_command_line);\r
193                 context->t_command_line = NULL;\r
194         }\r
195         context->is_not_found = 0;\r
196 \r
197         #endif\r
198 \r
199         if(context->pos)\r
200         {\r
201                 free(context->pos);\r
202                 context->pos = NULL;\r
203         }\r
204 \r
205         context->exit_code = 0;\r
206         context->timeout = INDEFINITE;\r
207         \r
208         if(context->input)\r
209                 xbt_strbuff_empty(context->input);\r
210 \r
211         if(context->output)\r
212                 xbt_strbuff_empty(context->output);\r
213         \r
214         if(context->signal)\r
215         {\r
216                 free(context->signal);\r
217                 context->signal = INDEFINITE_SIGNAL;\r
218         }\r
219 \r
220         context->output_handling = oh_check;\r
221         context->async = 0;\r
222 \r
223 }\r
224 \r
225 void\r
226 context_input_write(context_t context, const char* buffer)\r
227 {\r
228         xbt_strbuff_append(context->input, buffer);\r
229 }\r
230 \r
231 void\r
232 context_ouput_read(context_t context, const char* buffer)\r
233 {\r
234         xbt_strbuff_append(context->output, buffer);\r
235 }\r
236 \r
237 \r
238 \r