int rtfClass;
int rtfMajor;
int rtfMinor;
int rtfParam;
char rtfTextBuf[rtfBufSiz];
int rtfTextLen;
/*
Private stuff
*/
static int pushedChar; /* pushback char if read too far */
static int pushedClass; /* pushed token info for RTFUngetToken() */
static int pushedMajor;
static int pushedMinor;
static int pushedParam;
static char pushedTextBuf[rtfBufSiz];
static RTFFont *fontList = (RTFFont *) NULL; /* these lists MUST be */
static RTFColor *colorList = (RTFColor *) NULL; /* initialized to NULL */
static RTFStyle *styleList = (RTFStyle *) NULL;
static FILE *rtffp = stdin;
/*
Initialize the reader. This may be called multiple times,
to read multiple files. The only thing not reset is the input
stream; that must be done with RTFSetStream().
*/
for (i = 0; i < rtfMaxClass; i++)
RTFSetClassCallback (i, (RTFFuncPtr) NULL);
for (i = 0; i < rtfMaxDestination; i++)
RTFSetDestinationCallback (i, (RTFFuncPtr) NULL);
/*
Route a token. If it's a destination for which a reader is
installed, process the destination internally, otherwise
pass the token to the writer's class callback.
*/
void RTFRouteToken ()
{
RTFFuncPtr p;
if (rtfClass < 0 || rtfClass >= rtfMaxClass) /* watchdog */
{
Error ("Unknown class %d: %s (reader malfunction)",
rtfClass, rtfTextBuf);
}
if (RTFCheckCM (rtfControl, rtfDestination))
{
/* invoke destination-specific callback if there is one */
if ((p = RTFGetDestinationCallback (rtfMinor))
!= (RTFFuncPtr) NULL)
{
(*p) ();
return;
}
}
/* invoke class callback if there is one */
if ((p = RTFGetClassCallback (rtfClass)) != (RTFFuncPtr) NULL)
(*p) ();
}
/*
Skip to the end of the current group. When this returns,
writers that maintain a state stack may want to call their
state unstacker; global vars will still be set to the group's
closing brace.
*/
void RTFSkipGroup ()
{
int level = 1;
while (RTFGetToken () != rtfEOF)
{
if (rtfClass == rtfGroup)
{
if (rtfMajor == rtfBeginGroup)
++level;
else if (rtfMajor == rtfEndGroup)
{
if (--level < 1)
break; /* end of initial group */
}
}
}
}
/*
Read one token. Call the read hook if there is one. The
token class is the return value. Returns rtfEOF when there
are no more tokens.
*/
int RTFGetToken ()
{
RTFFuncPtr p;
for (;;)
{
_RTFGetToken ();
if ((p = RTFGetReadHook ()) != (RTFFuncPtr) NULL)
(*p) (); /* give read hook a look at token */
/* control symbol */
Lookup (rtfTextBuf); /* sets class, major, minor */
return;
}
/* control word */
while (isalpha (c))
{
if ((c = GetChar ()) == EOF)
break;
}
/*
At this point, the control word is all collected, so the
major/minor numbers are determined before the parameter
(if any) is scanned. There will be one too many characters
in the buffer, though, so fix up before and restore after
looking up.
*/
if (c != EOF)
rtfTextBuf[rtfTextLen-1] = '\0';
Lookup (rtfTextBuf); /* sets class, major, minor */
if (c != EOF)
rtfTextBuf[rtfTextLen-1] = c;
/*
Should be looking at first digit of parameter if there
is one, unless it's negative. In that case, next char
is '-', so need to gobble next char, and remember sign.
*/
sign = 1;
if (c == '-')
{
sign = -1;
c = GetChar ();
}
if (c != EOF && isdigit (c))
{
rtfParam = 0;
while (isdigit (c)) /* gobble parameter */
{
rtfParam = rtfParam * 10 + c - '0';
if ((c = GetChar ()) == EOF)
break;
}
rtfParam *= sign;
}
/*
If control symbol delimiter was a blank, gobble it.
Otherwise the character is first char of next token, so
push it back for next call. In either case, delete the
delimiter from the token buffer.
*/
if (c != EOF)
{
if (c != ' ')
pushedChar = c;
rtfTextBuf[--rtfTextLen] = '\0';
}
return;
}
/*
Distributions up through 1.04 assumed high bit could be set in
RTF file characters. Beginning with 1.05, that's not true, but
still check and ignore such characters. (Cope with things like
WriteNow on NeXT, which generates bad RTF by writing 8-bit
characters.)
*/
static int GetChar ()
{
int c;
if ((c = getc (rtffp)) != EOF)
{
if (c & 0x80)
{
fprintf (stderr, "Character found with high bit set");
fprintf (stderr, " (%#x) -> changed to '?'\n", c);
c = '?';
}
rtfTextBuf[rtfTextLen] = c;
rtfTextBuf[++rtfTextLen] = '\0';
}
return (c);
}
static int HexVal (c)
char c;
{
if (isupper (c))
c = tolower (c);
if (isdigit (c))
return (c - '0'); /* '0'..'9' */
return (c - 'a' + 10); /* 'a'..'f' */
}
/*
Synthesize a token by setting the global variables to the
values supplied. Typically this is followed with a call
to RTFRouteToken().
If param is non-negative, it becomes part of the token text.
*/
/*
Special destination readers. They gobble the destination so the
writer doesn't have to deal with them. That's wrong for any
translator that wants to process any of these itself. In that
case, these readers should be overridden by installing a different
destination callback.
NOTE: The last token read by each of these reader will be the
destination's terminating '}', which will then be the current token.
That '}' token is passed to RTFRouteToken() - the writer has already
seen the '{' that began the destination group, and may have pushed a
state; it also needs to know at the end of the group that a state
should be popped.
It's important that rtf.h and the control token lookup table list
as many symbols as possible, because these readers unfortunately
make strict assumptions about the input they expect, and a token
of class rtfUnknown will throw them off easily.
*/
/*
Read { \fonttbl ... } destination. Old font tables don't have
braces around each table entry; try to adjust for that.
*/
static void ReadFontTbl ()
{
RTFFont *fp;
char buf[rtfBufSiz], *bp;
int old = -1;
for (;;)
{
(void) RTFGetToken ();
if (RTFCheckCM (rtfGroup, rtfEndGroup))
break;
if (old < 0) /* first entry - determine tbl type */
{
if (RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
old = 1; /* no brace */
else if (RTFCheckCM (rtfGroup, rtfBeginGroup))
old = 0; /* brace */
else /* can't tell! */
Error ("FTErr - Cannot determine format");
}
if (old == 0) /* need to find "{" here */
{
if (!RTFCheckCM (rtfGroup, rtfBeginGroup))
Error ("FTErr - missing \"{\"");
(void) RTFGetToken (); /* yes, skip to next token */
}
if ((fp = New (RTFFont)) == (RTFFont *) NULL)
Error ("FTErr - cannot allocate font entry");
fp->rtfNextFont = fontList;
fontList = fp;
if (!RTFCheckCMM (rtfControl, rtfCharAttr, rtfFontNum))
Error ("FTErr - missing font number");
fp->rtfFNum = rtfParam;
(void) RTFGetToken ();
if (!RTFCheckCM (rtfControl, rtfFontFamily))
Error ("FTErr - missing font family");
fp->rtfFFamily = rtfMinor;
bp = buf;
while (RTFGetToken () == rtfText)
{
if (rtfMajor == ';')
break;
*bp++ = rtfMajor;
}
*bp = '\0';
if (buf[0] == '\0')
Error ("FTErr - missing font name");
if ((fp->rtfFName = RTFStrSave (buf)) == (char *) NULL)
Error ("FTErr - cannot allocate font name");
if (old == 0) /* need to see "}" here */
{
(void) RTFGetToken ();
if (!RTFCheckCM (rtfGroup, rtfEndGroup))
Error ("FTErr - missing \"}\"");
}
}
RTFRouteToken (); /* feed "}" back to router */
}
/*
The color table entries have color values of -1 if
the default color should be used for the entry (only
a semi-colon is given in the definition, no color values).
There will be a problem if a partial entry (1 or 2 but
not 3 color values) is given. The possibility is ignored
here.
*/
/*
Routines to return pieces of stylesheet, or font or color tables
*/
RTFStyle *RTFGetStyle (num)
int num;
{
RTFStyle *s;
if (num == -1)
return (styleList);
for (s = styleList; s != (RTFStyle *) NULL; s = s->rtfNextStyle)
{
if (s->rtfSNum == num)
break;
}
return (s); /* NULL if not found */
}
RTFFont *RTFGetFont (num)
int num;
{
RTFFont *f;
if (num == -1)
return (fontList);
for (f = fontList; f != (RTFFont *) NULL; f = f->rtfNextFont)
{
if (f->rtfFNum == num)
break;
}
return (f); /* NULL if not found */
}
RTFColor *RTFGetColor (num)
int num;
{
RTFColor *c;
if (num == -1)
return (colorList);
for (c = colorList; c != (RTFColor *) NULL; c = c->rtfNextColor)
{
if (c->rtfCNum == num)
break;
}
return (c); /* NULL if not found */
}
void RTFExpandStyle (n)
int n;
{
RTFStyle *s;
RTFStyleElt *se;
if (n == -1 || (s = RTFGetStyle (n)) == (RTFStyle *) NULL)
return;
if (s->rtfExpanding != 0)
Error ("Style expansion loop, style %d", n);
s->rtfExpanding = 1; /* set expansion flag for loop detection */
/*
Expand "based-on" style. This is done by synthesizing
the token that the writer needs to see in order to trigger
another style expansion, and feeding to token back through
the router so the writer sees it.
*/
RTFSetToken (rtfControl, rtfParAttr, rtfStyleNum, s->rtfSBasedOn, "\\s");
RTFRouteToken ();
/*
Now route the tokens unique to this style. RTFSetToken()
isn't used because it would add the param value to the end
of the token text, which already has it in.
*/
for (se = s->rtfSSEList; se != (RTFStyleElt *) NULL; se = se->rtfNextSE)
{
rtfClass = se->rtfSEClass;
rtfMajor = se->rtfSEMajor;
rtfMinor = se->rtfSEMinor;
rtfParam = se->rtfSEParam;
(void) strcpy (rtfTextBuf, se->rtfSEText);
rtfTextLen = strlen (rtfTextBuf);
RTFRouteToken ();
}
s->rtfExpanding = 0; /* done - clear expansion flag */
}
struct RTFKey
{
int rtfKMajor; /* major number */
int rtfKMinor; /* minor number */
char *rtfKStr; /* symbol name */
int rtfKHash; /* symbol name hash value */
};
/*
A minor number of -1 means the token has no minor number
(all valid minor numbers are >= 0).
*/