index.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /* index.c -- indexing for Texinfo.
  2. $Id: index.c,v 1.27 2008-05-19 18:26:48 karl Exp $
  3. Copyright (C) 1998, 1999, 2002, 2003, 2004, 2007, 2008
  4. Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "system.h"
  16. #include "mbswidth.h"
  17. #include "files.h"
  18. #include "footnote.h"
  19. #include "html.h"
  20. #include "index.h"
  21. #include "lang.h"
  22. #include "macro.h"
  23. #include "sectioning.h"
  24. #include "toc.h"
  25. #include "xml.h"
  26. /* Nonzero means that we are in the middle of printing an index. */
  27. int printing_index = 0;
  28. /* The number of defined indices. */
  29. int defined_indices = 0;
  30. /* This is the order of the index. */
  31. int index_counter = 0;
  32. /* Stuff for defining commands on the fly. */
  33. COMMAND **user_command_array = NULL;
  34. int user_command_array_len = 0;
  35. INDEX_ALIST **name_index_alist = NULL;
  36. /* An array of pointers. Each one is for a different index. The @synindex
  37. command changes which array slot is pointed to by a given index. */
  38. static INDEX_ELT **the_indices = NULL;
  39. /* How to compare index entries for sorting. May be set to strcoll. */
  40. static int (*index_compare_fn) (const char *a, const char *b) = mbscasecmp;
  41. /* Find which element in the known list of indices has this name.
  42. Returns -1 if NAME isn't found. */
  43. static int
  44. find_index_offset (char *name)
  45. {
  46. int i;
  47. for (i = 0; i < defined_indices; i++)
  48. if (name_index_alist[i] && STREQ (name, name_index_alist[i]->name))
  49. return i;
  50. return -1;
  51. }
  52. /* Return a pointer to the entry of (name . index) for this name.
  53. Return NULL if the index doesn't exist. */
  54. static INDEX_ALIST *
  55. find_index (char *name)
  56. {
  57. int offset = find_index_offset (name);
  58. if (offset > -1)
  59. return name_index_alist[offset];
  60. else
  61. return NULL;
  62. }
  63. /* User-defined commands, which happens only from user-defined indexes.
  64. Used to initialize the builtin indices, too. */
  65. static void
  66. define_user_command (char *name, COMMAND_FUNCTION (*proc), int needs_braces_p)
  67. {
  68. int slot = user_command_array_len;
  69. user_command_array_len++;
  70. if (!user_command_array)
  71. user_command_array = xmalloc (1 * sizeof (COMMAND *));
  72. user_command_array = xrealloc (user_command_array,
  73. (1 + user_command_array_len) * sizeof (COMMAND *));
  74. user_command_array[slot] = xmalloc (sizeof (COMMAND));
  75. user_command_array[slot]->name = xstrdup (name);
  76. user_command_array[slot]->proc = proc;
  77. user_command_array[slot]->argument_in_braces = needs_braces_p;
  78. }
  79. /* Please release me, let me go... */
  80. static void
  81. free_index (INDEX_ELT *index)
  82. {
  83. INDEX_ELT *temp;
  84. while ((temp = index))
  85. {
  86. free (temp->entry);
  87. free (temp->entry_text);
  88. /* Do not free the node, because we already freed the tag table,
  89. which freed all the node names. */
  90. /* free (temp->node); */
  91. index = index->next;
  92. free (temp);
  93. }
  94. }
  95. /* Flush an index by name. This will delete the list of entries that
  96. would be written by a @printindex command for this index. */
  97. static void
  98. undefindex (char *name)
  99. {
  100. int i;
  101. int which = find_index_offset (name);
  102. /* The index might have already been freed if this was the target of
  103. an @synindex. */
  104. if (which < 0 || !name_index_alist[which])
  105. return;
  106. i = name_index_alist[which]->read_index;
  107. free_index (the_indices[i]);
  108. the_indices[i] = NULL;
  109. free (name_index_alist[which]->name);
  110. free (name_index_alist[which]);
  111. name_index_alist[which] = NULL;
  112. }
  113. /* Add the arguments to the current index command to the index NAME. */
  114. static void
  115. index_add_arg (char *name)
  116. {
  117. int which;
  118. char *index_entry;
  119. INDEX_ALIST *tem;
  120. tem = find_index (name);
  121. which = tem ? tem->write_index : -1;
  122. if (macro_expansion_output_stream && !executing_string)
  123. append_to_expansion_output (input_text_offset + 1);
  124. get_rest_of_line (0, &index_entry);
  125. ignore_blank_line ();
  126. if (macro_expansion_output_stream && !executing_string)
  127. {
  128. char *index_line = xmalloc (strlen (index_entry) + 2);
  129. sprintf (index_line, "%s\n", index_entry);
  130. me_execute_string_keep_state (index_line, NULL);
  131. free (index_line);
  132. }
  133. if (which < 0)
  134. {
  135. line_error (_("Unknown index `%s'"), name);
  136. free (index_entry);
  137. }
  138. else
  139. {
  140. INDEX_ELT *new = xmalloc (sizeof (INDEX_ELT));
  141. index_counter++;
  142. /* Get output line number updated before doing anything. */
  143. if (!html && !xml)
  144. flush_output ();
  145. new->next = the_indices[which];
  146. new->entry = NULL;
  147. new->entry_text = index_entry;
  148. /* Since footnotes are handled at the very end of the document,
  149. node name in the non-split HTML outputs always show the last
  150. node. We artificially make it ``Footnotes''. */
  151. if (html && !splitting && already_outputting_pending_notes)
  152. new->node = xstrdup (_("Footnotes"));
  153. else
  154. new->node = current_node ? current_node : xstrdup ("");
  155. if (!html && !xml && no_headers)
  156. {
  157. new->section = current_sectioning_number ();
  158. if (strlen (new->section) == 0)
  159. new->section_name = current_sectioning_name ();
  160. else
  161. new->section_name = "";
  162. }
  163. else
  164. {
  165. new->section = NULL;
  166. new->section_name = NULL;
  167. }
  168. new->code = tem->code;
  169. new->defining_line = line_number - 1;
  170. new->output_line = no_headers ? output_line_number : node_line_number;
  171. /* We need to make a copy since input_filename may point to
  172. something that goes away, for example, inside a macro.
  173. (see the findexerr test). */
  174. new->defining_file = xstrdup (input_filename);
  175. if (html && splitting)
  176. {
  177. if (current_output_filename && *current_output_filename)
  178. new->output_file = filename_part (current_output_filename);
  179. else
  180. new->output_file = xstrdup ("");
  181. }
  182. else
  183. new->output_file = NULL;
  184. new->entry_number = index_counter;
  185. the_indices[which] = new;
  186. #if 0
  187. /* The index breaks if there are colons in the entry.
  188. -- This is true, but it's too painful to force changing index
  189. entries to use `colon', and too confusing for users. The real
  190. fix is to change Info support to support arbitrary characters
  191. in node names, and we're not ready to do that. --karl,
  192. 19mar02. */
  193. if (strchr (new->entry_text, ':'))
  194. warning (_("Info cannot handle `:' in index entry `%s'"),
  195. new->entry_text);
  196. #endif
  197. if (html)
  198. {
  199. /* Anchor. */
  200. int removed_empty_elt = 0;
  201. /* We must put the anchor outside the <dl> and <ul> blocks. */
  202. if (rollback_empty_tag ("dl"))
  203. removed_empty_elt = 1;
  204. else if (rollback_empty_tag ("ul"))
  205. removed_empty_elt = 2;
  206. add_word ("<a name=\"index-");
  207. add_escaped_anchor_name (index_entry, 0);
  208. add_word_args ("-%d\"></a>", index_counter);
  209. if (removed_empty_elt == 1)
  210. add_html_block_elt_args ("\n<dl>");
  211. else if (removed_empty_elt == 2)
  212. add_html_block_elt_args ("\n<ul>");
  213. }
  214. }
  215. if (xml)
  216. xml_insert_indexterm (index_entry, name);
  217. }
  218. /* The function which user defined index commands call. */
  219. static void
  220. gen_index (void)
  221. {
  222. char *name = xstrdup (command);
  223. if (strlen (name) >= strlen ("index"))
  224. name[strlen (name) - strlen ("index")] = 0;
  225. index_add_arg (name);
  226. free (name);
  227. }
  228. /* Define an index known as NAME. We assign the slot number.
  229. If CODE is nonzero, make this a code index. */
  230. static void
  231. defindex (char *name, int code)
  232. {
  233. int i, slot;
  234. /* If it already exists, flush it. */
  235. undefindex (name);
  236. /* Try to find an empty slot. */
  237. slot = -1;
  238. for (i = 0; i < defined_indices; i++)
  239. if (!name_index_alist[i])
  240. {
  241. slot = i;
  242. break;
  243. }
  244. if (slot < 0)
  245. { /* No such luck. Make space for another index. */
  246. slot = defined_indices;
  247. defined_indices++;
  248. name_index_alist = (INDEX_ALIST **)
  249. xrealloc (name_index_alist, (1 + defined_indices)
  250. * sizeof (INDEX_ALIST *));
  251. the_indices = (INDEX_ELT **)
  252. xrealloc (the_indices, (1 + defined_indices) * sizeof (INDEX_ELT *));
  253. }
  254. /* We have a slot. Start assigning. */
  255. name_index_alist[slot] = xmalloc (sizeof (INDEX_ALIST));
  256. name_index_alist[slot]->name = xstrdup (name);
  257. name_index_alist[slot]->read_index = slot;
  258. name_index_alist[slot]->write_index = slot;
  259. name_index_alist[slot]->code = code;
  260. the_indices[slot] = NULL;
  261. }
  262. /* Define an index NAME, implicitly @code if CODE is nonzero. */
  263. static void
  264. top_defindex (char *name, int code)
  265. {
  266. char *temp;
  267. temp = xmalloc (1 + strlen (name) + strlen ("index"));
  268. sprintf (temp, "%sindex", name);
  269. define_user_command (temp, gen_index, 0);
  270. defindex (name, code);
  271. free (temp);
  272. }
  273. /* Set up predefined indices. */
  274. void
  275. init_indices (void)
  276. {
  277. int i;
  278. /* Create the default data structures. */
  279. /* Initialize data space. */
  280. if (!the_indices)
  281. {
  282. the_indices = xmalloc ((1 + defined_indices) * sizeof (INDEX_ELT *));
  283. the_indices[defined_indices] = NULL;
  284. name_index_alist = xmalloc ((1 + defined_indices)
  285. * sizeof (INDEX_ALIST *));
  286. name_index_alist[defined_indices] = NULL;
  287. }
  288. /* If there were existing indices, get rid of them now. */
  289. for (i = 0; i < defined_indices; i++)
  290. {
  291. if (name_index_alist[i])
  292. { /* Suppose we're called with two input files, and the first
  293. does a @synindex pg cp. Then, when we get here to start
  294. the second file, the "pg" element won't get freed by
  295. undefindex (because it's pointing to "cp"). So free it
  296. here; otherwise, when we try to define the pg index again
  297. just below, it will still point to cp. */
  298. undefindex (name_index_alist[i]->name);
  299. /* undefindex sets all this to null in some cases. */
  300. if (name_index_alist[i])
  301. {
  302. free (name_index_alist[i]->name);
  303. free (name_index_alist[i]);
  304. name_index_alist[i] = NULL;
  305. }
  306. }
  307. }
  308. /* Add the default indices. */
  309. top_defindex ("cp", 0); /* cp is the only non-code index. */
  310. top_defindex ("fn", 1);
  311. top_defindex ("ky", 1);
  312. top_defindex ("pg", 1);
  313. top_defindex ("tp", 1);
  314. top_defindex ("vr", 1);
  315. }
  316. /* Given an index name, return the offset in the_indices of this index,
  317. or -1 if there is no such index. */
  318. static int
  319. translate_index (char *name)
  320. {
  321. INDEX_ALIST *which = find_index (name);
  322. if (which)
  323. return which->read_index;
  324. else
  325. return -1;
  326. }
  327. /* Return the index list which belongs to NAME. */
  328. INDEX_ELT *
  329. index_list (char *name)
  330. {
  331. int which = translate_index (name);
  332. if (which < 0)
  333. return (INDEX_ELT *) -1;
  334. else
  335. return the_indices[which];
  336. }
  337. /* Define a new index command. Arg is name of index. */
  338. static void
  339. gen_defindex (int code)
  340. {
  341. char *name;
  342. get_rest_of_line (0, &name);
  343. if (find_index (name))
  344. {
  345. line_error (_("Index `%s' already exists"), name);
  346. }
  347. else
  348. {
  349. char *temp = xmalloc (strlen (name) + sizeof ("index"));
  350. sprintf (temp, "%sindex", name);
  351. define_user_command (temp, gen_index, 0);
  352. defindex (name, code);
  353. free (temp);
  354. }
  355. free (name);
  356. }
  357. void
  358. cm_defindex (void)
  359. {
  360. gen_defindex (0);
  361. }
  362. void
  363. cm_defcodeindex (void)
  364. {
  365. gen_defindex (1);
  366. }
  367. /* Expects 2 args, on the same line. Both are index abbreviations.
  368. Make the first one be a synonym for the second one, i.e. make the
  369. first one have the same index as the second one. */
  370. void
  371. cm_synindex (void)
  372. {
  373. int source, target;
  374. char *abbrev1, *abbrev2;
  375. skip_whitespace ();
  376. get_until_in_line (0, " ", &abbrev1);
  377. target = find_index_offset (abbrev1);
  378. skip_whitespace ();
  379. get_until_in_line (0, " ", &abbrev2);
  380. source = find_index_offset (abbrev2);
  381. if (source < 0 || target < 0)
  382. {
  383. line_error (_("Unknown index `%s' and/or `%s' in @synindex"),
  384. abbrev1, abbrev2);
  385. }
  386. else
  387. {
  388. if (xml && !docbook)
  389. xml_synindex (abbrev1, abbrev2);
  390. else
  391. name_index_alist[target]->write_index
  392. = name_index_alist[source]->write_index;
  393. }
  394. free (abbrev1);
  395. free (abbrev2);
  396. }
  397. void
  398. cm_pindex (void) /* Pinhead index. */
  399. {
  400. index_add_arg ("pg");
  401. }
  402. void
  403. cm_vindex (void) /* Variable index. */
  404. {
  405. index_add_arg ("vr");
  406. }
  407. void
  408. cm_kindex (void) /* Key index. */
  409. {
  410. index_add_arg ("ky");
  411. }
  412. void
  413. cm_cindex (void) /* Concept index. */
  414. {
  415. index_add_arg ("cp");
  416. }
  417. void
  418. cm_findex (void) /* Function index. */
  419. {
  420. index_add_arg ("fn");
  421. }
  422. void
  423. cm_tindex (void) /* Data Type index. */
  424. {
  425. index_add_arg ("tp");
  426. }
  427. static int
  428. index_element_compare (const void *element1, const void *element2)
  429. {
  430. INDEX_ELT **elt1 = (INDEX_ELT **) element1;
  431. INDEX_ELT **elt2 = (INDEX_ELT **) element2;
  432. return index_compare_fn ((*elt1)->entry, (*elt2)->entry);
  433. }
  434. /* Force all index entries to be unique. */
  435. static void
  436. make_index_entries_unique (INDEX_ELT **array, int count)
  437. {
  438. int i, j;
  439. INDEX_ELT **copy;
  440. int counter = 1;
  441. copy = xmalloc ((1 + count) * sizeof (INDEX_ELT *));
  442. for (i = 0, j = 0; i < count; i++)
  443. {
  444. if (i == (count - 1)
  445. || array[i]->node != array[i + 1]->node
  446. || !STREQ (array[i]->entry, array[i + 1]->entry))
  447. copy[j++] = array[i];
  448. else
  449. {
  450. free (array[i]->entry);
  451. free (array[i]->entry_text);
  452. free (array[i]);
  453. }
  454. }
  455. copy[j] = NULL;
  456. /* Now COPY contains only unique entries. Duplicated entries in the
  457. original array have been freed. Replace the current array with
  458. the copy, fixing the NEXT pointers. */
  459. for (i = 0; copy[i]; i++)
  460. {
  461. copy[i]->next = copy[i + 1];
  462. /* Fix entry names which are the same. They point to different nodes,
  463. so we make the entry name unique. */
  464. if (copy[i+1]
  465. && STREQ (copy[i]->entry, copy[i + 1]->entry)
  466. && !html)
  467. {
  468. char *new_entry_name;
  469. new_entry_name = xmalloc (10 + strlen (copy[i]->entry));
  470. sprintf (new_entry_name, "%s <%d>", copy[i]->entry, counter);
  471. free (copy[i]->entry);
  472. copy[i]->entry = new_entry_name;
  473. counter++;
  474. }
  475. else
  476. counter = 1;
  477. array[i] = copy[i];
  478. }
  479. array[i] = NULL;
  480. /* Free the storage used only by COPY. */
  481. free (copy);
  482. }
  483. /* Sort the index passed in INDEX, returning an array of pointers to
  484. elements. The array is terminated with a NULL pointer. */
  485. static INDEX_ELT **
  486. sort_index (INDEX_ELT *index)
  487. {
  488. INDEX_ELT **array;
  489. INDEX_ELT *temp;
  490. int count = 0;
  491. int save_line_number = line_number;
  492. char *save_input_filename = input_filename;
  493. int save_html = html;
  494. /* Pretend we are in non-HTML mode, for the purpose of getting the
  495. expanded index entry that lacks any markup and other HTML escape
  496. characters which could produce a wrong sort order. */
  497. /* fixme: html: this still causes some markup, such as non-ASCII
  498. characters @AE{} etc., to sort incorrectly. */
  499. html = 0;
  500. for (temp = index, count = 0; temp; temp = temp->next, count++)
  501. ;
  502. /* We have the length, now we can allocate an array. */
  503. array = xmalloc ((count + 1) * sizeof (INDEX_ELT *));
  504. for (temp = index, count = 0; temp; temp = temp->next, count++)
  505. {
  506. /* Allocate new memory for the return array, since parts of the
  507. original INDEX get freed. Otherwise, if the document calls
  508. @printindex twice on the same index, with duplicate entries,
  509. we'll have garbage the second time. There are cleaner ways to
  510. deal, but this will suffice for now. */
  511. array[count] = xmalloc (sizeof (INDEX_ELT));
  512. *(array[count]) = *(temp); /* struct assignment, hope it's ok */
  513. /* Adjust next pointers to use the new memory. */
  514. if (count > 0)
  515. array[count-1]->next = array[count];
  516. /* Set line number and input filename to the source line for this
  517. index entry, as this expansion finds any errors. */
  518. line_number = array[count]->defining_line;
  519. input_filename = array[count]->defining_file;
  520. /* If this particular entry should be printed as a "code" index,
  521. then expand it as @code{entry}, i.e., as in fixed-width font. */
  522. array[count]->entry = expansion (temp->entry_text, array[count]->code);
  523. }
  524. array[count] = NULL; /* terminate the array. */
  525. line_number = save_line_number;
  526. input_filename = save_input_filename;
  527. html = save_html;
  528. #ifdef HAVE_STRCOLL
  529. /* This is not perfect. We should set (then restore) the locale to the
  530. documentlanguage, so strcoll operates according to the document's
  531. locale, not the user's. For now, I'm just going to assume that
  532. those few new documents which use @documentlanguage will be
  533. processed in the appropriate locale. In any case, don't use
  534. strcoll in the C (aka POSIX) locale, that is the ASCII ordering. */
  535. if (language_code != en)
  536. {
  537. char *lang_env = getenv ("LANG");
  538. if (lang_env && !STREQ (lang_env, "C") && !STREQ (lang_env, "POSIX"))
  539. index_compare_fn = strcoll;
  540. }
  541. #endif /* HAVE_STRCOLL */
  542. /* Sort the array. */
  543. qsort (array, count, sizeof (INDEX_ELT *), index_element_compare);
  544. /* Remove duplicate entries. */
  545. make_index_entries_unique (array, count);
  546. /* Replace the original index with the sorted one, in case the
  547. document wants to print it again. If the index wasn't empty. */
  548. if (index)
  549. *index = **array;
  550. return array;
  551. }
  552. /* Return the number of times that the byte CH occurs in the LEN bytes
  553. starting at STR. Multibyte strings are not taken into account, which
  554. is incorrect, but we need this for @tie; see more comments below. */
  555. static int
  556. count_strn_chars (const char *str, int len, int ch)
  557. {
  558. int count = 0;
  559. int i;
  560. for (i = 0; i < len; i++)
  561. if (str[i] == ch)
  562. count++;
  563. return count;
  564. }
  565. static void
  566. insert_index_output_line_no (int line_number, int output_line_number_len)
  567. {
  568. int last_column, out_line_no_width;
  569. int str_size = output_line_number_len + strlen (_("(line )"))
  570. + sizeof (NULL);
  571. char *out_line_no_str = (char *) xmalloc (str_size + 1);
  572. /* Do not translate ``(line NNN)'' below for !no_headers case (Info output),
  573. because it's something like the ``* Menu'' strings. For plaintext output
  574. it should be translated though. */
  575. sprintf (out_line_no_str,
  576. no_headers ? _("(line %*d)") : "(line %*d)",
  577. output_line_number_len, line_number);
  578. {
  579. int i = output_paragraph_offset;
  580. while (0 < i && output_paragraph[i-1] != '\n')
  581. i--;
  582. last_column = mbsnwidth ((char *)(output_paragraph + i),
  583. output_paragraph_offset - i, 0);
  584. last_column += count_strn_chars (output_paragraph + i,
  585. output_paragraph_offset - i, NON_BREAKING_SPACE);
  586. }
  587. out_line_no_width = mbswidth (out_line_no_str, 0);
  588. if (last_column + out_line_no_width > fill_column)
  589. {
  590. insert ('\n');
  591. last_column = 0;
  592. }
  593. while (last_column + out_line_no_width < fill_column)
  594. {
  595. insert (' ');
  596. last_column++;
  597. }
  598. insert_string (out_line_no_str);
  599. insert ('\n');
  600. free (out_line_no_str);
  601. }
  602. /* Takes one arg, a short name of an index to print.
  603. Outputs a menu of the sorted elements of the index. */
  604. void
  605. cm_printindex (void)
  606. {
  607. char *index_name;
  608. get_rest_of_line (0, &index_name);
  609. /* get_rest_of_line increments the line number by one,
  610. so to make warnings/errors point to the correct line,
  611. we decrement the line_number again. */
  612. if (!handling_delayed_writes)
  613. line_number--;
  614. if (xml)
  615. {
  616. xml_insert_element (PRINTINDEX, START);
  617. if (! docbook)
  618. insert_string (index_name);
  619. xml_insert_element (PRINTINDEX, END);
  620. }
  621. else if (!handling_delayed_writes)
  622. {
  623. int command_len = sizeof ("@ ") + strlen (command) + strlen (index_name);
  624. char *index_command = xmalloc (command_len + 1);
  625. close_paragraph ();
  626. sprintf (index_command, "@%s %s", command, index_name);
  627. register_delayed_write (index_command);
  628. free (index_command);
  629. }
  630. else
  631. {
  632. int item;
  633. INDEX_ELT *index;
  634. INDEX_ELT *last_index = 0;
  635. INDEX_ELT **array;
  636. unsigned line_length;
  637. char *line;
  638. int saved_inhibit_paragraph_indentation = inhibit_paragraph_indentation;
  639. int saved_filling_enabled = filling_enabled;
  640. int saved_line_number = line_number;
  641. char *saved_input_filename = input_filename;
  642. unsigned output_line_number_len;
  643. FILE *saved_output_stream = output_stream;
  644. index = index_list (index_name);
  645. if (index == (INDEX_ELT *)-1)
  646. {
  647. line_error (_("Unknown index `%s' in @printindex"), index_name);
  648. free (index_name);
  649. return;
  650. }
  651. /* Do this before sorting, so execute_string is in the good environment */
  652. if (xml && docbook)
  653. xml_begin_index ();
  654. /* Do this before sorting, so execute_string in index_element_compare
  655. will give the same results as when we actually print. */
  656. printing_index = 1;
  657. filling_enabled = 0;
  658. inhibit_paragraph_indentation = 1;
  659. xml_sort_index = 1;
  660. array = sort_index (index);
  661. xml_sort_index = 0;
  662. close_paragraph ();
  663. if (html)
  664. add_html_block_elt_args ("<ul class=\"index-%s\" compact>",
  665. index_name);
  666. else if (!no_headers && !docbook)
  667. { /* Info. Add magic cookie for info readers (to treat this
  668. menu differently), and the usual start-of-menu. */
  669. add_char ('\0');
  670. add_word ("\010[index");
  671. add_char ('\0');
  672. add_word ("\010]\n");
  673. add_word ("* Menu:\n\n");
  674. }
  675. me_inhibit_expansion++;
  676. /* This will probably be enough. */
  677. line_length = 100;
  678. line = xmalloc (line_length);
  679. {
  680. char *max_output_line_number = (char *) xmalloc (25 * sizeof (char));
  681. if (no_headers)
  682. sprintf (max_output_line_number, "%d", output_line_number);
  683. else
  684. {
  685. INDEX_ELT *tmp_entry = index;
  686. unsigned tmp = 0;
  687. for (tmp_entry = index; tmp_entry; tmp_entry = tmp_entry->next)
  688. tmp = tmp_entry->output_line > tmp ? tmp_entry->output_line : tmp;
  689. sprintf (max_output_line_number, "%d", tmp);
  690. }
  691. output_line_number_len = strlen (max_output_line_number);
  692. free (max_output_line_number);
  693. }
  694. for (item = 0; (index = array[item]); item++)
  695. {
  696. /* A pathological document might have an index entry outside of any
  697. node. Don't crash; try using the section name instead. */
  698. char *index_node = index->node;
  699. line_number = index->defining_line;
  700. input_filename = index->defining_file;
  701. if ((!index_node || !*index_node) && html)
  702. index_node = toc_find_section_of_node (index_node);
  703. if (!index_node || !*index_node)
  704. {
  705. line_error (_("Entry for index `%s' outside of any node"),
  706. index_name);
  707. if (html || !no_headers)
  708. index_node = (char *) _("(outside of any node)");
  709. }
  710. if (html)
  711. {
  712. /* For HTML, we need to expand and HTML-escape the
  713. original entry text, at the same time. Consider
  714. @cindex J@"urgen. We want J&uuml;urgen. We can't
  715. expand and then escape since we'll end up with
  716. J&amp;uuml;rgen. We can't escape and then expand
  717. because then `expansion' will see J@&quot;urgen, and
  718. @&quot;urgen is not a command. */
  719. char *html_entry =
  720. maybe_escaped_expansion (index->entry_text, index->code, 1);
  721. add_html_block_elt_args ("\n<li><a href=\"%s#index-",
  722. (splitting && index->output_file) ? index->output_file : "");
  723. add_escaped_anchor_name (index->entry_text, 0);
  724. add_word_args ("-%d\">%s</a>: ", index->entry_number,
  725. html_entry);
  726. free (html_entry);
  727. add_word ("<a href=\"");
  728. if (index->node && *index->node)
  729. {
  730. /* Ensure any non-macros in the node name are expanded. */
  731. char *expanded_index;
  732. in_fixed_width_font++;
  733. expanded_index = expansion (index_node, 0);
  734. in_fixed_width_font--;
  735. add_anchor_name (expanded_index, 1);
  736. expanded_index = escape_string (expanded_index);
  737. add_word_args ("\">%s</a>", expanded_index);
  738. free (expanded_index);
  739. }
  740. else if (STREQ (index_node, _("(outside of any node)")))
  741. {
  742. add_anchor_name (index_node, 1);
  743. add_word_args ("\">%s</a>", index_node);
  744. }
  745. else
  746. /* If we use the section instead of the (missing) node, then
  747. index_node already includes all we need except the #. */
  748. add_word_args ("#%s</a>", index_node);
  749. add_html_block_elt ("</li>");
  750. if (internal_links_stream)
  751. {
  752. char *escaped = escaped_anchor_name (index->entry_text);
  753. fprintf (internal_links_stream, "%s#index-%s-%d\t%s\t%s\n",
  754. (splitting && index->output_file) ? index->output_file : "",
  755. escaped, index->entry_number, index_name,
  756. index->entry_text);
  757. free (escaped);
  758. }
  759. }
  760. else if (xml && docbook)
  761. {
  762. /* Let DocBook processor generate the index. */
  763. }
  764. else
  765. {
  766. #define MIN_ENTRY_COLUMNS 37
  767. /* Make sure there is enough space even if index->entry has zero
  768. width. */
  769. unsigned new_length = strlen (index->entry) + MIN_ENTRY_COLUMNS;
  770. if (new_length < 50) /* minimum length used below */
  771. new_length = 50;
  772. new_length += strlen (index_node) + 7; /* * : .\n\0 */
  773. if (new_length > line_length)
  774. {
  775. line_length = new_length;
  776. line = xrealloc (line, line_length);
  777. }
  778. /* Print the entry, nicely formatted. We've already
  779. expanded any commands in index->entry, including any
  780. implicit @code. Thus, can't call execute_string, since
  781. @@ has turned into @. */
  782. if (!no_headers)
  783. {
  784. int nspaces;
  785. int width = mbswidth (index->entry, 0);
  786. /* Unfortunately, our @tie{} / @w{ } magic is an
  787. unprintable character, and so mbswidth doesn't
  788. count it. If that byte value occurs in a multibyte
  789. string, we'd lose, but at least it's only a
  790. question of minor formatting, not functionality. */
  791. width += count_strn_chars (index->entry,
  792. strlen (index->entry), NON_BREAKING_SPACE);
  793. nspaces = -(strlen (index->entry)
  794. + (MIN_ENTRY_COLUMNS - width));
  795. sprintf (line, "* %*s ",
  796. width < MIN_ENTRY_COLUMNS ? nspaces : 0,
  797. index->entry);
  798. line[2 + strlen (index->entry)] = ':';
  799. insert_string (line);
  800. /* Expand any non-macros in the node name. */
  801. in_fixed_width_font++;
  802. execute_string ("%s. ", index_node);
  803. insert_index_output_line_no (index->output_line,
  804. output_line_number_len);
  805. in_fixed_width_font--;
  806. }
  807. else
  808. {
  809. /* With --no-headers, the @node lines are gone, so
  810. there's little sense in referring to them in the
  811. index. Instead, output the number or name of the
  812. section that corresponds to that node. */
  813. sprintf (line, "%-*s ", number_sections ? 46 : 1,
  814. index->entry);
  815. line[strlen (index->entry)] = ':';
  816. insert_string (line);
  817. if (strlen (index->section) > 0)
  818. { /* We got your number. */
  819. insert_string ((char *) _("See "));
  820. insert_string (index->section);
  821. }
  822. else
  823. { /* Sigh, index in an @unnumbered. :-\ */
  824. insert_string ("\n ");
  825. insert_string ((char *) _("See "));
  826. insert_string ("``");
  827. insert_string (expansion (index->section_name, 0));
  828. insert_string ("''");
  829. }
  830. insert_string (". ");
  831. insert_index_output_line_no (index->output_line,
  832. output_line_number_len);
  833. }
  834. }
  835. /* Prevent `output_paragraph' from growing to the size of the
  836. whole index. */
  837. flush_output ();
  838. last_index = index;
  839. }
  840. free (line);
  841. me_inhibit_expansion--;
  842. printing_index = 0;
  843. close_single_paragraph ();
  844. filling_enabled = saved_filling_enabled;
  845. inhibit_paragraph_indentation = saved_inhibit_paragraph_indentation;
  846. input_filename = saved_input_filename;
  847. line_number = saved_line_number;
  848. if (html)
  849. add_html_block_elt ("</ul>");
  850. else if (xml && docbook)
  851. xml_end_index ();
  852. }
  853. free (index_name);
  854. /* Re-increment the line number, because get_rest_of_line
  855. left us looking at the next line after the command. */
  856. line_number++;
  857. }