/* 
 *      Copyright (c) 2000 Paul Campbell/VeriFarm Inc
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
                    

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "acc_user.h"

static void
dump_hier(handle h, int depth, int type)
{
	handle h2; 
	static int list[] = {accPrimitive, accModule, accStatement, accTask, accFunction, 0};
	char *cp;
	int i, t;

	t = acc_fetch_type(h);
	switch(t) {
	case accPrimitive:
		for (i = 0; i < depth; i++)
			printf(". ");
		printf("%s:%s\n", acc_fetch_name(h), ((cp=acc_fetch_defname(h))!=NULL?cp:"?"));
		break;
	case accModule:
		for (i = 0; i < depth; i++)
			printf(". ");
		printf("%s:%s\n", acc_fetch_name(h), acc_fetch_defname(h));
		h2 = NULL;
		for (h2 = acc_next(list, h, h2); h2; h2 = acc_next(list, h, h2))
			dump_hier(h2, depth+1, type);
		break;
	case accTask:
		if (type == 0)
			return;
		for (i = 0; i < depth; i++)
			printf(". ");
		printf("%s:task\n", acc_fetch_name(h));
		break;;
	case accFunction:
		if (type == 0)
			return;
		for (i = 0; i < depth; i++)
			printf(". ");
		printf("%s:function\n", acc_fetch_name(h));
		break;
	case accStatement:
		if (type == 0)
			return;
		for (i = 0; i < depth; i++)
			printf(". ");
		printf("%s:statement\n", acc_fetch_name(h));
		h2 = NULL;
		for (h2 = acc_next(list, h, h2); h2; h2 = acc_next(list, h, h2))
			dump_hier(h2, depth+1, type);
		break;
	default:	
		break;
	}
}

int
call_print_hier(int data, int reason)
{
	char *cp;
	handle h, h2;
	int n, i, t, depth;

	n = tf_nump();
	acc_initialize();
	if (n == 0) {
		h = null;
		while (h = acc_next_topmod(h))
			dump_hier(h, 0, data);
	} else {
		for (i = 1; i <= n; i++) {
			h = acc_handle_tfarg(i);
			if (h)
				dump_hier(h, 0, data);
		}
	}
	acc_close();
}
