Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
877ffc22bf193f8de75fcdb44d72a88d39a6e672
[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 WIN32\r
39         context->t_command_line = NULL;\r
40         #endif\r
41         \r
42         return context;\r
43 }\r
44 \r
45 int\r
46 context_free(context_t* ptr)\r
47 {\r
48         if(((*ptr)->input))\r
49                 xbt_strbuff_free(((*ptr)->input));\r
50 \r
51         if(((*ptr)->output))\r
52                 xbt_strbuff_free(((*ptr)->output));\r
53         \r
54         if((*ptr)->command_line)\r
55                 free((*ptr)->command_line);\r
56 \r
57         if((*ptr)->pos)\r
58                 free((*ptr)->pos);\r
59 \r
60         if((*ptr)->signal)\r
61                 free((*ptr)->signal);\r
62 \r
63         #ifdef WIN32\r
64         if((*ptr)->t_command_line)\r
65                 free((*ptr)->t_command_line);\r
66         #endif\r
67 \r
68         *ptr = NULL;\r
69         \r
70         return 0;\r
71 }\r
72 \r
73 int\r
74 context_reset(context_t context)\r
75 {\r
76         context->line = NULL;\r
77         context->pos = NULL;\r
78 \r
79         if(context->command_line)\r
80         {\r
81                 free(context->command_line);\r
82                 context->command_line = NULL;\r
83         }\r
84 \r
85         #ifdef WIN32\r
86         if(context->t_command_line)\r
87         {\r
88                 free(context->t_command_line);\r
89                 context->t_command_line = NULL;\r
90         }\r
91         #endif\r
92 \r
93         if(context->pos)\r
94         {\r
95                 free(context->pos);\r
96                 context->pos = NULL;\r
97         }\r
98 \r
99         if(context->input)\r
100                 xbt_strbuff_empty(context->input);\r
101 \r
102         if(context->output)\r
103                 xbt_strbuff_empty(context->output);\r
104         \r
105         if(context->signal)\r
106         {\r
107                 free(context->signal);\r
108                 context->signal = NULL;\r
109         }\r
110         \r
111         /* default expected exit code */\r
112         context->exit_code = 0;\r
113 \r
114         context->output_handling = oh_check;\r
115         context->async = 0;\r
116         \r
117         return 0;\r
118 \r
119 }\r
120 \r
121 context_t\r
122 context_dup(context_t context)\r
123 {\r
124         context_t dup;\r
125         \r
126         dup = xbt_new0(s_context_t, 1);\r
127         \r
128         dup->line = context->line;\r
129         dup->pos = strdup(context->pos);\r
130         dup->command_line = strdup(context->command_line);\r
131 \r
132         \r
133         #ifdef WIN32\r
134         dup->t_command_line = strdup(context->t_command_line);\r
135         #endif\r
136 \r
137         dup->exit_code = context->exit_code;\r
138         dup->timeout = context->timeout;\r
139         dup->output = NULL;\r
140         dup->input = NULL;\r
141         dup->signal = NULL;\r
142         \r
143         if(context->input->used)\r
144         {\r
145                 dup->input = xbt_strbuff_new();\r
146                 xbt_strbuff_append(dup->input,context->input->data);\r
147         }\r
148         \r
149         dup->output = xbt_strbuff_new();\r
150 \r
151         if(context->output->used)\r
152         {\r
153                 xbt_strbuff_append(dup->output,context->output->data);\r
154         }\r
155 \r
156         if(context->signal)\r
157         {\r
158                 if(!(dup->signal = strdup(context->signal)))\r
159                 {\r
160                         free(dup);\r
161                         return NULL;\r
162                 }\r
163         }\r
164 \r
165         dup->output_handling = context->output_handling;\r
166         \r
167         dup->async = context->async;\r
168         \r
169         return dup;\r
170 }\r
171 \r
172 void\r
173 context_clear(context_t context)\r
174 {\r
175         context->line = NULL;\r
176         context->pos = NULL;\r
177         \r
178         if(context->command_line)\r
179         {\r
180                 free(context->command_line);\r
181                 context->command_line = NULL;\r
182         }\r
183 \r
184         #ifdef WIN32\r
185         if(context->t_command_line)\r
186         {\r
187                 free(context->t_command_line);\r
188                 context->t_command_line = NULL;\r
189         }\r
190         #endif\r
191 \r
192         if(context->pos)\r
193         {\r
194                 free(context->pos);\r
195                 context->pos = NULL;\r
196         }\r
197 \r
198         context->exit_code = 0;\r
199         context->timeout = INDEFINITE;\r
200         \r
201         if(context->input)\r
202                 xbt_strbuff_empty(context->input);\r
203 \r
204         if(context->output)\r
205                 xbt_strbuff_empty(context->output);\r
206         \r
207         if(context->signal)\r
208         {\r
209                 free(context->signal);\r
210                 context->signal = INDEFINITE_SIGNAL;\r
211         }\r
212 \r
213         context->output_handling = oh_check;\r
214         context->async = 0;\r
215 \r
216 }\r
217 \r
218 void\r
219 context_input_write(context_t context, const char* buffer)\r
220 {\r
221         xbt_strbuff_append(context->input, buffer);\r
222 }\r
223 \r
224 void\r
225 context_ouput_read(context_t context, const char* buffer)\r
226 {\r
227         xbt_strbuff_append(context->output, buffer);\r
228 }\r
229 \r
230 \r
231 \r