2 * src/context.c - type representing the tesh file stream.
\r
4 * Copyright 2008,2009 Martin Quinson, Malek Cherier All right reserved.
\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
10 * This file contains all the definitions of the functions related with
\r
11 * the tesh context type.
\r
15 #include <context.h>
\r
16 \r\rXBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
18 #define INDEFINITE_SIGNAL NULL
\r
19 \rcontext_t
\r context_new(void)
\r
21 \rcontext_t context = xbt_new0(s_context_t, 1);
22 \r\rcontext->line = NULL;
23 \rcontext->pos = NULL;
24 \rcontext->command_line = NULL;
25 \rcontext->exit_code = 0;
26 \rcontext->timeout = INDEFINITE;
27 \rcontext->input = xbt_strbuff_new();
28 \rcontext->output = xbt_strbuff_new();
29 \rcontext->signal = INDEFINITE_SIGNAL;
30 \rcontext->output_handling = oh_check;
34 context->t_command_line = NULL;
35 \rcontext->is_not_found = 0;
41 \r\rint
\r context_free(context_t * ptr)
\r
43 \rif (((*ptr)->input))
44 \rxbt_strbuff_free(((*ptr)->input));
45 \r\rif (((*ptr)->output))
46 \rxbt_strbuff_free(((*ptr)->output));
47 \r\rif ((*ptr)->command_line)
48 \rfree((*ptr)->command_line);
51 \r\rif ((*ptr)->signal)
52 \rfree((*ptr)->signal);
55 if ((*ptr)->t_command_line)
56 \rfree((*ptr)->t_command_line);
63 \r\rint
\r context_reset(context_t context)
\r
65 \rcontext->line = NULL;
66 \rcontext->pos = NULL;
67 \r\rif (context->command_line)
69 \rfree(context->command_line);
70 \rcontext->command_line = NULL;
74 if (context->t_command_line)
76 \rfree(context->t_command_line);
77 \rcontext->t_command_line = NULL;
79 \r\rcontext->is_not_found = 0;
85 \rcontext->pos = NULL;
87 \r\rif (context->input)
88 \rxbt_strbuff_empty(context->input);
89 \r\rif (context->output)
90 \rxbt_strbuff_empty(context->output);
91 \r\rif (context->signal)
93 \rfree(context->signal);
94 \rcontext->signal = NULL;
97 /* default expected exit code */
\r
98 context->exit_code = 0;
99 \r\rcontext->output_handling = oh_check;
100 \rcontext->async = 0;
104 \r\rcontext_t
\r context_dup(context_t context)
\r
107 \r\rdup = xbt_new0(s_context_t, 1);
108 \r\rdup->line = context->line;
109 \rdup->pos = strdup(context->pos);
110 \rdup->command_line = strdup(context->command_line);
113 dup->t_command_line = strdup(context->t_command_line);
114 \rdup->is_not_found = context->is_not_found;
117 \rdup->exit_code = context->exit_code;
118 \rdup->timeout = context->timeout;
119 \rdup->output = NULL;
121 \rdup->signal = NULL;
122 \r\rif (context->input->used)
124 \rdup->input = xbt_strbuff_new();
125 \rxbt_strbuff_append(dup->input, context->input->data);
127 \r\rdup->output = xbt_strbuff_new();
128 \r\rif (context->output->used)
130 \rxbt_strbuff_append(dup->output, context->output->data);
132 \r\rif (context->signal)
134 \rif (!(dup->signal = strdup(context->signal)))
140 \r\rdup->output_handling = context->output_handling;
141 \r\rdup->async = context->async;
145 \r\rvoid
\r context_clear(context_t context)
\r
147 \rcontext->line = NULL;
148 \rcontext->pos = NULL;
149 \r\rif (context->command_line)
151 \rfree(context->command_line);
152 \rcontext->command_line = NULL;
156 if (context->t_command_line)
158 \rfree(context->t_command_line);
159 \rcontext->t_command_line = NULL;
161 \rcontext->is_not_found = 0;
166 \rfree(context->pos);
167 \rcontext->pos = NULL;
169 \r\rcontext->exit_code = 0;
170 \rcontext->timeout = INDEFINITE;
171 \r\rif (context->input)
172 \rxbt_strbuff_empty(context->input);
173 \r\rif (context->output)
174 \rxbt_strbuff_empty(context->output);
175 \r\rif (context->signal)
177 \rfree(context->signal);
178 \rcontext->signal = INDEFINITE_SIGNAL;
180 \r\rcontext->output_handling = oh_check;
181 \rcontext->async = 0;
184 \r\rvoid
\r context_input_write(context_t context, const char *buffer)
\r
186 \rxbt_strbuff_append(context->input, buffer);
187 \r}
\r\rvoid
\r context_ouput_read(context_t context, const char *buffer)
\r
189 \rxbt_strbuff_append(context->output, buffer);