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