7 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
10 fstream_new(const char* directory, const char* name)
13 /*struct stat buffer = {0};*/
21 if(!directory && !strcmp("stdin", name))
23 fstream = xbt_new0(s_fstream_t, 1);
24 fstream->name = strdup("stdin");
33 /*if(stat(name, &buffer))
36 if(!S_ISREG(buffer.st_mode))
42 fstream = xbt_new0(s_fstream_t, 1);
44 fstream->name = strdup(name);
46 fstream->directory = strdup(directory);
47 fstream->stream = NULL;
54 fstream_open(fstream_t fstream)
56 if(!fstream || fstream->stream)
59 if(!strcmp(fstream->name, "stdin"))
61 fstream->stream = stdin;
65 if(!(fstream->stream = fopen(fstream->name, "r")))
72 fstream_close(fstream_t fstream)
74 if(!fstream || !strcmp(fstream->name, "stdin"))
80 fclose(fstream->stream);
81 fstream->stream = NULL;
86 fstream_free(void** fstreamptr)
91 if((*((fstream_t*)fstreamptr))->stream)
92 fclose((*((fstream_t*)fstreamptr))->stream);
94 free((*((fstream_t*)fstreamptr))->name);
96 if((*((fstream_t*)fstreamptr))->directory)
97 free((*((fstream_t*)fstreamptr))->directory);
108 fstream_parse( fstream_t fstream, unit_t unit)
117 xbt_os_mutex_t mutex = unit->mutex;
119 /* Count the line length while checking wheather it's blank */
122 /* Deal with \ at the end of the line, and call handle_line on result */
125 buff=xbt_strbuff_new();
126 context = context_new();
128 while(!unit->interrupted && getline(&line, &len, fstream->stream) != -1)
136 while(line[linelen] != '\0')
138 if (line[linelen] != ' ' && line[linelen] != '\t' && line[linelen]!='\n' && line[linelen]!='\r')
146 if(!context->command_line && (context->input->used || context->output->used))
148 ERROR1("[%d] Error: no command found in this chunk of lines.",buffbegin);
150 if(unit->parsing_include_file)
151 ERROR1("Unit `%s': NOK (syntax error)", fstream->name);
153 ERROR2("Unit `%s' inclued in `%s' : NOK (syntax error)", fstream->name, fstream->name);
156 unit_handle_failure(unit);
160 if(context->command_line)
164 command_t command = command_new(unit, context, mutex);
165 command_run(command);
168 context_reset(context);
176 if(linelen>1 && line[linelen-2]=='\\')
178 if (linelen>2 && line[linelen-3] == '\\')
180 /* Damn. Escaped \ */
181 line[linelen-2] = '\n';
182 line[linelen-1] = '\0';
187 line[linelen-2] = '\0';
191 buffbegin = line_num;
195 if(buff->used || to_be_continued)
197 xbt_strbuff_append(buff,line);
199 if (!to_be_continued)
201 snprintf(file_pos,256,"%s:%d",fstream->name, buffbegin);
202 unit_handle_line(unit, context, mutex, file_pos, buff->data);
203 xbt_strbuff_empty(buff);
208 snprintf(file_pos,256,"%s:%d",fstream->name, line_num);
209 unit_handle_line(unit, context, mutex, file_pos, line);
213 /* Check that last command of the file ran well */
214 if(context->command_line)
218 command_t command = command_new(unit, context, mutex);
219 command_run(command);
222 context_reset(context);
229 xbt_strbuff_free(buff);