Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Last corrections of Tesh tool.
[simgrid.git] / tools / tesh2 / src / context.c
index 261d4b7..22a44fc 100644 (file)
-#include <context.h>
-
-
-XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
-
-#define INDEFINITE_SIGNAL      NULL
-
-context_t
-context_new(void)
-{
-       context_t context = xbt_new0(s_context_t, 1);
-       
-       context->line = NULL;
-       context->command_line = NULL;
-       context->exit_code = 0;
-       context->timeout = INDEFINITE;
-       context->input = xbt_strbuff_new();
-       context->output = xbt_strbuff_new();
-       context->signal = INDEFINITE_SIGNAL;
-       context->output_handling = oh_check;
-       context->async = 0;
-       
-       return context;
-}
-
-context_t
-context_dup(context_t context)
-{
-       
-       context_t dup = xbt_new0(s_context_t, 1);
-       
-       dup->line = context->line;
-       dup->command_line = context->command_line;
-       dup->exit_code = context->exit_code;
-       dup->timeout = context->timeout;
-       dup->output = NULL;
-       dup->input = NULL;
-       dup->signal = NULL;
-       
-       if(context->input->used)
-       {
-               dup->input = xbt_strbuff_new();
-               xbt_strbuff_append(dup->input,context->input->data);
-       }
-
-       if(context->output->used)
-       {
-               dup->output = xbt_strbuff_new();
-               xbt_strbuff_append(dup->output,context->output->data);
-       }
-
-       if(context->signal)
-               dup->signal = strdup(context->signal);
-
-       dup->output_handling = context->output_handling;
-       
-       dup->async = context->async;
-       
-       return dup;
-}
-
-void
-context_clear(context_t context)
-{
-       context->line = NULL;
-       context->command_line = NULL;
-       context->exit_code = 0;
-       context->timeout = INDEFINITE;
-       
-       if(context->input)
-               xbt_strbuff_empty(context->input);
-
-       if(context->output)
-               xbt_strbuff_empty(context->output);
-       
-       if(context->signal)
-       {
-               free(context->signal);
-               context->signal = INDEFINITE_SIGNAL;
-       }
-
-       context->output_handling = oh_check;
-       context->async = 0;
-
-}
-
-void
-context_reset(context_t context)
-{
-       context->line = NULL;
-       context->command_line = NULL;
-
-       if(context->input)
-               xbt_strbuff_empty(context->input);
-
-       if(context->output)
-               xbt_strbuff_empty(context->output);
-       
-       if(context->signal)
-       {
-               free(context->signal);
-               context->signal = NULL;
-       }
-       
-       /* default expected exit code */
-       context->exit_code = 0;
-
-       context->output_handling = oh_check;
-       context->async = 0;
-
-}
-
-void
-context_input_write(context_t context, const char* buffer)
-{
-       xbt_strbuff_append(context->input, buffer);
-}
-
-void
-context_ouput_read(context_t context, const char* buffer)
-{
-       xbt_strbuff_append(context->output, buffer);
-}
-
-void
-context_free(context_t* context)
-{
-       if(((*context)->input))
-               xbt_strbuff_free(((*context)->input));
-
-       if(((*context)->output))
-               xbt_strbuff_free(((*context)->output));
-       
-       if((*context)->signal)
-               free((*context)->signal);
-
-       *context = NULL;
-}
-
+/*\r
+ * src/context.c - type representing the tesh file stream.\r
+ *\r
+ * Copyright 2008,2009 Martin Quinson, Malek Cherier All right reserved. \r
+ *\r
+ * This program is free software; you can redistribute it and/or modify it \r
+ * under the terms of the license (GNU LGPL) which comes with this package.\r
+ *\r
+ * Purpose:\r
+ *             This file contains all the definitions of the functions related with\r
+ *             the tesh context type.\r
+ *\r
+ */\r
\r
+#include <context.h>\r
+\r
+\r
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);\r
+\r
+#define INDEFINITE_SIGNAL      NULL\r
+\r
+context_t\r
+context_new(void)\r
+{\r
+       context_t context = xbt_new0(s_context_t,1);\r
+       \r
+       context->line = NULL;\r
+       context->pos = NULL;\r
+       context->command_line = NULL;\r
+       context->exit_code = 0;\r
+       context->timeout = INDEFINITE;\r
+       context->input = xbt_strbuff_new();\r
+       context->output = xbt_strbuff_new();\r
+       context->signal = INDEFINITE_SIGNAL;\r
+       context->output_handling = oh_check;\r
+       context->async = 0;\r
+       \r
+       return context;\r
+}\r
+\r
+int\r
+context_free(context_t* ptr)\r
+{\r
+       if(((*ptr)->input))\r
+               xbt_strbuff_free(((*ptr)->input));\r
+\r
+       if(((*ptr)->output))\r
+               xbt_strbuff_free(((*ptr)->output));\r
+       \r
+       if((*ptr)->command_line)\r
+               free((*ptr)->command_line);\r
+\r
+       if((*ptr)->pos)\r
+               free((*ptr)->pos);\r
+\r
+       if((*ptr)->signal)\r
+               free((*ptr)->signal);\r
+\r
+       *ptr = NULL;\r
+       \r
+       return 0;\r
+}\r
+\r
+int\r
+context_reset(context_t context)\r
+{\r
+       context->line = NULL;\r
+       context->pos = NULL;\r
+\r
+       if(context->command_line)\r
+       {\r
+               free(context->command_line);\r
+               context->command_line = NULL;\r
+       }\r
+\r
+       if(context->pos)\r
+       {\r
+               free(context->pos);\r
+               context->pos = NULL;\r
+       }\r
+\r
+       if(context->input)\r
+               xbt_strbuff_empty(context->input);\r
+\r
+       if(context->output)\r
+               xbt_strbuff_empty(context->output);\r
+       \r
+       if(context->signal)\r
+       {\r
+               free(context->signal);\r
+               context->signal = NULL;\r
+       }\r
+       \r
+       /* default expected exit code */\r
+       context->exit_code = 0;\r
+\r
+       context->output_handling = oh_check;\r
+       context->async = 0;\r
+       \r
+       return 0;\r
+\r
+}\r
+\r
+context_t\r
+context_dup(context_t context)\r
+{\r
+       context_t dup;\r
+       \r
+       dup = xbt_new0(s_context_t, 1);\r
+       \r
+       dup->line = context->line;\r
+       dup->pos = strdup(context->pos);\r
+       dup->command_line = strdup(context->command_line);\r
+       dup->exit_code = context->exit_code;\r
+       dup->timeout = context->timeout;\r
+       dup->output = NULL;\r
+       dup->input = NULL;\r
+       dup->signal = NULL;\r
+       \r
+       if(context->input->used)\r
+       {\r
+               dup->input = xbt_strbuff_new();\r
+               xbt_strbuff_append(dup->input,context->input->data);\r
+       }\r
+       \r
+       dup->output = xbt_strbuff_new();\r
+\r
+       if(context->output->used)\r
+       {\r
+               xbt_strbuff_append(dup->output,context->output->data);\r
+       }\r
+\r
+       if(context->signal)\r
+       {\r
+               if(!(dup->signal = strdup(context->signal)))\r
+               {\r
+                       free(dup);\r
+                       return NULL;\r
+               }\r
+       }\r
+\r
+       dup->output_handling = context->output_handling;\r
+       \r
+       dup->async = context->async;\r
+       \r
+       return dup;\r
+}\r
+\r
+void\r
+context_clear(context_t context)\r
+{\r
+       context->line = NULL;\r
+       context->pos = NULL;\r
+       \r
+       if(context->command_line)\r
+       {\r
+               free(context->command_line);\r
+               context->command_line = NULL;\r
+       }\r
+\r
+       if(context->pos)\r
+       {\r
+               free(context->pos);\r
+               context->pos = NULL;\r
+       }\r
+\r
+       context->exit_code = 0;\r
+       context->timeout = INDEFINITE;\r
+       \r
+       if(context->input)\r
+               xbt_strbuff_empty(context->input);\r
+\r
+       if(context->output)\r
+               xbt_strbuff_empty(context->output);\r
+       \r
+       if(context->signal)\r
+       {\r
+               free(context->signal);\r
+               context->signal = INDEFINITE_SIGNAL;\r
+       }\r
+\r
+       context->output_handling = oh_check;\r
+       context->async = 0;\r
+\r
+}\r
+\r
+void\r
+context_input_write(context_t context, const char* buffer)\r
+{\r
+       xbt_strbuff_append(context->input, buffer);\r
+}\r
+\r
+void\r
+context_ouput_read(context_t context, const char* buffer)\r
+{\r
+       xbt_strbuff_append(context->output, buffer);\r
+}\r
+\r
+\r
+\r