node.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "config.h"
  4. #include "node.h"
  5. static void S_node_unlink(cmark_node *node);
  6. #define NODE_MEM(node) cmark_node_mem(node)
  7. static CMARK_INLINE bool S_is_block(cmark_node *node) {
  8. if (node == NULL) {
  9. return false;
  10. }
  11. return node->type >= CMARK_NODE_FIRST_BLOCK &&
  12. node->type <= CMARK_NODE_LAST_BLOCK;
  13. }
  14. static CMARK_INLINE bool S_is_inline(cmark_node *node) {
  15. if (node == NULL) {
  16. return false;
  17. }
  18. return node->type >= CMARK_NODE_FIRST_INLINE &&
  19. node->type <= CMARK_NODE_LAST_INLINE;
  20. }
  21. static bool S_can_contain(cmark_node *node, cmark_node *child) {
  22. cmark_node *cur;
  23. if (node == NULL || child == NULL) {
  24. return false;
  25. }
  26. // Verify that child is not an ancestor of node or equal to node.
  27. cur = node;
  28. do {
  29. if (cur == child) {
  30. return false;
  31. }
  32. cur = cur->parent;
  33. } while (cur != NULL);
  34. if (child->type == CMARK_NODE_DOCUMENT) {
  35. return false;
  36. }
  37. switch (node->type) {
  38. case CMARK_NODE_DOCUMENT:
  39. case CMARK_NODE_BLOCK_QUOTE:
  40. case CMARK_NODE_ITEM:
  41. return S_is_block(child) && child->type != CMARK_NODE_ITEM;
  42. case CMARK_NODE_LIST:
  43. return child->type == CMARK_NODE_ITEM;
  44. case CMARK_NODE_CUSTOM_BLOCK:
  45. return true;
  46. case CMARK_NODE_PARAGRAPH:
  47. case CMARK_NODE_HEADING:
  48. case CMARK_NODE_EMPH:
  49. case CMARK_NODE_STRONG:
  50. case CMARK_NODE_LINK:
  51. case CMARK_NODE_IMAGE:
  52. case CMARK_NODE_CUSTOM_INLINE:
  53. return S_is_inline(child);
  54. default:
  55. break;
  56. }
  57. return false;
  58. }
  59. cmark_node *cmark_node_new_with_mem(cmark_node_type type, cmark_mem *mem) {
  60. cmark_node *node = (cmark_node *)mem->calloc(1, sizeof(*node));
  61. cmark_strbuf_init(mem, &node->content, 0);
  62. node->type = (uint16_t)type;
  63. switch (node->type) {
  64. case CMARK_NODE_HEADING:
  65. node->as.heading.level = 1;
  66. break;
  67. case CMARK_NODE_LIST: {
  68. cmark_list *list = &node->as.list;
  69. list->list_type = CMARK_BULLET_LIST;
  70. list->start = 0;
  71. list->tight = false;
  72. break;
  73. }
  74. default:
  75. break;
  76. }
  77. return node;
  78. }
  79. cmark_node *cmark_node_new(cmark_node_type type) {
  80. extern cmark_mem DEFAULT_MEM_ALLOCATOR;
  81. return cmark_node_new_with_mem(type, &DEFAULT_MEM_ALLOCATOR);
  82. }
  83. // Free a cmark_node list and any children.
  84. static void S_free_nodes(cmark_node *e) {
  85. cmark_node *next;
  86. while (e != NULL) {
  87. cmark_strbuf_free(&e->content);
  88. switch (e->type) {
  89. case CMARK_NODE_CODE_BLOCK:
  90. cmark_chunk_free(NODE_MEM(e), &e->as.code.info);
  91. cmark_chunk_free(NODE_MEM(e), &e->as.code.literal);
  92. break;
  93. case CMARK_NODE_TEXT:
  94. case CMARK_NODE_HTML_INLINE:
  95. case CMARK_NODE_CODE:
  96. case CMARK_NODE_HTML_BLOCK:
  97. cmark_chunk_free(NODE_MEM(e), &e->as.literal);
  98. break;
  99. case CMARK_NODE_LINK:
  100. case CMARK_NODE_IMAGE:
  101. cmark_chunk_free(NODE_MEM(e), &e->as.link.url);
  102. cmark_chunk_free(NODE_MEM(e), &e->as.link.title);
  103. break;
  104. case CMARK_NODE_CUSTOM_BLOCK:
  105. case CMARK_NODE_CUSTOM_INLINE:
  106. cmark_chunk_free(NODE_MEM(e), &e->as.custom.on_enter);
  107. cmark_chunk_free(NODE_MEM(e), &e->as.custom.on_exit);
  108. break;
  109. default:
  110. break;
  111. }
  112. if (e->last_child) {
  113. // Splice children into list
  114. e->last_child->next = e->next;
  115. e->next = e->first_child;
  116. }
  117. next = e->next;
  118. NODE_MEM(e)->free(e);
  119. e = next;
  120. }
  121. }
  122. void cmark_node_free(cmark_node *node) {
  123. S_node_unlink(node);
  124. node->next = NULL;
  125. S_free_nodes(node);
  126. }
  127. cmark_node_type cmark_node_get_type(cmark_node *node) {
  128. if (node == NULL) {
  129. return CMARK_NODE_NONE;
  130. } else {
  131. return (cmark_node_type)node->type;
  132. }
  133. }
  134. const char *cmark_node_get_type_string(cmark_node *node) {
  135. if (node == NULL) {
  136. return "NONE";
  137. }
  138. switch (node->type) {
  139. case CMARK_NODE_NONE:
  140. return "none";
  141. case CMARK_NODE_DOCUMENT:
  142. return "document";
  143. case CMARK_NODE_BLOCK_QUOTE:
  144. return "block_quote";
  145. case CMARK_NODE_LIST:
  146. return "list";
  147. case CMARK_NODE_ITEM:
  148. return "item";
  149. case CMARK_NODE_CODE_BLOCK:
  150. return "code_block";
  151. case CMARK_NODE_HTML_BLOCK:
  152. return "html_block";
  153. case CMARK_NODE_CUSTOM_BLOCK:
  154. return "custom_block";
  155. case CMARK_NODE_PARAGRAPH:
  156. return "paragraph";
  157. case CMARK_NODE_HEADING:
  158. return "heading";
  159. case CMARK_NODE_THEMATIC_BREAK:
  160. return "thematic_break";
  161. case CMARK_NODE_TEXT:
  162. return "text";
  163. case CMARK_NODE_SOFTBREAK:
  164. return "softbreak";
  165. case CMARK_NODE_LINEBREAK:
  166. return "linebreak";
  167. case CMARK_NODE_CODE:
  168. return "code";
  169. case CMARK_NODE_HTML_INLINE:
  170. return "html_inline";
  171. case CMARK_NODE_CUSTOM_INLINE:
  172. return "custom_inline";
  173. case CMARK_NODE_EMPH:
  174. return "emph";
  175. case CMARK_NODE_STRONG:
  176. return "strong";
  177. case CMARK_NODE_LINK:
  178. return "link";
  179. case CMARK_NODE_IMAGE:
  180. return "image";
  181. }
  182. return "<unknown>";
  183. }
  184. cmark_node *cmark_node_next(cmark_node *node) {
  185. if (node == NULL) {
  186. return NULL;
  187. } else {
  188. return node->next;
  189. }
  190. }
  191. cmark_node *cmark_node_previous(cmark_node *node) {
  192. if (node == NULL) {
  193. return NULL;
  194. } else {
  195. return node->prev;
  196. }
  197. }
  198. cmark_node *cmark_node_parent(cmark_node *node) {
  199. if (node == NULL) {
  200. return NULL;
  201. } else {
  202. return node->parent;
  203. }
  204. }
  205. cmark_node *cmark_node_first_child(cmark_node *node) {
  206. if (node == NULL) {
  207. return NULL;
  208. } else {
  209. return node->first_child;
  210. }
  211. }
  212. cmark_node *cmark_node_last_child(cmark_node *node) {
  213. if (node == NULL) {
  214. return NULL;
  215. } else {
  216. return node->last_child;
  217. }
  218. }
  219. void *cmark_node_get_user_data(cmark_node *node) {
  220. if (node == NULL) {
  221. return NULL;
  222. } else {
  223. return node->user_data;
  224. }
  225. }
  226. int cmark_node_set_user_data(cmark_node *node, void *user_data) {
  227. if (node == NULL) {
  228. return 0;
  229. }
  230. node->user_data = user_data;
  231. return 1;
  232. }
  233. const char *cmark_node_get_literal(cmark_node *node) {
  234. if (node == NULL) {
  235. return NULL;
  236. }
  237. switch (node->type) {
  238. case CMARK_NODE_HTML_BLOCK:
  239. case CMARK_NODE_TEXT:
  240. case CMARK_NODE_HTML_INLINE:
  241. case CMARK_NODE_CODE:
  242. return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.literal);
  243. case CMARK_NODE_CODE_BLOCK:
  244. return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.literal);
  245. default:
  246. break;
  247. }
  248. return NULL;
  249. }
  250. int cmark_node_set_literal(cmark_node *node, const char *content) {
  251. if (node == NULL) {
  252. return 0;
  253. }
  254. switch (node->type) {
  255. case CMARK_NODE_HTML_BLOCK:
  256. case CMARK_NODE_TEXT:
  257. case CMARK_NODE_HTML_INLINE:
  258. case CMARK_NODE_CODE:
  259. cmark_chunk_set_cstr(NODE_MEM(node), &node->as.literal, content);
  260. return 1;
  261. case CMARK_NODE_CODE_BLOCK:
  262. cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.literal, content);
  263. return 1;
  264. default:
  265. break;
  266. }
  267. return 0;
  268. }
  269. int cmark_node_get_heading_level(cmark_node *node) {
  270. if (node == NULL) {
  271. return 0;
  272. }
  273. switch (node->type) {
  274. case CMARK_NODE_HEADING:
  275. return node->as.heading.level;
  276. default:
  277. break;
  278. }
  279. return 0;
  280. }
  281. int cmark_node_set_heading_level(cmark_node *node, int level) {
  282. if (node == NULL || level < 1 || level > 6) {
  283. return 0;
  284. }
  285. switch (node->type) {
  286. case CMARK_NODE_HEADING:
  287. node->as.heading.level = level;
  288. return 1;
  289. default:
  290. break;
  291. }
  292. return 0;
  293. }
  294. cmark_list_type cmark_node_get_list_type(cmark_node *node) {
  295. if (node == NULL) {
  296. return CMARK_NO_LIST;
  297. }
  298. if (node->type == CMARK_NODE_LIST) {
  299. return node->as.list.list_type;
  300. } else {
  301. return CMARK_NO_LIST;
  302. }
  303. }
  304. int cmark_node_set_list_type(cmark_node *node, cmark_list_type type) {
  305. if (!(type == CMARK_BULLET_LIST || type == CMARK_ORDERED_LIST)) {
  306. return 0;
  307. }
  308. if (node == NULL) {
  309. return 0;
  310. }
  311. if (node->type == CMARK_NODE_LIST) {
  312. node->as.list.list_type = type;
  313. return 1;
  314. } else {
  315. return 0;
  316. }
  317. }
  318. cmark_delim_type cmark_node_get_list_delim(cmark_node *node) {
  319. if (node == NULL) {
  320. return CMARK_NO_DELIM;
  321. }
  322. if (node->type == CMARK_NODE_LIST) {
  323. return node->as.list.delimiter;
  324. } else {
  325. return CMARK_NO_DELIM;
  326. }
  327. }
  328. int cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim) {
  329. if (!(delim == CMARK_PERIOD_DELIM || delim == CMARK_PAREN_DELIM)) {
  330. return 0;
  331. }
  332. if (node == NULL) {
  333. return 0;
  334. }
  335. if (node->type == CMARK_NODE_LIST) {
  336. node->as.list.delimiter = delim;
  337. return 1;
  338. } else {
  339. return 0;
  340. }
  341. }
  342. int cmark_node_get_list_start(cmark_node *node) {
  343. if (node == NULL) {
  344. return 0;
  345. }
  346. if (node->type == CMARK_NODE_LIST) {
  347. return node->as.list.start;
  348. } else {
  349. return 0;
  350. }
  351. }
  352. int cmark_node_set_list_start(cmark_node *node, int start) {
  353. if (node == NULL || start < 0) {
  354. return 0;
  355. }
  356. if (node->type == CMARK_NODE_LIST) {
  357. node->as.list.start = start;
  358. return 1;
  359. } else {
  360. return 0;
  361. }
  362. }
  363. int cmark_node_get_list_tight(cmark_node *node) {
  364. if (node == NULL) {
  365. return 0;
  366. }
  367. if (node->type == CMARK_NODE_LIST) {
  368. return node->as.list.tight;
  369. } else {
  370. return 0;
  371. }
  372. }
  373. int cmark_node_set_list_tight(cmark_node *node, int tight) {
  374. if (node == NULL) {
  375. return 0;
  376. }
  377. if (node->type == CMARK_NODE_LIST) {
  378. node->as.list.tight = tight == 1;
  379. return 1;
  380. } else {
  381. return 0;
  382. }
  383. }
  384. const char *cmark_node_get_fence_info(cmark_node *node) {
  385. if (node == NULL) {
  386. return NULL;
  387. }
  388. if (node->type == CMARK_NODE_CODE_BLOCK) {
  389. return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.info);
  390. } else {
  391. return NULL;
  392. }
  393. }
  394. int cmark_node_set_fence_info(cmark_node *node, const char *info) {
  395. if (node == NULL) {
  396. return 0;
  397. }
  398. if (node->type == CMARK_NODE_CODE_BLOCK) {
  399. cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.info, info);
  400. return 1;
  401. } else {
  402. return 0;
  403. }
  404. }
  405. const char *cmark_node_get_url(cmark_node *node) {
  406. if (node == NULL) {
  407. return NULL;
  408. }
  409. switch (node->type) {
  410. case CMARK_NODE_LINK:
  411. case CMARK_NODE_IMAGE:
  412. return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.url);
  413. default:
  414. break;
  415. }
  416. return NULL;
  417. }
  418. int cmark_node_set_url(cmark_node *node, const char *url) {
  419. if (node == NULL) {
  420. return 0;
  421. }
  422. switch (node->type) {
  423. case CMARK_NODE_LINK:
  424. case CMARK_NODE_IMAGE:
  425. cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.url, url);
  426. return 1;
  427. default:
  428. break;
  429. }
  430. return 0;
  431. }
  432. const char *cmark_node_get_title(cmark_node *node) {
  433. if (node == NULL) {
  434. return NULL;
  435. }
  436. switch (node->type) {
  437. case CMARK_NODE_LINK:
  438. case CMARK_NODE_IMAGE:
  439. return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.title);
  440. default:
  441. break;
  442. }
  443. return NULL;
  444. }
  445. int cmark_node_set_title(cmark_node *node, const char *title) {
  446. if (node == NULL) {
  447. return 0;
  448. }
  449. switch (node->type) {
  450. case CMARK_NODE_LINK:
  451. case CMARK_NODE_IMAGE:
  452. cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.title, title);
  453. return 1;
  454. default:
  455. break;
  456. }
  457. return 0;
  458. }
  459. const char *cmark_node_get_on_enter(cmark_node *node) {
  460. if (node == NULL) {
  461. return NULL;
  462. }
  463. switch (node->type) {
  464. case CMARK_NODE_CUSTOM_INLINE:
  465. case CMARK_NODE_CUSTOM_BLOCK:
  466. return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_enter);
  467. default:
  468. break;
  469. }
  470. return NULL;
  471. }
  472. int cmark_node_set_on_enter(cmark_node *node, const char *on_enter) {
  473. if (node == NULL) {
  474. return 0;
  475. }
  476. switch (node->type) {
  477. case CMARK_NODE_CUSTOM_INLINE:
  478. case CMARK_NODE_CUSTOM_BLOCK:
  479. cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_enter, on_enter);
  480. return 1;
  481. default:
  482. break;
  483. }
  484. return 0;
  485. }
  486. const char *cmark_node_get_on_exit(cmark_node *node) {
  487. if (node == NULL) {
  488. return NULL;
  489. }
  490. switch (node->type) {
  491. case CMARK_NODE_CUSTOM_INLINE:
  492. case CMARK_NODE_CUSTOM_BLOCK:
  493. return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_exit);
  494. default:
  495. break;
  496. }
  497. return NULL;
  498. }
  499. int cmark_node_set_on_exit(cmark_node *node, const char *on_exit) {
  500. if (node == NULL) {
  501. return 0;
  502. }
  503. switch (node->type) {
  504. case CMARK_NODE_CUSTOM_INLINE:
  505. case CMARK_NODE_CUSTOM_BLOCK:
  506. cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_exit, on_exit);
  507. return 1;
  508. default:
  509. break;
  510. }
  511. return 0;
  512. }
  513. int cmark_node_get_start_line(cmark_node *node) {
  514. if (node == NULL) {
  515. return 0;
  516. }
  517. return node->start_line;
  518. }
  519. int cmark_node_get_start_column(cmark_node *node) {
  520. if (node == NULL) {
  521. return 0;
  522. }
  523. return node->start_column;
  524. }
  525. int cmark_node_get_end_line(cmark_node *node) {
  526. if (node == NULL) {
  527. return 0;
  528. }
  529. return node->end_line;
  530. }
  531. int cmark_node_get_end_column(cmark_node *node) {
  532. if (node == NULL) {
  533. return 0;
  534. }
  535. return node->end_column;
  536. }
  537. // Unlink a node without adjusting its next, prev, and parent pointers.
  538. static void S_node_unlink(cmark_node *node) {
  539. if (node == NULL) {
  540. return;
  541. }
  542. if (node->prev) {
  543. node->prev->next = node->next;
  544. }
  545. if (node->next) {
  546. node->next->prev = node->prev;
  547. }
  548. // Adjust first_child and last_child of parent.
  549. cmark_node *parent = node->parent;
  550. if (parent) {
  551. if (parent->first_child == node) {
  552. parent->first_child = node->next;
  553. }
  554. if (parent->last_child == node) {
  555. parent->last_child = node->prev;
  556. }
  557. }
  558. }
  559. void cmark_node_unlink(cmark_node *node) {
  560. S_node_unlink(node);
  561. node->next = NULL;
  562. node->prev = NULL;
  563. node->parent = NULL;
  564. }
  565. int cmark_node_insert_before(cmark_node *node, cmark_node *sibling) {
  566. if (node == NULL || sibling == NULL) {
  567. return 0;
  568. }
  569. if (!node->parent || !S_can_contain(node->parent, sibling)) {
  570. return 0;
  571. }
  572. S_node_unlink(sibling);
  573. cmark_node *old_prev = node->prev;
  574. // Insert 'sibling' between 'old_prev' and 'node'.
  575. if (old_prev) {
  576. old_prev->next = sibling;
  577. }
  578. sibling->prev = old_prev;
  579. sibling->next = node;
  580. node->prev = sibling;
  581. // Set new parent.
  582. cmark_node *parent = node->parent;
  583. sibling->parent = parent;
  584. // Adjust first_child of parent if inserted as first child.
  585. if (parent && !old_prev) {
  586. parent->first_child = sibling;
  587. }
  588. return 1;
  589. }
  590. int cmark_node_insert_after(cmark_node *node, cmark_node *sibling) {
  591. if (node == NULL || sibling == NULL) {
  592. return 0;
  593. }
  594. if (!node->parent || !S_can_contain(node->parent, sibling)) {
  595. return 0;
  596. }
  597. S_node_unlink(sibling);
  598. cmark_node *old_next = node->next;
  599. // Insert 'sibling' between 'node' and 'old_next'.
  600. if (old_next) {
  601. old_next->prev = sibling;
  602. }
  603. sibling->next = old_next;
  604. sibling->prev = node;
  605. node->next = sibling;
  606. // Set new parent.
  607. cmark_node *parent = node->parent;
  608. sibling->parent = parent;
  609. // Adjust last_child of parent if inserted as last child.
  610. if (parent && !old_next) {
  611. parent->last_child = sibling;
  612. }
  613. return 1;
  614. }
  615. int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode) {
  616. if (!cmark_node_insert_before(oldnode, newnode)) {
  617. return 0;
  618. }
  619. cmark_node_unlink(oldnode);
  620. return 1;
  621. }
  622. int cmark_node_prepend_child(cmark_node *node, cmark_node *child) {
  623. if (!S_can_contain(node, child)) {
  624. return 0;
  625. }
  626. S_node_unlink(child);
  627. cmark_node *old_first_child = node->first_child;
  628. child->next = old_first_child;
  629. child->prev = NULL;
  630. child->parent = node;
  631. node->first_child = child;
  632. if (old_first_child) {
  633. old_first_child->prev = child;
  634. } else {
  635. // Also set last_child if node previously had no children.
  636. node->last_child = child;
  637. }
  638. return 1;
  639. }
  640. int cmark_node_append_child(cmark_node *node, cmark_node *child) {
  641. if (!S_can_contain(node, child)) {
  642. return 0;
  643. }
  644. S_node_unlink(child);
  645. cmark_node *old_last_child = node->last_child;
  646. child->next = NULL;
  647. child->prev = old_last_child;
  648. child->parent = node;
  649. node->last_child = child;
  650. if (old_last_child) {
  651. old_last_child->next = child;
  652. } else {
  653. // Also set first_child if node previously had no children.
  654. node->first_child = child;
  655. }
  656. return 1;
  657. }
  658. static void S_print_error(FILE *out, cmark_node *node, const char *elem) {
  659. if (out == NULL) {
  660. return;
  661. }
  662. fprintf(out, "Invalid '%s' in node type %s at %d:%d\n", elem,
  663. cmark_node_get_type_string(node), node->start_line,
  664. node->start_column);
  665. }
  666. int cmark_node_check(cmark_node *node, FILE *out) {
  667. cmark_node *cur;
  668. int errors = 0;
  669. if (!node) {
  670. return 0;
  671. }
  672. cur = node;
  673. for (;;) {
  674. if (cur->first_child) {
  675. if (cur->first_child->prev != NULL) {
  676. S_print_error(out, cur->first_child, "prev");
  677. cur->first_child->prev = NULL;
  678. ++errors;
  679. }
  680. if (cur->first_child->parent != cur) {
  681. S_print_error(out, cur->first_child, "parent");
  682. cur->first_child->parent = cur;
  683. ++errors;
  684. }
  685. cur = cur->first_child;
  686. continue;
  687. }
  688. next_sibling:
  689. if (cur == node) {
  690. break;
  691. }
  692. if (cur->next) {
  693. if (cur->next->prev != cur) {
  694. S_print_error(out, cur->next, "prev");
  695. cur->next->prev = cur;
  696. ++errors;
  697. }
  698. if (cur->next->parent != cur->parent) {
  699. S_print_error(out, cur->next, "parent");
  700. cur->next->parent = cur->parent;
  701. ++errors;
  702. }
  703. cur = cur->next;
  704. continue;
  705. }
  706. if (cur->parent->last_child != cur) {
  707. S_print_error(out, cur->parent, "last_child");
  708. cur->parent->last_child = cur;
  709. ++errors;
  710. }
  711. cur = cur->parent;
  712. goto next_sibling;
  713. }
  714. return errors;
  715. }