File: symbols.c

package info (click to toggle)
bibtool 2.68%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 2,608 kB
  • sloc: ansic: 11,808; perl: 8,145; makefile: 550; sh: 307; tcl: 51
file content (561 lines) | stat: -rw-r--r-- 23,385 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*** symbols.c ****************************************************************
** 
** This file is part of BibTool.
** It is distributed under the GNU General Public License.
** See the file COPYING for details.
** 
** (c) 1996-2020 Gerd Neugebauer
** 
** Net: gene@gerd-neugebauer.de
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
**-----------------------------------------------------------------------------
** Description:
**	This module contains functions which deal with symbols and
**	general memory management. 
**	This module implements a single symbol table.
**
**	This module required initialization before all functions can
**	be used. Especially the symbol table does not exist before
**	initialization. 
**
******************************************************************************/

#include <bibtool/general.h>
#include <bibtool/symbols.h>
#include <bibtool/error.h>
#include "config.h"

/*-----------------------------------------------------------------------------
** Typedef*:	SymTab
** Purpose:	This is the pointer type representing an entry in the symbol
**		table. It contains a string and some integers.
**		
**___________________________________________________			     */
 typedef struct STAB		/*                                           */
  { Symbol	st_name;	/* The symbol itself			     */
#ifndef COMPLEX_SYMBOL
    int		st_count;	/* 			                     */
#endif
    struct STAB *st_next;	/* Pointer to the next item.                 */
  } *SymTab;

/*-----------------------------------------------------------------------------
** Macro*:	NextSymTab()
** Type:	SymTab
** Purpose:	The next |SymTab| of the argument. This macro
**		can also be used as lvalue.
** Arguments:
**	ST	Current |SymTab|
** Returns:	The next |SymTab| or |NULL|.
**___________________________________________________			     */
#define NextSymTab(ST)	((ST)->st_next)

/*-----------------------------------------------------------------------------
** Macro*:	SymTabCount()
** Type:	int
** Purpose:	The count slot of a |SymTab|. This macro
**		can also be used as lvalue.
** Arguments:
**	ST	Current |SymTab|
** Returns:	The count slot of |ST|.
**___________________________________________________			     */
#ifdef COMPLEX_SYMBOL
#define SymTabCount(SYMTAB)  SymbolCount(SymTabSymbol(SYMTAB))
#define SymCount(SYM,SYMTAB) SymbolCount(SymTabSymbol(SYMTAB))
#else
#define SymTabCount(SYMTAB)  ((SYMTAB)->st_count)
#define SymCount(SYM,SYMTAB) SymTabCount(SYMTAB)
#endif

/*-----------------------------------------------------------------------------
** Macro*:	SymTabSymbol()
** Type:	Symbol
** Purpose:	The name slot of a |SymTab|, i.e.\ the string
**		representation. This macro can also be used as lvalue.
** Arguments:
**	ST	Current |SymTab|
** Returns:	The name slot of |ST|.
**___________________________________________________			     */
#define SymTabSymbol(ST) ((ST)->st_name)

/*****************************************************************************/
/* Internal Programs							     */
/*===========================================================================*/

#ifdef __STDC__
#define _ARG(A) A
#else
#define _ARG(A) ()
#endif
 Symbol  symbol _ARG((String s));	   	   /* symbols.c              */
 Symbol  sym_extract _ARG((String *sp, bool lowercase));/* symbols.c         */
 char * new_string _ARG((char * s));		   /* symbols.c              */
 static SymTab new_sym_tab _ARG((String value));   /* symbols.c              */
 static int hashindex _ARG((String s));		   /* symbols.c              */
 void init_symbols _ARG((void));		   /* symbols.c              */
 void sym_del _ARG((Symbol sym));		   /* symbols.c              */
#ifdef SYMBOL_DUMP
 void sym_dump _ARG((void));			   /* symbols.c              */
#endif
 void sym_gc _ARG((void));			   /* symbols.c              */
 void sym_unlink _ARG((Symbol s));		   /* symbols.c              */

/*****************************************************************************/
/* External Programs							     */
/*===========================================================================*/

