Graphviz 14.1.2~dev.20260121.0931
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 "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
36typedef struct {
37 Agrec_t h;
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
53static int tot_edges;
54static int tot_nodes;
55static int tot_cc;
56static int tot_cl;
57static int n_graphs;
58static int n_indent;
59static int recurse;
60static int silent;
61static int verbose;
62static int gtype;
63static int flags;
64static char *fname;
65static char **Inputs;
66static FILE *outfile;
67
68static 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\
80By default, gc prints nodes and edges\n\
81If no files are specified, stdin is used\n";
82
83static void usage(int v)
84{
85 printf("%s",useString);
87}
88
89static 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':
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)
150 outfile = stdout;
151}
152
153static LIST(Agnode_t *) Stk;
154
155static void push(Agnode_t * np)
156{
157 ND_dfs_mark(np) = -1;
158 LIST_PUSH_BACK(&Stk, np);
159}
160
161static Agnode_t *pop(void)
162{
163 if (LIST_IS_EMPTY(&Stk)) {
164 return 0;
165 }
166 return LIST_POP_BACK(&Stk);
167}
168
169static 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
188static 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
196static 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
214static void ipr(long num)
215{
216 printf(" %7ld", num);
217}
218
219static void
220wcp(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
242static 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
271static 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
297int 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}
int agapply(Agraph_t *g, Agobj_t *obj, agobjfn_t fn, void *arg, int preorder)
Definition apply.c:62
cgraph.h additions
abstract graph C library, Cgraph API
bool is_a_cluster(Agraph_t *g)
Definition utils.c:695
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
static void init(int argc, char *argv[])
Definition gc.c:89
static char ** Inputs
Definition gc.c:65
static int verbose
Definition gc.c:61
static void ipr(long num)
Definition gc.c:214
static int tot_nodes
Definition gc.c:54
static int tot_cc
Definition gc.c:55
static int recurse
Definition gc.c:59
static int tot_cl
Definition gc.c:56
static int cc_decompose(Agraph_t *g)
Definition gc.c:196
#define EDGES
Definition gc.c:46
static void wcp(int nnodes, int nedges, int ncc, int ncl, char *gname, char *filename)
Definition gc.c:220
static int n_indent
Definition gc.c:58
static int silent
Definition gc.c:60
static Agnode_t * pop(void)
Definition gc.c:161
#define UNDIRECTED
Definition gc.c:51
#define CC
Definition gc.c:47
static void cntCluster(Agraph_t *g, Agobj_t *sg, void *arg)
Definition gc.c:188
#define DIRECTED
Definition gc.c:50
#define CL
Definition gc.c:48
static int gtype
Definition gc.c:62
static int eval(Agraph_t *g, int root)
Definition gc.c:271
#define ND_dfs_mark(n)
Definition gc.c:41
static void emit(Agraph_t *g, int root, int cl_count)
Definition gc.c:242
static int n_graphs
Definition gc.c:57
static int tot_edges
Definition gc.c:53
#define NODES
Definition gc.c:45
#define GTYPE(g)
Definition gc.c:269
static char * useString
Definition gc.c:68
static int flags
Definition gc.c:63
static void cc_dfs(Agraph_t *g, Agnode_t *n)
Definition gc.c:169
static FILE * outfile
Definition gc.c:66
static char * fname
Definition gc.c:64
static char * gname
Definition gml2gv.c:27
static gstack_t * push(gstack_t *s, Agraph_t *subg)
Definition grammar.c:1655
node NULL
Definition grammar.y:181
int agnedges(Agraph_t *g)
Definition graph.c:163
int agnnodes(Agraph_t *g)
Definition graph.c:157
#define agtail(e)
Definition cgraph.h:977
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:98
#define aghead(e)
Definition cgraph.h:978
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:89
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:97
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:50
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:43
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:145
#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:172
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:75
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:80
static const char * usage
Definition gvpr.c:54
$2 prev
Definition htmlparse.y:291
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
type-generic dynamically expanding list
#define LIST(type)
Definition list.h:55
#define LIST_FREE(list)
Definition list.h:370
#define LIST_POP_BACK(list)
Definition list.h:407
#define LIST_IS_EMPTY(list)
Definition list.h:90
#define LIST_PUSH_BACK(list, item)
Definition list.h:384
static int nedges
total no. of edges used in routing
Definition routespl.c:32
a generic header of Agraph_s, Agnode_s and Agedge_s
Definition cgraph.h:210
graph or subgraph
Definition cgraph.h:424
Agobj_t base
Definition cgraph.h:425
implementation of Agrec_t
Definition cgraph.h:172
int dfs_mark
Definition gc.c:38
int main()