| 1 | /*** database.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-20120Gerd 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 databases. |
| 28 | ** Databases are stored in an abstract datatype |DB| which is defined |
| 29 | ** in |database.h|. Methods are provided to query and modify a database. |
| 30 | ** |
| 31 | ******************************************************************************/ |
| 32 | |
| 33 | #include <bibtool/general.h> |
| 34 | #include <bibtool/expand.h> |
| 35 | #include <bibtool/symbols.h> |
| 36 | #include <bibtool/database.h> |
| 37 | #include <bibtool/record.h> |
| 38 | #include <bibtool/parse.h> |
| 39 | #include <bibtool/macros.h> |
| 40 | #include <bibtool/print.h> |
| 41 | #include <bibtool/error.h> |
| 42 | #include <bibtool/entry.h> |
| 43 | #include <bibtool/key.h> |
| 44 | #include <bibtool/rsc.h> |
| 45 | |
| 46 | /*****************************************************************************/ |
| 47 | /* Internal Programs */ |
| 48 | /*===========================================================================*/ |
| 49 | |
| 50 | #ifdef __STDC__ |
| 51 | #define _ARG(A) A |
| 52 | #else |
| 53 | #define _ARG(A) () |
| 54 | #endif |
| 55 | static Record insert_record _ARG((Record rec, Record ptr,int (*less)_ARG((Record, Record))));/**/ |
| 56 | static Record rec__sort _ARG((Record rec,int (*less)_ARG((Record, Record))));/**/ |
| 57 | static int cmp_heap _ARG((Record r1, Record r2)); /* */ |
| 58 | static void mark_string _ARG((Record rec, String s));/* */ |
| 59 | |
| 60 | /*****************************************************************************/ |
| 61 | /* External Programs */ |
| 62 | /*===========================================================================*/ |
| 63 | |
| 64 | /*---------------------------------------------------------------------------*/ |
| 65 | |
| 66 | |
| 67 | /*---------------------------------------------------------------------------*/ |
| 68 | |
| 69 | #define SkipWarning WARNING("Skipping to next '@'") |
| 70 | |
| 71 | /*---------------------------------------------------------------------------*/ |
| 72 | |
| 73 | |
| 74 | /*----------------------------------------------------------------------------- |
| 75 | ** Function: new_db() |
| 76 | ** Purpose: Create a new database and initialize it to contain no |
| 77 | ** information. |
| 78 | ** If no memory is left then an error is raised and the program |
| 79 | ** is terminated. |
| 80 | ** Arguments: none |
| 81 | ** Returns: The new database. |
| 82 | **___________________________________________________ */ |
| 83 | DB new_db() /* */ |
| 84 | { register DB new; /* */ |
| 85 | /* */ |
| 86 | if ((new=(DB)malloc(sizeof(sDB))) == NoDB) /* */ |
| 87 | { OUT_OF_MEMORY("db"); } /* */ |
| 88 | DBnormal(new) = RecordNULL; /* */ |
| 89 | DBstring(new) = RecordNULL; /* */ |
| 90 | DBpreamble(new) = RecordNULL; /* */ |
| 91 | DBcomment(new) = RecordNULL; /* */ |
| 92 | DBalias(new) = RecordNULL; /* */ |
| 93 | DBinclude(new) = RecordNULL; /* */ |
| 94 | DBmodify(new) = RecordNULL; /* */ |
| 95 | return new; /* */ |
| 96 | } /*------------------------*/ |
| 97 | |
| 98 | /*----------------------------------------------------------------------------- |
| 99 | ** Function: free_db() |
| 100 | ** Purpose: Deallocate the memory occupied by a database. |
| 101 | ** Note that any references to this database becomes invalid. |
| 102 | ** Arguments: |
| 103 | ** db Database to release. |
| 104 | ** Returns: nothing |
| 105 | **___________________________________________________ */ |
| 106 | void free_db(db) /* */ |
| 107 | DB db; /* */ |
| 108 | { /* */ |
| 109 | free_record(DBnormal(db)); /* */ |
| 110 | free_record(DBstring(db)); /* */ |
| 111 | free_record(DBpreamble(db)); /* */ |
| 112 | free_record(DBcomment(db)); /* */ |
| 113 | free_record(DBalias(db)); /* */ |
| 114 | free_record(DBinclude(db)); /* */ |
| 115 | free_record(DBmodify(db)); /* */ |
| 116 | free(db); /* */ |
| 117 | } /*------------------------*/ |
| 118 | |
| 119 | /*----------------------------------------------------------------------------- |
| 120 | ** Function: apply_modify() |
| 121 | ** Type: int |
| 122 | ** Purpose: |
| 123 | ** |
| 124 | ** Arguments: |
| 125 | ** db the database |
| 126 | ** key the key |
| 127 | ** rec the record |
| 128 | ** Returns: |
| 129 | **___________________________________________________ */ |
| 130 | int apply_modify(db, key, rec) /* */ |
| 131 | DB db; /* */ |
| 132 | Symbol key; /* */ |
| 133 | Record rec; /* */ |
| 134 | { Symbol *hp = RecordHeap(rec); /* */ |
| 135 | int i; /* */ |
| 136 | Record r = db_find(db, key); /* */ |
| 137 | if (r == RecordNULL) /* */ |
| 138 | { WARNING2("Entry to modify not found: ", /* */ |
| 139 | SymbolValue(key)); /* */ |
| 140 | return 0; /* */ |
| 141 | } /* */ |
| 142 | DebugPrint2("Modify ", /* */ |
| 143 | SymbolValue(*RecordHeap(rec))); /* */ |
| 144 | /* */ |
| 145 | for (i = RecordFree(rec); i > 0; i -= 2) /* */ |
| 146 | { /* No deleted or */ |
| 147 | if (*hp && /* */ |
| 148 | is_allowed(*SymbolValue(*hp)) && /* */ |
| 149 | *(hp+1)) /* private entry */ |
| 150 | { DebugPrint3(SymbolValue(*hp), /* */ |
| 151 | " => ", /* */ |
| 152 | SymbolValue(*(hp+1))); /* */ |
| 153 | push_to_record(r, *hp, *(hp+1), false); /* */ |
| 154 | } /* */ |
| 155 | hp += 2; /* Goto next pair. */ |
| 156 | } /* */ |
| 157 | return 1; /* */ |
| 158 | } /*------------------------*/ |
| 159 | |
| 160 | /*----------------------------------------------------------------------------- |
| 161 | ** Function*: apply_alias() |
| 162 | ** Type: bool |
| 163 | ** Purpose: |
| 164 | ** |
| 165 | ** Arguments: |
| 166 | ** db the database |
| 167 | ** key the key |
| 168 | ** rec the record |
| 169 | ** verbose the verbose indicator |
| 170 | ** Returns: |
| 171 | **___________________________________________________ */ |
| 172 | static bool apply_alias(db, key, rec, verbose) /* */ |
| 173 | DB db; /* */ |
| 174 | Symbol key; /* */ |
| 175 | Record rec; /* */ |
| 176 | bool verbose; /* */ |
| 177 | { Record r = db_find(db, key); /* */ |
| 178 | if (r == RecordNULL) /* */ |
| 179 | { WARNING2("Entry to alias not found: ", key); /* */ |
| 180 | return false; /* */ |
| 181 | } /* */ |
| 182 | DebugPrint2("Alias ", /* */ |
| 183 | SymbolValue(*RecordHeap(rec))); /* */ |
| 184 | /* */ |
| 185 | r = copy_record(r); /* Make a private copy. */ |
| 186 | RecordOldKey(r) = *RecordHeap(rec); /* */ |
| 187 | *RecordHeap(r) = *RecordHeap(rec); /* */ |
| 188 | db_insert(db, r, verbose); /* */ |
| 189 | return true; /* */ |
| 190 | } /*------------------------*/ |
| 191 | |
| 192 | /*----------------------------------------------------------------------------- |
| 193 | ** Function: db_insert() |
| 194 | ** Purpose: Add a record to a database. |
| 195 | ** The record can be any kind of record. It is added to the |
| 196 | ** appropriate category. |
| 197 | ** Arguments: |
| 198 | ** db Database to insert the record into. |
| 199 | ** rec Record to add to the database. |
| 200 | ** verbose Boolean to determine whether progress should be reported. |
| 201 | ** Returns: nothing |
| 202 | **___________________________________________________ */ |
| 203 | void db_insert(db, rec, verbose) /* */ |
| 204 | DB db; /* */ |
| 205 | Record rec; /* */ |
| 206 | bool verbose; /* */ |
| 207 | { Record *rp; /* */ |
| 208 | /* */ |
| 209 | switch (RecordType(rec)) /* */ |
| 210 | { case BIB_STRING: /* */ |
| 211 | rp = &DBstring(db); /* */ |
| 212 | DebugPrint1("Inserting String"); /* */ |
| 213 | break; /* */ |
| 214 | case BIB_PREAMBLE: /* */ |
| 215 | rp = &DBpreamble(db); /* */ |
| 216 | DebugPrint1("Inserting Preamble"); /* */ |
| 217 | break; /* */ |
| 218 | case BIB_COMMENT: /* */ |
| 219 | rp = &DBcomment(db); /* */ |
| 220 | DebugPrint1("Inserting Comment"); /* */ |
| 221 | break; /* */ |
| 222 | case BIB_MODIFY: /* */ |
| 223 | if(rsc_apply_modify && /* */ |
| 224 | apply_modify(db, *RecordHeap(rec), rec)) /* */ |
| 225 | { free_record(rec); /* */ |
| 226 | return; /* */ |
| 227 | } else { /* */ |
| 228 | rp = &DBmodify(db); /* */ |
| 229 | DebugPrint1("Inserting Modify"); /* */ |
| 230 | } /* */ |
| 231 | break; /* */ |
| 232 | case BIB_INCLUDE: /* */ |
| 233 | if(rsc_apply_include) /* */ |
| 234 | { DebugPrint2("Including ", /* */ |
| 235 | SymbolValue(*RecordHeap(rec)));/* */ |
| 236 | if (read_db(db, /* */ |
| 237 | SymbolValue(*RecordHeap(rec)), /* */ |
| 238 | verbose)) /* */ |
| 239 | { ERROR2("File not found: ", /* */ |
| 240 | SymbolValue(*RecordHeap(rec))); /* */ |
| 241 | } /* */ |
| 242 | free_record(rec); /* */ |
| 243 | return; /* */ |
| 244 | } else { /* */ |
| 245 | rp = &DBinclude(db); /* */ |
| 246 | DebugPrint1("Inserting Include"); /* */ |
| 247 | } /* */ |
| 248 | break; /* */ |
| 249 | case BIB_ALIAS: /* */ |
| 250 | if (rsc_apply_alias) /* */ |
| 251 | { DebugPrint2("Alias ", /* */ |
| 252 | SymbolValue(*RecordHeap(rec)));/* */ |
| 253 | apply_alias(db, *(RecordHeap(rec)+1), /* */ |
| 254 | rec, verbose); /* */ |
| 255 | free_record(rec); /* */ |
| 256 | return; /* */ |
| 257 | } else { /* */ |
| 258 | rp = &DBalias(db); /* */ |
| 259 | DebugPrint1("Inserting Alias"); /* */ |
| 260 | } /* */ |
| 261 | break; /* */ |
| 262 | default: /* */ |
| 263 | rp = &DBnormal(db); /* */ |
| 264 | DebugPrint2("Inserting Entry ", /* */ |
| 265 | SymbolValue(*RecordHeap(rec))); /* */ |
| 266 | break; /* */ |
| 267 | } /* */ |
| 268 | /* */ |
| 269 | if (*rp == RecordNULL) /* List is empty */ |
| 270 | { *rp = rec; } /* Just remember the rec. */ |
| 271 | else /* */ |
| 272 | { NextRecord(*rp) = rec; /* */ |
| 273 | PrevRecord(rec) = *rp; /* */ |
| 274 | *rp = rec; /* */ |
| 275 | } /* */ |
| 276 | } /*------------------------*/ |
| 277 | |
| 278 | /*----------------------------------------------------------------------------- |
| 279 | ** Function: read_db() |
| 280 | ** Purpose: Read records from a file and add them to a database. |
| 281 | ** A function has to be given as one argument. This function is |
| 282 | ** called for each record. If this function returns |true| then |
| 283 | ** the record is added to the database. Otherwise the record is |
| 284 | ** discarded. |
| 285 | ** |
| 286 | ** The progress of reading is reported to |stderr| if the |
| 287 | ** boolean argument |verbose| is |true|. |
| 288 | ** Arguments: |
| 289 | ** db Database to augment. |
| 290 | ** file File name to read from. |
| 291 | ** verbose Boolean to determine whether progress should be reported. |
| 292 | ** Returns: |true| if the file can not be opened. |false| otherwise. |
| 293 | **___________________________________________________ */ |
| 294 | bool read_db(db, file, verbose) /* */ |
| 295 | DB db; /* */ |
| 296 | String file; /* */ |
| 297 | bool verbose; /* */ |
| 298 | { register int type; /* */ |
| 299 | static Record master_record = RecordNULL; /* */ |
| 300 | Record rec; /* */ |
| 301 | register Record dbn; /* */ |
| 302 | /* */ |
| 303 | if (!see_bib(file)) return 1; /* */ |
| 304 | if (master_record == RecordNULL) /* */ |
| 305 | { master_record = new_record(0, 32); } /* */ |
| 306 | /* */ |
| 307 | if (file == NULL) /* */ |
| 308 | { file = (String)"<stdin>"; /* */ |
| 309 | RecordSource(master_record) = sym_empty; /* */ |
| 310 | LinkSymbol(sym_empty); /* */ |
| 311 | } /* */ |
| 312 | else /* */ |
| 313 | { RecordSource(master_record) = symbol(file); /* */ |
| 314 | } /* */ |
| 315 | /* */ |
| 316 | if (verbose) /* If desired print an */ |
| 317 | { VerbosePrint2("Reading ",file); } /* open message. */ |
| 318 | /* */ |
| 319 | dbn = DBnormal(db); /* */ |
| 320 | if (dbn != RecordNULL) /* */ |
| 321 | { /* */ |
| 322 | while (NextRecord(dbn) != RecordNULL) /* */ |
| 323 | { dbn = NextRecord(dbn); } /* */ |
| 324 | } /* */ |
| 325 | DBnormal(db) = dbn; /* */ |
| 326 | /* */ |
| 327 | for (type = parse_bib(master_record); /* */ |
| 328 | type != BIB_EOF; /* */ |
| 329 | type = parse_bib(master_record)) /* */ |
| 330 | { /* */ |
| 331 | if (type < 0) /* Errors give rise to */ |
| 332 | { SkipWarning; } /* a warning. */ |
| 333 | else if (IsSpecialRecord(type)) /* STRING/PREAMBLE/COMMENT*/ |
| 334 | { db_insert(db, /* */ |
| 335 | copy_record(master_record), /* */ |
| 336 | verbose); /* */ |
| 337 | } /* */ |
| 338 | else /* */ |
| 339 | { rec = copy_record(master_record); /* Make a private copy. */ |
| 340 | RecordOldKey(rec) = *RecordHeap(rec); /* */ |
| 341 | db_insert(db,rec, verbose); /* */ |
| 342 | if (verbose) { ErrC('+'); FlushErr; } /* */ |
| 343 | } /* */ |
| 344 | } /* */ |
| 345 | /* */ |
| 346 | if (verbose) /* If desired print a */ |
| 347 | { VerbosePrint2("Done with ",file); } /* close message. */ |
| 348 | /* */ |
| 349 | (void)seen(); /* Close the file. */ |
| 350 | /* */ |
| 351 | return false; /* */ |
| 352 | } /*------------------------*/ |
| 353 | |
| 354 | /*----------------------------------------------------------------------------- |
| 355 | ** Function*: mark_string() |
| 356 | ** Purpose: |
| 357 | ** |
| 358 | ** |
| 359 | ** Arguments: |
| 360 | ** rec the record |
| 361 | ** s the string |
| 362 | ** Returns: nothing |
| 363 | **___________________________________________________ */ |
| 364 | static void mark_string(rec, s) /* */ |
| 365 | Record rec; /* */ |
| 366 | String s; /* */ |
| 367 | { int d; /* */ |
| 368 | Record r; /* */ |
| 369 | /* */ |
| 370 | while (*s) /* */ |
| 371 | { /* */ |
| 372 | switch (*s) /* */ |
| 373 | { case '\0': return; /* */ |
| 374 | case '"': /* */ |
| 375 | d = 0; /* */ |
| 376 | while (*++s && (*s != '"' || d > 0)) /* */ |
| 377 | { switch (*s) /* */ |
| 378 | { case '{': d++; break; /* */ |
| 379 | case '}': d--; break; /* */ |
| 380 | case '\\': if (s[1]) s++; /* */ |
| 381 | } /* */ |
| 382 | } /* */ |
| 383 | if (*s) s++; /* */ |
| 384 | break; /* */ |
| 385 | case '{': /* */ |
| 386 | d = 1; /* */ |
| 387 | while (*++s && d > 0) /* */ |
| 388 | { switch (*s) /* */ |
| 389 | { case '{': d++; break; /* */ |
| 390 | case '}': d--; break; /* */ |
| 391 | case '\\': if (s[1]) s++; /* */ |
| 392 | } /* */ |
| 393 | } /* */ |
| 394 | if (*s) s++; /* */ |
| 395 | break; /* */ |
| 396 | default: /* */ |
| 397 | if (is_allowed(*s)) /* */ |
| 398 | { Symbol mac = sym_extract(&s, false); /* */ |
| 399 | /* */ |
| 400 | for (r = rec; r; r = NextRecord(r)) /* */ |
| 401 | { if (*RecordHeap(r) == mac) /* */ |
| 402 | { SetRecordMARK(r); /* */ |
| 403 | break; /* */ |
| 404 | } /* */ |
| 405 | } /* */ |
| 406 | } /* */ |
| 407 | else s++; /* */ |
| 408 | } /* */ |
| 409 | } /* */ |
| 410 | } /*------------------------*/ |
| 411 | |
| 412 | /*----------------------------------------------------------------------------- |
| 413 | ** Function*: print_segment() |
| 414 | ** Type: void |
| 415 | ** Purpose: |
| 416 | ** |
| 417 | ** Arguments: |
| 418 | ** file the file |
| 419 | ** db the database |
| 420 | ** rec the record |
| 421 | ** allp the indicator for printing all |
| 422 | ** Returns: nothing |
| 423 | **___________________________________________________ */ |
| 424 | static void print_segment(file, db, rec, allp) /* */ |
| 425 | FILE *file; /* */ |
| 426 | DB db; /* */ |
| 427 | Record rec; /* */ |
| 428 | bool allp; /* */ |
| 429 | { /* */ |
| 430 | if (rec == RecordNULL) return; /* */ |
| 431 | /* */ |
| 432 | while (PrevRecord(rec) != RecordNULL) /* Rewind to beginning. */ |
| 433 | { rec = PrevRecord(rec); } /* */ |
| 434 | /* */ |
| 435 | for ( ; rec != RecordNULL; rec = NextRecord(rec))/* */ |
| 436 | { /* */ |
| 437 | if (!RecordIsDELETED(rec)) /* */ |
| 438 | { if (allp || RecordIsMARKED(rec)) /* */ |
| 439 | { fput_record(file, rec, db, (String)"@"); /* */ |
| 440 | } /* */ |
| 441 | } /* */ |
| 442 | else if (rsc_del_q) /* */ |
| 443 | { fput_record(file, rec,db, rsc_del_pre); } /* */ |
| 444 | } /* */ |
| 445 | } /*------------------------*/ |
| 446 | |
| 447 | /*----------------------------------------------------------------------------- |
| 448 | ** Function*: preprint_string() |
| 449 | ** Type: void |
| 450 | ** Purpose: |
| 451 | ** |
| 452 | ** Arguments: |
| 453 | ** file the file |
| 454 | ** db the database |
| 455 | ** strings the string definition |
| 456 | ** rec the record |
| 457 | ** Returns: nothing |
| 458 | **___________________________________________________ */ |
| 459 | static void preprint_string(file, db, strings, rec)/* */ |
| 460 | FILE *file; /* */ |
| 461 | DB db; /* */ |
| 462 | Record strings; /* */ |
| 463 | Record rec; /* */ |
| 464 | { int i, d; /* */ |
| 465 | Record r; /* */ |
| 466 | /* */ |
| 467 | for (i = 0; i < RecordFree(rec); i += 2) /* */ |
| 468 | { if (RecordHeap(rec)[i] != NO_SYMBOL) /* */ |
| 469 | { String s = SymbolValue(RecordHeap(rec)[i+1]);/* */ |
| 470 | /* */ |
| 471 | while(*s) /* */ |
| 472 | { /* */ |
| 473 | switch (*s) /* */ |
| 474 | { case '\0': return; /* */ |
| 475 | case '"': /* */ |
| 476 | d = 0; /* */ |
| 477 | while (*++s && (*s != '"' || d > 0)) /* */ |
| 478 | { switch (*s) /* */ |
| 479 | { case '{': d++; break; /* */ |
| 480 | case '}': d--; break; /* */ |
| 481 | case '\\': if (s[1]) s++; /* */ |
| 482 | } /* */ |
| 483 | } /* */ |
| 484 | if (*s) s++; /* */ |
| 485 | break; /* */ |
| 486 | case '{': /* */ |
| 487 | d = 1; /* */ |
| 488 | while (*++s && d > 0) /* */ |
| 489 | { switch (*s) /* */ |
| 490 | { case '{': d++; break; /* */ |
| 491 | case '}': d--; break; /* */ |
| 492 | case '\\': if (s[1]) s++; /* */ |
| 493 | } /* */ |
| 494 | } /* */ |
| 495 | if (*s) s++; /* */ |
| 496 | break; /* */ |
| 497 | default: /* */ |
| 498 | if (is_allowed(*s)) /* */ |
| 499 | { Symbol mac = sym_extract(&s, false); /* */ |
| 500 | /* */ |
| 501 | for (r = strings; /* */ |
| 502 | r != RecordNULL; /* */ |
| 503 | r = NextRecord(r)) /* */ |
| 504 | { if (*RecordHeap(r) == mac && /* */ |
| 505 | RecordIsMARKED(r)) /* */ |
| 506 | { ClearRecordMARK(r); /* */ |
| 507 | fput_record(file, /* */ |
| 508 | r, /* */ |
| 509 | db, /* */ |
| 510 | (String)"@"); /* */ |
| 511 | /* */ |
| 512 | preprint_string(file, /* */ |
| 513 | db, /* */ |
| 514 | strings, /* */ |
| 515 | r); /* */ |
| 516 | } /* */ |
| 517 | } /* */ |
| 518 | } /* */ |
| 519 | else s++; /* */ |
| 520 | } /* */ |
| 521 | } /* */ |
| 522 | } /* */ |
| 523 | } /* */ |
| 524 | } /*------------------------*/ |
| 525 | |
| 526 | /*----------------------------------------------------------------------------- |
| 527 | ** Function*: print_strings() |
| 528 | ** Type: void |
| 529 | ** Purpose: |
| 530 | ** |
| 531 | ** Arguments: |
| 532 | ** file the target file |
| 533 | ** db the database |
| 534 | ** allp the indicator to include all |
| 535 | ** Returns: nothing |
| 536 | **___________________________________________________ */ |
| 537 | static void print_strings(file, db, allp) /* */ |
| 538 | FILE *file; /* */ |
| 539 | DB db; /* */ |
| 540 | bool allp; /* */ |
| 541 | { Record strings = DBstring(db); /* */ |
| 542 | Record rec; /* */ |
| 543 | /* */ |
| 544 | if (strings == RecordNULL) return; /* */ |
| 545 | /* */ |
| 546 | while (PrevRecord(strings)) /* find beginning */ |
| 547 | { strings = PrevRecord(strings); } /* of strings */ |
| 548 | /* */ |
| 549 | if (allp) /* */ |
| 550 | { /* */ |
| 551 | for (rec = strings; /* */ |
| 552 | rec != RecordNULL; /* */ |
| 553 | rec = NextRecord(rec)) /* */ |
| 554 | { SetRecordMARK(rec); } /* */ |
| 555 | } /* */ |
| 556 | else /* */ |
| 557 | { int i; /* */ |
| 558 | /* */ |
| 559 | for (rec = strings; /* reset all marks */ |
| 560 | rec != RecordNULL; /* */ |
| 561 | rec = NextRecord(rec)) /* */ |
| 562 | { ClearRecordMARK(rec); } /* */ |
| 563 | /* */ |
| 564 | if ((rec=DBnormal(db)) != RecordNULL) /* */ |
| 565 | { /* */ |
| 566 | while (PrevRecord(rec)) rec = PrevRecord(rec);/* */ |
| 567 | /* */ |
| 568 | for ( ; /* */ |
| 569 | rec != RecordNULL; /* */ |
| 570 | rec = NextRecord(rec)) /* */ |
| 571 | { /* */ |
| 572 | if (!RecordIsDELETED(rec)) /* */ |
| 573 | { /* */ |
| 574 | for (i = 2; i < RecordFree(rec); i += 2) /* */ |
| 575 | { if (RecordHeap(rec)[i] != NULL) /* */ |
| 576 | { mark_string(strings, /* */ |
| 577 | SymbolValue(RecordHeap(rec)[i+1]));/* */ |
| 578 | } /* */ |
| 579 | } /* */ |
| 580 | } /* */ |
| 581 | } /* */ |
| 582 | /* */ |
| 583 | for (rec = strings; /* mark the dependencies */ |
| 584 | rec != RecordNULL; /* from within strings */ |
| 585 | rec = NextRecord(rec)) /* */ |
| 586 | { /* */ |
| 587 | if (RecordIsMARKED(rec)) /* */ |
| 588 | { /* */ |
| 589 | for (i = 1; i < RecordFree(rec); i++) /* */ |
| 590 | { if (RecordHeap(rec)[i] != NULL) /* */ |
| 591 | { mark_string(strings, /* */ |
| 592 | SymbolValue(RecordHeap(rec)[i]));/* */ |
| 593 | } /* */ |
| 594 | } /* */ |
| 595 | } /* */ |
| 596 | } /* */ |
| 597 | } /* */ |
| 598 | } /* */ |
| 599 | /* */ |
| 600 | for (rec = strings; /* */ |
| 601 | rec != RecordNULL; /* */ |
| 602 | rec = NextRecord(rec)) /* */ |
| 603 | { /* */ |
| 604 | if (!RecordIsDELETED(rec) && /* */ |
| 605 | RecordIsMARKED(rec)) /* */ |
| 606 | { preprint_string(file, db, strings, rec); } /* */ |
| 607 | } /* */ |
| 608 | /* */ |
| 609 | print_segment(file, db, strings, false); /* */ |
| 610 | } /*------------------------*/ |
| 611 | |
| 612 | /*----------------------------------------------------------------------------- |
| 613 | ** Function: print_db() |
| 614 | ** Purpose: Print a database to a file in a way which is readable by |
| 615 | ** \BibTeX. The spec determines which parts should be |
| 616 | ** printed and the order of this parts. The spec is |
| 617 | ** processed from left to right. Each unknown character |
| 618 | ** is silently ignored. The following characters |
| 619 | ** correspond to parts of the database: |
| 620 | ** \begin{description} |
| 621 | ** \item [p] The preamble. |
| 622 | ** \item [\$] All strings (macros) contained in the database. |
| 623 | ** \item [S] The strings (macros) which are used in the |
| 624 | ** database. |
| 625 | ** \item [s] The strings (macros) contained in the database |
| 626 | ** where the resource print.all.strings determines |
| 627 | ** whether all strings should be printed or the used |
| 628 | ** strings only. |
| 629 | ** \item [n] The normal records. |
| 630 | ** \item [c] The comments. |
| 631 | ** \item [i] The includes. |
| 632 | ** \item [a] The aliases. |
| 633 | ** \item [m] The modifies. |
| 634 | ** \end{description} |
| 635 | ** Upper-case letters which are not mentioned are silently folded |
| 636 | ** to their lower-case counterparts. |
| 637 | ** Arguments: |
| 638 | ** file The file handle for printing. |
| 639 | ** db The database to print |
| 640 | ** spec String containing the specification of the parts to print. |
| 641 | ** Returns: nothing |
| 642 | **___________________________________________________ */ |
| 643 | void print_db(file,db,spec) /* */ |
| 644 | FILE *file; /* */ |
| 645 | DB db; /* */ |
| 646 | char *spec; /* */ |
| 647 | { /* */ |
| 648 | while (*spec) /* */ |
| 649 | { switch (*(spec++)) /* */ |
| 650 | { case 'p': case 'P': /* */ |
| 651 | print_segment(file, db, DBpreamble(db), true);/* */ |
| 652 | break; /* */ |
| 653 | case 'n': case 'N': /* */ |
| 654 | print_segment(file, db, DBnormal(db), true);/* */ |
| 655 | break; /* */ |
| 656 | case 's': /* */ |
| 657 | print_strings(file, db, rsc_all_macs); /* */ |
| 658 | break; /* */ |
| 659 | case '$': /* */ |
| 660 | print_strings(file, db, true); /* */ |
| 661 | break; /* */ |
| 662 | case 'S': /* */ |
| 663 | print_strings(file, db, false); /* */ |
| 664 | break; /* */ |
| 665 | case 'c': case 'C': /* */ |
| 666 | print_segment(file, db, DBcomment(db), true);/* */ |
| 667 | break; /* */ |
| 668 | case 'i': case 'I': /* */ |
| 669 | print_segment(file, db, DBinclude(db), true);/* */ |
| 670 | break; /* */ |
| 671 | case 'a': case 'A': /* */ |
| 672 | print_segment(file, db, DBalias(db), true);/* */ |
| 673 | break; /* */ |
| 674 | case 'm': case 'M': /* */ |
| 675 | print_segment(file, db, DBmodify(db), true);/* */ |
| 676 | break; /* */ |
| 677 | } /* */ |
| 678 | } /* */ |
| 679 | } /*------------------------*/ |
| 680 | |
| 681 | /*----------------------------------------------------------------------------- |
| 682 | ** Function: delete_record() |
| 683 | ** Purpose: Delete a record from a database. |
| 684 | ** It is not checked, that the record really is part of the |
| 685 | ** database. The record is just unlinked from its |
| 686 | ** list. Just in case the record should be the first one |
| 687 | ** the database record is modified. |
| 688 | ** Arguments: |
| 689 | ** db Database containing |rec|. |
| 690 | ** rec Record to delete. |
| 691 | ** Returns: nothing |
| 692 | **___________________________________________________ */ |
| 693 | void delete_record(db,rec) /* */ |
| 694 | DB db; /* */ |
| 695 | Record rec; /* */ |
| 696 | { Record *rp; /* */ |
| 697 | /* */ |
| 698 | switch (RecordType(rec)) /* */ |
| 699 | { case BIB_PREAMBLE: rp = &DBpreamble(db); break;/* */ |
| 700 | case BIB_STRING: rp = &DBstring(db); break;/* */ |
| 701 | case BIB_COMMENT: rp = &DBcomment(db); break;/* */ |
| 702 | case BIB_MODIFY: rp = &DBmodify(db); break;/* */ |
| 703 | case BIB_INCLUDE: rp = &DBinclude(db); break;/* */ |
| 704 | case BIB_ALIAS: rp = &DBalias(db); break;/* */ |
| 705 | default: rp = &DBnormal(db); break;/* */ |
| 706 | } /* */ |
| 707 | if (rec == *rp) /* */ |
| 708 | { if (NextRecord(rec) != RecordNULL) /* */ |
| 709 | { *rp = NextRecord(rec); } /* */ |
| 710 | else if (PrevRecord(rec) != RecordNULL) /* */ |
| 711 | { *rp = PrevRecord(rec); } /* */ |
| 712 | else /* */ |
| 713 | { *rp = RecordNULL; } /* */ |
| 714 | } /* */ |
| 715 | if (PrevRecord(rec) != RecordNULL) /* */ |
| 716 | { NextRecord(PrevRecord(rec)) = NextRecord(rec); }/* */ |
| 717 | if (NextRecord(rec) != RecordNULL) /* */ |
| 718 | { PrevRecord(NextRecord(rec)) = PrevRecord(rec); }/* */ |
| 719 | free_1_record(rec); /* */ |
| 720 | } /*------------------------*/ |
| 721 | |
| 722 | /*----------------------------------------------------------------------------- |
| 723 | ** Function: db_forall() |
| 724 | ** Purpose: Visit all normal records in the data base and apply the given |
| 725 | ** function |fct| to each. |
| 726 | ** If this function returns |true| then no more records need to |
| 727 | ** be visited. |
| 728 | ** No special order can be assumed in which the records are seen. |
| 729 | ** Arguments: |
| 730 | ** fct Boolean valued function determining the end of the |
| 731 | ** processing. It takes two arguments a |DB| and a |Record|. |
| 732 | ** Returns: nothing |
| 733 | **___________________________________________________ */ |
| 734 | void db_forall(db,fct) /* */ |
| 735 | DB db; /* */ |
| 736 | bool (*fct)_ARG((DB,Record)); /* Function pointer */ |
| 737 | { register Record rec, next; /* */ |
| 738 | /* */ |
| 739 | if (DBnormal(db) == RecordNULL) return; /* */ |
| 740 | /* */ |
| 741 | for (rec = DBnormal(db); /* */ |
| 742 | rec != RecordNULL; /* */ |
| 743 | rec = next) /* */ |
| 744 | { next = NextRecord(rec); /* */ |
| 745 | if (!RecordIsDELETED(rec) && /* */ |
| 746 | (*fct)(db,rec)) return; /* */ |
| 747 | } /* */ |
| 748 | /* */ |
| 749 | for (rec = PrevRecord(DBnormal(db)); /* */ |
| 750 | rec != RecordNULL; /* */ |
| 751 | rec = next) /* */ |
| 752 | { next = PrevRecord(rec); /* */ |
| 753 | if (!RecordIsDELETED(rec) && /* */ |
| 754 | (*fct)(db,rec)) return; /* */ |
| 755 | } /* */ |
| 756 | } /*------------------------*/ |
| 757 | |
| 758 | /*----------------------------------------------------------------------------- |
| 759 | ** Function: db_find() |
| 760 | ** Purpose: Search the database for a record with a given key. |
| 761 | ** If |RecordOldKey| is set for the record then use this value. |
| 762 | ** Otherwise use |*Heap|. |*Heap| contains the reference |
| 763 | ** key of normal records. |
| 764 | ** |
| 765 | ** Deleted records are ignored. An arbitrary matching |
| 766 | ** record is returned. Thus if more than one record have |
| 767 | ** the same key then the behavior is nondeterministic. |
| 768 | ** Arguments: |
| 769 | ** db Database to search in. |
| 770 | ** key the key to search for |
| 771 | ** Returns: the record found or |NULL| if none is found |
| 772 | **___________________________________________________ */ |
| 773 | Record db_find(db,key) /* */ |
| 774 | DB db; /* */ |
| 775 | Symbol key; /* */ |
| 776 | { register Record rec; /* */ |
| 777 | /* */ |
| 778 | DebugPrint2("Finding... ", SymbolValue(key)); /* */ |
| 779 | /* */ |
| 780 | if (DBnormal(db) == RecordNULL) return RecordNULL;/* */ |
| 781 | /* */ |
| 782 | for (rec = DBnormal(db); /* */ |
| 783 | rec != RecordNULL; /* */ |
| 784 | rec = NextRecord(rec)) /* */ |
| 785 | { /* */ |
| 786 | if (!RecordIsDELETED(rec)) /* */ |
| 787 | { if (RecordOldKey(rec) != NULL) /* */ |
| 788 | { if (RecordOldKey(rec) == key) return rec; /* */ |
| 789 | } /* */ |
| 790 | else if (*RecordHeap(rec) == key) return rec;/* */ |
| 791 | } /* */ |
| 792 | } /* */ |
| 793 | /* */ |
| 794 | for (rec = PrevRecord(DBnormal(db)); /* */ |
| 795 | rec != RecordNULL; /* */ |
| 796 | rec = PrevRecord(rec)) /* */ |
| 797 | { /* */ |
| 798 | if (!RecordIsDELETED(rec)) /* */ |
| 799 | { if (RecordOldKey(rec) != NULL) /* */ |
| 800 | { if (RecordOldKey(rec) == key) return rec; /* */ |
| 801 | } /* */ |
| 802 | else if (*RecordHeap(rec) == key) return rec;/* */ |
| 803 | } /* */ |
| 804 | } /* */ |
| 805 | return RecordNULL; /* */ |
| 806 | } /*------------------------*/ |
| 807 | |
| 808 | /*----------------------------------------------------------------------------- |
| 809 | ** Function: db_search() |
| 810 | ** Purpose: Search the database for a record with a given key. |
| 811 | ** If |RecordOldKey| is set for the record then use this value. |
| 812 | ** Otherwise use |*Heap|. |*Heap| contains the reference |
| 813 | ** key of normal records. |
| 814 | ** |
| 815 | ** Deleted records are not ignored! An arbitrary matching |
| 816 | ** record is returned. Thus if more than one record have |
| 817 | ** the same key then the behavior is nondeterministic. |
| 818 | ** Arguments: |
| 819 | ** db Database to search in. |
| 820 | ** key the key to search for |
| 821 | ** Returns: the record found or |NULL| if none is found |
| 822 | **___________________________________________________ */ |
| 823 | Record db_search(db, key) /* */ |
| 824 | DB db; /* */ |
| 825 | Symbol key; /* */ |
| 826 | { register Record rec; /* */ |
| 827 | /* */ |
| 828 | if (DBnormal(db) == RecordNULL) return RecordNULL;/* */ |
| 829 | /* */ |
| 830 | for (rec = DBnormal(db); /* */ |
| 831 | rec != RecordNULL; /* */ |
| 832 | rec = NextRecord(rec)) /* */ |
| 833 | { if (RecordOldKey(rec) != NO_SYMBOL) /* */ |
| 834 | { if (RecordOldKey(rec) == key) return rec; } /* */ |
| 835 | else if (*RecordHeap(rec) == key) return rec; /* */ |
| 836 | } /* */ |
| 837 | /* */ |
| 838 | for (rec = PrevRecord(DBnormal(db)); /* */ |
| 839 | rec != RecordNULL; /* */ |
| 840 | rec = PrevRecord(rec)) /* */ |
| 841 | { if (RecordOldKey(rec) != NULL) /* */ |
| 842 | { if (RecordOldKey(rec) == key) return rec; } /* */ |
| 843 | else if (*RecordHeap(rec) == key) return rec; /* */ |
| 844 | } /* */ |
| 845 | return RecordNULL; /* */ |
| 846 | } /*------------------------*/ |
| 847 | |
| 848 | /*----------------------------------------------------------------------------- |
| 849 | ** Function: db_new_key() |
| 850 | ** Purpose: Search the database for a record with a given old key and |
| 851 | ** return the new one. |
| 852 | ** Arguments: |
| 853 | ** db Database to search in. |
| 854 | ** key Key to find. |
| 855 | ** Returns: nothing |
| 856 | **___________________________________________________ */ |
| 857 | Symbol db_new_key(db, key) /* */ |
| 858 | DB db; /* */ |
| 859 | Symbol key; /* */ |
| 860 | { register Record rec; /* */ |
| 861 | /* */ |
| 862 | if (DBnormal(db) == RecordNULL) return NULL; /* */ |
| 863 | /* */ |
| 864 | for (rec = DBnormal(db); /* */ |
| 865 | rec != RecordNULL; /* */ |
| 866 | rec = NextRecord(rec)) /* */ |
| 867 | { /* */ |
| 868 | if (RecordOldKey(rec) != NULL && /* */ |
| 869 | RecordOldKey(rec) == key) /* */ |
| 870 | return *RecordHeap(rec); /* */ |
| 871 | } /* */ |
| 872 | /* */ |
| 873 | for (rec = PrevRecord(DBnormal(db)); /* */ |
| 874 | rec != RecordNULL; /* */ |
| 875 | rec = PrevRecord(rec)) /* */ |
| 876 | { /* */ |
| 877 | if (RecordOldKey(rec) != NULL && /* */ |
| 878 | RecordOldKey(rec) == key) /* */ |
| 879 | return *RecordHeap(rec); /* */ |
| 880 | } /* */ |
| 881 | return NULL; /* */ |
| 882 | } /*------------------------*/ |
| 883 | |
| 884 | /*----------------------------------------------------------------------------- |
| 885 | ** Function: db_rewind() |
| 886 | ** Purpose: Rewind the normal records of a database to point to the first |
| 887 | ** record if at least one records exists. Otherwise nothing is |
| 888 | ** done. |
| 889 | ** Arguments: |
| 890 | ** db Database to rewind. |
| 891 | ** Returns: nothing |
| 892 | **___________________________________________________ */ |
| 893 | void db_rewind(db) /* */ |
| 894 | DB db; /* */ |
| 895 | { /* */ |
| 896 | if (DBnormal(db) == RecordNULL) return; /* */ |
| 897 | /* */ |
| 898 | while (PrevRecord(DBnormal(db)) != RecordNULL) /* */ |
| 899 | { DBnormal(db) = PrevRecord(DBnormal(db)); } /* */ |
| 900 | } /*------------------------*/ |
| 901 | |
| 902 | /*----------------------------------------------------------------------------- |
| 903 | ** Function*: rec__sort() |
| 904 | ** Purpose: |
| 905 | ** |
| 906 | ** Arguments: |
| 907 | ** rec the record |
| 908 | ** less the comparator |
| 909 | ** Returns: |
| 910 | **___________________________________________________ */ |
| 911 | static Record rec__sort(rec,less) /* */ |
| 912 | Record rec; /* */ |
| 913 | int (*less)_ARG((Record,Record)); /* Function pointer */ |
| 914 | { Record r; /* */ |
| 915 | Record new; /* */ |
| 916 | /* */ |
| 917 | if (rec == RecordNULL) return rec; /* */ |
| 918 | r = RecordNULL ; /* */ |
| 919 | /* */ |
| 920 | while (PrevRecord(rec) != RecordNULL) /* */ |
| 921 | { rec = PrevRecord(rec); } /* */ |
| 922 | /* */ |
| 923 | while (rec != RecordNULL) /* */ |
| 924 | { new = rec; /* */ |
| 925 | rec = NextRecord(rec); /* */ |
| 926 | r = insert_record(new,r,less); /* */ |
| 927 | } /* */ |
| 928 | return r; /* */ |
| 929 | } /*------------------------*/ |
| 930 | |
| 931 | /*----------------------------------------------------------------------------- |
| 932 | ** The sorting uses a variant of insertion sort. |
| 933 | ** Thus the major task is performed in this function, which is called to store |
| 934 | ** a record. |
| 935 | ** The Record structure contains two pointers to the same type. With those |
| 936 | ** pointers a double linked list is established. sort_rec is either NULL --- |
| 937 | ** if no record is stored --- or it is a pointer to some element in the |
| 938 | ** ORDERED list: |
| 939 | ** |
| 940 | ** +------+ +------+ +------+ +------+ +------+ +------+ |
| 941 | ** -->|Record|-->|Record|-->|Record|-->|Record|-->|Record|-->|Record|--> |
| 942 | ** <--| |<--| |<--| |<--| |<--| |<--| |<-- |
| 943 | ** +------+ +------+ +------+ +------+ +------+ +------+ |
| 944 | ** ^ |
| 945 | ** db_______| |
| 946 | ** |
| 947 | ** With this picture in mind it's obvious what's to be done. |
| 948 | ** There are two major cases. To find the right position to insert the new |
| 949 | ** record |
| 950 | ** - either the pointer has to be moved leftward |
| 951 | ** - or the pointer has to be moved rightward |
| 952 | ** |
| 953 | ** Special cases have to be considered if the new record is to be inserted |
| 954 | ** before the first or after the last element in the list. |
| 955 | ** |
| 956 | ** The rest is simply leg work. |
| 957 | ** |
| 958 | ** Well, a word on the complexity. |
| 959 | ** If the input is almost sorted (correct or in reverse order) then the |
| 960 | ** algorithm is linear. |
| 961 | ** In the worst case the algorithm is quadratic. This worst case looks as |
| 962 | ** follows: |
| 963 | ** A record has to be inserted which is smaller than the least element in the |
| 964 | ** list. Afterwards one which is greater than the largest. |
| 965 | ** |
| 966 | ** NOTE: CURRENTLY THE ALGORITHM IS NOT STABLE: |
| 967 | ** Records with the same key may get mixed in the sorted list. |
| 968 | **___________________________________________________ */ |
| 969 | static Record insert_record(rec,ptr,less) /* */ |
| 970 | register Record rec; /* */ |
| 971 | register Record ptr; /* */ |
| 972 | int (*less)_ARG((Record,Record)); /* Function pointer */ |
| 973 | { /* */ |
| 974 | if (ptr == RecordNULL) { /* List is empty */ |
| 975 | PrevRecord(rec) = RecordNULL; /* */ |
| 976 | NextRecord(rec) = RecordNULL; /* */ |
| 977 | return rec; /* */ |
| 978 | } /* */ |
| 979 | /* */ |
| 980 | if ((*less)(rec,ptr)) /* */ |
| 981 | { /* */ |
| 982 | while (PrevRecord(ptr) != RecordNULL) /* Move leftward. */ |
| 983 | { ptr = PrevRecord(ptr); /* */ |
| 984 | if (!(*less)(rec,ptr)) /* */ |
| 985 | { PrevRecord(rec) = ptr; /* Insert */ |
| 986 | NextRecord(rec) = NextRecord(ptr);/* */ |
| 987 | PrevRecord(NextRecord(rec))= rec; /* */ |
| 988 | NextRecord(ptr) = rec; /* */ |
| 989 | return rec; /* */ |
| 990 | } /* */ |
| 991 | } /* */ |
| 992 | PrevRecord(ptr) = rec; /* Insert before the first*/ |
| 993 | NextRecord(rec) = ptr; /* */ |
| 994 | PrevRecord(rec) = RecordNULL; /* */ |
| 995 | } /* */ |
| 996 | else /* */ |
| 997 | { /* */ |
| 998 | while (NextRecord(ptr) != RecordNULL) /* Move rightward. */ |
| 999 | { ptr = NextRecord(ptr); /* */ |
| 1000 | if ((*less)(rec,ptr)) /* */ |
| 1001 | { NextRecord(rec) = ptr; /* Insert */ |
| 1002 | PrevRecord(rec) = PrevRecord(ptr);/* */ |
| 1003 | NextRecord(PrevRecord(rec))= rec; /* */ |
| 1004 | PrevRecord(ptr) = rec; /* */ |
| 1005 | return rec; /* */ |
| 1006 | } /* */ |
| 1007 | } /* */ |
| 1008 | NextRecord(ptr) = rec; /* Insert after the last. */ |
| 1009 | PrevRecord(rec) = ptr; /* */ |
| 1010 | NextRecord(rec) = RecordNULL; /* */ |
| 1011 | } /* */ |
| 1012 | return rec; /* */ |
| 1013 | } /*------------------------*/ |
| 1014 | |
| 1015 | /*----------------------------------------------------------------------------- |
| 1016 | ** Function*: cmp_heap() |
| 1017 | ** Purpose: Compare the keys of two records. The keys are stored at the |
| 1018 | ** top of the heap. |
| 1019 | ** Arguments: |
| 1020 | ** r1 First record to compare. |
| 1021 | ** r2 Second record to compare. |
| 1022 | ** Returns: 1 if the first record is smaller than the second one. |
| 1023 | **___________________________________________________ */ |
| 1024 | static int cmp_heap(r1,r2) /* */ |
| 1025 | register Record r1; /* */ |
| 1026 | register Record r2; /* */ |
| 1027 | { /* */ |
| 1028 | return symcmp(*RecordHeap(r1), /* */ |
| 1029 | *RecordHeap(r2)) < 0; /* */ |
| 1030 | } /*------------------------*/ |
| 1031 | |
| 1032 | /*----------------------------------------------------------------------------- |
| 1033 | ** Function: db_mac_sort() |
| 1034 | ** Purpose: Sort the macros of a database. The sorting uses |
| 1035 | ** increasing lexicographic order according to the |
| 1036 | ** character codes of the macro names. Note that this |
| 1037 | ** might lead to different results on machines with |
| 1038 | ** different character encodings, e.g.\ ASCII vs.\ EBCDIC. |
| 1039 | ** Arguments: |
| 1040 | ** db Database to sort. |
| 1041 | ** less comparison function to use. |
| 1042 | ** Returns: nothing |
| 1043 | **___________________________________________________ */ |
| 1044 | void db_mac_sort(db) /* */ |
| 1045 | DB db; /* */ |
| 1046 | { /* */ |
| 1047 | DBstring(db) = rec__sort(DBstring(db), cmp_heap);/* */ |
| 1048 | } /*------------------------*/ |
| 1049 | |
| 1050 | /*----------------------------------------------------------------------------- |
| 1051 | ** Function: db_sort() |
| 1052 | ** Purpose: Sort the normal records of a database. As a side effect the |
| 1053 | ** records are kept in sorted order in the database. |
| 1054 | ** The sorting order can be determined by the argument |less| |
| 1055 | ** which is called to compare two records. |
| 1056 | ** Arguments: |
| 1057 | ** db Database to sort. |
| 1058 | ** less Comparison function to use. This boolean function |
| 1059 | ** takes two records and returns -1 if the first one |
| 1060 | ** is less than the second one. |
| 1061 | ** Returns: nothing |
| 1062 | **___________________________________________________ */ |
| 1063 | void db_sort(db,less) /* */ |
| 1064 | DB db; /* */ |
| 1065 | int (*less)_ARG((Record,Record)); /* Function pointer */ |
| 1066 | { /* */ |
| 1067 | DBnormal(db) = rec__sort(DBnormal(db), less); /* */ |
| 1068 | } /*------------------------*/ |
| 1069 | |
| 1070 | /*----------------------------------------------------------------------------- |
| 1071 | ** Function: db_string() |
| 1072 | ** Purpose: Try to find the definition of a macro. |
| 1073 | ** First, the local values in the database |db| are considered. |
| 1074 | ** If this fails and |localp| is |false| then the global list |
| 1075 | ** is searched aswell. If all fails |NULL| is returned. |
| 1076 | ** Arguments: |
| 1077 | ** db Database |
| 1078 | ** sym Name of the \BibTeX{} macro to expand. |
| 1079 | ** localp Boolean determining whether the search is only local to the db. |
| 1080 | ** Returns: The macro expansion or |NULL| upon failure. |
| 1081 | **___________________________________________________ */ |
| 1082 | Symbol db_string(db, sym, localp) /* */ |
| 1083 | DB db; /* */ |
| 1084 | Symbol sym; /* */ |
| 1085 | bool localp; /* */ |
| 1086 | { Record rec; /* */ |
| 1087 | /* */ |
| 1088 | if ((rec=DBstring(db))) /* */ |
| 1089 | { /* */ |
| 1090 | for (; rec; rec=NextRecord(rec)) /* */ |
| 1091 | { if (RecordHeap(rec)[0] == sym) /* */ |
| 1092 | { return RecordHeap(rec)[1]; } /* */ |
| 1093 | } /* */ |
| 1094 | for (rec = PrevRecord(DBstring(db)); /* */ |
| 1095 | rec; /* */ |
| 1096 | rec = PrevRecord(rec)) /* */ |
| 1097 | { if (RecordHeap(rec)[0] == sym) /* */ |
| 1098 | { return RecordHeap(rec)[1]; } /* */ |
| 1099 | } /* */ |
| 1100 | } /* */ |
| 1101 | /* */ |
| 1102 | return (localp ? NO_SYMBOL : look_macro(sym, 0));/* */ |
| 1103 | } /*------------------------*/ |
| 1104 | |
| 1105 | /*----------------------------------------------------------------------------- |
| 1106 | ** Function: db_count() |
| 1107 | ** Purpose: Count all entries in a database. This includes normal |
| 1108 | ** as well as special records. The result is stored in a |
| 1109 | ** static array which is reused by |db_count()|. A |
| 1110 | ** pointer to this array is returned. The indices |
| 1111 | ** correspond to the entry types defined with |
| 1112 | ** |add_entry_type()| or declared as symbolic constants |
| 1113 | ** in |entry.h|. |
| 1114 | ** |
| 1115 | ** The end of the array is marked by an element |
| 1116 | ** containing a negative number. In addition the argument |
| 1117 | ** |lp| can point to an integer where the number of valid |
| 1118 | ** elements is stored. If |lp| is |NULL| this step is |
| 1119 | ** omitted. |
| 1120 | ** Arguments: |
| 1121 | ** db Database to count. |
| 1122 | ** lp pointer to an integer for the length. |
| 1123 | ** Returns: Static array containing the statistics. |
| 1124 | **___________________________________________________ */ |
| 1125 | int * db_count(db,lp) /* */ |
| 1126 | DB db; /* */ |
| 1127 | int *lp; /* */ |
| 1128 | { static int *count; /* */ |
| 1129 | static int len = 0; /* */ |
| 1130 | int i; /* */ |
| 1131 | Record rec; /* */ |
| 1132 | /* */ |
| 1133 | for (i = 5; get_entry_type(i) != NULL; i++) ; /* */ |
| 1134 | if (lp) *lp = i; /* */ |
| 1135 | i++; /* */ |
| 1136 | /* */ |
| 1137 | if (len == 0) /* */ |
| 1138 | { len = i; /* */ |
| 1139 | count = (int*)malloc(len*sizeof(int)); /* */ |
| 1140 | } /* */ |
| 1141 | else if (len != i) /* */ |
| 1142 | { len = i; /* */ |
| 1143 | count = (int*)realloc(count,len*sizeof(int)); /* */ |
| 1144 | } /* */ |
| 1145 | if (count == NULL) /* */ |
| 1146 | { OUT_OF_MEMORY("count"); } /* */ |
| 1147 | /* */ |
| 1148 | for (i = 0; i < len; i++) count[i] = 0; /* */ |
| 1149 | count[len-1] = -1; /* */ |
| 1150 | /* */ |
| 1151 | if ((rec=DBstring(db)) != RecordNULL) /* */ |
| 1152 | { while (PrevRecord(rec) != RecordNULL) rec = PrevRecord(rec);/* */ |
| 1153 | while (rec != RecordNULL) /* */ |
| 1154 | { count[BIB_STRING]++; /* */ |
| 1155 | rec = NextRecord(rec); /* */ |
| 1156 | } /* */ |
| 1157 | } /* */ |
| 1158 | if ((rec=DBcomment(db)) != RecordNULL) /* */ |
| 1159 | { while (PrevRecord(rec) != RecordNULL) rec = PrevRecord(rec);/* */ |
| 1160 | while (rec != RecordNULL) /* */ |
| 1161 | { count[BIB_COMMENT]++; /* */ |
| 1162 | rec = NextRecord(rec); /* */ |
| 1163 | } /* */ |
| 1164 | } /* */ |
| 1165 | if ( (rec=DBpreamble(db)) != RecordNULL) /* */ |
| 1166 | { while (PrevRecord(rec) != RecordNULL) rec = PrevRecord(rec);/* */ |
| 1167 | while (rec != RecordNULL) /* */ |
| 1168 | { count[BIB_PREAMBLE]++; /* */ |
| 1169 | rec = NextRecord(rec); /* */ |
| 1170 | } /* */ |
| 1171 | } /* */ |
| 1172 | if ((rec=DBalias(db)) != RecordNULL) /* */ |
| 1173 | { while (PrevRecord(rec) != RecordNULL) rec = PrevRecord(rec);/* */ |
| 1174 | while (rec != RecordNULL) /* */ |
| 1175 | { count[BIB_ALIAS]++; /* */ |
| 1176 | rec = NextRecord(rec); /* */ |
| 1177 | } /* */ |
| 1178 | } /* */ |
| 1179 | if ((rec=DBmodify(db)) != RecordNULL) /* */ |
| 1180 | { while (PrevRecord(rec) != RecordNULL) rec = PrevRecord(rec);/* */ |
| 1181 | while (rec != RecordNULL) /* */ |
| 1182 | { count[BIB_MODIFY]++; /* */ |
| 1183 | rec = NextRecord(rec); /* */ |
| 1184 | } /* */ |
| 1185 | } /* */ |
| 1186 | if ((rec=DBinclude(db)) != RecordNULL) /* */ |
| 1187 | { while (PrevRecord(rec) != RecordNULL) rec = PrevRecord(rec);/* */ |
| 1188 | while (rec != RecordNULL) /* */ |
| 1189 | { count[BIB_INCLUDE]++; /* */ |
| 1190 | rec = NextRecord(rec); /* */ |
| 1191 | } /* */ |
| 1192 | } /* */ |
| 1193 | if ((rec=DBnormal(db)) != RecordNULL) /* */ |
| 1194 | { while (PrevRecord(rec) != RecordNULL) /* */ |
| 1195 | { rec = PrevRecord(rec); } /* */ |
| 1196 | while (rec != RecordNULL) /* */ |
| 1197 | { if (!RecordIsDELETED(rec)) /* */ |
| 1198 | { count[RecordType(rec)]++; } /* */ |
| 1199 | rec = NextRecord(rec); /* */ |
| 1200 | } /* */ |
| 1201 | } /* */ |
| 1202 | /* */ |
| 1203 | return count; /* */ |
| 1204 | } /*------------------------*/ |
| 1205 | |
| 1206 | /*----------------------------------------------------------------------------- |
| 1207 | ** Function: db_xref_undelete() |
| 1208 | ** Type: void |
| 1209 | ** Purpose: Scan through the database and undelete all entries |
| 1210 | ** which are in the transitive closure wrt the crossref |
| 1211 | ** relation. Initially all not deleted entries are in the |
| 1212 | ** set to consider. |
| 1213 | ** Arguments: |
| 1214 | ** db Database to treat |
| 1215 | ** Returns: nothing |
| 1216 | **___________________________________________________ */ |
| 1217 | void db_xref_undelete(db) /* */ |
| 1218 | DB db; /* */ |
| 1219 | { Record rec = DBnormal(db); /* */ |
| 1220 | if (rec == RecordNULL) return; /* No entries left anyhow.*/ |
| 1221 | /* */ |
| 1222 | while (PrevRecord(rec) != RecordNULL) /* Rewind */ |
| 1223 | { rec = PrevRecord(rec); } /* */ |
| 1224 | /* */ |
| 1225 | for (; /* Phase: */ |
| 1226 | rec != RecordNULL; /* For all marked entries*/ |
| 1227 | rec = NextRecord(rec)) /* which have a xref and */ |
| 1228 | { /* mark all xrefs. */ |
| 1229 | if (RecordIsXREF(rec) && /* */ |
| 1230 | !RecordIsDELETED(rec)) /* */ |
| 1231 | { Symbol key = sym_qqq; /* */ |
| 1232 | int count; /* */ |
| 1233 | Record r = rec; /* */ |
| 1234 | /* */ |
| 1235 | for (count=rsc_xref_limit; /* Prevent infinite loop */ |
| 1236 | count >= 0 && RecordIsXREF(r); /* */ |
| 1237 | count--) /* */ |
| 1238 | { /* */ |
| 1239 | if ((key = get_field(db, /* */ |
| 1240 | r, /* */ |
| 1241 | sym_crossref)) == NULL)/* */ |
| 1242 | { count = -1; /* */ |
| 1243 | } /* */ |
| 1244 | else /* */ |
| 1245 | { /* */ |
| 1246 | key = expand_rhs(key, /* */ |
| 1247 | sym_empty, /* */ |
| 1248 | sym_empty, /* */ |
| 1249 | db, /* */ |
| 1250 | true); /* */ |
| 1251 | if ((r=db_search(db,key)) == RecordNULL) /* */ |
| 1252 | { ErrPrintF("*** BibTool: Crossref `%s' not found.\n",/* */ |
| 1253 | SymbolValue(key)); /* */ |
| 1254 | count = -1; /* */ |
| 1255 | } /* */ |
| 1256 | else if (!RecordIsDELETED(r)) /* */ |
| 1257 | { count = -1; } /* */ |
| 1258 | else /* */ |
| 1259 | { ClearRecordDELETED(r); /* */ |
| 1260 | } /* */ |
| 1261 | } /* */ |
| 1262 | } /* */ |
| 1263 | if (count == -1) /* */ |
| 1264 | { ErrPrintF("*** BibTool: Crossref limit exceeded; `%s' possibly looped.\n", |
| 1265 | SymbolValue(key)); /* */ |
| 1266 | } /* */ |
| 1267 | } /* */ |
| 1268 | } /* */ |
| 1269 | } /*------------------------*/ |