/*-
 * Copyright (c) 2003 Poul-Henning Kamp
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the authors may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * FreeBSD: src/lib/libgeom/geom_xml2tree.c,v 1.5 2005/05/24 10:10:38 phk Exp
 * $xforce: geomgui/geom_xml2dot.c,v 1.5 2006/05/20 15:32:03 xride Exp $
 */

#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/queue.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <err.h>
#include <libutil.h>
#include <libgeom.h>
#include "geomgui.h"

void gstrcat(char *str, char *dot)
{
/*
	int dot_size;
	int sb_size
	dot_size = sizeof(dot);
	sb_size = sizeof(sb);
	if ( sb_size < dot_size )
*/
	if( dot[0] != 'z')
		strcpy(dot,str);
	else
		strcat(dot,str);
}
int
geom_xml2dot(struct myconfigs *configs, char *dot, char *p)
{
	struct gclass *cl;
	struct ggeom *ge;
	struct gprovider *pr;
	struct gconsumer *co;
	struct gmesh *gmp;
	char str[1000];
	char humanbuf[24];
	char humanizebuf[6];
	char *sb;
	int ret;


//	bzero(gmp, sizeof *gmp);
//	geom_gettree(gmp);
	ret = geom_xml2tree(gmp,p);
	if(ret != 0) {
		printf("Error in geom_xml2tree()");
	}

	/* Collect all identifiers */
	LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
		if ( configs->color == 1 ) {
			snprintf(str, sizeof(str), "z%p [shape=box,color=\"red\",label=\"CLASS\\n%s\"];\n", cl, cl->lg_name);
		}
		else {
			snprintf(str, sizeof(str), "z%p [shape=box,label=\"CLASS\\n%s\"];\n", cl, cl->lg_name);
		}
		gstrcat(str,dot);
		LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
			if ( configs->color == 1 ) {
				snprintf(str, sizeof(str), "z%p [shape=box,color=\"green\",label=\"%s\\n%s\\nr#%d\"];\n", ge, cl->lg_name,ge->lg_name, ge->lg_rank);
			}
			else {
				snprintf(str, sizeof(str), "z%p [shape=box,label=\"%s\\n%s\\nr#%d\"];\n", ge, cl->lg_name,ge->lg_name, ge->lg_rank);
			}
			gstrcat(str,dot);
			LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
				if (configs->human == 1 ) {
					ret = humanize_number(humanizebuf,sizeof(humanizebuf),(int64_t) pr->lg_mediasize,"B",HN_AUTOSCALE,HN_DECIMAL|HN_NOSPACE|HN_B);
					snprintf(humanbuf,sizeof(humanbuf),"%s", humanizebuf);
					if (ret == -1) {
						snprintf(humanbuf,sizeof(humanbuf),"%jd", pr->lg_mediasize);
					}
				}
				else {
					snprintf(humanbuf,sizeof(humanbuf),"%jd", pr->lg_mediasize);
				}
				if ( configs->color == 1 ) {
					snprintf(str, sizeof(str), \
						"z%p [shape=hexagon,color=\"blue\", \
						label=\"%s\\nmode:%s\\nmediasize:%s\\nsectorsize:%iB\"];\n",\
						pr, pr->lg_name, pr->lg_mode, humanbuf, \
						pr->lg_sectorsize);
					
				}
				else {
					snprintf(str, sizeof(str), "z%p [shape=hexagon, \
label=\"%s\\nmode:%s\\nmediasize:%s\\nsectorsize:%iB\"];\n",\
					pr, pr->lg_name, pr->lg_mode, humanbuf, \
					pr->lg_sectorsize);
				}
				gstrcat(str,dot);
				snprintf(str, sizeof(str), "z%p -> z%p;\n", pr, ge);
//				snprintf(str, sizeof(str), "z%p -> z%p;\n", pr->lg_id, ge->lg_id);
				gstrcat(str,dot);
			}
			LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
				if ( configs->color == 1 ) {
					snprintf(str, sizeof(str), "z%p [label=\"mode:%s\",color=\"yellow\"];\n",
						co, co->lg_mode);
				}
				else {
					snprintf(str, sizeof(str), "z%p [label=\"mode:%s\"];\n",
						co, co->lg_mode);
				}
				gstrcat(str,dot);
				if (co->lg_provider) {
					snprintf(str, sizeof(str), "z%p -> z%p;\n", co, co->lg_provider);
					gstrcat(str,dot);
				}
				snprintf(str, sizeof(str), "z%p -> z%p;\n", ge, co);
				gstrcat(str,dot);
			}
		}
	}

/*
	snprintf(str, sizeof(str), "};\n");
	gstrcat(str,dot);
	geom_deletetree(gmp);
*/
	return (0);
}


syntax highlighted by Code2HTML, v. 0.9.1