Debian Codegraph @

symbols.c

raw symbols SCIP
1 /*** symbols.c ****************************************************************
2 **
3 ** This file is part of BibTool.
4 ** It is distributed under the GNU General Public License.
5 ** See the file COPYING for details.
6 **
7 ** (c) 1996-2020 Gerd Neugebauer
8 **
9 ** Net: [email protected]
10 **
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation; either version 2, or (at your option)
14 ** any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **
25 **-----------------------------------------------------------------------------
26 ** Description:
27 ** This module contains functions which deal with symbols and
28 ** general memory management.
29 ** This module implements a single symbol table.
30 **
31 ** This module required initialization before all functions can
32 ** be used. Especially the symbol table does not exist before
33 ** initialization.
34 **
35 ******************************************************************************/
36
37 #include <bibtool/general.h>
38 #include <bibtool/symbols.h>
39 #include <bibtool/error.h>
40 #include "config.h"
41
42 /*-----------------------------------------------------------------------------
43 ** Typedef*: SymTab
44 ** Purpose: This is the pointer type representing an entry in the symbol
45 ** table. It contains a string and some integers.
46 **
47 **___________________________________________________ */
48 typedef struct STAB /* */
49 { Symbol st_name; /* The symbol itself */
50 #ifndef COMPLEX_SYMBOL
51 int st_count; /* */
52 #endif
53 struct STAB *st_next; /* Pointer to the next item. */
54 } *SymTab;
55
56 /*-----------------------------------------------------------------------------
57 ** Macro*: NextSymTab()
58 ** Type: SymTab
59 ** Purpose: The next |SymTab| of the argument. This macro
60 ** can also be used as lvalue.
61 ** Arguments:
62 ** ST Current |SymTab|
63 ** Returns: The next |SymTab| or |NULL|.
64 **___________________________________________________ */
65 #define NextSymTab(ST) ((ST)->st_next)
66
67 /*-----------------------------------------------------------------------------
68 ** Macro*: SymTabCount()
69 ** Type: int
70 ** Purpose: The count slot of a |SymTab|. This macro
71 ** can also be used as lvalue.
72 ** Arguments:
73 ** ST Current |SymTab|
74 ** Returns: The count slot of |ST|.
75 **___________________________________________________ */
76 #ifdef COMPLEX_SYMBOL
77 #define SymTabCount(SYMTAB) SymbolCount(SymTabSymbol(SYMTAB))
78 #define SymCount(SYM,SYMTAB) SymbolCount(SymTabSymbol(SYMTAB))
79 #else
80 #define SymTabCount(SYMTAB) ((SYMTAB)->st_count)
81 #define SymCount(SYM,SYMTAB) SymTabCount(SYMTAB)
82 #endif
83
84 /*-----------------------------------------------------------------------------
85 ** Macro*: SymTabSymbol()
86 ** Type: Symbol
87 ** Purpose: The name slot of a |SymTab|, i.e.\ the string
88 ** representation. This macro can also be used as lvalue.
89 ** Arguments:
90 ** ST Current |SymTab|
91 ** Returns: The name slot of |ST|.
92 **___________________________________________________ */
93 #define SymTabSymbol(ST) ((ST)->st_name)
94
95 /*****************************************************************************/
96 /* Internal Programs */
97 /*===========================================================================*/
98
99 #ifdef __STDC__
100 #define _ARG(A) A
101 #else
102 #define _ARG(A) ()
103 #endif
104 Symbol symbol _ARG((String s)); /* symbols.c */
105 Symbol sym_extract _ARG((String *sp, bool lowercase));/* symbols.c */
106 char * new_string _ARG((char * s)); /* symbols.c */
107 static SymTab new_sym_tab _ARG((String value)); /* symbols.c */
108 static int hashindex _ARG((String s)); /* symbols.c */
109 void init_symbols _ARG((void)); /* symbols.c */
110 void sym_del _ARG((Symbol sym)); /* symbols.c */
111 #ifdef SYMBOL_DUMP
112 void sym_dump _ARG((void)); /* symbols.c */
113 #endif
114 void sym_gc _ARG((void)); /* symbols.c */
115 void sym_unlink _ARG((Symbol s)); /* symbols.c */
116
117 /*****************************************************************************/
118 /* External Programs */
119 /*===========================================================================*/
120
121 #ifdef HASH_TABLE_SIZE
122 #define HASHMAX HASH_TABLE_SIZE
123 #else
124 #define HASHMAX 307
125 #endif
126
127
128
129 String s_empty = (String)"";
130
131 Symbol sym_empty = NO_SYMBOL; /* */
132 Symbol sym_space = NO_SYMBOL; /* */
133 Symbol sym_star = NO_SYMBOL; /* */
134 Symbol sym_crossref = NO_SYMBOL; /* */
135 Symbol sym_xdata = NO_SYMBOL; /* */
136 Symbol sym_xref = NO_SYMBOL; /* */
137 Symbol sym_qqq = NO_SYMBOL; /* */
138 Symbol sym_comma = NO_SYMBOL; /* */
139 Symbol sym_double_quote = NO_SYMBOL; /* */
140 Symbol sym_open_brace = NO_SYMBOL; /* */
141 Symbol sym_close_brace = NO_SYMBOL; /* */
142 Symbol sym_et = NO_SYMBOL; /* */
143 Symbol sym_key = NO_SYMBOL; /* */
144 Symbol sym_sortkey = NO_SYMBOL; /* */
145
146 /*****************************************************************************/
147 /*** Misc string allocation routine ***/
148 /*****************************************************************************/
149
150 /*-----------------------------------------------------------------------------
151 ** Function: new_string()
152 ** Purpose: Allocate a space for a string and copy the argument
153 ** there. Note this is just a new copy of the memory not
154 ** a symbol!
155 **
156 ** If no more memory is available then an error is raised
157 ** and the program is terminated.
158 ** Arguments:
159 ** s String to duplicate
160 ** Returns: Pointer to newly allocated memory containing a
161 ** duplicate of the argument string.
162 **___________________________________________________ */
163 char * new_string(s) /* */
164 register char * s; /* */
165 { register char * t; /* */
166 if ( (t=malloc((size_t)strlen(s) + 1)) == NULL ) /* */
167 { OUT_OF_MEMORY("string"); } /* */
168 (void)strcpy(t, s); /* */
169 return t; /* */
170 } /*------------------------*/
171
172
173 /*****************************************************************************/
174 /*** Symbol Table Section ***/
175 /*****************************************************************************/
176
177 /*-----------------------------------------------------------------------------
178 ** Function*: new_sym_tab()
179 ** Purpose: Allocate a new |SymTab| structure and fill it with initial
180 ** values.
181 **
182 ** If no more memory is available then an error is raised
183 ** and the program is terminated.
184 ** Arguments:
185 ** value String value of the |SymTab| node.
186 ** Returns: Pointer to a new instance of a |SymTab|.
187 **___________________________________________________ */
188 static SymTab new_sym_tab(value) /* */
189 String value; /* */
190 { register SymTab new_symtab; /* */
191 Symbol sym; /* */
192 /* */
193 if ((new_symtab=(SymTab)malloc(sizeof(struct STAB))) == 0L)/* */
194 { OUT_OF_MEMORY("SymTab"); } /* */
195 /* */
196 #ifdef COMPLEX_SYMBOL
197 if ((sym=(Symbol)malloc(sizeof(sSymbol))) /* */
198 == NO_SYMBOL) /* */
199 { OUT_OF_MEMORY("Symbol"); } /* */
200 SymbolValue(sym) = newString(value); /* */
201 #else
202 sym = newString(value); /* */
203 #endif
204 SymTabSymbol(new_symtab) = sym; /* */
205 SymCount(sym, new_symtab) = 1; /* */
206 NextSymTab(new_symtab) = (SymTab)NULL; /* */
207 return new_symtab; /* */
208 } /*------------------------*/
209
210 /*-----------------------------------------------------------------------------
211 ** Function*: hashindex()
212 ** Purpose: Compute the sum of ASCII values modulo |HASHMAX|
213 ** to be used as an hash index.
214 ** Arguments:
215 ** s string to be analyzed.
216 ** Returns: hash index
217 **___________________________________________________ */
218 static int hashindex(s) /* */
219 String s; /* */
220 { int index = 0; /* */
221 while (*s) index = (index + *(s++)) % HASHMAX; /* */
222 return ( index < 0 ? -index : index ); /* */
223 } /*------------------------*/
224
225 /*-----------------------------------------------------------------------------
226 ** Variable*: sym_tab
227 ** Purpose:
228 **
229 **___________________________________________________ */
230 static SymTab sym_tab[HASHMAX]; /* */
231
232 /*-----------------------------------------------------------------------------
233 ** Function: init_symbols()
234 ** Purpose: Initialize the symbols module.
235 ** The symbol table is cleared. This is not secure when
236 ** the symbols have already been initialized because it
237 ** would lead to a memory leak and a violation of the
238 ** symbol comparison assumption. Thus this case is caught
239 ** and nothing is done when the initialization seems to
240 ** be requested for the second time.
241 **
242 ** If no more memory is available then an error is raised
243 ** and the program is terminated.
244 **
245 ** Note that this function is for internal purposes
246 ** only. The normal user should call |init_bibtool()|
247 ** instead.
248 ** Arguments: none
249 ** Returns: nothing
250 **___________________________________________________ */
251 void init_symbols() /* */
252 { register int i; /* */
253 /* */
254 if (sym_empty) return; /* */
255 /* */
256 for (i = 0; i < HASHMAX; i++) sym_tab[i] = NULL; /* */
257 /* */
258 sym_empty = symbol((String)s_empty); /* */
259 sym_space = symbol((String)" "); /* */
260 sym_star = symbol((String)"*"); /* */
261 sym_crossref = symbol((String)"crossref"); /* */
262 sym_xref = symbol((String)"xref"); /* */
263 sym_xdata = symbol((String)"xdata"); /* */
264 sym_qqq = symbol((String)"???"); /* */
265 sym_comma = symbol((String)","); /* */
266 sym_double_quote = symbol((String)"\""); /* */
267 sym_open_brace = symbol((String)"{"); /* */
268 sym_close_brace = symbol((String)"}"); /* */
269 sym_et = symbol((String)"&"); /* */
270 sym_key = symbol((String)"$key"); /* */
271 sym_sortkey = symbol((String)"$sortkey"); /* */
272 } /*------------------------*/
273
274 #if defined(DEBUG) || defined(COMPLEX_SYMBOL)
275
276 /*-----------------------------------------------------------------------------
277 ** Function*: get_sym_tab()
278 ** Type: SymTab
279 ** Purpose:
280 **
281 ** Arguments:
282 ** sym the symbol to search for
283 ** Returns:
284 **___________________________________________________ */
285 static SymTab* get_sym_tab(sym) /* */
286 register Symbol sym; /* */
287 { register SymTab *stp; /* */
288 /* */
289 if (sym == NO_SYMBOL) return NULL; /* ignore dummies. */
290 /* */
291 for (stp = &sym_tab[hashindex(SymbolValue(sym))];/* */
292 *stp != NULL; /* */
293 stp = &NextSymTab(*stp) ) /* */
294 { if (sym == SymTabSymbol(*stp)) /* */
295 { DebugPrint2("Symbol found ", /* */
296 SymbolValue(sym)); /* */
297 return stp; /* */
298 } /* */
299 } /* */
300 /* */
301 return NULL; /* */
302 } /*------------------------*/
303
304 #endif
305
306 /*-----------------------------------------------------------------------------
307 ** Function: symbol()
308 ** Purpose: Add a symbol to the global symbol table. If the string
309 ** already has a symbol assigned to it then this symbol
310 ** is returned. If the symbol is not static then the use
311 ** count is incremented by |count|.
312 **
313 ** If the symbol does not exist already then a new symbol
314 ** is added to the symbol table and the use count is
315 ** initialized to |count|. A negative value for |count|
316 ** indicates that a static symbol is requested. A static
317 ** symbol will never be deleted from the symbol table.
318 ** Static can be used at places where one does not care
319 ** about the memory occupied.
320 **
321 ** If no more memory is available then an error is raised
322 ** and the program is terminated.
323 **
324 ** See also the macro |symbol()| in |symbols.h| for a
325 ** convenient alternative to this function.
326 ** Arguments:
327 ** s String which should be translated into a symbol.
328 ** Returns: The new symbol.
329 **___________________________________________________ */
330 Symbol symbol(s) /* */
331 String s; /* */
332 { register SymTab *stp; /* */
333 Symbol sym; /* */
334 /* */
335 if (s == StringNULL) return NO_SYMBOL; /* ignore dummies. */
336 /* */
337 DebugPrint2("Lookup symbol ", s); /* */
338 /* */
339 for ( stp = &sym_tab[hashindex(s)]; /* */
340 *stp != NULL; /* */
341 stp = &NextSymTab(*stp) ) /* */
342 { sym = SymTabSymbol(*stp); /* */
343 DebugPrintF3("\tlooking at '%s' == '%s'\n", /* */
344 (char*)s, (char*)SymbolValue(sym)); /* */
345 if (strcmp((char*)s, /* */
346 (char*)SymbolValue(sym)) == 0 ) /* */
347 { DebugPrint2("Symbol found ", /* */
348 SymbolValue(sym)); /* */
349 SymCount(sym,*stp)++; /* */
350 return sym; /* */
351 } /* */
352 } /* */
353 /* */
354 *stp = new_sym_tab(s); /* */
355 DebugPrint2("Symbol created ", /* */
356 SymbolValue(SymTabSymbol(*stp))); /* */
357 return SymTabSymbol(*stp); /* */
358 } /*------------------------*/
359
360 /*-----------------------------------------------------------------------------
361 ** Function: sym_unlink()
362 ** Purpose: Free a symbol since it is no longer used.
363 ** This does not mean that the memory is also freed. The
364 ** symbol can be static or used at other places. The real
365 ** free operation requires the garbage collector
366 ** |sym_gc()| to be called.
367 **
368 ** If the argument is |NULL| or an arbitrary string (no
369 ** symbol) then this case is also dealt with.
370 ** Arguments:
371 ** sym Symbol to be released.
372 ** Returns: nothing
373 **___________________________________________________ */
374 void sym_unlink(sym) /* */
375 register Symbol sym; /* */
376 { /* */
377 if (sym == NO_SYMBOL) return; /* ignore dummies. */
378 #ifdef COMPLEX_SYMBOL
379 if (--SymbolCount(sym) > 0) return; /* double check used. */
380 #else
381 /* */
382 #ifdef DEBUG
383 if (get_sym_tab(sym) == NULL) /* */
384 { ErrPrintF("*** Attempt to free an undefined symbol: %s\n",/* */
385 SymbolValue(sym)); /* */
386 } /* */
387 #endif
388 #endif
389 } /*------------------------*/
390
391 /*-----------------------------------------------------------------------------
392 ** Constant: SYM_PIPE_SIZE
393 ** Type:
394 ** Purpose:
395 **
396 ** Returns:
397 **___________________________________________________ */
398 #define SYM_PIPE_SIZE 32
399
400 #ifdef COMPLEX_SYMBOL
401 static Symbol sym_pipe[SYM_PIPE_SIZE];
402 static int sym_pipe_ptr = 0;
403 #endif
404
405 /*-----------------------------------------------------------------------------
406 ** Function: sym_del()
407 ** Type: void
408 ** Purpose:
409 **
410 ** Arguments:
411 ** sym
412 ** Returns: nothing
413 **___________________________________________________ */
414 void sym_del(sym) /* */
415 Symbol sym; /* */
416 { /* */
417 #ifdef COMPLEX_SYMBOL
418 SymTab *stp; /* */
419 SymTab st; /* */
420 /* */
421 sym_pipe[sym_pipe_ptr] = sym; /* */
422 if ( ++sym_pipe_ptr < SYM_PIPE_SIZE) return; /* */
423 /* */
424 while (--sym_pipe_ptr > 0) /* */
425 { sym = sym_pipe[sym_pipe_ptr]; /* */
426 if (SymbolCount(sym) > 0) continue; /* */
427 /* */
428 stp = get_sym_tab(sym); /* */
429 if (stp == NULL) continue; /* */
430 st = *stp; /* */
431 *stp = NextSymTab(st); /* */
432 free(st); /* */
433 free(SymbolValue(sym)); /* */
434 free(sym); /* */
435 } /* */
436 #endif
437 } /*------------------------*/
438
439 /*-----------------------------------------------------------------------------
440 ** Function*: sym_gc()
441 ** Purpose: This is the garbage collector. It analyzes the symbol
442 ** table and releases all |SymbolTab| nodes not needed
443 ** any more.
444 **
445 ** Right now it is purely experimental. Better let your
446 ** hands off.
447 **
448 ** Arguments: none
449 ** Returns: nothing
450 **___________________________________________________ */
451 void sym_gc() /* */
452 {
453 #ifdef COMPLEX_SYMBOL
454 #else
455 register SymTab st, st2; /* */
456 register int i; /* */
457 /* */
458 for ( i = 0; i < HASHMAX; i++ ) /* */
459 { /* */
460 while (sym_tab[i] && /* */
461 SymTabCount(sym_tab[i]) <= 0) /* */
462 { st = sym_tab[i]; /* */
463 sym_tab[i] = NextSymTab(st); /* */
464 free(SymTabSymbol(st)); /* */
465 free(st); /* */
466 } /* */
467 st = sym_tab[i]; /* */
468 if ( st ) /* */
469 { /* */
470 while ( (st2=NextSymTab(st)) != NULL && /* */
471 SymTabCount(st) <= 0 ) /* */
472 { NextSymTab(st) = NextSymTab(st2); /* */
473 free(SymTabSymbol(st2)); /* */
474 free(st2); /* */
475 } /* */
476 } /* */
477 } /* */
478 #endif
479 } /*------------------------*/
480
481 /*-----------------------------------------------------------------------------
482 ** Function: sym_extract()
483 ** Purpose: Extract a symbol from a string.
484 ** Arguments:
485 ** sp pointer to the string
486 ** lowercase indicate that lowering is requested
487 ** Returns:
488 **___________________________________________________ */
489 Symbol sym_extract(sp, lowercase) /* */
490 register String *sp; /* pointer to first char */
491 bool lowercase; /* */
492 { Uchar c; /* */
493 String t = *sp; /* */
494 Symbol sym; /* */
495 /* */
496 while (is_allowed(**sp)) (*sp)++; /* */
497 c = **sp; /* */
498 **sp = '\0'; /* */
499 if (lowercase) /* */
500 { t = lower(newString(t)); /* */
501 sym = symbol(t); /* */
502 free((void *)t); /* */
503 } else { /* */
504 sym = symbol(t); /* */
505 } /* */
506 **sp = c; /* */
507 return sym; /* */
508 } /*------------------------*/
509
510 /*-----------------------------------------------------------------------------
511 ** Function: free_sym_array()
512 ** Type: void
513 ** Purpose:
514 **
515 ** Arguments:
516 ** sym_arr symbol array
517 ** Returns: nothing
518 **___________________________________________________ */
519 void free_sym_array(sym_arr) /* */
520 Symbol *sym_arr; /* */
521 { Symbol *a; /* */
522 for (a = sym_arr; *a; a++) /* */
523 { if (*a) UnlinkSymbol(*a); } /* */
524 free(sym_arr); /* */
525 } /*------------------------*/
526
527 #ifdef SYMBOL_DUMP
528 /*-----------------------------------------------------------------------------
529 ** Function: sym_dump()
530 ** Purpose: Dump the symbol table to the error stream---see module
531 ** |error.c|.
532 ** The symbols are printed according to their hash value
533 ** and the sequence they are occurring in the buckets. A
534 ** summary of the memory used is also printed.
535 ** Arguments: none
536 ** Returns: nothing
537 **___________________________________________________ */
538 void sym_dump() /* */
539 { register int i,l; /* */
540 register SymTab st; /* */
541 register long len = 0l; /* */
542 register long cnt = 0l; /* */
543 register long used = 0l; /* */
544 /* */
545 for ( i = 0; i < HASHMAX; i++ ) /* */
546 { for ( st = sym_tab[i]; st; st=NextSymTab(st) ) /* */
547 { ErrPrintF2("--- BibTool symbol %4d %s\n", /* */
548 SymTabCount(st), /* */
549 SymbolValue(SymTabSymbol(st))); /* */
550 l = symlen(SymTabSymbol(st)) + 1; /* */
551 len += l; /* */
552 used += l * SymTabCount(st); /* */
553 ++cnt; /* */
554 } /* */
555 } /* */
556 ErrPrintF2("--- BibTool symbol table: %ld bytes for %ld symbols\n",/* */
557 len, cnt); /* */
558 ErrPrintF("--- BibTool symbol table emulates %ld bytes\n",/* */
559 used); /* */
560 } /*------------------------*/
561 #endif