dot.c 4.06 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
 * 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 {
Timo Teräs's avatar
Timo Teräs committed
22 23 24
	int not_empty : 1;
	int errors_only : 1;
	int installed_only : 1;
25 26
};

27
static int option_parse_applet(void *pctx, struct apk_db_options *dbopts, int optch, const char *optarg)
28 29 30 31 32 33 34
{
	struct dot_ctx *ctx = (struct dot_ctx *) pctx;

	switch (optch) {
	case 0x10000:
		ctx->errors_only = 1;
		break;
Timo Teräs's avatar
Timo Teräs committed
35 36 37 38
	case 0x10001:
		ctx->installed_only = 1;
		dbopts->open_flags &= ~APK_OPENF_NO_INSTALLED;
		break;
39
	default:
40
		return -ENOTSUP;
41 42 43 44
	}
	return 0;
}

45 46 47 48 49 50 51 52 53 54 55 56 57
static const struct apk_option options_applet[] = {
	{ 0x10000,	"errors",	"Output only parts of the graph which are considered "
					"erroneous: e.g. cycles and missing packages" },
	{ 0x10001,	"installed",	"Consider only installed packages" },
};

static const struct apk_option_group optgroup_applet = {
	.name = "Dot",
	.options = options_applet,
	.num_options = ARRAY_SIZE(options_applet),
	.parse = option_parse_applet,
};

58 59 60 61 62 63 64 65 66 67 68
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");
}

69 70 71 72 73 74 75 76 77 78 79 80 81
static void dump_name(struct dot_ctx *ctx, struct apk_name *name)
{
	if (name->state_int)
		return;
	name->state_int = 1;

	if (name->providers->num == 0) {
		start_graph(ctx);
		printf("  \"%s\" [style=dashed, color=red, fontcolor=red, shape=octagon];\n",
			name->name);
	}
}

82 83
static int dump_pkg(struct dot_ctx *ctx, struct apk_package *pkg)
{
84 85 86
	struct apk_dependency *dep;
	struct apk_provider *p0;
	int r, ret = 0;
87

Timo Teräs's avatar
Timo Teräs committed
88 89 90
	if (ctx->installed_only && pkg->ipkg == NULL)
		return 0;

91
	if (pkg->state_int == S_EVALUATED)
92
		return 0;
93 94 95

	if (pkg->state_int <= S_EVALUATING) {
		pkg->state_int--;
96 97 98
		return 1;
	}

99
	pkg->state_int = S_EVALUATING;
100
	foreach_array_item(dep, pkg->depends) {
101 102
		struct apk_name *name = dep->name;

103 104
		dump_name(ctx, name);

105
		if (name->providers->num == 0) {
106
			printf("  \"" PKG_VER_FMT "\" -> \"%s\" [color=red];\n",
107 108 109 110
				PKG_VER_PRINTF(pkg), name->name);
			continue;
		}

111
		foreach_array_item(p0, name->providers) {
Timo Teräs's avatar
Timo Teräs committed
112 113
			if (ctx->installed_only && p0->pkg->ipkg == NULL)
				continue;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
			if (!apk_dep_is_provided(dep, p0))
				continue;

			r = dump_pkg(ctx, p0->pkg);
			ret += r;
			if (r || (!ctx->errors_only)) {
				start_graph(ctx);

				printf("  \"" PKG_VER_FMT "\" -> \"" PKG_VER_FMT "\"[",
					PKG_VER_PRINTF(pkg),
					PKG_VER_PRINTF(p0->pkg));
				if (r)
					printf("color=red,");
				if (p0->pkg->name != dep->name)
					printf("arrowhead=inv,label=\"%s\",", dep->name->name);
				printf("];\n");
130 131 132
			}
		}
	}
133 134
	ret -= S_EVALUATING - pkg->state_int;
	pkg->state_int = S_EVALUATED;
135 136 137 138 139 140 141 142 143 144

	return ret;
}

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

145
static int dot_main(void *pctx, struct apk_database *db, struct apk_string_array *args)
146 147
{
	struct dot_ctx *ctx = (struct dot_ctx *) pctx;
148
	struct apk_provider *p;
149
	char **parg;
150

151 152 153
	if (args->num) {
		foreach_array_item(parg, args) {
			struct apk_name *name = apk_db_get_name(db, APK_BLOB_STR(*parg));
154 155
			if (!name)
				continue;
156 157
			foreach_array_item(p, name->providers)
				dump_pkg(ctx, p->pkg);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		}
	} else {
		apk_hash_foreach(&db->available.packages, foreach_pkg, pctx);
	}

	if (!ctx->not_empty)
		return 1;

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

static struct apk_applet apk_dot = {
	.name = "dot",
	.help = "Generate graphviz graphs",
	.arguments = "PKGMASK...",
	.open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE,
175
	.command_groups = APK_COMMAND_GROUP_QUERY,
176
	.context_size = sizeof(struct dot_ctx),
177
	.optgroups = { &optgroup_global, &optgroup_applet },
178 179 180 181
	.main = dot_main,
};

APK_DEFINE_APPLET(apk_dot);