Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtdocbook / 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 /*
24  *  Copyright 1993 Open Software Foundation, Inc., Cambridge, Massachusetts.
25  *  All rights reserved.
26  */
27 /*
28  * Copyright (c) 1994  
29  * Open Software Foundation, Inc. 
30  *  
31  * Permission is hereby granted to use, copy, modify and freely distribute 
32  * the software in this file and its documentation for any purpose without 
33  * fee, provided that the above copyright notice appears in all copies and 
34  * that both the copyright notice and this permission notice appear in 
35  * supporting documentation.  Further, provided that the name of Open 
36  * Software Foundation, Inc. ("OSF") not be used in advertising or 
37  * publicity pertaining to distribution of the software without prior 
38  * written permission from OSF.  OSF makes no representations about the 
39  * suitability of this software for any purpose.  It is provided "as is" 
40  * without express or implied warranty. 
41  */
42 /* ________________________________________________________________________
43  *
44  *  Common definitions for "instant" program.
45  * ________________________________________________________________________
46  */
47
48 #ifdef STORAGE
49 #ifndef lint
50 static char *gen_h_RCSid =
51   "$XConsortium: general.h /main/11 1996/08/08 14:41:50 cde-hp $";
52 #endif
53 #endif
54
55 /* get Tcl header so the variables make sense */
56 #include <tcl.h>
57
58 /* instant variable delimiter (can't use '$', collides with Tcl) */
59 #define VDELIM          '@'
60
61 /* string/numeric/character definitions */
62
63 #define EOS             '\0'
64 #define NL              '\n'
65 #define TAB             '\t'
66 #define CR              '\r'
67 #define ANCHOR          '^'
68
69 /* bigmask/flags for the Split() function */
70 #define S_STRDUP        0x01
71 #define S_ALVEC         0x02
72
73 /*  Command codes (1st char of esis lines) from sgmls.  See its manpage. */
74 #define CMD_DATA        '-'
75 #define CMD_OPEN        '('
76 #define CMD_CLOSE       ')'
77 #define CMD_ATT         'A'
78 #define CMD_D_ATT       'D'
79 #define CMD_NOTATION    'N'
80 #define CMD_EXT_ENT     'E'
81 #define CMD_INT_ENT     'I'
82 #define CMD_SYSID       's'
83 #define CMD_PUBID       'p'
84 #define CMD_FILENAME    'f'
85 #define CMD_LINE        'L'
86 #define CMD_PI          '?'
87 #define CMD_SUBDOC      'S'
88 #define CMD_SUBDOC_S    '{'
89 #define CMD_SUBDOC_E    '}'
90 #define CMD_EXT_REF     '&'
91 #define CMD_APPINFO     '#'
92 #define CMD_CONFORM     'C'
93
94 /*  Some sizes */
95 #define MAX_DEPTH       40
96 #define LINESIZE        60000
97
98 /*  Name of library env variable, and default value. */
99 #ifndef TPT_LIB
100 #define TPT_LIB "TPT_LIB"
101 #endif
102 #ifndef DEF_TPT_LIB
103 #define DEF_TPT_LIB     "/project/tools/libdata/tpt"
104 #endif
105
106 /* for buffered output (buffer gets passed to interpreter) */
107 typedef struct _OutputBuffer {
108     char *base;     /* pointer to start of buffer */
109     char *current;  /* pointer to current location in buffer */
110     int   size;     /* total size of buffer */
111 } OutputBuffer_t;
112
113 #define OBUF_INCR 4096 /* enlarge buffer by this when full */
114 #ifndef MIN
115 #define MIN(i,j) (((i) < (j)) ? (i) : (j))
116 #endif
117
118 /*  Relationships - for querying */
119 typedef enum {
120     REL_None, REL_Parent, REL_Child, REL_Ancestor, REL_Descendant,
121     REL_Sibling, REL_Preceding, REL_ImmPreceding, REL_Following,
122     REL_ImmFollowing, REL_Cousin, REL_Is1stContent, REL_HasOnlyContent,
123     REL_Unknown
124 } Relation_t;
125
126 /* Initial map sizes (IMS) */
127 #define IMS_relations           3
128 #define IMS_setvar              3
129 #define IMS_incvar              3
130 #define IMS_sdata               50
131 #define IMS_sdatacache          30
132 #define IMS_variables           20
133 #define IMS_attnames            50
134 #define IMS_elemnames           50
135
136 /* ----- structure definitions ----- */
137
138 /*  We use this for variables, attributes, etc., so the caller only needs an
139  *  opaque handle to the thing below, not worrying about array management.  */
140 typedef struct {
141     char        *name;                  /* name of the thing */
142     char        *sval;                  /* string value */
143 } Mapping_t;
144
145 typedef struct {
146     int         n_alloc;                /* number of elements allocated */
147     int         n_used;                 /* number of elements used */
148     int         slot_incr;              /* increment for allocating slots */
149     int         flags;                  /* info about this set of mappings */
150     Mapping_t   *maps;                  /* array of mappings */
151 } Map_t;
152
153 /* ______________________________________________________________________ */
154
155 /*  Information about an entity reference.  Not all fields will be used
156  *  at once.  */
157 typedef struct _ent {
158     char        *type;                  /* entity type */
159     char        *ename;                 /* entity name */
160     char        *nname;                 /* notation name */
161     char        *sysid;                 /* sys id */
162     char        *pubid;                 /* pub id */
163     char        *fname;                 /* filename */
164     struct _ent *next;                  /* next in linked list */
165 } Entity_t;
166
167 /*  Content (child nodes) of an element (node in the tree) -- both data
168  *  and other elements.  */
169 typedef struct {
170     char                type;           /* element, data, or pi? */
171     union {
172         struct _elem    *elem;          /* direct children of this elem */
173         char            *data;          /* character data of this elem */
174     } ch;
175 } Content_t;
176
177 /*  An element (node in the tree) */
178 typedef struct _elem {
179     char        *gi;                    /* element GI */
180     Content_t   *cont;                  /* content - element & data children */
181     int         ncont;                  /* # of content/children */
182     struct _elem **econt;               /* element children */
183     int         necont;                 /* # of element children */
184     char        **dcont;                /* character data children */
185     int         ndcont;                 /* # of data children */
186     Mapping_t   *atts;                  /* array of attributes */
187     int         natts;                  /* # of attributes */
188     Entity_t    *entity;                /* ext entity & notation info */
189     char        *id;                    /* for linking */
190     int         index;                  /* an internal bookkeeping mechanism */
191     int         depth;                  /* how deep in tree */
192     int         lineno;                 /* line number */
193     char        *infile;                /* input filename */
194     int         my_eorder;              /* order of this elem of its parent */
195     struct _elem *parent;               /* this elem's direct parent */
196     struct _elem *next;                 /* kept in linked list */
197     void        *trans;                 /* pointer to translation spec */
198     /* I'm not crazy about this, but it works */
199     int         gen_trans[2];           /* refs to generated trans specs */
200     int         processed;              /* was this node processed? */
201 } Element_t;
202
203 /*  For mapping of element IDs to elements themselves.  */
204 typedef struct id_s {
205     char        *id;                    /* ID of the element */
206     Element_t   *elem;                  /* pointer to it */
207     struct id_s *next;
208 } ID_t;
209
210 /* ----- global variable declarations ----- */
211
212 #ifdef STORAGE
213 # define def
214 #else
215 # define def    extern
216 #endif
217
218 def Element_t   *DocTree;               /* root of document tree */
219 def char        **UsedElem;             /* a unique list of used elem names */
220 def int         nUsedElem;              /* number of used elem names */
221 def char        **UsedAtt;              /* a unique list of used attrib names */
222 def int         nUsedAtt;               /* number of used attrib names */
223 def ID_t        *IDList;                /* list of IDs used in the doc */
224 def Map_t       *Variables;             /* general, global variables */
225 def Map_t       *SDATAmap;              /* SDATA mappings */
226 def Map_t       *PImap;                 /* Processing Instruction mappings */
227 def Entity_t    *Entities;              /* list of entities */
228
229 def FILE        *outfp;                 /* where output is written */
230 def char        *tpt_lib;               /* TPT library directory */
231 def int         verbose;                /* flag - verbose output? */
232 def int         warnings;               /* flag - show warnings? */
233 def int         interactive;            /* flag - interactive browsing? */
234 def int         slave;                  /* are we slave to another process? */
235 def int         fold_case;              /* flag - fold case of GIs? */
236 def Tcl_Interp  *interpreter;           /* interp for start_ and end_code */
237 def Element_t   *tclE;                  /* active element in Tcl call */
238
239
240 /* ----- some macros for convenience and ease of code reading ----- */
241
242 /* #define stripNL(s)   { char *_cp; if ((_cp=strchr(s, NL))) *_cp = EOS; } */
243 #define stripNL(s)      { char *_cp = s; while (*_cp) _cp++; \
244                           if ((_cp > s) && (*--_cp == NL)) *_cp = EOS; }
245
246 #define I18N_TRIGGER 0xFF
247
248 /*  Similar to calloc(), malloc(), and realloc(), but check for success.
249  *  Args to all:
250  *      (1) number of 'elements' to allocate
251  *      (2) variable to point at allocated space
252  *      (3) type of 'element'
253  *  Eg: Calloc(5, e, Element_t) replaces
254  *      if (!(e = (Element_t *)calloc(5, sizeof(Element_t))) {
255  *              ... handle error ... ;
256  *      }
257  */
258 #define Calloc(N,V,T)   \
259     { if (!((V) = (T *)calloc((size_t)N, sizeof(T)))) { \
260         perror("Calloc failed -- out of memory.  Bailing out.");  exit(1); \
261     } }
262 #define Malloc(N,V,T)   \
263     { if (!((V) = (T *)malloc((size_t)N*sizeof(T)))) { \
264         perror("Malloc failed -- out of memory.  Bailing out.");  exit(1); \
265     } }
266 #define Realloc(N,V,T)  \
267     { if (!((V) = (T *)realloc(V,(size_t)N*sizeof(T)))) { \
268         perror("Realloc failed -- out of memory.  Bailing out.");  exit(1); \
269     } }
270
271 /*  similar to strcmp(), but check first chars first, for efficiency */
272 #define StrEq(s1,s2)    (s1[0] == s2[0] && !strcmp(s1,s2))
273
274 /*  similar to isspace(), but check for blank or tab - without overhead
275  *  of procedure call */
276 #define IsWhite(c)      (c == ' ' || c == TAB)
277
278 #define ContType(e,i)   (e->cont[i].type)
279 #define ContData(e,i)   (e->cont[i].ch.data)
280 #define ContElem(e,i)   (e->cont[i].ch.elem)
281 #define IsContData(e,i) (e->cont[i].type == CMD_DATA)
282 #define IsContElem(e,i) (e->cont[i].type == CMD_OPEN)
283 #define IsContPI(e,i)   (e->cont[i].type == CMD_PI)
284
285 /* ----- function prototypes ----- */
286
287 /* things defined in util.c */
288 Element_t       *QRelation(Element_t *, char *, Relation_t);
289 Relation_t      FindRelByName(char *);
290 char            *FindAttValByName(Element_t *, char *);
291 char            *FindContext(Element_t *, int, char *);
292 char            *AddElemName(char *);
293 char            *AddAttName(char *);
294 void             OutputString(char *, FILE *, int);
295 int             Putc(int, FILE *);
296 int             FPuts(const char *, FILE *);
297 int             FFlush(FILE *);
298 FILE            *OpenFile(char *);
299 char            *FindElementPath(Element_t *, char *);
300 char            *NearestOlderElem(Element_t *, char *);
301 void            PrintLocation(Element_t *, FILE *);
302 char            **Split(char *, int *, int);
303 void            DescendTree(Element_t *, void(*)(), void(*)(), void(*)(), void *);
304 Map_t           *NewMap(int);
305 Mapping_t       *FindMapping(Map_t *, char *);
306 char            *FindMappingVal(Map_t *, char *);
307 void            SetMapping(Map_t *, char *);
308 void            SetMappingNV(Map_t *, char *, char *);
309 void            AddID(Element_t *, char *);
310 Element_t       *FindElemByID(char *);
311
312 /* things defined in translate.c */
313 void            DoTranslate(Element_t*, char *, FILE *);
314
315 /* things defined in traninit.c */
316 void            ReadTransSpec(char *);
317 void            ReadSDATA(char *);
318 void            ReadCharMap(char *);
319
320 /* things defined in info.c */
321 void            PrintContext(Element_t *e);
322 void            PrintElemSummary(Element_t *);
323 void            PrintElemTree(Element_t *);
324 void            PrintStats(Element_t *);
325 void            PrintIDList();
326
327 /* ----- other declarations ----- */
328
329 #ifdef ultrix
330 #define strdup(s)       strcpy((char *)malloc(strlen(s)+1), s)
331 #endif
332