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