Graphviz 13.1.1~dev.20250718.2324
Loading...
Searching...
No Matches
graphml2gv.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#include "convert.h"
18#include <assert.h>
19#include <getopt.h>
20#include <limits.h>
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include "openFile.h"
26#include <util/agxbuf.h>
27#include <util/alloc.h>
28#include <util/exit.h>
29#include <util/gv_ctype.h>
30#include <util/list.h>
31#include <util/unreachable.h>
32#ifdef HAVE_EXPAT
33#include <expat.h>
34
35#ifndef XML_STATUS_ERROR
36#define XML_STATUS_ERROR 0
37#endif
38
39#define NAMEBUF 100
40
41#define GRAPHML_ID "_graphml_id"
42
43#define TAG_NONE -1
44#define TAG_GRAPH 0
45#define TAG_NODE 1
46#define TAG_EDGE 2
47
48static FILE *outFile;
49static char *CmdName;
50static char **Files;
51static int Verbose;
52static char* gname = "";
53
54DEFINE_LIST_WITH_DTOR(strs, char *, free)
55
56static void pushString(strs_t *stk, const char *s) {
57
58 // duplicate the string we will push
59 char *copy = gv_strdup(s);
60
61 // push this onto the stack
62 strs_push_back(stk, copy);
63}
64
65static void popString(strs_t *stk) {
66
67 if (strs_is_empty(stk)) {
68 fprintf(stderr, "PANIC: graphml2gv: empty element stack\n");
69 graphviz_exit(EXIT_FAILURE);
70 }
71
72 char *const popped = strs_pop_back(stk);
73 free(popped);
74}
75
76static char *topString(strs_t *stk) {
77
78 if (strs_is_empty(stk)) {
79 fprintf(stderr, "PANIC: graphml2gv: empty element stack\n");
80 graphviz_exit(EXIT_FAILURE);
81 }
82
83 return *strs_back(stk);
84}
85
86static void freeString(strs_t *stk) {
87 strs_free(stk);
88}
89
90typedef struct {
91 char* gname;
92 strs_t elements;
93 int closedElementType;
94 bool edgeinverted;
95} userdata_t;
96
97static Agraph_t *root; /* root graph */
98static Agraph_t *G; /* Current graph */
99static Agedge_t *E; // current edge
100
101DEFINE_LIST(graph_stack, Agraph_t *)
102static graph_stack_t Gstack;
103
104static userdata_t genUserdata(char *dfltname) {
105 userdata_t user = {0};
106 user.elements = (strs_t){0};
107 user.closedElementType = TAG_NONE;
108 user.edgeinverted = false;
109 user.gname = dfltname;
110 return user;
111}
112
113static void freeUserdata(userdata_t ud) {
114 freeString(&ud.elements);
115}
116
117static int isAnonGraph(const char *name) {
118 if (*name++ != '%')
119 return 0;
120 while (gv_isdigit(*name))
121 name++; /* skip over digits */
122 return (*name == '\0');
123}
124
125static void push_subg(Agraph_t * g)
126{
127 // save the root if this is the first graph
128 if (graph_stack_is_empty(&Gstack)) {
129 root = g;
130 }
131
132 // insert the new graph
133 graph_stack_push_back(&Gstack, g);
134
135 // update the top graph
136 G = g;
137}
138
139static Agraph_t *pop_subg(void)
140{
141 if (graph_stack_is_empty(&Gstack)) {
142 fprintf(stderr, "graphml2gv: Gstack underflow in graph parser\n");
143 graphviz_exit(EXIT_FAILURE);
144 }
145
146 // pop the top graph
147 Agraph_t *g = graph_stack_pop_back(&Gstack);
148
149 // update the top graph
150 if (!graph_stack_is_empty(&Gstack)) {
151 G = *graph_stack_back(&Gstack);
152 }
153
154 return g;
155}
156
157static Agnode_t *bind_node(const char *name)
158{
159 return agnode(G, (char *)name, 1);
160}
161
162static Agedge_t *bind_edge(const char *tail, const char *head)
163{
164 Agnode_t *tailNode, *headNode;
165 char *key = 0;
166
167 tailNode = agnode(G, (char *) tail, 1);
168 headNode = agnode(G, (char *) head, 1);
169 E = agedge(G, tailNode, headNode, key, 1);
170 return E;
171}
172
173static int get_xml_attr(char *attrname, const char **atts)
174{
175 int count = 0;
176 while (atts[count] != NULL) {
177 if (strcmp(attrname, atts[count]) == 0) {
178 return count + 1;
179 }
180 count += 2;
181 }
182 return -1;
183}
184
185static char *defval = "";
186
187static void setEdgeAttr(Agedge_t *ep, char *name, const char *value,
188 userdata_t *ud) {
189 if (strcmp(name, "headport") == 0) {
190 char *const attrname = ud->edgeinverted ? "tailport" : "headport";
191 Agsym_t *ap = agattr_text(root, AGEDGE, attrname, 0);
192 if (!ap)
193 ap = agattr_text(root, AGEDGE, attrname, defval);
194 agxset(ep, ap, value);
195 } else if (strcmp(name, "tailport") == 0) {
196 char *const attrname = ud->edgeinverted ? "headport" : "tailport";
197 Agsym_t *ap = agattr_text(root, AGEDGE, attrname, 0);
198 if (!ap)
199 ap = agattr_text(root, AGEDGE, attrname, defval);
200 agxset(ep, ap, value);
201 } else {
202 Agsym_t *ap = agattr_text(root, AGEDGE, name, 0);
203 if (!ap)
204 ap = agattr_text(root, AGEDGE, name, defval);
205 agxset(ep, ap, value);
206 }
207}
208
209/*------------- expat handlers ----------------------------------*/
210
211static void
212startElementHandler(void *userData, const char *name, const char **atts)
213{
214 int pos;
215 userdata_t *ud = userData;
216 Agraph_t *g = NULL;
217
218 if (strcmp(name, "graphml") == 0) {
219 /* do nothing */
220 } else if (strcmp(name, "graph") == 0) {
221 const char *edgeMode = "";
222 const char *id;
223 Agdesc_t dir;
224 char buf[NAMEBUF]; /* holds % + number */
225
226 if (ud->closedElementType == TAG_GRAPH) {
227 fprintf(stderr,
228 "Warning: Node contains more than one graph.\n");
229 }
230 pos = get_xml_attr("id", atts);
231 if (pos > 0) {
232 id = atts[pos];
233 }
234 else
235 id = ud->gname;
236 pos = get_xml_attr("edgedefault", atts);
237 if (pos > 0) {
238 edgeMode = atts[pos];
239 }
240
241 if (graph_stack_is_empty(&Gstack)) {
242 if (strcmp(edgeMode, "directed") == 0) {
243 dir = Agdirected;
244 } else if (strcmp(edgeMode, "undirected") == 0) {
245 dir = Agundirected;
246 } else {
247 if (Verbose) {
248 fprintf(stderr,
249 "Warning: graph has no edgedefault attribute - assume directed\n");
250 }
251 dir = Agdirected;
252 }
253 g = agopen((char *) id, dir, &AgDefaultDisc);
254 push_subg(g);
255 } else {
256 Agraph_t *subg;
257 if (isAnonGraph(id)) {
258 static int anon_id = 1;
259 snprintf(buf, sizeof(buf), "%%%d", anon_id++);
260 id = buf;
261 }
262 subg = agsubg(G, (char *) id, 1);
263 push_subg(subg);
264 }
265
266 pushString(&ud->elements, id);
267 } else if (strcmp(name, "node") == 0) {
268 pos = get_xml_attr("id", atts);
269 if (pos > 0) {
270 const char *attrname;
271 attrname = atts[pos];
272 if (G == 0)
273 fprintf(stderr,"node %s outside graph, ignored\n",attrname);
274 else
275 bind_node(attrname);
276
277 pushString(&ud->elements, attrname);
278 }
279
280 } else if (strcmp(name, "edge") == 0) {
281 const char *head = "", *tail = "";
282 char *tname;
283 Agnode_t *t;
284
285 pos = get_xml_attr("source", atts);
286 if (pos > 0)
287 tail = atts[pos];
288 pos = get_xml_attr("target", atts);
289 if (pos > 0)
290 head = atts[pos];
291
292 if (G == 0)
293 fprintf(stderr,"edge source %s target %s outside graph, ignored\n",tail,head);
294 else {
295 bind_edge(tail, head);
296
297 t = AGTAIL(E);
298 tname = agnameof(t);
299
300 if (strcmp(tname, tail) == 0) {
301 ud->edgeinverted = false;
302 } else if (strcmp(tname, head) == 0) {
303 ud->edgeinverted = true;
304 }
305
306 pos = get_xml_attr("id", atts);
307 if (pos > 0) {
308 setEdgeAttr(E, GRAPHML_ID, atts[pos], ud);
309 }
310 }
311 } else {
312 /* must be some extension */
313 fprintf(stderr,
314 "Unknown node %s - ignoring.\n",
315 name);
316 }
317}
318
319static void endElementHandler(void *userData, const char *name)
320{
321 userdata_t *ud = userData;
322
323 if (strcmp(name, "graph") == 0) {
324 pop_subg();
325 popString(&ud->elements);
326 ud->closedElementType = TAG_GRAPH;
327 } else if (strcmp(name, "node") == 0) {
328 char *ele_name = topString(&ud->elements);
329 if (ud->closedElementType == TAG_GRAPH) {
330 Agnode_t *node = agnode(root, ele_name, 0);
331 if (node) agdelete(root, node);
332 }
333 popString(&ud->elements);
334 ud->closedElementType = TAG_NODE;
335 } else if (strcmp(name, "edge") == 0) {
336 E = 0;
337 ud->closedElementType = TAG_EDGE;
338 ud->edgeinverted = false;
339 }
340}
341
342static Agraph_t *graphml_to_gv(char *graphname, FILE *graphmlFile, int *rv) {
343 char buf[BUFSIZ];
344 int done;
345 userdata_t udata = genUserdata(graphname);
346 XML_Parser parser = XML_ParserCreate(NULL);
347
348 *rv = 0;
349 XML_SetUserData(parser, &udata);
350 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
351
352 root = 0;
353 do {
354 size_t len = fread(buf, 1, sizeof(buf), graphmlFile);
355 if (len == 0)
356 break;
357 done = len < sizeof(buf);
358 assert(len <= INT_MAX);
359 if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) {
360 fprintf(stderr,
361 "%s at line %lu\n",
362 XML_ErrorString(XML_GetErrorCode(parser)),
363 XML_GetCurrentLineNumber(parser));
364 *rv = 1;
365 break;
366 }
367 } while (!done);
368 XML_ParserFree(parser);
369 freeUserdata(udata);
370
371 return root;
372}
373
374static FILE *getFile(void)
375{
376 FILE *rv = NULL;
377 static FILE *savef = NULL;
378 static int cnt = 0;
379
380 if (Files == NULL) {
381 if (cnt++ == 0) {
382 rv = stdin;
383 }
384 } else {
385 if (savef)
386 fclose(savef);
387 while (Files[cnt]) {
388 if ((rv = fopen(Files[cnt++], "r")) != 0)
389 break;
390 else
391 fprintf(stderr, "Can't open %s\n", Files[cnt - 1]);
392 }
393 }
394 savef = rv;
395 return rv;
396}
397
398static const char *use = "Usage: %s [-gd?] [-o<file>] [<graphs>]\n\
399 -g<name> : use <name> as template for graph names\n\
400 -o<file> : output to <file> (stdout)\n\
401 -v : verbose mode\n\
402 -? : usage\n";
403
404static void usage(int v)
405{
406 fprintf(stderr, use, CmdName);
407 graphviz_exit(v);
408}
409
410static char *cmdName(char *path)
411{
412 char *sp;
413
414 sp = strrchr(path, '/');
415 if (sp)
416 sp++;
417 else
418 sp = path;
419 return sp;
420}
421
422static void initargs(int argc, char **argv)
423{
424 int c;
425
426 CmdName = cmdName(argv[0]);
427 opterr = 0;
428 while ((c = getopt(argc, argv, ":vg:o:")) != -1) {
429 switch (c) {
430 case 'g':
431 gname = optarg;
432 break;
433 case 'v':
434 Verbose = 1;
435 break;
436 case 'o':
437 if (outFile != NULL)
438 fclose(outFile);
439 outFile = openFile(CmdName, optarg, "w");
440 break;
441 case ':':
442 fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt);
443 usage(1);
444 break;
445 case '?':
446 if (optopt == '?')
447 usage(0);
448 else {
449 fprintf(stderr, "%s: option -%c unrecognized\n", CmdName,
450 optopt);
451 usage(1);
452 }
453 break;
454 default:
455 UNREACHABLE();
456 }
457 }
458
459 argv += optind;
460 argc -= optind;
461
462 if (argc)
463 Files = argv;
464 if (!outFile)
465 outFile = stdout;
466}
467
468static char *nameOf(agxbuf *buf, char *name, int cnt) {
469 if (*name == '\0')
470 return name;
471 if (cnt) {
472 agxbprint(buf, "%s%d", name, cnt);
473 return agxbuse(buf);
474 }
475 else
476 return name;
477}
478
479#endif
480
481int main(int argc, char **argv)
482{
484 Agraph_t *prev = 0;
485 FILE *inFile;
486 int rv = 0, gcnt = 0;
487
488#ifdef HAVE_EXPAT
489 agxbuf buf = {0};
490 initargs(argc, argv);
491 while ((inFile = getFile())) {
492 while ((graph = graphml_to_gv(nameOf(&buf, gname, gcnt), inFile, &rv))) {
493 gcnt++;
494 if (prev)
495 agclose(prev);
496 prev = graph;
497 if (Verbose)
498 fprintf (stderr, "%s: %d nodes %d edges\n",
501 fflush(outFile);
502 }
503 }
504
505 graph_stack_free(&Gstack);
506
507 agxbfree(&buf);
508 graphviz_exit(rv);
509#else
510 fputs("graphml2gv: not configured for conversion from GXL to GV\n", stderr);
511 graphviz_exit(1);
512#endif
513}
Agobj_t * copy(Agraph_t *g, Agobj_t *obj)
Definition actions.c:145
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:78
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:234
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:307
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
Definition alloc.h:101
static FILE * inFile
Definition acyclic.c:36
DOT-GXL converter API for gxl2gv.c and gv2gxl.c.
static const char * use
Definition cvtgxl.c:66
static FILE * outFile
Definition cvtgxl.c:35
static void initargs(int argc, char **argv)
Definition cvtgxl.c:130
static char * cmdName(char *path)
Definition cvtgxl.c:78
static char * CmdName
Definition cvtgxl.c:36
#define head
Definition dthdr.h:15
static char ** Files
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
#define E
Definition gdefs.h:6
#define G
Definition gdefs.h:7
static double len(glCompPoint p)
Definition glutils.c:136
static FILE * getFile(void)
Definition gml2gv.c:29
static char * gname
Definition gml2gv.c:24
static bool Verbose
Definition gml2gv.c:23
static char * nameOf(agxbuf *buf, char *name, int cnt)
Definition gml2gv.c:119
void free(void *)
node NULL
Definition grammar.y:180
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:198
int agnedges(Agraph_t *g)
Definition graph.c:163
int agnnodes(Agraph_t *g)
Definition graph.c:157
Agsym_t * agattr_text(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up text attributes of a graph
Definition attr.c:334
int agxset(void *obj, Agsym_t *sym, const char *value)
Definition attr.c:522
Agdisc_t AgDefaultDisc
Definition graph.c:277
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Definition edge.c:253
#define AGTAIL(e)
Definition cgraph.h:984
Agdesc_t Agundirected
undirected
Definition graph.c:274
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:95
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Definition graph.c:42
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:696
Agdesc_t Agdirected
directed
Definition graph.c:272
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
Definition node.c:141
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:143
int agdelete(Agraph_t *g, void *obj)
deletes object. Equivalent to agclose, agdelnode, and agdeledge for obj being a graph,...
Definition obj.c:20
@ AGEDGE
Definition cgraph.h:207
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
Definition subg.c:53
static uint64_t id
Definition gv2gml.c:40
Agraph_t * graph(char *name)
Definition gv.cpp:30
replacements for ctype.h functions
static bool gv_isdigit(int c)
Definition gv_ctype.h:41
static const char * usage
Definition gvpr.c:47
#define XML_STATUS_ERROR
Definition htmllex.c:41
$2 u p prev
Definition htmlparse.y:297
#define DEFINE_LIST_WITH_DTOR(name, type, dtor)
Definition list.h:30
#define DEFINE_LIST(name, type)
Definition list.h:22
static FILE * openFile(const char *argv0, const char *name, const char *mode)
Definition openFile.h:8
graph descriptor
Definition cgraph.h:284
graph or subgraph
Definition cgraph.h:424
string attribute descriptor symbol in Agattr_s.dict
Definition cgraph.h:651
Definition types.h:81
int main()
Definition grammar.c:89
#define UNREACHABLE()
Definition unreachable.h:30