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