Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / doc / util / dbtoman / instant / general.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: general.h /main/1 1996/07/23 19:42:38 rws $ */
24 /*
25  *  Copyright 1993 Open Software Foundation, Inc., Cambridge, Massachusetts.
26  *  All rights reserved.
27  */
28 /*
29  * Copyright (c) 1994  
30  * Open Software Foundation, Inc. 
31  *  
32  * Permission is hereby granted to use, copy, modify and freely distribute 
33  * the software in this file and its documentation for any purpose without 
34  * fee, provided that the above copyright notice appears in all copies and 
35  * that both the copyright notice and this permission notice appear in 
36  * supporting documentation.  Further, provided that the name of Open 
37  * Software Foundation, Inc. ("OSF") not be used in advertising or 
38  * publicity pertaining to distribution of the software without prior 
39  * written permission from OSF.  OSF makes no representations about the 
40  * suitability of this software for any purpose.  It is provided "as is" 
41  * without express or implied warranty. 
42  */
43 /*
44  * Copyright (c) 1996 X Consortium
45  * Copyright (c) 1995, 1996 Dalrymple Consulting
46  * 
47  * Permission is hereby granted, free of charge, to any person obtaining a copy
48  * of this software and associated documentation files (the "Software"), to deal
49  * in the Software without restriction, including without limitation the rights
50  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
51  * copies of the Software, and to permit persons to whom the Software is
52  * furnished to do so, subject to the following conditions:
53  * 
54  * The above copyright notice and this permission notice shall be included in
55  * all copies or substantial portions of the Software.
56  * 
57  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
60  * X CONSORTIUM OR DALRYMPLE CONSULTING BE LIABLE FOR ANY CLAIM, DAMAGES OR
61  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
62  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
63  * OTHER DEALINGS IN THE SOFTWARE.
64  * 
65  * Except as contained in this notice, the names of the X Consortium and
66  * Dalrymple Consulting shall not be used in advertising or otherwise to
67  * promote the sale, use or other dealings in this Software without prior
68  * written authorization.
69  */
70 /* ________________________________________________________________________
71  *
72  *  Common definitions for "instant" program.
73  * ________________________________________________________________________
74  */
75
76 /* string/numeric/character definitions */
77
78 #define EOS             '\0'
79 #define NL              '\n'
80 #define TAB             '\t'
81 #define CR              '\r'
82 #define ANCHOR          '^'
83
84 /* bigmask/flags for the Split() function */
85 #define S_STRDUP        0x01
86 #define S_ALVEC         0x02
87
88 /*  Command codes (1st char of esis lines) from sgmls.  See its manpage. */
89 #define CMD_DATA        '-'
90 #define CMD_OPEN        '('
91 #define CMD_CLOSE       ')'
92 #define CMD_ATT         'A'
93 #define CMD_D_ATT       'D'
94 #define CMD_NOTATION    'N'
95 #define CMD_EXT_ENT     'E'
96 #define CMD_INT_ENT     'I'
97 #define CMD_SYSID       's'
98 #define CMD_PUBID       'p'
99 #define CMD_FILENAME    'f'
100 #define CMD_LINE        'L'
101 #define CMD_PI          '?'
102 #define CMD_SUBDOC      'S'
103 #define CMD_SUBDOC_S    '{'
104 #define CMD_SUBDOC_E    '}'
105 #define CMD_EXT_REF     '&'
106 #define CMD_APPINFO     '#'
107 #define CMD_CONFORM     'C'
108
109 /*  Some sizes */
110 #define MAX_DEPTH       40
111 #define LINESIZE        60000
112
113 /*  Name of library env variable, and default value. */
114 #ifndef TPT_LIB
115 #define TPT_LIB "TPT_LIB"
116 #endif
117 #ifndef DEF_TPT_LIB
118 #define DEF_TPT_LIB     "/project/tools/libdata/tpt"
119 #endif
120
121 /*  Relationships - for querying */
122 typedef enum {
123     REL_None, REL_Parent, REL_Child, REL_Ancestor, REL_Descendant,
124     REL_Sibling, REL_Preceding, REL_ImmPreceding, REL_Following,
125     REL_ImmFollowing, REL_Cousin, REL_Unknown
126 } Relation_t;
127
128 /* Initial map sizes (IMS) */
129 #define IMS_relations           3
130 #define IMS_setvar              3
131 #define IMS_incvar              3
132 #define IMS_sdata               50
133 #define IMS_sdatacache          30
134 #define IMS_variables           20
135 #define IMS_attnames            50
136 #define IMS_elemnames           50
137
138 /* ----- typedef and other misc definitions ----- */
139
140 #ifndef TRUE
141 #define TRUE (1 == 1)
142 #endif
143
144 #ifndef FALSE
145 #define FALSE (1 == 0)
146 #endif
147
148 typedef short bool;
149
150
151 /* ----- structure definitions ----- */
152
153 /*  We use this for variables, attributes, etc., so the caller only needs an
154  *  opaque handle to the thing below, not worrying about array management.  */
155 typedef struct {
156     char        *name;                  /* name of the thing */
157     char        *sval;                  /* string value */
158 } Mapping_t;
159
160 typedef struct {
161     int         n_alloc;                /* number of elements allocated */
162     int         n_used;                 /* number of elements used */
163     int         slot_incr;              /* increment for allocating slots */
164     int         flags;                  /* info about this set of mappings */
165     Mapping_t   *maps;                  /* array of mappings */
166 } Map_t;
167
168 /* ______________________________________________________________________ */
169
170 /*  Information about an entity reference.  Not all fields will be used
171  *  at once.  */
172 typedef struct _ent {
173     char        *type;                  /* entity type */
174     char        *ename;                 /* entity name */
175     char        *nname;                 /* notation name */
176     char        *sysid;                 /* sys id */
177     char        *pubid;                 /* pub id */
178     char        *fname;                 /* filename */
179     struct _ent *next;                  /* next in linked list */
180 } Entity_t;
181
182 /*  Content (child nodes) of an element (node in the tree) -- both data
183  *  and other elements.  */
184 typedef struct {
185     char                type;           /* element, data, or pi? */
186     union {
187         struct _elem    *elem;          /* direct children of this elem */
188         char            *data;          /* character data of this elem */
189     } ch;
190 } Content_t;
191
192 /*  An element (node in the tree) */
193 typedef struct _elem {
194     char        *gi;                    /* element GI */
195     Content_t   *cont;                  /* content - element & data children */
196     int         ncont;                  /* # of content/children */
197     struct _elem **econt;               /* element children */
198     int         necont;                 /* # of element children */
199     char        **dcont;                /* character data children */
200     int         ndcont;                 /* # of data children */
201     Mapping_t   *atts;                  /* array of attributes */
202     int         natts;                  /* # of attributes */
203     Entity_t    *entity;                /* ext entity & notation info */
204     char        *id;                    /* for linking */
205     int         index;                  /* an internal bookkeeping mechanism */
206     int         depth;                  /* how deep in tree */
207     int         lineno;                 /* line number */
208     char        *infile;                /* input filename */
209     int         my_eorder;              /* order of this elem of its parent */
210     struct _elem *parent;               /* this elem's direct parent */
211     struct _elem *next;                 /* kept in linked list */
212     void        *trans;                 /* pointer to translation spec */
213     /* I'm not crazy about this, but it works */
214     int         gen_trans[2];           /* refs to generated trans specs */
215     int         processed;              /* was this node processed? */
216 } Element_t;
217
218 /*  For mapping of element IDs to elements themselves.  */
219 typedef struct id_s {
220     char        *id;                    /* ID of the element */
221     Element_t   *elem;                  /* pointer to it */
222     struct id_s *next;
223 } ID_t;
224
225 /* ----- global variable declarations ----- */
226
227 #ifdef STORAGE
228 # define def
229 #else
230 # define def    extern
231 #endif
232
233 def Element_t   *DocTree;               /* root of document tree */
234 def char        **UsedElem;             /* a unique list of used elem names */
235 def int         nUsedElem;              /* number of used elem names */
236 def char        **UsedAtt;              /* a unique list of used attrib names */
237 def int         nUsedAtt;               /* number of used attrib names */
238 def ID_t        *IDList;                /* list of IDs used in the doc */
239 def Map_t       *Variables;             /* general, global variables */
240 def Map_t       *SDATAmap;              /* SDATA mappings */
241 def Map_t       *PImap;                 /* Processing Instruction mappings */
242 def Entity_t    *Entities;              /* list of entities */
243
244 def FILE        *outfp;                 /* where output is written */
245 def char        *tpt_lib;               /* TPT library directory */
246 def int         verbose;                /* flag - verbose output? */
247 def int         warnings;               /* flag - show warnings? */
248 def int         interactive;            /* flag - interactive browsing? */
249 def int         slave;                  /* are we slave to another process? */
250 def int         fold_case;              /* flag - fold case of GIs? */
251
252 /* ----- some macros for convenience and ease of code reading ----- */
253
254 #define stripNL(s)      { char *_cp; if ((_cp=strchr(s, NL))) *_cp = EOS; }
255
256 /*  Similar to calloc(), malloc(), and realloc(), but check for success.
257  *  Args to all:
258  *      (1) number of 'elements' to allocate
259  *      (2) variable to point at allocated space
260  *      (3) type of 'element'
261  *  Eg: Calloc(5, e, Element_t) replaces
262  *      if (!(e = (Element_t *)calloc(5, sizeof(Element_t))) {
263  *              ... handle error ... ;
264  *      }
265  */
266 #define Calloc(N,V,T)   \
267     { if (!((V) = (T *)calloc((size_t)N, sizeof(T)))) { \
268         perror("Calloc failed -- out of memory.  Bailing out.");  exit(1); \
269     }; memset((void *) (V), 0, (size_t) sizeof(T)); }
270 #define Malloc(N,V,T)   \
271     { if (!((V) = (T *)malloc((size_t)N*sizeof(T)))) { \
272         perror("Malloc failed -- out of memory.  Bailing out.");  exit(1); \
273     }; memset((void *) (V), 0, (size_t) sizeof(T)); }
274 #define Realloc(N,V,T)  \
275     { if (!((V) = (T *)realloc(V,(size_t)N*sizeof(T)))) { \
276         perror("Realloc failed -- out of memory.  Bailing out.");  exit(1); \
277     } }
278
279 /*  similar to strcmp(), but check first chars first, for efficiency */
280 #define StrEq(s1,s2)    (s1[0] == s2[0] && !strcmp(s1,s2))
281
282 /*  similar to isspace(), but check for blank or tab - without overhead
283  *  of procedure call */
284 #define IsWhite(c)      (c == ' ' || c == TAB)
285
286 #define ContType(e,i)   (e->cont[i].type)
287 #define ContData(e,i)   (e->cont[i].ch.data)
288 #define ContElem(e,i)   (e->cont[i].ch.elem)
289 #define IsContData(e,i) (e->cont[i].type == CMD_DATA)
290 #define IsContElem(e,i) (e->cont[i].type == CMD_OPEN)
291 #define IsContPI(e,i)   (e->cont[i].type == CMD_PI)
292
293 /* ----- function prototypes ----- */
294
295 /* things defined in util.c */
296 Element_t       *QRelation(Element_t *, char *, Relation_t);
297 Relation_t      FindRelByName(char *);
298 char            *FindAttValByName(Element_t *, char *);
299 char            *FindContext(Element_t *, int, char *);
300 char            *AddElemName(char *);
301 char            *AddAttName(char *);
302 void             OutputString(char *, FILE *, int);
303 FILE            *OpenFile(char *);
304 char            *FindElementPath(Element_t *, char *);
305 char            *NearestOlderElem(Element_t *, char *);
306 void            PrintLocation(Element_t *, FILE *);
307 char            **Split(char *, int *, int);
308 void            DescendTree(Element_t *, void(*)(), void(*)(), void(*)(), void *);
309 Map_t           *NewMap(int);
310 Mapping_t       *FindMapping(Map_t *, char *);
311 char            *FindMappingVal(Map_t *, char *);
312 void            SetMapping(Map_t *, char *);
313 void            SetMappingNV(Map_t *, char *, char *);
314 void            AddID(Element_t *, char *);
315 Element_t       *FindElemByID(char *);
316
317 /* things defined in translate.c */
318 void            DoTranslate(Element_t*, char *, FILE *);
319
320 /* things defined in traninit.c */
321 void            ReadTransSpec(char *);
322 void            ReadSDATA(char *);
323 void            ReadCharMap(char *);
324
325 /* things defined in tranvar.c */
326 char            *Get_A_C_value(char *);
327
328 /* things defined in info.c */
329 void            PrintContext(Element_t *e);
330 void            PrintElemSummary(Element_t *);
331 void            PrintElemTree(Element_t *);
332 void            PrintStats(Element_t *);
333 void            PrintIDList();
334
335 /* ----- other declarations ----- */
336
337 #ifdef ultrix
338 #define strdup(s)       strcpy((char *)malloc(strlen(s)+1), s)
339 #endif
340