forked from ge-ne/bibtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.c
475 lines (445 loc) · 22.1 KB
/
macros.c
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
/*** macros.c ******************************************************************
**
** This file is part of BibTool.
** It is distributed under the GNU General Public License.
** See the file COPYING for details.
**
** (c) 1996-2017 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.
**
******************************************************************************/
#include <bibtool/general.h>
#include <bibtool/symbols.h>
#include <bibtool/error.h>
#include <bibtool/macros.h>
#include <bibtool/sbuffer.h>
#include "config.h"
/*****************************************************************************/
/* Internal Programs */
/*===========================================================================*/
/*****************************************************************************/
/* External Programs */
/*===========================================================================*/
/*---------------------------------------------------------------------------*/
static Macro macros = MacroNULL; /* */
/*-----------------------------------------------------------------------------
** Function: new_macro()
** Purpose: Allocate a new macro structure and fill it with initial values.
** Upon failure an error is raised and |exit()| is called.
** Arguments:
** name Name of the macro. This must be a symbol.
** value The value of the macro. This must be a symbol.
** next The next pointer of the |Macro| structure.
** count The initial reference count.
** Returns: The new |Macro|.
**___________________________________________________ */
Macro new_macro(name, value, next, count) /* */
Symbol name; /* */
Symbol value; /* */
int count; /* */
Macro next; /* */
{ register Macro macro; /* */
/* */
if ( (macro=(Macro)malloc(sizeof(SMacro))) == MacroNULL )/* */
{ OUT_OF_MEMORY("Macro"); } /* */
/* */
MacroName(macro) = name; /* */
MacroValue(macro) = value ; /* */
MacroCount(macro) = count; /* */
NextMacro(macro) = next; /* */
return(macro); /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: free_macro()
** Purpose: Free a list of macros. The memory allocated for the
** |Macro| given as argument and all structures
** reachable via the |NextMacro| pointer are released.
** Arguments:
** mac First Macro to release.
** Returns: nothing
**___________________________________________________ */
void free_macro(mac) /* */
Macro mac; /* */
{ Macro next; /* */
/* */
while (mac) /* */
{ next = NextMacro(mac); /* */
UnlinkSymbol(MacroName(mac)); /* */
UnlinkSymbol(MacroValue(mac)); /* */
free(mac); /* */
mac = next; /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: def_macro()
** Purpose: Define or undefine a macro.
** Arguments:
** name name of the macro.
** val NULL or the value of the new macro
** count initial count for the macro.
** Returns: nothing
**___________________________________________________ */
int def_macro(name, val, count) /* */
Symbol name; /* */
Symbol val; /* */
int count; /* */
{ register Macro *mp; /* */
/* */
for (mp = ¯os; /* */
*mp != MacroNULL; /* */
mp = &NextMacro(*mp)) /* */
{ if (MacroName(*mp) == name) /* */
{ if (val) /* */
{ MacroValue(*mp) = val; } /* */
else /* */
{ Macro mac = *mp; /* */
*mp = NextMacro(*mp); /* */
free(mac); /* */
} /* */
return 1; /* */
} /* */
} /* */
/* */
if (val) /* */
{ macros = new_macro(name, val, macros, count); }/* */
return 0; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: look_macro()
** Purpose: Return the value of a macro if it is defined. This
** value is a symbol.
** If the macro is undefined then |NULL| is returned. In
** this case the value of |add| determines whether or not
** the macro should be defined. If it is less than 0
** then no new macros is defined. Otherwise a new macro
** is defined. The value is the empty string and the
** initial reference count is |add|.
** Arguments:
** name The name of the macros to find. This needs not to be a symbol.
** add Initial reference count or indicator that no new macro
** is required.
** Returns: The value or |NULL|.
**___________________________________________________ */
Symbol look_macro(name, add) /* */
Symbol name; /* */
int add; /* */
{ register Macro *mp; /* */
/* */
for (mp = ¯os; /* */
*mp != MacroNULL; /* */
mp = &NextMacro(*mp)) /* */
{ if (MacroName(*mp) == name) /* */
{ if (MacroCount(*mp) >= 0) /* */
MacroCount(*mp) += add; /* */
return(MacroValue(*mp)); /* */
} /* */
} /* */
if (add >= 0) /* */
{ def_macro(name, sym_empty, add); /* */
WARNING3("Macro '",name,"' is undefined."); /* */
} /* */
return NULL; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: foreach_macro()
** Purpose: Apply a function to each macro in turn. The function
** is called with the name and the value of the macro. If
** it returns |false| then the processing of further
** macros is suppressed.
**
** The function given as argument is called with two
** string arguments. The first is the name of the macro
** and the second is its value. Both are symbols and must
** not be modified in any way.
**
** The order of the enumeration of the macros is
** determined by the implementation. No specific
** assumptions should be made about this order.
** Arguments:
** fct Function to apply to each macro.
** Returns: nothing
**___________________________________________________ */
void foreach_macro(fct) /* */
bool (*fct) _ARG((Symbol ,Symbol )); /* */
{ Macro mac; /* */
for (mac = macros; /* */
mac != MacroNULL; /* */
mac = NextMacro(mac)) /* */
{ if (MacroValue(mac) && /* */
! (*fct)(MacroName(mac), MacroValue(mac))) /* */
return; /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: each_macro()
** Type: int
** Purpose: Iterate over a linked list of macros. A function is applied
** to each macro found. The loop terminates if the
** function returns |false|.
** Arguments:
** m the macro to start with
** fct the function to apply
** Returns: |true| if the function has terminated the loop and
** |false| in case the end of the list has been reached
**___________________________________________________ */
bool each_macro(m, fct) /* */
Macro m; /* */
bool (*fct) _ARG((Symbol ,Symbol)); /* */
{ /* */
for ( ; m != MacroNULL; m = NextMacro(m)) /* */
{ if (! (*fct)(MacroName(m), MacroValue(m))) /* */
return true; /* */
} /* */
return false; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: dump_mac()
** Purpose: Write macros to a file.
** Arguments:
** fname File name of the target file.
** allp if == 0 only the used macros are written.
** Returns: nothing
**___________________________________________________ */
void dump_mac(fname,allp) /* */
char *fname; /* */
int allp; /* */
{ FILE *file; /* */
register Macro mac; /* */
/* */
if ( *fname == '-' && *(fname+1) == '\0' ) /* */
{ file = stdout; } /* */
else if ( (file=fopen(fname,"w")) == NULL ) /* */
{ ERROR2("File could not be opened: ", fname); /* */
return; /* */
} /* */
/* */
for (mac = macros; /* */
mac != MacroNULL; /* */
mac = NextMacro(mac)) /* */
{ if (MacroCount(mac) > 0 || /* */
(MacroCount(mac) >= 0 && allp)) /* */
{ if (MacroName(mac) == MacroValue(mac)) /* */
{ (void)fprintf(file, /* */
"_string{ %s = %s } used: %d\n",/* */
SymbolValue(MacroName(mac)), /* */
SymbolValue(MacroValue(mac)),/* */
MacroCount(mac)); /* */
} /* */
else /* */
{ (void)fprintf(file, /* */
"@STRING{ %s = %s } used: %d\n",/* */
SymbolValue(MacroName(mac)), /* */
SymbolValue(MacroValue(mac)),/* */
MacroCount(mac)); /* */
} /* */
} /* */
} /* */
/* */
if ( file != stdout ) (void)fclose(file); /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: init_macros()
** Purpose: Initialize some macros from a table defined in the
** configuration file or given as define to the C
** compiler. This function has to be called to initialize
** the global macros.
**
** Note that this function is for internal purposes
** only. The normal user should call |init_bibtool()|
** instead.
** Arguments: none
** Returns: nothing
**___________________________________________________ */
void init_macros() /* */
{ /* */
#ifdef INITIALIZE_MACROS
register Symbol name; /* */
register char**wp; /* */
static char *word_list[] = /* */
{ INITIALIZE_MACROS, NULL }; /* */
/* */
for (wp = word_list; *wp != NULL; ++wp) /* */
{ name = symbol((String)*wp); /* */
def_macro(name, name, -1); /* */
} /* */
#endif
} /*------------------------*/
/*****************************************************************************/
/*** ***/
/*****************************************************************************/
static Macro items = MacroNULL;
/*-----------------------------------------------------------------------------
** Function*: def_item()
** Purpose: Define a macro. The arguments have to be symbols.
** Arguments:
** name the name of the item
** value the value of the item
** Returns: nothing
**___________________________________________________ */
static void def_item(name, value) /* */
register Symbol name; /* */
register Symbol value; /* */
{ /* */
items = new_macro(name, value, items, 0); /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: def_field_type()
** Purpose: This function adds a printing representation for a
** field name to the used list. The argument is an
** equation of the following form
**
** \textit{type = value}
**
** \textit{type} is translated to lower case and compared
** against the internal representation. \textit{value} is
** printed at the appropriate places instead.
** Arguments:
** s String containing an equation. This string is modified
** during the process.
** Returns: nothing
**___________________________________________________ */
void def_field_type(s) /* */
String s; /* */
{ Symbol name; /* */
/* */
while (*s && !is_allowed(*s)) ++s; /* */
if (*s == '\0') return; /* */
/* */
name = sym_extract(&s, true); /* */
/* */
while (*s && !is_allowed(*s)) ++s; /* */
if (*s == '\0') return; /* */
/* */
def_item(name, sym_extract(&s, false)); /* */
} /*------------------------*/
/*****************************************************************************/
/*** ***/
/*****************************************************************************/
/*-----------------------------------------------------------------------------
** Function*: get_mapped_or_cased()
** Purpose: Query a mapping in a macro list or return the
** translated name.
** Arguments:
** name the lower-case name of the macro to get
** mac the list of macros to query
** type the type of the fall-back from |SYMBOL_TYPE_LOWER|,
** |SYMBOL_TYPE_UPPER|, or |SYMBOL_TYPE_CASED|.
** Returns: the requested value
**___________________________________________________ */
static Symbol get_mapped_or_cased(name, mac, type) /* */
Symbol name; /* */
int type; /* */
register Macro mac; /* */
{ static StringBuffer* sb = (StringBuffer*)NULL; /* */
register String s; /* */
/* */
for ( ; mac != MacroNULL; mac = NextMacro(mac) ) /* */
{ if (name == MacroName(mac)) /* */
{ /* */
LinkSymbol(MacroValue(mac)); /* */
return MacroValue(mac); /* */
} /* */
} /* */
/* */
if (sb == NULL) /* */
{ if ((sb=sbopen()) == NULL) /* */
{ OUT_OF_MEMORY("get_item()"); } /* */
} else /* */
{ sbrewind(sb); } /* */
/* */
switch (type) /* */
{ case SYMBOL_TYPE_LOWER: /* */
return name; /* */
/* */
case SYMBOL_TYPE_CASED: /* */
for (s = SymbolValue(name); *s; ) /* */
{ if (is_alpha(*s)) /* */
{ (void)sbputc(ToUpper(*s), sb); /* */
for (++s; is_alpha(*s); ++s) /* */
{ (void)sbputc(*s, sb); } /* */
} /* */
else /* */
{ (void)sbputc(*s, sb); /* */
++s; /* */
} /* */
} /* */
break; /* */
/* */
case SYMBOL_TYPE_UPPER: /* */
for (s = SymbolValue(name); *s; ++s) /* */
{ (void)sbputc(ToUpper(*s), sb); } /* */
break; /* */
} /* */
return symbol((String)sbflush(sb)); /* */
} /*------------------------*/
/*****************************************************************************/
/*** ***/
/*****************************************************************************/
/*-----------------------------------------------------------------------------
** Function: get_item()
** Purpose: Return the print representation of a \BibTeX{} string.
** The appearance is determined by the |items| mapping.
** If no appropriate entry is found then |type| is used to
** decide whether the item should be returned as
** upper-case, lower-case or first upper only.
** Arguments:
** name Symbol to get the print representation for.
** type One of the values |SYMBOL_TYPE_UPPER|, |SYMBOL_TYPE_LOWER|, or
** |SYMBOL_TYPE_CASED| as they are defined in |type.h|.
** Returns: A pointer to a static string. This location is reused
** upon the next invocation of this function.
**___________________________________________________ */
Symbol get_item(name, type) /* */
Symbol name; /* */
int type; /* */
{ return get_mapped_or_cased(name, items, type); /* */
} /*------------------------*/
/*****************************************************************************/
/*** ***/
/*****************************************************************************/
static Macro keys = MacroNULL; /* */
/*-----------------------------------------------------------------------------
** Function: save_key()
** Purpose: Save a mapping of a lower-case key to a printed
** representation.
** Arguments:
** name the name of the key in lower
** key the key as printed
** Returns: nothing
**___________________________________________________ */
void save_key(name, key) /* */
Symbol name; /* */
Symbol key; /* */
{ keys = new_macro(name, key, keys, 1); /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: get_key()
** Purpose: Get the printable representation of a key. If a special
** representation has been registered then this
** representation is returned. Otherwise the (lower-case)
** key is returned.
** Arguments:
** name the name of the key to find. This must be in lower-case
** Returns: the requested representation
**___________________________________________________ */
Symbol get_key(name) /* */
Symbol name; /* */
{ return get_mapped_or_cased(name, /* */
keys, /* */
SYMBOL_TYPE_LOWER); /* */
} /*------------------------*/