Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
gvpack.cpp
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 R. Gansner
19 */
20
21
22
23#include "config.h"
24
25#include <getopt.h>
26#include <algorithm>
27#include <cassert>
28#include <gvc/gvc.h>
29#include <cgraph/ingraphs.h>
30#include <common/render.h>
31#include <common/utils.h>
32#include <neatogen/neatoprocs.h>
33#include <iostream>
34#include <limits>
35#include <map>
36#include <optional>
37#include <pack/pack.h>
38#include <set>
39#include <stddef.h>
40#include <string>
41#include <utility>
42#include <util/alloc.h>
43#include <util/exit.h>
44#include <vector>
45#include "openFile.h"
46
47extern "C" {
48#ifdef GVDLL
49 __declspec(dllimport)
50#endif
52}
53
55#ifdef GVDLL
56 { "gvplugin_neato_layout_LTX_library", 0 },
57#else
58 { "gvplugin_neato_layout_LTX_library", &gvplugin_neato_layout_LTX_library },
59#endif
60 { 0, 0 }
61};
62
63/* gvpack:
64 * Input consists of graphs in dot format.
65 * The graphs should have pos, width and height set for nodes,
66 * bb set for clusters, and, optionally, spline info for edges.
67 * The graphs are packed geometrically and combined
68 * into a single output graph, ready to be sent to neato -s -n2.
69 * -m <i> specifies the margin, in points, about each graph.
70 */
71
72typedef struct {
73 char *name;
74 char *value;
75} attr_t;
76
77static int verbose = 0;
78static char **myFiles = 0;
79static FILE *outfp; /* output; stdout by default */
80static std::vector<attr_t> G_args; // Storage for -G arguments
81static bool doPack; /* Do packing if true */
82static char* gname = const_cast<char*>("root");
83
84#define NEWNODE(n) ((node_t*)ND_alg(n))
85
86static const char useString[] =
87 "Usage: gvpack [-gnuv?] [-m<margin>] {-array[_rc][n]] [-o<outf>] <files>\n\
88 -n - use node granularity\n\
89 -g - use graph granularity\n\
90 -array* - pack as array of graphs\n\
91 -G<n>=<v> - attach name/value attribute to output graph\n\
92 -m<n> - set margin to <n> points\n\
93 -s<gname> - use <gname> for name of root graph\n\
94 -o<outfile> - write output to <outfile>\n\
95 -u - no packing; just combine graphs\n\
96 -v - verbose\n\
97 -? - print usage\n\
98If no files are specified, stdin is used\n";
99
100static void usage(int v)
101{
102 std::cout << useString;
103 graphviz_exit(v);
104}
105
106/* setNameValue:
107 * If arg is a name-value pair, add it to the list
108 * and return 0; otherwise, return 1.
109 */
110static int setNameValue(char *arg)
111{
112 char *rhs = const_cast<char*>("true");
113
114 if (char *p = strchr(arg, '=')) {
115 *p++ = '\0';
116 rhs = p;
117 }
118 G_args.push_back(attr_t{arg, rhs});
119
120 return 0;
121}
122
123/* setUInt:
124 * If arg is an integer, value is stored in v
125 * and function returns 0; otherwise, returns 1.
126 */
127static int setUInt(unsigned int *v, char *arg)
128{
129 char *p;
130 unsigned int i;
131
132 i = (unsigned int) strtol(arg, &p, 10);
133 if (p == arg) {
134 std::cerr << "Error: bad value in flag -" << (arg - 1) << " - ignored\n";
135 return 1;
136 }
137 *v = i;
138 return 0;
139}
140
141static Agsym_t *agraphattr(Agraph_t *g, char *name, const char *value) {
142 return agattr(g, AGRAPH, name, value);
143}
144
145static Agsym_t *agnodeattr(Agraph_t *g, char *name, const char *value) {
146 return agattr(g, AGNODE, name, value);
147}
148
149static Agsym_t *agedgeattr(Agraph_t *g, char *name, const char *value) {
150 return agattr(g, AGEDGE, name, value);
151}
152
153static void init(int argc, char *argv[], pack_info* pinfo)
154{
155 int c;
156
157 agnodeattr(nullptr, const_cast<char*>("label"), NODENAME_ESC);
158 pinfo->mode = l_clust;
159 pinfo->margin = CL_OFFSET;
160 pinfo->doSplines = true; // Use edges in packing
161 pinfo->fixed = nullptr;
162 pinfo->sz = 0;
163
164 opterr = 0;
165 while ((c = getopt(argc, argv, ":na:gvum:s:o:G:?")) != -1) {
166 switch (c) {
167 case 'a': {
168 auto buf = std::string("a") + optarg + "\n";
169 parsePackModeInfo(buf.c_str(), pinfo->mode, pinfo);
170 break;
171 }
172 case 'n':
173 parsePackModeInfo ("node", pinfo->mode, pinfo);
174 break;
175 case 's':
176 gname = optarg;
177 break;
178 case 'g':
179 parsePackModeInfo ("graph", pinfo->mode, pinfo);
180 break;
181 case 'm':
182 setUInt(&pinfo->margin, optarg);
183 break;
184 case 'o':
185 if (outfp != nullptr)
186 fclose(outfp);
187 outfp = openFile("gvpack", optarg, "w");
188 break;
189 case 'u':
190 pinfo->mode = l_undef;
191 break;
192 case 'G':
193 if (*optarg)
194 setNameValue(optarg);
195 else
196 std::cerr << "gvpack: option -G missing argument - ignored\n";
197 break;
198 case 'v':
199 verbose = 1;
200 Verbose = 1;
201 break;
202 case ':':
203 std::cerr << "gvpack: option -" << (char)optopt
204 << " missing argument - ignored\n";
205 break;
206 case '?':
207 if (optopt == '\0' || optopt == '?')
208 usage(0);
209 else {
210 std::cerr << "gvpack: option -" << (char)optopt << " unrecognized\n";
211 usage(1);
212 }
213 break;
214 }
215 }
216 argv += optind;
217 argc -= optind;
218
219 if (argc > 0) {
220 myFiles = argv;
221 }
222 if (!outfp)
223 outfp = stdout; /* stdout the default */
224 if (verbose)
225 std::cerr << " margin " << pinfo->margin << '\n';
226}
227
228/* init_node_edge:
229 * initialize node and edge attributes
230 */
231static void init_node_edge(Agraph_t * g)
232{
233 node_t *n;
234 edge_t *e;
235 int nG = agnnodes(g);
236 attrsym_t *N_pos = agfindnodeattr(g, const_cast<char*>("pos"));
237 attrsym_t *N_pin = agfindnodeattr(g, const_cast<char*>("pin"));
238
239 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
241 user_pos(N_pos, N_pin, n, nG); /* set user position if given */
242 }
243 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
244 for (e = agfstout(g, n); e; e = agnxtout(g, e))
246 }
247}
248
249/* init_graph:
250 * Initialize attributes. We always do the minimum required by
251 * libcommon. If fill is true, we use init_nop (neato -n) to
252 * read in attributes relevant to the layout.
253 */
254static void init_graph(Agraph_t *g, bool fill, GVC_t *gvc) {
255 int d;
256 node_t *n;
257 edge_t *e;
258
259 aginit (g, AGRAPH, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
260 aginit (g, AGNODE, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true);
261 aginit (g, AGEDGE, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
262 GD_gvc(g) = gvc;
263 graph_init(g, false);
264 d = late_int(g, agfindgraphattr(g, const_cast<char*>("dim")), 2, 2);
265 if (d != 2) {
266 std::cerr << "Error: graph " << agnameof(g) << " has dim = " << d
267 << " (!= 2)\n";
268 graphviz_exit(1);
269 }
270 Ndim = GD_ndim(g) = 2;
272 if (fill) {
273 if (int ret = init_nop(g, 0)) {
274 if (ret < 0)
275 std::cerr << "Error loading layout info from graph " << agnameof(g) << '\n';
276 else if (ret > 0)
277 std::cerr << "gvpack does not support backgrounds as found in graph "
278 << agnameof(g) << '\n';
279 graphviz_exit(1);
280 }
281 if (Concentrate) { /* check for edges without pos info */
282 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
283 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
284 if (ED_spl(e) == nullptr) ED_edge_type(e) = IGNORED;
285 }
286 }
287 }
288 }
289}
290
291/* cloneAttrs:
292 * Copy all attributes from old object to new. Assume
293 * attributes have been initialized.
294 */
295static void cloneDfltAttrs(Agraph_t *old, Agraph_t *new_graph, int attr_kind) {
296 Agsym_t *a;
297
298 for (a = agnxtattr(old, attr_kind, 0); a; a = agnxtattr(old, attr_kind, a)) {
299 if (aghtmlstr(a->defval)) {
300 char *s = agstrdup_html(new_graph, a->defval);
301 agattr(new_graph, attr_kind, a->name, s);
302 agstrfree(new_graph, s); // drop the extra reference count we bumped for s
303 } else {
304 agattr(new_graph, attr_kind, a->name, a->defval);
305 }
306 }
307}
308static void cloneAttrs(void *old, void *new_graph) {
309 int attr_kind = AGTYPE(old);
310 Agsym_t *a;
311 char* s;
312 Agraph_t *g = agroot(old);
313 Agraph_t *ng = agroot(new_graph);
314
315 for (a = agnxtattr(g, attr_kind, 0); a; a = agnxtattr(g, attr_kind, a)) {
316 s = agxget (old, a);
317 if (aghtmlstr(s)) {
318 char *scopy = agstrdup_html(ng, s);
319 agset(new_graph, a->name, scopy);
320 agstrfree(ng, scopy); // drop the extra reference count we bumped for scopy
321 } else {
322 agset(new_graph, a->name, s);
323 }
324 }
325}
326
327/* cloneEdge:
328 * Note that here, and in cloneNode and cloneCluster,
329 * we do a shallow copy. We thus assume that attributes
330 * are not disturbed. In particular, we assume they are
331 * not deallocated.
332 */
333static void cloneEdge(Agedge_t *old, Agedge_t *new_edge) {
334 cloneAttrs(old, new_edge);
335 ED_spl(new_edge) = ED_spl(old);
336 ED_edge_type(new_edge) = ED_edge_type(old);
337 ED_label(new_edge) = ED_label(old);
338 ED_head_label(new_edge) = ED_head_label(old);
339 ED_tail_label(new_edge) = ED_tail_label(old);
340 ED_xlabel(new_edge) = ED_xlabel(old);
341}
342
343static void cloneNode(Agnode_t *old, Agnode_t *new_node) {
344 cloneAttrs(old, new_node);
345 ND_coord(new_node).x = POINTS(ND_pos(old)[0]);
346 ND_coord(new_node).y = POINTS(ND_pos(old)[1]);
347 ND_height(new_node) = ND_height(old);
348 ND_ht(new_node) = ND_ht(old);
349 ND_width(new_node) = ND_width(old);
350 ND_lw(new_node) = ND_lw(old);
351 ND_rw(new_node) = ND_rw(old);
352 ND_shape(new_node) = ND_shape(old);
353 ND_shape_info(new_node) = ND_shape_info(old);
354 ND_xlabel(new_node) = ND_xlabel(old);
355}
356
357static void cloneCluster(Agraph_t *old, Agraph_t *new_cluster) {
358 // string attributes were cloned as subgraphs
359 GD_label(new_cluster) = GD_label(old);
360 GD_bb(new_cluster) = GD_bb(old);
361}
362
363namespace {
365struct AttributeValue {
366 std::string value;
367 size_t instances;
368};
369} // namespace
370
372using attr_map_t = std::map<std::string, AttributeValue>;
373
374/* fillDict:
375 * Fill newdict with all the name-value attributes of
376 * objp. If the attribute has already been defined and
377 * has a different default, set default to "".
378 */
379static void fillDict(attr_map_t &newdict, Agraph_t *g, int attr_kind) {
380
381 for (Agsym_t *a = agnxtattr(g, attr_kind, 0); a; a = agnxtattr(g, attr_kind, a)) {
382 char *name = a->name;
383 char *value = a->defval;
384 auto it = newdict.find(name);
385 if (it == newdict.end()) {
386 newdict.insert({name, AttributeValue{value, 1}});
387 } else if (it->second.value == value)
388 ++it->second.instances;
389 }
390}
391
392/* fillGraph:
393 * Use all the name-value entries in the dictionary d to define
394 * to define universal node/edge/graph attributes for g.
395 * For a non-empty default value, the attribute must be defined and the
396 * same in all graphs.
397 */
398static void fillGraph(Agraph_t *g, const attr_map_t &d,
399 Agsym_t *(*setf)(Agraph_t *, char *, const char *),
400 size_t cnt) {
401 for (const auto &kv : d) {
402 const std::string &name = kv.first;
403 const std::string &value = kv.second.value;
404 const size_t &attr_cnt = kv.second.instances;
405 if (cnt == attr_cnt)
406 setf(g, const_cast<char *>(name.c_str()), value.c_str());
407 else
408 setf(g, const_cast<char *>(name.c_str()), "");
409 }
410}
411
412/* initAttrs:
413 * Initialize the attributes of root as the union of the
414 * attributes in the graphs gs.
415 */
416static void initAttrs(Agraph_t *root, std::vector<Agraph_t*> &gs) {
417 attr_map_t n_attrs;
418 attr_map_t e_attrs;
419 attr_map_t g_attrs;
420
421 for (Agraph_t *g : gs) {
422 fillDict(g_attrs, g, AGRAPH);
423 fillDict(n_attrs, g, AGNODE);
424 fillDict(e_attrs, g, AGEDGE);
425 }
426
427 fillGraph(root, g_attrs, agraphattr, gs.size());
428 fillGraph(root, n_attrs, agnodeattr, gs.size());
429 fillGraph(root, e_attrs, agedgeattr, gs.size());
430}
431
432static void cloneGraphAttr(Agraph_t * g, Agraph_t * ng)
433{
434 cloneAttrs(g, ng);
435 cloneDfltAttrs(g, ng, AGNODE);
436 cloneDfltAttrs(g, ng, AGEDGE);
437}
438
440using used_t = std::multiset<std::string>;
441
442/* xName:
443 * Create a name for an object in the new graph using the
444 * dictionary names and the old name. If the old name has not
445 * been used, use it and add it to names. If it has been used,
446 * create a new name using the old name and a number.
447 * Note that returned string will immediately made into an agstring.
448 */
449static std::string xName(used_t &names, char *oldname) {
450 size_t previous_instances = names.count(oldname);
451 names.insert(oldname);
452 if (previous_instances > 0) {
453 return std::string(oldname) + "_gv" + std::to_string(previous_instances);
454 }
455 return oldname;
456}
457
458#define MARK(e) (ED_alg(e) = e)
459#define MARKED(e) (ED_alg(e))
460#define SETCLUST(g,h) (GD_alg(g) = h)
461#define GETCLUST(g) ((Agraph_t*)GD_alg(g))
462
463/* cloneSubg:
464 * Create a copy of g in ng, copying attributes, inserting nodes
465 * and adding edges.
466 */
467static void
468cloneSubg(Agraph_t *g, Agraph_t *ng, Agsym_t *G_bb, used_t &gnames) {
469 node_t *n;
470 node_t *nn;
471 edge_t *e;
472 edge_t *ne;
473 node_t *nt;
474 node_t *nh;
475 Agraph_t *subg;
476 Agraph_t *nsubg;
477
478 cloneGraphAttr(g, ng);
479 if (doPack)
480 agxset(ng, G_bb, ""); /* Unset all subgraph bb */
481
482 /* clone subgraphs */
483 for (subg = agfstsubg (g); subg; subg = agnxtsubg (subg)) {
484 nsubg = agsubg(ng, xName(gnames, agnameof(subg)).data(), 1);
485 agbindrec (nsubg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
486 cloneSubg(subg, nsubg, G_bb, gnames);
487 /* if subgraphs are clusters, point to the new
488 * one so we can find it later.
489 */
490 if (is_a_cluster(subg))
491 SETCLUST(subg, nsubg);
492 }
493
494 /* add remaining nodes */
495 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
496 nn = NEWNODE(n);
497 agsubnode(ng, nn, 1);
498 }
499
500 /* add remaining edges. libgraph doesn't provide a way to find
501 * multiedges, so we add edges bottom up, marking edges when added.
502 */
503 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
504 for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
505 if (MARKED(e))
506 continue;
507 nt = NEWNODE(agtail(e));
508 nh = NEWNODE(aghead(e));
509 ne = agedge(ng, nt, nh, nullptr, 1);
510 agbindrec (ne, "Agedgeinfo_t", sizeof(Agedgeinfo_t), true);
511 cloneEdge(e, ne);
512 MARK(e);
513 }
514 }
515}
516
517/* cloneClusterTree:
518 * Given old and new subgraphs which are corresponding
519 * clusters, recursively create the subtree of clusters
520 * under ng using the subtree of clusters under g.
521 */
522static void cloneClusterTree(Agraph_t * g, Agraph_t * ng)
523{
524 int i;
525
526 cloneCluster(g, ng);
527
528 if (GD_n_cluster(g)) {
529 GD_n_cluster(ng) = GD_n_cluster(g);
530 GD_clust(ng) = reinterpret_cast<Agraph_t**>(gv_calloc(1 + GD_n_cluster(g), sizeof(Agraph_t*)));
531 for (i = 1; i <= GD_n_cluster(g); i++) {
532 Agraph_t *c = GETCLUST(GD_clust(g)[i]);
533 GD_clust(ng)[i] = c;
534 cloneClusterTree(GD_clust(g)[i], c);
535 }
536 }
537}
538
539/* cloneGraph:
540 * Create and return a new graph which is the logical union
541 * of the graphs gs.
542 */
543static Agraph_t *cloneGraph(std::vector<Agraph_t *> &gs, GVC_t *gvc,
544 Agdesc_t kind) {
545 Agraph_t *root;
546 Agraph_t *subg;
547 Agnode_t *n;
548 Agnode_t *np;
549 Agsym_t *G_bb;
550 bool doWarn = true;
551
552 if (verbose)
553 std::cerr << "Creating clone graph\n";
554 root = agopen(gname, kind, &AgDefaultDisc);
555 initAttrs(root, gs);
556 G_bb = agfindgraphattr(root, const_cast<char*>("bb"));
557 if (doPack) assert(G_bb);
558
559 /* add command-line attributes */
560 for (attr_t &a : G_args) {
561 if (Agsym_t *rv = agfindgraphattr(root, a.name))
562 agxset(root, rv, a.value);
563 else
564 agattr(root, AGRAPH, a.name, a.value);
565 }
566
567 /* do common initialization. This will handle root's label. */
568 init_graph(root, false, gvc);
570
571 used_t gnames; // dict of used subgraph names
572 used_t nnames; // dict of used node names
573 for (size_t i = 0; i < gs.size(); i++) {
574 Agraph_t *g = gs[i];
575 if (verbose)
576 std::cerr << "Cloning graph " << agnameof(g) << '\n';
577 GD_n_cluster(root) += GD_n_cluster(g);
578 GD_has_labels(root) |= GD_has_labels(g);
579
580 /* Clone nodes, checking for node name conflicts */
581 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
582 if (doWarn && agfindnode(root, agnameof(n))) {
583 std::cerr << "Warning: node " << agnameof(n) << " in graph[" << i << "] "
584 << agnameof(g) << " already defined\n"
585 << "Some nodes will be renamed.\n";
586 doWarn = false;
587 }
588 np = agnode(root, xName(nnames, agnameof(n)).data(), 1);
589 agbindrec (np, "Agnodeinfo_t", sizeof(Agnodeinfo_t), true);
590 ND_alg(n) = np;
591 cloneNode(n, np);
592 }
593
594 /* wrap the clone of g in a subgraph of root */
595 subg = agsubg(root, xName(gnames, agnameof(g)).data(), 1);
596 agbindrec (subg, "Agraphinfo_t", sizeof(Agraphinfo_t), true);
597 cloneSubg(g, subg, G_bb, gnames);
598 }
599
600 /* set up cluster tree */
601 if (GD_n_cluster(root)) {
602 int j, idx;
603 GD_clust(root) = reinterpret_cast<graph_t**>(gv_calloc(1 + GD_n_cluster(root), sizeof(graph_t*)));
604
605 idx = 1;
606 for (Agraph_t *g : gs) {
607 for (j = 1; j <= GD_n_cluster(g); j++) {
608 Agraph_t *c = GETCLUST(GD_clust(g)[j]);
609 GD_clust(root)[idx++] = c;
610 cloneClusterTree(GD_clust(g)[j], c);
611 }
612 }
613 }
614
615 return root;
616}
617
618/* readGraphs:
619 * Read input, parse the graphs, use init_nop (neato -n) to
620 * read in all attributes need for layout.
621 * Return the list of graphs. If cp != nullptr, set it to the number
622 * of graphs read.
623 * We keep track of the types of graphs read. They all must be
624 * either directed or undirected. If all graphs are strict, the
625 * combined graph will be strict; other, the combined graph will
626 * be non-strict.
627 *
628 * @param kind [out] The type to use for the combined graph
629 */
630static std::vector<Agraph_t *> readGraphs(GVC_t *gvc,
631 std::optional<Agdesc_t> &kind) {
632 std::vector<Agraph_t*> gs;
633 ingraph_state ig;
634
635 /* set various state values */
637 Nop = 2;
638
639 newIngraph(&ig, myFiles);
640 while (Agraph_t *g = nextGraph(&ig)) {
641 if (verbose)
642 std::cerr << "Reading graph " << agnameof(g) << '\n';
643 if (agnnodes(g) == 0) {
644 std::cerr << "Graph " << agnameof(g) << " is empty - ignoring\n";
645 continue;
646 }
647 if (!kind.has_value()) {
648 kind = g->desc;
649 }
650 else if (kind->directed != g->desc.directed) {
651 std::cerr << "Error: all graphs must be directed or undirected\n";
652 graphviz_exit(1);
653 } else if (!agisstrict(g))
654 kind = g->desc;
655 init_graph(g, doPack, gvc);
656 gs.push_back(g);
657 }
658
659 return gs;
660}
661
662/* compBB:
663 * Compute the bounding box containing the graphs.
664 * We can just use the bounding boxes of the graphs.
665 */
666static boxf compBB(std::vector<Agraph_t*> &gs) {
667 boxf bb, bb2;
668
669 bb = GD_bb(gs[0]);
670
671 for (size_t i = 1; i < gs.size(); i++) {
672 bb2 = GD_bb(gs[i]);
673 bb.LL.x = std::min(bb.LL.x, bb2.LL.x);
674 bb.LL.y = std::min(bb.LL.y, bb2.LL.y);
675 bb.UR.x = std::max(bb.UR.x, bb2.UR.x);
676 bb.UR.y = std::max(bb.UR.y, bb2.UR.y);
677 }
678
679 return bb;
680}
681
682#ifdef DEBUG
683void dump(Agraph_t * g)
684{
685 node_t *v;
686 edge_t *e;
687
688 for (v = agfstnode(g); v; v = agnxtnode(g, v)) {
689 std::cerr << agnameof(v) << '\n';
690 for (e = agfstout(g, v); e; e = agnxtout(g, e)) {
691 std::cerr << " " << agnameof(agtail(e)) << " -- " << agnameof(aghead(e))
692 << '\n';
693 }
694 }
695}
696
697void dumps(Agraph_t * g)
698{
699 graph_t *subg;
700
701 for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
702 dump(subg);
703 std::cerr << "====\n";
704 }
705}
706#endif
707
708int main(int argc, char *argv[])
709{
710 Agraph_t *g;
711 pack_info pinfo;
712 GVC_t * gvc;
713
714 init(argc, argv, &pinfo);
715
716 doPack = (pinfo.mode != l_undef);
717
718#if defined(_WIN32)
720#endif
721 gvc = gvContextPlugins(lt_preloaded_symbols, DEMAND_LOADING);
722 std::optional<Agdesc_t> kind; // type of graph
723 std::vector<Agraph_t*> gs = readGraphs(gvc, kind);
724 if (gs.empty())
725 graphviz_exit(0);
726
727 /* pack graphs */
728 if (doPack) {
729 if (packGraphs(gs.size(), gs.data(), 0, &pinfo)) {
730 std::cerr << "gvpack: packing of graphs failed.\n";
731 graphviz_exit(1);
732 }
733 }
734
735 /* create union graph and copy attributes */
736 assert(kind.has_value());
737 g = cloneGraph(gs, gvc, *kind);
738
739 /* compute new top-level bb and set */
740 if (doPack) {
741 GD_bb(g) = compBB(gs);
743 attach_attrs(g);
744 }
745 agwrite(g, outfp);
746 graphviz_exit(0);
747}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
int late_int(void *obj, attrsym_t *attr, int defaultValue, int minimum)
Definition utils.c:33
void common_init_edge(edge_t *e)
Definition utils.c:504
bool is_a_cluster(Agraph_t *g)
Definition utils.c:690
#define CL_OFFSET
Definition const.h:151
#define NODENAME_ESC
Definition const.h:80
#define IGNORED
Definition const.h:30
#define GVSPLINES
Definition const.h:173
#define agnodeattr(g, n, v)
Definition dotinit.c:272
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
#define POINTS(a_inches)
Definition geom.h:68
#define POINTS_PER_INCH
Definition geom.h:64
int State
Definition globals.h:62
bool Concentrate
Definition globals.h:58
int Nop
Definition globals.h:54
double PSinputscale
Definition globals.h:55
unsigned short Ndim
Definition globals.h:61
static bool Verbose
Definition gml2gv.c:23
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:210
int agnnodes(Agraph_t *g)
Definition graph.c:169
Agsym_t * agattr(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up attributes of a graph
Definition attr.c:338
int agset(void *obj, char *name, const char *value)
Definition attr.c:466
Agsym_t * agnxtattr(Agraph_t *g, int kind, Agsym_t *attr)
permits traversing the list of attributes of a given type
Definition attr.c:353
int agxset(void *obj, Agsym_t *sym, const char *value)
Definition attr.c:478
char * agxget(void *obj, Agsym_t *sym)
Definition attr.c:455
Agdisc_t AgDefaultDisc
Definition graph.c:289
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Definition edge.c:260
#define ED_xlabel(e)
Definition types.h:590
#define ED_head_label(e)
Definition types.h:587
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:24
#define ED_spl(e)
Definition types.h:595
#define agtail(e)
Definition cgraph.h:880
#define ED_edge_type(e)
Definition types.h:582
#define ED_tail_label(e)
Definition types.h:596
#define aghead(e)
Definition cgraph.h:881
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:39
#define ED_label(e)
Definition types.h:589
#define agfindgraphattr(g, a)
Definition types.h:613
#define GD_has_labels(g)
Definition types.h:368
#define GD_clust(g)
Definition types.h:360
int agisstrict(Agraph_t *g)
Definition graph.c:200
#define GD_bb(g)
Definition types.h:354
#define GD_n_cluster(g)
Definition types.h:389
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Definition graph.c:44
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:730
#define GD_ndim(g)
Definition types.h:390
#define GD_label(g)
Definition types.h:374
#define GD_gvc(g)
Definition types.h:355
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
Definition node.c:145
#define ND_ht(n)
Definition types.h:500
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
Definition node.c:259
#define agfindnodeattr(g, a)
Definition types.h:615
#define ND_alg(n)
Definition types.h:484
#define ND_rw(n)
Definition types.h:525
#define ND_height(n)
Definition types.h:498
#define ND_width(n)
Definition types.h:536
#define ND_lw(n)
Definition types.h:506
#define ND_xlabel(n)
Definition types.h:503
#define ND_shape_info(n)
Definition types.h:529
#define ND_pos(n)
Definition types.h:520
#define agfindnode(g, n)
Definition types.h:611
#define ND_coord(n)
Definition types.h:490
#define ND_shape(n)
Definition types.h:528
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
Agraph_t * agroot(void *obj)
Definition obj.c:168
@ AGEDGE
Definition cgraph.h:207
@ 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
void * agbindrec(void *obj, const char *name, unsigned int recsize, int move_to_front)
attaches a new record of the given size to the object
Definition rec.c:89
int aghtmlstr(const char *)
Definition refstr.c:164
int agstrfree(Agraph_t *, const char *)
Definition refstr.c:139
char * agstrdup_html(Agraph_t *, const char *)
Definition refstr.c:135
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:78
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:83
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
Definition subg.c:58
GVC_t * gvContextPlugins(const lt_symlist_t *builtins, int demand_loading)
Definition gvc.c:35
void attach_attrs(graph_t *g)
Definition output.c:394
static GVC_t * gvc
Definition gv.cpp:23
#define agraphattr(g, n, s)
Definition gv.cpp:19
Graphviz context library.
static int verbose
Definition gvpack.cpp:77
static void cloneClusterTree(Agraph_t *g, Agraph_t *ng)
Definition gvpack.cpp:522
#define MARK(e)
Definition gvpack.cpp:458
static void cloneCluster(Agraph_t *old, Agraph_t *new_cluster)
Definition gvpack.cpp:357
static void cloneDfltAttrs(Agraph_t *old, Agraph_t *new_graph, int attr_kind)
Definition gvpack.cpp:295
static Agraph_t * cloneGraph(std::vector< Agraph_t * > &gs, GVC_t *gvc, Agdesc_t kind)
Definition gvpack.cpp:543
std::multiset< std::string > used_t
names that have already been used during generation
Definition gvpack.cpp:440
static void init_node_edge(Agraph_t *g)
Definition gvpack.cpp:231
lt_symlist_t lt_preloaded_symbols[]
Definition gvpack.cpp:54
static std::vector< attr_t > G_args
Definition gvpack.cpp:80
static char * gname
Definition gvpack.cpp:82
static int setNameValue(char *arg)
Definition gvpack.cpp:110
static void init_graph(Agraph_t *g, bool fill, GVC_t *gvc)
Definition gvpack.cpp:254
#define NEWNODE(n)
Definition gvpack.cpp:84
static void cloneSubg(Agraph_t *g, Agraph_t *ng, Agsym_t *G_bb, used_t &gnames)
Definition gvpack.cpp:468
#define MARKED(e)
Definition gvpack.cpp:459
static void cloneNode(Agnode_t *old, Agnode_t *new_node)
Definition gvpack.cpp:343
static FILE * outfp
Definition gvpack.cpp:79
static boxf compBB(std::vector< Agraph_t * > &gs)
Definition gvpack.cpp:666
static bool doPack
Definition gvpack.cpp:81
static void fillDict(attr_map_t &newdict, Agraph_t *g, int attr_kind)
Definition gvpack.cpp:379
static std::string xName(used_t &names, char *oldname)
Definition gvpack.cpp:449
static void cloneEdge(Agedge_t *old, Agedge_t *new_edge)
Definition gvpack.cpp:333
static void fillGraph(Agraph_t *g, const attr_map_t &d, Agsym_t *(*setf)(Agraph_t *, char *, const char *), size_t cnt)
Definition gvpack.cpp:398
#define SETCLUST(g, h)
Definition gvpack.cpp:460
static int setUInt(unsigned int *v, char *arg)
Definition gvpack.cpp:127
static const char useString[]
Definition gvpack.cpp:86
static void cloneGraphAttr(Agraph_t *g, Agraph_t *ng)
Definition gvpack.cpp:432
static void initAttrs(Agraph_t *root, std::vector< Agraph_t * > &gs)
Definition gvpack.cpp:416
static void cloneAttrs(void *old, void *new_graph)
Definition gvpack.cpp:308
static std::vector< Agraph_t * > readGraphs(GVC_t *gvc, std::optional< Agdesc_t > &kind)
Definition gvpack.cpp:630
static void init(int argc, char *argv[], pack_info *pinfo)
Definition gvpack.cpp:153
static char ** myFiles
Definition gvpack.cpp:78
std::map< std::string, AttributeValue > attr_map_t
attribute name → value collection of those we have seen
Definition gvpack.cpp:372
gvplugin_library_t gvplugin_neato_layout_LTX_library
static Agsym_t * agedgeattr(Agraph_t *g, char *name, const char *value)
Definition gvpack.cpp:149
#define GETCLUST(g)
Definition gvpack.cpp:461
static const char * usage
Definition gvpr.c:51
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
void graph_init(graph_t *g, bool use_rankdir)
Definition input.c:592
bool user_pos(attrsym_t *posptr, attrsym_t *pinptr, node_t *np, int nG)
Definition neatoinit.c:74
int init_nop(Agraph_t *g, int adjust)
Definition neatoinit.c:537
static attrsym_t * N_pos
Definition neatoinit.c:53
void neato_init_node(node_t *n)
Definition neatoinit.c:59
static FILE * openFile(const char *argv0, const char *name, const char *mode)
Definition openFile.h:8
pack_mode parsePackModeInfo(const char *p, pack_mode dflt, pack_info *pinfo)
Definition pack.c:1208
int packGraphs(size_t ng, Agraph_t **gs, Agraph_t *root, pack_info *info)
Definition pack.c:1089
support for connected components
@ l_clust
Definition pack.h:55
@ l_undef
Definition pack.h:55
void dotneato_postprocess(Agraph_t *g)
Definition postproc.c:689
graph descriptor
Definition cgraph.h:284
unsigned directed
Definition cgraph.h:285
graph or subgraph
Definition cgraph.h:425
Agdesc_t desc
Definition cgraph.h:427
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:637
char * name
Definition cgraph.h:639
char * defval
Definition cgraph.h:640
Definition gvcint.h:80
Definition geom.h:41
pointf UR
Definition geom.h:41
pointf LL
Definition geom.h:41
Definition legal.c:50
void * address
Definition gvcext.h:41
pack_mode mode
Definition pack.h:72
int sz
Definition pack.h:69
bool doSplines
use splines in constructing graph shape
Definition pack.h:71
bool * fixed
Definition pack.h:73
unsigned int margin
Definition pack.h:70
double x
Definition geom.h:29
double y
Definition geom.h:29
int main()
Definition grammar.c:93