1 /* 2 * Copyright 1993 Open Software Foundation, Inc., Cambridge, Massachusetts. 3 * All rights reserved. 4 */ 5 /* 6 #pragma ident "%Z%%M% %I% %E% SMI" 7 * Copyright (c) 1994 8 * Open Software Foundation, Inc. 9 * 10 * Permission is hereby granted to use, copy, modify and freely distribute 11 * the software in this file and its documentation for any purpose without 12 * fee, provided that the above copyright notice appears in all copies and 13 * that both the copyright notice and this permission notice appear in 14 * supporting documentation. Further, provided that the name of Open 15 * Software Foundation, Inc. ("OSF") not be used in advertising or 16 * publicity pertaining to distribution of the software without prior 17 * written permission from OSF. OSF makes no representations about the 18 * suitability of this software for any purpose. It is provided "as is" 19 * without express or implied warranty. 20 */ 21 /* 22 * Copyright (c) 1996 X Consortium 23 * Copyright (c) 1995, 1996 Dalrymple Consulting 24 * 25 * Permission is hereby granted, free of charge, to any person obtaining a copy 26 * of this software and associated documentation files (the "Software"), to deal 27 * in the Software without restriction, including without limitation the rights 28 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 29 * copies of the Software, and to permit persons to whom the Software is 30 * furnished to do so, subject to the following conditions: 31 * 32 * The above copyright notice and this permission notice shall be included in 33 * all copies or substantial portions of the Software. 34 * 35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 38 * X CONSORTIUM OR DALRYMPLE CONSULTING BE LIABLE FOR ANY CLAIM, DAMAGES OR 39 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 40 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 41 * OTHER DEALINGS IN THE SOFTWARE. 42 * 43 * Except as contained in this notice, the names of the X Consortium and 44 * Dalrymple Consulting shall not be used in advertising or otherwise to 45 * promote the sale, use or other dealings in this Software without prior 46 * written authorization. 47 */ 48 /* ________________________________________________________________________ 49 * 50 * Common definitions for "instant" program. 51 * ________________________________________________________________________ 52 */ 53 54 #ifdef STORAGE 55 #ifndef lint 56 static char *gen_h_RCSid = 57 "$Header: /usr/src/docbook-to-man/Instant/RCS/general.h,v 1.6 1997/08/26 04:04:40 fld Exp $"; 58 #endif 59 #endif 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 /* Relationships - for querying */ 107 typedef enum { 108 REL_None, REL_Parent, REL_Child, REL_Ancestor, REL_Descendant, 109 REL_Sibling, REL_Preceding, REL_ImmPreceding, REL_Following, 110 REL_ImmFollowing, REL_Cousin, REL_Unknown 111 } Relation_t; 112 113 /* Initial map sizes (IMS) */ 114 #define IMS_relations 3 115 #define IMS_setvar 3 116 #define IMS_incvar 3 117 #define IMS_sdata 50 118 #define IMS_sdatacache 30 119 #define IMS_variables 20 120 #define IMS_attnames 50 121 #define IMS_elemnames 50 122 123 /* ----- typedef and other misc definitions ----- */ 124 125 #ifndef TRUE 126 #define TRUE (1 == 1) 127 #endif 128 129 #ifndef FALSE 130 #define FALSE (1 == 0) 131 #endif 132 133 typedef short bool; 134 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 237 /* ----- some macros for convenience and ease of code reading ----- */ 238 239 #define stripNL(s) { char *_cp; if ((_cp=strchr(s, NL))) *_cp = EOS; } 240 241 /* Similar to calloc(), malloc(), and realloc(), but check for success. 242 * Args to all: 243 * (1) number of 'elements' to allocate 244 * (2) variable to point at allocated space 245 * (3) type of 'element' 246 * Eg: Calloc(5, e, Element_t) replaces 247 * if (!(e = (Element_t *)calloc(5, sizeof(Element_t))) { 248 * ... handle error ... ; 249 * } 250 */ 251 #define Calloc(N,V,T) \ 252 { if (!((V) = (T *)calloc((size_t)N, sizeof(T)))) { \ 253 perror("Calloc failed -- out of memory. Bailing out."); exit(1); \ 254 }; memset((void *) (V), 0, (size_t) sizeof(T)); } 255 #define Malloc(N,V,T) \ 256 { if (!((V) = (T *)malloc((size_t)N*sizeof(T)))) { \ 257 perror("Malloc failed -- out of memory. Bailing out."); exit(1); \ 258 }; memset((void *) (V), 0, (size_t) sizeof(T)); } 259 #define Realloc(N,V,T) \ 260 { if (!((V) = (T *)realloc(V,(size_t)N*sizeof(T)))) { \ 261 perror("Realloc failed -- out of memory. Bailing out."); exit(1); \ 262 } } 263 264 /* similar to strcmp(), but check first chars first, for efficiency */ 265 #define StrEq(s1,s2) (s1[0] == s2[0] && !strcmp(s1,s2)) 266 267 /* similar to isspace(), but check for blank or tab - without overhead 268 * of procedure call */ 269 #define IsWhite(c) (c == ' ' || c == TAB) 270 271 #define ContType(e,i) (e->cont[i].type) 272 #define ContData(e,i) (e->cont[i].ch.data) 273 #define ContElem(e,i) (e->cont[i].ch.elem) 274 #define IsContData(e,i) (e->cont[i].type == CMD_DATA) 275 #define IsContElem(e,i) (e->cont[i].type == CMD_OPEN) 276 #define IsContPI(e,i) (e->cont[i].type == CMD_PI) 277 278 /* ----- function prototypes ----- */ 279 280 /* things defined in main.c */ 281 282 Entity_t * FindEntity(char *); 283 284 /* things defined in util.c */ 285 Element_t *QRelation(Element_t *, char *, Relation_t); 286 Relation_t FindRelByName(char *); 287 char *FindAttValByName(Element_t *, char *); 288 char *FindContext(Element_t *, int, char *); 289 char *AddElemName(char *); 290 char *AddAttName(char *); 291 void OutputString(char *, FILE *, int); 292 FILE *OpenFile(char *); 293 char *FindElementPath(Element_t *, char *); 294 char *NearestOlderElem(Element_t *, char *); 295 void PrintLocation(Element_t *, FILE *); 296 char **Split(char *, int *, int); 297 void DescendTree(Element_t *, void(*)(), void(*)(), void(*)(), void *); 298 Map_t *NewMap(int); 299 Mapping_t *FindMapping(Map_t *, char *); 300 char *FindMappingVal(Map_t *, char *); 301 void SetMapping(Map_t *, char *); 302 void SetMappingNV(Map_t *, char *, char *); 303 void AddID(Element_t *, char *); 304 Element_t *FindElemByID(char *); 305 306 /* things defined in translate.c */ 307 void DoTranslate(Element_t*, char *, FILE *); 308 309 /* things defined in traninit.c */ 310 void ReadTransSpec(char *); 311 void ReadSDATA(char *); 312 void ReadCharMap(char *); 313 314 /* things defined in tranvar.c */ 315 char *Get_A_C_value(char *); 316 317 /* things defined in info.c */ 318 void PrintContext(Element_t *e); 319 void PrintElemSummary(Element_t *); 320 void PrintElemTree(Element_t *); 321 void PrintStats(Element_t *); 322 void PrintIDList(); 323 324 /* ----- other declarations ----- */ 325 326 #ifdef ultrix 327 #define strdup(s) strcpy((char *)malloc(strlen(s)+1), s) 328 #endif 329 330