Abort with error if the app uses our api to subclass and then theirs
[wine] / dlls / richedit / reader.c
1 /*
2  * - Need to document error code meanings.
3  * - Need to do something with \* on destinations.
4  * - Make the parameter a long?
5  *
6  * reader.c - RTF file reader.  Release 1.10.
7  *
8  * ASCII 10 (\n) and 13 (\r) are ignored and silently discarded.
9  * Nulls are also discarded.
10  * (although the read hook will still get a look at them.)
11  *
12  * "\:" is not a ":", it's a control symbol.  But some versions of
13  * Word seem to write "\:" for ":".  This reader treats "\:" as a
14  * plain text ":"
15  *
16  * 19 Mar 93
17  * - Add hack to skip "{\*\keycode ... }" group in stylesheet.
18  * This is probably the wrong thing to do, but it's simple.
19  * 13 Jul 93
20  * - Add THINK C awareness to malloc() declaration.  Necessary so
21  * compiler knows the malloc argument is 4 bytes.  Ugh.
22  * 07 Sep 93
23  * - Text characters are mapped onto standard codes, which are placed
24  * in rtfMinor.
25  * - Eliminated use of index() function.
26  * 05 Mar 94
27  * - Added zillions of new symbols (those defined in RTF spec 1.2).
28  * 14 Mar 94
29  * - Public functions RTFMsg() and RTFPanic() now take variable arguments.
30  * This means RTFPanic() now is used in place of what was formerly the
31  * internal function Error().
32  * - 8-bit characters are now legal, so they're not converted to \'xx
33  * hex char representation now.
34  * 01 Apr 94
35  * - Added public variables rtfLineNum and rtfLinePos.
36  * - #include string.h or strings.h, avoiding strncmp() problem where
37  * last argument is treated as zero when prototype isn't available.
38  * 04 Apr 94
39  * - Treat style numbers 222 and 0 properly as "no style" and "normal".
40  *
41  * Author: Paul DuBois  dubois@primate.wisc.edu
42  *
43  * This software may be redistributed without restriction and used for
44  * any purpose whatsoever.
45  */
46
47 # ifndef STRING_H
48 # define STRING_H <string.h>
49 # endif
50
51 # include       <stdio.h>
52 # include       <ctype.h>
53 # include       STRING_H
54 # ifdef STDARG
55 # include       <stdarg.h>
56 # else
57 # ifdef VARARGS
58 # include       <varargs.h>
59 # endif /* VARARGS */
60 # endif /* STDARG */
61
62 # define        rtfInternal
63 # include       "rtf.h"
64 # undef         rtfInternal
65
66 /*
67  *  include hard coded charsets
68  */
69
70 #include "ansi_gen.h"
71 #include "ansi_sym.h"
72 #include "text_map.h"
73
74 #include <stdlib.h>
75
76 #include "charlist.h"
77 #include "windef.h"
78 #include "winbase.h"
79 #include "wine/debug.h"
80
81 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
82
83 extern HANDLE RICHED32_hHeap;
84
85 /*
86  * Return pointer to new element of type t, or NULL
87  * if no memory available.
88  */
89
90 # define        New(t)  ((t *) RTFAlloc ((int) sizeof (t)))
91
92 /* maximum number of character values representable in a byte */
93
94 # define        charSetSize             256
95
96 /* charset stack size */
97
98 # define        maxCSStack              10
99
100 static int      _RTFGetChar();
101 static void     _RTFGetToken ();
102 static void     _RTFGetToken2 ();
103 static int      GetChar ();
104 static void     ReadFontTbl ();
105 static void     ReadColorTbl ();
106 static void     ReadStyleSheet ();
107 static void     ReadInfoGroup ();
108 static void     ReadPictGroup ();
109 static void     ReadObjGroup ();
110 static void     LookupInit ();
111 static void     Lookup ();
112 static int      Hash ();
113
114 static void     CharSetInit ();
115 static void     ReadCharSetMaps ();
116
117
118 /*
119  * Public variables (listed in rtf.h)
120  */
121
122 int     rtfClass;
123 int     rtfMajor;
124 int     rtfMinor;
125 int     rtfParam;
126 int     rtfFormat;
127 char    *rtfTextBuf = (char *) NULL;
128 int     rtfTextLen;
129
130 long    rtfLineNum;
131 int     rtfLinePos;
132
133
134 /*
135  * Private stuff
136  */
137
138 static int      pushedChar;     /* pushback char if read too far */
139
140 static int      pushedClass;    /* pushed token info for RTFUngetToken() */
141 static int      pushedMajor;
142 static int      pushedMinor;
143 static int      pushedParam;
144 static char     *pushedTextBuf = (char *) NULL;
145
146 static int      prevChar;
147 static int      bumpLine;
148
149 static RTFFont  *fontList = (RTFFont *) NULL;   /* these lists MUST be */
150 static RTFColor *colorList = (RTFColor *) NULL; /* initialized to NULL */
151 static RTFStyle *styleList = (RTFStyle *) NULL;
152
153 static char     *inputName = (char *) NULL;
154 static char     *outputName = (char *) NULL;
155
156 static EDITSTREAM editstream;
157 static CHARLIST inputCharList = {0, NULL, NULL};
158
159 /*
160  * This array is used to map standard character names onto their numeric codes.
161  * The position of the name within the array is the code.
162  * stdcharnames.h is generated in the ../h directory.
163  */
164
165 #include "stdcharnames.h"
166
167 /*
168  * These arrays are used to map RTF input character values onto the standard
169  * character names represented by the values.  Input character values are
170  * used as indices into the arrays to produce standard character codes.
171  */
172
173
174 static char     *genCharSetFile = (char *) NULL;
175 static int      genCharCode[charSetSize];       /* general */
176 static int      haveGenCharSet = 0;
177
178 static char     *symCharSetFile = (char *) NULL;
179 static int      symCharCode[charSetSize];       /* symbol */
180 static int      haveSymCharSet = 0;
181
182 static int      curCharSet = rtfCSGeneral;
183 static int      *curCharCode = genCharCode;
184
185 /*
186  * By default, the reader is configured to handle charset mapping invisibly,
187  * including reading the charset files and switching charset maps as necessary
188  * for Symbol font.
189  */
190
191 static int      autoCharSetFlags;
192
193 /*
194  * Stack for keeping track of charset map on group begin/end.  This is
195  * necessary because group termination reverts the font to the previous
196  * value, which may implicitly change it.
197  */
198
199 static int      csStack[maxCSStack];
200 static int      csTop = 0;
201
202 /*
203  * Get a char from the charlist. The charlist is used to store characters
204  * from the editstream.
205  *
206  */
207
208 int _RTFGetChar(void)
209 {
210     char myChar;
211
212     TRACE("\n");
213
214     if(CHARLIST_GetNbItems(&inputCharList) == 0)
215     {
216         char buff[10];
217         long pcb;
218         editstream.pfnCallback(editstream.dwCookie, buff, 1, &pcb);
219         if(pcb == 0)
220            return EOF;
221         else
222            CHARLIST_Enqueue(&inputCharList, buff[0]);
223     }
224     myChar = CHARLIST_Dequeue(&inputCharList);
225     return (int) myChar;
226 }
227
228 void RTFSetEditStream(EDITSTREAM *es)
229 {
230     TRACE("\n");
231
232     editstream.dwCookie = es->dwCookie;
233     editstream.dwError  = es->dwError;
234     editstream.pfnCallback = es->pfnCallback;
235 }
236
237 /*
238  * Initialize the reader.  This may be called multiple times,
239  * to read multiple files.  The only thing not reset is the input
240  * stream; that must be done with RTFSetStream().
241  */
242
243 void RTFInit(void)
244 {
245 int     i;
246 RTFColor        *cp;
247 RTFFont         *fp;
248 RTFStyle        *sp;
249 RTFStyleElt     *eltList, *ep;
250
251     TRACE("\n");
252
253         if (rtfTextBuf == (char *) NULL)        /* initialize the text buffers */
254         {
255                 rtfTextBuf = RTFAlloc (rtfBufSiz);
256                 pushedTextBuf = RTFAlloc (rtfBufSiz);
257                 if (rtfTextBuf == (char *) NULL
258                         || pushedTextBuf == (char *) NULL)
259                         RTFPanic ("Cannot allocate text buffers.");
260                 rtfTextBuf[0] = pushedTextBuf[0] = '\0';
261         }
262
263         RTFFree (inputName);
264         RTFFree (outputName);
265         inputName = outputName = (char *) NULL;
266
267         /* initialize lookup table */
268         LookupInit ();
269
270         for (i = 0; i < rtfMaxClass; i++)
271                 RTFSetClassCallback (i, (RTFFuncPtr) NULL);
272         for (i = 0; i < rtfMaxDestination; i++)
273                 RTFSetDestinationCallback (i, (RTFFuncPtr) NULL);
274
275         /* install built-in destination readers */
276         RTFSetDestinationCallback (rtfFontTbl, ReadFontTbl);
277         RTFSetDestinationCallback (rtfColorTbl, ReadColorTbl);
278         RTFSetDestinationCallback (rtfStyleSheet, ReadStyleSheet);
279         RTFSetDestinationCallback (rtfInfo, ReadInfoGroup);
280         RTFSetDestinationCallback (rtfPict, ReadPictGroup);
281         RTFSetDestinationCallback (rtfObject, ReadObjGroup);
282
283
284         RTFSetReadHook ((RTFFuncPtr) NULL);
285
286         /* dump old lists if necessary */
287
288         while (fontList != (RTFFont *) NULL)
289         {
290                 fp = fontList->rtfNextFont;
291                 RTFFree (fontList->rtfFName);
292                 RTFFree ((char *) fontList);
293                 fontList = fp;
294         }
295         while (colorList != (RTFColor *) NULL)
296         {
297                 cp = colorList->rtfNextColor;
298                 RTFFree ((char *) colorList);
299                 colorList = cp;
300         }
301         while (styleList != (RTFStyle *) NULL)
302         {
303                 sp = styleList->rtfNextStyle;
304                 eltList = styleList->rtfSSEList;
305                 while (eltList != (RTFStyleElt *) NULL)
306                 {
307                         ep = eltList->rtfNextSE;
308                         RTFFree (eltList->rtfSEText);
309                         RTFFree ((char *) eltList);
310                         eltList = ep;
311                 }
312                 RTFFree (styleList->rtfSName);
313                 RTFFree ((char *) styleList);
314                 styleList = sp;
315         }
316
317         rtfClass = -1;
318         pushedClass = -1;
319         pushedChar = EOF;
320
321         rtfLineNum = 0;
322         rtfLinePos = 0;
323         prevChar = EOF;
324         bumpLine = 0;
325
326         CharSetInit ();
327         csTop = 0;
328 }
329
330 /*
331  * Set or get the input or output file name.  These are never guaranteed
332  * to be accurate, only insofar as the calling program makes them so.
333  */
334
335 void RTFSetInputName(char *name)
336 {
337     TRACE("\n");
338
339         if ((inputName = RTFStrSave (name)) == (char *) NULL)
340                 RTFPanic ("RTFSetInputName: out of memory");
341 }
342
343
344 char *RTFGetInputName(void)
345 {
346         return (inputName);
347 }
348
349
350 void RTFSetOutputName(char *name)
351 {
352     TRACE("\n");
353
354         if ((outputName = RTFStrSave (name)) == (char *) NULL)
355                 RTFPanic ("RTFSetOutputName: out of memory");
356 }
357
358
359 char *RTFGetOutputName(void)
360 {
361         return (outputName);
362 }
363
364
365
366 /* ---------------------------------------------------------------------- */
367
368 /*
369  * Callback table manipulation routines
370  */
371
372
373 /*
374  * Install or return a writer callback for a token class
375  */
376
377
378 static RTFFuncPtr       ccb[rtfMaxClass];               /* class callbacks */
379
380
381 void RTFSetClassCallback(int class, RTFFuncPtr callback)
382 {
383         if (class >= 0 && class < rtfMaxClass)
384                 ccb[class] = callback;
385 }
386
387
388 RTFFuncPtr RTFGetClassCallback(int class)
389 {
390         if (class >= 0 && class < rtfMaxClass)
391                 return (ccb[class]);
392         return ((RTFFuncPtr) NULL);
393 }
394
395
396 /*
397  * Install or return a writer callback for a destination type
398  */
399
400 static RTFFuncPtr       dcb[rtfMaxDestination]; /* destination callbacks */
401
402
403 void RTFSetDestinationCallback(int dest, RTFFuncPtr callback)
404 {
405         if (dest >= 0 && dest < rtfMaxDestination)
406                 dcb[dest] = callback;
407 }
408
409
410 RTFFuncPtr RTFGetDestinationCallback(int dest)
411 {
412         if (dest >= 0 && dest < rtfMaxDestination)
413                 return (dcb[dest]);
414         return ((RTFFuncPtr) NULL);
415 }
416
417
418 /* ---------------------------------------------------------------------- */
419
420 /*
421  * Token reading routines
422  */
423
424
425 /*
426  * Read the input stream, invoking the writer's callbacks
427  * where appropriate.
428  */
429
430 void RTFRead(void)
431 {
432         while (RTFGetToken () != rtfEOF)
433                 RTFRouteToken ();
434 }
435
436
437 /*
438  * Route a token.  If it's a destination for which a reader is
439  * installed, process the destination internally, otherwise
440  * pass the token to the writer's class callback.
441  */
442
443 void RTFRouteToken(void)
444 {
445 RTFFuncPtr      p;
446
447     TRACE("\n");
448
449         if (rtfClass < 0 || rtfClass >= rtfMaxClass)    /* watchdog */
450         {
451                 RTFPanic ("Unknown class %d: %s (reader malfunction)",
452                                                         rtfClass, rtfTextBuf);
453         }
454         if (RTFCheckCM (rtfControl, rtfDestination))
455         {
456                 /* invoke destination-specific callback if there is one */
457                 if ((p = RTFGetDestinationCallback (rtfMinor))
458                                                         != (RTFFuncPtr) NULL)
459                 {
460                         (*p) ();
461                         return;
462                 }
463         }
464         /* invoke class callback if there is one */
465         if ((p = RTFGetClassCallback (rtfClass)) != (RTFFuncPtr) NULL)
466                 (*p) ();
467 }
468
469
470 /*
471  * Skip to the end of the current group.  When this returns,
472  * writers that maintain a state stack may want to call their
473  * state unstacker; global vars will still be set to the group's
474  * closing brace.
475  */
476
477 void RTFSkipGroup(void)
478 {
479 int     level = 1;
480     TRACE("\n");
481
482         while (RTFGetToken () != rtfEOF)
483         {
484                 if (rtfClass == rtfGroup)
485                 {
486                         if (rtfMajor == rtfBeginGroup)
487                                 ++level;
488                         else if (rtfMajor == rtfEndGroup)
489                         {
490                                 if (--level < 1)
491                                         break;  /* end of initial group */
492                         }
493                 }
494         }
495 }
496
497
498 /*
499  * Read one token.  Call the read hook if there is one.  The
500  * token class is the return value.  Returns rtfEOF when there
501  * are no more tokens.
502  */
503
504 int RTFGetToken(void)
505 {
506 RTFFuncPtr      p;
507     TRACE("\n");
508
509         for (;;)
510         {
511                 _RTFGetToken ();
512                 if ((p = RTFGetReadHook ()) != (RTFFuncPtr) NULL)
513                         (*p) ();        /* give read hook a look at token */
514
515                 /* Silently discard newlines, carriage returns, nulls.  */
516                 if (!(rtfClass == rtfText && rtfFormat != SF_TEXT
517                         && (rtfMajor == '\r' || rtfMajor == '\n' || rtfMajor == '\0')))
518                         break;
519         }
520         return (rtfClass);
521 }
522
523
524 /*
525  * Install or return a token reader hook.
526  */
527
528 static RTFFuncPtr       readHook;
529
530
531 void RTFSetReadHook(RTFFuncPtr f)
532 {
533         readHook = f;
534 }
535
536
537 RTFFuncPtr RTFGetReadHook(void)
538 {
539         return (readHook);
540 }
541
542
543 void RTFUngetToken(void)
544 {
545     TRACE("\n");
546
547         if (pushedClass >= 0)   /* there's already an ungotten token */
548                 RTFPanic ("cannot unget two tokens");
549         if (rtfClass < 0)
550                 RTFPanic ("no token to unget");
551         pushedClass = rtfClass;
552         pushedMajor = rtfMajor;
553         pushedMinor = rtfMinor;
554         pushedParam = rtfParam;
555         (void) strcpy (pushedTextBuf, rtfTextBuf);
556 }
557
558
559 int RTFPeekToken(void)
560 {
561         _RTFGetToken ();
562         RTFUngetToken ();
563         return (rtfClass);
564 }
565
566
567 static void _RTFGetToken(void)
568 {
569 RTFFont *fp;
570
571     TRACE("\n");
572
573         if (rtfFormat == SF_TEXT) {
574             rtfMajor = GetChar ();
575             rtfMinor = rtfSC_nothing;
576             rtfParam = rtfNoParam;
577             rtfTextBuf[rtfTextLen = 0] = '\0';
578             if (rtfMajor == EOF)
579                 rtfClass = rtfEOF;
580             else
581                 rtfClass = rtfText;
582             return;
583         }
584
585         /* first check for pushed token from RTFUngetToken() */
586
587         if (pushedClass >= 0)
588         {
589                 rtfClass = pushedClass;
590                 rtfMajor = pushedMajor;
591                 rtfMinor = pushedMinor;
592                 rtfParam = pushedParam;
593                 (void) strcpy (rtfTextBuf, pushedTextBuf);
594                 rtfTextLen = strlen (rtfTextBuf);
595                 pushedClass = -1;
596                 return;
597         }
598
599         /*
600          * Beyond this point, no token is ever seen twice, which is
601          * important, e.g., for making sure no "}" pops the font stack twice.
602          */
603
604         _RTFGetToken2 ();
605         if (rtfClass == rtfText)        /* map RTF char to standard code */
606                 rtfMinor = RTFMapChar (rtfMajor);
607
608         /*
609          * If auto-charset stuff is activated, see if anything needs doing,
610          * like reading the charset maps or switching between them.
611          */
612
613         if (autoCharSetFlags == 0)
614                 return;
615
616         if ((autoCharSetFlags & rtfReadCharSet)
617                 && RTFCheckCM (rtfControl, rtfCharSet))
618         {
619                 ReadCharSetMaps ();
620         }
621         else if ((autoCharSetFlags & rtfSwitchCharSet)
622                 && RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
623         {
624                 if ((fp = RTFGetFont (rtfParam)) != (RTFFont *) NULL)
625                 {
626                         if (strncmp (fp->rtfFName, "Symbol", 6) == 0)
627                                 curCharSet = rtfCSSymbol;
628                         else
629                                 curCharSet = rtfCSGeneral;
630                         RTFSetCharSet (curCharSet);
631                 }
632         }
633         else if ((autoCharSetFlags & rtfSwitchCharSet) && rtfClass == rtfGroup)
634         {
635                 switch (rtfMajor)
636                 {
637                 case rtfBeginGroup:
638                         if (csTop >= maxCSStack)
639                                 RTFPanic ("_RTFGetToken: stack overflow");
640                         csStack[csTop++] = curCharSet;
641                         break;
642                 case rtfEndGroup:
643                         if (csTop <= 0)
644                                 RTFPanic ("_RTFGetToken: stack underflow");
645                         curCharSet = csStack[--csTop];
646                         RTFSetCharSet (curCharSet);
647                         break;
648                 }
649         }
650 }
651
652
653 /* this shouldn't be called anywhere but from _RTFGetToken() */
654
655 static void _RTFGetToken2(void)
656 {
657 int     sign;
658 int     c;
659
660     TRACE("\n");
661
662         /* initialize token vars */
663
664         rtfClass = rtfUnknown;
665         rtfParam = rtfNoParam;
666         rtfTextBuf[rtfTextLen = 0] = '\0';
667
668         /* get first character, which may be a pushback from previous token */
669
670         if (pushedChar != EOF)
671         {
672                 c = pushedChar;
673                 rtfTextBuf[rtfTextLen++] = c;
674                 rtfTextBuf[rtfTextLen] = '\0';
675                 pushedChar = EOF;
676         }
677         else if ((c = GetChar ()) == EOF)
678         {
679                 rtfClass = rtfEOF;
680                 return;
681         }
682
683         if (c == '{')
684         {
685                 rtfClass = rtfGroup;
686                 rtfMajor = rtfBeginGroup;
687                 return;
688         }
689         if (c == '}')
690         {
691                 rtfClass = rtfGroup;
692                 rtfMajor = rtfEndGroup;
693                 return;
694         }
695         if (c != '\\')
696         {
697                 /*
698                  * Two possibilities here:
699                  * 1) ASCII 9, effectively like \tab control symbol
700                  * 2) literal text char
701                  */
702                 if (c == '\t')                  /* ASCII 9 */
703                 {
704                         rtfClass = rtfControl;
705                         rtfMajor = rtfSpecialChar;
706                         rtfMinor = rtfTab;
707                 }
708                 else
709                 {
710                         rtfClass = rtfText;
711                         rtfMajor = c;
712                 }
713                 return;
714         }
715         if ((c = GetChar ()) == EOF)
716         {
717                 /* early eof, whoops (class is rtfUnknown) */
718                 return;
719         }
720         if (!isalpha (c))
721         {
722                 /*
723                  * Three possibilities here:
724                  * 1) hex encoded text char, e.g., \'d5, \'d3
725                  * 2) special escaped text char, e.g., \{, \}
726                  * 3) control symbol, e.g., \_, \-, \|, \<10>
727                  */
728                 if (c == '\'')                          /* hex char */
729                 {
730                 int     c2;
731
732                         if ((c = GetChar ()) != EOF && (c2 = GetChar ()) != EOF)
733                         {
734                                 /* should do isxdigit check! */
735                                 rtfClass = rtfText;
736                                 rtfMajor = RTFCharToHex (c) * 16
737                                                 + RTFCharToHex (c2);
738                                 return;
739                         }
740                         /* early eof, whoops (class is rtfUnknown) */
741                         return;
742                 }
743
744                 /* escaped char */
745                 /*if (index (":{}\\", c) != (char *) NULL)*/ /* escaped char */
746                 if (c == ':' || c == '{' || c == '}' || c == '\\')
747                 {
748                         rtfClass = rtfText;
749                         rtfMajor = c;
750                         return;
751                 }
752
753                 /* control symbol */
754                 Lookup (rtfTextBuf);    /* sets class, major, minor */
755                 return;
756         }
757         /* control word */
758         while (isalpha (c))
759         {
760                 if ((c = GetChar ()) == EOF)
761                         break;
762         }
763
764         /*
765          * At this point, the control word is all collected, so the
766          * major/minor numbers are determined before the parameter
767          * (if any) is scanned.  There will be one too many characters
768          * in the buffer, though, so fix up before and restore after
769          * looking up.
770          */
771
772         if (c != EOF)
773                 rtfTextBuf[rtfTextLen-1] = '\0';
774         Lookup (rtfTextBuf);    /* sets class, major, minor */
775         if (c != EOF)
776                 rtfTextBuf[rtfTextLen-1] = c;
777
778         /*
779          * Should be looking at first digit of parameter if there
780          * is one, unless it's negative.  In that case, next char
781          * is '-', so need to gobble next char, and remember sign.
782          */
783
784         sign = 1;
785         if (c == '-')
786         {
787                 sign = -1;
788                 c = GetChar ();
789         }
790         if (c != EOF && isdigit (c))
791         {
792                 rtfParam = 0;
793                 while (isdigit (c))     /* gobble parameter */
794                 {
795                         rtfParam = rtfParam * 10 + c - '0';
796                         if ((c = GetChar ()) == EOF)
797                                 break;
798                 }
799                 rtfParam *= sign;
800         }
801         /*
802          * If control symbol delimiter was a blank, gobble it.
803          * Otherwise the character is first char of next token, so
804          * push it back for next call.  In either case, delete the
805          * delimiter from the token buffer.
806          */
807         if (c != EOF)
808         {
809                 if (c != ' ')
810                         pushedChar = c;
811                 rtfTextBuf[--rtfTextLen] = '\0';
812         }
813 }
814
815
816 /*
817  * Read the next character from the input.  This handles setting the
818  * current line and position-within-line variables.  Those variable are
819  * set correctly whether lines end with CR, LF, or CRLF (the last being
820  * the tricky case).
821  *
822  * bumpLine indicates whether the line number should be incremented on
823  * the *next* input character.
824  */
825
826
827 static int GetChar(void)
828 {
829 int     c;
830 int     oldBumpLine;
831
832     TRACE("\n");
833
834         if ((c = _RTFGetChar()) != EOF)
835         {
836                 rtfTextBuf[rtfTextLen++] = c;
837                 rtfTextBuf[rtfTextLen] = '\0';
838         }
839         if (prevChar == EOF)
840                 bumpLine = 1;
841         oldBumpLine = bumpLine; /* non-zero if prev char was line ending */
842         bumpLine = 0;
843         if (c == '\r')
844                 bumpLine = 1;
845         else if (c == '\n')
846         {
847                 bumpLine = 1;
848                 if (prevChar == '\r')           /* oops, previous \r wasn't */
849                         oldBumpLine = 0;        /* really a line ending */
850         }
851         ++rtfLinePos;
852         if (oldBumpLine)        /* were we supposed to increment the */
853         {                       /* line count on this char? */
854                 ++rtfLineNum;
855                 rtfLinePos = 1;
856         }
857         prevChar = c;
858         return (c);
859 }
860
861
862 /*
863  * Synthesize a token by setting the global variables to the
864  * values supplied.  Typically this is followed with a call
865  * to RTFRouteToken().
866  *
867  * If a param value other than rtfNoParam is passed, it becomes
868  * part of the token text.
869  */
870
871 void RTFSetToken(int class, int major, int minor, int param, char *text)
872 {
873     TRACE("\n");
874
875         rtfClass = class;
876         rtfMajor = major;
877         rtfMinor = minor;
878         rtfParam = param;
879         if (param == rtfNoParam)
880                 (void) strcpy (rtfTextBuf, text);
881         else
882                 sprintf (rtfTextBuf, "%s%d", text, param);
883         rtfTextLen = strlen (rtfTextBuf);
884 }
885
886
887 /* ---------------------------------------------------------------------- */
888
889 /*
890  * Routines to handle mapping of RTF character sets
891  * onto standard characters.
892  *
893  * RTFStdCharCode(name) given char name, produce numeric code
894  * RTFStdCharName(code) given char code, return name
895  * RTFMapChar(c)        map input (RTF) char code to std code
896  * RTFSetCharSet(id)    select given charset map
897  * RTFGetCharSet()      get current charset map
898  *
899  * See ../h/README for more information about charset names and codes.
900  */
901
902
903 /*
904  * Initialize charset stuff.
905  */
906
907 static void CharSetInit(void)
908 {
909     TRACE("\n");
910
911         autoCharSetFlags = (rtfReadCharSet | rtfSwitchCharSet);
912         RTFFree (genCharSetFile);
913         genCharSetFile = (char *) NULL;
914         haveGenCharSet = 0;
915         RTFFree (symCharSetFile);
916         symCharSetFile = (char *) NULL;
917         haveSymCharSet = 0;
918         curCharSet = rtfCSGeneral;
919         curCharCode = genCharCode;
920 }
921
922
923 /*
924  * Specify the name of a file to be read when auto-charset-file reading is
925  * done.
926  */
927
928 void RTFSetCharSetMap (char *name, int csId)
929 {
930     TRACE("\n");
931
932         if ((name = RTFStrSave (name)) == (char *) NULL)        /* make copy */
933                 RTFPanic ("RTFSetCharSetMap: out of memory");
934         switch (csId)
935         {
936         case rtfCSGeneral:
937                 RTFFree (genCharSetFile);       /* free any previous value */
938                 genCharSetFile = name;
939                 break;
940         case rtfCSSymbol:
941                 RTFFree (symCharSetFile);       /* free any previous value */
942                 symCharSetFile = name;
943                 break;
944         }
945 }
946
947
948 /*
949  * Do auto-charset-file reading.
950  * will always use the ansi charset no mater what the value
951  * of the rtfTextBuf is.
952  *
953  * TODO: add support for other charset in the future.
954  *
955  */
956
957 static void ReadCharSetMaps(void)
958 {
959 char    buf[rtfBufSiz];
960
961     TRACE("\n");
962
963         if (genCharSetFile != (char *) NULL)
964                 (void) strcpy (buf, genCharSetFile);
965         else
966                 sprintf (buf, "%s-gen", &rtfTextBuf[1]);
967         if (RTFReadCharSetMap (rtfCSGeneral) == 0)
968                 RTFPanic ("ReadCharSetMaps: Cannot read charset map %s", buf);
969         if (symCharSetFile != (char *) NULL)
970             (void) strcpy (buf, symCharSetFile);
971         else
972                 sprintf (buf, "%s-sym", &rtfTextBuf[1]);
973         if (RTFReadCharSetMap (rtfCSSymbol) == 0)
974                 RTFPanic ("ReadCharSetMaps: Cannot read charset map %s", buf);
975 }
976
977
978
979 /*
980  * Convert a CaracterSetMap (caracter_name, caracter) into
981  * this form : array[caracter_ident] = caracter;
982  */
983
984 int RTFReadCharSetMap(int csId)
985 {
986         int     *stdCodeArray;
987         int i;
988
989     TRACE("\n");
990
991         switch (csId)
992         {
993         default:
994                 return (0);     /* illegal charset id */
995         case rtfCSGeneral:
996
997                 haveGenCharSet = 1;
998                 stdCodeArray = genCharCode;
999                 for (i = 0; i < charSetSize; i++)
1000                 {
1001                     stdCodeArray[i] = rtfSC_nothing;
1002                 }
1003
1004                 for ( i = 0 ; i< sizeof(ansi_gen)/(sizeof(int));i+=2)
1005                 {
1006                     stdCodeArray[ ansi_gen[i+1] ] = ansi_gen[i];
1007                 }
1008                 break;
1009
1010         case rtfCSSymbol:
1011
1012                 haveSymCharSet = 1;
1013                 stdCodeArray = symCharCode;
1014                 for (i = 0; i < charSetSize; i++)
1015                 {
1016                     stdCodeArray[i] = rtfSC_nothing;
1017                 }
1018
1019                 for ( i = 0 ; i< sizeof(ansi_sym)/(sizeof(int));i+=2)
1020                 {
1021                     stdCodeArray[ ansi_sym[i+1] ] = ansi_sym[i];
1022                 }
1023                 break;
1024         }
1025
1026         return (1);
1027 }
1028
1029
1030 /*
1031  * Given a standard character name (a string), find its code (a number).
1032  * Return -1 if name is unknown.
1033  */
1034
1035 int RTFStdCharCode(char *name)
1036 {
1037 int     i;
1038
1039     TRACE("\n");
1040
1041         for (i = 0; i < rtfSC_MaxChar; i++)
1042         {
1043                 if (strcmp (name, stdCharName[i]) == 0)
1044                         return (i);
1045         }
1046         return (-1);
1047 }
1048
1049
1050 /*
1051  * Given a standard character code (a number), find its name (a string).
1052  * Return NULL if code is unknown.
1053  */
1054
1055 char *RTFStdCharName(int code)
1056 {
1057         if (code < 0 || code >= rtfSC_MaxChar)
1058                 return ((char *) NULL);
1059         return (stdCharName[code]);
1060 }
1061
1062
1063 /*
1064  * Given an RTF input character code, find standard character code.
1065  * The translator should read the appropriate charset maps when it finds a
1066  * charset control.  However, the file might not contain one.  In this
1067  * case, no map will be available.  When the first attempt is made to
1068  * map a character under these circumstances, RTFMapChar() assumes ANSI
1069  * and reads the map as necessary.
1070  */
1071
1072 int RTFMapChar(int c)
1073 {
1074     TRACE("\n");
1075
1076         switch (curCharSet)
1077         {
1078         case rtfCSGeneral:
1079                 if (!haveGenCharSet)
1080                 {
1081                         if (RTFReadCharSetMap (rtfCSGeneral) == 0)
1082                                 RTFPanic ("RTFMapChar: cannot read ansi-gen");
1083                 }
1084                 break;
1085         case rtfCSSymbol:
1086                 if (!haveSymCharSet)
1087                 {
1088                         if (RTFReadCharSetMap (rtfCSSymbol) == 0)
1089                                 RTFPanic ("RTFMapChar: cannot read ansi-sym");
1090                 }
1091                 break;
1092         }
1093         if (c < 0 || c >= charSetSize)
1094                 return (rtfSC_nothing);
1095         return (curCharCode[c]);
1096 }
1097
1098
1099 /*
1100  * Set the current character set.  If csId is illegal, uses general charset.
1101  */
1102
1103 void RTFSetCharSet(int csId)
1104 {
1105     TRACE("\n");
1106
1107         switch (csId)
1108         {
1109         default:                /* use general if csId unknown */
1110         case rtfCSGeneral:
1111                 curCharCode = genCharCode;
1112                 curCharSet = csId;
1113                 break;
1114         case rtfCSSymbol:
1115                 curCharCode = symCharCode;
1116                 curCharSet = csId;
1117                 break;
1118         }
1119 }
1120
1121
1122 int RTFGetCharSet(void)
1123 {
1124         return (curCharSet);
1125 }
1126
1127
1128 /* ---------------------------------------------------------------------- */
1129
1130 /*
1131  * Special destination readers.  They gobble the destination so the
1132  * writer doesn't have to deal with them.  That's wrong for any
1133  * translator that wants to process any of these itself.  In that
1134  * case, these readers should be overridden by installing a different
1135  * destination callback.
1136  *
1137  * NOTE: The last token read by each of these reader will be the
1138  * destination's terminating '}', which will then be the current token.
1139  * That '}' token is passed to RTFRouteToken() - the writer has already
1140  * seen the '{' that began the destination group, and may have pushed a
1141  * state; it also needs to know at the end of the group that a state
1142  * should be popped.
1143  *
1144  * It's important that rtf.h and the control token lookup table list
1145  * as many symbols as possible, because these destination readers
1146  * unfortunately make strict assumptions about the input they expect,
1147  * and a token of class rtfUnknown will throw them off easily.
1148  */
1149
1150
1151 /*
1152  * Read { \fonttbl ... } destination.  Old font tables don't have
1153  * braces around each table entry; try to adjust for that.
1154  */
1155
1156 static void ReadFontTbl(void)
1157 {
1158 RTFFont *fp = NULL;
1159 char    buf[rtfBufSiz], *bp;
1160 int     old = -1;
1161 char    *fn = "ReadFontTbl";
1162
1163     TRACE("\n");
1164
1165         for (;;)
1166         {
1167                 (void) RTFGetToken ();
1168                 if (RTFCheckCM (rtfGroup, rtfEndGroup))
1169                         break;
1170                 if (old < 0)            /* first entry - determine tbl type */
1171                 {
1172                         if (RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
1173                                 old = 1;        /* no brace */
1174                         else if (RTFCheckCM (rtfGroup, rtfBeginGroup))
1175                                 old = 0;        /* brace */
1176                         else                    /* can't tell! */
1177                                 RTFPanic ("%s: Cannot determine format", fn);
1178                 }
1179                 if (old == 0)           /* need to find "{" here */
1180                 {
1181                         if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
1182                                 RTFPanic ("%s: missing \"{\"", fn);
1183                         (void) RTFGetToken ();  /* yes, skip to next token */
1184                 }
1185                 if ((fp = New (RTFFont)) == (RTFFont *) NULL)
1186                         RTFPanic ("%s: cannot allocate font entry", fn);
1187
1188                 fp->rtfNextFont = fontList;
1189                 fontList = fp;
1190
1191                 fp->rtfFName = (char *) NULL;
1192                 fp->rtfFAltName = (char *) NULL;
1193                 fp->rtfFNum = -1;
1194                 fp->rtfFFamily = 0;
1195                 fp->rtfFCharSet = 0;
1196                 fp->rtfFPitch = 0;
1197                 fp->rtfFType = 0;
1198                 fp->rtfFCodePage = 0;
1199
1200                 while (rtfClass != rtfEOF
1201                        && !RTFCheckCM (rtfText, ';')
1202                        && !RTFCheckCM (rtfGroup, rtfEndGroup))
1203                 {
1204                         if (rtfClass == rtfControl)
1205                         {
1206                                 switch (rtfMajor)
1207                                 {
1208                                 default:
1209                                         /* ignore token but announce it */
1210                                         RTFMsg ("%s: unknown token \"%s\"\n",
1211                                                         fn, rtfTextBuf);
1212                                 case rtfFontFamily:
1213                                         fp->rtfFFamily = rtfMinor;
1214                                         break;
1215                                 case rtfCharAttr:
1216                                         switch (rtfMinor)
1217                                         {
1218                                         default:
1219                                                 break;  /* ignore unknown? */
1220                                         case rtfFontNum:
1221                                                 fp->rtfFNum = rtfParam;
1222                                                 break;
1223                                         }
1224                                         break;
1225                                 case rtfFontAttr:
1226                                         switch (rtfMinor)
1227                                         {
1228                                         default:
1229                                                 break;  /* ignore unknown? */
1230                                         case rtfFontCharSet:
1231                                                 fp->rtfFCharSet = rtfParam;
1232                                                 break;
1233                                         case rtfFontPitch:
1234                                                 fp->rtfFPitch = rtfParam;
1235                                                 break;
1236                                         case rtfFontCodePage:
1237                                                 fp->rtfFCodePage = rtfParam;
1238                                                 break;
1239                                         case rtfFTypeNil:
1240                                         case rtfFTypeTrueType:
1241                                                 fp->rtfFType = rtfParam;
1242                                                 break;
1243                                         }
1244                                         break;
1245                                 }
1246                         }
1247                         else if (RTFCheckCM (rtfGroup, rtfBeginGroup))  /* dest */
1248                         {
1249                                 RTFSkipGroup ();        /* ignore for now */
1250                         }
1251                         else if (rtfClass == rtfText)   /* font name */
1252                         {
1253                                 bp = buf;
1254                                 while (rtfClass != rtfEOF
1255                                         && !RTFCheckCM (rtfText, ';')
1256                                         && !RTFCheckCM (rtfGroup, rtfEndGroup))
1257                                 {
1258                                         *bp++ = rtfMajor;
1259                                         (void) RTFGetToken ();
1260                                 }
1261
1262                                 /* FIX: in some cases the <fontinfo> isn't finished with a semi-column */
1263                                 if(RTFCheckCM (rtfGroup, rtfEndGroup))
1264                                 {
1265                                   RTFUngetToken ();
1266                                 }
1267                                 *bp = '\0';
1268                                 fp->rtfFName = RTFStrSave (buf);
1269                                 if (fp->rtfFName == (char *) NULL)
1270                                         RTFPanic ("%s: cannot allocate font name", fn);
1271                                 /* already have next token; don't read one */
1272                                 /* at bottom of loop */
1273                                 continue;
1274                         }
1275                         else
1276                         {
1277                                 /* ignore token but announce it */
1278                                 RTFMsg ("%s: unknown token \"%s\"\n",
1279                                                         fn, rtfTextBuf);
1280                         }
1281                         (void) RTFGetToken ();
1282                 }
1283                 if (old == 0)   /* need to see "}" here */
1284                 {
1285                         (void) RTFGetToken ();
1286                         if (!RTFCheckCM (rtfGroup, rtfEndGroup))
1287                                 RTFPanic ("%s: missing \"}\"", fn);
1288                 }
1289         }
1290         if (fp->rtfFNum == -1)
1291                 RTFPanic ("%s: missing font number", fn);
1292 /*
1293  * Could check other pieces of structure here, too, I suppose.
1294  */
1295         RTFRouteToken ();       /* feed "}" back to router */
1296 }
1297
1298
1299 /*
1300  * The color table entries have color values of -1 if
1301  * the default color should be used for the entry (only
1302  * a semi-colon is given in the definition, no color values).
1303  * There will be a problem if a partial entry (1 or 2 but
1304  * not 3 color values) is given.  The possibility is ignored
1305  * here.
1306  */
1307
1308 static void ReadColorTbl(void)
1309 {
1310 RTFColor        *cp;
1311 int             cnum = 0;
1312 char            *fn = "ReadColorTbl";
1313
1314     TRACE("\n");
1315
1316         for (;;)
1317         {
1318                 (void) RTFGetToken ();
1319                 if (RTFCheckCM (rtfGroup, rtfEndGroup))
1320                         break;
1321                 if ((cp = New (RTFColor)) == (RTFColor *) NULL)
1322                         RTFPanic ("%s: cannot allocate color entry", fn);
1323                 cp->rtfCNum = cnum++;
1324                 cp->rtfCRed = cp->rtfCGreen = cp->rtfCBlue = -1;
1325                 cp->rtfNextColor = colorList;
1326                 colorList = cp;
1327                 while (RTFCheckCM (rtfControl, rtfColorName))
1328                 {
1329                         switch (rtfMinor)
1330                         {
1331                         case rtfRed:    cp->rtfCRed = rtfParam; break;
1332                         case rtfGreen:  cp->rtfCGreen = rtfParam; break;
1333                         case rtfBlue:   cp->rtfCBlue = rtfParam; break;
1334                         }
1335                         RTFGetToken ();
1336                 }
1337                 if (!RTFCheckCM (rtfText, (int) ';'))
1338                         RTFPanic ("%s: malformed entry", fn);
1339         }
1340         RTFRouteToken ();       /* feed "}" back to router */
1341 }
1342
1343
1344 /*
1345  * The "Normal" style definition doesn't contain any style number,
1346  * all others do.  Normal style is given style rtfNormalStyleNum.
1347  */
1348
1349 static void ReadStyleSheet(void)
1350 {
1351 RTFStyle        *sp;
1352 RTFStyleElt     *sep, *sepLast;
1353 char            buf[rtfBufSiz], *bp;
1354 char            *fn = "ReadStyleSheet";
1355
1356     TRACE("\n");
1357
1358         for (;;)
1359         {
1360                 (void) RTFGetToken ();
1361                 if (RTFCheckCM (rtfGroup, rtfEndGroup))
1362                         break;
1363                 if ((sp = New (RTFStyle)) == (RTFStyle *) NULL)
1364                         RTFPanic ("%s: cannot allocate stylesheet entry", fn);
1365                 sp->rtfSName = (char *) NULL;
1366                 sp->rtfSNum = -1;
1367                 sp->rtfSType = rtfParStyle;
1368                 sp->rtfSAdditive = 0;
1369                 sp->rtfSBasedOn = rtfNoStyleNum;
1370                 sp->rtfSNextPar = -1;
1371                 sp->rtfSSEList = sepLast = (RTFStyleElt *) NULL;
1372                 sp->rtfNextStyle = styleList;
1373                 sp->rtfExpanding = 0;
1374                 styleList = sp;
1375                 if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
1376                         RTFPanic ("%s: missing \"{\"", fn);
1377                 for (;;)
1378                 {
1379                         (void) RTFGetToken ();
1380                         if (rtfClass == rtfEOF
1381                                 || RTFCheckCM (rtfText, ';'))
1382                                 break;
1383                         if (rtfClass == rtfControl)
1384                         {
1385                                 if (RTFCheckMM (rtfSpecialChar, rtfOptDest))
1386                                         continue;       /* ignore "\*" */
1387                                 if (RTFCheckMM (rtfParAttr, rtfStyleNum))
1388                                 {
1389                                         sp->rtfSNum = rtfParam;
1390                                         sp->rtfSType = rtfParStyle;
1391                                         continue;
1392                                 }
1393                                 if (RTFCheckMM (rtfCharAttr, rtfCharStyleNum))
1394                                 {
1395                                         sp->rtfSNum = rtfParam;
1396                                         sp->rtfSType = rtfCharStyle;
1397                                         continue;
1398                                 }
1399                                 if (RTFCheckMM (rtfSectAttr, rtfSectStyleNum))
1400                                 {
1401                                         sp->rtfSNum = rtfParam;
1402                                         sp->rtfSType = rtfSectStyle;
1403                                         continue;
1404                                 }
1405                                 if (RTFCheckMM (rtfStyleAttr, rtfBasedOn))
1406                                 {
1407                                         sp->rtfSBasedOn = rtfParam;
1408                                         continue;
1409                                 }
1410                                 if (RTFCheckMM (rtfStyleAttr, rtfAdditive))
1411                                 {
1412                                         sp->rtfSAdditive = 1;
1413                                         continue;
1414                                 }
1415                                 if (RTFCheckMM (rtfStyleAttr, rtfNext))
1416                                 {
1417                                         sp->rtfSNextPar = rtfParam;
1418                                         continue;
1419                                 }
1420                                 if ((sep = New (RTFStyleElt)) == (RTFStyleElt *) NULL)
1421                                         RTFPanic ("%s: cannot allocate style element", fn);
1422                                 sep->rtfSEClass = rtfClass;
1423                                 sep->rtfSEMajor = rtfMajor;
1424                                 sep->rtfSEMinor = rtfMinor;
1425                                 sep->rtfSEParam = rtfParam;
1426                                 if ((sep->rtfSEText = RTFStrSave (rtfTextBuf))
1427                                                                 == (char *) NULL)
1428                                         RTFPanic ("%s: cannot allocate style element text", fn);
1429                                 if (sepLast == (RTFStyleElt *) NULL)
1430                                         sp->rtfSSEList = sep;   /* first element */
1431                                 else                            /* add to end */
1432                                         sepLast->rtfNextSE = sep;
1433                                 sep->rtfNextSE = (RTFStyleElt *) NULL;
1434                                 sepLast = sep;
1435                         }
1436                         else if (RTFCheckCM (rtfGroup, rtfBeginGroup))
1437                         {
1438                                 /*
1439                                  * This passes over "{\*\keycode ... }, among
1440                                  * other things. A temporary (perhaps) hack.
1441                                  */
1442                                 RTFSkipGroup ();
1443                                 continue;
1444                         }
1445                         else if (rtfClass == rtfText)   /* style name */
1446                         {
1447                                 bp = buf;
1448                                 while (rtfClass == rtfText)
1449                                 {
1450                                         if (rtfMajor == ';')
1451                                         {
1452                                                 /* put back for "for" loop */
1453                                                 (void) RTFUngetToken ();
1454                                                 break;
1455                                         }
1456                                         *bp++ = rtfMajor;
1457                                         (void) RTFGetToken ();
1458                                 }
1459                                 *bp = '\0';
1460                                 if ((sp->rtfSName = RTFStrSave (buf)) == (char *) NULL)
1461                                         RTFPanic ("%s: cannot allocate style name", fn);
1462                         }
1463                         else            /* unrecognized */
1464                         {
1465                                 /* ignore token but announce it */
1466                                 RTFMsg ("%s: unknown token \"%s\"\n",
1467                                                         fn, rtfTextBuf);
1468                         }
1469                 }
1470                 (void) RTFGetToken ();
1471                 if (!RTFCheckCM (rtfGroup, rtfEndGroup))
1472                         RTFPanic ("%s: missing \"}\"", fn);
1473
1474                 /*
1475                  * Check over the style structure.  A name is a must.
1476                  * If no style number was specified, check whether it's the
1477                  * Normal style (in which case it's given style number
1478                  * rtfNormalStyleNum).  Note that some "normal" style names
1479                  * just begin with "Normal" and can have other stuff following,
1480                  * e.g., "Normal,Times 10 point".  Ugh.
1481                  *
1482                  * Some German RTF writers use "Standard" instead of "Normal".
1483                  */
1484                 if (sp->rtfSName == (char *) NULL)
1485                         RTFPanic ("%s: missing style name", fn);
1486                 if (sp->rtfSNum < 0)
1487                 {
1488                         if (strncmp (buf, "Normal", 6) != 0
1489                                 && strncmp (buf, "Standard", 8) != 0)
1490                                 RTFPanic ("%s: missing style number", fn);
1491                         sp->rtfSNum = rtfNormalStyleNum;
1492                 }
1493                 if (sp->rtfSNextPar == -1)      /* if \snext not given, */
1494                         sp->rtfSNextPar = sp->rtfSNum;  /* next is itself */
1495         }
1496         RTFRouteToken ();       /* feed "}" back to router */
1497 }
1498
1499
1500 static void ReadInfoGroup(void)
1501 {
1502         RTFSkipGroup ();
1503         RTFRouteToken ();       /* feed "}" back to router */
1504 }
1505
1506
1507 static void ReadPictGroup(void)
1508 {
1509         RTFSkipGroup ();
1510         RTFRouteToken ();       /* feed "}" back to router */
1511 }
1512
1513
1514 static void ReadObjGroup(void)
1515 {
1516         RTFSkipGroup ();
1517         RTFRouteToken ();       /* feed "}" back to router */
1518 }
1519
1520
1521 /* ---------------------------------------------------------------------- */
1522
1523 /*
1524  * Routines to return pieces of stylesheet, or font or color tables.
1525  * References to style 0 are mapped onto the Normal style.
1526  */
1527
1528
1529 RTFStyle *RTFGetStyle(int num)
1530 {
1531 RTFStyle        *s;
1532
1533         if (num == -1)
1534                 return (styleList);
1535         for (s = styleList; s != (RTFStyle *) NULL; s = s->rtfNextStyle)
1536         {
1537                 if (s->rtfSNum == num)
1538                         break;
1539         }
1540         return (s);             /* NULL if not found */
1541 }
1542
1543
1544 RTFFont *RTFGetFont(int num)
1545 {
1546 RTFFont *f;
1547
1548         if (num == -1)
1549                 return (fontList);
1550         for (f = fontList; f != (RTFFont *) NULL; f = f->rtfNextFont)
1551         {
1552                 if (f->rtfFNum == num)
1553                         break;
1554         }
1555         return (f);             /* NULL if not found */
1556 }
1557
1558
1559 RTFColor *RTFGetColor(int num)
1560 {
1561 RTFColor        *c;
1562
1563         if (num == -1)
1564                 return (colorList);
1565         for (c = colorList; c != (RTFColor *) NULL; c = c->rtfNextColor)
1566         {
1567                 if (c->rtfCNum == num)
1568                         break;
1569         }
1570         return (c);             /* NULL if not found */
1571 }
1572
1573
1574 /* ---------------------------------------------------------------------- */
1575
1576
1577 /*
1578  * Expand style n, if there is such a style.
1579  */
1580
1581 void RTFExpandStyle(int n)
1582 {
1583 RTFStyle        *s;
1584 RTFStyleElt     *se;
1585
1586     TRACE("\n");
1587
1588         if (n == -1 || (s = RTFGetStyle (n)) == (RTFStyle *) NULL)
1589                 return;
1590         if (s->rtfExpanding != 0)
1591                 RTFPanic ("Style expansion loop, style %d", n);
1592         s->rtfExpanding = 1;    /* set expansion flag for loop detection */
1593         /*
1594          * Expand "based-on" style (unless it's the same as the current
1595          * style -- Normal style usually gives itself as its own based-on
1596          * style).  Based-on style expansion is done by synthesizing
1597          * the token that the writer needs to see in order to trigger
1598          * another style expansion, and feeding to token back through
1599          * the router so the writer sees it.
1600          */
1601         if (n != s->rtfSBasedOn)
1602         {
1603                 RTFSetToken (rtfControl, rtfParAttr, rtfStyleNum,
1604                                                         s->rtfSBasedOn, "\\s");
1605                 RTFRouteToken ();
1606         }
1607         /*
1608          * Now route the tokens unique to this style.  RTFSetToken()
1609          * isn't used because it would add the param value to the end
1610          * of the token text, which already has it in.
1611          */
1612         for (se = s->rtfSSEList; se != (RTFStyleElt *) NULL; se = se->rtfNextSE)
1613         {
1614                 rtfClass = se->rtfSEClass;
1615                 rtfMajor = se->rtfSEMajor;
1616                 rtfMinor = se->rtfSEMinor;
1617                 rtfParam = se->rtfSEParam;
1618                 (void) strcpy (rtfTextBuf, se->rtfSEText);
1619                 rtfTextLen = strlen (rtfTextBuf);
1620                 RTFRouteToken ();
1621         }
1622         s->rtfExpanding = 0;    /* done - clear expansion flag */
1623 }
1624
1625
1626 /* ---------------------------------------------------------------------- */
1627
1628 /*
1629  * Control symbol lookup routines
1630  */
1631
1632
1633 typedef struct RTFKey   RTFKey;
1634
1635 struct RTFKey
1636 {
1637         int     rtfKMajor;      /* major number */
1638         int     rtfKMinor;      /* minor number */
1639         char    *rtfKStr;       /* symbol name */
1640         int     rtfKHash;       /* symbol name hash value */
1641 };
1642
1643 /*
1644  * A minor number of -1 means the token has no minor number
1645  * (all valid minor numbers are >= 0).
1646  */
1647
1648 static RTFKey   rtfKey[] =
1649 {
1650         /*
1651          * Special characters
1652          */
1653
1654         { rtfSpecialChar,       rtfIIntVersion,         "vern",         0 },
1655         { rtfSpecialChar,       rtfICreateTime,         "creatim",      0 },
1656         { rtfSpecialChar,       rtfIRevisionTime,       "revtim",       0 },
1657         { rtfSpecialChar,       rtfIPrintTime,          "printim",      0 },
1658         { rtfSpecialChar,       rtfIBackupTime,         "buptim",       0 },
1659         { rtfSpecialChar,       rtfIEditTime,           "edmins",       0 },
1660         { rtfSpecialChar,       rtfIYear,               "yr",           0 },
1661         { rtfSpecialChar,       rtfIMonth,              "mo",           0 },
1662         { rtfSpecialChar,       rtfIDay,                "dy",           0 },
1663         { rtfSpecialChar,       rtfIHour,               "hr",           0 },
1664         { rtfSpecialChar,       rtfIMinute,             "min",          0 },
1665         { rtfSpecialChar,       rtfISecond,             "sec",          0 },
1666         { rtfSpecialChar,       rtfINPages,             "nofpages",     0 },
1667         { rtfSpecialChar,       rtfINWords,             "nofwords",     0 },
1668         { rtfSpecialChar,       rtfINChars,             "nofchars",     0 },
1669         { rtfSpecialChar,       rtfIIntID,              "id",           0 },
1670
1671         { rtfSpecialChar,       rtfCurHeadDate,         "chdate",       0 },
1672         { rtfSpecialChar,       rtfCurHeadDateLong,     "chdpl",        0 },
1673         { rtfSpecialChar,       rtfCurHeadDateAbbrev,   "chdpa",        0 },
1674         { rtfSpecialChar,       rtfCurHeadTime,         "chtime",       0 },
1675         { rtfSpecialChar,       rtfCurHeadPage,         "chpgn",        0 },
1676         { rtfSpecialChar,       rtfSectNum,             "sectnum",      0 },
1677         { rtfSpecialChar,       rtfCurFNote,            "chftn",        0 },
1678         { rtfSpecialChar,       rtfCurAnnotRef,         "chatn",        0 },
1679         { rtfSpecialChar,       rtfFNoteSep,            "chftnsep",     0 },
1680         { rtfSpecialChar,       rtfFNoteCont,           "chftnsepc",    0 },
1681         { rtfSpecialChar,       rtfCell,                "cell",         0 },
1682         { rtfSpecialChar,       rtfRow,                 "row",          0 },
1683         { rtfSpecialChar,       rtfPar,                 "par",          0 },
1684         /* newline and carriage return are synonyms for */
1685         /* \par when they are preceded by a \ character */
1686         { rtfSpecialChar,       rtfPar,                 "\n",           0 },
1687         { rtfSpecialChar,       rtfPar,                 "\r",           0 },
1688         { rtfSpecialChar,       rtfSect,                "sect",         0 },
1689         { rtfSpecialChar,       rtfPage,                "page",         0 },
1690         { rtfSpecialChar,       rtfColumn,              "column",       0 },
1691         { rtfSpecialChar,       rtfLine,                "line",         0 },
1692         { rtfSpecialChar,       rtfSoftPage,            "softpage",     0 },
1693         { rtfSpecialChar,       rtfSoftColumn,          "softcol",      0 },
1694         { rtfSpecialChar,       rtfSoftLine,            "softline",     0 },
1695         { rtfSpecialChar,       rtfSoftLineHt,          "softlheight",  0 },
1696         { rtfSpecialChar,       rtfTab,                 "tab",          0 },
1697         { rtfSpecialChar,       rtfEmDash,              "emdash",       0 },
1698         { rtfSpecialChar,       rtfEnDash,              "endash",       0 },
1699         { rtfSpecialChar,       rtfEmSpace,             "emspace",      0 },
1700         { rtfSpecialChar,       rtfEnSpace,             "enspace",      0 },
1701         { rtfSpecialChar,       rtfBullet,              "bullet",       0 },
1702         { rtfSpecialChar,       rtfLQuote,              "lquote",       0 },
1703         { rtfSpecialChar,       rtfRQuote,              "rquote",       0 },
1704         { rtfSpecialChar,       rtfLDblQuote,           "ldblquote",    0 },
1705         { rtfSpecialChar,       rtfRDblQuote,           "rdblquote",    0 },
1706         { rtfSpecialChar,       rtfFormula,             "|",            0 },
1707         { rtfSpecialChar,       rtfNoBrkSpace,          "~",            0 },
1708         { rtfSpecialChar,       rtfNoReqHyphen,         "-",            0 },
1709         { rtfSpecialChar,       rtfNoBrkHyphen,         "_",            0 },
1710         { rtfSpecialChar,       rtfOptDest,             "*",            0 },
1711         { rtfSpecialChar,       rtfLTRMark,             "ltrmark",      0 },
1712         { rtfSpecialChar,       rtfRTLMark,             "rtlmark",      0 },
1713         { rtfSpecialChar,       rtfNoWidthJoiner,       "zwj",          0 },
1714         { rtfSpecialChar,       rtfNoWidthNonJoiner,    "zwnj",         0 },
1715         /* is this valid? */
1716         { rtfSpecialChar,       rtfCurHeadPict,         "chpict",       0 },
1717
1718         /*
1719          * Character formatting attributes
1720          */
1721
1722         { rtfCharAttr,  rtfPlain,               "plain",        0 },
1723         { rtfCharAttr,  rtfBold,                "b",            0 },
1724         { rtfCharAttr,  rtfAllCaps,             "caps",         0 },
1725         { rtfCharAttr,  rtfDeleted,             "deleted",      0 },
1726         { rtfCharAttr,  rtfSubScript,           "dn",           0 },
1727         { rtfCharAttr,  rtfSubScrShrink,        "sub",          0 },
1728         { rtfCharAttr,  rtfNoSuperSub,          "nosupersub",   0 },
1729         { rtfCharAttr,  rtfExpand,              "expnd",        0 },
1730         { rtfCharAttr,  rtfExpandTwips,         "expndtw",      0 },
1731         { rtfCharAttr,  rtfKerning,             "kerning",      0 },
1732         { rtfCharAttr,  rtfFontNum,             "f",            0 },
1733         { rtfCharAttr,  rtfFontSize,            "fs",           0 },
1734         { rtfCharAttr,  rtfItalic,              "i",            0 },
1735         { rtfCharAttr,  rtfOutline,             "outl",         0 },
1736         { rtfCharAttr,  rtfRevised,             "revised",      0 },
1737         { rtfCharAttr,  rtfRevAuthor,           "revauth",      0 },
1738         { rtfCharAttr,  rtfRevDTTM,             "revdttm",      0 },
1739         { rtfCharAttr,  rtfSmallCaps,           "scaps",        0 },
1740         { rtfCharAttr,  rtfShadow,              "shad",         0 },
1741         { rtfCharAttr,  rtfStrikeThru,          "strike",       0 },
1742         { rtfCharAttr,  rtfUnderline,           "ul",           0 },
1743         { rtfCharAttr,  rtfDotUnderline,        "uld",          0 },
1744         { rtfCharAttr,  rtfDbUnderline,         "uldb",         0 },
1745         { rtfCharAttr,  rtfNoUnderline,         "ulnone",       0 },
1746         { rtfCharAttr,  rtfWordUnderline,       "ulw",          0 },
1747         { rtfCharAttr,  rtfSuperScript,         "up",           0 },
1748         { rtfCharAttr,  rtfSuperScrShrink,      "super",        0 },
1749         { rtfCharAttr,  rtfInvisible,           "v",            0 },
1750         { rtfCharAttr,  rtfForeColor,           "cf",           0 },
1751         { rtfCharAttr,  rtfBackColor,           "cb",           0 },
1752         { rtfCharAttr,  rtfRTLChar,             "rtlch",        0 },
1753         { rtfCharAttr,  rtfLTRChar,             "ltrch",        0 },
1754         { rtfCharAttr,  rtfCharStyleNum,        "cs",           0 },
1755         { rtfCharAttr,  rtfCharCharSet,         "cchs",         0 },
1756         { rtfCharAttr,  rtfLanguage,            "lang",         0 },
1757         /* this has disappeared from spec 1.2 */
1758         { rtfCharAttr,  rtfGray,                "gray",         0 },
1759
1760         /*
1761          * Paragraph formatting attributes
1762          */
1763
1764         { rtfParAttr,   rtfParDef,              "pard",         0 },
1765         { rtfParAttr,   rtfStyleNum,            "s",            0 },
1766         { rtfParAttr,   rtfHyphenate,           "hyphpar",      0 },
1767         { rtfParAttr,   rtfInTable,             "intbl",        0 },
1768         { rtfParAttr,   rtfKeep,                "keep",         0 },
1769         { rtfParAttr,   rtfNoWidowControl,      "nowidctlpar",  0 },
1770         { rtfParAttr,   rtfKeepNext,            "keepn",        0 },
1771         { rtfParAttr,   rtfOutlineLevel,        "level",        0 },
1772         { rtfParAttr,   rtfNoLineNum,           "noline",       0 },
1773         { rtfParAttr,   rtfPBBefore,            "pagebb",       0 },
1774         { rtfParAttr,   rtfSideBySide,          "sbys",         0 },
1775         { rtfParAttr,   rtfQuadLeft,            "ql",           0 },
1776         { rtfParAttr,   rtfQuadRight,           "qr",           0 },
1777         { rtfParAttr,   rtfQuadJust,            "qj",           0 },
1778         { rtfParAttr,   rtfQuadCenter,          "qc",           0 },
1779         { rtfParAttr,   rtfFirstIndent,         "fi",           0 },
1780         { rtfParAttr,   rtfLeftIndent,          "li",           0 },
1781         { rtfParAttr,   rtfRightIndent,         "ri",           0 },
1782         { rtfParAttr,   rtfSpaceBefore,         "sb",           0 },
1783         { rtfParAttr,   rtfSpaceAfter,          "sa",           0 },
1784         { rtfParAttr,   rtfSpaceBetween,        "sl",           0 },
1785         { rtfParAttr,   rtfSpaceMultiply,       "slmult",       0 },
1786
1787         { rtfParAttr,   rtfSubDocument,         "subdocument",  0 },
1788
1789         { rtfParAttr,   rtfRTLPar,              "rtlpar",       0 },
1790         { rtfParAttr,   rtfLTRPar,              "ltrpar",       0 },
1791
1792         { rtfParAttr,   rtfTabPos,              "tx",           0 },
1793         /*
1794          * FrameMaker writes \tql (to mean left-justified tab, apparently)
1795          * although it's not in the spec.  It's also redundant, since lj
1796          * tabs are the default.
1797          */
1798         { rtfParAttr,   rtfTabLeft,             "tql",          0 },
1799         { rtfParAttr,   rtfTabRight,            "tqr",          0 },
1800         { rtfParAttr,   rtfTabCenter,           "tqc",          0 },
1801         { rtfParAttr,   rtfTabDecimal,          "tqdec",        0 },
1802         { rtfParAttr,   rtfTabBar,              "tb",           0 },
1803         { rtfParAttr,   rtfLeaderDot,           "tldot",        0 },
1804         { rtfParAttr,   rtfLeaderHyphen,        "tlhyph",       0 },
1805         { rtfParAttr,   rtfLeaderUnder,         "tlul",         0 },
1806         { rtfParAttr,   rtfLeaderThick,         "tlth",         0 },
1807         { rtfParAttr,   rtfLeaderEqual,         "tleq",         0 },
1808
1809         { rtfParAttr,   rtfParLevel,            "pnlvl",        0 },
1810         { rtfParAttr,   rtfParBullet,           "pnlvlblt",     0 },
1811         { rtfParAttr,   rtfParSimple,           "pnlvlbody",    0 },
1812         { rtfParAttr,   rtfParNumCont,          "pnlvlcont",    0 },
1813         { rtfParAttr,   rtfParNumOnce,          "pnnumonce",    0 },
1814         { rtfParAttr,   rtfParNumAcross,        "pnacross",     0 },
1815         { rtfParAttr,   rtfParHangIndent,       "pnhang",       0 },
1816         { rtfParAttr,   rtfParNumRestart,       "pnrestart",    0 },
1817         { rtfParAttr,   rtfParNumCardinal,      "pncard",       0 },
1818         { rtfParAttr,   rtfParNumDecimal,       "pndec",        0 },
1819         { rtfParAttr,   rtfParNumULetter,       "pnucltr",      0 },
1820         { rtfParAttr,   rtfParNumURoman,        "pnucrm",       0 },
1821         { rtfParAttr,   rtfParNumLLetter,       "pnlcltr",      0 },
1822         { rtfParAttr,   rtfParNumLRoman,        "pnlcrm",       0 },
1823         { rtfParAttr,   rtfParNumOrdinal,       "pnord",        0 },
1824         { rtfParAttr,   rtfParNumOrdinalText,   "pnordt",       0 },
1825         { rtfParAttr,   rtfParNumBold,          "pnb",          0 },
1826         { rtfParAttr,   rtfParNumItalic,        "pni",          0 },
1827         { rtfParAttr,   rtfParNumAllCaps,       "pncaps",       0 },
1828         { rtfParAttr,   rtfParNumSmallCaps,     "pnscaps",      0 },
1829         { rtfParAttr,   rtfParNumUnder,         "pnul",         0 },
1830         { rtfParAttr,   rtfParNumDotUnder,      "pnuld",        0 },
1831         { rtfParAttr,   rtfParNumDbUnder,       "pnuldb",       0 },
1832         { rtfParAttr,   rtfParNumNoUnder,       "pnulnone",     0 },
1833         { rtfParAttr,   rtfParNumWordUnder,     "pnulw",        0 },
1834         { rtfParAttr,   rtfParNumStrikethru,    "pnstrike",     0 },
1835         { rtfParAttr,   rtfParNumForeColor,     "pncf",         0 },
1836         { rtfParAttr,   rtfParNumFont,          "pnf",          0 },
1837         { rtfParAttr,   rtfParNumFontSize,      "pnfs",         0 },
1838         { rtfParAttr,   rtfParNumIndent,        "pnindent",     0 },
1839         { rtfParAttr,   rtfParNumSpacing,       "pnsp",         0 },
1840         { rtfParAttr,   rtfParNumInclPrev,      "pnprev",       0 },
1841         { rtfParAttr,   rtfParNumCenter,        "pnqc",         0 },
1842         { rtfParAttr,   rtfParNumLeft,          "pnql",         0 },
1843         { rtfParAttr,   rtfParNumRight,         "pnqr",         0 },
1844         { rtfParAttr,   rtfParNumStartAt,       "pnstart",      0 },
1845
1846         { rtfParAttr,   rtfBorderTop,           "brdrt",        0 },
1847         { rtfParAttr,   rtfBorderBottom,        "brdrb",        0 },
1848         { rtfParAttr,   rtfBorderLeft,          "brdrl",        0 },
1849         { rtfParAttr,   rtfBorderRight,         "brdrr",        0 },
1850         { rtfParAttr,   rtfBorderBetween,       "brdrbtw",      0 },
1851         { rtfParAttr,   rtfBorderBar,           "brdrbar",      0 },
1852         { rtfParAttr,   rtfBorderBox,           "box",          0 },
1853         { rtfParAttr,   rtfBorderSingle,        "brdrs",        0 },
1854         { rtfParAttr,   rtfBorderThick,         "brdrth",       0 },
1855         { rtfParAttr,   rtfBorderShadow,        "brdrsh",       0 },
1856         { rtfParAttr,   rtfBorderDouble,        "brdrdb",       0 },
1857         { rtfParAttr,   rtfBorderDot,           "brdrdot",      0 },
1858         { rtfParAttr,   rtfBorderDot,           "brdrdash",     0 },
1859         { rtfParAttr,   rtfBorderHair,          "brdrhair",     0 },
1860         { rtfParAttr,   rtfBorderWidth,         "brdrw",        0 },
1861         { rtfParAttr,   rtfBorderColor,         "brdrcf",       0 },
1862         { rtfParAttr,   rtfBorderSpace,         "brsp",         0 },
1863
1864         { rtfParAttr,   rtfShading,             "shading",      0 },
1865         { rtfParAttr,   rtfBgPatH,              "bghoriz",      0 },
1866         { rtfParAttr,   rtfBgPatV,              "bgvert",       0 },
1867         { rtfParAttr,   rtfFwdDiagBgPat,        "bgfdiag",      0 },
1868         { rtfParAttr,   rtfBwdDiagBgPat,        "bgbdiag",      0 },
1869         { rtfParAttr,   rtfHatchBgPat,          "bgcross",      0 },
1870         { rtfParAttr,   rtfDiagHatchBgPat,      "bgdcross",     0 },
1871         { rtfParAttr,   rtfDarkBgPatH,          "bgdkhoriz",    0 },
1872         { rtfParAttr,   rtfDarkBgPatV,          "bgdkvert",     0 },
1873         { rtfParAttr,   rtfFwdDarkBgPat,        "bgdkfdiag",    0 },
1874         { rtfParAttr,   rtfBwdDarkBgPat,        "bgdkbdiag",    0 },
1875         { rtfParAttr,   rtfDarkHatchBgPat,      "bgdkcross",    0 },
1876         { rtfParAttr,   rtfDarkDiagHatchBgPat,  "bgdkdcross",   0 },
1877         { rtfParAttr,   rtfBgPatLineColor,      "cfpat",        0 },
1878         { rtfParAttr,   rtfBgPatColor,          "cbpat",        0 },
1879
1880         /*
1881          * Section formatting attributes
1882          */
1883
1884         { rtfSectAttr,  rtfSectDef,             "sectd",        0 },
1885         { rtfSectAttr,  rtfENoteHere,           "endnhere",     0 },
1886         { rtfSectAttr,  rtfPrtBinFirst,         "binfsxn",      0 },
1887         { rtfSectAttr,  rtfPrtBin,              "binsxn",       0 },
1888         { rtfSectAttr,  rtfSectStyleNum,        "ds",           0 },
1889
1890         { rtfSectAttr,  rtfNoBreak,             "sbknone",      0 },
1891         { rtfSectAttr,  rtfColBreak,            "sbkcol",       0 },
1892         { rtfSectAttr,  rtfPageBreak,           "sbkpage",      0 },
1893         { rtfSectAttr,  rtfEvenBreak,           "sbkeven",      0 },
1894         { rtfSectAttr,  rtfOddBreak,            "sbkodd",       0 },
1895
1896         { rtfSectAttr,  rtfColumns,             "cols",         0 },
1897         { rtfSectAttr,  rtfColumnSpace,         "colsx",        0 },
1898         { rtfSectAttr,  rtfColumnNumber,        "colno",        0 },
1899         { rtfSectAttr,  rtfColumnSpRight,       "colsr",        0 },
1900         { rtfSectAttr,  rtfColumnWidth,         "colw",         0 },
1901         { rtfSectAttr,  rtfColumnLine,          "linebetcol",   0 },
1902
1903         { rtfSectAttr,  rtfLineModulus,         "linemod",      0 },
1904         { rtfSectAttr,  rtfLineDist,            "linex",        0 },
1905         { rtfSectAttr,  rtfLineStarts,          "linestarts",   0 },
1906         { rtfSectAttr,  rtfLineRestart,         "linerestart",  0 },
1907         { rtfSectAttr,  rtfLineRestartPg,       "lineppage",    0 },
1908         { rtfSectAttr,  rtfLineCont,            "linecont",     0 },
1909
1910         { rtfSectAttr,  rtfSectPageWid,         "pgwsxn",       0 },
1911         { rtfSectAttr,  rtfSectPageHt,          "pghsxn",       0 },
1912         { rtfSectAttr,  rtfSectMarginLeft,      "marglsxn",     0 },
1913         { rtfSectAttr,  rtfSectMarginRight,     "margrsxn",     0 },
1914         { rtfSectAttr,  rtfSectMarginTop,       "margtsxn",     0 },
1915         { rtfSectAttr,  rtfSectMarginBottom,    "margbsxn",     0 },
1916         { rtfSectAttr,  rtfSectMarginGutter,    "guttersxn",    0 },
1917         { rtfSectAttr,  rtfSectLandscape,       "lndscpsxn",    0 },
1918         { rtfSectAttr,  rtfTitleSpecial,        "titlepg",      0 },
1919         { rtfSectAttr,  rtfHeaderY,             "headery",      0 },
1920         { rtfSectAttr,  rtfFooterY,             "footery",      0 },
1921
1922         { rtfSectAttr,  rtfPageStarts,          "pgnstarts",    0 },
1923         { rtfSectAttr,  rtfPageCont,            "pgncont",      0 },
1924         { rtfSectAttr,  rtfPageRestart,         "pgnrestart",   0 },
1925         { rtfSectAttr,  rtfPageNumRight,        "pgnx",         0 },
1926         { rtfSectAttr,  rtfPageNumTop,          "pgny",         0 },
1927         { rtfSectAttr,  rtfPageDecimal,         "pgndec",       0 },
1928         { rtfSectAttr,  rtfPageURoman,          "pgnucrm",      0 },
1929         { rtfSectAttr,  rtfPageLRoman,          "pgnlcrm",      0 },
1930         { rtfSectAttr,  rtfPageULetter,         "pgnucltr",     0 },
1931         { rtfSectAttr,  rtfPageLLetter,         "pgnlcltr",     0 },
1932         { rtfSectAttr,  rtfPageNumHyphSep,      "pgnhnsh",      0 },
1933         { rtfSectAttr,  rtfPageNumSpaceSep,     "pgnhnsp",      0 },
1934         { rtfSectAttr,  rtfPageNumColonSep,     "pgnhnsc",      0 },
1935         { rtfSectAttr,  rtfPageNumEmdashSep,    "pgnhnsm",      0 },
1936         { rtfSectAttr,  rtfPageNumEndashSep,    "pgnhnsn",      0 },
1937
1938         { rtfSectAttr,  rtfTopVAlign,           "vertalt",      0 },
1939         /* misspelled as "vertal" in specification 1.0 */
1940         { rtfSectAttr,  rtfBottomVAlign,        "vertalb",      0 },
1941         { rtfSectAttr,  rtfCenterVAlign,        "vertalc",      0 },
1942         { rtfSectAttr,  rtfJustVAlign,          "vertalj",      0 },
1943
1944         { rtfSectAttr,  rtfRTLSect,             "rtlsect",      0 },
1945         { rtfSectAttr,  rtfLTRSect,             "ltrsect",      0 },
1946
1947         /* I've seen these in an old spec, but not in real files... */
1948         /*rtfSectAttr,  rtfNoBreak,             "nobreak",      0,*/
1949         /*rtfSectAttr,  rtfColBreak,            "colbreak",     0,*/
1950         /*rtfSectAttr,  rtfPageBreak,           "pagebreak",    0,*/
1951         /*rtfSectAttr,  rtfEvenBreak,           "evenbreak",    0,*/
1952         /*rtfSectAttr,  rtfOddBreak,            "oddbreak",     0,*/
1953
1954         /*
1955          * Document formatting attributes
1956          */
1957
1958         { rtfDocAttr,   rtfDefTab,              "deftab",       0 },
1959         { rtfDocAttr,   rtfHyphHotZone,         "hyphhotz",     0 },
1960         { rtfDocAttr,   rtfHyphConsecLines,     "hyphconsec",   0 },
1961         { rtfDocAttr,   rtfHyphCaps,            "hyphcaps",     0 },
1962         { rtfDocAttr,   rtfHyphAuto,            "hyphauto",     0 },
1963         { rtfDocAttr,   rtfLineStart,           "linestart",    0 },
1964         { rtfDocAttr,   rtfFracWidth,           "fracwidth",    0 },
1965         /* \makeback was given in old version of spec, it's now */
1966         /* listed as \makebackup */
1967         { rtfDocAttr,   rtfMakeBackup,          "makeback",     0 },
1968         { rtfDocAttr,   rtfMakeBackup,          "makebackup",   0 },
1969         { rtfDocAttr,   rtfRTFDefault,          "defformat",    0 },
1970         { rtfDocAttr,   rtfPSOverlay,           "psover",       0 },
1971         { rtfDocAttr,   rtfDocTemplate,         "doctemp",      0 },
1972         { rtfDocAttr,   rtfDefLanguage,         "deflang",      0 },
1973
1974         { rtfDocAttr,   rtfFENoteType,          "fet",          0 },
1975         { rtfDocAttr,   rtfFNoteEndSect,        "endnotes",     0 },
1976         { rtfDocAttr,   rtfFNoteEndDoc,         "enddoc",       0 },
1977         { rtfDocAttr,   rtfFNoteText,           "ftntj",        0 },
1978         { rtfDocAttr,   rtfFNoteBottom,         "ftnbj",        0 },
1979         { rtfDocAttr,   rtfENoteEndSect,        "aendnotes",    0 },
1980         { rtfDocAttr,   rtfENoteEndDoc,         "aenddoc",      0 },
1981         { rtfDocAttr,   rtfENoteText,           "aftntj",       0 },
1982         { rtfDocAttr,   rtfENoteBottom,         "aftnbj",       0 },
1983         { rtfDocAttr,   rtfFNoteStart,          "ftnstart",     0 },
1984         { rtfDocAttr,   rtfENoteStart,          "aftnstart",    0 },
1985         { rtfDocAttr,   rtfFNoteRestartPage,    "ftnrstpg",     0 },
1986         { rtfDocAttr,   rtfFNoteRestart,        "ftnrestart",   0 },
1987         { rtfDocAttr,   rtfFNoteRestartCont,    "ftnrstcont",   0 },
1988         { rtfDocAttr,   rtfENoteRestart,        "aftnrestart",  0 },
1989         { rtfDocAttr,   rtfENoteRestartCont,    "aftnrstcont",  0 },
1990         { rtfDocAttr,   rtfFNoteNumArabic,      "ftnnar",       0 },
1991         { rtfDocAttr,   rtfFNoteNumLLetter,     "ftnnalc",      0 },
1992         { rtfDocAttr,   rtfFNoteNumULetter,     "ftnnauc",      0 },
1993         { rtfDocAttr,   rtfFNoteNumLRoman,      "ftnnrlc",      0 },
1994         { rtfDocAttr,   rtfFNoteNumURoman,      "ftnnruc",      0 },
1995         { rtfDocAttr,   rtfFNoteNumChicago,     "ftnnchi",      0 },
1996         { rtfDocAttr,   rtfENoteNumArabic,      "aftnnar",      0 },
1997         { rtfDocAttr,   rtfENoteNumLLetter,     "aftnnalc",     0 },
1998         { rtfDocAttr,   rtfENoteNumULetter,     "aftnnauc",     0 },
1999         { rtfDocAttr,   rtfENoteNumLRoman,      "aftnnrlc",     0 },
2000         { rtfDocAttr,   rtfENoteNumURoman,      "aftnnruc",     0 },
2001         { rtfDocAttr,   rtfENoteNumChicago,     "aftnnchi",     0 },
2002
2003         { rtfDocAttr,   rtfPaperWidth,          "paperw",       0 },
2004         { rtfDocAttr,   rtfPaperHeight,         "paperh",       0 },
2005         { rtfDocAttr,   rtfPaperSize,           "psz",          0 },
2006         { rtfDocAttr,   rtfLeftMargin,          "margl",        0 },
2007         { rtfDocAttr,   rtfRightMargin,         "margr",        0 },
2008         { rtfDocAttr,   rtfTopMargin,           "margt",        0 },
2009         { rtfDocAttr,   rtfBottomMargin,        "margb",        0 },
2010         { rtfDocAttr,   rtfFacingPage,          "facingp",      0 },
2011         { rtfDocAttr,   rtfGutterWid,           "gutter",       0 },
2012         { rtfDocAttr,   rtfMirrorMargin,        "margmirror",   0 },
2013         { rtfDocAttr,   rtfLandscape,           "landscape",    0 },
2014         { rtfDocAttr,   rtfPageStart,           "pgnstart",     0 },
2015         { rtfDocAttr,   rtfWidowCtrl,           "widowctrl",    0 },
2016
2017         { rtfDocAttr,   rtfLinkStyles,          "linkstyles",   0 },
2018
2019         { rtfDocAttr,   rtfNoAutoTabIndent,     "notabind",     0 },
2020         { rtfDocAttr,   rtfWrapSpaces,          "wraptrsp",     0 },
2021         { rtfDocAttr,   rtfPrintColorsBlack,    "prcolbl",      0 },
2022         { rtfDocAttr,   rtfNoExtraSpaceRL,      "noextrasprl",  0 },
2023         { rtfDocAttr,   rtfNoColumnBalance,     "nocolbal",     0 },
2024         { rtfDocAttr,   rtfCvtMailMergeQuote,   "cvmme",        0 },
2025         { rtfDocAttr,   rtfSuppressTopSpace,    "sprstsp",      0 },
2026         { rtfDocAttr,   rtfSuppressPreParSpace, "sprsspbf",     0 },
2027         { rtfDocAttr,   rtfCombineTblBorders,   "otblrul",      0 },
2028         { rtfDocAttr,   rtfTranspMetafiles,     "transmf",      0 },
2029         { rtfDocAttr,   rtfSwapBorders,         "swpbdr",       0 },
2030         { rtfDocAttr,   rtfShowHardBreaks,      "brkfrm",       0 },
2031
2032         { rtfDocAttr,   rtfFormProtected,       "formprot",     0 },
2033         { rtfDocAttr,   rtfAllProtected,        "allprot",      0 },
2034         { rtfDocAttr,   rtfFormShading,         "formshade",    0 },
2035         { rtfDocAttr,   rtfFormDisplay,         "formdisp",     0 },
2036         { rtfDocAttr,   rtfPrintData,           "printdata",    0 },
2037
2038         { rtfDocAttr,   rtfRevProtected,        "revprot",      0 },
2039         { rtfDocAttr,   rtfRevisions,           "revisions",    0 },
2040         { rtfDocAttr,   rtfRevDisplay,          "revprop",      0 },
2041         { rtfDocAttr,   rtfRevBar,              "revbar",       0 },
2042
2043         { rtfDocAttr,   rtfAnnotProtected,      "annotprot",    0 },
2044
2045         { rtfDocAttr,   rtfRTLDoc,              "rtldoc",       0 },
2046         { rtfDocAttr,   rtfLTRDoc,              "ltrdoc",       0 },
2047
2048         /*
2049          * Style attributes
2050          */
2051
2052         { rtfStyleAttr, rtfAdditive,            "additive",     0 },
2053         { rtfStyleAttr, rtfBasedOn,             "sbasedon",     0 },
2054         { rtfStyleAttr, rtfNext,                "snext",        0 },
2055
2056         /*
2057          * Picture attributes
2058          */
2059
2060         { rtfPictAttr,  rtfMacQD,               "macpict",      0 },
2061         { rtfPictAttr,  rtfPMMetafile,          "pmmetafile",   0 },
2062         { rtfPictAttr,  rtfWinMetafile,         "wmetafile",    0 },
2063         { rtfPictAttr,  rtfDevIndBitmap,        "dibitmap",     0 },
2064         { rtfPictAttr,  rtfWinBitmap,           "wbitmap",      0 },
2065         { rtfPictAttr,  rtfPixelBits,           "wbmbitspixel", 0 },
2066         { rtfPictAttr,  rtfBitmapPlanes,        "wbmplanes",    0 },
2067         { rtfPictAttr,  rtfBitmapWid,           "wbmwidthbytes", 0 },
2068
2069         { rtfPictAttr,  rtfPicWid,              "picw",         0 },
2070         { rtfPictAttr,  rtfPicHt,               "pich",         0 },
2071         { rtfPictAttr,  rtfPicGoalWid,          "picwgoal",     0 },
2072         { rtfPictAttr,  rtfPicGoalHt,           "pichgoal",     0 },
2073         /* these two aren't in the spec, but some writers emit them */
2074         { rtfPictAttr,  rtfPicGoalWid,          "picwGoal",     0 },
2075         { rtfPictAttr,  rtfPicGoalHt,           "pichGoal",     0 },
2076         { rtfPictAttr,  rtfPicScaleX,           "picscalex",    0 },
2077         { rtfPictAttr,  rtfPicScaleY,           "picscaley",    0 },
2078         { rtfPictAttr,  rtfPicScaled,           "picscaled",    0 },
2079         { rtfPictAttr,  rtfPicCropTop,          "piccropt",     0 },
2080         { rtfPictAttr,  rtfPicCropBottom,       "piccropb",     0 },
2081         { rtfPictAttr,  rtfPicCropLeft,         "piccropl",     0 },
2082         { rtfPictAttr,  rtfPicCropRight,        "piccropr",     0 },
2083
2084         { rtfPictAttr,  rtfPicMFHasBitmap,      "picbmp",       0 },
2085         { rtfPictAttr,  rtfPicMFBitsPerPixel,   "picbpp",       0 },
2086
2087         { rtfPictAttr,  rtfPicBinary,           "bin",          0 },
2088
2089         /*
2090          * NeXT graphic attributes
2091          */
2092
2093         { rtfNeXTGrAttr,        rtfNeXTGWidth,          "width",        0 },
2094         { rtfNeXTGrAttr,        rtfNeXTGHeight,         "height",       0 },
2095
2096         /*
2097          * Destinations
2098          */
2099
2100         { rtfDestination,       rtfFontTbl,             "fonttbl",      0 },
2101         { rtfDestination,       rtfFontAltName,         "falt",         0 },
2102         { rtfDestination,       rtfEmbeddedFont,        "fonteb",       0 },
2103         { rtfDestination,       rtfFontFile,            "fontfile",     0 },
2104         { rtfDestination,       rtfFileTbl,             "filetbl",      0 },
2105         { rtfDestination,       rtfFileInfo,            "file",         0 },
2106         { rtfDestination,       rtfColorTbl,            "colortbl",     0 },
2107         { rtfDestination,       rtfStyleSheet,          "stylesheet",   0 },
2108         { rtfDestination,       rtfKeyCode,             "keycode",      0 },
2109         { rtfDestination,       rtfRevisionTbl,         "revtbl",       0 },
2110         { rtfDestination,       rtfInfo,                "info",         0 },
2111         { rtfDestination,       rtfITitle,              "title",        0 },
2112         { rtfDestination,       rtfISubject,            "subject",      0 },
2113         { rtfDestination,       rtfIAuthor,             "author",       0 },
2114         { rtfDestination,       rtfIOperator,           "operator",     0 },
2115         { rtfDestination,       rtfIKeywords,           "keywords",     0 },
2116         { rtfDestination,       rtfIComment,            "comment",      0 },
2117         { rtfDestination,       rtfIVersion,            "version",      0 },
2118         { rtfDestination,       rtfIDoccomm,            "doccomm",      0 },
2119         /* \verscomm may not exist -- was seen in earlier spec version */
2120         { rtfDestination,       rtfIVerscomm,           "verscomm",     0 },
2121         { rtfDestination,       rtfNextFile,            "nextfile",     0 },
2122         { rtfDestination,       rtfTemplate,            "template",     0 },
2123         { rtfDestination,       rtfFNSep,               "ftnsep",       0 },
2124         { rtfDestination,       rtfFNContSep,           "ftnsepc",      0 },
2125         { rtfDestination,       rtfFNContNotice,        "ftncn",        0 },
2126         { rtfDestination,       rtfENSep,               "aftnsep",      0 },
2127         { rtfDestination,       rtfENContSep,           "aftnsepc",     0 },
2128         { rtfDestination,       rtfENContNotice,        "aftncn",       0 },
2129         { rtfDestination,       rtfPageNumLevel,        "pgnhn",        0 },
2130         { rtfDestination,       rtfParNumLevelStyle,    "pnseclvl",     0 },
2131         { rtfDestination,       rtfHeader,              "header",       0 },
2132         { rtfDestination,       rtfFooter,              "footer",       0 },
2133         { rtfDestination,       rtfHeaderLeft,          "headerl",      0 },
2134         { rtfDestination,       rtfHeaderRight,         "headerr",      0 },
2135         { rtfDestination,       rtfHeaderFirst,         "headerf",      0 },
2136         { rtfDestination,       rtfFooterLeft,          "footerl",      0 },
2137         { rtfDestination,       rtfFooterRight,         "footerr",      0 },
2138         { rtfDestination,       rtfFooterFirst,         "footerf",      0 },
2139         { rtfDestination,       rtfParNumText,          "pntext",       0 },
2140         { rtfDestination,       rtfParNumbering,        "pn",           0 },
2141         { rtfDestination,       rtfParNumTextAfter,     "pntexta",      0 },
2142         { rtfDestination,       rtfParNumTextBefore,    "pntextb",      0 },
2143         { rtfDestination,       rtfBookmarkStart,       "bkmkstart",    0 },
2144         { rtfDestination,       rtfBookmarkEnd,         "bkmkend",      0 },
2145         { rtfDestination,       rtfPict,                "pict",         0 },
2146         { rtfDestination,       rtfObject,              "object",       0 },
2147         { rtfDestination,       rtfObjClass,            "objclass",     0 },
2148         { rtfDestination,       rtfObjName,             "objname",      0 },
2149         { rtfObjAttr,   rtfObjTime,             "objtime",      0 },
2150         { rtfDestination,       rtfObjData,             "objdata",      0 },
2151         { rtfDestination,       rtfObjAlias,            "objalias",     0 },
2152         { rtfDestination,       rtfObjSection,          "objsect",      0 },
2153         /* objitem and objtopic aren't documented in the spec! */
2154         { rtfDestination,       rtfObjItem,             "objitem",      0 },
2155         { rtfDestination,       rtfObjTopic,            "objtopic",     0 },
2156         { rtfDestination,       rtfObjResult,           "result",       0 },
2157         { rtfDestination,       rtfDrawObject,          "do",           0 },
2158         { rtfDestination,       rtfFootnote,            "footnote",     0 },
2159         { rtfDestination,       rtfAnnotRefStart,       "atrfstart",    0 },
2160         { rtfDestination,       rtfAnnotRefEnd,         "atrfend",      0 },
2161         { rtfDestination,       rtfAnnotID,             "atnid",        0 },
2162         { rtfDestination,       rtfAnnotAuthor,         "atnauthor",    0 },
2163         { rtfDestination,       rtfAnnotation,          "annotation",   0 },
2164         { rtfDestination,       rtfAnnotRef,            "atnref",       0 },
2165         { rtfDestination,       rtfAnnotTime,           "atntime",      0 },
2166         { rtfDestination,       rtfAnnotIcon,           "atnicn",       0 },
2167         { rtfDestination,       rtfField,               "field",        0 },
2168         { rtfDestination,       rtfFieldInst,           "fldinst",      0 },
2169         { rtfDestination,       rtfFieldResult,         "fldrslt",      0 },
2170         { rtfDestination,       rtfDataField,           "datafield",    0 },
2171         { rtfDestination,       rtfIndex,               "xe",           0 },
2172         { rtfDestination,       rtfIndexText,           "txe",          0 },
2173         { rtfDestination,       rtfIndexRange,          "rxe",          0 },
2174         { rtfDestination,       rtfTOC,                 "tc",           0 },
2175         { rtfDestination,       rtfNeXTGraphic,         "NeXTGraphic",  0 },
2176
2177         /*
2178          * Font families
2179          */
2180
2181         { rtfFontFamily,        rtfFFNil,               "fnil",         0 },
2182         { rtfFontFamily,        rtfFFRoman,             "froman",       0 },
2183         { rtfFontFamily,        rtfFFSwiss,             "fswiss",       0 },
2184         { rtfFontFamily,        rtfFFModern,            "fmodern",      0 },
2185         { rtfFontFamily,        rtfFFScript,            "fscript",      0 },
2186         { rtfFontFamily,        rtfFFDecor,             "fdecor",       0 },
2187         { rtfFontFamily,        rtfFFTech,              "ftech",        0 },
2188         { rtfFontFamily,        rtfFFBidirectional,     "fbidi",        0 },
2189
2190         /*
2191          * Font attributes
2192          */
2193
2194         { rtfFontAttr,  rtfFontCharSet,         "fcharset",     0 },
2195         { rtfFontAttr,  rtfFontPitch,           "fprq",         0 },
2196         { rtfFontAttr,  rtfFontCodePage,        "cpg",          0 },
2197         { rtfFontAttr,  rtfFTypeNil,            "ftnil",        0 },
2198         { rtfFontAttr,  rtfFTypeTrueType,       "fttruetype",   0 },
2199
2200         /*
2201          * File table attributes
2202          */
2203
2204         { rtfFileAttr,  rtfFileNum,             "fid",          0 },
2205         { rtfFileAttr,  rtfFileRelPath,         "frelative",    0 },
2206         { rtfFileAttr,  rtfFileOSNum,           "fosnum",       0 },
2207
2208         /*
2209          * File sources
2210          */
2211
2212         { rtfFileSource,        rtfSrcMacintosh,        "fvalidmac",    0 },
2213         { rtfFileSource,        rtfSrcDOS,              "fvaliddos",    0 },
2214         { rtfFileSource,        rtfSrcNTFS,             "fvalidntfs",   0 },
2215         { rtfFileSource,        rtfSrcHPFS,             "fvalidhpfs",   0 },
2216         { rtfFileSource,        rtfSrcNetwork,          "fnetwork",     0 },
2217
2218         /*
2219          * Color names
2220          */
2221
2222         { rtfColorName, rtfRed,                 "red",          0 },
2223         { rtfColorName, rtfGreen,               "green",        0 },
2224         { rtfColorName, rtfBlue,                "blue",         0 },
2225
2226         /*
2227          * Charset names
2228          */
2229
2230         { rtfCharSet,   rtfMacCharSet,          "mac",          0 },
2231         { rtfCharSet,   rtfAnsiCharSet,         "ansi",         0 },
2232         { rtfCharSet,   rtfPcCharSet,           "pc",           0 },
2233         { rtfCharSet,   rtfPcaCharSet,          "pca",          0 },
2234
2235         /*
2236          * Table attributes
2237          */
2238
2239         { rtfTblAttr,   rtfRowDef,              "trowd",        0 },
2240         { rtfTblAttr,   rtfRowGapH,             "trgaph",       0 },
2241         { rtfTblAttr,   rtfCellPos,             "cellx",        0 },
2242         { rtfTblAttr,   rtfMergeRngFirst,       "clmgf",        0 },
2243         { rtfTblAttr,   rtfMergePrevious,       "clmrg",        0 },
2244
2245         { rtfTblAttr,   rtfRowLeft,             "trql",         0 },
2246         { rtfTblAttr,   rtfRowRight,            "trqr",         0 },
2247         { rtfTblAttr,   rtfRowCenter,           "trqc",         0 },
2248         { rtfTblAttr,   rtfRowLeftEdge,         "trleft",       0 },
2249         { rtfTblAttr,   rtfRowHt,               "trrh",         0 },
2250         { rtfTblAttr,   rtfRowHeader,           "trhdr",        0 },
2251         { rtfTblAttr,   rtfRowKeep,             "trkeep",       0 },
2252
2253         { rtfTblAttr,   rtfRTLRow,              "rtlrow",       0 },
2254         { rtfTblAttr,   rtfLTRRow,              "ltrrow",       0 },
2255
2256         { rtfTblAttr,   rtfRowBordTop,          "trbrdrt",      0 },
2257         { rtfTblAttr,   rtfRowBordLeft,         "trbrdrl",      0 },
2258         { rtfTblAttr,   rtfRowBordBottom,       "trbrdrb",      0 },
2259         { rtfTblAttr,   rtfRowBordRight,        "trbrdrr",      0 },
2260         { rtfTblAttr,   rtfRowBordHoriz,        "trbrdrh",      0 },
2261         { rtfTblAttr,   rtfRowBordVert,         "trbrdrv",      0 },
2262
2263         { rtfTblAttr,   rtfCellBordBottom,      "clbrdrb",      0 },
2264         { rtfTblAttr,   rtfCellBordTop,         "clbrdrt",      0 },
2265         { rtfTblAttr,   rtfCellBordLeft,        "clbrdrl",      0 },
2266         { rtfTblAttr,   rtfCellBordRight,       "clbrdrr",      0 },
2267
2268         { rtfTblAttr,   rtfCellShading,         "clshdng",      0 },
2269         { rtfTblAttr,   rtfCellBgPatH,          "clbghoriz",    0 },
2270         { rtfTblAttr,   rtfCellBgPatV,          "clbgvert",     0 },
2271         { rtfTblAttr,   rtfCellFwdDiagBgPat,    "clbgfdiag",    0 },
2272         { rtfTblAttr,   rtfCellBwdDiagBgPat,    "clbgbdiag",    0 },
2273         { rtfTblAttr,   rtfCellHatchBgPat,      "clbgcross",    0 },
2274         { rtfTblAttr,   rtfCellDiagHatchBgPat,  "clbgdcross",   0 },
2275         /*
2276          * The spec lists "clbgdkhor", but the corresponding non-cell
2277          * control is "bgdkhoriz".  At any rate Macintosh Word seems
2278          * to accept both "clbgdkhor" and "clbgdkhoriz".
2279          */
2280         { rtfTblAttr,   rtfCellDarkBgPatH,      "clbgdkhoriz",  0 },
2281         { rtfTblAttr,   rtfCellDarkBgPatH,      "clbgdkhor",    0 },
2282         { rtfTblAttr,   rtfCellDarkBgPatV,      "clbgdkvert",   0 },
2283         { rtfTblAttr,   rtfCellFwdDarkBgPat,    "clbgdkfdiag",  0 },
2284         { rtfTblAttr,   rtfCellBwdDarkBgPat,    "clbgdkbdiag",  0 },
2285         { rtfTblAttr,   rtfCellDarkHatchBgPat,  "clbgdkcross",  0 },
2286         { rtfTblAttr,   rtfCellDarkDiagHatchBgPat, "clbgdkdcross",      0 },
2287         { rtfTblAttr,   rtfCellBgPatLineColor, "clcfpat",       0 },
2288         { rtfTblAttr,   rtfCellBgPatColor,      "clcbpat",      0 },
2289
2290         /*
2291          * Field attributes
2292          */
2293
2294         { rtfFieldAttr, rtfFieldDirty,          "flddirty",     0 },
2295         { rtfFieldAttr, rtfFieldEdited,         "fldedit",      0 },
2296         { rtfFieldAttr, rtfFieldLocked,         "fldlock",      0 },
2297         { rtfFieldAttr, rtfFieldPrivate,        "fldpriv",      0 },
2298         { rtfFieldAttr, rtfFieldAlt,            "fldalt",       0 },
2299
2300         /*
2301          * Positioning attributes
2302          */
2303
2304         { rtfPosAttr,   rtfAbsWid,              "absw",         0 },
2305         { rtfPosAttr,   rtfAbsHt,               "absh",         0 },
2306
2307         { rtfPosAttr,   rtfRPosMargH,           "phmrg",        0 },
2308         { rtfPosAttr,   rtfRPosPageH,           "phpg",         0 },
2309         { rtfPosAttr,   rtfRPosColH,            "phcol",        0 },
2310         { rtfPosAttr,   rtfPosX,                "posx",         0 },
2311         { rtfPosAttr,   rtfPosNegX,             "posnegx",      0 },
2312         { rtfPosAttr,   rtfPosXCenter,          "posxc",        0 },
2313         { rtfPosAttr,   rtfPosXInside,          "posxi",        0 },
2314         { rtfPosAttr,   rtfPosXOutSide,         "posxo",        0 },
2315         { rtfPosAttr,   rtfPosXRight,           "posxr",        0 },
2316         { rtfPosAttr,   rtfPosXLeft,            "posxl",        0 },
2317
2318         { rtfPosAttr,   rtfRPosMargV,           "pvmrg",        0 },
2319         { rtfPosAttr,   rtfRPosPageV,           "pvpg",         0 },
2320         { rtfPosAttr,   rtfRPosParaV,           "pvpara",       0 },
2321         { rtfPosAttr,   rtfPosY,                "posy",         0 },
2322         { rtfPosAttr,   rtfPosNegY,             "posnegy",      0 },
2323         { rtfPosAttr,   rtfPosYInline,          "posyil",       0 },
2324         { rtfPosAttr,   rtfPosYTop,             "posyt",        0 },
2325         { rtfPosAttr,   rtfPosYCenter,          "posyc",        0 },
2326         { rtfPosAttr,   rtfPosYBottom,          "posyb",        0 },
2327
2328         { rtfPosAttr,   rtfNoWrap,              "nowrap",       0 },
2329         { rtfPosAttr,   rtfDistFromTextAll,     "dxfrtext",     0 },
2330         { rtfPosAttr,   rtfDistFromTextX,       "dfrmtxtx",     0 },
2331         { rtfPosAttr,   rtfDistFromTextY,       "dfrmtxty",     0 },
2332         /* \dyfrtext no longer exists in spec 1.2, apparently */
2333         /* replaced by \dfrmtextx and \dfrmtexty. */
2334         { rtfPosAttr,   rtfTextDistY,           "dyfrtext",     0 },
2335
2336         { rtfPosAttr,   rtfDropCapLines,        "dropcapli",    0 },
2337         { rtfPosAttr,   rtfDropCapType,         "dropcapt",     0 },
2338
2339         /*
2340          * Object controls
2341          */
2342
2343         { rtfObjAttr,   rtfObjEmb,              "objemb",       0 },
2344         { rtfObjAttr,   rtfObjLink,             "objlink",      0 },
2345         { rtfObjAttr,   rtfObjAutoLink,         "objautlink",   0 },
2346         { rtfObjAttr,   rtfObjSubscriber,       "objsub",       0 },
2347         { rtfObjAttr,   rtfObjPublisher,        "objpub",       0 },
2348         { rtfObjAttr,   rtfObjICEmb,            "objicemb",     0 },
2349
2350         { rtfObjAttr,   rtfObjLinkSelf,         "linkself",     0 },
2351         { rtfObjAttr,   rtfObjLock,             "objupdate",    0 },
2352         { rtfObjAttr,   rtfObjUpdate,           "objlock",      0 },
2353
2354         { rtfObjAttr,   rtfObjHt,               "objh",         0 },
2355         { rtfObjAttr,   rtfObjWid,              "objw",         0 },
2356         { rtfObjAttr,   rtfObjSetSize,          "objsetsize",   0 },
2357         { rtfObjAttr,   rtfObjAlign,            "objalign",     0 },
2358         { rtfObjAttr,   rtfObjTransposeY,       "objtransy",    0 },
2359         { rtfObjAttr,   rtfObjCropTop,          "objcropt",     0 },
2360         { rtfObjAttr,   rtfObjCropBottom,       "objcropb",     0 },
2361         { rtfObjAttr,   rtfObjCropLeft,         "objcropl",     0 },
2362         { rtfObjAttr,   rtfObjCropRight,        "objcropr",     0 },
2363         { rtfObjAttr,   rtfObjScaleX,           "objscalex",    0 },
2364         { rtfObjAttr,   rtfObjScaleY,           "objscaley",    0 },
2365
2366         { rtfObjAttr,   rtfObjResRTF,           "rsltrtf",      0 },
2367         { rtfObjAttr,   rtfObjResPict,          "rsltpict",     0 },
2368         { rtfObjAttr,   rtfObjResBitmap,        "rsltbmp",      0 },
2369         { rtfObjAttr,   rtfObjResText,          "rslttxt",      0 },
2370         { rtfObjAttr,   rtfObjResMerge,         "rsltmerge",    0 },
2371
2372         { rtfObjAttr,   rtfObjBookmarkPubObj,   "bkmkpub",      0 },
2373         { rtfObjAttr,   rtfObjPubAutoUpdate,    "pubauto",      0 },
2374
2375         /*
2376          * Associated character formatting attributes
2377          */
2378
2379         { rtfACharAttr, rtfACBold,              "ab",           0 },
2380         { rtfACharAttr, rtfACAllCaps,           "caps",         0 },
2381         { rtfACharAttr, rtfACForeColor,         "acf",          0 },
2382         { rtfACharAttr, rtfACSubScript,         "adn",          0 },
2383         { rtfACharAttr, rtfACExpand,            "aexpnd",       0 },
2384         { rtfACharAttr, rtfACFontNum,           "af",           0 },
2385         { rtfACharAttr, rtfACFontSize,          "afs",          0 },
2386         { rtfACharAttr, rtfACItalic,            "ai",           0 },
2387         { rtfACharAttr, rtfACLanguage,          "alang",        0 },
2388         { rtfACharAttr, rtfACOutline,           "aoutl",        0 },
2389         { rtfACharAttr, rtfACSmallCaps,         "ascaps",       0 },
2390         { rtfACharAttr, rtfACShadow,            "ashad",        0 },
2391         { rtfACharAttr, rtfACStrikeThru,        "astrike",      0 },
2392         { rtfACharAttr, rtfACUnderline,         "aul",          0 },
2393         { rtfACharAttr, rtfACDotUnderline,      "auld",         0 },
2394         { rtfACharAttr, rtfACDbUnderline,       "auldb",        0 },
2395         { rtfACharAttr, rtfACNoUnderline,       "aulnone",      0 },
2396         { rtfACharAttr, rtfACWordUnderline,     "aulw",         0 },
2397         { rtfACharAttr, rtfACSuperScript,       "aup",          0 },
2398
2399         /*
2400          * Footnote attributes
2401          */
2402
2403         { rtfFNoteAttr, rtfFNAlt,               "ftnalt",       0 },
2404
2405         /*
2406          * Key code attributes
2407          */
2408
2409         { rtfKeyCodeAttr,       rtfAltKey,              "alt",          0 },
2410         { rtfKeyCodeAttr,       rtfShiftKey,            "shift",        0 },
2411         { rtfKeyCodeAttr,       rtfControlKey,          "ctrl",         0 },
2412         { rtfKeyCodeAttr,       rtfFunctionKey,         "fn",           0 },
2413
2414         /*
2415          * Bookmark attributes
2416          */
2417
2418         { rtfBookmarkAttr, rtfBookmarkFirstCol, "bkmkcolf",     0 },
2419         { rtfBookmarkAttr, rtfBookmarkLastCol,  "bkmkcoll",     0 },
2420
2421         /*
2422          * Index entry attributes
2423          */
2424
2425         { rtfIndexAttr, rtfIndexNumber,         "xef",          0 },
2426         { rtfIndexAttr, rtfIndexBold,           "bxe",          0 },
2427         { rtfIndexAttr, rtfIndexItalic,         "ixe",          0 },
2428
2429         /*
2430          * Table of contents attributes
2431          */
2432
2433         { rtfTOCAttr,   rtfTOCType,             "tcf",          0 },
2434         { rtfTOCAttr,   rtfTOCLevel,            "tcl",          0 },
2435
2436         /*
2437          * Drawing object attributes
2438          */
2439
2440         { rtfDrawAttr,  rtfDrawLock,            "dolock",       0 },
2441         { rtfDrawAttr,  rtfDrawPageRelX,        "doxpage",      0 },
2442         { rtfDrawAttr,  rtfDrawColumnRelX,      "dobxcolumn",   0 },
2443         { rtfDrawAttr,  rtfDrawMarginRelX,      "dobxmargin",   0 },
2444         { rtfDrawAttr,  rtfDrawPageRelY,        "dobypage",     0 },
2445         { rtfDrawAttr,  rtfDrawColumnRelY,      "dobycolumn",   0 },
2446         { rtfDrawAttr,  rtfDrawMarginRelY,      "dobymargin",   0 },
2447         { rtfDrawAttr,  rtfDrawHeight,          "dobhgt",       0 },
2448
2449         { rtfDrawAttr,  rtfDrawBeginGroup,      "dpgroup",      0 },
2450         { rtfDrawAttr,  rtfDrawGroupCount,      "dpcount",      0 },
2451         { rtfDrawAttr,  rtfDrawEndGroup,        "dpendgroup",   0 },
2452         { rtfDrawAttr,  rtfDrawArc,             "dparc",        0 },
2453         { rtfDrawAttr,  rtfDrawCallout,         "dpcallout",    0 },
2454         { rtfDrawAttr,  rtfDrawEllipse,         "dpellipse",    0 },
2455         { rtfDrawAttr,  rtfDrawLine,            "dpline",       0 },
2456         { rtfDrawAttr,  rtfDrawPolygon,         "dppolygon",    0 },
2457         { rtfDrawAttr,  rtfDrawPolyLine,        "dppolyline",   0 },
2458         { rtfDrawAttr,  rtfDrawRect,            "dprect",       0 },
2459         { rtfDrawAttr,  rtfDrawTextBox,         "dptxbx",       0 },
2460
2461         { rtfDrawAttr,  rtfDrawOffsetX,         "dpx",          0 },
2462         { rtfDrawAttr,  rtfDrawSizeX,           "dpxsize",      0 },
2463         { rtfDrawAttr,  rtfDrawOffsetY,         "dpy",          0 },
2464         { rtfDrawAttr,  rtfDrawSizeY,           "dpysize",      0 },
2465
2466         { rtfDrawAttr,  rtfCOAngle,             "dpcoa",        0 },
2467         { rtfDrawAttr,  rtfCOAccentBar,         "dpcoaccent",   0 },
2468         { rtfDrawAttr,  rtfCOBestFit,           "dpcobestfit",  0 },
2469         { rtfDrawAttr,  rtfCOBorder,            "dpcoborder",   0 },
2470         { rtfDrawAttr,  rtfCOAttachAbsDist,     "dpcodabs",     0 },
2471         { rtfDrawAttr,  rtfCOAttachBottom,      "dpcodbottom",  0 },
2472         { rtfDrawAttr,  rtfCOAttachCenter,      "dpcodcenter",  0 },
2473         { rtfDrawAttr,  rtfCOAttachTop,         "dpcodtop",     0 },
2474         { rtfDrawAttr,  rtfCOLength,            "dpcolength",   0 },
2475         { rtfDrawAttr,  rtfCONegXQuadrant,      "dpcominusx",   0 },
2476         { rtfDrawAttr,  rtfCONegYQuadrant,      "dpcominusy",   0 },
2477         { rtfDrawAttr,  rtfCOOffset,            "dpcooffset",   0 },
2478         { rtfDrawAttr,  rtfCOAttachSmart,       "dpcosmarta",   0 },
2479         { rtfDrawAttr,  rtfCODoubleLine,        "dpcotdouble",  0 },
2480         { rtfDrawAttr,  rtfCORightAngle,        "dpcotright",   0 },
2481         { rtfDrawAttr,  rtfCOSingleLine,        "dpcotsingle",  0 },
2482         { rtfDrawAttr,  rtfCOTripleLine,        "dpcottriple",  0 },
2483
2484         { rtfDrawAttr,  rtfDrawTextBoxMargin,   "dptxbxmar",    0 },
2485         { rtfDrawAttr,  rtfDrawTextBoxText,     "dptxbxtext",   0 },
2486         { rtfDrawAttr,  rtfDrawRoundRect,       "dproundr",     0 },
2487
2488         { rtfDrawAttr,  rtfDrawPointX,          "dpptx",        0 },
2489         { rtfDrawAttr,  rtfDrawPointY,          "dppty",        0 },
2490         { rtfDrawAttr,  rtfDrawPolyCount,       "dppolycount",  0 },
2491
2492         { rtfDrawAttr,  rtfDrawArcFlipX,        "dparcflipx",   0 },
2493         { rtfDrawAttr,  rtfDrawArcFlipY,        "dparcflipy",   0 },
2494
2495         { rtfDrawAttr,  rtfDrawLineBlue,        "dplinecob",    0 },
2496         { rtfDrawAttr,  rtfDrawLineGreen,       "dplinecog",    0 },
2497         { rtfDrawAttr,  rtfDrawLineRed,         "dplinecor",    0 },
2498         { rtfDrawAttr,  rtfDrawLinePalette,     "dplinepal",    0 },
2499         { rtfDrawAttr,  rtfDrawLineDashDot,     "dplinedado",   0 },
2500         { rtfDrawAttr,  rtfDrawLineDashDotDot,  "dplinedadodo", 0 },
2501         { rtfDrawAttr,  rtfDrawLineDash,        "dplinedash",   0 },
2502         { rtfDrawAttr,  rtfDrawLineDot,         "dplinedot",    0 },
2503         { rtfDrawAttr,  rtfDrawLineGray,        "dplinegray",   0 },
2504         { rtfDrawAttr,  rtfDrawLineHollow,      "dplinehollow", 0 },
2505         { rtfDrawAttr,  rtfDrawLineSolid,       "dplinesolid",  0 },
2506         { rtfDrawAttr,  rtfDrawLineWidth,       "dplinew",      0 },
2507
2508         { rtfDrawAttr,  rtfDrawHollowEndArrow,  "dpaendhol",    0 },
2509         { rtfDrawAttr,  rtfDrawEndArrowLength,  "dpaendl",      0 },
2510         { rtfDrawAttr,  rtfDrawSolidEndArrow,   "dpaendsol",    0 },
2511         { rtfDrawAttr,  rtfDrawEndArrowWidth,   "dpaendw",      0 },
2512         { rtfDrawAttr,  rtfDrawHollowStartArrow,"dpastarthol",  0 },
2513         { rtfDrawAttr,  rtfDrawStartArrowLength,"dpastartl",    0 },
2514         { rtfDrawAttr,  rtfDrawSolidStartArrow, "dpastartsol",  0 },
2515         { rtfDrawAttr,  rtfDrawStartArrowWidth, "dpastartw",    0 },
2516
2517         { rtfDrawAttr,  rtfDrawBgFillBlue,      "dpfillbgcb",   0 },
2518         { rtfDrawAttr,  rtfDrawBgFillGreen,     "dpfillbgcg",   0 },
2519         { rtfDrawAttr,  rtfDrawBgFillRed,       "dpfillbgcr",   0 },
2520         { rtfDrawAttr,  rtfDrawBgFillPalette,   "dpfillbgpal",  0 },
2521         { rtfDrawAttr,  rtfDrawBgFillGray,      "dpfillbggray", 0 },
2522         { rtfDrawAttr,  rtfDrawFgFillBlue,      "dpfillfgcb",   0 },
2523         { rtfDrawAttr,  rtfDrawFgFillGreen,     "dpfillfgcg",   0 },
2524         { rtfDrawAttr,  rtfDrawFgFillRed,       "dpfillfgcr",   0 },
2525         { rtfDrawAttr,  rtfDrawFgFillPalette,   "dpfillfgpal",  0 },
2526         { rtfDrawAttr,  rtfDrawFgFillGray,      "dpfillfggray", 0 },
2527         { rtfDrawAttr,  rtfDrawFillPatIndex,    "dpfillpat",    0 },
2528
2529         { rtfDrawAttr,  rtfDrawShadow,          "dpshadow",     0 },
2530         { rtfDrawAttr,  rtfDrawShadowXOffset,   "dpshadx",      0 },
2531         { rtfDrawAttr,  rtfDrawShadowYOffset,   "dpshady",      0 },
2532
2533         { rtfVersion,   -1,                     "rtf",          0 },
2534         { rtfDefFont,   -1,                     "deff",         0 },
2535
2536         { 0,            -1,                     (char *) NULL,  0 }
2537 };
2538
2539
2540 /*
2541  * Initialize lookup table hash values.  Only need to do this once.
2542  */
2543
2544 static void LookupInit(void)
2545 {
2546 static int      inited = 0;
2547 RTFKey  *rp;
2548
2549         if (inited == 0)
2550         {
2551                 for (rp = rtfKey; rp->rtfKStr != (char *) NULL; rp++)
2552                         rp->rtfKHash = Hash (rp->rtfKStr);
2553                 ++inited;
2554         }
2555 }
2556
2557
2558 /*
2559  * Determine major and minor number of control token.  If it's
2560  * not found, the class turns into rtfUnknown.
2561  */
2562
2563 static void Lookup(char *s)
2564 {
2565 RTFKey  *rp;
2566 int     hash;
2567
2568         TRACE("\n");
2569         ++s;                    /* skip over the leading \ character */
2570         hash = Hash (s);
2571         for (rp = rtfKey; rp->rtfKStr != (char *) NULL; rp++)
2572         {
2573                 if (hash == rp->rtfKHash && strcmp (s, rp->rtfKStr) == 0)
2574                 {
2575                         rtfClass = rtfControl;
2576                         rtfMajor = rp->rtfKMajor;
2577                         rtfMinor = rp->rtfKMinor;
2578                         return;
2579                 }
2580         }
2581         rtfClass = rtfUnknown;
2582 }
2583
2584
2585 /*
2586  * Compute hash value of symbol
2587  */
2588
2589 static int Hash(char *s)
2590 {
2591 char    c;
2592 int     val = 0;
2593
2594         while ((c = *s++) != '\0')
2595                 val += (int) c;
2596         return (val);
2597 }
2598
2599
2600 /* ---------------------------------------------------------------------- */
2601
2602 /*
2603  * Memory allocation routines
2604  */
2605
2606
2607 /*
2608  * Return pointer to block of size bytes, or NULL if there's
2609  * not enough memory available.
2610  *
2611  * This is called through RTFAlloc(), a define which coerces the
2612  * argument to int.  This avoids the persistent problem of allocation
2613  * failing under THINK C when a long is passed.
2614  */
2615
2616 char *_RTFAlloc(int size)
2617 {
2618         return HeapAlloc(RICHED32_hHeap, 0, size);
2619 }
2620
2621
2622 /*
2623  * Saves a string on the heap and returns a pointer to it.
2624  */
2625
2626
2627 char *RTFStrSave(char *s)
2628 {
2629 char    *p;
2630
2631         if ((p = RTFAlloc ((int) (strlen (s) + 1))) == (char *) NULL)
2632                 return ((char *) NULL);
2633         return (strcpy (p, s));
2634 }
2635
2636
2637 void RTFFree(char *p)
2638 {
2639         if (p != (char *) NULL)
2640                 HeapFree(RICHED32_hHeap, 0, p);
2641 }
2642
2643
2644 /* ---------------------------------------------------------------------- */
2645
2646
2647 /*
2648  * Token comparison routines
2649  */
2650
2651 int RTFCheckCM(int class, int major)
2652 {
2653         return (rtfClass == class && rtfMajor == major);
2654 }
2655
2656
2657 int RTFCheckCMM(int class, int major, int minor)
2658 {
2659         return (rtfClass == class && rtfMajor == major && rtfMinor == minor);
2660 }
2661
2662
2663 int RTFCheckMM(int major, int minor)
2664 {
2665         return (rtfMajor == major && rtfMinor == minor);
2666 }
2667
2668
2669 /* ---------------------------------------------------------------------- */
2670
2671
2672 int RTFCharToHex(char c)
2673 {
2674         if (isupper (c))
2675                 c = tolower (c);
2676         if (isdigit (c))
2677                 return (c - '0');       /* '0'..'9' */
2678         return (c - 'a' + 10);          /* 'a'..'f' */
2679 }
2680
2681
2682 int RTFHexToChar(int i)
2683 {
2684         if (i < 10)
2685                 return (i + '0');
2686         return (i - 10 + 'a');
2687 }
2688
2689
2690 /* ---------------------------------------------------------------------- */
2691
2692 /*
2693  * RTFReadOutputMap() -- Read output translation map
2694  */
2695
2696 /*
2697  * Read in an array describing the relation between the standard character set
2698  * and an RTF translator's corresponding output sequences.  Each line consists
2699  * of a standard character name and the output sequence for that character.
2700  *
2701  * outMap is an array of strings into which the sequences should be placed.
2702  * It should be declared like this in the calling program:
2703  *
2704  *      char *outMap[rtfSC_MaxChar];
2705  *
2706  * reinit should be non-zero if outMap should be initialized
2707  * zero otherwise.
2708  *
2709  */
2710
2711 int RTFReadOutputMap(char *outMap[], int reinit)
2712 {
2713     int  i;
2714     int  stdCode;
2715     char *name, *seq;
2716
2717     if (reinit)
2718     {
2719         for (i = 0; i < rtfSC_MaxChar; i++)
2720         {
2721             outMap[i] = (char *) NULL;
2722         }
2723     }
2724
2725     for (i=0 ;i< sizeof(text_map)/sizeof(char*); i+=2)
2726     {
2727         name = text_map[i];
2728         seq  = text_map[i+1];
2729         stdCode = RTFStdCharCode( name );
2730         outMap[stdCode] = seq;
2731     }
2732
2733     return (1);
2734 }
2735
2736 /* ---------------------------------------------------------------------- */
2737
2738 /*
2739  * Open a library file.
2740  */
2741
2742
2743 static FILE     *(*libFileOpen) () = NULL;
2744
2745
2746
2747 void RTFSetOpenLibFileProc(FILE *(*proc)())
2748 {
2749     libFileOpen = proc;
2750 }
2751
2752
2753 FILE *RTFOpenLibFile (char *file, char *mode)
2754 {
2755         if (libFileOpen == NULL)
2756                 return ((FILE *) NULL);
2757         return ((*libFileOpen) (file, mode));
2758 }
2759
2760
2761 /* ---------------------------------------------------------------------- */
2762
2763 /*
2764  * Print message.  Default is to send message to stderr
2765  * but this may be overridden with RTFSetMsgProc().
2766  *
2767  * Message should include linefeeds as necessary.  If the default
2768  * function is overridden, the overriding function may want to
2769  * map linefeeds to another line ending character or sequence if
2770  * the host system doesn't use linefeeds.
2771  */
2772
2773
2774 static void DefaultMsgProc(char *s)
2775 {
2776     MESSAGE( "%s", s);
2777 }
2778
2779
2780 static RTFFuncPtr       msgProc = DefaultMsgProc;
2781
2782
2783 void RTFSetMsgProc(RTFFuncPtr proc)
2784 {
2785         msgProc = proc;
2786 }
2787
2788
2789 # ifdef STDARG
2790
2791 /*
2792  * This version is for systems with stdarg
2793  */
2794
2795 void RTFMsg (char *fmt, ...)
2796 {
2797 char    buf[rtfBufSiz];
2798
2799         va_list args;
2800         va_start (args,fmt);
2801         vsprintf (buf, fmt, args);
2802         va_end (args);
2803         (*msgProc) (buf);
2804 }
2805
2806 # else /* !STDARG */
2807
2808 # ifdef VARARGS
2809
2810
2811 /*
2812  * This version is for systems that have varargs.
2813  */
2814
2815 void RTFMsg (va_dcl va_alist)
2816 {
2817 va_list args;
2818 char    *fmt;
2819 char    buf[rtfBufSiz];
2820
2821         va_start (args);
2822         fmt = va_arg (args, char *);
2823         vsprintf (buf, fmt, args);
2824         va_end (args);
2825         (*msgProc) (buf);
2826 }
2827
2828 # else  /* !VARARGS */
2829
2830 /*
2831  * This version is for systems that don't have varargs.
2832  */
2833
2834 void RTFMsg (char *fmt, char *a1, char *a2, char *a3, char *a4, char *a5, char *a6, char *a7, char *a8, char *a9)
2835 {
2836 char    buf[rtfBufSiz];
2837
2838         sprintf (buf, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
2839         (*msgProc) (buf);
2840 }
2841
2842 # endif /* !VARARGS */
2843 # endif /* !STDARG */
2844
2845
2846 /* ---------------------------------------------------------------------- */
2847
2848
2849 /*
2850  * Process termination.  Print error message and exit.  Also prints
2851  * current token, and current input line number and position within
2852  * line if any input has been read from the current file.  (No input
2853  * has been read if prevChar is EOF).
2854  */
2855
2856 static void DefaultPanicProc(char *s)
2857 {
2858     MESSAGE( "%s", s);
2859         /*exit (1);*/
2860 }
2861
2862
2863 static RTFFuncPtr       panicProc = DefaultPanicProc;
2864
2865
2866 void RTFSetPanicProc(RTFFuncPtr proc)
2867 {
2868         panicProc = proc;
2869 }
2870
2871
2872 # ifdef STDARG
2873
2874 /*
2875  * This version is for systems with stdarg
2876  */
2877
2878 void RTFPanic(char *fmt, ...)
2879 {
2880 char    buf[rtfBufSiz];
2881
2882         va_list args;
2883         va_start (args,fmt);
2884         vsprintf (buf, fmt, args);
2885         va_end (args);
2886         (void) strcat (buf, "\n");
2887         if (prevChar != EOF && rtfTextBuf != (char *) NULL)
2888         {
2889                 sprintf (buf + strlen (buf),
2890                         "Last token read was \"%s\" near line %ld, position %d.\n",
2891                         rtfTextBuf, rtfLineNum, rtfLinePos);
2892         }
2893         (*panicProc) (buf);
2894 }
2895
2896 # else /* !STDARG */
2897
2898 # ifdef VARARGS
2899
2900
2901 /*
2902  * This version is for systems that have varargs.
2903  */
2904
2905 void RTFPanic(va_dcl va_alist)
2906 {
2907 va_list args;
2908 char    *fmt;
2909 char    buf[rtfBufSiz];
2910
2911         va_start (args);
2912         fmt = va_arg (args, char *);
2913         vsprintf (buf, fmt, args);
2914         va_end (args);
2915         (void) strcat (buf, "\n");
2916         if (prevChar != EOF && rtfTextBuf != (char *) NULL)
2917         {
2918                 sprintf (buf + strlen (buf),
2919                         "Last token read was \"%s\" near line %ld, position %d.\n",
2920                         rtfTextBuf, rtfLineNum, rtfLinePos);
2921         }
2922         (*panicProc) (buf);
2923 }
2924
2925 # else  /* !VARARGS */
2926
2927 /*
2928  * This version is for systems that don't have varargs.
2929  */
2930
2931 void RTFPanic (char *fmt, char *a1, char *a2, char *a3, char *a4, char *a5, char *a6, char *a7, char *a8, char *a9)
2932 {
2933 char    buf[rtfBufSiz];
2934
2935         sprintf (buf, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
2936         (void) strcat (buf, "\n");
2937         if (prevChar != EOF && rtfTextBuf != (char *) NULL)
2938         {
2939                 sprintf (buf + strlen (buf),
2940                         "Last token read was \"%s\" near line %ld, position %d.\n",
2941                         rtfTextBuf, rtfLineNum, rtfLinePos);
2942         }
2943         (*panicProc) (buf);
2944 }
2945
2946 # endif /* !VARARGS */
2947 # endif /* !STDARG */