summaryrefslogtreecommitdiff
path: root/macros/texinfo/texinfo/tp/Texinfo/XS/parsetexi/commands.c
blob: 7b2d65a3c93a4c6c19b71dfc5bd06bfc50540a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Copyright 2010-2019 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>

#include <stdlib.h>
#include <string.h>

#include "command_ids.h"
#include "commands.h"
#include "command_data.c"

COMMAND *user_defined_command_data = 0;
static size_t user_defined_number = 0;
static size_t user_defined_space = 0;

static int
compare_command_fn (const void *a, const void *b)
{
  const COMMAND *ca = (COMMAND *) a;
  const COMMAND *cb = (COMMAND *) b;

  return strcmp (ca->cmdname, cb->cmdname);
}

/* Return element number in command_data array.  Return 0 if not found. */
enum command_id
lookup_command (char *cmdname)
{
  COMMAND *c;
  COMMAND target;
  int i;

  target.cmdname = cmdname;

  /* Check for user-defined commands: macros, indexes, etc. */
  /* Do this before looking in the built-in commands, in case the user uses 
     @definfoenclose or similar to override a command.
     If speed is a problem, then we could set a bit in the flags on the
     builtin command (maybe reusing CF_INFOENCLOSE) to say to look in the
     user commands instead. */

  for (i = 0; i < user_defined_number; i++)
    {
      if (!strcmp (user_defined_command_data[i].cmdname, cmdname))
        return ((enum command_id) i) | USER_COMMAND_BIT;
    }

  c = (COMMAND *) bsearch (&target, builtin_command_data + 1,
        /* number of elements */
        sizeof (builtin_command_data) / sizeof (builtin_command_data[0]) - 1,
        sizeof (builtin_command_data[0]),
        compare_command_fn);

  if (c)
    return c - &builtin_command_data[0];


  return 0;
}

/* Add a new user-defined Texinfo command, like an index or macro command.  No 
   reference to NAME is retained. */
enum command_id
add_texinfo_command (char *name)
{
  if (user_defined_number == user_defined_space)
    {
      user_defined_command_data
        = realloc (user_defined_command_data,
                   (user_defined_space += 10) * sizeof (COMMAND));
      if (!user_defined_command_data)
        abort ();
    }

  user_defined_command_data[user_defined_number].cmdname = strdup (name);
  user_defined_command_data[user_defined_number].flags = 0;
  user_defined_command_data[user_defined_number].data = 0;

  return ((enum command_id) user_defined_number++) | USER_COMMAND_BIT;
}

/* Remove CMD, for @unmacro. */
void
remove_texinfo_command (enum command_id cmd)
{
  cmd &= ~USER_COMMAND_BIT;
  free (user_defined_command_data[cmd].cmdname);
  user_defined_command_data[cmd].cmdname = strdup ("");
}

void
wipe_user_commands (void)
{
  int i;
  for (i = 0; i < user_defined_number; i++)
    free (user_defined_command_data[i].cmdname);
  user_defined_number = 0;
}

/* Commands that terminate a paragraph. */
/* We may replace this function with a macro, or represent this infomation in
   command_data. */
int
close_paragraph_command (enum command_id cmd)
{
  if (cmd == CM_verbatim)
    return 1;

  /* Block commands except 'raw' and 'conditional'.  */

  if (command_data(cmd).flags & CF_block)
    {
      if (command_data(cmd).data == BLOCK_conditional
          || command_data(cmd).data == BLOCK_raw)
        return 0;
      if (command_data(cmd).flags & CF_format_raw)
        return 0;

      return 1;
    }

  if (cmd == CM_titlefont
     || cmd == CM_insertcopying
     || cmd == CM_sp
     || cmd == CM_verbatiminclude
     || cmd == CM_page
     || cmd == CM_item
     || cmd == CM_itemx
     || cmd == CM_tab
     || cmd == CM_headitem
     || cmd == CM_printindex
     || cmd == CM_listoffloats
     || cmd == CM_center
     || cmd == CM_dircategory
     || cmd == CM_contents
     || cmd == CM_shortcontents
     || cmd == CM_summarycontents
     || cmd == CM_caption
     || cmd == CM_shortcaption
     || cmd == CM_setfilename
     || cmd == CM_exdent)
    return 1;

  if ((command_data(cmd).flags & CF_sectioning)
      && !(command_data(cmd).flags & CF_root))
    return 1;

  if ((command_data(cmd).flags & CF_def))
    return 1;

  return 0;
}

int
close_preformatted_command (enum command_id cmd_id)
{
  return cmd_id != CM_sp && close_paragraph_command (cmd_id);
}

int
item_line_command (enum command_id cmd_id)
{
  return cmd_id == CM_table || cmd_id == CM_ftable || cmd_id == CM_vtable;
}