| 1 | /*** entry.c ****************************************************************** |
| 2 | ** |
| 3 | ** This file is part of BibTool. |
| 4 | ** It is distributed under the GNU General Public License. |
| 5 | ** See the file COPYING for details. |
| 6 | ** |
| 7 | ** (c) 1996-2020 Gerd Neugebauer |
| 8 | ** |
| 9 | ** Net: [email protected] |
| 10 | ** |
| 11 | ** This program is free software; you can redistribute it and/or modify |
| 12 | ** it under the terms of the GNU General Public License as published by |
| 13 | ** the Free Software Foundation; either version 2, or (at your option) |
| 14 | ** any later version. |
| 15 | ** |
| 16 | ** This program is distributed in the hope that it will be useful, |
| 17 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ** GNU General Public License for more details. |
| 20 | ** |
| 21 | ** You should have received a copy of the GNU General Public License |
| 22 | ** along with this program; if not, write to the Free Software |
| 23 | ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | ** |
| 25 | **----------------------------------------------------------------------------- |
| 26 | ** Description: |
| 27 | ** This module contains functions which deal with entry types. |
| 28 | ** Right from the beginning only the special record types are |
| 29 | ** known. Those special record types are |@Comment|, |@Preamble|, |
| 30 | ** |@String|, |@Include|, |@Modify|, and |@Alias|. |
| 31 | ** |
| 32 | ** In addition to those special records the user can define |
| 33 | ** additional record types which are denoted as ``normal''. E.g.\ |
| 34 | ** usually |@Article| and |@Book| are defined which are |
| 35 | ** ``normal'' record types. |
| 36 | ** |
| 37 | ** The record types are are managed in this module. In the other |
| 38 | ** modules only numerical representations are used. This module |
| 39 | ** provides means to map those numerical ids to the string |
| 40 | ** representation and back. It is also possible to define |
| 41 | ** additional record types. |
| 42 | ** |
| 43 | ** Part of this module is likely to be integrated into databases. |
| 44 | ******************************************************************************/ |
| 45 | |
| 46 | #include <bibtool/general.h> |
| 47 | #include <bibtool/symbols.h> |
| 48 | #include <bibtool/entry.h> |
| 49 | #include <bibtool/error.h> |
| 50 | #include "config.h" |
| 51 | |
| 52 | /*****************************************************************************/ |
| 53 | /* Internal Programs */ |
| 54 | /*===========================================================================*/ |
| 55 | |
| 56 | #ifdef __STDC__ |
| 57 | #define _ARG(A) A |
| 58 | #else |
| 59 | #define _ARG(A) () |
| 60 | #endif |
| 61 | static bool match _ARG((String s, String t)); /* entry.c */ |
| 62 | |
| 63 | /*****************************************************************************/ |
| 64 | /* External Programs */ |
| 65 | /*===========================================================================*/ |
| 66 | |
| 67 | /*****************************************************************************/ |
| 68 | /*** ***/ |
| 69 | /*****************************************************************************/ |
| 70 | |
| 71 | int type_xdata = -1; /* */ |
| 72 | |
| 73 | #define EntrySizeIncrement 8 |
| 74 | |
| 75 | Symbol *entry_type; |
| 76 | static int entry_ptr = 0; |
| 77 | static int entry_size = 0; |
| 78 | |
| 79 | /*----------------------------------------------------------------------------- |
| 80 | ** Function: init_entries() |
| 81 | ** Purpose: Predefine some entry types which are stored at startup time |
| 82 | ** in an array. The following entry types are predefined |
| 83 | ** because they are considered special by \BibTeX: |
| 84 | ** \begin{description} |
| 85 | ** \item [BIB\_STRING] denotes a \BibTeX{} macro definition. |
| 86 | ** \item [BIB\_PREAMBLE] denotes a preamble item which goes |
| 87 | ** before the bibliography environment. |
| 88 | ** \item [BIB\_COMMENT] denotes a comment entry which is |
| 89 | ** passed to the output file. |
| 90 | ** \item [BIB\_ALIAS] denotes an alias entry which renames an |
| 91 | ** existing entry. |
| 92 | ** \item [BIB\_MODIFY] denotes a modification request which |
| 93 | ** alters an existing entry. |
| 94 | ** \item [BIB\_INCLUDE] denotes an include request which reads |
| 95 | ** in another \BibTeX{} file. |
| 96 | ** \end{description} |
| 97 | ** Note that this function is for internal purposes |
| 98 | ** only. The normal user should call |init_bibtool()| |
| 99 | ** instead. |
| 100 | ** Arguments: none |
| 101 | ** Returns: nothing |
| 102 | **___________________________________________________ */ |
| 103 | void init_entries() /* */ |
| 104 | { /* */ |
| 105 | #ifdef INITIALIZE_BIBTEX_ENTRIES |
| 106 | static char *word_list[] = /* default entry types. */ |
| 107 | { INITIALIZE_BIBTEX_ENTRIES, NULL }; /* Mark the end with NULL */ |
| 108 | register char**wp; /* */ |
| 109 | #endif |
| 110 | /* */ |
| 111 | def_entry_type(symbol((String)"STRING") ); /* */ |
| 112 | def_entry_type(symbol((String)"PREAMBLE")); /* */ |
| 113 | def_entry_type(symbol((String)"COMMENT") ); /* */ |
| 114 | def_entry_type(symbol((String)"ALIAS") ); /* */ |
| 115 | def_entry_type(symbol((String)"MODIFY") ); /* */ |
| 116 | def_entry_type(symbol((String)"INCLUDE") ); /* */ |
| 117 | #ifdef INITIALIZE_BIBTEX_ENTRIES |
| 118 | for ( wp = word_list; *wp != NULL; ++wp ) /* add compiled in types. */ |
| 119 | { def_entry_type(symbol((String)(*wp))); } /* */ |
| 120 | #endif |
| 121 | } /*------------------------*/ |
| 122 | |
| 123 | /*----------------------------------------------------------------------------- |
| 124 | ** Function: def_entry_type() |
| 125 | ** Purpose: Dynamically define an entry type. If the entry type |
| 126 | ** already exists then a new printing representation is |
| 127 | ** stored. |
| 128 | ** |
| 129 | ** If no memory is left then an error is raised and the |
| 130 | ** program is terminated |
| 131 | ** Arguments: |
| 132 | ** s String containing the name of the entry. |
| 133 | ** Returns: nothing |
| 134 | **___________________________________________________ */ |
| 135 | void def_entry_type(sym) /* */ |
| 136 | Symbol sym; /* */ |
| 137 | { int i; /* */ |
| 138 | String s = SymbolValue(sym); /* */ |
| 139 | /* */ |
| 140 | for (i = 0; i < entry_ptr; ++i) /* */ |
| 141 | { /* */ |
| 142 | if (case_eq(s, SymbolValue(EntryName(i)))) /* */ |
| 143 | { |
| 144 | EntryName(i) = sym; /* */ |
| 145 | return; /* */ |
| 146 | } /* */ |
| 147 | } /* */ |
| 148 | /* */ |
| 149 | if ( entry_ptr <= entry_size ) /* */ |
| 150 | { entry_size += EntrySizeIncrement; /* */ |
| 151 | entry_type = ( entry_ptr == 0 /* */ |
| 152 | ? (Symbol*)malloc((size_t)(entry_size * sizeof(Symbol))) |
| 153 | : (Symbol*)realloc((void*)entry_type, |
| 154 | (size_t)(entry_size * sizeof(Symbol))) |
| 155 | ); /* */ |
| 156 | if ( entry_type == (Symbol*)NULL ) /* */ |
| 157 | { OUT_OF_MEMORY("entry type"); } /* */ |
| 158 | } /* */ |
| 159 | /* */ |
| 160 | if (case_eq(s, (String)"xdata")) /* */ |
| 161 | { type_xdata = entry_ptr; } /* */ |
| 162 | /* */ |
| 163 | entry_type[entry_ptr++] = sym; /* */ |
| 164 | } /*------------------------*/ |
| 165 | |
| 166 | /*----------------------------------------------------------------------------- |
| 167 | ** Function*: match() |
| 168 | ** Purpose: Compare two strings ignoring case. |
| 169 | ** Arguments: |
| 170 | ** s First string |
| 171 | ** t Second string |
| 172 | ** Returns: |true| iff the arguments are identical or the second |
| 173 | ** string is a substring not followed by a letter or digit. |
| 174 | **___________________________________________________ */ |
| 175 | static bool match(s,t) /* */ |
| 176 | register String s; /* */ |
| 177 | register String t; /* */ |
| 178 | { /* */ |
| 179 | while (*t) /* */ |
| 180 | { if ( ToLower(*s) != ToLower(*t) ) return false;/* */ |
| 181 | s++; t++; /* */ |
| 182 | } /* */ |
| 183 | return (is_alpha(*s) || is_digit(*s) /* */ |
| 184 | ? false /* */ |
| 185 | : true); /* */ |
| 186 | } /*------------------------*/ |
| 187 | |
| 188 | /*----------------------------------------------------------------------------- |
| 189 | ** Function: find_entry_type() |
| 190 | ** Purpose: Look up an entry type in the array of defined entries. |
| 191 | ** Arguments: |
| 192 | ** s String of the potential entry name. |
| 193 | ** Returns: The index in the array or |NOOP|. |
| 194 | **___________________________________________________ */ |
| 195 | rec_type find_entry_type(s) /* */ |
| 196 | String s; /* */ |
| 197 | { int i; /* */ |
| 198 | /* */ |
| 199 | for (i = 0; i < entry_ptr; ++i) /* */ |
| 200 | { if (match(s, SymbolValue(EntryName(i)))) /* */ |
| 201 | { return i; } /* */ |
| 202 | } /* */ |
| 203 | return BIB_NOOP; /* */ |
| 204 | } /*------------------------*/ |
| 205 | |
| 206 | /*----------------------------------------------------------------------------- |
| 207 | ** Function: get_entry_type() |
| 208 | ** Purpose: Get the printable string representation corresponding |
| 209 | ** to the numerical entry type given as argument. If no |
| 210 | ** entry type is defined for the given index then |NULL| |
| 211 | ** is returned. |
| 212 | ** Arguments: |
| 213 | ** idx Index of entry type. |
| 214 | ** Returns: Print representation of the entry type or |NULL|. |
| 215 | **___________________________________________________ */ |
| 216 | Symbol get_entry_type(idx) /* */ |
| 217 | int idx; /* */ |
| 218 | { /* */ |
| 219 | return (idx < 0 || idx >= entry_ptr /* */ |
| 220 | ? NO_SYMBOL /* */ |
| 221 | : EntryName(idx)); /* */ |
| 222 | } /*------------------------*/ |