Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gc.c
Go to the documentation of this file.
1
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 <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <cgraph/cgraph.h>
27#include <cgraph/cghdr.h>
28#include <cgraph/ingraphs.h>
29#include <cgraph/list.h>
30#include <common/render.h>
31#include <common/utils.h>
32#include <util/exit.h>
33
34typedef struct {
35 Agrec_t h;
38
39#define ND_dfs_mark(n) (((nodeinfo_t*)(n->base.data))->dfs_mark)
40
41#include <getopt.h>
42
43#define NODES 1
44#define EDGES 2
45#define CC 4
46#define CL 8
47
48#define DIRECTED 1
49#define UNDIRECTED 2
50
51static int tot_edges;
52static int tot_nodes;
53static int tot_cc;
54static int tot_cl;
55static int n_graphs;
56static int n_indent;
57static int recurse;
58static int silent;
59static int verbose;
60static int gtype;
61static int flags;
62static char *fname;
63static char **Inputs;
64static FILE *outfile;
65
66static char *useString = "Usage: gc [-necCaDUrsv?] <files>\n\
67 -n - print number of nodes\n\
68 -e - print number of edges\n\
69 -c - print number of connected components\n\
70 -C - print number of clusters\n\
71 -a - print all counts\n\
72 -D - only directed graphs\n\
73 -U - only undirected graphs\n\
74 -r - recursively analyze subgraphs\n\
75 -s - silent\n\
76 -v - verbose\n\
77 -? - print usage\n\
78By default, gc prints nodes and edges\n\
79If no files are specified, stdin is used\n";
80
81static void usage(int v)
82{
83 printf("%s",useString);
85}
86
87static void init(int argc, char *argv[])
88{
89 int c;
90
91 opterr = 0;
92 while ((c = getopt(argc, argv, "necCaDUrsv?")) != -1) {
93 switch (c) {
94 case 'e':
95 flags |= EDGES;
96 break;
97 case 'n':
98 flags |= NODES;
99 break;
100 case 'c':
101 flags |= CC;
102 break;
103 case 'C':
104 flags |= CL;
105 tot_cl = 0;
106 break;
107 case 'a':
108 flags = NODES | EDGES | CC | CL;
109 break;
110 case 'r':
111 recurse = 1;
112 break;
113 case 's':
114 silent = 1;
115 break;
116 case 'v':
117 verbose = 1;
118 break;
119 case 'D':
120 gtype = DIRECTED;
121 break;
122 case 'U':
124 break;
125 case '?':
126 if (optopt == '\0' || optopt == '?')
127 usage(0);
128 else {
129 fprintf(stderr, "gc: option -%c unrecognized\n",
130 optopt);
131 usage(1);
132 }
133 break;
134 default:
135 fprintf(stderr, "gc: unexpected error\n");
136 graphviz_exit(EXIT_FAILURE);
137 }
138 }
139 argv += optind;
140 argc -= optind;
141
142 if (argc)
143 Inputs = argv;
144 if (flags == 0)
145 flags = NODES | EDGES;
146 if (gtype == 0)
148 outfile = stdout;
149}
150
151DEFINE_LIST(node_stack, Agnode_t *)
152static node_stack_t Stk;
153
154static void push(Agnode_t * np)
155{
156 ND_dfs_mark(np) = -1;
157 node_stack_push_back(&Stk, np);
158}
159
160static Agnode_t *pop(void)
161{
162 if (node_stack_is_empty(&Stk)) {
163 return 0;
164 }
165 return node_stack_pop_back(&Stk);
166}
167
168static void cc_dfs(Agraph_t * g, Agnode_t * n)
169{
170 Agedge_t *e;
171 Agnode_t *nxt;
172
173 push(n);
174 while ((n = pop())) {
175 ND_dfs_mark(n) = 1;
176 for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
177 if (n == agtail(e))
178 nxt = aghead(e);
179 else
180 nxt = agtail(e);
181 if (ND_dfs_mark(nxt) == 0)
182 push(nxt);
183 }
184 }
185}
186
187static void cntCluster(Agraph_t * g, Agobj_t * sg, void *arg)
188{
189 (void)g;
190
191 if (AGTYPE(sg) == AGRAPH && is_a_cluster((Agraph_t *)sg))
192 *(int *) (arg) += 1;
193}
194
195static int cc_decompose(Agraph_t * g)
196{
197 int c_cnt = 0;
198 Agnode_t *n;
199
200 c_cnt = 0;
201 for (n = agfstnode(g); n; n = agnxtnode(g, n))
202 ND_dfs_mark(n) = 0;
203 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
204 if (ND_dfs_mark(n))
205 continue;
206 c_cnt++;
207 cc_dfs(g, n);
208 }
209
210 return c_cnt;
211}
212
213static void ipr(long num)
214{
215 printf(" %7ld", num);
216}
217
218static void
219wcp(int nnodes, int nedges, int ncc, int ncl, char *gname, char *filename)
220{
221 int i;
222
223 if (silent)
224 return;
225 for (i = 0; i < n_indent; i++)
226 fputs(" ", outfile);
227 if (flags & NODES)
228 ipr(nnodes);
229 if (flags & EDGES)
230 ipr(nedges);
231 if (flags & CC)
232 ipr(ncc);
233 if (flags & CL)
234 ipr(ncl);
235 if (fname)
236 printf(" %s (%s)\n", gname, filename);
237 else
238 printf(" %s\n", gname);
239}
240
241static void emit(Agraph_t * g, int root, int cl_count)
242{
243 int n_edges = agnedges(g);
244 int n_nodes = agnnodes(g);
245 int n_cc = 0;
246 int n_cl = 0;
247 char *file = 0;
248
249 if (flags & CC)
250 n_cc = cc_decompose(g);
251
252 if (flags & CL)
253 n_cl = cl_count;
254
255 if (root)
256 file = fname;
257 wcp(n_nodes, n_edges, n_cc, n_cl, agnameof(g), file);
258
259 if (root) {
260 n_graphs++;
261 tot_edges += n_edges;
262 tot_nodes += n_nodes;
263 tot_cc += n_cc;
264 tot_cl += n_cl;
265 }
266}
267
268#define GTYPE(g) (agisdirected(g)?DIRECTED:UNDIRECTED)
269
270static int eval(Agraph_t * g, int root)
271{
272 Agraph_t *subg;
273 int cl_count = 0;
274
275 if (root && !(GTYPE(g) & gtype))
276 return 1;
277
278 if (root) {
279 aginit(g, AGNODE, "nodeinfo", sizeof(nodeinfo_t), true);
280 }
281
282 if ((flags & CL) && root)
283 agapply(g, (Agobj_t *) g, cntCluster, &cl_count, false);
284
285 emit(g, root, cl_count);
286
287 if (recurse) {
288 n_indent++;
289 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg))
290 eval(subg, 0);
291 n_indent--;
292 }
293 return 0;
294}
295
296int main(int argc, char *argv[])
297{
298 Agraph_t *g;
299 Agraph_t *prev = NULL;
300 ingraph_state ig;
301 int rv = 0;
302
303 init(argc, argv);
304 newIngraph(&ig, Inputs);
305
306 while ((g = nextGraph(&ig)) != 0) {
307 if (prev)
308 agclose(prev);
309 prev = g;
310 fname = fileName(&ig);
311 if (verbose)
312 fprintf(stderr, "Process graph %s in file %s\n", agnameof(g),
313 fname);
314 rv |= eval(g, 1);
315 }
316
317 if (n_graphs > 1)
318 wcp(tot_nodes, tot_edges, tot_cc, tot_cl, "total", 0);
319
320 node_stack_free(&Stk);
321
322 graphviz_exit(rv);
323}
int agapply(Agraph_t *g, Agobj_t *obj, agobjfn_t fn, void *arg, int preorder)
Definition apply.c:60
cgraph.h additions
abstract graph C library, Cgraph API
bool is_a_cluster(Agraph_t *g)
Definition utils.c:690
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
static void init(int argc, char *argv[])
Definition gc.c:87
static char ** Inputs
Definition gc.c:63
static int verbose
Definition gc.c:59
static void ipr(long num)
Definition gc.c:213
static int tot_nodes
Definition gc.c:52
static int tot_cc
Definition gc.c:53
static int recurse
Definition gc.c:57
static int tot_cl
Definition gc.c:54
static int cc_decompose(Agraph_t *g)
Definition gc.c:195
#define EDGES
Definition gc.c:44
static void wcp(int nnodes, int nedges, int ncc, int ncl, char *gname, char *filename)
Definition gc.c:219
static int n_indent
Definition gc.c:56
static int silent
Definition gc.c:58
static node_stack_t Stk
Definition gc.c:152
static Agnode_t * pop(void)
Definition gc.c:160
#define UNDIRECTED
Definition gc.c:49
#define CC
Definition gc.c:45
static void cntCluster(Agraph_t *g, Agobj_t *sg, void *arg)
Definition gc.c:187
#define DIRECTED
Definition gc.c:48
#define CL
Definition gc.c:46
static int gtype
Definition gc.c:60
static int eval(Agraph_t *g, int root)
Definition gc.c:270
#define ND_dfs_mark(n)
Definition gc.c:39
static void emit(Agraph_t *g, int root, int cl_count)
Definition gc.c:241
static int n_graphs
Definition gc.c:55
static int tot_edges
Definition gc.c:51
#define NODES
Definition gc.c:43
#define GTYPE(g)
Definition gc.c:268
static char * useString
Definition gc.c:66
static int flags
Definition gc.c:61
static void cc_dfs(Agraph_t *g, Agnode_t *n)
Definition gc.c:168
static FILE * outfile
Definition gc.c:64
static char * fname
Definition gc.c:62
static void push(Agnode_t *np)
Definition gc.c:154
static char * gname
Definition gml2gv.c:24
node NULL
Definition grammar.y:163
int agnedges(Agraph_t *g)
Definition graph.c:175
int agnnodes(Agraph_t *g)
Definition graph.c:169
#define agtail(e)
Definition cgraph.h:880
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:94
#define aghead(e)
Definition cgraph.h:881
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:85
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:102
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
#define AGTYPE(obj)
returns AGRAPH, AGNODE, or AGEDGE depending on the type of the object
Definition cgraph.h:216
@ AGNODE
Definition cgraph.h:207
@ AGRAPH
Definition cgraph.h:207
void aginit(Agraph_t *g, int kind, const char *rec_name, int rec_size, int move_to_front)
attach new records to objects of specified kind
Definition rec.c:170
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:78
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:83
static const char * usage
Definition gvpr.c:51
$2 u p prev
Definition htmlparse.y:297
char * fileName(ingraph_state *sp)
Return name of current file being processed.
Definition ingraphs.c:156
Agraph_t * nextGraph(ingraph_state *sp)
Definition ingraphs.c:61
ingraph_state * newIngraph(ingraph_state *sp, char **files)
Definition ingraphs.c:140
supports user-supplied data
#define DEFINE_LIST(name, type)
Definition list.h:26
static int nedges
total no. of edges used in routing
Definition routespl.c:31
a generic header of Agraph_s, Agnode_s and Agedge_s
Definition cgraph.h:210
graph or subgraph
Definition cgraph.h:425
implementation of Agrec_t
Definition cgraph.h:172
int dfs_mark
Definition gc.c:36
int main()