Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0a8ef5e60ad35b40042a9a27bb2b84c8205129fb
[simgrid.git] / tools / tesh2 / src / fstream.c
1 /*\r
2  * src/fstream.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 file stream type.\r
12  *\r
13  */\r
14 \r
15 #include <fstream.h>\r
16 #include <xerrno.h>\r
17 #include <context.h>\r
18 #include <command.h>\r
19 #include <unit.h>\r
20 #include <str_replace.h>\r
21 #include <variable.h>\r
22 \r
23 #include <readline.h>\r
24 \r
25 #include <is_cmd.h>\r
26 #include <getpath.h>\r
27 \r
28 #ifndef WIN32\r
29 #include <xsignal.h>\r
30 #endif\r
31 \r
32 #ifdef WIN32\r
33 static int\r
34 is_w32_cmd(char* cmd, char** path)\r
35 {\r
36         size_t i = 0;\r
37         struct stat stat_buff = {0};\r
38         char buff[PATH_MAX + 1] = {0};\r
39         \r
40 \r
41 \r
42         if(!cmd)\r
43         {\r
44                 errno = EINVAL;\r
45                 return 0;\r
46         }\r
47         \r
48         if(stat(cmd, &stat_buff) || !S_ISREG(stat_buff.st_mode))\r
49         {\r
50                 if(path)\r
51                 {\r
52                         for (i = 0; path[i] != NULL; i++)\r
53                         {\r
54                                 /* use Cat.exe on Windows */\r
55                                 if(!strcmp(cmd, "cat"))\r
56                                         cmd[0] = 'C';\r
57                                 \r
58                                 sprintf(buff,"%s\\%s",path[i], cmd);\r
59                                 \r
60                                 if(!stat(buff, &stat_buff) && S_ISREG(stat_buff.st_mode))\r
61                                         return 1;\r
62                         }\r
63                 }\r
64         }\r
65         else\r
66                 return 1;\r
67                 \r
68 \r
69         return 0;\r
70 }\r
71 #endif\r
72 \r
73 \r
74 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);\r
75 \r
76 \r
77 long fstream_getline(fstream_t fstream, char **buf, size_t *n) {\r
78 \r
79         return readline(fstream->stream, buf, n);\r
80         \r
81 }\r
82 \r
83 static void\r
84 failure(unit_t unit)\r
85 {\r
86         if(!keep_going_unit_flag)\r
87         {\r
88                 unit_t root = unit->root ? unit->root : unit;\r
89                         \r
90                 if(!root->interrupted)\r
91                 {\r
92                         /* the unit interrupted (exit for the loop) */\r
93                         root->interrupted = 1;\r
94 \r
95                         /* release the unit */\r
96                         xbt_os_sem_release(root->sem);\r
97                 }\r
98 \r
99                 /* if the --keep-going option is not specified */\r
100                 if(!keep_going_flag)\r
101                 {\r
102                         if(!interrupted)\r
103                         {\r
104                                 /* request an global interruption by the runner */\r
105                                 interrupted = 1;\r
106 \r
107                                 /* release the runner */\r
108                                 xbt_os_sem_release(units_sem);\r
109                         }\r
110                 }\r
111         }\r
112 }\r
113 \r
114 fstream_t\r
115 fstream_new(const char* directory, const char* name)\r
116 {\r
117         fstream_t fstream;\r
118         \r
119         if(!name)\r
120         {\r
121                 errno = EINVAL;\r
122                 return NULL;\r
123         }\r
124         \r
125         if(!directory && !strcmp("stdin", name))\r
126         {\r
127                 fstream = xbt_new0(s_fstream_t, 1);\r
128                 fstream->name = strdup("stdin");\r
129                 return fstream;\r
130         }\r
131         else if(!directory)\r
132         {\r
133                 errno = EINVAL;\r
134                 return NULL;\r
135         }\r
136         \r
137         fstream = xbt_new0(s_fstream_t, 1);\r
138         \r
139         if(!(fstream->name = strdup(name)))\r
140         {\r
141                 free(fstream);\r
142                 return NULL;\r
143         }\r
144         \r
145         if(!(fstream->directory = strdup(directory)))\r
146         {\r
147                 free(fstream->name);\r
148                 free(fstream);\r
149                 return NULL;\r
150         }\r
151         \r
152         fstream->stream = NULL;\r
153         fstream->unit = NULL;\r
154         fstream->parsed = 0;\r
155         \r
156         \r
157         return fstream;\r
158 }\r
159 \r
160 int\r
161 fstream_open(fstream_t fstream)\r
162 {\r
163         char path[PATH_MAX + 1] = {0};\r
164         \r
165         /* check the parameter */\r
166         if(!(fstream))\r
167     {\r
168         errno = EINVAL;\r
169         return -1;\r
170     }\r
171         \r
172         if(!fstream || fstream->stream)\r
173         {\r
174                 errno = EALREADY;\r
175                 return -1;\r
176         }\r
177                 \r
178         if(!strcmp(fstream->name, "stdin"))\r
179         {\r
180                 fstream->stream = stdin;\r
181                 return 0;\r
182         }\r
183         \r
184         #ifndef WIN32\r
185         sprintf(path,"%s/%s",fstream->directory, fstream->name);\r
186         #else\r
187         sprintf(path,"%s\\%s",fstream->directory, fstream->name);\r
188     #endif\r
189 \r
190         if(!(fstream->stream = fopen(path, "r")))\r
191         {\r
192                 return -1;\r
193         }\r
194         \r
195         return 0;\r
196 }\r
197 \r
198 int\r
199 fstream_close(fstream_t fstream)\r
200 {\r
201         /* check the parameter */\r
202         if(!(fstream) || !strcmp(fstream->name, "stdin") )\r
203     {\r
204         errno = EINVAL;\r
205         return -1;\r
206     }\r
207                 \r
208         if(!fstream->stream)\r
209                 return EBADF;   \r
210         \r
211         if(EOF == fclose(fstream->stream))\r
212                 return -1;\r
213                 \r
214         fstream->stream = NULL;\r
215         \r
216         return 0;\r
217 }\r
218 \r
219 int\r
220 fstream_free(fstream_t* ptr)\r
221 {\r
222         \r
223         /* check the parameter */\r
224         if(!(*ptr))\r
225     {\r
226         errno = EINVAL;\r
227         return -1;\r
228     }\r
229     \r
230         if(!(*ptr))\r
231                 return EINVAL;\r
232                 \r
233         if((*ptr)->stream)\r
234                 fclose((*ptr)->stream);\r
235         \r
236         if((*ptr)->name)\r
237                 free((*ptr)->name);\r
238         \r
239         if((*ptr)->directory)\r
240                 free((*ptr)->directory);\r
241                 \r
242         free(*ptr);\r
243 \r
244         *ptr = NULL;\r
245         \r
246         return 0;\r
247                 \r
248 }\r
249 \r
250 int\r
251 fstream_parse(fstream_t fstream, xbt_os_mutex_t mutex)\r
252 {\r
253         size_t len;\r
254         char * line = NULL;\r
255         int line_num = 0;\r
256         char file_pos[256];\r
257         xbt_strbuff_t buff;\r
258         int buffbegin = 0; \r
259         context_t context;\r
260         unit_t unit;\r
261         \r
262         /* Count the line length while checking wheather it's blank */\r
263         int blankline;\r
264         int linelen;    \r
265         /* Deal with \ at the end of the line, and call handle_line on result */\r
266         int to_be_continued;\r
267         \r
268         /* check the parameter */\r
269         if(!(fstream) || !mutex)\r
270     {\r
271         errno = EINVAL;\r
272         return -1;\r
273     }\r
274     \r
275         buff = xbt_strbuff_new();\r
276         \r
277         if(!(context = context_new()))\r
278                 return -1;\r
279                 \r
280         unit = fstream->unit;\r
281         \r
282         /*while(!(unit->root->interrupted)  && getline(&line, &len, fstream->stream) != -1)*/\r
283         while(!(unit->root->interrupted)  && fstream_getline(fstream, &line, &len) != -1)\r
284         {\r
285                 \r
286                 blankline=1;\r
287                 linelen = 0;    \r
288                 to_be_continued = 0;\r
289 \r
290                 line_num++;\r
291                 \r
292                 while(line[linelen] != '\0') \r
293                 {\r
294                         if (line[linelen] != ' ' && line[linelen] != '\t' && line[linelen]!='\n' && line[linelen]!='\r')\r
295                                 blankline = 0;\r
296                         \r
297                         linelen++;\r
298                 }\r
299         \r
300                 if(blankline) \r
301                 {\r
302                         if(!context->command_line && (context->input->used || context->output->used))\r
303                         {\r
304                                 snprintf(file_pos,256,"%s:%d",fstream->name, line_num);\r
305                                 ERROR1("[%s] Error : no command found in the last chunk of lines", file_pos);\r
306                                 \r
307                                 unit_set_error(fstream->unit, ESYNTAX, 1, file_pos);\r
308 \r
309                                 failure(unit);\r
310                                 break;\r
311                         }\r
312                         else if(unit->is_running_suite)\r
313                         {/* it's the end of a suite */\r
314                                 \r
315                                 unit_t* current_suite = xbt_dynar_get_ptr(unit->suites, xbt_dynar_length(unit->suites) - 1);\r
316 \r
317                                 if(!xbt_dynar_length((*current_suite)->includes))\r
318                                 {\r
319                                         ERROR2("[%s] Malformated suite `(%s)' : include missing", file_pos, (*current_suite)->description);\r
320                                 \r
321                                         unit_set_error(*current_suite, ESYNTAX, 1, file_pos);\r
322 \r
323                                         failure(unit);\r
324                                         \r
325                                 }\r
326                         \r
327                                 unit->is_running_suite = 0;\r
328                         }\r
329                                 \r
330                         if(context->command_line)\r
331                         {\r
332                                 if(fstream_launch_command(fstream, context, mutex) < 0)\r
333                                                 break;\r
334                         }\r
335                 \r
336                         continue;\r
337                 }\r
338                 \r
339                 if(linelen>1 && line[linelen-2]=='\\') \r
340                 {\r
341                         if(linelen>2 && line[linelen-3] == '\\') \r
342                         {\r
343                                 /* Damn. Escaped \ */\r
344                                 line[linelen-2] = '\n';\r
345                                 line[linelen-1] = '\0';\r
346                         } \r
347                         else \r
348                         {\r
349                                 to_be_continued = 1;\r
350                                 line[linelen-2] = '\0';\r
351                                 linelen -= 2;  \r
352                                 \r
353                                 if (!buff->used)\r
354                                         buffbegin = line_num;\r
355                         }\r
356                 }\r
357         \r
358                 if(buff->used || to_be_continued) \r
359                 { \r
360                         xbt_strbuff_append(buff,line);\r
361         \r
362                         if (!to_be_continued) \r
363                         {\r
364                                 snprintf(file_pos,256,"%s:%d",fstream->name, buffbegin);\r
365                                 fstream_lex_line(fstream, context, mutex, file_pos, buff->data);    \r
366                                 xbt_strbuff_empty(buff);\r
367                         }\r
368                 } \r
369                 else \r
370                 {\r
371                         snprintf(file_pos,256,"%s:%d",fstream->name, line_num);\r
372                         fstream_lex_line(fstream, context, mutex, file_pos, line);      \r
373                 }\r
374         }\r
375         \r
376         /* Check that last command of the file ran well */\r
377         if(context->command_line)\r
378         {\r
379                 if(fstream_launch_command(fstream, context, mutex) < 0)\r
380                         return -1;\r
381         }\r
382         \r
383         /* clear buffers */\r
384         if(line)\r
385                 free(line);\r
386                 \r
387         xbt_strbuff_free(buff); \r
388         \r
389         if(context_free(&context) < 0)\r
390                 return -1;\r
391         \r
392         return (exit_code || errno) ? -1 : 0;\r
393 }\r
394 \r
395 \r
396 void \r
397 fstream_lex_line(fstream_t fstream, context_t context, xbt_os_mutex_t mutex, const char * filepos, char *line) \r
398 {\r
399         char* line2;\r
400         variable_t variable;\r
401         unsigned int i;\r
402         char exp[PATH_MAX + 1] = {0};\r
403         unit_t unit = fstream->unit;\r
404         xbt_dynar_t variables = unit->runner->variables;\r
405         char* p= NULL;\r
406         char* end = NULL;\r
407         char* val = NULL;\r
408         char buff[PATH_MAX + 1] = {0}; \r
409         size_t len;\r
410         char delimiters[4] = {' ', '\t', '\n', '\0'}; \r
411         \r
412         int j;\r
413         \r
414         if(line[0] == '#')\r
415                 return;\r
416 \r
417         if(unit->is_running_suite && strncmp(line, "! include", strlen("! include")))\r
418         {/* it's the end of a suite */\r
419                 \r
420                 unit_t* current_suite = xbt_dynar_get_ptr(unit->suites, xbt_dynar_length(unit->suites) - 1);\r
421 \r
422                 if(!xbt_dynar_length((*current_suite)->includes))\r
423                         ERROR2("[%s] Malformated suite `(%s)': include missing", filepos, (*current_suite)->description);\r
424                 else\r
425                         ERROR2("[%s] Malformated suite `(%s)': blank line missing", filepos, (*current_suite)->description);\r
426                 \r
427                 unit_set_error(*current_suite, ESYNTAX, 1, filepos);\r
428 \r
429                 failure(fstream->unit);\r
430         }\r
431         \r
432         context->line = strdup(filepos);\r
433         \r
434         /* search end */\r
435         xbt_str_rtrim(line + 2,"\n");\r
436         \r
437         line2 = strdup(line);\r
438 \r
439         len = strlen(line2 + 2) + 1;\r
440 \r
441         /* replace each variable by its value */\r
442         xbt_os_mutex_acquire(unit->mutex);\r
443         \r
444 \r
445         /* replace all existing\r
446            ${var}\r
447            ${var:=val}\r
448            ${var:+val}\r
449            ${var:-val}\r
450            ${var:?val}\r
451            ${#var}\r
452    */\r
453         \r
454         xbt_dynar_foreach(variables, i, variable)\r
455         {\r
456                 if(!(p = strstr(line2 + 2, "${")))\r
457                         break;\r
458 \r
459                 memset(buff, 0, len);\r
460 \r
461                 sprintf(buff,"${%s",variable->name);\r
462                 \r
463                 /* FALSE */\r
464                 if((p = strstr(line2 + 2, buff)))\r
465                 {\r
466                         memset(buff, 0, len);\r
467                         p--;\r
468                         j = 0;\r
469 \r
470                         while(*(p++) != '\0')\r
471                         {\r
472                                 buff[j++] = *p;\r
473 \r
474                                 if(*p == '}')\r
475                                         break;\r
476                         }\r
477 \r
478                         if(buff[j - 1] != '}')\r
479                         {\r
480                                 xbt_os_mutex_release(unit->mutex);      \r
481                                 \r
482                                 \r
483                                 ERROR2("[%s] Syntax error : `%s'.",filepos, p - j);\r
484                                 \r
485                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
486                                 failure(fstream->unit);\r
487                                 return;\r
488                         }\r
489 \r
490                         if((p = strstr(buff , ":=")))\r
491                         {\r
492                                 /* ${var:=val} */\r
493                                 \r
494                                 /* if the value of the variable is empty, update its value by the value*/\r
495                                 p += 2;\r
496                                 \r
497                                 end = strchr(p, '}');\r
498 \r
499                                 if(!end || (end == p))\r
500                                 {\r
501                                         xbt_os_mutex_release(unit->mutex);      \r
502                                 \r
503                                 \r
504                                         ERROR2("[%s] Bad substitution : `%s'.",filepos, strstr(buff, "${"));\r
505                                 \r
506                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
507                                         failure(fstream->unit);\r
508                                         return;\r
509                                 }\r
510 \r
511                                 val = (char*) calloc((size_t)(end - p) + 1, sizeof(char));\r
512 \r
513                                 strncpy(val, p,(end - p));\r
514                                 \r
515                                 \r
516                                 /* replace the expression by the expression of the value of the variable*/\r
517                                 sprintf(exp, "${%s:=%s}", variable->name, val);\r
518 \r
519                                 if(variable->val)\r
520                                         str_replace_all(&line2, exp, variable->val, NULL);\r
521                                 else\r
522                                 {\r
523                                         str_replace_all(&line2, exp, val, NULL);\r
524 \r
525                                         variable->val = strdup(val);\r
526                                 }\r
527 \r
528                                 memset(exp, 0, VAR_NAME_MAX + 1);\r
529 \r
530                                 if(val)\r
531                                 {\r
532                                         free(val);\r
533                                         val = NULL;\r
534                                 }\r
535 \r
536                         }\r
537                         else if((p = strstr(buff, ":-")))\r
538                         {\r
539                                 /* ${var:-val} */\r
540                                 \r
541                                 /* if the value of the variable is empty, replace the expression by the value */\r
542                                 p += 2;\r
543                                 end = strchr(p, '}');\r
544 \r
545                                 if(!end || (end == p))\r
546                                 {\r
547                                         xbt_os_mutex_release(unit->mutex);      \r
548                                 \r
549                                         ERROR2("[%s] Bad substitution : `%s'.",filepos, strstr(line2, "${"));\r
550                                 \r
551                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
552                                         failure(fstream->unit);\r
553                                         return;\r
554                                 }\r
555 \r
556                                 val = (char*) calloc((size_t)(end - p) + 1, sizeof(char));\r
557 \r
558                                 strncpy(val, p,(end - p)); \r
559 \r
560                                 sprintf(exp, "${%s:-%s}", variable->name, val);\r
561                                 \r
562                                 str_replace_all(&line2, exp, variable->val ? variable->val : val, NULL);\r
563                                 \r
564 \r
565                                 memset(exp, 0, VAR_NAME_MAX + 1);\r
566                                 \r
567                                 if(val)\r
568                                 {\r
569                                         free(val);\r
570                                         val = NULL;\r
571                                 }\r
572 \r
573                         }\r
574                         else if((p = strstr(buff, ":+")))\r
575                         {\r
576                                 /* ${var:+val} */\r
577         \r
578                                 /* if the value of the variable is not empty, replace the expression by the value */\r
579                                 p += 2;\r
580 \r
581                                 end = strchr(p, '}');\r
582 \r
583                                 if(!end || (end == p))\r
584                                 {\r
585                                         xbt_os_mutex_release(unit->mutex);      \r
586                                 \r
587                                 \r
588                                         ERROR2("[%s] Bad substitution : `%s'.",filepos, strstr(line2, "${"));\r
589                                 \r
590                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
591                                         failure(fstream->unit);\r
592                                         return;\r
593                                 }\r
594 \r
595                                 val = (char*) calloc((size_t)(end - p) + 1, sizeof(char));\r
596 \r
597                                 strncpy(val, p,(end - p));\r
598 \r
599                                 sprintf(exp, "${%s:+%s}", variable->name, val);\r
600 \r
601                                 if(variable->val)\r
602                                 {\r
603                                         str_replace_all(&line2, exp, val, NULL);\r
604                                 }\r
605                                 else\r
606                                 {\r
607                                         str_replace_all(&line2, exp, NULL , NULL);\r
608                                         variable->val = strdup(val);\r
609                                 }\r
610                                 \r
611                                 memset(exp, 0, VAR_NAME_MAX + 1);\r
612                                 \r
613                                 if(val)\r
614                                 {\r
615                                         free(val);\r
616                                         val = NULL;\r
617                                 }\r
618                         }\r
619                         else if((p = strstr(buff, ":?")))\r
620                         {\r
621                                 /*  ${var:?val} */\r
622         \r
623                                 /* if the value of the variable is not empty, replace the expression by the value */\r
624                                 p += 2;\r
625                                 end = strchr(p, '}');\r
626 \r
627                                 if(!end || (end == p))\r
628                                 {\r
629                                         xbt_os_mutex_release(unit->mutex);      \r
630                                 \r
631                                         ERROR2("[%s] Bad substitution : `%s'.",filepos, strstr(line2, "${"));\r
632                                 \r
633                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
634                                         failure(fstream->unit);\r
635                                         return;\r
636                                 }\r
637 \r
638                                 val = (char*) calloc((size_t)(end - p) + 1, sizeof(char));\r
639 \r
640                                 strncpy(val, p,(end - p));\r
641 \r
642                                 sprintf(exp, "${%s:?%s}", variable->name, val);\r
643                                 \r
644                                 if(variable->val)\r
645                                         str_replace_all(&line2, exp, variable->val, NULL);\r
646                                 else\r
647                                 {\r
648 \r
649                                         xbt_os_mutex_release(unit->mutex);      \r
650 \r
651                                         ERROR2("[%s] %s.",filepos, val);\r
652 \r
653                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
654                                         failure(fstream->unit);\r
655                                         return;\r
656                                 }\r
657                                 \r
658                                 memset(exp, 0, VAR_NAME_MAX + 1);\r
659                                 \r
660                                 if(val)\r
661                                 {\r
662                                         free(val);\r
663                                         val = NULL;\r
664                                 }\r
665                         }\r
666                 }\r
667         }\r
668 \r
669         /* replace all existing $var */\r
670         xbt_dynar_foreach(variables, i, variable)\r
671         {\r
672                 if(!strchr(line2 + 2, '$'))\r
673                         break;\r
674 \r
675                 if(strstr(line2 + 2, variable->name))\r
676                 {\r
677 \r
678                         sprintf(exp, "${#%s}", variable->name);\r
679                         \r
680                         if(strstr(line2 + 2, exp))\r
681                         {\r
682 \r
683                                 if(variable->val)\r
684                                 {\r
685                                         char slen[4] = {0};\r
686                                         sprintf(slen,"%d", (int)strlen(variable->val));\r
687                                         str_replace_all(&line2, exp, slen, NULL);\r
688                                 }\r
689                                 else\r
690                                         str_replace_all(&line2, exp, "0", NULL);\r
691                         }\r
692 \r
693                         memset(exp, 0, VAR_NAME_MAX + 1);\r
694 \r
695                         sprintf(exp, "${%s}", variable->name);\r
696 \r
697                         if(strstr(line2 + 2, exp))\r
698                         {\r
699                                 if(variable->val)\r
700                                         str_replace_all(&line2, exp, variable->val, NULL);\r
701                                 else\r
702                                         str_replace_all(&line2, exp, NULL, NULL);\r
703                         }\r
704 \r
705                         memset(exp, 0, VAR_NAME_MAX + 1);\r
706 \r
707                         sprintf(exp, "$%s", variable->name);\r
708                         \r
709                         if((p = strstr(line2 + 2, exp)))\r
710                         {\r
711                                 if((p + strlen(variable->name) + 1)[0] != '\0' && !(isalpha((p + strlen(variable->name) + 1)[0])))\r
712                                         delimiters[0] = (p + strlen(variable->name) + 1)[0];\r
713 \r
714                                 if(variable->val)\r
715                                         str_replace_all(&line2, exp, variable->val,  delimiters);\r
716                                 else\r
717                                         str_replace_all(&line2, exp, NULL, delimiters);\r
718                         }\r
719 \r
720                         memset(exp, 0, VAR_NAME_MAX + 1);\r
721 \r
722                 }\r
723         }\r
724 \r
725         while((p = strstr(line2 + 2, "${")))\r
726         {\r
727                 /*if(*(p+1) != '{')\r
728                 {\r
729                         j = 0;\r
730                         p --;\r
731 \r
732                         while(*(p++) != '\0')\r
733                         {\r
734                                 if(*p != ' ' && *p !='\t')\r
735                                         exp[j++] = *p;\r
736                                 else\r
737                                         break;\r
738 \r
739                         }\r
740                         \r
741                         str_replace_all(&line2, exp, NULL, " \t\n\r");\r
742                         memset(exp, 0, VAR_NAME_MAX + 1);\r
743                 }.\r
744                 else\r
745                 */\r
746                 {\r
747                         char* begin = NULL;\r
748                         \r
749                         j = 0;\r
750                         p --;\r
751 \r
752                         while(*(p++) != '\0')\r
753                         {\r
754                                 if((!begin && *p != ' ' && *p !='\t') || begin)\r
755                                 {\r
756                                         /* `:' must be before this caracter, bad substitution : exit loop \r
757                                             ||\r
758                                                 the current character is already present, bad substitution : exit loop\r
759                                                 */\r
760                                         if(\r
761                                                         (\r
762                                                                 *(p - 1) != ':' && (\r
763                                                                                                                 (*p == '=') || (*p == '-') || (*p == '+') || (*p == '?')\r
764                                                                                                         )\r
765                                                         )\r
766                                                 || \r
767                                                         (\r
768                                                                 begin &&        (\r
769                                                                                                 (*p == ':') || (*p == '=') || (*p == '-') || (*p == '+') || (*p == '?')\r
770                                                                                         )\r
771                                                         )\r
772                                                 )\r
773                                                 break;\r
774                                         else\r
775                                                 exp[j++] = *p;\r
776 \r
777                                         if(*p == ':')\r
778                                         {\r
779                                                 /* save the begining of the value */\r
780                                                 if((*(p+1) == '=') || (*(p+1) == '-') || (*(p+1) == '+') || (*(p+1) == '?'))\r
781                                                 {\r
782                                                         begin = p + 2;\r
783                                                         exp[j++] = *(p+1);\r
784                                                         p++;\r
785                                                         continue;\r
786 \r
787                                                 }\r
788                                                 else\r
789                                                 /* the current char is `:' but the next is invalid, bad substitution : exit loop */\r
790                                                         break;\r
791                                         }\r
792                                         /* end of the substitution : exit loop */\r
793                                         else if(*p == '}')\r
794                                                 break;\r
795                                 }\r
796                                 else\r
797                                         break;\r
798                         }\r
799                         \r
800                         if(exp[j - 1] == '}')\r
801                         {\r
802                                 if(exp[2] == '#')\r
803                                 {\r
804                                         /* ${#var} */\r
805 \r
806 \r
807                                         if(4 == strlen(exp))\r
808                                         {\r
809                                                 xbt_os_mutex_release(unit->mutex);      \r
810                                         \r
811                                                 ERROR2("[%s] Bad substitution : `%s'.",filepos, strchr(line2 + 2, '$'));\r
812                                         \r
813                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
814                                                 failure(fstream->unit);\r
815                                                 return;\r
816                                         }\r
817                                         \r
818                                         str_replace_all(&line2, exp, "0", NULL);        \r
819                                 }\r
820                                 else if(strstr(exp,":="))\r
821                                 {\r
822                                         /* ${var:=value} */     \r
823                                         \r
824                                         end = strchr(p, '}');\r
825 \r
826                                         if(!end || (end == begin))\r
827                                         {\r
828                                                 xbt_os_mutex_release(unit->mutex);      \r
829                                         \r
830                                         \r
831                                                 ERROR2("[%s] Bad substitution : `%s'.",filepos, strchr(line2 + 2, '$'));\r
832                                         \r
833                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
834                                                 failure(fstream->unit);\r
835                                                 return;\r
836                                         }\r
837 \r
838                                         variable = xbt_new0(s_variable_t, 1);\r
839 \r
840                                         variable->val = (char*) calloc((size_t)(end - begin) + 1, sizeof(char));\r
841 \r
842                                         strncpy(variable->val, begin ,(end - begin));\r
843 \r
844                                         begin = exp + 2;\r
845                                         end = strchr(exp, ':');\r
846 \r
847                                         if(!end || (end == begin))\r
848                                         {\r
849                                                 xbt_os_mutex_release(unit->mutex);      \r
850                                         \r
851                                         \r
852                                                 ERROR2("[%s] Bad substitution : `%s'.",filepos, strchr(line2 + 2, '$'));\r
853                                         \r
854                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
855                                                 failure(fstream->unit);\r
856                                                 return;\r
857                                         }\r
858 \r
859                                         variable->name = (char*) calloc((size_t)(end - begin) + 1, sizeof(char));\r
860 \r
861                                         strncpy(variable->name, exp + 2 ,(end - begin));\r
862 \r
863                                         str_replace_all(&line2, exp, variable->val, NULL);\r
864 \r
865                                         xbt_dynar_push(variables, &variable);\r
866 \r
867                                 }\r
868                                 else if(strstr(exp,":-"))\r
869                                 {\r
870                                         /* ${var:-value} */     \r
871 \r
872                                         \r
873                                         end = strchr(p, '}');\r
874 \r
875                                         if(!end || (end == begin))\r
876                                         {\r
877                                                 xbt_os_mutex_release(unit->mutex);      \r
878                                         \r
879                                         \r
880                                                 ERROR2("[%s] Bad substitution : `%s'.",filepos, strchr(line2 + 2, '$'));\r
881                                         \r
882                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
883                                                 failure(fstream->unit);\r
884                                                 return;\r
885                                         }\r
886 \r
887                                         val = (char*) calloc((size_t)(end - begin) + 1, sizeof(char));\r
888 \r
889                                         strncpy(val, begin ,(end - begin));\r
890 \r
891                                         str_replace_all(&line2, exp, val, NULL);\r
892 \r
893                                         if(val)\r
894                                                 free(val);\r
895 \r
896                                 }\r
897                                 else if(strstr(exp,":+"))\r
898                                 {\r
899                                         /* ${var:+value} */     \r
900 \r
901                                         end = strchr(p, '}');\r
902 \r
903                                         if(!end || (end == begin))\r
904                                         {\r
905                                                 xbt_os_mutex_release(unit->mutex);      \r
906                                         \r
907                                                 ERROR2("[%s] Bad substitution : `%s'.",filepos, strchr(line2 + 2, '$'));\r
908                                         \r
909                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
910                                                 failure(fstream->unit);\r
911                                                 return;\r
912                                         }\r
913 \r
914                                         str_replace_all(&line2, exp, NULL, NULL);\r
915                                 }\r
916                                 else if(strstr(exp,":?"))\r
917                                 {\r
918                                         /* ${var:?value} */\r
919                                         \r
920                                         end = strchr(p, '}');\r
921 \r
922                                         if(!end || (end == begin))\r
923                                         {\r
924                                                 xbt_os_mutex_release(unit->mutex);      \r
925                                         \r
926                                                 ERROR2("[%s] Bad substitution : `%s'.",filepos, strchr(line2 + 2, '$'));\r
927                                         \r
928                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
929                                                 failure(fstream->unit);\r
930                                                 return;\r
931                                         }\r
932 \r
933                                         val = (char*) calloc((size_t)(end - begin) + 1, sizeof(char));\r
934 \r
935                                         strncpy(val, begin ,(end - begin));\r
936 \r
937                                         xbt_os_mutex_release(unit->mutex);      \r
938                                         \r
939                                         ERROR2("[%s] : `%s'.",filepos, val);\r
940 \r
941                                         if(val)\r
942                                                 free(val);\r
943                                         \r
944                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
945                                         failure(fstream->unit);\r
946 \r
947                                         return;\r
948                                         \r
949                                 }\r
950                                 else\r
951                                 {\r
952                                         /* ${var} */\r
953 \r
954                                         if(3 == strlen(exp))\r
955                                         {\r
956                                                 xbt_os_mutex_release(unit->mutex);      \r
957                                         \r
958                                                 ERROR2("[%s] Bad substitution : `%s'.",filepos, strchr(line2 + 2, '$'));\r
959                                         \r
960                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
961                                                 failure(fstream->unit);\r
962                                                 return;\r
963                                         }\r
964 \r
965                                         str_replace_all(&line2, exp, NULL, NULL);\r
966                                         \r
967                                 }\r
968 \r
969                                 memset(exp, 0, VAR_NAME_MAX + 1);\r
970                         }\r
971                         else\r
972                         {\r
973                                 xbt_os_mutex_release(unit->mutex);      \r
974                                 \r
975                                 if(strstr(line2 + 2, "${"))\r
976                                         ERROR2("[%s] Bad substitution : `%s'.",filepos, strstr(line2, "${"));\r
977                                 else\r
978                                         ERROR2("[%s] Syntax error : `%s'.",filepos, strstr(line2, "${"));\r
979 \r
980                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
981                                 failure(fstream->unit);\r
982                                 return;\r
983                         }\r
984 \r
985                 }\r
986                 \r
987         }\r
988         \r
989         p = line2 + 2;\r
990         \r
991         while(p && 1)\r
992         {\r
993                 if((p = strchr(p, '$')))\r
994                 {\r
995                         if(*(p+1) != ' ')\r
996                         {\r
997                                 j = 0;\r
998                                 p --;\r
999 \r
1000                                 while(*(p++) != '\0')\r
1001                                 {\r
1002                                         if(*p != ' ' && *p !='\t')\r
1003                                                 exp[j++] = *p;\r
1004                                         else\r
1005                                                 break;\r
1006 \r
1007                                 }\r
1008                                 \r
1009                                 str_replace_all(&line2, exp, NULL, " \t\n\r");\r
1010                                 memset(exp, 0, VAR_NAME_MAX + 1);\r
1011                         }\r
1012                         else\r
1013                         {\r
1014                                 /* maybe < $ cmd */\r
1015                                 p++;\r
1016                         }\r
1017                 }\r
1018                 else\r
1019                         break;\r
1020         }\r
1021 \r
1022         xbt_os_mutex_release(unit->mutex);      \r
1023         \r
1024         switch(line2[0]) \r
1025         {\r
1026                 /*case '#': \r
1027                 break;\r
1028                 */\r
1029                 \r
1030                 case '$':\r
1031                 case '&':\r
1032 \r
1033                 if(line[1] != ' ')\r
1034                 {\r
1035                         \r
1036                         if(line2[0] == '$')\r
1037                                 ERROR1("[%s] Missing space after `$' `(usage : $ <command>)'", filepos);\r
1038                         else\r
1039                                 ERROR1("[%s] Missing space after & `(usage : & <command>)'", filepos);\r
1040                 \r
1041                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1042 \r
1043                         failure(unit);\r
1044                         return;\r
1045                 }\r
1046                         \r
1047                 context->async = (line2[0] == '&');\r
1048 \r
1049                 \r
1050                 /* further trim useless chars which are significant for in/output */\r
1051                 xbt_str_rtrim(line2 + 2," \t");\r
1052                 \r
1053                 /* deal with CD commands here, not in context */\r
1054                 if(!strncmp("cd ",line2 + 2, 3)) \r
1055                 {\r
1056                         char* dir = strdup(line2 + 4);\r
1057                         \r
1058                         if(context->command_line)\r
1059                         {\r
1060                                 if(fstream_launch_command(fstream, context, mutex) < 0)\r
1061                                         return;\r
1062                         }\r
1063                 \r
1064                         /* search begining */\r
1065                         while(*(dir++) == ' ');\r
1066                         \r
1067                         dir--;\r
1068                         \r
1069                         if(!dry_run_flag)\r
1070                         {\r
1071                                 if(!silent_flag)\r
1072                                         INFO2("[%s] cd %s", filepos, dir);\r
1073                                 \r
1074                                 if(!just_print_flag)\r
1075                                 {\r
1076                                         if(chdir(dir))\r
1077                                         {\r
1078                                                 ERROR3("[%s] Chdir to %s failed: %s",filepos, dir,error_to_string(errno, 0));\r
1079                                                 unit_set_error(fstream->unit, errno, 0, filepos);\r
1080 \r
1081                                                 failure(unit);\r
1082                                         }\r
1083                                 }\r
1084                         }\r
1085                         \r
1086                         break;\r
1087                 }\r
1088                 else\r
1089                 {\r
1090                         fstream_process_token(fstream, context, mutex, filepos, line2[0], line2 + 2);\r
1091                         break;\r
1092                 }\r
1093                 \r
1094                 case '<':\r
1095                 case '>':\r
1096                 case '!':\r
1097                 \r
1098                 if(line[0] == '!' && line[1] != ' ')\r
1099                 {\r
1100                         ERROR1("[%s] Missing space after `!' `(usage : ! <command> [[=]value])'", filepos);\r
1101                 \r
1102                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1103 \r
1104                         failure(unit);\r
1105                         return;\r
1106                 }\r
1107 \r
1108                 fstream_process_token(fstream, context, mutex, filepos, line2[0], line2 + 2);    \r
1109                 break;\r
1110                 \r
1111                 case 'p':\r
1112                 \r
1113                 {\r
1114                         unsigned int j;\r
1115                         int is_blank = 1;\r
1116                         \r
1117                         char* prompt = line2 + 2;\r
1118 \r
1119                         for(j = 0; j < strlen(prompt); j++)\r
1120                         {\r
1121                                 if (prompt[j] != ' ' && prompt[j] != '\t')\r
1122                                 {\r
1123                                         is_blank = 0;\r
1124                                         break;\r
1125                                 }\r
1126                         }\r
1127 \r
1128                         if(is_blank)\r
1129                         {\r
1130                                 ERROR1("[%s] Bad usage of the metacommand p `(usage : p <prompt>)'", filepos);\r
1131                                 \r
1132                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1133 \r
1134                                 failure(unit);\r
1135                                 return;\r
1136                         }\r
1137 \r
1138                         if(!dry_run_flag)\r
1139                                 INFO2("[%s] %s",filepos,prompt);\r
1140                 }\r
1141 \r
1142                 \r
1143                 break;\r
1144                 \r
1145                 case 'P':\r
1146                 \r
1147                 {\r
1148                         unsigned int j;\r
1149                         int is_blank = 1;\r
1150                                 \r
1151                         char* prompt = line2 + 2;\r
1152 \r
1153                         for(j = 0; j < strlen(prompt); j++) \r
1154                                 if (prompt[j] != ' ' && prompt[j] != '\t')\r
1155                                         is_blank = 0;\r
1156 \r
1157                         if(is_blank)\r
1158                         {\r
1159                                 ERROR1("[%s] Bad usage of the metacommand P `(usage : P <prompt>)'", filepos);\r
1160                                 \r
1161                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1162 \r
1163                                 failure(unit);\r
1164                                 return;\r
1165                         }\r
1166 \r
1167                         if(!dry_run_flag)               \r
1168                                 CRITICAL2("[%s] %s",filepos, prompt);\r
1169                 }\r
1170 \r
1171                 break;\r
1172                 \r
1173                 case 'D':\r
1174                         if(unit->description)\r
1175                                 WARN2("[%s] Description already specified `%s'",filepos, line2 + 2); \r
1176                         else\r
1177                         {\r
1178                                 unsigned int j;\r
1179                                 int is_blank = 1;\r
1180                                 \r
1181                                 char* desc = line2 + 2;\r
1182 \r
1183                                 for(j = 0; j < strlen(desc); j++) \r
1184                                         if (desc[j] != ' ' && desc[j] != '\t')\r
1185                                                 is_blank = 0;\r
1186 \r
1187                                 if(is_blank)\r
1188                                 {\r
1189                                         ERROR1("[%s] Bad usage of the metacommand D `(usage : D <Description>)'", filepos);\r
1190                                         \r
1191                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1192 \r
1193                                         failure(unit);\r
1194                                         return;\r
1195                                 }\r
1196 \r
1197                                 unit->description = strdup(desc);\r
1198                         }\r
1199                 break;\r
1200                 \r
1201                 default:\r
1202                 ERROR2("[%s] Syntax error `%s'", filepos, line2);\r
1203                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1204                 failure(unit);\r
1205                 break;\r
1206         }\r
1207         \r
1208         free(line2);\r
1209 }\r
1210 \r
1211 void \r
1212 fstream_process_token(fstream_t fstream, context_t context, xbt_os_mutex_t mutex, const char* filepos, char token, char *line) \r
1213 {\r
1214         unit_t unit = fstream->unit;\r
1215         \r
1216         switch (token) \r
1217         {\r
1218                 case '$':\r
1219                 case '&':\r
1220                 \r
1221                 if(context->command_line) \r
1222                 {\r
1223                         \r
1224                         if(context->output->used || context->input->used) \r
1225                         {\r
1226                                 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
1227 \r
1228                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1229                                 failure(unit);\r
1230                                 return;\r
1231                         }\r
1232                         \r
1233                         if(fstream_launch_command(fstream, context, mutex) < 0)\r
1234                                         return;\r
1235                         \r
1236                         VERB1("[%s] More than one command in this chunk of lines",filepos);\r
1237                 }\r
1238                 \r
1239                 {\r
1240                         size_t j,\r
1241                         is_blank = 1;\r
1242 \r
1243                         for(j = 0; j < strlen(line); j++) \r
1244                                 if (line[j] != ' ' && line[j] != '\t')\r
1245                                         is_blank = 0;\r
1246 \r
1247                         if(is_blank)\r
1248                         {\r
1249                                 if(token == '$')\r
1250                                 ERROR1("[%s] Undefinite command for `$' `(usage: $ <command>)'", filepos);\r
1251                                 else\r
1252                                 ERROR1("[%s] Undefinite command for `&' `(usage: & <command>)'", filepos);\r
1253 \r
1254                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1255 \r
1256                                 failure(unit);\r
1257                                 return;\r
1258                         }\r
1259                 }\r
1260                 \r
1261                 context->command_line = strdup(line);\r
1262 \r
1263                 xbt_str_ltrim(context->command_line," ");\r
1264                 \r
1265                 context->line = /*strdup(filepos)*/ filepos;\r
1266                 context->pos = strdup(filepos);\r
1267                 \r
1268                 #ifdef WIN32\r
1269                 {\r
1270 \r
1271                 /* translate the command line */\r
1272 \r
1273                 char* path = NULL;\r
1274                 char* delimiter;\r
1275                 char command_line[PATH_MAX + 1] = {0};\r
1276                 size_t i = 0;\r
1277                 char* args = NULL;\r
1278 \r
1279                 \r
1280 \r
1281                 /*if(strstr(context->command_line,".exe"))\r
1282                         strcpy(command_line,context->command_line);*/\r
1283                 \r
1284                 {\r
1285                         size_t len;\r
1286                         \r
1287                         size_t j = 0;\r
1288         \r
1289                         len = strlen(context->command_line);\r
1290                         \r
1291                         while(i < len)\r
1292                         {\r
1293                                 if(context->command_line[i] != ' ' && context->command_line[i] != '\t' && context->command_line[i] != '>')\r
1294                                         command_line[j++] = context->command_line[i];\r
1295                                 else\r
1296                                         break;\r
1297                                         \r
1298                                 i++;\r
1299                         }\r
1300                         \r
1301                         if(!strstr(context->command_line,".exe"))\r
1302                                 strcat(command_line,".exe");\r
1303 \r
1304                         args = strdup(context->command_line + i);\r
1305                 }\r
1306                 \r
1307                 if(!is_w32_cmd(command_line, fstream->unit->runner->path) && getpath(command_line, &path) < 0)\r
1308                 {\r
1309                         ERROR3("[%s] `%s' : NOK (%s)", filepos, command_line, error_to_string(ECMDNOTFOUND, 1));\r
1310                         unit_set_error(fstream->unit, ECMDNOTFOUND, 1, filepos);\r
1311                         failure(unit);\r
1312                         return;\r
1313                 }\r
1314                 \r
1315                 delimiter = strrchr(command_line,'/');\r
1316 \r
1317                 if(!delimiter)\r
1318                         delimiter = strrchr(command_line,'\\');\r
1319                 \r
1320                 /*free(context->command_line);*/\r
1321                 \r
1322                 \r
1323                 if(path)\r
1324                 {\r
1325                         if(args)\r
1326                         {\r
1327                                 context->t_command_line = (char*)calloc(strlen(path) + strlen(delimiter ? delimiter + 1 : command_line) + strlen(args) + 2, sizeof(char));\r
1328                                 sprintf(context->t_command_line,"%s\\%s%s",path,delimiter ? delimiter + 1 : command_line, args);\r
1329 \r
1330                                 free(args);\r
1331 \r
1332                         }\r
1333                         else\r
1334                         {\r
1335                                 context->t_command_line = (char*)calloc(strlen(path) + strlen(delimiter ? delimiter + 1 : command_line) + 2, sizeof(char));\r
1336                                 sprintf(context->t_command_line,"%s\\%s",path,delimiter ? delimiter + 1 : command_line);\r
1337                         }\r
1338                 \r
1339                         free(path);\r
1340                 }\r
1341                 else\r
1342                 {\r
1343                         if(args)\r
1344                         {\r
1345 \r
1346                                 context->t_command_line = (char*)calloc(strlen(command_line) + strlen(args) + 1, sizeof(char));\r
1347                                 sprintf(context->t_command_line,"%s%s",command_line, args);\r
1348 \r
1349                         \r
1350                                 free(args);\r
1351 \r
1352                         }\r
1353                         else\r
1354                         {\r
1355                                 context->t_command_line = (char*)calloc(strlen(command_line) + 1, sizeof(char));\r
1356                                 strcpy(context->t_command_line,command_line);\r
1357                         }\r
1358                 }\r
1359 \r
1360 \r
1361                 }\r
1362                 #endif\r
1363 \r
1364 \r
1365                 break;\r
1366                 \r
1367                 case '<':\r
1368                 xbt_strbuff_append(context->input,line);\r
1369                 xbt_strbuff_append(context->input,"\n");\r
1370                 break;\r
1371                 \r
1372                 case '>':\r
1373                 xbt_strbuff_append(context->output,line);\r
1374                 xbt_strbuff_append(context->output,"\n");\r
1375                 break;\r
1376                 \r
1377                 case '!':\r
1378                 \r
1379                 if(context->command_line)\r
1380                 {\r
1381                         if(fstream_launch_command(fstream, context, mutex) < 0)\r
1382                                         return;\r
1383                 }\r
1384                 \r
1385                 if(!strncmp(line,"timeout no",strlen("timeout no"))) \r
1386                 {\r
1387                         VERB1("[%s] (disable timeout)", filepos);\r
1388                         context->timeout = INDEFINITE;\r
1389                 } \r
1390                 else if(!strncmp(line,"timeout ",strlen("timeout "))) \r
1391                 {\r
1392                         int i = 0;\r
1393                         unsigned int j;\r
1394                         int is_blank = 1;\r
1395                         char* p = line + strlen("timeout ");\r
1396 \r
1397 \r
1398                         for(j = 0; j < strlen(p); j++) \r
1399                                 if (p[j] != ' ' && p[j] != '\t')\r
1400                                         is_blank = 0;\r
1401 \r
1402                         if(is_blank)\r
1403                         {\r
1404                                 ERROR1("[%s] Undefinite timeout value `(usage :timeout <seconds>)'", filepos);\r
1405                                 \r
1406                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1407 \r
1408                                 failure(unit);\r
1409                                 return;\r
1410                         }\r
1411         \r
1412                         while(p[i] != '\0')\r
1413                         {\r
1414                                 if(!isdigit(p[i]))\r
1415                                 {\r
1416                                         ERROR2("[%s] Invalid timeout value `(%s)' : `(usage :timeout <seconds>)'", filepos, line + strlen("timeout "));\r
1417 \r
1418                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1419  \r
1420                                         failure(unit);\r
1421                                         return;\r
1422                                 }\r
1423 \r
1424                                 i++;\r
1425                         }\r
1426                         \r
1427                         context->timeout = atoi(line + strlen("timeout"));\r
1428                         VERB2("[%s] (new timeout value: %d)",filepos,context->timeout);\r
1429                 \r
1430                 } \r
1431                 else if (!strncmp(line,"expect signal ",strlen("expect signal "))) \r
1432                 {\r
1433                         unsigned int j;\r
1434                         int is_blank = 1;\r
1435 \r
1436                         \r
1437                         char* p = line + strlen("expect signal ");\r
1438 \r
1439 \r
1440                         for(j = 0; j < strlen(p); j++) \r
1441                                 if (p[j] != ' ' && p[j] != '\t')\r
1442                                         is_blank = 0;\r
1443 \r
1444                         if(is_blank)\r
1445                         {\r
1446                                 ERROR1("[%s] Undefinite signal name `(usage :expect signal <signal name>)'", filepos);\r
1447                                 \r
1448                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1449 \r
1450                                 failure(unit);\r
1451                                 return;\r
1452                         }\r
1453 \r
1454                         context->signal = strdup(line + strlen("expect signal "));\r
1455                         \r
1456                         xbt_str_trim(context->signal," \n");\r
1457 \r
1458                         #ifdef WIN32\r
1459                         if(!strstr("SIGSEGVSIGTRAPSIGBUSSIGFPESIGILL", context->signal))\r
1460                         {\r
1461                                 ERROR2("[%s] Signal `%s' not supported by this platform", filepos, context->signal);\r
1462 \r
1463                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1464 \r
1465 \r
1466                                 failure(unit);\r
1467                                 return;\r
1468                         }\r
1469                         #else\r
1470                         if(!sig_exists(context->signal))\r
1471                         {\r
1472                                 ERROR2("[%s] Signal `%s' not supported by Tesh", filepos, context->signal);\r
1473                                 \r
1474                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1475 \r
1476 \r
1477                                 failure(unit);\r
1478                                 return;\r
1479                         }\r
1480 \r
1481                         #endif\r
1482 \r
1483                         \r
1484                         VERB2("[%s] (next command must raise signal %s)", filepos, context->signal);\r
1485                 \r
1486                 } \r
1487                 else if (!strncmp(line,"expect return ",strlen("expect return "))) \r
1488                 {\r
1489 \r
1490                         int i = 0;\r
1491                         unsigned int j;\r
1492                         int is_blank = 1;\r
1493                         char* p = line + strlen("expect return ");\r
1494 \r
1495 \r
1496                         for(j = 0; j < strlen(p); j++) \r
1497                                 if (p[j] != ' ' && p[j] != '\t')\r
1498                                         is_blank = 0;\r
1499 \r
1500                         if(is_blank)\r
1501                         {\r
1502                                 ERROR1("[%s] Undefinite return value `(usage :expect return <return value>)'", filepos);\r
1503                                 \r
1504                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1505 \r
1506                                 failure(unit);\r
1507                                 return;\r
1508                         }\r
1509                         \r
1510                         while(p[i] != '\0')\r
1511                         {\r
1512                                 if(!isdigit(p[i]))\r
1513                                 {\r
1514                                         ERROR2("[%s] Invalid exit code value `(%s)' : must be an integer >= 0 and <=255",  filepos, line + strlen("expect return "));\r
1515 \r
1516                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1517 \r
1518                                         failure(unit);\r
1519                                         return;\r
1520                                 }\r
1521 \r
1522                                 i++;\r
1523                         }\r
1524 \r
1525                         context->exit_code = atoi(line+strlen("expect return "));\r
1526                         VERB2("[%s] (next command must return code %d)",filepos, context->exit_code);\r
1527                 \r
1528                 } \r
1529                 else if (!strncmp(line,"output ignore",strlen("output ignore"))) \r
1530                 {\r
1531                         context->output_handling = oh_ignore;\r
1532                         VERB1("[%s] (ignore output of next command)", filepos);\r
1533                 \r
1534                 } \r
1535                 else if (!strncmp(line,"output display",strlen("output display"))) \r
1536                 {\r
1537                         context->output_handling = oh_display;\r
1538                         VERB1("[%s] (ignore output of next command)", filepos);\r
1539                 \r
1540                 } \r
1541                 else if(!strncmp(line,"include ", strlen("include ")))\r
1542                 {\r
1543                         char* p1;\r
1544                         char* p2;\r
1545                         \r
1546                         p1 = line + strlen("include");\r
1547                         \r
1548                         while(*p1 == ' ' || *p1 == '\t')\r
1549                                 p1++;\r
1550                                 \r
1551                         \r
1552                         if(p1[0] == '\0')\r
1553                         {\r
1554                                 ERROR1("[%s] no file specified : `(usage : include <file> [<description>])'", filepos);\r
1555 \r
1556                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1557 \r
1558                                 failure(unit);\r
1559                                 return;\r
1560                         }\r
1561                         else\r
1562                         {\r
1563                                 char file_name[PATH_MAX + 1] = {0};\r
1564                                 \r
1565                                 p2 = p1;\r
1566                                 \r
1567                                 while(*p2 != '\0' && *p2 != ' ' && *p2 != '\t')\r
1568                                         p2++;\r
1569                                         \r
1570                                 strncpy(file_name, p1, p2 - p1);\r
1571                                 \r
1572                                 \r
1573                                 if(p2[0] != '\0')\r
1574                                         while(*p2 == ' ' || *p2 == '\t')\r
1575                                                 p2++;\r
1576                                         \r
1577                                 fstream_handle_include(fstream, context, mutex, file_name, p2[0] != '\0' ? p2 : NULL);\r
1578                                 \r
1579                         }\r
1580                 }\r
1581                 else if(!strncmp(line,"suite ", strlen("suite ")))\r
1582                 {\r
1583                         unsigned int j;\r
1584                         int is_blank = 1;\r
1585                         char* p = line + strlen("suite ");\r
1586 \r
1587 \r
1588                         for(j = 0; j < strlen(p); j++) \r
1589                                 if (p[j] != ' ' && p[j] != '\t')\r
1590                                         is_blank = 0;\r
1591                         \r
1592                         if(is_blank)\r
1593                         {\r
1594                                 ERROR1("[%s] Undefinite suit description : `(usage : suite <description>)", filepos);\r
1595 \r
1596                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1597 \r
1598                                 failure(unit);\r
1599                                 return;\r
1600                         }\r
1601 \r
1602                         if(unit->is_running_suite)\r
1603                         {\r
1604                                 ERROR1("[%s] Suite already in progress", filepos);\r
1605 \r
1606                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1607 \r
1608                                 failure(unit);\r
1609                                 return;\r
1610                         }\r
1611                         \r
1612                         fstream_handle_suite(fstream, line + strlen("suite "), filepos);\r
1613                 }\r
1614                 else if(!strncmp(line,"unsetenv ", strlen("unsetenv ")))\r
1615                 {\r
1616                         unsigned int i, j;\r
1617                         int exists = 0;\r
1618                         int env = 0;\r
1619                         int err = 0;\r
1620                         variable_t variable;\r
1621                         int is_blank;\r
1622 \r
1623                         char* name = line + strlen("unsetenv ");\r
1624 \r
1625                         is_blank = 1;\r
1626 \r
1627                         for(j = 0; j < strlen(name); j++) \r
1628                                 if (name[j] != ' ' && name[j] != '\t')\r
1629                                         is_blank = 0;\r
1630 \r
1631                         if(is_blank)\r
1632                         {\r
1633                                 ERROR1("[%s] Bad usage of the metacommand unsetenv : `(usage : unsetenv variable)'", filepos);\r
1634                                 \r
1635                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1636 \r
1637                                 failure(unit);\r
1638                                 return;\r
1639                         }\r
1640                         \r
1641                         xbt_os_mutex_acquire(unit->mutex);\r
1642                         \r
1643 \r
1644                         xbt_dynar_foreach(unit->runner->variables, i, variable)\r
1645                         {\r
1646                                 if(!strcmp(variable->name, name))\r
1647                                 {\r
1648                                         env = variable->env;\r
1649                                         err = variable->err;\r
1650                                         exists = 1;\r
1651                                         break;\r
1652                                 }\r
1653                         }\r
1654                                 \r
1655                         if(env)\r
1656                         {\r
1657                                 if(exists)\r
1658                                 {\r
1659                                         #ifndef WIN32\r
1660                                         unsetenv(name);\r
1661                                         #else\r
1662                                         SetEnvironmentVariable(name, NULL);\r
1663                                         #endif\r
1664                                         xbt_dynar_cursor_rm(unit->runner->variables, &i);\r
1665                                 }\r
1666                                 else\r
1667                                 {\r
1668                                         ERROR2("[%s] `(%s)' environment variable not found : impossible to unset it",filepos, name);    \r
1669                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1670                                         xbt_os_mutex_release(unit->mutex);\r
1671                                         failure(unit);\r
1672                                         return;\r
1673                                 }\r
1674                         }\r
1675                         else\r
1676                         {\r
1677                                 if(exists)\r
1678                                 {\r
1679                                         if(!err)\r
1680                                         {\r
1681                                                 ERROR2("[%s] `(%s)' is not an environment variable : use `unset' instead `unsetenv'",filepos, name);    \r
1682                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1683                                                 failure(unit);\r
1684                                                 xbt_os_mutex_release(unit->mutex);\r
1685                                                 return;\r
1686                                         }\r
1687                                         else\r
1688                                         {\r
1689                                                 ERROR2("[%s] `(%s)' is not an environment variable (it's a system variable) : impossible to unset it",filepos, name);   \r
1690                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1691                                                 xbt_os_mutex_release(unit->mutex);\r
1692                                                 failure(unit);\r
1693                                                 return;\r
1694                                         }\r
1695                                 }\r
1696                                 else\r
1697                                 {\r
1698                                         ERROR2("[%s] `(%s)' environment variable not found : impossible to unset it",filepos, name);    \r
1699                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1700                                         xbt_os_mutex_release(unit->mutex);\r
1701                                         failure(unit);\r
1702                                         return;\r
1703                                 }\r
1704                         }\r
1705                         \r
1706                         xbt_os_mutex_release(unit->mutex);      \r
1707                         \r
1708                                 \r
1709                 }\r
1710                 else if(!strncmp(line,"setenv ", strlen("setenv ")))\r
1711                 {\r
1712                         char* val;\r
1713                         char name[PATH_MAX + 1] = {0};\r
1714                         char* p;\r
1715                         unsigned int i;\r
1716                         int is_blank;\r
1717                         unsigned int j;\r
1718                         \r
1719                         p = line + strlen("setenv ");\r
1720                         \r
1721                         val = strchr(p, '=');\r
1722                         \r
1723                         if(val)\r
1724                         {\r
1725                                 variable_t variable;\r
1726                                 int exists = 0;\r
1727                                 int env = 0;\r
1728                                 int err = 0;\r
1729                                 val++;\r
1730                                 \r
1731                                 /* syntax error */\r
1732                                 if(val[0] == '\0' || val[0] ==' ' || val[0] =='\t')\r
1733                                 {\r
1734                                         ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);  \r
1735 \r
1736                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1737 \r
1738                                         failure(unit);\r
1739                                         return;\r
1740                                 }\r
1741                                 \r
1742                                 \r
1743                                 \r
1744                                 strncpy(name, p, (val - p -1));\r
1745 \r
1746                                 is_blank = 1;\r
1747 \r
1748                                 for(j = 0; j < strlen(name); j++) \r
1749                                         if (name[j] != ' ' && name[j] != '\t')\r
1750                                                 is_blank = 0;\r
1751 \r
1752                                 if(is_blank)\r
1753                                 {\r
1754                                         \r
1755                                         ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);\r
1756                                         \r
1757                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1758 \r
1759                                         failure(unit);\r
1760                                         return;\r
1761                                 }\r
1762                                 \r
1763                                 /* test if the variable is already registred */\r
1764                                 xbt_os_mutex_acquire(unit->mutex);\r
1765 \r
1766                                 xbt_dynar_foreach(unit->runner->variables, i, variable)\r
1767                                 {\r
1768                                         if(!strcmp(variable->name, name))\r
1769                                         {\r
1770                                                 env = variable->env;\r
1771                                                 err = variable->err;\r
1772                                                 exists = 1;\r
1773                                                 break;\r
1774                                         }\r
1775                                 }\r
1776                                 \r
1777                                 /* if the variable is already registred, update its value;\r
1778                                  * otherwise register it.\r
1779                                  */\r
1780                                 if(exists)\r
1781                                 {\r
1782                                         if(env)\r
1783                                         {\r
1784                                                 if(!strcmp(val, variable->val))\r
1785                                                         WARN3("[%s] This environment variable `(%s)' is already set with the value `(%s)'", filepos, name, val);\r
1786 \r
1787                                                 free(variable->val);\r
1788                                                 variable->val = strdup(val);\r
1789 \r
1790                                                 #ifdef WIN32\r
1791                                                 SetEnvironmentVariable(variable->name, variable->val);\r
1792                                                 #else\r
1793                                                 setenv(variable->name, variable->val, 1);\r
1794                                                 #endif\r
1795                                         }\r
1796                                         else\r
1797                                         {\r
1798                                                 if(err)\r
1799                                                         ERROR2("[%s] Conflict : a system variable `(%s)' already exists", filepos, name);\r
1800                                                 else\r
1801                                                         ERROR2("[%s] Conflict : (none environment) variable `(%s)' already exists", filepos, name);     \r
1802 \r
1803                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1804                                                 xbt_os_mutex_release(unit->mutex);\r
1805                                                 failure(unit);\r
1806                                                 return;\r
1807                                         }\r
1808                                 }\r
1809                                 else\r
1810                                 {\r
1811                                         if(err)\r
1812                                         {\r
1813                                                 ERROR2("[%s] A system variable named `(%s)' already exists", filepos, name);\r
1814                                         \r
1815                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1816                                                 xbt_os_mutex_release(unit->mutex);\r
1817                                                 failure(unit);\r
1818                                                 return;\r
1819                                         }\r
1820                                         else\r
1821                                         {\r
1822                                                 variable = variable_new(name, val);\r
1823                                                 variable->env = 1;\r
1824                                                 \r
1825                                                 xbt_dynar_push(unit->runner->variables, &variable);\r
1826                                                 \r
1827                                                 #ifdef WIN32\r
1828                                                 SetEnvironmentVariable(variable->name, variable->val);\r
1829                                                 #else\r
1830                                                 setenv(variable->name, variable->val, 0);\r
1831                                                 #endif\r
1832                                         }\r
1833                                 }\r
1834                                 \r
1835                                 xbt_os_mutex_release(unit->mutex);\r
1836                                 \r
1837                         }\r
1838                         else\r
1839                         {\r
1840                                 ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);\r
1841                                         \r
1842                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1843                                 failure(unit);\r
1844                                 return;\r
1845                         }\r
1846                 }\r
1847                 else if(!strncmp(line,"unset ", strlen("unset ")))\r
1848                 {\r
1849                         unsigned int i, j;\r
1850                         int exists = 0;\r
1851                         int env = 0;\r
1852                         int err = 0;\r
1853                         variable_t variable;\r
1854                         int is_blank;\r
1855 \r
1856                         char* name = line + strlen("unset ");\r
1857 \r
1858                         is_blank = 1;\r
1859 \r
1860                         for(j = 0; j < strlen(name); j++) \r
1861                                 if (name[j] != ' ' && name[j] != '\t')\r
1862                                         is_blank = 0;\r
1863 \r
1864                         if(is_blank)\r
1865                         {\r
1866                                 \r
1867                                 ERROR1("[%s] Bad usage of the metacommand unset `(usage : unset variable)'", filepos);\r
1868                                 \r
1869                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1870 \r
1871                                 failure(unit);\r
1872                                 return;\r
1873                         }\r
1874 \r
1875                         \r
1876                         xbt_os_mutex_acquire(unit->mutex);\r
1877 \r
1878                         xbt_dynar_foreach(unit->runner->variables, i, variable)\r
1879                         {\r
1880                                 if(!strcmp(variable->name, name))\r
1881                                 {\r
1882                                         env = variable->env;\r
1883                                         err = variable->err;\r
1884                                         exists = 1;\r
1885                                         break;\r
1886                                 }\r
1887                         }\r
1888                                 \r
1889                         if(!env && !err)\r
1890                         {\r
1891                                 if(exists)\r
1892                                 {\r
1893                                         /*xbt_dynar_remove_at(unit->runner->variables, i, NULL);*/\r
1894                                         /*xbt_dynar_cursor_rm(unit->runner->variables, &i);*/\r
1895                                         if(variable->val)\r
1896                                         {\r
1897                                                 free(variable->val);\r
1898                                                 variable->val = NULL;\r
1899                                         }\r
1900                                         else\r
1901                                         {\r
1902                                                 WARN2("[%s] Variable `(%s)' already unseted",filepos, variable->name);\r
1903                                         }\r
1904                                 }       \r
1905                                 else\r
1906                                 {\r
1907                                         ERROR2("[%s] `(%s)' variable not found",filepos, name); \r
1908                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1909                                         xbt_os_mutex_release(unit->mutex);\r
1910                                         failure(unit);\r
1911                                         return;\r
1912                                 }\r
1913                         }\r
1914                         else if(env)\r
1915                         {\r
1916                                 ERROR2("[%s] `(%s)' is an environment variable use `unsetenv' instead `unset'",filepos, name);  \r
1917                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1918                                 xbt_os_mutex_release(unit->mutex);\r
1919                                 failure(unit);\r
1920                                 return;\r
1921                         }\r
1922                         else if(err)\r
1923                         {\r
1924                                 ERROR2("[%s] `(%s)' is system variable : you can unset it",filepos, name);      \r
1925                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1926                                 xbt_os_mutex_release(unit->mutex);\r
1927                                 failure(unit);\r
1928                                 return;\r
1929                         }\r
1930                         \r
1931                         xbt_os_mutex_release(unit->mutex);\r
1932                                 \r
1933                 }\r
1934                 else if(!strncmp(line,"set ", strlen("set ")))\r
1935                 {\r
1936                         char* val;\r
1937                         char name[PATH_MAX + 1] = {0};\r
1938                         unsigned int j; \r
1939                         int is_blank;\r
1940                         \r
1941                         val = strchr(line + strlen("set "), '=');\r
1942                         \r
1943                         if(val)\r
1944                         {\r
1945                                 variable_t variable;\r
1946                                 int exists = 0;\r
1947                                 unsigned int i;\r
1948                                 int err;\r
1949                                 int env;\r
1950 \r
1951                                 val++;\r
1952                                 \r
1953                                 \r
1954                                 /* syntax error */\r
1955                                 if(val[0] == '\0')\r
1956                                 {\r
1957                                         ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);\r
1958                                         \r
1959                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1960 \r
1961                                         failure(unit);\r
1962                                         return;\r
1963                                 }\r
1964                                 else if(val[0] ==' ' || val[0] =='\t')\r
1965                                 {\r
1966                                         strncpy(name, line + strlen("set "), (val - (line + strlen("set "))));\r
1967 \r
1968                                         ERROR2("[%s] No space avaible after`(%s)'", filepos, name);\r
1969 \r
1970                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1971 \r
1972                                         failure(unit);\r
1973                                         return;\r
1974                                 }\r
1975                                 \r
1976                                 \r
1977                                 /* assume it's a varibale */\r
1978                                 \r
1979                                 strncpy(name, line + strlen("set "), (val - (line + strlen("set ")) -1));\r
1980                                 \r
1981                                 is_blank = 1;\r
1982 \r
1983                                 for(j = 0; j < strlen(name); j++) \r
1984                                         if (name[j] != ' ' && name[j] != '\t')\r
1985                                                 is_blank = 0;\r
1986 \r
1987                                 if(is_blank)\r
1988                                 {\r
1989                                         \r
1990                                         ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);\r
1991                                         \r
1992                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
1993 \r
1994                                         failure(unit);\r
1995                                         return;\r
1996                                 }\r
1997 \r
1998                                 xbt_os_mutex_acquire(unit->mutex);\r
1999                                 \r
2000                                 /* test if the variable is already registred */\r
2001                                 xbt_dynar_foreach(unit->runner->variables, i, variable)\r
2002                                 {\r
2003                                         if(!strcmp(variable->name, name))\r
2004                                         {\r
2005                                                 exists = 1;\r
2006                                                 err = variable->err;\r
2007                                                 env = variable->env;\r
2008                                                 break;\r
2009                                         }\r
2010                                 }\r
2011                                 \r
2012                                 /* if the variable is already registred, update its value (if same value warns);\r
2013                                  * otherwise register it.\r
2014                                  */\r
2015                                 if(exists)\r
2016                                 {\r
2017                                         if(err)\r
2018                                         {\r
2019                                                 ERROR2("[%s] A system variable named `(%s)' already exists", filepos, name);\r
2020                                         \r
2021                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2022                                                 xbt_os_mutex_release(unit->mutex);\r
2023 \r
2024                                                 failure(unit);\r
2025                                                 return;\r
2026                                         }\r
2027                                         if(env)\r
2028                                         {\r
2029                                                 ERROR2("[%s] `(%s)' is an environment variable use `setenv' instead `set'", filepos, name);\r
2030                                         \r
2031                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2032                                                 xbt_os_mutex_release(unit->mutex);\r
2033 \r
2034                                                 failure(unit);\r
2035                                                 return;\r
2036                                         }\r
2037                                         else\r
2038                                         {\r
2039                                                 if(!strcmp(val, variable->val))\r
2040                                                         WARN3("[%s] Variable `(%s)' already contains value `<%s>'",filepos, variable->name, val);\r
2041 \r
2042                                                 free(variable->val);\r
2043                                                 variable->val = strdup(val);\r
2044                                         }\r
2045                                 }\r
2046                                 else\r
2047                                 {\r
2048                                         variable_t new_var = variable_new(name, val);\r
2049                                         xbt_dynar_push(unit->runner->variables, &new_var);\r
2050                                 }\r
2051 \r
2052                                         \r
2053                                 xbt_os_mutex_release(unit->mutex);\r
2054                         }\r
2055                         else\r
2056                         {\r
2057                                 ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);\r
2058                                         \r
2059                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2060                                 failure(unit);\r
2061                                 return;\r
2062                         }\r
2063                 }\r
2064                 else\r
2065                 {/* assume it's a variable */\r
2066                         char* val;\r
2067                         char name[PATH_MAX + 1] = {0};\r
2068                         unsigned int i, j; \r
2069                         int is_blank;\r
2070                         \r
2071                         val = strchr(line, '=');\r
2072 \r
2073                         if(val)\r
2074                         {\r
2075                                 variable_t variable;\r
2076                                 int exists = 0;\r
2077                                 int err;\r
2078                                 int env;\r
2079                                 val++;\r
2080                                 \r
2081                                 \r
2082                                 /* syntax error */\r
2083                                 if(val[0] == '\0')\r
2084                                 {\r
2085                                         strncpy(name, line, (val - line -1));\r
2086 \r
2087                                         is_blank = 1;\r
2088 \r
2089                                         for(j = 0; j < strlen(name); j++) \r
2090                                                 if (name[j] != ' ' && name[j] != '\t')\r
2091                                                         is_blank = 0;\r
2092 \r
2093                                         if(is_blank)\r
2094                                                 ERROR1("[%s] Bad usage of Tesh variable mechanism `(usage : variable=value)'", filepos);\r
2095                                         else if(!strcmp("setenv", name))\r
2096                                                 ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);\r
2097                                         else if(!strcmp("set", name))\r
2098                                                 ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);\r
2099                                         else\r
2100                                                 ERROR2("[%s] Undefined variable `(%s)'", filepos, name);\r
2101 \r
2102                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2103 \r
2104                                         failure(unit);\r
2105                                         return;\r
2106                                 }\r
2107                                 else if(val[0] ==' ' || val[0] =='\t')\r
2108                                 {\r
2109                                         strncpy(name, line, (val - line));\r
2110 \r
2111                                         ERROR2("[%s] No space avaible after`(%s)'", filepos, name);\r
2112 \r
2113                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2114 \r
2115                                         failure(unit);   \r
2116                                 }\r
2117                                 \r
2118                                 \r
2119                                 /* assume it's a varibale */\r
2120                                 \r
2121                                 strncpy(name, line, (val - line -1));\r
2122 \r
2123                                 is_blank = 1;\r
2124 \r
2125                                 for(j = 0; j < strlen(name); j++) \r
2126                                         if (name[j] != ' ' && name[j] != '\t')\r
2127                                                 is_blank = 0;\r
2128 \r
2129                                 if(is_blank)\r
2130                                 {\r
2131                                         \r
2132                                         ERROR1("[%s] Bad usage of Tesh variable capability `(usage : variable=value)'", filepos);\r
2133 \r
2134                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2135 \r
2136                                         failure(unit);\r
2137                                         return;\r
2138                                 }\r
2139                                 \r
2140                                 if(!strcmp("set", name))\r
2141                                 {\r
2142                                         ERROR1("[%s] Bad usage of the metacommand set `(usage : set variable=value)'", filepos);\r
2143                                         \r
2144                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2145                                         failure(unit);\r
2146                                         return;\r
2147                                 }\r
2148                                 else if(!strcmp("setenv", name))\r
2149                                 {\r
2150                                         ERROR1("[%s] Bad usage of the metacommand setenv `(usage : setenv variable=value)'", filepos);\r
2151                                         \r
2152                                         unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2153                                         failure(unit);\r
2154                                         return;\r
2155                                 }\r
2156 \r
2157                                 xbt_os_mutex_acquire(unit->mutex);\r
2158                                 \r
2159                                 /* test if the variable is already registred */\r
2160                                 xbt_dynar_foreach(unit->runner->variables, i, variable)\r
2161                                 {\r
2162                                         if(!strcmp(variable->name, name))\r
2163                                         {\r
2164                                                 exists = 1;\r
2165                                                 err = variable->err;\r
2166                                                 env = variable->env;\r
2167                                                 break;\r
2168                                         }\r
2169                                 }\r
2170                                 \r
2171                                 /* if the variable is already registred, update its value (if same value warns);\r
2172                                  * otherwise register it.\r
2173                                  */\r
2174                                 if(exists)\r
2175                                 {\r
2176                                         if(err)\r
2177                                         {\r
2178                                                 ERROR2("[%s] A system variable named `(%s)' already exists", filepos, name);\r
2179                                         \r
2180                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2181                                                 xbt_os_mutex_release(unit->mutex);\r
2182                                                 failure(unit);\r
2183                                                 return;\r
2184                                         }\r
2185                                         if(env)\r
2186                                         {\r
2187                                                 ERROR2("[%s] `(%s)' is an environment variable use `setenv' metacommand", filepos, name);\r
2188                                         \r
2189                                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2190                                                 xbt_os_mutex_release(unit->mutex);\r
2191 \r
2192                                                 failure(unit);\r
2193                                                 return;\r
2194                                         }\r
2195                                         else\r
2196                                         {\r
2197                                                 if(!strcmp(val, variable->val))\r
2198                                                         WARN3("[%s] Variable `(%s)' already contains value `<%s>'",filepos, variable->name, val);\r
2199 \r
2200                                                 free(variable->val);\r
2201                                                 variable->val = strdup(val);\r
2202                                         }\r
2203                                 }\r
2204                                 else\r
2205                                 {\r
2206                                         variable_t new_var = variable_new(name, val);\r
2207                                         xbt_dynar_push(unit->runner->variables, &new_var);\r
2208                                 }\r
2209 \r
2210                                         \r
2211                                 xbt_os_mutex_release(unit->mutex);\r
2212                                 \r
2213                         }\r
2214                         else \r
2215                         {\r
2216                                 if(!strncmp("setenv", line, strlen("setenv")))\r
2217                                         ERROR1("[%s] Bad usage of the metacommand setenv : `(usage : setenv variable=value)'", filepos);\r
2218                                 else if(!strncmp("set", line, strlen("set")))\r
2219                                         ERROR1("[%s] Bad usage of the metacommand set : `(usage : set variable=value)'", filepos);\r
2220                                 else if(!strncmp("unsetenv", line, strlen("unsetenv")))\r
2221                                         ERROR1("[%s] Bad usage of the metacommand unsetenv : `(usage : unsetenv variable)'", filepos);\r
2222                                 else if(!strncmp("unset", line, strlen("unset")))\r
2223                                         ERROR1("[%s] Bad usage of the metacommand unset : `(usage : unset variable)'", filepos);\r
2224                                 else if(!strncmp("timeout", line, strlen("timeout")))\r
2225                                         ERROR1("[%s] Bad usage of the metacommand timeout : `(usage : timeout <integral positive integer>)'", filepos);\r
2226                                 else if(!strncmp("expect signal", line, strlen("expect signal")))\r
2227                                         ERROR1("[%s] Bad usage of the metacommand expect signal : `(usage : expect signal <sig_name>)'", filepos);\r
2228                                 else if(!strncmp("expect return", line, strlen("expect return")))\r
2229                                         ERROR1("[%s] Bad usage of the metacommand expect return : `(usage : expect return <return value (>=0 <=255)>)'", filepos);\r
2230                                 else if(!strncmp("include", line, strlen("include")))\r
2231                                         ERROR1("[%s] Bad usage of the metacommand include  :`(usage : include <file> [<description>])'", filepos);\r
2232                                 else if(!strncmp("suite", line, strlen("suite")))\r
2233                                         ERROR1("[%s] Bad usage of the metacommand suite : `(usage : suite <description>)'", filepos);\r
2234                                 else\r
2235                                         ERROR2("[%s] Unknown metacommand: `%s'",filepos,line);\r
2236 \r
2237                                 unit_set_error(fstream->unit, ESYNTAX, 1, filepos);\r
2238 \r
2239                                 failure(unit);\r
2240                                 return;\r
2241                         }\r
2242                 }\r
2243                 \r
2244                 break;\r
2245         }\r
2246 }\r
2247 \r
2248 void\r
2249 fstream_handle_include(fstream_t fstream, context_t context, xbt_os_mutex_t mutex, const char* file_name, const char* description)\r
2250 {\r
2251         directory_t dir;\r
2252         char* prev_directory = NULL;\r
2253         fstream_t _fstream = NULL;\r
2254         struct stat buffer = {0};\r
2255         unit_t unit = fstream->unit;\r
2256         \r
2257         if(!stat(file_name, &buffer) && S_ISREG(buffer.st_mode))\r
2258         {\r
2259                 /* the file is in the current directory */\r
2260                 _fstream = fstream_new(getcwd(NULL, 0), file_name);\r
2261                 fstream_open(_fstream);\r
2262         }\r
2263         /* the file to include is not in the current directory, check if it is in a include directory */\r
2264         else\r
2265         {\r
2266                 unsigned int i;\r
2267                 prev_directory = getcwd(NULL, 0);\r
2268                 \r
2269                 xbt_dynar_foreach(include_dirs, i, dir)\r
2270                 {\r
2271                         chdir(dir->name);\r
2272                         \r
2273                         if(!stat(file_name, &buffer) && S_ISREG(buffer.st_mode))\r
2274                         {\r
2275                                 _fstream = fstream_new(dir->name, file_name);\r
2276                                 fstream_open(_fstream);\r
2277                                 break;\r
2278                         }\r
2279                 }\r
2280 \r
2281                 chdir(prev_directory);\r
2282                 free(prev_directory);\r
2283         }\r
2284         \r
2285         /* the file to include is not found handle the failure */\r
2286         if(!_fstream)\r
2287         {\r
2288                 if(file_name[0] == '$')\r
2289                 {\r
2290                         ERROR3("[%s] Include file `(%s)' not found or variable `(%s)' doesn't exist",context->line, file_name, file_name + 1);\r
2291                 \r
2292                 }\r
2293                 else\r
2294                 {\r
2295                         /* may be a variable */\r
2296                         variable_t variable;\r
2297                         int exists = 0;\r
2298                         unsigned int i;\r
2299 \r
2300                         xbt_dynar_foreach(unit->runner->variables, i, variable)\r
2301                         {\r
2302                                 if(!strcmp(variable->name, file_name))\r
2303                                 {\r
2304                                         exists = 1;\r
2305                                         break;\r
2306                                 }\r
2307                         }\r
2308 \r
2309                         if(exists)\r
2310                                 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
2311                         else\r
2312                                 ERROR2("[%s] Include file `(%s)' not found",context->line, file_name);\r
2313                 }\r
2314                 \r
2315                 unit_set_error(fstream->unit, EINCLUDENOTFOUND, 1, context->line);\r
2316 \r
2317                 failure(fstream->unit);\r
2318 \r
2319                 return;\r
2320         }\r
2321         else\r
2322         {\r
2323                 if(!unit->is_running_suite)\r
2324                 {/* it's the unit of a suite */\r
2325                         unit_t include = unit_new(unit->runner, unit->root, unit, _fstream);\r
2326                         \r
2327                         include->mutex = unit->root->mutex;\r
2328                 \r
2329                         if(description)\r
2330                                 include->description = strdup(description);\r
2331                 \r
2332                         xbt_dynar_push(unit->includes, &include);\r
2333                  \r
2334                         if(!dry_run_flag)\r
2335                         {\r
2336                                 if(description)\r
2337                                         INFO2("Include from %s (%s)", _fstream->name, description);\r
2338                                 else\r
2339                                         INFO1("Include from %s", _fstream->name);\r
2340 \r
2341                         }\r
2342                         else\r
2343                                 INFO1("Checking include %s...",_fstream->name);\r
2344                         \r
2345                         fstream_parse(_fstream, mutex);\r
2346                 }\r
2347                 else\r
2348                 {/* it's a include */\r
2349 \r
2350                         unit_t* owner;\r
2351                         unit_t include;\r
2352 \r
2353                         owner = xbt_dynar_get_ptr(unit->suites, xbt_dynar_length(unit->suites) - 1);\r
2354                         \r
2355                         include = unit_new(unit->runner, unit->root, *owner, _fstream);\r
2356                         \r
2357                         include->mutex = unit->root->mutex;\r
2358                         \r
2359                         if(description)\r
2360                                 include->description = strdup(description);\r
2361                 \r
2362                         xbt_dynar_push((*owner)->includes, &include);\r
2363                         \r
2364                         if(!dry_run_flag)\r
2365                         {\r
2366                                 if(description)\r
2367                                         INFO2("Include from %s (%s)", _fstream->name, description);\r
2368                                 else\r
2369                                         INFO1("Include from %s", _fstream->name);\r
2370                         }\r
2371                         else\r
2372                                 INFO1("Checking include %s...",_fstream->name);\r
2373                         \r
2374                         fstream_parse(_fstream, mutex);\r
2375                 }\r
2376         }\r
2377 }\r
2378 \r
2379 void\r
2380 fstream_handle_suite(fstream_t fstream, const char* description, const char* filepos)\r
2381 {\r
2382         unit_t unit = fstream->unit;\r
2383         unit_t suite = unit_new(unit->runner, unit->root, unit, NULL);\r
2384         \r
2385         if(description)\r
2386                 suite->description = strdup(description);\r
2387 \r
2388         suite->filepos = strdup(filepos); \r
2389 \r
2390         xbt_dynar_push(unit->suites, &suite);\r
2391         unit->is_running_suite = 1;\r
2392         \r
2393         if(!dry_run_flag)\r
2394                 INFO1("Test suite %s", description);\r
2395         else\r
2396                 INFO1("Checking suite %s...",description);\r
2397         \r
2398 }\r
2399 \r
2400 int\r
2401 fstream_launch_command(fstream_t fstream, context_t context, xbt_os_mutex_t mutex)\r
2402 {\r
2403         unit_t unit = fstream->unit;\r
2404 \r
2405         if(!dry_run_flag)\r
2406         {\r
2407                 command_t command;\r
2408                 \r
2409                 if(!(command = command_new(unit, context, mutex)))\r
2410                 {\r
2411                         if(EINVAL == errno)\r
2412                         {\r
2413                                 ERROR3("[%s] Cannot instantiate the command `%s' (%d)",context->pos, strerror(errno), errno);   \r
2414 \r
2415                                 unit_set_error(unit, errno, 0, context->pos);\r
2416                                 failure(unit);\r
2417                                 return -1;\r
2418                         }\r
2419                         else if(ENOMEM == errno)\r
2420                         {\r
2421                                 ERROR3("[%s] Cannot instantiate the command `%s' (%d)",context->pos, strerror(errno), errno);\r
2422 \r
2423                                 unit_set_error(unit, errno, 0, context->pos);\r
2424 \r
2425                                 failure(unit);\r
2426                                 return -1;\r
2427                         }\r
2428                 }\r
2429                 \r
2430                 if(command_run(command) < 0)\r
2431                 {\r
2432                         ERROR3("[%s] Cannot run the command `%s' (%d)",context->pos, strerror(errno), errno);   \r
2433                         unit_set_error(unit, errno, 0, context->pos);\r
2434                         failure(unit);\r
2435                         return -1;      \r
2436                 }\r
2437         }\r
2438         \r
2439         if(context_reset(context) < 0)\r
2440         {\r
2441                 ERROR3("[%s] Cannot reset the context of the command `%s' (%d)",context->pos, strerror(errno), errno);  \r
2442 \r
2443                 unit_set_error(fstream->unit, errno, 0, context->pos);\r
2444 \r
2445                 failure(unit);\r
2446                 return -1;      \r
2447         }\r
2448         \r
2449         return 0;\r
2450 }\r
2451 \r
2452 \r
2453 \r
2454 \r