| 1 | /*** tex_aux.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/expand.h> |
| 29 | #include <bibtool/sbuffer.h> |
| 30 | #include <bibtool/error.h> |
| 31 | #include <bibtool/rsc.h> |
| 32 | #include <bibtool/key.h> |
| 33 | #include <bibtool/symbols.h> |
| 34 | #include <bibtool/sbuffer.h> |
| 35 | #include <bibtool/wordlist.h> |
| 36 | |
| 37 | /*****************************************************************************/ |
| 38 | /* Internal Programs */ |
| 39 | /*===========================================================================*/ |
| 40 | |
| 41 | #include <bibtool/tex_aux.h> |
| 42 | |
| 43 | #ifdef __STDC__ |
| 44 | #define _ARG(A) A |
| 45 | #else |
| 46 | #define _ARG(A) () |
| 47 | #endif |
| 48 | static void save_ref _ARG((String s)); |
| 49 | |
| 50 | /*****************************************************************************/ |
| 51 | /* External Programs */ |
| 52 | /*===========================================================================*/ |
| 53 | |
| 54 | /*---------------------------------------------------------------------------*/ |
| 55 | |
| 56 | static StringBuffer* aux_sb = (StringBuffer*)0; |
| 57 | |
| 58 | static bool cite_star = true; |
| 59 | |
| 60 | static WordList cite[32] = |
| 61 | { WordNULL, WordNULL, WordNULL, WordNULL, |
| 62 | WordNULL, WordNULL, WordNULL, WordNULL, |
| 63 | WordNULL, WordNULL, WordNULL, WordNULL, |
| 64 | WordNULL, WordNULL, WordNULL, WordNULL, |
| 65 | WordNULL, WordNULL, WordNULL, WordNULL, |
| 66 | WordNULL, WordNULL, WordNULL, WordNULL, |
| 67 | WordNULL, WordNULL, WordNULL, WordNULL, |
| 68 | WordNULL, WordNULL, WordNULL, WordNULL |
| 69 | }; |
| 70 | |
| 71 | /*----------------------------------------------------------------------------- |
| 72 | ** Function: clear_aux() |
| 73 | ** Purpose: Reset the aux table to the initial state. |
| 74 | ** Arguments: none |
| 75 | ** Returns: nothing |
| 76 | **___________________________________________________ */ |
| 77 | void clear_aux() /* */ |
| 78 | { int i; /* */ |
| 79 | cite_star = true; /* */ |
| 80 | for (i = 0; i < 32; i++) /* */ |
| 81 | { free_words(&cite[i], sym_unlink); } /* */ |
| 82 | } /*------------------------*/ |
| 83 | |
| 84 | /*----------------------------------------------------------------------------- |
| 85 | ** Function: foreach_aux() |
| 86 | ** Purpose: Apply the function to all words in the citation list of the |
| 87 | ** aux file. |
| 88 | ** Arguments: |
| 89 | ** fct funtion to apply |
| 90 | ** Returns: |cite_star| |
| 91 | **___________________________________________________ */ |
| 92 | bool foreach_aux(fct) /* */ |
| 93 | bool (fct)_ARG((Symbol)); /* */ |
| 94 | { int i; /* */ |
| 95 | for (i = 0; i < 32; i++) /* */ |
| 96 | { foreach_word(cite[i], fct); } /* */ |
| 97 | return cite_star; /* */ |
| 98 | } /*------------------------*/ |
| 99 | |
| 100 | /*----------------------------------------------------------------------------- |
| 101 | ** Function*: save_ref() |
| 102 | ** Purpose: Store the cite key for selection. |
| 103 | ** If the cite key is "*" then all should be selected. |
| 104 | ** This is written e.g. by \nocite{*} |
| 105 | ** Arguments: |
| 106 | ** key the reference key of * for all |
| 107 | ** Returns: nothing |
| 108 | **___________________________________________________ */ |
| 109 | static void save_ref(key) /* */ |
| 110 | register String key; /* */ |
| 111 | { /* */ |
| 112 | if (cite_star) return; /* */ |
| 113 | /* */ |
| 114 | if (*key == '*' && *(key+1) == '\0') /* */ |
| 115 | { clear_aux(); } /* */ |
| 116 | else { add_word(symbol(key), &cite[*key&31]); } /* */ |
| 117 | } /*------------------------*/ |
| 118 | |
| 119 | /*----------------------------------------------------------------------------- |
| 120 | ** Function: aux_used() |
| 121 | ** Purpose: Check whether a reference key has been requested by the |
| 122 | ** previously read aux file. The request can either be explicit |
| 123 | ** or implicit if a * is used. |
| 124 | ** Arguments: |
| 125 | ** s reference key to check |
| 126 | ** Returns: |true| if the reference is used. |
| 127 | **___________________________________________________ */ |
| 128 | bool aux_used(s) /* */ |
| 129 | Symbol s; /* */ |
| 130 | { /* */ |
| 131 | return cite_star /* */ |
| 132 | || find_word(SymbolValue(s), /* */ |
| 133 | cite[*SymbolValue(s)&31]); /* */ |
| 134 | } /*------------------------*/ |
| 135 | |
| 136 | /*----------------------------------------------------------------------------- |
| 137 | ** Function: read_aux() |
| 138 | ** Purpose: Analyze an aux file. If additional files are |
| 139 | ** requested, e.g. by |\include| instructions in the |
| 140 | ** original source file then those are read as well. Each |
| 141 | ** citation found is remembered and can be queried |
| 142 | ** afterwards. If a |\cite{*}| has been used then only a |
| 143 | ** flag is set and all citation keys are discarded. |
| 144 | ** |
| 145 | ** The aux file contains also the information about the |
| 146 | ** \BibTeX{} files used. For each such file the function |
| 147 | ** |fct| is called with the file name as argument. This |
| 148 | ** function can arrange things that those \BibTeX{} files |
| 149 | ** are read into a database. |
| 150 | ** |
| 151 | ** This function has only a very simple parser for the |
| 152 | ** aux file. Thus it can be confused by legal |
| 153 | ** contents. But a similar thing can happen to \BibTeX{} |
| 154 | ** as well. |
| 155 | ** Arguments: |
| 156 | ** fname The file name of the aux file. |
| 157 | ** fct A function to be called for each \BibTeX{} file requested. |
| 158 | ** verbose Boolean indicating whether messages should be produced |
| 159 | ** indicating the status of the operation. |
| 160 | ** Returns: |true| iff the file could not be opened. |
| 161 | **___________________________________________________ */ |
| 162 | bool read_aux(fname, fct, verbose) /* */ |
| 163 | String fname; /* aux file name */ |
| 164 | void (*fct)_ARG((Symbol)); /* */ |
| 165 | bool verbose; /* */ |
| 166 | { FILE *file; /* */ |
| 167 | int c; /* */ |
| 168 | char* s; /* */ |
| 169 | /* */ |
| 170 | cite_star = false; /* */ |
| 171 | rsc_select = true; /* */ |
| 172 | /* */ |
| 173 | if ( (file=fopen((char*)fname,"r") ) == NULL ) /* */ |
| 174 | { StringBuffer *sb = sbopen(); /* */ |
| 175 | sbputs((char*)fname,sb); /* */ |
| 176 | sbputs(".aux",sb); /* */ |
| 177 | file = fopen(sbflush(sb),"r"); /* */ |
| 178 | if (file != NULL && verbose) /* */ |
| 179 | { VerbosePrint2("Analyzing ", sbflush(sb)); } /* */ |
| 180 | (void)sbclose(sb); /* */ |
| 181 | } else if (verbose) { /* */ |
| 182 | VerbosePrint2("Analyzing ", fname); /* */ |
| 183 | } /* */ |
| 184 | if (file == NULL) /* */ |
| 185 | { ERROR3("aux file ",fname," not found."); /* */ |
| 186 | return true; /* */ |
| 187 | } /* */ |
| 188 | /* */ |
| 189 | rsc_del_q = false; /* */ |
| 190 | /* */ |
| 191 | if (aux_sb == (StringBuffer*)0) /* */ |
| 192 | { aux_sb = sbopen(); } /* */ |
| 193 | /* */ |
| 194 | for (c = getc(file); c != EOF; c = getc(file)) /* */ |
| 195 | { if (c == '\\') /* */ |
| 196 | { for (c = getc(file); /* */ |
| 197 | c != EOF && (is_alpha(c&0xff) || c == '@');/* */ |
| 198 | c = getc(file)) /* */ |
| 199 | { (void)sbputchar(c, aux_sb); } /* */ |
| 200 | s = sbflush(aux_sb); /* */ |
| 201 | sbrewind(aux_sb); /* */ |
| 202 | /* */ |
| 203 | if (strcmp((char*)s, "citation") == 0 ) /* */ |
| 204 | { do /* Save a cite key. */ |
| 205 | { switch (c=getc(file)) /* */ |
| 206 | { case EOF: break; /* */ |
| 207 | case ',': /* */ |
| 208 | case '}': /* */ |
| 209 | s = sbflush(aux_sb); /* */ |
| 210 | sbrewind(aux_sb); /* */ |
| 211 | save_ref((String)s); /* */ |
| 212 | break; /* */ |
| 213 | default: /* */ |
| 214 | (void)sbputchar(ToLower(c&0xff),aux_sb);/* */ |
| 215 | } /* */ |
| 216 | } while ( c != '}' && c != EOF ); /* */ |
| 217 | } /* */ |
| 218 | else if (strcmp((char*)s, "bibdata") == 0 ) /* */ |
| 219 | { c = getc(file); /* Save a bib file name */ |
| 220 | (void)sbputchar((Uchar)c, aux_sb); /* */ |
| 221 | while ( c != '}' && c != EOF ) /* */ |
| 222 | { c = getc(file); /* */ |
| 223 | if ( c == ',' || c == '}' ) /* */ |
| 224 | { s = sbflush(aux_sb); /* */ |
| 225 | sbrewind(aux_sb); /* */ |
| 226 | (*fct)(symbol((String)s)); /* */ |
| 227 | } /* */ |
| 228 | else /* */ |
| 229 | { (void)sbputchar(c, aux_sb); } /* */ |
| 230 | } /* */ |
| 231 | } /* */ |
| 232 | else if (strcmp((char*)s, "@input") == 0 ) /* Read another aux file */ |
| 233 | { while ((c=getc(file)) != '}' && c != EOF) /* */ |
| 234 | { (void)sbputchar(c,aux_sb); } /* */ |
| 235 | s = sbflush(aux_sb); /* */ |
| 236 | sbrewind(aux_sb); /* */ |
| 237 | /* */ |
| 238 | read_aux((String)s, fct, verbose); /* */ |
| 239 | } /* */ |
| 240 | else if (strcmp(s, "endinput") == 0 ) /* At end */ |
| 241 | { break; /* */ |
| 242 | } /* */ |
| 243 | } /* */ |
| 244 | } /* */ |
| 245 | /* */ |
| 246 | (void)fclose(file); /* */ |
| 247 | /* */ |
| 248 | #ifdef DEBUG |
| 249 | { register int i; /* */ |
| 250 | WordList wl; /* */ |
| 251 | /* */ |
| 252 | for (i = 0; i < 32; ++i) /* */ |
| 253 | { ErrPrintF("--- BibTool: %d\n", i); /* */ |
| 254 | for (wl = cite[i]; /* */ |
| 255 | wl != WordNULL; /* */ |
| 256 | wl = NextWord(wl) ) /* */ |
| 257 | { ErrPrintF("%s\n", /* */ |
| 258 | SymbolValue(ThisWord(wl))); } /* */ |
| 259 | } /* */ |
| 260 | } /* */ |
| 261 | #endif |
| 262 | return false; /* */ |
| 263 | } /*------------------------*/ |
| 264 | |
| 265 | /*----------------------------------------------------------------------------- |
| 266 | ** Function: apply_aux() |
| 267 | ** Purpose: This function deletes all entries which are not |
| 268 | ** requested by the recently read aux file. This means |
| 269 | ** that the entry to be kept is either mentioned directly, |
| 270 | ** it is crossreferenced, or all entries are requested |
| 271 | ** with the |\nocite{*}| feature. |
| 272 | ** |
| 273 | ** Note that the entries are in fact not deleted but only |
| 274 | ** marked as deleted. Thus they can be recovered if |
| 275 | ** necessary. |
| 276 | ** Arguments: |
| 277 | ** db Database to clean. |
| 278 | ** Returns: |false| iff all entries are kept because of an |
| 279 | ** explicit or implicit star (*). |
| 280 | **___________________________________________________ */ |
| 281 | bool apply_aux(db) /* */ |
| 282 | DB db; /* */ |
| 283 | { Record rec, rec1; /* */ |
| 284 | /* */ |
| 285 | if (cite_star) return false; /* No selection desired. */ |
| 286 | /* */ |
| 287 | DebugPrint1("Filtering from aux"); /* */ |
| 288 | /* */ |
| 289 | rec = DBnormal(db); /* */ |
| 290 | if (rec == RecordNULL) return true; /* No entries left anyhow.*/ |
| 291 | /* */ |
| 292 | while (PrevRecord(rec) != RecordNULL) /* Rewind */ |
| 293 | { rec = PrevRecord(rec); } /* */ |
| 294 | rec1 = rec; /* */ |
| 295 | /* */ |
| 296 | for ( ; /* Phase 1: */ |
| 297 | rec != RecordNULL; /* Mark all entries */ |
| 298 | rec = NextRecord(rec) ) /* contained in the aux */ |
| 299 | { if (*RecordHeap(rec) && /* file and unmark the */ |
| 300 | find_word(SymbolValue(*RecordHeap(rec)), /* others. */ |
| 301 | cite[(*SymbolValue(*RecordHeap(rec)))&31]) )/* */ |
| 302 | { SetRecordMARK(rec); } /* */ |
| 303 | else /* */ |
| 304 | { ClearRecordMARK(rec); } /* */ |
| 305 | } /* */ |
| 306 | /* */ |
| 307 | for (rec = rec1; /* Phase 2: */ |
| 308 | rec != RecordNULL; /* For all marked entries*/ |
| 309 | rec = NextRecord(rec) ) /* which have a xref and */ |
| 310 | { /* mark all xrefs. */ |
| 311 | if (RecordIsMARKED(rec) && /* */ |
| 312 | RecordIsXREF(rec) && /* */ |
| 313 | !RecordIsDELETED(rec) /* */ |
| 314 | ) /* */ |
| 315 | { Symbol key = sym_qqq; /* */ |
| 316 | int count; /* */ |
| 317 | Record r = rec; /* */ |
| 318 | /* */ |
| 319 | for (count = rsc_xref_limit; /* Prevent infinite loop */ |
| 320 | count >= 0 && /* */ |
| 321 | RecordIsXREF(r) && /* */ |
| 322 | !RecordIsDELETED(r); /* */ |
| 323 | count-- ) /* */ |
| 324 | { key = get_field(db,r,sym_crossref); /* */ |
| 325 | if (key == NULL) /* */ |
| 326 | { count = -1; } /* */ |
| 327 | else /* */ |
| 328 | { key = expand_rhs(key, /* */ |
| 329 | sym_empty, /* */ |
| 330 | sym_empty, /* */ |
| 331 | db, /* */ |
| 332 | true); /* */ |
| 333 | if ((r=db_find(db, key)) == RecordNULL) /* */ |
| 334 | { ErrPrintF("*** BibTool: Crossref `%s' not found.\n",/* */ |
| 335 | SymbolValue(key)); /* */ |
| 336 | count = -1; /* */ |
| 337 | } /* */ |
| 338 | else /* */ |
| 339 | { /* */ |
| 340 | if (RecordIsMARKED(r)) /* */ |
| 341 | { count = -1; } /* */ |
| 342 | SetRecordMARK(r); /* */ |
| 343 | ClearRecordDELETED(r); /* */ |
| 344 | } /* */ |
| 345 | } /* */ |
| 346 | } /* */ |
| 347 | if (count == -1) /* */ |
| 348 | { ErrPrintF("*** BibTool: Crossref limit exceeded; `%s' possibly looped.\n", |
| 349 | SymbolValue(key)); /* */ |
| 350 | } /* */ |
| 351 | } /* */ |
| 352 | } /* */ |
| 353 | /* */ |
| 354 | for (rec = rec1; /* Phase 3: */ |
| 355 | rec != RecordNULL; /* Delete unmarked */ |
| 356 | rec = NextRecord(rec)) /* entries. */ |
| 357 | { if ( !RecordIsMARKED(rec) ) /* */ |
| 358 | { SetRecordDELETED(rec); } /* */ |
| 359 | } /* */ |
| 360 | /* */ |
| 361 | return true; /* */ |
| 362 | } /*------------------------*/ |
| 363 |