#ifdef HASH_TABLE_SIZE
#define HASHMAX HASH_TABLE_SIZE
#else
#define HASHMAX 307
#endif



 String s_empty		  = (String)"";

 Symbol sym_empty	  = NO_SYMBOL;		   /*			     */
 Symbol sym_space	  = NO_SYMBOL;		   /*			     */
 Symbol sym_star	  = NO_SYMBOL;		   /*			     */
 Symbol sym_crossref	  = NO_SYMBOL;		   /*			     */
 Symbol sym_xdata	  = NO_SYMBOL;		   /*			     */
 Symbol sym_xref	  = NO_SYMBOL;		   /*			     */
 Symbol sym_qqq		  = NO_SYMBOL;		   /*			     */
 Symbol sym_comma	  = NO_SYMBOL;		   /*			     */
 Symbol sym_double_quote  = NO_SYMBOL;		   /*			     */
 Symbol sym_open_brace	  = NO_SYMBOL;		   /*			     */
 Symbol sym_close_brace	  = NO_SYMBOL;		   /*			     */
 Symbol sym_et		  = NO_SYMBOL;		   /*			     */
 Symbol sym_key		  = NO_SYMBOL;		   /*			     */
 Symbol sym_sortkey	  = NO_SYMBOL;		   /*			     */

/*****************************************************************************/
/***			Misc string allocation routine			   ***/
/*****************************************************************************/

/*-----------------------------------------------------------------------------
** Function:	new_string()
** Purpose:	Allocate a space for a string and copy the argument
**		there. Note this is just a new copy of the memory not
**		a symbol!
**
**		If no more memory is available then an error is raised
**		and the program is terminated.
** Arguments:
**	s	String to duplicate
** Returns:	Pointer to newly allocated memory containing a
**		duplicate of the argument string.
**___________________________________________________			     */
char * new_string(s)				   /*			     */
  register char * s;				   /*			     */
{ register char * t;				   /*			     */
  if ( (t=malloc((size_t)strlen(s) + 1)) == NULL ) /*			     */
  { OUT_OF_MEMORY("string"); }	   		   /*			     */
  (void)strcpy(t, s);				   /*			     */
  return t;					   /*			     */
}						   /*------------------------*/


/*****************************************************************************/
/***			     Symbol Table Section			   ***/
/*****************************************************************************/

/*-----------------------------------------------------------------------------
** Function*:	new_sym_tab()
** Purpose:	Allocate a new |SymTab| structure and fill it with initial
**		values.
**
**		If no more memory is available then an error is raised
**		and the program is terminated.
** Arguments:
**	value	String value of the |SymTab| node.
** Returns:	Pointer to a new instance of a |SymTab|.
**___________________________________________________			     */
static SymTab new_sym_tab(value)		   /*			     */
  String value;			   	   	   /*			     */
{ register SymTab new_symtab;		   	   /*			     */
  Symbol sym;					   /*                        */
 						   /*			     */
  if ((new_symtab=(SymTab)malloc(sizeof(struct STAB))) == 0L)/*              */
  { OUT_OF_MEMORY("SymTab"); }   		   /*			     */
 						   /*                        */
#ifdef COMPLEX_SYMBOL
  if ((sym=(Symbol)malloc(sizeof(sSymbol)))	   /*                        */
      == NO_SYMBOL)				   /*                        */
  { OUT_OF_MEMORY("Symbol"); }   		   /*			     */
  SymbolValue(sym) = newString(value);		   /*                        */
#else
  sym = newString(value);			   /*                        */
#endif
  SymTabSymbol(new_symtab)  = sym;		   /*			     */
  SymCount(sym, new_symtab) = 1;		   /*			     */
  NextSymTab(new_symtab)    = (SymTab)NULL;	   /*			     */
  return new_symtab;			   	   /*			     */
}						   /*------------------------*/

/*-----------------------------------------------------------------------------
** Function*:	hashindex()
** Purpose:	Compute the sum of ASCII values modulo |HASHMAX|
**		to be used as an hash index.
** Arguments:
**	s	string to be analyzed.
** Returns:	hash index
**___________________________________________________			     */
static int hashindex(s)				   /*                        */
  String s;					   /*                        */
{ int	index = 0;				   /*                        */
  while (*s) index = (index + *(s++)) % HASHMAX;   /*                        */
  return ( index < 0 ? -index : index );	   /*                        */
}						   /*------------------------*/

/*-----------------------------------------------------------------------------
** Variable*:	sym_tab
** Purpose:	
**		
**___________________________________________________			     */
 static SymTab sym_tab[HASHMAX];		   /*                        */

