api.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /* Copyright 2010, 2011, 2012, 2013, 2014, 2015
  2. Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Avoid namespace conflicts. */
  14. #define context perl_context
  15. #define PERL_NO_GET_CONTEXT
  16. #include "EXTERN.h"
  17. #include "perl.h"
  18. #include "XSUB.h"
  19. #undef context
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include "parser.h"
  23. #include "input.h"
  24. #include "labels.h"
  25. #include "indices.h"
  26. #include "api.h"
  27. #include "errors.h"
  28. ELEMENT *Root;
  29. static void
  30. reset_floats ()
  31. {
  32. floats_number = 0;
  33. }
  34. void
  35. reset_parser (void)
  36. {
  37. debug ("!!!!!!!!!!!!!!!! RESETTING THE PARSER !!!!!!!!!!!!!!!!!!!!!");
  38. wipe_user_commands ();
  39. wipe_values ();
  40. wipe_macros ();
  41. init_index_commands ();
  42. wipe_errors ();
  43. reset_context_stack ();
  44. reset_region_stack ();
  45. reset_floats ();
  46. clear_expanded_formats ();
  47. wipe_global_info ();
  48. reset_internal_xrefs ();
  49. reset_labels ();
  50. input_reset_input_stack ();
  51. reset_conf ();
  52. current_node = current_section = 0;
  53. }
  54. /* Set ROOT to root of tree obtained by parsing FILENAME. */
  55. void
  56. parse_file (char *filename)
  57. {
  58. debug_output = 0;
  59. parse_texi_file (filename);
  60. }
  61. ELEMENT *
  62. get_root (void)
  63. {
  64. return Root;
  65. }
  66. /* Set ROOT to root of tree obtained by parsing the Texinfo code in STRING.
  67. Used for parse_texi_line. */
  68. void
  69. parse_string (char *string)
  70. {
  71. ELEMENT *root;
  72. root = new_element (ET_root_line);
  73. input_push_text (strdup (string), 0);
  74. Root = parse_texi (root);
  75. }
  76. /* Used for parse_texi_text. */
  77. void
  78. parse_text (char *string)
  79. {
  80. ELEMENT *root;
  81. root = new_element (ET_text_root);
  82. input_push_text_with_line_nos (strdup (string), 1);
  83. Root = parse_texi (root);
  84. }
  85. char *
  86. element_type_name (ELEMENT *e)
  87. {
  88. return element_type_names[(e)->type];
  89. }
  90. int
  91. num_contents_children (ELEMENT *e)
  92. {
  93. return e->contents.number;
  94. }
  95. int
  96. num_args_children (ELEMENT *e)
  97. {
  98. return e->args.number;
  99. }
  100. static void element_to_perl_hash (ELEMENT *e);
  101. /* Return reference to Perl array built from e. If any of
  102. the elements in E don't have 'hv' set, set it to an empty
  103. hash table, or create it if route_not_in_tree. */
  104. static SV *
  105. build_perl_array (ELEMENT_LIST *e)
  106. {
  107. SV *sv;
  108. AV *av;
  109. int i;
  110. dTHX;
  111. av = newAV ();
  112. sv = newRV_inc ((SV *) av);
  113. for (i = 0; i < e->number; i++)
  114. {
  115. if (!e->list[i]) /* For arrays only, allow elements to be undef. */
  116. av_push (av, newSV (0));
  117. if (!e->list[i]->hv)
  118. {
  119. if (e->list[i]->parent_type != route_not_in_tree)
  120. e->list[i]->hv = newHV ();
  121. else
  122. {
  123. /* WARNING: This is possibly recursive. */
  124. element_to_perl_hash (e->list[i]);
  125. }
  126. }
  127. av_push (av, newRV_inc ((SV *) e->list[i]->hv));
  128. }
  129. return sv;
  130. }
  131. /* Return reference to hash corresponding to VALUE. */
  132. static SV *
  133. build_node_spec (NODE_SPEC_EXTRA *value)
  134. {
  135. HV *hv;
  136. dTHX;
  137. if (!value->manual_content && !value->node_content)
  138. return newSV(0); /* Perl 'undef' */
  139. hv = newHV ();
  140. if (value->manual_content)
  141. {
  142. hv_store (hv, "manual_content", strlen ("manual_content"),
  143. build_perl_array (&value->manual_content->contents), 0);
  144. }
  145. if (value->node_content)
  146. {
  147. hv_store (hv, "node_content", strlen ("node_content"),
  148. build_perl_array (&value->node_content->contents), 0);
  149. }
  150. return newRV_inc ((SV *)hv);
  151. }
  152. /* Set E->hv and 'hv' on E's descendants. e->parent->hv is assumed
  153. to already exist. */
  154. static void
  155. element_to_perl_hash (ELEMENT *e)
  156. {
  157. SV *sv;
  158. dTHX;
  159. /* e->hv may already exist if there was an extra value elsewhere
  160. referring to e. */
  161. if (!e->hv)
  162. {
  163. e->hv = newHV ();
  164. }
  165. if (e->parent) // && e->parent_type != route_not_in_tree)
  166. {
  167. if (!e->parent->hv)
  168. e->parent->hv = newHV ();
  169. sv = newRV_inc ((SV *) e->parent->hv);
  170. hv_store (e->hv, "parent", strlen ("parent"), sv, 0);
  171. }
  172. /* FIXME: this assumes we don't have nested out-of-tree subtrees,
  173. i.e. the only out-of-tree elements are simple text elements
  174. (or other elements with no children) - otherwise we shall fail
  175. to set "parent" properly. */
  176. /* FIXME: Sometimes extra values have parent set - try to remove this
  177. in the Perl code as well. */
  178. sv = 0;
  179. if (e->cmd == CM_verb)
  180. {
  181. char c = (char) e->type;
  182. if (c)
  183. sv = newSVpv (&c, 1);
  184. else
  185. sv = newSVpv ("", 0);
  186. }
  187. else if (e->type)
  188. {
  189. sv = newSVpv (element_type_names[e->type], 0);
  190. }
  191. if (sv)
  192. hv_store (e->hv, "type", strlen ("type"), sv, 0);
  193. if (e->cmd)
  194. {
  195. sv = newSVpv (command_name(e->cmd), 0);
  196. hv_store (e->hv, "cmdname", strlen ("cmdname"), sv, 0);
  197. /* TODO: Same optimizations as for 'type'. */
  198. }
  199. /* FIXME sort out all these special cases */
  200. if (e->contents.number > 0
  201. || e->type == ET_text_root
  202. || e->type == ET_root_line
  203. || e->type == ET_bracketed
  204. || e->type == ET_bracketed_def_content
  205. || e->type == ET_misc_line_arg
  206. || e->cmd == CM_image // why image?
  207. || e->cmd == CM_item && e->parent && e->parent->type == ET_row
  208. || e->cmd == CM_tab && e->parent && e->parent->type == ET_row
  209. || e->cmd == CM_anchor
  210. || e->cmd == CM_macro
  211. || e->cmd == CM_multitable
  212. || e->type == ET_menu_entry_name
  213. || e->type == ET_brace_command_arg
  214. || e->type == ET_brace_command_context
  215. || e->type == ET_before_item
  216. || e->type == ET_inter_item
  217. || e->cmd == CM_TeX
  218. || e->type == ET_elided
  219. || e->type == ET_elided_block
  220. || e->type == ET_preformatted
  221. || (command_flags(e) & CF_root)
  222. || (command_data(e->cmd).flags & CF_brace
  223. && (command_data(e->cmd).data >= 0
  224. || command_data(e->cmd).data == BRACE_style
  225. || command_data(e->cmd).data == BRACE_context
  226. || command_data(e->cmd).data == BRACE_other
  227. || command_data(e->cmd).data == BRACE_accent
  228. ))
  229. || e->cmd == CM_node) // FIXME special case
  230. // FIXME: Makes no sense to have 'contents' created for glyph commands like
  231. // @arrow{} or for accent commands.
  232. {
  233. AV *av;
  234. int i;
  235. av = newAV ();
  236. sv = newRV_inc ((SV *) av);
  237. hv_store (e->hv, "contents", strlen ("contents"), sv, 0);
  238. for (i = 0; i < e->contents.number; i++)
  239. {
  240. element_to_perl_hash (e->contents.list[i]);
  241. sv = newRV_inc ((SV *) e->contents.list[i]->hv);
  242. av_push (av, sv);
  243. }
  244. }
  245. if (e->args.number > 0)
  246. {
  247. AV *av;
  248. int i;
  249. av = newAV ();
  250. sv = newRV_inc ((SV *) av);
  251. hv_store (e->hv, "args", strlen ("args"), sv, 0);
  252. for (i = 0; i < e->args.number; i++)
  253. {
  254. element_to_perl_hash (e->args.list[i]);
  255. sv = newRV_inc ((SV *) e->args.list[i]->hv);
  256. av_push (av, sv);
  257. }
  258. }
  259. if (e->text.space > 0)
  260. {
  261. sv = newSVpv (e->text.text, e->text.end);
  262. if (e->cmd != CM_value)
  263. hv_store (e->hv, "text", strlen ("text"), sv, 0);
  264. else
  265. hv_store (e->hv, "type", strlen ("type"), sv, 0);
  266. SvUTF8_on (sv);
  267. /* FIXME: Check that the strings we have are in UTF-8 to start with.
  268. This would lead to an unnecessary round trip with "@documentencoding
  269. ISO-8859-1" for Info and plain text output, when we first convert the
  270. characters in the input file to UTF-8, and convert them back again for
  271. the output.
  272. The alternative is to leave the UTF-8 flag off, and hope that Perl
  273. interprets 8-bit encodings like ISO-8859-1 correctly. See
  274. "How does Perl store UTF-8 strings?" in "man perlguts". */
  275. }
  276. if (e->extra_number > 0)
  277. {
  278. HV *extra;
  279. int i;
  280. int all_deleted = 1;
  281. extra = newHV ();
  282. for (i = 0; i < e->extra_number; i++)
  283. {
  284. #define STORE(sv) hv_store (extra, key, strlen (key), sv, 0)
  285. char *key = e->extra[i].key;
  286. ELEMENT *f = e->extra[i].value;
  287. if (e->extra[i].type == extra_deleted)
  288. continue;
  289. all_deleted = 0;
  290. switch (e->extra[i].type)
  291. {
  292. case extra_element:
  293. /* For references to other parts of the tree, create the hash so
  294. we can point to it. */
  295. if (!f->hv)
  296. {
  297. if (f->parent_type != route_not_in_tree)
  298. {
  299. /* TODO: Are there any extra values which are
  300. extra_element that are route_not_in_tree? Consider
  301. eliminating use of 'parent_type' to differentiate types
  302. of extra value. */
  303. f->hv = newHV ();
  304. }
  305. else
  306. element_to_perl_hash (f);
  307. }
  308. STORE(newRV_inc ((SV *)f->hv));
  309. break;
  310. case extra_element_contents:
  311. {
  312. int j;
  313. if (f)
  314. STORE(build_perl_array (&f->contents));
  315. break;
  316. }
  317. case extra_element_contents_array:
  318. {
  319. /* Like extra_element_contents, but this time output an array
  320. of arrays (instead of an array). */
  321. int j, k;
  322. AV *av;
  323. av = newAV ();
  324. STORE(newRV_inc ((SV *)av));
  325. for (j = 0; j < f->contents.number; j++)
  326. {
  327. SV *array;
  328. ELEMENT *g;
  329. g = f->contents.list[j];
  330. if (g)
  331. array = build_perl_array (&g->contents);
  332. else
  333. array = newSV (0); /* undef */
  334. av_push (av, array);
  335. }
  336. break;
  337. }
  338. case extra_string:
  339. { /* A simple string. */
  340. char *value = (char *) f;
  341. if (strcmp (key, "level"))
  342. STORE(newSVpv (value, 0));
  343. else
  344. {
  345. // FIXME: don't use level as a separate key
  346. hv_store (e->hv, key, strlen (key),
  347. newSVpv(value, 0), 0);
  348. }
  349. break;
  350. }
  351. case extra_misc_args:
  352. {
  353. int j;
  354. AV *av;
  355. av = newAV ();
  356. STORE(newRV_inc ((SV *)av));
  357. /* An array of strings. */
  358. for (j = 0; j < f->contents.number; j++)
  359. {
  360. if (f->contents.list[j]->text.end > 0)
  361. {
  362. av_push (av,
  363. newSVpv (f->contents.list[j]->text.text,
  364. f->contents.list[j]->text.end));
  365. }
  366. else
  367. {
  368. /* Empty strings permitted. */
  369. av_push (av,
  370. newSVpv ("", 0));
  371. }
  372. }
  373. break;
  374. }
  375. case extra_node_spec:
  376. /* A complex structure - see "parse_node_manual" function
  377. in end_line.c */
  378. if (f)
  379. STORE(build_node_spec ((NODE_SPEC_EXTRA *) f));
  380. break;
  381. case extra_node_spec_array:
  382. {
  383. int j;
  384. AV *av;
  385. NODE_SPEC_EXTRA **array;
  386. av = newAV ();
  387. STORE(newRV_inc ((SV *)av));
  388. array = (NODE_SPEC_EXTRA **) f;
  389. while (*array)
  390. {
  391. av_push (av, build_node_spec (*array));
  392. array++;
  393. }
  394. break;
  395. }
  396. case extra_index_entry:
  397. /* A "index_entry" extra key on a command defining an index
  398. entry. Unlike the other keys, the value is not in the
  399. main parse tree, but in the indices_information. It would
  400. be much nicer if we could get rid of the need for this key.
  401. We set this afterwards in build_index_data. */
  402. break;
  403. case extra_def_args:
  404. {
  405. /* Value is an array of two-element arrays. */
  406. AV *av, *av2;
  407. HV *def_parsed_hash;
  408. int j;
  409. DEF_ARGS_EXTRA *d = (DEF_ARGS_EXTRA *) f;
  410. av = newAV ();
  411. STORE(newRV_inc ((SV *)av));
  412. /* Also create a "def_parsed_hash" extra value. The key name
  413. for this is hard-coded here. */
  414. def_parsed_hash = newHV ();
  415. hv_store (extra, "def_parsed_hash",
  416. strlen ("def_parsed_hash"),
  417. newRV_inc ((SV *)def_parsed_hash), 0);
  418. for (j = 0; j < d->nelements; j++)
  419. {
  420. ELEMENT *elt = d->elements[j];
  421. char *label = d->labels[j];
  422. av2 = newAV ();
  423. av_push (av, newRV_inc ((SV *)av2));
  424. av_push (av2, newSVpv (label, 0));
  425. if (!elt->hv)
  426. {
  427. /* TODO: Same problem as "extra_element" cross-tree
  428. references. */
  429. if (elt->parent_type != route_not_in_tree)
  430. abort ();
  431. element_to_perl_hash (elt);
  432. }
  433. if (!elt->hv)
  434. abort ();
  435. av_push (av2, newRV_inc ((SV *)elt->hv));
  436. /* Set keys of "def_parsed_hash". */
  437. // 2793
  438. if (strcmp (label, "spaces")
  439. && strcmp (label, "arg") && strcmp (label, "typearg")
  440. && strcmp (label, "delimiter"))
  441. {
  442. hv_store (def_parsed_hash, label, strlen (label),
  443. newRV_inc ((SV *)elt->hv), 0);
  444. }
  445. }
  446. break;
  447. }
  448. case extra_float_type:
  449. {
  450. EXTRA_FLOAT_TYPE *eft = (EXTRA_FLOAT_TYPE *) f;
  451. HV *type = newHV ();
  452. if (eft->content)
  453. hv_store (type, "content", strlen ("content"),
  454. build_perl_array (&eft->content->contents), 0);
  455. if (eft->normalized)
  456. hv_store (type, "normalized", strlen ("normalized"),
  457. newSVpv (eft->normalized, 0), 0);
  458. STORE(newRV_inc ((SV *)type));
  459. break;
  460. }
  461. default:
  462. abort ();
  463. break;
  464. }
  465. }
  466. #undef STORE
  467. if (!all_deleted)
  468. hv_store (e->hv, "extra", strlen ("extra"),
  469. newRV_inc((SV *)extra), 0);
  470. }
  471. if (e->line_nr.line_nr
  472. && !(command_flags(e) & CF_INFOENCLOSE))
  473. {
  474. #define STORE(key, sv) hv_store (hv, key, strlen (key), sv, 0)
  475. LINE_NR *line_nr = &e->line_nr;
  476. HV *hv = newHV ();
  477. hv_store (e->hv, "line_nr", strlen ("line_nr"),
  478. newRV_inc((SV *)hv), 0);
  479. if (line_nr->file_name)
  480. {
  481. STORE("file_name", newSVpv (line_nr->file_name, 0));
  482. }
  483. else
  484. STORE("file_name", newSVpv ("", 0));
  485. if (line_nr->line_nr)
  486. {
  487. STORE("line_nr", newSViv (line_nr->line_nr));
  488. }
  489. if (line_nr->macro)
  490. {
  491. STORE("macro", newSVpv (line_nr->macro, 0));
  492. }
  493. else
  494. STORE("macro", newSVpv ("", 0));
  495. #undef STORE
  496. }
  497. }
  498. HV *
  499. build_texinfo_tree (void)
  500. {
  501. element_to_perl_hash (Root);
  502. return Root->hv;
  503. }
  504. /* Return array of target elements. build_texinfo_tree must
  505. be called first. */
  506. AV *
  507. build_label_list (void)
  508. {
  509. AV *target_array;
  510. SV *sv;
  511. int i;
  512. dTHX;
  513. target_array = newAV ();
  514. for (i = 0; i < labels_number; i++)
  515. {
  516. sv = newRV_inc (labels_list[i].target->hv);
  517. av_push (target_array, sv);
  518. }
  519. return target_array;
  520. }
  521. AV *
  522. build_internal_xref_list (void)
  523. {
  524. AV *list_av;
  525. SV *sv;
  526. int i;
  527. dTHX;
  528. list_av = newAV ();
  529. for (i = 0; i < internal_xref_number; i++)
  530. {
  531. sv = newRV_inc (internal_xref_list[i]->hv);
  532. av_push (list_av, sv);
  533. }
  534. return list_av;
  535. }
  536. /* Return hash for list of @float's that appeared in the file. */
  537. HV *
  538. build_float_list (void)
  539. {
  540. HV *float_hash;
  541. SV **type_array;
  542. SV *sv;
  543. AV *av;
  544. int i;
  545. dTHX;
  546. float_hash = newHV ();
  547. for (i = 0; i < floats_number; i++)
  548. {
  549. type_array = hv_fetch (float_hash,
  550. floats_list[i].type,
  551. strlen (floats_list[i].type),
  552. 0);
  553. if (!type_array)
  554. {
  555. av = newAV ();
  556. hv_store (float_hash,
  557. floats_list[i].type,
  558. strlen (floats_list[i].type),
  559. newRV_inc ((SV *)av),
  560. 0);
  561. }
  562. else
  563. {
  564. av = (AV *)SvRV (*type_array);
  565. }
  566. sv = newRV_inc ((SV *)floats_list[i].element->hv);
  567. av_push (av, sv);
  568. }
  569. return float_hash;
  570. }
  571. /* Ensure that I->hv is a hash value for a single entry in
  572. $self->{'index_names'}, containing information about a single index. */
  573. static void
  574. build_single_index_data (INDEX *i)
  575. {
  576. #define STORE(key, value) hv_store (hv, key, strlen (key), value, 0)
  577. HV *hv;
  578. AV *entries;
  579. int j;
  580. dTHX;
  581. if (!i->hv)
  582. {
  583. hv = newHV ();
  584. i->hv = (void *) hv;
  585. }
  586. else
  587. {
  588. hv = (HV *) i->hv;
  589. }
  590. STORE("name", newSVpv (i->name, 0));
  591. STORE("in_code", i->in_code ? newSViv(1) : newSViv(0));
  592. if (i->merged_in)
  593. {
  594. /* This index is merged in another one. */
  595. INDEX *ultimate = ultimate_index (i);
  596. if (!ultimate->hv)
  597. {
  598. ultimate->hv = (void *) newHV ();
  599. ultimate->contained_hv = (void *) newHV ();
  600. hv_store (ultimate->hv,
  601. "contained_indices",
  602. strlen ("contained_indices"),
  603. newRV_inc ((SV *)(HV *) ultimate->contained_hv),
  604. 0);
  605. }
  606. hv_store (ultimate->contained_hv, i->name, strlen (i->name),
  607. newSViv (1), 0);
  608. STORE("merged_in", newSVpv (ultimate->name, 0));
  609. if (i->contained_hv)
  610. {
  611. hv_delete (i->hv,
  612. "contained_indices", strlen ("contained_indices"),
  613. G_DISCARD);
  614. i->contained_hv = 0;
  615. }
  616. /* See also code in end_line.c (parse_line_command_args) <CM_synindex>.
  617. FIXME: Do we need to keep the original values of contained_indices?
  618. I don't think so. */
  619. }
  620. else
  621. {
  622. if (!i->contained_hv)
  623. {
  624. i->contained_hv = newHV ();
  625. STORE("contained_indices", newRV_inc ((SV *)(HV *) i->contained_hv));
  626. }
  627. /* Record that this index "contains itself". */
  628. hv_store (i->contained_hv, i->name, strlen (i->name), newSViv(1), 0);
  629. }
  630. if (i->index_number > 0)
  631. {
  632. entries = newAV ();
  633. STORE("index_entries", newRV_inc ((SV *) entries));
  634. }
  635. #undef STORE
  636. if (i->index_number > 0)
  637. for (j = 0; j < i->index_number; j++)
  638. {
  639. #define STORE2(key, value) hv_store (entry, key, strlen (key), value, 0)
  640. HV *entry;
  641. INDEX_ENTRY *e;
  642. e = &i->index_entries[j];
  643. entry = newHV ();
  644. av_push (entries, newRV_inc ((SV *)entry));
  645. STORE2("index_name", newSVpv (i->name, 0));
  646. STORE2("index_at_command",
  647. newSVpv (command_name(e->index_at_command), 0));
  648. STORE2("index_type_command",
  649. newSVpv (command_name(e->index_type_command), 0));
  650. STORE2("command",
  651. newRV_inc ((SV *)e->command->hv));
  652. STORE2("number", newSViv (j + 1));
  653. if (e->region)
  654. {
  655. STORE2("region", newRV_inc ((SV *)e->region->hv));
  656. }
  657. if (e->content)
  658. {
  659. SV **contents_array;
  660. if (!e->content->hv)
  661. {
  662. if (e->content->parent_type != route_not_in_tree)
  663. abort ();
  664. element_to_perl_hash (e->content);
  665. }
  666. contents_array = hv_fetch (e->content->hv,
  667. "contents", strlen ("contents"), 0);
  668. if (!contents_array)
  669. {
  670. element_to_perl_hash (e->content);
  671. contents_array = hv_fetch (e->content->hv,
  672. "contents", strlen ("contents"), 0);
  673. }
  674. if (contents_array)
  675. {
  676. /* Copy the reference to the array. */
  677. STORE2("content", newRV_inc ((SV *)(AV *)SvRV(*contents_array)));
  678. /* FIXME: Allow to be different. */
  679. STORE2("content_normalized",
  680. newRV_inc ((SV *)(AV *)SvRV(*contents_array)));
  681. }
  682. else
  683. {
  684. STORE2("content", newRV_inc ((SV *)newAV ()));
  685. STORE2("content_normalized", newRV_inc ((SV *)newAV ()));
  686. }
  687. }
  688. if (e->node)
  689. STORE2("node", newRV_inc ((SV *)e->node->hv));
  690. /* We set this now because the index data structures don't
  691. exist at the time that the main tree is built. */
  692. {
  693. SV **extra_hash;
  694. extra_hash = hv_fetch (e->command->hv, "extra", strlen ("extra"), 0);
  695. if (!extra_hash)
  696. {
  697. /* There's no guarantee that the "extra" value was set on
  698. the element. */
  699. extra_hash = hv_store (e->command->hv, "extra", strlen ("extra"),
  700. newRV_inc ((SV *)newHV ()), 0);
  701. }
  702. hv_store ((HV *)SvRV(*extra_hash), "index_entry", strlen ("index_entry"),
  703. newRV_inc ((SV *)entry), 0);
  704. }
  705. #undef STORE2
  706. }
  707. }
  708. /* Return object to be used as $self->{'index_names'} in the perl code.
  709. build_texinfo_tree must be called before this so all the 'hv' fields
  710. are set on the elements in the tree. */
  711. HV *
  712. build_index_data (void)
  713. {
  714. HV *hv;
  715. INDEX **i, *idx;
  716. dTHX;
  717. hv = newHV ();
  718. for (i = index_names; (idx = *i); i++)
  719. {
  720. HV *hv2;
  721. build_single_index_data (idx);
  722. hv2 = idx->hv;
  723. hv_store (hv, idx->name, strlen (idx->name), newRV_inc ((SV *)hv2), 0);
  724. }
  725. return hv;
  726. }
  727. /* Return object to be used as $self->{'info'} in the Perl code, retrievable
  728. with the 'global_informations' function. */
  729. HV *
  730. build_global_info (void)
  731. {
  732. HV *hv;
  733. dTHX;
  734. hv = newHV ();
  735. if (global_info.input_encoding_name)
  736. hv_store (hv, "input_encoding_name", strlen ("input_encoding_name"),
  737. newSVpv (global_info.input_encoding_name, 0), 0);
  738. if (global_info.novalidate)
  739. {
  740. hv_store (hv, "novalidate", strlen ("novalidate"),
  741. newSVpv ("1", 0), 0);
  742. }
  743. return hv;
  744. }
  745. /* Return object to be used as $self->{'extra'} in the Perl code, which
  746. are mostly references to tree elements. */
  747. HV *
  748. build_global_info2 (void)
  749. {
  750. HV *hv;
  751. AV *av;
  752. int i;
  753. ELEMENT *e;
  754. dTHX;
  755. hv = newHV ();
  756. /* These should be unique elements. */
  757. #define BUILD_GLOBAL_UNIQ(cmd) \
  758. if (global_info.cmd && global_info.cmd->hv) \
  759. { \
  760. hv_store (hv, #cmd, strlen (#cmd), \
  761. newRV_inc ((SV *) global_info.cmd->hv), 0); \
  762. }
  763. BUILD_GLOBAL_UNIQ(setfilename);
  764. BUILD_GLOBAL_UNIQ(settitle);
  765. BUILD_GLOBAL_UNIQ(copying);
  766. BUILD_GLOBAL_UNIQ(titlepage);
  767. BUILD_GLOBAL_UNIQ(top);
  768. BUILD_GLOBAL_UNIQ(documentdescription);
  769. BUILD_GLOBAL_UNIQ(setcontentsaftertitlepage);
  770. BUILD_GLOBAL_UNIQ(setshortcontentsaftertitlepage);
  771. BUILD_GLOBAL_UNIQ(novalidate);
  772. BUILD_GLOBAL_UNIQ(validatemenus);
  773. BUILD_GLOBAL_UNIQ(pagesizes);
  774. BUILD_GLOBAL_UNIQ(fonttextsize);
  775. BUILD_GLOBAL_UNIQ(footnotestyle);
  776. BUILD_GLOBAL_UNIQ(setchapternewpage);
  777. BUILD_GLOBAL_UNIQ(everyheading);
  778. BUILD_GLOBAL_UNIQ(everyfooting);
  779. BUILD_GLOBAL_UNIQ(evenheading);
  780. BUILD_GLOBAL_UNIQ(evenfooting);
  781. BUILD_GLOBAL_UNIQ(oddheading);
  782. BUILD_GLOBAL_UNIQ(oddfooting);
  783. BUILD_GLOBAL_UNIQ(everyheadingmarks);
  784. BUILD_GLOBAL_UNIQ(everyfootingmarks);
  785. BUILD_GLOBAL_UNIQ(evenheadingmarks);
  786. BUILD_GLOBAL_UNIQ(oddheadingmarks);
  787. BUILD_GLOBAL_UNIQ(evenfootingmarks);
  788. BUILD_GLOBAL_UNIQ(oddfootingmarks);
  789. BUILD_GLOBAL_UNIQ(shorttitlepage);
  790. BUILD_GLOBAL_UNIQ(title);
  791. #undef BUILD_GLOBAL_UNIQ
  792. /* NOTE: Same list in handle_commands.c:register_global_command. */
  793. /* The following are arrays of elements. */
  794. if (global_info.footnotes.contents.number > 0)
  795. {
  796. av = newAV ();
  797. hv_store (hv, "footnote", strlen ("footnote"),
  798. newRV_inc ((SV *) av), 0);
  799. for (i = 0; i < global_info.footnotes.contents.number; i++)
  800. {
  801. e = contents_child_by_index (&global_info.footnotes, i);
  802. if (e->hv)
  803. av_push (av, newRV_inc ((SV *) e->hv));
  804. }
  805. }
  806. #define BUILD_GLOBAL_ARRAY(cmd) \
  807. if (global_info.cmd.contents.number > 0) \
  808. { \
  809. av = newAV (); \
  810. hv_store (hv, #cmd, strlen (#cmd), \
  811. newRV_inc ((SV *) av), 0); \
  812. for (i = 0; i < global_info.cmd.contents.number; i++) \
  813. { \
  814. e = contents_child_by_index (&global_info.cmd, i); \
  815. if (e->hv) \
  816. av_push (av, newRV_inc ((SV *) e->hv)); \
  817. } \
  818. }
  819. BUILD_GLOBAL_ARRAY(hyphenation);
  820. BUILD_GLOBAL_ARRAY(insertcopying);
  821. BUILD_GLOBAL_ARRAY(printindex);
  822. BUILD_GLOBAL_ARRAY(subtitle);
  823. BUILD_GLOBAL_ARRAY(titlefont);
  824. BUILD_GLOBAL_ARRAY(listoffloats);
  825. BUILD_GLOBAL_ARRAY(detailmenu);
  826. BUILD_GLOBAL_ARRAY(part);
  827. /* from Common.pm %document_settable_at_commands */
  828. BUILD_GLOBAL_ARRAY(allowcodebreaks);
  829. BUILD_GLOBAL_ARRAY(clickstyle);
  830. BUILD_GLOBAL_ARRAY(codequotebacktick);
  831. BUILD_GLOBAL_ARRAY(codequoteundirected);
  832. BUILD_GLOBAL_ARRAY(contents);
  833. BUILD_GLOBAL_ARRAY(deftypefnnewline);
  834. BUILD_GLOBAL_ARRAY(documentencoding);
  835. BUILD_GLOBAL_ARRAY(documentlanguage);
  836. BUILD_GLOBAL_ARRAY(exampleindent);
  837. BUILD_GLOBAL_ARRAY(firstparagraphindent);
  838. BUILD_GLOBAL_ARRAY(frenchspacing);
  839. BUILD_GLOBAL_ARRAY(headings);
  840. BUILD_GLOBAL_ARRAY(kbdinputstyle);
  841. BUILD_GLOBAL_ARRAY(paragraphindent);
  842. BUILD_GLOBAL_ARRAY(shortcontents);
  843. BUILD_GLOBAL_ARRAY(urefbreakstyle);
  844. BUILD_GLOBAL_ARRAY(xrefautomaticsectiontitle);
  845. return hv;
  846. }
  847. /* Configuration values. */
  848. CONF conf;
  849. void
  850. conf_set_show_menu (int i)
  851. {
  852. conf.show_menu = i;
  853. }
  854. void
  855. reset_conf (void)
  856. {
  857. memset (&conf, 0, sizeof (conf));
  858. conf.show_menu = 1;
  859. }