ali@0: #include ali@0: #include ali@0: #include ali@6: #include ali@5: #include ali@0: #include "testcaseparser.h" ali@9: #include "testcaseinput.h" ali@0: #include "testcaseio.h" ali@17: #include "warningsparser.h" ali@0: ali@0: /* ali@0: * Read a testcase in from a file. ali@11: * On error, print a suitable message using g_printerr and return NULL. ali@0: * The returned testcase should be freed with testcase_free(). ali@0: */ ali@0: Testcase *testcase_parse_file(const char *filename) ali@0: { ali@0: Testcase *testcase; ali@0: TestcaseParser *parser; ali@9: TestcaseInput *input=NULL; ali@17: GMarkupParseContext *context; ali@17: GError *err=NULL; ali@9: char *s,*arg; ali@0: const char *tag,*text; ali@101: gboolean found_tag=FALSE,newlines_set=FALSE; ali@0: parser=testcase_parser_new_from_file(filename); ali@0: if (!parser) ali@0: return NULL; ali@0: if (!*testcase_parser_get_flag(parser)) ali@0: { ali@11: g_printerr("%s: Not a valid testcase (flag)\n",filename); ali@0: testcase_parser_free(parser); ali@0: return NULL; ali@0: } ali@6: testcase=g_new0(Testcase,1); ali@6: testcase->basename=g_path_get_basename(filename); ali@0: s=strrchr(testcase->basename,'.'); ali@0: if (s) ali@0: *s='\0'; ali@0: while(testcase_parser_get_next_tag(parser,&tag,&text)) ali@0: { ali@9: if (!input && !strcmp(tag,"INPUT")) ali@9: input=testcase_input_new("TEST-XXXXXX",text); ali@9: else if (g_str_has_prefix(tag,"INPUT(") && tag[strlen(tag)-1]==')') ali@9: { ali@9: arg=g_strndup(tag+6,strlen(tag)-7); ali@9: s=g_path_get_dirname(arg); ali@9: if (strcmp(s,".")) ali@9: { ali@9: /* ali@9: * Otherwise it would be possible to overwrite an arbitary ali@9: * file on somebody's computer by getting them to run a ali@9: * testcase! ali@9: */ ali@11: g_printerr( ali@9: "%s: Input files may not have a directory component\n",arg); ali@9: g_free(s); ali@9: g_free(arg); ali@9: testcase_free(testcase); ali@9: testcase_parser_free(parser); ali@9: return NULL; ali@9: } ali@9: g_free(s); ali@9: testcase->inputs=g_slist_prepend(testcase->inputs, ali@9: testcase_input_new(arg,text)); ali@9: if (!strstr(arg,"XXXXXX")) ali@9: testcase->flags|=TESTCASE_TMP_DIR; ali@9: g_free(arg); ali@9: } ali@17: else if (!testcase->expected && !testcase->warnings && ali@17: !strcmp(tag,"EXPECTED")) ali@6: testcase->expected=g_strdup(text); ali@17: else if (!testcase->expected && !testcase->warnings && ali@17: !strcmp(tag,"WARNINGS")) ali@17: { ali@17: context=warnings_parse_context_new(testcase); ali@17: if (!g_markup_parse_context_parse(context,text,-1,&err) || ali@17: !g_markup_parse_context_end_parse(context,&err)) ali@17: { ali@17: g_markup_parse_context_free(context); ali@17: g_printerr("%s\n",err->message); ali@17: g_clear_error(&err); ali@17: testcase_free(testcase); ali@17: testcase_parser_free(parser); ali@17: return NULL; ali@17: } ali@17: g_markup_parse_context_free(context); ali@17: } ali@7: else if (!testcase->encoding && !strcmp(tag,"ENCODING")) ali@8: testcase->encoding=g_strchomp(g_strdup(text)); ali@101: else if (!newlines_set && !strcmp(tag,"NEWLINES")) ali@101: { ali@101: newlines_set=TRUE; ali@101: s=g_strdup(text); ali@101: g_strchomp(s); ali@101: if (!strcmp(s,"LF")) ali@101: testcase->flags|=TESTCASE_UNIX_NEWLINES; ali@101: else if (!strcmp(s,"CR")) ali@101: testcase->flags|=TESTCASE_OS9_NEWLINES; ali@101: else if (strcmp(s,"CRLF")) ali@101: { ali@101: g_printerr( ali@101: "%s: Unrecognised style for newlines. Try CR or LF.\n",s); ali@101: g_free(s); ali@101: testcase_free(testcase); ali@101: testcase_parser_free(parser); ali@101: return NULL; ali@101: } ali@101: g_free(s); ali@101: } ali@9: else if (!testcase->encoding && !strcmp(tag,"OPTIONS")) ali@9: { ali@9: testcase->options=g_strsplit(text,"\n",0); ali@9: g_free(testcase->options[g_strv_length(testcase->options)-1]); ali@9: testcase->options[g_strv_length(testcase->options)-1]=NULL; ali@9: } ali@0: else ali@0: { ali@11: g_printerr("%s: Not a valid testcase (%s)\n",filename,tag); ali@0: testcase_free(testcase); ali@0: testcase_parser_free(parser); ali@0: return NULL; ali@0: } ali@0: found_tag=TRUE; ali@0: } ali@0: if (!testcase_parser_at_eof(parser)) ali@0: { ali@0: if (found_tag) ali@11: g_printerr("%s: Not a valid testcase (garbage at end)\n", ali@0: filename); ali@0: else ali@11: g_printerr("%s: Not a valid testcase (no valid tags)\n", ali@0: filename); ali@0: testcase_free(testcase); ali@0: testcase_parser_free(parser); ali@0: return NULL; ali@0: } ali@9: if (!input) ali@9: input=testcase_input_new("TEST-XXXXXX",NULL); ali@9: testcase->inputs=g_slist_prepend(testcase->inputs,input); ali@0: testcase_parser_free(parser); ali@0: return testcase; ali@0: }