dot.c 3.6 KB
Newer Older
1 2
/* dot.c - Alpine Package Keeper (APK)
 *
3
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include <stdio.h>
#include <fnmatch.h>

#include "apk_applet.h"
#include "apk_database.h"
#include "apk_print.h"

#define S_EVALUATED	-1
#define S_EVALUATING	-2

struct dot_ctx {
	int errors_only;
	int not_empty;
};

static int dot_parse(void *pctx, struct apk_db_options *dbopts,
		     int optch, int optindex, const char *optarg)
{
	struct dot_ctx *ctx = (struct dot_ctx *) pctx;

	switch (optch) {
	case 0x10000:
		ctx->errors_only = 1;
		break;
	default:
		return -1;
	}
	return 0;
}

static void start_graph(struct dot_ctx *ctx)
{
	if (ctx->not_empty)
		return;
	ctx->not_empty = 1;

	printf( "digraph \"apkindex\" {\n"
		"  rankdir=LR;\n"
		"  node [shape=box];\n");
}

static int dump_pkg(struct dot_ctx *ctx, struct apk_package *pkg)
{
	int i, j, r, ret = 0;

56
	if (pkg->state_int == S_EVALUATED)
57
		return 0;
58 59 60

	if (pkg->state_int <= S_EVALUATING) {
		pkg->state_int--;
61 62 63
		return 1;
	}

64
	pkg->state_int = S_EVALUATING;
65 66 67 68
	for (i = 0; i < pkg->depends->num; i++) {
		struct apk_dependency *dep = &pkg->depends->item[i];
		struct apk_name *name = dep->name;

69
		if (name->providers->num == 0) {
70 71 72 73
			start_graph(ctx);
			printf("  \"" PKG_VER_FMT "\" -> \"%s\" [color=red];\n",
				PKG_VER_PRINTF(pkg),
				name->name);
74
			if (!name->state_int) {
75 76
				printf("  \"%s\" [style=dashed, color=red, fontcolor=red, shape=octagon];\n",
					name->name);
77
				name->state_int = 1;
78 79
			}
		} else {
80 81
			for (j = 0; j < name->providers->num; j++) {
				struct apk_provider *p0 = &name->providers->item[j];
82

83
				if (!apk_dep_is_provided(dep, p0))
84 85
					continue;

86
				r = dump_pkg(ctx, p0->pkg);
87 88 89
				ret += r;
				if (r || (!ctx->errors_only)) {
					start_graph(ctx);
90 91 92 93 94 95 96 97 98

					if (p0->pkg->name != dep->name) {
						/* provided package */
						printf("  \"" PROVIDER_FMT "\" -> \"" PKG_VER_FMT "\"[arrowhead=inv,color=green];\n",
							PROVIDER_PRINTF(p0),
							PKG_VER_PRINTF(p0->pkg));
					}

					printf("  \"" PKG_VER_FMT "\" -> \"" PROVIDER_FMT "\"%s;\n",
99
						PKG_VER_PRINTF(pkg),
100
						PROVIDER_PRINTF(p0),
101 102 103 104 105
						r ? "[color=red]" : "");
				}
			}
		}
	}
106 107
	ret -= S_EVALUATING - pkg->state_int;
	pkg->state_int = S_EVALUATED;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

	return ret;
}

static int foreach_pkg(apk_hash_item item, void *ctx)
{
	dump_pkg((struct dot_ctx *) ctx, (struct apk_package *) item);
	return 0;
}

static int dot_main(void *pctx, struct apk_database *db, int argc, char **argv)
{
	struct dot_ctx *ctx = (struct dot_ctx *) pctx;
	int i, j;

	if (argc) {
		for (i = 0; i < argc; i++) {
			struct apk_name *name = apk_db_get_name(db, APK_BLOB_STR(argv[i]));
			if (!name)
				continue;
128 129
			for (j = 0; j < name->providers->num; j++)
				dump_pkg(ctx, name->providers->item[j].pkg);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		}
	} else {
		apk_hash_foreach(&db->available.packages, foreach_pkg, pctx);
	}

	if (!ctx->not_empty)
		return 1;

	printf("}\n");
	return 0;
}

static struct apk_option dot_options[] = {
	{ 0x10000,	"errors",	"Output only parts of the graph which are considered "
					"errorneus: e.g. cycles and missing packages" },
};

static struct apk_applet apk_dot = {
	.name = "dot",
	.help = "Generate graphviz graphs",
	.arguments = "PKGMASK...",
	.open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE,
	.context_size = sizeof(struct dot_ctx),
	.num_options = ARRAY_SIZE(dot_options),
	.options = dot_options,
	.parse = dot_parse,
	.main = dot_main,
};

APK_DEFINE_APPLET(apk_dot);