2 * src/fstream.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 file stream type.
\r
15 #include <fstream.h>
\r
17 #include <context.h>
\r
18 #include <command.h>
\r
20 #include <str_replace.h>
\r
21 #include <variable.h>
\r
23 #include <readline.h>
\r
28 #include <xsignal.h>
\r
33 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
\r
38 long fstream_getline(fstream_t fstream, char **buf, size_t *n) {
\r
40 return readline(fstream->stream, buf, n);
\r
45 failure(unit_t unit)
\r
47 if(!keep_going_unit_flag)
\r
49 unit_t root = unit->root ? unit->root : unit;
\r
51 if(!root->interrupted)
\r
53 /* the unit interrupted (exit for the loop) */
\r
54 root->interrupted = 1;
\r
56 /* release the unit */
\r
57 xbt_os_sem_release(root->sem);
\r
60 /* if the --keep-going option is not specified */
\r
61 if(!keep_going_flag)
\r
65 /* request an global interruption by the runner */
\r
68 /* release the runner */
\r
69 xbt_os_sem_release(units_sem);
\r
76 fstream_new(const char* directory, const char* name)
\r
86 if(!directory && !strcmp("stdin", name))
\r
88 fstream = xbt_new0(s_fstream_t, 1);
\r
89 fstream->name = strdup("stdin");
\r
98 fstream = xbt_new0(s_fstream_t, 1);
\r
100 if(!(fstream->name = strdup(name)))
\r
106 if(!(fstream->directory = strdup(directory)))
\r
108 free(fstream->name);
\r
113 fstream->stream = NULL;
\r
114 fstream->unit = NULL;
\r
115 fstream->parsed = 0;
\r
122 fstream_open(fstream_t fstream)
\r
124 char path[PATH_MAX + 1] = {0};
\r
126 /* check the parameter */
\r
133 if(!fstream || fstream->stream)
\r
139 if(!strcmp(fstream->name, "stdin"))
\r
141 fstream->stream = stdin;
\r
146 sprintf(path,"%s/%s",fstream->directory, fstream->name);
\r
148 sprintf(path,"%s\\%s",fstream->directory, fstream->name);
\r
151 if(!(fstream->stream = fopen(path, "r")))
\r
158 fstream_close(fstream_t fstream)
\r
160 /* check the parameter */
\r
161 if(!(fstream) || !strcmp(fstream->name, "stdin") )
\r
167 if(!fstream->stream)
\r
170 if(EOF == fclose(fstream->stream))
\r
173 fstream->stream = NULL;
\r
179 fstream_free(fstream_t* ptr)
\r
182 /* check the parameter */
\r
193 fclose((*ptr)->stream);
\r
196 free((*ptr)->name);
\r
198 if((*ptr)->directory)
\r
199 free((*ptr)->directory);
\r
210 fstream_parse(fstream_t fstream, xbt_os_mutex_t mutex)
\r
213 char * line = NULL;
\r
215 char file_pos[256];
\r
216 xbt_strbuff_t buff;
\r
217 int buffbegin = 0;
\r
221 /* Count the line length while checking wheather it's blank */
\r
224 /* Deal with \ at the end of the line, and call handle_line on result */
\r
225 int to_be_continued;
\r
227 /* check the parameter */
\r
228 if(!(fstream) || !mutex)
\r
234 buff = xbt_strbuff_new();
\r
236 if(!(context = context_new()))
\r
239 unit = fstream->unit;
\r
241 /*while(!(unit->root->interrupted) && getline(&line, &len, fstream->stream) != -1)*/
\r
242 while(!(unit->root->interrupted) && fstream_getline(fstream, &line, &len) != -1)
\r
247 to_be_continued = 0;
\r
251 while(line[linelen] != '\0')
\r
253 if (line[linelen] != ' ' && line[linelen] != '\t' && line[linelen]!='\n' && line[linelen]!='\r')
\r
261 if(!context->command_line && (context->input->used || context->output->used))
\r
263 snprintf(file_pos,256,"%s:%d",fstream->name, line_num);
\r
264 ERROR1("[%s] Error : no command found in the last chunk of lines", file_pos);
\r
266 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
271 else if(unit->is_running_suite)
\r
272 {/* it's the end of a suite */
\r
274 unit_t* current_suite = xbt_dynar_get_ptr(unit->suites, xbt_dynar_length(unit->suites) - 1);
\r
276 if(!xbt_dynar_length((*current_suite)->includes))
\r
278 ERROR2("[%s] Malformated suite `(%s)' : include missing", file_pos, (*current_suite)->description);
\r
280 unit_set_error(*current_suite, ESYNTAX, 1);
\r
286 unit->is_running_suite = 0;
\r
289 if(context->command_line)
\r
291 if(fstream_launch_command(fstream, context, mutex) < 0)
\r
298 if(linelen>1 && line[linelen-2]=='\\')
\r
300 if(linelen>2 && line[linelen-3] == '\\')
\r
302 /* Damn. Escaped \ */
\r
303 line[linelen-2] = '\n';
\r
304 line[linelen-1] = '\0';
\r
308 to_be_continued = 1;
\r
309 line[linelen-2] = '\0';
\r
313 buffbegin = line_num;
\r
317 if(buff->used || to_be_continued)
\r
319 xbt_strbuff_append(buff,line);
\r
321 if (!to_be_continued)
\r
323 snprintf(file_pos,256,"%s:%d",fstream->name, buffbegin);
\r
324 fstream_lex_line(fstream, context, mutex, file_pos, buff->data);
\r
325 xbt_strbuff_empty(buff);
\r
330 snprintf(file_pos,256,"%s:%d",fstream->name, line_num);
\r
331 fstream_lex_line(fstream, context, mutex, file_pos, line);
\r
335 /* Check that last command of the file ran well */
\r
336 if(context->command_line)
\r
338 if(fstream_launch_command(fstream, context, mutex) < 0)
\r
342 /* clear buffers */
\r
346 xbt_strbuff_free(buff);
\r
348 if(context_free(&context) < 0)
\r
351 return (exit_code || errno) ? -1 : 0;
\r
356 fstream_lex_line(fstream_t fstream, context_t context, xbt_os_mutex_t mutex, const char * filepos, char *line)
\r
359 variable_t variable;
\r
361 char name[VAR_NAME_MAX + 1] = {0};
\r
362 unit_t unit = fstream->unit;
\r
363 xbt_dynar_t variables = unit->runner->variables;
\r
365 if(unit->is_running_suite && strncmp(line, "! include", strlen("! include")))
\r
366 {/* it's the end of a suite */
\r
368 unit_t* current_suite = xbt_dynar_get_ptr(unit->suites, xbt_dynar_length(unit->suites) - 1);
\r
370 if(!xbt_dynar_length((*current_suite)->includes))
\r
371 ERROR2("[%s] Malformated suite `(%s)': include missing", filepos, (*current_suite)->description);
\r
373 ERROR2("[%s] Malformated suite `(%s)': blank line missing", filepos, (*current_suite)->description);
\r
375 unit_set_error(*current_suite, ESYNTAX, 1);
\r
377 failure(fstream->unit);
\r
382 context->line = strdup(filepos);
\r
386 xbt_str_rtrim(line + 2,"\n");
\r
388 line2 = strdup(line);
\r
390 /* replace each variable by its value */
\r
391 xbt_os_mutex_acquire(unit->mutex);
\r
393 xbt_dynar_foreach(variables, i, variable)
\r
395 sprintf(name, "$%s", variable->name);
\r
396 str_replace_all(&line2, name, variable->val);
\r
397 memset(name, 0, VAR_NAME_MAX + 1);
\r
400 xbt_os_mutex_release(unit->mutex);
\r
414 if(line2[0] == '$')
\r
415 ERROR1("[%s] Missing space after `$' `(usage : $ <command>)'", filepos);
\r
417 ERROR1("[%s] Missing space after & `(usage : & <command>)'", filepos);
\r
419 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
425 context->async = (line2[0] == '&');
\r
428 /* further trim useless chars which are significant for in/output */
\r
429 xbt_str_rtrim(line2 + 2," \t");
\r
431 /* deal with CD commands here, not in context */
\r
432 if(!strncmp("cd ",line2 + 2, 3))
\r
434 char* dir = strdup(line2 + 4);
\r
436 if(context->command_line)
\r
438 if(fstream_launch_command(fstream, context, mutex) < 0)
\r
442 /* search begining */
\r
443 while(*(dir++) == ' ');
\r
450 INFO2("[%s] cd %s", filepos, dir);
\r
452 if(!just_print_flag)
\r
456 ERROR3("[%s] Chdir to %s failed: %s",filepos, dir,error_to_string(errno, 0));
\r
457 unit_set_error(fstream->unit, errno, 0);
\r
468 fstream_process_token(fstream, context, mutex, filepos, line2[0], line2 + 2);
\r
476 if(line[0] == '!' && line[1] != ' ')
\r
478 ERROR1("[%s] Missing space after `!' `(usage : ! <command> [[=]value])'", filepos);
\r
480 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
486 fstream_process_token(fstream, context, mutex, filepos, line2[0], line2 + 2);
\r
495 char* prompt = line2 + 2;
\r
497 for(j = 0; j < strlen(prompt); j++)
\r
498 if (prompt[j] != ' ' && prompt[j] != '\t')
\r
503 ERROR1("[%s] Bad usage of the metacommand p `(usage : p <prompt>)'", filepos);
\r
505 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
512 INFO2("[%s] %s",filepos,prompt);
\r
524 char* prompt = line2 + 2;
\r
526 for(j = 0; j < strlen(prompt); j++)
\r
527 if (prompt[j] != ' ' && prompt[j] != '\t')
\r
532 ERROR1("[%s] Bad usage of the metacommand P `(usage : P <prompt>)'", filepos);
\r
534 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
541 CRITICAL2("[%s] %s",filepos, prompt);
\r
547 if(unit->description)
\r
548 WARN2("[%s] Description already specified `%s'",filepos, line2 + 2);
\r
554 char* desc = line2 + 2;
\r
556 for(j = 0; j < strlen(desc); j++)
\r
557 if (desc[j] != ' ' && desc[j] != '\t')
\r
562 ERROR1("[%s] Bad usage of the metacommand D `(usage : D <Description>)'", filepos);
\r
564 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
570 unit->description = strdup(desc);
\r
575 ERROR2("[%s] Syntax error `%s'", filepos, line2);
\r
576 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
585 fstream_process_token(fstream_t fstream, context_t context, xbt_os_mutex_t mutex, const char* filepos, char token, char *line)
\r
587 unit_t unit = fstream->unit;
\r
594 if(context->command_line)
\r
597 if(context->output->used || context->input->used)
\r
599 ERROR2("[%s] More than one command in this chunk of lines (previous: %s).\nDunno which input/output belongs to which command.",filepos, context->command_line);
\r
601 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
606 if(fstream_launch_command(fstream, context, mutex) < 0)
\r
609 VERB1("[%s] More than one command in this chunk of lines",filepos);
\r
616 for(j = 0; j < strlen(line); j++)
\r
617 if (line[j] != ' ' && line[j] != '\t')
\r
623 ERROR1("[%s] Undefinite command for `$' `(usage: $ <command>)'", filepos);
\r
625 ERROR1("[%s] Undefinite command for `&' `(usage: & <command>)'", filepos);
\r
627 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
634 context->command_line = strdup(line);
\r
635 context->line = /*strdup(filepos)*/ filepos;
\r
636 context->pos = strdup(filepos);
\r
640 xbt_strbuff_append(context->input,line);
\r
641 xbt_strbuff_append(context->input,"\n");
\r
645 xbt_strbuff_append(context->output,line);
\r
646 xbt_strbuff_append(context->output,"\n");
\r
651 if(context->command_line)
\r
653 if(fstream_launch_command(fstream, context, mutex) < 0)
\r
657 if(!strncmp(line,"timeout no",strlen("timeout no")))
\r
659 VERB1("[%s] (disable timeout)", filepos);
\r
660 context->timeout = INDEFINITE;
\r
662 else if(!strncmp(line,"timeout ",strlen("timeout ")))
\r
667 char* p = line + strlen("timeout ");
\r
670 for(j = 0; j < strlen(p); j++)
\r
671 if (p[j] != ' ' && p[j] != '\t')
\r
676 ERROR1("[%s] Undefinite timeout value `(usage :timeout <seconds>)'", filepos);
\r
678 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
684 while(p[i] != '\0')
\r
688 ERROR2("[%s] Invalid timeout value `(%s)' : `(usage :timeout <seconds>)'", filepos, line + strlen("timeout "));
\r
690 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
699 context->timeout = atoi(line + strlen("timeout"));
\r
700 VERB2("[%s] (new timeout value: %d)",filepos,context->timeout);
\r
703 else if (!strncmp(line,"expect signal ",strlen("expect signal ")))
\r
709 char* p = line + strlen("expect signal ");
\r
712 for(j = 0; j < strlen(p); j++)
\r
713 if (p[j] != ' ' && p[j] != '\t')
\r
718 ERROR1("[%s] Undefinite signal name `(usage :expect signal <signal name>)'", filepos);
\r
720 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
726 context->signal = strdup(line + strlen("expect signal "));
\r
728 xbt_str_trim(context->signal," \n");
\r
731 if(!strstr("SIGSEGVSIGTRAPSIGBUSSIGFPESIGILL", context->signal))
\r
733 ERROR2("[%s] Signal `%s' not supported by this platform", filepos, context->signal);
\r
735 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
742 if(!sig_exists(context->signal))
\r
744 ERROR2("[%s] Signal `%s' not supported by Tesh", filepos, context->signal);
\r
746 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
756 VERB2("[%s] (next command must raise signal %s)", filepos, context->signal);
\r
759 else if (!strncmp(line,"expect return ",strlen("expect return ")))
\r
765 char* p = line + strlen("expect return ");
\r
768 for(j = 0; j < strlen(p); j++)
\r
769 if (p[j] != ' ' && p[j] != '\t')
\r
774 ERROR1("[%s] Undefinite return value `(usage :expect return <return value>)'", filepos);
\r
776 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
782 while(p[i] != '\0')
\r
786 ERROR2("[%s] Invalid exit code value `(%s)' : must be an integer >= 0 and <=255", filepos, line + strlen("expect return "));
\r
788 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
797 context->exit_code = atoi(line+strlen("expect return "));
\r
798 VERB2("[%s] (next command must return code %d)",filepos, context->exit_code);
\r
801 else if (!strncmp(line,"output ignore",strlen("output ignore")))
\r
803 context->output_handling = oh_ignore;
\r
804 VERB1("[%s] (ignore output of next command)", filepos);
\r
807 else if (!strncmp(line,"output display",strlen("output display")))
\r
809 context->output_handling = oh_display;
\r
810 VERB1("[%s] (ignore output of next command)", filepos);
\r
813 else if(!strncmp(line,"include ", strlen("include ")))
\r
818 p1 = line + strlen("include");
\r
820 while(*p1 == ' ' || *p1 == '\t')
\r
826 ERROR1("[%s] no file specified : `(usage : include <file> [<description>])'", filepos);
\r
828 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
835 char file_name[PATH_MAX + 1] = {0};
\r
839 while(*p2 != '\0' && *p2 != ' ' && *p2 != '\t')
\r
842 strncpy(file_name, p1, p2 - p1);
\r
846 while(*p2 == ' ' || *p2 == '\t')
\r
849 fstream_handle_include(fstream, context, mutex, file_name, p2[0] != '\0' ? p2 : NULL);
\r
853 else if(!strncmp(line,"suite ", strlen("suite ")))
\r
857 char* p = line + strlen("suite ");
\r
860 for(j = 0; j < strlen(p); j++)
\r
861 if (p[j] != ' ' && p[j] != '\t')
\r
866 ERROR1("[%s] Undefinite suit description : `(usage : suite <description>)", filepos);
\r
868 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
874 if(unit->is_running_suite)
\r
876 ERROR1("[%s] Suite already in progress", filepos);
\r
878 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
884 fstream_handle_suite(fstream, line + strlen("suite "), filepos);
\r
886 else if(!strncmp(line,"unsetenv ", strlen("unsetenv ")))
\r
892 variable_t variable;
\r
895 char* name = line + strlen("unsetenv ");
\r
899 for(j = 0; j < strlen(name); j++)
\r
900 if (name[j] != ' ' && name[j] != '\t')
\r
905 ERROR1("[%s] Bad usage of the metacommand unsetenv : `(usage : unsetenv variable)'", filepos);
\r
907 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
913 xbt_os_mutex_acquire(unit->mutex);
\r
916 xbt_dynar_foreach(unit->runner->variables, i, variable)
\r
918 if(!strcmp(variable->name, name))
\r
920 env = variable->env;
\r
921 err = variable->err;
\r
934 SetEnvironmentVariable(name, NULL);
\r
936 xbt_dynar_cursor_rm(unit->runner->variables, &i);
\r
940 ERROR2("[%s] `(%s)' environment variable not found : impossible to unset it",filepos, name);
\r
941 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
942 xbt_os_mutex_release(unit->mutex);
\r
953 ERROR2("[%s] `(%s)' is not an environment variable : use `unset' instead `unsetenv'",filepos, name);
\r
954 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
956 xbt_os_mutex_release(unit->mutex);
\r
961 ERROR2("[%s] `(%s)' is not an environment variable (it's a system variable) : impossible to unset it",filepos, name);
\r
962 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
963 xbt_os_mutex_release(unit->mutex);
\r
970 ERROR2("[%s] `(%s)' environment variable not found : impossible to unset it",filepos, name);
\r
971 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
972 xbt_os_mutex_release(unit->mutex);
\r
978 xbt_os_mutex_release(unit->mutex);
\r
982 else if(!strncmp(line,"setenv ", strlen("setenv ")))
\r
985 char name[PATH_MAX + 1] = {0};
\r
991 p = line + strlen("setenv ");
\r
993 val = strchr(p, '=');
\r
997 variable_t variable;
\r
1003 /* syntax error */
\r
1004 if(val[0] == '\0' || val[0] ==' ' || val[0] =='\t')
\r
1006 ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);
\r
1008 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1016 strncpy(name, p, (val - p -1));
\r
1020 for(j = 0; j < strlen(name); j++)
\r
1021 if (name[j] != ' ' && name[j] != '\t')
\r
1027 ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);
\r
1029 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1035 /* test if the variable is already registred */
\r
1036 xbt_os_mutex_acquire(unit->mutex);
\r
1038 xbt_dynar_foreach(unit->runner->variables, i, variable)
\r
1040 if(!strcmp(variable->name, name))
\r
1042 env = variable->env;
\r
1043 err = variable->err;
\r
1049 /* if the variable is already registred, update its value;
\r
1050 * otherwise register it.
\r
1056 if(!strcmp(val, variable->val))
\r
1057 WARN3("[%s] This environment variable `(%s)' is already set with the value `(%s)'", filepos, name, val);
\r
1059 free(variable->val);
\r
1060 variable->val = strdup(val);
\r
1063 SetEnvironmentVariable(variable->name, variable->val);
\r
1065 setenv(variable->name, variable->val, 1);
\r
1071 ERROR2("[%s] Conflict : a system variable `(%s)' already exists", filepos, name);
\r
1073 ERROR2("[%s] Conflict : (none environment) variable `(%s)' already exists", filepos, name);
\r
1075 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1076 xbt_os_mutex_release(unit->mutex);
\r
1085 ERROR2("[%s] A system variable named `(%s)' already exists", filepos, name);
\r
1087 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1088 xbt_os_mutex_release(unit->mutex);
\r
1094 variable = variable_new(name, val);
\r
1095 variable->env = 1;
\r
1097 xbt_dynar_push(unit->runner->variables, &variable);
\r
1100 SetEnvironmentVariable(variable->name, variable->val);
\r
1102 setenv(variable->name, variable->val, 0);
\r
1107 xbt_os_mutex_release(unit->mutex);
\r
1112 ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);
\r
1114 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1119 else if(!strncmp(line,"unset ", strlen("unset ")))
\r
1121 unsigned int i, j;
\r
1125 variable_t variable;
\r
1128 char* name = line + strlen("unset ");
\r
1132 for(j = 0; j < strlen(name); j++)
\r
1133 if (name[j] != ' ' && name[j] != '\t')
\r
1139 ERROR1("[%s] Bad usage of the metacommand unset `(usage : unset variable)'", filepos);
\r
1141 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1148 xbt_os_mutex_acquire(unit->mutex);
\r
1150 xbt_dynar_foreach(unit->runner->variables, i, variable)
\r
1152 if(!strcmp(variable->name, name))
\r
1154 env = variable->env;
\r
1155 err = variable->err;
\r
1164 /*xbt_dynar_remove_at(unit->runner->variables, i, NULL);*/
\r
1165 xbt_dynar_cursor_rm(unit->runner->variables, &i);
\r
1168 ERROR2("[%s] `(%s)' variable not found",filepos, name);
\r
1169 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1170 xbt_os_mutex_release(unit->mutex);
\r
1177 ERROR2("[%s] `(%s)' is an environment variable use `unsetenv' instead `unset'",filepos, name);
\r
1178 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1179 xbt_os_mutex_release(unit->mutex);
\r
1185 ERROR2("[%s] `(%s)' is system variable : you can unset it",filepos, name);
\r
1186 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1187 xbt_os_mutex_release(unit->mutex);
\r
1192 xbt_os_mutex_release(unit->mutex);
\r
1195 else if(!strncmp(line,"set ", strlen("set ")))
\r
1198 char name[PATH_MAX + 1] = {0};
\r
1202 val = strchr(line + strlen("set "), '=');
\r
1206 variable_t variable;
\r
1215 /* syntax error */
\r
1216 if(val[0] == '\0')
\r
1218 ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);
\r
1220 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1225 else if(val[0] ==' ' || val[0] =='\t')
\r
1227 strncpy(name, line + strlen("set "), (val - (line + strlen("set "))));
\r
1229 ERROR2("[%s] No space avaible after`(%s)'", filepos, name);
\r
1231 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1238 /* assume it's a varibale */
\r
1240 strncpy(name, line + strlen("set "), (val - (line + strlen("set ")) -1));
\r
1244 for(j = 0; j < strlen(name); j++)
\r
1245 if (name[j] != ' ' && name[j] != '\t')
\r
1251 ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);
\r
1253 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1259 xbt_os_mutex_acquire(unit->mutex);
\r
1261 /* test if the variable is already registred */
\r
1262 xbt_dynar_foreach(unit->runner->variables, i, variable)
\r
1264 if(!strcmp(variable->name, name))
\r
1267 err = variable->err;
\r
1268 env = variable->env;
\r
1273 /* if the variable is already registred, update its value (if same value warns);
\r
1274 * otherwise register it.
\r
1280 ERROR2("[%s] A system variable named `(%s)' already exists", filepos, name);
\r
1282 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1283 xbt_os_mutex_release(unit->mutex);
\r
1290 ERROR2("[%s] `(%s)' is an environment variable use `setenv' instead `set'", filepos, name);
\r
1292 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1293 xbt_os_mutex_release(unit->mutex);
\r
1300 if(!strcmp(val, variable->val))
\r
1301 WARN3("[%s] Variable `(%s)' already contains value `<%s>'",filepos, variable->name, val);
\r
1303 free(variable->val);
\r
1304 variable->val = strdup(val);
\r
1309 variable_t new_var = variable_new(name, val);
\r
1310 xbt_dynar_push(unit->runner->variables, &new_var);
\r
1314 xbt_os_mutex_release(unit->mutex);
\r
1318 ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);
\r
1320 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1326 {/* assume it's a variable */
\r
1328 char name[PATH_MAX + 1] = {0};
\r
1329 unsigned int i, j;
\r
1332 val = strchr(line, '=');
\r
1336 variable_t variable;
\r
1343 /* syntax error */
\r
1344 if(val[0] == '\0')
\r
1346 strncpy(name, line, (val - line -1));
\r
1350 for(j = 0; j < strlen(name); j++)
\r
1351 if (name[j] != ' ' && name[j] != '\t')
\r
1355 ERROR1("[%s] Bad usage of Tesh variable mechanism `(usage : variable=value)'", filepos);
\r
1356 else if(!strcmp("setenv", name))
\r
1357 ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);
\r
1358 else if(!strcmp("set", name))
\r
1359 ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);
\r
1361 ERROR2("[%s] Undefined variable `(%s)'", filepos, name);
\r
1363 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1368 else if(val[0] ==' ' || val[0] =='\t')
\r
1370 strncpy(name, line, (val - line));
\r
1372 ERROR2("[%s] No space avaible after`(%s)'", filepos, name);
\r
1374 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1380 /* assume it's a varibale */
\r
1382 strncpy(name, line, (val - line -1));
\r
1386 for(j = 0; j < strlen(name); j++)
\r
1387 if (name[j] != ' ' && name[j] != '\t')
\r
1393 ERROR1("[%s] Bad usage of Tesh variable capability `(usage : variable=value)'", filepos);
\r
1395 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1401 if(!strcmp("set", name))
\r
1403 ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);
\r
1405 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1409 else if(!strcmp("setenv", name))
\r
1411 ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);
\r
1413 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1418 xbt_os_mutex_acquire(unit->mutex);
\r
1420 /* test if the variable is already registred */
\r
1421 xbt_dynar_foreach(unit->runner->variables, i, variable)
\r
1423 if(!strcmp(variable->name, name))
\r
1426 err = variable->err;
\r
1427 env = variable->env;
\r
1432 /* if the variable is already registred, update its value (if same value warns);
\r
1433 * otherwise register it.
\r
1439 ERROR2("[%s] A system variable named `(%s)' already exists", filepos, name);
\r
1441 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1442 xbt_os_mutex_release(unit->mutex);
\r
1448 ERROR2("[%s] `(%s)' is an environment variable use `setenv' metacommand", filepos, name);
\r
1450 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1451 xbt_os_mutex_release(unit->mutex);
\r
1458 if(!strcmp(val, variable->val))
\r
1459 WARN3("[%s] Variable `(%s)' already contains value `<%s>'",filepos, variable->name, val);
\r
1461 free(variable->val);
\r
1462 variable->val = strdup(val);
\r
1467 variable_t new_var = variable_new(name, val);
\r
1468 xbt_dynar_push(unit->runner->variables, &new_var);
\r
1472 xbt_os_mutex_release(unit->mutex);
\r
1477 if(!strncmp("setenv", line, strlen("setenv")))
\r
1478 ERROR1("[%s] Bad usage of the metacommand setenv : `(usage : setenv variable=value)'", filepos);
\r
1479 else if(!strncmp("set", line, strlen("set")))
\r
1480 ERROR1("[%s] Bad usage of the metacommand set : `(usage : set variable=value)'", filepos);
\r
1481 else if(!strncmp("unsetenv", line, strlen("unsetenv")))
\r
1482 ERROR1("[%s] Bad usage of the metacommand unsetenv : `(usage : unsetenv variable)'", filepos);
\r
1483 else if(!strncmp("unset", line, strlen("unset")))
\r
1484 ERROR1("[%s] Bad usage of the metacommand unset : `(usage : unset variable)'", filepos);
\r
1485 else if(!strncmp("timeout", line, strlen("timeout")))
\r
1486 ERROR1("[%s] Bad usage of the metacommand timeout : `(usage : timeout <integral positive integer>)'", filepos);
\r
1487 else if(!strncmp("expect signal", line, strlen("expect signal")))
\r
1488 ERROR1("[%s] Bad usage of the metacommand expect signal : `(usage : expect signal <sig_name>)'", filepos);
\r
1489 else if(!strncmp("expect return", line, strlen("expect return")))
\r
1490 ERROR1("[%s] Bad usage of the metacommand expect return : `(usage : expect return <return value (>=0 <=255)>)'", filepos);
\r
1491 else if(!strncmp("include", line, strlen("include")))
\r
1492 ERROR1("[%s] Bad usage of the metacommand include :`(usage : include <file> [<description>])'", filepos);
\r
1493 else if(!strncmp("suite", line, strlen("suite")))
\r
1494 ERROR1("[%s] Bad usage of the metacommand suite : `(usage : suite <description>)'", filepos);
\r
1496 ERROR2("[%s] Unknown metacommand: `%s'",filepos,line);
\r
1498 unit_set_error(fstream->unit, ESYNTAX, 1);
\r
1510 fstream_handle_include(fstream_t fstream, context_t context, xbt_os_mutex_t mutex, const char* file_name, const char* description)
\r
1513 char* prev_directory = NULL;
\r
1514 fstream_t _fstream = NULL;
\r
1515 struct stat buffer = {0};
\r
1516 unit_t unit = fstream->unit;
\r
1518 if(!stat(file_name, &buffer) && S_ISREG(buffer.st_mode))
\r
1520 /* the file is in the current directory */
\r
1521 _fstream = fstream_new(getcwd(NULL, 0), file_name);
\r
1522 fstream_open(_fstream);
\r
1524 /* the file to include is not in the current directory, check if it is in a include directory */
\r
1528 prev_directory = getcwd(NULL, 0);
\r
1530 xbt_dynar_foreach(include_dirs, i, dir)
\r
1534 if(!stat(file_name, &buffer) && S_ISREG(buffer.st_mode))
\r
1536 _fstream = fstream_new(dir->name, file_name);
\r
1537 fstream_open(_fstream);
\r
1542 chdir(prev_directory);
\r
1543 free(prev_directory);
\r
1546 /* the file to include is not found handle the failure */
\r
1549 if(file_name[0] == '$')
\r
1551 ERROR3("[%s] Include file `(%s)' not found or variable `(%s)' doesn't exist",context->line, file_name, file_name + 1);
\r
1557 /* may be a variable */
\r
1558 variable_t variable;
\r
1562 xbt_dynar_foreach(unit->runner->variables, i, variable)
\r
1564 if(!strcmp(variable->name, file_name))
\r
1572 ERROR3("[%s] Include file `(%s)' not found (if you want to use the variable <%s> add the prefix `$')",context->line, file_name, file_name);
\r
1574 ERROR2("[%s] Include file `(%s)' not found",context->line, file_name);
\r
1577 unit_set_error(fstream->unit, EINCLUDENOTFOUND, 1);
\r
1583 if(!unit->is_running_suite)
\r
1584 {/* it's the unit of a suite */
\r
1585 unit_t include = unit_new(unit->runner, unit->root, unit, _fstream);
\r
1587 include->mutex = unit->root->mutex;
\r
1590 include->description = strdup(description);
\r
1592 xbt_dynar_push(unit->includes, &include);
\r
1595 INFO1("Include from %s", _fstream->name);
\r
1597 INFO1("Checking include %s...",_fstream->name);
\r
1599 fstream_parse(_fstream, mutex);
\r
1602 {/* it's a include */
\r
1607 owner = xbt_dynar_get_ptr(unit->suites, xbt_dynar_length(unit->suites) - 1);
\r
1609 include = unit_new(unit->runner, unit->root, *owner, _fstream);
\r
1611 include->mutex = unit->root->mutex;
\r
1614 include->description = strdup(description);
\r
1616 xbt_dynar_push((*owner)->includes, &include);
\r
1619 INFO1("Include from %s", _fstream->name);
\r
1621 INFO1("Checking include %s...",_fstream->name);
\r
1623 fstream_parse(_fstream, mutex);
\r
1629 fstream_handle_suite(fstream_t fstream, const char* description, const char* filepos)
\r
1631 unit_t unit = fstream->unit;
\r
1632 unit_t suite = unit_new(unit->runner, unit->root, unit, NULL);
\r
1635 suite->description = strdup(description);
\r
1637 suite->filepos = strdup(filepos);
\r
1639 xbt_dynar_push(unit->suites, &suite);
\r
1640 unit->is_running_suite = 1;
\r
1643 INFO1("Test suite %s", description);
\r
1645 INFO1("Checking suite %s...",description);
\r
1650 fstream_launch_command(fstream_t fstream, context_t context, xbt_os_mutex_t mutex)
\r
1652 unit_t unit = fstream->unit;
\r
1656 command_t command;
\r
1658 if(!(command = command_new(unit, context, mutex)))
\r
1660 if(EINVAL == errno)
\r
1662 ERROR3("[%s] Cannot instantiate the command `%s' (%d)",context->pos, strerror(errno), errno);
\r
1664 unit_set_error(unit, errno, 0);
\r
1668 else if(ENOMEM == errno)
\r
1670 ERROR3("[%s] Cannot instantiate the command `%s' (%d)",context->pos, strerror(errno), errno);
\r
1672 unit_set_error(unit, errno, 0);
\r
1679 if(command_run(command) < 0)
\r
1681 ERROR3("[%s] Cannot run the command `%s' (%d)",context->pos, strerror(errno), errno);
\r
1682 unit_set_error(unit, errno, 0);
\r
1688 if(context_reset(context) < 0)
\r
1690 ERROR3("[%s] Cannot reset the context of the command `%s' (%d)",context->pos, strerror(errno), errno);
\r
1692 unit_set_error(fstream->unit, errno, 0);
\r