1 /* ------------------------------------------------------------------------
2 @NAME       : btparse.h
3 @DESCRIPTION: Declarations and types for users of the btparse library.
4 
5               (Actually, btparse.h is generated from btparse.h.in by
6               the `configure' script, in order to automatically determine
7               the appropriate values of HAVE_USHORT and HAVE_BOOLEAN.)
8 @GLOBALS    :
9 @CALLS      :
10 @CREATED    : 1997/01/19, Greg Ward
11 @MODIFIED   :
12 @VERSION    : $Id: btparse.h.in,v 1.35 1999/12/28 18:23:17 greg Exp $
13 @COPYRIGHT  : Copyright (c) 1996-97 by Gregory P. Ward.  All rights reserved.
14 
15               This file is part of the btparse library.  This library is
16               free software; you can redistribute it and/or modify it under
17               the terms of the GNU General Public License as
18               published by the Free Software Foundation; either version 2
19               of the License, or (at your option) any later version.
20 -------------------------------------------------------------------------- */
21 #ifndef BTPARSE_H
22 #define BTPARSE_H
23 
24 #include <sys/types.h>                  /* probably supplies 'ushort' */
25 #include <stdio.h>
26 
27 #include "config.h" /* not btparse's config.h but Tellico's */
28 
29 /*
30  * Here we attempt to define HAVE_USHORT if a typdef for `ushort' appears
31  * in <sys/types.h>.  The detective work is actually done by the
32  * `configure' script, so if compilation fails because of duplicate
33  * definitions of `ushort', that's a bug in `configure' -- please tell me
34  * about it!
35  */
36 
37 #ifndef HAVE_USHORT
38 # define HAVE_USHORT 0
39 #endif
40 
41 #if ! HAVE_USHORT                       /* needed for various bitmaps */
42 typedef unsigned short ushort;
43 #endif
44 
45 
46 /* Likewise for boolean. */
47 
48 #ifndef HAVE_BOOLEAN
49 # define HAVE_BOOLEAN 0
50 #endif
51 
52 #if ! HAVE_BOOLEAN
53 typedef int boolean;
54 #endif
55 
56 #ifndef TRUE
57 # define TRUE 1
58 # define FALSE 0
59 #endif
60 
61 #ifndef HAVE_STRLWR
62 # define HAVE_STRLWR 0
63 #endif
64 
65 #ifndef HAVE_STRUPR
66 # define HAVE_STRUPR 0
67 #endif
68 
69 
70 /* Parsing (and post-processing) options */
71 
72 #define BTO_CONVERT   1                 /* convert numbers to strings? */
73 #define BTO_EXPAND    2                 /* expand macros? */
74 #define BTO_PASTE     4                 /* paste substrings together? */
75 #define BTO_COLLAPSE  8                 /* collapse whitespace? */
76 
77 #define BTO_NOSTORE   16
78 
79 #define BTO_FULL (BTO_CONVERT | BTO_EXPAND | BTO_PASTE | BTO_COLLAPSE)
80 #define BTO_MACRO (BTO_CONVERT | BTO_EXPAND | BTO_PASTE)
81 #define BTO_MINIMAL 0
82 
83 #define BTO_STRINGMASK (BTO_CONVERT | BTO_EXPAND | BTO_PASTE | BTO_COLLAPSE)
84 
85 #define BT_VALID_NAMEPARTS "fvlj"
86 #define BT_MAX_NAMEPARTS 4
87 
88 typedef enum
89 {
90    BTE_UNKNOWN,
91    BTE_REGULAR,
92    BTE_COMMENT,
93    BTE_PREAMBLE,
94    BTE_MACRODEF
95 /*
96    BTE_ALIAS,
97    BTE_MODIFY
98 */
99 } bt_metatype;
100 
101 #define NUM_METATYPES ((int) BTE_MACRODEF + 1)
102 
103 typedef enum
104 {
105    BTAST_BOGUS,                           /* to detect uninitialized nodes */
106    BTAST_ENTRY,
107    BTAST_KEY,
108    BTAST_FIELD,
109    BTAST_STRING,
110    BTAST_NUMBER,
111    BTAST_MACRO
112 } bt_nodetype;
113 
114 typedef enum
115 {
116    BTN_FIRST, BTN_VON, BTN_LAST, BTN_JR, BTN_NONE
117 } bt_namepart;
118 
119 typedef enum
120 {
121    BTJ_MAYTIE,                          /* "discretionary" tie between words */
122    BTJ_SPACE,                           /* force a space between words */
123    BTJ_FORCETIE,                        /* force a tie (~ in TeX) */
124    BTJ_NOTHING                          /* nothing between words */
125 } bt_joinmethod;
126 
127 
128 #define USER_DEFINED_AST 1
129 
130 #define zzcr_ast(ast,attr,tok,txt)              \
131 {                                               \
132    (ast)->filename = InputFilename;             \
133    (ast)->line = (attr)->line;                  \
134    (ast)->offset = (attr)->offset;              \
135    (ast)->text = strdup ((attr)->text);         \
136 }
137 
138 #define zzd_ast(ast)                            \
139 /* printf ("zzd_ast: free'ing ast node with string %p (%s)\n", \
140            (ast)->text, (ast)->text); */ \
141    if ((ast)->text != NULL) free ((ast)->text);
142 
143 
144 #ifdef USER_DEFINED_AST
145 typedef struct _ast
146 {
147    struct _ast *right, *down;
148    const char *     filename;
149    int              line;
150    int              offset;
151    bt_nodetype    nodetype;
152    bt_metatype    metatype;
153    char *           text;
154 } AST;
155 #endif /* USER_DEFINED_AST */
156 
157 
158 typedef struct
159 {
160    /*
161     * `string' is the string that has been split; items[0] ...
162     * items[num_items-1] are pointers into `string', or NULL for empty
163     * substrings.  Note that `string' is actually a copy of the string
164     * passed in to bt_split_list() with NULs inserted between substrings.
165     */
166 
167    char *  string;
168    int     num_items;
169    char ** items;
170 } bt_stringlist;
171 
172 
173 typedef struct
174 {
175    bt_stringlist * tokens;              /* flat list of all tokens in name */
176    char ** parts[BT_MAX_NAMEPARTS];     /* each elt. is list of pointers */
177                                         /* into `tokens->string' */
178    int     part_len[BT_MAX_NAMEPARTS];  /* length in tokens */
179 } bt_name;
180 
181 
182 typedef struct tex_tree_s
183 {
184    char * start;
185    int    len;
186    struct tex_tree_s
187         * child,
188         * next;
189 } bt_tex_tree;
190 
191 
192 typedef struct
193 {
194    /* These determine the order (and presence) of parts in the name. */
195    int         num_parts;
196    bt_namepart parts[BT_MAX_NAMEPARTS];
197 
198    /*
199     * These lists are always in the order of the bt_namepart enum -- *not*
200     * dependent on the particular order of parts the user specified!  (This
201     * will make it a bit harder if I ever allow more than one occurrence of
202     * a part in a format; since I don't allow that, I'm not [yet] worried
203     * about it!)
204     */
205    const char *       pre_part[BT_MAX_NAMEPARTS];
206    char *       post_part[BT_MAX_NAMEPARTS];
207    char *       pre_token[BT_MAX_NAMEPARTS];
208    const char *       post_token[BT_MAX_NAMEPARTS];
209    boolean      abbrev[BT_MAX_NAMEPARTS];
210    bt_joinmethod join_tokens[BT_MAX_NAMEPARTS];
211    bt_joinmethod join_part[BT_MAX_NAMEPARTS];
212 } bt_name_format;
213 
214 
215 typedef enum
216 {
217    BTERR_NOTIFY,                /* notification about next action */
218    BTERR_CONTENT,               /* warning about the content of a record */
219    BTERR_LEXWARN,               /* warning in lexical analysis */
220    BTERR_USAGEWARN,             /* warning about library usage */
221    BTERR_LEXERR,                /* error in lexical analysis */
222    BTERR_SYNTAX,                /* error in parser */
223    BTERR_USAGEERR,              /* fatal error in library usage */
224    BTERR_INTERNAL               /* my fault */
225 } bt_errclass;
226 
227 typedef enum
228 {
229    BTACT_NONE,                  /* do nothing on error */
230    BTACT_CRASH,                 /* call exit(1) */
231    BTACT_ABORT                  /* call abort() */
232 } bt_erraction;
233 
234 typedef struct
235 {
236    bt_errclass errclass;
237    const char *      filename;
238    int         line;
239    const char *      item_desc;
240    int         item;
241    const char *message;
242 } bt_error;
243 
244 typedef void (*bt_err_handler) (bt_error *);
245 
246 
247 #if defined(__cplusplus__) || defined(__cplusplus) || defined(c_plusplus)
248 extern "C" {
249 #endif
250 
251 /* Function prototypes */
252 
253 /* init.c */
254 void  bt_initialize (void);
255 void  bt_free_ast (AST *ast);
256 void  bt_cleanup (void);
257 
258 /* input.c */
259 void    bt_set_stringopts (bt_metatype metatype, ushort options);
260 AST * bt_parse_entry_s (char *    entry_text,
261                         const char *    filename,
262                         int       line,
263                         ushort    options,
264                         boolean * status);
265 AST * bt_parse_entry   (FILE *    infile,
266                         const char *    filename,
267                         ushort    options,
268                         boolean * status);
269 AST * bt_parse_file    (char *    filename,
270                         ushort    options,
271                         boolean * overall_status);
272 
273 /* postprocess.c */
274 void bt_postprocess_string (char * s, ushort options);
275 char * bt_postprocess_value (AST * value, ushort options, boolean replace);
276 char * bt_postprocess_field (AST * field, ushort options, boolean replace);
277 void bt_postprocess_entry (AST * entry, ushort options);
278 
279 /* error.c */
280 void   bt_reset_error_counts (void);
281 int    bt_get_error_count (bt_errclass errclass);
282 int *  bt_get_error_counts (int *counts);
283 ushort bt_error_status (int *saved_counts);
284 
285 /* macros.c */
286 void bt_add_macro_value (AST *assignment, ushort options);
287 void bt_add_macro_text (const char * macro, const char * text, const char * filename, int line);
288 void bt_delete_macro (const char * macro);
289 void bt_delete_all_macros (void);
290 int bt_macro_length (const char *macro);
291 char * bt_macro_text (const char * macro, const char * filename, int line);
292 
293 /* traversal.c */
294 AST *bt_next_entry (AST *entry_list, AST *prev_entry);
295 bt_metatype bt_entry_metatype (AST *entry);
296 char *bt_entry_type (AST *entry);
297 char *bt_entry_key (AST *entry);
298 AST *bt_next_field (AST *entry, AST *prev, char **name);
299 AST *bt_next_macro (AST *entry, AST *prev, char **name);
300 AST *bt_next_value (AST *head,
301                     AST *prev,
302                     bt_nodetype *nodetype,
303                     char **text);
304 char *bt_get_text (AST *node);
305 
306 /* modify.c */
307 void bt_set_text (AST * node, char * new_text);
308 void bt_entry_set_key (AST * entry, char * new_key);
309 
310 /* names.c */
311 bt_stringlist * bt_split_list (char *   string,
312                                char *   delim,
313                                char *   filename,
314                                int      line,
315                                const char *   description);
316 void bt_free_list (bt_stringlist *list);
317 bt_name * bt_split_name (const char *  name,
318                          const char *  filename,
319                          int     line,
320                          int     name_num);
321 void bt_free_name (bt_name * name);
322 
323 /* tex_tree.c */
324 bt_tex_tree * bt_build_tex_tree (char * string);
325 void          bt_free_tex_tree (bt_tex_tree **top);
326 void          bt_dump_tex_tree (bt_tex_tree *node, int depth, FILE *stream);
327 char *        bt_flatten_tex_tree (bt_tex_tree *top);
328 
329 /* string_util.c */
330 void bt_purify_string (char * string, ushort options);
331 void bt_change_case (char transform, char * string, ushort options);
332 
333 /* format_name.c */
334 bt_name_format * bt_create_name_format (char * parts, boolean abbrev_first);
335 void bt_free_name_format (bt_name_format * format);
336 void bt_set_format_text (bt_name_format * format,
337                          bt_namepart part,
338                          char * pre_part,
339                          char * post_part,
340                          char * pre_token,
341                          char * post_token);
342 void bt_set_format_options (bt_name_format * format,
343                             bt_namepart part,
344                             boolean abbrev,
345                             bt_joinmethod join_tokens,
346                             bt_joinmethod join_part);
347 char * bt_format_name (bt_name * name, bt_name_format * format);
348 
349 #if defined(__cplusplus__) || defined(__cplusplus) || defined(c_plusplus)
350 }
351 #endif
352 
353 #endif /* BTPARSE_H */
354