(root)/
texinfo-7.1/
tp/
Texinfo/
XS/
parsetexi/
extra.c
       1  /* Copyright 2010-2023 Free Software Foundation, Inc.
       2  
       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  
       8     This program is distributed in the hope that it will be useful,
       9     but WITHOUT ANY WARRANTY; without even the implied warranty of
      10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      11     GNU General Public License for more details.
      12  
      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  
      16  #include <config.h>
      17  #include <string.h>
      18  
      19  #include "parser.h"
      20  
      21  static void
      22  add_associated_info_key (ASSOCIATED_INFO *a, char *key, intptr_t value,
      23                           enum extra_type type)
      24  {
      25    int i;
      26    for (i = 0; i < a->info_number; i++)
      27      {
      28        if (!strcmp (a->info[i].key, key))
      29          break;
      30      }
      31    if (i == a->info_number)
      32      {
      33        if (a->info_number == a->info_space)
      34          {
      35            a->info = realloc (a->info,
      36                                (a->info_space += 5) * sizeof (KEY_PAIR));
      37            if (!a->info)
      38              fatal ("realloc failed");
      39          }
      40        a->info_number++;
      41      }
      42  
      43    a->info[i].key = key;
      44    a->info[i].value = value;
      45    a->info[i].type = type;
      46  }
      47  
      48  /* Add an extra key that is a reference to another element (for example, 
      49     'associated_section' on a node command element. */
      50  void
      51  add_extra_element (ELEMENT *e, char *key, ELEMENT *value)
      52  {
      53    add_associated_info_key (&e->extra_info, key,
      54                             (intptr_t) value, extra_element);
      55  }
      56  
      57  /* Add an extra key that is a reference to another element that is
      58     out-of-tree, i.e., not referenced anywhere in the tree.
      59     Unused in 2023.
      60  */
      61  void
      62  add_extra_element_oot (ELEMENT *e, char *key, ELEMENT *value)
      63  {
      64    add_associated_info_key (&e->extra_info,
      65                             key, (intptr_t) value, extra_element_oot);
      66  }
      67  
      68  void
      69  add_info_element_oot (ELEMENT *e, char *key, ELEMENT *value)
      70  {
      71    add_associated_info_key (&e->info_info,
      72                             key, (intptr_t) value, extra_element_oot);
      73  }
      74  
      75  /* Add an extra key that is a reference to the contents array of another
      76     element (for example, 'node_content' on a node command element). */
      77  void
      78  add_extra_contents (ELEMENT *e, char *key, ELEMENT *value)
      79  {
      80    add_associated_info_key (&e->extra_info,
      81                             key, (intptr_t) value, extra_contents);
      82  }
      83  
      84  /* Add an extra key that is a reference to the text field of another
      85     element. */
      86  void
      87  add_extra_text (ELEMENT *e, char *key, ELEMENT *value)
      88  {
      89    add_associated_info_key (&e->extra_info, key, (intptr_t) value, extra_text);
      90  }
      91  
      92  void
      93  add_extra_misc_args (ELEMENT *e, char *key, ELEMENT *value)
      94  {
      95    if (!value) return;
      96    add_associated_info_key (&e->extra_info,
      97                             key, (intptr_t) value, extra_misc_args);
      98  }
      99  
     100  void
     101  add_extra_string (ELEMENT *e, char *key, char *value)
     102  {
     103    add_associated_info_key (&e->extra_info, key,
     104                             (intptr_t) value, extra_string);
     105  }
     106  
     107  void
     108  add_info_string (ELEMENT *e, char *key, char *value)
     109  {
     110    add_associated_info_key (&e->info_info, key, (intptr_t) value, extra_string);
     111  }
     112  
     113  void
     114  add_extra_string_dup (ELEMENT *e, char *key, char *value)
     115  {
     116    add_associated_info_key (&e->extra_info,
     117                             key, (intptr_t) strdup (value), extra_string);
     118  }
     119  
     120  void
     121  add_info_string_dup (ELEMENT *e, char *key, char *value)
     122  {
     123    add_associated_info_key (&e->info_info,
     124                             key, (intptr_t) strdup (value), extra_string);
     125  }
     126  
     127  void
     128  add_extra_integer (ELEMENT *e, char *key, long value)
     129  {
     130    add_associated_info_key (&e->extra_info,
     131                             key, (intptr_t) value, extra_integer);
     132  }
     133  
     134  KEY_PAIR *
     135  lookup_associated_info (ASSOCIATED_INFO *a, char *key)
     136  {
     137    int i;
     138    for (i = 0; i < a->info_number; i++)
     139      {
     140        if (!strcmp (a->info[i].key, key))
     141          return &a->info[i];
     142      }
     143    return 0;
     144  }
     145  
     146  ELEMENT *
     147  lookup_extra_element (ELEMENT *e, char *key)
     148  {
     149    KEY_PAIR *k;
     150    k = lookup_associated_info (&e->extra_info, key);
     151    if (!k)
     152      return 0;
     153    return (ELEMENT *) k->value;
     154  }
     155  
     156  KEY_PAIR *
     157  lookup_extra (ELEMENT *e, char *key)
     158  {
     159    return lookup_associated_info (&e->extra_info, key);
     160  }
     161  
     162  ELEMENT *
     163  lookup_info_element (ELEMENT *e, char *key)
     164  {
     165    KEY_PAIR *k;
     166    k = lookup_associated_info (&e->info_info, key);
     167    if (!k)
     168      return 0;
     169    return (ELEMENT *) k->value;
     170  }
     171  
     172  
     173  KEY_PAIR *
     174  lookup_info (ELEMENT *e, char *key)
     175  {
     176    return lookup_associated_info (&e->info_info, key);
     177  }
     178