Graphviz 13.0.0~dev.20250607.1528
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 strs_resize(stk, strs_size(stk) - 1, NULL);
73}
74
75static char *topString(strs_t *stk) {
76
77 if (strs_is_empty(stk)) {
78 fprintf(stderr, "PANIC: graphml2gv: empty element stack\n");
79 graphviz_exit(EXIT_FAILURE);
80 }
81
82 return *strs_back(stk);
83}
84
85static void freeString(strs_t *stk) {
86 strs_free(stk);
87}
88
89typedef struct {
90 char* gname;
91 strs_t elements;
92 int closedElementType;
93 bool edgeinverted;
94} userdata_t;
95
96static Agraph_t *root; /* root graph */
97static Agraph_t *G; /* Current graph */
98static Agedge_t *E; // current edge
99
100DEFINE_LIST(graph_stack, Agraph_t *)
101static graph_stack_t Gstack;
102
103static userdata_t genUserdata(char *dfltname) {
104 userdata_t user = {0};
105 user.elements = (strs_t){0};
106 user.closedElementType = TAG_NONE;
107 user.edgeinverted = false;
108 user.gname = dfltname;
109 return user;
110}
111
112static void freeUserdata(userdata_t ud) {
113 freeString(&ud.elements);
114}
115
116static int isAnonGraph(const char *name) {
117 if (*name++ != '%')
118 return 0;
119 while (gv_isdigit(*name))
120 name++; /* skip over digits */
121 return (*name == '\0');
122}
123
124static void push_subg(Agraph_t * g)
125{
126 // save the root if this is the first graph
127 if (graph_stack_is_empty(&Gstack)) {
128 root = g;
129 }
130
131 // insert the new graph
132 graph_stack_push_back(&Gstack, g);
133
134 // update the top graph
135 G = g;
136}
137
138static Agraph_t *pop_subg(void)
139{
140 if (graph_stack_is_empty(&Gstack)) {
141 fprintf(stderr, "graphml2gv: Gstack underflow in graph parser\n");
142 graphviz_exit(EXIT_FAILURE);
143 }
144
145 // pop the top graph
146 Agraph_t *g = graph_stack_pop_back(&Gstack);
147
148 // update the top graph
149 if (!graph_stack_is_empty(&Gstack)) {
150 G = *graph_stack_back(&Gstack);
151 }
152
153 return g;
154}
155
156static Agnode_t *bind_node(const char *name)
157{
158 return agnode(G, (char *)name, 1);
159}
160
161static Agedge_t *bind_edge(const char *tail, const char *head)
162{
163 Agnode_t *tailNode, *headNode;
164 char *key = 0;
165
166 tailNode = agnode(G, (char *) tail, 1);
167 headNode = agnode(G, (char *) head, 1);
168 E = agedge(G, tailNode, headNode, key, 1);
169 return E;
170}
171
172static int get_xml_attr(char *attrname, const char **atts)
173{
174 int count = 0;
175 while (atts[count] != NULL) {
176 if (strcmp(attrname, atts[count]) == 0) {
177 return count + 1;
178 }
179 count += 2;
180 }
181 return -1;
182}
183
184static char *defval = "";
185
186static void setEdgeAttr(Agedge_t *ep, char *name, const char *value,
187 userdata_t *ud) {
188 if (strcmp(name, "headport") == 0) {
189 char *const attrname = ud->edgeinverted ? "tailport" : "headport";
190 Agsym_t *ap = agattr_text(root, AGEDGE, attrname, 0);
191 if (!ap)
192 ap = agattr_text(root, AGEDGE, attrname, defval);
193 agxset(ep, ap, value);
194 } else if (strcmp(name, "tailport") == 0) {
195 char *const attrname = ud->edgeinverted ? "headport" : "tailport";
196 Agsym_t *ap = agattr_text(root, AGEDGE, attrname, 0);
197 if (!ap)
198 ap = agattr_text(root, AGEDGE, attrname, defval);
199 agxset(ep, ap, value);
200 } else {
201 Agsym_t *ap = agattr_text(root, AGEDGE, name, 0);
202 if (!ap)
203 ap = agattr_text(root, AGEDGE, name, defval);
204 agxset(ep, ap, value);
205 }
206}
207
208/*------------- expat handlers ----------------------------------*/
209
210static void
211startElementHandler(void *userData, const char *name, const char **atts)
212{
213 int pos;
214 userdata_t *ud = userData;
215 Agraph_t *g = NULL;
216
217 if (strcmp(name, "graphml") == 0) {
218 /* do nothing */
219 } else if (strcmp(name, "graph") == 0) {
220 const char *edgeMode = "";
221 const char *id;
222 Agdesc_t dir;
223 char buf[NAMEBUF]; /* holds % + number */
224
225 if (ud->closedElementType == TAG_GRAPH) {
226 fprintf(stderr,
227 "Warning: Node contains more than one graph.\n");
228 }
229 pos = get_xml_attr("id", atts);
230 if (pos > 0) {
231 id = atts[pos];
232 }
233 else
234 id = ud->gname;
235 pos = get_xml_attr("edgedefault", atts);
236 if (pos > 0) {
237 edgeMode = atts[pos];
238 }
239
240 if (graph_stack_is_empty(&Gstack)) {
241 if (strcmp(edgeMode, "directed") == 0) {
242 dir = Agdirected;
243 } else if (strcmp(edgeMode, "undirected") == 0) {
244 dir = Agundirected;
245 } else {
246 if (Verbose) {
247 fprintf(stderr,
248 "Warning: graph has no edgedefault attribute - assume directed\n");
249 }
250 dir = Agdirected;
251 }
252 g = agopen((char *) id, dir, &AgDefaultDisc);
253 push_subg(g);
254 } else {
255 Agraph_t *subg;
256 if (isAnonGraph(id)) {
257 static int anon_id = 1;
258 snprintf(buf, sizeof(buf), "%%%d", anon_id++);
259 id = buf;
260 }
261 subg = agsubg(G, (char *) id, 1);
262 push_subg(subg);
263 }
264
265 pushString(&ud->elements, id);
266 } else if (strcmp(name, "node") == 0) {
267 pos = get_xml_attr("id", atts);
268 if (pos > 0) {
269 const char *attrname;
270 attrname = atts[pos];
271 if (G == 0)
272 fprintf(stderr,"node %s outside graph, ignored\n",attrname);
273 else
274 bind_node(attrname);
275
276 pushString(&ud->elements, attrname);
277 }
278
279 } else if (strcmp(name, "edge") == 0) {
280 const char *head = "", *tail = "";
281 char *tname;
282 Agnode_t *t;
283
284 pos = get_xml_attr("source", atts);
285 if (pos > 0)
286 tail = atts[pos];
287 pos = get_xml_attr("target", atts);
288 if (pos > 0)
289 head = atts[pos];
290
291 if (G == 0)
292 fprintf(stderr,"edge source %s target %s outside graph, ignored\n",tail,head);
293 else {
294 bind_edge(tail, head);
295
296 t = AGTAIL(E);
297 tname = agnameof(t);
298
299 if (strcmp(tname, tail) == 0) {
300 ud->edgeinverted = false;
301 } else if (strcmp(tname, head) == 0) {
302 ud->edgeinverted = true;
303 }
304
305 pos = get_xml_attr("id", atts);
306 if (pos > 0) {
307 setEdgeAttr(E, GRAPHML_ID, atts[pos], ud);
308 }
309 }
310 } else {
311 /* must be some extension */
312 fprintf(stderr,
313 "Unknown node %s - ignoring.\n",
314 name);
315 }
316}
317
318static void endElementHandler(void *userData, const char *name)
319{
320 userdata_t *ud = userData;
321
322 if (strcmp(name, "graph") == 0) {
323 pop_subg();
324 popString(&ud->elements);
325 ud->closedElementType = TAG_GRAPH;
326 } else if (strcmp(name, "node") == 0) {
327 char *ele_name = topString(&ud->elements);
328 if (ud->closedElementType == TAG_GRAPH) {
329 Agnode_t *node = agnode(root, ele_name, 0);
330 if (node) agdelete(root, node);
331 }
332 popString(&ud->elements);
333 ud->closedElementType = TAG_NODE;
334 } else if (strcmp(name, "edge") == 0) {
335 E = 0;
336 ud->closedElementType = TAG_EDGE;
337 ud->edgeinverted = false;
338 }
339}
340
341static Agraph_t *graphml_to_gv(char *graphname, FILE *graphmlFile, int *rv) {
342 char buf[BUFSIZ];
343 int done;
344 userdata_t udata = genUserdata(graphname);
345 XML_Parser parser = XML_ParserCreate(NULL);
346
347 *rv = 0;
348 XML_SetUserData(parser, &udata);
349 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
350
351 root = 0;
352 do {
353 size_t len = fread(buf, 1, sizeof(buf), graphmlFile);
354 if (len == 0)
355 break;
356 done = len < sizeof(buf);
357 assert(len <= INT_MAX);
358 if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) {
359 fprintf(stderr,
360 "%s at line %lu\n",
361 XML_ErrorString(XML_GetErrorCode(parser)),
362 XML_GetCurrentLineNumber(parser));
363 *rv = 1;
364 break;
365 }
366 } while (!done);
367 XML_ParserFree(parser);
368 freeUserdata(udata);
369
370 return root;
371}
372
373static FILE *getFile(void)
374{
375 FILE *rv = NULL;
376 static FILE *savef = NULL;
377 static int cnt = 0;
378
379 if (Files == NULL) {
380 if (cnt++ == 0) {
381 rv = stdin;
382 }
383 } else {
384 if (savef)
385 fclose(savef);
386 while (Files[cnt]) {
387 if ((rv = fopen(Files[cnt++], "r")) != 0)
388 break;
389 else
390 fprintf(stderr, "Can't open %s\n", Files[cnt - 1]);
391 }
392 }
393 savef = rv;
394 return rv;
395}
396
397static const char *use = "Usage: %s [-gd?] [-o<file>] [<graphs>]\n\
398 -g<name> : use <name> as template for graph names\n\
399 -o<file> : output to <file> (stdout)\n\
400 -v : verbose mode\n\
401 -? : usage\n";
402
403static void usage(int v)
404{
405 fprintf(stderr, use, CmdName);
406 graphviz_exit(v);
407}
408
409static char *cmdName(char *path)
410{
411 char *sp;
412
413 sp = strrchr(path, '/');
414 if (sp)
415 sp++;
416 else
417 sp = path;
418 return sp;
419}
420
421static void initargs(int argc, char **argv)
422{
423 int c;
424
425 CmdName = cmdName(argv[0]);
426 opterr = 0;
427 while ((c = getopt(argc, argv, ":vg:o:")) != -1) {
428 switch (c) {
429 case 'g':
430 gname = optarg;
431 break;
432 case 'v':
433 Verbose = 1;
434 break;
435 case 'o':
436 if (outFile != NULL)
437 fclose(outFile);
438 outFile = openFile(CmdName, optarg, "w");
439 break;
440 case ':':
441 fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt);
442 usage(1);
443 break;
444 case '?':
445 if (optopt == '?')
446 usage(0);
447 else {
448 fprintf(stderr, "%s: option -%c unrecognized\n", CmdName,
449 optopt);
450 usage(1);
451 }
452 break;
453 default:
454 UNREACHABLE();
455 }
456 }
457
458 argv += optind;
459 argc -= optind;
460
461 if (argc)
462 Files = argv;
463 if (!outFile)
464 outFile = stdout;
465}
466
467static char *nameOf(agxbuf *buf, char *name, int cnt) {
468 if (*name == '\0')
469 return name;
470 if (cnt) {
471 agxbprint(buf, "%s%d", name, cnt);
472 return agxbuse(buf);
473 }
474 else
475 return name;
476}
477
478#endif
479
480int main(int argc, char **argv)
481{
483 Agraph_t *prev = 0;
484 FILE *inFile;
485 int rv = 0, gcnt = 0;
486
487#ifdef HAVE_EXPAT
488 agxbuf buf = {0};
489 initargs(argc, argv);
490 while ((inFile = getFile())) {
491 while ((graph = graphml_to_gv(nameOf(&buf, gname, gcnt), inFile, &rv))) {
492 gcnt++;
493 if (prev)
494 agclose(prev);
495 prev = graph;
496 if (Verbose)
497 fprintf (stderr, "%s: %d nodes %d edges\n",
500 fflush(outFile);
501 }
502 }
503
504 graph_stack_free(&Gstack);
505
506 agxbfree(&buf);
507 graphviz_exit(rv);
508#else
509 fputs("graphml2gv: not configured for conversion from GXL to GV\n", stderr);
510 graphviz_exit(1);
511#endif
512}
Agobj_t * copy(Agraph_t *g, Agobj_t *obj)
Definition actions.c:144
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:150
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:348
int agxset(void *obj, Agsym_t *sym, const char *value)
Definition attr.c:536
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:693
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