gml_parser.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Copyright 2021 籽籮籮 籵籮籮籯类籲籷籰
  3. * (C) Universitaet Passau 1986-1991
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * These are the four essential freedoms with GNU GPL software:
  18. * 1: freedom to run the program, for any purpose
  19. * 2: freedom to study how the program works, and change it to make it do what you wish
  20. * 3: freedom to redistribute copies to help your Free Software friends
  21. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  22. * , ,
  23. * / \
  24. * ((__-^^-,-^^-__))
  25. * `-_---' `---_-'
  26. * `--|o` 'o|--'
  27. * \ ` /
  28. * ): :(
  29. * :o_o:
  30. * "-"
  31. */
  32. /*
  33. * id number as string as in node/edge example id "4" now accepted
  34. * because yed and other gml tool does accept it too.
  35. */
  36. /*
  37. * parser for the GML-file-format specified in:
  38. * this file has 1 memory leak
  39. * splay tree is used to track memory and free all at end.
  40. */
  41. #include "config.h"
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <assert.h>
  45. #include <string.h>
  46. #include <strings.h>
  47. #include <errno.h>
  48. #include "main.h"
  49. #include "hier.h"
  50. #include "uniqstr.h"
  51. #include "gml_scanner.h"
  52. #include "gml_parser.h"
  53. #include "splay-tree.h"
  54. /* struct for node graphics */
  55. struct nodegr
  56. {
  57. int foundncolor;
  58. int ncolor;
  59. };
  60. /* struct for edge graphics */
  61. struct edgegr
  62. {
  63. int foundecolor;
  64. int ecolor;
  65. };
  66. static splay_tree gmlparser_splaytree = NULL;
  67. struct GML_pair *
  68. GML_parser (FILE * source, struct GML_stat *stat, int open)
  69. {
  70. struct GML_token token;
  71. struct GML_pair *pair;
  72. struct GML_pair *list;
  73. struct GML_pair *tmp = NULL;
  74. struct GML_list_elem *tmp_elem;
  75. assert (stat);
  76. pair = (struct GML_pair *) gmlparser_calloc (1, sizeof (struct GML_pair));
  77. list = pair;
  78. for (;;)
  79. {
  80. token = GML_scanner (source);
  81. if (token.kind == GML_END)
  82. {
  83. if (open)
  84. {
  85. stat->err.err_num = GML_OPEN_BRACKET;
  86. stat->err.line = GML_line;
  87. stat->err.column = GML_column;
  88. gmlparser_free (pair);
  89. if (tmp == NULL)
  90. {
  91. return NULL;
  92. }
  93. else
  94. {
  95. tmp->next = NULL;
  96. return list;
  97. }
  98. }
  99. break;
  100. }
  101. else if (token.kind == GML_R_BRACKET)
  102. {
  103. if (!open)
  104. {
  105. stat->err.err_num = GML_TOO_MANY_BRACKETS;
  106. stat->err.line = GML_line;
  107. stat->err.column = GML_column;
  108. gmlparser_free (pair);
  109. if (tmp == NULL)
  110. {
  111. return NULL;
  112. }
  113. else
  114. {
  115. tmp->next = NULL;
  116. return list;
  117. }
  118. }
  119. break;
  120. }
  121. else if (token.kind == GML_ERROR)
  122. {
  123. stat->err.err_num = token.value.err.err_num;
  124. stat->err.line = token.value.err.line;
  125. stat->err.column = token.value.err.column;
  126. gmlparser_free (pair);
  127. if (tmp == NULL)
  128. {
  129. return NULL;
  130. }
  131. else
  132. {
  133. tmp->next = NULL;
  134. return list;
  135. }
  136. }
  137. else if (token.kind != GML_KEY)
  138. {
  139. stat->err.err_num = GML_SYNTAX;
  140. stat->err.line = GML_line;
  141. stat->err.column = GML_column;
  142. gmlparser_free (pair);
  143. if (token.kind == GML_STRING)
  144. {
  145. gmlparser_free (token.value.string);
  146. }
  147. if (tmp == NULL)
  148. {
  149. return NULL;
  150. }
  151. else
  152. {
  153. tmp->next = NULL;
  154. return list;
  155. }
  156. }
  157. if (!stat->key_list)
  158. {
  159. stat->key_list = (struct GML_list_elem *)
  160. gmlparser_calloc (1, sizeof (struct GML_list_elem));
  161. stat->key_list->next = NULL;
  162. stat->key_list->key = token.value.string;
  163. pair->key = token.value.string;
  164. }
  165. else
  166. {
  167. tmp_elem = stat->key_list;
  168. while (tmp_elem)
  169. {
  170. if (!strcmp (tmp_elem->key, token.value.string))
  171. {
  172. gmlparser_free (token.value.string);
  173. pair->key = tmp_elem->key;
  174. break;
  175. }
  176. tmp_elem = tmp_elem->next;
  177. }
  178. if (!tmp_elem)
  179. {
  180. /* #warning "memleak here " */
  181. tmp_elem = (struct GML_list_elem *)
  182. gmlparser_calloc (1, sizeof (struct GML_list_elem));
  183. tmp_elem->next = stat->key_list;
  184. stat->key_list = tmp_elem;
  185. tmp_elem->key = token.value.string;
  186. pair->key = token.value.string;
  187. }
  188. }
  189. token = GML_scanner (source);
  190. switch (token.kind)
  191. {
  192. case GML_INT:
  193. pair->kind = GML_INT;
  194. pair->value.integer = token.value.integer;
  195. break;
  196. case GML_DOUBLE:
  197. pair->kind = GML_DOUBLE;
  198. pair->value.floating = token.value.floating;
  199. break;
  200. case GML_STRING:
  201. pair->kind = GML_STRING;
  202. pair->value.string = token.value.string;
  203. break;
  204. case GML_L_BRACKET:
  205. pair->kind = GML_LIST;
  206. pair->value.list = GML_parser (source, stat, 1);
  207. if (stat->err.err_num != GML_OK)
  208. {
  209. return list;
  210. }
  211. break;
  212. case GML_ERROR:
  213. stat->err.err_num = token.value.err.err_num;
  214. stat->err.line = token.value.err.line;
  215. stat->err.column = token.value.err.column;
  216. gmlparser_free (pair);
  217. if (tmp == NULL)
  218. {
  219. return NULL;
  220. }
  221. else
  222. {
  223. tmp->next = NULL;
  224. return list;
  225. }
  226. default:
  227. stat->err.line = GML_line;
  228. stat->err.column = GML_column;
  229. stat->err.err_num = GML_SYNTAX;
  230. gmlparser_free (pair);
  231. if (tmp == NULL)
  232. {
  233. return NULL;
  234. }
  235. else
  236. {
  237. tmp->next = NULL;
  238. return list;
  239. }
  240. }
  241. tmp = pair;
  242. pair =
  243. (struct GML_pair *) gmlparser_calloc (1, sizeof (struct GML_pair));
  244. tmp->next = pair;
  245. }
  246. stat->err.err_num = GML_OK;
  247. gmlparser_free (pair);
  248. if (tmp == NULL)
  249. {
  250. return NULL;
  251. }
  252. else
  253. {
  254. tmp->next = NULL;
  255. return list;
  256. }
  257. }
  258. void
  259. GML_free_list (struct GML_pair *list, struct GML_list_elem *keys)
  260. {
  261. if (list)
  262. {
  263. }
  264. if (keys)
  265. {
  266. }
  267. /* old now replaced using splaytree
  268. struct GML_pair *tmp = list;
  269. struct GML_list_elem *tmp_key;
  270. while (keys)
  271. {
  272. gmlparser_free (keys->key);
  273. tmp_key = keys->next;
  274. gmlparser_free (keys);
  275. keys = tmp_key;
  276. }
  277. while (list)
  278. {
  279. switch (list->kind)
  280. {
  281. case GML_LIST:
  282. GML_free_list (list->value.list, NULL);
  283. break;
  284. case GML_STRING:
  285. gmlparser_free (list->value.string);
  286. break;
  287. default:
  288. break;
  289. }
  290. tmp = list->next;
  291. gmlparser_free (list);
  292. list = tmp;
  293. }
  294. */
  295. gmlparser_splaytree = splay_tree_delete (gmlparser_splaytree);
  296. return;
  297. }
  298. void
  299. GML_print_list (struct GML_pair *list, int level)
  300. {
  301. struct GML_pair *tmp = list;
  302. int i;
  303. while (tmp)
  304. {
  305. for (i = 0; i < level; i++)
  306. {
  307. printf (" ");
  308. }
  309. printf ("*KEY* : %s", tmp->key);
  310. switch (tmp->kind)
  311. {
  312. case GML_INT:
  313. printf (" *VALUE* (long) : %ld \n", tmp->value.integer);
  314. break;
  315. case GML_DOUBLE:
  316. printf (" *VALUE* (double) : %f \n", tmp->value.floating);
  317. break;
  318. case GML_STRING:
  319. printf (" *VALUE* (string) : %s \n", tmp->value.string);
  320. break;
  321. case GML_LIST:
  322. printf (" *VALUE* (list) : \n");
  323. GML_print_list (tmp->value.list, level + 1);
  324. break;
  325. default:
  326. printf ("Unknown kind %d\n", tmp->kind);
  327. break;
  328. }
  329. tmp = tmp->next;
  330. }
  331. return;
  332. }
  333. /* memory wrapping */
  334. void
  335. gmlparser_free (void *ptr)
  336. {
  337. if (ptr)
  338. {
  339. splay_tree_remove (gmlparser_splaytree, (splay_tree_value) ptr);
  340. }
  341. return;
  342. }
  343. /* tracking memory allocations in the splay */
  344. void *
  345. gmlparser_calloc (int nn, size_t sz)
  346. {
  347. void *ret;
  348. ret = calloc (nn, sz);
  349. if (gmlparser_splaytree == NULL)
  350. {
  351. gmlparser_splaytree =
  352. splay_tree_new (splay_tree_compare_pointers, splay_tree_free_key,
  353. NULL);
  354. }
  355. splay_tree_insert (gmlparser_splaytree, (splay_tree_value) ret, 0);
  356. return (ret);
  357. }
  358. /************* customize after this **************/
  359. /* parse a list with edge list unknown */
  360. static void
  361. GT_parse_list_edge_list (struct GML_pair *pairs)
  362. {
  363. if (pairs)
  364. {
  365. }
  366. return;
  367. }
  368. /* parse a list with edge graphics */
  369. static struct edgegr *
  370. GT_parse_list_edge_graphics (struct GML_pair *pairs)
  371. {
  372. struct edgegr *egr;
  373. int n = 0;
  374. unsigned int red = 0;
  375. unsigned int green = 0;
  376. unsigned int blue = 0;
  377. int ecolor;
  378. int foundecolor;
  379. ecolor = -1;
  380. foundecolor = 0;
  381. egr = NULL;
  382. while (pairs)
  383. {
  384. switch (pairs->kind)
  385. {
  386. case GML_INT:
  387. break;
  388. case GML_DOUBLE:
  389. break;
  390. case GML_STRING:
  391. /* color of edge */
  392. if (strcasecmp (pairs->key, "fill") == 0)
  393. {
  394. /* option here to allow agrb too */
  395. n =
  396. sscanf (pairs->value.string, "#%02x%02x%02x", &red, &green,
  397. &blue);
  398. if (n == 3)
  399. {
  400. ecolor = ((red << 16) | (green << 8) | blue);
  401. foundecolor = 1;
  402. }
  403. else
  404. {
  405. printf
  406. ("%s(): cannot parse edge fillcolor \"%s\" and expected #rrggbb\n",
  407. __func__, pairs->value.string);
  408. }
  409. }
  410. break;
  411. case GML_LIST:
  412. break;
  413. default:
  414. printf ("%s(): shouldnothappen\n", __func__);
  415. break;
  416. }
  417. pairs = pairs->next;
  418. }
  419. if (foundecolor)
  420. {
  421. egr = calloc (1, sizeof (struct edgegr));
  422. egr->foundecolor = foundecolor;
  423. egr->ecolor = ecolor;
  424. return (egr);
  425. }
  426. else
  427. {
  428. return (NULL);
  429. }
  430. }
  431. /* edge data list */
  432. static void
  433. GT_parse_list_edge (struct gml_graph *g, struct GML_pair *pairs)
  434. {
  435. int foundsource = -1;
  436. int foundtarget = -1;
  437. int foundsourceset = 0;
  438. int foundtargetset = 0;
  439. char *elabel = NULL;
  440. struct edgegr *egr = NULL;
  441. int ecolor = 0;
  442. char *strtolhelp = NULL;
  443. long int rs = 0;
  444. ecolor = 0; /* black edge line */
  445. foundsource = -1;
  446. foundtarget = -1;
  447. foundsourceset = 0;
  448. foundtargetset = 0;
  449. egr = NULL; /* optional edge graphics */
  450. elabel = NULL; /* optional edge label */
  451. while (pairs)
  452. {
  453. switch (pairs->kind)
  454. {
  455. case GML_INT:
  456. if (strcasecmp (pairs->key, "source") == 0)
  457. {
  458. foundsource = pairs->value.integer;
  459. foundsourceset++;
  460. }
  461. if (strcasecmp (pairs->key, "target") == 0)
  462. {
  463. foundtarget = pairs->value.integer;
  464. foundtargetset++;
  465. }
  466. break;
  467. case GML_DOUBLE:
  468. break;
  469. case GML_STRING:
  470. if (strcasecmp (pairs->key, "source") == 0)
  471. {
  472. errno = 0;
  473. rs = strtol (pairs->value.string, &strtolhelp, 10);
  474. if (errno || (*strtolhelp) != 0)
  475. {
  476. /* error in number */
  477. printf
  478. ("%s(): string found source id but expecting a number and skipped\n",
  479. __func__);
  480. }
  481. else
  482. {
  483. foundsource = (int) rs;
  484. foundsourceset++;
  485. }
  486. }
  487. if (strcasecmp (pairs->key, "target") == 0)
  488. {
  489. errno = 0;
  490. rs = strtol (pairs->value.string, &strtolhelp, 10);
  491. if (errno || (*strtolhelp) != 0)
  492. {
  493. /* error in number */
  494. printf
  495. ("%s(): string found target id but expecting a number and skipped\n",
  496. __func__);
  497. }
  498. else
  499. {
  500. foundtarget = (int) rs;
  501. foundtargetset++;
  502. }
  503. }
  504. if (strcasecmp (pairs->key, "label") == 0)
  505. {
  506. elabel = uniqstr (pairs->value.string);
  507. }
  508. break;
  509. case GML_LIST:
  510. if (strcasecmp (pairs->key, "graphics") == 0)
  511. {
  512. egr = GT_parse_list_edge_graphics (pairs->value.list);
  513. }
  514. else
  515. {
  516. GT_parse_list_edge_list (pairs->value.list);
  517. }
  518. break;
  519. default:
  520. printf ("%s(): shouldnothappen\n", __func__);
  521. break;
  522. }
  523. /* see if a return from graphics */
  524. if (egr)
  525. {
  526. if (egr->foundecolor)
  527. {
  528. ecolor = egr->ecolor;
  529. }
  530. free (egr);
  531. egr = NULL;
  532. }
  533. pairs = pairs->next;
  534. }
  535. /* a edge must have source and target */
  536. if (foundsourceset == 0 || foundtargetset == 0)
  537. {
  538. printf
  539. ("%s(): edge missing a source and/or target number in edge from %d to %d and skipped\n",
  540. __func__, foundsource, foundtarget);
  541. fflush (stdout);
  542. return;
  543. }
  544. /* check for exactly 1 source and target id in edge,
  545. * this could be a (interesting) feature too. */
  546. if (foundsourceset > 1)
  547. {
  548. printf
  549. ("%s(): edge has multiple source id's in edge from %d to %d and skipped\n",
  550. __func__, foundsource, foundtarget);
  551. fflush (stdout);
  552. return;
  553. }
  554. if (foundtargetset > 1)
  555. {
  556. printf
  557. ("%s(): edge has multiple target id's in edge from %d to %d and skipped\n",
  558. __func__, foundsource, foundtarget);
  559. fflush (stdout);
  560. return;
  561. }
  562. /* this happens in radare2 gml data -- todo */
  563. if (foundsource < 0)
  564. {
  565. printf
  566. ("%s(): edge has negative source number in edge from %d to %d\n",
  567. __func__, foundsource, foundtarget);
  568. fflush (stdout);
  569. }
  570. /* in radare2 data */
  571. if (foundtarget < 0)
  572. {
  573. printf
  574. ("%s(): edge has negative target number in edge from %d to %d\n",
  575. __func__, foundsource, foundtarget);
  576. fflush (stdout);
  577. }
  578. if (foundsource == foundtarget)
  579. {
  580. /* selfedge todo */
  581. printf ("%s(): selfedge in edge from %d to %d\n", __func__,
  582. foundsource, foundtarget);
  583. fflush (stdout);
  584. add_new_edge (g, foundsource, foundtarget, elabel, ecolor, 0, NULL,
  585. NULL);
  586. }
  587. else
  588. {
  589. add_new_edge (g, foundsource, foundtarget, elabel, ecolor, 0, NULL,
  590. NULL);
  591. }
  592. return;
  593. }
  594. /* parse a list with node list unknown */
  595. static void
  596. GT_parse_list_node_list (struct GML_pair *pairs)
  597. {
  598. if (pairs)
  599. {
  600. }
  601. return;
  602. }
  603. /* parse a list with node graphics */
  604. static struct nodegr *
  605. GT_parse_list_node_graphics (struct GML_pair *pairs)
  606. {
  607. struct nodegr *ngr = NULL;
  608. int n = 0;
  609. unsigned int red = 0;
  610. unsigned int green = 0;
  611. unsigned int blue = 0;
  612. int ncolor;
  613. int foundncolor;
  614. ncolor = -1;
  615. foundncolor = 0;
  616. ngr = NULL;
  617. while (pairs)
  618. {
  619. switch (pairs->kind)
  620. {
  621. case GML_INT:
  622. break;
  623. case GML_DOUBLE:
  624. break;
  625. case GML_STRING:
  626. /* color of node */
  627. if (strcasecmp (pairs->key, "fill") == 0)
  628. {
  629. n =
  630. sscanf (pairs->value.string, "#%02x%02x%02x", &red,
  631. &green, &blue);
  632. if (n == 3)
  633. {
  634. ncolor = ((red << 16) | (green << 8) | blue);
  635. foundncolor = 1;
  636. }
  637. else
  638. {
  639. printf ("%s(): cannot parse node fillcolor %s\n",
  640. __func__, pairs->value.string);
  641. }
  642. }
  643. break;
  644. case GML_LIST:
  645. break;
  646. default:
  647. printf ("%s(): shouldnothappen\n", __func__);
  648. break;
  649. }
  650. pairs = pairs->next;
  651. }
  652. if (foundncolor)
  653. {
  654. ngr = calloc (1, sizeof (struct nodegr));
  655. ngr->foundncolor = foundncolor;
  656. ngr->ncolor = ncolor;
  657. return (ngr);
  658. }
  659. else
  660. {
  661. return (NULL);
  662. }
  663. }
  664. static void
  665. GT_parse_list_node (struct gml_graph *g, struct GML_pair *pairs)
  666. {
  667. int ncolor = 0;
  668. int foundid = -1;
  669. int foundidset = 0;
  670. char *nodelabel = NULL;
  671. struct nodegr *ngr = NULL;
  672. char *strtolhelp = NULL;
  673. long int rs = 0;
  674. /* node default white fill color r/g/b */
  675. ncolor = 0x00ffffff;
  676. ngr = NULL;
  677. foundid = -1;
  678. foundidset = 0;
  679. while (pairs)
  680. {
  681. switch (pairs->kind)
  682. {
  683. case GML_INT:
  684. if (strcasecmp (pairs->key, "id") == 0)
  685. {
  686. foundid = pairs->value.integer;
  687. foundidset++;
  688. }
  689. break;
  690. case GML_DOUBLE:
  691. break;
  692. case GML_STRING:
  693. if (strcasecmp (pairs->key, "id") == 0)
  694. {
  695. errno = 0;
  696. rs = strtol (pairs->value.string, &strtolhelp, 10);
  697. if (errno || (*strtolhelp) != 0)
  698. {
  699. /* error in number */
  700. printf
  701. ("%s(): string found at id but expecting a number and skipped\n",
  702. __func__);
  703. }
  704. else
  705. {
  706. foundid = (int) rs;
  707. foundidset++;
  708. }
  709. }
  710. /* if multiple label statements, it's overwritten without warning */
  711. if (strcasecmp (pairs->key, "label") == 0)
  712. {
  713. nodelabel = uniqstr (pairs->value.string);
  714. }
  715. /* instead of label, gv2gml uses name */
  716. if (strcasecmp (pairs->key, "name") == 0)
  717. {
  718. nodelabel = uniqstr (pairs->value.string);
  719. }
  720. break;
  721. case GML_LIST:
  722. if (strcasecmp (pairs->key, "graphics") == 0)
  723. {
  724. ngr = GT_parse_list_node_graphics (pairs->value.list);
  725. }
  726. else
  727. {
  728. GT_parse_list_node_list (pairs->value.list);
  729. }
  730. break;
  731. default:
  732. printf ("%s(): shouldnothappen\n", __func__);
  733. break;
  734. }
  735. /* see if a return from graphics */
  736. if (ngr)
  737. {
  738. /* optional node color */
  739. if (ngr->foundncolor)
  740. {
  741. ncolor = ngr->ncolor;
  742. }
  743. free (ngr);
  744. ngr = NULL;
  745. }
  746. pairs = pairs->next;
  747. }
  748. /* a node must have a id number */
  749. if (foundidset == 0)
  750. {
  751. printf ("%s(): node missing a id number and skipped\n", __func__);
  752. fflush (stdout);
  753. return;
  754. }
  755. /* a node must have a single id number. this can be a feature too. */
  756. if (foundidset > 1)
  757. {
  758. printf ("%s(): node has multiple id numbers and skipped\n", __func__);
  759. fflush (stdout);
  760. return;
  761. }
  762. /* happens in radare2 data */
  763. if (foundid < 0)
  764. {
  765. printf ("%s(): node with negative id found in node %d\n",
  766. __func__, foundid);
  767. fflush (stdout);
  768. }
  769. add_new_node (g, foundid, nodelabel, ncolor, NULL);
  770. return;
  771. }
  772. static void
  773. GT_parse_list (struct gml_graph *g, struct GML_pair *pairs)
  774. {
  775. while (pairs)
  776. {
  777. switch (pairs->kind)
  778. {
  779. case GML_INT:
  780. break;
  781. case GML_DOUBLE:
  782. break;
  783. case GML_STRING:
  784. break;
  785. case GML_LIST:
  786. if (strcasecmp (pairs->key, "node") == 0)
  787. {
  788. GT_parse_list_node (g, pairs->value.list);
  789. }
  790. else if (strcasecmp (pairs->key, "edge") == 0)
  791. {
  792. GT_parse_list_edge (g, pairs->value.list);
  793. }
  794. else
  795. {
  796. GT_parse_list (g, pairs->value.list);
  797. }
  798. break;
  799. default:
  800. printf ("%s(): shouldnothappen\n", __func__);
  801. break;
  802. }
  803. pairs = pairs->next;
  804. }
  805. return;
  806. }
  807. int
  808. gmlparse (struct gml_graph *g, FILE * f, char *fname)
  809. {
  810. struct GML_pair *data = NULL;
  811. struct GML_stat *status = NULL;
  812. status = calloc (1, sizeof (struct GML_stat));
  813. memset (parsermessage, 0, 256);
  814. data = GML_parser (f, status, 0);
  815. if (status->err.err_num != GML_OK)
  816. {
  817. /* parsed not oke */
  818. switch (status->err.err_num)
  819. {
  820. case GML_UNEXPECTED:
  821. snprintf (parsermessage, (256 - 1),
  822. "unexpected error near line %d:%d file %s",
  823. status->err.line, status->err.column, fname);
  824. break;
  825. case GML_SYNTAX:
  826. snprintf (parsermessage, (256 - 1),
  827. "syntax error near line %d:%d file %s",
  828. status->err.line, status->err.column, fname);
  829. break;
  830. case GML_PREMATURE_EOF:
  831. snprintf (parsermessage, (256 - 1),
  832. "early end of file near line %d:%d file %s",
  833. status->err.line, status->err.column, fname);
  834. break;
  835. case GML_TOO_MANY_DIGITS:
  836. snprintf (parsermessage, (256 - 1),
  837. "too many digits near line %d:%d file %s",
  838. status->err.line, status->err.column, fname);
  839. break;
  840. case GML_OPEN_BRACKET:
  841. snprintf (parsermessage, (256 - 1),
  842. "open bracket error near line %d:%d file %s",
  843. status->err.line, status->err.column, fname);
  844. break;
  845. case GML_TOO_MANY_BRACKETS:
  846. snprintf (parsermessage, (256 - 1),
  847. "too many brackets near line %d:%d file %s",
  848. status->err.line, status->err.column, fname);
  849. break;
  850. case GML_OK:
  851. /* parsed oke and no message */
  852. break;
  853. default:
  854. snprintf (parsermessage, (256 - 1),
  855. "unknown error near line %d:%d file %s",
  856. status->err.line, status->err.column, fname);
  857. break;
  858. }
  859. /* in the gui in a message dialog */
  860. printf ("%s(): gml-parsermessage: `%s'\n", __func__, parsermessage);
  861. fflush (stdout);
  862. }
  863. if (0)
  864. {
  865. GML_print_list (data, 0);
  866. }
  867. GT_parse_list (g, data);
  868. GML_free_list (data, 0);
  869. free (status);
  870. if (strlen (parsermessage))
  871. {
  872. /* parse error status */
  873. return (1);
  874. }
  875. else
  876. {
  877. /* oke status */
  878. return (0);
  879. }
  880. return (0);
  881. }
  882. /* end */