Debian Codegraph @

crossref.c

raw symbols SCIP
1 /*** crossref.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) 2007-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 to expand crossref entries.
28 **
29 ******************************************************************************/
30
31 #include <bibtool/general.h>
32 #include <bibtool/error.h>
33 #include <bibtool/crossref.h>
34 #include <bibtool/symbols.h>
35 #include <bibtool/s_parse.h>
36 #include <bibtool/database.h>
37 #include <bibtool/sbuffer.h>
38 #include <bibtool/rsc.h>
39 #include <bibtool/entry.h>
40
41 /*****************************************************************************/
42 /* Internal Programs */
43 /*===========================================================================*/
44
45 /*****************************************************************************/
46 /* External Programs */
47 /*===========================================================================*/
48
49 /*---------------------------------------------------------------------------*/
50
51 typedef struct mAP /* */
52 { rec_type src_rec; /*source record type index*/
53 Symbol src_field; /* */
54 rec_type dest_rec; /*target record type index*/
55 Symbol dest_field; /* */
56 struct mAP * next_map; /* */
57 } *Map, SMap; /* */
58
59 #define SourceRecord(M) ((M)->src_rec)
60 #define SourceField(M) ((M)->src_field)
61 #define DestinationRecord(M) ((M)->dest_rec)
62 #define DestinationField(M) ((M)->dest_field)
63 #define NextMap(M) ((M)->next_map)
64
65 /*-----------------------------------------------------------------------------
66 ** Function*: new_map()
67 ** Type: Map
68 ** Purpose: allocate a new Map structure
69 ** Arguments:
70 ** sr source record type index
71 ** sf source field name
72 ** dr destination record type index
73 ** df destination field name
74 ** Returns: the new map
75 **___________________________________________________ */
76 static Map new_map(sr,sf,dr,df) /* */
77 rec_type sr; /* */
78 Symbol sf; /* */
79 rec_type dr; /* */
80 Symbol df; /* */
81 { Map m; /* */
82 if ( (m = (Map)malloc(sizeof(SMap))) == (Map)NULL)/* */
83 { OUT_OF_MEMORY("map"); } /* */
84 /* */
85 SourceRecord(m) = sr; /* */
86 SourceField(m) = sf; /* */
87 DestinationRecord(m) = dr; /* */
88 DestinationField(m) = df; /* */
89 NextMap(m) = NULL; /* */
90 return m; /* */
91 } /*------------------------*/
92
93 /*-----------------------------------------------------------------------------
94 ** Function*: free_map()
95 ** Type: void
96 ** Purpose: Free a chain of map structures
97 ** Arguments:
98 ** m the map
99 ** Returns: nothing
100 **___________________________________________________ */
101 static void free_map(m) /* */
102 Map m; /* */
103 { Map nxt; /* */
104 while (m) /* */
105 { nxt = NextMap(m); /* */
106 free(m); /* */
107 m = nxt; /* */
108 } /* */
109 } /*------------------------*/
110
111 #define MAP_SIZE 101
112
113 static Map map[MAP_SIZE]; /* */
114
115 #define MAP_INDEX(SR,SF,DR) \
116 (int)((((SR) % 73) + \
117 ((long)(SF) & 0xfff) + \
118 ((DR) % 1023)) % MAP_SIZE);
119
120 /*-----------------------------------------------------------------------------
121 ** Function: clear_map()
122 ** Type: void
123 ** Purpose: Reset the map to it's initial state where no elements are
124 ** contained.
125 ** Arguments: none
126 ** Returns: nothing
127 **___________________________________________________ */
128 void clear_map() /* */
129 { register int i; /* */
130 /* */
131 for (i = 0; i < MAP_SIZE; i++) /* */
132 { free_map(map[i]); /* */
133 map[i] = NULL; /* */
134 } /* */
135 } /*------------------------*/
136
137 /*-----------------------------------------------------------------------------
138 ** Function: map_add()
139 ** Type: int
140 ** Purpose: Add or overwrite a filed name mapping.
141 **
142 ** Arguments:
143 ** s_rec the index of the source record type
144 ** s_fld the source field name
145 ** d_rec the index of the destination record type
146 ** d_fld the destination field name
147 ** Returns: nothing
148 **___________________________________________________ */
149 void map_add(s_rec,s_fld,d_rec,d_fld) /* */
150 rec_type s_rec; /* */
151 Symbol s_fld; /* */
152 rec_type d_rec; /* */
153 Symbol d_fld; /* */
154 { int idx; /* */
155 Map m; /* */
156 /* */
157 idx = MAP_INDEX(s_rec, s_fld, d_rec); /* */
158 m = map[idx]; /* */
159 if (m == NULL ) /* */
160 { map[idx] = new_map(s_rec, s_fld, d_rec, d_fld);/* */
161 return; /* */
162 } /* */
163 /* */
164 for (;;) /* */
165 { if (SourceRecord(m) == s_rec && /* */
166 SourceField(m) == s_fld && /* */
167 DestinationRecord(m) == d_rec ) /* */
168 { DestinationField(m) = d_fld; /* */
169 return; /* */
170 } /* */
171 if (NextMap(m) == NULL ) /* */
172 { NextMap(m) = new_map(s_rec, s_fld, /* */
173 d_rec, d_fld); /* */
174 return; /* */
175 } /* */
176 m = NextMap(m); /* */
177 } /* */
178 } /*------------------------*/
179
180 /*-----------------------------------------------------------------------------
181 ** Function: map_get()
182 ** Type: Symbol
183 ** Purpose: Getter for a map element.
184 ** Arguments:
185 ** s_rec the index of the source entry type
186 ** s_fld the name of the source field
187 ** d_rec the index of the destination entry type
188 ** Returns: the new field name or |NO_SYMBOL|
189 **___________________________________________________ */
190 Symbol map_get(s_rec, s_fld, d_rec) /* */
191 rec_type s_rec; /* */
192 Symbol s_fld; /* */
193 rec_type d_rec; /* */
194 { int i = MAP_INDEX(s_rec, s_fld, d_rec); /* */
195 Map m = map[i]; /* */
196 /* */
197 for (; m; m = NextMap(m)) /* */
198 { if (SourceRecord(m) == s_rec && /* */
199 SourceField(m) == s_fld && /* */
200 DestinationRecord(m) == d_rec ) /* */
201 { return DestinationField(m); } /* */
202 } /* */
203 return NO_SYMBOL; /* */
204 } /*------------------------*/
205
206 /*-----------------------------------------------------------------------------
207 ** Function: crossref_map()
208 ** Type: int
209 ** Purpose:
210 **
211 ** Arguments:
212 ** spec the argument
213 ** Returns: nothing
214 **___________________________________________________ */
215 void crossref_map(spec) /* */
216 String spec; /* */
217 { Symbol *src, *dest; /* */
218 Symbol src_field, dest_field; /* */
219 Symbol *sp, *dp; /* */
220 rec_type s_rec, d_rec; /* */
221 /* */
222 sp_open(spec); /* */
223 if ((src = sp_symbols(&spec)) == NULL) return; /* */
224 sp_skip(&spec); /* */
225 if ((src_field = SParseSymbol(&spec)) == NULL) return;/* */
226 sp_skip(&spec); /* */
227 if ((dest = sp_symbols(&spec)) == NULL) return; /* */
228 sp_skip(&spec); /* */
229 if ((dest_field = SParseSymbol(&spec)) == NULL) return;/* */
230 sp_eos(&spec); /* */
231 sp_close(); /* */
232 /* */
233 for (sp = src; *sp; sp++) /* */
234 { s_rec = find_entry_type(SymbolValue(*sp)); /* */
235 if (s_rec == BIB_NOOP) /* */
236 { WARNING3("Unknown source entry type `", /* */
237 SymbolValue(*sp), /* */
238 "'. Mapping ignored."); /* */
239 continue; /* */
240 } /* */
241 /* */
242 for (dp = dest; *dp; dp++) /* */
243 { d_rec = find_entry_type(SymbolValue(*dp)); /* */
244 if (d_rec == BIB_NOOP) /* */
245 { WARNING3("Unknown destination entry type `",/* */
246 SymbolValue(*dp), /* */
247 "'. Mapping ignored."); /* */
248 continue; /* */
249 } /* */
250 /* */
251 map_add(s_rec, src_field, d_rec, dest_field);/* */
252 } /* */
253 } /* */
254 /* */
255 #ifndef COMPLEX_SYMBOL
256 free(src); /* */
257 free(dest); /* */
258 #endif
259 } /*------------------------*/
260
261 static Symbol NONE = (Symbol)"x"; /* TODO */
262
263 /*-----------------------------------------------------------------------------
264 ** Function*: insert_record()
265 ** Type: bool
266 ** Purpose: Find a record and insert some new entryies.
267 ** Arguments:
268 ** db the database
269 ** rec the record
270 ** hp the heap pointer
271 ** s the key of the entry
272 ** msg the message prefix for an unknown entry message
273 ** Returns: |true| iff the record could be inserted
274 **___________________________________________________ */
275 static bool insert_record(db, rec, hp, s, msg) /* */
276 DB db; /* */
277 Record rec; /* */
278 register Symbol *hp; /* */
279 Symbol s; /* */
280 String msg; /* */
281 { Record r; /* */
282 Symbol t, ms; /* */
283 int i; /* */
284 /* */
285 if ((r = db_find(db, s)) == RecordNULL) /* */
286 { ERROR3(msg," entry not found: ", /* */
287 (char*)SymbolValue(s)); /* */
288 return false; /* */
289 } /* */
290 /* */
291 for (i = RecordFree(r), hp = RecordHeap(r); /* visit all fields */
292 i > 0; /* */
293 i -= 2) /* */
294 { s = *hp++; /* */
295 t = *hp++; /* */
296 if (t != NO_SYMBOL) /* */
297 { ms = map_get(RecordType(r), s, /* */
298 RecordType(rec)); /* */
299 provide_to_record(rec, ms ? ms : s, t); /* */
300 } /* */
301 } /* */
302 return true; /* */
303 } /*------------------------*/
304
305 /*-----------------------------------------------------------------------------
306 ** Function: expand_crossref()
307 ** Purpose: Expand all items inherited via a crossref.
308 ** Arguments:
309 ** db Database containing the entries.
310 ** rec The record to expand
311 ** Returns: |false| iff an error has occured
312 **___________________________________________________ */
313 bool expand_crossref(db, rec) /* */
314 DB db; /* */
315 Record rec; /* */
316 { register Symbol *hp; /* */
317 register int i; /* */
318 String x; /* */
319 Symbol s; /* */
320 Record r = rec; /* */
321 int limit = rsc_xref_limit; /* */
322 Symbol xdata = (rsc_expand_xdata /* */
323 ? sym_xdata /* */
324 : NONE); /* */
325 Symbol crossref = (rsc_expand_crossref /* */
326 ? sym_crossref /* */
327 : NONE); /* */
328 /* */
329 DebugPrint1("expand_crossref"); /* */
330 /* */
331 while (RecordIsXREF(r) && limit-- >= 0) /* */
332 { /* */
333 for (i = RecordFree(r), hp = RecordHeap(r); /* search crossref field */
334 i > 0 /* */
335 && (*hp != crossref && *hp != xdata); /* */
336 i -= 2, hp += 2) /* */
337 { } /* */
338 /* */
339 if (i <= 0) /* */
340 { DebugPrint1("*** No crossref found."); /* */
341 return false; /* */
342 } /* */
343 else if (*hp == sym_crossref) /* ---------------------- */
344 { /* */
345 if (rec == r) { *hp = NULL; } /* Delete the first xref */
346 x = SymbolValue(*++hp); /* */
347 x++; /* */
348 sp_open(x); /* Try to extract */
349 if ((s = SParseSymbol(&x)) == NO_SYMBOL) /* the crossref as symbol*/
350 { return false; } /* */
351 /* */
352 insert_record(db, rec, hp, s, "Crossref"); /* */
353 } /* */
354 else if (*hp == sym_xdata) /* ---------------------- */
355 { /* */
356 *hp = NULL; /* Delete the first xref */
357 x = SymbolValue(*++hp); /* */
358 x++; /* */
359 sp_open(x); /* Try to extract */
360 /* */
361 if (sp_expect(&x, (String)"}", 0) ) /* */
362 return false; /* */
363 for (;;) /* */
364 { if ((s = SParseSymbol(&x)) == NO_SYMBOL) /* */
365 { return true; } /* */
366 /* */
367 insert_record(db, rec, hp, s, "XData"); /* */
368 /* */
369 if (sp_expect(&x, (String)"}", 0) ) break; /* */
370 sp_expect(&x, (String)",", 1); /* */
371 } /* */
372 } /* */
373 } /* */
374 return false; /* */
375 } /*------------------------*/