/*-----------------------------------------------------------------------------
** Function:	init_symbols()
** Purpose:	Initialize the symbols module.
**		The symbol table is cleared. This is not secure when
**		the symbols have already been initialized because it
**		would lead to a memory leak and a violation of the
**		symbol comparison assumption. Thus this case is caught
**		and nothing is done when the initialization seems to
**		be requested for the second time.
**
**		If no more memory is available then an error is raised
**		and the program is terminated.
**
**		Note that this function is for internal purposes
**		only. The normal user should call |init_bibtool()|
**		instead.
** Arguments:	none
** Returns:	nothing
**___________________________________________________			     */
void init_symbols()				   /*			     */
{ register int i;				   /*			     */
						   /*			     */
  if (sym_empty) return;		   	   /*                        */
 						   /*                        */
  for (i = 0; i < HASHMAX; i++) sym_tab[i] = NULL; /*			     */
 						   /*                        */
  sym_empty        = symbol((String)s_empty);	   /*                        */
  sym_space        = symbol((String)" ");	   /*                        */
  sym_star         = symbol((String)"*");	   /*                        */
  sym_crossref     = symbol((String)"crossref");   /*                        */
  sym_xref         = symbol((String)"xref");	   /*                        */
  sym_xdata        = symbol((String)"xdata");	   /*                        */
  sym_qqq          = symbol((String)"???");	   /*                        */
  sym_comma        = symbol((String)",");	   /*                        */
  sym_double_quote = symbol((String)"\"");	   /*                        */
  sym_open_brace   = symbol((String)"{");	   /*                        */
  sym_close_brace  = symbol((String)"}");	   /*                        */
  sym_et	   = symbol((String)"&");	   /*                        */
  sym_key	   = symbol((String)"$key");	   /*                        */
  sym_sortkey	   = symbol((String)"$sortkey");   /*                        */
}						   /*------------------------*/

#if defined(DEBUG) || defined(COMPLEX_SYMBOL)

/*-----------------------------------------------------------------------------
** Function*:	get_sym_tab()
** Type:	SymTab
** Purpose:	
**		
** Arguments:
**	sym	the symbol to search for
** Returns:	
**___________________________________________________			     */
static SymTab* get_sym_tab(sym)			   /*			     */
  register Symbol sym;			   	   /*			     */
{ register SymTab *stp;			   	   /*			     */
						   /*			     */
  if (sym == NO_SYMBOL) return NULL;	   	   /* ignore dummies.	     */
 						   /*                        */
  for (stp = &sym_tab[hashindex(SymbolValue(sym))];/*			     */
       *stp != NULL;		   		   /*			     */
       stp = &NextSymTab(*stp) )		   /*			     */
  { if (sym == SymTabSymbol(*stp))	   	   /*		             */
    { DebugPrint2("Symbol found ",		   /*                        */
		  SymbolValue(sym));  		   /*                        */
      return stp;			   	   /*			     */
    }						   /*			     */
  }						   /*			     */
 						   /*                        */
  return NULL;			   		   /*			     */
}						   /*------------------------*/

#endif

/*-----------------------------------------------------------------------------
** Function:	symbol()
** Purpose:	Add a symbol to the global symbol table. If the string
**		already has a symbol assigned to it then this symbol
**		is returned. If the symbol is not static then the use
**		count is incremented by |count|.
**
**		If the symbol does not exist already then a new symbol
**		is added to the symbol table and the use count is
**		initialized to |count|. A negative value for |count|
**		indicates that a static symbol is requested. A static
**		symbol will never be deleted from the symbol table.
**		Static can be used at places where one does not care
**		about the memory occupied.
**
**		If no more memory is available then an error is raised
**		and the program is terminated.
**
**		See also the macro |symbol()| in |symbols.h| for a
**		convenient alternative to this function.
** Arguments:
**	s	String which should be translated into a symbol.
** Returns:	The new symbol.
**___________________________________________________			     */
Symbol symbol(s)			   	   /*			     */
  String  s;				   	   /*			     */
{ register SymTab *stp;			   	   /*			     */
  Symbol sym;				   	   /*                        */
						   /*			     */
  if (s == StringNULL) return NO_SYMBOL;	   /* ignore dummies.	     */
 						   /*                        */
  DebugPrint2("Lookup symbol ", s);  		   /*                        */
 						   /*                        */
  for ( stp = &sym_tab[hashindex(s)];		   /*			     */
       *stp != NULL;		   		   /*			     */
        stp = &NextSymTab(*stp) )		   /*			     */
  { sym	= SymTabSymbol(*stp);			   /*                        */
    DebugPrintF3("\tlooking at '%s' == '%s'\n",	   /*                        */
	      (char*)s, (char*)SymbolValue(sym));  /*                        */
    if (strcmp((char*)s,			   /*                        */
	       (char*)SymbolValue(sym)) == 0 )	   /*		             */
    { DebugPrint2("Symbol found ",		   /*                        */
		  SymbolValue(sym));  		   /*                        */
      SymCount(sym,*stp)++; 		   	   /*			     */
      return sym;				   /*			     */
    }						   /*                        */
  }						   /*			     */
 						   /*                        */
  *stp = new_sym_tab(s);  		   	   /*			     */
  DebugPrint2("Symbol created ",		   /*                        */
	      SymbolValue(SymTabSymbol(*stp)));	   /*                        */
  return SymTabSymbol(*stp);			   /*			     */
}						   /*------------------------*/

