dthelp: Further coverity fixes
[oweals/cde.git] / cde / programs / dthelp / parser / pass2 / htag2 / sdl.c
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 /* $TOG: sdl.c /main/5 1998/04/06 13:19:43 mgreess $ */
24 /*   Copyright (c) 1986, 1987, 1988, 1989 Hewlett-Packard Co. */
25 /* Miscellaneous Procedures for HP Tag/TeX translator */
26
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 #if defined(__osf__) || defined(linux)
30 #  include <sys/fcntl.h>
31 #endif
32 #if defined(__osf__)
33 #  include <sys/access.h>
34 #  include <locale.h>
35 #endif /* __osf__ */
36 #include <errno.h>
37
38 #if defined(linux)
39 #include <getopt.h>
40 #endif
41
42 #include "userinc.h"
43 #include "globdec.h"
44
45 #include "LocaleXlate.h"
46 #include "XlationSvc.h"
47
48 static char *operantLocale;
49
50 static char openLoids[]  = "<LOIDS COUNT=\"%d\">\n";
51 static char closeLoids[] = "</LOIDS>\n";
52 static int  nIds;
53
54 static char wideCharacterString[] = "wide character string";
55
56
57 #if defined(M_PROTO)
58 char *mb_malloc(long size)
59 #else
60 char *mb_malloc(size)
61 long  size;
62 #endif
63 {
64 char   *cptr;
65 size_t  ssize;
66
67 ssize = (size_t) size;
68 cptr = (char *) malloc(ssize);
69
70 #if DEBUG_MB_MALLOC
71 fprintf(stdout, "malloc 0x%p\n", cptr);
72 fflush(stdout);
73 #endif
74
75 *cptr = '\0';
76 return cptr;
77 }
78
79
80 #if defined(M_PROTO)
81 void mb_free(char **pptr)
82 #else
83 void  *mb_free(pptr)
84 char **pptr;
85 #endif
86 {
87
88 #if DEBUG_MB_MALLOC
89 fprintf(stdout, "free 0x%p\n", *pptr);
90 fflush(stdout);
91 #endif
92
93 free((void *) *pptr);
94 *pptr = NULL;
95 }
96
97
98 #if defined(M_PROTO)
99 char *mb_realloc(char *ptr, long size)
100 #else
101 char *mb_realloc(ptr, size)
102 char *ptr;
103 long  size;
104 #endif
105 {
106 void   *vptr;
107 size_t  ssize;
108
109 ssize = (size_t) size;
110 vptr = realloc((void *) ptr, ssize);
111
112 #if DEBUG_MB_MALLOC
113 if (ptr != vptr)
114     {
115     fprintf(stdout, "realloc 0x%p 0x%p\n", ptr, vptr);
116     fflush(stdout);
117     }
118 #endif
119
120 return (char *) vptr;
121 }
122
123
124 #if defined(M_PROTO)
125 void OpenTag(LOGICAL hasAttributes, LOGICAL newlineOK)
126 #else
127 void OpenTag(hasAttributes, newlineOK)
128   LOGICAL hasAttributes;
129   LOGICAL newlineOK;
130 #endif
131 {
132 PutString("<");
133 PutWString(m_parent(0));
134 if (!hasAttributes)
135     {
136     PutString(">");
137     if (newlineOK)
138         PutString("\n");
139     }
140 }
141
142
143 #if defined(M_PROTO)
144 static void OpenNamedTag(char *name, LOGICAL hasAttributes, LOGICAL newlineOK)
145 #else
146 static void OpenNamedTag(name, hasAttributes, newlineOK)
147   LOGICAL  hasAttributes;
148   LOGICAL  newlineOK;
149   char    *name;
150 #endif
151 {
152 PutString("<");
153 PutString(name);
154 if (!hasAttributes)
155     {
156     PutString(">");
157     if (newlineOK)
158         PutString("\n");
159     }
160 }
161
162
163 #if defined(M_PROTO)
164 void CloseTag(LOGICAL newlineOK)
165 #else
166 void CloseTag(newlineOK)
167   LOGICAL newlineOK;
168 #endif
169 {
170 char *etagc;
171
172 etagc = ">";
173 if (newlineOK)
174     etagc = ">\n";
175
176 PutString("</");
177 PutWString(m_parent(0));
178 PutString(etagc);
179 }
180
181
182 #if defined(M_PROTO)
183 static void CloseNamedTag(char *name, LOGICAL newlineOK)
184 #else
185 static void CloseNamedTag(name, newlineOK)
186   LOGICAL newlineOK;
187   char *name; 
188 #endif
189 {
190 char *etagc;
191
192 etagc = ">";
193 if (newlineOK)
194     etagc = ">\n";
195
196 PutString("</");
197 PutString(name);
198 PutString(etagc);
199 }
200
201
202 #if defined(M_PROTO)
203 void ImpliedAttribute(char *name, M_WCHAR *value)
204 #else
205 void ImpliedAttribute(name, value)
206   char    *name;
207   M_WCHAR *value;
208 #endif
209 {
210 if (value)
211     {
212     PutString(" ");
213     PutString(name);
214     PutString("=\"");
215     PutWString(value);
216     PutString("\"");
217     }
218 }
219
220
221 #if defined(M_PROTO)
222 void RequiredAttribute(char *name, M_WCHAR *value)
223 #else
224 void RequiredAttribute(name, value)
225   char    *name;
226   M_WCHAR *value;
227 #endif
228 {
229 M_WCHAR  null;
230 M_WCHAR *wc_name;
231
232 if (!value)
233     {
234     null = 0;
235     value = &null;
236     wc_name = MakeWideCharString(name);
237     m_err2("Missing %s in %s", wc_name, m_parent(0));
238     m_free(wc_name, "wide character name");
239     }
240 PutString(" ");
241 PutString(name);
242 PutString("=\"");
243 PutWString(value);
244 PutString("\"");
245 }
246
247
248 #if defined(M_PROTO)
249 M_WCHAR *CopyWString(M_WCHAR *string)
250 #else
251 M_WCHAR *CopyWString(string)
252   M_WCHAR *string;
253 #endif
254 {
255 M_WCHAR *retVal;
256
257 if (!string) return NULL;
258
259 retVal = m_malloc(w_strlen(string) + 1, wideCharacterString);
260 w_strcpy(retVal, string);
261 return retVal;
262 }
263
264
265 #if defined(M_PROTO)
266 void MakeNewElement(ElementTypes type)
267 #else
268 void MakeNewElement(type)
269   ElementTypes type;
270 #endif
271 {
272 pCurrentElement = (ElementPtr) mb_malloc(sizeof(Element));
273 memset((void *) pCurrentElement, 0, sizeof(Element));
274 pCurrentElement->type = type;
275 if (type == e_cdata)
276     {
277     pCurrentElement->u.u_cdata.buffer   = m_malloc(256, "u_cdata.buffer");
278     pCurrentElement->u.u_cdata.buffPos  = -1;
279     pCurrentElement->u.u_cdata.buffSize = 256;
280     }
281 }
282
283
284 #if defined(M_PROTO)
285 void DeleteElement(ElementPtr pElement)
286 #else
287 void DeleteElement(pElement)
288   ElementPtr pElement;
289 #endif
290 {
291 switch (pElement->type)
292     {
293     case e_sdldoc:
294         if (pElement->u.u_sdldoc.pub_id)
295             m_free(pElement->u.u_sdldoc.pub_id, wideCharacterString);
296         if (pElement->u.u_sdldoc.doc_id)
297             m_free(pElement->u.u_sdldoc.doc_id, wideCharacterString);
298         if (pElement->u.u_sdldoc.first_page)
299             m_free(pElement->u.u_sdldoc.first_page, wideCharacterString);
300         if (pElement->u.u_sdldoc.timestmp)
301             m_free(pElement->u.u_sdldoc.timestmp, wideCharacterString);
302         if (pElement->u.u_sdldoc.product)
303             m_free(pElement->u.u_sdldoc.product, wideCharacterString);
304         if (pElement->u.u_sdldoc.prodpn)
305             m_free(pElement->u.u_sdldoc.prodpn, wideCharacterString);
306         if (pElement->u.u_sdldoc.prodver)
307             m_free(pElement->u.u_sdldoc.prodver, wideCharacterString);
308         if (pElement->u.u_sdldoc.license)
309             m_free(pElement->u.u_sdldoc.license, wideCharacterString);
310         if (pElement->u.u_sdldoc.language)
311             m_free(pElement->u.u_sdldoc.language, wideCharacterString);
312         if (pElement->u.u_sdldoc.charset)
313             m_free(pElement->u.u_sdldoc.charset, wideCharacterString);
314         if (pElement->u.u_sdldoc.author)
315             m_free(pElement->u.u_sdldoc.author, wideCharacterString);
316         if (pElement->u.u_sdldoc.version)
317             m_free(pElement->u.u_sdldoc.version, wideCharacterString);
318         if (pElement->u.u_sdldoc.sdldtd)
319             m_free(pElement->u.u_sdldoc.sdldtd, wideCharacterString);
320         if (pElement->u.u_sdldoc.srcdtd)
321             m_free(pElement->u.u_sdldoc.srcdtd, wideCharacterString);
322         break;
323     case e_virpage:
324         if (pElement->u.u_virpage.id)
325             m_free(pElement->u.u_virpage.id, wideCharacterString);
326         if (pElement->u.u_virpage.level)
327             m_free(pElement->u.u_virpage.level, wideCharacterString);
328         if (pElement->u.u_virpage.version)
329             m_free(pElement->u.u_virpage.version, wideCharacterString);
330         if (pElement->u.u_virpage.language)
331             m_free(pElement->u.u_virpage.language, wideCharacterString);
332         if (pElement->u.u_virpage.charset)
333             m_free(pElement->u.u_virpage.charset, wideCharacterString);
334         if (pElement->u.u_virpage.doc_id)
335             m_free(pElement->u.u_virpage.doc_id, wideCharacterString);
336         if (pElement->u.u_virpage.ssi)
337             m_free(pElement->u.u_virpage.ssi, wideCharacterString);
338         break;
339     case e_snb:
340         if (pElement->u.u_snb.version)
341             m_free(pElement->u.u_snb.version, wideCharacterString);
342         break;
343     case e_block:
344         if (pElement->u.u_block.id)
345             m_free(pElement->u.u_block.id, wideCharacterString);
346         if (pElement->u.u_block.level)
347             m_free(pElement->u.u_block.level, wideCharacterString);
348         if (pElement->u.u_block.version)
349             m_free(pElement->u.u_block.version, wideCharacterString);
350         if (pElement->u.u_block.class)
351             m_free(pElement->u.u_block.class, wideCharacterString);
352         if (pElement->u.u_block.language)
353             m_free(pElement->u.u_block.language, wideCharacterString);
354         if (pElement->u.u_block.charset)
355             m_free(pElement->u.u_block.charset, wideCharacterString);
356         if (pElement->u.u_block.length)
357             m_free(pElement->u.u_block.length, wideCharacterString);
358         if (pElement->u.u_block.app)
359             m_free(pElement->u.u_block.app, wideCharacterString);
360         if (pElement->u.u_block.timing)
361             m_free(pElement->u.u_block.timing, wideCharacterString);
362         if (pElement->u.u_block.ssi)
363             m_free(pElement->u.u_block.ssi, wideCharacterString);
364         break;
365     case e_form:
366         if (pElement->u.u_form.id)
367             m_free(pElement->u.u_form.id, wideCharacterString);
368         if (pElement->u.u_form.level)
369             m_free(pElement->u.u_form.level, wideCharacterString);
370         if (pElement->u.u_form.version)
371             m_free(pElement->u.u_form.version, wideCharacterString);
372         if (pElement->u.u_form.class)
373             m_free(pElement->u.u_form.class, wideCharacterString);
374         if (pElement->u.u_form.language)
375             m_free(pElement->u.u_form.language, wideCharacterString);
376         if (pElement->u.u_form.charset)
377             m_free(pElement->u.u_form.charset, wideCharacterString);
378         if (pElement->u.u_form.length)
379             m_free(pElement->u.u_form.length, wideCharacterString);
380         if (pElement->u.u_form.app)
381             m_free(pElement->u.u_form.app, wideCharacterString);
382         if (pElement->u.u_form.ssi)
383             m_free(pElement->u.u_form.ssi, wideCharacterString);
384         break;
385     case e_fstyle:
386         if (pElement->u.u_fstyle.ncols)
387             m_free(pElement->u.u_fstyle.ncols, wideCharacterString);
388         break;
389     case e_frowvec:
390         if (pElement->u.u_frowvec.hdr)
391             m_free(pElement->u.u_frowvec.hdr, wideCharacterString);
392         if (pElement->u.u_frowvec.cells)
393             m_free(pElement->u.u_frowvec.cells, wideCharacterString);
394         break;
395     case e_fdata:
396         break;
397     case e_p:
398         if (pElement->u.u_p.id)
399             m_free(pElement->u.u_p.id, wideCharacterString);
400         if (pElement->u.u_p.version)
401             m_free(pElement->u.u_p.version, wideCharacterString);
402         if (pElement->u.u_p.type)
403             m_free(pElement->u.u_p.type, wideCharacterString);
404         if (pElement->u.u_p.ssi)
405             m_free(pElement->u.u_p.ssi, wideCharacterString);
406         break;
407     case e_cp:
408         if (pElement->u.u_cp.id)
409             m_free(pElement->u.u_cp.id, wideCharacterString);
410         if (pElement->u.u_cp.version)
411             m_free(pElement->u.u_cp.version, wideCharacterString);
412         if (pElement->u.u_cp.type)
413             m_free(pElement->u.u_cp.type, wideCharacterString);
414         if (pElement->u.u_cp.ssi)
415             m_free(pElement->u.u_cp.ssi, wideCharacterString);
416         break;
417     case e_head:
418         if (pElement->u.u_head.id)
419             m_free(pElement->u.u_head.id, wideCharacterString);
420         if (pElement->u.u_head.version)
421             m_free(pElement->u.u_head.version, wideCharacterString);
422         if (pElement->u.u_head.class)
423             m_free(pElement->u.u_head.class, wideCharacterString);
424         if (pElement->u.u_head.language)
425             m_free(pElement->u.u_head.language, wideCharacterString);
426         if (pElement->u.u_head.charset)
427             m_free(pElement->u.u_head.charset, wideCharacterString);
428         if (pElement->u.u_head.type)
429             m_free(pElement->u.u_head.type, wideCharacterString);
430         if (pElement->u.u_head.ssi)
431             m_free(pElement->u.u_head.ssi, wideCharacterString);
432         if (pElement->u.u_head.abbrev)
433             m_free(pElement->u.u_head.abbrev, wideCharacterString);
434         break;
435     case e_subhead:
436         if (pElement->u.u_subhead.id)
437             m_free(pElement->u.u_subhead.id, wideCharacterString);
438         if (pElement->u.u_subhead.version)
439             m_free(pElement->u.u_subhead.version, wideCharacterString);
440         if (pElement->u.u_subhead.class)
441             m_free(pElement->u.u_subhead.class, wideCharacterString);
442         if (pElement->u.u_subhead.language)
443             m_free(pElement->u.u_subhead.language, wideCharacterString);
444         if (pElement->u.u_subhead.charset)
445             m_free(pElement->u.u_subhead.charset, wideCharacterString);
446         if (pElement->u.u_subhead.type)
447             m_free(pElement->u.u_subhead.type, wideCharacterString);
448         if (pElement->u.u_subhead.ssi)
449             m_free(pElement->u.u_subhead.ssi, wideCharacterString);
450         if (pElement->u.u_subhead.abbrev)
451             m_free(pElement->u.u_subhead.abbrev, wideCharacterString);
452         break;
453     case e_anchor:
454         if (pElement->u.u_anchor.id)
455             m_free(pElement->u.u_anchor.id, wideCharacterString);
456         break;
457     case e_link:
458         if (pElement->u.u_link.rid)
459             m_free(pElement->u.u_link.rid, wideCharacterString);
460         if (pElement->u.u_link.button)
461             m_free(pElement->u.u_link.button, wideCharacterString);
462         if (pElement->u.u_link.linkinfo)
463             m_free(pElement->u.u_link.linkinfo, wideCharacterString);
464         if (pElement->u.u_link.descript)
465             m_free(pElement->u.u_link.descript, wideCharacterString);
466         if (pElement->u.u_link.window)
467             m_free(pElement->u.u_link.window, wideCharacterString);
468         if (pElement->u.u_link.traversal)
469             m_free(pElement->u.u_link.traversal, wideCharacterString);
470         break;
471     case e_snref:
472         if (pElement->u.u_snref.id)
473             m_free(pElement->u.u_snref.id, wideCharacterString);
474         break;
475     case e_refitem:
476         if (pElement->u.u_refitem.rid)
477             m_free(pElement->u.u_refitem.rid, wideCharacterString);
478         if (pElement->u.u_refitem.class)
479             m_free(pElement->u.u_refitem.class, wideCharacterString);
480         if (pElement->u.u_refitem.button)
481             m_free(pElement->u.u_refitem.button, wideCharacterString);
482         if (pElement->u.u_refitem.ssi)
483             m_free(pElement->u.u_refitem.ssi, wideCharacterString);
484         break;
485     case e_alttext:
486         break;
487     case e_rev:
488         break;
489     case e_key:
490         if (pElement->u.u_key.charset)
491             m_free(pElement->u.u_key.charset, wideCharacterString);
492         if (pElement->u.u_key.class)
493             m_free(pElement->u.u_key.class, wideCharacterString);
494         if (pElement->u.u_key.ssi)
495             m_free(pElement->u.u_key.ssi, wideCharacterString);
496         break;
497     case e_sphrase:
498         if (pElement->u.u_sphrase.class)
499             m_free(pElement->u.u_sphrase.class, wideCharacterString);
500         if (pElement->u.u_sphrase.ssi)
501             m_free(pElement->u.u_sphrase.ssi, wideCharacterString);
502         break;
503     case e_if:
504         break;
505     case e_cond:
506         if (pElement->u.u_cond.interp)
507             m_free(pElement->u.u_cond.interp, wideCharacterString);
508         break;
509     case e_then:
510         break;
511     case e_else:
512         break;
513     case e_spc:
514         if (pElement->u.u_spc.name)
515             m_free(pElement->u.u_spc.name, wideCharacterString);
516         break;
517     case e_graphic:
518         if (pElement->u.u_graphic.id)
519             m_free(pElement->u.u_graphic.id, wideCharacterString);
520         if (pElement->u.u_graphic.format)
521             m_free(pElement->u.u_graphic.format, wideCharacterString);
522         if (pElement->u.u_graphic.method)
523             m_free(pElement->u.u_graphic.method, wideCharacterString);
524         if (pElement->u.u_graphic.xid)
525             m_free(pElement->u.u_graphic.xid, wideCharacterString);
526         break;
527     case e_text:
528         if (pElement->u.u_text.id)
529             m_free(pElement->u.u_text.id, wideCharacterString);
530         if (pElement->u.u_text.language)
531             m_free(pElement->u.u_text.language, wideCharacterString);
532         if (pElement->u.u_text.charset)
533             m_free(pElement->u.u_text.charset, wideCharacterString);
534         break;
535     case e_audio:
536         if (pElement->u.u_audio.id)
537             m_free(pElement->u.u_audio.id, wideCharacterString);
538         if (pElement->u.u_audio.format)
539             m_free(pElement->u.u_audio.format, wideCharacterString);
540         if (pElement->u.u_audio.method)
541             m_free(pElement->u.u_audio.method, wideCharacterString);
542         if (pElement->u.u_audio.xid)
543             m_free(pElement->u.u_audio.xid, wideCharacterString);
544         break;
545     case e_video:
546         if (pElement->u.u_video.id)
547             m_free(pElement->u.u_video.id, wideCharacterString);
548         if (pElement->u.u_video.format)
549             m_free(pElement->u.u_video.format, wideCharacterString);
550         if (pElement->u.u_video.method)
551             m_free(pElement->u.u_video.method, wideCharacterString);
552         if (pElement->u.u_video.xid)
553             m_free(pElement->u.u_video.xid, wideCharacterString);
554         break;
555     case e_animate:
556         if (pElement->u.u_animate.id)
557             m_free(pElement->u.u_animate.id, wideCharacterString);
558         if (pElement->u.u_animate.format)
559             m_free(pElement->u.u_animate.format, wideCharacterString);
560         if (pElement->u.u_animate.method)
561             m_free(pElement->u.u_animate.method, wideCharacterString);
562         if (pElement->u.u_animate.xid)
563             m_free(pElement->u.u_animate.xid, wideCharacterString);
564         break;
565     case e_script:
566         if (pElement->u.u_script.id)
567             m_free(pElement->u.u_script.id, wideCharacterString);
568         if (pElement->u.u_script.interp)
569             m_free(pElement->u.u_script.interp, wideCharacterString);
570         break;
571     case e_crossdoc:
572         if (pElement->u.u_crossdoc.id)
573             m_free(pElement->u.u_crossdoc.id, wideCharacterString);
574         if (pElement->u.u_crossdoc.xid)
575             m_free(pElement->u.u_crossdoc.xid, wideCharacterString);
576         break;
577     case e_man_page:
578         if (pElement->u.u_man_page.id)
579             m_free(pElement->u.u_man_page.id, wideCharacterString);
580         if (pElement->u.u_man_page.xid)
581             m_free(pElement->u.u_man_page.xid, wideCharacterString);
582         break;
583     case e_textfile:
584         if (pElement->u.u_textfile.id)
585             m_free(pElement->u.u_textfile.id, wideCharacterString);
586         if (pElement->u.u_textfile.xid)
587             m_free(pElement->u.u_textfile.xid, wideCharacterString);
588         if (pElement->u.u_textfile.offset)
589             m_free(pElement->u.u_textfile.offset, wideCharacterString);
590         if (pElement->u.u_textfile.format)
591             m_free(pElement->u.u_textfile.format, wideCharacterString);
592         break;
593     case e_sys_cmd:
594         if (pElement->u.u_sys_cmd.id)
595             m_free(pElement->u.u_sys_cmd.id, wideCharacterString);
596         if (pElement->u.u_sys_cmd.command)
597             m_free(pElement->u.u_sys_cmd.command, wideCharacterString);
598         break;
599     case e_callback:
600         if (pElement->u.u_callback.id)
601             m_free(pElement->u.u_callback.id, wideCharacterString);
602         if (pElement->u.u_callback.data)
603             m_free(pElement->u.u_callback.data, wideCharacterString);
604         break;
605     case e_switch:
606         if (pElement->u.u_switch.id)
607             m_free(pElement->u.u_switch.id, wideCharacterString);
608         if (pElement->u.u_switch.interp)
609             m_free(pElement->u.u_switch.interp, wideCharacterString);
610         if (pElement->u.u_switch.branches)
611             m_free(pElement->u.u_switch.branches, wideCharacterString);
612         break;
613     case e_cdata:
614         m_free(pElement->u.u_cdata.buffer, "u_cdata.buffer");
615         break;
616     case e_rel_docs:
617         break;
618     case e_rel_file:
619         break;
620     case e_notes:
621         break;
622     case e_loids:
623         break;
624     case e_id:
625         if (pElement->u.u_id.type)
626             m_free(pElement->u.u_id.type, wideCharacterString);
627         if (pElement->u.u_id.rid)
628             m_free(pElement->u.u_id.rid, wideCharacterString);
629         if (pElement->u.u_id.rssi)
630             m_free(pElement->u.u_id.rssi, wideCharacterString);
631         if (pElement->u.u_id.rlevel)
632             m_free(pElement->u.u_id.rlevel, wideCharacterString);
633         if (pElement->u.u_id.offset)
634             m_free(pElement->u.u_id.offset, wideCharacterString);
635         break;
636     case e_index:
637         break;
638     case e_entry:
639         if (pElement->u.u_entry.id)
640             m_free(pElement->u.u_entry.id, wideCharacterString);
641         if (pElement->u.u_entry.main)
642             m_free(pElement->u.u_entry.main, wideCharacterString);
643         if (pElement->u.u_entry.locs)
644             m_free(pElement->u.u_entry.locs, wideCharacterString);
645         if (pElement->u.u_entry.syns)
646             m_free(pElement->u.u_entry.syns, wideCharacterString);
647         if (pElement->u.u_entry.sort)
648             m_free(pElement->u.u_entry.sort, wideCharacterString);
649         break;
650     case e_lophrases:
651         break;
652     case e_phrase:
653         break;
654     case e_toss:
655         break;
656     case e_keystyle:
657         if (pElement->u.u_keystyle.class)
658             m_free(pElement->u.u_keystyle.class, wideCharacterString);
659         if (pElement->u.u_keystyle.ssi)
660             m_free(pElement->u.u_keystyle.ssi, wideCharacterString);
661         if (pElement->u.u_keystyle.rlevel)
662             m_free(pElement->u.u_keystyle.rlevel, wideCharacterString);
663         if (pElement->u.u_keystyle.phrase)
664             m_free(pElement->u.u_keystyle.phrase, wideCharacterString);
665         if (pElement->u.u_keystyle.srch_wt)
666             m_free(pElement->u.u_keystyle.srch_wt, wideCharacterString);
667         if (pElement->u.u_keystyle.enter)
668             m_free(pElement->u.u_keystyle.enter, wideCharacterString);
669         if (pElement->u.u_keystyle.exit)
670             m_free(pElement->u.u_keystyle.exit, wideCharacterString);
671         if (pElement->u.u_keystyle.pointsz)
672             m_free(pElement->u.u_keystyle.pointsz, wideCharacterString);
673         if (pElement->u.u_keystyle.set_width)
674             m_free(pElement->u.u_keystyle.set_width, wideCharacterString);
675         if (pElement->u.u_keystyle.color)
676             m_free(pElement->u.u_keystyle.color, wideCharacterString);
677         if (pElement->u.u_keystyle.xlfd)
678             m_free(pElement->u.u_keystyle.xlfd, wideCharacterString);
679         if (pElement->u.u_keystyle.xlfdi)
680             m_free(pElement->u.u_keystyle.xlfdi, wideCharacterString);
681         if (pElement->u.u_keystyle.xlfdb)
682             m_free(pElement->u.u_keystyle.xlfdb, wideCharacterString);
683         if (pElement->u.u_keystyle.xlfdib)
684             m_free(pElement->u.u_keystyle.xlfdib, wideCharacterString);
685         if (pElement->u.u_keystyle.typenam)
686             m_free(pElement->u.u_keystyle.typenam, wideCharacterString);
687         if (pElement->u.u_keystyle.typenami)
688             m_free(pElement->u.u_keystyle.typenami, wideCharacterString);
689         if (pElement->u.u_keystyle.typenamb)
690             m_free(pElement->u.u_keystyle.typenamb, wideCharacterString);
691         if (pElement->u.u_keystyle.typenamib)
692             m_free(pElement->u.u_keystyle.typenamib, wideCharacterString);
693         if (pElement->u.u_keystyle.style)
694             m_free(pElement->u.u_keystyle.style, wideCharacterString);
695         if (pElement->u.u_keystyle.spacing)
696             m_free(pElement->u.u_keystyle.spacing, wideCharacterString);
697         if (pElement->u.u_keystyle.weight)
698             m_free(pElement->u.u_keystyle.weight, wideCharacterString);
699         if (pElement->u.u_keystyle.slant)
700             m_free(pElement->u.u_keystyle.slant, wideCharacterString);
701         if (pElement->u.u_keystyle.special)
702             m_free(pElement->u.u_keystyle.special, wideCharacterString);
703         break;
704     case e_headstyle:
705         if (pElement->u.u_headstyle.class)
706             m_free(pElement->u.u_headstyle.class, wideCharacterString);
707         if (pElement->u.u_headstyle.ssi)
708             m_free(pElement->u.u_headstyle.ssi, wideCharacterString);
709         if (pElement->u.u_headstyle.rlevel)
710             m_free(pElement->u.u_headstyle.rlevel, wideCharacterString);
711         if (pElement->u.u_headstyle.phrase)
712             m_free(pElement->u.u_headstyle.phrase, wideCharacterString);
713         if (pElement->u.u_headstyle.srch_wt)
714             m_free(pElement->u.u_headstyle.srch_wt, wideCharacterString);
715         if (pElement->u.u_headstyle.orient)
716             m_free(pElement->u.u_headstyle.orient, wideCharacterString);
717         if (pElement->u.u_headstyle.vorient)
718             m_free(pElement->u.u_headstyle.vorient, wideCharacterString);
719         if (pElement->u.u_headstyle.placement)
720             m_free(pElement->u.u_headstyle.placement, wideCharacterString);
721         if (pElement->u.u_headstyle.headw)
722             m_free(pElement->u.u_headstyle.headw, wideCharacterString);
723         if (pElement->u.u_headstyle.stack)
724             m_free(pElement->u.u_headstyle.stack, wideCharacterString);
725         if (pElement->u.u_headstyle.flow)
726             m_free(pElement->u.u_headstyle.flow, wideCharacterString);
727         if (pElement->u.u_headstyle.pointsz)
728             m_free(pElement->u.u_headstyle.pointsz, wideCharacterString);
729         if (pElement->u.u_headstyle.set_width)
730             m_free(pElement->u.u_headstyle.set_width, wideCharacterString);
731         if (pElement->u.u_headstyle.color)
732             m_free(pElement->u.u_headstyle.color, wideCharacterString);
733         if (pElement->u.u_headstyle.xlfd)
734             m_free(pElement->u.u_headstyle.xlfd, wideCharacterString);
735         if (pElement->u.u_headstyle.xlfdi)
736             m_free(pElement->u.u_headstyle.xlfdi, wideCharacterString);
737         if (pElement->u.u_headstyle.xlfdb)
738             m_free(pElement->u.u_headstyle.xlfdb, wideCharacterString);
739         if (pElement->u.u_headstyle.xlfdib)
740             m_free(pElement->u.u_headstyle.xlfdib, wideCharacterString);
741         if (pElement->u.u_headstyle.typenam)
742             m_free(pElement->u.u_headstyle.typenam, wideCharacterString);
743         if (pElement->u.u_headstyle.typenami)
744             m_free(pElement->u.u_headstyle.typenami, wideCharacterString);
745         if (pElement->u.u_headstyle.typenamb)
746             m_free(pElement->u.u_headstyle.typenamb, wideCharacterString);
747         if (pElement->u.u_headstyle.typenamib)
748             m_free(pElement->u.u_headstyle.typenamib, wideCharacterString);
749         if (pElement->u.u_headstyle.style)
750             m_free(pElement->u.u_headstyle.style, wideCharacterString);
751         if (pElement->u.u_headstyle.spacing)
752             m_free(pElement->u.u_headstyle.spacing, wideCharacterString);
753         if (pElement->u.u_headstyle.weight)
754             m_free(pElement->u.u_headstyle.weight, wideCharacterString);
755         if (pElement->u.u_headstyle.slant)
756             m_free(pElement->u.u_headstyle.slant, wideCharacterString);
757         if (pElement->u.u_headstyle.special)
758             m_free(pElement->u.u_headstyle.special, wideCharacterString);
759         if (pElement->u.u_headstyle.l_margin)
760             m_free(pElement->u.u_headstyle.l_margin, wideCharacterString);
761         if (pElement->u.u_headstyle.r_margin)
762             m_free(pElement->u.u_headstyle.r_margin, wideCharacterString);
763         if (pElement->u.u_headstyle.t_margin)
764             m_free(pElement->u.u_headstyle.t_margin, wideCharacterString);
765         if (pElement->u.u_headstyle.b_margin)
766             m_free(pElement->u.u_headstyle.b_margin, wideCharacterString);
767         if (pElement->u.u_headstyle.border)
768             m_free(pElement->u.u_headstyle.border, wideCharacterString);
769         if (pElement->u.u_headstyle.vjust)
770             m_free(pElement->u.u_headstyle.vjust, wideCharacterString);
771         if (pElement->u.u_headstyle.justify)
772             m_free(pElement->u.u_headstyle.justify, wideCharacterString);
773         break;
774     case e_formstyle:
775         if (pElement->u.u_formstyle.class)
776             m_free(pElement->u.u_formstyle.class, wideCharacterString);
777         if (pElement->u.u_formstyle.ssi)
778             m_free(pElement->u.u_formstyle.ssi, wideCharacterString);
779         if (pElement->u.u_formstyle.rlevel)
780             m_free(pElement->u.u_formstyle.rlevel, wideCharacterString);
781         if (pElement->u.u_formstyle.phrase)
782             m_free(pElement->u.u_formstyle.phrase, wideCharacterString);
783         if (pElement->u.u_formstyle.srch_wt)
784             m_free(pElement->u.u_formstyle.srch_wt, wideCharacterString);
785         if (pElement->u.u_formstyle.colw)
786             m_free(pElement->u.u_formstyle.colw, wideCharacterString);
787         if (pElement->u.u_formstyle.colj)
788             m_free(pElement->u.u_formstyle.colj, wideCharacterString);
789         if (pElement->u.u_formstyle.pointsz)
790             m_free(pElement->u.u_formstyle.pointsz, wideCharacterString);
791         if (pElement->u.u_formstyle.set_width)
792             m_free(pElement->u.u_formstyle.set_width, wideCharacterString);
793         if (pElement->u.u_formstyle.color)
794             m_free(pElement->u.u_formstyle.color, wideCharacterString);
795         if (pElement->u.u_formstyle.xlfd)
796             m_free(pElement->u.u_formstyle.xlfd, wideCharacterString);
797         if (pElement->u.u_formstyle.xlfdi)
798             m_free(pElement->u.u_formstyle.xlfdi, wideCharacterString);
799         if (pElement->u.u_formstyle.xlfdb)
800             m_free(pElement->u.u_formstyle.xlfdb, wideCharacterString);
801         if (pElement->u.u_formstyle.xlfdib)
802             m_free(pElement->u.u_formstyle.xlfdib, wideCharacterString);
803         if (pElement->u.u_formstyle.typenam)
804             m_free(pElement->u.u_formstyle.typenam, wideCharacterString);
805         if (pElement->u.u_formstyle.typenami)
806             m_free(pElement->u.u_formstyle.typenami, wideCharacterString);
807         if (pElement->u.u_formstyle.typenamb)
808             m_free(pElement->u.u_formstyle.typenamb, wideCharacterString);
809         if (pElement->u.u_formstyle.typenamib)
810             m_free(pElement->u.u_formstyle.typenamib, wideCharacterString);
811         if (pElement->u.u_formstyle.style)
812             m_free(pElement->u.u_formstyle.style, wideCharacterString);
813         if (pElement->u.u_formstyle.spacing)
814             m_free(pElement->u.u_formstyle.spacing, wideCharacterString);
815         if (pElement->u.u_formstyle.weight)
816             m_free(pElement->u.u_formstyle.weight, wideCharacterString);
817         if (pElement->u.u_formstyle.slant)
818             m_free(pElement->u.u_formstyle.slant, wideCharacterString);
819         if (pElement->u.u_formstyle.special)
820             m_free(pElement->u.u_formstyle.special, wideCharacterString);
821         if (pElement->u.u_formstyle.l_margin)
822             m_free(pElement->u.u_formstyle.l_margin, wideCharacterString);
823         if (pElement->u.u_formstyle.r_margin)
824             m_free(pElement->u.u_formstyle.r_margin, wideCharacterString);
825         if (pElement->u.u_formstyle.t_margin)
826             m_free(pElement->u.u_formstyle.t_margin, wideCharacterString);
827         if (pElement->u.u_formstyle.b_margin)
828             m_free(pElement->u.u_formstyle.b_margin, wideCharacterString);
829         if (pElement->u.u_formstyle.border)
830             m_free(pElement->u.u_formstyle.border, wideCharacterString);
831         if (pElement->u.u_formstyle.vjust)
832             m_free(pElement->u.u_formstyle.vjust, wideCharacterString);
833         break;
834     case e_frmtstyle:
835         if (pElement->u.u_frmtstyle.class)
836             m_free(pElement->u.u_frmtstyle.class, wideCharacterString);
837         if (pElement->u.u_frmtstyle.ssi)
838             m_free(pElement->u.u_frmtstyle.ssi, wideCharacterString);
839         if (pElement->u.u_frmtstyle.rlevel)
840             m_free(pElement->u.u_frmtstyle.rlevel, wideCharacterString);
841         if (pElement->u.u_frmtstyle.phrase)
842             m_free(pElement->u.u_frmtstyle.phrase, wideCharacterString);
843         if (pElement->u.u_frmtstyle.f_margin)
844             m_free(pElement->u.u_frmtstyle.f_margin, wideCharacterString);
845         if (pElement->u.u_frmtstyle.srch_wt)
846             m_free(pElement->u.u_frmtstyle.srch_wt, wideCharacterString);
847         if (pElement->u.u_frmtstyle.pointsz)
848             m_free(pElement->u.u_frmtstyle.pointsz, wideCharacterString);
849         if (pElement->u.u_frmtstyle.set_width)
850             m_free(pElement->u.u_frmtstyle.set_width, wideCharacterString);
851         if (pElement->u.u_frmtstyle.color)
852             m_free(pElement->u.u_frmtstyle.color, wideCharacterString);
853         if (pElement->u.u_frmtstyle.xlfd)
854             m_free(pElement->u.u_frmtstyle.xlfd, wideCharacterString);
855         if (pElement->u.u_frmtstyle.xlfdi)
856             m_free(pElement->u.u_frmtstyle.xlfdi, wideCharacterString);
857         if (pElement->u.u_frmtstyle.xlfdb)
858             m_free(pElement->u.u_frmtstyle.xlfdb, wideCharacterString);
859         if (pElement->u.u_frmtstyle.xlfdib)
860             m_free(pElement->u.u_frmtstyle.xlfdib, wideCharacterString);
861         if (pElement->u.u_frmtstyle.typenam)
862             m_free(pElement->u.u_frmtstyle.typenam, wideCharacterString);
863         if (pElement->u.u_frmtstyle.typenami)
864             m_free(pElement->u.u_frmtstyle.typenami, wideCharacterString);
865         if (pElement->u.u_frmtstyle.typenamb)
866             m_free(pElement->u.u_frmtstyle.typenamb, wideCharacterString);
867         if (pElement->u.u_frmtstyle.typenamib)
868             m_free(pElement->u.u_frmtstyle.typenamib, wideCharacterString);
869         if (pElement->u.u_frmtstyle.style)
870             m_free(pElement->u.u_frmtstyle.style, wideCharacterString);
871         if (pElement->u.u_frmtstyle.spacing)
872             m_free(pElement->u.u_frmtstyle.spacing, wideCharacterString);
873         if (pElement->u.u_frmtstyle.weight)
874             m_free(pElement->u.u_frmtstyle.weight, wideCharacterString);
875         if (pElement->u.u_frmtstyle.slant)
876             m_free(pElement->u.u_frmtstyle.slant, wideCharacterString);
877         if (pElement->u.u_frmtstyle.special)
878             m_free(pElement->u.u_frmtstyle.special, wideCharacterString);
879         if (pElement->u.u_frmtstyle.l_margin)
880             m_free(pElement->u.u_frmtstyle.l_margin, wideCharacterString);
881         if (pElement->u.u_frmtstyle.r_margin)
882             m_free(pElement->u.u_frmtstyle.r_margin, wideCharacterString);
883         if (pElement->u.u_frmtstyle.t_margin)
884             m_free(pElement->u.u_frmtstyle.t_margin, wideCharacterString);
885         if (pElement->u.u_frmtstyle.b_margin)
886             m_free(pElement->u.u_frmtstyle.b_margin, wideCharacterString);
887         if (pElement->u.u_frmtstyle.border)
888             m_free(pElement->u.u_frmtstyle.border, wideCharacterString);
889         if (pElement->u.u_frmtstyle.vjust)
890             m_free(pElement->u.u_frmtstyle.vjust, wideCharacterString);
891         if (pElement->u.u_frmtstyle.justify)
892             m_free(pElement->u.u_frmtstyle.justify, wideCharacterString);
893         break;
894     case e_grphstyle:
895         if (pElement->u.u_grphstyle.class)
896             m_free(pElement->u.u_grphstyle.class, wideCharacterString);
897         if (pElement->u.u_grphstyle.ssi)
898             m_free(pElement->u.u_grphstyle.ssi, wideCharacterString);
899         if (pElement->u.u_grphstyle.rlevel)
900             m_free(pElement->u.u_grphstyle.rlevel, wideCharacterString);
901         if (pElement->u.u_grphstyle.phrase)
902             m_free(pElement->u.u_grphstyle.phrase, wideCharacterString);
903         if (pElement->u.u_grphstyle.l_margin)
904             m_free(pElement->u.u_grphstyle.l_margin, wideCharacterString);
905         if (pElement->u.u_grphstyle.r_margin)
906             m_free(pElement->u.u_grphstyle.r_margin, wideCharacterString);
907         if (pElement->u.u_grphstyle.t_margin)
908             m_free(pElement->u.u_grphstyle.t_margin, wideCharacterString);
909         if (pElement->u.u_grphstyle.b_margin)
910             m_free(pElement->u.u_grphstyle.b_margin, wideCharacterString);
911         if (pElement->u.u_grphstyle.border)
912             m_free(pElement->u.u_grphstyle.border, wideCharacterString);
913         if (pElement->u.u_grphstyle.vjust)
914             m_free(pElement->u.u_grphstyle.vjust, wideCharacterString);
915         if (pElement->u.u_grphstyle.justify)
916             m_free(pElement->u.u_grphstyle.justify, wideCharacterString);
917         break;
918     default:
919         fprintf(stderr,
920                 "internal error - unrecognized element type in DeleteElement");
921         if (m_errfile && (m_errfile != stderr))
922             fprintf(m_errfile,
923                 "internal error - unrecognized element type in DeleteElement");
924         exit(xxx);
925     }
926 mb_free((char **) &pElement);
927 }
928
929
930 static ElementPtr *currentElementStack;
931 static int currentElementStackSize = 0;
932 static int currentElementStackTop = -1;
933
934 void PushCurrentElement()
935 {
936 currentElementStackTop++;
937 if (currentElementStackSize == 0)
938     {
939     currentElementStack =
940         (ElementPtr *) mb_malloc(100 * sizeof(ElementPtr *));
941     currentElementStackSize = 100;
942     }
943 else if (currentElementStackTop == currentElementStackSize)
944     {
945     currentElementStackSize += 100;
946     currentElementStack =
947         (ElementPtr *) mb_realloc((char *) currentElementStack,
948                               currentElementStackSize * sizeof(ElementPtr *));
949     }
950 currentElementStack[currentElementStackTop] = pCurrentElement;
951 }
952
953
954 static ElementPtr *thisElementStack;
955 static int thisElementStackSize = 0;
956 static int thisElementStackTop = -1;
957
958 #if defined(M_PROTO)
959 static void PushThisElement(ElementPtr pThis)
960 #else
961 static void PushThisElement(pThis)
962   ElementPtr pThis;
963 #endif
964 {
965 thisElementStackTop++;
966 if (thisElementStackSize == 0)
967     {
968     thisElementStack =
969         (ElementPtr *) mb_malloc(100 * sizeof(ElementPtr *));
970     thisElementStackSize = 100;
971     }
972 else if (thisElementStackTop == thisElementStackSize)
973     {
974     thisElementStackSize += 100;
975     thisElementStack =
976         (ElementPtr *) mb_realloc((char *) thisElementStack,
977                               thisElementStackSize * sizeof(ElementPtr *));
978     }
979 thisElementStack[thisElementStackTop] = pThis;
980 }
981
982
983 void PopCurrentElement()
984 {
985 pCurrentElement = currentElementStack[currentElementStackTop];
986 if (currentElementStackTop < 0)
987     {
988     fprintf(stderr, "internal error - stack underflow in PopCurrentElement");
989     if (m_errfile && (m_errfile != stderr))
990         fprintf(m_errfile,
991                 "internal error - stack underflow in PopCurrentElement");
992     exit(xxx);
993     }
994 else
995     --currentElementStackTop;
996 }
997
998
999 static ElementPtr PopThisElement()
1000 {
1001 ElementPtr pThisElement;
1002
1003 pThisElement = thisElementStack[thisElementStackTop];
1004 if (thisElementStackTop < 0)
1005     {
1006     fprintf(stderr, "internal error - stack underflow in PopThisElement");
1007     if (m_errfile && (m_errfile != stderr))
1008         fprintf(m_errfile,
1009                 "internal error - stack underflow in PopThisElement");
1010     exit(xxx);
1011     }
1012 else
1013     --thisElementStackTop;
1014 return pThisElement;
1015 }
1016
1017
1018 /*
1019  * A routine to remove occurrences of <BLOCK>\n</BLOCK>, if any, from
1020  * the .sdl file.  These occurrences are an artifact of translation
1021  * and seem to be easier to get rid of here than in the first pass.
1022 */
1023 #if defined(M_PROTO)
1024 static void RemoveSuperfluousBlocks(ElementPtr *ppBlockOrForm)
1025 #else
1026 static void RemoveSuperfluousBlocks(ppBlockOrForm)
1027   ElementPtr *ppBlockOrForm;
1028 #endif
1029 {
1030 ElementPtr  pElement, pFdata;
1031 ElementPtr *ppBack;
1032
1033 if (!(pElement = *ppBlockOrForm))
1034     return;
1035
1036 ppBack = ppBlockOrForm;
1037 while (pElement)
1038     {
1039     if (pElement->type == e_form)
1040         {
1041         if (pFdata = pElement->u.u_form.pFdata)
1042             RemoveSuperfluousBlocks(&(pFdata->u.u_fdata.pBlockOrForm));
1043         }
1044     else if (!pElement->u.u_block.level    &&
1045              !pElement->u.u_block.version  &&
1046              !pElement->u.u_block.class    &&
1047              !pElement->u.u_block.language &&
1048              !pElement->u.u_block.charset  &&
1049              !pElement->u.u_block.length   &&
1050              !pElement->u.u_block.app      &&
1051              !pElement->u.u_block.timing   &&
1052              !pElement->u.u_block.ssi      &&
1053              !pElement->u.u_block.pHead    &&
1054              !pElement->u.u_block.pCpOrP)
1055         {
1056         *ppBack = pElement->pNext;
1057         DeleteElement(pElement);
1058         pElement = *ppBack;
1059         continue;
1060         }
1061     ppBack   = &(pElement->pNext);
1062     pElement = pElement->pNext;
1063     }
1064 }
1065
1066
1067 #if defined(M_PROTO)
1068 static void ReplaceFormWithBlock(ElementPtr *ppBlockOrForm)
1069 #else
1070 static void ReplaceFormWithBlock(ppBlockOrForm)
1071   ElementPtr ppBlockOrForm;
1072 #endif
1073 {
1074 ElementPtr  pElement, pFstyle, pFrowvec, pFdata, pSubElement;
1075 ElementPtr *ppBack;
1076 M_WCHAR    *pId, *wc_sdlReservedName, *wc_quote1;
1077
1078 if (!(pElement = *ppBlockOrForm))
1079     return;
1080
1081 wc_sdlReservedName = MakeWideCharString(sdlReservedName);
1082 wc_quote1          = MakeWideCharString("\"1\"");
1083
1084 ppBack = ppBlockOrForm;
1085 while (pElement)
1086     {
1087     if (pElement->type != e_form)
1088         {
1089         ppBack   = &(pElement->pNext);
1090         pElement = pElement->pNext;
1091         continue;
1092         }
1093     if (!(pFdata = pElement->u.u_form.pFdata))
1094         { /* illegal <form> */
1095         ppBack   = &(pElement->pNext);
1096         pElement = pElement->pNext;
1097         continue;
1098         }
1099     ReplaceFormWithBlock(&(pFdata->u.u_fdata.pBlockOrForm));
1100     if ((pFstyle  = pElement->u.u_form.pFstyle)   &&
1101         (pFrowvec = pFstyle->u.u_fstyle.pFrowvec) &&
1102         !pFrowvec->pNext                          &&
1103         (!pFstyle->u.u_fstyle.ncols || !w_strcmp(pFstyle->u.u_fstyle.ncols,
1104                                                  wc_quote1)))
1105         { /* found a <form> containing only one element */
1106         if (((pSubElement = pFdata->u.u_fdata.pBlockOrForm)->type == e_form))
1107             {
1108             if (!pSubElement->u.u_form.level    &&
1109                 !pSubElement->u.u_form.version  &&
1110                 !pSubElement->u.u_form.class    &&
1111                 !pSubElement->u.u_form.language &&
1112                 !pSubElement->u.u_form.charset  &&
1113                 !pSubElement->u.u_form.length   &&
1114                 !pSubElement->u.u_form.app      &&
1115                 !pSubElement->u.u_form.ssi)
1116                 {
1117                 if ((pId = pSubElement->u.u_form.id) &&
1118                     w_strncmp(pId, wc_sdlReservedName, SDLNAMESIZ-1))
1119                     { /* user supplied id */
1120                     ppBack   = &(pElement->pNext);
1121                     pElement = pElement->pNext;
1122                     continue;
1123                     }
1124                 if (pElement->u.u_form.pHead)
1125                     {
1126                     if (pSubElement->u.u_form.pHead)
1127                         {
1128                         ppBack   = &(pElement->pNext);
1129                         pElement = pElement->pNext;
1130                         continue;
1131                         }
1132                     else
1133                         {
1134                         pSubElement->u.u_form.pHead =
1135                                                 pElement->u.u_form.pHead;
1136                         pElement->u.u_form.pHead = NULL;
1137                         }
1138                     }
1139                 if (pId)
1140                     m_free(pId, wideCharacterString);
1141                 *ppBack = pSubElement;
1142                 pSubElement->pNext             = pElement->pNext;
1143                 pSubElement->u.u_form.id       = pElement->u.u_form.id;
1144                 pSubElement->u.u_form.level    = pElement->u.u_form.level;
1145                 pSubElement->u.u_form.version  = pElement->u.u_form.version;
1146                 pSubElement->u.u_form.class    = pElement->u.u_form.class;
1147                 pSubElement->u.u_form.language = pElement->u.u_form.language;
1148                 pSubElement->u.u_form.charset  = pElement->u.u_form.charset;
1149                 pSubElement->u.u_form.length   = pElement->u.u_form.length;
1150                 pSubElement->u.u_form.app      = pElement->u.u_form.app;
1151                 pSubElement->u.u_form.ssi      = pElement->u.u_form.ssi;
1152                 pElement->u.u_form.id          = NULL;
1153                 pElement->u.u_form.level       = NULL;
1154                 pElement->u.u_form.version     = NULL;
1155                 pElement->u.u_form.class       = NULL;
1156                 pElement->u.u_form.language    = NULL;
1157                 pElement->u.u_form.charset     = NULL;
1158                 pElement->u.u_form.length      = NULL;
1159                 pElement->u.u_form.app         = NULL;
1160                 pElement->u.u_form.ssi         = NULL;
1161                 DeleteElement(pFstyle);
1162                 DeleteElement(pFrowvec);
1163                 DeleteElement(pFdata);
1164                 }
1165             else
1166                 {
1167                 ppBack   = &(pElement->pNext);
1168                 pElement = pElement->pNext;
1169                 continue;
1170                 }
1171             }
1172         else
1173             {
1174             if (!pSubElement->u.u_block.level    &&
1175                 !pSubElement->u.u_block.version  &&
1176                 !pSubElement->u.u_block.class    &&
1177                 !pSubElement->u.u_block.language &&
1178                 !pSubElement->u.u_block.charset  &&
1179                 !pSubElement->u.u_block.length   &&
1180                 !pSubElement->u.u_block.app      &&
1181                 !pSubElement->u.u_block.timing   &&
1182                 !pSubElement->u.u_block.ssi)
1183                 {
1184                 if ((pId = pSubElement->u.u_block.id) &&
1185                     w_strncmp(pId, wc_sdlReservedName, SDLNAMESIZ-1))
1186                     { /* user supplied id */
1187                     ppBack   = &(pElement->pNext);
1188                     pElement = pElement->pNext;
1189                     continue;
1190                     }
1191                 if (pElement->u.u_form.pHead)
1192                     {
1193                     if (pSubElement->u.u_block.pHead)
1194                         {
1195                         ppBack   = &(pElement->pNext);
1196                         pElement = pElement->pNext;
1197                         continue;
1198                         }
1199                     else
1200                         {
1201                         pSubElement->u.u_block.pHead =
1202                                                 pElement->u.u_form.pHead;
1203                         pElement->u.u_form.pHead = NULL;
1204                         }
1205                     }
1206                 if (pId)
1207                     m_free(pId, wideCharacterString);
1208                 *ppBack = pSubElement;
1209                 pSubElement->pNext              = pElement->pNext;
1210                 pSubElement->u.u_block.id       = pElement->u.u_form.id;
1211                 pSubElement->u.u_block.level    = pElement->u.u_form.level;
1212                 pSubElement->u.u_block.version  = pElement->u.u_form.version;
1213                 pSubElement->u.u_block.class    = pElement->u.u_form.class;
1214                 pSubElement->u.u_block.language = pElement->u.u_form.language;
1215                 pSubElement->u.u_block.charset  = pElement->u.u_form.charset;
1216                 pSubElement->u.u_block.length   = pElement->u.u_form.length;
1217                 pSubElement->u.u_block.app      = pElement->u.u_form.app;
1218                 pSubElement->u.u_block.timing   = NULL;
1219                 pSubElement->u.u_block.ssi      = pElement->u.u_form.ssi;
1220                 pElement->u.u_form.id           = NULL;
1221                 pElement->u.u_form.level        = NULL;
1222                 pElement->u.u_form.version      = NULL;
1223                 pElement->u.u_form.class        = NULL;
1224                 pElement->u.u_form.language     = NULL;
1225                 pElement->u.u_form.charset      = NULL;
1226                 pElement->u.u_form.length       = NULL;
1227                 pElement->u.u_form.app          = NULL;
1228                 pElement->u.u_form.ssi          = NULL;
1229                 DeleteElement(pFstyle);
1230                 DeleteElement(pFrowvec);
1231                 DeleteElement(pFdata);
1232                 }
1233             else
1234                 {
1235                 ppBack   = &(pElement->pNext);
1236                 pElement = pElement->pNext;
1237                 continue;
1238                 }
1239             }
1240         ppBack      = &(pSubElement->pNext);
1241         pSubElement = pElement->pNext;
1242         *ppBack     = pSubElement;
1243         DeleteElement(pElement);
1244         pElement = pSubElement;
1245         continue;
1246         }
1247     ppBack   = &(pElement->pNext);
1248     pElement = pElement->pNext;
1249     continue;
1250     }
1251 m_free(wc_sdlReservedName, wideCharacterString);
1252 m_free(wc_quote1, wideCharacterString);
1253 }
1254
1255
1256 #if defined(M_PROTO)
1257 static void OptimizeSDL(ElementPtr *ppBlockOrForm)
1258 #else
1259 static void OptimizeSDL(ppBlockOrForm)
1260   ElementPtr ppBlockOrForm;
1261 #endif
1262 {
1263 ReplaceFormWithBlock(ppBlockOrForm);
1264 RemoveSuperfluousBlocks(ppBlockOrForm);
1265 }
1266
1267
1268 /* build a linked list of the LOIDS, we'll need to postprocess it to
1269  * update the offsets when adding the INDEX and LOIDS; also when
1270  * compressing the VSTRUCT
1271 */
1272 #if defined(M_PROTO)
1273 static void AddToLOIDS(char     *pElementName,
1274                        M_WCHAR  *pwcId,
1275                        M_WCHAR  *pwcSSI,
1276                        M_WCHAR  *pwcLevel,
1277                        long int  virpageOffset)
1278 #else
1279 static void AddToLOIDS(pElementName, pwcId, pwcSSI, pwcLevel, virpageOffset)
1280 char     *pElementName;
1281 M_WCHAR  *pwcId;
1282 M_WCHAR  *pwcSSI;
1283 M_WCHAR  *pwcLevel;
1284 long int  virpageOffset;
1285 #endif
1286 {
1287 LoidsPtr pNewId;
1288
1289 if (!pwcId) return;
1290
1291 nIds++;
1292
1293 virpageOffset += vstructSize;
1294
1295 pNewId         = (LoidsPtr) mb_malloc(sizeof(LoidsRec));
1296
1297 pNewId->type   = mb_malloc(strlen(pElementName)+1);
1298 strcpy(pNewId->type, pElementName);
1299 pNewId->rid    = MakeMByteString(pwcId);
1300 if (pwcSSI)
1301     pNewId->rssi   = MakeMByteString(pwcSSI);
1302 else
1303     pNewId->rssi   = NULL;
1304 if (pwcLevel)
1305     pNewId->rlevel = MakeMByteString(pwcLevel);
1306 else
1307     pNewId->rlevel = NULL;
1308 pNewId->offset     = virpageOffset;
1309 pNewId->next       = NULL;
1310
1311 pLoidsEnd->next = pNewId;
1312 pLoidsEnd = pNewId;
1313 }
1314
1315
1316 /* compress or decompress a file; return the size after (de)compression
1317 */
1318 #if defined(M_PROTO)
1319 static int Compress(char *fileName, char *zFileName, LOGICAL doCompress)
1320 #else
1321 int Compress(fileName, zFileName, doCompress)
1322   char    *fileName;
1323   char    *zFileName;
1324   LOGICAL  doCompress;
1325 #endif
1326 {
1327        char cmd[BUFSIZ];
1328        int  errStatus;
1329        int  errCode;
1330 static char sysStr[] = "system()";
1331
1332 if (doCompress)
1333     sprintf(cmd, "compress -f < %s > %s", fileName, zFileName);
1334 else
1335     sprintf(cmd, "compress -d < %s > %s", zFileName, fileName);
1336 errStatus = system(cmd);
1337 if (errStatus)
1338     {
1339     fprintf(stderr, "%s: error executing \"%s\"\n", progName, cmd);
1340     if (m_errfile && (m_errfile != stderr))
1341         fprintf(m_errfile, "%s: error executing \"%s\"\n", progName, cmd);
1342     if (errStatus == -1)
1343         {
1344         perror(sysStr);
1345         if (m_errfile && (m_errfile != stderr))
1346             fprintf(m_errfile, "%s: %s", sysStr, strerror(errno));
1347         }
1348     else
1349         {
1350         if (WIFEXITED(errStatus))
1351             {
1352             if ((errCode = WEXITSTATUS(errStatus)) == 127)
1353                 {
1354                 fprintf(stderr,
1355                         "%s: %s\n",
1356                         sysStr,
1357                         "error executing command language interpreter");
1358                 if (m_errfile && (m_errfile != stderr))
1359                     fprintf(m_errfile,
1360                             "%s: %s\n",
1361                             sysStr,
1362                             "error executing command language interpreter");
1363                 }
1364             else
1365                 {
1366                 fprintf(stderr, "%s: exited with status %d\n", cmd, errCode);
1367                 if (m_errfile && (m_errfile != stderr))
1368                     fprintf(m_errfile,
1369                             "%s: exited with status %d\n",
1370                             cmd,
1371                             errCode);
1372                 }
1373             }
1374         else if (WIFSIGNALED(errStatus))
1375             {
1376             errCode = WTERMSIG(errStatus);
1377             fprintf(stderr, "%s: exited due to signal %d\n", cmd, errCode);
1378             if (m_errfile && (m_errfile != stderr))
1379                 fprintf(m_errfile,
1380                         "%s: exited due to signal %d\n",
1381                         cmd,
1382                         errCode);
1383             if (WCOREDUMP(errStatus))
1384                 {
1385                 fputs("core dumped\n", stderr);
1386                 if (m_errfile && (m_errfile != stderr))
1387                     fputs("core dumped\n", m_errfile);
1388                 }
1389             }
1390         else if (WIFSTOPPED(errStatus))
1391             {
1392             errCode = WSTOPSIG(errStatus);
1393             fprintf(stderr, "%s: stopped due to signal %d\n", cmd, errCode);
1394             if (m_errfile && (m_errfile != stderr))
1395                 fprintf(m_errfile,
1396                         "%s: stopped due to signal %d\n",
1397                         cmd,
1398                         errCode);
1399             }
1400         }
1401     exit(1);
1402     }
1403 if (doCompress)
1404     return FileSize(zFileName, xxx);
1405 else
1406     return FileSize(fileName, xxx);
1407 }
1408
1409
1410 /* decompress an SDL file */
1411 static void DecompressSDL()
1412 {
1413 int           outFd, zFd;
1414 FILE         *inFile;
1415 int           length, delta;
1416 unsigned char buffer[BUFSIZ];
1417
1418 inFile = FopenFile(inFileName, O_RDONLY, xxx);
1419 outFd  = OpenFile(tempFileName, O_WRONLY, xxx);
1420
1421 length = FreadFile(inFile, inFileName, (char *) buffer, 4, xxx);
1422 if (length < 4)
1423     {
1424     fprintf(stderr, "%s: premature end of %s\n", progName, inFileName);
1425     if (m_errfile && (m_errfile != stderr))
1426         {
1427         fprintf(m_errfile,
1428                 "%s: premature end of %s\n",
1429                 progName,
1430                 inFileName);
1431         }
1432     exit(xxx);
1433     }
1434
1435 if (*buffer) /* file isn't compressed */
1436     {
1437     length += FreadFile(inFile, inFileName, (char *) (buffer+4), BUFSIZ-4, xxx);
1438     do  {
1439         WriteFile(outFd, tempFileName, (char *) buffer, length, xxx);
1440         length = FreadFile(inFile, inFileName, (char *) buffer, BUFSIZ, xxx);
1441         }
1442     while (length > 0);
1443     FcloseFile(inFile, inFileName, xxx);
1444     CloseFile(outFd, tempFileName, xxx);
1445     return;
1446     }
1447
1448 do  {
1449     delta  = buffer[1];
1450     delta *= 256;
1451     delta += buffer[2];
1452     delta *= 256;
1453     delta += buffer[3];
1454
1455     unlink(compZFileName);
1456     zFd = OpenFile(compZFileName, O_WRONLY, xxx);
1457     while (length = FreadFile(inFile,
1458                               inFileName,
1459                               (char *) buffer,
1460                               MIN(delta, BUFSIZ),
1461                               xxx))
1462         {
1463         WriteFile(zFd, compZFileName, (char *) buffer, length, xxx);
1464         delta -= length;
1465         }
1466     if (delta != 0)
1467         {
1468         fprintf(stderr, "%s: premature end of %s\n", progName, inFileName);
1469         if (m_errfile && (m_errfile != stderr))
1470             {
1471             fprintf(m_errfile,
1472                     "%s: premature end of %s\n",
1473                     progName,
1474                     inFileName);
1475             }
1476         exit(xxx);
1477         }
1478     CloseFile(zFd, compZFileName, xxx);
1479     delta = Compress(compFileName, compZFileName, FALSE);
1480     zFd = OpenFile(compFileName, O_RDONLY, xxx);
1481     while (length = ReadFile(zFd,
1482                              compFileName,
1483                              (char *) buffer,
1484                              MIN(delta, BUFSIZ),
1485                              xxx))
1486         {
1487         WriteFile(outFd, tempFileName, (char *) buffer, length, xxx);
1488         delta -= length;
1489         }
1490     if (delta != 0)
1491         {
1492         fprintf(stderr, "%s: premature end of %s\n", progName, compFileName);
1493         if (m_errfile && (m_errfile != stderr))
1494             {
1495             fprintf(m_errfile,
1496                     "%s: premature end of %s\n",
1497                     progName,
1498                     compFileName);
1499             }
1500         exit(xxx);
1501         }
1502     CloseFile(zFd, compFileName, xxx);
1503     while ((delta = FreadFile(inFile,
1504                              inFileName,
1505                              (char *) buffer,
1506                              1,
1507                              xxx)) && (*buffer == 0xff));
1508     if (delta)
1509         {
1510         if (*buffer == '<') /* we're at the end, "</SDLDOC>\n" */
1511             {
1512             delta += FreadFile(inFile,
1513                                inFileName,
1514                                (char *) (buffer + 1),
1515                                9,
1516                                xxx);
1517             if (delta != 10)
1518                 delta = 1; /* force a premature end of document */
1519             else
1520                 {
1521                 WriteFile(outFd, tempFileName, (char *) buffer, delta, xxx);
1522                 delta = 0;
1523                 }
1524             }
1525         else
1526             delta += FreadFile(inFile,
1527                                inFileName,
1528                                (char *) (buffer + 1),
1529                                3,
1530                                xxx);
1531         }
1532     }
1533 while (delta >= 4);
1534 if (delta != 0)
1535     {
1536     fprintf(stderr, "%s: premature end of %s\n", progName, inFileName);
1537     if (m_errfile && (m_errfile != stderr))
1538         {
1539         fprintf(m_errfile,
1540                 "%s: premature end of %s\n",
1541                 progName,
1542                 inFileName);
1543         }
1544     exit(xxx);
1545     }
1546 FcloseFile(inFile, inFileName, xxx);
1547 CloseFile(outFd, tempFileName, xxx);
1548 unlink(compFileName);
1549 unlink(compZFileName);
1550 }
1551
1552
1553 /* A function too lookup a TOSS element and mark it as used if the
1554  * lookup succeeds.
1555 */
1556 #if defined(M_PROTO)
1557 static void MarkUsedStyle(M_WCHAR *level, M_WCHAR *class, M_WCHAR *ssi)
1558 #else
1559 static void MarkUsedStyle(level, class, ssi)
1560   M_WCHAR *level;
1561   M_WCHAR *class;
1562   M_WCHAR *ssi;
1563 #endif
1564 {
1565 ElementPtr  pThis;
1566 M_WCHAR    *rlevel = NULL, *rclass = NULL, *rssi = NULL;
1567 LOGICAL    *pBeenUsed;
1568
1569 pThis = pTossChain;
1570 while (pThis)
1571     {
1572     switch (pThis->type)
1573         {
1574         case e_keystyle:
1575             rlevel    = pThis->u.u_keystyle.rlevel;
1576             rclass    = pThis->u.u_keystyle.class;
1577             rssi      = pThis->u.u_keystyle.ssi;
1578             pBeenUsed = &pThis->u.u_keystyle.beenUsed;
1579             break;
1580         case e_headstyle:
1581             rlevel    = pThis->u.u_headstyle.rlevel;
1582             rclass    = pThis->u.u_headstyle.class;
1583             rssi      = pThis->u.u_headstyle.ssi;
1584             pBeenUsed = &pThis->u.u_headstyle.beenUsed;
1585             break;
1586         case e_formstyle:
1587             rlevel    = pThis->u.u_formstyle.rlevel;
1588             rclass    = pThis->u.u_formstyle.class;
1589             rssi      = pThis->u.u_formstyle.ssi;
1590             pBeenUsed = &pThis->u.u_formstyle.beenUsed;
1591             break;
1592         case e_frmtstyle:
1593             rlevel    = pThis->u.u_frmtstyle.rlevel;
1594             rclass    = pThis->u.u_frmtstyle.class;
1595             rssi      = pThis->u.u_frmtstyle.ssi;
1596             pBeenUsed = &pThis->u.u_frmtstyle.beenUsed;
1597             break;
1598         case e_grphstyle:
1599             rlevel    = pThis->u.u_grphstyle.rlevel;
1600             rclass    = pThis->u.u_grphstyle.class;
1601             rssi      = pThis->u.u_grphstyle.ssi;
1602             pBeenUsed = &pThis->u.u_grphstyle.beenUsed;
1603             break;
1604         }
1605     if ((!rlevel || (w_strcmp(rlevel, level) == 0)) &&
1606         (!rssi   || (w_strcmp(rssi, ssi) == 0))     &&
1607         (w_strcmp(rclass, class) == 0))
1608             {
1609             if (*pBeenUsed == FALSE)
1610                 {
1611                 nStyles++;
1612                 *pBeenUsed = TRUE;
1613                 pThis->beenEmitted = FALSE;
1614                 }
1615             return;
1616             }
1617     pThis = pThis->pNext;
1618     }
1619 }
1620
1621
1622 /* Look for "rid" in the "pSnb" list.  Return the element if found,
1623  * NULL otherwise.
1624 */
1625 #if defined(M_PROTO)
1626 static ElementPtr LookupInSnb(ElementPtr pSnb, M_WCHAR *rid)
1627 #else
1628 static ElementPtr LookupInSnb(pSnb, rid)
1629   ElementPtr pSnb;
1630   M_WCHAR *rid;
1631 #endif
1632 {
1633 ElementPtr   pThis;
1634 M_WCHAR    **pId;
1635 static char errMess[] =
1636             "internal error - unrecognized element type in LookupInSnb";
1637
1638 if (!pSnb) return NULL;
1639
1640 pThis = pSnb->u.u_snb.pSystemNotations;
1641
1642 while (pThis)
1643     {
1644     switch (pThis->type)
1645         {
1646         case e_graphic:
1647             pId = &pThis->u.u_graphic.id;
1648             break;
1649         case e_text:
1650             pId = &pThis->u.u_text.id;
1651             break;
1652         case e_audio:
1653             pId = &pThis->u.u_audio.id;
1654             break;
1655         case e_video:
1656             pId = &pThis->u.u_video.id;
1657             break;
1658         case e_animate:
1659             pId = &pThis->u.u_animate.id;
1660             break;
1661         case e_script:
1662             pId = &pThis->u.u_script.id;
1663             break;
1664         case e_crossdoc:
1665             pId = &pThis->u.u_crossdoc.id;
1666             break;
1667         case e_man_page:
1668             pId = &pThis->u.u_man_page.id;
1669             break;
1670         case e_textfile:
1671             pId = &pThis->u.u_textfile.id;
1672             break;
1673         case e_sys_cmd:
1674             pId = &pThis->u.u_sys_cmd.id;
1675             break;
1676         case e_callback:
1677             pId = &pThis->u.u_callback.id;
1678             break;
1679         case e_switch:
1680             pId = &pThis->u.u_switch.id;
1681             break;
1682         default:
1683           fprintf(stderr, "%s\n", errMess);
1684             if (m_errfile && (m_errfile != stderr))
1685                 fprintf(m_errfile, "%s\n", errMess);
1686             exit(xxx);
1687         }
1688     if (w_strcmp(rid, *pId) == 0) return pThis;
1689     pThis = pThis->pNext;
1690     }
1691 return NULL;
1692 }
1693
1694
1695 /* Clear the tossChecked field in the nodes of a sub-tree starting at
1696  * (and including) the element passed in. The assumption is that all
1697  * nodes in the sub-tree are either marked or all clear.  Once a clear
1698  * node is found, the tree is not descended further.
1699 */
1700 #if defined(M_PROTO)
1701 static void ClearTree(ElementPtr pRoot)
1702 #else
1703 static void ClearTree(pRoot)
1704   ElementPtr pRoot;
1705 #endif
1706 {
1707 ElementPtr  pThis;
1708 static char errMess[] =
1709             "internal error - unrecognized element type in ClearTree";
1710
1711 pThis = pRoot;
1712
1713 while (pThis)
1714     {
1715     switch (pThis->type)
1716         {
1717         case e_sdldoc:
1718             pThis->tossChecked = FALSE;
1719             if (pThis->u.u_sdldoc.pHead &&
1720                 pThis->u.u_sdldoc.pHead->tossChecked)
1721                 {
1722                 PushThisElement(pThis);
1723                 pThis = pThis->u.u_sdldoc.pHead;
1724                 break;
1725                 }
1726             if (pThis->u.u_sdldoc.pSnb &&
1727                 pThis->u.u_sdldoc.pSnb->tossChecked)
1728                 {
1729                 PushThisElement(pThis);
1730                 pThis = pThis->u.u_sdldoc.pSnb;
1731                 break;
1732                 }
1733             pThis = NULL;
1734             break;
1735         case e_virpage:
1736             pThis->tossChecked = FALSE;
1737             if (pThis->u.u_virpage.pHead &&
1738                 pThis->u.u_virpage.pHead->tossChecked)
1739                 {
1740                 PushThisElement(pThis);
1741                 pThis = pThis->u.u_virpage.pHead;
1742                 break;
1743                 }
1744             if (pThis->u.u_virpage.pSnb &&
1745                 pThis->u.u_virpage.pSnb->tossChecked)
1746                 {
1747                 PushThisElement(pThis);
1748                 pThis = pThis->u.u_virpage.pSnb;
1749                 break;
1750                 }
1751             if (pThis->u.u_virpage.pBlockOrForm &&
1752                 pThis->u.u_virpage.pBlockOrForm->tossChecked)
1753                 {
1754                 PushThisElement(pThis);
1755                 pThis = pThis->u.u_virpage.pBlockOrForm;
1756                 break;
1757                 }
1758             if (pThis->pNext)
1759                 {
1760                 pThis = pThis->pNext;
1761                 break;
1762                 }
1763             pThis = NULL;
1764             break;
1765         case e_snb:
1766             pThis->tossChecked = FALSE;
1767             if (pThis->u.u_snb.pHead &&
1768                 pThis->u.u_snb.pHead->tossChecked)
1769                 {
1770                 PushThisElement(pThis);
1771                 pThis = pThis->u.u_virpage.pHead;
1772                 break;
1773                 }
1774             if (pThis->u.u_snb.pSystemNotations)
1775                 {
1776                 PushThisElement(pThis);
1777                 pThis = pThis->u.u_snb.pSystemNotations;
1778                 break;
1779                 }
1780             if (pThis->pNext)
1781                 {
1782                 pThis = pThis->pNext;
1783                 break;
1784                 }
1785             pThis = PopThisElement();
1786             break;
1787         case e_block:
1788             pThis->tossChecked = FALSE;
1789             if (pThis->u.u_block.pHead &&
1790                 pThis->u.u_block.pHead->tossChecked)
1791                 {
1792                 PushThisElement(pThis);
1793                 pThis = pThis->u.u_block.pHead;
1794                 break;
1795                 }
1796             if (pThis->u.u_block.pCpOrP &&
1797                 pThis->u.u_block.pCpOrP->tossChecked)
1798                 {
1799                 PushThisElement(pThis);
1800                 pThis = pThis->u.u_block.pCpOrP;
1801                 break;
1802                 }
1803             if (pThis->pNext)
1804                 {
1805                 pThis = pThis->pNext;
1806                 break;
1807                 }
1808             pThis = PopThisElement();
1809             break;
1810         case e_form:
1811             pThis->tossChecked = FALSE;
1812             if (pThis->u.u_form.pHead &&
1813                 pThis->u.u_form.pHead->tossChecked)
1814                 {
1815                 PushThisElement(pThis);
1816                 pThis = pThis->u.u_form.pHead;
1817                 break;
1818                 }
1819             if (pThis->u.u_form.pFdata &&
1820                 pThis->u.u_form.pFdata->tossChecked)
1821                 {
1822                 PushThisElement(pThis);
1823                 pThis = pThis->u.u_form.pFdata;
1824                 break;
1825                 }
1826             if (pThis->u.u_form.pFstyle &&
1827                 pThis->u.u_form.pFstyle->tossChecked)
1828                 {
1829                 PushThisElement(pThis);
1830                 pThis = pThis->u.u_form.pFstyle;
1831                 break;
1832                 }
1833             if (pThis->pNext)
1834                 {
1835                 pThis = pThis->pNext;
1836                 break;
1837                 }
1838             pThis = PopThisElement();
1839             break;
1840         case e_fstyle:
1841             pThis->tossChecked = FALSE;
1842             if (pThis->u.u_fstyle.pFrowvec &&
1843                 pThis->u.u_fstyle.pFrowvec->tossChecked)
1844                 {
1845                 PushThisElement(pThis);
1846                 pThis = pThis->u.u_fstyle.pFrowvec;
1847                 break;
1848                 }
1849             if (pThis->pNext)
1850                 {
1851                 pThis = pThis->pNext;
1852                 break;
1853                 }
1854             pThis = PopThisElement();
1855             break;
1856         case e_frowvec:
1857             pThis->tossChecked = FALSE;
1858             if (pThis->pNext)
1859                 {
1860                 pThis = pThis->pNext;
1861                 break;
1862                 }
1863             pThis = PopThisElement();
1864             break;
1865         case e_fdata:
1866             pThis->tossChecked = FALSE;
1867             if (pThis->u.u_fdata.pBlockOrForm &&
1868                 pThis->u.u_fdata.pBlockOrForm->tossChecked)
1869                 {
1870                 PushThisElement(pThis);
1871                 pThis = pThis->u.u_fdata.pBlockOrForm;
1872                 break;
1873                 }
1874             if (pThis->pNext)
1875                 {
1876                 pThis = pThis->pNext;
1877                 break;
1878                 }
1879             pThis = PopThisElement();
1880             break;
1881         case e_p:
1882             pThis->tossChecked = FALSE;
1883             if (pThis->u.u_p.pHead &&
1884                 pThis->u.u_p.pHead->tossChecked)
1885                 {
1886                 PushThisElement(pThis);
1887                 pThis = pThis->u.u_p.pHead;
1888                 break;
1889                 }
1890             if (pThis->u.u_p.pAtomicOrPCDATA &&
1891                 pThis->u.u_p.pAtomicOrPCDATA->tossChecked)
1892                 {
1893                 PushThisElement(pThis);
1894                 pThis = pThis->u.u_p.pAtomicOrPCDATA;
1895                 break;
1896                 }
1897             if (pThis->pNext)
1898                 {
1899                 pThis = pThis->pNext;
1900                 break;
1901                 }
1902             pThis = PopThisElement();
1903             break;
1904         case e_cp:
1905             pThis->tossChecked = FALSE;
1906             if (pThis->u.u_cp.pCDATA &&
1907                 pThis->u.u_cp.pCDATA->tossChecked)
1908                 {
1909                 PushThisElement(pThis);
1910                 pThis = pThis->u.u_cp.pCDATA;
1911                 break;
1912                 }
1913             if (pThis->pNext)
1914                 {
1915                 pThis = pThis->pNext;
1916                 break;
1917                 }
1918             pThis = PopThisElement();
1919             break;
1920         case e_head:
1921             pThis->tossChecked = FALSE;
1922             if (pThis->u.u_head.pAtomicOrPCDATA &&
1923                 pThis->u.u_head.pAtomicOrPCDATA->tossChecked)
1924                 {
1925                 PushThisElement(pThis);
1926                 pThis = pThis->u.u_head.pAtomicOrPCDATA;
1927                 break;
1928                 }
1929             if (pThis->u.u_head.pSubhead &&
1930                 pThis->u.u_head.pSubhead->tossChecked)
1931                 {
1932                 PushThisElement(pThis);
1933                 pThis = pThis->u.u_head.pSubhead;
1934                 break;
1935                 }
1936             if (pThis->pNext)
1937                 {
1938                 pThis = pThis->pNext;
1939                 break;
1940                 }
1941             pThis = PopThisElement();
1942             break;
1943         case e_subhead:
1944             pThis->tossChecked = FALSE;
1945             if (pThis->u.u_subhead.pAtomicOrPCDATA &&
1946                 pThis->u.u_subhead.pAtomicOrPCDATA->tossChecked)
1947                 {
1948                 PushThisElement(pThis);
1949                 pThis = pThis->u.u_subhead.pAtomicOrPCDATA;
1950                 break;
1951                 }
1952             if (pThis->pNext)
1953                 {
1954                 pThis = pThis->pNext;
1955                 break;
1956                 }
1957             pThis = PopThisElement();
1958             break;
1959         case e_anchor:
1960             pThis->tossChecked = FALSE;
1961             if (pThis->pNext)
1962                 {
1963                 pThis = pThis->pNext;
1964                 break;
1965                 }
1966             pThis = PopThisElement();
1967             break;
1968         case e_link:
1969             pThis->tossChecked = FALSE;
1970             if (pThis->u.u_link.pAtomicOrPCDATA &&
1971                 pThis->u.u_link.pAtomicOrPCDATA->tossChecked)
1972                 {
1973                 PushThisElement(pThis);
1974                 pThis = pThis->u.u_link.pAtomicOrPCDATA;
1975                 break;
1976                 }
1977             if (pThis->pNext)
1978                 {
1979                 pThis = pThis->pNext;
1980                 break;
1981                 }
1982             pThis = PopThisElement();
1983             break;
1984         case e_snref:
1985             pThis->tossChecked = FALSE;
1986             if (pThis->u.u_snref.pRefitem &&
1987                 pThis->u.u_snref.pRefitem->tossChecked)
1988                 {
1989                 PushThisElement(pThis);
1990                 pThis = pThis->u.u_snref.pRefitem;
1991                 break;
1992                 }
1993             if (pThis->u.u_snref.pAlttext &&
1994                 pThis->u.u_snref.pAlttext->tossChecked)
1995                 {
1996                 PushThisElement(pThis);
1997                 pThis = pThis->u.u_snref.pAlttext;
1998                 break;
1999                 }
2000             if (pThis->pNext)
2001                 {
2002                 pThis = pThis->pNext;
2003                 break;
2004                 }
2005             pThis = PopThisElement();
2006             break;
2007         case e_refitem:
2008             pThis->tossChecked = FALSE;
2009             pThis->u.u_refitem.pReferencedItem = NULL;
2010             if (pThis->u.u_refitem.pHead &&
2011                 pThis->u.u_refitem.pHead->tossChecked)
2012                 {
2013                 PushThisElement(pThis);
2014                 pThis = pThis->u.u_refitem.pHead;
2015                 break;
2016                 }
2017             if (pThis->pNext)
2018                 {
2019                 pThis = pThis->pNext;
2020                 break;
2021                 }
2022             pThis = PopThisElement();
2023             break;
2024         case e_alttext:
2025             pThis->tossChecked = FALSE;
2026             if (pThis->u.u_alttext.pCDATA &&
2027                 pThis->u.u_alttext.pCDATA->tossChecked)
2028                 {
2029                 PushThisElement(pThis);
2030                 pThis = pThis->u.u_alttext.pCDATA;
2031                 break;
2032                 }
2033             if (pThis->pNext)
2034                 {
2035                 pThis = pThis->pNext;
2036                 break;
2037                 }
2038             pThis = PopThisElement();
2039             break;
2040         case e_rev:
2041             pThis->tossChecked = FALSE;
2042             if (pThis->u.u_rev.pAtomicOrPCDATA &&
2043                 pThis->u.u_rev.pAtomicOrPCDATA->tossChecked)
2044                 {
2045                 PushThisElement(pThis);
2046                 pThis = pThis->u.u_rev.pAtomicOrPCDATA;
2047                 break;
2048                 }
2049             if (pThis->pNext)
2050                 {
2051                 pThis = pThis->pNext;
2052                 break;
2053                 }
2054             pThis = PopThisElement();
2055             break;
2056         case e_key:
2057             pThis->tossChecked = FALSE;
2058             if (pThis->u.u_key.pAtomicOrPCDATA &&
2059                 pThis->u.u_key.pAtomicOrPCDATA->tossChecked)
2060                 {
2061                 PushThisElement(pThis);
2062                 pThis = pThis->u.u_key.pAtomicOrPCDATA;
2063                 break;
2064                 }
2065             if (pThis->pNext)
2066                 {
2067                 pThis = pThis->pNext;
2068                 break;
2069                 }
2070             pThis = PopThisElement();
2071             break;
2072         case e_sphrase:
2073             pThis->tossChecked = FALSE;
2074             if (pThis->u.u_sphrase.pSpcOrPCDATA &&
2075                 pThis->u.u_sphrase.pSpcOrPCDATA->tossChecked)
2076                 {
2077                 PushThisElement(pThis);
2078                 pThis = pThis->u.u_sphrase.pSpcOrPCDATA;
2079                 break;
2080                 }
2081             if (pThis->pNext)
2082                 {
2083                 pThis = pThis->pNext;
2084                 break;
2085                 }
2086             pThis = PopThisElement();
2087             break;
2088         case e_if:
2089             pThis->tossChecked = FALSE;
2090             if (pThis->u.u_if.pCond &&
2091                 pThis->u.u_if.pCond->tossChecked)
2092                 {
2093                 PushThisElement(pThis);
2094                 pThis = pThis->u.u_if.pCond;
2095                 break;
2096                 }
2097             if (pThis->u.u_if.pThen &&
2098                 pThis->u.u_if.pThen->tossChecked)
2099                 {
2100                 PushThisElement(pThis);
2101                 pThis = pThis->u.u_if.pThen;
2102                 break;
2103                 }
2104             if (pThis->u.u_if.pElse &&
2105                 pThis->u.u_if.pElse->tossChecked)
2106                 {
2107                 PushThisElement(pThis);
2108                 pThis = pThis->u.u_if.pElse;
2109                 break;
2110                 }
2111             if (pThis->pNext)
2112                 {
2113                 pThis = pThis->pNext;
2114                 break;
2115                 }
2116             pThis = PopThisElement();
2117             break;
2118         case e_cond:
2119             pThis->tossChecked = FALSE;
2120             if (pThis->u.u_cond.pCDATA &&
2121                 pThis->u.u_cond.pCDATA->tossChecked)
2122                 {
2123                 PushThisElement(pThis);
2124                 pThis = pThis->u.u_cond.pCDATA;
2125                 break;
2126                 }
2127             if (pThis->pNext)
2128                 {
2129                 pThis = pThis->pNext;
2130                 break;
2131                 }
2132             pThis = PopThisElement();
2133             break;
2134         case e_then:
2135             pThis->tossChecked = FALSE;
2136             if (pThis->u.u_then.pAtomicOrPCDATA &&
2137                 pThis->u.u_then.pAtomicOrPCDATA->tossChecked)
2138                 {
2139                 PushThisElement(pThis);
2140                 pThis = pThis->u.u_then.pAtomicOrPCDATA;
2141                 break;
2142                 }
2143             if (pThis->pNext)
2144                 {
2145                 pThis = pThis->pNext;
2146                 break;
2147                 }
2148             pThis = PopThisElement();
2149             break;
2150         case e_else:
2151             pThis->tossChecked = FALSE;
2152             if (pThis->u.u_else.pAtomicOrPCDATA &&
2153                 pThis->u.u_else.pAtomicOrPCDATA->tossChecked)
2154                 {
2155                 PushThisElement(pThis);
2156                 pThis = pThis->u.u_else.pAtomicOrPCDATA;
2157                 break;
2158                 }
2159             if (pThis->pNext)
2160                 {
2161                 pThis = pThis->pNext;
2162                 break;
2163                 }
2164             pThis = PopThisElement();
2165             break;
2166         case e_spc:
2167             pThis->tossChecked = FALSE;
2168             if (pThis->pNext)
2169                 {
2170                 pThis = pThis->pNext;
2171                 break;
2172                 }
2173             pThis = PopThisElement();
2174             break;
2175         case e_graphic:
2176             pThis->tossChecked = FALSE;
2177             if (pThis->pNext)
2178                 {
2179                 pThis = pThis->pNext;
2180                 break;
2181                 }
2182             pThis = PopThisElement();
2183             break;
2184         case e_text:
2185             pThis->tossChecked = FALSE;
2186             if (pThis->u.u_text.pCpOrP &&
2187                 pThis->u.u_text.pCpOrP->tossChecked)
2188                 {
2189                 PushThisElement(pThis);
2190                 pThis = pThis->u.u_text.pCpOrP;
2191                 break;
2192                 }
2193             if (pThis->pNext)
2194                 {
2195                 pThis = pThis->pNext;
2196                 break;
2197                 }
2198             pThis = PopThisElement();
2199             break;
2200         case e_audio:
2201             pThis->tossChecked = FALSE;
2202             if (pThis->pNext)
2203                 {
2204                 pThis = pThis->pNext;
2205                 break;
2206                 }
2207             pThis = PopThisElement();
2208             break;
2209         case e_video:
2210             pThis->tossChecked = FALSE;
2211             if (pThis->pNext)
2212                 {
2213                 pThis = pThis->pNext;
2214                 break;
2215                 }
2216             pThis = PopThisElement();
2217             break;
2218         case e_animate:
2219             pThis->tossChecked = FALSE;
2220             if (pThis->pNext)
2221                 {
2222                 pThis = pThis->pNext;
2223                 break;
2224                 }
2225             pThis = PopThisElement();
2226             break;
2227         case e_script:
2228             pThis->tossChecked = FALSE;
2229             if (pThis->u.u_script.pCDATA &&
2230                 pThis->u.u_script.pCDATA->tossChecked)
2231                 {
2232                 PushThisElement(pThis);
2233                 pThis = pThis->u.u_script.pCDATA;
2234                 break;
2235                 }
2236             if (pThis->pNext)
2237                 {
2238                 pThis = pThis->pNext;
2239                 break;
2240                 }
2241             pThis = PopThisElement();
2242             break;
2243         case e_crossdoc:
2244             pThis->tossChecked = FALSE;
2245             if (pThis->pNext)
2246                 {
2247                 pThis = pThis->pNext;
2248                 break;
2249                 }
2250             pThis = PopThisElement();
2251             break;
2252         case e_man_page:
2253             pThis->tossChecked = FALSE;
2254             if (pThis->pNext)
2255                 {
2256                 pThis = pThis->pNext;
2257                 break;
2258                 }
2259             pThis = PopThisElement();
2260             break;
2261         case e_textfile:
2262             pThis->tossChecked = FALSE;
2263             if (pThis->pNext)
2264                 {
2265                 pThis = pThis->pNext;
2266                 break;
2267                 }
2268             pThis = PopThisElement();
2269             break;
2270         case e_sys_cmd:
2271             pThis->tossChecked = FALSE;
2272             if (pThis->pNext)
2273                 {
2274                 pThis = pThis->pNext;
2275                 break;
2276                 }
2277             pThis = PopThisElement();
2278             break;
2279         case e_callback:
2280             pThis->tossChecked = FALSE;
2281             if (pThis->pNext)
2282                 {
2283                 pThis = pThis->pNext;
2284                 break;
2285                 }
2286             pThis = PopThisElement();
2287             break;
2288         case e_switch:
2289             pThis->tossChecked = FALSE;
2290             if (pThis->u.u_switch.pCDATA &&
2291                 pThis->u.u_switch.pCDATA->tossChecked)
2292                 {
2293                 PushThisElement(pThis);
2294                 pThis = pThis->u.u_switch.pCDATA;
2295                 break;
2296                 }
2297             if (pThis->pNext)
2298                 {
2299                 pThis = pThis->pNext;
2300                 break;
2301                 }
2302             pThis = PopThisElement();
2303             break;
2304         case e_cdata:
2305             pThis->tossChecked = FALSE;
2306             if (pThis->pNext)
2307                 {
2308                 pThis = pThis->pNext;
2309                 break;
2310                 }
2311             pThis = PopThisElement();
2312             break;
2313         case e_entry:
2314             pThis->tossChecked = FALSE;
2315             if (pThis->u.u_entry.pSimpleOrPCDATA &&
2316                 pThis->u.u_entry.pSimpleOrPCDATA->tossChecked)
2317                 {
2318                 PushThisElement(pThis);
2319                 pThis = pThis->u.u_entry.pSimpleOrPCDATA;
2320                 break;
2321                 }
2322             if (pThis->u.u_entry.pEntry &&
2323                 pThis->u.u_entry.pEntry->tossChecked)
2324                 {
2325                 PushThisElement(pThis);
2326                 pThis = pThis->u.u_entry.pEntry;
2327                 break;
2328                 }
2329             if (pThis->pNext)
2330                 {
2331                 pThis = pThis->pNext;
2332                 break;
2333                 }
2334             pThis = PopThisElement();
2335             break;
2336         case e_phrase:
2337         case e_keystyle:
2338         case e_headstyle:
2339         case e_formstyle:
2340         case e_frmtstyle:
2341         case e_grphstyle:
2342             pThis = pThis->pNext;
2343             break;
2344         default:
2345             fprintf(stderr, "%s\n", errMess);
2346             if (m_errfile && (m_errfile != stderr))
2347                 fprintf(m_errfile, "%s\n", errMess);
2348             exit(xxx);
2349         }
2350     }
2351 }
2352
2353
2354 /* A function to mark all elements in the TOSS that have been used
2355  * by the document up to this point.
2356 */
2357 #if defined(M_PROTO)
2358 static void MarkUsedTOSS(ElementPtr pRoot)
2359 #else
2360 static void MarkUsedTOSS(pRoot)
2361   ElementPtr pRoot;
2362 #endif
2363 {
2364 ElementPtr pParent, pSnb, pItem, pThis;
2365 static char errMess[] =
2366             "internal error - unrecognized element type in MarkUsedTOSS";
2367
2368 pThis   = pRoot;
2369 pParent = NULL;
2370
2371 while (pThis)
2372     {
2373     switch (pThis->type)
2374         {
2375         case e_sdldoc:
2376             if (!pThis->tossChecked)
2377                 {
2378                 pThis->tossChecked = TRUE;
2379                 pSnb = pThis->u.u_sdldoc.pSnb;
2380                 }
2381             if (pThis->u.u_sdldoc.pHead &&
2382                 !pThis->u.u_sdldoc.pHead->tossChecked)
2383                 {
2384                 PushThisElement(pParent);
2385                 pParent = pThis;
2386                 PushThisElement(pThis);
2387                 pThis = pThis->u.u_sdldoc.pHead;
2388                 break;
2389                 }
2390             if (pThis->u.u_sdldoc.pSnb &&
2391                 !pThis->u.u_sdldoc.pSnb->tossChecked)
2392                 {
2393                 PushThisElement(pParent);
2394                 pParent = pThis;
2395                 PushThisElement(pThis);
2396                 pThis = pThis->u.u_sdldoc.pSnb;
2397                 break;
2398                 }
2399             pThis = NULL;
2400             break;
2401         case e_virpage:
2402             if (!pThis->tossChecked)
2403                 {
2404                 pThis->inheritedLevel = pThis->u.u_virpage.level;
2405                 pThis->inheritedClass = MakeWideCharString("TEXT");
2406                 MarkUsedStyle(pThis->inheritedLevel,
2407                               pThis->inheritedClass,
2408                               pThis->u.u_virpage.ssi);
2409                 pThis->tossChecked = TRUE;
2410                 pSnb = pThis->u.u_virpage.pSnb;
2411                 }
2412             if (pThis->u.u_virpage.pHead &&
2413                 !pThis->u.u_virpage.pHead->tossChecked)
2414                 {
2415                 PushThisElement(pParent);
2416                 pParent = pThis;
2417                 PushThisElement(pThis);
2418                 pThis = pThis->u.u_virpage.pHead;
2419                 break;
2420                 }
2421             if (pThis->u.u_virpage.pSnb &&
2422                 !pThis->u.u_virpage.pSnb->tossChecked)
2423                 {
2424                 PushThisElement(pParent);
2425                 pParent = pThis;
2426                 PushThisElement(pThis);
2427                 pThis = pThis->u.u_virpage.pSnb;
2428                 break;
2429                 }
2430             if (pThis->u.u_virpage.pBlockOrForm &&
2431                 !pThis->u.u_virpage.pBlockOrForm->tossChecked)
2432                 {
2433                 PushThisElement(pParent);
2434                 pParent = pThis;
2435                 PushThisElement(pThis);
2436                 pThis = pThis->u.u_virpage.pBlockOrForm;
2437                 break;
2438                 }
2439             if (pThis->pNext)
2440                 {
2441                 pThis = pThis->pNext;
2442                 break;
2443                 }
2444             m_free(pThis->inheritedClass, "wide character inherited class");
2445             pThis = NULL;
2446             break;
2447         case e_snb:
2448             if (!pThis->tossChecked)
2449                 {
2450                 pThis->inheritedLevel = pParent->inheritedLevel;
2451                 pThis->inheritedClass = pParent->inheritedClass;
2452                 pThis->tossChecked = TRUE;
2453                 }
2454             if (pThis->u.u_snb.pHead &&
2455                 !pThis->u.u_snb.pHead->tossChecked)
2456                 {
2457                 PushThisElement(pParent);
2458                 pParent = pThis;
2459                 PushThisElement(pThis);
2460                 pThis = pThis->u.u_virpage.pHead;
2461                 break;
2462                 }
2463             if (pThis->u.u_snb.pSystemNotations)
2464                 { /* don't do anything now; wait for <snref> */
2465                 }
2466             if (pThis->pNext)
2467                 {
2468                 pThis = pThis->pNext;
2469                 break;
2470                 }
2471             pThis   = PopThisElement();
2472             pParent = PopThisElement();
2473             break;
2474         case e_block:
2475             if (!pThis->tossChecked)
2476                 {
2477                 if (!(pThis->inheritedLevel = pThis->u.u_block.level))
2478                     pThis->inheritedLevel = pParent->inheritedLevel;
2479                 if (!(pThis->inheritedClass = pThis->u.u_block.class))
2480                     pThis->inheritedClass = pParent->inheritedClass;
2481                 if (pThis->u.u_block.level ||
2482                     pThis->u.u_block.class ||
2483                     pThis->u.u_block.ssi)
2484                         MarkUsedStyle(pThis->inheritedLevel,
2485                                       pThis->inheritedClass,
2486                                       pThis->u.u_block.ssi);
2487                 pThis->tossChecked = TRUE;
2488                 }
2489             if (pThis->u.u_block.pHead &&
2490                 !pThis->u.u_block.pHead->tossChecked)
2491                 {
2492                 PushThisElement(pParent);
2493                 pParent = pThis;
2494                 PushThisElement(pThis);
2495                 pThis = pThis->u.u_block.pHead;
2496                 break;
2497                 }
2498             if (pThis->u.u_block.pCpOrP &&
2499                 !pThis->u.u_block.pCpOrP->tossChecked)
2500                 {
2501                 PushThisElement(pParent);
2502                 pParent = pThis;
2503                 PushThisElement(pThis);
2504                 pThis = pThis->u.u_block.pCpOrP;
2505                 break;
2506                 }
2507             if (pThis->pNext)
2508                 {
2509                 pThis = pThis->pNext;
2510                 break;
2511                 }
2512             pThis   = PopThisElement();
2513             pParent = PopThisElement();
2514             break;
2515         case e_form:
2516             if (!pThis->tossChecked)
2517                 {
2518                 if (!(pThis->inheritedLevel = pThis->u.u_form.level))
2519                     pThis->inheritedLevel = pParent->inheritedLevel;
2520                 if (!(pThis->inheritedClass = pThis->u.u_form.class))
2521                     pThis->inheritedClass = pParent->inheritedClass;
2522                 if (pThis->u.u_form.level ||
2523                     pThis->u.u_form.class ||
2524                     pThis->u.u_form.ssi)
2525                         MarkUsedStyle(pThis->inheritedLevel,
2526                                       pThis->inheritedClass,
2527                                       pThis->u.u_form.ssi);
2528                 pThis->tossChecked = TRUE;
2529                 }
2530             if (pThis->u.u_form.pHead &&
2531                 !pThis->u.u_form.pHead->tossChecked)
2532                 {
2533                 PushThisElement(pParent);
2534                 pParent = pThis;
2535                 PushThisElement(pThis);
2536                 pThis = pThis->u.u_form.pHead;
2537                 break;
2538                 }
2539             if (pThis->u.u_form.pFdata &&
2540                 !pThis->u.u_form.pFdata->tossChecked)
2541                 {
2542                 PushThisElement(pParent);
2543                 pParent = pThis;
2544                 PushThisElement(pThis);
2545                 pThis = pThis->u.u_form.pFdata;
2546                 break;
2547                 }
2548             if (pThis->u.u_form.pFstyle &&
2549                 !pThis->u.u_form.pFstyle->tossChecked)
2550                 {
2551                 PushThisElement(pParent);
2552                 pParent = pThis;
2553                 PushThisElement(pThis);
2554                 pThis = pThis->u.u_form.pFstyle;
2555                 break;
2556                 }
2557             if (pThis->pNext)
2558                 {
2559                 pThis = pThis->pNext;
2560                 break;
2561                 }
2562             pThis   = PopThisElement();
2563             pParent = PopThisElement();
2564             break;
2565         case e_fstyle:
2566             if (!pThis->tossChecked)
2567                 {
2568                 pThis->inheritedLevel = pParent->inheritedLevel;
2569                 pThis->inheritedClass = pParent->inheritedClass;
2570                 pThis->tossChecked = TRUE;
2571                 }
2572             if (pThis->u.u_fstyle.pFrowvec &&
2573                 !pThis->u.u_fstyle.pFrowvec->tossChecked)
2574                 {
2575                 PushThisElement(pParent);
2576                 pParent = pThis;
2577                 PushThisElement(pThis);
2578                 pThis = pThis->u.u_fstyle.pFrowvec;
2579                 break;
2580                 }
2581             if (pThis->pNext)
2582                 {
2583                 pThis = pThis->pNext;
2584                 break;
2585                 }
2586             pThis   = PopThisElement();
2587             pParent = PopThisElement();
2588             break;
2589         case e_frowvec:
2590             if (!pThis->tossChecked)
2591                 {
2592                 pThis->inheritedLevel = pParent->inheritedLevel;
2593                 pThis->inheritedClass = pParent->inheritedClass;
2594                 pThis->tossChecked = TRUE;
2595                 }
2596             if (pThis->pNext)
2597                 {
2598                 pThis = pThis->pNext;
2599                 break;
2600                 }
2601             pThis   = PopThisElement();
2602             pParent = PopThisElement();
2603             break;
2604         case e_fdata:
2605             if (!pThis->tossChecked)
2606                 {
2607                 pThis->inheritedLevel = pParent->inheritedLevel;
2608                 pThis->inheritedClass = pParent->inheritedClass;
2609                 pThis->tossChecked = TRUE;
2610                 }
2611             if (pThis->u.u_fdata.pBlockOrForm &&
2612                 !pThis->u.u_fdata.pBlockOrForm->tossChecked)
2613                 {
2614                 PushThisElement(pParent);
2615                 pParent = pThis;
2616                 PushThisElement(pThis);
2617                 pThis = pThis->u.u_fdata.pBlockOrForm;
2618                 break;
2619                 }
2620             if (pThis->pNext)
2621                 {
2622                 pThis = pThis->pNext;
2623                 break;
2624                 }
2625             pThis   = PopThisElement();
2626             pParent = PopThisElement();
2627             break;
2628         case e_p:
2629             if (!pThis->tossChecked)
2630                 {
2631                 pThis->inheritedLevel = pParent->inheritedLevel;
2632                 pThis->inheritedClass = pParent->inheritedClass;
2633                 if (pThis->u.u_p.ssi)
2634                     MarkUsedStyle(pThis->inheritedLevel,
2635                                   pThis->inheritedClass,
2636                                   pThis->u.u_p.ssi);
2637                 pThis->tossChecked = TRUE;
2638                 }
2639             if (pThis->u.u_p.pHead &&
2640                 !pThis->u.u_p.pHead->tossChecked)
2641                 {
2642                 PushThisElement(pParent);
2643                 pParent = pThis;
2644                 PushThisElement(pThis);
2645                 pThis = pThis->u.u_p.pHead;
2646                 break;
2647                 }
2648             if (pThis->u.u_p.pAtomicOrPCDATA &&
2649                 !pThis->u.u_p.pAtomicOrPCDATA->tossChecked)
2650                 {
2651                 PushThisElement(pParent);
2652                 pParent = pThis;
2653                 PushThisElement(pThis);
2654                 pThis = pThis->u.u_p.pAtomicOrPCDATA;
2655                 break;
2656                 }
2657             if (pThis->pNext)
2658                 {
2659                 pThis = pThis->pNext;
2660                 break;
2661                 }
2662             pThis   = PopThisElement();
2663             pParent = PopThisElement();
2664             break;
2665         case e_cp:
2666             if (!pThis->tossChecked)
2667                 {
2668                 pThis->inheritedLevel = pParent->inheritedLevel;
2669                 pThis->inheritedClass = pParent->inheritedClass;
2670                 if (pThis->u.u_cp.ssi)
2671                     MarkUsedStyle(pThis->inheritedLevel,
2672                                   pThis->inheritedClass,
2673                                   pThis->u.u_cp.ssi);
2674                 pThis->tossChecked = TRUE;
2675                 }
2676             if (pThis->u.u_cp.pCDATA &&
2677                 !pThis->u.u_cp.pCDATA->tossChecked)
2678                 {
2679                 PushThisElement(pParent);
2680                 pParent = pThis;
2681                 PushThisElement(pThis);
2682                 pThis = pThis->u.u_cp.pCDATA;
2683                 break;
2684                 }
2685             if (pThis->pNext)
2686                 {
2687                 pThis = pThis->pNext;
2688                 break;
2689                 }
2690             pThis   = PopThisElement();
2691             pParent = PopThisElement();
2692             break;
2693         case e_head:
2694             if (!pThis->tossChecked)
2695                 {
2696                 pThis->inheritedLevel = pParent->inheritedLevel;
2697                 if (!(pThis->inheritedClass = pThis->u.u_head.class))
2698                     pThis->inheritedClass = MakeWideCharString("HEAD");
2699                 MarkUsedStyle(pThis->inheritedLevel,
2700                               pThis->inheritedClass,
2701                               pThis->u.u_head.ssi);
2702                 pThis->tossChecked = TRUE;
2703                 }
2704             if (pThis->u.u_head.pAtomicOrPCDATA &&
2705                 !pThis->u.u_head.pAtomicOrPCDATA->tossChecked)
2706                 {
2707                 PushThisElement(pParent);
2708                 pParent = pThis;
2709                 PushThisElement(pThis);
2710                 pThis = pThis->u.u_head.pAtomicOrPCDATA;
2711                 break;
2712                 }
2713             if (pThis->u.u_head.pSubhead &&
2714                 !pThis->u.u_head.pSubhead->tossChecked)
2715                 {
2716                 PushThisElement(pParent);
2717                 pParent = pThis;
2718                 PushThisElement(pThis);
2719                 pThis = pThis->u.u_head.pSubhead;
2720                 break;
2721                 }
2722             if (pThis->pNext)
2723                 {
2724                 pThis = pThis->pNext;
2725                 break;
2726                 }
2727             if (!pThis->u.u_head.class)
2728                 m_free(pThis->inheritedClass,
2729                        "wide character inherited class");
2730             pThis   = PopThisElement();
2731             pParent = PopThisElement();
2732             break;
2733         case e_subhead:
2734             if (!pThis->tossChecked)
2735                 {
2736                 pThis->inheritedLevel = pParent->inheritedLevel;
2737                 if (!(pThis->inheritedClass = pThis->u.u_subhead.class))
2738                     pThis->inheritedClass = pParent->inheritedClass;
2739                 if (pThis->u.u_subhead.class || pThis->u.u_subhead.ssi)
2740                     MarkUsedStyle(pThis->inheritedLevel,
2741                                   pThis->inheritedClass,
2742                                   pThis->u.u_subhead.ssi);
2743                 pThis->tossChecked = TRUE;
2744                 }
2745             if (pThis->u.u_subhead.pAtomicOrPCDATA &&
2746                 !pThis->u.u_subhead.pAtomicOrPCDATA->tossChecked)
2747                 {
2748                 PushThisElement(pParent);
2749                 pParent = pThis;
2750                 PushThisElement(pThis);
2751                 pThis = pThis->u.u_subhead.pAtomicOrPCDATA;
2752                 break;
2753                 }
2754             if (pThis->pNext)
2755                 {
2756                 pThis = pThis->pNext;
2757                 break;
2758                 }
2759             pThis   = PopThisElement();
2760             pParent = PopThisElement();
2761             break;
2762         case e_anchor:
2763             if (!pThis->tossChecked)
2764                 {
2765                 pThis->inheritedLevel = pParent->inheritedLevel;
2766                 pThis->inheritedClass = pParent->inheritedClass;
2767                 pThis->tossChecked = TRUE;
2768                 }
2769             if (pThis->pNext)
2770                 {
2771                 pThis = pThis->pNext;
2772                 break;
2773                 }
2774             pThis   = PopThisElement();
2775             pParent = PopThisElement();
2776             break;
2777         case e_link:
2778             if (!pThis->tossChecked)
2779                 {
2780                 pThis->inheritedLevel = pParent->inheritedLevel;
2781                 pThis->inheritedClass = pParent->inheritedClass;
2782                 pThis->tossChecked = TRUE;
2783                 }
2784             if (pThis->u.u_link.pAtomicOrPCDATA &&
2785                 !pThis->u.u_link.pAtomicOrPCDATA->tossChecked)
2786                 {
2787                 PushThisElement(pParent);
2788                 pParent = pThis;
2789                 PushThisElement(pThis);
2790                 pThis = pThis->u.u_link.pAtomicOrPCDATA;
2791                 break;
2792                 }
2793             if (pThis->pNext)
2794                 {
2795                 pThis = pThis->pNext;
2796                 break;
2797                 }
2798             pThis   = PopThisElement();
2799             pParent = PopThisElement();
2800             break;
2801         case e_snref:
2802             if (!pThis->tossChecked)
2803                 {
2804                 pThis->inheritedLevel = pParent->inheritedLevel;
2805                 pThis->inheritedClass = pParent->inheritedClass;
2806                 pThis->tossChecked = TRUE;
2807                 }
2808             if (pThis->u.u_snref.pRefitem &&
2809                 !pThis->u.u_snref.pRefitem->tossChecked)
2810                 {
2811                 PushThisElement(pParent);
2812                 pParent = pThis;
2813                 PushThisElement(pThis);
2814                 pThis = pThis->u.u_snref.pRefitem;
2815                 break;
2816                 }
2817             if (pThis->u.u_snref.pAlttext &&
2818                 !pThis->u.u_snref.pAlttext->tossChecked)
2819                 {
2820                 PushThisElement(pParent);
2821                 pParent = pThis;
2822                 PushThisElement(pThis);
2823                 pThis = pThis->u.u_snref.pAlttext;
2824                 break;
2825                 }
2826             if (pThis->pNext)
2827                 {
2828                 pThis = pThis->pNext;
2829                 break;
2830                 }
2831             pThis   = PopThisElement();
2832             pParent = PopThisElement();
2833             break;
2834         case e_refitem:
2835             if (!pThis->tossChecked)
2836                 {
2837                 pThis->inheritedLevel = pParent->inheritedLevel;
2838                 pThis->inheritedClass = pThis->u.u_refitem.class;
2839                 MarkUsedStyle(pThis->inheritedLevel,
2840                               pThis->inheritedClass,
2841                               pThis->u.u_refitem.ssi);
2842                 if ((pItem = LookupInSnb(pSnb, pThis->u.u_refitem.rid)) &&
2843                     (pItem->type == e_text))
2844                         {
2845                         pThis->u.u_refitem.pReferencedItem = pItem;
2846                         ClearTree(pItem);
2847                         }
2848                 pThis->tossChecked = TRUE;
2849                 }
2850             if (pThis->u.u_refitem.pHead &&
2851                 !pThis->u.u_refitem.pHead->tossChecked)
2852                 {
2853                 PushThisElement(pParent);
2854                 pParent = pThis;
2855                 PushThisElement(pThis);
2856                 pThis = pThis->u.u_refitem.pHead;
2857                 break;
2858                 }
2859             if (pThis->u.u_refitem.pReferencedItem &&
2860                 !pThis->u.u_refitem.pReferencedItem->tossChecked)
2861                 {
2862                 PushThisElement(pParent);
2863                 pParent = pThis;
2864                 PushThisElement(pThis);
2865                 pThis = pThis->u.u_refitem.pReferencedItem;
2866                 break;
2867                 }
2868             if (pThis->pNext)
2869                 {
2870                 pThis = pThis->pNext;
2871                 break;
2872                 }
2873             pThis   = PopThisElement();
2874             pParent = PopThisElement();
2875             break;
2876         case e_alttext:
2877             if (!pThis->tossChecked)
2878                 {
2879                 pThis->inheritedLevel = pParent->inheritedLevel;
2880                 pThis->inheritedClass = pParent->inheritedClass;
2881                 pThis->tossChecked = TRUE;
2882                 }
2883             if (pThis->u.u_alttext.pCDATA &&
2884                 !pThis->u.u_alttext.pCDATA->tossChecked)
2885                 {
2886                 PushThisElement(pParent);
2887                 pParent = pThis;
2888                 PushThisElement(pThis);
2889                 pThis = pThis->u.u_alttext.pCDATA;
2890                 break;
2891                 }
2892             if (pThis->pNext)
2893                 {
2894                 pThis = pThis->pNext;
2895                 break;
2896                 }
2897             pThis   = PopThisElement();
2898             pParent = PopThisElement();
2899             break;
2900         case e_rev:
2901             if (!pThis->tossChecked)
2902                 {
2903                 pThis->inheritedLevel = pParent->inheritedLevel;
2904                 pThis->inheritedClass = pParent->inheritedClass;
2905                 pThis->tossChecked = TRUE;
2906                 }
2907             if (pThis->u.u_rev.pAtomicOrPCDATA &&
2908                 !pThis->u.u_rev.pAtomicOrPCDATA->tossChecked)
2909                 {
2910                 PushThisElement(pParent);
2911                 pParent = pThis;
2912                 PushThisElement(pThis);
2913                 pThis = pThis->u.u_rev.pAtomicOrPCDATA;
2914                 break;
2915                 }
2916             if (pThis->pNext)
2917                 {
2918                 pThis = pThis->pNext;
2919                 break;
2920                 }
2921             pThis   = PopThisElement();
2922             pParent = PopThisElement();
2923             break;
2924         case e_key:
2925             if (!pThis->tossChecked)
2926                 {
2927                 pThis->inheritedLevel = pParent->inheritedLevel;
2928                 pThis->inheritedClass = pThis->u.u_key.class;
2929                 MarkUsedStyle(pThis->inheritedLevel,
2930                               pThis->inheritedClass,
2931                               pThis->u.u_key.ssi);
2932                 pThis->tossChecked = TRUE;
2933                 }
2934             if (pThis->u.u_key.pAtomicOrPCDATA &&
2935                 !pThis->u.u_key.pAtomicOrPCDATA->tossChecked)
2936                 {
2937                 PushThisElement(pParent);
2938                 pParent = pThis;
2939                 PushThisElement(pThis);
2940                 pThis = pThis->u.u_key.pAtomicOrPCDATA;
2941                 break;
2942                 }
2943             if (pThis->pNext)
2944                 {
2945                 pThis = pThis->pNext;
2946                 break;
2947                 }
2948             pThis   = PopThisElement();
2949             pParent = PopThisElement();
2950             break;
2951         case e_sphrase:
2952             if (!pThis->tossChecked)
2953                 {
2954                 pThis->inheritedLevel = pParent->inheritedLevel;
2955                 pThis->inheritedClass = pThis->u.u_sphrase.class;
2956                 MarkUsedStyle(pThis->inheritedLevel,
2957                               pThis->inheritedClass,
2958                               pThis->u.u_sphrase.ssi);
2959                 pThis->tossChecked = TRUE;
2960                 }
2961             if (pThis->u.u_sphrase.pSpcOrPCDATA &&
2962                 !pThis->u.u_sphrase.pSpcOrPCDATA->tossChecked)
2963                 {
2964                 PushThisElement(pParent);
2965                 pParent = pThis;
2966                 PushThisElement(pThis);
2967                 pThis = pThis->u.u_sphrase.pSpcOrPCDATA;
2968                 break;
2969                 }
2970             if (pThis->pNext)
2971                 {
2972                 pThis = pThis->pNext;
2973                 break;
2974                 }
2975             pThis   = PopThisElement();
2976             pParent = PopThisElement();
2977             break;
2978         case e_if:
2979             if (!pThis->tossChecked)
2980                 {
2981                 pThis->inheritedLevel = pParent->inheritedLevel;
2982                 pThis->inheritedClass = pParent->inheritedClass;
2983                 pThis->tossChecked = TRUE;
2984                 }
2985             if (pThis->u.u_if.pCond &&
2986                 !pThis->u.u_if.pCond->tossChecked)
2987                 {
2988                 PushThisElement(pParent);
2989                 pParent = pThis;
2990                 PushThisElement(pThis);
2991                 pThis = pThis->u.u_if.pCond;
2992                 break;
2993                 }
2994             if (pThis->u.u_if.pThen &&
2995                 !pThis->u.u_if.pThen->tossChecked)
2996                 {
2997                 PushThisElement(pParent);
2998                 pParent = pThis;
2999                 PushThisElement(pThis);
3000                 pThis = pThis->u.u_if.pThen;
3001                 break;
3002                 }
3003             if (pThis->u.u_if.pElse &&
3004                 !pThis->u.u_if.pElse->tossChecked)
3005                 {
3006                 PushThisElement(pParent);
3007                 pParent = pThis;
3008                 PushThisElement(pThis);
3009                 pThis = pThis->u.u_if.pElse;
3010                 break;
3011                 }
3012             if (pThis->pNext)
3013                 {
3014                 pThis = pThis->pNext;
3015                 break;
3016                 }
3017             pThis   = PopThisElement();
3018             pParent = PopThisElement();
3019             break;
3020         case e_cond:
3021             if (!pThis->tossChecked)
3022                 {
3023                 pThis->inheritedLevel = pParent->inheritedLevel;
3024                 pThis->inheritedClass = pParent->inheritedClass;
3025                 pThis->tossChecked = TRUE;
3026                 }
3027             if (pThis->u.u_cond.pCDATA &&
3028                 !pThis->u.u_cond.pCDATA->tossChecked)
3029                 {
3030                 PushThisElement(pParent);
3031                 pParent = pThis;
3032                 PushThisElement(pThis);
3033                 pThis = pThis->u.u_cond.pCDATA;
3034                 break;
3035                 }
3036             if (pThis->pNext)
3037                 {
3038                 pThis = pThis->pNext;
3039                 break;
3040                 }
3041             pThis   = PopThisElement();
3042             pParent = PopThisElement();
3043             break;
3044         case e_then:
3045             if (!pThis->tossChecked)
3046                 {
3047                 pThis->inheritedLevel = pParent->inheritedLevel;
3048                 pThis->inheritedClass = pParent->inheritedClass;
3049                 pThis->tossChecked = TRUE;
3050                 }
3051             if (pThis->u.u_then.pAtomicOrPCDATA &&
3052                 !pThis->u.u_then.pAtomicOrPCDATA->tossChecked)
3053                 {
3054                 PushThisElement(pParent);
3055                 pParent = pThis;
3056                 PushThisElement(pThis);
3057                 pThis = pThis->u.u_then.pAtomicOrPCDATA;
3058                 break;
3059                 }
3060             if (pThis->pNext)
3061                 {
3062                 pThis = pThis->pNext;
3063                 break;
3064                 }
3065             pThis   = PopThisElement();
3066             pParent = PopThisElement();
3067             break;
3068         case e_else:
3069             if (!pThis->tossChecked)
3070                 {
3071                 pThis->inheritedLevel = pParent->inheritedLevel;
3072                 pThis->inheritedClass = pParent->inheritedClass;
3073                 pThis->tossChecked = TRUE;
3074                 }
3075             if (pThis->u.u_else.pAtomicOrPCDATA &&
3076                 !pThis->u.u_else.pAtomicOrPCDATA->tossChecked)
3077                 {
3078                 PushThisElement(pParent);
3079                 pParent = pThis;
3080                 PushThisElement(pThis);
3081                 pThis = pThis->u.u_else.pAtomicOrPCDATA;
3082                 break;
3083                 }
3084             if (pThis->pNext)
3085                 {
3086                 pThis = pThis->pNext;
3087                 break;
3088                 }
3089             pThis   = PopThisElement();
3090             pParent = PopThisElement();
3091             break;
3092         case e_spc:
3093             if (!pThis->tossChecked)
3094                 {
3095                 pThis->inheritedLevel = pParent->inheritedLevel;
3096                 pThis->inheritedClass = pParent->inheritedClass;
3097                 pThis->tossChecked = TRUE;
3098                 }
3099             if (pThis->pNext)
3100                 {
3101                 pThis = pThis->pNext;
3102                 break;
3103                 }
3104             pThis   = PopThisElement();
3105             pParent = PopThisElement();
3106             break;
3107         case e_graphic:
3108             if (!pThis->tossChecked)
3109                 {
3110                 pThis->inheritedLevel = pParent->inheritedLevel;
3111                 pThis->inheritedClass = pParent->inheritedClass;
3112                 pThis->tossChecked = TRUE;
3113                 }
3114             if (pThis->pNext)
3115                 {
3116                 pThis = pThis->pNext;
3117                 break;
3118                 }
3119             pThis   = PopThisElement();
3120             pParent = PopThisElement();
3121             break;
3122         case e_text:
3123             if (!pThis->tossChecked)
3124                 {
3125                 pThis->inheritedLevel = pParent->inheritedLevel;
3126                 pThis->inheritedClass = pParent->inheritedClass;
3127                 pThis->tossChecked = TRUE;
3128                 }
3129             if (pThis->u.u_text.pCpOrP &&
3130                 !pThis->u.u_text.pCpOrP->tossChecked)
3131                 {
3132                 PushThisElement(pParent);
3133                 pParent = pThis;
3134                 PushThisElement(pThis);
3135                 pThis = pThis->u.u_text.pCpOrP;
3136                 break;
3137                 }
3138             if (pThis->pNext)
3139                 {
3140                 pThis = pThis->pNext;
3141                 break;
3142                 }
3143             pThis   = PopThisElement();
3144             pParent = PopThisElement();
3145             break;
3146         case e_audio:
3147             if (!pThis->tossChecked)
3148                 {
3149                 pThis->inheritedLevel = pParent->inheritedLevel;
3150                 pThis->inheritedClass = pParent->inheritedClass;
3151                 pThis->tossChecked = TRUE;
3152                 }
3153             if (pThis->pNext)
3154                 {
3155                 pThis = pThis->pNext;
3156                 break;
3157                 }
3158             pThis   = PopThisElement();
3159             pParent = PopThisElement();
3160             break;
3161         case e_video:
3162             if (!pThis->tossChecked)
3163                 {
3164                 pThis->inheritedLevel = pParent->inheritedLevel;
3165                 pThis->inheritedClass = pParent->inheritedClass;
3166                 pThis->tossChecked = TRUE;
3167                 }
3168             if (pThis->pNext)
3169                 {
3170                 pThis = pThis->pNext;
3171                 break;
3172                 }
3173             pThis   = PopThisElement();
3174             pParent = PopThisElement();
3175             break;
3176         case e_animate:
3177             if (!pThis->tossChecked)
3178                 {
3179                 pThis->inheritedLevel = pParent->inheritedLevel;
3180                 pThis->inheritedClass = pParent->inheritedClass;
3181                 pThis->tossChecked = TRUE;
3182                 }
3183             if (pThis->pNext)
3184                 {
3185                 pThis = pThis->pNext;
3186                 break;
3187                 }
3188             pThis   = PopThisElement();
3189             pParent = PopThisElement();
3190             break;
3191         case e_script:
3192             if (!pThis->tossChecked)
3193                 {
3194                 pThis->inheritedLevel = pParent->inheritedLevel;
3195                 pThis->inheritedClass = pParent->inheritedClass;
3196                 pThis->tossChecked = TRUE;
3197                 }
3198             if (pThis->u.u_script.pCDATA &&
3199                 !pThis->u.u_script.pCDATA->tossChecked)
3200                 {
3201                 PushThisElement(pParent);
3202                 pParent = pThis;
3203                 PushThisElement(pThis);
3204                 pThis = pThis->u.u_script.pCDATA;
3205                 break;
3206                 }
3207             if (pThis->pNext)
3208                 {
3209                 pThis = pThis->pNext;
3210                 break;
3211                 }
3212             pThis   = PopThisElement();
3213             pParent = PopThisElement();
3214             break;
3215         case e_crossdoc:
3216             if (!pThis->tossChecked)
3217                 {
3218                 pThis->inheritedLevel = pParent->inheritedLevel;
3219                 pThis->inheritedClass = pParent->inheritedClass;
3220                 pThis->tossChecked = TRUE;
3221                 }
3222             if (pThis->pNext)
3223                 {
3224                 pThis = pThis->pNext;
3225                 break;
3226                 }
3227             pThis   = PopThisElement();
3228             pParent = PopThisElement();
3229             break;
3230         case e_man_page:
3231             if (!pThis->tossChecked)
3232                 {
3233                 pThis->inheritedLevel = pParent->inheritedLevel;
3234                 pThis->inheritedClass = pParent->inheritedClass;
3235                 pThis->tossChecked = TRUE;
3236                 }
3237             if (pThis->pNext)
3238                 {
3239                 pThis = pThis->pNext;
3240                 break;
3241                 }
3242             pThis   = PopThisElement();
3243             pParent = PopThisElement();
3244             break;
3245         case e_textfile:
3246             if (!pThis->tossChecked)
3247                 {
3248                 pThis->inheritedLevel = pParent->inheritedLevel;
3249                 pThis->inheritedClass = pParent->inheritedClass;
3250                 pThis->tossChecked = TRUE;
3251                 }
3252             if (pThis->pNext)
3253                 {
3254                 pThis = pThis->pNext;
3255                 break;
3256                 }
3257             pThis   = PopThisElement();
3258             pParent = PopThisElement();
3259             break;
3260         case e_sys_cmd:
3261             if (!pThis->tossChecked)
3262                 {
3263                 pThis->inheritedLevel = pParent->inheritedLevel;
3264                 pThis->inheritedClass = pParent->inheritedClass;
3265                 pThis->tossChecked = TRUE;
3266                 }
3267             if (pThis->pNext)
3268                 {
3269                 pThis = pThis->pNext;
3270                 break;
3271                 }
3272             pThis   = PopThisElement();
3273             pParent = PopThisElement();
3274             break;
3275         case e_callback:
3276             if (!pThis->tossChecked)
3277                 {
3278                 pThis->inheritedLevel = pParent->inheritedLevel;
3279                 pThis->inheritedClass = pParent->inheritedClass;
3280                 pThis->tossChecked = TRUE;
3281                 }
3282             if (pThis->pNext)
3283                 {
3284                 pThis = pThis->pNext;
3285                 break;
3286                 }
3287             pThis   = PopThisElement();
3288             pParent = PopThisElement();
3289             break;
3290         case e_switch:
3291             if (!pThis->tossChecked)
3292                 {
3293                 pThis->inheritedLevel = pParent->inheritedLevel;
3294                 pThis->inheritedClass = pParent->inheritedClass;
3295                 pThis->tossChecked = TRUE;
3296                 }
3297             if (pThis->u.u_switch.pCDATA &&
3298                 !pThis->u.u_switch.pCDATA->tossChecked)
3299                 {
3300                 PushThisElement(pParent);
3301                 pParent = pThis;
3302                 PushThisElement(pThis);
3303                 pThis = pThis->u.u_switch.pCDATA;
3304                 break;
3305                 }
3306             if (pThis->pNext)
3307                 {
3308                 pThis = pThis->pNext;
3309                 break;
3310                 }
3311             pThis   = PopThisElement();
3312             pParent = PopThisElement();
3313             break;
3314         case e_cdata:
3315             if (!pThis->tossChecked)
3316                 {
3317                 pThis->inheritedLevel = pParent->inheritedLevel;
3318                 pThis->inheritedClass = pParent->inheritedClass;
3319                 pThis->tossChecked = TRUE;
3320                 }
3321             if (pThis->pNext)
3322                 {
3323                 pThis = pThis->pNext;
3324                 break;
3325                 }
3326             pThis   = PopThisElement();
3327             pParent = PopThisElement();
3328             break;
3329         case e_entry:
3330             if (!pThis->tossChecked)
3331                 {
3332                 pThis->inheritedLevel = pParent->inheritedLevel;
3333                 pThis->inheritedClass = pParent->inheritedClass;
3334                 pThis->tossChecked = TRUE;
3335                 }
3336             if (pThis->u.u_entry.pSimpleOrPCDATA &&
3337                 !pThis->u.u_entry.pSimpleOrPCDATA->tossChecked)
3338                 {
3339                 PushThisElement(pParent);
3340                 pParent = pThis;
3341                 PushThisElement(pThis);
3342                 pThis = pThis->u.u_entry.pSimpleOrPCDATA;
3343                 break;
3344                 }
3345             if (pThis->u.u_entry.pEntry &&
3346                 !pThis->u.u_entry.pEntry->tossChecked)
3347                 {
3348                 PushThisElement(pParent);
3349                 pParent = pThis;
3350                 PushThisElement(pThis);
3351                 pThis = pThis->u.u_entry.pEntry;
3352                 break;
3353                 }
3354             if (pThis->pNext)
3355                 {
3356                 pThis = pThis->pNext;
3357                 break;
3358                 }
3359             pThis   = PopThisElement();
3360             pParent = PopThisElement();
3361             break;
3362         case e_phrase:
3363         case e_keystyle:
3364         case e_headstyle:
3365         case e_formstyle:
3366         case e_frmtstyle:
3367         case e_grphstyle:
3368             pThis = pThis->pNext;
3369             break;
3370         default:
3371             fprintf(stderr, "%s\n", errMess);
3372             if (m_errfile && (m_errfile != stderr))
3373                 fprintf(m_errfile, "%s\n", errMess);
3374             exit(xxx);
3375         }
3376     }
3377 }
3378
3379
3380 #if defined(M_PROTO)
3381 void EmitSDL(ElementPtr pRoot)
3382 #else
3383 void EmitSDL(pRoot)
3384   ElementPtr pRoot;
3385 #endif
3386 {
3387 ElementPtr     pThis, pThat;
3388 long int       virpageOffset;
3389 LOGICAL        doCompression;
3390 int            zFileSize, length;
3391 unsigned char  zTemp[4];
3392 int            zFd;
3393 char           buffer[BUFSIZ];
3394 FILE          *saveOutFile;
3395 char           cCount[32];
3396 static char    errMess[] =
3397                     "internal error - unrecognized element type in EmitSDL";
3398
3399 virpageOffset = 0;
3400 doCompression = FALSE;
3401 if (pRoot)
3402     {
3403     if (minimalTossFlag)
3404         MarkUsedTOSS(pRoot);
3405     if (pRoot->type == e_virpage)
3406         {
3407         virpageOffset = ftell(outFile);
3408         if (compressFlag)
3409             {
3410             doCompression = TRUE;
3411             saveOutFile = outFile;
3412             outFile = FopenFile(compFileName, O_WRONLY, xxx);
3413             }
3414         }
3415     pThis = pRoot;
3416     }
3417 else
3418     {
3419     pThis = pElementChain;
3420     sprintf(cCount, " COUNT=\"%d\">\n", nElements);
3421     PutString(cCount);
3422     pElementChain = NULL;
3423     nElements     = 0;
3424
3425     /* push a null so the last <entry> pop will terminate the loop */
3426     if (pThis && (pThis->type == e_entry))
3427         PushThisElement(NULL);
3428     }
3429
3430 while (pThis)
3431     {
3432     switch (pThis->type)
3433         {
3434         case e_sdldoc:
3435             /* we've already emitted the <sdldoc ...> stuff */
3436             if (pThis->u.u_sdldoc.pHead)
3437                 {
3438                 PushThisElement(pThis);
3439                 pThat = pThis;
3440                 pThis = pThis->u.u_sdldoc.pHead;
3441                 pThat->u.u_sdldoc.pHead = NULL;
3442                 break;
3443                 }
3444             if (pThis->u.u_sdldoc.pSnb)
3445                 {
3446                 PushThisElement(pThis);
3447                 pThat = pThis;
3448                 pThis = pThis->u.u_sdldoc.pSnb;
3449                 pThat->u.u_sdldoc.pSnb = NULL;
3450                 break;
3451                 }
3452             DeleteElement(pThis);
3453             pThis = NULL;
3454             break;
3455         case e_virpage:
3456             if (!pThis->beenEmitted)
3457                 {
3458                 OpenNamedTag("VIRPAGE", TRUE, FALSE);
3459                 RequiredAttribute("ID", pThis->u.u_virpage.id);
3460                 AddToLOIDS("VIRPAGE",
3461                            pThis->u.u_virpage.id,
3462                            pThis->u.u_virpage.ssi,
3463                            pThis->u.u_virpage.level,
3464                            virpageOffset);
3465                 RequiredAttribute("LEVEL", pThis->u.u_virpage.level);
3466                 ImpliedAttribute("VERSION", pThis->u.u_virpage.version);
3467                 ImpliedAttribute("LANGUAGE", pThis->u.u_virpage.language);
3468                 ImpliedAttribute("CHARSET", pThis->u.u_virpage.charset);
3469                 RequiredAttribute("DOC-ID", pThis->u.u_virpage.doc_id);
3470                 ImpliedAttribute("SSI", pThis->u.u_virpage.ssi);
3471                 PutString(">\n");
3472                 pThis->beenEmitted = TRUE;
3473                 }
3474             if (pThis->u.u_virpage.pHead)
3475                 {
3476                 PushThisElement(pThis);
3477                 pThat = pThis;
3478                 pThis = pThis->u.u_virpage.pHead;
3479                 pThat->u.u_virpage.pHead = NULL;
3480                 break;
3481                 }
3482             if (pThis->u.u_virpage.pSnb)
3483                 {
3484                 PushThisElement(pThis);
3485                 pThat = pThis;
3486                 pThis = pThis->u.u_virpage.pSnb;
3487                 pThat->u.u_virpage.pSnb = NULL;
3488                 break;
3489                 }
3490             if (pThis->u.u_virpage.pBlockOrForm)
3491                 {
3492                 PushThisElement(pThis);
3493                 pThat = pThis;
3494                 pThis = pThis->u.u_virpage.pBlockOrForm;
3495                 pThat->u.u_virpage.pBlockOrForm = NULL;
3496                 break;
3497                 }
3498             if (pThis->pNext)
3499                 {
3500                 pThat = pThis;
3501                 pThis = pThat->pNext;
3502                 pThat->pNext = NULL;
3503                 DeleteElement(pThat);
3504                 CloseNamedTag("VIRPAGE", TRUE);
3505                 break;
3506                 }
3507             DeleteElement(pThis);
3508             CloseNamedTag("VIRPAGE", TRUE);
3509             pThis = NULL;
3510             break;
3511         case e_snb:
3512             if (!pThis->beenEmitted)
3513                 {
3514                 OpenNamedTag("SNB", TRUE, FALSE);
3515                 ImpliedAttribute("VERSION", pThis->u.u_snb.version);
3516                 PutString(">\n");
3517                 pThis->beenEmitted = TRUE;
3518                 }
3519             if (pThis->u.u_snb.pHead)
3520                 {
3521                 PushThisElement(pThis);
3522                 pThat = pThis;
3523                 pThis = pThis->u.u_virpage.pHead;
3524                 pThat->u.u_virpage.pHead = NULL;
3525                 break;
3526                 }
3527             if (pThis->u.u_snb.pSystemNotations)
3528                 {
3529                 PushThisElement(pThis);
3530                 pThat = pThis;
3531                 pThis = pThis->u.u_snb.pSystemNotations;
3532                 pThat->u.u_snb.pSystemNotations = NULL;
3533                 break;
3534                 }
3535             if (pThis->pNext)
3536                 {
3537                 pThat = pThis;
3538                 pThis = pThat->pNext;
3539                 pThat->pNext = NULL;
3540                 DeleteElement(pThat);
3541                 CloseNamedTag("SNB", TRUE);
3542                 break;
3543                 }
3544             DeleteElement(pThis);
3545             CloseNamedTag("SNB", TRUE);
3546             pThis = PopThisElement();
3547             break;
3548         case e_block:
3549             if (!pThis->beenEmitted)
3550                 {
3551                 OpenNamedTag("BLOCK", TRUE, FALSE);
3552                 ImpliedAttribute("ID", pThis->u.u_block.id);
3553                 AddToLOIDS("BLOCK",
3554                            pThis->u.u_block.id,
3555                            pThis->u.u_block.ssi,
3556                            pThis->u.u_block.level,
3557                            virpageOffset);
3558                 ImpliedAttribute("LEVEL", pThis->u.u_block.level);
3559                 ImpliedAttribute("VERSION", pThis->u.u_block.version);
3560                 ImpliedAttribute("CLASS", pThis->u.u_block.class);
3561                 ImpliedAttribute("LANGUAGE", pThis->u.u_block.language);
3562                 ImpliedAttribute("CHARSET", pThis->u.u_block.charset);
3563                 ImpliedAttribute("LENGTH", pThis->u.u_block.length);
3564                 ImpliedAttribute("APP", pThis->u.u_block.app);
3565                 ImpliedAttribute("TIMING", pThis->u.u_block.timing);
3566                 ImpliedAttribute("SSI", pThis->u.u_block.ssi);
3567                 PutString(">\n");
3568                 pThis->beenEmitted = TRUE;
3569                 }
3570             if (pThis->u.u_block.pHead)
3571                 {
3572                 PushThisElement(pThis);
3573                 pThat = pThis;
3574                 pThis = pThis->u.u_block.pHead;
3575                 pThat->u.u_block.pHead = NULL;
3576                 break;
3577                 }
3578             if (pThis->u.u_block.pCpOrP)
3579                 {
3580                 PushThisElement(pThis);
3581                 pThat = pThis;
3582                 pThis = pThis->u.u_block.pCpOrP;
3583                 pThat->u.u_block.pCpOrP = NULL;
3584                 break;
3585                 }
3586             if (pThis->pNext)
3587                 {
3588                 pThat = pThis;
3589                 pThis = pThat->pNext;
3590                 pThat->pNext = NULL;
3591                 DeleteElement(pThat);
3592                 CloseNamedTag("BLOCK", TRUE);
3593                 break;
3594                 }
3595             DeleteElement(pThis);
3596             CloseNamedTag("BLOCK", TRUE);
3597             pThis = PopThisElement();
3598             break;
3599         case e_form:
3600             if (!pThis->beenEmitted)
3601                 {
3602                 OpenNamedTag("FORM", TRUE, FALSE);
3603                 ImpliedAttribute("ID", pThis->u.u_form.id);
3604                 AddToLOIDS("FORM",
3605                            pThis->u.u_form.id,
3606                            pThis->u.u_form.ssi,
3607                            pThis->u.u_form.level,
3608                            virpageOffset);
3609                 ImpliedAttribute("LEVEL", pThis->u.u_form.level);
3610                 ImpliedAttribute("VERSION", pThis->u.u_form.version);
3611                 ImpliedAttribute("CLASS", pThis->u.u_form.class);
3612                 ImpliedAttribute("LANGUAGE", pThis->u.u_form.language);
3613                 ImpliedAttribute("CHARSET", pThis->u.u_form.charset);
3614                 ImpliedAttribute("LENGTH", pThis->u.u_form.length);
3615                 ImpliedAttribute("APP", pThis->u.u_form.app);
3616                 ImpliedAttribute("SSI", pThis->u.u_form.ssi);
3617                 PutString(">\n");
3618                 pThis->beenEmitted = TRUE;
3619                 }
3620             if (pThis->u.u_form.pHead)
3621                 {
3622                 PushThisElement(pThis);
3623                 pThat = pThis;
3624                 pThis = pThis->u.u_form.pHead;
3625                 pThat->u.u_form.pHead = NULL;
3626                 break;
3627                 }
3628             if (pThis->u.u_form.pFdata)
3629                 {
3630                 PushThisElement(pThis);
3631                 pThat = pThis;
3632                 pThis = pThis->u.u_form.pFdata;
3633                 pThat->u.u_form.pFdata = NULL;
3634                 break;
3635                 }
3636             if (pThis->u.u_form.pFstyle)
3637                 {
3638                 PushThisElement(pThis);
3639                 pThat = pThis;
3640                 pThis = pThis->u.u_form.pFstyle;
3641                 pThat->u.u_form.pFstyle = NULL;
3642                 break;
3643                 }
3644             if (pThis->pNext)
3645                 {
3646                 pThat = pThis;
3647                 pThis = pThat->pNext;
3648                 pThat->pNext = NULL;
3649                 DeleteElement(pThat);
3650                 CloseNamedTag("FORM", TRUE);
3651                 break;
3652                 }
3653             DeleteElement(pThis);
3654             CloseNamedTag("FORM", TRUE);
3655             pThis = PopThisElement();
3656             break;
3657         case e_fstyle:
3658             if (!pThis->beenEmitted)
3659                 {
3660                 OpenNamedTag("FSTYLE", TRUE, FALSE);
3661                 ImpliedAttribute("NCOLS", pThis->u.u_fstyle.ncols);
3662                 PutString(">\n");
3663                 pThis->beenEmitted = TRUE;
3664                 }
3665             if (pThis->u.u_fstyle.pFrowvec)
3666                 {
3667                 PushThisElement(pThis);
3668                 pThat = pThis;
3669                 pThis = pThis->u.u_fstyle.pFrowvec;
3670                 pThat->u.u_fstyle.pFrowvec = NULL;
3671                 break;
3672                 }
3673             if (pThis->pNext)
3674                 {
3675                 pThat = pThis;
3676                 pThis = pThat->pNext;
3677                 pThat->pNext = NULL;
3678                 DeleteElement(pThat);
3679                 CloseNamedTag("FSTYLE", TRUE);
3680                 break;
3681                 }
3682             DeleteElement(pThis);
3683             CloseNamedTag("FSTYLE", TRUE);
3684             pThis = PopThisElement();
3685             break;
3686         case e_frowvec:
3687             if (!pThis->beenEmitted)
3688                 {
3689                 OpenNamedTag("FROWVEC", TRUE, FALSE);
3690                 ImpliedAttribute("HDR", pThis->u.u_frowvec.hdr);
3691                 RequiredAttribute("CELLS", pThis->u.u_frowvec.cells);
3692                 PutString(">\n");
3693                 pThis->beenEmitted = TRUE;
3694                 }
3695             if (pThis->pNext)
3696                 {
3697                 pThat = pThis;
3698                 pThis = pThat->pNext;
3699                 pThat->pNext = NULL;
3700                 DeleteElement(pThat);
3701                 break;
3702                 }
3703             DeleteElement(pThis);
3704             pThis = PopThisElement();
3705             break;
3706         case e_fdata:
3707             if (!pThis->beenEmitted)
3708                 {
3709                 OpenNamedTag("FDATA", FALSE, TRUE);
3710                 pThis->beenEmitted = TRUE;
3711                 }
3712             if (pThis->u.u_fdata.pBlockOrForm)
3713                 {
3714                 PushThisElement(pThis);
3715                 pThat = pThis;
3716                 pThis = pThis->u.u_fdata.pBlockOrForm;
3717                 pThat->u.u_fdata.pBlockOrForm = NULL;
3718                 break;
3719                 }
3720             if (pThis->pNext)
3721                 {
3722                 pThat = pThis;
3723                 pThis = pThat->pNext;
3724                 pThat->pNext = NULL;
3725                 DeleteElement(pThat);
3726                 CloseNamedTag("FDATA", TRUE);
3727                 break;
3728                 }
3729             DeleteElement(pThis);
3730             CloseNamedTag("FDATA", TRUE);
3731             pThis = PopThisElement();
3732             break;
3733         case e_p:
3734             if (!pThis->beenEmitted)
3735                 {
3736                 OpenNamedTag("P", TRUE, FALSE);
3737                 ImpliedAttribute("ID", pThis->u.u_p.id);
3738                 AddToLOIDS("P",
3739                            pThis->u.u_p.id,
3740                            pThis->u.u_p.ssi,
3741                            NULL,
3742                            virpageOffset);
3743                 ImpliedAttribute("VERSION", pThis->u.u_p.version);
3744                 ImpliedAttribute("TYPE", pThis->u.u_p.type);
3745                 ImpliedAttribute("SSI", pThis->u.u_p.ssi);
3746                 PutString(">");
3747                 pThis->beenEmitted = TRUE;
3748                 }
3749             if (pThis->u.u_p.pHead)
3750                 {
3751                 PushThisElement(pThis);
3752                 pThat = pThis;
3753                 pThis = pThis->u.u_p.pHead;
3754                 pThat->u.u_p.pHead = NULL;
3755                 break;
3756                 }
3757             if (pThis->u.u_p.pAtomicOrPCDATA)
3758                 {
3759                 PushThisElement(pThis);
3760                 pThat = pThis;
3761                 pThis = pThis->u.u_p.pAtomicOrPCDATA;
3762                 pThat->u.u_p.pAtomicOrPCDATA = NULL;
3763                 break;
3764                 }
3765             if (pThis->pNext)
3766                 {
3767                 pThat = pThis;
3768                 pThis = pThat->pNext;
3769                 pThat->pNext = NULL;
3770                 DeleteElement(pThat);
3771                 CloseNamedTag("P", TRUE);
3772                 break;
3773                 }
3774             DeleteElement(pThis);
3775             CloseNamedTag("P", TRUE);
3776             pThis = PopThisElement();
3777             break;
3778         case e_cp:
3779             if (!pThis->beenEmitted)
3780                 {
3781                 OpenNamedTag("CP", TRUE, FALSE);
3782                 ImpliedAttribute("ID", pThis->u.u_cp.id);
3783                 AddToLOIDS("CP",
3784                            pThis->u.u_cp.id,
3785                            pThis->u.u_cp.ssi,
3786                            NULL,
3787                            virpageOffset);
3788                 ImpliedAttribute("VERSION", pThis->u.u_cp.version);
3789                 ImpliedAttribute("TYPE", pThis->u.u_cp.type);
3790                 ImpliedAttribute("SSI", pThis->u.u_cp.ssi);
3791                 PutString(">");
3792                 pThis->beenEmitted = TRUE;
3793                 }
3794             if (pThis->u.u_cp.pCDATA)
3795                 {
3796                 PushThisElement(pThis);
3797                 pThat = pThis;
3798                 pThis = pThis->u.u_cp.pCDATA;
3799                 pThat->u.u_cp.pCDATA = NULL;
3800                 break;
3801                 }
3802             if (pThis->pNext)
3803                 {
3804                 pThat = pThis;
3805                 pThis = pThat->pNext;
3806                 pThat->pNext = NULL;
3807                 DeleteElement(pThat);
3808                 CloseNamedTag("CP", TRUE);
3809                 break;
3810                 }
3811             DeleteElement(pThis);
3812             CloseNamedTag("CP", TRUE);
3813             pThis = PopThisElement();
3814             break;
3815         case e_head:
3816             if (!pThis->beenEmitted)
3817                 {
3818                 OpenNamedTag("HEAD", TRUE, FALSE);
3819                 ImpliedAttribute("ID", pThis->u.u_head.id);
3820                 AddToLOIDS("HEAD",
3821                            pThis->u.u_head.id,
3822                            pThis->u.u_head.ssi,
3823                            NULL,
3824                            virpageOffset);
3825                 ImpliedAttribute("VERSION", pThis->u.u_head.version);
3826                 ImpliedAttribute("CLASS", pThis->u.u_head.class);
3827                 ImpliedAttribute("LANGUAGE", pThis->u.u_head.language);
3828                 ImpliedAttribute("CHARSET", pThis->u.u_head.charset);
3829                 ImpliedAttribute("TYPE", pThis->u.u_head.type);
3830                 ImpliedAttribute("SSI", pThis->u.u_head.ssi);
3831                 ImpliedAttribute("ABBREV", pThis->u.u_head.abbrev);
3832                 PutString(">");
3833                 pThis->beenEmitted = TRUE;
3834                 }
3835             if (pThis->u.u_head.pAtomicOrPCDATA)
3836                 {
3837                 PushThisElement(pThis);
3838                 pThat = pThis;
3839                 pThis = pThis->u.u_head.pAtomicOrPCDATA;
3840                 pThat->u.u_head.pAtomicOrPCDATA = NULL;
3841                 break;
3842                 }
3843             if (pThis->u.u_head.pSubhead)
3844                 {
3845                 PushThisElement(pThis);
3846                 pThat = pThis;
3847                 pThis = pThis->u.u_head.pSubhead;
3848                 pThat->u.u_head.pSubhead = NULL;
3849                 break;
3850                 }
3851             if (pThis->pNext)
3852                 {
3853                 pThat = pThis;
3854                 pThis = pThat->pNext;
3855                 pThat->pNext = NULL;
3856                 DeleteElement(pThat);
3857                 pThat = PopThisElement();
3858                 if (pThat->type == e_p)
3859                     CloseNamedTag("HEAD", FALSE);
3860                 else
3861                     CloseNamedTag("HEAD", TRUE);
3862                 PushThisElement(pThat);
3863                 break;
3864                 }
3865             DeleteElement(pThis);
3866             pThis = PopThisElement();
3867             if (pThis->type == e_p)
3868                 CloseNamedTag("HEAD", FALSE);
3869             else
3870                 CloseNamedTag("HEAD", TRUE);
3871             break;
3872         case e_subhead:
3873             if (!pThis->beenEmitted)
3874                 {
3875                 OpenNamedTag("SUBHEAD", TRUE, FALSE);
3876                 ImpliedAttribute("ID", pThis->u.u_subhead.id);
3877                 AddToLOIDS("SUBHEAD",
3878                            pThis->u.u_subhead.id,
3879                            pThis->u.u_subhead.ssi,
3880                            NULL,
3881                            virpageOffset);
3882                 ImpliedAttribute("VERSION", pThis->u.u_subhead.version);
3883                 ImpliedAttribute("CLASS", pThis->u.u_subhead.class);
3884                 ImpliedAttribute("LANGUAGE", pThis->u.u_subhead.language);
3885                 ImpliedAttribute("CHARSET", pThis->u.u_subhead.charset);
3886                 ImpliedAttribute("TYPE", pThis->u.u_subhead.type);
3887                 ImpliedAttribute("SSI", pThis->u.u_subhead.ssi);
3888                 ImpliedAttribute("ABBREV", pThis->u.u_subhead.abbrev);
3889                 PutString(">");
3890                 pThis->beenEmitted = TRUE;
3891                 }
3892             if (pThis->u.u_subhead.pAtomicOrPCDATA)
3893                 {
3894                 PushThisElement(pThis);
3895                 pThat = pThis;
3896                 pThis = pThis->u.u_subhead.pAtomicOrPCDATA;
3897                 pThat->u.u_subhead.pAtomicOrPCDATA = NULL;
3898                 break;
3899                 }
3900             if (pThis->pNext)
3901                 {
3902                 pThat = pThis;
3903                 pThis = pThat->pNext;
3904                 pThat->pNext = NULL;
3905                 DeleteElement(pThat);
3906                 CloseNamedTag("SUBHEAD", FALSE);
3907                 break;
3908                 }
3909             DeleteElement(pThis);
3910             CloseNamedTag("SUBHEAD", FALSE);
3911             pThis = PopThisElement();
3912             break;
3913         case e_anchor:
3914             if (!pThis->beenEmitted)
3915                 {
3916                 OpenNamedTag("ANCHOR", TRUE, FALSE);
3917                 RequiredAttribute("ID", pThis->u.u_anchor.id);
3918                 AddToLOIDS("ANCHOR",
3919                            pThis->u.u_anchor.id,
3920                            NULL,
3921                            NULL,
3922                            virpageOffset);
3923                 PutString(">");
3924                 pThis->beenEmitted = TRUE;
3925                 }
3926             if (pThis->pNext)
3927                 {
3928                 pThat = pThis;
3929                 pThis = pThat->pNext;
3930                 pThat->pNext = NULL;
3931                 DeleteElement(pThat);
3932                 break;
3933                 }
3934             DeleteElement(pThis);
3935             pThis = PopThisElement();
3936             break;
3937         case e_link:
3938             if (!pThis->beenEmitted)
3939                 {
3940                 OpenNamedTag("LINK", TRUE, FALSE);
3941                 RequiredAttribute("RID", pThis->u.u_link.rid);
3942                 ImpliedAttribute("BUTTON", pThis->u.u_link.button);
3943                 ImpliedAttribute("LINKINFO", pThis->u.u_link.linkinfo);
3944                 ImpliedAttribute("DESCRIPT", pThis->u.u_link.descript);
3945                 ImpliedAttribute("WINDOW", pThis->u.u_link.window);
3946                 ImpliedAttribute("TRAVERSAL", pThis->u.u_link.traversal);
3947                 PutString(">");
3948                 pThis->beenEmitted = TRUE;
3949                 }
3950             if (pThis->u.u_link.pAtomicOrPCDATA)
3951                 {
3952                 PushThisElement(pThis);
3953                 pThat = pThis;
3954                 pThis = pThis->u.u_link.pAtomicOrPCDATA;
3955                 pThat->u.u_link.pAtomicOrPCDATA = NULL;
3956                 break;
3957                 }
3958             if (pThis->pNext)
3959                 {
3960                 pThat = pThis;
3961                 pThis = pThat->pNext;
3962                 pThat->pNext = NULL;
3963                 DeleteElement(pThat);
3964                 CloseNamedTag("LINK", FALSE);
3965                 break;
3966                 }
3967             DeleteElement(pThis);
3968             CloseNamedTag("LINK", FALSE);
3969             pThis = PopThisElement();
3970             break;
3971         case e_snref:
3972             if (!pThis->beenEmitted)
3973                 {
3974                 OpenNamedTag("SNREF", TRUE, FALSE);
3975                 ImpliedAttribute("ID", pThis->u.u_snref.id);
3976                 AddToLOIDS("SNREF",
3977                            pThis->u.u_snref.id,
3978                            NULL,
3979                            NULL,
3980                            virpageOffset);
3981                 PutString(">");
3982                 pThis->beenEmitted = TRUE;
3983                 }
3984             if (pThis->u.u_snref.pRefitem)
3985                 {
3986                 PushThisElement(pThis);
3987                 pThat = pThis;
3988                 pThis = pThis->u.u_snref.pRefitem;
3989                 pThat->u.u_snref.pRefitem = NULL;
3990                 break;
3991                 }
3992             if (pThis->u.u_snref.pAlttext)
3993                 {
3994                 PushThisElement(pThis);
3995                 pThat = pThis;
3996                 pThis = pThis->u.u_snref.pAlttext;
3997                 pThat->u.u_snref.pAlttext = NULL;
3998                 break;
3999                 }
4000             if (pThis->pNext)
4001                 {
4002                 pThat = pThis;
4003                 pThis = pThat->pNext;
4004                 pThat->pNext = NULL;
4005                 DeleteElement(pThat);
4006                 CloseNamedTag("SNREF", FALSE);
4007                 break;
4008                 }
4009             DeleteElement(pThis);
4010             CloseNamedTag("SNREF", FALSE);
4011             pThis = PopThisElement();
4012             break;
4013         case e_refitem:
4014             if (!pThis->beenEmitted)
4015                 {
4016                 OpenNamedTag("REFITEM", TRUE, FALSE);
4017                 RequiredAttribute("RID", pThis->u.u_refitem.rid);
4018                 RequiredAttribute("CLASS", pThis->u.u_refitem.class);
4019                 ImpliedAttribute("BUTTON", pThis->u.u_refitem.button);
4020                 ImpliedAttribute("SSI", pThis->u.u_refitem.ssi);
4021                 PutString(">\n");
4022                 pThis->beenEmitted = TRUE;
4023                 }
4024             if (pThis->u.u_refitem.pHead)
4025                 {
4026                 PushThisElement(pThis);
4027                 pThat = pThis;
4028                 pThis = pThis->u.u_refitem.pHead;
4029                 pThat->u.u_refitem.pHead = NULL;
4030                 break;
4031                 }
4032             if (pThis->pNext)
4033                 {
4034                 pThat = pThis;
4035                 pThis = pThat->pNext;
4036                 pThat->pNext = NULL;
4037                 DeleteElement(pThat);
4038                 CloseNamedTag("REFITEM", TRUE);
4039                 break;
4040                 }
4041             DeleteElement(pThis);
4042             CloseNamedTag("REFITEM", TRUE);
4043             pThis = PopThisElement();
4044             break;
4045         case e_alttext:
4046             if (!pThis->beenEmitted)
4047                 {
4048                 OpenNamedTag("ALTTEXT", FALSE, FALSE);
4049                 pThis->beenEmitted = TRUE;
4050                 }
4051             if (pThis->u.u_alttext.pCDATA)
4052                 {
4053                 PushThisElement(pThis);
4054                 pThat = pThis;
4055                 pThis = pThis->u.u_alttext.pCDATA;
4056                 pThat->u.u_alttext.pCDATA = NULL;
4057                 break;
4058                 }
4059             if (pThis->pNext)
4060                 {
4061                 pThat = pThis;
4062                 pThis = pThat->pNext;
4063                 pThat->pNext = NULL;
4064                 DeleteElement(pThat);
4065                 CloseNamedTag("ALTTEXT", TRUE);
4066                 break;
4067                 }
4068             DeleteElement(pThis);
4069             CloseNamedTag("ALTTEXT", TRUE);
4070             pThis = PopThisElement();
4071             break;
4072         case e_rev:
4073             if (!pThis->beenEmitted)
4074                 {
4075                 OpenNamedTag("REV", FALSE, FALSE);
4076                 pThis->beenEmitted = TRUE;
4077                 }
4078             if (pThis->u.u_rev.pAtomicOrPCDATA)
4079                 {
4080                 PushThisElement(pThis);
4081                 pThat = pThis;
4082                 pThis = pThis->u.u_rev.pAtomicOrPCDATA;
4083                 pThat->u.u_rev.pAtomicOrPCDATA = NULL;
4084                 break;
4085                 }
4086             if (pThis->pNext)
4087                 {
4088                 pThat = pThis;
4089                 pThis = pThat->pNext;
4090                 pThat->pNext = NULL;
4091                 DeleteElement(pThat);
4092                 CloseNamedTag("REV", FALSE);
4093                 break;
4094                 }
4095             DeleteElement(pThis);
4096             CloseNamedTag("REV", FALSE);
4097             pThis = PopThisElement();
4098             break;
4099         case e_key:
4100             if (!pThis->beenEmitted)
4101                 {
4102                 OpenNamedTag("KEY", TRUE, FALSE);
4103                 ImpliedAttribute("CHARSET", pThis->u.u_key.charset);
4104                 RequiredAttribute("CLASS", pThis->u.u_key.class);
4105                 ImpliedAttribute("SSI", pThis->u.u_key.ssi);
4106                 PutString(">");
4107                 pThis->beenEmitted = TRUE;
4108                 }
4109             if (pThis->u.u_key.pAtomicOrPCDATA)
4110                 {
4111                 PushThisElement(pThis);
4112                 pThat = pThis;
4113                 pThis = pThis->u.u_key.pAtomicOrPCDATA;
4114                 pThat->u.u_key.pAtomicOrPCDATA = NULL;
4115                 break;
4116                 }
4117             if (pThis->pNext)
4118                 {
4119                 pThat = pThis;
4120                 pThis = pThat->pNext;
4121                 pThat->pNext = NULL;
4122                 DeleteElement(pThat);
4123                 CloseNamedTag("KEY", FALSE);
4124                 break;
4125                 }
4126             DeleteElement(pThis);
4127             CloseNamedTag("KEY", FALSE);
4128             pThis = PopThisElement();
4129             break;
4130         case e_sphrase:
4131             if (!pThis->beenEmitted)
4132                 {
4133                 OpenNamedTag("SPHRASE", TRUE, FALSE);
4134                 RequiredAttribute("CLASS", pThis->u.u_sphrase.class);
4135                 ImpliedAttribute("SSI", pThis->u.u_sphrase.ssi);
4136                 PutString(">");
4137                 pThis->beenEmitted = TRUE;
4138                 }
4139             if (pThis->u.u_sphrase.pSpcOrPCDATA) 
4140                 {
4141                 PushThisElement(pThis);
4142                 pThat = pThis;
4143                 pThis = pThis->u.u_sphrase.pSpcOrPCDATA;
4144                 pThat->u.u_sphrase.pSpcOrPCDATA = NULL;
4145                 break;
4146                 }
4147             if (pThis->pNext)
4148                 {
4149                 pThat = pThis;
4150                 pThis = pThat->pNext;
4151                 pThat->pNext = NULL;
4152                 DeleteElement(pThat);
4153                 CloseNamedTag("SPHRASE", FALSE);
4154                 break;
4155                 }
4156             DeleteElement(pThis);
4157             CloseNamedTag("SPHRASE", FALSE);
4158             pThis = PopThisElement();
4159             break;
4160         case e_if:
4161             if (!pThis->beenEmitted)
4162                 {
4163                 OpenNamedTag("IF", FALSE, FALSE);
4164                 pThis->beenEmitted = TRUE;
4165                 }
4166             if (pThis->u.u_if.pCond)
4167                 {
4168                 PushThisElement(pThis);
4169                 pThat = pThis;
4170                 pThis = pThis->u.u_if.pCond;
4171                 pThat->u.u_if.pCond = NULL;
4172                 break;
4173                 }
4174             if (pThis->u.u_if.pThen)
4175                 {
4176                 PushThisElement(pThis);
4177                 pThat = pThis;
4178                 pThis = pThis->u.u_if.pThen;
4179                 pThat->u.u_if.pThen = NULL;
4180                 break;
4181                 }
4182             if (pThis->u.u_if.pElse)
4183                 {
4184                 PushThisElement(pThis);
4185                 pThat = pThis;
4186                 pThis = pThis->u.u_if.pElse;
4187                 pThat->u.u_if.pElse = NULL;
4188                 break;
4189                 }
4190             if (pThis->pNext)
4191                 {
4192                 pThat = pThis;
4193                 pThis = pThat->pNext;
4194                 pThat->pNext = NULL;
4195                 DeleteElement(pThat);
4196                 CloseNamedTag("IF", FALSE);
4197                 break;
4198                 }
4199             DeleteElement(pThis);
4200             CloseNamedTag("IF", FALSE);
4201             pThis = PopThisElement();
4202             break;
4203         case e_cond:
4204             if (!pThis->beenEmitted)
4205                 {
4206                 OpenNamedTag("COND", TRUE, FALSE);
4207                 ImpliedAttribute("INTERP", pThis->u.u_cond.interp);
4208                 PutString(">");
4209                 pThis->beenEmitted = TRUE;
4210                 }
4211             if (pThis->u.u_cond.pCDATA)
4212                 {
4213                 PushThisElement(pThis);
4214                 pThat = pThis;
4215                 pThis = pThis->u.u_cond.pCDATA;
4216                 pThat->u.u_cond.pCDATA = NULL;
4217                 break;
4218                 }
4219             if (pThis->pNext)
4220                 {
4221                 pThat = pThis;
4222                 pThis = pThat->pNext;
4223                 pThat->pNext = NULL;
4224                 DeleteElement(pThat);
4225                 CloseNamedTag("COND", TRUE);
4226                 break;
4227                 }
4228             DeleteElement(pThis);
4229             CloseNamedTag("COND", TRUE);
4230             pThis = PopThisElement();
4231             break;
4232         case e_then:
4233             if (!pThis->beenEmitted)
4234                 {
4235                 OpenNamedTag("THEN", FALSE, FALSE);
4236                 pThis->beenEmitted = TRUE;
4237                 }
4238             if (pThis->u.u_then.pAtomicOrPCDATA)
4239                 {
4240                 PushThisElement(pThis);
4241                 pThat = pThis;
4242                 pThis = pThis->u.u_then.pAtomicOrPCDATA;
4243                 pThat->u.u_then.pAtomicOrPCDATA = NULL;
4244                 break;
4245                 }
4246             if (pThis->pNext)
4247                 {
4248                 pThat = pThis;
4249                 pThis = pThat->pNext;
4250                 pThat->pNext = NULL;
4251                 DeleteElement(pThat);
4252                 CloseNamedTag("THEN", TRUE);
4253                 break;
4254                 }
4255             DeleteElement(pThis);
4256             CloseNamedTag("THEN", TRUE);
4257             pThis = PopThisElement();
4258             break;
4259         case e_else:
4260             if (!pThis->beenEmitted)
4261                 {
4262                 OpenNamedTag("ELSE", FALSE, FALSE);
4263                 pThis->beenEmitted = TRUE;
4264                 }
4265             if (pThis->u.u_else.pAtomicOrPCDATA)
4266                 {
4267                 PushThisElement(pThis);
4268                 pThat = pThis;
4269                 pThis = pThis->u.u_else.pAtomicOrPCDATA;
4270                 pThat->u.u_else.pAtomicOrPCDATA = NULL;
4271                 break;
4272                 }
4273             if (pThis->pNext)
4274                 {
4275                 pThat = pThis;
4276                 pThis = pThat->pNext;
4277                 pThat->pNext = NULL;
4278                 DeleteElement(pThat);
4279                 CloseNamedTag("ELSE", TRUE);
4280                 break;
4281                 }
4282             DeleteElement(pThis);
4283             CloseNamedTag("ELSE", TRUE);
4284             pThis = PopThisElement();
4285             break;
4286         case e_spc:
4287             if (!pThis->beenEmitted)
4288                 {
4289                 OpenNamedTag("SPC", TRUE, FALSE);
4290                 RequiredAttribute("NAME", pThis->u.u_spc.name);
4291                 PutString(">");
4292                 pThis->beenEmitted = TRUE;
4293                 }
4294             if (pThis->pNext)
4295                 {
4296                 pThat = pThis;
4297                 pThis = pThat->pNext;
4298                 pThat->pNext = NULL;
4299                 DeleteElement(pThat);
4300                 break;
4301                 }
4302             DeleteElement(pThis);
4303             pThis = PopThisElement();
4304             break;
4305         case e_graphic:
4306             if (!pThis->beenEmitted)
4307                 {
4308                 OpenNamedTag("GRAPHIC", TRUE, FALSE);
4309                 RequiredAttribute("ID", pThis->u.u_graphic.id);
4310                 AddToLOIDS("GRAPHIC",
4311                            pThis->u.u_graphic.id,
4312                            NULL,
4313                            NULL,
4314                            virpageOffset);
4315                 ImpliedAttribute("FORMAT", pThis->u.u_graphic.format);
4316                 ImpliedAttribute("METHOD", pThis->u.u_graphic.method);
4317                 RequiredAttribute("XID", pThis->u.u_graphic.xid);
4318                 PutString(">\n");
4319                 pThis->beenEmitted = TRUE;
4320                 }
4321             if (pThis->pNext)
4322                 {
4323                 pThat = pThis;
4324                 pThis = pThat->pNext;
4325                 pThat->pNext = NULL;
4326                 DeleteElement(pThat);
4327                 break;
4328                 }
4329             DeleteElement(pThis);
4330             pThis = PopThisElement();
4331             break;
4332         case e_text:
4333             if (!pThis->beenEmitted)
4334                 {
4335                 OpenNamedTag("TEXT", TRUE, FALSE);
4336                 RequiredAttribute("ID", pThis->u.u_text.id);
4337                 AddToLOIDS("TEXT",
4338                            pThis->u.u_text.id,
4339                            NULL,
4340                            NULL,
4341                            virpageOffset);
4342                 ImpliedAttribute("LANGUAGE", pThis->u.u_text.language);
4343                 ImpliedAttribute("CHARSET", pThis->u.u_text.charset);
4344                 PutString(">\n");
4345                 pThis->beenEmitted = TRUE;
4346                 }
4347             if (pThis->u.u_text.pCpOrP)
4348                 {
4349                 PushThisElement(pThis);
4350                 pThat = pThis;
4351                 pThis = pThis->u.u_text.pCpOrP;
4352                 pThat->u.u_text.pCpOrP = NULL;
4353                 break;
4354                 }
4355             if (pThis->pNext)
4356                 {
4357                 pThat = pThis;
4358                 pThis = pThat->pNext;
4359                 pThat->pNext = NULL;
4360                 DeleteElement(pThat);
4361                 CloseNamedTag("TEXT", TRUE);
4362                 break;
4363                 }
4364             DeleteElement(pThis);
4365             CloseNamedTag("TEXT", TRUE);
4366             pThis = PopThisElement();
4367             break;
4368         case e_audio:
4369             if (!pThis->beenEmitted)
4370                 {
4371                 OpenNamedTag("AUDIO", TRUE, FALSE);
4372                 RequiredAttribute("ID", pThis->u.u_audio.id);
4373                 AddToLOIDS("AUDIO",
4374                            pThis->u.u_audio.id,
4375                            NULL,
4376                            NULL,
4377                            virpageOffset);
4378                 ImpliedAttribute("FORMAT", pThis->u.u_audio.format);
4379                 ImpliedAttribute("METHOD", pThis->u.u_audio.method);
4380                 RequiredAttribute("XID", pThis->u.u_audio.xid);
4381                 PutString(">\n");
4382                 pThis->beenEmitted = TRUE;
4383                 }
4384             if (pThis->pNext)
4385                 {
4386                 pThat = pThis;
4387                 pThis = pThat->pNext;
4388                 pThat->pNext = NULL;
4389                 DeleteElement(pThat);
4390                 break;
4391                 }
4392             DeleteElement(pThis);
4393             pThis = PopThisElement();
4394             break;
4395         case e_video:
4396             if (!pThis->beenEmitted)
4397                 {
4398                 OpenNamedTag("VIDEO", TRUE, FALSE);
4399                 RequiredAttribute("ID", pThis->u.u_video.id);
4400                 AddToLOIDS("VIDEO",
4401                            pThis->u.u_video.id,
4402                            NULL,
4403                            NULL,
4404                            virpageOffset);
4405                 ImpliedAttribute("FORMAT", pThis->u.u_video.format);
4406                 ImpliedAttribute("METHOD", pThis->u.u_video.method);
4407                 RequiredAttribute("XID", pThis->u.u_video.xid);
4408                 PutString(">\n");
4409                 pThis->beenEmitted = TRUE;
4410                 }
4411             if (pThis->pNext)
4412                 {
4413                 pThat = pThis;
4414                 pThis = pThat->pNext;
4415                 pThat->pNext = NULL;
4416                 DeleteElement(pThat);
4417                 break;
4418                 }
4419             DeleteElement(pThis);
4420             pThis = PopThisElement();
4421             break;
4422         case e_animate:
4423             if (!pThis->beenEmitted)
4424                 {
4425                 OpenNamedTag("ANIMATE", TRUE, FALSE);
4426                 RequiredAttribute("ID", pThis->u.u_animate.id);
4427                 AddToLOIDS("ANIMATE",
4428                            pThis->u.u_animate.id,
4429                            NULL,
4430                            NULL,
4431                            virpageOffset);
4432                 ImpliedAttribute("FORMAT", pThis->u.u_animate.format);
4433                 ImpliedAttribute("METHOD", pThis->u.u_animate.method);
4434                 RequiredAttribute("XID", pThis->u.u_animate.xid);
4435                 PutString(">\n");
4436                 pThis->beenEmitted = TRUE;
4437                 }
4438             if (pThis->pNext)
4439                 {
4440                 pThat = pThis;
4441                 pThis = pThat->pNext;
4442                 pThat->pNext = NULL;
4443                 DeleteElement(pThat);
4444                 break;
4445                 }
4446             DeleteElement(pThis);
4447             pThis = PopThisElement();
4448             break;
4449         case e_script:
4450             if (!pThis->beenEmitted)
4451                 {
4452                 OpenNamedTag("SCRIPT", TRUE, FALSE);
4453                 RequiredAttribute("ID", pThis->u.u_script.id);
4454                 AddToLOIDS("SCRIPT",
4455                            pThis->u.u_script.id,
4456                            NULL,
4457                            NULL,
4458                            virpageOffset);
4459                 ImpliedAttribute("INTERP", pThis->u.u_script.interp);
4460                 PutString(">\n");
4461                 pThis->beenEmitted = TRUE;
4462                 }
4463             if (pThis->u.u_script.pCDATA)
4464                 {
4465                 PushThisElement(pThis);
4466                 pThat = pThis;
4467                 pThis = pThis->u.u_script.pCDATA;
4468                 pThat->u.u_script.pCDATA = NULL;
4469                 break;
4470                 }
4471             if (pThis->pNext)
4472                 {
4473                 pThat = pThis;
4474                 pThis = pThat->pNext;
4475                 pThat->pNext = NULL;
4476                 DeleteElement(pThat);
4477                 CloseNamedTag("SCRIPT", TRUE);
4478                 break;
4479                 }
4480             DeleteElement(pThis);
4481             CloseNamedTag("SCRIPT", TRUE);
4482             pThis = PopThisElement();
4483             break;
4484         case e_crossdoc:
4485             if (!pThis->beenEmitted)
4486                 {
4487                 OpenNamedTag("CROSSDOC", TRUE, FALSE);
4488                 RequiredAttribute("ID", pThis->u.u_crossdoc.id);
4489                 AddToLOIDS("CROSSDOC",
4490                            pThis->u.u_crossdoc.id,
4491                            NULL,
4492                            NULL,
4493                            virpageOffset);
4494                 RequiredAttribute("XID", pThis->u.u_crossdoc.xid);
4495                 PutString(">\n");
4496                 pThis->beenEmitted = TRUE;
4497                 }
4498             if (pThis->pNext)
4499                 {
4500                 pThat = pThis;
4501                 pThis = pThat->pNext;
4502                 pThat->pNext = NULL;
4503                 DeleteElement(pThat);
4504                 break;
4505                 }
4506             DeleteElement(pThis);
4507             pThis = PopThisElement();
4508             break;
4509         case e_man_page:
4510             if (!pThis->beenEmitted)
4511                 {
4512                 OpenNamedTag("MAN-PAGE", TRUE, FALSE);
4513                 RequiredAttribute("ID", pThis->u.u_man_page.id);
4514                 AddToLOIDS("MAN-PAGE",
4515                            pThis->u.u_man_page.id,
4516                            NULL,
4517                            NULL,
4518                            virpageOffset);
4519                 RequiredAttribute("XID", pThis->u.u_man_page.xid);
4520                 PutString(">\n");
4521                 pThis->beenEmitted = TRUE;
4522                 }
4523             if (pThis->pNext)
4524                 {
4525                 pThat = pThis;
4526                 pThis = pThat->pNext;
4527                 pThat->pNext = NULL;
4528                 DeleteElement(pThat);
4529                 break;
4530                 }
4531             DeleteElement(pThis);
4532             pThis = PopThisElement();
4533             break;
4534         case e_textfile:
4535             if (!pThis->beenEmitted)
4536                 {
4537                 OpenNamedTag("TEXTFILE", TRUE, FALSE);
4538                 RequiredAttribute("ID", pThis->u.u_textfile.id);
4539                 AddToLOIDS("TEXTFILE",
4540                            pThis->u.u_textfile.id,
4541                            NULL,
4542                            NULL,
4543                            virpageOffset);
4544                 RequiredAttribute("XID", pThis->u.u_textfile.xid);
4545                 ImpliedAttribute("OFFSET", pThis->u.u_textfile.offset);
4546                 ImpliedAttribute("FORMAT", pThis->u.u_textfile.format);
4547                 PutString(">\n");
4548                 pThis->beenEmitted = TRUE;
4549                 }
4550             if (pThis->pNext)
4551                 {
4552                 pThat = pThis;
4553                 pThis = pThat->pNext;
4554                 pThat->pNext = NULL;
4555                 DeleteElement(pThat);
4556                 break;
4557                 }
4558             DeleteElement(pThis);
4559             pThis = PopThisElement();
4560             break;
4561         case e_sys_cmd:
4562             if (!pThis->beenEmitted)
4563                 {
4564                 OpenNamedTag("SYS-CMD", TRUE, FALSE);
4565                 RequiredAttribute("ID", pThis->u.u_sys_cmd.id);
4566                 AddToLOIDS("SYS-CMD",
4567                            pThis->u.u_sys_cmd.id,
4568                            NULL,
4569                            NULL,
4570                            virpageOffset);
4571                 RequiredAttribute("COMMAND", pThis->u.u_sys_cmd.command);
4572                 PutString(">\n");
4573                 pThis->beenEmitted = TRUE;
4574                 }
4575             if (pThis->pNext)
4576                 {
4577                 pThat = pThis;
4578                 pThis = pThat->pNext;
4579                 pThat->pNext = NULL;
4580                 DeleteElement(pThat);
4581                 break;
4582                 }
4583             DeleteElement(pThis);
4584             pThis = PopThisElement();
4585             break;
4586         case e_callback:
4587             if (!pThis->beenEmitted)
4588                 {
4589                 OpenNamedTag("CALLBACK", TRUE, FALSE);
4590                 RequiredAttribute("ID", pThis->u.u_callback.id);
4591                 AddToLOIDS("CALLBACK",
4592                            pThis->u.u_callback.id,
4593                            NULL,
4594                            NULL,
4595                            virpageOffset);
4596                 ImpliedAttribute("DATA", pThis->u.u_callback.data);
4597                 PutString(">\n");
4598                 pThis->beenEmitted = TRUE;
4599                 }
4600             if (pThis->pNext)
4601                 {
4602                 pThat = pThis;
4603                 pThis = pThat->pNext;
4604                 pThat->pNext = NULL;
4605                 DeleteElement(pThat);
4606                 break;
4607                 }
4608             DeleteElement(pThis);
4609             pThis = PopThisElement();
4610             break;
4611         case e_switch:
4612             if (!pThis->beenEmitted)
4613                 {
4614                 OpenNamedTag("SWITCH", TRUE, FALSE);
4615                 RequiredAttribute("ID", pThis->u.u_switch.id);
4616                 AddToLOIDS("SWITCH",
4617                            pThis->u.u_switch.id,
4618                            NULL,
4619                            NULL,
4620                            virpageOffset);
4621                 ImpliedAttribute("INTERP", pThis->u.u_switch.interp);
4622                 RequiredAttribute("BRANCHES", pThis->u.u_switch.branches);
4623                 PutString(">\n");
4624                 pThis->beenEmitted = TRUE;
4625             }
4626             if (pThis->u.u_switch.pCDATA)
4627                 {
4628                 PushThisElement(pThis);
4629                 pThat = pThis;
4630                 pThis = pThis->u.u_switch.pCDATA;
4631                 pThat->u.u_switch.pCDATA = NULL;
4632                 break;
4633                 }
4634             if (pThis->pNext)
4635                 {
4636                 pThat = pThis;
4637                 pThis = pThat->pNext;
4638                 pThat->pNext = NULL;
4639                 DeleteElement(pThat);
4640                 CloseNamedTag("SWITCH", TRUE);
4641                 break;
4642                 }
4643             DeleteElement(pThis);
4644             CloseNamedTag("SWITCH", TRUE);
4645             pThis = PopThisElement();
4646             break;
4647         case e_cdata:
4648             pCurrentElement = pThis;
4649             SaveWChar(0);
4650             PutWString(pThis->u.u_cdata.buffer);
4651             if (pThis->pNext)
4652                 {
4653                 pThat = pThis;
4654                 pThis = pThat->pNext;
4655                 pThat->pNext = NULL;
4656                 DeleteElement(pThat);
4657                 break;
4658                 }
4659             DeleteElement(pThis);
4660             pThis = PopThisElement();
4661             break;
4662         case e_entry:
4663             if (!pThis->beenEmitted)
4664                 {
4665                 OpenNamedTag("ENTRY", TRUE, FALSE);
4666                 ImpliedAttribute("ID", pThis->u.u_entry.id);
4667                 ImpliedAttribute("MAIN", pThis->u.u_entry.main);
4668                 ImpliedAttribute("LOCS", pThis->u.u_entry.locs);
4669                 ImpliedAttribute("SYNS", pThis->u.u_entry.syns);
4670                 ImpliedAttribute("SORT", pThis->u.u_entry.sort);
4671                 PutString(">");
4672                 pThis->beenEmitted = TRUE;
4673                 }
4674             if (pThis->u.u_entry.pSimpleOrPCDATA)
4675                 {
4676                 PushThisElement(pThis);
4677                 pThat = pThis;
4678                 pThis = pThis->u.u_entry.pSimpleOrPCDATA;
4679                 pThat->u.u_entry.pSimpleOrPCDATA = NULL;
4680                 break;
4681                 }
4682             if (pThis->u.u_entry.pEntry)
4683                 {
4684                 PushThisElement(pThis);
4685                 pThat = pThis;
4686                 pThis = pThis->u.u_entry.pEntry;
4687                 pThat->u.u_entry.pEntry = NULL;
4688                 break;
4689                 }
4690             if (pThis->pNext)
4691                 {
4692                 pThat = pThis;
4693                 pThis = pThat->pNext;
4694                 pThat->pNext = NULL;
4695                 DeleteElement(pThat);
4696                 CloseNamedTag("ENTRY", TRUE);
4697                 break;
4698                 }
4699             DeleteElement(pThis);
4700             CloseNamedTag("ENTRY", TRUE);
4701             pThis = PopThisElement();
4702             break;
4703         case e_phrase:
4704             OpenNamedTag("PHRASE", TRUE, FALSE);
4705             RequiredAttribute("TEXT", pThis->u.u_phrase.text);
4706             PutString(">\n");
4707             pThat = pThis;
4708             pThis = pThat->pNext;
4709             DeleteElement(pThat);
4710             break;
4711         case e_keystyle:
4712             if (minimalTossFlag && !pThis->u.u_keystyle.beenUsed)
4713                 {
4714                 pThat = pThis;
4715                 pThis = pThat->pNext;
4716                 pThat->pNext = NULL;
4717                 DeleteElement(pThat);
4718                 break;
4719                 }
4720             OpenNamedTag("KEYSTYLE", TRUE, FALSE);
4721             RequiredAttribute("CLASS", pThis->u.u_keystyle.class);
4722             ImpliedAttribute("SSI", pThis->u.u_keystyle.ssi);
4723             ImpliedAttribute("RLEVEL", pThis->u.u_keystyle.rlevel);
4724             ImpliedAttribute("PHRASE", pThis->u.u_keystyle.phrase);
4725             ImpliedAttribute("SRCH-WT", pThis->u.u_keystyle.srch_wt);
4726             ImpliedAttribute("ENTER", pThis->u.u_keystyle.enter);
4727             ImpliedAttribute("EXIT", pThis->u.u_keystyle.exit);
4728             ImpliedAttribute("POINTSZ", pThis->u.u_keystyle.pointsz);
4729             ImpliedAttribute("SET-WIDTH", pThis->u.u_keystyle.set_width);
4730             ImpliedAttribute("COLOR", pThis->u.u_keystyle.color);
4731             ImpliedAttribute("XLFD", pThis->u.u_keystyle.xlfd);
4732             ImpliedAttribute("XLFDI", pThis->u.u_keystyle.xlfdi);
4733             ImpliedAttribute("XLFDB", pThis->u.u_keystyle.xlfdb);
4734             ImpliedAttribute("XLFDIB", pThis->u.u_keystyle.xlfdib);
4735             ImpliedAttribute("TYPENAM", pThis->u.u_keystyle.typenam);
4736             ImpliedAttribute("TYPENAMI", pThis->u.u_keystyle.typenami);
4737             ImpliedAttribute("TYPENAMB", pThis->u.u_keystyle.typenamb);
4738             ImpliedAttribute("TYPENAMIB", pThis->u.u_keystyle.typenamib);
4739             ImpliedAttribute("STYLE", pThis->u.u_keystyle.style);
4740             ImpliedAttribute("SPACING", pThis->u.u_keystyle.spacing);
4741             ImpliedAttribute("WEIGHT", pThis->u.u_keystyle.weight);
4742             ImpliedAttribute("SLANT", pThis->u.u_keystyle.slant);
4743             ImpliedAttribute("SPECIAL", pThis->u.u_keystyle.special);
4744             PutString(">\n");
4745             pThat = pThis;
4746             pThis = pThat->pNext;
4747             pThat->pNext = NULL;
4748             DeleteElement(pThat);
4749             break;
4750         case e_headstyle:
4751             if (minimalTossFlag && !pThis->u.u_headstyle.beenUsed)
4752                 {
4753                 pThat = pThis;
4754                 pThis = pThat->pNext;
4755                 pThat->pNext = NULL;
4756                 DeleteElement(pThat);
4757                 break;
4758                 }
4759             OpenNamedTag("HEADSTYLE", TRUE, FALSE);
4760             RequiredAttribute("CLASS", pThis->u.u_headstyle.class);
4761             ImpliedAttribute("SSI", pThis->u.u_headstyle.ssi);
4762             ImpliedAttribute("RLEVEL", pThis->u.u_headstyle.rlevel);
4763             ImpliedAttribute("PHRASE", pThis->u.u_headstyle.phrase);
4764             ImpliedAttribute("SRCH-WT", pThis->u.u_headstyle.srch_wt);
4765             ImpliedAttribute("ORIENT", pThis->u.u_headstyle.orient);
4766             ImpliedAttribute("VORIENT", pThis->u.u_headstyle.vorient);
4767             ImpliedAttribute("PLACEMENT", pThis->u.u_headstyle.placement);
4768             ImpliedAttribute("HEADW", pThis->u.u_headstyle.headw);
4769             ImpliedAttribute("STACK", pThis->u.u_headstyle.stack);
4770             ImpliedAttribute("FLOW", pThis->u.u_headstyle.flow);
4771             ImpliedAttribute("POINTSZ", pThis->u.u_headstyle.pointsz);
4772             ImpliedAttribute("SET-WIDTH", pThis->u.u_headstyle.set_width);
4773             ImpliedAttribute("COLOR", pThis->u.u_headstyle.color);
4774             ImpliedAttribute("XLFD", pThis->u.u_headstyle.xlfd);
4775             ImpliedAttribute("XLFDI", pThis->u.u_headstyle.xlfdi);
4776             ImpliedAttribute("XLFDB", pThis->u.u_headstyle.xlfdb);
4777             ImpliedAttribute("XLFDIB", pThis->u.u_headstyle.xlfdib);
4778             ImpliedAttribute("TYPENAM", pThis->u.u_headstyle.typenam);
4779             ImpliedAttribute("TYPENAMI", pThis->u.u_headstyle.typenami);
4780             ImpliedAttribute("TYPENAMB", pThis->u.u_headstyle.typenamb);
4781             ImpliedAttribute("TYPENAMIB", pThis->u.u_headstyle.typenamib);
4782             ImpliedAttribute("STYLE", pThis->u.u_headstyle.style);
4783             ImpliedAttribute("SPACING", pThis->u.u_headstyle.spacing);
4784             ImpliedAttribute("WEIGHT", pThis->u.u_headstyle.weight);
4785             ImpliedAttribute("SLANT", pThis->u.u_headstyle.slant);
4786             ImpliedAttribute("SPECIAL", pThis->u.u_headstyle.special);
4787             ImpliedAttribute("L-MARGIN", pThis->u.u_headstyle.l_margin);
4788             ImpliedAttribute("R-MARGIN", pThis->u.u_headstyle.r_margin);
4789             ImpliedAttribute("T-MARGIN", pThis->u.u_headstyle.t_margin);
4790             ImpliedAttribute("B-MARGIN", pThis->u.u_headstyle.b_margin);
4791             ImpliedAttribute("BORDER", pThis->u.u_headstyle.border);
4792             ImpliedAttribute("VJUST", pThis->u.u_headstyle.vjust);
4793             ImpliedAttribute("JUSTIFY", pThis->u.u_headstyle.justify);
4794             PutString(">\n");
4795             pThat = pThis;
4796             pThis = pThat->pNext;
4797             pThat->pNext = NULL;
4798             DeleteElement(pThat);
4799             break;
4800         case e_formstyle:
4801             if (minimalTossFlag && !pThis->u.u_formstyle.beenUsed)
4802                 {
4803                 pThat = pThis;
4804                 pThis = pThat->pNext;
4805                 pThat->pNext = NULL;
4806                 DeleteElement(pThat);
4807                 break;
4808                 }
4809             OpenNamedTag("FORMSTYLE", TRUE, FALSE);
4810             RequiredAttribute("CLASS", pThis->u.u_formstyle.class);
4811             ImpliedAttribute("SSI", pThis->u.u_formstyle.ssi);
4812             ImpliedAttribute("RLEVEL", pThis->u.u_formstyle.rlevel);
4813             ImpliedAttribute("PHRASE", pThis->u.u_formstyle.phrase);
4814             ImpliedAttribute("SRCH-WT", pThis->u.u_formstyle.srch_wt);
4815             ImpliedAttribute("COLW", pThis->u.u_formstyle.colw);
4816             ImpliedAttribute("COLJ", pThis->u.u_formstyle.colj);
4817             ImpliedAttribute("POINTSZ", pThis->u.u_formstyle.pointsz);
4818             ImpliedAttribute("SET-WIDTH", pThis->u.u_formstyle.set_width);
4819             ImpliedAttribute("COLOR", pThis->u.u_formstyle.color);
4820             ImpliedAttribute("XLFD", pThis->u.u_formstyle.xlfd);
4821             ImpliedAttribute("XLFDI", pThis->u.u_formstyle.xlfdi);
4822             ImpliedAttribute("XLFDB", pThis->u.u_formstyle.xlfdb);
4823             ImpliedAttribute("XLFDIB", pThis->u.u_formstyle.xlfdib);
4824             ImpliedAttribute("TYPENAM", pThis->u.u_formstyle.typenam);
4825             ImpliedAttribute("TYPENAMI", pThis->u.u_formstyle.typenami);
4826             ImpliedAttribute("TYPENAMB", pThis->u.u_formstyle.typenamb);
4827             ImpliedAttribute("TYPENAMIB", pThis->u.u_formstyle.typenamib);
4828             ImpliedAttribute("STYLE", pThis->u.u_formstyle.style);
4829             ImpliedAttribute("SPACING", pThis->u.u_formstyle.spacing);
4830             ImpliedAttribute("WEIGHT", pThis->u.u_formstyle.weight);
4831             ImpliedAttribute("SLANT", pThis->u.u_formstyle.slant);
4832             ImpliedAttribute("SPECIAL", pThis->u.u_formstyle.special);
4833             ImpliedAttribute("L-MARGIN", pThis->u.u_formstyle.l_margin);
4834             ImpliedAttribute("R-MARGIN", pThis->u.u_formstyle.r_margin);
4835             ImpliedAttribute("T-MARGIN", pThis->u.u_formstyle.t_margin);
4836             ImpliedAttribute("B-MARGIN", pThis->u.u_formstyle.b_margin);
4837             ImpliedAttribute("BORDER", pThis->u.u_formstyle.border);
4838             ImpliedAttribute("VJUST", pThis->u.u_formstyle.vjust);
4839             PutString(">\n");
4840             pThat = pThis;
4841             pThis = pThat->pNext;
4842             pThat->pNext = NULL;
4843             DeleteElement(pThat);
4844             break;
4845         case e_frmtstyle:
4846             if (minimalTossFlag && !pThis->u.u_frmtstyle.beenUsed)
4847                 {
4848                 pThat = pThis;
4849                 pThis = pThat->pNext;
4850                 pThat->pNext = NULL;
4851                 DeleteElement(pThat);
4852                 break;
4853                 }
4854             OpenNamedTag("FRMTSTYLE", TRUE, FALSE);
4855             RequiredAttribute("CLASS", pThis->u.u_frmtstyle.class);
4856             ImpliedAttribute("SSI", pThis->u.u_frmtstyle.ssi);
4857             ImpliedAttribute("RLEVEL", pThis->u.u_frmtstyle.rlevel);
4858             ImpliedAttribute("PHRASE", pThis->u.u_frmtstyle.phrase);
4859             ImpliedAttribute("F-MARGIN", pThis->u.u_frmtstyle.f_margin);
4860             ImpliedAttribute("SRCH-WT", pThis->u.u_frmtstyle.srch_wt);
4861             ImpliedAttribute("POINTSZ", pThis->u.u_frmtstyle.pointsz);
4862             ImpliedAttribute("SET-WIDTH", pThis->u.u_frmtstyle.set_width);
4863             ImpliedAttribute("COLOR", pThis->u.u_frmtstyle.color);
4864             ImpliedAttribute("XLFD", pThis->u.u_frmtstyle.xlfd);
4865             ImpliedAttribute("XLFDI", pThis->u.u_frmtstyle.xlfdi);
4866             ImpliedAttribute("XLFDB", pThis->u.u_frmtstyle.xlfdb);
4867             ImpliedAttribute("XLFDIB", pThis->u.u_frmtstyle.xlfdib);
4868             ImpliedAttribute("TYPENAM", pThis->u.u_frmtstyle.typenam);
4869             ImpliedAttribute("TYPENAMI", pThis->u.u_frmtstyle.typenami);
4870             ImpliedAttribute("TYPENAMB", pThis->u.u_frmtstyle.typenamb);
4871             ImpliedAttribute("TYPENAMIB", pThis->u.u_frmtstyle.typenamib);
4872             ImpliedAttribute("STYLE", pThis->u.u_frmtstyle.style);
4873             ImpliedAttribute("SPACING", pThis->u.u_frmtstyle.spacing);
4874             ImpliedAttribute("WEIGHT", pThis->u.u_frmtstyle.weight);
4875             ImpliedAttribute("SLANT", pThis->u.u_frmtstyle.slant);
4876             ImpliedAttribute("SPECIAL", pThis->u.u_frmtstyle.special);
4877             ImpliedAttribute("L-MARGIN", pThis->u.u_frmtstyle.l_margin);
4878             ImpliedAttribute("R-MARGIN", pThis->u.u_frmtstyle.r_margin);
4879             ImpliedAttribute("T-MARGIN", pThis->u.u_frmtstyle.t_margin);
4880             ImpliedAttribute("B-MARGIN", pThis->u.u_frmtstyle.b_margin);
4881             ImpliedAttribute("BORDER", pThis->u.u_frmtstyle.border);
4882             ImpliedAttribute("VJUST", pThis->u.u_frmtstyle.vjust);
4883             ImpliedAttribute("JUSTIFY", pThis->u.u_frmtstyle.justify);
4884             PutString(">\n");
4885             pThat = pThis;
4886             pThis = pThat->pNext;
4887             pThat->pNext = NULL;
4888             DeleteElement(pThat);
4889             break;
4890         case e_grphstyle:
4891             if (minimalTossFlag && !pThis->u.u_grphstyle.beenUsed)
4892                 {
4893                 pThat = pThis;
4894                 pThis = pThat->pNext;
4895                 pThat->pNext = NULL;
4896                 DeleteElement(pThat);
4897                 break;
4898                 }
4899             OpenNamedTag("GRPHSTYLE", TRUE, FALSE);
4900             RequiredAttribute("CLASS", pThis->u.u_grphstyle.class);
4901             ImpliedAttribute("SSI", pThis->u.u_grphstyle.ssi);
4902             ImpliedAttribute("RLEVEL", pThis->u.u_grphstyle.rlevel);
4903             ImpliedAttribute("PHRASE", pThis->u.u_grphstyle.phrase);
4904             ImpliedAttribute("L-MARGIN", pThis->u.u_grphstyle.l_margin);
4905             ImpliedAttribute("R-MARGIN", pThis->u.u_grphstyle.r_margin);
4906             ImpliedAttribute("T-MARGIN", pThis->u.u_grphstyle.t_margin);
4907             ImpliedAttribute("B-MARGIN", pThis->u.u_grphstyle.b_margin);
4908             ImpliedAttribute("BORDER", pThis->u.u_grphstyle.border);
4909             ImpliedAttribute("VJUST", pThis->u.u_grphstyle.vjust);
4910             ImpliedAttribute("JUSTIFY", pThis->u.u_grphstyle.justify);
4911             PutString(">\n");
4912             pThat = pThis;
4913             pThis = pThat->pNext;
4914             pThat->pNext = NULL;
4915             DeleteElement(pThat);
4916             break;
4917         default:
4918             fprintf(stderr, "%s\n", errMess);
4919             if (m_errfile && (m_errfile != stderr))
4920                 fprintf(m_errfile, "%s\n", errMess);
4921             exit(xxx);
4922         }
4923     }
4924
4925 if (doCompression)
4926     {
4927     FcloseFile(outFile, compFileName, xxx);
4928     outFile   = saveOutFile;
4929     zFileSize = Compress(compFileName, compZFileName, TRUE);
4930
4931     /* write out the magic number and the file size */
4932     zTemp[3] = zFileSize % 256;
4933     zFileSize /= 256;
4934     zTemp[2] = zFileSize % 256;
4935     zFileSize /= 256;
4936     zTemp[1] = zFileSize % 256;
4937     zTemp[0] = '\0';
4938     FwriteFile(outFile, tempFileName, (char *) zTemp, 4, xxx);
4939
4940     /* copy the compressed virpage to output */
4941     zFd = OpenFile(compZFileName, O_RDONLY, xxx);
4942     while (length = ReadFile(zFd, compZFileName, buffer, BUFSIZ, xxx))
4943         FwriteFile(outFile, tempFileName, buffer, length, xxx);
4944     CloseFile(zFd, compZFileName, xxx);
4945     unlink(compZFileName);
4946     unlink(compFileName);
4947     }
4948 }
4949
4950
4951 #if defined(M_PROTO)
4952 void OptimizeAndEmit(ElementPtr pVirpage)
4953 #else
4954 void OptimizeAndEmit(pVirpage)
4955   ElementPtr pVirpage;
4956 #endif
4957 {
4958 if (optimizeFlag)
4959     OptimizeSDL(&(pVirpage->u.u_virpage.pBlockOrForm));
4960 EmitSDL(pVirpage);
4961 }
4962
4963
4964 /* A routine to emit a help message and exit */
4965 static void Help()
4966 {
4967 printf("The available options to %s are 'c', 'd', 'h', 'o' and 't'.\n\n",
4968        progName);
4969 printf("Their meanings are:\n");
4970 printf("    c: compress an SDL document (on a per virtual page basis)\n");
4971 printf("    d: decompress an SDL document\n");
4972 printf("    h: print this message and exit\n");
4973 printf("    o: perform peephole optimizations on this SDL document\n");
4974 printf("    t: eliminate any TOSS elements not used in this SDL document\n\n");
4975 printf("You may specify either one filename or two.  If one file is\n");
4976 printf("specified, it will be used as both the input and output file.\n");
4977 printf("If the input file has no .sdl extension, one will be added.  If\n");
4978 printf("an explicit output filename is specified, it will be used without\n");
4979 printf("modification.\n\n");
4980 exit(0);
4981 }
4982
4983
4984 /* A routine to emit a usage message and exit */
4985 static void Usage()
4986 {
4987 fprintf(stderr,
4988         "Usage: %s [-cdhot] <inFileName> [<outFileName>]\n",
4989         progName);
4990 if (m_errfile && (m_errfile != stderr))
4991     {
4992     fprintf(m_errfile,
4993             "Usage: %s [-cdhot] <inFileName> [<outFileName>]\n",
4994             progName);
4995     }
4996 exit(1);
4997 }
4998
4999
5000 /*
5001  * A routine to pre-build all the input, output and intermediate file
5002  * names that we'll need.
5003 */
5004 static void MakeFileNames()
5005 {
5006 int   argCount;
5007 int   length, c;
5008 char *cp;
5009 char  pid[32];
5010 char *tmpDir;
5011
5012 progName = strrchr(*m_argv, '/');
5013 if (progName) progName++;
5014 else progName = *m_argv;
5015 argCount  = 1;
5016
5017 while ((c = getopt(m_argc, m_argv, "cdhot")) != -1)
5018     {
5019     switch (c)
5020         {
5021         case 'c':
5022             compressFlag = TRUE;
5023             break;
5024         case 'd':
5025             decompressFlag = TRUE;
5026             break;
5027         case 'h':
5028             Help();
5029         case 'o':
5030             optimizeFlag = TRUE;
5031             break;
5032         case 't':
5033             minimalTossFlag = TRUE;
5034             break;
5035         case '?':
5036             Usage();
5037         }
5038     }
5039 argCount += (optind - 1);
5040
5041 if (((m_argc - argCount) < 1) || ((m_argc - argCount) > 2))
5042     Usage();
5043
5044 inFileName  = m_argv[argCount++];
5045 outFileName = m_argv[argCount];     /* may be NULL */
5046
5047 cp = strrchr(inFileName, '.');
5048 if (cp)
5049     {
5050     if (strcmp(cp, dotSDL) == 0)
5051         {
5052         length = cp - inFileName;
5053         cp = malloc(length + 1);
5054         strncpy(cp, inFileName, length);
5055         cp[length] = '\0';
5056         }
5057     else
5058         {
5059         cp = inFileName;
5060         }
5061     }
5062 else
5063     cp = inFileName;
5064 length = strlen(cp);
5065 inFileName = malloc(length + DOTSDLSIZ);
5066 strcpy(inFileName, cp);
5067 strcat(inFileName, dotSDL);
5068 if (!outFileName)
5069     outFileName = inFileName;
5070
5071 errFileName = malloc(length + DOTERRSIZ);
5072 strcpy(errFileName, cp);
5073 strcat(errFileName, dotERR);
5074
5075 idxFileName = malloc(length + DOTIDXSIZ);
5076 strcpy(idxFileName, cp);
5077 strcat(idxFileName, dotIDX);
5078
5079 snbFileName = malloc(length + DOTSNBSIZ);
5080 strcpy(snbFileName, cp);
5081 strcat(snbFileName, dotSNB);
5082
5083 tossFileName = malloc(length + DOTTSSSIZ);
5084 strcpy(tossFileName, cp);
5085 strcat(tossFileName, dotTSS);
5086
5087 tmpDir = getenv("TMPDIR");
5088 if (!tmpDir)
5089     tmpDir = TMPDIR;
5090 sprintf(pid, "%d", getpid());
5091 length = DOTSDLSIZ - 1 + strlen(tmpDir) + strlen(pid) + 1; 
5092 tempFileName = malloc(length);
5093 strcpy(tempFileName, tmpDir);
5094 strcat(tempFileName, "/");
5095 strcat(tempFileName, dotSDL + 1); /* skip the "." */
5096 strcat(tempFileName, pid);
5097 compFileName = malloc(length + 4);
5098 strcpy(compFileName, tempFileName);
5099 strcat(compFileName, "comp");
5100 compZFileName = malloc(length + 4 + 2);
5101 strcpy(compZFileName, compFileName);
5102 strcat(compZFileName, ".Z");
5103 sortedIdxFileName = malloc(length + 3);
5104 strcpy(sortedIdxFileName, tempFileName);
5105 strcat(sortedIdxFileName, "idx");
5106 vstructFileName = malloc(length + 4);
5107 strcpy(vstructFileName, tempFileName);
5108 strcat(vstructFileName, "vstr");
5109 }
5110
5111
5112
5113 /*
5114  * Some small and obvious utility routines for opening,
5115  * reading, writing and closing files.  Most have versions for both
5116  * file descriptor and stream operations.  When possible, the file
5117  * descriptor (unbuffered) versions are used for performance reasons.
5118  * Of course, when using the unbuffered versions we attempt to read in
5119  * BUFSIZ characters at a time.
5120 */
5121 #if defined(M_PROTO)
5122 int OpenFile(char *name, int type, int code)
5123 #else
5124 int OpenFile(name, type, code)
5125   char *name;
5126   int type;
5127   int code;
5128 #endif
5129 {
5130 int fd;
5131 int oflag, mode;
5132
5133 mode = 0;
5134 if ((oflag = type) == O_WRONLY)
5135     {
5136     oflag |= O_CREAT;
5137     mode = 0666;
5138     }
5139
5140 if ((fd = open(name, oflag, mode)) == -1)
5141     {
5142     fprintf(stderr,
5143             "%s: error opening \"%s\" for %s\n",
5144             progName,
5145             name,
5146             (type == O_RDONLY) ? "reading" : "writing");
5147     perror(name);
5148     if (m_errfile && (m_errfile != stderr))
5149         {
5150         fprintf(m_errfile,
5151                 "%s: error opening \"%s\" for %s\n",
5152                 progName,
5153                 name,
5154                 (type == O_RDONLY) ? "reading" : "writing");
5155         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5156         }
5157     exit(code);
5158     }
5159 return fd;
5160 }
5161
5162
5163 #if defined(M_PROTO)
5164 int ReadFile(int fd, char *name, char *buffer, int amount, int code)
5165 #else
5166 int ReadFile(fd, name, buffer, amount, code)
5167   int fd;
5168   char *name;
5169   char *buffer;
5170   int amount;
5171   int code;
5172 #endif
5173 {
5174 int length;
5175
5176 if ((length = read(fd, buffer, amount)) == -1)
5177     {
5178     fprintf(stderr, "%s: error reading from \"%s\"\n", progName, name);
5179     perror(name);
5180     if (m_errfile && (m_errfile != stderr))
5181         {
5182         fprintf(m_errfile, "%s: error reading from \"%s\"\n", progName, name);
5183         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5184         }
5185     exit(code);
5186     }
5187 return length;
5188 }
5189
5190
5191 #if defined(M_PROTO)
5192 int WriteFile(int fd, char *name, char *buffer, int amount, int code)
5193 #else
5194 int WriteFile(fd, name, buffer, amount, code)
5195 int fd;
5196   char *name;
5197   char *buffer;
5198   int amount;
5199   int code;
5200 #endif
5201 {
5202 int length;
5203
5204 if ((length = write(fd, buffer, amount)) == -1)
5205     {
5206     fprintf(stderr, "%s: error writing to \"%s\"\n", progName, name);
5207     perror(name);
5208     if (m_errfile && (m_errfile != stderr))
5209         {
5210         fprintf(m_errfile, "%s: error writing to \"%s\"\n", progName, name);
5211         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5212         }
5213     exit(code);
5214     }
5215 return length;
5216 }
5217
5218
5219 #if defined(M_PROTO)
5220 int FileExists(char *name)
5221 #else
5222 int FileExists(name)
5223   char *name;
5224 #endif
5225 {
5226 if (access(name, F_OK) == 0) return 1;
5227 return 0;
5228 }
5229
5230
5231 #if defined(M_PROTO)
5232 void AssertFileIsReadable(char *name, int code)
5233 #else
5234 void AssertFileIsReadable(name, code)
5235   char *name;
5236   int code;
5237 #endif
5238 {
5239 if (access(name, R_OK) == -1)
5240     {
5241     fprintf(stderr, "%s: cannot open \"%s\" for reading\n", progName, name);
5242     perror(name);
5243     if (m_errfile && (m_errfile != stderr))
5244         {
5245         fprintf(m_errfile,
5246                 "%s: cannot open \"%s\" for reading\n",
5247                 progName,
5248                 name);
5249         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5250         }
5251     exit(code);
5252     }
5253 }
5254
5255
5256 #if defined(M_PROTO)
5257 int FileSize(char *name, int code)
5258 #else
5259 int FileSize(name, code)
5260   char *name;
5261   int code;
5262 #endif
5263 {
5264 struct stat buf;
5265
5266 if (stat(name, &buf) == -1)
5267     {
5268     fprintf(stderr, "%s: error getting size of \"%s\"\n", progName, name);
5269     perror(name);
5270     if (m_errfile && (m_errfile != stderr))
5271         {
5272         fprintf(m_errfile,
5273                 "%s: error getting size of \"%s\"\n",
5274                 progName,
5275                 name);
5276         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5277         }
5278     exit(code);
5279     }
5280
5281 return buf.st_size;
5282 }
5283
5284
5285 #if defined(M_PROTO)
5286 void CloseFile(int fd, char *name, int code)
5287 #else
5288 void CloseFile(fd, name, code)
5289   int fd;
5290   char *name;
5291   int code;
5292 #endif
5293 {
5294 if (close(fd) == -1)
5295     {
5296     fprintf(stderr, "%s: error closing \"%s\"\n", progName, name);
5297     perror(name);
5298     if (m_errfile && (m_errfile != stderr))
5299         {
5300         fprintf(m_errfile, "%s: error closing \"%s\"\n", progName, name);
5301         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5302         }
5303     exit(code);
5304     }
5305 }
5306
5307
5308 #if defined(M_PROTO)
5309 FILE * FopenFile(char *name, int type, int code)
5310 #else
5311 FILE * FopenFile(name, type, code)
5312   char *name;
5313   int type;
5314   int code;
5315 #endif
5316 {
5317 FILE *file;
5318 char *mode;
5319
5320 if (type == O_RDONLY)
5321     mode = "r";
5322 else if (type == O_WRONLY)
5323     mode = "w";
5324 else if (type == (O_WRONLY|O_APPEND))
5325     mode = "a";
5326 else
5327     {
5328     fprintf(stderr,
5329             "%s: bad type (%d) in opening \"%s\"",
5330             progName,
5331             type,
5332             name);
5333     if (m_errfile && (m_errfile != stderr))
5334         {
5335         fprintf(m_errfile,
5336                 "%s: bad type (%d) in opening \"%s\"",
5337                 progName,
5338                 type,
5339                 name);
5340         }
5341     exit(code);
5342     }
5343
5344 if ((file = fopen(name, mode)) == NULL)
5345     {
5346     fprintf(stderr,
5347             "%s: error opening \"%s\" for %s\n",
5348             progName,
5349             name,
5350             (*mode == 'r') ? "reading" :
5351                 (*mode == 'w') ? "writing" : "appending");
5352     perror(name);
5353     if (m_errfile && (m_errfile != stderr))
5354         {
5355         fprintf(m_errfile,
5356                 "%s: error opening \"%s\" for %s\n",
5357                 progName,
5358                 name,
5359                 (*mode == 'r') ? "reading" :
5360                     (*mode == 'w') ? "writing" : "appending");
5361         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5362         }
5363     exit(code);
5364     }
5365 return file;
5366 }
5367
5368
5369 #if defined(M_PROTO)
5370 int GetALine(FILE *file, char *name, char *line, int max, int code)
5371 #else
5372 int GetALine(file, name, line, max, code)
5373   FILE *file;
5374   char *name;
5375   char *line;
5376   int max;
5377   int code;
5378 #endif
5379 {
5380 char *pc;
5381 int   length;
5382
5383 pc = fgets(line, max, file);
5384 if (!pc)
5385     {
5386     if (!ferror(file))
5387         {
5388         return 0;
5389         }
5390     else
5391         {
5392         fprintf(stderr,
5393                 "%s: error getting a line from \"%s\"\n",
5394                 progName,
5395                 name);
5396         perror(name);
5397         if (m_errfile && (m_errfile != stderr))
5398             {
5399             fprintf(m_errfile,
5400                     "%s: error getting a line from \"%s\"\n",
5401                     progName,
5402                     name);
5403             fprintf(m_errfile, "%s: %s", name, strerror(errno));
5404             }
5405         exit(code);
5406         }
5407     }
5408
5409 length = strlen(pc);
5410 if ((length >= (max - 1)) && (line[max-1] != '\n'))
5411     {
5412     fprintf(stderr,
5413             "%s: line longer than %d bytes in file \"%s\"\n",
5414             progName,
5415             max,
5416             name);
5417     if (m_errfile && (m_errfile != stderr))
5418         {
5419         fprintf(m_errfile,
5420                 "%s: line longer than %d bytes in file \"%s\"\n",
5421                 progName,
5422                 max,
5423                 name);
5424         }
5425     exit(code);
5426     }
5427 return length;
5428 }
5429
5430
5431 #if defined(M_PROTO)
5432 int FreadFile(FILE *file, char *name, char *buffer, int amount, int code)
5433 #else
5434 int FreadFile(file, name, buffer, amount, code)
5435   FILE *file;
5436   char *name;
5437   char *buffer;
5438   int amount;
5439   int code;
5440 #endif
5441 {
5442 int length;
5443
5444 if ((length = fread(buffer, sizeof(char), amount, file)) == -1)
5445     {
5446     fprintf(stderr, "%s: error reading from \"%s\"\n", progName, name);
5447     perror(name);
5448     if (m_errfile && (m_errfile != stderr))
5449         {
5450         fprintf(m_errfile, "%s: error reading from \"%s\"\n", progName, name);
5451         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5452         }
5453     exit(code);
5454     }
5455 return length;
5456 }
5457
5458
5459 #if defined(M_PROTO)
5460 void FwriteFile(FILE *file, char *name, char *buffer, size_t amount, int code)
5461 #else
5462 void FwriteFile(file, name, buffer, amount, code)
5463   FILE *file;
5464   char *name;
5465   char *buffer;
5466   size_t amount;
5467   int code;
5468 #endif
5469 {
5470 if (fwrite(buffer, 1, amount, file) != amount)
5471     {
5472     fprintf(stderr, "%s: error writing to \"%s\"\n", progName, name);
5473     perror(name);
5474     if (m_errfile && (m_errfile != stderr))
5475         {
5476         fprintf(m_errfile, "%s: error writing to \"%s\"\n", progName, name);
5477         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5478         }
5479     exit(code);
5480     }
5481 }
5482
5483
5484 #if defined(M_PROTO)
5485 void FcloseFile(FILE *file, char *name, int code)
5486 #else
5487 void FcloseFile(file, name, code)
5488   FILE *file;
5489   char *name;
5490   int code;
5491 #endif
5492 {
5493 if (fclose(file) != 0)
5494     {
5495     fprintf(stderr, "%s: error closing \"%s\"\n", progName, name);
5496     perror(name);
5497     if (m_errfile && (m_errfile != stderr))
5498         {
5499         fprintf(m_errfile, "%s: error closing \"%s\"\n", progName, name);
5500         fprintf(m_errfile, "%s: %s", name, strerror(errno));
5501         }
5502     exit(code);
5503     }
5504 }
5505
5506
5507 #if defined(M_PROTO)
5508 int StringToUpper(char *string)
5509 #else
5510 int StringToUpper(string)
5511   char *string;
5512 #endif
5513 {
5514 char *pc, c;
5515
5516 pc = string;
5517 while (c = *pc)
5518     {
5519     if (isalpha(c) && islower(c)) *pc = toupper(c);
5520     pc++;
5521     }
5522 return pc - string;
5523 }
5524
5525
5526 /*
5527  * A routine to build the index from the .idx file (if any).
5528 */
5529 void BuildIndex()
5530 {
5531 char  buffer[BUFSIZ+1], *pFrom, *pTo, *pRestart, thisChar;
5532 int   length, size;
5533 int   lineCount;
5534 char  line[LINEMAX];
5535 int   status;
5536 int   remnant;
5537 int   found;
5538 int   sortedIdxFd;
5539 FILE *sdlIdxFile;
5540 static char openIndex[]  = "<INDEX COUNT=\"%d\">\n";
5541 static char closeIndex[] = "</INDEX>\n";
5542 static char openEntry[]  = "<ENTRY LOCS=\"";
5543 static char closeEntry[] = "</ENTRY>\n";
5544 char sort[BUFSIZ], text[BUFSIZ], rid[BUFSIZ];
5545 char lastsort[BUFSIZ], lasttext[BUFSIZ], lastrid[BUFSIZ];
5546 char locs[BUFSIZ];
5547
5548 /* skip the index building step if no .idx file or .idx file is empty */
5549 if (!FileExists(idxFileName) || (FileSize(idxFileName, xxx) == 0))
5550     return;
5551
5552 AssertFileIsReadable(idxFileName, xxx);
5553
5554 haveIndex = TRUE;
5555
5556 sortedIdxFd = OpenFile(sortedIdxFileName, O_WRONLY, xxx);
5557 CloseFile(sortedIdxFd, sortedIdxFileName, xxx);
5558
5559 buffer[0] = 0;
5560 if (operantLocale)
5561     sprintf(buffer, "env LANG=%s ", operantLocale);
5562
5563 sprintf(buffer + strlen(buffer),
5564         "sort -f %s > %s",
5565         idxFileName,
5566         sortedIdxFileName);
5567 if (status = system(buffer))
5568     {
5569     if (status == -1)
5570         {
5571         fprintf(stderr,
5572                 "%s: error forking to execute \"%s\"\n",
5573                 progName,
5574                 buffer);
5575         perror(buffer);
5576         if (m_errfile && (m_errfile != stderr))
5577             {
5578             fprintf(m_errfile,
5579                     "%s: error forking to execute \"%s\"\n",
5580                     progName,
5581                     buffer);
5582             fprintf(m_errfile, "%s: %s", buffer, strerror(errno));
5583             }
5584         exit(1);
5585         }
5586     else
5587         {
5588         fprintf(stderr, "%s: error executing \"%s\"\n", progName, buffer);
5589         if (m_errfile && (m_errfile != stderr))
5590             {
5591             fprintf(m_errfile,
5592                     "%s: error executing \"%s\"\n",
5593                     progName,
5594                     buffer);
5595             }
5596         exit(1);
5597         }
5598     }
5599
5600 /* get the number of index entries */
5601 lineCount = 0;
5602 sortedIdxFd = OpenFile(sortedIdxFileName, O_RDONLY, xxx);
5603 lastsort[0]    = '\0';
5604 lasttext[0]    = '\0';
5605 lastrid[0]     = '\0';
5606 locs[0]        = '\0';
5607 buffer[BUFSIZ] = '\0';
5608 size    = 0;
5609 remnant = 0;
5610 pFrom = buffer;
5611 while (size ||
5612        ((size = ReadFile(sortedIdxFd,
5613                          sortedIdxFileName,
5614                          buffer+remnant,
5615                          BUFSIZ-remnant,
5616                          xxx)) > 0))
5617     {
5618     /* add in remnant in case we just got a new buffer load */
5619     size += remnant;
5620     remnant = 0;
5621
5622     /* First split out the "sort", "text" and "rid" fields.  They are
5623        separated by an ASCII Record Seperator (036) character */
5624     pTo = sort;
5625     pRestart = pFrom;
5626     found = 0;
5627     while (--size >= 0)
5628         {
5629         if ((*pTo++ = *pFrom++) == RS)
5630             {
5631             found = 1;
5632             break;
5633             }
5634         }
5635     if (!found)
5636         {
5637         size = 0;
5638         pTo = buffer;
5639         pFrom = pRestart;
5640         while (*pTo++ = *pFrom++);
5641         remnant = pTo - buffer - 1;
5642         pFrom = buffer;
5643         continue;
5644         }
5645     *(pTo-1) = '\0';
5646
5647     pTo = text;
5648     found = 0;
5649     while (--size >= 0)
5650         {
5651         thisChar = *pFrom++;
5652         if (thisChar == RS)
5653             {
5654             found = 1;
5655             *pTo  = '\0';
5656             break;
5657             }
5658         if ((thisChar == '<') || (thisChar == '&'))
5659             *pTo++ = '&';
5660         *pTo++ = thisChar;
5661         }
5662     if (!found)
5663         {
5664         size = 0;
5665         pTo = buffer;
5666         pFrom = pRestart;
5667         while (*pTo++ = *pFrom++);
5668         remnant = pTo - buffer - 1;
5669         pFrom = buffer;
5670         continue;
5671         }
5672
5673     pTo = rid;
5674     found = 0;
5675     while (--size >= 0)
5676         {
5677         if ((*pTo++ = *pFrom++) == '\n')
5678             {
5679             found = 1;
5680             break;
5681             }
5682         }
5683     if (!found)
5684         {
5685         size = 0;
5686         pTo = buffer;
5687         pFrom = pRestart;
5688         while (*pTo++ = *pFrom++);
5689         remnant = pTo - buffer - 1;
5690         pFrom = buffer;
5691         continue;
5692         }
5693     *(pTo-1) = '\0';
5694
5695     /* if buffer is consumed, reset ourselves for the next buffer load */
5696     if (size <= 0)
5697         {
5698         size    = 0;
5699         remnant = 0;
5700         pFrom   = buffer;
5701         }
5702
5703     if (strcmp(text, lasttext) == 0)
5704         {
5705         if (strcmp(rid, lastrid) != 0)
5706             {
5707             strcat(locs, " ");
5708             strcat(locs, rid);
5709             }
5710         }
5711     else
5712         {
5713         if (strlen(locs) != 0)
5714             lineCount++;
5715         strcpy(locs, rid);
5716         }
5717     strcpy(lasttext, text);
5718     strcpy(lastsort, sort);
5719     strcpy(lastrid,  rid);
5720     }
5721 if (strlen(locs) != 0)
5722     lineCount++;
5723 CloseFile(sortedIdxFd, sortedIdxFileName, xxx);
5724
5725 sortedIdxFd = OpenFile(sortedIdxFileName, O_RDONLY, xxx);
5726 unlink(sortedIdxFileName);
5727 sdlIdxFile = FopenFile(sortedIdxFileName, O_WRONLY, xxx);
5728
5729 fprintf(sdlIdxFile, openIndex, lineCount);
5730
5731 lastsort[0]    = '\0';
5732 lasttext[0]    = '\0';
5733 lastrid[0]     = '\0';
5734 locs[0]        = '\0';
5735 buffer[BUFSIZ] = '\0';
5736 size    = 0;
5737 remnant = 0;
5738 pFrom = buffer;
5739 while (size ||
5740        ((size = ReadFile(sortedIdxFd,
5741                          sortedIdxFileName,
5742                          buffer+remnant,
5743                          BUFSIZ-remnant,
5744                          xxx)) > 0))
5745     {
5746     /* add in remnant in case we just got a new buffer load */
5747     size += remnant;
5748     remnant = 0;
5749
5750     /* First split out the "sort", "text" and "rid" fields.  They are
5751        separated by an ASCII Record Seperator (036) character */
5752     pTo = sort;
5753     pRestart = pFrom;
5754     found = 0;
5755     while (--size >= 0)
5756         {
5757         if ((*pTo++ = *pFrom++) == RS)
5758             {
5759             found = 1;
5760             break;
5761             }
5762         }
5763     if (!found)
5764         {
5765         size = 0;
5766         pTo = buffer;
5767         pFrom = pRestart;
5768         while (*pTo++ = *pFrom++);
5769         remnant = pTo - buffer - 1;
5770         pFrom = buffer;
5771         continue;
5772         }
5773     *(pTo-1) = '\0';
5774
5775     pTo = text;
5776     found = 0;
5777     while (--size >= 0)
5778         {
5779         thisChar = *pFrom++;
5780         if (thisChar == RS)
5781             {
5782             found = 1;
5783             *pTo  = '\0';
5784             break;
5785             }
5786         if ((thisChar == '<') || (thisChar == '&'))
5787             *pTo++ = '&';
5788         *pTo++ = thisChar;
5789         }
5790     if (!found)
5791         {
5792         size = 0;
5793         pTo = buffer;
5794         pFrom = pRestart;
5795         while (*pTo++ = *pFrom++);
5796         remnant = pTo - buffer - 1;
5797         pFrom = buffer;
5798         continue;
5799         }
5800
5801     pTo = rid;
5802     found = 0;
5803     while (--size >= 0)
5804         {
5805         if ((*pTo++ = *pFrom++) == '\n')
5806             {
5807             found = 1;
5808             break;
5809             }
5810         }
5811     if (!found)
5812         {
5813         size = 0;
5814         pTo = buffer;
5815         pFrom = pRestart;
5816         while (*pTo++ = *pFrom++);
5817         remnant = pTo - buffer - 1;
5818         pFrom = buffer;
5819         continue;
5820         }
5821     *(pTo-1) = '\0';
5822
5823     /* if buffer is consumed, reset ourselves for the next buffer load */
5824     if (size <= 0)
5825         {
5826         size    = 0;
5827         remnant = 0;
5828         pFrom   = buffer;
5829         }
5830
5831     if (strcmp(text, lasttext) == 0)
5832         {
5833         if (strcmp(rid, lastrid) != 0)
5834             {
5835             strcat(locs, " ");
5836             strcat(locs, rid);
5837             }
5838         }
5839     else
5840         {
5841         if (strlen(locs) != 0)
5842             {
5843             if (strcmp(lastsort, lasttext) != 0)
5844                 {
5845                 length = fprintf(sdlIdxFile,
5846                                  "%s%s\" SORT=\"%s\">%s%s",
5847                                  openEntry,
5848                                  locs,
5849                                  lastsort,
5850                                  lasttext,
5851                                  closeEntry);
5852                 }
5853             else
5854                 {
5855                 length = fprintf(sdlIdxFile,
5856                                  "%s%s\">%s%s",
5857                                  openEntry,
5858                                  locs,
5859                                  lasttext,
5860                                  closeEntry);
5861                 }
5862             if (length < 0)
5863                 {
5864                 fprintf(stderr,
5865                         "%s: error writing to \"%s\"\n",
5866                         progName,
5867                         sortedIdxFileName);
5868                 perror(sortedIdxFileName);
5869                 if (m_errfile && (m_errfile != stderr))
5870                     {
5871                     fprintf(m_errfile,
5872                             "%s: error writing to \"%s\"\n",
5873                             progName,
5874                             sortedIdxFileName);
5875                     fprintf(m_errfile,
5876                             "%s: %s",
5877                             sortedIdxFileName,
5878                             strerror(errno));
5879                     }
5880                 exit(1);
5881                 }
5882             }
5883         strcpy(locs, rid);
5884         }
5885     strcpy(lasttext, text);
5886     strcpy(lastsort, sort);
5887     strcpy(lastrid,  rid);
5888     }
5889 if (strlen(locs) != 0)
5890     {
5891     if (strcmp(lastsort, lasttext) != 0)
5892         {
5893         length = fprintf(sdlIdxFile,
5894                          "%s%s\" SORT=\"%s\">%s%s",
5895                          openEntry,
5896                          locs,
5897                          lastsort,
5898                          lasttext,
5899                          closeEntry);
5900         }
5901     else
5902         {
5903         length = fprintf(sdlIdxFile,
5904                          "%s%s\">%s%s",
5905                          openEntry,
5906                          locs,
5907                          lasttext,
5908                          closeEntry);
5909         }
5910     if (length < 0)
5911         {
5912         fprintf(stderr,
5913                 "%s: error writing to \"%s\"\n",
5914                 progName,
5915                 sortedIdxFileName);
5916         perror(sortedIdxFileName);
5917         if (m_errfile && (m_errfile != stderr))
5918             {
5919             fprintf(m_errfile,
5920                     "%s: error writing to \"%s\"\n",
5921                     progName,
5922                     sortedIdxFileName);
5923             fprintf(m_errfile,
5924                     "%s: %s",
5925                     sortedIdxFileName,
5926                     strerror(errno));
5927             }
5928         exit(1);
5929         }
5930     }
5931 CloseFile(sortedIdxFd, sortedIdxFileName, xxx);
5932 FwriteFile(sdlIdxFile,
5933            sortedIdxFileName,
5934            closeIndex,
5935            sizeof(closeIndex)-1,
5936            xxx);
5937 FcloseFile(sdlIdxFile, sortedIdxFileName, xxx);
5938 }
5939
5940
5941 static void HandleSNB()
5942 {
5943 int   oldOffset, newOffset;
5944 FILE *snbFile;
5945 int   outFd, inFd;
5946 char  line[LINEMAX], buffer[BUFSIZ];
5947 int   length, delta;
5948 char *realInFileName;
5949 static char openSNB[]  = "<SNB>\n";
5950 static char closeSNB[] = "</SNB>\n";
5951
5952 if (decompressFlag)
5953     realInFileName = tempFileName;
5954 else
5955     realInFileName = inFileName;
5956
5957 oldOffset = 0;
5958 newOffset = 0;
5959 inFd  = OpenFile(realInFileName,   O_RDONLY, xxx);
5960 unlink(tempFileName);
5961 outFd = OpenFile(tempFileName, O_WRONLY, xxx);
5962
5963 if (FileExists(snbFileName))
5964     {
5965     snbFile = FopenFile(snbFileName, O_RDONLY, xxx);
5966     while ((length = GetALine(snbFile,
5967                               snbFileName,
5968                               line,
5969                               LINEMAX,
5970                               xxx)) > 0)
5971         {
5972         if (isdigit(*line))
5973             {
5974             if (newOffset != 0)
5975                 WriteFile(outFd,
5976                           tempFileName,
5977                           closeSNB,
5978                           sizeof(closeSNB)-1,
5979                           xxx);
5980             newOffset = atoi(line);
5981             delta = newOffset - oldOffset;
5982             while (delta)
5983                 {
5984                 length = ReadFile(inFd,
5985                                   realInFileName,
5986                                   buffer,
5987                                   MIN(BUFSIZ,delta),
5988                                   xxx);
5989                 WriteFile(outFd, tempFileName, buffer, length, xxx);
5990                 delta -= length;
5991                 }
5992             WriteFile(outFd, tempFileName, openSNB, sizeof(openSNB)-1, xxx);
5993             oldOffset = newOffset;
5994             }
5995         else
5996             WriteFile(outFd, tempFileName, line, length, xxx);
5997         }
5998     if (newOffset != 0)
5999         WriteFile(outFd, tempFileName, closeSNB, sizeof(closeSNB)-1, xxx);
6000     FcloseFile(snbFile, snbFileName, xxx);
6001     }
6002 while (length = ReadFile(inFd, realInFileName, buffer, BUFSIZ, xxx))
6003     WriteFile(outFd, tempFileName, buffer, length, xxx);
6004 CloseFile(inFd, realInFileName, xxx);
6005 CloseFile(outFd, tempFileName, xxx);
6006 }
6007
6008
6009 /* calculate the size of the TOSS if only the used elements are
6010  * emitted
6011 */
6012 static int RestrictedTossSize()
6013 {
6014 char       buffer[32];
6015 ElementPtr pThis;
6016 int        size;
6017
6018 /* in size computations, the constant "4" is for the leading space,
6019    the equals sign and the two quotation marks; the constant preceding
6020    the "4" is the length of the attribute name itself */
6021 size = 16 /* strlen("<TOSS COUNT=\"\">\n"); */;
6022 size += sprintf(buffer, "%d", nStyles);
6023 pThis = pTossChain;
6024 while (pThis)
6025     {
6026     switch (pThis->type)
6027         {
6028         case e_keystyle:
6029             if (!pThis->u.u_keystyle.beenUsed)
6030                 break;
6031             size += 9 /* strlen("<KEYSTYLE") */;
6032             if (pThis->u.u_keystyle.class)
6033                 size += 5 + 4 + w_strlen(pThis->u.u_keystyle.class);
6034             if (pThis->u.u_keystyle.ssi)
6035                 size += 3 + 4 + w_strlen(pThis->u.u_keystyle.ssi);
6036             if (pThis->u.u_keystyle.rlevel)
6037                 size += 6 + 4 + w_strlen(pThis->u.u_keystyle.rlevel);
6038             if (pThis->u.u_keystyle.phrase)
6039                 size += 6 + 4 + w_strlen(pThis->u.u_keystyle.phrase);
6040             if (pThis->u.u_keystyle.srch_wt)
6041                 size += 7 + 4 + w_strlen(pThis->u.u_keystyle.srch_wt);
6042             if (pThis->u.u_keystyle.enter)
6043                 size += 5 + 4 + w_strlen(pThis->u.u_keystyle.enter);
6044             if (pThis->u.u_keystyle.exit)
6045                 size += 4 + 4 + w_strlen(pThis->u.u_keystyle.exit);
6046             if (pThis->u.u_keystyle.pointsz)
6047                 size += 7 + 4 + w_strlen(pThis->u.u_keystyle.pointsz);
6048             if (pThis->u.u_keystyle.set_width)
6049                 size += 9 + 4 + w_strlen(pThis->u.u_keystyle.set_width);
6050             if (pThis->u.u_keystyle.color)
6051                 size += 5 + 4 + w_strlen(pThis->u.u_keystyle.color);
6052             if (pThis->u.u_keystyle.xlfd)
6053                 size += 4 + 4 + w_strlen(pThis->u.u_keystyle.xlfd);
6054             if (pThis->u.u_keystyle.xlfdi)
6055                 size += 5 + 4 + w_strlen(pThis->u.u_keystyle.xlfdi);
6056             if (pThis->u.u_keystyle.xlfdb)
6057                 size += 5 + 4 + w_strlen(pThis->u.u_keystyle.xlfdb);
6058             if (pThis->u.u_keystyle.xlfdib)
6059                 size += 6 + 4 + w_strlen(pThis->u.u_keystyle.xlfdib);
6060             if (pThis->u.u_keystyle.typenam)
6061                 size += 7 + 4 + w_strlen(pThis->u.u_keystyle.typenam);
6062             if (pThis->u.u_keystyle.typenami)
6063                 size += 8 + 4 + w_strlen(pThis->u.u_keystyle.typenami);
6064             if (pThis->u.u_keystyle.typenamb)
6065                 size += 8 + 4 + w_strlen(pThis->u.u_keystyle.typenamb);
6066             if (pThis->u.u_keystyle.typenamib)
6067                 size += 9 + 4 + w_strlen(pThis->u.u_keystyle.typenamib);
6068             if (pThis->u.u_keystyle.style)
6069                 size += 5 + 4 + w_strlen(pThis->u.u_keystyle.style);
6070             if (pThis->u.u_keystyle.spacing)
6071                 size += 7 + 4 + w_strlen(pThis->u.u_keystyle.spacing);
6072             if (pThis->u.u_keystyle.weight)
6073                 size += 6 + 4 + w_strlen(pThis->u.u_keystyle.weight);
6074             if (pThis->u.u_keystyle.slant)
6075                 size += 5 + 4 + w_strlen(pThis->u.u_keystyle.slant);
6076             if (pThis->u.u_keystyle.special)
6077                 size += 7 + 4 + w_strlen(pThis->u.u_keystyle.special);
6078             size += 2 /* strlen(">\n") */;
6079             break;
6080         case e_headstyle:
6081             if (!pThis->u.u_headstyle.beenUsed)
6082                 break;
6083             size += 10 /* strlen("<HEADSTYLE") */;
6084             if (pThis->u.u_headstyle.class)
6085                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.class);
6086             if (pThis->u.u_headstyle.ssi)
6087                 size += 3 + 4 + w_strlen(pThis->u.u_headstyle.ssi);
6088             if (pThis->u.u_headstyle.rlevel)
6089                 size += 6 + 4 + w_strlen(pThis->u.u_headstyle.rlevel);
6090             if (pThis->u.u_headstyle.phrase)
6091                 size += 6 + 4 + w_strlen(pThis->u.u_headstyle.phrase);
6092             if (pThis->u.u_headstyle.srch_wt)
6093                 size += 7 + 4 + w_strlen(pThis->u.u_headstyle.srch_wt);
6094             if (pThis->u.u_headstyle.orient)
6095                 size += 6 + 4 + w_strlen(pThis->u.u_headstyle.orient);
6096             if (pThis->u.u_headstyle.vorient)
6097                 size += 7 + 4 + w_strlen(pThis->u.u_headstyle.vorient);
6098             if (pThis->u.u_headstyle.placement)
6099                 size += 9 + 4 + w_strlen(pThis->u.u_headstyle.placement);
6100             if (pThis->u.u_headstyle.headw)
6101                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.headw);
6102             if (pThis->u.u_headstyle.stack)
6103                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.stack);
6104             if (pThis->u.u_headstyle.flow)
6105                 size += 4 + 4 + w_strlen(pThis->u.u_headstyle.flow);
6106             if (pThis->u.u_headstyle.pointsz)
6107                 size += 7 + 4 + w_strlen(pThis->u.u_headstyle.pointsz);
6108             if (pThis->u.u_headstyle.set_width)
6109                 size += 9 + 4 + w_strlen(pThis->u.u_headstyle.set_width);
6110             if (pThis->u.u_headstyle.color)
6111                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.color);
6112             if (pThis->u.u_headstyle.xlfd)
6113                 size += 4 + 4 + w_strlen(pThis->u.u_headstyle.xlfd);
6114             if (pThis->u.u_headstyle.xlfdi)
6115                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.xlfdi);
6116             if (pThis->u.u_headstyle.xlfdb)
6117                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.xlfdb);
6118             if (pThis->u.u_headstyle.xlfdib)
6119                 size += 6 + 4 + w_strlen(pThis->u.u_headstyle.xlfdib);
6120             if (pThis->u.u_headstyle.typenam)
6121                 size += 7 + 4 + w_strlen(pThis->u.u_headstyle.typenam);
6122             if (pThis->u.u_headstyle.typenami)
6123                 size += 8 + 4 + w_strlen(pThis->u.u_headstyle.typenami);
6124             if (pThis->u.u_headstyle.typenamb)
6125                 size += 8 + 4 + w_strlen(pThis->u.u_headstyle.typenamb);
6126             if (pThis->u.u_headstyle.typenamib)
6127                 size += 9 + 4 + w_strlen(pThis->u.u_headstyle.typenamib);
6128             if (pThis->u.u_headstyle.style)
6129                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.style);
6130             if (pThis->u.u_headstyle.spacing)
6131                 size += 7 + 4 + w_strlen(pThis->u.u_headstyle.spacing);
6132             if (pThis->u.u_headstyle.weight)
6133                 size += 6 + 4 + w_strlen(pThis->u.u_headstyle.weight);
6134             if (pThis->u.u_headstyle.slant)
6135                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.slant);
6136             if (pThis->u.u_headstyle.special)
6137                 size += 7 + 4 + w_strlen(pThis->u.u_headstyle.special);
6138             if (pThis->u.u_headstyle.l_margin)
6139                 size += 8 + 4 + w_strlen(pThis->u.u_headstyle.l_margin);
6140             if (pThis->u.u_headstyle.r_margin)
6141                 size += 8 + 4 + w_strlen(pThis->u.u_headstyle.r_margin);
6142             if (pThis->u.u_headstyle.t_margin)
6143                 size += 8 + 4 + w_strlen(pThis->u.u_headstyle.t_margin);
6144             if (pThis->u.u_headstyle.b_margin)
6145                 size += 8 + 4 + w_strlen(pThis->u.u_headstyle.b_margin);
6146             if (pThis->u.u_headstyle.border)
6147                 size += 6 + 4 + w_strlen(pThis->u.u_headstyle.border);
6148             if (pThis->u.u_headstyle.vjust)
6149                 size += 5 + 4 + w_strlen(pThis->u.u_headstyle.vjust);
6150             if (pThis->u.u_headstyle.justify)
6151                 size += 7 + 4 + w_strlen(pThis->u.u_headstyle.justify);
6152             size += 2 /* strlen(">\n") */;
6153             break;
6154         case e_formstyle:
6155             if (!pThis->u.u_formstyle.beenUsed)
6156                 break;
6157             size += 10 /* strlen("<FORMSTYLE") */;
6158             if (pThis->u.u_formstyle.class)
6159                 size += 5 + 4 + w_strlen(pThis->u.u_formstyle.class);
6160             if (pThis->u.u_formstyle.ssi)
6161                 size += 3 + 4 + w_strlen(pThis->u.u_formstyle.ssi);
6162             if (pThis->u.u_formstyle.rlevel)
6163                 size += 6 + 4 + w_strlen(pThis->u.u_formstyle.rlevel);
6164             if (pThis->u.u_formstyle.phrase)
6165                 size += 6 + 4 + w_strlen(pThis->u.u_formstyle.phrase);
6166             if (pThis->u.u_formstyle.srch_wt)
6167                 size += 7 + 4 + w_strlen(pThis->u.u_formstyle.srch_wt);
6168             if (pThis->u.u_formstyle.colw)
6169                 size += 4 + 4 + w_strlen(pThis->u.u_formstyle.colw);
6170             if (pThis->u.u_formstyle.colj)
6171                 size += 4 + 4 + w_strlen(pThis->u.u_formstyle.colj);
6172             if (pThis->u.u_formstyle.pointsz)
6173                 size += 7 + 4 + w_strlen(pThis->u.u_formstyle.pointsz);
6174             if (pThis->u.u_formstyle.set_width)
6175                 size += 9 + 4 + w_strlen(pThis->u.u_formstyle.set_width);
6176             if (pThis->u.u_formstyle.color)
6177                 size += 5 + 4 + w_strlen(pThis->u.u_formstyle.color);
6178             if (pThis->u.u_formstyle.xlfd)
6179                 size += 4 + 4 + w_strlen(pThis->u.u_formstyle.xlfd);
6180             if (pThis->u.u_formstyle.xlfdi)
6181                 size += 5 + 4 + w_strlen(pThis->u.u_formstyle.xlfdi);
6182             if (pThis->u.u_formstyle.xlfdb)
6183                 size += 5 + 4 + w_strlen(pThis->u.u_formstyle.xlfdb);
6184             if (pThis->u.u_formstyle.xlfdib)
6185                 size += 6 + 4 + w_strlen(pThis->u.u_formstyle.xlfdib);
6186             if (pThis->u.u_formstyle.typenam)
6187                 size += 7 + 4 + w_strlen(pThis->u.u_formstyle.typenam);
6188             if (pThis->u.u_formstyle.typenami)
6189                 size += 8 + 4 + w_strlen(pThis->u.u_formstyle.typenami);
6190             if (pThis->u.u_formstyle.typenamb)
6191                 size += 8 + 4 + w_strlen(pThis->u.u_formstyle.typenamb);
6192             if (pThis->u.u_formstyle.typenamib)
6193                 size += 9 + 4 + w_strlen(pThis->u.u_formstyle.typenamib);
6194             if (pThis->u.u_formstyle.style)
6195                 size += 5 + 4 + w_strlen(pThis->u.u_formstyle.style);
6196             if (pThis->u.u_formstyle.spacing)
6197                 size += 7 + 4 + w_strlen(pThis->u.u_formstyle.spacing);
6198             if (pThis->u.u_formstyle.weight)
6199                 size += 6 + 4 + w_strlen(pThis->u.u_formstyle.weight);
6200             if (pThis->u.u_formstyle.slant)
6201                 size += 5 + 4 + w_strlen(pThis->u.u_formstyle.slant);
6202             if (pThis->u.u_formstyle.special)
6203                 size += 7 + 4 + w_strlen(pThis->u.u_formstyle.special);
6204             if (pThis->u.u_formstyle.l_margin)
6205                 size += 8 + 4 + w_strlen(pThis->u.u_formstyle.l_margin);
6206             if (pThis->u.u_formstyle.r_margin)
6207                 size += 8 + 4 + w_strlen(pThis->u.u_formstyle.r_margin);
6208             if (pThis->u.u_formstyle.t_margin)
6209                 size += 8 + 4 + w_strlen(pThis->u.u_formstyle.t_margin);
6210             if (pThis->u.u_formstyle.b_margin)
6211                 size += 8 + 4 + w_strlen(pThis->u.u_formstyle.b_margin);
6212             if (pThis->u.u_formstyle.border)
6213                 size += 6 + 4 + w_strlen(pThis->u.u_formstyle.border);
6214             if (pThis->u.u_formstyle.vjust)
6215                 size += 5 + 4 + w_strlen(pThis->u.u_formstyle.vjust);
6216             size += 2 /* strlen(">\n") */;
6217             break;
6218         case e_frmtstyle:
6219             if (!pThis->u.u_frmtstyle.beenUsed)
6220                 break;
6221             size += 10 /* strlen("<FRMTSTYLE") */;
6222             if (pThis->u.u_frmtstyle.class)
6223                 size += 5 + 4 + w_strlen(pThis->u.u_frmtstyle.class);
6224             if (pThis->u.u_frmtstyle.ssi)
6225                 size += 3 + 4 + w_strlen(pThis->u.u_frmtstyle.ssi);
6226             if (pThis->u.u_frmtstyle.rlevel)
6227                 size += 6 + 4 + w_strlen(pThis->u.u_frmtstyle.rlevel);
6228             if (pThis->u.u_frmtstyle.phrase)
6229                 size += 6 + 4 + w_strlen(pThis->u.u_frmtstyle.phrase);
6230             if (pThis->u.u_frmtstyle.f_margin)
6231                 size += 8 + 4 + w_strlen(pThis->u.u_frmtstyle.f_margin);
6232             if (pThis->u.u_frmtstyle.srch_wt)
6233                 size += 7 + 4 + w_strlen(pThis->u.u_frmtstyle.srch_wt);
6234             if (pThis->u.u_frmtstyle.pointsz)
6235                 size += 7 + 4 + w_strlen(pThis->u.u_frmtstyle.pointsz);
6236             if (pThis->u.u_frmtstyle.set_width)
6237                 size += 9 + 4 + w_strlen(pThis->u.u_frmtstyle.set_width);
6238             if (pThis->u.u_frmtstyle.color)
6239                 size += 5 + 4 + w_strlen(pThis->u.u_frmtstyle.color);
6240             if (pThis->u.u_frmtstyle.xlfd)
6241                 size += 4 + 4 + w_strlen(pThis->u.u_frmtstyle.xlfd);
6242             if (pThis->u.u_frmtstyle.xlfdi)
6243                 size += 5 + 4 + w_strlen(pThis->u.u_frmtstyle.xlfdi);
6244             if (pThis->u.u_frmtstyle.xlfdb)
6245                 size += 5 + 4 + w_strlen(pThis->u.u_frmtstyle.xlfdb);
6246             if (pThis->u.u_frmtstyle.xlfdib)
6247                 size += 6 + 4 + w_strlen(pThis->u.u_frmtstyle.xlfdib);
6248             if (pThis->u.u_frmtstyle.typenam)
6249                 size += 7 + 4 + w_strlen(pThis->u.u_frmtstyle.typenam);
6250             if (pThis->u.u_frmtstyle.typenami)
6251                 size += 8 + 4 + w_strlen(pThis->u.u_frmtstyle.typenami);
6252             if (pThis->u.u_frmtstyle.typenamb)
6253                 size += 8 + 4 + w_strlen(pThis->u.u_frmtstyle.typenamb);
6254             if (pThis->u.u_frmtstyle.typenamib)
6255                 size += 9 + 4 + w_strlen(pThis->u.u_frmtstyle.typenamib);
6256             if (pThis->u.u_frmtstyle.style)
6257                 size += 5 + 4 + w_strlen(pThis->u.u_frmtstyle.style);
6258             if (pThis->u.u_frmtstyle.spacing)
6259                 size += 7 + 4 + w_strlen(pThis->u.u_frmtstyle.spacing);
6260             if (pThis->u.u_frmtstyle.weight)
6261                 size += 6 + 4 + w_strlen(pThis->u.u_frmtstyle.weight);
6262             if (pThis->u.u_frmtstyle.slant)
6263                 size += 5 + 4 + w_strlen(pThis->u.u_frmtstyle.slant);
6264             if (pThis->u.u_frmtstyle.special)
6265                 size += 7 + 4 + w_strlen(pThis->u.u_frmtstyle.special);
6266             if (pThis->u.u_frmtstyle.l_margin)
6267                 size += 8 + 4 + w_strlen(pThis->u.u_frmtstyle.l_margin);
6268             if (pThis->u.u_frmtstyle.r_margin)
6269                 size += 8 + 4 + w_strlen(pThis->u.u_frmtstyle.r_margin);
6270             if (pThis->u.u_frmtstyle.t_margin)
6271                 size += 8 + 4 + w_strlen(pThis->u.u_frmtstyle.t_margin);
6272             if (pThis->u.u_frmtstyle.b_margin)
6273                 size += 8 + 4 + w_strlen(pThis->u.u_frmtstyle.b_margin);
6274             if (pThis->u.u_frmtstyle.border)
6275                 size += 6 + 4 + w_strlen(pThis->u.u_frmtstyle.border);
6276             if (pThis->u.u_frmtstyle.vjust)
6277                 size += 5 + 4 + w_strlen(pThis->u.u_frmtstyle.vjust);
6278             if (pThis->u.u_frmtstyle.justify)
6279                 size += 7 + 4 + w_strlen(pThis->u.u_frmtstyle.justify);
6280             size += 2 /* strlen(">\n") */;
6281             break;
6282         case e_grphstyle:
6283             if (!pThis->u.u_grphstyle.beenUsed)
6284                 break;
6285             size += 10 /* strlen("<GRPHSTYLE") */;
6286             if (pThis->u.u_grphstyle.class)
6287                 size += 5 + 4 + w_strlen(pThis->u.u_grphstyle.class);
6288             if (pThis->u.u_grphstyle.ssi)
6289                 size += 3 + 4 + w_strlen(pThis->u.u_grphstyle.ssi);
6290             if (pThis->u.u_grphstyle.rlevel)
6291                 size += 6 + 4 + w_strlen(pThis->u.u_grphstyle.rlevel);
6292             if (pThis->u.u_grphstyle.phrase)
6293                 size += 6 + 4 + w_strlen(pThis->u.u_grphstyle.phrase);
6294             if (pThis->u.u_grphstyle.l_margin)
6295                 size += 8 + 4 + w_strlen(pThis->u.u_grphstyle.l_margin);
6296             if (pThis->u.u_grphstyle.r_margin)
6297                 size += 8 + 4 + w_strlen(pThis->u.u_grphstyle.r_margin);
6298             if (pThis->u.u_grphstyle.t_margin)
6299                 size += 8 + 4 + w_strlen(pThis->u.u_grphstyle.t_margin);
6300             if (pThis->u.u_grphstyle.b_margin)
6301                 size += 8 + 4 + w_strlen(pThis->u.u_grphstyle.b_margin);
6302             if (pThis->u.u_grphstyle.border)
6303                 size += 6 + 4 + w_strlen(pThis->u.u_grphstyle.border);
6304             if (pThis->u.u_grphstyle.vjust)
6305                 size += 5 + 4 + w_strlen(pThis->u.u_grphstyle.vjust);
6306             if (pThis->u.u_grphstyle.justify)
6307                 size += 7 + 4 + w_strlen(pThis->u.u_grphstyle.justify);
6308             size += 2 /* strlen(">\n") */;
6309             break;
6310         }
6311     pThis = pThis->pNext;
6312     }
6313 size += 8 /* strlen("</TOSS>\n") */;
6314
6315 return size;
6316 }
6317
6318
6319 /* iterate over the LOIDS computing the offsets as we remove the old
6320  * LOIDS and INDEX and replace them with the new
6321 */ 
6322 void IterateLoids()
6323 {
6324 int       replacing, length, incr, tmp;
6325 int       loidsSize;
6326 char      buffer[BUFSIZ];
6327 LoidsPtr  pLoids;
6328
6329 /* write the loids out */
6330 pLoids = loids.next;
6331 loidsSize = sprintf(buffer, openLoids, nIds);
6332 while (pLoids)
6333     {
6334     loidsSize += sprintf(buffer,
6335                          "<ID TYPE=\"%s\" RID=\"%s\"",
6336                          pLoids->type,
6337                          pLoids->rid);
6338     if (pLoids->rssi)
6339         loidsSize += sprintf(buffer, " RSSI=\"%s\"", pLoids->rssi);
6340     if (pLoids->rlevel)
6341         loidsSize += sprintf(buffer, " RLEVEL=\"%s\"", pLoids->rlevel);
6342     loidsSize += sprintf(buffer, " OFFSET=\"%ld\">\n", pLoids->offset);
6343     pLoids = pLoids->next;
6344     }
6345 loidsSize += sizeof(closeLoids) - 1;
6346
6347 /* compute how much we are deleting from and adding to the <vstruct> */
6348 replacing = endOfLOIDS - startOfLOIDS;
6349 if (haveIndex)
6350     {
6351     if (startOfIndex != 0)
6352         replacing += (endOfIndex - startOfIndex);
6353     replacing -=  FileSize(sortedIdxFileName, xxx);
6354     }
6355 if (minimalTossFlag)
6356     {
6357     if (startOfTOSS != 0)
6358         replacing += (endOfTOSS - startOfTOSS);
6359     replacing -= RestrictedTossSize();
6360     }
6361 incr = length = loidsSize - replacing;
6362
6363 /* Iterate over the <loids>, updating the offsets by "incr" until no
6364  * change in the size of the <loids>.  The "incr" may be positive or
6365  * negative based on original sizes of the elements and their
6366  * replacements.
6367 */
6368 while (incr)
6369     {
6370     loidsSize = sprintf(buffer, openLoids, nIds);
6371     pLoids = loids.next; /* first one is a dummy (simplified some logic) */
6372     while (pLoids)
6373         {
6374         pLoids->offset += incr;
6375         loidsSize += sprintf(buffer,
6376                              "<ID TYPE=\"%s\" RID=\"%s\" ",
6377                              pLoids->type,
6378                              pLoids->rid);
6379         if (pLoids->rssi)
6380             loidsSize += sprintf(buffer, "RSSI=\"%s\" ", pLoids->rssi);
6381         if (pLoids->rlevel)
6382             loidsSize += sprintf(buffer, "RLEVEL=\"%s\" ", pLoids->rlevel);
6383         loidsSize += sprintf(buffer, "OFFSET=\"%ld\">\n", pLoids->offset);
6384         pLoids = pLoids->next;
6385         }
6386     loidsSize += sizeof(closeLoids) - 1;
6387     tmp    = loidsSize - replacing;
6388     incr   = tmp - length;
6389     length = tmp;
6390     }
6391 }
6392
6393
6394 /* iterate compressing vstruct until we have a stable loids; return
6395  * the amount of padding needed in between the vstruct and the first
6396  * virpage to make everything work
6397 */
6398 static void CompressVstruct()
6399 {
6400 #define COMPRESS_PAD_LENGTH 10
6401 char      buffer[BUFSIZ], zTemp[4];
6402 char     *pc;
6403 int       length, delta, change, pad;
6404 int       newZsize, oldZsize;
6405 int       inFd, idxFd;
6406 LoidsPtr  pLoids;
6407 static char      padString[COMPRESS_PAD_LENGTH];
6408
6409 pc = padString;
6410 length = COMPRESS_PAD_LENGTH;
6411 while (--length >= 0)
6412   *pc++ = (char)0xff;
6413
6414 FcloseFile(outFile, compFileName, xxx);
6415 newZsize = Compress(compFileName, compZFileName, TRUE) + 4;
6416 change   = FileSize(compFileName, xxx) - newZsize;
6417
6418 pad = 0;
6419 change += COMPRESS_PAD_LENGTH; /* compensate for adding pad on first round */
6420 while (1)
6421     {
6422     oldZsize = newZsize;
6423
6424     /* recompute loids taking compression into account */
6425     pLoids = loids.next; /* first one is a dummy (simplified some logic) */
6426     while (pLoids)
6427         {
6428         pLoids->offset = pLoids->offset - change + COMPRESS_PAD_LENGTH;
6429         pLoids = pLoids->next;
6430         }
6431
6432     inFd    = OpenFile(compFileName, O_RDONLY, xxx);
6433     unlink(compFileName);
6434     outFile = FopenFile(compFileName, O_WRONLY, xxx);
6435
6436     /* copy inFd to outFile up to start of <loids> */
6437     delta = startOfLOIDS;
6438     while (length = ReadFile(inFd,
6439                              compFileName,
6440                              buffer,
6441                              MIN(BUFSIZ,delta),
6442                              xxx))
6443         {
6444         FwriteFile(outFile, compFileName, buffer, length, xxx);
6445         delta -= length;
6446         }
6447
6448     /* write out the start-tag for <loids> */
6449     fprintf(outFile, openLoids, nIds);
6450
6451     /* and write out the new <loids> */
6452     pLoids = loids.next; /* first one is a dummy (simplified some logic) */
6453     while (pLoids)
6454         {
6455         fprintf(outFile,
6456                 "<ID TYPE=\"%s\" RID=\"%s\" ",
6457                 pLoids->type,
6458                 pLoids->rid);
6459         if (pLoids->rssi)
6460             fprintf(outFile, "RSSI=\"%s\" ", pLoids->rssi);
6461         if (pLoids->rlevel)
6462             fprintf(outFile, "RLEVEL=\"%s\" ", pLoids->rlevel);
6463         fprintf(outFile, "OFFSET=\"%ld\">\n", pLoids->offset);
6464         pLoids = pLoids->next;
6465         }
6466
6467     /* followed by the end-tag for <loids> */
6468     FwriteFile(outFile,
6469                compFileName,
6470                closeLoids,
6471                sizeof(closeLoids)-1,
6472                xxx);
6473
6474     /* and skip over the old <loids> in inFd */
6475     lseek(inFd, endOfLOIDS, SEEK_SET);
6476
6477     /* save new <loids> end */
6478     endOfLOIDS = ftell(outFile);
6479
6480     /* copy the remainder of the vstruct and doc head+snb to outFile */
6481     while (length = ReadFile(inFd, compFileName, buffer, BUFSIZ, xxx))
6482         FwriteFile(outFile, compFileName, buffer, length, xxx);
6483     CloseFile(inFd, compFileName, xxx);
6484     FcloseFile(outFile, compFileName, xxx);
6485
6486     newZsize = Compress(compFileName, compZFileName, TRUE) + 4;
6487
6488     if (newZsize <= (oldZsize + pad))
6489         {
6490         pad += (oldZsize - newZsize);
6491         break;
6492         }
6493
6494     change = oldZsize - newZsize;
6495     pad += COMPRESS_PAD_LENGTH;
6496     }
6497
6498 /* open the final output file */
6499 if ((*outFileName == '-') && (outFileName[1] == '\0'))
6500     {
6501     outFile = stdout;
6502     outFileName = "<stdout>";
6503     }
6504 else
6505     {
6506     outFile = FopenFile(outFileName, O_WRONLY, xxx);
6507     }
6508
6509 /* write out the magic number and the file size */
6510 newZsize -= 4; /* remove the magic number and file size byte count */
6511 zTemp[3] = newZsize % 256;
6512 newZsize /= 256;
6513 zTemp[2] = newZsize % 256;
6514 newZsize /= 256;
6515 zTemp[1] = newZsize % 256;
6516 zTemp[0] = '\0';
6517 FwriteFile(outFile, outFileName, zTemp, 4, xxx);
6518
6519 /* copy the compressed vstruct, doc head+snb to output */
6520 inFd = OpenFile(compZFileName, O_RDONLY, xxx);
6521 while (length = ReadFile(inFd, compZFileName, buffer, BUFSIZ, xxx))
6522     FwriteFile(outFile, outFileName, buffer, length, xxx);
6523 CloseFile(inFd, compZFileName, xxx);
6524 unlink(compZFileName);
6525 unlink(compFileName);
6526
6527 /* pad the output to keep the loids happy */
6528 while (pad >= 0)
6529     {
6530     FwriteFile(outFile,
6531                outFileName,
6532                padString,
6533                MIN(pad, COMPRESS_PAD_LENGTH),
6534                xxx);
6535     pad -= COMPRESS_PAD_LENGTH;
6536     }
6537 }
6538
6539
6540 /* insert the new loids and index then close the output file */
6541 static void IncorporateVstructElements()
6542 {
6543 char      buffer[BUFSIZ];
6544 int       length, delta;
6545 int       inFd, idxFd;
6546 char     *realOutFileName;
6547 LoidsPtr  pLoids;
6548 int       newEndOfLOIDS;
6549
6550 inFd = OpenFile(vstructFileName, O_RDONLY, xxx);
6551 if (compressFlag)
6552     {
6553     realOutFileName = compFileName;
6554     outFile = FopenFile(compFileName, O_WRONLY, xxx);
6555     }
6556 else
6557     {
6558     if ((*outFileName == '-') && (outFileName[1] == '\0'))
6559         {
6560         outFile = stdout;
6561         realOutFileName = "<stdout>";
6562         }
6563     else
6564         {
6565         realOutFileName = outFileName;
6566         outFile = FopenFile(outFileName, O_WRONLY, xxx);
6567         }
6568     }
6569
6570 /* copy inFd to outFile up to start of <loids> */
6571 delta = startOfLOIDS;
6572 while (length = ReadFile(inFd, inFileName, buffer, MIN(BUFSIZ,delta), xxx))
6573     {
6574     FwriteFile(outFile, realOutFileName, buffer, length, xxx);
6575     delta -= length;
6576     }
6577
6578 /* write out the start-tag for <loids> */
6579 fprintf(outFile, openLoids, nIds);
6580
6581 /* and write out the new <loids> */
6582 pLoids = loids.next; /* first one is a dummy (simplified some logic) */
6583 while (pLoids)
6584     {
6585     fprintf(outFile,
6586             "<ID TYPE=\"%s\" RID=\"%s\" ",
6587             pLoids->type,
6588             pLoids->rid);
6589     if (pLoids->rssi)
6590         fprintf(outFile, "RSSI=\"%s\" ", pLoids->rssi);
6591     if (pLoids->rlevel)
6592         fprintf(outFile, "RLEVEL=\"%s\" ", pLoids->rlevel);
6593     fprintf(outFile, "OFFSET=\"%ld\">\n", pLoids->offset + delta);
6594     pLoids = pLoids->next;
6595     }
6596
6597 /* followed by the end-tag for <loids> */
6598 FwriteFile(outFile,
6599            realOutFileName,
6600            closeLoids,
6601            sizeof(closeLoids)-1,
6602            xxx);
6603
6604 /* save the new end of <loids> */
6605 newEndOfLOIDS = ftell(outFile);
6606
6607 /* and skip over the old <loids> in inFd */
6608 lseek(inFd, endOfLOIDS, SEEK_SET);
6609
6610 delta = endOfTOSS;
6611 if (!minimalTossFlag) /* use the full <toss> from the document */
6612     {
6613     if (delta) /* an old <toss> is in the document, copy it to outFile */
6614         {
6615         delta -= endOfLOIDS;
6616         while (length = ReadFile(inFd,
6617                                  inFileName,
6618                                  buffer,
6619                                  MIN(BUFSIZ,delta),
6620                                  xxx))
6621             {
6622             FwriteFile(outFile, realOutFileName, buffer, length, xxx);
6623             delta -= length;
6624             }
6625         }
6626     }
6627 else /* emit only those <toss> elements actually used in the document */
6628     {
6629     if (delta) /* an old <toss> is in the document, blow it away */
6630         lseek(inFd, delta, SEEK_SET);
6631
6632     /* and emit the restricted <toss> */
6633     OpenNamedTag("TOSS", TRUE, TRUE);
6634     pElementChain = pTossChain;
6635     nElements     = nStyles;
6636     EmitSDL(NULL);
6637     CloseNamedTag("TOSS", TRUE);
6638     }
6639
6640 /* if we have an <lophrases>, copy it over to outFile */
6641 if (endOfLOPhrases)
6642     {
6643     delta = endOfLOPhrases - startOfLOPhrases;
6644     while (length = ReadFile(inFd,
6645                              inFileName,
6646                              buffer,
6647                              MIN(BUFSIZ,delta),
6648                              xxx))
6649         {
6650         FwriteFile(outFile, realOutFileName, buffer, length, xxx);
6651         delta -= length;
6652         }
6653     }
6654
6655 /* if we have a new index, write it out to outFile */
6656 if (haveIndex)
6657     {
6658     idxFd = OpenFile(sortedIdxFileName, O_RDONLY, xxx);
6659     while (length = ReadFile(idxFd, sortedIdxFileName, buffer, BUFSIZ, xxx))
6660         FwriteFile(outFile, realOutFileName, buffer, length, xxx);
6661     unlink(sortedIdxFileName);
6662     }
6663
6664 /* if we have a new index and there was an old index, blow it away */
6665 if (endOfIndex && haveIndex)
6666     lseek(inFd, endOfIndex, SEEK_SET);
6667
6668 /*  update the end of <loids> to reflect new size */
6669 endOfLOIDS = newEndOfLOIDS;
6670
6671 /* copy the remainder of the vstruct and doc head+snb to outFile */
6672 while (length = ReadFile(inFd, vstructFileName, buffer, BUFSIZ, xxx))
6673     FwriteFile(outFile, realOutFileName, buffer, length, xxx);
6674 CloseFile(inFd, vstructFileName, xxx);
6675 unlink(vstructFileName);
6676
6677 if (compressFlag)
6678     CompressVstruct();
6679
6680 /* then copy the virpages over to outFile */
6681 inFd = OpenFile(tempFileName, O_RDONLY, xxx);
6682 while (length = ReadFile(inFd, tempFileName, buffer, BUFSIZ, xxx))
6683     FwriteFile(outFile, outFileName, buffer, length, xxx);
6684 CloseFile(inFd, tempFileName, xxx);
6685
6686 FcloseFile(outFile, outFileName, xxx);
6687 unlink(tempFileName);
6688 }
6689
6690 /* Below is a modified version of m_cyclent() that returns a pointer
6691  * to the entity content rather than its value.  Returning a pointer
6692  * to the entity's content field allows it to be modified.
6693 */
6694 /* Cyclent.c contains procedure m_cyclent(), callable by interface
6695    designers, to cycle through all defined entities, returning information
6696    about them */
6697 #if defined(M_PROTO)
6698 static M_WCHAR *CycleEnt(LOGICAL init,
6699                          unsigned char *type,
6700                          M_WCHAR ***content,
6701                          unsigned char *wheredef)
6702 #else
6703 static M_WCHAR *m_CycleEnt(init, type, content, wheredef)
6704   LOGICAL init ;
6705   unsigned char *type ;
6706   M_WCHAR ***content ;
6707   unsigned char *wheredef ;
6708 #endif
6709 {
6710 static M_TRIE *current ;
6711 static M_TRIE *ancestor[M_NAMELEN + 1] ;
6712 static length = 0 ;
6713 static M_WCHAR name[M_NAMELEN + 1] ;
6714
6715 if (init)
6716     {
6717     current = m_enttrie->data ;
6718     length = 0 ;
6719     }
6720
6721 if (length < 0) return(NULL) ;
6722
6723 while (current->symbol)
6724     {
6725     ancestor[length] = current ;
6726     name[length++] = current->symbol ;
6727     current = current->data ;
6728     }
6729 name[length] = M_EOS ;
6730
6731 *type     =  ((M_ENTITY *)  current->data)->type ;
6732 *content  = &(((M_ENTITY *) current->data)->content) ;
6733 *wheredef =  ((M_ENTITY *)  current->data)->wheredef ;
6734
6735 while (length >= 0)
6736     {
6737     if (current->next)
6738         {
6739         current = current->next ;
6740         break ;
6741         }
6742     length-- ;
6743     if (length < 0) break ;
6744     current = ancestor[length] ;
6745     }
6746
6747 return(name) ;
6748 }
6749
6750 /* A routine to examine all defined entities looking for ones of type
6751  * M_SYSTEM.  When found, if the entity was defined in the interface
6752  * file and its name is "TOSSFILE", change its content to be the
6753  * current file name base plus ".tss".
6754 */
6755 void UpdateTossFileName()
6756 {
6757 unsigned char type;
6758 unsigned char wheredef;
6759 M_WCHAR **content;
6760 M_WCHAR  *name;
6761 M_WCHAR  *newContent;
6762 char     *toss_name = "TOSSFILE";
6763 char     *mb_name;
6764
6765 name = CycleEnt(TRUE, &type, &content, &wheredef);
6766 if (!name) return;
6767
6768 do  {
6769     if ((type == M_SYSTEM) && (wheredef == M_DELTDEF) && name && *content)
6770         {
6771         mb_name = MakeMByteString(name);
6772         if (strcmp(mb_name, toss_name) == 0)
6773             *content = MakeWideCharString(tossFileName);
6774         m_free(mb_name, "multi-byte toss file entity name");
6775         }
6776     }
6777 while (name = CycleEnt(FALSE, &type, &content, &wheredef));
6778 }
6779
6780
6781 /* A function that takes a language/charset pair and:
6782  *     if they are standard, leave them unchanged but get local
6783  *                           versions and setlocale(3) using those
6784  *     if they are local, setlocale(3) with them and replace them with
6785  *                        standard versions.
6786 */
6787 void
6788 #if defined(M_PROTO)
6789 SetLocale(M_WCHAR *pLang, M_WCHAR *pCharset)
6790 #else
6791 SetLocale(pLang, pCharset)
6792 M_WCHAR *pLang;
6793 M_WCHAR *pCharset;
6794 #endif
6795 {
6796 const char  cString[] = "C";
6797 _DtXlateDb  myDb = NULL;
6798 char        myPlatform[_DtPLATFORM_MAX_LEN+1];
6799 char        myLocale[256]; /* arbitrarily large */
6800 char       *mb_lang;
6801 char       *mb_charset;
6802 char       *locale;
6803 char       *lang;
6804 char       *charset;
6805 int         execVer;
6806 int         compVer;
6807 int         isStd;
6808
6809 if (!pLang && !pCharset)
6810     return;
6811
6812 mb_charset = NULL;
6813 if (pLang)
6814     mb_lang = MakeMByteString(pLang);
6815 else
6816     mb_lang = (char *) cString;
6817 if (pCharset)
6818     mb_charset = MakeMByteString(pCharset);
6819   
6820 strcpy(myLocale, mb_lang);
6821 if (mb_charset && *mb_charset)
6822     {
6823     strcat(myLocale, ".");
6824     strcat(myLocale, mb_charset);
6825     }
6826
6827 if ((_DtLcxOpenAllDbs(&myDb) != 0) ||
6828     (_DtXlateGetXlateEnv(myDb,myPlatform,&execVer,&compVer) != 0))
6829     {
6830     fprintf(stderr,
6831             "Warning: could not open locale translation database.\n");
6832     if (m_errfile != stderr)
6833         fprintf(m_errfile,
6834                 "Warning: could not open locale translation database.\n");
6835     if (mb_lang != cString)
6836         mb_free(&mb_lang);
6837     if (mb_charset)
6838         mb_free(&mb_charset);
6839     if (myDb != 0)
6840         _DtLcxCloseDb(&myDb);
6841     return;
6842     }
6843
6844 isStd = !_DtLcxXlateOpToStd(myDb,
6845                             "CDE",
6846                             0,
6847                             DtLCX_OPER_STD,
6848                             myLocale,
6849                             NULL,
6850                             NULL,
6851                             NULL,
6852                             NULL);
6853
6854 if (isStd)
6855     { /* already standard - get local versions and set locale */
6856     if (_DtLcxXlateStdToOp(myDb,
6857                            myPlatform,
6858                            compVer,
6859                            DtLCX_OPER_SETLOCALE,
6860                            myLocale,
6861                            NULL,
6862                            NULL,
6863                            NULL,
6864                            &locale))
6865         {
6866         fprintf(stderr,
6867                 "Warning: could not translate CDE locale to local\n");
6868         if (m_errfile != stderr)
6869             fprintf(m_errfile,
6870                     "Warning: could not translate CDE locale to local\n");
6871         _DtLcxCloseDb(&myDb);
6872         if (mb_lang != cString)
6873             mb_free(&mb_lang);
6874         if (mb_charset)
6875             mb_free(&mb_charset);
6876         return;
6877         }
6878     else
6879         {
6880         setlocale(LC_CTYPE, locale);
6881         operantLocale = mb_malloc(strlen(locale)+1);
6882         strcpy(operantLocale, locale);
6883         }
6884     }
6885 else
6886     { /* already local - just set locale */
6887     setlocale(LC_CTYPE, myLocale);
6888     operantLocale = mb_malloc(strlen(myLocale)+1);
6889     strcpy(operantLocale, myLocale);
6890     }
6891
6892 if (mb_lang != cString)
6893     mb_free(&mb_lang);
6894 if (mb_charset)
6895     mb_free(&mb_charset);
6896 _DtLcxCloseDb(&myDb);
6897 }
6898
6899
6900 /* a routine to do all the things we want to do before actually
6901  * parsing the SDL document.
6902 */
6903 void OpenDocument()
6904 {
6905 MakeFileNames();
6906 m_errfile = FopenFile(errFileName, O_WRONLY|O_APPEND, xxx);
6907
6908 if (decompressFlag)
6909     {
6910     DecompressSDL();
6911     }
6912 HandleSNB();
6913
6914 inFile    = FopenFile(tempFileName, O_RDONLY, xxx);
6915
6916 unlink(tempFileName);
6917
6918 outFile   = FopenFile(vstructFileName, O_WRONLY, xxx);
6919 }
6920
6921
6922 /* a routine to do all those things we want to do after parsing the
6923  * document such as doing the fixups in the vstruct
6924 */
6925 void CloseDocument()
6926 {
6927 FcloseFile(outFile, tempFileName, xxx);
6928 BuildIndex();
6929 IterateLoids();
6930 IncorporateVstructElements();
6931 if (m_errcnt)
6932     exit(xxx);
6933 exit(0);
6934 }