/* Copyright (c) 2008 The NetBSD Foundation, Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */2425#include "atf-c/tp.h"2627#include <stdio.h>28#include <stdlib.h>29#include <string.h>30#include <unistd.h>3132#include "atf-c/detail/fs.h"33#include "atf-c/detail/map.h"34#include "atf-c/detail/sanity.h"35#include "atf-c/error.h"36#include "atf-c/tc.h"3738struct atf_tp_impl {39atf_list_t m_tcs;40atf_map_t m_config;41};4243/* ---------------------------------------------------------------------44* Auxiliary functions.45* --------------------------------------------------------------------- */4647static48const atf_tc_t *49find_tc(const atf_tp_t *tp, const char *ident)50{51const atf_tc_t *tc;52atf_list_citer_t iter;5354tc = NULL;55atf_list_for_each_c(iter, &tp->pimpl->m_tcs) {56const atf_tc_t *tc2;57tc2 = atf_list_citer_data(iter);58if (strcmp(atf_tc_get_ident(tc2), ident) == 0) {59tc = tc2;60break;61}62}63return tc;64}6566/* ---------------------------------------------------------------------67* The "atf_tp" type.68* --------------------------------------------------------------------- */6970/*71* Constructors/destructors.72*/7374atf_error_t75atf_tp_init(atf_tp_t *tp, const char *const *config)76{77atf_error_t err;7879PRE(config != NULL);8081tp->pimpl = malloc(sizeof(struct atf_tp_impl));82if (tp->pimpl == NULL)83return atf_no_memory_error();8485err = atf_list_init(&tp->pimpl->m_tcs);86if (atf_is_error(err))87goto out;8889err = atf_map_init_charpp(&tp->pimpl->m_config, config);90if (atf_is_error(err)) {91atf_list_fini(&tp->pimpl->m_tcs);92goto out;93}9495INV(!atf_is_error(err));96out:97return err;98}99100void101atf_tp_fini(atf_tp_t *tp)102{103atf_list_iter_t iter;104105atf_map_fini(&tp->pimpl->m_config);106107atf_list_for_each(iter, &tp->pimpl->m_tcs) {108atf_tc_t *tc = atf_list_iter_data(iter);109atf_tc_fini(tc);110}111atf_list_fini(&tp->pimpl->m_tcs);112113free(tp->pimpl);114}115116/*117* Getters.118*/119120char **121atf_tp_get_config(const atf_tp_t *tp)122{123return atf_map_to_charpp(&tp->pimpl->m_config);124}125126bool127atf_tp_has_tc(const atf_tp_t *tp, const char *id)128{129const atf_tc_t *tc = find_tc(tp, id);130return tc != NULL;131}132133const atf_tc_t *134atf_tp_get_tc(const atf_tp_t *tp, const char *id)135{136const atf_tc_t *tc = find_tc(tp, id);137PRE(tc != NULL);138return tc;139}140141const atf_tc_t *const *142atf_tp_get_tcs(const atf_tp_t *tp)143{144const atf_tc_t **array;145atf_list_citer_t iter;146size_t i;147148array = malloc(sizeof(atf_tc_t *) *149(atf_list_size(&tp->pimpl->m_tcs) + 1));150if (array == NULL)151goto out;152153i = 0;154atf_list_for_each_c(iter, &tp->pimpl->m_tcs) {155array[i] = atf_list_citer_data(iter);156if (array[i] == NULL) {157free(array);158array = NULL;159goto out;160}161162i++;163}164array[i] = NULL;165166out:167return array;168}169170/*171* Modifiers.172*/173174atf_error_t175atf_tp_add_tc(atf_tp_t *tp, atf_tc_t *tc)176{177atf_error_t err;178179PRE(find_tc(tp, atf_tc_get_ident(tc)) == NULL);180181err = atf_list_append(&tp->pimpl->m_tcs, tc, false);182183POST(find_tc(tp, atf_tc_get_ident(tc)) != NULL);184185return err;186}187188/* ---------------------------------------------------------------------189* Free functions.190* --------------------------------------------------------------------- */191192atf_error_t193atf_tp_run(const atf_tp_t *tp, const char *tcname, const char *resfile)194{195const atf_tc_t *tc;196197tc = find_tc(tp, tcname);198PRE(tc != NULL);199200return atf_tc_run(tc, resfile);201}202203atf_error_t204atf_tp_cleanup(const atf_tp_t *tp, const char *tcname)205{206const atf_tc_t *tc;207208tc = find_tc(tp, tcname);209PRE(tc != NULL);210211return atf_tc_cleanup(tc);212}213214215