context_stack.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "tree_types.h"
  15. #include "context_stack.h"
  16. #include "api.h"
  17. static enum context *stack;
  18. static size_t top; /* One above last pushed context. */
  19. static size_t space;
  20. void
  21. reset_context_stack (void)
  22. {
  23. top = 0;
  24. }
  25. void
  26. push_context (enum context c)
  27. {
  28. if (top >= space)
  29. {
  30. stack = realloc (stack, (space += 5) * sizeof (enum context));
  31. }
  32. debug (">>>>>>>>>>>>>>>>>PUSHING STACK AT %d -- %s", top,
  33. c == ct_preformatted ? "preformatted"
  34. : c == ct_line ? "line"
  35. : c == ct_def ? "def"
  36. : c == ct_menu ? "menu"
  37. : "");
  38. stack[top++] = c;
  39. }
  40. enum context
  41. pop_context ()
  42. {
  43. if (top == 0)
  44. abort ();
  45. debug (">>>>>>>>>>>>>POPPING STACK AT %d", top - 1);
  46. return stack[--top];
  47. }
  48. enum context
  49. current_context (void)
  50. {
  51. if (top == 0)
  52. return ct_NONE;
  53. return stack[top - 1];
  54. }
  55. /* The valid regions are 'titlepage', 'copying', and 'documentdescription'.
  56. This stack isn't used that much. */
  57. static ELEMENT **region_stack;
  58. static size_t region_top; /* One above last pushed region. */
  59. static size_t region_space;
  60. void
  61. reset_region_stack (void)
  62. {
  63. region_top = 0;
  64. }
  65. void
  66. push_region (ELEMENT *e)
  67. {
  68. if (region_top >= region_space)
  69. {
  70. region_stack = realloc (region_stack,
  71. (region_space += 5) * sizeof (*region_stack));
  72. }
  73. debug (">>>>>>>>>>>>>>>>>PUSHING REGION STACK AT %d", region_top);
  74. region_stack[region_top++] = e;
  75. }
  76. ELEMENT *
  77. pop_region ()
  78. {
  79. if (region_top == 0)
  80. abort ();
  81. debug (">>>>>>>>>>>>>POPPING REGION STACK AT %d", region_top - 1);
  82. return region_stack[--region_top];
  83. }
  84. enum command_id
  85. current_region_cmd (void)
  86. {
  87. if (region_top == 0)
  88. return CM_NONE;
  89. return region_stack[region_top - 1]->cmd;
  90. }
  91. ELEMENT *
  92. current_region (void)
  93. {
  94. if (region_top == 0)
  95. return 0;
  96. return region_stack[region_top - 1];
  97. }