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
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
\r
20 #define INDEFINITE_SIGNAL NULL
\r
25 context_t context = xbt_new0(s_context_t,1);
\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
42 context_free(context_t* ptr)
\r
45 xbt_strbuff_free(((*ptr)->input));
\r
47 if(((*ptr)->output))
\r
48 xbt_strbuff_free(((*ptr)->output));
\r
50 if((*ptr)->command_line)
\r
51 free((*ptr)->command_line);
\r
57 free((*ptr)->signal);
\r
65 context_reset(context_t context)
\r
67 context->line = NULL;
\r
68 context->pos = NULL;
\r
70 if(context->command_line)
\r
72 free(context->command_line);
\r
73 context->command_line = NULL;
\r
79 context->pos = NULL;
\r
83 xbt_strbuff_empty(context->input);
\r
86 xbt_strbuff_empty(context->output);
\r
90 free(context->signal);
\r
91 context->signal = NULL;
\r
94 /* default expected exit code */
\r
95 context->exit_code = 0;
\r
97 context->output_handling = oh_check;
\r
105 context_dup(context_t context)
\r
109 dup = xbt_new0(s_context_t, 1);
\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
118 dup->signal = NULL;
\r
120 if(context->input->used)
\r
122 dup->input = xbt_strbuff_new();
\r
123 xbt_strbuff_append(dup->input,context->input->data);
\r
126 dup->output = xbt_strbuff_new();
\r
128 if(context->output->used)
\r
130 xbt_strbuff_append(dup->output,context->output->data);
\r
133 if(context->signal)
\r
135 if(!(dup->signal = strdup(context->signal)))
\r
142 dup->output_handling = context->output_handling;
\r
144 dup->async = context->async;
\r
150 context_clear(context_t context)
\r
152 context->line = NULL;
\r
153 context->pos = NULL;
\r
155 if(context->command_line)
\r
157 free(context->command_line);
\r
158 context->command_line = NULL;
\r
163 free(context->pos);
\r
164 context->pos = NULL;
\r
167 context->exit_code = 0;
\r
168 context->timeout = INDEFINITE;
\r
171 xbt_strbuff_empty(context->input);
\r
173 if(context->output)
\r
174 xbt_strbuff_empty(context->output);
\r
176 if(context->signal)
\r
178 free(context->signal);
\r
179 context->signal = INDEFINITE_SIGNAL;
\r
182 context->output_handling = oh_check;
\r
183 context->async = 0;
\r
188 context_input_write(context_t context, const char* buffer)
\r
190 xbt_strbuff_append(context->input, buffer);
\r
194 context_ouput_read(context_t context, const char* buffer)
\r
196 xbt_strbuff_append(context->output, buffer);
\r