3 extern const char* TOKENS = "$<>#!p&";
7 const char * __metacommandlist[ ] =
10 "enable output checking",
11 "disable output checking",
12 "enable post output checking",
13 "disable post output checking",
19 "enable exit code checking",
20 "disable exit code checking",
26 * Create a new s_Stream struct and return
30 Stream_t Stream_new(void)
32 Stream_t ptr = (Stream_t)calloc(1,sizeof(s_Stream_t));
36 setErrno(E_STREAM_ALLOCATION_FAILED);
40 memset(&cs,0,sizeof(CRITICAL_SECTION)) ;
41 InitializeCriticalSection(&cs);
50 * Returns true if the current text line is blank.
52 bool Stream_lineIsBlank(Stream_t stream)
55 char* p = (char*)stream->line;
59 if(!Stream_isBlankChar(p[i]))
69 * Return true if the caracter is space or tab.
71 bool Stream_isBlankChar(char ch)
73 return ((ch ==' ') || ch == ('\t'));
77 * Return E_SUCCESS if the file is valid.
78 * Otherwise the fuction returns E_INVALID_FILE.
80 errno_t Stream_isValidFile(const char* file_name)
82 WIN32_FIND_DATA wfd ={0};
83 HANDLE hFile = FindFirstFile(file_name,&wfd);
85 if(INVALID_HANDLE_VALUE == hFile)
86 return E_FILE_NOT_FOUND;
93 * Return E_SUCCESS is the open file operation succeeded.
94 * Otherwise the functions returns E_OPEN_FILE_FAILED.
96 errno_t Stream_openFile(Stream_t ptr,const char* file_name)
98 ptr->file = fopen(file_name,"r");
100 if(NULL == ptr->file)
102 setErrno(E_OPEN_FILE_FAILED);
110 * This function reads an entire line, storing
111 * the address of the buffer containing the text into
114 ssize_t Stream_getLine(Stream_t stream)
116 size_t capacity_available; /* capacity available in the buffer */
117 char* pos; /* read operation position */
118 ssize_t size; /* the size of the text line (minus the 0 terminal */
119 static size_t len = 0;
120 register int ch; /* the current character */
121 FILE* file = stream->file;
125 setErrno(E_INVALID_FILE_Stream);
129 if(NULL == stream->line)
131 len = DEFAULT_ALLOC_SIZE;
132 stream->line = (char*)calloc(1,len);
134 if (NULL == stream->line)
136 setErrno(E_STREAM_LINE_ALLOCATION_FAILED);
142 memset(stream->line,0,len);
145 capacity_available = len ;
152 /* un byte for the next char and one byte for the zero terminal. */
153 if (capacity_available < 2)
155 if (len > DEFAULT_ALLOC_SIZE)
158 len += DEFAULT_ALLOC_SIZE;
160 capacity_available =stream->line + len - pos;
161 stream->line = realloc (stream->line,len);
163 if (NULL ==stream->line)
165 setErrno(E_STREAM_LINE_REALLOCATION_FAILED);
169 pos = stream->line + len - capacity_available ;
174 /* file error exit on error*/
175 setErrno(E_STREAM_ERROR);
182 if(!strlen(stream->line) && !stream->line_number)
184 setErrno(E_STREAM_EMPTY);
188 else if(!strlen(stream->line) && stream->line_number)
197 capacity_available--;
199 /* we have a line, exit loop */
204 /* append the zero terminal */
207 size = pos - stream->line;
209 stream->line_number++;
211 /* size of texte line without zero terminal */
220 void Stream_free(Stream_t ptr)
225 if((NULL !=ptr->file) && (stdin != ptr->file))
228 if(NULL != ptr->line)
231 DeleteCriticalSection(&cs);
237 * Return true if the current line is a comment.
238 * Otherwise the functions returns false.
240 bool Stream_lineIsComment(Stream_t stream)
242 return stream->line[0] == '#';
245 /* Return true if the current line contains a invalide token.
246 * Otherwise, the function returns false.
248 bool Stream_lineContainsInvalidToken(Stream_t stream)
250 if(strchr(TOKENS,stream->line[0]) == NULL)
252 Stream_printLine(stream,invalid_token_line_type);
253 setErrno(E_INVALID_TOKEN);
261 * Return true if the text line is a meta command.
262 * Otherwise, the functions returns false.
264 bool Stream_lineIsMetacommand(Stream_t stream)
266 return stream->line[0] == '!';
269 /* Retun true if the text line contains a unknown meta command.
270 * Otherwise the function returns false.
272 bool Stream_lineIsUnknwnMetaCommand(Stream_t stream)
275 while(__metacommandlist[i])
277 if(!strncmp(__metacommandlist[i],stream->line + 2,strlen(__metacommandlist[i])))
282 Stream_printLine(stream,unknwn_meta_command_line_type);
284 setErrno(E_UNKWN_META_COMMAND);
289 * Return true if the text line contains a invalid
290 * meta command. Otherwise the function returns false.
292 bool Stream_lineIsInvalidMetaCommand(Stream_t stream)
294 if(!strncmp("set timeout ",stream->line + 2,strlen("set timeout ")))
296 return Stream_isInvalidTimeout(stream);
298 else if(!strncmp("command line ",stream->line + 2,strlen("command line ")))
300 Stream_printLine(stream,command_line_line_type);
302 else if(!strncmp("enable output checking",stream->line + 2,strlen("enable output checking")))
304 Stream_printLine(stream,enable_output_checking_line_type);
306 else if(!strncmp("disable output checking",stream->line + 2,strlen("disable output checking")))
308 Stream_printLine(stream,disable_output_checking_line_type);
310 else if(!strncmp("enable post output checking",stream->line + 2,strlen("enable post output checking")))
312 Stream_printLine(stream,enable_post_output_checking_line_type);
314 else if(!strncmp("disable post output checking",stream->line + 2,strlen("disable post output checking")))
316 Stream_printLine(stream,disable_post_output_checking_line_type);
318 else if(!strncmp("expect exit code ",stream->line + 2,strlen("expect exit code ")))
320 return Stream_isInvalidExpectedCode(stream);
322 else if(!strncmp("export ",stream->line + 2,strlen("export ")))
324 return Stream_isInvalidExport(stream);
326 else if(!strncmp("unset ",stream->line + 2,strlen("unset ")))
328 return Stream_isInvalidUnset(stream);
330 else if(!strncmp("create console",stream->line + 2,strlen("create console")))
332 Stream_printLine(stream,create_console_line_type);
334 else if(!strncmp("create no console",stream->line + 2,strlen("create no console")))
336 Stream_printLine(stream,create_no_console_line_type);
338 else if(!strncmp("enable exit code checking",stream->line + 2,strlen("enable exit code checking")))
340 Stream_printLine(stream,enable_exit_code_checking_line_type);
342 else if(!strncmp("disable exit code checking",stream->line + 2,strlen("disaable exit code checking")))
344 Stream_printLine(stream,disable_exit_code_checking_line_type);
358 * Print the file line.
360 void Stream_printLine(Stream_t stream,line_type_t line_type)
363 __date = (char*)calloc(1,30);
373 case comment_line_type:
375 if(*(stream->line+2) != '\0')
376 printf("%s <COMMENT > %3d %s",__date,stream->line_number,stream->line + 2);
379 printf("%s <COMMENT > %3d %s",__date,stream->line_number," \n");
382 case timeout_value_line_type:
383 printf("%s <TIMEOUT VALUE IS NOW > %3d %s",__date,stream->line_number,stream->line + 2 + strlen("set timeout "));
386 case exit_code_line_type:
387 printf("%s <EXPECTED EXIT CODE > %3d %s",__date,stream->line_number,stream->line + 2 + strlen("expect exit code "));
390 case export_line_type:
391 printf("%s <EXPORT > %3d %s",__date,stream->line_number,stream->line + 2);
394 case unset_line_type:
395 printf("%s <UNSET > %3d %s",__date,stream->line_number,stream->line + 2);
398 case enable_output_checking_line_type:
399 printf("%s <OUTPUT CHECKING ENABLED > %3d\n",__date,stream->line_number);
402 case disable_output_checking_line_type:
403 printf("%s <OUTPUT CHECKING DISABLED > %3d\n",__date,stream->line_number);
406 case enable_post_output_checking_line_type:
407 printf("%s <POST OUTPUT CHECKING ENABLED> %3d\n",__date,stream->line_number);
410 case disable_post_output_checking_line_type:
411 printf("%s <POST OUTPUT CHECKING DISABLED> %3d\n",__date,stream->line_number);
414 case create_console_line_type:
415 printf("%s <CREATE CONSOLE SELECTED > %3d\n",__date,stream->line_number);
418 case create_no_console_line_type:
419 printf("%s <CREATE NO CONSOLE SELECTED > %3d\n",__date,stream->line_number);
422 case enable_exit_code_checking_line_type:
423 printf("%s <EXIT CODE CHECKING ENABLED > %3d\n",__date,stream->line_number);
426 case disable_exit_code_checking_line_type:
427 printf("%s <EXIT CODE CHECKING DISABLED > %3d\n",__date,stream->line_number);
430 case change_directory_line_type:
431 printf("%s <DIRECTORY IS NOW > %3d %s\n",__date,stream->line_number,stream->line + 5);
434 case command_line_line_type:
435 printf("%s <COMMAND LINE > %3d %s",__date,stream->line_number,stream->line + 2);
438 #endif /* #ifdef __VERBOSE */
440 case invalid_token_line_type:
441 printf("%s <INVALIDE TOKEN > %3d %s",__date,stream->line_number,stream->line);
444 case unknwn_meta_command_line_type:
445 printf("%s <UNKNOWN META COMMAND > %3d %s",__date,stream->line_number,stream->line);
448 case invalid_timeout_value_line_type:
449 printf("%s <INVALID TIMEOUT VALUE > %3d %s",__date,stream->line_number,stream->line);
452 case invalid_exit_code_line_type:
453 printf("%s <INVALID EXIT CODE > %3d %s",__date,stream->line_number,stream->line);
456 case invalid_export_line_type:
457 printf("%s <INVALID EXPORT > %3d %s",__date,stream->line_number,stream->line);
460 case invalid_unset_line_type:
461 printf("%s <INVALID UNSET > %3d %s",__date,stream->line_number,stream->line);
464 case export_failed_line_type:
465 printf("%s <EXPORT FAILED > %3d %s",__date,stream->line_number,stream->line);
468 case unset_failed_line_type:
469 printf("%s <UNSET FAILED > %3d %s",__date,stream->line_number,stream->line);
480 Stream_unlock(stream);
485 * Returns true if the timeout value is invalid.
486 * Otherwise the function returns false.
488 bool Stream_isInvalidTimeout(Stream_t stream)
491 char* p = stream->line + 2 + strlen("set timeout ");
497 setErrno(E_INVALID_TIMEOUT_VALUE);
498 Stream_printLine(stream,invalid_timeout_value_line_type);
505 Stream_printLine(stream,timeout_value_line_type);
511 * Returns true if the expected code value is invalid.
512 * Otherwise the function returns false.
514 bool Stream_isInvalidExpectedCode(Stream_t stream)
517 char* p = stream->line + 2 + strlen("expect exit code ");
523 setErrno(E_INVALID_EXIT_CODE_VALUE);
524 Stream_printLine(stream,invalid_exit_code_line_type);
530 Stream_printLine(stream,exit_code_line_type);
536 * Returns true if the export is invalid.
537 * Otherwise the function returns false.
539 bool Stream_isInvalidExport(Stream_t stream)
542 const char* ptr = strchr(stream->line,'=');
544 if(ptr && (*(++ptr) != '\n'))
546 Stream_printLine(stream,export_line_type);
550 setErrno(E_INVALID_EXPORT);
551 Stream_printLine(stream,invalid_export_line_type);
556 * Returns true if the unset is invalid.
557 * Otherwise the function returns false.
559 bool Stream_isInvalidUnset(Stream_t stream)
562 const char* ptr = strchr(stream->line,' ');
564 if((*(++ptr) != '\n'))
566 Stream_printLine(stream,unset_line_type);
570 setErrno(E_INVALID_UNSET);
571 Stream_printLine(stream,invalid_unset_line_type);
579 * Return true if the stream line contains a
580 * expected child output. Otherwhise the function
584 bool Stream_lineIsExpectedChildOutput(Stream_t stream)
586 return stream->line[0] == '>';
590 * Return true if the stream line contains a
591 * child input. Otherwhise the function
594 bool Stream_lineIsChildInput(Stream_t stream)
596 return stream->line[0] == '<';
602 * Return true, if the stream line containts a
603 * synchrone test case. otherwise the function
606 bool Stream_lineIsSyncTestCase(Stream_t stream)
608 return stream->line[0] == '$';
611 bool Stream_lineIsAsyncTestCase(Stream_t stream)
613 return stream->line[0] == '&';
616 bool Stream_lineIsChangeDir(Stream_t stream)
618 return ((stream->line[0] == '$') && (!strncmp(stream->line+2,"cd ",strlen("cd "))));
621 void Stream_lock(Stream_t ptr)
623 EnterCriticalSection(&cs);
626 void Stream_unlock(Stream_t ptr)
628 LeaveCriticalSection(&cs);