Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert svn logs into ChangeLog (up to r7858 for now)
[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\rXBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
17 \r\r
18 #define INDEFINITE_SIGNAL       NULL\r
19     \rcontext_t \r context_new(void) \r
20 {
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;
31   \rcontext->async = 0;
32   \r\r
33 #ifdef _XBT_WIN32\r
34       context->t_command_line = NULL;
35   \rcontext->is_not_found = 0;
36   \r
37 #endif  /* \r */
38       \rreturn context;
39 \r}
40
41 \r\rint \r context_free(context_t * ptr) \r
42 {
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);
49   \r\rif ((*ptr)->pos)
50     \rfree((*ptr)->pos);
51   \r\rif ((*ptr)->signal)
52     \rfree((*ptr)->signal);
53   \r\r
54 #ifdef _XBT_WIN32\r
55       if ((*ptr)->t_command_line)
56     \rfree((*ptr)->t_command_line);
57   \r
58 #endif  /* \r */
59       \r*ptr = NULL;
60   \r\rreturn 0;
61 \r}
62
63 \r\rint \r context_reset(context_t context) \r
64 {
65   \rcontext->line = NULL;
66   \rcontext->pos = NULL;
67   \r\rif (context->command_line)
68     \r {
69     \rfree(context->command_line);
70     \rcontext->command_line = NULL;
71     \r}
72   \r\r
73 #ifdef _XBT_WIN32\r
74       if (context->t_command_line)
75     \r {
76     \rfree(context->t_command_line);
77     \rcontext->t_command_line = NULL;
78     \r}
79   \r\rcontext->is_not_found = 0;
80   \r\r
81 #endif  /* \r */
82       \rif (context->pos)
83     \r {
84     \rfree(context->pos);
85     \rcontext->pos = NULL;
86     \r}
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)
92     \r {
93     \rfree(context->signal);
94     \rcontext->signal = NULL;
95     \r}
96   \r\r
97       /* default expected exit code */ \r
98       context->exit_code = 0;
99   \r\rcontext->output_handling = oh_check;
100   \rcontext->async = 0;
101   \r\rreturn 0;
102 \r\r}
103
104 \r\rcontext_t \r context_dup(context_t context) \r
105 {
106   \rcontext_t dup;
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);
111   \r\r\r
112 #ifdef _XBT_WIN32\r
113       dup->t_command_line = strdup(context->t_command_line);
114   \rdup->is_not_found = context->is_not_found;
115   \r
116 #endif  /* \r */
117       \rdup->exit_code = context->exit_code;
118   \rdup->timeout = context->timeout;
119   \rdup->output = NULL;
120   \rdup->input = NULL;
121   \rdup->signal = NULL;
122   \r\rif (context->input->used)
123     \r {
124     \rdup->input = xbt_strbuff_new();
125     \rxbt_strbuff_append(dup->input, context->input->data);
126     \r}
127   \r\rdup->output = xbt_strbuff_new();
128   \r\rif (context->output->used)
129     \r {
130     \rxbt_strbuff_append(dup->output, context->output->data);
131     \r}
132   \r\rif (context->signal)
133     \r {
134     \rif (!(dup->signal = strdup(context->signal)))
135       \r {
136       \rfree(dup);
137       \rreturn NULL;
138       \r}
139     \r}
140   \r\rdup->output_handling = context->output_handling;
141   \r\rdup->async = context->async;
142   \r\rreturn dup;
143 \r}
144
145 \r\rvoid \r context_clear(context_t context) \r
146 {
147   \rcontext->line = NULL;
148   \rcontext->pos = NULL;
149   \r\rif (context->command_line)
150     \r {
151     \rfree(context->command_line);
152     \rcontext->command_line = NULL;
153     \r}
154   \r\r
155 #ifdef _XBT_WIN32\r
156       if (context->t_command_line)
157     \r {
158     \rfree(context->t_command_line);
159     \rcontext->t_command_line = NULL;
160     \r}
161   \rcontext->is_not_found = 0;
162   \r\r
163 #endif  /* \r */
164       \rif (context->pos)
165     \r {
166     \rfree(context->pos);
167     \rcontext->pos = NULL;
168     \r}
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)
176     \r {
177     \rfree(context->signal);
178     \rcontext->signal = INDEFINITE_SIGNAL;
179     \r}
180   \r\rcontext->output_handling = oh_check;
181   \rcontext->async = 0;
182 \r\r}
183
184 \r\rvoid \r context_input_write(context_t context, const char *buffer) \r
185 {
186   \rxbt_strbuff_append(context->input, buffer);
187 \r\r\rvoid \r context_ouput_read(context_t context, const char *buffer) \r
188 {
189   \rxbt_strbuff_append(context->output, buffer);
190 \r\r\r\r\r