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