commands.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include <stdlib.h>
  14. #include <string.h>
  15. #include "command_ids.h"
  16. #include "commands.h"
  17. #include "command_data.c"
  18. COMMAND *user_defined_command_data = 0;
  19. static size_t user_defined_number = 0;
  20. static size_t user_defined_space = 0;
  21. static int
  22. compare_command_fn (const void *a, const void *b)
  23. {
  24. const COMMAND *ca = (COMMAND *) a;
  25. const COMMAND *cb = (COMMAND *) b;
  26. return strcmp (ca->cmdname, cb->cmdname);
  27. }
  28. /* Return element number in command_data array. Return 0 if not found. */
  29. enum command_id
  30. lookup_command (char *cmdname)
  31. {
  32. COMMAND *c;
  33. COMMAND target;
  34. int i;
  35. target.cmdname = cmdname;
  36. /* Check for user-defined commands: macros, indexes, etc. */
  37. /* Do this before looking in the built-in commands, in case the user uses
  38. @definfoenclose or similar to override a command.
  39. If speed is a problem, then we could set a bit in the flags on the
  40. builtin command (maybe reusing CF_INFOENCLOSE) to say to look in the
  41. user commands instead. */
  42. for (i = 0; i < user_defined_number; i++)
  43. {
  44. if (!strcmp (user_defined_command_data[i].cmdname, cmdname))
  45. return ((enum command_id) i) | USER_COMMAND_BIT;
  46. }
  47. c = (COMMAND *) bsearch (&target, builtin_command_data + 1,
  48. /* number of elements */
  49. sizeof (builtin_command_data) / sizeof (builtin_command_data[0]) - 1,
  50. sizeof (builtin_command_data[0]),
  51. compare_command_fn);
  52. if (c)
  53. return c - &builtin_command_data[0];
  54. return 0;
  55. }
  56. /* Add a new user-defined Texinfo command, like an index or macro command. No
  57. reference to NAME is retained. */
  58. enum command_id
  59. add_texinfo_command (char *name)
  60. {
  61. if (user_defined_number == user_defined_space)
  62. {
  63. user_defined_command_data
  64. = realloc (user_defined_command_data,
  65. (user_defined_space += 10) * sizeof (COMMAND));
  66. if (!user_defined_command_data)
  67. abort ();
  68. }
  69. user_defined_command_data[user_defined_number].cmdname = strdup (name);
  70. user_defined_command_data[user_defined_number].flags = 0;
  71. user_defined_command_data[user_defined_number].data = 0;
  72. return ((enum command_id) user_defined_number++) | USER_COMMAND_BIT;
  73. }
  74. /* Remove CMD, for @unmacro. */
  75. void
  76. remove_texinfo_command (enum command_id cmd)
  77. {
  78. cmd &= ~USER_COMMAND_BIT;
  79. free (user_defined_command_data[cmd].cmdname);
  80. user_defined_command_data[cmd].cmdname = "";
  81. }
  82. void
  83. wipe_user_commands (void)
  84. {
  85. user_defined_number = 0;
  86. }
  87. /* Common.pm:841. */
  88. /* Commands that terminate a paragraph. */
  89. /* We may replace this function with a macro, or represent this infomation in
  90. command_data. */
  91. int
  92. close_paragraph_command (enum command_id cmd)
  93. {
  94. if (cmd == CM_verbatim)
  95. return 1;
  96. /* Block commands except 'raw' and 'conditional'. */
  97. if (command_data(cmd).flags & CF_block)
  98. {
  99. if (command_data(cmd).data == BLOCK_conditional
  100. || command_data(cmd).data == BLOCK_raw)
  101. return 0;
  102. if (command_data(cmd).flags & CF_format_raw)
  103. return 0;
  104. return 1;
  105. }
  106. /* several others Common.pm:852 */
  107. if (cmd == CM_titlefont
  108. || cmd == CM_insertcopying
  109. || cmd == CM_sp
  110. || cmd == CM_verbatiminclude
  111. || cmd == CM_page
  112. || cmd == CM_item
  113. || cmd == CM_itemx
  114. || cmd == CM_tab
  115. || cmd == CM_headitem
  116. || cmd == CM_printindex
  117. || cmd == CM_listoffloats
  118. || cmd == CM_center
  119. || cmd == CM_dircategory
  120. || cmd == CM_contents
  121. || cmd == CM_shortcontents
  122. || cmd == CM_summarycontents
  123. || cmd == CM_caption
  124. || cmd == CM_shortcaption
  125. || cmd == CM_setfilename
  126. || cmd == CM_exdent)
  127. return 1;
  128. /* headings Common.pm:954 */
  129. if ((command_data(cmd).flags & CF_sectioning)
  130. && !(command_data(cmd).flags & CF_root))
  131. return 1;
  132. /* def commands 866 */
  133. if ((command_data(cmd).flags & CF_def))
  134. return 1;
  135. return 0;
  136. }
  137. // Parser.pm:348
  138. int
  139. close_preformatted_command (enum command_id cmd_id)
  140. {
  141. return cmd_id != CM_sp && close_paragraph_command (cmd_id);
  142. }
  143. int
  144. item_line_command (enum command_id cmd_id)
  145. {
  146. return cmd_id == CM_table || cmd_id == CM_ftable || cmd_id == CM_vtable;
  147. }