home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
gnu
/
gs-2.6.1.4-src.lha
/
src
/
amiga
/
gs-2.6.1.4
/
iscan.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-27
|
30KB
|
1,058 lines
/* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises. All rights reserved.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies. */
/* iscan.c */
/* Token scanner for Ghostscript interpreter */
#include "ghost.h"
#include "ctype_.h"
#include "memory_.h"
#include "stream.h"
#include "alloc.h"
#include "dict.h" /* for //name lookup */
#include "dstack.h" /* ditto */
#include "errors.h"
#include "ilevel.h"
#include "iname.h"
#include "iscan.h" /* defines interface */
#include "iutil.h"
#include "ivmspace.h"
#include "ostack.h" /* for accumulating proc bodies */
#include "packed.h"
#include "store.h"
#include "scanchar.h"
/* Array packing flag */
ref ref_array_packing; /* t_boolean */
/* Binary object format flag. This will never be set non-zero */
/* unless the binary token feature is enabled. */
ref ref_binary_object_format; /* t_integer */
#define recognize_btokens()\
(ref_binary_object_format.value.intval != 0 && level2_enabled)
/* Procedure for binary tokens. Set at initialization if Level 2 */
/* features are included; only called if recognize_btokens() is true. */
/* Returns 0 or scan_BOS on success, <0 on failure. */
int (*scan_btoken_proc)(P3(stream *, ref *, int)) = NULL;
/* Setup procedure for ASCII85 literals. Set at initialization if Level 2 */
/* features are included. */
void (*scan_ascii85_setup_proc)(P4(stream *, stream *, byte *, uint)) = NULL;
/*
* Level 2 includes some changes in the scanner:
* - \ is always recognized in strings, regardless of the data source;
* - << and >> are legal tokens;
* - <~ introduces an ASCII85 encoded string (terminated by ~>);
* - Character codes above 127 introduce binary objects.
* We explicitly enable or disable these changes here.
*/
#define scan_enable_level2 level2_enabled /* from ilevel.h */
/* Forward references */
private int scan_ascii85_string(P2(stream *, ref *)),
scan_hex_string(P2(stream *, ref *)),
scan_int(P6(const byte **, const byte *, int, int,
long *, double *)),
scan_number(P3(const byte *, const byte *, ref *)),
scan_string(P3(stream *, int, ref *));
/* Define the character scanning table (see scanchar.h). */
byte scan_char_array[258];
/* A structure for dynamically growable objects */
typedef struct dynamic_area_s {
byte *base;
byte *next;
uint num_elts;
uint elt_size;
int is_dynamic; /* false if using fixed buffer */
byte *limit;
} dynamic_area;
typedef dynamic_area _ss *da_ptr;
/* Begin a dynamic object. */
/* dynamic_begin returns the value of alloc, which may be 0: */
/* the invoker of dynamic_begin must test the value against 0. */
#define dynamic_begin(pda, dnum, desize)\
((pda)->base = (byte *)alloc((pda)->num_elts = (dnum),\
(pda)->elt_size = (desize), "scanner"),\
(pda)->limit = (pda)->base + (dnum) * (desize),\
(pda)->is_dynamic = 1,\
(pda)->next = (pda)->base)
/* Begin a dynamically allocated string in a static buffer. */
#define static_begin_string(pda, count, buf)\
((pda)->num_elts = (count),\
(pda)->elt_size = 1,\
(pda)->is_dynamic = 0,\
(pda)->limit = (buf) + (count),\
(pda)->next = (pda)->base = (buf))
/* Free a dynamic object. */
private void
dynamic_free(da_ptr pda)
{ if ( pda->is_dynamic )
alloc_free((char *)(pda->base), pda->num_elts, pda->elt_size,
"scanner");
}
/* Grow a dynamic object. */
/* If the allocation fails, free the old contents, and return NULL; */
/* otherwise, return the new `next' pointer. */
private byte *
dynamic_grow(register da_ptr pda, byte *next)
{ if ( next != pda->limit ) return next;
pda->next = next;
{ uint num = pda->num_elts;
uint old_size = num * pda->elt_size;
uint pos = pda->next - pda->base;
uint new_size = (old_size < 10 ? 20 :
old_size >= (max_uint >> 1) ? max_uint :
old_size << 1);
uint new_num = new_size / pda->elt_size;
if ( pda->is_dynamic )
{ byte *base = alloc_grow(pda->base, num, new_num, pda->elt_size, "scanner");
if ( base == 0 )
{ dynamic_free(pda);
return NULL;
}
pda->base = base;
pda->num_elts = new_num;
pda->limit = pda->base + new_size;
}
else
{ byte *base = pda->base;
if ( !dynamic_begin(pda, new_num, pda->elt_size) ) return NULL;
memcpy(pda->base, base, old_size);
pda->is_dynamic = 1;
}
pda->next = pda->base + pos;
}
return pda->next;
}
/* Initialize the scanner. */
void
scan_init(void)
{ /* Initialize decoder array */
register byte _ds *decoder = scan_char_decoder;
static const char _ds *stop_chars = "()<>[]{}/%";
static const char _ds *space_chars = " \f\t\n\r";
decoder[ERRC] = ctype_eof; /* ****** FIX THIS? ****** */
decoder[EOFC] = ctype_eof;
memset(decoder, ctype_name, 256);
memset(decoder + 128, ctype_btoken, 32);
{ register const char _ds *p;
for ( p = space_chars; *p; p++ )
decoder[*p] = ctype_space;
decoder[char_NULL] = decoder[char_VT] =
decoder[char_DOS_EOF] = ctype_space;
for ( p = stop_chars; *p; p++ )
decoder[*p] = ctype_other;
}
{ register int i;
for ( i = 0; i < 10; i++ )
decoder['0' + i] = i;
for ( i = 0; i < max_radix - 10; i++ )
decoder['A' + i] = decoder['a' + i] = i + 10;
}
/* Other initialization */
make_false(&ref_array_packing);
make_int(&ref_binary_object_format, 0);
}
/*
* Read a token from a stream.
* Return 1 for end-of-stream, 0 if a token was read,
* or a (negative) error code.
* If the token required a terminating character (i.e., was a name or
* number) and the next character was whitespace, read and discard
* that character: see the description of the 'token' operator on
* p. 232 of the Red Book (First Edition).
* from_string indicates reading from a string vs. a file,
* because Level 1 interpreters ignore \ escapes in the former case.
* (See the footnote on p. 23 of the Red Book.)
*/
int
scan_token(register stream *s, int from_string, ref *pref)
{ ref *myref = pref;
dynamic_area proc_da; /* (not actually dynamic) */
int pstack = 0; /* offset from proc_da.base */
int retcode = 0;
register int c;
s_declare_inline(s, sptr, endptr);
#define sreturn(code)\
{ s_end_inline(s, sptr, endptr); return_error(code); }
#define sreturn_no_error(code)\
{ s_end_inline(s, sptr, endptr); return(code); }
int name_type; /* number of /'s preceding */
int max_name_ctype =
(recognize_btokens() ? ctype_name : ctype_btoken);
int try_number;
byte s1[2];
register byte _ds *decoder = scan_char_decoder;
s_begin_inline(s, sptr, endptr);
top: c = sgetc_inline(s, sptr, endptr);
#ifdef DEBUG
if ( gs_debug['s'] )
fprintf(gs_debug_out, (c >= 32 && c <= 126 ? "`%c'" : "`%03o'"), c);
#endif
switch ( c )
{
case ' ': case '\f': case '\t': case '\n': case '\r':
case char_NULL: case char_VT: case char_DOS_EOF:
goto top;
case '[':
case ']':
s1[0] = (byte)c;
name_ref(s1, 1, myref, 1);
r_set_attrs(myref, a_executable);
break;
case '<':
if ( scan_enable_level2 )
{ c = sgetc_inline(s, sptr, endptr);
switch ( c )
{
case '<':
sputback_inline(s, sptr, endptr);
name_type = try_number = 0;
goto try_funny_name;
case '~':
s_end_inline(s, sptr, endptr);
retcode = scan_ascii85_string(s, myref);
goto sx;
}
if ( char_is_data(c) )
sputback_inline(s, sptr, endptr);
}
s_end_inline(s, sptr, endptr);
retcode = scan_hex_string(s, myref);
sx: s_begin_inline(s, sptr, endptr);
break;
case '(':
s_end_inline(s, sptr, endptr);
retcode = sc