gxl2gv.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /**
  2. * @file
  3. * @brief <a href=https://en.wikipedia.org/wiki/GXL>GXL</a>-DOT convert subroutines
  4. */
  5. /*************************************************************************
  6. * Copyright (c) 2011 AT&T Intellectual Property
  7. * All rights reserved. This program and the accompanying materials
  8. * are made available under the terms of the Eclipse Public License v1.0
  9. * which accompanies this distribution, and is available at
  10. * https://www.eclipse.org/legal/epl-v10.html
  11. *
  12. * Contributors: Details at https://graphviz.org
  13. *************************************************************************/
  14. #include <assert.h>
  15. #include "convert.h"
  16. #include <cgraph/gv_ctype.h>
  17. #include <cgraph/list.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <util/agxbuf.h>
  21. #include <util/alloc.h>
  22. #include <util/exit.h>
  23. #include <util/startswith.h>
  24. #include <util/unreachable.h>
  25. #ifdef HAVE_EXPAT
  26. #include <expat.h>
  27. #include <limits.h>
  28. #include <stdlib.h>
  29. #ifndef XML_STATUS_ERROR
  30. #define XML_STATUS_ERROR 0
  31. #endif
  32. #define NAMEBUF 100
  33. #define GXL_ATTR "_gxl_"
  34. #define GXL_ID "_gxl_id"
  35. #define GXL_ROLE "_gxl_role"
  36. #define GXL_HYPER "_gxl_hypergraph"
  37. #define GXL_FROM "_gxl_fromorder"
  38. #define GXL_TO "_gxl_toorder"
  39. #define GXL_TYPE "_gxl_type"
  40. #define GXL_COMP "_gxl_composite_"
  41. #define GXL_LOC "_gxl_locator_"
  42. typedef enum {
  43. TAG_NONE,
  44. TAG_GRAPH,
  45. TAG_NODE,
  46. TAG_EDGE,
  47. TAG_HTML_LIKE_STRING,
  48. } attr_t;
  49. typedef struct {
  50. agxbuf xml_attr_name;
  51. agxbuf xml_attr_value;
  52. agxbuf composite_buffer;
  53. bool listen;
  54. attr_t closedElementType;
  55. attr_t globalAttrType;
  56. bool compositeReadState;
  57. bool edgeinverted;
  58. Dt_t *nameMap;
  59. } userdata_t;
  60. static Agraph_t *root; /* root graph */
  61. static attr_t Current_class; /* Current element type */
  62. static Agraph_t *G; /* Current graph */
  63. static Agnode_t *N; /* Set if Current_class == TAG_NODE */
  64. static Agedge_t *E; /* Set if Current_class == TAG_EDGE */
  65. DEFINE_LIST(graph_stack, Agraph_t *)
  66. static graph_stack_t Gstack;
  67. typedef struct {
  68. Dtlink_t link;
  69. char *name;
  70. char *unique_name;
  71. } namev_t;
  72. static void *make_nitem(void *p, Dtdisc_t *disc){
  73. (void)disc;
  74. namev_t *objp = p;
  75. namev_t *np = malloc(sizeof(namev_t));
  76. if (np == NULL)
  77. return NULL;
  78. np->name = objp->name;
  79. np->unique_name = 0;
  80. return np;
  81. }
  82. static void free_nitem(void *name) {
  83. namev_t *np = name;
  84. free(np->unique_name);
  85. free(np);
  86. }
  87. static Dtdisc_t nameDisc = {
  88. .key = offsetof(namev_t, name),
  89. .size = -1,
  90. .link = offsetof(namev_t, link),
  91. .makef = make_nitem,
  92. .freef = free_nitem,
  93. };
  94. static userdata_t genUserdata(void) {
  95. userdata_t user = {0};
  96. user.listen = false;
  97. user.closedElementType = TAG_NONE;
  98. user.globalAttrType = TAG_NONE;
  99. user.compositeReadState = false;
  100. user.edgeinverted = false;
  101. user.nameMap = dtopen(&nameDisc, Dtoset);
  102. return user;
  103. }
  104. static void freeUserdata(userdata_t ud) {
  105. dtclose(ud.nameMap);
  106. agxbfree(&ud.xml_attr_name);
  107. agxbfree(&ud.xml_attr_value);
  108. agxbfree(&ud.composite_buffer);
  109. }
  110. static void addToMap(Dt_t * map, char *name, char *uniqueName)
  111. {
  112. namev_t obj;
  113. namev_t *objp;
  114. obj.name = name;
  115. objp = dtinsert(map, &obj);
  116. assert(objp->unique_name == 0);
  117. objp->unique_name = gv_strdup(uniqueName);
  118. }
  119. static char *mapLookup(Dt_t *nm, const char *name) {
  120. namev_t *objp = dtmatch(nm, name);
  121. if (objp)
  122. return objp->unique_name;
  123. else
  124. return 0;
  125. }
  126. static int isAnonGraph(const char *name)
  127. {
  128. if (*name++ != '%')
  129. return 0;
  130. while (gv_isdigit(*name))
  131. name++; /* skip over digits */
  132. return (*name == '\0');
  133. }
  134. static void push_subg(Agraph_t * g)
  135. {
  136. // insert the new graph
  137. graph_stack_push_back(&Gstack, g);
  138. // save the root if this is the first graph
  139. if (graph_stack_size(&Gstack) == 1) {
  140. root = g;
  141. }
  142. // update the top graph
  143. G = g;
  144. }
  145. static Agraph_t *pop_subg(void)
  146. {
  147. // is the stack empty?
  148. if (graph_stack_is_empty(&Gstack)) {
  149. fprintf(stderr, "gxl2gv: Gstack underflow in graph parser\n");
  150. graphviz_exit(EXIT_FAILURE);
  151. }
  152. // pop the top graph
  153. Agraph_t *g = graph_stack_pop_back(&Gstack);
  154. // update the top graph
  155. if (!graph_stack_is_empty(&Gstack))
  156. G = *graph_stack_back(&Gstack);
  157. return g;
  158. }
  159. static Agnode_t *bind_node(const char *name)
  160. {
  161. N = agnode(G, (char *) name, 1);
  162. return N;
  163. }
  164. static Agedge_t *bind_edge(const char *tail, const char *head)
  165. {
  166. Agnode_t *tailNode, *headNode;
  167. char *key = 0;
  168. tailNode = agnode(G, (char *) tail, 1);
  169. headNode = agnode(G, (char *) head, 1);
  170. E = agedge(G, tailNode, headNode, key, 1);
  171. return E;
  172. }
  173. static 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. static void setName(Dt_t * names, Agobj_t * n, char *value)
  185. {
  186. Agsym_t *ap;
  187. char *oldName;
  188. ap = agattr(root, AGTYPE(n), GXL_ID, "");
  189. agxset(n, ap, agnameof(n));
  190. oldName = agxget(n, ap); /* set/get gives us new copy */
  191. addToMap(names, oldName, value);
  192. agrename(n, value);
  193. }
  194. static char *defval = "";
  195. static void
  196. setNodeAttr(Agnode_t * np, char *name, char *value, userdata_t * ud,
  197. bool is_html)
  198. {
  199. Agsym_t *ap;
  200. if (strcmp(name, "name") == 0) {
  201. setName(ud->nameMap, (Agobj_t *) np, value);
  202. } else {
  203. ap = agattr(root, AGNODE, name, 0);
  204. if (!ap)
  205. ap = agattr(root, AGNODE, name, defval);
  206. if (is_html) {
  207. char *val = agstrdup_html(root, value);
  208. agxset(np, ap, val);
  209. agstrfree(root, val); // drop the extra reference count we bumped for val
  210. } else {
  211. agxset(np, ap, value);
  212. }
  213. }
  214. }
  215. #define NODELBL "node:"
  216. #define NLBLLEN (sizeof(NODELBL)-1)
  217. #define EDGELBL "edge:"
  218. #define ELBLLEN (sizeof(EDGELBL)-1)
  219. /* setGlobalNodeAttr:
  220. * Set global node attribute.
  221. * The names must always begin with "node:".
  222. */
  223. static void
  224. setGlobalNodeAttr(Agraph_t * g, char *name, char *value)
  225. {
  226. if (!startswith(name, NODELBL))
  227. fprintf(stderr,
  228. "Warning: global node attribute %s in graph %s does not begin with the prefix %s\n",
  229. name, agnameof(g), NODELBL);
  230. else
  231. name += NLBLLEN;
  232. if ((g != root) && !agattr(root, AGNODE, name, 0))
  233. agattr(root, AGNODE, name, defval);
  234. agattr(G, AGNODE, name, value);
  235. }
  236. static void
  237. setEdgeAttr(Agedge_t * ep, char *name, char *value, userdata_t * ud,
  238. bool is_html)
  239. {
  240. Agsym_t *ap;
  241. char *attrname;
  242. if (strcmp(name, "headport") == 0) {
  243. if (ud->edgeinverted)
  244. attrname = "tailport";
  245. else
  246. attrname = "headport";
  247. ap = agattr(root, AGEDGE, attrname, 0);
  248. if (!ap)
  249. ap = agattr(root, AGEDGE, attrname, defval);
  250. } else if (strcmp(name, "tailport") == 0) {
  251. if (ud->edgeinverted)
  252. attrname = "headport";
  253. else
  254. attrname = "tailport";
  255. ap = agattr(root, AGEDGE, attrname, 0);
  256. if (!ap)
  257. ap = agattr(root, AGEDGE, attrname, defval);
  258. } else {
  259. ap = agattr(root, AGEDGE, name, 0);
  260. if (!ap)
  261. ap = agattr(root, AGEDGE, name, defval);
  262. }
  263. if (is_html) {
  264. char *val = agstrdup_html(root, value);
  265. agxset(ep, ap, val);
  266. agstrfree(root, val); // drop the extra reference count we bumped for val
  267. } else {
  268. agxset(ep, ap, value);
  269. }
  270. }
  271. /* setGlobalEdgeAttr:
  272. * Set global edge attribute.
  273. * The names always begin with "edge:".
  274. */
  275. static void
  276. setGlobalEdgeAttr(Agraph_t * g, char *name, char *value)
  277. {
  278. if (!startswith(name, EDGELBL))
  279. fprintf(stderr,
  280. "Warning: global edge attribute %s in graph %s does not begin with the prefix %s\n",
  281. name, agnameof(g), EDGELBL);
  282. else
  283. name += ELBLLEN;
  284. if ((g != root) && !agattr(root, AGEDGE, name, 0))
  285. agattr(root, AGEDGE, name, defval);
  286. agattr(g, AGEDGE, name, value);
  287. }
  288. static void
  289. setGraphAttr(Agraph_t * g, char *name, char *value, userdata_t * ud)
  290. {
  291. Agsym_t *ap;
  292. if ((g == root) && !strcmp(name, "strict") && !strcmp(value, "true")) {
  293. g->desc.strict = true;
  294. } else if (strcmp(name, "name") == 0)
  295. setName(ud->nameMap, (Agobj_t *) g, value);
  296. else {
  297. ap = agattr(root, AGRAPH, name, 0);
  298. if (ap)
  299. agxset(g, ap, value);
  300. else if (g == root)
  301. agattr(root, AGRAPH, name, value);
  302. else {
  303. ap = agattr(root, AGRAPH, name, defval);
  304. agxset(g, ap, value);
  305. }
  306. }
  307. }
  308. static void setAttr(char *name, char *value, userdata_t * ud, bool is_html)
  309. {
  310. switch (Current_class) {
  311. case TAG_GRAPH:
  312. setGraphAttr(G, name, value, ud);
  313. break;
  314. case TAG_NODE:
  315. setNodeAttr(N, name, value, ud, is_html);
  316. break;
  317. case TAG_EDGE:
  318. setEdgeAttr(E, name, value, ud, is_html);
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. /*------------- expat handlers ----------------------------------*/
  325. static void
  326. startElementHandler(void *userData, const char *name, const char **atts)
  327. {
  328. int pos;
  329. userdata_t *ud = userData;
  330. Agraph_t *g = NULL;
  331. if (strcmp(name, "gxl") == 0) {
  332. /* do nothing */
  333. } else if (strcmp(name, "graph") == 0) {
  334. const char *edgeMode = "";
  335. char buf[NAMEBUF]; /* holds % + number */
  336. Current_class = TAG_GRAPH;
  337. if (ud->closedElementType == TAG_GRAPH) {
  338. fprintf(stderr,
  339. "Warning: Node contains more than one graph.\n");
  340. }
  341. pos = get_xml_attr("id", atts);
  342. if (pos <= 0) {
  343. fprintf(stderr, "Error: Graph has no ID attribute.\n");
  344. graphviz_exit(EXIT_FAILURE);
  345. }
  346. const char *id = atts[pos];
  347. pos = get_xml_attr("edgemode", atts);
  348. if (pos > 0) {
  349. edgeMode = atts[pos];
  350. }
  351. if (graph_stack_is_empty(&Gstack)) {
  352. if (strcmp(edgeMode, "directed") == 0) {
  353. g = agopen((char *) id, Agdirected, &AgDefaultDisc);
  354. } else if (strcmp(edgeMode, "undirected") == 0) {
  355. g = agopen((char *) id, Agundirected, &AgDefaultDisc);
  356. } else {
  357. fprintf(stderr,
  358. "Warning: graph has no edgemode attribute");
  359. fprintf(stderr, " - assume directed\n");
  360. g = agopen((char *) id, Agdirected, &AgDefaultDisc);
  361. }
  362. push_subg(g);
  363. } else {
  364. Agraph_t *subg;
  365. if (isAnonGraph(id)) {
  366. static int anon_id = 1;
  367. snprintf(buf, sizeof(buf), "%%%d", anon_id++);
  368. id = buf;
  369. }
  370. subg = agsubg(G, (char *) id, 1);
  371. push_subg(subg);
  372. }
  373. pos = get_xml_attr("role", atts);
  374. if (pos > 0) {
  375. setGraphAttr(G, GXL_ROLE, (char *) atts[pos], ud);
  376. }
  377. pos = get_xml_attr("hypergraph", atts);
  378. if (pos > 0) {
  379. setGraphAttr(G, GXL_HYPER, (char *) atts[pos], ud);
  380. }
  381. } else if (strcmp(name, "node") == 0) {
  382. Current_class = TAG_NODE;
  383. pos = get_xml_attr("id", atts);
  384. if (pos > 0) {
  385. const char *attrname;
  386. attrname = atts[pos];
  387. if (attrname != NULL && strcmp(attrname, "") != 0) {
  388. bind_node(attrname);
  389. }
  390. }
  391. } else if (strcmp(name, "edge") == 0) {
  392. const char *head = "", *tail = "";
  393. char *tname;
  394. Agnode_t *t;
  395. Current_class = TAG_EDGE;
  396. pos = get_xml_attr("from", atts);
  397. if (pos > 0)
  398. tail = atts[pos];
  399. pos = get_xml_attr("to", atts);
  400. if (pos > 0)
  401. head = atts[pos];
  402. tname = mapLookup(ud->nameMap, tail);
  403. if (tname)
  404. tail = tname;
  405. tname = mapLookup(ud->nameMap, head);
  406. if (tname)
  407. head = tname;
  408. bind_edge(tail, head);
  409. t = AGTAIL(E);
  410. tname = agnameof(t);
  411. if (strcmp(tname, tail) == 0) {
  412. ud->edgeinverted = false;
  413. } else if (strcmp(tname, head) == 0) {
  414. ud->edgeinverted = true;
  415. }
  416. pos = get_xml_attr("fromorder", atts);
  417. if (pos > 0) {
  418. setEdgeAttr(E, GXL_FROM, (char *) atts[pos], ud, false);
  419. }
  420. pos = get_xml_attr("toorder", atts);
  421. if (pos > 0) {
  422. setEdgeAttr(E, GXL_TO, (char *) atts[pos], ud, false);
  423. }
  424. pos = get_xml_attr("id", atts);
  425. if (pos > 0) {
  426. setEdgeAttr(E, GXL_ID, (char *) atts[pos], ud, false);
  427. }
  428. } else if (strcmp(name, "attr") == 0) {
  429. const char *attrname = atts[get_xml_attr("name", atts)];
  430. agxbput(&ud->xml_attr_name, attrname);
  431. pos = get_xml_attr("kind", atts);
  432. if (pos > 0) {
  433. if (strcmp("node", atts[pos]) == 0)
  434. ud->globalAttrType = TAG_NODE;
  435. else if (strcmp("edge", atts[pos]) == 0)
  436. ud->globalAttrType = TAG_EDGE;
  437. else if (strcmp("graph", atts[pos]) == 0)
  438. ud->globalAttrType = TAG_GRAPH;
  439. else if (strcmp("HTML-like string", atts[pos]) == 0)
  440. ud->globalAttrType = TAG_HTML_LIKE_STRING;
  441. } else {
  442. ud->globalAttrType = TAG_NONE;
  443. }
  444. } else if (strcmp(name, "string") == 0
  445. || strcmp(name, "bool") == 0
  446. || strcmp(name, "int") == 0 || strcmp(name, "float") == 0) {
  447. ud->listen = true;
  448. if (ud->compositeReadState) {
  449. agxbprint(&ud->composite_buffer, "<%s>", name);
  450. }
  451. } else if (strcmp(name, "rel") == 0 || strcmp(name, "relend") == 0) {
  452. fprintf(stderr, "%s element is ignored by DOT\n", name);
  453. } else if (strcmp(name, "type") == 0) {
  454. pos = get_xml_attr("xlink:href", atts);
  455. if (pos > 0) {
  456. setAttr(GXL_TYPE, (char *) atts[pos], ud, false);
  457. }
  458. } else if (strcmp(name, "locator") == 0) {
  459. pos = get_xml_attr("xlink:href", atts);
  460. if (pos > 0) {
  461. const char *href = atts[pos];
  462. agxbprint(&ud->xml_attr_value, "%s%s", GXL_LOC, href);
  463. }
  464. } else if (strcmp(name, "seq") == 0
  465. || strcmp(name, "set") == 0
  466. || strcmp(name, "bag") == 0
  467. || strcmp(name, "tup") == 0 || strcmp(name, "enum") == 0) {
  468. ud->compositeReadState = true;
  469. agxbprint(&ud->composite_buffer, "<%s>", name);
  470. } else {
  471. /* must be some extension */
  472. fprintf(stderr,
  473. "Unknown node %s; DOT does not support extensions.\n",
  474. name);
  475. }
  476. }
  477. static void endElementHandler(void *userData, const char *name)
  478. {
  479. userdata_t *ud = userData;
  480. if (strcmp(name, "graph") == 0) {
  481. pop_subg();
  482. ud->closedElementType = TAG_GRAPH;
  483. } else if (strcmp(name, "node") == 0) {
  484. Current_class = TAG_GRAPH;
  485. N = 0;
  486. ud->closedElementType = TAG_NODE;
  487. } else if (strcmp(name, "edge") == 0) {
  488. Current_class = TAG_GRAPH;
  489. E = 0;
  490. ud->closedElementType = TAG_EDGE;
  491. ud->edgeinverted = false;
  492. } else if (strcmp(name, "attr") == 0) {
  493. agxbuf new_name = {0};
  494. char *value;
  495. ud->closedElementType = TAG_NONE;
  496. if (ud->compositeReadState) {
  497. agxbprint(&new_name, "%s%s", GXL_COMP, agxbuse(&ud->xml_attr_name));
  498. value = agxbuse(&ud->composite_buffer);
  499. agxbclear(&ud->xml_attr_value);
  500. ud->compositeReadState = false;
  501. } else {
  502. agxbput(&new_name, agxbuse(&ud->xml_attr_name));
  503. value = agxbuse(&ud->xml_attr_value);
  504. }
  505. switch (ud->globalAttrType) {
  506. case TAG_NONE:
  507. setAttr(agxbuse(&new_name), value, ud, false);
  508. break;
  509. case TAG_NODE:
  510. setGlobalNodeAttr(G, agxbuse(&new_name), value);
  511. break;
  512. case TAG_EDGE:
  513. setGlobalEdgeAttr(G, agxbuse(&new_name), value);
  514. break;
  515. case TAG_GRAPH:
  516. setGraphAttr(G, agxbuse(&new_name), value, ud);
  517. break;
  518. case TAG_HTML_LIKE_STRING:
  519. setAttr(agxbuse(&new_name), value, ud, true);
  520. break;
  521. default:
  522. UNREACHABLE();
  523. }
  524. agxbfree(&new_name);
  525. ud->globalAttrType = TAG_NONE;
  526. } else if (strcmp(name, "string") == 0
  527. || strcmp(name, "bool") == 0
  528. || strcmp(name, "int") == 0 || strcmp(name, "float") == 0) {
  529. ud->listen = false;
  530. if (ud->compositeReadState) {
  531. agxbprint(&ud->composite_buffer, "</%s>", name);
  532. }
  533. } else if (strcmp(name, "seq") == 0
  534. || strcmp(name, "set") == 0
  535. || strcmp(name, "bag") == 0
  536. || strcmp(name, "tup") == 0 || strcmp(name, "enum") == 0) {
  537. agxbprint(&ud->composite_buffer, "</%s>", name);
  538. }
  539. }
  540. static void characterDataHandler(void *userData, const char *s, int length)
  541. {
  542. userdata_t *ud = userData;
  543. assert(length >= 0 && "Expat returned negative length data");
  544. size_t len = (size_t)length;
  545. if (!ud->listen)
  546. return;
  547. if (ud->compositeReadState) {
  548. agxbput_n(&ud->composite_buffer, s, len);
  549. return;
  550. }
  551. agxbput_n(&ud->xml_attr_value, s, len);
  552. }
  553. Agraph_t *gxl_to_gv(FILE * gxlFile)
  554. {
  555. char buf[BUFSIZ];
  556. int done;
  557. userdata_t udata = genUserdata();
  558. XML_Parser parser = XML_ParserCreate(NULL);
  559. XML_SetUserData(parser, &udata);
  560. XML_SetElementHandler(parser, startElementHandler, endElementHandler);
  561. XML_SetCharacterDataHandler(parser, characterDataHandler);
  562. Current_class = TAG_GRAPH;
  563. root = 0;
  564. do {
  565. size_t len = fread(buf, 1, sizeof(buf), gxlFile);
  566. if (len == 0)
  567. break;
  568. done = len < sizeof(buf);
  569. assert(len <= (size_t)INT_MAX && "too large data for Expat API");
  570. if (XML_Parse(parser, buf, (int)len, done) == XML_STATUS_ERROR) {
  571. fprintf(stderr,
  572. "%s at line %lu\n",
  573. XML_ErrorString(XML_GetErrorCode(parser)),
  574. XML_GetCurrentLineNumber(parser));
  575. graphviz_exit(1);
  576. }
  577. } while (!done);
  578. XML_ParserFree(parser);
  579. freeUserdata(udata);
  580. graph_stack_free(&Gstack);
  581. return root;
  582. }
  583. #endif