write.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /**
  2. * @file
  3. * @brief implements @ref agwrite, @ref agcanon, @ref agstrcanon,
  4. * and @ref agcanonStr
  5. *
  6. * @ingroup cgraph_graph
  7. * @ingroup cgraph_core
  8. */
  9. /*************************************************************************
  10. * Copyright (c) 2011 AT&T Intellectual Property
  11. * All rights reserved. This program and the accompanying materials
  12. * are made available under the terms of the Eclipse Public License v1.0
  13. * which accompanies this distribution, and is available at
  14. * https://www.eclipse.org/legal/epl-v10.html
  15. *
  16. * Contributors: Details at https://graphviz.org
  17. *************************************************************************/
  18. #include <assert.h>
  19. #include <limits.h>
  20. #include <stdbool.h>
  21. #include <stddef.h>
  22. #include <stdio.h> /* need sprintf() */
  23. #include <ctype.h>
  24. #include <cgraph/cghdr.h>
  25. #include <cgraph/gv_ctype.h>
  26. #include <inttypes.h>
  27. #include <util/strcasecmp.h>
  28. #define EMPTY(s) (((s) == 0) || (s)[0] == '\0')
  29. #define MAX(a,b) ((a)>(b)?(a):(b))
  30. #define CHKRV(v) {if ((v) == EOF) return EOF;}
  31. typedef void iochan_t;
  32. static int ioput(Agraph_t * g, iochan_t * ofile, char *str)
  33. {
  34. return AGDISC(g, io)->putstr(ofile, str);
  35. }
  36. #define MAX_OUTPUTLINE 128
  37. #define MIN_OUTPUTLINE 60
  38. static int write_body(Agraph_t * g, iochan_t * ofile);
  39. static int Level;
  40. static int Max_outputline = MAX_OUTPUTLINE;
  41. static Agsym_t *Tailport, *Headport;
  42. static int indent(Agraph_t * g, iochan_t * ofile)
  43. {
  44. int i;
  45. for (i = Level; i > 0; i--)
  46. CHKRV(ioput(g, ofile, "\t"));
  47. return 0;
  48. }
  49. // alphanumeric, '.', '-', or non-ascii; basically, chars used in unquoted ids
  50. static bool is_id_char(char c) {
  51. return gv_isalnum(c) || c == '.' || c == '-' || !isascii(c);
  52. }
  53. // is the prefix of this string a recognized Graphviz escape sequence?
  54. // https://graphviz.org/docs/attr-types/escString/
  55. static bool is_escape(const char *str) {
  56. assert(str != NULL);
  57. if (*str != '\\')
  58. return false;
  59. if (str[1] == 'E')
  60. return true;
  61. if (str[1] == 'G')
  62. return true;
  63. if (str[1] == 'H')
  64. return true;
  65. if (str[1] == 'L')
  66. return true;
  67. if (str[1] == 'N')
  68. return true;
  69. if (str[1] == 'T')
  70. return true;
  71. if (str[1] == 'l')
  72. return true;
  73. if (str[1] == 'n')
  74. return true;
  75. if (str[1] == 'r')
  76. return true;
  77. if (str[1] == '\\')
  78. return true;
  79. if (str[1] == '"')
  80. return true;
  81. return false;
  82. }
  83. /* Canonicalize ordinary strings.
  84. * Assumes buf is large enough to hold output.
  85. */
  86. static char *_agstrcanon(char *arg, char *buf)
  87. {
  88. char *s, *p;
  89. char uc;
  90. int cnt = 0, dotcnt = 0;
  91. bool needs_quotes = false;
  92. bool part_of_escape = false;
  93. bool maybe_num;
  94. bool backslash_pending = false;
  95. static const char *tokenlist[] /* must agree with scan.l */
  96. = { "node", "edge", "strict", "graph", "digraph", "subgraph",
  97. NULL
  98. };
  99. const char **tok;
  100. if (EMPTY(arg))
  101. return "\"\"";
  102. s = arg;
  103. p = buf;
  104. *p++ = '\"';
  105. uc = *s++;
  106. maybe_num = gv_isdigit(uc) || uc == '.' || uc == '-';
  107. while (uc) {
  108. if (uc == '\"' && !part_of_escape) {
  109. *p++ = '\\';
  110. needs_quotes = true;
  111. } else if (!part_of_escape && is_escape(&s[-1])) {
  112. needs_quotes = true;
  113. part_of_escape = true;
  114. } else if (maybe_num) {
  115. if (uc == '-') {
  116. if (cnt) {
  117. maybe_num = false;
  118. needs_quotes = true;
  119. }
  120. }
  121. else if (uc == '.') {
  122. if (dotcnt++) {
  123. maybe_num = false;
  124. needs_quotes = true;
  125. }
  126. }
  127. else if (!gv_isdigit(uc)) {
  128. maybe_num = false;
  129. needs_quotes = true;
  130. }
  131. part_of_escape = false;
  132. }
  133. else if (!(gv_isalnum(uc) || uc == '_' || !isascii(uc))) {
  134. needs_quotes = true;
  135. part_of_escape = false;
  136. } else {
  137. part_of_escape = false;
  138. }
  139. *p++ = uc;
  140. uc = *s++;
  141. cnt++;
  142. /* If breaking long strings into multiple lines, only allow breaks after a non-id char, not a backslash, where the next char is an
  143. * id char.
  144. */
  145. if (Max_outputline) {
  146. if (uc && backslash_pending && !(is_id_char(p[-1]) || p[-1] == '\\') && is_id_char(uc)) {
  147. *p++ = '\\';
  148. *p++ = '\n';
  149. needs_quotes = true;
  150. backslash_pending = false;
  151. cnt = 0;
  152. } else if (uc && (cnt >= Max_outputline)) {
  153. if (!(is_id_char(p[-1]) || p[-1] == '\\') && is_id_char(uc)) {
  154. *p++ = '\\';
  155. *p++ = '\n';
  156. needs_quotes = true;
  157. cnt = 0;
  158. } else {
  159. backslash_pending = true;
  160. }
  161. }
  162. }
  163. }
  164. *p++ = '\"';
  165. *p = '\0';
  166. if (needs_quotes || (cnt == 1 && (*arg == '.' || *arg == '-')))
  167. return buf;
  168. /* Use quotes to protect tokens (example, a node named "node") */
  169. /* It would be great if it were easier to use flex here. */
  170. for (tok = tokenlist; *tok; tok++)
  171. if (!strcasecmp(*tok, arg))
  172. return buf;
  173. return arg;
  174. }
  175. /**
  176. * Canonicalize html strings.
  177. */
  178. static char *agcanonhtmlstr(const char *arg, char *buf)
  179. {
  180. sprintf(buf, "<%s>", arg);
  181. return buf;
  182. }
  183. /**
  184. * canonicalize a string for printing.
  185. * must agree with strings in scan.l
  186. * Unsafe if buffer is not large enough.
  187. */
  188. char *agstrcanon(char *arg, char *buf)
  189. {
  190. if (aghtmlstr(arg))
  191. return agcanonhtmlstr(arg, buf);
  192. else
  193. return _agstrcanon(arg, buf);
  194. }
  195. static char *getoutputbuffer(const char *str)
  196. {
  197. static char *rv;
  198. static size_t len = 0;
  199. size_t req;
  200. req = MAX(2 * strlen(str) + 2, BUFSIZ);
  201. if (req > len) {
  202. char *r = realloc(rv, req);
  203. if (r == NULL)
  204. return NULL;
  205. rv = r;
  206. len = req;
  207. }
  208. return rv;
  209. }
  210. /**
  211. * canonicalize a string for printing.
  212. * must agree with strings in scan.l
  213. * Shared static buffer - unsafe.
  214. */
  215. char *agcanonStr(char *str)
  216. {
  217. char *buffer = getoutputbuffer(str);
  218. if (buffer == NULL)
  219. return NULL;
  220. return agstrcanon(str, buffer);
  221. }
  222. /**
  223. * canonicalize a string for printing.
  224. * If html is true, use HTML canonicalization.
  225. * Shared static buffer - unsafe.
  226. */
  227. char *agcanon(char *str, int html)
  228. {
  229. char* buf = getoutputbuffer(str);
  230. if (buf == NULL)
  231. return NULL;
  232. if (html)
  233. return agcanonhtmlstr(str, buf);
  234. else
  235. return _agstrcanon(str, buf);
  236. }
  237. static int _write_canonstr(Agraph_t *g, iochan_t *ofile, char *str, bool chk) {
  238. if (chk) {
  239. str = agcanonStr(str);
  240. } else {
  241. char *buffer = getoutputbuffer(str);
  242. if (buffer == NULL)
  243. return EOF;
  244. str = _agstrcanon(str, buffer);
  245. }
  246. return ioput(g, ofile, str);
  247. }
  248. static int write_canonstr(Agraph_t * g, iochan_t * ofile, char *str)
  249. {
  250. char *s;
  251. /* str may not have been allocated by agstrdup, so we first need to turn it
  252. * into a valid refstr
  253. */
  254. s = agstrdup(g, str);
  255. int r = _write_canonstr(g, ofile, s, true);
  256. agstrfree(g, s);
  257. return r;
  258. }
  259. static int write_dict(Agraph_t * g, iochan_t * ofile, char *name,
  260. Dict_t * dict, bool top) {
  261. int cnt = 0;
  262. Dict_t *view;
  263. Agsym_t *sym, *psym;
  264. if (!top)
  265. view = dtview(dict, NULL);
  266. else
  267. view = 0;
  268. for (sym = dtfirst(dict); sym; sym = dtnext(dict, sym)) {
  269. if (EMPTY(sym->defval) && !sym->print) { /* try to skip empty str (default) */
  270. if (view == NULL)
  271. continue; /* no parent */
  272. psym = dtsearch(view, sym);
  273. assert(psym);
  274. if (EMPTY(psym->defval) && psym->print)
  275. continue; /* also empty in parent */
  276. }
  277. if (cnt++ == 0) {
  278. CHKRV(indent(g, ofile));
  279. CHKRV(ioput(g, ofile, name));
  280. CHKRV(ioput(g, ofile, " ["));
  281. Level++;
  282. } else {
  283. CHKRV(ioput(g, ofile, ",\n"));
  284. CHKRV(indent(g, ofile));
  285. }
  286. CHKRV(write_canonstr(g, ofile, sym->name));
  287. CHKRV(ioput(g, ofile, "="));
  288. CHKRV(write_canonstr(g, ofile, sym->defval));
  289. }
  290. if (cnt > 0) {
  291. Level--;
  292. if (cnt > 1) {
  293. CHKRV(ioput(g, ofile, "\n"));
  294. CHKRV(indent(g, ofile));
  295. }
  296. CHKRV(ioput(g, ofile, "];\n"));
  297. }
  298. if (!top)
  299. dtview(dict, view); /* restore previous view */
  300. return 0;
  301. }
  302. static int write_dicts(Agraph_t *g, iochan_t *ofile, bool top) {
  303. Agdatadict_t *def;
  304. if ((def = agdatadict(g, false))) {
  305. CHKRV(write_dict(g, ofile, "graph", def->dict.g, top));
  306. CHKRV(write_dict(g, ofile, "node", def->dict.n, top));
  307. CHKRV(write_dict(g, ofile, "edge", def->dict.e, top));
  308. }
  309. return 0;
  310. }
  311. static int write_hdr(Agraph_t *g, iochan_t *ofile, bool top) {
  312. char *name, *sep, *kind, *strict;
  313. bool root = false;
  314. bool hasName = true;
  315. strict = "";
  316. if (!top && agparent(g))
  317. kind = "sub";
  318. else {
  319. root = true;
  320. if (g->desc.directed)
  321. kind = "di";
  322. else
  323. kind = "";
  324. if (agisstrict(g))
  325. strict = "strict ";
  326. Tailport = agattr(g, AGEDGE, TAILPORT_ID, NULL);
  327. Headport = agattr(g, AGEDGE, HEADPORT_ID, NULL);
  328. }
  329. name = agnameof(g);
  330. sep = " ";
  331. if (!name || name[0] == LOCALNAMEPREFIX) {
  332. sep = name = "";
  333. hasName = false;
  334. }
  335. CHKRV(indent(g, ofile));
  336. CHKRV(ioput(g, ofile, strict));
  337. /* output "<kind>graph" only for root graphs or graphs with names */
  338. if (root || hasName) {
  339. CHKRV(ioput(g, ofile, kind));
  340. CHKRV(ioput(g, ofile, "graph "));
  341. }
  342. if (hasName)
  343. CHKRV(write_canonstr(g, ofile, name));
  344. CHKRV(ioput(g, ofile, sep));
  345. CHKRV(ioput(g, ofile, "{\n"));
  346. Level++;
  347. CHKRV(write_dicts(g, ofile, top));
  348. AGATTRWF(g) = true;
  349. return 0;
  350. }
  351. static int write_trl(Agraph_t * g, iochan_t * ofile)
  352. {
  353. (void)g;
  354. Level--;
  355. CHKRV(indent(g, ofile));
  356. CHKRV(ioput(g, ofile, "}\n"));
  357. return 0;
  358. }
  359. /// is this graph unnamed?
  360. ///
  361. /// @param g Graph to inspect
  362. /// @return True if this graph was given no explicit name
  363. static bool is_anonymous(Agraph_t *g) {
  364. assert(g != NULL);
  365. // handle the common case inline for performance
  366. if (AGDISC(g, id) == &AgIdDisc) {
  367. // replicate `idprint`
  368. const IDTYPE id = AGID(g);
  369. if (id % 2 != 0) {
  370. return true;
  371. }
  372. return *(char *)(uintptr_t)id == LOCALNAMEPREFIX;
  373. }
  374. const char *const name = agnameof(g);
  375. return name == NULL || name[0] == LOCALNAMEPREFIX;
  376. }
  377. static bool irrelevant_subgraph(Agraph_t * g)
  378. {
  379. int i, n;
  380. Agattr_t *sdata, *pdata, *rdata;
  381. Agdatadict_t *dd;
  382. if (!is_anonymous(g))
  383. return false;
  384. if ((sdata = agattrrec(g)) && (pdata = agattrrec(agparent(g)))) {
  385. rdata = agattrrec(agroot(g));
  386. n = dtsize(rdata->dict);
  387. for (i = 0; i < n; i++)
  388. if (sdata->str[i] && pdata->str[i]
  389. && strcmp(sdata->str[i], pdata->str[i]))
  390. return false;
  391. }
  392. dd = agdatadict(g, false);
  393. if (!dd)
  394. return true;
  395. if (dtsize(dd->dict.n) > 0 || dtsize(dd->dict.e) > 0)
  396. return false;
  397. return true;
  398. }
  399. static bool node_in_subg(Agraph_t * g, Agnode_t * n)
  400. {
  401. Agraphs_t *subgs = g_seq2(g);
  402. for (size_t i = 0; i < Agraphs_size(subgs); ++i) {
  403. Agraph_t *subg = Agraphs_get(subgs, i);
  404. if (irrelevant_subgraph(subg))
  405. continue;
  406. if (agsubnode(subg, n, 0))
  407. return true;
  408. }
  409. return false;
  410. }
  411. static bool has_no_edges(Agraph_t * g, Agnode_t * n)
  412. {
  413. return agfstin(g, n) == NULL && agfstout(g, n) == NULL;
  414. }
  415. static bool has_no_predecessor_below(Agraph_t * g, Agnode_t * n,
  416. uint64_t val)
  417. {
  418. Agedge_t *e;
  419. if (AGSEQ(n) < val)
  420. return false;
  421. for (e = agfstin(g, n); e; e = agnxtin(g, e))
  422. if (AGSEQ(e->node) < val)
  423. return false;
  424. return true;
  425. }
  426. static bool not_default_attrs(Agraph_t * g, Agnode_t * n)
  427. {
  428. Agattr_t *data;
  429. Agsym_t *sym;
  430. (void)g;
  431. if ((data = agattrrec(n))) {
  432. for (sym = dtfirst(data->dict); sym; sym = dtnext(data->dict, sym)) {
  433. if (data->str[sym->id] != sym->defval)
  434. return true;
  435. }
  436. }
  437. return false;
  438. }
  439. static int write_subgs(Agraph_t * g, iochan_t * ofile)
  440. {
  441. Agraph_t *subg;
  442. for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
  443. if (irrelevant_subgraph(subg)) {
  444. write_subgs(subg, ofile);
  445. }
  446. else {
  447. CHKRV(write_hdr(subg, ofile, false));
  448. CHKRV(write_body(subg, ofile));
  449. CHKRV(write_trl(subg, ofile));
  450. }
  451. }
  452. return 0;
  453. }
  454. static int write_edge_name(Agedge_t *e, iochan_t *ofile, bool terminate) {
  455. char *p;
  456. Agraph_t *g;
  457. p = agnameof(e);
  458. g = agraphof(e);
  459. if (!EMPTY(p)) {
  460. if (!terminate) {
  461. Level++;
  462. }
  463. CHKRV(ioput(g, ofile, "\t[key="));
  464. CHKRV(write_canonstr(g, ofile, p));
  465. if (terminate)
  466. CHKRV(ioput(g, ofile, "]"));
  467. return 1;
  468. }
  469. return 0;
  470. }
  471. static int write_nondefault_attrs(void *obj, iochan_t * ofile,
  472. Dict_t * defdict)
  473. {
  474. Agattr_t *data;
  475. Agsym_t *sym;
  476. Agraph_t *g;
  477. int cnt = 0;
  478. int rv;
  479. if (AGTYPE(obj) == AGINEDGE || AGTYPE(obj) == AGOUTEDGE) {
  480. CHKRV(rv = write_edge_name(obj, ofile, false));
  481. if (rv)
  482. cnt++;
  483. }
  484. data = agattrrec(obj);
  485. g = agraphof(obj);
  486. if (data)
  487. for (sym = dtfirst(defdict); sym; sym = dtnext(defdict, sym)) {
  488. if (AGTYPE(obj) == AGINEDGE || AGTYPE(obj) == AGOUTEDGE) {
  489. if (Tailport && sym->id == Tailport->id)
  490. continue;
  491. if (Headport && sym->id == Headport->id)
  492. continue;
  493. }
  494. if (data->str[sym->id] != sym->defval) {
  495. if (cnt++ == 0) {
  496. CHKRV(ioput(g, ofile, "\t["));
  497. Level++;
  498. } else {
  499. CHKRV(ioput(g, ofile, ",\n"));
  500. CHKRV(indent(g, ofile));
  501. }
  502. CHKRV(write_canonstr(g, ofile, sym->name));
  503. CHKRV(ioput(g, ofile, "="));
  504. CHKRV(write_canonstr(g, ofile, data->str[sym->id]));
  505. }
  506. }
  507. if (cnt > 0) {
  508. CHKRV(ioput(g, ofile, "]"));
  509. Level--;
  510. }
  511. AGATTRWF(obj) = true;
  512. return 0;
  513. }
  514. static int write_nodename(Agnode_t * n, iochan_t * ofile)
  515. {
  516. char *name;
  517. Agraph_t *g;
  518. name = agnameof(n);
  519. g = agraphof(n);
  520. if (name) {
  521. CHKRV(write_canonstr(g, ofile, name));
  522. } else {
  523. char buf[sizeof("__SUSPECT") + 20];
  524. snprintf(buf, sizeof(buf), "_%" PRIu64 "_SUSPECT", AGID(n)); /* could be deadly wrong */
  525. CHKRV(ioput(g, ofile, buf));
  526. }
  527. return 0;
  528. }
  529. static int attrs_written(void *obj)
  530. {
  531. return AGATTRWF(obj);
  532. }
  533. static int write_node(Agnode_t * n, iochan_t * ofile, Dict_t * d)
  534. {
  535. Agraph_t *g;
  536. g = agraphof(n);
  537. CHKRV(indent(g, ofile));
  538. CHKRV(write_nodename(n, ofile));
  539. if (!attrs_written(n))
  540. CHKRV(write_nondefault_attrs(n, ofile, d));
  541. return ioput(g, ofile, ";\n");
  542. }
  543. /* node must be written if it wasn't already emitted because of
  544. * a subgraph or one of its predecessors, and if it is a singleton
  545. * or has non-default attributes.
  546. */
  547. static bool write_node_test(Agraph_t * g, Agnode_t * n,
  548. uint64_t pred_id)
  549. {
  550. if (has_no_predecessor_below(g, n, pred_id) && !node_in_subg(g, n)) {
  551. if (has_no_edges(g, n) || not_default_attrs(g, n))
  552. return true;
  553. }
  554. return false;
  555. }
  556. static int write_port(Agedge_t * e, iochan_t * ofile, Agsym_t * port)
  557. {
  558. char *val;
  559. Agraph_t *g;
  560. if (!port)
  561. return 0;
  562. g = agraphof(e);
  563. val = agxget(e, port);
  564. if (val[0] == '\0')
  565. return 0;
  566. CHKRV(ioput(g, ofile, ":"));
  567. if (aghtmlstr(val)) {
  568. CHKRV(write_canonstr(g, ofile, val));
  569. } else {
  570. char *s = strchr(val, ':');
  571. if (s) {
  572. *s = '\0';
  573. CHKRV(_write_canonstr(g, ofile, val, false));
  574. CHKRV(ioput(g, ofile, ":"));
  575. CHKRV(_write_canonstr(g, ofile, s + 1, false));
  576. *s = ':';
  577. } else {
  578. CHKRV(_write_canonstr(g, ofile, val, false));
  579. }
  580. }
  581. return 0;
  582. }
  583. static bool write_edge_test(Agraph_t *g, Agedge_t *e) {
  584. Agraphs_t *subgs = g_seq2(g);
  585. /* can use agedge() because we subverted the dict compar_f */
  586. for (size_t i = 0; i < Agraphs_size(subgs); ++i) {
  587. Agraph_t *subg = Agraphs_get(subgs, i);
  588. if (irrelevant_subgraph(subg))
  589. continue;
  590. if (agsubedge(subg, e, 0))
  591. return false;
  592. }
  593. return true;
  594. }
  595. static int write_edge(Agedge_t * e, iochan_t * ofile, Dict_t * d)
  596. {
  597. Agnode_t *t, *h;
  598. Agraph_t *g;
  599. t = AGTAIL(e);
  600. h = AGHEAD(e);
  601. g = agraphof(t);
  602. CHKRV(indent(g, ofile));
  603. CHKRV(write_nodename(t, ofile));
  604. CHKRV(write_port(e, ofile, Tailport));
  605. CHKRV(ioput(g, ofile, (agisdirected(agraphof(t)) ? " -> " : " -- ")));
  606. CHKRV(write_nodename(h, ofile));
  607. CHKRV(write_port(e, ofile, Headport));
  608. if (!attrs_written(e)) {
  609. CHKRV(write_nondefault_attrs(e, ofile, d));
  610. } else {
  611. CHKRV(write_edge_name(e, ofile, true));
  612. }
  613. return ioput(g, ofile, ";\n");
  614. }
  615. static int write_body(Agraph_t * g, iochan_t * ofile)
  616. {
  617. Agnode_t *n, *prev;
  618. Agedge_t *e;
  619. Agdatadict_t *dd;
  620. CHKRV(write_subgs(g, ofile));
  621. dd = agdatadict(g, false);
  622. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  623. if (write_node_test(g, n, AGSEQ(n)))
  624. CHKRV(write_node(n, ofile, dd ? dd->dict.n : 0));
  625. prev = n;
  626. for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
  627. if (prev != aghead(e) && write_node_test(g, aghead(e), AGSEQ(n))) {
  628. CHKRV(write_node(aghead(e), ofile, dd ? dd->dict.n : 0));
  629. prev = aghead(e);
  630. }
  631. if (write_edge_test(g, e))
  632. CHKRV(write_edge(e, ofile, dd ? dd->dict.e : 0));
  633. }
  634. }
  635. return 0;
  636. }
  637. static void set_attrwf(Agraph_t * g, bool toplevel, bool value)
  638. {
  639. Agraph_t *subg;
  640. Agnode_t *n;
  641. Agedge_t *e;
  642. AGATTRWF(g) = value;
  643. for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
  644. set_attrwf(subg, false, value);
  645. }
  646. if (toplevel) {
  647. for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
  648. AGATTRWF(n) = value;
  649. for (e = agfstout(g, n); e; e = agnxtout(g, e))
  650. AGATTRWF(e) = value;
  651. }
  652. }
  653. }
  654. /// Return 0 on success, EOF on failure
  655. int agwrite(Agraph_t * g, void *ofile)
  656. {
  657. char* s;
  658. Level = 0; /* re-initialize tab level */
  659. s = agget(g, "linelength");
  660. if (s != NULL && gv_isdigit(*s)) {
  661. unsigned long len = strtoul(s, NULL, 10);
  662. if ((len == 0 || len >= MIN_OUTPUTLINE) && len <= (unsigned long)INT_MAX)
  663. Max_outputline = (int)len;
  664. }
  665. set_attrwf(g, true, false);
  666. CHKRV(write_hdr(g, ofile, true));
  667. CHKRV(write_body(g, ofile));
  668. CHKRV(write_trl(g, ofile));
  669. Max_outputline = MAX_OUTPUTLINE;
  670. return AGDISC(g, io)->flush(ofile);
  671. }