/* Copyright 2005-2006 Garrett Rooney. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "etl_template.h" #include "etl_data.h" #include #include #ifndef APR_ARRAY_PUSH #define APR_ARRAY_PUSH(ary,type) (*((type *)apr_array_push(ary))) #endif #ifndef APR_FPROT_OS_DEFAULT #define APR_FPROT_OS_DEFAULT APR_OS_DEFAULT #endif int main (int argc, char *argv[]) { const char *etl_path, *ddl_path, *out_path, *inc_path; apr_array_header_t *search_path; apr_bucket_alloc_t *alloc; etl_resolver_t *resolver; apr_bucket_brigade *out; apr_hash_t *environment; etl_template_t *tmpl; apr_status_t apr_err; etl_error_t *err; apr_pool_t *pool; apr_initialize (); atexit (apr_terminate); if (argc != 2) { fprintf (stdout, "usage: compare \n"); return 1; } apr_pool_create (&pool, NULL); etl_path = apr_psprintf (pool, "%s.etl", argv[1]); ddl_path = apr_psprintf (pool, "%s.ddl", argv[1]); out_path = apr_psprintf (pool, "%s.out", argv[1]); inc_path = apr_pstrdup (pool, argv[1]); search_path = apr_array_make (pool, 1, sizeof (char *)); *strrchr(inc_path, '/') = '\0'; APR_ARRAY_PUSH (search_path, const char *) = inc_path; resolver = etl_resolver_file_create (search_path, pool); err = etl_ddl_parse_file (&environment, resolver, ddl_path, pool); if (err) goto cleanup; alloc = apr_bucket_alloc_create (pool); out = apr_brigade_create (pool, alloc); err = etl_template_parse_file (&tmpl, etl_path, pool); if (err) goto cleanup; err = etl_template_execute (tmpl, resolver, environment, out, pool); if (err) goto cleanup; { apr_size_t nread, datalen; char *data, *buffer; apr_finfo_t finfo; apr_off_t outlen; apr_file_t *file; apr_err = apr_file_open (&file, out_path, APR_READ, APR_FPROT_OS_DEFAULT, pool); if (apr_err) goto cleanup; apr_err = apr_file_info_get (&finfo, APR_FINFO_SIZE, file); if (apr_err) goto cleanup; buffer = apr_pcalloc (pool, finfo.size + 1); apr_err = apr_file_read_full (file, buffer, finfo.size, &nread); if (apr_err) goto cleanup; apr_err = apr_brigade_length (out, 1, &outlen); if (apr_err) goto cleanup; apr_err = apr_brigade_pflatten (out, &data, &datalen, pool); if (apr_err) goto cleanup; if (memcmp (buffer, data, datalen) != 0) { fprintf (stdout, "error: output of %s differs in content from %s\n" " got '%s'\n" " expected '%s'\n", etl_path, out_path, data, buffer); return 1; } } cleanup: if (err) { char buffer[1024] = { 0 }; fprintf (stdout, "error: %s\n" " %s\n" "thrown from %s:%d\n", err->msg, apr_strerror(err->err, buffer, sizeof(buffer)), err->file, err->line); return 1; } else if (apr_err) { char buffer[1024] = { 0 }; fprintf (stdout, "error: %s\n", apr_strerror (apr_err, buffer, sizeof (buffer))); return 1; } apr_brigade_destroy (out); apr_bucket_alloc_destroy (alloc); apr_pool_destroy (pool); return 0; }