Debian Codegraph @

cmd/tools/gc.c

raw
1 /**
2 * @file
3 * @brief count graph components
4 */
5
6 /*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16
17 /*
18 * Written by Emden Gansner
19 */
20
21 #include "config.h"
22
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <cgraph/cgraph.h>
29 #include <cgraph/cghdr.h>
30 #include <cgraph/ingraphs.h>
31 #include <common/render.h>
32 #include <common/utils.h>
33 #include <util/exit.h>
34 #include <util/list.h>
35
36 typedef struct {
37 Agrec_t h;
38 int dfs_mark;
39 } nodeinfo_t;
40
41 #define ND_dfs_mark(n) (((nodeinfo_t*)(n->base.data))->dfs_mark)
42
43 #include <getopt.h>
44
45 #define NODES 1
46 #define EDGES 2
47 #define CC 4
48 #define CL 8
49
50 #define DIRECTED 1
51 #define UNDIRECTED 2
52
53 static int tot_edges;
54 static int tot_nodes;
55 static int tot_cc;
56 static int tot_cl;
57 static int n_graphs;
58 static int n_indent;
59 static int recurse;
60 static int silent;
61 static int verbose;
62 static int gtype;
63 static int flags;
64 static char *fname;
65 static char **Inputs;
66 static FILE *outfile;
67
68 static char *useString = "Usage: gc [-necCaDUrsv?] <files>\n\
69 -n - print number of nodes\n\
70 -e - print number of edges\n\
71 -c - print number of connected components\n\
72 -C - print number of clusters\n\
73 -a - print all counts\n\
74 -D - only directed graphs\n\
75 -U - only undirected graphs\n\
76 -r - recursively analyze subgraphs\n\
77 -s - silent\n\
78 -v - verbose\n\
79 -? - print usage\n\
80 By default, gc prints nodes and edges\n\
81 If no files are specified, stdin is used\n";
82
83 static void usage(int v)
84 {
85 printf("%s",useString);
86 graphviz_exit(v);
87 }
88
89 static void init(int argc, char *argv[])
90 {
91 int c;
92
93 opterr = 0;
94 while ((c = getopt(argc, argv, "necCaDUrsv?")) != -1) {
95 switch (c) {
96 case 'e':
97 flags |= EDGES;
98 break;
99 case 'n':
100 flags |= NODES;
101 break;
102 case 'c':
103 flags |= CC;
104 break;
105 case 'C':
106 flags |= CL;
107 tot_cl = 0;
108 break;
109 case 'a':
110 flags = NODES | EDGES | CC | CL;
111 break;
112 case 'r':
113 recurse = 1;
114 break;
115 case 's':
116 silent = 1;
117 break;
118 case 'v':
119 verbose = 1;
120 break;
121 case 'D':
122 gtype = DIRECTED;
123 break;
124 case 'U':
125 gtype = UNDIRECTED;
126 break;
127 case '?':
128 if (optopt == '\0' || optopt == '?')
129 usage(0);
130 else {
131 fprintf(stderr, "gc: option -%c unrecognized\n",
132 optopt);
133 usage(1);
134 }
135 break;
136 default:
137 fprintf(stderr, "gc: unexpected error\n");
138 graphviz_exit(EXIT_FAILURE);
139 }
140 }
141 argv += optind;
142 argc -= optind;
143
144 if (argc)
145 Inputs = argv;
146 if (flags == 0)
147 flags = NODES | EDGES;
148 if (gtype == 0)
149 gtype = DIRECTED | UNDIRECTED;
150 outfile = stdout;
151 }
152
153 static LIST(Agnode_t *) Stk;
154
155 static void push(Agnode_t * np)
156 {
157 ND_dfs_mark(np) = -1;
158 LIST_PUSH_BACK(&Stk, np);
159 }
160
161 static Agnode_t *pop(void)
162 {
163 if (LIST_IS_EMPTY(&Stk)) {
164 return 0;
165 }
166 return LIST_POP_BACK(&Stk);
167 }
168
169 static void cc_dfs(Agraph_t * g, Agnode_t * n)
170 {
171 Agedge_t *e;
172 Agnode_t *nxt;
173
174 push(n);
175 while ((n = pop())) {
176 ND_dfs_mark(n) = 1;
177 for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
178 if (n == agtail(e))
179 nxt = aghead(e);
180 else
181 nxt = agtail(e);
182 if (ND_dfs_mark(nxt) == 0)
183 push(nxt);
184 }
185 }
186 }
187
188 static void cntCluster(Agraph_t * g, Agobj_t * sg, void *arg)
189 {
190 (void)g;
191
192 if (AGTYPE(sg) == AGRAPH && is_a_cluster((Agraph_t *)sg))
193 *(int *) (arg) += 1;
194 }
195
196 static int cc_decompose(Agraph_t * g)
197 {
198 int c_cnt = 0;
199 Agnode_t *n;
200
201 c_cnt = 0;
202 for (n = agfstnode(g); n; n = agnxtnode(g, n))
203 ND_dfs_mark(n) = 0;
204 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
205 if (ND_dfs_mark(n))
206 continue;
207 c_cnt++;
208 cc_dfs(g, n);
209 }
210
211 return c_cnt;
212 }
213
214 static void ipr(long num)
215 {
216 printf(" %7ld", num);
217 }
218
219 static void
220 wcp(int nnodes, int nedges, int ncc, int ncl, char *gname, char *filename)
221 {
222 int i;
223
224 if (silent)
225 return;
226 for (i = 0; i < n_indent; i++)
227 fputs(" ", outfile);
228 if (flags & NODES)
229 ipr(nnodes);
230 if (flags & EDGES)
231 ipr(nedges);
232 if (flags & CC)
233 ipr(ncc);
234 if (flags & CL)
235 ipr(ncl);
236 if (fname)
237 printf(" %s (%s)\n", gname, filename);
238 else
239 printf(" %s\n", gname);
240 }
241
242 static void emit(Agraph_t * g, int root, int cl_count)
243 {
244 int n_edges = agnedges(g);
245 int n_nodes = agnnodes(g);
246 int n_cc = 0;
247 int n_cl = 0;
248 char *file = 0;
249
250 if (flags & CC)
251 n_cc = cc_decompose(g);
252
253 if (flags & CL)
254 n_cl = cl_count;
255
256 if (root)
257 file = fname;
258 wcp(n_nodes, n_edges, n_cc, n_cl, agnameof(g), file);
259
260 if (root) {
261 n_graphs++;
262 tot_edges += n_edges;
263 tot_nodes += n_nodes;
264 tot_cc += n_cc;
265 tot_cl += n_cl;
266 }
267 }
268
269 #define GTYPE(g) (agisdirected(g)?DIRECTED:UNDIRECTED)
270
271 static int eval(Agraph_t * g, int root)
272 {
273 Agraph_t *subg;
274 int cl_count = 0;
275
276 if (root && !(GTYPE(g) & gtype))
277 return 1;
278
279 if (root) {
280 aginit(g, AGNODE, "nodeinfo", sizeof(nodeinfo_t), true);
281 }
282
283 if ((flags & CL) && root)
284 agapply(g, &g->base, cntCluster, &cl_count, false);
285
286 emit(g, root, cl_count);
287
288 if (recurse) {
289 n_indent++;
290 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg))
291 eval(subg, 0);
292 n_indent--;
293 }
294 return 0;
295 }
296
297 int main(int argc, char *argv[])
298 {
299 Agraph_t *g;
300 Agraph_t *prev = NULL;
301 ingraph_state ig;
302 int rv = 0;
303
304 init(argc, argv);
305 newIngraph(&ig, Inputs);
306
307 while ((g = nextGraph(&ig)) != 0) {
308 if (prev)
309 agclose(prev);
310 prev = g;
311 fname = fileName(&ig);
312 if (verbose)
313 fprintf(stderr, "Process graph %s in file %s\n", agnameof(g),
314 fname);
315 rv |= eval(g, 1);
316 }
317
318 if (n_graphs > 1)
319 wcp(tot_nodes, tot_edges, tot_cc, tot_cl, "total", 0);
320
321 LIST_FREE(&Stk);
322
323 graphviz_exit(rv);
324 }