Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add the new integrated files version (use xbt data structures instead my own data...
[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->command_line = NULL;\r
29         context->exit_code = 0;\r
30         context->timeout = INDEFINITE;\r
31         context->input = xbt_strbuff_new();\r
32         context->output = xbt_strbuff_new();\r
33         context->signal = INDEFINITE_SIGNAL;\r
34         context->output_handling = oh_check;\r
35         context->async = 0;\r
36         \r
37         return context;\r
38 }\r
39 \r
40 int\r
41 context_free(context_t* ptr)\r
42 {\r
43         /* TODO : check the parameter */\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)->signal)\r
51                 free((*ptr)->signal);\r
52 \r
53         *ptr = NULL;\r
54         \r
55         return 0;\r
56 }\r
57 \r
58 int\r
59 context_reset(context_t context)\r
60 {\r
61         \r
62         /* TODO : check the parameter */\r
63         \r
64         context->line = NULL;\r
65         context->command_line = NULL;\r
66 \r
67         if(context->input)\r
68                 xbt_strbuff_empty(context->input);\r
69 \r
70         if(context->output)\r
71                 xbt_strbuff_empty(context->output);\r
72         \r
73         if(context->signal)\r
74         {\r
75                 free(context->signal);\r
76                 context->signal = NULL;\r
77         }\r
78         \r
79         /* default expected exit code */\r
80         context->exit_code = 0;\r
81 \r
82         context->output_handling = oh_check;\r
83         context->async = 0;\r
84         \r
85         return 0;\r
86 \r
87 }\r
88 \r
89 context_t\r
90 context_dup(context_t context)\r
91 {\r
92         \r
93         context_t dup;\r
94         \r
95         /* TODO : check the parameter */\r
96         \r
97         dup = xbt_new0(s_context_t, 1);\r
98         \r
99         dup->line = context->line;\r
100         dup->command_line = context->command_line;\r
101         dup->exit_code = context->exit_code;\r
102         dup->timeout = context->timeout;\r
103         dup->output = NULL;\r
104         dup->input = NULL;\r
105         dup->signal = NULL;\r
106         \r
107         if(context->input->used)\r
108         {\r
109                 dup->input = xbt_strbuff_new();\r
110                 xbt_strbuff_append(dup->input,context->input->data);\r
111         }\r
112 \r
113         if(context->output->used)\r
114         {\r
115                 dup->output = xbt_strbuff_new();\r
116                 xbt_strbuff_append(dup->output,context->output->data);\r
117         }\r
118 \r
119         if(context->signal)\r
120         {\r
121                 if(!(dup->signal = strdup(context->signal)))\r
122                 {\r
123                         free(dup);\r
124                         return NULL;\r
125                 }\r
126         }\r
127 \r
128         dup->output_handling = context->output_handling;\r
129         \r
130         dup->async = context->async;\r
131         \r
132         return dup;\r
133 }\r
134 \r
135 void\r
136 context_clear(context_t context)\r
137 {\r
138         context->line = NULL;\r
139         context->command_line = NULL;\r
140         context->exit_code = 0;\r
141         context->timeout = INDEFINITE;\r
142         \r
143         if(context->input)\r
144                 xbt_strbuff_empty(context->input);\r
145 \r
146         if(context->output)\r
147                 xbt_strbuff_empty(context->output);\r
148         \r
149         if(context->signal)\r
150         {\r
151                 free(context->signal);\r
152                 context->signal = INDEFINITE_SIGNAL;\r
153         }\r
154 \r
155         context->output_handling = oh_check;\r
156         context->async = 0;\r
157 \r
158 }\r
159 \r
160 void\r
161 context_input_write(context_t context, const char* buffer)\r
162 {\r
163         xbt_strbuff_append(context->input, buffer);\r
164 }\r
165 \r
166 void\r
167 context_ouput_read(context_t context, const char* buffer)\r
168 {\r
169         xbt_strbuff_append(context->output, buffer);\r
170 }\r
171 \r
172 \r
173 \r