/*-----------------------------------------------------------------------------
** Function:	sym_unlink()
** Purpose:	Free a symbol since it is no longer used.
**		This does not mean that the memory is also freed. The
**		symbol can be static or used at other places. The real
**		free operation requires the garbage collector
**		|sym_gc()| to be called.
**
**		If the argument is |NULL| or an arbitrary string (no
**		symbol) then this case is also dealt with.
** Arguments:
**	sym	Symbol to be released.
** Returns:	nothing
**___________________________________________________			     */
void sym_unlink(sym)				   /*			     */
  register Symbol sym;			   	   /*			     */
{						   /*                        */
  if (sym == NO_SYMBOL) return;		   	   /* ignore dummies.	     */
#ifdef COMPLEX_SYMBOL
  if (--SymbolCount(sym) > 0) return;		   /* double check used.     */
#else
 						   /*                        */
#ifdef DEBUG
  if (get_sym_tab(sym) == NULL)		   	   /*                        */
  { ErrPrintF("*** Attempt to free an undefined symbol: %s\n",/*             */
	      SymbolValue(sym));		   /*                        */
  }						   /*                        */
#endif
#endif
}						   /*------------------------*/

/*-----------------------------------------------------------------------------
** Constant:	SYM_PIPE_SIZE
** Type:	
** Purpose:	
**		
** Returns:	
**___________________________________________________			     */
#define SYM_PIPE_SIZE 32

#ifdef COMPLEX_SYMBOL
 static Symbol sym_pipe[SYM_PIPE_SIZE];
 static int sym_pipe_ptr = 0;
#endif

/*-----------------------------------------------------------------------------
** Function:	sym_del()
** Type:	void
** Purpose:	
**		
** Arguments:
**	sym	
** Returns:	nothing
**___________________________________________________			     */
void sym_del(sym)				   /*                        */
  Symbol sym;		   			   /*                        */
{						   /*                        */
#ifdef COMPLEX_SYMBOL
  SymTab *stp;					   /*                        */
  SymTab st;					   /*                        */
 						   /*                        */
  sym_pipe[sym_pipe_ptr] = sym;			   /*                        */
  if ( ++sym_pipe_ptr < SYM_PIPE_SIZE) return;	   /*                        */
 						   /*                        */
  while (--sym_pipe_ptr > 0)			   /*                        */
  { sym = sym_pipe[sym_pipe_ptr];		   /*                        */
    if (SymbolCount(sym) > 0) continue;		   /*                        */
						   /*                        */
    stp = get_sym_tab(sym);			   /*                        */
    if (stp == NULL) continue;			   /*                        */
    st	 = *stp;				   /*                        */
    *stp = NextSymTab(st);			   /*                        */
    free(st);					   /*                        */
    free(SymbolValue(sym));			   /*                        */
    free(sym);					   /*                        */
  }						   /*                        */
#endif
}						   /*------------------------*/

