home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / utils / rtfprsr / troff / reader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  35.8 KB  |  1,418 lines

  1. /*
  2.     Need to document error code meanings.
  3.     Change document to reflect RTFFuncPtr.
  4.  
  5.     reader.c - RTF file reader.  Distribution 1.06a2.
  6.  
  7.     ASCII 10 (\n) and 13 (\r) are ignored and silently discarded
  8.     (although the read hook will still get a look at them.)
  9.  
  10.     "\:" is not a ":", it's a control symbol.  But some versions of
  11.     Word seem to write "\:" for ":".  This reader treats "\:" as a
  12.     plain text ":"
  13.  
  14.     This reader needs to catch \cf and \cb in the color table
  15.     reader?  (Doesn't yet.)
  16. */
  17.  
  18. # include    <stdio.h>
  19. # include    <ctype.h>
  20. # ifdef    VARARGS
  21. # include    <varargs.h>
  22. # endif    /* VARARGS */
  23.  
  24. # include    "rtf.h"
  25.  
  26.  
  27. /*
  28.     Return pointer to new element of type t, or NULL
  29. */
  30.  
  31. # define    New(t)    ((t *) RTFAlloc ((int) sizeof (t)))
  32.  
  33.  
  34. # define    index    strchr
  35.  
  36. extern char    *index ();
  37. extern char    *strcpy ();
  38. extern char    *malloc ();
  39.  
  40.  
  41. static void    _RTFGetToken ();
  42. static int    GetChar ();
  43. static int    HexVal ();
  44. static void    ReadFontTbl ();
  45. static void    ReadColorTbl ();
  46. static void    ReadStyleSheet ();
  47. static void    ReadInfoGroup ();
  48. static void    ReadPictGroup ();
  49. static void    LookupInit ();
  50. static void    Lookup ();
  51. static int    Hash ();
  52. static void    Error ();
  53.  
  54.  
  55. /*
  56.     Public variables (listed in rtf.h)
  57. */
  58.  
  59. int    rtfClass;
  60. int    rtfMajor;
  61. int    rtfMinor;
  62. int    rtfParam;
  63. char    rtfTextBuf[rtfBufSiz];
  64. int    rtfTextLen;
  65.  
  66.  
  67. /*
  68.     Private stuff
  69. */
  70.  
  71. static int    pushedChar;    /* pushback char if read too far */
  72.  
  73. static int    pushedClass;    /* pushed token info for RTFUngetToken() */
  74. static int    pushedMajor;
  75. static int    pushedMinor;
  76. static int    pushedParam;
  77. static char    pushedTextBuf[rtfBufSiz];
  78.  
  79.  
  80. static RTFFont    *fontList = (RTFFont *) NULL;    /* these lists MUST be */
  81. static RTFColor    *colorList = (RTFColor *) NULL;    /* initialized to NULL */
  82. static RTFStyle    *styleList = (RTFStyle *) NULL;
  83.  
  84.  
  85. static FILE    *rtffp = stdin;
  86.  
  87.  
  88. /*
  89.     Initialize the reader.  This may be called multiple times,
  90.     to read multiple files.  The only thing not reset is the input
  91.     stream; that must be done with RTFSetStream().
  92. */
  93.  
  94. void RTFInit ()
  95. {
  96. int    i;
  97. RTFColor    *cp;
  98. RTFFont        *fp;
  99. RTFStyle    *sp;
  100. RTFStyleElt    *eltList, *ep;
  101.  
  102.     /* initialize lookup table */
  103.     LookupInit ();
  104.  
  105.     for (i = 0; i < rtfMaxClass; i++)
  106.         RTFSetClassCallback (i, (RTFFuncPtr) NULL);
  107.     for (i = 0; i < rtfMaxDestination; i++)
  108.         RTFSetDestinationCallback (i, (RTFFuncPtr) NULL);
  109.  
  110.     /* install built-in destination readers */
  111.     RTFSetDestinationCallback (rtfFontTbl, ReadFontTbl);
  112.     RTFSetDestinationCallback (rtfColorTbl, ReadColorTbl);
  113.     RTFSetDestinationCallback (rtfStyleSheet, ReadStyleSheet);
  114.     RTFSetDestinationCallback (rtfInfo, ReadInfoGroup);
  115.     RTFSetDestinationCallback (rtfPict, ReadPictGroup);
  116.  
  117.     RTFSetReadHook ((RTFFuncPtr) NULL);
  118.  
  119.     /* dump old lists if necessary */
  120.  
  121.     while (fontList != (RTFFont *) NULL)
  122.     {
  123.         fp = fontList->rtfNextFont;
  124.         RTFFree (fontList->rtfFName);
  125.         RTFFree ((char *) fontList);
  126.         fontList = fp;
  127.     }
  128.     while (colorList != (RTFColor *) NULL)
  129.     {
  130.         cp = colorList->rtfNextColor;
  131.         RTFFree ((char *) colorList);
  132.         colorList = cp;
  133.     }
  134.     while (styleList != (RTFStyle *) NULL)
  135.     {
  136.         sp = styleList->rtfNextStyle;
  137.         eltList = styleList->rtfSSEList;
  138.         while (eltList != (RTFStyleElt *) NULL)
  139.         {
  140.             ep = eltList->rtfNextSE;
  141.             RTFFree (eltList->rtfSEText);
  142.             RTFFree ((char *) eltList);
  143.             eltList = ep;
  144.         }
  145.         RTFFree (styleList->rtfSName);
  146.         RTFFree ((char *) styleList);
  147.         styleList = sp;
  148.     }
  149.  
  150.     rtfClass = -1;
  151.     pushedClass = -1;
  152.     pushedChar = EOF;
  153. }
  154.  
  155.  
  156. /*
  157.     Set the reader's input stream to the given stream.  Can
  158.     be used to redirect to other than the default (stdin).
  159. */
  160.  
  161. void RTFSetStream (stream)
  162. FILE    *stream;
  163. {
  164.     rtffp = stream;
  165. }
  166.  
  167.  
  168. /* ---------------------------------------------------------------------- */
  169.  
  170. /*
  171.     Callback table manipulation routines
  172. */
  173.  
  174.  
  175. /*
  176.     Install or return a writer callback for a token class
  177. */
  178.  
  179.  
  180. static RTFFuncPtr    ccb[rtfMaxClass];        /* class callbacks */
  181.  
  182. void RTFSetClassCallback (int class, RTFFuncPtr callback)
  183. {
  184.     if (class >= 0 && class < rtfMaxClass)
  185.         ccb[class] = callback;
  186. }
  187.  
  188.  
  189. RTFFuncPtr RTFGetClassCallback (int class)
  190. {
  191.     if (class >= 0 && class < rtfMaxClass)
  192.         return (ccb[class]);
  193.     return ((RTFFuncPtr) NULL);
  194. }
  195.  
  196.  
  197. /*
  198.     Install or return a writer callback for a destination type
  199. */
  200.  
  201. static RTFFuncPtr    dcb[rtfMaxDestination];    /* destination callbacks */
  202.  
  203. void RTFSetDestinationCallback (int dest, RTFFuncPtr callback)
  204. {
  205.     if (dest >= 0 && dest < rtfMaxDestination)
  206.         dcb[dest] = callback;
  207. }
  208.  
  209.  
  210. RTFFuncPtr RTFGetDestinationCallback (int dest)
  211. {
  212.     if (dest >= 0 && dest < rtfMaxDestination)
  213.         return (dcb[dest]);
  214.     return ((RTFFuncPtr) NULL);
  215. }
  216.  
  217.  
  218. /* ---------------------------------------------------------------------- */
  219.  
  220. /*
  221.     Token reading routines
  222. */
  223.  
  224.  
  225. /*
  226.     Read the input stream, invoking the writer's callbacks
  227.     where appropriate.
  228. */
  229.  
  230. void RTFRead ()
  231. {
  232.     while (RTFGetToken () != rtfEOF)
  233.         RTFRouteToken ();
  234. }
  235.  
  236.  
  237. /*
  238.     Route a token.  If it's a destination for which a reader is
  239.     installed, process the destination internally, otherwise
  240.     pass the token to the writer's class callback.
  241. */
  242.  
  243. void RTFRouteToken ()
  244. {
  245. RTFFuncPtr    p;
  246.  
  247.     if (rtfClass < 0 || rtfClass >= rtfMaxClass)    /* watchdog */
  248.     {
  249.         Error ("Unknown class %d: %s (reader malfunction)",
  250.                             rtfClass, rtfTextBuf);
  251.     }
  252.     if (RTFCheckCM (rtfControl, rtfDestination))
  253.     {
  254.         /* invoke destination-specific callback if there is one */
  255.         if ((p = RTFGetDestinationCallback (rtfMinor))
  256.                             != (RTFFuncPtr) NULL)
  257.         {
  258.             (*p) ();
  259.             return;
  260.         }
  261.     }
  262.     /* invoke class callback if there is one */
  263.     if ((p = RTFGetClassCallback (rtfClass)) != (RTFFuncPtr) NULL)
  264.         (*p) ();
  265. }
  266.  
  267.  
  268. /*
  269.     Skip to the end of the current group.  When this returns,
  270.     writers that maintain a state stack may want to call their
  271.     state unstacker; global vars will still be set to the group's
  272.     closing brace.
  273. */
  274.  
  275. void RTFSkipGroup ()
  276. {
  277. int    level = 1;
  278.  
  279.     while (RTFGetToken () != rtfEOF)
  280.     {
  281.         if (rtfClass == rtfGroup)
  282.         {
  283.             if (rtfMajor == rtfBeginGroup)
  284.                 ++level;
  285.             else if (rtfMajor == rtfEndGroup)
  286.             {
  287.                 if (--level < 1)
  288.                     break;    /* end of initial group */
  289.             }
  290.         }
  291.     }
  292. }
  293.  
  294.  
  295. /*
  296.     Read one token.  Call the read hook if there is one.  The
  297.     token class is the return value.  Returns rtfEOF when there
  298.     are no more tokens.
  299. */
  300.  
  301. int RTFGetToken ()
  302. {
  303. RTFFuncPtr    p;
  304.  
  305.     for (;;)
  306.     {
  307.         _RTFGetToken ();
  308.         if ((p = RTFGetReadHook ()) != (RTFFuncPtr) NULL)
  309.             (*p) ();    /* give read hook a look at token */
  310.  
  311.         /* Silently discard newlines and carriage returns.  */
  312.         if (!(rtfClass == rtfText
  313.             && (rtfMajor == '\n' || rtfMajor == '\r')))
  314.             break;
  315.     }
  316.     return (rtfClass);
  317. }
  318.  
  319.  
  320. /*
  321.     Install or return a token reader hook.
  322. */
  323.  
  324. static RTFFuncPtr    readHook;
  325.  
  326. void RTFSetReadHook (RTFFuncPtr    f)
  327. {
  328.     readHook = f;
  329. }
  330.  
  331.  
  332. RTFFuncPtr RTFGetReadHook ()
  333. {
  334.     return (readHook);
  335. }
  336.  
  337.  
  338. void RTFUngetToken ()
  339. {
  340.     if (pushedClass >= 0)    /* there's already an ungotten token */
  341.         Error ("cannot unget two tokens");
  342.     if (rtfClass < 0)
  343.         Error ("no token to unget");
  344.     pushedClass = rtfClass;
  345.     pushedMajor = rtfMajor;
  346.     pushedMinor = rtfMinor;
  347.     pushedParam = rtfParam;
  348.     (void) strcpy (pushedTextBuf, rtfTextBuf);
  349. }
  350.  
  351.  
  352. int RTFPeekToken ()
  353. {
  354.     _RTFGetToken ();
  355.     RTFUngetToken ();
  356.     return (rtfClass);
  357. }
  358.  
  359.  
  360. static void _RTFGetToken ()
  361. {
  362. int    sign;
  363. int    c;
  364.  
  365.     /* check for pushed token from RTFUngetToken() */
  366.  
  367.     if (pushedClass >= 0)
  368.     {
  369.         rtfClass = pushedClass;
  370.         rtfMajor = pushedMajor;
  371.         rtfMinor = pushedMinor;
  372.         rtfParam = pushedParam;
  373.         (void) strcpy (rtfTextBuf, pushedTextBuf);
  374.         rtfTextLen = strlen (rtfTextBuf);
  375.         pushedClass = -1;
  376.         return;
  377.     }
  378.  
  379.     /* initialize token vars */
  380.  
  381.     rtfClass = rtfUnknown;
  382.     rtfParam = rtfNoParam;
  383.     rtfTextBuf[rtfTextLen = 0] = '\0';
  384.  
  385.     /* get first character, which may be a pushback from previous token */
  386.  
  387.     if (pushedChar != EOF)
  388.     {
  389.         c = pushedChar;
  390.         rtfTextBuf[rtfTextLen] = c;
  391.         rtfTextBuf[++rtfTextLen] = '\0';
  392.         pushedChar = EOF;
  393.     }
  394.     else if ((c = GetChar ()) == EOF)
  395.     {
  396.         rtfClass = rtfEOF;
  397.         return;
  398.     }
  399.  
  400.     if (c == '{')
  401.     {
  402.         rtfClass = rtfGroup;
  403.         rtfMajor = rtfBeginGroup;
  404.         return;
  405.     }
  406.     if (c == '}')
  407.     {
  408.         rtfClass = rtfGroup;
  409.         rtfMajor = rtfEndGroup;
  410.         return;
  411.     }
  412.     if (c != '\\')
  413.     {
  414.         /*
  415.             Two possibilities here:
  416.             1) ASCII 9, effectively like \tab control symbol
  417.             2) literal text char
  418.         */
  419.         if (c == '\t')            /* ASCII 9 */
  420.         {
  421.             rtfClass = rtfControl;
  422.             rtfMajor = rtfSpecialChar;
  423.             rtfMinor = rtfTab;
  424.         }
  425.         else
  426.         {
  427.             rtfClass = rtfText;
  428.             rtfMajor = c;
  429.         }
  430.         return;
  431.     }
  432.     if ((c = GetChar ()) == EOF)
  433.     {
  434.         /* early eof, whoops (class is rtfUnknown) */
  435.         return;
  436.     }
  437.     if (!isalpha (c))
  438.     {
  439.         /*
  440.             Three possibilities here:
  441.             1) hex encoded text char, e.g., \'d5, \'d3
  442.             2) special escaped text char, e.g., \{, \}
  443.             3) control symbol, e.g., \_, \-, \|, \<10>
  444.         */
  445.         if (c == '\'')                /* hex char */
  446.         {
  447.         int    c2;
  448.  
  449.             if ((c = GetChar ()) != EOF && (c2 = GetChar ()) != EOF)
  450.             {
  451.                 /* should do isxdigit check! */
  452.                 rtfClass = rtfText;
  453.                 rtfMajor = HexVal (c) * 16 + HexVal (c2);
  454.                 return;
  455.             }
  456.             /* early eof, whoops (class is rtfUnknown) */
  457.             return;
  458.         }
  459.  
  460.         if (index (":{}\\", c) != (char *) NULL) /* escaped char */
  461.         {
  462.             rtfClass = rtfText;
  463.             rtfMajor = c;
  464.             return;
  465.         }
  466.  
  467.         /* control symbol */
  468.         Lookup (rtfTextBuf);    /* sets class, major, minor */
  469.         return;
  470.     }
  471.     /* control word */
  472.     while (isalpha (c))
  473.     {
  474.         if ((c = GetChar ()) == EOF)
  475.             break;
  476.     }
  477.  
  478.     /*
  479.         At this point, the control word is all collected, so the
  480.         major/minor numbers are determined before the parameter
  481.         (if any) is scanned.  There will be one too many characters
  482.         in the buffer, though, so fix up before and restore after
  483.         looking up.
  484.     */
  485.  
  486.     if (c != EOF)
  487.         rtfTextBuf[rtfTextLen-1] = '\0';
  488.     Lookup (rtfTextBuf);    /* sets class, major, minor */
  489.     if (c != EOF)
  490.         rtfTextBuf[rtfTextLen-1] = c;
  491.  
  492.     /*
  493.         Should be looking at first digit of parameter if there
  494.         is one, unless it's negative.  In that case, next char
  495.         is '-', so need to gobble next char, and remember sign.
  496.     */
  497.  
  498.     sign = 1;
  499.     if (c == '-')
  500.     {
  501.         sign = -1;
  502.         c = GetChar ();
  503.     }
  504.     if (c != EOF && isdigit (c))
  505.     {
  506.         rtfParam = 0;
  507.         while (isdigit (c))    /* gobble parameter */
  508.         {
  509.             rtfParam = rtfParam * 10 + c - '0';
  510.             if ((c = GetChar ()) == EOF)
  511.                 break;
  512.         }
  513.         rtfParam *= sign;
  514.     }
  515.     /*
  516.         If control symbol delimiter was a blank, gobble it.
  517.         Otherwise the character is first char of next token, so
  518.         push it back for next call.  In either case, delete the
  519.         delimiter from the token buffer.
  520.     */
  521.     if (c != EOF)
  522.     {
  523.         if (c != ' ')
  524.             pushedChar = c;
  525.         rtfTextBuf[--rtfTextLen] = '\0';
  526.     }
  527.     return;
  528. }
  529.  
  530.  
  531. /*
  532.     Distributions up through 1.04 assumed high bit could be set in
  533.     RTF file characters.  Beginning with 1.05, that's not true, but
  534.     still check and ignore such characters.  (Cope with things like
  535.     WriteNow on NeXT, which generates bad RTF by writing 8-bit
  536.     characters.)
  537. */
  538.  
  539. static int GetChar ()
  540. {
  541. int    c;
  542.  
  543.     if ((c = getc (rtffp)) != EOF)
  544.     {
  545.         if (c & 0x80)
  546.         {
  547.             fprintf (stderr, "Character found with high bit set");
  548.             fprintf (stderr, " (%#x) -> changed to '?'\n", c);
  549.             c = '?';
  550.         }
  551.         rtfTextBuf[rtfTextLen] = c;
  552.         rtfTextBuf[++rtfTextLen] = '\0';
  553.     }
  554.     return (c);
  555. }
  556.  
  557.  
  558. static int HexVal (char    c)
  559. {
  560.     if (isupper (c))
  561.         c = tolower (c);
  562.     if (isdigit (c))
  563.         return (c - '0');    /* '0'..'9' */
  564.     return (c - 'a' + 10);        /* 'a'..'f' */
  565. }
  566.  
  567.  
  568. /*
  569.     Synthesize a token by setting the global variables to the
  570.     values supplied.  Typically this is followed with a call
  571.     to RTFRouteToken().
  572.  
  573.     If param is non-negative, it becomes part of the token text.
  574. */
  575.  
  576. void RTFSetToken (int class, int major, int minor, int param, char *text)
  577. {
  578.     rtfClass = class;
  579.     rtfMajor = major;
  580.     rtfMinor = minor;
  581.     rtfParam = param;
  582.     if (param == rtfNoParam)
  583.         (void) strcpy (rtfTextBuf, text);
  584.     else
  585.         sprintf (rtfTextBuf, "%s%d", text, param);
  586.     rtfTextLen = strlen (rtfTextBuf);
  587. }
  588.  
  589.  
  590. /* ---------------------------------------------------------------------- */
  591.  
  592. /*
  593.     Special destination readers.  They gobble the destination so the
  594.     writer doesn't have to deal with them.  That's wrong for any
  595.     translator that wants to process any of these itself.  In that
  596.     case, these readers should be overridden by installing a different
  597.     destination callback.
  598.  
  599.     NOTE: The last token read by each of these reader will be the
  600.     destination's terminating '}', which will then be the current token.
  601.     That '}' token is passed to RTFRouteToken() - the writer has already
  602.     seen the '{' that began the destination group, and may have pushed a
  603.     state; it also needs to know at the end of the group that a state
  604.     should be popped.
  605.  
  606.     It's important that rtf.h and the control token lookup table list
  607.     as many symbols as possible, because these readers unfortunately
  608.     make strict assumptions about the input they expect, and a token
  609.     of class rtfUnknown will throw them off easily.
  610. */
  611.  
  612.  
  613. /*
  614.     Read { \fonttbl ... } destination.  Old font tables don't have
  615.     braces around each table entry; try to adjust for that.
  616. */
  617.  
  618. static void ReadFontTbl ()
  619. {
  620. RTFFont    *fp;
  621. char    buf[rtfBufSiz], *bp;
  622. int    old = -1;
  623.  
  624.     for (;;)
  625.     {
  626.         (void) RTFGetToken ();
  627.         if (RTFCheckCM (rtfGroup, rtfEndGroup))
  628.             break;
  629.         if (old < 0)        /* first entry - determine tbl type */
  630.         {
  631.             if (RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
  632.                 old = 1;    /* no brace */
  633.             else if (RTFCheckCM (rtfGroup, rtfBeginGroup))
  634.                 old = 0;    /* brace */
  635.             else            /* can't tell! */
  636.                 Error ("FTErr - Cannot determine format");
  637.         }
  638.         if (old == 0)        /* need to find "{" here */
  639.         {
  640.             if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
  641.                 Error ("FTErr - missing \"{\"");
  642.             (void) RTFGetToken ();    /* yes, skip to next token */
  643.         }
  644.         if ((fp = New (RTFFont)) == (RTFFont *) NULL)
  645.             Error ("FTErr - cannot allocate font entry");
  646.         fp->rtfNextFont = fontList;
  647.         fontList = fp;
  648.         if (!RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
  649.             Error ("FTErr - missing font number");
  650.         fp->rtfFNum = rtfParam;
  651.         (void) RTFGetToken ();
  652.         if (!RTFCheckCM (rtfControl, rtfFontFamily))
  653.             Error ("FTErr - missing font family");
  654.         fp->rtfFFamily = rtfMinor;
  655.         bp = buf;
  656.         while (RTFGetToken () == rtfText)
  657.         {
  658.             if (rtfMajor == ';')
  659.                 break;
  660.             *bp++ = rtfMajor;
  661.         }
  662.         *bp = '\0';
  663.         if (buf[0] == '\0')
  664.             Error ("FTErr - missing font name");
  665.         if ((fp->rtfFName = RTFStrSave (buf)) == (char *) NULL)
  666.             Error ("FTErr - cannot allocate font name");
  667.         if (old == 0)    /* need to see "}" here */
  668.         {
  669.             (void) RTFGetToken ();
  670.             if (!RTFCheckCM (rtfGroup, rtfEndGroup))
  671.                 Error ("FTErr - missing \"}\"");
  672.         }
  673.     }
  674.     RTFRouteToken ();    /* feed "}" back to router */
  675. }
  676.  
  677.  
  678. /*
  679.     The color table entries have color values of -1 if
  680.     the default color should be used for the entry (only
  681.     a semi-colon is given in the definition, no color values).
  682.     There will be a problem if a partial entry (1 or 2 but
  683.     not 3 color values) is given.  The possibility is ignored
  684.     here.
  685. */
  686.  
  687. static void ReadColorTbl ()
  688. {
  689. RTFColor    *cp;
  690. int        cnum = 0;
  691.  
  692.     for (;;)
  693.     {
  694.         (void) RTFGetToken ();
  695.         if (RTFCheckCM (rtfGroup, rtfEndGroup))
  696.             break;
  697.         if ((cp = New (RTFColor)) == (RTFColor *) NULL)
  698.             Error ("CTErr - cannot allocate color entry");
  699.         cp->rtfCNum = cnum++;
  700.         cp->rtfCRed = cp->rtfCGreen = cp->rtfCBlue = -1;
  701.         cp->rtfNextColor = colorList;
  702.         colorList = cp;
  703.         for (;;)
  704.         {
  705.             if (!RTFCheckCM (rtfControl, rtfColorName))
  706.                 break;
  707.             switch (rtfMinor)
  708.             {
  709.             case rtfRed:    cp->rtfCRed = rtfParam; break;
  710.             case rtfGreen:    cp->rtfCGreen = rtfParam; break;
  711.             case rtfBlue:    cp->rtfCBlue = rtfParam; break;
  712.             }
  713.             RTFGetToken ();
  714.         }
  715.         if (!RTFCheckCM (rtfText, (int) ';'))
  716.             Error ("CTErr - malformed entry");
  717.     }
  718.     RTFRouteToken ();    /* feed "}" back to router */
  719. }
  720.  
  721.  
  722. /*
  723.     The "Normal" style definition doesn't contain any style number
  724.     (why?), all others do.  Normal style is given style 0.
  725. */
  726.  
  727. static void ReadStyleSheet ()
  728. {
  729. RTFStyle    *sp;
  730. RTFStyleElt    *sep, *sepLast;
  731. char        buf[rtfBufSiz], *bp;
  732.  
  733.     for (;;)
  734.     {
  735.         (void) RTFGetToken ();
  736.         if (RTFCheckCM (rtfGroup, rtfEndGroup))
  737.             break;
  738.         if ((sp = New (RTFStyle)) == (RTFStyle *) NULL)
  739.             Error ("SSErr - cannot allocate stylesheet entry");
  740.         sp->rtfSNum = -1;
  741.         sp->rtfSBasedOn = rtfBasedOnNone;
  742.         sp->rtfSNextPar = -1;
  743.         sp->rtfSSEList = sepLast = (RTFStyleElt *) NULL;
  744.         sp->rtfNextStyle = styleList;
  745.         sp->rtfExpanding = 0;
  746.         styleList = sp;
  747.         if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
  748.             Error ("SSErr - missing \"{\"");
  749.         while (RTFGetToken () == rtfControl)
  750.         {
  751.             if (RTFCheckMM (rtfParAttr, rtfStyleNum))
  752.             {
  753.                 sp->rtfSNum = rtfParam;
  754.                 continue;
  755.             }
  756.             if (RTFCheckMM (rtfStyleAttr, rtfBasedOn))
  757.             {
  758.                 sp->rtfSBasedOn = rtfParam;
  759.                 continue;
  760.             }
  761.             if (RTFCheckMM (rtfStyleAttr, rtfNext))
  762.             {
  763.                 sp->rtfSNextPar = rtfParam;
  764.                 continue;
  765.             }
  766.             if ((sep = New (RTFStyleElt)) == (RTFStyleElt *) NULL)
  767.                 Error ("SSErr - cannot allocate style element");
  768.             sep->rtfSEClass = rtfClass;
  769.             sep->rtfSEMajor = rtfMajor;
  770.             sep->rtfSEMinor = rtfMinor;
  771.             sep->rtfSEParam = rtfParam;
  772.             if ((sep->rtfSEText = RTFStrSave (rtfTextBuf))
  773.                             == (char *) NULL)
  774.                 Error ("SSErr - cannot allocate style element text");
  775.             if (sepLast == (RTFStyleElt *) NULL)
  776.                 sp->rtfSSEList = sep;    /* first element */
  777.             else                /* add to end */
  778.                 sepLast->rtfNextSE = sep;
  779.             sep->rtfNextSE = (RTFStyleElt *) NULL;
  780.             sepLast = sep;
  781.         }
  782.         if (sp->rtfSNextPar == -1)        /* \snext not given */
  783.             sp->rtfSNextPar = sp->rtfSNum;    /* next is itself */
  784.         if (rtfClass != rtfText)
  785.             Error ("SSErr - missing style name");
  786.         bp = buf;
  787.         while (rtfClass == rtfText)
  788.         {
  789.             if (rtfMajor == ';')
  790.             {
  791.                 (void) RTFGetToken ();
  792.                 break;
  793.             }
  794.             *bp++ = rtfMajor;
  795.             (void) RTFGetToken ();
  796.         }
  797.         *bp = '\0';
  798.         if (sp->rtfSNum < 0)    /* no style number was specified */
  799.         {            /* (only legal for Normal style) */
  800.             if (strcmp (buf, "Normal") != 0)
  801.                 Error ("SSErr - missing style number");
  802.             sp->rtfSNum = 0;
  803.         }
  804.         if ((sp->rtfSName = RTFStrSave (buf)) == (char *) NULL)
  805.             Error ("SSErr - cannot allocate style name");
  806.         if (!RTFCheckCM (rtfGroup, rtfEndGroup))
  807.             Error ("SSErr - missing \"}\"");
  808.     }
  809.     RTFRouteToken ();    /* feed "}" back to router */
  810. }
  811.  
  812.  
  813. static void ReadInfoGroup ()
  814. {
  815.     RTFSkipGroup ();
  816.     RTFRouteToken ();    /* feed "}" back to router */
  817. }
  818.  
  819.  
  820. static void ReadPictGroup ()
  821. {
  822.     RTFSkipGroup ();
  823.     RTFRouteToken ();    /* feed "}" back to router */
  824. }
  825.  
  826.  
  827. /* ---------------------------------------------------------------------- */
  828.  
  829. /*
  830.     Routines to return pieces of stylesheet, or font or color tables
  831. */
  832.  
  833.  
  834. RTFStyle *RTFGetStyle (int num)
  835. {
  836. RTFStyle    *s;
  837.  
  838.     if (num == -1)
  839.         return (styleList);
  840.     for (s = styleList; s != (RTFStyle *) NULL; s = s->rtfNextStyle)
  841.     {
  842.         if (s->rtfSNum == num)
  843.             break;
  844.     }
  845.     return (s);        /* NULL if not found */
  846. }
  847.  
  848.  
  849. RTFFont *RTFGetFont (int num)
  850. {
  851. RTFFont    *f;
  852.  
  853.     if (num == -1)
  854.         return (fontList);
  855.     for (f = fontList; f != (RTFFont *) NULL; f = f->rtfNextFont)
  856.     {
  857.         if (f->rtfFNum == num)
  858.             break;
  859.     }
  860.     return (f);        /* NULL if not found */
  861. }
  862.  
  863.  
  864. RTFColor *RTFGetColor (int num)
  865. {
  866. RTFColor    *c;
  867.  
  868.     if (num == -1)
  869.         return (colorList);
  870.     for (c = colorList; c != (RTFColor *) NULL; c = c->rtfNextColor)
  871.     {
  872.         if (c->rtfCNum == num)
  873.             break;
  874.     }
  875.     return (c);        /* NULL if not found */
  876. }
  877.  
  878.  
  879. /* ---------------------------------------------------------------------- */
  880.  
  881.  
  882. /*
  883.     Expand style n, if there is such a style.
  884. */
  885.  
  886. void RTFExpandStyle (int n)
  887. {
  888. RTFStyle    *s;
  889. RTFStyleElt    *se;
  890.  
  891.     if (n == -1 || (s = RTFGetStyle (n)) == (RTFStyle *) NULL)
  892.         return;
  893.     if (s->rtfExpanding != 0)
  894.         Error ("Style expansion loop, style %d", n);
  895.     s->rtfExpanding = 1;    /* set expansion flag for loop detection */
  896.     /*
  897.         Expand "based-on" style.  This is done by synthesizing
  898.         the token that the writer needs to see in order to trigger
  899.         another style expansion, and feeding to token back through
  900.         the router so the writer sees it.
  901.     */
  902.     RTFSetToken (rtfControl, rtfParAttr, rtfStyleNum, s->rtfSBasedOn, "\\s");
  903.     RTFRouteToken ();
  904.     /*
  905.         Now route the tokens unique to this style.  RTFSetToken()
  906.         isn't used because it would add the param value to the end
  907.         of the token text, which already has it in.
  908.     */
  909.     for (se = s->rtfSSEList; se != (RTFStyleElt *) NULL; se = se->rtfNextSE)
  910.     {
  911.         rtfClass = se->rtfSEClass;
  912.         rtfMajor = se->rtfSEMajor;
  913.         rtfMinor = se->rtfSEMinor;
  914.         rtfParam = se->rtfSEParam;
  915.         (void) strcpy (rtfTextBuf, se->rtfSEText);
  916.         rtfTextLen = strlen (rtfTextBuf);
  917.         RTFRouteToken ();
  918.     }
  919.     s->rtfExpanding = 0;    /* done - clear expansion flag */
  920. }
  921.  
  922.  
  923. /* ---------------------------------------------------------------------- */
  924.  
  925. /*
  926.     Control symbol lookup routines
  927. */
  928.  
  929.  
  930. typedef struct RTFKey    RTFKey;
  931.  
  932. struct RTFKey
  933. {
  934.     int    rtfKMajor;    /* major number */
  935.     int    rtfKMinor;    /* minor number */
  936.     char    *rtfKStr;    /* symbol name */
  937.     int    rtfKHash;    /* symbol name hash value */
  938. };
  939.  
  940. /*
  941.     A minor number of -1 means the token has no minor number
  942.     (all valid minor numbers are >= 0).
  943. */
  944.  
  945. static RTFKey    rtfKey[] =
  946. {
  947.     rtfSpecialChar,    rtfCurHeadPict,        "chpict",    0,    /* ?? */
  948.  
  949.     rtfSpecialChar,    rtfCurHeadDate,        "chdate",    0,
  950.     rtfSpecialChar,    rtfCurHeadTime,        "chtime",    0,
  951.     rtfSpecialChar,    rtfCurHeadPage,        "chpgn",    0,
  952.     rtfSpecialChar,    rtfCurFNote,        "chftn",    0,
  953.     rtfSpecialChar,    rtfCurAnnotRef,        "chatn",    0,
  954.     rtfSpecialChar,    rtfFNoteSep,        "chftnsep",    0,
  955.     rtfSpecialChar,    rtfFNoteCont,        "chftnsepc",    0,
  956.     rtfSpecialChar,    rtfFormula,        "|",        0,
  957.     rtfSpecialChar,    rtfNoBrkSpace,        "~",        0,
  958.     rtfSpecialChar,    rtfNoReqHyphen,        "-",        0,
  959.     rtfSpecialChar,    rtfNoBrkHyphen,        "_",        0,
  960.     rtfSpecialChar,    rtfCell,        "cell",        0,
  961.     rtfSpecialChar,    rtfRow,            "row",        0,
  962.     rtfSpecialChar,    rtfPar,            "par",        0,
  963.     rtfSpecialChar,    rtfPar,            "\n",        0,
  964.     rtfSpecialChar,    rtfPar,            "\r",        0,
  965.     rtfSpecialChar,    rtfSect,        "sect",        0,
  966.     rtfSpecialChar,    rtfPage,        "page",        0,
  967.     rtfSpecialChar,    rtfColumn,        "column",    0,
  968.     rtfSpecialChar,    rtfLine,        "line",        0,
  969.     rtfSpecialChar,    rtfTab,            "tab",        0,
  970.     rtfSpecialChar,    rtfOptDest,        "*",        0,
  971.     rtfSpecialChar,    rtfIIntVersion,        "vern",        0,
  972.     rtfSpecialChar,    rtfICreateTime,        "creatim",    0,
  973.     rtfSpecialChar,    rtfIRevisionTime,    "revtim",    0,
  974.     rtfSpecialChar,    rtfIPrintTime,        "printim",    0,
  975.     rtfSpecialChar,    rtfIBackupTime,        "buptim",    0,
  976.     rtfSpecialChar,    rtfIEditTime,        "edmins",    0,
  977.     rtfSpecialChar,    rtfIYear,        "yr",        0,
  978.     rtfSpecialChar,    rtfIMonth,        "mo",        0,
  979.     rtfSpecialChar,    rtfIDay,        "dy",        0,
  980.     rtfSpecialChar,    rtfIHour,        "hr",        0,
  981.     rtfSpecialChar,    rtfIMinute,        "min",        0,
  982.     rtfSpecialChar,    rtfINPages,        "nofpages",    0,
  983.     rtfSpecialChar,    rtfINWords,        "nofwords",    0,
  984.     rtfSpecialChar,    rtfINChars,        "nofchars",    0,
  985.     rtfSpecialChar,    rtfIIntID,        "id",        0,
  986.  
  987.     rtfCharAttr,    rtfPlain,        "plain",    0,
  988.     rtfCharAttr,    rtfBold,        "b",        0,
  989.     rtfCharAttr,    rtfItalic,        "i",        0,
  990.     rtfCharAttr,    rtfStrikeThru,        "strike",    0,
  991.     rtfCharAttr,    rtfOutline,        "outl",        0,
  992.     rtfCharAttr,    rtfShadow,        "shad",        0,
  993.     rtfCharAttr,    rtfSmallCaps,        "scaps",    0,
  994.     rtfCharAttr,    rtfAllCaps,        "caps",        0,
  995.     rtfCharAttr,    rtfInvisible,        "v",        0,
  996.     rtfCharAttr,    rtfFontNum,        "f",        0,
  997.     rtfCharAttr,    rtfFontSize,        "fs",        0,
  998.     rtfCharAttr,    rtfExpand,        "expnd",    0,
  999.     rtfCharAttr,    rtfUnderline,        "ul",        0,
  1000.     rtfCharAttr,    rtfWUnderline,        "ulw",        0,
  1001.     rtfCharAttr,    rtfDUnderline,        "uld",        0,
  1002.     rtfCharAttr,    rtfDbUnderline,        "uldb",        0,
  1003.     rtfCharAttr,    rtfNoUnderline,        "ulnone",    0,
  1004.     rtfCharAttr,    rtfSuperScript,        "up",        0,
  1005.     rtfCharAttr,    rtfSubScript,        "dn",        0,
  1006.     rtfCharAttr,    rtfRevised,        "revised",    0,
  1007.     rtfCharAttr,    rtfForeColor,        "cf",        0,
  1008.     rtfCharAttr,    rtfBackColor,        "cb",        0,
  1009.     rtfCharAttr,    rtfGray,        "gray",        0,
  1010.  
  1011.     rtfParAttr,    rtfParDef,        "pard",        0,
  1012.     rtfParAttr,    rtfStyleNum,        "s",        0,
  1013.     rtfParAttr,    rtfQuadLeft,        "ql",        0,
  1014.     rtfParAttr,    rtfQuadRight,        "qr",        0,
  1015.     rtfParAttr,    rtfQuadJust,        "qj",        0,
  1016.     rtfParAttr,    rtfQuadCenter,        "qc",        0,
  1017.     rtfParAttr,    rtfFirstIndent,        "fi",        0,
  1018.     rtfParAttr,    rtfLeftIndent,        "li",        0,
  1019.     rtfParAttr,    rtfRightIndent,        "ri",        0,
  1020.     rtfParAttr,    rtfSpaceBefore,        "sb",        0,
  1021.     rtfParAttr,    rtfSpaceAfter,        "sa",        0,
  1022.     rtfParAttr,    rtfSpaceBetween,    "sl",        0,
  1023.     rtfParAttr,    rtfInTable,        "intbl",    0,
  1024.     rtfParAttr,    rtfKeep,        "keep",        0,
  1025.     rtfParAttr,    rtfKeepNext,        "keepn",    0,
  1026.     rtfParAttr,    rtfSideBySide,        "sbys",        0,
  1027.     rtfParAttr,    rtfPBBefore,        "pagebb",    0,
  1028.     rtfParAttr,    rtfNoLineNum,        "noline",    0,
  1029.     rtfParAttr,    rtfTabPos,        "tx",        0,
  1030.     rtfParAttr,    rtfTabRight,        "tqr",        0,
  1031.     rtfParAttr,    rtfTabCenter,        "tqc",        0,
  1032.     rtfParAttr,    rtfTabDecimal,        "tqdec",    0,
  1033.     rtfParAttr,    rtfTabBar,        "tb",        0,
  1034.     rtfParAttr,    rtfBorderTop,        "brdrt",    0,
  1035.     rtfParAttr,    rtfBorderBottom,    "brdrb",    0,
  1036.     rtfParAttr,    rtfBorderLeft,        "brdrl",    0,
  1037.     rtfParAttr,    rtfBorderRight,        "brdrr",    0,
  1038.     rtfParAttr,    rtfBorderBar,        "bar",        0,
  1039.     rtfParAttr,    rtfBorderBox,        "box",        0,
  1040.     rtfParAttr,    rtfBorderBetween,    "brdrbtw",    0,
  1041.     rtfParAttr,    rtfBorderSingle,    "brdrs",    0,
  1042.     rtfParAttr,    rtfBorderThick,        "brdrth",    0,
  1043.     rtfParAttr,    rtfBorderShadow,    "brdrsh",    0,
  1044.     rtfParAttr,    rtfBorderDouble,    "brdrdb",    0,
  1045.     rtfParAttr,    rtfBorderDot,        "brdrdot",    0,
  1046.     rtfParAttr,    rtfBorderHair,        "brdrhair",    0,
  1047.     rtfParAttr,    rtfLeaderDot,        "tldot",    0,
  1048.     rtfParAttr,    rtfLeaderHyphen,    "tlhyph",    0,
  1049.     rtfParAttr,    rtfLeaderUnder,        "tlul",        0,
  1050.     rtfParAttr,    rtfLeaderThick,        "tlth",        0,
  1051.     rtfParAttr,    rtfBorderSpace,        "brsp",        0,
  1052.  
  1053.     rtfSectAttr,    rtfSectDef,        "sectd",    0,
  1054.     /*rtfSectAttr,    rtfNoBreak,        "nobreak",    0,
  1055.     rtfSectAttr,    rtfColBreak,        "colbreak",    0,
  1056.     rtfSectAttr,    rtfPageBreak,        "pagebreak",    0,
  1057.     rtfSectAttr,    rtfEvenBreak,        "evenbreak",    0,
  1058.     rtfSectAttr,    rtfOddBreak,        "oddbreak",    0,*/
  1059.     rtfSectAttr,    rtfNoBreak,        "sbknone",    0,
  1060.     rtfSectAttr,    rtfColBreak,        "sbkcol",    0,
  1061.     rtfSectAttr,    rtfPageBreak,        "sbkpage",    0,
  1062.     rtfSectAttr,    rtfEvenBreak,        "sbkeven",    0,
  1063.     rtfSectAttr,    rtfOddBreak,        "sbkodd",    0,
  1064.     rtfSectAttr,    rtfPageCont,        "pgncont",    0,
  1065.     rtfSectAttr,    rtfPageStarts,        "pgnstarts",    0,
  1066.     rtfSectAttr,    rtfPageRestart,        "pgnrestart",    0,
  1067.     rtfSectAttr,    rtfPageDecimal,        "pgndec",    0,
  1068.     rtfSectAttr,    rtfPageURoman,        "pgnucrm",    0,
  1069.     rtfSectAttr,    rtfPageLRoman,        "pgnlcrm",    0,
  1070.     rtfSectAttr,    rtfPageULetter,        "pgnucltr",    0,
  1071.     rtfSectAttr,    rtfPageLLetter,        "pgnlcltr",    0,
  1072.     rtfSectAttr,    rtfPageNumLeft,        "pgnx",        0,
  1073.     rtfSectAttr,    rtfPageNumTop,        "pgny",        0,
  1074.     rtfSectAttr,    rtfHeaderY,        "headery",    0,
  1075.     rtfSectAttr,    rtfFooterY,        "footery",    0,
  1076.     rtfSectAttr,    rtfLineModulus,        "linemod",    0,
  1077.     rtfSectAttr,    rtfLineDist,        "linex",    0,
  1078.     rtfSectAttr,    rtfLineStarts,        "linestarts",    0,
  1079.     rtfSectAttr,    rtfLineRestart,        "linerestart",    0,
  1080.     rtfSectAttr,    rtfLineRestartPg,    "lineppage",    0,
  1081.     rtfSectAttr,    rtfLineCont,        "linecont",    0,
  1082.     rtfSectAttr,    rtfTopVAlign,        "vertalt",    0,
  1083.     rtfSectAttr,    rtfBottomVAlign,    "vertal",    0,
  1084.     rtfSectAttr,    rtfCenterVAlign,    "vertalc",    0,
  1085.     rtfSectAttr,    rtfJustVAlign,        "vertalj",    0,
  1086.     rtfSectAttr,    rtfColumns,        "cols",        0,
  1087.     rtfSectAttr,    rtfColumnSpace,        "colsx",    0,
  1088.     rtfSectAttr,    rtfColumnLine,        "linebetcol",    0,
  1089.     rtfSectAttr,    rtfENoteHere,        "endnhere",    0,
  1090.     rtfSectAttr,    rtfTitleSpecial,    "titlepg",    0,
  1091.  
  1092.     rtfDocAttr,    rtfPaperWidth,        "paperw",    0,
  1093.     rtfDocAttr,    rtfPaperHeight,        "paperh",    0,
  1094.     rtfDocAttr,    rtfLeftMargin,        "margl",    0,
  1095.     rtfDocAttr,    rtfRightMargin,        "margr",    0,
  1096.     rtfDocAttr,    rtfTopMargin,        "margt",    0,
  1097.     rtfDocAttr,    rtfBottomMargin,    "margb",    0,
  1098.     rtfDocAttr,    rtfFacingPage,        "facingp",    0,
  1099.     rtfDocAttr,    rtfGutterWid,        "gutter",    0,
  1100.     rtfDocAttr,    rtfDefTab,        "deftab",    0,
  1101.     rtfDocAttr,    rtfWidowCtrl,        "widowctrl",    0,
  1102.     rtfDocAttr,    rtfHyphHotZone,        "hyphhotz",    0,
  1103.     rtfDocAttr,    rtfFNoteEndSect,    "endnotes",    0,
  1104.     rtfDocAttr,    rtfFNoteEndDoc,        "enddoc",    0,
  1105.     rtfDocAttr,    rtfFNoteBottom,        "ftnbj",    0,
  1106.     rtfDocAttr,    rtfFNoteText,        "ftntj",    0,
  1107.     rtfDocAttr,    rtfFNoteStart,        "ftnstart",    0,
  1108.     rtfDocAttr,    rtfFNoteRestart,    "ftnrestart",    0,
  1109.     rtfDocAttr,    rtfPageStart,        "pgnstart",    0,
  1110.     rtfDocAttr,    rtfLineStart,        "linestart",    0,
  1111.     rtfDocAttr,    rtfLandscape,        "landscape",    0,
  1112.     rtfDocAttr,    rtfFracWidth,        "fracwidth",    0,
  1113.     rtfDocAttr,    rtfNextFile,        "nextfile",    0,
  1114.     rtfDocAttr,    rtfTemplate,        "template",    0,
  1115.     rtfDocAttr,    rtfMakeBackup,        "makeback",    0,
  1116.     rtfDocAttr,    rtfRTFDefault,        "defformat",    0,
  1117.     rtfDocAttr,    rtfRevisions,        "revisions",    0,
  1118.     rtfDocAttr,    rtfMirrorMargin,    "margmirror",    0,
  1119.     rtfDocAttr,    rtfRevDisplay,        "revprop",    0,
  1120.     rtfDocAttr,    rtfRevBar,        "revbar",    0,
  1121.  
  1122.     rtfStyleAttr,    rtfBasedOn,        "sbasedon",    0,
  1123.     rtfStyleAttr,    rtfNext,        "snext",    0,
  1124.  
  1125.     rtfPictAttr,    rtfMacQD,        "macpict",    0,
  1126.     rtfPictAttr,    rtfWinMetafile,        "wmetafile",    0,
  1127.     rtfPictAttr,    rtfWinBitmap,        "wbitmap",    0,
  1128.     rtfPictAttr,    rtfPicWid,        "picw",        0,
  1129.     rtfPictAttr,    rtfPicHt,        "pich",        0,
  1130.     rtfPictAttr,    rtfPicGoalWid,        "picwgoal",    0,
  1131.     rtfPictAttr,    rtfPicGoalWid,        "picwGoal",    0,
  1132.     rtfPictAttr,    rtfPicGoalHt,        "pichgoal",    0,
  1133.     rtfPictAttr,    rtfPicGoalHt,        "pichGoal",    0,
  1134.     rtfPictAttr,    rtfPicScaleX,        "picscalex",    0,
  1135.     rtfPictAttr,    rtfPicScaleY,        "picscaley",    0,
  1136.     rtfPictAttr,    rtfPicScaled,        "picscaled",    0,
  1137.     rtfPictAttr,    rtfPicCropTop,        "piccropt",    0,
  1138.     rtfPictAttr,    rtfPicCropBottom,    "piccropb",    0,
  1139.     rtfPictAttr,    rtfPicCropLeft,        "piccropl",    0,
  1140.     rtfPictAttr,    rtfPicCropRight,    "piccropr",    0,
  1141.     rtfPictAttr,    rtfPixelBits,        "wbmbitspixel",    0,
  1142.     rtfPictAttr,    rtfBitmapPlanes,    "wbmplanes",    0,
  1143.     rtfPictAttr,    rtfBitmapWid,        "wbmwidthbytes", 0,
  1144.     rtfPictAttr,    rtfPicBinary,        "bin",        0,
  1145.  
  1146.     rtfNeXTGrAttr,    rtfNeXTGWidth,        "width",    0,
  1147.     rtfNeXTGrAttr,    rtfNeXTGHeight,        "height",    0,
  1148.  
  1149.     rtfDestination,    rtfPict,        "pict",        0,
  1150.     rtfDestination,    rtfNeXTGraphic,        "NeXTGraphic",    0,
  1151.     rtfDestination,    rtfFootnote,        "footnote",    0,
  1152.     rtfDestination,    rtfHeader,        "header",    0,
  1153.     rtfDestination,    rtfHeaderLeft,        "headerl",    0,
  1154.     rtfDestination,    rtfHeaderRight,        "headerr",    0,
  1155.     rtfDestination,    rtfHeaderFirst,        "headerf",    0,
  1156.     rtfDestination,    rtfFooter,        "footer",    0,
  1157.     rtfDestination,    rtfFooterLeft,        "footerl",    0,
  1158.     rtfDestination,    rtfFooterRight,        "footerr",    0,
  1159.     rtfDestination,    rtfFooterFirst,        "footerf",    0,
  1160.     rtfDestination,    rtfFNSep,        "ftnsep",    0,
  1161.     rtfDestination,    rtfFNContSep,        "ftnsepc",    0,
  1162.     rtfDestination,    rtfFNContNotice,    "ftncn",    0,
  1163.     rtfDestination,    rtfInfo,        "info",        0,
  1164.     rtfDestination,    rtfStyleSheet,        "stylesheet",    0,
  1165.     rtfDestination,    rtfFontTbl,        "fonttbl",    0,
  1166.     rtfDestination,    rtfColorTbl,        "colortbl",    0,
  1167.     rtfDestination,    rtfAnnotation,        "annotation",    0,
  1168.     rtfDestination,    rtfAnnotID,        "atnid",    0,
  1169.     rtfDestination,    rtfField,        "field",    0,
  1170.     rtfDestination,    rtfFieldInst,        "fldinst",    0,
  1171.     rtfDestination,    rtfFieldResult,        "fldrslt",    0,
  1172.     rtfDestination,    rtfIndex,        "xe",        0,
  1173.     rtfDestination,    rtfIndexBold,        "bxe",        0,
  1174.     rtfDestination,    rtfIndexItalic,        "ixe",        0,
  1175.     rtfDestination,    rtfIndexText,        "txe",        0,
  1176.     rtfDestination,    rtfIndexRange,        "rxe",        0,
  1177.     rtfDestination,    rtfTOC,            "tc",        0,
  1178.     rtfDestination,    rtfBookmarkStart,    "bkmkstart",    0,
  1179.     rtfDestination,    rtfBookmarkEnd,        "bkmkend",    0,
  1180.     rtfDestination,    rtfITitle,        "title",    0,
  1181.     rtfDestination,    rtfISubject,        "subject",    0,
  1182.     rtfDestination,    rtfIAuthor,        "author",    0,
  1183.     rtfDestination,    rtfIOperator,        "operator",    0,
  1184.     rtfDestination,    rtfIKeywords,        "keywords",    0,
  1185.     rtfDestination,    rtfIComment,        "comment",    0,
  1186.     rtfDestination,    rtfIVersion,        "version",    0,
  1187.     rtfDestination,    rtfIDoccomm,        "doccomm",    0,
  1188.  
  1189.     rtfTOCAttr,    rtfTOCType,        "tcf",        0,
  1190.     rtfTOCAttr,    rtfTOCLevel,        "tcl",        0,
  1191.  
  1192.     rtfFontFamily,    rtfFFNil,        "fnil",        0,
  1193.     rtfFontFamily,    rtfFFRoman,        "froman",    0,
  1194.     rtfFontFamily,    rtfFFSwiss,        "fswiss",    0,
  1195.     rtfFontFamily,    rtfFFModern,        "fmodern",    0,
  1196.     rtfFontFamily,    rtfFFScript,        "fscript",    0,
  1197.     rtfFontFamily,    rtfFFDecor,        "fdecor",    0,
  1198.     rtfFontFamily,    rtfFFTech,        "ftech",    0,
  1199.  
  1200.     rtfColorName,    rtfRed,            "red",        0,
  1201.     rtfColorName,    rtfGreen,        "green",    0,
  1202.     rtfColorName,    rtfBlue,        "blue",        0,
  1203.  
  1204.     rtfCharSet,    rtfMacCharSet,        "mac",        0,
  1205.     rtfCharSet,    rtfAnsiCharSet,        "ansi",        0,
  1206.     rtfCharSet,    rtfPcCharSet,        "pc",        0,
  1207.     rtfCharSet,    rtfPcaCharSet,        "pca",        0,
  1208.  
  1209.     rtfTblAttr,    rtfCellBordBottom,    "clbrdrb",    0,
  1210.     rtfTblAttr,    rtfCellBordTop,        "clbrdrt",    0,
  1211.     rtfTblAttr,    rtfCellBordLeft,    "clbrdrl",    0,
  1212.     rtfTblAttr,    rtfCellBordRight,    "clbrdrr",    0,
  1213.     rtfTblAttr,    rtfRowDef,        "trowd",    0,
  1214.     rtfTblAttr,    rtfRowLeft,        "trql",        0,
  1215.     rtfTblAttr,    rtfRowRight,        "trqr",        0,
  1216.     rtfTblAttr,    rtfRowCenter,        "trqc",        0,
  1217.     rtfTblAttr,    rtfRowGapH,        "trgaph",    0,
  1218.     rtfTblAttr,    rtfRowHt,        "trrh",        0,
  1219.     rtfTblAttr,    rtfRowLeftEdge,        "trleft",    0,
  1220.     rtfTblAttr,    rtfCellPos,        "cellx",    0,
  1221.     rtfTblAttr,    rtfMergeRngFirst,    "clmgf",    0,
  1222.     rtfTblAttr,    rtfMergePrevious,    "clmrg",    0,
  1223.  
  1224.     rtfFieldAttr,    rtfFieldDirty,        "flddirty",    0,
  1225.     rtfFieldAttr,    rtfFieldEdited,        "fldedit",    0,
  1226.     rtfFieldAttr,    rtfFieldLocked,        "fldlock",    0,
  1227.     rtfFieldAttr,    rtfFieldPrivate,    "fldpriv",    0,
  1228.  
  1229.     rtfPosAttr,    rtfPosX,        "posx",        0,
  1230.     rtfPosAttr,    rtfPosXCenter,        "posxc",    0,
  1231.     rtfPosAttr,    rtfPosXInside,        "posxi",    0,
  1232.     rtfPosAttr,    rtfPosXLeft,        "posxl",    0,
  1233.     rtfPosAttr,    rtfPosXOutSide,        "posxo",    0,
  1234.     rtfPosAttr,    rtfPosXRight,        "posxr",    0,
  1235.     rtfPosAttr,    rtfPosY,        "posy",        0,
  1236.     rtfPosAttr,    rtfPosYInline,        "posyil",    0,
  1237.     rtfPosAttr,    rtfPosYTop,        "posyt",    0,
  1238.     rtfPosAttr,    rtfPosYCenter,        "posyc",    0,
  1239.     rtfPosAttr,    rtfPosYBottom,        "posyb",    0,
  1240.     rtfPosAttr,    rtfAbsWid,        "absw",        0,
  1241.     rtfPosAttr,    rtfTextDist,        "dxfrtext",    0,
  1242.     rtfPosAttr,    rtfRPosMargV,        "pvmrg",    0,
  1243.     rtfPosAttr,    rtfRPosPageV,        "pvpg",        0,
  1244.     rtfPosAttr,    rtfRPosMargH,        "phmrg",    0,
  1245.     rtfPosAttr,    rtfRPosPageH,        "phpg",        0,
  1246.     rtfPosAttr,    rtfRPosColH,        "phcol",    0,
  1247.  
  1248.     rtfVersion,    -1,            "rtf",        0,
  1249.     rtfDefFont,    -1,            "deff",        0,
  1250.  
  1251.     0,        -1,            (char *) NULL,    0
  1252. };
  1253.  
  1254.  
  1255. /*
  1256.     Initialize lookup table hash values.  Only need to do this the
  1257.     first time it's called.
  1258. */
  1259.  
  1260. static void LookupInit ()
  1261. {
  1262. static int    inited = 0;
  1263. RTFKey    *rp;
  1264.  
  1265.     if (inited == 0)
  1266.     {
  1267.         for (rp = rtfKey; rp->rtfKStr != (char *) NULL; rp++)
  1268.             rp->rtfKHash = Hash (rp->rtfKStr);
  1269.         ++inited;
  1270.     }
  1271. }
  1272.  
  1273.  
  1274. /*
  1275.     Determine major and minor number of control token.  If it's
  1276.     not found, the class turns into rtfUnknown.
  1277. */
  1278.  
  1279. static void Lookup (char    *s)
  1280. {
  1281. RTFKey    *rp;
  1282. int    hash;
  1283.  
  1284.     ++s;            /* skip over the leading \ character */
  1285.     hash = Hash (s);
  1286.     for (rp = rtfKey; rp->rtfKStr != (char *) NULL; rp++)
  1287.     {
  1288.         if (hash == rp->rtfKHash && strcmp (s, rp->rtfKStr) == 0)
  1289.         {
  1290.             rtfClass = rtfControl;
  1291.             rtfMajor = rp->rtfKMajor;
  1292.             rtfMinor = rp->rtfKMinor;
  1293.             return;
  1294.         }
  1295.     }
  1296.     rtfClass = rtfUnknown;
  1297. }
  1298.  
  1299.  
  1300. /*
  1301.     Compute hash value of symbol
  1302. */
  1303.  
  1304. static int Hash (char    *s)
  1305. {
  1306. char    c;
  1307. int    val = 0;
  1308.  
  1309.     while ((c = *s++) != '\0')
  1310.         val += (int) c;
  1311.     return (val);
  1312. }
  1313.  
  1314.  
  1315. /*
  1316.     Print helpful error message and give up
  1317. */
  1318.  
  1319. # ifdef    VARARGS
  1320.  
  1321. /*
  1322.     This version is for systems that have varargs.
  1323. */
  1324.  
  1325. static void Error (...)
  1326. {
  1327. va_list    args;
  1328. char    *fmt;
  1329.  
  1330.     va_start (args);
  1331.     fmt = va_arg (args, char *);
  1332.     vfprintf (stderr, fmt, args);
  1333.     va_end (args);
  1334.     fprintf (stderr, "\nLast token read was \"%s\"\n", rtfTextBuf);
  1335.     exit (1);
  1336. }
  1337.  
  1338. # else    /* !VARARGS */
  1339.  
  1340. /*
  1341.     This version is for systems that don't have varargs.
  1342. */
  1343.  
  1344. static void Error ( char *fmt, char *a1,  char *a2,  char *a3,  char *a4,  
  1345.             char *a5,  char *a6,  char *a7,  char *a8,  char *a9)
  1346. {
  1347.     fprintf (stderr, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
  1348.     fprintf (stderr, "\nLast token read was \"%s\"\n", rtfTextBuf);
  1349.     exit (1);
  1350. }
  1351.  
  1352. # endif    /* VARARGS */
  1353.  
  1354.  
  1355. /* ---------------------------------------------------------------------- */
  1356.  
  1357. /*
  1358.     Memory allocation routines
  1359. */
  1360.  
  1361.  
  1362. /*
  1363.     Return pointer to block of size bytes, or NULL if there's
  1364.     not enough memory available.
  1365. */
  1366.  
  1367. char *RTFAlloc (int    size)
  1368. {
  1369.     return ((char *) malloc (size));
  1370. }
  1371.  
  1372.  
  1373. /*
  1374.     Saves a string on the heap and returns a pointer to it.
  1375. */
  1376.  
  1377.  
  1378. char *RTFStrSave (char    *s)
  1379. {
  1380. char    *p;
  1381.  
  1382.     if ((p = RTFAlloc (strlen (s) + 1)) == (char *) NULL)
  1383.         return ((char *) NULL);
  1384.     return (strcpy (p, s));
  1385. }
  1386.  
  1387.  
  1388. void RTFFree (char    *p)
  1389. {
  1390.     if (p != (char *) NULL)
  1391.         free (p);
  1392. }
  1393.  
  1394.  
  1395. /* ---------------------------------------------------------------------- */
  1396.  
  1397.  
  1398. /*
  1399.     Token comparison routines
  1400. */
  1401.  
  1402. int RTFCheckCM (int    class, int major)
  1403. {
  1404.     return (rtfClass == class && rtfMajor == major);
  1405. }
  1406.  
  1407.  
  1408. int RTFCheckCMM ( int    class, int major, int minor)
  1409. {
  1410.     return (rtfClass == class && rtfMajor == major && rtfMinor == minor);
  1411. }
  1412.  
  1413.  
  1414. int RTFCheckMM (int    major, int minor)
  1415. {
  1416.     return (rtfMajor == major && rtfMinor == minor);
  1417. }
  1418.