| 1 | /*** macros.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 | |
| 27 | #include <bibtool/general.h> |
| 28 | #include <bibtool/symbols.h> |
| 29 | #include <bibtool/error.h> |
| 30 | #include <bibtool/macros.h> |
| 31 | #include <bibtool/sbuffer.h> |
| 32 | #include "config.h" |
| 33 | |
| 34 | /*****************************************************************************/ |
| 35 | /* Internal Programs */ |
| 36 | /*===========================================================================*/ |
| 37 | |
| 38 | /*****************************************************************************/ |
| 39 | /* External Programs */ |
| 40 | /*===========================================================================*/ |
| 41 | |
| 42 | /*---------------------------------------------------------------------------*/ |
| 43 | |
| 44 | static Macro macros = MacroNULL; /* */ |
| 45 | |
| 46 | /*----------------------------------------------------------------------------- |
| 47 | ** Function: new_macro() |
| 48 | ** Purpose: Allocate a new macro structure and fill it with initial values. |
| 49 | ** Upon failure an error is raised and |exit()| is called. |
| 50 | ** Arguments: |
| 51 | ** name Name of the macro. This must be a symbol. |
| 52 | ** value The value of the macro. This must be a symbol. |
| 53 | ** next The next pointer of the |Macro| structure. |
| 54 | ** count The initial reference count. |
| 55 | ** Returns: The new |Macro|. |
| 56 | **___________________________________________________ */ |
| 57 | Macro new_macro(name, value, next, count) /* */ |
| 58 | Symbol name; /* */ |
| 59 | Symbol value; /* */ |
| 60 | int count; /* */ |
| 61 | Macro next; /* */ |
| 62 | { register Macro macro; /* */ |
| 63 | /* */ |
| 64 | if ( (macro=(Macro)malloc(sizeof(SMacro))) == MacroNULL )/* */ |
| 65 | { OUT_OF_MEMORY("Macro"); } /* */ |
| 66 | /* */ |
| 67 | MacroName(macro) = name; /* */ |
| 68 | MacroValue(macro) = value ; /* */ |
| 69 | MacroCount(macro) = count; /* */ |
| 70 | NextMacro(macro) = next; /* */ |
| 71 | return(macro); /* */ |
| 72 | } /*------------------------*/ |
| 73 | |
| 74 | /*----------------------------------------------------------------------------- |
| 75 | ** Function: free_macro() |
| 76 | ** Purpose: Free a list of macros. The memory allocated for the |
| 77 | ** |Macro| given as argument and all structures |
| 78 | ** reachable via the |NextMacro| pointer are released. |
| 79 | ** Arguments: |
| 80 | ** mac First Macro to release. |
| 81 | ** Returns: nothing |
| 82 | **___________________________________________________ */ |
| 83 | void free_macro(mac) /* */ |
| 84 | Macro mac; /* */ |
| 85 | { Macro next; /* */ |
| 86 | /* */ |
| 87 | while (mac) /* */ |
| 88 | { next = NextMacro(mac); /* */ |
| 89 | UnlinkSymbol(MacroName(mac)); /* */ |
| 90 | UnlinkSymbol(MacroValue(mac)); /* */ |
| 91 | free(mac); /* */ |
| 92 | mac = next; /* */ |
| 93 | } /* */ |
| 94 | } /*------------------------*/ |
| 95 | |
| 96 | /*----------------------------------------------------------------------------- |
| 97 | ** Function: def_macro() |
| 98 | ** Purpose: Define or undefine a macro. |
| 99 | ** Arguments: |
| 100 | ** name name of the macro. |
| 101 | ** val NULL or the value of the new macro |
| 102 | ** count initial count for the macro. |
| 103 | ** Returns: nothing |
| 104 | **___________________________________________________ */ |
| 105 | int def_macro(name, val, count) /* */ |
| 106 | Symbol name; /* */ |
| 107 | Symbol val; /* */ |
| 108 | int count; /* */ |
| 109 | { register Macro *mp; /* */ |
| 110 | /* */ |
| 111 | for (mp = ¯os; /* */ |
| 112 | *mp != MacroNULL; /* */ |
| 113 | mp = &NextMacro(*mp)) /* */ |
| 114 | { if (MacroName(*mp) == name) /* */ |
| 115 | { if (val) /* */ |
| 116 | { MacroValue(*mp) = val; } /* */ |
| 117 | else /* */ |
| 118 | { Macro mac = *mp; /* */ |
| 119 | *mp = NextMacro(*mp); /* */ |
| 120 | free(mac); /* */ |
| 121 | } /* */ |
| 122 | return 1; /* */ |
| 123 | } /* */ |
| 124 | } /* */ |
| 125 | /* */ |
| 126 | if (val) /* */ |
| 127 | { macros = new_macro(name, val, macros, count); }/* */ |
| 128 | return 0; /* */ |
| 129 | } /*------------------------*/ |
| 130 | |
| 131 | /*----------------------------------------------------------------------------- |
| 132 | ** Function: look_macro() |
| 133 | ** Purpose: Return the value of a macro if it is defined. This |
| 134 | ** value is a symbol. |
| 135 | ** If the macro is undefined then |NULL| is returned. In |
| 136 | ** this case the value of |add| determines whether or not |
| 137 | ** the macro should be defined. If it is less than 0 |
| 138 | ** then no new macros is defined. Otherwise a new macro |
| 139 | ** is defined. The value is the empty string and the |
| 140 | ** initial reference count is |add|. |
| 141 | ** Arguments: |
| 142 | ** name The name of the macros to find. This needs not to be a symbol. |
| 143 | ** add Initial reference count or indicator that no new macro |
| 144 | ** is required. |
| 145 | ** Returns: The value or |NULL|. |
| 146 | **___________________________________________________ */ |
| 147 | Symbol look_macro(name, add) /* */ |
| 148 | Symbol name; /* */ |
| 149 | int add; /* */ |
| 150 | { register Macro *mp; /* */ |
| 151 | /* */ |
| 152 | for (mp = ¯os; /* */ |
| 153 | *mp != MacroNULL; /* */ |
| 154 | mp = &NextMacro(*mp)) /* */ |
| 155 | { if (MacroName(*mp) == name) /* */ |
| 156 | { if (MacroCount(*mp) >= 0) /* */ |
| 157 | MacroCount(*mp) += add; /* */ |
| 158 | return(MacroValue(*mp)); /* */ |
| 159 | } /* */ |
| 160 | } /* */ |
| 161 | if (add >= 0) /* */ |
| 162 | { def_macro(name, sym_empty, add); /* */ |
| 163 | WARNING3("Macro '",name,"' is undefined."); /* */ |
| 164 | } /* */ |
| 165 | return NULL; /* */ |
| 166 | } /*------------------------*/ |
| 167 | |
| 168 | /*----------------------------------------------------------------------------- |
| 169 | ** Function: foreach_macro() |
| 170 | ** Purpose: Apply a function to each macro in turn. The function |
| 171 | ** is called with the name and the value of the macro. If |
| 172 | ** it returns |false| then the processing of further |
| 173 | ** macros is suppressed. |
| 174 | ** |
| 175 | ** The function given as argument is called with two |
| 176 | ** string arguments. The first is the name of the macro |
| 177 | ** and the second is its value. Both are symbols and must |
| 178 | ** not be modified in any way. |
| 179 | ** |
| 180 | ** The order of the enumeration of the macros is |
| 181 | ** determined by the implementation. No specific |
| 182 | ** assumptions should be made about this order. |
| 183 | ** Arguments: |
| 184 | ** fct Function to apply to each macro. |
| 185 | ** Returns: nothing |
| 186 | **___________________________________________________ */ |
| 187 | void foreach_macro(fct) /* */ |
| 188 | bool (*fct) _ARG((Symbol ,Symbol )); /* */ |
| 189 | { Macro mac; /* */ |
| 190 | for (mac = macros; /* */ |
| 191 | mac != MacroNULL; /* */ |
| 192 | mac = NextMacro(mac)) /* */ |
| 193 | { if (MacroValue(mac) && /* */ |
| 194 | ! (*fct)(MacroName(mac), MacroValue(mac))) /* */ |
| 195 | return; /* */ |
| 196 | } /* */ |
| 197 | } /*------------------------*/ |
| 198 | |
| 199 | /*----------------------------------------------------------------------------- |
| 200 | ** Function: each_macro() |
| 201 | ** Type: int |
| 202 | ** Purpose: Iterate over a linked list of macros. A function is applied |
| 203 | ** to each macro found. The loop terminates if the |
| 204 | ** function returns |false|. |
| 205 | ** Arguments: |
| 206 | ** m the macro to start with |
| 207 | ** fct the function to apply |
| 208 | ** Returns: |true| if the function has terminated the loop and |
| 209 | ** |false| in case the end of the list has been reached |
| 210 | **___________________________________________________ */ |
| 211 | bool each_macro(m, fct) /* */ |
| 212 | Macro m; /* */ |
| 213 | bool (*fct) _ARG((Symbol ,Symbol)); /* */ |
| 214 | { /* */ |
| 215 | for ( ; m != MacroNULL; m = NextMacro(m)) /* */ |
| 216 | { if (! (*fct)(MacroName(m), MacroValue(m))) /* */ |
| 217 | return true; /* */ |
| 218 | } /* */ |
| 219 | return false; /* */ |
| 220 | } /*------------------------*/ |
| 221 | |
| 222 | /*----------------------------------------------------------------------------- |
| 223 | ** Function: dump_mac() |
| 224 | ** Purpose: Write macros to a file. |
| 225 | ** Arguments: |
| 226 | ** fname File name of the target file. |
| 227 | ** allp if == 0 only the used macros are written. |
| 228 | ** Returns: nothing |
| 229 | **___________________________________________________ */ |
| 230 | void dump_mac(fname,allp) /* */ |
| 231 | char *fname; /* */ |
| 232 | int allp; /* */ |
| 233 | { FILE *file; /* */ |
| 234 | register Macro mac; /* */ |
| 235 | /* */ |
| 236 | if ( *fname == '-' && *(fname+1) == '\0' ) /* */ |
| 237 | { file = stdout; } /* */ |
| 238 | else if ( (file=fopen(fname,"w")) == NULL ) /* */ |
| 239 | { ERROR2("File could not be opened: ", fname); /* */ |
| 240 | return; /* */ |
| 241 | } /* */ |
| 242 | /* */ |
| 243 | for (mac = macros; /* */ |
| 244 | mac != MacroNULL; /* */ |
| 245 | mac = NextMacro(mac)) /* */ |
| 246 | { if (MacroCount(mac) > 0 || /* */ |
| 247 | (MacroCount(mac) >= 0 && allp)) /* */ |
| 248 | { if (MacroName(mac) == MacroValue(mac)) /* */ |
| 249 | { (void)fprintf(file, /* */ |
| 250 | "_string{ %s = %s } used: %d\n",/* */ |
| 251 | SymbolValue(MacroName(mac)), /* */ |
| 252 | SymbolValue(MacroValue(mac)),/* */ |
| 253 | MacroCount(mac)); /* */ |
| 254 | } /* */ |
| 255 | else /* */ |
| 256 | { (void)fprintf(file, /* */ |
| 257 | "@STRING{ %s = %s } used: %d\n",/* */ |
| 258 | SymbolValue(MacroName(mac)), /* */ |
| 259 | SymbolValue(MacroValue(mac)),/* */ |
| 260 | MacroCount(mac)); /* */ |
| 261 | } /* */ |
| 262 | } /* */ |
| 263 | } /* */ |
| 264 | /* */ |
| 265 | if ( file != stdout ) (void)fclose(file); /* */ |
| 266 | } /*------------------------*/ |
| 267 | |
| 268 | /*----------------------------------------------------------------------------- |
| 269 | ** Function: init_macros() |
| 270 | ** Purpose: Initialize some macros from a table defined in the |
| 271 | ** configuration file or given as define to the C |
| 272 | ** compiler. This function has to be called to initialize |
| 273 | ** the global macros. |
| 274 | ** |
| 275 | ** Note that this function is for internal purposes |
| 276 | ** only. The normal user should call |init_bibtool()| |
| 277 | ** instead. |
| 278 | ** Arguments: none |
| 279 | ** Returns: nothing |
| 280 | **___________________________________________________ */ |
| 281 | void init_macros() /* */ |
| 282 | { /* */ |
| 283 | #ifdef INITIALIZE_MACROS |
| 284 | register Symbol name; /* */ |
| 285 | register char**wp; /* */ |
| 286 | static char *word_list[] = /* */ |
| 287 | { INITIALIZE_MACROS, NULL }; /* */ |
| 288 | /* */ |
| 289 | for (wp = word_list; *wp != NULL; ++wp) /* */ |
| 290 | { name = symbol((String)*wp); /* */ |
| 291 | def_macro(name, name, -1); /* */ |
| 292 | } /* */ |
| 293 | #endif |
| 294 | } /*------------------------*/ |
| 295 | |
| 296 | |
| 297 | /*****************************************************************************/ |
| 298 | /*** ***/ |
| 299 | /*****************************************************************************/ |
| 300 | |
| 301 | static Macro items = MacroNULL; |
| 302 | |
| 303 | /*----------------------------------------------------------------------------- |
| 304 | ** Function*: def_item() |
| 305 | ** Purpose: Define a macro. The arguments have to be symbols. |
| 306 | ** Arguments: |
| 307 | ** name the name of the item |
| 308 | ** value the value of the item |
| 309 | ** Returns: nothing |
| 310 | **___________________________________________________ */ |
| 311 | static void def_item(name, value) /* */ |
| 312 | register Symbol name; /* */ |
| 313 | register Symbol value; /* */ |
| 314 | { /* */ |
| 315 | items = new_macro(name, value, items, 0); /* */ |
| 316 | } /*------------------------*/ |
| 317 | |
| 318 | /*----------------------------------------------------------------------------- |
| 319 | ** Function: def_field_type() |
| 320 | ** Purpose: This function adds a printing representation for a |
| 321 | ** field name to the used list. The argument is an |
| 322 | ** equation of the following form |
| 323 | ** |
| 324 | ** \textit{type = value} |
| 325 | ** |
| 326 | ** \textit{type} is translated to lower case and compared |
| 327 | ** against the internal representation. \textit{value} is |
| 328 | ** printed at the appropriate places instead. |
| 329 | ** Arguments: |
| 330 | ** s String containing an equation. This string is modified |
| 331 | ** during the process. |
| 332 | ** Returns: nothing |
| 333 | **___________________________________________________ */ |
| 334 | void def_field_type(s) /* */ |
| 335 | String s; /* */ |
| 336 | { Symbol name; /* */ |
| 337 | /* */ |
| 338 | while (*s && !is_allowed(*s)) ++s; /* */ |
| 339 | if (*s == '\0') return; /* */ |
| 340 | /* */ |
| 341 | name = sym_extract(&s, true); /* */ |
| 342 | /* */ |
| 343 | while (*s && !is_allowed(*s)) ++s; /* */ |
| 344 | if (*s == '\0') return; /* */ |
| 345 | /* */ |
| 346 | def_item(name, sym_extract(&s, false)); /* */ |
| 347 | } /*------------------------*/ |
| 348 | |
| 349 | |
| 350 | /*****************************************************************************/ |
| 351 | /*** ***/ |
| 352 | /*****************************************************************************/ |
| 353 | |
| 354 | /*----------------------------------------------------------------------------- |
| 355 | ** Function*: get_mapped_or_cased() |
| 356 | ** Purpose: Query a mapping in a macro list or return the |
| 357 | ** translated name. |
| 358 | ** Arguments: |
| 359 | ** name the lower-case name of the macro to get |
| 360 | ** mac the list of macros to query |
| 361 | ** type the type of the fall-back from |SYMBOL_TYPE_LOWER|, |
| 362 | ** |SYMBOL_TYPE_UPPER|, or |SYMBOL_TYPE_CASED|. |
| 363 | ** Returns: the requested value |
| 364 | **___________________________________________________ */ |
| 365 | static Symbol get_mapped_or_cased(name, mac, type) /* */ |
| 366 | Symbol name; /* */ |
| 367 | int type; /* */ |
| 368 | register Macro mac; /* */ |
| 369 | { static StringBuffer* sb = (StringBuffer*)NULL; /* */ |
| 370 | register String s; /* */ |
| 371 | /* */ |
| 372 | for ( ; mac != MacroNULL; mac = NextMacro(mac) ) /* */ |
| 373 | { if (name == MacroName(mac)) /* */ |
| 374 | { /* */ |
| 375 | LinkSymbol(MacroValue(mac)); /* */ |
| 376 | return MacroValue(mac); /* */ |
| 377 | } /* */ |
| 378 | } /* */ |
| 379 | /* */ |
| 380 | if (sb == NULL) /* */ |
| 381 | { if ((sb=sbopen()) == NULL) /* */ |
| 382 | { OUT_OF_MEMORY("get_item()"); } /* */ |
| 383 | } else /* */ |
| 384 | { sbrewind(sb); } /* */ |
| 385 | /* */ |
| 386 | switch (type) /* */ |
| 387 | { case SYMBOL_TYPE_LOWER: /* */ |
| 388 | return name; /* */ |
| 389 | /* */ |
| 390 | case SYMBOL_TYPE_CASED: /* */ |
| 391 | for (s = SymbolValue(name); *s; ) /* */ |
| 392 | { if (is_alpha(*s)) /* */ |
| 393 | { (void)sbputc(ToUpper(*s), sb); /* */ |
| 394 | for (++s; is_alpha(*s); ++s) /* */ |
| 395 | { (void)sbputc(*s, sb); } /* */ |
| 396 | } /* */ |
| 397 | else /* */ |
| 398 | { (void)sbputc(*s, sb); /* */ |
| 399 | ++s; /* */ |
| 400 | } /* */ |
| 401 | } /* */ |
| 402 | break; /* */ |
| 403 | /* */ |
| 404 | case SYMBOL_TYPE_UPPER: /* */ |
| 405 | for (s = SymbolValue(name); *s; ++s) /* */ |
| 406 | { (void)sbputc(ToUpper(*s), sb); } /* */ |
| 407 | break; /* */ |
| 408 | } /* */ |
| 409 | return symbol((String)sbflush(sb)); /* */ |
| 410 | } /*------------------------*/ |
| 411 | |
| 412 | |
| 413 | |
| 414 | /*****************************************************************************/ |
| 415 | /*** ***/ |
| 416 | /*****************************************************************************/ |
| 417 | |
| 418 | /*----------------------------------------------------------------------------- |
| 419 | ** Function: get_item() |
| 420 | ** Purpose: Return the print representation of a \BibTeX{} string. |
| 421 | ** The appearance is determined by the |items| mapping. |
| 422 | ** If no appropriate entry is found then |type| is used to |
| 423 | ** decide whether the item should be returned as |
| 424 | ** upper-case, lower-case or first upper only. |
| 425 | ** Arguments: |
| 426 | ** name Symbol to get the print representation for. |
| 427 | ** type One of the values |SYMBOL_TYPE_UPPER|, |SYMBOL_TYPE_LOWER|, or |
| 428 | ** |SYMBOL_TYPE_CASED| as they are defined in |type.h|. |
| 429 | ** Returns: A pointer to a static string. This location is reused |
| 430 | ** upon the next invocation of this function. |
| 431 | **___________________________________________________ */ |
| 432 | Symbol get_item(name, type) /* */ |
| 433 | Symbol name; /* */ |
| 434 | int type; /* */ |
| 435 | { return get_mapped_or_cased(name, items, type); /* */ |
| 436 | } /*------------------------*/ |
| 437 | |
| 438 | |
| 439 | /*****************************************************************************/ |
| 440 | /*** ***/ |
| 441 | /*****************************************************************************/ |
| 442 | |
| 443 | static Macro keys = MacroNULL; /* */ |
| 444 | |
| 445 | /*----------------------------------------------------------------------------- |
| 446 | ** Function: save_key() |
| 447 | ** Purpose: Save a mapping of a lower-case key to a printed |
| 448 | ** representation. |
| 449 | ** Arguments: |
| 450 | ** name the name of the key in lower |
| 451 | ** key the key as printed |
| 452 | ** Returns: nothing |
| 453 | **___________________________________________________ */ |
| 454 | void save_key(name, key) /* */ |
| 455 | Symbol name; /* */ |
| 456 | Symbol key; /* */ |
| 457 | { keys = new_macro(name, key, keys, 1); /* */ |
| 458 | } /*------------------------*/ |
| 459 | |
| 460 | /*----------------------------------------------------------------------------- |
| 461 | ** Function: get_key() |
| 462 | ** Purpose: Get the printable representation of a key. If a special |
| 463 | ** representation has been registered then this |
| 464 | ** representation is returned. Otherwise the (lower-case) |
| 465 | ** key is returned. |
| 466 | ** Arguments: |
| 467 | ** name the name of the key to find. This must be in lower-case |
| 468 | ** Returns: the requested representation |
| 469 | **___________________________________________________ */ |
| 470 | Symbol get_key(name) /* */ |
| 471 | Symbol name; /* */ |
| 472 | { return get_mapped_or_cased(name, /* */ |
| 473 | keys, /* */ |
| 474 | SYMBOL_TYPE_LOWER); /* */ |
| 475 | } /*------------------------*/ |