Logo AND Algorithmique Numérique Distribuée

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