/*-----------------------------------------------------------------------------
** Function*:	sym_gc()
** Purpose:	This is the garbage collector. It analyzes the symbol
**		table and releases all |SymbolTab| nodes not needed
**		any more.
**
**		Right now it is purely experimental. Better let your
**		hands off.
**
** Arguments:	none
** Returns:	nothing
**___________________________________________________			     */
void sym_gc()					   /*                        */
{
#ifdef COMPLEX_SYMBOL
#else
  register SymTab st, st2;			   /*			     */
  register int i;				   /*                        */
  						   /*                        */
  for ( i = 0; i < HASHMAX; i++ )		   /*			     */
  {						   /*                        */
    while (sym_tab[i] &&			   /*                        */
	   SymTabCount(sym_tab[i]) <= 0)	   /*                        */
    { st = sym_tab[i];				   /*                        */
      sym_tab[i] = NextSymTab(st);		   /*                        */
      free(SymTabSymbol(st));			   /*                        */
      free(st);					   /*                        */
    }						   /*                        */
    st = sym_tab[i];				   /*                        */
    if ( st )					   /*                        */
    {						   /*                        */
      while ( (st2=NextSymTab(st)) != NULL &&	   /*                        */
	      SymTabCount(st) <= 0 )		   /*                        */
      { NextSymTab(st) = NextSymTab(st2);	   /*                        */
        free(SymTabSymbol(st2));		   /*                        */
	free(st2);				   /*                        */
      }						   /*                        */
    }						   /*                        */
  }						   /*			     */
#endif
}						   /*------------------------*/

/*-----------------------------------------------------------------------------
** Function:	sym_extract()
** Purpose:	Extract a symbol from a string.
** Arguments:
**	sp	pointer to the string
**	lowercase	indicate that lowering is requested
** Returns:	
**___________________________________________________			     */
Symbol  sym_extract(sp, lowercase)		   /*			     */
  register String *sp;				   /* pointer to first char  */
  bool   lowercase;				   /*                        */
{ Uchar  c;					   /*			     */
  String t = *sp;				   /*                        */
  Symbol sym;					   /*                        */
						   /*			     */
  while (is_allowed(**sp)) (*sp)++;		   /*                        */
  c    = **sp;					   /*			     */
  **sp = '\0';					   /*			     */
  if (lowercase)				   /*                        */
  { t	= lower(newString(t));	   		   /*                        */
    sym	= symbol(t);		   	   	   /*			     */
    free((void *)t);			   	   /*                        */
  } else {					   /*                        */
    sym  = symbol(t);			   	   /*			     */
  }						   /*                        */
  **sp = c;					   /*			     */
  return sym;					   /*			     */
}						   /*------------------------*/

/*-----------------------------------------------------------------------------
** Function:	free_sym_array()
** Type:	void
** Purpose:	
**		
** Arguments:
**	sym_arr	symbol array
** Returns:	nothing
**___________________________________________________			     */
void free_sym_array(sym_arr)			   /*                        */
  Symbol *sym_arr;				   /*                        */
{ Symbol *a;					   /*                        */
  for (a = sym_arr; *a; a++)			   /*                        */
  { if (*a) UnlinkSymbol(*a); }			   /*                        */
  free(sym_arr);				   /*                        */
}						   /*------------------------*/

#ifdef SYMBOL_DUMP
/*-----------------------------------------------------------------------------
** Function:	sym_dump()
** Purpose:	Dump the symbol table to the error stream---see module
**		|error.c|. 
**		The symbols are printed according to their hash value
**		and the sequence they are occurring in the buckets.  A
**		summary of the memory used is also printed.
** Arguments:	none
** Returns:	nothing
**___________________________________________________			     */
void sym_dump()					   /*			     */
{ register int	  i,l;			   	   /*			     */
  register SymTab st;			   	   /*			     */
  register long	  len  = 0l;			   /*			     */
  register long	  cnt  = 0l;			   /*			     */
  register long	  used = 0l;			   /*			     */
						   /*			     */
  for ( i = 0; i < HASHMAX; i++ )		   /*			     */
  { for ( st = sym_tab[i]; st; st=NextSymTab(st) ) /*			     */
    { ErrPrintF2("--- BibTool symbol %4d %s\n",	   /*			     */
		 SymTabCount(st),		   /*			     */
		 SymbolValue(SymTabSymbol(st)));   /*			     */
      l     = symlen(SymTabSymbol(st)) + 1;	   /*			     */
      len  += l;				   /*			     */
      used += l * SymTabCount(st);		   /*                        */
      ++cnt;					   /*			     */
    }						   /*			     */
  }						   /*			     */
  ErrPrintF2("--- BibTool symbol table: %ld bytes for %ld symbols\n",/*	     */
	     len, cnt);				   /*			     */
  ErrPrintF("--- BibTool symbol table emulates %ld bytes\n",/*		     */
	     used);				   /*			     */
}						   /*------------------------*/
#endif