18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * This file is part of ltrace.
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2011 Petr Machata, Red Hat Inc.
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * This program is free software; you can redistribute it and/or
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * modify it under the terms of the GNU General Public License as
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * published by the Free Software Foundation; either version 2 of the
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * License, or (at your option) any later version.
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * This program is distributed in the hope that it will be useful, but
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT ANY WARRANTY; without even the implied warranty of
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * General Public License for more details.
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You should have received a copy of the GNU General Public License
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * along with this program; if not, write to the Free Software
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 02110-1301 USA
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include <assert.h>
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "lens.h"
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "lens_default.h"
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "type.h"
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "value.h"
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comformat_argument(FILE *stream, struct value *value,
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		struct value_dict *arguments)
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	/* Find the closest enclosing parental value whose type has a
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	 * lens assigned.  */
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	struct value *parent;
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	for (parent = value; (parent != NULL && parent->type != NULL
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com			      && parent->type->lens == NULL);
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	     parent = parent->parent)
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		;
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	struct lens *lens = &default_lens;
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	if (parent != NULL && parent->type != NULL
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	    && parent->type->lens != NULL)
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		lens = parent->type->lens;
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	return lens_format(lens, stream, value, arguments);
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comlens_format(struct lens *lens, FILE *stream, struct value *value,
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	    struct value_dict *arguments)
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	assert(lens->format_cb != NULL);
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	return lens->format_cb(lens, stream, value, arguments);
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comlens_destroy(struct lens *lens)
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	if (lens != NULL
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com	    && lens->destroy_cb != NULL)
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com		lens->destroy_cb(lens);
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com